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