1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
/*
* vim:ts=4:sw=4:expandtab
*
* i3 - an improved dynamic tiling window manager
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
*
*/
#include "libi3.h"
#include <stdint.h>
#include <stdlib.h>
#include <xcb/xcb.h>
#include <xcb/xcb_keysyms.h>
/*
* All-in-one function which returns the modifier mask (XCB_MOD_MASK_*) for the
* given keysymbol, for example for XCB_NUM_LOCK (usually configured to mod2).
*
* This function initiates one round-trip. Use get_mod_mask_for() directly if
* you already have the modifier mapping and key symbols.
*
*/
uint32_t aio_get_mod_mask_for(uint32_t keysym, xcb_key_symbols_t *symbols) {
xcb_get_modifier_mapping_cookie_t cookie;
xcb_get_modifier_mapping_reply_t *modmap_r;
xcb_flush(conn);
/* Get the current modifier mapping (this is blocking!) */
cookie = xcb_get_modifier_mapping(conn);
if (!(modmap_r = xcb_get_modifier_mapping_reply(conn, cookie, NULL))) {
return 0;
}
uint32_t result = get_mod_mask_for(keysym, symbols, modmap_r);
free(modmap_r);
return result;
}
/*
* Returns the modifier mask (XCB_MOD_MASK_*) for the given keysymbol, for
* example for XCB_NUM_LOCK (usually configured to mod2).
*
* This function does not initiate any round-trips.
*
*/
uint32_t get_mod_mask_for(uint32_t keysym,
xcb_key_symbols_t *symbols,
xcb_get_modifier_mapping_reply_t *modmap_reply) {
xcb_keycode_t *codes, *modmap;
xcb_keycode_t mod_code;
modmap = xcb_get_modifier_mapping_keycodes(modmap_reply);
/* Get the list of keycodes for the given symbol */
if (!(codes = xcb_key_symbols_get_keycode(symbols, keysym))) {
return 0;
}
/* Loop through all modifiers (Mod1-Mod5, Shift, Control, Lock) */
for (int mod = 0; mod < 8; mod++) {
for (int j = 0; j < modmap_reply->keycodes_per_modifier; j++) {
/* Store the current keycode (for modifier 'mod') */
mod_code = modmap[(mod * modmap_reply->keycodes_per_modifier) + j];
/* Check if that keycode is in the list of previously resolved
* keycodes for our symbol. If so, return the modifier mask. */
for (xcb_keycode_t *code = codes; *code; code++) {
if (*code != mod_code) {
continue;
}
free(codes);
/* This corresponds to the XCB_MOD_MASK_* constants */
return (1 << mod);
}
}
}
return 0;
}
|