4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
24 * This function resolves ~ in pathnames.
27 static char *glob_path(const char *path) {
28 static glob_t globbuf;
29 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
31 char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
38 * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
40 * If you specify override_configpath, only this path is used to look for a
44 void load_configuration(const char *override_configpath) {
45 #define OPTION_STRING(name) \
46 if (strcasecmp(key, #name) == 0) { \
47 config.name = sstrdup(value); \
51 #define REQUIRED_OPTION(name) \
52 if (config.name == NULL) \
53 die("You did not specify required configuration option " #name "\n");
55 /* Clear the old config or initialize the data structure */
56 memset(&config, 0, sizeof(config));
59 if (override_configpath != NULL) {
60 if ((handle = fopen(override_configpath, "r")) == NULL)
61 die("Could not open configfile \"%s\".\n", override_configpath);
63 /* We first check for ~/.i3/config, then for /etc/i3/config */
64 char *globbed = glob_path("~/.i3/config");
65 if ((handle = fopen(globbed, "r")) == NULL)
66 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
67 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
70 char key[512], value[512], buffer[1026];
72 while (!feof(handle)) {
73 if (fgets(buffer, 1024, handle) == NULL) {
74 /* fgets returns NULL on EOF and on error, so see which one it is. */
77 die("Could not read configuration file\n");
80 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
81 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
82 key[0] == '#' || strlen(key) < 3)
85 OPTION_STRING(terminal);
88 if (strcasecmp(key, "bind") == 0) {
89 #define CHECK_MODIFIER(name) \
90 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
91 modifiers |= BIND_##name; \
92 walk += strlen(#name) + 1; \
95 char *walk = value, *rest;
96 uint32_t modifiers = 0;
98 while (*walk != '\0') {
99 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
100 CHECK_MODIFIER(SHIFT);
101 CHECK_MODIFIER(CONTROL);
102 CHECK_MODIFIER(MODE_SWITCH);
103 CHECK_MODIFIER(MOD1);
104 CHECK_MODIFIER(MOD2);
105 CHECK_MODIFIER(MOD3);
106 CHECK_MODIFIER(MOD4);
107 CHECK_MODIFIER(MOD5);
109 /* No modifier found? Then we’re done with this step */
113 /* Now check for the keycode */
114 int keycode = strtol(walk, &rest, 10);
115 if (!rest || *rest != ' ')
116 die("Invalid binding\n");
118 LOG("keycode = %d, modifiers = %d, command = *%s*\n", keycode, modifiers, rest);
119 Binding *new = smalloc(sizeof(Binding));
120 new->keycode = keycode;
121 new->mods = modifiers;
122 new->command = sstrdup(rest);
123 TAILQ_INSERT_TAIL(&bindings, new, bindings);
127 fprintf(stderr, "Unknown configfile option: %s\n", key);
132 REQUIRED_OPTION(terminal);
133 REQUIRED_OPTION(font);