split src/devices/input into src/devices/input/{raw,wrap}

This commit is contained in:
bel
2023-03-24 19:51:38 -06:00
parent ab673a81f0
commit 38b00e55b0
16 changed files with 161 additions and 159 deletions

View File

@@ -0,0 +1,49 @@
package wrap
import (
"context"
"log"
"os"
"os/signal"
)
type Refresh struct {
can context.CancelFunc
input Wrap
}
func NewRefreshCh(sig os.Signal) <-chan os.Signal {
c := make(chan os.Signal, 1)
signal.Notify(c, sig)
return c
}
func NewRefresh(newWrap func() Wrap, ch <-chan os.Signal) *Refresh {
ctx, can := context.WithCancel(context.Background())
result := &Refresh{
can: can,
input: newWrap(),
}
go func() {
defer log.Println("refreshing done")
for {
select {
case <-ctx.Done():
return
case sig := <-ch:
log.Println("refreshing for", sig)
result.input = newWrap()
}
}
}()
return result
}
func (r *Refresh) Read() []Button {
return r.input.Read()
}
func (r *Refresh) Close() {
r.can()
r.input.Close()
}