2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2014 Michael Stapelberg and contributors (see also: LICENSE)
7 * bindings.c: Functions for configuring, finding and, running bindings.
12 * The name of the default mode.
15 const char *DEFAULT_BINDING_MODE = "default";
18 * Returns the mode specified by `name` or creates a new mode and adds it to
22 static struct Mode *mode_from_name(const char *name) {
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)
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);
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
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);
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);
63 new_binding->mods = modifiers_from_str(modifiers);
64 new_binding->command = sstrdup(command);
65 new_binding->input_type = B_KEYBOARD;
67 struct Mode *mode = mode_from_name(modename);
68 TAILQ_INSERT_TAIL(mode->bindings, new_binding, bindings);