]> git.sur5r.net Git - i3/i3/blob - src/config.c
562b2e928bb3a02afc72dd1b7be42f56a233f78a
[i3/i3] / src / config.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #include <stdio.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <stdlib.h>
15 #include <glob.h>
16
17 #include "i3.h"
18 #include "util.h"
19 #include "config.h"
20
21 Config config;
22
23 /*
24  * This function resolves ~ in pathnames.
25  *
26  */
27 static char *glob_path(const char *path) {
28         static glob_t globbuf;
29         if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
30                 die("glob() failed");
31         char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
32         globfree(&globbuf);
33         return result;
34 }
35
36
37 /*
38  * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
39  *
40  * If you specify override_configpath, only this path is used to look for a
41  * configuration file.
42  *
43  */
44 void load_configuration(const char *override_configpath) {
45 #define OPTION_STRING(name) \
46         if (strcasecmp(key, #name) == 0) { \
47                 config.name = sstrdup(value); \
48                 continue; \
49         }
50
51 #define REQUIRED_OPTION(name) \
52         if (config.name == NULL) \
53                 die("You did not specify required configuration option " #name "\n");
54
55         /* Clear the old config or initialize the data structure */
56         memset(&config, 0, sizeof(config));
57
58         FILE *handle;
59         if (override_configpath != NULL) {
60                 if ((handle = fopen(override_configpath, "r")) == NULL)
61                         die("Could not open configfile \"%s\".\n", override_configpath);
62         } else {
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);
68                 free(globbed);
69         }
70         char key[512], value[512], buffer[1026];
71
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. */
75                         if (feof(handle))
76                                 break;
77                         die("Could not read configuration file\n");
78                 }
79
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)
83                         continue;
84
85                 OPTION_STRING(terminal);
86                 OPTION_STRING(font);
87
88                 /* exec-lines (autostart) */
89                 if (strcasecmp(key, "exec") == 0) {
90                         struct Autostart *new = smalloc(sizeof(struct Autostart));
91                         new->command = sstrdup(value);
92                         TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
93                         continue;
94                 }
95
96                 /* key bindings */
97                 if (strcasecmp(key, "bind") == 0) {
98                         #define CHECK_MODIFIER(name) \
99                                 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
100                                         modifiers |= BIND_##name; \
101                                         walk += strlen(#name) + 1; \
102                                         continue; \
103                                 }
104                         char *walk = value, *rest;
105                         uint32_t modifiers = 0;
106
107                         while (*walk != '\0') {
108                                 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
109                                 CHECK_MODIFIER(SHIFT);
110                                 CHECK_MODIFIER(CONTROL);
111                                 CHECK_MODIFIER(MODE_SWITCH);
112                                 CHECK_MODIFIER(MOD1);
113                                 CHECK_MODIFIER(MOD2);
114                                 CHECK_MODIFIER(MOD3);
115                                 CHECK_MODIFIER(MOD4);
116                                 CHECK_MODIFIER(MOD5);
117
118                                 /* No modifier found? Then we’re done with this step */
119                                 break;
120                         }
121
122                         /* Now check for the keycode */
123                         int keycode = strtol(walk, &rest, 10);
124                         if (!rest || *rest != ' ')
125                                 die("Invalid binding\n");
126                         rest++;
127                         LOG("keycode = %d, modifiers = %d, command = *%s*\n", keycode, modifiers, rest);
128                         Binding *new = smalloc(sizeof(Binding));
129                         new->keycode = keycode;
130                         new->mods = modifiers;
131                         new->command = sstrdup(rest);
132                         TAILQ_INSERT_TAIL(&bindings, new, bindings);
133                         continue;
134                 }
135
136                 /* assign window class[/window title] → workspace */
137                 if (strcasecmp(key, "assign") == 0) {
138                         LOG("assign: \"%s\"\n", value);
139                         char *class_title = sstrdup(value);
140                         char *target;
141
142                         /* If the window class/title is quoted we skip quotes */
143                         if (class_title[0] == '"') {
144                                 class_title++;
145                                 char *end = strchr(class_title, '"');
146                                 if (end == NULL)
147                                         die("Malformatted assignment, couldn't find finishing quote\n");
148                                 *end = '\0';
149                         } else {
150                                 /* If it is not quoted, we terminate it at the first space */
151                                 char *end = strchr(class_title, ' ');
152                                 if (end == NULL)
153                                         die("Malformed assignment, couldn't find terminating space\n");
154                                 *end = '\0';
155                         }
156
157                         /* The target is the last argument separated by a space */
158                         if ((target = strrchr(value, ' ')) == NULL)
159                                 die("Malformed assignment, couldn't find target\n");
160                         target++;
161
162                         if (atoi(target) < 1 || atoi(target) > 10)
163                                 die("Malformed assignment, invalid workspace number\n");
164
165                         LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
166
167                         struct Assignment *new = smalloc(sizeof(struct Assignment));
168                         new->windowclass_title = class_title;
169                         new->workspace = atoi(target);
170                         TAILQ_INSERT_TAIL(&assignments, new, assignments);
171                         continue;
172                 }
173
174                 fprintf(stderr, "Unknown configfile option: %s\n", key);
175                 exit(1);
176         }
177         fclose(handle);
178
179         REQUIRED_OPTION(terminal);
180         REQUIRED_OPTION(font);
181
182         return;
183 }