This commit is contained in:
lordwelch 2018-10-20 16:49:09 -07:00
parent 9d04ddc8be
commit 47d8950fd4
2 changed files with 26 additions and 33 deletions

View File

@ -1,43 +1,39 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
"timmy.narnian.us/hid/ghid"
"github.com/alexflint/go-arg"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
"timmy.narnian.us/git/timmy/hid"
) )
func main() { func main() {
var ( var (
err error SHORTCUT string
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"`
}
) )
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) 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 { if err != nil {
panic(err) panic(err)
} }
hid.Write(transform.NewReader(os.Stdin, unicode.BOMOverride(unicode.UTF8.NewDecoder()))) hid.Hidg0 = file
defer file.Close()
// if err != nil { hid.Write(os.Stdin)
// panic(err)
// } if err != nil {
panic(err)
}
fmt.Println("Success!") fmt.Println("Success!")
} }

View File

@ -62,7 +62,7 @@ var (
"RSUPER": RSUPER, "RSUPER": RSUPER,
"NONE": NONE, "NONE": NONE,
} }
Hidg0 io.WriteCloser Hidg0 io.Writer
) )
func (k Keyboard) Write(p []byte) (n int, err error) { 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 { func Write(r io.Reader) error {
_, err := io.Copy(Keyboard{}, r) _, err := io.Copy(Keyboard{}, r)
if closeCheck, ok := r.(io.Closer); ok {
closeCheck.Close()
}
return err return err
} }
@ -99,37 +96,37 @@ func write(p []byte) (n int, err error) {
} }
cur, ok := CurrentKeymap()[string(r)] cur, ok := CurrentKeymap()[string(r)]
if !ok { if !ok {
if i == 2 { if i == 2 { // can't press two keys from different keymaps
if !changeKeymap(r) && ErrOnUnknownKey { if !changeKeymap(r) && ErrOnUnknownKey {
return index, fmt.Errorf("rune not in keymap: %c", r) return index, fmt.Errorf("rune not in keymap: %c", r)
} }
} else { } 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 // Check if this is a delay
if cur.DelayDelimiter { if cur.DelayDelimiter {
index += s + parseDelay(p[index:]) 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 // Calculate next modifier byte
for _, v := range cur.Modifier { for _, v := range cur.Modifier {
mod = mod | flags[v] 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 // check if the next modifier byte is the same
if i == 2 { if i == 2 {
flag = mod flag = mod
} else if 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. // Check for duplicate key press. You can't press a key if it is already pressed.
for u := 2; u < i; u++ { for u := 2; u < i; u++ {
if cur.Decimal == report[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 report[i] = cur.Decimal