Fix up refactored rule targets.
This commit is contained in:
parent
c14ec074d1
commit
db848db810
5 changed files with 71 additions and 38 deletions
|
@ -107,29 +107,45 @@ func makeLatchedRule(ruleConfig RuleConfig, pDevs map[string]*evdev.InputDevice,
|
||||||
|
|
||||||
// makeInputRuleTarget takes an Input declaration from the YAML and returns a fully formed RuleTarget.
|
// makeInputRuleTarget takes an Input declaration from the YAML and returns a fully formed RuleTarget.
|
||||||
func makeRuleTarget(targetConfig RuleTargetConfig, devs map[string]*evdev.InputDevice) (mappingrules.RuleTarget, error) {
|
func makeRuleTarget(targetConfig RuleTargetConfig, devs map[string]*evdev.InputDevice) (mappingrules.RuleTarget, error) {
|
||||||
ruleTarget := mappingrules.RuleTarget{}
|
|
||||||
|
|
||||||
if len(targetConfig.ModeSelect) > 0 {
|
if len(targetConfig.ModeSelect) > 0 {
|
||||||
ruleTarget.ModeSelect = targetConfig.ModeSelect
|
return &mappingrules.RuleTargetModeSelect{
|
||||||
return ruleTarget, nil
|
ModeSelect: targetConfig.ModeSelect,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
device, ok := devs[targetConfig.Device]
|
device, ok := devs[targetConfig.Device]
|
||||||
if !ok {
|
if !ok {
|
||||||
return mappingrules.RuleTarget{}, fmt.Errorf("couldn't build rule due to non-existent device '%s'", targetConfig.Device)
|
return nil, fmt.Errorf("couldn't build rule due to non-existent device '%s'", targetConfig.Device)
|
||||||
}
|
}
|
||||||
ruleTarget.Device = device
|
|
||||||
|
|
||||||
eventType, eventCode, err := decodeRuleTargetValues(targetConfig)
|
eventType, eventCode, err := decodeRuleTargetValues(targetConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ruleTarget, err
|
return nil, err
|
||||||
}
|
}
|
||||||
ruleTarget.Type = eventType
|
|
||||||
ruleTarget.Code = eventCode
|
|
||||||
ruleTarget.Inverted = targetConfig.Inverted
|
|
||||||
ruleTarget.DeviceName = targetConfig.Device
|
|
||||||
|
|
||||||
return ruleTarget, nil
|
baseParams := mappingrules.RuleTargetBase{
|
||||||
|
DeviceName: targetConfig.Device,
|
||||||
|
Device: device,
|
||||||
|
Inverted: targetConfig.Inverted,
|
||||||
|
Code: eventCode,
|
||||||
|
}
|
||||||
|
|
||||||
|
switch eventType {
|
||||||
|
case evdev.EV_KEY:
|
||||||
|
return &mappingrules.RuleTargetButton{
|
||||||
|
RuleTargetBase: baseParams,
|
||||||
|
}, nil
|
||||||
|
|
||||||
|
case evdev.EV_ABS:
|
||||||
|
return &mappingrules.RuleTargetAxis{
|
||||||
|
RuleTargetBase: baseParams,
|
||||||
|
AxisStart: targetConfig.AxisStart,
|
||||||
|
AxisEnd: targetConfig.AxisEnd,
|
||||||
|
}, nil
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("skipping rule due to unsupported event type '%d'", eventType)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// decodeRuleTargetValues returns the appropriate evdev.EvType and evdev.EvCode values
|
// decodeRuleTargetValues returns the appropriate evdev.EvType and evdev.EvCode values
|
||||||
|
|
|
@ -31,6 +31,8 @@ type RuleTargetConfig struct {
|
||||||
Device string `yaml:"device,omitempty"`
|
Device string `yaml:"device,omitempty"`
|
||||||
Button string `yaml:"button,omitempty"`
|
Button string `yaml:"button,omitempty"`
|
||||||
Axis string `yaml:"axis,omitempty"`
|
Axis string `yaml:"axis,omitempty"`
|
||||||
|
AxisStart int32 `yaml:"axis_start,omitempty"`
|
||||||
|
AxisEnd int32 `yaml:"axis_end,omitempty"`
|
||||||
Inverted bool `yaml:"inverted,omitempty"`
|
Inverted bool `yaml:"inverted,omitempty"`
|
||||||
ModeSelect []string `yaml:"mode_select,omitempty"`
|
ModeSelect []string `yaml:"mode_select,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (rule *MappingRuleBase) OutputName() string {
|
func (rule *MappingRuleBase) OutputName() string {
|
||||||
return rule.Output.DeviceName
|
return rule.Output.GetDeviceName()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rule *MappingRuleBase) modeCheck(mode *string) bool {
|
func (rule *MappingRuleBase) modeCheck(mode *string) bool {
|
||||||
|
@ -22,12 +22,12 @@ func (rule *SimpleMappingRule) MatchEvent(device *evdev.InputDevice, event *evde
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if device != rule.Input.Device ||
|
if device != rule.Input.GetDevice() ||
|
||||||
event.Code != rule.Input.Code {
|
event.Code != rule.Input.GetCode() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return eventFromTarget(rule.Output, valueFromTarget(rule.Input, event), mode)
|
return rule.Output.CreateEvent(rule.Input.NormalizeValue(event.Value), mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rule *ComboMappingRule) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) *evdev.InputEvent {
|
func (rule *ComboMappingRule) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) *evdev.InputEvent {
|
||||||
|
@ -36,11 +36,11 @@ func (rule *ComboMappingRule) MatchEvent(device *evdev.InputDevice, event *evdev
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check each of the inputs, and if we find a match, proceed
|
// Check each of the inputs, and if we find a match, proceed
|
||||||
var match *RuleTarget
|
var match RuleTarget
|
||||||
for _, input := range rule.Inputs {
|
for _, input := range rule.Inputs {
|
||||||
if device == input.Device &&
|
if device == input.GetDevice() &&
|
||||||
event.Code == input.Code {
|
event.Code == input.GetCode() {
|
||||||
match = &input
|
match = input
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ func (rule *ComboMappingRule) MatchEvent(device *evdev.InputDevice, event *evdev
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the value and add/subtract it from State
|
// Get the value and add/subtract it from State
|
||||||
inputValue := valueFromTarget(*match, event)
|
inputValue := match.NormalizeValue(event.Value)
|
||||||
oldState := rule.State
|
oldState := rule.State
|
||||||
if inputValue == 0 {
|
if inputValue == 0 {
|
||||||
rule.State = max(rule.State-1, 0)
|
rule.State = max(rule.State-1, 0)
|
||||||
|
@ -60,10 +60,10 @@ func (rule *ComboMappingRule) MatchEvent(device *evdev.InputDevice, event *evdev
|
||||||
targetState := len(rule.Inputs)
|
targetState := len(rule.Inputs)
|
||||||
|
|
||||||
if oldState == targetState-1 && rule.State == targetState {
|
if oldState == targetState-1 && rule.State == targetState {
|
||||||
return eventFromTarget(rule.Output, 1, mode)
|
return rule.Output.CreateEvent(1, mode)
|
||||||
}
|
}
|
||||||
if oldState == targetState && rule.State == targetState-1 {
|
if oldState == targetState && rule.State == targetState-1 {
|
||||||
return eventFromTarget(rule.Output, 0, mode)
|
return rule.Output.CreateEvent(0, mode)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -73,8 +73,8 @@ func (rule *LatchedMappingRule) MatchEvent(device *evdev.InputDevice, event *evd
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if device != rule.Input.Device ||
|
if device != rule.Input.GetDevice() ||
|
||||||
event.Code != rule.Input.Code ||
|
event.Code != rule.Input.GetCode() ||
|
||||||
rule.Input.NormalizeValue(event.Value) == 0 {
|
rule.Input.NormalizeValue(event.Value) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,18 @@ import (
|
||||||
"github.com/holoplot/go-evdev"
|
"github.com/holoplot/go-evdev"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (target *RuleTargetBase) GetCode() evdev.EvCode {
|
||||||
|
return target.Code
|
||||||
|
}
|
||||||
|
|
||||||
|
func (target *RuleTargetBase) GetDeviceName() string {
|
||||||
|
return target.DeviceName
|
||||||
|
}
|
||||||
|
|
||||||
|
func (target *RuleTargetBase) GetDevice() *evdev.InputDevice {
|
||||||
|
return target.Device
|
||||||
|
}
|
||||||
|
|
||||||
func (target *RuleTargetButton) NormalizeValue(value int32) int32 {
|
func (target *RuleTargetButton) NormalizeValue(value int32) int32 {
|
||||||
if value == 0 {
|
if value == 0 {
|
||||||
return 1
|
return 1
|
||||||
|
@ -14,6 +26,14 @@ func (target *RuleTargetButton) NormalizeValue(value int32) int32 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (target *RuleTargetButton) CreateEvent(value int32, mode *string) *evdev.InputEvent {
|
||||||
|
return &evdev.InputEvent{
|
||||||
|
Type: evdev.EV_KEY,
|
||||||
|
Code: target.Code,
|
||||||
|
Value: value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (target *RuleTargetAxis) NormalizeValue(value int32) int32 {
|
func (target *RuleTargetAxis) NormalizeValue(value int32) int32 {
|
||||||
if !target.Inverted {
|
if !target.Inverted {
|
||||||
return value
|
return value
|
||||||
|
@ -37,19 +57,6 @@ func (target *RuleTargetAxis) NormalizeValue(value int32) int32 {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
// RuleTargetModeSelect doesn't make sense as an input type
|
|
||||||
func (target *RuleTargetModeSelect) NormalizeValue(value int32) int32 {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (target *RuleTargetButton) CreateEvent(value int32, mode *string) *evdev.InputEvent {
|
|
||||||
return &evdev.InputEvent{
|
|
||||||
Type: evdev.EV_KEY,
|
|
||||||
Code: target.Code,
|
|
||||||
Value: value,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (target *RuleTargetAxis) CreateEvent(value int32, mode *string) *evdev.InputEvent {
|
func (target *RuleTargetAxis) CreateEvent(value int32, mode *string) *evdev.InputEvent {
|
||||||
return &evdev.InputEvent{
|
return &evdev.InputEvent{
|
||||||
Type: evdev.EV_ABS,
|
Type: evdev.EV_ABS,
|
||||||
|
@ -58,6 +65,11 @@ func (target *RuleTargetAxis) CreateEvent(value int32, mode *string) *evdev.Inpu
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RuleTargetModeSelect doesn't make sense as an input type
|
||||||
|
func (target *RuleTargetModeSelect) NormalizeValue(value int32) int32 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
func (target *RuleTargetModeSelect) CreateEvent(value int32, mode *string) *evdev.InputEvent {
|
func (target *RuleTargetModeSelect) CreateEvent(value int32, mode *string) *evdev.InputEvent {
|
||||||
if value == 0 {
|
if value == 0 {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -47,6 +47,9 @@ type ProportionalAxisMappingRule struct {
|
||||||
type RuleTarget interface {
|
type RuleTarget interface {
|
||||||
NormalizeValue(int32) int32
|
NormalizeValue(int32) int32
|
||||||
CreateEvent(int32, *string) *evdev.InputEvent
|
CreateEvent(int32, *string) *evdev.InputEvent
|
||||||
|
GetCode() evdev.EvCode
|
||||||
|
GetDeviceName() string
|
||||||
|
GetDevice() *evdev.InputDevice
|
||||||
}
|
}
|
||||||
|
|
||||||
type RuleTargetBase struct {
|
type RuleTargetBase struct {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue