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
32 #define NONAMELESSUNION
34 #define WIN32_NO_STATUS
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];
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 );
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
)
89 status
= NtDeviceIoControlFile( handle
, NULL
, NULL
, NULL
, &io
, code
, in_buff
, in_count
,
90 out_buff
, out_count
);
94 if (read
) *read
= io
.Information
;
96 case STATUS_INVALID_PARAMETER
:
99 status
= STATUS_INVALID_HANDLE
;
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();
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();
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
)
143 WideCharToMultiByte( cp
, 0, &buffer
->Char
.UnicodeChar
, 1, &ch
, 1, NULL
, NULL
);
144 buffer
->Char
.AsciiChar
= ch
;
149 /* map char infos to Unicode */
150 static void char_info_AtoW( CHAR_INFO
*buffer
, int count
)
152 UINT cp
= GetConsoleOutputCP();
157 MultiByteToWideChar( cp
, 0, &buffer
->Char
.AsciiChar
, 1, &ch
, 1 );
158 buffer
->Char
.UnicodeChar
= ch
;
163 /* helper function for GetLargestConsoleWindowSize */
164 static COORD
get_largest_console_window_size( HANDLE handle
)
166 struct condrv_output_info info
;
169 if (!console_ioctl( handle
, IOCTL_CONDRV_GET_OUTPUT_INFO
, NULL
, 0, &info
, sizeof(info
), NULL
))
172 c
.X
= info
.max_width
;
173 c
.Y
= info
.max_height
;
174 TRACE( "(%p), returning %dx%d\n", handle
, c
.X
, c
.Y
);
178 static HANDLE
create_console_server( void )
180 OBJECT_ATTRIBUTES attr
= {sizeof(attr
)};
181 UNICODE_STRING string
;
182 IO_STATUS_BLOCK iosb
;
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
;
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
;
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
;
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
);
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
;
265 console_flags
|= CONSOLE_OUTPUT_HANDLE
;
266 SetStdHandle( STD_OUTPUT_HANDLE
, handle
);
271 if (!std_out
&& !DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
272 &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
274 console_flags
|= CONSOLE_ERROR_HANDLE
;
275 SetStdHandle( STD_ERROR_HANDLE
, handle
);
281 /******************************************************************
282 * AttachConsole (kernelbase.@)
284 BOOL WINAPI DECLSPEC_HOTPATCH
AttachConsole( DWORD pid
)
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
);
300 ret
= create_console_connection( NULL
) &&
301 console_ioctl( console_connection
, IOCTL_CONDRV_BIND_PID
, &pid
, sizeof(pid
), NULL
, 0, NULL
);
304 RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
= create_console_reference( console_connection
);
305 if (RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
)
308 GetStartupInfoW( &si
);
309 init_console_std_handles( !(si
.dwFlags
& STARTF_USESTDHANDLES
) );
314 if (!ret
) FreeConsole();
315 RtlLeaveCriticalSection( &console_section
);
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
;
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
);
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
;
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
);
394 ERR("Can't allocate console\n");
398 RtlLeaveCriticalSection( &console_section
);
403 /******************************************************************************
404 * CreateConsoleScreenBuffer (kernelbase.@)
406 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateConsoleScreenBuffer( DWORD access
, DWORD share
,
407 SECURITY_ATTRIBUTES
*sa
, DWORD flags
,
410 OBJECT_ATTRIBUTES attr
= {sizeof(attr
)};
411 IO_STATUS_BLOCK iosb
;
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
451 RaiseException( DBG_CONTROL_C
, 0, 0, NULL
);
455 caught_by_dbg
= FALSE
;
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
);
473 /******************************************************************************
474 * FillConsoleOutputAttribute (kernelbase.@)
476 BOOL WINAPI DECLSPEC_HOTPATCH
FillConsoleOutputAttribute( HANDLE handle
, WORD attr
, DWORD length
,
477 COORD coord
, DWORD
*written
)
479 struct condrv_fill_output_params params
;
481 TRACE( "(%p,%d,%d,(%dx%d),%p)\n", handle
, attr
, length
, coord
.X
, coord
.Y
, written
);
485 SetLastError( ERROR_INVALID_ACCESS
);
491 params
.mode
= CHAR_INFO_MODE_ATTR
;
494 params
.count
= length
;
498 return console_ioctl( handle
, IOCTL_CONDRV_FILL_OUTPUT
, ¶ms
, sizeof(params
),
499 written
, sizeof(*written
), NULL
);
503 /******************************************************************************
504 * FillConsoleOutputCharacterA (kernelbase.@)
506 BOOL WINAPI DECLSPEC_HOTPATCH
FillConsoleOutputCharacterA( HANDLE handle
, CHAR ch
, DWORD length
,
507 COORD coord
, DWORD
*written
)
511 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch
, 1, &wch
, 1 );
512 return FillConsoleOutputCharacterW( handle
, wch
, length
, coord
, written
);
516 /******************************************************************************
517 * FillConsoleOutputCharacterW (kernelbase.@)
519 BOOL WINAPI DECLSPEC_HOTPATCH
FillConsoleOutputCharacterW( HANDLE handle
, WCHAR ch
, DWORD length
,
520 COORD coord
, DWORD
*written
)
522 struct condrv_fill_output_params params
;
524 TRACE( "(%p,%s,%d,(%dx%d),%p)\n", handle
, debugstr_wn(&ch
, 1), length
, coord
.X
, coord
.Y
, written
);
528 SetLastError( ERROR_INVALID_ACCESS
);
534 params
.mode
= CHAR_INFO_MODE_TEXT
;
537 params
.count
= length
;
541 return console_ioctl( handle
, IOCTL_CONDRV_FILL_OUTPUT
, ¶ms
, sizeof(params
),
542 written
, sizeof(*written
), NULL
);
546 /***********************************************************************
547 * FreeConsole (kernelbase.@)
549 BOOL WINAPI DECLSPEC_HOTPATCH
FreeConsole(void)
551 RtlEnterCriticalSection( &console_section
);
553 NtClose( console_connection
);
554 console_connection
= NULL
;
556 NtClose( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
);
557 RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
= NULL
;
559 if (console_flags
& CONSOLE_INPUT_HANDLE
) NtClose( GetStdHandle( STD_INPUT_HANDLE
));
560 if (console_flags
& CONSOLE_OUTPUT_HANDLE
) NtClose( GetStdHandle( STD_OUTPUT_HANDLE
));
561 if (console_flags
& CONSOLE_ERROR_HANDLE
) NtClose( GetStdHandle( STD_ERROR_HANDLE
));
564 RtlLeaveCriticalSection( &console_section
);
569 /******************************************************************************
570 * GenerateConsoleCtrlEvent (kernelbase.@)
572 BOOL WINAPI DECLSPEC_HOTPATCH
GenerateConsoleCtrlEvent( DWORD event
, DWORD group
)
574 struct condrv_ctrl_event ctrl_event
;
576 TRACE( "(%d, %x)\n", event
, group
);
578 if (event
!= CTRL_C_EVENT
&& event
!= CTRL_BREAK_EVENT
)
580 ERR( "Invalid event %d for PGID %x\n", event
, group
);
584 ctrl_event
.event
= event
;
585 ctrl_event
.group_id
= group
;
586 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
,
587 IOCTL_CONDRV_CTRL_EVENT
, &ctrl_event
, sizeof(ctrl_event
), NULL
, 0, NULL
);
591 /******************************************************************************
592 * GetConsoleCP (kernelbase.@)
594 UINT WINAPI DECLSPEC_HOTPATCH
GetConsoleCP(void)
596 struct condrv_input_info info
;
598 if (!console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
,
599 IOCTL_CONDRV_GET_INPUT_INFO
, NULL
, 0, &info
, sizeof(info
), NULL
))
601 return info
.input_cp
;
605 /******************************************************************************
606 * GetConsoleCursorInfo (kernelbase.@)
608 BOOL WINAPI DECLSPEC_HOTPATCH
GetConsoleCursorInfo( HANDLE handle
, CONSOLE_CURSOR_INFO
*info
)
610 struct condrv_output_info condrv_info
;
612 if (!console_ioctl( handle
, IOCTL_CONDRV_GET_OUTPUT_INFO
, NULL
, 0, &condrv_info
, sizeof(condrv_info
), NULL
))
617 SetLastError( ERROR_INVALID_ACCESS
);
621 info
->dwSize
= condrv_info
.cursor_size
;
622 info
->bVisible
= condrv_info
.cursor_visible
;
623 TRACE("(%p) returning (%d,%d)\n", handle
, info
->dwSize
, info
->bVisible
);
628 /***********************************************************************
629 * GetConsoleInputExeNameA (kernelbase.@)
631 BOOL WINAPI DECLSPEC_HOTPATCH
GetConsoleInputExeNameA( DWORD len
, LPSTR buffer
)
633 RtlEnterCriticalSection( &console_section
);
634 if (WideCharToMultiByte( CP_ACP
, 0, input_exe
, -1, NULL
, 0, NULL
, NULL
) <= len
)
635 WideCharToMultiByte( CP_ACP
, 0, input_exe
, -1, buffer
, len
, NULL
, NULL
);
636 else SetLastError(ERROR_BUFFER_OVERFLOW
);
637 RtlLeaveCriticalSection( &console_section
);
642 /***********************************************************************
643 * GetConsoleInputExeNameW (kernelbase.@)
645 BOOL WINAPI DECLSPEC_HOTPATCH
GetConsoleInputExeNameW( DWORD len
, LPWSTR buffer
)
647 RtlEnterCriticalSection( &console_section
);
648 if (len
> lstrlenW(input_exe
)) lstrcpyW( buffer
, input_exe
);
649 else SetLastError( ERROR_BUFFER_OVERFLOW
);
650 RtlLeaveCriticalSection( &console_section
);
655 /***********************************************************************
656 * GetConsoleMode (kernelbase.@)
658 BOOL WINAPI DECLSPEC_HOTPATCH
GetConsoleMode( HANDLE handle
, DWORD
*mode
)
660 return console_ioctl( handle
, IOCTL_CONDRV_GET_MODE
, NULL
, 0, mode
, sizeof(*mode
), NULL
);
664 /***********************************************************************
665 * GetConsoleOutputCP (kernelbase.@)
667 UINT WINAPI DECLSPEC_HOTPATCH
GetConsoleOutputCP(void)
669 struct condrv_input_info info
;
671 if (!console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
,
672 IOCTL_CONDRV_GET_INPUT_INFO
, NULL
, 0, &info
, sizeof(info
), NULL
))
674 return info
.output_cp
;
678 /***********************************************************************
679 * GetConsoleScreenBufferInfo (kernelbase.@)
681 BOOL WINAPI DECLSPEC_HOTPATCH
GetConsoleScreenBufferInfo( HANDLE handle
, CONSOLE_SCREEN_BUFFER_INFO
*info
)
683 struct condrv_output_info condrv_info
;
685 if (!console_ioctl( handle
, IOCTL_CONDRV_GET_OUTPUT_INFO
, NULL
, 0,
686 &condrv_info
, sizeof(condrv_info
), NULL
))
689 info
->dwSize
.X
= condrv_info
.width
;
690 info
->dwSize
.Y
= condrv_info
.height
;
691 info
->dwCursorPosition
.X
= condrv_info
.cursor_x
;
692 info
->dwCursorPosition
.Y
= condrv_info
.cursor_y
;
693 info
->wAttributes
= condrv_info
.attr
;
694 info
->srWindow
.Left
= condrv_info
.win_left
;
695 info
->srWindow
.Right
= condrv_info
.win_right
;
696 info
->srWindow
.Top
= condrv_info
.win_top
;
697 info
->srWindow
.Bottom
= condrv_info
.win_bottom
;
698 info
->dwMaximumWindowSize
.X
= min(condrv_info
.width
, condrv_info
.max_width
);
699 info
->dwMaximumWindowSize
.Y
= min(condrv_info
.height
, condrv_info
.max_height
);
701 TRACE( "(%p,(%d,%d) (%d,%d) %d (%d,%d-%d,%d) (%d,%d)\n", handle
,
702 info
->dwSize
.X
, info
->dwSize
.Y
, info
->dwCursorPosition
.X
, info
->dwCursorPosition
.Y
,
703 info
->wAttributes
, info
->srWindow
.Left
, info
->srWindow
.Top
, info
->srWindow
.Right
,
704 info
->srWindow
.Bottom
, info
->dwMaximumWindowSize
.X
, info
->dwMaximumWindowSize
.Y
);
709 /***********************************************************************
710 * GetConsoleScreenBufferInfoEx (kernelbase.@)
712 BOOL WINAPI DECLSPEC_HOTPATCH
GetConsoleScreenBufferInfoEx( HANDLE handle
,
713 CONSOLE_SCREEN_BUFFER_INFOEX
*info
)
715 struct condrv_output_info condrv_info
;
717 if (info
->cbSize
!= sizeof(CONSOLE_SCREEN_BUFFER_INFOEX
))
719 SetLastError( ERROR_INVALID_PARAMETER
);
723 if (!console_ioctl( handle
, IOCTL_CONDRV_GET_OUTPUT_INFO
, NULL
, 0, &condrv_info
,
724 sizeof(condrv_info
), NULL
))
727 info
->dwSize
.X
= condrv_info
.width
;
728 info
->dwSize
.Y
= condrv_info
.height
;
729 info
->dwCursorPosition
.X
= condrv_info
.cursor_x
;
730 info
->dwCursorPosition
.Y
= condrv_info
.cursor_y
;
731 info
->wAttributes
= condrv_info
.attr
;
732 info
->srWindow
.Left
= condrv_info
.win_left
;
733 info
->srWindow
.Top
= condrv_info
.win_top
;
734 info
->srWindow
.Right
= condrv_info
.win_right
;
735 info
->srWindow
.Bottom
= condrv_info
.win_bottom
;
736 info
->dwMaximumWindowSize
.X
= min( condrv_info
.width
, condrv_info
.max_width
);
737 info
->dwMaximumWindowSize
.Y
= min( condrv_info
.height
, condrv_info
.max_height
);
738 info
->wPopupAttributes
= condrv_info
.popup_attr
;
739 info
->bFullscreenSupported
= FALSE
;
740 memcpy( info
->ColorTable
, condrv_info
.color_map
, sizeof(info
->ColorTable
) );
745 /******************************************************************************
746 * GetConsoleTitleW (kernelbase.@)
748 DWORD WINAPI DECLSPEC_HOTPATCH
GetConsoleTitleW( LPWSTR title
, DWORD size
)
752 if (!console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
, IOCTL_CONDRV_GET_TITLE
,
753 NULL
, 0, title
, (size
- 1) * sizeof(WCHAR
), &size
))
756 size
/= sizeof(WCHAR
);
762 /***********************************************************************
763 * GetLargestConsoleWindowSize (kernelbase.@)
765 #if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
766 #undef GetLargestConsoleWindowSize
767 DWORD WINAPI DECLSPEC_HOTPATCH
GetLargestConsoleWindowSize( HANDLE handle
)
773 x
.c
= get_largest_console_window_size( handle
);
779 COORD WINAPI DECLSPEC_HOTPATCH
GetLargestConsoleWindowSize( HANDLE handle
)
781 return get_largest_console_window_size( handle
);
784 #endif /* !defined(__i386__) */
787 /***********************************************************************
788 * GetNumberOfConsoleInputEvents (kernelbase.@)
790 BOOL WINAPI DECLSPEC_HOTPATCH
GetNumberOfConsoleInputEvents( HANDLE handle
, DWORD
*count
)
792 struct condrv_input_info info
;
793 if (!console_ioctl( handle
, IOCTL_CONDRV_GET_INPUT_INFO
, NULL
, 0, &info
, sizeof(info
), NULL
))
795 *count
= info
.input_count
;
800 /***********************************************************************
801 * PeekConsoleInputA (kernelbase.@)
803 BOOL WINAPI DECLSPEC_HOTPATCH
PeekConsoleInputA( HANDLE handle
, INPUT_RECORD
*buffer
,
804 DWORD length
, DWORD
*count
)
808 if (!PeekConsoleInputW( handle
, buffer
, length
, &read
)) return FALSE
;
809 input_records_WtoA( buffer
, read
);
810 if (count
) *count
= read
;
815 /***********************************************************************
816 * PeekConsoleInputW (kernelbase.@)
818 BOOL WINAPI DECLSPEC_HOTPATCH
PeekConsoleInputW( HANDLE handle
, INPUT_RECORD
*buffer
,
819 DWORD length
, DWORD
*count
)
822 if (!console_ioctl( handle
, IOCTL_CONDRV_PEEK
, NULL
, 0, buffer
, length
* sizeof(*buffer
), &read
))
824 if (count
) *count
= read
/ sizeof(*buffer
);
829 /******************************************************************************
830 * ReadConsoleOutputAttribute (kernelbase.@)
832 BOOL WINAPI DECLSPEC_HOTPATCH
ReadConsoleOutputAttribute( HANDLE handle
, WORD
*attr
, DWORD length
,
833 COORD coord
, DWORD
*count
)
835 struct condrv_output_params params
;
838 TRACE( "(%p,%p,%d,%dx%d,%p)\n", handle
, attr
, length
, coord
.X
, coord
.Y
, count
);
842 SetLastError( ERROR_INVALID_ACCESS
);
846 params
.mode
= CHAR_INFO_MODE_ATTR
;
850 ret
= console_ioctl( handle
, IOCTL_CONDRV_READ_OUTPUT
, ¶ms
, sizeof(params
),
851 attr
, length
* sizeof(*attr
), count
);
852 *count
/= sizeof(*attr
);
857 /******************************************************************************
858 * ReadConsoleOutputCharacterA (kernelbase.@)
860 BOOL WINAPI DECLSPEC_HOTPATCH
ReadConsoleOutputCharacterA( HANDLE handle
, LPSTR buffer
, DWORD length
,
861 COORD coord
, DWORD
*count
)
869 SetLastError( ERROR_INVALID_ACCESS
);
874 if (!(wptr
= HeapAlloc( GetProcessHeap(), 0, length
* sizeof(WCHAR
) )))
876 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
879 if ((ret
= ReadConsoleOutputCharacterW( handle
, wptr
, length
, coord
, &read
)))
881 read
= WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr
, read
, buffer
, length
, NULL
, NULL
);
884 HeapFree( GetProcessHeap(), 0, wptr
);
889 /******************************************************************************
890 * ReadConsoleOutputCharacterW (kernelbase.@)
892 BOOL WINAPI DECLSPEC_HOTPATCH
ReadConsoleOutputCharacterW( HANDLE handle
, LPWSTR buffer
, DWORD length
,
893 COORD coord
, DWORD
*count
)
895 struct condrv_output_params params
;
898 TRACE( "(%p,%p,%d,%dx%d,%p)\n", handle
, buffer
, length
, coord
.X
, coord
.Y
, count
);
902 SetLastError( ERROR_INVALID_ACCESS
);
906 params
.mode
= CHAR_INFO_MODE_TEXT
;
910 ret
= console_ioctl( handle
, IOCTL_CONDRV_READ_OUTPUT
, ¶ms
, sizeof(params
), buffer
,
911 length
* sizeof(*buffer
), count
);
912 *count
/= sizeof(*buffer
);
917 /******************************************************************************
918 * ReadConsoleOutputA (kernelbase.@)
920 BOOL WINAPI DECLSPEC_HOTPATCH
ReadConsoleOutputA( HANDLE handle
, CHAR_INFO
*buffer
, COORD size
,
921 COORD coord
, SMALL_RECT
*region
)
926 ret
= ReadConsoleOutputW( handle
, buffer
, size
, coord
, region
);
927 if (ret
&& region
->Right
>= region
->Left
)
929 UINT cp
= GetConsoleOutputCP();
930 for (y
= 0; y
<= region
->Bottom
- region
->Top
; y
++)
931 char_info_WtoA( cp
, &buffer
[(coord
.Y
+ y
) * size
.X
+ coord
.X
], region
->Right
- region
->Left
+ 1 );
937 /******************************************************************************
938 * ReadConsoleOutputW (kernelbase.@)
940 BOOL WINAPI DECLSPEC_HOTPATCH
ReadConsoleOutputW( HANDLE handle
, CHAR_INFO
*buffer
, COORD size
,
941 COORD coord
, SMALL_RECT
*region
)
943 struct condrv_output_params params
;
944 unsigned int width
, height
, y
;
949 if (region
->Left
> region
->Right
|| region
->Top
> region
->Bottom
)
951 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
954 if (size
.X
<= coord
.X
|| size
.Y
<= coord
.Y
)
956 region
->Right
= region
->Left
- 1;
957 region
->Bottom
= region
->Top
- 1;
958 SetLastError( ERROR_INVALID_FUNCTION
);
961 width
= min( region
->Right
- region
->Left
+ 1, size
.X
- coord
.X
);
962 height
= min( region
->Bottom
- region
->Top
+ 1, size
.Y
- coord
.Y
);
963 region
->Right
= region
->Left
+ width
- 1;
964 region
->Bottom
= region
->Top
+ height
- 1;
966 count
= sizeof(*result
) + width
* height
* sizeof(*buffer
);
967 if (!(result
= HeapAlloc( GetProcessHeap(), 0, count
)))
969 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
973 params
.mode
= CHAR_INFO_MODE_TEXTATTR
;
974 params
.x
= region
->Left
;
975 params
.y
= region
->Top
;
976 params
.width
= width
;
977 if ((ret
= console_ioctl( handle
, IOCTL_CONDRV_READ_OUTPUT
, ¶ms
, sizeof(params
), result
, count
, &count
)) && count
)
979 CHAR_INFO
*char_info
= (CHAR_INFO
*)(result
+ 1);
981 width
= region
->Right
- region
->Left
+ 1;
982 height
= region
->Bottom
- region
->Top
+ 1;
983 for (y
= 0; y
< height
; y
++)
984 memcpy( &buffer
[(y
+ coord
.Y
) * size
.X
+ coord
.X
], &char_info
[y
* width
], width
* sizeof(*buffer
) );
986 HeapFree( GetProcessHeap(), 0, result
);
991 /******************************************************************************
992 * ScrollConsoleScreenBufferA (kernelbase.@)
994 BOOL WINAPI DECLSPEC_HOTPATCH
ScrollConsoleScreenBufferA( HANDLE handle
, const SMALL_RECT
*scroll
,
995 const SMALL_RECT
*clip
, COORD origin
, const CHAR_INFO
*fill
)
999 ciW
.Attributes
= fill
->Attributes
;
1000 MultiByteToWideChar( GetConsoleOutputCP(), 0, &fill
->Char
.AsciiChar
, 1, &ciW
.Char
.UnicodeChar
, 1 );
1002 return ScrollConsoleScreenBufferW( handle
, scroll
, clip
, origin
, &ciW
);
1006 /******************************************************************************
1007 * ScrollConsoleScreenBufferW (kernelbase.@)
1009 BOOL WINAPI DECLSPEC_HOTPATCH
ScrollConsoleScreenBufferW( HANDLE handle
, const SMALL_RECT
*scroll
,
1010 const SMALL_RECT
*clip_rect
, COORD origin
,
1011 const CHAR_INFO
*fill
)
1013 struct condrv_scroll_params params
;
1016 TRACE( "(%p,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", handle
,
1017 scroll
->Left
, scroll
->Top
, scroll
->Right
, scroll
->Bottom
,
1018 clip_rect
->Left
, clip_rect
->Top
, clip_rect
->Right
, clip_rect
->Bottom
,
1019 origin
.X
, origin
.Y
, fill
);
1021 TRACE("(%p,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", handle
,
1022 scroll
->Left
, scroll
->Top
, scroll
->Right
, scroll
->Bottom
,
1023 origin
.X
, origin
.Y
, fill
);
1025 params
.scroll
= *scroll
;
1026 params
.origin
= origin
;
1027 params
.fill
.ch
= fill
->Char
.UnicodeChar
;
1028 params
.fill
.attr
= fill
->Attributes
;
1031 params
.clip
.Left
= params
.clip
.Top
= 0;
1032 params
.clip
.Right
= params
.clip
.Bottom
= SHRT_MAX
;
1034 else params
.clip
= *clip_rect
;
1035 return console_ioctl( handle
, IOCTL_CONDRV_SCROLL
, (void *)¶ms
, sizeof(params
), NULL
, 0, NULL
);
1039 /******************************************************************************
1040 * SetConsoleActiveScreenBuffer (kernelbase.@)
1042 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleActiveScreenBuffer( HANDLE handle
)
1044 TRACE( "(%p)\n", handle
);
1045 return console_ioctl( handle
, IOCTL_CONDRV_ACTIVATE
, NULL
, 0, NULL
, 0, NULL
);
1049 /******************************************************************************
1050 * SetConsoleCP (kernelbase.@)
1052 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleCP( UINT cp
)
1054 struct condrv_input_info_params params
= { SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE
};
1056 params
.info
.input_cp
= cp
;
1057 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
,
1058 IOCTL_CONDRV_SET_INPUT_INFO
, ¶ms
, sizeof(params
), NULL
, 0, NULL
);
1062 /******************************************************************************
1063 * SetConsoleCtrlHandler (kernelbase.@)
1065 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleCtrlHandler( PHANDLER_ROUTINE func
, BOOL add
)
1067 struct ctrl_handler
*handler
;
1070 TRACE( "(%p,%d)\n", func
, add
);
1072 RtlEnterCriticalSection( &console_section
);
1076 if (add
) NtCurrentTeb()->Peb
->ProcessParameters
->ConsoleFlags
|= 1;
1077 else NtCurrentTeb()->Peb
->ProcessParameters
->ConsoleFlags
&= ~1;
1082 if ((handler
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler
) )))
1084 handler
->func
= func
;
1085 handler
->next
= ctrl_handlers
;
1086 ctrl_handlers
= handler
;
1092 struct ctrl_handler
**p_handler
;
1094 for (p_handler
= &ctrl_handlers
; *p_handler
; p_handler
= &(*p_handler
)->next
)
1096 if ((*p_handler
)->func
== func
) break;
1098 if (*p_handler
&& *p_handler
!= &default_handler
)
1100 handler
= *p_handler
;
1101 *p_handler
= handler
->next
;
1102 RtlFreeHeap( GetProcessHeap(), 0, handler
);
1105 else SetLastError( ERROR_INVALID_PARAMETER
);
1108 RtlLeaveCriticalSection( &console_section
);
1113 /******************************************************************************
1114 * SetConsoleCursorInfo (kernelbase.@)
1116 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleCursorInfo( HANDLE handle
, CONSOLE_CURSOR_INFO
*info
)
1118 struct condrv_output_info_params params
= { SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM
};
1120 TRACE( "(%p,%d,%d)\n", handle
, info
->dwSize
, info
->bVisible
);
1122 params
.info
.cursor_size
= info
->dwSize
;
1123 params
.info
.cursor_visible
= info
->bVisible
;
1124 return console_ioctl( handle
, IOCTL_CONDRV_SET_OUTPUT_INFO
, ¶ms
, sizeof(params
),
1129 /******************************************************************************
1130 * SetConsoleCursorPosition (kernelbase.@)
1132 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleCursorPosition( HANDLE handle
, COORD pos
)
1134 struct condrv_output_info_params params
= { SET_CONSOLE_OUTPUT_INFO_CURSOR_POS
};
1136 TRACE( "%p %d %d\n", handle
, pos
.X
, pos
.Y
);
1138 params
.info
.cursor_x
= pos
.X
;
1139 params
.info
.cursor_y
= pos
.Y
;
1140 return console_ioctl( handle
, IOCTL_CONDRV_SET_OUTPUT_INFO
, ¶ms
, sizeof(params
), NULL
, 0, NULL
);
1144 /******************************************************************************
1145 * SetConsoleInputExeNameA (kernelbase.@)
1147 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleInputExeNameA( LPCSTR name
)
1149 if (!name
|| !name
[0])
1151 SetLastError( ERROR_INVALID_PARAMETER
);
1154 RtlEnterCriticalSection( &console_section
);
1155 MultiByteToWideChar( CP_ACP
, 0, name
, -1, input_exe
, ARRAY_SIZE(input_exe
) );
1156 RtlLeaveCriticalSection( &console_section
);
1161 /******************************************************************************
1162 * SetConsoleInputExeNameW (kernelbase.@)
1164 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleInputExeNameW( LPCWSTR name
)
1166 if (!name
|| !name
[0])
1168 SetLastError( ERROR_INVALID_PARAMETER
);
1171 RtlEnterCriticalSection( &console_section
);
1172 lstrcpynW( input_exe
, name
, ARRAY_SIZE(input_exe
) );
1173 RtlLeaveCriticalSection( &console_section
);
1178 /******************************************************************************
1179 * SetConsoleMode (kernelbase.@)
1181 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleMode( HANDLE handle
, DWORD mode
)
1183 TRACE( "(%p,%x)\n", handle
, mode
);
1184 return console_ioctl( handle
, IOCTL_CONDRV_SET_MODE
, &mode
, sizeof(mode
), NULL
, 0, NULL
);
1188 /******************************************************************************
1189 * SetConsoleOutputCP (kernelbase.@)
1191 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleOutputCP( UINT cp
)
1193 struct condrv_input_info_params params
= { SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE
};
1195 params
.info
.output_cp
= cp
;
1196 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
,
1197 IOCTL_CONDRV_SET_INPUT_INFO
, ¶ms
, sizeof(params
), NULL
, 0, NULL
);
1201 /******************************************************************************
1202 * SetConsoleScreenBufferInfoEx (kernelbase.@)
1204 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleScreenBufferInfoEx( HANDLE handle
,
1205 CONSOLE_SCREEN_BUFFER_INFOEX
*info
)
1207 struct condrv_output_info_params params
=
1208 { SET_CONSOLE_OUTPUT_INFO_CURSOR_POS
| SET_CONSOLE_OUTPUT_INFO_SIZE
|
1209 SET_CONSOLE_OUTPUT_INFO_ATTR
| SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR
|
1210 SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW
| SET_CONSOLE_OUTPUT_INFO_MAX_SIZE
};
1212 TRACE("(%p, %p)\n", handle
, info
);
1214 if (info
->cbSize
!= sizeof(CONSOLE_SCREEN_BUFFER_INFOEX
))
1216 SetLastError(ERROR_INVALID_PARAMETER
);
1220 params
.info
.width
= info
->dwSize
.X
;
1221 params
.info
.height
= info
->dwSize
.Y
;
1222 params
.info
.cursor_x
= info
->dwCursorPosition
.X
;
1223 params
.info
.cursor_y
= info
->dwCursorPosition
.Y
;
1224 params
.info
.attr
= info
->wAttributes
;
1225 params
.info
.win_left
= info
->srWindow
.Left
;
1226 params
.info
.win_top
= info
->srWindow
.Top
;
1227 params
.info
.win_right
= info
->srWindow
.Right
;
1228 params
.info
.win_bottom
= info
->srWindow
.Bottom
;
1229 params
.info
.popup_attr
= info
->wPopupAttributes
;
1230 params
.info
.max_width
= min( info
->dwMaximumWindowSize
.X
, info
->dwSize
.X
);
1231 params
.info
.max_height
= min( info
->dwMaximumWindowSize
.Y
, info
->dwSize
.Y
);
1232 return console_ioctl( handle
, IOCTL_CONDRV_SET_OUTPUT_INFO
, ¶ms
, sizeof(params
), NULL
, 0, NULL
);
1236 /******************************************************************************
1237 * SetConsoleScreenBufferSize (kernelbase.@)
1239 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleScreenBufferSize( HANDLE handle
, COORD size
)
1241 struct condrv_output_info_params params
= { SET_CONSOLE_OUTPUT_INFO_SIZE
};
1243 TRACE( "(%p,(%d,%d))\n", handle
, size
.X
, size
.Y
);
1245 params
.info
.width
= size
.X
;
1246 params
.info
.height
= size
.Y
;
1247 return console_ioctl( handle
, IOCTL_CONDRV_SET_OUTPUT_INFO
, ¶ms
, sizeof(params
), NULL
, 0, NULL
);
1251 /******************************************************************************
1252 * SetConsoleTextAttribute (kernelbase.@)
1254 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleTextAttribute( HANDLE handle
, WORD attr
)
1256 struct condrv_output_info_params params
= { SET_CONSOLE_OUTPUT_INFO_ATTR
};
1258 TRACE( "(%p,%d)\n", handle
, attr
);
1260 params
.info
.attr
= attr
;
1261 return console_ioctl( handle
, IOCTL_CONDRV_SET_OUTPUT_INFO
, ¶ms
, sizeof(params
), NULL
, 0, NULL
);
1265 /******************************************************************************
1266 * SetConsoleTitleW (kernelbase.@)
1268 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleTitleW( LPCWSTR title
)
1270 TRACE( "%s\n", debugstr_w( title
));
1272 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
, IOCTL_CONDRV_SET_TITLE
,
1273 (void *)title
, lstrlenW(title
) * sizeof(WCHAR
), NULL
, 0, NULL
);
1277 /******************************************************************************
1278 * SetConsoleWindowInfo (kernelbase.@)
1280 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleWindowInfo( HANDLE handle
, BOOL absolute
, SMALL_RECT
*window
)
1282 struct condrv_output_info_params params
= { SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW
};
1283 SMALL_RECT rect
= *window
;
1285 TRACE( "(%p,%d,(%d,%d-%d,%d))\n", handle
, absolute
, rect
.Left
, rect
.Top
, rect
.Right
, rect
.Bottom
);
1289 CONSOLE_SCREEN_BUFFER_INFO info
;
1291 if (!GetConsoleScreenBufferInfo( handle
, &info
)) return FALSE
;
1292 rect
.Left
+= info
.srWindow
.Left
;
1293 rect
.Top
+= info
.srWindow
.Top
;
1294 rect
.Right
+= info
.srWindow
.Right
;
1295 rect
.Bottom
+= info
.srWindow
.Bottom
;
1298 params
.info
.win_left
= rect
.Left
;
1299 params
.info
.win_top
= rect
.Top
;
1300 params
.info
.win_right
= rect
.Right
;
1301 params
.info
.win_bottom
= rect
.Bottom
;
1302 return console_ioctl( handle
, IOCTL_CONDRV_SET_OUTPUT_INFO
, ¶ms
, sizeof(params
), NULL
, 0, NULL
);
1306 /***********************************************************************
1307 * ReadConsoleInputA (kernelbase.@)
1309 BOOL WINAPI
ReadConsoleInputA( HANDLE handle
, INPUT_RECORD
*buffer
, DWORD length
, DWORD
*count
)
1313 if (!ReadConsoleInputW( handle
, buffer
, length
, &read
)) return FALSE
;
1314 input_records_WtoA( buffer
, read
);
1315 if (count
) *count
= read
;
1320 /***********************************************************************
1321 * ReadConsoleInputW (kernelbase.@)
1323 BOOL WINAPI
ReadConsoleInputW( HANDLE handle
, INPUT_RECORD
*buffer
, DWORD length
, DWORD
*count
)
1325 if (!console_ioctl( handle
, IOCTL_CONDRV_READ_INPUT
, NULL
, 0,
1326 buffer
, length
* sizeof(*buffer
), count
))
1328 *count
/= sizeof(*buffer
);
1333 /******************************************************************************
1334 * WriteConsoleInputA (kernelbase.@)
1336 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleInputA( HANDLE handle
, const INPUT_RECORD
*buffer
,
1337 DWORD count
, DWORD
*written
)
1339 INPUT_RECORD
*recW
= NULL
;
1346 SetLastError( ERROR_INVALID_ACCESS
);
1349 if (!(recW
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*recW
) )))
1351 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1354 memcpy( recW
, buffer
, count
* sizeof(*recW
) );
1355 input_records_AtoW( recW
, count
);
1357 ret
= WriteConsoleInputW( handle
, recW
, count
, written
);
1358 HeapFree( GetProcessHeap(), 0, recW
);
1363 /******************************************************************************
1364 * WriteConsoleInputW (kernelbase.@)
1366 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleInputW( HANDLE handle
, const INPUT_RECORD
*buffer
,
1367 DWORD count
, DWORD
*written
)
1369 TRACE( "(%p,%p,%d,%p)\n", handle
, buffer
, count
, written
);
1371 if (count
> 0 && !buffer
)
1373 SetLastError( ERROR_INVALID_ACCESS
);
1377 if (!DeviceIoControl( handle
, IOCTL_CONDRV_WRITE_INPUT
, (void *)buffer
, count
* sizeof(*buffer
), NULL
, 0, NULL
, NULL
))
1382 SetLastError( ERROR_INVALID_ACCESS
);
1390 /***********************************************************************
1391 * WriteConsoleOutputA (kernelbase.@)
1393 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleOutputA( HANDLE handle
, const CHAR_INFO
*buffer
,
1394 COORD size
, COORD coord
, SMALL_RECT
*region
)
1398 COORD new_size
, new_coord
;
1401 new_size
.X
= min( region
->Right
- region
->Left
+ 1, size
.X
- coord
.X
);
1402 new_size
.Y
= min( region
->Bottom
- region
->Top
+ 1, size
.Y
- coord
.Y
);
1404 if (new_size
.X
<= 0 || new_size
.Y
<= 0)
1406 region
->Bottom
= region
->Top
+ new_size
.Y
- 1;
1407 region
->Right
= region
->Left
+ new_size
.X
- 1;
1411 /* only copy the useful rectangle */
1412 if (!(ciW
= HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO
) * new_size
.X
* new_size
.Y
)))
1414 for (y
= 0; y
< new_size
.Y
; y
++)
1415 memcpy( &ciW
[y
* new_size
.X
], &buffer
[(y
+ coord
.Y
) * size
.X
+ coord
.X
],
1416 new_size
.X
* sizeof(CHAR_INFO
) );
1417 char_info_AtoW( ciW
, new_size
.X
* new_size
.Y
);
1418 new_coord
.X
= new_coord
.Y
= 0;
1419 ret
= WriteConsoleOutputW( handle
, ciW
, new_size
, new_coord
, region
);
1420 HeapFree( GetProcessHeap(), 0, ciW
);
1425 /***********************************************************************
1426 * WriteConsoleOutputW (kernelbase.@)
1428 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleOutputW( HANDLE handle
, const CHAR_INFO
*buffer
,
1429 COORD size
, COORD coord
, SMALL_RECT
*region
)
1431 struct condrv_output_params
*params
;
1432 unsigned int width
, height
, y
;
1436 TRACE( "(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n",
1437 handle
, buffer
, size
.X
, size
.Y
, coord
.X
, coord
.Y
,
1438 region
->Left
, region
->Top
, region
->Right
, region
->Bottom
);
1440 if (region
->Left
> region
->Right
|| region
->Top
> region
->Bottom
|| size
.X
<= coord
.X
|| size
.Y
<= coord
.Y
)
1442 SetLastError( ERROR_INVALID_PARAMETER
);
1446 width
= min( region
->Right
- region
->Left
+ 1, size
.X
- coord
.X
);
1447 height
= min( region
->Bottom
- region
->Top
+ 1, size
.Y
- coord
.Y
);
1448 region
->Right
= region
->Left
+ width
- 1;
1449 region
->Bottom
= region
->Top
+ height
- 1;
1451 params_size
= sizeof(*params
) + width
* height
* sizeof(*buffer
);
1452 if (!(params
= HeapAlloc( GetProcessHeap(), 0, params_size
)))
1454 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1458 params
->mode
= CHAR_INFO_MODE_TEXTATTR
;
1459 params
->x
= region
->Left
;
1460 params
->y
= region
->Top
;
1461 params
->width
= width
;
1463 for (y
= 0; y
< height
; y
++)
1464 memcpy( &((CHAR_INFO
*)(params
+ 1))[y
* width
], &buffer
[(y
+ coord
.Y
) * size
.X
+ coord
.X
], width
* sizeof(CHAR_INFO
) );
1466 ret
= console_ioctl( handle
, IOCTL_CONDRV_WRITE_OUTPUT
, params
, params_size
, region
, sizeof(*region
), NULL
);
1467 HeapFree( GetProcessHeap(), 0, params
);
1472 /******************************************************************************
1473 * WriteConsoleOutputAttribute (kernelbase.@)
1475 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleOutputAttribute( HANDLE handle
, const WORD
*attr
, DWORD length
,
1476 COORD coord
, DWORD
*written
)
1478 struct condrv_output_params
*params
;
1482 TRACE( "(%p,%p,%d,%dx%d,%p)\n", handle
, attr
, length
, coord
.X
, coord
.Y
, written
);
1484 if ((length
> 0 && !attr
) || !written
)
1486 SetLastError( ERROR_INVALID_ACCESS
);
1491 size
= sizeof(*params
) + length
* sizeof(WORD
);
1492 if (!(params
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
1493 params
->mode
= CHAR_INFO_MODE_ATTR
;
1494 params
->x
= coord
.X
;
1495 params
->y
= coord
.Y
;
1497 memcpy( params
+ 1, attr
, length
* sizeof(*attr
) );
1498 ret
= console_ioctl( handle
, IOCTL_CONDRV_WRITE_OUTPUT
, params
, size
, written
, sizeof(*written
), NULL
);
1499 HeapFree( GetProcessHeap(), 0, params
);
1504 /******************************************************************************
1505 * WriteConsoleOutputCharacterA (kernelbase.@)
1507 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleOutputCharacterA( HANDLE handle
, LPCSTR str
, DWORD length
,
1508 COORD coord
, DWORD
*written
)
1514 TRACE( "(%p,%s,%d,%dx%d,%p)\n", handle
, debugstr_an(str
, length
), length
, coord
.X
, coord
.Y
, written
);
1518 UINT cp
= GetConsoleOutputCP();
1521 SetLastError( ERROR_INVALID_ACCESS
);
1524 lenW
= MultiByteToWideChar( cp
, 0, str
, length
, NULL
, 0 );
1526 if (!(strW
= HeapAlloc( GetProcessHeap(), 0, lenW
* sizeof(WCHAR
) )))
1528 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1531 MultiByteToWideChar( cp
, 0, str
, length
, strW
, lenW
);
1533 ret
= WriteConsoleOutputCharacterW( handle
, strW
, lenW
, coord
, written
);
1534 HeapFree( GetProcessHeap(), 0, strW
);
1539 /******************************************************************************
1540 * WriteConsoleOutputCharacterW (kernelbase.@)
1542 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleOutputCharacterW( HANDLE handle
, LPCWSTR str
, DWORD length
,
1543 COORD coord
, DWORD
*written
)
1545 struct condrv_output_params
*params
;
1549 TRACE( "(%p,%s,%d,%dx%d,%p)\n", handle
, debugstr_wn(str
, length
), length
, coord
.X
, coord
.Y
, written
);
1551 if ((length
> 0 && !str
) || !written
)
1553 SetLastError( ERROR_INVALID_ACCESS
);
1558 size
= sizeof(*params
) + length
* sizeof(WCHAR
);
1559 if (!(params
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
1560 params
->mode
= CHAR_INFO_MODE_TEXT
;
1561 params
->x
= coord
.X
;
1562 params
->y
= coord
.Y
;
1564 memcpy( params
+ 1, str
, length
* sizeof(*str
) );
1565 ret
= console_ioctl( handle
, IOCTL_CONDRV_WRITE_OUTPUT
, params
, size
, written
, sizeof(*written
), NULL
);
1566 HeapFree( GetProcessHeap(), 0, params
);
1571 /***********************************************************************
1572 * ReadConsoleA (kernelbase.@)
1574 BOOL WINAPI
ReadConsoleA( HANDLE handle
, void *buffer
, DWORD length
, DWORD
*count
, void *reserved
)
1576 if (length
> INT_MAX
)
1578 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1582 return console_ioctl( handle
, IOCTL_CONDRV_READ_FILE
, NULL
, 0, buffer
, length
, count
);
1586 /***********************************************************************
1587 * ReadConsoleW (kernelbase.@)
1589 BOOL WINAPI
ReadConsoleW( HANDLE handle
, void *buffer
, DWORD length
, DWORD
*count
, void *reserved
)
1593 TRACE( "(%p,%p,%d,%p,%p)\n", handle
, buffer
, length
, count
, reserved
);
1595 if (length
> INT_MAX
)
1597 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1601 ret
= console_ioctl( handle
, IOCTL_CONDRV_READ_CONSOLE
, NULL
, 0, buffer
,
1602 length
* sizeof(WCHAR
), count
);
1603 if (ret
) *count
/= sizeof(WCHAR
);
1608 /***********************************************************************
1609 * WriteConsoleA (kernelbase.@)
1611 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleA( HANDLE handle
, const void *buffer
, DWORD length
,
1612 DWORD
*written
, void *reserved
)
1616 TRACE( "(%p,%s,%d,%p,%p)\n", handle
, debugstr_an(buffer
, length
), length
, written
, reserved
);
1618 ret
= console_ioctl( handle
, IOCTL_CONDRV_WRITE_FILE
, (void *)buffer
, length
, NULL
, 0, NULL
);
1619 if (written
) *written
= ret
? length
: 0;
1624 /***********************************************************************
1625 * WriteConsoleW (kernelbase.@)
1627 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleW( HANDLE handle
, const void *buffer
, DWORD length
,
1628 DWORD
*written
, void *reserved
)
1632 TRACE( "(%p,%s,%d,%p,%p)\n", handle
, debugstr_wn(buffer
, length
), length
, written
, reserved
);
1634 ret
= console_ioctl( handle
, IOCTL_CONDRV_WRITE_CONSOLE
, (void *)buffer
,
1635 length
* sizeof(WCHAR
), NULL
, 0, NULL
);
1636 if (written
) *written
= ret
? length
: 0;
1641 /***********************************************************************
1642 * FlushConsoleInputBuffer (kernelbase.@)
1644 BOOL WINAPI
FlushConsoleInputBuffer( HANDLE handle
)
1646 return console_ioctl( handle
, IOCTL_CONDRV_FLUSH
, NULL
, 0, NULL
, 0, NULL
);
1650 /***********************************************************************
1651 * Beep (kernelbase.@)
1653 BOOL WINAPI
Beep( DWORD frequency
, DWORD duration
)
1655 /* FIXME: we should not require a console to be attached */
1656 console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
,
1657 IOCTL_CONDRV_BEEP
, NULL
, 0, NULL
, 0, NULL
);
1662 static HANDLE
create_pseudo_console( COORD size
, HANDLE input
, HANDLE output
, HANDLE signal
,
1663 DWORD flags
, HANDLE
*process
)
1665 WCHAR cmd
[MAX_PATH
], conhost_path
[MAX_PATH
];
1666 PROCESS_INFORMATION pi
;
1667 HANDLE server
, console
;
1672 if (!(server
= create_console_server())) return NULL
;
1674 console
= create_console_reference( server
);
1681 memset( &si
, 0, sizeof(si
) );
1682 si
.StartupInfo
.cb
= sizeof(STARTUPINFOEXW
);
1683 si
.StartupInfo
.hStdInput
= input
;
1684 si
.StartupInfo
.hStdOutput
= output
;
1685 si
.StartupInfo
.hStdError
= output
;
1686 si
.StartupInfo
.dwFlags
= STARTF_USESTDHANDLES
;
1687 swprintf( conhost_path
, ARRAY_SIZE(conhost_path
), L
"%s\\conhost.exe", system_dir
);
1690 swprintf( cmd
, ARRAY_SIZE(cmd
),
1691 L
"\"%s\" --headless %s--width %u --height %u --signal 0x%x --server 0x%x",
1692 conhost_path
, (flags
& PSEUDOCONSOLE_INHERIT_CURSOR
) ? L
"--inheritcursor " : L
"",
1693 size
.X
, size
.Y
, signal
, server
);
1697 swprintf( cmd
, ARRAY_SIZE(cmd
), L
"\"%s\" --unix --width %u --height %u --server 0x%x",
1698 conhost_path
, size
.X
, size
.Y
, server
);
1700 Wow64DisableWow64FsRedirection( &redir
);
1701 res
= CreateProcessW( conhost_path
, cmd
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
,
1702 &si
.StartupInfo
, &pi
);
1703 Wow64RevertWow64FsRedirection( redir
);
1711 NtClose( pi
.hThread
);
1712 *process
= pi
.hProcess
;
1716 /******************************************************************************
1717 * CreatePseudoConsole (kernelbase.@)
1719 HRESULT WINAPI
CreatePseudoConsole( COORD size
, HANDLE input
, HANDLE output
, DWORD flags
, HPCON
*ret
)
1721 SECURITY_ATTRIBUTES inherit_attr
= { sizeof(inherit_attr
), NULL
, TRUE
};
1722 struct pseudo_console
*pseudo_console
;
1723 HANDLE tty_input
= NULL
, tty_output
;
1724 HANDLE signal
= NULL
;
1725 WCHAR pipe_name
[64];
1727 TRACE( "(%u,%u) %p %p %x %p\n", size
.X
, size
.Y
, input
, output
, flags
, ret
);
1729 if (!size
.X
|| !size
.Y
|| !ret
) return E_INVALIDARG
;
1731 if (!(pseudo_console
= HeapAlloc( GetProcessHeap(), 0, HEAP_ZERO_MEMORY
))) return E_OUTOFMEMORY
;
1733 swprintf( pipe_name
, ARRAY_SIZE(pipe_name
), L
"\\\\.\\pipe\\wine_pty_signal_pipe%x",
1734 GetCurrentThreadId() );
1735 signal
= CreateNamedPipeW( pipe_name
, PIPE_ACCESS_INBOUND
| FILE_FLAG_OVERLAPPED
, PIPE_TYPE_BYTE
,
1736 PIPE_UNLIMITED_INSTANCES
, 4096, 4096, NMPWAIT_USE_DEFAULT_WAIT
, &inherit_attr
);
1737 if (signal
== INVALID_HANDLE_VALUE
)
1739 HeapFree( GetProcessHeap(), 0, pseudo_console
);
1740 return HRESULT_FROM_WIN32( GetLastError() );
1742 pseudo_console
->signal
= CreateFileW( pipe_name
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
1743 if (pseudo_console
->signal
!= INVALID_HANDLE_VALUE
&&
1744 DuplicateHandle( GetCurrentProcess(), input
, GetCurrentProcess(), &tty_input
, 0, TRUE
, DUPLICATE_SAME_ACCESS
) &&
1745 DuplicateHandle( GetCurrentProcess(), output
, GetCurrentProcess(), &tty_output
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1747 pseudo_console
->reference
= create_pseudo_console( size
, tty_input
, tty_output
, signal
, flags
,
1748 &pseudo_console
->process
);
1749 NtClose( tty_output
);
1751 NtClose( tty_input
);
1753 if (!pseudo_console
->reference
)
1755 ClosePseudoConsole( pseudo_console
);
1756 return HRESULT_FROM_WIN32( GetLastError() );
1759 *ret
= pseudo_console
;
1763 /******************************************************************************
1764 * ClosePseudoConsole (kernelbase.@)
1766 void WINAPI
ClosePseudoConsole( HPCON handle
)
1768 struct pseudo_console
*pseudo_console
= handle
;
1770 TRACE( "%p\n", handle
);
1772 if (!pseudo_console
) return;
1773 if (pseudo_console
->signal
) CloseHandle( pseudo_console
->signal
);
1774 if (pseudo_console
->process
)
1776 WaitForSingleObject( pseudo_console
->process
, INFINITE
);
1777 CloseHandle( pseudo_console
->process
);
1779 if (pseudo_console
->reference
) CloseHandle( pseudo_console
->reference
);
1782 /******************************************************************************
1783 * ResizePseudoConsole (kernelbase.@)
1785 HRESULT WINAPI
ResizePseudoConsole( HPCON handle
, COORD size
)
1787 FIXME( "%p (%u,%u)\n", handle
, size
.X
, size
.Y
);
1791 static BOOL
is_tty_handle( HANDLE handle
)
1793 return ((UINT_PTR
)handle
& 3) == 1;
1796 void init_console( void )
1798 RTL_USER_PROCESS_PARAMETERS
*params
= RtlGetCurrentPeb()->ProcessParameters
;
1800 if (params
->ConsoleHandle
== CONSOLE_HANDLE_SHELL
)
1802 HANDLE tty_in
= NULL
, tty_out
= NULL
, process
= NULL
;
1805 if (is_tty_handle( params
->hStdInput
))
1807 tty_in
= params
->hStdInput
;
1808 params
->hStdInput
= NULL
;
1810 if (is_tty_handle( params
->hStdOutput
))
1812 tty_out
= params
->hStdOutput
;
1813 params
->hStdOutput
= NULL
;
1815 if (is_tty_handle( params
->hStdError
))
1817 if (tty_out
) CloseHandle( params
->hStdError
);
1818 else tty_out
= params
->hStdError
;
1819 params
->hStdError
= NULL
;
1822 size
.X
= params
->dwXCountChars
;
1823 size
.Y
= params
->dwYCountChars
;
1824 TRACE( "creating unix console (size %u %u)\n", size
.X
, size
.Y
);
1825 params
->ConsoleHandle
= create_pseudo_console( size
, tty_in
, tty_out
, NULL
, 0, &process
);
1826 CloseHandle( process
);
1827 CloseHandle( tty_in
);
1828 CloseHandle( tty_out
);
1830 if (params
->ConsoleHandle
&& create_console_connection( params
->ConsoleHandle
))
1832 init_console_std_handles( FALSE
);
1835 else if (params
->ConsoleHandle
== CONSOLE_HANDLE_ALLOC
)
1837 HMODULE mod
= GetModuleHandleW( NULL
);
1838 params
->ConsoleHandle
= NULL
;
1839 if (RtlImageNtHeader( mod
)->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
)
1842 else if (params
->ConsoleHandle
) create_console_connection( params
->ConsoleHandle
);