]> git.sur5r.net Git - i3/i3/blob - src/config.c
930b09c5a7adb088ec808644d5188e97b411c2a5
[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  * src/config.c: Contains all functions handling the configuration file (calling
11  * the parser (src/cfgparse.y) with the correct path, switching key bindings
12  * mode).
13  *
14  */
15 #include <stdio.h>
16 #include <string.h>
17 #include <sys/stat.h>
18 #include <stdlib.h>
19 #include <glob.h>
20
21 /* We need Xlib for XStringToKeysym */
22 #include <X11/Xlib.h>
23
24 #include <xcb/xcb_keysyms.h>
25
26 #include "i3.h"
27 #include "util.h"
28 #include "config.h"
29 #include "xcb.h"
30 #include "table.h"
31 #include "workspace.h"
32 #include "log.h"
33
34 Config config;
35 struct modes_head modes;
36
37 /*
38  * This function resolves ~ in pathnames.
39  *
40  */
41 static char *glob_path(const char *path) {
42         static glob_t globbuf;
43         if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
44                 die("glob() failed");
45         char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
46         globfree(&globbuf);
47         return result;
48 }
49
50 /**
51  * Ungrabs all keys, to be called before re-grabbing the keys because of a
52  * mapping_notify event or a configuration file reload
53  *
54  */
55 void ungrab_all_keys(xcb_connection_t *conn) {
56         DLOG("Ungrabbing all keys\n");
57         xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
58 }
59
60 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
61         DLOG("Grabbing %d\n", keycode);
62         if ((bind->mods & BIND_MODE_SWITCH) != 0)
63                 xcb_grab_key(conn, 0, root, 0, keycode,
64                         XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
65         else {
66                 /* Grab the key in all combinations */
67                 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, keycode, \
68                                                         XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
69                 GRAB_KEY(bind->mods);
70                 GRAB_KEY(bind->mods | xcb_numlock_mask);
71                 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
72         }
73 }
74
75 /*
76  * Grab the bound keys (tell X to send us keypress events for those keycodes)
77  *
78  */
79 void grab_all_keys(xcb_connection_t *conn) {
80         Binding *bind;
81         TAILQ_FOREACH(bind, bindings, bindings) {
82                 /* The easy case: the user specified a keycode directly. */
83                 if (bind->keycode > 0) {
84                         grab_keycode_for_binding(conn, bind, bind->keycode);
85                         continue;
86                 }
87
88                 /* We need to translate the symbol to a keycode */
89                 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
90                 if (keysym == NoSymbol) {
91                         ELOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
92                         continue;
93                 }
94
95 #ifdef OLD_XCB_KEYSYMS_API
96                 bind->number_keycodes = 1;
97                 xcb_keycode_t code = xcb_key_symbols_get_keycode(keysyms, keysym);
98                 DLOG("Translated symbol \"%s\" to 1 keycode (%d)\n", bind->symbol, code);
99                 grab_keycode_for_binding(conn, bind, code);
100                 bind->translated_to = smalloc(sizeof(xcb_keycode_t));
101                 memcpy(bind->translated_to, &code, sizeof(xcb_keycode_t));
102 #else
103                 uint32_t last_keycode = 0;
104                 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
105                 if (keycodes == NULL) {
106                         DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
107                         continue;
108                 }
109
110                 bind->number_keycodes = 0;
111
112                 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
113                         /* We hope duplicate keycodes will be returned in order
114                          * and skip them */
115                         if (last_keycode == *walk)
116                                 continue;
117                         grab_keycode_for_binding(conn, bind, *walk);
118                         last_keycode = *walk;
119                         bind->number_keycodes++;
120                 }
121                 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
122                 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
123                 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
124                 free(keycodes);
125 #endif
126         }
127 }
128
129 /*
130  * Switches the key bindings to the given mode, if the mode exists
131  *
132  */
133 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
134         struct Mode *mode;
135
136         LOG("Switching to mode %s\n", new_mode);
137
138         SLIST_FOREACH(mode, &modes, modes) {
139                 if (strcasecmp(mode->name, new_mode) != 0)
140                         continue;
141
142                 ungrab_all_keys(conn);
143                 bindings = mode->bindings;
144                 grab_all_keys(conn);
145                 return;
146         }
147
148         ELOG("ERROR: Mode not found\n");
149 }
150
151 /*
152  * Finds the configuration file to use (either the one specified by
153  * override_configpath), the user’s one or the system default) and calls
154  * parse_file().
155  *
156  */
157 static void parse_configuration(const char *override_configpath) {
158         if (override_configpath != NULL) {
159                 parse_file(override_configpath);
160                 return;
161         }
162
163         FILE *handle;
164         char *globbed = glob_path("~/.i3/config");
165         if ((handle = fopen(globbed, "r")) == NULL) {
166                 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
167                         die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
168
169                 parse_file("/etc/i3/config");
170                 fclose(handle);
171                 return;
172         }
173
174         parse_file(globbed);
175         fclose(handle);
176 }
177
178 /*
179  * (Re-)loads the configuration file (sets useful defaults before).
180  *
181  */
182 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
183         if (reload) {
184                 /* First ungrab the keys */
185                 ungrab_all_keys(conn);
186
187                 struct Mode *mode;
188                 Binding *bind;
189                 while (!SLIST_EMPTY(&modes)) {
190                         mode = SLIST_FIRST(&modes);
191                         FREE(mode->name);
192
193                         /* Clear the old binding list */
194                         bindings = mode->bindings;
195                         while (!TAILQ_EMPTY(bindings)) {
196                                 bind = TAILQ_FIRST(bindings);
197                                 TAILQ_REMOVE(bindings, bind, bindings);
198                                 FREE(bind->translated_to);
199                                 FREE(bind->command);
200                                 FREE(bind);
201                         }
202                         FREE(bindings);
203                         SLIST_REMOVE(&modes, mode, Mode, modes);
204                 }
205
206                 struct Assignment *assign;
207                 while (!TAILQ_EMPTY(&assignments)) {
208                         assign = TAILQ_FIRST(&assignments);
209                         FREE(assign->windowclass_title);
210                         TAILQ_REMOVE(&assignments, assign, assignments);
211                         FREE(assign);
212                 }
213         }
214
215         SLIST_INIT(&modes);
216
217         struct Mode *default_mode = scalloc(sizeof(struct Mode));
218         default_mode->name = sstrdup("default");
219         default_mode->bindings = scalloc(sizeof(struct bindings_head));
220         TAILQ_INIT(default_mode->bindings);
221         SLIST_INSERT_HEAD(&modes, default_mode, modes);
222
223         bindings = default_mode->bindings;
224
225 #define REQUIRED_OPTION(name) \
226         if (config.name == NULL) \
227                 die("You did not specify required configuration option " #name "\n");
228
229         /* Clear the old config or initialize the data structure */
230         memset(&config, 0, sizeof(config));
231
232         /* Initialize default colors */
233 #define INIT_COLOR(x, cborder, cbackground, ctext) \
234         do { \
235                 x.border = get_colorpixel(conn, cborder); \
236                 x.background = get_colorpixel(conn, cbackground); \
237                 x.text = get_colorpixel(conn, ctext); \
238         } while (0)
239
240         INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
241         INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
242         INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
243         INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
244         INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
245         INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
246         INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
247
248         parse_configuration(override_configpath);
249
250         if (reload)
251                 grab_all_keys(conn);
252
253         REQUIRED_OPTION(font);
254
255         /* Set an empty name for every workspace which got no name */
256         Workspace *ws;
257         TAILQ_FOREACH(ws, workspaces, workspaces) {
258                 if (ws->name != NULL) {
259                         /* If the font was not specified when the workspace name
260                          * was loaded, we need to predict the text width now */
261                         if (ws->text_width == 0)
262                                 ws->text_width = predict_text_width(global_conn,
263                                                 config.font, ws->name, ws->name_len);
264                         continue;
265                 }
266
267                 workspace_set_name(ws, NULL);
268         }
269 }