2
0
mirror of https://github.com/OrbTools/OrbMap synced 2024-11-15 18:13:58 +00:00

Major refactor and code rebuild

Main new feature is the idea of "Backends"
This allows customization beyond what is already available
Devices can now be defined in a json file as part of OrbCommon
This commit is contained in:
Minizbot2012 2021-07-24 15:53:04 -04:00
parent 838a8806ad
commit 9c905fab16
No known key found for this signature in database
GPG Key ID: 977C8ADE12361917
10 changed files with 71 additions and 68 deletions

View File

@ -0,0 +1,42 @@
package keypad
import (
"fmt"
"path/filepath"
"github.com/OrbTools/OrbCommon/devices"
"github.com/OrbTools/OrbMap/registry"
)
type Keypad struct {
eventcodes []byte
ecm map[uint16]int
keymaps *devices.KeyMaps
definition *devices.DeviceDef
}
//ProbcOrbFiles processes orbs
func (p *Keypad) ProcOrbs(dev *devices.DeviceDef, orbs []string) {
p.definition = dev
p.keymaps = &devices.KeyMaps{Currentmap: 0}
if len(orbs) > 0 {
for _, orb := range orbs {
abs, _ := filepath.Abs(orb)
fmt.Println("Loading Orb " + abs)
KMap := devices.LoadKeymap(abs, dev)
p.keymaps.Maps = append(p.keymaps.Maps, KMap)
}
p.keymaps.MCount = len(orbs)
} else {
panic("No orbs")
}
p.ecm = make(map[uint16]int)
p.eventcodes = p.definition.Binding
for i := 0; i < len(p.eventcodes); i++ {
p.ecm[uint16(p.eventcodes[i])] = i
}
}
func init() {
registry.Systems["keypad"] = registry.Device(&Keypad{})
}

View File

@ -1,6 +1,5 @@
// +build linux !windows
package orbweaver
package keypad
import (
"bytes"
@ -12,18 +11,15 @@ import (
)
//OrbLoop Main loop for this device
func (p *Orbweaver) OrbLoop(KeyBus chan *keyevents.KeyEvent) {
func (p *Keypad) OrbLoop(KeyBus chan *keyevents.KeyEvent) {
println("UnixLoop starting")
f, err := evdev.Open("/dev/input/by-id/usb-Razer_Razer_Orbweaver_Chroma-event-kbd")
if err != nil {
panic(err)
}
f, _ := evdev.Open(p.definition.Device.SystemFile)
f.Grab()
var evsize = int(unsafe.Sizeof(keyevents.KeyEvent{}))
b := make([]byte, evsize)
for {
f.File.Read(b)
KeyEv := &keyevents.KeyEvent{}
KeyEv := new(keyevents.KeyEvent)
binary.Read(bytes.NewBuffer(b), binary.LittleEndian, KeyEv)
KeyEv.Code = p.keymaps.Maps[p.keymaps.Currentmap].Keymap[p.ecm[KeyEv.Code]]
if KeyEv.Code != 0 && KeyEv.Type != 4 {

View File

@ -1,6 +1,6 @@
// +build windows
package orbweaver
package keypad
import (
"bytes"
@ -13,8 +13,6 @@ import (
)
const (
vendor = gousb.ID(0x1532)
prod = gousb.ID(0x0207)
leftControl byte = 0x1
leftShift byte = 0x2
leftAlt byte = 0x4
@ -100,10 +98,10 @@ func (s *swapInt) Differ(s2 *swapInt) []byte {
}
//OrbLoop Main loop for this device
func (p *Orbweaver) OrbLoop(KeyBus chan *keyevents.KeyEvent) {
func (p *Keypad) OrbLoop(KeyBus chan *keyevents.KeyEvent) {
fmt.Println("Windows Loop Init")
ctx := gousb.NewContext()
dev, err := ctx.OpenDeviceWithVIDPID(vendor, prod)
dev, err := ctx.OpenDeviceWithVIDPID(gousb.ID(p.definition.Device.VendorID), gousb.ID(p.definition.Device.ProdID))
if err != nil {
panic(err)
}

View File

@ -1,5 +1,5 @@
package main
import (
_ "github.com/OrbTools/OrbMap/devices/orbweaver"
_ "github.com/OrbTools/OrbMap/backend/keypad"
)

View File

@ -1,43 +0,0 @@
package orbweaver
import (
"fmt"
"path/filepath"
morb "github.com/OrbTools/OrbCommon/devices/orbweaver"
"github.com/OrbTools/OrbMap/registry"
)
type Orbweaver struct {
eventcodes []byte
ecm map[uint16]int
keymaps *morb.KeyMaps
}
//ProbcOrbFiles processes orbs
func (p *Orbweaver) ProcOrbs(orbs []string) {
p.keymaps = &morb.KeyMaps{Currentmap: 0}
if len(orbs) > 0 {
for idx, orb := range orbs {
abs, err := filepath.Abs(orb)
if err != nil {
panic(err)
}
fmt.Println("Loading Orb " + abs)
KMap := morb.LoadKM(abs)
p.keymaps.Maps[idx] = KMap
}
p.keymaps.MCount = len(orbs)
} else {
panic("No orbs")
}
p.ecm = make(map[uint16]int)
p.eventcodes = morb.BINDING[:]
for i := 0; i < len(p.eventcodes); i++ {
p.ecm[uint16(p.eventcodes[i])] = i
}
}
func init() {
registry.Systems["orbweaver"] = registry.Device(&Orbweaver{})
}

View File

@ -8,8 +8,8 @@ import (
func main() {
out, _ := os.Create("boot.go")
out.Write([]byte("package main\n\nimport (\n"))
files, _ := ioutil.ReadDir("./devices/")
BasePkg := "github.com/OrbTools/OrbMap/devices/"
files, _ := ioutil.ReadDir("./backend/")
BasePkg := "github.com/OrbTools/OrbMap/backend/"
for _, fil := range files {
if fil.IsDir() {
out.Write([]byte("\t_ \"" + BasePkg + fil.Name() + "\"\n"))

2
go.mod
View File

@ -3,7 +3,7 @@ module github.com/OrbTools/OrbMap
go 1.16
require (
github.com/OrbTools/OrbCommon v0.0.0-20210724065833-4ff2abe0a12c
github.com/OrbTools/OrbCommon v0.0.0-20210724194347-38cc36047c7d
github.com/bendahl/uinput v1.4.1
github.com/google/gousb v1.1.1
github.com/gvalkov/golang-evdev v0.0.0-20191114124502-287e62b94bcb

4
go.sum
View File

@ -1,5 +1,5 @@
github.com/OrbTools/OrbCommon v0.0.0-20210724065833-4ff2abe0a12c h1:oTZtQVfLPB3ef+m8NVY8pnvAZgrvMRyr6u21HdkRa9I=
github.com/OrbTools/OrbCommon v0.0.0-20210724065833-4ff2abe0a12c/go.mod h1:8HEhD7wF9Fqritt/jYxDmjbxEScWhFfVajdYdPO5Y14=
github.com/OrbTools/OrbCommon v0.0.0-20210724194347-38cc36047c7d h1:v3tlCft2Rbg3Fojm+bKWnkMjmABiMhGeL+wVAcSTp8Q=
github.com/OrbTools/OrbCommon v0.0.0-20210724194347-38cc36047c7d/go.mod h1:8HEhD7wF9Fqritt/jYxDmjbxEScWhFfVajdYdPO5Y14=
github.com/bendahl/uinput v1.4.1 h1:ecxSLcVxWk0EFyZBtmCTnOKjK/HCNdsUcWXRTkNt06k=
github.com/bendahl/uinput v1.4.1/go.mod h1:Np7w3DINc9wB83p12fTAM3DPPhFnAKP0WTXRqCQJ6Z8=
github.com/google/gousb v1.1.1 h1:2sjwXlc0PIBgDnXtNxUrHcD/RRFOmAtRq4QgnFBE6xc=

12
main.go
View File

@ -5,23 +5,25 @@ import (
"flag"
"strings"
"github.com/OrbTools/OrbCommon/devices"
"github.com/OrbTools/OrbMap/emu"
"github.com/OrbTools/OrbMap/keyevents"
"github.com/OrbTools/OrbMap/registry"
)
//go:generate go run generators/devices.go
//go:generate go run generators/backends.go
func main() {
str := make(map[string]*string)
for d := range registry.Systems {
str[d] = flag.String(d, "", "Comma seperated list of orb files for "+d)
for d, dev := range devices.DeviceTypes {
str[d] = flag.String(d, "", "Comma seperated list of orb files for "+d+" "+dev.Backend)
}
flag.Parse()
KeyBus := make(chan *keyevents.KeyEvent, 128)
for sys, orbs := range str {
if len(*orbs) > 0 {
registry.Systems[sys].ProcOrbs(strings.Split(*orbs, ","))
go registry.Systems[sys].OrbLoop(KeyBus)
devh := registry.NewOf(devices.DeviceTypes[sys].Backend)
devh.ProcOrbs(devices.DeviceTypes[sys], strings.Split(*orbs, ","))
go devh.OrbLoop(KeyBus)
}
}
emu.ProcKey(KeyBus)

View File

@ -1,6 +1,9 @@
package registry
import (
"reflect"
"github.com/OrbTools/OrbCommon/devices"
"github.com/OrbTools/OrbMap/keyevents"
)
@ -10,9 +13,14 @@ var (
type Device interface {
OrbLoop(chan *keyevents.KeyEvent)
ProcOrbs([]string)
ProcOrbs(*devices.DeviceDef, []string)
}
func init() {
Systems = make(map[string]Device)
}
func NewOf(name string) Device {
nInter := reflect.New(reflect.ValueOf(Systems[name]).Type().Elem())
return nInter.Interface().(Device)
}