include: Add missing enum XHR_PROP_ values.
[wine.git] / dlls / kernelbase / console.c
blob7cd87f53b3c94287691d4aa3de1c173bd6285ee7
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 %lx on event %lx\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 if (handle == CONSOLE_HANDLE_SHELL_NO_WINDOW)
91 WARN("Incorrect access to Shell-no-window console (ioctl=%lx)\n", code);
92 SetLastError( ERROR_INVALID_ACCESS );
93 return FALSE;
95 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io, code, in_buff, in_count,
96 out_buff, out_count );
97 switch( status )
99 case STATUS_SUCCESS:
100 if (read) *read = io.Information;
101 return TRUE;
102 case STATUS_INVALID_PARAMETER:
103 break;
104 default:
105 status = STATUS_INVALID_HANDLE;
106 break;
108 if (read) *read = 0;
109 return set_ntstatus( status );
112 /* map input records to ASCII */
113 static void input_records_WtoA( INPUT_RECORD *buffer, int count )
115 UINT cp = GetConsoleCP();
116 int i;
117 char ch;
119 for (i = 0; i < count; i++)
121 if (buffer[i].EventType != KEY_EVENT) continue;
122 WideCharToMultiByte( cp, 0, &buffer[i].Event.KeyEvent.uChar.UnicodeChar, 1, &ch, 1, NULL, NULL );
123 buffer[i].Event.KeyEvent.uChar.AsciiChar = ch;
127 /* map input records to Unicode */
128 static void input_records_AtoW( INPUT_RECORD *buffer, int count )
130 UINT cp = GetConsoleCP();
131 int i;
132 WCHAR ch;
134 for (i = 0; i < count; i++)
136 if (buffer[i].EventType != KEY_EVENT) continue;
137 MultiByteToWideChar( cp, 0, &buffer[i].Event.KeyEvent.uChar.AsciiChar, 1, &ch, 1 );
138 buffer[i].Event.KeyEvent.uChar.UnicodeChar = ch;
142 /* map char infos to ASCII */
143 static void char_info_WtoA( UINT cp, CHAR_INFO *buffer, int count )
145 char ch;
147 while (count-- > 0)
149 WideCharToMultiByte( cp, 0, &buffer->Char.UnicodeChar, 1, &ch, 1, NULL, NULL );
150 buffer->Char.AsciiChar = ch;
151 buffer++;
155 /* map char infos to Unicode */
156 static void char_info_AtoW( CHAR_INFO *buffer, int count )
158 UINT cp = GetConsoleOutputCP();
159 WCHAR ch;
161 while (count-- > 0)
163 MultiByteToWideChar( cp, 0, &buffer->Char.AsciiChar, 1, &ch, 1 );
164 buffer->Char.UnicodeChar = ch;
165 buffer++;
169 /* helper function for GetLargestConsoleWindowSize */
170 static COORD get_largest_console_window_size( HANDLE handle )
172 struct condrv_output_info info;
173 COORD c = { 0, 0 };
175 if (!console_ioctl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &info, sizeof(info), NULL ))
176 return c;
178 c.X = info.max_width;
179 c.Y = info.max_height;
180 TRACE( "(%p), returning %dx%d\n", handle, c.X, c.Y );
181 return c;
184 /* helper function for GetConsoleFontSize */
185 static COORD get_console_font_size( HANDLE handle, DWORD index )
187 struct condrv_output_info info;
188 COORD c = {0,0};
190 if (index >= 1 /* number of console fonts */)
192 SetLastError(ERROR_INVALID_PARAMETER);
193 return c;
196 if (DeviceIoControl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &info, sizeof(info), NULL, NULL ))
198 c.X = info.font_width;
199 c.Y = info.font_height;
201 else SetLastError( ERROR_INVALID_HANDLE );
202 return c;
205 static HANDLE create_console_server( void )
207 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
208 UNICODE_STRING string;
209 IO_STATUS_BLOCK iosb;
210 HANDLE handle;
211 NTSTATUS status;
213 RtlInitUnicodeString( &string, L"\\Device\\ConDrv\\Server" );
214 attr.ObjectName = &string;
215 attr.Attributes = OBJ_INHERIT;
216 status = NtCreateFile( &handle, FILE_WRITE_PROPERTIES | FILE_READ_PROPERTIES | SYNCHRONIZE,
217 &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN,
218 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
219 return set_ntstatus( status ) ? handle : NULL;
222 static HANDLE create_console_reference( HANDLE root )
224 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
225 UNICODE_STRING string;
226 IO_STATUS_BLOCK iosb;
227 HANDLE handle;
228 NTSTATUS status;
230 RtlInitUnicodeString( &string, L"Reference" );
231 attr.RootDirectory = root;
232 attr.ObjectName = &string;
233 status = NtCreateFile( &handle, FILE_READ_DATA | FILE_WRITE_DATA | FILE_WRITE_PROPERTIES |
234 FILE_READ_PROPERTIES | SYNCHRONIZE, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL,
235 0, FILE_OPEN, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
236 return set_ntstatus( status ) ? handle : NULL;
239 static BOOL create_console_connection( HANDLE root )
241 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
242 UNICODE_STRING string;
243 IO_STATUS_BLOCK iosb;
244 NTSTATUS status;
246 RtlInitUnicodeString( &string, root ? L"Connection" : L"\\Device\\ConDrv\\Connection" );
247 attr.RootDirectory = root;
248 attr.ObjectName = &string;
249 status = NtCreateFile( &console_connection, FILE_WRITE_PROPERTIES | FILE_READ_PROPERTIES | SYNCHRONIZE, &attr,
250 &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_NON_DIRECTORY_FILE, NULL, 0 );
251 return set_ntstatus( status );
254 static BOOL init_console_std_handles( BOOL override_all )
256 HANDLE std_out = NULL, std_err = NULL, handle;
257 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
258 IO_STATUS_BLOCK iosb;
259 UNICODE_STRING name;
260 NTSTATUS status;
262 attr.ObjectName = &name;
263 attr.Attributes = OBJ_INHERIT;
265 if (override_all || !GetStdHandle( STD_INPUT_HANDLE ))
267 RtlInitUnicodeString( &name, L"\\Device\\ConDrv\\Input" );
268 status = NtCreateFile( &handle, FILE_READ_DATA | FILE_WRITE_DATA | SYNCHRONIZE | FILE_READ_ATTRIBUTES |
269 FILE_WRITE_ATTRIBUTES, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL,
270 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_CREATE,
271 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
272 if (!set_ntstatus( status )) return FALSE;
273 console_flags |= CONSOLE_INPUT_HANDLE;
274 SetStdHandle( STD_INPUT_HANDLE, handle );
277 if (!override_all)
279 std_out = GetStdHandle( STD_OUTPUT_HANDLE );
280 std_err = GetStdHandle( STD_ERROR_HANDLE );
281 if (std_out && std_err) return TRUE;
284 RtlInitUnicodeString( &name, L"\\Device\\ConDrv\\Output" );
285 status = NtCreateFile( &handle, FILE_READ_DATA | FILE_WRITE_DATA | SYNCHRONIZE | FILE_READ_ATTRIBUTES |
286 FILE_WRITE_ATTRIBUTES, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL,
287 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_CREATE,
288 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
289 if (!set_ntstatus( status )) return FALSE;
290 if (!std_out)
292 console_flags |= CONSOLE_OUTPUT_HANDLE;
293 SetStdHandle( STD_OUTPUT_HANDLE, handle );
296 if (!std_err)
298 if (!std_out && !DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
299 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS ))
300 return FALSE;
301 console_flags |= CONSOLE_ERROR_HANDLE;
302 SetStdHandle( STD_ERROR_HANDLE, handle );
305 return TRUE;
309 /******************************************************************
310 * AddConsoleAliasA (kernelbase.@)
312 BOOL WINAPI AddConsoleAliasA( LPSTR source, LPSTR target, LPSTR exename )
314 FIXME( ": (%s, %s, %s) stub!\n", debugstr_a(source), debugstr_a(target), debugstr_a(exename) );
315 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
316 return FALSE;
320 /******************************************************************
321 * AddConsoleAliasW (kernelbase.@)
323 BOOL WINAPI AddConsoleAliasW( LPWSTR source, LPWSTR target, LPWSTR exename )
325 FIXME( ": (%s, %s, %s) stub!\n", debugstr_w(source), debugstr_w(target), debugstr_w(exename) );
326 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
327 return FALSE;
331 /******************************************************************
332 * AttachConsole (kernelbase.@)
334 BOOL WINAPI DECLSPEC_HOTPATCH AttachConsole( DWORD pid )
336 BOOL ret;
338 TRACE( "(%lx)\n", pid );
340 RtlEnterCriticalSection( &console_section );
342 if (RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle)
344 RtlLeaveCriticalSection( &console_section );
345 WARN( "console already attached\n" );
346 SetLastError( ERROR_ACCESS_DENIED );
347 return FALSE;
350 ret = create_console_connection( NULL ) &&
351 console_ioctl( console_connection, IOCTL_CONDRV_BIND_PID, &pid, sizeof(pid), NULL, 0, NULL );
352 if (ret)
354 RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle = create_console_reference( console_connection );
355 if (RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle)
357 STARTUPINFOW si;
358 GetStartupInfoW( &si );
359 init_console_std_handles( !(si.dwFlags & STARTF_USESTDHANDLES) );
361 else ret = FALSE;
364 if (!ret) FreeConsole();
365 RtlLeaveCriticalSection( &console_section );
366 return ret;
370 static BOOL alloc_console( BOOL headless )
372 SECURITY_ATTRIBUTES inheritable_attr = { sizeof(inheritable_attr), NULL, TRUE };
373 STARTUPINFOW app_si, console_si;
374 HANDLE server, console = NULL;
375 WCHAR buffer[1024], cmd[256], conhost_path[MAX_PATH];
376 PROCESS_INFORMATION pi;
377 void *redir;
378 BOOL ret;
380 TRACE("()\n");
382 RtlEnterCriticalSection( &console_section );
384 if (RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle)
386 /* we already have a console opened on this process, don't create a new one */
387 RtlLeaveCriticalSection( &console_section );
388 SetLastError( ERROR_ACCESS_DENIED );
389 return FALSE;
392 if (!(server = create_console_server()) || !(console = create_console_reference( server ))) goto error;
394 GetStartupInfoW(&app_si);
396 memset(&console_si, 0, sizeof(console_si));
397 console_si.cb = sizeof(console_si);
398 /* setup a view arguments for conhost (it'll use them as default values) */
399 if (app_si.dwFlags & STARTF_USECOUNTCHARS)
401 console_si.dwFlags |= STARTF_USECOUNTCHARS;
402 console_si.dwXCountChars = app_si.dwXCountChars;
403 console_si.dwYCountChars = app_si.dwYCountChars;
405 if (app_si.dwFlags & STARTF_USEFILLATTRIBUTE)
407 console_si.dwFlags |= STARTF_USEFILLATTRIBUTE;
408 console_si.dwFillAttribute = app_si.dwFillAttribute;
410 if (app_si.dwFlags & STARTF_USESHOWWINDOW)
412 console_si.dwFlags |= STARTF_USESHOWWINDOW;
413 console_si.wShowWindow = app_si.wShowWindow;
415 if (app_si.lpTitle)
416 console_si.lpTitle = app_si.lpTitle;
417 else if (GetModuleFileNameW(0, buffer, ARRAY_SIZE(buffer)))
419 buffer[ARRAY_SIZE(buffer) - 1] = 0;
420 console_si.lpTitle = buffer;
423 swprintf( conhost_path, ARRAY_SIZE(conhost_path), L"%s\\conhost.exe", system_dir );
424 swprintf( cmd, ARRAY_SIZE(cmd), L"\"%s\" --server 0x%x", conhost_path, condrv_handle( server ));
425 if (headless) wcscat( cmd, L" --headless" );
426 Wow64DisableWow64FsRedirection( &redir );
427 ret = CreateProcessW( conhost_path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &console_si, &pi );
428 Wow64RevertWow64FsRedirection( redir );
430 if (!ret || !create_console_connection( console)) goto error;
431 if (!init_console_std_handles( !(app_si.dwFlags & STARTF_USESTDHANDLES) )) goto error;
433 RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle = console;
434 TRACE( "Started conhost pid=%08lx tid=%08lx\n", pi.dwProcessId, pi.dwThreadId );
436 CloseHandle( server );
437 RtlLeaveCriticalSection( &console_section );
438 SetLastError( ERROR_SUCCESS );
439 return TRUE;
441 error:
442 ERR("Can't allocate console\n");
443 NtClose( console );
444 NtClose( server );
445 FreeConsole();
446 RtlLeaveCriticalSection( &console_section );
447 return FALSE;
451 /******************************************************************
452 * AllocConsole (kernelbase.@)
454 BOOL WINAPI AllocConsole(void)
456 return alloc_console( FALSE );
460 /******************************************************************************
461 * CreateConsoleScreenBuffer (kernelbase.@)
463 HANDLE WINAPI DECLSPEC_HOTPATCH CreateConsoleScreenBuffer( DWORD access, DWORD share,
464 SECURITY_ATTRIBUTES *sa, DWORD flags,
465 void *data )
467 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
468 IO_STATUS_BLOCK iosb;
469 UNICODE_STRING name;
470 HANDLE handle;
471 NTSTATUS status;
473 TRACE( "(%lx,%lx,%p,%lx,%p)\n", access, share, sa, flags, data );
475 if (flags != CONSOLE_TEXTMODE_BUFFER || data)
477 SetLastError( ERROR_INVALID_PARAMETER );
478 return INVALID_HANDLE_VALUE;
481 RtlInitUnicodeString( &name, L"\\Device\\ConDrv\\ScreenBuffer" );
482 attr.ObjectName = &name;
483 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
484 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
485 status = NtCreateFile( &handle, access, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN,
486 FILE_NON_DIRECTORY_FILE, NULL, 0 );
487 return set_ntstatus( status ) ? handle : INVALID_HANDLE_VALUE;
491 /******************************************************************************
492 * CtrlRoutine (kernelbase.@)
494 DWORD WINAPI CtrlRoutine( void *arg )
496 DWORD_PTR event = (DWORD_PTR)arg;
497 struct ctrl_handler *handler;
499 if (event == CTRL_C_EVENT)
501 BOOL caught_by_dbg = TRUE;
502 /* First, try to pass the ctrl-C event to the debugger (if any)
503 * If it continues, there's nothing more to do
504 * Otherwise, we need to send the ctrl-C event to the handlers
506 __TRY
508 RaiseException( DBG_CONTROL_C, 0, 0, NULL );
510 __EXCEPT_ALL
512 caught_by_dbg = FALSE;
514 __ENDTRY
515 if (caught_by_dbg) return 0;
518 if (NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags & 1) return 0;
520 RtlEnterCriticalSection( &console_section );
521 for (handler = ctrl_handlers; handler; handler = handler->next)
523 if (handler->func( event )) break;
525 RtlLeaveCriticalSection( &console_section );
526 return 1;
530 /******************************************************************
531 * ExpungeConsoleCommandHistoryA (kernelbase.@)
533 void WINAPI ExpungeConsoleCommandHistoryA( LPCSTR exename )
535 FIXME( ": (%s) stub!\n", debugstr_a(exename) );
536 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
540 /******************************************************************
541 * ExpungeConsoleCommandHistoryW (kernelbase.@)
543 void WINAPI ExpungeConsoleCommandHistoryW( LPCWSTR exename )
545 FIXME( ": (%s) stub!\n", debugstr_w(exename) );
546 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
550 /******************************************************************************
551 * FillConsoleOutputAttribute (kernelbase.@)
553 BOOL WINAPI DECLSPEC_HOTPATCH FillConsoleOutputAttribute( HANDLE handle, WORD attr, DWORD length,
554 COORD coord, DWORD *written )
556 struct condrv_fill_output_params params;
558 TRACE( "(%p,%d,%ld,(%dx%d),%p)\n", handle, attr, length, coord.X, coord.Y, written );
560 if (!written)
562 SetLastError( ERROR_INVALID_ACCESS );
563 return FALSE;
566 *written = 0;
568 params.mode = CHAR_INFO_MODE_ATTR;
569 params.x = coord.X;
570 params.y = coord.Y;
571 params.count = length;
572 params.wrap = TRUE;
573 params.ch = 0;
574 params.attr = attr;
575 return console_ioctl( handle, IOCTL_CONDRV_FILL_OUTPUT, &params, sizeof(params),
576 written, sizeof(*written), NULL );
580 /******************************************************************************
581 * FillConsoleOutputCharacterA (kernelbase.@)
583 BOOL WINAPI DECLSPEC_HOTPATCH FillConsoleOutputCharacterA( HANDLE handle, CHAR ch, DWORD length,
584 COORD coord, DWORD *written )
586 WCHAR wch;
588 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch, 1, &wch, 1 );
589 return FillConsoleOutputCharacterW( handle, wch, length, coord, written );
593 /******************************************************************************
594 * FillConsoleOutputCharacterW (kernelbase.@)
596 BOOL WINAPI DECLSPEC_HOTPATCH FillConsoleOutputCharacterW( HANDLE handle, WCHAR ch, DWORD length,
597 COORD coord, DWORD *written )
599 struct condrv_fill_output_params params;
601 TRACE( "(%p,%s,%ld,(%dx%d),%p)\n", handle, debugstr_wn(&ch, 1), length, coord.X, coord.Y, written );
603 if (!written)
605 SetLastError( ERROR_INVALID_ACCESS );
606 return FALSE;
609 *written = 0;
611 params.mode = CHAR_INFO_MODE_TEXT;
612 params.x = coord.X;
613 params.y = coord.Y;
614 params.count = length;
615 params.wrap = TRUE;
616 params.ch = ch;
617 params.attr = 0;
618 return console_ioctl( handle, IOCTL_CONDRV_FILL_OUTPUT, &params, sizeof(params),
619 written, sizeof(*written), NULL );
623 /***********************************************************************
624 * FreeConsole (kernelbase.@)
626 BOOL WINAPI DECLSPEC_HOTPATCH FreeConsole(void)
628 RtlEnterCriticalSection( &console_section );
630 if (RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle != CONSOLE_HANDLE_SHELL_NO_WINDOW)
632 NtClose( console_connection );
633 console_connection = NULL;
635 NtClose( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle );
637 RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle = NULL;
639 if (console_flags & CONSOLE_INPUT_HANDLE) NtClose( GetStdHandle( STD_INPUT_HANDLE ));
640 if (console_flags & CONSOLE_OUTPUT_HANDLE) NtClose( GetStdHandle( STD_OUTPUT_HANDLE ));
641 if (console_flags & CONSOLE_ERROR_HANDLE) NtClose( GetStdHandle( STD_ERROR_HANDLE ));
642 console_flags = 0;
644 RtlLeaveCriticalSection( &console_section );
645 return TRUE;
649 /******************************************************************************
650 * GenerateConsoleCtrlEvent (kernelbase.@)
652 BOOL WINAPI DECLSPEC_HOTPATCH GenerateConsoleCtrlEvent( DWORD event, DWORD group )
654 struct condrv_ctrl_event ctrl_event;
656 TRACE( "(%ld, %lx)\n", event, group );
658 if (event != CTRL_C_EVENT && event != CTRL_BREAK_EVENT)
660 ERR( "Invalid event %ld for PGID %lx\n", event, group );
661 return FALSE;
664 ctrl_event.event = event;
665 ctrl_event.group_id = group;
666 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
667 IOCTL_CONDRV_CTRL_EVENT, &ctrl_event, sizeof(ctrl_event), NULL, 0, NULL );
671 /******************************************************************
672 * GetConsoleAliasA (kernelbase.@)
674 DWORD WINAPI GetConsoleAliasA( LPSTR source, LPSTR buffer, DWORD len, LPSTR exename )
676 FIXME( "(%s,%p,%ld,%s): stub\n", debugstr_a(source), buffer, len, debugstr_a(exename) );
677 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
678 return 0;
682 /******************************************************************
683 * GetConsoleAliasW (kernelbase.@)
685 DWORD WINAPI GetConsoleAliasW( LPWSTR source, LPWSTR buffer, DWORD len, LPWSTR exename )
687 FIXME( "(%s,%p,%ld,%s): stub\n", debugstr_w(source), buffer, len, debugstr_w(exename) );
688 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
689 return 0;
693 /******************************************************************
694 * GetConsoleAliasExesLengthA (kernelbase.@)
696 DWORD WINAPI GetConsoleAliasExesLengthA(void)
698 FIXME( ": stub\n" );
699 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
700 return 0;
704 /******************************************************************
705 * GetConsoleAliasExesLengthW (kernelbase.@)
707 DWORD WINAPI GetConsoleAliasExesLengthW(void)
709 FIXME( ": stub\n" );
710 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
711 return 0;
715 /******************************************************************
716 * GetConsoleAliasesLengthA (kernelbase.@)
718 DWORD WINAPI GetConsoleAliasesLengthA( LPSTR unknown )
720 FIXME( ": (%s) stub!\n", debugstr_a(unknown) );
721 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
722 return 0;
726 /******************************************************************
727 * GetConsoleAliasesLengthW (kernelbase.@)
729 DWORD WINAPI GetConsoleAliasesLengthW( LPWSTR unknown )
731 FIXME( ": (%s) stub!\n", debugstr_w(unknown) );
732 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
733 return 0;
737 /******************************************************************
738 * GetConsoleCommandHistoryA (kernelbase.@)
740 DWORD WINAPI GetConsoleCommandHistoryA( LPSTR buffer, DWORD len, LPCSTR exename )
742 FIXME( ": (%p, 0x%lx, %s) stub\n", buffer, len, debugstr_a(exename) );
743 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
744 return 0;
748 /******************************************************************
749 * GetConsoleCommandHistoryW (kernelbase.@)
751 DWORD WINAPI GetConsoleCommandHistoryW( LPWSTR buffer, DWORD len, LPCWSTR exename )
753 FIXME( ": (%p, 0x%lx, %s) stub\n", buffer, len, debugstr_w(exename) );
754 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
755 return 0;
759 /******************************************************************
760 * GetConsoleCommandHistoryLengthA (kernelbase.@)
762 DWORD WINAPI GetConsoleCommandHistoryLengthA( LPCSTR exename )
764 FIXME( ": (%s) stub!\n", debugstr_a(exename) );
765 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
766 return 0;
770 /******************************************************************
771 * GetConsoleCommandHistoryLengthW (kernelbase.@)
773 DWORD WINAPI GetConsoleCommandHistoryLengthW( LPCWSTR exename )
775 FIXME( ": (%s) stub!\n", debugstr_w(exename) );
776 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
777 return 0;
781 /******************************************************************************
782 * GetConsoleCP (kernelbase.@)
784 UINT WINAPI DECLSPEC_HOTPATCH GetConsoleCP(void)
786 struct condrv_input_info info;
788 if (!console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
789 IOCTL_CONDRV_GET_INPUT_INFO, NULL, 0, &info, sizeof(info), NULL ))
790 return 0;
791 return info.input_cp;
795 /******************************************************************************
796 * GetConsoleCursorInfo (kernelbase.@)
798 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleCursorInfo( HANDLE handle, CONSOLE_CURSOR_INFO *info )
800 struct condrv_output_info condrv_info;
802 if (!console_ioctl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &condrv_info, sizeof(condrv_info), NULL ))
803 return FALSE;
805 if (!info)
807 SetLastError( ERROR_INVALID_ACCESS );
808 return FALSE;
811 info->dwSize = condrv_info.cursor_size;
812 info->bVisible = condrv_info.cursor_visible;
813 TRACE("(%p) returning (%ld,%d)\n", handle, info->dwSize, info->bVisible);
814 return TRUE;
818 /***********************************************************************
819 * GetConsoleDisplayMode (kernelbase.@)
821 BOOL WINAPI GetConsoleDisplayMode( LPDWORD flags )
823 TRACE( "semi-stub: %p\n", flags );
824 /* It is safe to successfully report windowed mode */
825 *flags = 0;
826 return TRUE;
830 /***********************************************************************
831 * GetConsoleFontSize (kernelbase.@)
833 #if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
834 #undef GetConsoleFontSize
835 DWORD WINAPI GetConsoleFontSize( HANDLE handle, DWORD index )
837 union {
838 COORD c;
839 DWORD w;
840 } x;
842 x.c = get_console_font_size( handle, index );
843 return x.w;
845 #else
846 COORD WINAPI GetConsoleFontSize( HANDLE handle, DWORD index )
848 return get_console_font_size( handle, index );
850 #endif /* !defined(__i386__) */
853 /***********************************************************************
854 * GetConsoleInputExeNameA (kernelbase.@)
856 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleInputExeNameA( DWORD len, LPSTR buffer )
858 RtlEnterCriticalSection( &console_section );
859 if (WideCharToMultiByte( CP_ACP, 0, input_exe, -1, NULL, 0, NULL, NULL ) <= len)
860 WideCharToMultiByte( CP_ACP, 0, input_exe, -1, buffer, len, NULL, NULL );
861 else SetLastError(ERROR_BUFFER_OVERFLOW);
862 RtlLeaveCriticalSection( &console_section );
863 return TRUE;
867 /***********************************************************************
868 * GetConsoleInputExeNameW (kernelbase.@)
870 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleInputExeNameW( DWORD len, LPWSTR buffer )
872 RtlEnterCriticalSection( &console_section );
873 if (len > lstrlenW(input_exe)) lstrcpyW( buffer, input_exe );
874 else SetLastError( ERROR_BUFFER_OVERFLOW );
875 RtlLeaveCriticalSection( &console_section );
876 return TRUE;
880 /***********************************************************************
881 * GetConsoleMode (kernelbase.@)
883 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleMode( HANDLE handle, DWORD *mode )
885 return console_ioctl( handle, IOCTL_CONDRV_GET_MODE, NULL, 0, mode, sizeof(*mode), NULL );
889 /***********************************************************************
890 * GetConsoleOriginalTitleA (kernelbase.@)
892 DWORD WINAPI DECLSPEC_HOTPATCH GetConsoleOriginalTitleA( LPSTR title, DWORD size )
894 FIXME( ": (%p, %lu) stub!\n", title, size );
895 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
896 return 0;
900 /***********************************************************************
901 * GetConsoleOriginalTitleW (kernelbase.@)
903 DWORD WINAPI DECLSPEC_HOTPATCH GetConsoleOriginalTitleW( LPWSTR title, DWORD size )
905 FIXME( ": (%p, %lu) stub!\n", title, size );
906 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
907 return 0;
911 /***********************************************************************
912 * GetConsoleOutputCP (kernelbase.@)
914 UINT WINAPI DECLSPEC_HOTPATCH GetConsoleOutputCP(void)
916 struct condrv_input_info info;
918 if (!console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
919 IOCTL_CONDRV_GET_INPUT_INFO, NULL, 0, &info, sizeof(info), NULL ))
920 return 0;
921 return info.output_cp;
925 /***********************************************************************
926 * GetConsoleProcessList (kernelbase.@)
928 DWORD WINAPI GetConsoleProcessList( DWORD *list, DWORD count )
930 DWORD saved;
931 NTSTATUS status;
932 IO_STATUS_BLOCK io;
934 TRACE( "(%p,%ld)\n", list, count);
936 if (!list || count < 1)
938 SetLastError(ERROR_INVALID_PARAMETER);
939 return 0;
942 saved = *list;
943 status = NtDeviceIoControlFile( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
944 NULL, NULL, NULL, &io, IOCTL_CONDRV_GET_PROCESS_LIST,
945 NULL, 0, list, count * sizeof(DWORD) );
947 if (!status) return io.Information / sizeof(DWORD);
948 if (status == STATUS_BUFFER_TOO_SMALL)
950 DWORD ret = *list;
951 *list = saved;
952 return ret;
955 *list = saved;
956 set_ntstatus( status );
957 return 0;
961 /***********************************************************************
962 * GetConsoleScreenBufferInfo (kernelbase.@)
964 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleScreenBufferInfo( HANDLE handle, CONSOLE_SCREEN_BUFFER_INFO *info )
966 struct condrv_output_info condrv_info;
968 if (!console_ioctl( handle , IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0,
969 &condrv_info, sizeof(condrv_info), NULL ))
970 return FALSE;
972 info->dwSize.X = condrv_info.width;
973 info->dwSize.Y = condrv_info.height;
974 info->dwCursorPosition.X = condrv_info.cursor_x;
975 info->dwCursorPosition.Y = condrv_info.cursor_y;
976 info->wAttributes = condrv_info.attr;
977 info->srWindow.Left = condrv_info.win_left;
978 info->srWindow.Right = condrv_info.win_right;
979 info->srWindow.Top = condrv_info.win_top;
980 info->srWindow.Bottom = condrv_info.win_bottom;
981 info->dwMaximumWindowSize.X = min(condrv_info.width, condrv_info.max_width);
982 info->dwMaximumWindowSize.Y = min(condrv_info.height, condrv_info.max_height);
984 TRACE( "(%p,(%d,%d) (%d,%d) %d (%d,%d-%d,%d) (%d,%d)\n", handle,
985 info->dwSize.X, info->dwSize.Y, info->dwCursorPosition.X, info->dwCursorPosition.Y,
986 info->wAttributes, info->srWindow.Left, info->srWindow.Top, info->srWindow.Right,
987 info->srWindow.Bottom, info->dwMaximumWindowSize.X, info->dwMaximumWindowSize.Y );
988 return TRUE;
992 /***********************************************************************
993 * GetConsoleScreenBufferInfoEx (kernelbase.@)
995 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleScreenBufferInfoEx( HANDLE handle,
996 CONSOLE_SCREEN_BUFFER_INFOEX *info )
998 struct condrv_output_info condrv_info;
1000 if (info->cbSize != sizeof(CONSOLE_SCREEN_BUFFER_INFOEX))
1002 SetLastError( ERROR_INVALID_PARAMETER );
1003 return FALSE;
1006 if (!console_ioctl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &condrv_info,
1007 sizeof(condrv_info), NULL ))
1008 return FALSE;
1010 info->dwSize.X = condrv_info.width;
1011 info->dwSize.Y = condrv_info.height;
1012 info->dwCursorPosition.X = condrv_info.cursor_x;
1013 info->dwCursorPosition.Y = condrv_info.cursor_y;
1014 info->wAttributes = condrv_info.attr;
1015 info->srWindow.Left = condrv_info.win_left;
1016 info->srWindow.Top = condrv_info.win_top;
1017 info->srWindow.Right = condrv_info.win_right;
1018 info->srWindow.Bottom = condrv_info.win_bottom;
1019 info->dwMaximumWindowSize.X = min( condrv_info.width, condrv_info.max_width );
1020 info->dwMaximumWindowSize.Y = min( condrv_info.height, condrv_info.max_height );
1021 info->wPopupAttributes = condrv_info.popup_attr;
1022 info->bFullscreenSupported = FALSE;
1023 memcpy( info->ColorTable, condrv_info.color_map, sizeof(info->ColorTable) );
1024 return TRUE;
1028 /******************************************************************************
1029 * GetConsoleTitleA (kernelbase.@)
1031 DWORD WINAPI DECLSPEC_HOTPATCH GetConsoleTitleA( LPSTR title, DWORD size )
1033 WCHAR *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR) * size );
1034 DWORD ret;
1036 if (!ptr) return 0;
1038 ret = GetConsoleTitleW( ptr, size );
1039 if (ret)
1040 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, -1, title, size, NULL, NULL);
1042 HeapFree( GetProcessHeap(), 0, ptr );
1043 return ret;
1047 /******************************************************************************
1048 * GetConsoleTitleW (kernelbase.@)
1050 DWORD WINAPI DECLSPEC_HOTPATCH GetConsoleTitleW( LPWSTR title, DWORD size )
1052 struct condrv_title_params *params;
1053 size_t max_size = sizeof(*params) + (size - 1) * sizeof(WCHAR);
1055 if (!title || !size) return 0;
1057 if (!(params = HeapAlloc( GetProcessHeap(), 0, max_size )))
1058 return 0;
1060 if (console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, IOCTL_CONDRV_GET_TITLE,
1061 NULL, 0, params, max_size, &size ) &&
1062 size >= sizeof(*params))
1064 size -= sizeof(*params);
1065 memcpy( title, params->buffer, size );
1066 title[ size / sizeof(WCHAR) ] = 0;
1067 size = params->title_len;
1069 else size = 0;
1071 HeapFree( GetProcessHeap(), 0, params );
1072 return size;
1076 /******************************************************************************
1077 * GetConsoleWindow (kernelbase.@)
1079 HWND WINAPI GetConsoleWindow(void)
1081 condrv_handle_t win;
1082 BOOL ret;
1084 ret = DeviceIoControl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
1085 IOCTL_CONDRV_GET_WINDOW, NULL, 0, &win, sizeof(win), NULL, NULL );
1086 return ret ? LongToHandle( win ) : NULL;
1090 /***********************************************************************
1091 * GetCurrentConsoleFontEx (kernelbase.@)
1093 BOOL WINAPI GetCurrentConsoleFontEx( HANDLE handle, BOOL maxwindow, CONSOLE_FONT_INFOEX *info )
1095 DWORD size;
1096 struct
1098 struct condrv_output_info info;
1099 WCHAR face_name[LF_FACESIZE - 1];
1100 } data;
1102 if (info->cbSize != sizeof(CONSOLE_FONT_INFOEX))
1104 SetLastError(ERROR_INVALID_PARAMETER);
1105 return FALSE;
1108 if (!DeviceIoControl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0,
1109 &data, sizeof(data), &size, NULL ))
1111 SetLastError( ERROR_INVALID_HANDLE );
1112 return FALSE;
1115 info->nFont = 0;
1116 if (maxwindow)
1118 info->dwFontSize.X = min( data.info.width, data.info.max_width );
1119 info->dwFontSize.Y = min( data.info.height, data.info.max_height );
1121 else
1123 info->dwFontSize.X = data.info.font_width;
1124 info->dwFontSize.Y = data.info.font_height;
1126 size -= sizeof(data.info);
1127 if (size) memcpy( info->FaceName, data.face_name, size );
1128 info->FaceName[size / sizeof(WCHAR)] = 0;
1129 info->FontFamily = data.info.font_pitch_family;
1130 info->FontWeight = data.info.font_weight;
1131 return TRUE;
1135 /***********************************************************************
1136 * GetCurrentConsoleFont (kernelbase.@)
1138 BOOL WINAPI GetCurrentConsoleFont( HANDLE handle, BOOL maxwindow, CONSOLE_FONT_INFO *info )
1140 BOOL ret;
1141 CONSOLE_FONT_INFOEX res;
1143 res.cbSize = sizeof(CONSOLE_FONT_INFOEX);
1145 ret = GetCurrentConsoleFontEx( handle, maxwindow, &res );
1146 if (ret)
1148 info->nFont = res.nFont;
1149 info->dwFontSize.X = res.dwFontSize.X;
1150 info->dwFontSize.Y = res.dwFontSize.Y;
1152 return ret;
1156 /***********************************************************************
1157 * GetLargestConsoleWindowSize (kernelbase.@)
1159 #if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
1160 #undef GetLargestConsoleWindowSize
1161 DWORD WINAPI DECLSPEC_HOTPATCH GetLargestConsoleWindowSize( HANDLE handle )
1163 union {
1164 COORD c;
1165 DWORD w;
1166 } x;
1167 x.c = get_largest_console_window_size( handle );
1168 return x.w;
1171 #else
1173 COORD WINAPI DECLSPEC_HOTPATCH GetLargestConsoleWindowSize( HANDLE handle )
1175 return get_largest_console_window_size( handle );
1178 #endif /* !defined(__i386__) */
1181 /***********************************************************************
1182 * GetNumberOfConsoleInputEvents (kernelbase.@)
1184 BOOL WINAPI DECLSPEC_HOTPATCH GetNumberOfConsoleInputEvents( HANDLE handle, DWORD *count )
1186 struct condrv_input_info info;
1187 if (!console_ioctl( handle, IOCTL_CONDRV_GET_INPUT_INFO, NULL, 0, &info, sizeof(info), NULL ))
1188 return FALSE;
1189 *count = info.input_count;
1190 return TRUE;
1194 /***********************************************************************
1195 * GetNumberOfConsoleMouseButtons (kernelbase.@)
1197 BOOL WINAPI GetNumberOfConsoleMouseButtons( DWORD *count )
1199 FIXME( "(%p): stub\n", count );
1200 *count = 2;
1201 return TRUE;
1205 /***********************************************************************
1206 * PeekConsoleInputA (kernelbase.@)
1208 BOOL WINAPI DECLSPEC_HOTPATCH PeekConsoleInputA( HANDLE handle, INPUT_RECORD *buffer,
1209 DWORD length, DWORD *count )
1211 DWORD read;
1213 if (!PeekConsoleInputW( handle, buffer, length, &read )) return FALSE;
1214 input_records_WtoA( buffer, read );
1215 if (count) *count = read;
1216 return TRUE;
1220 /***********************************************************************
1221 * PeekConsoleInputW (kernelbase.@)
1223 BOOL WINAPI DECLSPEC_HOTPATCH PeekConsoleInputW( HANDLE handle, INPUT_RECORD *buffer,
1224 DWORD length, DWORD *count )
1226 DWORD read;
1227 if (!console_ioctl( handle, IOCTL_CONDRV_PEEK, NULL, 0, buffer, length * sizeof(*buffer), &read ))
1228 return FALSE;
1229 if (count) *count = read / sizeof(*buffer);
1230 return TRUE;
1234 /******************************************************************************
1235 * ReadConsoleOutputAttribute (kernelbase.@)
1237 BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputAttribute( HANDLE handle, WORD *attr, DWORD length,
1238 COORD coord, DWORD *count )
1240 struct condrv_output_params params;
1241 BOOL ret;
1243 TRACE( "(%p,%p,%ld,%dx%d,%p)\n", handle, attr, length, coord.X, coord.Y, count );
1245 if (!count)
1247 SetLastError( ERROR_INVALID_ACCESS );
1248 return FALSE;
1251 params.mode = CHAR_INFO_MODE_ATTR;
1252 params.x = coord.X;
1253 params.y = coord.Y;
1254 params.width = 0;
1255 ret = console_ioctl( handle, IOCTL_CONDRV_READ_OUTPUT, &params, sizeof(params),
1256 attr, length * sizeof(*attr), count );
1257 *count /= sizeof(*attr);
1258 return ret;
1262 /******************************************************************************
1263 * ReadConsoleOutputCharacterA (kernelbase.@)
1265 BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputCharacterA( HANDLE handle, LPSTR buffer, DWORD length,
1266 COORD coord, DWORD *count )
1268 DWORD read;
1269 BOOL ret;
1270 LPWSTR wptr;
1272 if (!count)
1274 SetLastError( ERROR_INVALID_ACCESS );
1275 return FALSE;
1278 *count = 0;
1279 if (!(wptr = HeapAlloc( GetProcessHeap(), 0, length * sizeof(WCHAR) )))
1281 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1282 return FALSE;
1284 if ((ret = ReadConsoleOutputCharacterW( handle, wptr, length, coord, &read )))
1286 read = WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr, read, buffer, length, NULL, NULL);
1287 *count = read;
1289 HeapFree( GetProcessHeap(), 0, wptr );
1290 return ret;
1294 /******************************************************************************
1295 * ReadConsoleOutputCharacterW (kernelbase.@)
1297 BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputCharacterW( HANDLE handle, LPWSTR buffer, DWORD length,
1298 COORD coord, DWORD *count )
1300 struct condrv_output_params params;
1301 BOOL ret;
1303 TRACE( "(%p,%p,%ld,%dx%d,%p)\n", handle, buffer, length, coord.X, coord.Y, count );
1305 if (!count)
1307 SetLastError( ERROR_INVALID_ACCESS );
1308 return FALSE;
1311 params.mode = CHAR_INFO_MODE_TEXT;
1312 params.x = coord.X;
1313 params.y = coord.Y;
1314 params.width = 0;
1315 ret = console_ioctl( handle, IOCTL_CONDRV_READ_OUTPUT, &params, sizeof(params), buffer,
1316 length * sizeof(*buffer), count );
1317 *count /= sizeof(*buffer);
1318 return ret;
1322 /******************************************************************************
1323 * ReadConsoleOutputA (kernelbase.@)
1325 BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputA( HANDLE handle, CHAR_INFO *buffer, COORD size,
1326 COORD coord, SMALL_RECT *region )
1328 BOOL ret;
1329 int y;
1331 ret = ReadConsoleOutputW( handle, buffer, size, coord, region );
1332 if (ret && region->Right >= region->Left)
1334 UINT cp = GetConsoleOutputCP();
1335 for (y = 0; y <= region->Bottom - region->Top; y++)
1336 char_info_WtoA( cp, &buffer[(coord.Y + y) * size.X + coord.X], region->Right - region->Left + 1 );
1338 return ret;
1342 /******************************************************************************
1343 * ReadConsoleOutputW (kernelbase.@)
1345 BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputW( HANDLE handle, CHAR_INFO *buffer, COORD size,
1346 COORD coord, SMALL_RECT *region )
1348 struct condrv_output_params params;
1349 unsigned int width, height, y;
1350 SMALL_RECT *result;
1351 DWORD count;
1352 BOOL ret;
1354 if (region->Left > region->Right || region->Top > region->Bottom)
1356 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1357 return FALSE;
1359 if (size.X <= coord.X || size.Y <= coord.Y)
1361 region->Right = region->Left - 1;
1362 region->Bottom = region->Top - 1;
1363 SetLastError( ERROR_INVALID_FUNCTION );
1364 return FALSE;
1366 width = min( region->Right - region->Left + 1, size.X - coord.X );
1367 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
1368 region->Right = region->Left + width - 1;
1369 region->Bottom = region->Top + height - 1;
1371 count = sizeof(*result) + width * height * sizeof(*buffer);
1372 if (!(result = HeapAlloc( GetProcessHeap(), 0, count )))
1374 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1375 return FALSE;
1378 params.mode = CHAR_INFO_MODE_TEXTATTR;
1379 params.x = region->Left;
1380 params.y = region->Top;
1381 params.width = width;
1382 if ((ret = console_ioctl( handle, IOCTL_CONDRV_READ_OUTPUT, &params, sizeof(params), result, count, &count )) && count)
1384 CHAR_INFO *char_info = (CHAR_INFO *)(result + 1);
1385 *region = *result;
1386 width = region->Right - region->Left + 1;
1387 height = region->Bottom - region->Top + 1;
1388 for (y = 0; y < height; y++)
1389 memcpy( &buffer[(y + coord.Y) * size.X + coord.X], &char_info[y * width], width * sizeof(*buffer) );
1391 HeapFree( GetProcessHeap(), 0, result );
1392 return ret;
1396 /******************************************************************************
1397 * ScrollConsoleScreenBufferA (kernelbase.@)
1399 BOOL WINAPI DECLSPEC_HOTPATCH ScrollConsoleScreenBufferA( HANDLE handle, const SMALL_RECT *scroll,
1400 const SMALL_RECT *clip, COORD origin, const CHAR_INFO *fill )
1402 CHAR_INFO ciW;
1404 ciW.Attributes = fill->Attributes;
1405 MultiByteToWideChar( GetConsoleOutputCP(), 0, &fill->Char.AsciiChar, 1, &ciW.Char.UnicodeChar, 1 );
1407 return ScrollConsoleScreenBufferW( handle, scroll, clip, origin, &ciW );
1411 /******************************************************************************
1412 * ScrollConsoleScreenBufferW (kernelbase.@)
1414 BOOL WINAPI DECLSPEC_HOTPATCH ScrollConsoleScreenBufferW( HANDLE handle, const SMALL_RECT *scroll,
1415 const SMALL_RECT *clip_rect, COORD origin,
1416 const CHAR_INFO *fill )
1418 struct condrv_scroll_params params;
1420 if (clip_rect)
1421 TRACE( "(%p,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", handle,
1422 scroll->Left, scroll->Top, scroll->Right, scroll->Bottom,
1423 clip_rect->Left, clip_rect->Top, clip_rect->Right, clip_rect->Bottom,
1424 origin.X, origin.Y, fill );
1425 else
1426 TRACE("(%p,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", handle,
1427 scroll->Left, scroll->Top, scroll->Right, scroll->Bottom,
1428 origin.X, origin.Y, fill );
1430 params.scroll = *scroll;
1431 params.origin = origin;
1432 params.fill.ch = fill->Char.UnicodeChar;
1433 params.fill.attr = fill->Attributes;
1434 if (!clip_rect)
1436 params.clip.Left = params.clip.Top = 0;
1437 params.clip.Right = params.clip.Bottom = SHRT_MAX;
1439 else params.clip = *clip_rect;
1440 return console_ioctl( handle, IOCTL_CONDRV_SCROLL, (void *)&params, sizeof(params), NULL, 0, NULL );
1444 /******************************************************************************
1445 * SetConsoleActiveScreenBuffer (kernelbase.@)
1447 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleActiveScreenBuffer( HANDLE handle )
1449 TRACE( "(%p)\n", handle );
1450 return console_ioctl( handle, IOCTL_CONDRV_ACTIVATE, NULL, 0, NULL, 0, NULL );
1454 /******************************************************************************
1455 * SetConsoleCP (kernelbase.@)
1457 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCP( UINT cp )
1459 struct condrv_input_info_params params = { SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE };
1461 params.info.input_cp = cp;
1462 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
1463 IOCTL_CONDRV_SET_INPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1467 /******************************************************************************
1468 * SetConsoleCtrlHandler (kernelbase.@)
1470 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCtrlHandler( PHANDLER_ROUTINE func, BOOL add )
1472 struct ctrl_handler *handler;
1473 BOOL ret = FALSE;
1475 TRACE( "(%p,%d)\n", func, add );
1477 RtlEnterCriticalSection( &console_section );
1479 if (!func)
1481 if (add) NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags |= 1;
1482 else NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags &= ~1;
1483 ret = TRUE;
1485 else if (add)
1487 if ((handler = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler) )))
1489 handler->func = func;
1490 handler->next = ctrl_handlers;
1491 ctrl_handlers = handler;
1492 ret = TRUE;
1495 else
1497 struct ctrl_handler **p_handler;
1499 for (p_handler = &ctrl_handlers; *p_handler; p_handler = &(*p_handler)->next)
1501 if ((*p_handler)->func == func) break;
1503 if (*p_handler && *p_handler != &default_handler)
1505 handler = *p_handler;
1506 *p_handler = handler->next;
1507 RtlFreeHeap( GetProcessHeap(), 0, handler );
1508 ret = TRUE;
1510 else SetLastError( ERROR_INVALID_PARAMETER );
1513 RtlLeaveCriticalSection( &console_section );
1514 return ret;
1518 /******************************************************************************
1519 * SetConsoleCursorInfo (kernelbase.@)
1521 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCursorInfo( HANDLE handle, CONSOLE_CURSOR_INFO *info )
1523 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM };
1525 TRACE( "(%p,%ld,%d)\n", handle, info->dwSize, info->bVisible);
1527 params.info.cursor_size = info->dwSize;
1528 params.info.cursor_visible = info->bVisible;
1529 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params),
1530 NULL, 0, NULL );
1534 /******************************************************************************
1535 * SetConsoleCursorPosition (kernelbase.@)
1537 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCursorPosition( HANDLE handle, COORD pos )
1539 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_CURSOR_POS };
1541 TRACE( "%p %d %d\n", handle, pos.X, pos.Y );
1543 params.info.cursor_x = pos.X;
1544 params.info.cursor_y = pos.Y;
1545 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1549 /***********************************************************************
1550 * SetConsoleDisplayMode (kernelbase.@)
1552 BOOL WINAPI SetConsoleDisplayMode( HANDLE handle, DWORD flags, COORD *size )
1554 TRACE( "(%p, %lx, (%d, %d))\n", handle, flags, size->X, size->Y );
1555 if (flags == 1)
1557 /* We cannot switch to fullscreen */
1558 return FALSE;
1560 return TRUE;
1564 /******************************************************************************
1565 * SetConsoleInputExeNameA (kernelbase.@)
1567 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleInputExeNameA( LPCSTR name )
1569 if (!name || !name[0])
1571 SetLastError( ERROR_INVALID_PARAMETER );
1572 return FALSE;
1574 RtlEnterCriticalSection( &console_section );
1575 MultiByteToWideChar( CP_ACP, 0, name, -1, input_exe, ARRAY_SIZE(input_exe) );
1576 RtlLeaveCriticalSection( &console_section );
1577 return TRUE;
1581 /******************************************************************************
1582 * SetConsoleInputExeNameW (kernelbase.@)
1584 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleInputExeNameW( LPCWSTR name )
1586 if (!name || !name[0])
1588 SetLastError( ERROR_INVALID_PARAMETER );
1589 return FALSE;
1591 RtlEnterCriticalSection( &console_section );
1592 lstrcpynW( input_exe, name, ARRAY_SIZE(input_exe) );
1593 RtlLeaveCriticalSection( &console_section );
1594 return TRUE;
1598 /******************************************************************************
1599 * SetConsoleMode (kernelbase.@)
1601 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleMode( HANDLE handle, DWORD mode )
1603 TRACE( "(%p,%lx)\n", handle, mode );
1604 return console_ioctl( handle, IOCTL_CONDRV_SET_MODE, &mode, sizeof(mode), NULL, 0, NULL );
1608 /******************************************************************************
1609 * SetConsoleOutputCP (kernelbase.@)
1611 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleOutputCP( UINT cp )
1613 struct condrv_input_info_params params = { SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE };
1615 params.info.output_cp = cp;
1616 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
1617 IOCTL_CONDRV_SET_INPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1621 /******************************************************************************
1622 * SetConsoleScreenBufferInfoEx (kernelbase.@)
1624 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleScreenBufferInfoEx( HANDLE handle,
1625 CONSOLE_SCREEN_BUFFER_INFOEX *info )
1627 struct condrv_output_info_params params =
1628 { SET_CONSOLE_OUTPUT_INFO_CURSOR_POS | SET_CONSOLE_OUTPUT_INFO_SIZE |
1629 SET_CONSOLE_OUTPUT_INFO_ATTR | SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR |
1630 SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW | SET_CONSOLE_OUTPUT_INFO_MAX_SIZE };
1632 TRACE("(%p, %p)\n", handle, info);
1634 if (info->cbSize != sizeof(CONSOLE_SCREEN_BUFFER_INFOEX))
1636 SetLastError(ERROR_INVALID_PARAMETER);
1637 return FALSE;
1640 params.info.width = info->dwSize.X;
1641 params.info.height = info->dwSize.Y;
1642 params.info.cursor_x = info->dwCursorPosition.X;
1643 params.info.cursor_y = info->dwCursorPosition.Y;
1644 params.info.attr = info->wAttributes;
1645 params.info.win_left = info->srWindow.Left;
1646 params.info.win_top = info->srWindow.Top;
1647 params.info.win_right = info->srWindow.Right;
1648 params.info.win_bottom = info->srWindow.Bottom;
1649 params.info.popup_attr = info->wPopupAttributes;
1650 params.info.max_width = min( info->dwMaximumWindowSize.X, info->dwSize.X );
1651 params.info.max_height = min( info->dwMaximumWindowSize.Y, info->dwSize.Y );
1652 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1656 /******************************************************************************
1657 * SetConsoleScreenBufferSize (kernelbase.@)
1659 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleScreenBufferSize( HANDLE handle, COORD size )
1661 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_SIZE };
1663 TRACE( "(%p,(%d,%d))\n", handle, size.X, size.Y );
1665 params.info.width = size.X;
1666 params.info.height = size.Y;
1667 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1671 /******************************************************************************
1672 * SetConsoleTextAttribute (kernelbase.@)
1674 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleTextAttribute( HANDLE handle, WORD attr )
1676 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_ATTR };
1678 TRACE( "(%p,%d)\n", handle, attr );
1680 params.info.attr = attr;
1681 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1685 /******************************************************************************
1686 * SetConsoleTitleA (kernelbase.@)
1688 BOOL WINAPI SetConsoleTitleA( LPCSTR title )
1690 LPWSTR titleW;
1691 BOOL ret;
1692 DWORD len = MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, NULL, 0 );
1693 if (!(titleW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
1694 MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, titleW, len );
1695 ret = SetConsoleTitleW(titleW);
1696 HeapFree( GetProcessHeap(), 0, titleW );
1697 return ret;
1701 /******************************************************************************
1702 * SetConsoleTitleW (kernelbase.@)
1704 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleTitleW( LPCWSTR title )
1706 TRACE( "%s\n", debugstr_w( title ));
1708 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, IOCTL_CONDRV_SET_TITLE,
1709 (void *)title, lstrlenW(title) * sizeof(WCHAR), NULL, 0, NULL );
1713 /******************************************************************************
1714 * SetConsoleWindowInfo (kernelbase.@)
1716 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleWindowInfo( HANDLE handle, BOOL absolute, SMALL_RECT *window )
1718 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW };
1719 SMALL_RECT rect = *window;
1721 TRACE( "(%p,%d,(%d,%d-%d,%d))\n", handle, absolute, rect.Left, rect.Top, rect.Right, rect.Bottom );
1723 if (!absolute)
1725 CONSOLE_SCREEN_BUFFER_INFO info;
1727 if (!GetConsoleScreenBufferInfo( handle, &info )) return FALSE;
1728 rect.Left += info.srWindow.Left;
1729 rect.Top += info.srWindow.Top;
1730 rect.Right += info.srWindow.Right;
1731 rect.Bottom += info.srWindow.Bottom;
1734 params.info.win_left = rect.Left;
1735 params.info.win_top = rect.Top;
1736 params.info.win_right = rect.Right;
1737 params.info.win_bottom = rect.Bottom;
1738 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1742 /******************************************************************************
1743 * SetCurrentConsoleFontEx (kernelbase.@)
1745 BOOL WINAPI SetCurrentConsoleFontEx( HANDLE handle, BOOL maxwindow, CONSOLE_FONT_INFOEX *info )
1747 struct
1749 struct condrv_output_info_params params;
1750 WCHAR face_name[LF_FACESIZE];
1751 } data;
1753 size_t size;
1755 TRACE( "(%p %d %p)\n", handle, maxwindow, info );
1757 if (info->cbSize != sizeof(*info))
1759 SetLastError(ERROR_INVALID_PARAMETER);
1760 return FALSE;
1763 data.params.mask = SET_CONSOLE_OUTPUT_INFO_FONT;
1765 data.params.info.font_width = info->dwFontSize.X;
1766 data.params.info.font_height = info->dwFontSize.Y;
1767 data.params.info.font_pitch_family = info->FontFamily;
1768 data.params.info.font_weight = info->FontWeight;
1770 size = wcsnlen( info->FaceName, LF_FACESIZE - 1 ) * sizeof(WCHAR);
1771 memcpy( data.face_name, info->FaceName, size );
1773 size += sizeof(struct condrv_output_info_params);
1774 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &data, size, NULL, 0, NULL );
1778 /***********************************************************************
1779 * ReadConsoleInputA (kernelbase.@)
1781 BOOL WINAPI ReadConsoleInputA( HANDLE handle, INPUT_RECORD *buffer, DWORD length, DWORD *count )
1783 DWORD read;
1785 if (!ReadConsoleInputW( handle, buffer, length, &read )) return FALSE;
1786 input_records_WtoA( buffer, read );
1787 if (count) *count = read;
1788 return TRUE;
1792 /***********************************************************************
1793 * ReadConsoleInputW (kernelbase.@)
1795 BOOL WINAPI ReadConsoleInputW( HANDLE handle, INPUT_RECORD *buffer, DWORD length, DWORD *count )
1797 if (!console_ioctl( handle, IOCTL_CONDRV_READ_INPUT, NULL, 0,
1798 buffer, length * sizeof(*buffer), count ))
1799 return FALSE;
1800 *count /= sizeof(*buffer);
1801 return TRUE;
1805 /******************************************************************************
1806 * WriteConsoleInputA (kernelbase.@)
1808 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleInputA( HANDLE handle, const INPUT_RECORD *buffer,
1809 DWORD count, DWORD *written )
1811 INPUT_RECORD *recW = NULL;
1812 BOOL ret;
1814 if (count > 0)
1816 if (!buffer)
1818 SetLastError( ERROR_INVALID_ACCESS );
1819 return FALSE;
1821 if (!(recW = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*recW) )))
1823 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1824 return FALSE;
1826 memcpy( recW, buffer, count * sizeof(*recW) );
1827 input_records_AtoW( recW, count );
1829 ret = WriteConsoleInputW( handle, recW, count, written );
1830 HeapFree( GetProcessHeap(), 0, recW );
1831 return ret;
1835 /******************************************************************************
1836 * WriteConsoleInputW (kernelbase.@)
1838 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleInputW( HANDLE handle, const INPUT_RECORD *buffer,
1839 DWORD count, DWORD *written )
1841 TRACE( "(%p,%p,%ld,%p)\n", handle, buffer, count, written );
1843 if (count > 0 && !buffer)
1845 SetLastError( ERROR_INVALID_ACCESS );
1846 return FALSE;
1849 if (!DeviceIoControl( handle, IOCTL_CONDRV_WRITE_INPUT, (void *)buffer, count * sizeof(*buffer), NULL, 0, NULL, NULL ))
1850 return FALSE;
1852 if (!written)
1854 SetLastError( ERROR_INVALID_ACCESS );
1855 return FALSE;
1857 *written = count;
1858 return TRUE;
1862 /***********************************************************************
1863 * WriteConsoleOutputA (kernelbase.@)
1865 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputA( HANDLE handle, const CHAR_INFO *buffer,
1866 COORD size, COORD coord, SMALL_RECT *region )
1868 int y;
1869 BOOL ret;
1870 COORD new_size, new_coord;
1871 CHAR_INFO *ciW;
1873 new_size.X = min( region->Right - region->Left + 1, size.X - coord.X );
1874 new_size.Y = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
1876 if (new_size.X <= 0 || new_size.Y <= 0)
1878 region->Bottom = region->Top + new_size.Y - 1;
1879 region->Right = region->Left + new_size.X - 1;
1880 return TRUE;
1883 /* only copy the useful rectangle */
1884 if (!(ciW = HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO) * new_size.X * new_size.Y )))
1885 return FALSE;
1886 for (y = 0; y < new_size.Y; y++)
1887 memcpy( &ciW[y * new_size.X], &buffer[(y + coord.Y) * size.X + coord.X],
1888 new_size.X * sizeof(CHAR_INFO) );
1889 char_info_AtoW( ciW, new_size.X * new_size.Y );
1890 new_coord.X = new_coord.Y = 0;
1891 ret = WriteConsoleOutputW( handle, ciW, new_size, new_coord, region );
1892 HeapFree( GetProcessHeap(), 0, ciW );
1893 return ret;
1897 /***********************************************************************
1898 * WriteConsoleOutputW (kernelbase.@)
1900 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputW( HANDLE handle, const CHAR_INFO *buffer,
1901 COORD size, COORD coord, SMALL_RECT *region )
1903 struct condrv_output_params *params;
1904 unsigned int width, height, y;
1905 size_t params_size;
1906 BOOL ret;
1908 TRACE( "(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n",
1909 handle, buffer, size.X, size.Y, coord.X, coord.Y,
1910 region->Left, region->Top, region->Right, region->Bottom );
1912 if (region->Left > region->Right || region->Top > region->Bottom || size.X <= coord.X || size.Y <= coord.Y)
1914 SetLastError( ERROR_INVALID_PARAMETER );
1915 return FALSE;
1918 width = min( region->Right - region->Left + 1, size.X - coord.X );
1919 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
1920 region->Right = region->Left + width - 1;
1921 region->Bottom = region->Top + height - 1;
1923 params_size = sizeof(*params) + width * height * sizeof(*buffer);
1924 if (!(params = HeapAlloc( GetProcessHeap(), 0, params_size )))
1926 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1927 return FALSE;
1930 params->mode = CHAR_INFO_MODE_TEXTATTR;
1931 params->x = region->Left;
1932 params->y = region->Top;
1933 params->width = width;
1935 for (y = 0; y < height; y++)
1936 memcpy( &((CHAR_INFO *)(params + 1))[y * width], &buffer[(y + coord.Y) * size.X + coord.X], width * sizeof(CHAR_INFO) );
1938 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_OUTPUT, params, params_size, region, sizeof(*region), NULL );
1939 HeapFree( GetProcessHeap(), 0, params );
1940 return ret;
1944 /******************************************************************************
1945 * WriteConsoleOutputAttribute (kernelbase.@)
1947 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputAttribute( HANDLE handle, const WORD *attr, DWORD length,
1948 COORD coord, DWORD *written )
1950 struct condrv_output_params *params;
1951 size_t size;
1952 BOOL ret;
1954 TRACE( "(%p,%p,%ld,%dx%d,%p)\n", handle, attr, length, coord.X, coord.Y, written );
1956 if ((length > 0 && !attr) || !written)
1958 SetLastError( ERROR_INVALID_ACCESS );
1959 return FALSE;
1962 *written = 0;
1963 size = sizeof(*params) + length * sizeof(WORD);
1964 if (!(params = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1965 params->mode = CHAR_INFO_MODE_ATTR;
1966 params->x = coord.X;
1967 params->y = coord.Y;
1968 params->width = 0;
1969 memcpy( params + 1, attr, length * sizeof(*attr) );
1970 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_OUTPUT, params, size, written, sizeof(*written), NULL );
1971 HeapFree( GetProcessHeap(), 0, params );
1972 return ret;
1976 /******************************************************************************
1977 * WriteConsoleOutputCharacterA (kernelbase.@)
1979 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputCharacterA( HANDLE handle, LPCSTR str, DWORD length,
1980 COORD coord, DWORD *written )
1982 BOOL ret;
1983 LPWSTR strW = NULL;
1984 DWORD lenW = 0;
1986 TRACE( "(%p,%s,%ld,%dx%d,%p)\n", handle, debugstr_an(str, length), length, coord.X, coord.Y, written );
1988 if (length > 0)
1990 UINT cp = GetConsoleOutputCP();
1991 if (!str)
1993 SetLastError( ERROR_INVALID_ACCESS );
1994 return FALSE;
1996 lenW = MultiByteToWideChar( cp, 0, str, length, NULL, 0 );
1998 if (!(strW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
2000 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
2001 return FALSE;
2003 MultiByteToWideChar( cp, 0, str, length, strW, lenW );
2005 ret = WriteConsoleOutputCharacterW( handle, strW, lenW, coord, written );
2006 HeapFree( GetProcessHeap(), 0, strW );
2007 return ret;
2011 /******************************************************************************
2012 * WriteConsoleOutputCharacterW (kernelbase.@)
2014 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputCharacterW( HANDLE handle, LPCWSTR str, DWORD length,
2015 COORD coord, DWORD *written )
2017 struct condrv_output_params *params;
2018 size_t size;
2019 BOOL ret;
2021 TRACE( "(%p,%s,%ld,%dx%d,%p)\n", handle, debugstr_wn(str, length), length, coord.X, coord.Y, written );
2023 if ((length > 0 && !str) || !written)
2025 SetLastError( ERROR_INVALID_ACCESS );
2026 return FALSE;
2029 *written = 0;
2030 size = sizeof(*params) + length * sizeof(WCHAR);
2031 if (!(params = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
2032 params->mode = CHAR_INFO_MODE_TEXT;
2033 params->x = coord.X;
2034 params->y = coord.Y;
2035 params->width = 0;
2036 memcpy( params + 1, str, length * sizeof(*str) );
2037 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_OUTPUT, params, size, written, sizeof(*written), NULL );
2038 HeapFree( GetProcessHeap(), 0, params );
2039 return ret;
2043 /***********************************************************************
2044 * ReadConsoleA (kernelbase.@)
2046 BOOL WINAPI ReadConsoleA( HANDLE handle, void *buffer, DWORD length, DWORD *count, void *reserved )
2048 if (length > INT_MAX)
2050 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
2051 return FALSE;
2054 return console_ioctl( handle, IOCTL_CONDRV_READ_FILE, NULL, 0, buffer, length, count );
2058 /***********************************************************************
2059 * ReadConsoleW (kernelbase.@)
2061 BOOL WINAPI ReadConsoleW( HANDLE handle, void *buffer, DWORD length, DWORD *count, void *reserved )
2063 BOOL ret;
2065 TRACE( "(%p,%p,%ld,%p,%p)\n", handle, buffer, length, count, reserved );
2067 if (length > INT_MAX)
2069 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2070 return FALSE;
2073 if (reserved)
2075 CONSOLE_READCONSOLE_CONTROL* crc = reserved;
2076 char *tmp;
2078 if (crc->nLength != sizeof(*crc) || crc->nInitialChars >= length)
2080 SetLastError( ERROR_INVALID_PARAMETER );
2081 return FALSE;
2083 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD) + length * sizeof(WCHAR) )))
2085 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
2086 return FALSE;
2089 memcpy( tmp, &crc->dwCtrlWakeupMask, sizeof(DWORD) );
2090 memcpy( tmp + sizeof(DWORD), buffer, crc->nInitialChars * sizeof(WCHAR) );
2091 ret = console_ioctl( handle, IOCTL_CONDRV_READ_CONSOLE_CONTROL,
2092 tmp, sizeof(DWORD) + crc->nInitialChars * sizeof(WCHAR),
2093 tmp, sizeof(DWORD) + length * sizeof(WCHAR), count );
2094 if (ret)
2096 memcpy( &crc->dwConsoleKeyState, tmp, sizeof(DWORD) );
2097 *count -= sizeof(DWORD);
2098 memcpy( buffer, tmp + sizeof(DWORD), *count );
2100 HeapFree( GetProcessHeap(), 0, tmp );
2102 else
2104 ret = console_ioctl( handle, IOCTL_CONDRV_READ_CONSOLE, NULL, 0, buffer,
2105 length * sizeof(WCHAR), count );
2107 if (ret) *count /= sizeof(WCHAR);
2108 return ret;
2112 /***********************************************************************
2113 * WriteConsoleA (kernelbase.@)
2115 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleA( HANDLE handle, const void *buffer, DWORD length,
2116 DWORD *written, void *reserved )
2118 BOOL ret;
2120 TRACE( "(%p,%s,%ld,%p,%p)\n", handle, debugstr_an(buffer, length), length, written, reserved );
2122 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_FILE, (void *)buffer, length, NULL, 0, NULL );
2123 if (written) *written = ret ? length : 0;
2124 return ret;
2128 /***********************************************************************
2129 * WriteConsoleW (kernelbase.@)
2131 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleW( HANDLE handle, const void *buffer, DWORD length,
2132 DWORD *written, void *reserved )
2134 BOOL ret;
2136 TRACE( "(%p,%s,%ld,%p,%p)\n", handle, debugstr_wn(buffer, length), length, written, reserved );
2138 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_CONSOLE, (void *)buffer,
2139 length * sizeof(WCHAR), NULL, 0, NULL );
2140 if (written) *written = ret ? length : 0;
2141 return ret;
2145 /***********************************************************************
2146 * FlushConsoleInputBuffer (kernelbase.@)
2148 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
2150 return console_ioctl( handle, IOCTL_CONDRV_FLUSH, NULL, 0, NULL, 0, NULL );
2154 /***********************************************************************
2155 * Beep (kernelbase.@)
2157 BOOL WINAPI Beep( DWORD frequency, DWORD duration )
2159 /* FIXME: we should not require a console to be attached */
2160 console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
2161 IOCTL_CONDRV_BEEP, NULL, 0, NULL, 0, NULL );
2162 return TRUE;
2166 static HANDLE create_pseudo_console( COORD size, HANDLE input, HANDLE output, HANDLE signal,
2167 DWORD flags, HANDLE *process )
2169 WCHAR cmd[MAX_PATH], conhost_path[MAX_PATH];
2170 PROCESS_INFORMATION pi;
2171 HANDLE server, console;
2172 STARTUPINFOEXW si;
2173 void *redir;
2174 BOOL res;
2176 if (!(server = create_console_server())) return NULL;
2178 console = create_console_reference( server );
2179 if (!console)
2181 NtClose( server );
2182 return NULL;
2185 memset( &si, 0, sizeof(si) );
2186 si.StartupInfo.cb = sizeof(STARTUPINFOEXW);
2187 si.StartupInfo.hStdInput = input;
2188 si.StartupInfo.hStdOutput = output;
2189 si.StartupInfo.hStdError = output;
2190 si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
2191 swprintf( conhost_path, ARRAY_SIZE(conhost_path), L"%s\\conhost.exe", system_dir );
2192 if (signal)
2194 swprintf( cmd, ARRAY_SIZE(cmd),
2195 L"\"%s\" --headless %s--width %u --height %u --signal 0x%x --server 0x%x",
2196 conhost_path, (flags & PSEUDOCONSOLE_INHERIT_CURSOR) ? L"--inheritcursor " : L"",
2197 size.X, size.Y, signal, server );
2199 else
2201 swprintf( cmd, ARRAY_SIZE(cmd), L"\"%s\" --unix --width %u --height %u --server 0x%x",
2202 conhost_path, size.X, size.Y, server );
2204 Wow64DisableWow64FsRedirection( &redir );
2205 res = CreateProcessW( conhost_path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL,
2206 &si.StartupInfo, &pi );
2207 Wow64RevertWow64FsRedirection( redir );
2208 NtClose( server );
2209 if (!res)
2211 NtClose( console );
2212 return NULL;
2215 NtClose( pi.hThread );
2216 *process = pi.hProcess;
2217 return console;
2220 /******************************************************************************
2221 * CreatePseudoConsole (kernelbase.@)
2223 HRESULT WINAPI CreatePseudoConsole( COORD size, HANDLE input, HANDLE output, DWORD flags, HPCON *ret )
2225 SECURITY_ATTRIBUTES inherit_attr = { sizeof(inherit_attr), NULL, TRUE };
2226 struct pseudo_console *pseudo_console;
2227 HANDLE tty_input = NULL, tty_output;
2228 HANDLE signal = NULL;
2229 WCHAR pipe_name[64];
2231 TRACE( "(%u,%u) %p %p %lx %p\n", size.X, size.Y, input, output, flags, ret );
2233 if (!size.X || !size.Y || !ret) return E_INVALIDARG;
2235 if (!(pseudo_console = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pseudo_console) ))) return E_OUTOFMEMORY;
2237 swprintf( pipe_name, ARRAY_SIZE(pipe_name), L"\\\\.\\pipe\\wine_pty_signal_pipe%x",
2238 GetCurrentThreadId() );
2239 signal = CreateNamedPipeW( pipe_name, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE,
2240 PIPE_UNLIMITED_INSTANCES, 4096, 4096, NMPWAIT_USE_DEFAULT_WAIT, &inherit_attr );
2241 if (signal == INVALID_HANDLE_VALUE)
2243 HeapFree( GetProcessHeap(), 0, pseudo_console );
2244 return HRESULT_FROM_WIN32( GetLastError() );
2246 pseudo_console->signal = CreateFileW( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
2247 if (pseudo_console->signal != INVALID_HANDLE_VALUE &&
2248 DuplicateHandle( GetCurrentProcess(), input, GetCurrentProcess(), &tty_input, 0, TRUE, DUPLICATE_SAME_ACCESS) &&
2249 DuplicateHandle( GetCurrentProcess(), output, GetCurrentProcess(), &tty_output, 0, TRUE, DUPLICATE_SAME_ACCESS))
2251 pseudo_console->reference = create_pseudo_console( size, tty_input, tty_output, signal, flags,
2252 &pseudo_console->process );
2253 NtClose( tty_output );
2255 NtClose( tty_input );
2256 NtClose( signal );
2257 if (!pseudo_console->reference)
2259 ClosePseudoConsole( pseudo_console );
2260 return HRESULT_FROM_WIN32( GetLastError() );
2263 *ret = pseudo_console;
2264 return S_OK;
2267 /******************************************************************************
2268 * ClosePseudoConsole (kernelbase.@)
2270 void WINAPI ClosePseudoConsole( HPCON handle )
2272 struct pseudo_console *pseudo_console = handle;
2274 TRACE( "%p\n", handle );
2276 if (!pseudo_console) return;
2277 if (pseudo_console->signal) CloseHandle( pseudo_console->signal );
2278 if (pseudo_console->process)
2280 WaitForSingleObject( pseudo_console->process, INFINITE );
2281 CloseHandle( pseudo_console->process );
2283 if (pseudo_console->reference) CloseHandle( pseudo_console->reference );
2286 /******************************************************************************
2287 * ResizePseudoConsole (kernelbase.@)
2289 HRESULT WINAPI ResizePseudoConsole( HPCON handle, COORD size )
2291 FIXME( "%p (%u,%u)\n", handle, size.X, size.Y );
2292 return E_NOTIMPL;
2295 static BOOL is_tty_handle( HANDLE handle )
2297 return ((UINT_PTR)handle & 3) == 1;
2300 void init_console( void )
2302 RTL_USER_PROCESS_PARAMETERS *params = RtlGetCurrentPeb()->ProcessParameters;
2304 if (params->ConsoleHandle == CONSOLE_HANDLE_SHELL)
2306 HANDLE tty_in = NULL, tty_out = NULL, process = NULL;
2307 COORD size;
2309 if (is_tty_handle( params->hStdInput ))
2311 tty_in = params->hStdInput;
2312 params->hStdInput = NULL;
2314 if (is_tty_handle( params->hStdOutput ))
2316 tty_out = params->hStdOutput;
2317 params->hStdOutput = NULL;
2319 if (is_tty_handle( params->hStdError ))
2321 if (tty_out) CloseHandle( params->hStdError );
2322 else tty_out = params->hStdError;
2323 params->hStdError = NULL;
2326 size.X = params->dwXCountChars;
2327 size.Y = params->dwYCountChars;
2328 TRACE( "creating unix console (size %u %u)\n", size.X, size.Y );
2329 params->ConsoleHandle = create_pseudo_console( size, tty_in, tty_out, NULL, 0, &process );
2330 CloseHandle( process );
2331 CloseHandle( tty_in );
2332 CloseHandle( tty_out );
2334 if (params->ConsoleHandle && create_console_connection( params->ConsoleHandle ))
2336 init_console_std_handles( FALSE );
2339 else if (params->ConsoleHandle == CONSOLE_HANDLE_ALLOC ||
2340 params->ConsoleHandle == CONSOLE_HANDLE_ALLOC_NO_WINDOW)
2342 BOOL no_window = params->ConsoleHandle == CONSOLE_HANDLE_ALLOC_NO_WINDOW;
2343 HMODULE mod = GetModuleHandleW( NULL );
2344 params->ConsoleHandle = NULL;
2345 if (RtlImageNtHeader( mod )->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
2346 alloc_console( no_window );
2348 else if (params->ConsoleHandle && params->ConsoleHandle != CONSOLE_HANDLE_SHELL_NO_WINDOW)
2349 create_console_connection( params->ConsoleHandle );