makefiles: Generate the config.status dependency as part of the makefile dependencies.
[wine/multimedia.git] / dlls / msvcrt / console.c
blob268f6dbe5143c30ae98e6a2895ec8a0228188639
1 /*
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
24 #include "msvcrt.h"
25 #include "wincon.h"
26 #include "mtdll.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
33 /* MT */
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 /*********************************************************************
65 * _cputs (MSVCRT.@)
67 int CDECL _cputs(const char* str)
69 DWORD count;
70 int retval = MSVCRT_EOF;
72 LOCK_CONSOLE;
73 if (WriteConsoleA(MSVCRT_console_out, str, strlen(str), &count, NULL)
74 && count == 1)
75 retval = 0;
76 UNLOCK_CONSOLE;
77 return retval;
80 #define NORMAL_CHAR 0
81 #define ALT_CHAR 1
82 #define CTRL_CHAR 2
83 #define SHIFT_CHAR 3
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 /*********************************************************************
99 * _getch (MSVCRT.@)
101 int CDECL _getch(void)
103 int retval = MSVCRT_EOF;
105 LOCK_CONSOLE;
106 if (__MSVCRT_console_buffer != MSVCRT_EOF)
108 retval = __MSVCRT_console_buffer;
109 __MSVCRT_console_buffer = MSVCRT_EOF;
111 else
113 INPUT_RECORD ir;
114 DWORD count;
115 DWORD mode = 0;
117 GetConsoleMode(MSVCRT_console_in, &mode);
118 if(mode)
119 SetConsoleMode(MSVCRT_console_in, 0);
121 do {
122 if (ReadConsoleInputA(MSVCRT_console_in, &ir, 1, &count))
124 unsigned int i;
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;
132 break;
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]))
140 unsigned idx;
142 if (ir.Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
143 idx = ALT_CHAR;
144 else if (ir.Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED) )
145 idx = CTRL_CHAR;
146 else if (ir.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED)
147 idx = SHIFT_CHAR;
148 else
149 idx = NORMAL_CHAR;
151 retval = enh_map[i].ch[idx][0];
152 __MSVCRT_console_buffer = enh_map[i].ch[idx][1];
153 break;
155 WARN("Unmapped char keyState=%x vk=%x\n",
156 ir.Event.KeyEvent.dwControlKeyState, ir.Event.KeyEvent.wVirtualScanCode);
159 else
160 break;
161 } while(1);
162 if (mode)
163 SetConsoleMode(MSVCRT_console_in, mode);
165 UNLOCK_CONSOLE;
166 return retval;
169 /*********************************************************************
170 * _putch (MSVCRT.@)
172 int CDECL _putch(int c)
174 int retval = MSVCRT_EOF;
175 DWORD count;
176 LOCK_CONSOLE;
177 if (WriteConsoleA(MSVCRT_console_out, &c, 1, &count, NULL) && count == 1)
178 retval = c;
179 UNLOCK_CONSOLE;
180 return retval;
183 /*********************************************************************
184 * _getche (MSVCRT.@)
186 int CDECL _getche(void)
188 int retval;
189 LOCK_CONSOLE;
190 retval = _getch();
191 if (retval != MSVCRT_EOF)
192 retval = _putch(retval);
193 UNLOCK_CONSOLE;
194 return retval;
197 /*********************************************************************
198 * _cgets (MSVCRT.@)
200 char* CDECL _cgets(char* str)
202 char *buf = str + 2;
203 DWORD got;
204 DWORD conmode = 0;
206 TRACE("(%p)\n", str);
207 str[1] = 0; /* Length */
208 LOCK_CONSOLE;
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') {
214 buf[got-2] = 0;
215 str[1] = got-2;
217 else if(got == 1 && buf[got-1] == '\n') {
218 buf[0] = 0;
219 str[1] = 0;
221 else if(got == str[0] && buf[got-1] == '\r') {
222 buf[got-1] = 0;
223 str[1] = got-1;
225 else
226 str[1] = got;
228 else
229 buf = NULL;
230 SetConsoleMode(MSVCRT_console_in, conmode);
231 UNLOCK_CONSOLE;
232 return buf;
235 /*********************************************************************
236 * _ungetch (MSVCRT.@)
238 int CDECL _ungetch(int c)
240 int retval = MSVCRT_EOF;
241 LOCK_CONSOLE;
242 if (c != MSVCRT_EOF && __MSVCRT_console_buffer == MSVCRT_EOF)
243 retval = __MSVCRT_console_buffer = c;
244 UNLOCK_CONSOLE;
245 return retval;
248 /*********************************************************************
249 * _kbhit (MSVCRT.@)
251 int CDECL _kbhit(void)
253 int retval = 0;
255 LOCK_CONSOLE;
256 if (__MSVCRT_console_buffer != MSVCRT_EOF)
257 retval = 1;
258 else
260 /* FIXME: There has to be a faster way than this in Win32.. */
261 INPUT_RECORD *ir = NULL;
262 DWORD count = 0, i;
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)
274 retval = 1;
275 break;
278 MSVCRT_free(ir);
280 UNLOCK_CONSOLE;
281 return retval;
285 /*********************************************************************
286 * _cprintf (MSVCRT.@)
288 int CDECL _cprintf(const char* format, ...)
290 char buf[2048], *mem = buf;
291 int written, resize = sizeof(buf), retval;
292 __ms_va_list valist;
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 ||
301 written > resize)
303 resize = (written == -1 ? resize * 2 : written + 1);
304 if (mem != buf)
305 MSVCRT_free (mem);
306 if (!(mem = MSVCRT_malloc(resize)))
307 return MSVCRT_EOF;
308 __ms_va_start( valist, format );
310 __ms_va_end(valist);
311 LOCK_CONSOLE;
312 retval = _cputs( mem );
313 UNLOCK_CONSOLE;
314 if (mem != buf)
315 MSVCRT_free (mem);
316 return retval;