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/server.h"
48 #include "wine/exception.h"
49 #include "wine/unicode.h"
50 #include "wine/debug.h"
52 #include "console_private.h"
53 #include "kernel_private.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(console);
57 static CRITICAL_SECTION CONSOLE_CritSect;
58 static CRITICAL_SECTION_DEBUG critsect_debug =
60 0, 0, &CONSOLE_CritSect,
61 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
62 0, 0, { (DWORD_PTR)(__FILE__ ": CONSOLE_CritSect") }
64 static CRITICAL_SECTION CONSOLE_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
66 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
67 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
69 /* FIXME: this is not thread safe */
70 static HANDLE console_wait_event;
72 /* map input records to ASCII */
73 static void input_records_WtoA( INPUT_RECORD *buffer, int count )
78 for (i = 0; i < count; i++)
80 if (buffer[i].EventType != KEY_EVENT) continue;
81 WideCharToMultiByte( GetConsoleCP(), 0,
82 &buffer[i].Event.KeyEvent.uChar.UnicodeChar, 1, &ch, 1, NULL, NULL );
83 buffer[i].Event.KeyEvent.uChar.AsciiChar = ch;
87 /* map input records to Unicode */
88 static void input_records_AtoW( INPUT_RECORD *buffer, int count )
93 for (i = 0; i < count; i++)
95 if (buffer[i].EventType != KEY_EVENT) continue;
96 MultiByteToWideChar( GetConsoleCP(), 0,
97 &buffer[i].Event.KeyEvent.uChar.AsciiChar, 1, &ch, 1 );
98 buffer[i].Event.KeyEvent.uChar.UnicodeChar = ch;
102 /* map char infos to ASCII */
103 static void char_info_WtoA( CHAR_INFO *buffer, int count )
109 WideCharToMultiByte( GetConsoleOutputCP(), 0, &buffer->Char.UnicodeChar, 1,
110 &ch, 1, NULL, NULL );
111 buffer->Char.AsciiChar = ch;
116 /* map char infos to Unicode */
117 static void char_info_AtoW( CHAR_INFO *buffer, int count )
123 MultiByteToWideChar( GetConsoleOutputCP(), 0, &buffer->Char.AsciiChar, 1, &ch, 1 );
124 buffer->Char.UnicodeChar = ch;
130 /******************************************************************************
131 * GetConsoleWindow [KERNEL32.@] Get hwnd of the console window.
134 * Success: hwnd of the console window.
137 HWND WINAPI GetConsoleWindow(VOID)
141 SERVER_START_REQ(get_console_input_info)
144 if (!wine_server_call_err(req)) hWnd = wine_server_ptr_handle( reply->win );
152 /******************************************************************************
153 * GetConsoleCP [KERNEL32.@] Returns the OEM code page for the console
158 UINT WINAPI GetConsoleCP(VOID)
161 UINT codepage = GetOEMCP(); /* default value */
163 SERVER_START_REQ(get_console_input_info)
166 ret = !wine_server_call_err(req);
167 if (ret && reply->input_cp)
168 codepage = reply->input_cp;
176 /******************************************************************************
177 * SetConsoleCP [KERNEL32.@]
179 BOOL WINAPI SetConsoleCP(UINT cp)
183 if (!IsValidCodePage(cp))
185 SetLastError(ERROR_INVALID_PARAMETER);
189 SERVER_START_REQ(set_console_input_info)
192 req->mask = SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE;
194 ret = !wine_server_call_err(req);
202 /***********************************************************************
203 * GetConsoleOutputCP (KERNEL32.@)
205 UINT WINAPI GetConsoleOutputCP(VOID)
208 UINT codepage = GetOEMCP(); /* default value */
210 SERVER_START_REQ(get_console_input_info)
213 ret = !wine_server_call_err(req);
214 if (ret && reply->output_cp)
215 codepage = reply->output_cp;
223 /******************************************************************************
224 * SetConsoleOutputCP [KERNEL32.@] Set the output codepage used by the console
227 * cp [I] code page to set
233 BOOL WINAPI SetConsoleOutputCP(UINT cp)
237 if (!IsValidCodePage(cp))
239 SetLastError(ERROR_INVALID_PARAMETER);
243 SERVER_START_REQ(set_console_input_info)
246 req->mask = SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE;
248 ret = !wine_server_call_err(req);
256 /***********************************************************************
259 BOOL WINAPI Beep( DWORD dwFreq, DWORD dwDur )
261 static const char beep = '\a';
262 /* dwFreq and dwDur are ignored by Win95 */
263 if (isatty(2)) write( 2, &beep, 1 );
268 /******************************************************************
269 * OpenConsoleW (KERNEL32.@)
272 * Open a handle to the current process console.
273 * Returns INVALID_HANDLE_VALUE on failure.
275 HANDLE WINAPI OpenConsoleW(LPCWSTR name, DWORD access, BOOL inherit, DWORD creation)
277 HANDLE output = INVALID_HANDLE_VALUE;
280 TRACE("(%s, 0x%08x, %d, %u)\n", debugstr_w(name), access, inherit, creation);
284 if (strcmpiW(coninW, name) == 0)
285 output = (HANDLE) FALSE;
286 else if (strcmpiW(conoutW, name) == 0)
287 output = (HANDLE) TRUE;
290 if (output == INVALID_HANDLE_VALUE)
292 SetLastError(ERROR_INVALID_PARAMETER);
293 return INVALID_HANDLE_VALUE;
295 else if (creation != OPEN_EXISTING)
297 if (!creation || creation == CREATE_NEW || creation == CREATE_ALWAYS)
298 SetLastError(ERROR_SHARING_VIOLATION);
300 SetLastError(ERROR_INVALID_PARAMETER);
301 return INVALID_HANDLE_VALUE;
304 SERVER_START_REQ( open_console )
306 req->from = wine_server_obj_handle( output );
307 req->access = access;
308 req->attributes = inherit ? OBJ_INHERIT : 0;
309 req->share = FILE_SHARE_READ | FILE_SHARE_WRITE;
310 wine_server_call_err( req );
311 ret = wine_server_ptr_handle( reply->handle );
315 ret = console_handle_map(ret);
318 /* likely, we're not attached to wineconsole
319 * let's try to return a handle to the unix-console
321 int fd = open("/dev/tty", output ? O_WRONLY : O_RDONLY);
322 ret = INVALID_HANDLE_VALUE;
325 DWORD access = (output ? GENERIC_WRITE : GENERIC_READ) | SYNCHRONIZE;
326 wine_server_fd_to_handle(fd, access, inherit ? OBJ_INHERIT : 0, &ret);
333 /******************************************************************
334 * VerifyConsoleIoHandle (KERNEL32.@)
338 BOOL WINAPI VerifyConsoleIoHandle(HANDLE handle)
342 if (!is_console_handle(handle)) return FALSE;
343 SERVER_START_REQ(get_console_mode)
345 req->handle = console_handle_unmap(handle);
346 ret = !wine_server_call_err( req );
352 /******************************************************************
353 * DuplicateConsoleHandle (KERNEL32.@)
357 HANDLE WINAPI DuplicateConsoleHandle(HANDLE handle, DWORD access, BOOL inherit,
362 if (!is_console_handle(handle) ||
363 !DuplicateHandle(GetCurrentProcess(), wine_server_ptr_handle(console_handle_unmap(handle)),
364 GetCurrentProcess(), &ret, access, inherit, options))
365 return INVALID_HANDLE_VALUE;
366 return console_handle_map(ret);
369 /******************************************************************
370 * CloseConsoleHandle (KERNEL32.@)
374 BOOL WINAPI CloseConsoleHandle(HANDLE handle)
376 if (!is_console_handle(handle))
378 SetLastError(ERROR_INVALID_PARAMETER);
381 return CloseHandle(wine_server_ptr_handle(console_handle_unmap(handle)));
384 /******************************************************************
385 * GetConsoleInputWaitHandle (KERNEL32.@)
389 HANDLE WINAPI GetConsoleInputWaitHandle(void)
391 if (!console_wait_event)
393 SERVER_START_REQ(get_console_wait_event)
395 if (!wine_server_call_err( req ))
396 console_wait_event = wine_server_ptr_handle( reply->handle );
400 return console_wait_event;
404 /******************************************************************************
405 * WriteConsoleInputA [KERNEL32.@]
407 BOOL WINAPI WriteConsoleInputA( HANDLE handle, const INPUT_RECORD *buffer,
408 DWORD count, LPDWORD written )
413 if (!(recW = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*recW) ))) return FALSE;
414 memcpy( recW, buffer, count*sizeof(*recW) );
415 input_records_AtoW( recW, count );
416 ret = WriteConsoleInputW( handle, recW, count, written );
417 HeapFree( GetProcessHeap(), 0, recW );
422 /******************************************************************************
423 * WriteConsoleInputW [KERNEL32.@]
425 BOOL WINAPI WriteConsoleInputW( HANDLE handle, const INPUT_RECORD *buffer,
426 DWORD count, LPDWORD written )
430 TRACE("(%p,%p,%d,%p)\n", handle, buffer, count, written);
432 if (written) *written = 0;
433 SERVER_START_REQ( write_console_input )
435 req->handle = console_handle_unmap(handle);
436 wine_server_add_data( req, buffer, count * sizeof(INPUT_RECORD) );
437 if ((ret = !wine_server_call_err( req )) && written)
438 *written = reply->written;
446 /***********************************************************************
447 * WriteConsoleOutputA (KERNEL32.@)
449 BOOL WINAPI WriteConsoleOutputA( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
450 COORD size, COORD coord, LPSMALL_RECT region )
454 COORD new_size, new_coord;
457 new_size.X = min( region->Right - region->Left + 1, size.X - coord.X );
458 new_size.Y = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
460 if (new_size.X <= 0 || new_size.Y <= 0)
462 region->Bottom = region->Top + new_size.Y - 1;
463 region->Right = region->Left + new_size.X - 1;
467 /* only copy the useful rectangle */
468 if (!(ciw = HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO) * new_size.X * new_size.Y )))
470 for (y = 0; y < new_size.Y; y++)
472 memcpy( &ciw[y * new_size.X], &lpBuffer[(y + coord.Y) * size.X + coord.X],
473 new_size.X * sizeof(CHAR_INFO) );
474 char_info_AtoW( &ciw[ y * new_size.X ], new_size.X );
476 new_coord.X = new_coord.Y = 0;
477 ret = WriteConsoleOutputW( hConsoleOutput, ciw, new_size, new_coord, region );
478 HeapFree( GetProcessHeap(), 0, ciw );
483 /***********************************************************************
484 * WriteConsoleOutputW (KERNEL32.@)
486 BOOL WINAPI WriteConsoleOutputW( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
487 COORD size, COORD coord, LPSMALL_RECT region )
489 int width, height, y;
492 TRACE("(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n",
493 hConsoleOutput, lpBuffer, size.X, size.Y, coord.X, coord.Y,
494 region->Left, region->Top, region->Right, region->Bottom);
496 width = min( region->Right - region->Left + 1, size.X - coord.X );
497 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
499 if (width > 0 && height > 0)
501 for (y = 0; y < height; y++)
503 SERVER_START_REQ( write_console_output )
505 req->handle = console_handle_unmap(hConsoleOutput);
506 req->x = region->Left;
507 req->y = region->Top + y;
508 req->mode = CHAR_INFO_MODE_TEXTATTR;
510 wine_server_add_data( req, &lpBuffer[(y + coord.Y) * size.X + coord.X],
511 width * sizeof(CHAR_INFO));
512 if ((ret = !wine_server_call_err( req )))
514 width = min( width, reply->width - region->Left );
515 height = min( height, reply->height - region->Top );
522 region->Bottom = region->Top + height - 1;
523 region->Right = region->Left + width - 1;
528 /******************************************************************************
529 * WriteConsoleOutputCharacterA [KERNEL32.@]
531 * See WriteConsoleOutputCharacterW.
533 BOOL WINAPI WriteConsoleOutputCharacterA( HANDLE hConsoleOutput, LPCSTR str, DWORD length,
534 COORD coord, LPDWORD lpNumCharsWritten )
540 TRACE("(%p,%s,%d,%dx%d,%p)\n", hConsoleOutput,
541 debugstr_an(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
543 lenW = MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, NULL, 0 );
545 if (lpNumCharsWritten) *lpNumCharsWritten = 0;
547 if (!(strW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) ))) return FALSE;
548 MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, strW, lenW );
550 ret = WriteConsoleOutputCharacterW( hConsoleOutput, strW, lenW, coord, lpNumCharsWritten );
551 HeapFree( GetProcessHeap(), 0, strW );
556 /******************************************************************************
557 * WriteConsoleOutputAttribute [KERNEL32.@] Sets attributes for some cells in
558 * the console screen buffer
561 * hConsoleOutput [I] Handle to screen buffer
562 * attr [I] Pointer to buffer with write attributes
563 * length [I] Number of cells to write to
564 * coord [I] Coords of first cell
565 * lpNumAttrsWritten [O] Pointer to number of cells written
572 BOOL WINAPI WriteConsoleOutputAttribute( HANDLE hConsoleOutput, CONST WORD *attr, DWORD length,
573 COORD coord, LPDWORD lpNumAttrsWritten )
577 TRACE("(%p,%p,%d,%dx%d,%p)\n", hConsoleOutput,attr,length,coord.X,coord.Y,lpNumAttrsWritten);
579 SERVER_START_REQ( write_console_output )
581 req->handle = console_handle_unmap(hConsoleOutput);
584 req->mode = CHAR_INFO_MODE_ATTR;
586 wine_server_add_data( req, attr, length * sizeof(WORD) );
587 if ((ret = !wine_server_call_err( req )))
589 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
597 /******************************************************************************
598 * FillConsoleOutputCharacterA [KERNEL32.@]
600 * See FillConsoleOutputCharacterW.
602 BOOL WINAPI FillConsoleOutputCharacterA( HANDLE hConsoleOutput, CHAR ch, DWORD length,
603 COORD coord, LPDWORD lpNumCharsWritten )
607 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch, 1, &wch, 1 );
608 return FillConsoleOutputCharacterW(hConsoleOutput, wch, length, coord, lpNumCharsWritten);
612 /******************************************************************************
613 * FillConsoleOutputCharacterW [KERNEL32.@] Writes characters to console
616 * hConsoleOutput [I] Handle to screen buffer
617 * ch [I] Character to write
618 * length [I] Number of cells to write to
619 * coord [I] Coords of first cell
620 * lpNumCharsWritten [O] Pointer to number of cells written
626 BOOL WINAPI FillConsoleOutputCharacterW( HANDLE hConsoleOutput, WCHAR ch, DWORD length,
627 COORD coord, LPDWORD lpNumCharsWritten)
631 TRACE("(%p,%s,%d,(%dx%d),%p)\n",
632 hConsoleOutput, debugstr_wn(&ch, 1), length, coord.X, coord.Y, lpNumCharsWritten);
634 SERVER_START_REQ( fill_console_output )
636 req->handle = console_handle_unmap(hConsoleOutput);
639 req->mode = CHAR_INFO_MODE_TEXT;
643 if ((ret = !wine_server_call_err( req )))
645 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
653 /******************************************************************************
654 * FillConsoleOutputAttribute [KERNEL32.@] Sets attributes for console
657 * hConsoleOutput [I] Handle to screen buffer
658 * attr [I] Color attribute to write
659 * length [I] Number of cells to write to
660 * coord [I] Coords of first cell
661 * lpNumAttrsWritten [O] Pointer to number of cells written
667 BOOL WINAPI FillConsoleOutputAttribute( HANDLE hConsoleOutput, WORD attr, DWORD length,
668 COORD coord, LPDWORD lpNumAttrsWritten )
672 TRACE("(%p,%d,%d,(%dx%d),%p)\n",
673 hConsoleOutput, attr, length, coord.X, coord.Y, lpNumAttrsWritten);
675 SERVER_START_REQ( fill_console_output )
677 req->handle = console_handle_unmap(hConsoleOutput);
680 req->mode = CHAR_INFO_MODE_ATTR;
682 req->data.attr = attr;
684 if ((ret = !wine_server_call_err( req )))
686 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
694 /******************************************************************************
695 * ReadConsoleOutputCharacterA [KERNEL32.@]
698 BOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput, LPSTR lpstr, DWORD count,
699 COORD coord, LPDWORD read_count)
703 LPWSTR wptr = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
705 if (read_count) *read_count = 0;
706 if (!wptr) return FALSE;
708 if ((ret = ReadConsoleOutputCharacterW( hConsoleOutput, wptr, count, coord, &read )))
710 read = WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr, read, lpstr, count, NULL, NULL);
711 if (read_count) *read_count = read;
713 HeapFree( GetProcessHeap(), 0, wptr );
718 /******************************************************************************
719 * ReadConsoleOutputCharacterW [KERNEL32.@]
722 BOOL WINAPI ReadConsoleOutputCharacterW( HANDLE hConsoleOutput, LPWSTR buffer, DWORD count,
723 COORD coord, LPDWORD read_count )
727 TRACE( "(%p,%p,%d,%dx%d,%p)\n", hConsoleOutput, buffer, count, coord.X, coord.Y, read_count );
729 SERVER_START_REQ( read_console_output )
731 req->handle = console_handle_unmap(hConsoleOutput);
734 req->mode = CHAR_INFO_MODE_TEXT;
736 wine_server_set_reply( req, buffer, count * sizeof(WCHAR) );
737 if ((ret = !wine_server_call_err( req )))
739 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WCHAR);
747 /******************************************************************************
748 * ReadConsoleOutputAttribute [KERNEL32.@]
750 BOOL WINAPI ReadConsoleOutputAttribute(HANDLE hConsoleOutput, LPWORD lpAttribute, DWORD length,
751 COORD coord, LPDWORD read_count)
755 TRACE("(%p,%p,%d,%dx%d,%p)\n",
756 hConsoleOutput, lpAttribute, length, coord.X, coord.Y, read_count);
758 SERVER_START_REQ( read_console_output )
760 req->handle = console_handle_unmap(hConsoleOutput);
763 req->mode = CHAR_INFO_MODE_ATTR;
765 wine_server_set_reply( req, lpAttribute, length * sizeof(WORD) );
766 if ((ret = !wine_server_call_err( req )))
768 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WORD);
776 /******************************************************************************
777 * ReadConsoleOutputA [KERNEL32.@]
780 BOOL WINAPI ReadConsoleOutputA( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
781 COORD coord, LPSMALL_RECT region )
786 ret = ReadConsoleOutputW( hConsoleOutput, lpBuffer, size, coord, region );
787 if (ret && region->Right >= region->Left)
789 for (y = 0; y <= region->Bottom - region->Top; y++)
791 char_info_WtoA( &lpBuffer[(coord.Y + y) * size.X + coord.X],
792 region->Right - region->Left + 1 );
799 /******************************************************************************
800 * ReadConsoleOutputW [KERNEL32.@]
802 * NOTE: The NT4 (sp5) kernel crashes on me if size is (0,0). I don't
803 * think we need to be *that* compatible. -- AJ
805 BOOL WINAPI ReadConsoleOutputW( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
806 COORD coord, LPSMALL_RECT region )
808 int width, height, y;
811 width = min( region->Right - region->Left + 1, size.X - coord.X );
812 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
814 if (width > 0 && height > 0)
816 for (y = 0; y < height; y++)
818 SERVER_START_REQ( read_console_output )
820 req->handle = console_handle_unmap(hConsoleOutput);
821 req->x = region->Left;
822 req->y = region->Top + y;
823 req->mode = CHAR_INFO_MODE_TEXTATTR;
825 wine_server_set_reply( req, &lpBuffer[(y+coord.Y) * size.X + coord.X],
826 width * sizeof(CHAR_INFO) );
827 if ((ret = !wine_server_call_err( req )))
829 width = min( width, reply->width - region->Left );
830 height = min( height, reply->height - region->Top );
837 region->Bottom = region->Top + height - 1;
838 region->Right = region->Left + width - 1;
843 /******************************************************************************
844 * ReadConsoleInputA [KERNEL32.@] Reads data from a console
847 * handle [I] Handle to console input buffer
848 * buffer [O] Address of buffer for read data
849 * count [I] Number of records to read
850 * pRead [O] Address of number of records read
856 BOOL WINAPI ReadConsoleInputA( HANDLE handle, PINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
860 if (!ReadConsoleInputW( handle, buffer, count, &read )) return FALSE;
861 input_records_WtoA( buffer, read );
862 if (pRead) *pRead = read;
867 /***********************************************************************
868 * PeekConsoleInputA (KERNEL32.@)
870 * Gets 'count' first events (or less) from input queue.
872 BOOL WINAPI PeekConsoleInputA( HANDLE handle, PINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
876 if (!PeekConsoleInputW( handle, buffer, count, &read )) return FALSE;
877 input_records_WtoA( buffer, read );
878 if (pRead) *pRead = read;
883 /***********************************************************************
884 * PeekConsoleInputW (KERNEL32.@)
886 BOOL WINAPI PeekConsoleInputW( HANDLE handle, PINPUT_RECORD buffer, DWORD count, LPDWORD read )
889 SERVER_START_REQ( read_console_input )
891 req->handle = console_handle_unmap(handle);
893 wine_server_set_reply( req, buffer, count * sizeof(INPUT_RECORD) );
894 if ((ret = !wine_server_call_err( req )))
896 if (read) *read = count ? reply->read : 0;
904 /***********************************************************************
905 * GetNumberOfConsoleInputEvents (KERNEL32.@)
907 BOOL WINAPI GetNumberOfConsoleInputEvents( HANDLE handle, LPDWORD nrofevents )
910 SERVER_START_REQ( read_console_input )
912 req->handle = console_handle_unmap(handle);
914 if ((ret = !wine_server_call_err( req )))
916 if (nrofevents) *nrofevents = reply->read;
924 /******************************************************************************
927 * Helper function for ReadConsole, ReadConsoleInput and FlushConsoleInputBuffer
930 * 0 for error, 1 for no INPUT_RECORD ready, 2 with INPUT_RECORD ready
932 enum read_console_input_return {rci_error = 0, rci_timeout = 1, rci_gotone = 2};
933 static enum read_console_input_return read_console_input(HANDLE handle, PINPUT_RECORD ir, DWORD timeout)
935 enum read_console_input_return ret;
937 if (WaitForSingleObject(GetConsoleInputWaitHandle(), timeout) != WAIT_OBJECT_0)
939 SERVER_START_REQ( read_console_input )
941 req->handle = console_handle_unmap(handle);
943 wine_server_set_reply( req, ir, sizeof(INPUT_RECORD) );
944 if (wine_server_call_err( req ) || !reply->read) ret = rci_error;
945 else ret = rci_gotone;
953 /***********************************************************************
954 * FlushConsoleInputBuffer (KERNEL32.@)
956 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
958 enum read_console_input_return last;
961 while ((last = read_console_input(handle, &ir, 0)) == rci_gotone);
963 return last == rci_timeout;
967 /***********************************************************************
968 * SetConsoleTitleA (KERNEL32.@)
970 BOOL WINAPI SetConsoleTitleA( LPCSTR title )
975 DWORD len = MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, NULL, 0 );
976 if (!(titleW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
977 MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, titleW, len );
978 ret = SetConsoleTitleW(titleW);
979 HeapFree(GetProcessHeap(), 0, titleW);
984 /***********************************************************************
985 * GetConsoleKeyboardLayoutNameA (KERNEL32.@)
987 BOOL WINAPI GetConsoleKeyboardLayoutNameA(LPSTR layoutName)
989 FIXME( "stub %p\n", layoutName);
993 /***********************************************************************
994 * GetConsoleKeyboardLayoutNameW (KERNEL32.@)
996 BOOL WINAPI GetConsoleKeyboardLayoutNameW(LPWSTR layoutName)
998 FIXME( "stub %p\n", layoutName);
1002 static WCHAR input_exe[MAX_PATH + 1];
1004 /***********************************************************************
1005 * GetConsoleInputExeNameW (KERNEL32.@)
1007 BOOL WINAPI GetConsoleInputExeNameW(DWORD buflen, LPWSTR buffer)
1009 TRACE("%u %p\n", buflen, buffer);
1011 RtlEnterCriticalSection(&CONSOLE_CritSect);
1012 if (buflen > strlenW(input_exe)) strcpyW(buffer, input_exe);
1013 else SetLastError(ERROR_BUFFER_OVERFLOW);
1014 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1019 /***********************************************************************
1020 * GetConsoleInputExeNameA (KERNEL32.@)
1022 BOOL WINAPI GetConsoleInputExeNameA(DWORD buflen, LPSTR buffer)
1024 TRACE("%u %p\n", buflen, buffer);
1026 RtlEnterCriticalSection(&CONSOLE_CritSect);
1027 if (WideCharToMultiByte(CP_ACP, 0, input_exe, -1, NULL, 0, NULL, NULL) <= buflen)
1028 WideCharToMultiByte(CP_ACP, 0, input_exe, -1, buffer, buflen, NULL, NULL);
1029 else SetLastError(ERROR_BUFFER_OVERFLOW);
1030 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1035 /***********************************************************************
1036 * GetConsoleTitleA (KERNEL32.@)
1038 * See GetConsoleTitleW.
1040 DWORD WINAPI GetConsoleTitleA(LPSTR title, DWORD size)
1042 WCHAR *ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * size);
1046 ret = GetConsoleTitleW( ptr, size );
1049 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, ret + 1, title, size, NULL, NULL);
1050 ret = strlen(title);
1052 HeapFree(GetProcessHeap(), 0, ptr);
1057 /******************************************************************************
1058 * GetConsoleTitleW [KERNEL32.@] Retrieves title string for console
1061 * title [O] Address of buffer for title
1062 * size [I] Size of buffer
1065 * Success: Length of string copied
1068 DWORD WINAPI GetConsoleTitleW(LPWSTR title, DWORD size)
1072 SERVER_START_REQ( get_console_input_info )
1075 wine_server_set_reply( req, title, (size-1) * sizeof(WCHAR) );
1076 if (!wine_server_call_err( req ))
1078 ret = wine_server_reply_size(reply) / sizeof(WCHAR);
1087 /***********************************************************************
1088 * GetLargestConsoleWindowSize (KERNEL32.@)
1091 * This should return a COORD, but calling convention for returning
1092 * structures is different between Windows and gcc on i386.
1097 #undef GetLargestConsoleWindowSize
1098 DWORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
1106 TRACE("(%p), returning %dx%d (%x)\n", hConsoleOutput, x.c.X, x.c.Y, x.w);
1109 #endif /* defined(__i386__) */
1112 /***********************************************************************
1113 * GetLargestConsoleWindowSize (KERNEL32.@)
1116 * This should return a COORD, but calling convention for returning
1117 * structures is different between Windows and gcc on i386.
1122 COORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
1127 TRACE("(%p), returning %dx%d\n", hConsoleOutput, c.X, c.Y);
1130 #endif /* defined(__i386__) */
1132 static WCHAR* S_EditString /* = NULL */;
1133 static unsigned S_EditStrPos /* = 0 */;
1135 /***********************************************************************
1136 * FreeConsole (KERNEL32.@)
1138 BOOL WINAPI FreeConsole(VOID)
1142 /* invalidate local copy of input event handle */
1143 console_wait_event = 0;
1145 SERVER_START_REQ(free_console)
1147 ret = !wine_server_call_err( req );
1153 /******************************************************************
1154 * start_console_renderer
1156 * helper for AllocConsole
1157 * starts the renderer process
1159 static BOOL start_console_renderer_helper(const char* appname, STARTUPINFOA* si,
1164 PROCESS_INFORMATION pi;
1166 /* FIXME: use dynamic allocation for most of the buffers below */
1167 ret = snprintf(buffer, sizeof(buffer), "%s --use-event=%ld", appname, (DWORD_PTR)hEvent);
1168 if ((ret > -1) && (ret < sizeof(buffer)) &&
1169 CreateProcessA(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS,
1170 NULL, NULL, si, &pi))
1176 wh[1] = pi.hProcess;
1177 ret = WaitForMultipleObjects(2, wh, FALSE, INFINITE);
1179 CloseHandle(pi.hThread);
1180 CloseHandle(pi.hProcess);
1182 if (ret != WAIT_OBJECT_0) return FALSE;
1184 TRACE("Started wineconsole pid=%08x tid=%08x\n",
1185 pi.dwProcessId, pi.dwThreadId);
1192 static BOOL start_console_renderer(STARTUPINFOA* si)
1196 OBJECT_ATTRIBUTES attr;
1199 attr.Length = sizeof(attr);
1200 attr.RootDirectory = 0;
1201 attr.Attributes = OBJ_INHERIT;
1202 attr.ObjectName = NULL;
1203 attr.SecurityDescriptor = NULL;
1204 attr.SecurityQualityOfService = NULL;
1206 NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, &attr, NotificationEvent, FALSE);
1207 if (!hEvent) return FALSE;
1209 /* first try environment variable */
1210 if ((p = getenv("WINECONSOLE")) != NULL)
1212 ret = start_console_renderer_helper(p, si, hEvent);
1214 ERR("Couldn't launch Wine console from WINECONSOLE env var (%s)... "
1215 "trying default access\n", p);
1218 /* then try the regular PATH */
1220 ret = start_console_renderer_helper("wineconsole", si, hEvent);
1222 CloseHandle(hEvent);
1226 /***********************************************************************
1227 * AllocConsole (KERNEL32.@)
1229 * creates an xterm with a pty to our program
1231 BOOL WINAPI AllocConsole(void)
1233 HANDLE handle_in = INVALID_HANDLE_VALUE;
1234 HANDLE handle_out = INVALID_HANDLE_VALUE;
1235 HANDLE handle_err = INVALID_HANDLE_VALUE;
1236 STARTUPINFOA siCurrent;
1237 STARTUPINFOA siConsole;
1242 handle_in = OpenConsoleW( coninW, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
1243 FALSE, OPEN_EXISTING );
1245 if (VerifyConsoleIoHandle(handle_in))
1247 /* we already have a console opened on this process, don't create a new one */
1248 CloseHandle(handle_in);
1251 /* happens when we're running on a Unix console */
1252 if (handle_in != INVALID_HANDLE_VALUE) CloseHandle(handle_in);
1254 /* invalidate local copy of input event handle */
1255 console_wait_event = 0;
1257 GetStartupInfoA(&siCurrent);
1259 memset(&siConsole, 0, sizeof(siConsole));
1260 siConsole.cb = sizeof(siConsole);
1261 /* setup a view arguments for wineconsole (it'll use them as default values) */
1262 if (siCurrent.dwFlags & STARTF_USECOUNTCHARS)
1264 siConsole.dwFlags |= STARTF_USECOUNTCHARS;
1265 siConsole.dwXCountChars = siCurrent.dwXCountChars;
1266 siConsole.dwYCountChars = siCurrent.dwYCountChars;
1268 if (siCurrent.dwFlags & STARTF_USEFILLATTRIBUTE)
1270 siConsole.dwFlags |= STARTF_USEFILLATTRIBUTE;
1271 siConsole.dwFillAttribute = siCurrent.dwFillAttribute;
1273 if (siCurrent.dwFlags & STARTF_USESHOWWINDOW)
1275 siConsole.dwFlags |= STARTF_USESHOWWINDOW;
1276 siConsole.wShowWindow = siCurrent.wShowWindow;
1278 /* FIXME (should pass the unicode form) */
1279 if (siCurrent.lpTitle)
1280 siConsole.lpTitle = siCurrent.lpTitle;
1281 else if (GetModuleFileNameA(0, buffer, sizeof(buffer)))
1283 buffer[sizeof(buffer) - 1] = '\0';
1284 siConsole.lpTitle = buffer;
1287 if (!start_console_renderer(&siConsole))
1290 if( !(siCurrent.dwFlags & STARTF_USESTDHANDLES) ) {
1291 /* all std I/O handles are inheritable by default */
1292 handle_in = OpenConsoleW( coninW, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
1293 TRUE, OPEN_EXISTING );
1294 if (handle_in == INVALID_HANDLE_VALUE) goto the_end;
1296 handle_out = OpenConsoleW( conoutW, GENERIC_READ|GENERIC_WRITE,
1297 TRUE, OPEN_EXISTING );
1298 if (handle_out == INVALID_HANDLE_VALUE) goto the_end;
1300 if (!DuplicateHandle(GetCurrentProcess(), handle_out, GetCurrentProcess(),
1301 &handle_err, 0, TRUE, DUPLICATE_SAME_ACCESS))
1304 /* STARTF_USESTDHANDLES flag: use handles from StartupInfo */
1305 handle_in = siCurrent.hStdInput;
1306 handle_out = siCurrent.hStdOutput;
1307 handle_err = siCurrent.hStdError;
1310 /* NT resets the STD_*_HANDLEs on console alloc */
1311 SetStdHandle(STD_INPUT_HANDLE, handle_in);
1312 SetStdHandle(STD_OUTPUT_HANDLE, handle_out);
1313 SetStdHandle(STD_ERROR_HANDLE, handle_err);
1315 SetLastError(ERROR_SUCCESS);
1320 ERR("Can't allocate console\n");
1321 if (handle_in != INVALID_HANDLE_VALUE) CloseHandle(handle_in);
1322 if (handle_out != INVALID_HANDLE_VALUE) CloseHandle(handle_out);
1323 if (handle_err != INVALID_HANDLE_VALUE) CloseHandle(handle_err);
1329 /***********************************************************************
1330 * ReadConsoleA (KERNEL32.@)
1332 BOOL WINAPI ReadConsoleA(HANDLE hConsoleInput, LPVOID lpBuffer, DWORD nNumberOfCharsToRead,
1333 LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1335 LPWSTR ptr = HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead * sizeof(WCHAR));
1339 if ((ret = ReadConsoleW(hConsoleInput, ptr, nNumberOfCharsToRead, &ncr, NULL)))
1340 ncr = WideCharToMultiByte(GetConsoleCP(), 0, ptr, ncr, lpBuffer, nNumberOfCharsToRead, NULL, NULL);
1342 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = ncr;
1343 HeapFree(GetProcessHeap(), 0, ptr);
1348 /***********************************************************************
1349 * ReadConsoleW (KERNEL32.@)
1351 BOOL WINAPI ReadConsoleW(HANDLE hConsoleInput, LPVOID lpBuffer,
1352 DWORD nNumberOfCharsToRead, LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1355 LPWSTR xbuf = lpBuffer;
1358 TRACE("(%p,%p,%d,%p,%p)\n",
1359 hConsoleInput, lpBuffer, nNumberOfCharsToRead, lpNumberOfCharsRead, lpReserved);
1361 if (!GetConsoleMode(hConsoleInput, &mode))
1364 if (mode & ENABLE_LINE_INPUT)
1366 if (!S_EditString || S_EditString[S_EditStrPos] == 0)
1368 HeapFree(GetProcessHeap(), 0, S_EditString);
1369 if (!(S_EditString = CONSOLE_Readline(hConsoleInput)))
1373 charsread = lstrlenW(&S_EditString[S_EditStrPos]);
1374 if (charsread > nNumberOfCharsToRead) charsread = nNumberOfCharsToRead;
1375 memcpy(xbuf, &S_EditString[S_EditStrPos], charsread * sizeof(WCHAR));
1376 S_EditStrPos += charsread;
1381 DWORD timeout = INFINITE;
1383 /* FIXME: should we read at least 1 char? The SDK does not say */
1384 /* wait for at least one available input record (it doesn't mean we'll have
1385 * chars stored in xbuf...)
1387 * Although SDK doc keeps silence about 1 char, SDK examples assume
1388 * that we should wait for at least one character (not key). --KS
1393 if (read_console_input(hConsoleInput, &ir, timeout) != rci_gotone) break;
1394 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown &&
1395 ir.Event.KeyEvent.uChar.UnicodeChar)
1397 xbuf[charsread++] = ir.Event.KeyEvent.uChar.UnicodeChar;
1400 } while (charsread < nNumberOfCharsToRead);
1401 /* nothing has been read */
1402 if (timeout == INFINITE) return FALSE;
1405 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = charsread;
1411 /***********************************************************************
1412 * ReadConsoleInputW (KERNEL32.@)
1414 BOOL WINAPI ReadConsoleInputW(HANDLE hConsoleInput, PINPUT_RECORD lpBuffer,
1415 DWORD nLength, LPDWORD lpNumberOfEventsRead)
1418 DWORD timeout = INFINITE;
1422 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = 0;
1426 /* loop until we get at least one event */
1427 while (read_console_input(hConsoleInput, &lpBuffer[idx], timeout) == rci_gotone &&
1431 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = idx;
1436 /******************************************************************************
1437 * WriteConsoleOutputCharacterW [KERNEL32.@]
1439 * Copy character to consecutive cells in the console screen buffer.
1442 * hConsoleOutput [I] Handle to screen buffer
1443 * str [I] Pointer to buffer with chars to write
1444 * length [I] Number of cells to write to
1445 * coord [I] Coords of first cell
1446 * lpNumCharsWritten [O] Pointer to number of cells written
1453 BOOL WINAPI WriteConsoleOutputCharacterW( HANDLE hConsoleOutput, LPCWSTR str, DWORD length,
1454 COORD coord, LPDWORD lpNumCharsWritten )
1458 TRACE("(%p,%s,%d,%dx%d,%p)\n", hConsoleOutput,
1459 debugstr_wn(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
1461 SERVER_START_REQ( write_console_output )
1463 req->handle = console_handle_unmap(hConsoleOutput);
1466 req->mode = CHAR_INFO_MODE_TEXT;
1468 wine_server_add_data( req, str, length * sizeof(WCHAR) );
1469 if ((ret = !wine_server_call_err( req )))
1471 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
1479 /******************************************************************************
1480 * SetConsoleTitleW [KERNEL32.@] Sets title bar string for console
1483 * title [I] Address of new title
1489 BOOL WINAPI SetConsoleTitleW(LPCWSTR title)
1493 TRACE("(%s)\n", debugstr_w(title));
1494 SERVER_START_REQ( set_console_input_info )
1497 req->mask = SET_CONSOLE_INPUT_INFO_TITLE;
1498 wine_server_add_data( req, title, strlenW(title) * sizeof(WCHAR) );
1499 ret = !wine_server_call_err( req );
1506 /***********************************************************************
1507 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
1509 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1511 FIXME("(%p): stub\n", nrofbuttons);
1516 /******************************************************************************
1517 * SetConsoleInputExeNameW [KERNEL32.@]
1519 BOOL WINAPI SetConsoleInputExeNameW(LPCWSTR name)
1521 TRACE("(%s)\n", debugstr_w(name));
1523 if (!name || !name[0])
1525 SetLastError(ERROR_INVALID_PARAMETER);
1529 RtlEnterCriticalSection(&CONSOLE_CritSect);
1530 if (strlenW(name) < sizeof(input_exe)/sizeof(WCHAR)) strcpyW(input_exe, name);
1531 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1536 /******************************************************************************
1537 * SetConsoleInputExeNameA [KERNEL32.@]
1539 BOOL WINAPI SetConsoleInputExeNameA(LPCSTR name)
1545 if (!name || !name[0])
1547 SetLastError(ERROR_INVALID_PARAMETER);
1551 len = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
1552 if (!(nameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
1554 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, len);
1555 ret = SetConsoleInputExeNameW(nameW);
1556 HeapFree(GetProcessHeap(), 0, nameW);
1561 /******************************************************************
1562 * CONSOLE_DefaultHandler
1564 * Final control event handler
1566 static BOOL WINAPI CONSOLE_DefaultHandler(DWORD dwCtrlType)
1568 FIXME("Terminating process %x on event %x\n", GetCurrentProcessId(), dwCtrlType);
1570 /* should never go here */
1574 /******************************************************************************
1575 * SetConsoleCtrlHandler [KERNEL32.@] Adds function to calling process list
1578 * func [I] Address of handler function
1579 * add [I] Handler to add or remove
1586 struct ConsoleHandler
1588 PHANDLER_ROUTINE handler;
1589 struct ConsoleHandler* next;
1592 static struct ConsoleHandler CONSOLE_DefaultConsoleHandler = {CONSOLE_DefaultHandler, NULL};
1593 static struct ConsoleHandler* CONSOLE_Handlers = &CONSOLE_DefaultConsoleHandler;
1595 /*****************************************************************************/
1597 /******************************************************************
1598 * SetConsoleCtrlHandler (KERNEL32.@)
1600 BOOL WINAPI SetConsoleCtrlHandler(PHANDLER_ROUTINE func, BOOL add)
1604 TRACE("(%p,%i)\n", func, add);
1608 RtlEnterCriticalSection(&CONSOLE_CritSect);
1610 NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags |= 1;
1612 NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags &= ~1;
1613 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1617 struct ConsoleHandler* ch = HeapAlloc(GetProcessHeap(), 0, sizeof(struct ConsoleHandler));
1619 if (!ch) return FALSE;
1621 RtlEnterCriticalSection(&CONSOLE_CritSect);
1622 ch->next = CONSOLE_Handlers;
1623 CONSOLE_Handlers = ch;
1624 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1628 struct ConsoleHandler** ch;
1629 RtlEnterCriticalSection(&CONSOLE_CritSect);
1630 for (ch = &CONSOLE_Handlers; *ch; ch = &(*ch)->next)
1632 if ((*ch)->handler == func) break;
1636 struct ConsoleHandler* rch = *ch;
1639 if (rch == &CONSOLE_DefaultConsoleHandler)
1641 ERR("Who's trying to remove default handler???\n");
1642 SetLastError(ERROR_INVALID_PARAMETER);
1648 HeapFree(GetProcessHeap(), 0, rch);
1653 WARN("Attempt to remove non-installed CtrlHandler %p\n", func);
1654 SetLastError(ERROR_INVALID_PARAMETER);
1657 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1662 static LONG WINAPI CONSOLE_CtrlEventHandler(EXCEPTION_POINTERS *eptr)
1664 TRACE("(%x)\n", eptr->ExceptionRecord->ExceptionCode);
1665 return EXCEPTION_EXECUTE_HANDLER;
1668 /******************************************************************
1669 * CONSOLE_SendEventThread
1671 * Internal helper to pass an event to the list on installed handlers
1673 static DWORD WINAPI CONSOLE_SendEventThread(void* pmt)
1675 DWORD_PTR event = (DWORD_PTR)pmt;
1676 struct ConsoleHandler* ch;
1678 if (event == CTRL_C_EVENT)
1680 BOOL caught_by_dbg = TRUE;
1681 /* First, try to pass the ctrl-C event to the debugger (if any)
1682 * If it continues, there's nothing more to do
1683 * Otherwise, we need to send the ctrl-C event to the handlers
1687 RaiseException( DBG_CONTROL_C, 0, 0, NULL );
1689 __EXCEPT(CONSOLE_CtrlEventHandler)
1691 caught_by_dbg = FALSE;
1694 if (caught_by_dbg) return 0;
1695 /* the debugger didn't continue... so, pass to ctrl handlers */
1697 RtlEnterCriticalSection(&CONSOLE_CritSect);
1698 for (ch = CONSOLE_Handlers; ch; ch = ch->next)
1700 if (ch->handler(event)) break;
1702 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1706 /******************************************************************
1707 * CONSOLE_HandleCtrlC
1709 * Check whether the shall manipulate CtrlC events
1711 int CONSOLE_HandleCtrlC(unsigned sig)
1713 /* FIXME: better test whether a console is attached to this process ??? */
1714 extern unsigned CONSOLE_GetNumHistoryEntries(void);
1715 if (CONSOLE_GetNumHistoryEntries() == (unsigned)-1) return 0;
1717 /* check if we have to ignore ctrl-C events */
1718 if (!(NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags & 1))
1720 /* Create a separate thread to signal all the events.
1721 * This is needed because:
1722 * - this function can be called in an Unix signal handler (hence on an
1723 * different stack than the thread that's running). This breaks the
1724 * Win32 exception mechanisms (where the thread's stack is checked).
1725 * - since the current thread, while processing the signal, can hold the
1726 * console critical section, we need another execution environment where
1727 * we can wait on this critical section
1729 CreateThread(NULL, 0, CONSOLE_SendEventThread, (void*)CTRL_C_EVENT, 0, NULL);
1734 /******************************************************************************
1735 * GenerateConsoleCtrlEvent [KERNEL32.@] Simulate a CTRL-C or CTRL-BREAK
1738 * dwCtrlEvent [I] Type of event
1739 * dwProcessGroupID [I] Process group ID to send event to
1743 * Failure: False (and *should* [but doesn't] set LastError)
1745 BOOL WINAPI GenerateConsoleCtrlEvent(DWORD dwCtrlEvent,
1746 DWORD dwProcessGroupID)
1750 TRACE("(%d, %d)\n", dwCtrlEvent, dwProcessGroupID);
1752 if (dwCtrlEvent != CTRL_C_EVENT && dwCtrlEvent != CTRL_BREAK_EVENT)
1754 ERR("Invalid event %d for PGID %d\n", dwCtrlEvent, dwProcessGroupID);
1758 SERVER_START_REQ( send_console_signal )
1760 req->signal = dwCtrlEvent;
1761 req->group_id = dwProcessGroupID;
1762 ret = !wine_server_call_err( req );
1766 /* FIXME: Shall this function be synchronous, i.e., only return when all events
1767 * have been handled by all processes in the given group?
1768 * As of today, we don't wait...
1774 /******************************************************************************
1775 * CreateConsoleScreenBuffer [KERNEL32.@] Creates a console screen buffer
1778 * dwDesiredAccess [I] Access flag
1779 * dwShareMode [I] Buffer share mode
1780 * sa [I] Security attributes
1781 * dwFlags [I] Type of buffer to create
1782 * lpScreenBufferData [I] Reserved
1785 * Should call SetLastError
1788 * Success: Handle to new console screen buffer
1789 * Failure: INVALID_HANDLE_VALUE
1791 HANDLE WINAPI CreateConsoleScreenBuffer(DWORD dwDesiredAccess, DWORD dwShareMode,
1792 LPSECURITY_ATTRIBUTES sa, DWORD dwFlags,
1793 LPVOID lpScreenBufferData)
1795 HANDLE ret = INVALID_HANDLE_VALUE;
1797 TRACE("(%d,%d,%p,%d,%p)\n",
1798 dwDesiredAccess, dwShareMode, sa, dwFlags, lpScreenBufferData);
1800 if (dwFlags != CONSOLE_TEXTMODE_BUFFER || lpScreenBufferData != NULL)
1802 SetLastError(ERROR_INVALID_PARAMETER);
1803 return INVALID_HANDLE_VALUE;
1806 SERVER_START_REQ(create_console_output)
1809 req->access = dwDesiredAccess;
1810 req->attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
1811 req->share = dwShareMode;
1812 if (!wine_server_call_err( req ))
1813 ret = console_handle_map( wine_server_ptr_handle( reply->handle_out ));
1821 /***********************************************************************
1822 * GetConsoleScreenBufferInfo (KERNEL32.@)
1824 BOOL WINAPI GetConsoleScreenBufferInfo(HANDLE hConsoleOutput, LPCONSOLE_SCREEN_BUFFER_INFO csbi)
1828 SERVER_START_REQ(get_console_output_info)
1830 req->handle = console_handle_unmap(hConsoleOutput);
1831 if ((ret = !wine_server_call_err( req )))
1833 csbi->dwSize.X = reply->width;
1834 csbi->dwSize.Y = reply->height;
1835 csbi->dwCursorPosition.X = reply->cursor_x;
1836 csbi->dwCursorPosition.Y = reply->cursor_y;
1837 csbi->wAttributes = reply->attr;
1838 csbi->srWindow.Left = reply->win_left;
1839 csbi->srWindow.Right = reply->win_right;
1840 csbi->srWindow.Top = reply->win_top;
1841 csbi->srWindow.Bottom = reply->win_bottom;
1842 csbi->dwMaximumWindowSize.X = reply->max_width;
1843 csbi->dwMaximumWindowSize.Y = reply->max_height;
1848 TRACE("(%p,(%d,%d) (%d,%d) %d (%d,%d-%d,%d) (%d,%d)\n",
1849 hConsoleOutput, csbi->dwSize.X, csbi->dwSize.Y,
1850 csbi->dwCursorPosition.X, csbi->dwCursorPosition.Y,
1852 csbi->srWindow.Left, csbi->srWindow.Top, csbi->srWindow.Right, csbi->srWindow.Bottom,
1853 csbi->dwMaximumWindowSize.X, csbi->dwMaximumWindowSize.Y);
1859 /******************************************************************************
1860 * SetConsoleActiveScreenBuffer [KERNEL32.@] Sets buffer to current console
1866 BOOL WINAPI SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput)
1870 TRACE("(%p)\n", hConsoleOutput);
1872 SERVER_START_REQ( set_console_input_info )
1875 req->mask = SET_CONSOLE_INPUT_INFO_ACTIVE_SB;
1876 req->active_sb = wine_server_obj_handle( hConsoleOutput );
1877 ret = !wine_server_call_err( req );
1884 /***********************************************************************
1885 * GetConsoleMode (KERNEL32.@)
1887 BOOL WINAPI GetConsoleMode(HANDLE hcon, LPDWORD mode)
1891 SERVER_START_REQ(get_console_mode)
1893 req->handle = console_handle_unmap(hcon);
1894 ret = !wine_server_call_err( req );
1895 if (ret && mode) *mode = reply->mode;
1902 /******************************************************************************
1903 * SetConsoleMode [KERNEL32.@] Sets input mode of console's input buffer
1906 * hcon [I] Handle to console input or screen buffer
1907 * mode [I] Input or output mode to set
1914 * ENABLE_PROCESSED_INPUT 0x01
1915 * ENABLE_LINE_INPUT 0x02
1916 * ENABLE_ECHO_INPUT 0x04
1917 * ENABLE_WINDOW_INPUT 0x08
1918 * ENABLE_MOUSE_INPUT 0x10
1920 BOOL WINAPI SetConsoleMode(HANDLE hcon, DWORD mode)
1924 SERVER_START_REQ(set_console_mode)
1926 req->handle = console_handle_unmap(hcon);
1928 ret = !wine_server_call_err( req );
1931 /* FIXME: when resetting a console input to editline mode, I think we should
1932 * empty the S_EditString buffer
1935 TRACE("(%p,%x) retval == %d\n", hcon, mode, ret);
1941 /******************************************************************
1942 * CONSOLE_WriteChars
1944 * WriteConsoleOutput helper: hides server call semantics
1945 * writes a string at a given pos with standard attribute
1947 static int CONSOLE_WriteChars(HANDLE hCon, LPCWSTR lpBuffer, int nc, COORD* pos)
1953 SERVER_START_REQ( write_console_output )
1955 req->handle = console_handle_unmap(hCon);
1958 req->mode = CHAR_INFO_MODE_TEXTSTDATTR;
1960 wine_server_add_data( req, lpBuffer, nc * sizeof(WCHAR) );
1961 if (!wine_server_call_err( req )) written = reply->written;
1965 if (written > 0) pos->X += written;
1969 /******************************************************************
1972 * WriteConsoleOutput helper: handles passing to next line (+scrolling if necessary)
1975 static int next_line(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi)
1981 csbi->dwCursorPosition.X = 0;
1982 csbi->dwCursorPosition.Y++;
1984 if (csbi->dwCursorPosition.Y < csbi->dwSize.Y) return 1;
1987 src.Bottom = csbi->dwSize.Y - 1;
1989 src.Right = csbi->dwSize.X - 1;
1994 ci.Attributes = csbi->wAttributes;
1995 ci.Char.UnicodeChar = ' ';
1997 csbi->dwCursorPosition.Y--;
1998 if (!ScrollConsoleScreenBufferW(hCon, &src, NULL, dst, &ci))
2003 /******************************************************************
2006 * WriteConsoleOutput helper: writes a block of non special characters
2007 * Block can spread on several lines, and wrapping, if needed, is
2011 static int write_block(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi,
2012 DWORD mode, LPCWSTR ptr, int len)
2014 int blk; /* number of chars to write on current line */
2015 int done; /* number of chars already written */
2017 if (len <= 0) return 1;
2019 if (mode & ENABLE_WRAP_AT_EOL_OUTPUT) /* writes remaining on next line */
2021 for (done = 0; done < len; done += blk)
2023 blk = min(len - done, csbi->dwSize.X - csbi->dwCursorPosition.X);
2025 if (CONSOLE_WriteChars(hCon, ptr + done, blk, &csbi->dwCursorPosition) != blk)
2027 if (csbi->dwCursorPosition.X == csbi->dwSize.X && !next_line(hCon, csbi))
2033 int pos = csbi->dwCursorPosition.X;
2034 /* FIXME: we could reduce the number of loops
2035 * but, in most cases we wouldn't gain lots of time (it would only
2036 * happen if we're asked to overwrite more than twice the part of the line,
2039 for (blk = done = 0; done < len; done += blk)
2041 blk = min(len - done, csbi->dwSize.X - csbi->dwCursorPosition.X);
2043 csbi->dwCursorPosition.X = pos;
2044 if (CONSOLE_WriteChars(hCon, ptr + done, blk, &csbi->dwCursorPosition) != blk)
2052 /***********************************************************************
2053 * WriteConsoleW (KERNEL32.@)
2055 BOOL WINAPI WriteConsoleW(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
2056 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
2060 const WCHAR* psz = lpBuffer;
2061 CONSOLE_SCREEN_BUFFER_INFO csbi;
2064 TRACE("%p %s %d %p %p\n",
2065 hConsoleOutput, debugstr_wn(lpBuffer, nNumberOfCharsToWrite),
2066 nNumberOfCharsToWrite, lpNumberOfCharsWritten, lpReserved);
2068 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
2070 if (!GetConsoleMode(hConsoleOutput, &mode) ||
2071 !GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
2074 if (!nNumberOfCharsToWrite) return TRUE;
2076 if (mode & ENABLE_PROCESSED_OUTPUT)
2080 for (i = 0; i < nNumberOfCharsToWrite; i++)
2084 case '\b': case '\t': case '\n': case '\a': case '\r':
2085 /* don't handle here the i-th char... done below */
2086 if ((k = i - first) > 0)
2088 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
2098 if (csbi.dwCursorPosition.X > 0) csbi.dwCursorPosition.X--;
2102 WCHAR tmp[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
2104 if (!write_block(hConsoleOutput, &csbi, mode, tmp,
2105 ((csbi.dwCursorPosition.X + 8) & ~7) - csbi.dwCursorPosition.X))
2110 next_line(hConsoleOutput, &csbi);
2116 csbi.dwCursorPosition.X = 0;
2124 /* write the remaining block (if any) if processed output is enabled, or the
2125 * entire buffer otherwise
2127 if ((k = nNumberOfCharsToWrite - first) > 0)
2129 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
2135 SetConsoleCursorPosition(hConsoleOutput, csbi.dwCursorPosition);
2136 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = nw;
2141 /***********************************************************************
2142 * WriteConsoleA (KERNEL32.@)
2144 BOOL WINAPI WriteConsoleA(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
2145 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
2151 n = MultiByteToWideChar(GetConsoleOutputCP(), 0, lpBuffer, nNumberOfCharsToWrite, NULL, 0);
2153 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
2154 xstring = HeapAlloc(GetProcessHeap(), 0, n * sizeof(WCHAR));
2155 if (!xstring) return 0;
2157 MultiByteToWideChar(GetConsoleOutputCP(), 0, lpBuffer, nNumberOfCharsToWrite, xstring, n);
2159 ret = WriteConsoleW(hConsoleOutput, xstring, n, lpNumberOfCharsWritten, 0);
2161 HeapFree(GetProcessHeap(), 0, xstring);
2166 /******************************************************************************
2167 * SetConsoleCursorPosition [KERNEL32.@]
2168 * Sets the cursor position in console
2171 * hConsoleOutput [I] Handle of console screen buffer
2172 * dwCursorPosition [I] New cursor position coordinates
2178 BOOL WINAPI SetConsoleCursorPosition(HANDLE hcon, COORD pos)
2181 CONSOLE_SCREEN_BUFFER_INFO csbi;
2185 TRACE("%p %d %d\n", hcon, pos.X, pos.Y);
2187 SERVER_START_REQ(set_console_output_info)
2189 req->handle = console_handle_unmap(hcon);
2190 req->cursor_x = pos.X;
2191 req->cursor_y = pos.Y;
2192 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_POS;
2193 ret = !wine_server_call_err( req );
2197 if (!ret || !GetConsoleScreenBufferInfo(hcon, &csbi))
2200 /* if cursor is no longer visible, scroll the visible window... */
2201 w = csbi.srWindow.Right - csbi.srWindow.Left + 1;
2202 h = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
2203 if (pos.X < csbi.srWindow.Left)
2205 csbi.srWindow.Left = min(pos.X, csbi.dwSize.X - w);
2208 else if (pos.X > csbi.srWindow.Right)
2210 csbi.srWindow.Left = max(pos.X, w) - w + 1;
2213 csbi.srWindow.Right = csbi.srWindow.Left + w - 1;
2215 if (pos.Y < csbi.srWindow.Top)
2217 csbi.srWindow.Top = min(pos.Y, csbi.dwSize.Y - h);
2220 else if (pos.Y > csbi.srWindow.Bottom)
2222 csbi.srWindow.Top = max(pos.Y, h) - h + 1;
2225 csbi.srWindow.Bottom = csbi.srWindow.Top + h - 1;
2227 ret = (do_move) ? SetConsoleWindowInfo(hcon, TRUE, &csbi.srWindow) : TRUE;
2232 /******************************************************************************
2233 * GetConsoleCursorInfo [KERNEL32.@] Gets size and visibility of console
2236 * hcon [I] Handle to console screen buffer
2237 * cinfo [O] Address of cursor information
2243 BOOL WINAPI GetConsoleCursorInfo(HANDLE hCon, LPCONSOLE_CURSOR_INFO cinfo)
2247 SERVER_START_REQ(get_console_output_info)
2249 req->handle = console_handle_unmap(hCon);
2250 ret = !wine_server_call_err( req );
2253 cinfo->dwSize = reply->cursor_size;
2254 cinfo->bVisible = reply->cursor_visible;
2259 if (!ret) return FALSE;
2263 SetLastError(ERROR_INVALID_ACCESS);
2266 else TRACE("(%p) returning (%d,%d)\n", hCon, cinfo->dwSize, cinfo->bVisible);
2272 /******************************************************************************
2273 * SetConsoleCursorInfo [KERNEL32.@] Sets size and visibility of cursor
2276 * hcon [I] Handle to console screen buffer
2277 * cinfo [I] Address of cursor information
2282 BOOL WINAPI SetConsoleCursorInfo(HANDLE hCon, LPCONSOLE_CURSOR_INFO cinfo)
2286 TRACE("(%p,%d,%d)\n", hCon, cinfo->dwSize, cinfo->bVisible);
2287 SERVER_START_REQ(set_console_output_info)
2289 req->handle = console_handle_unmap(hCon);
2290 req->cursor_size = cinfo->dwSize;
2291 req->cursor_visible = cinfo->bVisible;
2292 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM;
2293 ret = !wine_server_call_err( req );
2300 /******************************************************************************
2301 * SetConsoleWindowInfo [KERNEL32.@] Sets size and position of console
2304 * hcon [I] Handle to console screen buffer
2305 * bAbsolute [I] Coordinate type flag
2306 * window [I] Address of new window rectangle
2311 BOOL WINAPI SetConsoleWindowInfo(HANDLE hCon, BOOL bAbsolute, LPSMALL_RECT window)
2313 SMALL_RECT p = *window;
2316 TRACE("(%p,%d,(%d,%d-%d,%d))\n", hCon, bAbsolute, p.Left, p.Top, p.Right, p.Bottom);
2320 CONSOLE_SCREEN_BUFFER_INFO csbi;
2322 if (!GetConsoleScreenBufferInfo(hCon, &csbi))
2324 p.Left += csbi.srWindow.Left;
2325 p.Top += csbi.srWindow.Top;
2326 p.Right += csbi.srWindow.Right;
2327 p.Bottom += csbi.srWindow.Bottom;
2329 SERVER_START_REQ(set_console_output_info)
2331 req->handle = console_handle_unmap(hCon);
2332 req->win_left = p.Left;
2333 req->win_top = p.Top;
2334 req->win_right = p.Right;
2335 req->win_bottom = p.Bottom;
2336 req->mask = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
2337 ret = !wine_server_call_err( req );
2345 /******************************************************************************
2346 * SetConsoleTextAttribute [KERNEL32.@] Sets colors for text
2348 * Sets the foreground and background color attributes of characters
2349 * written to the screen buffer.
2355 BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttr)
2359 TRACE("(%p,%d)\n", hConsoleOutput, wAttr);
2360 SERVER_START_REQ(set_console_output_info)
2362 req->handle = console_handle_unmap(hConsoleOutput);
2364 req->mask = SET_CONSOLE_OUTPUT_INFO_ATTR;
2365 ret = !wine_server_call_err( req );
2372 /******************************************************************************
2373 * SetConsoleScreenBufferSize [KERNEL32.@] Changes size of console
2376 * hConsoleOutput [I] Handle to console screen buffer
2377 * dwSize [I] New size in character rows and cols
2383 BOOL WINAPI SetConsoleScreenBufferSize(HANDLE hConsoleOutput, COORD dwSize)
2387 TRACE("(%p,(%d,%d))\n", hConsoleOutput, dwSize.X, dwSize.Y);
2388 SERVER_START_REQ(set_console_output_info)
2390 req->handle = console_handle_unmap(hConsoleOutput);
2391 req->width = dwSize.X;
2392 req->height = dwSize.Y;
2393 req->mask = SET_CONSOLE_OUTPUT_INFO_SIZE;
2394 ret = !wine_server_call_err( req );
2401 /******************************************************************************
2402 * ScrollConsoleScreenBufferA [KERNEL32.@]
2405 BOOL WINAPI ScrollConsoleScreenBufferA(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2406 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2411 ciw.Attributes = lpFill->Attributes;
2412 MultiByteToWideChar(GetConsoleOutputCP(), 0, &lpFill->Char.AsciiChar, 1, &ciw.Char.UnicodeChar, 1);
2414 return ScrollConsoleScreenBufferW(hConsoleOutput, lpScrollRect, lpClipRect,
2415 dwDestOrigin, &ciw);
2418 /******************************************************************
2419 * CONSOLE_FillLineUniform
2421 * Helper function for ScrollConsoleScreenBufferW
2422 * Fills a part of a line with a constant character info
2424 void CONSOLE_FillLineUniform(HANDLE hConsoleOutput, int i, int j, int len, LPCHAR_INFO lpFill)
2426 SERVER_START_REQ( fill_console_output )
2428 req->handle = console_handle_unmap(hConsoleOutput);
2429 req->mode = CHAR_INFO_MODE_TEXTATTR;
2434 req->data.ch = lpFill->Char.UnicodeChar;
2435 req->data.attr = lpFill->Attributes;
2436 wine_server_call_err( req );
2441 /******************************************************************************
2442 * ScrollConsoleScreenBufferW [KERNEL32.@]
2446 BOOL WINAPI ScrollConsoleScreenBufferW(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2447 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2455 CONSOLE_SCREEN_BUFFER_INFO csbi;
2460 TRACE("(%p,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", hConsoleOutput,
2461 lpScrollRect->Left, lpScrollRect->Top,
2462 lpScrollRect->Right, lpScrollRect->Bottom,
2463 lpClipRect->Left, lpClipRect->Top,
2464 lpClipRect->Right, lpClipRect->Bottom,
2465 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2467 TRACE("(%p,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", hConsoleOutput,
2468 lpScrollRect->Left, lpScrollRect->Top,
2469 lpScrollRect->Right, lpScrollRect->Bottom,
2470 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2472 if (!GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
2475 src.X = lpScrollRect->Left;
2476 src.Y = lpScrollRect->Top;
2478 /* step 1: get dst rect */
2479 dst.Left = dwDestOrigin.X;
2480 dst.Top = dwDestOrigin.Y;
2481 dst.Right = dst.Left + (lpScrollRect->Right - lpScrollRect->Left);
2482 dst.Bottom = dst.Top + (lpScrollRect->Bottom - lpScrollRect->Top);
2484 /* step 2a: compute the final clip rect (optional passed clip and screen buffer limits */
2487 clip.Left = max(0, lpClipRect->Left);
2488 clip.Right = min(csbi.dwSize.X - 1, lpClipRect->Right);
2489 clip.Top = max(0, lpClipRect->Top);
2490 clip.Bottom = min(csbi.dwSize.Y - 1, lpClipRect->Bottom);
2495 clip.Right = csbi.dwSize.X - 1;
2497 clip.Bottom = csbi.dwSize.Y - 1;
2499 if (clip.Left > clip.Right || clip.Top > clip.Bottom) return FALSE;
2501 /* step 2b: clip dst rect */
2502 if (dst.Left < clip.Left ) {src.X += clip.Left - dst.Left; dst.Left = clip.Left;}
2503 if (dst.Top < clip.Top ) {src.Y += clip.Top - dst.Top; dst.Top = clip.Top;}
2504 if (dst.Right > clip.Right ) dst.Right = clip.Right;
2505 if (dst.Bottom > clip.Bottom) dst.Bottom = clip.Bottom;
2507 /* step 3: transfer the bits */
2508 SERVER_START_REQ(move_console_output)
2510 req->handle = console_handle_unmap(hConsoleOutput);
2513 req->x_dst = dst.Left;
2514 req->y_dst = dst.Top;
2515 req->w = dst.Right - dst.Left + 1;
2516 req->h = dst.Bottom - dst.Top + 1;
2517 ret = !wine_server_call_err( req );
2521 if (!ret) return FALSE;
2523 /* step 4: clean out the exposed part */
2525 /* have to write cell [i,j] if it is not in dst rect (because it has already
2526 * been written to by the scroll) and is in clip (we shall not write
2529 for (j = max(lpScrollRect->Top, clip.Top); j <= min(lpScrollRect->Bottom, clip.Bottom); j++)
2531 inside = dst.Top <= j && j <= dst.Bottom;
2533 for (i = max(lpScrollRect->Left, clip.Left); i <= min(lpScrollRect->Right, clip.Right); i++)
2535 if (inside && dst.Left <= i && i <= dst.Right)
2539 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2545 if (start == -1) start = i;
2549 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2555 /******************************************************************
2556 * AttachConsole (KERNEL32.@)
2558 BOOL WINAPI AttachConsole(DWORD dwProcessId)
2560 FIXME("stub %x\n",dwProcessId);
2564 /******************************************************************
2565 * GetConsoleDisplayMode (KERNEL32.@)
2567 BOOL WINAPI GetConsoleDisplayMode(LPDWORD lpModeFlags)
2569 TRACE("semi-stub: %p\n", lpModeFlags);
2570 /* It is safe to successfully report windowed mode */
2575 /******************************************************************
2576 * SetConsoleDisplayMode (KERNEL32.@)
2578 BOOL WINAPI SetConsoleDisplayMode(HANDLE hConsoleOutput, DWORD dwFlags,
2579 COORD *lpNewScreenBufferDimensions)
2581 TRACE("(%p, %x, (%d, %d))\n", hConsoleOutput, dwFlags,
2582 lpNewScreenBufferDimensions->X, lpNewScreenBufferDimensions->Y);
2585 /* We cannot switch to fullscreen */
2592 /* ====================================================================
2594 * Console manipulation functions
2596 * ====================================================================*/
2598 /* some missing functions...
2599 * FIXME: those are likely to be defined as undocumented function in kernel32 (or part of them)
2600 * should get the right API and implement them
2601 * GetConsoleCommandHistory[AW] (dword dword dword)
2602 * GetConsoleCommandHistoryLength[AW]
2603 * SetConsoleCommandHistoryMode
2604 * SetConsoleNumberOfCommands[AW]
2606 int CONSOLE_GetHistory(int idx, WCHAR* buf, int buf_len)
2610 SERVER_START_REQ( get_console_input_history )
2614 if (buf && buf_len > 1)
2616 wine_server_set_reply( req, buf, (buf_len - 1) * sizeof(WCHAR) );
2618 if (!wine_server_call_err( req ))
2620 if (buf) buf[wine_server_reply_size(reply) / sizeof(WCHAR)] = 0;
2621 len = reply->total / sizeof(WCHAR) + 1;
2628 /******************************************************************
2629 * CONSOLE_AppendHistory
2633 BOOL CONSOLE_AppendHistory(const WCHAR* ptr)
2635 size_t len = strlenW(ptr);
2638 while (len && (ptr[len - 1] == '\n' || ptr[len - 1] == '\r')) len--;
2639 if (!len) return FALSE;
2641 SERVER_START_REQ( append_console_input_history )
2644 wine_server_add_data( req, ptr, len * sizeof(WCHAR) );
2645 ret = !wine_server_call_err( req );
2651 /******************************************************************
2652 * CONSOLE_GetNumHistoryEntries
2656 unsigned CONSOLE_GetNumHistoryEntries(void)
2659 SERVER_START_REQ(get_console_input_info)
2662 if (!wine_server_call_err( req )) ret = reply->history_index;
2668 /******************************************************************
2669 * CONSOLE_GetEditionMode
2673 BOOL CONSOLE_GetEditionMode(HANDLE hConIn, int* mode)
2675 unsigned ret = FALSE;
2676 SERVER_START_REQ(get_console_input_info)
2678 req->handle = console_handle_unmap(hConIn);
2679 if ((ret = !wine_server_call_err( req )))
2680 *mode = reply->edition_mode;
2686 /******************************************************************
2691 * 0 if an error occurred, non-zero for success
2694 DWORD WINAPI GetConsoleAliasW(LPWSTR lpSource, LPWSTR lpTargetBuffer,
2695 DWORD TargetBufferLength, LPWSTR lpExename)
2697 FIXME("(%s,%p,%d,%s): stub\n", debugstr_w(lpSource), lpTargetBuffer, TargetBufferLength, debugstr_w(lpExename));
2698 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2702 /******************************************************************
2703 * GetConsoleProcessList (KERNEL32.@)
2705 DWORD WINAPI GetConsoleProcessList(LPDWORD processlist, DWORD processcount)
2707 FIXME("(%p,%d): stub\n", processlist, processcount);
2709 if (!processlist || processcount < 1)
2711 SetLastError(ERROR_INVALID_PARAMETER);