2 # vim:ts=4:sw=4:expandtab
8 use i3test i3_autostart => 0;
10 use ExtUtils::PkgConfig;
29 i3test::XTEST - Inline::C wrappers for xcb-xtest and xcb-xkb
33 # We need to use libxcb-xkb because xdotool cannot trigger ISO_Next_Group
34 # anymore: it contains code to set the XKB group to 1 and then restore the
35 # previous group, effectively rendering any keys that switch groups
39 %sn_config = ExtUtils::PkgConfig->find('xcb-xkb xcb-xtest');
42 use Inline C => Config => LIBS => $sn_config{libs}, CCFLAGS => $sn_config{cflags};
43 use Inline C => <<'END_OF_C_CODE';
53 #include <xcb/xtest.h>
55 static xcb_connection_t *conn = NULL;
57 bool inlinec_connect() {
60 if ((conn = xcb_connect(NULL, &screen)) == NULL ||
61 xcb_connection_has_error(conn)) {
62 fprintf(stderr, "Could not connect to X11\n");
66 if (!xcb_get_extension_data(conn, &xcb_xkb_id)->present) {
67 fprintf(stderr, "XKB not present\n");
71 if (!xcb_get_extension_data(conn, &xcb_test_id)->present) {
72 fprintf(stderr, "XTEST not present\n");
76 xcb_generic_error_t *err = NULL;
77 xcb_xkb_use_extension_reply_t *usereply;
78 usereply = xcb_xkb_use_extension_reply(
79 conn, xcb_xkb_use_extension(conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION), &err);
80 if (err != NULL || usereply == NULL) {
81 fprintf(stderr, "xcb_xkb_use_extension() failed\n");
89 // NOTE: while |group| should be a uint8_t, Inline::C will not define the
90 // function unless we use an int.
91 bool set_xkb_group(int group) {
92 xcb_generic_error_t *err = NULL;
93 // Needs libxcb ≥ 1.11 so that we have the following bug fix:
94 // http://cgit.freedesktop.org/xcb/proto/commit/src/xkb.xml?id=8d7ee5b6ba4cf343f7df70372a3e1f85b82aeed7
95 xcb_void_cookie_t cookie = xcb_xkb_latch_lock_state_checked(
97 XCB_XKB_ID_USE_CORE_KBD, /* deviceSpec */
98 0, /* affectModLocks */
101 group, /* groupLock */
102 0, /* affectModLatches */
105 if ((err = xcb_request_check(conn, cookie)) != NULL) {
106 fprintf(stderr, "X error code %d\n", err->error_code);
112 bool xtest_input(int type, int detail, int x, int y) {
113 xcb_generic_error_t *err;
114 xcb_void_cookie_t cookie;
116 cookie = xcb_test_fake_input_checked(
120 XCB_CURRENT_TIME, /* time */
124 XCB_NONE); /* deviceid */
125 if ((err = xcb_request_check(conn, cookie)) != NULL) {
126 fprintf(stderr, "X error code %d\n", err->error_code);
133 bool xtest_key(int type, int detail) {
134 return xtest_input(type, detail, 0, 0);
137 bool xtest_key_press(int detail) {
138 return xtest_key(XCB_KEY_PRESS, detail);
141 bool xtest_key_release(int detail) {
142 return xtest_key(XCB_KEY_RELEASE, detail);
145 bool xtest_button_press(int button, int x, int y) {
146 return xtest_input(XCB_BUTTON_PRESS, button, x, y);
149 bool xtest_button_release(int button, int x, int y) {
150 return xtest_input(XCB_BUTTON_RELEASE, button, x, y);
156 my ($class, %args) = @_;
157 ok(inlinec_connect(), 'Connect to X11, verify XKB and XTEST are present (via Inline::C)');
158 goto \&Exporter::import;
168 =head2 start_binding_capture()
170 Captures all binding events sent by i3 in the C<@binding_events> symbol, so
171 that you can verify the correct number of binding events was generated.
173 my $pid = launch_with_config($config);
174 start_binding_capture;
177 is(scalar @i3test::XTEST::binding_events, 2, 'Received exactly 2 binding events');
181 sub start_binding_capture {
182 # Store a copy of each binding event so that we can count the expected
183 # events in test cases.
184 $i3 = i3(get_socket_path());
185 $i3->connect()->recv;
189 @binding_events = (@binding_events, $event);
194 =head2 listen_for_binding($cb)
196 Helper function to evaluate whether sending KeyPress/KeyRelease events via
197 XTEST triggers an i3 key binding or not (with a timeout of 0.5s). Expects key
198 bindings to be configured in the form “bindsym <binding> nop <binding>”, e.g.
199 “bindsym Mod4+Return nop Mod4+Return”.
201 is(listen_for_binding(
203 xtest_key_press(133); # Super_L
204 xtest_key_press(36); # Return
205 xtest_key_release(36); # Return
206 xtest_key_release(133); # Super_L
210 'triggered the "Mod4+Return" keybinding');
214 sub listen_for_binding {
216 my $triggered = AnyEvent->condvar;
217 my $i3 = i3(get_socket_path());
218 $i3->connect()->recv;
222 return unless $event->{change} eq 'run';
223 # We look at the command (which is “nop <binding>”) because that is
224 # easier than re-assembling the string representation of
226 $triggered->send($event->{binding}->{command});
231 $t = AnyEvent->timer(
234 $triggered->send('timeout');
240 my $recv = $triggered->recv;
245 =head2 set_xkb_group($group)
247 Changes the current XKB group from the default of 1 to C<$group>, which must be
250 Returns false when there was an X11 error changing the group, true otherwise.
252 =head2 xtest_key_press($detail)
254 Sends a KeyPress event via XTEST, with the specified C<$detail>, i.e. key code.
255 Use C<xev(1)> to find key codes.
257 Returns false when there was an X11 error, true otherwise.
259 =head2 xtest_key_release($detail)
261 Sends a KeyRelease event via XTEST, with the specified C<$detail>, i.e. key code.
262 Use C<xev(1)> to find key codes.
264 Returns false when there was an X11 error, true otherwise.
266 =head2 xtest_button_press($button, $x, $y)
268 Sends a ButtonPress event via XTEST, with the specified C<$button>.
270 Returns false when there was an X11 error, true otherwise.
272 =head2 xtest_button_release($button, $x, $y)
274 Sends a ButtonRelease event via XTEST, with the specified C<$button>.
276 Returns false when there was an X11 error, true otherwise.
280 Michael Stapelberg <michael@i3wm.org>