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