split wrap protocol parsing into input.button
parent
9990273b19
commit
b319ed7e6d
|
|
@ -1,4 +1,4 @@
|
|||
package wrap
|
||||
package button
|
||||
|
||||
type Button struct {
|
||||
Char byte
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue