i3 - improved tiling WM


Abstract binding configuration to bindings.[ch]

Patch status: merged

Patch by Tony Crisci

Long description:

Create files bindings.[ch] to contain functions for configuring,
finding, and running bindings.

Use the new function `configure_binding` for binding configuration. This
function adds a binding from config parameters.

Export the function `modifiers_from_str` from config_directives.h.

This change is made in preparation for the new bindmouse functionality.

To apply this patch, use:
curl http://cr.i3wm.org/patch/389/raw.patch | git am

b/include/all.h

28
@@ -79,6 +79,7 @@
29
 #include "scratchpad.h"
30
 #include "commands.h"
31
 #include "commands_parser.h"
32
+#include "bindings.h"
33
 #include "config_directives.h"
34
 #include "config_parser.h"
35
 #include "fake_outputs.h"

b/include/bindings.h

41
@@ -0,0 +1,25 @@
42
+/*
43
+ * vim:ts=4:sw=4:expandtab
44
+ *
45
+ * i3 - an improved dynamic tiling window manager
46
+ * © 2009-2014 Michael Stapelberg and contributors (see also: LICENSE)
47
+ *
48
+ * bindings.h: Functions for configuring, finding, and running bindings.
49
+ *
50
+ */
51
+#pragma once
52
+
53
+/**
54
+ * The name of the default mode.
55
+ *
56
+ */
57
+const char *DEFAULT_BINDING_MODE;
58
+
59
+/**
60
+ * Adds a binding from config parameters given as strings and returns a
61
+ * pointer to the binding structure. Returns NULL if the input code could not
62
+ * be parsed.
63
+ *
64
+ */
65
+Binding *configure_binding(const char *bindtype, const char *modifiers, const char *input_code,
66
+        const char *release, const char *command, const char *mode);

b/include/config_directives.h

71
@@ -11,6 +11,12 @@
72
 
73
 #include "config_parser.h"
74
 
75
+/**
76
+ * A utility function to convert a string of modifiers to the corresponding bit
77
+ * mask.
78
+ */
79
+uint32_t modifiers_from_str(const char *str);
80
+
81
 /** The beginning of the prototype for every cfg_ function. */
82
 #define I3_CFG Match *current_match, struct ConfigResult *result
83
 

b/src/bindings.c

89
@@ -0,0 +1,71 @@
90
+/*
91
+ * vim:ts=4:sw=4:expandtab
92
+ *
93
+ * i3 - an improved dynamic tiling window manager
94
+ * © 2009-2014 Michael Stapelberg and contributors (see also: LICENSE)
95
+ *
96
+ * bindings.c: Functions for configuring, finding and, running bindings.
97
+ */
98
+#include "all.h"
99
+
100
+/*
101
+ * The name of the default mode.
102
+ *
103
+ */
104
+const char *DEFAULT_BINDING_MODE = "default";
105
+
106
+/*
107
+ * Returns the mode specified by `name` or creates a new mode and adds it to
108
+ * the list of modes.
109
+ *
110
+ */
111
+static struct Mode *mode_from_name(const char *name) {
112
+    struct Mode *mode;
113
+
114
+    /* Try to find the mode in the list of modes and return it */
115
+    SLIST_FOREACH(mode, &modes, modes) {
116
+        if (strcmp(mode->name, name) == 0)
117
+            return mode;
118
+    }
119
+
120
+    /* If the mode was not found, create a new one */
121
+    mode = scalloc(sizeof(struct Mode));
122
+    mode->name = sstrdup(name);
123
+    mode->bindings = scalloc(sizeof(struct bindings_head));
124
+    TAILQ_INIT(mode->bindings);
125
+    SLIST_INSERT_HEAD(&modes, mode, modes);
126
+
127
+    return mode;
128
+}
129
+
130
+/*
131
+ * Adds a binding from config parameters given as strings and returns a
132
+ * pointer to the binding structure. Returns NULL if the input code could not
133
+ * be parsed.
134
+ *
135
+ */
136
+Binding *configure_binding(const char *bindtype, const char *modifiers, const char *input_code,
137
+        const char *release, const char *command, const char *modename) {
138
+    Binding *new_binding = scalloc(sizeof(Binding));
139
+    DLOG("bindtype %s, modifiers %s, input code %s, release %s\n", bindtype, modifiers, input_code, release);
140
+    new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
141
+    if (strcmp(bindtype, "bindsym") == 0) {
142
+        new_binding->symbol = sstrdup(input_code);
143
+    } else {
144
+        // TODO: strtol with proper error handling
145
+        new_binding->keycode = atoi(input_code);
146
+        if (new_binding->keycode == 0) {
147
+            ELOG("Could not parse \"%s\" as an input code, ignoring this binding.\n", input_code);
148
+            FREE(new_binding);
149
+            return NULL;
150
+        }
151
+    }
152
+    new_binding->mods = modifiers_from_str(modifiers);
153
+    new_binding->command = sstrdup(command);
154
+    new_binding->input_type = B_KEYBOARD;
155
+
156
+    struct Mode *mode = mode_from_name(modename);
157
+    TAILQ_INSERT_TAIL(mode->bindings, new_binding, bindings);
158
+
159
+    return new_binding;
160
+}

b/src/config_directives.c

165
@@ -130,7 +130,11 @@ static bool eval_boolstr(const char *str) {
166
             strcasecmp(str, "active") == 0);
167
 }
168
 
169
-static uint32_t modifiers_from_str(const char *str) {
170
+/*
171
+ * A utility function to convert a string of modifiers to the corresponding bit
172
+ * mask.
173
+ */
174
+uint32_t modifiers_from_str(const char *str) {
175
     /* It might be better to use strtok() here, but the simpler strstr() should
176
      * do for now. */
177
     uint32_t result = 0;
178
@@ -167,24 +171,8 @@ CFGFUN(font, const char *font) {
179
 	font_pattern = sstrdup(font);
180
 }
181
 
182
-// TODO: refactor with mode_binding
183
 CFGFUN(binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
184
-    Binding *new_binding = scalloc(sizeof(Binding));
185
-    DLOG("bindtype %s, modifiers %s, key %s, release %s\n", bindtype, modifiers, key, release);
186
-    new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
187
-    if (strcmp(bindtype, "bindsym") == 0) {
188
-        new_binding->symbol = sstrdup(key);
189
-    } else {
190
-        // TODO: strtol with proper error handling
191
-        new_binding->keycode = atoi(key);
192
-        if (new_binding->keycode == 0) {
193
-            ELOG("Could not parse \"%s\" as a keycode, ignoring this binding.\n", key);
194
-            return;
195
-        }
196
-    }
197
-    new_binding->mods = modifiers_from_str(modifiers);
198
-    new_binding->command = sstrdup(command);
199
-    TAILQ_INSERT_TAIL(bindings, new_binding, bindings);
200
+    configure_binding(bindtype, modifiers, key, release, command, DEFAULT_BINDING_MODE);
201
 }
202
 
203
 
204
@@ -192,39 +180,20 @@ CFGFUN(binding, const char *bindtype, const char *modifiers, const char *key, co
205
  * Mode handling
206
  ******************************************************************************/
207
 
208
-static struct bindings_head *current_bindings;
209
+static char *current_mode;
210
 
211
 CFGFUN(mode_binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
212
-    Binding *new_binding = scalloc(sizeof(Binding));
213
-    DLOG("bindtype %s, modifiers %s, key %s, release %s\n", bindtype, modifiers, key, release);
214
-    new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
215
-    if (strcmp(bindtype, "bindsym") == 0) {
216
-        new_binding->symbol = sstrdup(key);
217
-    } else {
218
-        // TODO: strtol with proper error handling
219
-        new_binding->keycode = atoi(key);
220
-        if (new_binding->keycode == 0) {
221
-            ELOG("Could not parse \"%s\" as a keycode, ignoring this binding.\n", key);
222
-            return;
223
-        }
224
-    }
225
-    new_binding->mods = modifiers_from_str(modifiers);
226
-    new_binding->command = sstrdup(command);
227
-    TAILQ_INSERT_TAIL(current_bindings, new_binding, bindings);
228
+    configure_binding(bindtype, modifiers, key, release, command, current_mode);
229
 }
230
 
231
 CFGFUN(enter_mode, const char *modename) {
232
-    if (strcasecmp(modename, "default") == 0) {
233
-        ELOG("You cannot use the name \"default\" for your mode\n");
234
+    if (strcasecmp(modename, DEFAULT_BINDING_MODE) == 0) {
235
+        ELOG("You cannot use the name %s for your mode\n", DEFAULT_BINDING_MODE);
236
         exit(1);
237
     }
238
     DLOG("\t now in mode %s\n", modename);
239
-    struct Mode *mode = scalloc(sizeof(struct Mode));
240
-    mode->name = sstrdup(modename);
241
-    mode->bindings = scalloc(sizeof(struct bindings_head));
242
-    TAILQ_INIT(mode->bindings);
243
-    current_bindings = mode->bindings;
244
-    SLIST_INSERT_HEAD(&modes, mode, modes);
245
+    FREE(current_mode);
246
+    current_mode = sstrdup(modename);
247
 }
248
 
249
 CFGFUN(exec, const char *exectype, const char *no_startup_id, const char *command) {