~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/msvcrt/console.c

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  1 /*
  2  * msvcrt.dll console functions
  3  *
  4  * Copyright 2000 Jon Griffiths
  5  *
  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.
 10  *
 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.
 15  *
 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
 19  *
 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
 22  */
 23 
 24 #include "msvcrt.h"
 25 #include "wincon.h"
 26 #include "mtdll.h"
 27 #include "wine/debug.h"
 28 
 29 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
 30 
 31 
 32 
 33 /* MT */
 34 #define LOCK_CONSOLE   _mlock(_CONIO_LOCK)
 35 #define UNLOCK_CONSOLE _munlock(_CONIO_LOCK)
 36 
 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;
 40 
 41 /* INTERNAL: Initialise console handles */
 42 void msvcrt_init_console(void)
 43 {
 44   TRACE(":Opening console handles\n");
 45 
 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);
 50 
 51   if ((MSVCRT_console_in == INVALID_HANDLE_VALUE) ||
 52       (MSVCRT_console_out== INVALID_HANDLE_VALUE))
 53     WARN(":Console handle Initialisation FAILED!\n");
 54 }
 55 
 56 /* INTERNAL: Free console handles */
 57 void msvcrt_free_console(void)
 58 {
 59   TRACE(":Closing console handles\n");
 60   CloseHandle(MSVCRT_console_in);
 61   CloseHandle(MSVCRT_console_out);
 62 }
 63 
 64 /*********************************************************************
 65  *              _cputs (MSVCRT.@)
 66  */
 67 int CDECL _cputs(const char* str)
 68 {
 69   DWORD count;
 70   int retval = MSVCRT_EOF;
 71 
 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;
 78 }
 79 
 80 #define NORMAL_CHAR     0
 81 #define ALT_CHAR        1
 82 #define CTRL_CHAR       2
 83 #define SHIFT_CHAR      3
 84 
 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}}},
 96 };
 97 
 98 /*********************************************************************
 99  *              _getch (MSVCRT.@)
100  */
101 int CDECL _getch(void)
102 {
103   int retval = MSVCRT_EOF;
104 
105   LOCK_CONSOLE;
106   if (__MSVCRT_console_buffer != MSVCRT_EOF)
107   {
108     retval = __MSVCRT_console_buffer;
109     __MSVCRT_console_buffer = MSVCRT_EOF;
110   }
111   else
112   {
113     INPUT_RECORD ir;
114     DWORD count;
115     DWORD mode = 0;
116 
117     GetConsoleMode(MSVCRT_console_in, &mode);
118     if(mode)
119       SetConsoleMode(MSVCRT_console_in, 0);
120 
121     do {
122       if (ReadConsoleInputA(MSVCRT_console_in, &ir, 1, &count))
123       {
124           unsigned int i;
125         /* Only interested in ASCII chars */
126         if (ir.EventType == KEY_EVENT &&
127             ir.Event.KeyEvent.bKeyDown)
128         {
129             if (ir.Event.KeyEvent.uChar.AsciiChar)
130             {
131                 retval = ir.Event.KeyEvent.uChar.AsciiChar;
132                 break;
133             }
134             for (i = 0; i < sizeof(enh_map) / sizeof(enh_map[0]); i++)
135             {
136                 if (ir.Event.KeyEvent.wVirtualScanCode == enh_map[i].vk) break;
137             }
138             if (i < sizeof(enh_map) / sizeof(enh_map[0]))
139             {
140                 unsigned idx;
141 
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;
150 
151                 retval = enh_map[i].ch[idx][0];
152                 __MSVCRT_console_buffer = enh_map[i].ch[idx][1];
153                 break;
154             }
155             WARN("Unmapped char keyState=%x vk=%x\n",
156                  ir.Event.KeyEvent.dwControlKeyState, ir.Event.KeyEvent.wVirtualScanCode);
157         }
158       }
159       else
160         break;
161     } while(1);
162     if (mode)
163       SetConsoleMode(MSVCRT_console_in, mode);
164   }
165   UNLOCK_CONSOLE;
166   return retval;
167 }
168 
169 /*********************************************************************
170  *              _putch (MSVCRT.@)
171  */
172 int CDECL _putch(int c)
173 {
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;
181 }
182 
183 /*********************************************************************
184  *              _getche (MSVCRT.@)
185  */
186 int CDECL _getche(void)
187 {
188   int retval;
189   LOCK_CONSOLE;
190   retval = _getch();
191   if (retval != MSVCRT_EOF)
192     retval = _putch(retval);
193   UNLOCK_CONSOLE;
194   return retval;
195 }
196 
197 /*********************************************************************
198  *              _cgets (MSVCRT.@)
199  */
200 char* CDECL _cgets(char* str)
201 {
202   char *buf = str + 2;
203   DWORD got;
204   DWORD conmode = 0;
205 
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);
211 
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;
216     }
217     else if(got == 1 && buf[got-1] == '\n') {
218       buf[0] = 0;
219       str[1] = 0;
220     }
221     else if(got == str[0] && buf[got-1] == '\r') {
222       buf[got-1] = 0;
223       str[1] = got-1;
224     }
225     else
226       str[1] = got;
227   }
228   else
229     buf = NULL;
230   SetConsoleMode(MSVCRT_console_in, conmode);
231   UNLOCK_CONSOLE;
232   return buf;
233 }
234 
235 /*********************************************************************
236  *              _ungetch (MSVCRT.@)
237  */
238 int CDECL _ungetch(int c)
239 {
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;
246 }
247 
248 /*********************************************************************
249  *              _kbhit (MSVCRT.@)
250  */
251 int CDECL _kbhit(void)
252 {
253   int retval = 0;
254 
255   LOCK_CONSOLE;
256   if (__MSVCRT_console_buffer != MSVCRT_EOF)
257     retval = 1;
258   else
259   {
260     /* FIXME: There has to be a faster way than this in Win32.. */
261     INPUT_RECORD *ir = NULL;
262     DWORD count = 0, i;
263 
264     GetNumberOfConsoleInputEvents(MSVCRT_console_in, &count);
265 
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++)
269       {
270         if (ir[i].EventType == KEY_EVENT &&
271             ir[i].Event.KeyEvent.bKeyDown &&
272             ir[i].Event.KeyEvent.uChar.AsciiChar)
273         {
274           retval = 1;
275           break;
276         }
277       }
278     MSVCRT_free(ir);
279   }
280   UNLOCK_CONSOLE;
281   return retval;
282 }
283 
284 
285 /*********************************************************************
286  *              _cprintf (MSVCRT.@)
287  */
288 int CDECL _cprintf(const char* format, ...)
289 {
290   char buf[2048], *mem = buf;
291   int written, resize = sizeof(buf), retval;
292   __ms_va_list valist;
293 
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
299    */
300   while ((written = MSVCRT_vsnprintf( mem, resize, format, valist )) == -1 ||
301           written > resize)
302   {
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 );
309   }
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;
317 }
318 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.