2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
11 #include <xcb/xcb_keysyms.h>
16 * All-in-one function which returns the modifier mask (XCB_MOD_MASK_*) for the
17 * given keysymbol, for example for XCB_NUM_LOCK (usually configured to mod2).
19 * This function initiates one round-trip. Use get_mod_mask_for() directly if
20 * you already have the modifier mapping and key symbols.
23 uint32_t aio_get_mod_mask_for(uint32_t keysym, xcb_key_symbols_t *symbols) {
24 xcb_get_modifier_mapping_cookie_t cookie;
25 xcb_get_modifier_mapping_reply_t *modmap_r;
29 /* Get the current modifier mapping (this is blocking!) */
30 cookie = xcb_get_modifier_mapping(conn);
31 if (!(modmap_r = xcb_get_modifier_mapping_reply(conn, cookie, NULL)))
34 uint32_t result = get_mod_mask_for(keysym, symbols, modmap_r);
40 * Returns the modifier mask (XCB_MOD_MASK_*) for the given keysymbol, for
41 * example for XCB_NUM_LOCK (usually configured to mod2).
43 * This function does not initiate any round-trips.
46 uint32_t get_mod_mask_for(uint32_t keysym,
47 xcb_key_symbols_t *symbols,
48 xcb_get_modifier_mapping_reply_t *modmap_reply) {
49 xcb_keycode_t *codes, *modmap;
50 xcb_keycode_t mod_code;
52 modmap = xcb_get_modifier_mapping_keycodes(modmap_reply);
54 /* Get the list of keycodes for the given symbol */
55 if (!(codes = xcb_key_symbols_get_keycode(symbols, keysym)))
58 /* Loop through all modifiers (Mod1-Mod5, Shift, Control, Lock) */
59 for (int mod = 0; mod < 8; mod++)
60 for (int j = 0; j < modmap_reply->keycodes_per_modifier; j++) {
61 /* Store the current keycode (for modifier 'mod') */
62 mod_code = modmap[(mod * modmap_reply->keycodes_per_modifier) + j];
63 /* Check if that keycode is in the list of previously resolved
64 * keycodes for our symbol. If so, return the modifier mask. */
65 for (xcb_keycode_t *code = codes; *code; code++) {
66 if (*code != mod_code)
70 /* This corresponds to the XCB_MOD_MASK_* constants */