| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package tty |
| 19 |
|
| 20 |
import ( |
| 21 |
"encoding/binary" |
| 22 |
"errors" |
| 23 |
"fmt" |
| 24 |
"sync" |
| 25 |
"syscall" |
| 26 |
"time" |
| 27 |
"unicode/utf16" |
| 28 |
"unsafe" |
| 29 |
) |
| 30 |
|
| 31 |
var ( |
| 32 |
k32 = syscall.NewLazyDLL("kernel32.dll") |
| 33 |
) |
| 34 |
|
| 35 |
var ( |
| 36 |
procReadConsoleInput = k32.NewProc("ReadConsoleInputW") |
| 37 |
procGetNumberOfConsoleInputEvents = k32.NewProc("GetNumberOfConsoleInputEvents") |
| 38 |
procFlushConsoleInputBuffer = k32.NewProc("FlushConsoleInputBuffer") |
| 39 |
procWaitForMultipleObjects = k32.NewProc("WaitForMultipleObjects") |
| 40 |
procSetConsoleMode = k32.NewProc("SetConsoleMode") |
| 41 |
procGetConsoleMode = k32.NewProc("GetConsoleMode") |
| 42 |
procGetConsoleScreenBufferInfo = k32.NewProc("GetConsoleScreenBufferInfo") |
| 43 |
procCreateEvent = k32.NewProc("CreateEventW") |
| 44 |
procSetEvent = k32.NewProc("SetEvent") |
| 45 |
) |
| 46 |
|
| 47 |
const ( |
| 48 |
keyEvent uint16 = 1 |
| 49 |
mouseEvent uint16 = 2 |
| 50 |
resizeEvent uint16 = 4 |
| 51 |
menuEvent uint16 = 8 |
| 52 |
focusEvent uint16 = 16 |
| 53 |
) |
| 54 |
|
| 55 |
const ( |
| 56 |
w32Infinite = ^uintptr(0) |
| 57 |
w32WaitObject0 = uintptr(0) |
| 58 |
) |
| 59 |
|
| 60 |
const ( |
| 61 |
|
| 62 |
modeExtendFlg = uint32(0x0080) |
| 63 |
modeMouseEn = uint32(0x0010) |
| 64 |
modeResizeEn = uint32(0x0008) |
| 65 |
modeVtInput = uint32(0x0200) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
modeCookedOut = uint32(0x0001) |
| 70 |
modeVtOutput = uint32(0x0004) |
| 71 |
modeNoAutoNL = uint32(0x0008) |
| 72 |
modeUnderline = uint32(0x0010) |
| 73 |
|
| 74 |
) |
| 75 |
|
| 76 |
type coord struct { |
| 77 |
x int16 |
| 78 |
y int16 |
| 79 |
} |
| 80 |
|
| 81 |
type rect struct { |
| 82 |
left int16 |
| 83 |
top int16 |
| 84 |
right int16 |
| 85 |
bottom int16 |
| 86 |
} |
| 87 |
|
| 88 |
type consoleInfo struct { |
| 89 |
size coord |
| 90 |
pos coord |
| 91 |
attrs uint16 |
| 92 |
win rect |
| 93 |
maxsz coord |
| 94 |
} |
| 95 |
|
| 96 |
type inputRecord struct { |
| 97 |
typ uint16 |
| 98 |
_ uint16 |
| 99 |
data [16]byte |
| 100 |
} |
| 101 |
|
| 102 |
func encodeWinKeyRecord(data [16]byte, surrogate *rune) []byte { |
| 103 |
keyDown := binary.LittleEndian.Uint32(data[0:]) != 0 |
| 104 |
repeat := binary.LittleEndian.Uint16(data[4:]) |
| 105 |
virtualKey := binary.LittleEndian.Uint16(data[6:]) |
| 106 |
scanCode := binary.LittleEndian.Uint16(data[8:]) |
| 107 |
|
| 108 |
wc := rune(binary.LittleEndian.Uint16(data[10:])) |
| 109 |
controlState := binary.LittleEndian.Uint32(data[12:]) |
| 110 |
|
| 111 |
if virtualKey != 0 || scanCode != 0 { |
| 112 |
kd := 0 |
| 113 |
if keyDown { |
| 114 |
kd = 1 |
| 115 |
} |
| 116 |
return fmt.Appendf(nil, "\x1b[%d;%d;%d;%d;%d;%d_", |
| 117 |
virtualKey, scanCode, wc, kd, controlState, max(1, repeat)) |
| 118 |
} |
| 119 |
|
| 120 |
if !keyDown { |
| 121 |
return nil |
| 122 |
} |
| 123 |
|
| 124 |
var encoded []byte |
| 125 |
decodedRunes := decodeUTF16Rune(surrogate, wc) |
| 126 |
for range max(1, repeat) { |
| 127 |
for _, decoded := range decodedRunes { |
| 128 |
encoded = append(encoded, []byte(string(decoded))...) |
| 129 |
} |
| 130 |
} |
| 131 |
return encoded |
| 132 |
} |
| 133 |
|
| 134 |
type winTty struct { |
| 135 |
buf chan byte |
| 136 |
out syscall.Handle |
| 137 |
in syscall.Handle |
| 138 |
cancelFlag syscall.Handle |
| 139 |
running bool |
| 140 |
stopQ chan struct{} |
| 141 |
resizeQ chan<- bool |
| 142 |
cols uint16 |
| 143 |
rows uint16 |
| 144 |
pair []uint16 |
| 145 |
oimode uint32 |
| 146 |
oomode uint32 |
| 147 |
oscreen consoleInfo |
| 148 |
wg sync.WaitGroup |
| 149 |
surrogate rune |
| 150 |
sync.Mutex |
| 151 |
} |
| 152 |
|
| 153 |
func (w *winTty) Read(b []byte) (int, error) { |
| 154 |
|
| 155 |
var num int |
| 156 |
select { |
| 157 |
case c := <-w.buf: |
| 158 |
b[0] = c |
| 159 |
num++ |
| 160 |
case <-w.stopQ: |
| 161 |
|
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
for ; num < len(b); num++ { |
| 167 |
select { |
| 168 |
case c := <-w.buf: |
| 169 |
b[num] = c |
| 170 |
case <-time.After(time.Millisecond * 10): |
| 171 |
return num, nil |
| 172 |
} |
| 173 |
} |
| 174 |
return num, nil |
| 175 |
} |
| 176 |
|
| 177 |
func (w *winTty) Write(b []byte) (int, error) { |
| 178 |
esc := utf16.Encode([]rune(string(b))) |
| 179 |
if len(esc) > 0 { |
| 180 |
err := syscall.WriteConsole(w.out, &esc[0], uint32(len(esc)), nil, nil) |
| 181 |
if err != nil { |
| 182 |
return 0, err |
| 183 |
} |
| 184 |
} |
| 185 |
return len(b), nil |
| 186 |
} |
| 187 |
|
| 188 |
func (w *winTty) Close() error { |
| 189 |
_ = syscall.Close(w.in) |
| 190 |
_ = syscall.Close(w.out) |
| 191 |
return nil |
| 192 |
} |
| 193 |
|
| 194 |
func (w *winTty) Drain() error { |
| 195 |
close(w.stopQ) |
| 196 |
time.Sleep(time.Millisecond * 10) |
| 197 |
_, _, _ = procSetEvent.Call(uintptr(w.cancelFlag)) |
| 198 |
return nil |
| 199 |
} |
| 200 |
|
| 201 |
func (w *winTty) getConsoleInput() error { |
| 202 |
|
| 203 |
|
| 204 |
waitObjects := []syscall.Handle{w.cancelFlag, w.in} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
pWaitObjects := unsafe.Pointer(&waitObjects[0]) |
| 209 |
|
| 210 |
rv, _, er := procWaitForMultipleObjects.Call( |
| 211 |
uintptr(len(waitObjects)), |
| 212 |
uintptr(pWaitObjects), |
| 213 |
uintptr(0), |
| 214 |
w32Infinite) |
| 215 |
|
| 216 |
|
| 217 |
switch rv { |
| 218 |
case w32WaitObject0: |
| 219 |
return errors.New("cancelled") |
| 220 |
case w32WaitObject0 + 1: |
| 221 |
var nrec int32 |
| 222 |
rv, _, er := procGetNumberOfConsoleInputEvents.Call( |
| 223 |
uintptr(w.in), |
| 224 |
uintptr(unsafe.Pointer(&nrec))) |
| 225 |
if rv == 0 { |
| 226 |
return er |
| 227 |
} |
| 228 |
|
| 229 |
rec := make([]inputRecord, max(nrec, 1)) |
| 230 |
rv, _, er = procReadConsoleInput.Call( |
| 231 |
uintptr(w.in), |
| 232 |
uintptr(unsafe.Pointer(&rec[0])), |
| 233 |
uintptr(nrec), |
| 234 |
uintptr(unsafe.Pointer(&nrec))) |
| 235 |
if rv == 0 { |
| 236 |
return er |
| 237 |
} |
| 238 |
loop: |
| 239 |
for i := range nrec { |
| 240 |
ir := rec[i] |
| 241 |
switch ir.typ { |
| 242 |
case keyEvent: |
| 243 |
for _, chr := range encodeWinKeyRecord(ir.data, &w.surrogate) { |
| 244 |
select { |
| 245 |
case w.buf <- chr: |
| 246 |
case <-w.stopQ: |
| 247 |
break loop |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
case resizeEvent: |
| 252 |
w.Lock() |
| 253 |
w.cols = binary.LittleEndian.Uint16(ir.data[0:]) |
| 254 |
w.rows = binary.LittleEndian.Uint16(ir.data[2:]) |
| 255 |
if w.resizeQ != nil { |
| 256 |
select { |
| 257 |
case w.resizeQ <- true: |
| 258 |
default: |
| 259 |
} |
| 260 |
} |
| 261 |
w.Unlock() |
| 262 |
|
| 263 |
default: |
| 264 |
} |
| 265 |
} |
| 266 |
return nil |
| 267 |
default: |
| 268 |
return er |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
func (w *winTty) scanInput() { |
| 273 |
defer w.wg.Done() |
| 274 |
for { |
| 275 |
if e := w.getConsoleInput(); e != nil { |
| 276 |
return |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
func (w *winTty) Start() error { |
| 282 |
|
| 283 |
if w.running { |
| 284 |
return nil |
| 285 |
} |
| 286 |
_, _, _ = procFlushConsoleInputBuffer.Call(uintptr(w.in)) |
| 287 |
w.stopQ = make(chan struct{}) |
| 288 |
cf, _, err := procCreateEvent.Call( |
| 289 |
uintptr(0), |
| 290 |
uintptr(1), |
| 291 |
uintptr(0), |
| 292 |
uintptr(0)) |
| 293 |
if cf == uintptr(0) { |
| 294 |
return err |
| 295 |
} |
| 296 |
w.running = true |
| 297 |
w.cancelFlag = syscall.Handle(cf) |
| 298 |
|
| 299 |
_, _, _ = procSetConsoleMode.Call(uintptr(w.in), |
| 300 |
uintptr(modeVtInput|modeResizeEn|modeExtendFlg)) |
| 301 |
_, _, _ = procSetConsoleMode.Call(uintptr(w.out), |
| 302 |
uintptr(modeVtOutput|modeNoAutoNL|modeCookedOut|modeUnderline)) |
| 303 |
|
| 304 |
w.wg.Add(1) |
| 305 |
go w.scanInput() |
| 306 |
return nil |
| 307 |
} |
| 308 |
|
| 309 |
func (w *winTty) Stop() error { |
| 310 |
w.wg.Wait() |
| 311 |
_, _, _ = procSetConsoleMode.Call(uintptr(w.in), uintptr(w.oimode)) |
| 312 |
_, _, _ = procSetConsoleMode.Call(uintptr(w.out), uintptr(w.oomode)) |
| 313 |
_, _, _ = procFlushConsoleInputBuffer.Call(uintptr(w.in)) |
| 314 |
w.running = false |
| 315 |
|
| 316 |
return nil |
| 317 |
} |
| 318 |
|
| 319 |
func (tty *winTty) NotifyResize(resizeQ chan<- bool) { |
| 320 |
tty.Lock() |
| 321 |
tty.resizeQ = resizeQ |
| 322 |
tty.Unlock() |
| 323 |
} |
| 324 |
|
| 325 |
func (w *winTty) WindowSize() (WindowSize, error) { |
| 326 |
w.Lock() |
| 327 |
defer w.Unlock() |
| 328 |
return WindowSize{Width: int(w.cols), Height: int(w.rows)}, nil |
| 329 |
} |
| 330 |
|
| 331 |
func NewDevTty() (Tty, error) { |
| 332 |
w := &winTty{} |
| 333 |
var err error |
| 334 |
w.in, err = syscall.Open("CONIN$", syscall.O_RDWR, 0) |
| 335 |
if err != nil { |
| 336 |
return nil, err |
| 337 |
} |
| 338 |
w.out, err = syscall.Open("CONOUT$", syscall.O_RDWR, 0) |
| 339 |
if err != nil { |
| 340 |
_ = syscall.Close(w.in) |
| 341 |
return nil, err |
| 342 |
} |
| 343 |
w.buf = make(chan byte, 128) |
| 344 |
|
| 345 |
_, _, _ = procGetConsoleScreenBufferInfo.Call(uintptr(w.out), uintptr(unsafe.Pointer(&w.oscreen))) |
| 346 |
_, _, _ = procGetConsoleMode.Call(uintptr(w.out), uintptr(unsafe.Pointer(&w.oomode))) |
| 347 |
_, _, _ = procGetConsoleMode.Call(uintptr(w.in), uintptr(unsafe.Pointer(&w.oimode))) |
| 348 |
w.rows = uint16(w.oscreen.size.y) |
| 349 |
w.cols = uint16(w.oscreen.size.x) |
| 350 |
|
| 351 |
return w, nil |
| 352 |
} |
| 353 |
|
| 354 |
func NewDevTtyFromDev(dev string) (Tty, error) { |
| 355 |
return nil, errors.New("No tty device on Windows") |
| 356 |
} |
| 357 |
|
| 358 |
func NewStdIoTty() (Tty, error) { |
| 359 |
w := &winTty{} |
| 360 |
w.in = syscall.Stdin |
| 361 |
w.out = syscall.Stdout |
| 362 |
w.buf = make(chan byte, 128) |
| 363 |
|
| 364 |
_, _, _ = procGetConsoleScreenBufferInfo.Call(uintptr(w.out), uintptr(unsafe.Pointer(&w.oscreen))) |
| 365 |
if r, _, err := procGetConsoleMode.Call(uintptr(w.out), uintptr(unsafe.Pointer(&w.oomode))); r == 0 || err != nil { |
| 366 |
return nil, errors.New("output is not a terminal") |
| 367 |
} |
| 368 |
if r, _, err := procGetConsoleMode.Call(uintptr(w.in), uintptr(unsafe.Pointer(&w.oimode))); r == 0 || err != nil { |
| 369 |
return nil, errors.New("input is not a terminal") |
| 370 |
} |
| 371 |
w.rows = uint16(w.oscreen.size.y) |
| 372 |
w.cols = uint16(w.oscreen.size.x) |
| 373 |
|
| 374 |
return w, nil |
| 375 |
} |