blob: 0804591d02eac2480068395ab764121ce373686a [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,
392 pixman_region32_t *damage)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500393{
Armin Krezović295e9d02016-08-01 19:17:58 +0200394 struct x11_output *output = to_x11_output(output_base);
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400395 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500396
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400397 ec->renderer->repaint_output(output_base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500398
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200399 pixman_region32_subtract(&ec->primary_plane.damage,
400 &ec->primary_plane.damage, damage);
401
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500402 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200403 return 0;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500404}
405
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300406static void
Alexander Larsson80f91632013-05-22 14:41:38 +0200407set_clip_for_output(struct weston_output *output_base, pixman_region32_t *region)
408{
Armin Krezović295e9d02016-08-01 19:17:58 +0200409 struct x11_output *output = to_x11_output(output_base);
Alexander Larsson80f91632013-05-22 14:41:38 +0200410 struct weston_compositor *ec = output->base.compositor;
Armin Krezović295e9d02016-08-01 19:17:58 +0200411 struct x11_backend *b = to_x11_backend(ec);
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500412 pixman_region32_t transformed_region;
Alexander Larsson80f91632013-05-22 14:41:38 +0200413 pixman_box32_t *rects;
414 xcb_rectangle_t *output_rects;
Alexander Larsson80f91632013-05-22 14:41:38 +0200415 xcb_void_cookie_t cookie;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500416 int nrects, i;
Alexander Larsson80f91632013-05-22 14:41:38 +0200417 xcb_generic_error_t *err;
418
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500419 pixman_region32_init(&transformed_region);
420 pixman_region32_copy(&transformed_region, region);
421 pixman_region32_translate(&transformed_region,
422 -output_base->x, -output_base->y);
423 weston_transformed_region(output_base->width, output_base->height,
424 output_base->transform,
425 output_base->current_scale,
426 &transformed_region, &transformed_region);
427
428 rects = pixman_region32_rectangles(&transformed_region, &nrects);
Alexander Larsson80f91632013-05-22 14:41:38 +0200429 output_rects = calloc(nrects, sizeof(xcb_rectangle_t));
430
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500431 if (output_rects == NULL) {
432 pixman_region32_fini(&transformed_region);
Alexander Larsson80f91632013-05-22 14:41:38 +0200433 return;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500434 }
Alexander Larsson80f91632013-05-22 14:41:38 +0200435
436 for (i = 0; i < nrects; i++) {
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500437 output_rects[i].x = rects[i].x1;
438 output_rects[i].y = rects[i].y1;
439 output_rects[i].width = rects[i].x2 - rects[i].x1;
440 output_rects[i].height = rects[i].y2 - rects[i].y1;
Alexander Larsson80f91632013-05-22 14:41:38 +0200441 }
442
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500443 pixman_region32_fini(&transformed_region);
444
Giulio Camuffo954f1832014-10-11 18:27:30 +0300445 cookie = xcb_set_clip_rectangles_checked(b->conn, XCB_CLIP_ORDERING_UNSORTED,
Alexander Larsson80f91632013-05-22 14:41:38 +0200446 output->gc,
447 0, 0, nrects,
448 output_rects);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300449 err = xcb_request_check(b->conn, cookie);
Alexander Larsson80f91632013-05-22 14:41:38 +0200450 if (err != NULL) {
451 weston_log("Failed to set clip rects, err: %d\n", err->error_code);
452 free(err);
453 }
454 free(output_rects);
455}
456
457
David Herrmann1edf44c2013-10-22 17:11:26 +0200458static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300459x11_output_repaint_shm(struct weston_output *output_base,
460 pixman_region32_t *damage)
461{
Armin Krezović295e9d02016-08-01 19:17:58 +0200462 struct x11_output *output = to_x11_output(output_base);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300463 struct weston_compositor *ec = output->base.compositor;
Armin Krezović295e9d02016-08-01 19:17:58 +0200464 struct x11_backend *b = to_x11_backend(ec);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300465 xcb_void_cookie_t cookie;
466 xcb_generic_error_t *err;
467
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200468 pixman_renderer_output_set_buffer(output_base, output->hw_surface);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300469 ec->renderer->repaint_output(output_base, damage);
470
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300471 pixman_region32_subtract(&ec->primary_plane.damage,
472 &ec->primary_plane.damage, damage);
Alexander Larsson80f91632013-05-22 14:41:38 +0200473 set_clip_for_output(output_base, damage);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300474 cookie = xcb_shm_put_image_checked(b->conn, output->window, output->gc,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300475 pixman_image_get_width(output->hw_surface),
476 pixman_image_get_height(output->hw_surface),
477 0, 0,
478 pixman_image_get_width(output->hw_surface),
479 pixman_image_get_height(output->hw_surface),
480 0, 0, output->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
481 0, output->segment, 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300482 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300483 if (err != NULL) {
484 weston_log("Failed to put shm image, err: %d\n", err->error_code);
485 free(err);
486 }
487
488 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200489 return 0;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300490}
491
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100492static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400493finish_frame_handler(void *data)
494{
495 struct x11_output *output = data;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200496 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200497
Pekka Paalanen662f3842015-03-18 12:17:26 +0200498 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200499 weston_output_finish_frame(&output->base, &ts, 0);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400500
501 return 1;
502}
503
Matt Roper361d2ad2011-08-29 13:52:23 -0700504static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300505x11_output_deinit_shm(struct x11_backend *b, struct x11_output *output)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300506{
507 xcb_void_cookie_t cookie;
508 xcb_generic_error_t *err;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300509 xcb_free_gc(b->conn, output->gc);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300510
511 pixman_image_unref(output->hw_surface);
512 output->hw_surface = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300513 cookie = xcb_shm_detach_checked(b->conn, output->segment);
514 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300515 if (err) {
516 weston_log("xcb_shm_detach failed, error %d\n", err->error_code);
517 free(err);
518 }
519 shmdt(output->buf);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300520}
521
522static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300523x11_output_set_wm_protocols(struct x11_backend *b,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500524 struct x11_output *output)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400525{
526 xcb_atom_t list[1];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400527
Giulio Camuffo954f1832014-10-11 18:27:30 +0300528 list[0] = b->atom.wm_delete_window;
529 xcb_change_property (b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400530 XCB_PROP_MODE_REPLACE,
531 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300532 b->atom.wm_protocols,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400533 XCB_ATOM_ATOM,
534 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500535 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400536 list);
537}
538
539struct wm_normal_hints {
540 uint32_t flags;
541 uint32_t pad[4];
542 int32_t min_width, min_height;
543 int32_t max_width, max_height;
544 int32_t width_inc, height_inc;
545 int32_t min_aspect_x, min_aspect_y;
546 int32_t max_aspect_x, max_aspect_y;
547 int32_t base_width, base_height;
548 int32_t win_gravity;
549};
550
551#define WM_NORMAL_HINTS_MIN_SIZE 16
552#define WM_NORMAL_HINTS_MAX_SIZE 32
553
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500554static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300555x11_output_set_icon(struct x11_backend *b,
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400556 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500557{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400558 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400559 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400560 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500561
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400562 image = load_image(filename);
563 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500564 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400565 width = pixman_image_get_width(image);
566 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500567 icon = malloc(width * height * 4 + 8);
568 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400569 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500570 return;
571 }
572
573 icon[0] = width;
574 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400575 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300576 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
577 b->atom.net_wm_icon, b->atom.cardinal, 32,
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500578 width * height + 2, icon);
579 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400580 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500581}
582
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500583static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300584x11_output_wait_for_map(struct x11_backend *b, struct x11_output *output)
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500585{
586 xcb_map_notify_event_t *map_notify;
587 xcb_configure_notify_event_t *configure_notify;
588 xcb_generic_event_t *event;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500589 int mapped = 0, configured = 0;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500590 uint8_t response_type;
591
592 /* This isn't the nicest way to do this. Ideally, we could
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500593 * just go back to the main loop and once we get the configure
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500594 * notify, we add the output to the compositor. While we do
595 * support output hotplug, we can't start up with no outputs.
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500596 * We could add the output and then resize once we get the
597 * configure notify, but we don't want to start up and
598 * immediately resize the output.
599 *
600 * Also, some window managers don't give us our final
601 * fullscreen size before map_notify, so if we don't get a
602 * configure_notify before map_notify, we just wait for the
603 * first one and hope that's our size. */
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500604
Giulio Camuffo954f1832014-10-11 18:27:30 +0300605 xcb_flush(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500606
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500607 while (!mapped || !configured) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300608 event = xcb_wait_for_event(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500609 response_type = event->response_type & ~0x80;
610
611 switch (response_type) {
612 case XCB_MAP_NOTIFY:
613 map_notify = (xcb_map_notify_event_t *) event;
614 if (map_notify->window == output->window)
615 mapped = 1;
616 break;
617
618 case XCB_CONFIGURE_NOTIFY:
619 configure_notify =
620 (xcb_configure_notify_event_t *) event;
621
Alexander Larsson4ea95522013-05-22 14:41:37 +0200622
623 if (configure_notify->width % output->scale != 0 ||
624 configure_notify->height % output->scale != 0)
625 weston_log("Resolution is not a multiple of screen size, rounding\n");
Jason Ekstrandd89a0942014-03-07 15:29:14 -0600626 output->mode.width = configure_notify->width;
627 output->mode.height = configure_notify->height;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500628 configured = 1;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500629 break;
630 }
631 }
632}
633
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300634static xcb_visualtype_t *
635find_visual_by_id(xcb_screen_t *screen,
636 xcb_visualid_t id)
637{
638 xcb_depth_iterator_t i;
639 xcb_visualtype_iterator_t j;
640 for (i = xcb_screen_allowed_depths_iterator(screen);
641 i.rem;
642 xcb_depth_next(&i)) {
643 for (j = xcb_depth_visuals_iterator(i.data);
644 j.rem;
645 xcb_visualtype_next(&j)) {
646 if (j.data->visual_id == id)
647 return j.data;
648 }
649 }
650 return 0;
651}
652
653static uint8_t
654get_depth_of_visual(xcb_screen_t *screen,
655 xcb_visualid_t id)
656{
657 xcb_depth_iterator_t i;
658 xcb_visualtype_iterator_t j;
659 for (i = xcb_screen_allowed_depths_iterator(screen);
660 i.rem;
661 xcb_depth_next(&i)) {
662 for (j = xcb_depth_visuals_iterator(i.data);
663 j.rem;
664 xcb_visualtype_next(&j)) {
665 if (j.data->visual_id == id)
666 return i.data->depth;
667 }
668 }
669 return 0;
670}
671
672static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300673x11_output_init_shm(struct x11_backend *b, struct x11_output *output,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300674 int width, int height)
675{
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300676 xcb_visualtype_t *visual_type;
Marko61b4d3e2015-10-05 17:27:54 -0500677 xcb_screen_t *screen;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300678 xcb_format_iterator_t fmt;
679 xcb_void_cookie_t cookie;
680 xcb_generic_error_t *err;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300681 const xcb_query_extension_reply_t *ext;
682 int bitsperpixel = 0;
683 pixman_format_code_t pixman_format;
684
685 /* Check if SHM is available */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300686 ext = xcb_get_extension_data(b->conn, &xcb_shm_id);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300687 if (ext == NULL || !ext->present) {
688 /* SHM is missing */
689 weston_log("SHM extension is not available\n");
690 errno = ENOENT;
691 return -1;
692 }
693
Marko61b4d3e2015-10-05 17:27:54 -0500694 screen = x11_compositor_get_default_screen(b);
695 visual_type = find_visual_by_id(screen, screen->root_visual);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300696 if (!visual_type) {
697 weston_log("Failed to lookup visual for root window\n");
698 errno = ENOENT;
699 return -1;
700 }
701 weston_log("Found visual, bits per value: %d, red_mask: %.8x, green_mask: %.8x, blue_mask: %.8x\n",
702 visual_type->bits_per_rgb_value,
703 visual_type->red_mask,
704 visual_type->green_mask,
705 visual_type->blue_mask);
Marko61b4d3e2015-10-05 17:27:54 -0500706 output->depth = get_depth_of_visual(screen, screen->root_visual);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300707 weston_log("Visual depth is %d\n", output->depth);
708
Giulio Camuffo954f1832014-10-11 18:27:30 +0300709 for (fmt = xcb_setup_pixmap_formats_iterator(xcb_get_setup(b->conn));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300710 fmt.rem;
711 xcb_format_next(&fmt)) {
712 if (fmt.data->depth == output->depth) {
713 bitsperpixel = fmt.data->bits_per_pixel;
714 break;
715 }
716 }
717 weston_log("Found format for depth %d, bpp: %d\n",
718 output->depth, bitsperpixel);
719
720 if (bitsperpixel == 32 &&
721 visual_type->red_mask == 0xff0000 &&
722 visual_type->green_mask == 0x00ff00 &&
723 visual_type->blue_mask == 0x0000ff) {
724 weston_log("Will use x8r8g8b8 format for SHM surfaces\n");
725 pixman_format = PIXMAN_x8r8g8b8;
MoD1b55a592013-11-28 05:24:21 +0000726 } else if (bitsperpixel == 16 &&
727 visual_type->red_mask == 0x00f800 &&
728 visual_type->green_mask == 0x0007e0 &&
729 visual_type->blue_mask == 0x00001f) {
730 weston_log("Will use r5g6b5 format for SHM surfaces\n");
731 pixman_format = PIXMAN_r5g6b5;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300732 } else {
733 weston_log("Can't find appropriate format for SHM pixmap\n");
734 errno = ENOTSUP;
735 return -1;
736 }
737
738
739 /* Create SHM segment and attach it */
740 output->shm_id = shmget(IPC_PRIVATE, width * height * (bitsperpixel / 8), IPC_CREAT | S_IRWXU);
741 if (output->shm_id == -1) {
742 weston_log("x11shm: failed to allocate SHM segment\n");
743 return -1;
744 }
745 output->buf = shmat(output->shm_id, NULL, 0 /* read/write */);
746 if (-1 == (long)output->buf) {
747 weston_log("x11shm: failed to attach SHM segment\n");
748 return -1;
749 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300750 output->segment = xcb_generate_id(b->conn);
751 cookie = xcb_shm_attach_checked(b->conn, output->segment, output->shm_id, 1);
752 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300753 if (err) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700754 weston_log("x11shm: xcb_shm_attach error %d, op code %d, resource id %d\n",
755 err->error_code, err->major_code, err->minor_code);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300756 free(err);
757 return -1;
758 }
759
760 shmctl(output->shm_id, IPC_RMID, NULL);
761
762 /* Now create pixman image */
763 output->hw_surface = pixman_image_create_bits(pixman_format, width, height, output->buf,
764 width * (bitsperpixel / 8));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300765
Giulio Camuffo954f1832014-10-11 18:27:30 +0300766 output->gc = xcb_generate_id(b->conn);
767 xcb_create_gc(b->conn, output->gc, output->window, 0, NULL);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300768
769 return 0;
770}
771
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200772static int
773x11_output_disable(struct weston_output *base)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400774{
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200775 struct x11_output *output = to_x11_output(base);
776 struct x11_backend *backend = to_x11_backend(base->compositor);
777
778 if (!output->base.enabled)
779 return 0;
780
781 wl_event_source_remove(output->finish_frame_timer);
782
783 if (backend->use_pixman) {
784 pixman_renderer_output_destroy(&output->base);
785 x11_output_deinit_shm(backend, output);
786 } else {
787 gl_renderer->output_destroy(&output->base);
788 }
789
790 xcb_destroy_window(backend->conn, output->window);
791 xcb_flush(backend->conn);
792
793 return 0;
794}
795
796static void
797x11_output_destroy(struct weston_output *base)
798{
799 struct x11_output *output = to_x11_output(base);
800
801 x11_output_disable(&output->base);
802 weston_output_destroy(&output->base);
803
804 free(output);
805}
806
807static int
808x11_output_enable(struct weston_output *base)
809{
810 struct x11_output *output = to_x11_output(base);
811 struct x11_backend *b = to_x11_backend(base->compositor);
812
Scott Moreau1bad5db2012-08-18 01:04:05 -0600813 static const char name[] = "Weston Compositor";
Kristian Høgsberg86000402012-01-03 23:24:14 -0500814 static const char class[] = "weston-1\0Weston Compositor";
Benoit Gschwind5c2f20e2016-06-05 19:01:11 +0200815 char *title = NULL;
Marko61b4d3e2015-10-05 17:27:54 -0500816 xcb_screen_t *screen;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400817 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400818 struct wl_event_loop *loop;
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200819
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300820 int ret;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400821 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500822 xcb_atom_t atom_list[1];
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300823 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400824 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300825 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400826 0
827 };
828
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200829 if (!b->no_input)
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300830 values[0] |=
831 XCB_EVENT_MASK_KEY_PRESS |
832 XCB_EVENT_MASK_KEY_RELEASE |
833 XCB_EVENT_MASK_BUTTON_PRESS |
834 XCB_EVENT_MASK_BUTTON_RELEASE |
835 XCB_EVENT_MASK_POINTER_MOTION |
836 XCB_EVENT_MASK_ENTER_WINDOW |
837 XCB_EVENT_MASK_LEAVE_WINDOW |
838 XCB_EVENT_MASK_KEYMAP_STATE |
839 XCB_EVENT_MASK_FOCUS_CHANGE;
840
Giulio Camuffo954f1832014-10-11 18:27:30 +0300841 values[1] = b->null_cursor;
842 output->window = xcb_generate_id(b->conn);
Marko61b4d3e2015-10-05 17:27:54 -0500843 screen = x11_compositor_get_default_screen(b);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300844 xcb_create_window(b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400845 XCB_COPY_FROM_PARENT,
846 output->window,
Marko61b4d3e2015-10-05 17:27:54 -0500847 screen->root,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400848 0, 0,
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200849 output->base.current_mode->width,
850 output->base.current_mode->height,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400851 0,
852 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Marko61b4d3e2015-10-05 17:27:54 -0500853 screen->root_visual,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400854 mask, values);
855
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200856 if (b->fullscreen) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300857 atom_list[0] = b->atom.net_wm_state_fullscreen;
858 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500859 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300860 b->atom.net_wm_state,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500861 XCB_ATOM_ATOM, 32,
862 ARRAY_LENGTH(atom_list), atom_list);
863 } else {
864 /* Don't resize me. */
865 memset(&normal_hints, 0, sizeof normal_hints);
866 normal_hints.flags =
867 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200868 normal_hints.min_width = output->base.current_mode->width;
869 normal_hints.min_height = output->base.current_mode->height;
870 normal_hints.max_width = output->base.current_mode->width;
871 normal_hints.max_height = output->base.current_mode->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300872 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
873 b->atom.wm_normal_hints,
874 b->atom.wm_size_hints, 32,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500875 sizeof normal_hints / 4,
876 (uint8_t *) &normal_hints);
877 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400878
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400879 /* Set window name. Don't bother with non-EWMH WMs. */
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200880 if (output->base.name) {
881 if (asprintf(&title, "%s - %s", name, output->base.name) < 0)
Benoit Gschwind5c2f20e2016-06-05 19:01:11 +0200882 title = NULL;
883 } else {
884 title = strdup(name);
885 }
886
887 if (title) {
888 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
889 b->atom.net_wm_name, b->atom.utf8_string, 8,
890 strlen(title), title);
891 free(title);
892 } else {
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200893 goto err;
Benoit Gschwind5c2f20e2016-06-05 19:01:11 +0200894 }
895
Giulio Camuffo954f1832014-10-11 18:27:30 +0300896 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
897 b->atom.wm_class, b->atom.string, 8,
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500898 sizeof class, class);
899
Giulio Camuffo954f1832014-10-11 18:27:30 +0300900 x11_output_set_icon(b, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500901
Giulio Camuffo954f1832014-10-11 18:27:30 +0300902 x11_output_set_wm_protocols(b, output);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500903
Giulio Camuffo954f1832014-10-11 18:27:30 +0300904 xcb_map_window(b->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400905
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200906 if (b->fullscreen)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300907 x11_output_wait_for_map(b, output);
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400908
Giulio Camuffo954f1832014-10-11 18:27:30 +0300909 if (b->use_pixman) {
910 if (x11_output_init_shm(b, output,
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200911 output->base.current_mode->width,
912 output->base.current_mode->height) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700913 weston_log("Failed to initialize SHM for the X11 output\n");
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200914 goto err;
Bryce Harrington665b0252015-05-26 18:14:21 -0700915 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300916 if (pixman_renderer_output_create(&output->base) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700917 weston_log("Failed to create pixman renderer for output\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300918 x11_output_deinit_shm(b, output);
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200919 goto err;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300920 }
921 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +0100922 /* eglCreatePlatformWindowSurfaceEXT takes a Window*
923 * but eglCreateWindowSurface takes a Window. */
924 Window xid = (Window) output->window;
925
Miguel A. Vicoc095cde2016-05-18 17:43:00 +0200926 ret = gl_renderer->output_window_create(
927 &output->base,
928 (EGLNativeWindowType) output->window,
929 &xid,
930 gl_renderer->opaque_attribs,
931 NULL,
932 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300933 if (ret < 0)
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200934 goto err;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300935 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100936
Giulio Camuffo954f1832014-10-11 18:27:30 +0300937 loop = wl_display_get_event_loop(b->compositor->wl_display);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100938 output->finish_frame_timer =
939 wl_event_loop_add_timer(loop, finish_frame_handler, output);
940
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400941 weston_log("x11 output %dx%d, window id %d\n",
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200942 output->base.current_mode->width,
943 output->base.current_mode->height,
944 output->window);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400945
Armin Krezovićc3d2f962016-09-30 14:11:10 +0200946 return 0;
947
948err:
949 xcb_destroy_window(b->conn, output->window);
950 xcb_flush(b->conn);
951
952 return -1;
953}
954
955static int
956x11_output_set_size(struct weston_output *base, int width, int height)
957{
958 struct x11_output *output = to_x11_output(base);
959 struct x11_backend *b = to_x11_backend(base->compositor);
960 int output_width, output_height;
961
962 /* We can only be called once. */
963 assert(!output->base.current_mode);
964
965 /* Make sure we have scale set. */
966 assert(output->base.scale);
967
968 if (width < 1) {
969 weston_log("Invalid width \"%d\" for output %s\n",
970 width, output->base.name);
971 return -1;
972 }
973
974 if (height < 1) {
975 weston_log("Invalid height \"%d\" for output %s\n",
976 height, output->base.name);
977 return -1;
978 }
979
980 output_width = width * output->base.scale;
981 output_height = height * output->base.scale;
982
983 output->mode.flags =
984 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
985
986 output->mode.width = output_width;
987 output->mode.height = output_height;
988 output->mode.refresh = 60000;
989 output->scale = output->base.scale;
990 wl_list_init(&output->base.mode_list);
991 wl_list_insert(&output->base.mode_list, &output->mode.link);
992
993 output->base.current_mode = &output->mode;
994 output->base.make = "weston-X11";
995 output->base.model = "none";
996
997 output->base.mm_width = width * b->screen->width_in_millimeters /
998 b->screen->width_in_pixels;
999 output->base.mm_height = height * b->screen->height_in_millimeters /
1000 b->screen->height_in_pixels;
1001
1002 if (b->use_pixman)
1003 output->base.repaint = x11_output_repaint_shm;
1004 else
1005 output->base.repaint = x11_output_repaint_gl;
1006
1007 output->base.start_repaint_loop = x11_output_start_repaint_loop;
1008 output->base.assign_planes = NULL;
1009 output->base.set_backlight = NULL;
1010 output->base.set_dpms = NULL;
1011 output->base.switch_mode = NULL;
1012
1013 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
1031 output->base.name = strdup(name);
1032 output->base.destroy = x11_output_destroy;
1033 output->base.disable = x11_output_disable;
1034 output->base.enable = x11_output_enable;
1035
Armin Krezović40087402016-09-30 14:11:12 +02001036 weston_output_init(&output->base, compositor);
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{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001302 if (mask & WL_EVENT_READABLE) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001303 *event = xcb_poll_for_event(b->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -04001304 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001305#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Giulio Camuffo954f1832014-10-11 18:27:30 +03001306 *event = xcb_poll_for_queued_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001307#else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001308 *event = xcb_poll_for_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001309#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -04001310 }
1311
1312 return *event != NULL;
1313}
1314
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001315static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001316x11_backend_handle_event(int fd, uint32_t mask, void *data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001317{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001318 struct x11_backend *b = data;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001319 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001320 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001321 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001322 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001323 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001324 xcb_keymap_notify_event_t *keymap_notify;
1325 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001326 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001327 xcb_atom_t atom;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001328 xcb_window_t window;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001329 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001330 uint32_t i, set;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001331 uint8_t response_type;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001332 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001333
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001334 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001335 count = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001336 while (x11_backend_next_event(b, &event, mask)) {
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001337 response_type = event->response_type & ~0x80;
1338
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001339 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1340 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001341 /* Suppress key repeat events; this is only used if we
1342 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001343 key_release = (xcb_key_press_event_t *) prev;
1344 key_press = (xcb_key_press_event_t *) event;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001345 if (response_type == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -07001346 key_release->time == key_press->time &&
1347 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001348 /* Don't deliver the held key release
1349 * event or the new key press event. */
1350 free(event);
1351 free(prev);
1352 prev = NULL;
1353 continue;
1354 } else {
1355 /* Deliver the held key release now
1356 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001357 * event below. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001358 update_xkb_state_from_core(b, key_release->state);
1359 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001360 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001361 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001362 WL_KEYBOARD_KEY_STATE_RELEASED,
1363 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001364 free(prev);
1365 prev = NULL;
1366 break;
1367 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001368
1369 case XCB_FOCUS_IN:
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001370 assert(response_type == XCB_KEYMAP_NOTIFY);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001371 keymap_notify = (xcb_keymap_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001372 b->keys.size = 0;
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001373 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
1374 set = keymap_notify->keys[i >> 3] &
1375 (1 << (i & 7));
1376 if (set) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001377 k = wl_array_add(&b->keys, sizeof *k);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001378 *k = i;
1379 }
1380 }
1381
Daniel Stoned6da09e2012-06-22 13:21:29 +01001382 /* Unfortunately the state only comes with the enter
1383 * event, rather than with the focus event. I'm not
1384 * sure of the exact semantics around it and whether
1385 * we can ensure that we get both? */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001386 notify_keyboard_focus_in(&b->core_seat, &b->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001387 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001388
1389 free(prev);
1390 prev = NULL;
1391 break;
1392
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001393 default:
1394 /* No previous event held */
1395 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001396 }
1397
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001398 switch (response_type) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001399 case XCB_KEY_PRESS:
1400 key_press = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001401 if (!b->has_xkb)
1402 update_xkb_state_from_core(b, key_press->state);
1403 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001404 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001405 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001406 WL_KEYBOARD_KEY_STATE_PRESSED,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001407 b->has_xkb ? STATE_UPDATE_NONE :
Daniel Stonee2faa122012-06-22 13:21:39 +01001408 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001409 break;
1410 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001411 /* If we don't have XKB, we need to use the lame
1412 * autorepeat detection above. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001413 if (!b->has_xkb) {
Daniel Stone3ee91e12012-06-22 13:21:36 +01001414 prev = event;
1415 break;
1416 }
1417 key_release = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001418 notify_key(&b->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +01001419 weston_compositor_get_time(),
1420 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001421 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +01001422 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001423 break;
1424 case XCB_BUTTON_PRESS:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001425 case XCB_BUTTON_RELEASE:
Benoit Gschwind51c6f632016-05-18 21:32:12 +02001426 x11_backend_deliver_button_event(b, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001427 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001428 case XCB_MOTION_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001429 x11_backend_deliver_motion_event(b, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001430 break;
1431
1432 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001433 expose = (xcb_expose_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001434 output = x11_backend_find_output(b, expose->window);
Derek Foreman99bfa642014-12-11 15:44:46 -06001435 if (!output)
1436 break;
1437
Kristian Høgsberga98c2e12013-12-05 12:30:37 -08001438 weston_output_damage(&output->base);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001439 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001440 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001441
1442 case XCB_ENTER_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001443 x11_backend_deliver_enter_event(b, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001444 break;
1445
1446 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001447 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -05001448 if (enter_notify->state >= Button1Mask)
1449 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001450 if (!b->has_xkb)
1451 update_xkb_state_from_core(b, enter_notify->state);
1452 notify_pointer_focus(&b->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001453 break;
1454
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001455 case XCB_CLIENT_MESSAGE:
1456 client_message = (xcb_client_message_event_t *) event;
1457 atom = client_message->data.data32[0];
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001458 window = client_message->window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001459 if (atom == b->atom.wm_delete_window) {
Derek Foremand540f4b2015-01-27 16:26:49 -06001460 struct wl_event_loop *loop;
1461 struct window_delete_data *data = malloc(sizeof *data);
1462
1463 /* if malloc failed we should at least try to
1464 * delete the window, even if it may result in
1465 * a crash.
1466 */
1467 if (!data) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001468 x11_backend_delete_window(b, window);
Derek Foremand540f4b2015-01-27 16:26:49 -06001469 break;
1470 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001471 data->backend = b;
Derek Foremand540f4b2015-01-27 16:26:49 -06001472 data->window = window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001473 loop = wl_display_get_event_loop(b->compositor->wl_display);
Derek Foremand540f4b2015-01-27 16:26:49 -06001474 wl_event_loop_add_idle(loop, delete_cb, data);
1475 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001476 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001477
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001478 case XCB_FOCUS_IN:
1479 focus_in = (xcb_focus_in_event_t *) event;
1480 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1481 break;
1482
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001483 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001484 break;
1485
1486 case XCB_FOCUS_OUT:
1487 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -05001488 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1489 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001490 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001491 notify_keyboard_focus_out(&b->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001492 break;
1493
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001494 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001495 break;
1496 }
1497
Daniel Stonee2faa122012-06-22 13:21:39 +01001498#ifdef HAVE_XCB_XKB
Giulio Camuffo954f1832014-10-11 18:27:30 +03001499 if (b->has_xkb) {
1500 if (response_type == b->xkb_event_base) {
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001501 xcb_xkb_state_notify_event_t *state =
1502 (xcb_xkb_state_notify_event_t *) event;
1503 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001504 update_xkb_state(b, state);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001505 } else if (response_type == XCB_PROPERTY_NOTIFY) {
1506 xcb_property_notify_event_t *prop_notify =
1507 (xcb_property_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001508 if (prop_notify->window == b->screen->root &&
1509 prop_notify->atom == b->atom.xkb_names &&
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001510 prop_notify->state == XCB_PROPERTY_NEW_VALUE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001511 update_xkb_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001512 }
Daniel Stonee2faa122012-06-22 13:21:39 +01001513 }
1514#endif
1515
Bill Spitzakb715cec2012-06-05 17:08:23 -04001516 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001517 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001518 free (event);
1519 }
1520
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001521 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1522 case XCB_KEY_RELEASE:
1523 key_release = (xcb_key_press_event_t *) prev;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001524 update_xkb_state_from_core(b, key_release->state);
1525 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001526 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001527 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001528 WL_KEYBOARD_KEY_STATE_RELEASED,
1529 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001530 free(prev);
1531 prev = NULL;
1532 break;
1533 default:
1534 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001535 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001536
Bill Spitzakb715cec2012-06-05 17:08:23 -04001537 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001538}
1539
Giulio Camuffo954f1832014-10-11 18:27:30 +03001540#define F(field) offsetof(struct x11_backend, field)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001541
1542static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001543x11_backend_get_resources(struct x11_backend *b)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001544{
1545 static const struct { const char *name; int offset; } atoms[] = {
1546 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1547 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1548 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1549 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001550 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001551 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001552 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001553 { "_NET_WM_STATE", F(atom.net_wm_state) },
1554 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001555 { "_NET_SUPPORTING_WM_CHECK",
1556 F(atom.net_supporting_wm_check) },
1557 { "_NET_SUPPORTED", F(atom.net_supported) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001558 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001559 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001560 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001561 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001562 };
1563
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001564 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001565 xcb_intern_atom_reply_t *reply;
1566 xcb_pixmap_t pixmap;
1567 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001568 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001569 uint8_t data[] = { 0, 0, 0, 0 };
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 cookies[i] = xcb_intern_atom (b->conn, 0,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001573 strlen(atoms[i].name),
1574 atoms[i].name);
1575
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001576 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001577 reply = xcb_intern_atom_reply (b->conn, cookies[i], NULL);
1578 *(xcb_atom_t *) ((char *) b + atoms[i].offset) = reply->atom;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001579 free(reply);
1580 }
1581
Giulio Camuffo954f1832014-10-11 18:27:30 +03001582 pixmap = xcb_generate_id(b->conn);
1583 gc = xcb_generate_id(b->conn);
1584 xcb_create_pixmap(b->conn, 1, pixmap, b->screen->root, 1, 1);
1585 xcb_create_gc(b->conn, gc, pixmap, 0, NULL);
1586 xcb_put_image(b->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001587 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001588 b->null_cursor = xcb_generate_id(b->conn);
1589 xcb_create_cursor (b->conn, b->null_cursor,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001590 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001591 xcb_free_gc(b->conn, gc);
1592 xcb_free_pixmap(b->conn, pixmap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001593}
1594
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001595static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001596x11_backend_get_wm_info(struct x11_backend *c)
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001597{
1598 xcb_get_property_cookie_t cookie;
1599 xcb_get_property_reply_t *reply;
1600 xcb_atom_t *atom;
1601 unsigned int i;
1602
1603 cookie = xcb_get_property(c->conn, 0, c->screen->root,
1604 c->atom.net_supported,
1605 XCB_ATOM_ATOM, 0, 1024);
1606 reply = xcb_get_property_reply(c->conn, cookie, NULL);
1607 if (reply == NULL)
1608 return;
1609
1610 atom = (xcb_atom_t *) xcb_get_property_value(reply);
1611
1612 for (i = 0; i < reply->value_len; i++) {
1613 if (atom[i] == c->atom.net_wm_state_fullscreen)
1614 c->has_net_wm_state_fullscreen = 1;
1615 }
Ryo Munakata76fb7ec2015-03-08 19:17:06 +09001616
1617 free(reply);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001618}
1619
1620static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001621x11_restore(struct weston_compositor *ec)
1622{
1623}
1624
1625static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001626x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001627{
Armin Krezović295e9d02016-08-01 19:17:58 +02001628 struct x11_backend *backend = to_x11_backend(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001629
Giulio Camuffo954f1832014-10-11 18:27:30 +03001630 wl_event_source_remove(backend->xcb_source);
1631 x11_input_destroy(backend);
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001632
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03001633 weston_compositor_shutdown(ec); /* destroys outputs, too */
1634
Giulio Camuffo954f1832014-10-11 18:27:30 +03001635 XCloseDisplay(backend->dpy);
1636 free(backend);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001637}
1638
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001639static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001640init_gl_renderer(struct x11_backend *b)
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001641{
1642 int ret;
1643
1644 gl_renderer = weston_load_module("gl-renderer.so",
1645 "gl_renderer_interface");
1646 if (!gl_renderer)
1647 return -1;
1648
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001649 ret = gl_renderer->display_create(b->compositor, EGL_PLATFORM_X11_KHR,
1650 (void *) b->dpy,
1651 gl_renderer->opaque_attribs, NULL, 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001652
1653 return ret;
1654}
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001655
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001656static const struct weston_windowed_output_api api = {
1657 x11_output_set_size,
1658 x11_output_create,
1659};
1660
Giulio Camuffo954f1832014-10-11 18:27:30 +03001661static struct x11_backend *
1662x11_backend_create(struct weston_compositor *compositor,
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001663 struct weston_x11_backend_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001664{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001665 struct x11_backend *b;
Pekka Paalanen157109b2016-03-24 15:43:44 +02001666 struct wl_event_loop *loop;
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001667 int ret;
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001668
Giulio Camuffo954f1832014-10-11 18:27:30 +03001669 b = zalloc(sizeof *b);
1670 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001671 return NULL;
1672
Giulio Camuffo954f1832014-10-11 18:27:30 +03001673 b->compositor = compositor;
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001674 b->fullscreen = config->fullscreen;
1675 b->no_input = config->no_input;
1676
Giulio Camuffo954f1832014-10-11 18:27:30 +03001677 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001678 goto err_free;
1679
Giulio Camuffo954f1832014-10-11 18:27:30 +03001680 b->dpy = XOpenDisplay(NULL);
1681 if (b->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001682 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001683
Giulio Camuffo954f1832014-10-11 18:27:30 +03001684 b->conn = XGetXCBConnection(b->dpy);
1685 XSetEventQueueOwner(b->dpy, XCBOwnsEventQueue);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001686
Giulio Camuffo954f1832014-10-11 18:27:30 +03001687 if (xcb_connection_has_error(b->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001688 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001689
Marko61b4d3e2015-10-05 17:27:54 -05001690 b->screen = x11_compositor_get_default_screen(b);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001691 wl_array_init(&b->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001692
Giulio Camuffo954f1832014-10-11 18:27:30 +03001693 x11_backend_get_resources(b);
1694 x11_backend_get_wm_info(b);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001695
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001696 if (!b->has_net_wm_state_fullscreen && config->fullscreen) {
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001697 weston_log("Can not fullscreen without window manager support"
1698 "(need _NET_WM_STATE_FULLSCREEN)\n");
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001699 config->fullscreen = 0;
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001700 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001701
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001702 b->use_pixman = config->use_pixman;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001703 if (b->use_pixman) {
1704 if (pixman_renderer_init(compositor) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001705 weston_log("Failed to initialize pixman renderer for X11 backend\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001706 goto err_xdisplay;
Bryce Harrington665b0252015-05-26 18:14:21 -07001707 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001708 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001709 else if (init_gl_renderer(b) < 0) {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001710 goto err_xdisplay;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001711 }
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001712 weston_log("Using %s renderer\n", config->use_pixman ? "pixman" : "gl");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001713
Giulio Camuffo954f1832014-10-11 18:27:30 +03001714 b->base.destroy = x11_destroy;
1715 b->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001716
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001717 if (x11_input_create(b, config->no_input) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001718 weston_log("Failed to create X11 input\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001719 goto err_renderer;
Bryce Harrington665b0252015-05-26 18:14:21 -07001720 }
Daniel Stone22815f92012-06-22 13:21:34 +01001721
Pekka Paalanen157109b2016-03-24 15:43:44 +02001722 loop = wl_display_get_event_loop(compositor->wl_display);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001723 b->xcb_source =
Pekka Paalanen157109b2016-03-24 15:43:44 +02001724 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001725 xcb_get_file_descriptor(b->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001726 WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001727 x11_backend_handle_event, b);
1728 wl_event_source_check(b->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001729
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001730 if (compositor->renderer->import_dmabuf) {
1731 if (linux_dmabuf_setup(compositor) < 0)
1732 weston_log("Error: initializing dmabuf "
1733 "support failed.\n");
1734 }
1735
Giulio Camuffo954f1832014-10-11 18:27:30 +03001736 compositor->backend = &b->base;
Pekka Paalanen5ffb4402014-06-12 16:52:53 +03001737
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001738 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
1739 &api, sizeof(api));
1740
1741 if (ret < 0) {
1742 weston_log("Failed to register output API.\n");
1743 goto err_x11_input;
1744 }
1745
Giulio Camuffo954f1832014-10-11 18:27:30 +03001746 return b;
Martin Olsson11434bb2012-07-08 03:03:44 +02001747
1748err_x11_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001749 x11_input_destroy(b);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001750err_renderer:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001751 compositor->renderer->destroy(compositor);
Martin Olsson11434bb2012-07-08 03:03:44 +02001752err_xdisplay:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001753 XCloseDisplay(b->dpy);
Martin Olsson11434bb2012-07-08 03:03:44 +02001754err_free:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001755 free(b);
Martin Olsson11434bb2012-07-08 03:03:44 +02001756 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001757}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001758
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001759static void
1760config_init_to_defaults(struct weston_x11_backend_config *config)
1761{
1762}
1763
Giulio Camuffo954f1832014-10-11 18:27:30 +03001764WL_EXPORT int
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001765backend_init(struct weston_compositor *compositor,
Giulio Camuffo93daabb2015-10-17 19:24:14 +03001766 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001767{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001768 struct x11_backend *b;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001769 struct weston_x11_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001770
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001771 if (config_base == NULL ||
1772 config_base->struct_version != WESTON_X11_BACKEND_CONFIG_VERSION ||
1773 config_base->struct_size > sizeof(struct weston_x11_backend_config)) {
1774 weston_log("X11 backend config structure is invalid\n");
1775 return -1;
1776 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001777
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001778 config_init_to_defaults(&config);
1779 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001780
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001781 b = x11_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001782 if (b == NULL)
1783 return -1;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001784
Giulio Camuffo954f1832014-10-11 18:27:30 +03001785 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001786}