-
Notifications
You must be signed in to change notification settings - Fork 77
/
term_win32.cpp
328 lines (281 loc) · 10.3 KB
/
term_win32.cpp
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
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstdio>
#include <sstream>
#include <string>
#include <algorithm>
#include <windows.h>
#include "cxxmatrix.hpp"
namespace cxxmatrix {
static int default_cols = 80;
static int default_rows = 25;
static void print_error_message(const char* title) {
LPSTR lpMsgBuf;
FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(),
//MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
(LPSTR) &lpMsgBuf, 0, NULL);
std::fprintf(stderr, "cxxmatrix: %s: Win32Error %lu\n%s\n", title, GetLastError(), lpMsgBuf);
LocalFree(lpMsgBuf);
}
static bool conpty_enabled = false;
static HANDLE conpty_hStdOutput = INVALID_HANDLE_VALUE;
static bool conpty_internal = false;
static DWORD conpty_input_mode_save = 0;
static DWORD conpty_output_mode_save = 0;
static UINT conpty_input_cp_save = 0;
static UINT conpty_output_cp_save = 0;
static bool conpty_winsize(int& cols, int& rows) {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE) {
print_error_message("GetStdHandle");
return false;
}
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (GetConsoleScreenBufferInfo(hOut, &csbi) == 0) {
print_error_message("GetConsoleScreenBufferInfo");
return false;
}
cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
return true;
}
static bool conpty_init(HANDLE hIn) {
conpty_hStdOutput = hIn;
conpty_enabled = conpty_winsize(default_cols, default_rows);
return conpty_enabled;
}
static void conpty_enter() {
if (conpty_internal) return;
conpty_internal = true;
if (HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); hIn == INVALID_HANDLE_VALUE) {
print_error_message("GetStdHandle (stdin)");
} else {
if (GetConsoleMode(hIn, &conpty_input_mode_save) == 0) {
print_error_message("GetConsoleMode (stdin)");
} else {
if (SetConsoleMode(hIn, conpty_input_mode_save | ENABLE_VIRTUAL_TERMINAL_INPUT) == 0)
print_error_message("SetConsoleMode (stdin)");
}
}
if (HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); hOut == INVALID_HANDLE_VALUE) {
print_error_message("GetStdHandle (stdout)");
} else {
if (GetConsoleMode(hOut, &conpty_output_mode_save) == 0) {
print_error_message("GetConsoleMode (stdout)");
} else {
if (SetConsoleMode(hOut, conpty_output_mode_save | ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0)
print_error_message("SetConsoleMode (stdout)");
}
}
conpty_input_cp_save = GetConsoleCP();
if (conpty_input_cp_save == 0)
print_error_message("GetConsoleCP");
else if (SetConsoleCP(CP_UTF8) == 0)
print_error_message("SetConsoleCP");
conpty_output_cp_save = GetConsoleOutputCP();
if (conpty_output_cp_save == 0)
print_error_message("GetConsoleOutputCP");
else if (SetConsoleOutputCP(CP_UTF8) == 0)
print_error_message("SetConsoleOutputCP");
}
static void conpty_leave() {
if (!conpty_internal) return;
conpty_internal = false;
if (HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); hIn == INVALID_HANDLE_VALUE) {
print_error_message("GetStdHandle (stdin)");
} else if (SetConsoleMode(hIn, conpty_input_mode_save) == 0) {
print_error_message("SetConsoleMode (stdin)");
}
if (HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); hOut == INVALID_HANDLE_VALUE) {
print_error_message("GetStdHandle (stdout)");
} else if (SetConsoleMode(hOut, conpty_output_mode_save) == 0) {
print_error_message("SetConsoleMode (stdout)");
}
if (conpty_input_cp_save != 0 && SetConsoleCP(conpty_input_cp_save) == 0)
print_error_message("SetConsoleCP (restore)");
if (conpty_output_cp_save != 0 && SetConsoleOutputCP(conpty_output_cp_save) == 0)
print_error_message("SetConsoleOutputCP (restore)");
}
static std::ptrdiff_t conpty_read(byte* buffer, std::size_t size) {
DWORD dwNumberOfEvents;
if (GetNumberOfConsoleInputEvents(conpty_hStdOutput, &dwNumberOfEvents) == 0) {
print_error_message("GetNumberOfConsoleInputEvents (broken console)");
return 0;
}
std::size_t read_size = 0;
INPUT_RECORD input_buffer;
while (read_size < size && dwNumberOfEvents--) {
DWORD dwNumberOfEventsRead;
if (ReadConsoleInput(conpty_hStdOutput, &input_buffer, 1, &dwNumberOfEventsRead) == 0) {
print_error_message("GetNumberOfConsoleInputEvents (broken console)");
return read_size;
}
if (dwNumberOfEventsRead) {
switch (input_buffer.EventType) {
case KEY_EVENT:
{
auto const& event = input_buffer.Event.KeyEvent;
if (event.bKeyDown && event.uChar.UnicodeChar < 0x100)
buffer[read_size++] = (std::uint8_t) event.uChar.UnicodeChar & 0xFF;
}
break;
case WINDOW_BUFFER_SIZE_EVENT:
{
auto const& event = input_buffer.Event.WindowBufferSizeEvent;
if (event.dwSize.X != default_cols || event.dwSize.Y != default_rows)
trapwinch(28);
}
break;
}
}
}
return (std::ptrdiff_t) read_size;
}
static constexpr std::uint32_t stty_checkwinsize_interval = 20;
static bool stty_enabled = false;
static HANDLE stty_hStdInput = INVALID_HANDLE_VALUE;
static std::uint32_t stty_refresh_count = 0;
static bool stty_update_winsize(int& cols, int& rows) {
// We want to do something like str=$(stty size 2> NUL) using Win API
char command[] = "stty.exe size";
SECURITY_ATTRIBUTES securityAttributes = {};
securityAttributes.nLength = sizeof securityAttributes;
securityAttributes.bInheritHandle = TRUE;
securityAttributes.lpSecurityDescriptor = NULL;
HANDLE hNulFile = CreateFileA("nul", GENERIC_WRITE, FILE_SHARE_WRITE, &securityAttributes, OPEN_EXISTING, 0, NULL);
if (hNulFile == INVALID_HANDLE_VALUE) {
print_error_message("CreateFileA");
return false;
}
HANDLE hPipeRead, hPipeWrite;
if (CreatePipe(&hPipeRead, &hPipeWrite, &securityAttributes, 0) == 0) {
print_error_message("CreatePipe");
CloseHandle(hNulFile);
return false;
}
STARTUPINFOA startupInfo = {};
{
startupInfo.cb = sizeof startupInfo;
startupInfo.dwFlags |= STARTF_USESTDHANDLES;
startupInfo.hStdInput = stty_hStdInput;
startupInfo.hStdOutput = hPipeWrite;
startupInfo.hStdError = hNulFile;
startupInfo.dwFlags |= STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = SW_HIDE;
}
PROCESS_INFORMATION proc = {};
if (CreateProcessA(NULL, command, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &startupInfo, &proc) == 0) {
print_error_message("CreateProcessA");
CloseHandle(hPipeRead);
CloseHandle(hPipeWrite);
CloseHandle(hNulFile);
return false;
}
std::string output;
bool alive;
do {
alive = WaitForSingleObject(proc.hProcess, 50) != WAIT_OBJECT_0;
char buff[1024];
DWORD available_size = 0;
while (PeekNamedPipe(hPipeRead, NULL, 0, NULL, &available_size, NULL) && available_size > 0) {
do {
DWORD read_size = std::min((DWORD) sizeof(buff), available_size);
if (ReadFile(hPipeRead, &buff[0], read_size, &read_size, NULL) == 0) {
print_error_message("ReadFile (broken pipe)");
goto end_read;
}
for (DWORD i = 0; i < read_size; i++) {
if (buff[i] == '\r') {
buff[i] = '\n';
if (i + 1 < read_size && buff[i + 1] == '\n') i++;
}
output += buff[i];
}
available_size -= read_size;
} while (available_size > 0);
}
} while (alive);
end_read:
CloseHandle(proc.hProcess);
CloseHandle(proc.hThread);
CloseHandle(hPipeRead);
CloseHandle(hPipeWrite);
CloseHandle(hNulFile);
std::istringstream sstr(output);
int int_cols = -1, int_rows = -1;
if (sstr >> int_rows >> int_cols && int_cols > 0 && int_rows > 0) {
cols = int_cols;
rows = int_rows;
return true;
} else
return false;
}
static bool stty_init(HANDLE hIn) {
stty_hStdInput = hIn;
stty_enabled = stty_update_winsize(default_cols, default_rows);
return stty_enabled;
}
static void stty_enter() { std::system("stty.exe raw isig"); }
static void stty_leave() { std::system("stty.exe sane"); }
static std::ptrdiff_t stty_read(byte* buffer, std::size_t size) {
DWORD read_size = 0;
if (PeekNamedPipe(stty_hStdInput, NULL, 0, NULL, &read_size, NULL) && read_size > 0) {
if (read_size > size) read_size = size;
if (ReadFile(stty_hStdInput, buffer, read_size, &read_size, NULL) == 0) {
print_error_message("ReadFile (broken pipe)");
return 0;
}
}
if (++stty_refresh_count % stty_checkwinsize_interval == 0) {
int new_cols = -1, new_rows = -1;
if (stty_update_winsize(new_cols, new_rows) && (new_cols != default_cols || new_rows != default_rows)) {
default_cols = new_cols;
default_rows = new_rows;
trapwinch(28);
}
}
return (std::ptrdiff_t) read_size;
}
void term_init() {
if (HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); hIn == INVALID_HANDLE_VALUE) {
print_error_message("GetStdHandle (stdin)");
} else {
switch (GetFileType(hIn)) {
case FILE_TYPE_CHAR: // Console/ConPTY
if (conpty_init(hIn)) return;
break;
case FILE_TYPE_PIPE: // Cygwin/MSYS2 PTY?
if (stty_init(hIn)) return;
break;
}
}
term_winsize_from_env(default_cols, default_rows);
}
bool term_get_size(int& cols, int& rows) {
if (conpty_enabled && conpty_winsize(cols, rows)) return true;
cols = default_cols;
rows = default_rows;
return true;
}
void term_enter() {
if (conpty_enabled) conpty_enter();
if (stty_enabled) stty_enter();
}
void term_leave() {
if (conpty_enabled) conpty_leave();
if (stty_enabled) stty_leave();
}
std::ptrdiff_t term_read(byte* buffer, std::size_t size) {
if (conpty_enabled)
return conpty_read(buffer, size);
if (stty_enabled)
return stty_read(buffer, size);
return 0;
}
}