From 9652df93660411e826ef3ccad546ab184b02fd9d Mon Sep 17 00:00:00 2001 From: Anna Rose Wiggins Date: Fri, 1 Aug 2025 13:49:03 -0400 Subject: [PATCH] Add device locking with a flag to disable for testing. --- cmd/joyful/main.go | 8 +++++--- internal/config/devices.go | 9 +++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/joyful/main.go b/cmd/joyful/main.go index 17482bf..41eaae5 100644 --- a/cmd/joyful/main.go +++ b/cmd/joyful/main.go @@ -52,8 +52,8 @@ func getVirtualDevices(buffers map[string]*virtualdevice.EventBuffer) map[string return devices } -func initPhysicalDevices(config *config.ConfigParser) map[string]*evdev.InputDevice { - pDeviceMap := config.ConnectPhysicalDevices() +func initPhysicalDevices(config *config.ConfigParser, lock bool) map[string]*evdev.InputDevice { + pDeviceMap := config.ConnectPhysicalDevices(lock) if len(pDeviceMap) == 0 { logger.Log("Warning: no physical devices found in configuration. No rules will work.") } @@ -63,7 +63,9 @@ func initPhysicalDevices(config *config.ConfigParser) map[string]*evdev.InputDev func main() { // parse command-line var configFlag string + var noLockFlag bool flag.BoolVarP(&logger.IsDebugMode, "debug", "d", false, "Output very verbose debug messages.") + flag.BoolVar(&noLockFlag, "no-lock", false, "Disable locking the physical devices for exclusive reading.") flag.StringVarP(&configFlag, "config", "c", "~/.config/joyful", "Directory to read configuration from.") ttsOps := addTTSFlags() flag.Parse() @@ -80,7 +82,7 @@ func main() { vBuffersByName, vBuffersByDevice := initVirtualBuffers(config) // Initialize physical devices - pDevices := initPhysicalDevices(config) + pDevices := initPhysicalDevices(config, !noLockFlag) // Load the rules rules, eventChannel, cancel, wg := loadRules(config, pDevices, getVirtualDevices(vBuffersByName)) diff --git a/internal/config/devices.go b/internal/config/devices.go index 5da9849..d77147d 100644 --- a/internal/config/devices.go +++ b/internal/config/devices.go @@ -66,7 +66,7 @@ func (parser *ConfigParser) CreateVirtualDevices() map[string]*evdev.InputDevice // This function assumes you have already called Parse() on the config directory. // // This function should only be called once. -func (parser *ConfigParser) ConnectPhysicalDevices() map[string]*evdev.InputDevice { +func (parser *ConfigParser) ConnectPhysicalDevices(lock bool) map[string]*evdev.InputDevice { deviceMap := make(map[string]*evdev.InputDevice) for _, deviceConfig := range parser.config.Devices { @@ -80,7 +80,12 @@ func (parser *ConfigParser) ConnectPhysicalDevices() map[string]*evdev.InputDevi continue } - // TODO: grab exclusive access to device (add config option) + if lock { + err := device.Grab() + if err != nil { + logger.LogError(err, "Failed to grab device for exclusive access") + } + } logger.Log(fmt.Sprintf("Connected to '%s' as '%s'", deviceConfig.DeviceName, deviceConfig.Name)) deviceMap[deviceConfig.Name] = device