hid/main.go

146 lines
2.8 KiB
Go
Raw Normal View History

2017-10-26 13:51:49 -07:00
package main
import (
"bufio"
2017-10-26 16:50:36 -07:00
"encoding/binary"
"encoding/json"
2017-11-06 20:53:22 -08:00
"fmt"
2017-10-31 13:06:45 -07:00
"io"
"io/ioutil"
2017-10-26 13:51:49 -07:00
"os"
"path"
"strings"
"github.com/alexflint/go-arg"
2017-10-26 13:51:49 -07:00
)
type Key struct {
modifier string
decimal int
}
2017-11-06 21:52:49 -08:00
type Keys map[string]Key
2017-11-06 20:53:22 -08:00
type Args struct {
SHORTCUT string `arg:"-S,help:Keymap cycle shortcut"`
2017-11-06 21:09:55 -08:00
ORDER []string `arg:"positional,help:Order of keymaps"`
2017-11-06 20:53:22 -08:00
}
2017-10-26 16:05:49 -07:00
const (
LCTRL byte = 1 << iota
LSHIFT
LALT
LSUPER
RCTRL
RSHIFT
RALT
RSUPER
)
2017-10-31 13:06:45 -07:00
func Press(press [8]byte, file io.Writer) {
binary.Write(file, binary.BigEndian, press[:])
binary.Write(file, binary.BigEndian, [8]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
2017-11-06 22:00:02 -08:00
fmt.Println(press)
fmt.Println([8]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
}
func Hold(press [8]byte, file io.Writer) {
binary.Write(file, binary.BigEndian, press[:])
2017-10-31 13:06:45 -07:00
}
2017-11-06 20:53:22 -08:00
func changeKeymap(r rune, keys map[string]Keys, args Args, hidg0 *os.File, currentKeyMap *int) {
2017-11-06 21:09:55 -08:00
fmt.Println(*currentKeyMap)
2017-11-06 21:22:44 -08:00
fmt.Println(args)
2017-11-06 21:24:46 -08:00
kmap := args.ORDER[(*currentKeyMap)]
fmt.Println(kmap)
2017-11-06 21:52:49 -08:00
for keys[kmap][string(r)].decimal != 0 {
2017-11-06 20:53:22 -08:00
Press([8]byte{LCTRL, 0x00, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00}, hidg0)
*currentKeyMap++
if *currentKeyMap == len(keys) {
panic("key not in keymap")
}
}
}
2017-10-26 16:05:49 -07:00
func main() {
2017-10-26 13:51:49 -07:00
var (
2017-11-06 20:53:22 -08:00
args Args
hidg0 *os.File
err error
keymapsF []os.FileInfo
2017-11-06 21:21:15 -08:00
keys = make(map[string]Keys)
2017-11-06 21:09:55 -08:00
cfgPath = "./" //path.Join(os.Getenv("XDG_CONFIG_HOME"), "hid")
stdin = bufio.NewReader(os.Stdin)
currentKeyMap int
2017-10-26 13:51:49 -07:00
)
arg.MustParse(&args)
keymapsF, err = ioutil.ReadDir(cfgPath)
if err != nil {
panic(err)
}
2017-11-06 21:15:27 -08:00
fmt.Println(cfgPath)
fmt.Println(keymapsF)
hidg0, err = os.OpenFile("/dev/hidg0", os.O_WRONLY, os.ModePerm)
if err != nil {
panic(err)
}
for _, file := range keymapsF {
var (
ext string
)
2017-10-31 13:06:45 -07:00
ext = path.Ext(file.Name())
2017-11-06 21:16:15 -08:00
fmt.Println(ext)
2017-11-06 21:16:59 -08:00
if strings.ToLower(ext) == ".json" {
var (
tmp Keys
T *os.File
content []byte
)
2017-11-06 21:14:09 -08:00
fmt.Println(file.Name())
T, err = os.Open(file.Name())
if err != nil {
panic(err)
}
content, err = ioutil.ReadAll(T)
if err != nil {
panic(err)
}
2017-11-06 22:03:20 -08:00
err = json.Unmarshal(content, tmp)
if err != nil {
panic(err)
}
2017-11-06 21:14:09 -08:00
fmt.Println(strings.TrimSuffix(file.Name(), ext))
keys[strings.TrimSuffix(file.Name(), ext)] = tmp
T.Close()
}
}
2017-11-06 22:03:32 -08:00
fmt.Println(keys)
2017-11-06 20:53:22 -08:00
for {
var (
r rune
flag byte
report [6]byte
)
r, _, err = stdin.ReadRune()
2017-11-06 21:58:36 -08:00
fmt.Printf("%s\n", string(r))
2017-11-06 20:53:22 -08:00
if err != nil {
panic(err)
}
2017-11-06 20:53:22 -08:00
changeKeymap(r, keys, args, hidg0, &currentKeyMap)
2017-11-06 21:52:49 -08:00
_, err = fmt.Sscanf(keys[args.ORDER[currentKeyMap]][string(r)].modifier, "%b", flag)
binary.PutVarint(report[:], int64(keys[args.ORDER[currentKeyMap]][string(r)].decimal))
2017-11-06 20:53:22 -08:00
Press([8]byte{flag, 0, report[0], report[1], report[2], report[3], report[4], report[5]}, hidg0)
2017-10-27 13:14:03 -07:00
}
hidg0.Close()
2017-10-27 13:14:03 -07:00
2017-10-26 13:51:49 -07:00
}