Add more tests (#4)
This also refactors some of the code parsing logic. Reviewed-on: #4 Co-authored-by: Anna Rose Wiggins <annabunches@gmail.com> Co-committed-by: Anna Rose Wiggins <annabunches@gmail.com>
This commit is contained in:
parent
a05dc9126d
commit
712dcdbc07
7 changed files with 198 additions and 83 deletions
|
@ -3,8 +3,6 @@ package config
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.annabunches.net/annabunches/joyful/internal/mappingrules"
|
||||
"github.com/holoplot/go-evdev"
|
||||
|
@ -16,39 +14,9 @@ func makeRuleTargetButton(targetConfig RuleTargetConfig, devs map[string]*evdev.
|
|||
return nil, fmt.Errorf("non-existent device '%s'", targetConfig.Device)
|
||||
}
|
||||
|
||||
var eventCode evdev.EvCode
|
||||
buttonConfig := strings.ToUpper(targetConfig.Button)
|
||||
switch {
|
||||
case strings.HasPrefix(buttonConfig, "BTN_"):
|
||||
eventCode, ok = evdev.KEYFromString[buttonConfig]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid button specification '%s'", buttonConfig)
|
||||
}
|
||||
|
||||
case strings.HasPrefix(buttonConfig, "0X"):
|
||||
codeInt, err := strconv.ParseUint(buttonConfig[2:], 16, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
eventCode = evdev.EvCode(codeInt)
|
||||
|
||||
case !hasError(strconv.Atoi(buttonConfig)):
|
||||
index, err := strconv.Atoi(buttonConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if index >= len(ButtonFromIndex) {
|
||||
return nil, fmt.Errorf("button index '%d' out of bounds", index)
|
||||
}
|
||||
|
||||
eventCode = ButtonFromIndex[index]
|
||||
|
||||
default:
|
||||
eventCode, ok = evdev.KEYFromString["BTN_"+buttonConfig]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid button specification '%s'", buttonConfig)
|
||||
}
|
||||
eventCode, err := parseCode(targetConfig.Button, "BTN")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mappingrules.NewRuleTargetButton(
|
||||
|
@ -69,27 +37,9 @@ func makeRuleTargetAxis(targetConfig RuleTargetConfig, devs map[string]*evdev.In
|
|||
return nil, errors.New("deadzone_end must be greater than deadzone_start")
|
||||
}
|
||||
|
||||
var eventCode evdev.EvCode
|
||||
axisConfig := strings.ToUpper(targetConfig.Axis)
|
||||
switch {
|
||||
case strings.HasPrefix(axisConfig, "ABS_"):
|
||||
eventCode, ok = evdev.ABSFromString[axisConfig]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid axis code '%s'", axisConfig)
|
||||
}
|
||||
|
||||
case strings.HasPrefix(axisConfig, "0X"):
|
||||
codeInt, err := strconv.ParseUint(axisConfig[2:], 16, 32)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
eventCode = evdev.EvCode(codeInt)
|
||||
|
||||
default:
|
||||
eventCode, ok = evdev.ABSFromString["ABS_"+axisConfig]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid axis code '%s'", axisConfig)
|
||||
}
|
||||
eventCode, err := parseCode(targetConfig.Axis, "ABS")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mappingrules.NewRuleTargetAxis(
|
||||
|
@ -108,28 +58,11 @@ func makeRuleTargetRelaxis(targetConfig RuleTargetConfig, devs map[string]*evdev
|
|||
return nil, fmt.Errorf("non-existent device '%s'", targetConfig.Device)
|
||||
}
|
||||
|
||||
var eventCode evdev.EvCode
|
||||
axisConfig := strings.ToUpper(targetConfig.Axis)
|
||||
switch {
|
||||
case strings.HasPrefix(axisConfig, "REL_"):
|
||||
eventCode, ok = evdev.RELFromString[axisConfig]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid axis code '%s'", axisConfig)
|
||||
}
|
||||
|
||||
case strings.HasPrefix(axisConfig, "0X"):
|
||||
codeInt, err := strconv.ParseUint(axisConfig[2:], 16, 32)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
eventCode = evdev.EvCode(codeInt)
|
||||
|
||||
default:
|
||||
eventCode, ok = evdev.RELFromString["REL_"+axisConfig]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid axis code '%s'", axisConfig)
|
||||
}
|
||||
eventCode, err := parseCode(targetConfig.Axis, "REL")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mappingrules.NewRuleTargetRelaxis(
|
||||
targetConfig.Device,
|
||||
device,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue