1 /****************************************************************************
2 * Copyright 2020-2021,2023 Thomas E. Dickey *
3 * Copyright 1998-2009,2010 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Juergen Pfeifer *
32 * and: Thomas E. Dickey *
33 ****************************************************************************/
36 * TODO - GetMousePos(POINT * result) from ntconio.c
39 #include <curses.priv.h>
41 MODULE_ID("$Id: lib_win32con.c,v 1.14 2023/08/05 20:44:38 tom Exp $")
54 #define write_screen WriteConsoleOutputW
55 #define read_screen ReadConsoleOutputW
57 #define write_screen WriteConsoleOutput
58 #define read_screen ReadConsoleOutput
61 static bool read_screen_data(void);
63 #define GenMap(vKey,key) MAKELONG(key, vKey)
64 static const LONG keylist[] =
66 GenMap(VK_PRIOR, KEY_PPAGE),
67 GenMap(VK_NEXT, KEY_NPAGE),
68 GenMap(VK_END, KEY_END),
69 GenMap(VK_HOME, KEY_HOME),
70 GenMap(VK_LEFT, KEY_LEFT),
71 GenMap(VK_UP, KEY_UP),
72 GenMap(VK_RIGHT, KEY_RIGHT),
73 GenMap(VK_DOWN, KEY_DOWN),
74 GenMap(VK_DELETE, KEY_DC),
75 GenMap(VK_INSERT, KEY_IC)
77 static const LONG ansi_keys[] =
79 GenMap(VK_PRIOR, 'I'),
85 GenMap(VK_RIGHT, 'M'),
87 GenMap(VK_DELETE, 'S'),
88 GenMap(VK_INSERT, 'R')
90 #define array_length(a) (sizeof(a)/sizeof(a[0]))
91 #define N_INI ((int)array_length(keylist))
93 #define MAPSIZE (FKEYS + N_INI)
95 /* A process can only have a single console, so it is safe
96 to maintain all the information about it in a single
99 NCURSES_EXPORT_VAR(ConsoleInfo) _nc_CONSOLE;
100 static bool console_initialized = FALSE;
102 #define EnsureInit() (void)(console_initialized ? TRUE : _nc_console_checkinit(TRUE, TRUE))
104 #define REQUIRED_MAX_V (DWORD)10
105 #define REQUIRED_MIN_V (DWORD)0
106 #define REQUIRED_BUILD (DWORD)17763
108 This function returns 0 if the Windows version has no support for
109 the modern Console interface, otherwise it returns 1
112 _nc_console_vt_supported(void)
117 T((T_CALLED("lib_win32con::_nc_console_vt_supported")));
118 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
119 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
122 T(("GetVersionEx returnedMajor=%ld, Minor=%ld, Build=%ld",
125 osvi.dwBuildNumber));
126 if (osvi.dwMajorVersion >= REQUIRED_MAX_V) {
127 if (osvi.dwMajorVersion == REQUIRED_MAX_V) {
128 if (((osvi.dwMinorVersion == REQUIRED_MIN_V) &&
129 (osvi.dwBuildNumber >= REQUIRED_BUILD)) ||
130 ((osvi.dwMinorVersion > REQUIRED_MIN_V)))
139 _nc_console_size(int *Lines, int *Cols)
142 if (Lines != NULL && Cols != NULL) {
143 if (WINCONSOLE.buffered) {
144 *Lines = (int) (WINCONSOLE.SBI.dwSize.Y);
145 *Cols = (int) (WINCONSOLE.SBI.dwSize.X);
147 *Lines = (int) (WINCONSOLE.SBI.srWindow.Bottom + 1 -
148 WINCONSOLE.SBI.srWindow.Top);
149 *Cols = (int) (WINCONSOLE.SBI.srWindow.Right + 1 -
150 WINCONSOLE.SBI.srWindow.Left);
155 /* Convert a file descriptor into a HANDLE
156 That's not necessarily a console HANDLE
158 NCURSES_EXPORT(HANDLE)
159 _nc_console_handle(int fd)
161 intptr_t value = _get_osfhandle(fd);
162 return (HANDLE) value;
165 /* Validate that a HANDLE is actually a
169 IsConsoleHandle(HANDLE hdl)
174 T((T_CALLED("lib_win32con::IsConsoleHandle(HANDLE=%p"), hdl));
178 if (!GetConsoleMode(hdl, &dwFlag)) {
179 T(("GetConsoleMode failed"));
187 /* This is used when running in terminfo mode to discover,
188 whether or not the "terminal" is actually a Windows
189 Console. It is the responsibility of the console to deal
190 with the terminal escape sequences that are sent by
194 _nc_console_test(int fd)
197 HANDLE hdl = INVALID_HANDLE_VALUE;
198 T((T_CALLED("lib_win32con::_nc_console_test(%d)"), fd));
199 hdl = _nc_console_handle(fd);
200 code = (int) IsConsoleHandle(hdl);
204 #define OutHandle() ((WINCONSOLE.isTermInfoConsole || WINCONSOLE.progMode) ? WINCONSOLE.hdl : WINCONSOLE.out)
207 _nc_console_selectActiveHandle(void)
209 if (WINCONSOLE.lastOut != WINCONSOLE.hdl) {
210 WINCONSOLE.lastOut = WINCONSOLE.hdl;
211 SetConsoleActiveScreenBuffer(WINCONSOLE.lastOut);
215 NCURSES_EXPORT(HANDLE)
216 _nc_console_fd2handle(int fd)
218 HANDLE hdl = _nc_console_handle(fd);
219 if (hdl == WINCONSOLE.inp) {
220 T(("lib_win32con:validateHandle %d -> WINCONSOLE.inp", fd));
221 } else if (hdl == WINCONSOLE.hdl) {
222 T(("lib_win32con:validateHandle %d -> WINCONSOLE.hdl", fd));
223 } else if (hdl == WINCONSOLE.out) {
224 T(("lib_win32con:validateHandle %d -> WINCONSOLE.out", fd));
226 T(("lib_win32con:validateHandle %d maps to unknown HANDLE", fd));
227 hdl = INVALID_HANDLE_VALUE;
230 assert(hdl != INVALID_HANDLE_VALUE);
232 if (hdl != INVALID_HANDLE_VALUE) {
233 if (hdl != WINCONSOLE.inp && (!WINCONSOLE.isTermInfoConsole && WINCONSOLE.progMode)) {
234 if (hdl == WINCONSOLE.out && hdl != WINCONSOLE.hdl) {
235 T(("lib_win32con:validateHandle forcing WINCONSOLE.out -> WINCONSOLE.hdl"));
236 hdl = WINCONSOLE.hdl;
244 _nc_console_setmode(HANDLE hdl, const TTY * arg)
253 #define TRCTTYOUT(flag) TRCTTY.dwFlagOut = flag
254 #define TRCTTYIN(flag) TRCTTY.dwFlagIn = flag
256 #define TRCTTYOUT(flag)
257 #define TRCTTYIN(flag)
259 T(("lib_win32con:_nc_console_setmode %s", _nc_trace_ttymode(arg)));
260 if (hdl == WINCONSOLE.inp) {
261 dwFlag = arg->dwFlagIn | ENABLE_MOUSE_INPUT | VT_FLAG_IN;
262 if (WINCONSOLE.isTermInfoConsole)
263 dwFlag |= (VT_FLAG_IN);
265 dwFlag &= (DWORD) ~ (VT_FLAG_IN);
267 SetConsoleMode(hdl, dwFlag);
270 dwFlag = arg->dwFlagOut;
271 if (WINCONSOLE.isTermInfoConsole)
272 dwFlag |= (VT_FLAG_OUT);
274 dwFlag |= (VT_FLAG_OUT);
276 SetConsoleMode(alt, dwFlag);
278 dwFlag = arg->dwFlagOut;
279 if (WINCONSOLE.isTermInfoConsole)
280 dwFlag |= (VT_FLAG_OUT);
282 dwFlag |= (VT_FLAG_OUT);
284 SetConsoleMode(hdl, dwFlag);
286 alt = WINCONSOLE.inp;
287 dwFlag = arg->dwFlagIn | ENABLE_MOUSE_INPUT;
288 if (WINCONSOLE.isTermInfoConsole)
289 dwFlag |= (VT_FLAG_IN);
291 dwFlag &= (DWORD) ~ (VT_FLAG_IN);
293 SetConsoleMode(alt, dwFlag);
294 T(("effective mode set %s", _nc_trace_ttymode(&TRCTTY)));
302 _nc_console_getmode(HANDLE hdl, TTY * arg)
310 if (hdl == WINCONSOLE.inp) {
311 if (GetConsoleMode(hdl, &dwFlag)) {
312 arg->dwFlagIn = dwFlag;
314 if (GetConsoleMode(alt, &dwFlag)) {
315 arg->dwFlagOut = dwFlag;
320 if (GetConsoleMode(hdl, &dwFlag)) {
321 arg->dwFlagOut = dwFlag;
322 alt = WINCONSOLE.inp;
323 if (GetConsoleMode(alt, &dwFlag)) {
324 arg->dwFlagIn = dwFlag;
330 T(("lib_win32con:_nc_console_getmode %s", _nc_trace_ttymode(arg)));
335 _nc_console_flush(HANDLE hdl)
339 T((T_CALLED("lib_win32con::_nc_console_flush(hdl=%p"), hdl));
341 if (hdl != INVALID_HANDLE_VALUE) {
342 if (hdl == WINCONSOLE.hdl ||
343 hdl == WINCONSOLE.inp ||
344 hdl == WINCONSOLE.out) {
345 if (!FlushConsoleInputBuffer(WINCONSOLE.inp))
349 T(("_nc_console_flush not requesting a handle owned by console."));
356 _nc_console_MapColor(bool fore, int color)
358 static const int _cmap[] =
359 {0, 4, 2, 6, 1, 5, 3, 7};
361 if (color < 0 || color > 7)
371 * Attempt to save the screen contents. PDCurses does this if
372 * PDC_RESTORE_SCREEN is set, giving the same visual appearance on
373 * restoration as if the library had allocated a console buffer. MSDN
374 * says that the data which can be read is limited to 64Kb (and may be
378 save_original_screen(void)
382 WINCONSOLE.save_region.Top = 0;
383 WINCONSOLE.save_region.Left = 0;
384 WINCONSOLE.save_region.Bottom = (SHORT) (WINCONSOLE.SBI.dwSize.Y - 1);
385 WINCONSOLE.save_region.Right = (SHORT) (WINCONSOLE.SBI.dwSize.X - 1);
387 if (read_screen_data()) {
391 WINCONSOLE.save_region.Top = WINCONSOLE.SBI.srWindow.Top;
392 WINCONSOLE.save_region.Left = WINCONSOLE.SBI.srWindow.Left;
393 WINCONSOLE.save_region.Bottom = WINCONSOLE.SBI.srWindow.Bottom;
394 WINCONSOLE.save_region.Right = WINCONSOLE.SBI.srWindow.Right;
396 WINCONSOLE.window_only = TRUE;
398 if (read_screen_data()) {
403 T(("... save original screen contents %s", result ? "ok" : "err"));
409 restore_original_screen(void)
413 SMALL_RECT save_region = WINCONSOLE.save_region;
415 T(("... restoring %s",
416 WINCONSOLE.window_only ? "window" : "entire buffer"));
418 bufferCoord.X = (SHORT) (WINCONSOLE.window_only ?
419 WINCONSOLE.SBI.srWindow.Left : 0);
420 bufferCoord.Y = (SHORT) (WINCONSOLE.window_only ?
421 WINCONSOLE.SBI.srWindow.Top : 0);
423 if (write_screen(WINCONSOLE.hdl,
424 WINCONSOLE.save_screen,
425 WINCONSOLE.save_size,
429 mvcur(-1, -1, LINES - 2, 0);
430 T(("... restore original screen contents ok %dx%d (%d,%d - %d,%d)",
431 WINCONSOLE.save_size.Y,
432 WINCONSOLE.save_size.X,
438 T(("... restore original screen contents err"));
445 read_screen_data(void)
451 WINCONSOLE.save_size.X = (SHORT) (WINCONSOLE.save_region.Right
452 - WINCONSOLE.save_region.Left + 1);
453 WINCONSOLE.save_size.Y = (SHORT) (WINCONSOLE.save_region.Bottom
454 - WINCONSOLE.save_region.Top + 1);
456 want = (size_t) (WINCONSOLE.save_size.X * WINCONSOLE.save_size.Y);
458 if ((WINCONSOLE.save_screen = malloc(want * sizeof(CHAR_INFO))) != 0) {
459 bufferCoord.X = (SHORT) (WINCONSOLE.window_only ?
460 WINCONSOLE.SBI.srWindow.Left : 0);
461 bufferCoord.Y = (SHORT) (WINCONSOLE.window_only ?
462 WINCONSOLE.SBI.srWindow.Top : 0);
464 T(("... reading console %s %dx%d into %d,%d - %d,%d at %d,%d",
465 WINCONSOLE.window_only ? "window" : "buffer",
466 WINCONSOLE.save_size.Y, WINCONSOLE.save_size.X,
467 WINCONSOLE.save_region.Top,
468 WINCONSOLE.save_region.Left,
469 WINCONSOLE.save_region.Bottom,
470 WINCONSOLE.save_region.Right,
474 if (read_screen(WINCONSOLE.hdl,
475 WINCONSOLE.save_screen,
476 WINCONSOLE.save_size,
478 &WINCONSOLE.save_region)) {
481 T((" error %#lx", (unsigned long) GetLastError()));
482 FreeAndNull(WINCONSOLE.save_screen);
490 _nc_console_get_SBI(void)
493 if (GetConsoleScreenBufferInfo(WINCONSOLE.hdl, &(WINCONSOLE.SBI))) {
494 T(("GetConsoleScreenBufferInfo"));
495 T(("... buffer(X:%d Y:%d)",
496 WINCONSOLE.SBI.dwSize.X,
497 WINCONSOLE.SBI.dwSize.Y));
498 T(("... window(X:%d Y:%d)",
499 WINCONSOLE.SBI.dwMaximumWindowSize.X,
500 WINCONSOLE.SBI.dwMaximumWindowSize.Y));
501 T(("... cursor(X:%d Y:%d)",
502 WINCONSOLE.SBI.dwCursorPosition.X,
503 WINCONSOLE.SBI.dwCursorPosition.Y));
504 T(("... display(Top:%d Bottom:%d Left:%d Right:%d)",
505 WINCONSOLE.SBI.srWindow.Top,
506 WINCONSOLE.SBI.srWindow.Bottom,
507 WINCONSOLE.SBI.srWindow.Left,
508 WINCONSOLE.SBI.srWindow.Right));
509 if (WINCONSOLE.buffered) {
510 WINCONSOLE.origin.X = 0;
511 WINCONSOLE.origin.Y = 0;
513 WINCONSOLE.origin.X = WINCONSOLE.SBI.srWindow.Left;
514 WINCONSOLE.origin.Y = WINCONSOLE.SBI.srWindow.Top;
518 T(("GetConsoleScreenBufferInfo ERR"));
527 * In "normal" mode, reset the buffer- and window-sizes back to their original values.
530 _nc_console_set_scrollback(bool normal, CONSOLE_SCREEN_BUFFER_INFO * info)
534 bool changed = FALSE;
536 T((T_CALLED("lib_win32con::_nc_console_set_scrollback(%s)"),
541 T(("... SBI.srWindow %d,%d .. %d,%d",
544 info->srWindow.Bottom,
545 info->srWindow.Right));
546 T(("... SBI.dwSize %dx%d",
551 rect = info->srWindow;
552 coord = info->dwSize;
553 if (memcmp(info, &WINCONSOLE.SBI, sizeof(*info)) != 0) {
555 WINCONSOLE.SBI = *info;
558 int high = info->srWindow.Bottom - info->srWindow.Top + 1;
559 int wide = info->srWindow.Right - info->srWindow.Left + 1;
561 if (high < MIN_HIGH) {
562 T(("... height %d < %d", high, MIN_HIGH));
566 if (wide < MIN_WIDE) {
567 T(("... width %d < %d", wide, MIN_WIDE));
574 rect.Right = (SHORT) (wide - 1);
575 rect.Bottom = (SHORT) (high - 1);
577 coord.X = (SHORT) wide;
578 coord.Y = (SHORT) high;
580 if (info->dwSize.Y != high ||
581 info->dwSize.X != wide ||
582 info->srWindow.Top != 0 ||
583 info->srWindow.Left != 0) {
590 T(("... coord %d,%d", coord.Y, coord.X));
591 T(("... rect %d,%d - %d,%d",
593 rect.Bottom, rect.Right));
594 SetConsoleScreenBufferSize(WINCONSOLE.hdl, coord); /* dwSize */
595 SetConsoleWindowInfo(WINCONSOLE.hdl, TRUE, &rect); /* srWindow */
596 _nc_console_get_SBI();
602 tdiff(FILETIME fstart, FILETIME fend)
604 ULARGE_INTEGER ustart;
608 ustart.LowPart = fstart.dwLowDateTime;
609 ustart.HighPart = fstart.dwHighDateTime;
610 uend.LowPart = fend.dwLowDateTime;
611 uend.HighPart = fend.dwHighDateTime;
613 diff = (uend.QuadPart - ustart.QuadPart) / 10000;
618 Adjust(int milliseconds, int diff)
620 if (milliseconds != INFINITY) {
621 milliseconds -= diff;
622 if (milliseconds < 0)
628 #define BUTTON_MASK (FROM_LEFT_1ST_BUTTON_PRESSED | \
629 FROM_LEFT_2ND_BUTTON_PRESSED | \
630 FROM_LEFT_3RD_BUTTON_PRESSED | \
631 FROM_LEFT_4TH_BUTTON_PRESSED | \
632 RIGHTMOST_BUTTON_PRESSED)
635 decode_mouse(SCREEN *sp, int mask)
640 assert(sp && console_initialized);
642 if (mask & FROM_LEFT_1ST_BUTTON_PRESSED)
643 result |= BUTTON1_PRESSED;
644 if (mask & FROM_LEFT_2ND_BUTTON_PRESSED)
645 result |= BUTTON2_PRESSED;
646 if (mask & FROM_LEFT_3RD_BUTTON_PRESSED)
647 result |= BUTTON3_PRESSED;
648 if (mask & FROM_LEFT_4TH_BUTTON_PRESSED)
649 result |= BUTTON4_PRESSED;
651 if (mask & RIGHTMOST_BUTTON_PRESSED) {
652 switch (WINCONSOLE.numButtons) {
654 result |= BUTTON1_PRESSED;
657 result |= BUTTON2_PRESSED;
660 result |= BUTTON3_PRESSED;
663 result |= BUTTON4_PRESSED;
671 #define AdjustY() (WINCONSOLE.buffered ? 0 : (int) WINCONSOLE.SBI.srWindow.Top)
674 handle_mouse(SCREEN *sp, MOUSE_EVENT_RECORD mer)
681 sp->_drv_mouse_old_buttons = sp->_drv_mouse_new_buttons;
682 sp->_drv_mouse_new_buttons = mer.dwButtonState & BUTTON_MASK;
685 * We're only interested if the button is pressed or released.
686 * FIXME: implement continuous event-tracking.
688 if (sp->_drv_mouse_new_buttons != sp->_drv_mouse_old_buttons) {
689 memset(&work, 0, sizeof(work));
691 if (sp->_drv_mouse_new_buttons) {
692 work.bstate |= decode_mouse(sp, sp->_drv_mouse_new_buttons);
694 /* cf: BUTTON_PRESSED, BUTTON_RELEASED */
695 work.bstate |= (decode_mouse(sp, sp->_drv_mouse_old_buttons)
700 work.x = mer.dwMousePosition.X;
701 work.y = mer.dwMousePosition.Y - AdjustY();
703 sp->_drv_mouse_fifo[sp->_drv_mouse_tail] = work;
704 sp->_drv_mouse_tail += 1;
710 rkeycompare(const void *el1, const void *el2)
712 WORD key1 = (LOWORD((*((const LONG *) el1)))) & 0x7fff;
713 WORD key2 = (LOWORD((*((const LONG *) el2)))) & 0x7fff;
715 return ((key1 < key2) ? -1 : ((key1 == key2) ? 0 : 1));
719 keycompare(const void *el1, const void *el2)
721 WORD key1 = HIWORD((*((const LONG *) el1)));
722 WORD key2 = HIWORD((*((const LONG *) el2)));
724 return ((key1 < key2) ? -1 : ((key1 == key2) ? 0 : 1));
732 if (!WINCONSOLE.isTermInfoConsole) {
735 LONG key = GenMap(vKey, 0);
739 (size_t) (N_INI + FKEYS),
743 key = *((LONG *) res);
745 code = (int) (nKey & 0x7fff);
758 if (!WINCONSOLE.isTermInfoConsole) {
761 LONG key = GenMap(vKey, 0);
765 (size_t) (N_INI + FKEYS),
769 key = *((LONG *) res);
771 code = (int) (nKey & 0x7fff);
780 _nc_console_keyok(int keycode, int flag)
786 LONG key = GenMap(0, (WORD) keycode);
788 T((T_CALLED("lib_win32con::_nc_console_keyok(%d, %d)"), keycode, flag));
792 (size_t) (N_INI + FKEYS),
796 key = *((LONG *) res);
798 nKey = (LOWORD(key)) & 0x7fff;
801 *(LONG *) res = GenMap(vKey, nKey);
807 _nc_console_keyExist(int keycode)
812 LONG key = GenMap(0, (WORD) keycode);
814 T((T_CALLED("lib_win32con::_nc_console_keyExist(%d)"), keycode));
817 (size_t) (N_INI + FKEYS),
821 key = *((LONG *) res);
823 if (!(nKey & 0x8000))
836 EVENTLIST_2nd(_nc_eventlist * evl))
838 INPUT_RECORD inp_rec;
840 DWORD nRead = 0, rc = (DWORD) (-1);
845 bool isNoDelay = (milliseconds == 0);
847 #ifdef NCURSES_WGETCH_EVENTS
848 (void) evl; /* TODO: implement wgetch-events */
851 #define IGNORE_CTRL_KEYS (SHIFT_PRESSED|LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED| \
852 LEFT_CTRL_PRESSED|RIGHT_CTRL_PRESSED)
853 #define CONSUME() ReadConsoleInput(hdl, &inp_rec, 1, &nRead)
857 TR(TRACE_IEVENT, ("start twait: hdl=%p, %d milliseconds, mode: %d",
858 hdl, milliseconds, mode));
860 if (milliseconds < 0)
861 milliseconds = INFINITY;
863 memset(&inp_rec, 0, sizeof(inp_rec));
867 GetSystemTimeAsFileTime(&fstart);
868 rc = WaitForSingleObject(hdl, (DWORD) milliseconds);
869 GetSystemTimeAsFileTime(&fend);
870 diff = (int) tdiff(fstart, fend);
871 milliseconds = Adjust(milliseconds, diff);
872 if (milliseconds < 0)
876 if (isNoDelay || (rc == WAIT_OBJECT_0)) {
879 b = GetNumberOfConsoleInputEvents(hdl, &nRead);
881 T(("twait:err GetNumberOfConsoleInputEvents"));
883 if (isNoDelay && b) {
884 T(("twait: Events Available: %ld", nRead));
890 INPUT_RECORD *pInpRec =
891 TypeAlloca(INPUT_RECORD, nRead);
892 if (pInpRec != NULL) {
895 memset(pInpRec, 0, sizeof(INPUT_RECORD) * nRead);
896 f = PeekConsoleInput(hdl, pInpRec, nRead, &n);
898 for (i = 0; i < n; i++) {
899 if (pInpRec[i].EventType == KEY_EVENT) {
900 if (pInpRec[i].Event.KeyEvent.bKeyDown) {
902 (pInpRec[i].Event.KeyEvent.dwControlKeyState &
912 T(("twait:err PeekConsoleInput"));
917 T(("twait:err could not alloca input records"));
921 if (b && nRead > 0) {
922 b = PeekConsoleInput(hdl, &inp_rec, 1, &nRead);
924 T(("twait:err PeekConsoleInput"));
926 if (b && nRead > 0) {
927 switch (inp_rec.EventType) {
929 if (mode & TW_INPUT) {
931 inp_rec.Event.KeyEvent.wVirtualKeyCode;
933 inp_rec.Event.KeyEvent.uChar.AsciiChar;
934 T(("twait:event KEY_EVENT"));
935 T(("twait vk=%d, ch=%d, keydown=%d",
936 vk, ch, inp_rec.Event.KeyEvent.bKeyDown));
937 if (inp_rec.Event.KeyEvent.bKeyDown) {
938 T(("twait:event KeyDown"));
939 if (!WINCONSOLE.isTermInfoConsole &&
941 int nKey = MapKey(vk);
955 T(("twait:event MOUSE_EVENT"));
957 (inp_rec.Event.MouseEvent.dwButtonState
958 & BUTTON_MASK)) == 0) {
960 } else if (mode & TW_MOUSE) {
965 /* e.g., FOCUS_EVENT */
967 T(("twait:event Tyoe %d", inp_rec.EventType));
969 _nc_console_selectActiveHandle();
977 if (rc != WAIT_TIMEOUT) {
988 TR(TRACE_IEVENT, ("end twait: returned %d (%lu), remaining time %d msec",
989 code, GetLastError(), milliseconds));
992 *timeleft = milliseconds;
998 _nc_console_testmouse(
1002 EVENTLIST_2nd(_nc_eventlist * evl))
1008 if (sp->_drv_mouse_head < sp->_drv_mouse_tail) {
1011 rc = _nc_console_twait(sp,
1016 EVENTLIST_2nd(evl));
1028 INPUT_RECORD inp_rec;
1036 memset(&inp_rec, 0, sizeof(inp_rec));
1038 T((T_CALLED("lib_win32con::_nc_console_read(%p)"), sp));
1040 while ((b = ReadConsoleInput(hdl, &inp_rec, 1, &nRead))) {
1041 if (b && nRead > 0) {
1044 rc = rc + (int) nRead;
1045 if (inp_rec.EventType == KEY_EVENT) {
1046 if (!inp_rec.Event.KeyEvent.bKeyDown)
1048 *buf = (int) inp_rec.Event.KeyEvent.uChar.AsciiChar;
1049 vk = inp_rec.Event.KeyEvent.wVirtualKeyCode;
1051 * There are 24 virtual function-keys, and typically
1052 * 12 function-keys on a keyboard. Use the shift-modifier
1053 * to provide the remaining 12 keys.
1055 if (vk >= VK_F1 && vk <= VK_F12) {
1056 if (inp_rec.Event.KeyEvent.dwControlKeyState &
1058 vk = (WORD) (vk + 12);
1062 int key = MapKey(vk);
1065 if (sp->_keypad_on) {
1073 } else if (inp_rec.EventType == MOUSE_EVENT) {
1074 if (handle_mouse(sp,
1075 inp_rec.Event.MouseEvent)) {
1086 /* Our replacement for the systems _isatty to include also
1087 a test for mintty. This is called from the NC_ISATTY macro
1088 defined in curses.priv.h
1092 - 1 : A Windows character device detected by _isatty
1093 - 2 : A future implementation may return 2 for mintty
1096 _nc_console_isatty(int fd)
1099 T((T_CALLED("lib_win32con::_nc_console_isatty(%d"), fd));
1103 #ifdef _NC_CHECK_MINTTY
1105 if (_nc_console_checkmintty(fd, NULL)) {
1108 "ncurses on Windows must run in a Windows console.\n"
1109 "On newer versions of Windows, the calling program should create a PTY-like.\n"
1110 "device using the CreatePseudoConsole Windows API call.\n");
1118 NCURSES_EXPORT(bool)
1119 _nc_console_checkinit(bool initFlag, bool assumeTermInfo)
1123 T((T_CALLED("lib_win32con::_nc_console_checkinit(initFlag=%d, assumeTermInfo=%d)"),
1124 initFlag, assumeTermInfo));
1127 res = console_initialized;
1129 /* initialize once, or not at all */
1130 if (!console_initialized) {
1134 BOOL buffered = FALSE;
1138 WINCONSOLE.isTermInfoConsole = assumeTermInfo;
1140 WINCONSOLE.map = (LPDWORD) malloc(sizeof(DWORD) * MAPSIZE);
1141 WINCONSOLE.rmap = (LPDWORD) malloc(sizeof(DWORD) * MAPSIZE);
1142 WINCONSOLE.ansi_map = (LPDWORD) malloc(sizeof(DWORD) * MAPSIZE);
1144 for (i = 0; i < (N_INI + FKEYS); i++) {
1146 WINCONSOLE.rmap[i] = WINCONSOLE.map[i] =
1148 WINCONSOLE.ansi_map[i] = (DWORD) ansi_keys[i];
1150 WINCONSOLE.rmap[i] = WINCONSOLE.map[i] =
1151 (DWORD) GenMap((VK_F1 + (i - N_INI)),
1152 (KEY_F(1) + (i - N_INI)));
1153 WINCONSOLE.ansi_map[i] =
1154 (DWORD) GenMap((VK_F1 + (i - N_INI)),
1155 (';' + (i - N_INI)));
1158 qsort(WINCONSOLE.ansi_map,
1162 qsort(WINCONSOLE.map,
1166 qsort(WINCONSOLE.rmap,
1171 if (GetNumberOfConsoleMouseButtons(&num_buttons)) {
1172 WINCONSOLE.numButtons = (int) num_buttons;
1174 WINCONSOLE.numButtons = 1;
1177 a = _nc_console_MapColor(true, COLOR_WHITE) |
1178 _nc_console_MapColor(false, COLOR_BLACK);
1179 for (i = 0; i < CON_NUMPAIRS; i++)
1180 WINCONSOLE.pairs[i] = a;
1182 #define SaveConsoleMode(handle, value) \
1183 GetConsoleMode(WINCONSOLE.handle, &WINCONSOLE.originalMode.value)
1185 if (WINCONSOLE.isTermInfoConsole) {
1186 WINCONSOLE.inp = GetStdHandle(STD_INPUT_HANDLE);
1187 WINCONSOLE.out = GetStdHandle(STD_OUTPUT_HANDLE);
1188 WINCONSOLE.hdl = WINCONSOLE.out;
1190 SaveConsoleMode(inp, dwFlagIn);
1191 SaveConsoleMode(out, dwFlagOut);
1197 b = AttachConsole(ATTACH_PARENT_PROCESS);
1199 WINCONSOLE.inp = GetDirectHandle("CONIN$", FILE_SHARE_READ);
1200 WINCONSOLE.out = GetDirectHandle("CONOUT$", FILE_SHARE_WRITE);
1202 SaveConsoleMode(inp, dwFlagIn);
1203 SaveConsoleMode(out, dwFlagOut);
1205 if (getenv("NCGDB") || getenv("NCURSES_CONSOLE2")) {
1206 WINCONSOLE.hdl = WINCONSOLE.out;
1207 T(("... will not buffer console"));
1209 T(("... creating console buffer"));
1211 CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
1212 FILE_SHARE_READ | FILE_SHARE_WRITE,
1214 CONSOLE_TEXTMODE_BUFFER,
1220 /* We set binary I/O even when using the console
1221 driver to cover the situation, that the
1222 TERM variable is set to #win32con, but actually
1223 Windows supports virtual terminal processing.
1224 So if terminfo functions are used in this setup,
1225 they actually may work.
1227 _setmode(fileno(stdin), _O_BINARY);
1228 _setmode(fileno(stdout), _O_BINARY);
1230 if (WINCONSOLE.hdl != INVALID_HANDLE_VALUE) {
1231 WINCONSOLE.buffered = buffered;
1232 _nc_console_get_SBI();
1233 WINCONSOLE.save_SBI = WINCONSOLE.SBI;
1235 save_original_screen();
1236 _nc_console_set_scrollback(FALSE, &WINCONSOLE.SBI);
1238 GetConsoleCursorInfo(WINCONSOLE.hdl, &WINCONSOLE.save_CI);
1239 T(("... initial cursor is %svisible, %d%%",
1240 (WINCONSOLE.save_CI.bVisible ? "" : "not-"),
1241 (int) WINCONSOLE.save_CI.dwSize));
1244 WINCONSOLE.initialized = TRUE;
1245 console_initialized = TRUE;
1247 res = (WINCONSOLE.hdl != INVALID_HANDLE_VALUE);
1252 #endif // _NC_WINDOWS