Implement config file parsing for physical devices.
This commit is contained in:
parent
faa51bdda2
commit
5b3b70da14
3 changed files with 38 additions and 22 deletions
|
@ -2,8 +2,10 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"maps"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.annabunches.net/annabunches/joyful/internal/config"
|
"git.annabunches.net/annabunches/joyful/internal/config"
|
||||||
|
@ -23,6 +25,10 @@ func readConfig() *config.ConfigParser {
|
||||||
|
|
||||||
func initVirtualDevices(config *config.ConfigParser) map[string]*virtualdevice.EventBuffer {
|
func initVirtualDevices(config *config.ConfigParser) map[string]*virtualdevice.EventBuffer {
|
||||||
vDevices := config.CreateVirtualDevices()
|
vDevices := config.CreateVirtualDevices()
|
||||||
|
if len(vDevices) == 0 {
|
||||||
|
logger.Log("Warning: no virtual devices found in configuration. No rules will work.")
|
||||||
|
}
|
||||||
|
|
||||||
vBuffers := make(map[string]*virtualdevice.EventBuffer)
|
vBuffers := make(map[string]*virtualdevice.EventBuffer)
|
||||||
for name, device := range vDevices {
|
for name, device := range vDevices {
|
||||||
vBuffers[name] = virtualdevice.NewEventBuffer(device)
|
vBuffers[name] = virtualdevice.NewEventBuffer(device)
|
||||||
|
@ -30,6 +36,14 @@ func initVirtualDevices(config *config.ConfigParser) map[string]*virtualdevice.E
|
||||||
return vBuffers
|
return vBuffers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initPhysicalDevices(config *config.ConfigParser) map[string]*evdev.InputDevice {
|
||||||
|
pDeviceMap := config.ConnectPhysicalDevices()
|
||||||
|
if len(pDeviceMap) == 0 {
|
||||||
|
logger.Log("Warning: no physical devices found in configuration. No rules will work.")
|
||||||
|
}
|
||||||
|
return pDeviceMap
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// parse configs
|
// parse configs
|
||||||
config := readConfig()
|
config := readConfig()
|
||||||
|
@ -38,17 +52,20 @@ func main() {
|
||||||
vBuffers := initVirtualDevices(config)
|
vBuffers := initVirtualDevices(config)
|
||||||
|
|
||||||
// Initialize physical devices
|
// Initialize physical devices
|
||||||
// pDevices := config.ConnectPhysicalDevices()
|
pDevices := initPhysicalDevices(config)
|
||||||
|
|
||||||
// TEST CODE
|
// TEST CODE
|
||||||
pDevice, err := evdev.Open("/dev/input/event11")
|
testDriver(vBuffers, pDevices)
|
||||||
logger.FatalIfError(err, "Couldn't open physical device")
|
}
|
||||||
|
|
||||||
|
func testDriver(vBuffers map[string]*virtualdevice.EventBuffer, pDevices map[string]*evdev.InputDevice) {
|
||||||
|
pDevice := slices.Collect(maps.Values(pDevices))[0]
|
||||||
|
|
||||||
name, err := pDevice.Name()
|
name, err := pDevice.Name()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
name = "Unknown"
|
name = "Unknown"
|
||||||
}
|
}
|
||||||
fmt.Printf("Connected to physical device %s\n", name)
|
fmt.Printf("Test Driver using physical device %s\n", name)
|
||||||
|
|
||||||
var combo int32 = 0
|
var combo int32 = 0
|
||||||
buffer := vBuffers["main"]
|
buffer := vBuffers["main"]
|
||||||
|
@ -63,31 +80,25 @@ func main() {
|
||||||
switch event.Code {
|
switch event.Code {
|
||||||
case evdev.BTN_TRIGGER:
|
case evdev.BTN_TRIGGER:
|
||||||
if event.Value == 0 {
|
if event.Value == 0 {
|
||||||
fmt.Println("Trigger 0")
|
|
||||||
combo++
|
combo++
|
||||||
}
|
}
|
||||||
if event.Value == 1 {
|
if event.Value == 1 {
|
||||||
fmt.Println("Trigger 1")
|
|
||||||
combo--
|
combo--
|
||||||
}
|
}
|
||||||
|
|
||||||
case evdev.BTN_THUMB:
|
case evdev.BTN_THUMB:
|
||||||
if event.Value == 0 {
|
if event.Value == 0 {
|
||||||
fmt.Println("Thumb 0")
|
|
||||||
combo--
|
combo--
|
||||||
}
|
}
|
||||||
if event.Value == 1 {
|
if event.Value == 1 {
|
||||||
fmt.Println("Thumb 1")
|
|
||||||
combo++
|
combo++
|
||||||
}
|
}
|
||||||
|
|
||||||
case evdev.BTN_THUMB2:
|
case evdev.BTN_THUMB2:
|
||||||
if event.Value == 0 {
|
if event.Value == 0 {
|
||||||
fmt.Println("Thumb2 0")
|
|
||||||
combo--
|
combo--
|
||||||
}
|
}
|
||||||
if event.Value == 1 {
|
if event.Value == 1 {
|
||||||
fmt.Println("Thumb2 1")
|
|
||||||
combo++
|
combo++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -73,8 +73,9 @@ func (parser *ConfigParser) CreateVirtualDevices() map[string]*evdev.InputDevice
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
vDevice, err := evdev.CreateDevice(
|
name := fmt.Sprintf("joyful-%s", deviceConfig.Name)
|
||||||
fmt.Sprintf("joyful-%s", deviceConfig.Name),
|
device, err := evdev.CreateDevice(
|
||||||
|
name,
|
||||||
// TODO: who knows what these should actually be
|
// TODO: who knows what these should actually be
|
||||||
evdev.InputID{
|
evdev.InputID{
|
||||||
BusType: 0x03,
|
BusType: 0x03,
|
||||||
|
@ -93,7 +94,8 @@ func (parser *ConfigParser) CreateVirtualDevices() map[string]*evdev.InputDevice
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
deviceMap[deviceConfig.Name] = vDevice
|
deviceMap[deviceConfig.Name] = device
|
||||||
|
logger.Log(fmt.Sprintf("Created virtual device '%s'", name))
|
||||||
}
|
}
|
||||||
|
|
||||||
return deviceMap
|
return deviceMap
|
||||||
|
@ -116,14 +118,16 @@ func (parser *ConfigParser) ConnectPhysicalDevices() map[string]*evdev.InputDevi
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
vDevice, err := evdev.Open("/dev/input/foo")
|
device, err := evdev.OpenByName(deviceConfig.DeviceName)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.LogIfError(err, "Failed to open physical device")
|
logger.LogError(err, "Failed to open physical device, skipping. Confirm the device name with 'evtest'")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
deviceMap[deviceConfig.Name] = vDevice
|
// TODO: grab exclusive access to device
|
||||||
|
|
||||||
|
logger.Log(fmt.Sprintf("Connected to '%s' as '%s'", deviceConfig.DeviceName, deviceConfig.Name))
|
||||||
|
deviceMap[deviceConfig.Name] = device
|
||||||
}
|
}
|
||||||
|
|
||||||
return deviceMap
|
return deviceMap
|
||||||
|
|
|
@ -13,6 +13,7 @@ type Config struct {
|
||||||
type DeviceConfig struct {
|
type DeviceConfig struct {
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
Type string `yaml:"type"`
|
Type string `yaml:"type"`
|
||||||
|
DeviceName string `yaml:"device_name,omitempty"`
|
||||||
Uuid string `yaml:"uuid,omitempty"`
|
Uuid string `yaml:"uuid,omitempty"`
|
||||||
Buttons int `yaml:"buttons,omitempty"`
|
Buttons int `yaml:"buttons,omitempty"`
|
||||||
Axes int `yaml:"axes,omitempty"`
|
Axes int `yaml:"axes,omitempty"`
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue