]> git.sur5r.net Git - i3/i3/blob - src/config.c
Add missing files to make dist, delete old RFC file
[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                 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; \
93                                         continue; \
94                                 }
95                         char *walk = value, *rest;
96                         uint32_t modifiers = 0;
97
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);
108
109                                 /* No modifier found? Then we’re done with this step */
110                                 break;
111                         }
112
113                         /* Now check for the keycode */
114                         int keycode = strtol(walk, &rest, 10);
115                         if (!rest || *rest != ' ')
116                                 die("Invalid binding\n");
117                         rest++;
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);
124                         continue;
125                 }
126
127                 fprintf(stderr, "Unknown configfile option: %s\n", key);
128                 exit(1);
129         }
130         fclose(handle);
131
132         REQUIRED_OPTION(terminal);
133         REQUIRED_OPTION(font);
134
135         return;
136 }