Begin to overhaul config to couple initialization logic closer to the structs themselves.

This commit is contained in:
Anna Rose Wiggins 2025-08-11 12:38:07 -04:00
parent d9babf5dc0
commit 1b374bccc6
15 changed files with 122 additions and 33 deletions

View file

@ -5,7 +5,7 @@ import (
"slices"
// TODO: using config here feels like bad coupling... ButtonFromIndex might need a refactor / move
"git.annabunches.net/annabunches/joyful/internal/config"
"git.annabunches.net/annabunches/joyful/internal/configparser"
"git.annabunches.net/annabunches/joyful/internal/logger"
"github.com/holoplot/go-evdev"
flag "github.com/spf13/pflag"
@ -20,7 +20,7 @@ func isJoystickLike(device *evdev.InputDevice) bool {
if slices.Contains(types, evdev.EV_KEY) {
buttons := device.CapableEvents(evdev.EV_KEY)
for _, code := range config.ButtonFromIndex {
for _, code := range configparser.ButtonFromIndex {
if slices.Contains(buttons, code) {
return true
}

47
cmd/joyful/device_init.go Normal file
View file

@ -0,0 +1,47 @@
package main
import (
"git.annabunches.net/annabunches/joyful/internal/configparser"
"git.annabunches.net/annabunches/joyful/internal/logger"
"github.com/holoplot/go-evdev"
)
func initPhysicalDevices(conf *configparser.Config) map[string]*evdev.InputDevice {
pDeviceMap := make(map[string]*evdev.InputDevice)
for _, devConfig := range conf.Devices {
if devConfig.Type != configparser.DeviceTypePhysical {
continue
}
name, device, err := initPhysicalDevice(devConfig.Config.(configparser.DeviceConfigPhysical))
if err != nil {
logger.LogError(err, "Failed to initialize device")
}
pDeviceMap[name] = device
}
if len(pDeviceMap) == 0 {
logger.Log("Warning: no physical devices found in configuration. No rules will work.")
}
return pDeviceMap
}
func initPhysicalDevice(config configparser.DeviceConfigPhysical) (string, *evdev.InputDevice, error) {
name := config.Name
var device *evdev.InputDevice
var err error
if config.DevicePath != "" {
device, err = evdev.Open(config.DevicePath)
} else {
device, err = evdev.OpenByName(config.DeviceName)
}
if config.Lock && err == nil {
grabErr := device.Grab()
logger.LogIfError(grabErr, "Failed to lock device for exclusive access")
}
return name, device, err
}

View file

@ -10,7 +10,7 @@ import (
"github.com/holoplot/go-evdev"
flag "github.com/spf13/pflag"
"git.annabunches.net/annabunches/joyful/internal/config"
"git.annabunches.net/annabunches/joyful/internal/configparser"
"git.annabunches.net/annabunches/joyful/internal/logger"
"git.annabunches.net/annabunches/joyful/internal/mappingrules"
"git.annabunches.net/annabunches/joyful/internal/virtualdevice"
@ -21,14 +21,7 @@ func getConfigDir(dir string) string {
return os.ExpandEnv(configDir)
}
func readConfig(configDir string) *config.ConfigParser {
parser := &config.ConfigParser{}
err := parser.Parse(configDir)
logger.FatalIfError(err, "Failed to parse config")
return parser
}
func initVirtualBuffers(config *config.ConfigParser) (map[string]*evdev.InputDevice,
func initVirtualBuffers(config *configparser.ConfigParser) (map[string]*evdev.InputDevice,
map[string]*virtualdevice.EventBuffer,
map[*evdev.InputDevice]*virtualdevice.EventBuffer) {
@ -46,14 +39,6 @@ func initVirtualBuffers(config *config.ConfigParser) (map[string]*evdev.InputDev
return vDevices, vBuffersByName, vBuffersByDevice
}
func initPhysicalDevices(config *config.ConfigParser) map[string]*evdev.InputDevice {
pDeviceMap := config.InitPhysicalDevices()
if len(pDeviceMap) == 0 {
logger.Log("Warning: no physical devices found in configuration. No rules will work.")
}
return pDeviceMap
}
func main() {
// parse command-line
var configFlag string
@ -64,7 +49,8 @@ func main() {
// parse configs
configDir := getConfigDir(configFlag)
config := readConfig(configDir)
config, err := configparser.ParseConfig(configDir)
logger.FatalIfError(err, "Failed to parse configuration")
// initialize TTS
tts, err := newTTS(ttsOps)
@ -144,7 +130,7 @@ func main() {
}
func loadRules(
config *config.ConfigParser,
config *configparser.ConfigParser,
pDevices map[string]*evdev.InputDevice,
vDevices map[string]*evdev.InputDevice) ([]mappingrules.MappingRule, <-chan ChannelEvent, func(), *sync.WaitGroup) {