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