blob: 86241c671177ac1354676cae55cc5eb4fe640385 [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"
Armin Krezovićc3d2f962016-09-30 14:11:10 +020062#include "windowed-output-api.h"
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040063
Giulio Camuffo90a6fc62016-03-22 17:44:54 +020064#define DEFAULT_AXIS_STEP_DISTANCE 10
Jonas Ådahl62efe202012-09-27 18:40:41 +020065
Giulio Camuffo954f1832014-10-11 18:27:30 +030066struct x11_backend {
67 struct weston_backend base;
68 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040069
Benjamin Franzke3b288af2011-02-20 19:58:42 +010070 Display *dpy;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040071 xcb_connection_t *conn;
72 xcb_screen_t *screen;
73 xcb_cursor_t null_cursor;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050074 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040075 struct wl_event_source *xcb_source;
Daniel Stone576cd152012-06-01 12:14:02 +010076 struct xkb_keymap *xkb_keymap;
Daniel Stone62b33b62012-06-22 13:21:35 +010077 unsigned int has_xkb;
78 uint8_t xkb_event_base;
Armin Krezovićc3d2f962016-09-30 14:11:10 +020079 int fullscreen;
80 int no_input;
Vasily Khoruzhickd8943512013-01-07 22:36:02 +030081 int use_pixman;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040082
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -050083 int has_net_wm_state_fullscreen;
84
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040085 /* We could map multi-pointer X to multiple wayland seats, but
86 * for now we only support core X input. */
Kristian Høgsberg068b61c2013-02-25 17:04:47 -050087 struct weston_seat core_seat;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +020088 double prev_x;
89 double prev_y;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040090
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040091 struct {
92 xcb_atom_t wm_protocols;
93 xcb_atom_t wm_normal_hints;
94 xcb_atom_t wm_size_hints;
95 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050096 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040097 xcb_atom_t net_wm_name;
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -050098 xcb_atom_t net_supporting_wm_check;
99 xcb_atom_t net_supported;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500100 xcb_atom_t net_wm_icon;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400101 xcb_atom_t net_wm_state;
102 xcb_atom_t net_wm_state_fullscreen;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500103 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400104 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500105 xcb_atom_t cardinal;
Daniel Stone855028f2012-05-01 20:37:10 +0100106 xcb_atom_t xkb_names;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400107 } atom;
108};
109
110struct x11_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500111 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400112
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400113 xcb_window_t window;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500114 struct weston_mode mode;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400115 struct wl_event_source *finish_frame_timer;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300116
117 xcb_gc_t gc;
118 xcb_shm_seg_t segment;
119 pixman_image_t *hw_surface;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300120 int shm_id;
121 void *buf;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300122 uint8_t depth;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200123 int32_t scale;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400124};
125
Derek Foremand540f4b2015-01-27 16:26:49 -0600126struct window_delete_data {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300127 struct x11_backend *backend;
Derek Foremand540f4b2015-01-27 16:26:49 -0600128 xcb_window_t window;
129};
130
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300131struct gl_renderer_interface *gl_renderer;
132
Armin Krezović295e9d02016-08-01 19:17:58 +0200133static inline struct x11_output *
134to_x11_output(struct weston_output *base)
135{
136 return container_of(base, struct x11_output, base);
137}
138
139static inline struct x11_backend *
140to_x11_backend(struct weston_compositor *base)
141{
142 return container_of(base->backend, struct x11_backend, base);
143}
144
Marko61b4d3e2015-10-05 17:27:54 -0500145static xcb_screen_t *
146x11_compositor_get_default_screen(struct x11_backend *b)
147{
148 xcb_screen_iterator_t iter;
149 int i, screen_nbr = XDefaultScreen(b->dpy);
150
151 iter = xcb_setup_roots_iterator(xcb_get_setup(b->conn));
152 for (i = 0; iter.rem; xcb_screen_next(&iter), i++)
153 if (i == screen_nbr)
154 return iter.data;
155
156 return xcb_setup_roots_iterator(xcb_get_setup(b->conn)).data;
157}
158
Daniel Stone576cd152012-06-01 12:14:02 +0100159static struct xkb_keymap *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300160x11_backend_get_keymap(struct x11_backend *b)
Daniel Stone576cd152012-06-01 12:14:02 +0100161{
162 xcb_get_property_cookie_t cookie;
163 xcb_get_property_reply_t *reply;
Daniel Stone576cd152012-06-01 12:14:02 +0100164 struct xkb_rule_names names;
165 struct xkb_keymap *ret;
166 const char *value_all, *value_part;
167 int length_all, length_part;
168
169 memset(&names, 0, sizeof(names));
170
Giulio Camuffo954f1832014-10-11 18:27:30 +0300171 cookie = xcb_get_property(b->conn, 0, b->screen->root,
172 b->atom.xkb_names, b->atom.string, 0, 1024);
173 reply = xcb_get_property_reply(b->conn, cookie, NULL);
Daniel Stone576cd152012-06-01 12:14:02 +0100174 if (reply == NULL)
175 return NULL;
176
177 value_all = xcb_get_property_value(reply);
178 length_all = xcb_get_property_value_length(reply);
179 value_part = value_all;
180
181#define copy_prop_value(to) \
182 length_part = strlen(value_part); \
183 if (value_part + length_part < (value_all + length_all) && \
184 length_part > 0) \
185 names.to = value_part; \
186 value_part += length_part + 1;
187
188 copy_prop_value(rules);
189 copy_prop_value(model);
190 copy_prop_value(layout);
191 copy_prop_value(variant);
192 copy_prop_value(options);
193#undef copy_prop_value
194
Giulio Camuffo954f1832014-10-11 18:27:30 +0300195 ret = xkb_keymap_new_from_names(b->compositor->xkb_context, &names, 0);
Daniel Stone576cd152012-06-01 12:14:02 +0100196
197 free(reply);
198 return ret;
199}
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400200
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400201static uint32_t
Giulio Camuffo954f1832014-10-11 18:27:30 +0300202get_xkb_mod_mask(struct x11_backend *b, uint32_t in)
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400203{
Derek Foreman1281a362015-07-31 16:55:32 -0500204 struct weston_keyboard *keyboard =
205 weston_seat_get_keyboard(&b->core_seat);
206 struct weston_xkb_info *info = keyboard->xkb_info;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400207 uint32_t ret = 0;
208
209 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
210 ret |= (1 << info->shift_mod);
211 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
212 ret |= (1 << info->caps_mod);
213 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
214 ret |= (1 << info->ctrl_mod);
215 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
216 ret |= (1 << info->alt_mod);
217 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
218 ret |= (1 << info->mod2_mod);
219 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
220 ret |= (1 << info->mod3_mod);
221 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
222 ret |= (1 << info->super_mod);
223 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
224 ret |= (1 << info->mod5_mod);
225
226 return ret;
227}
228
Daniel Stone62b33b62012-06-22 13:21:35 +0100229static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300230x11_backend_setup_xkb(struct x11_backend *b)
Daniel Stone62b33b62012-06-22 13:21:35 +0100231{
232#ifndef HAVE_XCB_XKB
233 weston_log("XCB-XKB not available during build\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300234 b->has_xkb = 0;
235 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100236 return;
237#else
Derek Foreman1281a362015-07-31 16:55:32 -0500238 struct weston_keyboard *keyboard;
Daniel Stone62b33b62012-06-22 13:21:35 +0100239 const xcb_query_extension_reply_t *ext;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100240 xcb_generic_error_t *error;
Daniel Stonee2faa122012-06-22 13:21:39 +0100241 xcb_void_cookie_t select;
Ran Benita6a39d872012-10-31 20:14:57 +0200242 xcb_xkb_use_extension_cookie_t use_ext;
243 xcb_xkb_use_extension_reply_t *use_ext_reply;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100244 xcb_xkb_per_client_flags_cookie_t pcf;
245 xcb_xkb_per_client_flags_reply_t *pcf_reply;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400246 xcb_xkb_get_state_cookie_t state;
247 xcb_xkb_get_state_reply_t *state_reply;
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200248 uint32_t values[1] = { XCB_EVENT_MASK_PROPERTY_CHANGE };
Daniel Stone62b33b62012-06-22 13:21:35 +0100249
Giulio Camuffo954f1832014-10-11 18:27:30 +0300250 b->has_xkb = 0;
251 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100252
Giulio Camuffo954f1832014-10-11 18:27:30 +0300253 ext = xcb_get_extension_data(b->conn, &xcb_xkb_id);
Daniel Stone62b33b62012-06-22 13:21:35 +0100254 if (!ext) {
255 weston_log("XKB extension not available on host X11 server\n");
256 return;
257 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300258 b->xkb_event_base = ext->first_event;
Daniel Stone62b33b62012-06-22 13:21:35 +0100259
Giulio Camuffo954f1832014-10-11 18:27:30 +0300260 select = xcb_xkb_select_events_checked(b->conn,
Ran Benita424ae012012-10-31 00:13:08 +0200261 XCB_XKB_ID_USE_CORE_KBD,
262 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
263 0,
264 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
265 0,
266 0,
267 NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300268 error = xcb_request_check(b->conn, select);
Daniel Stonee2faa122012-06-22 13:21:39 +0100269 if (error) {
270 weston_log("error: failed to select for XKB state events\n");
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200271 free(error);
Daniel Stonee2faa122012-06-22 13:21:39 +0100272 return;
273 }
274
Giulio Camuffo954f1832014-10-11 18:27:30 +0300275 use_ext = xcb_xkb_use_extension(b->conn,
Ran Benita6a39d872012-10-31 20:14:57 +0200276 XCB_XKB_MAJOR_VERSION,
277 XCB_XKB_MINOR_VERSION);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300278 use_ext_reply = xcb_xkb_use_extension_reply(b->conn, use_ext, NULL);
Ran Benita6a39d872012-10-31 20:14:57 +0200279 if (!use_ext_reply) {
280 weston_log("couldn't start using XKB extension\n");
281 return;
282 }
283
284 if (!use_ext_reply->supported) {
285 weston_log("XKB extension version on the server is too old "
286 "(want %d.%d, has %d.%d)\n",
287 XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION,
288 use_ext_reply->serverMajor, use_ext_reply->serverMinor);
289 free(use_ext_reply);
290 return;
291 }
292 free(use_ext_reply);
293
Giulio Camuffo954f1832014-10-11 18:27:30 +0300294 pcf = xcb_xkb_per_client_flags(b->conn,
Daniel Stone3ee91e12012-06-22 13:21:36 +0100295 XCB_XKB_ID_USE_CORE_KBD,
296 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
297 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
298 0,
299 0,
300 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300301 pcf_reply = xcb_xkb_per_client_flags_reply(b->conn, pcf, NULL);
Ran Benita7109cc82012-10-31 20:14:58 +0200302 if (!pcf_reply ||
303 !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT)) {
Daniel Stone3ee91e12012-06-22 13:21:36 +0100304 weston_log("failed to set XKB per-client flags, not using "
305 "detectable repeat\n");
Ran Benita7109cc82012-10-31 20:14:58 +0200306 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100307 return;
308 }
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200309 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100310
Giulio Camuffo954f1832014-10-11 18:27:30 +0300311 state = xcb_xkb_get_state(b->conn, XCB_XKB_ID_USE_CORE_KBD);
312 state_reply = xcb_xkb_get_state_reply(b->conn, state, NULL);
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200313 if (!state_reply) {
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400314 weston_log("failed to get initial XKB state\n");
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400315 return;
316 }
317
Derek Foreman1281a362015-07-31 16:55:32 -0500318 keyboard = weston_seat_get_keyboard(&b->core_seat);
319 xkb_state_update_mask(keyboard->xkb_state.state,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300320 get_xkb_mod_mask(b, state_reply->baseMods),
321 get_xkb_mod_mask(b, state_reply->latchedMods),
322 get_xkb_mod_mask(b, state_reply->lockedMods),
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400323 0,
324 0,
325 state_reply->group);
326
327 free(state_reply);
328
Giulio Camuffo954f1832014-10-11 18:27:30 +0300329 xcb_change_window_attributes(b->conn, b->screen->root,
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200330 XCB_CW_EVENT_MASK, values);
331
Giulio Camuffo954f1832014-10-11 18:27:30 +0300332 b->has_xkb = 1;
Daniel Stone62b33b62012-06-22 13:21:35 +0100333#endif
334}
335
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200336#ifdef HAVE_XCB_XKB
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200337static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300338update_xkb_keymap(struct x11_backend *b)
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200339{
340 struct xkb_keymap *keymap;
341
Giulio Camuffo954f1832014-10-11 18:27:30 +0300342 keymap = x11_backend_get_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200343 if (!keymap) {
344 weston_log("failed to get XKB keymap\n");
345 return;
346 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300347 weston_seat_update_keymap(&b->core_seat, keymap);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200348 xkb_keymap_unref(keymap);
349}
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200350#endif
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200351
Darxus55973f22010-11-22 21:24:39 -0500352static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300353x11_input_create(struct x11_backend *b, int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400354{
Daniel Stone576cd152012-06-01 12:14:02 +0100355 struct xkb_keymap *keymap;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400356
Giulio Camuffo954f1832014-10-11 18:27:30 +0300357 weston_seat_init(&b->core_seat, b->compositor, "default");
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300358
359 if (no_input)
360 return 0;
361
Giulio Camuffo954f1832014-10-11 18:27:30 +0300362 weston_seat_init_pointer(&b->core_seat);
Daniel Stone576cd152012-06-01 12:14:02 +0100363
Giulio Camuffo954f1832014-10-11 18:27:30 +0300364 keymap = x11_backend_get_keymap(b);
365 if (weston_seat_init_keyboard(&b->core_seat, keymap) < 0)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500366 return -1;
Ran Benitac9c74152014-08-19 23:59:52 +0300367 xkb_keymap_unref(keymap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400368
Giulio Camuffo954f1832014-10-11 18:27:30 +0300369 x11_backend_setup_xkb(b);
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400370
Darxus55973f22010-11-22 21:24:39 -0500371 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400372}
373
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200374static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300375x11_input_destroy(struct x11_backend *b)
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200376{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300377 weston_seat_release(&b->core_seat);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200378}
379
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500380static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200381x11_output_start_repaint_loop(struct weston_output *output)
382{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400383 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200384
Pekka Paalanen662f3842015-03-18 12:17:26 +0200385 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200386 weston_output_finish_frame(output, &ts, WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200387}
388
David Herrmann1edf44c2013-10-22 17:11:26 +0200389static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300390x11_output_repaint_gl(struct weston_output *output_base,
391 pixman_region32_t *damage)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500392{
Armin Krezović295e9d02016-08-01 19:17:58 +0200393 struct x11_output *output = to_x11_output(output_base);
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400394 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500395
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400396 ec->renderer->repaint_output(output_base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500397
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200398 pixman_region32_subtract(&ec->primary_plane.damage,
399 &ec->primary_plane.damage, damage);
400
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500401 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200402 return 0;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500403}
404
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300405static void
Alexander Larsson80f91632013-05-22 14:41:38 +0200406set_clip_for_output(struct weston_output *output_base, pixman_region32_t *region)
407{
Armin Krezović295e9d02016-08-01 19:17:58 +0200408 struct x11_output *output = to_x11_output(output_base);
Alexander Larsson80f91632013-05-22 14:41:38 +0200409 struct weston_compositor *ec = output->base.compositor;
Armin Krezović295e9d02016-08-01 19:17:58 +0200410 struct x11_backend *b = to_x11_backend(ec);
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500411 pixman_region32_t transformed_region;
Alexander Larsson80f91632013-05-22 14:41:38 +0200412 pixman_box32_t *rects;
413 xcb_rectangle_t *output_rects;
Alexander Larsson80f91632013-05-22 14:41:38 +0200414 xcb_void_cookie_t cookie;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500415 int nrects, i;
Alexander Larsson80f91632013-05-22 14:41:38 +0200416 xcb_generic_error_t *err;
417
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500418 pixman_region32_init(&transformed_region);
419 pixman_region32_copy(&transformed_region, region);
420 pixman_region32_translate(&transformed_region,
421 -output_base->x, -output_base->y);
422 weston_transformed_region(output_base->width, output_base->height,
423 output_base->transform,
424 output_base->current_scale,
425 &transformed_region, &transformed_region);
426
427 rects = pixman_region32_rectangles(&transformed_region, &nrects);
Alexander Larsson80f91632013-05-22 14:41:38 +0200428 output_rects = calloc(nrects, sizeof(xcb_rectangle_t));
429
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500430 if (output_rects == NULL) {
431 pixman_region32_fini(&transformed_region);
Alexander Larsson80f91632013-05-22 14:41:38 +0200432 return;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500433 }
Alexander Larsson80f91632013-05-22 14:41:38 +0200434
435 for (i = 0; i < nrects; i++) {
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500436 output_rects[i].x = rects[i].x1;
437 output_rects[i].y = rects[i].y1;
438 output_rects[i].width = rects[i].x2 - rects[i].x1;
439 output_rects[i].height = rects[i].y2 - rects[i].y1;
Alexander Larsson80f91632013-05-22 14:41:38 +0200440 }
441
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500442 pixman_region32_fini(&transformed_region);
443
Giulio Camuffo954f1832014-10-11 18:27:30 +0300444 cookie = xcb_set_clip_rectangles_checked(b->conn, XCB_CLIP_ORDERING_UNSORTED,
Alexander Larsson80f91632013-05-22 14:41:38 +0200445 output->gc,
446 0, 0, nrects,
447 output_rects);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300448 err = xcb_request_check(b->conn, cookie);
Alexander Larsson80f91632013-05-22 14:41:38 +0200449 if (err != NULL) {
450 weston_log("Failed to set clip rects, err: %d\n", err->error_code);
451 free(err);
452 }
453 free(output_rects);
454}
455
456
David Herrmann1edf44c2013-10-22 17:11:26 +0200457static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300458x11_output_repaint_shm(struct weston_output *output_base,
459 pixman_region32_t *damage)
460{
Armin Krezović295e9d02016-08-01 19:17:58 +0200461 struct x11_output *output = to_x11_output(output_base);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300462 struct weston_compositor *ec = output->base.compositor;
Armin Krezović295e9d02016-08-01 19:17:58 +0200463 struct x11_backend *b = to_x11_backend(ec);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300464 xcb_void_cookie_t cookie;
465 xcb_generic_error_t *err;
466
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200467 pixman_renderer_output_set_buffer(output_base, output->hw_surface);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300468 ec->renderer->repaint_output(output_base, damage);
469
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300470 pixman_region32_subtract(&ec->primary_plane.damage,
471 &ec->primary_plane.damage, damage);
Alexander Larsson80f91632013-05-22 14:41:38 +0200472 set_clip_for_output(output_base, damage);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300473 cookie = xcb_shm_put_image_checked(b->conn, output->window, output->gc,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300474 pixman_image_get_width(output->hw_surface),
475 pixman_image_get_height(output->hw_surface),
476 0, 0,
477 pixman_image_get_width(output->hw_surface),
478 pixman_image_get_height(output->hw_surface),
479 0, 0, output->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
480 0, output->segment, 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300481 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300482 if (err != NULL) {
483 weston_log("Failed to put shm image, err: %d\n", err->error_code);
484 free(err);
485 }
486
487 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200488 return 0;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300489}
490
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100491static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400492finish_frame_handler(void *data)
493{
494 struct x11_output *output = data;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200495 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200496
Pekka Paalanen662f3842015-03-18 12:17:26 +0200497 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200498 weston_output_finish_frame(&output->base, &ts, 0);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400499
500 return 1;
501}
502
Matt Roper361d2ad2011-08-29 13:52:23 -0700503static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300504x11_output_deinit_shm(struct x11_backend *b, struct x11_output *output)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300505{
506 xcb_void_cookie_t cookie;
507 xcb_generic_error_t *err;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300508 xcb_free_gc(b->conn, output->gc);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300509
510 pixman_image_unref(output->hw_surface);
511 output->hw_surface = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300512 cookie = xcb_shm_detach_checked(b->conn, output->segment);
513 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300514 if (err) {
515 weston_log("xcb_shm_detach failed, error %d\n", err->error_code);
516 free(err);
517 }
518 shmdt(output->buf);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300519}
520
521static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300522x11_output_set_wm_protocols(struct x11_backend *b,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500523 struct x11_output *output)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400524{
525 xcb_atom_t list[1];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400526
Giulio Camuffo954f1832014-10-11 18:27:30 +0300527 list[0] = b->atom.wm_delete_window;
528 xcb_change_property (b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400529 XCB_PROP_MODE_REPLACE,
530 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300531 b->atom.wm_protocols,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400532 XCB_ATOM_ATOM,
533 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500534 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400535 list);
536}
537
538struct wm_normal_hints {
539 uint32_t flags;
540 uint32_t pad[4];
541 int32_t min_width, min_height;
542 int32_t max_width, max_height;
543 int32_t width_inc, height_inc;
544 int32_t min_aspect_x, min_aspect_y;
545 int32_t max_aspect_x, max_aspect_y;
546 int32_t base_width, base_height;
547 int32_t win_gravity;
548};
549
550#define WM_NORMAL_HINTS_MIN_SIZE 16
551#define WM_NORMAL_HINTS_MAX_SIZE 32
552
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500553static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300554x11_output_set_icon(struct x11_backend *b,
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400555 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500556{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400557 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400558 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400559 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500560
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400561 image = load_image(filename);
562 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500563 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400564 width = pixman_image_get_width(image);
565 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500566 icon = malloc(width * height * 4 + 8);
567 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400568 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500569 return;
570 }
571
572 icon[0] = width;
573 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400574 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300575 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
576 b->atom.net_wm_icon, b->atom.cardinal, 32,
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500577 width * height + 2, icon);
578 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400579 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500580}
581
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500582static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300583x11_output_wait_for_map(struct x11_backend *b, struct x11_output *output)
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500584{
585 xcb_map_notify_event_t *map_notify;
586 xcb_configure_notify_event_t *configure_notify;
587 xcb_generic_event_t *event;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500588 int mapped = 0, configured = 0;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500589 uint8_t response_type;
590
591 /* This isn't the nicest way to do this. Ideally, we could
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500592 * just go back to the main loop and once we get the configure
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500593 * notify, we add the output to the compositor. While we do
594 * support output hotplug, we can't start up with no outputs.
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500595 * We could add the output and then resize once we get the
596 * configure notify, but we don't want to start up and
597 * immediately resize the output.
598 *
599 * Also, some window managers don't give us our final
600 * fullscreen size before map_notify, so if we don't get a
601 * configure_notify before map_notify, we just wait for the
602 * first one and hope that's our size. */
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500603
Giulio Camuffo954f1832014-10-11 18:27:30 +0300604 xcb_flush(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500605
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500606 while (!mapped || !configured) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300607 event = xcb_wait_for_event(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500608 response_type = event->response_type & ~0x80;
609
610 switch (response_type) {
611 case XCB_MAP_NOTIFY:
612 map_notify = (xcb_map_notify_event_t *) event;
613 if (map_notify->window == output->window)
614 mapped = 1;
615 break;
616
617 case XCB_CONFIGURE_NOTIFY:
618 configure_notify =
619 (xcb_configure_notify_event_t *) event;
620
Alexander Larsson4ea95522013-05-22 14:41:37 +0200621
622 if (configure_notify->width % output->scale != 0 ||
623 configure_notify->height % output->scale != 0)
624 weston_log("Resolution is not a multiple of screen size, rounding\n");
Jason Ekstrandd89a0942014-03-07 15:29:14 -0600625 output->mode.width = configure_notify->width;
626 output->mode.height = configure_notify->height;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500627 configured = 1;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500628 break;
629 }
630 }
631}
632
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300633static xcb_visualtype_t *
634find_visual_by_id(xcb_screen_t *screen,
635 xcb_visualid_t id)
636{
637 xcb_depth_iterator_t i;
638 xcb_visualtype_iterator_t j;
639 for (i = xcb_screen_allowed_depths_iterator(screen);
640 i.rem;
641 xcb_depth_next(&i)) {
642 for (j = xcb_depth_visuals_iterator(i.data);
643 j.rem;
644 xcb_visualtype_next(&j)) {
645 if (j.data->visual_id == id)
646 return j.data;
647 }
648 }
649 return 0;
650}
651
652static uint8_t
653get_depth_of_visual(xcb_screen_t *screen,
654 xcb_visualid_t id)
655{
656 xcb_depth_iterator_t i;
657 xcb_visualtype_iterator_t j;
658 for (i = xcb_screen_allowed_depths_iterator(screen);
659 i.rem;
660 xcb_depth_next(&i)) {
661 for (j = xcb_depth_visuals_iterator(i.data);
662 j.rem;
663 xcb_visualtype_next(&j)) {
664 if (j.data->visual_id == id)
665 return i.data->depth;
666 }
667 }
668 return 0;
669}
670
671static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300672x11_output_init_shm(struct x11_backend *b, struct x11_output *output,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300673 int width, int height)
674{
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300675 xcb_visualtype_t *visual_type;
Marko61b4d3e2015-10-05 17:27:54 -0500676 xcb_screen_t *screen;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300677 xcb_format_iterator_t fmt;
678 xcb_void_cookie_t cookie;
679 xcb_generic_error_t *err;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300680 const xcb_query_extension_reply_t *ext;
681 int bitsperpixel = 0;
682 pixman_format_code_t pixman_format;
683
684 /* Check if SHM is available */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300685 ext = xcb_get_extension_data(b->conn, &xcb_shm_id);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300686 if (ext == NULL || !ext->present) {
687 /* SHM is missing */
688 weston_log("SHM extension is not available\n");
689 errno = ENOENT;
690 return -1;
691 }
692
Marko61b4d3e2015-10-05 17:27:54 -0500693 screen = x11_compositor_get_default_screen(b);
694 visual_type = find_visual_by_id(screen, screen->root_visual);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300695 if (!visual_type) {
696 weston_log("Failed to lookup visual for root window\n");
697 errno = ENOENT;
698 return -1;
699 }
700 weston_log("Found visual, bits per value: %d, red_mask: %.8x, green_mask: %.8x, blue_mask: %.8x\n",
701 visual_type->bits_per_rgb_value,
702 visual_type->red_mask,
703 visual_type->green_mask,
704 visual_type->blue_mask);
Marko61b4d3e2015-10-05 17:27:54 -0500705 output->depth = get_depth_of_visual(screen, screen->root_visual);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300706 weston_log("Visual depth is %d\n", output->depth);
707
Giulio Camuffo954f1832014-10-11 18:27:30 +0300708 for (fmt = xcb_setup_pixmap_formats_iterator(xcb_get_setup(b->conn));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300709 fmt.rem;
710 xcb_format_next(&fmt)) {
711 if (fmt.data->depth == output->depth) {
712 bitsperpixel = fmt.data->bits_per_pixel;
713 break;
714 }
715 }
716 weston_log("Found format for depth %d, bpp: %d\n",
717 output->depth, bitsperpixel);
718
719 if (bitsperpixel == 32 &&
720 visual_type->red_mask == 0xff0000 &&
721 visual_type->green_mask == 0x00ff00 &&
722 visual_type->blue_mask == 0x0000ff) {
723 weston_log("Will use x8r8g8b8 format for SHM surfaces\n");
724 pixman_format = PIXMAN_x8r8g8b8;
MoD1b55a592013-11-28 05:24:21 +0000725 } else if (bitsperpixel == 16 &&
726 visual_type->red_mask == 0x00f800 &&
727 visual_type->green_mask == 0x0007e0 &&
728 visual_type->blue_mask == 0x00001f) {
729 weston_log("Will use r5g6b5 format for SHM surfaces\n");
730 pixman_format = PIXMAN_r5g6b5;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300731 } else {
732 weston_log("Can't find appropriate format for SHM pixmap\n");
733 errno = ENOTSUP;
734 return -1;
735 }
736
737
738 /* Create SHM segment and attach it */
739 output->shm_id = shmget(IPC_PRIVATE, width * height * (bitsperpixel / 8), IPC_CREAT | S_IRWXU);
740 if (output->shm_id == -1) {
741 weston_log("x11shm: failed to allocate SHM segment\n");
742 return -1;
743 }
744 output->buf = shmat(output->shm_id, NULL, 0 /* read/write */);
745 if (-1 == (long)output->buf) {
746 weston_log("x11shm: failed to attach SHM segment\n");
747 return -1;
748 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300749 output->segment = xcb_generate_id(b->conn);
750 cookie = xcb_shm_attach_checked(b->conn, output->segment, output->shm_id, 1);
751 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300752 if (err) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700753 weston_log("x11shm: xcb_shm_attach error %d, op code %d, resource id %d\n",
754 err->error_code, err->major_code, err->minor_code);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300755 free(err);
756 return -1;
757 }
758
759 shmctl(output->shm_id, IPC_RMID, NULL);
760
761 /* Now create pixman image */
762 output->hw_surface = pixman_image_create_bits(pixman_format, width, height, output->buf,
763 width * (bitsperpixel / 8));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300764
Giulio Camuffo954f1832014-10-11 18:27:30 +0300765 output->gc = xcb_generate_id(b->conn);
766 xcb_create_gc(b->conn, output->gc, output->window, 0, NULL);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300767
768 return 0;
769}
770
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200771static int
772x11_output_disable(struct weston_output *base)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400773{
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200774 struct x11_output *output = to_x11_output(base);
775 struct x11_backend *backend = to_x11_backend(base->compositor);
776
777 if (!output->base.enabled)
778 return 0;
779
780 wl_event_source_remove(output->finish_frame_timer);
781
782 if (backend->use_pixman) {
783 pixman_renderer_output_destroy(&output->base);
784 x11_output_deinit_shm(backend, output);
785 } else {
786 gl_renderer->output_destroy(&output->base);
787 }
788
789 xcb_destroy_window(backend->conn, output->window);
790 xcb_flush(backend->conn);
791
792 return 0;
793}
794
795static void
796x11_output_destroy(struct weston_output *base)
797{
798 struct x11_output *output = to_x11_output(base);
799
800 x11_output_disable(&output->base);
801 weston_output_destroy(&output->base);
802
803 free(output);
804}
805
806static int
807x11_output_enable(struct weston_output *base)
808{
809 struct x11_output *output = to_x11_output(base);
810 struct x11_backend *b = to_x11_backend(base->compositor);
811
Scott Moreau1bad5db2012-08-18 01:04:05 -0600812 static const char name[] = "Weston Compositor";
Kristian Høgsberg86000402012-01-03 23:24:14 -0500813 static const char class[] = "weston-1\0Weston Compositor";
Benoit Gschwind5c2f20e2016-06-05 19:01:11 +0200814 char *title = NULL;
Marko61b4d3e2015-10-05 17:27:54 -0500815 xcb_screen_t *screen;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400816 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400817 struct wl_event_loop *loop;
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200818
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300819 int ret;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400820 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500821 xcb_atom_t atom_list[1];
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300822 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400823 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300824 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400825 0
826 };
827
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200828 if (!b->no_input)
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300829 values[0] |=
830 XCB_EVENT_MASK_KEY_PRESS |
831 XCB_EVENT_MASK_KEY_RELEASE |
832 XCB_EVENT_MASK_BUTTON_PRESS |
833 XCB_EVENT_MASK_BUTTON_RELEASE |
834 XCB_EVENT_MASK_POINTER_MOTION |
835 XCB_EVENT_MASK_ENTER_WINDOW |
836 XCB_EVENT_MASK_LEAVE_WINDOW |
837 XCB_EVENT_MASK_KEYMAP_STATE |
838 XCB_EVENT_MASK_FOCUS_CHANGE;
839
Giulio Camuffo954f1832014-10-11 18:27:30 +0300840 values[1] = b->null_cursor;
841 output->window = xcb_generate_id(b->conn);
Marko61b4d3e2015-10-05 17:27:54 -0500842 screen = x11_compositor_get_default_screen(b);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300843 xcb_create_window(b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400844 XCB_COPY_FROM_PARENT,
845 output->window,
Marko61b4d3e2015-10-05 17:27:54 -0500846 screen->root,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400847 0, 0,
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200848 output->base.current_mode->width,
849 output->base.current_mode->height,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400850 0,
851 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Marko61b4d3e2015-10-05 17:27:54 -0500852 screen->root_visual,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400853 mask, values);
854
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200855 if (b->fullscreen) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300856 atom_list[0] = b->atom.net_wm_state_fullscreen;
857 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500858 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300859 b->atom.net_wm_state,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500860 XCB_ATOM_ATOM, 32,
861 ARRAY_LENGTH(atom_list), atom_list);
862 } else {
863 /* Don't resize me. */
864 memset(&normal_hints, 0, sizeof normal_hints);
865 normal_hints.flags =
866 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200867 normal_hints.min_width = output->base.current_mode->width;
868 normal_hints.min_height = output->base.current_mode->height;
869 normal_hints.max_width = output->base.current_mode->width;
870 normal_hints.max_height = output->base.current_mode->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300871 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
872 b->atom.wm_normal_hints,
873 b->atom.wm_size_hints, 32,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500874 sizeof normal_hints / 4,
875 (uint8_t *) &normal_hints);
876 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400877
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400878 /* Set window name. Don't bother with non-EWMH WMs. */
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200879 if (output->base.name) {
880 if (asprintf(&title, "%s - %s", name, output->base.name) < 0)
Benoit Gschwind5c2f20e2016-06-05 19:01:11 +0200881 title = NULL;
882 } else {
883 title = strdup(name);
884 }
885
886 if (title) {
887 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
888 b->atom.net_wm_name, b->atom.utf8_string, 8,
889 strlen(title), title);
890 free(title);
891 } else {
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200892 goto err;
Benoit Gschwind5c2f20e2016-06-05 19:01:11 +0200893 }
894
Giulio Camuffo954f1832014-10-11 18:27:30 +0300895 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
896 b->atom.wm_class, b->atom.string, 8,
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500897 sizeof class, class);
898
Giulio Camuffo954f1832014-10-11 18:27:30 +0300899 x11_output_set_icon(b, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500900
Giulio Camuffo954f1832014-10-11 18:27:30 +0300901 x11_output_set_wm_protocols(b, output);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500902
Giulio Camuffo954f1832014-10-11 18:27:30 +0300903 xcb_map_window(b->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400904
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200905 if (b->fullscreen)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300906 x11_output_wait_for_map(b, output);
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400907
Giulio Camuffo954f1832014-10-11 18:27:30 +0300908 if (b->use_pixman) {
909 if (x11_output_init_shm(b, output,
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200910 output->base.current_mode->width,
911 output->base.current_mode->height) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700912 weston_log("Failed to initialize SHM for the X11 output\n");
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200913 goto err;
Bryce Harrington665b0252015-05-26 18:14:21 -0700914 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300915 if (pixman_renderer_output_create(&output->base) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700916 weston_log("Failed to create pixman renderer for output\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300917 x11_output_deinit_shm(b, output);
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200918 goto err;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300919 }
920 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +0100921 /* eglCreatePlatformWindowSurfaceEXT takes a Window*
922 * but eglCreateWindowSurface takes a Window. */
923 Window xid = (Window) output->window;
924
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300925 ret = gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000926 (EGLNativeWindowType) output->window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100927 &xid,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000928 gl_renderer->opaque_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500929 NULL,
930 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300931 if (ret < 0)
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200932 goto err;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300933 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100934
Giulio Camuffo954f1832014-10-11 18:27:30 +0300935 loop = wl_display_get_event_loop(b->compositor->wl_display);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100936 output->finish_frame_timer =
937 wl_event_loop_add_timer(loop, finish_frame_handler, output);
938
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400939 weston_log("x11 output %dx%d, window id %d\n",
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200940 output->base.current_mode->width,
941 output->base.current_mode->height,
942 output->window);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400943
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200944 return 0;
945
946err:
947 xcb_destroy_window(b->conn, output->window);
948 xcb_flush(b->conn);
949
950 return -1;
951}
952
953static int
954x11_output_set_size(struct weston_output *base, int width, int height)
955{
956 struct x11_output *output = to_x11_output(base);
957 struct x11_backend *b = to_x11_backend(base->compositor);
958 int output_width, output_height;
959
960 /* We can only be called once. */
961 assert(!output->base.current_mode);
962
963 /* Make sure we have scale set. */
964 assert(output->base.scale);
965
966 if (width < 1) {
967 weston_log("Invalid width \"%d\" for output %s\n",
968 width, output->base.name);
969 return -1;
970 }
971
972 if (height < 1) {
973 weston_log("Invalid height \"%d\" for output %s\n",
974 height, output->base.name);
975 return -1;
976 }
977
978 output_width = width * output->base.scale;
979 output_height = height * output->base.scale;
980
981 output->mode.flags =
982 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
983
984 output->mode.width = output_width;
985 output->mode.height = output_height;
986 output->mode.refresh = 60000;
987 output->scale = output->base.scale;
988 wl_list_init(&output->base.mode_list);
989 wl_list_insert(&output->base.mode_list, &output->mode.link);
990
991 output->base.current_mode = &output->mode;
992 output->base.make = "weston-X11";
993 output->base.model = "none";
994
995 output->base.mm_width = width * b->screen->width_in_millimeters /
996 b->screen->width_in_pixels;
997 output->base.mm_height = height * b->screen->height_in_millimeters /
998 b->screen->height_in_pixels;
999
1000 if (b->use_pixman)
1001 output->base.repaint = x11_output_repaint_shm;
1002 else
1003 output->base.repaint = x11_output_repaint_gl;
1004
1005 output->base.start_repaint_loop = x11_output_start_repaint_loop;
1006 output->base.assign_planes = NULL;
1007 output->base.set_backlight = NULL;
1008 output->base.set_dpms = NULL;
1009 output->base.switch_mode = NULL;
1010
1011 return 0;
1012}
1013
1014static int
1015x11_output_create(struct weston_compositor *compositor,
1016 const char *name)
1017{
1018 struct x11_output *output;
1019
1020 /* name can't be NULL. */
1021 assert(name);
1022
1023 output = zalloc(sizeof *output);
1024 if (output == NULL) {
1025 perror("zalloc");
1026 return -1;
1027 }
1028
1029 output->base.name = strdup(name);
1030 output->base.destroy = x11_output_destroy;
1031 output->base.disable = x11_output_disable;
1032 output->base.enable = x11_output_enable;
1033
1034 weston_output_init_pending(&output->base, compositor);
1035 weston_compositor_add_pending_output(&output->base, compositor);
1036
1037 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001038}
1039
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001040static struct x11_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001041x11_backend_find_output(struct x11_backend *b, xcb_window_t window)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001042{
1043 struct x11_output *output;
1044
Giulio Camuffo954f1832014-10-11 18:27:30 +03001045 wl_list_for_each(output, &b->compositor->output_list, base.link) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001046 if (output->window == window)
1047 return output;
1048 }
1049
Derek Foreman99bfa642014-12-11 15:44:46 -06001050 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001051}
1052
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001053static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001054x11_backend_delete_window(struct x11_backend *b, xcb_window_t window)
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001055{
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02001056 struct x11_output *output;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001057
Giulio Camuffo954f1832014-10-11 18:27:30 +03001058 output = x11_backend_find_output(b, window);
Derek Foreman99bfa642014-12-11 15:44:46 -06001059 if (output)
1060 x11_output_destroy(&output->base);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001061
Giulio Camuffo954f1832014-10-11 18:27:30 +03001062 if (wl_list_empty(&b->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001063 weston_compositor_exit(b->compositor);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001064}
1065
Derek Foremand540f4b2015-01-27 16:26:49 -06001066static void delete_cb(void *data)
1067{
1068 struct window_delete_data *wd = data;
1069
Giulio Camuffo954f1832014-10-11 18:27:30 +03001070 x11_backend_delete_window(wd->backend, wd->window);
Derek Foremand540f4b2015-01-27 16:26:49 -06001071 free(wd);
1072}
1073
Daniel Stone154c34c2012-06-22 13:21:40 +01001074#ifdef HAVE_XCB_XKB
Daniel Stonee2faa122012-06-22 13:21:39 +01001075static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001076update_xkb_state(struct x11_backend *b, xcb_xkb_state_notify_event_t *state)
Daniel Stonee2faa122012-06-22 13:21:39 +01001077{
Derek Foreman1281a362015-07-31 16:55:32 -05001078 struct weston_keyboard *keyboard =
1079 weston_seat_get_keyboard(&b->core_seat);
1080
1081 xkb_state_update_mask(keyboard->xkb_state.state,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001082 get_xkb_mod_mask(b, state->baseMods),
1083 get_xkb_mod_mask(b, state->latchedMods),
1084 get_xkb_mod_mask(b, state->lockedMods),
Daniel Stonee2faa122012-06-22 13:21:39 +01001085 0,
1086 0,
1087 state->group);
1088
Giulio Camuffo954f1832014-10-11 18:27:30 +03001089 notify_modifiers(&b->core_seat,
1090 wl_display_next_serial(b->compositor->wl_display));
Daniel Stonee2faa122012-06-22 13:21:39 +01001091}
1092#endif
1093
Daniel Stone154c34c2012-06-22 13:21:40 +01001094/**
1095 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
1096 * the best we can do (given that XCB also lacks XI2 support), is to take
1097 * the state from the core key events. Unfortunately that only gives us
1098 * the effective (i.e. union of depressed/latched/locked) state, and we
1099 * need the granularity.
1100 *
1101 * So we still update the state with every key event we see, but also use
1102 * the state field from X11 events as a mask so we don't get any stuck
1103 * modifiers.
1104 */
1105static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001106update_xkb_state_from_core(struct x11_backend *b, uint16_t x11_mask)
Daniel Stone154c34c2012-06-22 13:21:40 +01001107{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001108 uint32_t mask = get_xkb_mod_mask(b, x11_mask);
Derek Foreman1281a362015-07-31 16:55:32 -05001109 struct weston_keyboard *keyboard
1110 = weston_seat_get_keyboard(&b->core_seat);
Daniel Stone154c34c2012-06-22 13:21:40 +01001111
Derek Foreman1281a362015-07-31 16:55:32 -05001112 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone154c34c2012-06-22 13:21:40 +01001113 keyboard->modifiers.mods_depressed & mask,
1114 keyboard->modifiers.mods_latched & mask,
1115 keyboard->modifiers.mods_locked & mask,
1116 0,
1117 0,
1118 (x11_mask >> 13) & 3);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001119 notify_modifiers(&b->core_seat,
1120 wl_display_next_serial(b->compositor->wl_display));
Daniel Stone154c34c2012-06-22 13:21:40 +01001121}
1122
Tiago Vignattia3a71622011-12-08 17:03:17 +02001123static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001124x11_backend_deliver_button_event(struct x11_backend *b,
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001125 xcb_generic_event_t *event)
Tiago Vignattia3a71622011-12-08 17:03:17 +02001126{
1127 xcb_button_press_event_t *button_event =
1128 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +01001129 uint32_t button;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001130 struct x11_output *output;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001131 struct weston_pointer_axis_event weston_event;
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001132 bool is_button_pressed = event->response_type == XCB_BUTTON_PRESS;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001133
Benoit Gschwind4ddc4cc2016-05-18 21:32:11 +02001134 assert(event->response_type == XCB_BUTTON_PRESS ||
1135 event->response_type == XCB_BUTTON_RELEASE);
1136
Giulio Camuffo954f1832014-10-11 18:27:30 +03001137 output = x11_backend_find_output(b, button_event->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001138 if (!output)
1139 return;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001140
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001141 if (is_button_pressed)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001142 xcb_grab_pointer(b->conn, 0, output->window,
Kristian Høgsberg73308622012-10-30 11:04:52 -04001143 XCB_EVENT_MASK_BUTTON_PRESS |
1144 XCB_EVENT_MASK_BUTTON_RELEASE |
1145 XCB_EVENT_MASK_POINTER_MOTION |
1146 XCB_EVENT_MASK_ENTER_WINDOW |
1147 XCB_EVENT_MASK_LEAVE_WINDOW,
1148 XCB_GRAB_MODE_ASYNC,
1149 XCB_GRAB_MODE_ASYNC,
1150 output->window, XCB_CURSOR_NONE,
1151 button_event->time);
1152 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001153 xcb_ungrab_pointer(b->conn, button_event->time);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001154
Giulio Camuffo954f1832014-10-11 18:27:30 +03001155 if (!b->has_xkb)
1156 update_xkb_state_from_core(b, button_event->state);
Daniel Stone154c34c2012-06-22 13:21:40 +01001157
Tiago Vignattia3a71622011-12-08 17:03:17 +02001158 switch (button_event->detail) {
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001159 case 1:
1160 button = BTN_LEFT;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001161 break;
1162 case 2:
1163 button = BTN_MIDDLE;
1164 break;
1165 case 3:
1166 button = BTN_RIGHT;
1167 break;
1168 case 4:
Jonas Ådahl62efe202012-09-27 18:40:41 +02001169 /* Axis are measured in pixels, but the xcb events are discrete
1170 * steps. Therefore move the axis by some pixels every step. */
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001171 if (is_button_pressed) {
Peter Hutterer89b6a492016-01-18 15:58:17 +10001172 weston_event.value = -DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001173 weston_event.discrete = -1;
1174 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001175 weston_event.axis =
1176 WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001177 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001178 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001179 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001180 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001181 }
Scott Moreau210d0792012-03-22 10:47:01 -06001182 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001183 case 5:
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001184 if (is_button_pressed) {
Peter Hutterer89b6a492016-01-18 15:58:17 +10001185 weston_event.value = DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001186 weston_event.discrete = 1;
1187 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001188 weston_event.axis =
1189 WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001190 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001191 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001192 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001193 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001194 }
Scott Moreau210d0792012-03-22 10:47:01 -06001195 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001196 case 6:
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001197 if (is_button_pressed) {
Peter Hutterer89b6a492016-01-18 15:58:17 +10001198 weston_event.value = -DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001199 weston_event.discrete = -1;
1200 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001201 weston_event.axis =
1202 WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001203 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001204 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001205 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001206 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001207 }
Scott Moreau210d0792012-03-22 10:47:01 -06001208 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001209 case 7:
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001210 if (is_button_pressed) {
Peter Hutterer89b6a492016-01-18 15:58:17 +10001211 weston_event.value = DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001212 weston_event.discrete = 1;
1213 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001214 weston_event.axis =
1215 WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001216 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001217 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001218 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001219 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001220 }
Tiago Vignattia3a71622011-12-08 17:03:17 +02001221 return;
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001222 default:
1223 button = button_event->detail + BTN_SIDE - 8;
1224 break;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001225 }
1226
Giulio Camuffo954f1832014-10-11 18:27:30 +03001227 notify_button(&b->core_seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001228 weston_compositor_get_time(), button,
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001229 is_button_pressed ? WL_POINTER_BUTTON_STATE_PRESSED :
1230 WL_POINTER_BUTTON_STATE_RELEASED);
Peter Hutterer87743e92016-01-18 16:38:22 +10001231 notify_pointer_frame(&b->core_seat);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001232}
1233
Scott Moreau1bad5db2012-08-18 01:04:05 -06001234static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001235x11_backend_deliver_motion_event(struct x11_backend *b,
1236 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001237{
1238 struct x11_output *output;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001239 double x, y;
Jonas Ådahld2510102014-10-05 21:39:14 +02001240 struct weston_pointer_motion_event motion_event = { 0 };
Scott Moreau1bad5db2012-08-18 01:04:05 -06001241 xcb_motion_notify_event_t *motion_notify =
1242 (xcb_motion_notify_event_t *) event;
1243
Giulio Camuffo954f1832014-10-11 18:27:30 +03001244 if (!b->has_xkb)
1245 update_xkb_state_from_core(b, motion_notify->state);
1246 output = x11_backend_find_output(b, motion_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001247 if (!output)
1248 return;
1249
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001250 weston_output_transform_coordinate(&output->base,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001251 motion_notify->event_x,
1252 motion_notify->event_y,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05001253 &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001254
Jonas Ådahld2510102014-10-05 21:39:14 +02001255 motion_event = (struct weston_pointer_motion_event) {
1256 .mask = WESTON_POINTER_MOTION_REL,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001257 .dx = x - b->prev_x,
1258 .dy = y - b->prev_y
Jonas Ådahld2510102014-10-05 21:39:14 +02001259 };
1260
Giulio Camuffo954f1832014-10-11 18:27:30 +03001261 notify_motion(&b->core_seat, weston_compositor_get_time(),
Jonas Ådahld2510102014-10-05 21:39:14 +02001262 &motion_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001263 notify_pointer_frame(&b->core_seat);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001264
Giulio Camuffo954f1832014-10-11 18:27:30 +03001265 b->prev_x = x;
1266 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001267}
1268
1269static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001270x11_backend_deliver_enter_event(struct x11_backend *b,
1271 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001272{
1273 struct x11_output *output;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001274 double x, y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001275
1276 xcb_enter_notify_event_t *enter_notify =
1277 (xcb_enter_notify_event_t *) event;
1278 if (enter_notify->state >= Button1Mask)
1279 return;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001280 if (!b->has_xkb)
1281 update_xkb_state_from_core(b, enter_notify->state);
1282 output = x11_backend_find_output(b, enter_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001283 if (!output)
1284 return;
1285
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001286 weston_output_transform_coordinate(&output->base,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001287 enter_notify->event_x,
1288 enter_notify->event_y, &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001289
Giulio Camuffo954f1832014-10-11 18:27:30 +03001290 notify_pointer_focus(&b->core_seat, &output->base, x, y);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001291
Giulio Camuffo954f1832014-10-11 18:27:30 +03001292 b->prev_x = x;
1293 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001294}
1295
Kristian Høgsbergee724822011-04-21 14:51:44 -04001296static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001297x11_backend_next_event(struct x11_backend *b,
1298 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -04001299{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001300 if (mask & WL_EVENT_READABLE) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001301 *event = xcb_poll_for_event(b->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -04001302 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001303#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Giulio Camuffo954f1832014-10-11 18:27:30 +03001304 *event = xcb_poll_for_queued_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001305#else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001306 *event = xcb_poll_for_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001307#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -04001308 }
1309
1310 return *event != NULL;
1311}
1312
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001313static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001314x11_backend_handle_event(int fd, uint32_t mask, void *data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001315{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001316 struct x11_backend *b = data;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001317 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001318 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001319 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001320 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001321 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001322 xcb_keymap_notify_event_t *keymap_notify;
1323 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001324 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001325 xcb_atom_t atom;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001326 xcb_window_t window;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001327 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001328 uint32_t i, set;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001329 uint8_t response_type;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001330 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001331
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001332 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001333 count = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001334 while (x11_backend_next_event(b, &event, mask)) {
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001335 response_type = event->response_type & ~0x80;
1336
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001337 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1338 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001339 /* Suppress key repeat events; this is only used if we
1340 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001341 key_release = (xcb_key_press_event_t *) prev;
1342 key_press = (xcb_key_press_event_t *) event;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001343 if (response_type == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -07001344 key_release->time == key_press->time &&
1345 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001346 /* Don't deliver the held key release
1347 * event or the new key press event. */
1348 free(event);
1349 free(prev);
1350 prev = NULL;
1351 continue;
1352 } else {
1353 /* Deliver the held key release now
1354 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001355 * event below. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001356 update_xkb_state_from_core(b, key_release->state);
1357 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001358 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001359 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001360 WL_KEYBOARD_KEY_STATE_RELEASED,
1361 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001362 free(prev);
1363 prev = NULL;
1364 break;
1365 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001366
1367 case XCB_FOCUS_IN:
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001368 assert(response_type == XCB_KEYMAP_NOTIFY);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001369 keymap_notify = (xcb_keymap_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001370 b->keys.size = 0;
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001371 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
1372 set = keymap_notify->keys[i >> 3] &
1373 (1 << (i & 7));
1374 if (set) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001375 k = wl_array_add(&b->keys, sizeof *k);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001376 *k = i;
1377 }
1378 }
1379
Daniel Stoned6da09e2012-06-22 13:21:29 +01001380 /* Unfortunately the state only comes with the enter
1381 * event, rather than with the focus event. I'm not
1382 * sure of the exact semantics around it and whether
1383 * we can ensure that we get both? */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001384 notify_keyboard_focus_in(&b->core_seat, &b->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001385 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001386
1387 free(prev);
1388 prev = NULL;
1389 break;
1390
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001391 default:
1392 /* No previous event held */
1393 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001394 }
1395
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001396 switch (response_type) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001397 case XCB_KEY_PRESS:
1398 key_press = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001399 if (!b->has_xkb)
1400 update_xkb_state_from_core(b, key_press->state);
1401 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001402 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001403 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001404 WL_KEYBOARD_KEY_STATE_PRESSED,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001405 b->has_xkb ? STATE_UPDATE_NONE :
Daniel Stonee2faa122012-06-22 13:21:39 +01001406 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001407 break;
1408 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001409 /* If we don't have XKB, we need to use the lame
1410 * autorepeat detection above. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001411 if (!b->has_xkb) {
Daniel Stone3ee91e12012-06-22 13:21:36 +01001412 prev = event;
1413 break;
1414 }
1415 key_release = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001416 notify_key(&b->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +01001417 weston_compositor_get_time(),
1418 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001419 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +01001420 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001421 break;
1422 case XCB_BUTTON_PRESS:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001423 case XCB_BUTTON_RELEASE:
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001424 x11_backend_deliver_button_event(b, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001425 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001426 case XCB_MOTION_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001427 x11_backend_deliver_motion_event(b, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001428 break;
1429
1430 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001431 expose = (xcb_expose_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001432 output = x11_backend_find_output(b, expose->window);
Derek Foreman99bfa642014-12-11 15:44:46 -06001433 if (!output)
1434 break;
1435
Kristian Høgsberga98c2e12013-12-05 12:30:37 -08001436 weston_output_damage(&output->base);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001437 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001438 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001439
1440 case XCB_ENTER_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001441 x11_backend_deliver_enter_event(b, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001442 break;
1443
1444 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001445 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -05001446 if (enter_notify->state >= Button1Mask)
1447 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001448 if (!b->has_xkb)
1449 update_xkb_state_from_core(b, enter_notify->state);
1450 notify_pointer_focus(&b->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001451 break;
1452
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001453 case XCB_CLIENT_MESSAGE:
1454 client_message = (xcb_client_message_event_t *) event;
1455 atom = client_message->data.data32[0];
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001456 window = client_message->window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001457 if (atom == b->atom.wm_delete_window) {
Derek Foremand540f4b2015-01-27 16:26:49 -06001458 struct wl_event_loop *loop;
1459 struct window_delete_data *data = malloc(sizeof *data);
1460
1461 /* if malloc failed we should at least try to
1462 * delete the window, even if it may result in
1463 * a crash.
1464 */
1465 if (!data) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001466 x11_backend_delete_window(b, window);
Derek Foremand540f4b2015-01-27 16:26:49 -06001467 break;
1468 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001469 data->backend = b;
Derek Foremand540f4b2015-01-27 16:26:49 -06001470 data->window = window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001471 loop = wl_display_get_event_loop(b->compositor->wl_display);
Derek Foremand540f4b2015-01-27 16:26:49 -06001472 wl_event_loop_add_idle(loop, delete_cb, data);
1473 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001474 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001475
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001476 case XCB_FOCUS_IN:
1477 focus_in = (xcb_focus_in_event_t *) event;
1478 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1479 break;
1480
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001481 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001482 break;
1483
1484 case XCB_FOCUS_OUT:
1485 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -05001486 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1487 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001488 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001489 notify_keyboard_focus_out(&b->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001490 break;
1491
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001492 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001493 break;
1494 }
1495
Daniel Stonee2faa122012-06-22 13:21:39 +01001496#ifdef HAVE_XCB_XKB
Giulio Camuffo954f1832014-10-11 18:27:30 +03001497 if (b->has_xkb) {
1498 if (response_type == b->xkb_event_base) {
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001499 xcb_xkb_state_notify_event_t *state =
1500 (xcb_xkb_state_notify_event_t *) event;
1501 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001502 update_xkb_state(b, state);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001503 } else if (response_type == XCB_PROPERTY_NOTIFY) {
1504 xcb_property_notify_event_t *prop_notify =
1505 (xcb_property_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001506 if (prop_notify->window == b->screen->root &&
1507 prop_notify->atom == b->atom.xkb_names &&
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001508 prop_notify->state == XCB_PROPERTY_NEW_VALUE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001509 update_xkb_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001510 }
Daniel Stonee2faa122012-06-22 13:21:39 +01001511 }
1512#endif
1513
Bill Spitzakb715cec2012-06-05 17:08:23 -04001514 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001515 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001516 free (event);
1517 }
1518
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001519 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1520 case XCB_KEY_RELEASE:
1521 key_release = (xcb_key_press_event_t *) prev;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001522 update_xkb_state_from_core(b, key_release->state);
1523 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001524 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001525 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001526 WL_KEYBOARD_KEY_STATE_RELEASED,
1527 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001528 free(prev);
1529 prev = NULL;
1530 break;
1531 default:
1532 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001533 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001534
Bill Spitzakb715cec2012-06-05 17:08:23 -04001535 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001536}
1537
Giulio Camuffo954f1832014-10-11 18:27:30 +03001538#define F(field) offsetof(struct x11_backend, field)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001539
1540static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001541x11_backend_get_resources(struct x11_backend *b)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001542{
1543 static const struct { const char *name; int offset; } atoms[] = {
1544 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1545 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1546 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1547 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001548 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001549 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001550 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001551 { "_NET_WM_STATE", F(atom.net_wm_state) },
1552 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001553 { "_NET_SUPPORTING_WM_CHECK",
1554 F(atom.net_supporting_wm_check) },
1555 { "_NET_SUPPORTED", F(atom.net_supported) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001556 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001557 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001558 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001559 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001560 };
1561
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001562 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001563 xcb_intern_atom_reply_t *reply;
1564 xcb_pixmap_t pixmap;
1565 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001566 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001567 uint8_t data[] = { 0, 0, 0, 0 };
1568
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001569 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001570 cookies[i] = xcb_intern_atom (b->conn, 0,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001571 strlen(atoms[i].name),
1572 atoms[i].name);
1573
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001574 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001575 reply = xcb_intern_atom_reply (b->conn, cookies[i], NULL);
1576 *(xcb_atom_t *) ((char *) b + atoms[i].offset) = reply->atom;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001577 free(reply);
1578 }
1579
Giulio Camuffo954f1832014-10-11 18:27:30 +03001580 pixmap = xcb_generate_id(b->conn);
1581 gc = xcb_generate_id(b->conn);
1582 xcb_create_pixmap(b->conn, 1, pixmap, b->screen->root, 1, 1);
1583 xcb_create_gc(b->conn, gc, pixmap, 0, NULL);
1584 xcb_put_image(b->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001585 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001586 b->null_cursor = xcb_generate_id(b->conn);
1587 xcb_create_cursor (b->conn, b->null_cursor,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001588 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001589 xcb_free_gc(b->conn, gc);
1590 xcb_free_pixmap(b->conn, pixmap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001591}
1592
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001593static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001594x11_backend_get_wm_info(struct x11_backend *c)
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001595{
1596 xcb_get_property_cookie_t cookie;
1597 xcb_get_property_reply_t *reply;
1598 xcb_atom_t *atom;
1599 unsigned int i;
1600
1601 cookie = xcb_get_property(c->conn, 0, c->screen->root,
1602 c->atom.net_supported,
1603 XCB_ATOM_ATOM, 0, 1024);
1604 reply = xcb_get_property_reply(c->conn, cookie, NULL);
1605 if (reply == NULL)
1606 return;
1607
1608 atom = (xcb_atom_t *) xcb_get_property_value(reply);
1609
1610 for (i = 0; i < reply->value_len; i++) {
1611 if (atom[i] == c->atom.net_wm_state_fullscreen)
1612 c->has_net_wm_state_fullscreen = 1;
1613 }
Ryo Munakata76fb7ec2015-03-08 19:17:06 +09001614
1615 free(reply);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001616}
1617
1618static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001619x11_restore(struct weston_compositor *ec)
1620{
1621}
1622
1623static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001624x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001625{
Armin Krezović295e9d02016-08-01 19:17:58 +02001626 struct x11_backend *backend = to_x11_backend(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001627
Giulio Camuffo954f1832014-10-11 18:27:30 +03001628 wl_event_source_remove(backend->xcb_source);
1629 x11_input_destroy(backend);
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001630
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03001631 weston_compositor_shutdown(ec); /* destroys outputs, too */
1632
Giulio Camuffo954f1832014-10-11 18:27:30 +03001633 XCloseDisplay(backend->dpy);
1634 free(backend);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001635}
1636
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001637static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001638init_gl_renderer(struct x11_backend *b)
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001639{
1640 int ret;
1641
1642 gl_renderer = weston_load_module("gl-renderer.so",
1643 "gl_renderer_interface");
1644 if (!gl_renderer)
1645 return -1;
1646
Giulio Camuffo954f1832014-10-11 18:27:30 +03001647 ret = gl_renderer->create(b->compositor, EGL_PLATFORM_X11_KHR, (void *) b->dpy,
Derek Foremane76f1852015-05-15 12:12:39 -05001648 gl_renderer->opaque_attribs, NULL, 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001649
1650 return ret;
1651}
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001652
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001653static const struct weston_windowed_output_api api = {
1654 x11_output_set_size,
1655 x11_output_create,
1656};
1657
Giulio Camuffo954f1832014-10-11 18:27:30 +03001658static struct x11_backend *
1659x11_backend_create(struct weston_compositor *compositor,
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001660 struct weston_x11_backend_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001661{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001662 struct x11_backend *b;
Pekka Paalanen157109b2016-03-24 15:43:44 +02001663 struct wl_event_loop *loop;
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001664 int ret;
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001665
Giulio Camuffo954f1832014-10-11 18:27:30 +03001666 b = zalloc(sizeof *b);
1667 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001668 return NULL;
1669
Giulio Camuffo954f1832014-10-11 18:27:30 +03001670 b->compositor = compositor;
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001671 b->fullscreen = config->fullscreen;
1672 b->no_input = config->no_input;
1673
Giulio Camuffo954f1832014-10-11 18:27:30 +03001674 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001675 goto err_free;
1676
Giulio Camuffo954f1832014-10-11 18:27:30 +03001677 b->dpy = XOpenDisplay(NULL);
1678 if (b->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001679 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001680
Giulio Camuffo954f1832014-10-11 18:27:30 +03001681 b->conn = XGetXCBConnection(b->dpy);
1682 XSetEventQueueOwner(b->dpy, XCBOwnsEventQueue);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001683
Giulio Camuffo954f1832014-10-11 18:27:30 +03001684 if (xcb_connection_has_error(b->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001685 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001686
Marko61b4d3e2015-10-05 17:27:54 -05001687 b->screen = x11_compositor_get_default_screen(b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001688 wl_array_init(&b->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001689
Giulio Camuffo954f1832014-10-11 18:27:30 +03001690 x11_backend_get_resources(b);
1691 x11_backend_get_wm_info(b);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001692
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001693 if (!b->has_net_wm_state_fullscreen && config->fullscreen) {
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001694 weston_log("Can not fullscreen without window manager support"
1695 "(need _NET_WM_STATE_FULLSCREEN)\n");
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001696 config->fullscreen = 0;
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001697 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001698
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001699 b->use_pixman = config->use_pixman;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001700 if (b->use_pixman) {
1701 if (pixman_renderer_init(compositor) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001702 weston_log("Failed to initialize pixman renderer for X11 backend\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001703 goto err_xdisplay;
Bryce Harrington665b0252015-05-26 18:14:21 -07001704 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001705 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001706 else if (init_gl_renderer(b) < 0) {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001707 goto err_xdisplay;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001708 }
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001709 weston_log("Using %s renderer\n", config->use_pixman ? "pixman" : "gl");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001710
Giulio Camuffo954f1832014-10-11 18:27:30 +03001711 b->base.destroy = x11_destroy;
1712 b->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001713
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001714 if (x11_input_create(b, config->no_input) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001715 weston_log("Failed to create X11 input\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001716 goto err_renderer;
Bryce Harrington665b0252015-05-26 18:14:21 -07001717 }
Daniel Stone22815f92012-06-22 13:21:34 +01001718
Pekka Paalanen157109b2016-03-24 15:43:44 +02001719 loop = wl_display_get_event_loop(compositor->wl_display);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001720 b->xcb_source =
Pekka Paalanen157109b2016-03-24 15:43:44 +02001721 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001722 xcb_get_file_descriptor(b->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001723 WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001724 x11_backend_handle_event, b);
1725 wl_event_source_check(b->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001726
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001727 if (compositor->renderer->import_dmabuf) {
1728 if (linux_dmabuf_setup(compositor) < 0)
1729 weston_log("Error: initializing dmabuf "
1730 "support failed.\n");
1731 }
1732
Giulio Camuffo954f1832014-10-11 18:27:30 +03001733 compositor->backend = &b->base;
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001734
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001735 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
1736 &api, sizeof(api));
1737
1738 if (ret < 0) {
1739 weston_log("Failed to register output API.\n");
1740 goto err_x11_input;
1741 }
1742
Giulio Camuffo954f1832014-10-11 18:27:30 +03001743 return b;
Martin Olsson11434bb2012-07-08 03:03:44 +02001744
1745err_x11_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001746 x11_input_destroy(b);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001747err_renderer:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001748 compositor->renderer->destroy(compositor);
Martin Olsson11434bb2012-07-08 03:03:44 +02001749err_xdisplay:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001750 XCloseDisplay(b->dpy);
Martin Olsson11434bb2012-07-08 03:03:44 +02001751err_free:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001752 free(b);
Martin Olsson11434bb2012-07-08 03:03:44 +02001753 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001754}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001755
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001756static void
1757config_init_to_defaults(struct weston_x11_backend_config *config)
1758{
1759}
1760
Giulio Camuffo954f1832014-10-11 18:27:30 +03001761WL_EXPORT int
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001762backend_init(struct weston_compositor *compositor,
Giulio Camuffo93daabb2015-10-17 19:24:14 +03001763 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001764{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001765 struct x11_backend *b;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001766 struct weston_x11_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001767
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001768 if (config_base == NULL ||
1769 config_base->struct_version != WESTON_X11_BACKEND_CONFIG_VERSION ||
1770 config_base->struct_size > sizeof(struct weston_x11_backend_config)) {
1771 weston_log("X11 backend config structure is invalid\n");
1772 return -1;
1773 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001774
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001775 config_init_to_defaults(&config);
1776 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001777
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001778 b = x11_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001779 if (b == NULL)
1780 return -1;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001781
Giulio Camuffo954f1832014-10-11 18:27:30 +03001782 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001783}