blob: 9d37142841ae44b66ff8e2e25331f004f100220a [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;
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001002 wl_list_insert(&output->base.mode_list, &output->mode.link);
1003
1004 output->base.current_mode = &output->mode;
1005 output->base.make = "weston-X11";
1006 output->base.model = "none";
1007
1008 output->base.mm_width = width * b->screen->width_in_millimeters /
1009 b->screen->width_in_pixels;
1010 output->base.mm_height = height * b->screen->height_in_millimeters /
1011 b->screen->height_in_pixels;
1012
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001013 return 0;
1014}
1015
1016static int
1017x11_output_create(struct weston_compositor *compositor,
1018 const char *name)
1019{
1020 struct x11_output *output;
1021
1022 /* name can't be NULL. */
1023 assert(name);
1024
1025 output = zalloc(sizeof *output);
1026 if (output == NULL) {
1027 perror("zalloc");
1028 return -1;
1029 }
1030
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03001031 weston_output_init(&output->base, compositor, name);
1032
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001033 output->base.destroy = x11_output_destroy;
1034 output->base.disable = x11_output_disable;
1035 output->base.enable = x11_output_enable;
1036
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001037 weston_compositor_add_pending_output(&output->base, compositor);
1038
1039 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001040}
1041
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001042static struct x11_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +03001043x11_backend_find_output(struct x11_backend *b, xcb_window_t window)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001044{
1045 struct x11_output *output;
1046
Giulio Camuffo954f1832014-10-11 18:27:30 +03001047 wl_list_for_each(output, &b->compositor->output_list, base.link) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001048 if (output->window == window)
1049 return output;
1050 }
1051
Derek Foreman99bfa642014-12-11 15:44:46 -06001052 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001053}
1054
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001055static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001056x11_backend_delete_window(struct x11_backend *b, xcb_window_t window)
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001057{
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02001058 struct x11_output *output;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001059
Giulio Camuffo954f1832014-10-11 18:27:30 +03001060 output = x11_backend_find_output(b, window);
Derek Foreman99bfa642014-12-11 15:44:46 -06001061 if (output)
1062 x11_output_destroy(&output->base);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001063
Giulio Camuffo954f1832014-10-11 18:27:30 +03001064 if (wl_list_empty(&b->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +03001065 weston_compositor_exit(b->compositor);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001066}
1067
Derek Foremand540f4b2015-01-27 16:26:49 -06001068static void delete_cb(void *data)
1069{
1070 struct window_delete_data *wd = data;
1071
Giulio Camuffo954f1832014-10-11 18:27:30 +03001072 x11_backend_delete_window(wd->backend, wd->window);
Derek Foremand540f4b2015-01-27 16:26:49 -06001073 free(wd);
1074}
1075
Daniel Stone154c34c2012-06-22 13:21:40 +01001076#ifdef HAVE_XCB_XKB
Daniel Stonee2faa122012-06-22 13:21:39 +01001077static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001078update_xkb_state(struct x11_backend *b, xcb_xkb_state_notify_event_t *state)
Daniel Stonee2faa122012-06-22 13:21:39 +01001079{
Derek Foreman1281a362015-07-31 16:55:32 -05001080 struct weston_keyboard *keyboard =
1081 weston_seat_get_keyboard(&b->core_seat);
1082
1083 xkb_state_update_mask(keyboard->xkb_state.state,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001084 get_xkb_mod_mask(b, state->baseMods),
1085 get_xkb_mod_mask(b, state->latchedMods),
1086 get_xkb_mod_mask(b, state->lockedMods),
Daniel Stonee2faa122012-06-22 13:21:39 +01001087 0,
1088 0,
1089 state->group);
1090
Giulio Camuffo954f1832014-10-11 18:27:30 +03001091 notify_modifiers(&b->core_seat,
1092 wl_display_next_serial(b->compositor->wl_display));
Daniel Stonee2faa122012-06-22 13:21:39 +01001093}
1094#endif
1095
Daniel Stone154c34c2012-06-22 13:21:40 +01001096/**
1097 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
1098 * the best we can do (given that XCB also lacks XI2 support), is to take
1099 * the state from the core key events. Unfortunately that only gives us
1100 * the effective (i.e. union of depressed/latched/locked) state, and we
1101 * need the granularity.
1102 *
1103 * So we still update the state with every key event we see, but also use
1104 * the state field from X11 events as a mask so we don't get any stuck
1105 * modifiers.
1106 */
1107static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001108update_xkb_state_from_core(struct x11_backend *b, uint16_t x11_mask)
Daniel Stone154c34c2012-06-22 13:21:40 +01001109{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001110 uint32_t mask = get_xkb_mod_mask(b, x11_mask);
Derek Foreman1281a362015-07-31 16:55:32 -05001111 struct weston_keyboard *keyboard
1112 = weston_seat_get_keyboard(&b->core_seat);
Daniel Stone154c34c2012-06-22 13:21:40 +01001113
Derek Foreman1281a362015-07-31 16:55:32 -05001114 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone154c34c2012-06-22 13:21:40 +01001115 keyboard->modifiers.mods_depressed & mask,
1116 keyboard->modifiers.mods_latched & mask,
1117 keyboard->modifiers.mods_locked & mask,
1118 0,
1119 0,
1120 (x11_mask >> 13) & 3);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001121 notify_modifiers(&b->core_seat,
1122 wl_display_next_serial(b->compositor->wl_display));
Daniel Stone154c34c2012-06-22 13:21:40 +01001123}
1124
Tiago Vignattia3a71622011-12-08 17:03:17 +02001125static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001126x11_backend_deliver_button_event(struct x11_backend *b,
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001127 xcb_generic_event_t *event)
Tiago Vignattia3a71622011-12-08 17:03:17 +02001128{
1129 xcb_button_press_event_t *button_event =
1130 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +01001131 uint32_t button;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001132 struct x11_output *output;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001133 struct weston_pointer_axis_event weston_event;
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001134 bool is_button_pressed = event->response_type == XCB_BUTTON_PRESS;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001135
Benoit Gschwind4ddc4cc2016-05-18 21:32:11 +02001136 assert(event->response_type == XCB_BUTTON_PRESS ||
1137 event->response_type == XCB_BUTTON_RELEASE);
1138
Giulio Camuffo954f1832014-10-11 18:27:30 +03001139 output = x11_backend_find_output(b, button_event->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001140 if (!output)
1141 return;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001142
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001143 if (is_button_pressed)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001144 xcb_grab_pointer(b->conn, 0, output->window,
Kristian Høgsberg73308622012-10-30 11:04:52 -04001145 XCB_EVENT_MASK_BUTTON_PRESS |
1146 XCB_EVENT_MASK_BUTTON_RELEASE |
1147 XCB_EVENT_MASK_POINTER_MOTION |
1148 XCB_EVENT_MASK_ENTER_WINDOW |
1149 XCB_EVENT_MASK_LEAVE_WINDOW,
1150 XCB_GRAB_MODE_ASYNC,
1151 XCB_GRAB_MODE_ASYNC,
1152 output->window, XCB_CURSOR_NONE,
1153 button_event->time);
1154 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001155 xcb_ungrab_pointer(b->conn, button_event->time);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001156
Giulio Camuffo954f1832014-10-11 18:27:30 +03001157 if (!b->has_xkb)
1158 update_xkb_state_from_core(b, button_event->state);
Daniel Stone154c34c2012-06-22 13:21:40 +01001159
Tiago Vignattia3a71622011-12-08 17:03:17 +02001160 switch (button_event->detail) {
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001161 case 1:
1162 button = BTN_LEFT;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001163 break;
1164 case 2:
1165 button = BTN_MIDDLE;
1166 break;
1167 case 3:
1168 button = BTN_RIGHT;
1169 break;
1170 case 4:
Jonas Ådahl62efe202012-09-27 18:40:41 +02001171 /* Axis are measured in pixels, but the xcb events are discrete
1172 * steps. Therefore move the axis by some pixels every step. */
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001173 if (is_button_pressed) {
Peter Hutterer89b6a492016-01-18 15:58:17 +10001174 weston_event.value = -DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001175 weston_event.discrete = -1;
1176 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001177 weston_event.axis =
1178 WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001179 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001180 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001181 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001182 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001183 }
Scott Moreau210d0792012-03-22 10:47:01 -06001184 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001185 case 5:
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001186 if (is_button_pressed) {
Peter Hutterer89b6a492016-01-18 15:58:17 +10001187 weston_event.value = DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001188 weston_event.discrete = 1;
1189 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001190 weston_event.axis =
1191 WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001192 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001193 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001194 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001195 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001196 }
Scott Moreau210d0792012-03-22 10:47:01 -06001197 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001198 case 6:
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001199 if (is_button_pressed) {
Peter Hutterer89b6a492016-01-18 15:58:17 +10001200 weston_event.value = -DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001201 weston_event.discrete = -1;
1202 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001203 weston_event.axis =
1204 WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001205 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001206 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001207 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001208 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001209 }
Scott Moreau210d0792012-03-22 10:47:01 -06001210 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001211 case 7:
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001212 if (is_button_pressed) {
Peter Hutterer89b6a492016-01-18 15:58:17 +10001213 weston_event.value = DEFAULT_AXIS_STEP_DISTANCE;
Peter Hutterer87743e92016-01-18 16:38:22 +10001214 weston_event.discrete = 1;
1215 weston_event.has_discrete = true;
Peter Hutterer89b6a492016-01-18 15:58:17 +10001216 weston_event.axis =
1217 WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001218 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001219 weston_compositor_get_time(),
Peter Hutterer89b6a492016-01-18 15:58:17 +10001220 &weston_event);
Marek Chalupa345b4f52016-02-03 14:03:00 +01001221 notify_pointer_frame(&b->core_seat);
Peter Hutterer89b6a492016-01-18 15:58:17 +10001222 }
Tiago Vignattia3a71622011-12-08 17:03:17 +02001223 return;
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001224 default:
1225 button = button_event->detail + BTN_SIDE - 8;
1226 break;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001227 }
1228
Giulio Camuffo954f1832014-10-11 18:27:30 +03001229 notify_button(&b->core_seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001230 weston_compositor_get_time(), button,
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001231 is_button_pressed ? WL_POINTER_BUTTON_STATE_PRESSED :
1232 WL_POINTER_BUTTON_STATE_RELEASED);
Peter Hutterer87743e92016-01-18 16:38:22 +10001233 notify_pointer_frame(&b->core_seat);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001234}
1235
Scott Moreau1bad5db2012-08-18 01:04:05 -06001236static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001237x11_backend_deliver_motion_event(struct x11_backend *b,
1238 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001239{
1240 struct x11_output *output;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001241 double x, y;
Jonas Ådahld2510102014-10-05 21:39:14 +02001242 struct weston_pointer_motion_event motion_event = { 0 };
Scott Moreau1bad5db2012-08-18 01:04:05 -06001243 xcb_motion_notify_event_t *motion_notify =
1244 (xcb_motion_notify_event_t *) event;
1245
Giulio Camuffo954f1832014-10-11 18:27:30 +03001246 if (!b->has_xkb)
1247 update_xkb_state_from_core(b, motion_notify->state);
1248 output = x11_backend_find_output(b, motion_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001249 if (!output)
1250 return;
1251
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001252 weston_output_transform_coordinate(&output->base,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001253 motion_notify->event_x,
1254 motion_notify->event_y,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05001255 &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001256
Jonas Ådahld2510102014-10-05 21:39:14 +02001257 motion_event = (struct weston_pointer_motion_event) {
1258 .mask = WESTON_POINTER_MOTION_REL,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001259 .dx = x - b->prev_x,
1260 .dy = y - b->prev_y
Jonas Ådahld2510102014-10-05 21:39:14 +02001261 };
1262
Giulio Camuffo954f1832014-10-11 18:27:30 +03001263 notify_motion(&b->core_seat, weston_compositor_get_time(),
Jonas Ådahld2510102014-10-05 21:39:14 +02001264 &motion_event);
Peter Hutterer87743e92016-01-18 16:38:22 +10001265 notify_pointer_frame(&b->core_seat);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001266
Giulio Camuffo954f1832014-10-11 18:27:30 +03001267 b->prev_x = x;
1268 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001269}
1270
1271static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001272x11_backend_deliver_enter_event(struct x11_backend *b,
1273 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001274{
1275 struct x11_output *output;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001276 double x, y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001277
1278 xcb_enter_notify_event_t *enter_notify =
1279 (xcb_enter_notify_event_t *) event;
1280 if (enter_notify->state >= Button1Mask)
1281 return;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001282 if (!b->has_xkb)
1283 update_xkb_state_from_core(b, enter_notify->state);
1284 output = x11_backend_find_output(b, enter_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001285 if (!output)
1286 return;
1287
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001288 weston_output_transform_coordinate(&output->base,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02001289 enter_notify->event_x,
1290 enter_notify->event_y, &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001291
Giulio Camuffo954f1832014-10-11 18:27:30 +03001292 notify_pointer_focus(&b->core_seat, &output->base, x, y);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001293
Giulio Camuffo954f1832014-10-11 18:27:30 +03001294 b->prev_x = x;
1295 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001296}
1297
Kristian Høgsbergee724822011-04-21 14:51:44 -04001298static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001299x11_backend_next_event(struct x11_backend *b,
1300 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -04001301{
Daniel Stonef86e67d2016-11-29 12:03:35 +00001302 if (mask & WL_EVENT_READABLE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001303 *event = xcb_poll_for_event(b->conn);
Daniel Stonef86e67d2016-11-29 12:03:35 +00001304 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001305 *event = xcb_poll_for_queued_event(b->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -04001306
1307 return *event != NULL;
1308}
1309
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001310static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001311x11_backend_handle_event(int fd, uint32_t mask, void *data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001312{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001313 struct x11_backend *b = data;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001314 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001315 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001316 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001317 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001318 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001319 xcb_keymap_notify_event_t *keymap_notify;
1320 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001321 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001322 xcb_atom_t atom;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001323 xcb_window_t window;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001324 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001325 uint32_t i, set;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001326 uint8_t response_type;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001327 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001328
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001329 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001330 count = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001331 while (x11_backend_next_event(b, &event, mask)) {
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001332 response_type = event->response_type & ~0x80;
1333
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001334 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1335 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001336 /* Suppress key repeat events; this is only used if we
1337 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001338 key_release = (xcb_key_press_event_t *) prev;
1339 key_press = (xcb_key_press_event_t *) event;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001340 if (response_type == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -07001341 key_release->time == key_press->time &&
1342 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001343 /* Don't deliver the held key release
1344 * event or the new key press event. */
1345 free(event);
1346 free(prev);
1347 prev = NULL;
1348 continue;
1349 } else {
1350 /* Deliver the held key release now
1351 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001352 * event below. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001353 update_xkb_state_from_core(b, key_release->state);
1354 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001355 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001356 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001357 WL_KEYBOARD_KEY_STATE_RELEASED,
1358 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001359 free(prev);
1360 prev = NULL;
1361 break;
1362 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001363
1364 case XCB_FOCUS_IN:
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001365 assert(response_type == XCB_KEYMAP_NOTIFY);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001366 keymap_notify = (xcb_keymap_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001367 b->keys.size = 0;
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001368 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
1369 set = keymap_notify->keys[i >> 3] &
1370 (1 << (i & 7));
1371 if (set) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001372 k = wl_array_add(&b->keys, sizeof *k);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001373 *k = i;
1374 }
1375 }
1376
Daniel Stoned6da09e2012-06-22 13:21:29 +01001377 /* Unfortunately the state only comes with the enter
1378 * event, rather than with the focus event. I'm not
1379 * sure of the exact semantics around it and whether
1380 * we can ensure that we get both? */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001381 notify_keyboard_focus_in(&b->core_seat, &b->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001382 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001383
1384 free(prev);
1385 prev = NULL;
1386 break;
1387
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001388 default:
1389 /* No previous event held */
1390 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001391 }
1392
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001393 switch (response_type) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001394 case XCB_KEY_PRESS:
1395 key_press = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001396 if (!b->has_xkb)
1397 update_xkb_state_from_core(b, key_press->state);
1398 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001399 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001400 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001401 WL_KEYBOARD_KEY_STATE_PRESSED,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001402 b->has_xkb ? STATE_UPDATE_NONE :
Daniel Stonee2faa122012-06-22 13:21:39 +01001403 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001404 break;
1405 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001406 /* If we don't have XKB, we need to use the lame
1407 * autorepeat detection above. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001408 if (!b->has_xkb) {
Daniel Stone3ee91e12012-06-22 13:21:36 +01001409 prev = event;
1410 break;
1411 }
1412 key_release = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001413 notify_key(&b->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +01001414 weston_compositor_get_time(),
1415 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001416 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +01001417 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001418 break;
1419 case XCB_BUTTON_PRESS:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001420 case XCB_BUTTON_RELEASE:
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001421 x11_backend_deliver_button_event(b, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001422 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001423 case XCB_MOTION_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001424 x11_backend_deliver_motion_event(b, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001425 break;
1426
1427 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001428 expose = (xcb_expose_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001429 output = x11_backend_find_output(b, expose->window);
Derek Foreman99bfa642014-12-11 15:44:46 -06001430 if (!output)
1431 break;
1432
Kristian Høgsberga98c2e12013-12-05 12:30:37 -08001433 weston_output_damage(&output->base);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001434 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001435 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001436
1437 case XCB_ENTER_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001438 x11_backend_deliver_enter_event(b, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001439 break;
1440
1441 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001442 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -05001443 if (enter_notify->state >= Button1Mask)
1444 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001445 if (!b->has_xkb)
1446 update_xkb_state_from_core(b, enter_notify->state);
1447 notify_pointer_focus(&b->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001448 break;
1449
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001450 case XCB_CLIENT_MESSAGE:
1451 client_message = (xcb_client_message_event_t *) event;
1452 atom = client_message->data.data32[0];
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001453 window = client_message->window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001454 if (atom == b->atom.wm_delete_window) {
Derek Foremand540f4b2015-01-27 16:26:49 -06001455 struct wl_event_loop *loop;
1456 struct window_delete_data *data = malloc(sizeof *data);
1457
1458 /* if malloc failed we should at least try to
1459 * delete the window, even if it may result in
1460 * a crash.
1461 */
1462 if (!data) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001463 x11_backend_delete_window(b, window);
Derek Foremand540f4b2015-01-27 16:26:49 -06001464 break;
1465 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001466 data->backend = b;
Derek Foremand540f4b2015-01-27 16:26:49 -06001467 data->window = window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001468 loop = wl_display_get_event_loop(b->compositor->wl_display);
Derek Foremand540f4b2015-01-27 16:26:49 -06001469 wl_event_loop_add_idle(loop, delete_cb, data);
1470 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001471 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001472
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001473 case XCB_FOCUS_IN:
1474 focus_in = (xcb_focus_in_event_t *) event;
1475 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1476 break;
1477
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001478 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001479 break;
1480
1481 case XCB_FOCUS_OUT:
1482 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -05001483 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1484 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001485 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001486 notify_keyboard_focus_out(&b->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001487 break;
1488
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001489 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001490 break;
1491 }
1492
Daniel Stonee2faa122012-06-22 13:21:39 +01001493#ifdef HAVE_XCB_XKB
Giulio Camuffo954f1832014-10-11 18:27:30 +03001494 if (b->has_xkb) {
1495 if (response_type == b->xkb_event_base) {
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001496 xcb_xkb_state_notify_event_t *state =
1497 (xcb_xkb_state_notify_event_t *) event;
1498 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001499 update_xkb_state(b, state);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001500 } else if (response_type == XCB_PROPERTY_NOTIFY) {
1501 xcb_property_notify_event_t *prop_notify =
1502 (xcb_property_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001503 if (prop_notify->window == b->screen->root &&
1504 prop_notify->atom == b->atom.xkb_names &&
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001505 prop_notify->state == XCB_PROPERTY_NEW_VALUE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001506 update_xkb_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001507 }
Daniel Stonee2faa122012-06-22 13:21:39 +01001508 }
1509#endif
1510
Bill Spitzakb715cec2012-06-05 17:08:23 -04001511 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001512 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001513 free (event);
1514 }
1515
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001516 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1517 case XCB_KEY_RELEASE:
1518 key_release = (xcb_key_press_event_t *) prev;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001519 update_xkb_state_from_core(b, key_release->state);
1520 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001521 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001522 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001523 WL_KEYBOARD_KEY_STATE_RELEASED,
1524 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001525 free(prev);
1526 prev = NULL;
1527 break;
1528 default:
1529 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001530 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001531
Bill Spitzakb715cec2012-06-05 17:08:23 -04001532 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001533}
1534
Giulio Camuffo954f1832014-10-11 18:27:30 +03001535#define F(field) offsetof(struct x11_backend, field)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001536
1537static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001538x11_backend_get_resources(struct x11_backend *b)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001539{
1540 static const struct { const char *name; int offset; } atoms[] = {
1541 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1542 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1543 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1544 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001545 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001546 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001547 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001548 { "_NET_WM_STATE", F(atom.net_wm_state) },
1549 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001550 { "_NET_SUPPORTING_WM_CHECK",
1551 F(atom.net_supporting_wm_check) },
1552 { "_NET_SUPPORTED", F(atom.net_supported) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001553 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001554 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001555 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001556 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001557 };
1558
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001559 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001560 xcb_intern_atom_reply_t *reply;
1561 xcb_pixmap_t pixmap;
1562 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001563 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001564 uint8_t data[] = { 0, 0, 0, 0 };
1565
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001566 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001567 cookies[i] = xcb_intern_atom (b->conn, 0,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001568 strlen(atoms[i].name),
1569 atoms[i].name);
1570
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001571 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001572 reply = xcb_intern_atom_reply (b->conn, cookies[i], NULL);
1573 *(xcb_atom_t *) ((char *) b + atoms[i].offset) = reply->atom;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001574 free(reply);
1575 }
1576
Giulio Camuffo954f1832014-10-11 18:27:30 +03001577 pixmap = xcb_generate_id(b->conn);
1578 gc = xcb_generate_id(b->conn);
1579 xcb_create_pixmap(b->conn, 1, pixmap, b->screen->root, 1, 1);
1580 xcb_create_gc(b->conn, gc, pixmap, 0, NULL);
1581 xcb_put_image(b->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001582 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001583 b->null_cursor = xcb_generate_id(b->conn);
1584 xcb_create_cursor (b->conn, b->null_cursor,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001585 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001586 xcb_free_gc(b->conn, gc);
1587 xcb_free_pixmap(b->conn, pixmap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001588}
1589
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001590static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001591x11_backend_get_wm_info(struct x11_backend *c)
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001592{
1593 xcb_get_property_cookie_t cookie;
1594 xcb_get_property_reply_t *reply;
1595 xcb_atom_t *atom;
1596 unsigned int i;
1597
1598 cookie = xcb_get_property(c->conn, 0, c->screen->root,
1599 c->atom.net_supported,
1600 XCB_ATOM_ATOM, 0, 1024);
1601 reply = xcb_get_property_reply(c->conn, cookie, NULL);
1602 if (reply == NULL)
1603 return;
1604
1605 atom = (xcb_atom_t *) xcb_get_property_value(reply);
1606
1607 for (i = 0; i < reply->value_len; i++) {
1608 if (atom[i] == c->atom.net_wm_state_fullscreen)
1609 c->has_net_wm_state_fullscreen = 1;
1610 }
Ryo Munakata76fb7ec2015-03-08 19:17:06 +09001611
1612 free(reply);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001613}
1614
1615static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001616x11_restore(struct weston_compositor *ec)
1617{
1618}
1619
1620static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001621x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001622{
Armin Krezović295e9d02016-08-01 19:17:58 +02001623 struct x11_backend *backend = to_x11_backend(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001624
Giulio Camuffo954f1832014-10-11 18:27:30 +03001625 wl_event_source_remove(backend->xcb_source);
1626 x11_input_destroy(backend);
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001627
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03001628 weston_compositor_shutdown(ec); /* destroys outputs, too */
1629
Giulio Camuffo954f1832014-10-11 18:27:30 +03001630 XCloseDisplay(backend->dpy);
1631 free(backend);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001632}
1633
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001634static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001635init_gl_renderer(struct x11_backend *b)
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001636{
1637 int ret;
1638
1639 gl_renderer = weston_load_module("gl-renderer.so",
1640 "gl_renderer_interface");
1641 if (!gl_renderer)
1642 return -1;
1643
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001644 ret = gl_renderer->display_create(b->compositor, EGL_PLATFORM_X11_KHR,
Miguel A. Vico41700e32016-05-18 17:47:59 +02001645 (void *) b->dpy, NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001646 gl_renderer->opaque_attribs, NULL, 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001647
1648 return ret;
1649}
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001650
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001651static const struct weston_windowed_output_api api = {
1652 x11_output_set_size,
1653 x11_output_create,
1654};
1655
Giulio Camuffo954f1832014-10-11 18:27:30 +03001656static struct x11_backend *
1657x11_backend_create(struct weston_compositor *compositor,
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001658 struct weston_x11_backend_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001659{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001660 struct x11_backend *b;
Pekka Paalanen157109b2016-03-24 15:43:44 +02001661 struct wl_event_loop *loop;
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001662 int ret;
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001663
Giulio Camuffo954f1832014-10-11 18:27:30 +03001664 b = zalloc(sizeof *b);
1665 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001666 return NULL;
1667
Giulio Camuffo954f1832014-10-11 18:27:30 +03001668 b->compositor = compositor;
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001669 b->fullscreen = config->fullscreen;
1670 b->no_input = config->no_input;
1671
Giulio Camuffo954f1832014-10-11 18:27:30 +03001672 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001673 goto err_free;
1674
Giulio Camuffo954f1832014-10-11 18:27:30 +03001675 b->dpy = XOpenDisplay(NULL);
1676 if (b->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001677 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001678
Giulio Camuffo954f1832014-10-11 18:27:30 +03001679 b->conn = XGetXCBConnection(b->dpy);
1680 XSetEventQueueOwner(b->dpy, XCBOwnsEventQueue);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001681
Giulio Camuffo954f1832014-10-11 18:27:30 +03001682 if (xcb_connection_has_error(b->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001683 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001684
Marko61b4d3e2015-10-05 17:27:54 -05001685 b->screen = x11_compositor_get_default_screen(b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001686 wl_array_init(&b->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001687
Giulio Camuffo954f1832014-10-11 18:27:30 +03001688 x11_backend_get_resources(b);
1689 x11_backend_get_wm_info(b);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001690
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001691 if (!b->has_net_wm_state_fullscreen && config->fullscreen) {
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001692 weston_log("Can not fullscreen without window manager support"
1693 "(need _NET_WM_STATE_FULLSCREEN)\n");
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001694 config->fullscreen = 0;
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001695 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001696
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001697 b->use_pixman = config->use_pixman;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001698 if (b->use_pixman) {
1699 if (pixman_renderer_init(compositor) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001700 weston_log("Failed to initialize pixman renderer for X11 backend\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001701 goto err_xdisplay;
Bryce Harrington665b0252015-05-26 18:14:21 -07001702 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001703 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001704 else if (init_gl_renderer(b) < 0) {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001705 goto err_xdisplay;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001706 }
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001707 weston_log("Using %s renderer\n", config->use_pixman ? "pixman" : "gl");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001708
Giulio Camuffo954f1832014-10-11 18:27:30 +03001709 b->base.destroy = x11_destroy;
1710 b->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001711
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001712 if (x11_input_create(b, config->no_input) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001713 weston_log("Failed to create X11 input\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001714 goto err_renderer;
Bryce Harrington665b0252015-05-26 18:14:21 -07001715 }
Daniel Stone22815f92012-06-22 13:21:34 +01001716
Pekka Paalanen157109b2016-03-24 15:43:44 +02001717 loop = wl_display_get_event_loop(compositor->wl_display);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001718 b->xcb_source =
Pekka Paalanen157109b2016-03-24 15:43:44 +02001719 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001720 xcb_get_file_descriptor(b->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001721 WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001722 x11_backend_handle_event, b);
1723 wl_event_source_check(b->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001724
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001725 if (compositor->renderer->import_dmabuf) {
1726 if (linux_dmabuf_setup(compositor) < 0)
1727 weston_log("Error: initializing dmabuf "
1728 "support failed.\n");
1729 }
1730
Giulio Camuffo954f1832014-10-11 18:27:30 +03001731 compositor->backend = &b->base;
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001732
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001733 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
1734 &api, sizeof(api));
1735
1736 if (ret < 0) {
1737 weston_log("Failed to register output API.\n");
1738 goto err_x11_input;
1739 }
1740
Giulio Camuffo954f1832014-10-11 18:27:30 +03001741 return b;
Martin Olsson11434bb2012-07-08 03:03:44 +02001742
1743err_x11_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001744 x11_input_destroy(b);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001745err_renderer:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001746 compositor->renderer->destroy(compositor);
Martin Olsson11434bb2012-07-08 03:03:44 +02001747err_xdisplay:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001748 XCloseDisplay(b->dpy);
Martin Olsson11434bb2012-07-08 03:03:44 +02001749err_free:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001750 free(b);
Martin Olsson11434bb2012-07-08 03:03:44 +02001751 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001752}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001753
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001754static void
1755config_init_to_defaults(struct weston_x11_backend_config *config)
1756{
1757}
1758
Giulio Camuffo954f1832014-10-11 18:27:30 +03001759WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01001760weston_backend_init(struct weston_compositor *compositor,
1761 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001762{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001763 struct x11_backend *b;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001764 struct weston_x11_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001765
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001766 if (config_base == NULL ||
1767 config_base->struct_version != WESTON_X11_BACKEND_CONFIG_VERSION ||
1768 config_base->struct_size > sizeof(struct weston_x11_backend_config)) {
1769 weston_log("X11 backend config structure is invalid\n");
1770 return -1;
1771 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001772
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001773 config_init_to_defaults(&config);
1774 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001775
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001776 b = x11_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001777 if (b == NULL)
1778 return -1;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001779
Giulio Camuffo954f1832014-10-11 18:27:30 +03001780 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001781}