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.12 2023/02/26 19:55:10 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)
383 * Attempt to save the screen contents. PDCurses does this if
384 * PDC_RESTORE_SCREEN is set, giving the same visual appearance on
385 * restoration as if the library had allocated a console buffer. MSDN
386 * says that the data which can be read is limited to 64Kb (and may be
390 save_original_screen(void)
394 WINCONSOLE.save_region.Top = 0;
395 WINCONSOLE.save_region.Left = 0;
396 WINCONSOLE.save_region.Bottom = (SHORT) (WINCONSOLE.SBI.dwSize.Y - 1);
397 WINCONSOLE.save_region.Right = (SHORT) (WINCONSOLE.SBI.dwSize.X - 1);
399 if (read_screen_data()) {
403 WINCONSOLE.save_region.Top = WINCONSOLE.SBI.srWindow.Top;
404 WINCONSOLE.save_region.Left = WINCONSOLE.SBI.srWindow.Left;
405 WINCONSOLE.save_region.Bottom = WINCONSOLE.SBI.srWindow.Bottom;
406 WINCONSOLE.save_region.Right = WINCONSOLE.SBI.srWindow.Right;
408 WINCONSOLE.window_only = TRUE;
410 if (read_screen_data()) {
415 T(("... save original screen contents %s", result ? "ok" : "err"));
420 restore_original_screen(void)
424 SMALL_RECT save_region = WINCONSOLE.save_region;
426 T(("... restoring %s",
427 WINCONSOLE.window_only ? "window" : "entire buffer"));
429 bufferCoord.X = (SHORT) (WINCONSOLE.window_only ?
430 WINCONSOLE.SBI.srWindow.Left : 0);
431 bufferCoord.Y = (SHORT) (WINCONSOLE.window_only ?
432 WINCONSOLE.SBI.srWindow.Top : 0);
434 if (write_screen(WINCONSOLE.hdl,
435 WINCONSOLE.save_screen,
436 WINCONSOLE.save_size,
440 mvcur(-1, -1, LINES - 2, 0);
441 T(("... restore original screen contents ok %dx%d (%d,%d - %d,%d)",
442 WINCONSOLE.save_size.Y,
443 WINCONSOLE.save_size.X,
449 T(("... restore original screen contents err"));
455 read_screen_data(void)
461 WINCONSOLE.save_size.X = (SHORT) (WINCONSOLE.save_region.Right
462 - WINCONSOLE.save_region.Left + 1);
463 WINCONSOLE.save_size.Y = (SHORT) (WINCONSOLE.save_region.Bottom
464 - WINCONSOLE.save_region.Top + 1);
466 want = (size_t) (WINCONSOLE.save_size.X * WINCONSOLE.save_size.Y);
468 if ((WINCONSOLE.save_screen = malloc(want * sizeof(CHAR_INFO))) != 0) {
469 bufferCoord.X = (SHORT) (WINCONSOLE.window_only ?
470 WINCONSOLE.SBI.srWindow.Left : 0);
471 bufferCoord.Y = (SHORT) (WINCONSOLE.window_only ?
472 WINCONSOLE.SBI.srWindow.Top : 0);
474 T(("... reading console %s %dx%d into %d,%d - %d,%d at %d,%d",
475 WINCONSOLE.window_only ? "window" : "buffer",
476 WINCONSOLE.save_size.Y, WINCONSOLE.save_size.X,
477 WINCONSOLE.save_region.Top,
478 WINCONSOLE.save_region.Left,
479 WINCONSOLE.save_region.Bottom,
480 WINCONSOLE.save_region.Right,
484 if (read_screen(WINCONSOLE.hdl,
485 WINCONSOLE.save_screen,
486 WINCONSOLE.save_size,
488 &WINCONSOLE.save_region)) {
491 T((" error %#lx", (unsigned long) GetLastError()));
492 FreeAndNull(WINCONSOLE.save_screen);
500 _nc_console_get_SBI(void)
503 if (GetConsoleScreenBufferInfo(WINCONSOLE.hdl, &(WINCONSOLE.SBI))) {
504 T(("GetConsoleScreenBufferInfo"));
505 T(("... buffer(X:%d Y:%d)",
506 WINCONSOLE.SBI.dwSize.X,
507 WINCONSOLE.SBI.dwSize.Y));
508 T(("... window(X:%d Y:%d)",
509 WINCONSOLE.SBI.dwMaximumWindowSize.X,
510 WINCONSOLE.SBI.dwMaximumWindowSize.Y));
511 T(("... cursor(X:%d Y:%d)",
512 WINCONSOLE.SBI.dwCursorPosition.X,
513 WINCONSOLE.SBI.dwCursorPosition.Y));
514 T(("... display(Top:%d Bottom:%d Left:%d Right:%d)",
515 WINCONSOLE.SBI.srWindow.Top,
516 WINCONSOLE.SBI.srWindow.Bottom,
517 WINCONSOLE.SBI.srWindow.Left,
518 WINCONSOLE.SBI.srWindow.Right));
519 if (WINCONSOLE.buffered) {
520 WINCONSOLE.origin.X = 0;
521 WINCONSOLE.origin.Y = 0;
523 WINCONSOLE.origin.X = WINCONSOLE.SBI.srWindow.Left;
524 WINCONSOLE.origin.Y = WINCONSOLE.SBI.srWindow.Top;
528 T(("GetConsoleScreenBufferInfo ERR"));
537 * In "normal" mode, reset the buffer- and window-sizes back to their original values.
540 _nc_console_set_scrollback(bool normal, CONSOLE_SCREEN_BUFFER_INFO * info)
544 bool changed = FALSE;
546 T((T_CALLED("lib_win32con::_nc_console_set_scrollback(%s)"),
551 T(("... SBI.srWindow %d,%d .. %d,%d",
554 info->srWindow.Bottom,
555 info->srWindow.Right));
556 T(("... SBI.dwSize %dx%d",
561 rect = info->srWindow;
562 coord = info->dwSize;
563 if (memcmp(info, &WINCONSOLE.SBI, sizeof(*info)) != 0) {
565 WINCONSOLE.SBI = *info;
568 int high = info->srWindow.Bottom - info->srWindow.Top + 1;
569 int wide = info->srWindow.Right - info->srWindow.Left + 1;
571 if (high < MIN_HIGH) {
572 T(("... height %d < %d", high, MIN_HIGH));
576 if (wide < MIN_WIDE) {
577 T(("... width %d < %d", wide, MIN_WIDE));
584 rect.Right = (SHORT) (wide - 1);
585 rect.Bottom = (SHORT) (high - 1);
587 coord.X = (SHORT) wide;
588 coord.Y = (SHORT) high;
590 if (info->dwSize.Y != high ||
591 info->dwSize.X != wide ||
592 info->srWindow.Top != 0 ||
593 info->srWindow.Left != 0) {
600 T(("... coord %d,%d", coord.Y, coord.X));
601 T(("... rect %d,%d - %d,%d",
603 rect.Bottom, rect.Right));
604 SetConsoleScreenBufferSize(WINCONSOLE.hdl, coord); /* dwSize */
605 SetConsoleWindowInfo(WINCONSOLE.hdl, TRUE, &rect); /* srWindow */
606 _nc_console_get_SBI();
612 tdiff(FILETIME fstart, FILETIME fend)
614 ULARGE_INTEGER ustart;
618 ustart.LowPart = fstart.dwLowDateTime;
619 ustart.HighPart = fstart.dwHighDateTime;
620 uend.LowPart = fend.dwLowDateTime;
621 uend.HighPart = fend.dwHighDateTime;
623 diff = (uend.QuadPart - ustart.QuadPart) / 10000;
628 Adjust(int milliseconds, int diff)
630 if (milliseconds != INFINITY) {
631 milliseconds -= diff;
632 if (milliseconds < 0)
638 #define BUTTON_MASK (FROM_LEFT_1ST_BUTTON_PRESSED | \
639 FROM_LEFT_2ND_BUTTON_PRESSED | \
640 FROM_LEFT_3RD_BUTTON_PRESSED | \
641 FROM_LEFT_4TH_BUTTON_PRESSED | \
642 RIGHTMOST_BUTTON_PRESSED)
645 decode_mouse(SCREEN *sp, int mask)
650 assert(sp && console_initialized);
652 if (mask & FROM_LEFT_1ST_BUTTON_PRESSED)
653 result |= BUTTON1_PRESSED;
654 if (mask & FROM_LEFT_2ND_BUTTON_PRESSED)
655 result |= BUTTON2_PRESSED;
656 if (mask & FROM_LEFT_3RD_BUTTON_PRESSED)
657 result |= BUTTON3_PRESSED;
658 if (mask & FROM_LEFT_4TH_BUTTON_PRESSED)
659 result |= BUTTON4_PRESSED;
661 if (mask & RIGHTMOST_BUTTON_PRESSED) {
662 switch (WINCONSOLE.numButtons) {
664 result |= BUTTON1_PRESSED;
667 result |= BUTTON2_PRESSED;
670 result |= BUTTON3_PRESSED;
673 result |= BUTTON4_PRESSED;
681 #define AdjustY() (WINCONSOLE.buffered ? 0 : (int) WINCONSOLE.SBI.srWindow.Top)
684 handle_mouse(SCREEN *sp, MOUSE_EVENT_RECORD mer)
691 sp->_drv_mouse_old_buttons = sp->_drv_mouse_new_buttons;
692 sp->_drv_mouse_new_buttons = mer.dwButtonState & BUTTON_MASK;
695 * We're only interested if the button is pressed or released.
696 * FIXME: implement continuous event-tracking.
698 if (sp->_drv_mouse_new_buttons != sp->_drv_mouse_old_buttons) {
699 memset(&work, 0, sizeof(work));
701 if (sp->_drv_mouse_new_buttons) {
703 (mmask_t) decode_mouse(sp,
704 sp->_drv_mouse_new_buttons);
706 /* cf: BUTTON_PRESSED, BUTTON_RELEASED */
708 (mmask_t) (decode_mouse(sp,
709 sp->_drv_mouse_old_buttons)
714 work.x = mer.dwMousePosition.X;
715 work.y = mer.dwMousePosition.Y - AdjustY();
717 sp->_drv_mouse_fifo[sp->_drv_mouse_tail] = work;
718 sp->_drv_mouse_tail += 1;
724 rkeycompare(const void *el1, const void *el2)
726 WORD key1 = (LOWORD((*((const LONG *) el1)))) & 0x7fff;
727 WORD key2 = (LOWORD((*((const LONG *) el2)))) & 0x7fff;
729 return ((key1 < key2) ? -1 : ((key1 == key2) ? 0 : 1));
733 keycompare(const void *el1, const void *el2)
735 WORD key1 = HIWORD((*((const LONG *) el1)));
736 WORD key2 = HIWORD((*((const LONG *) el2)));
738 return ((key1 < key2) ? -1 : ((key1 == key2) ? 0 : 1));
746 if (!WINCONSOLE.isTermInfoConsole) {
749 LONG key = GenMap(vKey, 0);
753 (size_t) (N_INI + FKEYS),
757 key = *((LONG *) res);
759 code = (int) (nKey & 0x7fff);
772 if (!WINCONSOLE.isTermInfoConsole) {
775 LONG key = GenMap(vKey, 0);
779 (size_t) (N_INI + FKEYS),
783 key = *((LONG *) res);
785 code = (int) (nKey & 0x7fff);
794 _nc_console_keyok(int keycode, int flag)
800 LONG key = GenMap(0, (WORD) keycode);
802 T((T_CALLED("lib_win32con::_nc_console_keyok(%d, %d)"), keycode, flag));
806 (size_t) (N_INI + FKEYS),
810 key = *((LONG *) res);
812 nKey = (LOWORD(key)) & 0x7fff;
815 *(LONG *) res = GenMap(vKey, nKey);
821 _nc_console_keyExist(int keycode)
826 LONG key = GenMap(0, (WORD) keycode);
828 T((T_CALLED("lib_win32con::_nc_console_keyExist(%d)"), keycode));
831 (size_t) (N_INI + FKEYS),
835 key = *((LONG *) res);
837 if (!(nKey & 0x8000))
850 EVENTLIST_2nd(_nc_eventlist * evl))
852 INPUT_RECORD inp_rec;
854 DWORD nRead = 0, rc = (DWORD) (-1);
859 bool isNoDelay = (milliseconds == 0);
861 #ifdef NCURSES_WGETCH_EVENTS
862 (void) evl; /* TODO: implement wgetch-events */
865 #define IGNORE_CTRL_KEYS (SHIFT_PRESSED|LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED| \
866 LEFT_CTRL_PRESSED|RIGHT_CTRL_PRESSED)
867 #define CONSUME() ReadConsoleInput(hdl, &inp_rec, 1, &nRead)
871 TR(TRACE_IEVENT, ("start twait: hdl=%p, %d milliseconds, mode: %d",
872 hdl, milliseconds, mode));
874 if (milliseconds < 0)
875 milliseconds = INFINITY;
877 memset(&inp_rec, 0, sizeof(inp_rec));
881 GetSystemTimeAsFileTime(&fstart);
882 rc = WaitForSingleObject(hdl, (DWORD) milliseconds);
883 GetSystemTimeAsFileTime(&fend);
884 diff = (int) tdiff(fstart, fend);
885 milliseconds = Adjust(milliseconds, diff);
886 if (milliseconds < 0)
890 if (isNoDelay || (rc == WAIT_OBJECT_0)) {
893 b = GetNumberOfConsoleInputEvents(hdl, &nRead);
895 T(("twait:err GetNumberOfConsoleInputEvents"));
897 if (isNoDelay && b) {
898 T(("twait: Events Available: %ld", nRead));
904 INPUT_RECORD *pInpRec =
905 TypeAlloca(INPUT_RECORD, nRead);
906 if (pInpRec != NULL) {
909 memset(pInpRec, 0, sizeof(INPUT_RECORD) * nRead);
910 f = PeekConsoleInput(hdl, pInpRec, nRead, &n);
912 for (i = 0; i < n; i++) {
913 if (pInpRec[i].EventType == KEY_EVENT) {
914 if (pInpRec[i].Event.KeyEvent.bKeyDown) {
916 (pInpRec[i].Event.KeyEvent.dwControlKeyState &
926 T(("twait:err PeekConsoleInput"));
931 T(("twait:err could not alloca input records"));
935 if (b && nRead > 0) {
936 b = PeekConsoleInput(hdl, &inp_rec, 1, &nRead);
938 T(("twait:err PeekConsoleInput"));
940 if (b && nRead > 0) {
941 switch (inp_rec.EventType) {
943 if (mode & TW_INPUT) {
945 inp_rec.Event.KeyEvent.wVirtualKeyCode;
947 inp_rec.Event.KeyEvent.uChar.AsciiChar;
948 T(("twait:event KEY_EVENT"));
949 T(("twait vk=%d, ch=%d, keydown=%d",
950 vk, ch, inp_rec.Event.KeyEvent.bKeyDown));
951 if (inp_rec.Event.KeyEvent.bKeyDown) {
952 T(("twait:event KeyDown"));
953 if (!WINCONSOLE.isTermInfoConsole &&
955 int nKey = MapKey(vk);
969 T(("twait:event MOUSE_EVENT"));
971 (inp_rec.Event.MouseEvent.dwButtonState
972 & BUTTON_MASK)) == 0) {
974 } else if (mode & TW_MOUSE) {
979 /* e.g., FOCUS_EVENT */
981 T(("twait:event Tyoe %d", inp_rec.EventType));
983 _nc_console_selectActiveHandle();
991 if (rc != WAIT_TIMEOUT) {
1002 TR(TRACE_IEVENT, ("end twait: returned %d (%lu), remaining time %d msec",
1003 code, GetLastError(), milliseconds));
1006 *timeleft = milliseconds;
1012 _nc_console_testmouse(
1016 EVENTLIST_2nd(_nc_eventlist * evl))
1022 if (sp->_drv_mouse_head < sp->_drv_mouse_tail) {
1025 rc = _nc_console_twait(sp,
1030 EVENTLIST_2nd(evl));
1042 INPUT_RECORD inp_rec;
1050 memset(&inp_rec, 0, sizeof(inp_rec));
1052 T((T_CALLED("lib_win32con::_nc_console_read(%p)"), sp));
1054 while ((b = ReadConsoleInput(hdl, &inp_rec, 1, &nRead))) {
1055 if (b && nRead > 0) {
1058 rc = rc + (int) nRead;
1059 if (inp_rec.EventType == KEY_EVENT) {
1060 if (!inp_rec.Event.KeyEvent.bKeyDown)
1062 *buf = (int) inp_rec.Event.KeyEvent.uChar.AsciiChar;
1063 vk = inp_rec.Event.KeyEvent.wVirtualKeyCode;
1065 * There are 24 virtual function-keys, and typically
1066 * 12 function-keys on a keyboard. Use the shift-modifier
1067 * to provide the remaining 12 keys.
1069 if (vk >= VK_F1 && vk <= VK_F12) {
1070 if (inp_rec.Event.KeyEvent.dwControlKeyState &
1072 vk = (WORD) (vk + 12);
1076 int key = MapKey(vk);
1079 if (sp->_keypad_on) {
1087 } else if (inp_rec.EventType == MOUSE_EVENT) {
1088 if (handle_mouse(sp,
1089 inp_rec.Event.MouseEvent)) {
1100 /* Our replacement for the systems _isatty to include also
1101 a test for mintty. This is called from the NC_ISATTY macro
1102 defined in curses.priv.h
1106 - 1 : A Windows character device detected by _isatty
1107 - 2 : A future implementation may return 2 for mintty
1110 _nc_console_isatty(int fd)
1113 T((T_CALLED("lib_win32con::_nc_console_isatty(%d"), fd));
1117 #ifdef _NC_CHECK_MINTTY
1119 if (_nc_console_checkmintty(fd, NULL)) {
1122 "ncurses on Windows must run in a Windows console.\n"
1123 "On newer versions of Windows, the calling program should create a PTY-like.\n"
1124 "device using the CreatePseudoConsole Windows API call.\n");
1132 NCURSES_EXPORT(bool)
1133 _nc_console_checkinit(bool initFlag, bool assumeTermInfo)
1137 T((T_CALLED("lib_win32con::_nc_console_checkinit(initFlag=%d, assumeTermInfo=%d)"),
1138 initFlag, assumeTermInfo));
1141 res = console_initialized;
1143 /* initialize once, or not at all */
1144 if (!console_initialized) {
1148 BOOL buffered = FALSE;
1152 WINCONSOLE.isTermInfoConsole = assumeTermInfo;
1154 WINCONSOLE.map = (LPDWORD) malloc(sizeof(DWORD) * MAPSIZE);
1155 WINCONSOLE.rmap = (LPDWORD) malloc(sizeof(DWORD) * MAPSIZE);
1156 WINCONSOLE.ansi_map = (LPDWORD) malloc(sizeof(DWORD) * MAPSIZE);
1158 for (i = 0; i < (N_INI + FKEYS); i++) {
1160 WINCONSOLE.rmap[i] = WINCONSOLE.map[i] =
1162 WINCONSOLE.ansi_map[i] = (DWORD) ansi_keys[i];
1164 WINCONSOLE.rmap[i] = WINCONSOLE.map[i] =
1165 (DWORD) GenMap((VK_F1 + (i - N_INI)),
1166 (KEY_F(1) + (i - N_INI)));
1167 WINCONSOLE.ansi_map[i] =
1168 (DWORD) GenMap((VK_F1 + (i - N_INI)),
1169 (';' + (i - N_INI)));
1172 qsort(WINCONSOLE.ansi_map,
1176 qsort(WINCONSOLE.map,
1180 qsort(WINCONSOLE.rmap,
1185 if (GetNumberOfConsoleMouseButtons(&num_buttons)) {
1186 WINCONSOLE.numButtons = (int) num_buttons;
1188 WINCONSOLE.numButtons = 1;
1191 a = _nc_console_MapColor(true, COLOR_WHITE) |
1192 _nc_console_MapColor(false, COLOR_BLACK);
1193 for (i = 0; i < CON_NUMPAIRS; i++)
1194 WINCONSOLE.pairs[i] = a;
1196 #define SaveConsoleMode(handle, value) \
1197 GetConsoleMode(WINCONSOLE.handle, &WINCONSOLE.originalMode.value)
1199 if (WINCONSOLE.isTermInfoConsole) {
1200 WINCONSOLE.inp = GetStdHandle(STD_INPUT_HANDLE);
1201 WINCONSOLE.out = GetStdHandle(STD_OUTPUT_HANDLE);
1202 WINCONSOLE.hdl = WINCONSOLE.out;
1204 SaveConsoleMode(inp, dwFlagIn);
1205 SaveConsoleMode(out, dwFlagOut);
1211 b = AttachConsole(ATTACH_PARENT_PROCESS);
1213 WINCONSOLE.inp = GetDirectHandle("CONIN$", FILE_SHARE_READ);
1214 WINCONSOLE.out = GetDirectHandle("CONOUT$", FILE_SHARE_WRITE);
1216 SaveConsoleMode(inp, dwFlagIn);
1217 SaveConsoleMode(out, dwFlagOut);
1219 if (getenv("NCGDB") || getenv("NCURSES_CONSOLE2")) {
1220 WINCONSOLE.hdl = WINCONSOLE.out;
1221 T(("... will not buffer console"));
1223 T(("... creating console buffer"));
1225 CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
1226 FILE_SHARE_READ | FILE_SHARE_WRITE,
1228 CONSOLE_TEXTMODE_BUFFER,
1234 /* We set binary I/O even when using the console
1235 driver to cover the situation, that the
1236 TERM variable is set to #win32con, but actually
1237 Windows supports virtual terminal processing.
1238 So if terminfo functions are used in this setup,
1239 they actually may work.
1241 _setmode(fileno(stdin), _O_BINARY);
1242 _setmode(fileno(stdout), _O_BINARY);
1244 if (WINCONSOLE.hdl != INVALID_HANDLE_VALUE) {
1245 WINCONSOLE.buffered = buffered;
1246 _nc_console_get_SBI();
1247 WINCONSOLE.save_SBI = WINCONSOLE.SBI;
1249 save_original_screen();
1250 _nc_console_set_scrollback(FALSE, &WINCONSOLE.SBI);
1252 GetConsoleCursorInfo(WINCONSOLE.hdl, &WINCONSOLE.save_CI);
1253 T(("... initial cursor is %svisible, %d%%",
1254 (WINCONSOLE.save_CI.bVisible ? "" : "not-"),
1255 (int) WINCONSOLE.save_CI.dwSize));
1258 WINCONSOLE.initialized = TRUE;
1259 console_initialized = TRUE;
1261 res = (WINCONSOLE.hdl != INVALID_HANDLE_VALUE);
1266 #endif // _NC_WINDOWS