2 * msvcrt.dll console functions
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Note: init and free don't need MT locking since they are called at DLL
21 * (de)attachment time, which is syncronised for us
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
34 #define LOCK_CONSOLE _mlock(_CONIO_LOCK)
35 #define UNLOCK_CONSOLE _munlock(_CONIO_LOCK)
37 static HANDLE MSVCRT_console_in = INVALID_HANDLE_VALUE;
38 static HANDLE MSVCRT_console_out= INVALID_HANDLE_VALUE;
39 static int __MSVCRT_console_buffer = MSVCRT_EOF;
41 /* INTERNAL: Initialise console handles */
42 void msvcrt_init_console(void)
44 TRACE(":Opening console handles\n");
46 MSVCRT_console_in = CreateFileA("CONIN$", GENERIC_READ, FILE_SHARE_READ,
47 NULL, OPEN_EXISTING, 0, NULL);
48 MSVCRT_console_out= CreateFileA("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE,
49 NULL, OPEN_EXISTING, 0, NULL);
51 if ((MSVCRT_console_in == INVALID_HANDLE_VALUE) ||
52 (MSVCRT_console_out== INVALID_HANDLE_VALUE))
53 WARN(":Console handle Initialisation FAILED!\n");
56 /* INTERNAL: Free console handles */
57 void msvcrt_free_console(void)
59 TRACE(":Closing console handles\n");
60 CloseHandle(MSVCRT_console_in);
61 CloseHandle(MSVCRT_console_out);
64 /*********************************************************************
67 int CDECL _cputs(const char* str)
70 int retval = MSVCRT_EOF;
73 if (WriteConsoleA(MSVCRT_console_out, str, strlen(str), &count, NULL)
85 static const struct {unsigned vk; unsigned ch[4][2];} enh_map[] = {
86 {0x47, {{0xE0, 0x47}, {0x00, 0x97}, {0xE0, 0x77}, {0xE0, 0x47}}},
87 {0x48, {{0xE0, 0x48}, {0x00, 0x98}, {0xE0, 0x8D}, {0xE0, 0x48}}},
88 {0x49, {{0xE0, 0x49}, {0x00, 0x99}, {0xE0, 0x86}, {0xE0, 0x49}}},
89 {0x4B, {{0xE0, 0x4B}, {0x00, 0x9B}, {0xE0, 0x73}, {0xE0, 0x4B}}},
90 {0x4D, {{0xE0, 0x4D}, {0x00, 0x9D}, {0xE0, 0x74}, {0xE0, 0x4D}}},
91 {0x4F, {{0xE0, 0x4F}, {0x00, 0x9F}, {0xE0, 0x75}, {0xE0, 0x4F}}},
92 {0x50, {{0xE0, 0x50}, {0x00, 0xA0}, {0xE0, 0x91}, {0xE0, 0x50}}},
93 {0x51, {{0xE0, 0x51}, {0x00, 0xA1}, {0xE0, 0x76}, {0xE0, 0x51}}},
94 {0x52, {{0xE0, 0x52}, {0x00, 0xA2}, {0xE0, 0x92}, {0xE0, 0x52}}},
95 {0x53, {{0xE0, 0x53}, {0x00, 0xA3}, {0xE0, 0x93}, {0xE0, 0x53}}},
98 /*********************************************************************
101 int CDECL _getch(void)
103 int retval = MSVCRT_EOF;
106 if (__MSVCRT_console_buffer != MSVCRT_EOF)
108 retval = __MSVCRT_console_buffer;
109 __MSVCRT_console_buffer = MSVCRT_EOF;
117 GetConsoleMode(MSVCRT_console_in, &mode);
119 SetConsoleMode(MSVCRT_console_in, 0);
122 if (ReadConsoleInputA(MSVCRT_console_in, &ir, 1, &count))
125 /* Only interested in ASCII chars */
126 if (ir.EventType == KEY_EVENT &&
127 ir.Event.KeyEvent.bKeyDown)
129 if (ir.Event.KeyEvent.uChar.AsciiChar)
131 retval = ir.Event.KeyEvent.uChar.AsciiChar;
134 for (i = 0; i < sizeof(enh_map) / sizeof(enh_map[0]); i++)
136 if (ir.Event.KeyEvent.wVirtualScanCode == enh_map[i].vk) break;
138 if (i < sizeof(enh_map) / sizeof(enh_map[0]))
142 if (ir.Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
144 else if (ir.Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED) )
146 else if (ir.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED)
151 retval = enh_map[i].ch[idx][0];
152 __MSVCRT_console_buffer = enh_map[i].ch[idx][1];
155 WARN("Unmapped char keyState=%x vk=%x\n",
156 ir.Event.KeyEvent.dwControlKeyState, ir.Event.KeyEvent.wVirtualScanCode);
163 SetConsoleMode(MSVCRT_console_in, mode);
169 /*********************************************************************
172 int CDECL _putch(int c)
174 int retval = MSVCRT_EOF;
177 if (WriteConsoleA(MSVCRT_console_out, &c, 1, &count, NULL) && count == 1)
183 /*********************************************************************
186 int CDECL _getche(void)
191 if (retval != MSVCRT_EOF)
192 retval = _putch(retval);
197 /*********************************************************************
200 char* CDECL _cgets(char* str)
206 TRACE("(%p)\n", str);
207 str[1] = 0; /* Length */
209 GetConsoleMode(MSVCRT_console_in, &conmode);
210 SetConsoleMode(MSVCRT_console_in, ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT|ENABLE_PROCESSED_INPUT);
212 if(ReadConsoleA(MSVCRT_console_in, buf, str[0], &got, NULL)) {
213 if(buf[got-2] == '\r') {
217 else if(got == 1 && buf[got-1] == '\n') {
221 else if(got == str[0] && buf[got-1] == '\r') {
230 SetConsoleMode(MSVCRT_console_in, conmode);
235 /*********************************************************************
236 * _ungetch (MSVCRT.@)
238 int CDECL _ungetch(int c)
240 int retval = MSVCRT_EOF;
242 if (c != MSVCRT_EOF && __MSVCRT_console_buffer == MSVCRT_EOF)
243 retval = __MSVCRT_console_buffer = c;
248 /*********************************************************************
251 int CDECL _kbhit(void)
256 if (__MSVCRT_console_buffer != MSVCRT_EOF)
260 /* FIXME: There has to be a faster way than this in Win32.. */
261 INPUT_RECORD *ir = NULL;
264 GetNumberOfConsoleInputEvents(MSVCRT_console_in, &count);
266 if (count && (ir = MSVCRT_malloc(count * sizeof(INPUT_RECORD))) &&
267 PeekConsoleInputA(MSVCRT_console_in, ir, count, &count))
268 for(i = 0; i < count - 1; i++)
270 if (ir[i].EventType == KEY_EVENT &&
271 ir[i].Event.KeyEvent.bKeyDown &&
272 ir[i].Event.KeyEvent.uChar.AsciiChar)
285 /*********************************************************************
286 * _cprintf (MSVCRT.@)
288 int CDECL _cprintf(const char* format, ...)
290 char buf[2048], *mem = buf;
291 int written, resize = sizeof(buf), retval;
294 __ms_va_start( valist, format );
295 /* There are two conventions for snprintf failing:
296 * Return -1 if we truncated, or
297 * Return the number of bytes that would have been written
298 * The code below handles both cases
300 while ((written = MSVCRT_vsnprintf( mem, resize, format, valist )) == -1 ||
303 resize = (written == -1 ? resize * 2 : written + 1);
306 if (!(mem = MSVCRT_malloc(resize)))
308 __ms_va_start( valist, format );
312 retval = _cputs( mem );