i3
config_directives.c
Go to the documentation of this file.
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 
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);
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 */
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);
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) {
246  } else {
248  }
249 }
250 
251 CFGFUN(for_window, const char *command) {
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);
262 }
263 
264 CFGFUN(floating_minimum_size, const long width, const long height) {
267 }
268 
269 CFGFUN(floating_maximum_size, const long width, const long height) {
272 }
273 
274 CFGFUN(floating_modifier, const char *modifiers) {
276 }
277 
278 CFGFUN(default_orientation, const char *orientation) {
279  if (strcmp(orientation, "horizontal") == 0)
281  else if (strcmp(orientation, "vertical") == 0)
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)
328  else if (strcmp(borders, "horizontal") == 0)
330  else if (strcmp(borders, "both") == 0)
332  else if (strcmp(borders, "none") == 0)
334  else if (eval_boolstr(borders))
337 }
338 
339 CFGFUN(focus_follows_mouse, const char *value) {
341 }
342 
343 CFGFUN(force_xinerama, const char *value) {
345 }
346 
347 CFGFUN(force_focus_wrapping, const char *value) {
349 }
350 
351 CFGFUN(workspace_back_and_forth, const char *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;
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);
383  }
384 }
385 
386 CFGFUN(ipc_socket, const char *path) {
388 }
389 
390 CFGFUN(restart_state, const char *path) {
392 }
393 
394 CFGFUN(popup_during_fullscreen, const char *value) {
396  (strcmp(value, "ignore") == 0 ? PDF_IGNORE : PDF_LEAVE_FULLSCREEN);
397 }
398 
399 CFGFUN(color_single, const char *colorclass, const char *color) {
400  /* used for client.background only currently */
402 }
403 
404 CFGFUN(color, const char *colorclass, const char *border, const char *background, const char *text, const char *indicator) {
405 #define APPLY_COLORS(classname) \
406  do { \
407  if (strcmp(colorclass, "client." #classname) == 0) { \
408  config.client.classname.border = get_colorpixel(border); \
409  config.client.classname.background = get_colorpixel(background); \
410  config.client.classname.text = get_colorpixel(text); \
411  if (indicator != NULL) { \
412  config.client. classname .indicator = get_colorpixel(indicator); \
413  } \
414  } \
415  } while (0)
416 
417  APPLY_COLORS(focused_inactive);
419  APPLY_COLORS(unfocused);
420  APPLY_COLORS(urgent);
421 
422 #undef APPLY_COLORS
423 }
424 
425 CFGFUN(assign, const char *workspace) {
427  ELOG("Match is empty, ignoring this assignment\n");
428  return;
429  }
430  DLOG("new assignment, using above criteria, to workspace %s\n", workspace);
431  Assignment *assignment = scalloc(sizeof(Assignment));
432  match_copy(&(assignment->match), current_match);
433  assignment->type = A_TO_WORKSPACE;
434  assignment->dest.workspace = sstrdup(workspace);
436 }
437 
438 /*******************************************************************************
439  * Bar configuration (i3bar)
440  ******************************************************************************/
441 
443 
444 CFGFUN(bar_font, const char *font) {
445  FREE(current_bar.font);
446  current_bar.font = sstrdup(font);
447 }
448 
449 CFGFUN(bar_mode, const char *mode) {
450  current_bar.mode = (strcmp(mode, "hide") == 0 ? M_HIDE : M_DOCK);
451 }
452 
453 CFGFUN(bar_output, const char *output) {
454  int new_outputs = current_bar.num_outputs + 1;
455  current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
456  current_bar.outputs[current_bar.num_outputs] = sstrdup(output);
457  current_bar.num_outputs = new_outputs;
458 }
459 
460 CFGFUN(bar_verbose, const char *verbose) {
461  current_bar.verbose = eval_boolstr(verbose);
462 }
463 
464 CFGFUN(bar_modifier, const char *modifier) {
465  if (strcmp(modifier, "Mod1") == 0)
466  current_bar.modifier = M_MOD1;
467  else if (strcmp(modifier, "Mod2") == 0)
468  current_bar.modifier = M_MOD2;
469  else if (strcmp(modifier, "Mod3") == 0)
470  current_bar.modifier = M_MOD3;
471  else if (strcmp(modifier, "Mod4") == 0)
472  current_bar.modifier = M_MOD4;
473  else if (strcmp(modifier, "Mod5") == 0)
474  current_bar.modifier = M_MOD5;
475  else if (strcmp(modifier, "Control") == 0 ||
476  strcmp(modifier, "Ctrl") == 0)
477  current_bar.modifier = M_CONTROL;
478  else if (strcmp(modifier, "Shift") == 0)
479  current_bar.modifier = M_SHIFT;
480 }
481 
482 CFGFUN(bar_position, const char *position) {
483  current_bar.position = (strcmp(position, "top") == 0 ? P_TOP : P_BOTTOM);
484 }
485 
486 CFGFUN(bar_i3bar_command, const char *i3bar_command) {
487  FREE(current_bar.i3bar_command);
488  current_bar.i3bar_command = sstrdup(i3bar_command);
489 }
490 
491 CFGFUN(bar_color, const char *colorclass, const char *border, const char *background, const char *text) {
492 #define APPLY_COLORS(classname) \
493  do { \
494  if (strcmp(colorclass, #classname) == 0) { \
495  if (text != NULL) { \
496  /* New syntax: border, background, text */ \
497  current_bar.colors. classname ## _border = sstrdup(border); \
498  current_bar.colors. classname ## _bg = sstrdup(background); \
499  current_bar.colors. classname ## _text = sstrdup(text); \
500  } else { \
501  /* Old syntax: text, background */ \
502  current_bar.colors. classname ## _bg = sstrdup(background); \
503  current_bar.colors. classname ## _text = sstrdup(border); \
504  } \
505  } \
506  } while (0)
507 
508  APPLY_COLORS(focused_workspace);
509  APPLY_COLORS(active_workspace);
510  APPLY_COLORS(inactive_workspace);
511  APPLY_COLORS(urgent_workspace);
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_color_single, const char *colorclass, const char *color) {
527  if (strcmp(colorclass, "background") == 0)
528  current_bar.colors.background = sstrdup(color);
529  else current_bar.colors.statusline = sstrdup(color);
530 }
531 
532 CFGFUN(bar_status_command, const char *command) {
533  FREE(current_bar.status_command);
534  current_bar.status_command = sstrdup(command);
535 }
536 
537 CFGFUN(bar_workspace_buttons, const char *value) {
538  current_bar.hide_workspace_buttons = !eval_boolstr(value);
539 }
540 
541 CFGFUN(bar_finish) {
542  DLOG("\t new bar configuration finished, saving.\n");
543  /* Generate a unique ID for this bar */
544  current_bar.id = sstrdup("bar-XXXXXX");
545  /* This works similar to mktemp in that it replaces the last six X with
546  * random letters, but without the restriction that the given buffer
547  * has to contain a valid path name. */
548  char *x = current_bar.id + strlen("bar-");
549  while (*x != '\0') {
550  *(x++) = (rand() % 26) + 'a';
551  }
552 
553  /* If no font was explicitly set, we use the i3 font as default */
554  if (!current_bar.font && font_pattern)
555  current_bar.font = sstrdup(font_pattern);
556 
557  /* Copy the current (static) structure into a dynamically allocated
558  * one, then cleanup our static one. */
559  Barconfig *bar_config = scalloc(sizeof(Barconfig));
560  memcpy(bar_config, &current_bar, sizeof(Barconfig));
561  TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
562 
563  memset(&current_bar, '\0', sizeof(Barconfig));
564 }