2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2011 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
14 #include <xcb/xcb_keysyms.h>
18 extern xcb_connection_t *conn;
21 * All-in-one function which returns the modifier mask (XCB_MOD_MASK_*) for the
22 * given keysymbol, for example for XCB_NUM_LOCK (usually configured to mod2).
24 * This function initiates one round-trip. Use get_mod_mask_for() directly if
25 * you already have the modifier mapping and key symbols.
28 uint32_t aio_get_mod_mask_for(uint32_t keysym, xcb_key_symbols_t *symbols) {
29 xcb_get_modifier_mapping_cookie_t cookie;
30 xcb_get_modifier_mapping_reply_t *modmap_r;
34 /* Get the current modifier mapping (this is blocking!) */
35 cookie = xcb_get_modifier_mapping(conn);
36 if (!(modmap_r = xcb_get_modifier_mapping_reply(conn, cookie, NULL)))
39 uint32_t result = get_mod_mask_for(keysym, symbols, modmap_r);
45 * Returns the modifier mask (XCB_MOD_MASK_*) for the given keysymbol, for
46 * example for XCB_NUM_LOCK (usually configured to mod2).
48 * This function does not initiate any round-trips.
51 uint32_t get_mod_mask_for(uint32_t keysym,
52 xcb_key_symbols_t *symbols,
53 xcb_get_modifier_mapping_reply_t *modmap_reply) {
54 xcb_keycode_t *codes, *modmap;
55 xcb_keycode_t mod_code;
57 modmap = xcb_get_modifier_mapping_keycodes(modmap_reply);
59 /* Get the list of keycodes for the given symbol */
60 if (!(codes = xcb_key_symbols_get_keycode(symbols, keysym)))
63 /* Loop through all modifiers (Mod1-Mod5, Shift, Control, Lock) */
64 for (int mod = 0; mod < 8; mod++)
65 for (int j = 0; j < modmap_reply->keycodes_per_modifier; j++) {
66 /* Store the current keycode (for modifier 'mod') */
67 mod_code = modmap[(mod * modmap_reply->keycodes_per_modifier) + j];
68 /* Check if that keycode is in the list of previously resolved
69 * keycodes for our symbol. If so, return the modifier mask. */
70 for (xcb_keycode_t *code = codes; *code; code++) {
71 if (*code != mod_code)
75 /* This corresponds to the XCB_MOD_MASK_* constants */