ReactOS 0.4.16-dev-2522-g97cc325
screen.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Console Utilities Library
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Console/terminal screen management.
5 * COPYRIGHT: Copyright 2017-2018 ReactOS Team
6 * Copyright 2017-2018 Hermes Belusca-Maito
7 */
8
16/* FIXME: Temporary HACK before we cleanly support UNICODE functions */
17#define UNICODE
18#define _UNICODE
19
20#include <windef.h>
21#include <winbase.h>
22#include <wincon.h> // Console APIs (only if kernel32 support included)
23#include <strsafe.h>
24
25#include "conutils.h"
26#include "stream.h"
27#include "screen.h"
28
29// Temporary HACK
30#define CON_STREAM_WRITE ConStreamWrite
31
32
33#if 0
34
35VOID
37{
39
40 /*
41 * Erase the full line where the cursor is, and move
42 * the cursor back to the beginning of the line.
43 */
44
45 if (IsConsoleHandle(hOutput))
46 {
48 DWORD dwWritten;
49
50 GetConsoleScreenBufferInfo(hOutput, &csbi);
51
52 csbi.dwCursorPosition.X = 0;
53 // csbi.dwCursorPosition.Y;
54
56 csbi.dwSize.X,
58 &dwWritten);
60 }
61 else if (IsTTYHandle(hOutput))
62 {
63 ConPuts(Stream, L"\x1B[2K\x1B[1G"); // FIXME: Just use WriteFile
64 }
65 // else, do nothing for files
66}
67
68#endif
69
70
71BOOL
75{
77 HANDLE hOutput;
78
79 /* Parameters validation */
80 if (!Screen || !pcsbi)
81 return FALSE;
82
83 hOutput = ConStreamGetOSHandle(Screen->Stream);
84
85 /* Screen handle must be of TTY type (console or TTY) */
86 if (!IsTTYHandle(hOutput))
87 return FALSE;
88
89 /* Update cached screen information */
90 if (IsConsoleHandle(hOutput))
91 {
92 Success = GetConsoleScreenBufferInfo(hOutput, &Screen->csbi);
93 }
94 else
95 {
96#if 0
97 /* TODO: Do something adequate for TTYs */
98 // FIXME: At the moment we return hardcoded info.
99 Screen->csbi.dwSize.X = 80;
100 Screen->csbi.dwSize.Y = 25;
101
102 // Screen->csbi.dwCursorPosition;
103 // Screen->csbi.wAttributes;
104 // Screen->csbi.srWindow;
105 Screen->csbi.dwMaximumWindowSize = Screen->csbi.dwSize;
106#else
107 hOutput = CreateFileW(L"CONOUT$", GENERIC_READ|GENERIC_WRITE,
109 OPEN_EXISTING, 0, NULL);
110
111 Success = IsConsoleHandle(hOutput) &&
112 GetConsoleScreenBufferInfo(hOutput, &Screen->csbi);
113
114 CloseHandle(hOutput);
115#endif
116 }
117
118 if (Success)
119 {
120 /* Return it to the caller */
121 *pcsbi = Screen->csbi;
122 }
123
124 return Success;
125}
126
127// For real consoles, erase everything, otherwise (TTY) erase just the "screen".
128// FIXME: Or we can add a BOOL flag?
129VOID
131{
132 HANDLE hOutput;
133
134 /* Parameters validation */
135 if (!Screen) return;
136
137#if 0
138 /* Get the size of the visual screen */
139 if (!ConGetScreenInfo(Screen, &csbi))
140 {
141 /* We assume it's a file handle */
142 return;
143 }
144#endif
145
146 hOutput = ConStreamGetOSHandle(Screen->Stream);
147
148 if (IsConsoleHandle(hOutput))
149 {
151 COORD coPos;
152 DWORD dwWritten;
153
154 GetConsoleScreenBufferInfo(hOutput, &csbi);
155
156 coPos.X = 0;
157 coPos.Y = 0;
159 csbi.dwSize.X * csbi.dwSize.Y,
160 coPos, &dwWritten);
161 FillConsoleOutputCharacterW(hOutput, L' ',
162 csbi.dwSize.X * csbi.dwSize.Y,
163 coPos, &dwWritten);
164 SetConsoleCursorPosition(hOutput, coPos);
165 }
166 else if (IsTTYHandle(hOutput))
167 {
168 /* Clear the full screen and move the cursor to (0,0) */
169 ConPuts(Screen->Stream, L"\x1B[2J\x1B[1;1H");
170 }
171 else
172 {
173 /* Issue a Form-Feed control */
174 WCHAR ch = L'\f';
175 CON_STREAM_WRITE(Screen->Stream, &ch, 1);
176 }
177}
178
179/* EOF */
BOOL WINAPI SetConsoleCursorPosition(IN HANDLE hConsoleOutput, IN COORD dwCursorPosition)
Definition: console.c:641
BOOL WINAPI FillConsoleOutputAttribute(IN HANDLE hConsoleOutput, IN WORD wAttribute, IN DWORD nLength, IN COORD dwWriteCoord, OUT LPDWORD lpNumberOfAttrsWritten)
Definition: console.c:525
BOOL WINAPI GetConsoleScreenBufferInfo(IN HANDLE hConsoleOutput, OUT PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo)
Definition: console.c:595
void ConPuts(FILE *fp, LPCWSTR psz)
Definition: conutils_noros.h:8
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
@ Screen
Definition: console.h:34
#define CloseHandle
Definition: compat.h:739
#define OPEN_EXISTING
Definition: compat.h:775
#define GENERIC_READ
Definition: compat.h:135
#define CreateFileW
Definition: compat.h:741
#define FILE_SHARE_READ
Definition: compat.h:136
BOOL WINAPI DECLSPEC_HOTPATCH FillConsoleOutputCharacterW(IN HANDLE hConsoleOutput, IN WCHAR cCharacter, IN DWORD nLength, IN COORD dwWriteCoord, OUT LPDWORD lpNumberOfCharsWritten)
Definition: readwrite.c:1674
unsigned char ch[4][2]
Definition: console.c:118
#define L(x)
Definition: resources.c:13
@ Success
Definition: eventcreate.c:712
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
static IStream Stream
Definition: htmldoc.c:1115
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define GENERIC_WRITE
Definition: nt_native.h:90
VOID ConClearLine(IN PCON_STREAM Stream)
Definition: outstream.c:1480
Console/terminal screen management.
#define IsConsoleHandle(h)
Definition: console.h:14
BOOL ConGetScreenInfo(IN PCON_SCREEN Screen, OUT PCONSOLE_SCREEN_BUFFER_INFO pcsbi)
Definition: screen.c:72
#define CON_STREAM_WRITE
Definition: screen.c:30
VOID ConClearScreen(IN PCON_SCREEN Screen)
Definition: screen.c:130
HANDLE ConStreamGetOSHandle(IN PCON_STREAM Stream)
Definition: stream.c:239
Console I/O streams.
BOOL IsTTYHandle(IN HANDLE hHandle)
Definition: utils.c:403
Definition: blue.h:25
SHORT Y
Definition: blue.h:27
SHORT X
Definition: blue.h:26
#define IN
Definition: typedefs.h:39
#define OUT
Definition: typedefs.h:40
__wchar_t WCHAR
Definition: xmlstorage.h:180