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) {

View file

@ -1,4 +1,4 @@
package config
package configparser
import (
"fmt"

View file

@ -1,4 +1,4 @@
package config
package configparser
import (
"fmt"

View file

@ -10,7 +10,7 @@
//
// nb: there are methods defined on ConfigParser in other files in this package!
package config
package configparser
import (
"errors"
@ -75,3 +75,59 @@ func (parser *ConfigParser) GetModes() []string {
}
return parser.config.Modes
}
func ParseConfig(directory string) (*Config, error) {
config := new(Config)
configFiles, err := getConfigFilePaths(directory)
if err != nil {
return nil, err
}
// Open each yaml file and add its contents to the global config
for _, filePath := range configFiles {
data, err := os.ReadFile(filePath)
if err != nil {
logger.LogError(err, "Error while opening config file")
continue
}
newConfig := Config{}
err = yaml.Unmarshal(data, &newConfig)
logger.LogIfError(err, "Error parsing YAML")
config.Rules = append(config.Rules, newConfig.Rules...)
config.Devices = append(config.Devices, newConfig.Devices...)
config.Modes = append(config.Modes, newConfig.Modes...)
}
if len(config.Devices) == 0 {
return nil, errors.New("Found no devices in configuration. Please add configuration at " + directory)
}
return config, nil
}
func getConfigFilePaths(directory string) ([]string, error) {
paths := make([]string, 0)
dirEntries, err := os.ReadDir(directory)
if err != nil {
err = os.Mkdir(directory, 0755)
if err != nil {
return nil, errors.New("failed to create config directory at " + directory)
} else {
return nil, errors.New("no config files found at " + directory)
}
}
for _, file := range dirEntries {
name := strings.ToLower(file.Name())
if file.IsDir() || !(strings.HasSuffix(name, ".yaml") || strings.HasSuffix(name, ".yml")) {
continue
}
paths = append(paths, filepath.Join(directory, file.Name()))
}
return paths, nil
}

View file

@ -1,4 +1,4 @@
package config
package configparser
import (
"fmt"

View file

@ -1,4 +1,4 @@
package config
package configparser
import (
"testing"

View file

@ -1,4 +1,4 @@
package config
package configparser
import "github.com/holoplot/go-evdev"

View file

@ -1,4 +1,4 @@
package config
package configparser
import (
"errors"

View file

@ -1,4 +1,4 @@
package config
package configparser
import (
"fmt"

View file

@ -1,4 +1,4 @@
package config
package configparser
import (
"fmt"

View file

@ -1,4 +1,4 @@
package config
package configparser
import "slices"

View file

@ -1,7 +1,7 @@
// These types comprise the YAML schema for configuring Joyful.
// The config files will be combined and then unmarshalled into this
package config
package configparser
import (
"fmt"

View file

@ -1,4 +1,4 @@
package config
package configparser
import (
"github.com/holoplot/go-evdev"