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