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 if (weston_seat_init_keyboard(&c->core_seat, keymap) < 0)
299 xkb_map_unref(keymap);
301 x11_compositor_setup_xkb(c);
307 x11_input_destroy(struct x11_compositor *compositor)
309 weston_seat_release(&compositor->core_seat);
313 x11_compositor_init_egl(struct x11_compositor *c)
317 EGLint config_attribs[] = {
318 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
322 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
326 c->base.egl_display = eglGetDisplay(c->dpy);
327 if (c->base.egl_display == NULL) {
328 weston_log("failed to create display\n");
332 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
333 weston_log("failed to initialize display\n");
337 if (!eglChooseConfig(c->base.egl_display, config_attribs,
338 &c->base.egl_config, 1, &n) || n == 0) {
339 weston_log("failed to choose config: %d\n", n);
347 x11_compositor_fini_egl(struct x11_compositor *compositor)
349 gles2_renderer_destroy(&compositor->base);
351 eglMakeCurrent(compositor->base.egl_display,
352 EGL_NO_SURFACE, EGL_NO_SURFACE,
355 eglTerminate(compositor->base.egl_display);
360 x11_output_repaint(struct weston_output *output_base,
361 pixman_region32_t *damage)
363 struct x11_output *output = (struct x11_output *)output_base;
364 struct weston_compositor *ec = output->base.compositor;
366 ec->renderer->repaint_output(output_base, damage);
368 pixman_region32_subtract(&ec->primary_plane.damage,
369 &ec->primary_plane.damage, damage);
371 wl_event_source_timer_update(output->finish_frame_timer, 10);
375 finish_frame_handler(void *data)
377 struct x11_output *output = data;
381 gettimeofday(&tv, NULL);
382 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
383 weston_output_finish_frame(&output->base, msec);
389 x11_output_destroy(struct weston_output *output_base)
391 struct x11_output *output = (struct x11_output *)output_base;
392 struct x11_compositor *compositor =
393 (struct x11_compositor *)output->base.compositor;
395 wl_list_remove(&output->base.link);
396 wl_event_source_remove(output->finish_frame_timer);
398 eglDestroySurface(compositor->base.egl_display,
399 output->base.egl_surface);
401 xcb_destroy_window(compositor->conn, output->window);
403 weston_output_destroy(&output->base);
409 x11_output_set_wm_protocols(struct x11_output *output)
412 struct x11_compositor *c =
413 (struct x11_compositor *) output->base.compositor;
415 list[0] = c->atom.wm_delete_window;
416 xcb_change_property (c->conn,
417 XCB_PROP_MODE_REPLACE,
419 c->atom.wm_protocols,
427 x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state)
429 xcb_client_message_event_t event;
430 struct x11_compositor *c =
431 (struct x11_compositor *) output->base.compositor;
432 xcb_screen_iterator_t iter;
434 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
435 #define _NET_WM_STATE_ADD 1 /* add/set property */
436 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
438 memset(&event, 0, sizeof event);
439 event.response_type = XCB_CLIENT_MESSAGE;
441 event.window = output->window;
442 event.type = c->atom.net_wm_state;
444 event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
445 event.data.data32[1] = state;
446 event.data.data32[2] = 0;
447 event.data.data32[3] = 0;
448 event.data.data32[4] = 0;
450 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
451 xcb_send_event(c->conn, 0, iter.data->root,
452 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
453 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
458 struct wm_normal_hints {
461 int32_t min_width, min_height;
462 int32_t max_width, max_height;
463 int32_t width_inc, height_inc;
464 int32_t min_aspect_x, min_aspect_y;
465 int32_t max_aspect_x, max_aspect_y;
466 int32_t base_width, base_height;
470 #define WM_NORMAL_HINTS_MIN_SIZE 16
471 #define WM_NORMAL_HINTS_MAX_SIZE 32
474 x11_output_set_icon(struct x11_compositor *c,
475 struct x11_output *output, const char *filename)
478 int32_t width, height;
479 pixman_image_t *image;
481 image = load_image(filename);
484 width = pixman_image_get_width(image);
485 height = pixman_image_get_height(image);
486 icon = malloc(width * height * 4 + 8);
488 pixman_image_unref(image);
494 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
495 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
496 c->atom.net_wm_icon, c->atom.cardinal, 32,
497 width * height + 2, icon);
499 pixman_image_unref(image);
502 static struct x11_output *
503 x11_compositor_create_output(struct x11_compositor *c, int x, int y,
504 int width, int height, int fullscreen,
505 int no_input, char *configured_name,
508 static const char name[] = "Weston Compositor";
509 static const char class[] = "weston-1\0Weston Compositor";
511 struct x11_output *output;
512 xcb_screen_iterator_t iter;
513 struct wm_normal_hints normal_hints;
514 struct wl_event_loop *loop;
515 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
516 uint32_t values[2] = {
517 XCB_EVENT_MASK_EXPOSURE |
518 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
523 sprintf(title, "%s - %s", name, configured_name);
529 XCB_EVENT_MASK_KEY_PRESS |
530 XCB_EVENT_MASK_KEY_RELEASE |
531 XCB_EVENT_MASK_BUTTON_PRESS |
532 XCB_EVENT_MASK_BUTTON_RELEASE |
533 XCB_EVENT_MASK_POINTER_MOTION |
534 XCB_EVENT_MASK_ENTER_WINDOW |
535 XCB_EVENT_MASK_LEAVE_WINDOW |
536 XCB_EVENT_MASK_KEYMAP_STATE |
537 XCB_EVENT_MASK_FOCUS_CHANGE;
539 output = malloc(sizeof *output);
543 memset(output, 0, sizeof *output);
546 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
547 output->mode.width = width;
548 output->mode.height = height;
549 output->mode.refresh = 60000;
550 wl_list_init(&output->base.mode_list);
551 wl_list_insert(&output->base.mode_list, &output->mode.link);
553 output->base.current = &output->mode;
554 output->base.make = "xwayland";
555 output->base.model = "none";
556 weston_output_init(&output->base, &c->base,
557 x, y, width, height, transform);
559 values[1] = c->null_cursor;
560 output->window = xcb_generate_id(c->conn);
561 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
562 xcb_create_window(c->conn,
563 XCB_COPY_FROM_PARENT,
569 XCB_WINDOW_CLASS_INPUT_OUTPUT,
570 iter.data->root_visual,
573 /* Don't resize me. */
574 memset(&normal_hints, 0, sizeof normal_hints);
576 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
577 normal_hints.min_width = width;
578 normal_hints.min_height = height;
579 normal_hints.max_width = width;
580 normal_hints.max_height = height;
581 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
582 c->atom.wm_normal_hints,
583 c->atom.wm_size_hints, 32,
584 sizeof normal_hints / 4,
585 (uint8_t *) &normal_hints);
587 /* Set window name. Don't bother with non-EWMH WMs. */
588 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
589 c->atom.net_wm_name, c->atom.utf8_string, 8,
590 strlen(title), title);
591 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
592 c->atom.wm_class, c->atom.string, 8,
593 sizeof class, class);
595 x11_output_set_icon(c, output, DATADIR "/weston/wayland.png");
597 xcb_map_window(c->conn, output->window);
599 x11_output_set_wm_protocols(output);
602 x11_output_change_state(output, 1,
603 c->atom.net_wm_state_fullscreen);
605 output->base.egl_surface =
606 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
607 output->window, NULL);
608 if (!output->base.egl_surface) {
609 weston_log("failed to create window surface\n");
613 loop = wl_display_get_event_loop(c->base.wl_display);
614 output->finish_frame_timer =
615 wl_event_loop_add_timer(loop, finish_frame_handler, output);
617 output->base.origin = output->base.current;
618 output->base.repaint = x11_output_repaint;
619 output->base.destroy = x11_output_destroy;
620 output->base.assign_planes = NULL;
621 output->base.set_backlight = NULL;
622 output->base.set_dpms = NULL;
623 output->base.switch_mode = NULL;
625 wl_list_insert(c->base.output_list.prev, &output->base.link);
627 weston_log("x11 output %dx%d, window id %d\n",
628 width, height, output->window);
633 static struct x11_output *
634 x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
636 struct x11_output *output;
638 wl_list_for_each(output, &c->base.output_list, base.link) {
639 if (output->window == window)
648 update_xkb_state(struct x11_compositor *c, xcb_xkb_state_notify_event_t *state)
650 xkb_state_update_mask(c->core_seat.xkb_state.state,
651 get_xkb_mod_mask(c, state->baseMods),
652 get_xkb_mod_mask(c, state->latchedMods),
653 get_xkb_mod_mask(c, state->lockedMods),
658 notify_modifiers(&c->core_seat,
659 wl_display_next_serial(c->base.wl_display));
664 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
665 * the best we can do (given that XCB also lacks XI2 support), is to take
666 * the state from the core key events. Unfortunately that only gives us
667 * the effective (i.e. union of depressed/latched/locked) state, and we
668 * need the granularity.
670 * So we still update the state with every key event we see, but also use
671 * the state field from X11 events as a mask so we don't get any stuck
675 update_xkb_state_from_core(struct x11_compositor *c, uint16_t x11_mask)
677 uint32_t mask = get_xkb_mod_mask(c, x11_mask);
678 struct wl_keyboard *keyboard = &c->core_seat.keyboard;
680 xkb_state_update_mask(c->core_seat.xkb_state.state,
681 keyboard->modifiers.mods_depressed & mask,
682 keyboard->modifiers.mods_latched & mask,
683 keyboard->modifiers.mods_locked & mask,
686 (x11_mask >> 13) & 3);
687 notify_modifiers(&c->core_seat,
688 wl_display_next_serial(c->base.wl_display));
692 x11_compositor_deliver_button_event(struct x11_compositor *c,
693 xcb_generic_event_t *event, int state)
695 xcb_button_press_event_t *button_event =
696 (xcb_button_press_event_t *) event;
698 struct x11_output *output;
700 output = x11_compositor_find_output(c, button_event->event);
703 xcb_grab_pointer(c->conn, 0, output->window,
704 XCB_EVENT_MASK_BUTTON_PRESS |
705 XCB_EVENT_MASK_BUTTON_RELEASE |
706 XCB_EVENT_MASK_POINTER_MOTION |
707 XCB_EVENT_MASK_ENTER_WINDOW |
708 XCB_EVENT_MASK_LEAVE_WINDOW,
711 output->window, XCB_CURSOR_NONE,
714 xcb_ungrab_pointer(c->conn, button_event->time);
717 update_xkb_state_from_core(c, button_event->state);
719 switch (button_event->detail) {
721 button = button_event->detail + BTN_LEFT - 1;
730 /* Axis are measured in pixels, but the xcb events are discrete
731 * steps. Therefore move the axis by some pixels every step. */
733 notify_axis(&c->core_seat,
734 weston_compositor_get_time(),
735 WL_POINTER_AXIS_VERTICAL_SCROLL,
736 -DEFAULT_AXIS_STEP_DISTANCE);
740 notify_axis(&c->core_seat,
741 weston_compositor_get_time(),
742 WL_POINTER_AXIS_VERTICAL_SCROLL,
743 DEFAULT_AXIS_STEP_DISTANCE);
747 notify_axis(&c->core_seat,
748 weston_compositor_get_time(),
749 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
750 -DEFAULT_AXIS_STEP_DISTANCE);
754 notify_axis(&c->core_seat,
755 weston_compositor_get_time(),
756 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
757 DEFAULT_AXIS_STEP_DISTANCE);
761 notify_button(&c->core_seat,
762 weston_compositor_get_time(), button,
763 state ? WL_POINTER_BUTTON_STATE_PRESSED :
764 WL_POINTER_BUTTON_STATE_RELEASED);
768 x11_output_transform_coordinate(struct x11_output *x11_output,
769 wl_fixed_t *x, wl_fixed_t *y)
771 struct weston_output *output = &x11_output->base;
773 wl_fixed_t width = wl_fixed_from_int(output->width - 1);
774 wl_fixed_t height = wl_fixed_from_int(output->height - 1);
776 switch(output->transform) {
777 case WL_OUTPUT_TRANSFORM_NORMAL:
782 case WL_OUTPUT_TRANSFORM_90:
786 case WL_OUTPUT_TRANSFORM_180:
790 case WL_OUTPUT_TRANSFORM_270:
794 case WL_OUTPUT_TRANSFORM_FLIPPED:
798 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
802 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
806 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
812 tx += wl_fixed_from_int(output->x);
813 ty += wl_fixed_from_int(output->y);
820 x11_compositor_deliver_motion_event(struct x11_compositor *c,
821 xcb_generic_event_t *event)
823 struct x11_output *output;
825 xcb_motion_notify_event_t *motion_notify =
826 (xcb_motion_notify_event_t *) event;
829 update_xkb_state_from_core(c, motion_notify->state);
830 output = x11_compositor_find_output(c, motion_notify->event);
831 x = wl_fixed_from_int(motion_notify->event_x);
832 y = wl_fixed_from_int(motion_notify->event_y);
833 x11_output_transform_coordinate(output, &x, &y);
835 notify_motion(&c->core_seat, weston_compositor_get_time(), x, y);
839 x11_compositor_deliver_enter_event(struct x11_compositor *c,
840 xcb_generic_event_t *event)
842 struct x11_output *output;
845 xcb_enter_notify_event_t *enter_notify =
846 (xcb_enter_notify_event_t *) event;
847 if (enter_notify->state >= Button1Mask)
850 update_xkb_state_from_core(c, enter_notify->state);
851 output = x11_compositor_find_output(c, enter_notify->event);
852 x = wl_fixed_from_int(enter_notify->event_x);
853 y = wl_fixed_from_int(enter_notify->event_y);
854 x11_output_transform_coordinate(output, &x, &y);
856 notify_pointer_focus(&c->core_seat, &output->base, x, y);
860 x11_compositor_next_event(struct x11_compositor *c,
861 xcb_generic_event_t **event, uint32_t mask)
863 if (mask & WL_EVENT_READABLE) {
864 *event = xcb_poll_for_event(c->conn);
866 #ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
867 *event = xcb_poll_for_queued_event(c->conn);
869 *event = xcb_poll_for_event(c->conn);
873 return *event != NULL;
877 x11_compositor_handle_event(int fd, uint32_t mask, void *data)
879 struct x11_compositor *c = data;
880 struct x11_output *output;
881 xcb_generic_event_t *event, *prev;
882 xcb_client_message_event_t *client_message;
883 xcb_enter_notify_event_t *enter_notify;
884 xcb_key_press_event_t *key_press, *key_release;
885 xcb_keymap_notify_event_t *keymap_notify;
886 xcb_focus_in_event_t *focus_in;
887 xcb_expose_event_t *expose;
891 uint8_t response_type;
896 while (x11_compositor_next_event(c, &event, mask)) {
897 response_type = event->response_type & ~0x80;
899 switch (prev ? prev->response_type & ~0x80 : 0x80) {
900 case XCB_KEY_RELEASE:
901 /* Suppress key repeat events; this is only used if we
902 * don't have XCB XKB support. */
903 key_release = (xcb_key_press_event_t *) prev;
904 key_press = (xcb_key_press_event_t *) event;
905 if (response_type == XCB_KEY_PRESS &&
906 key_release->time == key_press->time &&
907 key_release->detail == key_press->detail) {
908 /* Don't deliver the held key release
909 * event or the new key press event. */
915 /* Deliver the held key release now
916 * and fall through and handle the new
918 update_xkb_state_from_core(c, key_release->state);
919 notify_key(&c->core_seat,
920 weston_compositor_get_time(),
921 key_release->detail - 8,
922 WL_KEYBOARD_KEY_STATE_RELEASED,
923 STATE_UPDATE_AUTOMATIC);
930 assert(response_type == XCB_KEYMAP_NOTIFY);
931 keymap_notify = (xcb_keymap_notify_event_t *) event;
933 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
934 set = keymap_notify->keys[i >> 3] &
937 k = wl_array_add(&c->keys, sizeof *k);
942 /* Unfortunately the state only comes with the enter
943 * event, rather than with the focus event. I'm not
944 * sure of the exact semantics around it and whether
945 * we can ensure that we get both? */
946 notify_keyboard_focus_in(&c->core_seat, &c->keys,
947 STATE_UPDATE_AUTOMATIC);
954 /* No previous event held */
958 switch (response_type) {
960 key_press = (xcb_key_press_event_t *) event;
962 update_xkb_state_from_core(c, key_press->state);
963 notify_key(&c->core_seat,
964 weston_compositor_get_time(),
965 key_press->detail - 8,
966 WL_KEYBOARD_KEY_STATE_PRESSED,
967 c->has_xkb ? STATE_UPDATE_NONE :
968 STATE_UPDATE_AUTOMATIC);
970 case XCB_KEY_RELEASE:
971 /* If we don't have XKB, we need to use the lame
972 * autorepeat detection above. */
977 key_release = (xcb_key_press_event_t *) event;
978 notify_key(&c->core_seat,
979 weston_compositor_get_time(),
980 key_release->detail - 8,
981 WL_KEYBOARD_KEY_STATE_RELEASED,
984 case XCB_BUTTON_PRESS:
985 x11_compositor_deliver_button_event(c, event, 1);
987 case XCB_BUTTON_RELEASE:
988 x11_compositor_deliver_button_event(c, event, 0);
990 case XCB_MOTION_NOTIFY:
991 x11_compositor_deliver_motion_event(c, event);
995 expose = (xcb_expose_event_t *) event;
996 output = x11_compositor_find_output(c, expose->window);
997 weston_output_schedule_repaint(&output->base);
1000 case XCB_ENTER_NOTIFY:
1001 x11_compositor_deliver_enter_event(c, event);
1004 case XCB_LEAVE_NOTIFY:
1005 enter_notify = (xcb_enter_notify_event_t *) event;
1006 if (enter_notify->state >= Button1Mask)
1009 update_xkb_state_from_core(c, enter_notify->state);
1010 notify_pointer_focus(&c->core_seat, NULL, 0, 0);
1013 case XCB_CLIENT_MESSAGE:
1014 client_message = (xcb_client_message_event_t *) event;
1015 atom = client_message->data.data32[0];
1016 if (atom == c->atom.wm_delete_window)
1017 wl_display_terminate(c->base.wl_display);
1021 focus_in = (xcb_focus_in_event_t *) event;
1022 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1029 focus_in = (xcb_focus_in_event_t *) event;
1030 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1031 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
1033 notify_keyboard_focus_out(&c->core_seat);
1042 response_type == c->xkb_event_base) {
1043 xcb_xkb_state_notify_event_t *state =
1044 (xcb_xkb_state_notify_event_t *) event;
1045 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
1046 update_xkb_state(c, state);
1055 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1056 case XCB_KEY_RELEASE:
1057 key_release = (xcb_key_press_event_t *) prev;
1058 update_xkb_state_from_core(c, key_release->state);
1059 notify_key(&c->core_seat,
1060 weston_compositor_get_time(),
1061 key_release->detail - 8,
1062 WL_KEYBOARD_KEY_STATE_RELEASED,
1063 STATE_UPDATE_AUTOMATIC);
1074 #define F(field) offsetof(struct x11_compositor, field)
1077 x11_compositor_get_resources(struct x11_compositor *c)
1079 static const struct { const char *name; int offset; } atoms[] = {
1080 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1081 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1082 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1083 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
1084 { "WM_CLASS", F(atom.wm_class) },
1085 { "_NET_WM_NAME", F(atom.net_wm_name) },
1086 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1087 { "_NET_WM_STATE", F(atom.net_wm_state) },
1088 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1089 { "STRING", F(atom.string) },
1090 { "UTF8_STRING", F(atom.utf8_string) },
1091 { "CARDINAL", F(atom.cardinal) },
1092 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
1095 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1096 xcb_intern_atom_reply_t *reply;
1097 xcb_pixmap_t pixmap;
1100 uint8_t data[] = { 0, 0, 0, 0 };
1102 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1103 cookies[i] = xcb_intern_atom (c->conn, 0,
1104 strlen(atoms[i].name),
1107 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1108 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
1109 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
1113 pixmap = xcb_generate_id(c->conn);
1114 gc = xcb_generate_id(c->conn);
1115 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
1116 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
1117 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
1118 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
1119 c->null_cursor = xcb_generate_id(c->conn);
1120 xcb_create_cursor (c->conn, c->null_cursor,
1121 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
1122 xcb_free_gc(c->conn, gc);
1123 xcb_free_pixmap(c->conn, pixmap);
1127 x11_restore(struct weston_compositor *ec)
1132 x11_free_configured_output(struct x11_configured_output *output)
1139 x11_destroy(struct weston_compositor *ec)
1141 struct x11_compositor *compositor = (struct x11_compositor *)ec;
1142 struct x11_configured_output *o, *n;
1144 wl_list_for_each_safe(o, n, &configured_output_list, link)
1145 x11_free_configured_output(o);
1147 wl_event_source_remove(compositor->xcb_source);
1148 x11_input_destroy(compositor);
1150 weston_compositor_shutdown(ec); /* destroys outputs, too */
1152 x11_compositor_fini_egl(compositor);
1154 XCloseDisplay(compositor->dpy);
1158 static struct weston_compositor *
1159 x11_compositor_create(struct wl_display *display,
1162 int argc, char *argv[], const char *config_file)
1164 struct x11_compositor *c;
1165 struct x11_configured_output *o;
1166 struct x11_output *output;
1167 xcb_screen_iterator_t s;
1168 int i, x = 0, output_count = 0;
1169 int width, height, count;
1171 weston_log("initializing x11 backend\n");
1173 c = malloc(sizeof *c);
1177 memset(c, 0, sizeof *c);
1179 if (weston_compositor_init(&c->base, display, argc, argv,
1183 c->dpy = XOpenDisplay(NULL);
1187 c->conn = XGetXCBConnection(c->dpy);
1188 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
1190 if (xcb_connection_has_error(c->conn))
1193 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
1195 wl_array_init(&c->keys);
1197 x11_compositor_get_resources(c);
1199 c->base.wl_display = display;
1200 if (x11_compositor_init_egl(c) < 0)
1203 c->base.destroy = x11_destroy;
1204 c->base.restore = x11_restore;
1206 if (x11_input_create(c, no_input) < 0)
1209 width = option_width ? option_width : 1024;
1210 height = option_height ? option_height : 640;
1211 count = option_count ? option_count : 1;
1213 wl_list_for_each(o, &configured_output_list, link) {
1214 output = x11_compositor_create_output(c, x, 0,
1215 option_width ? width :
1217 option_height ? height :
1219 fullscreen, no_input,
1220 o->name, o->transform);
1224 x = pixman_region32_extents(&output->base.region)->x2;
1227 if (option_count && output_count >= option_count)
1231 for (i = output_count; i < count; i++) {
1232 output = x11_compositor_create_output(c, x, 0, width, height,
1233 fullscreen, no_input, NULL,
1234 WL_OUTPUT_TRANSFORM_NORMAL);
1237 x = pixman_region32_extents(&output->base.region)->x2;
1240 if (gles2_renderer_init(&c->base) < 0)
1244 wl_event_loop_add_fd(c->base.input_loop,
1245 xcb_get_file_descriptor(c->conn),
1247 x11_compositor_handle_event, c);
1248 wl_event_source_check(c->xcb_source);
1253 x11_input_destroy(c);
1255 x11_compositor_fini_egl(c);
1257 XCloseDisplay(c->dpy);
1264 x11_output_set_transform(struct x11_configured_output *output)
1266 if (!output_transform) {
1267 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1271 if (!strcmp(output_transform, "normal"))
1272 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1273 else if (!strcmp(output_transform, "90"))
1274 output->transform = WL_OUTPUT_TRANSFORM_90;
1275 else if (!strcmp(output_transform, "180"))
1276 output->transform = WL_OUTPUT_TRANSFORM_180;
1277 else if (!strcmp(output_transform, "270"))
1278 output->transform = WL_OUTPUT_TRANSFORM_270;
1279 else if (!strcmp(output_transform, "flipped"))
1280 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
1281 else if (!strcmp(output_transform, "flipped-90"))
1282 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
1283 else if (!strcmp(output_transform, "flipped-180"))
1284 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
1285 else if (!strcmp(output_transform, "flipped-270"))
1286 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
1288 weston_log("Invalid transform \"%s\" for output %s\n",
1289 output_transform, output_name);
1290 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1295 output_section_done(void *data)
1297 struct x11_configured_output *output;
1299 output = malloc(sizeof *output);
1301 if (!output || !output_name || (output_name[0] != 'X') ||
1302 (!output_mode && !output_transform)) {
1310 output->name = output_name;
1313 if (sscanf(output_mode, "%dx%d", &output->width,
1314 &output->height) != 2) {
1315 weston_log("Invalid mode \"%s\" for output %s\n",
1316 output_mode, output_name);
1317 x11_free_configured_output(output);
1321 output->width = 1024;
1322 output->height = 640;
1325 x11_output_set_transform(output);
1327 wl_list_insert(configured_output_list.prev, &output->link);
1332 if (output_transform)
1333 free(output_transform);
1335 output_transform = NULL;
1338 WL_EXPORT struct weston_compositor *
1339 backend_init(struct wl_display *display, int argc, char *argv[],
1340 const char *config_file)
1345 const struct weston_option x11_options[] = {
1346 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1347 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
1348 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
1349 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
1350 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1353 parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
1355 wl_list_init(&configured_output_list);
1357 const struct config_key x11_config_keys[] = {
1358 { "name", CONFIG_KEY_STRING, &output_name },
1359 { "mode", CONFIG_KEY_STRING, &output_mode },
1360 { "transform", CONFIG_KEY_STRING, &output_transform },
1363 const struct config_section config_section[] = {
1364 { "output", x11_config_keys,
1365 ARRAY_LENGTH(x11_config_keys), output_section_done },
1368 parse_config_file(config_file, config_section,
1369 ARRAY_LENGTH(config_section), NULL);
1371 return x11_compositor_create(display,
1374 argc, argv, config_file);