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.
26 #include "xkbcommon/xkbcommon-x11.h"
32 * Note: This program only handles the core keyboard device for now.
33 * It should be straigtforward to change struct keyboard to a list of
34 * keyboards with device IDs, as in test/interactive-evdev.c. This would
37 * - Initially listing the keyboard devices.
38 * - Listening to device changes.
39 * - Matching events to their devices.
41 * XKB itself knows about xinput1 devices, and most requests and events are
44 * In order to list the devices and react to changes, you need xinput1/2.
45 * You also need xinput for the key press/release event, since the core
46 * protocol key press event does not carry a device ID to match on.
50 xcb_connection_t *conn;
51 uint8_t first_xkb_event;
52 struct xkb_context *ctx;
54 struct xkb_keymap *keymap;
55 struct xkb_state *state;
59 static bool terminate;
62 select_xkb_events_for_device(xcb_connection_t *conn, int32_t device_id)
66 (XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY |
67 XCB_XKB_EVENT_TYPE_MAP_NOTIFY |
68 XCB_XKB_EVENT_TYPE_STATE_NOTIFY),
70 required_nkn_details =
71 (XCB_XKB_NKN_DETAIL_KEYCODES),
74 (XCB_XKB_MAP_PART_KEY_TYPES |
75 XCB_XKB_MAP_PART_KEY_SYMS |
76 XCB_XKB_MAP_PART_MODIFIER_MAP |
77 XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS |
78 XCB_XKB_MAP_PART_KEY_ACTIONS |
79 XCB_XKB_MAP_PART_VIRTUAL_MODS |
80 XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP),
82 required_state_details =
83 (XCB_XKB_STATE_PART_MODIFIER_BASE |
84 XCB_XKB_STATE_PART_MODIFIER_LATCH |
85 XCB_XKB_STATE_PART_MODIFIER_LOCK |
86 XCB_XKB_STATE_PART_GROUP_BASE |
87 XCB_XKB_STATE_PART_GROUP_LATCH |
88 XCB_XKB_STATE_PART_GROUP_LOCK),
91 static const xcb_xkb_select_events_details_t details = {
92 .affectNewKeyboard = required_nkn_details,
93 .newKeyboardDetails = required_nkn_details,
94 .affectState = required_state_details,
95 .stateDetails = required_state_details,
98 xcb_void_cookie_t cookie =
99 xcb_xkb_select_events_aux_checked(conn,
101 required_events, /* affectWhich */
104 required_map_parts, /* affectMap */
105 required_map_parts, /* map */
106 &details); /* details */
108 xcb_generic_error_t *error = xcb_request_check(conn, cookie);
118 update_keymap(struct keyboard *kbd)
120 struct xkb_keymap *new_keymap;
121 struct xkb_state *new_state;
123 new_keymap = xkb_x11_keymap_new_from_device(kbd->ctx, kbd->conn,
128 new_state = xkb_x11_state_new_from_device(new_keymap, kbd->conn,
134 printf("Keymap updated!\n");
136 xkb_state_unref(kbd->state);
137 xkb_keymap_unref(kbd->keymap);
138 kbd->keymap = new_keymap;
139 kbd->state = new_state;
143 xkb_keymap_unref(new_keymap);
149 init_kbd(struct keyboard *kbd, xcb_connection_t *conn, uint8_t first_xkb_event,
150 int32_t device_id, struct xkb_context *ctx)
155 kbd->first_xkb_event = first_xkb_event;
159 kbd->device_id = device_id;
161 ret = update_keymap(kbd);
165 ret = select_xkb_events_for_device(conn, device_id);
172 xkb_state_unref(kbd->state);
173 xkb_keymap_unref(kbd->keymap);
179 deinit_kbd(struct keyboard *kbd)
181 xkb_state_unref(kbd->state);
182 xkb_keymap_unref(kbd->keymap);
186 process_xkb_event(xcb_generic_event_t *gevent, struct keyboard *kbd)
190 uint8_t response_type;
193 xcb_timestamp_t time;
196 xcb_xkb_new_keyboard_notify_event_t new_keyboard_notify;
197 xcb_xkb_map_notify_event_t map_notify;
198 xcb_xkb_state_notify_event_t state_notify;
199 } *event = (union xkb_event *) gevent;
201 if (event->any.deviceID != kbd->device_id)
205 * XkbNewKkdNotify and XkbMapNotify together capture all sorts of keymap
206 * updates (e.g. xmodmap, xkbcomp, setxkbmap), with minimal redundent
209 switch (event->any.xkbType) {
210 case XCB_XKB_NEW_KEYBOARD_NOTIFY:
211 if (event->new_keyboard_notify.changed & XCB_XKB_NKN_DETAIL_KEYCODES)
215 case XCB_XKB_MAP_NOTIFY:
219 case XCB_XKB_STATE_NOTIFY:
220 xkb_state_update_mask(kbd->state,
221 event->state_notify.baseMods,
222 event->state_notify.latchedMods,
223 event->state_notify.lockedMods,
224 event->state_notify.baseGroup,
225 event->state_notify.latchedGroup,
226 event->state_notify.lockedGroup);
232 process_event(xcb_generic_event_t *gevent, struct keyboard *kbd)
234 switch (gevent->response_type) {
235 case XCB_KEY_PRESS: {
236 xcb_key_press_event_t *event = (xcb_key_press_event_t *) gevent;
237 xkb_keycode_t keycode = event->detail;
239 test_print_keycode_state(kbd->state, NULL, keycode,
240 XKB_CONSUMED_MODE_XKB);
248 if (gevent->response_type == kbd->first_xkb_event)
249 process_xkb_event(gevent, kbd);
255 loop(xcb_connection_t *conn, struct keyboard *kbd)
258 xcb_generic_event_t *event;
260 switch (xcb_connection_has_error(conn)) {
265 "Closed connection to X server: connection error\n");
267 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
269 "Closed connection to X server: extension not supported\n");
273 "Closed connection to X server: error code %d\n",
274 xcb_connection_has_error(conn));
278 event = xcb_wait_for_event(conn);
283 process_event(event, kbd);
291 create_capture_window(xcb_connection_t *conn)
293 xcb_generic_error_t *error;
294 xcb_void_cookie_t cookie;
295 xcb_screen_t *screen =
296 xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
297 xcb_window_t window = xcb_generate_id(conn);
298 uint32_t values[2] = {
300 XCB_EVENT_MASK_KEY_PRESS,
303 cookie = xcb_create_window_checked(conn, XCB_COPY_FROM_PARENT,
304 window, screen->root,
306 XCB_WINDOW_CLASS_INPUT_OUTPUT,
308 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
310 if ((error = xcb_request_check(conn, cookie)) != NULL) {
315 cookie = xcb_map_window_checked(conn, window);
316 if ((error = xcb_request_check(conn, cookie)) != NULL) {
325 main(int argc, char *argv[])
328 xcb_connection_t *conn;
329 uint8_t first_xkb_event;
330 int32_t core_kbd_device_id;
331 struct xkb_context *ctx;
332 struct keyboard core_kbd;
334 setlocale(LC_ALL, "");
336 conn = xcb_connect(NULL, NULL);
337 if (!conn || xcb_connection_has_error(conn)) {
338 fprintf(stderr, "Couldn't connect to X server: error code %d\n",
339 conn ? xcb_connection_has_error(conn) : -1);
344 ret = xkb_x11_setup_xkb_extension(conn,
345 XKB_X11_MIN_MAJOR_XKB_VERSION,
346 XKB_X11_MIN_MINOR_XKB_VERSION,
347 XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
348 NULL, NULL, &first_xkb_event, NULL);
350 fprintf(stderr, "Couldn't setup XKB extension\n");
354 ctx = test_get_context(0);
357 fprintf(stderr, "Couldn't create xkb context\n");
361 core_kbd_device_id = xkb_x11_get_core_keyboard_device_id(conn);
362 if (core_kbd_device_id == -1) {
364 fprintf(stderr, "Couldn't find core keyboard device\n");
368 ret = init_kbd(&core_kbd, conn, first_xkb_event, core_kbd_device_id, ctx);
370 fprintf(stderr, "Couldn't initialize core keyboard device\n");
374 ret = create_capture_window(conn);
376 fprintf(stderr, "Couldn't create a capture window\n");
380 test_disable_stdin_echo();
381 ret = loop(conn, &core_kbd);
382 test_enable_stdin_echo();
385 deinit_kbd(&core_kbd);
387 xkb_context_unref(ctx);
389 xcb_disconnect(conn);
391 exit(ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);