blob: 5b1cdd99b06bfe04acab75c3b7c5b8506df9d74c [file] [log] [blame]
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2010-2011 Intel Corporation
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04005 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040014 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040015 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040022 */
23
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +080024#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040028#include <stddef.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040029#include <stdlib.h>
30#include <string.h>
31#include <fcntl.h>
32#include <unistd.h>
33#include <errno.h>
34#include <sys/time.h>
Kristian Høgsbergf9112b22010-06-14 12:53:43 -040035#include <linux/input.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040036
37#include <xcb/xcb.h>
Daniel Stone62b33b62012-06-22 13:21:35 +010038#ifdef HAVE_XCB_XKB
39#include <xcb/xkb.h>
40#endif
41
Benjamin Franzke3b288af2011-02-20 19:58:42 +010042#include <X11/Xlib.h>
43#include <X11/Xlib-xcb.h>
44
Daniel Stone576cd152012-06-01 12:14:02 +010045#include <xkbcommon/xkbcommon.h>
46
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040047#include <GLES2/gl2.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040048#include <EGL/egl.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040049
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040050#include "compositor.h"
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040051#include "../shared/config-parser.h"
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040052
Scott Moreau248aaec2012-08-03 14:19:52 -060053static char *output_name;
54static char *output_mode;
Scott Moreau1bad5db2012-08-18 01:04:05 -060055static char *output_transform;
Scott Moreau248aaec2012-08-03 14:19:52 -060056static int option_width;
57static int option_height;
58static int option_count;
59static struct wl_list configured_output_list;
60
61struct x11_configured_output {
62 char *name;
63 int width, height;
Scott Moreau1bad5db2012-08-18 01:04:05 -060064 uint32_t transform;
Scott Moreau248aaec2012-08-03 14:19:52 -060065 struct wl_list link;
66};
67
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040068struct x11_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050069 struct weston_compositor base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040070
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040071 EGLSurface dummy_pbuffer;
72
Benjamin Franzke3b288af2011-02-20 19:58:42 +010073 Display *dpy;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040074 xcb_connection_t *conn;
75 xcb_screen_t *screen;
76 xcb_cursor_t null_cursor;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050077 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040078 struct wl_event_source *xcb_source;
Daniel Stone576cd152012-06-01 12:14:02 +010079 struct xkb_keymap *xkb_keymap;
Daniel Stone62b33b62012-06-22 13:21:35 +010080 unsigned int has_xkb;
81 uint8_t xkb_event_base;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040082
83 /* We could map multi-pointer X to multiple wayland seats, but
84 * for now we only support core X input. */
85 struct weston_seat core_seat;
86
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040087 struct {
88 xcb_atom_t wm_protocols;
89 xcb_atom_t wm_normal_hints;
90 xcb_atom_t wm_size_hints;
91 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050092 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040093 xcb_atom_t net_wm_name;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050094 xcb_atom_t net_wm_icon;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -040095 xcb_atom_t net_wm_state;
96 xcb_atom_t net_wm_state_fullscreen;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050097 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040098 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050099 xcb_atom_t cardinal;
Daniel Stone855028f2012-05-01 20:37:10 +0100100 xcb_atom_t xkb_names;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400101 } atom;
102};
103
104struct x11_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500105 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400106
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400107 xcb_window_t window;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100108 EGLSurface egl_surface;
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
Daniel Stone62b33b62012-06-22 13:21:35 +0100156static void
157x11_compositor_setup_xkb(struct x11_compositor *c)
158{
159#ifndef HAVE_XCB_XKB
160 weston_log("XCB-XKB not available during build\n");
161 c->has_xkb = 0;
162 c->xkb_event_base = 0;
163 return;
164#else
165 const xcb_query_extension_reply_t *ext;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100166 xcb_generic_error_t *error;
Daniel Stonee2faa122012-06-22 13:21:39 +0100167 xcb_void_cookie_t select;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100168 xcb_xkb_per_client_flags_cookie_t pcf;
169 xcb_xkb_per_client_flags_reply_t *pcf_reply;
Daniel Stone62b33b62012-06-22 13:21:35 +0100170
171 c->has_xkb = 0;
172 c->xkb_event_base = 0;
173
174 ext = xcb_get_extension_data(c->conn, &xcb_xkb_id);
175 if (!ext) {
176 weston_log("XKB extension not available on host X11 server\n");
177 return;
178 }
179 c->xkb_event_base = ext->first_event;
180
Daniel Stonee2faa122012-06-22 13:21:39 +0100181 select = xcb_xkb_select_events(c->conn,
182 XCB_XKB_ID_USE_CORE_KBD,
183 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
184 0,
185 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
186 0,
187 0,
188 NULL);
189 error = xcb_request_check(c->conn, select);
190 if (error) {
191 weston_log("error: failed to select for XKB state events\n");
192 return;
193 }
194
Daniel Stone3ee91e12012-06-22 13:21:36 +0100195 pcf = xcb_xkb_per_client_flags(c->conn,
196 XCB_XKB_ID_USE_CORE_KBD,
197 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
198 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
199 0,
200 0,
201 0);
202 pcf_reply = xcb_xkb_per_client_flags_reply(c->conn, pcf, &error);
203 free(pcf_reply);
204 if (error) {
205 weston_log("failed to set XKB per-client flags, not using "
206 "detectable repeat\n");
207 return;
208 }
209
Daniel Stone62b33b62012-06-22 13:21:35 +0100210 c->has_xkb = 1;
211#endif
212}
213
Darxus55973f22010-11-22 21:24:39 -0500214static int
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300215x11_input_create(struct x11_compositor *c, int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400216{
Daniel Stone576cd152012-06-01 12:14:02 +0100217 struct xkb_keymap *keymap;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400218
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400219 weston_seat_init(&c->core_seat, &c->base);
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300220
221 if (no_input)
222 return 0;
223
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400224 weston_seat_init_pointer(&c->core_seat);
Daniel Stone576cd152012-06-01 12:14:02 +0100225
Daniel Stone62b33b62012-06-22 13:21:35 +0100226 x11_compositor_setup_xkb(c);
227
Daniel Stone576cd152012-06-01 12:14:02 +0100228 keymap = x11_compositor_get_keymap(c);
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400229 weston_seat_init_keyboard(&c->core_seat, keymap);
Daniel Stone576cd152012-06-01 12:14:02 +0100230 if (keymap)
231 xkb_map_unref(keymap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400232
Darxus55973f22010-11-22 21:24:39 -0500233 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400234}
235
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200236static void
237x11_input_destroy(struct x11_compositor *compositor)
238{
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400239 weston_seat_release(&compositor->core_seat);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200240}
241
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400242static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400243x11_compositor_init_egl(struct x11_compositor *c)
244{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400245 EGLint major, minor;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100246 EGLint n;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100247 EGLint config_attribs[] = {
248 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
249 EGL_RED_SIZE, 1,
250 EGL_GREEN_SIZE, 1,
251 EGL_BLUE_SIZE, 1,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100252 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
253 EGL_NONE
254 };
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400255 static const EGLint context_attribs[] = {
256 EGL_CONTEXT_CLIENT_VERSION, 2,
257 EGL_NONE
258 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400259
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400260 static const EGLint pbuffer_attribs[] = {
261 EGL_WIDTH, 10,
262 EGL_HEIGHT, 10,
263 EGL_NONE
264 };
265
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400266 c->base.egl_display = eglGetDisplay(c->dpy);
267 if (c->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200268 weston_log("failed to create display\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400269 return -1;
270 }
271
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400272 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200273 weston_log("failed to initialize display\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400274 return -1;
275 }
276
Darxus55973f22010-11-22 21:24:39 -0500277 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200278 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Darxus55973f22010-11-22 21:24:39 -0500279 return -1;
280 }
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400281 if (!eglChooseConfig(c->base.egl_display, config_attribs,
282 &c->base.egl_config, 1, &n) || n == 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200283 weston_log("failed to choose config: %d\n", n);
Benjamin Franzke84290d02011-03-02 10:07:59 +0100284 return -1;
285 }
Darxus55973f22010-11-22 21:24:39 -0500286
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400287 c->base.egl_context =
288 eglCreateContext(c->base.egl_display, c->base.egl_config,
289 EGL_NO_CONTEXT, context_attribs);
290 if (c->base.egl_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200291 weston_log("failed to create context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400292 return -1;
293 }
294
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400295 c->dummy_pbuffer = eglCreatePbufferSurface(c->base.egl_display,
296 c->base.egl_config,
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400297 pbuffer_attribs);
Kristian Høgsbergc0bc8442012-05-17 09:40:23 -0400298 if (c->dummy_pbuffer == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200299 weston_log("failed to create dummy pbuffer\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400300 return -1;
301 }
302
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400303 if (!eglMakeCurrent(c->base.egl_display, c->dummy_pbuffer,
304 c->dummy_pbuffer, c->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200305 weston_log("failed to make context current\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400306 return -1;
307 }
308
309 return 0;
310}
311
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200312static void
313x11_compositor_fini_egl(struct x11_compositor *compositor)
314{
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400315 eglMakeCurrent(compositor->base.egl_display,
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200316 EGL_NO_SURFACE, EGL_NO_SURFACE,
317 EGL_NO_CONTEXT);
318
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400319 eglTerminate(compositor->base.egl_display);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200320 eglReleaseThread();
321}
322
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500323static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500324x11_output_repaint(struct weston_output *output_base,
325 pixman_region32_t *damage)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500326{
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500327 struct x11_output *output = (struct x11_output *)output_base;
328 struct x11_compositor *compositor =
329 (struct x11_compositor *)output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500330 struct weston_surface *surface;
331
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400332 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
333 output->egl_surface,
334 compositor->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200335 weston_log("failed to make current\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500336 return;
337 }
338
339 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500340 weston_surface_draw(surface, &output->base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500341
Kristian Høgsberge0f832b2012-06-20 00:13:18 -0400342 wl_signal_emit(&output->base.frame_signal, output);
Scott Moreau062be7e2012-04-20 13:37:33 -0600343
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400344 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500345
346 wl_event_source_timer_update(output->finish_frame_timer, 10);
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500347}
348
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100349static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400350finish_frame_handler(void *data)
351{
352 struct x11_output *output = data;
353 uint32_t msec;
354 struct timeval tv;
355
356 gettimeofday(&tv, NULL);
357 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500358 weston_output_finish_frame(&output->base, msec);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400359
360 return 1;
361}
362
Matt Roper361d2ad2011-08-29 13:52:23 -0700363static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500364x11_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700365{
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200366 struct x11_output *output = (struct x11_output *)output_base;
367 struct x11_compositor *compositor =
368 (struct x11_compositor *)output->base.compositor;
369
370 wl_list_remove(&output->base.link);
371 wl_event_source_remove(output->finish_frame_timer);
372
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400373 eglDestroySurface(compositor->base.egl_display, output->egl_surface);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200374
375 xcb_destroy_window(compositor->conn, output->window);
376
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500377 weston_output_destroy(&output->base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200378
379 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700380}
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200381
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400382static void
383x11_output_set_wm_protocols(struct x11_output *output)
384{
385 xcb_atom_t list[1];
386 struct x11_compositor *c =
387 (struct x11_compositor *) output->base.compositor;
388
389 list[0] = c->atom.wm_delete_window;
390 xcb_change_property (c->conn,
391 XCB_PROP_MODE_REPLACE,
392 output->window,
393 c->atom.wm_protocols,
394 XCB_ATOM_ATOM,
395 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500396 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400397 list);
398}
399
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400400static void
401x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state)
402{
403 xcb_client_message_event_t event;
404 struct x11_compositor *c =
405 (struct x11_compositor *) output->base.compositor;
406 xcb_screen_iterator_t iter;
407
408#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
409#define _NET_WM_STATE_ADD 1 /* add/set property */
410#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
411
412 memset(&event, 0, sizeof event);
413 event.response_type = XCB_CLIENT_MESSAGE;
414 event.format = 32;
415 event.window = output->window;
416 event.type = c->atom.net_wm_state;
417
418 event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
419 event.data.data32[1] = state;
420 event.data.data32[2] = 0;
421 event.data.data32[3] = 0;
422 event.data.data32[4] = 0;
423
424 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
425 xcb_send_event(c->conn, 0, iter.data->root,
426 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
427 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
428 (void *) &event);
429}
430
431
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400432struct wm_normal_hints {
433 uint32_t flags;
434 uint32_t pad[4];
435 int32_t min_width, min_height;
436 int32_t max_width, max_height;
437 int32_t width_inc, height_inc;
438 int32_t min_aspect_x, min_aspect_y;
439 int32_t max_aspect_x, max_aspect_y;
440 int32_t base_width, base_height;
441 int32_t win_gravity;
442};
443
444#define WM_NORMAL_HINTS_MIN_SIZE 16
445#define WM_NORMAL_HINTS_MAX_SIZE 32
446
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500447static void
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400448x11_output_set_icon(struct x11_compositor *c,
449 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500450{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400451 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400452 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400453 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500454
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400455 image = load_image(filename);
456 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500457 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400458 width = pixman_image_get_width(image);
459 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500460 icon = malloc(width * height * 4 + 8);
461 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400462 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500463 return;
464 }
465
466 icon[0] = width;
467 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400468 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500469 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
470 c->atom.net_wm_icon, c->atom.cardinal, 32,
471 width * height + 2, icon);
472 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400473 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500474}
475
Scott Moreau1bad5db2012-08-18 01:04:05 -0600476static struct x11_output *
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700477x11_compositor_create_output(struct x11_compositor *c, int x, int y,
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300478 int width, int height, int fullscreen,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600479 int no_input, char *configured_name,
480 uint32_t transform)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400481{
Scott Moreau1bad5db2012-08-18 01:04:05 -0600482 static const char name[] = "Weston Compositor";
Kristian Høgsberg86000402012-01-03 23:24:14 -0500483 static const char class[] = "weston-1\0Weston Compositor";
Scott Moreau1bad5db2012-08-18 01:04:05 -0600484 char title[32];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400485 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400486 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400487 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400488 struct wl_event_loop *loop;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400489 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300490 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400491 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300492 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400493 0
494 };
495
Scott Moreau1bad5db2012-08-18 01:04:05 -0600496 if (configured_name)
497 sprintf(title, "%s - %s", name, configured_name);
498 else
499 strcpy(title, name);
500
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300501 if (!no_input)
502 values[0] |=
503 XCB_EVENT_MASK_KEY_PRESS |
504 XCB_EVENT_MASK_KEY_RELEASE |
505 XCB_EVENT_MASK_BUTTON_PRESS |
506 XCB_EVENT_MASK_BUTTON_RELEASE |
507 XCB_EVENT_MASK_POINTER_MOTION |
508 XCB_EVENT_MASK_ENTER_WINDOW |
509 XCB_EVENT_MASK_LEAVE_WINDOW |
510 XCB_EVENT_MASK_KEYMAP_STATE |
511 XCB_EVENT_MASK_FOCUS_CHANGE;
512
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400513 output = malloc(sizeof *output);
514 if (output == NULL)
Scott Moreau1bad5db2012-08-18 01:04:05 -0600515 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400516
517 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400518
519 output->mode.flags =
520 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
521 output->mode.width = width;
522 output->mode.height = height;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400523 output->mode.refresh = 60000;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400524 wl_list_init(&output->base.mode_list);
525 wl_list_insert(&output->base.mode_list, &output->mode.link);
526
527 output->base.current = &output->mode;
Kristian Høgsberg12481582012-07-02 21:24:57 -0400528 output->base.make = "xwayland";
529 output->base.model = "none";
Scott Moreau1bad5db2012-08-18 01:04:05 -0600530 weston_output_init(&output->base, &c->base,
531 x, y, width, height, transform);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400532
533 values[1] = c->null_cursor;
534 output->window = xcb_generate_id(c->conn);
535 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
536 xcb_create_window(c->conn,
537 XCB_COPY_FROM_PARENT,
538 output->window,
539 iter.data->root,
540 0, 0,
541 width, height,
542 0,
543 XCB_WINDOW_CLASS_INPUT_OUTPUT,
544 iter.data->root_visual,
545 mask, values);
546
547 /* Don't resize me. */
548 memset(&normal_hints, 0, sizeof normal_hints);
549 normal_hints.flags =
550 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
551 normal_hints.min_width = width;
552 normal_hints.min_height = height;
553 normal_hints.max_width = width;
554 normal_hints.max_height = height;
555 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
556 c->atom.wm_normal_hints,
557 c->atom.wm_size_hints, 32,
558 sizeof normal_hints / 4,
559 (uint8_t *) &normal_hints);
560
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400561 /* Set window name. Don't bother with non-EWMH WMs. */
562 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
563 c->atom.net_wm_name, c->atom.utf8_string, 8,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600564 strlen(title), title);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500565 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
566 c->atom.wm_class, c->atom.string, 8,
567 sizeof class, class);
568
Kristian Høgsberg86000402012-01-03 23:24:14 -0500569 x11_output_set_icon(c, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500570
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500571 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400572
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400573 x11_output_set_wm_protocols(output);
574
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400575 if (fullscreen)
576 x11_output_change_state(output, 1,
577 c->atom.net_wm_state_fullscreen);
578
Benjamin Franzke84290d02011-03-02 10:07:59 +0100579 output->egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400580 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100581 output->window, NULL);
582 if (!output->egl_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200583 weston_log("failed to create window surface\n");
Scott Moreau1bad5db2012-08-18 01:04:05 -0600584 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400585 }
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400586 if (!eglMakeCurrent(c->base.egl_display, output->egl_surface,
587 output->egl_surface, c->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200588 weston_log("failed to make surface current\n");
Scott Moreau1bad5db2012-08-18 01:04:05 -0600589 return NULL;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100590 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400591
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400592 loop = wl_display_get_event_loop(c->base.wl_display);
593 output->finish_frame_timer =
594 wl_event_loop_add_timer(loop, finish_frame_handler, output);
595
Alex Wubd3354b2012-04-17 17:20:49 +0800596 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500597 output->base.repaint = x11_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700598 output->base.destroy = x11_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800599 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200600 output->base.set_backlight = NULL;
601 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800602 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100603
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400604 wl_list_insert(c->base.output_list.prev, &output->base.link);
605
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400606 weston_log("x11 output %dx%d, window id %d\n",
607 width, height, output->window);
608
Scott Moreau1bad5db2012-08-18 01:04:05 -0600609 return output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400610}
611
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400612static struct x11_output *
613x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
614{
615 struct x11_output *output;
616
617 wl_list_for_each(output, &c->base.output_list, base.link) {
618 if (output->window == window)
619 return output;
620 }
621
622 return NULL;
623}
624
Daniel Stonee2faa122012-06-22 13:21:39 +0100625static uint32_t
626get_xkb_mod_mask(struct x11_compositor *c, uint32_t in)
627{
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400628 struct weston_xkb_info *info = &c->core_seat.xkb_info;
Daniel Stonee2faa122012-06-22 13:21:39 +0100629 uint32_t ret = 0;
630
631 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
632 ret |= (1 << info->shift_mod);
633 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
634 ret |= (1 << info->caps_mod);
635 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
636 ret |= (1 << info->ctrl_mod);
637 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
638 ret |= (1 << info->alt_mod);
639 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
640 ret |= (1 << info->mod2_mod);
641 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
642 ret |= (1 << info->mod3_mod);
643 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
644 ret |= (1 << info->super_mod);
645 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
646 ret |= (1 << info->mod5_mod);
647
648 return ret;
649}
650
Daniel Stone154c34c2012-06-22 13:21:40 +0100651#ifdef HAVE_XCB_XKB
Daniel Stonee2faa122012-06-22 13:21:39 +0100652static void
653update_xkb_state(struct x11_compositor *c, xcb_xkb_state_notify_event_t *state)
654{
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400655 xkb_state_update_mask(c->core_seat.xkb_state.state,
Daniel Stonee2faa122012-06-22 13:21:39 +0100656 get_xkb_mod_mask(c, state->baseMods),
657 get_xkb_mod_mask(c, state->latchedMods),
658 get_xkb_mod_mask(c, state->lockedMods),
659 0,
660 0,
661 state->group);
662
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400663 notify_modifiers(&c->core_seat,
664 wl_display_next_serial(c->base.wl_display));
Daniel Stonee2faa122012-06-22 13:21:39 +0100665}
666#endif
667
Daniel Stone154c34c2012-06-22 13:21:40 +0100668/**
669 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
670 * the best we can do (given that XCB also lacks XI2 support), is to take
671 * the state from the core key events. Unfortunately that only gives us
672 * the effective (i.e. union of depressed/latched/locked) state, and we
673 * need the granularity.
674 *
675 * So we still update the state with every key event we see, but also use
676 * the state field from X11 events as a mask so we don't get any stuck
677 * modifiers.
678 */
679static void
680update_xkb_state_from_core(struct x11_compositor *c, uint16_t x11_mask)
681{
682 uint32_t mask = get_xkb_mod_mask(c, x11_mask);
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400683 struct wl_keyboard *keyboard = &c->core_seat.keyboard;
Daniel Stone154c34c2012-06-22 13:21:40 +0100684
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400685 xkb_state_update_mask(c->core_seat.xkb_state.state,
Daniel Stone154c34c2012-06-22 13:21:40 +0100686 keyboard->modifiers.mods_depressed & mask,
687 keyboard->modifiers.mods_latched & mask,
688 keyboard->modifiers.mods_locked & mask,
689 0,
690 0,
691 (x11_mask >> 13) & 3);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400692 notify_modifiers(&c->core_seat,
Daniel Stone154c34c2012-06-22 13:21:40 +0100693 wl_display_next_serial(c->base.wl_display));
694}
695
Tiago Vignattia3a71622011-12-08 17:03:17 +0200696static void
697x11_compositor_deliver_button_event(struct x11_compositor *c,
698 xcb_generic_event_t *event, int state)
699{
700 xcb_button_press_event_t *button_event =
701 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +0100702 uint32_t button;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200703
Daniel Stone154c34c2012-06-22 13:21:40 +0100704 if (!c->has_xkb)
705 update_xkb_state_from_core(c, button_event->state);
706
Tiago Vignattia3a71622011-12-08 17:03:17 +0200707 switch (button_event->detail) {
708 default:
709 button = button_event->detail + BTN_LEFT - 1;
710 break;
711 case 2:
712 button = BTN_MIDDLE;
713 break;
714 case 3:
715 button = BTN_RIGHT;
716 break;
717 case 4:
Scott Moreau210d0792012-03-22 10:47:01 -0600718 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400719 notify_axis(&c->core_seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600720 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100721 WL_POINTER_AXIS_VERTICAL_SCROLL,
722 wl_fixed_from_int(1));
Scott Moreau210d0792012-03-22 10:47:01 -0600723 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200724 case 5:
Scott Moreau210d0792012-03-22 10:47:01 -0600725 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400726 notify_axis(&c->core_seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600727 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100728 WL_POINTER_AXIS_VERTICAL_SCROLL,
729 wl_fixed_from_int(-1));
Scott Moreau210d0792012-03-22 10:47:01 -0600730 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200731 case 6:
Scott Moreau210d0792012-03-22 10:47:01 -0600732 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400733 notify_axis(&c->core_seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600734 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100735 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
736 wl_fixed_from_int(1));
Scott Moreau210d0792012-03-22 10:47:01 -0600737 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200738 case 7:
Scott Moreau210d0792012-03-22 10:47:01 -0600739 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400740 notify_axis(&c->core_seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600741 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100742 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
743 wl_fixed_from_int(-1));
Tiago Vignattia3a71622011-12-08 17:03:17 +0200744 return;
745 }
746
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400747 notify_button(&c->core_seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100748 weston_compositor_get_time(), button,
749 state ? WL_POINTER_BUTTON_STATE_PRESSED :
750 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200751}
752
Scott Moreau1bad5db2012-08-18 01:04:05 -0600753static void
754x11_output_transform_coordinate(struct x11_output *x11_output,
755 wl_fixed_t *x, wl_fixed_t *y)
756{
757 struct weston_output *output = &x11_output->base;
758 wl_fixed_t tx, ty;
759 wl_fixed_t width = wl_fixed_from_int(output->width - 1);
760 wl_fixed_t height = wl_fixed_from_int(output->height - 1);
761
762 switch(output->transform) {
763 case WL_OUTPUT_TRANSFORM_NORMAL:
764 default:
765 tx = *x;
766 ty = *y;
767 break;
768 case WL_OUTPUT_TRANSFORM_90:
769 tx = *y;
770 ty = height - *x;
771 break;
772 case WL_OUTPUT_TRANSFORM_180:
773 tx = width - *x;
774 ty = height - *y;
775 break;
776 case WL_OUTPUT_TRANSFORM_270:
777 tx = width - *y;
778 ty = *x;
779 break;
780 case WL_OUTPUT_TRANSFORM_FLIPPED:
781 tx = width - *x;
782 ty = *y;
783 break;
784 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
785 tx = width - *y;
786 ty = height - *x;
787 break;
788 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
789 tx = *x;
790 ty = height - *y;
791 break;
792 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
793 tx = *y;
794 ty = *x;
795 break;
796 }
797
798 tx += wl_fixed_from_int(output->x);
799 ty += wl_fixed_from_int(output->y);
800
801 *x = tx;
802 *y = ty;
803}
804
805static void
806x11_compositor_deliver_motion_event(struct x11_compositor *c,
807 xcb_generic_event_t *event)
808{
809 struct x11_output *output;
810 wl_fixed_t x, y;
811 xcb_motion_notify_event_t *motion_notify =
812 (xcb_motion_notify_event_t *) event;
813
814 if (!c->has_xkb)
815 update_xkb_state_from_core(c, motion_notify->state);
816 output = x11_compositor_find_output(c, motion_notify->event);
817 x = wl_fixed_from_int(motion_notify->event_x);
818 y = wl_fixed_from_int(motion_notify->event_y);
819 x11_output_transform_coordinate(output, &x, &y);
820
821 notify_motion(&c->core_seat, weston_compositor_get_time(), x, y);
822}
823
824static void
825x11_compositor_deliver_enter_event(struct x11_compositor *c,
826 xcb_generic_event_t *event)
827{
828 struct x11_output *output;
829 wl_fixed_t x, y;
830
831 xcb_enter_notify_event_t *enter_notify =
832 (xcb_enter_notify_event_t *) event;
833 if (enter_notify->state >= Button1Mask)
834 return;
835 if (!c->has_xkb)
836 update_xkb_state_from_core(c, enter_notify->state);
837 output = x11_compositor_find_output(c, enter_notify->event);
838 x = wl_fixed_from_int(enter_notify->event_x);
839 y = wl_fixed_from_int(enter_notify->event_y);
840 x11_output_transform_coordinate(output, &x, &y);
841
842 notify_pointer_focus(&c->core_seat, &output->base, x, y);
843}
844
Kristian Høgsbergee724822011-04-21 14:51:44 -0400845static int
846x11_compositor_next_event(struct x11_compositor *c,
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400847 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -0400848{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400849 if (mask & WL_EVENT_READABLE) {
850 *event = xcb_poll_for_event(c->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -0400851 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400852#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400853 *event = xcb_poll_for_queued_event(c->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400854#else
855 *event = xcb_poll_for_event(c->conn);
856#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -0400857 }
858
859 return *event != NULL;
860}
861
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400862static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400863x11_compositor_handle_event(int fd, uint32_t mask, void *data)
864{
865 struct x11_compositor *c = data;
866 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400867 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400868 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500869 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400870 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500871 xcb_keymap_notify_event_t *keymap_notify;
872 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400873 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400874 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500875 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400876 uint32_t i, set;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400877 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400878
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400879 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400880 count = 0;
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400881 while (x11_compositor_next_event(c, &event, mask)) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400882 switch (prev ? prev->response_type & ~0x80 : 0x80) {
883 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100884 /* Suppress key repeat events; this is only used if we
885 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400886 key_release = (xcb_key_press_event_t *) prev;
887 key_press = (xcb_key_press_event_t *) event;
888 if ((event->response_type & ~0x80) == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -0700889 key_release->time == key_press->time &&
890 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400891 /* Don't deliver the held key release
892 * event or the new key press event. */
893 free(event);
894 free(prev);
895 prev = NULL;
896 continue;
897 } else {
898 /* Deliver the held key release now
899 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400900 * event below. */
Daniel Stone154c34c2012-06-22 13:21:40 +0100901 update_xkb_state_from_core(c, key_release->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400902 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500903 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100904 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100905 WL_KEYBOARD_KEY_STATE_RELEASED,
906 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400907 free(prev);
908 prev = NULL;
909 break;
910 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400911
912 case XCB_FOCUS_IN:
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400913 /* assert event is keymap_notify */
914 focus_in = (xcb_focus_in_event_t *) prev;
915 keymap_notify = (xcb_keymap_notify_event_t *) event;
916 c->keys.size = 0;
917 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
918 set = keymap_notify->keys[i >> 3] &
919 (1 << (i & 7));
920 if (set) {
921 k = wl_array_add(&c->keys, sizeof *k);
922 *k = i;
923 }
924 }
925
926 output = x11_compositor_find_output(c, focus_in->event);
Daniel Stoned6da09e2012-06-22 13:21:29 +0100927 /* Unfortunately the state only comes with the enter
928 * event, rather than with the focus event. I'm not
929 * sure of the exact semantics around it and whether
930 * we can ensure that we get both? */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400931 notify_keyboard_focus_in(&c->core_seat, &c->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +0100932 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400933
934 free(prev);
935 prev = NULL;
936 break;
937
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400938 default:
939 /* No previous event held */
940 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400941 }
942
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400943 switch (event->response_type & ~0x80) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400944 case XCB_KEY_PRESS:
945 key_press = (xcb_key_press_event_t *) event;
Daniel Stone154c34c2012-06-22 13:21:40 +0100946 if (!c->has_xkb)
947 update_xkb_state_from_core(c, key_press->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400948 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500949 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100950 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100951 WL_KEYBOARD_KEY_STATE_PRESSED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100952 c->has_xkb ? STATE_UPDATE_NONE :
953 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400954 break;
955 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100956 /* If we don't have XKB, we need to use the lame
957 * autorepeat detection above. */
958 if (!c->has_xkb) {
959 prev = event;
960 break;
961 }
962 key_release = (xcb_key_press_event_t *) event;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400963 notify_key(&c->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +0100964 weston_compositor_get_time(),
965 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100966 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100967 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400968 break;
969 case XCB_BUTTON_PRESS:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200970 x11_compositor_deliver_button_event(c, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400971 break;
972 case XCB_BUTTON_RELEASE:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200973 x11_compositor_deliver_button_event(c, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400974 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400975 case XCB_MOTION_NOTIFY:
Scott Moreau1bad5db2012-08-18 01:04:05 -0600976 x11_compositor_deliver_motion_event(c, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400977 break;
978
979 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400980 expose = (xcb_expose_event_t *) event;
981 output = x11_compositor_find_output(c, expose->window);
982 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400983 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400984
985 case XCB_ENTER_NOTIFY:
Scott Moreau1bad5db2012-08-18 01:04:05 -0600986 x11_compositor_deliver_enter_event(c, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400987 break;
988
989 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500990 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500991 if (enter_notify->state >= Button1Mask)
992 break;
Daniel Stone154c34c2012-06-22 13:21:40 +0100993 if (!c->has_xkb)
994 update_xkb_state_from_core(c, enter_notify->state);
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700995 output = x11_compositor_find_output(c, enter_notify->event);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400996 notify_pointer_focus(&c->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400997 break;
998
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400999 case XCB_CLIENT_MESSAGE:
1000 client_message = (xcb_client_message_event_t *) event;
1001 atom = client_message->data.data32[0];
1002 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001003 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001004 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001005
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001006 case XCB_FOCUS_IN:
1007 focus_in = (xcb_focus_in_event_t *) event;
1008 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1009 break;
1010
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001011 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001012 break;
1013
1014 case XCB_FOCUS_OUT:
1015 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -05001016 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1017 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001018 break;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001019 notify_keyboard_focus_out(&c->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001020 break;
1021
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001022 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001023 break;
1024 }
1025
Daniel Stonee2faa122012-06-22 13:21:39 +01001026#ifdef HAVE_XCB_XKB
1027 if (c->has_xkb &&
1028 (event->response_type & ~0x80) == c->xkb_event_base) {
1029 xcb_xkb_state_notify_event_t *state =
1030 (xcb_xkb_state_notify_event_t *) event;
1031 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
1032 update_xkb_state(c, state);
1033 }
1034#endif
1035
Bill Spitzakb715cec2012-06-05 17:08:23 -04001036 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001037 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001038 free (event);
1039 }
1040
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001041 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1042 case XCB_KEY_RELEASE:
1043 key_release = (xcb_key_press_event_t *) prev;
Daniel Stone154c34c2012-06-22 13:21:40 +01001044 update_xkb_state_from_core(c, key_release->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001045 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001046 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001047 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001048 WL_KEYBOARD_KEY_STATE_RELEASED,
1049 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001050 free(prev);
1051 prev = NULL;
1052 break;
1053 default:
1054 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001055 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001056
Bill Spitzakb715cec2012-06-05 17:08:23 -04001057 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001058}
1059
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001060#define F(field) offsetof(struct x11_compositor, field)
1061
1062static void
1063x11_compositor_get_resources(struct x11_compositor *c)
1064{
1065 static const struct { const char *name; int offset; } atoms[] = {
1066 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1067 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1068 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1069 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001070 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001071 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001072 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001073 { "_NET_WM_STATE", F(atom.net_wm_state) },
1074 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001075 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001076 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001077 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001078 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001079 };
1080
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001081 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001082 xcb_intern_atom_reply_t *reply;
1083 xcb_pixmap_t pixmap;
1084 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001085 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001086 uint8_t data[] = { 0, 0, 0, 0 };
1087
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001088 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001089 cookies[i] = xcb_intern_atom (c->conn, 0,
1090 strlen(atoms[i].name),
1091 atoms[i].name);
1092
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001093 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001094 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
1095 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
1096 free(reply);
1097 }
1098
1099 pixmap = xcb_generate_id(c->conn);
1100 gc = xcb_generate_id(c->conn);
1101 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
1102 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
1103 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
1104 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
1105 c->null_cursor = xcb_generate_id(c->conn);
1106 xcb_create_cursor (c->conn, c->null_cursor,
1107 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
1108 xcb_free_gc(c->conn, gc);
1109 xcb_free_pixmap(c->conn, pixmap);
1110}
1111
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001112static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001113x11_restore(struct weston_compositor *ec)
1114{
1115}
1116
1117static void
Scott Moreau248aaec2012-08-03 14:19:52 -06001118x11_free_configured_output(struct x11_configured_output *output)
1119{
1120 free(output->name);
1121 free(output);
1122}
1123
1124static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001125x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001126{
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001127 struct x11_compositor *compositor = (struct x11_compositor *)ec;
Scott Moreau248aaec2012-08-03 14:19:52 -06001128 struct x11_configured_output *o, *n;
1129
1130 wl_list_for_each_safe(o, n, &configured_output_list, link)
1131 x11_free_configured_output(o);
Matt Roper361d2ad2011-08-29 13:52:23 -07001132
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001133 wl_event_source_remove(compositor->xcb_source);
1134 x11_input_destroy(compositor);
1135
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001136 weston_compositor_shutdown(ec); /* destroys outputs, too */
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001137
1138 x11_compositor_fini_egl(compositor);
1139
1140 XCloseDisplay(compositor->dpy);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001141 free(ec);
1142}
1143
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001144static struct weston_compositor *
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001145x11_compositor_create(struct wl_display *display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001146 int fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001147 int no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001148 int argc, char *argv[], const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001149{
1150 struct x11_compositor *c;
Scott Moreau248aaec2012-08-03 14:19:52 -06001151 struct x11_configured_output *o;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001152 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001153 xcb_screen_iterator_t s;
Scott Moreau248aaec2012-08-03 14:19:52 -06001154 int i, x = 0, output_count = 0;
1155 int width, height, count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001156
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001157 weston_log("initializing x11 backend\n");
1158
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001159 c = malloc(sizeof *c);
1160 if (c == NULL)
1161 return NULL;
1162
1163 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001164
Daniel Stone725c2c32012-06-22 14:04:36 +01001165 if (weston_compositor_init(&c->base, display, argc, argv,
1166 config_file) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001167 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001168
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001169 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +02001170 if (c->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001171 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001172
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001173 c->conn = XGetXCBConnection(c->dpy);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001174 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
1175
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001176 if (xcb_connection_has_error(c->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001177 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001178
1179 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
1180 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001181 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001182
1183 x11_compositor_get_resources(c);
1184
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001185 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +02001186 if (x11_compositor_init_egl(c) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001187 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001188
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001189 c->base.destroy = x11_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001190 c->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001191
Daniel Stone725c2c32012-06-22 14:04:36 +01001192 if (weston_compositor_init_gl(&c->base) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001193 goto err_egl;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001194
Daniel Stone22815f92012-06-22 13:21:34 +01001195 if (x11_input_create(c, no_input) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001196 goto err_egl;
Daniel Stone22815f92012-06-22 13:21:34 +01001197
Scott Moreau248aaec2012-08-03 14:19:52 -06001198 width = option_width ? option_width : 1024;
1199 height = option_height ? option_height : 640;
1200 count = option_count ? option_count : 1;
1201
1202 wl_list_for_each(o, &configured_output_list, link) {
Scott Moreau1bad5db2012-08-18 01:04:05 -06001203 output = x11_compositor_create_output(c, x, 0,
1204 option_width ? width :
1205 o->width,
1206 option_height ? height :
1207 o->height,
1208 fullscreen, no_input,
1209 o->name, o->transform);
1210 if (output == NULL)
Scott Moreau248aaec2012-08-03 14:19:52 -06001211 goto err_x11_input;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001212
1213 x = pixman_region32_extents(&output->base.region)->x2;
1214
Scott Moreau248aaec2012-08-03 14:19:52 -06001215 output_count++;
1216 if (option_count && output_count >= option_count)
1217 break;
1218 }
1219
1220 for (i = output_count; i < count; i++) {
Scott Moreau1bad5db2012-08-18 01:04:05 -06001221 output = x11_compositor_create_output(c, x, 0, width, height,
1222 fullscreen, no_input, NULL,
1223 WL_OUTPUT_TRANSFORM_NORMAL);
1224 if (output == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001225 goto err_x11_input;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001226 x = pixman_region32_extents(&output->base.region)->x2;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001227 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001228
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001229 c->xcb_source =
Kristian Høgsberg22ba60e2012-03-12 01:18:24 -04001230 wl_event_loop_add_fd(c->base.input_loop,
1231 xcb_get_file_descriptor(c->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001232 WL_EVENT_READABLE,
1233 x11_compositor_handle_event, c);
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001234 wl_event_source_check(c->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001235
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001236 return &c->base;
Martin Olsson11434bb2012-07-08 03:03:44 +02001237
1238err_x11_input:
1239 x11_input_destroy(c);
1240err_egl:
1241 x11_compositor_fini_egl(c);
1242err_xdisplay:
1243 XCloseDisplay(c->dpy);
1244err_free:
1245 free(c);
1246 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001247}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001248
Scott Moreau248aaec2012-08-03 14:19:52 -06001249static void
Scott Moreau1bad5db2012-08-18 01:04:05 -06001250x11_output_set_transform(struct x11_configured_output *output)
1251{
1252 if (!output_transform) {
1253 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1254 return;
1255 }
1256
1257 if (!strcmp(output_transform, "normal"))
1258 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1259 else if (!strcmp(output_transform, "90"))
1260 output->transform = WL_OUTPUT_TRANSFORM_90;
1261 else if (!strcmp(output_transform, "180"))
1262 output->transform = WL_OUTPUT_TRANSFORM_180;
1263 else if (!strcmp(output_transform, "270"))
1264 output->transform = WL_OUTPUT_TRANSFORM_270;
1265 else if (!strcmp(output_transform, "flipped"))
1266 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
1267 else if (!strcmp(output_transform, "flipped-90"))
1268 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
1269 else if (!strcmp(output_transform, "flipped-180"))
1270 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
1271 else if (!strcmp(output_transform, "flipped-270"))
1272 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
1273 else {
1274 weston_log("Invalid transform \"%s\" for output %s\n",
1275 output_transform, output_name);
1276 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1277 }
1278}
1279
1280static void
Scott Moreau248aaec2012-08-03 14:19:52 -06001281output_section_done(void *data)
1282{
1283 struct x11_configured_output *output;
1284
1285 output = malloc(sizeof *output);
1286
Scott Moreau1bad5db2012-08-18 01:04:05 -06001287 if (!output || !output_name || (output_name[0] != 'X') ||
1288 (!output_mode && !output_transform)) {
1289 if (output_name)
1290 free(output_name);
Scott Moreau248aaec2012-08-03 14:19:52 -06001291 output_name = NULL;
Scott Moreauca9b09d2012-08-06 01:27:06 -06001292 goto err_free;
Scott Moreau248aaec2012-08-03 14:19:52 -06001293 }
1294
1295 output->name = output_name;
1296
Scott Moreau1bad5db2012-08-18 01:04:05 -06001297 if (output_mode) {
1298 if (sscanf(output_mode, "%dx%d", &output->width,
1299 &output->height) != 2) {
1300 weston_log("Invalid mode \"%s\" for output %s\n",
1301 output_mode, output_name);
1302 x11_free_configured_output(output);
1303 goto err_free;
1304 }
1305 } else {
1306 output->width = 1024;
1307 output->height = 640;
Scott Moreau248aaec2012-08-03 14:19:52 -06001308 }
1309
Scott Moreau1bad5db2012-08-18 01:04:05 -06001310 x11_output_set_transform(output);
Scott Moreau248aaec2012-08-03 14:19:52 -06001311
Scott Moreauca9b09d2012-08-06 01:27:06 -06001312 wl_list_insert(configured_output_list.prev, &output->link);
1313
1314err_free:
Scott Moreau1bad5db2012-08-18 01:04:05 -06001315 if (output_mode)
1316 free(output_mode);
1317 if (output_transform)
1318 free(output_transform);
Scott Moreau248aaec2012-08-03 14:19:52 -06001319 output_mode = NULL;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001320 output_transform = NULL;
Scott Moreau248aaec2012-08-03 14:19:52 -06001321}
1322
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001323WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -04001324backend_init(struct wl_display *display, int argc, char *argv[],
1325 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001326{
Scott Moreau248aaec2012-08-03 14:19:52 -06001327 int fullscreen = 0;
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001328 int no_input = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001329
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001330 const struct weston_option x11_options[] = {
Scott Moreau248aaec2012-08-03 14:19:52 -06001331 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1332 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001333 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Scott Moreau248aaec2012-08-03 14:19:52 -06001334 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001335 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001336 };
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001337
1338 parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001339
Scott Moreau248aaec2012-08-03 14:19:52 -06001340 wl_list_init(&configured_output_list);
1341
1342 const struct config_key x11_config_keys[] = {
1343 { "name", CONFIG_KEY_STRING, &output_name },
1344 { "mode", CONFIG_KEY_STRING, &output_mode },
Scott Moreau1bad5db2012-08-18 01:04:05 -06001345 { "transform", CONFIG_KEY_STRING, &output_transform },
Scott Moreau248aaec2012-08-03 14:19:52 -06001346 };
1347
1348 const struct config_section config_section[] = {
1349 { "output", x11_config_keys,
1350 ARRAY_LENGTH(x11_config_keys), output_section_done },
1351 };
1352
1353 parse_config_file(config_file, config_section,
1354 ARRAY_LENGTH(config_section), NULL);
1355
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001356 return x11_compositor_create(display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001357 fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001358 no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001359 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001360}