Add support for combining 2 axes into one virtual axis. (#11)
Reviewed-on: #11
This commit is contained in:
parent
7b520af24a
commit
3196d4ea22
14 changed files with 321 additions and 61 deletions
51
internal/mappingrules/mapping_rule_axis_combined.go
Normal file
51
internal/mappingrules/mapping_rule_axis_combined.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue