blob: daa9e156b93eae04a912318216ced5b01c71afb6 [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øgsberg99fd1012012-08-10 09:57:56 -0400531 weston_output_init(&output->base, &c->base, x, y, width, height);
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,
564 strlen(name), name);
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");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400584 return -1;
585 }
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");
Benjamin Franzke84290d02011-03-02 10:07:59 +0100589 return -1;
590 }
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
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400609 return 0;
610}
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{
628 struct weston_xkb_info *info = &c->base.seat->xkb_info;
629 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{
655 struct weston_compositor *ec = &c->base;
656 struct wl_seat *seat = &ec->seat->seat;
657
658 xkb_state_update_mask(c->base.seat->xkb_state.state,
659 get_xkb_mod_mask(c, state->baseMods),
660 get_xkb_mod_mask(c, state->latchedMods),
661 get_xkb_mod_mask(c, state->lockedMods),
662 0,
663 0,
664 state->group);
665
666 notify_modifiers(seat, wl_display_next_serial(c->base.wl_display));
667}
668#endif
669
Daniel Stone154c34c2012-06-22 13:21:40 +0100670/**
671 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
672 * the best we can do (given that XCB also lacks XI2 support), is to take
673 * the state from the core key events. Unfortunately that only gives us
674 * the effective (i.e. union of depressed/latched/locked) state, and we
675 * need the granularity.
676 *
677 * So we still update the state with every key event we see, but also use
678 * the state field from X11 events as a mask so we don't get any stuck
679 * modifiers.
680 */
681static void
682update_xkb_state_from_core(struct x11_compositor *c, uint16_t x11_mask)
683{
684 uint32_t mask = get_xkb_mod_mask(c, x11_mask);
685 struct wl_keyboard *keyboard = &c->base.seat->keyboard;
686
687 xkb_state_update_mask(c->base.seat->xkb_state.state,
688 keyboard->modifiers.mods_depressed & mask,
689 keyboard->modifiers.mods_latched & mask,
690 keyboard->modifiers.mods_locked & mask,
691 0,
692 0,
693 (x11_mask >> 13) & 3);
694 notify_modifiers(&c->base.seat->seat,
695 wl_display_next_serial(c->base.wl_display));
696}
697
Tiago Vignattia3a71622011-12-08 17:03:17 +0200698static void
699x11_compositor_deliver_button_event(struct x11_compositor *c,
700 xcb_generic_event_t *event, int state)
701{
702 xcb_button_press_event_t *button_event =
703 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +0100704 uint32_t button;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200705
Daniel Stone154c34c2012-06-22 13:21:40 +0100706 if (!c->has_xkb)
707 update_xkb_state_from_core(c, button_event->state);
708
Tiago Vignattia3a71622011-12-08 17:03:17 +0200709 switch (button_event->detail) {
710 default:
711 button = button_event->detail + BTN_LEFT - 1;
712 break;
713 case 2:
714 button = BTN_MIDDLE;
715 break;
716 case 3:
717 button = BTN_RIGHT;
718 break;
719 case 4:
Scott Moreau210d0792012-03-22 10:47:01 -0600720 if (state)
Daniel Stone37816df2012-05-16 18:45:18 +0100721 notify_axis(&c->base.seat->seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600722 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100723 WL_POINTER_AXIS_VERTICAL_SCROLL,
724 wl_fixed_from_int(1));
Scott Moreau210d0792012-03-22 10:47:01 -0600725 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200726 case 5:
Scott Moreau210d0792012-03-22 10:47:01 -0600727 if (state)
Daniel Stone37816df2012-05-16 18:45:18 +0100728 notify_axis(&c->base.seat->seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600729 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100730 WL_POINTER_AXIS_VERTICAL_SCROLL,
731 wl_fixed_from_int(-1));
Scott Moreau210d0792012-03-22 10:47:01 -0600732 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200733 case 6:
Scott Moreau210d0792012-03-22 10:47:01 -0600734 if (state)
Daniel Stone37816df2012-05-16 18:45:18 +0100735 notify_axis(&c->base.seat->seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600736 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100737 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
738 wl_fixed_from_int(1));
Scott Moreau210d0792012-03-22 10:47:01 -0600739 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200740 case 7:
Scott Moreau210d0792012-03-22 10:47:01 -0600741 if (state)
Daniel Stone37816df2012-05-16 18:45:18 +0100742 notify_axis(&c->base.seat->seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600743 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100744 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
745 wl_fixed_from_int(-1));
Tiago Vignattia3a71622011-12-08 17:03:17 +0200746 return;
747 }
748
Daniel Stone37816df2012-05-16 18:45:18 +0100749 notify_button(&c->base.seat->seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100750 weston_compositor_get_time(), button,
751 state ? WL_POINTER_BUTTON_STATE_PRESSED :
752 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200753}
754
Kristian Høgsbergee724822011-04-21 14:51:44 -0400755static int
756x11_compositor_next_event(struct x11_compositor *c,
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400757 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -0400758{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400759 if (mask & WL_EVENT_READABLE) {
760 *event = xcb_poll_for_event(c->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -0400761 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400762#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400763 *event = xcb_poll_for_queued_event(c->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400764#else
765 *event = xcb_poll_for_event(c->conn);
766#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -0400767 }
768
769 return *event != NULL;
770}
771
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400772static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400773x11_compositor_handle_event(int fd, uint32_t mask, void *data)
774{
775 struct x11_compositor *c = data;
776 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400777 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400778 xcb_client_message_event_t *client_message;
779 xcb_motion_notify_event_t *motion_notify;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500780 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400781 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500782 xcb_keymap_notify_event_t *keymap_notify;
783 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400784 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400785 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500786 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400787 uint32_t i, set;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400788 wl_fixed_t x, y;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400789 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400790
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400791 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400792 count = 0;
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400793 while (x11_compositor_next_event(c, &event, mask)) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400794 switch (prev ? prev->response_type & ~0x80 : 0x80) {
795 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100796 /* Suppress key repeat events; this is only used if we
797 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400798 key_release = (xcb_key_press_event_t *) prev;
799 key_press = (xcb_key_press_event_t *) event;
800 if ((event->response_type & ~0x80) == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -0700801 key_release->time == key_press->time &&
802 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400803 /* Don't deliver the held key release
804 * event or the new key press event. */
805 free(event);
806 free(prev);
807 prev = NULL;
808 continue;
809 } else {
810 /* Deliver the held key release now
811 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400812 * event below. */
Daniel Stone154c34c2012-06-22 13:21:40 +0100813 update_xkb_state_from_core(c, key_release->state);
Daniel Stone37816df2012-05-16 18:45:18 +0100814 notify_key(&c->base.seat->seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500815 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100816 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100817 WL_KEYBOARD_KEY_STATE_RELEASED,
818 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400819 free(prev);
820 prev = NULL;
821 break;
822 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400823
824 case XCB_FOCUS_IN:
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400825 /* assert event is keymap_notify */
826 focus_in = (xcb_focus_in_event_t *) prev;
827 keymap_notify = (xcb_keymap_notify_event_t *) event;
828 c->keys.size = 0;
829 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
830 set = keymap_notify->keys[i >> 3] &
831 (1 << (i & 7));
832 if (set) {
833 k = wl_array_add(&c->keys, sizeof *k);
834 *k = i;
835 }
836 }
837
838 output = x11_compositor_find_output(c, focus_in->event);
Daniel Stoned6da09e2012-06-22 13:21:29 +0100839 /* Unfortunately the state only comes with the enter
840 * event, rather than with the focus event. I'm not
841 * sure of the exact semantics around it and whether
842 * we can ensure that we get both? */
843 notify_keyboard_focus_in(&c->base.seat->seat, &c->keys,
844 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400845
846 free(prev);
847 prev = NULL;
848 break;
849
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400850 default:
851 /* No previous event held */
852 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400853 }
854
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400855 switch (event->response_type & ~0x80) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400856 case XCB_KEY_PRESS:
857 key_press = (xcb_key_press_event_t *) event;
Daniel Stone154c34c2012-06-22 13:21:40 +0100858 if (!c->has_xkb)
859 update_xkb_state_from_core(c, key_press->state);
Daniel Stone37816df2012-05-16 18:45:18 +0100860 notify_key(&c->base.seat->seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500861 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100862 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100863 WL_KEYBOARD_KEY_STATE_PRESSED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100864 c->has_xkb ? STATE_UPDATE_NONE :
865 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400866 break;
867 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100868 /* If we don't have XKB, we need to use the lame
869 * autorepeat detection above. */
870 if (!c->has_xkb) {
871 prev = event;
872 break;
873 }
874 key_release = (xcb_key_press_event_t *) event;
875 notify_key(&c->base.seat->seat,
876 weston_compositor_get_time(),
877 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100878 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100879 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400880 break;
881 case XCB_BUTTON_PRESS:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200882 x11_compositor_deliver_button_event(c, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400883 break;
884 case XCB_BUTTON_RELEASE:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200885 x11_compositor_deliver_button_event(c, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400886 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400887 case XCB_MOTION_NOTIFY:
888 motion_notify = (xcb_motion_notify_event_t *) event;
Daniel Stone154c34c2012-06-22 13:21:40 +0100889 if (!c->has_xkb)
890 update_xkb_state_from_core(c, motion_notify->state);
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700891 output = x11_compositor_find_output(c, motion_notify->event);
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400892 x = wl_fixed_from_int(output->base.x + motion_notify->event_x);
893 y = wl_fixed_from_int(output->base.y + motion_notify->event_y);
Daniel Stone37816df2012-05-16 18:45:18 +0100894 notify_motion(&c->base.seat->seat,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400895 weston_compositor_get_time(), x, y);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400896 break;
897
898 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400899 expose = (xcb_expose_event_t *) event;
900 output = x11_compositor_find_output(c, expose->window);
901 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400902 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400903
904 case XCB_ENTER_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500905 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500906 if (enter_notify->state >= Button1Mask)
907 break;
Daniel Stone154c34c2012-06-22 13:21:40 +0100908 if (!c->has_xkb)
909 update_xkb_state_from_core(c, enter_notify->state);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500910 output = x11_compositor_find_output(c, enter_notify->event);
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400911 x = wl_fixed_from_int(output->base.x + enter_notify->event_x);
912 y = wl_fixed_from_int(output->base.y + enter_notify->event_y);
913
Daniel Stone37816df2012-05-16 18:45:18 +0100914 notify_pointer_focus(&c->base.seat->seat,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400915 &output->base, x, y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400916 break;
917
918 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500919 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500920 if (enter_notify->state >= Button1Mask)
921 break;
Daniel Stone154c34c2012-06-22 13:21:40 +0100922 if (!c->has_xkb)
923 update_xkb_state_from_core(c, enter_notify->state);
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700924 output = x11_compositor_find_output(c, enter_notify->event);
Daniel Stone37816df2012-05-16 18:45:18 +0100925 notify_pointer_focus(&c->base.seat->seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400926 break;
927
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400928 case XCB_CLIENT_MESSAGE:
929 client_message = (xcb_client_message_event_t *) event;
930 atom = client_message->data.data32[0];
931 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500932 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400933 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400934
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500935 case XCB_FOCUS_IN:
936 focus_in = (xcb_focus_in_event_t *) event;
937 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
938 break;
939
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400940 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500941 break;
942
943 case XCB_FOCUS_OUT:
944 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -0500945 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
946 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500947 break;
Daniel Stoned6da09e2012-06-22 13:21:29 +0100948 notify_keyboard_focus_out(&c->base.seat->seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500949 break;
950
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500951 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400952 break;
953 }
954
Daniel Stonee2faa122012-06-22 13:21:39 +0100955#ifdef HAVE_XCB_XKB
956 if (c->has_xkb &&
957 (event->response_type & ~0x80) == c->xkb_event_base) {
958 xcb_xkb_state_notify_event_t *state =
959 (xcb_xkb_state_notify_event_t *) event;
960 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
961 update_xkb_state(c, state);
962 }
963#endif
964
Bill Spitzakb715cec2012-06-05 17:08:23 -0400965 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400966 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400967 free (event);
968 }
969
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400970 switch (prev ? prev->response_type & ~0x80 : 0x80) {
971 case XCB_KEY_RELEASE:
972 key_release = (xcb_key_press_event_t *) prev;
Daniel Stone154c34c2012-06-22 13:21:40 +0100973 update_xkb_state_from_core(c, key_release->state);
Daniel Stone37816df2012-05-16 18:45:18 +0100974 notify_key(&c->base.seat->seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500975 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100976 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100977 WL_KEYBOARD_KEY_STATE_RELEASED,
978 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400979 free(prev);
980 prev = NULL;
981 break;
982 default:
983 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500984 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400985
Bill Spitzakb715cec2012-06-05 17:08:23 -0400986 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -0400987}
988
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400989#define F(field) offsetof(struct x11_compositor, field)
990
991static void
992x11_compositor_get_resources(struct x11_compositor *c)
993{
994 static const struct { const char *name; int offset; } atoms[] = {
995 { "WM_PROTOCOLS", F(atom.wm_protocols) },
996 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
997 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
998 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500999 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001000 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001001 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001002 { "_NET_WM_STATE", F(atom.net_wm_state) },
1003 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001004 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001005 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001006 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001007 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001008 };
1009
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001010 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001011 xcb_intern_atom_reply_t *reply;
1012 xcb_pixmap_t pixmap;
1013 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001014 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001015 uint8_t data[] = { 0, 0, 0, 0 };
1016
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001017 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001018 cookies[i] = xcb_intern_atom (c->conn, 0,
1019 strlen(atoms[i].name),
1020 atoms[i].name);
1021
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001022 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001023 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
1024 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
1025 free(reply);
1026 }
1027
1028 pixmap = xcb_generate_id(c->conn);
1029 gc = xcb_generate_id(c->conn);
1030 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
1031 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
1032 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
1033 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
1034 c->null_cursor = xcb_generate_id(c->conn);
1035 xcb_create_cursor (c->conn, c->null_cursor,
1036 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
1037 xcb_free_gc(c->conn, gc);
1038 xcb_free_pixmap(c->conn, pixmap);
1039}
1040
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001041static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001042x11_restore(struct weston_compositor *ec)
1043{
1044}
1045
1046static void
Scott Moreau248aaec2012-08-03 14:19:52 -06001047x11_free_configured_output(struct x11_configured_output *output)
1048{
1049 free(output->name);
1050 free(output);
1051}
1052
1053static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001054x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001055{
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001056 struct x11_compositor *compositor = (struct x11_compositor *)ec;
Scott Moreau248aaec2012-08-03 14:19:52 -06001057 struct x11_configured_output *o, *n;
1058
1059 wl_list_for_each_safe(o, n, &configured_output_list, link)
1060 x11_free_configured_output(o);
Matt Roper361d2ad2011-08-29 13:52:23 -07001061
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001062 wl_event_source_remove(compositor->xcb_source);
1063 x11_input_destroy(compositor);
1064
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001065 weston_compositor_shutdown(ec); /* destroys outputs, too */
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001066
1067 x11_compositor_fini_egl(compositor);
1068
1069 XCloseDisplay(compositor->dpy);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001070 free(ec);
1071}
1072
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001073static struct weston_compositor *
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001074x11_compositor_create(struct wl_display *display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001075 int fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001076 int no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001077 int argc, char *argv[], const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001078{
Scott Moreau248aaec2012-08-03 14:19:52 -06001079 static const char name[] = "Weston Compositor";
1080 char configured_name[32];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001081 struct x11_compositor *c;
Scott Moreau248aaec2012-08-03 14:19:52 -06001082 struct x11_configured_output *o;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001083 xcb_screen_iterator_t s;
Scott Moreau248aaec2012-08-03 14:19:52 -06001084 int i, x = 0, output_count = 0;
1085 int width, height, count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001086
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001087 weston_log("initializing x11 backend\n");
1088
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001089 c = malloc(sizeof *c);
1090 if (c == NULL)
1091 return NULL;
1092
1093 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001094
Daniel Stone725c2c32012-06-22 14:04:36 +01001095 if (weston_compositor_init(&c->base, display, argc, argv,
1096 config_file) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001097 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001098
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001099 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +02001100 if (c->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001101 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001102
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001103 c->conn = XGetXCBConnection(c->dpy);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001104 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
1105
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001106 if (xcb_connection_has_error(c->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001107 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001108
1109 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
1110 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001111 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001112
1113 x11_compositor_get_resources(c);
1114
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001115 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +02001116 if (x11_compositor_init_egl(c) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001117 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001118
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001119 c->base.destroy = x11_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001120 c->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001121
Daniel Stone725c2c32012-06-22 14:04:36 +01001122 if (weston_compositor_init_gl(&c->base) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001123 goto err_egl;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001124
Daniel Stone22815f92012-06-22 13:21:34 +01001125 if (x11_input_create(c, no_input) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001126 goto err_egl;
Daniel Stone22815f92012-06-22 13:21:34 +01001127
Scott Moreau248aaec2012-08-03 14:19:52 -06001128 width = option_width ? option_width : 1024;
1129 height = option_height ? option_height : 640;
1130 count = option_count ? option_count : 1;
1131
1132 wl_list_for_each(o, &configured_output_list, link) {
1133 sprintf(configured_name, "%s - %s", name, o->name);
1134 if (x11_compositor_create_output(c, x, 0,
1135 option_width ? option_width :
1136 o->width,
1137 option_height ? option_height :
1138 o->height,
1139 fullscreen, no_input,
1140 configured_name) < 0)
1141 goto err_x11_input;
1142 x += option_width ? option_width : o->width;
1143 output_count++;
1144 if (option_count && output_count >= option_count)
1145 break;
1146 }
1147
1148 for (i = output_count; i < count; i++) {
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001149 if (x11_compositor_create_output(c, x, 0, width, height,
Scott Moreau248aaec2012-08-03 14:19:52 -06001150 fullscreen, no_input, name) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001151 goto err_x11_input;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001152 x += width;
1153 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001154
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001155 c->xcb_source =
Kristian Høgsberg22ba60e2012-03-12 01:18:24 -04001156 wl_event_loop_add_fd(c->base.input_loop,
1157 xcb_get_file_descriptor(c->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001158 WL_EVENT_READABLE,
1159 x11_compositor_handle_event, c);
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001160 wl_event_source_check(c->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001161
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001162 return &c->base;
Martin Olsson11434bb2012-07-08 03:03:44 +02001163
1164err_x11_input:
1165 x11_input_destroy(c);
1166err_egl:
1167 x11_compositor_fini_egl(c);
1168err_xdisplay:
1169 XCloseDisplay(c->dpy);
1170err_free:
1171 free(c);
1172 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001173}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001174
Scott Moreau248aaec2012-08-03 14:19:52 -06001175static void
1176output_section_done(void *data)
1177{
1178 struct x11_configured_output *output;
1179
1180 output = malloc(sizeof *output);
1181
1182 if (!output || !output_name || !output_mode) {
1183 free(output_name);
1184 output_name = NULL;
Scott Moreauca9b09d2012-08-06 01:27:06 -06001185 goto err_free;
Scott Moreau248aaec2012-08-03 14:19:52 -06001186 }
1187
1188 output->name = output_name;
1189
1190 if (output_name[0] != 'X') {
1191 x11_free_configured_output(output);
Scott Moreauca9b09d2012-08-06 01:27:06 -06001192 goto err_free;
Scott Moreau248aaec2012-08-03 14:19:52 -06001193 }
1194
1195 if (sscanf(output_mode, "%dx%d", &output->width, &output->height) != 2) {
1196 weston_log("Invalid mode \"%s\" for output %s\n",
1197 output_mode, output_name);
1198 x11_free_configured_output(output);
Scott Moreauca9b09d2012-08-06 01:27:06 -06001199 goto err_free;
Scott Moreau248aaec2012-08-03 14:19:52 -06001200 }
1201
Scott Moreauca9b09d2012-08-06 01:27:06 -06001202 wl_list_insert(configured_output_list.prev, &output->link);
1203
1204err_free:
Scott Moreau248aaec2012-08-03 14:19:52 -06001205 free(output_mode);
1206 output_mode = NULL;
Scott Moreau248aaec2012-08-03 14:19:52 -06001207}
1208
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001209WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -04001210backend_init(struct wl_display *display, int argc, char *argv[],
1211 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001212{
Scott Moreau248aaec2012-08-03 14:19:52 -06001213 int fullscreen = 0;
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001214 int no_input = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001215
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001216 const struct weston_option x11_options[] = {
Scott Moreau248aaec2012-08-03 14:19:52 -06001217 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1218 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001219 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Scott Moreau248aaec2012-08-03 14:19:52 -06001220 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001221 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001222 };
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001223
1224 parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001225
Scott Moreau248aaec2012-08-03 14:19:52 -06001226 wl_list_init(&configured_output_list);
1227
1228 const struct config_key x11_config_keys[] = {
1229 { "name", CONFIG_KEY_STRING, &output_name },
1230 { "mode", CONFIG_KEY_STRING, &output_mode },
1231 };
1232
1233 const struct config_section config_section[] = {
1234 { "output", x11_config_keys,
1235 ARRAY_LENGTH(x11_config_keys), output_section_done },
1236 };
1237
1238 parse_config_file(config_file, config_section,
1239 ARRAY_LENGTH(config_section), NULL);
1240
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001241 return x11_compositor_create(display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001242 fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001243 no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001244 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001245}