2 # vim:ts=4:sw=4:expandtab
9 use i3test::Util qw(get_socket_path);
10 use lib qw(@abs_top_srcdir@/AnyEvent-I3/blib/lib);
12 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 xcb-util');
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>
55 #include <xcb/xcb_aux.h>
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;
62 bool inlinec_connect() {
65 if ((conn = xcb_connect(NULL, &screen)) == NULL ||
66 xcb_connection_has_error(conn)) {
70 fprintf(stderr, "Could not connect to X11\n");
74 if (!xcb_get_extension_data(conn, &xcb_xkb_id)->present) {
75 fprintf(stderr, "XKB not present\n");
79 if (!xcb_get_extension_data(conn, &xcb_test_id)->present) {
80 fprintf(stderr, "XTEST not present\n");
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");
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;
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
111 XCB_WINDOW_CLASS_INPUT_OUTPUT, // class
112 XCB_COPY_FROM_PARENT, // visual
113 XCB_CW_OVERRIDE_REDIRECT, // value_mask
115 1, // override_redirect
121 void xtest_sync_with_i3() {
122 xcb_client_message_event_t ev;
123 memset(&ev, '\0', sizeof(xcb_client_message_event_t));
125 const int nonce = rand() % 255;
127 ev.response_type = XCB_CLIENT_MESSAGE;
128 ev.window = sync_window;
129 ev.type = i3_sync_atom;
131 ev.data.data32[0] = sync_window;
132 ev.data.data32[1] = nonce;
134 xcb_send_event(conn, false, root_window, XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (char *)&ev);
137 xcb_generic_event_t *event = NULL;
140 if ((event = xcb_wait_for_event(conn)) == NULL) {
143 if (event->response_type == 0) {
144 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
148 /* Strip off the highest bit (set if the event is generated) */
149 const int type = (event->response_type & 0x7F);
151 case XCB_CLIENT_MESSAGE: {
152 xcb_client_message_event_t *ev = (xcb_client_message_event_t *)event;
154 const uint32_t got = ev->data.data32[0];
155 const uint32_t want = sync_window;
157 fprintf(stderr, "Ignoring ClientMessage: unknown window: got %d, want %d\n", got, want);
162 const uint32_t got = ev->data.data32[1];
163 const uint32_t want = nonce;
165 fprintf(stderr, "Ignoring ClientMessage: unknown nonce: got %d, want %d\n", got, want);
172 fprintf(stderr, "Unexpected X11 event of type %d received (XCB_CLIENT_MESSAGE = %d)\n", type, XCB_CLIENT_MESSAGE);
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(
187 XCB_XKB_ID_USE_CORE_KBD, /* deviceSpec */
188 0, /* affectModLocks */
191 group, /* groupLock */
192 0, /* affectModLatches */
195 if ((err = xcb_request_check(conn, cookie)) != NULL) {
196 fprintf(stderr, "X error code %d\n", err->error_code);
203 bool xtest_input(int type, int detail, int x, int y) {
204 xcb_generic_error_t *err;
205 xcb_void_cookie_t cookie;
207 cookie = xcb_test_fake_input_checked(
211 XCB_CURRENT_TIME, /* time */
215 XCB_NONE); /* deviceid */
216 if ((err = xcb_request_check(conn, cookie)) != NULL) {
217 fprintf(stderr, "X error code %d\n", err->error_code);
225 bool xtest_key(int type, int detail) {
226 return xtest_input(type, detail, 0, 0);
229 bool xtest_key_press(int detail) {
230 return xtest_key(XCB_KEY_PRESS, detail);
233 bool xtest_key_release(int detail) {
234 return xtest_key(XCB_KEY_RELEASE, detail);
237 bool xtest_button_press(int button, int x, int y) {
238 return xtest_input(XCB_BUTTON_PRESS, button, x, y);
241 bool xtest_button_release(int button, int x, int y) {
242 return xtest_input(XCB_BUTTON_RELEASE, button, x, y);
248 my ($class, %args) = @_;
249 ok(inlinec_connect(), 'Connect to X11, verify XKB and XTEST are present (via Inline::C)');
250 goto \&Exporter::import;
257 =head2 set_xkb_group($group)
259 Changes the current XKB group from the default of 1 to C<$group>, which must be
262 Returns false when there was an X11 error changing the group, true otherwise.
264 =head2 xtest_key_press($detail)
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.
269 Returns false when there was an X11 error, true otherwise.
271 =head2 xtest_key_release($detail)
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.
276 Returns false when there was an X11 error, true otherwise.
278 =head2 xtest_button_press($button, $x, $y)
280 Sends a ButtonPress event via XTEST, with the specified C<$button>.
282 Returns false when there was an X11 error, true otherwise.
284 =head2 xtest_button_release($button, $x, $y)
286 Sends a ButtonRelease event via XTEST, with the specified C<$button>.
288 Returns false when there was an X11 error, true otherwise.
290 =head2 xtest_sync_with_i3()
292 Ensures i3 has processed all X11 events which were triggered by this module.
296 Michael Stapelberg <michael@i3wm.org>