blob: 9e2dc37287ba3fb1744276f58965dc7586afc131 [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>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030032#include <stdint.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040033#include <stdlib.h>
34#include <string.h>
35#include <fcntl.h>
36#include <unistd.h>
37#include <errno.h>
38#include <sys/time.h>
Vasily Khoruzhickb3add192013-01-07 20:39:50 +030039#include <sys/shm.h>
Kristian Høgsbergf9112b22010-06-14 12:53:43 -040040#include <linux/input.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040041
42#include <xcb/xcb.h>
Vasily Khoruzhickb3add192013-01-07 20:39:50 +030043#include <xcb/shm.h>
Daniel Stone62b33b62012-06-22 13:21:35 +010044#ifdef HAVE_XCB_XKB
45#include <xcb/xkb.h>
46#endif
47
Benjamin Franzke3b288af2011-02-20 19:58:42 +010048#include <X11/Xlib.h>
49#include <X11/Xlib-xcb.h>
50
Daniel Stone576cd152012-06-01 12:14:02 +010051#include <xkbcommon/xkbcommon.h>
52
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040053#include "compositor.h"
Benoit Gschwinde16acab2016-04-15 20:28:31 -070054#include "compositor-x11.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"
Benoit Gschwinde16acab2016-04-15 20:28:31 -070058#include "gl-renderer.h"
59#include "pixman-renderer.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020060#include "presentation-time-server-protocol.h"
Pekka Paalanen5ffb4402014-06-12 16:52:53 +030061#include "linux-dmabuf.h"
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040062
Giulio Camuffo90a6fc62016-03-22 17:44:54 +020063#define DEFAULT_AXIS_STEP_DISTANCE 10
Jonas Ådahl62efe202012-09-27 18:40:41 +020064
Giulio Camuffo954f1832014-10-11 18:27:30 +030065struct x11_backend {
66 struct weston_backend base;
67 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040068
Benjamin Franzke3b288af2011-02-20 19:58:42 +010069 Display *dpy;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040070 xcb_connection_t *conn;
71 xcb_screen_t *screen;
72 xcb_cursor_t null_cursor;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050073 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040074 struct wl_event_source *xcb_source;
Daniel Stone576cd152012-06-01 12:14:02 +010075 struct xkb_keymap *xkb_keymap;
Daniel Stone62b33b62012-06-22 13:21:35 +010076 unsigned int has_xkb;
77 uint8_t xkb_event_base;
Vasily Khoruzhickd8943512013-01-07 22:36:02 +030078 int use_pixman;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040079
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -050080 int has_net_wm_state_fullscreen;
81
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040082 /* We could map multi-pointer X to multiple wayland seats, but
83 * for now we only support core X input. */
Kristian Høgsberg068b61c2013-02-25 17:04:47 -050084 struct weston_seat core_seat;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +020085 double prev_x;
86 double prev_y;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040087
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040088 struct {
89 xcb_atom_t wm_protocols;
90 xcb_atom_t wm_normal_hints;
91 xcb_atom_t wm_size_hints;
92 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050093 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040094 xcb_atom_t net_wm_name;
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -050095 xcb_atom_t net_supporting_wm_check;
96 xcb_atom_t net_supported;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050097 xcb_atom_t net_wm_icon;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -040098 xcb_atom_t net_wm_state;
99 xcb_atom_t net_wm_state_fullscreen;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500100 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400101 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500102 xcb_atom_t cardinal;
Daniel Stone855028f2012-05-01 20:37:10 +0100103 xcb_atom_t xkb_names;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400104 } atom;
105};
106
107struct x11_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500108 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400109
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400110 xcb_window_t window;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500111 struct weston_mode mode;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400112 struct wl_event_source *finish_frame_timer;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300113
114 xcb_gc_t gc;
115 xcb_shm_seg_t segment;
116 pixman_image_t *hw_surface;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300117 int shm_id;
118 void *buf;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300119 uint8_t depth;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200120 int32_t scale;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400121};
122
Derek Foremand540f4b2015-01-27 16:26:49 -0600123struct window_delete_data {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300124 struct x11_backend *backend;
Derek Foremand540f4b2015-01-27 16:26:49 -0600125 xcb_window_t window;
126};
127
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300128struct gl_renderer_interface *gl_renderer;
129
Marko61b4d3e2015-10-05 17:27:54 -0500130static xcb_screen_t *
131x11_compositor_get_default_screen(struct x11_backend *b)
132{
133 xcb_screen_iterator_t iter;
134 int i, screen_nbr = XDefaultScreen(b->dpy);
135
136 iter = xcb_setup_roots_iterator(xcb_get_setup(b->conn));
137 for (i = 0; iter.rem; xcb_screen_next(&iter), i++)
138 if (i == screen_nbr)
139 return iter.data;
140
141 return xcb_setup_roots_iterator(xcb_get_setup(b->conn)).data;
142}
143
Daniel Stone576cd152012-06-01 12:14:02 +0100144static struct xkb_keymap *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300145x11_backend_get_keymap(struct x11_backend *b)
Daniel Stone576cd152012-06-01 12:14:02 +0100146{
147 xcb_get_property_cookie_t cookie;
148 xcb_get_property_reply_t *reply;
Daniel Stone576cd152012-06-01 12:14:02 +0100149 struct xkb_rule_names names;
150 struct xkb_keymap *ret;
151 const char *value_all, *value_part;
152 int length_all, length_part;
153
154 memset(&names, 0, sizeof(names));
155
Giulio Camuffo954f1832014-10-11 18:27:30 +0300156 cookie = xcb_get_property(b->conn, 0, b->screen->root,
157 b->atom.xkb_names, b->atom.string, 0, 1024);
158 reply = xcb_get_property_reply(b->conn, cookie, NULL);
Daniel Stone576cd152012-06-01 12:14:02 +0100159 if (reply == NULL)
160 return NULL;
161
162 value_all = xcb_get_property_value(reply);
163 length_all = xcb_get_property_value_length(reply);
164 value_part = value_all;
165
166#define copy_prop_value(to) \
167 length_part = strlen(value_part); \
168 if (value_part + length_part < (value_all + length_all) && \
169 length_part > 0) \
170 names.to = value_part; \
171 value_part += length_part + 1;
172
173 copy_prop_value(rules);
174 copy_prop_value(model);
175 copy_prop_value(layout);
176 copy_prop_value(variant);
177 copy_prop_value(options);
178#undef copy_prop_value
179
Giulio Camuffo954f1832014-10-11 18:27:30 +0300180 ret = xkb_keymap_new_from_names(b->compositor->xkb_context, &names, 0);
Daniel Stone576cd152012-06-01 12:14:02 +0100181
182 free(reply);
183 return ret;
184}
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400185
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400186static uint32_t
Giulio Camuffo954f1832014-10-11 18:27:30 +0300187get_xkb_mod_mask(struct x11_backend *b, uint32_t in)
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400188{
Derek Foreman1281a362015-07-31 16:55:32 -0500189 struct weston_keyboard *keyboard =
190 weston_seat_get_keyboard(&b->core_seat);
191 struct weston_xkb_info *info = keyboard->xkb_info;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400192 uint32_t ret = 0;
193
194 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
195 ret |= (1 << info->shift_mod);
196 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
197 ret |= (1 << info->caps_mod);
198 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
199 ret |= (1 << info->ctrl_mod);
200 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
201 ret |= (1 << info->alt_mod);
202 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
203 ret |= (1 << info->mod2_mod);
204 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
205 ret |= (1 << info->mod3_mod);
206 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
207 ret |= (1 << info->super_mod);
208 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
209 ret |= (1 << info->mod5_mod);
210
211 return ret;
212}
213
Daniel Stone62b33b62012-06-22 13:21:35 +0100214static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300215x11_backend_setup_xkb(struct x11_backend *b)
Daniel Stone62b33b62012-06-22 13:21:35 +0100216{
217#ifndef HAVE_XCB_XKB
218 weston_log("XCB-XKB not available during build\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300219 b->has_xkb = 0;
220 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100221 return;
222#else
Derek Foreman1281a362015-07-31 16:55:32 -0500223 struct weston_keyboard *keyboard;
Daniel Stone62b33b62012-06-22 13:21:35 +0100224 const xcb_query_extension_reply_t *ext;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100225 xcb_generic_error_t *error;
Daniel Stonee2faa122012-06-22 13:21:39 +0100226 xcb_void_cookie_t select;
Ran Benita6a39d872012-10-31 20:14:57 +0200227 xcb_xkb_use_extension_cookie_t use_ext;
228 xcb_xkb_use_extension_reply_t *use_ext_reply;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100229 xcb_xkb_per_client_flags_cookie_t pcf;
230 xcb_xkb_per_client_flags_reply_t *pcf_reply;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400231 xcb_xkb_get_state_cookie_t state;
232 xcb_xkb_get_state_reply_t *state_reply;
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200233 uint32_t values[1] = { XCB_EVENT_MASK_PROPERTY_CHANGE };
Daniel Stone62b33b62012-06-22 13:21:35 +0100234
Giulio Camuffo954f1832014-10-11 18:27:30 +0300235 b->has_xkb = 0;
236 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100237
Giulio Camuffo954f1832014-10-11 18:27:30 +0300238 ext = xcb_get_extension_data(b->conn, &xcb_xkb_id);
Daniel Stone62b33b62012-06-22 13:21:35 +0100239 if (!ext) {
240 weston_log("XKB extension not available on host X11 server\n");
241 return;
242 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300243 b->xkb_event_base = ext->first_event;
Daniel Stone62b33b62012-06-22 13:21:35 +0100244
Giulio Camuffo954f1832014-10-11 18:27:30 +0300245 select = xcb_xkb_select_events_checked(b->conn,
Ran Benita424ae012012-10-31 00:13:08 +0200246 XCB_XKB_ID_USE_CORE_KBD,
247 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
248 0,
249 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
250 0,
251 0,
252 NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300253 error = xcb_request_check(b->conn, select);
Daniel Stonee2faa122012-06-22 13:21:39 +0100254 if (error) {
255 weston_log("error: failed to select for XKB state events\n");
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200256 free(error);
Daniel Stonee2faa122012-06-22 13:21:39 +0100257 return;
258 }
259
Giulio Camuffo954f1832014-10-11 18:27:30 +0300260 use_ext = xcb_xkb_use_extension(b->conn,
Ran Benita6a39d872012-10-31 20:14:57 +0200261 XCB_XKB_MAJOR_VERSION,
262 XCB_XKB_MINOR_VERSION);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300263 use_ext_reply = xcb_xkb_use_extension_reply(b->conn, use_ext, NULL);
Ran Benita6a39d872012-10-31 20:14:57 +0200264 if (!use_ext_reply) {
265 weston_log("couldn't start using XKB extension\n");
266 return;
267 }
268
269 if (!use_ext_reply->supported) {
270 weston_log("XKB extension version on the server is too old "
271 "(want %d.%d, has %d.%d)\n",
272 XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION,
273 use_ext_reply->serverMajor, use_ext_reply->serverMinor);
274 free(use_ext_reply);
275 return;
276 }
277 free(use_ext_reply);
278
Giulio Camuffo954f1832014-10-11 18:27:30 +0300279 pcf = xcb_xkb_per_client_flags(b->conn,
Daniel Stone3ee91e12012-06-22 13:21:36 +0100280 XCB_XKB_ID_USE_CORE_KBD,
281 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
282 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
283 0,
284 0,
285 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300286 pcf_reply = xcb_xkb_per_client_flags_reply(b->conn, pcf, NULL);
Ran Benita7109cc82012-10-31 20:14:58 +0200287 if (!pcf_reply ||
288 !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT)) {
Daniel Stone3ee91e12012-06-22 13:21:36 +0100289 weston_log("failed to set XKB per-client flags, not using "
290 "detectable repeat\n");
Ran Benita7109cc82012-10-31 20:14:58 +0200291 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100292 return;
293 }
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200294 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100295
Giulio Camuffo954f1832014-10-11 18:27:30 +0300296 state = xcb_xkb_get_state(b->conn, XCB_XKB_ID_USE_CORE_KBD);
297 state_reply = xcb_xkb_get_state_reply(b->conn, state, NULL);
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200298 if (!state_reply) {
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400299 weston_log("failed to get initial XKB state\n");
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400300 return;
301 }
302
Derek Foreman1281a362015-07-31 16:55:32 -0500303 keyboard = weston_seat_get_keyboard(&b->core_seat);
304 xkb_state_update_mask(keyboard->xkb_state.state,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300305 get_xkb_mod_mask(b, state_reply->baseMods),
306 get_xkb_mod_mask(b, state_reply->latchedMods),
307 get_xkb_mod_mask(b, state_reply->lockedMods),
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400308 0,
309 0,
310 state_reply->group);
311
312 free(state_reply);
313
Giulio Camuffo954f1832014-10-11 18:27:30 +0300314 xcb_change_window_attributes(b->conn, b->screen->root,
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200315 XCB_CW_EVENT_MASK, values);
316
Giulio Camuffo954f1832014-10-11 18:27:30 +0300317 b->has_xkb = 1;
Daniel Stone62b33b62012-06-22 13:21:35 +0100318#endif
319}
320
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200321#ifdef HAVE_XCB_XKB
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200322static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300323update_xkb_keymap(struct x11_backend *b)
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200324{
325 struct xkb_keymap *keymap;
326
Giulio Camuffo954f1832014-10-11 18:27:30 +0300327 keymap = x11_backend_get_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200328 if (!keymap) {
329 weston_log("failed to get XKB keymap\n");
330 return;
331 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300332 weston_seat_update_keymap(&b->core_seat, keymap);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200333 xkb_keymap_unref(keymap);
334}
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200335#endif
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200336
Darxus55973f22010-11-22 21:24:39 -0500337static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300338x11_input_create(struct x11_backend *b, int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400339{
Daniel Stone576cd152012-06-01 12:14:02 +0100340 struct xkb_keymap *keymap;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400341
Giulio Camuffo954f1832014-10-11 18:27:30 +0300342 weston_seat_init(&b->core_seat, b->compositor, "default");
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300343
344 if (no_input)
345 return 0;
346
Giulio Camuffo954f1832014-10-11 18:27:30 +0300347 weston_seat_init_pointer(&b->core_seat);
Daniel Stone576cd152012-06-01 12:14:02 +0100348
Giulio Camuffo954f1832014-10-11 18:27:30 +0300349 keymap = x11_backend_get_keymap(b);
350 if (weston_seat_init_keyboard(&b->core_seat, keymap) < 0)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500351 return -1;
Ran Benitac9c74152014-08-19 23:59:52 +0300352 xkb_keymap_unref(keymap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400353
Giulio Camuffo954f1832014-10-11 18:27:30 +0300354 x11_backend_setup_xkb(b);
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400355
Darxus55973f22010-11-22 21:24:39 -0500356 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400357}
358
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200359static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300360x11_input_destroy(struct x11_backend *b)
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200361{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300362 weston_seat_release(&b->core_seat);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200363}
364
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500365static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200366x11_output_start_repaint_loop(struct weston_output *output)
367{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400368 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200369
Pekka Paalanen662f3842015-03-18 12:17:26 +0200370 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200371 weston_output_finish_frame(output, &ts, WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200372}
373
David Herrmann1edf44c2013-10-22 17:11:26 +0200374static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300375x11_output_repaint_gl(struct weston_output *output_base,
376 pixman_region32_t *damage)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500377{
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500378 struct x11_output *output = (struct x11_output *)output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400379 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500380
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400381 ec->renderer->repaint_output(output_base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500382
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200383 pixman_region32_subtract(&ec->primary_plane.damage,
384 &ec->primary_plane.damage, damage);
385
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500386 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200387 return 0;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500388}
389
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300390static void
Alexander Larsson80f91632013-05-22 14:41:38 +0200391set_clip_for_output(struct weston_output *output_base, pixman_region32_t *region)
392{
393 struct x11_output *output = (struct x11_output *)output_base;
394 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300395 struct x11_backend *b = (struct x11_backend *)ec->backend;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500396 pixman_region32_t transformed_region;
Alexander Larsson80f91632013-05-22 14:41:38 +0200397 pixman_box32_t *rects;
398 xcb_rectangle_t *output_rects;
Alexander Larsson80f91632013-05-22 14:41:38 +0200399 xcb_void_cookie_t cookie;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500400 int nrects, i;
Alexander Larsson80f91632013-05-22 14:41:38 +0200401 xcb_generic_error_t *err;
402
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500403 pixman_region32_init(&transformed_region);
404 pixman_region32_copy(&transformed_region, region);
405 pixman_region32_translate(&transformed_region,
406 -output_base->x, -output_base->y);
407 weston_transformed_region(output_base->width, output_base->height,
408 output_base->transform,
409 output_base->current_scale,
410 &transformed_region, &transformed_region);
411
412 rects = pixman_region32_rectangles(&transformed_region, &nrects);
Alexander Larsson80f91632013-05-22 14:41:38 +0200413 output_rects = calloc(nrects, sizeof(xcb_rectangle_t));
414
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500415 if (output_rects == NULL) {
416 pixman_region32_fini(&transformed_region);
Alexander Larsson80f91632013-05-22 14:41:38 +0200417 return;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500418 }
Alexander Larsson80f91632013-05-22 14:41:38 +0200419
420 for (i = 0; i < nrects; i++) {
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500421 output_rects[i].x = rects[i].x1;
422 output_rects[i].y = rects[i].y1;
423 output_rects[i].width = rects[i].x2 - rects[i].x1;
424 output_rects[i].height = rects[i].y2 - rects[i].y1;
Alexander Larsson80f91632013-05-22 14:41:38 +0200425 }
426
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500427 pixman_region32_fini(&transformed_region);
428
Giulio Camuffo954f1832014-10-11 18:27:30 +0300429 cookie = xcb_set_clip_rectangles_checked(b->conn, XCB_CLIP_ORDERING_UNSORTED,
Alexander Larsson80f91632013-05-22 14:41:38 +0200430 output->gc,
431 0, 0, nrects,
432 output_rects);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300433 err = xcb_request_check(b->conn, cookie);
Alexander Larsson80f91632013-05-22 14:41:38 +0200434 if (err != NULL) {
435 weston_log("Failed to set clip rects, err: %d\n", err->error_code);
436 free(err);
437 }
438 free(output_rects);
439}
440
441
David Herrmann1edf44c2013-10-22 17:11:26 +0200442static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300443x11_output_repaint_shm(struct weston_output *output_base,
444 pixman_region32_t *damage)
445{
446 struct x11_output *output = (struct x11_output *)output_base;
447 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300448 struct x11_backend *b = (struct x11_backend *)ec->backend;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300449 xcb_void_cookie_t cookie;
450 xcb_generic_error_t *err;
451
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200452 pixman_renderer_output_set_buffer(output_base, output->hw_surface);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300453 ec->renderer->repaint_output(output_base, damage);
454
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300455 pixman_region32_subtract(&ec->primary_plane.damage,
456 &ec->primary_plane.damage, damage);
Alexander Larsson80f91632013-05-22 14:41:38 +0200457 set_clip_for_output(output_base, damage);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300458 cookie = xcb_shm_put_image_checked(b->conn, output->window, output->gc,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300459 pixman_image_get_width(output->hw_surface),
460 pixman_image_get_height(output->hw_surface),
461 0, 0,
462 pixman_image_get_width(output->hw_surface),
463 pixman_image_get_height(output->hw_surface),
464 0, 0, output->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
465 0, output->segment, 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300466 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300467 if (err != NULL) {
468 weston_log("Failed to put shm image, err: %d\n", err->error_code);
469 free(err);
470 }
471
472 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200473 return 0;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300474}
475
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100476static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400477finish_frame_handler(void *data)
478{
479 struct x11_output *output = data;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200480 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200481
Pekka Paalanen662f3842015-03-18 12:17:26 +0200482 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200483 weston_output_finish_frame(&output->base, &ts, 0);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400484
485 return 1;
486}
487
Matt Roper361d2ad2011-08-29 13:52:23 -0700488static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300489x11_output_deinit_shm(struct x11_backend *b, struct x11_output *output)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300490{
491 xcb_void_cookie_t cookie;
492 xcb_generic_error_t *err;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300493 xcb_free_gc(b->conn, output->gc);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300494
495 pixman_image_unref(output->hw_surface);
496 output->hw_surface = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300497 cookie = xcb_shm_detach_checked(b->conn, output->segment);
498 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300499 if (err) {
500 weston_log("xcb_shm_detach failed, error %d\n", err->error_code);
501 free(err);
502 }
503 shmdt(output->buf);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300504}
505
506static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500507x11_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700508{
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200509 struct x11_output *output = (struct x11_output *)output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300510 struct x11_backend *backend =
511 (struct x11_backend *)output->base.compositor->backend;
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200512
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200513 wl_event_source_remove(output->finish_frame_timer);
514
Giulio Camuffo954f1832014-10-11 18:27:30 +0300515 if (backend->use_pixman) {
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300516 pixman_renderer_output_destroy(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300517 x11_output_deinit_shm(backend, output);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300518 } else
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300519 gl_renderer->output_destroy(output_base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200520
Giulio Camuffo954f1832014-10-11 18:27:30 +0300521 xcb_destroy_window(backend->conn, output->window);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200522
Armin Krezovićad276932016-06-30 06:29:00 +0200523 xcb_flush(backend->conn);
524
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500525 weston_output_destroy(&output->base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200526
527 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700528}
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200529
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400530static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300531x11_output_set_wm_protocols(struct x11_backend *b,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500532 struct x11_output *output)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400533{
534 xcb_atom_t list[1];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400535
Giulio Camuffo954f1832014-10-11 18:27:30 +0300536 list[0] = b->atom.wm_delete_window;
537 xcb_change_property (b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400538 XCB_PROP_MODE_REPLACE,
539 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300540 b->atom.wm_protocols,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400541 XCB_ATOM_ATOM,
542 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500543 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400544 list);
545}
546
547struct wm_normal_hints {
548 uint32_t flags;
549 uint32_t pad[4];
550 int32_t min_width, min_height;
551 int32_t max_width, max_height;
552 int32_t width_inc, height_inc;
553 int32_t min_aspect_x, min_aspect_y;
554 int32_t max_aspect_x, max_aspect_y;
555 int32_t base_width, base_height;
556 int32_t win_gravity;
557};
558
559#define WM_NORMAL_HINTS_MIN_SIZE 16
560#define WM_NORMAL_HINTS_MAX_SIZE 32
561
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500562static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300563x11_output_set_icon(struct x11_backend *b,
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400564 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500565{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400566 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400567 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400568 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500569
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400570 image = load_image(filename);
571 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500572 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400573 width = pixman_image_get_width(image);
574 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500575 icon = malloc(width * height * 4 + 8);
576 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400577 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500578 return;
579 }
580
581 icon[0] = width;
582 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400583 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300584 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
585 b->atom.net_wm_icon, b->atom.cardinal, 32,
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500586 width * height + 2, icon);
587 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400588 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500589}
590
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500591static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300592x11_output_wait_for_map(struct x11_backend *b, struct x11_output *output)
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500593{
594 xcb_map_notify_event_t *map_notify;
595 xcb_configure_notify_event_t *configure_notify;
596 xcb_generic_event_t *event;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500597 int mapped = 0, configured = 0;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500598 uint8_t response_type;
599
600 /* This isn't the nicest way to do this. Ideally, we could
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500601 * just go back to the main loop and once we get the configure
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500602 * notify, we add the output to the compositor. While we do
603 * support output hotplug, we can't start up with no outputs.
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500604 * We could add the output and then resize once we get the
605 * configure notify, but we don't want to start up and
606 * immediately resize the output.
607 *
608 * Also, some window managers don't give us our final
609 * fullscreen size before map_notify, so if we don't get a
610 * configure_notify before map_notify, we just wait for the
611 * first one and hope that's our size. */
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500612
Giulio Camuffo954f1832014-10-11 18:27:30 +0300613 xcb_flush(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500614
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500615 while (!mapped || !configured) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300616 event = xcb_wait_for_event(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500617 response_type = event->response_type & ~0x80;
618
619 switch (response_type) {
620 case XCB_MAP_NOTIFY:
621 map_notify = (xcb_map_notify_event_t *) event;
622 if (map_notify->window == output->window)
623 mapped = 1;
624 break;
625
626 case XCB_CONFIGURE_NOTIFY:
627 configure_notify =
628 (xcb_configure_notify_event_t *) event;
629
Alexander Larsson4ea95522013-05-22 14:41:37 +0200630
631 if (configure_notify->width % output->scale != 0 ||
632 configure_notify->height % output->scale != 0)
633 weston_log("Resolution is not a multiple of screen size, rounding\n");
Jason Ekstrandd89a0942014-03-07 15:29:14 -0600634 output->mode.width = configure_notify->width;
635 output->mode.height = configure_notify->height;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500636 configured = 1;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500637 break;
638 }
639 }
640}
641
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300642static xcb_visualtype_t *
643find_visual_by_id(xcb_screen_t *screen,
644 xcb_visualid_t id)
645{
646 xcb_depth_iterator_t i;
647 xcb_visualtype_iterator_t j;
648 for (i = xcb_screen_allowed_depths_iterator(screen);
649 i.rem;
650 xcb_depth_next(&i)) {
651 for (j = xcb_depth_visuals_iterator(i.data);
652 j.rem;
653 xcb_visualtype_next(&j)) {
654 if (j.data->visual_id == id)
655 return j.data;
656 }
657 }
658 return 0;
659}
660
661static uint8_t
662get_depth_of_visual(xcb_screen_t *screen,
663 xcb_visualid_t id)
664{
665 xcb_depth_iterator_t i;
666 xcb_visualtype_iterator_t j;
667 for (i = xcb_screen_allowed_depths_iterator(screen);
668 i.rem;
669 xcb_depth_next(&i)) {
670 for (j = xcb_depth_visuals_iterator(i.data);
671 j.rem;
672 xcb_visualtype_next(&j)) {
673 if (j.data->visual_id == id)
674 return i.data->depth;
675 }
676 }
677 return 0;
678}
679
680static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300681x11_output_init_shm(struct x11_backend *b, struct x11_output *output,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300682 int width, int height)
683{
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300684 xcb_visualtype_t *visual_type;
Marko61b4d3e2015-10-05 17:27:54 -0500685 xcb_screen_t *screen;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300686 xcb_format_iterator_t fmt;
687 xcb_void_cookie_t cookie;
688 xcb_generic_error_t *err;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300689 const xcb_query_extension_reply_t *ext;
690 int bitsperpixel = 0;
691 pixman_format_code_t pixman_format;
692
693 /* Check if SHM is available */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300694 ext = xcb_get_extension_data(b->conn, &xcb_shm_id);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300695 if (ext == NULL || !ext->present) {
696 /* SHM is missing */
697 weston_log("SHM extension is not available\n");
698 errno = ENOENT;
699 return -1;
700 }
701
Marko61b4d3e2015-10-05 17:27:54 -0500702 screen = x11_compositor_get_default_screen(b);
703 visual_type = find_visual_by_id(screen, screen->root_visual);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300704 if (!visual_type) {
705 weston_log("Failed to lookup visual for root window\n");
706 errno = ENOENT;
707 return -1;
708 }
709 weston_log("Found visual, bits per value: %d, red_mask: %.8x, green_mask: %.8x, blue_mask: %.8x\n",
710 visual_type->bits_per_rgb_value,
711 visual_type->red_mask,
712 visual_type->green_mask,
713 visual_type->blue_mask);
Marko61b4d3e2015-10-05 17:27:54 -0500714 output->depth = get_depth_of_visual(screen, screen->root_visual);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300715 weston_log("Visual depth is %d\n", output->depth);
716
Giulio Camuffo954f1832014-10-11 18:27:30 +0300717 for (fmt = xcb_setup_pixmap_formats_iterator(xcb_get_setup(b->conn));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300718 fmt.rem;
719 xcb_format_next(&fmt)) {
720 if (fmt.data->depth == output->depth) {
721 bitsperpixel = fmt.data->bits_per_pixel;
722 break;
723 }
724 }
725 weston_log("Found format for depth %d, bpp: %d\n",
726 output->depth, bitsperpixel);
727
728 if (bitsperpixel == 32 &&
729 visual_type->red_mask == 0xff0000 &&
730 visual_type->green_mask == 0x00ff00 &&
731 visual_type->blue_mask == 0x0000ff) {
732 weston_log("Will use x8r8g8b8 format for SHM surfaces\n");
733 pixman_format = PIXMAN_x8r8g8b8;
MoD1b55a592013-11-28 05:24:21 +0000734 } else if (bitsperpixel == 16 &&
735 visual_type->red_mask == 0x00f800 &&
736 visual_type->green_mask == 0x0007e0 &&
737 visual_type->blue_mask == 0x00001f) {
738 weston_log("Will use r5g6b5 format for SHM surfaces\n");
739 pixman_format = PIXMAN_r5g6b5;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300740 } else {
741 weston_log("Can't find appropriate format for SHM pixmap\n");
742 errno = ENOTSUP;
743 return -1;
744 }
745
746
747 /* Create SHM segment and attach it */
748 output->shm_id = shmget(IPC_PRIVATE, width * height * (bitsperpixel / 8), IPC_CREAT | S_IRWXU);
749 if (output->shm_id == -1) {
750 weston_log("x11shm: failed to allocate SHM segment\n");
751 return -1;
752 }
753 output->buf = shmat(output->shm_id, NULL, 0 /* read/write */);
754 if (-1 == (long)output->buf) {
755 weston_log("x11shm: failed to attach SHM segment\n");
756 return -1;
757 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300758 output->segment = xcb_generate_id(b->conn);
759 cookie = xcb_shm_attach_checked(b->conn, output->segment, output->shm_id, 1);
760 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300761 if (err) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700762 weston_log("x11shm: xcb_shm_attach error %d, op code %d, resource id %d\n",
763 err->error_code, err->major_code, err->minor_code);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300764 free(err);
765 return -1;
766 }
767
768 shmctl(output->shm_id, IPC_RMID, NULL);
769
770 /* Now create pixman image */
771 output->hw_surface = pixman_image_create_bits(pixman_format, width, height, output->buf,
772 width * (bitsperpixel / 8));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300773
Giulio Camuffo954f1832014-10-11 18:27:30 +0300774 output->gc = xcb_generate_id(b->conn);
775 xcb_create_gc(b->conn, output->gc, output->window, 0, NULL);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300776
777 return 0;
778}
779
Scott Moreau1bad5db2012-08-18 01:04:05 -0600780static struct x11_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300781x11_backend_create_output(struct x11_backend *b, int x, int y,
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300782 int width, int height, int fullscreen,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600783 int no_input, char *configured_name,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200784 uint32_t transform, int32_t scale)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400785{
Scott Moreau1bad5db2012-08-18 01:04:05 -0600786 static const char name[] = "Weston Compositor";
Kristian Høgsberg86000402012-01-03 23:24:14 -0500787 static const char class[] = "weston-1\0Weston Compositor";
Benoit Gschwind5c2f20e2016-06-05 19:01:11 +0200788 char *title = NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400789 struct x11_output *output;
Marko61b4d3e2015-10-05 17:27:54 -0500790 xcb_screen_t *screen;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400791 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400792 struct wl_event_loop *loop;
Kristian Høgsberg8404acb2014-03-18 12:15:42 -0700793 int output_width, output_height, width_mm, height_mm;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300794 int ret;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400795 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500796 xcb_atom_t atom_list[1];
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300797 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400798 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300799 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400800 0
801 };
802
Alexander Larsson4ea95522013-05-22 14:41:37 +0200803 output_width = width * scale;
804 output_height = height * scale;
805
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300806 if (!no_input)
807 values[0] |=
808 XCB_EVENT_MASK_KEY_PRESS |
809 XCB_EVENT_MASK_KEY_RELEASE |
810 XCB_EVENT_MASK_BUTTON_PRESS |
811 XCB_EVENT_MASK_BUTTON_RELEASE |
812 XCB_EVENT_MASK_POINTER_MOTION |
813 XCB_EVENT_MASK_ENTER_WINDOW |
814 XCB_EVENT_MASK_LEAVE_WINDOW |
815 XCB_EVENT_MASK_KEYMAP_STATE |
816 XCB_EVENT_MASK_FOCUS_CHANGE;
817
Peter Huttererf3d62272013-08-08 11:57:05 +1000818 output = zalloc(sizeof *output);
Bryce Harrington665b0252015-05-26 18:14:21 -0700819 if (output == NULL) {
820 perror("zalloc");
Scott Moreau1bad5db2012-08-18 01:04:05 -0600821 return NULL;
Bryce Harrington665b0252015-05-26 18:14:21 -0700822 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400823
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400824 output->mode.flags =
825 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200826
Alexander Larsson0b135062013-05-28 16:23:36 +0200827 output->mode.width = output_width;
828 output->mode.height = output_height;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400829 output->mode.refresh = 60000;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200830 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400831 wl_list_init(&output->base.mode_list);
832 wl_list_insert(&output->base.mode_list, &output->mode.link);
833
Giulio Camuffo954f1832014-10-11 18:27:30 +0300834 values[1] = b->null_cursor;
835 output->window = xcb_generate_id(b->conn);
Marko61b4d3e2015-10-05 17:27:54 -0500836 screen = x11_compositor_get_default_screen(b);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300837 xcb_create_window(b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400838 XCB_COPY_FROM_PARENT,
839 output->window,
Marko61b4d3e2015-10-05 17:27:54 -0500840 screen->root,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400841 0, 0,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200842 output_width, output_height,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400843 0,
844 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Marko61b4d3e2015-10-05 17:27:54 -0500845 screen->root_visual,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400846 mask, values);
847
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500848 if (fullscreen) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300849 atom_list[0] = b->atom.net_wm_state_fullscreen;
850 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500851 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300852 b->atom.net_wm_state,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500853 XCB_ATOM_ATOM, 32,
854 ARRAY_LENGTH(atom_list), atom_list);
855 } else {
856 /* Don't resize me. */
857 memset(&normal_hints, 0, sizeof normal_hints);
858 normal_hints.flags =
859 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200860 normal_hints.min_width = output_width;
861 normal_hints.min_height = output_height;
862 normal_hints.max_width = output_width;
863 normal_hints.max_height = output_height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300864 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
865 b->atom.wm_normal_hints,
866 b->atom.wm_size_hints, 32,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500867 sizeof normal_hints / 4,
868 (uint8_t *) &normal_hints);
869 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400870
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400871 /* Set window name. Don't bother with non-EWMH WMs. */
Benoit Gschwind5c2f20e2016-06-05 19:01:11 +0200872 if (configured_name) {
873 if (asprintf(&title, "%s - %s", name, configured_name) < 0)
874 title = NULL;
875 } else {
876 title = strdup(name);
877 }
878
879 if (title) {
880 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
881 b->atom.net_wm_name, b->atom.utf8_string, 8,
882 strlen(title), title);
883 free(title);
884 } else {
885 xcb_destroy_window(b->conn, output->window);
886 free(output);
887 return NULL;
888 }
889
Giulio Camuffo954f1832014-10-11 18:27:30 +0300890 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
891 b->atom.wm_class, b->atom.string, 8,
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500892 sizeof class, class);
893
Giulio Camuffo954f1832014-10-11 18:27:30 +0300894 x11_output_set_icon(b, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500895
Giulio Camuffo954f1832014-10-11 18:27:30 +0300896 x11_output_set_wm_protocols(b, output);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500897
Giulio Camuffo954f1832014-10-11 18:27:30 +0300898 xcb_map_window(b->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400899
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -0500900 if (fullscreen)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300901 x11_output_wait_for_map(b, output);
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400902
Jonas Ådahle5a12252013-04-05 23:07:11 +0200903 output->base.start_repaint_loop = x11_output_start_repaint_loop;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300904 if (b->use_pixman)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300905 output->base.repaint = x11_output_repaint_shm;
906 else
907 output->base.repaint = x11_output_repaint_gl;
Matt Roper361d2ad2011-08-29 13:52:23 -0700908 output->base.destroy = x11_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800909 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200910 output->base.set_backlight = NULL;
911 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800912 output->base.switch_mode = NULL;
Hardeningff39efa2013-09-18 23:56:35 +0200913 output->base.current_mode = &output->mode;
Jason Ekstrand22e16b62014-09-15 12:10:27 -0700914 output->base.make = "weston-X11";
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500915 output->base.model = "none";
Ander Conselvan de Oliveirad4ea71e2013-12-18 19:51:40 +0200916
917 if (configured_name)
918 output->base.name = strdup(configured_name);
919
Giulio Camuffo954f1832014-10-11 18:27:30 +0300920 width_mm = width * b->screen->width_in_millimeters /
921 b->screen->width_in_pixels;
922 height_mm = height * b->screen->height_in_millimeters /
923 b->screen->height_in_pixels;
924 weston_output_init(&output->base, b->compositor,
Kristian Høgsberg8404acb2014-03-18 12:15:42 -0700925 x, y, width_mm, height_mm, transform, scale);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100926
Giulio Camuffo954f1832014-10-11 18:27:30 +0300927 if (b->use_pixman) {
928 if (x11_output_init_shm(b, output,
Kristian Høgsbergb5a4f982013-10-23 23:38:50 -0700929 output->mode.width,
Bryce Harrington665b0252015-05-26 18:14:21 -0700930 output->mode.height) < 0) {
931 weston_log("Failed to initialize SHM for the X11 output\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300932 return NULL;
Bryce Harrington665b0252015-05-26 18:14:21 -0700933 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300934 if (pixman_renderer_output_create(&output->base) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700935 weston_log("Failed to create pixman renderer for output\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300936 x11_output_deinit_shm(b, output);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300937 return NULL;
938 }
939 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +0100940 /* eglCreatePlatformWindowSurfaceEXT takes a Window*
941 * but eglCreateWindowSurface takes a Window. */
942 Window xid = (Window) output->window;
943
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300944 ret = gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000945 (EGLNativeWindowType) output->window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100946 &xid,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000947 gl_renderer->opaque_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500948 NULL,
949 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300950 if (ret < 0)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300951 return NULL;
952 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100953
Giulio Camuffo954f1832014-10-11 18:27:30 +0300954 loop = wl_display_get_event_loop(b->compositor->wl_display);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100955 output->finish_frame_timer =
956 wl_event_loop_add_timer(loop, finish_frame_handler, output);
957
Giulio Camuffo954f1832014-10-11 18:27:30 +0300958 weston_compositor_add_output(b->compositor, &output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400959
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400960 weston_log("x11 output %dx%d, window id %d\n",
961 width, height, output->window);
962
Scott Moreau1bad5db2012-08-18 01:04:05 -0600963 return output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400964}
965
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400966static struct x11_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300967x11_backend_find_output(struct x11_backend *b, xcb_window_t window)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400968{
969 struct x11_output *output;
970
Giulio Camuffo954f1832014-10-11 18:27:30 +0300971 wl_list_for_each(output, &b->compositor->output_list, base.link) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400972 if (output->window == window)
973 return output;
974 }
975
Derek Foreman99bfa642014-12-11 15:44:46 -0600976 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400977}
978
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200979static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300980x11_backend_delete_window(struct x11_backend *b, xcb_window_t window)
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200981{
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +0200982 struct x11_output *output;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200983
Giulio Camuffo954f1832014-10-11 18:27:30 +0300984 output = x11_backend_find_output(b, window);
Derek Foreman99bfa642014-12-11 15:44:46 -0600985 if (output)
986 x11_output_destroy(&output->base);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200987
Giulio Camuffo954f1832014-10-11 18:27:30 +0300988 if (wl_list_empty(&b->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +0300989 weston_compositor_exit(b->compositor);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200990}
991
Derek Foremand540f4b2015-01-27 16:26:49 -0600992static void delete_cb(void *data)
993{
994 struct window_delete_data *wd = data;
995
Giulio Camuffo954f1832014-10-11 18:27:30 +0300996 x11_backend_delete_window(wd->backend, wd->window);
Derek Foremand540f4b2015-01-27 16:26:49 -0600997 free(wd);
998}
999
Daniel Stone154c34c2012-06-22 13:21:40 +01001000#ifdef HAVE_XCB_XKB
Daniel Stonee2faa122012-06-22 13:21:39 +01001001static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001002update_xkb_state(struct x11_backend *b, xcb_xkb_state_notify_event_t *state)
Daniel Stonee2faa122012-06-22 13:21:39 +01001003{
Derek Foreman1281a362015-07-31 16:55:32 -05001004 struct weston_keyboard *keyboard =
1005 weston_seat_get_keyboard(&b->core_seat);
1006
1007 xkb_state_update_mask(keyboard->xkb_state.state,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001008 get_xkb_mod_mask(b, state->baseMods),
1009 get_xkb_mod_mask(b, state->latchedMods),
1010 get_xkb_mod_mask(b, state->lockedMods),
Daniel Stonee2faa122012-06-22 13:21:39 +01001011 0,
1012 0,
1013 state->group);
1014
Giulio Camuffo954f1832014-10-11 18:27:30 +03001015 notify_modifiers(&b->core_seat,
1016 wl_display_next_serial(b->compositor->wl_display));
Daniel Stonee2faa122012-06-22 13:21:39 +01001017}
1018#endif
1019
Daniel Stone154c34c2012-06-22 13:21:40 +01001020/**
1021 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
1022 * the best we can do (given that XCB also lacks XI2 support), is to take
1023 * the state from the core key events. Unfortunately that only gives us
1024 * the effective (i.e. union of depressed/latched/locked) state, and we
1025 * need the granularity.
1026 *
1027 * So we still update the state with every key event we see, but also use
1028 * the state field from X11 events as a mask so we don't get any stuck
1029 * modifiers.
1030 */
1031static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001032update_xkb_state_from_core(struct x11_backend *b, uint16_t x11_mask)
Daniel Stone154c34c2012-06-22 13:21:40 +01001033{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001034 uint32_t mask = get_xkb_mod_mask(b, x11_mask);
Derek Foreman1281a362015-07-31 16:55:32 -05001035 struct weston_keyboard *keyboard
1036 = weston_seat_get_keyboard(&b->core_seat);
Daniel Stone154c34c2012-06-22 13:21:40 +01001037
Derek Foreman1281a362015-07-31 16:55:32 -05001038 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone154c34c2012-06-22 13:21:40 +01001039 keyboard->modifiers.mods_depressed & mask,
1040 keyboard->modifiers.mods_latched & mask,
1041 keyboard->modifiers.mods_locked & mask,
1042 0,
1043 0,
1044 (x11_mask >> 13) & 3);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001045 notify_modifiers(&b->core_seat,
1046 wl_display_next_serial(b->compositor->wl_display));
Daniel Stone154c34c2012-06-22 13:21:40 +01001047}
1048
Tiago Vignattia3a71622011-12-08 17:03:17 +02001049static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001050x11_backend_deliver_button_event(struct x11_backend *b,
1051 xcb_generic_event_t *event, int state)
Tiago Vignattia3a71622011-12-08 17:03:17 +02001052{
1053 xcb_button_press_event_t *button_event =
1054 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +01001055 uint32_t button;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001056 struct x11_output *output;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001057 struct weston_pointer_axis_event weston_event;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001058
Giulio Camuffo954f1832014-10-11 18:27:30 +03001059 output = x11_backend_find_output(b, button_event->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001060 if (!output)
1061 return;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001062
1063 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001064 xcb_grab_pointer(b->conn, 0, output->window,
Kristian Høgsberg73308622012-10-30 11:04:52 -04001065 XCB_EVENT_MASK_BUTTON_PRESS |
1066 XCB_EVENT_MASK_BUTTON_RELEASE |
1067 XCB_EVENT_MASK_POINTER_MOTION |
1068 XCB_EVENT_MASK_ENTER_WINDOW |
1069 XCB_EVENT_MASK_LEAVE_WINDOW,
1070 XCB_GRAB_MODE_ASYNC,
1071 XCB_GRAB_MODE_ASYNC,
1072 output->window, XCB_CURSOR_NONE,
1073 button_event->time);
1074 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001075 xcb_ungrab_pointer(b->conn, button_event->time);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001076
Giulio Camuffo954f1832014-10-11 18:27:30 +03001077 if (!b->has_xkb)
1078 update_xkb_state_from_core(b, button_event->state);
Daniel Stone154c34c2012-06-22 13:21:40 +01001079
Tiago Vignattia3a71622011-12-08 17:03:17 +02001080 switch (button_event->detail) {
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001081 case 1:
1082 button = BTN_LEFT;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001083 break;
1084 case 2:
1085 button = BTN_MIDDLE;
1086 break;
1087 case 3:
1088 button = BTN_RIGHT;
1089 break;
1090 case 4:
Jonas Ådahl62efe202012-09-27 18:40:41 +02001091 /* Axis are measured in pixels, but the xcb events are discrete
1092 * steps. Therefore move the axis by some pixels every step. */
Peter Hutterer89b6a492016-01-18 15:58:17 +10001093 if (state) {
1094 weston_event.value = -DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001095 weston_event.discrete = -1;
1096 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001097 weston_event.axis =
1098 WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001099 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001100 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001101 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001102 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001103 }
Scott Moreau210d0792012-03-22 10:47:01 -06001104 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001105 case 5:
Peter Hutterer89b6a492016-01-18 15:58:17 +10001106 if (state) {
1107 weston_event.value = DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001108 weston_event.discrete = 1;
1109 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001110 weston_event.axis =
1111 WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001112 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001113 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001114 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001115 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001116 }
Scott Moreau210d0792012-03-22 10:47:01 -06001117 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001118 case 6:
Peter Hutterer89b6a492016-01-18 15:58:17 +10001119 if (state) {
1120 weston_event.value = -DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001121 weston_event.discrete = -1;
1122 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001123 weston_event.axis =
1124 WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001125 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001126 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001127 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001128 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001129 }
Scott Moreau210d0792012-03-22 10:47:01 -06001130 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001131 case 7:
Peter Hutterer89b6a492016-01-18 15:58:17 +10001132 if (state) {
1133 weston_event.value = DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001134 weston_event.discrete = 1;
1135 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001136 weston_event.axis =
1137 WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001138 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001139 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001140 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001141 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001142 }
Tiago Vignattia3a71622011-12-08 17:03:17 +02001143 return;
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001144 default:
1145 button = button_event->detail + BTN_SIDE - 8;
1146 break;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001147 }
1148
Giulio Camuffo954f1832014-10-11 18:27:30 +03001149 notify_button(&b->core_seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001150 weston_compositor_get_time(), button,
1151 state ? WL_POINTER_BUTTON_STATE_PRESSED :
1152 WL_POINTER_BUTTON_STATE_RELEASED);
Peter Hutterer87743e92016-01-18 16:38:22 +10001153 notify_pointer_frame(&b->core_seat);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001154}
1155
Scott Moreau1bad5db2012-08-18 01:04:05 -06001156static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001157x11_backend_deliver_motion_event(struct x11_backend *b,
1158 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001159{
1160 struct x11_output *output;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001161 double x, y;
Jonas Ådahld2510102014-10-05 21:39:14 +02001162 struct weston_pointer_motion_event motion_event = { 0 };
Scott Moreau1bad5db2012-08-18 01:04:05 -06001163 xcb_motion_notify_event_t *motion_notify =
1164 (xcb_motion_notify_event_t *) event;
1165
Giulio Camuffo954f1832014-10-11 18:27:30 +03001166 if (!b->has_xkb)
1167 update_xkb_state_from_core(b, motion_notify->state);
1168 output = x11_backend_find_output(b, motion_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001169 if (!output)
1170 return;
1171
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001172 weston_output_transform_coordinate(&output->base,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001173 motion_notify->event_x,
1174 motion_notify->event_y,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05001175 &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001176
Jonas Ådahld2510102014-10-05 21:39:14 +02001177 motion_event = (struct weston_pointer_motion_event) {
1178 .mask = WESTON_POINTER_MOTION_REL,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001179 .dx = x - b->prev_x,
1180 .dy = y - b->prev_y
Jonas Ådahld2510102014-10-05 21:39:14 +02001181 };
1182
Giulio Camuffo954f1832014-10-11 18:27:30 +03001183 notify_motion(&b->core_seat, weston_compositor_get_time(),
Jonas Ådahld2510102014-10-05 21:39:14 +02001184 &motion_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001185 notify_pointer_frame(&b->core_seat);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001186
Giulio Camuffo954f1832014-10-11 18:27:30 +03001187 b->prev_x = x;
1188 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001189}
1190
1191static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001192x11_backend_deliver_enter_event(struct x11_backend *b,
1193 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001194{
1195 struct x11_output *output;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001196 double x, y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001197
1198 xcb_enter_notify_event_t *enter_notify =
1199 (xcb_enter_notify_event_t *) event;
1200 if (enter_notify->state >= Button1Mask)
1201 return;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001202 if (!b->has_xkb)
1203 update_xkb_state_from_core(b, enter_notify->state);
1204 output = x11_backend_find_output(b, enter_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001205 if (!output)
1206 return;
1207
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001208 weston_output_transform_coordinate(&output->base,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001209 enter_notify->event_x,
1210 enter_notify->event_y, &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001211
Giulio Camuffo954f1832014-10-11 18:27:30 +03001212 notify_pointer_focus(&b->core_seat, &output->base, x, y);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001213
Giulio Camuffo954f1832014-10-11 18:27:30 +03001214 b->prev_x = x;
1215 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001216}
1217
Kristian Høgsbergee724822011-04-21 14:51:44 -04001218static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001219x11_backend_next_event(struct x11_backend *b,
1220 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -04001221{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001222 if (mask & WL_EVENT_READABLE) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001223 *event = xcb_poll_for_event(b->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -04001224 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001225#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Giulio Camuffo954f1832014-10-11 18:27:30 +03001226 *event = xcb_poll_for_queued_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001227#else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001228 *event = xcb_poll_for_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001229#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -04001230 }
1231
1232 return *event != NULL;
1233}
1234
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001235static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001236x11_backend_handle_event(int fd, uint32_t mask, void *data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001237{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001238 struct x11_backend *b = data;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001239 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001240 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001241 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001242 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001243 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001244 xcb_keymap_notify_event_t *keymap_notify;
1245 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001246 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001247 xcb_atom_t atom;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001248 xcb_window_t window;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001249 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001250 uint32_t i, set;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001251 uint8_t response_type;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001252 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001253
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001254 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001255 count = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001256 while (x11_backend_next_event(b, &event, mask)) {
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001257 response_type = event->response_type & ~0x80;
1258
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001259 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1260 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001261 /* Suppress key repeat events; this is only used if we
1262 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001263 key_release = (xcb_key_press_event_t *) prev;
1264 key_press = (xcb_key_press_event_t *) event;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001265 if (response_type == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -07001266 key_release->time == key_press->time &&
1267 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001268 /* Don't deliver the held key release
1269 * event or the new key press event. */
1270 free(event);
1271 free(prev);
1272 prev = NULL;
1273 continue;
1274 } else {
1275 /* Deliver the held key release now
1276 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001277 * event below. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001278 update_xkb_state_from_core(b, key_release->state);
1279 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001280 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001281 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001282 WL_KEYBOARD_KEY_STATE_RELEASED,
1283 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001284 free(prev);
1285 prev = NULL;
1286 break;
1287 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001288
1289 case XCB_FOCUS_IN:
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001290 assert(response_type == XCB_KEYMAP_NOTIFY);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001291 keymap_notify = (xcb_keymap_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001292 b->keys.size = 0;
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001293 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
1294 set = keymap_notify->keys[i >> 3] &
1295 (1 << (i & 7));
1296 if (set) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001297 k = wl_array_add(&b->keys, sizeof *k);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001298 *k = i;
1299 }
1300 }
1301
Daniel Stoned6da09e2012-06-22 13:21:29 +01001302 /* Unfortunately the state only comes with the enter
1303 * event, rather than with the focus event. I'm not
1304 * sure of the exact semantics around it and whether
1305 * we can ensure that we get both? */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001306 notify_keyboard_focus_in(&b->core_seat, &b->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001307 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001308
1309 free(prev);
1310 prev = NULL;
1311 break;
1312
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001313 default:
1314 /* No previous event held */
1315 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001316 }
1317
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001318 switch (response_type) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001319 case XCB_KEY_PRESS:
1320 key_press = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001321 if (!b->has_xkb)
1322 update_xkb_state_from_core(b, key_press->state);
1323 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001324 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001325 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001326 WL_KEYBOARD_KEY_STATE_PRESSED,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001327 b->has_xkb ? STATE_UPDATE_NONE :
Daniel Stonee2faa122012-06-22 13:21:39 +01001328 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001329 break;
1330 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001331 /* If we don't have XKB, we need to use the lame
1332 * autorepeat detection above. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001333 if (!b->has_xkb) {
Daniel Stone3ee91e12012-06-22 13:21:36 +01001334 prev = event;
1335 break;
1336 }
1337 key_release = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001338 notify_key(&b->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +01001339 weston_compositor_get_time(),
1340 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001341 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +01001342 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001343 break;
1344 case XCB_BUTTON_PRESS:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001345 x11_backend_deliver_button_event(b, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001346 break;
1347 case XCB_BUTTON_RELEASE:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001348 x11_backend_deliver_button_event(b, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001349 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001350 case XCB_MOTION_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001351 x11_backend_deliver_motion_event(b, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001352 break;
1353
1354 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001355 expose = (xcb_expose_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001356 output = x11_backend_find_output(b, expose->window);
Derek Foreman99bfa642014-12-11 15:44:46 -06001357 if (!output)
1358 break;
1359
Kristian Høgsberga98c2e12013-12-05 12:30:37 -08001360 weston_output_damage(&output->base);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001361 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001362 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001363
1364 case XCB_ENTER_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001365 x11_backend_deliver_enter_event(b, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001366 break;
1367
1368 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001369 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -05001370 if (enter_notify->state >= Button1Mask)
1371 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001372 if (!b->has_xkb)
1373 update_xkb_state_from_core(b, enter_notify->state);
1374 notify_pointer_focus(&b->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001375 break;
1376
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001377 case XCB_CLIENT_MESSAGE:
1378 client_message = (xcb_client_message_event_t *) event;
1379 atom = client_message->data.data32[0];
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001380 window = client_message->window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001381 if (atom == b->atom.wm_delete_window) {
Derek Foremand540f4b2015-01-27 16:26:49 -06001382 struct wl_event_loop *loop;
1383 struct window_delete_data *data = malloc(sizeof *data);
1384
1385 /* if malloc failed we should at least try to
1386 * delete the window, even if it may result in
1387 * a crash.
1388 */
1389 if (!data) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001390 x11_backend_delete_window(b, window);
Derek Foremand540f4b2015-01-27 16:26:49 -06001391 break;
1392 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001393 data->backend = b;
Derek Foremand540f4b2015-01-27 16:26:49 -06001394 data->window = window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001395 loop = wl_display_get_event_loop(b->compositor->wl_display);
Derek Foremand540f4b2015-01-27 16:26:49 -06001396 wl_event_loop_add_idle(loop, delete_cb, data);
1397 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001398 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001399
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001400 case XCB_FOCUS_IN:
1401 focus_in = (xcb_focus_in_event_t *) event;
1402 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1403 break;
1404
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001405 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001406 break;
1407
1408 case XCB_FOCUS_OUT:
1409 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -05001410 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1411 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001412 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001413 notify_keyboard_focus_out(&b->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001414 break;
1415
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001416 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001417 break;
1418 }
1419
Daniel Stonee2faa122012-06-22 13:21:39 +01001420#ifdef HAVE_XCB_XKB
Giulio Camuffo954f1832014-10-11 18:27:30 +03001421 if (b->has_xkb) {
1422 if (response_type == b->xkb_event_base) {
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001423 xcb_xkb_state_notify_event_t *state =
1424 (xcb_xkb_state_notify_event_t *) event;
1425 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001426 update_xkb_state(b, state);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001427 } else if (response_type == XCB_PROPERTY_NOTIFY) {
1428 xcb_property_notify_event_t *prop_notify =
1429 (xcb_property_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001430 if (prop_notify->window == b->screen->root &&
1431 prop_notify->atom == b->atom.xkb_names &&
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001432 prop_notify->state == XCB_PROPERTY_NEW_VALUE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001433 update_xkb_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001434 }
Daniel Stonee2faa122012-06-22 13:21:39 +01001435 }
1436#endif
1437
Bill Spitzakb715cec2012-06-05 17:08:23 -04001438 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001439 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001440 free (event);
1441 }
1442
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001443 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1444 case XCB_KEY_RELEASE:
1445 key_release = (xcb_key_press_event_t *) prev;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001446 update_xkb_state_from_core(b, key_release->state);
1447 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001448 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001449 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001450 WL_KEYBOARD_KEY_STATE_RELEASED,
1451 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001452 free(prev);
1453 prev = NULL;
1454 break;
1455 default:
1456 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001457 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001458
Bill Spitzakb715cec2012-06-05 17:08:23 -04001459 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001460}
1461
Giulio Camuffo954f1832014-10-11 18:27:30 +03001462#define F(field) offsetof(struct x11_backend, field)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001463
1464static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001465x11_backend_get_resources(struct x11_backend *b)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001466{
1467 static const struct { const char *name; int offset; } atoms[] = {
1468 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1469 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1470 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1471 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001472 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001473 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001474 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001475 { "_NET_WM_STATE", F(atom.net_wm_state) },
1476 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001477 { "_NET_SUPPORTING_WM_CHECK",
1478 F(atom.net_supporting_wm_check) },
1479 { "_NET_SUPPORTED", F(atom.net_supported) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001480 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001481 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001482 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001483 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001484 };
1485
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001486 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001487 xcb_intern_atom_reply_t *reply;
1488 xcb_pixmap_t pixmap;
1489 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001490 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001491 uint8_t data[] = { 0, 0, 0, 0 };
1492
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001493 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001494 cookies[i] = xcb_intern_atom (b->conn, 0,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001495 strlen(atoms[i].name),
1496 atoms[i].name);
1497
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001498 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001499 reply = xcb_intern_atom_reply (b->conn, cookies[i], NULL);
1500 *(xcb_atom_t *) ((char *) b + atoms[i].offset) = reply->atom;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001501 free(reply);
1502 }
1503
Giulio Camuffo954f1832014-10-11 18:27:30 +03001504 pixmap = xcb_generate_id(b->conn);
1505 gc = xcb_generate_id(b->conn);
1506 xcb_create_pixmap(b->conn, 1, pixmap, b->screen->root, 1, 1);
1507 xcb_create_gc(b->conn, gc, pixmap, 0, NULL);
1508 xcb_put_image(b->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001509 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001510 b->null_cursor = xcb_generate_id(b->conn);
1511 xcb_create_cursor (b->conn, b->null_cursor,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001512 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001513 xcb_free_gc(b->conn, gc);
1514 xcb_free_pixmap(b->conn, pixmap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001515}
1516
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001517static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001518x11_backend_get_wm_info(struct x11_backend *c)
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001519{
1520 xcb_get_property_cookie_t cookie;
1521 xcb_get_property_reply_t *reply;
1522 xcb_atom_t *atom;
1523 unsigned int i;
1524
1525 cookie = xcb_get_property(c->conn, 0, c->screen->root,
1526 c->atom.net_supported,
1527 XCB_ATOM_ATOM, 0, 1024);
1528 reply = xcb_get_property_reply(c->conn, cookie, NULL);
1529 if (reply == NULL)
1530 return;
1531
1532 atom = (xcb_atom_t *) xcb_get_property_value(reply);
1533
1534 for (i = 0; i < reply->value_len; i++) {
1535 if (atom[i] == c->atom.net_wm_state_fullscreen)
1536 c->has_net_wm_state_fullscreen = 1;
1537 }
Ryo Munakata76fb7ec2015-03-08 19:17:06 +09001538
1539 free(reply);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001540}
1541
1542static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001543x11_restore(struct weston_compositor *ec)
1544{
1545}
1546
1547static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001548x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001549{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001550 struct x11_backend *backend = (struct x11_backend *)ec->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -07001551
Giulio Camuffo954f1832014-10-11 18:27:30 +03001552 wl_event_source_remove(backend->xcb_source);
1553 x11_input_destroy(backend);
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001554
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03001555 weston_compositor_shutdown(ec); /* destroys outputs, too */
1556
Giulio Camuffo954f1832014-10-11 18:27:30 +03001557 XCloseDisplay(backend->dpy);
1558 free(backend);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001559}
1560
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001561static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001562init_gl_renderer(struct x11_backend *b)
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001563{
1564 int ret;
1565
1566 gl_renderer = weston_load_module("gl-renderer.so",
1567 "gl_renderer_interface");
1568 if (!gl_renderer)
1569 return -1;
1570
Giulio Camuffo954f1832014-10-11 18:27:30 +03001571 ret = gl_renderer->create(b->compositor, EGL_PLATFORM_X11_KHR, (void *) b->dpy,
Derek Foremane76f1852015-05-15 12:12:39 -05001572 gl_renderer->opaque_attribs, NULL, 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001573
1574 return ret;
1575}
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001576
Giulio Camuffo954f1832014-10-11 18:27:30 +03001577static struct x11_backend *
1578x11_backend_create(struct weston_compositor *compositor,
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001579 struct weston_x11_backend_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001580{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001581 struct x11_backend *b;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001582 struct x11_output *output;
Pekka Paalanen157109b2016-03-24 15:43:44 +02001583 struct wl_event_loop *loop;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001584 int x = 0;
1585 unsigned i;
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001586
Giulio Camuffo954f1832014-10-11 18:27:30 +03001587 b = zalloc(sizeof *b);
1588 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001589 return NULL;
1590
Giulio Camuffo954f1832014-10-11 18:27:30 +03001591 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001592 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001593 goto err_free;
1594
Giulio Camuffo954f1832014-10-11 18:27:30 +03001595 b->dpy = XOpenDisplay(NULL);
1596 if (b->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001597 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001598
Giulio Camuffo954f1832014-10-11 18:27:30 +03001599 b->conn = XGetXCBConnection(b->dpy);
1600 XSetEventQueueOwner(b->dpy, XCBOwnsEventQueue);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001601
Giulio Camuffo954f1832014-10-11 18:27:30 +03001602 if (xcb_connection_has_error(b->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001603 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001604
Marko61b4d3e2015-10-05 17:27:54 -05001605 b->screen = x11_compositor_get_default_screen(b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001606 wl_array_init(&b->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001607
Giulio Camuffo954f1832014-10-11 18:27:30 +03001608 x11_backend_get_resources(b);
1609 x11_backend_get_wm_info(b);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001610
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001611 if (!b->has_net_wm_state_fullscreen && config->fullscreen) {
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001612 weston_log("Can not fullscreen without window manager support"
1613 "(need _NET_WM_STATE_FULLSCREEN)\n");
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001614 config->fullscreen = 0;
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001615 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001616
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001617 b->use_pixman = config->use_pixman;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001618 if (b->use_pixman) {
1619 if (pixman_renderer_init(compositor) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001620 weston_log("Failed to initialize pixman renderer for X11 backend\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001621 goto err_xdisplay;
Bryce Harrington665b0252015-05-26 18:14:21 -07001622 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001623 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001624 else if (init_gl_renderer(b) < 0) {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001625 goto err_xdisplay;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001626 }
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001627 weston_log("Using %s renderer\n", config->use_pixman ? "pixman" : "gl");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001628
Giulio Camuffo954f1832014-10-11 18:27:30 +03001629 b->base.destroy = x11_destroy;
1630 b->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001631
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001632 if (x11_input_create(b, config->no_input) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001633 weston_log("Failed to create X11 input\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001634 goto err_renderer;
Bryce Harrington665b0252015-05-26 18:14:21 -07001635 }
Daniel Stone22815f92012-06-22 13:21:34 +01001636
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001637 for (i = 0; i < config->num_outputs; ++i) {
1638 struct weston_x11_backend_output_config *output_iterator =
1639 &config->outputs[i];
Scott Moreau248aaec2012-08-03 14:19:52 -06001640
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001641 if (output_iterator->name == NULL) {
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001642 continue;
Rob Bradford5c4245d2013-07-26 16:29:44 +01001643 }
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001644
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001645 if (output_iterator->width < 1) {
1646 weston_log("Invalid width \"%d\" for output %s\n",
1647 output_iterator->width, output_iterator->name);
1648 goto err_x11_input;
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001649 }
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001650
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001651 if (output_iterator->height < 1) {
1652 weston_log("Invalid height \"%d\" for output %s\n",
1653 output_iterator->height, output_iterator->name);
1654 goto err_x11_input;
1655 }
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001656
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001657 output = x11_backend_create_output(b,
1658 x,
1659 0,
1660 output_iterator->width,
1661 output_iterator->height,
1662 config->fullscreen,
1663 config->no_input,
1664 output_iterator->name,
1665 output_iterator->transform,
1666 output_iterator->scale);
Bryce Harrington665b0252015-05-26 18:14:21 -07001667 if (output == NULL) {
1668 weston_log("Failed to create configured x11 output\n");
Scott Moreau248aaec2012-08-03 14:19:52 -06001669 goto err_x11_input;
Bryce Harrington665b0252015-05-26 18:14:21 -07001670 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06001671
1672 x = pixman_region32_extents(&output->base.region)->x2;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001673 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001674
Pekka Paalanen157109b2016-03-24 15:43:44 +02001675 loop = wl_display_get_event_loop(compositor->wl_display);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001676 b->xcb_source =
Pekka Paalanen157109b2016-03-24 15:43:44 +02001677 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001678 xcb_get_file_descriptor(b->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001679 WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001680 x11_backend_handle_event, b);
1681 wl_event_source_check(b->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001682
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001683 if (compositor->renderer->import_dmabuf) {
1684 if (linux_dmabuf_setup(compositor) < 0)
1685 weston_log("Error: initializing dmabuf "
1686 "support failed.\n");
1687 }
1688
Giulio Camuffo954f1832014-10-11 18:27:30 +03001689 compositor->backend = &b->base;
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001690
Giulio Camuffo954f1832014-10-11 18:27:30 +03001691 return b;
Martin Olsson11434bb2012-07-08 03:03:44 +02001692
1693err_x11_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001694 x11_input_destroy(b);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001695err_renderer:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001696 compositor->renderer->destroy(compositor);
Martin Olsson11434bb2012-07-08 03:03:44 +02001697err_xdisplay:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001698 XCloseDisplay(b->dpy);
Martin Olsson11434bb2012-07-08 03:03:44 +02001699err_free:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001700 free(b);
Martin Olsson11434bb2012-07-08 03:03:44 +02001701 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001702}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001703
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001704static void
1705config_init_to_defaults(struct weston_x11_backend_config *config)
1706{
1707}
1708
Giulio Camuffo954f1832014-10-11 18:27:30 +03001709WL_EXPORT int
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001710backend_init(struct weston_compositor *compositor,
Giulio Camuffo93daabb2015-10-17 19:24:14 +03001711 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001712{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001713 struct x11_backend *b;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001714 struct weston_x11_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001715
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001716 if (config_base == NULL ||
1717 config_base->struct_version != WESTON_X11_BACKEND_CONFIG_VERSION ||
1718 config_base->struct_size > sizeof(struct weston_x11_backend_config)) {
1719 weston_log("X11 backend config structure is invalid\n");
1720 return -1;
1721 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001722
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001723 config_init_to_defaults(&config);
1724 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001725
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001726 b = x11_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001727 if (b == NULL)
1728 return -1;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001729
Giulio Camuffo954f1832014-10-11 18:27:30 +03001730 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001731}