2 * Copyright © 2013 Ran Benita <ran234@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
30 #include "xkbcommon/xkbcommon-x11.h"
34 * Note: This program only handles the core keyboard device for now.
35 * It should be straigtforward to change struct keyboard to a list of
36 * keyboards with device IDs, as in test/interactive-evdev.c. This would
39 * - Initially listing the keyboard devices.
40 * - Listening to device changes.
41 * - Matching events to their devices.
43 * XKB itself knows about xinput1 devices, and most requests and events are
46 * In order to list the devices and react to changes, you need xinput1/2.
47 * You also need xinput for the key press/release event, since the core
48 * protocol key press event does not carry a device ID to match on.
52 xcb_connection_t *conn;
53 uint8_t first_xkb_event;
54 struct xkb_context *ctx;
56 struct xkb_keymap *keymap;
57 struct xkb_state *state;
61 static bool terminate;
64 select_xkb_events_for_device(xcb_connection_t *conn, int32_t device_id)
68 (XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY |
69 XCB_XKB_EVENT_TYPE_MAP_NOTIFY |
70 XCB_XKB_EVENT_TYPE_STATE_NOTIFY),
72 required_nkn_details =
73 (XCB_XKB_NKN_DETAIL_KEYCODES),
76 (XCB_XKB_MAP_PART_KEY_TYPES |
77 XCB_XKB_MAP_PART_KEY_SYMS |
78 XCB_XKB_MAP_PART_MODIFIER_MAP |
79 XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS |
80 XCB_XKB_MAP_PART_KEY_ACTIONS |
81 XCB_XKB_MAP_PART_VIRTUAL_MODS |
82 XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP),
84 required_state_details =
85 (XCB_XKB_STATE_PART_MODIFIER_BASE |
86 XCB_XKB_STATE_PART_MODIFIER_LATCH |
87 XCB_XKB_STATE_PART_MODIFIER_LOCK |
88 XCB_XKB_STATE_PART_GROUP_BASE |
89 XCB_XKB_STATE_PART_GROUP_LATCH |
90 XCB_XKB_STATE_PART_GROUP_LOCK),
93 static const xcb_xkb_select_events_details_t details = {
94 .affectNewKeyboard = required_nkn_details,
95 .newKeyboardDetails = required_nkn_details,
96 .affectState = required_state_details,
97 .stateDetails = required_state_details,
100 xcb_void_cookie_t cookie =
101 xcb_xkb_select_events_aux_checked(conn,
103 required_events, /* affectWhich */
106 required_map_parts, /* affectMap */
107 required_map_parts, /* map */
108 &details); /* details */
110 xcb_generic_error_t *error = xcb_request_check(conn, cookie);
120 update_keymap(struct keyboard *kbd)
122 struct xkb_keymap *new_keymap;
123 struct xkb_state *new_state;
125 new_keymap = xkb_x11_keymap_new_from_device(kbd->ctx, kbd->conn,
130 new_state = xkb_x11_state_new_from_device(new_keymap, kbd->conn,
136 printf("Keymap updated!\n");
138 xkb_state_unref(kbd->state);
139 xkb_keymap_unref(kbd->keymap);
140 kbd->keymap = new_keymap;
141 kbd->state = new_state;
145 xkb_keymap_unref(new_keymap);
151 init_kbd(struct keyboard *kbd, xcb_connection_t *conn, uint8_t first_xkb_event,
152 int32_t device_id, struct xkb_context *ctx)
157 kbd->first_xkb_event = first_xkb_event;
161 kbd->device_id = device_id;
163 ret = update_keymap(kbd);
167 ret = select_xkb_events_for_device(conn, device_id);
174 xkb_state_unref(kbd->state);
175 xkb_keymap_unref(kbd->keymap);
181 deinit_kbd(struct keyboard *kbd)
183 xkb_state_unref(kbd->state);
184 xkb_keymap_unref(kbd->keymap);
188 process_xkb_event(xcb_generic_event_t *gevent, struct keyboard *kbd)
192 uint8_t response_type;
195 xcb_timestamp_t time;
198 xcb_xkb_new_keyboard_notify_event_t new_keyboard_notify;
199 xcb_xkb_map_notify_event_t map_notify;
200 xcb_xkb_state_notify_event_t state_notify;
201 } *event = (union xkb_event *) gevent;
203 if (event->any.deviceID != kbd->device_id)
207 * XkbNewKkdNotify and XkbMapNotify together capture all sorts of keymap
208 * updates (e.g. xmodmap, xkbcomp, setxkbmap), with minimal redundent
211 switch (event->any.xkbType) {
212 case XCB_XKB_NEW_KEYBOARD_NOTIFY:
213 if (event->new_keyboard_notify.changed & XCB_XKB_NKN_DETAIL_KEYCODES)
217 case XCB_XKB_MAP_NOTIFY:
221 case XCB_XKB_STATE_NOTIFY:
222 xkb_state_update_mask(kbd->state,
223 event->state_notify.baseMods,
224 event->state_notify.latchedMods,
225 event->state_notify.lockedMods,
226 event->state_notify.baseGroup,
227 event->state_notify.latchedGroup,
228 event->state_notify.lockedGroup);
234 process_event(xcb_generic_event_t *gevent, struct keyboard *kbd)
236 switch (gevent->response_type) {
237 case XCB_KEY_PRESS: {
238 xcb_key_press_event_t *event = (xcb_key_press_event_t *) gevent;
239 xkb_keycode_t keycode = event->detail;
241 test_print_keycode_state(kbd->state, NULL, keycode,
242 XKB_CONSUMED_MODE_XKB);
250 if (gevent->response_type == kbd->first_xkb_event)
251 process_xkb_event(gevent, kbd);
257 loop(xcb_connection_t *conn, struct keyboard *kbd)
260 xcb_generic_event_t *event;
262 switch (xcb_connection_has_error(conn)) {
267 "Closed connection to X server: connection error\n");
269 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
271 "Closed connection to X server: extension not supported\n");
275 "Closed connection to X server: error code %d\n",
276 xcb_connection_has_error(conn));
280 event = xcb_wait_for_event(conn);
285 process_event(event, kbd);
293 create_capture_window(xcb_connection_t *conn)
295 xcb_generic_error_t *error;
296 xcb_void_cookie_t cookie;
297 xcb_screen_t *screen =
298 xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
299 xcb_window_t window = xcb_generate_id(conn);
300 uint32_t values[2] = {
302 XCB_EVENT_MASK_KEY_PRESS,
305 cookie = xcb_create_window_checked(conn, XCB_COPY_FROM_PARENT,
306 window, screen->root,
308 XCB_WINDOW_CLASS_INPUT_OUTPUT,
310 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
312 if ((error = xcb_request_check(conn, cookie)) != NULL) {
317 cookie = xcb_map_window_checked(conn, window);
318 if ((error = xcb_request_check(conn, cookie)) != NULL) {
327 main(int argc, char *argv[])
330 xcb_connection_t *conn;
331 uint8_t first_xkb_event;
332 int32_t core_kbd_device_id;
333 struct xkb_context *ctx;
334 struct keyboard core_kbd;
336 setlocale(LC_ALL, "");
338 conn = xcb_connect(NULL, NULL);
339 if (!conn || xcb_connection_has_error(conn)) {
340 fprintf(stderr, "Couldn't connect to X server: error code %d\n",
341 conn ? xcb_connection_has_error(conn) : -1);
346 ret = xkb_x11_setup_xkb_extension(conn,
347 XKB_X11_MIN_MAJOR_XKB_VERSION,
348 XKB_X11_MIN_MINOR_XKB_VERSION,
349 XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
350 NULL, NULL, &first_xkb_event, NULL);
352 fprintf(stderr, "Couldn't setup XKB extension\n");
356 ctx = test_get_context(0);
359 fprintf(stderr, "Couldn't create xkb context\n");
363 core_kbd_device_id = xkb_x11_get_core_keyboard_device_id(conn);
364 if (core_kbd_device_id == -1) {
366 fprintf(stderr, "Couldn't find core keyboard device\n");
370 ret = init_kbd(&core_kbd, conn, first_xkb_event, core_kbd_device_id, ctx);
372 fprintf(stderr, "Couldn't initialize core keyboard device\n");
376 ret = create_capture_window(conn);
378 fprintf(stderr, "Couldn't create a capture window\n");
382 test_disable_stdin_echo();
383 ret = loop(conn, &core_kbd);
384 test_enable_stdin_echo();
387 deinit_kbd(&core_kbd);
389 xkb_context_unref(ctx);
391 xcb_disconnect(conn);
393 exit(ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);