1 /****************************************************************************
2 * Copyright 2020,2021 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.7 2021/09/04 10:54:35 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 IsConsoleHandle(HANDLE hdl);
62 static bool save_original_screen(void);
63 static bool restore_original_screen(void) GCC_UNUSED;
64 static bool read_screen_data(void);
65 static int Adjust(int milliseconds, int diff);
66 static int decode_mouse(SCREEN *sp, int mask);
67 static bool handle_mouse(SCREEN *sp, MOUSE_EVENT_RECORD mer);
68 static int rkeycompare(const void *el1, const void *el2);
69 static int keycompare(const void *el1, const void *el2);
70 static int MapKey(WORD vKey);
71 static int AnsiKey(WORD vKey);
73 static ULONGLONG tdiff(FILETIME fstart, FILETIME fend);
75 #define GenMap(vKey,key) MAKELONG(key, vKey)
76 static const LONG keylist[] =
78 GenMap(VK_PRIOR, KEY_PPAGE),
79 GenMap(VK_NEXT, KEY_NPAGE),
80 GenMap(VK_END, KEY_END),
81 GenMap(VK_HOME, KEY_HOME),
82 GenMap(VK_LEFT, KEY_LEFT),
83 GenMap(VK_UP, KEY_UP),
84 GenMap(VK_RIGHT, KEY_RIGHT),
85 GenMap(VK_DOWN, KEY_DOWN),
86 GenMap(VK_DELETE, KEY_DC),
87 GenMap(VK_INSERT, KEY_IC)
89 static const LONG ansi_keys[] =
91 GenMap(VK_PRIOR, 'I'),
97 GenMap(VK_RIGHT, 'M'),
99 GenMap(VK_DELETE, 'S'),
100 GenMap(VK_INSERT, 'R')
102 #define array_length(a) (sizeof(a)/sizeof(a[0]))
103 #define N_INI ((int)array_length(keylist))
105 #define MAPSIZE (FKEYS + N_INI)
107 /* A process can only have a single console, so it is safe
108 to maintain all the information about it in a single
111 NCURSES_EXPORT_VAR(ConsoleInfo) _nc_CONSOLE;
112 static bool console_initialized = FALSE;
114 #define EnsureInit() (void)(console_initialized ? TRUE : _nc_console_checkinit(TRUE, TRUE))
116 #define REQUIRED_MAX_V (DWORD)10
117 #define REQUIRED_MIN_V (DWORD)0
118 #define REQUIRED_BUILD (DWORD)17763
120 This function returns 0 if the Windows version has no support for
121 the modern Console interface, otherwise it returns 1
124 _nc_console_vt_supported(void)
129 T((T_CALLED("lib_win32con::_nc_console_vt_supported")));
130 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
131 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
134 T(("GetVersionEx returnedMajor=%ld, Minor=%ld, Build=%ld",
137 osvi.dwBuildNumber));
138 if (osvi.dwMajorVersion >= REQUIRED_MAX_V) {
139 if (osvi.dwMajorVersion == REQUIRED_MAX_V) {
140 if (((osvi.dwMinorVersion == REQUIRED_MIN_V) &&
141 (osvi.dwBuildNumber >= REQUIRED_BUILD)) ||
142 ((osvi.dwMinorVersion > REQUIRED_MIN_V)))
151 _nc_console_size(int* Lines, int* Cols)
154 if (Lines != NULL && Cols != NULL) {
155 if (WINCONSOLE.buffered) {
156 *Lines = (int) (WINCONSOLE.SBI.dwSize.Y);
157 *Cols = (int) (WINCONSOLE.SBI.dwSize.X);
159 *Lines = (int) (WINCONSOLE.SBI.srWindow.Bottom + 1 -
160 WINCONSOLE.SBI.srWindow.Top);
161 *Cols = (int) (WINCONSOLE.SBI.srWindow.Right + 1 -
162 WINCONSOLE.SBI.srWindow.Left);
167 /* Convert a file descriptor into a HANDLE
168 That's not necessarily a console HANDLE
170 NCURSES_EXPORT(HANDLE)
171 _nc_console_handle(int fd)
173 intptr_t value = _get_osfhandle(fd);
174 return (HANDLE) value;
177 /* Validate that a HANDLE is actually a
181 IsConsoleHandle(HANDLE hdl)
186 T((T_CALLED("lib_win32con::IsConsoleHandle(HANDLE=%p"), hdl));
190 if (!GetConsoleMode(hdl, &dwFlag)) {
191 T(("GetConsoleMode failed"));
199 /* This is used when running in terminfo mode to discover,
200 whether or not the "terminal" is actually a Windows
201 Console. It is the responsibility of the console to deal
202 with the terminal escape sequences that are sent by
206 _nc_console_test(int fd)
209 HANDLE hdl = INVALID_HANDLE_VALUE;
210 T((T_CALLED("lib_win32con::_nc_console_test(%d)"), fd));
211 hdl = _nc_console_handle(fd);
212 code = (int) IsConsoleHandle(hdl);
216 #define OutHandle() ((WINCONSOLE.isTermInfoConsole || WINCONSOLE.progMode) ? WINCONSOLE.hdl : WINCONSOLE.out)
219 _nc_console_selectActiveHandle(void)
221 if (WINCONSOLE.lastOut != WINCONSOLE.hdl) {
222 WINCONSOLE.lastOut = WINCONSOLE.hdl;
223 SetConsoleActiveScreenBuffer(WINCONSOLE.lastOut);
227 NCURSES_EXPORT(HANDLE)
228 _nc_console_fd2handle(int fd)
230 HANDLE hdl = _nc_console_handle(fd);
231 if (hdl==WINCONSOLE.inp) {
232 T(("lib_win32con:validateHandle %d -> WINCONSOLE.inp", fd));
233 } else if (hdl==WINCONSOLE.hdl) {
234 T(("lib_win32con:validateHandle %d -> WINCONSOLE.hdl", fd));
235 } else if (hdl==WINCONSOLE.out) {
236 T(("lib_win32con:validateHandle %d -> WINCONSOLE.out", fd));
238 T(("lib_win32con:validateHandle %d maps to unknown HANDLE", fd));
239 hdl = INVALID_HANDLE_VALUE;
242 assert(hdl != INVALID_HANDLE_VALUE);
244 if (hdl != INVALID_HANDLE_VALUE) {
245 if (hdl != WINCONSOLE.inp && (!WINCONSOLE.isTermInfoConsole && WINCONSOLE.progMode)) {
246 if (hdl==WINCONSOLE.out && hdl!=WINCONSOLE.hdl) {
247 T(("lib_win32con:validateHandle forcing WINCONSOLE.out -> WINCONSOLE.hdl"));
248 hdl = WINCONSOLE.hdl;
256 _nc_console_setmode(HANDLE hdl, const TTY *arg)
265 #define TRCTTYOUT(flag) TRCTTY.dwFlagOut = flag
266 #define TRCTTYIN(flag) TRCTTY.dwFlagIn = flag
268 #define TRCTTYOUT(flag)
269 #define TRCTTYIN(flag)
271 T(("lib_win32con:_nc_console_setmode %s", _nc_trace_ttymode(arg)));
272 if (hdl==WINCONSOLE.inp) {
273 dwFlag = arg->dwFlagIn | ENABLE_MOUSE_INPUT | VT_FLAG_IN;
274 if (WINCONSOLE.isTermInfoConsole)
275 dwFlag |= (VT_FLAG_IN);
277 dwFlag &= (DWORD) ~(VT_FLAG_IN);
279 SetConsoleMode(hdl, dwFlag);
282 dwFlag = arg->dwFlagOut;
283 if (WINCONSOLE.isTermInfoConsole)
284 dwFlag |= (VT_FLAG_OUT);
286 dwFlag |= (VT_FLAG_OUT);
288 SetConsoleMode(alt, dwFlag);
290 dwFlag = arg->dwFlagOut;
291 if (WINCONSOLE.isTermInfoConsole)
292 dwFlag |= (VT_FLAG_OUT);
294 dwFlag |= (VT_FLAG_OUT);
296 SetConsoleMode(hdl, dwFlag);
298 alt = WINCONSOLE.inp;
299 dwFlag = arg->dwFlagIn | ENABLE_MOUSE_INPUT;
300 if (WINCONSOLE.isTermInfoConsole)
301 dwFlag |= (VT_FLAG_IN);
303 dwFlag &= (DWORD) ~(VT_FLAG_IN);
305 SetConsoleMode(alt, dwFlag);
306 T(("effective mode set %s", _nc_trace_ttymode(&TRCTTY)));
314 _nc_console_getmode(HANDLE hdl, TTY *arg)
322 if (hdl==WINCONSOLE.inp) {
323 if(GetConsoleMode(hdl, &dwFlag)) {
324 arg->dwFlagIn = dwFlag;
326 if (GetConsoleMode(alt, &dwFlag)) {
327 arg->dwFlagOut = dwFlag;
332 if (GetConsoleMode(hdl, &dwFlag)) {
333 arg->dwFlagOut = dwFlag;
334 alt = WINCONSOLE.inp;
335 if (GetConsoleMode(alt, &dwFlag)) {
336 arg->dwFlagIn = dwFlag;
342 T(("lib_win32con:_nc_console_getmode %s", _nc_trace_ttymode(arg)));
347 _nc_console_flush(HANDLE hdl)
351 T((T_CALLED("lib_win32con::_nc_console_flush(hdl=%p"), hdl));
353 if (hdl != INVALID_HANDLE_VALUE) {
354 if (hdl == WINCONSOLE.hdl ||
355 hdl == WINCONSOLE.inp ||
356 hdl == WINCONSOLE.out) {
357 if (!FlushConsoleInputBuffer(WINCONSOLE.inp))
361 T(("_nc_console_flush not requesting a handle owned by console."));
368 _nc_console_MapColor(bool fore, int color)
370 static const int _cmap[] =
371 {0, 4, 2, 6, 1, 5, 3, 7};
373 if (color < 0 || color > 7)
384 * Attempt to save the screen contents. PDCurses does this if
385 * PDC_RESTORE_SCREEN is set, giving the same visual appearance on
386 * restoration as if the library had allocated a console buffer. MSDN
387 * says that the data which can be read is limited to 64Kb (and may be
391 save_original_screen(void)
395 WINCONSOLE.save_region.Top = 0;
396 WINCONSOLE.save_region.Left = 0;
397 WINCONSOLE.save_region.Bottom = (SHORT) (WINCONSOLE.SBI.dwSize.Y - 1);
398 WINCONSOLE.save_region.Right = (SHORT) (WINCONSOLE.SBI.dwSize.X - 1);
400 if (read_screen_data()) {
404 WINCONSOLE.save_region.Top = WINCONSOLE.SBI.srWindow.Top;
405 WINCONSOLE.save_region.Left = WINCONSOLE.SBI.srWindow.Left;
406 WINCONSOLE.save_region.Bottom = WINCONSOLE.SBI.srWindow.Bottom;
407 WINCONSOLE.save_region.Right = WINCONSOLE.SBI.srWindow.Right;
409 WINCONSOLE.window_only = TRUE;
411 if (read_screen_data()) {
416 T(("... save original screen contents %s", result ? "ok" : "err"));
421 restore_original_screen(void)
425 SMALL_RECT save_region = WINCONSOLE.save_region;
427 T(("... restoring %s",
428 WINCONSOLE.window_only ? "window" : "entire buffer"));
430 bufferCoord.X = (SHORT) (WINCONSOLE.window_only ?
431 WINCONSOLE.SBI.srWindow.Left : 0);
432 bufferCoord.Y = (SHORT) (WINCONSOLE.window_only ?
433 WINCONSOLE.SBI.srWindow.Top : 0);
435 if (write_screen(WINCONSOLE.hdl,
436 WINCONSOLE.save_screen,
437 WINCONSOLE.save_size,
441 mvcur(-1, -1, LINES - 2, 0);
442 T(("... restore original screen contents ok %dx%d (%d,%d - %d,%d)",
443 WINCONSOLE.save_size.Y,
444 WINCONSOLE.save_size.X,
450 T(("... restore original screen contents err"));
456 read_screen_data(void)
462 WINCONSOLE.save_size.X = (SHORT) (WINCONSOLE.save_region.Right
463 - WINCONSOLE.save_region.Left + 1);
464 WINCONSOLE.save_size.Y = (SHORT) (WINCONSOLE.save_region.Bottom
465 - WINCONSOLE.save_region.Top + 1);
467 want = (size_t) (WINCONSOLE.save_size.X * WINCONSOLE.save_size.Y);
469 if ((WINCONSOLE.save_screen = malloc(want * sizeof(CHAR_INFO))) != 0) {
470 bufferCoord.X = (SHORT) (WINCONSOLE.window_only ?
471 WINCONSOLE.SBI.srWindow.Left : 0);
472 bufferCoord.Y = (SHORT) (WINCONSOLE.window_only ?
473 WINCONSOLE.SBI.srWindow.Top : 0);
475 T(("... reading console %s %dx%d into %d,%d - %d,%d at %d,%d",
476 WINCONSOLE.window_only ? "window" : "buffer",
477 WINCONSOLE.save_size.Y, WINCONSOLE.save_size.X,
478 WINCONSOLE.save_region.Top,
479 WINCONSOLE.save_region.Left,
480 WINCONSOLE.save_region.Bottom,
481 WINCONSOLE.save_region.Right,
485 if (read_screen(WINCONSOLE.hdl,
486 WINCONSOLE.save_screen,
487 WINCONSOLE.save_size,
489 &WINCONSOLE.save_region)) {
492 T((" error %#lx", (unsigned long) GetLastError()));
493 FreeAndNull(WINCONSOLE.save_screen);
501 _nc_console_get_SBI(void)
504 if (GetConsoleScreenBufferInfo(WINCONSOLE.hdl, &(WINCONSOLE.SBI))) {
505 T(("GetConsoleScreenBufferInfo"));
506 T(("... buffer(X:%d Y:%d)",
507 WINCONSOLE.SBI.dwSize.X,
508 WINCONSOLE.SBI.dwSize.Y));
509 T(("... window(X:%d Y:%d)",
510 WINCONSOLE.SBI.dwMaximumWindowSize.X,
511 WINCONSOLE.SBI.dwMaximumWindowSize.Y));
512 T(("... cursor(X:%d Y:%d)",
513 WINCONSOLE.SBI.dwCursorPosition.X,
514 WINCONSOLE.SBI.dwCursorPosition.Y));
515 T(("... display(Top:%d Bottom:%d Left:%d Right:%d)",
516 WINCONSOLE.SBI.srWindow.Top,
517 WINCONSOLE.SBI.srWindow.Bottom,
518 WINCONSOLE.SBI.srWindow.Left,
519 WINCONSOLE.SBI.srWindow.Right));
520 if (WINCONSOLE.buffered) {
521 WINCONSOLE.origin.X = 0;
522 WINCONSOLE.origin.Y = 0;
524 WINCONSOLE.origin.X = WINCONSOLE.SBI.srWindow.Left;
525 WINCONSOLE.origin.Y = WINCONSOLE.SBI.srWindow.Top;
529 T(("GetConsoleScreenBufferInfo ERR"));
538 * In "normal" mode, reset the buffer- and window-sizes back to their original values.
541 _nc_console_set_scrollback(bool normal, CONSOLE_SCREEN_BUFFER_INFO * info)
545 bool changed = FALSE;
547 T((T_CALLED("lib_win32con::_nc_console_set_scrollback(%s)"),
552 T(("... SBI.srWindow %d,%d .. %d,%d",
555 info->srWindow.Bottom,
556 info->srWindow.Right));
557 T(("... SBI.dwSize %dx%d",
562 rect = info->srWindow;
563 coord = info->dwSize;
564 if (memcmp(info, &WINCONSOLE.SBI, sizeof(*info)) != 0) {
566 WINCONSOLE.SBI = *info;
569 int high = info->srWindow.Bottom - info->srWindow.Top + 1;
570 int wide = info->srWindow.Right - info->srWindow.Left + 1;
572 if (high < MIN_HIGH) {
573 T(("... height %d < %d", high, MIN_HIGH));
577 if (wide < MIN_WIDE) {
578 T(("... width %d < %d", wide, MIN_WIDE));
585 rect.Right = (SHORT) (wide - 1);
586 rect.Bottom = (SHORT) (high - 1);
588 coord.X = (SHORT) wide;
589 coord.Y = (SHORT) high;
591 if (info->dwSize.Y != high ||
592 info->dwSize.X != wide ||
593 info->srWindow.Top != 0 ||
594 info->srWindow.Left != 0) {
601 T(("... coord %d,%d", coord.Y, coord.X));
602 T(("... rect %d,%d - %d,%d",
604 rect.Bottom, rect.Right));
605 SetConsoleScreenBufferSize(WINCONSOLE.hdl, coord); /* dwSize */
606 SetConsoleWindowInfo(WINCONSOLE.hdl, TRUE, &rect); /* srWindow */
607 _nc_console_get_SBI();
613 tdiff(FILETIME fstart, FILETIME fend)
615 ULARGE_INTEGER ustart;
619 ustart.LowPart = fstart.dwLowDateTime;
620 ustart.HighPart = fstart.dwHighDateTime;
621 uend.LowPart = fend.dwLowDateTime;
622 uend.HighPart = fend.dwHighDateTime;
624 diff = (uend.QuadPart - ustart.QuadPart) / 10000;
629 Adjust(int milliseconds, int diff)
631 if (milliseconds != INFINITY) {
632 milliseconds -= diff;
633 if (milliseconds < 0)
639 #define BUTTON_MASK (FROM_LEFT_1ST_BUTTON_PRESSED | \
640 FROM_LEFT_2ND_BUTTON_PRESSED | \
641 FROM_LEFT_3RD_BUTTON_PRESSED | \
642 FROM_LEFT_4TH_BUTTON_PRESSED | \
643 RIGHTMOST_BUTTON_PRESSED)
646 decode_mouse(SCREEN *sp, int mask)
651 assert(sp && console_initialized);
653 if (mask & FROM_LEFT_1ST_BUTTON_PRESSED)
654 result |= BUTTON1_PRESSED;
655 if (mask & FROM_LEFT_2ND_BUTTON_PRESSED)
656 result |= BUTTON2_PRESSED;
657 if (mask & FROM_LEFT_3RD_BUTTON_PRESSED)
658 result |= BUTTON3_PRESSED;
659 if (mask & FROM_LEFT_4TH_BUTTON_PRESSED)
660 result |= BUTTON4_PRESSED;
662 if (mask & RIGHTMOST_BUTTON_PRESSED) {
663 switch (WINCONSOLE.numButtons) {
665 result |= BUTTON1_PRESSED;
668 result |= BUTTON2_PRESSED;
671 result |= BUTTON3_PRESSED;
674 result |= BUTTON4_PRESSED;
682 #define AdjustY() (WINCONSOLE.buffered ? 0 : (int) WINCONSOLE.SBI.srWindow.Top)
685 handle_mouse(SCREEN *sp, MOUSE_EVENT_RECORD mer)
692 sp->_drv_mouse_old_buttons = sp->_drv_mouse_new_buttons;
693 sp->_drv_mouse_new_buttons = mer.dwButtonState & BUTTON_MASK;
696 * We're only interested if the button is pressed or released.
697 * FIXME: implement continuous event-tracking.
699 if (sp->_drv_mouse_new_buttons != sp->_drv_mouse_old_buttons) {
700 memset(&work, 0, sizeof(work));
702 if (sp->_drv_mouse_new_buttons) {
704 (mmask_t) decode_mouse(sp,
705 sp->_drv_mouse_new_buttons);
707 /* cf: BUTTON_PRESSED, BUTTON_RELEASED */
709 (mmask_t) (decode_mouse(sp,
710 sp->_drv_mouse_old_buttons)
715 work.x = mer.dwMousePosition.X;
716 work.y = mer.dwMousePosition.Y - AdjustY();
718 sp->_drv_mouse_fifo[sp->_drv_mouse_tail] = work;
719 sp->_drv_mouse_tail += 1;
725 rkeycompare(const void *el1, const void *el2)
727 WORD key1 = (LOWORD((*((const LONG *) el1)))) & 0x7fff;
728 WORD key2 = (LOWORD((*((const LONG *) el2)))) & 0x7fff;
730 return ((key1 < key2) ? -1 : ((key1 == key2) ? 0 : 1));
735 keycompare(const void *el1, const void *el2)
737 WORD key1 = HIWORD((*((const LONG *) el1)));
738 WORD key2 = HIWORD((*((const LONG *) el2)));
740 return ((key1 < key2) ? -1 : ((key1 == key2) ? 0 : 1));
748 if (!WINCONSOLE.isTermInfoConsole) {
751 LONG key = GenMap(vKey, 0);
755 (size_t) (N_INI + FKEYS),
759 key = *((LONG *) res);
761 code = (int) (nKey & 0x7fff);
774 if (!WINCONSOLE.isTermInfoConsole) {
777 LONG key = GenMap(vKey, 0);
781 (size_t) (N_INI + FKEYS),
785 key = *((LONG *) res);
787 code = (int) (nKey & 0x7fff);
796 _nc_console_keyok(int keycode, int flag)
802 LONG key = GenMap(0, (WORD) keycode);
804 T((T_CALLED("lib_win32con::_nc_console_keyok(%d, %d)"), keycode, flag));
808 (size_t) (N_INI + FKEYS),
812 key = *((LONG *) res);
814 nKey = (LOWORD(key)) & 0x7fff;
817 *(LONG *) res = GenMap(vKey, nKey);
823 _nc_console_keyExist(int keycode)
828 LONG key = GenMap(0, (WORD) keycode);
830 T((T_CALLED("lib_win32con::_nc_console_keyExist(%d)"), keycode));
833 (size_t) (N_INI + FKEYS),
837 key = *((LONG *) res);
839 if (!(nKey & 0x8000))
852 EVENTLIST_2nd(_nc_eventlist * evl))
854 INPUT_RECORD inp_rec;
856 DWORD nRead = 0, rc = (DWORD) (-1);
861 bool isNoDelay = (milliseconds == 0);
863 #ifdef NCURSES_WGETCH_EVENTS
864 (void) evl; /* TODO: implement wgetch-events */
867 #define IGNORE_CTRL_KEYS (SHIFT_PRESSED|LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED| \
868 LEFT_CTRL_PRESSED|RIGHT_CTRL_PRESSED)
869 #define CONSUME() ReadConsoleInput(hdl, &inp_rec, 1, &nRead)
873 TR(TRACE_IEVENT, ("start twait: hdl=%p, %d milliseconds, mode: %d",
874 hdl, milliseconds, mode));
876 if (milliseconds < 0)
877 milliseconds = INFINITY;
879 memset(&inp_rec, 0, sizeof(inp_rec));
883 GetSystemTimeAsFileTime(&fstart);
884 rc = WaitForSingleObject(hdl, (DWORD) milliseconds);
885 GetSystemTimeAsFileTime(&fend);
886 diff = (int) tdiff(fstart, fend);
887 milliseconds = Adjust(milliseconds, diff);
892 if (isNoDelay || (rc == WAIT_OBJECT_0)) {
895 b = GetNumberOfConsoleInputEvents(hdl, &nRead);
897 T(("twait:err GetNumberOfConsoleInputEvents"));
899 if (isNoDelay && b) {
900 T(("twait: Events Available: %ld", nRead));
906 INPUT_RECORD* pInpRec =
907 TypeAlloca(INPUT_RECORD, nRead);
908 if (pInpRec != NULL) {
911 memset(pInpRec, 0, sizeof(INPUT_RECORD)*nRead);
912 f = PeekConsoleInput(hdl, pInpRec, nRead, &n);
914 for(i = 0; i < n; i++) {
915 if (pInpRec[i].EventType==KEY_EVENT) {
916 if(pInpRec[i].Event.KeyEvent.bKeyDown) {
918 (pInpRec[i].Event.KeyEvent.dwControlKeyState &
928 T(("twait:err PeekConsoleInput"));
933 T(("twait:err could not alloca input records"));
937 if (b && nRead > 0) {
938 b = PeekConsoleInput(hdl, &inp_rec, 1, &nRead);
940 T(("twait:err PeekConsoleInput"));
942 if (b && nRead > 0) {
943 switch (inp_rec.EventType) {
945 if (mode & TW_INPUT) {
947 inp_rec.Event.KeyEvent.wVirtualKeyCode;
949 inp_rec.Event.KeyEvent.uChar.AsciiChar;
950 T(("twait:event KEY_EVENT"));
951 T(("twait vk=%d, ch=%d, keydown=%d",
952 vk, ch, inp_rec.Event.KeyEvent.bKeyDown));
953 if (inp_rec.Event.KeyEvent.bKeyDown) {
954 T(("twait:event KeyDown"));
955 if (!WINCONSOLE.isTermInfoConsole &&
957 int nKey = MapKey(vk);
971 T(("twait:event MOUSE_EVENT"));
973 (inp_rec.Event.MouseEvent.dwButtonState
974 & BUTTON_MASK)) == 0) {
976 } else if (mode & TW_MOUSE) {
981 /* e.g., FOCUS_EVENT */
983 T(("twait:event Tyoe %d", inp_rec.EventType));
985 _nc_console_selectActiveHandle();
993 if (rc != WAIT_TIMEOUT) {
1004 TR(TRACE_IEVENT, ("end twait: returned %d (%lu), remaining time %d msec",
1005 code, GetLastError(), milliseconds));
1008 *timeleft = milliseconds;
1014 _nc_console_testmouse(
1018 EVENTLIST_2nd(_nc_eventlist * evl))
1024 if (sp->_drv_mouse_head < sp->_drv_mouse_tail) {
1027 rc = _nc_console_twait(sp,
1032 EVENTLIST_2nd(evl));
1044 INPUT_RECORD inp_rec;
1052 memset(&inp_rec, 0, sizeof(inp_rec));
1054 T((T_CALLED("lib_win32con::_nc_console_read(%p)"), sp));
1056 while ((b = ReadConsoleInput(hdl, &inp_rec, 1, &nRead))) {
1057 if (b && nRead > 0) {
1060 rc = rc + (int) nRead;
1061 if (inp_rec.EventType == KEY_EVENT) {
1062 if (!inp_rec.Event.KeyEvent.bKeyDown)
1064 *buf = (int) inp_rec.Event.KeyEvent.uChar.AsciiChar;
1065 vk = inp_rec.Event.KeyEvent.wVirtualKeyCode;
1067 * There are 24 virtual function-keys, and typically
1068 * 12 function-keys on a keyboard. Use the shift-modifier
1069 * to provide the remaining 12 keys.
1071 if (vk >= VK_F1 && vk <= VK_F12) {
1072 if (inp_rec.Event.KeyEvent.dwControlKeyState &
1074 vk = (WORD) (vk + 12);
1078 int key = MapKey(vk);
1081 if (sp->_keypad_on) {
1089 } else if (inp_rec.EventType == MOUSE_EVENT) {
1090 if (handle_mouse(sp,
1091 inp_rec.Event.MouseEvent)) {
1102 /* Our replacement for the systems _isatty to include also
1103 a test for mintty. This is called from the NC_ISATTY macro
1104 defined in curses.priv.h
1108 - 1 : A Windows character device detected by _isatty
1109 - 2 : A future implementation may return 2 for mintty
1112 _nc_console_isatty(int fd)
1115 T((T_CALLED("lib_win32con::_nc_console_isatty(%d"), fd));
1119 #ifdef _NC_CHECK_MINTTY
1121 if (_nc_console_checkmintty(fd, NULL)) {
1123 fprintf(stderr, "ncurses on Windows must run in a Windows console.\n");
1124 fprintf(stderr, "On newer versions of Windows, the calling program should create a PTY-like.\n");
1125 fprintf(stderr, "device using the CreatePseudoConsole Windows API call.\n");
1133 NCURSES_EXPORT(bool)
1134 _nc_console_checkinit(bool initFlag, bool assumeTermInfo)
1138 T((T_CALLED("lib_win32con::_nc_console_checkinit(initFlag=%d, assumeTermInfo=%d)"),
1139 initFlag, assumeTermInfo));
1142 res = console_initialized;
1144 /* initialize once, or not at all */
1145 if (!console_initialized) {
1149 BOOL buffered = FALSE;
1153 WINCONSOLE.isTermInfoConsole = assumeTermInfo;
1155 WINCONSOLE.map = (LPDWORD)malloc(sizeof(DWORD)*MAPSIZE);
1156 WINCONSOLE.rmap = (LPDWORD)malloc(sizeof(DWORD)*MAPSIZE);
1157 WINCONSOLE.ansi_map = (LPDWORD)malloc(sizeof(DWORD)*MAPSIZE);
1159 for (i = 0; i < (N_INI + FKEYS); i++) {
1161 WINCONSOLE.rmap[i] = WINCONSOLE.map[i] =
1163 WINCONSOLE.ansi_map[i] = (DWORD) ansi_keys[i];
1165 WINCONSOLE.rmap[i] = WINCONSOLE.map[i] =
1166 (DWORD) GenMap((VK_F1 + (i - N_INI)),
1167 (KEY_F(1) + (i - N_INI)));
1168 WINCONSOLE.ansi_map[i] =
1169 (DWORD) GenMap((VK_F1 + (i - N_INI)),
1170 (';' + (i - N_INI)));
1173 qsort(WINCONSOLE.ansi_map,
1177 qsort(WINCONSOLE.map,
1181 qsort(WINCONSOLE.rmap,
1186 if (GetNumberOfConsoleMouseButtons(&num_buttons)) {
1187 WINCONSOLE.numButtons = (int) num_buttons;
1189 WINCONSOLE.numButtons = 1;
1192 a = _nc_console_MapColor(true, COLOR_WHITE) |
1193 _nc_console_MapColor(false, COLOR_BLACK);
1194 for (i = 0; i < CON_NUMPAIRS; i++)
1195 WINCONSOLE.pairs[i] = a;
1197 WINCONSOLE.inp = GetStdHandle(STD_INPUT_HANDLE);
1198 WINCONSOLE.out = GetStdHandle(STD_OUTPUT_HANDLE);
1199 WINCONSOLE.hdl = WINCONSOLE.out;
1201 GetConsoleMode(WINCONSOLE.inp, &WINCONSOLE.originalMode.dwFlagIn);
1202 GetConsoleMode(WINCONSOLE.out, &WINCONSOLE.originalMode.dwFlagOut);
1204 if (!WINCONSOLE.isTermInfoConsole) {
1208 b = AttachConsole(ATTACH_PARENT_PROCESS);
1210 if (getenv("NCGDB") || getenv("NCURSES_CONSOLE2")) {
1211 T(("... will not buffer console"));
1213 T(("... creating console buffer"));
1215 CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
1218 CONSOLE_TEXTMODE_BUFFER,
1224 /* We set binary I/O even when using the console
1225 driver to cover the situation, that the
1226 TERM variable is set to #win32con, but actually
1227 Windows supports virtual terminal processing.
1228 So if terminfo functions are used in this setup,
1229 they actually may work.
1231 _setmode(fileno(stdin), _O_BINARY);
1232 _setmode(fileno(stdout), _O_BINARY);
1234 if (WINCONSOLE.hdl != INVALID_HANDLE_VALUE) {
1235 WINCONSOLE.buffered = buffered;
1236 _nc_console_get_SBI();
1237 WINCONSOLE.save_SBI = WINCONSOLE.SBI;
1239 save_original_screen();
1240 _nc_console_set_scrollback(FALSE, &WINCONSOLE.SBI);
1242 GetConsoleCursorInfo(WINCONSOLE.hdl, &WINCONSOLE.save_CI);
1243 T(("... initial cursor is %svisible, %d%%",
1244 (WINCONSOLE.save_CI.bVisible ? "" : "not-"),
1245 (int) WINCONSOLE.save_CI.dwSize));
1248 WINCONSOLE.initialized = TRUE;
1249 console_initialized = TRUE;
1251 res = (WINCONSOLE.hdl != INVALID_HANDLE_VALUE);
1256 #endif // _NC_WINDOWS