+#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 it's 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;
+ DWORD dw;
+
+ handle = HANDLE_CAST(_get_osfhandle(fd));
+ if (handle == INVALID_HANDLE_VALUE)
+ return 0;
+
+ 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)
+ return 0;
+ if (len = GetProcessImageFileName(pHandle,
+ buf,
+ (DWORD) (sizeof(buf) /
+ sizeof(*buf)))) {
+ TCHAR *pos = _tcsrchr(buf, _T('\\'));
+ if (pos) {
+ pos++;
+ if (_tcsnicmp(pos, _TEXT("mintty.exe"), 10) == 0) {
+ if (pMinTTY)
+ *pMinTTY = pHandle;
+ return 1;
+ }
+ }
+ }
+ }
+ }
+ }
+ return 0;
+}
+#endif
+
+/* 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)
+{
+ if (_isatty(fd))
+ return 1;
+#if WINVER < 0x0600
+ return 0;
+#else
+ return _ismintty(fd, NULL);
+#endif
+}
+
+/* Borrowed from ansicon project.
+ Check whether or not a I/O handle is associated with
+ a Windows console.
+*/
+static BOOL
+IsConsoleHandle(HANDLE hdl)
+{
+ DWORD dwFlag = 0;
+ if (!GetConsoleMode(hdl, &dwFlag)) {
+ return (int) WriteConsoleA(hdl, NULL, 0, &dwFlag, NULL);
+ }
+ return (int) (dwFlag & ENABLE_PROCESSED_OUTPUT);
+}
+
+/* This is used when running in terminfo mode to discover,
+ whether or not the "terminal" is actually a Windows
+ Console. It's the responsibilty of the console to deal
+ with the terminal escape sequences that are sent by
+ terminfo.
+ */
+int
+_nc_mingw_isconsole(int fd)
+{
+ HANDLE hdl = HANDLE_CAST(_get_osfhandle(fd));
+ return (int) IsConsoleHandle(hdl);
+}
+