]> git.sur5r.net Git - i3/i3/blob - testcases/lib/i3test/XTEST.pm
tests: implement xtest_sync_with_i3
[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     xtest_sync_with_i3
18     set_xkb_group
19     xtest_key_press
20     xtest_key_release
21     xtest_button_press
22     xtest_button_release
23     listen_for_binding
24     start_binding_capture
25     binding_events
26 );
27
28 =encoding utf-8
29
30 =head1 NAME
31
32 i3test::XTEST - Inline::C wrappers for xcb-xtest and xcb-xkb
33
34 =cut
35
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
39 # ineffective.
40 my %sn_config;
41 BEGIN {
42     %sn_config = ExtUtils::PkgConfig->find('xcb-xkb xcb-xtest xcb-util');
43 }
44
45 use Inline C => Config => LIBS => $sn_config{libs}, CCFLAGS => $sn_config{cflags};
46 use Inline C => <<'END_OF_C_CODE';
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <stdbool.h>
52 #include <stdint.h>
53
54 #include <xcb/xcb.h>
55 #include <xcb/xkb.h>
56 #include <xcb/xtest.h>
57 #include <xcb/xcb_aux.h>
58
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;
63
64 bool inlinec_connect() {
65     int screen;
66
67     if ((conn = xcb_connect(NULL, &screen)) == NULL ||
68         xcb_connection_has_error(conn)) {
69         if (conn != NULL) {
70             xcb_disconnect(conn);
71         }
72         fprintf(stderr, "Could not connect to X11\n");
73         return false;
74     }
75
76     if (!xcb_get_extension_data(conn, &xcb_xkb_id)->present) {
77         fprintf(stderr, "XKB not present\n");
78         return false;
79     }
80
81     if (!xcb_get_extension_data(conn, &xcb_test_id)->present) {
82         fprintf(stderr, "XTEST not present\n");
83         return false;
84     }
85
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");
92         free(err);
93         return false;
94     }
95     free(usereply);
96
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;
99     free(reply);
100
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
108                       -15,                            // x
109                       -15,                            // y
110                       1,                              // width
111                       1,                              // height
112                       0,                              // border_width
113                       XCB_WINDOW_CLASS_INPUT_OUTPUT,  // class
114                       XCB_COPY_FROM_PARENT,           // visual
115                       XCB_CW_OVERRIDE_REDIRECT,       // value_mask
116                       (uint32_t[]){
117                           1,  // override_redirect
118                       });     // value_list
119
120     return true;
121 }
122
123 void xtest_sync_with_i3() {
124     xcb_client_message_event_t ev;
125     memset(&ev, '\0', sizeof(xcb_client_message_event_t));
126
127     const int nonce = rand() % 255;
128
129     ev.response_type = XCB_CLIENT_MESSAGE;
130     ev.window = sync_window;
131     ev.type = i3_sync_atom;
132     ev.format = 32;
133     ev.data.data32[0] = sync_window;
134     ev.data.data32[1] = nonce;
135
136     xcb_send_event(conn, false, root_window, XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (char *)&ev);
137     xcb_flush(conn);
138
139     xcb_generic_event_t *event = NULL;
140     while (1) {
141         free(event);
142         if ((event = xcb_wait_for_event(conn)) == NULL) {
143             break;
144         }
145         if (event->response_type == 0) {
146             fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
147             continue;
148         }
149
150         /* Strip off the highest bit (set if the event is generated) */
151         const int type = (event->response_type & 0x7F);
152         switch (type) {
153             case XCB_CLIENT_MESSAGE: {
154                 xcb_client_message_event_t *ev = (xcb_client_message_event_t *)event;
155                 {
156                     const uint32_t got = ev->data.data32[0];
157                     const uint32_t want = sync_window;
158                     if (got != want) {
159                         fprintf(stderr, "Ignoring ClientMessage: unknown window: got %d, want %d\n", got, want);
160                         continue;
161                     }
162                 }
163                 {
164                     const uint32_t got = ev->data.data32[1];
165                     const uint32_t want = nonce;
166                     if (got != want) {
167                         fprintf(stderr, "Ignoring ClientMessage: unknown nonce: got %d, want %d\n", got, want);
168                         continue;
169                     }
170                 }
171                 return;
172             }
173             default:
174                 fprintf(stderr, "Unexpected X11 event of type %d received (XCB_CLIENT_MESSAGE = %d)\n", type, XCB_CLIENT_MESSAGE);
175                 break;
176         }
177     }
178     free(event);
179 }
180
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(
188         conn,
189         XCB_XKB_ID_USE_CORE_KBD, /* deviceSpec */
190         0,                       /* affectModLocks */
191         0,                       /* modLocks */
192         1,                       /* lockGroup */
193         group,                   /* groupLock */
194         0,                       /* affectModLatches */
195         0,                       /* latchGroup */
196         0);                      /* groupLatch */
197     if ((err = xcb_request_check(conn, cookie)) != NULL) {
198         fprintf(stderr, "X error code %d\n", err->error_code);
199         free(err);
200         return false;
201     }
202     return true;
203 }
204
205 bool xtest_input(int type, int detail, int x, int y) {
206     xcb_generic_error_t *err;
207     xcb_void_cookie_t cookie;
208
209     cookie = xcb_test_fake_input_checked(
210         conn,
211         type,             /* type */
212         detail,           /* detail */
213         XCB_CURRENT_TIME, /* time */
214         XCB_NONE,         /* root */
215         x,                /* rootX */
216         y,                /* rootY */
217         XCB_NONE);        /* deviceid */
218     if ((err = xcb_request_check(conn, cookie)) != NULL) {
219         fprintf(stderr, "X error code %d\n", err->error_code);
220         free(err);
221         return false;
222     }
223
224     return true;
225 }
226
227 bool xtest_key(int type, int detail) {
228     return xtest_input(type, detail, 0, 0);
229 }
230
231 bool xtest_key_press(int detail) {
232     return xtest_key(XCB_KEY_PRESS, detail);
233 }
234
235 bool xtest_key_release(int detail) {
236     return xtest_key(XCB_KEY_RELEASE, detail);
237 }
238
239 bool xtest_button_press(int button, int x, int y) {
240     return xtest_input(XCB_BUTTON_PRESS, button, x, y);
241 }
242
243 bool xtest_button_release(int button, int x, int y) {
244     return xtest_input(XCB_BUTTON_RELEASE, button, x, y);
245 }
246
247 END_OF_C_CODE
248
249 sub import {
250     my ($class, %args) = @_;
251     ok(inlinec_connect(), 'Connect to X11, verify XKB and XTEST are present (via Inline::C)');
252     goto \&Exporter::import;
253 }
254
255 =head1 EXPORT
256
257 =cut
258
259 my $i3;
260 our @binding_events;
261
262 =head2 start_binding_capture()
263
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.
266
267   my $pid = launch_with_config($config);
268   start_binding_capture;
269   # …
270   sync_with_i3;
271   is(scalar @i3test::XTEST::binding_events, 2, 'Received exactly 2 binding events');
272
273 =cut
274
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;
280     $i3->subscribe({
281         binding => sub {
282             my ($event) = @_;
283             @binding_events = (@binding_events, $event);
284         },
285     })->recv;
286 }
287
288 =head2 listen_for_binding($cb)
289
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”.
294
295   is(listen_for_binding(
296       sub {
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
301       },
302       ),
303      'Mod4+Return',
304      'triggered the "Mod4+Return" keybinding');
305
306 =cut
307
308 sub listen_for_binding {
309     my ($cb) = @_;
310     my $triggered = AnyEvent->condvar;
311     my $i3 = i3(get_socket_path());
312     $i3->connect()->recv;
313     $i3->subscribe({
314         binding => sub {
315             my ($event) = @_;
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
319             # $event->{binding}.
320             $triggered->send($event->{binding}->{command});
321         },
322     })->recv;
323
324     my $t;
325     $t = AnyEvent->timer(
326         after => 0.5,
327         cb => sub {
328             $triggered->send('timeout');
329         }
330     );
331
332     $cb->();
333
334     my $recv = $triggered->recv;
335     $recv =~ s/^nop //g;
336     return $recv;
337 }
338
339 =head2 set_xkb_group($group)
340
341 Changes the current XKB group from the default of 1 to C<$group>, which must be
342 one of 1, 2, 3, 4.
343
344 Returns false when there was an X11 error changing the group, true otherwise.
345
346 =head2 xtest_key_press($detail)
347
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.
350
351 Returns false when there was an X11 error, true otherwise.
352
353 =head2 xtest_key_release($detail)
354
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.
357
358 Returns false when there was an X11 error, true otherwise.
359
360 =head2 xtest_button_press($button, $x, $y)
361
362 Sends a ButtonPress event via XTEST, with the specified C<$button>.
363
364 Returns false when there was an X11 error, true otherwise.
365
366 =head2 xtest_button_release($button, $x, $y)
367
368 Sends a ButtonRelease event via XTEST, with the specified C<$button>.
369
370 Returns false when there was an X11 error, true otherwise.
371
372 =head2 xtest_sync_with_i3()
373
374 Ensures i3 has processed all X11 events which were triggered by this module.
375
376 =head1 AUTHOR
377
378 Michael Stapelberg <michael@i3wm.org>
379
380 =cut
381
382 1