+
+/* --------------------------------------------------------- */
+
+static HANDLE
+get_handle(int fd)
+{
+ intptr_t value = _get_osfhandle(fd);
+ return (HANDLE) value;
+}
+
+#if WINVER >= 0x0600
+/* This function tests, whether or not the ncurses application
+ is running as a descendant of MSYS2/cygwin mintty terminal
+ application. mintty doesn't use Windows Console for its screen
+ I/O, so the native Windows _isatty doesn't recognize it as
+ character device. But we can discover we are at the end of an
+ Pipe and can query to server side of the pipe, looking whether
+ or not this is mintty.
+ */
+static int
+_ismintty(int fd, LPHANDLE pMinTTY)
+{
+ HANDLE handle = get_handle(fd);
+ DWORD dw;
+ int code = 0;
+
+ T((T_CALLED("win32con::_ismintty(%d, %p)"), fd, pMinTTY));
+
+ if (handle != INVALID_HANDLE_VALUE) {
+ dw = GetFileType(handle);
+ if (dw == FILE_TYPE_PIPE) {
+ if (GetNamedPipeInfo(handle, 0, 0, 0, 0)) {
+ ULONG pPid;
+ /* Requires NT6 */
+ if (GetNamedPipeServerProcessId(handle, &pPid)) {
+ TCHAR buf[MAX_PATH];
+ DWORD len = 0;
+ /* These security attributes may allow us to
+ create a remote thread in mintty to manipulate
+ the terminal state remotely */
+ HANDLE pHandle = OpenProcess(
+ PROCESS_CREATE_THREAD
+ | PROCESS_QUERY_INFORMATION
+ | PROCESS_VM_OPERATION
+ | PROCESS_VM_WRITE
+ | PROCESS_VM_READ,
+ FALSE,
+ pPid);
+ if (pMinTTY)
+ *pMinTTY = INVALID_HANDLE_VALUE;
+ if (pHandle != INVALID_HANDLE_VALUE) {
+ if ((len = GetProcessImageFileName(
+ pHandle,
+ buf,
+ (DWORD)
+ array_length(buf)))) {
+ TCHAR *pos = _tcsrchr(buf, _T('\\'));
+ if (pos) {
+ pos++;
+ if (_tcsnicmp(pos, _TEXT("mintty.exe"), 10)
+ == 0) {
+ if (pMinTTY)
+ *pMinTTY = pHandle;
+ code = 1;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ returnCode(code);
+}
+#endif
+
+/* Borrowed from ansicon project.
+ Check whether or not an I/O handle is associated with
+ a Windows console.
+*/
+static BOOL
+IsConsoleHandle(HANDLE hdl)
+{
+ DWORD dwFlag = 0;
+ BOOL result;
+
+ if (!GetConsoleMode(hdl, &dwFlag)) {
+ result = (int) WriteConsoleA(hdl, NULL, 0, &dwFlag, NULL);
+ } else {
+ result = (int) (dwFlag & ENABLE_PROCESSED_OUTPUT);
+ }
+ return result;
+}
+
+/* Our replacement for the systems _isatty to include also
+ a test for mintty. This is called from the NC_ISATTY macro
+ defined in curses.priv.h
+ */
+int
+_nc_mingw_isatty(int fd)
+{
+ int result = 0;
+
+#ifdef __MING32__
+#define SysISATTY(fd) _isatty(fd)
+#else
+#define SysISATTY(fd) isatty(fd)
+#endif
+ if (SysISATTY(fd)) {
+ result = 1;
+ } else {
+#if WINVER >= 0x0600
+ result = _ismintty(fd, NULL);
+#endif
+ }
+ return result;
+}
+
+/* This is used when running in terminfo mode to discover,
+ whether or not the "terminal" is actually a Windows
+ Console. It is the responsibility of the console to deal
+ with the terminal escape sequences that are sent by
+ terminfo.
+ */
+int
+_nc_mingw_isconsole(int fd)
+{
+ HANDLE hdl = get_handle(fd);
+ int code = 0;
+
+ T((T_CALLED("win32con::_nc_mingw_isconsole(%d)"), fd));
+
+ code = (int) IsConsoleHandle(hdl);
+
+ returnCode(code);
+}
+
+#define TC_PROLOGUE(fd) \
+ SCREEN *sp; \
+ TERMINAL *term = 0; \
+ int code = ERR; \
+ if (_nc_screen_chain == 0) \
+ return 0; \
+ for (each_screen(sp)) { \
+ if (sp->_term && (sp->_term->Filedes == fd)) { \
+ term = sp->_term; \
+ break; \
+ } \
+ } \
+ assert(term != 0)
+
+int
+_nc_mingw_tcsetattr(
+ int fd,
+ int optional_action GCC_UNUSED,
+ const struct termios *arg)
+{
+ TC_PROLOGUE(fd);
+
+ if (_nc_mingw_isconsole(fd)) {
+ DWORD dwFlag = 0;
+ HANDLE ofd = get_handle(fd);
+ if (ofd != INVALID_HANDLE_VALUE) {
+ if (arg) {
+ if (arg->c_lflag & ICANON)
+ dwFlag |= ENABLE_LINE_INPUT;
+ else
+ dwFlag = dwFlag & (DWORD) (~ENABLE_LINE_INPUT);
+
+ if (arg->c_lflag & ECHO)
+ dwFlag = dwFlag | ENABLE_ECHO_INPUT;
+ else
+ dwFlag = dwFlag & (DWORD) (~ENABLE_ECHO_INPUT);
+
+ if (arg->c_iflag & BRKINT)
+ dwFlag |= ENABLE_PROCESSED_INPUT;
+ else
+ dwFlag = dwFlag & (DWORD) (~ENABLE_PROCESSED_INPUT);
+ }
+ dwFlag |= ENABLE_MOUSE_INPUT;
+ SetConsoleMode(ofd, dwFlag);
+ code = OK;
+ }
+ }
+ if (arg)
+ term->Nttyb = *arg;
+
+ return code;
+}
+
+int
+_nc_mingw_tcgetattr(int fd, struct termios *arg)
+{
+ TC_PROLOGUE(fd);
+
+ if (_nc_mingw_isconsole(fd)) {
+ if (arg)
+ *arg = term->Nttyb;
+ }
+ return code;
+}
+
+int
+_nc_mingw_tcflush(int fd, int queue)
+{
+ TC_PROLOGUE(fd);
+ (void) term;
+
+ if (_nc_mingw_isconsole(fd)) {
+ if (queue == TCIFLUSH) {
+ BOOL b = FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
+ if (!b)
+ return (int) GetLastError();
+ }
+ }
+ return code;
+}
+
+int
+_nc_mingw_testmouse(
+ SCREEN *sp,
+ HANDLE fd,
+ int delay
+ EVENTLIST_2nd(_nc_eventlist * evl))
+{
+ int rc = 0;
+
+ assert(sp);
+
+ if (sp->_drv_mouse_head < sp->_drv_mouse_tail) {
+ rc = TW_MOUSE;
+ } else {
+ rc = console_twait(sp,
+ fd,
+ TWAIT_MASK,
+ delay,
+ (int *) 0
+ EVENTLIST_2nd(evl));
+ }
+ return rc;
+}
+
+int
+_nc_mingw_console_read(
+ SCREEN *sp,
+ HANDLE fd,
+ int *buf)
+{
+ int rc = -1;
+ INPUT_RECORD inp_rec;
+ BOOL b;
+ DWORD nRead;
+ WORD vk;
+
+ assert(sp);
+ assert(buf);
+
+ memset(&inp_rec, 0, sizeof(inp_rec));
+
+ T((T_CALLED("_nc_mingw_console_read(%p)"), sp));
+
+ while ((b = ReadConsoleInput(fd, &inp_rec, 1, &nRead))) {
+ if (b && nRead > 0) {
+ if (rc < 0)
+ rc = 0;
+ rc = rc + (int) nRead;
+ if (inp_rec.EventType == KEY_EVENT) {
+ if (!inp_rec.Event.KeyEvent.bKeyDown)
+ continue;
+ *buf = (int) inp_rec.Event.KeyEvent.uChar.AsciiChar;
+ vk = inp_rec.Event.KeyEvent.wVirtualKeyCode;
+ /*
+ * There are 24 virtual function-keys, and typically
+ * 12 function-keys on a keyboard. Use the shift-modifier
+ * to provide the remaining 12 keys.
+ */
+ if (vk >= VK_F1 && vk <= VK_F12) {
+ if (inp_rec.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
+ vk = (WORD) (vk + 12);
+ }
+ }
+ if (*buf == 0) {
+ int key = MapKey(vk);
+ if (key < 0)
+ continue;
+ if (sp->_keypad_on) {
+ *buf = key;
+ } else {
+ ungetch('\0');
+ *buf = AnsiKey(vk);
+ }
+ }
+ break;
+ } else if (inp_rec.EventType == MOUSE_EVENT) {
+ if (handle_mouse(sp,
+ inp_rec.Event.MouseEvent)) {
+ *buf = KEY_MOUSE;
+ break;
+ }
+ }
+ continue;
+ }
+ }
+ returnCode(rc);
+}
+
+static bool
+InitConsole(void)
+{
+ /* initialize once, or not at all */
+ if (!console_initialized) {
+ int i;
+ DWORD num_buttons;
+ WORD a;
+ BOOL buffered = TRUE;
+ BOOL b;
+
+ START_TRACE();
+ if (_nc_mingw_isatty(0)) {
+ CON.isMinTTY = TRUE;
+ }
+
+ for (i = 0; i < (N_INI + FKEYS); i++) {
+ if (i < N_INI) {
+ CON.rmap[i] = CON.map[i] =
+ (DWORD) keylist[i];
+ CON.ansi_map[i] = (DWORD) ansi_keys[i];
+ } else {
+ CON.rmap[i] = CON.map[i] =
+ (DWORD) GenMap((VK_F1 + (i - N_INI)),
+ (KEY_F(1) + (i - N_INI)));
+ CON.ansi_map[i] =
+ (DWORD) GenMap((VK_F1 + (i - N_INI)),
+ (';' + (i - N_INI)));
+ }
+ }
+ qsort(CON.ansi_map,
+ (size_t) (MAPSIZE),
+ sizeof(keylist[0]),
+ keycompare);
+ qsort(CON.map,
+ (size_t) (MAPSIZE),
+ sizeof(keylist[0]),
+ keycompare);
+ qsort(CON.rmap,
+ (size_t) (MAPSIZE),
+ sizeof(keylist[0]),
+ rkeycompare);
+
+ if (GetNumberOfConsoleMouseButtons(&num_buttons)) {
+ CON.numButtons = (int) num_buttons;
+ } else {
+ CON.numButtons = 1;
+ }
+
+ a = MapColor(true, COLOR_WHITE) | MapColor(false, COLOR_BLACK);
+ for (i = 0; i < NUMPAIRS; i++)
+ CON.pairs[i] = a;
+
+ CON.inp = GetStdHandle(STD_INPUT_HANDLE);
+ CON.out = GetStdHandle(STD_OUTPUT_HANDLE);
+
+ b = AllocConsole();
+
+ if (!b)
+ b = AttachConsole(ATTACH_PARENT_PROCESS);
+
+ if (getenv("NCGDB") || getenv("NCURSES_CONSOLE2")) {
+ T(("... will not buffer console"));
+ buffered = FALSE;
+ CON.hdl = CON.out;
+ } else {
+ T(("... creating console buffer"));
+ CON.hdl = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
+ 0,
+ NULL,
+ CONSOLE_TEXTMODE_BUFFER,
+ NULL);
+ }
+
+ if (CON.hdl != INVALID_HANDLE_VALUE) {
+ CON.buffered = buffered;
+ get_SBI();
+ CON.save_SBI = CON.SBI;
+ if (!buffered) {
+ save_original_screen();
+ set_scrollback(FALSE, &CON.SBI);
+ }
+ GetConsoleCursorInfo(CON.hdl, &CON.save_CI);
+ T(("... initial cursor is %svisible, %d%%",
+ (CON.save_CI.bVisible ? "" : "not-"),
+ (int) CON.save_CI.dwSize));
+ }
+
+ console_initialized = TRUE;
+ }
+ return (CON.hdl != INVALID_HANDLE_VALUE);
+}
+
+static bool
+okConsoleHandle(TERMINAL_CONTROL_BLOCK * TCB)
+{
+ return ((TCB != 0) &&
+ (TCB->magic == WINMAGIC) &&
+ InitConsole());
+}
+
+/*
+ * While a constructor would ensure that this module is initialized, that will
+ * interfere with applications that may combine this with GUI interfaces.
+ */
+#if 0
+static
+__attribute__((constructor))
+ void _enter_console(void)
+{
+ (void) InitConsole();
+}
+#endif