2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2010-2011 Intel Corporation
4 * Copyright © 2013 Vasily Khoruzhick <anarsoul@gmail.com>
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 #include <linux/input.h>
47 #include <X11/Xlib-xcb.h>
49 #include <xkbcommon/xkbcommon.h>
51 #include "compositor.h"
52 #include "gl-renderer.h"
53 #include "pixman-renderer.h"
54 #include "../shared/config-parser.h"
55 #include "../shared/cairo-util.h"
57 #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
59 static char *output_name;
60 static char *output_mode;
61 static char *output_transform;
62 static int option_width;
63 static int option_height;
64 static int option_count;
65 static struct wl_list configured_output_list;
67 struct x11_configured_output {
74 struct x11_compositor {
75 struct weston_compositor base;
78 xcb_connection_t *conn;
80 xcb_cursor_t null_cursor;
82 struct wl_event_source *xcb_source;
83 struct xkb_keymap *xkb_keymap;
85 uint8_t xkb_event_base;
88 /* We could map multi-pointer X to multiple wayland seats, but
89 * for now we only support core X input. */
90 struct weston_seat core_seat;
93 xcb_atom_t wm_protocols;
94 xcb_atom_t wm_normal_hints;
95 xcb_atom_t wm_size_hints;
96 xcb_atom_t wm_delete_window;
98 xcb_atom_t net_wm_name;
99 xcb_atom_t net_wm_icon;
100 xcb_atom_t net_wm_state;
101 xcb_atom_t net_wm_state_fullscreen;
103 xcb_atom_t utf8_string;
105 xcb_atom_t xkb_names;
110 struct weston_output base;
113 struct weston_mode mode;
114 struct wl_event_source *finish_frame_timer;
117 xcb_shm_seg_t segment;
118 pixman_image_t *hw_surface;
119 pixman_image_t *shadow_surface;
126 static struct xkb_keymap *
127 x11_compositor_get_keymap(struct x11_compositor *c)
129 xcb_get_property_cookie_t cookie;
130 xcb_get_property_reply_t *reply;
131 struct xkb_rule_names names;
132 struct xkb_keymap *ret;
133 const char *value_all, *value_part;
134 int length_all, length_part;
136 memset(&names, 0, sizeof(names));
138 cookie = xcb_get_property(c->conn, 0, c->screen->root,
139 c->atom.xkb_names, c->atom.string, 0, 1024);
140 reply = xcb_get_property_reply(c->conn, cookie, NULL);
144 value_all = xcb_get_property_value(reply);
145 length_all = xcb_get_property_value_length(reply);
146 value_part = value_all;
148 #define copy_prop_value(to) \
149 length_part = strlen(value_part); \
150 if (value_part + length_part < (value_all + length_all) && \
152 names.to = value_part; \
153 value_part += length_part + 1;
155 copy_prop_value(rules);
156 copy_prop_value(model);
157 copy_prop_value(layout);
158 copy_prop_value(variant);
159 copy_prop_value(options);
160 #undef copy_prop_value
162 ret = xkb_map_new_from_names(c->base.xkb_context, &names, 0);
169 get_xkb_mod_mask(struct x11_compositor *c, uint32_t in)
171 struct weston_xkb_info *info = &c->core_seat.xkb_info;
174 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
175 ret |= (1 << info->shift_mod);
176 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
177 ret |= (1 << info->caps_mod);
178 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
179 ret |= (1 << info->ctrl_mod);
180 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
181 ret |= (1 << info->alt_mod);
182 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
183 ret |= (1 << info->mod2_mod);
184 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
185 ret |= (1 << info->mod3_mod);
186 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
187 ret |= (1 << info->super_mod);
188 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
189 ret |= (1 << info->mod5_mod);
195 x11_compositor_setup_xkb(struct x11_compositor *c)
198 weston_log("XCB-XKB not available during build\n");
200 c->xkb_event_base = 0;
203 const xcb_query_extension_reply_t *ext;
204 xcb_generic_error_t *error;
205 xcb_void_cookie_t select;
206 xcb_xkb_use_extension_cookie_t use_ext;
207 xcb_xkb_use_extension_reply_t *use_ext_reply;
208 xcb_xkb_per_client_flags_cookie_t pcf;
209 xcb_xkb_per_client_flags_reply_t *pcf_reply;
210 xcb_xkb_get_state_cookie_t state;
211 xcb_xkb_get_state_reply_t *state_reply;
214 c->xkb_event_base = 0;
216 ext = xcb_get_extension_data(c->conn, &xcb_xkb_id);
218 weston_log("XKB extension not available on host X11 server\n");
221 c->xkb_event_base = ext->first_event;
223 select = xcb_xkb_select_events_checked(c->conn,
224 XCB_XKB_ID_USE_CORE_KBD,
225 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
227 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
231 error = xcb_request_check(c->conn, select);
233 weston_log("error: failed to select for XKB state events\n");
238 use_ext = xcb_xkb_use_extension(c->conn,
239 XCB_XKB_MAJOR_VERSION,
240 XCB_XKB_MINOR_VERSION);
241 use_ext_reply = xcb_xkb_use_extension_reply(c->conn, use_ext, NULL);
242 if (!use_ext_reply) {
243 weston_log("couldn't start using XKB extension\n");
247 if (!use_ext_reply->supported) {
248 weston_log("XKB extension version on the server is too old "
249 "(want %d.%d, has %d.%d)\n",
250 XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION,
251 use_ext_reply->serverMajor, use_ext_reply->serverMinor);
257 pcf = xcb_xkb_per_client_flags(c->conn,
258 XCB_XKB_ID_USE_CORE_KBD,
259 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
260 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
264 pcf_reply = xcb_xkb_per_client_flags_reply(c->conn, pcf, NULL);
266 !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT)) {
267 weston_log("failed to set XKB per-client flags, not using "
268 "detectable repeat\n");
274 state = xcb_xkb_get_state(c->conn, XCB_XKB_ID_USE_CORE_KBD);
275 state_reply = xcb_xkb_get_state_reply(c->conn, state, NULL);
277 weston_log("failed to get initial XKB state\n");
281 xkb_state_update_mask(c->core_seat.xkb_state.state,
282 get_xkb_mod_mask(c, state_reply->baseMods),
283 get_xkb_mod_mask(c, state_reply->latchedMods),
284 get_xkb_mod_mask(c, state_reply->lockedMods),
296 x11_input_create(struct x11_compositor *c, int no_input)
298 struct xkb_keymap *keymap;
300 weston_seat_init(&c->core_seat, &c->base);
305 weston_seat_init_pointer(&c->core_seat);
307 keymap = x11_compositor_get_keymap(c);
308 weston_seat_init_keyboard(&c->core_seat, keymap);
310 xkb_map_unref(keymap);
312 x11_compositor_setup_xkb(c);
318 x11_input_destroy(struct x11_compositor *compositor)
320 weston_seat_release(&compositor->core_seat);
324 x11_output_repaint_gl(struct weston_output *output_base,
325 pixman_region32_t *damage)
327 struct x11_output *output = (struct x11_output *)output_base;
328 struct weston_compositor *ec = output->base.compositor;
330 ec->renderer->repaint_output(output_base, damage);
332 pixman_region32_subtract(&ec->primary_plane.damage,
333 &ec->primary_plane.damage, damage);
335 wl_event_source_timer_update(output->finish_frame_timer, 10);
339 x11_output_repaint_shm(struct weston_output *output_base,
340 pixman_region32_t *damage)
342 struct x11_output *output = (struct x11_output *)output_base;
343 struct weston_compositor *ec = output->base.compositor;
344 struct x11_compositor *c = (struct x11_compositor *)ec;
345 pixman_box32_t *rects;
346 int nrects, i, src_x, src_y, x1, y1, x2, y2, width, height;
347 xcb_void_cookie_t cookie;
348 xcb_generic_error_t *err;
350 pixman_renderer_output_set_buffer(output_base, output->shadow_surface);
351 ec->renderer->repaint_output(output_base, damage);
353 width = pixman_image_get_width(output->shadow_surface);
354 height = pixman_image_get_height(output->shadow_surface);
355 rects = pixman_region32_rectangles(damage, &nrects);
356 for (i = 0; i < nrects; i++) {
357 switch (output_base->transform) {
359 case WL_OUTPUT_TRANSFORM_NORMAL:
365 case WL_OUTPUT_TRANSFORM_180:
366 x1 = width - rects[i].x2;
367 x2 = width - rects[i].x1;
368 y1 = height - rects[i].y2;
369 y2 = height - rects[i].y1;
371 case WL_OUTPUT_TRANSFORM_90:
372 x1 = height - rects[i].y2;
373 x2 = height - rects[i].y1;
377 case WL_OUTPUT_TRANSFORM_270:
380 y1 = width - rects[i].x2;
381 y2 = width - rects[i].x1;
387 pixman_image_composite32(PIXMAN_OP_SRC,
388 output->shadow_surface, /* src */
390 output->hw_surface, /* dest */
391 src_x, src_y, /* src_x, src_y */
392 0, 0, /* mask_x, mask_y */
393 x1, y1, /* dest_x, dest_y */
395 y2 - y1 /* height */);
398 pixman_region32_subtract(&ec->primary_plane.damage,
399 &ec->primary_plane.damage, damage);
400 cookie = xcb_shm_put_image_checked(c->conn, output->window, output->gc,
401 pixman_image_get_width(output->hw_surface),
402 pixman_image_get_height(output->hw_surface),
404 pixman_image_get_width(output->hw_surface),
405 pixman_image_get_height(output->hw_surface),
406 0, 0, output->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
407 0, output->segment, 0);
408 err = xcb_request_check(c->conn, cookie);
410 weston_log("Failed to put shm image, err: %d\n", err->error_code);
414 wl_event_source_timer_update(output->finish_frame_timer, 10);
418 finish_frame_handler(void *data)
420 struct x11_output *output = data;
424 gettimeofday(&tv, NULL);
425 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
426 weston_output_finish_frame(&output->base, msec);
432 x11_output_deinit_shm(struct x11_compositor *c, struct x11_output *output)
434 xcb_void_cookie_t cookie;
435 xcb_generic_error_t *err;
436 xcb_free_gc(c->conn, output->gc);
438 pixman_image_unref(output->hw_surface);
439 output->hw_surface = NULL;
440 cookie = xcb_shm_detach_checked(c->conn, output->segment);
441 err = xcb_request_check(c->conn, cookie);
443 weston_log("xcb_shm_detach failed, error %d\n", err->error_code);
448 pixman_image_unref(output->shadow_surface);
449 output->shadow_surface = NULL;
450 free(output->shadow_buf);
454 x11_output_destroy(struct weston_output *output_base)
456 struct x11_output *output = (struct x11_output *)output_base;
457 struct x11_compositor *compositor =
458 (struct x11_compositor *)output->base.compositor;
460 wl_list_remove(&output->base.link);
461 wl_event_source_remove(output->finish_frame_timer);
463 if (compositor->use_pixman) {
464 pixman_renderer_output_destroy(output_base);
465 x11_output_deinit_shm(compositor, output);
467 gl_renderer_output_destroy(output_base);
469 xcb_destroy_window(compositor->conn, output->window);
471 weston_output_destroy(&output->base);
477 x11_output_set_wm_protocols(struct x11_compositor *c,
478 struct x11_output *output)
482 list[0] = c->atom.wm_delete_window;
483 xcb_change_property (c->conn,
484 XCB_PROP_MODE_REPLACE,
486 c->atom.wm_protocols,
493 struct wm_normal_hints {
496 int32_t min_width, min_height;
497 int32_t max_width, max_height;
498 int32_t width_inc, height_inc;
499 int32_t min_aspect_x, min_aspect_y;
500 int32_t max_aspect_x, max_aspect_y;
501 int32_t base_width, base_height;
505 #define WM_NORMAL_HINTS_MIN_SIZE 16
506 #define WM_NORMAL_HINTS_MAX_SIZE 32
509 x11_output_set_icon(struct x11_compositor *c,
510 struct x11_output *output, const char *filename)
513 int32_t width, height;
514 pixman_image_t *image;
516 image = load_image(filename);
519 width = pixman_image_get_width(image);
520 height = pixman_image_get_height(image);
521 icon = malloc(width * height * 4 + 8);
523 pixman_image_unref(image);
529 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
530 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
531 c->atom.net_wm_icon, c->atom.cardinal, 32,
532 width * height + 2, icon);
534 pixman_image_unref(image);
538 x11_output_wait_for_map(struct x11_compositor *c, struct x11_output *output)
540 xcb_map_notify_event_t *map_notify;
541 xcb_configure_notify_event_t *configure_notify;
542 xcb_generic_event_t *event;
544 uint8_t response_type;
546 /* This isn't the nicest way to do this. Ideally, we could
547 * just go back to the main loop and once we get the map
548 * notify, we add the output to the compositor. While we do
549 * support output hotplug, we can't start up with no outputs.
550 * We could add the output and then resize once we get the map
551 * notify, but we don't want to start up and immediately
552 * resize the output. */
557 event = xcb_wait_for_event(c->conn);
558 response_type = event->response_type & ~0x80;
560 switch (response_type) {
562 map_notify = (xcb_map_notify_event_t *) event;
563 if (map_notify->window == output->window)
567 case XCB_CONFIGURE_NOTIFY:
569 (xcb_configure_notify_event_t *) event;
571 output->mode.width = configure_notify->width;
572 output->mode.height = configure_notify->height;
578 static xcb_visualtype_t *
579 find_visual_by_id(xcb_screen_t *screen,
582 xcb_depth_iterator_t i;
583 xcb_visualtype_iterator_t j;
584 for (i = xcb_screen_allowed_depths_iterator(screen);
586 xcb_depth_next(&i)) {
587 for (j = xcb_depth_visuals_iterator(i.data);
589 xcb_visualtype_next(&j)) {
590 if (j.data->visual_id == id)
598 get_depth_of_visual(xcb_screen_t *screen,
601 xcb_depth_iterator_t i;
602 xcb_visualtype_iterator_t j;
603 for (i = xcb_screen_allowed_depths_iterator(screen);
605 xcb_depth_next(&i)) {
606 for (j = xcb_depth_visuals_iterator(i.data);
608 xcb_visualtype_next(&j)) {
609 if (j.data->visual_id == id)
610 return i.data->depth;
617 x11_output_init_shm(struct x11_compositor *c, struct x11_output *output,
618 int width, int height)
620 xcb_screen_iterator_t iter;
621 xcb_visualtype_t *visual_type;
622 xcb_format_iterator_t fmt;
623 xcb_void_cookie_t cookie;
624 xcb_generic_error_t *err;
625 int shadow_width, shadow_height;
626 pixman_transform_t transform;
627 const xcb_query_extension_reply_t *ext;
628 int bitsperpixel = 0;
629 pixman_format_code_t pixman_format;
631 /* Check if SHM is available */
632 ext = xcb_get_extension_data(c->conn, &xcb_shm_id);
633 if (ext == NULL || !ext->present) {
635 weston_log("SHM extension is not available\n");
640 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
641 visual_type = find_visual_by_id(iter.data, iter.data->root_visual);
643 weston_log("Failed to lookup visual for root window\n");
647 weston_log("Found visual, bits per value: %d, red_mask: %.8x, green_mask: %.8x, blue_mask: %.8x\n",
648 visual_type->bits_per_rgb_value,
649 visual_type->red_mask,
650 visual_type->green_mask,
651 visual_type->blue_mask);
652 output->depth = get_depth_of_visual(iter.data, iter.data->root_visual);
653 weston_log("Visual depth is %d\n", output->depth);
655 for (fmt = xcb_setup_pixmap_formats_iterator(xcb_get_setup(c->conn));
657 xcb_format_next(&fmt)) {
658 if (fmt.data->depth == output->depth) {
659 bitsperpixel = fmt.data->bits_per_pixel;
663 weston_log("Found format for depth %d, bpp: %d\n",
664 output->depth, bitsperpixel);
666 if (bitsperpixel == 32 &&
667 visual_type->red_mask == 0xff0000 &&
668 visual_type->green_mask == 0x00ff00 &&
669 visual_type->blue_mask == 0x0000ff) {
670 weston_log("Will use x8r8g8b8 format for SHM surfaces\n");
671 pixman_format = PIXMAN_x8r8g8b8;
673 weston_log("Can't find appropriate format for SHM pixmap\n");
679 /* Create SHM segment and attach it */
680 output->shm_id = shmget(IPC_PRIVATE, width * height * (bitsperpixel / 8), IPC_CREAT | S_IRWXU);
681 if (output->shm_id == -1) {
682 weston_log("x11shm: failed to allocate SHM segment\n");
685 output->buf = shmat(output->shm_id, NULL, 0 /* read/write */);
686 if (-1 == (long)output->buf) {
687 weston_log("x11shm: failed to attach SHM segment\n");
690 output->segment = xcb_generate_id(c->conn);
691 cookie = xcb_shm_attach_checked(c->conn, output->segment, output->shm_id, 1);
692 err = xcb_request_check(c->conn, cookie);
694 weston_log("x11shm: xcb_shm_attach error %d\n", err->error_code);
699 shmctl(output->shm_id, IPC_RMID, NULL);
701 /* Now create pixman image */
702 output->hw_surface = pixman_image_create_bits(pixman_format, width, height, output->buf,
703 width * (bitsperpixel / 8));
704 pixman_transform_init_identity(&transform);
705 switch (output->base.transform) {
707 case WL_OUTPUT_TRANSFORM_NORMAL:
708 shadow_width = width;
709 shadow_height = height;
710 pixman_transform_rotate(&transform,
712 pixman_transform_translate(&transform, NULL,
715 case WL_OUTPUT_TRANSFORM_180:
716 shadow_width = width;
717 shadow_height = height;
718 pixman_transform_rotate(&transform,
719 NULL, -pixman_fixed_1, 0);
720 pixman_transform_translate(NULL, &transform,
721 pixman_int_to_fixed(shadow_width),
722 pixman_int_to_fixed(shadow_height));
724 case WL_OUTPUT_TRANSFORM_270:
725 shadow_width = height;
726 shadow_height = width;
727 pixman_transform_rotate(&transform,
728 NULL, 0, pixman_fixed_1);
729 pixman_transform_translate(&transform,
731 pixman_int_to_fixed(shadow_width),
734 case WL_OUTPUT_TRANSFORM_90:
735 shadow_width = height;
736 shadow_height = width;
737 pixman_transform_rotate(&transform,
738 NULL, 0, -pixman_fixed_1);
739 pixman_transform_translate(&transform,
742 pixman_int_to_fixed(shadow_height));
745 output->shadow_buf = malloc(width * height * (bitsperpixel / 8));
746 output->shadow_surface = pixman_image_create_bits(pixman_format, shadow_width, shadow_height,
747 output->shadow_buf, shadow_width * (bitsperpixel / 8));
748 /* No need in transform for normal output */
749 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
750 pixman_image_set_transform(output->shadow_surface, &transform);
752 output->gc = xcb_generate_id(c->conn);
753 xcb_create_gc(c->conn, output->gc, output->window, 0, NULL);
758 static struct x11_output *
759 x11_compositor_create_output(struct x11_compositor *c, int x, int y,
760 int width, int height, int fullscreen,
761 int no_input, char *configured_name,
764 static const char name[] = "Weston Compositor";
765 static const char class[] = "weston-1\0Weston Compositor";
767 struct x11_output *output;
768 xcb_screen_iterator_t iter;
769 struct wm_normal_hints normal_hints;
770 struct wl_event_loop *loop;
771 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
772 xcb_atom_t atom_list[1];
773 uint32_t values[2] = {
774 XCB_EVENT_MASK_EXPOSURE |
775 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
780 sprintf(title, "%s - %s", name, configured_name);
786 XCB_EVENT_MASK_KEY_PRESS |
787 XCB_EVENT_MASK_KEY_RELEASE |
788 XCB_EVENT_MASK_BUTTON_PRESS |
789 XCB_EVENT_MASK_BUTTON_RELEASE |
790 XCB_EVENT_MASK_POINTER_MOTION |
791 XCB_EVENT_MASK_ENTER_WINDOW |
792 XCB_EVENT_MASK_LEAVE_WINDOW |
793 XCB_EVENT_MASK_KEYMAP_STATE |
794 XCB_EVENT_MASK_FOCUS_CHANGE;
796 output = malloc(sizeof *output);
800 memset(output, 0, sizeof *output);
803 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
804 output->mode.width = width;
805 output->mode.height = height;
806 output->mode.refresh = 60000;
807 wl_list_init(&output->base.mode_list);
808 wl_list_insert(&output->base.mode_list, &output->mode.link);
810 values[1] = c->null_cursor;
811 output->window = xcb_generate_id(c->conn);
812 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
813 xcb_create_window(c->conn,
814 XCB_COPY_FROM_PARENT,
820 XCB_WINDOW_CLASS_INPUT_OUTPUT,
821 iter.data->root_visual,
825 atom_list[0] = c->atom.net_wm_state_fullscreen;
826 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE,
828 c->atom.net_wm_state,
830 ARRAY_LENGTH(atom_list), atom_list);
832 /* Don't resize me. */
833 memset(&normal_hints, 0, sizeof normal_hints);
835 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
836 normal_hints.min_width = width;
837 normal_hints.min_height = height;
838 normal_hints.max_width = width;
839 normal_hints.max_height = height;
840 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
841 c->atom.wm_normal_hints,
842 c->atom.wm_size_hints, 32,
843 sizeof normal_hints / 4,
844 (uint8_t *) &normal_hints);
847 /* Set window name. Don't bother with non-EWMH WMs. */
848 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
849 c->atom.net_wm_name, c->atom.utf8_string, 8,
850 strlen(title), title);
851 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
852 c->atom.wm_class, c->atom.string, 8,
853 sizeof class, class);
855 x11_output_set_icon(c, output, DATADIR "/weston/wayland.png");
857 x11_output_set_wm_protocols(c, output);
859 xcb_map_window(c->conn, output->window);
861 x11_output_wait_for_map(c, output);
863 output->base.origin = output->base.current;
865 output->base.repaint = x11_output_repaint_shm;
867 output->base.repaint = x11_output_repaint_gl;
868 output->base.destroy = x11_output_destroy;
869 output->base.assign_planes = NULL;
870 output->base.set_backlight = NULL;
871 output->base.set_dpms = NULL;
872 output->base.switch_mode = NULL;
873 output->base.current = &output->mode;
874 output->base.make = "xwayland";
875 output->base.model = "none";
876 weston_output_init(&output->base, &c->base,
877 x, y, width, height, transform);
880 if (x11_output_init_shm(c, output, width, height) < 0)
882 if (pixman_renderer_output_create(&output->base) < 0) {
883 x11_output_deinit_shm(c, output);
887 if (gl_renderer_output_create(&output->base, output->window) < 0)
891 loop = wl_display_get_event_loop(c->base.wl_display);
892 output->finish_frame_timer =
893 wl_event_loop_add_timer(loop, finish_frame_handler, output);
895 wl_list_insert(c->base.output_list.prev, &output->base.link);
897 weston_log("x11 output %dx%d, window id %d\n",
898 width, height, output->window);
903 static struct x11_output *
904 x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
906 struct x11_output *output;
908 wl_list_for_each(output, &c->base.output_list, base.link) {
909 if (output->window == window)
918 update_xkb_state(struct x11_compositor *c, xcb_xkb_state_notify_event_t *state)
920 xkb_state_update_mask(c->core_seat.xkb_state.state,
921 get_xkb_mod_mask(c, state->baseMods),
922 get_xkb_mod_mask(c, state->latchedMods),
923 get_xkb_mod_mask(c, state->lockedMods),
928 notify_modifiers(&c->core_seat,
929 wl_display_next_serial(c->base.wl_display));
934 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
935 * the best we can do (given that XCB also lacks XI2 support), is to take
936 * the state from the core key events. Unfortunately that only gives us
937 * the effective (i.e. union of depressed/latched/locked) state, and we
938 * need the granularity.
940 * So we still update the state with every key event we see, but also use
941 * the state field from X11 events as a mask so we don't get any stuck
945 update_xkb_state_from_core(struct x11_compositor *c, uint16_t x11_mask)
947 uint32_t mask = get_xkb_mod_mask(c, x11_mask);
948 struct wl_keyboard *keyboard = &c->core_seat.keyboard;
950 xkb_state_update_mask(c->core_seat.xkb_state.state,
951 keyboard->modifiers.mods_depressed & mask,
952 keyboard->modifiers.mods_latched & mask,
953 keyboard->modifiers.mods_locked & mask,
956 (x11_mask >> 13) & 3);
957 notify_modifiers(&c->core_seat,
958 wl_display_next_serial(c->base.wl_display));
962 x11_compositor_deliver_button_event(struct x11_compositor *c,
963 xcb_generic_event_t *event, int state)
965 xcb_button_press_event_t *button_event =
966 (xcb_button_press_event_t *) event;
968 struct x11_output *output;
970 output = x11_compositor_find_output(c, button_event->event);
973 xcb_grab_pointer(c->conn, 0, output->window,
974 XCB_EVENT_MASK_BUTTON_PRESS |
975 XCB_EVENT_MASK_BUTTON_RELEASE |
976 XCB_EVENT_MASK_POINTER_MOTION |
977 XCB_EVENT_MASK_ENTER_WINDOW |
978 XCB_EVENT_MASK_LEAVE_WINDOW,
981 output->window, XCB_CURSOR_NONE,
984 xcb_ungrab_pointer(c->conn, button_event->time);
987 update_xkb_state_from_core(c, button_event->state);
989 switch (button_event->detail) {
991 button = button_event->detail + BTN_LEFT - 1;
1000 /* Axis are measured in pixels, but the xcb events are discrete
1001 * steps. Therefore move the axis by some pixels every step. */
1003 notify_axis(&c->core_seat,
1004 weston_compositor_get_time(),
1005 WL_POINTER_AXIS_VERTICAL_SCROLL,
1006 -DEFAULT_AXIS_STEP_DISTANCE);
1010 notify_axis(&c->core_seat,
1011 weston_compositor_get_time(),
1012 WL_POINTER_AXIS_VERTICAL_SCROLL,
1013 DEFAULT_AXIS_STEP_DISTANCE);
1017 notify_axis(&c->core_seat,
1018 weston_compositor_get_time(),
1019 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
1020 -DEFAULT_AXIS_STEP_DISTANCE);
1024 notify_axis(&c->core_seat,
1025 weston_compositor_get_time(),
1026 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
1027 DEFAULT_AXIS_STEP_DISTANCE);
1031 notify_button(&c->core_seat,
1032 weston_compositor_get_time(), button,
1033 state ? WL_POINTER_BUTTON_STATE_PRESSED :
1034 WL_POINTER_BUTTON_STATE_RELEASED);
1038 x11_output_transform_coordinate(struct x11_output *x11_output,
1039 wl_fixed_t *x, wl_fixed_t *y)
1041 struct weston_output *output = &x11_output->base;
1043 wl_fixed_t width = wl_fixed_from_int(output->width - 1);
1044 wl_fixed_t height = wl_fixed_from_int(output->height - 1);
1046 switch(output->transform) {
1047 case WL_OUTPUT_TRANSFORM_NORMAL:
1052 case WL_OUTPUT_TRANSFORM_90:
1056 case WL_OUTPUT_TRANSFORM_180:
1060 case WL_OUTPUT_TRANSFORM_270:
1064 case WL_OUTPUT_TRANSFORM_FLIPPED:
1068 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1072 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1076 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1082 tx += wl_fixed_from_int(output->x);
1083 ty += wl_fixed_from_int(output->y);
1090 x11_compositor_deliver_motion_event(struct x11_compositor *c,
1091 xcb_generic_event_t *event)
1093 struct x11_output *output;
1095 xcb_motion_notify_event_t *motion_notify =
1096 (xcb_motion_notify_event_t *) event;
1099 update_xkb_state_from_core(c, motion_notify->state);
1100 output = x11_compositor_find_output(c, motion_notify->event);
1101 x = wl_fixed_from_int(motion_notify->event_x);
1102 y = wl_fixed_from_int(motion_notify->event_y);
1103 x11_output_transform_coordinate(output, &x, &y);
1105 notify_motion(&c->core_seat, weston_compositor_get_time(), x, y);
1109 x11_compositor_deliver_enter_event(struct x11_compositor *c,
1110 xcb_generic_event_t *event)
1112 struct x11_output *output;
1115 xcb_enter_notify_event_t *enter_notify =
1116 (xcb_enter_notify_event_t *) event;
1117 if (enter_notify->state >= Button1Mask)
1120 update_xkb_state_from_core(c, enter_notify->state);
1121 output = x11_compositor_find_output(c, enter_notify->event);
1122 x = wl_fixed_from_int(enter_notify->event_x);
1123 y = wl_fixed_from_int(enter_notify->event_y);
1124 x11_output_transform_coordinate(output, &x, &y);
1126 notify_pointer_focus(&c->core_seat, &output->base, x, y);
1130 x11_compositor_next_event(struct x11_compositor *c,
1131 xcb_generic_event_t **event, uint32_t mask)
1133 if (mask & WL_EVENT_READABLE) {
1134 *event = xcb_poll_for_event(c->conn);
1136 #ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
1137 *event = xcb_poll_for_queued_event(c->conn);
1139 *event = xcb_poll_for_event(c->conn);
1143 return *event != NULL;
1147 x11_compositor_handle_event(int fd, uint32_t mask, void *data)
1149 struct x11_compositor *c = data;
1150 struct x11_output *output;
1151 xcb_generic_event_t *event, *prev;
1152 xcb_client_message_event_t *client_message;
1153 xcb_enter_notify_event_t *enter_notify;
1154 xcb_key_press_event_t *key_press, *key_release;
1155 xcb_keymap_notify_event_t *keymap_notify;
1156 xcb_focus_in_event_t *focus_in;
1157 xcb_expose_event_t *expose;
1161 uint8_t response_type;
1166 while (x11_compositor_next_event(c, &event, mask)) {
1167 response_type = event->response_type & ~0x80;
1169 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1170 case XCB_KEY_RELEASE:
1171 /* Suppress key repeat events; this is only used if we
1172 * don't have XCB XKB support. */
1173 key_release = (xcb_key_press_event_t *) prev;
1174 key_press = (xcb_key_press_event_t *) event;
1175 if (response_type == XCB_KEY_PRESS &&
1176 key_release->time == key_press->time &&
1177 key_release->detail == key_press->detail) {
1178 /* Don't deliver the held key release
1179 * event or the new key press event. */
1185 /* Deliver the held key release now
1186 * and fall through and handle the new
1188 update_xkb_state_from_core(c, key_release->state);
1189 notify_key(&c->core_seat,
1190 weston_compositor_get_time(),
1191 key_release->detail - 8,
1192 WL_KEYBOARD_KEY_STATE_RELEASED,
1193 STATE_UPDATE_AUTOMATIC);
1200 assert(response_type == XCB_KEYMAP_NOTIFY);
1201 keymap_notify = (xcb_keymap_notify_event_t *) event;
1203 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
1204 set = keymap_notify->keys[i >> 3] &
1207 k = wl_array_add(&c->keys, sizeof *k);
1212 /* Unfortunately the state only comes with the enter
1213 * event, rather than with the focus event. I'm not
1214 * sure of the exact semantics around it and whether
1215 * we can ensure that we get both? */
1216 notify_keyboard_focus_in(&c->core_seat, &c->keys,
1217 STATE_UPDATE_AUTOMATIC);
1224 /* No previous event held */
1228 switch (response_type) {
1230 key_press = (xcb_key_press_event_t *) event;
1232 update_xkb_state_from_core(c, key_press->state);
1233 notify_key(&c->core_seat,
1234 weston_compositor_get_time(),
1235 key_press->detail - 8,
1236 WL_KEYBOARD_KEY_STATE_PRESSED,
1237 c->has_xkb ? STATE_UPDATE_NONE :
1238 STATE_UPDATE_AUTOMATIC);
1240 case XCB_KEY_RELEASE:
1241 /* If we don't have XKB, we need to use the lame
1242 * autorepeat detection above. */
1247 key_release = (xcb_key_press_event_t *) event;
1248 notify_key(&c->core_seat,
1249 weston_compositor_get_time(),
1250 key_release->detail - 8,
1251 WL_KEYBOARD_KEY_STATE_RELEASED,
1254 case XCB_BUTTON_PRESS:
1255 x11_compositor_deliver_button_event(c, event, 1);
1257 case XCB_BUTTON_RELEASE:
1258 x11_compositor_deliver_button_event(c, event, 0);
1260 case XCB_MOTION_NOTIFY:
1261 x11_compositor_deliver_motion_event(c, event);
1265 expose = (xcb_expose_event_t *) event;
1266 output = x11_compositor_find_output(c, expose->window);
1267 weston_output_schedule_repaint(&output->base);
1270 case XCB_ENTER_NOTIFY:
1271 x11_compositor_deliver_enter_event(c, event);
1274 case XCB_LEAVE_NOTIFY:
1275 enter_notify = (xcb_enter_notify_event_t *) event;
1276 if (enter_notify->state >= Button1Mask)
1279 update_xkb_state_from_core(c, enter_notify->state);
1280 notify_pointer_focus(&c->core_seat, NULL, 0, 0);
1283 case XCB_CLIENT_MESSAGE:
1284 client_message = (xcb_client_message_event_t *) event;
1285 atom = client_message->data.data32[0];
1286 if (atom == c->atom.wm_delete_window)
1287 wl_display_terminate(c->base.wl_display);
1291 focus_in = (xcb_focus_in_event_t *) event;
1292 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1299 focus_in = (xcb_focus_in_event_t *) event;
1300 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1301 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
1303 notify_keyboard_focus_out(&c->core_seat);
1312 response_type == c->xkb_event_base) {
1313 xcb_xkb_state_notify_event_t *state =
1314 (xcb_xkb_state_notify_event_t *) event;
1315 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
1316 update_xkb_state(c, state);
1325 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1326 case XCB_KEY_RELEASE:
1327 key_release = (xcb_key_press_event_t *) prev;
1328 update_xkb_state_from_core(c, key_release->state);
1329 notify_key(&c->core_seat,
1330 weston_compositor_get_time(),
1331 key_release->detail - 8,
1332 WL_KEYBOARD_KEY_STATE_RELEASED,
1333 STATE_UPDATE_AUTOMATIC);
1344 #define F(field) offsetof(struct x11_compositor, field)
1347 x11_compositor_get_resources(struct x11_compositor *c)
1349 static const struct { const char *name; int offset; } atoms[] = {
1350 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1351 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1352 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1353 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
1354 { "WM_CLASS", F(atom.wm_class) },
1355 { "_NET_WM_NAME", F(atom.net_wm_name) },
1356 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1357 { "_NET_WM_STATE", F(atom.net_wm_state) },
1358 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1359 { "STRING", F(atom.string) },
1360 { "UTF8_STRING", F(atom.utf8_string) },
1361 { "CARDINAL", F(atom.cardinal) },
1362 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
1365 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1366 xcb_intern_atom_reply_t *reply;
1367 xcb_pixmap_t pixmap;
1370 uint8_t data[] = { 0, 0, 0, 0 };
1372 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1373 cookies[i] = xcb_intern_atom (c->conn, 0,
1374 strlen(atoms[i].name),
1377 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1378 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
1379 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
1383 pixmap = xcb_generate_id(c->conn);
1384 gc = xcb_generate_id(c->conn);
1385 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
1386 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
1387 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
1388 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
1389 c->null_cursor = xcb_generate_id(c->conn);
1390 xcb_create_cursor (c->conn, c->null_cursor,
1391 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
1392 xcb_free_gc(c->conn, gc);
1393 xcb_free_pixmap(c->conn, pixmap);
1397 x11_restore(struct weston_compositor *ec)
1402 x11_free_configured_output(struct x11_configured_output *output)
1409 x11_destroy(struct weston_compositor *ec)
1411 struct x11_compositor *compositor = (struct x11_compositor *)ec;
1412 struct x11_configured_output *o, *n;
1414 wl_list_for_each_safe(o, n, &configured_output_list, link)
1415 x11_free_configured_output(o);
1417 wl_event_source_remove(compositor->xcb_source);
1418 x11_input_destroy(compositor);
1420 weston_compositor_shutdown(ec); /* destroys outputs, too */
1422 if (compositor->use_pixman)
1423 pixman_renderer_destroy(ec);
1425 gl_renderer_destroy(ec);
1427 XCloseDisplay(compositor->dpy);
1431 static struct weston_compositor *
1432 x11_compositor_create(struct wl_display *display,
1436 int argc, char *argv[], const char *config_file)
1438 struct x11_compositor *c;
1439 struct x11_configured_output *o;
1440 struct x11_output *output;
1441 xcb_screen_iterator_t s;
1442 int i, x = 0, output_count = 0;
1443 int width, height, count;
1445 weston_log("initializing x11 backend\n");
1447 c = malloc(sizeof *c);
1451 memset(c, 0, sizeof *c);
1453 if (weston_compositor_init(&c->base, display, argc, argv,
1457 c->dpy = XOpenDisplay(NULL);
1461 c->conn = XGetXCBConnection(c->dpy);
1462 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
1464 if (xcb_connection_has_error(c->conn))
1467 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
1469 wl_array_init(&c->keys);
1471 x11_compositor_get_resources(c);
1473 c->base.wl_display = display;
1474 c->use_pixman = use_pixman;
1475 if (c->use_pixman) {
1476 if (pixman_renderer_init(&c->base) < 0)
1480 if (gl_renderer_create(&c->base, c->dpy, gl_renderer_opaque_attribs,
1484 weston_log("Using %s renderer\n", use_pixman ? "pixman" : "gl");
1486 c->base.destroy = x11_destroy;
1487 c->base.restore = x11_restore;
1489 if (x11_input_create(c, no_input) < 0)
1492 width = option_width ? option_width : 1024;
1493 height = option_height ? option_height : 640;
1494 count = option_count ? option_count : 1;
1496 wl_list_for_each(o, &configured_output_list, link) {
1497 output = x11_compositor_create_output(c, x, 0,
1498 option_width ? width :
1500 option_height ? height :
1502 fullscreen, no_input,
1503 o->name, o->transform);
1507 x = pixman_region32_extents(&output->base.region)->x2;
1510 if (option_count && output_count >= option_count)
1514 for (i = output_count; i < count; i++) {
1515 output = x11_compositor_create_output(c, x, 0, width, height,
1516 fullscreen, no_input, NULL,
1517 WL_OUTPUT_TRANSFORM_NORMAL);
1520 x = pixman_region32_extents(&output->base.region)->x2;
1524 wl_event_loop_add_fd(c->base.input_loop,
1525 xcb_get_file_descriptor(c->conn),
1527 x11_compositor_handle_event, c);
1528 wl_event_source_check(c->xcb_source);
1533 x11_input_destroy(c);
1536 pixman_renderer_destroy(&c->base);
1538 gl_renderer_destroy(&c->base);
1540 XCloseDisplay(c->dpy);
1547 x11_output_set_transform(struct x11_configured_output *output)
1549 if (!output_transform) {
1550 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1554 if (!strcmp(output_transform, "normal"))
1555 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1556 else if (!strcmp(output_transform, "90"))
1557 output->transform = WL_OUTPUT_TRANSFORM_90;
1558 else if (!strcmp(output_transform, "180"))
1559 output->transform = WL_OUTPUT_TRANSFORM_180;
1560 else if (!strcmp(output_transform, "270"))
1561 output->transform = WL_OUTPUT_TRANSFORM_270;
1562 else if (!strcmp(output_transform, "flipped"))
1563 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
1564 else if (!strcmp(output_transform, "flipped-90"))
1565 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
1566 else if (!strcmp(output_transform, "flipped-180"))
1567 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
1568 else if (!strcmp(output_transform, "flipped-270"))
1569 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
1571 weston_log("Invalid transform \"%s\" for output %s\n",
1572 output_transform, output_name);
1573 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1578 output_section_done(void *data)
1580 struct x11_configured_output *output;
1582 output = malloc(sizeof *output);
1584 if (!output || !output_name || (output_name[0] != 'X') ||
1585 (!output_mode && !output_transform)) {
1593 output->name = output_name;
1596 if (sscanf(output_mode, "%dx%d", &output->width,
1597 &output->height) != 2) {
1598 weston_log("Invalid mode \"%s\" for output %s\n",
1599 output_mode, output_name);
1600 x11_free_configured_output(output);
1604 output->width = 1024;
1605 output->height = 640;
1608 x11_output_set_transform(output);
1610 wl_list_insert(configured_output_list.prev, &output->link);
1615 if (output_transform)
1616 free(output_transform);
1618 output_transform = NULL;
1621 WL_EXPORT struct weston_compositor *
1622 backend_init(struct wl_display *display, int argc, char *argv[],
1623 const char *config_file)
1629 const struct weston_option x11_options[] = {
1630 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1631 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
1632 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &fullscreen },
1633 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
1634 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1635 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1638 parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
1640 wl_list_init(&configured_output_list);
1642 const struct config_key x11_config_keys[] = {
1643 { "name", CONFIG_KEY_STRING, &output_name },
1644 { "mode", CONFIG_KEY_STRING, &output_mode },
1645 { "transform", CONFIG_KEY_STRING, &output_transform },
1648 const struct config_section config_section[] = {
1649 { "output", x11_config_keys,
1650 ARRAY_LENGTH(x11_config_keys), output_section_done },
1653 parse_config_file(config_file, config_section,
1654 ARRAY_LENGTH(config_section), NULL);
1656 return x11_compositor_create(display,
1660 argc, argv, config_file);