mfreadwrite/reader: Add missing allocation check (Coverity).
[wine/zf.git] / dlls / kernelbase / console.c
blob6a3bfdeba65d4ed96d0ce7fc9e69a2cf356a2b90
1 /*
2 * Win32 console functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
5 * Copyright 1997 Karl Garrison
6 * Copyright 1998 John Richardson
7 * Copyright 1998 Marcus Meissner
8 * Copyright 2001,2002,2004,2005,2010 Eric Pouech
9 * Copyright 2001 Alexandre Julliard
10 * Copyright 2020 Jacek Caban for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <limits.h>
32 #define NONAMELESSUNION
33 #include "ntstatus.h"
34 #define WIN32_NO_STATUS
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winnls.h"
38 #include "winerror.h"
39 #include "wincon.h"
40 #include "winternl.h"
41 #include "wine/condrv.h"
42 #include "wine/exception.h"
43 #include "wine/debug.h"
44 #include "kernelbase.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(console);
49 static CRITICAL_SECTION console_section;
50 static CRITICAL_SECTION_DEBUG critsect_debug =
52 0, 0, &console_section,
53 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
54 0, 0, { (DWORD_PTR)(__FILE__ ": console_section") }
56 static CRITICAL_SECTION console_section = { &critsect_debug, -1, 0, 0, 0, 0 };
58 static HANDLE console_connection;
59 static unsigned int console_flags;
61 #define CONSOLE_INPUT_HANDLE 0x01
62 #define CONSOLE_OUTPUT_HANDLE 0x02
63 #define CONSOLE_ERROR_HANDLE 0x04
65 static WCHAR input_exe[MAX_PATH + 1];
67 struct ctrl_handler
69 PHANDLER_ROUTINE func;
70 struct ctrl_handler *next;
73 static BOOL WINAPI default_ctrl_handler( DWORD type )
75 FIXME( "Terminating process %x on event %x\n", GetCurrentProcessId(), type );
76 RtlExitUserProcess( 0 );
77 return TRUE;
80 static struct ctrl_handler default_handler = { default_ctrl_handler, NULL };
81 static struct ctrl_handler *ctrl_handlers = &default_handler;
83 static BOOL console_ioctl( HANDLE handle, DWORD code, void *in_buff, DWORD in_count,
84 void *out_buff, DWORD out_count, DWORD *read )
86 IO_STATUS_BLOCK io;
87 NTSTATUS status;
89 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io, code, in_buff, in_count,
90 out_buff, out_count );
91 switch( status )
93 case STATUS_SUCCESS:
94 if (read) *read = io.Information;
95 return TRUE;
96 case STATUS_INVALID_PARAMETER:
97 break;
98 default:
99 status = STATUS_INVALID_HANDLE;
100 break;
102 if (read) *read = 0;
103 return set_ntstatus( status );
106 /* map input records to ASCII */
107 static void input_records_WtoA( INPUT_RECORD *buffer, int count )
109 UINT cp = GetConsoleCP();
110 int i;
111 char ch;
113 for (i = 0; i < count; i++)
115 if (buffer[i].EventType != KEY_EVENT) continue;
116 WideCharToMultiByte( cp, 0, &buffer[i].Event.KeyEvent.uChar.UnicodeChar, 1, &ch, 1, NULL, NULL );
117 buffer[i].Event.KeyEvent.uChar.AsciiChar = ch;
121 /* map input records to Unicode */
122 static void input_records_AtoW( INPUT_RECORD *buffer, int count )
124 UINT cp = GetConsoleCP();
125 int i;
126 WCHAR ch;
128 for (i = 0; i < count; i++)
130 if (buffer[i].EventType != KEY_EVENT) continue;
131 MultiByteToWideChar( cp, 0, &buffer[i].Event.KeyEvent.uChar.AsciiChar, 1, &ch, 1 );
132 buffer[i].Event.KeyEvent.uChar.UnicodeChar = ch;
136 /* map char infos to ASCII */
137 static void char_info_WtoA( UINT cp, CHAR_INFO *buffer, int count )
139 char ch;
141 while (count-- > 0)
143 WideCharToMultiByte( cp, 0, &buffer->Char.UnicodeChar, 1, &ch, 1, NULL, NULL );
144 buffer->Char.AsciiChar = ch;
145 buffer++;
149 /* map char infos to Unicode */
150 static void char_info_AtoW( CHAR_INFO *buffer, int count )
152 UINT cp = GetConsoleOutputCP();
153 WCHAR ch;
155 while (count-- > 0)
157 MultiByteToWideChar( cp, 0, &buffer->Char.AsciiChar, 1, &ch, 1 );
158 buffer->Char.UnicodeChar = ch;
159 buffer++;
163 /* helper function for GetLargestConsoleWindowSize */
164 static COORD get_largest_console_window_size( HANDLE handle )
166 struct condrv_output_info info;
167 COORD c = { 0, 0 };
169 if (!console_ioctl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &info, sizeof(info), NULL ))
170 return c;
172 c.X = info.max_width;
173 c.Y = info.max_height;
174 TRACE( "(%p), returning %dx%d\n", handle, c.X, c.Y );
175 return c;
178 static HANDLE create_console_server( void )
180 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
181 UNICODE_STRING string;
182 IO_STATUS_BLOCK iosb;
183 HANDLE handle;
184 NTSTATUS status;
186 RtlInitUnicodeString( &string, L"\\Device\\ConDrv\\Server" );
187 attr.ObjectName = &string;
188 attr.Attributes = OBJ_INHERIT;
189 status = NtCreateFile( &handle, FILE_WRITE_PROPERTIES | FILE_READ_PROPERTIES | SYNCHRONIZE,
190 &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN,
191 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
192 return set_ntstatus( status ) ? handle : NULL;
195 static HANDLE create_console_reference( HANDLE root )
197 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
198 UNICODE_STRING string;
199 IO_STATUS_BLOCK iosb;
200 HANDLE handle;
201 NTSTATUS status;
203 RtlInitUnicodeString( &string, L"Reference" );
204 attr.RootDirectory = root;
205 attr.ObjectName = &string;
206 status = NtCreateFile( &handle, FILE_READ_DATA | FILE_WRITE_DATA | FILE_WRITE_PROPERTIES |
207 FILE_READ_PROPERTIES | SYNCHRONIZE, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL,
208 0, FILE_OPEN, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
209 return set_ntstatus( status ) ? handle : NULL;
212 static BOOL create_console_connection( HANDLE root )
214 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
215 UNICODE_STRING string;
216 IO_STATUS_BLOCK iosb;
217 NTSTATUS status;
219 RtlInitUnicodeString( &string, root ? L"Connection" : L"\\Device\\ConDrv\\Connection" );
220 attr.RootDirectory = root;
221 attr.ObjectName = &string;
222 status = NtCreateFile( &console_connection, FILE_WRITE_PROPERTIES | FILE_READ_PROPERTIES | SYNCHRONIZE, &attr,
223 &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_NON_DIRECTORY_FILE, NULL, 0 );
224 return set_ntstatus( status );
227 static BOOL init_console_std_handles( BOOL override_all )
229 HANDLE std_out = NULL, std_err = NULL, handle;
230 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
231 IO_STATUS_BLOCK iosb;
232 UNICODE_STRING name;
233 NTSTATUS status;
235 attr.ObjectName = &name;
236 attr.Attributes = OBJ_INHERIT;
238 if (override_all || !GetStdHandle( STD_INPUT_HANDLE ))
240 RtlInitUnicodeString( &name, L"\\Device\\ConDrv\\Input" );
241 status = NtCreateFile( &handle, FILE_READ_DATA | FILE_WRITE_DATA | SYNCHRONIZE | FILE_READ_ATTRIBUTES |
242 FILE_WRITE_ATTRIBUTES, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL,
243 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_CREATE,
244 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
245 if (!set_ntstatus( status )) return FALSE;
246 console_flags |= CONSOLE_INPUT_HANDLE;
247 SetStdHandle( STD_INPUT_HANDLE, handle );
250 if (!override_all)
252 std_out = GetStdHandle( STD_OUTPUT_HANDLE );
253 std_err = GetStdHandle( STD_ERROR_HANDLE );
254 if (std_out && std_err) return TRUE;
257 RtlInitUnicodeString( &name, L"\\Device\\ConDrv\\Output" );
258 status = NtCreateFile( &handle, FILE_READ_DATA | FILE_WRITE_DATA | SYNCHRONIZE | FILE_READ_ATTRIBUTES |
259 FILE_WRITE_ATTRIBUTES, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL,
260 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_CREATE,
261 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
262 if (!set_ntstatus( status )) return FALSE;
263 if (!std_out)
265 console_flags |= CONSOLE_OUTPUT_HANDLE;
266 SetStdHandle( STD_OUTPUT_HANDLE, handle );
269 if (!std_err)
271 if (!std_out && !DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
272 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS ))
273 return FALSE;
274 console_flags |= CONSOLE_ERROR_HANDLE;
275 SetStdHandle( STD_ERROR_HANDLE, handle );
278 return TRUE;
281 /******************************************************************
282 * AttachConsole (kernelbase.@)
284 BOOL WINAPI DECLSPEC_HOTPATCH AttachConsole( DWORD pid )
286 BOOL ret;
288 TRACE( "(%x)\n", pid );
290 RtlEnterCriticalSection( &console_section );
292 if (RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle)
294 RtlLeaveCriticalSection( &console_section );
295 WARN( "console already attached\n" );
296 SetLastError( ERROR_ACCESS_DENIED );
297 return FALSE;
300 ret = create_console_connection( NULL ) &&
301 console_ioctl( console_connection, IOCTL_CONDRV_BIND_PID, &pid, sizeof(pid), NULL, 0, NULL );
302 if (ret)
304 RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle = create_console_reference( console_connection );
305 if (RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle)
307 STARTUPINFOW si;
308 GetStartupInfoW( &si );
309 init_console_std_handles( !(si.dwFlags & STARTF_USESTDHANDLES) );
311 else ret = FALSE;
314 if (!ret) FreeConsole();
315 RtlLeaveCriticalSection( &console_section );
316 return ret;
320 /******************************************************************
321 * AllocConsole (kernelbase.@)
323 BOOL WINAPI AllocConsole(void)
325 SECURITY_ATTRIBUTES inheritable_attr = { sizeof(inheritable_attr), NULL, TRUE };
326 STARTUPINFOW app_si, console_si;
327 HANDLE server, console = NULL;
328 WCHAR buffer[1024], cmd[256], conhost_path[MAX_PATH];
329 PROCESS_INFORMATION pi;
330 void *redir;
331 BOOL ret;
333 TRACE("()\n");
335 RtlEnterCriticalSection( &console_section );
337 if (RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle)
339 /* we already have a console opened on this process, don't create a new one */
340 RtlLeaveCriticalSection( &console_section );
341 SetLastError( ERROR_ACCESS_DENIED );
342 return FALSE;
345 if (!(server = create_console_server()) || !(console = create_console_reference( server ))) goto error;
347 GetStartupInfoW(&app_si);
349 memset(&console_si, 0, sizeof(console_si));
350 console_si.cb = sizeof(console_si);
351 /* setup a view arguments for conhost (it'll use them as default values) */
352 if (app_si.dwFlags & STARTF_USECOUNTCHARS)
354 console_si.dwFlags |= STARTF_USECOUNTCHARS;
355 console_si.dwXCountChars = app_si.dwXCountChars;
356 console_si.dwYCountChars = app_si.dwYCountChars;
358 if (app_si.dwFlags & STARTF_USEFILLATTRIBUTE)
360 console_si.dwFlags |= STARTF_USEFILLATTRIBUTE;
361 console_si.dwFillAttribute = app_si.dwFillAttribute;
363 if (app_si.dwFlags & STARTF_USESHOWWINDOW)
365 console_si.dwFlags |= STARTF_USESHOWWINDOW;
366 console_si.wShowWindow = app_si.wShowWindow;
368 if (app_si.lpTitle)
369 console_si.lpTitle = app_si.lpTitle;
370 else if (GetModuleFileNameW(0, buffer, ARRAY_SIZE(buffer)))
372 buffer[ARRAY_SIZE(buffer) - 1] = 0;
373 console_si.lpTitle = buffer;
376 swprintf( conhost_path, ARRAY_SIZE(conhost_path), L"%s\\conhost.exe", system_dir );
377 swprintf( cmd, ARRAY_SIZE(cmd), L"\"%s\" --server 0x%x", conhost_path, condrv_handle( server ));
378 Wow64DisableWow64FsRedirection( &redir );
379 ret = CreateProcessW( conhost_path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &console_si, &pi );
380 Wow64RevertWow64FsRedirection( redir );
382 if (!ret || !create_console_connection( console)) goto error;
383 if (!init_console_std_handles( !(app_si.dwFlags & STARTF_USESTDHANDLES) )) goto error;
385 RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle = console;
386 TRACE( "Started conhost pid=%08x tid=%08x\n", pi.dwProcessId, pi.dwThreadId );
388 CloseHandle( server );
389 RtlLeaveCriticalSection( &console_section );
390 SetLastError( ERROR_SUCCESS );
391 return TRUE;
393 error:
394 ERR("Can't allocate console\n");
395 NtClose( console );
396 NtClose( server );
397 FreeConsole();
398 RtlLeaveCriticalSection( &console_section );
399 return FALSE;
403 /******************************************************************************
404 * CreateConsoleScreenBuffer (kernelbase.@)
406 HANDLE WINAPI DECLSPEC_HOTPATCH CreateConsoleScreenBuffer( DWORD access, DWORD share,
407 SECURITY_ATTRIBUTES *sa, DWORD flags,
408 void *data )
410 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
411 IO_STATUS_BLOCK iosb;
412 UNICODE_STRING name;
413 HANDLE handle;
414 NTSTATUS status;
416 TRACE( "(%x,%x,%p,%x,%p)\n", access, share, sa, flags, data );
418 if (flags != CONSOLE_TEXTMODE_BUFFER || data)
420 SetLastError( ERROR_INVALID_PARAMETER );
421 return INVALID_HANDLE_VALUE;
424 RtlInitUnicodeString( &name, L"\\Device\\ConDrv\\ScreenBuffer" );
425 attr.ObjectName = &name;
426 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
427 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
428 status = NtCreateFile( &handle, access, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN,
429 FILE_NON_DIRECTORY_FILE, NULL, 0 );
430 return set_ntstatus( status ) ? handle : INVALID_HANDLE_VALUE;
434 /******************************************************************************
435 * CtrlRoutine (kernelbase.@)
437 DWORD WINAPI CtrlRoutine( void *arg )
439 DWORD_PTR event = (DWORD_PTR)arg;
440 struct ctrl_handler *handler;
442 if (event == CTRL_C_EVENT)
444 BOOL caught_by_dbg = TRUE;
445 /* First, try to pass the ctrl-C event to the debugger (if any)
446 * If it continues, there's nothing more to do
447 * Otherwise, we need to send the ctrl-C event to the handlers
449 __TRY
451 RaiseException( DBG_CONTROL_C, 0, 0, NULL );
453 __EXCEPT_ALL
455 caught_by_dbg = FALSE;
457 __ENDTRY
458 if (caught_by_dbg) return 0;
461 if (NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags & 1) return 0;
463 RtlEnterCriticalSection( &console_section );
464 for (handler = ctrl_handlers; handler; handler = handler->next)
466 if (handler->func( event )) break;
468 RtlLeaveCriticalSection( &console_section );
469 return 1;
473 static LONG WINAPI handle_ctrl_c( EXCEPTION_POINTERS *eptr )
475 if (eptr->ExceptionRecord->ExceptionCode != CONTROL_C_EXIT) return EXCEPTION_CONTINUE_SEARCH;
476 if (!RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle) return EXCEPTION_CONTINUE_SEARCH;
478 if (!(NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags & 1))
480 HANDLE thread = CreateThread( NULL, 0, CtrlRoutine, (void*)CTRL_C_EVENT, 0, NULL );
481 if (thread) CloseHandle( thread );
483 return EXCEPTION_CONTINUE_EXECUTION;
487 /******************************************************************************
488 * FillConsoleOutputAttribute (kernelbase.@)
490 BOOL WINAPI DECLSPEC_HOTPATCH FillConsoleOutputAttribute( HANDLE handle, WORD attr, DWORD length,
491 COORD coord, DWORD *written )
493 struct condrv_fill_output_params params;
495 TRACE( "(%p,%d,%d,(%dx%d),%p)\n", handle, attr, length, coord.X, coord.Y, written );
497 if (!written)
499 SetLastError( ERROR_INVALID_ACCESS );
500 return FALSE;
503 *written = 0;
505 params.mode = CHAR_INFO_MODE_ATTR;
506 params.x = coord.X;
507 params.y = coord.Y;
508 params.count = length;
509 params.wrap = TRUE;
510 params.ch = 0;
511 params.attr = attr;
512 return console_ioctl( handle, IOCTL_CONDRV_FILL_OUTPUT, &params, sizeof(params),
513 written, sizeof(*written), NULL );
517 /******************************************************************************
518 * FillConsoleOutputCharacterA (kernelbase.@)
520 BOOL WINAPI DECLSPEC_HOTPATCH FillConsoleOutputCharacterA( HANDLE handle, CHAR ch, DWORD length,
521 COORD coord, DWORD *written )
523 WCHAR wch;
525 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch, 1, &wch, 1 );
526 return FillConsoleOutputCharacterW( handle, wch, length, coord, written );
530 /******************************************************************************
531 * FillConsoleOutputCharacterW (kernelbase.@)
533 BOOL WINAPI DECLSPEC_HOTPATCH FillConsoleOutputCharacterW( HANDLE handle, WCHAR ch, DWORD length,
534 COORD coord, DWORD *written )
536 struct condrv_fill_output_params params;
538 TRACE( "(%p,%s,%d,(%dx%d),%p)\n", handle, debugstr_wn(&ch, 1), length, coord.X, coord.Y, written );
540 if (!written)
542 SetLastError( ERROR_INVALID_ACCESS );
543 return FALSE;
546 *written = 0;
548 params.mode = CHAR_INFO_MODE_TEXT;
549 params.x = coord.X;
550 params.y = coord.Y;
551 params.count = length;
552 params.wrap = TRUE;
553 params.ch = ch;
554 params.attr = 0;
555 return console_ioctl( handle, IOCTL_CONDRV_FILL_OUTPUT, &params, sizeof(params),
556 written, sizeof(*written), NULL );
560 /***********************************************************************
561 * FreeConsole (kernelbase.@)
563 BOOL WINAPI DECLSPEC_HOTPATCH FreeConsole(void)
565 RtlEnterCriticalSection( &console_section );
567 NtClose( console_connection );
568 console_connection = NULL;
570 NtClose( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle );
571 RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle = NULL;
573 if (console_flags & CONSOLE_INPUT_HANDLE) NtClose( GetStdHandle( STD_INPUT_HANDLE ));
574 if (console_flags & CONSOLE_OUTPUT_HANDLE) NtClose( GetStdHandle( STD_OUTPUT_HANDLE ));
575 if (console_flags & CONSOLE_ERROR_HANDLE) NtClose( GetStdHandle( STD_ERROR_HANDLE ));
576 console_flags = 0;
578 RtlLeaveCriticalSection( &console_section );
579 return TRUE;
583 /******************************************************************************
584 * GenerateConsoleCtrlEvent (kernelbase.@)
586 BOOL WINAPI DECLSPEC_HOTPATCH GenerateConsoleCtrlEvent( DWORD event, DWORD group )
588 struct condrv_ctrl_event ctrl_event;
590 TRACE( "(%d, %x)\n", event, group );
592 if (event != CTRL_C_EVENT && event != CTRL_BREAK_EVENT)
594 ERR( "Invalid event %d for PGID %x\n", event, group );
595 return FALSE;
598 ctrl_event.event = event;
599 ctrl_event.group_id = group;
600 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
601 IOCTL_CONDRV_CTRL_EVENT, &ctrl_event, sizeof(ctrl_event), NULL, 0, NULL );
605 /******************************************************************************
606 * GetConsoleCP (kernelbase.@)
608 UINT WINAPI DECLSPEC_HOTPATCH GetConsoleCP(void)
610 struct condrv_input_info info;
612 if (!console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
613 IOCTL_CONDRV_GET_INPUT_INFO, NULL, 0, &info, sizeof(info), NULL ))
614 return 0;
615 return info.input_cp;
619 /******************************************************************************
620 * GetConsoleCursorInfo (kernelbase.@)
622 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleCursorInfo( HANDLE handle, CONSOLE_CURSOR_INFO *info )
624 struct condrv_output_info condrv_info;
626 if (!console_ioctl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &condrv_info, sizeof(condrv_info), NULL ))
627 return FALSE;
629 if (!info)
631 SetLastError( ERROR_INVALID_ACCESS );
632 return FALSE;
635 info->dwSize = condrv_info.cursor_size;
636 info->bVisible = condrv_info.cursor_visible;
637 TRACE("(%p) returning (%d,%d)\n", handle, info->dwSize, info->bVisible);
638 return TRUE;
642 /***********************************************************************
643 * GetConsoleInputExeNameA (kernelbase.@)
645 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleInputExeNameA( DWORD len, LPSTR buffer )
647 RtlEnterCriticalSection( &console_section );
648 if (WideCharToMultiByte( CP_ACP, 0, input_exe, -1, NULL, 0, NULL, NULL ) <= len)
649 WideCharToMultiByte( CP_ACP, 0, input_exe, -1, buffer, len, NULL, NULL );
650 else SetLastError(ERROR_BUFFER_OVERFLOW);
651 RtlLeaveCriticalSection( &console_section );
652 return TRUE;
656 /***********************************************************************
657 * GetConsoleInputExeNameW (kernelbase.@)
659 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleInputExeNameW( DWORD len, LPWSTR buffer )
661 RtlEnterCriticalSection( &console_section );
662 if (len > lstrlenW(input_exe)) lstrcpyW( buffer, input_exe );
663 else SetLastError( ERROR_BUFFER_OVERFLOW );
664 RtlLeaveCriticalSection( &console_section );
665 return TRUE;
669 /***********************************************************************
670 * GetConsoleMode (kernelbase.@)
672 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleMode( HANDLE handle, DWORD *mode )
674 return console_ioctl( handle, IOCTL_CONDRV_GET_MODE, NULL, 0, mode, sizeof(*mode), NULL );
678 /***********************************************************************
679 * GetConsoleOutputCP (kernelbase.@)
681 UINT WINAPI DECLSPEC_HOTPATCH GetConsoleOutputCP(void)
683 struct condrv_input_info info;
685 if (!console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
686 IOCTL_CONDRV_GET_INPUT_INFO, NULL, 0, &info, sizeof(info), NULL ))
687 return 0;
688 return info.output_cp;
692 /***********************************************************************
693 * GetConsoleScreenBufferInfo (kernelbase.@)
695 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleScreenBufferInfo( HANDLE handle, CONSOLE_SCREEN_BUFFER_INFO *info )
697 struct condrv_output_info condrv_info;
699 if (!console_ioctl( handle , IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0,
700 &condrv_info, sizeof(condrv_info), NULL ))
701 return FALSE;
703 info->dwSize.X = condrv_info.width;
704 info->dwSize.Y = condrv_info.height;
705 info->dwCursorPosition.X = condrv_info.cursor_x;
706 info->dwCursorPosition.Y = condrv_info.cursor_y;
707 info->wAttributes = condrv_info.attr;
708 info->srWindow.Left = condrv_info.win_left;
709 info->srWindow.Right = condrv_info.win_right;
710 info->srWindow.Top = condrv_info.win_top;
711 info->srWindow.Bottom = condrv_info.win_bottom;
712 info->dwMaximumWindowSize.X = min(condrv_info.width, condrv_info.max_width);
713 info->dwMaximumWindowSize.Y = min(condrv_info.height, condrv_info.max_height);
715 TRACE( "(%p,(%d,%d) (%d,%d) %d (%d,%d-%d,%d) (%d,%d)\n", handle,
716 info->dwSize.X, info->dwSize.Y, info->dwCursorPosition.X, info->dwCursorPosition.Y,
717 info->wAttributes, info->srWindow.Left, info->srWindow.Top, info->srWindow.Right,
718 info->srWindow.Bottom, info->dwMaximumWindowSize.X, info->dwMaximumWindowSize.Y );
719 return TRUE;
723 /***********************************************************************
724 * GetConsoleScreenBufferInfoEx (kernelbase.@)
726 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleScreenBufferInfoEx( HANDLE handle,
727 CONSOLE_SCREEN_BUFFER_INFOEX *info )
729 struct condrv_output_info condrv_info;
731 if (info->cbSize != sizeof(CONSOLE_SCREEN_BUFFER_INFOEX))
733 SetLastError( ERROR_INVALID_PARAMETER );
734 return FALSE;
737 if (!console_ioctl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &condrv_info,
738 sizeof(condrv_info), NULL ))
739 return FALSE;
741 info->dwSize.X = condrv_info.width;
742 info->dwSize.Y = condrv_info.height;
743 info->dwCursorPosition.X = condrv_info.cursor_x;
744 info->dwCursorPosition.Y = condrv_info.cursor_y;
745 info->wAttributes = condrv_info.attr;
746 info->srWindow.Left = condrv_info.win_left;
747 info->srWindow.Top = condrv_info.win_top;
748 info->srWindow.Right = condrv_info.win_right;
749 info->srWindow.Bottom = condrv_info.win_bottom;
750 info->dwMaximumWindowSize.X = min( condrv_info.width, condrv_info.max_width );
751 info->dwMaximumWindowSize.Y = min( condrv_info.height, condrv_info.max_height );
752 info->wPopupAttributes = condrv_info.popup_attr;
753 info->bFullscreenSupported = FALSE;
754 memcpy( info->ColorTable, condrv_info.color_map, sizeof(info->ColorTable) );
755 return TRUE;
759 /******************************************************************************
760 * GetConsoleTitleW (kernelbase.@)
762 DWORD WINAPI DECLSPEC_HOTPATCH GetConsoleTitleW( LPWSTR title, DWORD size )
764 if (!size) return 0;
766 if (!console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, IOCTL_CONDRV_GET_TITLE,
767 NULL, 0, title, (size - 1) * sizeof(WCHAR), &size ))
768 return 0;
770 size /= sizeof(WCHAR);
771 title[size] = 0;
772 return size + 1;
776 /***********************************************************************
777 * GetLargestConsoleWindowSize (kernelbase.@)
779 #if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
780 #undef GetLargestConsoleWindowSize
781 DWORD WINAPI DECLSPEC_HOTPATCH GetLargestConsoleWindowSize( HANDLE handle )
783 union {
784 COORD c;
785 DWORD w;
786 } x;
787 x.c = get_largest_console_window_size( handle );
788 return x.w;
791 #else
793 COORD WINAPI DECLSPEC_HOTPATCH GetLargestConsoleWindowSize( HANDLE handle )
795 return get_largest_console_window_size( handle );
798 #endif /* !defined(__i386__) */
801 /***********************************************************************
802 * GetNumberOfConsoleInputEvents (kernelbase.@)
804 BOOL WINAPI DECLSPEC_HOTPATCH GetNumberOfConsoleInputEvents( HANDLE handle, DWORD *count )
806 struct condrv_input_info info;
807 if (!console_ioctl( handle, IOCTL_CONDRV_GET_INPUT_INFO, NULL, 0, &info, sizeof(info), NULL ))
808 return FALSE;
809 *count = info.input_count;
810 return TRUE;
814 /***********************************************************************
815 * PeekConsoleInputA (kernelbase.@)
817 BOOL WINAPI DECLSPEC_HOTPATCH PeekConsoleInputA( HANDLE handle, INPUT_RECORD *buffer,
818 DWORD length, DWORD *count )
820 DWORD read;
822 if (!PeekConsoleInputW( handle, buffer, length, &read )) return FALSE;
823 input_records_WtoA( buffer, read );
824 if (count) *count = read;
825 return TRUE;
829 /***********************************************************************
830 * PeekConsoleInputW (kernelbase.@)
832 BOOL WINAPI DECLSPEC_HOTPATCH PeekConsoleInputW( HANDLE handle, INPUT_RECORD *buffer,
833 DWORD length, DWORD *count )
835 DWORD read;
836 if (!console_ioctl( handle, IOCTL_CONDRV_PEEK, NULL, 0, buffer, length * sizeof(*buffer), &read ))
837 return FALSE;
838 if (count) *count = read / sizeof(*buffer);
839 return TRUE;
843 /******************************************************************************
844 * ReadConsoleOutputAttribute (kernelbase.@)
846 BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputAttribute( HANDLE handle, WORD *attr, DWORD length,
847 COORD coord, DWORD *count )
849 struct condrv_output_params params;
850 BOOL ret;
852 TRACE( "(%p,%p,%d,%dx%d,%p)\n", handle, attr, length, coord.X, coord.Y, count );
854 if (!count)
856 SetLastError( ERROR_INVALID_ACCESS );
857 return FALSE;
860 params.mode = CHAR_INFO_MODE_ATTR;
861 params.x = coord.X;
862 params.y = coord.Y;
863 params.width = 0;
864 ret = console_ioctl( handle, IOCTL_CONDRV_READ_OUTPUT, &params, sizeof(params),
865 attr, length * sizeof(*attr), count );
866 *count /= sizeof(*attr);
867 return ret;
871 /******************************************************************************
872 * ReadConsoleOutputCharacterA (kernelbase.@)
874 BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputCharacterA( HANDLE handle, LPSTR buffer, DWORD length,
875 COORD coord, DWORD *count )
877 DWORD read;
878 BOOL ret;
879 LPWSTR wptr;
881 if (!count)
883 SetLastError( ERROR_INVALID_ACCESS );
884 return FALSE;
887 *count = 0;
888 if (!(wptr = HeapAlloc( GetProcessHeap(), 0, length * sizeof(WCHAR) )))
890 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
891 return FALSE;
893 if ((ret = ReadConsoleOutputCharacterW( handle, wptr, length, coord, &read )))
895 read = WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr, read, buffer, length, NULL, NULL);
896 *count = read;
898 HeapFree( GetProcessHeap(), 0, wptr );
899 return ret;
903 /******************************************************************************
904 * ReadConsoleOutputCharacterW (kernelbase.@)
906 BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputCharacterW( HANDLE handle, LPWSTR buffer, DWORD length,
907 COORD coord, DWORD *count )
909 struct condrv_output_params params;
910 BOOL ret;
912 TRACE( "(%p,%p,%d,%dx%d,%p)\n", handle, buffer, length, coord.X, coord.Y, count );
914 if (!count)
916 SetLastError( ERROR_INVALID_ACCESS );
917 return FALSE;
920 params.mode = CHAR_INFO_MODE_TEXT;
921 params.x = coord.X;
922 params.y = coord.Y;
923 params.width = 0;
924 ret = console_ioctl( handle, IOCTL_CONDRV_READ_OUTPUT, &params, sizeof(params), buffer,
925 length * sizeof(*buffer), count );
926 *count /= sizeof(*buffer);
927 return ret;
931 /******************************************************************************
932 * ReadConsoleOutputA (kernelbase.@)
934 BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputA( HANDLE handle, CHAR_INFO *buffer, COORD size,
935 COORD coord, SMALL_RECT *region )
937 BOOL ret;
938 int y;
940 ret = ReadConsoleOutputW( handle, buffer, size, coord, region );
941 if (ret && region->Right >= region->Left)
943 UINT cp = GetConsoleOutputCP();
944 for (y = 0; y <= region->Bottom - region->Top; y++)
945 char_info_WtoA( cp, &buffer[(coord.Y + y) * size.X + coord.X], region->Right - region->Left + 1 );
947 return ret;
951 /******************************************************************************
952 * ReadConsoleOutputW (kernelbase.@)
954 BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputW( HANDLE handle, CHAR_INFO *buffer, COORD size,
955 COORD coord, SMALL_RECT *region )
957 struct condrv_output_params params;
958 unsigned int width, height, y;
959 SMALL_RECT *result;
960 DWORD count;
961 BOOL ret;
963 if (region->Left > region->Right || region->Top > region->Bottom)
965 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
966 return FALSE;
968 if (size.X <= coord.X || size.Y <= coord.Y)
970 region->Right = region->Left - 1;
971 region->Bottom = region->Top - 1;
972 SetLastError( ERROR_INVALID_FUNCTION );
973 return FALSE;
975 width = min( region->Right - region->Left + 1, size.X - coord.X );
976 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
977 region->Right = region->Left + width - 1;
978 region->Bottom = region->Top + height - 1;
980 count = sizeof(*result) + width * height * sizeof(*buffer);
981 if (!(result = HeapAlloc( GetProcessHeap(), 0, count )))
983 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
984 return FALSE;
987 params.mode = CHAR_INFO_MODE_TEXTATTR;
988 params.x = region->Left;
989 params.y = region->Top;
990 params.width = width;
991 if ((ret = console_ioctl( handle, IOCTL_CONDRV_READ_OUTPUT, &params, sizeof(params), result, count, &count )) && count)
993 CHAR_INFO *char_info = (CHAR_INFO *)(result + 1);
994 *region = *result;
995 width = region->Right - region->Left + 1;
996 height = region->Bottom - region->Top + 1;
997 for (y = 0; y < height; y++)
998 memcpy( &buffer[(y + coord.Y) * size.X + coord.X], &char_info[y * width], width * sizeof(*buffer) );
1000 HeapFree( GetProcessHeap(), 0, result );
1001 return ret;
1005 /******************************************************************************
1006 * ScrollConsoleScreenBufferA (kernelbase.@)
1008 BOOL WINAPI DECLSPEC_HOTPATCH ScrollConsoleScreenBufferA( HANDLE handle, const SMALL_RECT *scroll,
1009 const SMALL_RECT *clip, COORD origin, const CHAR_INFO *fill )
1011 CHAR_INFO ciW;
1013 ciW.Attributes = fill->Attributes;
1014 MultiByteToWideChar( GetConsoleOutputCP(), 0, &fill->Char.AsciiChar, 1, &ciW.Char.UnicodeChar, 1 );
1016 return ScrollConsoleScreenBufferW( handle, scroll, clip, origin, &ciW );
1020 /******************************************************************************
1021 * ScrollConsoleScreenBufferW (kernelbase.@)
1023 BOOL WINAPI DECLSPEC_HOTPATCH ScrollConsoleScreenBufferW( HANDLE handle, const SMALL_RECT *scroll,
1024 const SMALL_RECT *clip_rect, COORD origin,
1025 const CHAR_INFO *fill )
1027 struct condrv_scroll_params params;
1029 if (clip_rect)
1030 TRACE( "(%p,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", handle,
1031 scroll->Left, scroll->Top, scroll->Right, scroll->Bottom,
1032 clip_rect->Left, clip_rect->Top, clip_rect->Right, clip_rect->Bottom,
1033 origin.X, origin.Y, fill );
1034 else
1035 TRACE("(%p,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", handle,
1036 scroll->Left, scroll->Top, scroll->Right, scroll->Bottom,
1037 origin.X, origin.Y, fill );
1039 params.scroll = *scroll;
1040 params.origin = origin;
1041 params.fill.ch = fill->Char.UnicodeChar;
1042 params.fill.attr = fill->Attributes;
1043 if (!clip_rect)
1045 params.clip.Left = params.clip.Top = 0;
1046 params.clip.Right = params.clip.Bottom = SHRT_MAX;
1048 else params.clip = *clip_rect;
1049 return console_ioctl( handle, IOCTL_CONDRV_SCROLL, (void *)&params, sizeof(params), NULL, 0, NULL );
1053 /******************************************************************************
1054 * SetConsoleActiveScreenBuffer (kernelbase.@)
1056 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleActiveScreenBuffer( HANDLE handle )
1058 TRACE( "(%p)\n", handle );
1059 return console_ioctl( handle, IOCTL_CONDRV_ACTIVATE, NULL, 0, NULL, 0, NULL );
1063 /******************************************************************************
1064 * SetConsoleCP (kernelbase.@)
1066 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCP( UINT cp )
1068 struct condrv_input_info_params params = { SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE };
1070 params.info.input_cp = cp;
1071 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
1072 IOCTL_CONDRV_SET_INPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1076 /******************************************************************************
1077 * SetConsoleCtrlHandler (kernelbase.@)
1079 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCtrlHandler( PHANDLER_ROUTINE func, BOOL add )
1081 struct ctrl_handler *handler;
1082 BOOL ret = FALSE;
1084 TRACE( "(%p,%d)\n", func, add );
1086 RtlEnterCriticalSection( &console_section );
1088 if (!func)
1090 if (add) NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags |= 1;
1091 else NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags &= ~1;
1092 ret = TRUE;
1094 else if (add)
1096 if ((handler = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler) )))
1098 handler->func = func;
1099 handler->next = ctrl_handlers;
1100 ctrl_handlers = handler;
1101 ret = TRUE;
1104 else
1106 struct ctrl_handler **p_handler;
1108 for (p_handler = &ctrl_handlers; *p_handler; p_handler = &(*p_handler)->next)
1110 if ((*p_handler)->func == func) break;
1112 if (*p_handler && *p_handler != &default_handler)
1114 handler = *p_handler;
1115 *p_handler = handler->next;
1116 RtlFreeHeap( GetProcessHeap(), 0, handler );
1117 ret = TRUE;
1119 else SetLastError( ERROR_INVALID_PARAMETER );
1122 RtlLeaveCriticalSection( &console_section );
1123 return ret;
1127 /******************************************************************************
1128 * SetConsoleCursorInfo (kernelbase.@)
1130 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCursorInfo( HANDLE handle, CONSOLE_CURSOR_INFO *info )
1132 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM };
1134 TRACE( "(%p,%d,%d)\n", handle, info->dwSize, info->bVisible);
1136 params.info.cursor_size = info->dwSize;
1137 params.info.cursor_visible = info->bVisible;
1138 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params),
1139 NULL, 0, NULL );
1143 /******************************************************************************
1144 * SetConsoleCursorPosition (kernelbase.@)
1146 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCursorPosition( HANDLE handle, COORD pos )
1148 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_CURSOR_POS };
1150 TRACE( "%p %d %d\n", handle, pos.X, pos.Y );
1152 params.info.cursor_x = pos.X;
1153 params.info.cursor_y = pos.Y;
1154 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1158 /******************************************************************************
1159 * SetConsoleInputExeNameA (kernelbase.@)
1161 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleInputExeNameA( LPCSTR name )
1163 if (!name || !name[0])
1165 SetLastError( ERROR_INVALID_PARAMETER );
1166 return FALSE;
1168 RtlEnterCriticalSection( &console_section );
1169 MultiByteToWideChar( CP_ACP, 0, name, -1, input_exe, ARRAY_SIZE(input_exe) );
1170 RtlLeaveCriticalSection( &console_section );
1171 return TRUE;
1175 /******************************************************************************
1176 * SetConsoleInputExeNameW (kernelbase.@)
1178 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleInputExeNameW( LPCWSTR name )
1180 if (!name || !name[0])
1182 SetLastError( ERROR_INVALID_PARAMETER );
1183 return FALSE;
1185 RtlEnterCriticalSection( &console_section );
1186 lstrcpynW( input_exe, name, ARRAY_SIZE(input_exe) );
1187 RtlLeaveCriticalSection( &console_section );
1188 return TRUE;
1192 /******************************************************************************
1193 * SetConsoleMode (kernelbase.@)
1195 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleMode( HANDLE handle, DWORD mode )
1197 TRACE( "(%p,%x)\n", handle, mode );
1198 return console_ioctl( handle, IOCTL_CONDRV_SET_MODE, &mode, sizeof(mode), NULL, 0, NULL );
1202 /******************************************************************************
1203 * SetConsoleOutputCP (kernelbase.@)
1205 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleOutputCP( UINT cp )
1207 struct condrv_input_info_params params = { SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE };
1209 params.info.output_cp = cp;
1210 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
1211 IOCTL_CONDRV_SET_INPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1215 /******************************************************************************
1216 * SetConsoleScreenBufferInfoEx (kernelbase.@)
1218 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleScreenBufferInfoEx( HANDLE handle,
1219 CONSOLE_SCREEN_BUFFER_INFOEX *info )
1221 struct condrv_output_info_params params =
1222 { SET_CONSOLE_OUTPUT_INFO_CURSOR_POS | SET_CONSOLE_OUTPUT_INFO_SIZE |
1223 SET_CONSOLE_OUTPUT_INFO_ATTR | SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR |
1224 SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW | SET_CONSOLE_OUTPUT_INFO_MAX_SIZE };
1226 TRACE("(%p, %p)\n", handle, info);
1228 if (info->cbSize != sizeof(CONSOLE_SCREEN_BUFFER_INFOEX))
1230 SetLastError(ERROR_INVALID_PARAMETER);
1231 return FALSE;
1234 params.info.width = info->dwSize.X;
1235 params.info.height = info->dwSize.Y;
1236 params.info.cursor_x = info->dwCursorPosition.X;
1237 params.info.cursor_y = info->dwCursorPosition.Y;
1238 params.info.attr = info->wAttributes;
1239 params.info.win_left = info->srWindow.Left;
1240 params.info.win_top = info->srWindow.Top;
1241 params.info.win_right = info->srWindow.Right;
1242 params.info.win_bottom = info->srWindow.Bottom;
1243 params.info.popup_attr = info->wPopupAttributes;
1244 params.info.max_width = min( info->dwMaximumWindowSize.X, info->dwSize.X );
1245 params.info.max_height = min( info->dwMaximumWindowSize.Y, info->dwSize.Y );
1246 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1250 /******************************************************************************
1251 * SetConsoleScreenBufferSize (kernelbase.@)
1253 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleScreenBufferSize( HANDLE handle, COORD size )
1255 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_SIZE };
1257 TRACE( "(%p,(%d,%d))\n", handle, size.X, size.Y );
1259 params.info.width = size.X;
1260 params.info.height = size.Y;
1261 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1265 /******************************************************************************
1266 * SetConsoleTextAttribute (kernelbase.@)
1268 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleTextAttribute( HANDLE handle, WORD attr )
1270 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_ATTR };
1272 TRACE( "(%p,%d)\n", handle, attr );
1274 params.info.attr = attr;
1275 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1279 /******************************************************************************
1280 * SetConsoleTitleW (kernelbase.@)
1282 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleTitleW( LPCWSTR title )
1284 TRACE( "%s\n", debugstr_w( title ));
1286 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, IOCTL_CONDRV_SET_TITLE,
1287 (void *)title, lstrlenW(title) * sizeof(WCHAR), NULL, 0, NULL );
1291 /******************************************************************************
1292 * SetConsoleWindowInfo (kernelbase.@)
1294 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleWindowInfo( HANDLE handle, BOOL absolute, SMALL_RECT *window )
1296 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW };
1297 SMALL_RECT rect = *window;
1299 TRACE( "(%p,%d,(%d,%d-%d,%d))\n", handle, absolute, rect.Left, rect.Top, rect.Right, rect.Bottom );
1301 if (!absolute)
1303 CONSOLE_SCREEN_BUFFER_INFO info;
1305 if (!GetConsoleScreenBufferInfo( handle, &info )) return FALSE;
1306 rect.Left += info.srWindow.Left;
1307 rect.Top += info.srWindow.Top;
1308 rect.Right += info.srWindow.Right;
1309 rect.Bottom += info.srWindow.Bottom;
1312 params.info.win_left = rect.Left;
1313 params.info.win_top = rect.Top;
1314 params.info.win_right = rect.Right;
1315 params.info.win_bottom = rect.Bottom;
1316 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1320 /***********************************************************************
1321 * ReadConsoleInputA (kernelbase.@)
1323 BOOL WINAPI ReadConsoleInputA( HANDLE handle, INPUT_RECORD *buffer, DWORD length, DWORD *count )
1325 DWORD read;
1327 if (!ReadConsoleInputW( handle, buffer, length, &read )) return FALSE;
1328 input_records_WtoA( buffer, read );
1329 if (count) *count = read;
1330 return TRUE;
1334 /***********************************************************************
1335 * ReadConsoleInputW (kernelbase.@)
1337 BOOL WINAPI ReadConsoleInputW( HANDLE handle, INPUT_RECORD *buffer, DWORD length, DWORD *count )
1339 if (!console_ioctl( handle, IOCTL_CONDRV_READ_INPUT, NULL, 0,
1340 buffer, length * sizeof(*buffer), count ))
1341 return FALSE;
1342 *count /= sizeof(*buffer);
1343 return TRUE;
1347 /******************************************************************************
1348 * WriteConsoleInputA (kernelbase.@)
1350 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleInputA( HANDLE handle, const INPUT_RECORD *buffer,
1351 DWORD count, DWORD *written )
1353 INPUT_RECORD *recW = NULL;
1354 BOOL ret;
1356 if (count > 0)
1358 if (!buffer)
1360 SetLastError( ERROR_INVALID_ACCESS );
1361 return FALSE;
1363 if (!(recW = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*recW) )))
1365 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1366 return FALSE;
1368 memcpy( recW, buffer, count * sizeof(*recW) );
1369 input_records_AtoW( recW, count );
1371 ret = WriteConsoleInputW( handle, recW, count, written );
1372 HeapFree( GetProcessHeap(), 0, recW );
1373 return ret;
1377 /******************************************************************************
1378 * WriteConsoleInputW (kernelbase.@)
1380 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleInputW( HANDLE handle, const INPUT_RECORD *buffer,
1381 DWORD count, DWORD *written )
1383 TRACE( "(%p,%p,%d,%p)\n", handle, buffer, count, written );
1385 if (count > 0 && !buffer)
1387 SetLastError( ERROR_INVALID_ACCESS );
1388 return FALSE;
1391 if (!DeviceIoControl( handle, IOCTL_CONDRV_WRITE_INPUT, (void *)buffer, count * sizeof(*buffer), NULL, 0, NULL, NULL ))
1392 return FALSE;
1394 if (!written)
1396 SetLastError( ERROR_INVALID_ACCESS );
1397 return FALSE;
1399 *written = count;
1400 return TRUE;
1404 /***********************************************************************
1405 * WriteConsoleOutputA (kernelbase.@)
1407 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputA( HANDLE handle, const CHAR_INFO *buffer,
1408 COORD size, COORD coord, SMALL_RECT *region )
1410 int y;
1411 BOOL ret;
1412 COORD new_size, new_coord;
1413 CHAR_INFO *ciW;
1415 new_size.X = min( region->Right - region->Left + 1, size.X - coord.X );
1416 new_size.Y = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
1418 if (new_size.X <= 0 || new_size.Y <= 0)
1420 region->Bottom = region->Top + new_size.Y - 1;
1421 region->Right = region->Left + new_size.X - 1;
1422 return TRUE;
1425 /* only copy the useful rectangle */
1426 if (!(ciW = HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO) * new_size.X * new_size.Y )))
1427 return FALSE;
1428 for (y = 0; y < new_size.Y; y++)
1429 memcpy( &ciW[y * new_size.X], &buffer[(y + coord.Y) * size.X + coord.X],
1430 new_size.X * sizeof(CHAR_INFO) );
1431 char_info_AtoW( ciW, new_size.X * new_size.Y );
1432 new_coord.X = new_coord.Y = 0;
1433 ret = WriteConsoleOutputW( handle, ciW, new_size, new_coord, region );
1434 HeapFree( GetProcessHeap(), 0, ciW );
1435 return ret;
1439 /***********************************************************************
1440 * WriteConsoleOutputW (kernelbase.@)
1442 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputW( HANDLE handle, const CHAR_INFO *buffer,
1443 COORD size, COORD coord, SMALL_RECT *region )
1445 struct condrv_output_params *params;
1446 unsigned int width, height, y;
1447 size_t params_size;
1448 BOOL ret;
1450 TRACE( "(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n",
1451 handle, buffer, size.X, size.Y, coord.X, coord.Y,
1452 region->Left, region->Top, region->Right, region->Bottom );
1454 if (region->Left > region->Right || region->Top > region->Bottom || size.X <= coord.X || size.Y <= coord.Y)
1456 SetLastError( ERROR_INVALID_PARAMETER );
1457 return FALSE;
1460 width = min( region->Right - region->Left + 1, size.X - coord.X );
1461 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
1462 region->Right = region->Left + width - 1;
1463 region->Bottom = region->Top + height - 1;
1465 params_size = sizeof(*params) + width * height * sizeof(*buffer);
1466 if (!(params = HeapAlloc( GetProcessHeap(), 0, params_size )))
1468 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1469 return FALSE;
1472 params->mode = CHAR_INFO_MODE_TEXTATTR;
1473 params->x = region->Left;
1474 params->y = region->Top;
1475 params->width = width;
1477 for (y = 0; y < height; y++)
1478 memcpy( &((CHAR_INFO *)(params + 1))[y * width], &buffer[(y + coord.Y) * size.X + coord.X], width * sizeof(CHAR_INFO) );
1480 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_OUTPUT, params, params_size, region, sizeof(*region), NULL );
1481 HeapFree( GetProcessHeap(), 0, params );
1482 return ret;
1486 /******************************************************************************
1487 * WriteConsoleOutputAttribute (kernelbase.@)
1489 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputAttribute( HANDLE handle, const WORD *attr, DWORD length,
1490 COORD coord, DWORD *written )
1492 struct condrv_output_params *params;
1493 size_t size;
1494 BOOL ret;
1496 TRACE( "(%p,%p,%d,%dx%d,%p)\n", handle, attr, length, coord.X, coord.Y, written );
1498 if ((length > 0 && !attr) || !written)
1500 SetLastError( ERROR_INVALID_ACCESS );
1501 return FALSE;
1504 *written = 0;
1505 size = sizeof(*params) + length * sizeof(WORD);
1506 if (!(params = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1507 params->mode = CHAR_INFO_MODE_ATTR;
1508 params->x = coord.X;
1509 params->y = coord.Y;
1510 params->width = 0;
1511 memcpy( params + 1, attr, length * sizeof(*attr) );
1512 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_OUTPUT, params, size, written, sizeof(*written), NULL );
1513 HeapFree( GetProcessHeap(), 0, params );
1514 return ret;
1518 /******************************************************************************
1519 * WriteConsoleOutputCharacterA (kernelbase.@)
1521 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputCharacterA( HANDLE handle, LPCSTR str, DWORD length,
1522 COORD coord, DWORD *written )
1524 BOOL ret;
1525 LPWSTR strW = NULL;
1526 DWORD lenW = 0;
1528 TRACE( "(%p,%s,%d,%dx%d,%p)\n", handle, debugstr_an(str, length), length, coord.X, coord.Y, written );
1530 if (length > 0)
1532 UINT cp = GetConsoleOutputCP();
1533 if (!str)
1535 SetLastError( ERROR_INVALID_ACCESS );
1536 return FALSE;
1538 lenW = MultiByteToWideChar( cp, 0, str, length, NULL, 0 );
1540 if (!(strW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
1542 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1543 return FALSE;
1545 MultiByteToWideChar( cp, 0, str, length, strW, lenW );
1547 ret = WriteConsoleOutputCharacterW( handle, strW, lenW, coord, written );
1548 HeapFree( GetProcessHeap(), 0, strW );
1549 return ret;
1553 /******************************************************************************
1554 * WriteConsoleOutputCharacterW (kernelbase.@)
1556 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputCharacterW( HANDLE handle, LPCWSTR str, DWORD length,
1557 COORD coord, DWORD *written )
1559 struct condrv_output_params *params;
1560 size_t size;
1561 BOOL ret;
1563 TRACE( "(%p,%s,%d,%dx%d,%p)\n", handle, debugstr_wn(str, length), length, coord.X, coord.Y, written );
1565 if ((length > 0 && !str) || !written)
1567 SetLastError( ERROR_INVALID_ACCESS );
1568 return FALSE;
1571 *written = 0;
1572 size = sizeof(*params) + length * sizeof(WCHAR);
1573 if (!(params = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1574 params->mode = CHAR_INFO_MODE_TEXT;
1575 params->x = coord.X;
1576 params->y = coord.Y;
1577 params->width = 0;
1578 memcpy( params + 1, str, length * sizeof(*str) );
1579 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_OUTPUT, params, size, written, sizeof(*written), NULL );
1580 HeapFree( GetProcessHeap(), 0, params );
1581 return ret;
1585 /***********************************************************************
1586 * ReadConsoleA (kernelbase.@)
1588 BOOL WINAPI ReadConsoleA( HANDLE handle, void *buffer, DWORD length, DWORD *count, void *reserved )
1590 if (length > INT_MAX)
1592 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1593 return FALSE;
1596 return console_ioctl( handle, IOCTL_CONDRV_READ_FILE, NULL, 0, buffer, length, count );
1600 /***********************************************************************
1601 * ReadConsoleW (kernelbase.@)
1603 BOOL WINAPI ReadConsoleW( HANDLE handle, void *buffer, DWORD length, DWORD *count, void *reserved )
1605 BOOL ret;
1607 TRACE( "(%p,%p,%d,%p,%p)\n", handle, buffer, length, count, reserved );
1609 if (length > INT_MAX)
1611 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1612 return FALSE;
1615 ret = console_ioctl( handle, IOCTL_CONDRV_READ_CONSOLE, NULL, 0, buffer,
1616 length * sizeof(WCHAR), count );
1617 if (ret) *count /= sizeof(WCHAR);
1618 return ret;
1622 /***********************************************************************
1623 * WriteConsoleA (kernelbase.@)
1625 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleA( HANDLE handle, const void *buffer, DWORD length,
1626 DWORD *written, void *reserved )
1628 BOOL ret;
1630 TRACE( "(%p,%s,%d,%p,%p)\n", handle, debugstr_an(buffer, length), length, written, reserved );
1632 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_FILE, (void *)buffer, length, NULL, 0, NULL );
1633 if (written) *written = ret ? length : 0;
1634 return ret;
1638 /***********************************************************************
1639 * WriteConsoleW (kernelbase.@)
1641 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleW( HANDLE handle, const void *buffer, DWORD length,
1642 DWORD *written, void *reserved )
1644 BOOL ret;
1646 TRACE( "(%p,%s,%d,%p,%p)\n", handle, debugstr_wn(buffer, length), length, written, reserved );
1648 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_CONSOLE, (void *)buffer,
1649 length * sizeof(WCHAR), NULL, 0, NULL );
1650 if (written) *written = ret ? length : 0;
1651 return ret;
1655 /***********************************************************************
1656 * FlushConsoleInputBuffer (kernelbase.@)
1658 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
1660 return console_ioctl( handle, IOCTL_CONDRV_FLUSH, NULL, 0, NULL, 0, NULL );
1664 /***********************************************************************
1665 * Beep (kernelbase.@)
1667 BOOL WINAPI Beep( DWORD frequency, DWORD duration )
1669 /* FIXME: we should not require a console to be attached */
1670 console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
1671 IOCTL_CONDRV_BEEP, NULL, 0, NULL, 0, NULL );
1672 return TRUE;
1676 static HANDLE create_pseudo_console( COORD size, HANDLE input, HANDLE output, HANDLE signal,
1677 DWORD flags, HANDLE *process )
1679 WCHAR cmd[MAX_PATH], conhost_path[MAX_PATH];
1680 PROCESS_INFORMATION pi;
1681 HANDLE server, console;
1682 STARTUPINFOEXW si;
1683 void *redir;
1684 BOOL res;
1686 if (!(server = create_console_server())) return NULL;
1688 console = create_console_reference( server );
1689 if (!console)
1691 NtClose( server );
1692 return NULL;
1695 memset( &si, 0, sizeof(si) );
1696 si.StartupInfo.cb = sizeof(STARTUPINFOEXW);
1697 si.StartupInfo.hStdInput = input;
1698 si.StartupInfo.hStdOutput = output;
1699 si.StartupInfo.hStdError = output;
1700 si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
1701 swprintf( conhost_path, ARRAY_SIZE(conhost_path), L"%s\\conhost.exe", system_dir );
1702 if (signal)
1704 swprintf( cmd, ARRAY_SIZE(cmd),
1705 L"\"%s\" --headless %s--width %u --height %u --signal 0x%x --server 0x%x",
1706 conhost_path, (flags & PSEUDOCONSOLE_INHERIT_CURSOR) ? L"--inheritcursor " : L"",
1707 size.X, size.Y, signal, server );
1709 else
1711 swprintf( cmd, ARRAY_SIZE(cmd), L"\"%s\" --unix --width %u --height %u --server 0x%x",
1712 conhost_path, size.X, size.Y, server );
1714 Wow64DisableWow64FsRedirection( &redir );
1715 res = CreateProcessW( conhost_path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL,
1716 &si.StartupInfo, &pi );
1717 Wow64RevertWow64FsRedirection( redir );
1718 NtClose( server );
1719 if (!res)
1721 NtClose( console );
1722 return NULL;
1725 NtClose( pi.hThread );
1726 *process = pi.hProcess;
1727 return console;
1730 /******************************************************************************
1731 * CreatePseudoConsole (kernelbase.@)
1733 HRESULT WINAPI CreatePseudoConsole( COORD size, HANDLE input, HANDLE output, DWORD flags, HPCON *ret )
1735 SECURITY_ATTRIBUTES inherit_attr = { sizeof(inherit_attr), NULL, TRUE };
1736 struct pseudo_console *pseudo_console;
1737 HANDLE tty_input = NULL, tty_output;
1738 HANDLE signal = NULL;
1739 WCHAR pipe_name[64];
1741 TRACE( "(%u,%u) %p %p %x %p\n", size.X, size.Y, input, output, flags, ret );
1743 if (!size.X || !size.Y || !ret) return E_INVALIDARG;
1745 if (!(pseudo_console = HeapAlloc( GetProcessHeap(), 0, HEAP_ZERO_MEMORY ))) return E_OUTOFMEMORY;
1747 swprintf( pipe_name, ARRAY_SIZE(pipe_name), L"\\\\.\\pipe\\wine_pty_signal_pipe%x",
1748 GetCurrentThreadId() );
1749 signal = CreateNamedPipeW( pipe_name, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE,
1750 PIPE_UNLIMITED_INSTANCES, 4096, 4096, NMPWAIT_USE_DEFAULT_WAIT, &inherit_attr );
1751 if (signal == INVALID_HANDLE_VALUE)
1753 HeapFree( GetProcessHeap(), 0, pseudo_console );
1754 return HRESULT_FROM_WIN32( GetLastError() );
1756 pseudo_console->signal = CreateFileW( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
1757 if (pseudo_console->signal != INVALID_HANDLE_VALUE &&
1758 DuplicateHandle( GetCurrentProcess(), input, GetCurrentProcess(), &tty_input, 0, TRUE, DUPLICATE_SAME_ACCESS) &&
1759 DuplicateHandle( GetCurrentProcess(), output, GetCurrentProcess(), &tty_output, 0, TRUE, DUPLICATE_SAME_ACCESS))
1761 pseudo_console->reference = create_pseudo_console( size, tty_input, tty_output, signal, flags,
1762 &pseudo_console->process );
1763 NtClose( tty_output );
1765 NtClose( tty_input );
1766 NtClose( signal );
1767 if (!pseudo_console->reference)
1769 ClosePseudoConsole( pseudo_console );
1770 return HRESULT_FROM_WIN32( GetLastError() );
1773 *ret = pseudo_console;
1774 return S_OK;
1777 /******************************************************************************
1778 * ClosePseudoConsole (kernelbase.@)
1780 void WINAPI ClosePseudoConsole( HPCON handle )
1782 struct pseudo_console *pseudo_console = handle;
1784 TRACE( "%p\n", handle );
1786 if (!pseudo_console) return;
1787 if (pseudo_console->signal) CloseHandle( pseudo_console->signal );
1788 if (pseudo_console->process)
1790 WaitForSingleObject( pseudo_console->process, INFINITE );
1791 CloseHandle( pseudo_console->process );
1793 if (pseudo_console->reference) CloseHandle( pseudo_console->reference );
1796 /******************************************************************************
1797 * ResizePseudoConsole (kernelbase.@)
1799 HRESULT WINAPI ResizePseudoConsole( HPCON handle, COORD size )
1801 FIXME( "%p (%u,%u)\n", handle, size.X, size.Y );
1802 return E_NOTIMPL;
1805 static BOOL is_tty_handle( HANDLE handle )
1807 return ((UINT_PTR)handle & 3) == 1;
1810 void init_console( void )
1812 RTL_USER_PROCESS_PARAMETERS *params = RtlGetCurrentPeb()->ProcessParameters;
1814 if (params->ConsoleHandle == CONSOLE_HANDLE_SHELL)
1816 HANDLE tty_in = NULL, tty_out = NULL, process = NULL;
1817 COORD size;
1819 if (is_tty_handle( params->hStdInput ))
1821 tty_in = params->hStdInput;
1822 params->hStdInput = NULL;
1824 if (is_tty_handle( params->hStdOutput ))
1826 tty_out = params->hStdOutput;
1827 params->hStdOutput = NULL;
1829 if (is_tty_handle( params->hStdError ))
1831 if (tty_out) CloseHandle( params->hStdError );
1832 else tty_out = params->hStdError;
1833 params->hStdError = NULL;
1836 size.X = params->dwXCountChars;
1837 size.Y = params->dwYCountChars;
1838 TRACE( "creating unix console (size %u %u)\n", size.X, size.Y );
1839 params->ConsoleHandle = create_pseudo_console( size, tty_in, tty_out, NULL, 0, &process );
1840 CloseHandle( process );
1841 CloseHandle( tty_in );
1842 CloseHandle( tty_out );
1844 if (params->ConsoleHandle && create_console_connection( params->ConsoleHandle ))
1846 init_console_std_handles( FALSE );
1849 else if (params->ConsoleHandle == CONSOLE_HANDLE_ALLOC)
1851 HMODULE mod = GetModuleHandleW( NULL );
1852 params->ConsoleHandle = NULL;
1853 if (RtlImageNtHeader( mod )->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
1854 AllocConsole();
1856 else if (params->ConsoleHandle) create_console_connection( params->ConsoleHandle );
1858 RtlAddVectoredExceptionHandler( FALSE, handle_ctrl_c );