Add support for Joystick hats.
This commit is contained in:
parent
62befa045a
commit
fce8888c77
8 changed files with 134 additions and 13 deletions
|
|
@ -31,3 +31,9 @@ type RuleTargetConfigRelaxis struct {
|
|||
type RuleTargetConfigModeSelect struct {
|
||||
Modes []string
|
||||
}
|
||||
|
||||
type RuleTargetConfigHat struct {
|
||||
Device string
|
||||
Hat string
|
||||
Inverted bool
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,6 +40,11 @@ type RuleConfigAxis struct {
|
|||
Output RuleTargetConfigAxis
|
||||
}
|
||||
|
||||
type RuleConfigHat struct {
|
||||
Input RuleTargetConfigHat
|
||||
Output RuleTargetConfigHat
|
||||
}
|
||||
|
||||
type RuleConfigAxisCombined struct {
|
||||
InputLower RuleTargetConfigAxis `yaml:"input_lower,omitempty"`
|
||||
InputUpper RuleTargetConfigAxis `yaml:"input_upper,omitempty"`
|
||||
|
|
|
|||
|
|
@ -22,9 +22,6 @@ type RuleTarget interface {
|
|||
// (e.g., inverting the value if Inverted == true)
|
||||
NormalizeValue(int32) int32
|
||||
|
||||
// MatchEvent returns true if the provided device and input event are a match for this rule target
|
||||
ValidateEvent(*evdev.InputDevice, *evdev.InputEvent) bool
|
||||
|
||||
// CreateEvent creates an event that can be emitted on a virtual device.
|
||||
// For RuleTargetModeSelect, this method modifies the active mode and returns nil.
|
||||
//
|
||||
|
|
@ -35,6 +32,7 @@ type RuleTarget interface {
|
|||
// for most implementations.
|
||||
CreateEvent(int32, *string) *evdev.InputEvent
|
||||
|
||||
// MatchEvent returns true if the provided device and input event are a match for this rule target
|
||||
MatchEvent(device Device, event *evdev.InputEvent) bool
|
||||
}
|
||||
|
||||
|
|
|
|||
45
internal/mappingrules/mapping_rule_hat.go
Normal file
45
internal/mappingrules/mapping_rule_hat.go
Normal 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)
|
||||
}
|
||||
53
internal/mappingrules/rule_target_hat.go
Normal file
53
internal/mappingrules/rule_target_hat.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package mappingrules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.annabunches.net/annabunches/joyful/internal/configparser"
|
||||
"git.annabunches.net/annabunches/joyful/internal/eventcodes"
|
||||
"github.com/holoplot/go-evdev"
|
||||
)
|
||||
|
||||
type RuleTargetHat struct {
|
||||
Device Device
|
||||
Hat evdev.EvCode
|
||||
Inverted bool
|
||||
}
|
||||
|
||||
func NewRuleTargetHatFromConfig(config configparser.RuleTargetConfigHat, devs map[string]Device) (*RuleTargetHat, error) {
|
||||
dev, ok := devs[config.Device]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("device '%s' not found", config.Device)
|
||||
}
|
||||
|
||||
code, err := eventcodes.ParseCode(config.Hat, eventcodes.CodePrefixAxis)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &RuleTargetHat{
|
||||
Device: dev,
|
||||
Hat: code,
|
||||
Inverted: config.Inverted,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (target *RuleTargetHat) NormalizeValue(value int32) int32 {
|
||||
if !target.Inverted {
|
||||
return value
|
||||
}
|
||||
|
||||
return value * -1
|
||||
}
|
||||
|
||||
func (target *RuleTargetHat) CreateEvent(value int32, _ *string) *evdev.InputEvent {
|
||||
return &evdev.InputEvent{
|
||||
Type: evdev.EV_ABS,
|
||||
Code: target.Hat,
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
|
||||
func (target *RuleTargetHat) MatchEvent(device Device, event *evdev.InputEvent) bool {
|
||||
return device == target.Device && event.Code == target.Hat
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue