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;
32 i3test::XTEST - Inline::C wrappers for xcb-xtest and xcb-xkb
36 # We need to use libxcb-xkb because xdotool cannot trigger ISO_Next_Group
37 # anymore: it contains code to set the XKB group to 1 and then restore the
38 # previous group, effectively rendering any keys that switch groups
42 %sn_config = ExtUtils::PkgConfig->find('xcb-xkb xcb-xtest xcb-util');
45 use Inline C => Config => LIBS => $sn_config{libs}, CCFLAGS => $sn_config{cflags};
46 use Inline C => <<'END_OF_C_CODE';
56 #include <xcb/xtest.h>
57 #include <xcb/xcb_aux.h>
59 static xcb_connection_t *conn = NULL;
60 static xcb_window_t sync_window;
61 static xcb_window_t root_window;
62 static xcb_atom_t i3_sync_atom;
64 bool inlinec_connect() {
67 if ((conn = xcb_connect(NULL, &screen)) == NULL ||
68 xcb_connection_has_error(conn)) {
72 fprintf(stderr, "Could not connect to X11\n");
76 if (!xcb_get_extension_data(conn, &xcb_xkb_id)->present) {
77 fprintf(stderr, "XKB not present\n");
81 if (!xcb_get_extension_data(conn, &xcb_test_id)->present) {
82 fprintf(stderr, "XTEST not present\n");
86 xcb_generic_error_t *err = NULL;
87 xcb_xkb_use_extension_reply_t *usereply;
88 usereply = xcb_xkb_use_extension_reply(
89 conn, xcb_xkb_use_extension(conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION), &err);
90 if (err != NULL || usereply == NULL) {
91 fprintf(stderr, "xcb_xkb_use_extension() failed\n");
97 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, xcb_intern_atom(conn, 0, strlen("I3_SYNC"), "I3_SYNC"), NULL);
98 i3_sync_atom = reply->atom;
101 xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screen);
102 root_window = root_screen->root;
103 sync_window = xcb_generate_id(conn);
104 xcb_create_window(conn,
105 XCB_COPY_FROM_PARENT, // depth
106 sync_window, // window
107 root_window, // parent
113 XCB_WINDOW_CLASS_INPUT_OUTPUT, // class
114 XCB_COPY_FROM_PARENT, // visual
115 XCB_CW_OVERRIDE_REDIRECT, // value_mask
117 1, // override_redirect
123 void xtest_sync_with_i3() {
124 xcb_client_message_event_t ev;
125 memset(&ev, '\0', sizeof(xcb_client_message_event_t));
127 const int nonce = rand() % 255;
129 ev.response_type = XCB_CLIENT_MESSAGE;
130 ev.window = sync_window;
131 ev.type = i3_sync_atom;
133 ev.data.data32[0] = sync_window;
134 ev.data.data32[1] = nonce;
136 xcb_send_event(conn, false, root_window, XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (char *)&ev);
139 xcb_generic_event_t *event = NULL;
142 if ((event = xcb_wait_for_event(conn)) == NULL) {
145 if (event->response_type == 0) {
146 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
150 /* Strip off the highest bit (set if the event is generated) */
151 const int type = (event->response_type & 0x7F);
153 case XCB_CLIENT_MESSAGE: {
154 xcb_client_message_event_t *ev = (xcb_client_message_event_t *)event;
156 const uint32_t got = ev->data.data32[0];
157 const uint32_t want = sync_window;
159 fprintf(stderr, "Ignoring ClientMessage: unknown window: got %d, want %d\n", got, want);
164 const uint32_t got = ev->data.data32[1];
165 const uint32_t want = nonce;
167 fprintf(stderr, "Ignoring ClientMessage: unknown nonce: got %d, want %d\n", got, want);
174 fprintf(stderr, "Unexpected X11 event of type %d received (XCB_CLIENT_MESSAGE = %d)\n", type, XCB_CLIENT_MESSAGE);
181 // NOTE: while |group| should be a uint8_t, Inline::C will not define the
182 // function unless we use an int.
183 bool set_xkb_group(int group) {
184 xcb_generic_error_t *err = NULL;
185 // Needs libxcb ≥ 1.11 so that we have the following bug fix:
186 // https://cgit.freedesktop.org/xcb/proto/commit/src/xkb.xml?id=8d7ee5b6ba4cf343f7df70372a3e1f85b82aeed7
187 xcb_void_cookie_t cookie = xcb_xkb_latch_lock_state_checked(
189 XCB_XKB_ID_USE_CORE_KBD, /* deviceSpec */
190 0, /* affectModLocks */
193 group, /* groupLock */
194 0, /* affectModLatches */
197 if ((err = xcb_request_check(conn, cookie)) != NULL) {
198 fprintf(stderr, "X error code %d\n", err->error_code);
205 bool xtest_input(int type, int detail, int x, int y) {
206 xcb_generic_error_t *err;
207 xcb_void_cookie_t cookie;
209 cookie = xcb_test_fake_input_checked(
213 XCB_CURRENT_TIME, /* time */
217 XCB_NONE); /* deviceid */
218 if ((err = xcb_request_check(conn, cookie)) != NULL) {
219 fprintf(stderr, "X error code %d\n", err->error_code);
227 bool xtest_key(int type, int detail) {
228 return xtest_input(type, detail, 0, 0);
231 bool xtest_key_press(int detail) {
232 return xtest_key(XCB_KEY_PRESS, detail);
235 bool xtest_key_release(int detail) {
236 return xtest_key(XCB_KEY_RELEASE, detail);
239 bool xtest_button_press(int button, int x, int y) {
240 return xtest_input(XCB_BUTTON_PRESS, button, x, y);
243 bool xtest_button_release(int button, int x, int y) {
244 return xtest_input(XCB_BUTTON_RELEASE, button, x, y);
250 my ($class, %args) = @_;
251 ok(inlinec_connect(), 'Connect to X11, verify XKB and XTEST are present (via Inline::C)');
252 goto \&Exporter::import;
262 =head2 start_binding_capture()
264 Captures all binding events sent by i3 in the C<@binding_events> symbol, so
265 that you can verify the correct number of binding events was generated.
267 my $pid = launch_with_config($config);
268 start_binding_capture;
271 is(scalar @i3test::XTEST::binding_events, 2, 'Received exactly 2 binding events');
275 sub start_binding_capture {
276 # Store a copy of each binding event so that we can count the expected
277 # events in test cases.
278 $i3 = i3(get_socket_path());
279 $i3->connect()->recv;
283 @binding_events = (@binding_events, $event);
288 =head2 listen_for_binding($cb)
290 Helper function to evaluate whether sending KeyPress/KeyRelease events via
291 XTEST triggers an i3 key binding or not (with a timeout of 0.5s). Expects key
292 bindings to be configured in the form “bindsym <binding> nop <binding>”, e.g.
293 “bindsym Mod4+Return nop Mod4+Return”.
295 is(listen_for_binding(
297 xtest_key_press(133); # Super_L
298 xtest_key_press(36); # Return
299 xtest_key_release(36); # Return
300 xtest_key_release(133); # Super_L
304 'triggered the "Mod4+Return" keybinding');
308 sub listen_for_binding {
310 my $triggered = AnyEvent->condvar;
311 my $i3 = i3(get_socket_path());
312 $i3->connect()->recv;
316 return unless $event->{change} eq 'run';
317 # We look at the command (which is “nop <binding>”) because that is
318 # easier than re-assembling the string representation of
320 $triggered->send($event->{binding}->{command});
325 $t = AnyEvent->timer(
328 $triggered->send('timeout');
334 my $recv = $triggered->recv;
339 =head2 set_xkb_group($group)
341 Changes the current XKB group from the default of 1 to C<$group>, which must be
344 Returns false when there was an X11 error changing the group, true otherwise.
346 =head2 xtest_key_press($detail)
348 Sends a KeyPress event via XTEST, with the specified C<$detail>, i.e. key code.
349 Use C<xev(1)> to find key codes.
351 Returns false when there was an X11 error, true otherwise.
353 =head2 xtest_key_release($detail)
355 Sends a KeyRelease event via XTEST, with the specified C<$detail>, i.e. key code.
356 Use C<xev(1)> to find key codes.
358 Returns false when there was an X11 error, true otherwise.
360 =head2 xtest_button_press($button, $x, $y)
362 Sends a ButtonPress event via XTEST, with the specified C<$button>.
364 Returns false when there was an X11 error, true otherwise.
366 =head2 xtest_button_release($button, $x, $y)
368 Sends a ButtonRelease event via XTEST, with the specified C<$button>.
370 Returns false when there was an X11 error, true otherwise.
372 =head2 xtest_sync_with_i3()
374 Ensures i3 has processed all X11 events which were triggered by this module.
378 Michael Stapelberg <michael@i3wm.org>