forked from charmbracelet/bubbletea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cancelreader_windows.go
245 lines (202 loc) Β· 6.67 KB
/
cancelreader_windows.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
244
245
//go:build windows
// +build windows
package tea
import (
"fmt"
"io"
"os"
"syscall"
"time"
"unicode/utf16"
"golang.org/x/sys/windows"
)
var fileShareValidFlags uint32 = 0x00000007
// newCancelReader returns a reader and a cancel function. If the input reader
// is an *os.File with the same file descriptor as os.Stdin, the cancel function
// can be used to interrupt a blocking call read call. In this case, the cancel
// function returns true if the call was cancelled successfully. If the input
// reader is not a *os.File with the same file descriptor as os.Stdin, the
// cancel function does nothing and always returns false. The Windows
// implementation is based on WaitForMultipleObject with overlapping reads from
// CONIN$.
func newCancelReader(reader io.Reader) (cancelReader, error) {
if f, ok := reader.(*os.File); !ok || f.Fd() != os.Stdin.Fd() {
return newFallbackCancelReader(reader)
}
// it is neccessary to open CONIN$ (NOT windows.STD_INPUT_HANDLE) in
// overlapped mode to be able to use it with WaitForMultipleObjects.
conin, err := windows.CreateFile(
&(utf16.Encode([]rune("CONIN$\x00"))[0]), windows.GENERIC_READ|windows.GENERIC_WRITE,
fileShareValidFlags, nil, windows.OPEN_EXISTING, windows.FILE_FLAG_OVERLAPPED, 0)
if err != nil {
return nil, fmt.Errorf("open CONIN$ in overlapping mode: %w", err)
}
resetConsole, err := prepareConsole(conin)
if err != nil {
return nil, fmt.Errorf("prepare console: %w", err)
}
// flush input, otherwise it can contain events which trigger
// WaitForMultipleObjects but which ReadFile cannot read, resulting in an
// un-cancelable read
err = flushConsoleInputBuffer(conin)
if err != nil {
return nil, fmt.Errorf("flush console input buffer: %w", err)
}
cancelEvent, err := windows.CreateEvent(nil, 0, 0, nil)
if err != nil {
return nil, fmt.Errorf("create stop event: %w", err)
}
return &winCancelReader{
conin: conin,
cancelEvent: cancelEvent,
resetConsole: resetConsole,
blockingReadSignal: make(chan struct{}, 1),
}, nil
}
type winCancelReader struct {
conin windows.Handle
cancelEvent windows.Handle
cancelMixin
resetConsole func() error
blockingReadSignal chan struct{}
}
func (r *winCancelReader) Read(data []byte) (int, error) {
if r.isCancelled() {
return 0, errCanceled
}
err := r.wait()
if err != nil {
return 0, err
}
if r.isCancelled() {
return 0, errCanceled
}
// windows.Read does not work on overlapping windows.Handles
return r.readAsync(data)
}
// Cancel cancels ongoing and future Read() calls and returns true if the
// cancelation of the ongoing Read() was successful. On Windows Terminal,
// WaitForMultipleObjects sometimes immediately returns without input being
// available. In this case, graceful cancelation is not possible and Cancel()
// returns false.
func (r *winCancelReader) Cancel() bool {
r.setCancelled()
select {
case r.blockingReadSignal <- struct{}{}:
err := windows.SetEvent(r.cancelEvent)
if err != nil {
return false
}
<-r.blockingReadSignal
case <-time.After(100 * time.Millisecond):
// Read() hangs in a GetOverlappedResult which is likely due to
// WaitForMultipleObjects returning without input being available
// so we cannot cancel this ongoing read.
return false
}
return true
}
func (r *winCancelReader) Close() error {
err := windows.CloseHandle(r.cancelEvent)
if err != nil {
return fmt.Errorf("closing cancel event handle: %w", err)
}
err = r.resetConsole()
if err != nil {
return err
}
err = windows.Close(r.conin)
if err != nil {
return fmt.Errorf("closing CONIN$")
}
return nil
}
func (r *winCancelReader) wait() error {
event, err := windows.WaitForMultipleObjects([]windows.Handle{r.conin, r.cancelEvent}, false, windows.INFINITE)
switch {
case windows.WAIT_OBJECT_0 <= event && event < windows.WAIT_OBJECT_0+2:
if event == windows.WAIT_OBJECT_0+1 {
return errCanceled
}
if event == windows.WAIT_OBJECT_0 {
return nil
}
return fmt.Errorf("unexpected wait object is ready: %d", event-windows.WAIT_OBJECT_0)
case windows.WAIT_ABANDONED <= event && event < windows.WAIT_ABANDONED+2:
return fmt.Errorf("abandoned")
case event == uint32(windows.WAIT_TIMEOUT):
return fmt.Errorf("timeout")
case event == windows.WAIT_FAILED:
return fmt.Errorf("failed")
default:
return fmt.Errorf("unexpected error: %w", error(err))
}
}
// readAsync is neccessary to read from a windows.Handle in overlapping mode.
func (r *winCancelReader) readAsync(data []byte) (int, error) {
hevent, err := windows.CreateEvent(nil, 0, 0, nil)
if err != nil {
return 0, fmt.Errorf("create event: %w", err)
}
overlapped := windows.Overlapped{
HEvent: hevent,
}
var n uint32
err = windows.ReadFile(r.conin, data, &n, &overlapped)
if err != nil && err != windows.ERROR_IO_PENDING {
return int(n), err
}
r.blockingReadSignal <- struct{}{}
err = windows.GetOverlappedResult(r.conin, &overlapped, &n, true)
if err != nil {
return int(n), nil
}
<-r.blockingReadSignal
return int(n), nil
}
func prepareConsole(input windows.Handle) (reset func() error, err error) {
var originalMode uint32
err = windows.GetConsoleMode(input, &originalMode)
if err != nil {
return nil, fmt.Errorf("get console mode: %w", err)
}
var newMode uint32
newMode &^= windows.ENABLE_ECHO_INPUT
newMode &^= windows.ENABLE_LINE_INPUT
newMode &^= windows.ENABLE_MOUSE_INPUT
newMode &^= windows.ENABLE_WINDOW_INPUT
newMode &^= windows.ENABLE_PROCESSED_INPUT
newMode |= windows.ENABLE_EXTENDED_FLAGS
newMode |= windows.ENABLE_INSERT_MODE
newMode |= windows.ENABLE_QUICK_EDIT_MODE
// Enabling virutal terminal input is necessary for processing certain
// types of input like X10 mouse events and arrows keys with the current
// bytes-based input reader. It does, however, prevent cancelReader from
// being able to cancel input. The planned solution for this is to read
// Windows events in a more native fashion, rather than the current simple
// bytes-based input reader which works well on unix systems.
newMode |= windows.ENABLE_VIRTUAL_TERMINAL_INPUT
err = windows.SetConsoleMode(input, newMode)
if err != nil {
return nil, fmt.Errorf("set console mode: %w", err)
}
return func() error {
err := windows.SetConsoleMode(input, originalMode)
if err != nil {
return fmt.Errorf("reset console mode: %w", err)
}
return nil
}, nil
}
var (
modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
procFlushConsoleInputBuffer = modkernel32.NewProc("FlushConsoleInputBuffer")
)
func flushConsoleInputBuffer(consoleInput windows.Handle) error {
r, _, e := syscall.Syscall(procFlushConsoleInputBuffer.Addr(), 1,
uintptr(consoleInput), 0, 0)
if r == 0 {
return error(e)
}
return nil
}