From f6162d0f7b769b2738c4eb28af370030b76de8b2 Mon Sep 17 00:00:00 2001 From: Anna Rose Wiggins Date: Fri, 4 Jul 2025 09:40:35 -0400 Subject: [PATCH] Refactoring. --- cmd/joyful/main.go | 51 +------------------------------------------ cmd/joyful/threads.go | 39 +++++++++++++++++++++++++++++++++ cmd/joyful/types.go | 16 ++++++++++++++ 3 files changed, 56 insertions(+), 50 deletions(-) create mode 100644 cmd/joyful/threads.go create mode 100644 cmd/joyful/types.go diff --git a/cmd/joyful/main.go b/cmd/joyful/main.go index 9fe6d4d..87748c2 100644 --- a/cmd/joyful/main.go +++ b/cmd/joyful/main.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "path/filepath" - "time" "git.annabunches.net/annabunches/joyful/internal/config" "git.annabunches.net/annabunches/joyful/internal/logger" @@ -13,11 +12,6 @@ import ( "github.com/holoplot/go-evdev" ) -const ( - TimerCheckIntervalMs = 250 - DeviceCheckIntervalMs = 1 -) - func readConfig() *config.ConfigParser { parser := &config.ConfigParser{} homeDir, err := os.UserHomeDir() @@ -71,25 +65,7 @@ func main() { rules := config.BuildRules(pDevices, getVirtualDevices(vBuffers)) logger.Logf("Created %d mapping rules.", len(rules)) - // Listen for events and map them forever - mapEvents(vBuffers, pDevices, rules) -} - -type ChannelEventType int - -const ( - ChannelEventInput ChannelEventType = iota - ChannelEventTimer -) - -type ChannelEvent struct { - Type ChannelEventType - Device *evdev.InputDevice - Event *evdev.InputEvent -} - -func mapEvents(vBuffers map[string]*virtualdevice.EventBuffer, pDevices map[string]*evdev.InputDevice, rules []mappingrules.MappingRule) { - // start listening for events on all devices + // start listening for events on devices and timers eventChannel := make(chan ChannelEvent, 1000) for _, device := range pDevices { go eventWatcher(device, eventChannel) @@ -139,28 +115,3 @@ func mapEvents(vBuffers map[string]*virtualdevice.EventBuffer, pDevices map[stri } } } - -func eventWatcher(device *evdev.InputDevice, channel chan<- ChannelEvent) { - for { - event, err := device.ReadOne() - if err != nil { - logger.LogError(err, "Error while reading event. Disconnecting device.") - return - } - channel <- ChannelEvent{Device: device, Event: event, Type: ChannelEventInput} - - if event.Type == evdev.EV_SYN { - time.Sleep(DeviceCheckIntervalMs * time.Millisecond) - } - } -} - -func timerWatcher(rule *mappingrules.ProportionalAxisMappingRule, channel chan<- ChannelEvent) { - for { - event := rule.TimerEvent() - if event != nil { - channel <- ChannelEvent{Device: rule.Output.Device, Event: event, Type: ChannelEventTimer} - } - time.Sleep(TimerCheckIntervalMs * time.Millisecond) - } -} diff --git a/cmd/joyful/threads.go b/cmd/joyful/threads.go new file mode 100644 index 0000000..410bf5c --- /dev/null +++ b/cmd/joyful/threads.go @@ -0,0 +1,39 @@ +package main + +import ( + "time" + + "git.annabunches.net/annabunches/joyful/internal/logger" + "git.annabunches.net/annabunches/joyful/internal/mappingrules" + "github.com/holoplot/go-evdev" +) + +const ( + TimerCheckIntervalMs = 250 + DeviceCheckIntervalMs = 1 +) + +func eventWatcher(device *evdev.InputDevice, channel chan<- ChannelEvent) { + for { + event, err := device.ReadOne() + if err != nil { + logger.LogError(err, "Error while reading event. Disconnecting device.") + return + } + channel <- ChannelEvent{Device: device, Event: event, Type: ChannelEventInput} + + if event.Type == evdev.EV_SYN { + time.Sleep(DeviceCheckIntervalMs * time.Millisecond) + } + } +} + +func timerWatcher(rule *mappingrules.ProportionalAxisMappingRule, channel chan<- ChannelEvent) { + for { + event := rule.TimerEvent() + if event != nil { + channel <- ChannelEvent{Device: rule.Output.Device, Event: event, Type: ChannelEventTimer} + } + time.Sleep(TimerCheckIntervalMs * time.Millisecond) + } +} diff --git a/cmd/joyful/types.go b/cmd/joyful/types.go new file mode 100644 index 0000000..33cc015 --- /dev/null +++ b/cmd/joyful/types.go @@ -0,0 +1,16 @@ +package main + +import "github.com/holoplot/go-evdev" + +type ChannelEventType int + +const ( + ChannelEventInput ChannelEventType = iota + ChannelEventTimer +) + +type ChannelEvent struct { + Type ChannelEventType + Device *evdev.InputDevice + Event *evdev.InputEvent +}