]> git.sur5r.net Git - i3/i3/blob - src/config_directives.c
daf4339ba947dc16a8ab615349931365698f0a40
[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             DLOG("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             DLOG("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 /*
134  * A utility function to convert a string of modifiers to the corresponding bit
135  * mask.
136  */
137 uint32_t modifiers_from_str(const char *str) {
138     /* It might be better to use strtok() here, but the simpler strstr() should
139      * do for now. */
140     uint32_t result = 0;
141     if (str == NULL)
142         return result;
143     if (strstr(str, "Mod1") != NULL)
144         result |= BIND_MOD1;
145     if (strstr(str, "Mod2") != NULL)
146         result |= BIND_MOD2;
147     if (strstr(str, "Mod3") != NULL)
148         result |= BIND_MOD3;
149     if (strstr(str, "Mod4") != NULL)
150         result |= BIND_MOD4;
151     if (strstr(str, "Mod5") != NULL)
152         result |= BIND_MOD5;
153     if (strstr(str, "Control") != NULL ||
154         strstr(str, "Ctrl") != NULL)
155         result |= BIND_CONTROL;
156     if (strstr(str, "Shift") != NULL)
157         result |= BIND_SHIFT;
158     if (strstr(str, "Mode_switch") != NULL)
159         result |= BIND_MODE_SWITCH;
160     return result;
161 }
162
163 static char *font_pattern;
164
165 CFGFUN(font, const char *font) {
166     config.font = load_font(font, true);
167     set_font(&config.font);
168
169     /* Save the font pattern for using it as bar font later on */
170     FREE(font_pattern);
171     font_pattern = sstrdup(font);
172 }
173
174 CFGFUN(binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *border, const char *whole_window, const char *command) {
175     configure_binding(bindtype, modifiers, key, release, border, whole_window, command, DEFAULT_BINDING_MODE);
176 }
177
178 /*******************************************************************************
179  * Mode handling
180  ******************************************************************************/
181
182 static char *current_mode;
183
184 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) {
185     configure_binding(bindtype, modifiers, key, release, border, whole_window, command, current_mode);
186 }
187
188 CFGFUN(enter_mode, const char *modename) {
189     if (strcasecmp(modename, DEFAULT_BINDING_MODE) == 0) {
190         ELOG("You cannot use the name %s for your mode\n", DEFAULT_BINDING_MODE);
191         exit(1);
192     }
193     DLOG("\t now in mode %s\n", modename);
194     FREE(current_mode);
195     current_mode = sstrdup(modename);
196 }
197
198 CFGFUN(exec, const char *exectype, const char *no_startup_id, const char *command) {
199     struct Autostart *new = smalloc(sizeof(struct Autostart));
200     new->command = sstrdup(command);
201     new->no_startup_id = (no_startup_id != NULL);
202     if (strcmp(exectype, "exec") == 0) {
203         TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
204     } else {
205         TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
206     }
207 }
208
209 CFGFUN(for_window, const char *command) {
210     if (match_is_empty(current_match)) {
211         ELOG("Match is empty, ignoring this for_window statement\n");
212         return;
213     }
214     DLOG("\t should execute command %s for the criteria mentioned above\n", command);
215     Assignment *assignment = scalloc(sizeof(Assignment));
216     assignment->type = A_COMMAND;
217     match_copy(&(assignment->match), current_match);
218     assignment->dest.command = sstrdup(command);
219     TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
220 }
221
222 CFGFUN(floating_minimum_size, const long width, const long height) {
223     config.floating_minimum_width = width;
224     config.floating_minimum_height = height;
225 }
226
227 CFGFUN(floating_maximum_size, const long width, const long height) {
228     config.floating_maximum_width = width;
229     config.floating_maximum_height = height;
230 }
231
232 CFGFUN(floating_modifier, const char *modifiers) {
233     config.floating_modifier = modifiers_from_str(modifiers);
234 }
235
236 CFGFUN(default_orientation, const char *orientation) {
237     if (strcmp(orientation, "horizontal") == 0)
238         config.default_orientation = HORIZ;
239     else if (strcmp(orientation, "vertical") == 0)
240         config.default_orientation = VERT;
241     else
242         config.default_orientation = NO_ORIENTATION;
243 }
244
245 CFGFUN(workspace_layout, const char *layout) {
246     if (strcmp(layout, "default") == 0)
247         config.default_layout = L_DEFAULT;
248     else if (strcmp(layout, "stacking") == 0 ||
249              strcmp(layout, "stacked") == 0)
250         config.default_layout = L_STACKED;
251     else
252         config.default_layout = L_TABBED;
253 }
254
255 CFGFUN(new_window, const char *windowtype, const char *border, const long width) {
256     int border_style;
257     int border_width;
258
259     if (strcmp(border, "1pixel") == 0) {
260         border_style = BS_PIXEL;
261         border_width = 1;
262     } else if (strcmp(border, "none") == 0) {
263         border_style = BS_NONE;
264         border_width = 0;
265     } else if (strcmp(border, "pixel") == 0) {
266         border_style = BS_PIXEL;
267         border_width = width;
268     } else {
269         border_style = BS_NORMAL;
270         border_width = width;
271     }
272
273     if (strcmp(windowtype, "new_window") == 0) {
274         DLOG("default tiled border style = %d and border width = %d (%d physical px)\n",
275              border_style, border_width, logical_px(border_width));
276         config.default_border = border_style;
277         config.default_border_width = logical_px(border_width);
278     } else {
279         DLOG("default floating border style = %d and border width = %d (%d physical px)\n",
280              border_style, border_width, logical_px(border_width));
281         config.default_floating_border = border_style;
282         config.default_floating_border_width = logical_px(border_width);
283     }
284 }
285
286 CFGFUN(hide_edge_borders, const char *borders) {
287     if (strcmp(borders, "vertical") == 0)
288         config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
289     else if (strcmp(borders, "horizontal") == 0)
290         config.hide_edge_borders = ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
291     else if (strcmp(borders, "both") == 0)
292         config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE | ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
293     else if (strcmp(borders, "none") == 0)
294         config.hide_edge_borders = ADJ_NONE;
295     else if (eval_boolstr(borders))
296         config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
297     else
298         config.hide_edge_borders = ADJ_NONE;
299 }
300
301 CFGFUN(focus_follows_mouse, const char *value) {
302     config.disable_focus_follows_mouse = !eval_boolstr(value);
303 }
304
305 CFGFUN(mouse_warping, const char *value) {
306     if (strcmp(value, "none") == 0)
307         config.mouse_warping = POINTER_WARPING_NONE;
308     else if (strcmp(value, "output") == 0)
309         config.mouse_warping = POINTER_WARPING_OUTPUT;
310 }
311
312 CFGFUN(force_xinerama, const char *value) {
313     config.force_xinerama = eval_boolstr(value);
314 }
315
316 CFGFUN(force_focus_wrapping, const char *value) {
317     config.force_focus_wrapping = eval_boolstr(value);
318 }
319
320 CFGFUN(workspace_back_and_forth, const char *value) {
321     config.workspace_auto_back_and_forth = eval_boolstr(value);
322 }
323
324 CFGFUN(fake_outputs, const char *outputs) {
325     config.fake_outputs = sstrdup(outputs);
326 }
327
328 CFGFUN(force_display_urgency_hint, const long duration_ms) {
329     config.workspace_urgency_timer = duration_ms / 1000.0;
330 }
331
332 CFGFUN(focus_on_window_activation, const char *mode) {
333     if (strcmp(mode, "smart") == 0)
334         config.focus_on_window_activation = FOWA_SMART;
335     else if (strcmp(mode, "urgent") == 0)
336         config.focus_on_window_activation = FOWA_URGENT;
337     else if (strcmp(mode, "focus") == 0)
338         config.focus_on_window_activation = FOWA_FOCUS;
339     else if (strcmp(mode, "none") == 0)
340         config.focus_on_window_activation = FOWA_NONE;
341     else {
342         ELOG("Unknown focus_on_window_activation mode \"%s\", ignoring it.\n", mode);
343         return;
344     }
345
346     DLOG("Set new focus_on_window_activation mode = %i", config.focus_on_window_activation);
347 }
348
349 CFGFUN(show_marks, const char *value) {
350     config.show_marks = eval_boolstr(value);
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     APPLY_COLORS(placeholder);
417
418 #undef APPLY_COLORS
419 }
420
421 CFGFUN(assign, const char *workspace) {
422     if (match_is_empty(current_match)) {
423         ELOG("Match is empty, ignoring this assignment\n");
424         return;
425     }
426     DLOG("new assignment, using above criteria, to workspace %s\n", workspace);
427     Assignment *assignment = scalloc(sizeof(Assignment));
428     match_copy(&(assignment->match), current_match);
429     assignment->type = A_TO_WORKSPACE;
430     assignment->dest.workspace = sstrdup(workspace);
431     TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
432 }
433
434 CFGFUN(no_focus) {
435     if (match_is_empty(current_match)) {
436         ELOG("Match is empty, ignoring this assignment\n");
437         return;
438     }
439
440     DLOG("new assignment, using above criteria, to ignore focus on manage");
441     Assignment *assignment = scalloc(sizeof(Assignment));
442     match_copy(&(assignment->match), current_match);
443     assignment->type = A_NO_FOCUS;
444     TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
445 }
446
447 /*******************************************************************************
448  * Bar configuration (i3bar)
449  ******************************************************************************/
450
451 static Barconfig current_bar;
452
453 CFGFUN(bar_font, const char *font) {
454     FREE(current_bar.font);
455     current_bar.font = sstrdup(font);
456 }
457
458 CFGFUN(bar_separator_symbol, const char *separator) {
459     FREE(current_bar.separator_symbol);
460     current_bar.separator_symbol = sstrdup(separator);
461 }
462
463 CFGFUN(bar_mode, const char *mode) {
464     current_bar.mode = (strcmp(mode, "dock") == 0 ? M_DOCK : (strcmp(mode, "hide") == 0 ? M_HIDE : M_INVISIBLE));
465 }
466
467 CFGFUN(bar_hidden_state, const char *hidden_state) {
468     current_bar.hidden_state = (strcmp(hidden_state, "hide") == 0 ? S_HIDE : S_SHOW);
469 }
470
471 CFGFUN(bar_id, const char *bar_id) {
472     current_bar.id = sstrdup(bar_id);
473 }
474
475 CFGFUN(bar_output, const char *output) {
476     int new_outputs = current_bar.num_outputs + 1;
477     current_bar.outputs = srealloc(current_bar.outputs, sizeof(char *) * new_outputs);
478     current_bar.outputs[current_bar.num_outputs] = sstrdup(output);
479     current_bar.num_outputs = new_outputs;
480 }
481
482 CFGFUN(bar_verbose, const char *verbose) {
483     current_bar.verbose = eval_boolstr(verbose);
484 }
485
486 CFGFUN(bar_modifier, const char *modifier) {
487     if (strcmp(modifier, "Mod1") == 0)
488         current_bar.modifier = M_MOD1;
489     else if (strcmp(modifier, "Mod2") == 0)
490         current_bar.modifier = M_MOD2;
491     else if (strcmp(modifier, "Mod3") == 0)
492         current_bar.modifier = M_MOD3;
493     else if (strcmp(modifier, "Mod4") == 0)
494         current_bar.modifier = M_MOD4;
495     else if (strcmp(modifier, "Mod5") == 0)
496         current_bar.modifier = M_MOD5;
497     else if (strcmp(modifier, "Control") == 0 ||
498              strcmp(modifier, "Ctrl") == 0)
499         current_bar.modifier = M_CONTROL;
500     else if (strcmp(modifier, "Shift") == 0)
501         current_bar.modifier = M_SHIFT;
502 }
503
504 CFGFUN(bar_wheel_up_cmd, const char *command) {
505     FREE(current_bar.wheel_up_cmd);
506     current_bar.wheel_up_cmd = sstrdup(command);
507 }
508
509 CFGFUN(bar_wheel_down_cmd, const char *command) {
510     FREE(current_bar.wheel_down_cmd);
511     current_bar.wheel_down_cmd = sstrdup(command);
512 }
513
514 CFGFUN(bar_position, const char *position) {
515     current_bar.position = (strcmp(position, "top") == 0 ? P_TOP : P_BOTTOM);
516 }
517
518 CFGFUN(bar_i3bar_command, const char *i3bar_command) {
519     FREE(current_bar.i3bar_command);
520     current_bar.i3bar_command = sstrdup(i3bar_command);
521 }
522
523 CFGFUN(bar_color, const char *colorclass, const char *border, const char *background, const char *text) {
524 #define APPLY_COLORS(classname)                                          \
525     do {                                                                 \
526         if (strcmp(colorclass, #classname) == 0) {                       \
527             if (text != NULL) {                                          \
528                 /* New syntax: border, background, text */               \
529                 current_bar.colors.classname##_border = sstrdup(border); \
530                 current_bar.colors.classname##_bg = sstrdup(background); \
531                 current_bar.colors.classname##_text = sstrdup(text);     \
532             } else {                                                     \
533                 /* Old syntax: text, background */                       \
534                 current_bar.colors.classname##_bg = sstrdup(background); \
535                 current_bar.colors.classname##_text = sstrdup(border);   \
536             }                                                            \
537         }                                                                \
538     } while (0)
539
540     APPLY_COLORS(focused_workspace);
541     APPLY_COLORS(active_workspace);
542     APPLY_COLORS(inactive_workspace);
543     APPLY_COLORS(urgent_workspace);
544
545 #undef APPLY_COLORS
546 }
547
548 CFGFUN(bar_socket_path, const char *socket_path) {
549     FREE(current_bar.socket_path);
550     current_bar.socket_path = sstrdup(socket_path);
551 }
552
553 CFGFUN(bar_tray_output, const char *output) {
554     FREE(current_bar.tray_output);
555     current_bar.tray_output = sstrdup(output);
556 }
557
558 CFGFUN(bar_color_single, const char *colorclass, const char *color) {
559     if (strcmp(colorclass, "background") == 0)
560         current_bar.colors.background = sstrdup(color);
561     else if (strcmp(colorclass, "separator") == 0)
562         current_bar.colors.separator = sstrdup(color);
563     else
564         current_bar.colors.statusline = sstrdup(color);
565 }
566
567 CFGFUN(bar_status_command, const char *command) {
568     FREE(current_bar.status_command);
569     current_bar.status_command = sstrdup(command);
570 }
571
572 CFGFUN(bar_binding_mode_indicator, const char *value) {
573     current_bar.hide_binding_mode_indicator = !eval_boolstr(value);
574 }
575
576 CFGFUN(bar_workspace_buttons, const char *value) {
577     current_bar.hide_workspace_buttons = !eval_boolstr(value);
578 }
579
580 CFGFUN(bar_strip_workspace_numbers, const char *value) {
581     current_bar.strip_workspace_numbers = eval_boolstr(value);
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)
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 && font_pattern)
594         current_bar.font = sstrdup(font_pattern);
595
596     /* Copy the current (static) structure into a dynamically allocated
597      * one, then cleanup our static one. */
598     Barconfig *bar_config = scalloc(sizeof(Barconfig));
599     memcpy(bar_config, &current_bar, sizeof(Barconfig));
600     TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
601
602     memset(&current_bar, '\0', sizeof(Barconfig));
603 }