diff --git a/dotool.go b/dotool.go index 08428f5..53626cf 100644 --- a/dotool.go +++ b/dotool.go @@ -221,25 +221,29 @@ func cutWord(s, word string) (string, bool) { func main() { var keymap *xkb.Keymap { - names := xkb.RuleNames{ - Rules: "", - Model: "", - Layout: os.Getenv("DOTOOL_XKB_LAYOUT"), - Variant: os.Getenv("DOTOOL_XKB_VARIANT"), - Options: "", + layout := os.Getenv("DOTOOL_XKB_LAYOUT") + if layout == "" { + layout = os.Getenv("XKB_DEFAULT_LAYOUT") } + variant := os.Getenv("DOTOOL_XKB_VARIANT") + if variant == "" { + variant = os.Getenv("XKB_DEFAULT_VARIANT") + } + if variant != "" && layout == "" { + // Otherwise xkbcommon just ignores the variant. + fatal("you need to set $DOTOOL_XKB_LAYOUT or $XKB_DEFAULT_LAYOUT if the variant is set") + } + names := xkb.RuleNames{Layout: layout, Variant: variant} ctx := xkb.ContextNew(xkb.ContextNoFlags) defer ctx.Unref() - keymap = ctx.KeymapNewFromNames(&names, xkb.KeymapCompileNoFlags) - defer keymap.Unref() - if keymap == nil { fatal("failed to compile keymap") } + defer keymap.Unref() - initKeys(keymap, names != xkb.RuleNames{}) + initKeys(keymap) } { diff --git a/keys.go b/keys.go index 7187627..4410a4f 100644 --- a/keys.go +++ b/keys.go @@ -27,6 +27,7 @@ var LinuxKeys = map[string]Chord{ "ro": Chord{Key: uinput.KeyRo}, "zenkakuhankaku": Chord{Key: uinput.KeyZenkakuhankaku}, + // Potentially overwritten by initKeys() "esc": Chord{Key: uinput.KeyEsc}, "1": Chord{Key: uinput.Key1}, "2": Chord{Key: uinput.Key2}, @@ -495,18 +496,16 @@ func newChord(keymap *xkb.Keymap, mask, code uint32) Chord{ } } -func initKeys(keymap *xkb.Keymap, linux bool) { +func initKeys(keymap *xkb.Keymap) { maxCode := keymap.MaxKeycode() for code := keymap.MinKeycode(); code <= maxCode && code < 256; code++ { numLevels := keymap.NumLevelsForKey(code, 0) for level := uint32(0); level < numLevels; level++ { for _, sym := range keymap.KeyGetSymsByLevel(code, 0, level) { chord := newChord(keymap, keymap.KeyGetMod(code, 0, level), code) - if linux { - for name, s := range linuxXSyms { - if s == sym { - LinuxKeys[name] = chord - } + for name, s := range linuxXSyms { + if s == sym { + LinuxKeys[name] = chord } } XKeys[xkb.KeysymGetName(sym)] = chord