]> git.sur5r.net Git - i3/i3/blob - testcases/lib/i3test/XTEST.pm
Synchronize with i3bar+i3, not just 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
18     xtest_sync_with_i3
19     set_xkb_group
20     xtest_key_press
21     xtest_key_release
22     xtest_button_press
23     xtest_button_release
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 xcb-util');
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 #include <xcb/xcb_aux.h>
57
58 static xcb_connection_t *conn = NULL;
59 static xcb_window_t sync_window;
60 static xcb_window_t root_window;
61 static xcb_atom_t i3_sync_atom;
62
63 bool inlinec_connect() {
64     int screen;
65
66     if ((conn = xcb_connect(NULL, &screen)) == NULL ||
67         xcb_connection_has_error(conn)) {
68         if (conn != NULL) {
69             xcb_disconnect(conn);
70         }
71         fprintf(stderr, "Could not connect to X11\n");
72         return false;
73     }
74
75     if (!xcb_get_extension_data(conn, &xcb_xkb_id)->present) {
76         fprintf(stderr, "XKB not present\n");
77         return false;
78     }
79
80     if (!xcb_get_extension_data(conn, &xcb_test_id)->present) {
81         fprintf(stderr, "XTEST not present\n");
82         return false;
83     }
84
85     xcb_generic_error_t *err = NULL;
86     xcb_xkb_use_extension_reply_t *usereply;
87     usereply = xcb_xkb_use_extension_reply(
88         conn, xcb_xkb_use_extension(conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION), &err);
89     if (err != NULL || usereply == NULL) {
90         fprintf(stderr, "xcb_xkb_use_extension() failed\n");
91         free(err);
92         return false;
93     }
94     free(usereply);
95
96     xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, xcb_intern_atom(conn, 0, strlen("I3_SYNC"), "I3_SYNC"), NULL);
97     i3_sync_atom = reply->atom;
98     free(reply);
99
100     xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screen);
101     root_window = root_screen->root;
102     sync_window = xcb_generate_id(conn);
103     xcb_create_window(conn,
104                       XCB_COPY_FROM_PARENT,           // depth
105                       sync_window,                    // window
106                       root_window,                    // parent
107                       -15,                            // x
108                       -15,                            // y
109                       1,                              // width
110                       1,                              // height
111                       0,                              // border_width
112                       XCB_WINDOW_CLASS_INPUT_OUTPUT,  // class
113                       XCB_COPY_FROM_PARENT,           // visual
114                       XCB_CW_OVERRIDE_REDIRECT,       // value_mask
115                       (uint32_t[]){
116                           1,  // override_redirect
117                       });     // value_list
118
119     return true;
120 }
121
122 void xtest_sync_with(int window) {
123     xcb_client_message_event_t ev;
124     memset(&ev, '\0', sizeof(xcb_client_message_event_t));
125
126     const int nonce = rand() % 255;
127
128     ev.response_type = XCB_CLIENT_MESSAGE;
129     ev.window = sync_window;
130     ev.type = i3_sync_atom;
131     ev.format = 32;
132     ev.data.data32[0] = sync_window;
133     ev.data.data32[1] = nonce;
134
135     xcb_send_event(conn, false, (xcb_window_t)window, XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (char *)&ev);
136     xcb_flush(conn);
137
138     xcb_generic_event_t *event = NULL;
139     while (1) {
140         free(event);
141         if ((event = xcb_wait_for_event(conn)) == NULL) {
142             break;
143         }
144         if (event->response_type == 0) {
145             fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
146             continue;
147         }
148
149         /* Strip off the highest bit (set if the event is generated) */
150         const int type = (event->response_type & 0x7F);
151         switch (type) {
152             case XCB_CLIENT_MESSAGE: {
153                 xcb_client_message_event_t *ev = (xcb_client_message_event_t *)event;
154                 {
155                     const uint32_t got = ev->data.data32[0];
156                     const uint32_t want = sync_window;
157                     if (got != want) {
158                         fprintf(stderr, "Ignoring ClientMessage: unknown window: got %d, want %d\n", got, want);
159                         continue;
160                     }
161                 }
162                 {
163                     const uint32_t got = ev->data.data32[1];
164                     const uint32_t want = nonce;
165                     if (got != want) {
166                         fprintf(stderr, "Ignoring ClientMessage: unknown nonce: got %d, want %d\n", got, want);
167                         continue;
168                     }
169                 }
170                 return;
171             }
172             default:
173                 fprintf(stderr, "Unexpected X11 event of type %d received (XCB_CLIENT_MESSAGE = %d)\n", type, XCB_CLIENT_MESSAGE);
174                 break;
175         }
176     }
177     free(event);
178 }
179
180 void xtest_sync_with_i3() {
181     xtest_sync_with((int)root_window);
182 }
183
184 // NOTE: while |group| should be a uint8_t, Inline::C will not define the
185 // function unless we use an int.
186 bool set_xkb_group(int group) {
187     xcb_generic_error_t *err = NULL;
188     // Needs libxcb ≥ 1.11 so that we have the following bug fix:
189     // https://cgit.freedesktop.org/xcb/proto/commit/src/xkb.xml?id=8d7ee5b6ba4cf343f7df70372a3e1f85b82aeed7
190     xcb_void_cookie_t cookie = xcb_xkb_latch_lock_state_checked(
191         conn,
192         XCB_XKB_ID_USE_CORE_KBD, /* deviceSpec */
193         0,                       /* affectModLocks */
194         0,                       /* modLocks */
195         1,                       /* lockGroup */
196         group,                   /* groupLock */
197         0,                       /* affectModLatches */
198         0,                       /* latchGroup */
199         0);                      /* groupLatch */
200     if ((err = xcb_request_check(conn, cookie)) != NULL) {
201         fprintf(stderr, "X error code %d\n", err->error_code);
202         free(err);
203         return false;
204     }
205     return true;
206 }
207
208 bool xtest_input(int type, int detail, int x, int y) {
209     xcb_generic_error_t *err;
210     xcb_void_cookie_t cookie;
211
212     cookie = xcb_test_fake_input_checked(
213         conn,
214         type,             /* type */
215         detail,           /* detail */
216         XCB_CURRENT_TIME, /* time */
217         XCB_NONE,         /* root */
218         x,                /* rootX */
219         y,                /* rootY */
220         XCB_NONE);        /* deviceid */
221     if ((err = xcb_request_check(conn, cookie)) != NULL) {
222         fprintf(stderr, "X error code %d\n", err->error_code);
223         free(err);
224         return false;
225     }
226
227     return true;
228 }
229
230 bool xtest_key(int type, int detail) {
231     return xtest_input(type, detail, 0, 0);
232 }
233
234 bool xtest_key_press(int detail) {
235     return xtest_key(XCB_KEY_PRESS, detail);
236 }
237
238 bool xtest_key_release(int detail) {
239     return xtest_key(XCB_KEY_RELEASE, detail);
240 }
241
242 bool xtest_button_press(int button, int x, int y) {
243     return xtest_input(XCB_BUTTON_PRESS, button, x, y);
244 }
245
246 bool xtest_button_release(int button, int x, int y) {
247     return xtest_input(XCB_BUTTON_RELEASE, button, x, y);
248 }
249
250 END_OF_C_CODE
251
252 sub import {
253     my ($class, %args) = @_;
254     ok(inlinec_connect(), 'Connect to X11, verify XKB and XTEST are present (via Inline::C)');
255     goto \&Exporter::import;
256 }
257
258 =head1 EXPORT
259
260 =cut
261
262 =head2 set_xkb_group($group)
263
264 Changes the current XKB group from the default of 1 to C<$group>, which must be
265 one of 1, 2, 3, 4.
266
267 Returns false when there was an X11 error changing the group, true otherwise.
268
269 =head2 xtest_key_press($detail)
270
271 Sends a KeyPress event via XTEST, with the specified C<$detail>, i.e. key code.
272 Use C<xev(1)> to find key codes.
273
274 Returns false when there was an X11 error, true otherwise.
275
276 =head2 xtest_key_release($detail)
277
278 Sends a KeyRelease event via XTEST, with the specified C<$detail>, i.e. key code.
279 Use C<xev(1)> to find key codes.
280
281 Returns false when there was an X11 error, true otherwise.
282
283 =head2 xtest_button_press($button, $x, $y)
284
285 Sends a ButtonPress event via XTEST, with the specified C<$button>.
286
287 Returns false when there was an X11 error, true otherwise.
288
289 =head2 xtest_button_release($button, $x, $y)
290
291 Sends a ButtonRelease event via XTEST, with the specified C<$button>.
292
293 Returns false when there was an X11 error, true otherwise.
294
295 =head2 xtest_sync_with($window)
296
297 Ensures the specified window has processed all X11 events which were triggered
298 by this module, provided the window response to the i3 sync protocol.
299
300 =head2 xtest_sync_with_i3()
301
302 Ensures i3 has processed all X11 events which were triggered by this module.
303
304 =head1 AUTHOR
305
306 Michael Stapelberg <michael@i3wm.org>
307
308 =cut
309
310 1