[MKHIVE/USETUP]
[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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_InvertTextXY(
208 IN SHORT x,
209 IN SHORT y,
210 IN SHORT col,
211 IN SHORT row)
212 {
213 COORD coPos;
214 DWORD Written;
215
216 for (coPos.Y = y; coPos.Y < y + row; coPos.Y++)
217 {
218 coPos.X = x;
219
220 FillConsoleOutputAttribute(
221 StdOutput,
222 FOREGROUND_BLUE | BACKGROUND_WHITE,
223 col,
224 coPos,
225 &Written);
226 }
227 }
228
229 VOID
230 CONSOLE_NormalTextXY(
231 IN SHORT x,
232 IN SHORT y,
233 IN SHORT col,
234 IN SHORT row)
235 {
236 COORD coPos;
237 DWORD Written;
238
239 for (coPos.Y = y; coPos.Y < y + row; coPos.Y++)
240 {
241 coPos.X = x;
242
243 FillConsoleOutputAttribute(
244 StdOutput,
245 FOREGROUND_WHITE | BACKGROUND_BLUE,
246 col,
247 coPos,
248 &Written);
249 }
250 }
251
252 VOID
253 CONSOLE_SetTextXY(
254 IN SHORT x,
255 IN SHORT y,
256 IN LPCSTR Text)
257 {
258 COORD coPos;
259 DWORD Written;
260
261 coPos.X = x;
262 coPos.Y = y;
263
264 WriteConsoleOutputCharacterA(
265 StdOutput,
266 Text,
267 (ULONG)strlen(Text),
268 coPos,
269 &Written);
270 }
271
272 VOID
273 CONSOLE_SetInputTextXY(
274 IN SHORT x,
275 IN SHORT y,
276 IN SHORT len,
277 IN LPCWSTR Text)
278 {
279 COORD coPos;
280 SHORT Length;
281 DWORD Written;
282
283 coPos.X = x;
284 coPos.Y = y;
285
286 Length = (SHORT)wcslen(Text);
287 if (Length > len - 1)
288 Length = len - 1;
289
290 FillConsoleOutputAttribute(
291 StdOutput,
292 BACKGROUND_WHITE,
293 len,
294 coPos,
295 &Written);
296
297 WriteConsoleOutputCharacterW(
298 StdOutput,
299 Text,
300 (ULONG)Length,
301 coPos,
302 &Written);
303
304 coPos.X += Length;
305 FillConsoleOutputCharacterA(
306 StdOutput,
307 '_',
308 1,
309 coPos,
310 &Written);
311
312 if (len > Length + 1)
313 {
314 coPos.X++;
315 FillConsoleOutputCharacterA(
316 StdOutput,
317 ' ',
318 len - Length - 1,
319 coPos,
320 &Written);
321 }
322 }
323
324 VOID
325 CONSOLE_SetUnderlinedTextXY(
326 IN SHORT x,
327 IN SHORT y,
328 IN LPCSTR Text)
329 {
330 COORD coPos;
331 DWORD Length;
332 DWORD Written;
333
334 coPos.X = x;
335 coPos.Y = y;
336
337 Length = (ULONG)strlen(Text);
338
339 WriteConsoleOutputCharacterA(
340 StdOutput,
341 Text,
342 Length,
343 coPos,
344 &Written);
345
346 coPos.Y++;
347 FillConsoleOutputCharacterA(
348 StdOutput,
349 0xCD,
350 Length,
351 coPos,
352 &Written);
353 }
354
355 VOID
356 CONSOLE_SetStatusText(
357 IN LPCSTR fmt, ...)
358 {
359 CHAR Buffer[128];
360 va_list ap;
361 COORD coPos;
362 DWORD Written;
363
364 va_start(ap, fmt);
365 vsprintf(Buffer, fmt, ap);
366 va_end(ap);
367
368 coPos.X = 0;
369 coPos.Y = yScreen - 1;
370
371 FillConsoleOutputAttribute(
372 StdOutput,
373 BACKGROUND_WHITE,
374 xScreen,
375 coPos,
376 &Written);
377
378 FillConsoleOutputCharacterA(
379 StdOutput,
380 ' ',
381 xScreen,
382 coPos,
383 &Written);
384
385 WriteConsoleOutputCharacterA(
386 StdOutput,
387 Buffer,
388 (ULONG)strlen(Buffer),
389 coPos,
390 &Written);
391 }
392
393 VOID
394 CONSOLE_SetStatusTextX(
395 IN SHORT x,
396 IN LPCSTR fmt, ...)
397 {
398 CHAR Buffer[128];
399 va_list ap;
400 COORD coPos;
401 DWORD Written;
402
403 va_start(ap, fmt);
404 vsprintf(Buffer, fmt, ap);
405 va_end(ap);
406
407 coPos.X = 0;
408 coPos.Y = yScreen - 1;
409
410 FillConsoleOutputAttribute(
411 StdOutput,
412 BACKGROUND_WHITE,
413 xScreen,
414 coPos,
415 &Written);
416
417 FillConsoleOutputCharacterA(
418 StdOutput,
419 ' ',
420 xScreen,
421 coPos,
422 &Written);
423
424 coPos.X = x;
425
426 WriteConsoleOutputCharacterA(
427 StdOutput,
428 Buffer,
429 (ULONG)strlen(Buffer),
430 coPos,
431 &Written);
432 }
433
434 VOID
435 CONSOLE_SetStatusTextAutoFitX(
436 IN SHORT x,
437 IN LPCSTR fmt, ...)
438 {
439 CHAR Buffer[128];
440 DWORD Length;
441 va_list ap;
442
443 va_start(ap, fmt);
444 vsprintf(Buffer, fmt, ap);
445 va_end(ap);
446
447 Length = (ULONG)strlen(Buffer);
448
449 if (Length + x <= 79)
450 {
451 CONSOLE_SetStatusTextX (x , Buffer);
452 }
453 else
454 {
455 CONSOLE_SetStatusTextX (79 - Length , Buffer);
456 }
457 }
458
459 VOID
460 CONSOLE_SetInvertedTextXY(
461 IN SHORT x,
462 IN SHORT y,
463 IN LPCSTR Text)
464 {
465 COORD coPos;
466 DWORD Length;
467 DWORD Written;
468
469 coPos.X = x;
470 coPos.Y = y;
471
472 Length = (ULONG)strlen(Text);
473
474 FillConsoleOutputAttribute(
475 StdOutput,
476 FOREGROUND_BLUE | BACKGROUND_WHITE,
477 Length,
478 coPos,
479 &Written);
480
481 WriteConsoleOutputCharacterA(
482 StdOutput,
483 Text,
484 Length,
485 coPos,
486 &Written);
487 }
488
489 VOID
490 CONSOLE_SetHighlightedTextXY(
491 IN SHORT x,
492 IN SHORT y,
493 IN LPCSTR Text)
494 {
495 COORD coPos;
496 DWORD Length;
497 DWORD Written;
498
499 coPos.X = x;
500 coPos.Y = y;
501
502 Length = (ULONG)strlen(Text);
503
504 FillConsoleOutputAttribute(
505 StdOutput,
506 FOREGROUND_WHITE | FOREGROUND_INTENSITY | BACKGROUND_BLUE,
507 Length,
508 coPos,
509 &Written);
510
511 WriteConsoleOutputCharacterA(
512 StdOutput,
513 Text,
514 Length,
515 coPos,
516 &Written);
517 }
518
519 VOID
520 CONSOLE_PrintTextXY(
521 IN SHORT x,
522 IN SHORT y,
523 IN LPCSTR fmt, ...)
524 {
525 CHAR buffer[512];
526 va_list ap;
527 COORD coPos;
528 DWORD Written;
529
530 va_start(ap, fmt);
531 vsprintf(buffer, fmt, ap);
532 va_end(ap);
533
534 coPos.X = x;
535 coPos.Y = y;
536
537 WriteConsoleOutputCharacterA(
538 StdOutput,
539 buffer,
540 (ULONG)strlen(buffer),
541 coPos,
542 &Written);
543 }
544
545 VOID
546 CONSOLE_PrintTextXYN(
547 IN SHORT x,
548 IN SHORT y,
549 IN SHORT len,
550 IN LPCSTR fmt, ...)
551 {
552 CHAR buffer[512];
553 va_list ap;
554 COORD coPos;
555 SHORT Length;
556 DWORD Written;
557
558 va_start(ap, fmt);
559 vsprintf(buffer, fmt, ap);
560 va_end(ap);
561
562 coPos.X = x;
563 coPos.Y = y;
564
565 Length = (SHORT)strlen(buffer);
566 if (Length > len - 1)
567 Length = len - 1;
568
569 WriteConsoleOutputCharacterA(
570 StdOutput,
571 buffer,
572 Length,
573 coPos,
574 &Written);
575
576 coPos.X += Length;
577
578 if (len > Length)
579 {
580 FillConsoleOutputCharacterA(
581 StdOutput,
582 ' ',
583 len - Length,
584 coPos,
585 &Written);
586 }
587 }
588
589 VOID
590 CONSOLE_SetStyledText(
591 IN SHORT x,
592 IN SHORT y,
593 IN INT Flags,
594 IN LPCSTR Text)
595 {
596 COORD coPos;
597 DWORD Length;
598
599 coPos.X = x;
600 coPos.Y = y;
601
602 Length = (ULONG)strlen(Text);
603
604 if (Flags & TEXT_TYPE_STATUS)
605 {
606 coPos.X = x;
607 coPos.Y = yScreen - 1;
608 }
609 else /* TEXT_TYPE_REGULAR (Default) */
610 {
611 coPos.X = x;
612 coPos.Y = y;
613 }
614
615 if (Flags & TEXT_ALIGN_CENTER)
616 {
617 coPos.X = (xScreen - Length) /2;
618 }
619 else if(Flags & TEXT_ALIGN_RIGHT)
620 {
621 coPos.X = coPos.X - Length;
622
623 if (Flags & TEXT_PADDING_SMALL)
624 {
625 coPos.X -= 1;
626 }
627 else if (Flags & TEXT_PADDING_MEDIUM)
628 {
629 coPos.X -= 2;
630 }
631 else if (Flags & TEXT_PADDING_BIG)
632 {
633 coPos.X -= 3;
634 }
635 }
636 else /* TEXT_ALIGN_LEFT (Default) */
637 {
638 if (Flags & TEXT_PADDING_SMALL)
639 {
640 coPos.X += 1;
641 }
642 else if (Flags & TEXT_PADDING_MEDIUM)
643 {
644 coPos.X += 2;
645 }
646 else if (Flags & TEXT_PADDING_BIG)
647 {
648 coPos.X += 3;
649 }
650 }
651
652 if (Flags & TEXT_TYPE_STATUS)
653 {
654 CONSOLE_SetStatusTextX(coPos.X, Text);
655 }
656 else /* TEXT_TYPE_REGULAR (Default) */
657 {
658 if (Flags & TEXT_STYLE_HIGHLIGHT)
659 {
660 CONSOLE_SetHighlightedTextXY(coPos.X, coPos.Y, Text);
661 }
662 else if (Flags & TEXT_STYLE_UNDERLINE)
663 {
664 CONSOLE_SetUnderlinedTextXY(coPos.X, coPos.Y, Text);
665 }
666 else /* TEXT_STYLE_NORMAL (Default) */
667 {
668 CONSOLE_SetTextXY(coPos.X, coPos.Y, Text);
669 }
670 }
671 }
672
673 /* EOF */