Add stale cleanup function (still WIP)
This commit is contained in:
parent
309b3d3984
commit
4d8cfb9298
4 changed files with 79 additions and 1 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
build/
|
27
.vscode/tasks.json
vendored
Normal file
27
.vscode/tasks.json
vendored
Normal file
|
@ -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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
|
@ -5,11 +5,20 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.annabunches.net/annabunches/joyful/internal/virtualdevice"
|
||||||
"github.com/holoplot/go-evdev"
|
"github.com/holoplot/go-evdev"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// Check for and destroy any existing joyful devices
|
||||||
|
virtualdevice.CleanupStaleVirtualDevices()
|
||||||
|
|
||||||
|
// STUB: parse virtual device config
|
||||||
|
|
||||||
|
// STUB: parse mapping config
|
||||||
|
|
||||||
// Define virtual device
|
// Define virtual device
|
||||||
|
// TODO: create virtual devices from config
|
||||||
vDevice, err := evdev.CreateDevice(
|
vDevice, err := evdev.CreateDevice(
|
||||||
"joyful-joystick",
|
"joyful-joystick",
|
||||||
evdev.InputID{
|
evdev.InputID{
|
||||||
|
@ -36,6 +45,12 @@ func main() {
|
||||||
fmt.Printf("Failed to create vDevice: %s", err.Error())
|
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
|
var value int32 = 1
|
||||||
for {
|
for {
|
||||||
eventTime := syscall.NsecToTimeval(int64(time.Now().Nanosecond()))
|
eventTime := syscall.NsecToTimeval(int64(time.Now().Nanosecond()))
|
||||||
|
@ -53,7 +68,7 @@ func main() {
|
||||||
value = 0
|
value = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(1000)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
35
internal/virtualdevice/cleanup.go
Normal file
35
internal/virtualdevice/cleanup.go
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue