-
Notifications
You must be signed in to change notification settings - Fork 91
/
io.go
243 lines (219 loc) · 3.55 KB
/
io.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"log"
"9fans.net/go/draw"
"9fans.net/go/plan9/client"
)
var (
protodebug bool
cursorfd int
plumbfd *client.Fid
got int
block int
kbdc rune
resized bool
externcmd []rune
plumbc chan string
hostp []byte
hostc chan []byte
mousectl *draw.Mousectl
mousep *draw.Mouse
keyboardctl *draw.Keyboardctl
)
func initio() {
if protodebug {
print("mouse\n")
}
mousectl = display.InitMouse()
if mousectl == nil {
log.Fatalf("mouse init failed")
}
mousep = &mousectl.Mouse
if protodebug {
print("kbd\n")
}
keyboardctl = display.InitKeyboard()
if keyboardctl == nil {
log.Fatalf("keyboard init failed")
}
if protodebug {
print("hoststart\n")
}
hoststart()
if protodebug {
print("plumbstart\n")
}
if err := plumbstart(); err != nil {
if protodebug {
print("extstart\n")
}
extstart()
}
if protodebug {
print("initio done\n")
}
}
func getmouse() {
mousectl.Read()
}
func mouseunblock() {
got &^= 1 << RMouse
}
func kbdblock() { /* ca suffit */
block = 1<<RKeyboard | 1<<RPlumb
}
func button(but int) int {
getmouse()
return mousep.Buttons & (1 << (but - 1))
}
func externload(cmd string) {
// TODO(rsc): drawtopwindow()
externcmd = []rune(cmd)
got |= 1 << RPlumb
}
func waitforio() int {
if got&^block != 0 {
return got &^ block
}
again:
hostc := hostc
if block&(1<<RHost) != 0 {
hostc = nil
}
plumbc := plumbc
if block&(1<<RPlumb) != 0 {
plumbc = nil
}
kbdch := keyboardctl.C
if block&(1<<RKeyboard) != 0 {
kbdch = nil
}
mousec := mousectl.C
if block&(1<<RMouse) != 0 {
mousec = nil
}
resizec := mousectl.Resize
if block&(1<<RResize) != 0 {
resizec = nil
}
display.Flush()
select {
case hostp = <-hostc:
block = 0
got |= 1 << RHost
case cmd := <-plumbc:
externload(cmd)
got |= 1 << RPlumb
case r := <-kbdch:
kbdc = r
got |= 1 << RKeyboard
case mousectl.Mouse = <-mousec:
got |= 1 << RMouse
case <-resizec:
resized = true
/* do the resize in line if we've finished initializing and we're not in a blocking state */
if hasunlocked && block == 0 && RESIZED() {
resize()
}
goto again
}
return got
}
func rcvchar() int {
if got&(1<<RHost) == 0 {
return -1
}
c := hostp[0]
hostp = hostp[1:]
if len(hostp) == 0 {
got &^= 1 << RHost
}
return int(c)
}
func rcvstring() []byte {
got &^= 1 << RHost
return hostp
}
func getch() int {
var c int
for c = rcvchar(); c == -1; c = rcvchar() {
block = ^(1 << RHost)
waitforio()
block = 0
}
return c
}
func externchar() rune {
for got&(1<<RPlumb)&^block != 0 {
if len(externcmd) == 0 {
got &^= 1 << RPlumb
break
}
r := externcmd[0]
externcmd = externcmd[1:]
return r
}
return -1
}
var kpeekc rune = -1
func ecankbd() bool {
if kpeekc >= 0 {
return true
}
select {
default:
return false
case r := <-keyboardctl.C:
kpeekc = r
return true
}
}
func ekbd() rune {
if kpeekc >= 0 {
c := kpeekc
kpeekc = -1
return c
}
return <-keyboardctl.C
}
func kbdchar() rune {
if c := externchar(); c > 0 {
return c
}
if got&(1<<RKeyboard) != 0 {
c := kbdc
kbdc = -1
got &^= 1 << RKeyboard
return c
}
Loop:
for {
select {
default:
break Loop
case cmd := <-plumbc:
externload(cmd)
c := externchar()
if c > 0 {
return c
}
}
}
if !ecankbd() {
return -1
}
return ekbd()
}
func qpeekc() rune {
return kbdc
}
func RESIZED() bool {
if resized {
if err := display.Attach(draw.RefNone); err != nil {
panic("can't reattach to window: " + err.Error())
}
resized = false
return true
}
return false
}