split src/devices/input into src/devices/input/{raw,wrap}
This commit is contained in:
64
src/device/input/wrap/wrap.go
Normal file
64
src/device/input/wrap/wrap.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package wrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"mayhem-party/src/device/input/raw"
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type Wrap interface {
|
||||
Read() []Button
|
||||
Close()
|
||||
}
|
||||
|
||||
func New(ctx context.Context, src raw.Raw) Wrap {
|
||||
maker := func() Wrap {
|
||||
return explicit{src: src}
|
||||
}
|
||||
if os.Getenv("WRAP_BUFFERED") == "true" {
|
||||
oldMaker := maker
|
||||
maker = func() Wrap {
|
||||
return NewBuffered(ctx, oldMaker())
|
||||
}
|
||||
}
|
||||
if p := os.Getenv("WRAP_REMAP_FILE"); p != "" {
|
||||
oldMaker := maker
|
||||
maker = func() Wrap {
|
||||
return NewRemapFromFile(oldMaker(), p)
|
||||
}
|
||||
}
|
||||
if os.Getenv("WRAP_REFRESH_ON_SIGUSR1") != "" {
|
||||
oldMaker := maker
|
||||
c := NewRefreshCh(syscall.SIGUSR1)
|
||||
maker = func() Wrap {
|
||||
return NewRefresh(oldMaker, c)
|
||||
}
|
||||
}
|
||||
return maker()
|
||||
}
|
||||
|
||||
type explicit struct {
|
||||
src raw.Raw
|
||||
}
|
||||
|
||||
func (e explicit) Close() {
|
||||
e.src.Close()
|
||||
}
|
||||
|
||||
func (e explicit) Read() []Button {
|
||||
b := e.src.Read()
|
||||
buttons := make([]Button, 0, len(b))
|
||||
down := true
|
||||
for i := range b {
|
||||
if b[i] == '!' {
|
||||
down = false
|
||||
} else {
|
||||
if b[i] != '\n' {
|
||||
buttons = append(buttons, Button{Char: b[i], Down: down})
|
||||
}
|
||||
down = true
|
||||
}
|
||||
}
|
||||
return buttons
|
||||
}
|
||||
Reference in New Issue
Block a user