Configure per-device locking with default set to true.

This commit is contained in:
Anna Rose Wiggins 2025-08-04 15:28:06 -04:00
parent e4246dd62e
commit ff582d864f
3 changed files with 49 additions and 9 deletions

View file

@ -52,8 +52,8 @@ func getVirtualDevices(buffers map[string]*virtualdevice.EventBuffer) map[string
return devices
}
func initPhysicalDevices(config *config.ConfigParser, lock bool) map[string]*evdev.InputDevice {
pDeviceMap := config.ConnectPhysicalDevices(lock)
func initPhysicalDevices(config *config.ConfigParser) map[string]*evdev.InputDevice {
pDeviceMap := config.ConnectPhysicalDevices()
if len(pDeviceMap) == 0 {
logger.Log("Warning: no physical devices found in configuration. No rules will work.")
}
@ -63,9 +63,7 @@ func initPhysicalDevices(config *config.ConfigParser, lock bool) map[string]*evd
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()
@ -82,7 +80,7 @@ func main() {
vBuffersByName, vBuffersByDevice := initVirtualBuffers(config)
// Initialize physical devices
pDevices := initPhysicalDevices(config, !noLockFlag)
pDevices := initPhysicalDevices(config)
// Load the rules
rules, eventChannel, cancel, wg := loadRules(config, pDevices, getVirtualDevices(vBuffersByName))

View file

@ -75,13 +75,12 @@ func (parser *ConfigParser) CreateVirtualDevices() map[string]*evdev.InputDevice
}
// ConnectPhysicalDevices will create InputDevices corresponding to any registered
// devices with type = physical. It will also attempt to acquire exclusive access
// to those devices, to prevent the same inputs from being read on multiple devices.
// devices with type = physical.
//
// This function assumes you have already called Parse() on the config directory.
//
// This function should only be called once.
func (parser *ConfigParser) ConnectPhysicalDevices(lock bool) map[string]*evdev.InputDevice {
func (parser *ConfigParser) ConnectPhysicalDevices() map[string]*evdev.InputDevice {
deviceMap := make(map[string]*evdev.InputDevice)
for _, deviceConfig := range parser.config.Devices {
@ -95,7 +94,8 @@ func (parser *ConfigParser) ConnectPhysicalDevices(lock bool) map[string]*evdev.
continue
}
if lock {
if deviceConfig.Lock {
logger.LogDebugf("Locking device '%s'", deviceConfig.DeviceName)
err := device.Grab()
if err != nil {
logger.LogError(err, "Failed to grab device for exclusive access")

View file

@ -9,6 +9,8 @@
package config
import "git.annabunches.net/annabunches/joyful/internal/logger"
type Config struct {
Devices []DeviceConfig `yaml:"devices"`
Modes []string `yaml:"modes,omitempty"`
@ -27,6 +29,7 @@ type DeviceConfig struct {
Buttons []string `yaml:"buttons,omitempty"`
Axes []string `yaml:"axes,omitempty"`
RelativeAxes []string `yaml:"rel_axes,omitempty"`
Lock bool `yaml:"lock,omitempty"`
}
type RuleConfig struct {
@ -55,3 +58,42 @@ type RuleTargetConfig struct {
Inverted bool `yaml:"inverted,omitempty"`
Modes []string `yaml:"modes,omitempty"`
}
func (dc *DeviceConfig) UnmarshalYAML(unmarshal func(data interface{}) error) error {
var raw struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
DeviceName string `yaml:"device_name,omitempty"`
Uuid string `yaml:"uuid,omitempty"`
Preset string `yaml:"preset,omitempty"`
NumButtons int `yaml:"num_buttons,omitempty"`
NumAxes int `yaml:"num_axes,omitempty"`
NumRelativeAxes int `yaml:"num_rel_axes"`
Buttons []string `yaml:"buttons,omitempty"`
Axes []string `yaml:"axes,omitempty"`
RelativeAxes []string `yaml:"rel_axes,omitempty"`
Lock bool `yaml:"lock,omitempty"`
}
raw.Lock = true
err := unmarshal(&raw)
if err != nil {
return err
}
logger.LogDebugf("%v", raw)
*dc = DeviceConfig{
Name: raw.Name,
Type: raw.Type,
DeviceName: raw.DeviceName,
Uuid: raw.Uuid,
Preset: raw.Preset,
NumButtons: raw.NumButtons,
NumAxes: raw.NumAxes,
NumRelativeAxes: raw.NumRelativeAxes,
Buttons: raw.Buttons,
Axes: raw.Axes,
RelativeAxes: raw.RelativeAxes,
Lock: raw.Lock,
}
return nil
}