Add device locking with a flag to disable for testing.

This commit is contained in:
Anna Rose Wiggins 2025-08-01 13:49:03 -04:00
parent 7f104f054a
commit 9652df9366
2 changed files with 12 additions and 5 deletions

View file

@ -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))

View file

@ -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