-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.go
92 lines (81 loc) · 1.38 KB
/
keyboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package insta
import (
"log"
"os"
"os/exec"
"os/signal"
"sync"
)
func NewKeyboardPad() *KeyboardPad {
kp := KeyboardPad{
mu: &sync.Mutex{},
}
go kp.run()
return &kp
}
type KeyboardPad struct {
b byte
mu *sync.Mutex
}
func (m *KeyboardPad) run() {
cmd := exec.Command("/bin/stty", "-f", "/dev/tty", "-icanon", "min", "1", "-echo")
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
cmd := exec.Command("/bin/stty", "-f", "/dev/tty", "sane")
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
os.Exit(1)
}
}()
var keybuf [1]byte
for {
if n, err := os.Stdin.Read(keybuf[0:1]); err == nil && n == 1 {
m.mu.Lock()
m.b = keybuf[0]
m.mu.Unlock()
}
}
}
func (m *KeyboardPad) Pads() []Pad {
m.mu.Lock()
defer m.mu.Unlock()
ps := byteToPad(m.b)
m.b = 0
return ps
}
func byteToPad(b byte) []Pad {
pad0 := PSController{}
pad1 := PSController{}
switch b {
case 'q':
pad0[1] = PsMaskSquare
case 'w':
pad0[0] = PsMaskUp
case 's':
pad0[0] = PsMaskDown
case 'a':
pad0[0] = PsMaskLeft
case 'd':
pad0[0] = PsMaskRight
case 'u':
pad1[0] = PsMaskSquare
case 'i':
pad1[0] = PsMaskUp
case 'k':
pad1[0] = PsMaskDown
case 'j':
pad1[0] = PsMaskLeft
case 'l':
pad1[0] = PsMaskRight
}
ps := make([]Pad, 4)
ps[0] = &pad0
ps[1] = &pad1
return ps
}