blob: 8ca809aedccf60ac62b885b4c111ed52929c99ed [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{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300177 struct weston_xkb_info *info = b->core_seat.keyboard->xkb_info;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400178 uint32_t ret = 0;
179
180 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
181 ret |= (1 << info->shift_mod);
182 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
183 ret |= (1 << info->caps_mod);
184 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
185 ret |= (1 << info->ctrl_mod);
186 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
187 ret |= (1 << info->alt_mod);
188 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
189 ret |= (1 << info->mod2_mod);
190 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
191 ret |= (1 << info->mod3_mod);
192 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
193 ret |= (1 << info->super_mod);
194 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
195 ret |= (1 << info->mod5_mod);
196
197 return ret;
198}
199
Daniel Stone62b33b62012-06-22 13:21:35 +0100200static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300201x11_backend_setup_xkb(struct x11_backend *b)
Daniel Stone62b33b62012-06-22 13:21:35 +0100202{
203#ifndef HAVE_XCB_XKB
204 weston_log("XCB-XKB not available during build\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300205 b->has_xkb = 0;
206 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100207 return;
208#else
209 const xcb_query_extension_reply_t *ext;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100210 xcb_generic_error_t *error;
Daniel Stonee2faa122012-06-22 13:21:39 +0100211 xcb_void_cookie_t select;
Ran Benita6a39d872012-10-31 20:14:57 +0200212 xcb_xkb_use_extension_cookie_t use_ext;
213 xcb_xkb_use_extension_reply_t *use_ext_reply;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100214 xcb_xkb_per_client_flags_cookie_t pcf;
215 xcb_xkb_per_client_flags_reply_t *pcf_reply;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400216 xcb_xkb_get_state_cookie_t state;
217 xcb_xkb_get_state_reply_t *state_reply;
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200218 uint32_t values[1] = { XCB_EVENT_MASK_PROPERTY_CHANGE };
Daniel Stone62b33b62012-06-22 13:21:35 +0100219
Giulio Camuffo954f1832014-10-11 18:27:30 +0300220 b->has_xkb = 0;
221 b->xkb_event_base = 0;
Daniel Stone62b33b62012-06-22 13:21:35 +0100222
Giulio Camuffo954f1832014-10-11 18:27:30 +0300223 ext = xcb_get_extension_data(b->conn, &xcb_xkb_id);
Daniel Stone62b33b62012-06-22 13:21:35 +0100224 if (!ext) {
225 weston_log("XKB extension not available on host X11 server\n");
226 return;
227 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300228 b->xkb_event_base = ext->first_event;
Daniel Stone62b33b62012-06-22 13:21:35 +0100229
Giulio Camuffo954f1832014-10-11 18:27:30 +0300230 select = xcb_xkb_select_events_checked(b->conn,
Ran Benita424ae012012-10-31 00:13:08 +0200231 XCB_XKB_ID_USE_CORE_KBD,
232 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
233 0,
234 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
235 0,
236 0,
237 NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300238 error = xcb_request_check(b->conn, select);
Daniel Stonee2faa122012-06-22 13:21:39 +0100239 if (error) {
240 weston_log("error: failed to select for XKB state events\n");
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200241 free(error);
Daniel Stonee2faa122012-06-22 13:21:39 +0100242 return;
243 }
244
Giulio Camuffo954f1832014-10-11 18:27:30 +0300245 use_ext = xcb_xkb_use_extension(b->conn,
Ran Benita6a39d872012-10-31 20:14:57 +0200246 XCB_XKB_MAJOR_VERSION,
247 XCB_XKB_MINOR_VERSION);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300248 use_ext_reply = xcb_xkb_use_extension_reply(b->conn, use_ext, NULL);
Ran Benita6a39d872012-10-31 20:14:57 +0200249 if (!use_ext_reply) {
250 weston_log("couldn't start using XKB extension\n");
251 return;
252 }
253
254 if (!use_ext_reply->supported) {
255 weston_log("XKB extension version on the server is too old "
256 "(want %d.%d, has %d.%d)\n",
257 XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION,
258 use_ext_reply->serverMajor, use_ext_reply->serverMinor);
259 free(use_ext_reply);
260 return;
261 }
262 free(use_ext_reply);
263
Giulio Camuffo954f1832014-10-11 18:27:30 +0300264 pcf = xcb_xkb_per_client_flags(b->conn,
Daniel Stone3ee91e12012-06-22 13:21:36 +0100265 XCB_XKB_ID_USE_CORE_KBD,
266 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
267 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
268 0,
269 0,
270 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300271 pcf_reply = xcb_xkb_per_client_flags_reply(b->conn, pcf, NULL);
Ran Benita7109cc82012-10-31 20:14:58 +0200272 if (!pcf_reply ||
273 !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT)) {
Daniel Stone3ee91e12012-06-22 13:21:36 +0100274 weston_log("failed to set XKB per-client flags, not using "
275 "detectable repeat\n");
Ran Benita7109cc82012-10-31 20:14:58 +0200276 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100277 return;
278 }
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200279 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100280
Giulio Camuffo954f1832014-10-11 18:27:30 +0300281 state = xcb_xkb_get_state(b->conn, XCB_XKB_ID_USE_CORE_KBD);
282 state_reply = xcb_xkb_get_state_reply(b->conn, state, NULL);
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200283 if (!state_reply) {
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400284 weston_log("failed to get initial XKB state\n");
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400285 return;
286 }
287
Giulio Camuffo954f1832014-10-11 18:27:30 +0300288 xkb_state_update_mask(b->core_seat.keyboard->xkb_state.state,
289 get_xkb_mod_mask(b, state_reply->baseMods),
290 get_xkb_mod_mask(b, state_reply->latchedMods),
291 get_xkb_mod_mask(b, state_reply->lockedMods),
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400292 0,
293 0,
294 state_reply->group);
295
296 free(state_reply);
297
Giulio Camuffo954f1832014-10-11 18:27:30 +0300298 xcb_change_window_attributes(b->conn, b->screen->root,
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200299 XCB_CW_EVENT_MASK, values);
300
Giulio Camuffo954f1832014-10-11 18:27:30 +0300301 b->has_xkb = 1;
Daniel Stone62b33b62012-06-22 13:21:35 +0100302#endif
303}
304
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200305#ifdef HAVE_XCB_XKB
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200306static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300307update_xkb_keymap(struct x11_backend *b)
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200308{
309 struct xkb_keymap *keymap;
310
Giulio Camuffo954f1832014-10-11 18:27:30 +0300311 keymap = x11_backend_get_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200312 if (!keymap) {
313 weston_log("failed to get XKB keymap\n");
314 return;
315 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300316 weston_seat_update_keymap(&b->core_seat, keymap);
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200317 xkb_keymap_unref(keymap);
318}
Jonas Ådahlf88571f2013-10-25 23:18:01 +0200319#endif
Rui Matosc6c2f8e2013-10-10 19:44:20 +0200320
Darxus55973f22010-11-22 21:24:39 -0500321static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300322x11_input_create(struct x11_backend *b, int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400323{
Daniel Stone576cd152012-06-01 12:14:02 +0100324 struct xkb_keymap *keymap;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400325
Giulio Camuffo954f1832014-10-11 18:27:30 +0300326 weston_seat_init(&b->core_seat, b->compositor, "default");
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300327
328 if (no_input)
329 return 0;
330
Giulio Camuffo954f1832014-10-11 18:27:30 +0300331 weston_seat_init_pointer(&b->core_seat);
Daniel Stone576cd152012-06-01 12:14:02 +0100332
Giulio Camuffo954f1832014-10-11 18:27:30 +0300333 keymap = x11_backend_get_keymap(b);
334 if (weston_seat_init_keyboard(&b->core_seat, keymap) < 0)
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500335 return -1;
Ran Benitac9c74152014-08-19 23:59:52 +0300336 xkb_keymap_unref(keymap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400337
Giulio Camuffo954f1832014-10-11 18:27:30 +0300338 x11_backend_setup_xkb(b);
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400339
Darxus55973f22010-11-22 21:24:39 -0500340 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400341}
342
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200343static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300344x11_input_destroy(struct x11_backend *b)
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200345{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300346 weston_seat_release(&b->core_seat);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200347}
348
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500349static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200350x11_output_start_repaint_loop(struct weston_output *output)
351{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400352 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200353
Pekka Paalanen662f3842015-03-18 12:17:26 +0200354 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200355 weston_output_finish_frame(output, &ts, PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200356}
357
David Herrmann1edf44c2013-10-22 17:11:26 +0200358static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300359x11_output_repaint_gl(struct weston_output *output_base,
360 pixman_region32_t *damage)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500361{
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500362 struct x11_output *output = (struct x11_output *)output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400363 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500364
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400365 ec->renderer->repaint_output(output_base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500366
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200367 pixman_region32_subtract(&ec->primary_plane.damage,
368 &ec->primary_plane.damage, damage);
369
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500370 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200371 return 0;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500372}
373
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300374static void
Alexander Larsson80f91632013-05-22 14:41:38 +0200375set_clip_for_output(struct weston_output *output_base, pixman_region32_t *region)
376{
377 struct x11_output *output = (struct x11_output *)output_base;
378 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300379 struct x11_backend *b = (struct x11_backend *)ec->backend;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500380 pixman_region32_t transformed_region;
Alexander Larsson80f91632013-05-22 14:41:38 +0200381 pixman_box32_t *rects;
382 xcb_rectangle_t *output_rects;
Alexander Larsson80f91632013-05-22 14:41:38 +0200383 xcb_void_cookie_t cookie;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500384 int nrects, i;
Alexander Larsson80f91632013-05-22 14:41:38 +0200385 xcb_generic_error_t *err;
386
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500387 pixman_region32_init(&transformed_region);
388 pixman_region32_copy(&transformed_region, region);
389 pixman_region32_translate(&transformed_region,
390 -output_base->x, -output_base->y);
391 weston_transformed_region(output_base->width, output_base->height,
392 output_base->transform,
393 output_base->current_scale,
394 &transformed_region, &transformed_region);
395
396 rects = pixman_region32_rectangles(&transformed_region, &nrects);
Alexander Larsson80f91632013-05-22 14:41:38 +0200397 output_rects = calloc(nrects, sizeof(xcb_rectangle_t));
398
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500399 if (output_rects == NULL) {
400 pixman_region32_fini(&transformed_region);
Alexander Larsson80f91632013-05-22 14:41:38 +0200401 return;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500402 }
Alexander Larsson80f91632013-05-22 14:41:38 +0200403
404 for (i = 0; i < nrects; i++) {
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500405 output_rects[i].x = rects[i].x1;
406 output_rects[i].y = rects[i].y1;
407 output_rects[i].width = rects[i].x2 - rects[i].x1;
408 output_rects[i].height = rects[i].y2 - rects[i].y1;
Alexander Larsson80f91632013-05-22 14:41:38 +0200409 }
410
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500411 pixman_region32_fini(&transformed_region);
412
Giulio Camuffo954f1832014-10-11 18:27:30 +0300413 cookie = xcb_set_clip_rectangles_checked(b->conn, XCB_CLIP_ORDERING_UNSORTED,
Alexander Larsson80f91632013-05-22 14:41:38 +0200414 output->gc,
415 0, 0, nrects,
416 output_rects);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300417 err = xcb_request_check(b->conn, cookie);
Alexander Larsson80f91632013-05-22 14:41:38 +0200418 if (err != NULL) {
419 weston_log("Failed to set clip rects, err: %d\n", err->error_code);
420 free(err);
421 }
422 free(output_rects);
423}
424
425
David Herrmann1edf44c2013-10-22 17:11:26 +0200426static int
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300427x11_output_repaint_shm(struct weston_output *output_base,
428 pixman_region32_t *damage)
429{
430 struct x11_output *output = (struct x11_output *)output_base;
431 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300432 struct x11_backend *b = (struct x11_backend *)ec->backend;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300433 xcb_void_cookie_t cookie;
434 xcb_generic_error_t *err;
435
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200436 pixman_renderer_output_set_buffer(output_base, output->hw_surface);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300437 ec->renderer->repaint_output(output_base, damage);
438
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300439 pixman_region32_subtract(&ec->primary_plane.damage,
440 &ec->primary_plane.damage, damage);
Alexander Larsson80f91632013-05-22 14:41:38 +0200441 set_clip_for_output(output_base, damage);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300442 cookie = xcb_shm_put_image_checked(b->conn, output->window, output->gc,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300443 pixman_image_get_width(output->hw_surface),
444 pixman_image_get_height(output->hw_surface),
445 0, 0,
446 pixman_image_get_width(output->hw_surface),
447 pixman_image_get_height(output->hw_surface),
448 0, 0, output->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
449 0, output->segment, 0);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300450 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300451 if (err != NULL) {
452 weston_log("Failed to put shm image, err: %d\n", err->error_code);
453 free(err);
454 }
455
456 wl_event_source_timer_update(output->finish_frame_timer, 10);
David Herrmann1edf44c2013-10-22 17:11:26 +0200457 return 0;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300458}
459
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100460static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400461finish_frame_handler(void *data)
462{
463 struct x11_output *output = data;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200464 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200465
Pekka Paalanen662f3842015-03-18 12:17:26 +0200466 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200467 weston_output_finish_frame(&output->base, &ts, 0);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400468
469 return 1;
470}
471
Matt Roper361d2ad2011-08-29 13:52:23 -0700472static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300473x11_output_deinit_shm(struct x11_backend *b, struct x11_output *output)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300474{
475 xcb_void_cookie_t cookie;
476 xcb_generic_error_t *err;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300477 xcb_free_gc(b->conn, output->gc);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300478
479 pixman_image_unref(output->hw_surface);
480 output->hw_surface = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300481 cookie = xcb_shm_detach_checked(b->conn, output->segment);
482 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300483 if (err) {
484 weston_log("xcb_shm_detach failed, error %d\n", err->error_code);
485 free(err);
486 }
487 shmdt(output->buf);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300488}
489
490static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500491x11_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700492{
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200493 struct x11_output *output = (struct x11_output *)output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300494 struct x11_backend *backend =
495 (struct x11_backend *)output->base.compositor->backend;
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200496
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200497 wl_event_source_remove(output->finish_frame_timer);
498
Giulio Camuffo954f1832014-10-11 18:27:30 +0300499 if (backend->use_pixman) {
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300500 pixman_renderer_output_destroy(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300501 x11_output_deinit_shm(backend, output);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300502 } else
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300503 gl_renderer->output_destroy(output_base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200504
Giulio Camuffo954f1832014-10-11 18:27:30 +0300505 xcb_destroy_window(backend->conn, output->window);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200506
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500507 weston_output_destroy(&output->base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200508
509 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700510}
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200511
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400512static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300513x11_output_set_wm_protocols(struct x11_backend *b,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500514 struct x11_output *output)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400515{
516 xcb_atom_t list[1];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400517
Giulio Camuffo954f1832014-10-11 18:27:30 +0300518 list[0] = b->atom.wm_delete_window;
519 xcb_change_property (b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400520 XCB_PROP_MODE_REPLACE,
521 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300522 b->atom.wm_protocols,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400523 XCB_ATOM_ATOM,
524 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500525 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400526 list);
527}
528
529struct wm_normal_hints {
530 uint32_t flags;
531 uint32_t pad[4];
532 int32_t min_width, min_height;
533 int32_t max_width, max_height;
534 int32_t width_inc, height_inc;
535 int32_t min_aspect_x, min_aspect_y;
536 int32_t max_aspect_x, max_aspect_y;
537 int32_t base_width, base_height;
538 int32_t win_gravity;
539};
540
541#define WM_NORMAL_HINTS_MIN_SIZE 16
542#define WM_NORMAL_HINTS_MAX_SIZE 32
543
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500544static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300545x11_output_set_icon(struct x11_backend *b,
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400546 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500547{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400548 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400549 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400550 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500551
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400552 image = load_image(filename);
553 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500554 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400555 width = pixman_image_get_width(image);
556 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500557 icon = malloc(width * height * 4 + 8);
558 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400559 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500560 return;
561 }
562
563 icon[0] = width;
564 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400565 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300566 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
567 b->atom.net_wm_icon, b->atom.cardinal, 32,
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500568 width * height + 2, icon);
569 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400570 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500571}
572
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500573static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300574x11_output_wait_for_map(struct x11_backend *b, struct x11_output *output)
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500575{
576 xcb_map_notify_event_t *map_notify;
577 xcb_configure_notify_event_t *configure_notify;
578 xcb_generic_event_t *event;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500579 int mapped = 0, configured = 0;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500580 uint8_t response_type;
581
582 /* This isn't the nicest way to do this. Ideally, we could
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500583 * just go back to the main loop and once we get the configure
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500584 * notify, we add the output to the compositor. While we do
585 * support output hotplug, we can't start up with no outputs.
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500586 * We could add the output and then resize once we get the
587 * configure notify, but we don't want to start up and
588 * immediately resize the output.
589 *
590 * Also, some window managers don't give us our final
591 * fullscreen size before map_notify, so if we don't get a
592 * configure_notify before map_notify, we just wait for the
593 * first one and hope that's our size. */
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500594
Giulio Camuffo954f1832014-10-11 18:27:30 +0300595 xcb_flush(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500596
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500597 while (!mapped || !configured) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300598 event = xcb_wait_for_event(b->conn);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500599 response_type = event->response_type & ~0x80;
600
601 switch (response_type) {
602 case XCB_MAP_NOTIFY:
603 map_notify = (xcb_map_notify_event_t *) event;
604 if (map_notify->window == output->window)
605 mapped = 1;
606 break;
607
608 case XCB_CONFIGURE_NOTIFY:
609 configure_notify =
610 (xcb_configure_notify_event_t *) event;
611
Alexander Larsson4ea95522013-05-22 14:41:37 +0200612
613 if (configure_notify->width % output->scale != 0 ||
614 configure_notify->height % output->scale != 0)
615 weston_log("Resolution is not a multiple of screen size, rounding\n");
Jason Ekstrandd89a0942014-03-07 15:29:14 -0600616 output->mode.width = configure_notify->width;
617 output->mode.height = configure_notify->height;
Kristian Høgsberg8e7adbf2013-02-14 21:14:11 -0500618 configured = 1;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500619 break;
620 }
621 }
622}
623
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300624static xcb_visualtype_t *
625find_visual_by_id(xcb_screen_t *screen,
626 xcb_visualid_t id)
627{
628 xcb_depth_iterator_t i;
629 xcb_visualtype_iterator_t j;
630 for (i = xcb_screen_allowed_depths_iterator(screen);
631 i.rem;
632 xcb_depth_next(&i)) {
633 for (j = xcb_depth_visuals_iterator(i.data);
634 j.rem;
635 xcb_visualtype_next(&j)) {
636 if (j.data->visual_id == id)
637 return j.data;
638 }
639 }
640 return 0;
641}
642
643static uint8_t
644get_depth_of_visual(xcb_screen_t *screen,
645 xcb_visualid_t id)
646{
647 xcb_depth_iterator_t i;
648 xcb_visualtype_iterator_t j;
649 for (i = xcb_screen_allowed_depths_iterator(screen);
650 i.rem;
651 xcb_depth_next(&i)) {
652 for (j = xcb_depth_visuals_iterator(i.data);
653 j.rem;
654 xcb_visualtype_next(&j)) {
655 if (j.data->visual_id == id)
656 return i.data->depth;
657 }
658 }
659 return 0;
660}
661
662static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300663x11_output_init_shm(struct x11_backend *b, struct x11_output *output,
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300664 int width, int height)
665{
666 xcb_screen_iterator_t iter;
667 xcb_visualtype_t *visual_type;
668 xcb_format_iterator_t fmt;
669 xcb_void_cookie_t cookie;
670 xcb_generic_error_t *err;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300671 const xcb_query_extension_reply_t *ext;
672 int bitsperpixel = 0;
673 pixman_format_code_t pixman_format;
674
675 /* Check if SHM is available */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300676 ext = xcb_get_extension_data(b->conn, &xcb_shm_id);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300677 if (ext == NULL || !ext->present) {
678 /* SHM is missing */
679 weston_log("SHM extension is not available\n");
680 errno = ENOENT;
681 return -1;
682 }
683
Giulio Camuffo954f1832014-10-11 18:27:30 +0300684 iter = xcb_setup_roots_iterator(xcb_get_setup(b->conn));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300685 visual_type = find_visual_by_id(iter.data, iter.data->root_visual);
686 if (!visual_type) {
687 weston_log("Failed to lookup visual for root window\n");
688 errno = ENOENT;
689 return -1;
690 }
691 weston_log("Found visual, bits per value: %d, red_mask: %.8x, green_mask: %.8x, blue_mask: %.8x\n",
692 visual_type->bits_per_rgb_value,
693 visual_type->red_mask,
694 visual_type->green_mask,
695 visual_type->blue_mask);
696 output->depth = get_depth_of_visual(iter.data, iter.data->root_visual);
697 weston_log("Visual depth is %d\n", output->depth);
698
Giulio Camuffo954f1832014-10-11 18:27:30 +0300699 for (fmt = xcb_setup_pixmap_formats_iterator(xcb_get_setup(b->conn));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300700 fmt.rem;
701 xcb_format_next(&fmt)) {
702 if (fmt.data->depth == output->depth) {
703 bitsperpixel = fmt.data->bits_per_pixel;
704 break;
705 }
706 }
707 weston_log("Found format for depth %d, bpp: %d\n",
708 output->depth, bitsperpixel);
709
710 if (bitsperpixel == 32 &&
711 visual_type->red_mask == 0xff0000 &&
712 visual_type->green_mask == 0x00ff00 &&
713 visual_type->blue_mask == 0x0000ff) {
714 weston_log("Will use x8r8g8b8 format for SHM surfaces\n");
715 pixman_format = PIXMAN_x8r8g8b8;
MoD1b55a592013-11-28 05:24:21 +0000716 } else if (bitsperpixel == 16 &&
717 visual_type->red_mask == 0x00f800 &&
718 visual_type->green_mask == 0x0007e0 &&
719 visual_type->blue_mask == 0x00001f) {
720 weston_log("Will use r5g6b5 format for SHM surfaces\n");
721 pixman_format = PIXMAN_r5g6b5;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300722 } else {
723 weston_log("Can't find appropriate format for SHM pixmap\n");
724 errno = ENOTSUP;
725 return -1;
726 }
727
728
729 /* Create SHM segment and attach it */
730 output->shm_id = shmget(IPC_PRIVATE, width * height * (bitsperpixel / 8), IPC_CREAT | S_IRWXU);
731 if (output->shm_id == -1) {
732 weston_log("x11shm: failed to allocate SHM segment\n");
733 return -1;
734 }
735 output->buf = shmat(output->shm_id, NULL, 0 /* read/write */);
736 if (-1 == (long)output->buf) {
737 weston_log("x11shm: failed to attach SHM segment\n");
738 return -1;
739 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300740 output->segment = xcb_generate_id(b->conn);
741 cookie = xcb_shm_attach_checked(b->conn, output->segment, output->shm_id, 1);
742 err = xcb_request_check(b->conn, cookie);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300743 if (err) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700744 weston_log("x11shm: xcb_shm_attach error %d, op code %d, resource id %d\n",
745 err->error_code, err->major_code, err->minor_code);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300746 free(err);
747 return -1;
748 }
749
750 shmctl(output->shm_id, IPC_RMID, NULL);
751
752 /* Now create pixman image */
753 output->hw_surface = pixman_image_create_bits(pixman_format, width, height, output->buf,
754 width * (bitsperpixel / 8));
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300755
Giulio Camuffo954f1832014-10-11 18:27:30 +0300756 output->gc = xcb_generate_id(b->conn);
757 xcb_create_gc(b->conn, output->gc, output->window, 0, NULL);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300758
759 return 0;
760}
761
Scott Moreau1bad5db2012-08-18 01:04:05 -0600762static struct x11_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300763x11_backend_create_output(struct x11_backend *b, int x, int y,
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300764 int width, int height, int fullscreen,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600765 int no_input, char *configured_name,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200766 uint32_t transform, int32_t scale)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400767{
Scott Moreau1bad5db2012-08-18 01:04:05 -0600768 static const char name[] = "Weston Compositor";
Kristian Høgsberg86000402012-01-03 23:24:14 -0500769 static const char class[] = "weston-1\0Weston Compositor";
Scott Moreau1bad5db2012-08-18 01:04:05 -0600770 char title[32];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400771 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400772 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400773 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400774 struct wl_event_loop *loop;
Kristian Høgsberg8404acb2014-03-18 12:15:42 -0700775 int output_width, output_height, width_mm, height_mm;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300776 int ret;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400777 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500778 xcb_atom_t atom_list[1];
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300779 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400780 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300781 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400782 0
783 };
784
Alexander Larsson4ea95522013-05-22 14:41:37 +0200785 output_width = width * scale;
786 output_height = height * scale;
787
Scott Moreau1bad5db2012-08-18 01:04:05 -0600788 if (configured_name)
789 sprintf(title, "%s - %s", name, configured_name);
790 else
791 strcpy(title, name);
792
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300793 if (!no_input)
794 values[0] |=
795 XCB_EVENT_MASK_KEY_PRESS |
796 XCB_EVENT_MASK_KEY_RELEASE |
797 XCB_EVENT_MASK_BUTTON_PRESS |
798 XCB_EVENT_MASK_BUTTON_RELEASE |
799 XCB_EVENT_MASK_POINTER_MOTION |
800 XCB_EVENT_MASK_ENTER_WINDOW |
801 XCB_EVENT_MASK_LEAVE_WINDOW |
802 XCB_EVENT_MASK_KEYMAP_STATE |
803 XCB_EVENT_MASK_FOCUS_CHANGE;
804
Peter Huttererf3d62272013-08-08 11:57:05 +1000805 output = zalloc(sizeof *output);
Bryce Harrington665b0252015-05-26 18:14:21 -0700806 if (output == NULL) {
807 perror("zalloc");
Scott Moreau1bad5db2012-08-18 01:04:05 -0600808 return NULL;
Bryce Harrington665b0252015-05-26 18:14:21 -0700809 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400810
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400811 output->mode.flags =
812 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200813
Alexander Larsson0b135062013-05-28 16:23:36 +0200814 output->mode.width = output_width;
815 output->mode.height = output_height;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400816 output->mode.refresh = 60000;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200817 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400818 wl_list_init(&output->base.mode_list);
819 wl_list_insert(&output->base.mode_list, &output->mode.link);
820
Giulio Camuffo954f1832014-10-11 18:27:30 +0300821 values[1] = b->null_cursor;
822 output->window = xcb_generate_id(b->conn);
823 iter = xcb_setup_roots_iterator(xcb_get_setup(b->conn));
824 xcb_create_window(b->conn,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400825 XCB_COPY_FROM_PARENT,
826 output->window,
827 iter.data->root,
828 0, 0,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200829 output_width, output_height,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400830 0,
831 XCB_WINDOW_CLASS_INPUT_OUTPUT,
832 iter.data->root_visual,
833 mask, values);
834
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500835 if (fullscreen) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300836 atom_list[0] = b->atom.net_wm_state_fullscreen;
837 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500838 output->window,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300839 b->atom.net_wm_state,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500840 XCB_ATOM_ATOM, 32,
841 ARRAY_LENGTH(atom_list), atom_list);
842 } else {
843 /* Don't resize me. */
844 memset(&normal_hints, 0, sizeof normal_hints);
845 normal_hints.flags =
846 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200847 normal_hints.min_width = output_width;
848 normal_hints.min_height = output_height;
849 normal_hints.max_width = output_width;
850 normal_hints.max_height = output_height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300851 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
852 b->atom.wm_normal_hints,
853 b->atom.wm_size_hints, 32,
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500854 sizeof normal_hints / 4,
855 (uint8_t *) &normal_hints);
856 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400857
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400858 /* Set window name. Don't bother with non-EWMH WMs. */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300859 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
860 b->atom.net_wm_name, b->atom.utf8_string, 8,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600861 strlen(title), title);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300862 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
863 b->atom.wm_class, b->atom.string, 8,
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500864 sizeof class, class);
865
Giulio Camuffo954f1832014-10-11 18:27:30 +0300866 x11_output_set_icon(b, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500867
Giulio Camuffo954f1832014-10-11 18:27:30 +0300868 x11_output_set_wm_protocols(b, output);
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500869
Giulio Camuffo954f1832014-10-11 18:27:30 +0300870 xcb_map_window(b->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400871
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -0500872 if (fullscreen)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300873 x11_output_wait_for_map(b, output);
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400874
Jonas Ådahle5a12252013-04-05 23:07:11 +0200875 output->base.start_repaint_loop = x11_output_start_repaint_loop;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300876 if (b->use_pixman)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300877 output->base.repaint = x11_output_repaint_shm;
878 else
879 output->base.repaint = x11_output_repaint_gl;
Matt Roper361d2ad2011-08-29 13:52:23 -0700880 output->base.destroy = x11_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800881 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200882 output->base.set_backlight = NULL;
883 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800884 output->base.switch_mode = NULL;
Hardeningff39efa2013-09-18 23:56:35 +0200885 output->base.current_mode = &output->mode;
Jason Ekstrand22e16b62014-09-15 12:10:27 -0700886 output->base.make = "weston-X11";
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -0500887 output->base.model = "none";
Ander Conselvan de Oliveirad4ea71e2013-12-18 19:51:40 +0200888
889 if (configured_name)
890 output->base.name = strdup(configured_name);
891
Giulio Camuffo954f1832014-10-11 18:27:30 +0300892 width_mm = width * b->screen->width_in_millimeters /
893 b->screen->width_in_pixels;
894 height_mm = height * b->screen->height_in_millimeters /
895 b->screen->height_in_pixels;
896 weston_output_init(&output->base, b->compositor,
Kristian Høgsberg8404acb2014-03-18 12:15:42 -0700897 x, y, width_mm, height_mm, transform, scale);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100898
Giulio Camuffo954f1832014-10-11 18:27:30 +0300899 if (b->use_pixman) {
900 if (x11_output_init_shm(b, output,
Kristian Høgsbergb5a4f982013-10-23 23:38:50 -0700901 output->mode.width,
Bryce Harrington665b0252015-05-26 18:14:21 -0700902 output->mode.height) < 0) {
903 weston_log("Failed to initialize SHM for the X11 output\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300904 return NULL;
Bryce Harrington665b0252015-05-26 18:14:21 -0700905 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300906 if (pixman_renderer_output_create(&output->base) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -0700907 weston_log("Failed to create pixman renderer for output\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300908 x11_output_deinit_shm(b, output);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300909 return NULL;
910 }
911 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +0100912 /* eglCreatePlatformWindowSurfaceEXT takes a Window*
913 * but eglCreateWindowSurface takes a Window. */
914 Window xid = (Window) output->window;
915
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300916 ret = gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000917 (EGLNativeWindowType) output->window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100918 &xid,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000919 gl_renderer->opaque_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500920 NULL,
921 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300922 if (ret < 0)
Vasily Khoruzhickb3add192013-01-07 20:39:50 +0300923 return NULL;
924 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100925
Giulio Camuffo954f1832014-10-11 18:27:30 +0300926 loop = wl_display_get_event_loop(b->compositor->wl_display);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100927 output->finish_frame_timer =
928 wl_event_loop_add_timer(loop, finish_frame_handler, output);
929
Giulio Camuffo954f1832014-10-11 18:27:30 +0300930 weston_compositor_add_output(b->compositor, &output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400931
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400932 weston_log("x11 output %dx%d, window id %d\n",
933 width, height, output->window);
934
Scott Moreau1bad5db2012-08-18 01:04:05 -0600935 return output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400936}
937
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400938static struct x11_output *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300939x11_backend_find_output(struct x11_backend *b, xcb_window_t window)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400940{
941 struct x11_output *output;
942
Giulio Camuffo954f1832014-10-11 18:27:30 +0300943 wl_list_for_each(output, &b->compositor->output_list, base.link) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400944 if (output->window == window)
945 return output;
946 }
947
Derek Foreman99bfa642014-12-11 15:44:46 -0600948 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400949}
950
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200951static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300952x11_backend_delete_window(struct x11_backend *b, xcb_window_t window)
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200953{
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +0200954 struct x11_output *output;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200955
Giulio Camuffo954f1832014-10-11 18:27:30 +0300956 output = x11_backend_find_output(b, window);
Derek Foreman99bfa642014-12-11 15:44:46 -0600957 if (output)
958 x11_output_destroy(&output->base);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200959
Giulio Camuffo954f1832014-10-11 18:27:30 +0300960 xcb_flush(b->conn);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200961
Giulio Camuffo954f1832014-10-11 18:27:30 +0300962 if (wl_list_empty(&b->compositor->output_list))
963 wl_display_terminate(b->compositor->wl_display);
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +0200964}
965
Derek Foremand540f4b2015-01-27 16:26:49 -0600966static void delete_cb(void *data)
967{
968 struct window_delete_data *wd = data;
969
Giulio Camuffo954f1832014-10-11 18:27:30 +0300970 x11_backend_delete_window(wd->backend, wd->window);
Derek Foremand540f4b2015-01-27 16:26:49 -0600971 free(wd);
972}
973
Daniel Stone154c34c2012-06-22 13:21:40 +0100974#ifdef HAVE_XCB_XKB
Daniel Stonee2faa122012-06-22 13:21:39 +0100975static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300976update_xkb_state(struct x11_backend *b, xcb_xkb_state_notify_event_t *state)
Daniel Stonee2faa122012-06-22 13:21:39 +0100977{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300978 xkb_state_update_mask(b->core_seat.keyboard->xkb_state.state,
979 get_xkb_mod_mask(b, state->baseMods),
980 get_xkb_mod_mask(b, state->latchedMods),
981 get_xkb_mod_mask(b, state->lockedMods),
Daniel Stonee2faa122012-06-22 13:21:39 +0100982 0,
983 0,
984 state->group);
985
Giulio Camuffo954f1832014-10-11 18:27:30 +0300986 notify_modifiers(&b->core_seat,
987 wl_display_next_serial(b->compositor->wl_display));
Daniel Stonee2faa122012-06-22 13:21:39 +0100988}
989#endif
990
Daniel Stone154c34c2012-06-22 13:21:40 +0100991/**
992 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
993 * the best we can do (given that XCB also lacks XI2 support), is to take
994 * the state from the core key events. Unfortunately that only gives us
995 * the effective (i.e. union of depressed/latched/locked) state, and we
996 * need the granularity.
997 *
998 * So we still update the state with every key event we see, but also use
999 * the state field from X11 events as a mask so we don't get any stuck
1000 * modifiers.
1001 */
1002static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001003update_xkb_state_from_core(struct x11_backend *b, uint16_t x11_mask)
Daniel Stone154c34c2012-06-22 13:21:40 +01001004{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001005 uint32_t mask = get_xkb_mod_mask(b, x11_mask);
1006 struct weston_keyboard *keyboard = b->core_seat.keyboard;
Daniel Stone154c34c2012-06-22 13:21:40 +01001007
Giulio Camuffo954f1832014-10-11 18:27:30 +03001008 xkb_state_update_mask(b->core_seat.keyboard->xkb_state.state,
Daniel Stone154c34c2012-06-22 13:21:40 +01001009 keyboard->modifiers.mods_depressed & mask,
1010 keyboard->modifiers.mods_latched & mask,
1011 keyboard->modifiers.mods_locked & mask,
1012 0,
1013 0,
1014 (x11_mask >> 13) & 3);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001015 notify_modifiers(&b->core_seat,
1016 wl_display_next_serial(b->compositor->wl_display));
Daniel Stone154c34c2012-06-22 13:21:40 +01001017}
1018
Tiago Vignattia3a71622011-12-08 17:03:17 +02001019static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001020x11_backend_deliver_button_event(struct x11_backend *b,
1021 xcb_generic_event_t *event, int state)
Tiago Vignattia3a71622011-12-08 17:03:17 +02001022{
1023 xcb_button_press_event_t *button_event =
1024 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +01001025 uint32_t button;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001026 struct x11_output *output;
1027
Giulio Camuffo954f1832014-10-11 18:27:30 +03001028 output = x11_backend_find_output(b, button_event->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001029 if (!output)
1030 return;
Kristian Høgsberg73308622012-10-30 11:04:52 -04001031
1032 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001033 xcb_grab_pointer(b->conn, 0, output->window,
Kristian Høgsberg73308622012-10-30 11:04:52 -04001034 XCB_EVENT_MASK_BUTTON_PRESS |
1035 XCB_EVENT_MASK_BUTTON_RELEASE |
1036 XCB_EVENT_MASK_POINTER_MOTION |
1037 XCB_EVENT_MASK_ENTER_WINDOW |
1038 XCB_EVENT_MASK_LEAVE_WINDOW,
1039 XCB_GRAB_MODE_ASYNC,
1040 XCB_GRAB_MODE_ASYNC,
1041 output->window, XCB_CURSOR_NONE,
1042 button_event->time);
1043 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001044 xcb_ungrab_pointer(b->conn, button_event->time);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001045
Giulio Camuffo954f1832014-10-11 18:27:30 +03001046 if (!b->has_xkb)
1047 update_xkb_state_from_core(b, button_event->state);
Daniel Stone154c34c2012-06-22 13:21:40 +01001048
Tiago Vignattia3a71622011-12-08 17:03:17 +02001049 switch (button_event->detail) {
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001050 case 1:
1051 button = BTN_LEFT;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001052 break;
1053 case 2:
1054 button = BTN_MIDDLE;
1055 break;
1056 case 3:
1057 button = BTN_RIGHT;
1058 break;
1059 case 4:
Jonas Ådahl62efe202012-09-27 18:40:41 +02001060 /* Axis are measured in pixels, but the xcb events are discrete
1061 * steps. Therefore move the axis by some pixels every step. */
Scott Moreau210d0792012-03-22 10:47:01 -06001062 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001063 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001064 weston_compositor_get_time(),
1065 WL_POINTER_AXIS_VERTICAL_SCROLL,
1066 -DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -06001067 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001068 case 5:
Scott Moreau210d0792012-03-22 10:47:01 -06001069 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001070 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001071 weston_compositor_get_time(),
1072 WL_POINTER_AXIS_VERTICAL_SCROLL,
1073 DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -06001074 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001075 case 6:
Scott Moreau210d0792012-03-22 10:47:01 -06001076 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001077 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001078 weston_compositor_get_time(),
1079 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
1080 -DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -06001081 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001082 case 7:
Scott Moreau210d0792012-03-22 10:47:01 -06001083 if (state)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001084 notify_axis(&b->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +02001085 weston_compositor_get_time(),
1086 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
1087 DEFAULT_AXIS_STEP_DISTANCE);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001088 return;
Dima Ryazanov3e4d4bd2015-02-04 01:51:57 -08001089 default:
1090 button = button_event->detail + BTN_SIDE - 8;
1091 break;
Tiago Vignattia3a71622011-12-08 17:03:17 +02001092 }
1093
Giulio Camuffo954f1832014-10-11 18:27:30 +03001094 notify_button(&b->core_seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001095 weston_compositor_get_time(), button,
1096 state ? WL_POINTER_BUTTON_STATE_PRESSED :
1097 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignattia3a71622011-12-08 17:03:17 +02001098}
1099
Scott Moreau1bad5db2012-08-18 01:04:05 -06001100static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001101x11_backend_deliver_motion_event(struct x11_backend *b,
1102 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001103{
1104 struct x11_output *output;
1105 wl_fixed_t x, y;
1106 xcb_motion_notify_event_t *motion_notify =
1107 (xcb_motion_notify_event_t *) event;
1108
Giulio Camuffo954f1832014-10-11 18:27:30 +03001109 if (!b->has_xkb)
1110 update_xkb_state_from_core(b, motion_notify->state);
1111 output = x11_backend_find_output(b, motion_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001112 if (!output)
1113 return;
1114
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001115 weston_output_transform_coordinate(&output->base,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05001116 wl_fixed_from_int(motion_notify->event_x),
1117 wl_fixed_from_int(motion_notify->event_y),
1118 &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001119
Giulio Camuffo954f1832014-10-11 18:27:30 +03001120 notify_motion(&b->core_seat, weston_compositor_get_time(),
1121 x - b->prev_x, y - b->prev_y);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001122
Giulio Camuffo954f1832014-10-11 18:27:30 +03001123 b->prev_x = x;
1124 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001125}
1126
1127static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001128x11_backend_deliver_enter_event(struct x11_backend *b,
1129 xcb_generic_event_t *event)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001130{
1131 struct x11_output *output;
1132 wl_fixed_t x, y;
1133
1134 xcb_enter_notify_event_t *enter_notify =
1135 (xcb_enter_notify_event_t *) event;
1136 if (enter_notify->state >= Button1Mask)
1137 return;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001138 if (!b->has_xkb)
1139 update_xkb_state_from_core(b, enter_notify->state);
1140 output = x11_backend_find_output(b, enter_notify->event);
Derek Foreman99bfa642014-12-11 15:44:46 -06001141 if (!output)
1142 return;
1143
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07001144 weston_output_transform_coordinate(&output->base,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05001145 wl_fixed_from_int(enter_notify->event_x),
1146 wl_fixed_from_int(enter_notify->event_y), &x, &y);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001147
Giulio Camuffo954f1832014-10-11 18:27:30 +03001148 notify_pointer_focus(&b->core_seat, &output->base, x, y);
Kristian Høgsberg50065962013-03-27 15:14:07 -04001149
Giulio Camuffo954f1832014-10-11 18:27:30 +03001150 b->prev_x = x;
1151 b->prev_y = y;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001152}
1153
Kristian Høgsbergee724822011-04-21 14:51:44 -04001154static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001155x11_backend_next_event(struct x11_backend *b,
1156 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -04001157{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001158 if (mask & WL_EVENT_READABLE) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001159 *event = xcb_poll_for_event(b->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -04001160 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001161#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Giulio Camuffo954f1832014-10-11 18:27:30 +03001162 *event = xcb_poll_for_queued_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001163#else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001164 *event = xcb_poll_for_event(b->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -04001165#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -04001166 }
1167
1168 return *event != NULL;
1169}
1170
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001171static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001172x11_backend_handle_event(int fd, uint32_t mask, void *data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001173{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001174 struct x11_backend *b = data;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001175 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001176 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001177 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001178 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001179 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001180 xcb_keymap_notify_event_t *keymap_notify;
1181 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001182 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001183 xcb_atom_t atom;
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001184 xcb_window_t window;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001185 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001186 uint32_t i, set;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001187 uint8_t response_type;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001188 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001189
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001190 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -04001191 count = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001192 while (x11_backend_next_event(b, &event, mask)) {
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001193 response_type = event->response_type & ~0x80;
1194
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001195 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1196 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001197 /* Suppress key repeat events; this is only used if we
1198 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001199 key_release = (xcb_key_press_event_t *) prev;
1200 key_press = (xcb_key_press_event_t *) event;
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001201 if (response_type == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -07001202 key_release->time == key_press->time &&
1203 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001204 /* Don't deliver the held key release
1205 * event or the new key press event. */
1206 free(event);
1207 free(prev);
1208 prev = NULL;
1209 continue;
1210 } else {
1211 /* Deliver the held key release now
1212 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001213 * event below. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001214 update_xkb_state_from_core(b, key_release->state);
1215 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001216 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001217 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001218 WL_KEYBOARD_KEY_STATE_RELEASED,
1219 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001220 free(prev);
1221 prev = NULL;
1222 break;
1223 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001224
1225 case XCB_FOCUS_IN:
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001226 assert(response_type == XCB_KEYMAP_NOTIFY);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001227 keymap_notify = (xcb_keymap_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001228 b->keys.size = 0;
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001229 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
1230 set = keymap_notify->keys[i >> 3] &
1231 (1 << (i & 7));
1232 if (set) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001233 k = wl_array_add(&b->keys, sizeof *k);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001234 *k = i;
1235 }
1236 }
1237
Daniel Stoned6da09e2012-06-22 13:21:29 +01001238 /* Unfortunately the state only comes with the enter
1239 * event, rather than with the focus event. I'm not
1240 * sure of the exact semantics around it and whether
1241 * we can ensure that we get both? */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001242 notify_keyboard_focus_in(&b->core_seat, &b->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001243 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001244
1245 free(prev);
1246 prev = NULL;
1247 break;
1248
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001249 default:
1250 /* No previous event held */
1251 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001252 }
1253
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001254 switch (response_type) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001255 case XCB_KEY_PRESS:
1256 key_press = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001257 if (!b->has_xkb)
1258 update_xkb_state_from_core(b, key_press->state);
1259 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001260 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001261 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001262 WL_KEYBOARD_KEY_STATE_PRESSED,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001263 b->has_xkb ? STATE_UPDATE_NONE :
Daniel Stonee2faa122012-06-22 13:21:39 +01001264 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001265 break;
1266 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +01001267 /* If we don't have XKB, we need to use the lame
1268 * autorepeat detection above. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001269 if (!b->has_xkb) {
Daniel Stone3ee91e12012-06-22 13:21:36 +01001270 prev = event;
1271 break;
1272 }
1273 key_release = (xcb_key_press_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001274 notify_key(&b->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +01001275 weston_compositor_get_time(),
1276 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001277 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +01001278 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001279 break;
1280 case XCB_BUTTON_PRESS:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001281 x11_backend_deliver_button_event(b, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001282 break;
1283 case XCB_BUTTON_RELEASE:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001284 x11_backend_deliver_button_event(b, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001285 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001286 case XCB_MOTION_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001287 x11_backend_deliver_motion_event(b, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001288 break;
1289
1290 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001291 expose = (xcb_expose_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001292 output = x11_backend_find_output(b, expose->window);
Derek Foreman99bfa642014-12-11 15:44:46 -06001293 if (!output)
1294 break;
1295
Kristian Høgsberga98c2e12013-12-05 12:30:37 -08001296 weston_output_damage(&output->base);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001297 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001298 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001299
1300 case XCB_ENTER_NOTIFY:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001301 x11_backend_deliver_enter_event(b, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001302 break;
1303
1304 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001305 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -05001306 if (enter_notify->state >= Button1Mask)
1307 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001308 if (!b->has_xkb)
1309 update_xkb_state_from_core(b, enter_notify->state);
1310 notify_pointer_focus(&b->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001311 break;
1312
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001313 case XCB_CLIENT_MESSAGE:
1314 client_message = (xcb_client_message_event_t *) event;
1315 atom = client_message->data.data32[0];
Ander Conselvan de Oliveiraf54fa4d2013-12-13 22:10:49 +02001316 window = client_message->window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001317 if (atom == b->atom.wm_delete_window) {
Derek Foremand540f4b2015-01-27 16:26:49 -06001318 struct wl_event_loop *loop;
1319 struct window_delete_data *data = malloc(sizeof *data);
1320
1321 /* if malloc failed we should at least try to
1322 * delete the window, even if it may result in
1323 * a crash.
1324 */
1325 if (!data) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001326 x11_backend_delete_window(b, window);
Derek Foremand540f4b2015-01-27 16:26:49 -06001327 break;
1328 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001329 data->backend = b;
Derek Foremand540f4b2015-01-27 16:26:49 -06001330 data->window = window;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001331 loop = wl_display_get_event_loop(b->compositor->wl_display);
Derek Foremand540f4b2015-01-27 16:26:49 -06001332 wl_event_loop_add_idle(loop, delete_cb, data);
1333 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001334 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001335
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001336 case XCB_FOCUS_IN:
1337 focus_in = (xcb_focus_in_event_t *) event;
1338 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1339 break;
1340
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001341 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001342 break;
1343
1344 case XCB_FOCUS_OUT:
1345 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -05001346 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1347 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001348 break;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001349 notify_keyboard_focus_out(&b->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001350 break;
1351
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001352 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001353 break;
1354 }
1355
Daniel Stonee2faa122012-06-22 13:21:39 +01001356#ifdef HAVE_XCB_XKB
Giulio Camuffo954f1832014-10-11 18:27:30 +03001357 if (b->has_xkb) {
1358 if (response_type == b->xkb_event_base) {
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001359 xcb_xkb_state_notify_event_t *state =
1360 (xcb_xkb_state_notify_event_t *) event;
1361 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001362 update_xkb_state(b, state);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001363 } else if (response_type == XCB_PROPERTY_NOTIFY) {
1364 xcb_property_notify_event_t *prop_notify =
1365 (xcb_property_notify_event_t *) event;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001366 if (prop_notify->window == b->screen->root &&
1367 prop_notify->atom == b->atom.xkb_names &&
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001368 prop_notify->state == XCB_PROPERTY_NEW_VALUE)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001369 update_xkb_keymap(b);
Rui Matosc6c2f8e2013-10-10 19:44:20 +02001370 }
Daniel Stonee2faa122012-06-22 13:21:39 +01001371 }
1372#endif
1373
Bill Spitzakb715cec2012-06-05 17:08:23 -04001374 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001375 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001376 free (event);
1377 }
1378
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001379 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1380 case XCB_KEY_RELEASE:
1381 key_release = (xcb_key_press_event_t *) prev;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001382 update_xkb_state_from_core(b, key_release->state);
1383 notify_key(&b->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001384 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001385 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001386 WL_KEYBOARD_KEY_STATE_RELEASED,
1387 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001388 free(prev);
1389 prev = NULL;
1390 break;
1391 default:
1392 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001393 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001394
Bill Spitzakb715cec2012-06-05 17:08:23 -04001395 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001396}
1397
Giulio Camuffo954f1832014-10-11 18:27:30 +03001398#define F(field) offsetof(struct x11_backend, field)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001399
1400static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001401x11_backend_get_resources(struct x11_backend *b)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001402{
1403 static const struct { const char *name; int offset; } atoms[] = {
1404 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1405 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1406 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1407 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001408 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001409 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001410 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001411 { "_NET_WM_STATE", F(atom.net_wm_state) },
1412 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001413 { "_NET_SUPPORTING_WM_CHECK",
1414 F(atom.net_supporting_wm_check) },
1415 { "_NET_SUPPORTED", F(atom.net_supported) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001416 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001417 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001418 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001419 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001420 };
1421
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001422 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001423 xcb_intern_atom_reply_t *reply;
1424 xcb_pixmap_t pixmap;
1425 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001426 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001427 uint8_t data[] = { 0, 0, 0, 0 };
1428
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001429 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001430 cookies[i] = xcb_intern_atom (b->conn, 0,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001431 strlen(atoms[i].name),
1432 atoms[i].name);
1433
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001434 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001435 reply = xcb_intern_atom_reply (b->conn, cookies[i], NULL);
1436 *(xcb_atom_t *) ((char *) b + atoms[i].offset) = reply->atom;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001437 free(reply);
1438 }
1439
Giulio Camuffo954f1832014-10-11 18:27:30 +03001440 pixmap = xcb_generate_id(b->conn);
1441 gc = xcb_generate_id(b->conn);
1442 xcb_create_pixmap(b->conn, 1, pixmap, b->screen->root, 1, 1);
1443 xcb_create_gc(b->conn, gc, pixmap, 0, NULL);
1444 xcb_put_image(b->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001445 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001446 b->null_cursor = xcb_generate_id(b->conn);
1447 xcb_create_cursor (b->conn, b->null_cursor,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001448 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001449 xcb_free_gc(b->conn, gc);
1450 xcb_free_pixmap(b->conn, pixmap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001451}
1452
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001453static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001454x11_backend_get_wm_info(struct x11_backend *c)
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001455{
1456 xcb_get_property_cookie_t cookie;
1457 xcb_get_property_reply_t *reply;
1458 xcb_atom_t *atom;
1459 unsigned int i;
1460
1461 cookie = xcb_get_property(c->conn, 0, c->screen->root,
1462 c->atom.net_supported,
1463 XCB_ATOM_ATOM, 0, 1024);
1464 reply = xcb_get_property_reply(c->conn, cookie, NULL);
1465 if (reply == NULL)
1466 return;
1467
1468 atom = (xcb_atom_t *) xcb_get_property_value(reply);
1469
1470 for (i = 0; i < reply->value_len; i++) {
1471 if (atom[i] == c->atom.net_wm_state_fullscreen)
1472 c->has_net_wm_state_fullscreen = 1;
1473 }
Ryo Munakata76fb7ec2015-03-08 19:17:06 +09001474
1475 free(reply);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001476}
1477
1478static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001479x11_restore(struct weston_compositor *ec)
1480{
1481}
1482
1483static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001484x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001485{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001486 struct x11_backend *backend = (struct x11_backend *)ec->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -07001487
Giulio Camuffo954f1832014-10-11 18:27:30 +03001488 wl_event_source_remove(backend->xcb_source);
1489 x11_input_destroy(backend);
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001490
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03001491 weston_compositor_shutdown(ec); /* destroys outputs, too */
1492
Giulio Camuffo954f1832014-10-11 18:27:30 +03001493 XCloseDisplay(backend->dpy);
1494 free(backend);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001495}
1496
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001497static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001498init_gl_renderer(struct x11_backend *b)
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001499{
1500 int ret;
1501
1502 gl_renderer = weston_load_module("gl-renderer.so",
1503 "gl_renderer_interface");
1504 if (!gl_renderer)
1505 return -1;
1506
Giulio Camuffo954f1832014-10-11 18:27:30 +03001507 ret = gl_renderer->create(b->compositor, EGL_PLATFORM_X11_KHR, (void *) b->dpy,
Derek Foremane76f1852015-05-15 12:12:39 -05001508 gl_renderer->opaque_attribs, NULL, 0);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001509
1510 return ret;
1511}
Giulio Camuffo954f1832014-10-11 18:27:30 +03001512static struct x11_backend *
1513x11_backend_create(struct weston_compositor *compositor,
1514 int fullscreen,
1515 int no_input,
1516 int use_pixman,
1517 int *argc, char *argv[],
1518 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001519{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001520 struct x11_backend *b;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001521 struct x11_output *output;
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001522 struct weston_config_section *section;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001523 xcb_screen_iterator_t s;
Scott Moreau248aaec2012-08-03 14:19:52 -06001524 int i, x = 0, output_count = 0;
Jason Ekstrandd89a0942014-03-07 15:29:14 -06001525 int width, height, scale, count;
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001526 const char *section_name;
1527 char *name, *t, *mode;
1528 uint32_t transform;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001529
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001530 weston_log("initializing x11 backend\n");
1531
Giulio Camuffo954f1832014-10-11 18:27:30 +03001532 b = zalloc(sizeof *b);
1533 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001534 return NULL;
1535
Giulio Camuffo954f1832014-10-11 18:27:30 +03001536 b->compositor = compositor;
1537 if (weston_compositor_init(compositor, argc, argv, config) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001538 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001539
Giulio Camuffo954f1832014-10-11 18:27:30 +03001540 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001541 goto err_free;
1542
Giulio Camuffo954f1832014-10-11 18:27:30 +03001543 b->dpy = XOpenDisplay(NULL);
1544 if (b->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001545 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001546
Giulio Camuffo954f1832014-10-11 18:27:30 +03001547 b->conn = XGetXCBConnection(b->dpy);
1548 XSetEventQueueOwner(b->dpy, XCBOwnsEventQueue);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001549
Giulio Camuffo954f1832014-10-11 18:27:30 +03001550 if (xcb_connection_has_error(b->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001551 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001552
Giulio Camuffo954f1832014-10-11 18:27:30 +03001553 s = xcb_setup_roots_iterator(xcb_get_setup(b->conn));
1554 b->screen = s.data;
1555 wl_array_init(&b->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001556
Giulio Camuffo954f1832014-10-11 18:27:30 +03001557 x11_backend_get_resources(b);
1558 x11_backend_get_wm_info(b);
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001559
Giulio Camuffo954f1832014-10-11 18:27:30 +03001560 if (!b->has_net_wm_state_fullscreen && fullscreen) {
Kristian Høgsberg7cc3d842013-02-19 21:19:04 -05001561 weston_log("Can not fullscreen without window manager support"
1562 "(need _NET_WM_STATE_FULLSCREEN)\n");
1563 fullscreen = 0;
1564 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001565
Giulio Camuffo954f1832014-10-11 18:27:30 +03001566 b->use_pixman = use_pixman;
1567 if (b->use_pixman) {
1568 if (pixman_renderer_init(compositor) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001569 weston_log("Failed to initialize pixman renderer for X11 backend\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001570 goto err_xdisplay;
Bryce Harrington665b0252015-05-26 18:14:21 -07001571 }
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001572 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03001573 else if (init_gl_renderer(b) < 0) {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001574 goto err_xdisplay;
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001575 }
Vasily Khoruzhickd8943512013-01-07 22:36:02 +03001576 weston_log("Using %s renderer\n", use_pixman ? "pixman" : "gl");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001577
Giulio Camuffo954f1832014-10-11 18:27:30 +03001578 b->base.destroy = x11_destroy;
1579 b->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001580
Giulio Camuffo954f1832014-10-11 18:27:30 +03001581 if (x11_input_create(b, no_input) < 0) {
Bryce Harrington665b0252015-05-26 18:14:21 -07001582 weston_log("Failed to create X11 input\n");
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001583 goto err_renderer;
Bryce Harrington665b0252015-05-26 18:14:21 -07001584 }
Daniel Stone22815f92012-06-22 13:21:34 +01001585
Scott Moreau248aaec2012-08-03 14:19:52 -06001586 width = option_width ? option_width : 1024;
1587 height = option_height ? option_height : 640;
Jason Ekstrandd89a0942014-03-07 15:29:14 -06001588 scale = option_scale ? option_scale : 1;
Scott Moreau248aaec2012-08-03 14:19:52 -06001589 count = option_count ? option_count : 1;
1590
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001591 section = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001592 while (weston_config_next_section(config,
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001593 &section, &section_name)) {
1594 if (strcmp(section_name, "output") != 0)
1595 continue;
1596 weston_config_section_get_string(section, "name", &name, NULL);
Rob Bradford5c4245d2013-07-26 16:29:44 +01001597 if (name == NULL || name[0] != 'X') {
1598 free(name);
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001599 continue;
Rob Bradford5c4245d2013-07-26 16:29:44 +01001600 }
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001601
1602 weston_config_section_get_string(section,
1603 "mode", &mode, "1024x600");
1604 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1605 weston_log("Invalid mode \"%s\" for output %s\n",
1606 mode, name);
1607 width = 1024;
1608 height = 600;
1609 }
1610 free(mode);
1611
1612 if (option_width)
1613 width = option_width;
1614 if (option_height)
1615 height = option_height;
1616
1617 weston_config_section_get_int(section, "scale", &scale, 1);
Kristian Høgsberg3c2e66b2014-03-10 13:41:35 -07001618 if (option_scale)
1619 scale = option_scale;
1620
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001621 weston_config_section_get_string(section,
1622 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001623 if (weston_parse_transform(t, &transform) < 0)
1624 weston_log("Invalid transform \"%s\" for output %s\n",
1625 t, name);
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001626 free(t);
1627
Giulio Camuffo954f1832014-10-11 18:27:30 +03001628 output = x11_backend_create_output(b, x, 0,
1629 width, height,
1630 fullscreen, no_input,
1631 name, transform, scale);
Kristian Høgsberg8f6fcbf2013-05-26 21:21:23 -04001632 free(name);
Bryce Harrington665b0252015-05-26 18:14:21 -07001633 if (output == NULL) {
1634 weston_log("Failed to create configured x11 output\n");
Scott Moreau248aaec2012-08-03 14:19:52 -06001635 goto err_x11_input;
Bryce Harrington665b0252015-05-26 18:14:21 -07001636 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06001637
1638 x = pixman_region32_extents(&output->base.region)->x2;
1639
Scott Moreau248aaec2012-08-03 14:19:52 -06001640 output_count++;
1641 if (option_count && output_count >= option_count)
1642 break;
1643 }
1644
1645 for (i = output_count; i < count; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001646 output = x11_backend_create_output(b, x, 0, width, height,
1647 fullscreen, no_input, NULL,
1648 WL_OUTPUT_TRANSFORM_NORMAL, scale);
Bryce Harrington665b0252015-05-26 18:14:21 -07001649 if (output == NULL) {
1650 weston_log("Failed to create x11 output #%d\n", i);
Martin Olsson11434bb2012-07-08 03:03:44 +02001651 goto err_x11_input;
Bryce Harrington665b0252015-05-26 18:14:21 -07001652 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06001653 x = pixman_region32_extents(&output->base.region)->x2;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001654 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001655
Giulio Camuffo954f1832014-10-11 18:27:30 +03001656 b->xcb_source =
1657 wl_event_loop_add_fd(compositor->input_loop,
1658 xcb_get_file_descriptor(b->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001659 WL_EVENT_READABLE,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001660 x11_backend_handle_event, b);
1661 wl_event_source_check(b->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001662
Giulio Camuffo954f1832014-10-11 18:27:30 +03001663 compositor->backend = &b->base;
1664 return b;
Martin Olsson11434bb2012-07-08 03:03:44 +02001665
1666err_x11_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001667 x11_input_destroy(b);
Vasily Khoruzhickb3add192013-01-07 20:39:50 +03001668err_renderer:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001669 compositor->renderer->destroy(compositor);
Martin Olsson11434bb2012-07-08 03:03:44 +02001670err_xdisplay:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001671 XCloseDisplay(b->dpy);
Martin Olsson11434bb2012-07-08 03:03:44 +02001672err_free:
Giulio Camuffo954f1832014-10-11 18:27:30 +03001673 free(b);
Martin Olsson11434bb2012-07-08 03:03:44 +02001674 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001675}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001676
Giulio Camuffo954f1832014-10-11 18:27:30 +03001677WL_EXPORT int
1678backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001679 struct weston_config *config)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001680{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001681 struct x11_backend *b;
Scott Moreau248aaec2012-08-03 14:19:52 -06001682 int fullscreen = 0;
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001683 int no_input = 0;
Vasily Khoruzhickd8943512013-01-07 22:36:02 +03001684 int use_pixman = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001685
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001686 const struct weston_option x11_options[] = {
Scott Moreau248aaec2012-08-03 14:19:52 -06001687 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1688 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
Jason Ekstrandd89a0942014-03-07 15:29:14 -06001689 { WESTON_OPTION_INTEGER, "scale", 0, &option_scale },
Kristian Høgsbergd6f09a72012-11-09 11:12:21 -05001690 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &fullscreen },
Scott Moreau248aaec2012-08-03 14:19:52 -06001691 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001692 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
Vasily Khoruzhickd8943512013-01-07 22:36:02 +03001693 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001694 };
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001695
1696 parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001697
Giulio Camuffo954f1832014-10-11 18:27:30 +03001698 b = x11_backend_create(compositor,
1699 fullscreen,
1700 no_input,
1701 use_pixman,
1702 argc, argv, config);
1703 if (b == NULL)
1704 return -1;
1705 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001706}