-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathkeyboard.go
216 lines (189 loc) · 4.32 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//go:build !windows
// +build !windows
package keyboard
import (
"errors"
"os"
"os/signal"
"runtime"
"strings"
"unicode/utf8"
"golang.org/x/sys/unix"
)
type (
input_event struct {
data []byte
err error
}
)
var (
out *os.File
in int
// term specific keys
keys []string
// termbox inner state
orig_tios unix.Termios
sigio = make(chan os.Signal, 1)
quitEvProd = make(chan bool)
quitConsole = make(chan bool)
inbuf = make([]byte, 0, 128)
input_buf = make(chan input_event)
)
func parse_escape_sequence(buf []byte) (size int, event KeyEvent) {
bufstr := string(buf)
for i, key := range keys {
if strings.HasPrefix(bufstr, key) {
event.Rune = 0
event.Key = Key(0xFFFF - i)
size = len(key)
return
}
}
// Might be an Alt combo in format of ESC+letter
if buf[0] == '\033' {
event.Key = KeyEsc
event.Rune, size = utf8.DecodeRune(buf[1:])
size = len(buf)
return
}
return 0, event
}
func extract_event(inbuf []byte) (int, KeyEvent) {
if len(inbuf) == 0 {
return 0, KeyEvent{}
}
if inbuf[0] == '\033' {
if len(inbuf) == 1 {
return 1, KeyEvent{Key: KeyEsc}
}
// possible escape sequence
if size, event := parse_escape_sequence(inbuf); size != 0 {
return size, event
} else {
// it's not a recognized escape sequence, return error
i := 1 // check for multiple sequences in the buffer
for ; i < len(inbuf) && inbuf[i] != '\033'; i++ {
}
return i, KeyEvent{Key: KeyEsc, Err: errors.New("Unrecognized escape sequence")}
}
}
// if we're here, this is not an escape sequence and not an alt sequence
// so, it's a FUNCTIONAL KEY or a UNICODE character
// first of all check if it's a functional key
if Key(inbuf[0]) <= KeySpace || Key(inbuf[0]) == KeyBackspace2 {
return 1, KeyEvent{Key: Key(inbuf[0])}
}
// the only possible option is utf8 rune
if r, n := utf8.DecodeRune(inbuf); r != utf8.RuneError {
return n, KeyEvent{Rune: r}
}
return 0, KeyEvent{}
}
// Wait for an event and return it. This is a blocking function call.
func inputEventsProducer() {
for {
select {
case <-quitEvProd:
return
case ev := <-input_buf:
if ev.err != nil {
select {
case <-quitEvProd:
return
case inputComm <- KeyEvent{Err: ev.err}:
}
break
}
inbuf = append(inbuf, ev.data...)
for {
size, event := extract_event(inbuf)
if size > 0 {
select {
case <-quitEvProd:
return
case inputComm <- event:
}
copy(inbuf, inbuf[size:])
inbuf = inbuf[:len(inbuf)-size]
}
if size == 0 || len(inbuf) == 0 {
break
}
}
}
}
}
func initConsole() (err error) {
out, err = os.OpenFile("/dev/tty", unix.O_WRONLY, 0)
if err != nil {
return
}
in, err = unix.Open("/dev/tty", unix.O_RDONLY, 0)
if err != nil {
return
}
err = setup_term()
if err != nil {
return errors.New("Error while reading terminfo data:" + err.Error())
}
signal.Notify(sigio, unix.SIGIO)
if _, err = unix.FcntlInt(uintptr(in), unix.F_SETFL, unix.O_ASYNC|unix.O_NONBLOCK); err != nil {
return
}
_, err = unix.FcntlInt(uintptr(in), unix.F_SETOWN, unix.Getpid())
if runtime.GOOS != "darwin" && err != nil {
return
}
if err = unix.IoctlSetTermios(int(out.Fd()), ioctl_GETATTR, &orig_tios); err != nil {
return
}
tios := orig_tios
tios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK |
unix.ISTRIP | unix.INLCR | unix.IGNCR |
unix.ICRNL | unix.IXON
tios.Lflag &^= unix.ECHO | unix.ECHONL | unix.ICANON |
unix.ISIG | unix.IEXTEN
tios.Cflag &^= unix.CSIZE | unix.PARENB
tios.Cflag |= unix.CS8
tios.Cc[unix.VMIN] = 1
tios.Cc[unix.VTIME] = 0
if err = unix.IoctlSetTermios(int(out.Fd()), ioctl_SETATTR, &tios); err != nil {
return
}
go func() {
buf := make([]byte, 128)
for {
select {
case <-quitConsole:
return
case <-sigio:
for {
bytesRead, err := unix.Read(in, buf)
if err == unix.EAGAIN || err == unix.EWOULDBLOCK {
break
}
if err != nil {
bytesRead = 0
}
data := make([]byte, bytesRead)
copy(data, buf)
select {
case <-quitConsole:
return
case input_buf <- input_event{data, err}:
continue
}
}
}
}
}()
go inputEventsProducer()
return
}
func releaseConsole() {
quitConsole <- true
quitEvProd <- true
unix.IoctlSetTermios(int(out.Fd()), ioctl_SETATTR, &orig_tios)
out.Close()
unix.Close(in)
}