-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.go
More file actions
44 lines (41 loc) · 1.06 KB
/
Copy pathclock.go
File metadata and controls
44 lines (41 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"log/slog"
"os"
"os/signal"
"time"
)
const (
acpiLidOpenEvent = "button/lid LID open"
acpiListenCmd = "acpi_listen"
)
// repeatUntilInterrupt runs the given function every interval.
// It will return whenever one of the signals in interruptSignals is received.
func repeatUntilInterrupt(callback func(), interval time.Duration, interruptSignals ...os.Signal) {
slog.Info("running continuously")
slog.Debug("loop timing", "interval", DefaultLoopInterval)
ticker := time.NewTicker(interval)
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, interruptSignals...)
quit := make(chan bool)
acpiEvent, acpiErr := AcpiEvent(acpiListenCmd, acpiLidOpenEvent)
if acpiErr != nil {
slog.Warn("ACPI event listener could not be started", "error", acpiErr)
}
for {
select {
case <-ticker.C:
callback()
case <-acpiEvent:
slog.Info("acpi event received")
callback()
case sig := <-sigc:
slog.Debug("received signal", "signal", sig)
go func() { quit <- true }()
case <-quit:
ticker.Stop()
slog.Debug("quit")
return
}
}
}