|
2 | 2 | #include "w32fd.h"
|
3 | 3 | #include "inc/defs.h"
|
4 | 4 |
|
5 |
| -/* Win7 - Read Term Support - START*/ |
| 5 | +#define TERM_IO_BUF_SIZE 2048 |
| 6 | +int errno_from_Win32Error(int win32_error); |
6 | 7 |
|
7 |
| -int |
8 |
| -fileio_initiateReadTerm_Win7(struct w32_io* pio) { |
| 8 | +struct io_status { |
| 9 | + char* buf[TERM_IO_BUF_SIZE]; |
| 10 | + DWORD transferred; |
| 11 | + DWORD error; |
| 12 | +}; |
9 | 13 |
|
10 |
| - if (pio->read_details.pending || w32_io_is_io_available(pio, TRUE)) { |
11 |
| - debug("Win7 term read - ERROR - called in wrong state"); |
12 |
| - errno = EINVAL; |
| 14 | +static struct io_status read_status, write_status; |
| 15 | + |
| 16 | +static VOID CALLBACK ReadAPCProc( |
| 17 | + _In_ ULONG_PTR dwParam |
| 18 | + ) { |
| 19 | + struct w32_io* pio = (struct w32_io*)dwParam; |
| 20 | + pio->read_details.error = read_status.error; |
| 21 | + pio->read_details.remaining = read_status.transferred; |
| 22 | + pio->read_details.completed = 0; |
| 23 | + pio->read_details.pending = FALSE; |
| 24 | +} |
| 25 | + |
| 26 | +static DWORD WINAPI ReadThread( |
| 27 | + _In_ LPVOID lpParameter |
| 28 | + ) { |
| 29 | + struct w32_io* pio = (struct w32_io*)lpParameter; |
| 30 | + memset(&read_status, 0, sizeof(read_status)); |
| 31 | + if (!ReadFile(WINHANDLE(pio), read_status.buf, TERM_IO_BUF_SIZE, &read_status.transferred, NULL)) { |
| 32 | + read_status.error = GetLastError(); |
| 33 | + } |
| 34 | + |
| 35 | + if (0 == QueueUserAPC(ReadAPCProc, main_thread, pio)) |
| 36 | + DebugBreak(); |
| 37 | +} |
| 38 | + |
| 39 | +static int |
| 40 | +termio_initiate_read(struct w32_io* pio) { |
| 41 | + HANDLE read_thread = CreateThread(NULL, 0, ReadThread, pio, 0, NULL); |
| 42 | + if (read_thread == NULL) { |
| 43 | + errno = errno_from_Win32Error(GetLastError()); |
13 | 44 | return -1;
|
14 | 45 | }
|
15 | 46 |
|
16 | 47 | return 0;
|
17 | 48 | }
|
18 | 49 |
|
19 |
| -/* Win7 - Read Term Support - END*/ |
20 |
| - |
21 | 50 | int
|
22 | 51 | termio_on_select(struct w32_io* pio, BOOL rd) {
|
23 |
| - return fileio_on_select(pio, rd); |
| 52 | + if (!rd) |
| 53 | + return 0; |
| 54 | + |
| 55 | + if ((!fileio_is_io_available(pio, rd)) && (!pio->read_details.pending)) |
| 56 | + return termio_initiate_read(pio); |
24 | 57 | }
|
25 | 58 |
|
26 | 59 | int
|
|
0 commit comments