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