Begin to overhaul config to couple initialization logic closer to the structs themselves.
This commit is contained in:
parent
d9babf5dc0
commit
1b374bccc6
15 changed files with 122 additions and 33 deletions
|
@ -5,7 +5,7 @@ import (
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
// TODO: using config here feels like bad coupling... ButtonFromIndex might need a refactor / move
|
// 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"
|
"git.annabunches.net/annabunches/joyful/internal/logger"
|
||||||
"github.com/holoplot/go-evdev"
|
"github.com/holoplot/go-evdev"
|
||||||
flag "github.com/spf13/pflag"
|
flag "github.com/spf13/pflag"
|
||||||
|
@ -20,7 +20,7 @@ func isJoystickLike(device *evdev.InputDevice) bool {
|
||||||
if slices.Contains(types, evdev.EV_KEY) {
|
if slices.Contains(types, evdev.EV_KEY) {
|
||||||
buttons := device.CapableEvents(evdev.EV_KEY)
|
buttons := device.CapableEvents(evdev.EV_KEY)
|
||||||
|
|
||||||
for _, code := range config.ButtonFromIndex {
|
for _, code := range configparser.ButtonFromIndex {
|
||||||
if slices.Contains(buttons, code) {
|
if slices.Contains(buttons, code) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
47
cmd/joyful/device_init.go
Normal file
47
cmd/joyful/device_init.go
Normal 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
|
||||||
|
}
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"github.com/holoplot/go-evdev"
|
"github.com/holoplot/go-evdev"
|
||||||
flag "github.com/spf13/pflag"
|
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/logger"
|
||||||
"git.annabunches.net/annabunches/joyful/internal/mappingrules"
|
"git.annabunches.net/annabunches/joyful/internal/mappingrules"
|
||||||
"git.annabunches.net/annabunches/joyful/internal/virtualdevice"
|
"git.annabunches.net/annabunches/joyful/internal/virtualdevice"
|
||||||
|
@ -21,14 +21,7 @@ func getConfigDir(dir string) string {
|
||||||
return os.ExpandEnv(configDir)
|
return os.ExpandEnv(configDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
func readConfig(configDir string) *config.ConfigParser {
|
func initVirtualBuffers(config *configparser.ConfigParser) (map[string]*evdev.InputDevice,
|
||||||
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,
|
|
||||||
map[string]*virtualdevice.EventBuffer,
|
map[string]*virtualdevice.EventBuffer,
|
||||||
map[*evdev.InputDevice]*virtualdevice.EventBuffer) {
|
map[*evdev.InputDevice]*virtualdevice.EventBuffer) {
|
||||||
|
|
||||||
|
@ -46,14 +39,6 @@ func initVirtualBuffers(config *config.ConfigParser) (map[string]*evdev.InputDev
|
||||||
return vDevices, vBuffersByName, vBuffersByDevice
|
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() {
|
func main() {
|
||||||
// parse command-line
|
// parse command-line
|
||||||
var configFlag string
|
var configFlag string
|
||||||
|
@ -64,7 +49,8 @@ func main() {
|
||||||
|
|
||||||
// parse configs
|
// parse configs
|
||||||
configDir := getConfigDir(configFlag)
|
configDir := getConfigDir(configFlag)
|
||||||
config := readConfig(configDir)
|
config, err := configparser.ParseConfig(configDir)
|
||||||
|
logger.FatalIfError(err, "Failed to parse configuration")
|
||||||
|
|
||||||
// initialize TTS
|
// initialize TTS
|
||||||
tts, err := newTTS(ttsOps)
|
tts, err := newTTS(ttsOps)
|
||||||
|
@ -144,7 +130,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadRules(
|
func loadRules(
|
||||||
config *config.ConfigParser,
|
config *configparser.ConfigParser,
|
||||||
pDevices map[string]*evdev.InputDevice,
|
pDevices map[string]*evdev.InputDevice,
|
||||||
vDevices map[string]*evdev.InputDevice) ([]mappingrules.MappingRule, <-chan ChannelEvent, func(), *sync.WaitGroup) {
|
vDevices map[string]*evdev.InputDevice) ([]mappingrules.MappingRule, <-chan ChannelEvent, func(), *sync.WaitGroup) {
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package config
|
package configparser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
|
@ -1,4 +1,4 @@
|
||||||
package config
|
package configparser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
|
@ -10,7 +10,7 @@
|
||||||
//
|
//
|
||||||
// nb: there are methods defined on ConfigParser in other files in this package!
|
// nb: there are methods defined on ConfigParser in other files in this package!
|
||||||
|
|
||||||
package config
|
package configparser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
@ -75,3 +75,59 @@ func (parser *ConfigParser) GetModes() []string {
|
||||||
}
|
}
|
||||||
return parser.config.Modes
|
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
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package config
|
package configparser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
|
@ -1,4 +1,4 @@
|
||||||
package config
|
package configparser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
|
@ -1,4 +1,4 @@
|
||||||
package config
|
package configparser
|
||||||
|
|
||||||
import "github.com/holoplot/go-evdev"
|
import "github.com/holoplot/go-evdev"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package config
|
package configparser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
|
@ -1,4 +1,4 @@
|
||||||
package config
|
package configparser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
|
@ -1,4 +1,4 @@
|
||||||
package config
|
package configparser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
|
@ -1,4 +1,4 @@
|
||||||
package config
|
package configparser
|
||||||
|
|
||||||
import "slices"
|
import "slices"
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// These types comprise the YAML schema for configuring Joyful.
|
// These types comprise the YAML schema for configuring Joyful.
|
||||||
// The config files will be combined and then unmarshalled into this
|
// The config files will be combined and then unmarshalled into this
|
||||||
|
|
||||||
package config
|
package configparser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
|
@ -1,4 +1,4 @@
|
||||||
package config
|
package configparser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/holoplot/go-evdev"
|
"github.com/holoplot/go-evdev"
|
Loading…
Add table
Add a link
Reference in a new issue