Fix explicit device capabilities and add tests.

This commit is contained in:
Anna Rose Wiggins 2025-07-17 15:30:32 -04:00
parent 10d8c67a68
commit 56e38a9ba1
2 changed files with 95 additions and 29 deletions

View file

@ -23,6 +23,12 @@ func (parser *ConfigParser) CreateVirtualDevices() map[string]*evdev.InputDevice
}
name := fmt.Sprintf("joyful-%s", deviceConfig.Name)
capabilities := map[evdev.EvType][]evdev.EvCode{
evdev.EV_KEY: makeButtons(deviceConfig.NumButtons, deviceConfig.Buttons),
evdev.EV_ABS: makeAxes(deviceConfig.NumAxes, deviceConfig.Axes),
evdev.EV_REL: makeRelativeAxes(deviceConfig.NumRelativeAxes, deviceConfig.RelativeAxes),
}
device, err := evdev.CreateDevice(
name,
// TODO: who knows what these should actually be
@ -32,11 +38,7 @@ func (parser *ConfigParser) CreateVirtualDevices() map[string]*evdev.InputDevice
Product: 0x0816,
Version: 1,
},
map[evdev.EvType][]evdev.EvCode{
evdev.EV_KEY: makeButtons(deviceConfig.NumButtons, deviceConfig.Buttons),
evdev.EV_ABS: makeAxes(deviceConfig.NumAxes, deviceConfig.Axes),
evdev.EV_REL: makeRelativeAxes(deviceConfig.NumRelativeAxes, deviceConfig.RelativeAxes),
},
capabilities,
)
if err != nil {
@ -45,7 +47,13 @@ func (parser *ConfigParser) CreateVirtualDevices() map[string]*evdev.InputDevice
}
deviceMap[deviceConfig.Name] = device
logger.Log(fmt.Sprintf("Created virtual device '%s'", name))
logger.Log(fmt.Sprintf(
"Created virtual device '%s' with %d buttons, %d axes, and %d relative axes",
name,
len(capabilities[evdev.EV_KEY]),
len(capabilities[evdev.EV_ABS]),
len(capabilities[evdev.EV_REL]),
))
}
return deviceMap
@ -81,6 +89,8 @@ func (parser *ConfigParser) ConnectPhysicalDevices() map[string]*evdev.InputDevi
return deviceMap
}
// TODO: these functions have a lot of duplication; we need to figure out how to refactor it cleanly
// without losing logging context...
func makeButtons(numButtons int, buttonList []string) []evdev.EvCode {
if numButtons > 0 && len(buttonList) > 0 {
logger.Log("'num_buttons' and 'buttons' both specified, ignoring 'num_buttons'")
@ -92,7 +102,7 @@ func makeButtons(numButtons int, buttonList []string) []evdev.EvCode {
}
if len(buttonList) > 0 {
buttons := make([]evdev.EvCode, len(buttonList))
buttons := make([]evdev.EvCode, 0, len(buttonList))
for _, codeStr := range buttonList {
code, err := parseCode(codeStr, "BTN")
if err != nil {
@ -127,7 +137,7 @@ func makeAxes(numAxes int, axisList []string) []evdev.EvCode {
}
if len(axisList) > 0 {
axes := make([]evdev.EvCode, len(axisList))
axes := make([]evdev.EvCode, 0, len(axisList))
for _, codeStr := range axisList {
code, err := parseCode(codeStr, "ABS")
if err != nil {
@ -158,7 +168,7 @@ func makeRelativeAxes(numAxes int, axisList []string) []evdev.EvCode {
}
if len(axisList) > 0 {
axes := make([]evdev.EvCode, len(axisList))
axes := make([]evdev.EvCode, 0, len(axisList))
for _, codeStr := range axisList {
code, err := parseCode(codeStr, "REL")
if err != nil {
@ -170,9 +180,9 @@ func makeRelativeAxes(numAxes int, axisList []string) []evdev.EvCode {
return axes
}
if numAxes > 8 {
numAxes = 8
logger.Log("Limiting virtual device relative axes to 8")
if numAxes > 10 {
numAxes = 10
logger.Log("Limiting virtual device relative axes to 10")
}
axes := make([]evdev.EvCode, numAxes)