installed pty
[VSoRC/.git] / node_modules / node-pty / deps / winpty / misc / IsNewConsole.cc
1 // Determines whether this is a new console by testing whether MARK moves the
2 // cursor.
3 //
4 // WARNING: This test program may behave erratically if run under winpty.
5 //
6
7 #include <windows.h>
8
9 #include <stdio.h>
10 #include <string.h>
11
12 #include "TestUtil.cc"
13
14 const int SC_CONSOLE_MARK = 0xFFF2;
15 const int SC_CONSOLE_SELECT_ALL = 0xFFF5;
16
17 static COORD getWindowPos(HANDLE conout) {
18     CONSOLE_SCREEN_BUFFER_INFO info = {};
19     BOOL ret = GetConsoleScreenBufferInfo(conout, &info);
20     ASSERT(ret && "GetConsoleScreenBufferInfo failed");
21     return { info.srWindow.Left, info.srWindow.Top };
22 }
23
24 static COORD getWindowSize(HANDLE conout) {
25     CONSOLE_SCREEN_BUFFER_INFO info = {};
26     BOOL ret = GetConsoleScreenBufferInfo(conout, &info);
27     ASSERT(ret && "GetConsoleScreenBufferInfo failed");
28     return {
29         static_cast<short>(info.srWindow.Right - info.srWindow.Left + 1),
30         static_cast<short>(info.srWindow.Bottom - info.srWindow.Top + 1)
31     };
32 }
33
34 static COORD getCursorPos(HANDLE conout) {
35     CONSOLE_SCREEN_BUFFER_INFO info = {};
36     BOOL ret = GetConsoleScreenBufferInfo(conout, &info);
37     ASSERT(ret && "GetConsoleScreenBufferInfo failed");
38     return info.dwCursorPosition;
39 }
40
41 static void setCursorPos(HANDLE conout, COORD pos) {
42     BOOL ret = SetConsoleCursorPosition(conout, pos);
43     ASSERT(ret && "SetConsoleCursorPosition failed");
44 }
45
46 int main() {
47     const HANDLE conout = openConout();
48     const HWND hwnd = GetConsoleWindow();
49     ASSERT(hwnd != NULL && "GetConsoleWindow() returned NULL");
50
51     // With the legacy console, the Mark command moves the the cursor to the
52     // top-left cell of the visible console window.  Determine whether this
53     // is the new console by seeing if the cursor moves.
54
55     const auto windowSize = getWindowSize(conout);
56     if (windowSize.X <= 1) {
57         printf("Error: console window must be at least 2 columns wide\n");
58         trace("Error: console window must be at least 2 columns wide");
59         return 1;
60     }
61
62     bool cursorMoved = false;
63     const auto initialPos = getCursorPos(conout);
64
65     const auto windowPos = getWindowPos(conout);
66     setCursorPos(conout, { static_cast<short>(windowPos.X + 1), windowPos.Y });
67
68     {
69         const auto posA = getCursorPos(conout);
70         SendMessage(hwnd, WM_SYSCOMMAND, SC_CONSOLE_MARK, 0);
71         const auto posB = getCursorPos(conout);
72         cursorMoved = memcmp(&posA, &posB, sizeof(posA)) != 0;
73         SendMessage(hwnd, WM_CHAR, 27, 0x00010001); // Send ESCAPE
74     }
75
76     setCursorPos(conout, initialPos);
77
78     if (cursorMoved) {
79         printf("Legacy console (i.e. MARK moved cursor)\n");
80         trace("Legacy console (i.e. MARK moved cursor)");
81     } else {
82         printf("Windows 10 new console (i.e MARK did not move cursor)\n");
83         trace("Windows 10 new console (i.e MARK did not move cursor)");
84     }
85
86     return 0;
87 }