]> git.sur5r.net Git - i3/i3/blob - src/config_directives.c
port the entire old config parser to the new one
[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         result |= BIND_CONTROL;
161     if (strstr(str, "Shift") != NULL)
162         result |= BIND_SHIFT;
163     if (strstr(str, "Mode_switch") != NULL)
164         result |= BIND_MODE_SWITCH;
165     return result;
166 }
167
168 static char *font_pattern;
169
170 CFGFUN(font, const char *font) {
171         config.font = load_font(font, true);
172         set_font(&config.font);
173
174         /* Save the font pattern for using it as bar font later on */
175         FREE(font_pattern);
176         font_pattern = sstrdup(font);
177 }
178
179 // TODO: refactor with mode_binding
180 CFGFUN(binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
181     Binding *new_binding = scalloc(sizeof(Binding));
182     new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
183     if (strcmp(bindtype, "bindsym") == 0) {
184         new_binding->symbol = sstrdup(key);
185     } else {
186         // TODO: strtol with proper error handling
187         new_binding->keycode = atoi(key);
188     }
189     new_binding->mods = modifiers_from_str(modifiers);
190     new_binding->command = sstrdup(command);
191     TAILQ_INSERT_TAIL(bindings, new_binding, bindings);
192 }
193
194
195 /*******************************************************************************
196  * Mode handling
197  ******************************************************************************/
198
199 static struct bindings_head *current_bindings;
200
201 CFGFUN(mode_binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
202     Binding *new_binding = scalloc(sizeof(Binding));
203     new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
204     if (strcmp(bindtype, "bindsym") == 0) {
205         new_binding->symbol = sstrdup(key);
206     } else {
207         // TODO: strtol with proper error handling
208         new_binding->keycode = atoi(key);
209     }
210     new_binding->mods = modifiers_from_str(modifiers);
211     new_binding->command = sstrdup(command);
212     TAILQ_INSERT_TAIL(current_bindings, new_binding, bindings);
213 }
214
215 CFGFUN(enter_mode, const char *modename) {
216     if (strcasecmp(modename, "default") == 0) {
217         ELOG("You cannot use the name \"default\" for your mode\n");
218         exit(1);
219     }
220     DLOG("\t now in mode %s\n", modename);
221     struct Mode *mode = scalloc(sizeof(struct Mode));
222     mode->name = sstrdup(modename);
223     mode->bindings = scalloc(sizeof(struct bindings_head));
224     TAILQ_INIT(mode->bindings);
225     current_bindings = mode->bindings;
226     SLIST_INSERT_HEAD(&modes, mode, modes);
227 }
228
229 CFGFUN(exec, const char *exectype, const char *no_startup_id, const char *command) {
230         struct Autostart *new = smalloc(sizeof(struct Autostart));
231         new->command = sstrdup(command);
232         new->no_startup_id = (no_startup_id != NULL);
233         if (strcmp(exectype, "exec") == 0) {
234                 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
235         } else {
236                 TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
237         }
238 }
239
240 CFGFUN(for_window, const char *command) {
241     if (match_is_empty(current_match)) {
242         ELOG("Match is empty, ignoring this for_window statement\n");
243         return;
244     }
245     DLOG("\t should execute command %s for the criteria mentioned above\n", command);
246     Assignment *assignment = scalloc(sizeof(Assignment));
247     assignment->type = A_COMMAND;
248     match_copy(&(assignment->match), current_match);
249     assignment->dest.command = sstrdup(command);
250     TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
251 }
252
253 CFGFUN(floating_minimum_size, const long width, const long height) {
254     config.floating_minimum_width = width;
255     config.floating_minimum_height = height;
256 }
257
258 CFGFUN(floating_maximum_size, const long width, const long height) {
259     config.floating_maximum_width = width;
260     config.floating_maximum_height = height;
261 }
262
263 CFGFUN(floating_modifier, const char *modifiers) {
264     config.floating_modifier = modifiers_from_str(modifiers);
265 }
266
267 CFGFUN(default_orientation, const char *orientation) {
268     if (strcmp(orientation, "horizontal") == 0)
269         config.default_orientation = HORIZ;
270     else if (strcmp(orientation, "vertical") == 0)
271         config.default_orientation = VERT;
272     else config.default_orientation = NO_ORIENTATION;
273 }
274
275 CFGFUN(workspace_layout, const char *layout) {
276     if (strcmp(layout, "default") == 0)
277         config.default_layout = L_DEFAULT;
278     else if (strcmp(layout, "stacking") == 0 ||
279              strcmp(layout, "stacked") == 0)
280         config.default_layout = L_STACKED;
281     else config.default_layout = L_TABBED;
282 }
283
284 CFGFUN(new_window, const char *windowtype, const char *border, const long width) {
285     // FIXME: when using new_float *and* new_window with different border
286     // types, this breaks because default_border_width gets overwritten.
287
288     int border_style;
289     int border_width;
290
291     if (strcmp(border, "1pixel") == 0) {
292         border_style = BS_PIXEL;
293         border_width = 1;
294     } else if (strcmp(border, "none") == 0) {
295         border_style = BS_NONE;
296         border_width = 0;
297     } else if (strcmp(border, "pixel") == 0) {
298         border_style = BS_PIXEL;
299         border_width = width;
300     } else {
301         border_style = BS_NORMAL;
302         border_width = width;
303     }
304
305     if (strcmp(windowtype, "new_window") == 0) {
306         config.default_border = border_style;
307     } else {
308         config.default_floating_border = border_style;
309     }
310 }
311
312 CFGFUN(hide_edge_borders, const char *borders) {
313     if (strcmp(borders, "vertical") == 0)
314         config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
315     else if (strcmp(borders, "horizontal") == 0)
316         config.hide_edge_borders = ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
317     else if (strcmp(borders, "both") == 0)
318         config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE | ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
319     else if (strcmp(borders, "none") == 0)
320         config.hide_edge_borders = ADJ_NONE;
321     else if (eval_boolstr(borders))
322         config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
323     else config.hide_edge_borders = ADJ_NONE;
324 }
325
326 CFGFUN(focus_follows_mouse, const char *value) {
327     config.disable_focus_follows_mouse = !eval_boolstr(value);
328 }
329
330 CFGFUN(force_xinerama, const char *value) {
331     config.force_xinerama = eval_boolstr(value);
332 }
333
334 CFGFUN(force_focus_wrapping, const char *value) {
335     config.force_focus_wrapping = eval_boolstr(value);
336 }
337
338 CFGFUN(workspace_back_and_forth, const char *value) {
339     config.workspace_auto_back_and_forth = eval_boolstr(value);
340 }
341
342 CFGFUN(fake_outputs, const char *outputs) {
343     config.fake_outputs = sstrdup(outputs);
344 }
345
346 CFGFUN(force_display_urgency_hint, const long duration_ms) {
347     config.workspace_urgency_timer = duration_ms / 1000.0;
348 }
349
350 CFGFUN(workspace, const char *workspace, const char *output) {
351     DLOG("Assigning workspace \"%s\" to output \"%s\"\n", workspace, output);
352     /* Check for earlier assignments of the same workspace so that we
353      * don’t have assignments of a single workspace to different
354      * outputs */
355     struct Workspace_Assignment *assignment;
356     bool duplicate = false;
357     TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
358         if (strcasecmp(assignment->name, workspace) == 0) {
359             ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
360                  workspace);
361             assignment->output = sstrdup(output);
362             duplicate = true;
363         }
364     }
365     if (!duplicate) {
366         assignment = scalloc(sizeof(struct Workspace_Assignment));
367         assignment->name = sstrdup(workspace);
368         assignment->output = sstrdup(output);
369         TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
370     }
371 }
372
373 CFGFUN(ipc_socket, const char *path) {
374     config.ipc_socket_path = sstrdup(path);
375 }
376
377 CFGFUN(restart_state, const char *path) {
378     config.restart_state_path = sstrdup(path);
379 }
380
381 CFGFUN(popup_during_fullscreen, const char *value) {
382     config.popup_during_fullscreen =
383         (strcmp(value, "ignore") == 0 ? PDF_IGNORE : PDF_LEAVE_FULLSCREEN);
384 }
385
386 CFGFUN(color_single, const char *colorclass, const char *color) {
387     /* used for client.background only currently */
388     config.client.background = get_colorpixel(color);
389 }
390
391 CFGFUN(color, const char *colorclass, const char *border, const char *background, const char *text, const char *indicator) {
392 #define APPLY_COLORS(classname) \
393     do { \
394         if (strcmp(colorclass, "client." #classname) == 0) { \
395             config.client.classname.border = get_colorpixel(border); \
396             config.client.classname.background = get_colorpixel(background); \
397             config.client.classname.text = get_colorpixel(text); \
398             if (indicator != NULL) { \
399                 config.client. classname .indicator = get_colorpixel(indicator); \
400             } \
401         } \
402     } while (0)
403
404     APPLY_COLORS(focused_inactive);
405     APPLY_COLORS(focused);
406     APPLY_COLORS(unfocused);
407     APPLY_COLORS(urgent);
408
409 #undef APPLY_COLORS
410 }
411
412 CFGFUN(assign, const char *workspace) {
413     if (match_is_empty(current_match)) {
414         ELOG("Match is empty, ignoring this assignment\n");
415         return;
416     }
417     DLOG("new assignment, using above criteria, to workspace %s\n", workspace);
418     Assignment *assignment = scalloc(sizeof(Assignment));
419     match_copy(&(assignment->match), current_match);
420     assignment->type = A_TO_WORKSPACE;
421     assignment->dest.workspace = sstrdup(workspace);
422     TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
423 }
424
425 /*******************************************************************************
426  * Bar configuration (i3bar)
427  ******************************************************************************/
428
429 static Barconfig current_bar;
430
431 CFGFUN(bar_font, const char *font) {
432     FREE(current_bar.font);
433     current_bar.font = sstrdup(font);
434 }
435
436 CFGFUN(bar_mode, const char *mode) {
437     current_bar.mode = (strcmp(mode, "hide") == 0 ? M_HIDE : M_DOCK);
438 }
439
440 CFGFUN(bar_output, const char *output) {
441     int new_outputs = current_bar.num_outputs + 1;
442     current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
443     current_bar.outputs[current_bar.num_outputs] = sstrdup(output);
444     current_bar.num_outputs = new_outputs;
445 }
446
447 CFGFUN(bar_verbose, const char *verbose) {
448     current_bar.verbose = eval_boolstr(verbose);
449 }
450
451 CFGFUN(bar_modifier, const char *modifier) {
452     if (strcmp(modifier, "Mod1") == 0)
453         current_bar.modifier = M_MOD1;
454     else if (strcmp(modifier, "Mod2") == 0)
455         current_bar.modifier = M_MOD2;
456     else if (strcmp(modifier, "Mod3") == 0)
457         current_bar.modifier = M_MOD3;
458     else if (strcmp(modifier, "Mod4") == 0)
459         current_bar.modifier = M_MOD4;
460     else if (strcmp(modifier, "Mod5") == 0)
461         current_bar.modifier = M_MOD5;
462     else if (strcmp(modifier, "Control") == 0)
463         current_bar.modifier = M_CONTROL;
464     else if (strcmp(modifier, "Shift") == 0)
465         current_bar.modifier = M_SHIFT;
466 }
467
468 CFGFUN(bar_position, const char *position) {
469     current_bar.position = (strcmp(position, "top") == 0 ? P_TOP : P_BOTTOM);
470 }
471
472 CFGFUN(bar_i3bar_command, const char *i3bar_command) {
473     FREE(current_bar.i3bar_command);
474     current_bar.i3bar_command = sstrdup(i3bar_command);
475 }
476
477 CFGFUN(bar_color, const char *colorclass, const char *border, const char *background, const char *text) {
478 #define APPLY_COLORS(classname) \
479     do { \
480         if (strcmp(colorclass, #classname) == 0) { \
481             if (text != NULL) { \
482                 /* New syntax: border, background, text */ \
483                 current_bar.colors. classname ## _border = sstrdup(border); \
484                 current_bar.colors. classname ## _bg = sstrdup(background); \
485                 current_bar.colors. classname ## _text = sstrdup(text); \
486             } else { \
487                 /* Old syntax: text, background */ \
488                 current_bar.colors. classname ## _bg = sstrdup(background); \
489                 current_bar.colors. classname ## _text = sstrdup(border); \
490             } \
491         } \
492     } while (0)
493
494     APPLY_COLORS(focused_workspace);
495     APPLY_COLORS(active_workspace);
496     APPLY_COLORS(inactive_workspace);
497     APPLY_COLORS(urgent_workspace);
498
499 #undef APPLY_COLORS
500 }
501
502 CFGFUN(bar_socket_path, const char *socket_path) {
503     FREE(current_bar.socket_path);
504     current_bar.socket_path = sstrdup(socket_path);
505 }
506
507 CFGFUN(bar_tray_output, const char *output) {
508     FREE(current_bar.tray_output);
509     current_bar.tray_output = sstrdup(output);
510 }
511
512 CFGFUN(bar_color_single, const char *colorclass, const char *color) {
513     if (strcmp(colorclass, "background") == 0)
514         current_bar.colors.background = sstrdup(color);
515     else current_bar.colors.statusline = sstrdup(color);
516 }
517
518 CFGFUN(bar_status_command, const char *command) {
519     FREE(current_bar.status_command);
520     current_bar.status_command = sstrdup(command);
521 }
522
523 CFGFUN(bar_workspace_buttons, const char *value) {
524     current_bar.hide_workspace_buttons = !eval_boolstr(value);
525 }
526
527 CFGFUN(bar_finish) {
528     DLOG("\t new bar configuration finished, saving.\n");
529     /* Generate a unique ID for this bar */
530     current_bar.id = sstrdup("bar-XXXXXX");
531     /* This works similar to mktemp in that it replaces the last six X with
532      * random letters, but without the restriction that the given buffer
533      * has to contain a valid path name. */
534     char *x = current_bar.id + strlen("bar-");
535     while (*x != '\0') {
536         *(x++) = (rand() % 26) + 'a';
537     }
538
539     /* If no font was explicitly set, we use the i3 font as default */
540     if (!current_bar.font && font_pattern)
541         current_bar.font = sstrdup(font_pattern);
542
543     /* Copy the current (static) structure into a dynamically allocated
544      * one, then cleanup our static one. */
545     Barconfig *bar_config = scalloc(sizeof(Barconfig));
546     memcpy(bar_config, &current_bar, sizeof(Barconfig));
547     TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
548
549     memset(&current_bar, '\0', sizeof(Barconfig));
550 }