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() {
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)
}
{

11
keys.go
View file

@ -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