2012-03-23 10:11:34 +00:00
|
|
|
// Copyright (c) 2011-2012 Ryan Prichard
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
|
2011-11-17 11:00:02 +00:00
|
|
|
#include "Win32Console.h"
|
2012-03-14 06:27:21 +00:00
|
|
|
#include "AgentAssert.h"
|
2012-04-01 09:13:21 +00:00
|
|
|
#include "../shared/DebugClient.h"
|
2011-11-17 11:00:02 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
2012-03-13 07:16:51 +00:00
|
|
|
Win32Console::Win32Console()
|
2011-11-17 11:00:02 +00:00
|
|
|
{
|
2012-03-28 08:05:14 +00:00
|
|
|
m_conin = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
m_conout = GetStdHandle(STD_OUTPUT_HANDLE);
|
2011-11-17 11:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Win32Console::~Win32Console()
|
|
|
|
{
|
|
|
|
CloseHandle(m_conin);
|
|
|
|
CloseHandle(m_conout);
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE Win32Console::conin()
|
|
|
|
{
|
|
|
|
return m_conin;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE Win32Console::conout()
|
|
|
|
{
|
|
|
|
return m_conout;
|
|
|
|
}
|
|
|
|
|
|
|
|
HWND Win32Console::hwnd()
|
|
|
|
{
|
|
|
|
return GetConsoleWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Win32Console::postCloseMessage()
|
|
|
|
{
|
|
|
|
HWND h = hwnd();
|
|
|
|
if (h != NULL)
|
|
|
|
PostMessage(h, WM_CLOSE, 0, 0);
|
|
|
|
}
|
|
|
|
|
2012-03-28 08:05:26 +00:00
|
|
|
// A Windows console window can never be larger than the desktop window. To
|
|
|
|
// maximize the possible size of the console in rows*cols, try to configure
|
|
|
|
// the console with a small font.
|
|
|
|
void Win32Console::setSmallFont()
|
|
|
|
{
|
|
|
|
// Some of these types and functions are missing from the MinGW headers.
|
|
|
|
// Others are undocumented.
|
|
|
|
|
|
|
|
struct AGENT_CONSOLE_FONT_INFO {
|
|
|
|
DWORD nFont;
|
|
|
|
COORD dwFontSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AGENT_CONSOLE_FONT_INFOEX {
|
|
|
|
ULONG cbSize;
|
|
|
|
DWORD nFont;
|
|
|
|
COORD dwFontSize;
|
|
|
|
UINT FontFamily;
|
|
|
|
UINT FontWeight;
|
|
|
|
WCHAR FaceName[LF_FACESIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef BOOL WINAPI SetConsoleFontType(
|
|
|
|
HANDLE hOutput,
|
|
|
|
DWORD dwFontIndex);
|
|
|
|
typedef BOOL WINAPI GetCurrentConsoleFontType(
|
|
|
|
HANDLE hOutput,
|
|
|
|
BOOL bMaximize,
|
|
|
|
AGENT_CONSOLE_FONT_INFO *pFontInfo);
|
|
|
|
typedef BOOL WINAPI SetCurrentConsoleFontExType(
|
|
|
|
HANDLE hConsoleOutput,
|
|
|
|
BOOL bMaximumWindow,
|
|
|
|
AGENT_CONSOLE_FONT_INFOEX *lpConsoleCurrentFontEx);
|
|
|
|
typedef COORD WINAPI GetConsoleFontSizeType(
|
|
|
|
HANDLE hConsoleOutput,
|
|
|
|
DWORD nFont);
|
|
|
|
|
|
|
|
HINSTANCE dll = LoadLibrary(L"kernel32.dll");
|
|
|
|
ASSERT(dll != NULL);
|
|
|
|
|
|
|
|
SetConsoleFontType *pSetConsoleFont =
|
|
|
|
(SetConsoleFontType*)GetProcAddress(dll, "SetConsoleFont");
|
|
|
|
GetCurrentConsoleFontType *pGetCurrentConsoleFont =
|
|
|
|
(GetCurrentConsoleFontType*)GetProcAddress(dll, "GetCurrentConsoleFont");
|
|
|
|
SetCurrentConsoleFontExType *pSetCurrentConsoleFontEx =
|
|
|
|
(SetCurrentConsoleFontExType*)GetProcAddress(dll, "SetCurrentConsoleFontEx");
|
|
|
|
GetConsoleFontSizeType *pGetConsoleFontSize =
|
|
|
|
(GetConsoleFontSizeType*)GetProcAddress(dll, "GetConsoleFontSize");
|
|
|
|
|
|
|
|
BOOL success;
|
|
|
|
|
|
|
|
// The undocumented GetNumberOfConsoleFonts API reports that my Windows 7
|
|
|
|
// system has 12 fonts on it. Each font is really just a differently-sized
|
|
|
|
// raster/Terminal font. Font index 0 is the smallest font, so we want to
|
|
|
|
// choose it.
|
|
|
|
if (pGetConsoleFontSize == NULL) {
|
|
|
|
// This API should exist even on Windows XP.
|
|
|
|
trace("error: GetConsoleFontSize API is missing");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pGetCurrentConsoleFont == NULL) {
|
|
|
|
// This API should exist even on Windows XP.
|
|
|
|
trace("error: GetCurrentConsoleFont API is missing");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AGENT_CONSOLE_FONT_INFO fi;
|
|
|
|
success = pGetCurrentConsoleFont(m_conout, FALSE, &fi);
|
|
|
|
if (!success) {
|
|
|
|
trace("error: GetCurrentConsoleFont failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
COORD smallest = pGetConsoleFontSize(m_conout, 0);
|
|
|
|
if (smallest.X == 0 || smallest.Y == 0) {
|
|
|
|
trace("error: GetConsoleFontSize failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
trace("font #0: X=%d Y=%d", smallest.X, smallest.Y);
|
|
|
|
trace("current font: idx=%d X=%d Y=%d",
|
|
|
|
(int)fi.nFont, fi.dwFontSize.X, fi.dwFontSize.Y);
|
|
|
|
if (fi.dwFontSize.X <= smallest.X && fi.dwFontSize.Y <= smallest.Y)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// First try to call the documented Vista API.
|
|
|
|
if (pSetCurrentConsoleFontEx != NULL) {
|
|
|
|
AGENT_CONSOLE_FONT_INFOEX fix = {0};
|
|
|
|
fix.cbSize = sizeof(fix);
|
|
|
|
fix.nFont = 0;
|
|
|
|
fix.dwFontSize = smallest;
|
|
|
|
success = pSetCurrentConsoleFontEx(m_conout, FALSE, &fix);
|
|
|
|
trace("SetCurrentConsoleFontEx call %s",
|
|
|
|
success ? "succeeded" : "failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then try to call the undocumented Windows XP API.
|
|
|
|
//
|
|
|
|
// Somewhat described here:
|
|
|
|
// http://blogs.microsoft.co.il/blogs/pavely/archive/2009/07/23/changing-console-fonts.aspx
|
|
|
|
//
|
|
|
|
if (pSetConsoleFont != NULL) {
|
|
|
|
success = pSetConsoleFont(m_conout, 0);
|
|
|
|
trace("SetConsoleFont call %s", success ? "succeeded" : "failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace("Not setting console font size -- "
|
|
|
|
"neither SetConsoleFont nor SetCurrentConsoleFontEx API exists");
|
|
|
|
}
|
|
|
|
|
2012-03-13 07:16:51 +00:00
|
|
|
Coord Win32Console::bufferSize()
|
2011-11-17 11:00:02 +00:00
|
|
|
{
|
|
|
|
// TODO: error handling
|
2012-03-15 08:59:12 +00:00
|
|
|
CONSOLE_SCREEN_BUFFER_INFO info;
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
if (!GetConsoleScreenBufferInfo(m_conout, &info)) {
|
2012-03-24 23:43:50 +00:00
|
|
|
trace("GetConsoleScreenBufferInfo failed");
|
2012-03-15 08:59:12 +00:00
|
|
|
}
|
2012-03-13 07:16:51 +00:00
|
|
|
return info.dwSize;
|
2011-11-17 11:00:02 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 07:16:51 +00:00
|
|
|
SmallRect Win32Console::windowRect()
|
2011-11-17 11:00:02 +00:00
|
|
|
{
|
|
|
|
// TODO: error handling
|
2012-03-15 08:59:12 +00:00
|
|
|
CONSOLE_SCREEN_BUFFER_INFO info;
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
if (!GetConsoleScreenBufferInfo(m_conout, &info)) {
|
2012-03-24 23:43:50 +00:00
|
|
|
trace("GetConsoleScreenBufferInfo failed");
|
2012-03-15 08:59:12 +00:00
|
|
|
}
|
2012-03-13 07:16:51 +00:00
|
|
|
return info.srWindow;
|
2011-11-17 11:00:02 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 07:16:51 +00:00
|
|
|
void Win32Console::resizeBuffer(const Coord &size)
|
2011-11-17 11:00:02 +00:00
|
|
|
{
|
|
|
|
// TODO: error handling
|
2012-03-15 08:59:12 +00:00
|
|
|
if (!SetConsoleScreenBufferSize(m_conout, size)) {
|
2012-03-24 23:43:50 +00:00
|
|
|
trace("SetConsoleScreenBufferSize failed");
|
2012-03-15 08:59:12 +00:00
|
|
|
}
|
2011-11-17 11:00:02 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 07:16:51 +00:00
|
|
|
void Win32Console::moveWindow(const SmallRect &rect)
|
2011-11-17 11:00:02 +00:00
|
|
|
{
|
|
|
|
// TODO: error handling
|
2012-03-15 08:59:12 +00:00
|
|
|
if (!SetConsoleWindowInfo(m_conout, TRUE, &rect)) {
|
2012-03-24 23:43:50 +00:00
|
|
|
trace("SetConsoleWindowInfo failed");
|
2012-03-15 08:59:12 +00:00
|
|
|
}
|
2011-11-17 11:00:02 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 07:16:51 +00:00
|
|
|
void Win32Console::reposition(const Coord &newBufferSize,
|
|
|
|
const SmallRect &newWindowRect)
|
2011-11-17 11:00:02 +00:00
|
|
|
{
|
|
|
|
// Windows has one API for resizing the screen buffer and a different one
|
|
|
|
// for resizing the window. It seems that either API can fail if the
|
|
|
|
// window does not fit on the screen buffer.
|
|
|
|
|
2012-03-13 07:16:51 +00:00
|
|
|
const SmallRect origWindowRect(windowRect());
|
|
|
|
const SmallRect origBufferRect(Coord(), bufferSize());
|
2011-11-17 11:00:02 +00:00
|
|
|
|
2012-03-14 06:27:21 +00:00
|
|
|
ASSERT(!newBufferSize.isEmpty());
|
2012-03-13 07:16:51 +00:00
|
|
|
SmallRect bufferRect(Coord(), newBufferSize);
|
2012-03-14 06:27:21 +00:00
|
|
|
ASSERT(bufferRect.contains(newWindowRect));
|
2011-11-17 11:00:02 +00:00
|
|
|
|
2012-03-13 07:16:51 +00:00
|
|
|
SmallRect tempWindowRect = origWindowRect.intersected(bufferRect);
|
2011-11-17 11:00:02 +00:00
|
|
|
if (tempWindowRect.width() <= 0) {
|
2012-03-13 07:16:51 +00:00
|
|
|
tempWindowRect.setLeft(newBufferSize.X - 1);
|
2011-11-17 11:00:02 +00:00
|
|
|
tempWindowRect.setWidth(1);
|
|
|
|
}
|
|
|
|
if (tempWindowRect.height() <= 0) {
|
2012-03-13 07:16:51 +00:00
|
|
|
tempWindowRect.setTop(newBufferSize.Y - 1);
|
2011-11-17 11:00:02 +00:00
|
|
|
tempWindowRect.setHeight(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alternatively, if we can immediately use the new window size,
|
|
|
|
// do that instead.
|
|
|
|
if (origBufferRect.contains(newWindowRect))
|
|
|
|
tempWindowRect = newWindowRect;
|
|
|
|
|
|
|
|
if (tempWindowRect != origWindowRect)
|
|
|
|
moveWindow(tempWindowRect);
|
|
|
|
resizeBuffer(newBufferSize);
|
|
|
|
if (newWindowRect != tempWindowRect)
|
|
|
|
moveWindow(newWindowRect);
|
|
|
|
}
|
|
|
|
|
2012-03-13 07:16:51 +00:00
|
|
|
Coord Win32Console::cursorPosition()
|
2011-11-17 11:00:02 +00:00
|
|
|
{
|
|
|
|
// TODO: error handling
|
2012-03-15 08:59:12 +00:00
|
|
|
CONSOLE_SCREEN_BUFFER_INFO info;
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
if (!GetConsoleScreenBufferInfo(m_conout, &info)) {
|
2012-03-24 23:43:50 +00:00
|
|
|
trace("GetConsoleScreenBufferInfo failed");
|
2012-03-15 08:59:12 +00:00
|
|
|
}
|
2012-03-13 07:16:51 +00:00
|
|
|
return info.dwCursorPosition;
|
2011-11-17 11:00:02 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 07:16:51 +00:00
|
|
|
void Win32Console::setCursorPosition(const Coord &coord)
|
2011-11-17 11:00:02 +00:00
|
|
|
{
|
|
|
|
// TODO: error handling
|
2012-03-15 08:59:12 +00:00
|
|
|
if (!SetConsoleCursorPosition(m_conout, coord)) {
|
2012-03-24 23:43:50 +00:00
|
|
|
trace("SetConsoleCursorPosition failed");
|
2012-03-15 08:59:12 +00:00
|
|
|
}
|
2011-11-17 11:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Win32Console::writeInput(const INPUT_RECORD *ir, int count)
|
|
|
|
{
|
|
|
|
// TODO: error handling
|
2012-03-15 08:59:12 +00:00
|
|
|
DWORD dummy = 0;
|
|
|
|
if (!WriteConsoleInput(m_conin, ir, count, &dummy)) {
|
2012-03-24 23:43:50 +00:00
|
|
|
trace("WriteConsoleInput failed");
|
2012-03-15 08:59:12 +00:00
|
|
|
}
|
2011-11-17 11:00:02 +00:00
|
|
|
}
|
|
|
|
|
2012-03-20 04:30:16 +00:00
|
|
|
bool Win32Console::processedInputMode()
|
|
|
|
{
|
|
|
|
// TODO: error handling
|
|
|
|
DWORD mode = 0;
|
|
|
|
if (!GetConsoleMode(m_conin, &mode)) {
|
2012-03-24 23:43:50 +00:00
|
|
|
trace("GetConsoleMode failed");
|
2012-03-20 04:30:16 +00:00
|
|
|
}
|
|
|
|
return (mode & ENABLE_PROCESSED_INPUT) == ENABLE_PROCESSED_INPUT;
|
|
|
|
}
|
|
|
|
|
2012-03-13 07:16:51 +00:00
|
|
|
void Win32Console::read(const SmallRect &rect, CHAR_INFO *data)
|
2011-11-17 11:00:02 +00:00
|
|
|
{
|
|
|
|
// TODO: error handling
|
2012-03-15 08:59:12 +00:00
|
|
|
SmallRect tmp(rect);
|
|
|
|
if (!ReadConsoleOutput(m_conout, data, rect.size(), Coord(), &tmp)) {
|
2012-03-24 23:43:50 +00:00
|
|
|
trace("ReadConsoleOutput failed [x:%d,y:%d,w:%d,h:%d]",
|
2012-03-15 08:59:12 +00:00
|
|
|
rect.Left, rect.Top, rect.width(), rect.height());
|
|
|
|
}
|
2011-11-17 11:00:02 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 07:16:51 +00:00
|
|
|
void Win32Console::write(const SmallRect &rect, const CHAR_INFO *data)
|
2011-11-17 11:00:02 +00:00
|
|
|
{
|
|
|
|
// TODO: error handling
|
2012-03-15 08:59:12 +00:00
|
|
|
SmallRect tmp(rect);
|
|
|
|
if (!WriteConsoleOutput(m_conout, data, rect.size(), Coord(), &tmp)) {
|
2012-03-24 23:43:50 +00:00
|
|
|
trace("WriteConsoleOutput failed");
|
2012-03-15 08:59:12 +00:00
|
|
|
}
|
2011-11-17 11:00:02 +00:00
|
|
|
}
|