From 47d8950fd4419b229b3d6d7ef0957aacd07088ec Mon Sep 17 00:00:00 2001 From: lordwelch Date: Sat, 20 Oct 2018 16:49:09 -0700 Subject: [PATCH] cleanup --- cmd/main.go | 42 +++++++++++++++++++----------------------- keyboard.go | 17 +++++++---------- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 23389e0..99197d8 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,43 +1,39 @@ package main import ( + "flag" "fmt" "os" - - "github.com/alexflint/go-arg" - - "golang.org/x/text/encoding/unicode" - "golang.org/x/text/transform" - - "timmy.narnian.us/git/timmy/hid" + "timmy.narnian.us/hid/ghid" ) func main() { var ( - err error - - args struct { - SHORTCUT string `arg:"-S,help:Keymap cycle shortcut"` - PATH string `arg:"-P,help:Path to config dir default: $XDG_CONFIG_HOME"` - ORDER []string `arg:"required,positional,help:Order of keymaps"` - } + SHORTCUT string ) - args.PATH = os.ExpandEnv("$XDG_CONFIG_HOME") - arg.MustParse(&args) - hid.KeymapOrder = args.ORDER - hid.KeymapPath = args.PATH + flag.StringVar(&SHORTCUT, "shortcut", "", "Keymap cycle shortcut") + flag.StringVar(&SHORTCUT, "s", "", "Keymap cycle shortcut") + flag.StringVar(&hid.KeymapPath, "path", os.ExpandEnv("$XDG_CONFIG_HOME"), "Path to config dir default: $XDG_CONFIG_HOME") + flag.StringVar(&hid.KeymapPath, "p", os.ExpandEnv("$XDG_CONFIG_HOME"), "Path to config dir default: $XDG_CONFIG_HOME") + flag.Parse() + + hid.KeymapOrder = flag.Args() + fmt.Println(hid.KeymapPath) - hid.Hidg0, err = os.OpenFile("hidg0", os.O_APPEND|os.O_WRONLY, 0755) + file, err := os.OpenFile("/dev/hidg0", os.O_APPEND|os.O_WRONLY, 0755) if err != nil { panic(err) } - hid.Write(transform.NewReader(os.Stdin, unicode.BOMOverride(unicode.UTF8.NewDecoder()))) + hid.Hidg0 = file + defer file.Close() - // if err != nil { - // panic(err) - // } + hid.Write(os.Stdin) + + if err != nil { + panic(err) + } fmt.Println("Success!") } diff --git a/keyboard.go b/keyboard.go index eb705a4..75d9e47 100644 --- a/keyboard.go +++ b/keyboard.go @@ -62,7 +62,7 @@ var ( "RSUPER": RSUPER, "NONE": NONE, } - Hidg0 io.WriteCloser + Hidg0 io.Writer ) func (k Keyboard) Write(p []byte) (n int, err error) { @@ -71,9 +71,6 @@ func (k Keyboard) Write(p []byte) (n int, err error) { func Write(r io.Reader) error { _, err := io.Copy(Keyboard{}, r) - if closeCheck, ok := r.(io.Closer); ok { - closeCheck.Close() - } return err } @@ -99,37 +96,37 @@ func write(p []byte) (n int, err error) { } cur, ok := CurrentKeymap()[string(r)] if !ok { - if i == 2 { + if i == 2 { // can't press two keys from different keymaps if !changeKeymap(r) && ErrOnUnknownKey { return index, fmt.Errorf("rune not in keymap: %c", r) } } else { - break press // I should make a temp var to hold this Usage id to reduce unneeded processing + break } } // Check if this is a delay if cur.DelayDelimiter { index += s + parseDelay(p[index:]) - break press // I should make a temp var to hold this Usage id to reduce unneeded processing + break } // Calculate next modifier byte for _, v := range cur.Modifier { mod = mod | flags[v] } - // Set the modifier if it is the firs key otherwise + // Set the modifier if it is the first key otherwise // check if the next modifier byte is the same if i == 2 { flag = mod } else if flag != mod { - break press // I should make a temp var to hold this Usage id to reduce unneeded processing + break } // Check for duplicate key press. You can't press a key if it is already pressed. for u := 2; u < i; u++ { if cur.Decimal == report[u] { - break press // I should make a temp var to hold this Usage id to reduce unneeded processing + break press } } report[i] = cur.Decimal