1 /***********************************************************************\
2 *                                wincon.d                               *
3 *                                                                       *
4 *                       Windows API header module                       *
5 *                                                                       *
6 *                 Translated from MinGW Windows headers                 *
7 *                                                                       *
8 *                       Placed into public domain                       *
9 \***********************************************************************/
10 module windows.wincon;
11 pragma(lib, "kernel32");
12 nothrow:
13 private import windows.w32api, windows.windef;
14 
15 // FIXME: clean up Windows version support
16 
17 enum {
18 	FOREGROUND_BLUE      = 1,
19 	FOREGROUND_GREEN     = 2,
20 	FOREGROUND_RED       = 4,
21 	FOREGROUND_INTENSITY = 8,
22 	BACKGROUND_BLUE      = 16,
23 	BACKGROUND_GREEN     = 32,
24 	BACKGROUND_RED       = 64,
25 	BACKGROUND_INTENSITY = 128
26 }
27 
28 static if (_WIN32_WINNT >= 0x501) {
29 	enum {
30 		CONSOLE_FULLSCREEN_MODE = 1,
31 		CONSOLE_WINDOWED_MODE   = 0
32 	}
33 }
34 
35 enum {
36 	CTRL_C_EVENT        = 0,
37 	CTRL_BREAK_EVENT    = 1,
38 	CTRL_CLOSE_EVENT    = 2,
39 	CTRL_LOGOFF_EVENT   = 5,
40 	CTRL_SHUTDOWN_EVENT = 6
41 }
42 
43 enum {
44 	ENABLE_PROCESSED_INPUT  = 1,
45 	ENABLE_LINE_INPUT       = 2,
46 	ENABLE_ECHO_INPUT       = 4,
47 	ENABLE_WINDOW_INPUT     = 8,
48 	ENABLE_MOUSE_INPUT      = 16
49 }
50 
51 enum {
52 	ENABLE_PROCESSED_OUTPUT    = 1,
53 	ENABLE_WRAP_AT_EOL_OUTPUT  = 2
54 }
55 
56 enum {
57 	KEY_EVENT                 = 1,
58 	MOUSE_EVENT               = 2,
59 	WINDOW_BUFFER_SIZE_EVENT  = 4,
60 	MENU_EVENT                = 8,
61 	FOCUS_EVENT               = 16
62 }
63 enum {
64 	RIGHT_ALT_PRESSED  = 1,
65 	LEFT_ALT_PRESSED   = 2,
66 	RIGHT_CTRL_PRESSED = 4,
67 	LEFT_CTRL_PRESSED  = 8,
68 	SHIFT_PRESSED      = 16,
69 	NUMLOCK_ON         = 32,
70 	SCROLLLOCK_ON      = 64,
71 	CAPSLOCK_ON        = 128,
72 	ENHANCED_KEY       = 256
73 }
74 enum {
75 	FROM_LEFT_1ST_BUTTON_PRESSED  = 1,
76 	RIGHTMOST_BUTTON_PRESSED      = 2,
77 	FROM_LEFT_2ND_BUTTON_PRESSED  = 4,
78 	FROM_LEFT_3RD_BUTTON_PRESSED  = 8,
79 	FROM_LEFT_4TH_BUTTON_PRESSED  = 16
80 }
81 
82 enum {
83 	MOUSE_MOVED   = 1,
84 	DOUBLE_CLICK  = 2,
85 	MOUSE_WHEELED = 4
86 }
87 
88 struct CHAR_INFO {
89 	union _Char {
90 		WCHAR UnicodeChar;
91 		CHAR AsciiChar;
92 	}
93 	_Char Char;	
94 	WORD Attributes;
95 }
96 alias CHAR_INFO* PCHAR_INFO;
97 
98 struct SMALL_RECT {
99 	SHORT Left;
100 	SHORT Top;
101 	SHORT Right;
102 	SHORT Bottom;
103 }
104 alias SMALL_RECT* PSMALL_RECT;
105 
106 struct CONSOLE_CURSOR_INFO {
107 	DWORD dwSize;
108 	BOOL  bVisible;
109 }
110 alias CONSOLE_CURSOR_INFO* PCONSOLE_CURSOR_INFO;
111 
112 struct COORD {
113 	SHORT X;
114 	SHORT Y;
115 }
116 alias COORD* PCOORD;
117 
118 struct CONSOLE_FONT_INFO {
119 	DWORD nFont;
120 	COORD dwFontSize;
121 }
122 alias CONSOLE_FONT_INFO* PCONSOLE_FONT_INFO;
123 
124 struct CONSOLE_SCREEN_BUFFER_INFO {
125 	COORD      dwSize;
126 	COORD      dwCursorPosition;
127 	WORD       wAttributes;
128 	SMALL_RECT srWindow;
129 	COORD      dwMaximumWindowSize;
130 }
131 alias CONSOLE_SCREEN_BUFFER_INFO* PCONSOLE_SCREEN_BUFFER_INFO;
132 
133 alias BOOL function(DWORD) PHANDLER_ROUTINE;
134 
135 struct KEY_EVENT_RECORD {
136 	BOOL  bKeyDown;
137 	WORD  wRepeatCount;
138 	WORD  wVirtualKeyCode;
139 	WORD  wVirtualScanCode;
140 	union _uChar {
141 		WCHAR UnicodeChar;
142 		CHAR  AsciiChar;
143 	}
144 	_uChar uChar;	
145 	DWORD dwControlKeyState;
146 }
147 
148 struct MOUSE_EVENT_RECORD {
149 	COORD dwMousePosition;
150 	DWORD dwButtonState;
151 	DWORD dwControlKeyState;
152 	DWORD dwEventFlags;
153 }
154 
155 struct WINDOW_BUFFER_SIZE_RECORD {
156 	COORD dwSize;
157 }
158 
159 struct MENU_EVENT_RECORD {
160 	UINT dwCommandId;
161 }
162 alias MENU_EVENT_RECORD* PMENU_EVENT_RECORD;
163 
164 struct FOCUS_EVENT_RECORD {
165 	BOOL bSetFocus;
166 }
167 
168 struct INPUT_RECORD {
169 	WORD EventType;
170 	union _Event {
171 		KEY_EVENT_RECORD KeyEvent;
172 		MOUSE_EVENT_RECORD MouseEvent;
173 		WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent;
174 		MENU_EVENT_RECORD MenuEvent;
175 		FOCUS_EVENT_RECORD FocusEvent;
176 	}
177 	_Event Event;	
178 }
179 alias INPUT_RECORD* PINPUT_RECORD;
180 
181 extern (Windows):
182 
183 BOOL AllocConsole();
184 HANDLE CreateConsoleScreenBuffer(DWORD, DWORD, SECURITY_ATTRIBUTES*, DWORD, LPVOID);
185 BOOL FillConsoleOutputAttribute(HANDLE, WORD, DWORD, COORD, PDWORD);
186 BOOL FillConsoleOutputCharacterA(HANDLE, CHAR, DWORD, COORD, PDWORD);
187 BOOL FillConsoleOutputCharacterW(HANDLE, WCHAR, DWORD, COORD, PDWORD);
188 BOOL FlushConsoleInputBuffer(HANDLE);
189 BOOL FreeConsole();
190 BOOL GenerateConsoleCtrlEvent(DWORD, DWORD);
191 UINT GetConsoleCP();
192 BOOL GetConsoleCursorInfo(HANDLE, PCONSOLE_CURSOR_INFO);
193 BOOL GetConsoleMode(HANDLE,PDWORD);
194 UINT GetConsoleOutputCP();
195 BOOL GetConsoleScreenBufferInfo(HANDLE, PCONSOLE_SCREEN_BUFFER_INFO);
196 DWORD GetConsoleTitleA(LPSTR, DWORD);
197 DWORD GetConsoleTitleW(LPWSTR, DWORD);
198 COORD GetLargestConsoleWindowSize(HANDLE);
199 BOOL GetNumberOfConsoleInputEvents(HANDLE, PDWORD);
200 BOOL GetNumberOfConsoleMouseButtons(PDWORD);
201 BOOL PeekConsoleInputA(HANDLE, PINPUT_RECORD, DWORD, PDWORD);
202 BOOL PeekConsoleInputW(HANDLE, PINPUT_RECORD, DWORD, PDWORD);
203 BOOL ReadConsoleA(HANDLE, PVOID, DWORD, PDWORD, PVOID);
204 BOOL ReadConsoleW(HANDLE, PVOID, DWORD, PDWORD, PVOID);
205 BOOL ReadConsoleInputA(HANDLE, PINPUT_RECORD, DWORD, PDWORD);
206 BOOL ReadConsoleInputW(HANDLE, PINPUT_RECORD, DWORD, PDWORD);
207 BOOL ReadConsoleOutputAttribute(HANDLE, LPWORD, DWORD, COORD, LPDWORD);
208 BOOL ReadConsoleOutputCharacterA(HANDLE, LPSTR, DWORD, COORD, PDWORD);
209 BOOL ReadConsoleOutputCharacterW(HANDLE, LPWSTR, DWORD, COORD, PDWORD);
210 BOOL ReadConsoleOutputA(HANDLE, PCHAR_INFO, COORD, COORD, PSMALL_RECT);
211 BOOL ReadConsoleOutputW(HANDLE, PCHAR_INFO, COORD, COORD, PSMALL_RECT);
212 BOOL ScrollConsoleScreenBufferA(HANDLE, const(SMALL_RECT)*, const(SMALL_RECT)*, COORD, const(CHAR_INFO)*);
213 BOOL ScrollConsoleScreenBufferW(HANDLE, const(SMALL_RECT)*, const(SMALL_RECT)*, COORD, const(CHAR_INFO)*);
214 BOOL SetConsoleActiveScreenBuffer(HANDLE);
215 BOOL SetConsoleCP(UINT);
216 BOOL SetConsoleCtrlHandler(PHANDLER_ROUTINE, BOOL);
217 BOOL SetConsoleCursorInfo(HANDLE, const(CONSOLE_CURSOR_INFO)*);
218 BOOL SetConsoleCursorPosition(HANDLE, COORD);
219 
220 
221 static if (_WIN32_WINNT >= 0x500) {
222 BOOL GetConsoleDisplayMode(LPDWORD);
223 HWND GetConsoleWindow();
224 }
225 
226 static if (_WIN32_WINNT >= 0x501) {
227 BOOL AttachConsole(DWORD);
228 BOOL SetConsoleDisplayMode(HANDLE, DWORD, PCOORD);
229 const DWORD ATTACH_PARENT_PROCESS = cast(DWORD)-1;
230 }
231 
232 BOOL SetConsoleMode(HANDLE, DWORD);
233 BOOL SetConsoleOutputCP(UINT);
234 BOOL SetConsoleScreenBufferSize(HANDLE, COORD);
235 BOOL SetConsoleTextAttribute(HANDLE, WORD);
236 BOOL SetConsoleTitleA(LPCSTR);
237 BOOL SetConsoleTitleW(LPCWSTR);
238 BOOL SetConsoleWindowInfo(HANDLE, BOOL, const(SMALL_RECT)*);
239 BOOL WriteConsoleA(HANDLE, PCVOID, DWORD, PDWORD, PVOID);
240 BOOL WriteConsoleW(HANDLE, PCVOID, DWORD, PDWORD, PVOID);
241 BOOL WriteConsoleInputA(HANDLE, const(INPUT_RECORD)*, DWORD, PDWORD);
242 BOOL WriteConsoleInputW(HANDLE, const(INPUT_RECORD)*, DWORD, PDWORD);
243 BOOL WriteConsoleOutputA(HANDLE, const(CHAR_INFO)*, COORD, COORD, PSMALL_RECT);
244 BOOL WriteConsoleOutputW(HANDLE, const(CHAR_INFO)*, COORD, COORD, PSMALL_RECT);
245 BOOL WriteConsoleOutputAttribute(HANDLE, const(WORD)*, DWORD, COORD, PDWORD);
246 BOOL WriteConsoleOutputCharacterA(HANDLE, LPCSTR, DWORD, COORD, PDWORD);
247 BOOL WriteConsoleOutputCharacterW(HANDLE, LPCWSTR, DWORD, COORD, PDWORD);
248 
249 version (Unicode) {
250 	alias FillConsoleOutputCharacterW FillConsoleOutputCharacter;
251 	alias GetConsoleTitleW GetConsoleTitle;
252 	alias PeekConsoleInputW PeekConsoleInput;
253 	alias ReadConsoleW ReadConsole;
254 	alias ReadConsoleInputW ReadConsoleInput;
255 	alias ReadConsoleOutputW ReadConsoleOutput;
256 	alias ReadConsoleOutputCharacterW ReadConsoleOutputCharacter;
257 	alias ScrollConsoleScreenBufferW ScrollConsoleScreenBuffer;
258 	alias SetConsoleTitleW SetConsoleTitle;
259 	alias WriteConsoleW WriteConsole;
260 	alias WriteConsoleInputW WriteConsoleInput;
261 	alias WriteConsoleOutputW WriteConsoleOutput;
262 	alias WriteConsoleOutputCharacterW WriteConsoleOutputCharacter;
263 } else {
264 	alias FillConsoleOutputCharacterA FillConsoleOutputCharacter;
265 	alias GetConsoleTitleA GetConsoleTitle;
266 	alias PeekConsoleInputA PeekConsoleInput;
267 	alias ReadConsoleA ReadConsole;
268 	alias ReadConsoleInputA ReadConsoleInput;
269 	alias ReadConsoleOutputA ReadConsoleOutput;
270 	alias ReadConsoleOutputCharacterA ReadConsoleOutputCharacter;
271 	alias ScrollConsoleScreenBufferA ScrollConsoleScreenBuffer;
272 	alias SetConsoleTitleA SetConsoleTitle;
273 	alias WriteConsoleA WriteConsole;
274 	alias WriteConsoleInputA WriteConsoleInput;
275 	alias WriteConsoleOutputA WriteConsoleOutput;
276 	alias WriteConsoleOutputCharacterA WriteConsoleOutputCharacter;
277 }