]> git.sur5r.net Git - i3/i3/blob - src/config_directives.c
bugfix: config-parser: bind is a synonym for bindcode
[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     }
190     new_binding->mods = modifiers_from_str(modifiers);
191     new_binding->command = sstrdup(command);
192     TAILQ_INSERT_TAIL(bindings, new_binding, bindings);
193 }
194
195
196 /*******************************************************************************
197  * Mode handling
198  ******************************************************************************/
199
200 static struct bindings_head *current_bindings;
201
202 CFGFUN(mode_binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
203     Binding *new_binding = scalloc(sizeof(Binding));
204     DLOG("bindtype %s, modifiers %s, key %s, release %s\n", bindtype, modifiers, key, release);
205     new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
206     if (strcmp(bindtype, "bindsym") == 0) {
207         new_binding->symbol = sstrdup(key);
208     } else {
209         // TODO: strtol with proper error handling
210         new_binding->keycode = atoi(key);
211     }
212     new_binding->mods = modifiers_from_str(modifiers);
213     new_binding->command = sstrdup(command);
214     TAILQ_INSERT_TAIL(current_bindings, new_binding, bindings);
215 }
216
217 CFGFUN(enter_mode, const char *modename) {
218     if (strcasecmp(modename, "default") == 0) {
219         ELOG("You cannot use the name \"default\" for your mode\n");
220         exit(1);
221     }
222     DLOG("\t now in mode %s\n", modename);
223     struct Mode *mode = scalloc(sizeof(struct Mode));
224     mode->name = sstrdup(modename);
225     mode->bindings = scalloc(sizeof(struct bindings_head));
226     TAILQ_INIT(mode->bindings);
227     current_bindings = mode->bindings;
228     SLIST_INSERT_HEAD(&modes, mode, modes);
229 }
230
231 CFGFUN(exec, const char *exectype, const char *no_startup_id, const char *command) {
232         struct Autostart *new = smalloc(sizeof(struct Autostart));
233         new->command = sstrdup(command);
234         new->no_startup_id = (no_startup_id != NULL);
235         if (strcmp(exectype, "exec") == 0) {
236                 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
237         } else {
238                 TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
239         }
240 }
241
242 CFGFUN(for_window, const char *command) {
243     if (match_is_empty(current_match)) {
244         ELOG("Match is empty, ignoring this for_window statement\n");
245         return;
246     }
247     DLOG("\t should execute command %s for the criteria mentioned above\n", command);
248     Assignment *assignment = scalloc(sizeof(Assignment));
249     assignment->type = A_COMMAND;
250     match_copy(&(assignment->match), current_match);
251     assignment->dest.command = sstrdup(command);
252     TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
253 }
254
255 CFGFUN(floating_minimum_size, const long width, const long height) {
256     config.floating_minimum_width = width;
257     config.floating_minimum_height = height;
258 }
259
260 CFGFUN(floating_maximum_size, const long width, const long height) {
261     config.floating_maximum_width = width;
262     config.floating_maximum_height = height;
263 }
264
265 CFGFUN(floating_modifier, const char *modifiers) {
266     config.floating_modifier = modifiers_from_str(modifiers);
267 }
268
269 CFGFUN(default_orientation, const char *orientation) {
270     if (strcmp(orientation, "horizontal") == 0)
271         config.default_orientation = HORIZ;
272     else if (strcmp(orientation, "vertical") == 0)
273         config.default_orientation = VERT;
274     else config.default_orientation = NO_ORIENTATION;
275 }
276
277 CFGFUN(workspace_layout, const char *layout) {
278     if (strcmp(layout, "default") == 0)
279         config.default_layout = L_DEFAULT;
280     else if (strcmp(layout, "stacking") == 0 ||
281              strcmp(layout, "stacked") == 0)
282         config.default_layout = L_STACKED;
283     else config.default_layout = L_TABBED;
284 }
285
286 CFGFUN(new_window, const char *windowtype, const char *border, const long width) {
287     // FIXME: when using new_float *and* new_window with different border
288     // types, this breaks because default_border_width gets overwritten.
289
290     int border_style;
291     int border_width;
292
293     if (strcmp(border, "1pixel") == 0) {
294         border_style = BS_PIXEL;
295         border_width = 1;
296     } else if (strcmp(border, "none") == 0) {
297         border_style = BS_NONE;
298         border_width = 0;
299     } else if (strcmp(border, "pixel") == 0) {
300         border_style = BS_PIXEL;
301         border_width = width;
302     } else {
303         border_style = BS_NORMAL;
304         border_width = width;
305     }
306
307     if (strcmp(windowtype, "new_window") == 0) {
308         config.default_border = border_style;
309     } else {
310         config.default_floating_border = border_style;
311     }
312 }
313
314 CFGFUN(hide_edge_borders, const char *borders) {
315     if (strcmp(borders, "vertical") == 0)
316         config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
317     else if (strcmp(borders, "horizontal") == 0)
318         config.hide_edge_borders = ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
319     else if (strcmp(borders, "both") == 0)
320         config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE | ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
321     else if (strcmp(borders, "none") == 0)
322         config.hide_edge_borders = ADJ_NONE;
323     else if (eval_boolstr(borders))
324         config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
325     else config.hide_edge_borders = ADJ_NONE;
326 }
327
328 CFGFUN(focus_follows_mouse, const char *value) {
329     config.disable_focus_follows_mouse = !eval_boolstr(value);
330 }
331
332 CFGFUN(force_xinerama, const char *value) {
333     config.force_xinerama = eval_boolstr(value);
334 }
335
336 CFGFUN(force_focus_wrapping, const char *value) {
337     config.force_focus_wrapping = eval_boolstr(value);
338 }
339
340 CFGFUN(workspace_back_and_forth, const char *value) {
341     config.workspace_auto_back_and_forth = eval_boolstr(value);
342 }
343
344 CFGFUN(fake_outputs, const char *outputs) {
345     config.fake_outputs = sstrdup(outputs);
346 }
347
348 CFGFUN(force_display_urgency_hint, const long duration_ms) {
349     config.workspace_urgency_timer = duration_ms / 1000.0;
350 }
351
352 CFGFUN(workspace, const char *workspace, const char *output) {
353     DLOG("Assigning workspace \"%s\" to output \"%s\"\n", workspace, output);
354     /* Check for earlier assignments of the same workspace so that we
355      * don’t have assignments of a single workspace to different
356      * outputs */
357     struct Workspace_Assignment *assignment;
358     bool duplicate = false;
359     TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
360         if (strcasecmp(assignment->name, workspace) == 0) {
361             ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
362                  workspace);
363             assignment->output = sstrdup(output);
364             duplicate = true;
365         }
366     }
367     if (!duplicate) {
368         assignment = scalloc(sizeof(struct Workspace_Assignment));
369         assignment->name = sstrdup(workspace);
370         assignment->output = sstrdup(output);
371         TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
372     }
373 }
374
375 CFGFUN(ipc_socket, const char *path) {
376     config.ipc_socket_path = sstrdup(path);
377 }
378
379 CFGFUN(restart_state, const char *path) {
380     config.restart_state_path = sstrdup(path);
381 }
382
383 CFGFUN(popup_during_fullscreen, const char *value) {
384     config.popup_during_fullscreen =
385         (strcmp(value, "ignore") == 0 ? PDF_IGNORE : PDF_LEAVE_FULLSCREEN);
386 }
387
388 CFGFUN(color_single, const char *colorclass, const char *color) {
389     /* used for client.background only currently */
390     config.client.background = get_colorpixel(color);
391 }
392
393 CFGFUN(color, const char *colorclass, const char *border, const char *background, const char *text, const char *indicator) {
394 #define APPLY_COLORS(classname) \
395     do { \
396         if (strcmp(colorclass, "client." #classname) == 0) { \
397             config.client.classname.border = get_colorpixel(border); \
398             config.client.classname.background = get_colorpixel(background); \
399             config.client.classname.text = get_colorpixel(text); \
400             if (indicator != NULL) { \
401                 config.client. classname .indicator = get_colorpixel(indicator); \
402             } \
403         } \
404     } while (0)
405
406     APPLY_COLORS(focused_inactive);
407     APPLY_COLORS(focused);
408     APPLY_COLORS(unfocused);
409     APPLY_COLORS(urgent);
410
411 #undef APPLY_COLORS
412 }
413
414 CFGFUN(assign, const char *workspace) {
415     if (match_is_empty(current_match)) {
416         ELOG("Match is empty, ignoring this assignment\n");
417         return;
418     }
419     DLOG("new assignment, using above criteria, to workspace %s\n", workspace);
420     Assignment *assignment = scalloc(sizeof(Assignment));
421     match_copy(&(assignment->match), current_match);
422     assignment->type = A_TO_WORKSPACE;
423     assignment->dest.workspace = sstrdup(workspace);
424     TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
425 }
426
427 /*******************************************************************************
428  * Bar configuration (i3bar)
429  ******************************************************************************/
430
431 static Barconfig current_bar;
432
433 CFGFUN(bar_font, const char *font) {
434     FREE(current_bar.font);
435     current_bar.font = sstrdup(font);
436 }
437
438 CFGFUN(bar_mode, const char *mode) {
439     current_bar.mode = (strcmp(mode, "hide") == 0 ? M_HIDE : M_DOCK);
440 }
441
442 CFGFUN(bar_output, const char *output) {
443     int new_outputs = current_bar.num_outputs + 1;
444     current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
445     current_bar.outputs[current_bar.num_outputs] = sstrdup(output);
446     current_bar.num_outputs = new_outputs;
447 }
448
449 CFGFUN(bar_verbose, const char *verbose) {
450     current_bar.verbose = eval_boolstr(verbose);
451 }
452
453 CFGFUN(bar_modifier, const char *modifier) {
454     if (strcmp(modifier, "Mod1") == 0)
455         current_bar.modifier = M_MOD1;
456     else if (strcmp(modifier, "Mod2") == 0)
457         current_bar.modifier = M_MOD2;
458     else if (strcmp(modifier, "Mod3") == 0)
459         current_bar.modifier = M_MOD3;
460     else if (strcmp(modifier, "Mod4") == 0)
461         current_bar.modifier = M_MOD4;
462     else if (strcmp(modifier, "Mod5") == 0)
463         current_bar.modifier = M_MOD5;
464     else if (strcmp(modifier, "Control") == 0)
465         current_bar.modifier = M_CONTROL;
466     else if (strcmp(modifier, "Shift") == 0)
467         current_bar.modifier = M_SHIFT;
468 }
469
470 CFGFUN(bar_position, const char *position) {
471     current_bar.position = (strcmp(position, "top") == 0 ? P_TOP : P_BOTTOM);
472 }
473
474 CFGFUN(bar_i3bar_command, const char *i3bar_command) {
475     FREE(current_bar.i3bar_command);
476     current_bar.i3bar_command = sstrdup(i3bar_command);
477 }
478
479 CFGFUN(bar_color, const char *colorclass, const char *border, const char *background, const char *text) {
480 #define APPLY_COLORS(classname) \
481     do { \
482         if (strcmp(colorclass, #classname) == 0) { \
483             if (text != NULL) { \
484                 /* New syntax: border, background, text */ \
485                 current_bar.colors. classname ## _border = sstrdup(border); \
486                 current_bar.colors. classname ## _bg = sstrdup(background); \
487                 current_bar.colors. classname ## _text = sstrdup(text); \
488             } else { \
489                 /* Old syntax: text, background */ \
490                 current_bar.colors. classname ## _bg = sstrdup(background); \
491                 current_bar.colors. classname ## _text = sstrdup(border); \
492             } \
493         } \
494     } while (0)
495
496     APPLY_COLORS(focused_workspace);
497     APPLY_COLORS(active_workspace);
498     APPLY_COLORS(inactive_workspace);
499     APPLY_COLORS(urgent_workspace);
500
501 #undef APPLY_COLORS
502 }
503
504 CFGFUN(bar_socket_path, const char *socket_path) {
505     FREE(current_bar.socket_path);
506     current_bar.socket_path = sstrdup(socket_path);
507 }
508
509 CFGFUN(bar_tray_output, const char *output) {
510     FREE(current_bar.tray_output);
511     current_bar.tray_output = sstrdup(output);
512 }
513
514 CFGFUN(bar_color_single, const char *colorclass, const char *color) {
515     if (strcmp(colorclass, "background") == 0)
516         current_bar.colors.background = sstrdup(color);
517     else current_bar.colors.statusline = sstrdup(color);
518 }
519
520 CFGFUN(bar_status_command, const char *command) {
521     FREE(current_bar.status_command);
522     current_bar.status_command = sstrdup(command);
523 }
524
525 CFGFUN(bar_workspace_buttons, const char *value) {
526     current_bar.hide_workspace_buttons = !eval_boolstr(value);
527 }
528
529 CFGFUN(bar_finish) {
530     DLOG("\t new bar configuration finished, saving.\n");
531     /* Generate a unique ID for this bar */
532     current_bar.id = sstrdup("bar-XXXXXX");
533     /* This works similar to mktemp in that it replaces the last six X with
534      * random letters, but without the restriction that the given buffer
535      * has to contain a valid path name. */
536     char *x = current_bar.id + strlen("bar-");
537     while (*x != '\0') {
538         *(x++) = (rand() % 26) + 'a';
539     }
540
541     /* If no font was explicitly set, we use the i3 font as default */
542     if (!current_bar.font && font_pattern)
543         current_bar.font = sstrdup(font_pattern);
544
545     /* Copy the current (static) structure into a dynamically allocated
546      * one, then cleanup our static one. */
547     Barconfig *bar_config = scalloc(sizeof(Barconfig));
548     memcpy(bar_config, &current_bar, sizeof(Barconfig));
549     TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
550
551     memset(&current_bar, '\0', sizeof(Barconfig));
552 }