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