Support live re-loading of rules. (#2)

Reviewed-on: #2
Co-authored-by: Anna Rose Wiggins <annabunches@gmail.com>
Co-committed-by: Anna Rose Wiggins <annabunches@gmail.com>
This commit is contained in:
Anna Rose Wiggins 2025-07-15 23:38:53 +00:00 committed by Anna Rose Wiggins
parent 4ebcbb4dc9
commit e1940006d8
4 changed files with 102 additions and 25 deletions

View file

@ -1,6 +1,9 @@
package main
import (
"bufio"
"os"
"sync"
"time"
"git.annabunches.net/annabunches/joyful/internal/logger"
@ -13,8 +16,24 @@ const (
DeviceCheckIntervalMs = 1
)
func eventWatcher(device *evdev.InputDevice, channel chan<- ChannelEvent) {
func eventWatcher(
device *evdev.InputDevice,
channel chan<- ChannelEvent,
done chan bool,
wg *sync.WaitGroup) {
defer wg.Done()
for {
select {
case cancel := <-done:
if cancel {
done <- true
return
}
default:
}
event, err := device.ReadOne()
if err != nil {
logger.LogError(err, "Error while reading event. Disconnecting device.")
@ -28,8 +47,24 @@ func eventWatcher(device *evdev.InputDevice, channel chan<- ChannelEvent) {
}
}
func timerWatcher(rule mappingrules.TimedEventEmitter, channel chan<- ChannelEvent) {
func timerWatcher(
rule mappingrules.TimedEventEmitter,
channel chan<- ChannelEvent,
done chan bool,
wg *sync.WaitGroup) {
defer wg.Done()
for {
select {
case cancel := <-done:
if cancel {
done <- true
return
}
default:
}
event := rule.TimerEvent()
if event != nil {
channel <- ChannelEvent{
@ -41,3 +76,21 @@ func timerWatcher(rule mappingrules.TimedEventEmitter, channel chan<- ChannelEve
time.Sleep(TimerCheckIntervalMs * time.Millisecond)
}
}
// consoleWatcher reads input from stdin, and on receiving anything
func consoleWatcher(channel chan<- ChannelEvent, wg *sync.WaitGroup) {
defer wg.Done()
stdin := bufio.NewReader(os.Stdin)
for {
_, err := stdin.ReadString('\n')
if err != nil {
logger.LogErrorf(err, "Error in console input thread")
continue
}
channel <- ChannelEvent{
Type: ChannelEventReload,
}
return
}
}