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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//! Pure Windows console ABI: constants and arithmetic, no `unsafe`, no calls.
//!
//! This module is compiled on **every** target — it is declared unconditionally
//! by `sys/mod.rs` next to [`crate::sys::abi`] — for the same reason the Darwin
//! tables are: there is no Windows machine in this project's loop, so anything
//! that can be written down as arithmetic is written down here, where the Linux
//! host compiles it, `const _: () = assert!(…)` pins it, and `cargo test` runs
//! it. `sys/windows.rs` and its FFI submodules are then only "call the console
//! API, hand the result to a function in this module".
//!
//! Authorities: Windows SDK `consoleapi.h` / `wincon.h` / `wincontypes.h`
//! (console mode flags, event-record types, `CTRL_*_EVENT`), `winbase.h`
//! (`STD_*_HANDLE`, `WAIT_*`), `winerror.h` (`ERROR_*`), `winnt.h`
//! (`GENERIC_*`), and the documented behaviour of `SetConsoleMode`,
//! `GetConsoleScreenBufferInfo` and `ENABLE_VIRTUAL_TERMINAL_INPUT`.
// Every item here exists for the Windows backend; on Linux/Darwin builds the
// module is compiled and tested but not called.
use crateWriteStep;
// ---------------------------------------------------------------------------
// Handles, files, code pages
// ---------------------------------------------------------------------------
/// `GetStdHandle` selectors (`winbase.h`): `(DWORD)-10 / -11 / -12`.
pub const STD_INPUT_HANDLE: u32 = -10i32 as u32;
pub const STD_OUTPUT_HANDLE: u32 = -11i32 as u32;
pub const STD_ERROR_HANDLE: u32 = -12i32 as u32;
/// `INVALID_HANDLE_VALUE` is `(HANDLE)-1`, i.e. all bits set.
pub const INVALID_HANDLE_VALUE: usize = usizeMAX;
/// `CreateFileW` arguments for the `CONIN$` / `CONOUT$` open (`winnt.h`).
pub const GENERIC_READ: u32 = 0x8000_0000;
pub const GENERIC_WRITE: u32 = 0x4000_0000;
pub const FILE_SHARE_READ: u32 = 0x0000_0001;
pub const FILE_SHARE_WRITE: u32 = 0x0000_0002;
pub const OPEN_EXISTING: u32 = 3;
/// UTF-8 code page. Set on both console code pages so that the byte stream
/// `ReadFile`/`WriteFile` carry is the UTF-8 `key.rs` and `term.rs` assume.
pub const CP_UTF8: u32 = 65001;
/// `CONIN$` and `CONOUT$` as NUL-terminated UTF-16, ready for `CreateFileW`.
/// Written out rather than converted at runtime so no allocation happens on the
/// path that runs before the terminal is usable.
pub const CONIN: = ;
pub const CONOUT: = ;
// ---------------------------------------------------------------------------
// Console mode bits
// ---------------------------------------------------------------------------
// Input handle (`SetConsoleMode` on STD_INPUT_HANDLE).
pub const ENABLE_PROCESSED_INPUT: u32 = 0x0001;
pub const ENABLE_LINE_INPUT: u32 = 0x0002;
pub const ENABLE_ECHO_INPUT: u32 = 0x0004;
pub const ENABLE_WINDOW_INPUT: u32 = 0x0008;
pub const ENABLE_MOUSE_INPUT: u32 = 0x0010;
pub const ENABLE_INSERT_MODE: u32 = 0x0020;
pub const ENABLE_QUICK_EDIT_MODE: u32 = 0x0040;
pub const ENABLE_EXTENDED_FLAGS: u32 = 0x0080;
pub const ENABLE_AUTO_POSITION: u32 = 0x0100;
pub const ENABLE_VIRTUAL_TERMINAL_INPUT: u32 = 0x0200;
// Output handle (`SetConsoleMode` on STD_OUTPUT_HANDLE).
pub const ENABLE_PROCESSED_OUTPUT: u32 = 0x0001;
pub const ENABLE_WRAP_AT_EOL_OUTPUT: u32 = 0x0002;
pub const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004;
pub const DISABLE_NEWLINE_AUTO_RETURN: u32 = 0x0008;
pub const ENABLE_LVB_GRID_WORLDWIDE: u32 = 0x0010;
/// The raw-mode transformation for the **input** handle: the Windows analogue
/// of [`crate::sys::abi::apply_raw_mode`].
///
/// Cleared: `ENABLE_LINE_INPUT` (keys must arrive unbuffered), `ENABLE_ECHO_INPUT`
/// (the pager paints its own screen), `ENABLE_PROCESSED_INPUT` (Ctrl-C arrives
/// as `\x03`, matching the `ISIG` clear on unix).
///
/// Set: `ENABLE_VIRTUAL_TERMINAL_INPUT`, which makes the console deliver arrows,
/// Home/End, function keys and bracketed paste as the very ANSI sequences
/// `key.rs` already decodes — the reason `key.rs` needs no Windows branch.
///
/// **Never** set: `ENABLE_MOUSE_INPUT`. **Never** cleared:
/// `ENABLE_QUICK_EDIT_MODE`, which is how console users drag-select
/// (SPEC.md §"Hard constraints" #5). Quick edit is only honoured while
/// `ENABLE_EXTENDED_FLAGS` is set, and the classic bug is to write back a mode
/// with the extended bit set but the quick-edit bit dropped — so when the
/// incoming mode had quick edit on, this re-asserts *both* bits explicitly
/// rather than trusting the round trip.
pub const
/// The raw-mode transformation for the **output** handle.
///
/// Sets `ENABLE_VIRTUAL_TERMINAL_PROCESSING` (the frame buffer emits SGR/CUP and
/// the console must interpret them), keeps `ENABLE_PROCESSED_OUTPUT`, sets
/// `DISABLE_NEWLINE_AUTO_RETURN` and clears `ENABLE_WRAP_AT_EOL_OUTPUT` so that
/// painting the last cell of a row does not scroll the screen — together the
/// equivalent of clearing `OPOST` on unix (docs/windows.md §1).
pub const
// The two product invariants, pinned at compile time on every target so that a
// future edit to the arithmetic above cannot quietly take the mouse away.
const _: = assert!;
const _: = assert!;
const _: = assert!;
const _: = assert!;
// ---------------------------------------------------------------------------
// Window size
// ---------------------------------------------------------------------------
/// Visible-window dimensions as `(cols, rows)` from a `SMALL_RECT`.
///
/// This is `srWindow`, **not** `dwSize`: `dwSize` is the screen *buffer*
/// (scrollback), routinely 9000 rows tall, and laying frames out to it would
/// paint most of the pager where the user cannot see it (docs/windows.md §3).
///
/// The rectangle is inclusive on both ends, hence the `+ 1`. A degenerate or
/// inverted rectangle yields `None`, which `term.rs` turns into 80x24 — the
/// same treatment a zero dimension gets on unix.
/// Interpret a `GetConsoleScreenBufferInfo` result plus the rectangle it filled.
/// Pack `(cols, rows)` into one word so the last observed size can live in a
/// single lock-free atomic. `0` is reserved for "nothing observed yet", which is
/// unambiguous because [`window_dims`] never returns a zero dimension.
pub const
/// Resize detection, as a pure function: Windows has no `SIGWINCH`, so the
/// backend polls `srWindow` and compares. A transition to or from "unknown"
/// (a failed query, e.g. while the console is being torn down) is not a resize;
/// only two known, different sizes are.
pub const
// ---------------------------------------------------------------------------
// Waiting and reading
// ---------------------------------------------------------------------------
pub const WAIT_OBJECT_0: u32 = 0x0000_0000;
pub const WAIT_ABANDONED: u32 = 0x0000_0080;
pub const WAIT_TIMEOUT: u32 = 0x0000_0102;
pub const WAIT_FAILED: u32 = 0xFFFF_FFFF;
/// The event-loop tick, in milliseconds. Mirrors `VTIME = 1` (tenths of a
/// second) on unix: `read_input` must come back within roughly this long even
/// when nothing is typed, because that return is when the loop polls
/// `winch_pending()`.
pub const READ_POLL_MS: u32 = 100;
// `winerror.h`.
pub const ERROR_HANDLE_EOF: u32 = 38;
pub const ERROR_BROKEN_PIPE: u32 = 109;
pub const ERROR_NO_DATA: u32 = 232;
pub const ERROR_OPERATION_ABORTED: u32 = 995;
pub const ERROR_INVALID_HANDLE: u32 = 6;
/// What the backend should do with the return value of one `ReadFile`.
///
/// Deliberately *not* [`crate::sys::abi::ReadStep`]: that enum's `Timeout` is
/// the unix "a zero-length read is a `VTIME` expiry, not EOF" rule, and Windows
/// splits the two cases apart — see [`classify_read`].
/// Classify one `ReadFile` on the input handle.
///
/// `console` is what makes a successful zero-byte read mean two different
/// things. A **console** never reaches end of file: `ReadConsole` (which is what
/// `ReadFile` becomes on a console handle) can legitimately return zero
/// characters when the records it consumed — a focus change, a menu event, a
/// key-up that raced in after the peek — translate to no bytes at all. Calling
/// that EOF would quit the pager out from under the user for clicking on another
/// window, so it is [`WinRead::Idle`]: go back and wait.
///
/// On a **pipe or file** (`console == false`) there is no such record stream, so
/// a successful zero-byte read is the real thing, and this is what finally makes
/// [`crate::sys::ReadOutcome::Eof`] reachable — as the contract always allowed
/// but the unix backend cannot do.
/// Classify one `WriteFile`. A successful zero-byte write on a non-empty buffer
/// cannot make progress, so it fails rather than spinning — same rule as unix.
// ---------------------------------------------------------------------------
// Input records
// ---------------------------------------------------------------------------
pub const KEY_EVENT: u16 = 0x0001;
pub const MOUSE_EVENT: u16 = 0x0002;
pub const WINDOW_BUFFER_SIZE_EVENT: u16 = 0x0004;
pub const MENU_EVENT: u16 = 0x0008;
pub const FOCUS_EVENT: u16 = 0x0010;
/// Will `ReadFile` produce bytes for this key record under
/// `ENABLE_VIRTUAL_TERMINAL_INPUT`?
///
/// The backend peeks before it reads, because `WaitForSingleObject` signals for
/// *any* input record while `ReadFile` only returns once one of them translates
/// to bytes — pressing and holding Shift would otherwise block the event loop
/// and stall the resize tick. Key-ups never produce bytes, modifier keys never
/// produce bytes, a record carrying a `UnicodeChar` always does, and the
/// navigation keys VT translates are whitelisted rather than assumed: an
/// unrecognised zero-`UnicodeChar` key is dropped, which loses a keystroke `tread`
/// binds nothing to, instead of hanging on one.
/// Virtual keys that only ever change the state of another key.
const
/// Virtual keys the console turns into an escape sequence even though the
/// record carries no `UnicodeChar`.
const
/// What a `SetConsoleCtrlHandler` callback should do with a control event.
pub const CTRL_C_EVENT: u32 = 0;
pub const CTRL_BREAK_EVENT: u32 = 1;
pub const CTRL_CLOSE_EVENT: u32 = 2;
pub const CTRL_LOGOFF_EVENT: u32 = 5;
pub const CTRL_SHUTDOWN_EVENT: u32 = 6;
/// Leave the alternate screen, show the cursor, reset SGR — the teardown a
/// console control handler has to do for itself.
///
/// `term.rs` owns every other escape sequence in this crate and the backend
/// emits none. The exception is `CTRL_CLOSE_EVENT` and friends: they kill the
/// process a moment after the handler returns, so the event loop never gets
/// another turn and `term.rs` never runs its teardown. Calling back up into a
/// layer that allocates and takes a mutex from an injected handler thread is not
/// worth the deadlock, so the three sequences are one `const` buffer instead.
/// Leaving a screen that was never entered is a no-op in every terminal, so the
/// handler needs no state to decide.
///
/// It lives here, rather than next to its one caller in `windows.rs`, so the
/// Linux host can assert what is *not* in it: no mouse tracking, ever
/// (SPEC.md §"Hard constraints" #5).
pub const LEAVE_SCREEN: & = b"\x1b[?1049l\x1b[?25h\x1b[0m";
/// Map a `CTRL_*_EVENT` to the flag it should raise. The three termination
/// events are the Windows counterpart of SIGTERM/SIGHUP/SIGQUIT, which the unix
/// backend catches for exactly the same reason: not to be killed while the
/// terminal is still in raw mode.
pub const