Browse Source

XDR compatible build mode (changes orb format)

main
Minizbot2012 4 months ago
parent
commit
af5653f2c1
12 changed files with 175 additions and 41 deletions
  1. +0
    -41
      devices/devices.go
  2. +44
    -0
      devices/devices_json.go
  3. +40
    -0
      devices/devices_xdr.go
  4. +22
    -0
      devices/translator/jsoxdr.go
  5. BIN
     
  6. +2
    -0
      go.mod
  7. +2
    -0
      go.sum
  8. BIN
     
  9. +5
    -0
      hid/generator/gen.go
  10. +2
    -0
      hid/hid_json.go
  11. +57
    -0
      hid/hid_xdr.go
  12. +1
    -0
      module.go

+ 0
- 41
devices/devices.go View File

@ -1,15 +1,5 @@
package devices
import (
"embed"
"encoding/binary"
"encoding/json"
"io"
"io/fs"
"os"
"strings"
)
//KeyMap singular keymap
type KeyMap struct {
Keymap []uint16
@ -23,9 +13,6 @@ type KeyMaps struct {
MCount int
}
//go:embed json/*
var jsons embed.FS
type DeviceDef struct {
Backend string
IsColor bool
@ -49,31 +36,3 @@ type DeviceDef struct {
}
var DeviceTypes map[string]*DeviceDef
func init() {
DeviceTypes = make(map[string]*DeviceDef)
files, _ := fs.ReadDir(jsons, "json")
for _, file := range files {
dev := new(DeviceDef)
data, _ := jsons.ReadFile("json/" + file.Name())
json.Unmarshal(data, dev)
DeviceTypes[strings.Split(file.Name(), ".")[0]] = dev
}
}
//LoadKM Load Orbmap KM structure
func LoadKeymap(file string, dev *DeviceDef) *KeyMap {
mapped := new(KeyMap)
of, _ := os.Open(file)
defer of.Close()
mapped.Keymap = make([]uint16, dev.NumKeys)
binary.Read(of, binary.LittleEndian, mapped.Keymap)
binary.Read(of, binary.LittleEndian, mapped.Color)
return mapped
}
//SavePKMKeymap saves an orb after edit
func SaveKeymap(mapped interface{}, file io.WriteCloser) {
binary.Write(file, binary.LittleEndian, mapped)
file.Close()
}

+ 44
- 0
devices/devices_json.go View File

@ -0,0 +1,44 @@
// +build !xdr
package devices
import (
"embed"
"encoding/binary"
"encoding/json"
"io"
"io/fs"
"os"
"strings"
)
//go:embed json/*
var data embed.FS
func init() {
DeviceTypes = make(map[string]*DeviceDef)
files, _ := fs.ReadDir(data, "json")
for _, file := range files {
dev := new(DeviceDef)
data, _ := data.ReadFile("json/" + file.Name())
json.Unmarshal(data, dev)
DeviceTypes[strings.Split(file.Name(), ".")[0]] = dev
}
}
//LoadKM Load Orbmap KM structure
func LoadKeymap(file string, dev *DeviceDef) *KeyMap {
mapped := new(KeyMap)
of, _ := os.Open(file)
defer of.Close()
mapped.Keymap = make([]uint16, dev.NumKeys)
binary.Read(of, binary.LittleEndian, mapped.Keymap)
binary.Read(of, binary.LittleEndian, mapped.Color)
return mapped
}
//SavePKMKeymap saves an orb after edit
func SaveKeymap(mapped interface{}, file io.WriteCloser) {
binary.Write(file, binary.LittleEndian, mapped)
file.Close()
}

+ 40
- 0
devices/devices_xdr.go View File

@ -0,0 +1,40 @@
// +build xdr
package devices
import (
"embed"
"github.com/davecgh/go-xdr/xdr2"
"io"
"io/fs"
"os"
"strings"
)
//go:embed xdr/*
var data embed.FS
func init() {
DeviceTypes = make(map[string]*DeviceDef)
files, _ := fs.ReadDir(data, "xdr")
for _, file := range files {
dev := new(DeviceDef)
data, _ := jsons.ReadFile("xdr/" + file.Name())
xdr2.Unmarshal(data, dev)
DeviceTypes[strings.Split(file.Name(), ".")[0]] = dev
}
}
//LoadKM Load Orbmap KM structure
func LoadKeymap(file string, dev *DeviceDef) *KeyMap {
mapped := new(KeyMap)
of, _ := os.Open(file)
xdr.Unmarshal(of, KeyMap)
return mapped
}
//SavePKMKeymap saves an orb after edit
func SaveKeymap(mapped interface{}, file io.WriteCloser) {
xdr.Marshal(file, mapped)
file.Close()
}

+ 22
- 0
devices/translator/jsoxdr.go View File

@ -0,0 +1,22 @@
package main
import (
"encoding/json"
"os"
"strings"
"github.com/OrbTools/OrbCommon/devices"
xdr "github.com/davecgh/go-xdr/xdr2"
)
func main() {
files, _ := os.ReadDir("devices/json/")
for _, file := range files {
data, _ := os.ReadFile("devices/json/" + file.Name())
DevDef := &devices.DeviceDef{}
json.Unmarshal(data, DevDef)
xdo, _ := os.Create("devices/xdr/" + strings.Split(file.Name(), ".")[0] + ".bin")
xdr.Marshal(xdo, DevDef)
xdo.Close()
}
}

BIN
View File


+ 2
- 0
go.mod View File

@ -1,3 +1,5 @@
module github.com/OrbTools/OrbCommon
go 1.16
require github.com/davecgh/go-xdr v0.0.0-20161123171359-e6a2ba005892 // indirect

+ 2
- 0
go.sum View File

@ -0,0 +1,2 @@
github.com/davecgh/go-xdr v0.0.0-20161123171359-e6a2ba005892 h1:qg9VbHo1TlL0KDM0vYvBG9EY0X0Yku5WYIPoFWt8f6o=
github.com/davecgh/go-xdr v0.0.0-20161123171359-e6a2ba005892/go.mod h1:CTDl0pzVzE5DEzZhPfvhY/9sPFMQIxaJ9VAMs9AagrE=

BIN
View File


+ 5
- 0
hid/generator/gen.go View File

@ -7,6 +7,8 @@ import (
"os"
"regexp"
"strconv"
xdr "github.com/davecgh/go-xdr/xdr2"
)
type KeyMaps struct {
@ -78,7 +80,10 @@ func main() {
}
KeyMaps.Arr = Arr
out, _ := os.Create("hid/generated.json")
xdo, _ := os.Create("hid/generated.bin")
defer xdo.Close()
defer out.Close()
jso, _ := json.Marshal(KeyMaps)
xdr.Marshal(xdo, KeyMaps)
out.Write(jso)
}

hid/hid.go → hid/hid_json.go View File

@ -1,3 +1,5 @@
// +build !xdr
package hid
import (

+ 57
- 0
hid/hid_xdr.go View File

@ -0,0 +1,57 @@
// +build xdr
package hid
import (
"bytes"
_ "embed"
xdr "github.com/davecgh/go-xdr/xdr2"
)
type KeyMaps struct {
Usb map[uint16]Key
Evdev map[uint16]Key
Xkb map[uint16]Key
Win map[uint16]Key
Mac map[uint16]Key
Code map[string]Key
Arr []Key
}
type Key struct {
Usb uint16
Evdev uint16
Xkb uint16
Win uint16
Mac uint16
Code string
}
var Mappings KeyMaps = KeyMaps{}
//go:embed generated.bin
var file []byte
func init() {
xdr.Unmarshal(bytes.NewReader(file), Mappings)
}
func GetMappingFromHID(uv uint16) Key {
return Mappings.Usb[uv]
}
func GetMappingFromWindows(uv uint16) Key {
return Mappings.Win[uv]
}
func GetMappingFromLinux(uv uint16) Key {
return Mappings.Evdev[uv]
}
func GetMappingFromName(name string) Key {
return Mappings.Code[name]
}
func GetMappingFromX(code uint16) Key {
return Mappings.Xkb[code]
}

+ 1
- 0
module.go View File

@ -1,3 +1,4 @@
package OrbCommon
//go:generate go run hid/generator/gen.go
//go:generate go run devices/translator/jsoxdr.go

Loading…
Cancel
Save