1 /* $Id: console.c,v 1.4 2003/08/13 05:18:40 jimtabor Exp $
3 * CONSOLE.C - console input/output functions.
8 * 20-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
23 #define OUTPUT_BUFFER_SIZE 4096
26 VOID
ConInDisable (VOID
)
28 HANDLE hInput
= GetStdHandle (STD_INPUT_HANDLE
);
31 GetConsoleMode (hInput
, &dwMode
);
32 dwMode
&= ~ENABLE_PROCESSED_INPUT
;
33 SetConsoleMode (hInput
, dwMode
);
37 VOID
ConInEnable (VOID
)
39 HANDLE hInput
= GetStdHandle (STD_INPUT_HANDLE
);
42 GetConsoleMode (hInput
, &dwMode
);
43 dwMode
|= ENABLE_PROCESSED_INPUT
;
44 SetConsoleMode (hInput
, dwMode
);
48 VOID
ConInDummy (VOID
)
50 HANDLE hInput
= GetStdHandle (STD_INPUT_HANDLE
);
55 if (hInput
== INVALID_HANDLE_VALUE
)
56 DebugPrintf (_T("Invalid input handle!!!\n"));
59 /* ReadConsoleInputW isn't implwmented within ROS. */
60 ReadConsoleInputA (hInput
, &dummy
, 1, &dwRead
);
62 ReadConsoleInput (hInput
, &dummy
, 1, &dwRead
);
66 VOID
ConInFlush (VOID
)
68 FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE
));
72 VOID
ConInKey (PINPUT_RECORD lpBuffer
)
74 HANDLE hInput
= GetStdHandle (STD_INPUT_HANDLE
);
78 if (hInput
== INVALID_HANDLE_VALUE
)
79 DebugPrintf (_T("Invalid input handle!!!\n"));
85 /* ReadConsoleInputW isn't implwmented within ROS. */
86 ReadConsoleInputA (hInput
, lpBuffer
, 1, &dwRead
);
88 ReadConsoleInput (hInput
, lpBuffer
, 1, &dwRead
);
90 if ((lpBuffer
->EventType
== KEY_EVENT
) &&
91 (lpBuffer
->Event
.KeyEvent
.bKeyDown
== TRUE
))
99 VOID
ConInString (LPTSTR lpInput
, DWORD dwLength
)
110 pBuf
= (PCHAR
)alloca(dwLength
);
114 ZeroMemory (lpInput
, dwLength
* sizeof(TCHAR
));
115 hFile
= GetStdHandle (STD_INPUT_HANDLE
);
116 GetConsoleMode (hFile
, &dwOldMode
);
118 SetConsoleMode (hFile
, ENABLE_LINE_INPUT
| ENABLE_ECHO_INPUT
);
120 ReadFile (hFile
, (PVOID
)pBuf
, dwLength
, &dwRead
, NULL
);
123 MultiByteToWideChar(CP_ACP
, 0, pBuf
, dwLength
+ 1, lpInput
, dwLength
+ 1);
126 for (i
= 0; i
< dwRead
; i
++, p
++)
128 if (*p
== _T('\x0d'))
135 SetConsoleMode (hFile
, dwOldMode
);
138 static VOID
ConChar(TCHAR c
, DWORD nStdHandle
)
147 WideCharToMultiByte(CP_ACP
, 0, ws
, 2, as
, 2, NULL
, NULL
);
152 WriteFile (GetStdHandle (nStdHandle
),
159 VOID
ConOutChar (TCHAR c
)
161 ConChar(c
, STD_OUTPUT_HANDLE
);
164 VOID
ConPuts(LPTSTR szText
, DWORD nStdHandle
)
170 len
= _tcslen(szText
);
172 pBuf
= alloca(len
+ 1);
173 len
= WideCharToMultiByte(CP_ACP
, 0, szText
, len
+ 1, pBuf
, len
+ 1, NULL
, NULL
) - 1;
177 WriteFile (GetStdHandle (nStdHandle
),
182 WriteFile (GetStdHandle (nStdHandle
),
189 VOID
ConOutPuts (LPTSTR szText
)
191 ConPuts(szText
, STD_OUTPUT_HANDLE
);
195 VOID
ConPrintf(LPTSTR szFormat
, va_list arg_ptr
, DWORD nStdHandle
)
199 TCHAR szOut
[OUTPUT_BUFFER_SIZE
];
202 len
= _vstprintf (szOut
, szFormat
, arg_ptr
);
204 pBuf
= alloca(len
+ 1);
205 len
= WideCharToMultiByte(CP_ACP
, 0, szOut
, len
+ 1, pBuf
, len
+ 1, NULL
, NULL
) - 1;
209 WriteFile (GetStdHandle (nStdHandle
),
217 VOID
ConOutPrintf (LPTSTR szFormat
, ...)
221 va_start (arg_ptr
, szFormat
);
222 ConPrintf(szFormat
, arg_ptr
, STD_OUTPUT_HANDLE
);
226 VOID
ConErrChar (TCHAR c
)
228 ConChar(c
, STD_ERROR_HANDLE
);
232 VOID
ConErrPuts (LPTSTR szText
)
234 ConPuts(szText
, STD_ERROR_HANDLE
);
238 VOID
ConErrPrintf (LPTSTR szFormat
, ...)
242 va_start (arg_ptr
, szFormat
);
243 ConPrintf(szFormat
, arg_ptr
, STD_ERROR_HANDLE
);
248 VOID
DebugPrintf (LPTSTR szFormat
, ...)
252 va_start (arg_ptr
, szFormat
);
253 ConPrintf(szFormat
, arg_ptr
, STD_ERROR_HANDLE
);
256 TCHAR szOut
[OUTPUT_BUFFER_SIZE
];
257 va_start (arg_ptr
, szFormat
);
258 _vstprintf (szOut
, szFormat
, arg_ptr
);
259 OutputDebugString (szOut
);
265 VOID
SetCursorXY (SHORT x
, SHORT y
)
271 SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE
), coPos
);
275 VOID
GetCursorXY (PSHORT x
, PSHORT y
)
277 CONSOLE_SCREEN_BUFFER_INFO csbi
;
279 GetConsoleScreenBufferInfo (hConsole
, &csbi
);
281 *x
= csbi
.dwCursorPosition
.X
;
282 *y
= csbi
.dwCursorPosition
.Y
;
286 SHORT
GetCursorX (VOID
)
288 CONSOLE_SCREEN_BUFFER_INFO csbi
;
290 GetConsoleScreenBufferInfo (hConsole
, &csbi
);
292 return csbi
.dwCursorPosition
.X
;
296 SHORT
GetCursorY (VOID
)
298 CONSOLE_SCREEN_BUFFER_INFO csbi
;
300 GetConsoleScreenBufferInfo (hConsole
, &csbi
);
302 return csbi
.dwCursorPosition
.Y
;
306 VOID
GetScreenSize (PSHORT maxx
, PSHORT maxy
)
308 CONSOLE_SCREEN_BUFFER_INFO csbi
;
310 GetConsoleScreenBufferInfo (hConsole
, &csbi
);
313 *maxx
= csbi
.dwSize
.X
;
315 *maxy
= csbi
.dwSize
.Y
;
319 VOID
SetCursorType (BOOL bInsert
, BOOL bVisible
)
321 CONSOLE_CURSOR_INFO cci
;
323 cci
.dwSize
= bInsert
? 10 : 99;
324 cci
.bVisible
= bVisible
;
326 SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE
), &cci
);