diff --git a/cmd/joyful/main.go b/cmd/joyful/main.go index 94d71a9..735b4d5 100644 --- a/cmd/joyful/main.go +++ b/cmd/joyful/main.go @@ -2,8 +2,10 @@ package main import ( "fmt" + "maps" "os" "path/filepath" + "slices" "time" "git.annabunches.net/annabunches/joyful/internal/config" @@ -23,6 +25,10 @@ func readConfig() *config.ConfigParser { func initVirtualDevices(config *config.ConfigParser) map[string]*virtualdevice.EventBuffer { 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) for name, device := range vDevices { vBuffers[name] = virtualdevice.NewEventBuffer(device) @@ -30,6 +36,14 @@ func initVirtualDevices(config *config.ConfigParser) map[string]*virtualdevice.E 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() { // parse configs config := readConfig() @@ -38,17 +52,20 @@ func main() { vBuffers := initVirtualDevices(config) // Initialize physical devices - // pDevices := config.ConnectPhysicalDevices() + pDevices := initPhysicalDevices(config) // TEST CODE - pDevice, err := evdev.Open("/dev/input/event11") - logger.FatalIfError(err, "Couldn't open physical device") + testDriver(vBuffers, pDevices) +} + +func testDriver(vBuffers map[string]*virtualdevice.EventBuffer, pDevices map[string]*evdev.InputDevice) { + pDevice := slices.Collect(maps.Values(pDevices))[0] name, err := pDevice.Name() if err != nil { 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 buffer := vBuffers["main"] @@ -63,31 +80,25 @@ func main() { switch event.Code { case evdev.BTN_TRIGGER: if event.Value == 0 { - fmt.Println("Trigger 0") combo++ } if event.Value == 1 { - fmt.Println("Trigger 1") combo-- } case evdev.BTN_THUMB: if event.Value == 0 { - fmt.Println("Thumb 0") combo-- } if event.Value == 1 { - fmt.Println("Thumb 1") combo++ } case evdev.BTN_THUMB2: if event.Value == 0 { - fmt.Println("Thumb2 0") combo-- } if event.Value == 1 { - fmt.Println("Thumb2 1") combo++ } diff --git a/internal/config/configparser.go b/internal/config/configparser.go index b837d66..e4f278b 100644 --- a/internal/config/configparser.go +++ b/internal/config/configparser.go @@ -73,8 +73,9 @@ func (parser *ConfigParser) CreateVirtualDevices() map[string]*evdev.InputDevice continue } - vDevice, err := evdev.CreateDevice( - fmt.Sprintf("joyful-%s", deviceConfig.Name), + name := fmt.Sprintf("joyful-%s", deviceConfig.Name) + device, err := evdev.CreateDevice( + name, // TODO: who knows what these should actually be evdev.InputID{ BusType: 0x03, @@ -93,7 +94,8 @@ func (parser *ConfigParser) CreateVirtualDevices() map[string]*evdev.InputDevice continue } - deviceMap[deviceConfig.Name] = vDevice + deviceMap[deviceConfig.Name] = device + logger.Log(fmt.Sprintf("Created virtual device '%s'", name)) } return deviceMap @@ -116,14 +118,16 @@ func (parser *ConfigParser) ConnectPhysicalDevices() map[string]*evdev.InputDevi continue } - vDevice, err := evdev.Open("/dev/input/foo") - + device, err := evdev.OpenByName(deviceConfig.DeviceName) 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 } - 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 diff --git a/internal/config/schema.go b/internal/config/schema.go index 43e95b8..a033cad 100644 --- a/internal/config/schema.go +++ b/internal/config/schema.go @@ -11,11 +11,12 @@ type Config struct { } type DeviceConfig struct { - Name string `yaml:"name"` - Type string `yaml:"type"` - Uuid string `yaml:"uuid,omitempty"` - Buttons int `yaml:"buttons,omitempty"` - Axes int `yaml:"axes,omitempty"` + Name string `yaml:"name"` + Type string `yaml:"type"` + DeviceName string `yaml:"device_name,omitempty"` + Uuid string `yaml:"uuid,omitempty"` + Buttons int `yaml:"buttons,omitempty"` + Axes int `yaml:"axes,omitempty"` } type RuleConfig struct {