Implement axis targets, axis -> button and axis -> relative axis mappings. #1
8 changed files with 164 additions and 9 deletions
1
go.mod
1
go.mod
|
@ -12,5 +12,6 @@ require (
|
|||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/objx v0.5.2 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
|
2
go.sum
2
go.sum
|
@ -6,6 +6,8 @@ github.com/holoplot/go-evdev v0.0.0-20240306072622-217e18f17db1 h1:92OsBIf5KB1Ta
|
|||
github.com/holoplot/go-evdev v0.0.0-20240306072622-217e18f17db1/go.mod h1:iHAf8OIncO2gcQ8XOjS7CMJ2aPbX2Bs0wl5pZyanEqk=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
|
||||
|
|
|
@ -26,3 +26,10 @@ type RuleTarget interface {
|
|||
// for most implementations.
|
||||
CreateEvent(int32, *string) *evdev.InputEvent
|
||||
}
|
||||
|
||||
// RuleTargetDevice is an interface abstraction on top of evdev.InputDevice, implementing
|
||||
// only the methods we need in this package. This is used for testing, and the
|
||||
// RuleTargetDevice can be safely cast to an *evdev.InputDevice when necessary.
|
||||
type RuleTargetDevice interface {
|
||||
AbsInfos() (map[evdev.EvCode]evdev.AbsInfo, error)
|
||||
}
|
||||
|
|
|
@ -23,5 +23,6 @@ func (rule *MappingRuleAxis) MatchEvent(device *evdev.InputDevice, event *evdev.
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
return rule.Output.Device, rule.Output.CreateEvent(rule.Input.NormalizeValue(event.Value), mode)
|
||||
// The cast here is safe because the interface is only ever different for unit tests
|
||||
return rule.Output.Device.(*evdev.InputDevice), rule.Output.CreateEvent(rule.Input.NormalizeValue(event.Value), mode)
|
||||
}
|
||||
|
|
|
@ -80,6 +80,6 @@ func (t *MappingRuleButtonTests) TestMatchEventInverted() {
|
|||
t.EqualValues(expected, event)
|
||||
}
|
||||
|
||||
func TestRunnerMatching(t *testing.T) {
|
||||
func TestRunnerMappingRuleButtonTests(t *testing.T) {
|
||||
suite.Run(t, new(MappingRuleButtonTests))
|
||||
}
|
||||
|
|
|
@ -13,3 +13,13 @@ func AbsInt[T constraints.Integer](value T) T {
|
|||
func LerpInt[T constraints.Integer](min, max T, t float64) T {
|
||||
return T((1-t)*float64(min) + t*float64(max))
|
||||
}
|
||||
|
||||
func ClampInt[T constraints.Integer](value, min, max T) T {
|
||||
if value < min {
|
||||
value = min
|
||||
}
|
||||
if value > max {
|
||||
value = max
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
|
|
@ -9,12 +9,11 @@ import (
|
|||
|
||||
type RuleTargetAxis struct {
|
||||
DeviceName string
|
||||
Device *evdev.InputDevice
|
||||
Device RuleTargetDevice
|
||||
Axis evdev.EvCode
|
||||
Inverted bool
|
||||
DeadzoneStart int32
|
||||
DeadzoneEnd int32
|
||||
Sensitivity float64 // TODO: is this even a value that makes sense?
|
||||
axisSize int32
|
||||
deadzoneSize int32
|
||||
}
|
||||
|
@ -25,7 +24,7 @@ const (
|
|||
)
|
||||
|
||||
func NewRuleTargetAxis(device_name string,
|
||||
device *evdev.InputDevice,
|
||||
device RuleTargetDevice,
|
||||
axis evdev.EvCode,
|
||||
inverted bool,
|
||||
deadzoneStart int32,
|
||||
|
@ -83,8 +82,7 @@ func (target *RuleTargetAxis) NormalizeValue(value int32) int32 {
|
|||
}
|
||||
|
||||
func (target *RuleTargetAxis) CreateEvent(value int32, mode *string) *evdev.InputEvent {
|
||||
// TODO: we can use the axis begin/end to decide whether to emit the event
|
||||
// TODO: oh no we need center deadzones actually...
|
||||
value = ClampInt(value, MinAxisValue, MaxAxisValue)
|
||||
return &evdev.InputEvent{
|
||||
Type: evdev.EV_ABS,
|
||||
Code: target.Axis,
|
||||
|
@ -92,9 +90,9 @@ func (target *RuleTargetAxis) CreateEvent(value int32, mode *string) *evdev.Inpu
|
|||
}
|
||||
}
|
||||
|
||||
func (target *RuleTargetAxis) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent) bool {
|
||||
func (target *RuleTargetAxis) MatchEvent(device RuleTargetDevice, event *evdev.InputEvent) bool {
|
||||
return device == target.Device &&
|
||||
event.Type == evdev.EV_ABS &&
|
||||
event.Code == target.Axis &&
|
||||
(event.Value <= target.DeadzoneStart || event.Value >= target.DeadzoneEnd)
|
||||
(event.Value < target.DeadzoneStart || event.Value > target.DeadzoneEnd)
|
||||
}
|
||||
|
|
136
internal/mappingrules/rule_target_axis_test.go
Normal file
136
internal/mappingrules/rule_target_axis_test.go
Normal file
|
@ -0,0 +1,136 @@
|
|||
package mappingrules
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/holoplot/go-evdev"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type RuleTargetAxisTests struct {
|
||||
suite.Suite
|
||||
mock *InputDeviceMock
|
||||
call *mock.Call
|
||||
}
|
||||
|
||||
type InputDeviceMock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *InputDeviceMock) AbsInfos() (map[evdev.EvCode]evdev.AbsInfo, error) {
|
||||
args := m.Called()
|
||||
return args.Get(0).(map[evdev.EvCode]evdev.AbsInfo), args.Error(1)
|
||||
}
|
||||
|
||||
func (t *RuleTargetAxisTests) SetupSuite() {
|
||||
t.mock = new(InputDeviceMock)
|
||||
t.call = t.mock.On("AbsInfos").Return(map[evdev.EvCode]evdev.AbsInfo{
|
||||
evdev.ABS_X: {
|
||||
Minimum: 0,
|
||||
Maximum: 10000,
|
||||
},
|
||||
evdev.ABS_Y: {
|
||||
Minimum: -10000,
|
||||
Maximum: 10000,
|
||||
},
|
||||
}, nil)
|
||||
}
|
||||
|
||||
func (t *RuleTargetAxisTests) TearDownSuite() {
|
||||
t.call.Unset()
|
||||
}
|
||||
|
||||
func (t *RuleTargetAxisTests) TestNewRuleTargetAxis() {
|
||||
// RuleTargets should get created
|
||||
ruleTarget, err := NewRuleTargetAxis("", t.mock, evdev.ABS_X, false, 0, 0)
|
||||
t.Nil(err)
|
||||
t.EqualValues(10000, ruleTarget.axisSize)
|
||||
|
||||
ruleTarget, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Y, false, 0, 0)
|
||||
t.Nil(err)
|
||||
t.EqualValues(20000, ruleTarget.axisSize)
|
||||
|
||||
// Creating a rule with a deadzone should work and reduce the axisSize
|
||||
ruleTarget, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Y, false, -500, 500)
|
||||
t.Nil(err)
|
||||
t.EqualValues(19000, ruleTarget.axisSize)
|
||||
t.EqualValues(-500, ruleTarget.DeadzoneStart)
|
||||
t.EqualValues(500, ruleTarget.DeadzoneEnd)
|
||||
|
||||
// Creating a rule with a deadzone should fail if end > start
|
||||
_, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Y, false, 500, -500)
|
||||
t.NotNil(err)
|
||||
|
||||
// Creating a rule on a non-existent axis should err
|
||||
_, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Z, false, 0, 0)
|
||||
t.NotNil(err)
|
||||
}
|
||||
|
||||
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.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.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)))
|
||||
}
|
||||
|
||||
func (t *RuleTargetAxisTests) TestMatchEvent() {
|
||||
ruleTarget, _ := NewRuleTargetAxis("", t.mock, evdev.ABS_Y, false, -500, 500)
|
||||
validEvent := &evdev.InputEvent{
|
||||
Type: evdev.EV_ABS,
|
||||
Code: evdev.ABS_Y,
|
||||
Value: 800,
|
||||
}
|
||||
deadzoneEvent := &evdev.InputEvent{
|
||||
Type: evdev.EV_ABS,
|
||||
Code: evdev.ABS_Y,
|
||||
Value: 200,
|
||||
}
|
||||
|
||||
// An event on the correct device and axis should match
|
||||
t.True(ruleTarget.MatchEvent(t.mock, validEvent))
|
||||
|
||||
// A value on the wrong device should not match
|
||||
t.False(ruleTarget.MatchEvent(&evdev.InputDevice{}, validEvent))
|
||||
|
||||
// A value in the deadzone should not match
|
||||
t.False(ruleTarget.MatchEvent(t.mock, deadzoneEvent))
|
||||
}
|
||||
|
||||
func (t *RuleTargetAxisTests) TestCreateEvent() {
|
||||
ruleTarget, _ := NewRuleTargetAxis("", t.mock, evdev.ABS_X, false, 0, 0)
|
||||
expected := &evdev.InputEvent{
|
||||
Type: evdev.EV_ABS,
|
||||
Code: evdev.ABS_X,
|
||||
}
|
||||
|
||||
// Basic event creation
|
||||
testValue := int32(3928) // Arbitrarily chosen test value
|
||||
expected.Value = testValue
|
||||
t.EqualValues(expected, ruleTarget.CreateEvent(testValue, nil))
|
||||
|
||||
// Validate axis clamping
|
||||
testValue = int32(64000)
|
||||
expected.Value = MaxAxisValue
|
||||
t.EqualValues(expected, ruleTarget.CreateEvent(testValue, nil))
|
||||
|
||||
testValue = int32(-64000)
|
||||
expected.Value = MinAxisValue
|
||||
t.EqualValues(expected, ruleTarget.CreateEvent(testValue, nil))
|
||||
}
|
||||
|
||||
func TestRunnerRuleTargetAxisTests(t *testing.T) {
|
||||
suite.Run(t, new(RuleTargetAxisTests))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue