Don't fail target creation when AbsInfo errors.
This commit is contained in:
parent
6646044d28
commit
681e1fef70
2 changed files with 30 additions and 14 deletions
|
@ -19,8 +19,8 @@ type RuleTargetAxis struct {
|
|||
}
|
||||
|
||||
const (
|
||||
MinAxisValue = int32(-32768)
|
||||
MaxAxisValue = int32(32767)
|
||||
AxisValueMin = int32(-32768)
|
||||
AxisValueMax = int32(32767)
|
||||
)
|
||||
|
||||
func NewRuleTargetAxis(device_name string,
|
||||
|
@ -31,8 +31,16 @@ func NewRuleTargetAxis(device_name string,
|
|||
deadzoneEnd int32) (*RuleTargetAxis, error) {
|
||||
|
||||
info, err := device.AbsInfos()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// If we can't get AbsInfo (for example, we're a virtual device)
|
||||
// we set the bounds to the maximum allowable
|
||||
info = map[evdev.EvCode]evdev.AbsInfo{
|
||||
axis: {
|
||||
Minimum: AxisValueMin,
|
||||
Maximum: AxisValueMax,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if _, ok := info[axis]; !ok {
|
||||
|
@ -77,12 +85,12 @@ func (target *RuleTargetAxis) NormalizeValue(value int32) int32 {
|
|||
if target.Inverted {
|
||||
axisStrength = 1.0 - axisStrength
|
||||
}
|
||||
normalizedValue := LerpInt(MinAxisValue, MaxAxisValue, axisStrength)
|
||||
normalizedValue := LerpInt(AxisValueMin, AxisValueMax, axisStrength)
|
||||
return normalizedValue
|
||||
}
|
||||
|
||||
func (target *RuleTargetAxis) CreateEvent(value int32, mode *string) *evdev.InputEvent {
|
||||
value = ClampInt(value, MinAxisValue, MaxAxisValue)
|
||||
value = ClampInt(value, AxisValueMin, AxisValueMax)
|
||||
return &evdev.InputEvent{
|
||||
Type: evdev.EV_ABS,
|
||||
Code: target.Axis,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package mappingrules
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/holoplot/go-evdev"
|
||||
|
@ -23,7 +24,7 @@ func (m *InputDeviceMock) AbsInfos() (map[evdev.EvCode]evdev.AbsInfo, error) {
|
|||
return args.Get(0).(map[evdev.EvCode]evdev.AbsInfo), args.Error(1)
|
||||
}
|
||||
|
||||
func (t *RuleTargetAxisTests) SetupSuite() {
|
||||
func (t *RuleTargetAxisTests) SetupTest() {
|
||||
t.mock = new(InputDeviceMock)
|
||||
t.call = t.mock.On("AbsInfos").Return(map[evdev.EvCode]evdev.AbsInfo{
|
||||
evdev.ABS_X: {
|
||||
|
@ -37,7 +38,7 @@ func (t *RuleTargetAxisTests) SetupSuite() {
|
|||
}, nil)
|
||||
}
|
||||
|
||||
func (t *RuleTargetAxisTests) TearDownSuite() {
|
||||
func (t *RuleTargetAxisTests) TearDownTest() {
|
||||
t.call.Unset()
|
||||
}
|
||||
|
||||
|
@ -65,25 +66,32 @@ func (t *RuleTargetAxisTests) TestNewRuleTargetAxis() {
|
|||
// Creating a rule on a non-existent axis should err
|
||||
_, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Z, false, 0, 0)
|
||||
t.NotNil(err)
|
||||
|
||||
// If Absinfo has an error, we should create a device with permissive bounds
|
||||
t.call.Unset()
|
||||
t.mock.On("AbsInfos").Return(nil, errors.New("Test Error"))
|
||||
ruleTarget, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Y, false, -500, 500)
|
||||
t.Nil(err)
|
||||
t.Equal(AxisValueMax-AxisValueMin, ruleTarget.axisSize)
|
||||
}
|
||||
|
||||
func (t *RuleTargetAxisTests) TestNormalizeValue() {
|
||||
// Basic normalization should work
|
||||
ruleTarget, _ := NewRuleTargetAxis("", t.mock, evdev.ABS_X, false, 0, 0)
|
||||
t.Equal(MaxAxisValue, ruleTarget.NormalizeValue(int32(10000)))
|
||||
t.Equal(MinAxisValue, ruleTarget.NormalizeValue(int32(0)))
|
||||
t.Equal(AxisValueMax, ruleTarget.NormalizeValue(int32(10000)))
|
||||
t.Equal(AxisValueMin, ruleTarget.NormalizeValue(int32(0)))
|
||||
t.EqualValues(0, ruleTarget.NormalizeValue(int32(5000)))
|
||||
|
||||
// Normalization with a deadzone should work
|
||||
ruleTarget, _ = NewRuleTargetAxis("", t.mock, evdev.ABS_X, false, 0, 5000)
|
||||
t.Equal(MaxAxisValue, ruleTarget.NormalizeValue(int32(10000)))
|
||||
t.Equal(AxisValueMax, ruleTarget.NormalizeValue(int32(10000)))
|
||||
t.True(ruleTarget.NormalizeValue(int32(5001)) < int32(-31000))
|
||||
t.EqualValues(0, ruleTarget.NormalizeValue(int32(7500)))
|
||||
|
||||
// Normalization on an inverted axis should work
|
||||
ruleTarget, _ = NewRuleTargetAxis("", t.mock, evdev.ABS_X, true, 0, 0)
|
||||
t.Equal(MaxAxisValue, ruleTarget.NormalizeValue(int32(0)))
|
||||
t.Equal(MinAxisValue, ruleTarget.NormalizeValue(int32(10000)))
|
||||
t.Equal(AxisValueMax, ruleTarget.NormalizeValue(int32(0)))
|
||||
t.Equal(AxisValueMin, ruleTarget.NormalizeValue(int32(10000)))
|
||||
}
|
||||
|
||||
func (t *RuleTargetAxisTests) TestMatchEvent() {
|
||||
|
@ -123,11 +131,11 @@ func (t *RuleTargetAxisTests) TestCreateEvent() {
|
|||
|
||||
// Validate axis clamping
|
||||
testValue = int32(64000)
|
||||
expected.Value = MaxAxisValue
|
||||
expected.Value = AxisValueMax
|
||||
t.EqualValues(expected, ruleTarget.CreateEvent(testValue, nil))
|
||||
|
||||
testValue = int32(-64000)
|
||||
expected.Value = MinAxisValue
|
||||
expected.Value = AxisValueMin
|
||||
t.EqualValues(expected, ruleTarget.CreateEvent(testValue, nil))
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue