-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.cpp
257 lines (210 loc) · 8.94 KB
/
console.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* palette.cpp
*
* Created on: Jul 5, 2013
* Author: Danyl
*/
#include "console.h"
/******************************************************************************
windowsVersionTest
Checks to see if have Vista/7 or earlier by attempting to retrieve function
from kernel32.dll that is only available in Vista+ version of Windows.
******************************************************************************/
int windowsVersionTest(void)
{
/* Retrieving pointers for Windows Vista/7 Functions */
PGetCurrentConsoleFontEx pGetCurrentConsoleFontEx = (PGetCurrentConsoleFontEx)
GetProcAddress(GetModuleHandle("kernel32.dll"), "GetCurrentConsoleFontEx");
/* If exists then we have Vita/7 */
if (pGetCurrentConsoleFontEx)
{
return 1;
}
else
{
return 0;
}
}
/******************************************************************************
SetConsolePalette
Sets the console palette and font size.
******************************************************************************/
VOID WINAPI SetConsolePalette(COLORREF palette[], int fontX, int fontY, wchar_t *fontName)
{
int i;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
/* Retrieving pointers for Windows Vista/7 Functions */
PGetCurrentConsoleFontEx pGetCurrentConsoleFontEx = (PGetCurrentConsoleFontEx)
GetProcAddress(GetModuleHandle("kernel32.dll"), "GetCurrentConsoleFontEx");
PSetCurrentConsoleFontEx pSetCurrentConsoleFontEx = (PSetCurrentConsoleFontEx)
GetProcAddress(GetModuleHandle("kernel32.dll"), "SetCurrentConsoleFontEx");
PGetConsoleScreenBufferInfoEx pGetConsoleScreenBufferInfoEx = (PGetConsoleScreenBufferInfoEx)
GetProcAddress(GetModuleHandle("kernel32.dll"), "GetConsoleScreenBufferInfoEx");
PSetConsoleScreenBufferInfoEx pSetConsoleScreenBufferInfoEx = (PSetConsoleScreenBufferInfoEx)
GetProcAddress(GetModuleHandle("kernel32.dll"), "SetConsoleScreenBufferInfoEx");
/* Check for pointers: if they exist, we have Vista/7 and we can use them */
if (pGetCurrentConsoleFontEx && pSetCurrentConsoleFontEx &&
pGetConsoleScreenBufferInfoEx && pSetConsoleScreenBufferInfoEx)
{
CONSOLE_SCREEN_BUFFER_INFOEX cbi;
CONSOLE_FONT_INFOEX cfi = {sizeof(CONSOLE_FONT_INFOEX)};
/* Tell the font info how big it is, to avoid memory corruption */
cfi.cbSize = sizeof(CONSOLE_FONT_INFOEX);
pGetCurrentConsoleFontEx(hOutput, TRUE, &cfi);
/* Set the font type to name indicated by wide string literal */
/* Set to 0 to keep current settings */
lstrcpyW(cfi.FaceName, fontName);
cfi.dwFontSize.X = fontX;
cfi.dwFontSize.Y = fontY;
cfi.FontFamily = 0; /* Set to 0x30 for Terminal font */
cfi.FontWeight = 0;
PSetCurrentConsoleFontEx pSetCurrentConsoleFontEx = (PSetCurrentConsoleFontEx)
GetProcAddress(GetModuleHandle("kernel32.dll"), "SetCurrentConsoleFontEx");
PGetConsoleScreenBufferInfoEx pGetConsoleScreenBufferInfoEx = (PGetConsoleScreenBufferInfoEx)
GetProcAddress(GetModuleHandle("kernel32.dll"), "GetConsoleScreenBufferInfoEx");
PSetConsoleScreenBufferInfoEx pSetConsoleScreenBufferInfoEx = (PSetConsoleScreenBufferInfoEx)
GetProcAddress(GetModuleHandle("kernel32.dll"), "SetConsoleScreenBufferInfoEx");
/* Set the console font info */
pSetCurrentConsoleFontEx(hOutput, TRUE, &cfi);
/* Get the size of the structure before filling the struct */
cbi.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
pGetConsoleScreenBufferInfoEx(hOutput, &cbi);
/* Loop through the given palette, copying the data into the color table */
for (i = 0 ; i < 16 ; ++i)
{
cbi.ColorTable[i] = palette[i];
}
/* Set the console screen buffer info */
pSetConsoleScreenBufferInfoEx(hOutput, &cbi);
}
else /* We don't have access to the vista functions */
{
int i;
CONSOLE_INFO ci = { sizeof(ci) };
HWND hwndConsole = GetConsoleWindow();
GetConsoleSizeInfo(&ci, hOutput);
/* Set to 0 to keep current settings */
ci.FontSize.X = fontX;
ci.FontSize.Y = fontY;
ci.FontFamily = 0; /* Set to 0x30 for Terminal font */
ci.FontWeight = 0;
lstrcpyW(ci.FaceName, fontName);
ci.CursorSize = 100;
ci.FullScreen = FALSE;
ci.QuickEdit = FALSE;
ci.AutoPosition = 0x10000;
ci.InsertMode = TRUE;
ci.ScreenColors = MAKEWORD(0x7, 0x0);
ci.PopupColors = MAKEWORD(0x5, 0xf);
ci.HistoryNoDup = TRUE;
ci.HistoryBufferSize = 50;
ci.NumberOfHistoryBuffers = 4;
for(i = 0; i < 16; i++)
{
ci.ColorTable[i] = palette[i];
}
ci.CodePage = 0;
ci.Hwnd = hwndConsole;
lstrcpyW(ci.ConsoleTitle, L"");
SetConsoleInfo(hwndConsole, &ci);
}
}
/******************************************************************************
GetConsoleSizeInfo (XP only)
Fills up some info about the console font in the CONSOLE_INFO struct.
******************************************************************************/
static void GetConsoleSizeInfo(CONSOLE_INFO *pci, HANDLE hOutput)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hOutput, &csbi);
pci->ScreenBufferSize = csbi.dwSize;
pci->WindowSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
pci->WindowSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
pci->WindowPosX = csbi.srWindow.Left;
pci->WindowPosY = csbi.srWindow.Top;
}
/******************************************************************************
SetConsoleInfo (XP only)
Ends up sending a message to windows to reset the console info.
******************************************************************************/
BOOL SetConsoleInfo(HWND hwndConsole, CONSOLE_INFO *pci)
{
DWORD dwConsoleOwnerPid;
HANDLE hProcess;
HANDLE hSection, hDupSection;
PVOID ptrView = 0;
HANDLE hThread;
/* Open the process which "owns" the console */
GetWindowThreadProcessId(hwndConsole, &dwConsoleOwnerPid);
hProcess = OpenProcess(MAXIMUM_ALLOWED, FALSE, dwConsoleOwnerPid);
/* Create a SECTION object backed by page-file, then map a view of */
/* this section into the owner process so we can write the contents */
/* of the CONSOLE_INFO buffer into it */
hSection = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, pci->Length, 0);
/* Copy our console structure into the section-object */
ptrView = MapViewOfFile(hSection, FILE_MAP_WRITE|FILE_MAP_READ, 0, 0, pci->Length);
memcpy(ptrView, pci, pci->Length);
UnmapViewOfFile(ptrView);
/* Map the memory into owner process */
DuplicateHandle(GetCurrentProcess(), hSection, hProcess, &hDupSection, 0, FALSE, DUPLICATE_SAME_ACCESS);
/* Send console window the "update" message WM_SETCONSOLEINFO */
SendMessage(hwndConsole, WM_SETCONSOLEINFO, (WPARAM)hDupSection, 0);
/*clean up */
hThread = CreateRemoteThread(hProcess, 0, 0, (LPTHREAD_START_ROUTINE)CloseHandle, hDupSection, 0, 0);
CloseHandle(hThread);
CloseHandle(hSection);
CloseHandle(hProcess);
return TRUE;
}
/***************************************************************************
* bound_check
*
* ensures that variables x and y are within the boundaries of the console window,
* and changes them to be so if they are not
***************************************************************************/
void bound_check(int &x, int &y, Image *image) {
if (x < 0)
x = 0;
if (x > WIDTH - image->getWidth())
x = WIDTH - image->getWidth();
if (y < 0)
y = 0;
if (y > HEIGHT - image->getHeight())
y = HEIGHT - image->getHeight();
}
/***************************************************************************
* initializeConsole
*
* sets console's title, font size and font name, as well as setting the palette
* to the values of palette in the function - defaults to Windows default RGB,
* search MSDN for RGB macros to learn more about changing these values
*
* note - also initializes default background to blue, use any of the provided
* palette values or FOREGROUND_RED || FOREGROUND_GREEN || FOREGROUND_BLUE to change
***************************************************************************/
void initializeConsole(char *title, int fontX, int fontY, wchar_t *fontName) {
wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
rHnd = GetStdHandle(STD_INPUT_HANDLE);
COLORREF palette[16] =
{
0x00000000, 0x00800000, 0x00008000, 0x00808000,
0x00000080, 0x00800080, 0x00008080, 0x00c0c0c0,
0x00808080, 0x00ff0000, 0x0000ff00, 0x00ffff00,
0x000000ff, 0x00ff00ff, 0x0000ffff, 0x00ffffff
};
SetConsoleTitle(title);
SetConsolePalette(palette, fontX, fontY, fontName);
COORD bufferSize = {WIDTH, HEIGHT};
SMALL_RECT windowSize = {0, 0, WIDTH - 1, HEIGHT - 1};
SetConsoleScreenBufferSize(wHnd, bufferSize);
SetConsoleWindowInfo(wHnd, TRUE, &windowSize);
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
consoleBuffer[x + WIDTH * y].Char.AsciiChar = (unsigned char)219;
consoleBuffer[x + WIDTH * y].Attributes = FOREGROUND_BLUE;
}
}
}
COORD characterBufferSize = {WIDTH, HEIGHT};
COORD characterPosition = {0, 0};
SMALL_RECT consoleWriteArea = {0, 0, WIDTH - 1, HEIGHT - 1};