]> git.sur5r.net Git - i3/i3/blob - testcases/lib/i3test/XTEST.pm
b7a9cdc5e256792d0cafe988ceb1752f81f20d9c
[i3/i3] / testcases / lib / i3test / XTEST.pm
1 package i3test::XTEST;
2 # vim:ts=4:sw=4:expandtab
3
4 use strict;
5 use warnings;
6 use v5.10;
7
8 use Test::More;
9 use i3test::Util qw(get_socket_path);
10 use lib qw(@abs_top_srcdir@/AnyEvent-I3/blib/lib);
11 use AnyEvent::I3;
12 use ExtUtils::PkgConfig;
13
14 use Exporter ();
15 our @EXPORT = qw(
16     inlinec_connect
17     set_xkb_group
18     xtest_key_press
19     xtest_key_release
20     xtest_button_press
21     xtest_button_release
22     listen_for_binding
23     start_binding_capture
24     binding_events
25 );
26
27 =encoding utf-8
28
29 =head1 NAME
30
31 i3test::XTEST - Inline::C wrappers for xcb-xtest and xcb-xkb
32
33 =cut
34
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
38 # ineffective.
39 my %sn_config;
40 BEGIN {
41     %sn_config = ExtUtils::PkgConfig->find('xcb-xkb xcb-xtest');
42 }
43
44 use Inline C => Config => LIBS => $sn_config{libs}, CCFLAGS => $sn_config{cflags};
45 use Inline C => <<'END_OF_C_CODE';
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <stdbool.h>
51 #include <stdint.h>
52
53 #include <xcb/xcb.h>
54 #include <xcb/xkb.h>
55 #include <xcb/xtest.h>
56
57 static xcb_connection_t *conn = NULL;
58
59 bool inlinec_connect() {
60     int screen;
61
62     if ((conn = xcb_connect(NULL, &screen)) == NULL ||
63         xcb_connection_has_error(conn)) {
64         if (conn != NULL) {
65             xcb_disconnect(conn);
66         }
67         fprintf(stderr, "Could not connect to X11\n");
68         return false;
69     }
70
71     if (!xcb_get_extension_data(conn, &xcb_xkb_id)->present) {
72         fprintf(stderr, "XKB not present\n");
73         return false;
74     }
75
76     if (!xcb_get_extension_data(conn, &xcb_test_id)->present) {
77         fprintf(stderr, "XTEST not present\n");
78         return false;
79     }
80
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");
87         free(err);
88         return false;
89     }
90     free(usereply);
91
92     return true;
93 }
94
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     // http://cgit.freedesktop.org/xcb/proto/commit/src/xkb.xml?id=8d7ee5b6ba4cf343f7df70372a3e1f85b82aeed7
101     xcb_void_cookie_t cookie = xcb_xkb_latch_lock_state_checked(
102         conn,
103         XCB_XKB_ID_USE_CORE_KBD, /* deviceSpec */
104         0,                       /* affectModLocks */
105         0,                       /* modLocks */
106         1,                       /* lockGroup */
107         group,                   /* groupLock */
108         0,                       /* affectModLatches */
109         0,                       /* latchGroup */
110         0);                      /* groupLatch */
111     if ((err = xcb_request_check(conn, cookie)) != NULL) {
112         fprintf(stderr, "X error code %d\n", err->error_code);
113         free(err);
114         return false;
115     }
116     return true;
117 }
118
119 bool xtest_input(int type, int detail, int x, int y) {
120     xcb_generic_error_t *err;
121     xcb_void_cookie_t cookie;
122
123     cookie = xcb_test_fake_input_checked(
124         conn,
125         type,             /* type */
126         detail,           /* detail */
127         XCB_CURRENT_TIME, /* time */
128         XCB_NONE,         /* root */
129         x,                /* rootX */
130         y,                /* rootY */
131         XCB_NONE);        /* deviceid */
132     if ((err = xcb_request_check(conn, cookie)) != NULL) {
133         fprintf(stderr, "X error code %d\n", err->error_code);
134         free(err);
135         return false;
136     }
137
138     return true;
139 }
140
141 bool xtest_key(int type, int detail) {
142     return xtest_input(type, detail, 0, 0);
143 }
144
145 bool xtest_key_press(int detail) {
146     return xtest_key(XCB_KEY_PRESS, detail);
147 }
148
149 bool xtest_key_release(int detail) {
150     return xtest_key(XCB_KEY_RELEASE, detail);
151 }
152
153 bool xtest_button_press(int button, int x, int y) {
154     return xtest_input(XCB_BUTTON_PRESS, button, x, y);
155 }
156
157 bool xtest_button_release(int button, int x, int y) {
158     return xtest_input(XCB_BUTTON_RELEASE, button, x, y);
159 }
160
161 END_OF_C_CODE
162
163 sub import {
164     my ($class, %args) = @_;
165     ok(inlinec_connect(), 'Connect to X11, verify XKB and XTEST are present (via Inline::C)');
166     goto \&Exporter::import;
167 }
168
169 =head1 EXPORT
170
171 =cut
172
173 my $i3;
174 our @binding_events;
175
176 =head2 start_binding_capture()
177
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.
180
181   my $pid = launch_with_config($config);
182   start_binding_capture;
183   # …
184   sync_with_i3;
185   is(scalar @i3test::XTEST::binding_events, 2, 'Received exactly 2 binding events');
186
187 =cut
188
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;
194     $i3->subscribe({
195         binding => sub {
196             my ($event) = @_;
197             @binding_events = (@binding_events, $event);
198         },
199     })->recv;
200 }
201
202 =head2 listen_for_binding($cb)
203
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”.
208
209   is(listen_for_binding(
210       sub {
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
215       },
216       ),
217      'Mod4+Return',
218      'triggered the "Mod4+Return" keybinding');
219
220 =cut
221
222 sub listen_for_binding {
223     my ($cb) = @_;
224     my $triggered = AnyEvent->condvar;
225     my $i3 = i3(get_socket_path());
226     $i3->connect()->recv;
227     $i3->subscribe({
228         binding => sub {
229             my ($event) = @_;
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
233             # $event->{binding}.
234             $triggered->send($event->{binding}->{command});
235         },
236     })->recv;
237
238     my $t;
239     $t = AnyEvent->timer(
240         after => 0.5,
241         cb => sub {
242             $triggered->send('timeout');
243         }
244     );
245
246     $cb->();
247
248     my $recv = $triggered->recv;
249     $recv =~ s/^nop //g;
250     return $recv;
251 }
252
253 =head2 set_xkb_group($group)
254
255 Changes the current XKB group from the default of 1 to C<$group>, which must be
256 one of 1, 2, 3, 4.
257
258 Returns false when there was an X11 error changing the group, true otherwise.
259
260 =head2 xtest_key_press($detail)
261
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.
264
265 Returns false when there was an X11 error, true otherwise.
266
267 =head2 xtest_key_release($detail)
268
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.
271
272 Returns false when there was an X11 error, true otherwise.
273
274 =head2 xtest_button_press($button, $x, $y)
275
276 Sends a ButtonPress event via XTEST, with the specified C<$button>.
277
278 Returns false when there was an X11 error, true otherwise.
279
280 =head2 xtest_button_release($button, $x, $y)
281
282 Sends a ButtonRelease event via XTEST, with the specified C<$button>.
283
284 Returns false when there was an X11 error, true otherwise.
285
286 =head1 AUTHOR
287
288 Michael Stapelberg <michael@i3wm.org>
289
290 =cut
291
292 1