2 * Win32 kernel 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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 UINT console_input_codepage;
59 static UINT console_output_codepage;
62 /* map input records to ASCII */
63 static void input_records_WtoA( INPUT_RECORD *buffer, int count )
68 for (i = 0; i < count; i++)
70 if (buffer[i].EventType != KEY_EVENT) continue;
71 WideCharToMultiByte( GetConsoleCP(), 0,
72 &buffer[i].Event.KeyEvent.uChar.UnicodeChar, 1, &ch, 1, NULL, NULL );
73 buffer[i].Event.KeyEvent.uChar.AsciiChar = ch;
77 /* map input records to Unicode */
78 static void input_records_AtoW( INPUT_RECORD *buffer, int count )
83 for (i = 0; i < count; i++)
85 if (buffer[i].EventType != KEY_EVENT) continue;
86 MultiByteToWideChar( GetConsoleCP(), 0,
87 &buffer[i].Event.KeyEvent.uChar.AsciiChar, 1, &ch, 1 );
88 buffer[i].Event.KeyEvent.uChar.UnicodeChar = ch;
92 /* map char infos to ASCII */
93 static void char_info_WtoA( CHAR_INFO *buffer, int count )
99 WideCharToMultiByte( GetConsoleOutputCP(), 0, &buffer->Char.UnicodeChar, 1,
100 &ch, 1, NULL, NULL );
101 buffer->Char.AsciiChar = ch;
106 /* map char infos to Unicode */
107 static void char_info_AtoW( CHAR_INFO *buffer, int count )
113 MultiByteToWideChar( GetConsoleOutputCP(), 0, &buffer->Char.AsciiChar, 1, &ch, 1 );
114 buffer->Char.UnicodeChar = ch;
120 /******************************************************************************
121 * GetConsoleCP [KERNEL32.@] Returns the OEM code page for the console
126 UINT WINAPI GetConsoleCP(VOID)
128 if (!console_input_codepage) console_input_codepage = GetOEMCP();
129 return console_input_codepage;
133 /******************************************************************************
134 * SetConsoleCP [KERNEL32.@]
136 BOOL WINAPI SetConsoleCP(UINT cp)
138 if (!IsValidCodePage( cp )) return FALSE;
139 console_input_codepage = cp;
144 /***********************************************************************
145 * GetConsoleOutputCP (KERNEL32.@)
147 UINT WINAPI GetConsoleOutputCP(VOID)
149 if (!console_output_codepage) console_output_codepage = GetOEMCP();
150 return console_output_codepage;
154 /******************************************************************************
155 * SetConsoleOutputCP [KERNEL32.@] Set the output codepage used by the console
158 * cp [I] code page to set
164 BOOL WINAPI SetConsoleOutputCP(UINT cp)
166 if (!IsValidCodePage( cp )) return FALSE;
167 console_output_codepage = cp;
172 /***********************************************************************
175 BOOL WINAPI Beep( DWORD dwFreq, DWORD dwDur )
177 static const char beep = '\a';
178 /* dwFreq and dwDur are ignored by Win95 */
179 if (isatty(2)) write( 2, &beep, 1 );
184 /******************************************************************************
185 * WriteConsoleInputA [KERNEL32.@]
187 BOOL WINAPI WriteConsoleInputA( HANDLE handle, const INPUT_RECORD *buffer,
188 DWORD count, LPDWORD written )
193 if (!(recW = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*recW) ))) return FALSE;
194 memcpy( recW, buffer, count*sizeof(*recW) );
195 input_records_AtoW( recW, count );
196 ret = WriteConsoleInputW( handle, recW, count, written );
197 HeapFree( GetProcessHeap(), 0, recW );
202 /******************************************************************************
203 * WriteConsoleInputW [KERNEL32.@]
205 BOOL WINAPI WriteConsoleInputW( HANDLE handle, const INPUT_RECORD *buffer,
206 DWORD count, LPDWORD written )
210 TRACE("(%p,%p,%ld,%p)\n", handle, buffer, count, written);
212 if (written) *written = 0;
213 SERVER_START_REQ( write_console_input )
215 req->handle = console_handle_unmap(handle);
216 wine_server_add_data( req, buffer, count * sizeof(INPUT_RECORD) );
217 if ((ret = !wine_server_call_err( req )) && written)
218 *written = reply->written;
226 /***********************************************************************
227 * WriteConsoleOutputA (KERNEL32.@)
229 BOOL WINAPI WriteConsoleOutputA( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
230 COORD size, COORD coord, LPSMALL_RECT region )
234 COORD new_size, new_coord;
237 new_size.X = min( region->Right - region->Left + 1, size.X - coord.X );
238 new_size.Y = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
240 if (new_size.X <= 0 || new_size.Y <= 0)
242 region->Bottom = region->Top + new_size.Y - 1;
243 region->Right = region->Left + new_size.X - 1;
247 /* only copy the useful rectangle */
248 if (!(ciw = HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO) * new_size.X * new_size.Y )))
250 for (y = 0; y < new_size.Y; y++)
252 memcpy( &ciw[y * new_size.X], &lpBuffer[(y + coord.Y) * size.X + coord.X],
253 new_size.X * sizeof(CHAR_INFO) );
254 char_info_AtoW( ciw, new_size.X );
256 new_coord.X = new_coord.Y = 0;
257 ret = WriteConsoleOutputW( hConsoleOutput, ciw, new_size, new_coord, region );
258 if (ciw) HeapFree( GetProcessHeap(), 0, ciw );
263 /***********************************************************************
264 * WriteConsoleOutputW (KERNEL32.@)
266 BOOL WINAPI WriteConsoleOutputW( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
267 COORD size, COORD coord, LPSMALL_RECT region )
269 int width, height, y;
272 TRACE("(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n",
273 hConsoleOutput, lpBuffer, size.X, size.Y, coord.X, coord.Y,
274 region->Left, region->Top, region->Right, region->Bottom);
276 width = min( region->Right - region->Left + 1, size.X - coord.X );
277 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
279 if (width > 0 && height > 0)
281 for (y = 0; y < height; y++)
283 SERVER_START_REQ( write_console_output )
285 req->handle = console_handle_unmap(hConsoleOutput);
286 req->x = region->Left;
287 req->y = region->Top + y;
288 req->mode = CHAR_INFO_MODE_TEXTATTR;
290 wine_server_add_data( req, &lpBuffer[(y + coord.Y) * size.X + coord.X],
291 width * sizeof(CHAR_INFO));
292 if ((ret = !wine_server_call_err( req )))
294 width = min( width, reply->width - region->Left );
295 height = min( height, reply->height - region->Top );
302 region->Bottom = region->Top + height - 1;
303 region->Right = region->Left + width - 1;
308 /******************************************************************************
309 * WriteConsoleOutputCharacterA [KERNEL32.@] Copies character to consecutive
310 * cells in the console screen buffer
313 * hConsoleOutput [I] Handle to screen buffer
314 * str [I] Pointer to buffer with chars to write
315 * length [I] Number of cells to write to
316 * coord [I] Coords of first cell
317 * lpNumCharsWritten [O] Pointer to number of cells written
319 BOOL WINAPI WriteConsoleOutputCharacterA( HANDLE hConsoleOutput, LPCSTR str, DWORD length,
320 COORD coord, LPDWORD lpNumCharsWritten )
326 TRACE("(%p,%s,%ld,%dx%d,%p)\n", hConsoleOutput,
327 debugstr_an(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
329 lenW = MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, NULL, 0 );
331 if (lpNumCharsWritten) *lpNumCharsWritten = 0;
333 if (!(strW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) ))) return FALSE;
334 MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, strW, lenW );
336 ret = WriteConsoleOutputCharacterW( hConsoleOutput, strW, lenW, coord, lpNumCharsWritten );
337 HeapFree( GetProcessHeap(), 0, strW );
342 /******************************************************************************
343 * WriteConsoleOutputAttribute [KERNEL32.@] Sets attributes for some cells in
344 * the console screen buffer
347 * hConsoleOutput [I] Handle to screen buffer
348 * attr [I] Pointer to buffer with write attributes
349 * length [I] Number of cells to write to
350 * coord [I] Coords of first cell
351 * lpNumAttrsWritten [O] Pointer to number of cells written
358 BOOL WINAPI WriteConsoleOutputAttribute( HANDLE hConsoleOutput, CONST WORD *attr, DWORD length,
359 COORD coord, LPDWORD lpNumAttrsWritten )
363 TRACE("(%p,%p,%ld,%dx%d,%p)\n", hConsoleOutput,attr,length,coord.X,coord.Y,lpNumAttrsWritten);
365 SERVER_START_REQ( write_console_output )
367 req->handle = console_handle_unmap(hConsoleOutput);
370 req->mode = CHAR_INFO_MODE_ATTR;
372 wine_server_add_data( req, attr, length * sizeof(WORD) );
373 if ((ret = !wine_server_call_err( req )))
375 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
383 /******************************************************************************
384 * FillConsoleOutputCharacterA [KERNEL32.@]
387 * hConsoleOutput [I] Handle to screen buffer
388 * ch [I] Character to write
389 * length [I] Number of cells to write to
390 * coord [I] Coords of first cell
391 * lpNumCharsWritten [O] Pointer to number of cells written
397 BOOL WINAPI FillConsoleOutputCharacterA( HANDLE hConsoleOutput, CHAR ch, DWORD length,
398 COORD coord, LPDWORD lpNumCharsWritten )
402 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch, 1, &wch, 1 );
403 return FillConsoleOutputCharacterW(hConsoleOutput, wch, length, coord, lpNumCharsWritten);
407 /******************************************************************************
408 * FillConsoleOutputCharacterW [KERNEL32.@] Writes characters to console
411 * hConsoleOutput [I] Handle to screen buffer
412 * ch [I] Character to write
413 * length [I] Number of cells to write to
414 * coord [I] Coords of first cell
415 * lpNumCharsWritten [O] Pointer to number of cells written
421 BOOL WINAPI FillConsoleOutputCharacterW( HANDLE hConsoleOutput, WCHAR ch, DWORD length,
422 COORD coord, LPDWORD lpNumCharsWritten)
426 TRACE("(%p,%s,%ld,(%dx%d),%p)\n",
427 hConsoleOutput, debugstr_wn(&ch, 1), length, coord.X, coord.Y, lpNumCharsWritten);
429 SERVER_START_REQ( fill_console_output )
431 req->handle = console_handle_unmap(hConsoleOutput);
434 req->mode = CHAR_INFO_MODE_TEXT;
438 if ((ret = !wine_server_call_err( req )))
440 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
448 /******************************************************************************
449 * FillConsoleOutputAttribute [KERNEL32.@] Sets attributes for console
452 * hConsoleOutput [I] Handle to screen buffer
453 * attr [I] Color attribute to write
454 * length [I] Number of cells to write to
455 * coord [I] Coords of first cell
456 * lpNumAttrsWritten [O] Pointer to number of cells written
462 BOOL WINAPI FillConsoleOutputAttribute( HANDLE hConsoleOutput, WORD attr, DWORD length,
463 COORD coord, LPDWORD lpNumAttrsWritten )
467 TRACE("(%p,%d,%ld,(%dx%d),%p)\n",
468 hConsoleOutput, attr, length, coord.X, coord.Y, lpNumAttrsWritten);
470 SERVER_START_REQ( fill_console_output )
472 req->handle = console_handle_unmap(hConsoleOutput);
475 req->mode = CHAR_INFO_MODE_ATTR;
477 req->data.attr = attr;
479 if ((ret = !wine_server_call_err( req )))
481 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
489 /******************************************************************************
490 * ReadConsoleOutputCharacterA [KERNEL32.@]
493 BOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput, LPSTR lpstr, DWORD count,
494 COORD coord, LPDWORD read_count)
498 LPWSTR wptr = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
500 if (read_count) *read_count = 0;
501 if (!wptr) return FALSE;
503 if ((ret = ReadConsoleOutputCharacterW( hConsoleOutput, wptr, count, coord, &read )))
505 read = WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr, read, lpstr, count, NULL, NULL);
506 if (read_count) *read_count = read;
508 HeapFree( GetProcessHeap(), 0, wptr );
513 /******************************************************************************
514 * ReadConsoleOutputCharacterW [KERNEL32.@]
517 BOOL WINAPI ReadConsoleOutputCharacterW( HANDLE hConsoleOutput, LPWSTR buffer, DWORD count,
518 COORD coord, LPDWORD read_count )
522 TRACE( "(%p,%p,%ld,%dx%d,%p)\n", hConsoleOutput, buffer, count, coord.X, coord.Y, read_count );
524 SERVER_START_REQ( read_console_output )
526 req->handle = console_handle_unmap(hConsoleOutput);
529 req->mode = CHAR_INFO_MODE_TEXT;
531 wine_server_set_reply( req, buffer, count * sizeof(WCHAR) );
532 if ((ret = !wine_server_call_err( req )))
534 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WCHAR);
542 /******************************************************************************
543 * ReadConsoleOutputAttribute [KERNEL32.@]
545 BOOL WINAPI ReadConsoleOutputAttribute(HANDLE hConsoleOutput, LPWORD lpAttribute, DWORD length,
546 COORD coord, LPDWORD read_count)
550 TRACE("(%p,%p,%ld,%dx%d,%p)\n",
551 hConsoleOutput, lpAttribute, length, coord.X, coord.Y, read_count);
553 SERVER_START_REQ( read_console_output )
555 req->handle = console_handle_unmap(hConsoleOutput);
558 req->mode = CHAR_INFO_MODE_ATTR;
560 wine_server_set_reply( req, lpAttribute, length * sizeof(WORD) );
561 if ((ret = !wine_server_call_err( req )))
563 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WORD);
571 /******************************************************************************
572 * ReadConsoleOutputA [KERNEL32.@]
575 BOOL WINAPI ReadConsoleOutputA( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
576 COORD coord, LPSMALL_RECT region )
581 ret = ReadConsoleOutputW( hConsoleOutput, lpBuffer, size, coord, region );
582 if (ret && region->Right >= region->Left)
584 for (y = 0; y <= region->Bottom - region->Top; y++)
586 char_info_WtoA( &lpBuffer[(coord.Y + y) * size.X + coord.X],
587 region->Right - region->Left + 1 );
594 /******************************************************************************
595 * ReadConsoleOutputW [KERNEL32.@]
597 * NOTE: The NT4 (sp5) kernel crashes on me if size is (0,0). I don't
598 * think we need to be *that* compatible. -- AJ
600 BOOL WINAPI ReadConsoleOutputW( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
601 COORD coord, LPSMALL_RECT region )
603 int width, height, y;
606 width = min( region->Right - region->Left + 1, size.X - coord.X );
607 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
609 if (width > 0 && height > 0)
611 for (y = 0; y < height; y++)
613 SERVER_START_REQ( read_console_output )
615 req->handle = console_handle_unmap(hConsoleOutput);
616 req->x = region->Left;
617 req->y = region->Top + y;
618 req->mode = CHAR_INFO_MODE_TEXTATTR;
620 wine_server_set_reply( req, &lpBuffer[(y+coord.Y) * size.X + coord.X],
621 width * sizeof(CHAR_INFO) );
622 if ((ret = !wine_server_call_err( req )))
624 width = min( width, reply->width - region->Left );
625 height = min( height, reply->height - region->Top );
632 region->Bottom = region->Top + height - 1;
633 region->Right = region->Left + width - 1;
638 /******************************************************************************
639 * ReadConsoleInputA [KERNEL32.@] Reads data from a console
642 * handle [I] Handle to console input buffer
643 * buffer [O] Address of buffer for read data
644 * count [I] Number of records to read
645 * pRead [O] Address of number of records read
651 BOOL WINAPI ReadConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
655 if (!ReadConsoleInputW( handle, buffer, count, &read )) return FALSE;
656 input_records_WtoA( buffer, read );
657 if (pRead) *pRead = read;
662 /***********************************************************************
663 * PeekConsoleInputA (KERNEL32.@)
665 * Gets 'count' first events (or less) from input queue.
667 BOOL WINAPI PeekConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
671 if (!PeekConsoleInputW( handle, buffer, count, &read )) return FALSE;
672 input_records_WtoA( buffer, read );
673 if (pRead) *pRead = read;
678 /***********************************************************************
679 * PeekConsoleInputW (KERNEL32.@)
681 BOOL WINAPI PeekConsoleInputW( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD read )
684 SERVER_START_REQ( read_console_input )
686 req->handle = console_handle_unmap(handle);
688 wine_server_set_reply( req, buffer, count * sizeof(INPUT_RECORD) );
689 if ((ret = !wine_server_call_err( req )))
691 if (read) *read = count ? reply->read : 0;
699 /***********************************************************************
700 * GetNumberOfConsoleInputEvents (KERNEL32.@)
702 BOOL WINAPI GetNumberOfConsoleInputEvents( HANDLE handle, LPDWORD nrofevents )
705 SERVER_START_REQ( read_console_input )
707 req->handle = console_handle_unmap(handle);
709 if ((ret = !wine_server_call_err( req )))
711 if (nrofevents) *nrofevents = reply->read;
719 /******************************************************************************
722 * Helper function for ReadConsole, ReadConsoleInput and FlushConsoleInputBuffer
725 * 0 for error, 1 for no INPUT_RECORD ready, 2 with INPUT_RECORD ready
727 enum read_console_input_return {rci_error = 0, rci_timeout = 1, rci_gotone = 2};
728 static enum read_console_input_return read_console_input(HANDLE handle, LPINPUT_RECORD ir, DWORD timeout)
730 enum read_console_input_return ret;
732 if (WaitForSingleObject(GetConsoleInputWaitHandle(), timeout) != WAIT_OBJECT_0)
734 SERVER_START_REQ( read_console_input )
736 req->handle = console_handle_unmap(handle);
738 wine_server_set_reply( req, ir, sizeof(INPUT_RECORD) );
739 if (wine_server_call_err( req ) || !reply->read) ret = rci_error;
740 else ret = rci_gotone;
748 /***********************************************************************
749 * FlushConsoleInputBuffer (KERNEL32.@)
751 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
753 enum read_console_input_return last;
756 while ((last = read_console_input(handle, &ir, 0)) == rci_gotone);
758 return last == rci_timeout;
762 /***********************************************************************
763 * SetConsoleTitleA (KERNEL32.@)
765 BOOL WINAPI SetConsoleTitleA( LPCSTR title )
770 DWORD len = MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, NULL, 0 );
771 if (!(titleW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
772 MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, titleW, len );
773 ret = SetConsoleTitleW(titleW);
774 HeapFree(GetProcessHeap(), 0, titleW);
779 /***********************************************************************
780 * GetConsoleTitleA (KERNEL32.@)
782 DWORD WINAPI GetConsoleTitleA(LPSTR title, DWORD size)
784 WCHAR *ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * size);
788 ret = GetConsoleTitleW( ptr, size );
791 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, ret + 1, title, size, NULL, NULL);
794 HeapFree(GetProcessHeap(), 0, ptr);
799 /******************************************************************************
800 * GetConsoleTitleW [KERNEL32.@] Retrieves title string for console
803 * title [O] Address of buffer for title
804 * size [I] Size of buffer
807 * Success: Length of string copied
810 DWORD WINAPI GetConsoleTitleW(LPWSTR title, DWORD size)
814 SERVER_START_REQ( get_console_input_info )
817 wine_server_set_reply( req, title, (size-1) * sizeof(WCHAR) );
818 if (!wine_server_call_err( req ))
820 ret = wine_server_reply_size(reply) / sizeof(WCHAR);
829 /***********************************************************************
830 * GetLargestConsoleWindowSize (KERNEL32.@)
833 * This should return a COORD, but calling convention for returning
834 * structures is different between Windows and gcc on i386.
839 #undef GetLargestConsoleWindowSize
840 DWORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
850 #endif /* defined(__i386__) */
853 /***********************************************************************
854 * GetLargestConsoleWindowSize (KERNEL32.@)
857 * This should return a COORD, but calling convention for returning
858 * structures is different between Windows and gcc on i386.
863 COORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
870 #endif /* defined(__i386__) */
872 static WCHAR* S_EditString /* = NULL */;
873 static unsigned S_EditStrPos /* = 0 */;
875 /***********************************************************************
876 * FreeConsole (KERNEL32.@)
878 BOOL WINAPI FreeConsole(VOID)
882 SERVER_START_REQ(free_console)
884 ret = !wine_server_call_err( req );
890 /******************************************************************
891 * start_console_renderer
893 * helper for AllocConsole
894 * starts the renderer process
896 static BOOL start_console_renderer_helper(const char* appname, STARTUPINFOA* si,
901 PROCESS_INFORMATION pi;
903 /* FIXME: use dynamic allocation for most of the buffers below */
904 ret = snprintf(buffer, sizeof(buffer), "%s --use-event=%d", appname, (INT)hEvent);
905 if ((ret > -1) && (ret < sizeof(buffer)) &&
906 CreateProcessA(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS,
907 NULL, NULL, si, &pi))
909 if (WaitForSingleObject(hEvent, INFINITE) != WAIT_OBJECT_0) return FALSE;
911 TRACE("Started wineconsole pid=%08lx tid=%08lx\n",
912 pi.dwProcessId, pi.dwThreadId);
919 static BOOL start_console_renderer(STARTUPINFOA* si)
923 OBJECT_ATTRIBUTES attr;
926 attr.Length = sizeof(attr);
927 attr.RootDirectory = 0;
928 attr.Attributes = OBJ_INHERIT;
929 attr.ObjectName = NULL;
930 attr.SecurityDescriptor = NULL;
931 attr.SecurityQualityOfService = NULL;
933 NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, &attr, TRUE, FALSE);
934 if (!hEvent) return FALSE;
936 /* first try environment variable */
937 if ((p = getenv("WINECONSOLE")) != NULL)
939 ret = start_console_renderer_helper(p, si, hEvent);
941 ERR("Couldn't launch Wine console from WINECONSOLE env var (%s)... "
942 "trying default access\n", p);
945 /* then try the regular PATH */
947 ret = start_console_renderer_helper("wineconsole", si, hEvent);
953 /***********************************************************************
954 * AllocConsole (KERNEL32.@)
956 * creates an xterm with a pty to our program
958 BOOL WINAPI AllocConsole(void)
960 HANDLE handle_in = INVALID_HANDLE_VALUE;
961 HANDLE handle_out = INVALID_HANDLE_VALUE;
962 HANDLE handle_err = INVALID_HANDLE_VALUE;
963 STARTUPINFOA siCurrent;
964 STARTUPINFOA siConsole;
966 SECURITY_ATTRIBUTES sa;
970 handle_in = CreateFileA( "CONIN$", GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
971 0, NULL, OPEN_EXISTING, 0, 0 );
973 if (handle_in != INVALID_HANDLE_VALUE)
975 /* we already have a console opened on this process, don't create a new one */
976 CloseHandle(handle_in);
980 GetStartupInfoA(&siCurrent);
982 memset(&siConsole, 0, sizeof(siConsole));
983 siConsole.cb = sizeof(siConsole);
984 /* setup a view arguments for wineconsole (it'll use them as default values) */
985 if (siCurrent.dwFlags & STARTF_USECOUNTCHARS)
987 siConsole.dwFlags |= STARTF_USECOUNTCHARS;
988 siConsole.dwXCountChars = siCurrent.dwXCountChars;
989 siConsole.dwYCountChars = siCurrent.dwYCountChars;
991 if (siCurrent.dwFlags & STARTF_USEFILLATTRIBUTE)
993 siConsole.dwFlags |= STARTF_USEFILLATTRIBUTE;
994 siConsole.dwFillAttribute = siCurrent.dwFillAttribute;
996 /* FIXME (should pass the unicode form) */
997 if (siCurrent.lpTitle)
998 siConsole.lpTitle = siCurrent.lpTitle;
999 else if (GetModuleFileNameA(0, buffer, sizeof(buffer)))
1000 siConsole.lpTitle = buffer;
1002 if (!start_console_renderer(&siConsole))
1005 /* all std I/O handles are inheritable by default */
1006 sa.nLength = sizeof(sa);
1007 sa.lpSecurityDescriptor = NULL;
1008 sa.bInheritHandle = TRUE;
1010 handle_in = CreateFileA( "CONIN$", GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
1011 0, &sa, OPEN_EXISTING, 0, 0 );
1012 if (handle_in == INVALID_HANDLE_VALUE) goto the_end;
1014 handle_out = CreateFileA( "CONOUT$", GENERIC_READ|GENERIC_WRITE,
1015 0, &sa, OPEN_EXISTING, 0, 0 );
1016 if (handle_out == INVALID_HANDLE_VALUE) goto the_end;
1018 if (!DuplicateHandle(GetCurrentProcess(), handle_out, GetCurrentProcess(), &handle_err,
1019 0, TRUE, DUPLICATE_SAME_ACCESS))
1022 /* NT resets the STD_*_HANDLEs on console alloc */
1023 SetStdHandle(STD_INPUT_HANDLE, handle_in);
1024 SetStdHandle(STD_OUTPUT_HANDLE, handle_out);
1025 SetStdHandle(STD_ERROR_HANDLE, handle_err);
1027 SetLastError(ERROR_SUCCESS);
1032 ERR("Can't allocate console\n");
1033 if (handle_in != INVALID_HANDLE_VALUE) CloseHandle(handle_in);
1034 if (handle_out != INVALID_HANDLE_VALUE) CloseHandle(handle_out);
1035 if (handle_err != INVALID_HANDLE_VALUE) CloseHandle(handle_err);
1041 /***********************************************************************
1042 * ReadConsoleA (KERNEL32.@)
1044 BOOL WINAPI ReadConsoleA(HANDLE hConsoleInput, LPVOID lpBuffer, DWORD nNumberOfCharsToRead,
1045 LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1047 LPWSTR ptr = HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead * sizeof(WCHAR));
1051 if ((ret = ReadConsoleW(hConsoleInput, ptr, nNumberOfCharsToRead, &ncr, NULL)))
1052 ncr = WideCharToMultiByte(CP_ACP, 0, ptr, ncr, lpBuffer, nNumberOfCharsToRead, NULL, NULL);
1054 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = ncr;
1055 HeapFree(GetProcessHeap(), 0, ptr);
1060 /***********************************************************************
1061 * ReadConsoleW (KERNEL32.@)
1063 BOOL WINAPI ReadConsoleW(HANDLE hConsoleInput, LPVOID lpBuffer,
1064 DWORD nNumberOfCharsToRead, LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1067 LPWSTR xbuf = (LPWSTR)lpBuffer;
1070 TRACE("(%p,%p,%ld,%p,%p)\n",
1071 hConsoleInput, lpBuffer, nNumberOfCharsToRead, lpNumberOfCharsRead, lpReserved);
1073 if (!GetConsoleMode(hConsoleInput, &mode))
1076 if (mode & ENABLE_LINE_INPUT)
1078 if (!S_EditString || S_EditString[S_EditStrPos] == 0)
1080 if (S_EditString) HeapFree(GetProcessHeap(), 0, S_EditString);
1081 if (!(S_EditString = CONSOLE_Readline(hConsoleInput)))
1085 charsread = lstrlenW(&S_EditString[S_EditStrPos]);
1086 if (charsread > nNumberOfCharsToRead) charsread = nNumberOfCharsToRead;
1087 memcpy(xbuf, &S_EditString[S_EditStrPos], charsread * sizeof(WCHAR));
1088 S_EditStrPos += charsread;
1093 DWORD timeout = INFINITE;
1095 /* FIXME: should we read at least 1 char? The SDK does not say */
1096 /* wait for at least one available input record (it doesn't mean we'll have
1097 * chars stored in xbuf...)
1102 if (read_console_input(hConsoleInput, &ir, timeout) != rci_gotone) break;
1104 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown &&
1105 ir.Event.KeyEvent.uChar.UnicodeChar &&
1106 !(ir.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
1108 xbuf[charsread++] = ir.Event.KeyEvent.uChar.UnicodeChar;
1110 } while (charsread < nNumberOfCharsToRead);
1111 /* nothing has been read */
1112 if (timeout == INFINITE) return FALSE;
1115 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = charsread;
1121 /***********************************************************************
1122 * ReadConsoleInputW (KERNEL32.@)
1124 BOOL WINAPI ReadConsoleInputW(HANDLE hConsoleInput, LPINPUT_RECORD lpBuffer,
1125 DWORD nLength, LPDWORD lpNumberOfEventsRead)
1128 DWORD timeout = INFINITE;
1132 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = 0;
1136 /* loop until we get at least one event */
1137 while (read_console_input(hConsoleInput, &lpBuffer[idx], timeout) == rci_gotone &&
1141 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = idx;
1146 /******************************************************************************
1147 * WriteConsoleOutputCharacterW [KERNEL32.@] Copies character to consecutive
1148 * cells in the console screen buffer
1151 * hConsoleOutput [I] Handle to screen buffer
1152 * str [I] Pointer to buffer with chars to write
1153 * length [I] Number of cells to write to
1154 * coord [I] Coords of first cell
1155 * lpNumCharsWritten [O] Pointer to number of cells written
1162 BOOL WINAPI WriteConsoleOutputCharacterW( HANDLE hConsoleOutput, LPCWSTR str, DWORD length,
1163 COORD coord, LPDWORD lpNumCharsWritten )
1167 TRACE("(%p,%s,%ld,%dx%d,%p)\n", hConsoleOutput,
1168 debugstr_wn(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
1170 SERVER_START_REQ( write_console_output )
1172 req->handle = console_handle_unmap(hConsoleOutput);
1175 req->mode = CHAR_INFO_MODE_TEXT;
1177 wine_server_add_data( req, str, length * sizeof(WCHAR) );
1178 if ((ret = !wine_server_call_err( req )))
1180 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
1188 /******************************************************************************
1189 * SetConsoleTitleW [KERNEL32.@] Sets title bar string for console
1192 * title [I] Address of new title
1198 BOOL WINAPI SetConsoleTitleW(LPCWSTR title)
1202 SERVER_START_REQ( set_console_input_info )
1205 req->mask = SET_CONSOLE_INPUT_INFO_TITLE;
1206 wine_server_add_data( req, title, strlenW(title) * sizeof(WCHAR) );
1207 ret = !wine_server_call_err( req );
1214 /***********************************************************************
1215 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
1217 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1219 FIXME("(%p): stub\n", nrofbuttons);
1224 /******************************************************************************
1225 * SetConsoleInputExeNameW [KERNEL32.@]
1230 BOOL WINAPI SetConsoleInputExeNameW(LPCWSTR name)
1232 FIXME("(%s): stub!\n", debugstr_w(name));
1234 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1238 /******************************************************************************
1239 * SetConsoleInputExeNameA [KERNEL32.@]
1244 BOOL WINAPI SetConsoleInputExeNameA(LPCSTR name)
1246 int len = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
1247 LPWSTR xptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1250 if (!xptr) return FALSE;
1252 MultiByteToWideChar(CP_ACP, 0, name, -1, xptr, len);
1253 ret = SetConsoleInputExeNameW(xptr);
1254 HeapFree(GetProcessHeap(), 0, xptr);
1259 /******************************************************************
1260 * CONSOLE_DefaultHandler
1262 * Final control event handler
1264 static BOOL WINAPI CONSOLE_DefaultHandler(DWORD dwCtrlType)
1266 FIXME("Terminating process %lx on event %lx\n", GetCurrentProcessId(), dwCtrlType);
1268 /* should never go here */
1272 /******************************************************************************
1273 * SetConsoleCtrlHandler [KERNEL32.@] Adds function to calling process list
1276 * func [I] Address of handler function
1277 * add [I] Handler to add or remove
1284 * James Sutherland (JamesSutherland@gmx.de)
1285 * Added global variables console_ignore_ctrl_c and handlers[]
1286 * Does not yet do any error checking, or set LastError if failed.
1287 * This doesn't yet matter, since these handlers are not yet called...!
1290 struct ConsoleHandler {
1291 PHANDLER_ROUTINE handler;
1292 struct ConsoleHandler* next;
1295 static unsigned int CONSOLE_IgnoreCtrlC = 0; /* FIXME: this should be inherited somehow */
1296 static struct ConsoleHandler CONSOLE_DefaultConsoleHandler = {CONSOLE_DefaultHandler, NULL};
1297 static struct ConsoleHandler* CONSOLE_Handlers = &CONSOLE_DefaultConsoleHandler;
1299 static CRITICAL_SECTION CONSOLE_CritSect;
1300 static CRITICAL_SECTION_DEBUG critsect_debug =
1302 0, 0, &CONSOLE_CritSect,
1303 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
1304 0, 0, { 0, (DWORD)(__FILE__ ": CONSOLE_CritSect") }
1306 static CRITICAL_SECTION CONSOLE_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
1308 /*****************************************************************************/
1310 BOOL WINAPI SetConsoleCtrlHandler(PHANDLER_ROUTINE func, BOOL add)
1314 FIXME("(%p,%i) - no error checking or testing yet\n", func, add);
1318 CONSOLE_IgnoreCtrlC = add;
1322 struct ConsoleHandler* ch = HeapAlloc(GetProcessHeap(), 0, sizeof(struct ConsoleHandler));
1324 if (!ch) return FALSE;
1326 RtlEnterCriticalSection(&CONSOLE_CritSect);
1327 ch->next = CONSOLE_Handlers;
1328 CONSOLE_Handlers = ch;
1329 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1333 struct ConsoleHandler** ch;
1334 RtlEnterCriticalSection(&CONSOLE_CritSect);
1335 for (ch = &CONSOLE_Handlers; *ch; *ch = (*ch)->next)
1337 if ((*ch)->handler == func) break;
1341 struct ConsoleHandler* rch = *ch;
1344 if (rch == &CONSOLE_DefaultConsoleHandler)
1346 ERR("Who's trying to remove default handler???\n");
1353 HeapFree(GetProcessHeap(), 0, rch);
1358 WARN("Attempt to remove non-installed CtrlHandler %p\n", func);
1361 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1366 static WINE_EXCEPTION_FILTER(CONSOLE_CtrlEventHandler)
1368 TRACE("(%lx)\n", GetExceptionCode());
1369 return EXCEPTION_EXECUTE_HANDLER;
1372 static DWORD WINAPI CONSOLE_HandleCtrlCEntry(void* pmt)
1374 struct ConsoleHandler* ch;
1376 RtlEnterCriticalSection(&CONSOLE_CritSect);
1377 /* the debugger didn't continue... so, pass to ctrl handlers */
1378 for (ch = CONSOLE_Handlers; ch; ch = ch->next)
1380 if (ch->handler((DWORD)pmt)) break;
1382 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1386 /******************************************************************
1387 * CONSOLE_HandleCtrlC
1389 * Check whether the shall manipulate CtrlC events
1391 int CONSOLE_HandleCtrlC(unsigned sig)
1393 /* FIXME: better test whether a console is attached to this process ??? */
1394 extern unsigned CONSOLE_GetNumHistoryEntries(void);
1395 if (CONSOLE_GetNumHistoryEntries() == (unsigned)-1) return 0;
1396 if (CONSOLE_IgnoreCtrlC) return 1;
1398 /* try to pass the exception to the debugger
1399 * if it continues, there's nothing more to do
1400 * otherwise, we need to send the ctrl-event to the handlers
1404 RaiseException( DBG_CONTROL_C, 0, 0, NULL );
1406 __EXCEPT(CONSOLE_CtrlEventHandler)
1408 /* Create a separate thread to signal all the events. This would allow to
1409 * synchronize between setting the handlers and actually calling them
1411 CreateThread(NULL, 0, CONSOLE_HandleCtrlCEntry, (void*)CTRL_C_EVENT, 0, NULL);
1417 /******************************************************************************
1418 * GenerateConsoleCtrlEvent [KERNEL32.@] Simulate a CTRL-C or CTRL-BREAK
1421 * dwCtrlEvent [I] Type of event
1422 * dwProcessGroupID [I] Process group ID to send event to
1426 * Failure: False (and *should* [but doesn't] set LastError)
1428 BOOL WINAPI GenerateConsoleCtrlEvent(DWORD dwCtrlEvent,
1429 DWORD dwProcessGroupID)
1433 TRACE("(%ld, %ld)\n", dwCtrlEvent, dwProcessGroupID);
1435 if (dwCtrlEvent != CTRL_C_EVENT && dwCtrlEvent != CTRL_BREAK_EVENT)
1437 ERR("Invalid event %ld for PGID %ld\n", dwCtrlEvent, dwProcessGroupID);
1441 SERVER_START_REQ( send_console_signal )
1443 req->signal = dwCtrlEvent;
1444 req->group_id = dwProcessGroupID;
1445 ret = !wine_server_call_err( req );
1453 /******************************************************************************
1454 * CreateConsoleScreenBuffer [KERNEL32.@] Creates a console screen buffer
1457 * dwDesiredAccess [I] Access flag
1458 * dwShareMode [I] Buffer share mode
1459 * sa [I] Security attributes
1460 * dwFlags [I] Type of buffer to create
1461 * lpScreenBufferData [I] Reserved
1464 * Should call SetLastError
1467 * Success: Handle to new console screen buffer
1468 * Failure: INVALID_HANDLE_VALUE
1470 HANDLE WINAPI CreateConsoleScreenBuffer(DWORD dwDesiredAccess, DWORD dwShareMode,
1471 LPSECURITY_ATTRIBUTES sa, DWORD dwFlags,
1472 LPVOID lpScreenBufferData)
1474 HANDLE ret = INVALID_HANDLE_VALUE;
1476 TRACE("(%ld,%ld,%p,%ld,%p)\n",
1477 dwDesiredAccess, dwShareMode, sa, dwFlags, lpScreenBufferData);
1479 if (dwFlags != CONSOLE_TEXTMODE_BUFFER || lpScreenBufferData != NULL)
1481 SetLastError(ERROR_INVALID_PARAMETER);
1482 return INVALID_HANDLE_VALUE;
1485 SERVER_START_REQ(create_console_output)
1488 req->access = dwDesiredAccess;
1489 req->share = dwShareMode;
1490 req->inherit = (sa && sa->bInheritHandle);
1491 if (!wine_server_call_err( req )) ret = reply->handle_out;
1499 /***********************************************************************
1500 * GetConsoleScreenBufferInfo (KERNEL32.@)
1502 BOOL WINAPI GetConsoleScreenBufferInfo(HANDLE hConsoleOutput, LPCONSOLE_SCREEN_BUFFER_INFO csbi)
1506 SERVER_START_REQ(get_console_output_info)
1508 req->handle = console_handle_unmap(hConsoleOutput);
1509 if ((ret = !wine_server_call_err( req )))
1511 csbi->dwSize.X = reply->width;
1512 csbi->dwSize.Y = reply->height;
1513 csbi->dwCursorPosition.X = reply->cursor_x;
1514 csbi->dwCursorPosition.Y = reply->cursor_y;
1515 csbi->wAttributes = reply->attr;
1516 csbi->srWindow.Left = reply->win_left;
1517 csbi->srWindow.Right = reply->win_right;
1518 csbi->srWindow.Top = reply->win_top;
1519 csbi->srWindow.Bottom = reply->win_bottom;
1520 csbi->dwMaximumWindowSize.X = reply->max_width;
1521 csbi->dwMaximumWindowSize.Y = reply->max_height;
1530 /******************************************************************************
1531 * SetConsoleActiveScreenBuffer [KERNEL32.@] Sets buffer to current console
1537 BOOL WINAPI SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput)
1541 TRACE("(%p)\n", hConsoleOutput);
1543 SERVER_START_REQ( set_console_input_info )
1546 req->mask = SET_CONSOLE_INPUT_INFO_ACTIVE_SB;
1547 req->active_sb = hConsoleOutput;
1548 ret = !wine_server_call_err( req );
1555 /***********************************************************************
1556 * GetConsoleMode (KERNEL32.@)
1558 BOOL WINAPI GetConsoleMode(HANDLE hcon, LPDWORD mode)
1562 SERVER_START_REQ(get_console_mode)
1564 req->handle = console_handle_unmap(hcon);
1565 ret = !wine_server_call_err( req );
1566 if (ret && mode) *mode = reply->mode;
1573 /******************************************************************************
1574 * SetConsoleMode [KERNEL32.@] Sets input mode of console's input buffer
1577 * hcon [I] Handle to console input or screen buffer
1578 * mode [I] Input or output mode to set
1585 * ENABLE_PROCESSED_INPUT 0x01
1586 * ENABLE_LINE_INPUT 0x02
1587 * ENABLE_ECHO_INPUT 0x04
1588 * ENABLE_WINDOW_INPUT 0x08
1589 * ENABLE_MOUSE_INPUT 0x10
1591 BOOL WINAPI SetConsoleMode(HANDLE hcon, DWORD mode)
1595 SERVER_START_REQ(set_console_mode)
1597 req->handle = console_handle_unmap(hcon);
1599 ret = !wine_server_call_err( req );
1602 /* FIXME: when resetting a console input to editline mode, I think we should
1603 * empty the S_EditString buffer
1606 TRACE("(%p,%lx) retval == %d\n", hcon, mode, ret);
1612 /******************************************************************
1613 * CONSOLE_WriteChars
1615 * WriteConsoleOutput helper: hides server call semantics
1616 * writes a string at a given pos with standard attribute
1618 int CONSOLE_WriteChars(HANDLE hCon, LPCWSTR lpBuffer, int nc, COORD* pos)
1624 SERVER_START_REQ( write_console_output )
1626 req->handle = console_handle_unmap(hCon);
1629 req->mode = CHAR_INFO_MODE_TEXTSTDATTR;
1631 wine_server_add_data( req, lpBuffer, nc * sizeof(WCHAR) );
1632 if (!wine_server_call_err( req )) written = reply->written;
1636 if (written > 0) pos->X += written;
1640 /******************************************************************
1643 * WriteConsoleOutput helper: handles passing to next line (+scrolling if necessary)
1646 static int next_line(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi)
1652 csbi->dwCursorPosition.X = 0;
1653 csbi->dwCursorPosition.Y++;
1655 if (csbi->dwCursorPosition.Y < csbi->dwSize.Y) return 1;
1658 src.Bottom = csbi->dwSize.Y - 1;
1660 src.Right = csbi->dwSize.X - 1;
1665 ci.Attributes = csbi->wAttributes;
1666 ci.Char.UnicodeChar = ' ';
1668 csbi->dwCursorPosition.Y--;
1669 if (!ScrollConsoleScreenBufferW(hCon, &src, NULL, dst, &ci))
1674 /******************************************************************
1677 * WriteConsoleOutput helper: writes a block of non special characters
1678 * Block can spread on several lines, and wrapping, if needed, is
1682 static int write_block(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi,
1683 DWORD mode, LPWSTR ptr, int len)
1685 int blk; /* number of chars to write on current line */
1686 int done; /* number of chars already written */
1688 if (len <= 0) return 1;
1690 if (mode & ENABLE_WRAP_AT_EOL_OUTPUT) /* writes remaining on next line */
1692 for (done = 0; done < len; done += blk)
1694 blk = min(len - done, csbi->dwSize.X - csbi->dwCursorPosition.X);
1696 if (CONSOLE_WriteChars(hCon, ptr + done, blk, &csbi->dwCursorPosition) != blk)
1698 if (csbi->dwCursorPosition.X == csbi->dwSize.X && !next_line(hCon, csbi))
1704 int pos = csbi->dwCursorPosition.X;
1705 /* FIXME: we could reduce the number of loops
1706 * but, in most cases we wouldn't gain lots of time (it would only
1707 * happen if we're asked to overwrite more than twice the part of the line,
1710 for (blk = done = 0; done < len; done += blk)
1712 blk = min(len - done, csbi->dwSize.X - csbi->dwCursorPosition.X);
1714 csbi->dwCursorPosition.X = pos;
1715 if (CONSOLE_WriteChars(hCon, ptr + done, blk, &csbi->dwCursorPosition) != blk)
1723 /***********************************************************************
1724 * WriteConsoleW (KERNEL32.@)
1726 BOOL WINAPI WriteConsoleW(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
1727 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
1731 WCHAR* psz = (WCHAR*)lpBuffer;
1732 CONSOLE_SCREEN_BUFFER_INFO csbi;
1735 TRACE("%p %s %ld %p %p\n",
1736 hConsoleOutput, debugstr_wn(lpBuffer, nNumberOfCharsToWrite),
1737 nNumberOfCharsToWrite, lpNumberOfCharsWritten, lpReserved);
1739 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
1741 if (!GetConsoleMode(hConsoleOutput, &mode) ||
1742 !GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
1745 if (mode & ENABLE_PROCESSED_OUTPUT)
1749 for (i = 0; i < nNumberOfCharsToWrite; i++)
1753 case '\b': case '\t': case '\n': case '\a': case '\r':
1754 /* don't handle here the i-th char... done below */
1755 if ((k = i - first) > 0)
1757 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
1767 if (csbi.dwCursorPosition.X > 0) csbi.dwCursorPosition.X--;
1771 WCHAR tmp[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
1773 if (!write_block(hConsoleOutput, &csbi, mode, tmp,
1774 ((csbi.dwCursorPosition.X + 8) & ~7) - csbi.dwCursorPosition.X))
1779 next_line(hConsoleOutput, &csbi);
1785 csbi.dwCursorPosition.X = 0;
1793 /* write the remaining block (if any) if processed output is enabled, or the
1794 * entire buffer otherwise
1796 if ((k = nNumberOfCharsToWrite - first) > 0)
1798 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
1804 SetConsoleCursorPosition(hConsoleOutput, csbi.dwCursorPosition);
1805 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = nw;
1810 /***********************************************************************
1811 * WriteConsoleA (KERNEL32.@)
1813 BOOL WINAPI WriteConsoleA(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
1814 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
1820 n = MultiByteToWideChar(CP_ACP, 0, lpBuffer, nNumberOfCharsToWrite, NULL, 0);
1822 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
1823 xstring = HeapAlloc(GetProcessHeap(), 0, n * sizeof(WCHAR));
1824 if (!xstring) return 0;
1826 MultiByteToWideChar(CP_ACP, 0, lpBuffer, nNumberOfCharsToWrite, xstring, n);
1828 ret = WriteConsoleW(hConsoleOutput, xstring, n, lpNumberOfCharsWritten, 0);
1830 HeapFree(GetProcessHeap(), 0, xstring);
1835 /******************************************************************************
1836 * SetConsoleCursorPosition [KERNEL32.@]
1837 * Sets the cursor position in console
1840 * hConsoleOutput [I] Handle of console screen buffer
1841 * dwCursorPosition [I] New cursor position coordinates
1845 BOOL WINAPI SetConsoleCursorPosition(HANDLE hcon, COORD pos)
1848 CONSOLE_SCREEN_BUFFER_INFO csbi;
1852 TRACE("%p %d %d\n", hcon, pos.X, pos.Y);
1854 SERVER_START_REQ(set_console_output_info)
1856 req->handle = console_handle_unmap(hcon);
1857 req->cursor_x = pos.X;
1858 req->cursor_y = pos.Y;
1859 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_POS;
1860 ret = !wine_server_call_err( req );
1864 if (!ret || !GetConsoleScreenBufferInfo(hcon, &csbi))
1867 /* if cursor is no longer visible, scroll the visible window... */
1868 w = csbi.srWindow.Right - csbi.srWindow.Left + 1;
1869 h = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
1870 if (pos.X < csbi.srWindow.Left)
1872 csbi.srWindow.Left = min(pos.X, csbi.dwSize.X - w);
1875 else if (pos.X > csbi.srWindow.Right)
1877 csbi.srWindow.Left = max(pos.X, w) - w + 1;
1880 csbi.srWindow.Right = csbi.srWindow.Left + w - 1;
1882 if (pos.Y < csbi.srWindow.Top)
1884 csbi.srWindow.Top = min(pos.Y, csbi.dwSize.Y - h);
1887 else if (pos.Y > csbi.srWindow.Bottom)
1889 csbi.srWindow.Top = max(pos.Y, h) - h + 1;
1892 csbi.srWindow.Bottom = csbi.srWindow.Top + h - 1;
1894 ret = (do_move) ? SetConsoleWindowInfo(hcon, TRUE, &csbi.srWindow) : TRUE;
1899 /******************************************************************************
1900 * GetConsoleCursorInfo [KERNEL32.@] Gets size and visibility of console
1903 * hcon [I] Handle to console screen buffer
1904 * cinfo [O] Address of cursor information
1910 BOOL WINAPI GetConsoleCursorInfo(HANDLE hcon, LPCONSOLE_CURSOR_INFO cinfo)
1914 SERVER_START_REQ(get_console_output_info)
1916 req->handle = console_handle_unmap(hcon);
1917 ret = !wine_server_call_err( req );
1920 cinfo->dwSize = reply->cursor_size;
1921 cinfo->bVisible = reply->cursor_visible;
1929 /******************************************************************************
1930 * SetConsoleCursorInfo [KERNEL32.@] Sets size and visibility of cursor
1933 * hcon [I] Handle to console screen buffer
1934 * cinfo [I] Address of cursor information
1939 BOOL WINAPI SetConsoleCursorInfo(HANDLE hCon, LPCONSOLE_CURSOR_INFO cinfo)
1943 SERVER_START_REQ(set_console_output_info)
1945 req->handle = console_handle_unmap(hCon);
1946 req->cursor_size = cinfo->dwSize;
1947 req->cursor_visible = cinfo->bVisible;
1948 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM;
1949 ret = !wine_server_call_err( req );
1956 /******************************************************************************
1957 * SetConsoleWindowInfo [KERNEL32.@] Sets size and position of console
1960 * hcon [I] Handle to console screen buffer
1961 * bAbsolute [I] Coordinate type flag
1962 * window [I] Address of new window rectangle
1967 BOOL WINAPI SetConsoleWindowInfo(HANDLE hCon, BOOL bAbsolute, LPSMALL_RECT window)
1969 SMALL_RECT p = *window;
1974 CONSOLE_SCREEN_BUFFER_INFO csbi;
1975 if (!GetConsoleScreenBufferInfo(hCon, &csbi))
1977 p.Left += csbi.srWindow.Left;
1978 p.Top += csbi.srWindow.Top;
1979 p.Right += csbi.srWindow.Left;
1980 p.Bottom += csbi.srWindow.Top;
1982 SERVER_START_REQ(set_console_output_info)
1984 req->handle = console_handle_unmap(hCon);
1985 req->win_left = p.Left;
1986 req->win_top = p.Top;
1987 req->win_right = p.Right;
1988 req->win_bottom = p.Bottom;
1989 req->mask = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
1990 ret = !wine_server_call_err( req );
1998 /******************************************************************************
1999 * SetConsoleTextAttribute [KERNEL32.@] Sets colors for text
2001 * Sets the foreground and background color attributes of characters
2002 * written to the screen buffer.
2008 BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttr)
2012 SERVER_START_REQ(set_console_output_info)
2014 req->handle = console_handle_unmap(hConsoleOutput);
2016 req->mask = SET_CONSOLE_OUTPUT_INFO_ATTR;
2017 ret = !wine_server_call_err( req );
2024 /******************************************************************************
2025 * SetConsoleScreenBufferSize [KERNEL32.@] Changes size of console
2028 * hConsoleOutput [I] Handle to console screen buffer
2029 * dwSize [I] New size in character rows and cols
2035 BOOL WINAPI SetConsoleScreenBufferSize(HANDLE hConsoleOutput, COORD dwSize)
2039 SERVER_START_REQ(set_console_output_info)
2041 req->handle = console_handle_unmap(hConsoleOutput);
2042 req->width = dwSize.X;
2043 req->height = dwSize.Y;
2044 req->mask = SET_CONSOLE_OUTPUT_INFO_SIZE;
2045 ret = !wine_server_call_err( req );
2052 /******************************************************************************
2053 * ScrollConsoleScreenBufferA [KERNEL32.@]
2056 BOOL WINAPI ScrollConsoleScreenBufferA(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2057 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2062 ciw.Attributes = lpFill->Attributes;
2063 MultiByteToWideChar(CP_ACP, 0, &lpFill->Char.AsciiChar, 1, &ciw.Char.UnicodeChar, 1);
2065 return ScrollConsoleScreenBufferW(hConsoleOutput, lpScrollRect, lpClipRect,
2066 dwDestOrigin, &ciw);
2069 /******************************************************************
2070 * CONSOLE_FillLineUniform
2072 * Helper function for ScrollConsoleScreenBufferW
2073 * Fills a part of a line with a constant character info
2075 void CONSOLE_FillLineUniform(HANDLE hConsoleOutput, int i, int j, int len, LPCHAR_INFO lpFill)
2077 SERVER_START_REQ( fill_console_output )
2079 req->handle = console_handle_unmap(hConsoleOutput);
2080 req->mode = CHAR_INFO_MODE_TEXTATTR;
2085 req->data.ch = lpFill->Char.UnicodeChar;
2086 req->data.attr = lpFill->Attributes;
2087 wine_server_call_err( req );
2092 /******************************************************************************
2093 * ScrollConsoleScreenBufferW [KERNEL32.@]
2097 BOOL WINAPI ScrollConsoleScreenBufferW(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2098 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2106 CONSOLE_SCREEN_BUFFER_INFO csbi;
2110 TRACE("(%p,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", hConsoleOutput,
2111 lpScrollRect->Left, lpScrollRect->Top,
2112 lpScrollRect->Right, lpScrollRect->Bottom,
2113 lpClipRect->Left, lpClipRect->Top,
2114 lpClipRect->Right, lpClipRect->Bottom,
2115 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2117 TRACE("(%p,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", hConsoleOutput,
2118 lpScrollRect->Left, lpScrollRect->Top,
2119 lpScrollRect->Right, lpScrollRect->Bottom,
2120 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2122 if (!GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
2125 /* step 1: get dst rect */
2126 dst.Left = dwDestOrigin.X;
2127 dst.Top = dwDestOrigin.Y;
2128 dst.Right = dst.Left + (lpScrollRect->Right - lpScrollRect->Left);
2129 dst.Bottom = dst.Top + (lpScrollRect->Bottom - lpScrollRect->Top);
2131 /* step 2a: compute the final clip rect (optional passed clip and screen buffer limits */
2134 clip.Left = max(0, lpClipRect->Left);
2135 clip.Right = min(csbi.dwSize.X - 1, lpClipRect->Right);
2136 clip.Top = max(0, lpClipRect->Top);
2137 clip.Bottom = min(csbi.dwSize.Y - 1, lpClipRect->Bottom);
2142 clip.Right = csbi.dwSize.X - 1;
2144 clip.Bottom = csbi.dwSize.Y - 1;
2146 if (clip.Left > clip.Right || clip.Top > clip.Bottom) return FALSE;
2148 /* step 2b: clip dst rect */
2149 if (dst.Left < clip.Left ) dst.Left = clip.Left;
2150 if (dst.Top < clip.Top ) dst.Top = clip.Top;
2151 if (dst.Right > clip.Right ) dst.Right = clip.Right;
2152 if (dst.Bottom > clip.Bottom) dst.Bottom = clip.Bottom;
2154 /* step 3: transfer the bits */
2155 SERVER_START_REQ(move_console_output)
2157 req->handle = console_handle_unmap(hConsoleOutput);
2158 req->x_src = lpScrollRect->Left;
2159 req->y_src = lpScrollRect->Top;
2160 req->x_dst = dst.Left;
2161 req->y_dst = dst.Top;
2162 req->w = dst.Right - dst.Left + 1;
2163 req->h = dst.Bottom - dst.Top + 1;
2164 ret = !wine_server_call_err( req );
2168 if (!ret) return FALSE;
2170 /* step 4: clean out the exposed part */
2172 /* have to write cell [i,j] if it is not in dst rect (because it has already
2173 * been written to by the scroll) and is in clip (we shall not write
2176 for (j = max(lpScrollRect->Top, clip.Top); j <= min(lpScrollRect->Bottom, clip.Bottom); j++)
2178 inside = dst.Top <= j && j <= dst.Bottom;
2180 for (i = max(lpScrollRect->Left, clip.Left); i <= min(lpScrollRect->Right, clip.Right); i++)
2182 if (inside && dst.Left <= i && i <= dst.Right)
2186 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2192 if (start == -1) start = i;
2196 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2203 /* ====================================================================
2205 * Console manipulation functions
2207 * ====================================================================*/
2209 /* some missing functions...
2210 * FIXME: those are likely to be defined as undocumented function in kernel32 (or part of them)
2211 * should get the right API and implement them
2212 * GetConsoleCommandHistory[AW] (dword dword dword)
2213 * GetConsoleCommandHistoryLength[AW]
2214 * SetConsoleCommandHistoryMode
2215 * SetConsoleNumberOfCommands[AW]
2217 int CONSOLE_GetHistory(int idx, WCHAR* buf, int buf_len)
2221 SERVER_START_REQ( get_console_input_history )
2225 if (buf && buf_len > 1)
2227 wine_server_set_reply( req, buf, (buf_len - 1) * sizeof(WCHAR) );
2229 if (!wine_server_call_err( req ))
2231 if (buf) buf[wine_server_reply_size(reply) / sizeof(WCHAR)] = 0;
2232 len = reply->total / sizeof(WCHAR) + 1;
2239 /******************************************************************
2240 * CONSOLE_AppendHistory
2244 BOOL CONSOLE_AppendHistory(const WCHAR* ptr)
2246 size_t len = strlenW(ptr);
2249 while (len && (ptr[len - 1] == '\n' || ptr[len - 1] == '\r')) len--;
2251 SERVER_START_REQ( append_console_input_history )
2254 wine_server_add_data( req, ptr, len * sizeof(WCHAR) );
2255 ret = !wine_server_call_err( req );
2261 /******************************************************************
2262 * CONSOLE_GetNumHistoryEntries
2266 unsigned CONSOLE_GetNumHistoryEntries(void)
2269 SERVER_START_REQ(get_console_input_info)
2272 if (!wine_server_call_err( req )) ret = reply->history_index;
2278 /******************************************************************
2279 * CONSOLE_GetEditionMode
2283 BOOL CONSOLE_GetEditionMode(HANDLE hConIn, int* mode)
2285 unsigned ret = FALSE;
2286 SERVER_START_REQ(get_console_input_info)
2288 req->handle = console_handle_unmap(hConIn);
2289 if ((ret = !wine_server_call_err( req )))
2290 *mode = reply->edition_mode;