Code cleanup and refactoring.
This commit is contained in:
parent
4b4930ebdc
commit
a876dabe6e
3 changed files with 19 additions and 25 deletions
|
@ -28,8 +28,11 @@ func readConfig(configDir string) *config.ConfigParser {
|
||||||
return parser
|
return parser
|
||||||
}
|
}
|
||||||
|
|
||||||
func initVirtualBuffers(config *config.ConfigParser) (map[string]*virtualdevice.EventBuffer, map[*evdev.InputDevice]*virtualdevice.EventBuffer) {
|
func initVirtualBuffers(config *config.ConfigParser) (map[string]*evdev.InputDevice,
|
||||||
vDevices := config.CreateVirtualDevices()
|
map[string]*virtualdevice.EventBuffer,
|
||||||
|
map[*evdev.InputDevice]*virtualdevice.EventBuffer) {
|
||||||
|
|
||||||
|
vDevices := config.InitVirtualDevices()
|
||||||
if len(vDevices) == 0 {
|
if len(vDevices) == 0 {
|
||||||
logger.Log("Warning: no virtual devices found in configuration. No rules will work.")
|
logger.Log("Warning: no virtual devices found in configuration. No rules will work.")
|
||||||
}
|
}
|
||||||
|
@ -40,20 +43,11 @@ func initVirtualBuffers(config *config.ConfigParser) (map[string]*virtualdevice.
|
||||||
vBuffersByName[name] = virtualdevice.NewEventBuffer(device)
|
vBuffersByName[name] = virtualdevice.NewEventBuffer(device)
|
||||||
vBuffersByDevice[device] = vBuffersByName[name]
|
vBuffersByDevice[device] = vBuffersByName[name]
|
||||||
}
|
}
|
||||||
return vBuffersByName, vBuffersByDevice
|
return vDevices, vBuffersByName, vBuffersByDevice
|
||||||
}
|
|
||||||
|
|
||||||
// Extracts the evdev devices from a list of virtual buffers and returns them.
|
|
||||||
func getVirtualDevices(buffers map[string]*virtualdevice.EventBuffer) map[string]*evdev.InputDevice {
|
|
||||||
devices := make(map[string]*evdev.InputDevice)
|
|
||||||
for name, buffer := range buffers {
|
|
||||||
devices[name] = buffer.Device.(*evdev.InputDevice)
|
|
||||||
}
|
|
||||||
return devices
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func initPhysicalDevices(config *config.ConfigParser) map[string]*evdev.InputDevice {
|
func initPhysicalDevices(config *config.ConfigParser) map[string]*evdev.InputDevice {
|
||||||
pDeviceMap := config.ConnectPhysicalDevices()
|
pDeviceMap := config.InitPhysicalDevices()
|
||||||
if len(pDeviceMap) == 0 {
|
if len(pDeviceMap) == 0 {
|
||||||
logger.Log("Warning: no physical devices found in configuration. No rules will work.")
|
logger.Log("Warning: no physical devices found in configuration. No rules will work.")
|
||||||
}
|
}
|
||||||
|
@ -77,13 +71,13 @@ func main() {
|
||||||
logger.LogIfError(err, "Failed to initialize TTS")
|
logger.LogIfError(err, "Failed to initialize TTS")
|
||||||
|
|
||||||
// Initialize virtual devices with event buffers
|
// Initialize virtual devices with event buffers
|
||||||
vBuffersByName, vBuffersByDevice := initVirtualBuffers(config)
|
vDevicesByName, vBuffersByName, vBuffersByDevice := initVirtualBuffers(config)
|
||||||
|
|
||||||
// Initialize physical devices
|
// Initialize physical devices
|
||||||
pDevices := initPhysicalDevices(config)
|
pDevices := initPhysicalDevices(config)
|
||||||
|
|
||||||
// Load the rules
|
// Load the rules
|
||||||
rules, eventChannel, cancel, wg := loadRules(config, pDevices, getVirtualDevices(vBuffersByName))
|
rules, eventChannel, cancel, wg := loadRules(config, pDevices, vDevicesByName)
|
||||||
|
|
||||||
// initialize the mode variable
|
// initialize the mode variable
|
||||||
mode := config.GetModes()[0]
|
mode := config.GetModes()[0]
|
||||||
|
@ -139,7 +133,7 @@ func main() {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
fmt.Println("Listeners exited. Parsing config.")
|
fmt.Println("Listeners exited. Parsing config.")
|
||||||
config := readConfig(configDir) // reload the config
|
config := readConfig(configDir) // reload the config
|
||||||
rules, eventChannel, cancel, wg = loadRules(config, pDevices, getVirtualDevices(vBuffersByName))
|
rules, eventChannel, cancel, wg = loadRules(config, pDevices, vDevicesByName)
|
||||||
fmt.Println("Config re-loaded. Only rule changes applied. Device and Mode changes require restart.")
|
fmt.Println("Config re-loaded. Only rule changes applied. Device and Mode changes require restart.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,7 +153,7 @@ func loadRules(
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
// Initialize rules
|
// Initialize rules
|
||||||
rules := config.BuildRules(pDevices, vDevices)
|
rules := config.InitRules(pDevices, vDevices)
|
||||||
logger.Logf("Created %d mapping rules.", len(rules))
|
logger.Logf("Created %d mapping rules.", len(rules))
|
||||||
|
|
||||||
// start listening for events on devices and timers
|
// start listening for events on devices and timers
|
||||||
|
|
|
@ -8,13 +8,13 @@ import (
|
||||||
"github.com/holoplot/go-evdev"
|
"github.com/holoplot/go-evdev"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreateVirtualDevices will register any configured devices with type = virtual
|
// InitVirtualDevices will register any configured devices with type = virtual
|
||||||
// using /dev/uinput, and return a map of those devices.
|
// using /dev/uinput, and return a map of those devices.
|
||||||
//
|
//
|
||||||
// This function assumes you have already called Parse() on the config directory.
|
// This function assumes Parse() has been called.
|
||||||
//
|
//
|
||||||
// This function should only be called once, unless you want to create duplicate devices for some reason.
|
// This function should only be called once, unless we want to create duplicate devices for some reason.
|
||||||
func (parser *ConfigParser) CreateVirtualDevices() map[string]*evdev.InputDevice {
|
func (parser *ConfigParser) InitVirtualDevices() map[string]*evdev.InputDevice {
|
||||||
deviceMap := make(map[string]*evdev.InputDevice)
|
deviceMap := make(map[string]*evdev.InputDevice)
|
||||||
|
|
||||||
for _, deviceConfig := range parser.config.Devices {
|
for _, deviceConfig := range parser.config.Devices {
|
||||||
|
@ -76,13 +76,13 @@ func (parser *ConfigParser) CreateVirtualDevices() map[string]*evdev.InputDevice
|
||||||
return deviceMap
|
return deviceMap
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConnectPhysicalDevices will create InputDevices corresponding to any registered
|
// InitPhysicalDevices will create InputDevices corresponding to any registered
|
||||||
// devices with type = physical.
|
// devices with type = physical.
|
||||||
//
|
//
|
||||||
// This function assumes you have already called Parse() on the config directory.
|
// This function assumes Parse() has been called.
|
||||||
//
|
//
|
||||||
// This function should only be called once.
|
// This function should only be called once.
|
||||||
func (parser *ConfigParser) ConnectPhysicalDevices() map[string]*evdev.InputDevice {
|
func (parser *ConfigParser) InitPhysicalDevices() map[string]*evdev.InputDevice {
|
||||||
deviceMap := make(map[string]*evdev.InputDevice)
|
deviceMap := make(map[string]*evdev.InputDevice)
|
||||||
|
|
||||||
for _, deviceConfig := range parser.config.Devices {
|
for _, deviceConfig := range parser.config.Devices {
|
||||||
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
// This would speed up rule matching by only checking relevant rules for a given input event.
|
// This would speed up rule matching by only checking relevant rules for a given input event.
|
||||||
// We could take this further and make it a map[<struct of *inputdevice, type, and code>][]rule
|
// We could take this further and make it a map[<struct of *inputdevice, type, and code>][]rule
|
||||||
// For very large rule-bases this may be helpful for staying performant.
|
// For very large rule-bases this may be helpful for staying performant.
|
||||||
func (parser *ConfigParser) BuildRules(pInputDevs map[string]*evdev.InputDevice, vInputDevs map[string]*evdev.InputDevice) []mappingrules.MappingRule {
|
func (parser *ConfigParser) InitRules(pInputDevs map[string]*evdev.InputDevice, vInputDevs map[string]*evdev.InputDevice) []mappingrules.MappingRule {
|
||||||
rules := make([]mappingrules.MappingRule, 0)
|
rules := make([]mappingrules.MappingRule, 0)
|
||||||
modes := parser.GetModes()
|
modes := parser.GetModes()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue