[USETUP] Simplify the unattended code path in the install directory page and verify...
[reactos.git] / base / setup / usetup / 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: base/setup/usetup/consup.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 != FALSE))
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 VOID
138 CONSOLE_GetCursorXY(
139 PSHORT x,
140 PSHORT y)
141 {
142 CONSOLE_SCREEN_BUFFER_INFO csbi;
143
144 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
145
146 *x = csbi.dwCursorPosition.X;
147 *y = csbi.dwCursorPosition.Y;
148 }
149
150 SHORT
151 CONSOLE_GetCursorX(VOID)
152 {
153 CONSOLE_SCREEN_BUFFER_INFO csbi;
154
155 GetConsoleScreenBufferInfo(StdOutput, &csbi);
156
157 return csbi.dwCursorPosition.X;
158 }
159
160 SHORT
161 CONSOLE_GetCursorY(VOID)
162 {
163 CONSOLE_SCREEN_BUFFER_INFO csbi;
164
165 GetConsoleScreenBufferInfo(StdOutput, &csbi);
166
167 return csbi.dwCursorPosition.Y;
168 }
169
170 VOID
171 CONSOLE_SetCursorType(
172 IN BOOL bInsert,
173 IN BOOL bVisible)
174 {
175 CONSOLE_CURSOR_INFO cci;
176
177 cci.dwSize = bInsert ? 10 : 99;
178 cci.bVisible = bVisible;
179
180 SetConsoleCursorInfo(StdOutput, &cci);
181 }
182
183 VOID
184 CONSOLE_SetCursorXY(
185 IN SHORT x,
186 IN SHORT y)
187 {
188 COORD coPos;
189
190 coPos.X = x;
191 coPos.Y = y;
192 SetConsoleCursorPosition(StdOutput, coPos);
193 }
194
195 VOID
196 CONSOLE_ClearScreen(VOID)
197 {
198 COORD coPos;
199 DWORD Written;
200
201 coPos.X = 0;
202 coPos.Y = 0;
203
204 FillConsoleOutputAttribute(
205 StdOutput,
206 FOREGROUND_WHITE | BACKGROUND_BLUE,
207 xScreen * yScreen,
208 coPos,
209 &Written);
210
211 FillConsoleOutputCharacterA(
212 StdOutput,
213 ' ',
214 xScreen * yScreen,
215 coPos,
216 &Written);
217 }
218
219 VOID
220 CONSOLE_InvertTextXY(
221 IN SHORT x,
222 IN SHORT y,
223 IN SHORT col,
224 IN SHORT row)
225 {
226 COORD coPos;
227 DWORD Written;
228
229 for (coPos.Y = y; coPos.Y < y + row; coPos.Y++)
230 {
231 coPos.X = x;
232
233 FillConsoleOutputAttribute(
234 StdOutput,
235 FOREGROUND_BLUE | BACKGROUND_WHITE,
236 col,
237 coPos,
238 &Written);
239 }
240 }
241
242 VOID
243 CONSOLE_NormalTextXY(
244 IN SHORT x,
245 IN SHORT y,
246 IN SHORT col,
247 IN SHORT row)
248 {
249 COORD coPos;
250 DWORD Written;
251
252 for (coPos.Y = y; coPos.Y < y + row; coPos.Y++)
253 {
254 coPos.X = x;
255
256 FillConsoleOutputAttribute(
257 StdOutput,
258 FOREGROUND_WHITE | BACKGROUND_BLUE,
259 col,
260 coPos,
261 &Written);
262 }
263 }
264
265 VOID
266 CONSOLE_SetTextXY(
267 IN SHORT x,
268 IN SHORT y,
269 IN LPCSTR Text)
270 {
271 COORD coPos;
272 DWORD Written;
273
274 coPos.X = x;
275 coPos.Y = y;
276
277 WriteConsoleOutputCharacterA(
278 StdOutput,
279 Text,
280 (ULONG)strlen(Text),
281 coPos,
282 &Written);
283 }
284
285 static
286 VOID
287 CONSOLE_ClearTextXY(IN SHORT x,
288 IN SHORT y,
289 IN SHORT Length)
290 {
291 COORD coPos;
292 DWORD Written;
293
294 coPos.X = x;
295 coPos.Y = y;
296
297 FillConsoleOutputCharacterA(StdOutput,
298 ' ',
299 Length,
300 coPos,
301 &Written);
302 }
303
304 VOID
305 CONSOLE_SetInputTextXY(
306 IN SHORT x,
307 IN SHORT y,
308 IN SHORT len,
309 IN LPCWSTR Text)
310 {
311 COORD coPos;
312 SHORT Length;
313 DWORD Written;
314
315 coPos.X = x;
316 coPos.Y = y;
317
318 Length = (SHORT)wcslen(Text);
319 if (Length > len - 1)
320 Length = len - 1;
321
322 FillConsoleOutputAttribute(
323 StdOutput,
324 BACKGROUND_WHITE,
325 len,
326 coPos,
327 &Written);
328
329 WriteConsoleOutputCharacterW(
330 StdOutput,
331 Text,
332 (ULONG)Length,
333 coPos,
334 &Written);
335
336 coPos.X += Length;
337 if (len > Length)
338 {
339 FillConsoleOutputCharacterA(
340 StdOutput,
341 ' ',
342 len - Length,
343 coPos,
344 &Written);
345 }
346 }
347
348 VOID
349 CONSOLE_SetUnderlinedTextXY(
350 IN SHORT x,
351 IN SHORT y,
352 IN LPCSTR Text)
353 {
354 COORD coPos;
355 DWORD Length;
356 DWORD Written;
357
358 coPos.X = x;
359 coPos.Y = y;
360
361 Length = (ULONG)strlen(Text);
362
363 WriteConsoleOutputCharacterA(
364 StdOutput,
365 Text,
366 Length,
367 coPos,
368 &Written);
369
370 coPos.Y++;
371 FillConsoleOutputCharacterA(
372 StdOutput,
373 0xCD,
374 Length,
375 coPos,
376 &Written);
377 }
378
379 VOID
380 CONSOLE_SetStatusText(
381 IN LPCSTR fmt, ...)
382 {
383 CHAR Buffer[128];
384 va_list ap;
385 COORD coPos;
386 DWORD Written;
387
388 va_start(ap, fmt);
389 vsprintf(Buffer, fmt, ap);
390 va_end(ap);
391
392 coPos.X = 0;
393 coPos.Y = yScreen - 1;
394
395 FillConsoleOutputAttribute(
396 StdOutput,
397 BACKGROUND_WHITE,
398 xScreen,
399 coPos,
400 &Written);
401
402 FillConsoleOutputCharacterA(
403 StdOutput,
404 ' ',
405 xScreen,
406 coPos,
407 &Written);
408
409 WriteConsoleOutputCharacterA(
410 StdOutput,
411 Buffer,
412 (ULONG)strlen(Buffer),
413 coPos,
414 &Written);
415 }
416
417 VOID
418 CONSOLE_SetStatusTextX(
419 IN SHORT x,
420 IN LPCSTR fmt, ...)
421 {
422 CHAR Buffer[128];
423 va_list ap;
424 COORD coPos;
425 DWORD Written;
426
427 va_start(ap, fmt);
428 vsprintf(Buffer, fmt, ap);
429 va_end(ap);
430
431 coPos.X = 0;
432 coPos.Y = yScreen - 1;
433
434 FillConsoleOutputAttribute(
435 StdOutput,
436 BACKGROUND_WHITE,
437 xScreen,
438 coPos,
439 &Written);
440
441 FillConsoleOutputCharacterA(
442 StdOutput,
443 ' ',
444 xScreen,
445 coPos,
446 &Written);
447
448 coPos.X = x;
449
450 WriteConsoleOutputCharacterA(
451 StdOutput,
452 Buffer,
453 (ULONG)strlen(Buffer),
454 coPos,
455 &Written);
456 }
457
458 static
459 VOID
460 CONSOLE_ClearStatusTextX(IN SHORT x,
461 IN SHORT Length)
462 {
463 COORD coPos;
464 DWORD Written;
465
466 coPos.X = x;
467 coPos.Y = yScreen - 1;
468
469 FillConsoleOutputCharacterA(StdOutput,
470 ' ',
471 Length,
472 coPos,
473 &Written);
474 }
475
476
477 VOID
478 CONSOLE_SetStatusTextAutoFitX(
479 IN SHORT x,
480 IN LPCSTR fmt, ...)
481 {
482 CHAR Buffer[128];
483 DWORD Length;
484 va_list ap;
485
486 va_start(ap, fmt);
487 vsprintf(Buffer, fmt, ap);
488 va_end(ap);
489
490 Length = (ULONG)strlen(Buffer);
491
492 if (Length + x <= 79)
493 {
494 CONSOLE_SetStatusTextX (x , Buffer);
495 }
496 else
497 {
498 CONSOLE_SetStatusTextX (79 - Length , Buffer);
499 }
500 }
501
502 VOID
503 CONSOLE_SetInvertedTextXY(
504 IN SHORT x,
505 IN SHORT y,
506 IN LPCSTR Text)
507 {
508 COORD coPos;
509 DWORD Length;
510 DWORD Written;
511
512 coPos.X = x;
513 coPos.Y = y;
514
515 Length = (ULONG)strlen(Text);
516
517 FillConsoleOutputAttribute(
518 StdOutput,
519 FOREGROUND_BLUE | BACKGROUND_WHITE,
520 Length,
521 coPos,
522 &Written);
523
524 WriteConsoleOutputCharacterA(
525 StdOutput,
526 Text,
527 Length,
528 coPos,
529 &Written);
530 }
531
532 VOID
533 CONSOLE_SetHighlightedTextXY(
534 IN SHORT x,
535 IN SHORT y,
536 IN LPCSTR Text)
537 {
538 COORD coPos;
539 DWORD Length;
540 DWORD Written;
541
542 coPos.X = x;
543 coPos.Y = y;
544
545 Length = (ULONG)strlen(Text);
546
547 FillConsoleOutputAttribute(
548 StdOutput,
549 FOREGROUND_WHITE | FOREGROUND_INTENSITY | BACKGROUND_BLUE,
550 Length,
551 coPos,
552 &Written);
553
554 WriteConsoleOutputCharacterA(
555 StdOutput,
556 Text,
557 Length,
558 coPos,
559 &Written);
560 }
561
562 VOID
563 CONSOLE_PrintTextXY(
564 IN SHORT x,
565 IN SHORT y,
566 IN LPCSTR fmt, ...)
567 {
568 CHAR buffer[512];
569 va_list ap;
570 COORD coPos;
571 DWORD Written;
572
573 va_start(ap, fmt);
574 vsprintf(buffer, fmt, ap);
575 va_end(ap);
576
577 coPos.X = x;
578 coPos.Y = y;
579
580 WriteConsoleOutputCharacterA(
581 StdOutput,
582 buffer,
583 (ULONG)strlen(buffer),
584 coPos,
585 &Written);
586 }
587
588 VOID
589 CONSOLE_PrintTextXYN(
590 IN SHORT x,
591 IN SHORT y,
592 IN SHORT len,
593 IN LPCSTR fmt, ...)
594 {
595 CHAR buffer[512];
596 va_list ap;
597 COORD coPos;
598 SHORT Length;
599 DWORD Written;
600
601 va_start(ap, fmt);
602 vsprintf(buffer, fmt, ap);
603 va_end(ap);
604
605 coPos.X = x;
606 coPos.Y = y;
607
608 Length = (SHORT)strlen(buffer);
609 if (Length > len - 1)
610 Length = len - 1;
611
612 WriteConsoleOutputCharacterA(
613 StdOutput,
614 buffer,
615 Length,
616 coPos,
617 &Written);
618
619 coPos.X += Length;
620
621 if (len > Length)
622 {
623 FillConsoleOutputCharacterA(
624 StdOutput,
625 ' ',
626 len - Length,
627 coPos,
628 &Written);
629 }
630 }
631
632 VOID
633 CONSOLE_SetStyledText(
634 IN SHORT x,
635 IN SHORT y,
636 IN INT Flags,
637 IN LPCSTR Text)
638 {
639 COORD coPos;
640 DWORD Length;
641
642 coPos.X = x;
643 coPos.Y = y;
644
645 Length = (ULONG)strlen(Text);
646
647 if (Flags & TEXT_TYPE_STATUS)
648 {
649 coPos.X = x;
650 coPos.Y = yScreen - 1;
651 }
652 else /* TEXT_TYPE_REGULAR (Default) */
653 {
654 coPos.X = x;
655 coPos.Y = y;
656 }
657
658 if (Flags & TEXT_ALIGN_CENTER)
659 {
660 coPos.X = (xScreen - Length) /2;
661 }
662 else if(Flags & TEXT_ALIGN_RIGHT)
663 {
664 coPos.X = coPos.X - Length;
665
666 if (Flags & TEXT_PADDING_SMALL)
667 {
668 coPos.X -= 1;
669 }
670 else if (Flags & TEXT_PADDING_MEDIUM)
671 {
672 coPos.X -= 2;
673 }
674 else if (Flags & TEXT_PADDING_BIG)
675 {
676 coPos.X -= 3;
677 }
678 }
679 else /* TEXT_ALIGN_LEFT (Default) */
680 {
681 if (Flags & TEXT_PADDING_SMALL)
682 {
683 coPos.X += 1;
684 }
685 else if (Flags & TEXT_PADDING_MEDIUM)
686 {
687 coPos.X += 2;
688 }
689 else if (Flags & TEXT_PADDING_BIG)
690 {
691 coPos.X += 3;
692 }
693 }
694
695 if (Flags & TEXT_TYPE_STATUS)
696 {
697 CONSOLE_SetStatusTextX(coPos.X, Text);
698 }
699 else /* TEXT_TYPE_REGULAR (Default) */
700 {
701 if (Flags & TEXT_STYLE_HIGHLIGHT)
702 {
703 CONSOLE_SetHighlightedTextXY(coPos.X, coPos.Y, Text);
704 }
705 else if (Flags & TEXT_STYLE_UNDERLINE)
706 {
707 CONSOLE_SetUnderlinedTextXY(coPos.X, coPos.Y, Text);
708 }
709 else /* TEXT_STYLE_NORMAL (Default) */
710 {
711 CONSOLE_SetTextXY(coPos.X, coPos.Y, Text);
712 }
713 }
714 }
715
716
717 VOID
718 CONSOLE_ClearStyledText(IN SHORT x,
719 IN SHORT y,
720 IN INT Flags,
721 IN SHORT Length)
722 {
723 COORD coPos;
724
725 coPos.X = x;
726 coPos.Y = y;
727
728 if (Flags & TEXT_TYPE_STATUS)
729 {
730 coPos.X = x;
731 coPos.Y = yScreen - 1;
732 }
733 else /* TEXT_TYPE_REGULAR (Default) */
734 {
735 coPos.X = x;
736 coPos.Y = y;
737 }
738
739 if (Flags & TEXT_ALIGN_CENTER)
740 {
741 coPos.X = (xScreen - Length) /2;
742 }
743 else if(Flags & TEXT_ALIGN_RIGHT)
744 {
745 coPos.X = coPos.X - Length;
746
747 if (Flags & TEXT_PADDING_SMALL)
748 {
749 coPos.X -= 1;
750 }
751 else if (Flags & TEXT_PADDING_MEDIUM)
752 {
753 coPos.X -= 2;
754 }
755 else if (Flags & TEXT_PADDING_BIG)
756 {
757 coPos.X -= 3;
758 }
759 }
760 else /* TEXT_ALIGN_LEFT (Default) */
761 {
762 if (Flags & TEXT_PADDING_SMALL)
763 {
764 coPos.X += 1;
765 }
766 else if (Flags & TEXT_PADDING_MEDIUM)
767 {
768 coPos.X += 2;
769 }
770 else if (Flags & TEXT_PADDING_BIG)
771 {
772 coPos.X += 3;
773 }
774 }
775
776 if (Flags & TEXT_TYPE_STATUS)
777 {
778 CONSOLE_ClearStatusTextX(coPos.X, Length);
779 }
780 else /* TEXT_TYPE_REGULAR (Default) */
781 {
782 CONSOLE_ClearTextXY(coPos.X, coPos.Y, Length);
783 }
784 }
785
786 /* EOF */