]> git.sur5r.net Git - i3/i3/blob - src/config_directives.c
error out instead of accepting invalid key bindings (Thanks SardemFF7)
[i3/i3] / src / config_directives.c
1 #undef I3__FILE__
2 #define I3__FILE__ "config_directives.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * config_directives.c: all config storing functions (see config_parser.c)
10  *
11  */
12 #include <float.h>
13 #include <stdarg.h>
14
15 #include "all.h"
16
17 // Macros to make the YAJL API a bit easier to use.
18 #define y(x, ...) yajl_gen_ ## x (cmd_output->json_gen, ##__VA_ARGS__)
19 #define ystr(str) yajl_gen_string(cmd_output->json_gen, (unsigned char*)str, strlen(str))
20 #define ysuccess(success) do { \
21     y(map_open); \
22     ystr("success"); \
23     y(bool, success); \
24     y(map_close); \
25 } while (0)
26
27 /*******************************************************************************
28  * Criteria functions.
29  ******************************************************************************/
30
31 static int criteria_next_state;
32
33 /*
34  * Initializes the specified 'Match' data structure and the initial state of
35  * commands.c for matching target windows of a command.
36  *
37  */
38 CFGFUN(criteria_init, int _state) {
39     criteria_next_state = _state;
40
41     DLOG("Initializing criteria, current_match = %p, state = %d\n", current_match, _state);
42     match_init(current_match);
43 }
44
45 CFGFUN(criteria_pop_state) {
46     result->next_state = criteria_next_state;
47 }
48
49 /*
50  * Interprets a ctype=cvalue pair and adds it to the current match
51  * specification.
52  *
53  */
54 CFGFUN(criteria_add, const char *ctype, const char *cvalue) {
55     DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
56
57     if (strcmp(ctype, "class") == 0) {
58         current_match->class = regex_new(cvalue);
59         return;
60     }
61
62     if (strcmp(ctype, "instance") == 0) {
63         current_match->instance = regex_new(cvalue);
64         return;
65     }
66
67     if (strcmp(ctype, "window_role") == 0) {
68         current_match->role = regex_new(cvalue);
69         return;
70     }
71
72     if (strcmp(ctype, "con_id") == 0) {
73         char *end;
74         long parsed = strtol(cvalue, &end, 10);
75         if (parsed == LONG_MIN ||
76             parsed == LONG_MAX ||
77             parsed < 0 ||
78             (end && *end != '\0')) {
79             ELOG("Could not parse con id \"%s\"\n", cvalue);
80         } else {
81             current_match->con_id = (Con*)parsed;
82             printf("id as int = %p\n", current_match->con_id);
83         }
84         return;
85     }
86
87     if (strcmp(ctype, "id") == 0) {
88         char *end;
89         long parsed = strtol(cvalue, &end, 10);
90         if (parsed == LONG_MIN ||
91             parsed == LONG_MAX ||
92             parsed < 0 ||
93             (end && *end != '\0')) {
94             ELOG("Could not parse window id \"%s\"\n", cvalue);
95         } else {
96             current_match->id = parsed;
97             printf("window id as int = %d\n", current_match->id);
98         }
99         return;
100     }
101
102     if (strcmp(ctype, "con_mark") == 0) {
103         current_match->mark = regex_new(cvalue);
104         return;
105     }
106
107     if (strcmp(ctype, "title") == 0) {
108         current_match->title = regex_new(cvalue);
109         return;
110     }
111
112     if (strcmp(ctype, "urgent") == 0) {
113         if (strcasecmp(cvalue, "latest") == 0 ||
114             strcasecmp(cvalue, "newest") == 0 ||
115             strcasecmp(cvalue, "recent") == 0 ||
116             strcasecmp(cvalue, "last") == 0) {
117             current_match->urgent = U_LATEST;
118         } else if (strcasecmp(cvalue, "oldest") == 0 ||
119                    strcasecmp(cvalue, "first") == 0) {
120             current_match->urgent = U_OLDEST;
121         }
122         return;
123     }
124
125     ELOG("Unknown criterion: %s\n", ctype);
126 }
127
128 /* TODO: refactor the above criteria code into a single file (with src/commands.c). */
129
130 /*******************************************************************************
131  * Utility functions
132  ******************************************************************************/
133
134 static bool eval_boolstr(const char *str) {
135     return (strcasecmp(str, "1") == 0 ||
136             strcasecmp(str, "yes") == 0 ||
137             strcasecmp(str, "true") == 0 ||
138             strcasecmp(str, "on") == 0 ||
139             strcasecmp(str, "enable") == 0 ||
140             strcasecmp(str, "active") == 0);
141 }
142
143 static uint32_t modifiers_from_str(const char *str) {
144     /* It might be better to use strtok() here, but the simpler strstr() should
145      * do for now. */
146     uint32_t result = 0;
147     if (str == NULL)
148         return result;
149     if (strstr(str, "Mod1") != NULL)
150         result |= BIND_MOD1;
151     if (strstr(str, "Mod2") != NULL)
152         result |= BIND_MOD2;
153     if (strstr(str, "Mod3") != NULL)
154         result |= BIND_MOD3;
155     if (strstr(str, "Mod4") != NULL)
156         result |= BIND_MOD4;
157     if (strstr(str, "Mod5") != NULL)
158         result |= BIND_MOD5;
159     if (strstr(str, "Control") != NULL)
160         result |= BIND_CONTROL;
161     if (strstr(str, "Shift") != NULL)
162         result |= BIND_SHIFT;
163     if (strstr(str, "Mode_switch") != NULL)
164         result |= BIND_MODE_SWITCH;
165     return result;
166 }
167
168 static char *font_pattern;
169
170 CFGFUN(font, const char *font) {
171         config.font = load_font(font, true);
172         set_font(&config.font);
173
174         /* Save the font pattern for using it as bar font later on */
175         FREE(font_pattern);
176         font_pattern = sstrdup(font);
177 }
178
179 // TODO: refactor with mode_binding
180 CFGFUN(binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
181     Binding *new_binding = scalloc(sizeof(Binding));
182     DLOG("bindtype %s, modifiers %s, key %s, release %s\n", bindtype, modifiers, key, release);
183     new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
184     if (strcmp(bindtype, "bindsym") == 0) {
185         new_binding->symbol = sstrdup(key);
186     } else {
187         // TODO: strtol with proper error handling
188         new_binding->keycode = atoi(key);
189         if (new_binding->keycode == 0) {
190             ELOG("Could not parse \"%s\" as a keycode, ignoring this binding.\n", key);
191             return;
192         }
193     }
194     new_binding->mods = modifiers_from_str(modifiers);
195     new_binding->command = sstrdup(command);
196     TAILQ_INSERT_TAIL(bindings, new_binding, bindings);
197 }
198
199
200 /*******************************************************************************
201  * Mode handling
202  ******************************************************************************/
203
204 static struct bindings_head *current_bindings;
205
206 CFGFUN(mode_binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
207     Binding *new_binding = scalloc(sizeof(Binding));
208     DLOG("bindtype %s, modifiers %s, key %s, release %s\n", bindtype, modifiers, key, release);
209     new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
210     if (strcmp(bindtype, "bindsym") == 0) {
211         new_binding->symbol = sstrdup(key);
212     } else {
213         // TODO: strtol with proper error handling
214         new_binding->keycode = atoi(key);
215         if (new_binding->keycode == 0) {
216             ELOG("Could not parse \"%s\" as a keycode, ignoring this binding.\n", key);
217             return;
218         }
219     }
220     new_binding->mods = modifiers_from_str(modifiers);
221     new_binding->command = sstrdup(command);
222     TAILQ_INSERT_TAIL(current_bindings, new_binding, bindings);
223 }
224
225 CFGFUN(enter_mode, const char *modename) {
226     if (strcasecmp(modename, "default") == 0) {
227         ELOG("You cannot use the name \"default\" for your mode\n");
228         exit(1);
229     }
230     DLOG("\t now in mode %s\n", modename);
231     struct Mode *mode = scalloc(sizeof(struct Mode));
232     mode->name = sstrdup(modename);
233     mode->bindings = scalloc(sizeof(struct bindings_head));
234     TAILQ_INIT(mode->bindings);
235     current_bindings = mode->bindings;
236     SLIST_INSERT_HEAD(&modes, mode, modes);
237 }
238
239 CFGFUN(exec, const char *exectype, const char *no_startup_id, const char *command) {
240         struct Autostart *new = smalloc(sizeof(struct Autostart));
241         new->command = sstrdup(command);
242         new->no_startup_id = (no_startup_id != NULL);
243         if (strcmp(exectype, "exec") == 0) {
244                 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
245         } else {
246                 TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
247         }
248 }
249
250 CFGFUN(for_window, const char *command) {
251     if (match_is_empty(current_match)) {
252         ELOG("Match is empty, ignoring this for_window statement\n");
253         return;
254     }
255     DLOG("\t should execute command %s for the criteria mentioned above\n", command);
256     Assignment *assignment = scalloc(sizeof(Assignment));
257     assignment->type = A_COMMAND;
258     match_copy(&(assignment->match), current_match);
259     assignment->dest.command = sstrdup(command);
260     TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
261 }
262
263 CFGFUN(floating_minimum_size, const long width, const long height) {
264     config.floating_minimum_width = width;
265     config.floating_minimum_height = height;
266 }
267
268 CFGFUN(floating_maximum_size, const long width, const long height) {
269     config.floating_maximum_width = width;
270     config.floating_maximum_height = height;
271 }
272
273 CFGFUN(floating_modifier, const char *modifiers) {
274     config.floating_modifier = modifiers_from_str(modifiers);
275 }
276
277 CFGFUN(default_orientation, const char *orientation) {
278     if (strcmp(orientation, "horizontal") == 0)
279         config.default_orientation = HORIZ;
280     else if (strcmp(orientation, "vertical") == 0)
281         config.default_orientation = VERT;
282     else config.default_orientation = NO_ORIENTATION;
283 }
284
285 CFGFUN(workspace_layout, const char *layout) {
286     if (strcmp(layout, "default") == 0)
287         config.default_layout = L_DEFAULT;
288     else if (strcmp(layout, "stacking") == 0 ||
289              strcmp(layout, "stacked") == 0)
290         config.default_layout = L_STACKED;
291     else config.default_layout = L_TABBED;
292 }
293
294 CFGFUN(new_window, const char *windowtype, const char *border, const long width) {
295     // FIXME: when using new_float *and* new_window with different border
296     // types, this breaks because default_border_width gets overwritten.
297
298     int border_style;
299     int border_width;
300
301     if (strcmp(border, "1pixel") == 0) {
302         border_style = BS_PIXEL;
303         border_width = 1;
304     } else if (strcmp(border, "none") == 0) {
305         border_style = BS_NONE;
306         border_width = 0;
307     } else if (strcmp(border, "pixel") == 0) {
308         border_style = BS_PIXEL;
309         border_width = width;
310     } else {
311         border_style = BS_NORMAL;
312         border_width = width;
313     }
314
315     if (strcmp(windowtype, "new_window") == 0) {
316         config.default_border = border_style;
317     } else {
318         config.default_floating_border = border_style;
319     }
320 }
321
322 CFGFUN(hide_edge_borders, const char *borders) {
323     if (strcmp(borders, "vertical") == 0)
324         config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
325     else if (strcmp(borders, "horizontal") == 0)
326         config.hide_edge_borders = ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
327     else if (strcmp(borders, "both") == 0)
328         config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE | ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
329     else if (strcmp(borders, "none") == 0)
330         config.hide_edge_borders = ADJ_NONE;
331     else if (eval_boolstr(borders))
332         config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
333     else config.hide_edge_borders = ADJ_NONE;
334 }
335
336 CFGFUN(focus_follows_mouse, const char *value) {
337     config.disable_focus_follows_mouse = !eval_boolstr(value);
338 }
339
340 CFGFUN(force_xinerama, const char *value) {
341     config.force_xinerama = eval_boolstr(value);
342 }
343
344 CFGFUN(force_focus_wrapping, const char *value) {
345     config.force_focus_wrapping = eval_boolstr(value);
346 }
347
348 CFGFUN(workspace_back_and_forth, const char *value) {
349     config.workspace_auto_back_and_forth = eval_boolstr(value);
350 }
351
352 CFGFUN(fake_outputs, const char *outputs) {
353     config.fake_outputs = sstrdup(outputs);
354 }
355
356 CFGFUN(force_display_urgency_hint, const long duration_ms) {
357     config.workspace_urgency_timer = duration_ms / 1000.0;
358 }
359
360 CFGFUN(workspace, const char *workspace, const char *output) {
361     DLOG("Assigning workspace \"%s\" to output \"%s\"\n", workspace, output);
362     /* Check for earlier assignments of the same workspace so that we
363      * don’t have assignments of a single workspace to different
364      * outputs */
365     struct Workspace_Assignment *assignment;
366     bool duplicate = false;
367     TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
368         if (strcasecmp(assignment->name, workspace) == 0) {
369             ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
370                  workspace);
371             assignment->output = sstrdup(output);
372             duplicate = true;
373         }
374     }
375     if (!duplicate) {
376         assignment = scalloc(sizeof(struct Workspace_Assignment));
377         assignment->name = sstrdup(workspace);
378         assignment->output = sstrdup(output);
379         TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
380     }
381 }
382
383 CFGFUN(ipc_socket, const char *path) {
384     config.ipc_socket_path = sstrdup(path);
385 }
386
387 CFGFUN(restart_state, const char *path) {
388     config.restart_state_path = sstrdup(path);
389 }
390
391 CFGFUN(popup_during_fullscreen, const char *value) {
392     config.popup_during_fullscreen =
393         (strcmp(value, "ignore") == 0 ? PDF_IGNORE : PDF_LEAVE_FULLSCREEN);
394 }
395
396 CFGFUN(color_single, const char *colorclass, const char *color) {
397     /* used for client.background only currently */
398     config.client.background = get_colorpixel(color);
399 }
400
401 CFGFUN(color, const char *colorclass, const char *border, const char *background, const char *text, const char *indicator) {
402 #define APPLY_COLORS(classname) \
403     do { \
404         if (strcmp(colorclass, "client." #classname) == 0) { \
405             config.client.classname.border = get_colorpixel(border); \
406             config.client.classname.background = get_colorpixel(background); \
407             config.client.classname.text = get_colorpixel(text); \
408             if (indicator != NULL) { \
409                 config.client. classname .indicator = get_colorpixel(indicator); \
410             } \
411         } \
412     } while (0)
413
414     APPLY_COLORS(focused_inactive);
415     APPLY_COLORS(focused);
416     APPLY_COLORS(unfocused);
417     APPLY_COLORS(urgent);
418
419 #undef APPLY_COLORS
420 }
421
422 CFGFUN(assign, const char *workspace) {
423     if (match_is_empty(current_match)) {
424         ELOG("Match is empty, ignoring this assignment\n");
425         return;
426     }
427     DLOG("new assignment, using above criteria, to workspace %s\n", workspace);
428     Assignment *assignment = scalloc(sizeof(Assignment));
429     match_copy(&(assignment->match), current_match);
430     assignment->type = A_TO_WORKSPACE;
431     assignment->dest.workspace = sstrdup(workspace);
432     TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
433 }
434
435 /*******************************************************************************
436  * Bar configuration (i3bar)
437  ******************************************************************************/
438
439 static Barconfig current_bar;
440
441 CFGFUN(bar_font, const char *font) {
442     FREE(current_bar.font);
443     current_bar.font = sstrdup(font);
444 }
445
446 CFGFUN(bar_mode, const char *mode) {
447     current_bar.mode = (strcmp(mode, "hide") == 0 ? M_HIDE : M_DOCK);
448 }
449
450 CFGFUN(bar_output, const char *output) {
451     int new_outputs = current_bar.num_outputs + 1;
452     current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
453     current_bar.outputs[current_bar.num_outputs] = sstrdup(output);
454     current_bar.num_outputs = new_outputs;
455 }
456
457 CFGFUN(bar_verbose, const char *verbose) {
458     current_bar.verbose = eval_boolstr(verbose);
459 }
460
461 CFGFUN(bar_modifier, const char *modifier) {
462     if (strcmp(modifier, "Mod1") == 0)
463         current_bar.modifier = M_MOD1;
464     else if (strcmp(modifier, "Mod2") == 0)
465         current_bar.modifier = M_MOD2;
466     else if (strcmp(modifier, "Mod3") == 0)
467         current_bar.modifier = M_MOD3;
468     else if (strcmp(modifier, "Mod4") == 0)
469         current_bar.modifier = M_MOD4;
470     else if (strcmp(modifier, "Mod5") == 0)
471         current_bar.modifier = M_MOD5;
472     else if (strcmp(modifier, "Control") == 0)
473         current_bar.modifier = M_CONTROL;
474     else if (strcmp(modifier, "Shift") == 0)
475         current_bar.modifier = M_SHIFT;
476 }
477
478 CFGFUN(bar_position, const char *position) {
479     current_bar.position = (strcmp(position, "top") == 0 ? P_TOP : P_BOTTOM);
480 }
481
482 CFGFUN(bar_i3bar_command, const char *i3bar_command) {
483     FREE(current_bar.i3bar_command);
484     current_bar.i3bar_command = sstrdup(i3bar_command);
485 }
486
487 CFGFUN(bar_color, const char *colorclass, const char *border, const char *background, const char *text) {
488 #define APPLY_COLORS(classname) \
489     do { \
490         if (strcmp(colorclass, #classname) == 0) { \
491             if (text != NULL) { \
492                 /* New syntax: border, background, text */ \
493                 current_bar.colors. classname ## _border = sstrdup(border); \
494                 current_bar.colors. classname ## _bg = sstrdup(background); \
495                 current_bar.colors. classname ## _text = sstrdup(text); \
496             } else { \
497                 /* Old syntax: text, background */ \
498                 current_bar.colors. classname ## _bg = sstrdup(background); \
499                 current_bar.colors. classname ## _text = sstrdup(border); \
500             } \
501         } \
502     } while (0)
503
504     APPLY_COLORS(focused_workspace);
505     APPLY_COLORS(active_workspace);
506     APPLY_COLORS(inactive_workspace);
507     APPLY_COLORS(urgent_workspace);
508
509 #undef APPLY_COLORS
510 }
511
512 CFGFUN(bar_socket_path, const char *socket_path) {
513     FREE(current_bar.socket_path);
514     current_bar.socket_path = sstrdup(socket_path);
515 }
516
517 CFGFUN(bar_tray_output, const char *output) {
518     FREE(current_bar.tray_output);
519     current_bar.tray_output = sstrdup(output);
520 }
521
522 CFGFUN(bar_color_single, const char *colorclass, const char *color) {
523     if (strcmp(colorclass, "background") == 0)
524         current_bar.colors.background = sstrdup(color);
525     else current_bar.colors.statusline = sstrdup(color);
526 }
527
528 CFGFUN(bar_status_command, const char *command) {
529     FREE(current_bar.status_command);
530     current_bar.status_command = sstrdup(command);
531 }
532
533 CFGFUN(bar_workspace_buttons, const char *value) {
534     current_bar.hide_workspace_buttons = !eval_boolstr(value);
535 }
536
537 CFGFUN(bar_finish) {
538     DLOG("\t new bar configuration finished, saving.\n");
539     /* Generate a unique ID for this bar */
540     current_bar.id = sstrdup("bar-XXXXXX");
541     /* This works similar to mktemp in that it replaces the last six X with
542      * random letters, but without the restriction that the given buffer
543      * has to contain a valid path name. */
544     char *x = current_bar.id + strlen("bar-");
545     while (*x != '\0') {
546         *(x++) = (rand() % 26) + 'a';
547     }
548
549     /* If no font was explicitly set, we use the i3 font as default */
550     if (!current_bar.font && font_pattern)
551         current_bar.font = sstrdup(font_pattern);
552
553     /* Copy the current (static) structure into a dynamically allocated
554      * one, then cleanup our static one. */
555     Barconfig *bar_config = scalloc(sizeof(Barconfig));
556     memcpy(bar_config, &current_bar, sizeof(Barconfig));
557     TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
558
559     memset(&current_bar, '\0', sizeof(Barconfig));
560 }