2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2010-2011 Intel Corporation
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36 #include <linux/input.h>
44 #include <X11/Xlib-xcb.h>
46 #include <xkbcommon/xkbcommon.h>
48 #include <GLES2/gl2.h>
51 #include "compositor.h"
52 #include "../shared/config-parser.h"
53 #include "../shared/image-loader.h"
55 #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
57 static char *output_name;
58 static char *output_mode;
59 static char *output_transform;
60 static int option_width;
61 static int option_height;
62 static int option_count;
63 static struct wl_list configured_output_list;
65 struct x11_configured_output {
72 struct x11_compositor {
73 struct weston_compositor base;
76 xcb_connection_t *conn;
78 xcb_cursor_t null_cursor;
80 struct wl_event_source *xcb_source;
81 struct xkb_keymap *xkb_keymap;
83 uint8_t xkb_event_base;
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;
90 xcb_atom_t wm_protocols;
91 xcb_atom_t wm_normal_hints;
92 xcb_atom_t wm_size_hints;
93 xcb_atom_t wm_delete_window;
95 xcb_atom_t net_wm_name;
96 xcb_atom_t net_wm_icon;
97 xcb_atom_t net_wm_state;
98 xcb_atom_t net_wm_state_fullscreen;
100 xcb_atom_t utf8_string;
102 xcb_atom_t xkb_names;
107 struct weston_output base;
110 struct weston_mode mode;
111 struct wl_event_source *finish_frame_timer;
114 static struct xkb_keymap *
115 x11_compositor_get_keymap(struct x11_compositor *c)
117 xcb_get_property_cookie_t cookie;
118 xcb_get_property_reply_t *reply;
119 struct xkb_rule_names names;
120 struct xkb_keymap *ret;
121 const char *value_all, *value_part;
122 int length_all, length_part;
124 memset(&names, 0, sizeof(names));
126 cookie = xcb_get_property(c->conn, 0, c->screen->root,
127 c->atom.xkb_names, c->atom.string, 0, 1024);
128 reply = xcb_get_property_reply(c->conn, cookie, NULL);
132 value_all = xcb_get_property_value(reply);
133 length_all = xcb_get_property_value_length(reply);
134 value_part = value_all;
136 #define copy_prop_value(to) \
137 length_part = strlen(value_part); \
138 if (value_part + length_part < (value_all + length_all) && \
140 names.to = value_part; \
141 value_part += length_part + 1;
143 copy_prop_value(rules);
144 copy_prop_value(model);
145 copy_prop_value(layout);
146 copy_prop_value(variant);
147 copy_prop_value(options);
148 #undef copy_prop_value
150 ret = xkb_map_new_from_names(c->base.xkb_context, &names, 0);
157 get_xkb_mod_mask(struct x11_compositor *c, uint32_t in)
159 struct weston_xkb_info *info = &c->core_seat.xkb_info;
162 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
163 ret |= (1 << info->shift_mod);
164 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
165 ret |= (1 << info->caps_mod);
166 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
167 ret |= (1 << info->ctrl_mod);
168 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
169 ret |= (1 << info->alt_mod);
170 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
171 ret |= (1 << info->mod2_mod);
172 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
173 ret |= (1 << info->mod3_mod);
174 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
175 ret |= (1 << info->super_mod);
176 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
177 ret |= (1 << info->mod5_mod);
183 x11_compositor_setup_xkb(struct x11_compositor *c)
186 weston_log("XCB-XKB not available during build\n");
188 c->xkb_event_base = 0;
191 const xcb_query_extension_reply_t *ext;
192 xcb_generic_error_t *error;
193 xcb_void_cookie_t select;
194 xcb_xkb_use_extension_cookie_t use_ext;
195 xcb_xkb_use_extension_reply_t *use_ext_reply;
196 xcb_xkb_per_client_flags_cookie_t pcf;
197 xcb_xkb_per_client_flags_reply_t *pcf_reply;
198 xcb_xkb_get_state_cookie_t state;
199 xcb_xkb_get_state_reply_t *state_reply;
202 c->xkb_event_base = 0;
204 ext = xcb_get_extension_data(c->conn, &xcb_xkb_id);
206 weston_log("XKB extension not available on host X11 server\n");
209 c->xkb_event_base = ext->first_event;
211 select = xcb_xkb_select_events_checked(c->conn,
212 XCB_XKB_ID_USE_CORE_KBD,
213 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
215 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
219 error = xcb_request_check(c->conn, select);
221 weston_log("error: failed to select for XKB state events\n");
226 use_ext = xcb_xkb_use_extension(c->conn,
227 XCB_XKB_MAJOR_VERSION,
228 XCB_XKB_MINOR_VERSION);
229 use_ext_reply = xcb_xkb_use_extension_reply(c->conn, use_ext, NULL);
230 if (!use_ext_reply) {
231 weston_log("couldn't start using XKB extension\n");
235 if (!use_ext_reply->supported) {
236 weston_log("XKB extension version on the server is too old "
237 "(want %d.%d, has %d.%d)\n",
238 XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION,
239 use_ext_reply->serverMajor, use_ext_reply->serverMinor);
245 pcf = xcb_xkb_per_client_flags(c->conn,
246 XCB_XKB_ID_USE_CORE_KBD,
247 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
248 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
252 pcf_reply = xcb_xkb_per_client_flags_reply(c->conn, pcf, NULL);
254 !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT)) {
255 weston_log("failed to set XKB per-client flags, not using "
256 "detectable repeat\n");
262 state = xcb_xkb_get_state(c->conn, XCB_XKB_ID_USE_CORE_KBD);
263 state_reply = xcb_xkb_get_state_reply(c->conn, state, NULL);
265 weston_log("failed to get initial XKB state\n");
269 xkb_state_update_mask(c->core_seat.xkb_state.state,
270 get_xkb_mod_mask(c, state_reply->baseMods),
271 get_xkb_mod_mask(c, state_reply->latchedMods),
272 get_xkb_mod_mask(c, state_reply->lockedMods),
284 x11_input_create(struct x11_compositor *c, int no_input)
286 struct xkb_keymap *keymap;
288 weston_seat_init(&c->core_seat, &c->base);
293 weston_seat_init_pointer(&c->core_seat);
295 keymap = x11_compositor_get_keymap(c);
296 weston_seat_init_keyboard(&c->core_seat, keymap);
298 xkb_map_unref(keymap);
300 x11_compositor_setup_xkb(c);
306 x11_input_destroy(struct x11_compositor *compositor)
308 weston_seat_release(&compositor->core_seat);
312 x11_compositor_init_egl(struct x11_compositor *c)
316 EGLint config_attribs[] = {
317 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
321 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
325 c->base.egl_display = eglGetDisplay(c->dpy);
326 if (c->base.egl_display == NULL) {
327 weston_log("failed to create display\n");
331 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
332 weston_log("failed to initialize display\n");
336 if (!eglChooseConfig(c->base.egl_display, config_attribs,
337 &c->base.egl_config, 1, &n) || n == 0) {
338 weston_log("failed to choose config: %d\n", n);
346 x11_compositor_fini_egl(struct x11_compositor *compositor)
348 gles2_renderer_destroy(&compositor->base);
350 eglMakeCurrent(compositor->base.egl_display,
351 EGL_NO_SURFACE, EGL_NO_SURFACE,
354 eglTerminate(compositor->base.egl_display);
359 x11_output_repaint(struct weston_output *output_base,
360 pixman_region32_t *damage)
362 struct x11_output *output = (struct x11_output *)output_base;
363 struct weston_compositor *ec = output->base.compositor;
365 ec->renderer->repaint_output(output_base, damage);
367 pixman_region32_subtract(&ec->primary_plane.damage,
368 &ec->primary_plane.damage, damage);
370 wl_event_source_timer_update(output->finish_frame_timer, 10);
374 finish_frame_handler(void *data)
376 struct x11_output *output = data;
380 gettimeofday(&tv, NULL);
381 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
382 weston_output_finish_frame(&output->base, msec);
388 x11_output_destroy(struct weston_output *output_base)
390 struct x11_output *output = (struct x11_output *)output_base;
391 struct x11_compositor *compositor =
392 (struct x11_compositor *)output->base.compositor;
394 wl_list_remove(&output->base.link);
395 wl_event_source_remove(output->finish_frame_timer);
397 eglDestroySurface(compositor->base.egl_display,
398 output->base.egl_surface);
400 xcb_destroy_window(compositor->conn, output->window);
402 weston_output_destroy(&output->base);
408 x11_output_set_wm_protocols(struct x11_output *output)
411 struct x11_compositor *c =
412 (struct x11_compositor *) output->base.compositor;
414 list[0] = c->atom.wm_delete_window;
415 xcb_change_property (c->conn,
416 XCB_PROP_MODE_REPLACE,
418 c->atom.wm_protocols,
426 x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state)
428 xcb_client_message_event_t event;
429 struct x11_compositor *c =
430 (struct x11_compositor *) output->base.compositor;
431 xcb_screen_iterator_t iter;
433 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
434 #define _NET_WM_STATE_ADD 1 /* add/set property */
435 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
437 memset(&event, 0, sizeof event);
438 event.response_type = XCB_CLIENT_MESSAGE;
440 event.window = output->window;
441 event.type = c->atom.net_wm_state;
443 event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
444 event.data.data32[1] = state;
445 event.data.data32[2] = 0;
446 event.data.data32[3] = 0;
447 event.data.data32[4] = 0;
449 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
450 xcb_send_event(c->conn, 0, iter.data->root,
451 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
452 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
457 struct wm_normal_hints {
460 int32_t min_width, min_height;
461 int32_t max_width, max_height;
462 int32_t width_inc, height_inc;
463 int32_t min_aspect_x, min_aspect_y;
464 int32_t max_aspect_x, max_aspect_y;
465 int32_t base_width, base_height;
469 #define WM_NORMAL_HINTS_MIN_SIZE 16
470 #define WM_NORMAL_HINTS_MAX_SIZE 32
473 x11_output_set_icon(struct x11_compositor *c,
474 struct x11_output *output, const char *filename)
477 int32_t width, height;
478 pixman_image_t *image;
480 image = load_image(filename);
483 width = pixman_image_get_width(image);
484 height = pixman_image_get_height(image);
485 icon = malloc(width * height * 4 + 8);
487 pixman_image_unref(image);
493 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
494 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
495 c->atom.net_wm_icon, c->atom.cardinal, 32,
496 width * height + 2, icon);
498 pixman_image_unref(image);
501 static struct x11_output *
502 x11_compositor_create_output(struct x11_compositor *c, int x, int y,
503 int width, int height, int fullscreen,
504 int no_input, char *configured_name,
507 static const char name[] = "Weston Compositor";
508 static const char class[] = "weston-1\0Weston Compositor";
510 struct x11_output *output;
511 xcb_screen_iterator_t iter;
512 struct wm_normal_hints normal_hints;
513 struct wl_event_loop *loop;
514 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
515 uint32_t values[2] = {
516 XCB_EVENT_MASK_EXPOSURE |
517 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
522 sprintf(title, "%s - %s", name, configured_name);
528 XCB_EVENT_MASK_KEY_PRESS |
529 XCB_EVENT_MASK_KEY_RELEASE |
530 XCB_EVENT_MASK_BUTTON_PRESS |
531 XCB_EVENT_MASK_BUTTON_RELEASE |
532 XCB_EVENT_MASK_POINTER_MOTION |
533 XCB_EVENT_MASK_ENTER_WINDOW |
534 XCB_EVENT_MASK_LEAVE_WINDOW |
535 XCB_EVENT_MASK_KEYMAP_STATE |
536 XCB_EVENT_MASK_FOCUS_CHANGE;
538 output = malloc(sizeof *output);
542 memset(output, 0, sizeof *output);
545 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
546 output->mode.width = width;
547 output->mode.height = height;
548 output->mode.refresh = 60000;
549 wl_list_init(&output->base.mode_list);
550 wl_list_insert(&output->base.mode_list, &output->mode.link);
552 output->base.current = &output->mode;
553 output->base.make = "xwayland";
554 output->base.model = "none";
555 weston_output_init(&output->base, &c->base,
556 x, y, width, height, transform);
558 values[1] = c->null_cursor;
559 output->window = xcb_generate_id(c->conn);
560 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
561 xcb_create_window(c->conn,
562 XCB_COPY_FROM_PARENT,
568 XCB_WINDOW_CLASS_INPUT_OUTPUT,
569 iter.data->root_visual,
572 /* Don't resize me. */
573 memset(&normal_hints, 0, sizeof normal_hints);
575 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
576 normal_hints.min_width = width;
577 normal_hints.min_height = height;
578 normal_hints.max_width = width;
579 normal_hints.max_height = height;
580 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
581 c->atom.wm_normal_hints,
582 c->atom.wm_size_hints, 32,
583 sizeof normal_hints / 4,
584 (uint8_t *) &normal_hints);
586 /* Set window name. Don't bother with non-EWMH WMs. */
587 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
588 c->atom.net_wm_name, c->atom.utf8_string, 8,
589 strlen(title), title);
590 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
591 c->atom.wm_class, c->atom.string, 8,
592 sizeof class, class);
594 x11_output_set_icon(c, output, DATADIR "/weston/wayland.png");
596 xcb_map_window(c->conn, output->window);
598 x11_output_set_wm_protocols(output);
601 x11_output_change_state(output, 1,
602 c->atom.net_wm_state_fullscreen);
604 output->base.egl_surface =
605 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
606 output->window, NULL);
607 if (!output->base.egl_surface) {
608 weston_log("failed to create window surface\n");
612 loop = wl_display_get_event_loop(c->base.wl_display);
613 output->finish_frame_timer =
614 wl_event_loop_add_timer(loop, finish_frame_handler, output);
616 output->base.origin = output->base.current;
617 output->base.repaint = x11_output_repaint;
618 output->base.destroy = x11_output_destroy;
619 output->base.assign_planes = NULL;
620 output->base.set_backlight = NULL;
621 output->base.set_dpms = NULL;
622 output->base.switch_mode = NULL;
624 wl_list_insert(c->base.output_list.prev, &output->base.link);
626 weston_log("x11 output %dx%d, window id %d\n",
627 width, height, output->window);
632 static struct x11_output *
633 x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
635 struct x11_output *output;
637 wl_list_for_each(output, &c->base.output_list, base.link) {
638 if (output->window == window)
647 update_xkb_state(struct x11_compositor *c, xcb_xkb_state_notify_event_t *state)
649 xkb_state_update_mask(c->core_seat.xkb_state.state,
650 get_xkb_mod_mask(c, state->baseMods),
651 get_xkb_mod_mask(c, state->latchedMods),
652 get_xkb_mod_mask(c, state->lockedMods),
657 notify_modifiers(&c->core_seat,
658 wl_display_next_serial(c->base.wl_display));
663 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
664 * the best we can do (given that XCB also lacks XI2 support), is to take
665 * the state from the core key events. Unfortunately that only gives us
666 * the effective (i.e. union of depressed/latched/locked) state, and we
667 * need the granularity.
669 * So we still update the state with every key event we see, but also use
670 * the state field from X11 events as a mask so we don't get any stuck
674 update_xkb_state_from_core(struct x11_compositor *c, uint16_t x11_mask)
676 uint32_t mask = get_xkb_mod_mask(c, x11_mask);
677 struct wl_keyboard *keyboard = &c->core_seat.keyboard;
679 xkb_state_update_mask(c->core_seat.xkb_state.state,
680 keyboard->modifiers.mods_depressed & mask,
681 keyboard->modifiers.mods_latched & mask,
682 keyboard->modifiers.mods_locked & mask,
685 (x11_mask >> 13) & 3);
686 notify_modifiers(&c->core_seat,
687 wl_display_next_serial(c->base.wl_display));
691 x11_compositor_deliver_button_event(struct x11_compositor *c,
692 xcb_generic_event_t *event, int state)
694 xcb_button_press_event_t *button_event =
695 (xcb_button_press_event_t *) event;
697 struct x11_output *output;
699 output = x11_compositor_find_output(c, button_event->event);
702 xcb_grab_pointer(c->conn, 0, output->window,
703 XCB_EVENT_MASK_BUTTON_PRESS |
704 XCB_EVENT_MASK_BUTTON_RELEASE |
705 XCB_EVENT_MASK_POINTER_MOTION |
706 XCB_EVENT_MASK_ENTER_WINDOW |
707 XCB_EVENT_MASK_LEAVE_WINDOW,
710 output->window, XCB_CURSOR_NONE,
713 xcb_ungrab_pointer(c->conn, button_event->time);
716 update_xkb_state_from_core(c, button_event->state);
718 switch (button_event->detail) {
720 button = button_event->detail + BTN_LEFT - 1;
729 /* Axis are measured in pixels, but the xcb events are discrete
730 * steps. Therefore move the axis by some pixels every step. */
732 notify_axis(&c->core_seat,
733 weston_compositor_get_time(),
734 WL_POINTER_AXIS_VERTICAL_SCROLL,
735 -DEFAULT_AXIS_STEP_DISTANCE);
739 notify_axis(&c->core_seat,
740 weston_compositor_get_time(),
741 WL_POINTER_AXIS_VERTICAL_SCROLL,
742 DEFAULT_AXIS_STEP_DISTANCE);
746 notify_axis(&c->core_seat,
747 weston_compositor_get_time(),
748 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
749 -DEFAULT_AXIS_STEP_DISTANCE);
753 notify_axis(&c->core_seat,
754 weston_compositor_get_time(),
755 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
756 DEFAULT_AXIS_STEP_DISTANCE);
760 notify_button(&c->core_seat,
761 weston_compositor_get_time(), button,
762 state ? WL_POINTER_BUTTON_STATE_PRESSED :
763 WL_POINTER_BUTTON_STATE_RELEASED);
767 x11_output_transform_coordinate(struct x11_output *x11_output,
768 wl_fixed_t *x, wl_fixed_t *y)
770 struct weston_output *output = &x11_output->base;
772 wl_fixed_t width = wl_fixed_from_int(output->width - 1);
773 wl_fixed_t height = wl_fixed_from_int(output->height - 1);
775 switch(output->transform) {
776 case WL_OUTPUT_TRANSFORM_NORMAL:
781 case WL_OUTPUT_TRANSFORM_90:
785 case WL_OUTPUT_TRANSFORM_180:
789 case WL_OUTPUT_TRANSFORM_270:
793 case WL_OUTPUT_TRANSFORM_FLIPPED:
797 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
801 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
805 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
811 tx += wl_fixed_from_int(output->x);
812 ty += wl_fixed_from_int(output->y);
819 x11_compositor_deliver_motion_event(struct x11_compositor *c,
820 xcb_generic_event_t *event)
822 struct x11_output *output;
824 xcb_motion_notify_event_t *motion_notify =
825 (xcb_motion_notify_event_t *) event;
828 update_xkb_state_from_core(c, motion_notify->state);
829 output = x11_compositor_find_output(c, motion_notify->event);
830 x = wl_fixed_from_int(motion_notify->event_x);
831 y = wl_fixed_from_int(motion_notify->event_y);
832 x11_output_transform_coordinate(output, &x, &y);
834 notify_motion(&c->core_seat, weston_compositor_get_time(), x, y);
838 x11_compositor_deliver_enter_event(struct x11_compositor *c,
839 xcb_generic_event_t *event)
841 struct x11_output *output;
844 xcb_enter_notify_event_t *enter_notify =
845 (xcb_enter_notify_event_t *) event;
846 if (enter_notify->state >= Button1Mask)
849 update_xkb_state_from_core(c, enter_notify->state);
850 output = x11_compositor_find_output(c, enter_notify->event);
851 x = wl_fixed_from_int(enter_notify->event_x);
852 y = wl_fixed_from_int(enter_notify->event_y);
853 x11_output_transform_coordinate(output, &x, &y);
855 notify_pointer_focus(&c->core_seat, &output->base, x, y);
859 x11_compositor_next_event(struct x11_compositor *c,
860 xcb_generic_event_t **event, uint32_t mask)
862 if (mask & WL_EVENT_READABLE) {
863 *event = xcb_poll_for_event(c->conn);
865 #ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
866 *event = xcb_poll_for_queued_event(c->conn);
868 *event = xcb_poll_for_event(c->conn);
872 return *event != NULL;
876 x11_compositor_handle_event(int fd, uint32_t mask, void *data)
878 struct x11_compositor *c = data;
879 struct x11_output *output;
880 xcb_generic_event_t *event, *prev;
881 xcb_client_message_event_t *client_message;
882 xcb_enter_notify_event_t *enter_notify;
883 xcb_key_press_event_t *key_press, *key_release;
884 xcb_keymap_notify_event_t *keymap_notify;
885 xcb_focus_in_event_t *focus_in;
886 xcb_expose_event_t *expose;
890 uint8_t response_type;
895 while (x11_compositor_next_event(c, &event, mask)) {
896 response_type = event->response_type & ~0x80;
898 switch (prev ? prev->response_type & ~0x80 : 0x80) {
899 case XCB_KEY_RELEASE:
900 /* Suppress key repeat events; this is only used if we
901 * don't have XCB XKB support. */
902 key_release = (xcb_key_press_event_t *) prev;
903 key_press = (xcb_key_press_event_t *) event;
904 if (response_type == XCB_KEY_PRESS &&
905 key_release->time == key_press->time &&
906 key_release->detail == key_press->detail) {
907 /* Don't deliver the held key release
908 * event or the new key press event. */
914 /* Deliver the held key release now
915 * and fall through and handle the new
917 update_xkb_state_from_core(c, key_release->state);
918 notify_key(&c->core_seat,
919 weston_compositor_get_time(),
920 key_release->detail - 8,
921 WL_KEYBOARD_KEY_STATE_RELEASED,
922 STATE_UPDATE_AUTOMATIC);
929 assert(response_type == XCB_KEYMAP_NOTIFY);
930 keymap_notify = (xcb_keymap_notify_event_t *) event;
932 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
933 set = keymap_notify->keys[i >> 3] &
936 k = wl_array_add(&c->keys, sizeof *k);
941 /* Unfortunately the state only comes with the enter
942 * event, rather than with the focus event. I'm not
943 * sure of the exact semantics around it and whether
944 * we can ensure that we get both? */
945 notify_keyboard_focus_in(&c->core_seat, &c->keys,
946 STATE_UPDATE_AUTOMATIC);
953 /* No previous event held */
957 switch (response_type) {
959 key_press = (xcb_key_press_event_t *) event;
961 update_xkb_state_from_core(c, key_press->state);
962 notify_key(&c->core_seat,
963 weston_compositor_get_time(),
964 key_press->detail - 8,
965 WL_KEYBOARD_KEY_STATE_PRESSED,
966 c->has_xkb ? STATE_UPDATE_NONE :
967 STATE_UPDATE_AUTOMATIC);
969 case XCB_KEY_RELEASE:
970 /* If we don't have XKB, we need to use the lame
971 * autorepeat detection above. */
976 key_release = (xcb_key_press_event_t *) event;
977 notify_key(&c->core_seat,
978 weston_compositor_get_time(),
979 key_release->detail - 8,
980 WL_KEYBOARD_KEY_STATE_RELEASED,
983 case XCB_BUTTON_PRESS:
984 x11_compositor_deliver_button_event(c, event, 1);
986 case XCB_BUTTON_RELEASE:
987 x11_compositor_deliver_button_event(c, event, 0);
989 case XCB_MOTION_NOTIFY:
990 x11_compositor_deliver_motion_event(c, event);
994 expose = (xcb_expose_event_t *) event;
995 output = x11_compositor_find_output(c, expose->window);
996 weston_output_schedule_repaint(&output->base);
999 case XCB_ENTER_NOTIFY:
1000 x11_compositor_deliver_enter_event(c, event);
1003 case XCB_LEAVE_NOTIFY:
1004 enter_notify = (xcb_enter_notify_event_t *) event;
1005 if (enter_notify->state >= Button1Mask)
1008 update_xkb_state_from_core(c, enter_notify->state);
1009 notify_pointer_focus(&c->core_seat, NULL, 0, 0);
1012 case XCB_CLIENT_MESSAGE:
1013 client_message = (xcb_client_message_event_t *) event;
1014 atom = client_message->data.data32[0];
1015 if (atom == c->atom.wm_delete_window)
1016 wl_display_terminate(c->base.wl_display);
1020 focus_in = (xcb_focus_in_event_t *) event;
1021 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1028 focus_in = (xcb_focus_in_event_t *) event;
1029 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1030 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
1032 notify_keyboard_focus_out(&c->core_seat);
1041 response_type == c->xkb_event_base) {
1042 xcb_xkb_state_notify_event_t *state =
1043 (xcb_xkb_state_notify_event_t *) event;
1044 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
1045 update_xkb_state(c, state);
1054 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1055 case XCB_KEY_RELEASE:
1056 key_release = (xcb_key_press_event_t *) prev;
1057 update_xkb_state_from_core(c, key_release->state);
1058 notify_key(&c->core_seat,
1059 weston_compositor_get_time(),
1060 key_release->detail - 8,
1061 WL_KEYBOARD_KEY_STATE_RELEASED,
1062 STATE_UPDATE_AUTOMATIC);
1073 #define F(field) offsetof(struct x11_compositor, field)
1076 x11_compositor_get_resources(struct x11_compositor *c)
1078 static const struct { const char *name; int offset; } atoms[] = {
1079 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1080 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1081 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1082 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
1083 { "WM_CLASS", F(atom.wm_class) },
1084 { "_NET_WM_NAME", F(atom.net_wm_name) },
1085 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1086 { "_NET_WM_STATE", F(atom.net_wm_state) },
1087 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1088 { "STRING", F(atom.string) },
1089 { "UTF8_STRING", F(atom.utf8_string) },
1090 { "CARDINAL", F(atom.cardinal) },
1091 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
1094 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1095 xcb_intern_atom_reply_t *reply;
1096 xcb_pixmap_t pixmap;
1099 uint8_t data[] = { 0, 0, 0, 0 };
1101 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1102 cookies[i] = xcb_intern_atom (c->conn, 0,
1103 strlen(atoms[i].name),
1106 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1107 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
1108 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
1112 pixmap = xcb_generate_id(c->conn);
1113 gc = xcb_generate_id(c->conn);
1114 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
1115 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
1116 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
1117 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
1118 c->null_cursor = xcb_generate_id(c->conn);
1119 xcb_create_cursor (c->conn, c->null_cursor,
1120 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
1121 xcb_free_gc(c->conn, gc);
1122 xcb_free_pixmap(c->conn, pixmap);
1126 x11_restore(struct weston_compositor *ec)
1131 x11_free_configured_output(struct x11_configured_output *output)
1138 x11_destroy(struct weston_compositor *ec)
1140 struct x11_compositor *compositor = (struct x11_compositor *)ec;
1141 struct x11_configured_output *o, *n;
1143 wl_list_for_each_safe(o, n, &configured_output_list, link)
1144 x11_free_configured_output(o);
1146 wl_event_source_remove(compositor->xcb_source);
1147 x11_input_destroy(compositor);
1149 weston_compositor_shutdown(ec); /* destroys outputs, too */
1151 x11_compositor_fini_egl(compositor);
1153 XCloseDisplay(compositor->dpy);
1157 static struct weston_compositor *
1158 x11_compositor_create(struct wl_display *display,
1161 int argc, char *argv[], const char *config_file)
1163 struct x11_compositor *c;
1164 struct x11_configured_output *o;
1165 struct x11_output *output;
1166 xcb_screen_iterator_t s;
1167 int i, x = 0, output_count = 0;
1168 int width, height, count;
1170 weston_log("initializing x11 backend\n");
1172 c = malloc(sizeof *c);
1176 memset(c, 0, sizeof *c);
1178 if (weston_compositor_init(&c->base, display, argc, argv,
1182 c->dpy = XOpenDisplay(NULL);
1186 c->conn = XGetXCBConnection(c->dpy);
1187 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
1189 if (xcb_connection_has_error(c->conn))
1192 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
1194 wl_array_init(&c->keys);
1196 x11_compositor_get_resources(c);
1198 c->base.wl_display = display;
1199 if (x11_compositor_init_egl(c) < 0)
1202 c->base.destroy = x11_destroy;
1203 c->base.restore = x11_restore;
1205 if (x11_input_create(c, no_input) < 0)
1208 width = option_width ? option_width : 1024;
1209 height = option_height ? option_height : 640;
1210 count = option_count ? option_count : 1;
1212 wl_list_for_each(o, &configured_output_list, link) {
1213 output = x11_compositor_create_output(c, x, 0,
1214 option_width ? width :
1216 option_height ? height :
1218 fullscreen, no_input,
1219 o->name, o->transform);
1223 x = pixman_region32_extents(&output->base.region)->x2;
1226 if (option_count && output_count >= option_count)
1230 for (i = output_count; i < count; i++) {
1231 output = x11_compositor_create_output(c, x, 0, width, height,
1232 fullscreen, no_input, NULL,
1233 WL_OUTPUT_TRANSFORM_NORMAL);
1236 x = pixman_region32_extents(&output->base.region)->x2;
1239 if (gles2_renderer_init(&c->base) < 0)
1243 wl_event_loop_add_fd(c->base.input_loop,
1244 xcb_get_file_descriptor(c->conn),
1246 x11_compositor_handle_event, c);
1247 wl_event_source_check(c->xcb_source);
1252 x11_input_destroy(c);
1254 x11_compositor_fini_egl(c);
1256 XCloseDisplay(c->dpy);
1263 x11_output_set_transform(struct x11_configured_output *output)
1265 if (!output_transform) {
1266 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1270 if (!strcmp(output_transform, "normal"))
1271 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1272 else if (!strcmp(output_transform, "90"))
1273 output->transform = WL_OUTPUT_TRANSFORM_90;
1274 else if (!strcmp(output_transform, "180"))
1275 output->transform = WL_OUTPUT_TRANSFORM_180;
1276 else if (!strcmp(output_transform, "270"))
1277 output->transform = WL_OUTPUT_TRANSFORM_270;
1278 else if (!strcmp(output_transform, "flipped"))
1279 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
1280 else if (!strcmp(output_transform, "flipped-90"))
1281 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
1282 else if (!strcmp(output_transform, "flipped-180"))
1283 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
1284 else if (!strcmp(output_transform, "flipped-270"))
1285 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
1287 weston_log("Invalid transform \"%s\" for output %s\n",
1288 output_transform, output_name);
1289 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1294 output_section_done(void *data)
1296 struct x11_configured_output *output;
1298 output = malloc(sizeof *output);
1300 if (!output || !output_name || (output_name[0] != 'X') ||
1301 (!output_mode && !output_transform)) {
1309 output->name = output_name;
1312 if (sscanf(output_mode, "%dx%d", &output->width,
1313 &output->height) != 2) {
1314 weston_log("Invalid mode \"%s\" for output %s\n",
1315 output_mode, output_name);
1316 x11_free_configured_output(output);
1320 output->width = 1024;
1321 output->height = 640;
1324 x11_output_set_transform(output);
1326 wl_list_insert(configured_output_list.prev, &output->link);
1331 if (output_transform)
1332 free(output_transform);
1334 output_transform = NULL;
1337 WL_EXPORT struct weston_compositor *
1338 backend_init(struct wl_display *display, int argc, char *argv[],
1339 const char *config_file)
1344 const struct weston_option x11_options[] = {
1345 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1346 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
1347 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
1348 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
1349 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1352 parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
1354 wl_list_init(&configured_output_list);
1356 const struct config_key x11_config_keys[] = {
1357 { "name", CONFIG_KEY_STRING, &output_name },
1358 { "mode", CONFIG_KEY_STRING, &output_mode },
1359 { "transform", CONFIG_KEY_STRING, &output_transform },
1362 const struct config_section config_section[] = {
1363 { "output", x11_config_keys,
1364 ARRAY_LENGTH(x11_config_keys), output_section_done },
1367 parse_config_file(config_file, config_section,
1368 ARRAY_LENGTH(config_section), NULL);
1370 return x11_compositor_create(display,
1373 argc, argv, config_file);