1 /*
2 * Win32 console functions
3 *
4 * Copyright 1995 Martin von Loewis and Cameron Heide
5 * Copyright 1997 Karl Garrison
6 * Copyright 1998 John Richardson
7 * Copyright 1998 Marcus Meissner
8 * Copyright 2001,2002,2004,2005 Eric Pouech
9 * Copyright 2001 Alexandre Julliard
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26 /* Reference applications:
27 * - IDA (interactive disassembler) full version 3.75. Works.
28 * - LYNX/W32. Works mostly, some keys crash it.
29 */
30
31 #include "config.h"
32 #include "wine/port.h"
33
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <string.h>
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #include <assert.h>
41
42 #include "windef.h"
43 #include "winbase.h"
44 #include "winnls.h"
45 #include "winerror.h"
46 #include "wincon.h"
47 #include "wine/winbase16.h"
48 #include "wine/server.h"
49 #include "wine/exception.h"
50 #include "wine/unicode.h"
51 #include "wine/debug.h"
52 #include "excpt.h"
53 #include "console_private.h"
54 #include "kernel_private.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL(console);
57
58 static CRITICAL_SECTION CONSOLE_CritSect;
59 static CRITICAL_SECTION_DEBUG critsect_debug =
60 {
61 0, 0, &CONSOLE_CritSect,
62 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
63 0, 0, { (DWORD_PTR)(__FILE__ ": CONSOLE_CritSect") }
64 };
65 static CRITICAL_SECTION CONSOLE_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
66
67 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
68 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
69
70 /* FIXME: this is not thread safe */
71 static HANDLE console_wait_event;
72
73 /* map input records to ASCII */
74 static void input_records_WtoA( INPUT_RECORD *buffer, int count )
75 {
76 int i;
77 char ch;
78
79 for (i = 0; i < count; i++)
80 {
81 if (buffer[i].EventType != KEY_EVENT) continue;
82 WideCharToMultiByte( GetConsoleCP(), 0,
83 &buffer[i].Event.KeyEvent.uChar.UnicodeChar, 1, &ch, 1, NULL, NULL );
84 buffer[i].Event.KeyEvent.uChar.AsciiChar = ch;
85 }
86 }
87
88 /* map input records to Unicode */
89 static void input_records_AtoW( INPUT_RECORD *buffer, int count )
90 {
91 int i;
92 WCHAR ch;
93
94 for (i = 0; i < count; i++)
95 {
96 if (buffer[i].EventType != KEY_EVENT) continue;
97 MultiByteToWideChar( GetConsoleCP(), 0,
98 &buffer[i].Event.KeyEvent.uChar.AsciiChar, 1, &ch, 1 );
99 buffer[i].Event.KeyEvent.uChar.UnicodeChar = ch;
100 }
101 }
102
103 /* map char infos to ASCII */
104 static void char_info_WtoA( CHAR_INFO *buffer, int count )
105 {
106 char ch;
107
108 while (count-- > 0)
109 {
110 WideCharToMultiByte( GetConsoleOutputCP(), 0, &buffer->Char.UnicodeChar, 1,
111 &ch, 1, NULL, NULL );
112 buffer->Char.AsciiChar = ch;
113 buffer++;
114 }
115 }
116
117 /* map char infos to Unicode */
118 static void char_info_AtoW( CHAR_INFO *buffer, int count )
119 {
120 WCHAR ch;
121
122 while (count-- > 0)
123 {
124 MultiByteToWideChar( GetConsoleOutputCP(), 0, &buffer->Char.AsciiChar, 1, &ch, 1 );
125 buffer->Char.UnicodeChar = ch;
126 buffer++;
127 }
128 }
129
130
131 /******************************************************************************
132 * GetConsoleWindow [KERNEL32.@] Get hwnd of the console window.
133 *
134 * RETURNS
135 * Success: hwnd of the console window.
136 * Failure: NULL
137 */
138 HWND WINAPI GetConsoleWindow(VOID)
139 {
140 HWND hWnd = NULL;
141
142 SERVER_START_REQ(get_console_input_info)
143 {
144 req->handle = 0;
145 if (!wine_server_call_err(req)) hWnd = wine_server_ptr_handle( reply->win );
146 }
147 SERVER_END_REQ;
148
149 return hWnd;
150 }
151
152
153 /******************************************************************************
154 * GetConsoleCP [KERNEL32.@] Returns the OEM code page for the console
155 *
156 * RETURNS
157 * Code page code
158 */
159 UINT WINAPI GetConsoleCP(VOID)
160 {
161 BOOL ret;
162 UINT codepage = GetOEMCP(); /* default value */
163
164 SERVER_START_REQ(get_console_input_info)
165 {
166 req->handle = 0;
167 ret = !wine_server_call_err(req);
168 if (ret && reply->input_cp)
169 codepage = reply->input_cp;
170 }
171 SERVER_END_REQ;
172
173 return codepage;
174 }
175
176
177 /******************************************************************************
178 * SetConsoleCP [KERNEL32.@]
179 */
180 BOOL WINAPI SetConsoleCP(UINT cp)
181 {
182 BOOL ret;
183
184 if (!IsValidCodePage(cp))
185 {
186 SetLastError(ERROR_INVALID_PARAMETER);
187 return FALSE;
188 }
189
190 SERVER_START_REQ(set_console_input_info)
191 {
192 req->handle = 0;
193 req->mask = SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE;
194 req->input_cp = cp;
195 ret = !wine_server_call_err(req);
196 }
197 SERVER_END_REQ;
198
199 return ret;
200 }
201
202
203 /***********************************************************************
204 * GetConsoleOutputCP (KERNEL32.@)
205 */
206 UINT WINAPI GetConsoleOutputCP(VOID)
207 {
208 BOOL ret;
209 UINT codepage = GetOEMCP(); /* default value */
210
211 SERVER_START_REQ(get_console_input_info)
212 {
213 req->handle = 0;
214 ret = !wine_server_call_err(req);
215 if (ret && reply->output_cp)
216 codepage = reply->output_cp;
217 }
218 SERVER_END_REQ;
219
220 return codepage;
221 }
222
223
224 /******************************************************************************
225 * SetConsoleOutputCP [KERNEL32.@] Set the output codepage used by the console
226 *
227 * PARAMS
228 * cp [I] code page to set
229 *
230 * RETURNS
231 * Success: TRUE
232 * Failure: FALSE
233 */
234 BOOL WINAPI SetConsoleOutputCP(UINT cp)
235 {
236 BOOL ret;
237
238 if (!IsValidCodePage(cp))
239 {
240 SetLastError(ERROR_INVALID_PARAMETER);
241 return FALSE;
242 }
243
244 SERVER_START_REQ(set_console_input_info)
245 {
246 req->handle = 0;
247 req->mask = SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE;
248 req->output_cp = cp;
249 ret = !wine_server_call_err(req);
250 }
251 SERVER_END_REQ;
252
253 return ret;
254 }
255
256
257 /***********************************************************************
258 * Beep (KERNEL32.@)
259 */
260 BOOL WINAPI Beep( DWORD dwFreq, DWORD dwDur )
261 {
262 static const char beep = '\a';
263 /* dwFreq and dwDur are ignored by Win95 */
264 if (isatty(2)) write( 2, &beep, 1 );
265 return TRUE;
266 }
267
268
269 /******************************************************************
270 * OpenConsoleW (KERNEL32.@)
271 *
272 * Undocumented
273 * Open a handle to the current process console.
274 * Returns INVALID_HANDLE_VALUE on failure.
275 */
276 HANDLE WINAPI OpenConsoleW(LPCWSTR name, DWORD access, BOOL inherit, DWORD creation)
277 {
278 HANDLE output;
279 HANDLE ret;
280
281 if (strcmpiW(coninW, name) == 0)
282 output = (HANDLE) FALSE;
283 else if (strcmpiW(conoutW, name) == 0)
284 output = (HANDLE) TRUE;
285 else
286 {
287 SetLastError(ERROR_INVALID_NAME);
288 return INVALID_HANDLE_VALUE;
289 }
290 if (creation != OPEN_EXISTING)
291 {
292 SetLastError(ERROR_INVALID_PARAMETER);
293 return INVALID_HANDLE_VALUE;
294 }
295
296 SERVER_START_REQ( open_console )
297 {
298 req->from = wine_server_obj_handle( output );
299 req->access = access;
300 req->attributes = inherit ? OBJ_INHERIT : 0;
301 req->share = FILE_SHARE_READ | FILE_SHARE_WRITE;
302 SetLastError(0);
303 wine_server_call_err( req );
304 ret = wine_server_ptr_handle( reply->handle );
305 }
306 SERVER_END_REQ;
307 if (ret)
308 ret = console_handle_map(ret);
309 else
310 {
311 /* likely, we're not attached to wineconsole
312 * let's try to return a handle to the unix-console
313 */
314 int fd = open("/dev/tty", output ? O_WRONLY : O_RDONLY);
315 ret = INVALID_HANDLE_VALUE;
316 if (fd != -1)
317 {
318 DWORD access = (output ? GENERIC_WRITE : GENERIC_READ) | SYNCHRONIZE;
319 wine_server_fd_to_handle(fd, access, inherit ? OBJ_INHERIT : 0, &ret);
320 close(fd);
321 }
322 }
323 return ret;
324 }
325
326 /******************************************************************
327 * VerifyConsoleIoHandle (KERNEL32.@)
328 *
329 * Undocumented
330 */
331 BOOL WINAPI VerifyConsoleIoHandle(HANDLE handle)
332 {
333 BOOL ret;
334
335 if (!is_console_handle(handle)) return FALSE;
336 SERVER_START_REQ(get_console_mode)
337 {
338 req->handle = console_handle_unmap(handle);
339 ret = !wine_server_call_err( req );
340 }
341 SERVER_END_REQ;
342 return ret;
343 }
344
345 /******************************************************************
346 * DuplicateConsoleHandle (KERNEL32.@)
347 *
348 * Undocumented
349 */
350 HANDLE WINAPI DuplicateConsoleHandle(HANDLE handle, DWORD access, BOOL inherit,
351 DWORD options)
352 {
353 HANDLE ret;
354
355 if (!is_console_handle(handle) ||
356 !DuplicateHandle(GetCurrentProcess(), wine_server_ptr_handle(console_handle_unmap(handle)),
357 GetCurrentProcess(), &ret, access, inherit, options))
358 return INVALID_HANDLE_VALUE;
359 return console_handle_map(ret);
360 }
361
362 /******************************************************************
363 * CloseConsoleHandle (KERNEL32.@)
364 *
365 * Undocumented
366 */
367 BOOL WINAPI CloseConsoleHandle(HANDLE handle)
368 {
369 if (!is_console_handle(handle))
370 {
371 SetLastError(ERROR_INVALID_PARAMETER);
372 return FALSE;
373 }
374 return CloseHandle(wine_server_ptr_handle(console_handle_unmap(handle)));
375 }
376
377 /******************************************************************
378 * GetConsoleInputWaitHandle (KERNEL32.@)
379 *
380 * Undocumented
381 */
382 HANDLE WINAPI GetConsoleInputWaitHandle(void)
383 {
384 if (!console_wait_event)
385 {
386 SERVER_START_REQ(get_console_wait_event)
387 {
388 if (!wine_server_call_err( req ))
389 console_wait_event = wine_server_ptr_handle( reply->handle );
390 }
391 SERVER_END_REQ;
392 }
393 return console_wait_event;
394 }
395
396
397 /******************************************************************************
398 * WriteConsoleInputA [KERNEL32.@]
399 */
400 BOOL WINAPI WriteConsoleInputA( HANDLE handle, const INPUT_RECORD *buffer,
401 DWORD count, LPDWORD written )
402 {
403 INPUT_RECORD *recW;
404 BOOL ret;
405
406 if (!(recW = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*recW) ))) return FALSE;
407 memcpy( recW, buffer, count*sizeof(*recW) );
408 input_records_AtoW( recW, count );
409 ret = WriteConsoleInputW( handle, recW, count, written );
410 HeapFree( GetProcessHeap(), 0, recW );
411 return ret;
412 }
413
414
415 /******************************************************************************
416 * WriteConsoleInputW [KERNEL32.@]
417 */
418 BOOL WINAPI WriteConsoleInputW( HANDLE handle, const INPUT_RECORD *buffer,
419 DWORD count, LPDWORD written )
420 {
421 BOOL ret;
422
423 TRACE("(%p,%p,%d,%p)\n", handle, buffer, count, written);
424
425 if (written) *written = 0;
426 SERVER_START_REQ( write_console_input )
427 {
428 req->handle = console_handle_unmap(handle);
429 wine_server_add_data( req, buffer, count * sizeof(INPUT_RECORD) );
430 if ((ret = !wine_server_call_err( req )) && written)
431 *written = reply->written;
432 }
433 SERVER_END_REQ;
434
435 return ret;
436 }
437
438
439 /***********************************************************************
440 * WriteConsoleOutputA (KERNEL32.@)
441 */
442 BOOL WINAPI WriteConsoleOutputA( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
443 COORD size, COORD coord, LPSMALL_RECT region )
444 {
445 int y;
446 BOOL ret;
447 COORD new_size, new_coord;
448 CHAR_INFO *ciw;
449
450 new_size.X = min( region->Right - region->Left + 1, size.X - coord.X );
451 new_size.Y = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
452
453 if (new_size.X <= 0 || new_size.Y <= 0)
454 {
455 region->Bottom = region->Top + new_size.Y - 1;
456 region->Right = region->Left + new_size.X - 1;
457 return TRUE;
458 }
459
460 /* only copy the useful rectangle */
461 if (!(ciw = HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO) * new_size.X * new_size.Y )))
462 return FALSE;
463 for (y = 0; y < new_size.Y; y++)
464 {
465 memcpy( &ciw[y * new_size.X], &lpBuffer[(y + coord.Y) * size.X + coord.X],
466 new_size.X * sizeof(CHAR_INFO) );
467 char_info_AtoW( &ciw[ y * new_size.X ], new_size.X );
468 }
469 new_coord.X = new_coord.Y = 0;
470 ret = WriteConsoleOutputW( hConsoleOutput, ciw, new_size, new_coord, region );
471 HeapFree( GetProcessHeap(), 0, ciw );
472 return ret;
473 }
474
475
476 /***********************************************************************
477 * WriteConsoleOutputW (KERNEL32.@)
478 */
479 BOOL WINAPI WriteConsoleOutputW( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
480 COORD size, COORD coord, LPSMALL_RECT region )
481 {
482 int width, height, y;
483 BOOL ret = TRUE;
484
485 TRACE("(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n",
486 hConsoleOutput, lpBuffer, size.X, size.Y, coord.X, coord.Y,
487 region->Left, region->Top, region->Right, region->Bottom);
488
489 width = min( region->Right - region->Left + 1, size.X - coord.X );
490 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
491
492 if (width > 0 && height > 0)
493 {
494 for (y = 0; y < height; y++)
495 {
496 SERVER_START_REQ( write_console_output )
497 {
498 req->handle = console_handle_unmap(hConsoleOutput);
499 req->x = region->Left;
500 req->y = region->Top + y;
501 req->mode = CHAR_INFO_MODE_TEXTATTR;
502 req->wrap = FALSE;
503 wine_server_add_data( req, &lpBuffer[(y + coord.Y) * size.X + coord.X],
504 width * sizeof(CHAR_INFO));
505 if ((ret = !wine_server_call_err( req )))
506 {
507 width = min( width, reply->width - region->Left );
508 height = min( height, reply->height - region->Top );
509 }
510 }
511 SERVER_END_REQ;
512 if (!ret) break;
513 }
514 }
515 region->Bottom = region->Top + height - 1;
516 region->Right = region->Left + width - 1;
517 return ret;
518 }
519
520
521 /******************************************************************************
522 * WriteConsoleOutputCharacterA [KERNEL32.@]
523 *
524 * See WriteConsoleOutputCharacterW.
525 */
526 BOOL WINAPI WriteConsoleOutputCharacterA( HANDLE hConsoleOutput, LPCSTR str, DWORD length,
527 COORD coord, LPDWORD lpNumCharsWritten )
528 {
529 BOOL ret;
530 LPWSTR strW;
531 DWORD lenW;
532
533 TRACE("(%p,%s,%d,%dx%d,%p)\n", hConsoleOutput,
534 debugstr_an(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
535
536 lenW = MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, NULL, 0 );
537
538 if (lpNumCharsWritten) *lpNumCharsWritten = 0;
539
540 if (!(strW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) ))) return FALSE;
541 MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, strW, lenW );
542
543 ret = WriteConsoleOutputCharacterW( hConsoleOutput, strW, lenW, coord, lpNumCharsWritten );
544 HeapFree( GetProcessHeap(), 0, strW );
545 return ret;
546 }
547
548
549 /******************************************************************************
550 * WriteConsoleOutputAttribute [KERNEL32.@] Sets attributes for some cells in
551 * the console screen buffer
552 *
553 * PARAMS
554 * hConsoleOutput [I] Handle to screen buffer
555 * attr [I] Pointer to buffer with write attributes
556 * length [I] Number of cells to write to
557 * coord [I] Coords of first cell
558 * lpNumAttrsWritten [O] Pointer to number of cells written
559 *
560 * RETURNS
561 * Success: TRUE
562 * Failure: FALSE
563 *
564 */
565 BOOL WINAPI WriteConsoleOutputAttribute( HANDLE hConsoleOutput, CONST WORD *attr, DWORD length,
566 COORD coord, LPDWORD lpNumAttrsWritten )
567 {
568 BOOL ret;
569
570 TRACE("(%p,%p,%d,%dx%d,%p)\n", hConsoleOutput,attr,length,coord.X,coord.Y,lpNumAttrsWritten);
571
572 SERVER_START_REQ( write_console_output )
573 {
574 req->handle = console_handle_unmap(hConsoleOutput);
575 req->x = coord.X;
576 req->y = coord.Y;
577 req->mode = CHAR_INFO_MODE_ATTR;
578 req->wrap = TRUE;
579 wine_server_add_data( req, attr, length * sizeof(WORD) );
580 if ((ret = !wine_server_call_err( req )))
581 {
582 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
583 }
584 }
585 SERVER_END_REQ;
586 return ret;
587 }
588
589
590 /******************************************************************************
591 * FillConsoleOutputCharacterA [KERNEL32.@]
592 *
593 * See FillConsoleOutputCharacterW.
594 */
595 BOOL WINAPI FillConsoleOutputCharacterA( HANDLE hConsoleOutput, CHAR ch, DWORD length,
596 COORD coord, LPDWORD lpNumCharsWritten )
597 {
598 WCHAR wch;
599
600 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch, 1, &wch, 1 );
601 return FillConsoleOutputCharacterW(hConsoleOutput, wch, length, coord, lpNumCharsWritten);
602 }
603
604
605 /******************************************************************************
606 * FillConsoleOutputCharacterW [KERNEL32.@] Writes characters to console
607 *
608 * PARAMS
609 * hConsoleOutput [I] Handle to screen buffer
610 * ch [I] Character to write
611 * length [I] Number of cells to write to
612 * coord [I] Coords of first cell
613 * lpNumCharsWritten [O] Pointer to number of cells written
614 *
615 * RETURNS
616 * Success: TRUE
617 * Failure: FALSE
618 */
619 BOOL WINAPI FillConsoleOutputCharacterW( HANDLE hConsoleOutput, WCHAR ch, DWORD length,
620 COORD coord, LPDWORD lpNumCharsWritten)
621 {
622 BOOL ret;
623
624 TRACE("(%p,%s,%d,(%dx%d),%p)\n",
625 hConsoleOutput, debugstr_wn(&ch, 1), length, coord.X, coord.Y, lpNumCharsWritten);
626
627 SERVER_START_REQ( fill_console_output )
628 {
629 req->handle = console_handle_unmap(hConsoleOutput);
630 req->x = coord.X;
631 req->y = coord.Y;
632 req->mode = CHAR_INFO_MODE_TEXT;
633 req->wrap = TRUE;
634 req->data.ch = ch;
635 req->count = length;
636 if ((ret = !wine_server_call_err( req )))
637 {
638 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
639 }
640 }
641 SERVER_END_REQ;
642 return ret;
643 }
644
645
646 /******************************************************************************
647 * FillConsoleOutputAttribute [KERNEL32.@] Sets attributes for console
648 *
649 * PARAMS
650 * hConsoleOutput [I] Handle to screen buffer
651 * attr [I] Color attribute to write
652 * length [I] Number of cells to write to
653 * coord [I] Coords of first cell
654 * lpNumAttrsWritten [O] Pointer to number of cells written
655 *
656 * RETURNS
657 * Success: TRUE
658 * Failure: FALSE
659 */
660 BOOL WINAPI FillConsoleOutputAttribute( HANDLE hConsoleOutput, WORD attr, DWORD length,
661 COORD coord, LPDWORD lpNumAttrsWritten )
662 {
663 BOOL ret;
664
665 TRACE("(%p,%d,%d,(%dx%d),%p)\n",
666 hConsoleOutput, attr, length, coord.X, coord.Y, lpNumAttrsWritten);
667
668 SERVER_START_REQ( fill_console_output )
669 {
670 req->handle = console_handle_unmap(hConsoleOutput);
671 req->x = coord.X;
672 req->y = coord.Y;
673 req->mode = CHAR_INFO_MODE_ATTR;
674 req->wrap = TRUE;
675 req->data.attr = attr;
676 req->count = length;
677 if ((ret = !wine_server_call_err( req )))
678 {
679 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
680 }
681 }
682 SERVER_END_REQ;
683 return ret;
684 }
685
686
687 /******************************************************************************
688 * ReadConsoleOutputCharacterA [KERNEL32.@]
689 *
690 */
691 BOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput, LPSTR lpstr, DWORD count,
692 COORD coord, LPDWORD read_count)
693 {
694 DWORD read;
695 BOOL ret;
696 LPWSTR wptr = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
697
698 if (read_count) *read_count = 0;
699 if (!wptr) return FALSE;
700
701 if ((ret = ReadConsoleOutputCharacterW( hConsoleOutput, wptr, count, coord, &read )))
702 {
703 read = WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr, read, lpstr, count, NULL, NULL);
704 if (read_count) *read_count = read;
705 }
706 HeapFree( GetProcessHeap(), 0, wptr );
707 return ret;
708 }
709
710
711 /******************************************************************************
712 * ReadConsoleOutputCharacterW [KERNEL32.@]
713 *
714 */
715 BOOL WINAPI ReadConsoleOutputCharacterW( HANDLE hConsoleOutput, LPWSTR buffer, DWORD count,
716 COORD coord, LPDWORD read_count )
717 {
718 BOOL ret;
719
720 TRACE( "(%p,%p,%d,%dx%d,%p)\n", hConsoleOutput, buffer, count, coord.X, coord.Y, read_count );
721
722 SERVER_START_REQ( read_console_output )
723 {
724 req->handle = console_handle_unmap(hConsoleOutput);
725 req->x = coord.X;
726 req->y = coord.Y;
727 req->mode = CHAR_INFO_MODE_TEXT;
728 req->wrap = TRUE;
729 wine_server_set_reply( req, buffer, count * sizeof(WCHAR) );
730 if ((ret = !wine_server_call_err( req )))
731 {
732 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WCHAR);
733 }
734 }
735 SERVER_END_REQ;
736 return ret;
737 }
738
739
740 /******************************************************************************
741 * ReadConsoleOutputAttribute [KERNEL32.@]
742 */
743 BOOL WINAPI ReadConsoleOutputAttribute(HANDLE hConsoleOutput, LPWORD lpAttribute, DWORD length,
744 COORD coord, LPDWORD read_count)
745 {
746 BOOL ret;
747
748 TRACE("(%p,%p,%d,%dx%d,%p)\n",
749 hConsoleOutput, lpAttribute, length, coord.X, coord.Y, read_count);
750
751 SERVER_START_REQ( read_console_output )
752 {
753 req->handle = console_handle_unmap(hConsoleOutput);
754 req->x = coord.X;
755 req->y = coord.Y;
756 req->mode = CHAR_INFO_MODE_ATTR;
757 req->wrap = TRUE;
758 wine_server_set_reply( req, lpAttribute, length * sizeof(WORD) );
759 if ((ret = !wine_server_call_err( req )))
760 {
761 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WORD);
762 }
763 }
764 SERVER_END_REQ;
765 return ret;
766 }
767
768
769 /******************************************************************************
770 * ReadConsoleOutputA [KERNEL32.@]
771 *
772 */
773 BOOL WINAPI ReadConsoleOutputA( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
774 COORD coord, LPSMALL_RECT region )
775 {
776 BOOL ret;
777 int y;
778
779 ret = ReadConsoleOutputW( hConsoleOutput, lpBuffer, size, coord, region );
780 if (ret && region->Right >= region->Left)
781 {
782 for (y = 0; y <= region->Bottom - region->Top; y++)
783 {
784 char_info_WtoA( &lpBuffer[(coord.Y + y) * size.X + coord.X],
785 region->Right - region->Left + 1 );
786 }
787 }
788 return ret;
789 }
790
791
792 /******************************************************************************
793 * ReadConsoleOutputW [KERNEL32.@]
794 *
795 * NOTE: The NT4 (sp5) kernel crashes on me if size is (0,0). I don't
796 * think we need to be *that* compatible. -- AJ
797 */
798 BOOL WINAPI ReadConsoleOutputW( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
799 COORD coord, LPSMALL_RECT region )
800 {
801 int width, height, y;
802 BOOL ret = TRUE;
803
804 width = min( region->Right - region->Left + 1, size.X - coord.X );
805 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
806
807 if (width > 0 && height > 0)
808 {
809 for (y = 0; y < height; y++)
810 {
811 SERVER_START_REQ( read_console_output )
812 {
813 req->handle = console_handle_unmap(hConsoleOutput);
814 req->x = region->Left;
815 req->y = region->Top + y;
816 req->mode = CHAR_INFO_MODE_TEXTATTR;
817 req->wrap = FALSE;
818 wine_server_set_reply( req, &lpBuffer[(y+coord.Y) * size.X + coord.X],
819 width * sizeof(CHAR_INFO) );
820 if ((ret = !wine_server_call_err( req )))
821 {
822 width = min( width, reply->width - region->Left );
823 height = min( height, reply->height - region->Top );
824 }
825 }
826 SERVER_END_REQ;
827 if (!ret) break;
828 }
829 }
830 region->Bottom = region->Top + height - 1;
831 region->Right = region->Left + width - 1;
832 return ret;
833 }
834
835
836 /******************************************************************************
837 * ReadConsoleInputA [KERNEL32.@] Reads data from a console
838 *
839 * PARAMS
840 * handle [I] Handle to console input buffer
841 * buffer [O] Address of buffer for read data
842 * count [I] Number of records to read
843 * pRead [O] Address of number of records read
844 *
845 * RETURNS
846 * Success: TRUE
847 * Failure: FALSE
848 */
849 BOOL WINAPI ReadConsoleInputA( HANDLE handle, PINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
850 {
851 DWORD read;
852
853 if (!ReadConsoleInputW( handle, buffer, count, &read )) return FALSE;
854 input_records_WtoA( buffer, read );
855 if (pRead) *pRead = read;
856 return TRUE;
857 }
858
859
860 /***********************************************************************
861 * PeekConsoleInputA (KERNEL32.@)
862 *
863 * Gets 'count' first events (or less) from input queue.
864 */
865 BOOL WINAPI PeekConsoleInputA( HANDLE handle, PINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
866 {
867 DWORD read;
868
869 if (!PeekConsoleInputW( handle, buffer, count, &read )) return FALSE;
870 input_records_WtoA( buffer, read );
871 if (pRead) *pRead = read;
872 return TRUE;
873 }
874
875
876 /***********************************************************************
877 * PeekConsoleInputW (KERNEL32.@)
878 */
879 BOOL WINAPI PeekConsoleInputW( HANDLE handle, PINPUT_RECORD buffer, DWORD count, LPDWORD read )
880 {
881 BOOL ret;
882 SERVER_START_REQ( read_console_input )
883 {
884 req->handle = console_handle_unmap(handle);
885 req->flush = FALSE;
886 wine_server_set_reply( req, buffer, count * sizeof(INPUT_RECORD) );
887 if ((ret = !wine_server_call_err( req )))
888 {
889 if (read) *read = count ? reply->read : 0;
890 }
891 }
892 SERVER_END_REQ;
893 return ret;
894 }
895
896
897 /***********************************************************************
898 * GetNumberOfConsoleInputEvents (KERNEL32.@)
899 */
900 BOOL WINAPI GetNumberOfConsoleInputEvents( HANDLE handle, LPDWORD nrofevents )
901 {
902 BOOL ret;
903 SERVER_START_REQ( read_console_input )
904 {
905 req->handle = console_handle_unmap(handle);
906 req->flush = FALSE;
907 if ((ret = !wine_server_call_err( req )))
908 {
909 if (nrofevents) *nrofevents = reply->read;
910 }
911 }
912 SERVER_END_REQ;
913 return ret;
914 }
915
916
917 /******************************************************************************
918 * read_console_input
919 *
920 * Helper function for ReadConsole, ReadConsoleInput and FlushConsoleInputBuffer
921 *
922 * Returns
923 * 0 for error, 1 for no INPUT_RECORD ready, 2 with INPUT_RECORD ready
924 */
925 enum read_console_input_return {rci_error = 0, rci_timeout = 1, rci_gotone = 2};
926 static enum read_console_input_return read_console_input(HANDLE handle, PINPUT_RECORD ir, DWORD timeout)
927 {
928 enum read_console_input_return ret;
929
930 if (WaitForSingleObject(GetConsoleInputWaitHandle(), timeout) != WAIT_OBJECT_0)
931 return rci_timeout;
932 SERVER_START_REQ( read_console_input )
933 {
934 req->handle = console_handle_unmap(handle);
935 req->flush = TRUE;
936 wine_server_set_reply( req, ir, sizeof(INPUT_RECORD) );
937 if (wine_server_call_err( req ) || !reply->read) ret = rci_error;
938 else ret = rci_gotone;
939 }
940 SERVER_END_REQ;
941
942 return ret;
943 }
944
945
946 /***********************************************************************
947 * FlushConsoleInputBuffer (KERNEL32.@)
948 */
949 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
950 {
951 enum read_console_input_return last;
952 INPUT_RECORD ir;
953
954 while ((last = read_console_input(handle, &ir, 0)) == rci_gotone);
955
956 return last == rci_timeout;
957 }
958
959
960 /***********************************************************************
961 * SetConsoleTitleA (KERNEL32.@)
962 */
963 BOOL WINAPI SetConsoleTitleA( LPCSTR title )
964 {
965 LPWSTR titleW;
966 BOOL ret;
967
968 DWORD len = MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, NULL, 0 );
969 if (!(titleW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
970 MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, titleW, len );
971 ret = SetConsoleTitleW(titleW);
972 HeapFree(GetProcessHeap(), 0, titleW);
973 return ret;
974 }
975
976
977 /***********************************************************************
978 * GetConsoleKeyboardLayoutNameA (KERNEL32.@)
979 */
980 BOOL WINAPI GetConsoleKeyboardLayoutNameA(LPSTR layoutName)
981 {
982 FIXME( "stub %p\n", layoutName);
983 return TRUE;
984 }
985
986 /***********************************************************************
987 * GetConsoleKeyboardLayoutNameW (KERNEL32.@)
988 */
989 BOOL WINAPI GetConsoleKeyboardLayoutNameW(LPWSTR layoutName)
990 {
991 FIXME( "stub %p\n", layoutName);
992 return TRUE;
993 }
994
995 static WCHAR input_exe[MAX_PATH + 1];
996
997 /***********************************************************************
998 * GetConsoleInputExeNameW (KERNEL32.@)
999 */
1000 BOOL WINAPI GetConsoleInputExeNameW(DWORD buflen, LPWSTR buffer)
1001 {
1002 TRACE("%u %p\n", buflen, buffer);
1003
1004 RtlEnterCriticalSection(&CONSOLE_CritSect);
1005 if (buflen > strlenW(input_exe)) strcpyW(buffer, input_exe);
1006 else SetLastError(ERROR_BUFFER_OVERFLOW);
1007 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1008
1009 return TRUE;
1010 }
1011
1012 /********************