Improve evlist and rename it evinfo #12

Merged
anna merged 2 commits from evinfo into main 2025-07-28 17:45:51 +00:00
5 changed files with 103 additions and 18 deletions

99
cmd/evinfo/main.go Normal file
View file

@ -0,0 +1,99 @@
package main
import (
"fmt"
"slices"
// TODO: using config here feels like bad coupling... ButtonFromIndex might need a refactor / move
"git.annabunches.net/annabunches/joyful/internal/config"
"git.annabunches.net/annabunches/joyful/internal/logger"
"github.com/holoplot/go-evdev"
flag "github.com/spf13/pflag"
)
func isJoystickLike(device *evdev.InputDevice) bool {
types := device.CapableTypes()
if slices.Contains(types, evdev.EV_ABS) {
return true
}
if slices.Contains(types, evdev.EV_KEY) {
buttons := device.CapableEvents(evdev.EV_KEY)
for _, code := range config.ButtonFromIndex {
if slices.Contains(buttons, code) {
return true
}
}
}
return false
}
func printDevice(devPath evdev.InputPath) {
// Get the device
device, err := evdev.Open(devPath.Path)
if err != nil {
fmt.Printf("ERROR: Couldn't get data for device '%s'\n", devPath.Name)
return
}
// If the device doesn't support any joystick-shaped inputs, don't print it.
if !isJoystickLike(device) {
return
}
// Get axis info
var axisOutputs []string
absInfos, err := device.AbsInfos()
if err != nil {
axisOutputs = []string{"ERROR: Failed to get axis data"}
} else {
axisOutputs = make([]string, 0, len(absInfos))
for axisCode, info := range absInfos {
absStr := fmt.Sprintf("%s: %d - %d", evdev.ABSNames[axisCode], info.Minimum, info.Maximum)
axisOutputs = append(axisOutputs, absStr)
}
}
fmt.Printf("%s:\n", devPath.Path)
fmt.Printf("\tName: '%s'\n", devPath.Name)
if len(axisOutputs) > 0 {
fmt.Println("\tAxes:")
for _, str := range axisOutputs {
fmt.Printf("\t\t%s\n", str)
}
}
}
func printDeviceQuiet(devPath evdev.InputPath) {
// If it's not at least minimally joystick-shaped, skip.
// If we can't open the device, err on the side of printing it
// anyway
device, err := evdev.Open(devPath.Path)
if err != nil ||
!isJoystickLike(device) {
return
}
fmt.Printf("'%s'\n", devPath.Name)
}
// TODO: it would be nice to be able to specify a device by name or device file and get axis info
// just for that device
func main() {
var quietFlag bool
flag.BoolVarP(&quietFlag, "quiet", "q", false, "Only print device names")
flag.Parse()
devices, err := evdev.ListDevicePaths()
logger.FatalIfError(err, "")
for _, device := range devices {
if quietFlag {
printDeviceQuiet(device)
} else {
printDevice(device)
}
}
}

View file

@ -1,17 +0,0 @@
package main
import (
"fmt"
"git.annabunches.net/annabunches/joyful/internal/logger"
"github.com/holoplot/go-evdev"
)
func main() {
devices, err := evdev.ListDevicePaths()
logger.FatalIfError(err, "")
for _, device := range devices {
fmt.Printf("%s: '%s'\n", device.Path, device.Name)
}
}

View file

@ -11,7 +11,7 @@ Each entry in `devices` must have a couple of parameters:
`physical` devices must additionally define these parameters:
* `device_name` - The name of the device as reported by the included `evlist` command. If your device name ends with a space, use quotation marks (`""`) around the name.
* `device_name` - The name of the device as reported by the included `evinfo` command. If your device name ends with a space, use quotation marks (`""`) around the name.
`virtual` devices can additionally define these parameters:

1
go.mod
View file

@ -6,6 +6,7 @@ require (
github.com/goccy/go-yaml v1.18.0
github.com/holoplot/go-evdev v0.0.0-20240306072622-217e18f17db1
github.com/jonboulle/clockwork v0.5.0
github.com/spf13/pflag v1.0.7
github.com/stretchr/testify v1.10.0
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b
)

2
go.sum
View file

@ -8,6 +8,8 @@ github.com/jonboulle/clockwork v0.5.0 h1:Hyh9A8u51kptdkR+cqRpT1EebBwTn1oK9YfGYbd
github.com/jonboulle/clockwork v0.5.0/go.mod h1:3mZlmanh0g2NDKO5TWZVJAfofYk64M7XN3SzBPjZF60=
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/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M=
github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
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=