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