From 0ebca5a84c642e06bca660f2879dbd9943757bd0 Mon Sep 17 00:00:00 2001 From: Anna Rose Wiggins Date: Mon, 28 Jul 2025 17:45:51 +0000 Subject: [PATCH] Improve evlist and rename it evinfo (#12) Reviewed-on: https://git.annabunches.net/anna/joyful/pulls/12 --- cmd/evinfo/main.go | 99 ++++++++++++++++++++++++++++++++++++++++++++++ cmd/evlist/main.go | 17 -------- docs/readme.md | 2 +- go.mod | 1 + go.sum | 2 + 5 files changed, 103 insertions(+), 18 deletions(-) create mode 100644 cmd/evinfo/main.go delete mode 100644 cmd/evlist/main.go diff --git a/cmd/evinfo/main.go b/cmd/evinfo/main.go new file mode 100644 index 0000000..d9e600b --- /dev/null +++ b/cmd/evinfo/main.go @@ -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) + } + } +} diff --git a/cmd/evlist/main.go b/cmd/evlist/main.go deleted file mode 100644 index 568ce87..0000000 --- a/cmd/evlist/main.go +++ /dev/null @@ -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) - } -} diff --git a/docs/readme.md b/docs/readme.md index 58e5453..c6ffe75 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -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: diff --git a/go.mod b/go.mod index b469a18..26d806f 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 4942bf2..baba603 100644 --- a/go.sum +++ b/go.sum @@ -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=