1 /****************************************************************************
2 * Copyright 2020 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.4 2020/11/14 23:37:16 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's 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) {
128 T((T_CALLED("lib_win32con::_nc_console_vt_supported")));
129 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
130 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
133 T(("GetVersionEx returnedMajor=%ld, Minor=%ld, Build=%ld", osvi.dwMajorVersion,osvi.dwMinorVersion,osvi.dwBuildNumber)); if (osvi.dwMajorVersion >= REQUIRED_MAX_V) {
134 if (osvi.dwMajorVersion == REQUIRED_MAX_V) {
135 if (((osvi.dwMinorVersion == REQUIRED_MIN_V) &&
136 (osvi.dwBuildNumber >= REQUIRED_BUILD)) ||
137 ((osvi.dwMinorVersion > REQUIRED_MIN_V)))
146 _nc_console_size(int* Lines, int* Cols) {
148 if (Lines != NULL && Cols != NULL) {
149 if (WINCONSOLE.buffered) {
150 *Lines = (int) (WINCONSOLE.SBI.dwSize.Y);
151 *Cols = (int) (WINCONSOLE.SBI.dwSize.X);
153 *Lines = (int) (WINCONSOLE.SBI.srWindow.Bottom + 1 -
154 WINCONSOLE.SBI.srWindow.Top);
155 *Cols = (int) (WINCONSOLE.SBI.srWindow.Right + 1 -
156 WINCONSOLE.SBI.srWindow.Left);
161 /* Convert a file descriptor into a HANDLE
162 That's not necessarily a console HANDLE
164 NCURSES_EXPORT(HANDLE)
165 _nc_console_handle(int fd)
167 intptr_t value = _get_osfhandle(fd);
168 return (HANDLE) value;
171 /* Validate that a HANDLE is actually a
175 IsConsoleHandle(HANDLE hdl)
180 T((T_CALLED("lib_win32con::IsConsoleHandle(HANDLE=%p"),hdl));
184 if (!GetConsoleMode(hdl, &dwFlag)) {
185 T(("GetConsoleMode failed"));
193 /* This is used when running in terminfo mode to discover,
194 whether or not the "terminal" is actually a Windows
195 Console. It's the responsibility of the console to deal
196 with the terminal escape sequences that are sent by
200 _nc_console_test(int fd)
203 HANDLE hdl = INVALID_HANDLE_VALUE;
204 T((T_CALLED("lib_win32con::_nc_console_test(%d)"), fd));
205 hdl = _nc_console_handle(fd);
206 code = (int) IsConsoleHandle(hdl);
210 #define OutHandle() ((WINCONSOLE.isTermInfoConsole || WINCONSOLE.progMode) ? WINCONSOLE.hdl : WINCONSOLE.out)
213 _nc_console_selectActiveHandle(void)
215 if (WINCONSOLE.lastOut != WINCONSOLE.hdl) {
216 WINCONSOLE.lastOut = WINCONSOLE.hdl;
217 SetConsoleActiveScreenBuffer(WINCONSOLE.lastOut);
221 NCURSES_EXPORT(HANDLE)
222 _nc_console_fd2handle(int fd) {
223 HANDLE hdl = _nc_console_handle(fd);
224 if (hdl==WINCONSOLE.inp) {
225 T(("lib_win32con:validateHandle %d -> WINCONSOLE.inp", fd));
226 } else if (hdl==WINCONSOLE.hdl) {
227 T(("lib_win32con:validateHandle %d -> WINCONSOLE.hdl", fd));
228 } else if (hdl==WINCONSOLE.out) {
229 T(("lib_win32con:validateHandle %d -> WINCONSOLE.out", fd));
231 T(("lib_win32con:validateHandle %d maps to unknown HANDLE", fd));
232 hdl=INVALID_HANDLE_VALUE;
235 assert(hdl!=INVALID_HANDLE_VALUE);
237 if (hdl != INVALID_HANDLE_VALUE) {
238 if (hdl!=WINCONSOLE.inp && (!WINCONSOLE.isTermInfoConsole && WINCONSOLE.progMode)) {
239 if (hdl==WINCONSOLE.out && hdl!=WINCONSOLE.hdl) {
240 T(("lib_win32con:validateHandle forcing WINCONSOLE.out -> WINCONSOLE.hdl"));
249 _nc_console_setmode(HANDLE hdl, const TTY *arg)
258 #define TRCTTYOUT(flag) TRCTTY.dwFlagOut=flag
259 #define TRCTTYIN(flag) TRCTTY.dwFlagIn=flag
261 #define TRCTTYOUT(flag)
262 #define TRCTTYIN(flag)
264 T(("lib_win32con:_nc_console_setmode %s", _nc_trace_ttymode(arg)));
265 if (hdl==WINCONSOLE.inp) {
266 dwFlag = arg->dwFlagIn | ENABLE_MOUSE_INPUT | VT_FLAG_IN;
267 if (WINCONSOLE.isTermInfoConsole)
268 dwFlag |= (VT_FLAG_IN);
270 dwFlag &= ~(VT_FLAG_IN);
272 SetConsoleMode(hdl, dwFlag);
275 dwFlag = arg->dwFlagOut;
276 if (WINCONSOLE.isTermInfoConsole)
277 dwFlag |= (VT_FLAG_OUT);
279 dwFlag |= (VT_FLAG_OUT);
281 SetConsoleMode(alt, dwFlag);
283 dwFlag = arg->dwFlagOut;
284 if (WINCONSOLE.isTermInfoConsole)
285 dwFlag |= (VT_FLAG_OUT);
287 dwFlag |= (VT_FLAG_OUT);
289 SetConsoleMode(hdl, dwFlag);
292 dwFlag = arg->dwFlagIn | ENABLE_MOUSE_INPUT;
293 if (WINCONSOLE.isTermInfoConsole)
294 dwFlag |= (VT_FLAG_IN);
296 dwFlag &= ~(VT_FLAG_IN);
298 SetConsoleMode(alt, dwFlag);
299 T(("effective mode set %s",_nc_trace_ttymode(&TRCTTY)));
307 _nc_console_getmode(HANDLE hdl, TTY *arg)
315 if (hdl==WINCONSOLE.inp) {
316 if(GetConsoleMode(hdl,&dwFlag)) {
317 arg->dwFlagIn = dwFlag;
319 if (GetConsoleMode(alt,&dwFlag)) {
320 arg->dwFlagOut = dwFlag;
325 if (GetConsoleMode(hdl,&dwFlag)) {
326 arg->dwFlagOut = dwFlag;
328 if (GetConsoleMode(alt,&dwFlag)) {
329 arg->dwFlagIn = dwFlag;
335 T(("lib_win32con:_nc_console_getmode %s", _nc_trace_ttymode(arg)));
340 _nc_console_flush(HANDLE hdl)
344 T((T_CALLED("lib_win32con::_nc_console_flush(hdl=%p"),hdl));
346 if (hdl != INVALID_HANDLE_VALUE) {
347 if (hdl == WINCONSOLE.hdl ||
348 hdl == WINCONSOLE.inp ||
349 hdl == WINCONSOLE.out) {
350 if (!FlushConsoleInputBuffer(WINCONSOLE.inp))
354 T(("_nc_console_flush not requesting a handle owned by console."));
361 _nc_console_MapColor(bool fore, int color)
363 static const int _cmap[] =
364 {0, 4, 2, 6, 1, 5, 3, 7};
366 if (color < 0 || color > 7)
377 * Attempt to save the screen contents. PDCurses does this if
378 * PDC_RESTORE_SCREEN is set, giving the same visual appearance on
379 * restoration as if the library had allocated a console buffer. MSDN
380 * says that the data which can be read is limited to 64Kb (and may be
384 save_original_screen(void)
388 WINCONSOLE.save_region.Top = 0;
389 WINCONSOLE.save_region.Left = 0;
390 WINCONSOLE.save_region.Bottom = (SHORT) (WINCONSOLE.SBI.dwSize.Y - 1);
391 WINCONSOLE.save_region.Right = (SHORT) (WINCONSOLE.SBI.dwSize.X - 1);
393 if (read_screen_data()) {
397 WINCONSOLE.save_region.Top = WINCONSOLE.SBI.srWindow.Top;
398 WINCONSOLE.save_region.Left = WINCONSOLE.SBI.srWindow.Left;
399 WINCONSOLE.save_region.Bottom = WINCONSOLE.SBI.srWindow.Bottom;
400 WINCONSOLE.save_region.Right = WINCONSOLE.SBI.srWindow.Right;
402 WINCONSOLE.window_only = TRUE;
404 if (read_screen_data()) {
409 T(("... save original screen contents %s", result ? "ok" : "err"));
414 restore_original_screen(void)
418 SMALL_RECT save_region = WINCONSOLE.save_region;
420 T(("... restoring %s",
421 WINCONSOLE.window_only ? "window" : "entire buffer"));
423 bufferCoord.X = (SHORT) (WINCONSOLE.window_only ?
424 WINCONSOLE.SBI.srWindow.Left : 0);
425 bufferCoord.Y = (SHORT) (WINCONSOLE.window_only ?
426 WINCONSOLE.SBI.srWindow.Top : 0);
428 if (write_screen(WINCONSOLE.hdl,
429 WINCONSOLE.save_screen,
430 WINCONSOLE.save_size,
434 mvcur(-1, -1, LINES - 2, 0);
435 T(("... restore original screen contents ok %dx%d (%d,%d - %d,%d)",
436 WINCONSOLE.save_size.Y,
437 WINCONSOLE.save_size.X,
443 T(("... restore original screen contents err"));
449 read_screen_data(void)
455 WINCONSOLE.save_size.X = (SHORT) (WINCONSOLE.save_region.Right
456 - WINCONSOLE.save_region.Left + 1);
457 WINCONSOLE.save_size.Y = (SHORT) (WINCONSOLE.save_region.Bottom
458 - WINCONSOLE.save_region.Top + 1);
460 want = (size_t) (WINCONSOLE.save_size.X * WINCONSOLE.save_size.Y);
462 if ((WINCONSOLE.save_screen = malloc(want * sizeof(CHAR_INFO))) != 0) {
463 bufferCoord.X = (SHORT) (WINCONSOLE.window_only ?
464 WINCONSOLE.SBI.srWindow.Left : 0);
465 bufferCoord.Y = (SHORT) (WINCONSOLE.window_only ?
466 WINCONSOLE.SBI.srWindow.Top : 0);
468 T(("... reading console %s %dx%d into %d,%d - %d,%d at %d,%d",
469 WINCONSOLE.window_only ? "window" : "buffer",
470 WINCONSOLE.save_size.Y, WINCONSOLE.save_size.X,
471 WINCONSOLE.save_region.Top,
472 WINCONSOLE.save_region.Left,
473 WINCONSOLE.save_region.Bottom,
474 WINCONSOLE.save_region.Right,
478 if (read_screen(WINCONSOLE.hdl,
479 WINCONSOLE.save_screen,
480 WINCONSOLE.save_size,
482 &WINCONSOLE.save_region)) {
485 T((" error %#lx", (unsigned long) GetLastError()));
486 FreeAndNull(WINCONSOLE.save_screen);
494 _nc_console_get_SBI(void)
497 if (GetConsoleScreenBufferInfo(WINCONSOLE.hdl, &(WINCONSOLE.SBI))) {
498 T(("GetConsoleScreenBufferInfo"));
499 T(("... buffer(X:%d Y:%d)",
500 WINCONSOLE.SBI.dwSize.X,
501 WINCONSOLE.SBI.dwSize.Y));
502 T(("... window(X:%d Y:%d)",
503 WINCONSOLE.SBI.dwMaximumWindowSize.X,
504 WINCONSOLE.SBI.dwMaximumWindowSize.Y));
505 T(("... cursor(X:%d Y:%d)",
506 WINCONSOLE.SBI.dwCursorPosition.X,
507 WINCONSOLE.SBI.dwCursorPosition.Y));
508 T(("... display(Top:%d Bottom:%d Left:%d Right:%d)",
509 WINCONSOLE.SBI.srWindow.Top,
510 WINCONSOLE.SBI.srWindow.Bottom,
511 WINCONSOLE.SBI.srWindow.Left,
512 WINCONSOLE.SBI.srWindow.Right));
513 if (WINCONSOLE.buffered) {
514 WINCONSOLE.origin.X = 0;
515 WINCONSOLE.origin.Y = 0;
517 WINCONSOLE.origin.X = WINCONSOLE.SBI.srWindow.Left;
518 WINCONSOLE.origin.Y = WINCONSOLE.SBI.srWindow.Top;
522 T(("GetConsoleScreenBufferInfo ERR"));
531 * In "normal" mode, reset the buffer- and window-sizes back to their original values.
534 _nc_console_set_scrollback(bool normal, CONSOLE_SCREEN_BUFFER_INFO * info)
538 bool changed = FALSE;
540 T((T_CALLED("lib_win32con::_nc_console_set_scrollback(%s)"),
545 T(("... SBI.srWindow %d,%d .. %d,%d",
548 info->srWindow.Bottom,
549 info->srWindow.Right));
550 T(("... SBI.dwSize %dx%d",
555 rect = info->srWindow;
556 coord = info->dwSize;
557 if (memcmp(info, &WINCONSOLE.SBI, sizeof(*info)) != 0) {
559 WINCONSOLE.SBI = *info;
562 int high = info->srWindow.Bottom - info->srWindow.Top + 1;
563 int wide = info->srWindow.Right - info->srWindow.Left + 1;
565 if (high < MIN_HIGH) {
566 T(("... height %d < %d", high, MIN_HIGH));
570 if (wide < MIN_WIDE) {
571 T(("... width %d < %d", wide, MIN_WIDE));
578 rect.Right = (SHORT) (wide - 1);
579 rect.Bottom = (SHORT) (high - 1);
581 coord.X = (SHORT) wide;
582 coord.Y = (SHORT) high;
584 if (info->dwSize.Y != high ||
585 info->dwSize.X != wide ||
586 info->srWindow.Top != 0 ||
587 info->srWindow.Left != 0) {
594 T(("... coord %d,%d", coord.Y, coord.X));
595 T(("... rect %d,%d - %d,%d",
597 rect.Bottom, rect.Right));
598 SetConsoleScreenBufferSize(WINCONSOLE.hdl, coord); /* dwSize */
599 SetConsoleWindowInfo(WINCONSOLE.hdl, TRUE, &rect); /* srWindow */
600 _nc_console_get_SBI();
606 tdiff(FILETIME fstart, FILETIME fend)
608 ULARGE_INTEGER ustart;
612 ustart.LowPart = fstart.dwLowDateTime;
613 ustart.HighPart = fstart.dwHighDateTime;
614 uend.LowPart = fend.dwLowDateTime;
615 uend.HighPart = fend.dwHighDateTime;
617 diff = (uend.QuadPart - ustart.QuadPart) / 10000;
622 Adjust(int milliseconds, int diff)
624 if (milliseconds != INFINITY) {
625 milliseconds -= diff;
626 if (milliseconds < 0)
632 #define BUTTON_MASK (FROM_LEFT_1ST_BUTTON_PRESSED | \
633 FROM_LEFT_2ND_BUTTON_PRESSED | \
634 FROM_LEFT_3RD_BUTTON_PRESSED | \
635 FROM_LEFT_4TH_BUTTON_PRESSED | \
636 RIGHTMOST_BUTTON_PRESSED)
639 decode_mouse(SCREEN *sp, int mask)
644 assert(sp && console_initialized);
646 if (mask & FROM_LEFT_1ST_BUTTON_PRESSED)
647 result |= BUTTON1_PRESSED;
648 if (mask & FROM_LEFT_2ND_BUTTON_PRESSED)
649 result |= BUTTON2_PRESSED;
650 if (mask & FROM_LEFT_3RD_BUTTON_PRESSED)
651 result |= BUTTON3_PRESSED;
652 if (mask & FROM_LEFT_4TH_BUTTON_PRESSED)
653 result |= BUTTON4_PRESSED;
655 if (mask & RIGHTMOST_BUTTON_PRESSED) {
656 switch (WINCONSOLE.numButtons) {
658 result |= BUTTON1_PRESSED;
661 result |= BUTTON2_PRESSED;
664 result |= BUTTON3_PRESSED;
667 result |= BUTTON4_PRESSED;
675 #define AdjustY() (WINCONSOLE.buffered ? 0 : (int) WINCONSOLE.SBI.srWindow.Top)
678 handle_mouse(SCREEN *sp, MOUSE_EVENT_RECORD mer)
685 sp->_drv_mouse_old_buttons = sp->_drv_mouse_new_buttons;
686 sp->_drv_mouse_new_buttons = mer.dwButtonState & BUTTON_MASK;
689 * We're only interested if the button is pressed or released.
690 * FIXME: implement continuous event-tracking.
692 if (sp->_drv_mouse_new_buttons != sp->_drv_mouse_old_buttons) {
693 memset(&work, 0, sizeof(work));
695 if (sp->_drv_mouse_new_buttons) {
697 (mmask_t) decode_mouse(sp,
698 sp->_drv_mouse_new_buttons);
700 /* cf: BUTTON_PRESSED, BUTTON_RELEASED */
702 (mmask_t) (decode_mouse(sp,
703 sp->_drv_mouse_old_buttons)
708 work.x = mer.dwMousePosition.X;
709 work.y = mer.dwMousePosition.Y - AdjustY();
711 sp->_drv_mouse_fifo[sp->_drv_mouse_tail] = work;
712 sp->_drv_mouse_tail += 1;
718 rkeycompare(const void *el1, const void *el2)
720 WORD key1 = (LOWORD((*((const LONG *) el1)))) & 0x7fff;
721 WORD key2 = (LOWORD((*((const LONG *) el2)))) & 0x7fff;
723 return ((key1 < key2) ? -1 : ((key1 == key2) ? 0 : 1));
728 keycompare(const void *el1, const void *el2)
730 WORD key1 = HIWORD((*((const LONG *) el1)));
731 WORD key2 = HIWORD((*((const LONG *) el2)));
733 return ((key1 < key2) ? -1 : ((key1 == key2) ? 0 : 1));
741 if (!WINCONSOLE.isTermInfoConsole) {
744 LONG key = GenMap(vKey, 0);
748 (size_t) (N_INI + FKEYS),
752 key = *((LONG *) res);
754 code = (int) (nKey & 0x7fff);
767 if (!WINCONSOLE.isTermInfoConsole) {
770 LONG key = GenMap(vKey, 0);
774 (size_t) (N_INI + FKEYS),
778 key = *((LONG *) res);
780 code = (int) (nKey & 0x7fff);
789 _nc_console_keyok(int keycode,int flag)
795 LONG key = GenMap(0, (WORD) keycode);
797 T((T_CALLED("lib_win32con::_nc_console_keyok(%d, %d)"), keycode, flag));
801 (size_t) (N_INI + FKEYS),
805 key = *((LONG *) res);
807 nKey = (LOWORD(key)) & 0x7fff;
810 *(LONG *) res = GenMap(vKey, nKey);
816 _nc_console_keyExist(int keycode)
821 LONG key = GenMap(0, (WORD) keycode);
823 T((T_CALLED("lib_win32con::_nc_console_keyExist(%d)"), keycode));
826 (size_t) (N_INI + FKEYS),
830 key = *((LONG *) res);
832 if (!(nKey & 0x8000))
845 EVENTLIST_2nd(_nc_eventlist * evl))
847 INPUT_RECORD inp_rec;
849 DWORD nRead = 0, rc = (DWORD) (-1);
854 bool isNoDelay = (milliseconds == 0);
856 #ifdef NCURSES_WGETCH_EVENTS
857 (void) evl; /* TODO: implement wgetch-events */
860 #define IGNORE_CTRL_KEYS (SHIFT_PRESSED|LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED| \
861 LEFT_CTRL_PRESSED|RIGHT_CTRL_PRESSED)
862 #define CONSUME() ReadConsoleInput(hdl,&inp_rec,1,&nRead)
866 TR(TRACE_IEVENT, ("start twait: hdl=%p, %d milliseconds, mode: %d",
867 hdl, milliseconds, mode));
869 if (milliseconds < 0)
870 milliseconds = INFINITY;
872 memset(&inp_rec, 0, sizeof(inp_rec));
876 GetSystemTimeAsFileTime(&fstart);
877 rc = WaitForSingleObject(hdl, (DWORD) milliseconds);
878 GetSystemTimeAsFileTime(&fend);
879 diff = (int) tdiff(fstart, fend);
880 milliseconds = Adjust(milliseconds, diff);
885 if (isNoDelay || (rc == WAIT_OBJECT_0)) {
888 b = GetNumberOfConsoleInputEvents(hdl, &nRead);
890 T(("twait:err GetNumberOfConsoleInputEvents"));
892 if (isNoDelay && b) {
893 T(("twait: Events Available: %ld", nRead));
899 INPUT_RECORD* pInpRec =
900 TypeAlloca(INPUT_RECORD,nRead);
901 if (pInpRec != NULL) {
904 memset(pInpRec,0,sizeof(INPUT_RECORD)*nRead);
905 f = PeekConsoleInput(hdl, pInpRec, nRead, &n);
908 if (pInpRec[i].EventType==KEY_EVENT) {
909 if(pInpRec[i].Event.KeyEvent.bKeyDown) {
911 (pInpRec[i].Event.KeyEvent.dwControlKeyState & \
921 T(("twait:err PeekConsoleInput"));
926 T(("twait:err could not alloca input records"));
930 if (b && nRead > 0) {
931 b = PeekConsoleInput(hdl, &inp_rec, 1, &nRead);
933 T(("twait:err PeekConsoleInput"));
935 if (b && nRead > 0) {
936 switch (inp_rec.EventType) {
938 if (mode & TW_INPUT) {
940 inp_rec.Event.KeyEvent.wVirtualKeyCode;
942 inp_rec.Event.KeyEvent.uChar.AsciiChar;
943 T(("twait:event KEY_EVENT"));
944 T(("twait vk=%d, ch=%d, keydown=%d",
945 vk,ch,inp_rec.Event.KeyEvent.bKeyDown));
946 if (inp_rec.Event.KeyEvent.bKeyDown) {
947 T(("twait:event KeyDown"));
948 if (!WINCONSOLE.isTermInfoConsole &&
950 int nKey = MapKey(vk);
964 T(("twait:event MOUSE_EVENT"));
966 (inp_rec.Event.MouseEvent.dwButtonState
967 & BUTTON_MASK)) == 0) {
969 } else if (mode & TW_MOUSE) {
974 /* e.g., FOCUS_EVENT */
976 T(("twait:event Tyoe %d",inp_rec.EventType));
978 _nc_console_selectActiveHandle();
986 if (rc != WAIT_TIMEOUT) {
997 TR(TRACE_IEVENT, ("end twait: returned %d (%d), remaining time %d msec",
998 code, GetLastError(), milliseconds));
1001 *timeleft = milliseconds;
1007 _nc_console_testmouse(
1011 EVENTLIST_2nd(_nc_eventlist * evl))
1017 if (sp->_drv_mouse_head < sp->_drv_mouse_tail) {
1020 rc = _nc_console_twait(sp,
1025 EVENTLIST_2nd(evl));
1037 INPUT_RECORD inp_rec;
1045 memset(&inp_rec, 0, sizeof(inp_rec));
1047 T((T_CALLED("lib_win32con::_nc_console_read(%p)"), sp));
1049 while ((b = ReadConsoleInput(hdl, &inp_rec, 1, &nRead))) {
1050 if (b && nRead > 0) {
1053 rc = rc + (int) nRead;
1054 if (inp_rec.EventType == KEY_EVENT) {
1055 if (!inp_rec.Event.KeyEvent.bKeyDown)
1057 *buf = (int) inp_rec.Event.KeyEvent.uChar.AsciiChar;
1058 vk = inp_rec.Event.KeyEvent.wVirtualKeyCode;
1060 * There are 24 virtual function-keys, and typically
1061 * 12 function-keys on a keyboard. Use the shift-modifier
1062 * to provide the remaining 12 keys.
1064 if (vk >= VK_F1 && vk <= VK_F12) {
1065 if (inp_rec.Event.KeyEvent.dwControlKeyState &
1067 vk = (WORD) (vk + 12);
1071 int key = MapKey(vk);
1074 if (sp->_keypad_on) {
1082 } else if (inp_rec.EventType == MOUSE_EVENT) {
1083 if (handle_mouse(sp,
1084 inp_rec.Event.MouseEvent)) {
1095 /* Our replacement for the systems _isatty to include also
1096 a test for mintty. This is called from the NC_ISATTY macro
1097 defined in curses.priv.h
1101 - 1 : A Windows character device detected by _isatty
1102 - 2 : A future implementation may return 2 for mintty
1105 _nc_console_isatty(int fd)
1108 T((T_CALLED("lib_win32con::_nc_console_isatty(%d"),fd));
1112 #ifdef _NC_CHECK_MINTTY
1114 if (_nc_console_checkmintty(fd, NULL)) {
1116 fprintf(stderr,"ncurses on Windows must run in a Windows console.\n");
1117 fprintf(stderr,"On newer versions of Windows, the calling program should create a PTY-like.\n");
1118 fprintf(stderr,"device using the CreatePseudoConsole Windows API call.\n");
1126 NCURSES_EXPORT(bool)
1127 _nc_console_checkinit(bool initFlag, bool assumeTermInfo)
1131 T((T_CALLED("lib_win32con::_nc_console_checkinit(initFlag=%d, assumeTermInfo=%d)"),
1132 initFlag,assumeTermInfo));
1135 res = console_initialized;
1137 /* initialize once, or not at all */
1138 if (!console_initialized) {
1142 BOOL buffered = FALSE;
1146 WINCONSOLE.isTermInfoConsole = assumeTermInfo;
1148 WINCONSOLE.map = (LPDWORD)malloc(sizeof(DWORD)*MAPSIZE);
1149 WINCONSOLE.rmap = (LPDWORD)malloc(sizeof(DWORD)*MAPSIZE);
1150 WINCONSOLE.ansi_map = (LPDWORD)malloc(sizeof(DWORD)*MAPSIZE);
1152 for (i = 0; i < (N_INI + FKEYS); i++) {
1154 WINCONSOLE.rmap[i] = WINCONSOLE.map[i] =
1156 WINCONSOLE.ansi_map[i] = (DWORD) ansi_keys[i];
1158 WINCONSOLE.rmap[i] = WINCONSOLE.map[i] =
1159 (DWORD) GenMap((VK_F1 + (i - N_INI)),
1160 (KEY_F(1) + (i - N_INI)));
1161 WINCONSOLE.ansi_map[i] =
1162 (DWORD) GenMap((VK_F1 + (i - N_INI)),
1163 (';' + (i - N_INI)));
1166 qsort(WINCONSOLE.ansi_map,
1170 qsort(WINCONSOLE.map,
1174 qsort(WINCONSOLE.rmap,
1179 if (GetNumberOfConsoleMouseButtons(&num_buttons)) {
1180 WINCONSOLE.numButtons = (int) num_buttons;
1182 WINCONSOLE.numButtons = 1;
1185 a = _nc_console_MapColor(true, COLOR_WHITE) |
1186 _nc_console_MapColor(false, COLOR_BLACK);
1187 for (i = 0; i < CON_NUMPAIRS; i++)
1188 WINCONSOLE.pairs[i] = a;
1190 WINCONSOLE.inp = GetStdHandle(STD_INPUT_HANDLE);
1191 WINCONSOLE.out = GetStdHandle(STD_OUTPUT_HANDLE);
1192 WINCONSOLE.hdl = WINCONSOLE.out;
1194 GetConsoleMode(WINCONSOLE.inp,&WINCONSOLE.originalMode.dwFlagIn);
1195 GetConsoleMode(WINCONSOLE.out,&WINCONSOLE.originalMode.dwFlagOut);
1197 if (!WINCONSOLE.isTermInfoConsole) {
1201 b = AttachConsole(ATTACH_PARENT_PROCESS);
1203 if (getenv("NCGDB") || getenv("NCURSES_CONSOLE2")) {
1204 T(("... will not buffer console"));
1206 T(("... creating console buffer"));
1208 CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
1211 CONSOLE_TEXTMODE_BUFFER,
1217 /* We set binary I/O even when using the console
1218 driver to cover the situation, that the
1219 TERM variable is set to #win32con, but actually
1220 Windows supports virtual terminal processing.
1221 So if terminfo functions are used in this setup,
1222 they actually may work.
1224 _setmode(fileno(stdin) ,_O_BINARY);
1225 _setmode(fileno(stdout),_O_BINARY);
1227 if (WINCONSOLE.hdl != INVALID_HANDLE_VALUE) {
1228 WINCONSOLE.buffered = buffered;
1229 _nc_console_get_SBI();
1230 WINCONSOLE.save_SBI = WINCONSOLE.SBI;
1232 save_original_screen();
1233 _nc_console_set_scrollback(FALSE, &WINCONSOLE.SBI);
1235 GetConsoleCursorInfo(WINCONSOLE.hdl, &WINCONSOLE.save_CI);
1236 T(("... initial cursor is %svisible, %d%%",
1237 (WINCONSOLE.save_CI.bVisible ? "" : "not-"),
1238 (int) WINCONSOLE.save_CI.dwSize));
1241 WINCONSOLE.initialized = TRUE;
1242 console_initialized = TRUE;
1244 res = (WINCONSOLE.hdl != INVALID_HANDLE_VALUE);
1249 #endif // _NC_WINDOWS