blob: 5562b9ffa6a14458088e59888bfb55d05ad8bff6 [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
Armin Krezović295e9d02016-08-01 19:17:58 +0200130static inline struct x11_output *
131to_x11_output(struct weston_output *base)
132{
133 return container_of(base, struct x11_output, base);
134}
135
136static inline struct x11_backend *
137to_x11_backend(struct weston_compositor *base)
138{
139 return container_of(base->backend, struct x11_backend, base);
140}
141
Marko61b4d3e2015-10-05 17:27:54 -0500142static xcb_screen_t *
143x11_compositor_get_default_screen(struct x11_backend *b)
144{
145 xcb_screen_iterator_t iter;
146 int i, screen_nbr = XDefaultScreen(b->dpy);
147
148 iter = xcb_setup_roots_iterator(xcb_get_setup(b->conn));
149 for (i = 0; iter.rem; xcb_screen_next(&iter), i++)
150 if (i == screen_nbr)
151 return iter.data;
152
153 return xcb_setup_roots_iterator(xcb_get_setup(b->conn)).data;
154}
155
Daniel Stone576cd152012-06-01 12:14:02 +0100156static struct xkb_keymap *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300157x11_backend_get_keymap(struct x11_backend *b)
Daniel Stone576cd152012-06-01 12:14:02 +0100158{
159 xcb_get_property_cookie_t cookie;
160 xcb_get_property_reply_t *reply;
Daniel Stone576cd152012-06-01 12:14:02 +0100161 struct xkb_rule_names names;
162 struct xkb_keymap *ret;
163 const char *value_all, *value_part;
164 int length_all, length_part;
165
166 memset(&names, 0, sizeof(names));
167
Giulio Camuffo954f1832014-10-11 18:27:30 +0300168 cookie = xcb_get_property(b->conn, 0, b->screen->root,
169 b->atom.xkb_names, b->atom.string, 0, 1024);
170 reply = xcb_get_property_reply(b->conn, cookie, NULL);
Daniel Stone576cd152012-06-01 12:14:02 +0100171 if (reply == NULL)
172 return NULL;
173
174 value_all = xcb_get_property_value(reply);
175 length_all = xcb_get_property_value_length(reply);
176 value_part = value_all;
177
178#define copy_prop_value(to) \
179 length_part = strlen(value_part); \
180 if (value_part + length_part < (value_all + length_all) && \
181 length_part > 0) \
182 names.to = value_part; \
183 value_part += length_part + 1;
184
185 copy_prop_value(rules);
186 copy_prop_value(model);
187 copy_prop_value(layout);
188 copy_prop_value(variant);
189 copy_prop_value(options);
190#undef copy_prop_value
191
Giulio Camuffo954f1832014-10-11 18:27:30 +0300192 ret = xkb_keymap_new_from_names(b->compositor->xkb_context, &names, 0);
Daniel Stone576cd152012-06-01 12:14:02 +0100193
194 free(reply);
195 return ret;
196}
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400197
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400198static uint32_t
Giulio Camuffo954f1832014-10-11 18:27:30 +0300199get_xkb_mod_mask(struct x11_backend *b, uint32_t in)
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400200{
Derek Foreman1281a362015-07-31 16:55:32 -0500201 struct weston_keyboard *keyboard =
202 weston_seat_get_keyboard(&b->core_seat);
203 struct weston_xkb_info *info = keyboard->xkb_info;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400204 uint32_t ret = 0;
205
206 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
207 ret |= (1 << info->shift_mod);
208 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
209 ret |= (1 << info->caps_mod);
210 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
211 ret |= (1 << info->ctrl_mod);
212 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
213 ret |= (1 << info->alt_mod);
214 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
215 ret |= (1 << info->mod2_mod);
216 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
217 ret |= (1 << info->mod3_mod);
218 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
219 ret |= (1 << info->super_mod);
220 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
221 ret |= (1 << info->mod5_mod);
222
223 return ret;
224}
225
Daniel Stone62b33b62012-06-22 13:21:35 +0100226static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300227x11_backend_setup_xkb(struct x11_backend *b)
Daniel Stone62b33b62012-06-22 13:21:35 +0100228{
229#ifndef HAVE_XCB_XKB
230 weston_log("XCB-XKB not available during build\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300231 b->has_xkb = 0;
232 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100233 return;
234#else
Derek Foreman1281a362015-07-31 16:55:32 -0500235 struct weston_keyboard *keyboard;
Daniel Stone62b33b62012-06-22 13:21:35 +0100236 const xcb_query_extension_reply_t *ext;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100237 xcb_generic_error_t *error;
Daniel Stonee2faa122012-06-22 13:21:39 +0100238 xcb_void_cookie_t select;
Ran Benita6a39d872012-10-31 20:14:57 +0200239 xcb_xkb_use_extension_cookie_t use_ext;
240 xcb_xkb_use_extension_reply_t *use_ext_reply;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100241 xcb_xkb_per_client_flags_cookie_t pcf;
242 xcb_xkb_per_client_flags_reply_t *pcf_reply;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400243 xcb_xkb_get_state_cookie_t state;
244 xcb_xkb_get_state_reply_t *state_reply;
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200245 uint32_t values[1] = { XCB_EVENT_MASK_PROPERTY_CHANGE };
Daniel Stone62b33b62012-06-22 13:21:35 +0100246
Giulio Camuffo954f1832014-10-11 18:27:30 +0300247 b->has_xkb = 0;
248 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100249
Giulio Camuffo954f1832014-10-11 18:27:30 +0300250 ext = xcb_get_extension_data(b->conn, &xcb_xkb_id);
Daniel Stone62b33b62012-06-22 13:21:35 +0100251 if (!ext) {
252 weston_log("XKB extension not available on host X11 server\n");
253 return;
254 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300255 b->xkb_event_base = ext->first_event;
Daniel Stone62b33b62012-06-22 13:21:35 +0100256
Giulio Camuffo954f1832014-10-11 18:27:30 +0300257 select = xcb_xkb_select_events_checked(b->conn,
Ran Benita424ae012012-10-31 00:13:08 +0200258 XCB_XKB_ID_USE_CORE_KBD,
259 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
260 0,
261 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
262 0,
263 0,
264 NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300265 error = xcb_request_check(b->conn, select);
Daniel Stonee2faa122012-06-22 13:21:39 +0100266 if (error) {
267 weston_log("error: failed to select for XKB state events\n");
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200268 free(error);
Daniel Stonee2faa122012-06-22 13:21:39 +0100269 return;
270 }
271
Giulio Camuffo954f1832014-10-11 18:27:30 +0300272 use_ext = xcb_xkb_use_extension(b->conn,
Ran Benita6a39d872012-10-31 20:14:57 +0200273 XCB_XKB_MAJOR_VERSION,
274 XCB_XKB_MINOR_VERSION);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300275 use_ext_reply = xcb_xkb_use_extension_reply(b->conn, use_ext, NULL);
Ran Benita6a39d872012-10-31 20:14:57 +0200276 if (!use_ext_reply) {
277 weston_log("couldn't start using XKB extension\n");
278 return;
279 }
280
281 if (!use_ext_reply->supported) {
282 weston_log("XKB extension version on the server is too old "
283 "(want %d.%d, has %d.%d)\n",
284 XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION,
285 use_ext_reply->serverMajor, use_ext_reply->serverMinor);
286 free(use_ext_reply);
287 return;
288 }
289 free(use_ext_reply);
290
Giulio Camuffo954f1832014-10-11 18:27:30 +0300291 pcf = xcb_xkb_per_client_flags(b->conn,
Daniel Stone3ee91e12012-06-22 13:21:36 +0100292 XCB_XKB_ID_USE_CORE_KBD,
293 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
294 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
295 0,
296 0,
297 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300298 pcf_reply = xcb_xkb_per_client_flags_reply(b->conn, pcf, NULL);
Ran Benita7109cc82012-10-31 20:14:58 +0200299 if (!pcf_reply ||
300 !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT)) {
Daniel Stone3ee91e12012-06-22 13:21:36 +0100301 weston_log("failed to set XKB per-client flags, not using "
302 "detectable repeat\n");
Ran Benita7109cc82012-10-31 20:14:58 +0200303 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100304 return;
305 }
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200306 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100307
Giulio Camuffo954f1832014-10-11 18:27:30 +0300308 state = xcb_xkb_get_state(b->conn, XCB_XKB_ID_USE_CORE_KBD);
309 state_reply = xcb_xkb_get_state_reply(b->conn, state, NULL);
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200310 if (!state_reply) {
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400311 weston_log("failed to get initial XKB state\n");
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400312 return;
313 }
314
Derek Foreman1281a362015-07-31 16:55:32 -0500315 keyboard = weston_seat_get_keyboard(&b->core_seat);
316 xkb_state_update_mask(keyboard->xkb_state.state,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300317 get_xkb_mod_mask(b, state_reply->baseMods),
318 get_xkb_mod_mask(b, state_reply->latchedMods),
319 get_xkb_mod_mask(b, state_reply->lockedMods),
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400320 0,
321 0,
322 state_reply->group);
323
324 free(state_reply);
325
Giulio Camuffo954f1832014-10-11 18:27:30 +0300326 xcb_change_window_attributes(b->conn, b->screen->root,
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200327 XCB_CW_EVENT_MASK, values);
328
Giulio Camuffo954f1832014-10-11 18:27:30 +0300329 b->has_xkb = 1;
Daniel Stone62b33b62012-06-22 13:21:35 +0100330#endif
331}
332
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200333#ifdef HAVE_XCB_XKB
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200334static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300335update_xkb_keymap(struct x11_backend *b)
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200336{
337 struct xkb_keymap *keymap;
338
Giulio Camuffo954f1832014-10-11 18:27:30 +0300339 keymap = x11_backend_get_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200340 if (!keymap) {
341 weston_log("failed to get XKB keymap\n");
342 return;
343 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300344 weston_seat_update_keymap(&b->core_seat, keymap);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200345 xkb_keymap_unref(keymap);
346}
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200347#endif
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200348
Darxus55973f22010-11-22 21:24:39 -0500349static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300350x11_input_create(struct x11_backend *b, int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400351{
Daniel Stone576cd152012-06-01 12:14:02 +0100352 struct xkb_keymap *keymap;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400353
Giulio Camuffo954f1832014-10-11 18:27:30 +0300354 weston_seat_init(&b->core_seat, b->compositor, "default");
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300355
356 if (no_input)
357 return 0;
358
Giulio Camuffo954f1832014-10-11 18:27:30 +0300359 weston_seat_init_pointer(&b->core_seat);
Daniel Stone576cd152012-06-01 12:14:02 +0100360
Giulio Camuffo954f1832014-10-11 18:27:30 +0300361 keymap = x11_backend_get_keymap(b);
362 if (weston_seat_init_keyboard(&b->core_seat, keymap) < 0)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500363 return -1;
Ran Benitac9c74152014-08-19 23:59:52 +0300364 xkb_keymap_unref(keymap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400365
Giulio Camuffo954f1832014-10-11 18:27:30 +0300366 x11_backend_setup_xkb(b);
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400367
Darxus55973f22010-11-22 21:24:39 -0500368 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400369}
370
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200371static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300372x11_input_destroy(struct x11_backend *b)
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200373{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300374 weston_seat_release(&b->core_seat);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200375}
376
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500377static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200378x11_output_start_repaint_loop(struct weston_output *output)
379{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400380 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200381
Pekka Paalanen662f3842015-03-18 12:17:26 +0200382 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200383 weston_output_finish_frame(output, &ts, WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200384}
385
David Herrmann1edf44c2013-10-22 17:11:26 +0200386static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300387x11_output_repaint_gl(struct weston_output *output_base,
388 pixman_region32_t *damage)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500389{
Armin Krezović295e9d02016-08-01 19:17:58 +0200390 struct x11_output *output = to_x11_output(output_base);
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400391 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500392
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400393 ec->renderer->repaint_output(output_base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500394
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200395 pixman_region32_subtract(&ec->primary_plane.damage,
396 &ec->primary_plane.damage, damage);
397
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500398 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200399 return 0;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500400}
401
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300402static void
Alexander Larsson80f91632013-05-22 14:41:38 +0200403set_clip_for_output(struct weston_output *output_base, pixman_region32_t *region)
404{
Armin Krezović295e9d02016-08-01 19:17:58 +0200405 struct x11_output *output = to_x11_output(output_base);
Alexander Larsson80f91632013-05-22 14:41:38 +0200406 struct weston_compositor *ec = output->base.compositor;
Armin Krezović295e9d02016-08-01 19:17:58 +0200407 struct x11_backend *b = to_x11_backend(ec);
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500408 pixman_region32_t transformed_region;
Alexander Larsson80f91632013-05-22 14:41:38 +0200409 pixman_box32_t *rects;
410 xcb_rectangle_t *output_rects;
Alexander Larsson80f91632013-05-22 14:41:38 +0200411 xcb_void_cookie_t cookie;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500412 int nrects, i;
Alexander Larsson80f91632013-05-22 14:41:38 +0200413 xcb_generic_error_t *err;
414
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500415 pixman_region32_init(&transformed_region);
416 pixman_region32_copy(&transformed_region, region);
417 pixman_region32_translate(&transformed_region,
418 -output_base->x, -output_base->y);
419 weston_transformed_region(output_base->width, output_base->height,
420 output_base->transform,
421 output_base->current_scale,
422 &transformed_region, &transformed_region);
423
424 rects = pixman_region32_rectangles(&transformed_region, &nrects);
Alexander Larsson80f91632013-05-22 14:41:38 +0200425 output_rects = calloc(nrects, sizeof(xcb_rectangle_t));
426
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500427 if (output_rects == NULL) {
428 pixman_region32_fini(&transformed_region);
Alexander Larsson80f91632013-05-22 14:41:38 +0200429 return;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500430 }
Alexander Larsson80f91632013-05-22 14:41:38 +0200431
432 for (i = 0; i < nrects; i++) {
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500433 output_rects[i].x = rects[i].x1;
434 output_rects[i].y = rects[i].y1;
435 output_rects[i].width = rects[i].x2 - rects[i].x1;
436 output_rects[i].height = rects[i].y2 - rects[i].y1;
Alexander Larsson80f91632013-05-22 14:41:38 +0200437 }
438
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500439 pixman_region32_fini(&transformed_region);
440
Giulio Camuffo954f1832014-10-11 18:27:30 +0300441 cookie = xcb_set_clip_rectangles_checked(b->conn, XCB_CLIP_ORDERING_UNSORTED,
Alexander Larsson80f91632013-05-22 14:41:38 +0200442 output->gc,
443 0, 0, nrects,
444 output_rects);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300445 err = xcb_request_check(b->conn, cookie);
Alexander Larsson80f91632013-05-22 14:41:38 +0200446 if (err != NULL) {
447 weston_log("Failed to set clip rects, err: %d\n", err->error_code);
448 free(err);
449 }
450 free(output_rects);
451}
452
453
David Herrmann1edf44c2013-10-22 17:11:26 +0200454static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300455x11_output_repaint_shm(struct weston_output *output_base,
456 pixman_region32_t *damage)
457{
Armin Krezović295e9d02016-08-01 19:17:58 +0200458 struct x11_output *output = to_x11_output(output_base);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300459 struct weston_compositor *ec = output->base.compositor;
Armin Krezović295e9d02016-08-01 19:17:58 +0200460 struct x11_backend *b = to_x11_backend(ec);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300461 xcb_void_cookie_t cookie;
462 xcb_generic_error_t *err;
463
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200464 pixman_renderer_output_set_buffer(output_base, output->hw_surface);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300465 ec->renderer->repaint_output(output_base, damage);
466
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300467 pixman_region32_subtract(&ec->primary_plane.damage,
468 &ec->primary_plane.damage, damage);
Alexander Larsson80f91632013-05-22 14:41:38 +0200469 set_clip_for_output(output_base, damage);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300470 cookie = xcb_shm_put_image_checked(b->conn, output->window, output->gc,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300471 pixman_image_get_width(output->hw_surface),
472 pixman_image_get_height(output->hw_surface),
473 0, 0,
474 pixman_image_get_width(output->hw_surface),
475 pixman_image_get_height(output->hw_surface),
476 0, 0, output->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
477 0, output->segment, 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300478 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300479 if (err != NULL) {
480 weston_log("Failed to put shm image, err: %d\n", err->error_code);
481 free(err);
482 }
483
484 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200485 return 0;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300486}
487
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100488static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400489finish_frame_handler(void *data)
490{
491 struct x11_output *output = data;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200492 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200493
Pekka Paalanen662f3842015-03-18 12:17:26 +0200494 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200495 weston_output_finish_frame(&output->base, &ts, 0);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400496
497 return 1;
498}
499
Matt Roper361d2ad2011-08-29 13:52:23 -0700500static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300501x11_output_deinit_shm(struct x11_backend *b, struct x11_output *output)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300502{
503 xcb_void_cookie_t cookie;
504 xcb_generic_error_t *err;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300505 xcb_free_gc(b->conn, output->gc);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300506
507 pixman_image_unref(output->hw_surface);
508 output->hw_surface = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300509 cookie = xcb_shm_detach_checked(b->conn, output->segment);
510 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300511 if (err) {
512 weston_log("xcb_shm_detach failed, error %d\n", err->error_code);
513 free(err);
514 }
515 shmdt(output->buf);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300516}
517
518static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500519x11_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700520{
Armin Krezović295e9d02016-08-01 19:17:58 +0200521 struct x11_output *output = to_x11_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300522 struct x11_backend *backend =
Armin Krezović295e9d02016-08-01 19:17:58 +0200523 to_x11_backend(output->base.compositor);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200524
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200525 wl_event_source_remove(output->finish_frame_timer);
526
Giulio Camuffo954f1832014-10-11 18:27:30 +0300527 if (backend->use_pixman) {
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300528 pixman_renderer_output_destroy(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300529 x11_output_deinit_shm(backend, output);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300530 } else
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300531 gl_renderer->output_destroy(output_base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200532
Giulio Camuffo954f1832014-10-11 18:27:30 +0300533 xcb_destroy_window(backend->conn, output->window);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200534
Armin Krezovićad276932016-06-30 06:29:00 +0200535 xcb_flush(backend->conn);
536
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500537 weston_output_destroy(&output->base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200538
539 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700540}
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200541
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400542static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300543x11_output_set_wm_protocols(struct x11_backend *b,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500544 struct x11_output *output)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400545{
546 xcb_atom_t list[1];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400547
Giulio Camuffo954f1832014-10-11 18:27:30 +0300548 list[0] = b->atom.wm_delete_window;
549 xcb_change_property (b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400550 XCB_PROP_MODE_REPLACE,
551 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300552 b->atom.wm_protocols,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400553 XCB_ATOM_ATOM,
554 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500555 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400556 list);
557}
558
559struct wm_normal_hints {
560 uint32_t flags;
561 uint32_t pad[4];
562 int32_t min_width, min_height;
563 int32_t max_width, max_height;
564 int32_t width_inc, height_inc;
565 int32_t min_aspect_x, min_aspect_y;
566 int32_t max_aspect_x, max_aspect_y;
567 int32_t base_width, base_height;
568 int32_t win_gravity;
569};
570
571#define WM_NORMAL_HINTS_MIN_SIZE 16
572#define WM_NORMAL_HINTS_MAX_SIZE 32
573
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500574static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300575x11_output_set_icon(struct x11_backend *b,
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400576 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500577{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400578 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400579 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400580 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500581
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400582 image = load_image(filename);
583 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500584 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400585 width = pixman_image_get_width(image);
586 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500587 icon = malloc(width * height * 4 + 8);
588 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400589 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500590 return;
591 }
592
593 icon[0] = width;
594 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400595 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300596 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
597 b->atom.net_wm_icon, b->atom.cardinal, 32,
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500598 width * height + 2, icon);
599 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400600 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500601}
602
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500603static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300604x11_output_wait_for_map(struct x11_backend *b, struct x11_output *output)
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500605{
606 xcb_map_notify_event_t *map_notify;
607 xcb_configure_notify_event_t *configure_notify;
608 xcb_generic_event_t *event;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500609 int mapped = 0, configured = 0;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500610 uint8_t response_type;
611
612 /* This isn't the nicest way to do this. Ideally, we could
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500613 * just go back to the main loop and once we get the configure
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500614 * notify, we add the output to the compositor. While we do
615 * support output hotplug, we can't start up with no outputs.
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500616 * We could add the output and then resize once we get the
617 * configure notify, but we don't want to start up and
618 * immediately resize the output.
619 *
620 * Also, some window managers don't give us our final
621 * fullscreen size before map_notify, so if we don't get a
622 * configure_notify before map_notify, we just wait for the
623 * first one and hope that's our size. */
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500624
Giulio Camuffo954f1832014-10-11 18:27:30 +0300625 xcb_flush(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500626
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500627 while (!mapped || !configured) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300628 event = xcb_wait_for_event(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500629 response_type = event->response_type & ~0x80;
630
631 switch (response_type) {
632 case XCB_MAP_NOTIFY:
633 map_notify = (xcb_map_notify_event_t *) event;
634 if (map_notify->window == output->window)
635 mapped = 1;
636 break;
637
638 case XCB_CONFIGURE_NOTIFY:
639 configure_notify =
640 (xcb_configure_notify_event_t *) event;
641
Alexander Larsson4ea95522013-05-22 14:41:37 +0200642
643 if (configure_notify->width % output->scale != 0 ||
644 configure_notify->height % output->scale != 0)
645 weston_log("Resolution is not a multiple of screen size, rounding\n");
Jason Ekstrandd89a0942014-03-07 15:29:14 -0600646 output->mode.width = configure_notify->width;
647 output->mode.height = configure_notify->height;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500648 configured = 1;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500649 break;
650 }
651 }
652}
653
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300654static xcb_visualtype_t *
655find_visual_by_id(xcb_screen_t *screen,
656 xcb_visualid_t id)
657{
658 xcb_depth_iterator_t i;
659 xcb_visualtype_iterator_t j;
660 for (i = xcb_screen_allowed_depths_iterator(screen);
661 i.rem;
662 xcb_depth_next(&i)) {
663 for (j = xcb_depth_visuals_iterator(i.data);
664 j.rem;
665 xcb_visualtype_next(&j)) {
666 if (j.data->visual_id == id)
667 return j.data;
668 }
669 }
670 return 0;
671}
672
673static uint8_t
674get_depth_of_visual(xcb_screen_t *screen,
675 xcb_visualid_t id)
676{
677 xcb_depth_iterator_t i;
678 xcb_visualtype_iterator_t j;
679 for (i = xcb_screen_allowed_depths_iterator(screen);
680 i.rem;
681 xcb_depth_next(&i)) {
682 for (j = xcb_depth_visuals_iterator(i.data);
683 j.rem;
684 xcb_visualtype_next(&j)) {
685 if (j.data->visual_id == id)
686 return i.data->depth;
687 }
688 }
689 return 0;
690}
691
692static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300693x11_output_init_shm(struct x11_backend *b, struct x11_output *output,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300694 int width, int height)
695{
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300696 xcb_visualtype_t *visual_type;
Marko61b4d3e2015-10-05 17:27:54 -0500697 xcb_screen_t *screen;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300698 xcb_format_iterator_t fmt;
699 xcb_void_cookie_t cookie;
700 xcb_generic_error_t *err;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300701 const xcb_query_extension_reply_t *ext;
702 int bitsperpixel = 0;
703 pixman_format_code_t pixman_format;
704
705 /* Check if SHM is available */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300706 ext = xcb_get_extension_data(b->conn, &xcb_shm_id);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300707 if (ext == NULL || !ext->present) {
708 /* SHM is missing */
709 weston_log("SHM extension is not available\n");
710 errno = ENOENT;
711 return -1;
712 }
713
Marko61b4d3e2015-10-05 17:27:54 -0500714 screen = x11_compositor_get_default_screen(b);
715 visual_type = find_visual_by_id(screen, screen->root_visual);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300716 if (!visual_type) {
717 weston_log("Failed to lookup visual for root window\n");
718 errno = ENOENT;
719 return -1;
720 }
721 weston_log("Found visual, bits per value: %d, red_mask: %.8x, green_mask: %.8x, blue_mask: %.8x\n",
722 visual_type->bits_per_rgb_value,
723 visual_type->red_mask,
724 visual_type->green_mask,
725 visual_type->blue_mask);
Marko61b4d3e2015-10-05 17:27:54 -0500726 output->depth = get_depth_of_visual(screen, screen->root_visual);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300727 weston_log("Visual depth is %d\n", output->depth);
728
Giulio Camuffo954f1832014-10-11 18:27:30 +0300729 for (fmt = xcb_setup_pixmap_formats_iterator(xcb_get_setup(b->conn));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300730 fmt.rem;
731 xcb_format_next(&fmt)) {
732 if (fmt.data->depth == output->depth) {
733 bitsperpixel = fmt.data->bits_per_pixel;
734 break;
735 }
736 }
737 weston_log("Found format for depth %d, bpp: %d\n",
738 output->depth, bitsperpixel);
739
740 if (bitsperpixel == 32 &&
741 visual_type->red_mask == 0xff0000 &&
742 visual_type->green_mask == 0x00ff00 &&
743 visual_type->blue_mask == 0x0000ff) {
744 weston_log("Will use x8r8g8b8 format for SHM surfaces\n");
745 pixman_format = PIXMAN_x8r8g8b8;
MoD1b55a592013-11-28 05:24:21 +0000746 } else if (bitsperpixel == 16 &&
747 visual_type->red_mask == 0x00f800 &&
748 visual_type->green_mask == 0x0007e0 &&
749 visual_type->blue_mask == 0x00001f) {
750 weston_log("Will use r5g6b5 format for SHM surfaces\n");
751 pixman_format = PIXMAN_r5g6b5;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300752 } else {
753 weston_log("Can't find appropriate format for SHM pixmap\n");
754 errno = ENOTSUP;
755 return -1;
756 }
757
758
759 /* Create SHM segment and attach it */
760 output->shm_id = shmget(IPC_PRIVATE, width * height * (bitsperpixel / 8), IPC_CREAT | S_IRWXU);
761 if (output->shm_id == -1) {
762 weston_log("x11shm: failed to allocate SHM segment\n");
763 return -1;
764 }
765 output->buf = shmat(output->shm_id, NULL, 0 /* read/write */);
766 if (-1 == (long)output->buf) {
767 weston_log("x11shm: failed to attach SHM segment\n");
768 return -1;
769 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300770 output->segment = xcb_generate_id(b->conn);
771 cookie = xcb_shm_attach_checked(b->conn, output->segment, output->shm_id, 1);
772 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300773 if (err) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700774 weston_log("x11shm: xcb_shm_attach error %d, op code %d, resource id %d\n",
775 err->error_code, err->major_code, err->minor_code);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300776 free(err);
777 return -1;
778 }
779
780 shmctl(output->shm_id, IPC_RMID, NULL);
781
782 /* Now create pixman image */
783 output->hw_surface = pixman_image_create_bits(pixman_format, width, height, output->buf,
784 width * (bitsperpixel / 8));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300785
Giulio Camuffo954f1832014-10-11 18:27:30 +0300786 output->gc = xcb_generate_id(b->conn);
787 xcb_create_gc(b->conn, output->gc, output->window, 0, NULL);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300788
789 return 0;
790}
791
Scott Moreau1bad5db2012-08-18 01:04:05 -0600792static struct x11_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300793x11_backend_create_output(struct x11_backend *b, int x, int y,
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300794 int width, int height, int fullscreen,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600795 int no_input, char *configured_name,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200796 uint32_t transform, int32_t scale)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400797{
Scott Moreau1bad5db2012-08-18 01:04:05 -0600798 static const char name[] = "Weston Compositor";
Kristian Høgsberg86000402012-01-03 23:24:14 -0500799 static const char class[] = "weston-1\0Weston Compositor";
Benoit Gschwind5c2f20e2016-06-05 19:01:11 +0200800 char *title = NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400801 struct x11_output *output;
Marko61b4d3e2015-10-05 17:27:54 -0500802 xcb_screen_t *screen;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400803 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400804 struct wl_event_loop *loop;
Kristian Høgsberg8404acb2014-03-18 12:15:42 -0700805 int output_width, output_height, width_mm, height_mm;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300806 int ret;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400807 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500808 xcb_atom_t atom_list[1];
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300809 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400810 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300811 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400812 0
813 };
814
Alexander Larsson4ea95522013-05-22 14:41:37 +0200815 output_width = width * scale;
816 output_height = height * scale;
817
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300818 if (!no_input)
819 values[0] |=
820 XCB_EVENT_MASK_KEY_PRESS |
821 XCB_EVENT_MASK_KEY_RELEASE |
822 XCB_EVENT_MASK_BUTTON_PRESS |
823 XCB_EVENT_MASK_BUTTON_RELEASE |
824 XCB_EVENT_MASK_POINTER_MOTION |
825 XCB_EVENT_MASK_ENTER_WINDOW |
826 XCB_EVENT_MASK_LEAVE_WINDOW |
827 XCB_EVENT_MASK_KEYMAP_STATE |
828 XCB_EVENT_MASK_FOCUS_CHANGE;
829
Peter Huttererf3d62272013-08-08 11:57:05 +1000830 output = zalloc(sizeof *output);
Bryce Harrington665b0252015-05-26 18:14:21 -0700831 if (output == NULL) {
832 perror("zalloc");
Scott Moreau1bad5db2012-08-18 01:04:05 -0600833 return NULL;
Bryce Harrington665b0252015-05-26 18:14:21 -0700834 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400835
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400836 output->mode.flags =
837 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200838
Alexander Larsson0b135062013-05-28 16:23:36 +0200839 output->mode.width = output_width;
840 output->mode.height = output_height;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400841 output->mode.refresh = 60000;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200842 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400843 wl_list_init(&output->base.mode_list);
844 wl_list_insert(&output->base.mode_list, &output->mode.link);
845
Giulio Camuffo954f1832014-10-11 18:27:30 +0300846 values[1] = b->null_cursor;
847 output->window = xcb_generate_id(b->conn);
Marko61b4d3e2015-10-05 17:27:54 -0500848 screen = x11_compositor_get_default_screen(b);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300849 xcb_create_window(b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400850 XCB_COPY_FROM_PARENT,
851 output->window,
Marko61b4d3e2015-10-05 17:27:54 -0500852 screen->root,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400853 0, 0,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200854 output_width, output_height,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400855 0,
856 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Marko61b4d3e2015-10-05 17:27:54 -0500857 screen->root_visual,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400858 mask, values);
859
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500860 if (fullscreen) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300861 atom_list[0] = b->atom.net_wm_state_fullscreen;
862 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500863 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300864 b->atom.net_wm_state,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500865 XCB_ATOM_ATOM, 32,
866 ARRAY_LENGTH(atom_list), atom_list);
867 } else {
868 /* Don't resize me. */
869 memset(&normal_hints, 0, sizeof normal_hints);
870 normal_hints.flags =
871 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200872 normal_hints.min_width = output_width;
873 normal_hints.min_height = output_height;
874 normal_hints.max_width = output_width;
875 normal_hints.max_height = output_height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300876 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
877 b->atom.wm_normal_hints,
878 b->atom.wm_size_hints, 32,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500879 sizeof normal_hints / 4,
880 (uint8_t *) &normal_hints);
881 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400882
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400883 /* Set window name. Don't bother with non-EWMH WMs. */
Benoit Gschwind5c2f20e2016-06-05 19:01:11 +0200884 if (configured_name) {
885 if (asprintf(&title, "%s - %s", name, configured_name) < 0)
886 title = NULL;
887 } else {
888 title = strdup(name);
889 }
890
891 if (title) {
892 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
893 b->atom.net_wm_name, b->atom.utf8_string, 8,
894 strlen(title), title);
895 free(title);
896 } else {
897 xcb_destroy_window(b->conn, output->window);
898 free(output);
899 return NULL;
900 }
901
Giulio Camuffo954f1832014-10-11 18:27:30 +0300902 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
903 b->atom.wm_class, b->atom.string, 8,
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500904 sizeof class, class);
905
Giulio Camuffo954f1832014-10-11 18:27:30 +0300906 x11_output_set_icon(b, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500907
Giulio Camuffo954f1832014-10-11 18:27:30 +0300908 x11_output_set_wm_protocols(b, output);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500909
Giulio Camuffo954f1832014-10-11 18:27:30 +0300910 xcb_map_window(b->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400911
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -0500912 if (fullscreen)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300913 x11_output_wait_for_map(b, output);
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400914
Jonas Ådahle5a12252013-04-05 23:07:11 +0200915 output->base.start_repaint_loop = x11_output_start_repaint_loop;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300916 if (b->use_pixman)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300917 output->base.repaint = x11_output_repaint_shm;
918 else
919 output->base.repaint = x11_output_repaint_gl;
Matt Roper361d2ad2011-08-29 13:52:23 -0700920 output->base.destroy = x11_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800921 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200922 output->base.set_backlight = NULL;
923 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800924 output->base.switch_mode = NULL;
Hardeningff39efa2013-09-18 23:56:35 +0200925 output->base.current_mode = &output->mode;
Jason Ekstrand22e16b62014-09-15 12:10:27 -0700926 output->base.make = "weston-X11";
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500927 output->base.model = "none";
Ander Conselvan de Oliveirad4ea71e2013-12-18 19:51:40 +0200928
929 if (configured_name)
930 output->base.name = strdup(configured_name);
931
Giulio Camuffo954f1832014-10-11 18:27:30 +0300932 width_mm = width * b->screen->width_in_millimeters /
933 b->screen->width_in_pixels;
934 height_mm = height * b->screen->height_in_millimeters /
935 b->screen->height_in_pixels;
936 weston_output_init(&output->base, b->compositor,
Kristian Høgsberg8404acb2014-03-18 12:15:42 -0700937 x, y, width_mm, height_mm, transform, scale);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100938
Giulio Camuffo954f1832014-10-11 18:27:30 +0300939 if (b->use_pixman) {
940 if (x11_output_init_shm(b, output,
Kristian Høgsbergb5a4f982013-10-23 23:38:50 -0700941 output->mode.width,
Bryce Harrington665b0252015-05-26 18:14:21 -0700942 output->mode.height) < 0) {
943 weston_log("Failed to initialize SHM for the X11 output\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300944 return NULL;
Bryce Harrington665b0252015-05-26 18:14:21 -0700945 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300946 if (pixman_renderer_output_create(&output->base) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700947 weston_log("Failed to create pixman renderer for output\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300948 x11_output_deinit_shm(b, output);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300949 return NULL;
950 }
951 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +0100952 /* eglCreatePlatformWindowSurfaceEXT takes a Window*
953 * but eglCreateWindowSurface takes a Window. */
954 Window xid = (Window) output->window;
955
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300956 ret = gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000957 (EGLNativeWindowType) output->window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100958 &xid,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000959 gl_renderer->opaque_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500960 NULL,
961 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300962 if (ret < 0)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300963 return NULL;
964 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100965
Giulio Camuffo954f1832014-10-11 18:27:30 +0300966 loop = wl_display_get_event_loop(b->compositor->wl_display);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100967 output->finish_frame_timer =
968 wl_event_loop_add_timer(loop, finish_frame_handler, output);
969
Giulio Camuffo954f1832014-10-11 18:27:30 +0300970 weston_compositor_add_output(b->compositor, &output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400971
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400972 weston_log("x11 output %dx%d, window id %d\n",
973 width, height, output->window);
974
Scott Moreau1bad5db2012-08-18 01:04:05 -0600975 return output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400976}
977
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400978static struct x11_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300979x11_backend_find_output(struct x11_backend *b, xcb_window_t window)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400980{
981 struct x11_output *output;
982
Giulio Camuffo954f1832014-10-11 18:27:30 +0300983 wl_list_for_each(output, &b->compositor->output_list, base.link) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400984 if (output->window == window)
985 return output;
986 }
987
Derek Foreman99bfa642014-12-11 15:44:46 -0600988 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400989}
990
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200991static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300992x11_backend_delete_window(struct x11_backend *b, xcb_window_t window)
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200993{
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +0200994 struct x11_output *output;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200995
Giulio Camuffo954f1832014-10-11 18:27:30 +0300996 output = x11_backend_find_output(b, window);
Derek Foreman99bfa642014-12-11 15:44:46 -0600997 if (output)
998 x11_output_destroy(&output->base);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200999
Giulio Camuffo954f1832014-10-11 18:27:30 +03001000 if (wl_list_empty(&b->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001001 weston_compositor_exit(b->compositor);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001002}
1003
Derek Foremand540f4b2015-01-27 16:26:49 -06001004static void delete_cb(void *data)
1005{
1006 struct window_delete_data *wd = data;
1007
Giulio Camuffo954f1832014-10-11 18:27:30 +03001008 x11_backend_delete_window(wd->backend, wd->window);
Derek Foremand540f4b2015-01-27 16:26:49 -06001009 free(wd);
1010}
1011
Daniel Stone154c34c2012-06-22 13:21:40 +01001012#ifdef HAVE_XCB_XKB
Daniel Stonee2faa122012-06-22 13:21:39 +01001013static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001014update_xkb_state(struct x11_backend *b, xcb_xkb_state_notify_event_t *state)
Daniel Stonee2faa122012-06-22 13:21:39 +01001015{
Derek Foreman1281a362015-07-31 16:55:32 -05001016 struct weston_keyboard *keyboard =
1017 weston_seat_get_keyboard(&b->core_seat);
1018
1019 xkb_state_update_mask(keyboard->xkb_state.state,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001020 get_xkb_mod_mask(b, state->baseMods),
1021 get_xkb_mod_mask(b, state->latchedMods),
1022 get_xkb_mod_mask(b, state->lockedMods),
Daniel Stonee2faa122012-06-22 13:21:39 +01001023 0,
1024 0,
1025 state->group);
1026
Giulio Camuffo954f1832014-10-11 18:27:30 +03001027 notify_modifiers(&b->core_seat,
1028 wl_display_next_serial(b->compositor->wl_display));
Daniel Stonee2faa122012-06-22 13:21:39 +01001029}
1030#endif
1031
Daniel Stone154c34c2012-06-22 13:21:40 +01001032/**
1033 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
1034 * the best we can do (given that XCB also lacks XI2 support), is to take
1035 * the state from the core key events. Unfortunately that only gives us
1036 * the effective (i.e. union of depressed/latched/locked) state, and we
1037 * need the granularity.
1038 *
1039 * So we still update the state with every key event we see, but also use
1040 * the state field from X11 events as a mask so we don't get any stuck
1041 * modifiers.
1042 */
1043static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001044update_xkb_state_from_core(struct x11_backend *b, uint16_t x11_mask)
Daniel Stone154c34c2012-06-22 13:21:40 +01001045{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001046 uint32_t mask = get_xkb_mod_mask(b, x11_mask);
Derek Foreman1281a362015-07-31 16:55:32 -05001047 struct weston_keyboard *keyboard
1048 = weston_seat_get_keyboard(&b->core_seat);
Daniel Stone154c34c2012-06-22 13:21:40 +01001049
Derek Foreman1281a362015-07-31 16:55:32 -05001050 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone154c34c2012-06-22 13:21:40 +01001051 keyboard->modifiers.mods_depressed & mask,
1052 keyboard->modifiers.mods_latched & mask,
1053 keyboard->modifiers.mods_locked & mask,
1054 0,
1055 0,
1056 (x11_mask >> 13) & 3);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001057 notify_modifiers(&b->core_seat,
1058 wl_display_next_serial(b->compositor->wl_display));
Daniel Stone154c34c2012-06-22 13:21:40 +01001059}
1060
Tiago Vignattia3a71622011-12-08 17:03:17 +02001061static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001062x11_backend_deliver_button_event(struct x11_backend *b,
1063 xcb_generic_event_t *event, int state)
Tiago Vignattia3a71622011-12-08 17:03:17 +02001064{
1065 xcb_button_press_event_t *button_event =
1066 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +01001067 uint32_t button;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001068 struct x11_output *output;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001069 struct weston_pointer_axis_event weston_event;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001070
Benoit Gschwind4ddc4cc2016-05-18 21:32:11 +02001071 assert(event->response_type == XCB_BUTTON_PRESS ||
1072 event->response_type == XCB_BUTTON_RELEASE);
1073
Giulio Camuffo954f1832014-10-11 18:27:30 +03001074 output = x11_backend_find_output(b, button_event->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001075 if (!output)
1076 return;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001077
1078 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001079 xcb_grab_pointer(b->conn, 0, output->window,
Kristian Høgsberg73308622012-10-30 11:04:52 -04001080 XCB_EVENT_MASK_BUTTON_PRESS |
1081 XCB_EVENT_MASK_BUTTON_RELEASE |
1082 XCB_EVENT_MASK_POINTER_MOTION |
1083 XCB_EVENT_MASK_ENTER_WINDOW |
1084 XCB_EVENT_MASK_LEAVE_WINDOW,
1085 XCB_GRAB_MODE_ASYNC,
1086 XCB_GRAB_MODE_ASYNC,
1087 output->window, XCB_CURSOR_NONE,
1088 button_event->time);
1089 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001090 xcb_ungrab_pointer(b->conn, button_event->time);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001091
Giulio Camuffo954f1832014-10-11 18:27:30 +03001092 if (!b->has_xkb)
1093 update_xkb_state_from_core(b, button_event->state);
Daniel Stone154c34c2012-06-22 13:21:40 +01001094
Tiago Vignattia3a71622011-12-08 17:03:17 +02001095 switch (button_event->detail) {
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001096 case 1:
1097 button = BTN_LEFT;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001098 break;
1099 case 2:
1100 button = BTN_MIDDLE;
1101 break;
1102 case 3:
1103 button = BTN_RIGHT;
1104 break;
1105 case 4:
Jonas Ådahl62efe202012-09-27 18:40:41 +02001106 /* Axis are measured in pixels, but the xcb events are discrete
1107 * steps. Therefore move the axis by some pixels every step. */
Peter Hutterer89b6a492016-01-18 15:58:17 +10001108 if (state) {
1109 weston_event.value = -DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001110 weston_event.discrete = -1;
1111 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001112 weston_event.axis =
1113 WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001114 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001115 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001116 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001117 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001118 }
Scott Moreau210d0792012-03-22 10:47:01 -06001119 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001120 case 5:
Peter Hutterer89b6a492016-01-18 15:58:17 +10001121 if (state) {
1122 weston_event.value = DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001123 weston_event.discrete = 1;
1124 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001125 weston_event.axis =
1126 WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001127 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001128 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001129 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001130 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001131 }
Scott Moreau210d0792012-03-22 10:47:01 -06001132 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001133 case 6:
Peter Hutterer89b6a492016-01-18 15:58:17 +10001134 if (state) {
1135 weston_event.value = -DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001136 weston_event.discrete = -1;
1137 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001138 weston_event.axis =
1139 WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001140 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001141 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001142 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001143 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001144 }
Scott Moreau210d0792012-03-22 10:47:01 -06001145 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001146 case 7:
Peter Hutterer89b6a492016-01-18 15:58:17 +10001147 if (state) {
1148 weston_event.value = DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001149 weston_event.discrete = 1;
1150 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001151 weston_event.axis =
1152 WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001153 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001154 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001155 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001156 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001157 }
Tiago Vignattia3a71622011-12-08 17:03:17 +02001158 return;
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001159 default:
1160 button = button_event->detail + BTN_SIDE - 8;
1161 break;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001162 }
1163
Giulio Camuffo954f1832014-10-11 18:27:30 +03001164 notify_button(&b->core_seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001165 weston_compositor_get_time(), button,
1166 state ? WL_POINTER_BUTTON_STATE_PRESSED :
1167 WL_POINTER_BUTTON_STATE_RELEASED);
Peter Hutterer87743e92016-01-18 16:38:22 +10001168 notify_pointer_frame(&b->core_seat);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001169}
1170
Scott Moreau1bad5db2012-08-18 01:04:05 -06001171static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001172x11_backend_deliver_motion_event(struct x11_backend *b,
1173 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001174{
1175 struct x11_output *output;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001176 double x, y;
Jonas Ådahld2510102014-10-05 21:39:14 +02001177 struct weston_pointer_motion_event motion_event = { 0 };
Scott Moreau1bad5db2012-08-18 01:04:05 -06001178 xcb_motion_notify_event_t *motion_notify =
1179 (xcb_motion_notify_event_t *) event;
1180
Giulio Camuffo954f1832014-10-11 18:27:30 +03001181 if (!b->has_xkb)
1182 update_xkb_state_from_core(b, motion_notify->state);
1183 output = x11_backend_find_output(b, motion_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001184 if (!output)
1185 return;
1186
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001187 weston_output_transform_coordinate(&output->base,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001188 motion_notify->event_x,
1189 motion_notify->event_y,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05001190 &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001191
Jonas Ådahld2510102014-10-05 21:39:14 +02001192 motion_event = (struct weston_pointer_motion_event) {
1193 .mask = WESTON_POINTER_MOTION_REL,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001194 .dx = x - b->prev_x,
1195 .dy = y - b->prev_y
Jonas Ådahld2510102014-10-05 21:39:14 +02001196 };
1197
Giulio Camuffo954f1832014-10-11 18:27:30 +03001198 notify_motion(&b->core_seat, weston_compositor_get_time(),
Jonas Ådahld2510102014-10-05 21:39:14 +02001199 &motion_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001200 notify_pointer_frame(&b->core_seat);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001201
Giulio Camuffo954f1832014-10-11 18:27:30 +03001202 b->prev_x = x;
1203 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001204}
1205
1206static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001207x11_backend_deliver_enter_event(struct x11_backend *b,
1208 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001209{
1210 struct x11_output *output;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001211 double x, y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001212
1213 xcb_enter_notify_event_t *enter_notify =
1214 (xcb_enter_notify_event_t *) event;
1215 if (enter_notify->state >= Button1Mask)
1216 return;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001217 if (!b->has_xkb)
1218 update_xkb_state_from_core(b, enter_notify->state);
1219 output = x11_backend_find_output(b, enter_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001220 if (!output)
1221 return;
1222
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001223 weston_output_transform_coordinate(&output->base,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001224 enter_notify->event_x,
1225 enter_notify->event_y, &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001226
Giulio Camuffo954f1832014-10-11 18:27:30 +03001227 notify_pointer_focus(&b->core_seat, &output->base, x, y);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001228
Giulio Camuffo954f1832014-10-11 18:27:30 +03001229 b->prev_x = x;
1230 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001231}
1232
Kristian Høgsbergee724822011-04-21 14:51:44 -04001233static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001234x11_backend_next_event(struct x11_backend *b,
1235 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -04001236{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001237 if (mask & WL_EVENT_READABLE) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001238 *event = xcb_poll_for_event(b->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -04001239 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001240#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Giulio Camuffo954f1832014-10-11 18:27:30 +03001241 *event = xcb_poll_for_queued_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001242#else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001243 *event = xcb_poll_for_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001244#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -04001245 }
1246
1247 return *event != NULL;
1248}
1249
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001250static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001251x11_backend_handle_event(int fd, uint32_t mask, void *data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001252{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001253 struct x11_backend *b = data;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001254 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001255 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001256 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001257 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001258 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001259 xcb_keymap_notify_event_t *keymap_notify;
1260 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001261 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001262 xcb_atom_t atom;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001263 xcb_window_t window;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001264 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001265 uint32_t i, set;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001266 uint8_t response_type;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001267 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001268
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001269 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001270 count = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001271 while (x11_backend_next_event(b, &event, mask)) {
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001272 response_type = event->response_type & ~0x80;
1273
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001274 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1275 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001276 /* Suppress key repeat events; this is only used if we
1277 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001278 key_release = (xcb_key_press_event_t *) prev;
1279 key_press = (xcb_key_press_event_t *) event;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001280 if (response_type == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -07001281 key_release->time == key_press->time &&
1282 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001283 /* Don't deliver the held key release
1284 * event or the new key press event. */
1285 free(event);
1286 free(prev);
1287 prev = NULL;
1288 continue;
1289 } else {
1290 /* Deliver the held key release now
1291 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001292 * event below. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001293 update_xkb_state_from_core(b, key_release->state);
1294 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001295 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001296 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001297 WL_KEYBOARD_KEY_STATE_RELEASED,
1298 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001299 free(prev);
1300 prev = NULL;
1301 break;
1302 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001303
1304 case XCB_FOCUS_IN:
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001305 assert(response_type == XCB_KEYMAP_NOTIFY);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001306 keymap_notify = (xcb_keymap_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001307 b->keys.size = 0;
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001308 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
1309 set = keymap_notify->keys[i >> 3] &
1310 (1 << (i & 7));
1311 if (set) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001312 k = wl_array_add(&b->keys, sizeof *k);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001313 *k = i;
1314 }
1315 }
1316
Daniel Stoned6da09e2012-06-22 13:21:29 +01001317 /* Unfortunately the state only comes with the enter
1318 * event, rather than with the focus event. I'm not
1319 * sure of the exact semantics around it and whether
1320 * we can ensure that we get both? */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001321 notify_keyboard_focus_in(&b->core_seat, &b->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001322 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001323
1324 free(prev);
1325 prev = NULL;
1326 break;
1327
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001328 default:
1329 /* No previous event held */
1330 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001331 }
1332
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001333 switch (response_type) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001334 case XCB_KEY_PRESS:
1335 key_press = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001336 if (!b->has_xkb)
1337 update_xkb_state_from_core(b, key_press->state);
1338 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001339 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001340 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001341 WL_KEYBOARD_KEY_STATE_PRESSED,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001342 b->has_xkb ? STATE_UPDATE_NONE :
Daniel Stonee2faa122012-06-22 13:21:39 +01001343 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001344 break;
1345 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001346 /* If we don't have XKB, we need to use the lame
1347 * autorepeat detection above. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001348 if (!b->has_xkb) {
Daniel Stone3ee91e12012-06-22 13:21:36 +01001349 prev = event;
1350 break;
1351 }
1352 key_release = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001353 notify_key(&b->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +01001354 weston_compositor_get_time(),
1355 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001356 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +01001357 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001358 break;
1359 case XCB_BUTTON_PRESS:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001360 x11_backend_deliver_button_event(b, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001361 break;
1362 case XCB_BUTTON_RELEASE:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001363 x11_backend_deliver_button_event(b, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001364 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001365 case XCB_MOTION_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001366 x11_backend_deliver_motion_event(b, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001367 break;
1368
1369 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001370 expose = (xcb_expose_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001371 output = x11_backend_find_output(b, expose->window);
Derek Foreman99bfa642014-12-11 15:44:46 -06001372 if (!output)
1373 break;
1374
Kristian Høgsberga98c2e12013-12-05 12:30:37 -08001375 weston_output_damage(&output->base);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001376 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001377 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001378
1379 case XCB_ENTER_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001380 x11_backend_deliver_enter_event(b, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001381 break;
1382
1383 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001384 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -05001385 if (enter_notify->state >= Button1Mask)
1386 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001387 if (!b->has_xkb)
1388 update_xkb_state_from_core(b, enter_notify->state);
1389 notify_pointer_focus(&b->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001390 break;
1391
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001392 case XCB_CLIENT_MESSAGE:
1393 client_message = (xcb_client_message_event_t *) event;
1394 atom = client_message->data.data32[0];
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001395 window = client_message->window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001396 if (atom == b->atom.wm_delete_window) {
Derek Foremand540f4b2015-01-27 16:26:49 -06001397 struct wl_event_loop *loop;
1398 struct window_delete_data *data = malloc(sizeof *data);
1399
1400 /* if malloc failed we should at least try to
1401 * delete the window, even if it may result in
1402 * a crash.
1403 */
1404 if (!data) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001405 x11_backend_delete_window(b, window);
Derek Foremand540f4b2015-01-27 16:26:49 -06001406 break;
1407 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001408 data->backend = b;
Derek Foremand540f4b2015-01-27 16:26:49 -06001409 data->window = window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001410 loop = wl_display_get_event_loop(b->compositor->wl_display);
Derek Foremand540f4b2015-01-27 16:26:49 -06001411 wl_event_loop_add_idle(loop, delete_cb, data);
1412 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001413 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001414
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001415 case XCB_FOCUS_IN:
1416 focus_in = (xcb_focus_in_event_t *) event;
1417 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1418 break;
1419
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001420 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001421 break;
1422
1423 case XCB_FOCUS_OUT:
1424 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -05001425 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1426 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001427 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001428 notify_keyboard_focus_out(&b->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001429 break;
1430
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001431 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001432 break;
1433 }
1434
Daniel Stonee2faa122012-06-22 13:21:39 +01001435#ifdef HAVE_XCB_XKB
Giulio Camuffo954f1832014-10-11 18:27:30 +03001436 if (b->has_xkb) {
1437 if (response_type == b->xkb_event_base) {
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001438 xcb_xkb_state_notify_event_t *state =
1439 (xcb_xkb_state_notify_event_t *) event;
1440 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001441 update_xkb_state(b, state);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001442 } else if (response_type == XCB_PROPERTY_NOTIFY) {
1443 xcb_property_notify_event_t *prop_notify =
1444 (xcb_property_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001445 if (prop_notify->window == b->screen->root &&
1446 prop_notify->atom == b->atom.xkb_names &&
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001447 prop_notify->state == XCB_PROPERTY_NEW_VALUE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001448 update_xkb_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001449 }
Daniel Stonee2faa122012-06-22 13:21:39 +01001450 }
1451#endif
1452
Bill Spitzakb715cec2012-06-05 17:08:23 -04001453 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001454 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001455 free (event);
1456 }
1457
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001458 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1459 case XCB_KEY_RELEASE:
1460 key_release = (xcb_key_press_event_t *) prev;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001461 update_xkb_state_from_core(b, key_release->state);
1462 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001463 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001464 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001465 WL_KEYBOARD_KEY_STATE_RELEASED,
1466 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001467 free(prev);
1468 prev = NULL;
1469 break;
1470 default:
1471 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001472 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001473
Bill Spitzakb715cec2012-06-05 17:08:23 -04001474 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001475}
1476
Giulio Camuffo954f1832014-10-11 18:27:30 +03001477#define F(field) offsetof(struct x11_backend, field)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001478
1479static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001480x11_backend_get_resources(struct x11_backend *b)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001481{
1482 static const struct { const char *name; int offset; } atoms[] = {
1483 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1484 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1485 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1486 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001487 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001488 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001489 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001490 { "_NET_WM_STATE", F(atom.net_wm_state) },
1491 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001492 { "_NET_SUPPORTING_WM_CHECK",
1493 F(atom.net_supporting_wm_check) },
1494 { "_NET_SUPPORTED", F(atom.net_supported) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001495 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001496 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001497 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001498 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001499 };
1500
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001501 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001502 xcb_intern_atom_reply_t *reply;
1503 xcb_pixmap_t pixmap;
1504 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001505 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001506 uint8_t data[] = { 0, 0, 0, 0 };
1507
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001508 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001509 cookies[i] = xcb_intern_atom (b->conn, 0,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001510 strlen(atoms[i].name),
1511 atoms[i].name);
1512
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001513 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001514 reply = xcb_intern_atom_reply (b->conn, cookies[i], NULL);
1515 *(xcb_atom_t *) ((char *) b + atoms[i].offset) = reply->atom;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001516 free(reply);
1517 }
1518
Giulio Camuffo954f1832014-10-11 18:27:30 +03001519 pixmap = xcb_generate_id(b->conn);
1520 gc = xcb_generate_id(b->conn);
1521 xcb_create_pixmap(b->conn, 1, pixmap, b->screen->root, 1, 1);
1522 xcb_create_gc(b->conn, gc, pixmap, 0, NULL);
1523 xcb_put_image(b->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001524 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001525 b->null_cursor = xcb_generate_id(b->conn);
1526 xcb_create_cursor (b->conn, b->null_cursor,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001527 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001528 xcb_free_gc(b->conn, gc);
1529 xcb_free_pixmap(b->conn, pixmap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001530}
1531
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001532static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001533x11_backend_get_wm_info(struct x11_backend *c)
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001534{
1535 xcb_get_property_cookie_t cookie;
1536 xcb_get_property_reply_t *reply;
1537 xcb_atom_t *atom;
1538 unsigned int i;
1539
1540 cookie = xcb_get_property(c->conn, 0, c->screen->root,
1541 c->atom.net_supported,
1542 XCB_ATOM_ATOM, 0, 1024);
1543 reply = xcb_get_property_reply(c->conn, cookie, NULL);
1544 if (reply == NULL)
1545 return;
1546
1547 atom = (xcb_atom_t *) xcb_get_property_value(reply);
1548
1549 for (i = 0; i < reply->value_len; i++) {
1550 if (atom[i] == c->atom.net_wm_state_fullscreen)
1551 c->has_net_wm_state_fullscreen = 1;
1552 }
Ryo Munakata76fb7ec2015-03-08 19:17:06 +09001553
1554 free(reply);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001555}
1556
1557static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001558x11_restore(struct weston_compositor *ec)
1559{
1560}
1561
1562static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001563x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001564{
Armin Krezović295e9d02016-08-01 19:17:58 +02001565 struct x11_backend *backend = to_x11_backend(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001566
Giulio Camuffo954f1832014-10-11 18:27:30 +03001567 wl_event_source_remove(backend->xcb_source);
1568 x11_input_destroy(backend);
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001569
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03001570 weston_compositor_shutdown(ec); /* destroys outputs, too */
1571
Giulio Camuffo954f1832014-10-11 18:27:30 +03001572 XCloseDisplay(backend->dpy);
1573 free(backend);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001574}
1575
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001576static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001577init_gl_renderer(struct x11_backend *b)
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001578{
1579 int ret;
1580
1581 gl_renderer = weston_load_module("gl-renderer.so",
1582 "gl_renderer_interface");
1583 if (!gl_renderer)
1584 return -1;
1585
Giulio Camuffo954f1832014-10-11 18:27:30 +03001586 ret = gl_renderer->create(b->compositor, EGL_PLATFORM_X11_KHR, (void *) b->dpy,
Derek Foremane76f1852015-05-15 12:12:39 -05001587 gl_renderer->opaque_attribs, NULL, 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001588
1589 return ret;
1590}
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001591
Giulio Camuffo954f1832014-10-11 18:27:30 +03001592static struct x11_backend *
1593x11_backend_create(struct weston_compositor *compositor,
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001594 struct weston_x11_backend_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001595{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001596 struct x11_backend *b;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001597 struct x11_output *output;
Pekka Paalanen157109b2016-03-24 15:43:44 +02001598 struct wl_event_loop *loop;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001599 int x = 0;
1600 unsigned i;
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001601
Giulio Camuffo954f1832014-10-11 18:27:30 +03001602 b = zalloc(sizeof *b);
1603 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001604 return NULL;
1605
Giulio Camuffo954f1832014-10-11 18:27:30 +03001606 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001607 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001608 goto err_free;
1609
Giulio Camuffo954f1832014-10-11 18:27:30 +03001610 b->dpy = XOpenDisplay(NULL);
1611 if (b->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001612 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001613
Giulio Camuffo954f1832014-10-11 18:27:30 +03001614 b->conn = XGetXCBConnection(b->dpy);
1615 XSetEventQueueOwner(b->dpy, XCBOwnsEventQueue);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001616
Giulio Camuffo954f1832014-10-11 18:27:30 +03001617 if (xcb_connection_has_error(b->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001618 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001619
Marko61b4d3e2015-10-05 17:27:54 -05001620 b->screen = x11_compositor_get_default_screen(b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001621 wl_array_init(&b->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001622
Giulio Camuffo954f1832014-10-11 18:27:30 +03001623 x11_backend_get_resources(b);
1624 x11_backend_get_wm_info(b);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001625
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001626 if (!b->has_net_wm_state_fullscreen && config->fullscreen) {
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001627 weston_log("Can not fullscreen without window manager support"
1628 "(need _NET_WM_STATE_FULLSCREEN)\n");
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001629 config->fullscreen = 0;
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001630 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001631
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001632 b->use_pixman = config->use_pixman;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001633 if (b->use_pixman) {
1634 if (pixman_renderer_init(compositor) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001635 weston_log("Failed to initialize pixman renderer for X11 backend\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001636 goto err_xdisplay;
Bryce Harrington665b0252015-05-26 18:14:21 -07001637 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001638 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001639 else if (init_gl_renderer(b) < 0) {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001640 goto err_xdisplay;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001641 }
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001642 weston_log("Using %s renderer\n", config->use_pixman ? "pixman" : "gl");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001643
Giulio Camuffo954f1832014-10-11 18:27:30 +03001644 b->base.destroy = x11_destroy;
1645 b->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001646
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001647 if (x11_input_create(b, config->no_input) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001648 weston_log("Failed to create X11 input\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001649 goto err_renderer;
Bryce Harrington665b0252015-05-26 18:14:21 -07001650 }
Daniel Stone22815f92012-06-22 13:21:34 +01001651
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001652 for (i = 0; i < config->num_outputs; ++i) {
1653 struct weston_x11_backend_output_config *output_iterator =
1654 &config->outputs[i];
Scott Moreau248aaec2012-08-03 14:19:52 -06001655
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001656 if (output_iterator->name == NULL) {
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001657 continue;
Rob Bradford5c4245d2013-07-26 16:29:44 +01001658 }
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001659
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001660 if (output_iterator->width < 1) {
1661 weston_log("Invalid width \"%d\" for output %s\n",
1662 output_iterator->width, output_iterator->name);
1663 goto err_x11_input;
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001664 }
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001665
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001666 if (output_iterator->height < 1) {
1667 weston_log("Invalid height \"%d\" for output %s\n",
1668 output_iterator->height, output_iterator->name);
1669 goto err_x11_input;
1670 }
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001671
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001672 output = x11_backend_create_output(b,
1673 x,
1674 0,
1675 output_iterator->width,
1676 output_iterator->height,
1677 config->fullscreen,
1678 config->no_input,
1679 output_iterator->name,
1680 output_iterator->transform,
1681 output_iterator->scale);
Bryce Harrington665b0252015-05-26 18:14:21 -07001682 if (output == NULL) {
1683 weston_log("Failed to create configured x11 output\n");
Scott Moreau248aaec2012-08-03 14:19:52 -06001684 goto err_x11_input;
Bryce Harrington665b0252015-05-26 18:14:21 -07001685 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06001686
1687 x = pixman_region32_extents(&output->base.region)->x2;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001688 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001689
Pekka Paalanen157109b2016-03-24 15:43:44 +02001690 loop = wl_display_get_event_loop(compositor->wl_display);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001691 b->xcb_source =
Pekka Paalanen157109b2016-03-24 15:43:44 +02001692 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001693 xcb_get_file_descriptor(b->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001694 WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001695 x11_backend_handle_event, b);
1696 wl_event_source_check(b->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001697
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001698 if (compositor->renderer->import_dmabuf) {
1699 if (linux_dmabuf_setup(compositor) < 0)
1700 weston_log("Error: initializing dmabuf "
1701 "support failed.\n");
1702 }
1703
Giulio Camuffo954f1832014-10-11 18:27:30 +03001704 compositor->backend = &b->base;
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001705
Giulio Camuffo954f1832014-10-11 18:27:30 +03001706 return b;
Martin Olsson11434bb2012-07-08 03:03:44 +02001707
1708err_x11_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001709 x11_input_destroy(b);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001710err_renderer:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001711 compositor->renderer->destroy(compositor);
Martin Olsson11434bb2012-07-08 03:03:44 +02001712err_xdisplay:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001713 XCloseDisplay(b->dpy);
Martin Olsson11434bb2012-07-08 03:03:44 +02001714err_free:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001715 free(b);
Martin Olsson11434bb2012-07-08 03:03:44 +02001716 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001717}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001718
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001719static void
1720config_init_to_defaults(struct weston_x11_backend_config *config)
1721{
1722}
1723
Giulio Camuffo954f1832014-10-11 18:27:30 +03001724WL_EXPORT int
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001725backend_init(struct weston_compositor *compositor,
Giulio Camuffo93daabb2015-10-17 19:24:14 +03001726 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001727{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001728 struct x11_backend *b;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001729 struct weston_x11_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001730
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001731 if (config_base == NULL ||
1732 config_base->struct_version != WESTON_X11_BACKEND_CONFIG_VERSION ||
1733 config_base->struct_size > sizeof(struct weston_x11_backend_config)) {
1734 weston_log("X11 backend config structure is invalid\n");
1735 return -1;
1736 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001737
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001738 config_init_to_defaults(&config);
1739 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001740
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001741 b = x11_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001742 if (b == NULL)
1743 return -1;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001744
Giulio Camuffo954f1832014-10-11 18:27:30 +03001745 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001746}