JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
new keyboard_mapping option to load pre-defined key bindings for
[spectrwm.git] / scrotwm.c
index 740c3f2..47e7319 100644 (file)
--- a/scrotwm.c
+++ b/scrotwm.c
@@ -1,4 +1,3 @@
-/* $scrotwm$ */
 /*
  * Copyright (c) 2009-2010-2011 Marco Peereboom <marco@peereboom.us>
  * Copyright (c) 2009-2010-2011 Ryan McBride <mcbride@countersiege.com>
@@ -163,6 +162,9 @@ u_int32_t           swm_debug = 0
 #define SWM_FOCUS_SYNERGY      (1)
 #define SWM_FOCUS_FOLLOW       (2)
 
+#define SWM_CONF_DEFAULT       (0)
+#define SWM_CONF_KEYMAPPING    (1)
+
 #ifndef SWM_LIB
 #define SWM_LIB                        "/usr/local/lib/libswmhack.so"
 #endif
@@ -237,6 +239,7 @@ int                 bar_fidx = 0;
 XFontStruct            *bar_fs;
 char                   *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
 char                   *spawn_term[] = { NULL, NULL };         /* XXX Make fully dynamic */
+struct passwd          *pwd;
 
 #define SWM_MENU_FN    (2)
 #define SWM_MENU_NB    (4)
@@ -322,6 +325,8 @@ void        new_region(struct swm_screen *, int, int, int, int);
 void   unmanage_window(struct ws_win *);
 long   getstate(Window);
 
+int    conf_load(char *, int);
+
 struct layout {
        void            (*l_stack)(struct workspace *, struct swm_geometry *);
        void            (*l_config)(struct workspace *, int);
@@ -3353,10 +3358,13 @@ resize(struct ws_win *win, union arg *args)
 {
        XEvent                  ev;
        Time                    time = 0;
-       struct swm_region       *r = win->ws->r;
+       struct swm_region       *r = NULL;
        int                     relx, rely;
        int                     resize_step = 0;
 
+       if (win == NULL)
+               return;
+       r = win->ws->r;
 
        DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
            win->id, win->floating, win->transient);
@@ -3471,7 +3479,7 @@ resize_step(struct swm_region *r, union arg *args)
 {
        struct ws_win           *win = NULL;
 
-       if (r && r->ws)
+       if (r && r->ws && r->ws->focus)
                win = r->ws->focus;
        else
                return;
@@ -3508,7 +3516,11 @@ move(struct ws_win *win, union arg *args)
        XEvent                  ev;
        Time                    time = 0;
        int                     move_step = 0;
-       struct swm_region       *r = win->ws->r;
+       struct swm_region       *r = NULL;
+
+       if (win == NULL)
+               return;
+       r = win->ws->r;
 
        DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
            win->id, win->floating, win->transient);
@@ -3605,7 +3617,7 @@ move_step(struct swm_region *r, union arg *args)
 {
        struct ws_win           *win = NULL;
 
-       if (r && r->ws)
+       if (r && r->ws && r->ws->focus)
                win = r->ws->focus;
        else
                return;
@@ -4377,6 +4389,37 @@ setup_keys(void)
 }
 
 void
+clear_keys(void)
+{
+       int                     i;
+
+       /* clear all key bindings, if any */
+       for (i = 0; i < keys_length; i++)
+               free(keys[i].spawn_name);
+       keys_length = 0;
+}
+
+int
+setkeymapping(char *selector, char *value, int flags)
+{
+       char                    keymapping_file[PATH_MAX];
+       DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
+       if (value[0] == '~')
+               snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
+                   pwd->pw_dir, &value[1]);
+       else
+               strlcpy(keymapping_file, value, sizeof keymapping_file);
+       clear_keys();
+       /* load new key bindings; if it fails, revert to default bindings */
+       if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
+               clear_keys();
+               setup_keys();
+       }
+       DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
+       return (0);
+}
+
+void
 updatenumlockmask(void)
 {
        unsigned int            i, j;
@@ -4821,7 +4864,8 @@ setautorun(char *selector, char *value, int flags)
 int
 setlayout(char *selector, char *value, int flags)
 {
-       int                     ws_id, st, i, x, mg, ma, si, raise;
+       int                     ws_id, i, x, mg, ma, si, raise;
+       int                     st = SWM_V_STACK;
        char                    s[1024];
        struct workspace        *ws;
 
@@ -4899,6 +4943,7 @@ struct config_option configopt[] = {
        { "bar_font",                   setconfvalue,   SWM_S_BAR_FONT },
        { "bar_action",                 setconfvalue,   SWM_S_BAR_ACTION },
        { "bar_delay",                  setconfvalue,   SWM_S_BAR_DELAY },
+       { "keyboard_mapping",           setkeymapping,  0 },
        { "bind",                       setconfbinding, 0 },
        { "stack_enabled",              setconfvalue,   SWM_S_STACK_ENABLED },
        { "clock_enabled",              setconfvalue,   SWM_S_CLOCK_ENABLED },
@@ -4930,7 +4975,7 @@ struct config_option configopt[] = {
 
 
 int
-conf_load(char *filename)
+conf_load(char *filename, int keymapping)
 {
        FILE                    *config;
        char                    *line, *cp, *optsub, *optval;
@@ -4945,7 +4990,7 @@ conf_load(char *filename)
                return (1);
        }
        if ((config = fopen(filename, "r")) == NULL) {
-               warn("conf_load: fopen");
+               warn("conf_load: fopen: %s", filename);
                return (1);
        }
 
@@ -4985,6 +5030,11 @@ conf_load(char *filename)
                            filename, lineno, wordlen, cp);
                        return (1);
                }
+               if (keymapping && strcmp(opt->optname, "bind")) {
+                       warnx("%s: line %zd: invalid option %.*s",
+                           filename, lineno, wordlen, cp);
+                       return (1);
+               }
                cp += wordlen;
                cp += strspn(cp, " \t\n"); /* eat whitespace */
                /* get [selector] if any */
@@ -5428,15 +5478,14 @@ void
 keypress(XEvent *e)
 {
        unsigned int            i;
-       KeySym                  keysym, skeysym;
+       KeySym                  keysym;
        XKeyEvent               *ev = &e->xkey;
 
        DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
 
        keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
-       skeysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 1);
        for (i = 0; i < keys_length; i++)
-               if ((keysym == keys[i].keysym || skeysym == keys[i].keysym)
+               if (keysym == keys[i].keysym
                    && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
                    && keyfuncs[keys[i].funcid].func) {
                        if (keys[i].funcid == kf_spawn_custom)
@@ -5462,7 +5511,6 @@ buttonpress(XEvent *e)
 
        DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
 
-       action = root_click;
        if ((win = find_window(ev->window)) == NULL)
                return;
 
@@ -6261,7 +6309,6 @@ workaround(void)
 int
 main(int argc, char *argv[])
 {
-       struct passwd           *pwd;
        struct swm_region       *r, *rr;
        struct ws_win           *winfocus = NULL;
        struct timeval          tv;
@@ -6332,7 +6379,7 @@ main(int argc, char *argv[])
                                cfile = conf;
        }
        if (cfile)
-               conf_load(cfile);
+               conf_load(cfile, SWM_CONF_DEFAULT);
 
        setup_ewmh();
        /* set some values to work around bad programs */