blob: 73ba783fef4b2b9b8cdc805a84aa69c2552d5977 [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>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040032#include <stdlib.h>
33#include <string.h>
34#include <fcntl.h>
35#include <unistd.h>
36#include <errno.h>
37#include <sys/time.h>
Vasily Khoruzhickb3add192013-01-07 20:39:50 +030038#include <sys/shm.h>
Kristian Høgsbergf9112b22010-06-14 12:53:43 -040039#include <linux/input.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040040
41#include <xcb/xcb.h>
Vasily Khoruzhickb3add192013-01-07 20:39:50 +030042#include <xcb/shm.h>
Daniel Stone62b33b62012-06-22 13:21:35 +010043#ifdef HAVE_XCB_XKB
44#include <xcb/xkb.h>
45#endif
46
Benjamin Franzke3b288af2011-02-20 19:58:42 +010047#include <X11/Xlib.h>
48#include <X11/Xlib-xcb.h>
49
Daniel Stone576cd152012-06-01 12:14:02 +010050#include <xkbcommon/xkbcommon.h>
51
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040052#include "compositor.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010053#include "gl-renderer.h"
Vasily Khoruzhickb3add192013-01-07 20:39:50 +030054#include "pixman-renderer.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"
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020058#include "presentation_timing-server-protocol.h"
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040059
Jonas Ådahl62efe202012-09-27 18:40:41 +020060#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
61
Scott Moreau248aaec2012-08-03 14:19:52 -060062static int option_width;
63static int option_height;
Jason Ekstrandd89a0942014-03-07 15:29:14 -060064static int option_scale;
Scott Moreau248aaec2012-08-03 14:19:52 -060065static int option_count;
Scott Moreau248aaec2012-08-03 14:19:52 -060066
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;
Vasily Khoruzhickd8943512013-01-07 22:36:02 +030080 int use_pixman;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040081
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -050082 int has_net_wm_state_fullscreen;
83
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040084 /* We could map multi-pointer X to multiple wayland seats, but
85 * for now we only support core X input. */
Kristian Høgsberg068b61c2013-02-25 17:04:47 -050086 struct weston_seat core_seat;
Kristian Høgsberg50065962013-03-27 15:14:07 -040087 wl_fixed_t prev_x;
88 wl_fixed_t prev_y;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040089
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040090 struct {
91 xcb_atom_t wm_protocols;
92 xcb_atom_t wm_normal_hints;
93 xcb_atom_t wm_size_hints;
94 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050095 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040096 xcb_atom_t net_wm_name;
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -050097 xcb_atom_t net_supporting_wm_check;
98 xcb_atom_t net_supported;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050099 xcb_atom_t net_wm_icon;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400100 xcb_atom_t net_wm_state;
101 xcb_atom_t net_wm_state_fullscreen;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500102 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400103 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500104 xcb_atom_t cardinal;
Daniel Stone855028f2012-05-01 20:37:10 +0100105 xcb_atom_t xkb_names;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400106 } atom;
107};
108
109struct x11_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500110 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400111
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400112 xcb_window_t window;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500113 struct weston_mode mode;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400114 struct wl_event_source *finish_frame_timer;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300115
116 xcb_gc_t gc;
117 xcb_shm_seg_t segment;
118 pixman_image_t *hw_surface;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300119 int shm_id;
120 void *buf;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300121 uint8_t depth;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200122 int32_t scale;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400123};
124
Derek Foremand540f4b2015-01-27 16:26:49 -0600125struct window_delete_data {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300126 struct x11_backend *backend;
Derek Foremand540f4b2015-01-27 16:26:49 -0600127 xcb_window_t window;
128};
129
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300130struct gl_renderer_interface *gl_renderer;
131
Daniel Stone576cd152012-06-01 12:14:02 +0100132static struct xkb_keymap *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300133x11_backend_get_keymap(struct x11_backend *b)
Daniel Stone576cd152012-06-01 12:14:02 +0100134{
135 xcb_get_property_cookie_t cookie;
136 xcb_get_property_reply_t *reply;
Daniel Stone576cd152012-06-01 12:14:02 +0100137 struct xkb_rule_names names;
138 struct xkb_keymap *ret;
139 const char *value_all, *value_part;
140 int length_all, length_part;
141
142 memset(&names, 0, sizeof(names));
143
Giulio Camuffo954f1832014-10-11 18:27:30 +0300144 cookie = xcb_get_property(b->conn, 0, b->screen->root,
145 b->atom.xkb_names, b->atom.string, 0, 1024);
146 reply = xcb_get_property_reply(b->conn, cookie, NULL);
Daniel Stone576cd152012-06-01 12:14:02 +0100147 if (reply == NULL)
148 return NULL;
149
150 value_all = xcb_get_property_value(reply);
151 length_all = xcb_get_property_value_length(reply);
152 value_part = value_all;
153
154#define copy_prop_value(to) \
155 length_part = strlen(value_part); \
156 if (value_part + length_part < (value_all + length_all) && \
157 length_part > 0) \
158 names.to = value_part; \
159 value_part += length_part + 1;
160
161 copy_prop_value(rules);
162 copy_prop_value(model);
163 copy_prop_value(layout);
164 copy_prop_value(variant);
165 copy_prop_value(options);
166#undef copy_prop_value
167
Giulio Camuffo954f1832014-10-11 18:27:30 +0300168 ret = xkb_keymap_new_from_names(b->compositor->xkb_context, &names, 0);
Daniel Stone576cd152012-06-01 12:14:02 +0100169
170 free(reply);
171 return ret;
172}
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400173
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400174static uint32_t
Giulio Camuffo954f1832014-10-11 18:27:30 +0300175get_xkb_mod_mask(struct x11_backend *b, uint32_t in)
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400176{
Derek Foreman1281a362015-07-31 16:55:32 -0500177 struct weston_keyboard *keyboard =
178 weston_seat_get_keyboard(&b->core_seat);
179 struct weston_xkb_info *info = keyboard->xkb_info;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400180 uint32_t ret = 0;
181
182 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
183 ret |= (1 << info->shift_mod);
184 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
185 ret |= (1 << info->caps_mod);
186 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
187 ret |= (1 << info->ctrl_mod);
188 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
189 ret |= (1 << info->alt_mod);
190 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
191 ret |= (1 << info->mod2_mod);
192 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
193 ret |= (1 << info->mod3_mod);
194 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
195 ret |= (1 << info->super_mod);
196 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
197 ret |= (1 << info->mod5_mod);
198
199 return ret;
200}
201
Daniel Stone62b33b62012-06-22 13:21:35 +0100202static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300203x11_backend_setup_xkb(struct x11_backend *b)
Daniel Stone62b33b62012-06-22 13:21:35 +0100204{
205#ifndef HAVE_XCB_XKB
206 weston_log("XCB-XKB not available during build\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300207 b->has_xkb = 0;
208 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100209 return;
210#else
Derek Foreman1281a362015-07-31 16:55:32 -0500211 struct weston_keyboard *keyboard;
Daniel Stone62b33b62012-06-22 13:21:35 +0100212 const xcb_query_extension_reply_t *ext;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100213 xcb_generic_error_t *error;
Daniel Stonee2faa122012-06-22 13:21:39 +0100214 xcb_void_cookie_t select;
Ran Benita6a39d872012-10-31 20:14:57 +0200215 xcb_xkb_use_extension_cookie_t use_ext;
216 xcb_xkb_use_extension_reply_t *use_ext_reply;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100217 xcb_xkb_per_client_flags_cookie_t pcf;
218 xcb_xkb_per_client_flags_reply_t *pcf_reply;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400219 xcb_xkb_get_state_cookie_t state;
220 xcb_xkb_get_state_reply_t *state_reply;
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200221 uint32_t values[1] = { XCB_EVENT_MASK_PROPERTY_CHANGE };
Daniel Stone62b33b62012-06-22 13:21:35 +0100222
Giulio Camuffo954f1832014-10-11 18:27:30 +0300223 b->has_xkb = 0;
224 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100225
Giulio Camuffo954f1832014-10-11 18:27:30 +0300226 ext = xcb_get_extension_data(b->conn, &xcb_xkb_id);
Daniel Stone62b33b62012-06-22 13:21:35 +0100227 if (!ext) {
228 weston_log("XKB extension not available on host X11 server\n");
229 return;
230 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300231 b->xkb_event_base = ext->first_event;
Daniel Stone62b33b62012-06-22 13:21:35 +0100232
Giulio Camuffo954f1832014-10-11 18:27:30 +0300233 select = xcb_xkb_select_events_checked(b->conn,
Ran Benita424ae012012-10-31 00:13:08 +0200234 XCB_XKB_ID_USE_CORE_KBD,
235 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
236 0,
237 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
238 0,
239 0,
240 NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300241 error = xcb_request_check(b->conn, select);
Daniel Stonee2faa122012-06-22 13:21:39 +0100242 if (error) {
243 weston_log("error: failed to select for XKB state events\n");
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200244 free(error);
Daniel Stonee2faa122012-06-22 13:21:39 +0100245 return;
246 }
247
Giulio Camuffo954f1832014-10-11 18:27:30 +0300248 use_ext = xcb_xkb_use_extension(b->conn,
Ran Benita6a39d872012-10-31 20:14:57 +0200249 XCB_XKB_MAJOR_VERSION,
250 XCB_XKB_MINOR_VERSION);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300251 use_ext_reply = xcb_xkb_use_extension_reply(b->conn, use_ext, NULL);
Ran Benita6a39d872012-10-31 20:14:57 +0200252 if (!use_ext_reply) {
253 weston_log("couldn't start using XKB extension\n");
254 return;
255 }
256
257 if (!use_ext_reply->supported) {
258 weston_log("XKB extension version on the server is too old "
259 "(want %d.%d, has %d.%d)\n",
260 XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION,
261 use_ext_reply->serverMajor, use_ext_reply->serverMinor);
262 free(use_ext_reply);
263 return;
264 }
265 free(use_ext_reply);
266
Giulio Camuffo954f1832014-10-11 18:27:30 +0300267 pcf = xcb_xkb_per_client_flags(b->conn,
Daniel Stone3ee91e12012-06-22 13:21:36 +0100268 XCB_XKB_ID_USE_CORE_KBD,
269 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
270 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
271 0,
272 0,
273 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300274 pcf_reply = xcb_xkb_per_client_flags_reply(b->conn, pcf, NULL);
Ran Benita7109cc82012-10-31 20:14:58 +0200275 if (!pcf_reply ||
276 !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT)) {
Daniel Stone3ee91e12012-06-22 13:21:36 +0100277 weston_log("failed to set XKB per-client flags, not using "
278 "detectable repeat\n");
Ran Benita7109cc82012-10-31 20:14:58 +0200279 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100280 return;
281 }
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200282 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100283
Giulio Camuffo954f1832014-10-11 18:27:30 +0300284 state = xcb_xkb_get_state(b->conn, XCB_XKB_ID_USE_CORE_KBD);
285 state_reply = xcb_xkb_get_state_reply(b->conn, state, NULL);
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200286 if (!state_reply) {
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400287 weston_log("failed to get initial XKB state\n");
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400288 return;
289 }
290
Derek Foreman1281a362015-07-31 16:55:32 -0500291 keyboard = weston_seat_get_keyboard(&b->core_seat);
292 xkb_state_update_mask(keyboard->xkb_state.state,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300293 get_xkb_mod_mask(b, state_reply->baseMods),
294 get_xkb_mod_mask(b, state_reply->latchedMods),
295 get_xkb_mod_mask(b, state_reply->lockedMods),
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400296 0,
297 0,
298 state_reply->group);
299
300 free(state_reply);
301
Giulio Camuffo954f1832014-10-11 18:27:30 +0300302 xcb_change_window_attributes(b->conn, b->screen->root,
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200303 XCB_CW_EVENT_MASK, values);
304
Giulio Camuffo954f1832014-10-11 18:27:30 +0300305 b->has_xkb = 1;
Daniel Stone62b33b62012-06-22 13:21:35 +0100306#endif
307}
308
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200309#ifdef HAVE_XCB_XKB
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200310static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300311update_xkb_keymap(struct x11_backend *b)
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200312{
313 struct xkb_keymap *keymap;
314
Giulio Camuffo954f1832014-10-11 18:27:30 +0300315 keymap = x11_backend_get_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200316 if (!keymap) {
317 weston_log("failed to get XKB keymap\n");
318 return;
319 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300320 weston_seat_update_keymap(&b->core_seat, keymap);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200321 xkb_keymap_unref(keymap);
322}
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200323#endif
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200324
Darxus55973f22010-11-22 21:24:39 -0500325static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300326x11_input_create(struct x11_backend *b, int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400327{
Daniel Stone576cd152012-06-01 12:14:02 +0100328 struct xkb_keymap *keymap;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400329
Giulio Camuffo954f1832014-10-11 18:27:30 +0300330 weston_seat_init(&b->core_seat, b->compositor, "default");
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300331
332 if (no_input)
333 return 0;
334
Giulio Camuffo954f1832014-10-11 18:27:30 +0300335 weston_seat_init_pointer(&b->core_seat);
Daniel Stone576cd152012-06-01 12:14:02 +0100336
Giulio Camuffo954f1832014-10-11 18:27:30 +0300337 keymap = x11_backend_get_keymap(b);
338 if (weston_seat_init_keyboard(&b->core_seat, keymap) < 0)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500339 return -1;
Ran Benitac9c74152014-08-19 23:59:52 +0300340 xkb_keymap_unref(keymap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400341
Giulio Camuffo954f1832014-10-11 18:27:30 +0300342 x11_backend_setup_xkb(b);
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400343
Darxus55973f22010-11-22 21:24:39 -0500344 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400345}
346
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200347static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300348x11_input_destroy(struct x11_backend *b)
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200349{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300350 weston_seat_release(&b->core_seat);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200351}
352
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500353static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200354x11_output_start_repaint_loop(struct weston_output *output)
355{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400356 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200357
Pekka Paalanen662f3842015-03-18 12:17:26 +0200358 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200359 weston_output_finish_frame(output, &ts, PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200360}
361
David Herrmann1edf44c2013-10-22 17:11:26 +0200362static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300363x11_output_repaint_gl(struct weston_output *output_base,
364 pixman_region32_t *damage)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500365{
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500366 struct x11_output *output = (struct x11_output *)output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400367 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500368
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400369 ec->renderer->repaint_output(output_base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500370
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200371 pixman_region32_subtract(&ec->primary_plane.damage,
372 &ec->primary_plane.damage, damage);
373
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500374 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200375 return 0;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500376}
377
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300378static void
Alexander Larsson80f91632013-05-22 14:41:38 +0200379set_clip_for_output(struct weston_output *output_base, pixman_region32_t *region)
380{
381 struct x11_output *output = (struct x11_output *)output_base;
382 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300383 struct x11_backend *b = (struct x11_backend *)ec->backend;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500384 pixman_region32_t transformed_region;
Alexander Larsson80f91632013-05-22 14:41:38 +0200385 pixman_box32_t *rects;
386 xcb_rectangle_t *output_rects;
Alexander Larsson80f91632013-05-22 14:41:38 +0200387 xcb_void_cookie_t cookie;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500388 int nrects, i;
Alexander Larsson80f91632013-05-22 14:41:38 +0200389 xcb_generic_error_t *err;
390
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500391 pixman_region32_init(&transformed_region);
392 pixman_region32_copy(&transformed_region, region);
393 pixman_region32_translate(&transformed_region,
394 -output_base->x, -output_base->y);
395 weston_transformed_region(output_base->width, output_base->height,
396 output_base->transform,
397 output_base->current_scale,
398 &transformed_region, &transformed_region);
399
400 rects = pixman_region32_rectangles(&transformed_region, &nrects);
Alexander Larsson80f91632013-05-22 14:41:38 +0200401 output_rects = calloc(nrects, sizeof(xcb_rectangle_t));
402
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500403 if (output_rects == NULL) {
404 pixman_region32_fini(&transformed_region);
Alexander Larsson80f91632013-05-22 14:41:38 +0200405 return;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500406 }
Alexander Larsson80f91632013-05-22 14:41:38 +0200407
408 for (i = 0; i < nrects; i++) {
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500409 output_rects[i].x = rects[i].x1;
410 output_rects[i].y = rects[i].y1;
411 output_rects[i].width = rects[i].x2 - rects[i].x1;
412 output_rects[i].height = rects[i].y2 - rects[i].y1;
Alexander Larsson80f91632013-05-22 14:41:38 +0200413 }
414
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500415 pixman_region32_fini(&transformed_region);
416
Giulio Camuffo954f1832014-10-11 18:27:30 +0300417 cookie = xcb_set_clip_rectangles_checked(b->conn, XCB_CLIP_ORDERING_UNSORTED,
Alexander Larsson80f91632013-05-22 14:41:38 +0200418 output->gc,
419 0, 0, nrects,
420 output_rects);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300421 err = xcb_request_check(b->conn, cookie);
Alexander Larsson80f91632013-05-22 14:41:38 +0200422 if (err != NULL) {
423 weston_log("Failed to set clip rects, err: %d\n", err->error_code);
424 free(err);
425 }
426 free(output_rects);
427}
428
429
David Herrmann1edf44c2013-10-22 17:11:26 +0200430static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300431x11_output_repaint_shm(struct weston_output *output_base,
432 pixman_region32_t *damage)
433{
434 struct x11_output *output = (struct x11_output *)output_base;
435 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300436 struct x11_backend *b = (struct x11_backend *)ec->backend;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300437 xcb_void_cookie_t cookie;
438 xcb_generic_error_t *err;
439
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200440 pixman_renderer_output_set_buffer(output_base, output->hw_surface);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300441 ec->renderer->repaint_output(output_base, damage);
442
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300443 pixman_region32_subtract(&ec->primary_plane.damage,
444 &ec->primary_plane.damage, damage);
Alexander Larsson80f91632013-05-22 14:41:38 +0200445 set_clip_for_output(output_base, damage);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300446 cookie = xcb_shm_put_image_checked(b->conn, output->window, output->gc,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300447 pixman_image_get_width(output->hw_surface),
448 pixman_image_get_height(output->hw_surface),
449 0, 0,
450 pixman_image_get_width(output->hw_surface),
451 pixman_image_get_height(output->hw_surface),
452 0, 0, output->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
453 0, output->segment, 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300454 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300455 if (err != NULL) {
456 weston_log("Failed to put shm image, err: %d\n", err->error_code);
457 free(err);
458 }
459
460 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200461 return 0;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300462}
463
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100464static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400465finish_frame_handler(void *data)
466{
467 struct x11_output *output = data;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200468 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200469
Pekka Paalanen662f3842015-03-18 12:17:26 +0200470 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200471 weston_output_finish_frame(&output->base, &ts, 0);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400472
473 return 1;
474}
475
Matt Roper361d2ad2011-08-29 13:52:23 -0700476static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300477x11_output_deinit_shm(struct x11_backend *b, struct x11_output *output)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300478{
479 xcb_void_cookie_t cookie;
480 xcb_generic_error_t *err;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300481 xcb_free_gc(b->conn, output->gc);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300482
483 pixman_image_unref(output->hw_surface);
484 output->hw_surface = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300485 cookie = xcb_shm_detach_checked(b->conn, output->segment);
486 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300487 if (err) {
488 weston_log("xcb_shm_detach failed, error %d\n", err->error_code);
489 free(err);
490 }
491 shmdt(output->buf);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300492}
493
494static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500495x11_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700496{
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200497 struct x11_output *output = (struct x11_output *)output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300498 struct x11_backend *backend =
499 (struct x11_backend *)output->base.compositor->backend;
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200500
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200501 wl_event_source_remove(output->finish_frame_timer);
502
Giulio Camuffo954f1832014-10-11 18:27:30 +0300503 if (backend->use_pixman) {
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300504 pixman_renderer_output_destroy(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300505 x11_output_deinit_shm(backend, output);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300506 } else
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300507 gl_renderer->output_destroy(output_base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200508
Giulio Camuffo954f1832014-10-11 18:27:30 +0300509 xcb_destroy_window(backend->conn, output->window);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200510
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500511 weston_output_destroy(&output->base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200512
513 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700514}
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200515
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400516static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300517x11_output_set_wm_protocols(struct x11_backend *b,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500518 struct x11_output *output)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400519{
520 xcb_atom_t list[1];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400521
Giulio Camuffo954f1832014-10-11 18:27:30 +0300522 list[0] = b->atom.wm_delete_window;
523 xcb_change_property (b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400524 XCB_PROP_MODE_REPLACE,
525 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300526 b->atom.wm_protocols,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400527 XCB_ATOM_ATOM,
528 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500529 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400530 list);
531}
532
533struct wm_normal_hints {
534 uint32_t flags;
535 uint32_t pad[4];
536 int32_t min_width, min_height;
537 int32_t max_width, max_height;
538 int32_t width_inc, height_inc;
539 int32_t min_aspect_x, min_aspect_y;
540 int32_t max_aspect_x, max_aspect_y;
541 int32_t base_width, base_height;
542 int32_t win_gravity;
543};
544
545#define WM_NORMAL_HINTS_MIN_SIZE 16
546#define WM_NORMAL_HINTS_MAX_SIZE 32
547
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500548static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300549x11_output_set_icon(struct x11_backend *b,
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400550 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500551{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400552 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400553 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400554 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500555
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400556 image = load_image(filename);
557 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500558 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400559 width = pixman_image_get_width(image);
560 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500561 icon = malloc(width * height * 4 + 8);
562 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400563 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500564 return;
565 }
566
567 icon[0] = width;
568 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400569 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300570 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
571 b->atom.net_wm_icon, b->atom.cardinal, 32,
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500572 width * height + 2, icon);
573 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400574 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500575}
576
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500577static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300578x11_output_wait_for_map(struct x11_backend *b, struct x11_output *output)
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500579{
580 xcb_map_notify_event_t *map_notify;
581 xcb_configure_notify_event_t *configure_notify;
582 xcb_generic_event_t *event;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500583 int mapped = 0, configured = 0;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500584 uint8_t response_type;
585
586 /* This isn't the nicest way to do this. Ideally, we could
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500587 * just go back to the main loop and once we get the configure
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500588 * notify, we add the output to the compositor. While we do
589 * support output hotplug, we can't start up with no outputs.
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500590 * We could add the output and then resize once we get the
591 * configure notify, but we don't want to start up and
592 * immediately resize the output.
593 *
594 * Also, some window managers don't give us our final
595 * fullscreen size before map_notify, so if we don't get a
596 * configure_notify before map_notify, we just wait for the
597 * first one and hope that's our size. */
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500598
Giulio Camuffo954f1832014-10-11 18:27:30 +0300599 xcb_flush(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500600
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500601 while (!mapped || !configured) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300602 event = xcb_wait_for_event(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500603 response_type = event->response_type & ~0x80;
604
605 switch (response_type) {
606 case XCB_MAP_NOTIFY:
607 map_notify = (xcb_map_notify_event_t *) event;
608 if (map_notify->window == output->window)
609 mapped = 1;
610 break;
611
612 case XCB_CONFIGURE_NOTIFY:
613 configure_notify =
614 (xcb_configure_notify_event_t *) event;
615
Alexander Larsson4ea95522013-05-22 14:41:37 +0200616
617 if (configure_notify->width % output->scale != 0 ||
618 configure_notify->height % output->scale != 0)
619 weston_log("Resolution is not a multiple of screen size, rounding\n");
Jason Ekstrandd89a0942014-03-07 15:29:14 -0600620 output->mode.width = configure_notify->width;
621 output->mode.height = configure_notify->height;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500622 configured = 1;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500623 break;
624 }
625 }
626}
627
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300628static xcb_visualtype_t *
629find_visual_by_id(xcb_screen_t *screen,
630 xcb_visualid_t id)
631{
632 xcb_depth_iterator_t i;
633 xcb_visualtype_iterator_t j;
634 for (i = xcb_screen_allowed_depths_iterator(screen);
635 i.rem;
636 xcb_depth_next(&i)) {
637 for (j = xcb_depth_visuals_iterator(i.data);
638 j.rem;
639 xcb_visualtype_next(&j)) {
640 if (j.data->visual_id == id)
641 return j.data;
642 }
643 }
644 return 0;
645}
646
647static uint8_t
648get_depth_of_visual(xcb_screen_t *screen,
649 xcb_visualid_t id)
650{
651 xcb_depth_iterator_t i;
652 xcb_visualtype_iterator_t j;
653 for (i = xcb_screen_allowed_depths_iterator(screen);
654 i.rem;
655 xcb_depth_next(&i)) {
656 for (j = xcb_depth_visuals_iterator(i.data);
657 j.rem;
658 xcb_visualtype_next(&j)) {
659 if (j.data->visual_id == id)
660 return i.data->depth;
661 }
662 }
663 return 0;
664}
665
666static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300667x11_output_init_shm(struct x11_backend *b, struct x11_output *output,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300668 int width, int height)
669{
670 xcb_screen_iterator_t iter;
671 xcb_visualtype_t *visual_type;
672 xcb_format_iterator_t fmt;
673 xcb_void_cookie_t cookie;
674 xcb_generic_error_t *err;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300675 const xcb_query_extension_reply_t *ext;
676 int bitsperpixel = 0;
677 pixman_format_code_t pixman_format;
678
679 /* Check if SHM is available */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300680 ext = xcb_get_extension_data(b->conn, &xcb_shm_id);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300681 if (ext == NULL || !ext->present) {
682 /* SHM is missing */
683 weston_log("SHM extension is not available\n");
684 errno = ENOENT;
685 return -1;
686 }
687
Giulio Camuffo954f1832014-10-11 18:27:30 +0300688 iter = xcb_setup_roots_iterator(xcb_get_setup(b->conn));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300689 visual_type = find_visual_by_id(iter.data, iter.data->root_visual);
690 if (!visual_type) {
691 weston_log("Failed to lookup visual for root window\n");
692 errno = ENOENT;
693 return -1;
694 }
695 weston_log("Found visual, bits per value: %d, red_mask: %.8x, green_mask: %.8x, blue_mask: %.8x\n",
696 visual_type->bits_per_rgb_value,
697 visual_type->red_mask,
698 visual_type->green_mask,
699 visual_type->blue_mask);
700 output->depth = get_depth_of_visual(iter.data, iter.data->root_visual);
701 weston_log("Visual depth is %d\n", output->depth);
702
Giulio Camuffo954f1832014-10-11 18:27:30 +0300703 for (fmt = xcb_setup_pixmap_formats_iterator(xcb_get_setup(b->conn));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300704 fmt.rem;
705 xcb_format_next(&fmt)) {
706 if (fmt.data->depth == output->depth) {
707 bitsperpixel = fmt.data->bits_per_pixel;
708 break;
709 }
710 }
711 weston_log("Found format for depth %d, bpp: %d\n",
712 output->depth, bitsperpixel);
713
714 if (bitsperpixel == 32 &&
715 visual_type->red_mask == 0xff0000 &&
716 visual_type->green_mask == 0x00ff00 &&
717 visual_type->blue_mask == 0x0000ff) {
718 weston_log("Will use x8r8g8b8 format for SHM surfaces\n");
719 pixman_format = PIXMAN_x8r8g8b8;
MoD1b55a592013-11-28 05:24:21 +0000720 } else if (bitsperpixel == 16 &&
721 visual_type->red_mask == 0x00f800 &&
722 visual_type->green_mask == 0x0007e0 &&
723 visual_type->blue_mask == 0x00001f) {
724 weston_log("Will use r5g6b5 format for SHM surfaces\n");
725 pixman_format = PIXMAN_r5g6b5;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300726 } else {
727 weston_log("Can't find appropriate format for SHM pixmap\n");
728 errno = ENOTSUP;
729 return -1;
730 }
731
732
733 /* Create SHM segment and attach it */
734 output->shm_id = shmget(IPC_PRIVATE, width * height * (bitsperpixel / 8), IPC_CREAT | S_IRWXU);
735 if (output->shm_id == -1) {
736 weston_log("x11shm: failed to allocate SHM segment\n");
737 return -1;
738 }
739 output->buf = shmat(output->shm_id, NULL, 0 /* read/write */);
740 if (-1 == (long)output->buf) {
741 weston_log("x11shm: failed to attach SHM segment\n");
742 return -1;
743 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300744 output->segment = xcb_generate_id(b->conn);
745 cookie = xcb_shm_attach_checked(b->conn, output->segment, output->shm_id, 1);
746 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300747 if (err) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700748 weston_log("x11shm: xcb_shm_attach error %d, op code %d, resource id %d\n",
749 err->error_code, err->major_code, err->minor_code);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300750 free(err);
751 return -1;
752 }
753
754 shmctl(output->shm_id, IPC_RMID, NULL);
755
756 /* Now create pixman image */
757 output->hw_surface = pixman_image_create_bits(pixman_format, width, height, output->buf,
758 width * (bitsperpixel / 8));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300759
Giulio Camuffo954f1832014-10-11 18:27:30 +0300760 output->gc = xcb_generate_id(b->conn);
761 xcb_create_gc(b->conn, output->gc, output->window, 0, NULL);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300762
763 return 0;
764}
765
Scott Moreau1bad5db2012-08-18 01:04:05 -0600766static struct x11_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300767x11_backend_create_output(struct x11_backend *b, int x, int y,
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300768 int width, int height, int fullscreen,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600769 int no_input, char *configured_name,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200770 uint32_t transform, int32_t scale)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400771{
Scott Moreau1bad5db2012-08-18 01:04:05 -0600772 static const char name[] = "Weston Compositor";
Kristian Høgsberg86000402012-01-03 23:24:14 -0500773 static const char class[] = "weston-1\0Weston Compositor";
Scott Moreau1bad5db2012-08-18 01:04:05 -0600774 char title[32];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400775 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400776 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400777 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400778 struct wl_event_loop *loop;
Kristian Høgsberg8404acb2014-03-18 12:15:42 -0700779 int output_width, output_height, width_mm, height_mm;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300780 int ret;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400781 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500782 xcb_atom_t atom_list[1];
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300783 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400784 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300785 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400786 0
787 };
788
Alexander Larsson4ea95522013-05-22 14:41:37 +0200789 output_width = width * scale;
790 output_height = height * scale;
791
Scott Moreau1bad5db2012-08-18 01:04:05 -0600792 if (configured_name)
793 sprintf(title, "%s - %s", name, configured_name);
794 else
795 strcpy(title, name);
796
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300797 if (!no_input)
798 values[0] |=
799 XCB_EVENT_MASK_KEY_PRESS |
800 XCB_EVENT_MASK_KEY_RELEASE |
801 XCB_EVENT_MASK_BUTTON_PRESS |
802 XCB_EVENT_MASK_BUTTON_RELEASE |
803 XCB_EVENT_MASK_POINTER_MOTION |
804 XCB_EVENT_MASK_ENTER_WINDOW |
805 XCB_EVENT_MASK_LEAVE_WINDOW |
806 XCB_EVENT_MASK_KEYMAP_STATE |
807 XCB_EVENT_MASK_FOCUS_CHANGE;
808
Peter Huttererf3d62272013-08-08 11:57:05 +1000809 output = zalloc(sizeof *output);
Bryce Harrington665b0252015-05-26 18:14:21 -0700810 if (output == NULL) {
811 perror("zalloc");
Scott Moreau1bad5db2012-08-18 01:04:05 -0600812 return NULL;
Bryce Harrington665b0252015-05-26 18:14:21 -0700813 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400814
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400815 output->mode.flags =
816 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200817
Alexander Larsson0b135062013-05-28 16:23:36 +0200818 output->mode.width = output_width;
819 output->mode.height = output_height;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400820 output->mode.refresh = 60000;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200821 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400822 wl_list_init(&output->base.mode_list);
823 wl_list_insert(&output->base.mode_list, &output->mode.link);
824
Giulio Camuffo954f1832014-10-11 18:27:30 +0300825 values[1] = b->null_cursor;
826 output->window = xcb_generate_id(b->conn);
827 iter = xcb_setup_roots_iterator(xcb_get_setup(b->conn));
828 xcb_create_window(b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400829 XCB_COPY_FROM_PARENT,
830 output->window,
831 iter.data->root,
832 0, 0,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200833 output_width, output_height,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400834 0,
835 XCB_WINDOW_CLASS_INPUT_OUTPUT,
836 iter.data->root_visual,
837 mask, values);
838
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500839 if (fullscreen) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300840 atom_list[0] = b->atom.net_wm_state_fullscreen;
841 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500842 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300843 b->atom.net_wm_state,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500844 XCB_ATOM_ATOM, 32,
845 ARRAY_LENGTH(atom_list), atom_list);
846 } else {
847 /* Don't resize me. */
848 memset(&normal_hints, 0, sizeof normal_hints);
849 normal_hints.flags =
850 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200851 normal_hints.min_width = output_width;
852 normal_hints.min_height = output_height;
853 normal_hints.max_width = output_width;
854 normal_hints.max_height = output_height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300855 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
856 b->atom.wm_normal_hints,
857 b->atom.wm_size_hints, 32,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500858 sizeof normal_hints / 4,
859 (uint8_t *) &normal_hints);
860 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400861
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400862 /* Set window name. Don't bother with non-EWMH WMs. */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300863 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
864 b->atom.net_wm_name, b->atom.utf8_string, 8,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600865 strlen(title), title);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300866 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
867 b->atom.wm_class, b->atom.string, 8,
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500868 sizeof class, class);
869
Giulio Camuffo954f1832014-10-11 18:27:30 +0300870 x11_output_set_icon(b, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500871
Giulio Camuffo954f1832014-10-11 18:27:30 +0300872 x11_output_set_wm_protocols(b, output);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500873
Giulio Camuffo954f1832014-10-11 18:27:30 +0300874 xcb_map_window(b->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400875
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -0500876 if (fullscreen)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300877 x11_output_wait_for_map(b, output);
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400878
Jonas Ådahle5a12252013-04-05 23:07:11 +0200879 output->base.start_repaint_loop = x11_output_start_repaint_loop;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300880 if (b->use_pixman)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300881 output->base.repaint = x11_output_repaint_shm;
882 else
883 output->base.repaint = x11_output_repaint_gl;
Matt Roper361d2ad2011-08-29 13:52:23 -0700884 output->base.destroy = x11_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800885 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200886 output->base.set_backlight = NULL;
887 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800888 output->base.switch_mode = NULL;
Hardeningff39efa2013-09-18 23:56:35 +0200889 output->base.current_mode = &output->mode;
Jason Ekstrand22e16b62014-09-15 12:10:27 -0700890 output->base.make = "weston-X11";
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500891 output->base.model = "none";
Ander Conselvan de Oliveirad4ea71e2013-12-18 19:51:40 +0200892
893 if (configured_name)
894 output->base.name = strdup(configured_name);
895
Giulio Camuffo954f1832014-10-11 18:27:30 +0300896 width_mm = width * b->screen->width_in_millimeters /
897 b->screen->width_in_pixels;
898 height_mm = height * b->screen->height_in_millimeters /
899 b->screen->height_in_pixels;
900 weston_output_init(&output->base, b->compositor,
Kristian Høgsberg8404acb2014-03-18 12:15:42 -0700901 x, y, width_mm, height_mm, transform, scale);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100902
Giulio Camuffo954f1832014-10-11 18:27:30 +0300903 if (b->use_pixman) {
904 if (x11_output_init_shm(b, output,
Kristian Høgsbergb5a4f982013-10-23 23:38:50 -0700905 output->mode.width,
Bryce Harrington665b0252015-05-26 18:14:21 -0700906 output->mode.height) < 0) {
907 weston_log("Failed to initialize SHM for the X11 output\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300908 return NULL;
Bryce Harrington665b0252015-05-26 18:14:21 -0700909 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300910 if (pixman_renderer_output_create(&output->base) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700911 weston_log("Failed to create pixman renderer for output\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300912 x11_output_deinit_shm(b, output);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300913 return NULL;
914 }
915 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +0100916 /* eglCreatePlatformWindowSurfaceEXT takes a Window*
917 * but eglCreateWindowSurface takes a Window. */
918 Window xid = (Window) output->window;
919
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300920 ret = gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000921 (EGLNativeWindowType) output->window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100922 &xid,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000923 gl_renderer->opaque_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500924 NULL,
925 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300926 if (ret < 0)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300927 return NULL;
928 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100929
Giulio Camuffo954f1832014-10-11 18:27:30 +0300930 loop = wl_display_get_event_loop(b->compositor->wl_display);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100931 output->finish_frame_timer =
932 wl_event_loop_add_timer(loop, finish_frame_handler, output);
933
Giulio Camuffo954f1832014-10-11 18:27:30 +0300934 weston_compositor_add_output(b->compositor, &output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400935
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400936 weston_log("x11 output %dx%d, window id %d\n",
937 width, height, output->window);
938
Scott Moreau1bad5db2012-08-18 01:04:05 -0600939 return output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400940}
941
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400942static struct x11_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300943x11_backend_find_output(struct x11_backend *b, xcb_window_t window)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400944{
945 struct x11_output *output;
946
Giulio Camuffo954f1832014-10-11 18:27:30 +0300947 wl_list_for_each(output, &b->compositor->output_list, base.link) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400948 if (output->window == window)
949 return output;
950 }
951
Derek Foreman99bfa642014-12-11 15:44:46 -0600952 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400953}
954
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200955static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300956x11_backend_delete_window(struct x11_backend *b, xcb_window_t window)
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200957{
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +0200958 struct x11_output *output;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200959
Giulio Camuffo954f1832014-10-11 18:27:30 +0300960 output = x11_backend_find_output(b, window);
Derek Foreman99bfa642014-12-11 15:44:46 -0600961 if (output)
962 x11_output_destroy(&output->base);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200963
Giulio Camuffo954f1832014-10-11 18:27:30 +0300964 xcb_flush(b->conn);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200965
Giulio Camuffo954f1832014-10-11 18:27:30 +0300966 if (wl_list_empty(&b->compositor->output_list))
Giulio Camuffo459137b2014-10-11 23:56:24 +0300967 weston_compositor_exit(b->compositor);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200968}
969
Derek Foremand540f4b2015-01-27 16:26:49 -0600970static void delete_cb(void *data)
971{
972 struct window_delete_data *wd = data;
973
Giulio Camuffo954f1832014-10-11 18:27:30 +0300974 x11_backend_delete_window(wd->backend, wd->window);
Derek Foremand540f4b2015-01-27 16:26:49 -0600975 free(wd);
976}
977
Daniel Stone154c34c2012-06-22 13:21:40 +0100978#ifdef HAVE_XCB_XKB
Daniel Stonee2faa122012-06-22 13:21:39 +0100979static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300980update_xkb_state(struct x11_backend *b, xcb_xkb_state_notify_event_t *state)
Daniel Stonee2faa122012-06-22 13:21:39 +0100981{
Derek Foreman1281a362015-07-31 16:55:32 -0500982 struct weston_keyboard *keyboard =
983 weston_seat_get_keyboard(&b->core_seat);
984
985 xkb_state_update_mask(keyboard->xkb_state.state,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300986 get_xkb_mod_mask(b, state->baseMods),
987 get_xkb_mod_mask(b, state->latchedMods),
988 get_xkb_mod_mask(b, state->lockedMods),
Daniel Stonee2faa122012-06-22 13:21:39 +0100989 0,
990 0,
991 state->group);
992
Giulio Camuffo954f1832014-10-11 18:27:30 +0300993 notify_modifiers(&b->core_seat,
994 wl_display_next_serial(b->compositor->wl_display));
Daniel Stonee2faa122012-06-22 13:21:39 +0100995}
996#endif
997
Daniel Stone154c34c2012-06-22 13:21:40 +0100998/**
999 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
1000 * the best we can do (given that XCB also lacks XI2 support), is to take
1001 * the state from the core key events. Unfortunately that only gives us
1002 * the effective (i.e. union of depressed/latched/locked) state, and we
1003 * need the granularity.
1004 *
1005 * So we still update the state with every key event we see, but also use
1006 * the state field from X11 events as a mask so we don't get any stuck
1007 * modifiers.
1008 */
1009static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001010update_xkb_state_from_core(struct x11_backend *b, uint16_t x11_mask)
Daniel Stone154c34c2012-06-22 13:21:40 +01001011{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001012 uint32_t mask = get_xkb_mod_mask(b, x11_mask);
Derek Foreman1281a362015-07-31 16:55:32 -05001013 struct weston_keyboard *keyboard
1014 = weston_seat_get_keyboard(&b->core_seat);
Daniel Stone154c34c2012-06-22 13:21:40 +01001015
Derek Foreman1281a362015-07-31 16:55:32 -05001016 xkb_state_update_mask(keyboard->xkb_state.state,
Daniel Stone154c34c2012-06-22 13:21:40 +01001017 keyboard->modifiers.mods_depressed & mask,
1018 keyboard->modifiers.mods_latched & mask,
1019 keyboard->modifiers.mods_locked & mask,
1020 0,
1021 0,
1022 (x11_mask >> 13) & 3);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001023 notify_modifiers(&b->core_seat,
1024 wl_display_next_serial(b->compositor->wl_display));
Daniel Stone154c34c2012-06-22 13:21:40 +01001025}
1026
Tiago Vignattia3a71622011-12-08 17:03:17 +02001027static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001028x11_backend_deliver_button_event(struct x11_backend *b,
1029 xcb_generic_event_t *event, int state)
Tiago Vignattia3a71622011-12-08 17:03:17 +02001030{
1031 xcb_button_press_event_t *button_event =
1032 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +01001033 uint32_t button;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001034 struct x11_output *output;
1035
Giulio Camuffo954f1832014-10-11 18:27:30 +03001036 output = x11_backend_find_output(b, button_event->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001037 if (!output)
1038 return;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001039
1040 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001041 xcb_grab_pointer(b->conn, 0, output->window,
Kristian Høgsberg73308622012-10-30 11:04:52 -04001042 XCB_EVENT_MASK_BUTTON_PRESS |
1043 XCB_EVENT_MASK_BUTTON_RELEASE |
1044 XCB_EVENT_MASK_POINTER_MOTION |
1045 XCB_EVENT_MASK_ENTER_WINDOW |
1046 XCB_EVENT_MASK_LEAVE_WINDOW,
1047 XCB_GRAB_MODE_ASYNC,
1048 XCB_GRAB_MODE_ASYNC,
1049 output->window, XCB_CURSOR_NONE,
1050 button_event->time);
1051 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001052 xcb_ungrab_pointer(b->conn, button_event->time);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001053
Giulio Camuffo954f1832014-10-11 18:27:30 +03001054 if (!b->has_xkb)
1055 update_xkb_state_from_core(b, button_event->state);
Daniel Stone154c34c2012-06-22 13:21:40 +01001056
Tiago Vignattia3a71622011-12-08 17:03:17 +02001057 switch (button_event->detail) {
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001058 case 1:
1059 button = BTN_LEFT;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001060 break;
1061 case 2:
1062 button = BTN_MIDDLE;
1063 break;
1064 case 3:
1065 button = BTN_RIGHT;
1066 break;
1067 case 4:
Jonas Ådahl62efe202012-09-27 18:40:41 +02001068 /* Axis are measured in pixels, but the xcb events are discrete
1069 * steps. Therefore move the axis by some pixels every step. */
Scott Moreau210d0792012-03-22 10:47:01 -06001070 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001071 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001072 weston_compositor_get_time(),
1073 WL_POINTER_AXIS_VERTICAL_SCROLL,
1074 -DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -06001075 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001076 case 5:
Scott Moreau210d0792012-03-22 10:47:01 -06001077 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001078 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001079 weston_compositor_get_time(),
1080 WL_POINTER_AXIS_VERTICAL_SCROLL,
1081 DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -06001082 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001083 case 6:
Scott Moreau210d0792012-03-22 10:47:01 -06001084 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001085 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001086 weston_compositor_get_time(),
1087 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
1088 -DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -06001089 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001090 case 7:
Scott Moreau210d0792012-03-22 10:47:01 -06001091 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001092 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001093 weston_compositor_get_time(),
1094 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
1095 DEFAULT_AXIS_STEP_DISTANCE);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001096 return;
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001097 default:
1098 button = button_event->detail + BTN_SIDE - 8;
1099 break;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001100 }
1101
Giulio Camuffo954f1832014-10-11 18:27:30 +03001102 notify_button(&b->core_seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001103 weston_compositor_get_time(), button,
1104 state ? WL_POINTER_BUTTON_STATE_PRESSED :
1105 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001106}
1107
Scott Moreau1bad5db2012-08-18 01:04:05 -06001108static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001109x11_backend_deliver_motion_event(struct x11_backend *b,
1110 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001111{
1112 struct x11_output *output;
1113 wl_fixed_t x, y;
1114 xcb_motion_notify_event_t *motion_notify =
1115 (xcb_motion_notify_event_t *) event;
1116
Giulio Camuffo954f1832014-10-11 18:27:30 +03001117 if (!b->has_xkb)
1118 update_xkb_state_from_core(b, motion_notify->state);
1119 output = x11_backend_find_output(b, motion_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001120 if (!output)
1121 return;
1122
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001123 weston_output_transform_coordinate(&output->base,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05001124 wl_fixed_from_int(motion_notify->event_x),
1125 wl_fixed_from_int(motion_notify->event_y),
1126 &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001127
Giulio Camuffo954f1832014-10-11 18:27:30 +03001128 notify_motion(&b->core_seat, weston_compositor_get_time(),
1129 x - b->prev_x, y - b->prev_y);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001130
Giulio Camuffo954f1832014-10-11 18:27:30 +03001131 b->prev_x = x;
1132 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001133}
1134
1135static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001136x11_backend_deliver_enter_event(struct x11_backend *b,
1137 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001138{
1139 struct x11_output *output;
1140 wl_fixed_t x, y;
1141
1142 xcb_enter_notify_event_t *enter_notify =
1143 (xcb_enter_notify_event_t *) event;
1144 if (enter_notify->state >= Button1Mask)
1145 return;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001146 if (!b->has_xkb)
1147 update_xkb_state_from_core(b, enter_notify->state);
1148 output = x11_backend_find_output(b, enter_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001149 if (!output)
1150 return;
1151
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001152 weston_output_transform_coordinate(&output->base,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05001153 wl_fixed_from_int(enter_notify->event_x),
1154 wl_fixed_from_int(enter_notify->event_y), &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001155
Giulio Camuffo954f1832014-10-11 18:27:30 +03001156 notify_pointer_focus(&b->core_seat, &output->base, x, y);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001157
Giulio Camuffo954f1832014-10-11 18:27:30 +03001158 b->prev_x = x;
1159 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001160}
1161
Kristian Høgsbergee724822011-04-21 14:51:44 -04001162static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001163x11_backend_next_event(struct x11_backend *b,
1164 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -04001165{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001166 if (mask & WL_EVENT_READABLE) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001167 *event = xcb_poll_for_event(b->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -04001168 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001169#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Giulio Camuffo954f1832014-10-11 18:27:30 +03001170 *event = xcb_poll_for_queued_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001171#else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001172 *event = xcb_poll_for_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001173#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -04001174 }
1175
1176 return *event != NULL;
1177}
1178
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001179static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001180x11_backend_handle_event(int fd, uint32_t mask, void *data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001181{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001182 struct x11_backend *b = data;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001183 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001184 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001185 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001186 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001187 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001188 xcb_keymap_notify_event_t *keymap_notify;
1189 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001190 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001191 xcb_atom_t atom;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001192 xcb_window_t window;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001193 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001194 uint32_t i, set;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001195 uint8_t response_type;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001196 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001197
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001198 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001199 count = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001200 while (x11_backend_next_event(b, &event, mask)) {
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001201 response_type = event->response_type & ~0x80;
1202
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001203 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1204 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001205 /* Suppress key repeat events; this is only used if we
1206 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001207 key_release = (xcb_key_press_event_t *) prev;
1208 key_press = (xcb_key_press_event_t *) event;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001209 if (response_type == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -07001210 key_release->time == key_press->time &&
1211 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001212 /* Don't deliver the held key release
1213 * event or the new key press event. */
1214 free(event);
1215 free(prev);
1216 prev = NULL;
1217 continue;
1218 } else {
1219 /* Deliver the held key release now
1220 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001221 * event below. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001222 update_xkb_state_from_core(b, key_release->state);
1223 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001224 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001225 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001226 WL_KEYBOARD_KEY_STATE_RELEASED,
1227 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001228 free(prev);
1229 prev = NULL;
1230 break;
1231 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001232
1233 case XCB_FOCUS_IN:
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001234 assert(response_type == XCB_KEYMAP_NOTIFY);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001235 keymap_notify = (xcb_keymap_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001236 b->keys.size = 0;
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001237 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
1238 set = keymap_notify->keys[i >> 3] &
1239 (1 << (i & 7));
1240 if (set) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001241 k = wl_array_add(&b->keys, sizeof *k);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001242 *k = i;
1243 }
1244 }
1245
Daniel Stoned6da09e2012-06-22 13:21:29 +01001246 /* Unfortunately the state only comes with the enter
1247 * event, rather than with the focus event. I'm not
1248 * sure of the exact semantics around it and whether
1249 * we can ensure that we get both? */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001250 notify_keyboard_focus_in(&b->core_seat, &b->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001251 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001252
1253 free(prev);
1254 prev = NULL;
1255 break;
1256
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001257 default:
1258 /* No previous event held */
1259 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001260 }
1261
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001262 switch (response_type) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001263 case XCB_KEY_PRESS:
1264 key_press = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001265 if (!b->has_xkb)
1266 update_xkb_state_from_core(b, key_press->state);
1267 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001268 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001269 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001270 WL_KEYBOARD_KEY_STATE_PRESSED,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001271 b->has_xkb ? STATE_UPDATE_NONE :
Daniel Stonee2faa122012-06-22 13:21:39 +01001272 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001273 break;
1274 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001275 /* If we don't have XKB, we need to use the lame
1276 * autorepeat detection above. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001277 if (!b->has_xkb) {
Daniel Stone3ee91e12012-06-22 13:21:36 +01001278 prev = event;
1279 break;
1280 }
1281 key_release = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001282 notify_key(&b->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +01001283 weston_compositor_get_time(),
1284 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001285 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +01001286 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001287 break;
1288 case XCB_BUTTON_PRESS:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001289 x11_backend_deliver_button_event(b, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001290 break;
1291 case XCB_BUTTON_RELEASE:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001292 x11_backend_deliver_button_event(b, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001293 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001294 case XCB_MOTION_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001295 x11_backend_deliver_motion_event(b, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001296 break;
1297
1298 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001299 expose = (xcb_expose_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001300 output = x11_backend_find_output(b, expose->window);
Derek Foreman99bfa642014-12-11 15:44:46 -06001301 if (!output)
1302 break;
1303
Kristian Høgsberga98c2e12013-12-05 12:30:37 -08001304 weston_output_damage(&output->base);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001305 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001306 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001307
1308 case XCB_ENTER_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001309 x11_backend_deliver_enter_event(b, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001310 break;
1311
1312 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001313 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -05001314 if (enter_notify->state >= Button1Mask)
1315 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001316 if (!b->has_xkb)
1317 update_xkb_state_from_core(b, enter_notify->state);
1318 notify_pointer_focus(&b->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001319 break;
1320
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001321 case XCB_CLIENT_MESSAGE:
1322 client_message = (xcb_client_message_event_t *) event;
1323 atom = client_message->data.data32[0];
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001324 window = client_message->window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001325 if (atom == b->atom.wm_delete_window) {
Derek Foremand540f4b2015-01-27 16:26:49 -06001326 struct wl_event_loop *loop;
1327 struct window_delete_data *data = malloc(sizeof *data);
1328
1329 /* if malloc failed we should at least try to
1330 * delete the window, even if it may result in
1331 * a crash.
1332 */
1333 if (!data) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001334 x11_backend_delete_window(b, window);
Derek Foremand540f4b2015-01-27 16:26:49 -06001335 break;
1336 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001337 data->backend = b;
Derek Foremand540f4b2015-01-27 16:26:49 -06001338 data->window = window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001339 loop = wl_display_get_event_loop(b->compositor->wl_display);
Derek Foremand540f4b2015-01-27 16:26:49 -06001340 wl_event_loop_add_idle(loop, delete_cb, data);
1341 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001342 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001343
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001344 case XCB_FOCUS_IN:
1345 focus_in = (xcb_focus_in_event_t *) event;
1346 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1347 break;
1348
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001349 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001350 break;
1351
1352 case XCB_FOCUS_OUT:
1353 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -05001354 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1355 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001356 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001357 notify_keyboard_focus_out(&b->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001358 break;
1359
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001360 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001361 break;
1362 }
1363
Daniel Stonee2faa122012-06-22 13:21:39 +01001364#ifdef HAVE_XCB_XKB
Giulio Camuffo954f1832014-10-11 18:27:30 +03001365 if (b->has_xkb) {
1366 if (response_type == b->xkb_event_base) {
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001367 xcb_xkb_state_notify_event_t *state =
1368 (xcb_xkb_state_notify_event_t *) event;
1369 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001370 update_xkb_state(b, state);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001371 } else if (response_type == XCB_PROPERTY_NOTIFY) {
1372 xcb_property_notify_event_t *prop_notify =
1373 (xcb_property_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001374 if (prop_notify->window == b->screen->root &&
1375 prop_notify->atom == b->atom.xkb_names &&
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001376 prop_notify->state == XCB_PROPERTY_NEW_VALUE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001377 update_xkb_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001378 }
Daniel Stonee2faa122012-06-22 13:21:39 +01001379 }
1380#endif
1381
Bill Spitzakb715cec2012-06-05 17:08:23 -04001382 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001383 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001384 free (event);
1385 }
1386
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001387 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1388 case XCB_KEY_RELEASE:
1389 key_release = (xcb_key_press_event_t *) prev;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001390 update_xkb_state_from_core(b, key_release->state);
1391 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001392 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001393 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001394 WL_KEYBOARD_KEY_STATE_RELEASED,
1395 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001396 free(prev);
1397 prev = NULL;
1398 break;
1399 default:
1400 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001401 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001402
Bill Spitzakb715cec2012-06-05 17:08:23 -04001403 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001404}
1405
Giulio Camuffo954f1832014-10-11 18:27:30 +03001406#define F(field) offsetof(struct x11_backend, field)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001407
1408static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001409x11_backend_get_resources(struct x11_backend *b)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001410{
1411 static const struct { const char *name; int offset; } atoms[] = {
1412 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1413 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1414 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1415 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001416 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001417 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001418 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001419 { "_NET_WM_STATE", F(atom.net_wm_state) },
1420 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001421 { "_NET_SUPPORTING_WM_CHECK",
1422 F(atom.net_supporting_wm_check) },
1423 { "_NET_SUPPORTED", F(atom.net_supported) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001424 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001425 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001426 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001427 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001428 };
1429
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001430 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001431 xcb_intern_atom_reply_t *reply;
1432 xcb_pixmap_t pixmap;
1433 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001434 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001435 uint8_t data[] = { 0, 0, 0, 0 };
1436
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001437 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001438 cookies[i] = xcb_intern_atom (b->conn, 0,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001439 strlen(atoms[i].name),
1440 atoms[i].name);
1441
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001442 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001443 reply = xcb_intern_atom_reply (b->conn, cookies[i], NULL);
1444 *(xcb_atom_t *) ((char *) b + atoms[i].offset) = reply->atom;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001445 free(reply);
1446 }
1447
Giulio Camuffo954f1832014-10-11 18:27:30 +03001448 pixmap = xcb_generate_id(b->conn);
1449 gc = xcb_generate_id(b->conn);
1450 xcb_create_pixmap(b->conn, 1, pixmap, b->screen->root, 1, 1);
1451 xcb_create_gc(b->conn, gc, pixmap, 0, NULL);
1452 xcb_put_image(b->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001453 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001454 b->null_cursor = xcb_generate_id(b->conn);
1455 xcb_create_cursor (b->conn, b->null_cursor,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001456 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001457 xcb_free_gc(b->conn, gc);
1458 xcb_free_pixmap(b->conn, pixmap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001459}
1460
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001461static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001462x11_backend_get_wm_info(struct x11_backend *c)
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001463{
1464 xcb_get_property_cookie_t cookie;
1465 xcb_get_property_reply_t *reply;
1466 xcb_atom_t *atom;
1467 unsigned int i;
1468
1469 cookie = xcb_get_property(c->conn, 0, c->screen->root,
1470 c->atom.net_supported,
1471 XCB_ATOM_ATOM, 0, 1024);
1472 reply = xcb_get_property_reply(c->conn, cookie, NULL);
1473 if (reply == NULL)
1474 return;
1475
1476 atom = (xcb_atom_t *) xcb_get_property_value(reply);
1477
1478 for (i = 0; i < reply->value_len; i++) {
1479 if (atom[i] == c->atom.net_wm_state_fullscreen)
1480 c->has_net_wm_state_fullscreen = 1;
1481 }
Ryo Munakata76fb7ec2015-03-08 19:17:06 +09001482
1483 free(reply);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001484}
1485
1486static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001487x11_restore(struct weston_compositor *ec)
1488{
1489}
1490
1491static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001492x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001493{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001494 struct x11_backend *backend = (struct x11_backend *)ec->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -07001495
Giulio Camuffo954f1832014-10-11 18:27:30 +03001496 wl_event_source_remove(backend->xcb_source);
1497 x11_input_destroy(backend);
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001498
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03001499 weston_compositor_shutdown(ec); /* destroys outputs, too */
1500
Giulio Camuffo954f1832014-10-11 18:27:30 +03001501 XCloseDisplay(backend->dpy);
1502 free(backend);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001503}
1504
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001505static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001506init_gl_renderer(struct x11_backend *b)
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001507{
1508 int ret;
1509
1510 gl_renderer = weston_load_module("gl-renderer.so",
1511 "gl_renderer_interface");
1512 if (!gl_renderer)
1513 return -1;
1514
Giulio Camuffo954f1832014-10-11 18:27:30 +03001515 ret = gl_renderer->create(b->compositor, EGL_PLATFORM_X11_KHR, (void *) b->dpy,
Derek Foremane76f1852015-05-15 12:12:39 -05001516 gl_renderer->opaque_attribs, NULL, 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001517
1518 return ret;
1519}
Giulio Camuffo954f1832014-10-11 18:27:30 +03001520static struct x11_backend *
1521x11_backend_create(struct weston_compositor *compositor,
1522 int fullscreen,
1523 int no_input,
1524 int use_pixman,
1525 int *argc, char *argv[],
1526 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001527{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001528 struct x11_backend *b;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001529 struct x11_output *output;
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001530 struct weston_config_section *section;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001531 xcb_screen_iterator_t s;
Scott Moreau248aaec2012-08-03 14:19:52 -06001532 int i, x = 0, output_count = 0;
Jason Ekstrandd89a0942014-03-07 15:29:14 -06001533 int width, height, scale, count;
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001534 const char *section_name;
1535 char *name, *t, *mode;
1536 uint32_t transform;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001537
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001538 weston_log("initializing x11 backend\n");
1539
Giulio Camuffo954f1832014-10-11 18:27:30 +03001540 b = zalloc(sizeof *b);
1541 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001542 return NULL;
1543
Giulio Camuffo954f1832014-10-11 18:27:30 +03001544 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001545 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001546 goto err_free;
1547
Giulio Camuffo954f1832014-10-11 18:27:30 +03001548 b->dpy = XOpenDisplay(NULL);
1549 if (b->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001550 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001551
Giulio Camuffo954f1832014-10-11 18:27:30 +03001552 b->conn = XGetXCBConnection(b->dpy);
1553 XSetEventQueueOwner(b->dpy, XCBOwnsEventQueue);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001554
Giulio Camuffo954f1832014-10-11 18:27:30 +03001555 if (xcb_connection_has_error(b->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001556 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001557
Giulio Camuffo954f1832014-10-11 18:27:30 +03001558 s = xcb_setup_roots_iterator(xcb_get_setup(b->conn));
1559 b->screen = s.data;
1560 wl_array_init(&b->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001561
Giulio Camuffo954f1832014-10-11 18:27:30 +03001562 x11_backend_get_resources(b);
1563 x11_backend_get_wm_info(b);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001564
Giulio Camuffo954f1832014-10-11 18:27:30 +03001565 if (!b->has_net_wm_state_fullscreen && fullscreen) {
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001566 weston_log("Can not fullscreen without window manager support"
1567 "(need _NET_WM_STATE_FULLSCREEN)\n");
1568 fullscreen = 0;
1569 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001570
Giulio Camuffo954f1832014-10-11 18:27:30 +03001571 b->use_pixman = use_pixman;
1572 if (b->use_pixman) {
1573 if (pixman_renderer_init(compositor) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001574 weston_log("Failed to initialize pixman renderer for X11 backend\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001575 goto err_xdisplay;
Bryce Harrington665b0252015-05-26 18:14:21 -07001576 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001577 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001578 else if (init_gl_renderer(b) < 0) {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001579 goto err_xdisplay;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001580 }
Vasily Khoruzhickd8943512013-01-07 22:36:02 +03001581 weston_log("Using %s renderer\n", use_pixman ? "pixman" : "gl");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001582
Giulio Camuffo954f1832014-10-11 18:27:30 +03001583 b->base.destroy = x11_destroy;
1584 b->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001585
Giulio Camuffo954f1832014-10-11 18:27:30 +03001586 if (x11_input_create(b, no_input) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001587 weston_log("Failed to create X11 input\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001588 goto err_renderer;
Bryce Harrington665b0252015-05-26 18:14:21 -07001589 }
Daniel Stone22815f92012-06-22 13:21:34 +01001590
Scott Moreau248aaec2012-08-03 14:19:52 -06001591 width = option_width ? option_width : 1024;
1592 height = option_height ? option_height : 640;
Jason Ekstrandd89a0942014-03-07 15:29:14 -06001593 scale = option_scale ? option_scale : 1;
Scott Moreau248aaec2012-08-03 14:19:52 -06001594 count = option_count ? option_count : 1;
1595
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001596 section = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001597 while (weston_config_next_section(config,
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001598 &section, &section_name)) {
1599 if (strcmp(section_name, "output") != 0)
1600 continue;
1601 weston_config_section_get_string(section, "name", &name, NULL);
Rob Bradford5c4245d2013-07-26 16:29:44 +01001602 if (name == NULL || name[0] != 'X') {
1603 free(name);
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001604 continue;
Rob Bradford5c4245d2013-07-26 16:29:44 +01001605 }
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001606
1607 weston_config_section_get_string(section,
1608 "mode", &mode, "1024x600");
1609 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1610 weston_log("Invalid mode \"%s\" for output %s\n",
1611 mode, name);
1612 width = 1024;
1613 height = 600;
1614 }
1615 free(mode);
1616
1617 if (option_width)
1618 width = option_width;
1619 if (option_height)
1620 height = option_height;
1621
1622 weston_config_section_get_int(section, "scale", &scale, 1);
Kristian Høgsberg3c2e66b2014-03-10 13:41:35 -07001623 if (option_scale)
1624 scale = option_scale;
1625
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001626 weston_config_section_get_string(section,
1627 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001628 if (weston_parse_transform(t, &transform) < 0)
1629 weston_log("Invalid transform \"%s\" for output %s\n",
1630 t, name);
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001631 free(t);
1632
Giulio Camuffo954f1832014-10-11 18:27:30 +03001633 output = x11_backend_create_output(b, x, 0,
1634 width, height,
1635 fullscreen, no_input,
1636 name, transform, scale);
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001637 free(name);
Bryce Harrington665b0252015-05-26 18:14:21 -07001638 if (output == NULL) {
1639 weston_log("Failed to create configured x11 output\n");
Scott Moreau248aaec2012-08-03 14:19:52 -06001640 goto err_x11_input;
Bryce Harrington665b0252015-05-26 18:14:21 -07001641 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06001642
1643 x = pixman_region32_extents(&output->base.region)->x2;
1644
Scott Moreau248aaec2012-08-03 14:19:52 -06001645 output_count++;
1646 if (option_count && output_count >= option_count)
1647 break;
1648 }
1649
1650 for (i = output_count; i < count; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001651 output = x11_backend_create_output(b, x, 0, width, height,
1652 fullscreen, no_input, NULL,
1653 WL_OUTPUT_TRANSFORM_NORMAL, scale);
Bryce Harrington665b0252015-05-26 18:14:21 -07001654 if (output == NULL) {
1655 weston_log("Failed to create x11 output #%d\n", i);
Martin Olsson11434bb2012-07-08 03:03:44 +02001656 goto err_x11_input;
Bryce Harrington665b0252015-05-26 18:14:21 -07001657 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06001658 x = pixman_region32_extents(&output->base.region)->x2;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001659 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001660
Giulio Camuffo954f1832014-10-11 18:27:30 +03001661 b->xcb_source =
1662 wl_event_loop_add_fd(compositor->input_loop,
1663 xcb_get_file_descriptor(b->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001664 WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001665 x11_backend_handle_event, b);
1666 wl_event_source_check(b->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001667
Giulio Camuffo954f1832014-10-11 18:27:30 +03001668 compositor->backend = &b->base;
1669 return b;
Martin Olsson11434bb2012-07-08 03:03:44 +02001670
1671err_x11_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001672 x11_input_destroy(b);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001673err_renderer:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001674 compositor->renderer->destroy(compositor);
Martin Olsson11434bb2012-07-08 03:03:44 +02001675err_xdisplay:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001676 XCloseDisplay(b->dpy);
Martin Olsson11434bb2012-07-08 03:03:44 +02001677err_free:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001678 free(b);
Martin Olsson11434bb2012-07-08 03:03:44 +02001679 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001680}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001681
Giulio Camuffo954f1832014-10-11 18:27:30 +03001682WL_EXPORT int
1683backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001684 struct weston_config *config)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001685{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001686 struct x11_backend *b;
Scott Moreau248aaec2012-08-03 14:19:52 -06001687 int fullscreen = 0;
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001688 int no_input = 0;
Vasily Khoruzhickd8943512013-01-07 22:36:02 +03001689 int use_pixman = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001690
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001691 const struct weston_option x11_options[] = {
Scott Moreau248aaec2012-08-03 14:19:52 -06001692 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1693 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
Jason Ekstrandd89a0942014-03-07 15:29:14 -06001694 { WESTON_OPTION_INTEGER, "scale", 0, &option_scale },
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -05001695 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &fullscreen },
Scott Moreau248aaec2012-08-03 14:19:52 -06001696 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001697 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
Vasily Khoruzhickd8943512013-01-07 22:36:02 +03001698 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001699 };
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001700
1701 parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001702
Giulio Camuffo954f1832014-10-11 18:27:30 +03001703 b = x11_backend_create(compositor,
1704 fullscreen,
1705 no_input,
1706 use_pixman,
1707 argc, argv, config);
1708 if (b == NULL)
1709 return -1;
1710 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001711}