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"
45 #include "wine/server.h"
46 #include "wine/exception.h"
47 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 #include "console_private.h"
51 #include "kernel_private.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(console);
55 static UINT console_input_codepage;
56 static UINT console_output_codepage;
59 /* map input records to ASCII */
60 static void input_records_WtoA( INPUT_RECORD *buffer, int count )
65 for (i = 0; i < count; i++)
67 if (buffer[i].EventType != KEY_EVENT) continue;
68 WideCharToMultiByte( GetConsoleCP(), 0,
69 &buffer[i].Event.KeyEvent.uChar.UnicodeChar, 1, &ch, 1, NULL, NULL );
70 buffer[i].Event.KeyEvent.uChar.AsciiChar = ch;
74 /* map input records to Unicode */
75 static void input_records_AtoW( INPUT_RECORD *buffer, int count )
80 for (i = 0; i < count; i++)
82 if (buffer[i].EventType != KEY_EVENT) continue;
83 MultiByteToWideChar( GetConsoleCP(), 0,
84 &buffer[i].Event.KeyEvent.uChar.AsciiChar, 1, &ch, 1 );
85 buffer[i].Event.KeyEvent.uChar.UnicodeChar = ch;
89 /* map char infos to ASCII */
90 static void char_info_WtoA( CHAR_INFO *buffer, int count )
96 WideCharToMultiByte( GetConsoleOutputCP(), 0, &buffer->Char.UnicodeChar, 1,
98 buffer->Char.AsciiChar = ch;
103 /* map char infos to Unicode */
104 static void char_info_AtoW( CHAR_INFO *buffer, int count )
110 MultiByteToWideChar( GetConsoleOutputCP(), 0, &buffer->Char.AsciiChar, 1, &ch, 1 );
111 buffer->Char.UnicodeChar = ch;
117 /******************************************************************************
118 * GetConsoleCP [KERNEL32.@] Returns the OEM code page for the console
123 UINT WINAPI GetConsoleCP(VOID)
125 if (!console_input_codepage) console_input_codepage = GetOEMCP();
126 return console_input_codepage;
130 /******************************************************************************
131 * SetConsoleCP [KERNEL32.@]
133 BOOL WINAPI SetConsoleCP(UINT cp)
135 if (!IsValidCodePage( cp )) return FALSE;
136 console_input_codepage = cp;
141 /***********************************************************************
142 * GetConsoleOutputCP (KERNEL32.@)
144 UINT WINAPI GetConsoleOutputCP(VOID)
146 if (!console_output_codepage) console_output_codepage = GetOEMCP();
147 return console_output_codepage;
151 /******************************************************************************
152 * SetConsoleOutputCP [KERNEL32.@] Set the output codepage used by the console
155 * cp [I] code page to set
161 BOOL WINAPI SetConsoleOutputCP(UINT cp)
163 if (!IsValidCodePage( cp )) return FALSE;
164 console_output_codepage = cp;
169 /******************************************************************************
170 * WriteConsoleInputA [KERNEL32.@]
172 BOOL WINAPI WriteConsoleInputA( HANDLE handle, const INPUT_RECORD *buffer,
173 DWORD count, LPDWORD written )
178 if (!(recW = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*recW) ))) return FALSE;
179 memcpy( recW, buffer, count*sizeof(*recW) );
180 input_records_AtoW( recW, count );
181 ret = WriteConsoleInputW( handle, recW, count, written );
182 HeapFree( GetProcessHeap(), 0, recW );
187 /******************************************************************************
188 * WriteConsoleInputW [KERNEL32.@]
190 BOOL WINAPI WriteConsoleInputW( HANDLE handle, const INPUT_RECORD *buffer,
191 DWORD count, LPDWORD written )
195 TRACE("(%p,%p,%ld,%p)\n", handle, buffer, count, written);
197 if (written) *written = 0;
198 SERVER_START_REQ( write_console_input )
200 req->handle = console_handle_unmap(handle);
201 wine_server_add_data( req, buffer, count * sizeof(INPUT_RECORD) );
202 if ((ret = !wine_server_call_err( req )) && written)
203 *written = reply->written;
211 /***********************************************************************
212 * WriteConsoleOutputA (KERNEL32.@)
214 BOOL WINAPI WriteConsoleOutputA( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
215 COORD size, COORD coord, LPSMALL_RECT region )
219 COORD new_size, new_coord;
222 new_size.X = min( region->Right - region->Left + 1, size.X - coord.X );
223 new_size.Y = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
225 if (new_size.X <= 0 || new_size.Y <= 0)
227 region->Bottom = region->Top + new_size.Y - 1;
228 region->Right = region->Left + new_size.X - 1;
232 /* only copy the useful rectangle */
233 if (!(ciw = HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO) * new_size.X * new_size.Y )))
235 for (y = 0; y < new_size.Y; y++)
237 memcpy( &ciw[y * new_size.X], &lpBuffer[(y + coord.Y) * size.X + coord.X],
238 new_size.X * sizeof(CHAR_INFO) );
239 char_info_AtoW( ciw, new_size.X );
241 new_coord.X = new_coord.Y = 0;
242 ret = WriteConsoleOutputW( hConsoleOutput, ciw, new_size, new_coord, region );
243 if (ciw) HeapFree( GetProcessHeap(), 0, ciw );
248 /***********************************************************************
249 * WriteConsoleOutputW (KERNEL32.@)
251 BOOL WINAPI WriteConsoleOutputW( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
252 COORD size, COORD coord, LPSMALL_RECT region )
254 int width, height, y;
257 TRACE("(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n",
258 hConsoleOutput, lpBuffer, size.X, size.Y, coord.X, coord.Y,
259 region->Left, region->Top, region->Right, region->Bottom);
261 width = min( region->Right - region->Left + 1, size.X - coord.X );
262 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
264 if (width > 0 && height > 0)
266 for (y = 0; y < height; y++)
268 SERVER_START_REQ( write_console_output )
270 req->handle = console_handle_unmap(hConsoleOutput);
271 req->x = region->Left;
272 req->y = region->Top + y;
273 req->mode = CHAR_INFO_MODE_TEXTATTR;
275 wine_server_add_data( req, &lpBuffer[(y + coord.Y) * size.X + coord.X],
276 width * sizeof(CHAR_INFO));
277 if ((ret = !wine_server_call_err( req )))
279 width = min( width, reply->width - region->Left );
280 height = min( height, reply->height - region->Top );
287 region->Bottom = region->Top + height - 1;
288 region->Right = region->Left + width - 1;
293 /******************************************************************************
294 * WriteConsoleOutputCharacterA [KERNEL32.@] Copies character to consecutive
295 * cells in the console screen buffer
298 * hConsoleOutput [I] Handle to screen buffer
299 * str [I] Pointer to buffer with chars to write
300 * length [I] Number of cells to write to
301 * coord [I] Coords of first cell
302 * lpNumCharsWritten [O] Pointer to number of cells written
304 BOOL WINAPI WriteConsoleOutputCharacterA( HANDLE hConsoleOutput, LPCSTR str, DWORD length,
305 COORD coord, LPDWORD lpNumCharsWritten )
311 TRACE("(%p,%s,%ld,%dx%d,%p)\n", hConsoleOutput,
312 debugstr_an(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
314 lenW = MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, NULL, 0 );
316 if (lpNumCharsWritten) *lpNumCharsWritten = 0;
318 if (!(strW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) ))) return FALSE;
319 MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, strW, lenW );
321 ret = WriteConsoleOutputCharacterW( hConsoleOutput, strW, lenW, coord, lpNumCharsWritten );
322 HeapFree( GetProcessHeap(), 0, strW );
327 /******************************************************************************
328 * WriteConsoleOutputAttribute [KERNEL32.@] Sets attributes for some cells in
329 * the console screen buffer
332 * hConsoleOutput [I] Handle to screen buffer
333 * attr [I] Pointer to buffer with write attributes
334 * length [I] Number of cells to write to
335 * coord [I] Coords of first cell
336 * lpNumAttrsWritten [O] Pointer to number of cells written
343 BOOL WINAPI WriteConsoleOutputAttribute( HANDLE hConsoleOutput, CONST WORD *attr, DWORD length,
344 COORD coord, LPDWORD lpNumAttrsWritten )
348 TRACE("(%p,%p,%ld,%dx%d,%p)\n", hConsoleOutput,attr,length,coord.X,coord.Y,lpNumAttrsWritten);
350 SERVER_START_REQ( write_console_output )
352 req->handle = console_handle_unmap(hConsoleOutput);
355 req->mode = CHAR_INFO_MODE_ATTR;
357 wine_server_add_data( req, attr, length * sizeof(WORD) );
358 if ((ret = !wine_server_call_err( req )))
360 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
368 /******************************************************************************
369 * FillConsoleOutputCharacterA [KERNEL32.@]
372 * hConsoleOutput [I] Handle to screen buffer
373 * ch [I] Character to write
374 * length [I] Number of cells to write to
375 * coord [I] Coords of first cell
376 * lpNumCharsWritten [O] Pointer to number of cells written
382 BOOL WINAPI FillConsoleOutputCharacterA( HANDLE hConsoleOutput, CHAR ch, DWORD length,
383 COORD coord, LPDWORD lpNumCharsWritten )
387 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch, 1, &wch, 1 );
388 return FillConsoleOutputCharacterW(hConsoleOutput, wch, length, coord, lpNumCharsWritten);
392 /******************************************************************************
393 * FillConsoleOutputCharacterW [KERNEL32.@] Writes characters to console
396 * hConsoleOutput [I] Handle to screen buffer
397 * ch [I] Character to write
398 * length [I] Number of cells to write to
399 * coord [I] Coords of first cell
400 * lpNumCharsWritten [O] Pointer to number of cells written
406 BOOL WINAPI FillConsoleOutputCharacterW( HANDLE hConsoleOutput, WCHAR ch, DWORD length,
407 COORD coord, LPDWORD lpNumCharsWritten)
411 TRACE("(%p,%s,%ld,(%dx%d),%p)\n",
412 hConsoleOutput, debugstr_wn(&ch, 1), length, coord.X, coord.Y, lpNumCharsWritten);
414 SERVER_START_REQ( fill_console_output )
416 req->handle = console_handle_unmap(hConsoleOutput);
419 req->mode = CHAR_INFO_MODE_TEXT;
423 if ((ret = !wine_server_call_err( req )))
425 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
433 /******************************************************************************
434 * FillConsoleOutputAttribute [KERNEL32.@] Sets attributes for console
437 * hConsoleOutput [I] Handle to screen buffer
438 * attr [I] Color attribute to write
439 * length [I] Number of cells to write to
440 * coord [I] Coords of first cell
441 * lpNumAttrsWritten [O] Pointer to number of cells written
447 BOOL WINAPI FillConsoleOutputAttribute( HANDLE hConsoleOutput, WORD attr, DWORD length,
448 COORD coord, LPDWORD lpNumAttrsWritten )
452 TRACE("(%p,%d,%ld,(%dx%d),%p)\n",
453 hConsoleOutput, attr, length, coord.X, coord.Y, lpNumAttrsWritten);
455 SERVER_START_REQ( fill_console_output )
457 req->handle = console_handle_unmap(hConsoleOutput);
460 req->mode = CHAR_INFO_MODE_ATTR;
462 req->data.attr = attr;
464 if ((ret = !wine_server_call_err( req )))
466 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
474 /******************************************************************************
475 * ReadConsoleOutputCharacterA [KERNEL32.@]
478 BOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput, LPSTR lpstr, DWORD count,
479 COORD coord, LPDWORD read_count)
483 LPWSTR wptr = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
485 if (read_count) *read_count = 0;
486 if (!wptr) return FALSE;
488 if ((ret = ReadConsoleOutputCharacterW( hConsoleOutput, wptr, count, coord, &read )))
490 read = WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr, read, lpstr, count, NULL, NULL);
491 if (read_count) *read_count = read;
493 HeapFree( GetProcessHeap(), 0, wptr );
498 /******************************************************************************
499 * ReadConsoleOutputCharacterW [KERNEL32.@]
502 BOOL WINAPI ReadConsoleOutputCharacterW( HANDLE hConsoleOutput, LPWSTR buffer, DWORD count,
503 COORD coord, LPDWORD read_count )
507 TRACE( "(%p,%p,%ld,%dx%d,%p)\n", hConsoleOutput, buffer, count, coord.X, coord.Y, read_count );
509 SERVER_START_REQ( read_console_output )
511 req->handle = console_handle_unmap(hConsoleOutput);
514 req->mode = CHAR_INFO_MODE_TEXT;
516 wine_server_set_reply( req, buffer, count * sizeof(WCHAR) );
517 if ((ret = !wine_server_call_err( req )))
519 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WCHAR);
527 /******************************************************************************
528 * ReadConsoleOutputAttribute [KERNEL32.@]
530 BOOL WINAPI ReadConsoleOutputAttribute(HANDLE hConsoleOutput, LPWORD lpAttribute, DWORD length,
531 COORD coord, LPDWORD read_count)
535 TRACE("(%p,%p,%ld,%dx%d,%p)\n",
536 hConsoleOutput, lpAttribute, length, coord.X, coord.Y, read_count);
538 SERVER_START_REQ( read_console_output )
540 req->handle = console_handle_unmap(hConsoleOutput);
543 req->mode = CHAR_INFO_MODE_ATTR;
545 wine_server_set_reply( req, lpAttribute, length * sizeof(WORD) );
546 if ((ret = !wine_server_call_err( req )))
548 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WORD);
556 /******************************************************************************
557 * ReadConsoleOutputA [KERNEL32.@]
560 BOOL WINAPI ReadConsoleOutputA( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
561 COORD coord, LPSMALL_RECT region )
566 ret = ReadConsoleOutputW( hConsoleOutput, lpBuffer, size, coord, region );
567 if (ret && region->Right >= region->Left)
569 for (y = 0; y <= region->Bottom - region->Top; y++)
571 char_info_WtoA( &lpBuffer[(coord.Y + y) * size.X + coord.X],
572 region->Right - region->Left + 1 );
579 /******************************************************************************
580 * ReadConsoleOutputW [KERNEL32.@]
582 * NOTE: The NT4 (sp5) kernel crashes on me if size is (0,0). I don't
583 * think we need to be *that* compatible. -- AJ
585 BOOL WINAPI ReadConsoleOutputW( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
586 COORD coord, LPSMALL_RECT region )
588 int width, height, y;
591 width = min( region->Right - region->Left + 1, size.X - coord.X );
592 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
594 if (width > 0 && height > 0)
596 for (y = 0; y < height; y++)
598 SERVER_START_REQ( read_console_output )
600 req->handle = console_handle_unmap(hConsoleOutput);
601 req->x = region->Left;
602 req->y = region->Top + y;
603 req->mode = CHAR_INFO_MODE_TEXTATTR;
605 wine_server_set_reply( req, &lpBuffer[(y+coord.Y) * size.X + coord.X],
606 width * sizeof(CHAR_INFO) );
607 if ((ret = !wine_server_call_err( req )))
609 width = min( width, reply->width - region->Left );
610 height = min( height, reply->height - region->Top );
617 region->Bottom = region->Top + height - 1;
618 region->Right = region->Left + width - 1;
623 /******************************************************************************
624 * ReadConsoleInputA [KERNEL32.@] Reads data from a console
627 * handle [I] Handle to console input buffer
628 * buffer [O] Address of buffer for read data
629 * count [I] Number of records to read
630 * pRead [O] Address of number of records read
636 BOOL WINAPI ReadConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
640 if (!ReadConsoleInputW( handle, buffer, count, &read )) return FALSE;
641 input_records_WtoA( buffer, read );
642 if (pRead) *pRead = read;
647 /***********************************************************************
648 * PeekConsoleInputA (KERNEL32.@)
650 * Gets 'count' first events (or less) from input queue.
652 BOOL WINAPI PeekConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
656 if (!PeekConsoleInputW( handle, buffer, count, &read )) return FALSE;
657 input_records_WtoA( buffer, read );
658 if (pRead) *pRead = read;
663 /***********************************************************************
664 * PeekConsoleInputW (KERNEL32.@)
666 BOOL WINAPI PeekConsoleInputW( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD read )
669 SERVER_START_REQ( read_console_input )
671 req->handle = console_handle_unmap(handle);
673 wine_server_set_reply( req, buffer, count * sizeof(INPUT_RECORD) );
674 if ((ret = !wine_server_call_err( req )))
676 if (read) *read = count ? reply->read : 0;
684 /***********************************************************************
685 * GetNumberOfConsoleInputEvents (KERNEL32.@)
687 BOOL WINAPI GetNumberOfConsoleInputEvents( HANDLE handle, LPDWORD nrofevents )
690 SERVER_START_REQ( read_console_input )
692 req->handle = console_handle_unmap(handle);
694 if ((ret = !wine_server_call_err( req )))
696 if (nrofevents) *nrofevents = reply->read;
704 /******************************************************************************
707 * Helper function for ReadConsole, ReadConsoleInput and FlushConsoleInputBuffer
710 * 0 for error, 1 for no INPUT_RECORD ready, 2 with INPUT_RECORD ready
712 enum read_console_input_return {rci_error = 0, rci_timeout = 1, rci_gotone = 2};
713 static enum read_console_input_return read_console_input(HANDLE handle, LPINPUT_RECORD ir, DWORD timeout)
715 enum read_console_input_return ret;
717 if (WaitForSingleObject(GetConsoleInputWaitHandle(), timeout) != WAIT_OBJECT_0)
719 SERVER_START_REQ( read_console_input )
721 req->handle = console_handle_unmap(handle);
723 wine_server_set_reply( req, ir, sizeof(INPUT_RECORD) );
724 if (wine_server_call_err( req ) || !reply->read) ret = rci_error;
725 else ret = rci_gotone;
733 /***********************************************************************
734 * FlushConsoleInputBuffer (KERNEL32.@)
736 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
738 enum read_console_input_return last;
741 while ((last = read_console_input(handle, &ir, 0)) == rci_gotone);
743 return last == rci_timeout;
747 /***********************************************************************
748 * SetConsoleTitleA (KERNEL32.@)
750 BOOL WINAPI SetConsoleTitleA( LPCSTR title )
755 DWORD len = MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, NULL, 0 );
756 if (!(titleW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
757 MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, titleW, len );
758 ret = SetConsoleTitleW(titleW);
759 HeapFree(GetProcessHeap(), 0, titleW);
764 /***********************************************************************
765 * GetConsoleTitleA (KERNEL32.@)
767 DWORD WINAPI GetConsoleTitleA(LPSTR title, DWORD size)
769 WCHAR *ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * size);
773 ret = GetConsoleTitleW( ptr, size );
776 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, ret + 1, title, size, NULL, NULL);
779 HeapFree(GetProcessHeap(), 0, ptr);
784 /******************************************************************************
785 * GetConsoleTitleW [KERNEL32.@] Retrieves title string for console
788 * title [O] Address of buffer for title
789 * size [I] Size of buffer
792 * Success: Length of string copied
795 DWORD WINAPI GetConsoleTitleW(LPWSTR title, DWORD size)
799 SERVER_START_REQ( get_console_input_info )
802 wine_server_set_reply( req, title, (size-1) * sizeof(WCHAR) );
803 if (!wine_server_call_err( req ))
805 ret = wine_server_reply_size(reply) / sizeof(WCHAR);
814 /***********************************************************************
815 * GetLargestConsoleWindowSize (KERNEL32.@)
818 * This should return a COORD, but calling convention for returning
819 * structures is different between Windows and gcc on i386.
824 #undef GetLargestConsoleWindowSize
825 DWORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
835 #endif /* defined(__i386__) */
838 /***********************************************************************
839 * GetLargestConsoleWindowSize (KERNEL32.@)
842 * This should return a COORD, but calling convention for returning
843 * structures is different between Windows and gcc on i386.
848 COORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
855 #endif /* defined(__i386__) */
857 static WCHAR* S_EditString /* = NULL */;
858 static unsigned S_EditStrPos /* = 0 */;
860 /***********************************************************************
861 * FreeConsole (KERNEL32.@)
863 BOOL WINAPI FreeConsole(VOID)
867 SERVER_START_REQ(free_console)
869 ret = !wine_server_call_err( req );
875 /******************************************************************
876 * start_console_renderer
878 * helper for AllocConsole
879 * starts the renderer process
881 static BOOL start_console_renderer_helper(const char* appname, STARTUPINFOA* si,
886 PROCESS_INFORMATION pi;
888 /* FIXME: use dynamic allocation for most of the buffers below */
889 ret = snprintf(buffer, sizeof(buffer), "%s --use-event=%d", appname, (INT)hEvent);
890 if ((ret > -1) && (ret < sizeof(buffer)) &&
891 CreateProcessA(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS,
892 NULL, NULL, si, &pi))
894 if (WaitForSingleObject(hEvent, INFINITE) != WAIT_OBJECT_0) return FALSE;
896 TRACE("Started wineconsole pid=%08lx tid=%08lx\n",
897 pi.dwProcessId, pi.dwThreadId);
904 static BOOL start_console_renderer(STARTUPINFOA* si)
908 OBJECT_ATTRIBUTES attr;
911 attr.Length = sizeof(attr);
912 attr.RootDirectory = 0;
913 attr.Attributes = OBJ_INHERIT;
914 attr.ObjectName = NULL;
915 attr.SecurityDescriptor = NULL;
916 attr.SecurityQualityOfService = NULL;
918 NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, &attr, TRUE, FALSE);
919 if (!hEvent) return FALSE;
921 /* first try environment variable */
922 if ((p = getenv("WINECONSOLE")) != NULL)
924 ret = start_console_renderer_helper(p, si, hEvent);
926 ERR("Couldn't launch Wine console from WINECONSOLE env var (%s)... "
927 "trying default access\n", p);
930 /* then try the regular PATH */
932 ret = start_console_renderer_helper("wineconsole", si, hEvent);
938 /***********************************************************************
939 * AllocConsole (KERNEL32.@)
941 * creates an xterm with a pty to our program
943 BOOL WINAPI AllocConsole(void)
945 HANDLE handle_in = INVALID_HANDLE_VALUE;
946 HANDLE handle_out = INVALID_HANDLE_VALUE;
947 HANDLE handle_err = INVALID_HANDLE_VALUE;
948 STARTUPINFOA siCurrent;
949 STARTUPINFOA siConsole;
954 handle_in = CreateFileA( "CONIN$", GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
955 0, NULL, OPEN_EXISTING, 0, 0 );
957 if (handle_in != INVALID_HANDLE_VALUE)
959 /* we already have a console opened on this process, don't create a new one */
960 CloseHandle(handle_in);
964 GetStartupInfoA(&siCurrent);
966 memset(&siConsole, 0, sizeof(siConsole));
967 siConsole.cb = sizeof(siConsole);
968 /* setup a view arguments for wineconsole (it'll use them as default values) */
969 if (siCurrent.dwFlags & STARTF_USECOUNTCHARS)
971 siConsole.dwFlags |= STARTF_USECOUNTCHARS;
972 siConsole.dwXCountChars = siCurrent.dwXCountChars;
973 siConsole.dwYCountChars = siCurrent.dwYCountChars;
975 if (siCurrent.dwFlags & STARTF_USEFILLATTRIBUTE)
977 siConsole.dwFlags |= STARTF_USEFILLATTRIBUTE;
978 siConsole.dwFillAttribute = siCurrent.dwFillAttribute;
980 /* FIXME (should pass the unicode form) */
981 if (siCurrent.lpTitle)
982 siConsole.lpTitle = siCurrent.lpTitle;
983 else if (GetModuleFileNameA(0, buffer, sizeof(buffer)))
984 siConsole.lpTitle = buffer;
986 if (!start_console_renderer(&siConsole))
989 handle_in = CreateFileA( "CONIN$", GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
990 0, NULL, OPEN_EXISTING, 0, 0 );
991 if (handle_in == INVALID_HANDLE_VALUE) goto the_end;
993 handle_out = CreateFileA( "CONOUT$", GENERIC_READ|GENERIC_WRITE,
994 0, NULL, OPEN_EXISTING, 0, 0 );
995 if (handle_out == INVALID_HANDLE_VALUE) goto the_end;
997 if (!DuplicateHandle(GetCurrentProcess(), handle_out, GetCurrentProcess(), &handle_err,
998 0, TRUE, DUPLICATE_SAME_ACCESS))
1001 /* NT resets the STD_*_HANDLEs on console alloc */
1002 SetStdHandle(STD_INPUT_HANDLE, handle_in);
1003 SetStdHandle(STD_OUTPUT_HANDLE, handle_out);
1004 SetStdHandle(STD_ERROR_HANDLE, handle_err);
1006 SetLastError(ERROR_SUCCESS);
1011 ERR("Can't allocate console\n");
1012 if (handle_in != INVALID_HANDLE_VALUE) CloseHandle(handle_in);
1013 if (handle_out != INVALID_HANDLE_VALUE) CloseHandle(handle_out);
1014 if (handle_err != INVALID_HANDLE_VALUE) CloseHandle(handle_err);
1020 /***********************************************************************
1021 * ReadConsoleA (KERNEL32.@)
1023 BOOL WINAPI ReadConsoleA(HANDLE hConsoleInput, LPVOID lpBuffer, DWORD nNumberOfCharsToRead,
1024 LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1026 LPWSTR ptr = HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead * sizeof(WCHAR));
1030 if ((ret = ReadConsoleW(hConsoleInput, ptr, nNumberOfCharsToRead, &ncr, NULL)))
1031 ncr = WideCharToMultiByte(CP_ACP, 0, ptr, ncr, lpBuffer, nNumberOfCharsToRead, NULL, NULL);
1033 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = ncr;
1034 HeapFree(GetProcessHeap(), 0, ptr);
1039 /***********************************************************************
1040 * ReadConsoleW (KERNEL32.@)
1042 BOOL WINAPI ReadConsoleW(HANDLE hConsoleInput, LPVOID lpBuffer,
1043 DWORD nNumberOfCharsToRead, LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1046 LPWSTR xbuf = (LPWSTR)lpBuffer;
1049 TRACE("(%p,%p,%ld,%p,%p)\n",
1050 hConsoleInput, lpBuffer, nNumberOfCharsToRead, lpNumberOfCharsRead, lpReserved);
1052 if (!GetConsoleMode(hConsoleInput, &mode))
1055 if (mode & ENABLE_LINE_INPUT)
1057 if (!S_EditString || S_EditString[S_EditStrPos] == 0)
1059 if (S_EditString) HeapFree(GetProcessHeap(), 0, S_EditString);
1060 if (!(S_EditString = CONSOLE_Readline(hConsoleInput)))
1064 charsread = lstrlenW(&S_EditString[S_EditStrPos]);
1065 if (charsread > nNumberOfCharsToRead) charsread = nNumberOfCharsToRead;
1066 memcpy(xbuf, &S_EditString[S_EditStrPos], charsread * sizeof(WCHAR));
1067 S_EditStrPos += charsread;
1072 DWORD timeout = INFINITE;
1074 /* FIXME: should we read at least 1 char? The SDK does not say */
1075 /* wait for at least one available input record (it doesn't mean we'll have
1076 * chars stored in xbuf...)
1081 if (read_console_input(hConsoleInput, &ir, timeout) != rci_gotone) break;
1083 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown &&
1084 ir.Event.KeyEvent.uChar.UnicodeChar &&
1085 !(ir.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
1087 xbuf[charsread++] = ir.Event.KeyEvent.uChar.UnicodeChar;
1089 } while (charsread < nNumberOfCharsToRead);
1090 /* nothing has been read */
1091 if (timeout == INFINITE) return FALSE;
1094 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = charsread;
1100 /***********************************************************************
1101 * ReadConsoleInputW (KERNEL32.@)
1103 BOOL WINAPI ReadConsoleInputW(HANDLE hConsoleInput, LPINPUT_RECORD lpBuffer,
1104 DWORD nLength, LPDWORD lpNumberOfEventsRead)
1107 DWORD timeout = INFINITE;
1111 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = 0;
1115 /* loop until we get at least one event */
1116 while (read_console_input(hConsoleInput, &lpBuffer[idx], timeout) == rci_gotone &&
1120 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = idx;
1125 /******************************************************************************
1126 * WriteConsoleOutputCharacterW [KERNEL32.@] Copies character to consecutive
1127 * cells in the console screen buffer
1130 * hConsoleOutput [I] Handle to screen buffer
1131 * str [I] Pointer to buffer with chars to write
1132 * length [I] Number of cells to write to
1133 * coord [I] Coords of first cell
1134 * lpNumCharsWritten [O] Pointer to number of cells written
1141 BOOL WINAPI WriteConsoleOutputCharacterW( HANDLE hConsoleOutput, LPCWSTR str, DWORD length,
1142 COORD coord, LPDWORD lpNumCharsWritten )
1146 TRACE("(%p,%s,%ld,%dx%d,%p)\n", hConsoleOutput,
1147 debugstr_wn(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
1149 SERVER_START_REQ( write_console_output )
1151 req->handle = console_handle_unmap(hConsoleOutput);
1154 req->mode = CHAR_INFO_MODE_TEXT;
1156 wine_server_add_data( req, str, length * sizeof(WCHAR) );
1157 if ((ret = !wine_server_call_err( req )))
1159 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
1167 /******************************************************************************
1168 * SetConsoleTitleW [KERNEL32.@] Sets title bar string for console
1171 * title [I] Address of new title
1177 BOOL WINAPI SetConsoleTitleW(LPCWSTR title)
1181 SERVER_START_REQ( set_console_input_info )
1184 req->mask = SET_CONSOLE_INPUT_INFO_TITLE;
1185 wine_server_add_data( req, title, strlenW(title) * sizeof(WCHAR) );
1186 ret = !wine_server_call_err( req );
1193 /***********************************************************************
1194 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
1196 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1198 FIXME("(%p): stub\n", nrofbuttons);
1203 /******************************************************************************
1204 * SetConsoleInputExeNameW [KERNEL32.@]
1209 BOOL WINAPI SetConsoleInputExeNameW(LPCWSTR name)
1211 FIXME("(%s): stub!\n", debugstr_w(name));
1213 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1217 /******************************************************************************
1218 * SetConsoleInputExeNameA [KERNEL32.@]
1223 BOOL WINAPI SetConsoleInputExeNameA(LPCSTR name)
1225 int len = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
1226 LPWSTR xptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1229 if (!xptr) return FALSE;
1231 MultiByteToWideChar(CP_ACP, 0, name, -1, xptr, len);
1232 ret = SetConsoleInputExeNameW(xptr);
1233 HeapFree(GetProcessHeap(), 0, xptr);
1238 /******************************************************************
1239 * CONSOLE_DefaultHandler
1241 * Final control event handler
1243 static BOOL WINAPI CONSOLE_DefaultHandler(DWORD dwCtrlType)
1245 FIXME("Terminating process %lx on event %lx\n", GetCurrentProcessId(), dwCtrlType);
1247 /* should never go here */
1251 /******************************************************************************
1252 * SetConsoleCtrlHandler [KERNEL32.@] Adds function to calling process list
1255 * func [I] Address of handler function
1256 * add [I] Handler to add or remove
1263 * James Sutherland (JamesSutherland@gmx.de)
1264 * Added global variables console_ignore_ctrl_c and handlers[]
1265 * Does not yet do any error checking, or set LastError if failed.
1266 * This doesn't yet matter, since these handlers are not yet called...!
1269 struct ConsoleHandler {
1270 PHANDLER_ROUTINE handler;
1271 struct ConsoleHandler* next;
1274 static unsigned int CONSOLE_IgnoreCtrlC = 0; /* FIXME: this should be inherited somehow */
1275 static struct ConsoleHandler CONSOLE_DefaultConsoleHandler = {CONSOLE_DefaultHandler, NULL};
1276 static struct ConsoleHandler* CONSOLE_Handlers = &CONSOLE_DefaultConsoleHandler;
1278 static CRITICAL_SECTION CONSOLE_CritSect;
1279 static CRITICAL_SECTION_DEBUG critsect_debug =
1281 0, 0, &CONSOLE_CritSect,
1282 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
1283 0, 0, { 0, (DWORD)(__FILE__ ": CONSOLE_CritSect") }
1285 static CRITICAL_SECTION CONSOLE_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
1287 /*****************************************************************************/
1289 BOOL WINAPI SetConsoleCtrlHandler(PHANDLER_ROUTINE func, BOOL add)
1293 FIXME("(%p,%i) - no error checking or testing yet\n", func, add);
1297 CONSOLE_IgnoreCtrlC = add;
1301 struct ConsoleHandler* ch = HeapAlloc(GetProcessHeap(), 0, sizeof(struct ConsoleHandler));
1303 if (!ch) return FALSE;
1305 RtlEnterCriticalSection(&CONSOLE_CritSect);
1306 ch->next = CONSOLE_Handlers;
1307 CONSOLE_Handlers = ch;
1308 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1312 struct ConsoleHandler** ch;
1313 RtlEnterCriticalSection(&CONSOLE_CritSect);
1314 for (ch = &CONSOLE_Handlers; *ch; *ch = (*ch)->next)
1316 if ((*ch)->handler == func) break;
1320 struct ConsoleHandler* rch = *ch;
1323 if (rch == &CONSOLE_DefaultConsoleHandler)
1325 ERR("Who's trying to remove default handler???\n");
1332 HeapFree(GetProcessHeap(), 0, rch);
1337 WARN("Attempt to remove non-installed CtrlHandler %p\n", func);
1340 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1345 static WINE_EXCEPTION_FILTER(CONSOLE_CtrlEventHandler)
1347 TRACE("(%lx)\n", GetExceptionCode());
1348 return EXCEPTION_EXECUTE_HANDLER;
1351 static DWORD WINAPI CONSOLE_HandleCtrlCEntry(void* pmt)
1353 struct ConsoleHandler* ch;
1355 RtlEnterCriticalSection(&CONSOLE_CritSect);
1356 /* the debugger didn't continue... so, pass to ctrl handlers */
1357 for (ch = CONSOLE_Handlers; ch; ch = ch->next)
1359 if (ch->handler((DWORD)pmt)) break;
1361 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1365 /******************************************************************
1366 * CONSOLE_HandleCtrlC
1368 * Check whether the shall manipulate CtrlC events
1370 int CONSOLE_HandleCtrlC(unsigned sig)
1372 /* FIXME: better test whether a console is attached to this process ??? */
1373 extern unsigned CONSOLE_GetNumHistoryEntries(void);
1374 if (CONSOLE_GetNumHistoryEntries() == (unsigned)-1) return 0;
1375 if (CONSOLE_IgnoreCtrlC) return 1;
1377 /* try to pass the exception to the debugger
1378 * if it continues, there's nothing more to do
1379 * otherwise, we need to send the ctrl-event to the handlers
1383 RaiseException( DBG_CONTROL_C, 0, 0, NULL );
1385 __EXCEPT(CONSOLE_CtrlEventHandler)
1387 /* Create a separate thread to signal all the events. This would allow to
1388 * synchronize between setting the handlers and actually calling them
1390 CreateThread(NULL, 0, CONSOLE_HandleCtrlCEntry, (void*)CTRL_C_EVENT, 0, NULL);
1396 /******************************************************************************
1397 * GenerateConsoleCtrlEvent [KERNEL32.@] Simulate a CTRL-C or CTRL-BREAK
1400 * dwCtrlEvent [I] Type of event
1401 * dwProcessGroupID [I] Process group ID to send event to
1405 * Failure: False (and *should* [but doesn't] set LastError)
1407 BOOL WINAPI GenerateConsoleCtrlEvent(DWORD dwCtrlEvent,
1408 DWORD dwProcessGroupID)
1412 TRACE("(%ld, %ld)\n", dwCtrlEvent, dwProcessGroupID);
1414 if (dwCtrlEvent != CTRL_C_EVENT && dwCtrlEvent != CTRL_BREAK_EVENT)
1416 ERR("Invalid event %ld for PGID %ld\n", dwCtrlEvent, dwProcessGroupID);
1420 SERVER_START_REQ( send_console_signal )
1422 req->signal = dwCtrlEvent;
1423 req->group_id = dwProcessGroupID;
1424 ret = !wine_server_call_err( req );
1432 /******************************************************************************
1433 * CreateConsoleScreenBuffer [KERNEL32.@] Creates a console screen buffer
1436 * dwDesiredAccess [I] Access flag
1437 * dwShareMode [I] Buffer share mode
1438 * sa [I] Security attributes
1439 * dwFlags [I] Type of buffer to create
1440 * lpScreenBufferData [I] Reserved
1443 * Should call SetLastError
1446 * Success: Handle to new console screen buffer
1447 * Failure: INVALID_HANDLE_VALUE
1449 HANDLE WINAPI CreateConsoleScreenBuffer(DWORD dwDesiredAccess, DWORD dwShareMode,
1450 LPSECURITY_ATTRIBUTES sa, DWORD dwFlags,
1451 LPVOID lpScreenBufferData)
1453 HANDLE ret = INVALID_HANDLE_VALUE;
1455 TRACE("(%ld,%ld,%p,%ld,%p)\n",
1456 dwDesiredAccess, dwShareMode, sa, dwFlags, lpScreenBufferData);
1458 if (dwFlags != CONSOLE_TEXTMODE_BUFFER || lpScreenBufferData != NULL)
1460 SetLastError(ERROR_INVALID_PARAMETER);
1461 return INVALID_HANDLE_VALUE;
1464 SERVER_START_REQ(create_console_output)
1467 req->access = dwDesiredAccess;
1468 req->share = dwShareMode;
1469 req->inherit = (sa && sa->bInheritHandle);
1470 if (!wine_server_call_err( req )) ret = reply->handle_out;
1478 /***********************************************************************
1479 * GetConsoleScreenBufferInfo (KERNEL32.@)
1481 BOOL WINAPI GetConsoleScreenBufferInfo(HANDLE hConsoleOutput, LPCONSOLE_SCREEN_BUFFER_INFO csbi)
1485 SERVER_START_REQ(get_console_output_info)
1487 req->handle = console_handle_unmap(hConsoleOutput);
1488 if ((ret = !wine_server_call_err( req )))
1490 csbi->dwSize.X = reply->width;
1491 csbi->dwSize.Y = reply->height;
1492 csbi->dwCursorPosition.X = reply->cursor_x;
1493 csbi->dwCursorPosition.Y = reply->cursor_y;
1494 csbi->wAttributes = reply->attr;
1495 csbi->srWindow.Left = reply->win_left;
1496 csbi->srWindow.Right = reply->win_right;
1497 csbi->srWindow.Top = reply->win_top;
1498 csbi->srWindow.Bottom = reply->win_bottom;
1499 csbi->dwMaximumWindowSize.X = reply->max_width;
1500 csbi->dwMaximumWindowSize.Y = reply->max_height;
1509 /******************************************************************************
1510 * SetConsoleActiveScreenBuffer [KERNEL32.@] Sets buffer to current console
1516 BOOL WINAPI SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput)
1520 TRACE("(%p)\n", hConsoleOutput);
1522 SERVER_START_REQ( set_console_input_info )
1525 req->mask = SET_CONSOLE_INPUT_INFO_ACTIVE_SB;
1526 req->active_sb = hConsoleOutput;
1527 ret = !wine_server_call_err( req );
1534 /***********************************************************************
1535 * GetConsoleMode (KERNEL32.@)
1537 BOOL WINAPI GetConsoleMode(HANDLE hcon, LPDWORD mode)
1541 SERVER_START_REQ(get_console_mode)
1543 req->handle = console_handle_unmap(hcon);
1544 ret = !wine_server_call_err( req );
1545 if (ret && mode) *mode = reply->mode;
1552 /******************************************************************************
1553 * SetConsoleMode [KERNEL32.@] Sets input mode of console's input buffer
1556 * hcon [I] Handle to console input or screen buffer
1557 * mode [I] Input or output mode to set
1564 * ENABLE_PROCESSED_INPUT 0x01
1565 * ENABLE_LINE_INPUT 0x02
1566 * ENABLE_ECHO_INPUT 0x04
1567 * ENABLE_WINDOW_INPUT 0x08
1568 * ENABLE_MOUSE_INPUT 0x10
1570 BOOL WINAPI SetConsoleMode(HANDLE hcon, DWORD mode)
1574 SERVER_START_REQ(set_console_mode)
1576 req->handle = console_handle_unmap(hcon);
1578 ret = !wine_server_call_err( req );
1581 /* FIXME: when resetting a console input to editline mode, I think we should
1582 * empty the S_EditString buffer
1585 TRACE("(%p,%lx) retval == %d\n", hcon, mode, ret);
1591 /******************************************************************
1592 * CONSOLE_WriteChars
1594 * WriteConsoleOutput helper: hides server call semantics
1595 * writes a string at a given pos with standard attribute
1597 int CONSOLE_WriteChars(HANDLE hCon, LPCWSTR lpBuffer, int nc, COORD* pos)
1603 SERVER_START_REQ( write_console_output )
1605 req->handle = console_handle_unmap(hCon);
1608 req->mode = CHAR_INFO_MODE_TEXTSTDATTR;
1610 wine_server_add_data( req, lpBuffer, nc * sizeof(WCHAR) );
1611 if (!wine_server_call_err( req )) written = reply->written;
1615 if (written > 0) pos->X += written;
1619 /******************************************************************
1622 * WriteConsoleOutput helper: handles passing to next line (+scrolling if necessary)
1625 static int next_line(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi)
1631 csbi->dwCursorPosition.X = 0;
1632 csbi->dwCursorPosition.Y++;
1634 if (csbi->dwCursorPosition.Y < csbi->dwSize.Y) return 1;
1637 src.Bottom = csbi->dwSize.Y - 1;
1639 src.Right = csbi->dwSize.X - 1;
1644 ci.Attributes = csbi->wAttributes;
1645 ci.Char.UnicodeChar = ' ';
1647 csbi->dwCursorPosition.Y--;
1648 if (!ScrollConsoleScreenBufferW(hCon, &src, NULL, dst, &ci))
1653 /******************************************************************
1656 * WriteConsoleOutput helper: writes a block of non special characters
1657 * Block can spread on several lines, and wrapping, if needed, is
1661 static int write_block(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi,
1662 DWORD mode, LPWSTR ptr, int len)
1664 int blk; /* number of chars to write on current line */
1665 int done; /* number of chars already written */
1667 if (len <= 0) return 1;
1669 if (mode & ENABLE_WRAP_AT_EOL_OUTPUT) /* writes remaining on next line */
1671 for (done = 0; done < len; done += blk)
1673 blk = min(len - done, csbi->dwSize.X - csbi->dwCursorPosition.X);
1675 if (CONSOLE_WriteChars(hCon, ptr + done, blk, &csbi->dwCursorPosition) != blk)
1677 if (csbi->dwCursorPosition.X == csbi->dwSize.X && !next_line(hCon, csbi))
1683 int pos = csbi->dwCursorPosition.X;
1684 /* FIXME: we could reduce the number of loops
1685 * but, in most cases we wouldn't gain lots of time (it would only
1686 * happen if we're asked to overwrite more than twice the part of the line,
1689 for (blk = done = 0; done < len; done += blk)
1691 blk = min(len - done, csbi->dwSize.X - csbi->dwCursorPosition.X);
1693 csbi->dwCursorPosition.X = pos;
1694 if (CONSOLE_WriteChars(hCon, ptr + done, blk, &csbi->dwCursorPosition) != blk)
1702 /***********************************************************************
1703 * WriteConsoleW (KERNEL32.@)
1705 BOOL WINAPI WriteConsoleW(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
1706 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
1710 WCHAR* psz = (WCHAR*)lpBuffer;
1711 CONSOLE_SCREEN_BUFFER_INFO csbi;
1714 TRACE("%p %s %ld %p %p\n",
1715 hConsoleOutput, debugstr_wn(lpBuffer, nNumberOfCharsToWrite),
1716 nNumberOfCharsToWrite, lpNumberOfCharsWritten, lpReserved);
1718 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
1720 if (!GetConsoleMode(hConsoleOutput, &mode) ||
1721 !GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
1724 if (mode & ENABLE_PROCESSED_OUTPUT)
1728 for (i = 0; i < nNumberOfCharsToWrite; i++)
1732 case '\b': case '\t': case '\n': case '\a': case '\r':
1733 /* don't handle here the i-th char... done below */
1734 if ((k = i - first) > 0)
1736 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
1746 if (csbi.dwCursorPosition.X > 0) csbi.dwCursorPosition.X--;
1750 WCHAR tmp[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
1752 if (!write_block(hConsoleOutput, &csbi, mode, tmp,
1753 ((csbi.dwCursorPosition.X + 8) & ~7) - csbi.dwCursorPosition.X))
1758 next_line(hConsoleOutput, &csbi);
1764 csbi.dwCursorPosition.X = 0;
1772 /* write the remaining block (if any) if processed output is enabled, or the
1773 * entire buffer otherwise
1775 if ((k = nNumberOfCharsToWrite - first) > 0)
1777 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
1783 SetConsoleCursorPosition(hConsoleOutput, csbi.dwCursorPosition);
1784 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = nw;
1789 /***********************************************************************
1790 * WriteConsoleA (KERNEL32.@)
1792 BOOL WINAPI WriteConsoleA(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
1793 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
1799 n = MultiByteToWideChar(CP_ACP, 0, lpBuffer, nNumberOfCharsToWrite, NULL, 0);
1801 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
1802 xstring = HeapAlloc(GetProcessHeap(), 0, n * sizeof(WCHAR));
1803 if (!xstring) return 0;
1805 MultiByteToWideChar(CP_ACP, 0, lpBuffer, nNumberOfCharsToWrite, xstring, n);
1807 ret = WriteConsoleW(hConsoleOutput, xstring, n, lpNumberOfCharsWritten, 0);
1809 HeapFree(GetProcessHeap(), 0, xstring);
1814 /******************************************************************************
1815 * SetConsoleCursorPosition [KERNEL32.@]
1816 * Sets the cursor position in console
1819 * hConsoleOutput [I] Handle of console screen buffer
1820 * dwCursorPosition [I] New cursor position coordinates
1824 BOOL WINAPI SetConsoleCursorPosition(HANDLE hcon, COORD pos)
1827 CONSOLE_SCREEN_BUFFER_INFO csbi;
1831 TRACE("%p %d %d\n", hcon, pos.X, pos.Y);
1833 SERVER_START_REQ(set_console_output_info)
1835 req->handle = console_handle_unmap(hcon);
1836 req->cursor_x = pos.X;
1837 req->cursor_y = pos.Y;
1838 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_POS;
1839 ret = !wine_server_call_err( req );
1843 if (!ret || !GetConsoleScreenBufferInfo(hcon, &csbi))
1846 /* if cursor is no longer visible, scroll the visible window... */
1847 w = csbi.srWindow.Right - csbi.srWindow.Left + 1;
1848 h = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
1849 if (pos.X < csbi.srWindow.Left)
1851 csbi.srWindow.Left = min(pos.X, csbi.dwSize.X - w);
1854 else if (pos.X > csbi.srWindow.Right)
1856 csbi.srWindow.Left = max(pos.X, w) - w + 1;
1859 csbi.srWindow.Right = csbi.srWindow.Left + w - 1;
1861 if (pos.Y < csbi.srWindow.Top)
1863 csbi.srWindow.Top = min(pos.Y, csbi.dwSize.Y - h);
1866 else if (pos.Y > csbi.srWindow.Bottom)
1868 csbi.srWindow.Top = max(pos.Y, h) - h + 1;
1871 csbi.srWindow.Bottom = csbi.srWindow.Top + h - 1;
1873 ret = (do_move) ? SetConsoleWindowInfo(hcon, TRUE, &csbi.srWindow) : TRUE;
1878 /******************************************************************************
1879 * GetConsoleCursorInfo [KERNEL32.@] Gets size and visibility of console
1882 * hcon [I] Handle to console screen buffer
1883 * cinfo [O] Address of cursor information
1889 BOOL WINAPI GetConsoleCursorInfo(HANDLE hcon, LPCONSOLE_CURSOR_INFO cinfo)
1893 SERVER_START_REQ(get_console_output_info)
1895 req->handle = console_handle_unmap(hcon);
1896 ret = !wine_server_call_err( req );
1899 cinfo->dwSize = reply->cursor_size;
1900 cinfo->bVisible = reply->cursor_visible;
1908 /******************************************************************************
1909 * SetConsoleCursorInfo [KERNEL32.@] Sets size and visibility of cursor
1912 * hcon [I] Handle to console screen buffer
1913 * cinfo [I] Address of cursor information
1918 BOOL WINAPI SetConsoleCursorInfo(HANDLE hCon, LPCONSOLE_CURSOR_INFO cinfo)
1922 SERVER_START_REQ(set_console_output_info)
1924 req->handle = console_handle_unmap(hCon);
1925 req->cursor_size = cinfo->dwSize;
1926 req->cursor_visible = cinfo->bVisible;
1927 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM;
1928 ret = !wine_server_call_err( req );
1935 /******************************************************************************
1936 * SetConsoleWindowInfo [KERNEL32.@] Sets size and position of console
1939 * hcon [I] Handle to console screen buffer
1940 * bAbsolute [I] Coordinate type flag
1941 * window [I] Address of new window rectangle
1946 BOOL WINAPI SetConsoleWindowInfo(HANDLE hCon, BOOL bAbsolute, LPSMALL_RECT window)
1948 SMALL_RECT p = *window;
1953 CONSOLE_SCREEN_BUFFER_INFO csbi;
1954 if (!GetConsoleScreenBufferInfo(hCon, &csbi))
1956 p.Left += csbi.srWindow.Left;
1957 p.Top += csbi.srWindow.Top;
1958 p.Right += csbi.srWindow.Left;
1959 p.Bottom += csbi.srWindow.Top;
1961 SERVER_START_REQ(set_console_output_info)
1963 req->handle = console_handle_unmap(hCon);
1964 req->win_left = p.Left;
1965 req->win_top = p.Top;
1966 req->win_right = p.Right;
1967 req->win_bottom = p.Bottom;
1968 req->mask = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
1969 ret = !wine_server_call_err( req );
1977 /******************************************************************************
1978 * SetConsoleTextAttribute [KERNEL32.@] Sets colors for text
1980 * Sets the foreground and background color attributes of characters
1981 * written to the screen buffer.
1987 BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttr)
1991 SERVER_START_REQ(set_console_output_info)
1993 req->handle = console_handle_unmap(hConsoleOutput);
1995 req->mask = SET_CONSOLE_OUTPUT_INFO_ATTR;
1996 ret = !wine_server_call_err( req );
2003 /******************************************************************************
2004 * SetConsoleScreenBufferSize [KERNEL32.@] Changes size of console
2007 * hConsoleOutput [I] Handle to console screen buffer
2008 * dwSize [I] New size in character rows and cols
2014 BOOL WINAPI SetConsoleScreenBufferSize(HANDLE hConsoleOutput, COORD dwSize)
2018 SERVER_START_REQ(set_console_output_info)
2020 req->handle = console_handle_unmap(hConsoleOutput);
2021 req->width = dwSize.X;
2022 req->height = dwSize.Y;
2023 req->mask = SET_CONSOLE_OUTPUT_INFO_SIZE;
2024 ret = !wine_server_call_err( req );
2031 /******************************************************************************
2032 * ScrollConsoleScreenBufferA [KERNEL32.@]
2035 BOOL WINAPI ScrollConsoleScreenBufferA(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2036 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2041 ciw.Attributes = lpFill->Attributes;
2042 MultiByteToWideChar(CP_ACP, 0, &lpFill->Char.AsciiChar, 1, &ciw.Char.UnicodeChar, 1);
2044 return ScrollConsoleScreenBufferW(hConsoleOutput, lpScrollRect, lpClipRect,
2045 dwDestOrigin, &ciw);
2048 /******************************************************************
2049 * CONSOLE_FillLineUniform
2051 * Helper function for ScrollConsoleScreenBufferW
2052 * Fills a part of a line with a constant character info
2054 void CONSOLE_FillLineUniform(HANDLE hConsoleOutput, int i, int j, int len, LPCHAR_INFO lpFill)
2056 SERVER_START_REQ( fill_console_output )
2058 req->handle = console_handle_unmap(hConsoleOutput);
2059 req->mode = CHAR_INFO_MODE_TEXTATTR;
2064 req->data.ch = lpFill->Char.UnicodeChar;
2065 req->data.attr = lpFill->Attributes;
2066 wine_server_call_err( req );
2071 /******************************************************************************
2072 * ScrollConsoleScreenBufferW [KERNEL32.@]
2076 BOOL WINAPI ScrollConsoleScreenBufferW(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2077 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2085 CONSOLE_SCREEN_BUFFER_INFO csbi;
2089 TRACE("(%p,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", hConsoleOutput,
2090 lpScrollRect->Left, lpScrollRect->Top,
2091 lpScrollRect->Right, lpScrollRect->Bottom,
2092 lpClipRect->Left, lpClipRect->Top,
2093 lpClipRect->Right, lpClipRect->Bottom,
2094 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2096 TRACE("(%p,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", hConsoleOutput,
2097 lpScrollRect->Left, lpScrollRect->Top,
2098 lpScrollRect->Right, lpScrollRect->Bottom,
2099 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2101 if (!GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
2104 /* step 1: get dst rect */
2105 dst.Left = dwDestOrigin.X;
2106 dst.Top = dwDestOrigin.Y;
2107 dst.Right = dst.Left + (lpScrollRect->Right - lpScrollRect->Left);
2108 dst.Bottom = dst.Top + (lpScrollRect->Bottom - lpScrollRect->Top);
2110 /* step 2a: compute the final clip rect (optional passed clip and screen buffer limits */
2113 clip.Left = max(0, lpClipRect->Left);
2114 clip.Right = min(csbi.dwSize.X - 1, lpClipRect->Right);
2115 clip.Top = max(0, lpClipRect->Top);
2116 clip.Bottom = min(csbi.dwSize.Y - 1, lpClipRect->Bottom);
2121 clip.Right = csbi.dwSize.X - 1;
2123 clip.Bottom = csbi.dwSize.Y - 1;
2125 if (clip.Left > clip.Right || clip.Top > clip.Bottom) return FALSE;
2127 /* step 2b: clip dst rect */
2128 if (dst.Left < clip.Left ) dst.Left = clip.Left;
2129 if (dst.Top < clip.Top ) dst.Top = clip.Top;
2130 if (dst.Right > clip.Right ) dst.Right = clip.Right;
2131 if (dst.Bottom > clip.Bottom) dst.Bottom = clip.Bottom;
2133 /* step 3: transfer the bits */
2134 SERVER_START_REQ(move_console_output)
2136 req->handle = console_handle_unmap(hConsoleOutput);
2137 req->x_src = lpScrollRect->Left;
2138 req->y_src = lpScrollRect->Top;
2139 req->x_dst = dst.Left;
2140 req->y_dst = dst.Top;
2141 req->w = dst.Right - dst.Left + 1;
2142 req->h = dst.Bottom - dst.Top + 1;
2143 ret = !wine_server_call_err( req );
2147 if (!ret) return FALSE;
2149 /* step 4: clean out the exposed part */
2151 /* have to write cell [i,j] if it is not in dst rect (because it has already
2152 * been written to by the scroll) and is in clip (we shall not write
2155 for (j = max(lpScrollRect->Top, clip.Top); j <= min(lpScrollRect->Bottom, clip.Bottom); j++)
2157 inside = dst.Top <= j && j <= dst.Bottom;
2159 for (i = max(lpScrollRect->Left, clip.Left); i <= min(lpScrollRect->Right, clip.Right); i++)
2161 if (inside && dst.Left <= i && i <= dst.Right)
2165 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2171 if (start == -1) start = i;
2175 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2182 /* ====================================================================
2184 * Console manipulation functions
2186 * ====================================================================*/
2188 /* some missing functions...
2189 * FIXME: those are likely to be defined as undocumented function in kernel32 (or part of them)
2190 * should get the right API and implement them
2191 * GetConsoleCommandHistory[AW] (dword dword dword)
2192 * GetConsoleCommandHistoryLength[AW]
2193 * SetConsoleCommandHistoryMode
2194 * SetConsoleNumberOfCommands[AW]
2196 int CONSOLE_GetHistory(int idx, WCHAR* buf, int buf_len)
2200 SERVER_START_REQ( get_console_input_history )
2204 if (buf && buf_len > 1)
2206 wine_server_set_reply( req, buf, (buf_len - 1) * sizeof(WCHAR) );
2208 if (!wine_server_call_err( req ))
2210 if (buf) buf[wine_server_reply_size(reply) / sizeof(WCHAR)] = 0;
2211 len = reply->total / sizeof(WCHAR) + 1;
2218 /******************************************************************
2219 * CONSOLE_AppendHistory
2223 BOOL CONSOLE_AppendHistory(const WCHAR* ptr)
2225 size_t len = strlenW(ptr);
2228 while (len && (ptr[len - 1] == '\n' || ptr[len - 1] == '\r')) len--;
2230 SERVER_START_REQ( append_console_input_history )
2233 wine_server_add_data( req, ptr, len * sizeof(WCHAR) );
2234 ret = !wine_server_call_err( req );
2240 /******************************************************************
2241 * CONSOLE_GetNumHistoryEntries
2245 unsigned CONSOLE_GetNumHistoryEntries(void)
2248 SERVER_START_REQ(get_console_input_info)
2251 if (!wine_server_call_err( req )) ret = reply->history_index;
2257 /******************************************************************
2258 * CONSOLE_GetEditionMode
2262 BOOL CONSOLE_GetEditionMode(HANDLE hConIn, int* mode)
2264 unsigned ret = FALSE;
2265 SERVER_START_REQ(get_console_input_info)
2267 req->handle = console_handle_unmap(hConIn);
2268 if ((ret = !wine_server_call_err( req )))
2269 *mode = reply->edition_mode;