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