i3
src/config.c
Go to the documentation of this file.
00001 /*
00002  * vim:ts=8:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  *
00006  * © 2009-2010 Michael Stapelberg and contributors
00007  *
00008  * See file LICENSE for license information.
00009  *
00010  * src/config.c: Contains all functions handling the configuration file (calling
00011  * the parser (src/cfgparse.y) with the correct path, switching key bindings
00012  * mode).
00013  *
00014  */
00015 #include <stdio.h>
00016 #include <string.h>
00017 #include <sys/stat.h>
00018 #include <sys/types.h>
00019 #include <stdlib.h>
00020 #include <glob.h>
00021 #include <wordexp.h>
00022 #include <unistd.h>
00023 
00024 /* We need Xlib for XStringToKeysym */
00025 #include <X11/Xlib.h>
00026 
00027 #include <xcb/xcb_keysyms.h>
00028 
00029 #include "i3.h"
00030 #include "util.h"
00031 #include "config.h"
00032 #include "xcb.h"
00033 #include "table.h"
00034 #include "workspace.h"
00035 #include "log.h"
00036 
00037 Config config;
00038 struct modes_head modes;
00039 
00040 /*
00041  * This function resolves ~ in pathnames.
00042  *
00043  */
00044 char *glob_path(const char *path) {
00045         static glob_t globbuf;
00046         if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
00047                 die("glob() failed");
00048         char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
00049         globfree(&globbuf);
00050 
00051         /* If the file does not exist yet, we still may need to resolve tilde,
00052          * so call wordexp */
00053         if (strcmp(result, path) == 0) {
00054                 wordexp_t we;
00055                 wordexp(path, &we, WRDE_NOCMD);
00056                 if (we.we_wordc > 0) {
00057                         free(result);
00058                         result = sstrdup(we.we_wordv[0]);
00059                 }
00060                 wordfree(&we);
00061         }
00062 
00063         return result;
00064 }
00065 
00066 /*
00067  * Checks if the given path exists by calling stat().
00068  *
00069  */
00070 bool path_exists(const char *path) {
00071         struct stat buf;
00072         return (stat(path, &buf) == 0);
00073 }
00074 
00080 void ungrab_all_keys(xcb_connection_t *conn) {
00081         DLOG("Ungrabbing all keys\n");
00082         xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
00083 }
00084 
00085 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
00086         DLOG("Grabbing %d\n", keycode);
00087         /* Grab the key in all combinations */
00088         #define GRAB_KEY(modifier) \
00089                 do { \
00090                         xcb_grab_key(conn, 0, root, modifier, keycode, \
00091                                      XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
00092                 } while (0)
00093         int mods = bind->mods;
00094         if ((bind->mods & BIND_MODE_SWITCH) != 0) {
00095                 mods &= ~BIND_MODE_SWITCH;
00096                 if (mods == 0)
00097                         mods = XCB_MOD_MASK_ANY;
00098         }
00099         GRAB_KEY(mods);
00100         GRAB_KEY(mods | xcb_numlock_mask);
00101         GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
00102 }
00103 
00104 /*
00105  * Returns a pointer to the Binding with the specified modifiers and keycode
00106  * or NULL if no such binding exists.
00107  *
00108  */
00109 Binding *get_binding(uint16_t modifiers, xcb_keycode_t keycode) {
00110         Binding *bind;
00111 
00112         TAILQ_FOREACH(bind, bindings, bindings) {
00113                 /* First compare the modifiers */
00114                 if (bind->mods != modifiers)
00115                         continue;
00116 
00117                 /* If a symbol was specified by the user, we need to look in
00118                  * the array of translated keycodes for the event’s keycode */
00119                 if (bind->symbol != NULL) {
00120                         if (memmem(bind->translated_to,
00121                                    bind->number_keycodes * sizeof(xcb_keycode_t),
00122                                    &keycode, sizeof(xcb_keycode_t)) != NULL)
00123                                 break;
00124                 } else {
00125                         /* This case is easier: The user specified a keycode */
00126                         if (bind->keycode == keycode)
00127                                 break;
00128                 }
00129         }
00130 
00131         return (bind == TAILQ_END(bindings) ? NULL : bind);
00132 }
00133 
00134 /*
00135  * Translates keysymbols to keycodes for all bindings which use keysyms.
00136  *
00137  */
00138 void translate_keysyms() {
00139         Binding *bind;
00140         TAILQ_FOREACH(bind, bindings, bindings) {
00141                 if (bind->keycode > 0)
00142                         continue;
00143 
00144                 /* We need to translate the symbol to a keycode */
00145                 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
00146                 if (keysym == NoSymbol) {
00147                         ELOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
00148                         continue;
00149                 }
00150 
00151 #ifdef OLD_XCB_KEYSYMS_API
00152                 bind->number_keycodes = 1;
00153                 xcb_keycode_t code = xcb_key_symbols_get_keycode(keysyms, keysym);
00154                 DLOG("Translated symbol \"%s\" to 1 keycode (%d)\n", bind->symbol, code);
00155                 grab_keycode_for_binding(global_conn, bind, code);
00156                 bind->translated_to = smalloc(sizeof(xcb_keycode_t));
00157                 memcpy(bind->translated_to, &code, sizeof(xcb_keycode_t));
00158 #else
00159                 uint32_t last_keycode = 0;
00160                 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
00161                 if (keycodes == NULL) {
00162                         DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
00163                         continue;
00164                 }
00165 
00166                 bind->number_keycodes = 0;
00167 
00168                 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
00169                         /* We hope duplicate keycodes will be returned in order
00170                          * and skip them */
00171                         if (last_keycode == *walk)
00172                                 continue;
00173                         last_keycode = *walk;
00174                         bind->number_keycodes++;
00175                 }
00176                 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
00177                 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
00178                 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
00179                 free(keycodes);
00180 #endif
00181         }
00182 }
00183 
00184 /*
00185  * Grab the bound keys (tell X to send us keypress events for those keycodes)
00186  *
00187  */
00188 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
00189         Binding *bind;
00190         TAILQ_FOREACH(bind, bindings, bindings) {
00191                 if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
00192                     (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
00193                         continue;
00194 
00195                 /* The easy case: the user specified a keycode directly. */
00196                 if (bind->keycode > 0) {
00197                         grab_keycode_for_binding(conn, bind, bind->keycode);
00198                         continue;
00199                 }
00200 
00201                 xcb_keycode_t *walk = bind->translated_to;
00202                 for (int i = 0; i < bind->number_keycodes; i++)
00203                         grab_keycode_for_binding(conn, bind, *walk);
00204         }
00205 }
00206 
00207 /*
00208  * Switches the key bindings to the given mode, if the mode exists
00209  *
00210  */
00211 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
00212         struct Mode *mode;
00213 
00214         LOG("Switching to mode %s\n", new_mode);
00215 
00216         SLIST_FOREACH(mode, &modes, modes) {
00217                 if (strcasecmp(mode->name, new_mode) != 0)
00218                         continue;
00219 
00220                 ungrab_all_keys(conn);
00221                 bindings = mode->bindings;
00222                 translate_keysyms();
00223                 grab_all_keys(conn, false);
00224                 return;
00225         }
00226 
00227         ELOG("ERROR: Mode not found\n");
00228 }
00229 
00230 /*
00231  * Get the path of the first configuration file found. Checks the XDG folders
00232  * first ($XDG_CONFIG_HOME, $XDG_CONFIG_DIRS), then the traditional paths.
00233  *
00234  */
00235 static char *get_config_path() {
00236         /* 1: check for $XDG_CONFIG_HOME/i3/config */
00237         char *xdg_config_home, *xdg_config_dirs, *config_path;
00238 
00239         if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
00240                 xdg_config_home = "~/.config";
00241 
00242         xdg_config_home = glob_path(xdg_config_home);
00243         if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
00244                 die("asprintf() failed");
00245         free(xdg_config_home);
00246 
00247         if (path_exists(config_path))
00248                 return config_path;
00249         free(config_path);
00250 
00251         /* 2: check for $XDG_CONFIG_DIRS/i3/config */
00252         if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
00253                 xdg_config_dirs = "/etc/xdg";
00254 
00255         char *buf = sstrdup(xdg_config_dirs);
00256         char *tok = strtok(buf, ":");
00257         while (tok != NULL) {
00258                 tok = glob_path(tok);
00259                 if (asprintf(&config_path, "%s/i3/config", tok) == -1)
00260                         die("asprintf() failed");
00261                 free(tok);
00262                 if (path_exists(config_path)) {
00263                         free(buf);
00264                         return config_path;
00265                 }
00266                 free(config_path);
00267                 tok = strtok(NULL, ":");
00268         }
00269         free(buf);
00270 
00271         /* 3: check traditional paths */
00272         config_path = glob_path("~/.i3/config");
00273         if (path_exists(config_path))
00274                 return config_path;
00275 
00276         config_path = sstrdup("/etc/i3/config");
00277         if (!path_exists(config_path))
00278                 die("Neither $XDG_CONFIG_HOME/i3/config, nor "
00279                     "$XDG_CONFIG_DIRS/i3/config, nor ~/.i3/config nor "
00280                     "/etc/i3/config exist.");
00281 
00282         return config_path;
00283 }
00284 
00285 /*
00286  * Finds the configuration file to use (either the one specified by
00287  * override_configpath), the user’s one or the system default) and calls
00288  * parse_file().
00289  *
00290  */
00291 static void parse_configuration(const char *override_configpath) {
00292         if (override_configpath != NULL) {
00293                 parse_file(override_configpath);
00294                 return;
00295         }
00296 
00297         char *path = get_config_path();
00298         DLOG("Parsing configfile %s\n", path);
00299         parse_file(path);
00300         free(path);
00301 }
00302 
00303 /*
00304  * (Re-)loads the configuration file (sets useful defaults before).
00305  *
00306  */
00307 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
00308         if (reload) {
00309                 /* First ungrab the keys */
00310                 ungrab_all_keys(conn);
00311 
00312                 struct Mode *mode;
00313                 Binding *bind;
00314                 while (!SLIST_EMPTY(&modes)) {
00315                         mode = SLIST_FIRST(&modes);
00316                         FREE(mode->name);
00317 
00318                         /* Clear the old binding list */
00319                         bindings = mode->bindings;
00320                         while (!TAILQ_EMPTY(bindings)) {
00321                                 bind = TAILQ_FIRST(bindings);
00322                                 TAILQ_REMOVE(bindings, bind, bindings);
00323                                 FREE(bind->translated_to);
00324                                 FREE(bind->command);
00325                                 FREE(bind);
00326                         }
00327                         FREE(bindings);
00328                         SLIST_REMOVE(&modes, mode, Mode, modes);
00329                 }
00330 
00331                 struct Assignment *assign;
00332                 while (!TAILQ_EMPTY(&assignments)) {
00333                         assign = TAILQ_FIRST(&assignments);
00334                         FREE(assign->windowclass_title);
00335                         TAILQ_REMOVE(&assignments, assign, assignments);
00336                         FREE(assign);
00337                 }
00338 
00339                 /* Clear workspace names */
00340                 Workspace *ws;
00341                 TAILQ_FOREACH(ws, workspaces, workspaces)
00342                         workspace_set_name(ws, NULL);
00343         }
00344 
00345         SLIST_INIT(&modes);
00346 
00347         struct Mode *default_mode = scalloc(sizeof(struct Mode));
00348         default_mode->name = sstrdup("default");
00349         default_mode->bindings = scalloc(sizeof(struct bindings_head));
00350         TAILQ_INIT(default_mode->bindings);
00351         SLIST_INSERT_HEAD(&modes, default_mode, modes);
00352 
00353         bindings = default_mode->bindings;
00354 
00355 #define REQUIRED_OPTION(name) \
00356         if (config.name == NULL) \
00357                 die("You did not specify required configuration option " #name "\n");
00358 
00359         /* Clear the old config or initialize the data structure */
00360         memset(&config, 0, sizeof(config));
00361 
00362         /* Initialize default colors */
00363 #define INIT_COLOR(x, cborder, cbackground, ctext) \
00364         do { \
00365                 x.border = get_colorpixel(conn, cborder); \
00366                 x.background = get_colorpixel(conn, cbackground); \
00367                 x.text = get_colorpixel(conn, ctext); \
00368         } while (0)
00369 
00370         config.client.background = get_colorpixel(conn, "#000000");
00371         INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
00372         INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
00373         INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
00374         INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
00375         INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
00376         INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
00377         INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
00378 
00379         parse_configuration(override_configpath);
00380 
00381         if (reload) {
00382                 translate_keysyms();
00383                 grab_all_keys(conn, false);
00384         }
00385 
00386         REQUIRED_OPTION(font);
00387 
00388         /* Set an empty name for every workspace which got no name */
00389         Workspace *ws;
00390         TAILQ_FOREACH(ws, workspaces, workspaces) {
00391                 if (ws->name != NULL) {
00392                         /* If the font was not specified when the workspace name
00393                          * was loaded, we need to predict the text width now */
00394                         if (ws->text_width == 0)
00395                                 ws->text_width = predict_text_width(global_conn,
00396                                                 config.font, ws->name, ws->name_len);
00397                         continue;
00398                 }
00399 
00400                 workspace_set_name(ws, NULL);
00401         }
00402 }