2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Console Utilities Library
4 * FILE: sdk/lib/conutils/screen.c
5 * PURPOSE: Console/terminal screen management.
6 * PROGRAMMERS: - Hermes Belusca-Maito (for the library);
7 * - All programmers who wrote the different console applications
8 * from which I took those functions and improved them.
11 /* FIXME: Temporary HACK before we cleanly support UNICODE functions */
15 #include <stdlib.h> // limits.h // For MB_LEN_MAX
19 // #include <winnls.h>
20 #include <wincon.h> // Console APIs (only if kernel32 support included)
28 #define CON_STREAM_WRITE ConStreamWrite
34 ConClearLine(IN PCON_STREAM Stream
)
36 HANDLE hOutput
= ConStreamGetOSHandle(Stream
);
39 * Erase the full line where the cursor is, and move
40 * the cursor back to the beginning of the line.
43 if (IsConsoleHandle(hOutput
))
45 CONSOLE_SCREEN_BUFFER_INFO csbi
;
48 GetConsoleScreenBufferInfo(hOutput
, &csbi
);
50 csbi
.dwCursorPosition
.X
= 0;
51 // csbi.dwCursorPosition.Y;
53 FillConsoleOutputCharacterW(hOutput
, L
' ',
55 csbi
.dwCursorPosition
,
57 SetConsoleCursorPosition(hOutput
, csbi
.dwCursorPosition
);
59 else if (IsTTYHandle(hOutput
))
61 ConPuts(Stream
, L
"\x1B[2K\x1B[1G"); // FIXME: Just use WriteFile
63 // else, do nothing for files
71 IN PCON_SCREEN Screen
,
72 OUT PCONSOLE_SCREEN_BUFFER_INFO pcsbi
)
77 /* Parameters validation */
78 if (!Screen
|| !pcsbi
)
81 hOutput
= ConStreamGetOSHandle(Screen
->Stream
);
83 /* Screen handle must be of TTY type (console or TTY) */
84 if (!IsTTYHandle(hOutput
))
87 /* Update cached screen information */
88 if (IsConsoleHandle(hOutput
))
90 Success
= GetConsoleScreenBufferInfo(hOutput
, &Screen
->csbi
);
95 /* TODO: Do something adequate for TTYs */
96 // FIXME: At the moment we return hardcoded info.
97 Screen
->csbi
.dwSize
.X
= 80;
98 Screen
->csbi
.dwSize
.Y
= 25;
100 // Screen->csbi.dwCursorPosition;
101 // Screen->csbi.wAttributes;
102 // Screen->csbi.srWindow;
103 Screen
->csbi
.dwMaximumWindowSize
= Screen
->csbi
.dwSize
;
105 hOutput
= CreateFileW(L
"CONOUT$", GENERIC_READ
|GENERIC_WRITE
,
106 FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
,
107 OPEN_EXISTING
, 0, NULL
);
109 Success
= IsConsoleHandle(hOutput
) &&
110 GetConsoleScreenBufferInfo(hOutput
, &Screen
->csbi
);
112 CloseHandle(hOutput
);
118 /* Return it to the caller */
119 *pcsbi
= Screen
->csbi
;
125 // For real consoles, erase everything, otherwise (TTY) erase just the "screen".
126 // FIXME: Or we can add a BOOL flag?
128 ConClearScreen(IN PCON_SCREEN Screen
)
132 /* Parameters validation */
136 /* Get the size of the visual screen */
137 if (!ConGetScreenInfo(Screen
, &csbi
))
139 /* We assume it's a file handle */
144 hOutput
= ConStreamGetOSHandle(Screen
->Stream
);
146 if (IsConsoleHandle(hOutput
))
148 CONSOLE_SCREEN_BUFFER_INFO csbi
;
152 GetConsoleScreenBufferInfo(hOutput
, &csbi
);
156 FillConsoleOutputAttribute(hOutput
, csbi
.wAttributes
,
157 csbi
.dwSize
.X
* csbi
.dwSize
.Y
,
159 FillConsoleOutputCharacterW(hOutput
, L
' ',
160 csbi
.dwSize
.X
* csbi
.dwSize
.Y
,
162 SetConsoleCursorPosition(hOutput
, coPos
);
164 else if (IsTTYHandle(hOutput
))
166 /* Clear the full screen and move the cursor to (0,0) */
167 ConPuts(Screen
->Stream
, L
"\x1B[2J\x1B[1;1H");
171 /* Issue a Form-Feed control */
173 CON_STREAM_WRITE(Screen
->Stream
, &ch
, 1);