]> git.sur5r.net Git - i3/i3/blob - src/config.c
8e8a80bd6803826adf7dc52bba8643e8bb7fd040
[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 #define OPTION_COLORTRIPLE(opt, name) \
56         if (strcasecmp(key, opt) == 0) { \
57                 struct Colortriple buffer; \
58                 memset(&buffer, 0, sizeof(struct Colortriple)); \
59                 buffer.border[0] = buffer.background[0] = buffer.text[0] = '#'; \
60                 if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
61                     buffer.border + 1, buffer.background + 1, buffer.text + 1) != 3 || \
62                     strlen(buffer.border) != 7 || \
63                     strlen(buffer.background) != 7 || \
64                     strlen(buffer.text) != 7) \
65                         die("invalid color code line: %s\n", value); \
66                 memcpy(&config.name, &buffer, sizeof(struct Colortriple)); \
67                 continue; \
68         }
69
70         /* Clear the old config or initialize the data structure */
71         memset(&config, 0, sizeof(config));
72
73         /* Initialize default colors */
74         strcpy(config.client.focused.border, "#4c7899");
75         strcpy(config.client.focused.background, "#285577");
76         strcpy(config.client.focused.text, "#ffffff");
77
78         strcpy(config.client.focused_inactive.border, "#4c7899");
79         strcpy(config.client.focused_inactive.background, "#555555");
80         strcpy(config.client.focused_inactive.text, "#ffffff");
81
82         strcpy(config.client.unfocused.border, "#333333");
83         strcpy(config.client.unfocused.background, "#222222");
84         strcpy(config.client.unfocused.text, "#888888");
85
86         strcpy(config.bar.focused.border, "#4c7899");
87         strcpy(config.bar.focused.background, "#285577");
88         strcpy(config.bar.focused.text, "#ffffff");
89
90         strcpy(config.bar.unfocused.border, "#333333");
91         strcpy(config.bar.unfocused.background, "#222222");
92         strcpy(config.bar.unfocused.text, "#888888");
93
94         FILE *handle;
95         if (override_configpath != NULL) {
96                 if ((handle = fopen(override_configpath, "r")) == NULL)
97                         die("Could not open configfile \"%s\".\n", override_configpath);
98         } else {
99                 /* We first check for ~/.i3/config, then for /etc/i3/config */
100                 char *globbed = glob_path("~/.i3/config");
101                 if ((handle = fopen(globbed, "r")) == NULL)
102                         if ((handle = fopen("/etc/i3/config", "r")) == NULL)
103                                 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
104                 free(globbed);
105         }
106         char key[512], value[512], buffer[1026];
107
108         while (!feof(handle)) {
109                 if (fgets(buffer, 1024, handle) == NULL) {
110                         /* fgets returns NULL on EOF and on error, so see which one it is. */
111                         if (feof(handle))
112                                 break;
113                         die("Could not read configuration file\n");
114                 }
115
116                 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
117                 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
118                     key[0] == '#' || strlen(key) < 3)
119                         continue;
120
121                 OPTION_STRING(terminal);
122                 OPTION_STRING(font);
123
124                 /* Colors */
125                 OPTION_COLORTRIPLE("client.focused", client.focused);
126                 OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
127                 OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
128                 OPTION_COLORTRIPLE("bar.focused", bar.focused);
129                 OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
130
131                 /* exec-lines (autostart) */
132                 if (strcasecmp(key, "exec") == 0) {
133                         struct Autostart *new = smalloc(sizeof(struct Autostart));
134                         new->command = sstrdup(value);
135                         TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
136                         continue;
137                 }
138
139                 /* key bindings */
140                 if (strcasecmp(key, "bind") == 0) {
141                         #define CHECK_MODIFIER(name) \
142                                 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
143                                         modifiers |= BIND_##name; \
144                                         walk += strlen(#name) + 1; \
145                                         continue; \
146                                 }
147                         char *walk = value, *rest;
148                         uint32_t modifiers = 0;
149
150                         while (*walk != '\0') {
151                                 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
152                                 CHECK_MODIFIER(SHIFT);
153                                 CHECK_MODIFIER(CONTROL);
154                                 CHECK_MODIFIER(MODE_SWITCH);
155                                 CHECK_MODIFIER(MOD1);
156                                 CHECK_MODIFIER(MOD2);
157                                 CHECK_MODIFIER(MOD3);
158                                 CHECK_MODIFIER(MOD4);
159                                 CHECK_MODIFIER(MOD5);
160
161                                 /* No modifier found? Then we’re done with this step */
162                                 break;
163                         }
164
165                         /* Now check for the keycode */
166                         int keycode = strtol(walk, &rest, 10);
167                         if (!rest || *rest != ' ')
168                                 die("Invalid binding\n");
169                         rest++;
170                         LOG("keycode = %d, modifiers = %d, command = *%s*\n", keycode, modifiers, rest);
171                         Binding *new = smalloc(sizeof(Binding));
172                         new->keycode = keycode;
173                         new->mods = modifiers;
174                         new->command = sstrdup(rest);
175                         TAILQ_INSERT_TAIL(&bindings, new, bindings);
176                         continue;
177                 }
178
179                 /* assign window class[/window title] → workspace */
180                 if (strcasecmp(key, "assign") == 0) {
181                         LOG("assign: \"%s\"\n", value);
182                         char *class_title = sstrdup(value);
183                         char *target;
184
185                         /* If the window class/title is quoted we skip quotes */
186                         if (class_title[0] == '"') {
187                                 class_title++;
188                                 char *end = strchr(class_title, '"');
189                                 if (end == NULL)
190                                         die("Malformed assignment, couldn't find terminating quote\n");
191                                 *end = '\0';
192                         } else {
193                                 /* If it is not quoted, we terminate it at the first space */
194                                 char *end = strchr(class_title, ' ');
195                                 if (end == NULL)
196                                         die("Malformed assignment, couldn't find terminating space\n");
197                                 *end = '\0';
198                         }
199
200                         /* The target is the last argument separated by a space */
201                         if ((target = strrchr(value, ' ')) == NULL)
202                                 die("Malformed assignment, couldn't find target\n");
203                         target++;
204
205                         if (atoi(target) < 1 || atoi(target) > 10)
206                                 die("Malformed assignment, invalid workspace number\n");
207
208                         LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
209
210                         struct Assignment *new = smalloc(sizeof(struct Assignment));
211                         new->windowclass_title = class_title;
212                         new->workspace = atoi(target);
213                         TAILQ_INSERT_TAIL(&assignments, new, assignments);
214                         continue;
215                 }
216
217                 die("Unknown configfile option: %s\n", key);
218         }
219         fclose(handle);
220
221         REQUIRED_OPTION(terminal);
222         REQUIRED_OPTION(font);
223  
224         return;
225 }