Bring back ext2 code from branch
[reactos.git] / reactos / base / setup / usetup / interface / consup.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS text-mode setup
22 * FILE: subsys/system/usetup/console.c
23 * PURPOSE: Console support functions
24 * PROGRAMMER: Eric Kohl
25 */
26
27 /* INCLUDES ******************************************************************/
28
29 #include "usetup.h"
30
31 #define NDEBUG
32 #include <debug.h>
33
34 /* GLOBALS ******************************************************************/
35
36 HANDLE StdInput = INVALID_HANDLE_VALUE;
37 HANDLE StdOutput = INVALID_HANDLE_VALUE;
38
39 SHORT xScreen = 0;
40 SHORT yScreen = 0;
41
42 /* FUNCTIONS *****************************************************************/
43
44 BOOLEAN
45 CONSOLE_Init(
46 VOID)
47 {
48 CONSOLE_SCREEN_BUFFER_INFO csbi;
49 if (!HOST_InitConsole())
50 return FALSE;
51
52 StdInput = GetStdHandle(STD_INPUT_HANDLE);
53 StdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
54 if (!GetConsoleScreenBufferInfo(StdOutput, &csbi))
55 return FALSE;
56 xScreen = csbi.dwSize.X;
57 yScreen = 50;//csbi.dwSize.Y;
58 return TRUE;
59 }
60
61 VOID
62 CONSOLE_ConInKey(
63 OUT PINPUT_RECORD Buffer)
64 {
65 DWORD Read;
66
67 while (TRUE)
68 {
69 ReadConsoleInput(StdInput, Buffer, 1, &Read);
70
71 if ((Buffer->EventType == KEY_EVENT)
72 && (Buffer->Event.KeyEvent.bKeyDown == TRUE))
73 break;
74 }
75 }
76
77 VOID
78 CONSOLE_ConOutChar(
79 IN CHAR c)
80 {
81 DWORD Written;
82
83 WriteConsole(
84 StdOutput,
85 &c,
86 1,
87 &Written,
88 NULL);
89 }
90
91 VOID
92 CONSOLE_ConOutPuts(
93 IN LPCSTR szText)
94 {
95 DWORD Written;
96
97 WriteConsole(
98 StdOutput,
99 szText,
100 (ULONG)strlen(szText),
101 &Written,
102 NULL);
103 WriteConsole(
104 StdOutput,
105 "\n",
106 1,
107 &Written,
108 NULL);
109 }
110
111 VOID
112 CONSOLE_ConOutPrintf(
113 IN LPCSTR szFormat, ...)
114 {
115 CHAR szOut[256];
116 DWORD dwWritten;
117 va_list arg_ptr;
118
119 va_start(arg_ptr, szFormat);
120 vsprintf(szOut, szFormat, arg_ptr);
121 va_end(arg_ptr);
122
123 WriteConsole(
124 StdOutput,
125 szOut,
126 (ULONG)strlen(szOut),
127 &dwWritten,
128 NULL);
129 }
130
131 BOOL
132 CONSOLE_Flush(VOID)
133 {
134 return FlushConsoleInputBuffer(StdInput);
135 }
136
137 SHORT
138 CONSOLE_GetCursorX(VOID)
139 {
140 CONSOLE_SCREEN_BUFFER_INFO csbi;
141
142 GetConsoleScreenBufferInfo(StdOutput, &csbi);
143
144 return csbi.dwCursorPosition.X;
145 }
146
147 SHORT
148 CONSOLE_GetCursorY(VOID)
149 {
150 CONSOLE_SCREEN_BUFFER_INFO csbi;
151
152 GetConsoleScreenBufferInfo(StdOutput, &csbi);
153
154 return csbi.dwCursorPosition.Y;
155 }
156
157 VOID
158 CONSOLE_SetCursorType(
159 IN BOOL bInsert,
160 IN BOOL bVisible)
161 {
162 CONSOLE_CURSOR_INFO cci;
163
164 cci.dwSize = bInsert ? 10 : 99;
165 cci.bVisible = bVisible;
166
167 SetConsoleCursorInfo(StdOutput, &cci);
168 }
169
170 VOID
171 CONSOLE_SetCursorXY(
172 IN SHORT x,
173 IN SHORT y)
174 {
175 COORD coPos;
176
177 coPos.X = x;
178 coPos.Y = y;
179 SetConsoleCursorPosition(StdOutput, coPos);
180 }
181
182 VOID
183 CONSOLE_ClearScreen(VOID)
184 {
185 COORD coPos;
186 DWORD Written;
187
188 coPos.X = 0;
189 coPos.Y = 0;
190
191 FillConsoleOutputAttribute(
192 StdOutput,
193 FOREGROUND_WHITE | BACKGROUND_BLUE,
194 xScreen * yScreen,
195 coPos,
196 &Written);
197
198 FillConsoleOutputCharacterA(
199 StdOutput,
200 ' ',
201 xScreen * yScreen,
202 coPos,
203 &Written);
204 }
205
206 VOID
207 CONSOLE_SetStatusText(
208 IN LPCSTR fmt, ...)
209 {
210 CHAR Buffer[128];
211 va_list ap;
212 COORD coPos;
213 DWORD Written;
214
215 va_start(ap, fmt);
216 vsprintf(Buffer, fmt, ap);
217 va_end(ap);
218
219 coPos.X = 0;
220 coPos.Y = yScreen - 1;
221
222 FillConsoleOutputAttribute(
223 StdOutput,
224 BACKGROUND_WHITE,
225 xScreen,
226 coPos,
227 &Written);
228
229 FillConsoleOutputCharacterA(
230 StdOutput,
231 ' ',
232 xScreen,
233 coPos,
234 &Written);
235
236 WriteConsoleOutputCharacterA(
237 StdOutput,
238 Buffer,
239 (ULONG)strlen(Buffer),
240 coPos,
241 &Written);
242 }
243
244 VOID
245 CONSOLE_InvertTextXY(
246 IN SHORT x,
247 IN SHORT y,
248 IN SHORT col,
249 IN SHORT row)
250 {
251 COORD coPos;
252 DWORD Written;
253
254 for (coPos.Y = y; coPos.Y < y + row; coPos.Y++)
255 {
256 coPos.X = x;
257
258 FillConsoleOutputAttribute(
259 StdOutput,
260 FOREGROUND_BLUE | BACKGROUND_WHITE,
261 col,
262 coPos,
263 &Written);
264 }
265 }
266
267 VOID
268 CONSOLE_NormalTextXY(
269 IN SHORT x,
270 IN SHORT y,
271 IN SHORT col,
272 IN SHORT row)
273 {
274 COORD coPos;
275 DWORD Written;
276
277 for (coPos.Y = y; coPos.Y < y + row; coPos.Y++)
278 {
279 coPos.X = x;
280
281 FillConsoleOutputAttribute(
282 StdOutput,
283 FOREGROUND_WHITE | BACKGROUND_BLUE,
284 col,
285 coPos,
286 &Written);
287 }
288 }
289
290 VOID
291 CONSOLE_SetTextXY(
292 IN SHORT x,
293 IN SHORT y,
294 IN LPCSTR Text)
295 {
296 COORD coPos;
297 DWORD Written;
298
299 coPos.X = x;
300 coPos.Y = y;
301
302 WriteConsoleOutputCharacterA(
303 StdOutput,
304 Text,
305 (ULONG)strlen(Text),
306 coPos,
307 &Written);
308 }
309
310 VOID
311 CONSOLE_SetInputTextXY(
312 IN SHORT x,
313 IN SHORT y,
314 IN SHORT len,
315 IN LPCWSTR Text)
316 {
317 COORD coPos;
318 SHORT Length;
319 DWORD Written;
320
321 coPos.X = x;
322 coPos.Y = y;
323
324 Length = (SHORT)wcslen(Text);
325 if (Length > len - 1)
326 Length = len - 1;
327
328 FillConsoleOutputAttribute(
329 StdOutput,
330 BACKGROUND_WHITE,
331 len,
332 coPos,
333 &Written);
334
335 WriteConsoleOutputCharacterW(
336 StdOutput,
337 Text,
338 (ULONG)Length,
339 coPos,
340 &Written);
341
342 coPos.X += Length;
343 FillConsoleOutputCharacterA(
344 StdOutput,
345 '_',
346 1,
347 coPos,
348 &Written);
349
350 if (len > Length + 1)
351 {
352 coPos.X++;
353 FillConsoleOutputCharacterA(
354 StdOutput,
355 ' ',
356 len - Length - 1,
357 coPos,
358 &Written);
359 }
360 }
361
362 VOID
363 CONSOLE_SetUnderlinedTextXY(
364 IN SHORT x,
365 IN SHORT y,
366 IN LPCSTR Text)
367 {
368 COORD coPos;
369 DWORD Length;
370 DWORD Written;
371
372 coPos.X = x;
373 coPos.Y = y;
374
375 Length = (ULONG)strlen(Text);
376
377 WriteConsoleOutputCharacterA(
378 StdOutput,
379 Text,
380 Length,
381 coPos,
382 &Written);
383
384 coPos.Y++;
385 FillConsoleOutputCharacterA(
386 StdOutput,
387 0xCD,
388 Length,
389 coPos,
390 &Written);
391 }
392
393 VOID
394 CONSOLE_SetInvertedTextXY(
395 IN SHORT x,
396 IN SHORT y,
397 IN LPCSTR Text)
398 {
399 COORD coPos;
400 DWORD Length;
401 DWORD Written;
402
403 coPos.X = x;
404 coPos.Y = y;
405
406 Length = (ULONG)strlen(Text);
407
408 FillConsoleOutputAttribute(
409 StdOutput,
410 FOREGROUND_BLUE | BACKGROUND_WHITE,
411 Length,
412 coPos,
413 &Written);
414
415 WriteConsoleOutputCharacterA(
416 StdOutput,
417 Text,
418 Length,
419 coPos,
420 &Written);
421 }
422
423 VOID
424 CONSOLE_SetHighlightedTextXY(
425 IN SHORT x,
426 IN SHORT y,
427 IN LPCSTR Text)
428 {
429 COORD coPos;
430 DWORD Length;
431 DWORD Written;
432
433 coPos.X = x;
434 coPos.Y = y;
435
436 Length = (ULONG)strlen(Text);
437
438 FillConsoleOutputAttribute(
439 StdOutput,
440 FOREGROUND_WHITE | FOREGROUND_INTENSITY | BACKGROUND_BLUE,
441 Length,
442 coPos,
443 &Written);
444
445 WriteConsoleOutputCharacterA(
446 StdOutput,
447 Text,
448 Length,
449 coPos,
450 &Written);
451 }
452
453 VOID
454 CONSOLE_PrintTextXY(
455 IN SHORT x,
456 IN SHORT y,
457 IN LPCSTR fmt, ...)
458 {
459 CHAR buffer[512];
460 va_list ap;
461 COORD coPos;
462 DWORD Written;
463
464 va_start(ap, fmt);
465 vsprintf(buffer, fmt, ap);
466 va_end(ap);
467
468 coPos.X = x;
469 coPos.Y = y;
470
471 WriteConsoleOutputCharacterA(
472 StdOutput,
473 buffer,
474 (ULONG)strlen(buffer),
475 coPos,
476 &Written);
477 }
478
479 VOID
480 CONSOLE_PrintTextXYN(
481 IN SHORT x,
482 IN SHORT y,
483 IN SHORT len,
484 IN LPCSTR fmt, ...)
485 {
486 CHAR buffer[512];
487 va_list ap;
488 COORD coPos;
489 SHORT Length;
490 DWORD Written;
491
492 va_start(ap, fmt);
493 vsprintf(buffer, fmt, ap);
494 va_end(ap);
495
496 coPos.X = x;
497 coPos.Y = y;
498
499 Length = (SHORT)strlen(buffer);
500 if (Length > len - 1)
501 Length = len - 1;
502
503 WriteConsoleOutputCharacterA(
504 StdOutput,
505 buffer,
506 Length,
507 coPos,
508 &Written);
509
510 coPos.X += Length;
511
512 if (len > Length)
513 {
514 FillConsoleOutputCharacterA(
515 StdOutput,
516 ' ',
517 len - Length,
518 coPos,
519 &Written);
520 }
521 }
522
523 /* EOF */