]> git.sur5r.net Git - i3/i3/blob - src/bindings.c
Abstract binding configuration 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 }