blob: 02cdf3eaf580b8f236e5d448f7413b3dab8ba88c [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"
Vincent Abriouc9506672016-10-05 16:14:07 +020059#include "weston-egl-ext.h"
Benoit Gschwinde16acab2016-04-15 20:28:31 -070060#include "pixman-renderer.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020061#include "presentation-time-server-protocol.h"
Pekka Paalanen5ffb4402014-06-12 16:52:53 +030062#include "linux-dmabuf.h"
Armin Krezovićc3d2f962016-09-30 14:11:10 +020063#include "windowed-output-api.h"
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040064
Giulio Camuffo90a6fc62016-03-22 17:44:54 +020065#define DEFAULT_AXIS_STEP_DISTANCE 10
Jonas Ådahl62efe202012-09-27 18:40:41 +020066
Giulio Camuffo954f1832014-10-11 18:27:30 +030067struct x11_backend {
68 struct weston_backend base;
69 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040070
Benjamin Franzke3b288af2011-02-20 19:58:42 +010071 Display *dpy;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040072 xcb_connection_t *conn;
73 xcb_screen_t *screen;
74 xcb_cursor_t null_cursor;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050075 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040076 struct wl_event_source *xcb_source;
Daniel Stone576cd152012-06-01 12:14:02 +010077 struct xkb_keymap *xkb_keymap;
Daniel Stone62b33b62012-06-22 13:21:35 +010078 unsigned int has_xkb;
79 uint8_t xkb_event_base;
Armin Krezovićc3d2f962016-09-30 14:11:10 +020080 int fullscreen;
81 int no_input;
Vasily Khoruzhickd8943512013-01-07 22:36:02 +030082 int use_pixman;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040083
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -050084 int has_net_wm_state_fullscreen;
85
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040086 /* We could map multi-pointer X to multiple wayland seats, but
87 * for now we only support core X input. */
Kristian Høgsberg068b61c2013-02-25 17:04:47 -050088 struct weston_seat core_seat;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +020089 double prev_x;
90 double prev_y;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040091
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040092 struct {
93 xcb_atom_t wm_protocols;
94 xcb_atom_t wm_normal_hints;
95 xcb_atom_t wm_size_hints;
96 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050097 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040098 xcb_atom_t net_wm_name;
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -050099 xcb_atom_t net_supporting_wm_check;
100 xcb_atom_t net_supported;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500101 xcb_atom_t net_wm_icon;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400102 xcb_atom_t net_wm_state;
103 xcb_atom_t net_wm_state_fullscreen;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500104 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400105 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500106 xcb_atom_t cardinal;
Daniel Stone855028f2012-05-01 20:37:10 +0100107 xcb_atom_t xkb_names;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400108 } atom;
109};
110
111struct x11_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500112 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400113
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400114 xcb_window_t window;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500115 struct weston_mode mode;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400116 struct wl_event_source *finish_frame_timer;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300117
118 xcb_gc_t gc;
119 xcb_shm_seg_t segment;
120 pixman_image_t *hw_surface;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300121 int shm_id;
122 void *buf;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300123 uint8_t depth;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200124 int32_t scale;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400125};
126
Derek Foremand540f4b2015-01-27 16:26:49 -0600127struct window_delete_data {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300128 struct x11_backend *backend;
Derek Foremand540f4b2015-01-27 16:26:49 -0600129 xcb_window_t window;
130};
131
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300132struct gl_renderer_interface *gl_renderer;
133
Armin Krezović295e9d02016-08-01 19:17:58 +0200134static inline struct x11_output *
135to_x11_output(struct weston_output *base)
136{
137 return container_of(base, struct x11_output, base);
138}
139
140static inline struct x11_backend *
141to_x11_backend(struct weston_compositor *base)
142{
143 return container_of(base->backend, struct x11_backend, base);
144}
145
Marko61b4d3e2015-10-05 17:27:54 -0500146static xcb_screen_t *
147x11_compositor_get_default_screen(struct x11_backend *b)
148{
149 xcb_screen_iterator_t iter;
150 int i, screen_nbr = XDefaultScreen(b->dpy);
151
152 iter = xcb_setup_roots_iterator(xcb_get_setup(b->conn));
153 for (i = 0; iter.rem; xcb_screen_next(&iter), i++)
154 if (i == screen_nbr)
155 return iter.data;
156
157 return xcb_setup_roots_iterator(xcb_get_setup(b->conn)).data;
158}
159
Daniel Stone576cd152012-06-01 12:14:02 +0100160static struct xkb_keymap *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300161x11_backend_get_keymap(struct x11_backend *b)
Daniel Stone576cd152012-06-01 12:14:02 +0100162{
163 xcb_get_property_cookie_t cookie;
164 xcb_get_property_reply_t *reply;
Daniel Stone576cd152012-06-01 12:14:02 +0100165 struct xkb_rule_names names;
166 struct xkb_keymap *ret;
167 const char *value_all, *value_part;
168 int length_all, length_part;
169
170 memset(&names, 0, sizeof(names));
171
Giulio Camuffo954f1832014-10-11 18:27:30 +0300172 cookie = xcb_get_property(b->conn, 0, b->screen->root,
173 b->atom.xkb_names, b->atom.string, 0, 1024);
174 reply = xcb_get_property_reply(b->conn, cookie, NULL);
Daniel Stone576cd152012-06-01 12:14:02 +0100175 if (reply == NULL)
176 return NULL;
177
178 value_all = xcb_get_property_value(reply);
179 length_all = xcb_get_property_value_length(reply);
180 value_part = value_all;
181
182#define copy_prop_value(to) \
183 length_part = strlen(value_part); \
184 if (value_part + length_part < (value_all + length_all) && \
185 length_part > 0) \
186 names.to = value_part; \
187 value_part += length_part + 1;
188
189 copy_prop_value(rules);
190 copy_prop_value(model);
191 copy_prop_value(layout);
192 copy_prop_value(variant);
193 copy_prop_value(options);
194#undef copy_prop_value
195
Giulio Camuffo954f1832014-10-11 18:27:30 +0300196 ret = xkb_keymap_new_from_names(b->compositor->xkb_context, &names, 0);
Daniel Stone576cd152012-06-01 12:14:02 +0100197
198 free(reply);
199 return ret;
200}
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400201
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400202static uint32_t
Giulio Camuffo954f1832014-10-11 18:27:30 +0300203get_xkb_mod_mask(struct x11_backend *b, uint32_t in)
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400204{
Derek Foreman1281a362015-07-31 16:55:32 -0500205 struct weston_keyboard *keyboard =
206 weston_seat_get_keyboard(&b->core_seat);
207 struct weston_xkb_info *info = keyboard->xkb_info;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400208 uint32_t ret = 0;
209
210 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
211 ret |= (1 << info->shift_mod);
212 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
213 ret |= (1 << info->caps_mod);
214 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
215 ret |= (1 << info->ctrl_mod);
216 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
217 ret |= (1 << info->alt_mod);
218 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
219 ret |= (1 << info->mod2_mod);
220 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
221 ret |= (1 << info->mod3_mod);
222 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
223 ret |= (1 << info->super_mod);
224 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
225 ret |= (1 << info->mod5_mod);
226
227 return ret;
228}
229
Daniel Stone62b33b62012-06-22 13:21:35 +0100230static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300231x11_backend_setup_xkb(struct x11_backend *b)
Daniel Stone62b33b62012-06-22 13:21:35 +0100232{
233#ifndef HAVE_XCB_XKB
234 weston_log("XCB-XKB not available during build\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300235 b->has_xkb = 0;
236 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100237 return;
238#else
Derek Foreman1281a362015-07-31 16:55:32 -0500239 struct weston_keyboard *keyboard;
Daniel Stone62b33b62012-06-22 13:21:35 +0100240 const xcb_query_extension_reply_t *ext;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100241 xcb_generic_error_t *error;
Daniel Stonee2faa122012-06-22 13:21:39 +0100242 xcb_void_cookie_t select;
Ran Benita6a39d872012-10-31 20:14:57 +0200243 xcb_xkb_use_extension_cookie_t use_ext;
244 xcb_xkb_use_extension_reply_t *use_ext_reply;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100245 xcb_xkb_per_client_flags_cookie_t pcf;
246 xcb_xkb_per_client_flags_reply_t *pcf_reply;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400247 xcb_xkb_get_state_cookie_t state;
248 xcb_xkb_get_state_reply_t *state_reply;
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200249 uint32_t values[1] = { XCB_EVENT_MASK_PROPERTY_CHANGE };
Daniel Stone62b33b62012-06-22 13:21:35 +0100250
Giulio Camuffo954f1832014-10-11 18:27:30 +0300251 b->has_xkb = 0;
252 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100253
Giulio Camuffo954f1832014-10-11 18:27:30 +0300254 ext = xcb_get_extension_data(b->conn, &xcb_xkb_id);
Daniel Stone62b33b62012-06-22 13:21:35 +0100255 if (!ext) {
256 weston_log("XKB extension not available on host X11 server\n");
257 return;
258 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300259 b->xkb_event_base = ext->first_event;
Daniel Stone62b33b62012-06-22 13:21:35 +0100260
Giulio Camuffo954f1832014-10-11 18:27:30 +0300261 select = xcb_xkb_select_events_checked(b->conn,
Ran Benita424ae012012-10-31 00:13:08 +0200262 XCB_XKB_ID_USE_CORE_KBD,
263 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
264 0,
265 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
266 0,
267 0,
268 NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300269 error = xcb_request_check(b->conn, select);
Daniel Stonee2faa122012-06-22 13:21:39 +0100270 if (error) {
271 weston_log("error: failed to select for XKB state events\n");
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200272 free(error);
Daniel Stonee2faa122012-06-22 13:21:39 +0100273 return;
274 }
275
Giulio Camuffo954f1832014-10-11 18:27:30 +0300276 use_ext = xcb_xkb_use_extension(b->conn,
Ran Benita6a39d872012-10-31 20:14:57 +0200277 XCB_XKB_MAJOR_VERSION,
278 XCB_XKB_MINOR_VERSION);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300279 use_ext_reply = xcb_xkb_use_extension_reply(b->conn, use_ext, NULL);
Ran Benita6a39d872012-10-31 20:14:57 +0200280 if (!use_ext_reply) {
281 weston_log("couldn't start using XKB extension\n");
282 return;
283 }
284
285 if (!use_ext_reply->supported) {
286 weston_log("XKB extension version on the server is too old "
287 "(want %d.%d, has %d.%d)\n",
288 XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION,
289 use_ext_reply->serverMajor, use_ext_reply->serverMinor);
290 free(use_ext_reply);
291 return;
292 }
293 free(use_ext_reply);
294
Giulio Camuffo954f1832014-10-11 18:27:30 +0300295 pcf = xcb_xkb_per_client_flags(b->conn,
Daniel Stone3ee91e12012-06-22 13:21:36 +0100296 XCB_XKB_ID_USE_CORE_KBD,
297 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
298 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
299 0,
300 0,
301 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300302 pcf_reply = xcb_xkb_per_client_flags_reply(b->conn, pcf, NULL);
Ran Benita7109cc82012-10-31 20:14:58 +0200303 if (!pcf_reply ||
304 !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT)) {
Daniel Stone3ee91e12012-06-22 13:21:36 +0100305 weston_log("failed to set XKB per-client flags, not using "
306 "detectable repeat\n");
Ran Benita7109cc82012-10-31 20:14:58 +0200307 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100308 return;
309 }
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200310 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100311
Giulio Camuffo954f1832014-10-11 18:27:30 +0300312 state = xcb_xkb_get_state(b->conn, XCB_XKB_ID_USE_CORE_KBD);
313 state_reply = xcb_xkb_get_state_reply(b->conn, state, NULL);
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200314 if (!state_reply) {
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400315 weston_log("failed to get initial XKB state\n");
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400316 return;
317 }
318
Derek Foreman1281a362015-07-31 16:55:32 -0500319 keyboard = weston_seat_get_keyboard(&b->core_seat);
320 xkb_state_update_mask(keyboard->xkb_state.state,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300321 get_xkb_mod_mask(b, state_reply->baseMods),
322 get_xkb_mod_mask(b, state_reply->latchedMods),
323 get_xkb_mod_mask(b, state_reply->lockedMods),
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400324 0,
325 0,
326 state_reply->group);
327
328 free(state_reply);
329
Giulio Camuffo954f1832014-10-11 18:27:30 +0300330 xcb_change_window_attributes(b->conn, b->screen->root,
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200331 XCB_CW_EVENT_MASK, values);
332
Giulio Camuffo954f1832014-10-11 18:27:30 +0300333 b->has_xkb = 1;
Daniel Stone62b33b62012-06-22 13:21:35 +0100334#endif
335}
336
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200337#ifdef HAVE_XCB_XKB
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200338static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300339update_xkb_keymap(struct x11_backend *b)
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200340{
341 struct xkb_keymap *keymap;
342
Giulio Camuffo954f1832014-10-11 18:27:30 +0300343 keymap = x11_backend_get_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200344 if (!keymap) {
345 weston_log("failed to get XKB keymap\n");
346 return;
347 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300348 weston_seat_update_keymap(&b->core_seat, keymap);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200349 xkb_keymap_unref(keymap);
350}
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200351#endif
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200352
Darxus55973f22010-11-22 21:24:39 -0500353static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300354x11_input_create(struct x11_backend *b, int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400355{
Daniel Stone576cd152012-06-01 12:14:02 +0100356 struct xkb_keymap *keymap;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400357
Giulio Camuffo954f1832014-10-11 18:27:30 +0300358 weston_seat_init(&b->core_seat, b->compositor, "default");
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300359
360 if (no_input)
361 return 0;
362
Giulio Camuffo954f1832014-10-11 18:27:30 +0300363 weston_seat_init_pointer(&b->core_seat);
Daniel Stone576cd152012-06-01 12:14:02 +0100364
Giulio Camuffo954f1832014-10-11 18:27:30 +0300365 keymap = x11_backend_get_keymap(b);
366 if (weston_seat_init_keyboard(&b->core_seat, keymap) < 0)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500367 return -1;
Ran Benitac9c74152014-08-19 23:59:52 +0300368 xkb_keymap_unref(keymap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400369
Giulio Camuffo954f1832014-10-11 18:27:30 +0300370 x11_backend_setup_xkb(b);
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400371
Darxus55973f22010-11-22 21:24:39 -0500372 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400373}
374
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200375static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300376x11_input_destroy(struct x11_backend *b)
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200377{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300378 weston_seat_release(&b->core_seat);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200379}
380
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500381static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200382x11_output_start_repaint_loop(struct weston_output *output)
383{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400384 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200385
Pekka Paalanen662f3842015-03-18 12:17:26 +0200386 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200387 weston_output_finish_frame(output, &ts, WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200388}
389
David Herrmann1edf44c2013-10-22 17:11:26 +0200390static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300391x11_output_repaint_gl(struct weston_output *output_base,
Daniel Stoneb1f166d2017-03-01 11:34:10 +0000392 pixman_region32_t *damage,
393 void *repaint_data)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500394{
Armin Krezović295e9d02016-08-01 19:17:58 +0200395 struct x11_output *output = to_x11_output(output_base);
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400396 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500397
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400398 ec->renderer->repaint_output(output_base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500399
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200400 pixman_region32_subtract(&ec->primary_plane.damage,
401 &ec->primary_plane.damage, damage);
402
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500403 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200404 return 0;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500405}
406
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300407static void
Alexander Larsson80f91632013-05-22 14:41:38 +0200408set_clip_for_output(struct weston_output *output_base, pixman_region32_t *region)
409{
Armin Krezović295e9d02016-08-01 19:17:58 +0200410 struct x11_output *output = to_x11_output(output_base);
Alexander Larsson80f91632013-05-22 14:41:38 +0200411 struct weston_compositor *ec = output->base.compositor;
Armin Krezović295e9d02016-08-01 19:17:58 +0200412 struct x11_backend *b = to_x11_backend(ec);
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500413 pixman_region32_t transformed_region;
Alexander Larsson80f91632013-05-22 14:41:38 +0200414 pixman_box32_t *rects;
415 xcb_rectangle_t *output_rects;
Alexander Larsson80f91632013-05-22 14:41:38 +0200416 xcb_void_cookie_t cookie;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500417 int nrects, i;
Alexander Larsson80f91632013-05-22 14:41:38 +0200418 xcb_generic_error_t *err;
419
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500420 pixman_region32_init(&transformed_region);
421 pixman_region32_copy(&transformed_region, region);
422 pixman_region32_translate(&transformed_region,
423 -output_base->x, -output_base->y);
424 weston_transformed_region(output_base->width, output_base->height,
425 output_base->transform,
426 output_base->current_scale,
427 &transformed_region, &transformed_region);
428
429 rects = pixman_region32_rectangles(&transformed_region, &nrects);
Alexander Larsson80f91632013-05-22 14:41:38 +0200430 output_rects = calloc(nrects, sizeof(xcb_rectangle_t));
431
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500432 if (output_rects == NULL) {
433 pixman_region32_fini(&transformed_region);
Alexander Larsson80f91632013-05-22 14:41:38 +0200434 return;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500435 }
Alexander Larsson80f91632013-05-22 14:41:38 +0200436
437 for (i = 0; i < nrects; i++) {
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500438 output_rects[i].x = rects[i].x1;
439 output_rects[i].y = rects[i].y1;
440 output_rects[i].width = rects[i].x2 - rects[i].x1;
441 output_rects[i].height = rects[i].y2 - rects[i].y1;
Alexander Larsson80f91632013-05-22 14:41:38 +0200442 }
443
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500444 pixman_region32_fini(&transformed_region);
445
Giulio Camuffo954f1832014-10-11 18:27:30 +0300446 cookie = xcb_set_clip_rectangles_checked(b->conn, XCB_CLIP_ORDERING_UNSORTED,
Alexander Larsson80f91632013-05-22 14:41:38 +0200447 output->gc,
448 0, 0, nrects,
449 output_rects);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300450 err = xcb_request_check(b->conn, cookie);
Alexander Larsson80f91632013-05-22 14:41:38 +0200451 if (err != NULL) {
452 weston_log("Failed to set clip rects, err: %d\n", err->error_code);
453 free(err);
454 }
455 free(output_rects);
456}
457
458
David Herrmann1edf44c2013-10-22 17:11:26 +0200459static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300460x11_output_repaint_shm(struct weston_output *output_base,
Daniel Stoneb1f166d2017-03-01 11:34:10 +0000461 pixman_region32_t *damage,
462 void *repaint_data)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300463{
Armin Krezović295e9d02016-08-01 19:17:58 +0200464 struct x11_output *output = to_x11_output(output_base);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300465 struct weston_compositor *ec = output->base.compositor;
Armin Krezović295e9d02016-08-01 19:17:58 +0200466 struct x11_backend *b = to_x11_backend(ec);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300467 xcb_void_cookie_t cookie;
468 xcb_generic_error_t *err;
469
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200470 pixman_renderer_output_set_buffer(output_base, output->hw_surface);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300471 ec->renderer->repaint_output(output_base, damage);
472
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300473 pixman_region32_subtract(&ec->primary_plane.damage,
474 &ec->primary_plane.damage, damage);
Alexander Larsson80f91632013-05-22 14:41:38 +0200475 set_clip_for_output(output_base, damage);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300476 cookie = xcb_shm_put_image_checked(b->conn, output->window, output->gc,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300477 pixman_image_get_width(output->hw_surface),
478 pixman_image_get_height(output->hw_surface),
479 0, 0,
480 pixman_image_get_width(output->hw_surface),
481 pixman_image_get_height(output->hw_surface),
482 0, 0, output->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
483 0, output->segment, 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300484 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300485 if (err != NULL) {
486 weston_log("Failed to put shm image, err: %d\n", err->error_code);
487 free(err);
488 }
489
490 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200491 return 0;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300492}
493
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100494static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400495finish_frame_handler(void *data)
496{
497 struct x11_output *output = data;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200498 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200499
Pekka Paalanen662f3842015-03-18 12:17:26 +0200500 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200501 weston_output_finish_frame(&output->base, &ts, 0);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400502
503 return 1;
504}
505
Matt Roper361d2ad2011-08-29 13:52:23 -0700506static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300507x11_output_deinit_shm(struct x11_backend *b, struct x11_output *output)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300508{
509 xcb_void_cookie_t cookie;
510 xcb_generic_error_t *err;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300511 xcb_free_gc(b->conn, output->gc);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300512
513 pixman_image_unref(output->hw_surface);
514 output->hw_surface = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300515 cookie = xcb_shm_detach_checked(b->conn, output->segment);
516 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300517 if (err) {
518 weston_log("xcb_shm_detach failed, error %d\n", err->error_code);
519 free(err);
520 }
521 shmdt(output->buf);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300522}
523
524static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300525x11_output_set_wm_protocols(struct x11_backend *b,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500526 struct x11_output *output)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400527{
528 xcb_atom_t list[1];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400529
Giulio Camuffo954f1832014-10-11 18:27:30 +0300530 list[0] = b->atom.wm_delete_window;
531 xcb_change_property (b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400532 XCB_PROP_MODE_REPLACE,
533 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300534 b->atom.wm_protocols,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400535 XCB_ATOM_ATOM,
536 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500537 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400538 list);
539}
540
541struct wm_normal_hints {
542 uint32_t flags;
543 uint32_t pad[4];
544 int32_t min_width, min_height;
545 int32_t max_width, max_height;
546 int32_t width_inc, height_inc;
547 int32_t min_aspect_x, min_aspect_y;
548 int32_t max_aspect_x, max_aspect_y;
549 int32_t base_width, base_height;
550 int32_t win_gravity;
551};
552
553#define WM_NORMAL_HINTS_MIN_SIZE 16
554#define WM_NORMAL_HINTS_MAX_SIZE 32
555
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500556static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300557x11_output_set_icon(struct x11_backend *b,
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400558 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500559{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400560 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400561 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400562 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500563
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400564 image = load_image(filename);
565 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500566 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400567 width = pixman_image_get_width(image);
568 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500569 icon = malloc(width * height * 4 + 8);
570 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400571 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500572 return;
573 }
574
575 icon[0] = width;
576 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400577 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300578 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
579 b->atom.net_wm_icon, b->atom.cardinal, 32,
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500580 width * height + 2, icon);
581 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400582 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500583}
584
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500585static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300586x11_output_wait_for_map(struct x11_backend *b, struct x11_output *output)
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500587{
588 xcb_map_notify_event_t *map_notify;
589 xcb_configure_notify_event_t *configure_notify;
590 xcb_generic_event_t *event;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500591 int mapped = 0, configured = 0;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500592 uint8_t response_type;
593
594 /* This isn't the nicest way to do this. Ideally, we could
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500595 * just go back to the main loop and once we get the configure
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500596 * notify, we add the output to the compositor. While we do
597 * support output hotplug, we can't start up with no outputs.
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500598 * We could add the output and then resize once we get the
599 * configure notify, but we don't want to start up and
600 * immediately resize the output.
601 *
602 * Also, some window managers don't give us our final
603 * fullscreen size before map_notify, so if we don't get a
604 * configure_notify before map_notify, we just wait for the
605 * first one and hope that's our size. */
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500606
Giulio Camuffo954f1832014-10-11 18:27:30 +0300607 xcb_flush(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500608
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500609 while (!mapped || !configured) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300610 event = xcb_wait_for_event(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500611 response_type = event->response_type & ~0x80;
612
613 switch (response_type) {
614 case XCB_MAP_NOTIFY:
615 map_notify = (xcb_map_notify_event_t *) event;
616 if (map_notify->window == output->window)
617 mapped = 1;
618 break;
619
620 case XCB_CONFIGURE_NOTIFY:
621 configure_notify =
622 (xcb_configure_notify_event_t *) event;
623
Alexander Larsson4ea95522013-05-22 14:41:37 +0200624
625 if (configure_notify->width % output->scale != 0 ||
626 configure_notify->height % output->scale != 0)
627 weston_log("Resolution is not a multiple of screen size, rounding\n");
Jason Ekstrandd89a0942014-03-07 15:29:14 -0600628 output->mode.width = configure_notify->width;
629 output->mode.height = configure_notify->height;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500630 configured = 1;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500631 break;
632 }
633 }
634}
635
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300636static xcb_visualtype_t *
637find_visual_by_id(xcb_screen_t *screen,
638 xcb_visualid_t id)
639{
640 xcb_depth_iterator_t i;
641 xcb_visualtype_iterator_t j;
642 for (i = xcb_screen_allowed_depths_iterator(screen);
643 i.rem;
644 xcb_depth_next(&i)) {
645 for (j = xcb_depth_visuals_iterator(i.data);
646 j.rem;
647 xcb_visualtype_next(&j)) {
648 if (j.data->visual_id == id)
649 return j.data;
650 }
651 }
652 return 0;
653}
654
655static uint8_t
656get_depth_of_visual(xcb_screen_t *screen,
657 xcb_visualid_t id)
658{
659 xcb_depth_iterator_t i;
660 xcb_visualtype_iterator_t j;
661 for (i = xcb_screen_allowed_depths_iterator(screen);
662 i.rem;
663 xcb_depth_next(&i)) {
664 for (j = xcb_depth_visuals_iterator(i.data);
665 j.rem;
666 xcb_visualtype_next(&j)) {
667 if (j.data->visual_id == id)
668 return i.data->depth;
669 }
670 }
671 return 0;
672}
673
674static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300675x11_output_init_shm(struct x11_backend *b, struct x11_output *output,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300676 int width, int height)
677{
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300678 xcb_visualtype_t *visual_type;
Marko61b4d3e2015-10-05 17:27:54 -0500679 xcb_screen_t *screen;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300680 xcb_format_iterator_t fmt;
681 xcb_void_cookie_t cookie;
682 xcb_generic_error_t *err;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300683 const xcb_query_extension_reply_t *ext;
684 int bitsperpixel = 0;
685 pixman_format_code_t pixman_format;
686
687 /* Check if SHM is available */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300688 ext = xcb_get_extension_data(b->conn, &xcb_shm_id);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300689 if (ext == NULL || !ext->present) {
690 /* SHM is missing */
691 weston_log("SHM extension is not available\n");
692 errno = ENOENT;
693 return -1;
694 }
695
Marko61b4d3e2015-10-05 17:27:54 -0500696 screen = x11_compositor_get_default_screen(b);
697 visual_type = find_visual_by_id(screen, screen->root_visual);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300698 if (!visual_type) {
699 weston_log("Failed to lookup visual for root window\n");
700 errno = ENOENT;
701 return -1;
702 }
703 weston_log("Found visual, bits per value: %d, red_mask: %.8x, green_mask: %.8x, blue_mask: %.8x\n",
704 visual_type->bits_per_rgb_value,
705 visual_type->red_mask,
706 visual_type->green_mask,
707 visual_type->blue_mask);
Marko61b4d3e2015-10-05 17:27:54 -0500708 output->depth = get_depth_of_visual(screen, screen->root_visual);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300709 weston_log("Visual depth is %d\n", output->depth);
710
Giulio Camuffo954f1832014-10-11 18:27:30 +0300711 for (fmt = xcb_setup_pixmap_formats_iterator(xcb_get_setup(b->conn));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300712 fmt.rem;
713 xcb_format_next(&fmt)) {
714 if (fmt.data->depth == output->depth) {
715 bitsperpixel = fmt.data->bits_per_pixel;
716 break;
717 }
718 }
719 weston_log("Found format for depth %d, bpp: %d\n",
720 output->depth, bitsperpixel);
721
722 if (bitsperpixel == 32 &&
723 visual_type->red_mask == 0xff0000 &&
724 visual_type->green_mask == 0x00ff00 &&
725 visual_type->blue_mask == 0x0000ff) {
726 weston_log("Will use x8r8g8b8 format for SHM surfaces\n");
727 pixman_format = PIXMAN_x8r8g8b8;
MoD1b55a592013-11-28 05:24:21 +0000728 } else if (bitsperpixel == 16 &&
729 visual_type->red_mask == 0x00f800 &&
730 visual_type->green_mask == 0x0007e0 &&
731 visual_type->blue_mask == 0x00001f) {
732 weston_log("Will use r5g6b5 format for SHM surfaces\n");
733 pixman_format = PIXMAN_r5g6b5;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300734 } else {
735 weston_log("Can't find appropriate format for SHM pixmap\n");
736 errno = ENOTSUP;
737 return -1;
738 }
739
740
741 /* Create SHM segment and attach it */
742 output->shm_id = shmget(IPC_PRIVATE, width * height * (bitsperpixel / 8), IPC_CREAT | S_IRWXU);
743 if (output->shm_id == -1) {
744 weston_log("x11shm: failed to allocate SHM segment\n");
745 return -1;
746 }
747 output->buf = shmat(output->shm_id, NULL, 0 /* read/write */);
748 if (-1 == (long)output->buf) {
749 weston_log("x11shm: failed to attach SHM segment\n");
750 return -1;
751 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300752 output->segment = xcb_generate_id(b->conn);
753 cookie = xcb_shm_attach_checked(b->conn, output->segment, output->shm_id, 1);
754 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300755 if (err) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700756 weston_log("x11shm: xcb_shm_attach error %d, op code %d, resource id %d\n",
757 err->error_code, err->major_code, err->minor_code);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300758 free(err);
759 return -1;
760 }
761
762 shmctl(output->shm_id, IPC_RMID, NULL);
763
764 /* Now create pixman image */
765 output->hw_surface = pixman_image_create_bits(pixman_format, width, height, output->buf,
766 width * (bitsperpixel / 8));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300767
Giulio Camuffo954f1832014-10-11 18:27:30 +0300768 output->gc = xcb_generate_id(b->conn);
769 xcb_create_gc(b->conn, output->gc, output->window, 0, NULL);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300770
771 return 0;
772}
773
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200774static int
775x11_output_disable(struct weston_output *base)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400776{
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200777 struct x11_output *output = to_x11_output(base);
778 struct x11_backend *backend = to_x11_backend(base->compositor);
779
780 if (!output->base.enabled)
781 return 0;
782
783 wl_event_source_remove(output->finish_frame_timer);
784
785 if (backend->use_pixman) {
786 pixman_renderer_output_destroy(&output->base);
787 x11_output_deinit_shm(backend, output);
788 } else {
789 gl_renderer->output_destroy(&output->base);
790 }
791
792 xcb_destroy_window(backend->conn, output->window);
793 xcb_flush(backend->conn);
794
795 return 0;
796}
797
798static void
799x11_output_destroy(struct weston_output *base)
800{
801 struct x11_output *output = to_x11_output(base);
802
803 x11_output_disable(&output->base);
804 weston_output_destroy(&output->base);
805
806 free(output);
807}
808
809static int
810x11_output_enable(struct weston_output *base)
811{
812 struct x11_output *output = to_x11_output(base);
813 struct x11_backend *b = to_x11_backend(base->compositor);
814
Scott Moreau1bad5db2012-08-18 01:04:05 -0600815 static const char name[] = "Weston Compositor";
Kristian Høgsberg86000402012-01-03 23:24:14 -0500816 static const char class[] = "weston-1\0Weston Compositor";
Benoit Gschwind5c2f20e2016-06-05 19:01:11 +0200817 char *title = NULL;
Marko61b4d3e2015-10-05 17:27:54 -0500818 xcb_screen_t *screen;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400819 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400820 struct wl_event_loop *loop;
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200821
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300822 int ret;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400823 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500824 xcb_atom_t atom_list[1];
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300825 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400826 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300827 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400828 0
829 };
830
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200831 if (!b->no_input)
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300832 values[0] |=
833 XCB_EVENT_MASK_KEY_PRESS |
834 XCB_EVENT_MASK_KEY_RELEASE |
835 XCB_EVENT_MASK_BUTTON_PRESS |
836 XCB_EVENT_MASK_BUTTON_RELEASE |
837 XCB_EVENT_MASK_POINTER_MOTION |
838 XCB_EVENT_MASK_ENTER_WINDOW |
839 XCB_EVENT_MASK_LEAVE_WINDOW |
840 XCB_EVENT_MASK_KEYMAP_STATE |
841 XCB_EVENT_MASK_FOCUS_CHANGE;
842
Giulio Camuffo954f1832014-10-11 18:27:30 +0300843 values[1] = b->null_cursor;
844 output->window = xcb_generate_id(b->conn);
Marko61b4d3e2015-10-05 17:27:54 -0500845 screen = x11_compositor_get_default_screen(b);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300846 xcb_create_window(b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400847 XCB_COPY_FROM_PARENT,
848 output->window,
Marko61b4d3e2015-10-05 17:27:54 -0500849 screen->root,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400850 0, 0,
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200851 output->base.current_mode->width,
852 output->base.current_mode->height,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400853 0,
854 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Marko61b4d3e2015-10-05 17:27:54 -0500855 screen->root_visual,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400856 mask, values);
857
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200858 if (b->fullscreen) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300859 atom_list[0] = b->atom.net_wm_state_fullscreen;
860 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500861 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300862 b->atom.net_wm_state,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500863 XCB_ATOM_ATOM, 32,
864 ARRAY_LENGTH(atom_list), atom_list);
865 } else {
866 /* Don't resize me. */
867 memset(&normal_hints, 0, sizeof normal_hints);
868 normal_hints.flags =
869 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200870 normal_hints.min_width = output->base.current_mode->width;
871 normal_hints.min_height = output->base.current_mode->height;
872 normal_hints.max_width = output->base.current_mode->width;
873 normal_hints.max_height = output->base.current_mode->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300874 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
875 b->atom.wm_normal_hints,
876 b->atom.wm_size_hints, 32,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500877 sizeof normal_hints / 4,
878 (uint8_t *) &normal_hints);
879 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400880
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400881 /* Set window name. Don't bother with non-EWMH WMs. */
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200882 if (output->base.name) {
883 if (asprintf(&title, "%s - %s", name, output->base.name) < 0)
Benoit Gschwind5c2f20e2016-06-05 19:01:11 +0200884 title = NULL;
885 } else {
886 title = strdup(name);
887 }
888
889 if (title) {
890 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
891 b->atom.net_wm_name, b->atom.utf8_string, 8,
892 strlen(title), title);
893 free(title);
894 } else {
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200895 goto err;
Benoit Gschwind5c2f20e2016-06-05 19:01:11 +0200896 }
897
Giulio Camuffo954f1832014-10-11 18:27:30 +0300898 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
899 b->atom.wm_class, b->atom.string, 8,
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500900 sizeof class, class);
901
Giulio Camuffo954f1832014-10-11 18:27:30 +0300902 x11_output_set_icon(b, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500903
Giulio Camuffo954f1832014-10-11 18:27:30 +0300904 x11_output_set_wm_protocols(b, output);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500905
Giulio Camuffo954f1832014-10-11 18:27:30 +0300906 xcb_map_window(b->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400907
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200908 if (b->fullscreen)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300909 x11_output_wait_for_map(b, output);
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400910
Giulio Camuffo954f1832014-10-11 18:27:30 +0300911 if (b->use_pixman) {
912 if (x11_output_init_shm(b, output,
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200913 output->base.current_mode->width,
914 output->base.current_mode->height) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700915 weston_log("Failed to initialize SHM for the X11 output\n");
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200916 goto err;
Bryce Harrington665b0252015-05-26 18:14:21 -0700917 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300918 if (pixman_renderer_output_create(&output->base) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700919 weston_log("Failed to create pixman renderer for output\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300920 x11_output_deinit_shm(b, output);
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200921 goto err;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300922 }
Ryo Munakatae6dec902016-11-24 19:05:07 +0900923
924 output->base.repaint = x11_output_repaint_shm;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300925 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +0100926 /* eglCreatePlatformWindowSurfaceEXT takes a Window*
927 * but eglCreateWindowSurface takes a Window. */
928 Window xid = (Window) output->window;
929
Miguel A. Vicoc095cde2016-05-18 17:43:00 +0200930 ret = gl_renderer->output_window_create(
931 &output->base,
932 (EGLNativeWindowType) output->window,
933 &xid,
934 gl_renderer->opaque_attribs,
935 NULL,
936 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300937 if (ret < 0)
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200938 goto err;
Armin Krezović225bf9d2016-10-29 00:26:45 +0200939
940 output->base.repaint = x11_output_repaint_gl;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300941 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100942
Armin Krezović225bf9d2016-10-29 00:26:45 +0200943 output->base.start_repaint_loop = x11_output_start_repaint_loop;
944 output->base.assign_planes = NULL;
945 output->base.set_backlight = NULL;
946 output->base.set_dpms = NULL;
947 output->base.switch_mode = NULL;
948
Giulio Camuffo954f1832014-10-11 18:27:30 +0300949 loop = wl_display_get_event_loop(b->compositor->wl_display);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100950 output->finish_frame_timer =
951 wl_event_loop_add_timer(loop, finish_frame_handler, output);
952
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400953 weston_log("x11 output %dx%d, window id %d\n",
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200954 output->base.current_mode->width,
955 output->base.current_mode->height,
956 output->window);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400957
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200958 return 0;
959
960err:
961 xcb_destroy_window(b->conn, output->window);
962 xcb_flush(b->conn);
963
964 return -1;
965}
966
967static int
968x11_output_set_size(struct weston_output *base, int width, int height)
969{
970 struct x11_output *output = to_x11_output(base);
971 struct x11_backend *b = to_x11_backend(base->compositor);
972 int output_width, output_height;
973
974 /* We can only be called once. */
975 assert(!output->base.current_mode);
976
977 /* Make sure we have scale set. */
978 assert(output->base.scale);
979
980 if (width < 1) {
981 weston_log("Invalid width \"%d\" for output %s\n",
982 width, output->base.name);
983 return -1;
984 }
985
986 if (height < 1) {
987 weston_log("Invalid height \"%d\" for output %s\n",
988 height, output->base.name);
989 return -1;
990 }
991
992 output_width = width * output->base.scale;
993 output_height = height * output->base.scale;
994
995 output->mode.flags =
996 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
997
998 output->mode.width = output_width;
999 output->mode.height = output_height;
1000 output->mode.refresh = 60000;
1001 output->scale = output->base.scale;
1002 wl_list_init(&output->base.mode_list);
1003 wl_list_insert(&output->base.mode_list, &output->mode.link);
1004
1005 output->base.current_mode = &output->mode;
1006 output->base.make = "weston-X11";
1007 output->base.model = "none";
1008
1009 output->base.mm_width = width * b->screen->width_in_millimeters /
1010 b->screen->width_in_pixels;
1011 output->base.mm_height = height * b->screen->height_in_millimeters /
1012 b->screen->height_in_pixels;
1013
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001014 return 0;
1015}
1016
1017static int
1018x11_output_create(struct weston_compositor *compositor,
1019 const char *name)
1020{
1021 struct x11_output *output;
1022
1023 /* name can't be NULL. */
1024 assert(name);
1025
1026 output = zalloc(sizeof *output);
1027 if (output == NULL) {
1028 perror("zalloc");
1029 return -1;
1030 }
1031
1032 output->base.name = strdup(name);
1033 output->base.destroy = x11_output_destroy;
1034 output->base.disable = x11_output_disable;
1035 output->base.enable = x11_output_enable;
1036
Armin Krezović40087402016-09-30 14:11:12 +02001037 weston_output_init(&output->base, compositor);
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001038 weston_compositor_add_pending_output(&output->base, compositor);
1039
1040 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001041}
1042
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001043static struct x11_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001044x11_backend_find_output(struct x11_backend *b, xcb_window_t window)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001045{
1046 struct x11_output *output;
1047
Giulio Camuffo954f1832014-10-11 18:27:30 +03001048 wl_list_for_each(output, &b->compositor->output_list, base.link) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001049 if (output->window == window)
1050 return output;
1051 }
1052
Derek Foreman99bfa642014-12-11 15:44:46 -06001053 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001054}
1055
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001056static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001057x11_backend_delete_window(struct x11_backend *b, xcb_window_t window)
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001058{
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02001059 struct x11_output *output;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001060
Giulio Camuffo954f1832014-10-11 18:27:30 +03001061 output = x11_backend_find_output(b, window);
Derek Foreman99bfa642014-12-11 15:44:46 -06001062 if (output)
1063 x11_output_destroy(&output->base);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001064
Giulio Camuffo954f1832014-10-11 18:27:30 +03001065 if (wl_list_empty(&b->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001066 weston_compositor_exit(b->compositor);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001067}
1068
Derek Foremand540f4b2015-01-27 16:26:49 -06001069static void delete_cb(void *data)
1070{
1071 struct window_delete_data *wd = data;
1072
Giulio Camuffo954f1832014-10-11 18:27:30 +03001073 x11_backend_delete_window(wd->backend, wd->window);
Derek Foremand540f4b2015-01-27 16:26:49 -06001074 free(wd);
1075}
1076
Daniel Stone154c34c2012-06-22 13:21:40 +01001077#ifdef HAVE_XCB_XKB
Daniel Stonee2faa122012-06-22 13:21:39 +01001078static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001079update_xkb_state(struct x11_backend *b, xcb_xkb_state_notify_event_t *state)
Daniel Stonee2faa122012-06-22 13:21:39 +01001080{
Derek Foreman1281a362015-07-31 16:55:32 -05001081 struct weston_keyboard *keyboard =
1082 weston_seat_get_keyboard(&b->core_seat);
1083
1084 xkb_state_update_mask(keyboard->xkb_state.state,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001085 get_xkb_mod_mask(b, state->baseMods),
1086 get_xkb_mod_mask(b, state->latchedMods),
1087 get_xkb_mod_mask(b, state->lockedMods),
Daniel Stonee2faa122012-06-22 13:21:39 +01001088 0,
1089 0,
1090 state->group);
1091
Giulio Camuffo954f1832014-10-11 18:27:30 +03001092 notify_modifiers(&b->core_seat,
1093 wl_display_next_serial(b->compositor->wl_display));
Daniel Stonee2faa122012-06-22 13:21:39 +01001094}
1095#endif
1096
Daniel Stone154c34c2012-06-22 13:21:40 +01001097/**
1098 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
1099 * the best we can do (given that XCB also lacks XI2 support), is to take
1100 * the state from the core key events. Unfortunately that only gives us
1101 * the effective (i.e. union of depressed/latched/locked) state, and we
1102 * need the granularity.
1103 *
1104 * So we still update the state with every key event we see, but also use
1105 * the state field from X11 events as a mask so we don't get any stuck
1106 * modifiers.
1107 */
1108static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001109update_xkb_state_from_core(struct x11_backend *b, uint16_t x11_mask)
Daniel Stone154c34c2012-06-22 13:21:40 +01001110{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001111 uint32_t mask = get_xkb_mod_mask(b, x11_mask);
Derek Foreman1281a362015-07-31 16:55:32 -05001112 struct weston_keyboard *keyboard
1113 = weston_seat_get_keyboard(&b->core_seat);
Daniel Stone154c34c2012-06-22 13:21:40 +01001114
Derek Foreman1281a362015-07-31 16:55:32 -05001115 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone154c34c2012-06-22 13:21:40 +01001116 keyboard->modifiers.mods_depressed & mask,
1117 keyboard->modifiers.mods_latched & mask,
1118 keyboard->modifiers.mods_locked & mask,
1119 0,
1120 0,
1121 (x11_mask >> 13) & 3);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001122 notify_modifiers(&b->core_seat,
1123 wl_display_next_serial(b->compositor->wl_display));
Daniel Stone154c34c2012-06-22 13:21:40 +01001124}
1125
Tiago Vignattia3a71622011-12-08 17:03:17 +02001126static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001127x11_backend_deliver_button_event(struct x11_backend *b,
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001128 xcb_generic_event_t *event)
Tiago Vignattia3a71622011-12-08 17:03:17 +02001129{
1130 xcb_button_press_event_t *button_event =
1131 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +01001132 uint32_t button;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001133 struct x11_output *output;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001134 struct weston_pointer_axis_event weston_event;
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001135 bool is_button_pressed = event->response_type == XCB_BUTTON_PRESS;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001136
Benoit Gschwind4ddc4cc2016-05-18 21:32:11 +02001137 assert(event->response_type == XCB_BUTTON_PRESS ||
1138 event->response_type == XCB_BUTTON_RELEASE);
1139
Giulio Camuffo954f1832014-10-11 18:27:30 +03001140 output = x11_backend_find_output(b, button_event->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001141 if (!output)
1142 return;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001143
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001144 if (is_button_pressed)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001145 xcb_grab_pointer(b->conn, 0, output->window,
Kristian Høgsberg73308622012-10-30 11:04:52 -04001146 XCB_EVENT_MASK_BUTTON_PRESS |
1147 XCB_EVENT_MASK_BUTTON_RELEASE |
1148 XCB_EVENT_MASK_POINTER_MOTION |
1149 XCB_EVENT_MASK_ENTER_WINDOW |
1150 XCB_EVENT_MASK_LEAVE_WINDOW,
1151 XCB_GRAB_MODE_ASYNC,
1152 XCB_GRAB_MODE_ASYNC,
1153 output->window, XCB_CURSOR_NONE,
1154 button_event->time);
1155 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001156 xcb_ungrab_pointer(b->conn, button_event->time);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001157
Giulio Camuffo954f1832014-10-11 18:27:30 +03001158 if (!b->has_xkb)
1159 update_xkb_state_from_core(b, button_event->state);
Daniel Stone154c34c2012-06-22 13:21:40 +01001160
Tiago Vignattia3a71622011-12-08 17:03:17 +02001161 switch (button_event->detail) {
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001162 case 1:
1163 button = BTN_LEFT;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001164 break;
1165 case 2:
1166 button = BTN_MIDDLE;
1167 break;
1168 case 3:
1169 button = BTN_RIGHT;
1170 break;
1171 case 4:
Jonas Ådahl62efe202012-09-27 18:40:41 +02001172 /* Axis are measured in pixels, but the xcb events are discrete
1173 * steps. Therefore move the axis by some pixels every step. */
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001174 if (is_button_pressed) {
Peter Hutterer89b6a492016-01-18 15:58:17 +10001175 weston_event.value = -DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001176 weston_event.discrete = -1;
1177 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001178 weston_event.axis =
1179 WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001180 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001181 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001182 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001183 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001184 }
Scott Moreau210d0792012-03-22 10:47:01 -06001185 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001186 case 5:
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001187 if (is_button_pressed) {
Peter Hutterer89b6a492016-01-18 15:58:17 +10001188 weston_event.value = DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001189 weston_event.discrete = 1;
1190 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001191 weston_event.axis =
1192 WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001193 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001194 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001195 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001196 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001197 }
Scott Moreau210d0792012-03-22 10:47:01 -06001198 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001199 case 6:
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001200 if (is_button_pressed) {
Peter Hutterer89b6a492016-01-18 15:58:17 +10001201 weston_event.value = -DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001202 weston_event.discrete = -1;
1203 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001204 weston_event.axis =
1205 WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001206 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001207 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001208 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001209 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001210 }
Scott Moreau210d0792012-03-22 10:47:01 -06001211 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001212 case 7:
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001213 if (is_button_pressed) {
Peter Hutterer89b6a492016-01-18 15:58:17 +10001214 weston_event.value = DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001215 weston_event.discrete = 1;
1216 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001217 weston_event.axis =
1218 WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001219 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001220 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001221 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001222 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001223 }
Tiago Vignattia3a71622011-12-08 17:03:17 +02001224 return;
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001225 default:
1226 button = button_event->detail + BTN_SIDE - 8;
1227 break;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001228 }
1229
Giulio Camuffo954f1832014-10-11 18:27:30 +03001230 notify_button(&b->core_seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001231 weston_compositor_get_time(), button,
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001232 is_button_pressed ? WL_POINTER_BUTTON_STATE_PRESSED :
1233 WL_POINTER_BUTTON_STATE_RELEASED);
Peter Hutterer87743e92016-01-18 16:38:22 +10001234 notify_pointer_frame(&b->core_seat);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001235}
1236
Scott Moreau1bad5db2012-08-18 01:04:05 -06001237static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001238x11_backend_deliver_motion_event(struct x11_backend *b,
1239 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001240{
1241 struct x11_output *output;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001242 double x, y;
Jonas Ådahld2510102014-10-05 21:39:14 +02001243 struct weston_pointer_motion_event motion_event = { 0 };
Scott Moreau1bad5db2012-08-18 01:04:05 -06001244 xcb_motion_notify_event_t *motion_notify =
1245 (xcb_motion_notify_event_t *) event;
1246
Giulio Camuffo954f1832014-10-11 18:27:30 +03001247 if (!b->has_xkb)
1248 update_xkb_state_from_core(b, motion_notify->state);
1249 output = x11_backend_find_output(b, motion_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001250 if (!output)
1251 return;
1252
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001253 weston_output_transform_coordinate(&output->base,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001254 motion_notify->event_x,
1255 motion_notify->event_y,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05001256 &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001257
Jonas Ådahld2510102014-10-05 21:39:14 +02001258 motion_event = (struct weston_pointer_motion_event) {
1259 .mask = WESTON_POINTER_MOTION_REL,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001260 .dx = x - b->prev_x,
1261 .dy = y - b->prev_y
Jonas Ådahld2510102014-10-05 21:39:14 +02001262 };
1263
Giulio Camuffo954f1832014-10-11 18:27:30 +03001264 notify_motion(&b->core_seat, weston_compositor_get_time(),
Jonas Ådahld2510102014-10-05 21:39:14 +02001265 &motion_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001266 notify_pointer_frame(&b->core_seat);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001267
Giulio Camuffo954f1832014-10-11 18:27:30 +03001268 b->prev_x = x;
1269 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001270}
1271
1272static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001273x11_backend_deliver_enter_event(struct x11_backend *b,
1274 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001275{
1276 struct x11_output *output;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001277 double x, y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001278
1279 xcb_enter_notify_event_t *enter_notify =
1280 (xcb_enter_notify_event_t *) event;
1281 if (enter_notify->state >= Button1Mask)
1282 return;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001283 if (!b->has_xkb)
1284 update_xkb_state_from_core(b, enter_notify->state);
1285 output = x11_backend_find_output(b, enter_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001286 if (!output)
1287 return;
1288
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001289 weston_output_transform_coordinate(&output->base,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001290 enter_notify->event_x,
1291 enter_notify->event_y, &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001292
Giulio Camuffo954f1832014-10-11 18:27:30 +03001293 notify_pointer_focus(&b->core_seat, &output->base, x, y);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001294
Giulio Camuffo954f1832014-10-11 18:27:30 +03001295 b->prev_x = x;
1296 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001297}
1298
Kristian Høgsbergee724822011-04-21 14:51:44 -04001299static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001300x11_backend_next_event(struct x11_backend *b,
1301 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -04001302{
Daniel Stonef86e67d2016-11-29 12:03:35 +00001303 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001304 *event = xcb_poll_for_event(b->conn);
Daniel Stonef86e67d2016-11-29 12:03:35 +00001305 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001306 *event = xcb_poll_for_queued_event(b->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -04001307
1308 return *event != NULL;
1309}
1310
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001311static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001312x11_backend_handle_event(int fd, uint32_t mask, void *data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001313{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001314 struct x11_backend *b = data;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001315 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001316 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001317 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001318 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001319 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001320 xcb_keymap_notify_event_t *keymap_notify;
1321 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001322 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001323 xcb_atom_t atom;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001324 xcb_window_t window;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001325 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001326 uint32_t i, set;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001327 uint8_t response_type;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001328 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001329
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001330 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001331 count = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001332 while (x11_backend_next_event(b, &event, mask)) {
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001333 response_type = event->response_type & ~0x80;
1334
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001335 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1336 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001337 /* Suppress key repeat events; this is only used if we
1338 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001339 key_release = (xcb_key_press_event_t *) prev;
1340 key_press = (xcb_key_press_event_t *) event;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001341 if (response_type == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -07001342 key_release->time == key_press->time &&
1343 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001344 /* Don't deliver the held key release
1345 * event or the new key press event. */
1346 free(event);
1347 free(prev);
1348 prev = NULL;
1349 continue;
1350 } else {
1351 /* Deliver the held key release now
1352 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001353 * event below. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001354 update_xkb_state_from_core(b, key_release->state);
1355 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001356 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001357 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001358 WL_KEYBOARD_KEY_STATE_RELEASED,
1359 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001360 free(prev);
1361 prev = NULL;
1362 break;
1363 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001364
1365 case XCB_FOCUS_IN:
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001366 assert(response_type == XCB_KEYMAP_NOTIFY);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001367 keymap_notify = (xcb_keymap_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001368 b->keys.size = 0;
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001369 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
1370 set = keymap_notify->keys[i >> 3] &
1371 (1 << (i & 7));
1372 if (set) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001373 k = wl_array_add(&b->keys, sizeof *k);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001374 *k = i;
1375 }
1376 }
1377
Daniel Stoned6da09e2012-06-22 13:21:29 +01001378 /* Unfortunately the state only comes with the enter
1379 * event, rather than with the focus event. I'm not
1380 * sure of the exact semantics around it and whether
1381 * we can ensure that we get both? */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001382 notify_keyboard_focus_in(&b->core_seat, &b->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001383 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001384
1385 free(prev);
1386 prev = NULL;
1387 break;
1388
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001389 default:
1390 /* No previous event held */
1391 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001392 }
1393
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001394 switch (response_type) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001395 case XCB_KEY_PRESS:
1396 key_press = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001397 if (!b->has_xkb)
1398 update_xkb_state_from_core(b, key_press->state);
1399 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001400 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001401 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001402 WL_KEYBOARD_KEY_STATE_PRESSED,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001403 b->has_xkb ? STATE_UPDATE_NONE :
Daniel Stonee2faa122012-06-22 13:21:39 +01001404 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001405 break;
1406 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001407 /* If we don't have XKB, we need to use the lame
1408 * autorepeat detection above. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001409 if (!b->has_xkb) {
Daniel Stone3ee91e12012-06-22 13:21:36 +01001410 prev = event;
1411 break;
1412 }
1413 key_release = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001414 notify_key(&b->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +01001415 weston_compositor_get_time(),
1416 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001417 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +01001418 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001419 break;
1420 case XCB_BUTTON_PRESS:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001421 case XCB_BUTTON_RELEASE:
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001422 x11_backend_deliver_button_event(b, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001423 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001424 case XCB_MOTION_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001425 x11_backend_deliver_motion_event(b, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001426 break;
1427
1428 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001429 expose = (xcb_expose_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001430 output = x11_backend_find_output(b, expose->window);
Derek Foreman99bfa642014-12-11 15:44:46 -06001431 if (!output)
1432 break;
1433
Kristian Høgsberga98c2e12013-12-05 12:30:37 -08001434 weston_output_damage(&output->base);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001435 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001436 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001437
1438 case XCB_ENTER_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001439 x11_backend_deliver_enter_event(b, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001440 break;
1441
1442 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001443 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -05001444 if (enter_notify->state >= Button1Mask)
1445 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001446 if (!b->has_xkb)
1447 update_xkb_state_from_core(b, enter_notify->state);
1448 notify_pointer_focus(&b->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001449 break;
1450
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001451 case XCB_CLIENT_MESSAGE:
1452 client_message = (xcb_client_message_event_t *) event;
1453 atom = client_message->data.data32[0];
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001454 window = client_message->window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001455 if (atom == b->atom.wm_delete_window) {
Derek Foremand540f4b2015-01-27 16:26:49 -06001456 struct wl_event_loop *loop;
1457 struct window_delete_data *data = malloc(sizeof *data);
1458
1459 /* if malloc failed we should at least try to
1460 * delete the window, even if it may result in
1461 * a crash.
1462 */
1463 if (!data) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001464 x11_backend_delete_window(b, window);
Derek Foremand540f4b2015-01-27 16:26:49 -06001465 break;
1466 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001467 data->backend = b;
Derek Foremand540f4b2015-01-27 16:26:49 -06001468 data->window = window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001469 loop = wl_display_get_event_loop(b->compositor->wl_display);
Derek Foremand540f4b2015-01-27 16:26:49 -06001470 wl_event_loop_add_idle(loop, delete_cb, data);
1471 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001472 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001473
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001474 case XCB_FOCUS_IN:
1475 focus_in = (xcb_focus_in_event_t *) event;
1476 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1477 break;
1478
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001479 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001480 break;
1481
1482 case XCB_FOCUS_OUT:
1483 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -05001484 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1485 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001486 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001487 notify_keyboard_focus_out(&b->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001488 break;
1489
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001490 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001491 break;
1492 }
1493
Daniel Stonee2faa122012-06-22 13:21:39 +01001494#ifdef HAVE_XCB_XKB
Giulio Camuffo954f1832014-10-11 18:27:30 +03001495 if (b->has_xkb) {
1496 if (response_type == b->xkb_event_base) {
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001497 xcb_xkb_state_notify_event_t *state =
1498 (xcb_xkb_state_notify_event_t *) event;
1499 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001500 update_xkb_state(b, state);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001501 } else if (response_type == XCB_PROPERTY_NOTIFY) {
1502 xcb_property_notify_event_t *prop_notify =
1503 (xcb_property_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001504 if (prop_notify->window == b->screen->root &&
1505 prop_notify->atom == b->atom.xkb_names &&
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001506 prop_notify->state == XCB_PROPERTY_NEW_VALUE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001507 update_xkb_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001508 }
Daniel Stonee2faa122012-06-22 13:21:39 +01001509 }
1510#endif
1511
Bill Spitzakb715cec2012-06-05 17:08:23 -04001512 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001513 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001514 free (event);
1515 }
1516
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001517 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1518 case XCB_KEY_RELEASE:
1519 key_release = (xcb_key_press_event_t *) prev;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001520 update_xkb_state_from_core(b, key_release->state);
1521 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001522 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001523 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001524 WL_KEYBOARD_KEY_STATE_RELEASED,
1525 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001526 free(prev);
1527 prev = NULL;
1528 break;
1529 default:
1530 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001531 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001532
Bill Spitzakb715cec2012-06-05 17:08:23 -04001533 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001534}
1535
Giulio Camuffo954f1832014-10-11 18:27:30 +03001536#define F(field) offsetof(struct x11_backend, field)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001537
1538static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001539x11_backend_get_resources(struct x11_backend *b)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001540{
1541 static const struct { const char *name; int offset; } atoms[] = {
1542 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1543 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1544 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1545 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001546 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001547 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001548 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001549 { "_NET_WM_STATE", F(atom.net_wm_state) },
1550 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001551 { "_NET_SUPPORTING_WM_CHECK",
1552 F(atom.net_supporting_wm_check) },
1553 { "_NET_SUPPORTED", F(atom.net_supported) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001554 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001555 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001556 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001557 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001558 };
1559
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001560 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001561 xcb_intern_atom_reply_t *reply;
1562 xcb_pixmap_t pixmap;
1563 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001564 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001565 uint8_t data[] = { 0, 0, 0, 0 };
1566
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001567 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001568 cookies[i] = xcb_intern_atom (b->conn, 0,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001569 strlen(atoms[i].name),
1570 atoms[i].name);
1571
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001572 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001573 reply = xcb_intern_atom_reply (b->conn, cookies[i], NULL);
1574 *(xcb_atom_t *) ((char *) b + atoms[i].offset) = reply->atom;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001575 free(reply);
1576 }
1577
Giulio Camuffo954f1832014-10-11 18:27:30 +03001578 pixmap = xcb_generate_id(b->conn);
1579 gc = xcb_generate_id(b->conn);
1580 xcb_create_pixmap(b->conn, 1, pixmap, b->screen->root, 1, 1);
1581 xcb_create_gc(b->conn, gc, pixmap, 0, NULL);
1582 xcb_put_image(b->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001583 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001584 b->null_cursor = xcb_generate_id(b->conn);
1585 xcb_create_cursor (b->conn, b->null_cursor,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001586 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001587 xcb_free_gc(b->conn, gc);
1588 xcb_free_pixmap(b->conn, pixmap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001589}
1590
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001591static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001592x11_backend_get_wm_info(struct x11_backend *c)
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001593{
1594 xcb_get_property_cookie_t cookie;
1595 xcb_get_property_reply_t *reply;
1596 xcb_atom_t *atom;
1597 unsigned int i;
1598
1599 cookie = xcb_get_property(c->conn, 0, c->screen->root,
1600 c->atom.net_supported,
1601 XCB_ATOM_ATOM, 0, 1024);
1602 reply = xcb_get_property_reply(c->conn, cookie, NULL);
1603 if (reply == NULL)
1604 return;
1605
1606 atom = (xcb_atom_t *) xcb_get_property_value(reply);
1607
1608 for (i = 0; i < reply->value_len; i++) {
1609 if (atom[i] == c->atom.net_wm_state_fullscreen)
1610 c->has_net_wm_state_fullscreen = 1;
1611 }
Ryo Munakata76fb7ec2015-03-08 19:17:06 +09001612
1613 free(reply);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001614}
1615
1616static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001617x11_restore(struct weston_compositor *ec)
1618{
1619}
1620
1621static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001622x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001623{
Armin Krezović295e9d02016-08-01 19:17:58 +02001624 struct x11_backend *backend = to_x11_backend(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001625
Giulio Camuffo954f1832014-10-11 18:27:30 +03001626 wl_event_source_remove(backend->xcb_source);
1627 x11_input_destroy(backend);
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001628
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03001629 weston_compositor_shutdown(ec); /* destroys outputs, too */
1630
Giulio Camuffo954f1832014-10-11 18:27:30 +03001631 XCloseDisplay(backend->dpy);
1632 free(backend);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001633}
1634
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001635static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001636init_gl_renderer(struct x11_backend *b)
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001637{
1638 int ret;
1639
1640 gl_renderer = weston_load_module("gl-renderer.so",
1641 "gl_renderer_interface");
1642 if (!gl_renderer)
1643 return -1;
1644
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001645 ret = gl_renderer->display_create(b->compositor, EGL_PLATFORM_X11_KHR,
Miguel A. Vico41700e32016-05-18 17:47:59 +02001646 (void *) b->dpy, NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001647 gl_renderer->opaque_attribs, NULL, 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001648
1649 return ret;
1650}
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001651
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001652static const struct weston_windowed_output_api api = {
1653 x11_output_set_size,
1654 x11_output_create,
1655};
1656
Giulio Camuffo954f1832014-10-11 18:27:30 +03001657static struct x11_backend *
1658x11_backend_create(struct weston_compositor *compositor,
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001659 struct weston_x11_backend_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001660{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001661 struct x11_backend *b;
Pekka Paalanen157109b2016-03-24 15:43:44 +02001662 struct wl_event_loop *loop;
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001663 int ret;
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001664
Giulio Camuffo954f1832014-10-11 18:27:30 +03001665 b = zalloc(sizeof *b);
1666 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001667 return NULL;
1668
Giulio Camuffo954f1832014-10-11 18:27:30 +03001669 b->compositor = compositor;
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001670 b->fullscreen = config->fullscreen;
1671 b->no_input = config->no_input;
1672
Giulio Camuffo954f1832014-10-11 18:27:30 +03001673 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001674 goto err_free;
1675
Giulio Camuffo954f1832014-10-11 18:27:30 +03001676 b->dpy = XOpenDisplay(NULL);
1677 if (b->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001678 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001679
Giulio Camuffo954f1832014-10-11 18:27:30 +03001680 b->conn = XGetXCBConnection(b->dpy);
1681 XSetEventQueueOwner(b->dpy, XCBOwnsEventQueue);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001682
Giulio Camuffo954f1832014-10-11 18:27:30 +03001683 if (xcb_connection_has_error(b->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001684 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001685
Marko61b4d3e2015-10-05 17:27:54 -05001686 b->screen = x11_compositor_get_default_screen(b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001687 wl_array_init(&b->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001688
Giulio Camuffo954f1832014-10-11 18:27:30 +03001689 x11_backend_get_resources(b);
1690 x11_backend_get_wm_info(b);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001691
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001692 if (!b->has_net_wm_state_fullscreen && config->fullscreen) {
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001693 weston_log("Can not fullscreen without window manager support"
1694 "(need _NET_WM_STATE_FULLSCREEN)\n");
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001695 config->fullscreen = 0;
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001696 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001697
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001698 b->use_pixman = config->use_pixman;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001699 if (b->use_pixman) {
1700 if (pixman_renderer_init(compositor) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001701 weston_log("Failed to initialize pixman renderer for X11 backend\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001702 goto err_xdisplay;
Bryce Harrington665b0252015-05-26 18:14:21 -07001703 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001704 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001705 else if (init_gl_renderer(b) < 0) {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001706 goto err_xdisplay;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001707 }
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001708 weston_log("Using %s renderer\n", config->use_pixman ? "pixman" : "gl");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001709
Giulio Camuffo954f1832014-10-11 18:27:30 +03001710 b->base.destroy = x11_destroy;
1711 b->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001712
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001713 if (x11_input_create(b, config->no_input) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001714 weston_log("Failed to create X11 input\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001715 goto err_renderer;
Bryce Harrington665b0252015-05-26 18:14:21 -07001716 }
Daniel Stone22815f92012-06-22 13:21:34 +01001717
Pekka Paalanen157109b2016-03-24 15:43:44 +02001718 loop = wl_display_get_event_loop(compositor->wl_display);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001719 b->xcb_source =
Pekka Paalanen157109b2016-03-24 15:43:44 +02001720 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001721 xcb_get_file_descriptor(b->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001722 WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001723 x11_backend_handle_event, b);
1724 wl_event_source_check(b->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001725
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001726 if (compositor->renderer->import_dmabuf) {
1727 if (linux_dmabuf_setup(compositor) < 0)
1728 weston_log("Error: initializing dmabuf "
1729 "support failed.\n");
1730 }
1731
Giulio Camuffo954f1832014-10-11 18:27:30 +03001732 compositor->backend = &b->base;
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001733
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001734 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
1735 &api, sizeof(api));
1736
1737 if (ret < 0) {
1738 weston_log("Failed to register output API.\n");
1739 goto err_x11_input;
1740 }
1741
Giulio Camuffo954f1832014-10-11 18:27:30 +03001742 return b;
Martin Olsson11434bb2012-07-08 03:03:44 +02001743
1744err_x11_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001745 x11_input_destroy(b);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001746err_renderer:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001747 compositor->renderer->destroy(compositor);
Martin Olsson11434bb2012-07-08 03:03:44 +02001748err_xdisplay:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001749 XCloseDisplay(b->dpy);
Martin Olsson11434bb2012-07-08 03:03:44 +02001750err_free:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001751 free(b);
Martin Olsson11434bb2012-07-08 03:03:44 +02001752 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001753}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001754
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001755static void
1756config_init_to_defaults(struct weston_x11_backend_config *config)
1757{
1758}
1759
Giulio Camuffo954f1832014-10-11 18:27:30 +03001760WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01001761weston_backend_init(struct weston_compositor *compositor,
1762 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001763{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001764 struct x11_backend *b;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001765 struct weston_x11_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001766
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001767 if (config_base == NULL ||
1768 config_base->struct_version != WESTON_X11_BACKEND_CONFIG_VERSION ||
1769 config_base->struct_size > sizeof(struct weston_x11_backend_config)) {
1770 weston_log("X11 backend config structure is invalid\n");
1771 return -1;
1772 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001773
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001774 config_init_to_defaults(&config);
1775 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001776
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001777 b = x11_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001778 if (b == NULL)
1779 return -1;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001780
Giulio Camuffo954f1832014-10-11 18:27:30 +03001781 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001782}