Add support for Joystick hats.

This commit is contained in:
Anna Rose Wiggins 2025-09-15 13:17:40 -04:00
parent 62befa045a
commit fce8888c77
8 changed files with 134 additions and 13 deletions

View file

@ -0,0 +1,45 @@
package mappingrules
import (
"git.annabunches.net/annabunches/joyful/internal/configparser"
"github.com/holoplot/go-evdev"
)
// A Simple Mapping Rule can map a button to a button or an axis to an axis.
type MappingRuleHat struct {
MappingRuleBase
Input *RuleTargetHat
Output *RuleTargetHat
}
func NewMappingRuleHat(ruleConfig configparser.RuleConfigHat,
pDevs map[string]Device,
vDevs map[string]Device,
base MappingRuleBase) (*MappingRuleHat, error) {
input, err := NewRuleTargetHatFromConfig(ruleConfig.Input, pDevs)
if err != nil {
return nil, err
}
output, err := NewRuleTargetHatFromConfig(ruleConfig.Output, vDevs)
if err != nil {
return nil, err
}
return &MappingRuleHat{
MappingRuleBase: base,
Input: input,
Output: output,
}, nil
}
func (rule *MappingRuleHat) MatchEvent(device Device, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) {
if !rule.MappingRuleBase.modeCheck(mode) ||
!rule.Input.MatchEvent(device, event) {
return nil, nil
}
// The cast here is safe because the interface is only ever different for unit tests
return rule.Output.Device.(*evdev.InputDevice), rule.Output.CreateEvent(rule.Input.NormalizeValue(event.Value), mode)
}