From ed2627e1135a9fcc7ba81328b3dfc0ef024c30ae Mon Sep 17 00:00:00 2001 From: Anna Rose Wiggins Date: Sat, 12 Jul 2025 17:14:57 -0400 Subject: [PATCH] Implement config generator for AxisToButton. Use RuleTargetDevice interface more broadly. --- internal/config/make_rules.go | 14 +++++++++++--- internal/config/schema.go | 14 ++++++++------ internal/mappingrules/interfaces.go | 4 +++- internal/mappingrules/mapping_rule_axis.go | 2 +- internal/mappingrules/mapping_rule_button.go | 2 +- internal/mappingrules/mapping_rule_button_combo.go | 2 +- .../mappingrules/mapping_rule_button_latched.go | 2 +- internal/mappingrules/mapping_rule_mode_select.go | 2 +- internal/mappingrules/rule_target_button.go | 2 +- 9 files changed, 28 insertions(+), 16 deletions(-) diff --git a/internal/config/make_rules.go b/internal/config/make_rules.go index 5e7e9de..1b6490c 100644 --- a/internal/config/make_rules.go +++ b/internal/config/make_rules.go @@ -1,7 +1,6 @@ package config import ( - "errors" "fmt" "strings" @@ -134,13 +133,22 @@ func makeMappingRuleAxis(ruleConfig RuleConfig, return mappingrules.NewMappingRuleAxis(base, input, output), nil } -// STUB func makeMappingRuleAxisToButton(ruleConfig RuleConfig, pDevs map[string]*evdev.InputDevice, vDevs map[string]*evdev.InputDevice, base mappingrules.MappingRuleBase) (*mappingrules.MappingRuleAxisToButton, error) { - return nil, errors.New("stub: makeMappingRuleAxisToButton") + input, err := makeRuleTargetAxis(ruleConfig.Input, pDevs) + if err != nil { + return nil, err + } + + output, err := makeRuleTargetButton(ruleConfig.Output, vDevs) + if err != nil { + return nil, err + } + + return mappingrules.NewMappingRuleAxisToButton(base, input, output, ruleConfig.RepeatRateMin, ruleConfig.RepeatRateMax), nil } func makeMappingRuleModeSelect(ruleConfig RuleConfig, diff --git a/internal/config/schema.go b/internal/config/schema.go index 42bd339..1482c8c 100644 --- a/internal/config/schema.go +++ b/internal/config/schema.go @@ -19,12 +19,14 @@ type DeviceConfig struct { } type RuleConfig struct { - Name string `yaml:"name,omitempty"` - Type string `yaml:"type"` - Input RuleTargetConfig `yaml:"input,omitempty"` - Inputs []RuleTargetConfig `yaml:"inputs,omitempty"` - Output RuleTargetConfig `yaml:"output"` - Modes []string `yaml:"modes,omitempty"` + Name string `yaml:"name,omitempty"` + Type string `yaml:"type"` + Input RuleTargetConfig `yaml:"input,omitempty"` + Inputs []RuleTargetConfig `yaml:"inputs,omitempty"` + Output RuleTargetConfig `yaml:"output"` + Modes []string `yaml:"modes,omitempty"` + RepeatRateMin int `yaml:"repeat_rate_min,omitempty"` + RepeatRateMax int `yaml:"repeat_rate_max,omitempty"` } type RuleTargetConfig struct { diff --git a/internal/mappingrules/interfaces.go b/internal/mappingrules/interfaces.go index 8ea4f39..3be58b4 100644 --- a/internal/mappingrules/interfaces.go +++ b/internal/mappingrules/interfaces.go @@ -3,7 +3,7 @@ package mappingrules import "github.com/holoplot/go-evdev" type MappingRule interface { - MatchEvent(*evdev.InputDevice, *evdev.InputEvent, *string) (*evdev.InputDevice, *evdev.InputEvent) + MatchEvent(RuleTargetDevice, *evdev.InputEvent, *string) (*evdev.InputDevice, *evdev.InputEvent) } // RuleTargets represent either a device input to match on, or an output to produce. @@ -25,6 +25,8 @@ type RuleTarget interface { // Typically int32 is the input event's normalized value. *string is the current mode, but is optional // for most implementations. CreateEvent(int32, *string) *evdev.InputEvent + + MatchEvent(device RuleTargetDevice, event *evdev.InputEvent) bool } // RuleTargetDevice is an interface abstraction on top of evdev.InputDevice, implementing diff --git a/internal/mappingrules/mapping_rule_axis.go b/internal/mappingrules/mapping_rule_axis.go index da324e2..7b3e778 100644 --- a/internal/mappingrules/mapping_rule_axis.go +++ b/internal/mappingrules/mapping_rule_axis.go @@ -17,7 +17,7 @@ func NewMappingRuleAxis(base MappingRuleBase, input *RuleTargetAxis, output *Rul } } -func (rule *MappingRuleAxis) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) { +func (rule *MappingRuleAxis) MatchEvent(device RuleTargetDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) { if !rule.MappingRuleBase.modeCheck(mode) || !rule.Input.MatchEvent(device, event) { return nil, nil diff --git a/internal/mappingrules/mapping_rule_button.go b/internal/mappingrules/mapping_rule_button.go index bf47609..a13d5a6 100644 --- a/internal/mappingrules/mapping_rule_button.go +++ b/internal/mappingrules/mapping_rule_button.go @@ -21,7 +21,7 @@ func NewMappingRuleButton( } } -func (rule *MappingRuleButton) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) { +func (rule *MappingRuleButton) MatchEvent(device RuleTargetDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) { if !rule.MappingRuleBase.modeCheck(mode) { return nil, nil } diff --git a/internal/mappingrules/mapping_rule_button_combo.go b/internal/mappingrules/mapping_rule_button_combo.go index 8bfbda8..4f488ef 100644 --- a/internal/mappingrules/mapping_rule_button_combo.go +++ b/internal/mappingrules/mapping_rule_button_combo.go @@ -23,7 +23,7 @@ func NewMappingRuleButtonCombo( } } -func (rule *MappingRuleButtonCombo) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) { +func (rule *MappingRuleButtonCombo) MatchEvent(device RuleTargetDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) { if !rule.MappingRuleBase.modeCheck(mode) { return nil, nil } diff --git a/internal/mappingrules/mapping_rule_button_latched.go b/internal/mappingrules/mapping_rule_button_latched.go index 0915550..1204968 100644 --- a/internal/mappingrules/mapping_rule_button_latched.go +++ b/internal/mappingrules/mapping_rule_button_latched.go @@ -22,7 +22,7 @@ func NewMappingRuleButtonLatched( } } -func (rule *MappingRuleButtonLatched) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) { +func (rule *MappingRuleButtonLatched) MatchEvent(device RuleTargetDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) { if !rule.MappingRuleBase.modeCheck(mode) { return nil, nil } diff --git a/internal/mappingrules/mapping_rule_mode_select.go b/internal/mappingrules/mapping_rule_mode_select.go index c858e10..1bb13fa 100644 --- a/internal/mappingrules/mapping_rule_mode_select.go +++ b/internal/mappingrules/mapping_rule_mode_select.go @@ -22,7 +22,7 @@ func NewMappingRuleModeSelect( } func (rule *MappingRuleModeSelect) MatchEvent( - device *evdev.InputDevice, + device RuleTargetDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) { diff --git a/internal/mappingrules/rule_target_button.go b/internal/mappingrules/rule_target_button.go index 511249c..93534c7 100644 --- a/internal/mappingrules/rule_target_button.go +++ b/internal/mappingrules/rule_target_button.go @@ -36,7 +36,7 @@ func (target *RuleTargetButton) CreateEvent(value int32, _ *string) *evdev.Input } } -func (target *RuleTargetButton) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent) bool { +func (target *RuleTargetButton) MatchEvent(device RuleTargetDevice, event *evdev.InputEvent) bool { return device == target.Device && event.Type == evdev.EV_KEY && event.Code == target.Button