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');
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>
57 static xcb_connection_t *conn = NULL;
59 bool inlinec_connect() {
62 if ((conn = xcb_connect(NULL, &screen)) == NULL ||
63 xcb_connection_has_error(conn)) {
67 fprintf(stderr, "Could not connect to X11\n");
71 if (!xcb_get_extension_data(conn, &xcb_xkb_id)->present) {
72 fprintf(stderr, "XKB not present\n");
76 if (!xcb_get_extension_data(conn, &xcb_test_id)->present) {
77 fprintf(stderr, "XTEST not present\n");
81 xcb_generic_error_t *err = NULL;
82 xcb_xkb_use_extension_reply_t *usereply;
83 usereply = xcb_xkb_use_extension_reply(
84 conn, xcb_xkb_use_extension(conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION), &err);
85 if (err != NULL || usereply == NULL) {
86 fprintf(stderr, "xcb_xkb_use_extension() failed\n");
95 // NOTE: while |group| should be a uint8_t, Inline::C will not define the
96 // function unless we use an int.
97 bool set_xkb_group(int group) {
98 xcb_generic_error_t *err = NULL;
99 // Needs libxcb ≥ 1.11 so that we have the following bug fix:
100 // https://cgit.freedesktop.org/xcb/proto/commit/src/xkb.xml?id=8d7ee5b6ba4cf343f7df70372a3e1f85b82aeed7
101 xcb_void_cookie_t cookie = xcb_xkb_latch_lock_state_checked(
103 XCB_XKB_ID_USE_CORE_KBD, /* deviceSpec */
104 0, /* affectModLocks */
107 group, /* groupLock */
108 0, /* affectModLatches */
111 if ((err = xcb_request_check(conn, cookie)) != NULL) {
112 fprintf(stderr, "X error code %d\n", err->error_code);
119 bool xtest_input(int type, int detail, int x, int y) {
120 xcb_generic_error_t *err;
121 xcb_void_cookie_t cookie;
123 cookie = xcb_test_fake_input_checked(
127 XCB_CURRENT_TIME, /* time */
131 XCB_NONE); /* deviceid */
132 if ((err = xcb_request_check(conn, cookie)) != NULL) {
133 fprintf(stderr, "X error code %d\n", err->error_code);
141 bool xtest_key(int type, int detail) {
142 return xtest_input(type, detail, 0, 0);
145 bool xtest_key_press(int detail) {
146 return xtest_key(XCB_KEY_PRESS, detail);
149 bool xtest_key_release(int detail) {
150 return xtest_key(XCB_KEY_RELEASE, detail);
153 bool xtest_button_press(int button, int x, int y) {
154 return xtest_input(XCB_BUTTON_PRESS, button, x, y);
157 bool xtest_button_release(int button, int x, int y) {
158 return xtest_input(XCB_BUTTON_RELEASE, button, x, y);
164 my ($class, %args) = @_;
165 ok(inlinec_connect(), 'Connect to X11, verify XKB and XTEST are present (via Inline::C)');
166 goto \&Exporter::import;
176 =head2 start_binding_capture()
178 Captures all binding events sent by i3 in the C<@binding_events> symbol, so
179 that you can verify the correct number of binding events was generated.
181 my $pid = launch_with_config($config);
182 start_binding_capture;
185 is(scalar @i3test::XTEST::binding_events, 2, 'Received exactly 2 binding events');
189 sub start_binding_capture {
190 # Store a copy of each binding event so that we can count the expected
191 # events in test cases.
192 $i3 = i3(get_socket_path());
193 $i3->connect()->recv;
197 @binding_events = (@binding_events, $event);
202 =head2 listen_for_binding($cb)
204 Helper function to evaluate whether sending KeyPress/KeyRelease events via
205 XTEST triggers an i3 key binding or not (with a timeout of 0.5s). Expects key
206 bindings to be configured in the form “bindsym <binding> nop <binding>”, e.g.
207 “bindsym Mod4+Return nop Mod4+Return”.
209 is(listen_for_binding(
211 xtest_key_press(133); # Super_L
212 xtest_key_press(36); # Return
213 xtest_key_release(36); # Return
214 xtest_key_release(133); # Super_L
218 'triggered the "Mod4+Return" keybinding');
222 sub listen_for_binding {
224 my $triggered = AnyEvent->condvar;
225 my $i3 = i3(get_socket_path());
226 $i3->connect()->recv;
230 return unless $event->{change} eq 'run';
231 # We look at the command (which is “nop <binding>”) because that is
232 # easier than re-assembling the string representation of
234 $triggered->send($event->{binding}->{command});
239 $t = AnyEvent->timer(
242 $triggered->send('timeout');
248 my $recv = $triggered->recv;
253 =head2 set_xkb_group($group)
255 Changes the current XKB group from the default of 1 to C<$group>, which must be
258 Returns false when there was an X11 error changing the group, true otherwise.
260 =head2 xtest_key_press($detail)
262 Sends a KeyPress event via XTEST, with the specified C<$detail>, i.e. key code.
263 Use C<xev(1)> to find key codes.
265 Returns false when there was an X11 error, true otherwise.
267 =head2 xtest_key_release($detail)
269 Sends a KeyRelease event via XTEST, with the specified C<$detail>, i.e. key code.
270 Use C<xev(1)> to find key codes.
272 Returns false when there was an X11 error, true otherwise.
274 =head2 xtest_button_press($button, $x, $y)
276 Sends a ButtonPress event via XTEST, with the specified C<$button>.
278 Returns false when there was an X11 error, true otherwise.
280 =head2 xtest_button_release($button, $x, $y)
282 Sends a ButtonRelease event via XTEST, with the specified C<$button>.
284 Returns false when there was an X11 error, true otherwise.
288 Michael Stapelberg <michael@i3wm.org>