joyful/internal/mappingrules/mapping_rule_button_latched.go
Anna Rose Wiggins 8d2b15a7c8 Move initialization code closer to the appropriate structs. (#17)
Reviewed-on: #17
Co-authored-by: Anna Rose Wiggins <annabunches@gmail.com>
Co-committed-by: Anna Rose Wiggins <annabunches@gmail.com>
2025-08-12 00:57:11 +00:00

59 lines
1.4 KiB
Go

package mappingrules
import (
"git.annabunches.net/annabunches/joyful/internal/configparser"
"github.com/holoplot/go-evdev"
)
type MappingRuleButtonLatched struct {
MappingRuleBase
Input *RuleTargetButton
Output *RuleTargetButton
State bool
}
func NewMappingRuleButtonLatched(ruleConfig configparser.RuleConfigButtonLatched,
pDevs map[string]Device,
vDevs map[string]Device,
base MappingRuleBase) (*MappingRuleButtonLatched, error) {
input, err := NewRuleTargetButtonFromConfig(ruleConfig.Input, pDevs)
if err != nil {
return nil, err
}
output, err := NewRuleTargetButtonFromConfig(ruleConfig.Output, vDevs)
if err != nil {
return nil, err
}
return &MappingRuleButtonLatched{
MappingRuleBase: base,
Input: input,
Output: output,
State: false,
}, nil
}
func (rule *MappingRuleButtonLatched) MatchEvent(device Device, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) {
if !rule.MappingRuleBase.modeCheck(mode) {
return nil, nil
}
if device != rule.Input.Device ||
event.Code != rule.Input.Button ||
rule.Input.NormalizeValue(event.Value) == 0 {
return nil, nil
}
// 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 rule.Output.Device.(*evdev.InputDevice), rule.Output.CreateEvent(value, mode)
}