]> git.sur5r.net Git - i3/i3/blob - src/config_directives.c
Merge branch 'master' into next
[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 // Macros to make the YAJL API a bit easier to use.
18 #define y(x, ...) yajl_gen_ ## x (cmd_output->json_gen, ##__VA_ARGS__)
19 #define ystr(str) yajl_gen_string(cmd_output->json_gen, (unsigned char*)str, strlen(str))
20 #define ysuccess(success) do { \
21     y(map_open); \
22     ystr("success"); \
23     y(bool, success); \
24     y(map_close); \
25 } while (0)
26
27 /*******************************************************************************
28  * Criteria functions.
29  ******************************************************************************/
30
31 static int criteria_next_state;
32
33 /*
34  * Initializes the specified 'Match' data structure and the initial state of
35  * commands.c for matching target windows of a command.
36  *
37  */
38 CFGFUN(criteria_init, int _state) {
39     criteria_next_state = _state;
40
41     DLOG("Initializing criteria, current_match = %p, state = %d\n", current_match, _state);
42     match_init(current_match);
43 }
44
45 CFGFUN(criteria_pop_state) {
46     result->next_state = criteria_next_state;
47 }
48
49 /*
50  * Interprets a ctype=cvalue pair and adds it to the current match
51  * specification.
52  *
53  */
54 CFGFUN(criteria_add, const char *ctype, const char *cvalue) {
55     DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
56
57     if (strcmp(ctype, "class") == 0) {
58         current_match->class = regex_new(cvalue);
59         return;
60     }
61
62     if (strcmp(ctype, "instance") == 0) {
63         current_match->instance = regex_new(cvalue);
64         return;
65     }
66
67     if (strcmp(ctype, "window_role") == 0) {
68         current_match->role = regex_new(cvalue);
69         return;
70     }
71
72     if (strcmp(ctype, "con_id") == 0) {
73         char *end;
74         long parsed = strtol(cvalue, &end, 10);
75         if (parsed == LONG_MIN ||
76             parsed == LONG_MAX ||
77             parsed < 0 ||
78             (end && *end != '\0')) {
79             ELOG("Could not parse con id \"%s\"\n", cvalue);
80         } else {
81             current_match->con_id = (Con*)parsed;
82             printf("id as int = %p\n", current_match->con_id);
83         }
84         return;
85     }
86
87     if (strcmp(ctype, "id") == 0) {
88         char *end;
89         long parsed = strtol(cvalue, &end, 10);
90         if (parsed == LONG_MIN ||
91             parsed == LONG_MAX ||
92             parsed < 0 ||
93             (end && *end != '\0')) {
94             ELOG("Could not parse window id \"%s\"\n", cvalue);
95         } else {
96             current_match->id = parsed;
97             printf("window id as int = %d\n", current_match->id);
98         }
99         return;
100     }
101
102     if (strcmp(ctype, "con_mark") == 0) {
103         current_match->mark = regex_new(cvalue);
104         return;
105     }
106
107     if (strcmp(ctype, "title") == 0) {
108         current_match->title = regex_new(cvalue);
109         return;
110     }
111
112     if (strcmp(ctype, "urgent") == 0) {
113         if (strcasecmp(cvalue, "latest") == 0 ||
114             strcasecmp(cvalue, "newest") == 0 ||
115             strcasecmp(cvalue, "recent") == 0 ||
116             strcasecmp(cvalue, "last") == 0) {
117             current_match->urgent = U_LATEST;
118         } else if (strcasecmp(cvalue, "oldest") == 0 ||
119                    strcasecmp(cvalue, "first") == 0) {
120             current_match->urgent = U_OLDEST;
121         }
122         return;
123     }
124
125     ELOG("Unknown criterion: %s\n", ctype);
126 }
127
128 /* TODO: refactor the above criteria code into a single file (with src/commands.c). */
129
130 /*******************************************************************************
131  * Utility functions
132  ******************************************************************************/
133
134 static bool eval_boolstr(const char *str) {
135     return (strcasecmp(str, "1") == 0 ||
136             strcasecmp(str, "yes") == 0 ||
137             strcasecmp(str, "true") == 0 ||
138             strcasecmp(str, "on") == 0 ||
139             strcasecmp(str, "enable") == 0 ||
140             strcasecmp(str, "active") == 0);
141 }
142
143 static uint32_t modifiers_from_str(const char *str) {
144     /* It might be better to use strtok() here, but the simpler strstr() should
145      * do for now. */
146     uint32_t result = 0;
147     if (str == NULL)
148         return result;
149     if (strstr(str, "Mod1") != NULL)
150         result |= BIND_MOD1;
151     if (strstr(str, "Mod2") != NULL)
152         result |= BIND_MOD2;
153     if (strstr(str, "Mod3") != NULL)
154         result |= BIND_MOD3;
155     if (strstr(str, "Mod4") != NULL)
156         result |= BIND_MOD4;
157     if (strstr(str, "Mod5") != NULL)
158         result |= BIND_MOD5;
159     if (strstr(str, "Control") != NULL ||
160         strstr(str, "Ctrl") != NULL)
161         result |= BIND_CONTROL;
162     if (strstr(str, "Shift") != NULL)
163         result |= BIND_SHIFT;
164     if (strstr(str, "Mode_switch") != NULL)
165         result |= BIND_MODE_SWITCH;
166     return result;
167 }
168
169 static char *font_pattern;
170
171 CFGFUN(font, const char *font) {
172         config.font = load_font(font, true);
173         set_font(&config.font);
174
175         /* Save the font pattern for using it as bar font later on */
176         FREE(font_pattern);
177         font_pattern = sstrdup(font);
178 }
179
180 // TODO: refactor with mode_binding
181 CFGFUN(binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
182     Binding *new_binding = scalloc(sizeof(Binding));
183     DLOG("bindtype %s, modifiers %s, key %s, release %s\n", bindtype, modifiers, key, release);
184     new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
185     if (strcmp(bindtype, "bindsym") == 0) {
186         new_binding->symbol = sstrdup(key);
187     } else {
188         // TODO: strtol with proper error handling
189         new_binding->keycode = atoi(key);
190         if (new_binding->keycode == 0) {
191             ELOG("Could not parse \"%s\" as a keycode, ignoring this binding.\n", key);
192             return;
193         }
194     }
195     new_binding->mods = modifiers_from_str(modifiers);
196     new_binding->command = sstrdup(command);
197     TAILQ_INSERT_TAIL(bindings, new_binding, bindings);
198 }
199
200
201 /*******************************************************************************
202  * Mode handling
203  ******************************************************************************/
204
205 static struct bindings_head *current_bindings;
206
207 CFGFUN(mode_binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
208     Binding *new_binding = scalloc(sizeof(Binding));
209     DLOG("bindtype %s, modifiers %s, key %s, release %s\n", bindtype, modifiers, key, release);
210     new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
211     if (strcmp(bindtype, "bindsym") == 0) {
212         new_binding->symbol = sstrdup(key);
213     } else {
214         // TODO: strtol with proper error handling
215         new_binding->keycode = atoi(key);
216         if (new_binding->keycode == 0) {
217             ELOG("Could not parse \"%s\" as a keycode, ignoring this binding.\n", key);
218             return;
219         }
220     }
221     new_binding->mods = modifiers_from_str(modifiers);
222     new_binding->command = sstrdup(command);
223     TAILQ_INSERT_TAIL(current_bindings, new_binding, bindings);
224 }
225
226 CFGFUN(enter_mode, const char *modename) {
227     if (strcasecmp(modename, "default") == 0) {
228         ELOG("You cannot use the name \"default\" for your mode\n");
229         exit(1);
230     }
231     DLOG("\t now in mode %s\n", modename);
232     struct Mode *mode = scalloc(sizeof(struct Mode));
233     mode->name = sstrdup(modename);
234     mode->bindings = scalloc(sizeof(struct bindings_head));
235     TAILQ_INIT(mode->bindings);
236     current_bindings = mode->bindings;
237     SLIST_INSERT_HEAD(&modes, mode, modes);
238 }
239
240 CFGFUN(exec, const char *exectype, const char *no_startup_id, const char *command) {
241         struct Autostart *new = smalloc(sizeof(struct Autostart));
242         new->command = sstrdup(command);
243         new->no_startup_id = (no_startup_id != NULL);
244         if (strcmp(exectype, "exec") == 0) {
245                 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
246         } else {
247                 TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
248         }
249 }
250
251 CFGFUN(for_window, const char *command) {
252     if (match_is_empty(current_match)) {
253         ELOG("Match is empty, ignoring this for_window statement\n");
254         return;
255     }
256     DLOG("\t should execute command %s for the criteria mentioned above\n", command);
257     Assignment *assignment = scalloc(sizeof(Assignment));
258     assignment->type = A_COMMAND;
259     match_copy(&(assignment->match), current_match);
260     assignment->dest.command = sstrdup(command);
261     TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
262 }
263
264 CFGFUN(floating_minimum_size, const long width, const long height) {
265     config.floating_minimum_width = width;
266     config.floating_minimum_height = height;
267 }
268
269 CFGFUN(floating_maximum_size, const long width, const long height) {
270     config.floating_maximum_width = width;
271     config.floating_maximum_height = height;
272 }
273
274 CFGFUN(floating_modifier, const char *modifiers) {
275     config.floating_modifier = modifiers_from_str(modifiers);
276 }
277
278 CFGFUN(default_orientation, const char *orientation) {
279     if (strcmp(orientation, "horizontal") == 0)
280         config.default_orientation = HORIZ;
281     else if (strcmp(orientation, "vertical") == 0)
282         config.default_orientation = VERT;
283     else config.default_orientation = NO_ORIENTATION;
284 }
285
286 CFGFUN(workspace_layout, const char *layout) {
287     if (strcmp(layout, "default") == 0)
288         config.default_layout = L_DEFAULT;
289     else if (strcmp(layout, "stacking") == 0 ||
290              strcmp(layout, "stacked") == 0)
291         config.default_layout = L_STACKED;
292     else config.default_layout = L_TABBED;
293 }
294
295 CFGFUN(new_window, const char *windowtype, const char *border, const long width) {
296     // FIXME: when using new_float *and* new_window with different border
297     // types, this breaks because default_border_width gets overwritten.
298
299     int border_style;
300     int border_width;
301
302     if (strcmp(border, "1pixel") == 0) {
303         border_style = BS_PIXEL;
304         border_width = 1;
305     } else if (strcmp(border, "none") == 0) {
306         border_style = BS_NONE;
307         border_width = 0;
308     } else if (strcmp(border, "pixel") == 0) {
309         border_style = BS_PIXEL;
310         border_width = width;
311     } else {
312         border_style = BS_NORMAL;
313         border_width = width;
314     }
315
316     if (strcmp(windowtype, "new_window") == 0) {
317         config.default_border = border_style;
318     } else {
319         config.default_floating_border = border_style;
320     }
321
322     config.default_border_width = border_width;
323 }
324
325 CFGFUN(hide_edge_borders, const char *borders) {
326     if (strcmp(borders, "vertical") == 0)
327         config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
328     else if (strcmp(borders, "horizontal") == 0)
329         config.hide_edge_borders = ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
330     else if (strcmp(borders, "both") == 0)
331         config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE | ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
332     else if (strcmp(borders, "none") == 0)
333         config.hide_edge_borders = ADJ_NONE;
334     else if (eval_boolstr(borders))
335         config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
336     else config.hide_edge_borders = ADJ_NONE;
337 }
338
339 CFGFUN(focus_follows_mouse, const char *value) {
340     config.disable_focus_follows_mouse = !eval_boolstr(value);
341 }
342
343 CFGFUN(force_xinerama, const char *value) {
344     config.force_xinerama = eval_boolstr(value);
345 }
346
347 CFGFUN(force_focus_wrapping, const char *value) {
348     config.force_focus_wrapping = eval_boolstr(value);
349 }
350
351 CFGFUN(workspace_back_and_forth, const char *value) {
352     config.workspace_auto_back_and_forth = eval_boolstr(value);
353 }
354
355 CFGFUN(fake_outputs, const char *outputs) {
356     config.fake_outputs = sstrdup(outputs);
357 }
358
359 CFGFUN(force_display_urgency_hint, const long duration_ms) {
360     config.workspace_urgency_timer = duration_ms / 1000.0;
361 }
362
363 CFGFUN(workspace, const char *workspace, const char *output) {
364     DLOG("Assigning workspace \"%s\" to output \"%s\"\n", workspace, output);
365     /* Check for earlier assignments of the same workspace so that we
366      * don’t have assignments of a single workspace to different
367      * outputs */
368     struct Workspace_Assignment *assignment;
369     bool duplicate = false;
370     TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
371         if (strcasecmp(assignment->name, workspace) == 0) {
372             ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
373                  workspace);
374             assignment->output = sstrdup(output);
375             duplicate = true;
376         }
377     }
378     if (!duplicate) {
379         assignment = scalloc(sizeof(struct Workspace_Assignment));
380         assignment->name = sstrdup(workspace);
381         assignment->output = sstrdup(output);
382         TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
383     }
384 }
385
386 CFGFUN(ipc_socket, const char *path) {
387     config.ipc_socket_path = sstrdup(path);
388 }
389
390 CFGFUN(restart_state, const char *path) {
391     config.restart_state_path = sstrdup(path);
392 }
393
394 CFGFUN(popup_during_fullscreen, const char *value) {
395     if (strcmp(value, "ignore") == 0) {
396         config.popup_during_fullscreen = PDF_IGNORE;
397     } else if (strcmp(value, "leave_fullscreen") == 0) {
398         config.popup_during_fullscreen = PDF_LEAVE_FULLSCREEN;
399     } else {
400         config.popup_during_fullscreen = PDF_SMART;
401     }
402 }
403
404 CFGFUN(color_single, const char *colorclass, const char *color) {
405     /* used for client.background only currently */
406     config.client.background = get_colorpixel(color);
407 }
408
409 CFGFUN(color, const char *colorclass, const char *border, const char *background, const char *text, const char *indicator) {
410 #define APPLY_COLORS(classname) \
411     do { \
412         if (strcmp(colorclass, "client." #classname) == 0) { \
413             config.client.classname.border = get_colorpixel(border); \
414             config.client.classname.background = get_colorpixel(background); \
415             config.client.classname.text = get_colorpixel(text); \
416             if (indicator != NULL) { \
417                 config.client. classname .indicator = get_colorpixel(indicator); \
418             } \
419         } \
420     } while (0)
421
422     APPLY_COLORS(focused_inactive);
423     APPLY_COLORS(focused);
424     APPLY_COLORS(unfocused);
425     APPLY_COLORS(urgent);
426
427 #undef APPLY_COLORS
428 }
429
430 CFGFUN(assign, const char *workspace) {
431     if (match_is_empty(current_match)) {
432         ELOG("Match is empty, ignoring this assignment\n");
433         return;
434     }
435     DLOG("new assignment, using above criteria, to workspace %s\n", workspace);
436     Assignment *assignment = scalloc(sizeof(Assignment));
437     match_copy(&(assignment->match), current_match);
438     assignment->type = A_TO_WORKSPACE;
439     assignment->dest.workspace = sstrdup(workspace);
440     TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
441 }
442
443 /*******************************************************************************
444  * Bar configuration (i3bar)
445  ******************************************************************************/
446
447 static Barconfig current_bar;
448
449 CFGFUN(bar_font, const char *font) {
450     FREE(current_bar.font);
451     current_bar.font = sstrdup(font);
452 }
453
454 CFGFUN(bar_mode, const char *mode) {
455     current_bar.mode = (strcmp(mode, "dock") == 0 ? M_DOCK : (strcmp(mode, "hide") == 0 ? M_HIDE : M_INVISIBLE));
456 }
457
458 CFGFUN(bar_hidden_state, const char *hidden_state) {
459     current_bar.hidden_state = (strcmp(hidden_state, "hide") == 0 ? S_HIDE : S_SHOW);
460 }
461
462 CFGFUN(bar_id, const char *bar_id) {
463     current_bar.id = sstrdup(bar_id);
464 }
465
466 CFGFUN(bar_output, const char *output) {
467     int new_outputs = current_bar.num_outputs + 1;
468     current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
469     current_bar.outputs[current_bar.num_outputs] = sstrdup(output);
470     current_bar.num_outputs = new_outputs;
471 }
472
473 CFGFUN(bar_verbose, const char *verbose) {
474     current_bar.verbose = eval_boolstr(verbose);
475 }
476
477 CFGFUN(bar_modifier, const char *modifier) {
478     if (strcmp(modifier, "Mod1") == 0)
479         current_bar.modifier = M_MOD1;
480     else if (strcmp(modifier, "Mod2") == 0)
481         current_bar.modifier = M_MOD2;
482     else if (strcmp(modifier, "Mod3") == 0)
483         current_bar.modifier = M_MOD3;
484     else if (strcmp(modifier, "Mod4") == 0)
485         current_bar.modifier = M_MOD4;
486     else if (strcmp(modifier, "Mod5") == 0)
487         current_bar.modifier = M_MOD5;
488     else if (strcmp(modifier, "Control") == 0 ||
489              strcmp(modifier, "Ctrl") == 0)
490         current_bar.modifier = M_CONTROL;
491     else if (strcmp(modifier, "Shift") == 0)
492         current_bar.modifier = M_SHIFT;
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
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     FREE(current_bar.tray_output);
536     current_bar.tray_output = sstrdup(output);
537 }
538
539 CFGFUN(bar_color_single, const char *colorclass, const char *color) {
540     if (strcmp(colorclass, "background") == 0)
541         current_bar.colors.background = sstrdup(color);
542     else if (strcmp(colorclass, "separator") == 0)
543         current_bar.colors.separator = sstrdup(color);
544     else
545         current_bar.colors.statusline = sstrdup(color);
546 }
547
548 CFGFUN(bar_status_command, const char *command) {
549     FREE(current_bar.status_command);
550     current_bar.status_command = sstrdup(command);
551 }
552
553 CFGFUN(bar_workspace_buttons, const char *value) {
554     current_bar.hide_workspace_buttons = !eval_boolstr(value);
555 }
556
557 CFGFUN(bar_finish) {
558     DLOG("\t new bar configuration finished, saving.\n");
559     /* Generate a unique ID for this bar if not already configured */
560     if (!current_bar.id)
561         sasprintf(&current_bar.id, "bar-%d", config.number_barconfigs);
562
563     config.number_barconfigs++;
564
565     /* If no font was explicitly set, we use the i3 font as default */
566     if (!current_bar.font && font_pattern)
567         current_bar.font = sstrdup(font_pattern);
568
569     /* Copy the current (static) structure into a dynamically allocated
570      * one, then cleanup our static one. */
571     Barconfig *bar_config = scalloc(sizeof(Barconfig));
572     memcpy(bar_config, &current_bar, sizeof(Barconfig));
573     TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
574
575     memset(&current_bar, '\0', sizeof(Barconfig));
576 }