diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d163863 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..6ed2c5b --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,27 @@ +{ + "version": "2.0.0", + "type": "shell", + "command": "go", + "options": { + "cwd": "${workspaceFolder}", + }, + "presentation": { + "reveal": "never", + "revealProblems": "onProblem", + }, + "tasks": [ + { + "label": "Build Project", + "args": [ + "build", + "-o", + "build/", + "./cmd/joyful", + ], + "group": { + "kind": "build", + "isDefault": true, + }, + }, + ], +} \ No newline at end of file diff --git a/cmd/joyful/main.go b/cmd/joyful/main.go index 49cb922..8c21b09 100644 --- a/cmd/joyful/main.go +++ b/cmd/joyful/main.go @@ -5,11 +5,20 @@ import ( "syscall" "time" + "git.annabunches.net/annabunches/joyful/internal/virtualdevice" "github.com/holoplot/go-evdev" ) func main() { + // Check for and destroy any existing joyful devices + virtualdevice.CleanupStaleVirtualDevices() + + // STUB: parse virtual device config + + // STUB: parse mapping config + // Define virtual device + // TODO: create virtual devices from config vDevice, err := evdev.CreateDevice( "joyful-joystick", evdev.InputID{ @@ -36,6 +45,12 @@ func main() { fmt.Printf("Failed to create vDevice: %s", err.Error()) } + location, err := vDevice.PhysicalLocation() + if err != nil { + fmt.Printf("Couldn't get virtual device location: %s\n", err.Error()) + } + fmt.Printf("Device created as %s. Press Ctrl+C to quit and destroy the device.\n", location) + var value int32 = 1 for { eventTime := syscall.NsecToTimeval(int64(time.Now().Nanosecond())) @@ -53,7 +68,7 @@ func main() { value = 0 } - time.Sleep(1000) + time.Sleep(1 * time.Second) } } diff --git a/internal/virtualdevice/cleanup.go b/internal/virtualdevice/cleanup.go new file mode 100644 index 0000000..9839f6b --- /dev/null +++ b/internal/virtualdevice/cleanup.go @@ -0,0 +1,35 @@ +// Functions for cleaning up stale virtual devices + +package virtualdevice + +import ( + "fmt" + "strings" + + "github.com/holoplot/go-evdev" +) + +func CleanupStaleVirtualDevices() { + devices, err := evdev.ListDevicePaths() + if err != nil { + fmt.Printf("Couldn't list devices while running cleanup: %s\n", err.Error()) + return + } + + for _, devicePath := range devices { + if strings.HasPrefix(devicePath.Name, "joyful-joystick") { + device, err := evdev.Open(devicePath.Path) + if err != nil { + fmt.Printf("Failed to open existing joyful device at '%s': %s\n", devicePath.Path, err.Error()) + continue + } + + err = evdev.DestroyDevice(device) + if err != nil { + fmt.Printf("Failed to destroy existing joyful device '%s' at '%s': %s\n", devicePath.Name, devicePath.Path, err.Error()) + } else { + fmt.Printf("Destroyed stale joyful device '%s'\n", devicePath.Path) + } + } + } +}