From 33c5687d4a74b2bc3adef8fcf6c33f979453a1e5 Mon Sep 17 00:00:00 2001 From: Ryan Prichard Date: Sun, 5 Jun 2016 01:47:05 -0500 Subject: [PATCH] More updates to test programs for font/resizing work --- misc/GetBufferInfo.cc | 25 ----------------- misc/GetConsolePos.cc | 38 +++++++++++++++++++++++++ misc/IsNewConsole.cc | 58 ++++++++++++++++++++++++++++++++------- misc/MoveConsoleWindow.cc | 2 +- misc/build32.sh | 9 ++++++ misc/build64.sh | 9 ++++++ misc/build_win32.sh | 4 --- 7 files changed, 105 insertions(+), 40 deletions(-) delete mode 100644 misc/GetBufferInfo.cc create mode 100644 misc/GetConsolePos.cc create mode 100755 misc/build32.sh create mode 100755 misc/build64.sh delete mode 100755 misc/build_win32.sh diff --git a/misc/GetBufferInfo.cc b/misc/GetBufferInfo.cc deleted file mode 100644 index f8e2c92..0000000 --- a/misc/GetBufferInfo.cc +++ /dev/null @@ -1,25 +0,0 @@ -#include - -#include - -#include "TestUtil.cc" - -int main() { - const HANDLE conout = CreateFileW(L"CONOUT$", - GENERIC_READ | GENERIC_WRITE, - FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, OPEN_EXISTING, 0, NULL); - ASSERT(conout != INVALID_HANDLE_VALUE); - - CONSOLE_SCREEN_BUFFER_INFO info = {}; - BOOL ret = GetConsoleScreenBufferInfo(conout, &info); - ASSERT(ret && "GetConsoleScreenBufferInfo failed"); - - trace("srWindow={L=%d,T=%d,R=%d,B=%d}", info.srWindow.Left, info.srWindow.Top, info.srWindow.Right, info.srWindow.Bottom); - printf("srWindow={L=%d,T=%d,R=%d,B=%d}\n", info.srWindow.Left, info.srWindow.Top, info.srWindow.Right, info.srWindow.Bottom); - - trace("dwSize=%d,%d", info.dwSize.X); - printf("dwSize=%d,%d\n", info.dwSize.X); - - return 0; -} diff --git a/misc/GetConsolePos.cc b/misc/GetConsolePos.cc new file mode 100644 index 0000000..3dab8af --- /dev/null +++ b/misc/GetConsolePos.cc @@ -0,0 +1,38 @@ +#include + +#include + +#include "TestUtil.cc" + +int main() { + const HANDLE conout = openConout(); + + CONSOLE_SCREEN_BUFFER_INFO info = {}; + BOOL ret = GetConsoleScreenBufferInfo(conout, &info); + ASSERT(ret && "GetConsoleScreenBufferInfo failed"); + + trace("srWindow={L=%d,T=%d,R=%d,B=%d}", info.srWindow.Left, info.srWindow.Top, info.srWindow.Right, info.srWindow.Bottom); + printf("srWindow={L=%d,T=%d,R=%d,B=%d}\n", info.srWindow.Left, info.srWindow.Top, info.srWindow.Right, info.srWindow.Bottom); + + trace("dwSize=%d,%d", info.dwSize.X, info.dwSize.Y); + printf("dwSize=%d,%d\n", info.dwSize.X, info.dwSize.Y); + + const HWND hwnd = GetConsoleWindow(); + if (hwnd != NULL) { + RECT r = {}; + if (GetWindowRect(hwnd, &r)) { + const int w = r.right - r.left; + const int h = r.bottom - r.top; + trace("hwnd: pos=(%d,%d) size=(%d,%d)", r.left, r.top, w, h); + printf("hwnd: pos=(%d,%d) size=(%d,%d)\n", r.left, r.top, w, h); + } else { + trace("GetWindowRect failed"); + printf("GetWindowRect failed\n"); + } + } else { + trace("GetConsoleWindow returned NULL"); + printf("GetConsoleWindow returned NULL\n"); + } + + return 0; +} diff --git a/misc/IsNewConsole.cc b/misc/IsNewConsole.cc index a555f5a..2b554c7 100644 --- a/misc/IsNewConsole.cc +++ b/misc/IsNewConsole.cc @@ -1,3 +1,9 @@ +// Determines whether this is a new console by testing whether MARK moves the +// cursor. +// +// WARNING: This test program may behave erratically if run under winpty. +// + #include #include @@ -8,6 +14,23 @@ const int SC_CONSOLE_MARK = 0xFFF2; const int SC_CONSOLE_SELECT_ALL = 0xFFF5; +static COORD getWindowPos(HANDLE conout) { + CONSOLE_SCREEN_BUFFER_INFO info = {}; + BOOL ret = GetConsoleScreenBufferInfo(conout, &info); + ASSERT(ret && "GetConsoleScreenBufferInfo failed"); + return { info.srWindow.Left, info.srWindow.Top }; +} + +static COORD getWindowSize(HANDLE conout) { + CONSOLE_SCREEN_BUFFER_INFO info = {}; + BOOL ret = GetConsoleScreenBufferInfo(conout, &info); + ASSERT(ret && "GetConsoleScreenBufferInfo failed"); + return { + static_cast(info.srWindow.Right - info.srWindow.Left + 1), + static_cast(info.srWindow.Bottom - info.srWindow.Top + 1) + }; +} + static COORD getCursorPos(HANDLE conout) { CONSOLE_SCREEN_BUFFER_INFO info = {}; BOOL ret = GetConsoleScreenBufferInfo(conout, &info); @@ -25,24 +48,39 @@ int main() { const HWND hwnd = GetConsoleWindow(); ASSERT(hwnd != NULL && "GetConsoleWindow() returned NULL"); - bool isWindows10NewConsole = false; - COORD pos = getCursorPos(conout); - setCursorPos(conout, { 1, 0 }); + // With the legacy console, the Mark command moves the the cursor to the + // top-left cell of the visible console window. Determine whether this + // is the new console by seeing if the cursor moves. + + const auto windowSize = getWindowSize(conout); + if (windowSize.X <= 1) { + printf("Error: console window must be at least 2 columns wide\n"); + trace("Error: console window must be at least 2 columns wide"); + return 1; + } + + bool cursorMoved = false; + const auto initialPos = getCursorPos(conout); + + const auto windowPos = getWindowPos(conout); + setCursorPos(conout, { static_cast(windowPos.X + 1), windowPos.Y }); { - COORD posA = getCursorPos(conout); + const auto posA = getCursorPos(conout); SendMessage(hwnd, WM_SYSCOMMAND, SC_CONSOLE_MARK, 0); - COORD posB = getCursorPos(conout); - isWindows10NewConsole = !memcmp(&posA, &posB, sizeof(posA)); + const auto posB = getCursorPos(conout); + cursorMoved = memcmp(&posA, &posB, sizeof(posA)) != 0; SendMessage(hwnd, WM_CHAR, 27, 0x00010001); // Send ESCAPE } - setCursorPos(conout, pos); + setCursorPos(conout, initialPos); - if (isWindows10NewConsole) { - printf("New Windows 10 console\n"); + if (cursorMoved) { + printf("Legacy console (i.e. MARK moved cursor)\n"); + trace("Legacy console (i.e. MARK moved cursor)"); } else { - printf("Legacy console\n"); + printf("Windows 10 new console (i.e MARK did not move cursor)\n"); + trace("Windows 10 new console (i.e MARK did not move cursor)"); } return 0; diff --git a/misc/MoveConsoleWindow.cc b/misc/MoveConsoleWindow.cc index b1baee7..7d9684f 100644 --- a/misc/MoveConsoleWindow.cc +++ b/misc/MoveConsoleWindow.cc @@ -28,7 +28,7 @@ int main(int argc, char *argv[]) { BOOL ret = MoveWindow(hwnd, x, y, w, h, TRUE); trace("MoveWindow: ret=%d", ret); - printf("MoveWindow: ret=%d", ret); + printf("MoveWindow: ret=%d\n", ret); return 0; } diff --git a/misc/build32.sh b/misc/build32.sh new file mode 100755 index 0000000..162993c --- /dev/null +++ b/misc/build32.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -e +name=$1 +name=${name%.} +name=${name%.cc} +name=${name%.exe} +echo Compiling $name.cc to $name.exe +i686-w64-mingw32-g++.exe -static -std=c++11 $name.cc -o $name.exe +i686-w64-mingw32-strip $name.exe diff --git a/misc/build64.sh b/misc/build64.sh new file mode 100755 index 0000000..6757967 --- /dev/null +++ b/misc/build64.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -e +name=$1 +name=${name%.} +name=${name%.cc} +name=${name%.exe} +echo Compiling $name.cc to $name.exe +x86_64-w64-mingw32-g++.exe -static -std=c++11 $name.cc -o $name.exe +x86_64-w64-mingw32-strip $name.exe diff --git a/misc/build_win32.sh b/misc/build_win32.sh deleted file mode 100755 index 549cbbc..0000000 --- a/misc/build_win32.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -g++ Win32Echo1.cc -o Win32Echo1 -g++ Win32Echo2.cc -o Win32Echo2 -g++ Win32Write1.cc -o Win32Write1