From 72c447dfb8282137c036d5f153e8aee28b33cc54 Mon Sep 17 00:00:00 2001 From: lordwelch Date: Mon, 6 Nov 2017 21:53:22 -0700 Subject: [PATCH] Initial app done --- main.go | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index ff00e6d..f37fe96 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "bufio" "encoding/binary" "encoding/json" + "fmt" "io" "io/ioutil" "os" @@ -21,6 +22,11 @@ type Key struct { type Keys []Key +type Args struct { + SHORTCUT string `arg:"-S,help:Keymap cycle shortcut"` + ORDER []string `arg:positional,help:Order of keymaps` +} + const ( LCTRL byte = 1 << iota LSHIFT @@ -41,12 +47,19 @@ func Hold(press [8]byte, file io.Writer) { binary.Write(file, binary.BigEndian, press[:]) } +func changeKeymap(r rune, keys map[string]Keys, args Args, hidg0 *os.File, currentKeyMap *int) { + for keys[args.ORDER[(*currentKeyMap)]][r].name != r { + Press([8]byte{LCTRL, 0x00, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00}, hidg0) + *currentKeyMap++ + if *currentKeyMap == len(keys) { + panic("key not in keymap") + } + } +} + func main() { var ( - args struct { - SHORTCUT string `arg:"-S,help:Keymap cycle shortcut"` - ORDER []string `arg:positional,help:Order of keymaps` - } + args Args hidg0 *os.File err error keymapsF []os.FileInfo @@ -54,7 +67,6 @@ func main() { cfgPath = path.Join(os.Getenv("XDG_CONFIG_HOME"), "hid") stdin = bufio.NewReader(os.Stdin) currentKeyMap int - good bool ) arg.MustParse(&args) keymapsF, err = ioutil.ReadDir(cfgPath) @@ -94,13 +106,21 @@ func main() { T.Close() } } - for good { - var r rune + for { + var ( + r rune + flag byte + report [6]byte + ) + r, _, err = stdin.ReadRune() - for keys[args.ORDER[currentKeyMap]][r].name != r { - Press([8]byte{LCTRL, 0x00, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00}, hidg0) - currentKeyMap++ + if err != nil { + panic(err) } + changeKeymap(r, keys, args, hidg0, ¤tKeyMap) + _, err = fmt.Sscanf(keys[args.ORDER[currentKeyMap]][r].modifier, "%b", flag) + binary.PutVarint(report[:], int64(keys[args.ORDER[currentKeyMap]][r].decimal)) + Press([8]byte{flag, 0, report[0], report[1], report[2], report[3], report[4], report[5]}, hidg0) }