]> git.sur5r.net Git - i3/i3/blob - src/config.c
c4d940aeb8ae86cd5e119133a7416e6172179d95
[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_COLOR(opt, name) \
56         if (strcasecmp(key, opt) == 0) { \
57                 config.name = sstrdup(value); \
58                 continue; \
59         }
60
61 #define VERIFY_COLOR(name, def) \
62         if (config.name == NULL) { \
63                 config.name = (char *)malloc(7); \
64                 memset(config.name, 0, 7); \
65         } \
66         if ((strlen(config.name) != 7) || (config.name[0] != '#')) { \
67                 strncpy(config.name, def, 7); \
68         }
69
70         /* Clear the old config or initialize the data structure */
71         memset(&config, 0, sizeof(config));
72
73         FILE *handle;
74         if (override_configpath != NULL) {
75                 if ((handle = fopen(override_configpath, "r")) == NULL)
76                         die("Could not open configfile \"%s\".\n", override_configpath);
77         } else {
78                 /* We first check for ~/.i3/config, then for /etc/i3/config */
79                 char *globbed = glob_path("~/.i3/config");
80                 if ((handle = fopen(globbed, "r")) == NULL)
81                         if ((handle = fopen("/etc/i3/config", "r")) == NULL)
82                                 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
83                 free(globbed);
84         }
85         char key[512], value[512], buffer[1026];
86
87         while (!feof(handle)) {
88                 if (fgets(buffer, 1024, handle) == NULL) {
89                         /* fgets returns NULL on EOF and on error, so see which one it is. */
90                         if (feof(handle))
91                                 break;
92                         die("Could not read configuration file\n");
93                 }
94
95                 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
96                 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
97                     key[0] == '#' || strlen(key) < 3)
98                         continue;
99
100                 OPTION_STRING(terminal);
101                 OPTION_STRING(font);
102
103                 /* Colors */
104                 OPTION_COLOR("client.focused.background.active",
105                                 client_focused_background_active);
106                 OPTION_COLOR("client.focused.background.inactive",
107                                 client_focused_background_inactive);
108                 OPTION_COLOR("client.focused.text", 
109                                 client_focused_text);
110                 OPTION_COLOR("client.focused.border", 
111                                 client_focused_border);
112                 OPTION_COLOR("client.unfocused.background",
113                                 client_unfocused_background);
114                 OPTION_COLOR("client.unfocused.text",
115                                 client_unfocused_text);
116                 OPTION_COLOR("client.unfocused.border",
117                                 client_unfocused_border);
118                 OPTION_COLOR("bar.focused.background",
119                                 bar_focused_background);
120                 OPTION_COLOR("bar.focused.text",
121                                 bar_focused_text);
122                 OPTION_COLOR("bar.focused.border",
123                                 bar_focused_border);
124                 OPTION_COLOR("bar.unfocused.background",
125                                 bar_unfocused_background);
126                 OPTION_COLOR("bar.unfocused.text",
127                                 bar_unfocused_text);
128                 OPTION_COLOR("bar.unfocused.border",
129                                 bar_unfocused_border);
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                 fprintf(stderr, "Unknown configfile option: %s\n", key);
218                 exit(1);
219         }
220         fclose(handle);
221
222         VERIFY_COLOR(client_focused_background_active, "#285577");
223         VERIFY_COLOR(client_focused_background_inactive, "#555555");
224         VERIFY_COLOR(client_focused_text, "#ffffff");
225         VERIFY_COLOR(client_focused_border, "#4c7899");
226         VERIFY_COLOR(client_unfocused_background,"#222222");
227         VERIFY_COLOR(client_unfocused_text, "#888888");
228         VERIFY_COLOR(client_unfocused_border, "#333333");
229         VERIFY_COLOR(bar_focused_background, "#285577");
230         VERIFY_COLOR(bar_focused_text, "#ffffff");
231         VERIFY_COLOR(bar_focused_border, "#4c7899");
232         VERIFY_COLOR(bar_unfocused_background, "#222222");
233         VERIFY_COLOR(bar_unfocused_text, "#888888");
234         VERIFY_COLOR(bar_unfocused_border, "#333333");
235
236         REQUIRED_OPTION(terminal);
237         REQUIRED_OPTION(font);
238  
239         return;
240 }