always use xkb and complain if only variant is set

This commit is contained in:
John Gebbie 2023-05-11 11:03:36 +01:00
parent 874650ad22
commit 1b330dd7c1
2 changed files with 19 additions and 16 deletions

View file

@ -221,25 +221,29 @@ func cutWord(s, word string) (string, bool) {
func main() { func main() {
var keymap *xkb.Keymap var keymap *xkb.Keymap
{ {
names := xkb.RuleNames{ layout := os.Getenv("DOTOOL_XKB_LAYOUT")
Rules: "", if layout == "" {
Model: "", layout = os.Getenv("XKB_DEFAULT_LAYOUT")
Layout: os.Getenv("DOTOOL_XKB_LAYOUT"),
Variant: os.Getenv("DOTOOL_XKB_VARIANT"),
Options: "",
} }
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) ctx := xkb.ContextNew(xkb.ContextNoFlags)
defer ctx.Unref() defer ctx.Unref()
keymap = ctx.KeymapNewFromNames(&names, xkb.KeymapCompileNoFlags) keymap = ctx.KeymapNewFromNames(&names, xkb.KeymapCompileNoFlags)
defer keymap.Unref()
if keymap == nil { if keymap == nil {
fatal("failed to compile keymap") fatal("failed to compile keymap")
} }
defer keymap.Unref()
initKeys(keymap, names != xkb.RuleNames{}) initKeys(keymap)
} }
{ {

View file

@ -27,6 +27,7 @@ var LinuxKeys = map[string]Chord{
"ro": Chord{Key: uinput.KeyRo}, "ro": Chord{Key: uinput.KeyRo},
"zenkakuhankaku": Chord{Key: uinput.KeyZenkakuhankaku}, "zenkakuhankaku": Chord{Key: uinput.KeyZenkakuhankaku},
// Potentially overwritten by initKeys()
"esc": Chord{Key: uinput.KeyEsc}, "esc": Chord{Key: uinput.KeyEsc},
"1": Chord{Key: uinput.Key1}, "1": Chord{Key: uinput.Key1},
"2": Chord{Key: uinput.Key2}, "2": Chord{Key: uinput.Key2},
@ -495,20 +496,18 @@ func newChord(keymap *xkb.Keymap, mask, code uint32) Chord{
} }
} }
func initKeys(keymap *xkb.Keymap, linux bool) { func initKeys(keymap *xkb.Keymap) {
maxCode := keymap.MaxKeycode() maxCode := keymap.MaxKeycode()
for code := keymap.MinKeycode(); code <= maxCode && code < 256; code++ { for code := keymap.MinKeycode(); code <= maxCode && code < 256; code++ {
numLevels := keymap.NumLevelsForKey(code, 0) numLevels := keymap.NumLevelsForKey(code, 0)
for level := uint32(0); level < numLevels; level++ { for level := uint32(0); level < numLevels; level++ {
for _, sym := range keymap.KeyGetSymsByLevel(code, 0, level) { for _, sym := range keymap.KeyGetSymsByLevel(code, 0, level) {
chord := newChord(keymap, keymap.KeyGetMod(code, 0, level), code) chord := newChord(keymap, keymap.KeyGetMod(code, 0, level), code)
if linux {
for name, s := range linuxXSyms { for name, s := range linuxXSyms {
if s == sym { if s == sym {
LinuxKeys[name] = chord LinuxKeys[name] = chord
} }
} }
}
XKeys[xkb.KeysymGetName(sym)] = chord XKeys[xkb.KeysymGetName(sym)] = chord
} }
} }