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/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 UINT console_input_codepage;
58 static UINT console_output_codepage;
61 /* map input records to ASCII */
62 static void input_records_WtoA( INPUT_RECORD *buffer, int count )
67 for (i = 0; i < count; i++)
69 if (buffer[i].EventType != KEY_EVENT) continue;
70 WideCharToMultiByte( GetConsoleCP(), 0,
71 &buffer[i].Event.KeyEvent.uChar.UnicodeChar, 1, &ch, 1, NULL, NULL );
72 buffer[i].Event.KeyEvent.uChar.AsciiChar = ch;
76 /* map input records to Unicode */
77 static void input_records_AtoW( INPUT_RECORD *buffer, int count )
82 for (i = 0; i < count; i++)
84 if (buffer[i].EventType != KEY_EVENT) continue;
85 MultiByteToWideChar( GetConsoleCP(), 0,
86 &buffer[i].Event.KeyEvent.uChar.AsciiChar, 1, &ch, 1 );
87 buffer[i].Event.KeyEvent.uChar.UnicodeChar = ch;
91 /* map char infos to ASCII */
92 static void char_info_WtoA( CHAR_INFO *buffer, int count )
98 WideCharToMultiByte( GetConsoleOutputCP(), 0, &buffer->Char.UnicodeChar, 1,
100 buffer->Char.AsciiChar = ch;
105 /* map char infos to Unicode */
106 static void char_info_AtoW( CHAR_INFO *buffer, int count )
112 MultiByteToWideChar( GetConsoleOutputCP(), 0, &buffer->Char.AsciiChar, 1, &ch, 1 );
113 buffer->Char.UnicodeChar = ch;
119 /******************************************************************************
120 * GetConsoleCP [KERNEL32.@] Returns the OEM code page for the console
125 UINT WINAPI GetConsoleCP(VOID)
127 if (!console_input_codepage) console_input_codepage = GetOEMCP();
128 return console_input_codepage;
132 /******************************************************************************
133 * SetConsoleCP [KERNEL32.@]
135 BOOL WINAPI SetConsoleCP(UINT cp)
137 if (!IsValidCodePage( cp )) return FALSE;
138 console_input_codepage = cp;
143 /***********************************************************************
144 * GetConsoleOutputCP (KERNEL32.@)
146 UINT WINAPI GetConsoleOutputCP(VOID)
148 if (!console_output_codepage) console_output_codepage = GetOEMCP();
149 return console_output_codepage;
153 /******************************************************************************
154 * SetConsoleOutputCP [KERNEL32.@] Set the output codepage used by the console
157 * cp [I] code page to set
163 BOOL WINAPI SetConsoleOutputCP(UINT cp)
165 if (!IsValidCodePage( cp )) return FALSE;
166 console_output_codepage = cp;
171 /***********************************************************************
174 BOOL WINAPI Beep( DWORD dwFreq, DWORD dwDur )
176 static const char beep = '\a';
177 /* dwFreq and dwDur are ignored by Win95 */
178 if (isatty(2)) write( 2, &beep, 1 );
183 /******************************************************************************
184 * WriteConsoleInputA [KERNEL32.@]
186 BOOL WINAPI WriteConsoleInputA( HANDLE handle, const INPUT_RECORD *buffer,
187 DWORD count, LPDWORD written )
192 if (!(recW = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*recW) ))) return FALSE;
193 memcpy( recW, buffer, count*sizeof(*recW) );
194 input_records_AtoW( recW, count );
195 ret = WriteConsoleInputW( handle, recW, count, written );
196 HeapFree( GetProcessHeap(), 0, recW );
201 /******************************************************************************
202 * WriteConsoleInputW [KERNEL32.@]
204 BOOL WINAPI WriteConsoleInputW( HANDLE handle, const INPUT_RECORD *buffer,
205 DWORD count, LPDWORD written )
209 TRACE("(%p,%p,%ld,%p)\n", handle, buffer, count, written);
211 if (written) *written = 0;
212 SERVER_START_REQ( write_console_input )
214 req->handle = console_handle_unmap(handle);
215 wine_server_add_data( req, buffer, count * sizeof(INPUT_RECORD) );
216 if ((ret = !wine_server_call_err( req )) && written)
217 *written = reply->written;
225 /***********************************************************************
226 * WriteConsoleOutputA (KERNEL32.@)
228 BOOL WINAPI WriteConsoleOutputA( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
229 COORD size, COORD coord, LPSMALL_RECT region )
233 COORD new_size, new_coord;
236 new_size.X = min( region->Right - region->Left + 1, size.X - coord.X );
237 new_size.Y = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
239 if (new_size.X <= 0 || new_size.Y <= 0)
241 region->Bottom = region->Top + new_size.Y - 1;
242 region->Right = region->Left + new_size.X - 1;
246 /* only copy the useful rectangle */
247 if (!(ciw = HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO) * new_size.X * new_size.Y )))
249 for (y = 0; y < new_size.Y; y++)
251 memcpy( &ciw[y * new_size.X], &lpBuffer[(y + coord.Y) * size.X + coord.X],
252 new_size.X * sizeof(CHAR_INFO) );
253 char_info_AtoW( ciw, new_size.X );
255 new_coord.X = new_coord.Y = 0;
256 ret = WriteConsoleOutputW( hConsoleOutput, ciw, new_size, new_coord, region );
257 if (ciw) HeapFree( GetProcessHeap(), 0, ciw );
262 /***********************************************************************
263 * WriteConsoleOutputW (KERNEL32.@)
265 BOOL WINAPI WriteConsoleOutputW( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
266 COORD size, COORD coord, LPSMALL_RECT region )
268 int width, height, y;
271 TRACE("(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n",
272 hConsoleOutput, lpBuffer, size.X, size.Y, coord.X, coord.Y,
273 region->Left, region->Top, region->Right, region->Bottom);
275 width = min( region->Right - region->Left + 1, size.X - coord.X );
276 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
278 if (width > 0 && height > 0)
280 for (y = 0; y < height; y++)
282 SERVER_START_REQ( write_console_output )
284 req->handle = console_handle_unmap(hConsoleOutput);
285 req->x = region->Left;
286 req->y = region->Top + y;
287 req->mode = CHAR_INFO_MODE_TEXTATTR;
289 wine_server_add_data( req, &lpBuffer[(y + coord.Y) * size.X + coord.X],
290 width * sizeof(CHAR_INFO));
291 if ((ret = !wine_server_call_err( req )))
293 width = min( width, reply->width - region->Left );
294 height = min( height, reply->height - region->Top );
301 region->Bottom = region->Top + height - 1;
302 region->Right = region->Left + width - 1;
307 /******************************************************************************
308 * WriteConsoleOutputCharacterA [KERNEL32.@] Copies character to consecutive
309 * cells in the console screen buffer
312 * hConsoleOutput [I] Handle to screen buffer
313 * str [I] Pointer to buffer with chars to write
314 * length [I] Number of cells to write to
315 * coord [I] Coords of first cell
316 * lpNumCharsWritten [O] Pointer to number of cells written
318 BOOL WINAPI WriteConsoleOutputCharacterA( HANDLE hConsoleOutput, LPCSTR str, DWORD length,
319 COORD coord, LPDWORD lpNumCharsWritten )
325 TRACE("(%p,%s,%ld,%dx%d,%p)\n", hConsoleOutput,
326 debugstr_an(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
328 lenW = MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, NULL, 0 );
330 if (lpNumCharsWritten) *lpNumCharsWritten = 0;
332 if (!(strW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) ))) return FALSE;
333 MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, strW, lenW );
335 ret = WriteConsoleOutputCharacterW( hConsoleOutput, strW, lenW, coord, lpNumCharsWritten );
336 HeapFree( GetProcessHeap(), 0, strW );
341 /******************************************************************************
342 * WriteConsoleOutputAttribute [KERNEL32.@] Sets attributes for some cells in
343 * the console screen buffer
346 * hConsoleOutput [I] Handle to screen buffer
347 * attr [I] Pointer to buffer with write attributes
348 * length [I] Number of cells to write to
349 * coord [I] Coords of first cell
350 * lpNumAttrsWritten [O] Pointer to number of cells written
357 BOOL WINAPI WriteConsoleOutputAttribute( HANDLE hConsoleOutput, CONST WORD *attr, DWORD length,
358 COORD coord, LPDWORD lpNumAttrsWritten )
362 TRACE("(%p,%p,%ld,%dx%d,%p)\n", hConsoleOutput,attr,length,coord.X,coord.Y,lpNumAttrsWritten);
364 SERVER_START_REQ( write_console_output )
366 req->handle = console_handle_unmap(hConsoleOutput);
369 req->mode = CHAR_INFO_MODE_ATTR;
371 wine_server_add_data( req, attr, length * sizeof(WORD) );
372 if ((ret = !wine_server_call_err( req )))
374 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
382 /******************************************************************************
383 * FillConsoleOutputCharacterA [KERNEL32.@]
386 * hConsoleOutput [I] Handle to screen buffer
387 * ch [I] Character to write
388 * length [I] Number of cells to write to
389 * coord [I] Coords of first cell
390 * lpNumCharsWritten [O] Pointer to number of cells written
396 BOOL WINAPI FillConsoleOutputCharacterA( HANDLE hConsoleOutput, CHAR ch, DWORD length,
397 COORD coord, LPDWORD lpNumCharsWritten )
401 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch, 1, &wch, 1 );
402 return FillConsoleOutputCharacterW(hConsoleOutput, wch, length, coord, lpNumCharsWritten);
406 /******************************************************************************
407 * FillConsoleOutputCharacterW [KERNEL32.@] Writes characters to console
410 * hConsoleOutput [I] Handle to screen buffer
411 * ch [I] Character to write
412 * length [I] Number of cells to write to
413 * coord [I] Coords of first cell
414 * lpNumCharsWritten [O] Pointer to number of cells written
420 BOOL WINAPI FillConsoleOutputCharacterW( HANDLE hConsoleOutput, WCHAR ch, DWORD length,
421 COORD coord, LPDWORD lpNumCharsWritten)
425 TRACE("(%p,%s,%ld,(%dx%d),%p)\n",
426 hConsoleOutput, debugstr_wn(&ch, 1), length, coord.X, coord.Y, lpNumCharsWritten);
428 SERVER_START_REQ( fill_console_output )
430 req->handle = console_handle_unmap(hConsoleOutput);
433 req->mode = CHAR_INFO_MODE_TEXT;
437 if ((ret = !wine_server_call_err( req )))
439 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
447 /******************************************************************************
448 * FillConsoleOutputAttribute [KERNEL32.@] Sets attributes for console
451 * hConsoleOutput [I] Handle to screen buffer
452 * attr [I] Color attribute to write
453 * length [I] Number of cells to write to
454 * coord [I] Coords of first cell
455 * lpNumAttrsWritten [O] Pointer to number of cells written
461 BOOL WINAPI FillConsoleOutputAttribute( HANDLE hConsoleOutput, WORD attr, DWORD length,
462 COORD coord, LPDWORD lpNumAttrsWritten )
466 TRACE("(%p,%d,%ld,(%dx%d),%p)\n",
467 hConsoleOutput, attr, length, coord.X, coord.Y, lpNumAttrsWritten);
469 SERVER_START_REQ( fill_console_output )
471 req->handle = console_handle_unmap(hConsoleOutput);
474 req->mode = CHAR_INFO_MODE_ATTR;
476 req->data.attr = attr;
478 if ((ret = !wine_server_call_err( req )))
480 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
488 /******************************************************************************
489 * ReadConsoleOutputCharacterA [KERNEL32.@]
492 BOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput, LPSTR lpstr, DWORD count,
493 COORD coord, LPDWORD read_count)
497 LPWSTR wptr = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
499 if (read_count) *read_count = 0;
500 if (!wptr) return FALSE;
502 if ((ret = ReadConsoleOutputCharacterW( hConsoleOutput, wptr, count, coord, &read )))
504 read = WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr, read, lpstr, count, NULL, NULL);
505 if (read_count) *read_count = read;
507 HeapFree( GetProcessHeap(), 0, wptr );
512 /******************************************************************************
513 * ReadConsoleOutputCharacterW [KERNEL32.@]
516 BOOL WINAPI ReadConsoleOutputCharacterW( HANDLE hConsoleOutput, LPWSTR buffer, DWORD count,
517 COORD coord, LPDWORD read_count )
521 TRACE( "(%p,%p,%ld,%dx%d,%p)\n", hConsoleOutput, buffer, count, coord.X, coord.Y, read_count );
523 SERVER_START_REQ( read_console_output )
525 req->handle = console_handle_unmap(hConsoleOutput);
528 req->mode = CHAR_INFO_MODE_TEXT;
530 wine_server_set_reply( req, buffer, count * sizeof(WCHAR) );
531 if ((ret = !wine_server_call_err( req )))
533 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WCHAR);
541 /******************************************************************************
542 * ReadConsoleOutputAttribute [KERNEL32.@]
544 BOOL WINAPI ReadConsoleOutputAttribute(HANDLE hConsoleOutput, LPWORD lpAttribute, DWORD length,
545 COORD coord, LPDWORD read_count)
549 TRACE("(%p,%p,%ld,%dx%d,%p)\n",
550 hConsoleOutput, lpAttribute, length, coord.X, coord.Y, read_count);
552 SERVER_START_REQ( read_console_output )
554 req->handle = console_handle_unmap(hConsoleOutput);
557 req->mode = CHAR_INFO_MODE_ATTR;
559 wine_server_set_reply( req, lpAttribute, length * sizeof(WORD) );
560 if ((ret = !wine_server_call_err( req )))
562 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WORD);
570 /******************************************************************************
571 * ReadConsoleOutputA [KERNEL32.@]
574 BOOL WINAPI ReadConsoleOutputA( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
575 COORD coord, LPSMALL_RECT region )
580 ret = ReadConsoleOutputW( hConsoleOutput, lpBuffer, size, coord, region );
581 if (ret && region->Right >= region->Left)
583 for (y = 0; y <= region->Bottom - region->Top; y++)
585 char_info_WtoA( &lpBuffer[(coord.Y + y) * size.X + coord.X],
586 region->Right - region->Left + 1 );
593 /******************************************************************************
594 * ReadConsoleOutputW [KERNEL32.@]
596 * NOTE: The NT4 (sp5) kernel crashes on me if size is (0,0). I don't
597 * think we need to be *that* compatible. -- AJ
599 BOOL WINAPI ReadConsoleOutputW( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
600 COORD coord, LPSMALL_RECT region )
602 int width, height, y;
605 width = min( region->Right - region->Left + 1, size.X - coord.X );
606 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
608 if (width > 0 && height > 0)
610 for (y = 0; y < height; y++)
612 SERVER_START_REQ( read_console_output )
614 req->handle = console_handle_unmap(hConsoleOutput);
615 req->x = region->Left;
616 req->y = region->Top + y;
617 req->mode = CHAR_INFO_MODE_TEXTATTR;
619 wine_server_set_reply( req, &lpBuffer[(y+coord.Y) * size.X + coord.X],
620 width * sizeof(CHAR_INFO) );
621 if ((ret = !wine_server_call_err( req )))
623 width = min( width, reply->width - region->Left );
624 height = min( height, reply->height - region->Top );
631 region->Bottom = region->Top + height - 1;
632 region->Right = region->Left + width - 1;
637 /******************************************************************************
638 * ReadConsoleInputA [KERNEL32.@] Reads data from a console
641 * handle [I] Handle to console input buffer
642 * buffer [O] Address of buffer for read data
643 * count [I] Number of records to read
644 * pRead [O] Address of number of records read
650 BOOL WINAPI ReadConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
654 if (!ReadConsoleInputW( handle, buffer, count, &read )) return FALSE;
655 input_records_WtoA( buffer, read );
656 if (pRead) *pRead = read;
661 /***********************************************************************
662 * PeekConsoleInputA (KERNEL32.@)
664 * Gets 'count' first events (or less) from input queue.
666 BOOL WINAPI PeekConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
670 if (!PeekConsoleInputW( handle, buffer, count, &read )) return FALSE;
671 input_records_WtoA( buffer, read );
672 if (pRead) *pRead = read;
677 /***********************************************************************
678 * PeekConsoleInputW (KERNEL32.@)
680 BOOL WINAPI PeekConsoleInputW( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD read )
683 SERVER_START_REQ( read_console_input )
685 req->handle = console_handle_unmap(handle);
687 wine_server_set_reply( req, buffer, count * sizeof(INPUT_RECORD) );
688 if ((ret = !wine_server_call_err( req )))
690 if (read) *read = count ? reply->read : 0;
698 /***********************************************************************
699 * GetNumberOfConsoleInputEvents (KERNEL32.@)
701 BOOL WINAPI GetNumberOfConsoleInputEvents( HANDLE handle, LPDWORD nrofevents )
704 SERVER_START_REQ( read_console_input )
706 req->handle = console_handle_unmap(handle);
708 if ((ret = !wine_server_call_err( req )))
710 if (nrofevents) *nrofevents = reply->read;
718 /******************************************************************************
721 * Helper function for ReadConsole, ReadConsoleInput and FlushConsoleInputBuffer
724 * 0 for error, 1 for no INPUT_RECORD ready, 2 with INPUT_RECORD ready
726 enum read_console_input_return {rci_error = 0, rci_timeout = 1, rci_gotone = 2};
727 static enum read_console_input_return read_console_input(HANDLE handle, LPINPUT_RECORD ir, DWORD timeout)
729 enum read_console_input_return ret;
731 if (WaitForSingleObject(GetConsoleInputWaitHandle(), timeout) != WAIT_OBJECT_0)
733 SERVER_START_REQ( read_console_input )
735 req->handle = console_handle_unmap(handle);
737 wine_server_set_reply( req, ir, sizeof(INPUT_RECORD) );
738 if (wine_server_call_err( req ) || !reply->read) ret = rci_error;
739 else ret = rci_gotone;
747 /***********************************************************************
748 * FlushConsoleInputBuffer (KERNEL32.@)
750 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
752 enum read_console_input_return last;
755 while ((last = read_console_input(handle, &ir, 0)) == rci_gotone);
757 return last == rci_timeout;
761 /***********************************************************************
762 * SetConsoleTitleA (KERNEL32.@)
764 BOOL WINAPI SetConsoleTitleA( LPCSTR title )
769 DWORD len = MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, NULL, 0 );
770 if (!(titleW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
771 MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, titleW, len );
772 ret = SetConsoleTitleW(titleW);
773 HeapFree(GetProcessHeap(), 0, titleW);
778 /***********************************************************************
779 * GetConsoleTitleA (KERNEL32.@)
781 DWORD WINAPI GetConsoleTitleA(LPSTR title, DWORD size)
783 WCHAR *ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * size);
787 ret = GetConsoleTitleW( ptr, size );
790 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, ret + 1, title, size, NULL, NULL);
793 HeapFree(GetProcessHeap(), 0, ptr);
798 /******************************************************************************
799 * GetConsoleTitleW [KERNEL32.@] Retrieves title string for console
802 * title [O] Address of buffer for title
803 * size [I] Size of buffer
806 * Success: Length of string copied
809 DWORD WINAPI GetConsoleTitleW(LPWSTR title, DWORD size)
813 SERVER_START_REQ( get_console_input_info )
816 wine_server_set_reply( req, title, (size-1) * sizeof(WCHAR) );
817 if (!wine_server_call_err( req ))
819 ret = wine_server_reply_size(reply) / sizeof(WCHAR);
828 /***********************************************************************
829 * GetLargestConsoleWindowSize (KERNEL32.@)
832 * This should return a COORD, but calling convention for returning
833 * structures is different between Windows and gcc on i386.
838 #undef GetLargestConsoleWindowSize
839 DWORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
849 #endif /* defined(__i386__) */
852 /***********************************************************************
853 * GetLargestConsoleWindowSize (KERNEL32.@)
856 * This should return a COORD, but calling convention for returning
857 * structures is different between Windows and gcc on i386.
862 COORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
869 #endif /* defined(__i386__) */
871 static WCHAR* S_EditString /* = NULL */;
872 static unsigned S_EditStrPos /* = 0 */;
874 /***********************************************************************
875 * FreeConsole (KERNEL32.@)
877 BOOL WINAPI FreeConsole(VOID)
881 SERVER_START_REQ(free_console)
883 ret = !wine_server_call_err( req );
889 /******************************************************************
890 * start_console_renderer
892 * helper for AllocConsole
893 * starts the renderer process
895 static BOOL start_console_renderer_helper(const char* appname, STARTUPINFOA* si,
900 PROCESS_INFORMATION pi;
902 /* FIXME: use dynamic allocation for most of the buffers below */
903 ret = snprintf(buffer, sizeof(buffer), "%s --use-event=%d", appname, (INT)hEvent);
904 if ((ret > -1) && (ret < sizeof(buffer)) &&
905 CreateProcessA(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS,
906 NULL, NULL, si, &pi))
908 if (WaitForSingleObject(hEvent, INFINITE) != WAIT_OBJECT_0) return FALSE;
910 TRACE("Started wineconsole pid=%08lx tid=%08lx\n",
911 pi.dwProcessId, pi.dwThreadId);
918 static BOOL start_console_renderer(STARTUPINFOA* si)
922 OBJECT_ATTRIBUTES attr;
925 attr.Length = sizeof(attr);
926 attr.RootDirectory = 0;
927 attr.Attributes = OBJ_INHERIT;
928 attr.ObjectName = NULL;
929 attr.SecurityDescriptor = NULL;
930 attr.SecurityQualityOfService = NULL;
932 NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, &attr, TRUE, FALSE);
933 if (!hEvent) return FALSE;
935 /* first try environment variable */
936 if ((p = getenv("WINECONSOLE")) != NULL)
938 ret = start_console_renderer_helper(p, si, hEvent);
940 ERR("Couldn't launch Wine console from WINECONSOLE env var (%s)... "
941 "trying default access\n", p);
944 /* then try the regular PATH */
946 ret = start_console_renderer_helper("wineconsole", si, hEvent);
952 /***********************************************************************
953 * AllocConsole (KERNEL32.@)
955 * creates an xterm with a pty to our program
957 BOOL WINAPI AllocConsole(void)
959 HANDLE handle_in = INVALID_HANDLE_VALUE;
960 HANDLE handle_out = INVALID_HANDLE_VALUE;
961 HANDLE handle_err = INVALID_HANDLE_VALUE;
962 STARTUPINFOA siCurrent;
963 STARTUPINFOA siConsole;
965 SECURITY_ATTRIBUTES sa;
969 handle_in = CreateFileA( "CONIN$", GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
970 0, NULL, OPEN_EXISTING, 0, 0 );
972 if (handle_in != INVALID_HANDLE_VALUE)
974 /* we already have a console opened on this process, don't create a new one */
975 CloseHandle(handle_in);
979 GetStartupInfoA(&siCurrent);
981 memset(&siConsole, 0, sizeof(siConsole));
982 siConsole.cb = sizeof(siConsole);
983 /* setup a view arguments for wineconsole (it'll use them as default values) */
984 if (siCurrent.dwFlags & STARTF_USECOUNTCHARS)
986 siConsole.dwFlags |= STARTF_USECOUNTCHARS;
987 siConsole.dwXCountChars = siCurrent.dwXCountChars;
988 siConsole.dwYCountChars = siCurrent.dwYCountChars;
990 if (siCurrent.dwFlags & STARTF_USEFILLATTRIBUTE)
992 siConsole.dwFlags |= STARTF_USEFILLATTRIBUTE;
993 siConsole.dwFillAttribute = siCurrent.dwFillAttribute;
995 /* FIXME (should pass the unicode form) */
996 if (siCurrent.lpTitle)
997 siConsole.lpTitle = siCurrent.lpTitle;
998 else if (GetModuleFileNameA(0, buffer, sizeof(buffer)))
999 siConsole.lpTitle = buffer;
1001 if (!start_console_renderer(&siConsole))
1004 /* all std I/O handles are inheritable by default */
1005 sa.nLength = sizeof(sa);
1006 sa.lpSecurityDescriptor = NULL;
1007 sa.bInheritHandle = TRUE;
1009 handle_in = CreateFileA( "CONIN$", GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
1010 0, &sa, OPEN_EXISTING, 0, 0 );
1011 if (handle_in == INVALID_HANDLE_VALUE) goto the_end;
1013 handle_out = CreateFileA( "CONOUT$", GENERIC_READ|GENERIC_WRITE,
1014 0, &sa, OPEN_EXISTING, 0, 0 );
1015 if (handle_out == INVALID_HANDLE_VALUE) goto the_end;
1017 if (!DuplicateHandle(GetCurrentProcess(), handle_out, GetCurrentProcess(), &handle_err,
1018 0, TRUE, DUPLICATE_SAME_ACCESS))
1021 /* NT resets the STD_*_HANDLEs on console alloc */
1022 SetStdHandle(STD_INPUT_HANDLE, handle_in);
1023 SetStdHandle(STD_OUTPUT_HANDLE, handle_out);
1024 SetStdHandle(STD_ERROR_HANDLE, handle_err);
1026 SetLastError(ERROR_SUCCESS);
1031 ERR("Can't allocate console\n");
1032 if (handle_in != INVALID_HANDLE_VALUE) CloseHandle(handle_in);
1033 if (handle_out != INVALID_HANDLE_VALUE) CloseHandle(handle_out);
1034 if (handle_err != INVALID_HANDLE_VALUE) CloseHandle(handle_err);
1040 /***********************************************************************
1041 * ReadConsoleA (KERNEL32.@)
1043 BOOL WINAPI ReadConsoleA(HANDLE hConsoleInput, LPVOID lpBuffer, DWORD nNumberOfCharsToRead,
1044 LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1046 LPWSTR ptr = HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead * sizeof(WCHAR));
1050 if ((ret = ReadConsoleW(hConsoleInput, ptr, nNumberOfCharsToRead, &ncr, NULL)))
1051 ncr = WideCharToMultiByte(CP_ACP, 0, ptr, ncr, lpBuffer, nNumberOfCharsToRead, NULL, NULL);
1053 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = ncr;
1054 HeapFree(GetProcessHeap(), 0, ptr);
1059 /***********************************************************************
1060 * ReadConsoleW (KERNEL32.@)
1062 BOOL WINAPI ReadConsoleW(HANDLE hConsoleInput, LPVOID lpBuffer,
1063 DWORD nNumberOfCharsToRead, LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1066 LPWSTR xbuf = (LPWSTR)lpBuffer;
1069 TRACE("(%p,%p,%ld,%p,%p)\n",
1070 hConsoleInput, lpBuffer, nNumberOfCharsToRead, lpNumberOfCharsRead, lpReserved);
1072 if (!GetConsoleMode(hConsoleInput, &mode))
1075 if (mode & ENABLE_LINE_INPUT)
1077 if (!S_EditString || S_EditString[S_EditStrPos] == 0)
1079 if (S_EditString) HeapFree(GetProcessHeap(), 0, S_EditString);
1080 if (!(S_EditString = CONSOLE_Readline(hConsoleInput)))
1084 charsread = lstrlenW(&S_EditString[S_EditStrPos]);
1085 if (charsread > nNumberOfCharsToRead) charsread = nNumberOfCharsToRead;
1086 memcpy(xbuf, &S_EditString[S_EditStrPos], charsread * sizeof(WCHAR));
1087 S_EditStrPos += charsread;
1092 DWORD timeout = INFINITE;
1094 /* FIXME: should we read at least 1 char? The SDK does not say */
1095 /* wait for at least one available input record (it doesn't mean we'll have
1096 * chars stored in xbuf...)
1101 if (read_console_input(hConsoleInput, &ir, timeout) != rci_gotone) break;
1103 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown &&
1104 ir.Event.KeyEvent.uChar.UnicodeChar &&
1105 !(ir.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
1107 xbuf[charsread++] = ir.Event.KeyEvent.uChar.UnicodeChar;
1109 } while (charsread < nNumberOfCharsToRead);
1110 /* nothing has been read */
1111 if (timeout == INFINITE) return FALSE;
1114 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = charsread;
1120 /***********************************************************************
1121 * ReadConsoleInputW (KERNEL32.@)
1123 BOOL WINAPI ReadConsoleInputW(HANDLE hConsoleInput, LPINPUT_RECORD lpBuffer,
1124 DWORD nLength, LPDWORD lpNumberOfEventsRead)
1127 DWORD timeout = INFINITE;
1131 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = 0;
1135 /* loop until we get at least one event */
1136 while (read_console_input(hConsoleInput, &lpBuffer[idx], timeout) == rci_gotone &&
1140 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = idx;
1145 /******************************************************************************
1146 * WriteConsoleOutputCharacterW [KERNEL32.@] Copies character to consecutive
1147 * cells in the console screen buffer
1150 * hConsoleOutput [I] Handle to screen buffer
1151 * str [I] Pointer to buffer with chars to write
1152 * length [I] Number of cells to write to
1153 * coord [I] Coords of first cell
1154 * lpNumCharsWritten [O] Pointer to number of cells written
1161 BOOL WINAPI WriteConsoleOutputCharacterW( HANDLE hConsoleOutput, LPCWSTR str, DWORD length,
1162 COORD coord, LPDWORD lpNumCharsWritten )
1166 TRACE("(%p,%s,%ld,%dx%d,%p)\n", hConsoleOutput,
1167 debugstr_wn(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
1169 SERVER_START_REQ( write_console_output )
1171 req->handle = console_handle_unmap(hConsoleOutput);
1174 req->mode = CHAR_INFO_MODE_TEXT;
1176 wine_server_add_data( req, str, length * sizeof(WCHAR) );
1177 if ((ret = !wine_server_call_err( req )))
1179 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
1187 /******************************************************************************
1188 * SetConsoleTitleW [KERNEL32.@] Sets title bar string for console
1191 * title [I] Address of new title
1197 BOOL WINAPI SetConsoleTitleW(LPCWSTR title)
1201 SERVER_START_REQ( set_console_input_info )
1204 req->mask = SET_CONSOLE_INPUT_INFO_TITLE;
1205 wine_server_add_data( req, title, strlenW(title) * sizeof(WCHAR) );
1206 ret = !wine_server_call_err( req );
1213 /***********************************************************************
1214 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
1216 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1218 FIXME("(%p): stub\n", nrofbuttons);
1223 /******************************************************************************
1224 * SetConsoleInputExeNameW [KERNEL32.@]
1229 BOOL WINAPI SetConsoleInputExeNameW(LPCWSTR name)
1231 FIXME("(%s): stub!\n", debugstr_w(name));
1233 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1237 /******************************************************************************
1238 * SetConsoleInputExeNameA [KERNEL32.@]
1243 BOOL WINAPI SetConsoleInputExeNameA(LPCSTR name)
1245 int len = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
1246 LPWSTR xptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1249 if (!xptr) return FALSE;
1251 MultiByteToWideChar(CP_ACP, 0, name, -1, xptr, len);
1252 ret = SetConsoleInputExeNameW(xptr);
1253 HeapFree(GetProcessHeap(), 0, xptr);
1258 /******************************************************************
1259 * CONSOLE_DefaultHandler
1261 * Final control event handler
1263 static BOOL WINAPI CONSOLE_DefaultHandler(DWORD dwCtrlType)
1265 FIXME("Terminating process %lx on event %lx\n", GetCurrentProcessId(), dwCtrlType);
1267 /* should never go here */
1271 /******************************************************************************
1272 * SetConsoleCtrlHandler [KERNEL32.@] Adds function to calling process list
1275 * func [I] Address of handler function
1276 * add [I] Handler to add or remove
1283 * James Sutherland (JamesSutherland@gmx.de)
1284 * Added global variables console_ignore_ctrl_c and handlers[]
1285 * Does not yet do any error checking, or set LastError if failed.
1286 * This doesn't yet matter, since these handlers are not yet called...!
1289 struct ConsoleHandler {
1290 PHANDLER_ROUTINE handler;
1291 struct ConsoleHandler* next;
1294 static unsigned int CONSOLE_IgnoreCtrlC = 0; /* FIXME: this should be inherited somehow */
1295 static struct ConsoleHandler CONSOLE_DefaultConsoleHandler = {CONSOLE_DefaultHandler, NULL};
1296 static struct ConsoleHandler* CONSOLE_Handlers = &CONSOLE_DefaultConsoleHandler;
1298 static CRITICAL_SECTION CONSOLE_CritSect;
1299 static CRITICAL_SECTION_DEBUG critsect_debug =
1301 0, 0, &CONSOLE_CritSect,
1302 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
1303 0, 0, { 0, (DWORD)(__FILE__ ": CONSOLE_CritSect") }
1305 static CRITICAL_SECTION CONSOLE_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
1307 /*****************************************************************************/
1309 BOOL WINAPI SetConsoleCtrlHandler(PHANDLER_ROUTINE func, BOOL add)
1313 FIXME("(%p,%i) - no error checking or testing yet\n", func, add);
1317 CONSOLE_IgnoreCtrlC = add;
1321 struct ConsoleHandler* ch = HeapAlloc(GetProcessHeap(), 0, sizeof(struct ConsoleHandler));
1323 if (!ch) return FALSE;
1325 RtlEnterCriticalSection(&CONSOLE_CritSect);
1326 ch->next = CONSOLE_Handlers;
1327 CONSOLE_Handlers = ch;
1328 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1332 struct ConsoleHandler** ch;
1333 RtlEnterCriticalSection(&CONSOLE_CritSect);
1334 for (ch = &CONSOLE_Handlers; *ch; *ch = (*ch)->next)
1336 if ((*ch)->handler == func) break;
1340 struct ConsoleHandler* rch = *ch;
1343 if (rch == &CONSOLE_DefaultConsoleHandler)
1345 ERR("Who's trying to remove default handler???\n");
1352 HeapFree(GetProcessHeap(), 0, rch);
1357 WARN("Attempt to remove non-installed CtrlHandler %p\n", func);
1360 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1365 static WINE_EXCEPTION_FILTER(CONSOLE_CtrlEventHandler)
1367 TRACE("(%lx)\n", GetExceptionCode());
1368 return EXCEPTION_EXECUTE_HANDLER;
1371 static DWORD WINAPI CONSOLE_HandleCtrlCEntry(void* pmt)
1373 struct ConsoleHandler* ch;
1375 RtlEnterCriticalSection(&CONSOLE_CritSect);
1376 /* the debugger didn't continue... so, pass to ctrl handlers */
1377 for (ch = CONSOLE_Handlers; ch; ch = ch->next)
1379 if (ch->handler((DWORD)pmt)) break;
1381 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1385 /******************************************************************
1386 * CONSOLE_HandleCtrlC
1388 * Check whether the shall manipulate CtrlC events
1390 int CONSOLE_HandleCtrlC(unsigned sig)
1392 /* FIXME: better test whether a console is attached to this process ??? */
1393 extern unsigned CONSOLE_GetNumHistoryEntries(void);
1394 if (CONSOLE_GetNumHistoryEntries() == (unsigned)-1) return 0;
1395 if (CONSOLE_IgnoreCtrlC) return 1;
1397 /* try to pass the exception to the debugger
1398 * if it continues, there's nothing more to do
1399 * otherwise, we need to send the ctrl-event to the handlers
1403 RaiseException( DBG_CONTROL_C, 0, 0, NULL );
1405 __EXCEPT(CONSOLE_CtrlEventHandler)
1407 /* Create a separate thread to signal all the events. This would allow to
1408 * synchronize between setting the handlers and actually calling them
1410 CreateThread(NULL, 0, CONSOLE_HandleCtrlCEntry, (void*)CTRL_C_EVENT, 0, NULL);
1416 /******************************************************************************
1417 * GenerateConsoleCtrlEvent [KERNEL32.@] Simulate a CTRL-C or CTRL-BREAK
1420 * dwCtrlEvent [I] Type of event
1421 * dwProcessGroupID [I] Process group ID to send event to
1425 * Failure: False (and *should* [but doesn't] set LastError)
1427 BOOL WINAPI GenerateConsoleCtrlEvent(DWORD dwCtrlEvent,
1428 DWORD dwProcessGroupID)
1432 TRACE("(%ld, %ld)\n", dwCtrlEvent, dwProcessGroupID);
1434 if (dwCtrlEvent != CTRL_C_EVENT && dwCtrlEvent != CTRL_BREAK_EVENT)
1436 ERR("Invalid event %ld for PGID %ld\n", dwCtrlEvent, dwProcessGroupID);
1440 SERVER_START_REQ( send_console_signal )
1442 req->signal = dwCtrlEvent;
1443 req->group_id = dwProcessGroupID;
1444 ret = !wine_server_call_err( req );
1452 /******************************************************************************
1453 * CreateConsoleScreenBuffer [KERNEL32.@] Creates a console screen buffer
1456 * dwDesiredAccess [I] Access flag
1457 * dwShareMode [I] Buffer share mode
1458 * sa [I] Security attributes
1459 * dwFlags [I] Type of buffer to create
1460 * lpScreenBufferData [I] Reserved
1463 * Should call SetLastError
1466 * Success: Handle to new console screen buffer
1467 * Failure: INVALID_HANDLE_VALUE
1469 HANDLE WINAPI CreateConsoleScreenBuffer(DWORD dwDesiredAccess, DWORD dwShareMode,
1470 LPSECURITY_ATTRIBUTES sa, DWORD dwFlags,
1471 LPVOID lpScreenBufferData)
1473 HANDLE ret = INVALID_HANDLE_VALUE;
1475 TRACE("(%ld,%ld,%p,%ld,%p)\n",
1476 dwDesiredAccess, dwShareMode, sa, dwFlags, lpScreenBufferData);
1478 if (dwFlags != CONSOLE_TEXTMODE_BUFFER || lpScreenBufferData != NULL)
1480 SetLastError(ERROR_INVALID_PARAMETER);
1481 return INVALID_HANDLE_VALUE;
1484 SERVER_START_REQ(create_console_output)
1487 req->access = dwDesiredAccess;
1488 req->share = dwShareMode;
1489 req->inherit = (sa && sa->bInheritHandle);
1490 if (!wine_server_call_err( req )) ret = reply->handle_out;
1498 /***********************************************************************
1499 * GetConsoleScreenBufferInfo (KERNEL32.@)
1501 BOOL WINAPI GetConsoleScreenBufferInfo(HANDLE hConsoleOutput, LPCONSOLE_SCREEN_BUFFER_INFO csbi)
1505 SERVER_START_REQ(get_console_output_info)
1507 req->handle = console_handle_unmap(hConsoleOutput);
1508 if ((ret = !wine_server_call_err( req )))
1510 csbi->dwSize.X = reply->width;
1511 csbi->dwSize.Y = reply->height;
1512 csbi->dwCursorPosition.X = reply->cursor_x;
1513 csbi->dwCursorPosition.Y = reply->cursor_y;
1514 csbi->wAttributes = reply->attr;
1515 csbi->srWindow.Left = reply->win_left;
1516 csbi->srWindow.Right = reply->win_right;
1517 csbi->srWindow.Top = reply->win_top;
1518 csbi->srWindow.Bottom = reply->win_bottom;
1519 csbi->dwMaximumWindowSize.X = reply->max_width;
1520 csbi->dwMaximumWindowSize.Y = reply->max_height;
1529 /******************************************************************************
1530 * SetConsoleActiveScreenBuffer [KERNEL32.@] Sets buffer to current console
1536 BOOL WINAPI SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput)
1540 TRACE("(%p)\n", hConsoleOutput);
1542 SERVER_START_REQ( set_console_input_info )
1545 req->mask = SET_CONSOLE_INPUT_INFO_ACTIVE_SB;
1546 req->active_sb = hConsoleOutput;
1547 ret = !wine_server_call_err( req );
1554 /***********************************************************************
1555 * GetConsoleMode (KERNEL32.@)
1557 BOOL WINAPI GetConsoleMode(HANDLE hcon, LPDWORD mode)
1561 SERVER_START_REQ(get_console_mode)
1563 req->handle = console_handle_unmap(hcon);
1564 ret = !wine_server_call_err( req );
1565 if (ret && mode) *mode = reply->mode;
1572 /******************************************************************************
1573 * SetConsoleMode [KERNEL32.@] Sets input mode of console's input buffer
1576 * hcon [I] Handle to console input or screen buffer
1577 * mode [I] Input or output mode to set
1584 * ENABLE_PROCESSED_INPUT 0x01
1585 * ENABLE_LINE_INPUT 0x02
1586 * ENABLE_ECHO_INPUT 0x04
1587 * ENABLE_WINDOW_INPUT 0x08
1588 * ENABLE_MOUSE_INPUT 0x10
1590 BOOL WINAPI SetConsoleMode(HANDLE hcon, DWORD mode)
1594 SERVER_START_REQ(set_console_mode)
1596 req->handle = console_handle_unmap(hcon);
1598 ret = !wine_server_call_err( req );
1601 /* FIXME: when resetting a console input to editline mode, I think we should
1602 * empty the S_EditString buffer
1605 TRACE("(%p,%lx) retval == %d\n", hcon, mode, ret);
1611 /******************************************************************
1612 * CONSOLE_WriteChars
1614 * WriteConsoleOutput helper: hides server call semantics
1615 * writes a string at a given pos with standard attribute
1617 int CONSOLE_WriteChars(HANDLE hCon, LPCWSTR lpBuffer, int nc, COORD* pos)
1623 SERVER_START_REQ( write_console_output )
1625 req->handle = console_handle_unmap(hCon);
1628 req->mode = CHAR_INFO_MODE_TEXTSTDATTR;
1630 wine_server_add_data( req, lpBuffer, nc * sizeof(WCHAR) );
1631 if (!wine_server_call_err( req )) written = reply->written;
1635 if (written > 0) pos->X += written;
1639 /******************************************************************
1642 * WriteConsoleOutput helper: handles passing to next line (+scrolling if necessary)
1645 static int next_line(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi)
1651 csbi->dwCursorPosition.X = 0;
1652 csbi->dwCursorPosition.Y++;
1654 if (csbi->dwCursorPosition.Y < csbi->dwSize.Y) return 1;
1657 src.Bottom = csbi->dwSize.Y - 1;
1659 src.Right = csbi->dwSize.X - 1;
1664 ci.Attributes = csbi->wAttributes;
1665 ci.Char.UnicodeChar = ' ';
1667 csbi->dwCursorPosition.Y--;
1668 if (!ScrollConsoleScreenBufferW(hCon, &src, NULL, dst, &ci))
1673 /******************************************************************
1676 * WriteConsoleOutput helper: writes a block of non special characters
1677 * Block can spread on several lines, and wrapping, if needed, is
1681 static int write_block(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi,
1682 DWORD mode, LPWSTR ptr, int len)
1684 int blk; /* number of chars to write on current line */
1685 int done; /* number of chars already written */
1687 if (len <= 0) return 1;
1689 if (mode & ENABLE_WRAP_AT_EOL_OUTPUT) /* writes remaining on next line */
1691 for (done = 0; done < len; done += blk)
1693 blk = min(len - done, csbi->dwSize.X - csbi->dwCursorPosition.X);
1695 if (CONSOLE_WriteChars(hCon, ptr + done, blk, &csbi->dwCursorPosition) != blk)
1697 if (csbi->dwCursorPosition.X == csbi->dwSize.X && !next_line(hCon, csbi))
1703 int pos = csbi->dwCursorPosition.X;
1704 /* FIXME: we could reduce the number of loops
1705 * but, in most cases we wouldn't gain lots of time (it would only
1706 * happen if we're asked to overwrite more than twice the part of the line,
1709 for (blk = done = 0; done < len; done += blk)
1711 blk = min(len - done, csbi->dwSize.X - csbi->dwCursorPosition.X);
1713 csbi->dwCursorPosition.X = pos;
1714 if (CONSOLE_WriteChars(hCon, ptr + done, blk, &csbi->dwCursorPosition) != blk)
1722 /***********************************************************************
1723 * WriteConsoleW (KERNEL32.@)
1725 BOOL WINAPI WriteConsoleW(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
1726 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
1730 WCHAR* psz = (WCHAR*)lpBuffer;
1731 CONSOLE_SCREEN_BUFFER_INFO csbi;
1734 TRACE("%p %s %ld %p %p\n",
1735 hConsoleOutput, debugstr_wn(lpBuffer, nNumberOfCharsToWrite),
1736 nNumberOfCharsToWrite, lpNumberOfCharsWritten, lpReserved);
1738 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
1740 if (!GetConsoleMode(hConsoleOutput, &mode) ||
1741 !GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
1744 if (mode & ENABLE_PROCESSED_OUTPUT)
1748 for (i = 0; i < nNumberOfCharsToWrite; i++)
1752 case '\b': case '\t': case '\n': case '\a': case '\r':
1753 /* don't handle here the i-th char... done below */
1754 if ((k = i - first) > 0)
1756 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
1766 if (csbi.dwCursorPosition.X > 0) csbi.dwCursorPosition.X--;
1770 WCHAR tmp[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
1772 if (!write_block(hConsoleOutput, &csbi, mode, tmp,
1773 ((csbi.dwCursorPosition.X + 8) & ~7) - csbi.dwCursorPosition.X))
1778 next_line(hConsoleOutput, &csbi);
1784 csbi.dwCursorPosition.X = 0;
1792 /* write the remaining block (if any) if processed output is enabled, or the
1793 * entire buffer otherwise
1795 if ((k = nNumberOfCharsToWrite - first) > 0)
1797 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
1803 SetConsoleCursorPosition(hConsoleOutput, csbi.dwCursorPosition);
1804 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = nw;
1809 /***********************************************************************
1810 * WriteConsoleA (KERNEL32.@)
1812 BOOL WINAPI WriteConsoleA(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
1813 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
1819 n = MultiByteToWideChar(CP_ACP, 0, lpBuffer, nNumberOfCharsToWrite, NULL, 0);
1821 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
1822 xstring = HeapAlloc(GetProcessHeap(), 0, n * sizeof(WCHAR));
1823 if (!xstring) return 0;
1825 MultiByteToWideChar(CP_ACP, 0, lpBuffer, nNumberOfCharsToWrite, xstring, n);
1827 ret = WriteConsoleW(hConsoleOutput, xstring, n, lpNumberOfCharsWritten, 0);
1829 HeapFree(GetProcessHeap(), 0, xstring);
1834 /******************************************************************************
1835 * SetConsoleCursorPosition [KERNEL32.@]
1836 * Sets the cursor position in console
1839 * hConsoleOutput [I] Handle of console screen buffer
1840 * dwCursorPosition [I] New cursor position coordinates
1844 BOOL WINAPI SetConsoleCursorPosition(HANDLE hcon, COORD pos)
1847 CONSOLE_SCREEN_BUFFER_INFO csbi;
1851 TRACE("%p %d %d\n", hcon, pos.X, pos.Y);
1853 SERVER_START_REQ(set_console_output_info)
1855 req->handle = console_handle_unmap(hcon);
1856 req->cursor_x = pos.X;
1857 req->cursor_y = pos.Y;
1858 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_POS;
1859 ret = !wine_server_call_err( req );
1863 if (!ret || !GetConsoleScreenBufferInfo(hcon, &csbi))
1866 /* if cursor is no longer visible, scroll the visible window... */
1867 w = csbi.srWindow.Right - csbi.srWindow.Left + 1;
1868 h = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
1869 if (pos.X < csbi.srWindow.Left)
1871 csbi.srWindow.Left = min(pos.X, csbi.dwSize.X - w);
1874 else if (pos.X > csbi.srWindow.Right)
1876 csbi.srWindow.Left = max(pos.X, w) - w + 1;
1879 csbi.srWindow.Right = csbi.srWindow.Left + w - 1;
1881 if (pos.Y < csbi.srWindow.Top)
1883 csbi.srWindow.Top = min(pos.Y, csbi.dwSize.Y - h);
1886 else if (pos.Y > csbi.srWindow.Bottom)
1888 csbi.srWindow.Top = max(pos.Y, h) - h + 1;
1891 csbi.srWindow.Bottom = csbi.srWindow.Top + h - 1;
1893 ret = (do_move) ? SetConsoleWindowInfo(hcon, TRUE, &csbi.srWindow) : TRUE;
1898 /******************************************************************************
1899 * GetConsoleCursorInfo [KERNEL32.@] Gets size and visibility of console
1902 * hcon [I] Handle to console screen buffer
1903 * cinfo [O] Address of cursor information
1909 BOOL WINAPI GetConsoleCursorInfo(HANDLE hcon, LPCONSOLE_CURSOR_INFO cinfo)
1913 SERVER_START_REQ(get_console_output_info)
1915 req->handle = console_handle_unmap(hcon);
1916 ret = !wine_server_call_err( req );
1919 cinfo->dwSize = reply->cursor_size;
1920 cinfo->bVisible = reply->cursor_visible;
1928 /******************************************************************************
1929 * SetConsoleCursorInfo [KERNEL32.@] Sets size and visibility of cursor
1932 * hcon [I] Handle to console screen buffer
1933 * cinfo [I] Address of cursor information
1938 BOOL WINAPI SetConsoleCursorInfo(HANDLE hCon, LPCONSOLE_CURSOR_INFO cinfo)
1942 SERVER_START_REQ(set_console_output_info)
1944 req->handle = console_handle_unmap(hCon);
1945 req->cursor_size = cinfo->dwSize;
1946 req->cursor_visible = cinfo->bVisible;
1947 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM;
1948 ret = !wine_server_call_err( req );
1955 /******************************************************************************
1956 * SetConsoleWindowInfo [KERNEL32.@] Sets size and position of console
1959 * hcon [I] Handle to console screen buffer
1960 * bAbsolute [I] Coordinate type flag
1961 * window [I] Address of new window rectangle
1966 BOOL WINAPI SetConsoleWindowInfo(HANDLE hCon, BOOL bAbsolute, LPSMALL_RECT window)
1968 SMALL_RECT p = *window;
1973 CONSOLE_SCREEN_BUFFER_INFO csbi;
1974 if (!GetConsoleScreenBufferInfo(hCon, &csbi))
1976 p.Left += csbi.srWindow.Left;
1977 p.Top += csbi.srWindow.Top;
1978 p.Right += csbi.srWindow.Left;
1979 p.Bottom += csbi.srWindow.Top;
1981 SERVER_START_REQ(set_console_output_info)
1983 req->handle = console_handle_unmap(hCon);
1984 req->win_left = p.Left;
1985 req->win_top = p.Top;
1986 req->win_right = p.Right;
1987 req->win_bottom = p.Bottom;
1988 req->mask = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
1989 ret = !wine_server_call_err( req );
1997 /******************************************************************************
1998 * SetConsoleTextAttribute [KERNEL32.@] Sets colors for text
2000 * Sets the foreground and background color attributes of characters
2001 * written to the screen buffer.
2007 BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttr)
2011 SERVER_START_REQ(set_console_output_info)
2013 req->handle = console_handle_unmap(hConsoleOutput);
2015 req->mask = SET_CONSOLE_OUTPUT_INFO_ATTR;
2016 ret = !wine_server_call_err( req );
2023 /******************************************************************************
2024 * SetConsoleScreenBufferSize [KERNEL32.@] Changes size of console
2027 * hConsoleOutput [I] Handle to console screen buffer
2028 * dwSize [I] New size in character rows and cols
2034 BOOL WINAPI SetConsoleScreenBufferSize(HANDLE hConsoleOutput, COORD dwSize)
2038 SERVER_START_REQ(set_console_output_info)
2040 req->handle = console_handle_unmap(hConsoleOutput);
2041 req->width = dwSize.X;
2042 req->height = dwSize.Y;
2043 req->mask = SET_CONSOLE_OUTPUT_INFO_SIZE;
2044 ret = !wine_server_call_err( req );
2051 /******************************************************************************
2052 * ScrollConsoleScreenBufferA [KERNEL32.@]
2055 BOOL WINAPI ScrollConsoleScreenBufferA(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2056 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2061 ciw.Attributes = lpFill->Attributes;
2062 MultiByteToWideChar(CP_ACP, 0, &lpFill->Char.AsciiChar, 1, &ciw.Char.UnicodeChar, 1);
2064 return ScrollConsoleScreenBufferW(hConsoleOutput, lpScrollRect, lpClipRect,
2065 dwDestOrigin, &ciw);
2068 /******************************************************************
2069 * CONSOLE_FillLineUniform
2071 * Helper function for ScrollConsoleScreenBufferW
2072 * Fills a part of a line with a constant character info
2074 void CONSOLE_FillLineUniform(HANDLE hConsoleOutput, int i, int j, int len, LPCHAR_INFO lpFill)
2076 SERVER_START_REQ( fill_console_output )
2078 req->handle = console_handle_unmap(hConsoleOutput);
2079 req->mode = CHAR_INFO_MODE_TEXTATTR;
2084 req->data.ch = lpFill->Char.UnicodeChar;
2085 req->data.attr = lpFill->Attributes;
2086 wine_server_call_err( req );
2091 /******************************************************************************
2092 * ScrollConsoleScreenBufferW [KERNEL32.@]
2096 BOOL WINAPI ScrollConsoleScreenBufferW(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2097 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2105 CONSOLE_SCREEN_BUFFER_INFO csbi;
2109 TRACE("(%p,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", hConsoleOutput,
2110 lpScrollRect->Left, lpScrollRect->Top,
2111 lpScrollRect->Right, lpScrollRect->Bottom,
2112 lpClipRect->Left, lpClipRect->Top,
2113 lpClipRect->Right, lpClipRect->Bottom,
2114 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2116 TRACE("(%p,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", hConsoleOutput,
2117 lpScrollRect->Left, lpScrollRect->Top,
2118 lpScrollRect->Right, lpScrollRect->Bottom,
2119 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2121 if (!GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
2124 /* step 1: get dst rect */
2125 dst.Left = dwDestOrigin.X;
2126 dst.Top = dwDestOrigin.Y;
2127 dst.Right = dst.Left + (lpScrollRect->Right - lpScrollRect->Left);
2128 dst.Bottom = dst.Top + (lpScrollRect->Bottom - lpScrollRect->Top);
2130 /* step 2a: compute the final clip rect (optional passed clip and screen buffer limits */
2133 clip.Left = max(0, lpClipRect->Left);
2134 clip.Right = min(csbi.dwSize.X - 1, lpClipRect->Right);
2135 clip.Top = max(0, lpClipRect->Top);
2136 clip.Bottom = min(csbi.dwSize.Y - 1, lpClipRect->Bottom);
2141 clip.Right = csbi.dwSize.X - 1;
2143 clip.Bottom = csbi.dwSize.Y - 1;
2145 if (clip.Left > clip.Right || clip.Top > clip.Bottom) return FALSE;
2147 /* step 2b: clip dst rect */
2148 if (dst.Left < clip.Left ) dst.Left = clip.Left;
2149 if (dst.Top < clip.Top ) dst.Top = clip.Top;
2150 if (dst.Right > clip.Right ) dst.Right = clip.Right;
2151 if (dst.Bottom > clip.Bottom) dst.Bottom = clip.Bottom;
2153 /* step 3: transfer the bits */
2154 SERVER_START_REQ(move_console_output)
2156 req->handle = console_handle_unmap(hConsoleOutput);
2157 req->x_src = lpScrollRect->Left;
2158 req->y_src = lpScrollRect->Top;
2159 req->x_dst = dst.Left;
2160 req->y_dst = dst.Top;
2161 req->w = dst.Right - dst.Left + 1;
2162 req->h = dst.Bottom - dst.Top + 1;
2163 ret = !wine_server_call_err( req );
2167 if (!ret) return FALSE;
2169 /* step 4: clean out the exposed part */
2171 /* have to write cell [i,j] if it is not in dst rect (because it has already
2172 * been written to by the scroll) and is in clip (we shall not write
2175 for (j = max(lpScrollRect->Top, clip.Top); j <= min(lpScrollRect->Bottom, clip.Bottom); j++)
2177 inside = dst.Top <= j && j <= dst.Bottom;
2179 for (i = max(lpScrollRect->Left, clip.Left); i <= min(lpScrollRect->Right, clip.Right); i++)
2181 if (inside && dst.Left <= i && i <= dst.Right)
2185 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2191 if (start == -1) start = i;
2195 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2202 /* ====================================================================
2204 * Console manipulation functions
2206 * ====================================================================*/
2208 /* some missing functions...
2209 * FIXME: those are likely to be defined as undocumented function in kernel32 (or part of them)
2210 * should get the right API and implement them
2211 * GetConsoleCommandHistory[AW] (dword dword dword)
2212 * GetConsoleCommandHistoryLength[AW]
2213 * SetConsoleCommandHistoryMode
2214 * SetConsoleNumberOfCommands[AW]
2216 int CONSOLE_GetHistory(int idx, WCHAR* buf, int buf_len)
2220 SERVER_START_REQ( get_console_input_history )
2224 if (buf && buf_len > 1)
2226 wine_server_set_reply( req, buf, (buf_len - 1) * sizeof(WCHAR) );
2228 if (!wine_server_call_err( req ))
2230 if (buf) buf[wine_server_reply_size(reply) / sizeof(WCHAR)] = 0;
2231 len = reply->total / sizeof(WCHAR) + 1;
2238 /******************************************************************
2239 * CONSOLE_AppendHistory
2243 BOOL CONSOLE_AppendHistory(const WCHAR* ptr)
2245 size_t len = strlenW(ptr);
2248 while (len && (ptr[len - 1] == '\n' || ptr[len - 1] == '\r')) len--;
2250 SERVER_START_REQ( append_console_input_history )
2253 wine_server_add_data( req, ptr, len * sizeof(WCHAR) );
2254 ret = !wine_server_call_err( req );
2260 /******************************************************************
2261 * CONSOLE_GetNumHistoryEntries
2265 unsigned CONSOLE_GetNumHistoryEntries(void)
2268 SERVER_START_REQ(get_console_input_info)
2271 if (!wine_server_call_err( req )) ret = reply->history_index;
2277 /******************************************************************
2278 * CONSOLE_GetEditionMode
2282 BOOL CONSOLE_GetEditionMode(HANDLE hConIn, int* mode)
2284 unsigned ret = FALSE;
2285 SERVER_START_REQ(get_console_input_info)
2287 req->handle = console_handle_unmap(hConIn);
2288 if ((ret = !wine_server_call_err( req )))
2289 *mode = reply->edition_mode;