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