diff --git a/src/device/input/wrap/button.go b/src/device/input/button/button.go similarity index 75% rename from src/device/input/wrap/button.go rename to src/device/input/button/button.go index 16ae08c..32338aa 100644 --- a/src/device/input/wrap/button.go +++ b/src/device/input/button/button.go @@ -1,4 +1,4 @@ -package wrap +package button type Button struct { Char byte diff --git a/src/device/input/button/parser.go b/src/device/input/button/parser.go new file mode 100644 index 0000000..ee5397b --- /dev/null +++ b/src/device/input/button/parser.go @@ -0,0 +1,15 @@ +package button + +import ( + "context" + "mayhem-party/src/device/input/raw" +) + +type Parser interface { + Read() []Button + Close() +} + +func New(ctx context.Context, src raw.Raw) Parser { + panic(nil) +} diff --git a/src/device/input/button/plaintext.go b/src/device/input/button/plaintext.go new file mode 100644 index 0000000..dd258d6 --- /dev/null +++ b/src/device/input/button/plaintext.go @@ -0,0 +1,38 @@ +package button + +import ( + "mayhem-party/src/device/input/raw" + "os" +) + +type Plaintext struct { + src raw.Raw +} + +func NewPlaintext(src raw.Raw) Plaintext { + return Plaintext{src: src} +} + +func (p Plaintext) Close() { p.src.Close() } + +func (p Plaintext) Read() []Button { + releaseChar := byte('!') + if v := os.Getenv("BUTTON_PLAINTEXT_RELEASE"); v != "" { + releaseChar = byte(v[0]) + } + + b := p.src.Read() + buttons := make([]Button, 0, len(b)) + down := true + for i := range b { + if b[i] == releaseChar { + down = false + } else { + if b[i] != '\n' { + buttons = append(buttons, Button{Char: b[i], Down: down}) + } + down = true + } + } + return buttons +} diff --git a/src/device/input/wrap/buffered.go b/src/device/input/wrap/buffered.go index 8e5331e..0ef5cec 100644 --- a/src/device/input/wrap/buffered.go +++ b/src/device/input/wrap/buffered.go @@ -2,6 +2,7 @@ package wrap import ( "context" + "mayhem-party/src/device/input/button" "os" "sync" "time" @@ -61,7 +62,7 @@ func (b *Buffered) Close() { b.can() } -func (b *Buffered) Read() []Button { +func (b *Buffered) Read() []button.Button { for b.ctx.Err() == nil { result := b.read() if len(result) > 0 { @@ -72,19 +73,19 @@ func (b *Buffered) Read() []Button { case <-time.After(b.listenInterval): } } - return []Button{} + return []button.Button{} } -func (b *Buffered) read() []Button { +func (b *Buffered) read() []button.Button { b.lock.Lock() defer b.lock.Unlock() - result := make([]Button, 0, len(b.keys)) + result := make([]button.Button, 0, len(b.keys)) for k, v := range b.keys { isFresh := v > 0 isStale := v < 0 && time.Since(time.Unix(0, -1*v)) > b.expirationInterval if isFresh || isStale { - result = append(result, Button{Char: k, Down: isFresh}) + result = append(result, button.Button{Char: k, Down: isFresh}) } if isFresh { b.keys[k] = -1 * v diff --git a/src/device/input/wrap/protocol.go b/src/device/input/wrap/protocol.go index a240e77..1143e74 100644 --- a/src/device/input/wrap/protocol.go +++ b/src/device/input/wrap/protocol.go @@ -1,6 +1,9 @@ package wrap -import "mayhem-party/src/device/input/raw" +import ( + "mayhem-party/src/device/input/button" + "mayhem-party/src/device/input/raw" +) type Protocol struct { src raw.Raw @@ -16,6 +19,6 @@ func (p Protocol) Close() { p.src.Close() } -func (p Protocol) Read() []Button { +func (p Protocol) Read() []button.Button { panic(nil) } diff --git a/src/device/input/wrap/refresh.go b/src/device/input/wrap/refresh.go index d58ad20..a2a5e9a 100644 --- a/src/device/input/wrap/refresh.go +++ b/src/device/input/wrap/refresh.go @@ -3,6 +3,7 @@ package wrap import ( "context" "log" + "mayhem-party/src/device/input/button" "os" "os/signal" ) @@ -39,7 +40,7 @@ func NewRefresh(newWrap func() Wrap, ch <-chan os.Signal) *Refresh { return result } -func (r *Refresh) Read() []Button { +func (r *Refresh) Read() []button.Button { return r.input.Read() } diff --git a/src/device/input/wrap/remap.go b/src/device/input/wrap/remap.go index 4c2c09c..97e1e68 100644 --- a/src/device/input/wrap/remap.go +++ b/src/device/input/wrap/remap.go @@ -1,6 +1,7 @@ package wrap import ( + "mayhem-party/src/device/input/button" "os" "github.com/go-yaml/yaml" @@ -41,7 +42,7 @@ func (re Remap) Close() { re.input.Close() } -func (re Remap) Read() []Button { +func (re Remap) Read() []button.Button { result := re.input.Read() for i := range result { if v, ok := re.m[result[i].Char]; ok { diff --git a/src/device/input/wrap/wrap.go b/src/device/input/wrap/wrap.go index 0d700ad..b2e132f 100644 --- a/src/device/input/wrap/wrap.go +++ b/src/device/input/wrap/wrap.go @@ -2,13 +2,14 @@ package wrap import ( "context" + "mayhem-party/src/device/input/button" "mayhem-party/src/device/input/raw" "os" "syscall" ) type Wrap interface { - Read() []Button + Read() []button.Button Close() } @@ -51,19 +52,4 @@ 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 -} +func (e explicit) Read() []button.Button {