blob: e616f453fbc121c5872d74640f92d80438ee6ad0 [file] [log] [blame]
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2010-2011 Intel Corporation
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04005 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040014 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040015 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040022 */
23
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +080024#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
John Kåre Alsaker143a8982012-10-12 12:25:07 +020028#include <assert.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040029#include <stddef.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040030#include <stdlib.h>
31#include <string.h>
32#include <fcntl.h>
33#include <unistd.h>
34#include <errno.h>
35#include <sys/time.h>
Kristian Høgsbergf9112b22010-06-14 12:53:43 -040036#include <linux/input.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040037
38#include <xcb/xcb.h>
Daniel Stone62b33b62012-06-22 13:21:35 +010039#ifdef HAVE_XCB_XKB
40#include <xcb/xkb.h>
41#endif
42
Benjamin Franzke3b288af2011-02-20 19:58:42 +010043#include <X11/Xlib.h>
44#include <X11/Xlib-xcb.h>
45
Daniel Stone576cd152012-06-01 12:14:02 +010046#include <xkbcommon/xkbcommon.h>
47
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040048#include <GLES2/gl2.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040049#include <EGL/egl.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040050
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040051#include "compositor.h"
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040052#include "../shared/config-parser.h"
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040053
Jonas Ådahl62efe202012-09-27 18:40:41 +020054#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
55
Scott Moreau248aaec2012-08-03 14:19:52 -060056static char *output_name;
57static char *output_mode;
Scott Moreau1bad5db2012-08-18 01:04:05 -060058static char *output_transform;
Scott Moreau248aaec2012-08-03 14:19:52 -060059static int option_width;
60static int option_height;
61static int option_count;
62static struct wl_list configured_output_list;
63
64struct x11_configured_output {
65 char *name;
66 int width, height;
Scott Moreau1bad5db2012-08-18 01:04:05 -060067 uint32_t transform;
Scott Moreau248aaec2012-08-03 14:19:52 -060068 struct wl_list link;
69};
70
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040071struct x11_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050072 struct weston_compositor base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040073
Benjamin Franzke3b288af2011-02-20 19:58:42 +010074 Display *dpy;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040075 xcb_connection_t *conn;
76 xcb_screen_t *screen;
77 xcb_cursor_t null_cursor;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050078 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040079 struct wl_event_source *xcb_source;
Daniel Stone576cd152012-06-01 12:14:02 +010080 struct xkb_keymap *xkb_keymap;
Daniel Stone62b33b62012-06-22 13:21:35 +010081 unsigned int has_xkb;
82 uint8_t xkb_event_base;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040083
84 /* We could map multi-pointer X to multiple wayland seats, but
85 * for now we only support core X input. */
86 struct weston_seat core_seat;
87
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040088 struct {
89 xcb_atom_t wm_protocols;
90 xcb_atom_t wm_normal_hints;
91 xcb_atom_t wm_size_hints;
92 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050093 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040094 xcb_atom_t net_wm_name;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050095 xcb_atom_t net_wm_icon;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -040096 xcb_atom_t net_wm_state;
97 xcb_atom_t net_wm_state_fullscreen;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050098 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040099 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500100 xcb_atom_t cardinal;
Daniel Stone855028f2012-05-01 20:37:10 +0100101 xcb_atom_t xkb_names;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400102 } atom;
103};
104
105struct x11_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500106 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400107
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400108 xcb_window_t window;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500109 struct weston_mode mode;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400110 struct wl_event_source *finish_frame_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400111};
112
Daniel Stone576cd152012-06-01 12:14:02 +0100113static struct xkb_keymap *
114x11_compositor_get_keymap(struct x11_compositor *c)
115{
116 xcb_get_property_cookie_t cookie;
117 xcb_get_property_reply_t *reply;
118 xcb_generic_error_t *error;
119 struct xkb_rule_names names;
120 struct xkb_keymap *ret;
121 const char *value_all, *value_part;
122 int length_all, length_part;
123
124 memset(&names, 0, sizeof(names));
125
126 cookie = xcb_get_property(c->conn, 0, c->screen->root,
127 c->atom.xkb_names, c->atom.string, 0, 1024);
128 reply = xcb_get_property_reply(c->conn, cookie, &error);
129 if (reply == NULL)
130 return NULL;
131
132 value_all = xcb_get_property_value(reply);
133 length_all = xcb_get_property_value_length(reply);
134 value_part = value_all;
135
136#define copy_prop_value(to) \
137 length_part = strlen(value_part); \
138 if (value_part + length_part < (value_all + length_all) && \
139 length_part > 0) \
140 names.to = value_part; \
141 value_part += length_part + 1;
142
143 copy_prop_value(rules);
144 copy_prop_value(model);
145 copy_prop_value(layout);
146 copy_prop_value(variant);
147 copy_prop_value(options);
148#undef copy_prop_value
149
150 ret = xkb_map_new_from_names(c->base.xkb_context, &names, 0);
151
152 free(reply);
153 return ret;
154}
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400155
Daniel Stone62b33b62012-06-22 13:21:35 +0100156static void
157x11_compositor_setup_xkb(struct x11_compositor *c)
158{
159#ifndef HAVE_XCB_XKB
160 weston_log("XCB-XKB not available during build\n");
161 c->has_xkb = 0;
162 c->xkb_event_base = 0;
163 return;
164#else
165 const xcb_query_extension_reply_t *ext;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100166 xcb_generic_error_t *error;
Daniel Stonee2faa122012-06-22 13:21:39 +0100167 xcb_void_cookie_t select;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100168 xcb_xkb_per_client_flags_cookie_t pcf;
169 xcb_xkb_per_client_flags_reply_t *pcf_reply;
Daniel Stone62b33b62012-06-22 13:21:35 +0100170
171 c->has_xkb = 0;
172 c->xkb_event_base = 0;
173
174 ext = xcb_get_extension_data(c->conn, &xcb_xkb_id);
175 if (!ext) {
176 weston_log("XKB extension not available on host X11 server\n");
177 return;
178 }
179 c->xkb_event_base = ext->first_event;
180
Daniel Stonee2faa122012-06-22 13:21:39 +0100181 select = xcb_xkb_select_events(c->conn,
182 XCB_XKB_ID_USE_CORE_KBD,
183 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
184 0,
185 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
186 0,
187 0,
188 NULL);
189 error = xcb_request_check(c->conn, select);
190 if (error) {
191 weston_log("error: failed to select for XKB state events\n");
192 return;
193 }
194
Daniel Stone3ee91e12012-06-22 13:21:36 +0100195 pcf = xcb_xkb_per_client_flags(c->conn,
196 XCB_XKB_ID_USE_CORE_KBD,
197 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
198 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
199 0,
200 0,
201 0);
202 pcf_reply = xcb_xkb_per_client_flags_reply(c->conn, pcf, &error);
203 free(pcf_reply);
204 if (error) {
205 weston_log("failed to set XKB per-client flags, not using "
206 "detectable repeat\n");
207 return;
208 }
209
Daniel Stone62b33b62012-06-22 13:21:35 +0100210 c->has_xkb = 1;
211#endif
212}
213
Darxus55973f22010-11-22 21:24:39 -0500214static int
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300215x11_input_create(struct x11_compositor *c, int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400216{
Daniel Stone576cd152012-06-01 12:14:02 +0100217 struct xkb_keymap *keymap;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400218
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400219 weston_seat_init(&c->core_seat, &c->base);
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300220
221 if (no_input)
222 return 0;
223
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400224 weston_seat_init_pointer(&c->core_seat);
Daniel Stone576cd152012-06-01 12:14:02 +0100225
Daniel Stone62b33b62012-06-22 13:21:35 +0100226 x11_compositor_setup_xkb(c);
227
Daniel Stone576cd152012-06-01 12:14:02 +0100228 keymap = x11_compositor_get_keymap(c);
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400229 weston_seat_init_keyboard(&c->core_seat, keymap);
Daniel Stone576cd152012-06-01 12:14:02 +0100230 if (keymap)
231 xkb_map_unref(keymap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400232
Darxus55973f22010-11-22 21:24:39 -0500233 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400234}
235
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200236static void
237x11_input_destroy(struct x11_compositor *compositor)
238{
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400239 weston_seat_release(&compositor->core_seat);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200240}
241
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400242static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400243x11_compositor_init_egl(struct x11_compositor *c)
244{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400245 EGLint major, minor;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100246 EGLint n;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100247 EGLint config_attribs[] = {
248 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
249 EGL_RED_SIZE, 1,
250 EGL_GREEN_SIZE, 1,
251 EGL_BLUE_SIZE, 1,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100252 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
253 EGL_NONE
254 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400255
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400256 c->base.egl_display = eglGetDisplay(c->dpy);
257 if (c->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200258 weston_log("failed to create display\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400259 return -1;
260 }
261
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400262 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200263 weston_log("failed to initialize display\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400264 return -1;
265 }
266
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400267 if (!eglChooseConfig(c->base.egl_display, config_attribs,
268 &c->base.egl_config, 1, &n) || n == 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200269 weston_log("failed to choose config: %d\n", n);
Benjamin Franzke84290d02011-03-02 10:07:59 +0100270 return -1;
271 }
Darxus55973f22010-11-22 21:24:39 -0500272
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400273 return 0;
274}
275
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200276static void
277x11_compositor_fini_egl(struct x11_compositor *compositor)
278{
Kristian Høgsberg3a0de882012-09-06 21:44:24 -0400279 gles2_renderer_destroy(&compositor->base);
280
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400281 eglMakeCurrent(compositor->base.egl_display,
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200282 EGL_NO_SURFACE, EGL_NO_SURFACE,
283 EGL_NO_CONTEXT);
284
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400285 eglTerminate(compositor->base.egl_display);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200286 eglReleaseThread();
287}
288
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500289static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500290x11_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400291 pixman_region32_t *damage)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500292{
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500293 struct x11_output *output = (struct x11_output *)output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400294 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500295
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400296 ec->renderer->repaint_output(output_base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500297
298 wl_event_source_timer_update(output->finish_frame_timer, 10);
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500299}
300
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100301static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400302finish_frame_handler(void *data)
303{
304 struct x11_output *output = data;
305 uint32_t msec;
306 struct timeval tv;
307
308 gettimeofday(&tv, NULL);
309 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500310 weston_output_finish_frame(&output->base, msec);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400311
312 return 1;
313}
314
Matt Roper361d2ad2011-08-29 13:52:23 -0700315static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500316x11_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700317{
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200318 struct x11_output *output = (struct x11_output *)output_base;
319 struct x11_compositor *compositor =
320 (struct x11_compositor *)output->base.compositor;
321
322 wl_list_remove(&output->base.link);
323 wl_event_source_remove(output->finish_frame_timer);
324
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400325 eglDestroySurface(compositor->base.egl_display,
326 output->base.egl_surface);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200327
328 xcb_destroy_window(compositor->conn, output->window);
329
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500330 weston_output_destroy(&output->base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200331
332 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700333}
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200334
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400335static void
336x11_output_set_wm_protocols(struct x11_output *output)
337{
338 xcb_atom_t list[1];
339 struct x11_compositor *c =
340 (struct x11_compositor *) output->base.compositor;
341
342 list[0] = c->atom.wm_delete_window;
343 xcb_change_property (c->conn,
344 XCB_PROP_MODE_REPLACE,
345 output->window,
346 c->atom.wm_protocols,
347 XCB_ATOM_ATOM,
348 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500349 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400350 list);
351}
352
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400353static void
354x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state)
355{
356 xcb_client_message_event_t event;
357 struct x11_compositor *c =
358 (struct x11_compositor *) output->base.compositor;
359 xcb_screen_iterator_t iter;
360
361#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
362#define _NET_WM_STATE_ADD 1 /* add/set property */
363#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
364
365 memset(&event, 0, sizeof event);
366 event.response_type = XCB_CLIENT_MESSAGE;
367 event.format = 32;
368 event.window = output->window;
369 event.type = c->atom.net_wm_state;
370
371 event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
372 event.data.data32[1] = state;
373 event.data.data32[2] = 0;
374 event.data.data32[3] = 0;
375 event.data.data32[4] = 0;
376
377 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
378 xcb_send_event(c->conn, 0, iter.data->root,
379 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
380 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
381 (void *) &event);
382}
383
384
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400385struct wm_normal_hints {
386 uint32_t flags;
387 uint32_t pad[4];
388 int32_t min_width, min_height;
389 int32_t max_width, max_height;
390 int32_t width_inc, height_inc;
391 int32_t min_aspect_x, min_aspect_y;
392 int32_t max_aspect_x, max_aspect_y;
393 int32_t base_width, base_height;
394 int32_t win_gravity;
395};
396
397#define WM_NORMAL_HINTS_MIN_SIZE 16
398#define WM_NORMAL_HINTS_MAX_SIZE 32
399
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500400static void
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400401x11_output_set_icon(struct x11_compositor *c,
402 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500403{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400404 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400405 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400406 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500407
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400408 image = load_image(filename);
409 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500410 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400411 width = pixman_image_get_width(image);
412 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500413 icon = malloc(width * height * 4 + 8);
414 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400415 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500416 return;
417 }
418
419 icon[0] = width;
420 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400421 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500422 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
423 c->atom.net_wm_icon, c->atom.cardinal, 32,
424 width * height + 2, icon);
425 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400426 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500427}
428
Scott Moreau1bad5db2012-08-18 01:04:05 -0600429static struct x11_output *
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700430x11_compositor_create_output(struct x11_compositor *c, int x, int y,
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300431 int width, int height, int fullscreen,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600432 int no_input, char *configured_name,
433 uint32_t transform)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400434{
Scott Moreau1bad5db2012-08-18 01:04:05 -0600435 static const char name[] = "Weston Compositor";
Kristian Høgsberg86000402012-01-03 23:24:14 -0500436 static const char class[] = "weston-1\0Weston Compositor";
Scott Moreau1bad5db2012-08-18 01:04:05 -0600437 char title[32];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400438 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400439 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400440 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400441 struct wl_event_loop *loop;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400442 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300443 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400444 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300445 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400446 0
447 };
448
Scott Moreau1bad5db2012-08-18 01:04:05 -0600449 if (configured_name)
450 sprintf(title, "%s - %s", name, configured_name);
451 else
452 strcpy(title, name);
453
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300454 if (!no_input)
455 values[0] |=
456 XCB_EVENT_MASK_KEY_PRESS |
457 XCB_EVENT_MASK_KEY_RELEASE |
458 XCB_EVENT_MASK_BUTTON_PRESS |
459 XCB_EVENT_MASK_BUTTON_RELEASE |
460 XCB_EVENT_MASK_POINTER_MOTION |
461 XCB_EVENT_MASK_ENTER_WINDOW |
462 XCB_EVENT_MASK_LEAVE_WINDOW |
463 XCB_EVENT_MASK_KEYMAP_STATE |
464 XCB_EVENT_MASK_FOCUS_CHANGE;
465
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400466 output = malloc(sizeof *output);
467 if (output == NULL)
Scott Moreau1bad5db2012-08-18 01:04:05 -0600468 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400469
470 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400471
472 output->mode.flags =
473 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
474 output->mode.width = width;
475 output->mode.height = height;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400476 output->mode.refresh = 60000;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400477 wl_list_init(&output->base.mode_list);
478 wl_list_insert(&output->base.mode_list, &output->mode.link);
479
480 output->base.current = &output->mode;
Kristian Høgsberg12481582012-07-02 21:24:57 -0400481 output->base.make = "xwayland";
482 output->base.model = "none";
Scott Moreau1bad5db2012-08-18 01:04:05 -0600483 weston_output_init(&output->base, &c->base,
484 x, y, width, height, transform);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400485
486 values[1] = c->null_cursor;
487 output->window = xcb_generate_id(c->conn);
488 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
489 xcb_create_window(c->conn,
490 XCB_COPY_FROM_PARENT,
491 output->window,
492 iter.data->root,
493 0, 0,
494 width, height,
495 0,
496 XCB_WINDOW_CLASS_INPUT_OUTPUT,
497 iter.data->root_visual,
498 mask, values);
499
500 /* Don't resize me. */
501 memset(&normal_hints, 0, sizeof normal_hints);
502 normal_hints.flags =
503 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
504 normal_hints.min_width = width;
505 normal_hints.min_height = height;
506 normal_hints.max_width = width;
507 normal_hints.max_height = height;
508 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
509 c->atom.wm_normal_hints,
510 c->atom.wm_size_hints, 32,
511 sizeof normal_hints / 4,
512 (uint8_t *) &normal_hints);
513
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400514 /* Set window name. Don't bother with non-EWMH WMs. */
515 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
516 c->atom.net_wm_name, c->atom.utf8_string, 8,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600517 strlen(title), title);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500518 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
519 c->atom.wm_class, c->atom.string, 8,
520 sizeof class, class);
521
Kristian Høgsberg86000402012-01-03 23:24:14 -0500522 x11_output_set_icon(c, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500523
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500524 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400525
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400526 x11_output_set_wm_protocols(output);
527
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400528 if (fullscreen)
529 x11_output_change_state(output, 1,
530 c->atom.net_wm_state_fullscreen);
531
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400532 output->base.egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400533 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100534 output->window, NULL);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400535 if (!output->base.egl_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200536 weston_log("failed to create window surface\n");
Scott Moreau1bad5db2012-08-18 01:04:05 -0600537 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400538 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400539
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400540 loop = wl_display_get_event_loop(c->base.wl_display);
541 output->finish_frame_timer =
542 wl_event_loop_add_timer(loop, finish_frame_handler, output);
543
Alex Wubd3354b2012-04-17 17:20:49 +0800544 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500545 output->base.repaint = x11_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700546 output->base.destroy = x11_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800547 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200548 output->base.set_backlight = NULL;
549 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800550 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100551
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400552 wl_list_insert(c->base.output_list.prev, &output->base.link);
553
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400554 weston_log("x11 output %dx%d, window id %d\n",
555 width, height, output->window);
556
Scott Moreau1bad5db2012-08-18 01:04:05 -0600557 return output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400558}
559
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400560static struct x11_output *
561x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
562{
563 struct x11_output *output;
564
565 wl_list_for_each(output, &c->base.output_list, base.link) {
566 if (output->window == window)
567 return output;
568 }
569
570 return NULL;
571}
572
Daniel Stonee2faa122012-06-22 13:21:39 +0100573static uint32_t
574get_xkb_mod_mask(struct x11_compositor *c, uint32_t in)
575{
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400576 struct weston_xkb_info *info = &c->core_seat.xkb_info;
Daniel Stonee2faa122012-06-22 13:21:39 +0100577 uint32_t ret = 0;
578
579 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
580 ret |= (1 << info->shift_mod);
581 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
582 ret |= (1 << info->caps_mod);
583 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
584 ret |= (1 << info->ctrl_mod);
585 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
586 ret |= (1 << info->alt_mod);
587 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
588 ret |= (1 << info->mod2_mod);
589 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
590 ret |= (1 << info->mod3_mod);
591 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
592 ret |= (1 << info->super_mod);
593 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
594 ret |= (1 << info->mod5_mod);
595
596 return ret;
597}
598
Daniel Stone154c34c2012-06-22 13:21:40 +0100599#ifdef HAVE_XCB_XKB
Daniel Stonee2faa122012-06-22 13:21:39 +0100600static void
601update_xkb_state(struct x11_compositor *c, xcb_xkb_state_notify_event_t *state)
602{
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400603 xkb_state_update_mask(c->core_seat.xkb_state.state,
Daniel Stonee2faa122012-06-22 13:21:39 +0100604 get_xkb_mod_mask(c, state->baseMods),
605 get_xkb_mod_mask(c, state->latchedMods),
606 get_xkb_mod_mask(c, state->lockedMods),
607 0,
608 0,
609 state->group);
610
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400611 notify_modifiers(&c->core_seat,
612 wl_display_next_serial(c->base.wl_display));
Daniel Stonee2faa122012-06-22 13:21:39 +0100613}
614#endif
615
Daniel Stone154c34c2012-06-22 13:21:40 +0100616/**
617 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
618 * the best we can do (given that XCB also lacks XI2 support), is to take
619 * the state from the core key events. Unfortunately that only gives us
620 * the effective (i.e. union of depressed/latched/locked) state, and we
621 * need the granularity.
622 *
623 * So we still update the state with every key event we see, but also use
624 * the state field from X11 events as a mask so we don't get any stuck
625 * modifiers.
626 */
627static void
628update_xkb_state_from_core(struct x11_compositor *c, uint16_t x11_mask)
629{
630 uint32_t mask = get_xkb_mod_mask(c, x11_mask);
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400631 struct wl_keyboard *keyboard = &c->core_seat.keyboard;
Daniel Stone154c34c2012-06-22 13:21:40 +0100632
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400633 xkb_state_update_mask(c->core_seat.xkb_state.state,
Daniel Stone154c34c2012-06-22 13:21:40 +0100634 keyboard->modifiers.mods_depressed & mask,
635 keyboard->modifiers.mods_latched & mask,
636 keyboard->modifiers.mods_locked & mask,
637 0,
638 0,
639 (x11_mask >> 13) & 3);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400640 notify_modifiers(&c->core_seat,
Daniel Stone154c34c2012-06-22 13:21:40 +0100641 wl_display_next_serial(c->base.wl_display));
642}
643
Tiago Vignattia3a71622011-12-08 17:03:17 +0200644static void
645x11_compositor_deliver_button_event(struct x11_compositor *c,
646 xcb_generic_event_t *event, int state)
647{
648 xcb_button_press_event_t *button_event =
649 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +0100650 uint32_t button;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200651
Daniel Stone154c34c2012-06-22 13:21:40 +0100652 if (!c->has_xkb)
653 update_xkb_state_from_core(c, button_event->state);
654
Tiago Vignattia3a71622011-12-08 17:03:17 +0200655 switch (button_event->detail) {
656 default:
657 button = button_event->detail + BTN_LEFT - 1;
658 break;
659 case 2:
660 button = BTN_MIDDLE;
661 break;
662 case 3:
663 button = BTN_RIGHT;
664 break;
665 case 4:
Jonas Ådahl62efe202012-09-27 18:40:41 +0200666 /* Axis are measured in pixels, but the xcb events are discrete
667 * steps. Therefore move the axis by some pixels every step. */
Scott Moreau210d0792012-03-22 10:47:01 -0600668 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400669 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200670 weston_compositor_get_time(),
671 WL_POINTER_AXIS_VERTICAL_SCROLL,
672 -DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -0600673 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200674 case 5:
Scott Moreau210d0792012-03-22 10:47:01 -0600675 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400676 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200677 weston_compositor_get_time(),
678 WL_POINTER_AXIS_VERTICAL_SCROLL,
679 DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -0600680 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200681 case 6:
Scott Moreau210d0792012-03-22 10:47:01 -0600682 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400683 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200684 weston_compositor_get_time(),
685 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
686 -DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -0600687 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200688 case 7:
Scott Moreau210d0792012-03-22 10:47:01 -0600689 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400690 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200691 weston_compositor_get_time(),
692 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
693 DEFAULT_AXIS_STEP_DISTANCE);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200694 return;
695 }
696
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400697 notify_button(&c->core_seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100698 weston_compositor_get_time(), button,
699 state ? WL_POINTER_BUTTON_STATE_PRESSED :
700 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200701}
702
Scott Moreau1bad5db2012-08-18 01:04:05 -0600703static void
704x11_output_transform_coordinate(struct x11_output *x11_output,
705 wl_fixed_t *x, wl_fixed_t *y)
706{
707 struct weston_output *output = &x11_output->base;
708 wl_fixed_t tx, ty;
709 wl_fixed_t width = wl_fixed_from_int(output->width - 1);
710 wl_fixed_t height = wl_fixed_from_int(output->height - 1);
711
712 switch(output->transform) {
713 case WL_OUTPUT_TRANSFORM_NORMAL:
714 default:
715 tx = *x;
716 ty = *y;
717 break;
718 case WL_OUTPUT_TRANSFORM_90:
719 tx = *y;
720 ty = height - *x;
721 break;
722 case WL_OUTPUT_TRANSFORM_180:
723 tx = width - *x;
724 ty = height - *y;
725 break;
726 case WL_OUTPUT_TRANSFORM_270:
727 tx = width - *y;
728 ty = *x;
729 break;
730 case WL_OUTPUT_TRANSFORM_FLIPPED:
731 tx = width - *x;
732 ty = *y;
733 break;
734 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
735 tx = width - *y;
736 ty = height - *x;
737 break;
738 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
739 tx = *x;
740 ty = height - *y;
741 break;
742 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
743 tx = *y;
744 ty = *x;
745 break;
746 }
747
748 tx += wl_fixed_from_int(output->x);
749 ty += wl_fixed_from_int(output->y);
750
751 *x = tx;
752 *y = ty;
753}
754
755static void
756x11_compositor_deliver_motion_event(struct x11_compositor *c,
757 xcb_generic_event_t *event)
758{
759 struct x11_output *output;
760 wl_fixed_t x, y;
761 xcb_motion_notify_event_t *motion_notify =
762 (xcb_motion_notify_event_t *) event;
763
764 if (!c->has_xkb)
765 update_xkb_state_from_core(c, motion_notify->state);
766 output = x11_compositor_find_output(c, motion_notify->event);
767 x = wl_fixed_from_int(motion_notify->event_x);
768 y = wl_fixed_from_int(motion_notify->event_y);
769 x11_output_transform_coordinate(output, &x, &y);
770
771 notify_motion(&c->core_seat, weston_compositor_get_time(), x, y);
772}
773
774static void
775x11_compositor_deliver_enter_event(struct x11_compositor *c,
776 xcb_generic_event_t *event)
777{
778 struct x11_output *output;
779 wl_fixed_t x, y;
780
781 xcb_enter_notify_event_t *enter_notify =
782 (xcb_enter_notify_event_t *) event;
783 if (enter_notify->state >= Button1Mask)
784 return;
785 if (!c->has_xkb)
786 update_xkb_state_from_core(c, enter_notify->state);
787 output = x11_compositor_find_output(c, enter_notify->event);
788 x = wl_fixed_from_int(enter_notify->event_x);
789 y = wl_fixed_from_int(enter_notify->event_y);
790 x11_output_transform_coordinate(output, &x, &y);
791
792 notify_pointer_focus(&c->core_seat, &output->base, x, y);
793}
794
Kristian Høgsbergee724822011-04-21 14:51:44 -0400795static int
796x11_compositor_next_event(struct x11_compositor *c,
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400797 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -0400798{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400799 if (mask & WL_EVENT_READABLE) {
800 *event = xcb_poll_for_event(c->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -0400801 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400802#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400803 *event = xcb_poll_for_queued_event(c->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400804#else
805 *event = xcb_poll_for_event(c->conn);
806#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -0400807 }
808
809 return *event != NULL;
810}
811
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400812static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400813x11_compositor_handle_event(int fd, uint32_t mask, void *data)
814{
815 struct x11_compositor *c = data;
816 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400817 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400818 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500819 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400820 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500821 xcb_keymap_notify_event_t *keymap_notify;
822 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400823 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400824 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500825 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400826 uint32_t i, set;
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200827 uint8_t response_type;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400828 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400829
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400830 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400831 count = 0;
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400832 while (x11_compositor_next_event(c, &event, mask)) {
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200833 response_type = event->response_type & ~0x80;
834
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400835 switch (prev ? prev->response_type & ~0x80 : 0x80) {
836 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100837 /* Suppress key repeat events; this is only used if we
838 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400839 key_release = (xcb_key_press_event_t *) prev;
840 key_press = (xcb_key_press_event_t *) event;
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200841 if (response_type == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -0700842 key_release->time == key_press->time &&
843 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400844 /* Don't deliver the held key release
845 * event or the new key press event. */
846 free(event);
847 free(prev);
848 prev = NULL;
849 continue;
850 } else {
851 /* Deliver the held key release now
852 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400853 * event below. */
Daniel Stone154c34c2012-06-22 13:21:40 +0100854 update_xkb_state_from_core(c, key_release->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400855 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500856 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100857 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100858 WL_KEYBOARD_KEY_STATE_RELEASED,
859 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400860 free(prev);
861 prev = NULL;
862 break;
863 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400864
865 case XCB_FOCUS_IN:
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200866 assert(response_type == XCB_KEYMAP_NOTIFY);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400867 keymap_notify = (xcb_keymap_notify_event_t *) event;
868 c->keys.size = 0;
869 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
870 set = keymap_notify->keys[i >> 3] &
871 (1 << (i & 7));
872 if (set) {
873 k = wl_array_add(&c->keys, sizeof *k);
874 *k = i;
875 }
876 }
877
Daniel Stoned6da09e2012-06-22 13:21:29 +0100878 /* Unfortunately the state only comes with the enter
879 * event, rather than with the focus event. I'm not
880 * sure of the exact semantics around it and whether
881 * we can ensure that we get both? */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400882 notify_keyboard_focus_in(&c->core_seat, &c->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +0100883 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400884
885 free(prev);
886 prev = NULL;
887 break;
888
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400889 default:
890 /* No previous event held */
891 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400892 }
893
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200894 switch (response_type) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400895 case XCB_KEY_PRESS:
896 key_press = (xcb_key_press_event_t *) event;
Daniel Stone154c34c2012-06-22 13:21:40 +0100897 if (!c->has_xkb)
898 update_xkb_state_from_core(c, key_press->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400899 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500900 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100901 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100902 WL_KEYBOARD_KEY_STATE_PRESSED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100903 c->has_xkb ? STATE_UPDATE_NONE :
904 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400905 break;
906 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100907 /* If we don't have XKB, we need to use the lame
908 * autorepeat detection above. */
909 if (!c->has_xkb) {
910 prev = event;
911 break;
912 }
913 key_release = (xcb_key_press_event_t *) event;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400914 notify_key(&c->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +0100915 weston_compositor_get_time(),
916 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100917 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100918 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400919 break;
920 case XCB_BUTTON_PRESS:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200921 x11_compositor_deliver_button_event(c, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400922 break;
923 case XCB_BUTTON_RELEASE:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200924 x11_compositor_deliver_button_event(c, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400925 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400926 case XCB_MOTION_NOTIFY:
Scott Moreau1bad5db2012-08-18 01:04:05 -0600927 x11_compositor_deliver_motion_event(c, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400928 break;
929
930 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400931 expose = (xcb_expose_event_t *) event;
932 output = x11_compositor_find_output(c, expose->window);
933 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400934 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400935
936 case XCB_ENTER_NOTIFY:
Scott Moreau1bad5db2012-08-18 01:04:05 -0600937 x11_compositor_deliver_enter_event(c, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400938 break;
939
940 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500941 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500942 if (enter_notify->state >= Button1Mask)
943 break;
Daniel Stone154c34c2012-06-22 13:21:40 +0100944 if (!c->has_xkb)
945 update_xkb_state_from_core(c, enter_notify->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400946 notify_pointer_focus(&c->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400947 break;
948
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400949 case XCB_CLIENT_MESSAGE:
950 client_message = (xcb_client_message_event_t *) event;
951 atom = client_message->data.data32[0];
952 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500953 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400954 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400955
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500956 case XCB_FOCUS_IN:
957 focus_in = (xcb_focus_in_event_t *) event;
958 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
959 break;
960
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400961 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500962 break;
963
964 case XCB_FOCUS_OUT:
965 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -0500966 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
967 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500968 break;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400969 notify_keyboard_focus_out(&c->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500970 break;
971
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500972 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400973 break;
974 }
975
Daniel Stonee2faa122012-06-22 13:21:39 +0100976#ifdef HAVE_XCB_XKB
977 if (c->has_xkb &&
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200978 response_type == c->xkb_event_base) {
Daniel Stonee2faa122012-06-22 13:21:39 +0100979 xcb_xkb_state_notify_event_t *state =
980 (xcb_xkb_state_notify_event_t *) event;
981 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
982 update_xkb_state(c, state);
983 }
984#endif
985
Bill Spitzakb715cec2012-06-05 17:08:23 -0400986 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400987 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400988 free (event);
989 }
990
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400991 switch (prev ? prev->response_type & ~0x80 : 0x80) {
992 case XCB_KEY_RELEASE:
993 key_release = (xcb_key_press_event_t *) prev;
Daniel Stone154c34c2012-06-22 13:21:40 +0100994 update_xkb_state_from_core(c, key_release->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400995 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500996 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100997 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100998 WL_KEYBOARD_KEY_STATE_RELEASED,
999 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001000 free(prev);
1001 prev = NULL;
1002 break;
1003 default:
1004 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001005 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001006
Bill Spitzakb715cec2012-06-05 17:08:23 -04001007 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001008}
1009
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001010#define F(field) offsetof(struct x11_compositor, field)
1011
1012static void
1013x11_compositor_get_resources(struct x11_compositor *c)
1014{
1015 static const struct { const char *name; int offset; } atoms[] = {
1016 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1017 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1018 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1019 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001020 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001021 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001022 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001023 { "_NET_WM_STATE", F(atom.net_wm_state) },
1024 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001025 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001026 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001027 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001028 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001029 };
1030
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001031 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001032 xcb_intern_atom_reply_t *reply;
1033 xcb_pixmap_t pixmap;
1034 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001035 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001036 uint8_t data[] = { 0, 0, 0, 0 };
1037
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001038 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001039 cookies[i] = xcb_intern_atom (c->conn, 0,
1040 strlen(atoms[i].name),
1041 atoms[i].name);
1042
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001043 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001044 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
1045 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
1046 free(reply);
1047 }
1048
1049 pixmap = xcb_generate_id(c->conn);
1050 gc = xcb_generate_id(c->conn);
1051 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
1052 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
1053 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
1054 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
1055 c->null_cursor = xcb_generate_id(c->conn);
1056 xcb_create_cursor (c->conn, c->null_cursor,
1057 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
1058 xcb_free_gc(c->conn, gc);
1059 xcb_free_pixmap(c->conn, pixmap);
1060}
1061
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001062static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001063x11_restore(struct weston_compositor *ec)
1064{
1065}
1066
1067static void
Scott Moreau248aaec2012-08-03 14:19:52 -06001068x11_free_configured_output(struct x11_configured_output *output)
1069{
1070 free(output->name);
1071 free(output);
1072}
1073
1074static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001075x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001076{
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001077 struct x11_compositor *compositor = (struct x11_compositor *)ec;
Scott Moreau248aaec2012-08-03 14:19:52 -06001078 struct x11_configured_output *o, *n;
1079
1080 wl_list_for_each_safe(o, n, &configured_output_list, link)
1081 x11_free_configured_output(o);
Matt Roper361d2ad2011-08-29 13:52:23 -07001082
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001083 wl_event_source_remove(compositor->xcb_source);
1084 x11_input_destroy(compositor);
1085
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001086 weston_compositor_shutdown(ec); /* destroys outputs, too */
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001087
1088 x11_compositor_fini_egl(compositor);
1089
1090 XCloseDisplay(compositor->dpy);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001091 free(ec);
1092}
1093
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001094static struct weston_compositor *
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001095x11_compositor_create(struct wl_display *display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001096 int fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001097 int no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001098 int argc, char *argv[], const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001099{
1100 struct x11_compositor *c;
Scott Moreau248aaec2012-08-03 14:19:52 -06001101 struct x11_configured_output *o;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001102 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001103 xcb_screen_iterator_t s;
Scott Moreau248aaec2012-08-03 14:19:52 -06001104 int i, x = 0, output_count = 0;
1105 int width, height, count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001106
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001107 weston_log("initializing x11 backend\n");
1108
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001109 c = malloc(sizeof *c);
1110 if (c == NULL)
1111 return NULL;
1112
1113 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001114
Daniel Stone725c2c32012-06-22 14:04:36 +01001115 if (weston_compositor_init(&c->base, display, argc, argv,
1116 config_file) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001117 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001118
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001119 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +02001120 if (c->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001121 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001122
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001123 c->conn = XGetXCBConnection(c->dpy);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001124 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
1125
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001126 if (xcb_connection_has_error(c->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001127 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001128
1129 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
1130 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001131 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001132
1133 x11_compositor_get_resources(c);
1134
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001135 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +02001136 if (x11_compositor_init_egl(c) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001137 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001138
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001139 c->base.destroy = x11_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001140 c->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001141
Daniel Stone22815f92012-06-22 13:21:34 +01001142 if (x11_input_create(c, no_input) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001143 goto err_egl;
Daniel Stone22815f92012-06-22 13:21:34 +01001144
Scott Moreau248aaec2012-08-03 14:19:52 -06001145 width = option_width ? option_width : 1024;
1146 height = option_height ? option_height : 640;
1147 count = option_count ? option_count : 1;
1148
1149 wl_list_for_each(o, &configured_output_list, link) {
Scott Moreau1bad5db2012-08-18 01:04:05 -06001150 output = x11_compositor_create_output(c, x, 0,
1151 option_width ? width :
1152 o->width,
1153 option_height ? height :
1154 o->height,
1155 fullscreen, no_input,
1156 o->name, o->transform);
1157 if (output == NULL)
Scott Moreau248aaec2012-08-03 14:19:52 -06001158 goto err_x11_input;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001159
1160 x = pixman_region32_extents(&output->base.region)->x2;
1161
Scott Moreau248aaec2012-08-03 14:19:52 -06001162 output_count++;
1163 if (option_count && output_count >= option_count)
1164 break;
1165 }
1166
1167 for (i = output_count; i < count; i++) {
Scott Moreau1bad5db2012-08-18 01:04:05 -06001168 output = x11_compositor_create_output(c, x, 0, width, height,
1169 fullscreen, no_input, NULL,
1170 WL_OUTPUT_TRANSFORM_NORMAL);
1171 if (output == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001172 goto err_x11_input;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001173 x = pixman_region32_extents(&output->base.region)->x2;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001174 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001175
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001176 if (gles2_renderer_init(&c->base) < 0)
1177 goto err_egl;
1178
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001179 c->xcb_source =
Kristian Høgsberg22ba60e2012-03-12 01:18:24 -04001180 wl_event_loop_add_fd(c->base.input_loop,
1181 xcb_get_file_descriptor(c->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001182 WL_EVENT_READABLE,
1183 x11_compositor_handle_event, c);
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001184 wl_event_source_check(c->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001185
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001186 return &c->base;
Martin Olsson11434bb2012-07-08 03:03:44 +02001187
1188err_x11_input:
1189 x11_input_destroy(c);
1190err_egl:
1191 x11_compositor_fini_egl(c);
1192err_xdisplay:
1193 XCloseDisplay(c->dpy);
1194err_free:
1195 free(c);
1196 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001197}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001198
Scott Moreau248aaec2012-08-03 14:19:52 -06001199static void
Scott Moreau1bad5db2012-08-18 01:04:05 -06001200x11_output_set_transform(struct x11_configured_output *output)
1201{
1202 if (!output_transform) {
1203 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1204 return;
1205 }
1206
1207 if (!strcmp(output_transform, "normal"))
1208 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1209 else if (!strcmp(output_transform, "90"))
1210 output->transform = WL_OUTPUT_TRANSFORM_90;
1211 else if (!strcmp(output_transform, "180"))
1212 output->transform = WL_OUTPUT_TRANSFORM_180;
1213 else if (!strcmp(output_transform, "270"))
1214 output->transform = WL_OUTPUT_TRANSFORM_270;
1215 else if (!strcmp(output_transform, "flipped"))
1216 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
1217 else if (!strcmp(output_transform, "flipped-90"))
1218 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
1219 else if (!strcmp(output_transform, "flipped-180"))
1220 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
1221 else if (!strcmp(output_transform, "flipped-270"))
1222 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
1223 else {
1224 weston_log("Invalid transform \"%s\" for output %s\n",
1225 output_transform, output_name);
1226 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1227 }
1228}
1229
1230static void
Scott Moreau248aaec2012-08-03 14:19:52 -06001231output_section_done(void *data)
1232{
1233 struct x11_configured_output *output;
1234
1235 output = malloc(sizeof *output);
1236
Scott Moreau1bad5db2012-08-18 01:04:05 -06001237 if (!output || !output_name || (output_name[0] != 'X') ||
1238 (!output_mode && !output_transform)) {
1239 if (output_name)
1240 free(output_name);
Scott Moreau248aaec2012-08-03 14:19:52 -06001241 output_name = NULL;
John Kåre Alsaker5e48a852012-10-12 12:25:11 +02001242 free(output);
Scott Moreauca9b09d2012-08-06 01:27:06 -06001243 goto err_free;
Scott Moreau248aaec2012-08-03 14:19:52 -06001244 }
1245
1246 output->name = output_name;
1247
Scott Moreau1bad5db2012-08-18 01:04:05 -06001248 if (output_mode) {
1249 if (sscanf(output_mode, "%dx%d", &output->width,
1250 &output->height) != 2) {
1251 weston_log("Invalid mode \"%s\" for output %s\n",
1252 output_mode, output_name);
1253 x11_free_configured_output(output);
1254 goto err_free;
1255 }
1256 } else {
1257 output->width = 1024;
1258 output->height = 640;
Scott Moreau248aaec2012-08-03 14:19:52 -06001259 }
1260
Scott Moreau1bad5db2012-08-18 01:04:05 -06001261 x11_output_set_transform(output);
Scott Moreau248aaec2012-08-03 14:19:52 -06001262
Scott Moreauca9b09d2012-08-06 01:27:06 -06001263 wl_list_insert(configured_output_list.prev, &output->link);
1264
1265err_free:
Scott Moreau1bad5db2012-08-18 01:04:05 -06001266 if (output_mode)
1267 free(output_mode);
1268 if (output_transform)
1269 free(output_transform);
Scott Moreau248aaec2012-08-03 14:19:52 -06001270 output_mode = NULL;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001271 output_transform = NULL;
Scott Moreau248aaec2012-08-03 14:19:52 -06001272}
1273
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001274WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -04001275backend_init(struct wl_display *display, int argc, char *argv[],
1276 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001277{
Scott Moreau248aaec2012-08-03 14:19:52 -06001278 int fullscreen = 0;
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001279 int no_input = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001280
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001281 const struct weston_option x11_options[] = {
Scott Moreau248aaec2012-08-03 14:19:52 -06001282 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1283 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001284 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Scott Moreau248aaec2012-08-03 14:19:52 -06001285 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001286 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001287 };
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001288
1289 parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001290
Scott Moreau248aaec2012-08-03 14:19:52 -06001291 wl_list_init(&configured_output_list);
1292
1293 const struct config_key x11_config_keys[] = {
1294 { "name", CONFIG_KEY_STRING, &output_name },
1295 { "mode", CONFIG_KEY_STRING, &output_mode },
Scott Moreau1bad5db2012-08-18 01:04:05 -06001296 { "transform", CONFIG_KEY_STRING, &output_transform },
Scott Moreau248aaec2012-08-03 14:19:52 -06001297 };
1298
1299 const struct config_section config_section[] = {
1300 { "output", x11_config_keys,
1301 ARRAY_LENGTH(x11_config_keys), output_section_done },
1302 };
1303
1304 parse_config_file(config_file, config_section,
1305 ARRAY_LENGTH(config_section), NULL);
1306
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001307 return x11_compositor_create(display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001308 fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001309 no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001310 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001311}