blob: 1c53e3b58d56b8fe4826d78c4ed57d90db9a80d6 [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
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03004 * Copyright © 2013 Vasily Khoruzhick <anarsoul@gmail.com>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040013 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040026 */
27
Daniel Stonec228e232013-05-22 18:03:19 +030028#include "config.h"
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +080029
John Kåre Alsaker143a8982012-10-12 12:25:07 +020030#include <assert.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040031#include <stddef.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040032#include <stdlib.h>
33#include <string.h>
34#include <fcntl.h>
35#include <unistd.h>
36#include <errno.h>
37#include <sys/time.h>
Vasily Khoruzhickb3add192013-01-07 20:39:50 +030038#include <sys/shm.h>
Kristian Høgsbergf9112b22010-06-14 12:53:43 -040039#include <linux/input.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040040
41#include <xcb/xcb.h>
Vasily Khoruzhickb3add192013-01-07 20:39:50 +030042#include <xcb/shm.h>
Daniel Stone62b33b62012-06-22 13:21:35 +010043#ifdef HAVE_XCB_XKB
44#include <xcb/xkb.h>
45#endif
46
Benjamin Franzke3b288af2011-02-20 19:58:42 +010047#include <X11/Xlib.h>
48#include <X11/Xlib-xcb.h>
49
Daniel Stone576cd152012-06-01 12:14:02 +010050#include <xkbcommon/xkbcommon.h>
51
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040052#include "compositor.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010053#include "gl-renderer.h"
Vasily Khoruzhickb3add192013-01-07 20:39:50 +030054#include "pixman-renderer.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070055#include "shared/config-parser.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070056#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070057#include "shared/image-loader.h"
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020058#include "presentation_timing-server-protocol.h"
Pekka Paalanen5ffb4402014-06-12 16:52:53 +030059#include "linux-dmabuf.h"
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040060
Jonas Ådahl62efe202012-09-27 18:40:41 +020061#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
62
Scott Moreau248aaec2012-08-03 14:19:52 -060063static int option_width;
64static int option_height;
Jason Ekstrandd89a0942014-03-07 15:29:14 -060065static int option_scale;
Scott Moreau248aaec2012-08-03 14:19:52 -060066static int option_count;
Scott Moreau248aaec2012-08-03 14:19:52 -060067
Giulio Camuffo954f1832014-10-11 18:27:30 +030068struct x11_backend {
69 struct weston_backend base;
70 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040071
Benjamin Franzke3b288af2011-02-20 19:58:42 +010072 Display *dpy;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040073 xcb_connection_t *conn;
74 xcb_screen_t *screen;
75 xcb_cursor_t null_cursor;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050076 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040077 struct wl_event_source *xcb_source;
Daniel Stone576cd152012-06-01 12:14:02 +010078 struct xkb_keymap *xkb_keymap;
Daniel Stone62b33b62012-06-22 13:21:35 +010079 unsigned int has_xkb;
80 uint8_t xkb_event_base;
Vasily Khoruzhickd8943512013-01-07 22:36:02 +030081 int use_pixman;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040082
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -050083 int has_net_wm_state_fullscreen;
84
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040085 /* We could map multi-pointer X to multiple wayland seats, but
86 * for now we only support core X input. */
Kristian Høgsberg068b61c2013-02-25 17:04:47 -050087 struct weston_seat core_seat;
Kristian Høgsberg50065962013-03-27 15:14:07 -040088 wl_fixed_t prev_x;
89 wl_fixed_t prev_y;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040090
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040091 struct {
92 xcb_atom_t wm_protocols;
93 xcb_atom_t wm_normal_hints;
94 xcb_atom_t wm_size_hints;
95 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050096 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040097 xcb_atom_t net_wm_name;
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -050098 xcb_atom_t net_supporting_wm_check;
99 xcb_atom_t net_supported;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500100 xcb_atom_t net_wm_icon;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400101 xcb_atom_t net_wm_state;
102 xcb_atom_t net_wm_state_fullscreen;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500103 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400104 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500105 xcb_atom_t cardinal;
Daniel Stone855028f2012-05-01 20:37:10 +0100106 xcb_atom_t xkb_names;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400107 } atom;
108};
109
110struct x11_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500111 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400112
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400113 xcb_window_t window;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500114 struct weston_mode mode;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400115 struct wl_event_source *finish_frame_timer;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300116
117 xcb_gc_t gc;
118 xcb_shm_seg_t segment;
119 pixman_image_t *hw_surface;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300120 int shm_id;
121 void *buf;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300122 uint8_t depth;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200123 int32_t scale;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400124};
125
Derek Foremand540f4b2015-01-27 16:26:49 -0600126struct window_delete_data {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300127 struct x11_backend *backend;
Derek Foremand540f4b2015-01-27 16:26:49 -0600128 xcb_window_t window;
129};
130
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300131struct gl_renderer_interface *gl_renderer;
132
Daniel Stone576cd152012-06-01 12:14:02 +0100133static struct xkb_keymap *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300134x11_backend_get_keymap(struct x11_backend *b)
Daniel Stone576cd152012-06-01 12:14:02 +0100135{
136 xcb_get_property_cookie_t cookie;
137 xcb_get_property_reply_t *reply;
Daniel Stone576cd152012-06-01 12:14:02 +0100138 struct xkb_rule_names names;
139 struct xkb_keymap *ret;
140 const char *value_all, *value_part;
141 int length_all, length_part;
142
143 memset(&names, 0, sizeof(names));
144
Giulio Camuffo954f1832014-10-11 18:27:30 +0300145 cookie = xcb_get_property(b->conn, 0, b->screen->root,
146 b->atom.xkb_names, b->atom.string, 0, 1024);
147 reply = xcb_get_property_reply(b->conn, cookie, NULL);
Daniel Stone576cd152012-06-01 12:14:02 +0100148 if (reply == NULL)
149 return NULL;
150
151 value_all = xcb_get_property_value(reply);
152 length_all = xcb_get_property_value_length(reply);
153 value_part = value_all;
154
155#define copy_prop_value(to) \
156 length_part = strlen(value_part); \
157 if (value_part + length_part < (value_all + length_all) && \
158 length_part > 0) \
159 names.to = value_part; \
160 value_part += length_part + 1;
161
162 copy_prop_value(rules);
163 copy_prop_value(model);
164 copy_prop_value(layout);
165 copy_prop_value(variant);
166 copy_prop_value(options);
167#undef copy_prop_value
168
Giulio Camuffo954f1832014-10-11 18:27:30 +0300169 ret = xkb_keymap_new_from_names(b->compositor->xkb_context, &names, 0);
Daniel Stone576cd152012-06-01 12:14:02 +0100170
171 free(reply);
172 return ret;
173}
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400174
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400175static uint32_t
Giulio Camuffo954f1832014-10-11 18:27:30 +0300176get_xkb_mod_mask(struct x11_backend *b, uint32_t in)
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400177{
Derek Foreman1281a362015-07-31 16:55:32 -0500178 struct weston_keyboard *keyboard =
179 weston_seat_get_keyboard(&b->core_seat);
180 struct weston_xkb_info *info = keyboard->xkb_info;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400181 uint32_t ret = 0;
182
183 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
184 ret |= (1 << info->shift_mod);
185 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
186 ret |= (1 << info->caps_mod);
187 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
188 ret |= (1 << info->ctrl_mod);
189 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
190 ret |= (1 << info->alt_mod);
191 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
192 ret |= (1 << info->mod2_mod);
193 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
194 ret |= (1 << info->mod3_mod);
195 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
196 ret |= (1 << info->super_mod);
197 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
198 ret |= (1 << info->mod5_mod);
199
200 return ret;
201}
202
Daniel Stone62b33b62012-06-22 13:21:35 +0100203static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300204x11_backend_setup_xkb(struct x11_backend *b)
Daniel Stone62b33b62012-06-22 13:21:35 +0100205{
206#ifndef HAVE_XCB_XKB
207 weston_log("XCB-XKB not available during build\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300208 b->has_xkb = 0;
209 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100210 return;
211#else
Derek Foreman1281a362015-07-31 16:55:32 -0500212 struct weston_keyboard *keyboard;
Daniel Stone62b33b62012-06-22 13:21:35 +0100213 const xcb_query_extension_reply_t *ext;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100214 xcb_generic_error_t *error;
Daniel Stonee2faa122012-06-22 13:21:39 +0100215 xcb_void_cookie_t select;
Ran Benita6a39d872012-10-31 20:14:57 +0200216 xcb_xkb_use_extension_cookie_t use_ext;
217 xcb_xkb_use_extension_reply_t *use_ext_reply;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100218 xcb_xkb_per_client_flags_cookie_t pcf;
219 xcb_xkb_per_client_flags_reply_t *pcf_reply;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400220 xcb_xkb_get_state_cookie_t state;
221 xcb_xkb_get_state_reply_t *state_reply;
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200222 uint32_t values[1] = { XCB_EVENT_MASK_PROPERTY_CHANGE };
Daniel Stone62b33b62012-06-22 13:21:35 +0100223
Giulio Camuffo954f1832014-10-11 18:27:30 +0300224 b->has_xkb = 0;
225 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100226
Giulio Camuffo954f1832014-10-11 18:27:30 +0300227 ext = xcb_get_extension_data(b->conn, &xcb_xkb_id);
Daniel Stone62b33b62012-06-22 13:21:35 +0100228 if (!ext) {
229 weston_log("XKB extension not available on host X11 server\n");
230 return;
231 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300232 b->xkb_event_base = ext->first_event;
Daniel Stone62b33b62012-06-22 13:21:35 +0100233
Giulio Camuffo954f1832014-10-11 18:27:30 +0300234 select = xcb_xkb_select_events_checked(b->conn,
Ran Benita424ae012012-10-31 00:13:08 +0200235 XCB_XKB_ID_USE_CORE_KBD,
236 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
237 0,
238 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
239 0,
240 0,
241 NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300242 error = xcb_request_check(b->conn, select);
Daniel Stonee2faa122012-06-22 13:21:39 +0100243 if (error) {
244 weston_log("error: failed to select for XKB state events\n");
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200245 free(error);
Daniel Stonee2faa122012-06-22 13:21:39 +0100246 return;
247 }
248
Giulio Camuffo954f1832014-10-11 18:27:30 +0300249 use_ext = xcb_xkb_use_extension(b->conn,
Ran Benita6a39d872012-10-31 20:14:57 +0200250 XCB_XKB_MAJOR_VERSION,
251 XCB_XKB_MINOR_VERSION);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300252 use_ext_reply = xcb_xkb_use_extension_reply(b->conn, use_ext, NULL);
Ran Benita6a39d872012-10-31 20:14:57 +0200253 if (!use_ext_reply) {
254 weston_log("couldn't start using XKB extension\n");
255 return;
256 }
257
258 if (!use_ext_reply->supported) {
259 weston_log("XKB extension version on the server is too old "
260 "(want %d.%d, has %d.%d)\n",
261 XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION,
262 use_ext_reply->serverMajor, use_ext_reply->serverMinor);
263 free(use_ext_reply);
264 return;
265 }
266 free(use_ext_reply);
267
Giulio Camuffo954f1832014-10-11 18:27:30 +0300268 pcf = xcb_xkb_per_client_flags(b->conn,
Daniel Stone3ee91e12012-06-22 13:21:36 +0100269 XCB_XKB_ID_USE_CORE_KBD,
270 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
271 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
272 0,
273 0,
274 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300275 pcf_reply = xcb_xkb_per_client_flags_reply(b->conn, pcf, NULL);
Ran Benita7109cc82012-10-31 20:14:58 +0200276 if (!pcf_reply ||
277 !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT)) {
Daniel Stone3ee91e12012-06-22 13:21:36 +0100278 weston_log("failed to set XKB per-client flags, not using "
279 "detectable repeat\n");
Ran Benita7109cc82012-10-31 20:14:58 +0200280 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100281 return;
282 }
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200283 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100284
Giulio Camuffo954f1832014-10-11 18:27:30 +0300285 state = xcb_xkb_get_state(b->conn, XCB_XKB_ID_USE_CORE_KBD);
286 state_reply = xcb_xkb_get_state_reply(b->conn, state, NULL);
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200287 if (!state_reply) {
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400288 weston_log("failed to get initial XKB state\n");
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400289 return;
290 }
291
Derek Foreman1281a362015-07-31 16:55:32 -0500292 keyboard = weston_seat_get_keyboard(&b->core_seat);
293 xkb_state_update_mask(keyboard->xkb_state.state,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300294 get_xkb_mod_mask(b, state_reply->baseMods),
295 get_xkb_mod_mask(b, state_reply->latchedMods),
296 get_xkb_mod_mask(b, state_reply->lockedMods),
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400297 0,
298 0,
299 state_reply->group);
300
301 free(state_reply);
302
Giulio Camuffo954f1832014-10-11 18:27:30 +0300303 xcb_change_window_attributes(b->conn, b->screen->root,
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200304 XCB_CW_EVENT_MASK, values);
305
Giulio Camuffo954f1832014-10-11 18:27:30 +0300306 b->has_xkb = 1;
Daniel Stone62b33b62012-06-22 13:21:35 +0100307#endif
308}
309
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200310#ifdef HAVE_XCB_XKB
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200311static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300312update_xkb_keymap(struct x11_backend *b)
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200313{
314 struct xkb_keymap *keymap;
315
Giulio Camuffo954f1832014-10-11 18:27:30 +0300316 keymap = x11_backend_get_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200317 if (!keymap) {
318 weston_log("failed to get XKB keymap\n");
319 return;
320 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300321 weston_seat_update_keymap(&b->core_seat, keymap);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200322 xkb_keymap_unref(keymap);
323}
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200324#endif
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200325
Darxus55973f22010-11-22 21:24:39 -0500326static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300327x11_input_create(struct x11_backend *b, int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400328{
Daniel Stone576cd152012-06-01 12:14:02 +0100329 struct xkb_keymap *keymap;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400330
Giulio Camuffo954f1832014-10-11 18:27:30 +0300331 weston_seat_init(&b->core_seat, b->compositor, "default");
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300332
333 if (no_input)
334 return 0;
335
Giulio Camuffo954f1832014-10-11 18:27:30 +0300336 weston_seat_init_pointer(&b->core_seat);
Daniel Stone576cd152012-06-01 12:14:02 +0100337
Giulio Camuffo954f1832014-10-11 18:27:30 +0300338 keymap = x11_backend_get_keymap(b);
339 if (weston_seat_init_keyboard(&b->core_seat, keymap) < 0)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500340 return -1;
Ran Benitac9c74152014-08-19 23:59:52 +0300341 xkb_keymap_unref(keymap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400342
Giulio Camuffo954f1832014-10-11 18:27:30 +0300343 x11_backend_setup_xkb(b);
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400344
Darxus55973f22010-11-22 21:24:39 -0500345 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400346}
347
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200348static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300349x11_input_destroy(struct x11_backend *b)
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200350{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300351 weston_seat_release(&b->core_seat);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200352}
353
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500354static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200355x11_output_start_repaint_loop(struct weston_output *output)
356{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400357 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200358
Pekka Paalanen662f3842015-03-18 12:17:26 +0200359 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200360 weston_output_finish_frame(output, &ts, PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200361}
362
David Herrmann1edf44c2013-10-22 17:11:26 +0200363static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300364x11_output_repaint_gl(struct weston_output *output_base,
365 pixman_region32_t *damage)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500366{
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500367 struct x11_output *output = (struct x11_output *)output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400368 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500369
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400370 ec->renderer->repaint_output(output_base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500371
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200372 pixman_region32_subtract(&ec->primary_plane.damage,
373 &ec->primary_plane.damage, damage);
374
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500375 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200376 return 0;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500377}
378
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300379static void
Alexander Larsson80f91632013-05-22 14:41:38 +0200380set_clip_for_output(struct weston_output *output_base, pixman_region32_t *region)
381{
382 struct x11_output *output = (struct x11_output *)output_base;
383 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300384 struct x11_backend *b = (struct x11_backend *)ec->backend;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500385 pixman_region32_t transformed_region;
Alexander Larsson80f91632013-05-22 14:41:38 +0200386 pixman_box32_t *rects;
387 xcb_rectangle_t *output_rects;
Alexander Larsson80f91632013-05-22 14:41:38 +0200388 xcb_void_cookie_t cookie;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500389 int nrects, i;
Alexander Larsson80f91632013-05-22 14:41:38 +0200390 xcb_generic_error_t *err;
391
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500392 pixman_region32_init(&transformed_region);
393 pixman_region32_copy(&transformed_region, region);
394 pixman_region32_translate(&transformed_region,
395 -output_base->x, -output_base->y);
396 weston_transformed_region(output_base->width, output_base->height,
397 output_base->transform,
398 output_base->current_scale,
399 &transformed_region, &transformed_region);
400
401 rects = pixman_region32_rectangles(&transformed_region, &nrects);
Alexander Larsson80f91632013-05-22 14:41:38 +0200402 output_rects = calloc(nrects, sizeof(xcb_rectangle_t));
403
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500404 if (output_rects == NULL) {
405 pixman_region32_fini(&transformed_region);
Alexander Larsson80f91632013-05-22 14:41:38 +0200406 return;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500407 }
Alexander Larsson80f91632013-05-22 14:41:38 +0200408
409 for (i = 0; i < nrects; i++) {
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500410 output_rects[i].x = rects[i].x1;
411 output_rects[i].y = rects[i].y1;
412 output_rects[i].width = rects[i].x2 - rects[i].x1;
413 output_rects[i].height = rects[i].y2 - rects[i].y1;
Alexander Larsson80f91632013-05-22 14:41:38 +0200414 }
415
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500416 pixman_region32_fini(&transformed_region);
417
Giulio Camuffo954f1832014-10-11 18:27:30 +0300418 cookie = xcb_set_clip_rectangles_checked(b->conn, XCB_CLIP_ORDERING_UNSORTED,
Alexander Larsson80f91632013-05-22 14:41:38 +0200419 output->gc,
420 0, 0, nrects,
421 output_rects);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300422 err = xcb_request_check(b->conn, cookie);
Alexander Larsson80f91632013-05-22 14:41:38 +0200423 if (err != NULL) {
424 weston_log("Failed to set clip rects, err: %d\n", err->error_code);
425 free(err);
426 }
427 free(output_rects);
428}
429
430
David Herrmann1edf44c2013-10-22 17:11:26 +0200431static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300432x11_output_repaint_shm(struct weston_output *output_base,
433 pixman_region32_t *damage)
434{
435 struct x11_output *output = (struct x11_output *)output_base;
436 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300437 struct x11_backend *b = (struct x11_backend *)ec->backend;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300438 xcb_void_cookie_t cookie;
439 xcb_generic_error_t *err;
440
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200441 pixman_renderer_output_set_buffer(output_base, output->hw_surface);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300442 ec->renderer->repaint_output(output_base, damage);
443
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300444 pixman_region32_subtract(&ec->primary_plane.damage,
445 &ec->primary_plane.damage, damage);
Alexander Larsson80f91632013-05-22 14:41:38 +0200446 set_clip_for_output(output_base, damage);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300447 cookie = xcb_shm_put_image_checked(b->conn, output->window, output->gc,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300448 pixman_image_get_width(output->hw_surface),
449 pixman_image_get_height(output->hw_surface),
450 0, 0,
451 pixman_image_get_width(output->hw_surface),
452 pixman_image_get_height(output->hw_surface),
453 0, 0, output->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
454 0, output->segment, 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300455 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300456 if (err != NULL) {
457 weston_log("Failed to put shm image, err: %d\n", err->error_code);
458 free(err);
459 }
460
461 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200462 return 0;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300463}
464
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100465static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400466finish_frame_handler(void *data)
467{
468 struct x11_output *output = data;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200469 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200470
Pekka Paalanen662f3842015-03-18 12:17:26 +0200471 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200472 weston_output_finish_frame(&output->base, &ts, 0);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400473
474 return 1;
475}
476
Matt Roper361d2ad2011-08-29 13:52:23 -0700477static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300478x11_output_deinit_shm(struct x11_backend *b, struct x11_output *output)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300479{
480 xcb_void_cookie_t cookie;
481 xcb_generic_error_t *err;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300482 xcb_free_gc(b->conn, output->gc);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300483
484 pixman_image_unref(output->hw_surface);
485 output->hw_surface = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300486 cookie = xcb_shm_detach_checked(b->conn, output->segment);
487 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300488 if (err) {
489 weston_log("xcb_shm_detach failed, error %d\n", err->error_code);
490 free(err);
491 }
492 shmdt(output->buf);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300493}
494
495static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500496x11_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700497{
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200498 struct x11_output *output = (struct x11_output *)output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300499 struct x11_backend *backend =
500 (struct x11_backend *)output->base.compositor->backend;
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200501
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200502 wl_event_source_remove(output->finish_frame_timer);
503
Giulio Camuffo954f1832014-10-11 18:27:30 +0300504 if (backend->use_pixman) {
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300505 pixman_renderer_output_destroy(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300506 x11_output_deinit_shm(backend, output);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300507 } else
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300508 gl_renderer->output_destroy(output_base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200509
Giulio Camuffo954f1832014-10-11 18:27:30 +0300510 xcb_destroy_window(backend->conn, output->window);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200511
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500512 weston_output_destroy(&output->base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200513
514 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700515}
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200516
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400517static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300518x11_output_set_wm_protocols(struct x11_backend *b,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500519 struct x11_output *output)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400520{
521 xcb_atom_t list[1];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400522
Giulio Camuffo954f1832014-10-11 18:27:30 +0300523 list[0] = b->atom.wm_delete_window;
524 xcb_change_property (b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400525 XCB_PROP_MODE_REPLACE,
526 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300527 b->atom.wm_protocols,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400528 XCB_ATOM_ATOM,
529 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500530 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400531 list);
532}
533
534struct wm_normal_hints {
535 uint32_t flags;
536 uint32_t pad[4];
537 int32_t min_width, min_height;
538 int32_t max_width, max_height;
539 int32_t width_inc, height_inc;
540 int32_t min_aspect_x, min_aspect_y;
541 int32_t max_aspect_x, max_aspect_y;
542 int32_t base_width, base_height;
543 int32_t win_gravity;
544};
545
546#define WM_NORMAL_HINTS_MIN_SIZE 16
547#define WM_NORMAL_HINTS_MAX_SIZE 32
548
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500549static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300550x11_output_set_icon(struct x11_backend *b,
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400551 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500552{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400553 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400554 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400555 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500556
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400557 image = load_image(filename);
558 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500559 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400560 width = pixman_image_get_width(image);
561 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500562 icon = malloc(width * height * 4 + 8);
563 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400564 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500565 return;
566 }
567
568 icon[0] = width;
569 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400570 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300571 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
572 b->atom.net_wm_icon, b->atom.cardinal, 32,
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500573 width * height + 2, icon);
574 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400575 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500576}
577
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500578static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300579x11_output_wait_for_map(struct x11_backend *b, struct x11_output *output)
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500580{
581 xcb_map_notify_event_t *map_notify;
582 xcb_configure_notify_event_t *configure_notify;
583 xcb_generic_event_t *event;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500584 int mapped = 0, configured = 0;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500585 uint8_t response_type;
586
587 /* This isn't the nicest way to do this. Ideally, we could
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500588 * just go back to the main loop and once we get the configure
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500589 * notify, we add the output to the compositor. While we do
590 * support output hotplug, we can't start up with no outputs.
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500591 * We could add the output and then resize once we get the
592 * configure notify, but we don't want to start up and
593 * immediately resize the output.
594 *
595 * Also, some window managers don't give us our final
596 * fullscreen size before map_notify, so if we don't get a
597 * configure_notify before map_notify, we just wait for the
598 * first one and hope that's our size. */
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500599
Giulio Camuffo954f1832014-10-11 18:27:30 +0300600 xcb_flush(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500601
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500602 while (!mapped || !configured) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300603 event = xcb_wait_for_event(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500604 response_type = event->response_type & ~0x80;
605
606 switch (response_type) {
607 case XCB_MAP_NOTIFY:
608 map_notify = (xcb_map_notify_event_t *) event;
609 if (map_notify->window == output->window)
610 mapped = 1;
611 break;
612
613 case XCB_CONFIGURE_NOTIFY:
614 configure_notify =
615 (xcb_configure_notify_event_t *) event;
616
Alexander Larsson4ea95522013-05-22 14:41:37 +0200617
618 if (configure_notify->width % output->scale != 0 ||
619 configure_notify->height % output->scale != 0)
620 weston_log("Resolution is not a multiple of screen size, rounding\n");
Jason Ekstrandd89a0942014-03-07 15:29:14 -0600621 output->mode.width = configure_notify->width;
622 output->mode.height = configure_notify->height;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500623 configured = 1;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500624 break;
625 }
626 }
627}
628
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300629static xcb_visualtype_t *
630find_visual_by_id(xcb_screen_t *screen,
631 xcb_visualid_t id)
632{
633 xcb_depth_iterator_t i;
634 xcb_visualtype_iterator_t j;
635 for (i = xcb_screen_allowed_depths_iterator(screen);
636 i.rem;
637 xcb_depth_next(&i)) {
638 for (j = xcb_depth_visuals_iterator(i.data);
639 j.rem;
640 xcb_visualtype_next(&j)) {
641 if (j.data->visual_id == id)
642 return j.data;
643 }
644 }
645 return 0;
646}
647
648static uint8_t
649get_depth_of_visual(xcb_screen_t *screen,
650 xcb_visualid_t id)
651{
652 xcb_depth_iterator_t i;
653 xcb_visualtype_iterator_t j;
654 for (i = xcb_screen_allowed_depths_iterator(screen);
655 i.rem;
656 xcb_depth_next(&i)) {
657 for (j = xcb_depth_visuals_iterator(i.data);
658 j.rem;
659 xcb_visualtype_next(&j)) {
660 if (j.data->visual_id == id)
661 return i.data->depth;
662 }
663 }
664 return 0;
665}
666
667static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300668x11_output_init_shm(struct x11_backend *b, struct x11_output *output,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300669 int width, int height)
670{
671 xcb_screen_iterator_t iter;
672 xcb_visualtype_t *visual_type;
673 xcb_format_iterator_t fmt;
674 xcb_void_cookie_t cookie;
675 xcb_generic_error_t *err;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300676 const xcb_query_extension_reply_t *ext;
677 int bitsperpixel = 0;
678 pixman_format_code_t pixman_format;
679
680 /* Check if SHM is available */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300681 ext = xcb_get_extension_data(b->conn, &xcb_shm_id);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300682 if (ext == NULL || !ext->present) {
683 /* SHM is missing */
684 weston_log("SHM extension is not available\n");
685 errno = ENOENT;
686 return -1;
687 }
688
Giulio Camuffo954f1832014-10-11 18:27:30 +0300689 iter = xcb_setup_roots_iterator(xcb_get_setup(b->conn));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300690 visual_type = find_visual_by_id(iter.data, iter.data->root_visual);
691 if (!visual_type) {
692 weston_log("Failed to lookup visual for root window\n");
693 errno = ENOENT;
694 return -1;
695 }
696 weston_log("Found visual, bits per value: %d, red_mask: %.8x, green_mask: %.8x, blue_mask: %.8x\n",
697 visual_type->bits_per_rgb_value,
698 visual_type->red_mask,
699 visual_type->green_mask,
700 visual_type->blue_mask);
701 output->depth = get_depth_of_visual(iter.data, iter.data->root_visual);
702 weston_log("Visual depth is %d\n", output->depth);
703
Giulio Camuffo954f1832014-10-11 18:27:30 +0300704 for (fmt = xcb_setup_pixmap_formats_iterator(xcb_get_setup(b->conn));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300705 fmt.rem;
706 xcb_format_next(&fmt)) {
707 if (fmt.data->depth == output->depth) {
708 bitsperpixel = fmt.data->bits_per_pixel;
709 break;
710 }
711 }
712 weston_log("Found format for depth %d, bpp: %d\n",
713 output->depth, bitsperpixel);
714
715 if (bitsperpixel == 32 &&
716 visual_type->red_mask == 0xff0000 &&
717 visual_type->green_mask == 0x00ff00 &&
718 visual_type->blue_mask == 0x0000ff) {
719 weston_log("Will use x8r8g8b8 format for SHM surfaces\n");
720 pixman_format = PIXMAN_x8r8g8b8;
MoD1b55a592013-11-28 05:24:21 +0000721 } else if (bitsperpixel == 16 &&
722 visual_type->red_mask == 0x00f800 &&
723 visual_type->green_mask == 0x0007e0 &&
724 visual_type->blue_mask == 0x00001f) {
725 weston_log("Will use r5g6b5 format for SHM surfaces\n");
726 pixman_format = PIXMAN_r5g6b5;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300727 } else {
728 weston_log("Can't find appropriate format for SHM pixmap\n");
729 errno = ENOTSUP;
730 return -1;
731 }
732
733
734 /* Create SHM segment and attach it */
735 output->shm_id = shmget(IPC_PRIVATE, width * height * (bitsperpixel / 8), IPC_CREAT | S_IRWXU);
736 if (output->shm_id == -1) {
737 weston_log("x11shm: failed to allocate SHM segment\n");
738 return -1;
739 }
740 output->buf = shmat(output->shm_id, NULL, 0 /* read/write */);
741 if (-1 == (long)output->buf) {
742 weston_log("x11shm: failed to attach SHM segment\n");
743 return -1;
744 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300745 output->segment = xcb_generate_id(b->conn);
746 cookie = xcb_shm_attach_checked(b->conn, output->segment, output->shm_id, 1);
747 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300748 if (err) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700749 weston_log("x11shm: xcb_shm_attach error %d, op code %d, resource id %d\n",
750 err->error_code, err->major_code, err->minor_code);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300751 free(err);
752 return -1;
753 }
754
755 shmctl(output->shm_id, IPC_RMID, NULL);
756
757 /* Now create pixman image */
758 output->hw_surface = pixman_image_create_bits(pixman_format, width, height, output->buf,
759 width * (bitsperpixel / 8));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300760
Giulio Camuffo954f1832014-10-11 18:27:30 +0300761 output->gc = xcb_generate_id(b->conn);
762 xcb_create_gc(b->conn, output->gc, output->window, 0, NULL);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300763
764 return 0;
765}
766
Scott Moreau1bad5db2012-08-18 01:04:05 -0600767static struct x11_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300768x11_backend_create_output(struct x11_backend *b, int x, int y,
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300769 int width, int height, int fullscreen,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600770 int no_input, char *configured_name,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200771 uint32_t transform, int32_t scale)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400772{
Scott Moreau1bad5db2012-08-18 01:04:05 -0600773 static const char name[] = "Weston Compositor";
Kristian Høgsberg86000402012-01-03 23:24:14 -0500774 static const char class[] = "weston-1\0Weston Compositor";
Scott Moreau1bad5db2012-08-18 01:04:05 -0600775 char title[32];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400776 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400777 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400778 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400779 struct wl_event_loop *loop;
Kristian Høgsberg8404acb2014-03-18 12:15:42 -0700780 int output_width, output_height, width_mm, height_mm;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300781 int ret;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400782 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500783 xcb_atom_t atom_list[1];
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300784 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400785 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300786 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400787 0
788 };
789
Alexander Larsson4ea95522013-05-22 14:41:37 +0200790 output_width = width * scale;
791 output_height = height * scale;
792
Scott Moreau1bad5db2012-08-18 01:04:05 -0600793 if (configured_name)
794 sprintf(title, "%s - %s", name, configured_name);
795 else
796 strcpy(title, name);
797
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300798 if (!no_input)
799 values[0] |=
800 XCB_EVENT_MASK_KEY_PRESS |
801 XCB_EVENT_MASK_KEY_RELEASE |
802 XCB_EVENT_MASK_BUTTON_PRESS |
803 XCB_EVENT_MASK_BUTTON_RELEASE |
804 XCB_EVENT_MASK_POINTER_MOTION |
805 XCB_EVENT_MASK_ENTER_WINDOW |
806 XCB_EVENT_MASK_LEAVE_WINDOW |
807 XCB_EVENT_MASK_KEYMAP_STATE |
808 XCB_EVENT_MASK_FOCUS_CHANGE;
809
Peter Huttererf3d62272013-08-08 11:57:05 +1000810 output = zalloc(sizeof *output);
Bryce Harrington665b0252015-05-26 18:14:21 -0700811 if (output == NULL) {
812 perror("zalloc");
Scott Moreau1bad5db2012-08-18 01:04:05 -0600813 return NULL;
Bryce Harrington665b0252015-05-26 18:14:21 -0700814 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400815
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400816 output->mode.flags =
817 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200818
Alexander Larsson0b135062013-05-28 16:23:36 +0200819 output->mode.width = output_width;
820 output->mode.height = output_height;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400821 output->mode.refresh = 60000;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200822 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400823 wl_list_init(&output->base.mode_list);
824 wl_list_insert(&output->base.mode_list, &output->mode.link);
825
Giulio Camuffo954f1832014-10-11 18:27:30 +0300826 values[1] = b->null_cursor;
827 output->window = xcb_generate_id(b->conn);
828 iter = xcb_setup_roots_iterator(xcb_get_setup(b->conn));
829 xcb_create_window(b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400830 XCB_COPY_FROM_PARENT,
831 output->window,
832 iter.data->root,
833 0, 0,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200834 output_width, output_height,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400835 0,
836 XCB_WINDOW_CLASS_INPUT_OUTPUT,
837 iter.data->root_visual,
838 mask, values);
839
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500840 if (fullscreen) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300841 atom_list[0] = b->atom.net_wm_state_fullscreen;
842 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500843 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300844 b->atom.net_wm_state,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500845 XCB_ATOM_ATOM, 32,
846 ARRAY_LENGTH(atom_list), atom_list);
847 } else {
848 /* Don't resize me. */
849 memset(&normal_hints, 0, sizeof normal_hints);
850 normal_hints.flags =
851 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200852 normal_hints.min_width = output_width;
853 normal_hints.min_height = output_height;
854 normal_hints.max_width = output_width;
855 normal_hints.max_height = output_height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300856 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
857 b->atom.wm_normal_hints,
858 b->atom.wm_size_hints, 32,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500859 sizeof normal_hints / 4,
860 (uint8_t *) &normal_hints);
861 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400862
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400863 /* Set window name. Don't bother with non-EWMH WMs. */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300864 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
865 b->atom.net_wm_name, b->atom.utf8_string, 8,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600866 strlen(title), title);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300867 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
868 b->atom.wm_class, b->atom.string, 8,
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500869 sizeof class, class);
870
Giulio Camuffo954f1832014-10-11 18:27:30 +0300871 x11_output_set_icon(b, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500872
Giulio Camuffo954f1832014-10-11 18:27:30 +0300873 x11_output_set_wm_protocols(b, output);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500874
Giulio Camuffo954f1832014-10-11 18:27:30 +0300875 xcb_map_window(b->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400876
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -0500877 if (fullscreen)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300878 x11_output_wait_for_map(b, output);
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400879
Jonas Ådahle5a12252013-04-05 23:07:11 +0200880 output->base.start_repaint_loop = x11_output_start_repaint_loop;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300881 if (b->use_pixman)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300882 output->base.repaint = x11_output_repaint_shm;
883 else
884 output->base.repaint = x11_output_repaint_gl;
Matt Roper361d2ad2011-08-29 13:52:23 -0700885 output->base.destroy = x11_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800886 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200887 output->base.set_backlight = NULL;
888 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800889 output->base.switch_mode = NULL;
Hardeningff39efa2013-09-18 23:56:35 +0200890 output->base.current_mode = &output->mode;
Jason Ekstrand22e16b62014-09-15 12:10:27 -0700891 output->base.make = "weston-X11";
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500892 output->base.model = "none";
Ander Conselvan de Oliveirad4ea71e2013-12-18 19:51:40 +0200893
894 if (configured_name)
895 output->base.name = strdup(configured_name);
896
Giulio Camuffo954f1832014-10-11 18:27:30 +0300897 width_mm = width * b->screen->width_in_millimeters /
898 b->screen->width_in_pixels;
899 height_mm = height * b->screen->height_in_millimeters /
900 b->screen->height_in_pixels;
901 weston_output_init(&output->base, b->compositor,
Kristian Høgsberg8404acb2014-03-18 12:15:42 -0700902 x, y, width_mm, height_mm, transform, scale);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100903
Giulio Camuffo954f1832014-10-11 18:27:30 +0300904 if (b->use_pixman) {
905 if (x11_output_init_shm(b, output,
Kristian Høgsbergb5a4f982013-10-23 23:38:50 -0700906 output->mode.width,
Bryce Harrington665b0252015-05-26 18:14:21 -0700907 output->mode.height) < 0) {
908 weston_log("Failed to initialize SHM for the X11 output\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300909 return NULL;
Bryce Harrington665b0252015-05-26 18:14:21 -0700910 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300911 if (pixman_renderer_output_create(&output->base) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700912 weston_log("Failed to create pixman renderer for output\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300913 x11_output_deinit_shm(b, output);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300914 return NULL;
915 }
916 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +0100917 /* eglCreatePlatformWindowSurfaceEXT takes a Window*
918 * but eglCreateWindowSurface takes a Window. */
919 Window xid = (Window) output->window;
920
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300921 ret = gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000922 (EGLNativeWindowType) output->window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100923 &xid,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000924 gl_renderer->opaque_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500925 NULL,
926 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300927 if (ret < 0)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300928 return NULL;
929 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100930
Giulio Camuffo954f1832014-10-11 18:27:30 +0300931 loop = wl_display_get_event_loop(b->compositor->wl_display);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100932 output->finish_frame_timer =
933 wl_event_loop_add_timer(loop, finish_frame_handler, output);
934
Giulio Camuffo954f1832014-10-11 18:27:30 +0300935 weston_compositor_add_output(b->compositor, &output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400936
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400937 weston_log("x11 output %dx%d, window id %d\n",
938 width, height, output->window);
939
Scott Moreau1bad5db2012-08-18 01:04:05 -0600940 return output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400941}
942
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400943static struct x11_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300944x11_backend_find_output(struct x11_backend *b, xcb_window_t window)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400945{
946 struct x11_output *output;
947
Giulio Camuffo954f1832014-10-11 18:27:30 +0300948 wl_list_for_each(output, &b->compositor->output_list, base.link) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400949 if (output->window == window)
950 return output;
951 }
952
Derek Foreman99bfa642014-12-11 15:44:46 -0600953 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400954}
955
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200956static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300957x11_backend_delete_window(struct x11_backend *b, xcb_window_t window)
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200958{
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +0200959 struct x11_output *output;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200960
Giulio Camuffo954f1832014-10-11 18:27:30 +0300961 output = x11_backend_find_output(b, window);
Derek Foreman99bfa642014-12-11 15:44:46 -0600962 if (output)
963 x11_output_destroy(&output->base);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200964
Giulio Camuffo954f1832014-10-11 18:27:30 +0300965 xcb_flush(b->conn);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200966
Giulio Camuffo954f1832014-10-11 18:27:30 +0300967 if (wl_list_empty(&b->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +0300968 weston_compositor_exit(b->compositor);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200969}
970
Derek Foremand540f4b2015-01-27 16:26:49 -0600971static void delete_cb(void *data)
972{
973 struct window_delete_data *wd = data;
974
Giulio Camuffo954f1832014-10-11 18:27:30 +0300975 x11_backend_delete_window(wd->backend, wd->window);
Derek Foremand540f4b2015-01-27 16:26:49 -0600976 free(wd);
977}
978
Daniel Stone154c34c2012-06-22 13:21:40 +0100979#ifdef HAVE_XCB_XKB
Daniel Stonee2faa122012-06-22 13:21:39 +0100980static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300981update_xkb_state(struct x11_backend *b, xcb_xkb_state_notify_event_t *state)
Daniel Stonee2faa122012-06-22 13:21:39 +0100982{
Derek Foreman1281a362015-07-31 16:55:32 -0500983 struct weston_keyboard *keyboard =
984 weston_seat_get_keyboard(&b->core_seat);
985
986 xkb_state_update_mask(keyboard->xkb_state.state,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300987 get_xkb_mod_mask(b, state->baseMods),
988 get_xkb_mod_mask(b, state->latchedMods),
989 get_xkb_mod_mask(b, state->lockedMods),
Daniel Stonee2faa122012-06-22 13:21:39 +0100990 0,
991 0,
992 state->group);
993
Giulio Camuffo954f1832014-10-11 18:27:30 +0300994 notify_modifiers(&b->core_seat,
995 wl_display_next_serial(b->compositor->wl_display));
Daniel Stonee2faa122012-06-22 13:21:39 +0100996}
997#endif
998
Daniel Stone154c34c2012-06-22 13:21:40 +0100999/**
1000 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
1001 * the best we can do (given that XCB also lacks XI2 support), is to take
1002 * the state from the core key events. Unfortunately that only gives us
1003 * the effective (i.e. union of depressed/latched/locked) state, and we
1004 * need the granularity.
1005 *
1006 * So we still update the state with every key event we see, but also use
1007 * the state field from X11 events as a mask so we don't get any stuck
1008 * modifiers.
1009 */
1010static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001011update_xkb_state_from_core(struct x11_backend *b, uint16_t x11_mask)
Daniel Stone154c34c2012-06-22 13:21:40 +01001012{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001013 uint32_t mask = get_xkb_mod_mask(b, x11_mask);
Derek Foreman1281a362015-07-31 16:55:32 -05001014 struct weston_keyboard *keyboard
1015 = weston_seat_get_keyboard(&b->core_seat);
Daniel Stone154c34c2012-06-22 13:21:40 +01001016
Derek Foreman1281a362015-07-31 16:55:32 -05001017 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone154c34c2012-06-22 13:21:40 +01001018 keyboard->modifiers.mods_depressed & mask,
1019 keyboard->modifiers.mods_latched & mask,
1020 keyboard->modifiers.mods_locked & mask,
1021 0,
1022 0,
1023 (x11_mask >> 13) & 3);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001024 notify_modifiers(&b->core_seat,
1025 wl_display_next_serial(b->compositor->wl_display));
Daniel Stone154c34c2012-06-22 13:21:40 +01001026}
1027
Tiago Vignattia3a71622011-12-08 17:03:17 +02001028static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001029x11_backend_deliver_button_event(struct x11_backend *b,
1030 xcb_generic_event_t *event, int state)
Tiago Vignattia3a71622011-12-08 17:03:17 +02001031{
1032 xcb_button_press_event_t *button_event =
1033 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +01001034 uint32_t button;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001035 struct x11_output *output;
1036
Giulio Camuffo954f1832014-10-11 18:27:30 +03001037 output = x11_backend_find_output(b, button_event->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001038 if (!output)
1039 return;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001040
1041 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001042 xcb_grab_pointer(b->conn, 0, output->window,
Kristian Høgsberg73308622012-10-30 11:04:52 -04001043 XCB_EVENT_MASK_BUTTON_PRESS |
1044 XCB_EVENT_MASK_BUTTON_RELEASE |
1045 XCB_EVENT_MASK_POINTER_MOTION |
1046 XCB_EVENT_MASK_ENTER_WINDOW |
1047 XCB_EVENT_MASK_LEAVE_WINDOW,
1048 XCB_GRAB_MODE_ASYNC,
1049 XCB_GRAB_MODE_ASYNC,
1050 output->window, XCB_CURSOR_NONE,
1051 button_event->time);
1052 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001053 xcb_ungrab_pointer(b->conn, button_event->time);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001054
Giulio Camuffo954f1832014-10-11 18:27:30 +03001055 if (!b->has_xkb)
1056 update_xkb_state_from_core(b, button_event->state);
Daniel Stone154c34c2012-06-22 13:21:40 +01001057
Tiago Vignattia3a71622011-12-08 17:03:17 +02001058 switch (button_event->detail) {
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001059 case 1:
1060 button = BTN_LEFT;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001061 break;
1062 case 2:
1063 button = BTN_MIDDLE;
1064 break;
1065 case 3:
1066 button = BTN_RIGHT;
1067 break;
1068 case 4:
Jonas Ådahl62efe202012-09-27 18:40:41 +02001069 /* Axis are measured in pixels, but the xcb events are discrete
1070 * steps. Therefore move the axis by some pixels every step. */
Scott Moreau210d0792012-03-22 10:47:01 -06001071 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001072 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001073 weston_compositor_get_time(),
1074 WL_POINTER_AXIS_VERTICAL_SCROLL,
1075 -DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -06001076 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001077 case 5:
Scott Moreau210d0792012-03-22 10:47:01 -06001078 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001079 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001080 weston_compositor_get_time(),
1081 WL_POINTER_AXIS_VERTICAL_SCROLL,
1082 DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -06001083 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001084 case 6:
Scott Moreau210d0792012-03-22 10:47:01 -06001085 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001086 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001087 weston_compositor_get_time(),
1088 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
1089 -DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -06001090 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001091 case 7:
Scott Moreau210d0792012-03-22 10:47:01 -06001092 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001093 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001094 weston_compositor_get_time(),
1095 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
1096 DEFAULT_AXIS_STEP_DISTANCE);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001097 return;
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001098 default:
1099 button = button_event->detail + BTN_SIDE - 8;
1100 break;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001101 }
1102
Giulio Camuffo954f1832014-10-11 18:27:30 +03001103 notify_button(&b->core_seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001104 weston_compositor_get_time(), button,
1105 state ? WL_POINTER_BUTTON_STATE_PRESSED :
1106 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001107}
1108
Scott Moreau1bad5db2012-08-18 01:04:05 -06001109static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001110x11_backend_deliver_motion_event(struct x11_backend *b,
1111 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001112{
1113 struct x11_output *output;
1114 wl_fixed_t x, y;
1115 xcb_motion_notify_event_t *motion_notify =
1116 (xcb_motion_notify_event_t *) event;
1117
Giulio Camuffo954f1832014-10-11 18:27:30 +03001118 if (!b->has_xkb)
1119 update_xkb_state_from_core(b, motion_notify->state);
1120 output = x11_backend_find_output(b, motion_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001121 if (!output)
1122 return;
1123
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001124 weston_output_transform_coordinate(&output->base,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05001125 wl_fixed_from_int(motion_notify->event_x),
1126 wl_fixed_from_int(motion_notify->event_y),
1127 &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001128
Giulio Camuffo954f1832014-10-11 18:27:30 +03001129 notify_motion(&b->core_seat, weston_compositor_get_time(),
1130 x - b->prev_x, y - b->prev_y);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001131
Giulio Camuffo954f1832014-10-11 18:27:30 +03001132 b->prev_x = x;
1133 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001134}
1135
1136static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001137x11_backend_deliver_enter_event(struct x11_backend *b,
1138 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001139{
1140 struct x11_output *output;
1141 wl_fixed_t x, y;
1142
1143 xcb_enter_notify_event_t *enter_notify =
1144 (xcb_enter_notify_event_t *) event;
1145 if (enter_notify->state >= Button1Mask)
1146 return;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001147 if (!b->has_xkb)
1148 update_xkb_state_from_core(b, enter_notify->state);
1149 output = x11_backend_find_output(b, enter_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001150 if (!output)
1151 return;
1152
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001153 weston_output_transform_coordinate(&output->base,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05001154 wl_fixed_from_int(enter_notify->event_x),
1155 wl_fixed_from_int(enter_notify->event_y), &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001156
Giulio Camuffo954f1832014-10-11 18:27:30 +03001157 notify_pointer_focus(&b->core_seat, &output->base, x, y);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001158
Giulio Camuffo954f1832014-10-11 18:27:30 +03001159 b->prev_x = x;
1160 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001161}
1162
Kristian Høgsbergee724822011-04-21 14:51:44 -04001163static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001164x11_backend_next_event(struct x11_backend *b,
1165 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -04001166{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001167 if (mask & WL_EVENT_READABLE) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001168 *event = xcb_poll_for_event(b->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -04001169 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001170#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Giulio Camuffo954f1832014-10-11 18:27:30 +03001171 *event = xcb_poll_for_queued_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001172#else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001173 *event = xcb_poll_for_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001174#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -04001175 }
1176
1177 return *event != NULL;
1178}
1179
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001180static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001181x11_backend_handle_event(int fd, uint32_t mask, void *data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001182{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001183 struct x11_backend *b = data;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001184 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001185 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001186 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001187 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001188 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001189 xcb_keymap_notify_event_t *keymap_notify;
1190 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001191 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001192 xcb_atom_t atom;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001193 xcb_window_t window;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001194 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001195 uint32_t i, set;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001196 uint8_t response_type;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001197 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001198
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001199 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001200 count = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001201 while (x11_backend_next_event(b, &event, mask)) {
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001202 response_type = event->response_type & ~0x80;
1203
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001204 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1205 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001206 /* Suppress key repeat events; this is only used if we
1207 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001208 key_release = (xcb_key_press_event_t *) prev;
1209 key_press = (xcb_key_press_event_t *) event;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001210 if (response_type == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -07001211 key_release->time == key_press->time &&
1212 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001213 /* Don't deliver the held key release
1214 * event or the new key press event. */
1215 free(event);
1216 free(prev);
1217 prev = NULL;
1218 continue;
1219 } else {
1220 /* Deliver the held key release now
1221 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001222 * event below. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001223 update_xkb_state_from_core(b, key_release->state);
1224 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001225 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001226 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001227 WL_KEYBOARD_KEY_STATE_RELEASED,
1228 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001229 free(prev);
1230 prev = NULL;
1231 break;
1232 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001233
1234 case XCB_FOCUS_IN:
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001235 assert(response_type == XCB_KEYMAP_NOTIFY);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001236 keymap_notify = (xcb_keymap_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001237 b->keys.size = 0;
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001238 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
1239 set = keymap_notify->keys[i >> 3] &
1240 (1 << (i & 7));
1241 if (set) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001242 k = wl_array_add(&b->keys, sizeof *k);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001243 *k = i;
1244 }
1245 }
1246
Daniel Stoned6da09e2012-06-22 13:21:29 +01001247 /* Unfortunately the state only comes with the enter
1248 * event, rather than with the focus event. I'm not
1249 * sure of the exact semantics around it and whether
1250 * we can ensure that we get both? */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001251 notify_keyboard_focus_in(&b->core_seat, &b->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001252 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001253
1254 free(prev);
1255 prev = NULL;
1256 break;
1257
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001258 default:
1259 /* No previous event held */
1260 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001261 }
1262
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001263 switch (response_type) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001264 case XCB_KEY_PRESS:
1265 key_press = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001266 if (!b->has_xkb)
1267 update_xkb_state_from_core(b, key_press->state);
1268 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001269 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001270 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001271 WL_KEYBOARD_KEY_STATE_PRESSED,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001272 b->has_xkb ? STATE_UPDATE_NONE :
Daniel Stonee2faa122012-06-22 13:21:39 +01001273 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001274 break;
1275 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001276 /* If we don't have XKB, we need to use the lame
1277 * autorepeat detection above. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001278 if (!b->has_xkb) {
Daniel Stone3ee91e12012-06-22 13:21:36 +01001279 prev = event;
1280 break;
1281 }
1282 key_release = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001283 notify_key(&b->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +01001284 weston_compositor_get_time(),
1285 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001286 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +01001287 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001288 break;
1289 case XCB_BUTTON_PRESS:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001290 x11_backend_deliver_button_event(b, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001291 break;
1292 case XCB_BUTTON_RELEASE:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001293 x11_backend_deliver_button_event(b, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001294 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001295 case XCB_MOTION_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001296 x11_backend_deliver_motion_event(b, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001297 break;
1298
1299 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001300 expose = (xcb_expose_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001301 output = x11_backend_find_output(b, expose->window);
Derek Foreman99bfa642014-12-11 15:44:46 -06001302 if (!output)
1303 break;
1304
Kristian Høgsberga98c2e12013-12-05 12:30:37 -08001305 weston_output_damage(&output->base);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001306 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001307 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001308
1309 case XCB_ENTER_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001310 x11_backend_deliver_enter_event(b, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001311 break;
1312
1313 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001314 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -05001315 if (enter_notify->state >= Button1Mask)
1316 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001317 if (!b->has_xkb)
1318 update_xkb_state_from_core(b, enter_notify->state);
1319 notify_pointer_focus(&b->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001320 break;
1321
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001322 case XCB_CLIENT_MESSAGE:
1323 client_message = (xcb_client_message_event_t *) event;
1324 atom = client_message->data.data32[0];
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001325 window = client_message->window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001326 if (atom == b->atom.wm_delete_window) {
Derek Foremand540f4b2015-01-27 16:26:49 -06001327 struct wl_event_loop *loop;
1328 struct window_delete_data *data = malloc(sizeof *data);
1329
1330 /* if malloc failed we should at least try to
1331 * delete the window, even if it may result in
1332 * a crash.
1333 */
1334 if (!data) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001335 x11_backend_delete_window(b, window);
Derek Foremand540f4b2015-01-27 16:26:49 -06001336 break;
1337 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001338 data->backend = b;
Derek Foremand540f4b2015-01-27 16:26:49 -06001339 data->window = window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001340 loop = wl_display_get_event_loop(b->compositor->wl_display);
Derek Foremand540f4b2015-01-27 16:26:49 -06001341 wl_event_loop_add_idle(loop, delete_cb, data);
1342 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001343 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001344
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001345 case XCB_FOCUS_IN:
1346 focus_in = (xcb_focus_in_event_t *) event;
1347 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1348 break;
1349
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001350 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001351 break;
1352
1353 case XCB_FOCUS_OUT:
1354 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -05001355 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1356 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001357 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001358 notify_keyboard_focus_out(&b->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001359 break;
1360
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001361 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001362 break;
1363 }
1364
Daniel Stonee2faa122012-06-22 13:21:39 +01001365#ifdef HAVE_XCB_XKB
Giulio Camuffo954f1832014-10-11 18:27:30 +03001366 if (b->has_xkb) {
1367 if (response_type == b->xkb_event_base) {
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001368 xcb_xkb_state_notify_event_t *state =
1369 (xcb_xkb_state_notify_event_t *) event;
1370 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001371 update_xkb_state(b, state);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001372 } else if (response_type == XCB_PROPERTY_NOTIFY) {
1373 xcb_property_notify_event_t *prop_notify =
1374 (xcb_property_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001375 if (prop_notify->window == b->screen->root &&
1376 prop_notify->atom == b->atom.xkb_names &&
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001377 prop_notify->state == XCB_PROPERTY_NEW_VALUE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001378 update_xkb_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001379 }
Daniel Stonee2faa122012-06-22 13:21:39 +01001380 }
1381#endif
1382
Bill Spitzakb715cec2012-06-05 17:08:23 -04001383 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001384 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001385 free (event);
1386 }
1387
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001388 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1389 case XCB_KEY_RELEASE:
1390 key_release = (xcb_key_press_event_t *) prev;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001391 update_xkb_state_from_core(b, key_release->state);
1392 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001393 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001394 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001395 WL_KEYBOARD_KEY_STATE_RELEASED,
1396 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001397 free(prev);
1398 prev = NULL;
1399 break;
1400 default:
1401 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001402 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001403
Bill Spitzakb715cec2012-06-05 17:08:23 -04001404 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001405}
1406
Giulio Camuffo954f1832014-10-11 18:27:30 +03001407#define F(field) offsetof(struct x11_backend, field)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001408
1409static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001410x11_backend_get_resources(struct x11_backend *b)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001411{
1412 static const struct { const char *name; int offset; } atoms[] = {
1413 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1414 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1415 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1416 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001417 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001418 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001419 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001420 { "_NET_WM_STATE", F(atom.net_wm_state) },
1421 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001422 { "_NET_SUPPORTING_WM_CHECK",
1423 F(atom.net_supporting_wm_check) },
1424 { "_NET_SUPPORTED", F(atom.net_supported) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001425 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001426 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001427 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001428 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001429 };
1430
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001431 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001432 xcb_intern_atom_reply_t *reply;
1433 xcb_pixmap_t pixmap;
1434 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001435 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001436 uint8_t data[] = { 0, 0, 0, 0 };
1437
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001438 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001439 cookies[i] = xcb_intern_atom (b->conn, 0,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001440 strlen(atoms[i].name),
1441 atoms[i].name);
1442
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001443 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001444 reply = xcb_intern_atom_reply (b->conn, cookies[i], NULL);
1445 *(xcb_atom_t *) ((char *) b + atoms[i].offset) = reply->atom;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001446 free(reply);
1447 }
1448
Giulio Camuffo954f1832014-10-11 18:27:30 +03001449 pixmap = xcb_generate_id(b->conn);
1450 gc = xcb_generate_id(b->conn);
1451 xcb_create_pixmap(b->conn, 1, pixmap, b->screen->root, 1, 1);
1452 xcb_create_gc(b->conn, gc, pixmap, 0, NULL);
1453 xcb_put_image(b->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001454 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001455 b->null_cursor = xcb_generate_id(b->conn);
1456 xcb_create_cursor (b->conn, b->null_cursor,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001457 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001458 xcb_free_gc(b->conn, gc);
1459 xcb_free_pixmap(b->conn, pixmap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001460}
1461
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001462static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001463x11_backend_get_wm_info(struct x11_backend *c)
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001464{
1465 xcb_get_property_cookie_t cookie;
1466 xcb_get_property_reply_t *reply;
1467 xcb_atom_t *atom;
1468 unsigned int i;
1469
1470 cookie = xcb_get_property(c->conn, 0, c->screen->root,
1471 c->atom.net_supported,
1472 XCB_ATOM_ATOM, 0, 1024);
1473 reply = xcb_get_property_reply(c->conn, cookie, NULL);
1474 if (reply == NULL)
1475 return;
1476
1477 atom = (xcb_atom_t *) xcb_get_property_value(reply);
1478
1479 for (i = 0; i < reply->value_len; i++) {
1480 if (atom[i] == c->atom.net_wm_state_fullscreen)
1481 c->has_net_wm_state_fullscreen = 1;
1482 }
Ryo Munakata76fb7ec2015-03-08 19:17:06 +09001483
1484 free(reply);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001485}
1486
1487static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001488x11_restore(struct weston_compositor *ec)
1489{
1490}
1491
1492static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001493x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001494{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001495 struct x11_backend *backend = (struct x11_backend *)ec->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -07001496
Giulio Camuffo954f1832014-10-11 18:27:30 +03001497 wl_event_source_remove(backend->xcb_source);
1498 x11_input_destroy(backend);
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001499
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03001500 weston_compositor_shutdown(ec); /* destroys outputs, too */
1501
Giulio Camuffo954f1832014-10-11 18:27:30 +03001502 XCloseDisplay(backend->dpy);
1503 free(backend);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001504}
1505
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001506static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001507init_gl_renderer(struct x11_backend *b)
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001508{
1509 int ret;
1510
1511 gl_renderer = weston_load_module("gl-renderer.so",
1512 "gl_renderer_interface");
1513 if (!gl_renderer)
1514 return -1;
1515
Giulio Camuffo954f1832014-10-11 18:27:30 +03001516 ret = gl_renderer->create(b->compositor, EGL_PLATFORM_X11_KHR, (void *) b->dpy,
Derek Foremane76f1852015-05-15 12:12:39 -05001517 gl_renderer->opaque_attribs, NULL, 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001518
1519 return ret;
1520}
Giulio Camuffo954f1832014-10-11 18:27:30 +03001521static struct x11_backend *
1522x11_backend_create(struct weston_compositor *compositor,
1523 int fullscreen,
1524 int no_input,
1525 int use_pixman,
1526 int *argc, char *argv[],
1527 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001528{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001529 struct x11_backend *b;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001530 struct x11_output *output;
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001531 struct weston_config_section *section;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001532 xcb_screen_iterator_t s;
Scott Moreau248aaec2012-08-03 14:19:52 -06001533 int i, x = 0, output_count = 0;
Jason Ekstrandd89a0942014-03-07 15:29:14 -06001534 int width, height, scale, count;
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001535 const char *section_name;
1536 char *name, *t, *mode;
1537 uint32_t transform;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001538
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001539 weston_log("initializing x11 backend\n");
1540
Giulio Camuffo954f1832014-10-11 18:27:30 +03001541 b = zalloc(sizeof *b);
1542 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001543 return NULL;
1544
Giulio Camuffo954f1832014-10-11 18:27:30 +03001545 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001546 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001547 goto err_free;
1548
Giulio Camuffo954f1832014-10-11 18:27:30 +03001549 b->dpy = XOpenDisplay(NULL);
1550 if (b->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001551 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001552
Giulio Camuffo954f1832014-10-11 18:27:30 +03001553 b->conn = XGetXCBConnection(b->dpy);
1554 XSetEventQueueOwner(b->dpy, XCBOwnsEventQueue);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001555
Giulio Camuffo954f1832014-10-11 18:27:30 +03001556 if (xcb_connection_has_error(b->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001557 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001558
Giulio Camuffo954f1832014-10-11 18:27:30 +03001559 s = xcb_setup_roots_iterator(xcb_get_setup(b->conn));
1560 b->screen = s.data;
1561 wl_array_init(&b->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001562
Giulio Camuffo954f1832014-10-11 18:27:30 +03001563 x11_backend_get_resources(b);
1564 x11_backend_get_wm_info(b);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001565
Giulio Camuffo954f1832014-10-11 18:27:30 +03001566 if (!b->has_net_wm_state_fullscreen && fullscreen) {
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001567 weston_log("Can not fullscreen without window manager support"
1568 "(need _NET_WM_STATE_FULLSCREEN)\n");
1569 fullscreen = 0;
1570 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001571
Giulio Camuffo954f1832014-10-11 18:27:30 +03001572 b->use_pixman = use_pixman;
1573 if (b->use_pixman) {
1574 if (pixman_renderer_init(compositor) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001575 weston_log("Failed to initialize pixman renderer for X11 backend\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001576 goto err_xdisplay;
Bryce Harrington665b0252015-05-26 18:14:21 -07001577 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001578 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001579 else if (init_gl_renderer(b) < 0) {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001580 goto err_xdisplay;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001581 }
Vasily Khoruzhickd8943512013-01-07 22:36:02 +03001582 weston_log("Using %s renderer\n", use_pixman ? "pixman" : "gl");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001583
Giulio Camuffo954f1832014-10-11 18:27:30 +03001584 b->base.destroy = x11_destroy;
1585 b->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001586
Giulio Camuffo954f1832014-10-11 18:27:30 +03001587 if (x11_input_create(b, no_input) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001588 weston_log("Failed to create X11 input\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001589 goto err_renderer;
Bryce Harrington665b0252015-05-26 18:14:21 -07001590 }
Daniel Stone22815f92012-06-22 13:21:34 +01001591
Scott Moreau248aaec2012-08-03 14:19:52 -06001592 width = option_width ? option_width : 1024;
1593 height = option_height ? option_height : 640;
Jason Ekstrandd89a0942014-03-07 15:29:14 -06001594 scale = option_scale ? option_scale : 1;
Scott Moreau248aaec2012-08-03 14:19:52 -06001595 count = option_count ? option_count : 1;
1596
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001597 section = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001598 while (weston_config_next_section(config,
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001599 &section, &section_name)) {
1600 if (strcmp(section_name, "output") != 0)
1601 continue;
1602 weston_config_section_get_string(section, "name", &name, NULL);
Rob Bradford5c4245d2013-07-26 16:29:44 +01001603 if (name == NULL || name[0] != 'X') {
1604 free(name);
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001605 continue;
Rob Bradford5c4245d2013-07-26 16:29:44 +01001606 }
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001607
1608 weston_config_section_get_string(section,
1609 "mode", &mode, "1024x600");
1610 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1611 weston_log("Invalid mode \"%s\" for output %s\n",
1612 mode, name);
1613 width = 1024;
1614 height = 600;
1615 }
1616 free(mode);
1617
1618 if (option_width)
1619 width = option_width;
1620 if (option_height)
1621 height = option_height;
1622
1623 weston_config_section_get_int(section, "scale", &scale, 1);
Kristian Høgsberg3c2e66b2014-03-10 13:41:35 -07001624 if (option_scale)
1625 scale = option_scale;
1626
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001627 weston_config_section_get_string(section,
1628 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001629 if (weston_parse_transform(t, &transform) < 0)
1630 weston_log("Invalid transform \"%s\" for output %s\n",
1631 t, name);
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001632 free(t);
1633
Giulio Camuffo954f1832014-10-11 18:27:30 +03001634 output = x11_backend_create_output(b, x, 0,
1635 width, height,
1636 fullscreen, no_input,
1637 name, transform, scale);
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001638 free(name);
Bryce Harrington665b0252015-05-26 18:14:21 -07001639 if (output == NULL) {
1640 weston_log("Failed to create configured x11 output\n");
Scott Moreau248aaec2012-08-03 14:19:52 -06001641 goto err_x11_input;
Bryce Harrington665b0252015-05-26 18:14:21 -07001642 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06001643
1644 x = pixman_region32_extents(&output->base.region)->x2;
1645
Scott Moreau248aaec2012-08-03 14:19:52 -06001646 output_count++;
1647 if (option_count && output_count >= option_count)
1648 break;
1649 }
1650
1651 for (i = output_count; i < count; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001652 output = x11_backend_create_output(b, x, 0, width, height,
1653 fullscreen, no_input, NULL,
1654 WL_OUTPUT_TRANSFORM_NORMAL, scale);
Bryce Harrington665b0252015-05-26 18:14:21 -07001655 if (output == NULL) {
1656 weston_log("Failed to create x11 output #%d\n", i);
Martin Olsson11434bb2012-07-08 03:03:44 +02001657 goto err_x11_input;
Bryce Harrington665b0252015-05-26 18:14:21 -07001658 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06001659 x = pixman_region32_extents(&output->base.region)->x2;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001660 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001661
Giulio Camuffo954f1832014-10-11 18:27:30 +03001662 b->xcb_source =
1663 wl_event_loop_add_fd(compositor->input_loop,
1664 xcb_get_file_descriptor(b->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001665 WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001666 x11_backend_handle_event, b);
1667 wl_event_source_check(b->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001668
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001669 if (compositor->renderer->import_dmabuf) {
1670 if (linux_dmabuf_setup(compositor) < 0)
1671 weston_log("Error: initializing dmabuf "
1672 "support failed.\n");
1673 }
1674
Giulio Camuffo954f1832014-10-11 18:27:30 +03001675 compositor->backend = &b->base;
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001676
Giulio Camuffo954f1832014-10-11 18:27:30 +03001677 return b;
Martin Olsson11434bb2012-07-08 03:03:44 +02001678
1679err_x11_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001680 x11_input_destroy(b);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001681err_renderer:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001682 compositor->renderer->destroy(compositor);
Martin Olsson11434bb2012-07-08 03:03:44 +02001683err_xdisplay:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001684 XCloseDisplay(b->dpy);
Martin Olsson11434bb2012-07-08 03:03:44 +02001685err_free:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001686 free(b);
Martin Olsson11434bb2012-07-08 03:03:44 +02001687 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001688}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001689
Giulio Camuffo954f1832014-10-11 18:27:30 +03001690WL_EXPORT int
1691backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001692 struct weston_config *config)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001693{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001694 struct x11_backend *b;
Scott Moreau248aaec2012-08-03 14:19:52 -06001695 int fullscreen = 0;
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001696 int no_input = 0;
Vasily Khoruzhickd8943512013-01-07 22:36:02 +03001697 int use_pixman = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001698
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001699 const struct weston_option x11_options[] = {
Scott Moreau248aaec2012-08-03 14:19:52 -06001700 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1701 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
Jason Ekstrandd89a0942014-03-07 15:29:14 -06001702 { WESTON_OPTION_INTEGER, "scale", 0, &option_scale },
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -05001703 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &fullscreen },
Scott Moreau248aaec2012-08-03 14:19:52 -06001704 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001705 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
Vasily Khoruzhickd8943512013-01-07 22:36:02 +03001706 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001707 };
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001708
1709 parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001710
Giulio Camuffo954f1832014-10-11 18:27:30 +03001711 b = x11_backend_create(compositor,
1712 fullscreen,
1713 no_input,
1714 use_pixman,
1715 argc, argv, config);
1716 if (b == NULL)
1717 return -1;
1718 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001719}