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 /******************************************************************************
172 * WriteConsoleInputA [KERNEL32.@]
174 BOOL WINAPI WriteConsoleInputA( HANDLE handle, const INPUT_RECORD *buffer,
175 DWORD count, LPDWORD written )
180 if (!(recW = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*recW) ))) return FALSE;
181 memcpy( recW, buffer, count*sizeof(*recW) );
182 input_records_AtoW( recW, count );
183 ret = WriteConsoleInputW( handle, recW, count, written );
184 HeapFree( GetProcessHeap(), 0, recW );
189 /******************************************************************************
190 * WriteConsoleInputW [KERNEL32.@]
192 BOOL WINAPI WriteConsoleInputW( HANDLE handle, const INPUT_RECORD *buffer,
193 DWORD count, LPDWORD written )
197 TRACE("(%p,%p,%ld,%p)\n", handle, buffer, count, written);
199 if (written) *written = 0;
200 SERVER_START_REQ( write_console_input )
202 req->handle = console_handle_unmap(handle);
203 wine_server_add_data( req, buffer, count * sizeof(INPUT_RECORD) );
204 if ((ret = !wine_server_call_err( req )) && written)
205 *written = reply->written;
213 /***********************************************************************
214 * WriteConsoleOutputA (KERNEL32.@)
216 BOOL WINAPI WriteConsoleOutputA( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
217 COORD size, COORD coord, LPSMALL_RECT region )
221 COORD new_size, new_coord;
224 new_size.X = min( region->Right - region->Left + 1, size.X - coord.X );
225 new_size.Y = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
227 if (new_size.X <= 0 || new_size.Y <= 0)
229 region->Bottom = region->Top + new_size.Y - 1;
230 region->Right = region->Left + new_size.X - 1;
234 /* only copy the useful rectangle */
235 if (!(ciw = HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO) * new_size.X * new_size.Y )))
237 for (y = 0; y < new_size.Y; y++)
239 memcpy( &ciw[y * new_size.X], &lpBuffer[(y + coord.Y) * size.X + coord.X],
240 new_size.X * sizeof(CHAR_INFO) );
241 char_info_AtoW( ciw, new_size.X );
243 new_coord.X = new_coord.Y = 0;
244 ret = WriteConsoleOutputW( hConsoleOutput, ciw, new_size, new_coord, region );
245 if (ciw) HeapFree( GetProcessHeap(), 0, ciw );
250 /***********************************************************************
251 * WriteConsoleOutputW (KERNEL32.@)
253 BOOL WINAPI WriteConsoleOutputW( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
254 COORD size, COORD coord, LPSMALL_RECT region )
256 int width, height, y;
259 TRACE("(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n",
260 hConsoleOutput, lpBuffer, size.X, size.Y, coord.X, coord.Y,
261 region->Left, region->Top, region->Right, region->Bottom);
263 width = min( region->Right - region->Left + 1, size.X - coord.X );
264 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
266 if (width > 0 && height > 0)
268 for (y = 0; y < height; y++)
270 SERVER_START_REQ( write_console_output )
272 req->handle = console_handle_unmap(hConsoleOutput);
273 req->x = region->Left;
274 req->y = region->Top + y;
275 req->mode = CHAR_INFO_MODE_TEXTATTR;
277 wine_server_add_data( req, &lpBuffer[(y + coord.Y) * size.X + coord.X],
278 width * sizeof(CHAR_INFO));
279 if ((ret = !wine_server_call_err( req )))
281 width = min( width, reply->width - region->Left );
282 height = min( height, reply->height - region->Top );
289 region->Bottom = region->Top + height - 1;
290 region->Right = region->Left + width - 1;
295 /******************************************************************************
296 * WriteConsoleOutputCharacterA [KERNEL32.@] Copies character to consecutive
297 * cells in the console screen buffer
300 * hConsoleOutput [I] Handle to screen buffer
301 * str [I] Pointer to buffer with chars to write
302 * length [I] Number of cells to write to
303 * coord [I] Coords of first cell
304 * lpNumCharsWritten [O] Pointer to number of cells written
306 BOOL WINAPI WriteConsoleOutputCharacterA( HANDLE hConsoleOutput, LPCSTR str, DWORD length,
307 COORD coord, LPDWORD lpNumCharsWritten )
313 TRACE("(%p,%s,%ld,%dx%d,%p)\n", hConsoleOutput,
314 debugstr_an(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
316 lenW = MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, NULL, 0 );
318 if (lpNumCharsWritten) *lpNumCharsWritten = 0;
320 if (!(strW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) ))) return FALSE;
321 MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, strW, lenW );
323 ret = WriteConsoleOutputCharacterW( hConsoleOutput, strW, lenW, coord, lpNumCharsWritten );
324 HeapFree( GetProcessHeap(), 0, strW );
329 /******************************************************************************
330 * WriteConsoleOutputAttribute [KERNEL32.@] Sets attributes for some cells in
331 * the console screen buffer
334 * hConsoleOutput [I] Handle to screen buffer
335 * attr [I] Pointer to buffer with write attributes
336 * length [I] Number of cells to write to
337 * coord [I] Coords of first cell
338 * lpNumAttrsWritten [O] Pointer to number of cells written
345 BOOL WINAPI WriteConsoleOutputAttribute( HANDLE hConsoleOutput, CONST WORD *attr, DWORD length,
346 COORD coord, LPDWORD lpNumAttrsWritten )
350 TRACE("(%p,%p,%ld,%dx%d,%p)\n", hConsoleOutput,attr,length,coord.X,coord.Y,lpNumAttrsWritten);
352 SERVER_START_REQ( write_console_output )
354 req->handle = console_handle_unmap(hConsoleOutput);
357 req->mode = CHAR_INFO_MODE_ATTR;
359 wine_server_add_data( req, attr, length * sizeof(WORD) );
360 if ((ret = !wine_server_call_err( req )))
362 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
370 /******************************************************************************
371 * FillConsoleOutputCharacterA [KERNEL32.@]
374 * hConsoleOutput [I] Handle to screen buffer
375 * ch [I] Character to write
376 * length [I] Number of cells to write to
377 * coord [I] Coords of first cell
378 * lpNumCharsWritten [O] Pointer to number of cells written
384 BOOL WINAPI FillConsoleOutputCharacterA( HANDLE hConsoleOutput, CHAR ch, DWORD length,
385 COORD coord, LPDWORD lpNumCharsWritten )
389 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch, 1, &wch, 1 );
390 return FillConsoleOutputCharacterW(hConsoleOutput, wch, length, coord, lpNumCharsWritten);
394 /******************************************************************************
395 * FillConsoleOutputCharacterW [KERNEL32.@] Writes characters to console
398 * hConsoleOutput [I] Handle to screen buffer
399 * ch [I] Character to write
400 * length [I] Number of cells to write to
401 * coord [I] Coords of first cell
402 * lpNumCharsWritten [O] Pointer to number of cells written
408 BOOL WINAPI FillConsoleOutputCharacterW( HANDLE hConsoleOutput, WCHAR ch, DWORD length,
409 COORD coord, LPDWORD lpNumCharsWritten)
413 TRACE("(%p,%s,%ld,(%dx%d),%p)\n",
414 hConsoleOutput, debugstr_wn(&ch, 1), length, coord.X, coord.Y, lpNumCharsWritten);
416 SERVER_START_REQ( fill_console_output )
418 req->handle = console_handle_unmap(hConsoleOutput);
421 req->mode = CHAR_INFO_MODE_TEXT;
425 if ((ret = !wine_server_call_err( req )))
427 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
435 /******************************************************************************
436 * FillConsoleOutputAttribute [KERNEL32.@] Sets attributes for console
439 * hConsoleOutput [I] Handle to screen buffer
440 * attr [I] Color attribute to write
441 * length [I] Number of cells to write to
442 * coord [I] Coords of first cell
443 * lpNumAttrsWritten [O] Pointer to number of cells written
449 BOOL WINAPI FillConsoleOutputAttribute( HANDLE hConsoleOutput, WORD attr, DWORD length,
450 COORD coord, LPDWORD lpNumAttrsWritten )
454 TRACE("(%p,%d,%ld,(%dx%d),%p)\n",
455 hConsoleOutput, attr, length, coord.X, coord.Y, lpNumAttrsWritten);
457 SERVER_START_REQ( fill_console_output )
459 req->handle = console_handle_unmap(hConsoleOutput);
462 req->mode = CHAR_INFO_MODE_ATTR;
464 req->data.attr = attr;
466 if ((ret = !wine_server_call_err( req )))
468 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
476 /******************************************************************************
477 * ReadConsoleOutputCharacterA [KERNEL32.@]
480 BOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput, LPSTR lpstr, DWORD count,
481 COORD coord, LPDWORD read_count)
485 LPWSTR wptr = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
487 if (read_count) *read_count = 0;
488 if (!wptr) return FALSE;
490 if ((ret = ReadConsoleOutputCharacterW( hConsoleOutput, wptr, count, coord, &read )))
492 read = WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr, read, lpstr, count, NULL, NULL);
493 if (read_count) *read_count = read;
495 HeapFree( GetProcessHeap(), 0, wptr );
500 /******************************************************************************
501 * ReadConsoleOutputCharacterW [KERNEL32.@]
504 BOOL WINAPI ReadConsoleOutputCharacterW( HANDLE hConsoleOutput, LPWSTR buffer, DWORD count,
505 COORD coord, LPDWORD read_count )
509 TRACE( "(%p,%p,%ld,%dx%d,%p)\n", hConsoleOutput, buffer, count, coord.X, coord.Y, read_count );
511 SERVER_START_REQ( read_console_output )
513 req->handle = console_handle_unmap(hConsoleOutput);
516 req->mode = CHAR_INFO_MODE_TEXT;
518 wine_server_set_reply( req, buffer, count * sizeof(WCHAR) );
519 if ((ret = !wine_server_call_err( req )))
521 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WCHAR);
529 /******************************************************************************
530 * ReadConsoleOutputAttribute [KERNEL32.@]
532 BOOL WINAPI ReadConsoleOutputAttribute(HANDLE hConsoleOutput, LPWORD lpAttribute, DWORD length,
533 COORD coord, LPDWORD read_count)
537 TRACE("(%p,%p,%ld,%dx%d,%p)\n",
538 hConsoleOutput, lpAttribute, length, coord.X, coord.Y, read_count);
540 SERVER_START_REQ( read_console_output )
542 req->handle = console_handle_unmap(hConsoleOutput);
545 req->mode = CHAR_INFO_MODE_ATTR;
547 wine_server_set_reply( req, lpAttribute, length * sizeof(WORD) );
548 if ((ret = !wine_server_call_err( req )))
550 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WORD);
558 /******************************************************************************
559 * ReadConsoleOutputA [KERNEL32.@]
562 BOOL WINAPI ReadConsoleOutputA( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
563 COORD coord, LPSMALL_RECT region )
568 ret = ReadConsoleOutputW( hConsoleOutput, lpBuffer, size, coord, region );
569 if (ret && region->Right >= region->Left)
571 for (y = 0; y <= region->Bottom - region->Top; y++)
573 char_info_WtoA( &lpBuffer[(coord.Y + y) * size.X + coord.X],
574 region->Right - region->Left + 1 );
581 /******************************************************************************
582 * ReadConsoleOutputW [KERNEL32.@]
584 * NOTE: The NT4 (sp5) kernel crashes on me if size is (0,0). I don't
585 * think we need to be *that* compatible. -- AJ
587 BOOL WINAPI ReadConsoleOutputW( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
588 COORD coord, LPSMALL_RECT region )
590 int width, height, y;
593 width = min( region->Right - region->Left + 1, size.X - coord.X );
594 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
596 if (width > 0 && height > 0)
598 for (y = 0; y < height; y++)
600 SERVER_START_REQ( read_console_output )
602 req->handle = console_handle_unmap(hConsoleOutput);
603 req->x = region->Left;
604 req->y = region->Top + y;
605 req->mode = CHAR_INFO_MODE_TEXTATTR;
607 wine_server_set_reply( req, &lpBuffer[(y+coord.Y) * size.X + coord.X],
608 width * sizeof(CHAR_INFO) );
609 if ((ret = !wine_server_call_err( req )))
611 width = min( width, reply->width - region->Left );
612 height = min( height, reply->height - region->Top );
619 region->Bottom = region->Top + height - 1;
620 region->Right = region->Left + width - 1;
625 /******************************************************************************
626 * ReadConsoleInputA [KERNEL32.@] Reads data from a console
629 * handle [I] Handle to console input buffer
630 * buffer [O] Address of buffer for read data
631 * count [I] Number of records to read
632 * pRead [O] Address of number of records read
638 BOOL WINAPI ReadConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
642 if (!ReadConsoleInputW( handle, buffer, count, &read )) return FALSE;
643 input_records_WtoA( buffer, read );
644 if (pRead) *pRead = read;
649 /***********************************************************************
650 * PeekConsoleInputA (KERNEL32.@)
652 * Gets 'count' first events (or less) from input queue.
654 BOOL WINAPI PeekConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
658 if (!PeekConsoleInputW( handle, buffer, count, &read )) return FALSE;
659 input_records_WtoA( buffer, read );
660 if (pRead) *pRead = read;
665 /***********************************************************************
666 * PeekConsoleInputW (KERNEL32.@)
668 BOOL WINAPI PeekConsoleInputW( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD read )
671 SERVER_START_REQ( read_console_input )
673 req->handle = console_handle_unmap(handle);
675 wine_server_set_reply( req, buffer, count * sizeof(INPUT_RECORD) );
676 if ((ret = !wine_server_call_err( req )))
678 if (read) *read = count ? reply->read : 0;
686 /***********************************************************************
687 * GetNumberOfConsoleInputEvents (KERNEL32.@)
689 BOOL WINAPI GetNumberOfConsoleInputEvents( HANDLE handle, LPDWORD nrofevents )
692 SERVER_START_REQ( read_console_input )
694 req->handle = console_handle_unmap(handle);
696 if ((ret = !wine_server_call_err( req )))
698 if (nrofevents) *nrofevents = reply->read;
706 /******************************************************************************
709 * Helper function for ReadConsole, ReadConsoleInput and FlushConsoleInputBuffer
712 * 0 for error, 1 for no INPUT_RECORD ready, 2 with INPUT_RECORD ready
714 enum read_console_input_return {rci_error = 0, rci_timeout = 1, rci_gotone = 2};
715 static enum read_console_input_return read_console_input(HANDLE handle, LPINPUT_RECORD ir, DWORD timeout)
717 enum read_console_input_return ret;
719 if (WaitForSingleObject(GetConsoleInputWaitHandle(), timeout) != WAIT_OBJECT_0)
721 SERVER_START_REQ( read_console_input )
723 req->handle = console_handle_unmap(handle);
725 wine_server_set_reply( req, ir, sizeof(INPUT_RECORD) );
726 if (wine_server_call_err( req ) || !reply->read) ret = rci_error;
727 else ret = rci_gotone;
735 /***********************************************************************
736 * FlushConsoleInputBuffer (KERNEL32.@)
738 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
740 enum read_console_input_return last;
743 while ((last = read_console_input(handle, &ir, 0)) == rci_gotone);
745 return last == rci_timeout;
749 /***********************************************************************
750 * SetConsoleTitleA (KERNEL32.@)
752 BOOL WINAPI SetConsoleTitleA( LPCSTR title )
757 DWORD len = MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, NULL, 0 );
758 if (!(titleW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
759 MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, titleW, len );
760 ret = SetConsoleTitleW(titleW);
761 HeapFree(GetProcessHeap(), 0, titleW);
766 /***********************************************************************
767 * GetConsoleTitleA (KERNEL32.@)
769 DWORD WINAPI GetConsoleTitleA(LPSTR title, DWORD size)
771 WCHAR *ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * size);
775 ret = GetConsoleTitleW( ptr, size );
778 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, ret + 1, title, size, NULL, NULL);
781 HeapFree(GetProcessHeap(), 0, ptr);
786 /******************************************************************************
787 * GetConsoleTitleW [KERNEL32.@] Retrieves title string for console
790 * title [O] Address of buffer for title
791 * size [I] Size of buffer
794 * Success: Length of string copied
797 DWORD WINAPI GetConsoleTitleW(LPWSTR title, DWORD size)
801 SERVER_START_REQ( get_console_input_info )
804 wine_server_set_reply( req, title, (size-1) * sizeof(WCHAR) );
805 if (!wine_server_call_err( req ))
807 ret = wine_server_reply_size(reply) / sizeof(WCHAR);
816 /***********************************************************************
817 * GetLargestConsoleWindowSize (KERNEL32.@)
820 * This should return a COORD, but calling convention for returning
821 * structures is different between Windows and gcc on i386.
826 #undef GetLargestConsoleWindowSize
827 DWORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
837 #endif /* defined(__i386__) */
840 /***********************************************************************
841 * GetLargestConsoleWindowSize (KERNEL32.@)
844 * This should return a COORD, but calling convention for returning
845 * structures is different between Windows and gcc on i386.
850 COORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
857 #endif /* defined(__i386__) */
859 static WCHAR* S_EditString /* = NULL */;
860 static unsigned S_EditStrPos /* = 0 */;
862 /***********************************************************************
863 * FreeConsole (KERNEL32.@)
865 BOOL WINAPI FreeConsole(VOID)
869 SERVER_START_REQ(free_console)
871 ret = !wine_server_call_err( req );
877 /******************************************************************
878 * start_console_renderer
880 * helper for AllocConsole
881 * starts the renderer process
883 static BOOL start_console_renderer_helper(const char* appname, STARTUPINFOA* si,
888 PROCESS_INFORMATION pi;
890 /* FIXME: use dynamic allocation for most of the buffers below */
891 ret = snprintf(buffer, sizeof(buffer), "%s --use-event=%d", appname, (INT)hEvent);
892 if ((ret > -1) && (ret < sizeof(buffer)) &&
893 CreateProcessA(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS,
894 NULL, NULL, si, &pi))
896 if (WaitForSingleObject(hEvent, INFINITE) != WAIT_OBJECT_0) return FALSE;
898 TRACE("Started wineconsole pid=%08lx tid=%08lx\n",
899 pi.dwProcessId, pi.dwThreadId);
906 static BOOL start_console_renderer(STARTUPINFOA* si)
910 OBJECT_ATTRIBUTES attr;
913 attr.Length = sizeof(attr);
914 attr.RootDirectory = 0;
915 attr.Attributes = OBJ_INHERIT;
916 attr.ObjectName = NULL;
917 attr.SecurityDescriptor = NULL;
918 attr.SecurityQualityOfService = NULL;
920 NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, &attr, TRUE, FALSE);
921 if (!hEvent) return FALSE;
923 /* first try environment variable */
924 if ((p = getenv("WINECONSOLE")) != NULL)
926 ret = start_console_renderer_helper(p, si, hEvent);
928 ERR("Couldn't launch Wine console from WINECONSOLE env var (%s)... "
929 "trying default access\n", p);
932 /* then try the regular PATH */
934 ret = start_console_renderer_helper("wineconsole", si, hEvent);
940 /***********************************************************************
941 * AllocConsole (KERNEL32.@)
943 * creates an xterm with a pty to our program
945 BOOL WINAPI AllocConsole(void)
947 HANDLE handle_in = INVALID_HANDLE_VALUE;
948 HANDLE handle_out = INVALID_HANDLE_VALUE;
949 HANDLE handle_err = INVALID_HANDLE_VALUE;
950 STARTUPINFOA siCurrent;
951 STARTUPINFOA siConsole;
956 handle_in = CreateFileA( "CONIN$", GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
957 0, NULL, OPEN_EXISTING, 0, 0 );
959 if (handle_in != INVALID_HANDLE_VALUE)
961 /* we already have a console opened on this process, don't create a new one */
962 CloseHandle(handle_in);
966 GetStartupInfoA(&siCurrent);
968 memset(&siConsole, 0, sizeof(siConsole));
969 siConsole.cb = sizeof(siConsole);
970 /* setup a view arguments for wineconsole (it'll use them as default values) */
971 if (siCurrent.dwFlags & STARTF_USECOUNTCHARS)
973 siConsole.dwFlags |= STARTF_USECOUNTCHARS;
974 siConsole.dwXCountChars = siCurrent.dwXCountChars;
975 siConsole.dwYCountChars = siCurrent.dwYCountChars;
977 if (siCurrent.dwFlags & STARTF_USEFILLATTRIBUTE)
979 siConsole.dwFlags |= STARTF_USEFILLATTRIBUTE;
980 siConsole.dwFillAttribute = siCurrent.dwFillAttribute;
982 /* FIXME (should pass the unicode form) */
983 if (siCurrent.lpTitle)
984 siConsole.lpTitle = siCurrent.lpTitle;
985 else if (GetModuleFileNameA(0, buffer, sizeof(buffer)))
986 siConsole.lpTitle = buffer;
988 if (!start_console_renderer(&siConsole))
991 handle_in = CreateFileA( "CONIN$", GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
992 0, NULL, OPEN_EXISTING, 0, 0 );
993 if (handle_in == INVALID_HANDLE_VALUE) goto the_end;
995 handle_out = CreateFileA( "CONOUT$", GENERIC_READ|GENERIC_WRITE,
996 0, NULL, OPEN_EXISTING, 0, 0 );
997 if (handle_out == INVALID_HANDLE_VALUE) goto the_end;
999 if (!DuplicateHandle(GetCurrentProcess(), handle_out, GetCurrentProcess(), &handle_err,
1000 0, TRUE, DUPLICATE_SAME_ACCESS))
1003 /* NT resets the STD_*_HANDLEs on console alloc */
1004 SetStdHandle(STD_INPUT_HANDLE, handle_in);
1005 SetStdHandle(STD_OUTPUT_HANDLE, handle_out);
1006 SetStdHandle(STD_ERROR_HANDLE, handle_err);
1008 SetLastError(ERROR_SUCCESS);
1013 ERR("Can't allocate console\n");
1014 if (handle_in != INVALID_HANDLE_VALUE) CloseHandle(handle_in);
1015 if (handle_out != INVALID_HANDLE_VALUE) CloseHandle(handle_out);
1016 if (handle_err != INVALID_HANDLE_VALUE) CloseHandle(handle_err);
1022 /***********************************************************************
1023 * ReadConsoleA (KERNEL32.@)
1025 BOOL WINAPI ReadConsoleA(HANDLE hConsoleInput, LPVOID lpBuffer, DWORD nNumberOfCharsToRead,
1026 LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1028 LPWSTR ptr = HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead * sizeof(WCHAR));
1032 if ((ret = ReadConsoleW(hConsoleInput, ptr, nNumberOfCharsToRead, &ncr, NULL)))
1033 ncr = WideCharToMultiByte(CP_ACP, 0, ptr, ncr, lpBuffer, nNumberOfCharsToRead, NULL, NULL);
1035 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = ncr;
1036 HeapFree(GetProcessHeap(), 0, ptr);
1041 /***********************************************************************
1042 * ReadConsoleW (KERNEL32.@)
1044 BOOL WINAPI ReadConsoleW(HANDLE hConsoleInput, LPVOID lpBuffer,
1045 DWORD nNumberOfCharsToRead, LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1048 LPWSTR xbuf = (LPWSTR)lpBuffer;
1051 TRACE("(%p,%p,%ld,%p,%p)\n",
1052 hConsoleInput, lpBuffer, nNumberOfCharsToRead, lpNumberOfCharsRead, lpReserved);
1054 if (!GetConsoleMode(hConsoleInput, &mode))
1057 if (mode & ENABLE_LINE_INPUT)
1059 if (!S_EditString || S_EditString[S_EditStrPos] == 0)
1061 if (S_EditString) HeapFree(GetProcessHeap(), 0, S_EditString);
1062 if (!(S_EditString = CONSOLE_Readline(hConsoleInput)))
1066 charsread = lstrlenW(&S_EditString[S_EditStrPos]);
1067 if (charsread > nNumberOfCharsToRead) charsread = nNumberOfCharsToRead;
1068 memcpy(xbuf, &S_EditString[S_EditStrPos], charsread * sizeof(WCHAR));
1069 S_EditStrPos += charsread;
1074 DWORD timeout = INFINITE;
1076 /* FIXME: should we read at least 1 char? The SDK does not say */
1077 /* wait for at least one available input record (it doesn't mean we'll have
1078 * chars stored in xbuf...)
1083 if (read_console_input(hConsoleInput, &ir, timeout) != rci_gotone) break;
1085 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown &&
1086 ir.Event.KeyEvent.uChar.UnicodeChar &&
1087 !(ir.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
1089 xbuf[charsread++] = ir.Event.KeyEvent.uChar.UnicodeChar;
1091 } while (charsread < nNumberOfCharsToRead);
1092 /* nothing has been read */
1093 if (timeout == INFINITE) return FALSE;
1096 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = charsread;
1102 /***********************************************************************
1103 * ReadConsoleInputW (KERNEL32.@)
1105 BOOL WINAPI ReadConsoleInputW(HANDLE hConsoleInput, LPINPUT_RECORD lpBuffer,
1106 DWORD nLength, LPDWORD lpNumberOfEventsRead)
1109 DWORD timeout = INFINITE;
1113 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = 0;
1117 /* loop until we get at least one event */
1118 while (read_console_input(hConsoleInput, &lpBuffer[idx], timeout) == rci_gotone &&
1122 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = idx;
1127 /******************************************************************************
1128 * WriteConsoleOutputCharacterW [KERNEL32.@] Copies character to consecutive
1129 * cells in the console screen buffer
1132 * hConsoleOutput [I] Handle to screen buffer
1133 * str [I] Pointer to buffer with chars to write
1134 * length [I] Number of cells to write to
1135 * coord [I] Coords of first cell
1136 * lpNumCharsWritten [O] Pointer to number of cells written
1143 BOOL WINAPI WriteConsoleOutputCharacterW( HANDLE hConsoleOutput, LPCWSTR str, DWORD length,
1144 COORD coord, LPDWORD lpNumCharsWritten )
1148 TRACE("(%p,%s,%ld,%dx%d,%p)\n", hConsoleOutput,
1149 debugstr_wn(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
1151 SERVER_START_REQ( write_console_output )
1153 req->handle = console_handle_unmap(hConsoleOutput);
1156 req->mode = CHAR_INFO_MODE_TEXT;
1158 wine_server_add_data( req, str, length * sizeof(WCHAR) );
1159 if ((ret = !wine_server_call_err( req )))
1161 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
1169 /******************************************************************************
1170 * SetConsoleTitleW [KERNEL32.@] Sets title bar string for console
1173 * title [I] Address of new title
1179 BOOL WINAPI SetConsoleTitleW(LPCWSTR title)
1183 SERVER_START_REQ( set_console_input_info )
1186 req->mask = SET_CONSOLE_INPUT_INFO_TITLE;
1187 wine_server_add_data( req, title, strlenW(title) * sizeof(WCHAR) );
1188 ret = !wine_server_call_err( req );
1195 /***********************************************************************
1196 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
1198 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1200 FIXME("(%p): stub\n", nrofbuttons);
1205 /******************************************************************************
1206 * SetConsoleInputExeNameW [KERNEL32.@]
1211 BOOL WINAPI SetConsoleInputExeNameW(LPCWSTR name)
1213 FIXME("(%s): stub!\n", debugstr_w(name));
1215 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1219 /******************************************************************************
1220 * SetConsoleInputExeNameA [KERNEL32.@]
1225 BOOL WINAPI SetConsoleInputExeNameA(LPCSTR name)
1227 int len = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
1228 LPWSTR xptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1231 if (!xptr) return FALSE;
1233 MultiByteToWideChar(CP_ACP, 0, name, -1, xptr, len);
1234 ret = SetConsoleInputExeNameW(xptr);
1235 HeapFree(GetProcessHeap(), 0, xptr);
1240 /******************************************************************
1241 * CONSOLE_DefaultHandler
1243 * Final control event handler
1245 static BOOL WINAPI CONSOLE_DefaultHandler(DWORD dwCtrlType)
1247 FIXME("Terminating process %lx on event %lx\n", GetCurrentProcessId(), dwCtrlType);
1249 /* should never go here */
1253 /******************************************************************************
1254 * SetConsoleCtrlHandler [KERNEL32.@] Adds function to calling process list
1257 * func [I] Address of handler function
1258 * add [I] Handler to add or remove
1265 * James Sutherland (JamesSutherland@gmx.de)
1266 * Added global variables console_ignore_ctrl_c and handlers[]
1267 * Does not yet do any error checking, or set LastError if failed.
1268 * This doesn't yet matter, since these handlers are not yet called...!
1271 struct ConsoleHandler {
1272 PHANDLER_ROUTINE handler;
1273 struct ConsoleHandler* next;
1276 static unsigned int CONSOLE_IgnoreCtrlC = 0; /* FIXME: this should be inherited somehow */
1277 static struct ConsoleHandler CONSOLE_DefaultConsoleHandler = {CONSOLE_DefaultHandler, NULL};
1278 static struct ConsoleHandler* CONSOLE_Handlers = &CONSOLE_DefaultConsoleHandler;
1280 static CRITICAL_SECTION CONSOLE_CritSect;
1281 static CRITICAL_SECTION_DEBUG critsect_debug =
1283 0, 0, &CONSOLE_CritSect,
1284 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
1285 0, 0, { 0, (DWORD)(__FILE__ ": CONSOLE_CritSect") }
1287 static CRITICAL_SECTION CONSOLE_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
1289 /*****************************************************************************/
1291 BOOL WINAPI SetConsoleCtrlHandler(PHANDLER_ROUTINE func, BOOL add)
1295 FIXME("(%p,%i) - no error checking or testing yet\n", func, add);
1299 CONSOLE_IgnoreCtrlC = add;
1303 struct ConsoleHandler* ch = HeapAlloc(GetProcessHeap(), 0, sizeof(struct ConsoleHandler));
1305 if (!ch) return FALSE;
1307 RtlEnterCriticalSection(&CONSOLE_CritSect);
1308 ch->next = CONSOLE_Handlers;
1309 CONSOLE_Handlers = ch;
1310 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1314 struct ConsoleHandler** ch;
1315 RtlEnterCriticalSection(&CONSOLE_CritSect);
1316 for (ch = &CONSOLE_Handlers; *ch; *ch = (*ch)->next)
1318 if ((*ch)->handler == func) break;
1322 struct ConsoleHandler* rch = *ch;
1325 if (rch == &CONSOLE_DefaultConsoleHandler)
1327 ERR("Who's trying to remove default handler???\n");
1334 HeapFree(GetProcessHeap(), 0, rch);
1339 WARN("Attempt to remove non-installed CtrlHandler %p\n", func);
1342 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1347 static WINE_EXCEPTION_FILTER(CONSOLE_CtrlEventHandler)
1349 TRACE("(%lx)\n", GetExceptionCode());
1350 return EXCEPTION_EXECUTE_HANDLER;
1353 static DWORD WINAPI CONSOLE_HandleCtrlCEntry(void* pmt)
1355 struct ConsoleHandler* ch;
1357 RtlEnterCriticalSection(&CONSOLE_CritSect);
1358 /* the debugger didn't continue... so, pass to ctrl handlers */
1359 for (ch = CONSOLE_Handlers; ch; ch = ch->next)
1361 if (ch->handler((DWORD)pmt)) break;
1363 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1367 /******************************************************************
1368 * CONSOLE_HandleCtrlC
1370 * Check whether the shall manipulate CtrlC events
1372 int CONSOLE_HandleCtrlC(unsigned sig)
1374 /* FIXME: better test whether a console is attached to this process ??? */
1375 extern unsigned CONSOLE_GetNumHistoryEntries(void);
1376 if (CONSOLE_GetNumHistoryEntries() == (unsigned)-1) return 0;
1377 if (CONSOLE_IgnoreCtrlC) return 1;
1379 /* try to pass the exception to the debugger
1380 * if it continues, there's nothing more to do
1381 * otherwise, we need to send the ctrl-event to the handlers
1385 RaiseException( DBG_CONTROL_C, 0, 0, NULL );
1387 __EXCEPT(CONSOLE_CtrlEventHandler)
1389 /* Create a separate thread to signal all the events. This would allow to
1390 * synchronize between setting the handlers and actually calling them
1392 CreateThread(NULL, 0, CONSOLE_HandleCtrlCEntry, (void*)CTRL_C_EVENT, 0, NULL);
1398 /******************************************************************************
1399 * GenerateConsoleCtrlEvent [KERNEL32.@] Simulate a CTRL-C or CTRL-BREAK
1402 * dwCtrlEvent [I] Type of event
1403 * dwProcessGroupID [I] Process group ID to send event to
1407 * Failure: False (and *should* [but doesn't] set LastError)
1409 BOOL WINAPI GenerateConsoleCtrlEvent(DWORD dwCtrlEvent,
1410 DWORD dwProcessGroupID)
1414 TRACE("(%ld, %ld)\n", dwCtrlEvent, dwProcessGroupID);
1416 if (dwCtrlEvent != CTRL_C_EVENT && dwCtrlEvent != CTRL_BREAK_EVENT)
1418 ERR("Invalid event %ld for PGID %ld\n", dwCtrlEvent, dwProcessGroupID);
1422 SERVER_START_REQ( send_console_signal )
1424 req->signal = dwCtrlEvent;
1425 req->group_id = dwProcessGroupID;
1426 ret = !wine_server_call_err( req );
1434 /******************************************************************************
1435 * CreateConsoleScreenBuffer [KERNEL32.@] Creates a console screen buffer
1438 * dwDesiredAccess [I] Access flag
1439 * dwShareMode [I] Buffer share mode
1440 * sa [I] Security attributes
1441 * dwFlags [I] Type of buffer to create
1442 * lpScreenBufferData [I] Reserved
1445 * Should call SetLastError
1448 * Success: Handle to new console screen buffer
1449 * Failure: INVALID_HANDLE_VALUE
1451 HANDLE WINAPI CreateConsoleScreenBuffer(DWORD dwDesiredAccess, DWORD dwShareMode,
1452 LPSECURITY_ATTRIBUTES sa, DWORD dwFlags,
1453 LPVOID lpScreenBufferData)
1455 HANDLE ret = INVALID_HANDLE_VALUE;
1457 TRACE("(%ld,%ld,%p,%ld,%p)\n",
1458 dwDesiredAccess, dwShareMode, sa, dwFlags, lpScreenBufferData);
1460 if (dwFlags != CONSOLE_TEXTMODE_BUFFER || lpScreenBufferData != NULL)
1462 SetLastError(ERROR_INVALID_PARAMETER);
1463 return INVALID_HANDLE_VALUE;
1466 SERVER_START_REQ(create_console_output)
1469 req->access = dwDesiredAccess;
1470 req->share = dwShareMode;
1471 req->inherit = (sa && sa->bInheritHandle);
1472 if (!wine_server_call_err( req )) ret = reply->handle_out;
1480 /***********************************************************************
1481 * GetConsoleScreenBufferInfo (KERNEL32.@)
1483 BOOL WINAPI GetConsoleScreenBufferInfo(HANDLE hConsoleOutput, LPCONSOLE_SCREEN_BUFFER_INFO csbi)
1487 SERVER_START_REQ(get_console_output_info)
1489 req->handle = console_handle_unmap(hConsoleOutput);
1490 if ((ret = !wine_server_call_err( req )))
1492 csbi->dwSize.X = reply->width;
1493 csbi->dwSize.Y = reply->height;
1494 csbi->dwCursorPosition.X = reply->cursor_x;
1495 csbi->dwCursorPosition.Y = reply->cursor_y;
1496 csbi->wAttributes = reply->attr;
1497 csbi->srWindow.Left = reply->win_left;
1498 csbi->srWindow.Right = reply->win_right;
1499 csbi->srWindow.Top = reply->win_top;
1500 csbi->srWindow.Bottom = reply->win_bottom;
1501 csbi->dwMaximumWindowSize.X = reply->max_width;
1502 csbi->dwMaximumWindowSize.Y = reply->max_height;
1511 /******************************************************************************
1512 * SetConsoleActiveScreenBuffer [KERNEL32.@] Sets buffer to current console
1518 BOOL WINAPI SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput)
1522 TRACE("(%p)\n", hConsoleOutput);
1524 SERVER_START_REQ( set_console_input_info )
1527 req->mask = SET_CONSOLE_INPUT_INFO_ACTIVE_SB;
1528 req->active_sb = hConsoleOutput;
1529 ret = !wine_server_call_err( req );
1536 /***********************************************************************
1537 * GetConsoleMode (KERNEL32.@)
1539 BOOL WINAPI GetConsoleMode(HANDLE hcon, LPDWORD mode)
1543 SERVER_START_REQ(get_console_mode)
1545 req->handle = console_handle_unmap(hcon);
1546 ret = !wine_server_call_err( req );
1547 if (ret && mode) *mode = reply->mode;
1554 /******************************************************************************
1555 * SetConsoleMode [KERNEL32.@] Sets input mode of console's input buffer
1558 * hcon [I] Handle to console input or screen buffer
1559 * mode [I] Input or output mode to set
1566 * ENABLE_PROCESSED_INPUT 0x01
1567 * ENABLE_LINE_INPUT 0x02
1568 * ENABLE_ECHO_INPUT 0x04
1569 * ENABLE_WINDOW_INPUT 0x08
1570 * ENABLE_MOUSE_INPUT 0x10
1572 BOOL WINAPI SetConsoleMode(HANDLE hcon, DWORD mode)
1576 SERVER_START_REQ(set_console_mode)
1578 req->handle = console_handle_unmap(hcon);
1580 ret = !wine_server_call_err( req );
1583 /* FIXME: when resetting a console input to editline mode, I think we should
1584 * empty the S_EditString buffer
1587 TRACE("(%p,%lx) retval == %d\n", hcon, mode, ret);
1593 /******************************************************************
1594 * CONSOLE_WriteChars
1596 * WriteConsoleOutput helper: hides server call semantics
1597 * writes a string at a given pos with standard attribute
1599 int CONSOLE_WriteChars(HANDLE hCon, LPCWSTR lpBuffer, int nc, COORD* pos)
1605 SERVER_START_REQ( write_console_output )
1607 req->handle = console_handle_unmap(hCon);
1610 req->mode = CHAR_INFO_MODE_TEXTSTDATTR;
1612 wine_server_add_data( req, lpBuffer, nc * sizeof(WCHAR) );
1613 if (!wine_server_call_err( req )) written = reply->written;
1617 if (written > 0) pos->X += written;
1621 /******************************************************************
1624 * WriteConsoleOutput helper: handles passing to next line (+scrolling if necessary)
1627 static int next_line(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi)
1633 csbi->dwCursorPosition.X = 0;
1634 csbi->dwCursorPosition.Y++;
1636 if (csbi->dwCursorPosition.Y < csbi->dwSize.Y) return 1;
1639 src.Bottom = csbi->dwSize.Y - 1;
1641 src.Right = csbi->dwSize.X - 1;
1646 ci.Attributes = csbi->wAttributes;
1647 ci.Char.UnicodeChar = ' ';
1649 csbi->dwCursorPosition.Y--;
1650 if (!ScrollConsoleScreenBufferW(hCon, &src, NULL, dst, &ci))
1655 /******************************************************************
1658 * WriteConsoleOutput helper: writes a block of non special characters
1659 * Block can spread on several lines, and wrapping, if needed, is
1663 static int write_block(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi,
1664 DWORD mode, LPWSTR ptr, int len)
1666 int blk; /* number of chars to write on current line */
1667 int done; /* number of chars already written */
1669 if (len <= 0) return 1;
1671 if (mode & ENABLE_WRAP_AT_EOL_OUTPUT) /* writes remaining on next line */
1673 for (done = 0; done < len; done += blk)
1675 blk = min(len - done, csbi->dwSize.X - csbi->dwCursorPosition.X);
1677 if (CONSOLE_WriteChars(hCon, ptr + done, blk, &csbi->dwCursorPosition) != blk)
1679 if (csbi->dwCursorPosition.X == csbi->dwSize.X && !next_line(hCon, csbi))
1685 int pos = csbi->dwCursorPosition.X;
1686 /* FIXME: we could reduce the number of loops
1687 * but, in most cases we wouldn't gain lots of time (it would only
1688 * happen if we're asked to overwrite more than twice the part of the line,
1691 for (blk = done = 0; done < len; done += blk)
1693 blk = min(len - done, csbi->dwSize.X - csbi->dwCursorPosition.X);
1695 csbi->dwCursorPosition.X = pos;
1696 if (CONSOLE_WriteChars(hCon, ptr + done, blk, &csbi->dwCursorPosition) != blk)
1704 /***********************************************************************
1705 * WriteConsoleW (KERNEL32.@)
1707 BOOL WINAPI WriteConsoleW(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
1708 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
1712 WCHAR* psz = (WCHAR*)lpBuffer;
1713 CONSOLE_SCREEN_BUFFER_INFO csbi;
1716 TRACE("%p %s %ld %p %p\n",
1717 hConsoleOutput, debugstr_wn(lpBuffer, nNumberOfCharsToWrite),
1718 nNumberOfCharsToWrite, lpNumberOfCharsWritten, lpReserved);
1720 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
1722 if (!GetConsoleMode(hConsoleOutput, &mode) ||
1723 !GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
1726 if (mode & ENABLE_PROCESSED_OUTPUT)
1730 for (i = 0; i < nNumberOfCharsToWrite; i++)
1734 case '\b': case '\t': case '\n': case '\a': case '\r':
1735 /* don't handle here the i-th char... done below */
1736 if ((k = i - first) > 0)
1738 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
1748 if (csbi.dwCursorPosition.X > 0) csbi.dwCursorPosition.X--;
1752 WCHAR tmp[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
1754 if (!write_block(hConsoleOutput, &csbi, mode, tmp,
1755 ((csbi.dwCursorPosition.X + 8) & ~7) - csbi.dwCursorPosition.X))
1760 next_line(hConsoleOutput, &csbi);
1766 csbi.dwCursorPosition.X = 0;
1774 /* write the remaining block (if any) if processed output is enabled, or the
1775 * entire buffer otherwise
1777 if ((k = nNumberOfCharsToWrite - first) > 0)
1779 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
1785 SetConsoleCursorPosition(hConsoleOutput, csbi.dwCursorPosition);
1786 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = nw;
1791 /***********************************************************************
1792 * WriteConsoleA (KERNEL32.@)
1794 BOOL WINAPI WriteConsoleA(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
1795 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
1801 n = MultiByteToWideChar(CP_ACP, 0, lpBuffer, nNumberOfCharsToWrite, NULL, 0);
1803 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
1804 xstring = HeapAlloc(GetProcessHeap(), 0, n * sizeof(WCHAR));
1805 if (!xstring) return 0;
1807 MultiByteToWideChar(CP_ACP, 0, lpBuffer, nNumberOfCharsToWrite, xstring, n);
1809 ret = WriteConsoleW(hConsoleOutput, xstring, n, lpNumberOfCharsWritten, 0);
1811 HeapFree(GetProcessHeap(), 0, xstring);
1816 /******************************************************************************
1817 * SetConsoleCursorPosition [KERNEL32.@]
1818 * Sets the cursor position in console
1821 * hConsoleOutput [I] Handle of console screen buffer
1822 * dwCursorPosition [I] New cursor position coordinates
1826 BOOL WINAPI SetConsoleCursorPosition(HANDLE hcon, COORD pos)
1829 CONSOLE_SCREEN_BUFFER_INFO csbi;
1833 TRACE("%p %d %d\n", hcon, pos.X, pos.Y);
1835 SERVER_START_REQ(set_console_output_info)
1837 req->handle = console_handle_unmap(hcon);
1838 req->cursor_x = pos.X;
1839 req->cursor_y = pos.Y;
1840 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_POS;
1841 ret = !wine_server_call_err( req );
1845 if (!ret || !GetConsoleScreenBufferInfo(hcon, &csbi))
1848 /* if cursor is no longer visible, scroll the visible window... */
1849 w = csbi.srWindow.Right - csbi.srWindow.Left + 1;
1850 h = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
1851 if (pos.X < csbi.srWindow.Left)
1853 csbi.srWindow.Left = min(pos.X, csbi.dwSize.X - w);
1856 else if (pos.X > csbi.srWindow.Right)
1858 csbi.srWindow.Left = max(pos.X, w) - w + 1;
1861 csbi.srWindow.Right = csbi.srWindow.Left + w - 1;
1863 if (pos.Y < csbi.srWindow.Top)
1865 csbi.srWindow.Top = min(pos.Y, csbi.dwSize.Y - h);
1868 else if (pos.Y > csbi.srWindow.Bottom)
1870 csbi.srWindow.Top = max(pos.Y, h) - h + 1;
1873 csbi.srWindow.Bottom = csbi.srWindow.Top + h - 1;
1875 ret = (do_move) ? SetConsoleWindowInfo(hcon, TRUE, &csbi.srWindow) : TRUE;
1880 /******************************************************************************
1881 * GetConsoleCursorInfo [KERNEL32.@] Gets size and visibility of console
1884 * hcon [I] Handle to console screen buffer
1885 * cinfo [O] Address of cursor information
1891 BOOL WINAPI GetConsoleCursorInfo(HANDLE hcon, LPCONSOLE_CURSOR_INFO cinfo)
1895 SERVER_START_REQ(get_console_output_info)
1897 req->handle = console_handle_unmap(hcon);
1898 ret = !wine_server_call_err( req );
1901 cinfo->dwSize = reply->cursor_size;
1902 cinfo->bVisible = reply->cursor_visible;
1910 /******************************************************************************
1911 * SetConsoleCursorInfo [KERNEL32.@] Sets size and visibility of cursor
1914 * hcon [I] Handle to console screen buffer
1915 * cinfo [I] Address of cursor information
1920 BOOL WINAPI SetConsoleCursorInfo(HANDLE hCon, LPCONSOLE_CURSOR_INFO cinfo)
1924 SERVER_START_REQ(set_console_output_info)
1926 req->handle = console_handle_unmap(hCon);
1927 req->cursor_size = cinfo->dwSize;
1928 req->cursor_visible = cinfo->bVisible;
1929 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM;
1930 ret = !wine_server_call_err( req );
1937 /******************************************************************************
1938 * SetConsoleWindowInfo [KERNEL32.@] Sets size and position of console
1941 * hcon [I] Handle to console screen buffer
1942 * bAbsolute [I] Coordinate type flag
1943 * window [I] Address of new window rectangle
1948 BOOL WINAPI SetConsoleWindowInfo(HANDLE hCon, BOOL bAbsolute, LPSMALL_RECT window)
1950 SMALL_RECT p = *window;
1955 CONSOLE_SCREEN_BUFFER_INFO csbi;
1956 if (!GetConsoleScreenBufferInfo(hCon, &csbi))
1958 p.Left += csbi.srWindow.Left;
1959 p.Top += csbi.srWindow.Top;
1960 p.Right += csbi.srWindow.Left;
1961 p.Bottom += csbi.srWindow.Top;
1963 SERVER_START_REQ(set_console_output_info)
1965 req->handle = console_handle_unmap(hCon);
1966 req->win_left = p.Left;
1967 req->win_top = p.Top;
1968 req->win_right = p.Right;
1969 req->win_bottom = p.Bottom;
1970 req->mask = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
1971 ret = !wine_server_call_err( req );
1979 /******************************************************************************
1980 * SetConsoleTextAttribute [KERNEL32.@] Sets colors for text
1982 * Sets the foreground and background color attributes of characters
1983 * written to the screen buffer.
1989 BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttr)
1993 SERVER_START_REQ(set_console_output_info)
1995 req->handle = console_handle_unmap(hConsoleOutput);
1997 req->mask = SET_CONSOLE_OUTPUT_INFO_ATTR;
1998 ret = !wine_server_call_err( req );
2005 /******************************************************************************
2006 * SetConsoleScreenBufferSize [KERNEL32.@] Changes size of console
2009 * hConsoleOutput [I] Handle to console screen buffer
2010 * dwSize [I] New size in character rows and cols
2016 BOOL WINAPI SetConsoleScreenBufferSize(HANDLE hConsoleOutput, COORD dwSize)
2020 SERVER_START_REQ(set_console_output_info)
2022 req->handle = console_handle_unmap(hConsoleOutput);
2023 req->width = dwSize.X;
2024 req->height = dwSize.Y;
2025 req->mask = SET_CONSOLE_OUTPUT_INFO_SIZE;
2026 ret = !wine_server_call_err( req );
2033 /******************************************************************************
2034 * ScrollConsoleScreenBufferA [KERNEL32.@]
2037 BOOL WINAPI ScrollConsoleScreenBufferA(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2038 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2043 ciw.Attributes = lpFill->Attributes;
2044 MultiByteToWideChar(CP_ACP, 0, &lpFill->Char.AsciiChar, 1, &ciw.Char.UnicodeChar, 1);
2046 return ScrollConsoleScreenBufferW(hConsoleOutput, lpScrollRect, lpClipRect,
2047 dwDestOrigin, &ciw);
2050 /******************************************************************
2051 * CONSOLE_FillLineUniform
2053 * Helper function for ScrollConsoleScreenBufferW
2054 * Fills a part of a line with a constant character info
2056 void CONSOLE_FillLineUniform(HANDLE hConsoleOutput, int i, int j, int len, LPCHAR_INFO lpFill)
2058 SERVER_START_REQ( fill_console_output )
2060 req->handle = console_handle_unmap(hConsoleOutput);
2061 req->mode = CHAR_INFO_MODE_TEXTATTR;
2066 req->data.ch = lpFill->Char.UnicodeChar;
2067 req->data.attr = lpFill->Attributes;
2068 wine_server_call_err( req );
2073 /******************************************************************************
2074 * ScrollConsoleScreenBufferW [KERNEL32.@]
2078 BOOL WINAPI ScrollConsoleScreenBufferW(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2079 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2087 CONSOLE_SCREEN_BUFFER_INFO csbi;
2091 TRACE("(%p,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", hConsoleOutput,
2092 lpScrollRect->Left, lpScrollRect->Top,
2093 lpScrollRect->Right, lpScrollRect->Bottom,
2094 lpClipRect->Left, lpClipRect->Top,
2095 lpClipRect->Right, lpClipRect->Bottom,
2096 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2098 TRACE("(%p,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", hConsoleOutput,
2099 lpScrollRect->Left, lpScrollRect->Top,
2100 lpScrollRect->Right, lpScrollRect->Bottom,
2101 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2103 if (!GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
2106 /* step 1: get dst rect */
2107 dst.Left = dwDestOrigin.X;
2108 dst.Top = dwDestOrigin.Y;
2109 dst.Right = dst.Left + (lpScrollRect->Right - lpScrollRect->Left);
2110 dst.Bottom = dst.Top + (lpScrollRect->Bottom - lpScrollRect->Top);
2112 /* step 2a: compute the final clip rect (optional passed clip and screen buffer limits */
2115 clip.Left = max(0, lpClipRect->Left);
2116 clip.Right = min(csbi.dwSize.X - 1, lpClipRect->Right);
2117 clip.Top = max(0, lpClipRect->Top);
2118 clip.Bottom = min(csbi.dwSize.Y - 1, lpClipRect->Bottom);
2123 clip.Right = csbi.dwSize.X - 1;
2125 clip.Bottom = csbi.dwSize.Y - 1;
2127 if (clip.Left > clip.Right || clip.Top > clip.Bottom) return FALSE;
2129 /* step 2b: clip dst rect */
2130 if (dst.Left < clip.Left ) dst.Left = clip.Left;
2131 if (dst.Top < clip.Top ) dst.Top = clip.Top;
2132 if (dst.Right > clip.Right ) dst.Right = clip.Right;
2133 if (dst.Bottom > clip.Bottom) dst.Bottom = clip.Bottom;
2135 /* step 3: transfer the bits */
2136 SERVER_START_REQ(move_console_output)
2138 req->handle = console_handle_unmap(hConsoleOutput);
2139 req->x_src = lpScrollRect->Left;
2140 req->y_src = lpScrollRect->Top;
2141 req->x_dst = dst.Left;
2142 req->y_dst = dst.Top;
2143 req->w = dst.Right - dst.Left + 1;
2144 req->h = dst.Bottom - dst.Top + 1;
2145 ret = !wine_server_call_err( req );
2149 if (!ret) return FALSE;
2151 /* step 4: clean out the exposed part */
2153 /* have to write cell [i,j] if it is not in dst rect (because it has already
2154 * been written to by the scroll) and is in clip (we shall not write
2157 for (j = max(lpScrollRect->Top, clip.Top); j <= min(lpScrollRect->Bottom, clip.Bottom); j++)
2159 inside = dst.Top <= j && j <= dst.Bottom;
2161 for (i = max(lpScrollRect->Left, clip.Left); i <= min(lpScrollRect->Right, clip.Right); i++)
2163 if (inside && dst.Left <= i && i <= dst.Right)
2167 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2173 if (start == -1) start = i;
2177 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2184 /* ====================================================================
2186 * Console manipulation functions
2188 * ====================================================================*/
2190 /* some missing functions...
2191 * FIXME: those are likely to be defined as undocumented function in kernel32 (or part of them)
2192 * should get the right API and implement them
2193 * GetConsoleCommandHistory[AW] (dword dword dword)
2194 * GetConsoleCommandHistoryLength[AW]
2195 * SetConsoleCommandHistoryMode
2196 * SetConsoleNumberOfCommands[AW]
2198 int CONSOLE_GetHistory(int idx, WCHAR* buf, int buf_len)
2202 SERVER_START_REQ( get_console_input_history )
2206 if (buf && buf_len > 1)
2208 wine_server_set_reply( req, buf, (buf_len - 1) * sizeof(WCHAR) );
2210 if (!wine_server_call_err( req ))
2212 if (buf) buf[wine_server_reply_size(reply) / sizeof(WCHAR)] = 0;
2213 len = reply->total / sizeof(WCHAR) + 1;
2220 /******************************************************************
2221 * CONSOLE_AppendHistory
2225 BOOL CONSOLE_AppendHistory(const WCHAR* ptr)
2227 size_t len = strlenW(ptr);
2230 while (len && (ptr[len - 1] == '\n' || ptr[len - 1] == '\r')) len--;
2232 SERVER_START_REQ( append_console_input_history )
2235 wine_server_add_data( req, ptr, len * sizeof(WCHAR) );
2236 ret = !wine_server_call_err( req );
2242 /******************************************************************
2243 * CONSOLE_GetNumHistoryEntries
2247 unsigned CONSOLE_GetNumHistoryEntries(void)
2250 SERVER_START_REQ(get_console_input_info)
2253 if (!wine_server_call_err( req )) ret = reply->history_index;
2259 /******************************************************************
2260 * CONSOLE_GetEditionMode
2264 BOOL CONSOLE_GetEditionMode(HANDLE hConIn, int* mode)
2266 unsigned ret = FALSE;
2267 SERVER_START_REQ(get_console_input_info)
2269 req->handle = console_handle_unmap(hConIn);
2270 if ((ret = !wine_server_call_err( req )))
2271 *mode = reply->edition_mode;