1 /*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2010-2011 Intel Corporation
4 * Copyright © 2013 Vasily Khoruzhick <anarsoul@gmail.com>
5 *
6 * 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:
13 *
14 * 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.
26 */
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <stddef.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <sys/time.h>
39 #include <sys/shm.h>
40 #include <linux/input.h>
41
42 #include <drm_fourcc.h>
43 #include <xcb/xcb.h>
44 #include <xcb/shm.h>
45 #ifdef HAVE_XCB_XKB
46 #include <xcb/xkb.h>
47 #endif
48
49 #include <X11/Xlib.h>
50 #include <X11/Xlib-xcb.h>
51
52 #include <xkbcommon/xkbcommon.h>
53
54 #include <libweston/libweston.h>
55 #include <libweston/backend-x11.h>
56 #include "shared/helpers.h"
57 #include "shared/image-loader.h"
58 #include "shared/timespec-util.h"
59 #include "shared/file-util.h"
60 #include "renderer-gl/gl-renderer.h"
61 #include "shared/weston-egl-ext.h"
62 #include "pixman-renderer.h"
63 #include "presentation-time-server-protocol.h"
64 #include "linux-dmabuf.h"
65 #include "linux-explicit-synchronization.h"
66 #include <libweston/windowed-output-api.h>
67
68 #define DEFAULT_AXIS_STEP_DISTANCE 10
69
70 #define WINDOW_MIN_WIDTH 128
71 #define WINDOW_MIN_HEIGHT 128
72
73 #define WINDOW_MAX_WIDTH 8192
74 #define WINDOW_MAX_HEIGHT 8192
75
76 static const uint32_t x11_formats[] = {
77 DRM_FORMAT_XRGB8888,
78 };
79
80 struct x11_backend {
81 struct weston_backend base;
82 struct weston_compositor *compositor;
83
84 Display *dpy;
85 xcb_connection_t *conn;
86 xcb_screen_t *screen;
87 xcb_cursor_t null_cursor;
88 struct wl_array keys;
89 struct wl_event_source *xcb_source;
90 struct xkb_keymap *xkb_keymap;
91 unsigned int has_xkb;
92 uint8_t xkb_event_base;
93 int fullscreen;
94 int no_input;
95 int use_pixman;
96
97 int has_net_wm_state_fullscreen;
98
99 /* We could map multi-pointer X to multiple wayland seats, but
100 * for now we only support core X input. */
101 struct weston_seat core_seat;
102 double prev_x;
103 double prev_y;
104
105 struct {
106 xcb_atom_t wm_protocols;
107 xcb_atom_t wm_normal_hints;
108 xcb_atom_t wm_size_hints;
109 xcb_atom_t wm_delete_window;
110 xcb_atom_t wm_class;
111 xcb_atom_t net_wm_name;
112 xcb_atom_t net_supporting_wm_check;
113 xcb_atom_t net_supported;
114 xcb_atom_t net_wm_icon;
115 xcb_atom_t net_wm_state;
116 xcb_atom_t net_wm_state_fullscreen;
117 xcb_atom_t string;
118 xcb_atom_t utf8_string;
119 xcb_atom_t cardinal;
120 xcb_atom_t xkb_names;
121 } atom;
122 };
123
124 struct x11_head {
125 struct weston_head base;
126 };
127
128 struct x11_output {
129 struct weston_output base;
130
131 xcb_window_t window;
132 struct weston_mode mode;
133 struct weston_mode native;
134 struct wl_event_source *finish_frame_timer;
135
136 xcb_gc_t gc;
137 xcb_shm_seg_t segment;
138 pixman_image_t *hw_surface;
139 int shm_id;
140 void *buf;
141 uint8_t depth;
142 int32_t scale;
143 bool resize_pending;
144 bool window_resized;
145 };
146
147 struct window_delete_data {
148 struct x11_backend *backend;
149 xcb_window_t window;
150 };
151
152 struct gl_renderer_interface *gl_renderer;
153
154 static inline struct x11_head *
to_x11_head(struct weston_head * base)155 to_x11_head(struct weston_head *base)
156 {
157 return container_of(base, struct x11_head, base);
158 }
159
160 static inline struct x11_output *
to_x11_output(struct weston_output * base)161 to_x11_output(struct weston_output *base)
162 {
163 return container_of(base, struct x11_output, base);
164 }
165
166 static inline struct x11_backend *
to_x11_backend(struct weston_compositor * base)167 to_x11_backend(struct weston_compositor *base)
168 {
169 return container_of(base->backend, struct x11_backend, base);
170 }
171
172 static xcb_screen_t *
x11_compositor_get_default_screen(struct x11_backend * b)173 x11_compositor_get_default_screen(struct x11_backend *b)
174 {
175 xcb_screen_iterator_t iter;
176 int i, screen_nbr = XDefaultScreen(b->dpy);
177
178 iter = xcb_setup_roots_iterator(xcb_get_setup(b->conn));
179 for (i = 0; iter.rem; xcb_screen_next(&iter), i++)
180 if (i == screen_nbr)
181 return iter.data;
182
183 return xcb_setup_roots_iterator(xcb_get_setup(b->conn)).data;
184 }
185
186 static struct xkb_keymap *
x11_backend_get_keymap(struct x11_backend * b)187 x11_backend_get_keymap(struct x11_backend *b)
188 {
189 xcb_get_property_cookie_t cookie;
190 xcb_get_property_reply_t *reply;
191 struct xkb_rule_names names;
192 struct xkb_keymap *ret;
193 const char *value_all, *value_part;
194 int length_all, length_part;
195
196 memset(&names, 0, sizeof(names));
197
198 cookie = xcb_get_property(b->conn, 0, b->screen->root,
199 b->atom.xkb_names, b->atom.string, 0, 1024);
200 reply = xcb_get_property_reply(b->conn, cookie, NULL);
201 if (reply == NULL)
202 return NULL;
203
204 value_all = xcb_get_property_value(reply);
205 length_all = xcb_get_property_value_length(reply);
206 value_part = value_all;
207
208 #define copy_prop_value(to) \
209 length_part = strlen(value_part); \
210 if (value_part + length_part < (value_all + length_all) && \
211 length_part > 0) \
212 names.to = value_part; \
213 value_part += length_part + 1;
214
215 copy_prop_value(rules);
216 copy_prop_value(model);
217 copy_prop_value(layout);
218 copy_prop_value(variant);
219 copy_prop_value(options);
220 #undef copy_prop_value
221
222 ret = xkb_keymap_new_from_names(b->compositor->xkb_context, &names, 0);
223
224 free(reply);
225 return ret;
226 }
227
228 static uint32_t
get_xkb_mod_mask(struct x11_backend * b,uint32_t in)229 get_xkb_mod_mask(struct x11_backend *b, uint32_t in)
230 {
231 struct weston_keyboard *keyboard =
232 weston_seat_get_keyboard(&b->core_seat);
233 struct weston_xkb_info *info = keyboard->xkb_info;
234 uint32_t ret = 0;
235
236 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
237 ret |= (1 << info->shift_mod);
238 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
239 ret |= (1 << info->caps_mod);
240 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
241 ret |= (1 << info->ctrl_mod);
242 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
243 ret |= (1 << info->alt_mod);
244 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
245 ret |= (1 << info->mod2_mod);
246 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
247 ret |= (1 << info->mod3_mod);
248 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
249 ret |= (1 << info->super_mod);
250 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
251 ret |= (1 << info->mod5_mod);
252
253 return ret;
254 }
255
256 static void
x11_backend_setup_xkb(struct x11_backend * b)257 x11_backend_setup_xkb(struct x11_backend *b)
258 {
259 #ifndef HAVE_XCB_XKB
260 weston_log("XCB-XKB not available during build\n");
261 b->has_xkb = 0;
262 b->xkb_event_base = 0;
263 return;
264 #else
265 struct weston_keyboard *keyboard;
266 const xcb_query_extension_reply_t *ext;
267 xcb_generic_error_t *error;
268 xcb_void_cookie_t select;
269 xcb_xkb_use_extension_cookie_t use_ext;
270 xcb_xkb_use_extension_reply_t *use_ext_reply;
271 xcb_xkb_per_client_flags_cookie_t pcf;
272 xcb_xkb_per_client_flags_reply_t *pcf_reply;
273 xcb_xkb_get_state_cookie_t state;
274 xcb_xkb_get_state_reply_t *state_reply;
275 uint32_t values[1] = { XCB_EVENT_MASK_PROPERTY_CHANGE };
276
277 b->has_xkb = 0;
278 b->xkb_event_base = 0;
279
280 ext = xcb_get_extension_data(b->conn, &xcb_xkb_id);
281 if (!ext) {
282 weston_log("XKB extension not available on host X11 server\n");
283 return;
284 }
285 b->xkb_event_base = ext->first_event;
286
287 select = xcb_xkb_select_events_checked(b->conn,
288 XCB_XKB_ID_USE_CORE_KBD,
289 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
290 0,
291 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
292 0,
293 0,
294 NULL);
295 error = xcb_request_check(b->conn, select);
296 if (error) {
297 weston_log("error: failed to select for XKB state events\n");
298 free(error);
299 return;
300 }
301
302 use_ext = xcb_xkb_use_extension(b->conn,
303 XCB_XKB_MAJOR_VERSION,
304 XCB_XKB_MINOR_VERSION);
305 use_ext_reply = xcb_xkb_use_extension_reply(b->conn, use_ext, NULL);
306 if (!use_ext_reply) {
307 weston_log("couldn't start using XKB extension\n");
308 return;
309 }
310
311 if (!use_ext_reply->supported) {
312 weston_log("XKB extension version on the server is too old "
313 "(want %d.%d, has %d.%d)\n",
314 XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION,
315 use_ext_reply->serverMajor, use_ext_reply->serverMinor);
316 free(use_ext_reply);
317 return;
318 }
319 free(use_ext_reply);
320
321 pcf = xcb_xkb_per_client_flags(b->conn,
322 XCB_XKB_ID_USE_CORE_KBD,
323 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
324 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
325 0,
326 0,
327 0);
328 pcf_reply = xcb_xkb_per_client_flags_reply(b->conn, pcf, NULL);
329 if (!pcf_reply ||
330 !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT)) {
331 weston_log("failed to set XKB per-client flags, not using "
332 "detectable repeat\n");
333 free(pcf_reply);
334 return;
335 }
336 free(pcf_reply);
337
338 state = xcb_xkb_get_state(b->conn, XCB_XKB_ID_USE_CORE_KBD);
339 state_reply = xcb_xkb_get_state_reply(b->conn, state, NULL);
340 if (!state_reply) {
341 weston_log("failed to get initial XKB state\n");
342 return;
343 }
344
345 keyboard = weston_seat_get_keyboard(&b->core_seat);
346 xkb_state_update_mask(keyboard->xkb_state.state,
347 get_xkb_mod_mask(b, state_reply->baseMods),
348 get_xkb_mod_mask(b, state_reply->latchedMods),
349 get_xkb_mod_mask(b, state_reply->lockedMods),
350 0,
351 0,
352 state_reply->group);
353
354 free(state_reply);
355
356 xcb_change_window_attributes(b->conn, b->screen->root,
357 XCB_CW_EVENT_MASK, values);
358
359 b->has_xkb = 1;
360 #endif
361 }
362
363 #ifdef HAVE_XCB_XKB
364 static void
update_xkb_keymap(struct x11_backend * b)365 update_xkb_keymap(struct x11_backend *b)
366 {
367 struct xkb_keymap *keymap;
368
369 keymap = x11_backend_get_keymap(b);
370 if (!keymap) {
371 weston_log("failed to get XKB keymap\n");
372 return;
373 }
374 weston_seat_update_keymap(&b->core_seat, keymap);
375 xkb_keymap_unref(keymap);
376 }
377 #endif
378
379 static int
x11_input_create(struct x11_backend * b,int no_input)380 x11_input_create(struct x11_backend *b, int no_input)
381 {
382 struct xkb_keymap *keymap;
383
384 weston_seat_init(&b->core_seat, b->compositor, "default");
385
386 if (no_input)
387 return 0;
388
389 weston_seat_init_pointer(&b->core_seat);
390
391 keymap = x11_backend_get_keymap(b);
392 if (weston_seat_init_keyboard(&b->core_seat, keymap) < 0)
393 return -1;
394 xkb_keymap_unref(keymap);
395
396 x11_backend_setup_xkb(b);
397
398 return 0;
399 }
400
401 static void
x11_input_destroy(struct x11_backend * b)402 x11_input_destroy(struct x11_backend *b)
403 {
404 weston_seat_release(&b->core_seat);
405 }
406
407 static int
x11_output_start_repaint_loop(struct weston_output * output)408 x11_output_start_repaint_loop(struct weston_output *output)
409 {
410 struct timespec ts;
411
412 weston_compositor_read_presentation_clock(output->compositor, &ts);
413 weston_output_finish_frame(output, &ts, WP_PRESENTATION_FEEDBACK_INVALID);
414
415 return 0;
416 }
417
418 static int
x11_output_repaint_gl(struct weston_output * output_base,pixman_region32_t * damage,void * repaint_data)419 x11_output_repaint_gl(struct weston_output *output_base,
420 pixman_region32_t *damage,
421 void *repaint_data)
422 {
423 struct x11_output *output = to_x11_output(output_base);
424 struct weston_compositor *ec = output->base.compositor;
425
426 ec->renderer->repaint_output(output_base, damage);
427
428 pixman_region32_subtract(&ec->primary_plane.damage,
429 &ec->primary_plane.damage, damage);
430
431 wl_event_source_timer_update(output->finish_frame_timer, 10);
432 return 0;
433 }
434
435 static void
set_clip_for_output(struct weston_output * output_base,pixman_region32_t * region)436 set_clip_for_output(struct weston_output *output_base, pixman_region32_t *region)
437 {
438 struct x11_output *output = to_x11_output(output_base);
439 struct weston_compositor *ec = output->base.compositor;
440 struct x11_backend *b = to_x11_backend(ec);
441 pixman_region32_t transformed_region;
442 pixman_box32_t *rects;
443 xcb_rectangle_t *output_rects;
444 xcb_void_cookie_t cookie;
445 int nrects, i;
446 xcb_generic_error_t *err;
447
448 pixman_region32_init(&transformed_region);
449 pixman_region32_copy(&transformed_region, region);
450 pixman_region32_translate(&transformed_region,
451 -output_base->x, -output_base->y);
452 weston_transformed_region(output_base->width, output_base->height,
453 output_base->transform,
454 output_base->current_scale,
455 &transformed_region, &transformed_region);
456
457 rects = pixman_region32_rectangles(&transformed_region, &nrects);
458 output_rects = calloc(nrects, sizeof(xcb_rectangle_t));
459
460 if (output_rects == NULL) {
461 pixman_region32_fini(&transformed_region);
462 return;
463 }
464
465 for (i = 0; i < nrects; i++) {
466 output_rects[i].x = rects[i].x1;
467 output_rects[i].y = rects[i].y1;
468 output_rects[i].width = rects[i].x2 - rects[i].x1;
469 output_rects[i].height = rects[i].y2 - rects[i].y1;
470 }
471
472 pixman_region32_fini(&transformed_region);
473
474 cookie = xcb_set_clip_rectangles_checked(b->conn, XCB_CLIP_ORDERING_UNSORTED,
475 output->gc,
476 0, 0, nrects,
477 output_rects);
478 err = xcb_request_check(b->conn, cookie);
479 if (err != NULL) {
480 weston_log("Failed to set clip rects, err: %d\n", err->error_code);
481 free(err);
482 }
483 free(output_rects);
484 }
485
486
487 static int
x11_output_repaint_shm(struct weston_output * output_base,pixman_region32_t * damage,void * repaint_data)488 x11_output_repaint_shm(struct weston_output *output_base,
489 pixman_region32_t *damage,
490 void *repaint_data)
491 {
492 struct x11_output *output = to_x11_output(output_base);
493 struct weston_compositor *ec = output->base.compositor;
494 struct x11_backend *b = to_x11_backend(ec);
495 xcb_void_cookie_t cookie;
496 xcb_generic_error_t *err;
497
498 pixman_renderer_output_set_buffer(output_base, output->hw_surface);
499 ec->renderer->repaint_output(output_base, damage);
500
501 pixman_region32_subtract(&ec->primary_plane.damage,
502 &ec->primary_plane.damage, damage);
503 set_clip_for_output(output_base, damage);
504 cookie = xcb_shm_put_image_checked(b->conn, output->window, output->gc,
505 pixman_image_get_width(output->hw_surface),
506 pixman_image_get_height(output->hw_surface),
507 0, 0,
508 pixman_image_get_width(output->hw_surface),
509 pixman_image_get_height(output->hw_surface),
510 0, 0, output->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
511 0, output->segment, 0);
512 err = xcb_request_check(b->conn, cookie);
513 if (err != NULL) {
514 weston_log("Failed to put shm image, err: %d\n", err->error_code);
515 free(err);
516 }
517
518 wl_event_source_timer_update(output->finish_frame_timer, 10);
519 return 0;
520 }
521
522 static int
finish_frame_handler(void * data)523 finish_frame_handler(void *data)
524 {
525 struct x11_output *output = data;
526 struct timespec ts;
527
528 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
529 weston_output_finish_frame(&output->base, &ts, 0);
530
531 return 1;
532 }
533
534 static void
x11_output_deinit_shm(struct x11_backend * b,struct x11_output * output)535 x11_output_deinit_shm(struct x11_backend *b, struct x11_output *output)
536 {
537 xcb_void_cookie_t cookie;
538 xcb_generic_error_t *err;
539 xcb_free_gc(b->conn, output->gc);
540
541 pixman_image_unref(output->hw_surface);
542 output->hw_surface = NULL;
543 cookie = xcb_shm_detach_checked(b->conn, output->segment);
544 err = xcb_request_check(b->conn, cookie);
545 if (err) {
546 weston_log("xcb_shm_detach failed, error %d\n", err->error_code);
547 free(err);
548 }
549 shmdt(output->buf);
550 }
551
552 static void
x11_output_set_wm_protocols(struct x11_backend * b,struct x11_output * output)553 x11_output_set_wm_protocols(struct x11_backend *b,
554 struct x11_output *output)
555 {
556 xcb_atom_t list[1];
557
558 list[0] = b->atom.wm_delete_window;
559 xcb_change_property (b->conn,
560 XCB_PROP_MODE_REPLACE,
561 output->window,
562 b->atom.wm_protocols,
563 XCB_ATOM_ATOM,
564 32,
565 ARRAY_LENGTH(list),
566 list);
567 }
568
569 struct wm_normal_hints {
570 uint32_t flags;
571 uint32_t pad[4];
572 int32_t min_width, min_height;
573 int32_t max_width, max_height;
574 int32_t width_inc, height_inc;
575 int32_t min_aspect_x, min_aspect_y;
576 int32_t max_aspect_x, max_aspect_y;
577 int32_t base_width, base_height;
578 int32_t win_gravity;
579 };
580
581 #define WM_NORMAL_HINTS_MIN_SIZE 16
582 #define WM_NORMAL_HINTS_MAX_SIZE 32
583
584 static void
x11_output_set_icon(struct x11_backend * b,struct x11_output * output,const char * filename)585 x11_output_set_icon(struct x11_backend *b,
586 struct x11_output *output, const char *filename)
587 {
588 uint32_t *icon;
589 int32_t width, height;
590 pixman_image_t *image;
591
592 image = load_image(filename);
593 if (!image)
594 return;
595 width = pixman_image_get_width(image);
596 height = pixman_image_get_height(image);
597 icon = malloc(width * height * 4 + 8);
598 if (!icon) {
599 pixman_image_unref(image);
600 return;
601 }
602
603 icon[0] = width;
604 icon[1] = height;
605 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
606 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
607 b->atom.net_wm_icon, b->atom.cardinal, 32,
608 width * height + 2, icon);
609 free(icon);
610 pixman_image_unref(image);
611 }
612
613 static void
x11_output_wait_for_map(struct x11_backend * b,struct x11_output * output)614 x11_output_wait_for_map(struct x11_backend *b, struct x11_output *output)
615 {
616 xcb_map_notify_event_t *map_notify;
617 xcb_configure_notify_event_t *configure_notify;
618 xcb_generic_event_t *event;
619 int mapped = 0, configured = 0;
620 uint8_t response_type;
621
622 /* This isn't the nicest way to do this. Ideally, we could
623 * just go back to the main loop and once we get the configure
624 * notify, we add the output to the compositor. While we do
625 * support output hotplug, we can't start up with no outputs.
626 * We could add the output and then resize once we get the
627 * configure notify, but we don't want to start up and
628 * immediately resize the output.
629 *
630 * Also, some window managers don't give us our final
631 * fullscreen size before map_notify, so if we don't get a
632 * configure_notify before map_notify, we just wait for the
633 * first one and hope that's our size. */
634
635 xcb_flush(b->conn);
636
637 while (!mapped || !configured) {
638 event = xcb_wait_for_event(b->conn);
639 response_type = event->response_type & ~0x80;
640
641 switch (response_type) {
642 case XCB_MAP_NOTIFY:
643 map_notify = (xcb_map_notify_event_t *) event;
644 if (map_notify->window == output->window)
645 mapped = 1;
646 break;
647
648 case XCB_CONFIGURE_NOTIFY:
649 configure_notify =
650 (xcb_configure_notify_event_t *) event;
651
652
653 if (configure_notify->width % output->scale != 0 ||
654 configure_notify->height % output->scale != 0)
655 weston_log("Resolution is not a multiple of screen size, rounding\n");
656 output->mode.width = configure_notify->width;
657 output->mode.height = configure_notify->height;
658 configured = 1;
659 break;
660 }
661 }
662 }
663
664 static xcb_visualtype_t *
find_visual_by_id(xcb_screen_t * screen,xcb_visualid_t id)665 find_visual_by_id(xcb_screen_t *screen,
666 xcb_visualid_t id)
667 {
668 xcb_depth_iterator_t i;
669 xcb_visualtype_iterator_t j;
670 for (i = xcb_screen_allowed_depths_iterator(screen);
671 i.rem;
672 xcb_depth_next(&i)) {
673 for (j = xcb_depth_visuals_iterator(i.data);
674 j.rem;
675 xcb_visualtype_next(&j)) {
676 if (j.data->visual_id == id)
677 return j.data;
678 }
679 }
680 return 0;
681 }
682
683 static uint8_t
get_depth_of_visual(xcb_screen_t * screen,xcb_visualid_t id)684 get_depth_of_visual(xcb_screen_t *screen,
685 xcb_visualid_t id)
686 {
687 xcb_depth_iterator_t i;
688 xcb_visualtype_iterator_t j;
689 for (i = xcb_screen_allowed_depths_iterator(screen);
690 i.rem;
691 xcb_depth_next(&i)) {
692 for (j = xcb_depth_visuals_iterator(i.data);
693 j.rem;
694 xcb_visualtype_next(&j)) {
695 if (j.data->visual_id == id)
696 return i.data->depth;
697 }
698 }
699 return 0;
700 }
701
702 static int
x11_output_init_shm(struct x11_backend * b,struct x11_output * output,int width,int height)703 x11_output_init_shm(struct x11_backend *b, struct x11_output *output,
704 int width, int height)
705 {
706 xcb_visualtype_t *visual_type;
707 xcb_screen_t *screen;
708 xcb_format_iterator_t fmt;
709 xcb_void_cookie_t cookie;
710 xcb_generic_error_t *err;
711 const xcb_query_extension_reply_t *ext;
712 int bitsperpixel = 0;
713 pixman_format_code_t pixman_format;
714
715 /* Check if SHM is available */
716 ext = xcb_get_extension_data(b->conn, &xcb_shm_id);
717 if (ext == NULL || !ext->present) {
718 /* SHM is missing */
719 weston_log("SHM extension is not available\n");
720 errno = ENOENT;
721 return -1;
722 }
723
724 screen = x11_compositor_get_default_screen(b);
725 visual_type = find_visual_by_id(screen, screen->root_visual);
726 if (!visual_type) {
727 weston_log("Failed to lookup visual for root window\n");
728 errno = ENOENT;
729 return -1;
730 }
731 weston_log("Found visual, bits per value: %d, red_mask: %.8x, green_mask: %.8x, blue_mask: %.8x\n",
732 visual_type->bits_per_rgb_value,
733 visual_type->red_mask,
734 visual_type->green_mask,
735 visual_type->blue_mask);
736 output->depth = get_depth_of_visual(screen, screen->root_visual);
737 weston_log("Visual depth is %d\n", output->depth);
738
739 for (fmt = xcb_setup_pixmap_formats_iterator(xcb_get_setup(b->conn));
740 fmt.rem;
741 xcb_format_next(&fmt)) {
742 if (fmt.data->depth == output->depth) {
743 bitsperpixel = fmt.data->bits_per_pixel;
744 break;
745 }
746 }
747 weston_log("Found format for depth %d, bpp: %d\n",
748 output->depth, bitsperpixel);
749
750 if (bitsperpixel == 32 &&
751 visual_type->red_mask == 0xff0000 &&
752 visual_type->green_mask == 0x00ff00 &&
753 visual_type->blue_mask == 0x0000ff) {
754 weston_log("Will use x8r8g8b8 format for SHM surfaces\n");
755 pixman_format = PIXMAN_x8r8g8b8;
756 } else if (bitsperpixel == 16 &&
757 visual_type->red_mask == 0x00f800 &&
758 visual_type->green_mask == 0x0007e0 &&
759 visual_type->blue_mask == 0x00001f) {
760 weston_log("Will use r5g6b5 format for SHM surfaces\n");
761 pixman_format = PIXMAN_r5g6b5;
762 } else {
763 weston_log("Can't find appropriate format for SHM pixmap\n");
764 errno = ENOTSUP;
765 return -1;
766 }
767
768
769 /* Create SHM segment and attach it */
770 output->shm_id = shmget(IPC_PRIVATE, width * height * (bitsperpixel / 8), IPC_CREAT | S_IRWXU);
771 if (output->shm_id == -1) {
772 weston_log("x11shm: failed to allocate SHM segment\n");
773 return -1;
774 }
775 output->buf = shmat(output->shm_id, NULL, 0 /* read/write */);
776 if (-1 == (long)output->buf) {
777 weston_log("x11shm: failed to attach SHM segment\n");
778 return -1;
779 }
780 output->segment = xcb_generate_id(b->conn);
781 cookie = xcb_shm_attach_checked(b->conn, output->segment, output->shm_id, 1);
782 err = xcb_request_check(b->conn, cookie);
783 if (err) {
784 weston_log("x11shm: xcb_shm_attach error %d, op code %d, resource id %d\n",
785 err->error_code, err->major_code, err->minor_code);
786 free(err);
787 return -1;
788 }
789
790 shmctl(output->shm_id, IPC_RMID, NULL);
791
792 /* Now create pixman image */
793 output->hw_surface = pixman_image_create_bits(pixman_format, width, height, output->buf,
794 width * (bitsperpixel / 8));
795
796 output->gc = xcb_generate_id(b->conn);
797 xcb_create_gc(b->conn, output->gc, output->window, 0, NULL);
798
799 return 0;
800 }
801
802 static int
x11_output_switch_mode(struct weston_output * base,struct weston_mode * mode)803 x11_output_switch_mode(struct weston_output *base, struct weston_mode *mode)
804 {
805 struct x11_backend *b;
806 struct x11_output *output;
807 static uint32_t values[2];
808 int ret;
809
810 b = to_x11_backend(base->compositor);
811 output = to_x11_output(base);
812
813 if (mode->width == output->mode.width &&
814 mode->height == output->mode.height)
815 return 0;
816
817 if (mode->width < WINDOW_MIN_WIDTH || mode->width > WINDOW_MAX_WIDTH)
818 return -1;
819
820 if (mode->height < WINDOW_MIN_HEIGHT || mode->height > WINDOW_MAX_HEIGHT)
821 return -1;
822
823 /* xcb_configure_window will create an event, and we could end up
824 being called twice */
825 output->resize_pending = true;
826
827 /* window could've been resized by the user, so don't do it twice */
828 if (!output->window_resized) {
829 values[0] = mode->width;
830 values[1] = mode->height;
831 xcb_configure_window(b->conn, output->window, XCB_CONFIG_WINDOW_WIDTH |
832 XCB_CONFIG_WINDOW_HEIGHT, values);
833 }
834
835 output->mode.width = mode->width;
836 output->mode.height = mode->height;
837
838 if (b->use_pixman) {
839 const struct pixman_renderer_output_options options = {
840 .use_shadow = true,
841 };
842 pixman_renderer_output_destroy(&output->base);
843 x11_output_deinit_shm(b, output);
844
845 if (x11_output_init_shm(b, output,
846 output->base.current_mode->width,
847 output->base.current_mode->height) < 0) {
848 weston_log("Failed to initialize SHM for the X11 output\n");
849 return -1;
850 }
851
852 if (pixman_renderer_output_create(&output->base, &options) < 0) {
853 weston_log("Failed to create pixman renderer for output\n");
854 x11_output_deinit_shm(b, output);
855 return -1;
856 }
857 } else {
858 Window xid = (Window) output->window;
859 const struct gl_renderer_output_options options = {
860 .window_for_legacy = (EGLNativeWindowType) output->window,
861 .window_for_platform = &xid,
862 .drm_formats = x11_formats,
863 .drm_formats_count = ARRAY_LENGTH(x11_formats),
864 };
865
866 gl_renderer->output_destroy(&output->base);
867
868 ret = gl_renderer->output_window_create(&output->base, &options);
869 if (ret < 0)
870 return -1;
871 }
872
873 output->resize_pending = false;
874 output->window_resized = false;
875
876 return 0;
877 }
878
879 static int
x11_output_disable(struct weston_output * base)880 x11_output_disable(struct weston_output *base)
881 {
882 struct x11_output *output = to_x11_output(base);
883 struct x11_backend *backend = to_x11_backend(base->compositor);
884
885 if (!output->base.enabled)
886 return 0;
887
888 wl_event_source_remove(output->finish_frame_timer);
889
890 if (backend->use_pixman) {
891 pixman_renderer_output_destroy(&output->base);
892 x11_output_deinit_shm(backend, output);
893 } else {
894 gl_renderer->output_destroy(&output->base);
895 }
896
897 xcb_destroy_window(backend->conn, output->window);
898 xcb_flush(backend->conn);
899
900 return 0;
901 }
902
903 static void
x11_output_destroy(struct weston_output * base)904 x11_output_destroy(struct weston_output *base)
905 {
906 struct x11_output *output = to_x11_output(base);
907
908 x11_output_disable(&output->base);
909 weston_output_release(&output->base);
910
911 free(output);
912 }
913
914 static int
x11_output_enable(struct weston_output * base)915 x11_output_enable(struct weston_output *base)
916 {
917 struct x11_output *output = to_x11_output(base);
918 struct x11_backend *b = to_x11_backend(base->compositor);
919
920 static const char name[] = "Weston Compositor";
921 static const char class[] = "weston-1\0Weston Compositor";
922 char *title = NULL;
923 xcb_screen_t *screen;
924 struct wm_normal_hints normal_hints;
925 struct wl_event_loop *loop;
926 char *icon_filename;
927
928 int ret;
929 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
930 xcb_atom_t atom_list[1];
931 uint32_t values[2] = {
932 XCB_EVENT_MASK_EXPOSURE |
933 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
934 0
935 };
936
937 if (!b->no_input)
938 values[0] |=
939 XCB_EVENT_MASK_KEY_PRESS |
940 XCB_EVENT_MASK_KEY_RELEASE |
941 XCB_EVENT_MASK_BUTTON_PRESS |
942 XCB_EVENT_MASK_BUTTON_RELEASE |
943 XCB_EVENT_MASK_POINTER_MOTION |
944 XCB_EVENT_MASK_ENTER_WINDOW |
945 XCB_EVENT_MASK_LEAVE_WINDOW |
946 XCB_EVENT_MASK_KEYMAP_STATE |
947 XCB_EVENT_MASK_FOCUS_CHANGE;
948
949 values[1] = b->null_cursor;
950 output->window = xcb_generate_id(b->conn);
951 screen = x11_compositor_get_default_screen(b);
952 xcb_create_window(b->conn,
953 XCB_COPY_FROM_PARENT,
954 output->window,
955 screen->root,
956 0, 0,
957 output->base.current_mode->width,
958 output->base.current_mode->height,
959 0,
960 XCB_WINDOW_CLASS_INPUT_OUTPUT,
961 screen->root_visual,
962 mask, values);
963
964 if (b->fullscreen) {
965 atom_list[0] = b->atom.net_wm_state_fullscreen;
966 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE,
967 output->window,
968 b->atom.net_wm_state,
969 XCB_ATOM_ATOM, 32,
970 ARRAY_LENGTH(atom_list), atom_list);
971 } else {
972 memset(&normal_hints, 0, sizeof normal_hints);
973 normal_hints.flags =
974 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
975 normal_hints.min_width = WINDOW_MIN_WIDTH;
976 normal_hints.min_height = WINDOW_MIN_HEIGHT;
977 normal_hints.max_width = WINDOW_MAX_WIDTH;
978 normal_hints.max_height = WINDOW_MAX_HEIGHT;
979 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
980 b->atom.wm_normal_hints,
981 b->atom.wm_size_hints, 32,
982 sizeof normal_hints / 4,
983 (uint8_t *) &normal_hints);
984 }
985
986 /* Set window name. Don't bother with non-EWMH WMs. */
987 if (output->base.name) {
988 if (asprintf(&title, "%s - %s", name, output->base.name) < 0)
989 title = NULL;
990 } else {
991 title = strdup(name);
992 }
993
994 if (title) {
995 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
996 b->atom.net_wm_name, b->atom.utf8_string, 8,
997 strlen(title), title);
998 free(title);
999 } else {
1000 goto err;
1001 }
1002
1003 xcb_change_property(b->conn, XCB_PROP_MODE_REPLACE, output->window,
1004 b->atom.wm_class, b->atom.string, 8,
1005 sizeof class, class);
1006
1007 icon_filename = file_name_with_datadir("wayland.png");
1008 x11_output_set_icon(b, output, icon_filename);
1009 free(icon_filename);
1010
1011 x11_output_set_wm_protocols(b, output);
1012
1013 xcb_map_window(b->conn, output->window);
1014
1015 if (b->fullscreen)
1016 x11_output_wait_for_map(b, output);
1017
1018 if (b->use_pixman) {
1019 const struct pixman_renderer_output_options options = {
1020 .use_shadow = true,
1021 };
1022 if (x11_output_init_shm(b, output,
1023 output->base.current_mode->width,
1024 output->base.current_mode->height) < 0) {
1025 weston_log("Failed to initialize SHM for the X11 output\n");
1026 goto err;
1027 }
1028 if (pixman_renderer_output_create(&output->base, &options) < 0) {
1029 weston_log("Failed to create pixman renderer for output\n");
1030 x11_output_deinit_shm(b, output);
1031 goto err;
1032 }
1033
1034 output->base.repaint = x11_output_repaint_shm;
1035 } else {
1036 /* eglCreatePlatformWindowSurfaceEXT takes a Window*
1037 * but eglCreateWindowSurface takes a Window. */
1038 Window xid = (Window) output->window;
1039 const struct gl_renderer_output_options options = {
1040 .window_for_legacy = (EGLNativeWindowType) output->window,
1041 .window_for_platform = &xid,
1042 .drm_formats = x11_formats,
1043 .drm_formats_count = ARRAY_LENGTH(x11_formats),
1044 };
1045
1046 ret = gl_renderer->output_window_create(&output->base,
1047 &options);
1048 if (ret < 0)
1049 goto err;
1050
1051 output->base.repaint = x11_output_repaint_gl;
1052 }
1053
1054 output->base.start_repaint_loop = x11_output_start_repaint_loop;
1055 output->base.assign_planes = NULL;
1056 output->base.set_backlight = NULL;
1057 output->base.set_dpms = NULL;
1058 output->base.switch_mode = x11_output_switch_mode;
1059
1060 loop = wl_display_get_event_loop(b->compositor->wl_display);
1061 output->finish_frame_timer =
1062 wl_event_loop_add_timer(loop, finish_frame_handler, output);
1063
1064 weston_log("x11 output %dx%d, window id %d\n",
1065 output->base.current_mode->width,
1066 output->base.current_mode->height,
1067 output->window);
1068
1069 return 0;
1070
1071 err:
1072 xcb_destroy_window(b->conn, output->window);
1073 xcb_flush(b->conn);
1074
1075 return -1;
1076 }
1077
1078 static int
x11_output_set_size(struct weston_output * base,int width,int height)1079 x11_output_set_size(struct weston_output *base, int width, int height)
1080 {
1081 struct x11_output *output = to_x11_output(base);
1082 struct x11_backend *b = to_x11_backend(base->compositor);
1083 struct weston_head *head;
1084 xcb_screen_t *scrn = b->screen;
1085 int output_width, output_height;
1086
1087 /* We can only be called once. */
1088 assert(!output->base.current_mode);
1089
1090 /* Make sure we have scale set. */
1091 assert(output->base.scale);
1092
1093 if (width < WINDOW_MIN_WIDTH) {
1094 weston_log("Invalid width \"%d\" for output %s\n",
1095 width, output->base.name);
1096 return -1;
1097 }
1098
1099 if (height < WINDOW_MIN_HEIGHT) {
1100 weston_log("Invalid height \"%d\" for output %s\n",
1101 height, output->base.name);
1102 return -1;
1103 }
1104
1105 wl_list_for_each(head, &output->base.head_list, output_link) {
1106 weston_head_set_monitor_strings(head, "weston-X11", "none", NULL);
1107 weston_head_set_physical_size(head,
1108 width * scrn->width_in_millimeters / scrn->width_in_pixels,
1109 height * scrn->height_in_millimeters / scrn->height_in_pixels);
1110 }
1111
1112 output_width = width * output->base.scale;
1113 output_height = height * output->base.scale;
1114
1115 output->mode.flags =
1116 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1117
1118 output->mode.width = output_width;
1119 output->mode.height = output_height;
1120 output->mode.refresh = 60000;
1121 output->native = output->mode;
1122 output->scale = output->base.scale;
1123 wl_list_insert(&output->base.mode_list, &output->mode.link);
1124
1125 output->base.current_mode = &output->mode;
1126 output->base.native_mode = &output->native;
1127 output->base.native_scale = output->base.scale;
1128
1129 return 0;
1130 }
1131
1132 static struct weston_output *
x11_output_create(struct weston_compositor * compositor,const char * name)1133 x11_output_create(struct weston_compositor *compositor, const char *name)
1134 {
1135 struct x11_output *output;
1136
1137 /* name can't be NULL. */
1138 assert(name);
1139
1140 output = zalloc(sizeof *output);
1141 if (!output)
1142 return NULL;
1143
1144 weston_output_init(&output->base, compositor, name);
1145
1146 output->base.destroy = x11_output_destroy;
1147 output->base.disable = x11_output_disable;
1148 output->base.enable = x11_output_enable;
1149 output->base.attach_head = NULL;
1150
1151 weston_compositor_add_pending_output(&output->base, compositor);
1152
1153 return &output->base;
1154 }
1155
1156 static int
x11_head_create(struct weston_compositor * compositor,const char * name)1157 x11_head_create(struct weston_compositor *compositor, const char *name)
1158 {
1159 struct x11_head *head;
1160
1161 assert(name);
1162
1163 head = zalloc(sizeof *head);
1164 if (!head)
1165 return -1;
1166
1167 weston_head_init(&head->base, name);
1168 weston_head_set_connection_status(&head->base, true);
1169 weston_compositor_add_head(compositor, &head->base);
1170
1171 return 0;
1172 }
1173
1174 static void
x11_head_destroy(struct x11_head * head)1175 x11_head_destroy(struct x11_head *head)
1176 {
1177 weston_head_release(&head->base);
1178 free(head);
1179 }
1180
1181 static struct x11_output *
x11_backend_find_output(struct x11_backend * b,xcb_window_t window)1182 x11_backend_find_output(struct x11_backend *b, xcb_window_t window)
1183 {
1184 struct x11_output *output;
1185
1186 wl_list_for_each(output, &b->compositor->output_list, base.link) {
1187 if (output->window == window)
1188 return output;
1189 }
1190
1191 return NULL;
1192 }
1193
1194 static void
x11_backend_delete_window(struct x11_backend * b,xcb_window_t window)1195 x11_backend_delete_window(struct x11_backend *b, xcb_window_t window)
1196 {
1197 struct x11_output *output;
1198
1199 output = x11_backend_find_output(b, window);
1200 if (output)
1201 x11_output_destroy(&output->base);
1202
1203 if (wl_list_empty(&b->compositor->output_list))
1204 weston_compositor_exit(b->compositor);
1205 }
1206
delete_cb(void * data)1207 static void delete_cb(void *data)
1208 {
1209 struct window_delete_data *wd = data;
1210
1211 x11_backend_delete_window(wd->backend, wd->window);
1212 free(wd);
1213 }
1214
1215 #ifdef HAVE_XCB_XKB
1216 static void
update_xkb_state(struct x11_backend * b,xcb_xkb_state_notify_event_t * state)1217 update_xkb_state(struct x11_backend *b, xcb_xkb_state_notify_event_t *state)
1218 {
1219 struct weston_keyboard *keyboard =
1220 weston_seat_get_keyboard(&b->core_seat);
1221
1222 xkb_state_update_mask(keyboard->xkb_state.state,
1223 get_xkb_mod_mask(b, state->baseMods),
1224 get_xkb_mod_mask(b, state->latchedMods),
1225 get_xkb_mod_mask(b, state->lockedMods),
1226 0,
1227 0,
1228 state->group);
1229
1230 notify_modifiers(&b->core_seat,
1231 wl_display_next_serial(b->compositor->wl_display));
1232 }
1233 #endif
1234
1235 /**
1236 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
1237 * the best we can do (given that XCB also lacks XI2 support), is to take
1238 * the state from the core key events. Unfortunately that only gives us
1239 * the effective (i.e. union of depressed/latched/locked) state, and we
1240 * need the granularity.
1241 *
1242 * So we still update the state with every key event we see, but also use
1243 * the state field from X11 events as a mask so we don't get any stuck
1244 * modifiers.
1245 */
1246 static void
update_xkb_state_from_core(struct x11_backend * b,uint16_t x11_mask)1247 update_xkb_state_from_core(struct x11_backend *b, uint16_t x11_mask)
1248 {
1249 uint32_t mask = get_xkb_mod_mask(b, x11_mask);
1250 struct weston_keyboard *keyboard
1251 = weston_seat_get_keyboard(&b->core_seat);
1252
1253 xkb_state_update_mask(keyboard->xkb_state.state,
1254 keyboard->modifiers.mods_depressed & mask,
1255 keyboard->modifiers.mods_latched & mask,
1256 keyboard->modifiers.mods_locked & mask,
1257 0,
1258 0,
1259 (x11_mask >> 13) & 3);
1260 notify_modifiers(&b->core_seat,
1261 wl_display_next_serial(b->compositor->wl_display));
1262 }
1263
1264 static void
x11_backend_deliver_button_event(struct x11_backend * b,xcb_generic_event_t * event)1265 x11_backend_deliver_button_event(struct x11_backend *b,
1266 xcb_generic_event_t *event)
1267 {
1268 xcb_button_press_event_t *button_event =
1269 (xcb_button_press_event_t *) event;
1270 uint32_t button;
1271 struct x11_output *output;
1272 struct weston_pointer_axis_event weston_event;
1273 bool is_button_pressed = event->response_type == XCB_BUTTON_PRESS;
1274 struct timespec time = { 0 };
1275
1276 assert(event->response_type == XCB_BUTTON_PRESS ||
1277 event->response_type == XCB_BUTTON_RELEASE);
1278
1279 output = x11_backend_find_output(b, button_event->event);
1280 if (!output)
1281 return;
1282
1283 if (is_button_pressed)
1284 xcb_grab_pointer(b->conn, 0, output->window,
1285 XCB_EVENT_MASK_BUTTON_PRESS |
1286 XCB_EVENT_MASK_BUTTON_RELEASE |
1287 XCB_EVENT_MASK_POINTER_MOTION |
1288 XCB_EVENT_MASK_ENTER_WINDOW |
1289 XCB_EVENT_MASK_LEAVE_WINDOW,
1290 XCB_GRAB_MODE_ASYNC,
1291 XCB_GRAB_MODE_ASYNC,
1292 output->window, XCB_CURSOR_NONE,
1293 button_event->time);
1294 else
1295 xcb_ungrab_pointer(b->conn, button_event->time);
1296
1297 if (!b->has_xkb)
1298 update_xkb_state_from_core(b, button_event->state);
1299
1300 switch (button_event->detail) {
1301 case 1:
1302 button = BTN_LEFT;
1303 break;
1304 case 2:
1305 button = BTN_MIDDLE;
1306 break;
1307 case 3:
1308 button = BTN_RIGHT;
1309 break;
1310 case 4:
1311 /* Axis are measured in pixels, but the xcb events are discrete
1312 * steps. Therefore move the axis by some pixels every step. */
1313 if (is_button_pressed) {
1314 weston_event.value = -DEFAULT_AXIS_STEP_DISTANCE;
1315 weston_event.discrete = -1;
1316 weston_event.has_discrete = true;
1317 weston_event.axis =
1318 WL_POINTER_AXIS_VERTICAL_SCROLL;
1319 weston_compositor_get_time(&time);
1320 notify_axis(&b->core_seat, &time, &weston_event);
1321 notify_pointer_frame(&b->core_seat);
1322 }
1323 return;
1324 case 5:
1325 if (is_button_pressed) {
1326 weston_event.value = DEFAULT_AXIS_STEP_DISTANCE;
1327 weston_event.discrete = 1;
1328 weston_event.has_discrete = true;
1329 weston_event.axis =
1330 WL_POINTER_AXIS_VERTICAL_SCROLL;
1331 weston_compositor_get_time(&time);
1332 notify_axis(&b->core_seat, &time, &weston_event);
1333 notify_pointer_frame(&b->core_seat);
1334 }
1335 return;
1336 case 6:
1337 if (is_button_pressed) {
1338 weston_event.value = -DEFAULT_AXIS_STEP_DISTANCE;
1339 weston_event.discrete = -1;
1340 weston_event.has_discrete = true;
1341 weston_event.axis =
1342 WL_POINTER_AXIS_HORIZONTAL_SCROLL;
1343 weston_compositor_get_time(&time);
1344 notify_axis(&b->core_seat, &time, &weston_event);
1345 notify_pointer_frame(&b->core_seat);
1346 }
1347 return;
1348 case 7:
1349 if (is_button_pressed) {
1350 weston_event.value = DEFAULT_AXIS_STEP_DISTANCE;
1351 weston_event.discrete = 1;
1352 weston_event.has_discrete = true;
1353 weston_event.axis =
1354 WL_POINTER_AXIS_HORIZONTAL_SCROLL;
1355 weston_compositor_get_time(&time);
1356 notify_axis(&b->core_seat, &time, &weston_event);
1357 notify_pointer_frame(&b->core_seat);
1358 }
1359 return;
1360 default:
1361 button = button_event->detail + BTN_SIDE - 8;
1362 break;
1363 }
1364
1365 weston_compositor_get_time(&time);
1366
1367 notify_button(&b->core_seat, &time, button,
1368 is_button_pressed ? WL_POINTER_BUTTON_STATE_PRESSED :
1369 WL_POINTER_BUTTON_STATE_RELEASED);
1370 notify_pointer_frame(&b->core_seat);
1371 }
1372
1373 static void
x11_backend_deliver_motion_event(struct x11_backend * b,xcb_generic_event_t * event)1374 x11_backend_deliver_motion_event(struct x11_backend *b,
1375 xcb_generic_event_t *event)
1376 {
1377 struct x11_output *output;
1378 double x, y;
1379 struct weston_pointer_motion_event motion_event = { 0 };
1380 xcb_motion_notify_event_t *motion_notify =
1381 (xcb_motion_notify_event_t *) event;
1382 struct timespec time;
1383
1384 if (!b->has_xkb)
1385 update_xkb_state_from_core(b, motion_notify->state);
1386 output = x11_backend_find_output(b, motion_notify->event);
1387 if (!output)
1388 return;
1389
1390 weston_output_transform_coordinate(&output->base,
1391 motion_notify->event_x,
1392 motion_notify->event_y,
1393 &x, &y);
1394
1395 motion_event = (struct weston_pointer_motion_event) {
1396 .mask = WESTON_POINTER_MOTION_REL,
1397 .dx = x - b->prev_x,
1398 .dy = y - b->prev_y
1399 };
1400
1401 weston_compositor_get_time(&time);
1402 notify_motion(&b->core_seat, &time, &motion_event);
1403 notify_pointer_frame(&b->core_seat);
1404
1405 b->prev_x = x;
1406 b->prev_y = y;
1407 }
1408
1409 static void
x11_backend_deliver_enter_event(struct x11_backend * b,xcb_generic_event_t * event)1410 x11_backend_deliver_enter_event(struct x11_backend *b,
1411 xcb_generic_event_t *event)
1412 {
1413 struct x11_output *output;
1414 double x, y;
1415
1416 xcb_enter_notify_event_t *enter_notify =
1417 (xcb_enter_notify_event_t *) event;
1418 if (enter_notify->state >= Button1Mask)
1419 return;
1420 if (!b->has_xkb)
1421 update_xkb_state_from_core(b, enter_notify->state);
1422 output = x11_backend_find_output(b, enter_notify->event);
1423 if (!output)
1424 return;
1425
1426 weston_output_transform_coordinate(&output->base,
1427 enter_notify->event_x,
1428 enter_notify->event_y, &x, &y);
1429
1430 notify_pointer_focus(&b->core_seat, &output->base, x, y);
1431
1432 b->prev_x = x;
1433 b->prev_y = y;
1434 }
1435
1436 static int
x11_backend_next_event(struct x11_backend * b,xcb_generic_event_t ** event,uint32_t mask)1437 x11_backend_next_event(struct x11_backend *b,
1438 xcb_generic_event_t **event, uint32_t mask)
1439 {
1440 if (mask & WL_EVENT_READABLE)
1441 *event = xcb_poll_for_event(b->conn);
1442 else
1443 *event = xcb_poll_for_queued_event(b->conn);
1444
1445 return *event != NULL;
1446 }
1447
1448 static int
x11_backend_handle_event(int fd,uint32_t mask,void * data)1449 x11_backend_handle_event(int fd, uint32_t mask, void *data)
1450 {
1451 struct x11_backend *b = data;
1452 struct x11_output *output;
1453 xcb_generic_event_t *event, *prev;
1454 xcb_client_message_event_t *client_message;
1455 xcb_enter_notify_event_t *enter_notify;
1456 xcb_key_press_event_t *key_press, *key_release;
1457 xcb_keymap_notify_event_t *keymap_notify;
1458 xcb_focus_in_event_t *focus_in;
1459 xcb_expose_event_t *expose;
1460 xcb_configure_notify_event_t *configure;
1461 xcb_atom_t atom;
1462 xcb_window_t window;
1463 uint32_t *k;
1464 uint32_t i, set;
1465 uint8_t response_type;
1466 int count;
1467 struct timespec time;
1468
1469 prev = NULL;
1470 count = 0;
1471 while (x11_backend_next_event(b, &event, mask)) {
1472 response_type = event->response_type & ~0x80;
1473
1474 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1475 case XCB_KEY_RELEASE:
1476 /* Suppress key repeat events; this is only used if we
1477 * don't have XCB XKB support. */
1478 key_release = (xcb_key_press_event_t *) prev;
1479 key_press = (xcb_key_press_event_t *) event;
1480 if (response_type == XCB_KEY_PRESS &&
1481 key_release->time == key_press->time &&
1482 key_release->detail == key_press->detail) {
1483 /* Don't deliver the held key release
1484 * event or the new key press event. */
1485 free(event);
1486 free(prev);
1487 prev = NULL;
1488 continue;
1489 } else {
1490 /* Deliver the held key release now
1491 * and fall through and handle the new
1492 * event below. */
1493 update_xkb_state_from_core(b, key_release->state);
1494 weston_compositor_get_time(&time);
1495 notify_key(&b->core_seat,
1496 &time,
1497 key_release->detail - 8,
1498 WL_KEYBOARD_KEY_STATE_RELEASED,
1499 STATE_UPDATE_AUTOMATIC);
1500 free(prev);
1501 prev = NULL;
1502 break;
1503 }
1504
1505 case XCB_FOCUS_IN:
1506 assert(response_type == XCB_KEYMAP_NOTIFY);
1507 keymap_notify = (xcb_keymap_notify_event_t *) event;
1508 b->keys.size = 0;
1509 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
1510 set = keymap_notify->keys[i >> 3] &
1511 (1 << (i & 7));
1512 if (set) {
1513 k = wl_array_add(&b->keys, sizeof *k);
1514 *k = i;
1515 }
1516 }
1517
1518 /* Unfortunately the state only comes with the enter
1519 * event, rather than with the focus event. I'm not
1520 * sure of the exact semantics around it and whether
1521 * we can ensure that we get both? */
1522 notify_keyboard_focus_in(&b->core_seat, &b->keys,
1523 STATE_UPDATE_AUTOMATIC);
1524
1525 free(prev);
1526 prev = NULL;
1527 break;
1528
1529 default:
1530 /* No previous event held */
1531 break;
1532 }
1533
1534 switch (response_type) {
1535 case XCB_KEY_PRESS:
1536 key_press = (xcb_key_press_event_t *) event;
1537 if (!b->has_xkb)
1538 update_xkb_state_from_core(b, key_press->state);
1539 weston_compositor_get_time(&time);
1540 notify_key(&b->core_seat,
1541 &time,
1542 key_press->detail - 8,
1543 WL_KEYBOARD_KEY_STATE_PRESSED,
1544 b->has_xkb ? STATE_UPDATE_NONE :
1545 STATE_UPDATE_AUTOMATIC);
1546 break;
1547 case XCB_KEY_RELEASE:
1548 /* If we don't have XKB, we need to use the lame
1549 * autorepeat detection above. */
1550 if (!b->has_xkb) {
1551 prev = event;
1552 break;
1553 }
1554 key_release = (xcb_key_press_event_t *) event;
1555 weston_compositor_get_time(&time);
1556 notify_key(&b->core_seat,
1557 &time,
1558 key_release->detail - 8,
1559 WL_KEYBOARD_KEY_STATE_RELEASED,
1560 STATE_UPDATE_NONE);
1561 break;
1562 case XCB_BUTTON_PRESS:
1563 case XCB_BUTTON_RELEASE:
1564 x11_backend_deliver_button_event(b, event);
1565 break;
1566 case XCB_MOTION_NOTIFY:
1567 x11_backend_deliver_motion_event(b, event);
1568 break;
1569
1570 case XCB_EXPOSE:
1571 expose = (xcb_expose_event_t *) event;
1572 output = x11_backend_find_output(b, expose->window);
1573 if (!output)
1574 break;
1575
1576 weston_output_damage(&output->base);
1577 weston_output_schedule_repaint(&output->base);
1578 break;
1579
1580 case XCB_ENTER_NOTIFY:
1581 x11_backend_deliver_enter_event(b, event);
1582 break;
1583
1584 case XCB_LEAVE_NOTIFY:
1585 enter_notify = (xcb_enter_notify_event_t *) event;
1586 if (enter_notify->state >= Button1Mask)
1587 break;
1588 if (!b->has_xkb)
1589 update_xkb_state_from_core(b, enter_notify->state);
1590 notify_pointer_focus(&b->core_seat, NULL, 0, 0);
1591 break;
1592
1593 case XCB_CLIENT_MESSAGE:
1594 client_message = (xcb_client_message_event_t *) event;
1595 atom = client_message->data.data32[0];
1596 window = client_message->window;
1597 if (atom == b->atom.wm_delete_window) {
1598 struct wl_event_loop *loop;
1599 struct window_delete_data *data = malloc(sizeof *data);
1600
1601 /* if malloc failed we should at least try to
1602 * delete the window, even if it may result in
1603 * a crash.
1604 */
1605 if (!data) {
1606 x11_backend_delete_window(b, window);
1607 break;
1608 }
1609 data->backend = b;
1610 data->window = window;
1611 loop = wl_display_get_event_loop(b->compositor->wl_display);
1612 wl_event_loop_add_idle(loop, delete_cb, data);
1613 }
1614 break;
1615
1616 case XCB_CONFIGURE_NOTIFY:
1617 configure = (struct xcb_configure_notify_event_t *) event;
1618 struct x11_output *output =
1619 x11_backend_find_output(b, configure->window);
1620
1621 if (!output || output->resize_pending)
1622 break;
1623
1624 struct weston_mode mode = output->mode;
1625
1626 if (mode.width == configure->width &&
1627 mode.height == configure->height)
1628 break;
1629
1630 output->window_resized = true;
1631
1632 mode.width = configure->width;
1633 mode.height = configure->height;
1634
1635 if (weston_output_mode_set_native(&output->base,
1636 &mode, output->scale) < 0)
1637 weston_log("Mode switch failed\n");
1638
1639 break;
1640
1641 case XCB_FOCUS_IN:
1642 focus_in = (xcb_focus_in_event_t *) event;
1643 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1644 break;
1645
1646 prev = event;
1647 break;
1648
1649 case XCB_FOCUS_OUT:
1650 focus_in = (xcb_focus_in_event_t *) event;
1651 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1652 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
1653 break;
1654 notify_keyboard_focus_out(&b->core_seat);
1655 break;
1656
1657 default:
1658 break;
1659 }
1660
1661 #ifdef HAVE_XCB_XKB
1662 if (b->has_xkb) {
1663 if (response_type == b->xkb_event_base) {
1664 xcb_xkb_state_notify_event_t *state =
1665 (xcb_xkb_state_notify_event_t *) event;
1666 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
1667 update_xkb_state(b, state);
1668 } else if (response_type == XCB_PROPERTY_NOTIFY) {
1669 xcb_property_notify_event_t *prop_notify =
1670 (xcb_property_notify_event_t *) event;
1671 if (prop_notify->window == b->screen->root &&
1672 prop_notify->atom == b->atom.xkb_names &&
1673 prop_notify->state == XCB_PROPERTY_NEW_VALUE)
1674 update_xkb_keymap(b);
1675 }
1676 }
1677 #endif
1678
1679 count++;
1680 if (prev != event)
1681 free (event);
1682 }
1683
1684 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1685 case XCB_KEY_RELEASE:
1686 key_release = (xcb_key_press_event_t *) prev;
1687 update_xkb_state_from_core(b, key_release->state);
1688 weston_compositor_get_time(&time);
1689 notify_key(&b->core_seat,
1690 &time,
1691 key_release->detail - 8,
1692 WL_KEYBOARD_KEY_STATE_RELEASED,
1693 STATE_UPDATE_AUTOMATIC);
1694 free(prev);
1695 prev = NULL;
1696 break;
1697 default:
1698 break;
1699 }
1700
1701 return count;
1702 }
1703
1704 #define F(field) offsetof(struct x11_backend, field)
1705
1706 static void
x11_backend_get_resources(struct x11_backend * b)1707 x11_backend_get_resources(struct x11_backend *b)
1708 {
1709 static const struct { const char *name; int offset; } atoms[] = {
1710 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1711 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1712 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1713 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
1714 { "WM_CLASS", F(atom.wm_class) },
1715 { "_NET_WM_NAME", F(atom.net_wm_name) },
1716 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1717 { "_NET_WM_STATE", F(atom.net_wm_state) },
1718 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1719 { "_NET_SUPPORTING_WM_CHECK",
1720 F(atom.net_supporting_wm_check) },
1721 { "_NET_SUPPORTED", F(atom.net_supported) },
1722 { "STRING", F(atom.string) },
1723 { "UTF8_STRING", F(atom.utf8_string) },
1724 { "CARDINAL", F(atom.cardinal) },
1725 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
1726 };
1727
1728 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1729 xcb_intern_atom_reply_t *reply;
1730 xcb_pixmap_t pixmap;
1731 xcb_gc_t gc;
1732 unsigned int i;
1733 uint8_t data[] = { 0, 0, 0, 0 };
1734
1735 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1736 cookies[i] = xcb_intern_atom (b->conn, 0,
1737 strlen(atoms[i].name),
1738 atoms[i].name);
1739
1740 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1741 reply = xcb_intern_atom_reply (b->conn, cookies[i], NULL);
1742 *(xcb_atom_t *) ((char *) b + atoms[i].offset) = reply->atom;
1743 free(reply);
1744 }
1745
1746 pixmap = xcb_generate_id(b->conn);
1747 gc = xcb_generate_id(b->conn);
1748 xcb_create_pixmap(b->conn, 1, pixmap, b->screen->root, 1, 1);
1749 xcb_create_gc(b->conn, gc, pixmap, 0, NULL);
1750 xcb_put_image(b->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
1751 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
1752 b->null_cursor = xcb_generate_id(b->conn);
1753 xcb_create_cursor (b->conn, b->null_cursor,
1754 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
1755 xcb_free_gc(b->conn, gc);
1756 xcb_free_pixmap(b->conn, pixmap);
1757 }
1758
1759 static void
x11_backend_get_wm_info(struct x11_backend * c)1760 x11_backend_get_wm_info(struct x11_backend *c)
1761 {
1762 xcb_get_property_cookie_t cookie;
1763 xcb_get_property_reply_t *reply;
1764 xcb_atom_t *atom;
1765 unsigned int i;
1766
1767 cookie = xcb_get_property(c->conn, 0, c->screen->root,
1768 c->atom.net_supported,
1769 XCB_ATOM_ATOM, 0, 1024);
1770 reply = xcb_get_property_reply(c->conn, cookie, NULL);
1771 if (reply == NULL)
1772 return;
1773
1774 atom = (xcb_atom_t *) xcb_get_property_value(reply);
1775
1776 for (i = 0; i < reply->value_len; i++) {
1777 if (atom[i] == c->atom.net_wm_state_fullscreen)
1778 c->has_net_wm_state_fullscreen = 1;
1779 }
1780
1781 free(reply);
1782 }
1783
1784 static void
x11_destroy(struct weston_compositor * ec)1785 x11_destroy(struct weston_compositor *ec)
1786 {
1787 struct x11_backend *backend = to_x11_backend(ec);
1788 struct weston_head *base, *next;
1789
1790 wl_event_source_remove(backend->xcb_source);
1791 x11_input_destroy(backend);
1792
1793 weston_compositor_shutdown(ec); /* destroys outputs, too */
1794
1795 wl_list_for_each_safe(base, next, &ec->head_list, compositor_link)
1796 x11_head_destroy(to_x11_head(base));
1797
1798 XCloseDisplay(backend->dpy);
1799 free(backend);
1800 }
1801
1802 static int
init_gl_renderer(struct x11_backend * b)1803 init_gl_renderer(struct x11_backend *b)
1804 {
1805 const struct gl_renderer_display_options options = {
1806 .egl_platform = EGL_PLATFORM_X11_KHR,
1807 .egl_native_display = b->dpy,
1808 .egl_surface_type = EGL_WINDOW_BIT,
1809 .drm_formats = x11_formats,
1810 .drm_formats_count = ARRAY_LENGTH(x11_formats),
1811 };
1812
1813 gl_renderer = weston_load_module("gl-renderer.so",
1814 "gl_renderer_interface");
1815 if (!gl_renderer)
1816 return -1;
1817
1818 return gl_renderer->display_create(b->compositor, &options);
1819 }
1820
1821 static const struct weston_windowed_output_api api = {
1822 x11_output_set_size,
1823 x11_head_create,
1824 };
1825
1826 static struct x11_backend *
x11_backend_create(struct weston_compositor * compositor,struct weston_x11_backend_config * config)1827 x11_backend_create(struct weston_compositor *compositor,
1828 struct weston_x11_backend_config *config)
1829 {
1830 struct x11_backend *b;
1831 struct wl_event_loop *loop;
1832 int ret;
1833
1834 b = zalloc(sizeof *b);
1835 if (b == NULL)
1836 return NULL;
1837
1838 b->compositor = compositor;
1839 b->fullscreen = config->fullscreen;
1840 b->no_input = config->no_input;
1841
1842 compositor->backend = &b->base;
1843
1844 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
1845 goto err_free;
1846
1847 b->dpy = XOpenDisplay(NULL);
1848 if (b->dpy == NULL)
1849 goto err_free;
1850
1851 b->conn = XGetXCBConnection(b->dpy);
1852 XSetEventQueueOwner(b->dpy, XCBOwnsEventQueue);
1853
1854 if (xcb_connection_has_error(b->conn))
1855 goto err_xdisplay;
1856
1857 b->screen = x11_compositor_get_default_screen(b);
1858 wl_array_init(&b->keys);
1859
1860 x11_backend_get_resources(b);
1861 x11_backend_get_wm_info(b);
1862
1863 if (!b->has_net_wm_state_fullscreen && config->fullscreen) {
1864 weston_log("Can not fullscreen without window manager support"
1865 "(need _NET_WM_STATE_FULLSCREEN)\n");
1866 config->fullscreen = 0;
1867 }
1868
1869 b->use_pixman = config->use_pixman;
1870 if (b->use_pixman) {
1871 if (pixman_renderer_init(compositor) < 0) {
1872 weston_log("Failed to initialize pixman renderer for X11 backend\n");
1873 goto err_xdisplay;
1874 }
1875 }
1876 else if (init_gl_renderer(b) < 0) {
1877 goto err_xdisplay;
1878 }
1879 weston_log("Using %s renderer\n", config->use_pixman ? "pixman" : "gl");
1880
1881 b->base.destroy = x11_destroy;
1882 b->base.create_output = x11_output_create;
1883
1884 if (x11_input_create(b, config->no_input) < 0) {
1885 weston_log("Failed to create X11 input\n");
1886 goto err_renderer;
1887 }
1888
1889 loop = wl_display_get_event_loop(compositor->wl_display);
1890 b->xcb_source =
1891 wl_event_loop_add_fd(loop,
1892 xcb_get_file_descriptor(b->conn),
1893 WL_EVENT_READABLE,
1894 x11_backend_handle_event, b);
1895 wl_event_source_check(b->xcb_source);
1896
1897 if (compositor->renderer->import_dmabuf) {
1898 if (linux_dmabuf_setup(compositor) < 0)
1899 weston_log("Error: initializing dmabuf "
1900 "support failed.\n");
1901 }
1902
1903 if (compositor->capabilities & WESTON_CAP_EXPLICIT_SYNC) {
1904 if (linux_explicit_synchronization_setup(compositor) < 0)
1905 weston_log("Error: initializing explicit "
1906 " synchronization support failed.\n");
1907 }
1908
1909 ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME,
1910 &api, sizeof(api));
1911
1912 if (ret < 0) {
1913 weston_log("Failed to register output API.\n");
1914 goto err_x11_input;
1915 }
1916
1917 return b;
1918
1919 err_x11_input:
1920 x11_input_destroy(b);
1921 err_renderer:
1922 compositor->renderer->destroy(compositor);
1923 err_xdisplay:
1924 XCloseDisplay(b->dpy);
1925 err_free:
1926 free(b);
1927 return NULL;
1928 }
1929
1930 static void
config_init_to_defaults(struct weston_x11_backend_config * config)1931 config_init_to_defaults(struct weston_x11_backend_config *config)
1932 {
1933 }
1934
1935 WL_EXPORT int
weston_backend_init(struct weston_compositor * compositor,struct weston_backend_config * config_base)1936 weston_backend_init(struct weston_compositor *compositor,
1937 struct weston_backend_config *config_base)
1938 {
1939 struct x11_backend *b;
1940 struct weston_x11_backend_config config = {{ 0, }};
1941
1942 if (config_base == NULL ||
1943 config_base->struct_version != WESTON_X11_BACKEND_CONFIG_VERSION ||
1944 config_base->struct_size > sizeof(struct weston_x11_backend_config)) {
1945 weston_log("X11 backend config structure is invalid\n");
1946 return -1;
1947 }
1948
1949 config_init_to_defaults(&config);
1950 memcpy(&config, config_base, config_base->struct_size);
1951
1952 b = x11_backend_create(compositor, &config);
1953 if (b == NULL)
1954 return -1;
1955
1956 return 0;
1957 }
1958