blob: e13514310d921a4a8c865d15faa23a27bdcc86b2 [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;
55static int option_width;
56static int option_height;
57static int option_count;
58static struct wl_list configured_output_list;
59
60struct x11_configured_output {
61 char *name;
62 int width, height;
63 struct wl_list link;
64};
65
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040066struct x11_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_compositor base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040068
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040069 EGLSurface dummy_pbuffer;
70
Benjamin Franzke3b288af2011-02-20 19:58:42 +010071 Display *dpy;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040072 xcb_connection_t *conn;
73 xcb_screen_t *screen;
74 xcb_cursor_t null_cursor;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050075 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040076 struct wl_event_source *xcb_source;
Daniel Stone576cd152012-06-01 12:14:02 +010077 struct xkb_keymap *xkb_keymap;
Daniel Stone62b33b62012-06-22 13:21:35 +010078 unsigned int has_xkb;
79 uint8_t xkb_event_base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040080 struct {
81 xcb_atom_t wm_protocols;
82 xcb_atom_t wm_normal_hints;
83 xcb_atom_t wm_size_hints;
84 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050085 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040086 xcb_atom_t net_wm_name;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050087 xcb_atom_t net_wm_icon;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -040088 xcb_atom_t net_wm_state;
89 xcb_atom_t net_wm_state_fullscreen;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050090 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040091 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050092 xcb_atom_t cardinal;
Daniel Stone855028f2012-05-01 20:37:10 +010093 xcb_atom_t xkb_names;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040094 } atom;
95};
96
97struct x11_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050098 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040099
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400100 xcb_window_t window;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100101 EGLSurface egl_surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500102 struct weston_mode mode;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400103 struct wl_event_source *finish_frame_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400104};
105
106struct x11_input {
Daniel Stone37816df2012-05-16 18:45:18 +0100107 struct weston_seat base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400108};
109
Daniel Stone576cd152012-06-01 12:14:02 +0100110static struct xkb_keymap *
111x11_compositor_get_keymap(struct x11_compositor *c)
112{
113 xcb_get_property_cookie_t cookie;
114 xcb_get_property_reply_t *reply;
115 xcb_generic_error_t *error;
116 struct xkb_rule_names names;
117 struct xkb_keymap *ret;
118 const char *value_all, *value_part;
119 int length_all, length_part;
120
121 memset(&names, 0, sizeof(names));
122
123 cookie = xcb_get_property(c->conn, 0, c->screen->root,
124 c->atom.xkb_names, c->atom.string, 0, 1024);
125 reply = xcb_get_property_reply(c->conn, cookie, &error);
126 if (reply == NULL)
127 return NULL;
128
129 value_all = xcb_get_property_value(reply);
130 length_all = xcb_get_property_value_length(reply);
131 value_part = value_all;
132
133#define copy_prop_value(to) \
134 length_part = strlen(value_part); \
135 if (value_part + length_part < (value_all + length_all) && \
136 length_part > 0) \
137 names.to = value_part; \
138 value_part += length_part + 1;
139
140 copy_prop_value(rules);
141 copy_prop_value(model);
142 copy_prop_value(layout);
143 copy_prop_value(variant);
144 copy_prop_value(options);
145#undef copy_prop_value
146
147 ret = xkb_map_new_from_names(c->base.xkb_context, &names, 0);
148
149 free(reply);
150 return ret;
151}
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400152
Daniel Stone62b33b62012-06-22 13:21:35 +0100153static void
154x11_compositor_setup_xkb(struct x11_compositor *c)
155{
156#ifndef HAVE_XCB_XKB
157 weston_log("XCB-XKB not available during build\n");
158 c->has_xkb = 0;
159 c->xkb_event_base = 0;
160 return;
161#else
162 const xcb_query_extension_reply_t *ext;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100163 xcb_generic_error_t *error;
Daniel Stonee2faa122012-06-22 13:21:39 +0100164 xcb_void_cookie_t select;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100165 xcb_xkb_per_client_flags_cookie_t pcf;
166 xcb_xkb_per_client_flags_reply_t *pcf_reply;
Daniel Stone62b33b62012-06-22 13:21:35 +0100167
168 c->has_xkb = 0;
169 c->xkb_event_base = 0;
170
171 ext = xcb_get_extension_data(c->conn, &xcb_xkb_id);
172 if (!ext) {
173 weston_log("XKB extension not available on host X11 server\n");
174 return;
175 }
176 c->xkb_event_base = ext->first_event;
177
Daniel Stonee2faa122012-06-22 13:21:39 +0100178 select = xcb_xkb_select_events(c->conn,
179 XCB_XKB_ID_USE_CORE_KBD,
180 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
181 0,
182 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
183 0,
184 0,
185 NULL);
186 error = xcb_request_check(c->conn, select);
187 if (error) {
188 weston_log("error: failed to select for XKB state events\n");
189 return;
190 }
191
Daniel Stone3ee91e12012-06-22 13:21:36 +0100192 pcf = xcb_xkb_per_client_flags(c->conn,
193 XCB_XKB_ID_USE_CORE_KBD,
194 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
195 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
196 0,
197 0,
198 0);
199 pcf_reply = xcb_xkb_per_client_flags_reply(c->conn, pcf, &error);
200 free(pcf_reply);
201 if (error) {
202 weston_log("failed to set XKB per-client flags, not using "
203 "detectable repeat\n");
204 return;
205 }
206
Daniel Stone62b33b62012-06-22 13:21:35 +0100207 c->has_xkb = 1;
208#endif
209}
210
Darxus55973f22010-11-22 21:24:39 -0500211static int
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300212x11_input_create(struct x11_compositor *c, int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400213{
214 struct x11_input *input;
Daniel Stone576cd152012-06-01 12:14:02 +0100215 struct xkb_keymap *keymap;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400216
217 input = malloc(sizeof *input);
218 if (input == NULL)
Darxus55973f22010-11-22 21:24:39 -0500219 return -1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400220
221 memset(input, 0, sizeof *input);
Daniel Stone37816df2012-05-16 18:45:18 +0100222 weston_seat_init(&input->base, &c->base);
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300223 c->base.seat = &input->base;
224
225 if (no_input)
226 return 0;
227
Daniel Stone74419a22012-05-30 16:32:02 +0100228 weston_seat_init_pointer(&input->base);
Daniel Stone576cd152012-06-01 12:14:02 +0100229
Daniel Stone62b33b62012-06-22 13:21:35 +0100230 x11_compositor_setup_xkb(c);
231
Daniel Stone576cd152012-06-01 12:14:02 +0100232 keymap = x11_compositor_get_keymap(c);
233 weston_seat_init_keyboard(&input->base, keymap);
234 if (keymap)
235 xkb_map_unref(keymap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400236
Darxus55973f22010-11-22 21:24:39 -0500237 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400238}
239
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200240static void
241x11_input_destroy(struct x11_compositor *compositor)
242{
Daniel Stone37816df2012-05-16 18:45:18 +0100243 struct x11_input *input = container_of(compositor->base.seat,
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200244 struct x11_input,
Daniel Stone37816df2012-05-16 18:45:18 +0100245 base);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200246
Daniel Stone37816df2012-05-16 18:45:18 +0100247 weston_seat_release(&input->base);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200248 free(input);
249}
250
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400251static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400252x11_compositor_init_egl(struct x11_compositor *c)
253{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400254 EGLint major, minor;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100255 EGLint n;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100256 EGLint config_attribs[] = {
257 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
258 EGL_RED_SIZE, 1,
259 EGL_GREEN_SIZE, 1,
260 EGL_BLUE_SIZE, 1,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100261 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
262 EGL_NONE
263 };
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400264 static const EGLint context_attribs[] = {
265 EGL_CONTEXT_CLIENT_VERSION, 2,
266 EGL_NONE
267 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400268
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400269 static const EGLint pbuffer_attribs[] = {
270 EGL_WIDTH, 10,
271 EGL_HEIGHT, 10,
272 EGL_NONE
273 };
274
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400275 c->base.egl_display = eglGetDisplay(c->dpy);
276 if (c->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200277 weston_log("failed to create display\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400278 return -1;
279 }
280
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400281 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200282 weston_log("failed to initialize display\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400283 return -1;
284 }
285
Darxus55973f22010-11-22 21:24:39 -0500286 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200287 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Darxus55973f22010-11-22 21:24:39 -0500288 return -1;
289 }
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400290 if (!eglChooseConfig(c->base.egl_display, config_attribs,
291 &c->base.egl_config, 1, &n) || n == 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200292 weston_log("failed to choose config: %d\n", n);
Benjamin Franzke84290d02011-03-02 10:07:59 +0100293 return -1;
294 }
Darxus55973f22010-11-22 21:24:39 -0500295
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400296 c->base.egl_context =
297 eglCreateContext(c->base.egl_display, c->base.egl_config,
298 EGL_NO_CONTEXT, context_attribs);
299 if (c->base.egl_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200300 weston_log("failed to create context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400301 return -1;
302 }
303
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400304 c->dummy_pbuffer = eglCreatePbufferSurface(c->base.egl_display,
305 c->base.egl_config,
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400306 pbuffer_attribs);
Kristian Høgsbergc0bc8442012-05-17 09:40:23 -0400307 if (c->dummy_pbuffer == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200308 weston_log("failed to create dummy pbuffer\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400309 return -1;
310 }
311
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400312 if (!eglMakeCurrent(c->base.egl_display, c->dummy_pbuffer,
313 c->dummy_pbuffer, c->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200314 weston_log("failed to make context current\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400315 return -1;
316 }
317
318 return 0;
319}
320
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200321static void
322x11_compositor_fini_egl(struct x11_compositor *compositor)
323{
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400324 eglMakeCurrent(compositor->base.egl_display,
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200325 EGL_NO_SURFACE, EGL_NO_SURFACE,
326 EGL_NO_CONTEXT);
327
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400328 eglTerminate(compositor->base.egl_display);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200329 eglReleaseThread();
330}
331
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500332static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500333x11_output_repaint(struct weston_output *output_base,
334 pixman_region32_t *damage)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500335{
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500336 struct x11_output *output = (struct x11_output *)output_base;
337 struct x11_compositor *compositor =
338 (struct x11_compositor *)output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500339 struct weston_surface *surface;
340
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400341 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
342 output->egl_surface,
343 compositor->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200344 weston_log("failed to make current\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500345 return;
346 }
347
348 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500349 weston_surface_draw(surface, &output->base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500350
Kristian Høgsberge0f832b2012-06-20 00:13:18 -0400351 wl_signal_emit(&output->base.frame_signal, output);
Scott Moreau062be7e2012-04-20 13:37:33 -0600352
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400353 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500354
355 wl_event_source_timer_update(output->finish_frame_timer, 10);
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500356}
357
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100358static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400359finish_frame_handler(void *data)
360{
361 struct x11_output *output = data;
362 uint32_t msec;
363 struct timeval tv;
364
365 gettimeofday(&tv, NULL);
366 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500367 weston_output_finish_frame(&output->base, msec);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400368
369 return 1;
370}
371
Matt Roper361d2ad2011-08-29 13:52:23 -0700372static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500373x11_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700374{
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200375 struct x11_output *output = (struct x11_output *)output_base;
376 struct x11_compositor *compositor =
377 (struct x11_compositor *)output->base.compositor;
378
379 wl_list_remove(&output->base.link);
380 wl_event_source_remove(output->finish_frame_timer);
381
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400382 eglDestroySurface(compositor->base.egl_display, output->egl_surface);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200383
384 xcb_destroy_window(compositor->conn, output->window);
385
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500386 weston_output_destroy(&output->base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200387
388 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700389}
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200390
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400391static void
392x11_output_set_wm_protocols(struct x11_output *output)
393{
394 xcb_atom_t list[1];
395 struct x11_compositor *c =
396 (struct x11_compositor *) output->base.compositor;
397
398 list[0] = c->atom.wm_delete_window;
399 xcb_change_property (c->conn,
400 XCB_PROP_MODE_REPLACE,
401 output->window,
402 c->atom.wm_protocols,
403 XCB_ATOM_ATOM,
404 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500405 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400406 list);
407}
408
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400409static void
410x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state)
411{
412 xcb_client_message_event_t event;
413 struct x11_compositor *c =
414 (struct x11_compositor *) output->base.compositor;
415 xcb_screen_iterator_t iter;
416
417#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
418#define _NET_WM_STATE_ADD 1 /* add/set property */
419#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
420
421 memset(&event, 0, sizeof event);
422 event.response_type = XCB_CLIENT_MESSAGE;
423 event.format = 32;
424 event.window = output->window;
425 event.type = c->atom.net_wm_state;
426
427 event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
428 event.data.data32[1] = state;
429 event.data.data32[2] = 0;
430 event.data.data32[3] = 0;
431 event.data.data32[4] = 0;
432
433 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
434 xcb_send_event(c->conn, 0, iter.data->root,
435 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
436 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
437 (void *) &event);
438}
439
440
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400441struct wm_normal_hints {
442 uint32_t flags;
443 uint32_t pad[4];
444 int32_t min_width, min_height;
445 int32_t max_width, max_height;
446 int32_t width_inc, height_inc;
447 int32_t min_aspect_x, min_aspect_y;
448 int32_t max_aspect_x, max_aspect_y;
449 int32_t base_width, base_height;
450 int32_t win_gravity;
451};
452
453#define WM_NORMAL_HINTS_MIN_SIZE 16
454#define WM_NORMAL_HINTS_MAX_SIZE 32
455
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500456static void
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400457x11_output_set_icon(struct x11_compositor *c,
458 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500459{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400460 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400461 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400462 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500463
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400464 image = load_image(filename);
465 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500466 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400467 width = pixman_image_get_width(image);
468 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500469 icon = malloc(width * height * 4 + 8);
470 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400471 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500472 return;
473 }
474
475 icon[0] = width;
476 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400477 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500478 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
479 c->atom.net_wm_icon, c->atom.cardinal, 32,
480 width * height + 2, icon);
481 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400482 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500483}
484
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400485static int
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700486x11_compositor_create_output(struct x11_compositor *c, int x, int y,
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300487 int width, int height, int fullscreen,
Scott Moreau248aaec2012-08-03 14:19:52 -0600488 int no_input, const char *name)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400489{
Kristian Høgsberg86000402012-01-03 23:24:14 -0500490 static const char class[] = "weston-1\0Weston Compositor";
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400491 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400492 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400493 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400494 struct wl_event_loop *loop;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400495 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300496 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400497 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300498 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400499 0
500 };
501
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300502 if (!no_input)
503 values[0] |=
504 XCB_EVENT_MASK_KEY_PRESS |
505 XCB_EVENT_MASK_KEY_RELEASE |
506 XCB_EVENT_MASK_BUTTON_PRESS |
507 XCB_EVENT_MASK_BUTTON_RELEASE |
508 XCB_EVENT_MASK_POINTER_MOTION |
509 XCB_EVENT_MASK_ENTER_WINDOW |
510 XCB_EVENT_MASK_LEAVE_WINDOW |
511 XCB_EVENT_MASK_KEYMAP_STATE |
512 XCB_EVENT_MASK_FOCUS_CHANGE;
513
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400514 output = malloc(sizeof *output);
515 if (output == NULL)
516 return -1;
517
518 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400519
520 output->mode.flags =
521 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
522 output->mode.width = width;
523 output->mode.height = height;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400524 output->mode.refresh = 60000;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400525 wl_list_init(&output->base.mode_list);
526 wl_list_insert(&output->base.mode_list, &output->mode.link);
527
528 output->base.current = &output->mode;
Kristian Høgsberg12481582012-07-02 21:24:57 -0400529 output->base.make = "xwayland";
530 output->base.model = "none";
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500531 weston_output_init(&output->base, &c->base, x, y, width, height,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100532 WL_OUTPUT_FLIPPED);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400533
534 values[1] = c->null_cursor;
535 output->window = xcb_generate_id(c->conn);
536 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
537 xcb_create_window(c->conn,
538 XCB_COPY_FROM_PARENT,
539 output->window,
540 iter.data->root,
541 0, 0,
542 width, height,
543 0,
544 XCB_WINDOW_CLASS_INPUT_OUTPUT,
545 iter.data->root_visual,
546 mask, values);
547
548 /* Don't resize me. */
549 memset(&normal_hints, 0, sizeof normal_hints);
550 normal_hints.flags =
551 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
552 normal_hints.min_width = width;
553 normal_hints.min_height = height;
554 normal_hints.max_width = width;
555 normal_hints.max_height = height;
556 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
557 c->atom.wm_normal_hints,
558 c->atom.wm_size_hints, 32,
559 sizeof normal_hints / 4,
560 (uint8_t *) &normal_hints);
561
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400562 /* Set window name. Don't bother with non-EWMH WMs. */
563 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
564 c->atom.net_wm_name, c->atom.utf8_string, 8,
565 strlen(name), name);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500566 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
567 c->atom.wm_class, c->atom.string, 8,
568 sizeof class, class);
569
Kristian Høgsberg86000402012-01-03 23:24:14 -0500570 x11_output_set_icon(c, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500571
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500572 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400573
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400574 x11_output_set_wm_protocols(output);
575
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400576 if (fullscreen)
577 x11_output_change_state(output, 1,
578 c->atom.net_wm_state_fullscreen);
579
Benjamin Franzke84290d02011-03-02 10:07:59 +0100580 output->egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400581 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100582 output->window, NULL);
583 if (!output->egl_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200584 weston_log("failed to create window surface\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400585 return -1;
586 }
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400587 if (!eglMakeCurrent(c->base.egl_display, output->egl_surface,
588 output->egl_surface, c->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200589 weston_log("failed to make surface current\n");
Benjamin Franzke84290d02011-03-02 10:07:59 +0100590 return -1;
591 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400592
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400593 loop = wl_display_get_event_loop(c->base.wl_display);
594 output->finish_frame_timer =
595 wl_event_loop_add_timer(loop, finish_frame_handler, output);
596
Alex Wubd3354b2012-04-17 17:20:49 +0800597 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500598 output->base.repaint = x11_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700599 output->base.destroy = x11_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800600 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200601 output->base.set_backlight = NULL;
602 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800603 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100604
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400605 wl_list_insert(c->base.output_list.prev, &output->base.link);
606
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400607 weston_log("x11 output %dx%d, window id %d\n",
608 width, height, output->window);
609
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400610 return 0;
611}
612
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400613static struct x11_output *
614x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
615{
616 struct x11_output *output;
617
618 wl_list_for_each(output, &c->base.output_list, base.link) {
619 if (output->window == window)
620 return output;
621 }
622
623 return NULL;
624}
625
Daniel Stonee2faa122012-06-22 13:21:39 +0100626static uint32_t
627get_xkb_mod_mask(struct x11_compositor *c, uint32_t in)
628{
629 struct weston_xkb_info *info = &c->base.seat->xkb_info;
630 uint32_t ret = 0;
631
632 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
633 ret |= (1 << info->shift_mod);
634 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
635 ret |= (1 << info->caps_mod);
636 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
637 ret |= (1 << info->ctrl_mod);
638 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
639 ret |= (1 << info->alt_mod);
640 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
641 ret |= (1 << info->mod2_mod);
642 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
643 ret |= (1 << info->mod3_mod);
644 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
645 ret |= (1 << info->super_mod);
646 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
647 ret |= (1 << info->mod5_mod);
648
649 return ret;
650}
651
Daniel Stone154c34c2012-06-22 13:21:40 +0100652#ifdef HAVE_XCB_XKB
Daniel Stonee2faa122012-06-22 13:21:39 +0100653static void
654update_xkb_state(struct x11_compositor *c, xcb_xkb_state_notify_event_t *state)
655{
656 struct weston_compositor *ec = &c->base;
657 struct wl_seat *seat = &ec->seat->seat;
658
659 xkb_state_update_mask(c->base.seat->xkb_state.state,
660 get_xkb_mod_mask(c, state->baseMods),
661 get_xkb_mod_mask(c, state->latchedMods),
662 get_xkb_mod_mask(c, state->lockedMods),
663 0,
664 0,
665 state->group);
666
667 notify_modifiers(seat, wl_display_next_serial(c->base.wl_display));
668}
669#endif
670
Daniel Stone154c34c2012-06-22 13:21:40 +0100671/**
672 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
673 * the best we can do (given that XCB also lacks XI2 support), is to take
674 * the state from the core key events. Unfortunately that only gives us
675 * the effective (i.e. union of depressed/latched/locked) state, and we
676 * need the granularity.
677 *
678 * So we still update the state with every key event we see, but also use
679 * the state field from X11 events as a mask so we don't get any stuck
680 * modifiers.
681 */
682static void
683update_xkb_state_from_core(struct x11_compositor *c, uint16_t x11_mask)
684{
685 uint32_t mask = get_xkb_mod_mask(c, x11_mask);
686 struct wl_keyboard *keyboard = &c->base.seat->keyboard;
687
688 xkb_state_update_mask(c->base.seat->xkb_state.state,
689 keyboard->modifiers.mods_depressed & mask,
690 keyboard->modifiers.mods_latched & mask,
691 keyboard->modifiers.mods_locked & mask,
692 0,
693 0,
694 (x11_mask >> 13) & 3);
695 notify_modifiers(&c->base.seat->seat,
696 wl_display_next_serial(c->base.wl_display));
697}
698
Tiago Vignattia3a71622011-12-08 17:03:17 +0200699static void
700x11_compositor_deliver_button_event(struct x11_compositor *c,
701 xcb_generic_event_t *event, int state)
702{
703 xcb_button_press_event_t *button_event =
704 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +0100705 uint32_t button;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200706
Daniel Stone154c34c2012-06-22 13:21:40 +0100707 if (!c->has_xkb)
708 update_xkb_state_from_core(c, button_event->state);
709
Tiago Vignattia3a71622011-12-08 17:03:17 +0200710 switch (button_event->detail) {
711 default:
712 button = button_event->detail + BTN_LEFT - 1;
713 break;
714 case 2:
715 button = BTN_MIDDLE;
716 break;
717 case 3:
718 button = BTN_RIGHT;
719 break;
720 case 4:
Scott Moreau210d0792012-03-22 10:47:01 -0600721 if (state)
Daniel Stone37816df2012-05-16 18:45:18 +0100722 notify_axis(&c->base.seat->seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600723 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100724 WL_POINTER_AXIS_VERTICAL_SCROLL,
725 wl_fixed_from_int(1));
Scott Moreau210d0792012-03-22 10:47:01 -0600726 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200727 case 5:
Scott Moreau210d0792012-03-22 10:47:01 -0600728 if (state)
Daniel Stone37816df2012-05-16 18:45:18 +0100729 notify_axis(&c->base.seat->seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600730 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100731 WL_POINTER_AXIS_VERTICAL_SCROLL,
732 wl_fixed_from_int(-1));
Scott Moreau210d0792012-03-22 10:47:01 -0600733 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200734 case 6:
Scott Moreau210d0792012-03-22 10:47:01 -0600735 if (state)
Daniel Stone37816df2012-05-16 18:45:18 +0100736 notify_axis(&c->base.seat->seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600737 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100738 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
739 wl_fixed_from_int(1));
Scott Moreau210d0792012-03-22 10:47:01 -0600740 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200741 case 7:
Scott Moreau210d0792012-03-22 10:47:01 -0600742 if (state)
Daniel Stone37816df2012-05-16 18:45:18 +0100743 notify_axis(&c->base.seat->seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600744 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100745 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
746 wl_fixed_from_int(-1));
Tiago Vignattia3a71622011-12-08 17:03:17 +0200747 return;
748 }
749
Daniel Stone37816df2012-05-16 18:45:18 +0100750 notify_button(&c->base.seat->seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100751 weston_compositor_get_time(), button,
752 state ? WL_POINTER_BUTTON_STATE_PRESSED :
753 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200754}
755
Kristian Høgsbergee724822011-04-21 14:51:44 -0400756static int
757x11_compositor_next_event(struct x11_compositor *c,
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400758 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -0400759{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400760 if (mask & WL_EVENT_READABLE) {
761 *event = xcb_poll_for_event(c->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -0400762 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400763#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400764 *event = xcb_poll_for_queued_event(c->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400765#else
766 *event = xcb_poll_for_event(c->conn);
767#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -0400768 }
769
770 return *event != NULL;
771}
772
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400773static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400774x11_compositor_handle_event(int fd, uint32_t mask, void *data)
775{
776 struct x11_compositor *c = data;
777 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400778 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400779 xcb_client_message_event_t *client_message;
780 xcb_motion_notify_event_t *motion_notify;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500781 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400782 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500783 xcb_keymap_notify_event_t *keymap_notify;
784 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400785 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400786 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500787 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400788 uint32_t i, set;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400789 wl_fixed_t x, y;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400790 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400791
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400792 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400793 count = 0;
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400794 while (x11_compositor_next_event(c, &event, mask)) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400795 switch (prev ? prev->response_type & ~0x80 : 0x80) {
796 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100797 /* Suppress key repeat events; this is only used if we
798 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400799 key_release = (xcb_key_press_event_t *) prev;
800 key_press = (xcb_key_press_event_t *) event;
801 if ((event->response_type & ~0x80) == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -0700802 key_release->time == key_press->time &&
803 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400804 /* Don't deliver the held key release
805 * event or the new key press event. */
806 free(event);
807 free(prev);
808 prev = NULL;
809 continue;
810 } else {
811 /* Deliver the held key release now
812 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400813 * event below. */
Daniel Stone154c34c2012-06-22 13:21:40 +0100814 update_xkb_state_from_core(c, key_release->state);
Daniel Stone37816df2012-05-16 18:45:18 +0100815 notify_key(&c->base.seat->seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500816 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100817 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100818 WL_KEYBOARD_KEY_STATE_RELEASED,
819 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400820 free(prev);
821 prev = NULL;
822 break;
823 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400824
825 case XCB_FOCUS_IN:
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400826 /* assert event is keymap_notify */
827 focus_in = (xcb_focus_in_event_t *) prev;
828 keymap_notify = (xcb_keymap_notify_event_t *) event;
829 c->keys.size = 0;
830 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
831 set = keymap_notify->keys[i >> 3] &
832 (1 << (i & 7));
833 if (set) {
834 k = wl_array_add(&c->keys, sizeof *k);
835 *k = i;
836 }
837 }
838
839 output = x11_compositor_find_output(c, focus_in->event);
Daniel Stoned6da09e2012-06-22 13:21:29 +0100840 /* Unfortunately the state only comes with the enter
841 * event, rather than with the focus event. I'm not
842 * sure of the exact semantics around it and whether
843 * we can ensure that we get both? */
844 notify_keyboard_focus_in(&c->base.seat->seat, &c->keys,
845 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400846
847 free(prev);
848 prev = NULL;
849 break;
850
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400851 default:
852 /* No previous event held */
853 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400854 }
855
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400856 switch (event->response_type & ~0x80) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400857 case XCB_KEY_PRESS:
858 key_press = (xcb_key_press_event_t *) event;
Daniel Stone154c34c2012-06-22 13:21:40 +0100859 if (!c->has_xkb)
860 update_xkb_state_from_core(c, key_press->state);
Daniel Stone37816df2012-05-16 18:45:18 +0100861 notify_key(&c->base.seat->seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500862 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100863 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100864 WL_KEYBOARD_KEY_STATE_PRESSED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100865 c->has_xkb ? STATE_UPDATE_NONE :
866 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400867 break;
868 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100869 /* If we don't have XKB, we need to use the lame
870 * autorepeat detection above. */
871 if (!c->has_xkb) {
872 prev = event;
873 break;
874 }
875 key_release = (xcb_key_press_event_t *) event;
876 notify_key(&c->base.seat->seat,
877 weston_compositor_get_time(),
878 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100879 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100880 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400881 break;
882 case XCB_BUTTON_PRESS:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200883 x11_compositor_deliver_button_event(c, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400884 break;
885 case XCB_BUTTON_RELEASE:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200886 x11_compositor_deliver_button_event(c, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400887 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400888 case XCB_MOTION_NOTIFY:
889 motion_notify = (xcb_motion_notify_event_t *) event;
Daniel Stone154c34c2012-06-22 13:21:40 +0100890 if (!c->has_xkb)
891 update_xkb_state_from_core(c, motion_notify->state);
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700892 output = x11_compositor_find_output(c, motion_notify->event);
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400893 x = wl_fixed_from_int(output->base.x + motion_notify->event_x);
894 y = wl_fixed_from_int(output->base.y + motion_notify->event_y);
Daniel Stone37816df2012-05-16 18:45:18 +0100895 notify_motion(&c->base.seat->seat,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400896 weston_compositor_get_time(), x, y);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400897 break;
898
899 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400900 expose = (xcb_expose_event_t *) event;
901 output = x11_compositor_find_output(c, expose->window);
902 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400903 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400904
905 case XCB_ENTER_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500906 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500907 if (enter_notify->state >= Button1Mask)
908 break;
Daniel Stone154c34c2012-06-22 13:21:40 +0100909 if (!c->has_xkb)
910 update_xkb_state_from_core(c, enter_notify->state);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500911 output = x11_compositor_find_output(c, enter_notify->event);
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400912 x = wl_fixed_from_int(output->base.x + enter_notify->event_x);
913 y = wl_fixed_from_int(output->base.y + enter_notify->event_y);
914
Daniel Stone37816df2012-05-16 18:45:18 +0100915 notify_pointer_focus(&c->base.seat->seat,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400916 &output->base, x, y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400917 break;
918
919 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500920 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500921 if (enter_notify->state >= Button1Mask)
922 break;
Daniel Stone154c34c2012-06-22 13:21:40 +0100923 if (!c->has_xkb)
924 update_xkb_state_from_core(c, enter_notify->state);
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700925 output = x11_compositor_find_output(c, enter_notify->event);
Daniel Stone37816df2012-05-16 18:45:18 +0100926 notify_pointer_focus(&c->base.seat->seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400927 break;
928
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400929 case XCB_CLIENT_MESSAGE:
930 client_message = (xcb_client_message_event_t *) event;
931 atom = client_message->data.data32[0];
932 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500933 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400934 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400935
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500936 case XCB_FOCUS_IN:
937 focus_in = (xcb_focus_in_event_t *) event;
938 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
939 break;
940
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400941 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500942 break;
943
944 case XCB_FOCUS_OUT:
945 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -0500946 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
947 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500948 break;
Daniel Stoned6da09e2012-06-22 13:21:29 +0100949 notify_keyboard_focus_out(&c->base.seat->seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500950 break;
951
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500952 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400953 break;
954 }
955
Daniel Stonee2faa122012-06-22 13:21:39 +0100956#ifdef HAVE_XCB_XKB
957 if (c->has_xkb &&
958 (event->response_type & ~0x80) == c->xkb_event_base) {
959 xcb_xkb_state_notify_event_t *state =
960 (xcb_xkb_state_notify_event_t *) event;
961 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
962 update_xkb_state(c, state);
963 }
964#endif
965
Bill Spitzakb715cec2012-06-05 17:08:23 -0400966 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400967 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400968 free (event);
969 }
970
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400971 switch (prev ? prev->response_type & ~0x80 : 0x80) {
972 case XCB_KEY_RELEASE:
973 key_release = (xcb_key_press_event_t *) prev;
Daniel Stone154c34c2012-06-22 13:21:40 +0100974 update_xkb_state_from_core(c, key_release->state);
Daniel Stone37816df2012-05-16 18:45:18 +0100975 notify_key(&c->base.seat->seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500976 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100977 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100978 WL_KEYBOARD_KEY_STATE_RELEASED,
979 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400980 free(prev);
981 prev = NULL;
982 break;
983 default:
984 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500985 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400986
Bill Spitzakb715cec2012-06-05 17:08:23 -0400987 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -0400988}
989
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400990#define F(field) offsetof(struct x11_compositor, field)
991
992static void
993x11_compositor_get_resources(struct x11_compositor *c)
994{
995 static const struct { const char *name; int offset; } atoms[] = {
996 { "WM_PROTOCOLS", F(atom.wm_protocols) },
997 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
998 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
999 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001000 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001001 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001002 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001003 { "_NET_WM_STATE", F(atom.net_wm_state) },
1004 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001005 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001006 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001007 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001008 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001009 };
1010
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001011 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001012 xcb_intern_atom_reply_t *reply;
1013 xcb_pixmap_t pixmap;
1014 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001015 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001016 uint8_t data[] = { 0, 0, 0, 0 };
1017
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001018 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001019 cookies[i] = xcb_intern_atom (c->conn, 0,
1020 strlen(atoms[i].name),
1021 atoms[i].name);
1022
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001023 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001024 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
1025 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
1026 free(reply);
1027 }
1028
1029 pixmap = xcb_generate_id(c->conn);
1030 gc = xcb_generate_id(c->conn);
1031 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
1032 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
1033 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
1034 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
1035 c->null_cursor = xcb_generate_id(c->conn);
1036 xcb_create_cursor (c->conn, c->null_cursor,
1037 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
1038 xcb_free_gc(c->conn, gc);
1039 xcb_free_pixmap(c->conn, pixmap);
1040}
1041
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001042static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001043x11_restore(struct weston_compositor *ec)
1044{
1045}
1046
1047static void
Scott Moreau248aaec2012-08-03 14:19:52 -06001048x11_free_configured_output(struct x11_configured_output *output)
1049{
1050 free(output->name);
1051 free(output);
1052}
1053
1054static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001055x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001056{
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001057 struct x11_compositor *compositor = (struct x11_compositor *)ec;
Scott Moreau248aaec2012-08-03 14:19:52 -06001058 struct x11_configured_output *o, *n;
1059
1060 wl_list_for_each_safe(o, n, &configured_output_list, link)
1061 x11_free_configured_output(o);
Matt Roper361d2ad2011-08-29 13:52:23 -07001062
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001063 wl_event_source_remove(compositor->xcb_source);
1064 x11_input_destroy(compositor);
1065
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001066 weston_compositor_shutdown(ec); /* destroys outputs, too */
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001067
1068 x11_compositor_fini_egl(compositor);
1069
1070 XCloseDisplay(compositor->dpy);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001071 free(ec);
1072}
1073
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001074static struct weston_compositor *
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001075x11_compositor_create(struct wl_display *display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001076 int fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001077 int no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001078 int argc, char *argv[], const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001079{
Scott Moreau248aaec2012-08-03 14:19:52 -06001080 static const char name[] = "Weston Compositor";
1081 char configured_name[32];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001082 struct x11_compositor *c;
Scott Moreau248aaec2012-08-03 14:19:52 -06001083 struct x11_configured_output *o;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001084 xcb_screen_iterator_t s;
Scott Moreau248aaec2012-08-03 14:19:52 -06001085 int i, x = 0, output_count = 0;
1086 int width, height, count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001087
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001088 weston_log("initializing x11 backend\n");
1089
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001090 c = malloc(sizeof *c);
1091 if (c == NULL)
1092 return NULL;
1093
1094 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001095
Daniel Stone725c2c32012-06-22 14:04:36 +01001096 if (weston_compositor_init(&c->base, display, argc, argv,
1097 config_file) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001098 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001099
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001100 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +02001101 if (c->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001102 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001103
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001104 c->conn = XGetXCBConnection(c->dpy);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001105 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
1106
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001107 if (xcb_connection_has_error(c->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001108 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001109
1110 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
1111 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001112 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001113
1114 x11_compositor_get_resources(c);
1115
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001116 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +02001117 if (x11_compositor_init_egl(c) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001118 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001119
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001120 c->base.destroy = x11_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001121 c->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001122
Daniel Stone725c2c32012-06-22 14:04:36 +01001123 if (weston_compositor_init_gl(&c->base) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001124 goto err_egl;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001125
Daniel Stone22815f92012-06-22 13:21:34 +01001126 if (x11_input_create(c, no_input) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001127 goto err_egl;
Daniel Stone22815f92012-06-22 13:21:34 +01001128
Scott Moreau248aaec2012-08-03 14:19:52 -06001129 width = option_width ? option_width : 1024;
1130 height = option_height ? option_height : 640;
1131 count = option_count ? option_count : 1;
1132
1133 wl_list_for_each(o, &configured_output_list, link) {
1134 sprintf(configured_name, "%s - %s", name, o->name);
1135 if (x11_compositor_create_output(c, x, 0,
1136 option_width ? option_width :
1137 o->width,
1138 option_height ? option_height :
1139 o->height,
1140 fullscreen, no_input,
1141 configured_name) < 0)
1142 goto err_x11_input;
1143 x += option_width ? option_width : o->width;
1144 output_count++;
1145 if (option_count && output_count >= option_count)
1146 break;
1147 }
1148
1149 for (i = output_count; i < count; i++) {
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001150 if (x11_compositor_create_output(c, x, 0, width, height,
Scott Moreau248aaec2012-08-03 14:19:52 -06001151 fullscreen, no_input, name) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001152 goto err_x11_input;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001153 x += width;
1154 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001155
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001156 c->xcb_source =
Kristian Høgsberg22ba60e2012-03-12 01:18:24 -04001157 wl_event_loop_add_fd(c->base.input_loop,
1158 xcb_get_file_descriptor(c->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001159 WL_EVENT_READABLE,
1160 x11_compositor_handle_event, c);
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001161 wl_event_source_check(c->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001162
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001163 return &c->base;
Martin Olsson11434bb2012-07-08 03:03:44 +02001164
1165err_x11_input:
1166 x11_input_destroy(c);
1167err_egl:
1168 x11_compositor_fini_egl(c);
1169err_xdisplay:
1170 XCloseDisplay(c->dpy);
1171err_free:
1172 free(c);
1173 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001174}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001175
Scott Moreau248aaec2012-08-03 14:19:52 -06001176static void
1177output_section_done(void *data)
1178{
1179 struct x11_configured_output *output;
1180
1181 output = malloc(sizeof *output);
1182
1183 if (!output || !output_name || !output_mode) {
1184 free(output_name);
1185 output_name = NULL;
1186 free(output_mode);
1187 output_mode = NULL;
1188 return;
1189 }
1190
1191 output->name = output_name;
1192
1193 if (output_name[0] != 'X') {
1194 x11_free_configured_output(output);
1195 return;
1196 }
1197
1198 if (sscanf(output_mode, "%dx%d", &output->width, &output->height) != 2) {
1199 weston_log("Invalid mode \"%s\" for output %s\n",
1200 output_mode, output_name);
1201 x11_free_configured_output(output);
1202 return;
1203 }
1204
1205 free(output_mode);
1206 output_mode = NULL;
1207
1208 wl_list_insert(configured_output_list.prev, &output->link);
1209}
1210
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001211WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -04001212backend_init(struct wl_display *display, int argc, char *argv[],
1213 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001214{
Scott Moreau248aaec2012-08-03 14:19:52 -06001215 int fullscreen = 0;
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001216 int no_input = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001217
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001218 const struct weston_option x11_options[] = {
Scott Moreau248aaec2012-08-03 14:19:52 -06001219 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1220 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001221 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Scott Moreau248aaec2012-08-03 14:19:52 -06001222 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001223 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001224 };
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001225
1226 parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001227
Scott Moreau248aaec2012-08-03 14:19:52 -06001228 wl_list_init(&configured_output_list);
1229
1230 const struct config_key x11_config_keys[] = {
1231 { "name", CONFIG_KEY_STRING, &output_name },
1232 { "mode", CONFIG_KEY_STRING, &output_mode },
1233 };
1234
1235 const struct config_section config_section[] = {
1236 { "output", x11_config_keys,
1237 ARRAY_LENGTH(x11_config_keys), output_section_done },
1238 };
1239
1240 parse_config_file(config_file, config_section,
1241 ARRAY_LENGTH(config_section), NULL);
1242
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001243 return x11_compositor_create(display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001244 fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001245 no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001246 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001247}