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