]> git.sur5r.net Git - i3/i3/blobdiff - src/config.c
Don’t use SYNC key bindings for Mode_switch but re-grab keys
[i3/i3] / src / config.c
index 86f0381e6c9ba092af4bd403333bfe5084b91d0d..8d32ad4a22041d799cd7f4f9ba4c5d6bfeea5a27 100644 (file)
@@ -7,12 +7,18 @@
  *
  * See file LICENSE for license information.
  *
+ * src/config.c: Contains all functions handling the configuration file (calling
+ * the parser (src/cfgparse.y) with the correct path, switching key bindings
+ * mode).
+ *
  */
 #include <stdio.h>
 #include <string.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 #include <stdlib.h>
 #include <glob.h>
+#include <unistd.h>
 
 /* We need Xlib for XStringToKeysym */
 #include <X11/Xlib.h>
 #include "xcb.h"
 #include "table.h"
 #include "workspace.h"
+#include "log.h"
 
 Config config;
+struct modes_head modes;
 
 /*
  * This function resolves ~ in pathnames.
@@ -42,26 +50,12 @@ static char *glob_path(const char *path) {
 }
 
 /*
- * This function does a very simple replacement of each instance of key with value.
+ * Checks if the given path exists by calling stat().
  *
  */
-static void replace_variable(char *buffer, const char *key, const char *value) {
-        char *pos;
-        /* To prevent endless recursions when the user makes an error configuring,
-         * we stop after 100 replacements. That should be vastly more than enough. */
-        int c = 0;
-        LOG("Replacing %s with %s\n", key, value);
-        while ((pos = strcasestr(buffer, key)) != NULL && c++ < 100) {
-                LOG("replacing variable %s in \"%s\" with \"%s\"\n", key, buffer, value);
-                char *rest = pos + strlen(key);
-                *pos = '\0';
-                char *replaced;
-                asprintf(&replaced, "%s%s%s", buffer, value, rest);
-                /* Hm, this is a bit ugly, but sizeof(buffer) = 4, as it’s just a pointer.
-                 * So we need to hard-code the dimensions here. */
-                strncpy(buffer, replaced, 1026);
-                free(replaced);
-        }
+static bool path_exists(const char *path) {
+        struct stat buf;
+        return (stat(path, &buf) == 0);
 }
 
 /**
@@ -70,422 +64,309 @@ static void replace_variable(char *buffer, const char *key, const char *value) {
  *
  */
 void ungrab_all_keys(xcb_connection_t *conn) {
-        LOG("Ungrabbing all keys\n");
+        DLOG("Ungrabbing all keys\n");
         xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
 }
 
 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
-        LOG("Grabbing %d\n", keycode);
-        if ((bind->mods & BIND_MODE_SWITCH) != 0)
-                xcb_grab_key(conn, 0, root, 0, keycode,
-                        XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
-        else {
-                /* Grab the key in all combinations */
-                #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, keycode, \
-                                                        XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
-                GRAB_KEY(bind->mods);
-                GRAB_KEY(bind->mods | xcb_numlock_mask);
-                GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
+        DLOG("Grabbing %d\n", keycode);
+        /* Grab the key in all combinations */
+        #define GRAB_KEY(modifier) \
+                do { \
+                        xcb_grab_key(conn, 0, root, modifier, keycode, \
+                                     XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
+                } while (0)
+        int mods = bind->mods;
+        if ((bind->mods & BIND_MODE_SWITCH) != 0) {
+                mods &= ~BIND_MODE_SWITCH;
+                if (mods == 0)
+                        mods = XCB_MOD_MASK_ANY;
         }
+        GRAB_KEY(mods);
+        GRAB_KEY(mods | xcb_numlock_mask);
+        GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
 }
 
 /*
- * Grab the bound keys (tell X to send us keypress events for those keycodes)
+ * Returns a pointer to the Binding with the specified modifiers and keycode
+ * or NULL if no such binding exists.
  *
  */
-void grab_all_keys(xcb_connection_t *conn) {
+Binding *get_binding(uint16_t modifiers, xcb_keycode_t keycode) {
         Binding *bind;
-        TAILQ_FOREACH(bind, &bindings, bindings) {
-                /* The easy case: the user specified a keycode directly. */
-                if (bind->keycode > 0) {
-                        grab_keycode_for_binding(conn, bind, bind->keycode);
+
+        TAILQ_FOREACH(bind, bindings, bindings) {
+                /* First compare the modifiers */
+                if (bind->mods != modifiers)
                         continue;
+
+                /* If a symbol was specified by the user, we need to look in
+                 * the array of translated keycodes for the event’s keycode */
+                if (bind->symbol != NULL) {
+                        if (memmem(bind->translated_to,
+                                   bind->number_keycodes * sizeof(xcb_keycode_t),
+                                   &keycode, sizeof(xcb_keycode_t)) != NULL)
+                                break;
+                } else {
+                        /* This case is easier: The user specified a keycode */
+                        if (bind->keycode == keycode)
+                                break;
                 }
+        }
+
+        return (bind == TAILQ_END(bindings) ? NULL : bind);
+}
+
+/*
+ * Translates keysymbols to keycodes for all bindings which use keysyms.
+ *
+ */
+void translate_keysyms() {
+        Binding *bind;
+        TAILQ_FOREACH(bind, bindings, bindings) {
+                if (bind->keycode > 0)
+                        continue;
 
                 /* We need to translate the symbol to a keycode */
-                LOG("Translating symbol to keycode (\"%s\")\n", bind->symbol);
                 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
                 if (keysym == NoSymbol) {
-                        LOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
+                        ELOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
                         continue;
                 }
 
+#ifdef OLD_XCB_KEYSYMS_API
+                bind->number_keycodes = 1;
+                xcb_keycode_t code = xcb_key_symbols_get_keycode(keysyms, keysym);
+                DLOG("Translated symbol \"%s\" to 1 keycode (%d)\n", bind->symbol, code);
+                grab_keycode_for_binding(conn, bind, code);
+                bind->translated_to = smalloc(sizeof(xcb_keycode_t));
+                memcpy(bind->translated_to, &code, sizeof(xcb_keycode_t));
+#else
+                uint32_t last_keycode = 0;
                 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
                 if (keycodes == NULL) {
-                        LOG("Could not translate symbol \"%s\"\n", bind->symbol);
+                        DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
                         continue;
                 }
 
-                uint32_t last_keycode;
                 bind->number_keycodes = 0;
+
                 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
                         /* We hope duplicate keycodes will be returned in order
                          * and skip them */
                         if (last_keycode == *walk)
                                 continue;
-                        grab_keycode_for_binding(conn, bind, *walk);
                         last_keycode = *walk;
                         bind->number_keycodes++;
                 }
-                LOG("Got %d different keycodes\n", bind->number_keycodes);
+                DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
                 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
                 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
                 free(keycodes);
+#endif
         }
 }
 
 /*
- * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
- *
- * If you specify override_configpath, only this path is used to look for a
- * configuration file.
+ * Grab the bound keys (tell X to send us keypress events for those keycodes)
  *
  */
-void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
-        if (reload) {
-                /* First ungrab the keys */
-                ungrab_all_keys(conn);
+void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
+        Binding *bind;
+        TAILQ_FOREACH(bind, bindings, bindings) {
+                if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
+                    (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
+                        continue;
 
-                /* Clear the old binding and assignment lists */
-                Binding *bind;
-                while (!TAILQ_EMPTY(&bindings)) {
-                        bind = TAILQ_FIRST(&bindings);
-                        TAILQ_REMOVE(&bindings, bind, bindings);
-                        FREE(bind->command);
-                        FREE(bind);
+                /* The easy case: the user specified a keycode directly. */
+                if (bind->keycode > 0) {
+                        grab_keycode_for_binding(conn, bind, bind->keycode);
+                        continue;
                 }
 
-                struct Assignment *assign;
-                while (!TAILQ_EMPTY(&assignments)) {
-                        assign = TAILQ_FIRST(&assignments);
-                        FREE(assign->windowclass_title);
-                        TAILQ_REMOVE(&assignments, assign, assignments);
-                        FREE(assign);
-                }
+                xcb_keycode_t *walk = bind->translated_to;
+                for (int i = 0; i < bind->number_keycodes; i++)
+                        grab_keycode_for_binding(conn, bind, *walk);
         }
+}
 
-        SLIST_HEAD(variables_head, Variable) variables;
+/*
+ * Switches the key bindings to the given mode, if the mode exists
+ *
+ */
+void switch_mode(xcb_connection_t *conn, const char *new_mode) {
+        struct Mode *mode;
 
-#define OPTION_STRING(name) \
-        if (strcasecmp(key, #name) == 0) { \
-                config.name = sstrdup(value); \
-                continue; \
-        }
+        LOG("Switching to mode %s\n", new_mode);
 
-#define REQUIRED_OPTION(name) \
-        if (config.name == NULL) \
-                die("You did not specify required configuration option " #name "\n");
+        SLIST_FOREACH(mode, &modes, modes) {
+                if (strcasecmp(mode->name, new_mode) != 0)
+                        continue;
 
-#define OPTION_COLORTRIPLE(opt, name) \
-        if (strcasecmp(key, opt) == 0) { \
-                char border[8], background[8], text[8]; \
-                memset(border, 0, sizeof(border)); \
-                memset(background, 0, sizeof(background)); \
-                memset(text, 0, sizeof(text)); \
-                border[0] = background[0] = text[0] = '#'; \
-                if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
-                    border + 1, background + 1, text + 1) != 3 || \
-                    strlen(border) != 7 || \
-                    strlen(background) != 7 || \
-                    strlen(text) != 7) \
-                        die("invalid color code line: %s\n", value); \
-                config.name.border = get_colorpixel(conn, border); \
-                config.name.background = get_colorpixel(conn, background); \
-                config.name.text = get_colorpixel(conn, text); \
-                continue; \
+                ungrab_all_keys(conn);
+                bindings = mode->bindings;
+                grab_all_keys(conn, false);
+                return;
         }
 
-        /* Clear the old config or initialize the data structure */
-        memset(&config, 0, sizeof(config));
-
-        SLIST_INIT(&variables);
-
-        /* Initialize default colors */
-        config.client.focused.border = get_colorpixel(conn, "#4c7899");
-        config.client.focused.background = get_colorpixel(conn, "#285577");
-        config.client.focused.text = get_colorpixel(conn, "#ffffff");
+        ELOG("ERROR: Mode not found\n");
+}
 
-        config.client.focused_inactive.border = get_colorpixel(conn, "#4c7899");
-        config.client.focused_inactive.background = get_colorpixel(conn, "#555555");
-        config.client.focused_inactive.text = get_colorpixel(conn, "#ffffff");
+/*
+ * Get the path of the first configuration file found. Checks the XDG folders
+ * first ($XDG_CONFIG_HOME, $XDG_CONFIG_DIRS), then the traditional paths.
+ *
+ */
+static char *get_config_path() {
+        /* 1: check for $XDG_CONFIG_HOME/i3/config */
+        char *xdg_config_home, *xdg_config_dirs, *config_path;
+
+        if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
+                xdg_config_home = "~/.config";
+
+        xdg_config_home = glob_path(xdg_config_home);
+        if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
+                die("asprintf() failed");
+        free(xdg_config_home);
+
+        if (path_exists(config_path))
+                return config_path;
+        free(config_path);
+
+        /* 2: check for $XDG_CONFIG_DIRS/i3/config */
+        if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
+                xdg_config_dirs = "/etc/xdg";
+
+        char *buf = strdup(xdg_config_dirs);
+        char *tok = strtok(buf, ":");
+        while (tok != NULL) {
+                tok = glob_path(tok);
+                if (asprintf(&config_path, "%s/i3/config", tok) == -1)
+                        die("asprintf() failed");
+                free(tok);
+                if (path_exists(config_path)) {
+                        free(buf);
+                        return config_path;
+                }
+                free(config_path);
+                tok = strtok(NULL, ":");
+        }
+        free(buf);
 
-        config.client.unfocused.border = get_colorpixel(conn, "#333333");
-        config.client.unfocused.background = get_colorpixel(conn, "#222222");
-        config.client.unfocused.text = get_colorpixel(conn, "#888888");
+        /* 3: check traditional paths */
+        config_path = glob_path("~/.i3/config");
+        if (path_exists(config_path))
+                return config_path;
 
-        config.bar.focused.border = get_colorpixel(conn, "#4c7899");
-        config.bar.focused.background = get_colorpixel(conn, "#285577");
-        config.bar.focused.text = get_colorpixel(conn, "#ffffff");
+        config_path = strdup("/etc/i3/config");
+        if (!path_exists(config_path))
+                die("Neither $XDG_CONFIG_HOME/i3/config, nor "
+                    "$XDG_CONFIG_DIRS/i3/config, nor ~/.i3/config nor "
+                    "/etc/i3/config exist.");
 
-        config.bar.unfocused.border = get_colorpixel(conn, "#333333");
-        config.bar.unfocused.background = get_colorpixel(conn, "#222222");
-        config.bar.unfocused.text = get_colorpixel(conn, "#888888");
+        return config_path;
+}
 
-        FILE *handle;
+/*
+ * Finds the configuration file to use (either the one specified by
+ * override_configpath), the user’s one or the system default) and calls
+ * parse_file().
+ *
+ */
+static void parse_configuration(const char *override_configpath) {
         if (override_configpath != NULL) {
-                if ((handle = fopen(override_configpath, "r")) == NULL)
-                        die("Could not open configfile \"%s\".\n", override_configpath);
-        } else {
-                /* We first check for ~/.i3/config, then for /etc/i3/config */
-                char *globbed = glob_path("~/.i3/config");
-                if ((handle = fopen(globbed, "r")) == NULL)
-                        if ((handle = fopen("/etc/i3/config", "r")) == NULL)
-                                die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
-                free(globbed);
+                parse_file(override_configpath);
+                return;
         }
-        char key[512], value[512], buffer[1026];
 
-        while (!feof(handle)) {
-                if (fgets(buffer, 1024, handle) == NULL) {
-                        /* fgets returns NULL on EOF and on error, so see which one it is. */
-                        if (feof(handle))
-                                break;
-                        die("Could not read configuration file\n");
-                }
-
-                if (config.terminal != NULL)
-                        replace_variable(buffer, "$terminal", config.terminal);
-
-                /* Replace all custom variables */
-                struct Variable *current;
-                SLIST_FOREACH(current, &variables, variables)
-                        replace_variable(buffer, current->key, current->value);
-
-                /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
-                if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
-                    key[0] == '#' || strlen(key) < 3)
-                        continue;
-
-                OPTION_STRING(terminal);
-                OPTION_STRING(font);
-
-                /* Colors */
-                OPTION_COLORTRIPLE("client.focused", client.focused);
-                OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
-                OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
-                OPTION_COLORTRIPLE("bar.focused", bar.focused);
-                OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
-
-                /* exec-lines (autostart) */
-                if (strcasecmp(key, "exec") == 0) {
-                        struct Autostart *new = smalloc(sizeof(struct Autostart));
-                        new->command = sstrdup(value);
-                        TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
-                        continue;
-                }
-
-                /* key bindings */
-                if (strcasecmp(key, "bind") == 0 || strcasecmp(key, "bindsym") == 0) {
-                        #define CHECK_MODIFIER(name) \
-                                if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
-                                        modifiers |= BIND_##name; \
-                                        walk += strlen(#name) + 1; \
-                                        continue; \
-                                }
-                        char *walk = value, *rest;
-                        uint32_t modifiers = 0;
-
-                        while (*walk != '\0') {
-                                /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
-                                CHECK_MODIFIER(SHIFT);
-                                CHECK_MODIFIER(CONTROL);
-                                CHECK_MODIFIER(MODE_SWITCH);
-                                CHECK_MODIFIER(MOD1);
-                                CHECK_MODIFIER(MOD2);
-                                CHECK_MODIFIER(MOD3);
-                                CHECK_MODIFIER(MOD4);
-                                CHECK_MODIFIER(MOD5);
-
-                                /* No modifier found? Then we’re done with this step */
-                                break;
-                        }
+        char *path = get_config_path();
+        DLOG("Parsing configfile %s\n", path);
+        parse_file(path);
+        free(path);
+}
 
-                        Binding *new = scalloc(sizeof(Binding));
-
-                        /* Now check for the keycode or copy the symbol */
-                        if (strcasecmp(key, "bind") == 0) {
-                                int keycode = strtol(walk, &rest, 10);
-                                if (!rest || *rest != ' ')
-                                        die("Invalid binding (keycode)\n");
-                                new->keycode = keycode;
-                        } else {
-                                rest = walk;
-                                char *sym = rest;
-                                while (*rest != '\0' && *rest != ' ')
-                                        rest++;
-                                if (*rest != ' ')
-                                        die("Invalid binding (keysym)\n");
-                                new->symbol = strndup(sym, (rest - sym));
-                        }
-                        rest++;
-                        LOG("keycode = %d, symbol = %s, modifiers = %d, command = *%s*\n", new->keycode, new->symbol, modifiers, rest);
-                        new->mods = modifiers;
-                        new->command = sstrdup(rest);
-                        TAILQ_INSERT_TAIL(&bindings, new, bindings);
-                        continue;
-                }
+/*
+ * (Re-)loads the configuration file (sets useful defaults before).
+ *
+ */
+void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
+        if (reload) {
+                /* First ungrab the keys */
+                ungrab_all_keys(conn);
 
-                if (strcasecmp(key, "floating_modifier") == 0) {
-                        char *walk = value;
-                        uint32_t modifiers = 0;
-
-                        while (*walk != '\0') {
-                                /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
-                                CHECK_MODIFIER(SHIFT);
-                                CHECK_MODIFIER(CONTROL);
-                                CHECK_MODIFIER(MODE_SWITCH);
-                                CHECK_MODIFIER(MOD1);
-                                CHECK_MODIFIER(MOD2);
-                                CHECK_MODIFIER(MOD3);
-                                CHECK_MODIFIER(MOD4);
-                                CHECK_MODIFIER(MOD5);
-
-                                /* No modifier found? Then we’re done with this step */
-                                break;
+                struct Mode *mode;
+                Binding *bind;
+                while (!SLIST_EMPTY(&modes)) {
+                        mode = SLIST_FIRST(&modes);
+                        FREE(mode->name);
+
+                        /* Clear the old binding list */
+                        bindings = mode->bindings;
+                        while (!TAILQ_EMPTY(bindings)) {
+                                bind = TAILQ_FIRST(bindings);
+                                TAILQ_REMOVE(bindings, bind, bindings);
+                                FREE(bind->translated_to);
+                                FREE(bind->command);
+                                FREE(bind);
                         }
-
-                        LOG("Floating modifiers = %d\n", modifiers);
-                        config.floating_modifier = modifiers;
-                        continue;
+                        FREE(bindings);
+                        SLIST_REMOVE(&modes, mode, Mode, modes);
                 }
 
-                /* name "workspace number" "name of the workspace" */
-                if (strcasecmp(key, "name") == 0) {
-                        LOG("name workspace: %s\n",value);
-                        char *ws_str = sstrdup(value);
-                        char *end = strchr(ws_str, ' ');
-                        if (end == NULL)
-                                die("Malformed name, couln't find terminating space\n");
-                        *end = '\0';
-
-                        /* Strip trailing whitespace */
-                        while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
-                                value[strlen(value)-1] = '\0';
-
-                        int ws_num = atoi(ws_str);
-
-                        if (ws_num < 1 || ws_num > 10)
-                                die("Malformed name, invalid workspace number\n");
-
-                        /* find the name */
-                        char *name = value;
-                        name += strlen(ws_str) + 1;
-
-                        if (name == '\0') {
-                                free(ws_str);
-                                continue;
-                        }
-
-                        workspace_set_name(&(workspaces[ws_num - 1]), name);
-                        free(ws_str);
-                        continue;
+                struct Assignment *assign;
+                while (!TAILQ_EMPTY(&assignments)) {
+                        assign = TAILQ_FIRST(&assignments);
+                        FREE(assign->windowclass_title);
+                        TAILQ_REMOVE(&assignments, assign, assignments);
+                        FREE(assign);
                 }
+        }
 
-                /* assign window class[/window title] → workspace */
-                if (strcasecmp(key, "assign") == 0) {
-                        LOG("assign: \"%s\"\n", value);
-                        char *class_title;
-                        char *target;
-                        char *end;
-
-                        /* If the window class/title is quoted we skip quotes */
-                        if (value[0] == '"') {
-                                class_title = sstrdup(value+1);
-                                end = strchr(class_title, '"');
-                        } else {
-                                class_title = sstrdup(value);
-                                /* If it is not quoted, we terminate it at the first space */
-                                end = strchr(class_title, ' ');
-                        }
-                        if (end == NULL)
-                                die("Malformed assignment, couldn't find terminating quote\n");
-                        *end = '\0';
-
-                        /* Strip trailing whitespace */
-                        while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
-                                value[strlen(value)-1] = '\0';
-
-                        /* The target is the last argument separated by a space */
-                        if ((target = strrchr(value, ' ')) == NULL)
-                                die("Malformed assignment, couldn't find target (\"%s\")\n", value);
-                        target++;
-
-                        if (strchr(target, '~') == NULL && (atoi(target) < 1 || atoi(target) > 10))
-                                die("Malformed assignment, invalid workspace number\n");
-
-                        LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
-
-                        struct Assignment *new = scalloc(sizeof(struct Assignment));
-                        new->windowclass_title = class_title;
-                        if (strchr(target, '~') != NULL)
-                                new->floating = ASSIGN_FLOATING_ONLY;
-
-                        while (*target == '~')
-                                target++;
-
-                        if (atoi(target) >= 1 && atoi(target) <= 10) {
-                                if (new->floating == ASSIGN_FLOATING_ONLY)
-                                        new->floating = ASSIGN_FLOATING;
-                                new->workspace = atoi(target);
-                        }
-                        TAILQ_INSERT_TAIL(&assignments, new, assignments);
-
-                        LOG("Assignment loaded: \"%s\":\n", class_title);
-                        if (new->floating != ASSIGN_FLOATING_ONLY)
-                                LOG(" to workspace %d\n", new->workspace);
-
-                        if (new->floating != ASSIGN_FLOATING_NO)
-                                LOG(" will be floating\n");
+        SLIST_INIT(&modes);
 
-                        continue;
-                }
+        struct Mode *default_mode = scalloc(sizeof(struct Mode));
+        default_mode->name = sstrdup("default");
+        default_mode->bindings = scalloc(sizeof(struct bindings_head));
+        TAILQ_INIT(default_mode->bindings);
+        SLIST_INSERT_HEAD(&modes, default_mode, modes);
 
-                /* set a custom variable */
-                if (strcasecmp(key, "set") == 0) {
-                        if (value[0] != '$')
-                                die("Malformed variable assignment, name has to start with $\n");
+        bindings = default_mode->bindings;
 
-                        /* get key/value for this variable */
-                        char *v_key = value, *v_value;
-                        if ((v_value = strstr(value, " ")) == NULL)
-                                die("Malformed variable assignment, need a value\n");
-
-                        *(v_value++) = '\0';
+#define REQUIRED_OPTION(name) \
+        if (config.name == NULL) \
+                die("You did not specify required configuration option " #name "\n");
 
-                        struct Variable *new = scalloc(sizeof(struct Variable));
-                        new->key = sstrdup(v_key);
-                        new->value = sstrdup(v_value);
-                        SLIST_INSERT_HEAD(&variables, new, variables);
-                        LOG("Got new variable %s = %s\n", v_key, v_value);
-                        continue;
-                }
+        /* Clear the old config or initialize the data structure */
+        memset(&config, 0, sizeof(config));
 
-                if (strcasecmp(key, "ipc-socket") == 0) {
-                        config.ipc_socket_path = sstrdup(value);
-                        continue;
-                }
+        /* Initialize default colors */
+#define INIT_COLOR(x, cborder, cbackground, ctext) \
+        do { \
+                x.border = get_colorpixel(conn, cborder); \
+                x.background = get_colorpixel(conn, cbackground); \
+                x.text = get_colorpixel(conn, ctext); \
+        } while (0)
+
+        INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
+        INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
+        INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
+        INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
+        INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
+        INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
+        INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
+
+        parse_configuration(override_configpath);
 
-                die("Unknown configfile option: %s\n", key);
+        if (reload) {
+                translate_keysyms();
+                grab_all_keys(conn, false);
         }
-        /* now grab all keys again */
-        if (reload)
-                grab_all_keys(conn);
-        fclose(handle);
 
-        REQUIRED_OPTION(terminal);
         REQUIRED_OPTION(font);
 
-        while (!SLIST_EMPTY(&variables)) {
-                struct Variable *v = SLIST_FIRST(&variables);
-                SLIST_REMOVE_HEAD(&variables, variables);
-                free(v->key);
-                free(v->value);
-                free(v);
-        }
-
         /* Set an empty name for every workspace which got no name */
-        for (int i = 0; i < 10; i++) {
-                Workspace *ws = &(workspaces[i]);
+        Workspace *ws;
+        TAILQ_FOREACH(ws, workspaces, workspaces) {
                 if (ws->name != NULL) {
                         /* If the font was not specified when the workspace name
                          * was loaded, we need to predict the text width now */
@@ -495,8 +376,6 @@ void load_configuration(xcb_connection_t *conn, const char *override_configpath,
                         continue;
                 }
 
-                workspace_set_name(&(workspaces[i]), NULL);
+                workspace_set_name(ws, NULL);
         }
-        return;
 }