blob: a3319b44b4a0373c292242a518a39c03598a07c4 [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"
Benoit Gschwinde16acab2016-04-15 20:28:31 -070053#include "compositor-x11.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070054#include "shared/config-parser.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070055#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070056#include "shared/image-loader.h"
Benoit Gschwinde16acab2016-04-15 20:28:31 -070057#include "gl-renderer.h"
58#include "pixman-renderer.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020059#include "presentation-time-server-protocol.h"
Pekka Paalanen5ffb4402014-06-12 16:52:53 +030060#include "linux-dmabuf.h"
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040061
Giulio Camuffo90a6fc62016-03-22 17:44:54 +020062#define DEFAULT_AXIS_STEP_DISTANCE 10
Jonas Ådahl62efe202012-09-27 18:40:41 +020063
Giulio Camuffo954f1832014-10-11 18:27:30 +030064struct x11_backend {
65 struct weston_backend base;
66 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040067
Benjamin Franzke3b288af2011-02-20 19:58:42 +010068 Display *dpy;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040069 xcb_connection_t *conn;
70 xcb_screen_t *screen;
71 xcb_cursor_t null_cursor;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050072 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040073 struct wl_event_source *xcb_source;
Daniel Stone576cd152012-06-01 12:14:02 +010074 struct xkb_keymap *xkb_keymap;
Daniel Stone62b33b62012-06-22 13:21:35 +010075 unsigned int has_xkb;
76 uint8_t xkb_event_base;
Vasily Khoruzhickd8943512013-01-07 22:36:02 +030077 int use_pixman;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040078
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -050079 int has_net_wm_state_fullscreen;
80
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040081 /* We could map multi-pointer X to multiple wayland seats, but
82 * for now we only support core X input. */
Kristian Høgsberg068b61c2013-02-25 17:04:47 -050083 struct weston_seat core_seat;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +020084 double prev_x;
85 double prev_y;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040086
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040087 struct {
88 xcb_atom_t wm_protocols;
89 xcb_atom_t wm_normal_hints;
90 xcb_atom_t wm_size_hints;
91 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050092 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040093 xcb_atom_t net_wm_name;
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -050094 xcb_atom_t net_supporting_wm_check;
95 xcb_atom_t net_supported;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050096 xcb_atom_t net_wm_icon;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -040097 xcb_atom_t net_wm_state;
98 xcb_atom_t net_wm_state_fullscreen;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050099 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400100 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500101 xcb_atom_t cardinal;
Daniel Stone855028f2012-05-01 20:37:10 +0100102 xcb_atom_t xkb_names;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400103 } atom;
104};
105
106struct x11_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500107 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400108
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400109 xcb_window_t window;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500110 struct weston_mode mode;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400111 struct wl_event_source *finish_frame_timer;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300112
113 xcb_gc_t gc;
114 xcb_shm_seg_t segment;
115 pixman_image_t *hw_surface;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300116 int shm_id;
117 void *buf;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300118 uint8_t depth;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200119 int32_t scale;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400120};
121
Derek Foremand540f4b2015-01-27 16:26:49 -0600122struct window_delete_data {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300123 struct x11_backend *backend;
Derek Foremand540f4b2015-01-27 16:26:49 -0600124 xcb_window_t window;
125};
126
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300127struct gl_renderer_interface *gl_renderer;
128
Marko61b4d3e2015-10-05 17:27:54 -0500129static xcb_screen_t *
130x11_compositor_get_default_screen(struct x11_backend *b)
131{
132 xcb_screen_iterator_t iter;
133 int i, screen_nbr = XDefaultScreen(b->dpy);
134
135 iter = xcb_setup_roots_iterator(xcb_get_setup(b->conn));
136 for (i = 0; iter.rem; xcb_screen_next(&iter), i++)
137 if (i == screen_nbr)
138 return iter.data;
139
140 return xcb_setup_roots_iterator(xcb_get_setup(b->conn)).data;
141}
142
Daniel Stone576cd152012-06-01 12:14:02 +0100143static struct xkb_keymap *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300144x11_backend_get_keymap(struct x11_backend *b)
Daniel Stone576cd152012-06-01 12:14:02 +0100145{
146 xcb_get_property_cookie_t cookie;
147 xcb_get_property_reply_t *reply;
Daniel Stone576cd152012-06-01 12:14:02 +0100148 struct xkb_rule_names names;
149 struct xkb_keymap *ret;
150 const char *value_all, *value_part;
151 int length_all, length_part;
152
153 memset(&names, 0, sizeof(names));
154
Giulio Camuffo954f1832014-10-11 18:27:30 +0300155 cookie = xcb_get_property(b->conn, 0, b->screen->root,
156 b->atom.xkb_names, b->atom.string, 0, 1024);
157 reply = xcb_get_property_reply(b->conn, cookie, NULL);
Daniel Stone576cd152012-06-01 12:14:02 +0100158 if (reply == NULL)
159 return NULL;
160
161 value_all = xcb_get_property_value(reply);
162 length_all = xcb_get_property_value_length(reply);
163 value_part = value_all;
164
165#define copy_prop_value(to) \
166 length_part = strlen(value_part); \
167 if (value_part + length_part < (value_all + length_all) && \
168 length_part > 0) \
169 names.to = value_part; \
170 value_part += length_part + 1;
171
172 copy_prop_value(rules);
173 copy_prop_value(model);
174 copy_prop_value(layout);
175 copy_prop_value(variant);
176 copy_prop_value(options);
177#undef copy_prop_value
178
Giulio Camuffo954f1832014-10-11 18:27:30 +0300179 ret = xkb_keymap_new_from_names(b->compositor->xkb_context, &names, 0);
Daniel Stone576cd152012-06-01 12:14:02 +0100180
181 free(reply);
182 return ret;
183}
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400184
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400185static uint32_t
Giulio Camuffo954f1832014-10-11 18:27:30 +0300186get_xkb_mod_mask(struct x11_backend *b, uint32_t in)
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400187{
Derek Foreman1281a362015-07-31 16:55:32 -0500188 struct weston_keyboard *keyboard =
189 weston_seat_get_keyboard(&b->core_seat);
190 struct weston_xkb_info *info = keyboard->xkb_info;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400191 uint32_t ret = 0;
192
193 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
194 ret |= (1 << info->shift_mod);
195 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
196 ret |= (1 << info->caps_mod);
197 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
198 ret |= (1 << info->ctrl_mod);
199 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
200 ret |= (1 << info->alt_mod);
201 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
202 ret |= (1 << info->mod2_mod);
203 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
204 ret |= (1 << info->mod3_mod);
205 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
206 ret |= (1 << info->super_mod);
207 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
208 ret |= (1 << info->mod5_mod);
209
210 return ret;
211}
212
Daniel Stone62b33b62012-06-22 13:21:35 +0100213static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300214x11_backend_setup_xkb(struct x11_backend *b)
Daniel Stone62b33b62012-06-22 13:21:35 +0100215{
216#ifndef HAVE_XCB_XKB
217 weston_log("XCB-XKB not available during build\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300218 b->has_xkb = 0;
219 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100220 return;
221#else
Derek Foreman1281a362015-07-31 16:55:32 -0500222 struct weston_keyboard *keyboard;
Daniel Stone62b33b62012-06-22 13:21:35 +0100223 const xcb_query_extension_reply_t *ext;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100224 xcb_generic_error_t *error;
Daniel Stonee2faa122012-06-22 13:21:39 +0100225 xcb_void_cookie_t select;
Ran Benita6a39d872012-10-31 20:14:57 +0200226 xcb_xkb_use_extension_cookie_t use_ext;
227 xcb_xkb_use_extension_reply_t *use_ext_reply;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100228 xcb_xkb_per_client_flags_cookie_t pcf;
229 xcb_xkb_per_client_flags_reply_t *pcf_reply;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400230 xcb_xkb_get_state_cookie_t state;
231 xcb_xkb_get_state_reply_t *state_reply;
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200232 uint32_t values[1] = { XCB_EVENT_MASK_PROPERTY_CHANGE };
Daniel Stone62b33b62012-06-22 13:21:35 +0100233
Giulio Camuffo954f1832014-10-11 18:27:30 +0300234 b->has_xkb = 0;
235 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100236
Giulio Camuffo954f1832014-10-11 18:27:30 +0300237 ext = xcb_get_extension_data(b->conn, &xcb_xkb_id);
Daniel Stone62b33b62012-06-22 13:21:35 +0100238 if (!ext) {
239 weston_log("XKB extension not available on host X11 server\n");
240 return;
241 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300242 b->xkb_event_base = ext->first_event;
Daniel Stone62b33b62012-06-22 13:21:35 +0100243
Giulio Camuffo954f1832014-10-11 18:27:30 +0300244 select = xcb_xkb_select_events_checked(b->conn,
Ran Benita424ae012012-10-31 00:13:08 +0200245 XCB_XKB_ID_USE_CORE_KBD,
246 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
247 0,
248 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
249 0,
250 0,
251 NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300252 error = xcb_request_check(b->conn, select);
Daniel Stonee2faa122012-06-22 13:21:39 +0100253 if (error) {
254 weston_log("error: failed to select for XKB state events\n");
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200255 free(error);
Daniel Stonee2faa122012-06-22 13:21:39 +0100256 return;
257 }
258
Giulio Camuffo954f1832014-10-11 18:27:30 +0300259 use_ext = xcb_xkb_use_extension(b->conn,
Ran Benita6a39d872012-10-31 20:14:57 +0200260 XCB_XKB_MAJOR_VERSION,
261 XCB_XKB_MINOR_VERSION);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300262 use_ext_reply = xcb_xkb_use_extension_reply(b->conn, use_ext, NULL);
Ran Benita6a39d872012-10-31 20:14:57 +0200263 if (!use_ext_reply) {
264 weston_log("couldn't start using XKB extension\n");
265 return;
266 }
267
268 if (!use_ext_reply->supported) {
269 weston_log("XKB extension version on the server is too old "
270 "(want %d.%d, has %d.%d)\n",
271 XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION,
272 use_ext_reply->serverMajor, use_ext_reply->serverMinor);
273 free(use_ext_reply);
274 return;
275 }
276 free(use_ext_reply);
277
Giulio Camuffo954f1832014-10-11 18:27:30 +0300278 pcf = xcb_xkb_per_client_flags(b->conn,
Daniel Stone3ee91e12012-06-22 13:21:36 +0100279 XCB_XKB_ID_USE_CORE_KBD,
280 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
281 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
282 0,
283 0,
284 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300285 pcf_reply = xcb_xkb_per_client_flags_reply(b->conn, pcf, NULL);
Ran Benita7109cc82012-10-31 20:14:58 +0200286 if (!pcf_reply ||
287 !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT)) {
Daniel Stone3ee91e12012-06-22 13:21:36 +0100288 weston_log("failed to set XKB per-client flags, not using "
289 "detectable repeat\n");
Ran Benita7109cc82012-10-31 20:14:58 +0200290 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100291 return;
292 }
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200293 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100294
Giulio Camuffo954f1832014-10-11 18:27:30 +0300295 state = xcb_xkb_get_state(b->conn, XCB_XKB_ID_USE_CORE_KBD);
296 state_reply = xcb_xkb_get_state_reply(b->conn, state, NULL);
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200297 if (!state_reply) {
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400298 weston_log("failed to get initial XKB state\n");
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400299 return;
300 }
301
Derek Foreman1281a362015-07-31 16:55:32 -0500302 keyboard = weston_seat_get_keyboard(&b->core_seat);
303 xkb_state_update_mask(keyboard->xkb_state.state,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300304 get_xkb_mod_mask(b, state_reply->baseMods),
305 get_xkb_mod_mask(b, state_reply->latchedMods),
306 get_xkb_mod_mask(b, state_reply->lockedMods),
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400307 0,
308 0,
309 state_reply->group);
310
311 free(state_reply);
312
Giulio Camuffo954f1832014-10-11 18:27:30 +0300313 xcb_change_window_attributes(b->conn, b->screen->root,
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200314 XCB_CW_EVENT_MASK, values);
315
Giulio Camuffo954f1832014-10-11 18:27:30 +0300316 b->has_xkb = 1;
Daniel Stone62b33b62012-06-22 13:21:35 +0100317#endif
318}
319
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200320#ifdef HAVE_XCB_XKB
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200321static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300322update_xkb_keymap(struct x11_backend *b)
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200323{
324 struct xkb_keymap *keymap;
325
Giulio Camuffo954f1832014-10-11 18:27:30 +0300326 keymap = x11_backend_get_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200327 if (!keymap) {
328 weston_log("failed to get XKB keymap\n");
329 return;
330 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300331 weston_seat_update_keymap(&b->core_seat, keymap);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200332 xkb_keymap_unref(keymap);
333}
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200334#endif
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200335
Darxus55973f22010-11-22 21:24:39 -0500336static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300337x11_input_create(struct x11_backend *b, int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400338{
Daniel Stone576cd152012-06-01 12:14:02 +0100339 struct xkb_keymap *keymap;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400340
Giulio Camuffo954f1832014-10-11 18:27:30 +0300341 weston_seat_init(&b->core_seat, b->compositor, "default");
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300342
343 if (no_input)
344 return 0;
345
Giulio Camuffo954f1832014-10-11 18:27:30 +0300346 weston_seat_init_pointer(&b->core_seat);
Daniel Stone576cd152012-06-01 12:14:02 +0100347
Giulio Camuffo954f1832014-10-11 18:27:30 +0300348 keymap = x11_backend_get_keymap(b);
349 if (weston_seat_init_keyboard(&b->core_seat, keymap) < 0)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500350 return -1;
Ran Benitac9c74152014-08-19 23:59:52 +0300351 xkb_keymap_unref(keymap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400352
Giulio Camuffo954f1832014-10-11 18:27:30 +0300353 x11_backend_setup_xkb(b);
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400354
Darxus55973f22010-11-22 21:24:39 -0500355 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400356}
357
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200358static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300359x11_input_destroy(struct x11_backend *b)
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200360{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300361 weston_seat_release(&b->core_seat);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200362}
363
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500364static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200365x11_output_start_repaint_loop(struct weston_output *output)
366{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400367 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200368
Pekka Paalanen662f3842015-03-18 12:17:26 +0200369 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200370 weston_output_finish_frame(output, &ts, WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200371}
372
David Herrmann1edf44c2013-10-22 17:11:26 +0200373static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300374x11_output_repaint_gl(struct weston_output *output_base,
375 pixman_region32_t *damage)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500376{
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500377 struct x11_output *output = (struct x11_output *)output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400378 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500379
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400380 ec->renderer->repaint_output(output_base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500381
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200382 pixman_region32_subtract(&ec->primary_plane.damage,
383 &ec->primary_plane.damage, damage);
384
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500385 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200386 return 0;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500387}
388
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300389static void
Alexander Larsson80f91632013-05-22 14:41:38 +0200390set_clip_for_output(struct weston_output *output_base, pixman_region32_t *region)
391{
392 struct x11_output *output = (struct x11_output *)output_base;
393 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300394 struct x11_backend *b = (struct x11_backend *)ec->backend;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500395 pixman_region32_t transformed_region;
Alexander Larsson80f91632013-05-22 14:41:38 +0200396 pixman_box32_t *rects;
397 xcb_rectangle_t *output_rects;
Alexander Larsson80f91632013-05-22 14:41:38 +0200398 xcb_void_cookie_t cookie;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500399 int nrects, i;
Alexander Larsson80f91632013-05-22 14:41:38 +0200400 xcb_generic_error_t *err;
401
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500402 pixman_region32_init(&transformed_region);
403 pixman_region32_copy(&transformed_region, region);
404 pixman_region32_translate(&transformed_region,
405 -output_base->x, -output_base->y);
406 weston_transformed_region(output_base->width, output_base->height,
407 output_base->transform,
408 output_base->current_scale,
409 &transformed_region, &transformed_region);
410
411 rects = pixman_region32_rectangles(&transformed_region, &nrects);
Alexander Larsson80f91632013-05-22 14:41:38 +0200412 output_rects = calloc(nrects, sizeof(xcb_rectangle_t));
413
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500414 if (output_rects == NULL) {
415 pixman_region32_fini(&transformed_region);
Alexander Larsson80f91632013-05-22 14:41:38 +0200416 return;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500417 }
Alexander Larsson80f91632013-05-22 14:41:38 +0200418
419 for (i = 0; i < nrects; i++) {
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500420 output_rects[i].x = rects[i].x1;
421 output_rects[i].y = rects[i].y1;
422 output_rects[i].width = rects[i].x2 - rects[i].x1;
423 output_rects[i].height = rects[i].y2 - rects[i].y1;
Alexander Larsson80f91632013-05-22 14:41:38 +0200424 }
425
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500426 pixman_region32_fini(&transformed_region);
427
Giulio Camuffo954f1832014-10-11 18:27:30 +0300428 cookie = xcb_set_clip_rectangles_checked(b->conn, XCB_CLIP_ORDERING_UNSORTED,
Alexander Larsson80f91632013-05-22 14:41:38 +0200429 output->gc,
430 0, 0, nrects,
431 output_rects);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300432 err = xcb_request_check(b->conn, cookie);
Alexander Larsson80f91632013-05-22 14:41:38 +0200433 if (err != NULL) {
434 weston_log("Failed to set clip rects, err: %d\n", err->error_code);
435 free(err);
436 }
437 free(output_rects);
438}
439
440
David Herrmann1edf44c2013-10-22 17:11:26 +0200441static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300442x11_output_repaint_shm(struct weston_output *output_base,
443 pixman_region32_t *damage)
444{
445 struct x11_output *output = (struct x11_output *)output_base;
446 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300447 struct x11_backend *b = (struct x11_backend *)ec->backend;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300448 xcb_void_cookie_t cookie;
449 xcb_generic_error_t *err;
450
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200451 pixman_renderer_output_set_buffer(output_base, output->hw_surface);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300452 ec->renderer->repaint_output(output_base, damage);
453
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300454 pixman_region32_subtract(&ec->primary_plane.damage,
455 &ec->primary_plane.damage, damage);
Alexander Larsson80f91632013-05-22 14:41:38 +0200456 set_clip_for_output(output_base, damage);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300457 cookie = xcb_shm_put_image_checked(b->conn, output->window, output->gc,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300458 pixman_image_get_width(output->hw_surface),
459 pixman_image_get_height(output->hw_surface),
460 0, 0,
461 pixman_image_get_width(output->hw_surface),
462 pixman_image_get_height(output->hw_surface),
463 0, 0, output->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
464 0, output->segment, 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300465 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300466 if (err != NULL) {
467 weston_log("Failed to put shm image, err: %d\n", err->error_code);
468 free(err);
469 }
470
471 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200472 return 0;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300473}
474
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100475static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400476finish_frame_handler(void *data)
477{
478 struct x11_output *output = data;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200479 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200480
Pekka Paalanen662f3842015-03-18 12:17:26 +0200481 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200482 weston_output_finish_frame(&output->base, &ts, 0);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400483
484 return 1;
485}
486
Matt Roper361d2ad2011-08-29 13:52:23 -0700487static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300488x11_output_deinit_shm(struct x11_backend *b, struct x11_output *output)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300489{
490 xcb_void_cookie_t cookie;
491 xcb_generic_error_t *err;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300492 xcb_free_gc(b->conn, output->gc);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300493
494 pixman_image_unref(output->hw_surface);
495 output->hw_surface = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300496 cookie = xcb_shm_detach_checked(b->conn, output->segment);
497 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300498 if (err) {
499 weston_log("xcb_shm_detach failed, error %d\n", err->error_code);
500 free(err);
501 }
502 shmdt(output->buf);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300503}
504
505static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500506x11_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700507{
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200508 struct x11_output *output = (struct x11_output *)output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300509 struct x11_backend *backend =
510 (struct x11_backend *)output->base.compositor->backend;
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200511
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200512 wl_event_source_remove(output->finish_frame_timer);
513
Giulio Camuffo954f1832014-10-11 18:27:30 +0300514 if (backend->use_pixman) {
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300515 pixman_renderer_output_destroy(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300516 x11_output_deinit_shm(backend, output);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300517 } else
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300518 gl_renderer->output_destroy(output_base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200519
Giulio Camuffo954f1832014-10-11 18:27:30 +0300520 xcb_destroy_window(backend->conn, output->window);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200521
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500522 weston_output_destroy(&output->base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200523
524 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700525}
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200526
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400527static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300528x11_output_set_wm_protocols(struct x11_backend *b,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500529 struct x11_output *output)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400530{
531 xcb_atom_t list[1];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400532
Giulio Camuffo954f1832014-10-11 18:27:30 +0300533 list[0] = b->atom.wm_delete_window;
534 xcb_change_property (b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400535 XCB_PROP_MODE_REPLACE,
536 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300537 b->atom.wm_protocols,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400538 XCB_ATOM_ATOM,
539 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500540 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400541 list);
542}
543
544struct wm_normal_hints {
545 uint32_t flags;
546 uint32_t pad[4];
547 int32_t min_width, min_height;
548 int32_t max_width, max_height;
549 int32_t width_inc, height_inc;
550 int32_t min_aspect_x, min_aspect_y;
551 int32_t max_aspect_x, max_aspect_y;
552 int32_t base_width, base_height;
553 int32_t win_gravity;
554};
555
556#define WM_NORMAL_HINTS_MIN_SIZE 16
557#define WM_NORMAL_HINTS_MAX_SIZE 32
558
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500559static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300560x11_output_set_icon(struct x11_backend *b,
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400561 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500562{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400563 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400564 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400565 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500566
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400567 image = load_image(filename);
568 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500569 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400570 width = pixman_image_get_width(image);
571 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500572 icon = malloc(width * height * 4 + 8);
573 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400574 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500575 return;
576 }
577
578 icon[0] = width;
579 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400580 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300581 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
582 b->atom.net_wm_icon, b->atom.cardinal, 32,
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500583 width * height + 2, icon);
584 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400585 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500586}
587
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500588static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300589x11_output_wait_for_map(struct x11_backend *b, struct x11_output *output)
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500590{
591 xcb_map_notify_event_t *map_notify;
592 xcb_configure_notify_event_t *configure_notify;
593 xcb_generic_event_t *event;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500594 int mapped = 0, configured = 0;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500595 uint8_t response_type;
596
597 /* This isn't the nicest way to do this. Ideally, we could
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500598 * just go back to the main loop and once we get the configure
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500599 * notify, we add the output to the compositor. While we do
600 * support output hotplug, we can't start up with no outputs.
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500601 * We could add the output and then resize once we get the
602 * configure notify, but we don't want to start up and
603 * immediately resize the output.
604 *
605 * Also, some window managers don't give us our final
606 * fullscreen size before map_notify, so if we don't get a
607 * configure_notify before map_notify, we just wait for the
608 * first one and hope that's our size. */
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500609
Giulio Camuffo954f1832014-10-11 18:27:30 +0300610 xcb_flush(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500611
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500612 while (!mapped || !configured) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300613 event = xcb_wait_for_event(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500614 response_type = event->response_type & ~0x80;
615
616 switch (response_type) {
617 case XCB_MAP_NOTIFY:
618 map_notify = (xcb_map_notify_event_t *) event;
619 if (map_notify->window == output->window)
620 mapped = 1;
621 break;
622
623 case XCB_CONFIGURE_NOTIFY:
624 configure_notify =
625 (xcb_configure_notify_event_t *) event;
626
Alexander Larsson4ea95522013-05-22 14:41:37 +0200627
628 if (configure_notify->width % output->scale != 0 ||
629 configure_notify->height % output->scale != 0)
630 weston_log("Resolution is not a multiple of screen size, rounding\n");
Jason Ekstrandd89a0942014-03-07 15:29:14 -0600631 output->mode.width = configure_notify->width;
632 output->mode.height = configure_notify->height;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500633 configured = 1;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500634 break;
635 }
636 }
637}
638
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300639static xcb_visualtype_t *
640find_visual_by_id(xcb_screen_t *screen,
641 xcb_visualid_t id)
642{
643 xcb_depth_iterator_t i;
644 xcb_visualtype_iterator_t j;
645 for (i = xcb_screen_allowed_depths_iterator(screen);
646 i.rem;
647 xcb_depth_next(&i)) {
648 for (j = xcb_depth_visuals_iterator(i.data);
649 j.rem;
650 xcb_visualtype_next(&j)) {
651 if (j.data->visual_id == id)
652 return j.data;
653 }
654 }
655 return 0;
656}
657
658static uint8_t
659get_depth_of_visual(xcb_screen_t *screen,
660 xcb_visualid_t id)
661{
662 xcb_depth_iterator_t i;
663 xcb_visualtype_iterator_t j;
664 for (i = xcb_screen_allowed_depths_iterator(screen);
665 i.rem;
666 xcb_depth_next(&i)) {
667 for (j = xcb_depth_visuals_iterator(i.data);
668 j.rem;
669 xcb_visualtype_next(&j)) {
670 if (j.data->visual_id == id)
671 return i.data->depth;
672 }
673 }
674 return 0;
675}
676
677static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300678x11_output_init_shm(struct x11_backend *b, struct x11_output *output,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300679 int width, int height)
680{
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300681 xcb_visualtype_t *visual_type;
Marko61b4d3e2015-10-05 17:27:54 -0500682 xcb_screen_t *screen;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300683 xcb_format_iterator_t fmt;
684 xcb_void_cookie_t cookie;
685 xcb_generic_error_t *err;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300686 const xcb_query_extension_reply_t *ext;
687 int bitsperpixel = 0;
688 pixman_format_code_t pixman_format;
689
690 /* Check if SHM is available */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300691 ext = xcb_get_extension_data(b->conn, &xcb_shm_id);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300692 if (ext == NULL || !ext->present) {
693 /* SHM is missing */
694 weston_log("SHM extension is not available\n");
695 errno = ENOENT;
696 return -1;
697 }
698
Marko61b4d3e2015-10-05 17:27:54 -0500699 screen = x11_compositor_get_default_screen(b);
700 visual_type = find_visual_by_id(screen, screen->root_visual);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300701 if (!visual_type) {
702 weston_log("Failed to lookup visual for root window\n");
703 errno = ENOENT;
704 return -1;
705 }
706 weston_log("Found visual, bits per value: %d, red_mask: %.8x, green_mask: %.8x, blue_mask: %.8x\n",
707 visual_type->bits_per_rgb_value,
708 visual_type->red_mask,
709 visual_type->green_mask,
710 visual_type->blue_mask);
Marko61b4d3e2015-10-05 17:27:54 -0500711 output->depth = get_depth_of_visual(screen, screen->root_visual);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300712 weston_log("Visual depth is %d\n", output->depth);
713
Giulio Camuffo954f1832014-10-11 18:27:30 +0300714 for (fmt = xcb_setup_pixmap_formats_iterator(xcb_get_setup(b->conn));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300715 fmt.rem;
716 xcb_format_next(&fmt)) {
717 if (fmt.data->depth == output->depth) {
718 bitsperpixel = fmt.data->bits_per_pixel;
719 break;
720 }
721 }
722 weston_log("Found format for depth %d, bpp: %d\n",
723 output->depth, bitsperpixel);
724
725 if (bitsperpixel == 32 &&
726 visual_type->red_mask == 0xff0000 &&
727 visual_type->green_mask == 0x00ff00 &&
728 visual_type->blue_mask == 0x0000ff) {
729 weston_log("Will use x8r8g8b8 format for SHM surfaces\n");
730 pixman_format = PIXMAN_x8r8g8b8;
MoD1b55a592013-11-28 05:24:21 +0000731 } else if (bitsperpixel == 16 &&
732 visual_type->red_mask == 0x00f800 &&
733 visual_type->green_mask == 0x0007e0 &&
734 visual_type->blue_mask == 0x00001f) {
735 weston_log("Will use r5g6b5 format for SHM surfaces\n");
736 pixman_format = PIXMAN_r5g6b5;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300737 } else {
738 weston_log("Can't find appropriate format for SHM pixmap\n");
739 errno = ENOTSUP;
740 return -1;
741 }
742
743
744 /* Create SHM segment and attach it */
745 output->shm_id = shmget(IPC_PRIVATE, width * height * (bitsperpixel / 8), IPC_CREAT | S_IRWXU);
746 if (output->shm_id == -1) {
747 weston_log("x11shm: failed to allocate SHM segment\n");
748 return -1;
749 }
750 output->buf = shmat(output->shm_id, NULL, 0 /* read/write */);
751 if (-1 == (long)output->buf) {
752 weston_log("x11shm: failed to attach SHM segment\n");
753 return -1;
754 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300755 output->segment = xcb_generate_id(b->conn);
756 cookie = xcb_shm_attach_checked(b->conn, output->segment, output->shm_id, 1);
757 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300758 if (err) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700759 weston_log("x11shm: xcb_shm_attach error %d, op code %d, resource id %d\n",
760 err->error_code, err->major_code, err->minor_code);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300761 free(err);
762 return -1;
763 }
764
765 shmctl(output->shm_id, IPC_RMID, NULL);
766
767 /* Now create pixman image */
768 output->hw_surface = pixman_image_create_bits(pixman_format, width, height, output->buf,
769 width * (bitsperpixel / 8));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300770
Giulio Camuffo954f1832014-10-11 18:27:30 +0300771 output->gc = xcb_generate_id(b->conn);
772 xcb_create_gc(b->conn, output->gc, output->window, 0, NULL);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300773
774 return 0;
775}
776
Scott Moreau1bad5db2012-08-18 01:04:05 -0600777static struct x11_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300778x11_backend_create_output(struct x11_backend *b, int x, int y,
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300779 int width, int height, int fullscreen,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600780 int no_input, char *configured_name,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200781 uint32_t transform, int32_t scale)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400782{
Scott Moreau1bad5db2012-08-18 01:04:05 -0600783 static const char name[] = "Weston Compositor";
Kristian Høgsberg86000402012-01-03 23:24:14 -0500784 static const char class[] = "weston-1\0Weston Compositor";
Benoit Gschwind5c2f20e2016-06-05 19:01:11 +0200785 char *title = NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400786 struct x11_output *output;
Marko61b4d3e2015-10-05 17:27:54 -0500787 xcb_screen_t *screen;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400788 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400789 struct wl_event_loop *loop;
Kristian Høgsberg8404acb2014-03-18 12:15:42 -0700790 int output_width, output_height, width_mm, height_mm;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300791 int ret;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400792 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500793 xcb_atom_t atom_list[1];
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300794 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400795 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300796 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400797 0
798 };
799
Alexander Larsson4ea95522013-05-22 14:41:37 +0200800 output_width = width * scale;
801 output_height = height * scale;
802
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300803 if (!no_input)
804 values[0] |=
805 XCB_EVENT_MASK_KEY_PRESS |
806 XCB_EVENT_MASK_KEY_RELEASE |
807 XCB_EVENT_MASK_BUTTON_PRESS |
808 XCB_EVENT_MASK_BUTTON_RELEASE |
809 XCB_EVENT_MASK_POINTER_MOTION |
810 XCB_EVENT_MASK_ENTER_WINDOW |
811 XCB_EVENT_MASK_LEAVE_WINDOW |
812 XCB_EVENT_MASK_KEYMAP_STATE |
813 XCB_EVENT_MASK_FOCUS_CHANGE;
814
Peter Huttererf3d62272013-08-08 11:57:05 +1000815 output = zalloc(sizeof *output);
Bryce Harrington665b0252015-05-26 18:14:21 -0700816 if (output == NULL) {
817 perror("zalloc");
Scott Moreau1bad5db2012-08-18 01:04:05 -0600818 return NULL;
Bryce Harrington665b0252015-05-26 18:14:21 -0700819 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400820
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400821 output->mode.flags =
822 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200823
Alexander Larsson0b135062013-05-28 16:23:36 +0200824 output->mode.width = output_width;
825 output->mode.height = output_height;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400826 output->mode.refresh = 60000;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200827 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400828 wl_list_init(&output->base.mode_list);
829 wl_list_insert(&output->base.mode_list, &output->mode.link);
830
Giulio Camuffo954f1832014-10-11 18:27:30 +0300831 values[1] = b->null_cursor;
832 output->window = xcb_generate_id(b->conn);
Marko61b4d3e2015-10-05 17:27:54 -0500833 screen = x11_compositor_get_default_screen(b);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300834 xcb_create_window(b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400835 XCB_COPY_FROM_PARENT,
836 output->window,
Marko61b4d3e2015-10-05 17:27:54 -0500837 screen->root,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400838 0, 0,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200839 output_width, output_height,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400840 0,
841 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Marko61b4d3e2015-10-05 17:27:54 -0500842 screen->root_visual,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400843 mask, values);
844
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500845 if (fullscreen) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300846 atom_list[0] = b->atom.net_wm_state_fullscreen;
847 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500848 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300849 b->atom.net_wm_state,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500850 XCB_ATOM_ATOM, 32,
851 ARRAY_LENGTH(atom_list), atom_list);
852 } else {
853 /* Don't resize me. */
854 memset(&normal_hints, 0, sizeof normal_hints);
855 normal_hints.flags =
856 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200857 normal_hints.min_width = output_width;
858 normal_hints.min_height = output_height;
859 normal_hints.max_width = output_width;
860 normal_hints.max_height = output_height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300861 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
862 b->atom.wm_normal_hints,
863 b->atom.wm_size_hints, 32,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500864 sizeof normal_hints / 4,
865 (uint8_t *) &normal_hints);
866 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400867
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400868 /* Set window name. Don't bother with non-EWMH WMs. */
Benoit Gschwind5c2f20e2016-06-05 19:01:11 +0200869 if (configured_name) {
870 if (asprintf(&title, "%s - %s", name, configured_name) < 0)
871 title = NULL;
872 } else {
873 title = strdup(name);
874 }
875
876 if (title) {
877 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
878 b->atom.net_wm_name, b->atom.utf8_string, 8,
879 strlen(title), title);
880 free(title);
881 } else {
882 xcb_destroy_window(b->conn, output->window);
883 free(output);
884 return NULL;
885 }
886
Giulio Camuffo954f1832014-10-11 18:27:30 +0300887 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
888 b->atom.wm_class, b->atom.string, 8,
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500889 sizeof class, class);
890
Giulio Camuffo954f1832014-10-11 18:27:30 +0300891 x11_output_set_icon(b, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500892
Giulio Camuffo954f1832014-10-11 18:27:30 +0300893 x11_output_set_wm_protocols(b, output);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500894
Giulio Camuffo954f1832014-10-11 18:27:30 +0300895 xcb_map_window(b->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400896
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -0500897 if (fullscreen)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300898 x11_output_wait_for_map(b, output);
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400899
Jonas Ådahle5a12252013-04-05 23:07:11 +0200900 output->base.start_repaint_loop = x11_output_start_repaint_loop;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300901 if (b->use_pixman)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300902 output->base.repaint = x11_output_repaint_shm;
903 else
904 output->base.repaint = x11_output_repaint_gl;
Matt Roper361d2ad2011-08-29 13:52:23 -0700905 output->base.destroy = x11_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800906 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200907 output->base.set_backlight = NULL;
908 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800909 output->base.switch_mode = NULL;
Hardeningff39efa2013-09-18 23:56:35 +0200910 output->base.current_mode = &output->mode;
Jason Ekstrand22e16b62014-09-15 12:10:27 -0700911 output->base.make = "weston-X11";
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500912 output->base.model = "none";
Ander Conselvan de Oliveirad4ea71e2013-12-18 19:51:40 +0200913
914 if (configured_name)
915 output->base.name = strdup(configured_name);
916
Giulio Camuffo954f1832014-10-11 18:27:30 +0300917 width_mm = width * b->screen->width_in_millimeters /
918 b->screen->width_in_pixels;
919 height_mm = height * b->screen->height_in_millimeters /
920 b->screen->height_in_pixels;
921 weston_output_init(&output->base, b->compositor,
Kristian Høgsberg8404acb2014-03-18 12:15:42 -0700922 x, y, width_mm, height_mm, transform, scale);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100923
Giulio Camuffo954f1832014-10-11 18:27:30 +0300924 if (b->use_pixman) {
925 if (x11_output_init_shm(b, output,
Kristian Høgsbergb5a4f982013-10-23 23:38:50 -0700926 output->mode.width,
Bryce Harrington665b0252015-05-26 18:14:21 -0700927 output->mode.height) < 0) {
928 weston_log("Failed to initialize SHM for the X11 output\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300929 return NULL;
Bryce Harrington665b0252015-05-26 18:14:21 -0700930 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300931 if (pixman_renderer_output_create(&output->base) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700932 weston_log("Failed to create pixman renderer for output\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300933 x11_output_deinit_shm(b, output);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300934 return NULL;
935 }
936 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +0100937 /* eglCreatePlatformWindowSurfaceEXT takes a Window*
938 * but eglCreateWindowSurface takes a Window. */
939 Window xid = (Window) output->window;
940
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300941 ret = gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000942 (EGLNativeWindowType) output->window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100943 &xid,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000944 gl_renderer->opaque_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500945 NULL,
946 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300947 if (ret < 0)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300948 return NULL;
949 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100950
Giulio Camuffo954f1832014-10-11 18:27:30 +0300951 loop = wl_display_get_event_loop(b->compositor->wl_display);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100952 output->finish_frame_timer =
953 wl_event_loop_add_timer(loop, finish_frame_handler, output);
954
Giulio Camuffo954f1832014-10-11 18:27:30 +0300955 weston_compositor_add_output(b->compositor, &output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400956
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400957 weston_log("x11 output %dx%d, window id %d\n",
958 width, height, output->window);
959
Scott Moreau1bad5db2012-08-18 01:04:05 -0600960 return output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400961}
962
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400963static struct x11_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300964x11_backend_find_output(struct x11_backend *b, xcb_window_t window)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400965{
966 struct x11_output *output;
967
Giulio Camuffo954f1832014-10-11 18:27:30 +0300968 wl_list_for_each(output, &b->compositor->output_list, base.link) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400969 if (output->window == window)
970 return output;
971 }
972
Derek Foreman99bfa642014-12-11 15:44:46 -0600973 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400974}
975
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200976static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300977x11_backend_delete_window(struct x11_backend *b, xcb_window_t window)
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200978{
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +0200979 struct x11_output *output;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200980
Giulio Camuffo954f1832014-10-11 18:27:30 +0300981 output = x11_backend_find_output(b, window);
Derek Foreman99bfa642014-12-11 15:44:46 -0600982 if (output)
983 x11_output_destroy(&output->base);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200984
Giulio Camuffo954f1832014-10-11 18:27:30 +0300985 xcb_flush(b->conn);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200986
Giulio Camuffo954f1832014-10-11 18:27:30 +0300987 if (wl_list_empty(&b->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +0300988 weston_compositor_exit(b->compositor);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200989}
990
Derek Foremand540f4b2015-01-27 16:26:49 -0600991static void delete_cb(void *data)
992{
993 struct window_delete_data *wd = data;
994
Giulio Camuffo954f1832014-10-11 18:27:30 +0300995 x11_backend_delete_window(wd->backend, wd->window);
Derek Foremand540f4b2015-01-27 16:26:49 -0600996 free(wd);
997}
998
Daniel Stone154c34c2012-06-22 13:21:40 +0100999#ifdef HAVE_XCB_XKB
Daniel Stonee2faa122012-06-22 13:21:39 +01001000static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001001update_xkb_state(struct x11_backend *b, xcb_xkb_state_notify_event_t *state)
Daniel Stonee2faa122012-06-22 13:21:39 +01001002{
Derek Foreman1281a362015-07-31 16:55:32 -05001003 struct weston_keyboard *keyboard =
1004 weston_seat_get_keyboard(&b->core_seat);
1005
1006 xkb_state_update_mask(keyboard->xkb_state.state,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001007 get_xkb_mod_mask(b, state->baseMods),
1008 get_xkb_mod_mask(b, state->latchedMods),
1009 get_xkb_mod_mask(b, state->lockedMods),
Daniel Stonee2faa122012-06-22 13:21:39 +01001010 0,
1011 0,
1012 state->group);
1013
Giulio Camuffo954f1832014-10-11 18:27:30 +03001014 notify_modifiers(&b->core_seat,
1015 wl_display_next_serial(b->compositor->wl_display));
Daniel Stonee2faa122012-06-22 13:21:39 +01001016}
1017#endif
1018
Daniel Stone154c34c2012-06-22 13:21:40 +01001019/**
1020 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
1021 * the best we can do (given that XCB also lacks XI2 support), is to take
1022 * the state from the core key events. Unfortunately that only gives us
1023 * the effective (i.e. union of depressed/latched/locked) state, and we
1024 * need the granularity.
1025 *
1026 * So we still update the state with every key event we see, but also use
1027 * the state field from X11 events as a mask so we don't get any stuck
1028 * modifiers.
1029 */
1030static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001031update_xkb_state_from_core(struct x11_backend *b, uint16_t x11_mask)
Daniel Stone154c34c2012-06-22 13:21:40 +01001032{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001033 uint32_t mask = get_xkb_mod_mask(b, x11_mask);
Derek Foreman1281a362015-07-31 16:55:32 -05001034 struct weston_keyboard *keyboard
1035 = weston_seat_get_keyboard(&b->core_seat);
Daniel Stone154c34c2012-06-22 13:21:40 +01001036
Derek Foreman1281a362015-07-31 16:55:32 -05001037 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone154c34c2012-06-22 13:21:40 +01001038 keyboard->modifiers.mods_depressed & mask,
1039 keyboard->modifiers.mods_latched & mask,
1040 keyboard->modifiers.mods_locked & mask,
1041 0,
1042 0,
1043 (x11_mask >> 13) & 3);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001044 notify_modifiers(&b->core_seat,
1045 wl_display_next_serial(b->compositor->wl_display));
Daniel Stone154c34c2012-06-22 13:21:40 +01001046}
1047
Tiago Vignattia3a71622011-12-08 17:03:17 +02001048static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001049x11_backend_deliver_button_event(struct x11_backend *b,
1050 xcb_generic_event_t *event, int state)
Tiago Vignattia3a71622011-12-08 17:03:17 +02001051{
1052 xcb_button_press_event_t *button_event =
1053 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +01001054 uint32_t button;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001055 struct x11_output *output;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001056 struct weston_pointer_axis_event weston_event;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001057
Giulio Camuffo954f1832014-10-11 18:27:30 +03001058 output = x11_backend_find_output(b, button_event->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001059 if (!output)
1060 return;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001061
1062 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001063 xcb_grab_pointer(b->conn, 0, output->window,
Kristian Høgsberg73308622012-10-30 11:04:52 -04001064 XCB_EVENT_MASK_BUTTON_PRESS |
1065 XCB_EVENT_MASK_BUTTON_RELEASE |
1066 XCB_EVENT_MASK_POINTER_MOTION |
1067 XCB_EVENT_MASK_ENTER_WINDOW |
1068 XCB_EVENT_MASK_LEAVE_WINDOW,
1069 XCB_GRAB_MODE_ASYNC,
1070 XCB_GRAB_MODE_ASYNC,
1071 output->window, XCB_CURSOR_NONE,
1072 button_event->time);
1073 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001074 xcb_ungrab_pointer(b->conn, button_event->time);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001075
Giulio Camuffo954f1832014-10-11 18:27:30 +03001076 if (!b->has_xkb)
1077 update_xkb_state_from_core(b, button_event->state);
Daniel Stone154c34c2012-06-22 13:21:40 +01001078
Tiago Vignattia3a71622011-12-08 17:03:17 +02001079 switch (button_event->detail) {
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001080 case 1:
1081 button = BTN_LEFT;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001082 break;
1083 case 2:
1084 button = BTN_MIDDLE;
1085 break;
1086 case 3:
1087 button = BTN_RIGHT;
1088 break;
1089 case 4:
Jonas Ådahl62efe202012-09-27 18:40:41 +02001090 /* Axis are measured in pixels, but the xcb events are discrete
1091 * steps. Therefore move the axis by some pixels every step. */
Peter Hutterer89b6a492016-01-18 15:58:17 +10001092 if (state) {
1093 weston_event.value = -DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001094 weston_event.discrete = -1;
1095 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001096 weston_event.axis =
1097 WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001098 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001099 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001100 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001101 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001102 }
Scott Moreau210d0792012-03-22 10:47:01 -06001103 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001104 case 5:
Peter Hutterer89b6a492016-01-18 15:58:17 +10001105 if (state) {
1106 weston_event.value = DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001107 weston_event.discrete = 1;
1108 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001109 weston_event.axis =
1110 WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001111 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001112 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001113 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001114 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001115 }
Scott Moreau210d0792012-03-22 10:47:01 -06001116 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001117 case 6:
Peter Hutterer89b6a492016-01-18 15:58:17 +10001118 if (state) {
1119 weston_event.value = -DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001120 weston_event.discrete = -1;
1121 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001122 weston_event.axis =
1123 WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001124 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001125 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001126 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001127 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001128 }
Scott Moreau210d0792012-03-22 10:47:01 -06001129 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001130 case 7:
Peter Hutterer89b6a492016-01-18 15:58:17 +10001131 if (state) {
1132 weston_event.value = DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001133 weston_event.discrete = 1;
1134 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001135 weston_event.axis =
1136 WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001137 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001138 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001139 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001140 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001141 }
Tiago Vignattia3a71622011-12-08 17:03:17 +02001142 return;
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001143 default:
1144 button = button_event->detail + BTN_SIDE - 8;
1145 break;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001146 }
1147
Giulio Camuffo954f1832014-10-11 18:27:30 +03001148 notify_button(&b->core_seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001149 weston_compositor_get_time(), button,
1150 state ? WL_POINTER_BUTTON_STATE_PRESSED :
1151 WL_POINTER_BUTTON_STATE_RELEASED);
Peter Hutterer87743e92016-01-18 16:38:22 +10001152 notify_pointer_frame(&b->core_seat);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001153}
1154
Scott Moreau1bad5db2012-08-18 01:04:05 -06001155static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001156x11_backend_deliver_motion_event(struct x11_backend *b,
1157 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001158{
1159 struct x11_output *output;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001160 double x, y;
Jonas Ådahld2510102014-10-05 21:39:14 +02001161 struct weston_pointer_motion_event motion_event = { 0 };
Scott Moreau1bad5db2012-08-18 01:04:05 -06001162 xcb_motion_notify_event_t *motion_notify =
1163 (xcb_motion_notify_event_t *) event;
1164
Giulio Camuffo954f1832014-10-11 18:27:30 +03001165 if (!b->has_xkb)
1166 update_xkb_state_from_core(b, motion_notify->state);
1167 output = x11_backend_find_output(b, motion_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001168 if (!output)
1169 return;
1170
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001171 weston_output_transform_coordinate(&output->base,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001172 motion_notify->event_x,
1173 motion_notify->event_y,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05001174 &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001175
Jonas Ådahld2510102014-10-05 21:39:14 +02001176 motion_event = (struct weston_pointer_motion_event) {
1177 .mask = WESTON_POINTER_MOTION_REL,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001178 .dx = x - b->prev_x,
1179 .dy = y - b->prev_y
Jonas Ådahld2510102014-10-05 21:39:14 +02001180 };
1181
Giulio Camuffo954f1832014-10-11 18:27:30 +03001182 notify_motion(&b->core_seat, weston_compositor_get_time(),
Jonas Ådahld2510102014-10-05 21:39:14 +02001183 &motion_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001184 notify_pointer_frame(&b->core_seat);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001185
Giulio Camuffo954f1832014-10-11 18:27:30 +03001186 b->prev_x = x;
1187 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001188}
1189
1190static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001191x11_backend_deliver_enter_event(struct x11_backend *b,
1192 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001193{
1194 struct x11_output *output;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001195 double x, y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001196
1197 xcb_enter_notify_event_t *enter_notify =
1198 (xcb_enter_notify_event_t *) event;
1199 if (enter_notify->state >= Button1Mask)
1200 return;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001201 if (!b->has_xkb)
1202 update_xkb_state_from_core(b, enter_notify->state);
1203 output = x11_backend_find_output(b, enter_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001204 if (!output)
1205 return;
1206
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001207 weston_output_transform_coordinate(&output->base,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001208 enter_notify->event_x,
1209 enter_notify->event_y, &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001210
Giulio Camuffo954f1832014-10-11 18:27:30 +03001211 notify_pointer_focus(&b->core_seat, &output->base, x, y);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001212
Giulio Camuffo954f1832014-10-11 18:27:30 +03001213 b->prev_x = x;
1214 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001215}
1216
Kristian Høgsbergee724822011-04-21 14:51:44 -04001217static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001218x11_backend_next_event(struct x11_backend *b,
1219 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -04001220{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001221 if (mask & WL_EVENT_READABLE) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001222 *event = xcb_poll_for_event(b->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -04001223 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001224#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Giulio Camuffo954f1832014-10-11 18:27:30 +03001225 *event = xcb_poll_for_queued_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001226#else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001227 *event = xcb_poll_for_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001228#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -04001229 }
1230
1231 return *event != NULL;
1232}
1233
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001234static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001235x11_backend_handle_event(int fd, uint32_t mask, void *data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001236{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001237 struct x11_backend *b = data;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001238 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001239 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001240 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001241 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001242 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001243 xcb_keymap_notify_event_t *keymap_notify;
1244 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001245 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001246 xcb_atom_t atom;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001247 xcb_window_t window;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001248 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001249 uint32_t i, set;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001250 uint8_t response_type;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001251 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001252
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001253 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001254 count = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001255 while (x11_backend_next_event(b, &event, mask)) {
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001256 response_type = event->response_type & ~0x80;
1257
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001258 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1259 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001260 /* Suppress key repeat events; this is only used if we
1261 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001262 key_release = (xcb_key_press_event_t *) prev;
1263 key_press = (xcb_key_press_event_t *) event;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001264 if (response_type == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -07001265 key_release->time == key_press->time &&
1266 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001267 /* Don't deliver the held key release
1268 * event or the new key press event. */
1269 free(event);
1270 free(prev);
1271 prev = NULL;
1272 continue;
1273 } else {
1274 /* Deliver the held key release now
1275 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001276 * event below. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001277 update_xkb_state_from_core(b, key_release->state);
1278 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001279 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001280 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001281 WL_KEYBOARD_KEY_STATE_RELEASED,
1282 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001283 free(prev);
1284 prev = NULL;
1285 break;
1286 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001287
1288 case XCB_FOCUS_IN:
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001289 assert(response_type == XCB_KEYMAP_NOTIFY);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001290 keymap_notify = (xcb_keymap_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001291 b->keys.size = 0;
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001292 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
1293 set = keymap_notify->keys[i >> 3] &
1294 (1 << (i & 7));
1295 if (set) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001296 k = wl_array_add(&b->keys, sizeof *k);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001297 *k = i;
1298 }
1299 }
1300
Daniel Stoned6da09e2012-06-22 13:21:29 +01001301 /* Unfortunately the state only comes with the enter
1302 * event, rather than with the focus event. I'm not
1303 * sure of the exact semantics around it and whether
1304 * we can ensure that we get both? */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001305 notify_keyboard_focus_in(&b->core_seat, &b->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001306 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001307
1308 free(prev);
1309 prev = NULL;
1310 break;
1311
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001312 default:
1313 /* No previous event held */
1314 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001315 }
1316
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001317 switch (response_type) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001318 case XCB_KEY_PRESS:
1319 key_press = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001320 if (!b->has_xkb)
1321 update_xkb_state_from_core(b, key_press->state);
1322 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001323 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001324 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001325 WL_KEYBOARD_KEY_STATE_PRESSED,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001326 b->has_xkb ? STATE_UPDATE_NONE :
Daniel Stonee2faa122012-06-22 13:21:39 +01001327 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001328 break;
1329 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001330 /* If we don't have XKB, we need to use the lame
1331 * autorepeat detection above. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001332 if (!b->has_xkb) {
Daniel Stone3ee91e12012-06-22 13:21:36 +01001333 prev = event;
1334 break;
1335 }
1336 key_release = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001337 notify_key(&b->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +01001338 weston_compositor_get_time(),
1339 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001340 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +01001341 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001342 break;
1343 case XCB_BUTTON_PRESS:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001344 x11_backend_deliver_button_event(b, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001345 break;
1346 case XCB_BUTTON_RELEASE:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001347 x11_backend_deliver_button_event(b, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001348 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001349 case XCB_MOTION_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001350 x11_backend_deliver_motion_event(b, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001351 break;
1352
1353 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001354 expose = (xcb_expose_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001355 output = x11_backend_find_output(b, expose->window);
Derek Foreman99bfa642014-12-11 15:44:46 -06001356 if (!output)
1357 break;
1358
Kristian Høgsberga98c2e12013-12-05 12:30:37 -08001359 weston_output_damage(&output->base);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001360 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001361 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001362
1363 case XCB_ENTER_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001364 x11_backend_deliver_enter_event(b, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001365 break;
1366
1367 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001368 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -05001369 if (enter_notify->state >= Button1Mask)
1370 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001371 if (!b->has_xkb)
1372 update_xkb_state_from_core(b, enter_notify->state);
1373 notify_pointer_focus(&b->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001374 break;
1375
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001376 case XCB_CLIENT_MESSAGE:
1377 client_message = (xcb_client_message_event_t *) event;
1378 atom = client_message->data.data32[0];
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001379 window = client_message->window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001380 if (atom == b->atom.wm_delete_window) {
Derek Foremand540f4b2015-01-27 16:26:49 -06001381 struct wl_event_loop *loop;
1382 struct window_delete_data *data = malloc(sizeof *data);
1383
1384 /* if malloc failed we should at least try to
1385 * delete the window, even if it may result in
1386 * a crash.
1387 */
1388 if (!data) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001389 x11_backend_delete_window(b, window);
Derek Foremand540f4b2015-01-27 16:26:49 -06001390 break;
1391 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001392 data->backend = b;
Derek Foremand540f4b2015-01-27 16:26:49 -06001393 data->window = window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001394 loop = wl_display_get_event_loop(b->compositor->wl_display);
Derek Foremand540f4b2015-01-27 16:26:49 -06001395 wl_event_loop_add_idle(loop, delete_cb, data);
1396 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001397 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001398
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001399 case XCB_FOCUS_IN:
1400 focus_in = (xcb_focus_in_event_t *) event;
1401 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1402 break;
1403
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001404 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001405 break;
1406
1407 case XCB_FOCUS_OUT:
1408 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -05001409 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1410 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001411 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001412 notify_keyboard_focus_out(&b->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001413 break;
1414
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001415 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001416 break;
1417 }
1418
Daniel Stonee2faa122012-06-22 13:21:39 +01001419#ifdef HAVE_XCB_XKB
Giulio Camuffo954f1832014-10-11 18:27:30 +03001420 if (b->has_xkb) {
1421 if (response_type == b->xkb_event_base) {
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001422 xcb_xkb_state_notify_event_t *state =
1423 (xcb_xkb_state_notify_event_t *) event;
1424 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001425 update_xkb_state(b, state);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001426 } else if (response_type == XCB_PROPERTY_NOTIFY) {
1427 xcb_property_notify_event_t *prop_notify =
1428 (xcb_property_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001429 if (prop_notify->window == b->screen->root &&
1430 prop_notify->atom == b->atom.xkb_names &&
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001431 prop_notify->state == XCB_PROPERTY_NEW_VALUE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001432 update_xkb_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001433 }
Daniel Stonee2faa122012-06-22 13:21:39 +01001434 }
1435#endif
1436
Bill Spitzakb715cec2012-06-05 17:08:23 -04001437 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001438 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001439 free (event);
1440 }
1441
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001442 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1443 case XCB_KEY_RELEASE:
1444 key_release = (xcb_key_press_event_t *) prev;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001445 update_xkb_state_from_core(b, key_release->state);
1446 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001447 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001448 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001449 WL_KEYBOARD_KEY_STATE_RELEASED,
1450 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001451 free(prev);
1452 prev = NULL;
1453 break;
1454 default:
1455 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001456 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001457
Bill Spitzakb715cec2012-06-05 17:08:23 -04001458 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001459}
1460
Giulio Camuffo954f1832014-10-11 18:27:30 +03001461#define F(field) offsetof(struct x11_backend, field)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001462
1463static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001464x11_backend_get_resources(struct x11_backend *b)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001465{
1466 static const struct { const char *name; int offset; } atoms[] = {
1467 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1468 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1469 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1470 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001471 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001472 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001473 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001474 { "_NET_WM_STATE", F(atom.net_wm_state) },
1475 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001476 { "_NET_SUPPORTING_WM_CHECK",
1477 F(atom.net_supporting_wm_check) },
1478 { "_NET_SUPPORTED", F(atom.net_supported) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001479 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001480 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001481 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001482 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001483 };
1484
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001485 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001486 xcb_intern_atom_reply_t *reply;
1487 xcb_pixmap_t pixmap;
1488 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001489 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001490 uint8_t data[] = { 0, 0, 0, 0 };
1491
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001492 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001493 cookies[i] = xcb_intern_atom (b->conn, 0,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001494 strlen(atoms[i].name),
1495 atoms[i].name);
1496
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001497 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001498 reply = xcb_intern_atom_reply (b->conn, cookies[i], NULL);
1499 *(xcb_atom_t *) ((char *) b + atoms[i].offset) = reply->atom;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001500 free(reply);
1501 }
1502
Giulio Camuffo954f1832014-10-11 18:27:30 +03001503 pixmap = xcb_generate_id(b->conn);
1504 gc = xcb_generate_id(b->conn);
1505 xcb_create_pixmap(b->conn, 1, pixmap, b->screen->root, 1, 1);
1506 xcb_create_gc(b->conn, gc, pixmap, 0, NULL);
1507 xcb_put_image(b->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001508 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001509 b->null_cursor = xcb_generate_id(b->conn);
1510 xcb_create_cursor (b->conn, b->null_cursor,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001511 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001512 xcb_free_gc(b->conn, gc);
1513 xcb_free_pixmap(b->conn, pixmap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001514}
1515
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001516static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001517x11_backend_get_wm_info(struct x11_backend *c)
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001518{
1519 xcb_get_property_cookie_t cookie;
1520 xcb_get_property_reply_t *reply;
1521 xcb_atom_t *atom;
1522 unsigned int i;
1523
1524 cookie = xcb_get_property(c->conn, 0, c->screen->root,
1525 c->atom.net_supported,
1526 XCB_ATOM_ATOM, 0, 1024);
1527 reply = xcb_get_property_reply(c->conn, cookie, NULL);
1528 if (reply == NULL)
1529 return;
1530
1531 atom = (xcb_atom_t *) xcb_get_property_value(reply);
1532
1533 for (i = 0; i < reply->value_len; i++) {
1534 if (atom[i] == c->atom.net_wm_state_fullscreen)
1535 c->has_net_wm_state_fullscreen = 1;
1536 }
Ryo Munakata76fb7ec2015-03-08 19:17:06 +09001537
1538 free(reply);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001539}
1540
1541static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001542x11_restore(struct weston_compositor *ec)
1543{
1544}
1545
1546static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001547x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001548{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001549 struct x11_backend *backend = (struct x11_backend *)ec->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -07001550
Giulio Camuffo954f1832014-10-11 18:27:30 +03001551 wl_event_source_remove(backend->xcb_source);
1552 x11_input_destroy(backend);
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001553
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03001554 weston_compositor_shutdown(ec); /* destroys outputs, too */
1555
Giulio Camuffo954f1832014-10-11 18:27:30 +03001556 XCloseDisplay(backend->dpy);
1557 free(backend);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001558}
1559
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001560static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001561init_gl_renderer(struct x11_backend *b)
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001562{
1563 int ret;
1564
1565 gl_renderer = weston_load_module("gl-renderer.so",
1566 "gl_renderer_interface");
1567 if (!gl_renderer)
1568 return -1;
1569
Giulio Camuffo954f1832014-10-11 18:27:30 +03001570 ret = gl_renderer->create(b->compositor, EGL_PLATFORM_X11_KHR, (void *) b->dpy,
Derek Foremane76f1852015-05-15 12:12:39 -05001571 gl_renderer->opaque_attribs, NULL, 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001572
1573 return ret;
1574}
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001575
Giulio Camuffo954f1832014-10-11 18:27:30 +03001576static struct x11_backend *
1577x11_backend_create(struct weston_compositor *compositor,
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001578 struct weston_x11_backend_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001579{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001580 struct x11_backend *b;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001581 struct x11_output *output;
Pekka Paalanen157109b2016-03-24 15:43:44 +02001582 struct wl_event_loop *loop;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001583 int x = 0;
1584 unsigned i;
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001585
Giulio Camuffo954f1832014-10-11 18:27:30 +03001586 b = zalloc(sizeof *b);
1587 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001588 return NULL;
1589
Giulio Camuffo954f1832014-10-11 18:27:30 +03001590 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001591 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001592 goto err_free;
1593
Giulio Camuffo954f1832014-10-11 18:27:30 +03001594 b->dpy = XOpenDisplay(NULL);
1595 if (b->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001596 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001597
Giulio Camuffo954f1832014-10-11 18:27:30 +03001598 b->conn = XGetXCBConnection(b->dpy);
1599 XSetEventQueueOwner(b->dpy, XCBOwnsEventQueue);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001600
Giulio Camuffo954f1832014-10-11 18:27:30 +03001601 if (xcb_connection_has_error(b->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001602 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001603
Marko61b4d3e2015-10-05 17:27:54 -05001604 b->screen = x11_compositor_get_default_screen(b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001605 wl_array_init(&b->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001606
Giulio Camuffo954f1832014-10-11 18:27:30 +03001607 x11_backend_get_resources(b);
1608 x11_backend_get_wm_info(b);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001609
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001610 if (!b->has_net_wm_state_fullscreen && config->fullscreen) {
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001611 weston_log("Can not fullscreen without window manager support"
1612 "(need _NET_WM_STATE_FULLSCREEN)\n");
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001613 config->fullscreen = 0;
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001614 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001615
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001616 b->use_pixman = config->use_pixman;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001617 if (b->use_pixman) {
1618 if (pixman_renderer_init(compositor) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001619 weston_log("Failed to initialize pixman renderer for X11 backend\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001620 goto err_xdisplay;
Bryce Harrington665b0252015-05-26 18:14:21 -07001621 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001622 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001623 else if (init_gl_renderer(b) < 0) {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001624 goto err_xdisplay;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001625 }
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001626 weston_log("Using %s renderer\n", config->use_pixman ? "pixman" : "gl");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001627
Giulio Camuffo954f1832014-10-11 18:27:30 +03001628 b->base.destroy = x11_destroy;
1629 b->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001630
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001631 if (x11_input_create(b, config->no_input) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001632 weston_log("Failed to create X11 input\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001633 goto err_renderer;
Bryce Harrington665b0252015-05-26 18:14:21 -07001634 }
Daniel Stone22815f92012-06-22 13:21:34 +01001635
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001636 for (i = 0; i < config->num_outputs; ++i) {
1637 struct weston_x11_backend_output_config *output_iterator =
1638 &config->outputs[i];
Scott Moreau248aaec2012-08-03 14:19:52 -06001639
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001640 if (output_iterator->name == NULL) {
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001641 continue;
Rob Bradford5c4245d2013-07-26 16:29:44 +01001642 }
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001643
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001644 if (output_iterator->width < 1) {
1645 weston_log("Invalid width \"%d\" for output %s\n",
1646 output_iterator->width, output_iterator->name);
1647 goto err_x11_input;
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001648 }
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001649
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001650 if (output_iterator->height < 1) {
1651 weston_log("Invalid height \"%d\" for output %s\n",
1652 output_iterator->height, output_iterator->name);
1653 goto err_x11_input;
1654 }
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001655
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001656 output = x11_backend_create_output(b,
1657 x,
1658 0,
1659 output_iterator->width,
1660 output_iterator->height,
1661 config->fullscreen,
1662 config->no_input,
1663 output_iterator->name,
1664 output_iterator->transform,
1665 output_iterator->scale);
Bryce Harrington665b0252015-05-26 18:14:21 -07001666 if (output == NULL) {
1667 weston_log("Failed to create configured x11 output\n");
Scott Moreau248aaec2012-08-03 14:19:52 -06001668 goto err_x11_input;
Bryce Harrington665b0252015-05-26 18:14:21 -07001669 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06001670
1671 x = pixman_region32_extents(&output->base.region)->x2;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001672 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001673
Pekka Paalanen157109b2016-03-24 15:43:44 +02001674 loop = wl_display_get_event_loop(compositor->wl_display);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001675 b->xcb_source =
Pekka Paalanen157109b2016-03-24 15:43:44 +02001676 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001677 xcb_get_file_descriptor(b->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001678 WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001679 x11_backend_handle_event, b);
1680 wl_event_source_check(b->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001681
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001682 if (compositor->renderer->import_dmabuf) {
1683 if (linux_dmabuf_setup(compositor) < 0)
1684 weston_log("Error: initializing dmabuf "
1685 "support failed.\n");
1686 }
1687
Giulio Camuffo954f1832014-10-11 18:27:30 +03001688 compositor->backend = &b->base;
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001689
Giulio Camuffo954f1832014-10-11 18:27:30 +03001690 return b;
Martin Olsson11434bb2012-07-08 03:03:44 +02001691
1692err_x11_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001693 x11_input_destroy(b);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001694err_renderer:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001695 compositor->renderer->destroy(compositor);
Martin Olsson11434bb2012-07-08 03:03:44 +02001696err_xdisplay:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001697 XCloseDisplay(b->dpy);
Martin Olsson11434bb2012-07-08 03:03:44 +02001698err_free:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001699 free(b);
Martin Olsson11434bb2012-07-08 03:03:44 +02001700 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001701}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001702
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001703static void
1704config_init_to_defaults(struct weston_x11_backend_config *config)
1705{
1706}
1707
Giulio Camuffo954f1832014-10-11 18:27:30 +03001708WL_EXPORT int
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001709backend_init(struct weston_compositor *compositor,
Giulio Camuffo93daabb2015-10-17 19:24:14 +03001710 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001711{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001712 struct x11_backend *b;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001713 struct weston_x11_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001714
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001715 if (config_base == NULL ||
1716 config_base->struct_version != WESTON_X11_BACKEND_CONFIG_VERSION ||
1717 config_base->struct_size > sizeof(struct weston_x11_backend_config)) {
1718 weston_log("X11 backend config structure is invalid\n");
1719 return -1;
1720 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001721
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001722 config_init_to_defaults(&config);
1723 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001724
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001725 b = x11_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001726 if (b == NULL)
1727 return -1;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001728
Giulio Camuffo954f1832014-10-11 18:27:30 +03001729 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001730}