]> git.sur5r.net Git - i3/i3/blob - src/bindings.c
Move keyboard binding accessor to bindings.[ch]
[i3/i3] / src / bindings.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2014 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * bindings.c: Functions for configuring, finding and, running bindings.
8  */
9 #include "all.h"
10
11 /*
12  * The name of the default mode.
13  *
14  */
15 const char *DEFAULT_BINDING_MODE = "default";
16
17 /*
18  * Returns the mode specified by `name` or creates a new mode and adds it to
19  * the list of modes.
20  *
21  */
22 static struct Mode *mode_from_name(const char *name) {
23     struct Mode *mode;
24
25     /* Try to find the mode in the list of modes and return it */
26     SLIST_FOREACH(mode, &modes, modes) {
27         if (strcmp(mode->name, name) == 0)
28             return mode;
29     }
30
31     /* If the mode was not found, create a new one */
32     mode = scalloc(sizeof(struct Mode));
33     mode->name = sstrdup(name);
34     mode->bindings = scalloc(sizeof(struct bindings_head));
35     TAILQ_INIT(mode->bindings);
36     SLIST_INSERT_HEAD(&modes, mode, modes);
37
38     return mode;
39 }
40
41 /*
42  * Adds a binding from config parameters given as strings and returns a
43  * pointer to the binding structure. Returns NULL if the input code could not
44  * be parsed.
45  *
46  */
47 Binding *configure_binding(const char *bindtype, const char *modifiers, const char *input_code,
48         const char *release, const char *command, const char *modename) {
49     Binding *new_binding = scalloc(sizeof(Binding));
50     DLOG("bindtype %s, modifiers %s, input code %s, release %s\n", bindtype, modifiers, input_code, release);
51     new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
52     if (strcmp(bindtype, "bindsym") == 0) {
53         new_binding->symbol = sstrdup(input_code);
54     } else {
55         // TODO: strtol with proper error handling
56         new_binding->keycode = atoi(input_code);
57         if (new_binding->keycode == 0) {
58             ELOG("Could not parse \"%s\" as an input code, ignoring this binding.\n", input_code);
59             FREE(new_binding);
60             return NULL;
61         }
62     }
63     new_binding->mods = modifiers_from_str(modifiers);
64     new_binding->command = sstrdup(command);
65     new_binding->input_type = B_KEYBOARD;
66
67     struct Mode *mode = mode_from_name(modename);
68     TAILQ_INSERT_TAIL(mode->bindings, new_binding, bindings);
69
70     return new_binding;
71 }
72
73 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
74     if (bind->input_type != B_KEYBOARD)
75         return;
76
77     DLOG("Grabbing %d with modifiers %d (with mod_mask_lock %d)\n", keycode, bind->mods, bind->mods | XCB_MOD_MASK_LOCK);
78     /* Grab the key in all combinations */
79     #define GRAB_KEY(modifier) \
80         do { \
81             xcb_grab_key(conn, 0, root, modifier, keycode, \
82                          XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
83         } while (0)
84     int mods = bind->mods;
85     if ((bind->mods & BIND_MODE_SWITCH) != 0) {
86         mods &= ~BIND_MODE_SWITCH;
87         if (mods == 0)
88             mods = XCB_MOD_MASK_ANY;
89     }
90     GRAB_KEY(mods);
91     GRAB_KEY(mods | xcb_numlock_mask);
92     GRAB_KEY(mods | XCB_MOD_MASK_LOCK);
93     GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
94 }
95
96
97 /*
98  * Grab the bound keys (tell X to send us keypress events for those keycodes)
99  *
100  */
101 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
102     Binding *bind;
103     TAILQ_FOREACH(bind, bindings, bindings) {
104         if (bind->input_type != B_KEYBOARD ||
105                 (bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
106                 (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
107             continue;
108
109         /* The easy case: the user specified a keycode directly. */
110         if (bind->keycode > 0) {
111             grab_keycode_for_binding(conn, bind, bind->keycode);
112             continue;
113         }
114
115         xcb_keycode_t *walk = bind->translated_to;
116         for (uint32_t i = 0; i < bind->number_keycodes; i++)
117             grab_keycode_for_binding(conn, bind, *walk++);
118     }
119 }
120
121 /*
122  * Returns a pointer to the keyboard Binding with the specified modifiers and
123  * keycode or NULL if no such binding exists.
124  *
125  */
126 Binding *get_keyboard_binding(uint16_t modifiers, bool key_release, xcb_keycode_t keycode) {
127     Binding *bind;
128
129     if (!key_release) {
130         /* On a KeyPress event, we first reset all
131          * B_UPON_KEYRELEASE_IGNORE_MODS bindings back to B_UPON_KEYRELEASE */
132         TAILQ_FOREACH(bind, bindings, bindings) {
133             if (bind->input_type != B_KEYBOARD)
134                 continue;
135             if (bind->release == B_UPON_KEYRELEASE_IGNORE_MODS)
136                 bind->release = B_UPON_KEYRELEASE;
137         }
138     }
139
140     TAILQ_FOREACH(bind, bindings, bindings) {
141         /* First compare the modifiers (unless this is a
142          * B_UPON_KEYRELEASE_IGNORE_MODS binding and this is a KeyRelease
143          * event) */
144         if (bind->input_type != B_KEYBOARD)
145             continue;
146         if (bind->mods != modifiers &&
147             (bind->release != B_UPON_KEYRELEASE_IGNORE_MODS ||
148              !key_release))
149             continue;
150
151         /* If a symbol was specified by the user, we need to look in
152          * the array of translated keycodes for the event’s keycode */
153         if (bind->symbol != NULL) {
154             if (memmem(bind->translated_to,
155                        bind->number_keycodes * sizeof(xcb_keycode_t),
156                        &keycode, sizeof(xcb_keycode_t)) == NULL)
157                 continue;
158         } else {
159             /* This case is easier: The user specified a keycode */
160             if (bind->keycode != keycode)
161                 continue;
162         }
163
164         /* If this keybinding is a KeyRelease binding, it matches the key which
165          * the user pressed. We therefore mark it as
166          * B_UPON_KEYRELEASE_IGNORE_MODS for later, so that the user can
167          * release the modifiers before the actual key and the KeyRelease will
168          * still be matched. */
169         if (bind->release == B_UPON_KEYRELEASE && !key_release)
170             bind->release = B_UPON_KEYRELEASE_IGNORE_MODS;
171
172         /* Check if the binding is for a KeyPress or a KeyRelease event */
173         if ((bind->release == B_UPON_KEYPRESS && key_release) ||
174             (bind->release >= B_UPON_KEYRELEASE && !key_release))
175             continue;
176
177         break;
178     }
179
180     return (bind == TAILQ_END(bindings) ? NULL : bind);
181 }