split wrap protocol parsing into input.button

master
bel 2023-03-24 20:25:15 -06:00
parent 9990273b19
commit b319ed7e6d
8 changed files with 72 additions and 27 deletions

View File

@ -1,4 +1,4 @@
package wrap package button
type Button struct { type Button struct {
Char byte Char byte

View File

@ -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)
}

View File

@ -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
}

View File

@ -2,6 +2,7 @@ package wrap
import ( import (
"context" "context"
"mayhem-party/src/device/input/button"
"os" "os"
"sync" "sync"
"time" "time"
@ -61,7 +62,7 @@ func (b *Buffered) Close() {
b.can() b.can()
} }
func (b *Buffered) Read() []Button { func (b *Buffered) Read() []button.Button {
for b.ctx.Err() == nil { for b.ctx.Err() == nil {
result := b.read() result := b.read()
if len(result) > 0 { if len(result) > 0 {
@ -72,19 +73,19 @@ func (b *Buffered) Read() []Button {
case <-time.After(b.listenInterval): case <-time.After(b.listenInterval):
} }
} }
return []Button{} return []button.Button{}
} }
func (b *Buffered) read() []Button { func (b *Buffered) read() []button.Button {
b.lock.Lock() b.lock.Lock()
defer b.lock.Unlock() 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 { for k, v := range b.keys {
isFresh := v > 0 isFresh := v > 0
isStale := v < 0 && time.Since(time.Unix(0, -1*v)) > b.expirationInterval isStale := v < 0 && time.Since(time.Unix(0, -1*v)) > b.expirationInterval
if isFresh || isStale { if isFresh || isStale {
result = append(result, Button{Char: k, Down: isFresh}) result = append(result, button.Button{Char: k, Down: isFresh})
} }
if isFresh { if isFresh {
b.keys[k] = -1 * v b.keys[k] = -1 * v

View File

@ -1,6 +1,9 @@
package wrap 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 { type Protocol struct {
src raw.Raw src raw.Raw
@ -16,6 +19,6 @@ func (p Protocol) Close() {
p.src.Close() p.src.Close()
} }
func (p Protocol) Read() []Button { func (p Protocol) Read() []button.Button {
panic(nil) panic(nil)
} }

View File

@ -3,6 +3,7 @@ package wrap
import ( import (
"context" "context"
"log" "log"
"mayhem-party/src/device/input/button"
"os" "os"
"os/signal" "os/signal"
) )
@ -39,7 +40,7 @@ func NewRefresh(newWrap func() Wrap, ch <-chan os.Signal) *Refresh {
return result return result
} }
func (r *Refresh) Read() []Button { func (r *Refresh) Read() []button.Button {
return r.input.Read() return r.input.Read()
} }

View File

@ -1,6 +1,7 @@
package wrap package wrap
import ( import (
"mayhem-party/src/device/input/button"
"os" "os"
"github.com/go-yaml/yaml" "github.com/go-yaml/yaml"
@ -41,7 +42,7 @@ func (re Remap) Close() {
re.input.Close() re.input.Close()
} }
func (re Remap) Read() []Button { func (re Remap) Read() []button.Button {
result := re.input.Read() result := re.input.Read()
for i := range result { for i := range result {
if v, ok := re.m[result[i].Char]; ok { if v, ok := re.m[result[i].Char]; ok {

View File

@ -2,13 +2,14 @@ package wrap
import ( import (
"context" "context"
"mayhem-party/src/device/input/button"
"mayhem-party/src/device/input/raw" "mayhem-party/src/device/input/raw"
"os" "os"
"syscall" "syscall"
) )
type Wrap interface { type Wrap interface {
Read() []Button Read() []button.Button
Close() Close()
} }
@ -51,19 +52,4 @@ func (e explicit) Close() {
e.src.Close() e.src.Close()
} }
func (e explicit) Read() []Button { func (e explicit) Read() []button.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
}