joyful/internal/mappingrules/mapping_rule_axis_combined.go

51 lines
1.5 KiB
Go

package mappingrules
import (
"git.annabunches.net/annabunches/joyful/internal/logger"
"github.com/holoplot/go-evdev"
)
type MappingRuleAxisCombined struct {
MappingRuleBase
InputLower *RuleTargetAxis
InputUpper *RuleTargetAxis
Output *RuleTargetAxis
}
func NewMappingRuleAxisCombined(base MappingRuleBase, inputLower *RuleTargetAxis, inputUpper *RuleTargetAxis, output *RuleTargetAxis) *MappingRuleAxisCombined {
inputLower.OutputMax = 0
inputUpper.OutputMin = 0
return &MappingRuleAxisCombined{
MappingRuleBase: base,
InputLower: inputLower,
InputUpper: inputUpper,
Output: output,
}
}
func (rule *MappingRuleAxisCombined) MatchEvent(device Device, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) {
if !rule.MappingRuleBase.modeCheck(mode) ||
!(rule.InputLower.MatchEvent(device, event) ||
rule.InputUpper.MatchEvent(device, event)) {
return nil, nil
}
// Since lower and upper are guaranteed to have opposite signs,
// we can just sum them.
var value int32
value += getValueFromAbs(rule.InputLower)
value += getValueFromAbs(rule.InputUpper)
return rule.Output.Device.(*evdev.InputDevice), rule.Output.CreateEvent(value, mode)
}
func getValueFromAbs(ruleTarget *RuleTargetAxis) int32 {
absInfo, err := ruleTarget.Device.AbsInfos()
if err != nil {
logger.LogErrorf(err, "WARNING: Couldn't get axis data for device '%s'", ruleTarget.DeviceName)
return 0
}
return ruleTarget.NormalizeValue(absInfo[ruleTarget.Axis].Value)
}