]> git.sur5r.net Git - i3/i3/blob - libi3/get_mod_mask.c
ac71e6b23075fccbb62acb8c3c6918c417bb819b
[i3/i3] / libi3 / get_mod_mask.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  */
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <xcb/xcb.h>
11 #include <xcb/xcb_keysyms.h>
12
13 #include "libi3.h"
14
15 /*
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).
18  *
19  * This function initiates one round-trip. Use get_mod_mask_for() directly if
20  * you already have the modifier mapping and key symbols.
21  *
22  */
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;
26
27     xcb_flush(conn);
28
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)))
32         return 0;
33
34     uint32_t result = get_mod_mask_for(keysym, symbols, modmap_r);
35     free(modmap_r);
36     return result;
37 }
38
39 /*
40  * Returns the modifier mask (XCB_MOD_MASK_*) for the given keysymbol, for
41  * example for XCB_NUM_LOCK (usually configured to mod2).
42  *
43  * This function does not initiate any round-trips.
44  *
45  */
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;
51
52     modmap = xcb_get_modifier_mapping_keycodes(modmap_reply);
53
54     /* Get the list of keycodes for the given symbol */
55     if (!(codes = xcb_key_symbols_get_keycode(symbols, keysym)))
56         return 0;
57
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)
67                     continue;
68
69                 free(codes);
70                 /* This corresponds to the XCB_MOD_MASK_* constants */
71                 return (1 << mod);
72             }
73         }
74
75     return 0;
76 }