Use typed enums for cleaner parsing.
This commit is contained in:
parent
8d2b15a7c8
commit
553966ac87
5 changed files with 33 additions and 40 deletions
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"git.annabunches.net/annabunches/joyful/internal/configparser"
|
"git.annabunches.net/annabunches/joyful/internal/configparser"
|
||||||
|
@ -16,7 +15,7 @@ func initPhysicalDevices(conf *configparser.Config) map[string]*evdev.InputDevic
|
||||||
pDeviceMap := make(map[string]*evdev.InputDevice)
|
pDeviceMap := make(map[string]*evdev.InputDevice)
|
||||||
|
|
||||||
for _, devConfig := range conf.Devices {
|
for _, devConfig := range conf.Devices {
|
||||||
if strings.ToLower(devConfig.Type) != configparser.DeviceTypePhysical {
|
if devConfig.Type != configparser.DeviceTypePhysical {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +70,7 @@ func initVirtualBuffers(config *configparser.Config) (map[string]*evdev.InputDev
|
||||||
vBuffersByDevice := make(map[*evdev.InputDevice]*virtualdevice.EventBuffer)
|
vBuffersByDevice := make(map[*evdev.InputDevice]*virtualdevice.EventBuffer)
|
||||||
|
|
||||||
for _, devConfig := range config.Devices {
|
for _, devConfig := range config.Devices {
|
||||||
if strings.ToLower(devConfig.Type) != configparser.DeviceTypeVirtual {
|
if devConfig.Type != configparser.DeviceTypeVirtual {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,12 +15,12 @@ type Config struct {
|
||||||
|
|
||||||
// These top-level structs use custom unmarshaling to unpack each available sub-type
|
// These top-level structs use custom unmarshaling to unpack each available sub-type
|
||||||
type DeviceConfig struct {
|
type DeviceConfig struct {
|
||||||
Type string
|
Type DeviceType
|
||||||
Config interface{}
|
Config interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type RuleConfig struct {
|
type RuleConfig struct {
|
||||||
Type string
|
Type RuleType
|
||||||
Name string
|
Name string
|
||||||
Modes []string
|
Modes []string
|
||||||
Config interface{}
|
Config interface{}
|
||||||
|
@ -119,7 +119,7 @@ type RuleTargetConfigModeSelect struct {
|
||||||
|
|
||||||
func (dc *DeviceConfig) UnmarshalYAML(unmarshal func(data interface{}) error) error {
|
func (dc *DeviceConfig) UnmarshalYAML(unmarshal func(data interface{}) error) error {
|
||||||
metaConfig := &struct {
|
metaConfig := &struct {
|
||||||
Type string
|
Type DeviceType
|
||||||
}{}
|
}{}
|
||||||
err := unmarshal(metaConfig)
|
err := unmarshal(metaConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -145,7 +145,7 @@ func (dc *DeviceConfig) UnmarshalYAML(unmarshal func(data interface{}) error) er
|
||||||
|
|
||||||
func (dc *RuleConfig) UnmarshalYAML(unmarshal func(data interface{}) error) error {
|
func (dc *RuleConfig) UnmarshalYAML(unmarshal func(data interface{}) error) error {
|
||||||
metaConfig := &struct {
|
metaConfig := &struct {
|
||||||
Type string
|
Type RuleType
|
||||||
Name string
|
Name string
|
||||||
Modes []string
|
Modes []string
|
||||||
}{}
|
}{}
|
||||||
|
|
|
@ -1,15 +1,21 @@
|
||||||
package configparser
|
package configparser
|
||||||
|
|
||||||
const (
|
type DeviceType string
|
||||||
DeviceTypePhysical = "physical"
|
|
||||||
DeviceTypeVirtual = "virtual"
|
|
||||||
|
|
||||||
RuleTypeButton = "button"
|
const (
|
||||||
RuleTypeButtonCombo = "button-combo"
|
DeviceTypePhysical DeviceType = "physical"
|
||||||
RuleTypeButtonLatched = "button-latched"
|
DeviceTypeVirtual DeviceType = "virtual"
|
||||||
RuleTypeAxis = "axis"
|
)
|
||||||
RuleTypeAxisCombined = "axis-combined"
|
|
||||||
RuleTypeAxisToButton = "axis-to-button"
|
type RuleType string
|
||||||
RuleTypeAxisToRelaxis = "axis-to-relaxis"
|
|
||||||
RuleTypeModeSelect = "mode-select"
|
const (
|
||||||
|
RuleTypeButton RuleType = "button"
|
||||||
|
RuleTypeButtonCombo RuleType = "button-combo"
|
||||||
|
RuleTypeButtonLatched RuleType = "button-latched"
|
||||||
|
RuleTypeAxis RuleType = "axis"
|
||||||
|
RuleTypeAxisCombined RuleType = "axis-combined"
|
||||||
|
RuleTypeAxisToButton RuleType = "axis-to-button"
|
||||||
|
RuleTypeAxisToRelaxis RuleType = "axis-to-relaxis"
|
||||||
|
RuleTypeModeSelect RuleType = "mode-select"
|
||||||
)
|
)
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.annabunches.net/annabunches/joyful/internal/configparser"
|
"git.annabunches.net/annabunches/joyful/internal/configparser"
|
||||||
"git.annabunches.net/annabunches/joyful/internal/logger"
|
"git.annabunches.net/annabunches/joyful/internal/logger"
|
||||||
|
@ -33,24 +32,25 @@ func NewRule(config configparser.RuleConfig, pDevs map[string]Device, vDevs map[
|
||||||
|
|
||||||
base := NewMappingRuleBase(config.Name, config.Modes)
|
base := NewMappingRuleBase(config.Name, config.Modes)
|
||||||
|
|
||||||
switch strings.ToLower(config.Type) {
|
switch config.Type {
|
||||||
case RuleTypeButton:
|
case configparser.RuleTypeButton:
|
||||||
newRule, err = NewMappingRuleButton(config.Config.(configparser.RuleConfigButton), pDevs, vDevs, base)
|
newRule, err = NewMappingRuleButton(config.Config.(configparser.RuleConfigButton), pDevs, vDevs, base)
|
||||||
case RuleTypeButtonCombo:
|
case configparser.RuleTypeButtonCombo:
|
||||||
newRule, err = NewMappingRuleButtonCombo(config.Config.(configparser.RuleConfigButtonCombo), pDevs, vDevs, base)
|
newRule, err = NewMappingRuleButtonCombo(config.Config.(configparser.RuleConfigButtonCombo), pDevs, vDevs, base)
|
||||||
case RuleTypeButtonLatched:
|
case configparser.RuleTypeButtonLatched:
|
||||||
newRule, err = NewMappingRuleButtonLatched(config.Config.(configparser.RuleConfigButtonLatched), pDevs, vDevs, base)
|
newRule, err = NewMappingRuleButtonLatched(config.Config.(configparser.RuleConfigButtonLatched), pDevs, vDevs, base)
|
||||||
case RuleTypeAxis:
|
case configparser.RuleTypeAxis:
|
||||||
newRule, err = NewMappingRuleAxis(config.Config.(configparser.RuleConfigAxis), pDevs, vDevs, base)
|
newRule, err = NewMappingRuleAxis(config.Config.(configparser.RuleConfigAxis), pDevs, vDevs, base)
|
||||||
case RuleTypeAxisCombined:
|
case configparser.RuleTypeAxisCombined:
|
||||||
newRule, err = NewMappingRuleAxisCombined(config.Config.(configparser.RuleConfigAxisCombined), pDevs, vDevs, base)
|
newRule, err = NewMappingRuleAxisCombined(config.Config.(configparser.RuleConfigAxisCombined), pDevs, vDevs, base)
|
||||||
case RuleTypeAxisToButton:
|
case configparser.RuleTypeAxisToButton:
|
||||||
newRule, err = NewMappingRuleAxisToButton(config.Config.(configparser.RuleConfigAxisToButton), pDevs, vDevs, base)
|
newRule, err = NewMappingRuleAxisToButton(config.Config.(configparser.RuleConfigAxisToButton), pDevs, vDevs, base)
|
||||||
case RuleTypeAxisToRelaxis:
|
case configparser.RuleTypeAxisToRelaxis:
|
||||||
newRule, err = NewMappingRuleAxisToRelaxis(config.Config.(configparser.RuleConfigAxisToRelaxis), pDevs, vDevs, base)
|
newRule, err = NewMappingRuleAxisToRelaxis(config.Config.(configparser.RuleConfigAxisToRelaxis), pDevs, vDevs, base)
|
||||||
case RuleTypeModeSelect:
|
case configparser.RuleTypeModeSelect:
|
||||||
newRule, err = NewMappingRuleModeSelect(config.Config.(configparser.RuleConfigModeSelect), pDevs, modes, base)
|
newRule, err = NewMappingRuleModeSelect(config.Config.(configparser.RuleConfigModeSelect), pDevs, modes, base)
|
||||||
default:
|
default:
|
||||||
|
// Shouldn't actually be possible to get here...
|
||||||
err = fmt.Errorf("bad rule type '%s' for rule '%s'", config.Type, config.Name)
|
err = fmt.Errorf("bad rule type '%s' for rule '%s'", config.Type, config.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
package mappingrules
|
|
||||||
|
|
||||||
const (
|
|
||||||
RuleTypeButton = "button"
|
|
||||||
RuleTypeButtonCombo = "button-combo"
|
|
||||||
RuleTypeButtonLatched = "button-latched"
|
|
||||||
RuleTypeAxis = "axis"
|
|
||||||
RuleTypeAxisCombined = "axis-combined"
|
|
||||||
RuleTypeAxisToButton = "axis-to-button"
|
|
||||||
RuleTypeAxisToRelaxis = "axis-to-relaxis"
|
|
||||||
RuleTypeModeSelect = "mode-select"
|
|
||||||
)
|
|
Loading…
Add table
Add a link
Reference in a new issue