2 * Win32 console functions
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
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.
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.
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
26 /* Reference applications:
27 * - IDA (interactive disassembler) full version 3.75. Works.
28 * - LYNX/W32. Works mostly, some keys crash it.
32 #include "wine/port.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"
53 #include "console_private.h"
54 #include "kernel_private.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(console);
58 static CRITICAL_SECTION CONSOLE_CritSect;
59 static CRITICAL_SECTION_DEBUG critsect_debug =
61 0, 0, &CONSOLE_CritSect,
62 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
63 0, 0, { (DWORD_PTR)(__FILE__ ": CONSOLE_CritSect") }
65 static CRITICAL_SECTION CONSOLE_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
67 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
68 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
70 /* map input records to ASCII */
71 static void input_records_WtoA( INPUT_RECORD *buffer, int count )
76 for (i = 0; i < count; i++)
78 if (buffer[i].EventType != KEY_EVENT) continue;
79 WideCharToMultiByte( GetConsoleCP(), 0,
80 &buffer[i].Event.KeyEvent.uChar.UnicodeChar, 1, &ch, 1, NULL, NULL );
81 buffer[i].Event.KeyEvent.uChar.AsciiChar = ch;
85 /* map input records to Unicode */
86 static void input_records_AtoW( INPUT_RECORD *buffer, int count )
91 for (i = 0; i < count; i++)
93 if (buffer[i].EventType != KEY_EVENT) continue;
94 MultiByteToWideChar( GetConsoleCP(), 0,
95 &buffer[i].Event.KeyEvent.uChar.AsciiChar, 1, &ch, 1 );
96 buffer[i].Event.KeyEvent.uChar.UnicodeChar = ch;
100 /* map char infos to ASCII */
101 static void char_info_WtoA( CHAR_INFO *buffer, int count )
107 WideCharToMultiByte( GetConsoleOutputCP(), 0, &buffer->Char.UnicodeChar, 1,
108 &ch, 1, NULL, NULL );
109 buffer->Char.AsciiChar = ch;
114 /* map char infos to Unicode */
115 static void char_info_AtoW( CHAR_INFO *buffer, int count )
121 MultiByteToWideChar( GetConsoleOutputCP(), 0, &buffer->Char.AsciiChar, 1, &ch, 1 );
122 buffer->Char.UnicodeChar = ch;
128 /******************************************************************************
129 * GetConsoleWindow [KERNEL32.@] Get hwnd of the console window.
132 * Success: hwnd of the console window.
135 HWND WINAPI GetConsoleWindow(VOID)
139 SERVER_START_REQ(get_console_input_info)
142 if (!wine_server_call_err(req)) hWnd = reply->win;
150 /******************************************************************************
151 * GetConsoleCP [KERNEL32.@] Returns the OEM code page for the console
156 UINT WINAPI GetConsoleCP(VOID)
159 UINT codepage = GetOEMCP(); /* default value */
161 SERVER_START_REQ(get_console_input_info)
164 ret = !wine_server_call_err(req);
165 if (ret && reply->input_cp)
166 codepage = reply->input_cp;
174 /******************************************************************************
175 * SetConsoleCP [KERNEL32.@]
177 BOOL WINAPI SetConsoleCP(UINT cp)
181 if (!IsValidCodePage(cp))
183 SetLastError(ERROR_INVALID_PARAMETER);
187 SERVER_START_REQ(set_console_input_info)
190 req->mask = SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE;
192 ret = !wine_server_call_err(req);
200 /***********************************************************************
201 * GetConsoleOutputCP (KERNEL32.@)
203 UINT WINAPI GetConsoleOutputCP(VOID)
206 UINT codepage = GetOEMCP(); /* default value */
208 SERVER_START_REQ(get_console_input_info)
211 ret = !wine_server_call_err(req);
212 if (ret && reply->output_cp)
213 codepage = reply->output_cp;
221 /******************************************************************************
222 * SetConsoleOutputCP [KERNEL32.@] Set the output codepage used by the console
225 * cp [I] code page to set
231 BOOL WINAPI SetConsoleOutputCP(UINT cp)
235 if (!IsValidCodePage(cp))
237 SetLastError(ERROR_INVALID_PARAMETER);
241 SERVER_START_REQ(set_console_input_info)
244 req->mask = SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE;
246 ret = !wine_server_call_err(req);
254 /***********************************************************************
257 BOOL WINAPI Beep( DWORD dwFreq, DWORD dwDur )
259 static const char beep = '\a';
260 /* dwFreq and dwDur are ignored by Win95 */
261 if (isatty(2)) write( 2, &beep, 1 );
266 /******************************************************************
267 * OpenConsoleW (KERNEL32.@)
270 * Open a handle to the current process console.
271 * Returns INVALID_HANDLE_VALUE on failure.
273 HANDLE WINAPI OpenConsoleW(LPCWSTR name, DWORD access, BOOL inherit, DWORD creation)
278 if (strcmpiW(coninW, name) == 0)
279 output = (HANDLE) FALSE;
280 else if (strcmpiW(conoutW, name) == 0)
281 output = (HANDLE) TRUE;
284 SetLastError(ERROR_INVALID_NAME);
285 return INVALID_HANDLE_VALUE;
287 if (creation != OPEN_EXISTING)
289 SetLastError(ERROR_INVALID_PARAMETER);
290 return INVALID_HANDLE_VALUE;
293 SERVER_START_REQ( open_console )
296 req->access = access;
297 req->attributes = inherit ? OBJ_INHERIT : 0;
298 req->share = FILE_SHARE_READ | FILE_SHARE_WRITE;
300 wine_server_call_err( req );
305 ret = console_handle_map(ret);
308 /* likely, we're not attached to wineconsole
309 * let's try to return a handle to the unix-console
311 int fd = open("/dev/tty", output ? O_WRONLY : O_RDONLY);
312 ret = INVALID_HANDLE_VALUE;
315 DWORD access = (output ? GENERIC_WRITE : GENERIC_READ) | SYNCHRONIZE;
316 wine_server_fd_to_handle(fd, access, inherit ? OBJ_INHERIT : 0, &ret);
323 /******************************************************************
324 * VerifyConsoleIoHandle (KERNEL32.@)
328 BOOL WINAPI VerifyConsoleIoHandle(HANDLE handle)
332 if (!is_console_handle(handle)) return FALSE;
333 SERVER_START_REQ(get_console_mode)
335 req->handle = console_handle_unmap(handle);
336 ret = !wine_server_call_err( req );
342 /******************************************************************
343 * DuplicateConsoleHandle (KERNEL32.@)
347 HANDLE WINAPI DuplicateConsoleHandle(HANDLE handle, DWORD access, BOOL inherit,
352 if (!is_console_handle(handle) ||
353 !DuplicateHandle(GetCurrentProcess(), console_handle_unmap(handle),
354 GetCurrentProcess(), &ret, access, inherit, options))
355 return INVALID_HANDLE_VALUE;
356 return console_handle_map(ret);
359 /******************************************************************
360 * CloseConsoleHandle (KERNEL32.@)
364 BOOL WINAPI CloseConsoleHandle(HANDLE handle)
366 if (!is_console_handle(handle))
368 SetLastError(ERROR_INVALID_PARAMETER);
371 return CloseHandle(console_handle_unmap(handle));
374 /******************************************************************
375 * GetConsoleInputWaitHandle (KERNEL32.@)
379 HANDLE WINAPI GetConsoleInputWaitHandle(void)
381 static HANDLE console_wait_event;
383 /* FIXME: this is not thread safe */
384 if (!console_wait_event)
386 SERVER_START_REQ(get_console_wait_event)
388 if (!wine_server_call_err( req )) console_wait_event = reply->handle;
392 return console_wait_event;
396 /******************************************************************************
397 * WriteConsoleInputA [KERNEL32.@]
399 BOOL WINAPI WriteConsoleInputA( HANDLE handle, const INPUT_RECORD *buffer,
400 DWORD count, LPDWORD written )
405 if (!(recW = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*recW) ))) return FALSE;
406 memcpy( recW, buffer, count*sizeof(*recW) );
407 input_records_AtoW( recW, count );
408 ret = WriteConsoleInputW( handle, recW, count, written );
409 HeapFree( GetProcessHeap(), 0, recW );
414 /******************************************************************************
415 * WriteConsoleInputW [KERNEL32.@]
417 BOOL WINAPI WriteConsoleInputW( HANDLE handle, const INPUT_RECORD *buffer,
418 DWORD count, LPDWORD written )
422 TRACE("(%p,%p,%d,%p)\n", handle, buffer, count, written);
424 if (written) *written = 0;
425 SERVER_START_REQ( write_console_input )
427 req->handle = console_handle_unmap(handle);
428 wine_server_add_data( req, buffer, count * sizeof(INPUT_RECORD) );
429 if ((ret = !wine_server_call_err( req )) && written)
430 *written = reply->written;
438 /***********************************************************************
439 * WriteConsoleOutputA (KERNEL32.@)
441 BOOL WINAPI WriteConsoleOutputA( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
442 COORD size, COORD coord, LPSMALL_RECT region )
446 COORD new_size, new_coord;
449 new_size.X = min( region->Right - region->Left + 1, size.X - coord.X );
450 new_size.Y = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
452 if (new_size.X <= 0 || new_size.Y <= 0)
454 region->Bottom = region->Top + new_size.Y - 1;
455 region->Right = region->Left + new_size.X - 1;
459 /* only copy the useful rectangle */
460 if (!(ciw = HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO) * new_size.X * new_size.Y )))
462 for (y = 0; y < new_size.Y; y++)
464 memcpy( &ciw[y * new_size.X], &lpBuffer[(y + coord.Y) * size.X + coord.X],
465 new_size.X * sizeof(CHAR_INFO) );
466 char_info_AtoW( &ciw[ y * new_size.X ], new_size.X );
468 new_coord.X = new_coord.Y = 0;
469 ret = WriteConsoleOutputW( hConsoleOutput, ciw, new_size, new_coord, region );
470 HeapFree( GetProcessHeap(), 0, ciw );
475 /***********************************************************************
476 * WriteConsoleOutputW (KERNEL32.@)
478 BOOL WINAPI WriteConsoleOutputW( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
479 COORD size, COORD coord, LPSMALL_RECT region )
481 int width, height, y;
484 TRACE("(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n",
485 hConsoleOutput, lpBuffer, size.X, size.Y, coord.X, coord.Y,
486 region->Left, region->Top, region->Right, region->Bottom);
488 width = min( region->Right - region->Left + 1, size.X - coord.X );
489 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
491 if (width > 0 && height > 0)
493 for (y = 0; y < height; y++)
495 SERVER_START_REQ( write_console_output )
497 req->handle = console_handle_unmap(hConsoleOutput);
498 req->x = region->Left;
499 req->y = region->Top + y;
500 req->mode = CHAR_INFO_MODE_TEXTATTR;
502 wine_server_add_data( req, &lpBuffer[(y + coord.Y) * size.X + coord.X],
503 width * sizeof(CHAR_INFO));
504 if ((ret = !wine_server_call_err( req )))
506 width = min( width, reply->width - region->Left );
507 height = min( height, reply->height - region->Top );
514 region->Bottom = region->Top + height - 1;
515 region->Right = region->Left + width - 1;
520 /******************************************************************************
521 * WriteConsoleOutputCharacterA [KERNEL32.@]
523 * See WriteConsoleOutputCharacterW.
525 BOOL WINAPI WriteConsoleOutputCharacterA( HANDLE hConsoleOutput, LPCSTR str, DWORD length,
526 COORD coord, LPDWORD lpNumCharsWritten )
532 TRACE("(%p,%s,%d,%dx%d,%p)\n", hConsoleOutput,
533 debugstr_an(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
535 lenW = MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, NULL, 0 );
537 if (lpNumCharsWritten) *lpNumCharsWritten = 0;
539 if (!(strW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) ))) return FALSE;
540 MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, strW, lenW );
542 ret = WriteConsoleOutputCharacterW( hConsoleOutput, strW, lenW, coord, lpNumCharsWritten );
543 HeapFree( GetProcessHeap(), 0, strW );
548 /******************************************************************************
549 * WriteConsoleOutputAttribute [KERNEL32.@] Sets attributes for some cells in
550 * the console screen buffer
553 * hConsoleOutput [I] Handle to screen buffer
554 * attr [I] Pointer to buffer with write attributes
555 * length [I] Number of cells to write to
556 * coord [I] Coords of first cell
557 * lpNumAttrsWritten [O] Pointer to number of cells written
564 BOOL WINAPI WriteConsoleOutputAttribute( HANDLE hConsoleOutput, CONST WORD *attr, DWORD length,
565 COORD coord, LPDWORD lpNumAttrsWritten )
569 TRACE("(%p,%p,%d,%dx%d,%p)\n", hConsoleOutput,attr,length,coord.X,coord.Y,lpNumAttrsWritten);
571 SERVER_START_REQ( write_console_output )
573 req->handle = console_handle_unmap(hConsoleOutput);
576 req->mode = CHAR_INFO_MODE_ATTR;
578 wine_server_add_data( req, attr, length * sizeof(WORD) );
579 if ((ret = !wine_server_call_err( req )))
581 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
589 /******************************************************************************
590 * FillConsoleOutputCharacterA [KERNEL32.@]
592 * See FillConsoleOutputCharacterW.
594 BOOL WINAPI FillConsoleOutputCharacterA( HANDLE hConsoleOutput, CHAR ch, DWORD length,
595 COORD coord, LPDWORD lpNumCharsWritten )
599 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch, 1, &wch, 1 );
600 return FillConsoleOutputCharacterW(hConsoleOutput, wch, length, coord, lpNumCharsWritten);
604 /******************************************************************************
605 * FillConsoleOutputCharacterW [KERNEL32.@] Writes characters to console
608 * hConsoleOutput [I] Handle to screen buffer
609 * ch [I] Character to write
610 * length [I] Number of cells to write to
611 * coord [I] Coords of first cell
612 * lpNumCharsWritten [O] Pointer to number of cells written
618 BOOL WINAPI FillConsoleOutputCharacterW( HANDLE hConsoleOutput, WCHAR ch, DWORD length,
619 COORD coord, LPDWORD lpNumCharsWritten)
623 TRACE("(%p,%s,%d,(%dx%d),%p)\n",
624 hConsoleOutput, debugstr_wn(&ch, 1), length, coord.X, coord.Y, lpNumCharsWritten);
626 SERVER_START_REQ( fill_console_output )
628 req->handle = console_handle_unmap(hConsoleOutput);
631 req->mode = CHAR_INFO_MODE_TEXT;
635 if ((ret = !wine_server_call_err( req )))
637 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
645 /******************************************************************************
646 * FillConsoleOutputAttribute [KERNEL32.@] Sets attributes for console
649 * hConsoleOutput [I] Handle to screen buffer
650 * attr [I] Color attribute to write
651 * length [I] Number of cells to write to
652 * coord [I] Coords of first cell
653 * lpNumAttrsWritten [O] Pointer to number of cells written
659 BOOL WINAPI FillConsoleOutputAttribute( HANDLE hConsoleOutput, WORD attr, DWORD length,
660 COORD coord, LPDWORD lpNumAttrsWritten )
664 TRACE("(%p,%d,%d,(%dx%d),%p)\n",
665 hConsoleOutput, attr, length, coord.X, coord.Y, lpNumAttrsWritten);
667 SERVER_START_REQ( fill_console_output )
669 req->handle = console_handle_unmap(hConsoleOutput);
672 req->mode = CHAR_INFO_MODE_ATTR;
674 req->data.attr = attr;
676 if ((ret = !wine_server_call_err( req )))
678 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
686 /******************************************************************************
687 * ReadConsoleOutputCharacterA [KERNEL32.@]
690 BOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput, LPSTR lpstr, DWORD count,
691 COORD coord, LPDWORD read_count)
695 LPWSTR wptr = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
697 if (read_count) *read_count = 0;
698 if (!wptr) return FALSE;
700 if ((ret = ReadConsoleOutputCharacterW( hConsoleOutput, wptr, count, coord, &read )))
702 read = WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr, read, lpstr, count, NULL, NULL);
703 if (read_count) *read_count = read;
705 HeapFree( GetProcessHeap(), 0, wptr );
710 /******************************************************************************
711 * ReadConsoleOutputCharacterW [KERNEL32.@]
714 BOOL WINAPI ReadConsoleOutputCharacterW( HANDLE hConsoleOutput, LPWSTR buffer, DWORD count,
715 COORD coord, LPDWORD read_count )
719 TRACE( "(%p,%p,%d,%dx%d,%p)\n", hConsoleOutput, buffer, count, coord.X, coord.Y, read_count );
721 SERVER_START_REQ( read_console_output )
723 req->handle = console_handle_unmap(hConsoleOutput);
726 req->mode = CHAR_INFO_MODE_TEXT;
728 wine_server_set_reply( req, buffer, count * sizeof(WCHAR) );
729 if ((ret = !wine_server_call_err( req )))
731 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WCHAR);
739 /******************************************************************************
740 * ReadConsoleOutputAttribute [KERNEL32.@]
742 BOOL WINAPI ReadConsoleOutputAttribute(HANDLE hConsoleOutput, LPWORD lpAttribute, DWORD length,
743 COORD coord, LPDWORD read_count)
747 TRACE("(%p,%p,%d,%dx%d,%p)\n",
748 hConsoleOutput, lpAttribute, length, coord.X, coord.Y, read_count);
750 SERVER_START_REQ( read_console_output )
752 req->handle = console_handle_unmap(hConsoleOutput);
755 req->mode = CHAR_INFO_MODE_ATTR;
757 wine_server_set_reply( req, lpAttribute, length * sizeof(WORD) );
758 if ((ret = !wine_server_call_err( req )))
760 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WORD);
768 /******************************************************************************
769 * ReadConsoleOutputA [KERNEL32.@]
772 BOOL WINAPI ReadConsoleOutputA( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
773 COORD coord, LPSMALL_RECT region )
778 ret = ReadConsoleOutputW( hConsoleOutput, lpBuffer, size, coord, region );
779 if (ret && region->Right >= region->Left)
781 for (y = 0; y <= region->Bottom - region->Top; y++)
783 char_info_WtoA( &lpBuffer[(coord.Y + y) * size.X + coord.X],
784 region->Right - region->Left + 1 );
791 /******************************************************************************
792 * ReadConsoleOutputW [KERNEL32.@]
794 * NOTE: The NT4 (sp5) kernel crashes on me if size is (0,0). I don't
795 * think we need to be *that* compatible. -- AJ
797 BOOL WINAPI ReadConsoleOutputW( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
798 COORD coord, LPSMALL_RECT region )
800 int width, height, y;
803 width = min( region->Right - region->Left + 1, size.X - coord.X );
804 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
806 if (width > 0 && height > 0)
808 for (y = 0; y < height; y++)
810 SERVER_START_REQ( read_console_output )
812 req->handle = console_handle_unmap(hConsoleOutput);
813 req->x = region->Left;
814 req->y = region->Top + y;
815 req->mode = CHAR_INFO_MODE_TEXTATTR;
817 wine_server_set_reply( req, &lpBuffer[(y+coord.Y) * size.X + coord.X],
818 width * sizeof(CHAR_INFO) );
819 if ((ret = !wine_server_call_err( req )))
821 width = min( width, reply->width - region->Left );
822 height = min( height, reply->height - region->Top );
829 region->Bottom = region->Top + height - 1;
830 region->Right = region->Left + width - 1;
835 /******************************************************************************
836 * ReadConsoleInputA [KERNEL32.@] Reads data from a console
839 * handle [I] Handle to console input buffer
840 * buffer [O] Address of buffer for read data
841 * count [I] Number of records to read
842 * pRead [O] Address of number of records read
848 BOOL WINAPI ReadConsoleInputA( HANDLE handle, PINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
852 if (!ReadConsoleInputW( handle, buffer, count, &read )) return FALSE;
853 input_records_WtoA( buffer, read );
854 if (pRead) *pRead = read;
859 /***********************************************************************
860 * PeekConsoleInputA (KERNEL32.@)
862 * Gets 'count' first events (or less) from input queue.
864 BOOL WINAPI PeekConsoleInputA( HANDLE handle, PINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
868 if (!PeekConsoleInputW( handle, buffer, count, &read )) return FALSE;
869 input_records_WtoA( buffer, read );
870 if (pRead) *pRead = read;
875 /***********************************************************************
876 * PeekConsoleInputW (KERNEL32.@)
878 BOOL WINAPI PeekConsoleInputW( HANDLE handle, PINPUT_RECORD buffer, DWORD count, LPDWORD read )
881 SERVER_START_REQ( read_console_input )
883 req->handle = console_handle_unmap(handle);
885 wine_server_set_reply( req, buffer, count * sizeof(INPUT_RECORD) );
886 if ((ret = !wine_server_call_err( req )))
888 if (read) *read = count ? reply->read : 0;
896 /***********************************************************************
897 * GetNumberOfConsoleInputEvents (KERNEL32.@)
899 BOOL WINAPI GetNumberOfConsoleInputEvents( HANDLE handle, LPDWORD nrofevents )
902 SERVER_START_REQ( read_console_input )
904 req->handle = console_handle_unmap(handle);
906 if ((ret = !wine_server_call_err( req )))
908 if (nrofevents) *nrofevents = reply->read;
916 /******************************************************************************
919 * Helper function for ReadConsole, ReadConsoleInput and FlushConsoleInputBuffer
922 * 0 for error, 1 for no INPUT_RECORD ready, 2 with INPUT_RECORD ready
924 enum read_console_input_return {rci_error = 0, rci_timeout = 1, rci_gotone = 2};
925 static enum read_console_input_return read_console_input(HANDLE handle, PINPUT_RECORD ir, DWORD timeout)
927 enum read_console_input_return ret;
929 if (WaitForSingleObject(GetConsoleInputWaitHandle(), timeout) != WAIT_OBJECT_0)
931 SERVER_START_REQ( read_console_input )
933 req->handle = console_handle_unmap(handle);
935 wine_server_set_reply( req, ir, sizeof(INPUT_RECORD) );
936 if (wine_server_call_err( req ) || !reply->read) ret = rci_error;
937 else ret = rci_gotone;
945 /***********************************************************************
946 * FlushConsoleInputBuffer (KERNEL32.@)
948 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
950 enum read_console_input_return last;
953 while ((last = read_console_input(handle, &ir, 0)) == rci_gotone);
955 return last == rci_timeout;
959 /***********************************************************************
960 * SetConsoleTitleA (KERNEL32.@)
962 BOOL WINAPI SetConsoleTitleA( LPCSTR title )
967 DWORD len = MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, NULL, 0 );
968 if (!(titleW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
969 MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, titleW, len );
970 ret = SetConsoleTitleW(titleW);
971 HeapFree(GetProcessHeap(), 0, titleW);
976 /***********************************************************************
977 * GetConsoleKeyboardLayoutNameA (KERNEL32.@)
979 BOOL WINAPI GetConsoleKeyboardLayoutNameA(LPSTR layoutName)
981 FIXME( "stub %p\n", layoutName);
985 /***********************************************************************
986 * GetConsoleKeyboardLayoutNameW (KERNEL32.@)
988 BOOL WINAPI GetConsoleKeyboardLayoutNameW(LPWSTR layoutName)
990 FIXME( "stub %p\n", layoutName);
994 static WCHAR input_exe[MAX_PATH + 1];
996 /***********************************************************************
997 * GetConsoleInputExeNameW (KERNEL32.@)
999 BOOL WINAPI GetConsoleInputExeNameW(DWORD buflen, LPWSTR buffer)
1001 TRACE("%u %p\n", buflen, buffer);
1003 RtlEnterCriticalSection(&CONSOLE_CritSect);
1004 if (buflen > strlenW(input_exe)) strcpyW(buffer, input_exe);
1005 else SetLastError(ERROR_BUFFER_OVERFLOW);
1006 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1011 /***********************************************************************
1012 * GetConsoleInputExeNameA (KERNEL32.@)
1014 BOOL WINAPI GetConsoleInputExeNameA(DWORD buflen, LPSTR buffer)
1016 TRACE("%u %p\n", buflen, buffer);
1018 RtlEnterCriticalSection(&CONSOLE_CritSect);
1019 if (WideCharToMultiByte(CP_ACP, 0, input_exe, -1, NULL, 0, NULL, NULL) <= buflen)
1020 WideCharToMultiByte(CP_ACP, 0, input_exe, -1, buffer, buflen, NULL, NULL);
1021 else SetLastError(ERROR_BUFFER_OVERFLOW);
1022 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1027 /***********************************************************************
1028 * GetConsoleTitleA (KERNEL32.@)
1030 * See GetConsoleTitleW.
1032 DWORD WINAPI GetConsoleTitleA(LPSTR title, DWORD size)
1034 WCHAR *ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * size);
1038 ret = GetConsoleTitleW( ptr, size );
1041 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, ret + 1, title, size, NULL, NULL);
1042 ret = strlen(title);
1044 HeapFree(GetProcessHeap(), 0, ptr);
1049 /******************************************************************************
1050 * GetConsoleTitleW [KERNEL32.@] Retrieves title string for console
1053 * title [O] Address of buffer for title
1054 * size [I] Size of buffer
1057 * Success: Length of string copied
1060 DWORD WINAPI GetConsoleTitleW(LPWSTR title, DWORD size)
1064 SERVER_START_REQ( get_console_input_info )
1067 wine_server_set_reply( req, title, (size-1) * sizeof(WCHAR) );
1068 if (!wine_server_call_err( req ))
1070 ret = wine_server_reply_size(reply) / sizeof(WCHAR);
1079 /***********************************************************************
1080 * GetLargestConsoleWindowSize (KERNEL32.@)
1083 * This should return a COORD, but calling convention for returning
1084 * structures is different between Windows and gcc on i386.
1089 #undef GetLargestConsoleWindowSize
1090 DWORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
1098 TRACE("(%p), returning %dx%d (%x)\n", hConsoleOutput, x.c.X, x.c.Y, x.w);
1101 #endif /* defined(__i386__) */
1104 /***********************************************************************
1105 * GetLargestConsoleWindowSize (KERNEL32.@)
1108 * This should return a COORD, but calling convention for returning
1109 * structures is different between Windows and gcc on i386.
1114 COORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
1119 TRACE("(%p), returning %dx%d\n", hConsoleOutput, c.X, c.Y);
1122 #endif /* defined(__i386__) */
1124 static WCHAR* S_EditString /* = NULL */;
1125 static unsigned S_EditStrPos /* = 0 */;
1127 /***********************************************************************
1128 * FreeConsole (KERNEL32.@)
1130 BOOL WINAPI FreeConsole(VOID)
1134 SERVER_START_REQ(free_console)
1136 ret = !wine_server_call_err( req );
1142 /******************************************************************
1143 * start_console_renderer
1145 * helper for AllocConsole
1146 * starts the renderer process
1148 static BOOL start_console_renderer_helper(const char* appname, STARTUPINFOA* si,
1153 PROCESS_INFORMATION pi;
1155 /* FIXME: use dynamic allocation for most of the buffers below */
1156 ret = snprintf(buffer, sizeof(buffer), "%s --use-event=%ld", appname, (DWORD_PTR)hEvent);
1157 if ((ret > -1) && (ret < sizeof(buffer)) &&
1158 CreateProcessA(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS,
1159 NULL, NULL, si, &pi))
1161 if (WaitForSingleObject(hEvent, INFINITE) != WAIT_OBJECT_0) return FALSE;
1163 TRACE("Started wineconsole pid=%08x tid=%08x\n",
1164 pi.dwProcessId, pi.dwThreadId);
1171 static BOOL start_console_renderer(STARTUPINFOA* si)
1175 OBJECT_ATTRIBUTES attr;
1178 attr.Length = sizeof(attr);
1179 attr.RootDirectory = 0;
1180 attr.Attributes = OBJ_INHERIT;
1181 attr.ObjectName = NULL;
1182 attr.SecurityDescriptor = NULL;
1183 attr.SecurityQualityOfService = NULL;
1185 NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, &attr, TRUE, FALSE);
1186 if (!hEvent) return FALSE;
1188 /* first try environment variable */
1189 if ((p = getenv("WINECONSOLE")) != NULL)
1191 ret = start_console_renderer_helper(p, si, hEvent);
1193 ERR("Couldn't launch Wine console from WINECONSOLE env var (%s)... "
1194 "trying default access\n", p);
1197 /* then try the regular PATH */
1199 ret = start_console_renderer_helper("wineconsole", si, hEvent);
1201 CloseHandle(hEvent);
1205 /***********************************************************************
1206 * AllocConsole (KERNEL32.@)
1208 * creates an xterm with a pty to our program
1210 BOOL WINAPI AllocConsole(void)
1212 HANDLE handle_in = INVALID_HANDLE_VALUE;
1213 HANDLE handle_out = INVALID_HANDLE_VALUE;
1214 HANDLE handle_err = INVALID_HANDLE_VALUE;
1215 STARTUPINFOA siCurrent;
1216 STARTUPINFOA siConsole;
1221 handle_in = OpenConsoleW( coninW, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
1222 FALSE, OPEN_EXISTING );
1224 if (VerifyConsoleIoHandle(handle_in))
1226 /* we already have a console opened on this process, don't create a new one */
1227 CloseHandle(handle_in);
1230 /* happens when we're running on a Unix console */
1231 if (handle_in != INVALID_HANDLE_VALUE) CloseHandle(handle_in);
1233 GetStartupInfoA(&siCurrent);
1235 memset(&siConsole, 0, sizeof(siConsole));
1236 siConsole.cb = sizeof(siConsole);
1237 /* setup a view arguments for wineconsole (it'll use them as default values) */
1238 if (siCurrent.dwFlags & STARTF_USECOUNTCHARS)
1240 siConsole.dwFlags |= STARTF_USECOUNTCHARS;
1241 siConsole.dwXCountChars = siCurrent.dwXCountChars;
1242 siConsole.dwYCountChars = siCurrent.dwYCountChars;
1244 if (siCurrent.dwFlags & STARTF_USEFILLATTRIBUTE)
1246 siConsole.dwFlags |= STARTF_USEFILLATTRIBUTE;
1247 siConsole.dwFillAttribute = siCurrent.dwFillAttribute;
1249 if (siCurrent.dwFlags & STARTF_USESHOWWINDOW)
1251 siConsole.dwFlags |= STARTF_USESHOWWINDOW;
1252 siConsole.wShowWindow = siCurrent.wShowWindow;
1254 /* FIXME (should pass the unicode form) */
1255 if (siCurrent.lpTitle)
1256 siConsole.lpTitle = siCurrent.lpTitle;
1257 else if (GetModuleFileNameA(0, buffer, sizeof(buffer)))
1259 buffer[sizeof(buffer) - 1] = '\0';
1260 siConsole.lpTitle = buffer;
1263 if (!start_console_renderer(&siConsole))
1266 if( !(siCurrent.dwFlags & STARTF_USESTDHANDLES) ) {
1267 /* all std I/O handles are inheritable by default */
1268 handle_in = OpenConsoleW( coninW, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
1269 TRUE, OPEN_EXISTING );
1270 if (handle_in == INVALID_HANDLE_VALUE) goto the_end;
1272 handle_out = OpenConsoleW( conoutW, GENERIC_READ|GENERIC_WRITE,
1273 TRUE, OPEN_EXISTING );
1274 if (handle_out == INVALID_HANDLE_VALUE) goto the_end;
1276 if (!DuplicateHandle(GetCurrentProcess(), handle_out, GetCurrentProcess(),
1277 &handle_err, 0, TRUE, DUPLICATE_SAME_ACCESS))
1280 /* STARTF_USESTDHANDLES flag: use handles from StartupInfo */
1281 handle_in = siCurrent.hStdInput;
1282 handle_out = siCurrent.hStdOutput;
1283 handle_err = siCurrent.hStdError;
1286 /* NT resets the STD_*_HANDLEs on console alloc */
1287 SetStdHandle(STD_INPUT_HANDLE, handle_in);
1288 SetStdHandle(STD_OUTPUT_HANDLE, handle_out);
1289 SetStdHandle(STD_ERROR_HANDLE, handle_err);
1291 SetLastError(ERROR_SUCCESS);
1296 ERR("Can't allocate console\n");
1297 if (handle_in != INVALID_HANDLE_VALUE) CloseHandle(handle_in);
1298 if (handle_out != INVALID_HANDLE_VALUE) CloseHandle(handle_out);
1299 if (handle_err != INVALID_HANDLE_VALUE) CloseHandle(handle_err);
1305 /***********************************************************************
1306 * ReadConsoleA (KERNEL32.@)
1308 BOOL WINAPI ReadConsoleA(HANDLE hConsoleInput, LPVOID lpBuffer, DWORD nNumberOfCharsToRead,
1309 LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1311 LPWSTR ptr = HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead * sizeof(WCHAR));
1315 if ((ret = ReadConsoleW(hConsoleInput, ptr, nNumberOfCharsToRead, &ncr, NULL)))
1316 ncr = WideCharToMultiByte(GetConsoleCP(), 0, ptr, ncr, lpBuffer, nNumberOfCharsToRead, NULL, NULL);
1318 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = ncr;
1319 HeapFree(GetProcessHeap(), 0, ptr);
1324 /***********************************************************************
1325 * ReadConsoleW (KERNEL32.@)
1327 BOOL WINAPI ReadConsoleW(HANDLE hConsoleInput, LPVOID lpBuffer,
1328 DWORD nNumberOfCharsToRead, LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1331 LPWSTR xbuf = (LPWSTR)lpBuffer;
1334 TRACE("(%p,%p,%d,%p,%p)\n",
1335 hConsoleInput, lpBuffer, nNumberOfCharsToRead, lpNumberOfCharsRead, lpReserved);
1337 if (!GetConsoleMode(hConsoleInput, &mode))
1340 if (mode & ENABLE_LINE_INPUT)
1342 if (!S_EditString || S_EditString[S_EditStrPos] == 0)
1344 HeapFree(GetProcessHeap(), 0, S_EditString);
1345 if (!(S_EditString = CONSOLE_Readline(hConsoleInput)))
1349 charsread = lstrlenW(&S_EditString[S_EditStrPos]);
1350 if (charsread > nNumberOfCharsToRead) charsread = nNumberOfCharsToRead;
1351 memcpy(xbuf, &S_EditString[S_EditStrPos], charsread * sizeof(WCHAR));
1352 S_EditStrPos += charsread;
1357 DWORD timeout = INFINITE;
1359 /* FIXME: should we read at least 1 char? The SDK does not say */
1360 /* wait for at least one available input record (it doesn't mean we'll have
1361 * chars stored in xbuf...)
1366 if (read_console_input(hConsoleInput, &ir, timeout) != rci_gotone) break;
1368 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown &&
1369 ir.Event.KeyEvent.uChar.UnicodeChar &&
1370 !(ir.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
1372 xbuf[charsread++] = ir.Event.KeyEvent.uChar.UnicodeChar;
1374 } while (charsread < nNumberOfCharsToRead);
1375 /* nothing has been read */
1376 if (timeout == INFINITE) return FALSE;
1379 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = charsread;
1385 /***********************************************************************
1386 * ReadConsoleInputW (KERNEL32.@)
1388 BOOL WINAPI ReadConsoleInputW(HANDLE hConsoleInput, PINPUT_RECORD lpBuffer,
1389 DWORD nLength, LPDWORD lpNumberOfEventsRead)
1392 DWORD timeout = INFINITE;
1396 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = 0;
1400 /* loop until we get at least one event */
1401 while (read_console_input(hConsoleInput, &lpBuffer[idx], timeout) == rci_gotone &&
1405 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = idx;
1410 /******************************************************************************
1411 * WriteConsoleOutputCharacterW [KERNEL32.@]
1413 * Copy character to consecutive cells in the console screen buffer.
1416 * hConsoleOutput [I] Handle to screen buffer
1417 * str [I] Pointer to buffer with chars to write
1418 * length [I] Number of cells to write to
1419 * coord [I] Coords of first cell
1420 * lpNumCharsWritten [O] Pointer to number of cells written
1427 BOOL WINAPI WriteConsoleOutputCharacterW( HANDLE hConsoleOutput, LPCWSTR str, DWORD length,
1428 COORD coord, LPDWORD lpNumCharsWritten )
1432 TRACE("(%p,%s,%d,%dx%d,%p)\n", hConsoleOutput,
1433 debugstr_wn(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
1435 SERVER_START_REQ( write_console_output )
1437 req->handle = console_handle_unmap(hConsoleOutput);
1440 req->mode = CHAR_INFO_MODE_TEXT;
1442 wine_server_add_data( req, str, length * sizeof(WCHAR) );
1443 if ((ret = !wine_server_call_err( req )))
1445 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
1453 /******************************************************************************
1454 * SetConsoleTitleW [KERNEL32.@] Sets title bar string for console
1457 * title [I] Address of new title
1463 BOOL WINAPI SetConsoleTitleW(LPCWSTR title)
1467 TRACE("(%s)\n", debugstr_w(title));
1468 SERVER_START_REQ( set_console_input_info )
1471 req->mask = SET_CONSOLE_INPUT_INFO_TITLE;
1472 wine_server_add_data( req, title, strlenW(title) * sizeof(WCHAR) );
1473 ret = !wine_server_call_err( req );
1480 /***********************************************************************
1481 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
1483 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1485 FIXME("(%p): stub\n", nrofbuttons);
1490 /******************************************************************************
1491 * SetConsoleInputExeNameW [KERNEL32.@]
1493 BOOL WINAPI SetConsoleInputExeNameW(LPCWSTR name)
1495 TRACE("(%s)\n", debugstr_w(name));
1497 if (!name || !name[0])
1499 SetLastError(ERROR_INVALID_PARAMETER);
1503 RtlEnterCriticalSection(&CONSOLE_CritSect);
1504 if (strlenW(name) < sizeof(input_exe)/sizeof(WCHAR)) strcpyW(input_exe, name);
1505 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1510 /******************************************************************************
1511 * SetConsoleInputExeNameA [KERNEL32.@]
1513 BOOL WINAPI SetConsoleInputExeNameA(LPCSTR name)
1519 if (!name || !name[0])
1521 SetLastError(ERROR_INVALID_PARAMETER);
1525 len = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
1526 if (!(nameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
1528 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, len);
1529 ret = SetConsoleInputExeNameW(nameW);
1530 HeapFree(GetProcessHeap(), 0, nameW);
1535 /******************************************************************
1536 * CONSOLE_DefaultHandler
1538 * Final control event handler
1540 static BOOL WINAPI CONSOLE_DefaultHandler(DWORD dwCtrlType)
1542 FIXME("Terminating process %x on event %x\n", GetCurrentProcessId(), dwCtrlType);
1544 /* should never go here */
1548 /******************************************************************************
1549 * SetConsoleCtrlHandler [KERNEL32.@] Adds function to calling process list
1552 * func [I] Address of handler function
1553 * add [I] Handler to add or remove
1560 struct ConsoleHandler
1562 PHANDLER_ROUTINE handler;
1563 struct ConsoleHandler* next;
1566 static struct ConsoleHandler CONSOLE_DefaultConsoleHandler = {CONSOLE_DefaultHandler, NULL};
1567 static struct ConsoleHandler* CONSOLE_Handlers = &CONSOLE_DefaultConsoleHandler;
1569 /*****************************************************************************/
1571 /******************************************************************
1572 * SetConsoleCtrlHandler (KERNEL32.@)
1574 BOOL WINAPI SetConsoleCtrlHandler(PHANDLER_ROUTINE func, BOOL add)
1578 TRACE("(%p,%i)\n", func, add);
1582 RtlEnterCriticalSection(&CONSOLE_CritSect);
1584 NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags |= 1;
1586 NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags &= ~1;
1587 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1591 struct ConsoleHandler* ch = HeapAlloc(GetProcessHeap(), 0, sizeof(struct ConsoleHandler));
1593 if (!ch) return FALSE;
1595 RtlEnterCriticalSection(&CONSOLE_CritSect);
1596 ch->next = CONSOLE_Handlers;
1597 CONSOLE_Handlers = ch;
1598 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1602 struct ConsoleHandler** ch;
1603 RtlEnterCriticalSection(&CONSOLE_CritSect);
1604 for (ch = &CONSOLE_Handlers; *ch; ch = &(*ch)->next)
1606 if ((*ch)->handler == func) break;
1610 struct ConsoleHandler* rch = *ch;
1613 if (rch == &CONSOLE_DefaultConsoleHandler)
1615 ERR("Who's trying to remove default handler???\n");
1616 SetLastError(ERROR_INVALID_PARAMETER);
1622 HeapFree(GetProcessHeap(), 0, rch);
1627 WARN("Attempt to remove non-installed CtrlHandler %p\n", func);
1628 SetLastError(ERROR_INVALID_PARAMETER);
1631 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1636 static WINE_EXCEPTION_FILTER(CONSOLE_CtrlEventHandler)
1638 TRACE("(%x)\n", GetExceptionCode());
1639 return EXCEPTION_EXECUTE_HANDLER;
1642 /******************************************************************
1643 * CONSOLE_SendEventThread
1645 * Internal helper to pass an event to the list on installed handlers
1647 static DWORD WINAPI CONSOLE_SendEventThread(void* pmt)
1649 DWORD_PTR event = (DWORD_PTR)pmt;
1650 struct ConsoleHandler* ch;
1652 if (event == CTRL_C_EVENT)
1654 BOOL caught_by_dbg = TRUE;
1655 /* First, try to pass the ctrl-C event to the debugger (if any)
1656 * If it continues, there's nothing more to do
1657 * Otherwise, we need to send the ctrl-C event to the handlers
1661 RaiseException( DBG_CONTROL_C, 0, 0, NULL );
1663 __EXCEPT(CONSOLE_CtrlEventHandler)
1665 caught_by_dbg = FALSE;
1668 if (caught_by_dbg) return 0;
1669 /* the debugger didn't continue... so, pass to ctrl handlers */
1671 RtlEnterCriticalSection(&CONSOLE_CritSect);
1672 for (ch = CONSOLE_Handlers; ch; ch = ch->next)
1674 if (ch->handler(event)) break;
1676 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1680 /******************************************************************
1681 * CONSOLE_HandleCtrlC
1683 * Check whether the shall manipulate CtrlC events
1685 int CONSOLE_HandleCtrlC(unsigned sig)
1687 /* FIXME: better test whether a console is attached to this process ??? */
1688 extern unsigned CONSOLE_GetNumHistoryEntries(void);
1689 if (CONSOLE_GetNumHistoryEntries() == (unsigned)-1) return 0;
1691 /* check if we have to ignore ctrl-C events */
1692 if (!(NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags & 1))
1694 /* Create a separate thread to signal all the events.
1695 * This is needed because:
1696 * - this function can be called in an Unix signal handler (hence on an
1697 * different stack than the thread that's running). This breaks the
1698 * Win32 exception mechanisms (where the thread's stack is checked).
1699 * - since the current thread, while processing the signal, can hold the
1700 * console critical section, we need another execution environment where
1701 * we can wait on this critical section
1703 CreateThread(NULL, 0, CONSOLE_SendEventThread, (void*)CTRL_C_EVENT, 0, NULL);
1708 /******************************************************************************
1709 * GenerateConsoleCtrlEvent [KERNEL32.@] Simulate a CTRL-C or CTRL-BREAK
1712 * dwCtrlEvent [I] Type of event
1713 * dwProcessGroupID [I] Process group ID to send event to
1717 * Failure: False (and *should* [but doesn't] set LastError)
1719 BOOL WINAPI GenerateConsoleCtrlEvent(DWORD dwCtrlEvent,
1720 DWORD dwProcessGroupID)
1724 TRACE("(%d, %d)\n", dwCtrlEvent, dwProcessGroupID);
1726 if (dwCtrlEvent != CTRL_C_EVENT && dwCtrlEvent != CTRL_BREAK_EVENT)
1728 ERR("Invalid event %d for PGID %d\n", dwCtrlEvent, dwProcessGroupID);
1732 SERVER_START_REQ( send_console_signal )
1734 req->signal = dwCtrlEvent;
1735 req->group_id = dwProcessGroupID;
1736 ret = !wine_server_call_err( req );
1740 /* FIXME: shall this function be synchronous, ie only return when all events
1741 * have been handled by all processes in the given group ?
1742 * As of today, we don't wait...
1748 /******************************************************************************
1749 * CreateConsoleScreenBuffer [KERNEL32.@] Creates a console screen buffer
1752 * dwDesiredAccess [I] Access flag
1753 * dwShareMode [I] Buffer share mode
1754 * sa [I] Security attributes
1755 * dwFlags [I] Type of buffer to create
1756 * lpScreenBufferData [I] Reserved
1759 * Should call SetLastError
1762 * Success: Handle to new console screen buffer
1763 * Failure: INVALID_HANDLE_VALUE
1765 HANDLE WINAPI CreateConsoleScreenBuffer(DWORD dwDesiredAccess, DWORD dwShareMode,
1766 LPSECURITY_ATTRIBUTES sa, DWORD dwFlags,
1767 LPVOID lpScreenBufferData)
1769 HANDLE ret = INVALID_HANDLE_VALUE;
1771 TRACE("(%d,%d,%p,%d,%p)\n",
1772 dwDesiredAccess, dwShareMode, sa, dwFlags, lpScreenBufferData);
1774 if (dwFlags != CONSOLE_TEXTMODE_BUFFER || lpScreenBufferData != NULL)
1776 SetLastError(ERROR_INVALID_PARAMETER);
1777 return INVALID_HANDLE_VALUE;
1780 SERVER_START_REQ(create_console_output)
1783 req->access = dwDesiredAccess;
1784 req->attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
1785 req->share = dwShareMode;
1786 if (!wine_server_call_err( req )) ret = reply->handle_out;
1794 /***********************************************************************
1795 * GetConsoleScreenBufferInfo (KERNEL32.@)
1797 BOOL WINAPI GetConsoleScreenBufferInfo(HANDLE hConsoleOutput, LPCONSOLE_SCREEN_BUFFER_INFO csbi)
1801 SERVER_START_REQ(get_console_output_info)
1803 req->handle = console_handle_unmap(hConsoleOutput);
1804 if ((ret = !wine_server_call_err( req )))
1806 csbi->dwSize.X = reply->width;
1807 csbi->dwSize.Y = reply->height;
1808 csbi->dwCursorPosition.X = reply->cursor_x;
1809 csbi->dwCursorPosition.Y = reply->cursor_y;
1810 csbi->wAttributes = reply->attr;
1811 csbi->srWindow.Left = reply->win_left;
1812 csbi->srWindow.Right = reply->win_right;
1813 csbi->srWindow.Top = reply->win_top;
1814 csbi->srWindow.Bottom = reply->win_bottom;
1815 csbi->dwMaximumWindowSize.X = reply->max_width;
1816 csbi->dwMaximumWindowSize.Y = reply->max_height;
1821 TRACE("(%p,(%d,%d) (%d,%d) %d (%d,%d-%d,%d) (%d,%d)\n",
1822 hConsoleOutput, csbi->dwSize.X, csbi->dwSize.Y,
1823 csbi->dwCursorPosition.X, csbi->dwCursorPosition.Y,
1825 csbi->srWindow.Left, csbi->srWindow.Top, csbi->srWindow.Right, csbi->srWindow.Bottom,
1826 csbi->dwMaximumWindowSize.X, csbi->dwMaximumWindowSize.Y);
1832 /******************************************************************************
1833 * SetConsoleActiveScreenBuffer [KERNEL32.@] Sets buffer to current console
1839 BOOL WINAPI SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput)
1843 TRACE("(%p)\n", hConsoleOutput);
1845 SERVER_START_REQ( set_console_input_info )
1848 req->mask = SET_CONSOLE_INPUT_INFO_ACTIVE_SB;
1849 req->active_sb = hConsoleOutput;
1850 ret = !wine_server_call_err( req );
1857 /***********************************************************************
1858 * GetConsoleMode (KERNEL32.@)
1860 BOOL WINAPI GetConsoleMode(HANDLE hcon, LPDWORD mode)
1864 SERVER_START_REQ(get_console_mode)
1866 req->handle = console_handle_unmap(hcon);
1867 ret = !wine_server_call_err( req );
1868 if (ret && mode) *mode = reply->mode;
1875 /******************************************************************************
1876 * SetConsoleMode [KERNEL32.@] Sets input mode of console's input buffer
1879 * hcon [I] Handle to console input or screen buffer
1880 * mode [I] Input or output mode to set
1887 * ENABLE_PROCESSED_INPUT 0x01
1888 * ENABLE_LINE_INPUT 0x02
1889 * ENABLE_ECHO_INPUT 0x04
1890 * ENABLE_WINDOW_INPUT 0x08
1891 * ENABLE_MOUSE_INPUT 0x10
1893 BOOL WINAPI SetConsoleMode(HANDLE hcon, DWORD mode)
1897 SERVER_START_REQ(set_console_mode)
1899 req->handle = console_handle_unmap(hcon);
1901 ret = !wine_server_call_err( req );
1904 /* FIXME: when resetting a console input to editline mode, I think we should
1905 * empty the S_EditString buffer
1908 TRACE("(%p,%x) retval == %d\n", hcon, mode, ret);
1914 /******************************************************************
1915 * CONSOLE_WriteChars
1917 * WriteConsoleOutput helper: hides server call semantics
1918 * writes a string at a given pos with standard attribute
1920 static int CONSOLE_WriteChars(HANDLE hCon, LPCWSTR lpBuffer, int nc, COORD* pos)
1926 SERVER_START_REQ( write_console_output )
1928 req->handle = console_handle_unmap(hCon);
1931 req->mode = CHAR_INFO_MODE_TEXTSTDATTR;
1933 wine_server_add_data( req, lpBuffer, nc * sizeof(WCHAR) );
1934 if (!wine_server_call_err( req )) written = reply->written;
1938 if (written > 0) pos->X += written;
1942 /******************************************************************
1945 * WriteConsoleOutput helper: handles passing to next line (+scrolling if necessary)
1948 static int next_line(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi)
1954 csbi->dwCursorPosition.X = 0;
1955 csbi->dwCursorPosition.Y++;
1957 if (csbi->dwCursorPosition.Y < csbi->dwSize.Y) return 1;
1960 src.Bottom = csbi->dwSize.Y - 1;
1962 src.Right = csbi->dwSize.X - 1;
1967 ci.Attributes = csbi->wAttributes;
1968 ci.Char.UnicodeChar = ' ';
1970 csbi->dwCursorPosition.Y--;
1971 if (!ScrollConsoleScreenBufferW(hCon, &src, NULL, dst, &ci))
1976 /******************************************************************
1979 * WriteConsoleOutput helper: writes a block of non special characters
1980 * Block can spread on several lines, and wrapping, if needed, is
1984 static int write_block(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi,
1985 DWORD mode, LPCWSTR ptr, int len)
1987 int blk; /* number of chars to write on current line */
1988 int done; /* number of chars already written */
1990 if (len <= 0) return 1;
1992 if (mode & ENABLE_WRAP_AT_EOL_OUTPUT) /* writes remaining on next line */
1994 for (done = 0; done < len; done += blk)
1996 blk = min(len - done, csbi->dwSize.X - csbi->dwCursorPosition.X);
1998 if (CONSOLE_WriteChars(hCon, ptr + done, blk, &csbi->dwCursorPosition) != blk)
2000 if (csbi->dwCursorPosition.X == csbi->dwSize.X && !next_line(hCon, csbi))
2006 int pos = csbi->dwCursorPosition.X;
2007 /* FIXME: we could reduce the number of loops
2008 * but, in most cases we wouldn't gain lots of time (it would only
2009 * happen if we're asked to overwrite more than twice the part of the line,
2012 for (blk = done = 0; done < len; done += blk)
2014 blk = min(len - done, csbi->dwSize.X - csbi->dwCursorPosition.X);
2016 csbi->dwCursorPosition.X = pos;
2017 if (CONSOLE_WriteChars(hCon, ptr + done, blk, &csbi->dwCursorPosition) != blk)
2025 /***********************************************************************
2026 * WriteConsoleW (KERNEL32.@)
2028 BOOL WINAPI WriteConsoleW(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
2029 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
2033 const WCHAR* psz = lpBuffer;
2034 CONSOLE_SCREEN_BUFFER_INFO csbi;
2037 TRACE("%p %s %d %p %p\n",
2038 hConsoleOutput, debugstr_wn(lpBuffer, nNumberOfCharsToWrite),
2039 nNumberOfCharsToWrite, lpNumberOfCharsWritten, lpReserved);
2041 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
2043 if (!GetConsoleMode(hConsoleOutput, &mode) ||
2044 !GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
2047 if (mode & ENABLE_PROCESSED_OUTPUT)
2051 for (i = 0; i < nNumberOfCharsToWrite; i++)
2055 case '\b': case '\t': case '\n': case '\a': case '\r':
2056 /* don't handle here the i-th char... done below */
2057 if ((k = i - first) > 0)
2059 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
2069 if (csbi.dwCursorPosition.X > 0) csbi.dwCursorPosition.X--;
2073 WCHAR tmp[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
2075 if (!write_block(hConsoleOutput, &csbi, mode, tmp,
2076 ((csbi.dwCursorPosition.X + 8) & ~7) - csbi.dwCursorPosition.X))
2081 next_line(hConsoleOutput, &csbi);
2087 csbi.dwCursorPosition.X = 0;
2095 /* write the remaining block (if any) if processed output is enabled, or the
2096 * entire buffer otherwise
2098 if ((k = nNumberOfCharsToWrite - first) > 0)
2100 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
2106 SetConsoleCursorPosition(hConsoleOutput, csbi.dwCursorPosition);
2107 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = nw;
2112 /***********************************************************************
2113 * WriteConsoleA (KERNEL32.@)
2115 BOOL WINAPI WriteConsoleA(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
2116 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
2122 n = MultiByteToWideChar(GetConsoleOutputCP(), 0, lpBuffer, nNumberOfCharsToWrite, NULL, 0);
2124 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
2125 xstring = HeapAlloc(GetProcessHeap(), 0, n * sizeof(WCHAR));
2126 if (!xstring) return 0;
2128 MultiByteToWideChar(GetConsoleOutputCP(), 0, lpBuffer, nNumberOfCharsToWrite, xstring, n);
2130 ret = WriteConsoleW(hConsoleOutput, xstring, n, lpNumberOfCharsWritten, 0);
2132 HeapFree(GetProcessHeap(), 0, xstring);
2137 /******************************************************************************
2138 * SetConsoleCursorPosition [KERNEL32.@]
2139 * Sets the cursor position in console
2142 * hConsoleOutput [I] Handle of console screen buffer
2143 * dwCursorPosition [I] New cursor position coordinates
2149 BOOL WINAPI SetConsoleCursorPosition(HANDLE hcon, COORD pos)
2152 CONSOLE_SCREEN_BUFFER_INFO csbi;
2156 TRACE("%p %d %d\n", hcon, pos.X, pos.Y);
2158 SERVER_START_REQ(set_console_output_info)
2160 req->handle = console_handle_unmap(hcon);
2161 req->cursor_x = pos.X;
2162 req->cursor_y = pos.Y;
2163 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_POS;
2164 ret = !wine_server_call_err( req );
2168 if (!ret || !GetConsoleScreenBufferInfo(hcon, &csbi))
2171 /* if cursor is no longer visible, scroll the visible window... */
2172 w = csbi.srWindow.Right - csbi.srWindow.Left + 1;
2173 h = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
2174 if (pos.X < csbi.srWindow.Left)
2176 csbi.srWindow.Left = min(pos.X, csbi.dwSize.X - w);
2179 else if (pos.X > csbi.srWindow.Right)
2181 csbi.srWindow.Left = max(pos.X, w) - w + 1;
2184 csbi.srWindow.Right = csbi.srWindow.Left + w - 1;
2186 if (pos.Y < csbi.srWindow.Top)
2188 csbi.srWindow.Top = min(pos.Y, csbi.dwSize.Y - h);
2191 else if (pos.Y > csbi.srWindow.Bottom)
2193 csbi.srWindow.Top = max(pos.Y, h) - h + 1;
2196 csbi.srWindow.Bottom = csbi.srWindow.Top + h - 1;
2198 ret = (do_move) ? SetConsoleWindowInfo(hcon, TRUE, &csbi.srWindow) : TRUE;
2203 /******************************************************************************
2204 * GetConsoleCursorInfo [KERNEL32.@] Gets size and visibility of console
2207 * hcon [I] Handle to console screen buffer
2208 * cinfo [O] Address of cursor information
2214 BOOL WINAPI GetConsoleCursorInfo(HANDLE hCon, LPCONSOLE_CURSOR_INFO cinfo)
2218 SERVER_START_REQ(get_console_output_info)
2220 req->handle = console_handle_unmap(hCon);
2221 ret = !wine_server_call_err( req );
2224 cinfo->dwSize = reply->cursor_size;
2225 cinfo->bVisible = reply->cursor_visible;
2230 TRACE("(%p) returning (%d,%d)\n", hCon, cinfo->dwSize, cinfo->bVisible);
2235 /******************************************************************************
2236 * SetConsoleCursorInfo [KERNEL32.@] Sets size and visibility of cursor
2239 * hcon [I] Handle to console screen buffer
2240 * cinfo [I] Address of cursor information
2245 BOOL WINAPI SetConsoleCursorInfo(HANDLE hCon, LPCONSOLE_CURSOR_INFO cinfo)
2249 TRACE("(%p,%d,%d)\n", hCon, cinfo->dwSize, cinfo->bVisible);
2250 SERVER_START_REQ(set_console_output_info)
2252 req->handle = console_handle_unmap(hCon);
2253 req->cursor_size = cinfo->dwSize;
2254 req->cursor_visible = cinfo->bVisible;
2255 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM;
2256 ret = !wine_server_call_err( req );
2263 /******************************************************************************
2264 * SetConsoleWindowInfo [KERNEL32.@] Sets size and position of console
2267 * hcon [I] Handle to console screen buffer
2268 * bAbsolute [I] Coordinate type flag
2269 * window [I] Address of new window rectangle
2274 BOOL WINAPI SetConsoleWindowInfo(HANDLE hCon, BOOL bAbsolute, LPSMALL_RECT window)
2276 SMALL_RECT p = *window;
2279 TRACE("(%p,%d,(%d,%d-%d,%d))\n", hCon, bAbsolute, p.Left, p.Top, p.Right, p.Bottom);
2283 CONSOLE_SCREEN_BUFFER_INFO csbi;
2285 if (!GetConsoleScreenBufferInfo(hCon, &csbi))
2287 p.Left += csbi.srWindow.Left;
2288 p.Top += csbi.srWindow.Top;
2289 p.Right += csbi.srWindow.Right;
2290 p.Bottom += csbi.srWindow.Bottom;
2292 SERVER_START_REQ(set_console_output_info)
2294 req->handle = console_handle_unmap(hCon);
2295 req->win_left = p.Left;
2296 req->win_top = p.Top;
2297 req->win_right = p.Right;
2298 req->win_bottom = p.Bottom;
2299 req->mask = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
2300 ret = !wine_server_call_err( req );
2308 /******************************************************************************
2309 * SetConsoleTextAttribute [KERNEL32.@] Sets colors for text
2311 * Sets the foreground and background color attributes of characters
2312 * written to the screen buffer.
2318 BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttr)
2322 TRACE("(%p,%d)\n", hConsoleOutput, wAttr);
2323 SERVER_START_REQ(set_console_output_info)
2325 req->handle = console_handle_unmap(hConsoleOutput);
2327 req->mask = SET_CONSOLE_OUTPUT_INFO_ATTR;
2328 ret = !wine_server_call_err( req );
2335 /******************************************************************************
2336 * SetConsoleScreenBufferSize [KERNEL32.@] Changes size of console
2339 * hConsoleOutput [I] Handle to console screen buffer
2340 * dwSize [I] New size in character rows and cols
2346 BOOL WINAPI SetConsoleScreenBufferSize(HANDLE hConsoleOutput, COORD dwSize)
2350 TRACE("(%p,(%d,%d))\n", hConsoleOutput, dwSize.X, dwSize.Y);
2351 SERVER_START_REQ(set_console_output_info)
2353 req->handle = console_handle_unmap(hConsoleOutput);
2354 req->width = dwSize.X;
2355 req->height = dwSize.Y;
2356 req->mask = SET_CONSOLE_OUTPUT_INFO_SIZE;
2357 ret = !wine_server_call_err( req );
2364 /******************************************************************************
2365 * ScrollConsoleScreenBufferA [KERNEL32.@]
2368 BOOL WINAPI ScrollConsoleScreenBufferA(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2369 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2374 ciw.Attributes = lpFill->Attributes;
2375 MultiByteToWideChar(GetConsoleOutputCP(), 0, &lpFill->Char.AsciiChar, 1, &ciw.Char.UnicodeChar, 1);
2377 return ScrollConsoleScreenBufferW(hConsoleOutput, lpScrollRect, lpClipRect,
2378 dwDestOrigin, &ciw);
2381 /******************************************************************
2382 * CONSOLE_FillLineUniform
2384 * Helper function for ScrollConsoleScreenBufferW
2385 * Fills a part of a line with a constant character info
2387 void CONSOLE_FillLineUniform(HANDLE hConsoleOutput, int i, int j, int len, LPCHAR_INFO lpFill)
2389 SERVER_START_REQ( fill_console_output )
2391 req->handle = console_handle_unmap(hConsoleOutput);
2392 req->mode = CHAR_INFO_MODE_TEXTATTR;
2397 req->data.ch = lpFill->Char.UnicodeChar;
2398 req->data.attr = lpFill->Attributes;
2399 wine_server_call_err( req );
2404 /******************************************************************************
2405 * ScrollConsoleScreenBufferW [KERNEL32.@]
2409 BOOL WINAPI ScrollConsoleScreenBufferW(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2410 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2418 CONSOLE_SCREEN_BUFFER_INFO csbi;
2423 TRACE("(%p,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", hConsoleOutput,
2424 lpScrollRect->Left, lpScrollRect->Top,
2425 lpScrollRect->Right, lpScrollRect->Bottom,
2426 lpClipRect->Left, lpClipRect->Top,
2427 lpClipRect->Right, lpClipRect->Bottom,
2428 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2430 TRACE("(%p,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", hConsoleOutput,
2431 lpScrollRect->Left, lpScrollRect->Top,
2432 lpScrollRect->Right, lpScrollRect->Bottom,
2433 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2435 if (!GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
2438 src.X = lpScrollRect->Left;
2439 src.Y = lpScrollRect->Top;
2441 /* step 1: get dst rect */
2442 dst.Left = dwDestOrigin.X;
2443 dst.Top = dwDestOrigin.Y;
2444 dst.Right = dst.Left + (lpScrollRect->Right - lpScrollRect->Left);
2445 dst.Bottom = dst.Top + (lpScrollRect->Bottom - lpScrollRect->Top);
2447 /* step 2a: compute the final clip rect (optional passed clip and screen buffer limits */
2450 clip.Left = max(0, lpClipRect->Left);
2451 clip.Right = min(csbi.dwSize.X - 1, lpClipRect->Right);
2452 clip.Top = max(0, lpClipRect->Top);
2453 clip.Bottom = min(csbi.dwSize.Y - 1, lpClipRect->Bottom);
2458 clip.Right = csbi.dwSize.X - 1;
2460 clip.Bottom = csbi.dwSize.Y - 1;
2462 if (clip.Left > clip.Right || clip.Top > clip.Bottom) return FALSE;
2464 /* step 2b: clip dst rect */
2465 if (dst.Left < clip.Left ) {src.X += clip.Left - dst.Left; dst.Left = clip.Left;}
2466 if (dst.Top < clip.Top ) {src.Y += clip.Top - dst.Top; dst.Top = clip.Top;}
2467 if (dst.Right > clip.Right ) dst.Right = clip.Right;
2468 if (dst.Bottom > clip.Bottom) dst.Bottom = clip.Bottom;
2470 /* step 3: transfer the bits */
2471 SERVER_START_REQ(move_console_output)
2473 req->handle = console_handle_unmap(hConsoleOutput);
2476 req->x_dst = dst.Left;
2477 req->y_dst = dst.Top;
2478 req->w = dst.Right - dst.Left + 1;
2479 req->h = dst.Bottom - dst.Top + 1;
2480 ret = !wine_server_call_err( req );
2484 if (!ret) return FALSE;
2486 /* step 4: clean out the exposed part */
2488 /* have to write cell [i,j] if it is not in dst rect (because it has already
2489 * been written to by the scroll) and is in clip (we shall not write
2492 for (j = max(lpScrollRect->Top, clip.Top); j <= min(lpScrollRect->Bottom, clip.Bottom); j++)
2494 inside = dst.Top <= j && j <= dst.Bottom;
2496 for (i = max(lpScrollRect->Left, clip.Left); i <= min(lpScrollRect->Right, clip.Right); i++)
2498 if (inside && dst.Left <= i && i <= dst.Right)
2502 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2508 if (start == -1) start = i;
2512 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2518 /******************************************************************
2519 * AttachConsole (KERNEL32.@)
2521 BOOL WINAPI AttachConsole(DWORD dwProcessId)
2523 FIXME("stub %x\n",dwProcessId);
2528 /* ====================================================================
2530 * Console manipulation functions
2532 * ====================================================================*/
2534 /* some missing functions...
2535 * FIXME: those are likely to be defined as undocumented function in kernel32 (or part of them)
2536 * should get the right API and implement them
2537 * GetConsoleCommandHistory[AW] (dword dword dword)
2538 * GetConsoleCommandHistoryLength[AW]
2539 * SetConsoleCommandHistoryMode
2540 * SetConsoleNumberOfCommands[AW]
2542 int CONSOLE_GetHistory(int idx, WCHAR* buf, int buf_len)
2546 SERVER_START_REQ( get_console_input_history )
2550 if (buf && buf_len > 1)
2552 wine_server_set_reply( req, buf, (buf_len - 1) * sizeof(WCHAR) );
2554 if (!wine_server_call_err( req ))
2556 if (buf) buf[wine_server_reply_size(reply) / sizeof(WCHAR)] = 0;
2557 len = reply->total / sizeof(WCHAR) + 1;
2564 /******************************************************************
2565 * CONSOLE_AppendHistory
2569 BOOL CONSOLE_AppendHistory(const WCHAR* ptr)
2571 size_t len = strlenW(ptr);
2574 while (len && (ptr[len - 1] == '\n' || ptr[len - 1] == '\r')) len--;
2575 if (!len) return FALSE;
2577 SERVER_START_REQ( append_console_input_history )
2580 wine_server_add_data( req, ptr, len * sizeof(WCHAR) );
2581 ret = !wine_server_call_err( req );
2587 /******************************************************************
2588 * CONSOLE_GetNumHistoryEntries
2592 unsigned CONSOLE_GetNumHistoryEntries(void)
2595 SERVER_START_REQ(get_console_input_info)
2598 if (!wine_server_call_err( req )) ret = reply->history_index;
2604 /******************************************************************
2605 * CONSOLE_GetEditionMode
2609 BOOL CONSOLE_GetEditionMode(HANDLE hConIn, int* mode)
2611 unsigned ret = FALSE;
2612 SERVER_START_REQ(get_console_input_info)
2614 req->handle = console_handle_unmap(hConIn);
2615 if ((ret = !wine_server_call_err( req )))
2616 *mode = reply->edition_mode;