2 # vim:ts=4:sw=4:expandtab
9 use i3test::Util qw(get_socket_path);
10 use lib qw(@abs_top_srcdir@/AnyEvent-I3/blib/lib);
12 use ExtUtils::PkgConfig;
31 i3test::XTEST - Inline::C wrappers for xcb-xtest and xcb-xkb
35 # We need to use libxcb-xkb because xdotool cannot trigger ISO_Next_Group
36 # anymore: it contains code to set the XKB group to 1 and then restore the
37 # previous group, effectively rendering any keys that switch groups
41 %sn_config = ExtUtils::PkgConfig->find('xcb-xkb xcb-xtest xcb-util');
44 use Inline C => Config => LIBS => $sn_config{libs}, CCFLAGS => $sn_config{cflags};
45 use Inline C => <<'END_OF_C_CODE';
55 #include <xcb/xtest.h>
56 #include <xcb/xcb_aux.h>
58 static xcb_connection_t *conn = NULL;
59 static xcb_window_t sync_window;
60 static xcb_window_t root_window;
61 static xcb_atom_t i3_sync_atom;
63 bool inlinec_connect() {
66 if ((conn = xcb_connect(NULL, &screen)) == NULL ||
67 xcb_connection_has_error(conn)) {
71 fprintf(stderr, "Could not connect to X11\n");
75 if (!xcb_get_extension_data(conn, &xcb_xkb_id)->present) {
76 fprintf(stderr, "XKB not present\n");
80 if (!xcb_get_extension_data(conn, &xcb_test_id)->present) {
81 fprintf(stderr, "XTEST not present\n");
85 xcb_generic_error_t *err = NULL;
86 xcb_xkb_use_extension_reply_t *usereply;
87 usereply = xcb_xkb_use_extension_reply(
88 conn, xcb_xkb_use_extension(conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION), &err);
89 if (err != NULL || usereply == NULL) {
90 fprintf(stderr, "xcb_xkb_use_extension() failed\n");
96 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, xcb_intern_atom(conn, 0, strlen("I3_SYNC"), "I3_SYNC"), NULL);
97 i3_sync_atom = reply->atom;
100 xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screen);
101 root_window = root_screen->root;
102 sync_window = xcb_generate_id(conn);
103 xcb_create_window(conn,
104 XCB_COPY_FROM_PARENT, // depth
105 sync_window, // window
106 root_window, // parent
112 XCB_WINDOW_CLASS_INPUT_OUTPUT, // class
113 XCB_COPY_FROM_PARENT, // visual
114 XCB_CW_OVERRIDE_REDIRECT, // value_mask
116 1, // override_redirect
122 void xtest_sync_with(int window) {
123 xcb_client_message_event_t ev;
124 memset(&ev, '\0', sizeof(xcb_client_message_event_t));
126 const int nonce = rand() % 255;
128 ev.response_type = XCB_CLIENT_MESSAGE;
129 ev.window = sync_window;
130 ev.type = i3_sync_atom;
132 ev.data.data32[0] = sync_window;
133 ev.data.data32[1] = nonce;
135 xcb_send_event(conn, false, (xcb_window_t)window, XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (char *)&ev);
138 xcb_generic_event_t *event = NULL;
141 if ((event = xcb_wait_for_event(conn)) == NULL) {
144 if (event->response_type == 0) {
145 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
149 /* Strip off the highest bit (set if the event is generated) */
150 const int type = (event->response_type & 0x7F);
152 case XCB_CLIENT_MESSAGE: {
153 xcb_client_message_event_t *ev = (xcb_client_message_event_t *)event;
155 const uint32_t got = ev->data.data32[0];
156 const uint32_t want = sync_window;
158 fprintf(stderr, "Ignoring ClientMessage: unknown window: got %d, want %d\n", got, want);
163 const uint32_t got = ev->data.data32[1];
164 const uint32_t want = nonce;
166 fprintf(stderr, "Ignoring ClientMessage: unknown nonce: got %d, want %d\n", got, want);
173 fprintf(stderr, "Unexpected X11 event of type %d received (XCB_CLIENT_MESSAGE = %d)\n", type, XCB_CLIENT_MESSAGE);
180 void xtest_sync_with_i3() {
181 xtest_sync_with((int)root_window);
184 // NOTE: while |group| should be a uint8_t, Inline::C will not define the
185 // function unless we use an int.
186 bool set_xkb_group(int group) {
187 xcb_generic_error_t *err = NULL;
188 // Needs libxcb ≥ 1.11 so that we have the following bug fix:
189 // https://cgit.freedesktop.org/xcb/proto/commit/src/xkb.xml?id=8d7ee5b6ba4cf343f7df70372a3e1f85b82aeed7
190 xcb_void_cookie_t cookie = xcb_xkb_latch_lock_state_checked(
192 XCB_XKB_ID_USE_CORE_KBD, /* deviceSpec */
193 0, /* affectModLocks */
196 group, /* groupLock */
197 0, /* affectModLatches */
200 if ((err = xcb_request_check(conn, cookie)) != NULL) {
201 fprintf(stderr, "X error code %d\n", err->error_code);
208 bool xtest_input(int type, int detail, int x, int y) {
209 xcb_generic_error_t *err;
210 xcb_void_cookie_t cookie;
212 cookie = xcb_test_fake_input_checked(
216 XCB_CURRENT_TIME, /* time */
220 XCB_NONE); /* deviceid */
221 if ((err = xcb_request_check(conn, cookie)) != NULL) {
222 fprintf(stderr, "X error code %d\n", err->error_code);
230 bool xtest_key(int type, int detail) {
231 return xtest_input(type, detail, 0, 0);
234 bool xtest_key_press(int detail) {
235 return xtest_key(XCB_KEY_PRESS, detail);
238 bool xtest_key_release(int detail) {
239 return xtest_key(XCB_KEY_RELEASE, detail);
242 bool xtest_button_press(int button, int x, int y) {
243 return xtest_input(XCB_BUTTON_PRESS, button, x, y);
246 bool xtest_button_release(int button, int x, int y) {
247 return xtest_input(XCB_BUTTON_RELEASE, button, x, y);
253 my ($class, %args) = @_;
254 ok(inlinec_connect(), 'Connect to X11, verify XKB and XTEST are present (via Inline::C)');
255 goto \&Exporter::import;
262 =head2 set_xkb_group($group)
264 Changes the current XKB group from the default of 1 to C<$group>, which must be
267 Returns false when there was an X11 error changing the group, true otherwise.
269 =head2 xtest_key_press($detail)
271 Sends a KeyPress event via XTEST, with the specified C<$detail>, i.e. key code.
272 Use C<xev(1)> to find key codes.
274 Returns false when there was an X11 error, true otherwise.
276 =head2 xtest_key_release($detail)
278 Sends a KeyRelease event via XTEST, with the specified C<$detail>, i.e. key code.
279 Use C<xev(1)> to find key codes.
281 Returns false when there was an X11 error, true otherwise.
283 =head2 xtest_button_press($button, $x, $y)
285 Sends a ButtonPress event via XTEST, with the specified C<$button>.
287 Returns false when there was an X11 error, true otherwise.
289 =head2 xtest_button_release($button, $x, $y)
291 Sends a ButtonRelease event via XTEST, with the specified C<$button>.
293 Returns false when there was an X11 error, true otherwise.
295 =head2 xtest_sync_with($window)
297 Ensures the specified window has processed all X11 events which were triggered
298 by this module, provided the window response to the i3 sync protocol.
300 =head2 xtest_sync_with_i3()
302 Ensures i3 has processed all X11 events which were triggered by this module.
306 Michael Stapelberg <michael@i3wm.org>