2 # vim:ts=4:sw=4:expandtab
8 use i3test i3_autostart => 0;
9 use lib qw(@abs_top_srcdir@/AnyEvent-I3/blib/lib);
11 use ExtUtils::PkgConfig;
30 i3test::XTEST - Inline::C wrappers for xcb-xtest and xcb-xkb
34 # We need to use libxcb-xkb because xdotool cannot trigger ISO_Next_Group
35 # anymore: it contains code to set the XKB group to 1 and then restore the
36 # previous group, effectively rendering any keys that switch groups
40 %sn_config = ExtUtils::PkgConfig->find('xcb-xkb xcb-xtest');
43 use Inline C => Config => LIBS => $sn_config{libs}, CCFLAGS => $sn_config{cflags};
44 use Inline C => <<'END_OF_C_CODE';
54 #include <xcb/xtest.h>
56 static xcb_connection_t *conn = NULL;
58 bool inlinec_connect() {
61 if ((conn = xcb_connect(NULL, &screen)) == NULL ||
62 xcb_connection_has_error(conn)) {
66 fprintf(stderr, "Could not connect to X11\n");
70 if (!xcb_get_extension_data(conn, &xcb_xkb_id)->present) {
71 fprintf(stderr, "XKB not present\n");
75 if (!xcb_get_extension_data(conn, &xcb_test_id)->present) {
76 fprintf(stderr, "XTEST not present\n");
80 xcb_generic_error_t *err = NULL;
81 xcb_xkb_use_extension_reply_t *usereply;
82 usereply = xcb_xkb_use_extension_reply(
83 conn, xcb_xkb_use_extension(conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION), &err);
84 if (err != NULL || usereply == NULL) {
85 fprintf(stderr, "xcb_xkb_use_extension() failed\n");
94 // NOTE: while |group| should be a uint8_t, Inline::C will not define the
95 // function unless we use an int.
96 bool set_xkb_group(int group) {
97 xcb_generic_error_t *err = NULL;
98 // Needs libxcb ≥ 1.11 so that we have the following bug fix:
99 // http://cgit.freedesktop.org/xcb/proto/commit/src/xkb.xml?id=8d7ee5b6ba4cf343f7df70372a3e1f85b82aeed7
100 xcb_void_cookie_t cookie = xcb_xkb_latch_lock_state_checked(
102 XCB_XKB_ID_USE_CORE_KBD, /* deviceSpec */
103 0, /* affectModLocks */
106 group, /* groupLock */
107 0, /* affectModLatches */
110 if ((err = xcb_request_check(conn, cookie)) != NULL) {
111 fprintf(stderr, "X error code %d\n", err->error_code);
118 bool xtest_input(int type, int detail, int x, int y) {
119 xcb_generic_error_t *err;
120 xcb_void_cookie_t cookie;
122 cookie = xcb_test_fake_input_checked(
126 XCB_CURRENT_TIME, /* time */
130 XCB_NONE); /* deviceid */
131 if ((err = xcb_request_check(conn, cookie)) != NULL) {
132 fprintf(stderr, "X error code %d\n", err->error_code);
140 bool xtest_key(int type, int detail) {
141 return xtest_input(type, detail, 0, 0);
144 bool xtest_key_press(int detail) {
145 return xtest_key(XCB_KEY_PRESS, detail);
148 bool xtest_key_release(int detail) {
149 return xtest_key(XCB_KEY_RELEASE, detail);
152 bool xtest_button_press(int button, int x, int y) {
153 return xtest_input(XCB_BUTTON_PRESS, button, x, y);
156 bool xtest_button_release(int button, int x, int y) {
157 return xtest_input(XCB_BUTTON_RELEASE, button, x, y);
163 my ($class, %args) = @_;
164 ok(inlinec_connect(), 'Connect to X11, verify XKB and XTEST are present (via Inline::C)');
165 goto \&Exporter::import;
175 =head2 start_binding_capture()
177 Captures all binding events sent by i3 in the C<@binding_events> symbol, so
178 that you can verify the correct number of binding events was generated.
180 my $pid = launch_with_config($config);
181 start_binding_capture;
184 is(scalar @i3test::XTEST::binding_events, 2, 'Received exactly 2 binding events');
188 sub start_binding_capture {
189 # Store a copy of each binding event so that we can count the expected
190 # events in test cases.
191 $i3 = i3(get_socket_path());
192 $i3->connect()->recv;
196 @binding_events = (@binding_events, $event);
201 =head2 listen_for_binding($cb)
203 Helper function to evaluate whether sending KeyPress/KeyRelease events via
204 XTEST triggers an i3 key binding or not (with a timeout of 0.5s). Expects key
205 bindings to be configured in the form “bindsym <binding> nop <binding>”, e.g.
206 “bindsym Mod4+Return nop Mod4+Return”.
208 is(listen_for_binding(
210 xtest_key_press(133); # Super_L
211 xtest_key_press(36); # Return
212 xtest_key_release(36); # Return
213 xtest_key_release(133); # Super_L
217 'triggered the "Mod4+Return" keybinding');
221 sub listen_for_binding {
223 my $triggered = AnyEvent->condvar;
224 my $i3 = i3(get_socket_path());
225 $i3->connect()->recv;
229 return unless $event->{change} eq 'run';
230 # We look at the command (which is “nop <binding>”) because that is
231 # easier than re-assembling the string representation of
233 $triggered->send($event->{binding}->{command});
238 $t = AnyEvent->timer(
241 $triggered->send('timeout');
247 my $recv = $triggered->recv;
252 =head2 set_xkb_group($group)
254 Changes the current XKB group from the default of 1 to C<$group>, which must be
257 Returns false when there was an X11 error changing the group, true otherwise.
259 =head2 xtest_key_press($detail)
261 Sends a KeyPress 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_key_release($detail)
268 Sends a KeyRelease event via XTEST, with the specified C<$detail>, i.e. key code.
269 Use C<xev(1)> to find key codes.
271 Returns false when there was an X11 error, true otherwise.
273 =head2 xtest_button_press($button, $x, $y)
275 Sends a ButtonPress event via XTEST, with the specified C<$button>.
277 Returns false when there was an X11 error, true otherwise.
279 =head2 xtest_button_release($button, $x, $y)
281 Sends a ButtonRelease event via XTEST, with the specified C<$button>.
283 Returns false when there was an X11 error, true otherwise.
287 Michael Stapelberg <michael@i3wm.org>