Add latched rules.

This commit is contained in:
Anna Rose Wiggins 2025-07-02 20:05:36 -04:00
parent a078dcb193
commit f5283f33ca
5 changed files with 91 additions and 35 deletions

View file

@ -5,6 +5,10 @@ import (
"github.com/holoplot/go-evdev"
)
func (rule *MappingRuleBase) OutputName() string {
return rule.Output.DeviceName
}
// eventFromTarget creates an outputtable event from a RuleTarget
func eventFromTarget(output RuleTarget, value int32) *evdev.InputEvent {
return &evdev.InputEvent{
@ -79,10 +83,21 @@ func (rule *ComboMappingRule) MatchEvent(device *evdev.InputDevice, event *evdev
return nil
}
func (rule *SimpleMappingRule) OutputName() string {
return rule.Output.DeviceName
}
func (rule *LatchedMappingRule) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent) *evdev.InputEvent {
if device != rule.Input.Device ||
event.Code != rule.Input.Code ||
valueFromTarget(rule.Input, event) == 0 {
return nil
}
func (rule *ComboMappingRule) OutputName() string {
return rule.Output.DeviceName
// Input is pressed, so toggle state and emit event
var value int32
rule.State = !rule.State
if rule.State {
value = 1
} else {
value = 0
}
return eventFromTarget(rule.Output, value)
}

View file

@ -7,21 +7,32 @@ type MappingRule interface {
OutputName() string
}
type MappingRuleBase struct {
Output RuleTarget
}
// A Simple Mapping Rule can map a button to a button or an axis to an axis.
type SimpleMappingRule struct {
MappingRuleBase
Input RuleTarget
Output RuleTarget
Name string
}
// A Combo Mapping Rule can require multiple physical button presses for a single output button
type ComboMappingRule struct {
MappingRuleBase
Inputs []RuleTarget
Output RuleTarget
Name string
State int
}
type LatchedMappingRule struct {
MappingRuleBase
Input RuleTarget
Name string
State bool
}
type RuleTarget struct {
DeviceName string
Device *evdev.InputDevice