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
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #define NONAMELESSUNION
33 #define WIN32_NO_STATUS
39 #include "wine/condrv.h"
40 #include "wine/server.h"
41 #include "wine/exception.h"
42 #include "wine/debug.h"
43 #include "kernelbase.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(console
);
48 static CRITICAL_SECTION console_section
;
49 static CRITICAL_SECTION_DEBUG critsect_debug
=
51 0, 0, &console_section
,
52 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
53 0, 0, { (DWORD_PTR
)(__FILE__
": console_section") }
55 static CRITICAL_SECTION console_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
57 static HANDLE console_connection
;
58 static HANDLE console_wait_event
;
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 /* FIXME: Use unbound console handle */
241 RtlInitUnicodeString( &name
, L
"\\Device\\ConDrv\\CurrentIn" );
242 status
= NtCreateFile( &handle
, FILE_READ_DATA
| FILE_WRITE_DATA
| SYNCHRONIZE
| FILE_READ_ATTRIBUTES
|
243 FILE_WRITE_ATTRIBUTES
, &attr
, &iosb
, NULL
, FILE_ATTRIBUTE_NORMAL
,
244 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
, FILE_CREATE
,
245 FILE_NON_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
, NULL
, 0 );
246 if (!set_ntstatus( status
)) return FALSE
;
247 console_flags
|= CONSOLE_INPUT_HANDLE
;
248 SetStdHandle( STD_INPUT_HANDLE
, console_handle_map( handle
));
253 std_out
= GetStdHandle( STD_OUTPUT_HANDLE
);
254 std_err
= GetStdHandle( STD_ERROR_HANDLE
);
255 if (std_out
&& std_err
) return TRUE
;
258 /* FIXME: Use unbound console handle */
259 RtlInitUnicodeString( &name
, L
"\\Device\\ConDrv\\CurrentOut" );
260 status
= NtCreateFile( &handle
, FILE_READ_DATA
| FILE_WRITE_DATA
| SYNCHRONIZE
| FILE_READ_ATTRIBUTES
|
261 FILE_WRITE_ATTRIBUTES
, &attr
, &iosb
, NULL
, FILE_ATTRIBUTE_NORMAL
,
262 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
, FILE_CREATE
,
263 FILE_NON_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
, NULL
, 0 );
264 if (!set_ntstatus( status
)) return FALSE
;
267 console_flags
|= CONSOLE_OUTPUT_HANDLE
;
268 SetStdHandle( STD_OUTPUT_HANDLE
, console_handle_map( handle
));
273 if (!std_out
&& !DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
274 &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
276 console_flags
|= CONSOLE_ERROR_HANDLE
;
277 SetStdHandle( STD_ERROR_HANDLE
, console_handle_map( handle
));
283 /******************************************************************
284 * AttachConsole (kernelbase.@)
286 BOOL WINAPI DECLSPEC_HOTPATCH
AttachConsole( DWORD pid
)
290 TRACE( "(%x)\n", pid
);
292 RtlEnterCriticalSection( &console_section
);
294 if (RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
)
296 RtlLeaveCriticalSection( &console_section
);
297 WARN( "console already attached\n" );
298 SetLastError( ERROR_ACCESS_DENIED
);
302 ret
= create_console_connection( NULL
) &&
303 console_ioctl( console_connection
, IOCTL_CONDRV_BIND_PID
, &pid
, sizeof(pid
), NULL
, 0, NULL
);
306 RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
= create_console_reference( console_connection
);
307 if (RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
)
310 GetStartupInfoW( &si
);
311 init_console_std_handles( !(si
.dwFlags
& STARTF_USESTDHANDLES
) );
316 if (!ret
) FreeConsole();
317 RtlLeaveCriticalSection( &console_section
);
322 /******************************************************************
323 * AllocConsole (kernelbase.@)
325 BOOL WINAPI
AllocConsole(void)
327 SECURITY_ATTRIBUTES inheritable_attr
= { sizeof(inheritable_attr
), NULL
, TRUE
};
328 STARTUPINFOW app_si
, console_si
;
329 WCHAR buffer
[1024], cmd
[256];
330 PROCESS_INFORMATION pi
;
331 HANDLE event
, console
;
336 RtlEnterCriticalSection( &console_section
);
338 if (RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
)
340 /* we already have a console opened on this process, don't create a new one */
341 RtlLeaveCriticalSection( &console_section
);
342 SetLastError( ERROR_ACCESS_DENIED
);
346 GetStartupInfoW(&app_si
);
348 memset(&console_si
, 0, sizeof(console_si
));
349 console_si
.cb
= sizeof(console_si
);
350 /* setup a view arguments for wineconsole (it'll use them as default values) */
351 if (app_si
.dwFlags
& STARTF_USECOUNTCHARS
)
353 console_si
.dwFlags
|= STARTF_USECOUNTCHARS
;
354 console_si
.dwXCountChars
= app_si
.dwXCountChars
;
355 console_si
.dwYCountChars
= app_si
.dwYCountChars
;
357 if (app_si
.dwFlags
& STARTF_USEFILLATTRIBUTE
)
359 console_si
.dwFlags
|= STARTF_USEFILLATTRIBUTE
;
360 console_si
.dwFillAttribute
= app_si
.dwFillAttribute
;
362 if (app_si
.dwFlags
& STARTF_USESHOWWINDOW
)
364 console_si
.dwFlags
|= STARTF_USESHOWWINDOW
;
365 console_si
.wShowWindow
= app_si
.wShowWindow
;
368 console_si
.lpTitle
= app_si
.lpTitle
;
369 else if (GetModuleFileNameW(0, buffer
, ARRAY_SIZE(buffer
)))
371 buffer
[ARRAY_SIZE(buffer
) - 1] = 0;
372 console_si
.lpTitle
= buffer
;
375 if (!(event
= CreateEventW( &inheritable_attr
, TRUE
, FALSE
, NULL
))) goto error
;
377 swprintf( cmd
, ARRAY_SIZE(cmd
), L
"wineconsole --use-event=%ld", (DWORD_PTR
)event
);
378 if ((ret
= CreateProcessW( NULL
, cmd
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &console_si
, &pi
)))
380 HANDLE wait_handles
[2] = { event
, pi
.hProcess
};
381 ret
= WaitForMultipleObjects( ARRAY_SIZE(wait_handles
), wait_handles
, FALSE
, INFINITE
) == WAIT_OBJECT_0
;
382 CloseHandle( pi
.hThread
);
383 CloseHandle( pi
.hProcess
);
385 CloseHandle( event
);
386 if (!ret
|| !init_console_std_handles( !(app_si
.dwFlags
& STARTF_USESTDHANDLES
) )) goto error
;
387 console
= CreateFileW( L
"CONIN$", GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
388 if (console
== INVALID_HANDLE_VALUE
) goto error
;
389 RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
= console
;
391 TRACE( "Started wineconsole pid=%08x tid=%08x\n", pi
.dwProcessId
, pi
.dwThreadId
);
393 RtlLeaveCriticalSection( &console_section
);
394 SetLastError( ERROR_SUCCESS
);
398 ERR("Can't allocate console\n");
400 RtlLeaveCriticalSection( &console_section
);
405 /******************************************************************************
406 * CreateConsoleScreenBuffer (kernelbase.@)
408 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateConsoleScreenBuffer( DWORD access
, DWORD share
,
409 SECURITY_ATTRIBUTES
*sa
, DWORD flags
,
412 OBJECT_ATTRIBUTES attr
= {sizeof(attr
)};
413 IO_STATUS_BLOCK iosb
;
418 TRACE( "(%x,%x,%p,%x,%p)\n", access
, share
, sa
, flags
, data
);
420 if (flags
!= CONSOLE_TEXTMODE_BUFFER
|| data
)
422 SetLastError( ERROR_INVALID_PARAMETER
);
423 return INVALID_HANDLE_VALUE
;
426 RtlInitUnicodeString( &name
, L
"\\Device\\ConDrv\\ScreenBuffer" );
427 attr
.ObjectName
= &name
;
428 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
429 if (sa
&& sa
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
430 status
= NtCreateFile( &handle
, access
, &attr
, &iosb
, NULL
, FILE_ATTRIBUTE_NORMAL
, 0, FILE_OPEN
,
431 FILE_NON_DIRECTORY_FILE
, NULL
, 0 );
432 return set_ntstatus( status
) ? handle
: INVALID_HANDLE_VALUE
;
436 /******************************************************************************
437 * CtrlRoutine (kernelbase.@)
439 DWORD WINAPI
CtrlRoutine( void *arg
)
441 DWORD_PTR event
= (DWORD_PTR
)arg
;
442 struct ctrl_handler
*handler
;
444 if (event
== CTRL_C_EVENT
)
446 BOOL caught_by_dbg
= TRUE
;
447 /* First, try to pass the ctrl-C event to the debugger (if any)
448 * If it continues, there's nothing more to do
449 * Otherwise, we need to send the ctrl-C event to the handlers
453 RaiseException( DBG_CONTROL_C
, 0, 0, NULL
);
457 caught_by_dbg
= FALSE
;
460 if (caught_by_dbg
) return 0;
463 if (NtCurrentTeb()->Peb
->ProcessParameters
->ConsoleFlags
& 1) return 0;
465 RtlEnterCriticalSection( &console_section
);
466 for (handler
= ctrl_handlers
; handler
; handler
= handler
->next
)
468 if (handler
->func( event
)) break;
470 RtlLeaveCriticalSection( &console_section
);
475 static LONG WINAPI
handle_ctrl_c( EXCEPTION_POINTERS
*eptr
)
477 if (eptr
->ExceptionRecord
->ExceptionCode
!= CONTROL_C_EXIT
) return EXCEPTION_CONTINUE_SEARCH
;
478 if (!RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
) return EXCEPTION_CONTINUE_SEARCH
;
480 if (!(NtCurrentTeb()->Peb
->ProcessParameters
->ConsoleFlags
& 1))
482 HANDLE thread
= CreateThread( NULL
, 0, CtrlRoutine
, (void*)CTRL_C_EVENT
, 0, NULL
);
483 if (thread
) CloseHandle( thread
);
485 return EXCEPTION_CONTINUE_EXECUTION
;
489 /******************************************************************************
490 * FillConsoleOutputAttribute (kernelbase.@)
492 BOOL WINAPI DECLSPEC_HOTPATCH
FillConsoleOutputAttribute( HANDLE handle
, WORD attr
, DWORD length
,
493 COORD coord
, DWORD
*written
)
495 struct condrv_fill_output_params params
;
497 TRACE( "(%p,%d,%d,(%dx%d),%p)\n", handle
, attr
, length
, coord
.X
, coord
.Y
, written
);
501 SetLastError( ERROR_INVALID_ACCESS
);
507 params
.mode
= CHAR_INFO_MODE_ATTR
;
510 params
.count
= length
;
514 return console_ioctl( handle
, IOCTL_CONDRV_FILL_OUTPUT
, ¶ms
, sizeof(params
),
515 written
, sizeof(*written
), NULL
);
519 /******************************************************************************
520 * FillConsoleOutputCharacterA (kernelbase.@)
522 BOOL WINAPI DECLSPEC_HOTPATCH
FillConsoleOutputCharacterA( HANDLE handle
, CHAR ch
, DWORD length
,
523 COORD coord
, DWORD
*written
)
527 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch
, 1, &wch
, 1 );
528 return FillConsoleOutputCharacterW( handle
, wch
, length
, coord
, written
);
532 /******************************************************************************
533 * FillConsoleOutputCharacterW (kernelbase.@)
535 BOOL WINAPI DECLSPEC_HOTPATCH
FillConsoleOutputCharacterW( HANDLE handle
, WCHAR ch
, DWORD length
,
536 COORD coord
, DWORD
*written
)
538 struct condrv_fill_output_params params
;
540 TRACE( "(%p,%s,%d,(%dx%d),%p)\n", handle
, debugstr_wn(&ch
, 1), length
, coord
.X
, coord
.Y
, written
);
544 SetLastError( ERROR_INVALID_ACCESS
);
550 params
.mode
= CHAR_INFO_MODE_TEXT
;
553 params
.count
= length
;
557 return console_ioctl( handle
, IOCTL_CONDRV_FILL_OUTPUT
, ¶ms
, sizeof(params
),
558 written
, sizeof(*written
), NULL
);
561 HANDLE
get_console_wait_handle( HANDLE handle
)
565 SERVER_START_REQ( get_console_wait_event
)
567 req
->handle
= wine_server_obj_handle( console_handle_map( handle
));
568 if (!wine_server_call( req
)) event
= wine_server_ptr_handle( reply
->event
);
573 if (InterlockedCompareExchangePointer( &console_wait_event
, event
, 0 )) NtClose( event
);
574 handle
= console_wait_event
;
580 /***********************************************************************
581 * FreeConsole (kernelbase.@)
583 BOOL WINAPI DECLSPEC_HOTPATCH
FreeConsole(void)
588 RtlEnterCriticalSection( &console_section
);
590 NtClose( console_connection
);
591 console_connection
= NULL
;
593 NtClose( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
);
594 RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
= NULL
;
596 if (console_flags
& CONSOLE_INPUT_HANDLE
) NtClose( GetStdHandle( STD_INPUT_HANDLE
));
597 if (console_flags
& CONSOLE_OUTPUT_HANDLE
) NtClose( GetStdHandle( STD_OUTPUT_HANDLE
));
598 if (console_flags
& CONSOLE_ERROR_HANDLE
) NtClose( GetStdHandle( STD_ERROR_HANDLE
));
601 SERVER_START_REQ( free_console
)
603 ret
= !wine_server_call_err( req
);
606 if ((event
= InterlockedExchangePointer( &console_wait_event
, NULL
))) NtClose( event
);
608 RtlLeaveCriticalSection( &console_section
);
613 /******************************************************************************
614 * GenerateConsoleCtrlEvent (kernelbase.@)
616 BOOL WINAPI DECLSPEC_HOTPATCH
GenerateConsoleCtrlEvent( DWORD event
, DWORD group
)
618 struct condrv_ctrl_event ctrl_event
;
620 TRACE( "(%d, %x)\n", event
, group
);
622 if (event
!= CTRL_C_EVENT
&& event
!= CTRL_BREAK_EVENT
)
624 ERR( "Invalid event %d for PGID %x\n", event
, group
);
628 ctrl_event
.event
= event
;
629 ctrl_event
.group_id
= group
;
630 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
,
631 IOCTL_CONDRV_CTRL_EVENT
, &ctrl_event
, sizeof(ctrl_event
), NULL
, 0, NULL
);
635 /******************************************************************************
636 * GetConsoleCP (kernelbase.@)
638 UINT WINAPI DECLSPEC_HOTPATCH
GetConsoleCP(void)
640 struct condrv_input_info info
;
642 if (!console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
,
643 IOCTL_CONDRV_GET_INPUT_INFO
, NULL
, 0, &info
, sizeof(info
), NULL
))
645 return info
.input_cp
? info
.input_cp
: GetOEMCP();
649 /******************************************************************************
650 * GetConsoleCursorInfo (kernelbase.@)
652 BOOL WINAPI DECLSPEC_HOTPATCH
GetConsoleCursorInfo( HANDLE handle
, CONSOLE_CURSOR_INFO
*info
)
654 struct condrv_output_info condrv_info
;
656 if (!console_ioctl( handle
, IOCTL_CONDRV_GET_OUTPUT_INFO
, NULL
, 0, &condrv_info
, sizeof(condrv_info
), NULL
))
661 SetLastError( ERROR_INVALID_ACCESS
);
665 info
->dwSize
= condrv_info
.cursor_size
;
666 info
->bVisible
= condrv_info
.cursor_visible
;
667 TRACE("(%p) returning (%d,%d)\n", handle
, info
->dwSize
, info
->bVisible
);
672 /***********************************************************************
673 * GetConsoleInputExeNameA (kernelbase.@)
675 BOOL WINAPI DECLSPEC_HOTPATCH
GetConsoleInputExeNameA( DWORD len
, LPSTR buffer
)
677 RtlEnterCriticalSection( &console_section
);
678 if (WideCharToMultiByte( CP_ACP
, 0, input_exe
, -1, NULL
, 0, NULL
, NULL
) <= len
)
679 WideCharToMultiByte( CP_ACP
, 0, input_exe
, -1, buffer
, len
, NULL
, NULL
);
680 else SetLastError(ERROR_BUFFER_OVERFLOW
);
681 RtlLeaveCriticalSection( &console_section
);
686 /***********************************************************************
687 * GetConsoleInputExeNameW (kernelbase.@)
689 BOOL WINAPI DECLSPEC_HOTPATCH
GetConsoleInputExeNameW( DWORD len
, LPWSTR buffer
)
691 RtlEnterCriticalSection( &console_section
);
692 if (len
> lstrlenW(input_exe
)) lstrcpyW( buffer
, input_exe
);
693 else SetLastError( ERROR_BUFFER_OVERFLOW
);
694 RtlLeaveCriticalSection( &console_section
);
699 /***********************************************************************
700 * GetConsoleMode (kernelbase.@)
702 BOOL WINAPI DECLSPEC_HOTPATCH
GetConsoleMode( HANDLE handle
, DWORD
*mode
)
704 return console_ioctl( handle
, IOCTL_CONDRV_GET_MODE
, NULL
, 0, mode
, sizeof(*mode
), NULL
);
708 /***********************************************************************
709 * GetConsoleOutputCP (kernelbase.@)
711 UINT WINAPI DECLSPEC_HOTPATCH
GetConsoleOutputCP(void)
713 struct condrv_input_info info
;
715 if (!console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
,
716 IOCTL_CONDRV_GET_INPUT_INFO
, NULL
, 0, &info
, sizeof(info
), NULL
))
718 return info
.output_cp
? info
.output_cp
: GetOEMCP();
722 /***********************************************************************
723 * GetConsoleScreenBufferInfo (kernelbase.@)
725 BOOL WINAPI DECLSPEC_HOTPATCH
GetConsoleScreenBufferInfo( HANDLE handle
, CONSOLE_SCREEN_BUFFER_INFO
*info
)
727 struct condrv_output_info condrv_info
;
729 if (!console_ioctl( handle
, IOCTL_CONDRV_GET_OUTPUT_INFO
, NULL
, 0,
730 &condrv_info
, sizeof(condrv_info
), NULL
))
733 info
->dwSize
.X
= condrv_info
.width
;
734 info
->dwSize
.Y
= condrv_info
.height
;
735 info
->dwCursorPosition
.X
= condrv_info
.cursor_x
;
736 info
->dwCursorPosition
.Y
= condrv_info
.cursor_y
;
737 info
->wAttributes
= condrv_info
.attr
;
738 info
->srWindow
.Left
= condrv_info
.win_left
;
739 info
->srWindow
.Right
= condrv_info
.win_right
;
740 info
->srWindow
.Top
= condrv_info
.win_top
;
741 info
->srWindow
.Bottom
= condrv_info
.win_bottom
;
742 info
->dwMaximumWindowSize
.X
= min(condrv_info
.width
, condrv_info
.max_width
);
743 info
->dwMaximumWindowSize
.Y
= min(condrv_info
.height
, condrv_info
.max_height
);
745 TRACE( "(%p,(%d,%d) (%d,%d) %d (%d,%d-%d,%d) (%d,%d)\n", handle
,
746 info
->dwSize
.X
, info
->dwSize
.Y
, info
->dwCursorPosition
.X
, info
->dwCursorPosition
.Y
,
747 info
->wAttributes
, info
->srWindow
.Left
, info
->srWindow
.Top
, info
->srWindow
.Right
,
748 info
->srWindow
.Bottom
, info
->dwMaximumWindowSize
.X
, info
->dwMaximumWindowSize
.Y
);
753 /***********************************************************************
754 * GetConsoleScreenBufferInfoEx (kernelbase.@)
756 BOOL WINAPI DECLSPEC_HOTPATCH
GetConsoleScreenBufferInfoEx( HANDLE handle
,
757 CONSOLE_SCREEN_BUFFER_INFOEX
*info
)
759 struct condrv_output_info condrv_info
;
761 if (info
->cbSize
!= sizeof(CONSOLE_SCREEN_BUFFER_INFOEX
))
763 SetLastError( ERROR_INVALID_PARAMETER
);
767 if (!console_ioctl( handle
, IOCTL_CONDRV_GET_OUTPUT_INFO
, NULL
, 0, &condrv_info
,
768 sizeof(condrv_info
), NULL
))
771 info
->dwSize
.X
= condrv_info
.width
;
772 info
->dwSize
.Y
= condrv_info
.height
;
773 info
->dwCursorPosition
.X
= condrv_info
.cursor_x
;
774 info
->dwCursorPosition
.Y
= condrv_info
.cursor_y
;
775 info
->wAttributes
= condrv_info
.attr
;
776 info
->srWindow
.Left
= condrv_info
.win_left
;
777 info
->srWindow
.Top
= condrv_info
.win_top
;
778 info
->srWindow
.Right
= condrv_info
.win_right
;
779 info
->srWindow
.Bottom
= condrv_info
.win_bottom
;
780 info
->dwMaximumWindowSize
.X
= min( condrv_info
.width
, condrv_info
.max_width
);
781 info
->dwMaximumWindowSize
.Y
= min( condrv_info
.height
, condrv_info
.max_height
);
782 info
->wPopupAttributes
= condrv_info
.popup_attr
;
783 info
->bFullscreenSupported
= FALSE
;
784 memcpy( info
->ColorTable
, condrv_info
.color_map
, sizeof(info
->ColorTable
) );
789 /******************************************************************************
790 * GetConsoleTitleW (kernelbase.@)
792 DWORD WINAPI DECLSPEC_HOTPATCH
GetConsoleTitleW( LPWSTR title
, DWORD size
)
796 if (!console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
, IOCTL_CONDRV_GET_TITLE
,
797 NULL
, 0, title
, (size
- 1) * sizeof(WCHAR
), &size
))
800 size
/= sizeof(WCHAR
);
806 /***********************************************************************
807 * GetLargestConsoleWindowSize (kernelbase.@)
809 #if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
810 #undef GetLargestConsoleWindowSize
811 DWORD WINAPI DECLSPEC_HOTPATCH
GetLargestConsoleWindowSize( HANDLE handle
)
817 x
.c
= get_largest_console_window_size( handle
);
823 COORD WINAPI DECLSPEC_HOTPATCH
GetLargestConsoleWindowSize( HANDLE handle
)
825 return get_largest_console_window_size( handle
);
828 #endif /* !defined(__i386__) */
831 /***********************************************************************
832 * GetNumberOfConsoleInputEvents (kernelbase.@)
834 BOOL WINAPI DECLSPEC_HOTPATCH
GetNumberOfConsoleInputEvents( HANDLE handle
, DWORD
*count
)
836 struct condrv_input_info info
;
837 if (!console_ioctl( handle
, IOCTL_CONDRV_GET_INPUT_INFO
, NULL
, 0, &info
, sizeof(info
), NULL
))
839 *count
= info
.input_count
;
844 /***********************************************************************
845 * PeekConsoleInputA (kernelbase.@)
847 BOOL WINAPI DECLSPEC_HOTPATCH
PeekConsoleInputA( HANDLE handle
, INPUT_RECORD
*buffer
,
848 DWORD length
, DWORD
*count
)
852 if (!PeekConsoleInputW( handle
, buffer
, length
, &read
)) return FALSE
;
853 input_records_WtoA( buffer
, read
);
854 if (count
) *count
= read
;
859 /***********************************************************************
860 * PeekConsoleInputW (kernelbase.@)
862 BOOL WINAPI DECLSPEC_HOTPATCH
PeekConsoleInputW( HANDLE handle
, INPUT_RECORD
*buffer
,
863 DWORD length
, DWORD
*count
)
866 if (!console_ioctl( handle
, IOCTL_CONDRV_PEEK
, NULL
, 0, buffer
, length
* sizeof(*buffer
), &read
))
868 if (count
) *count
= read
/ sizeof(*buffer
);
873 /******************************************************************************
874 * ReadConsoleOutputAttribute (kernelbase.@)
876 BOOL WINAPI DECLSPEC_HOTPATCH
ReadConsoleOutputAttribute( HANDLE handle
, WORD
*attr
, DWORD length
,
877 COORD coord
, DWORD
*count
)
879 struct condrv_output_params params
;
882 TRACE( "(%p,%p,%d,%dx%d,%p)\n", handle
, attr
, length
, coord
.X
, coord
.Y
, count
);
886 SetLastError( ERROR_INVALID_ACCESS
);
890 params
.mode
= CHAR_INFO_MODE_ATTR
;
894 ret
= console_ioctl( handle
, IOCTL_CONDRV_READ_OUTPUT
, ¶ms
, sizeof(params
),
895 attr
, length
* sizeof(*attr
), count
);
896 *count
/= sizeof(*attr
);
901 /******************************************************************************
902 * ReadConsoleOutputCharacterA (kernelbase.@)
904 BOOL WINAPI DECLSPEC_HOTPATCH
ReadConsoleOutputCharacterA( HANDLE handle
, LPSTR buffer
, DWORD length
,
905 COORD coord
, DWORD
*count
)
913 SetLastError( ERROR_INVALID_ACCESS
);
918 if (!(wptr
= HeapAlloc( GetProcessHeap(), 0, length
* sizeof(WCHAR
) )))
920 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
923 if ((ret
= ReadConsoleOutputCharacterW( handle
, wptr
, length
, coord
, &read
)))
925 read
= WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr
, read
, buffer
, length
, NULL
, NULL
);
928 HeapFree( GetProcessHeap(), 0, wptr
);
933 /******************************************************************************
934 * ReadConsoleOutputCharacterW (kernelbase.@)
936 BOOL WINAPI DECLSPEC_HOTPATCH
ReadConsoleOutputCharacterW( HANDLE handle
, LPWSTR buffer
, DWORD length
,
937 COORD coord
, DWORD
*count
)
939 struct condrv_output_params params
;
942 TRACE( "(%p,%p,%d,%dx%d,%p)\n", handle
, buffer
, length
, coord
.X
, coord
.Y
, count
);
946 SetLastError( ERROR_INVALID_ACCESS
);
950 params
.mode
= CHAR_INFO_MODE_TEXT
;
954 ret
= console_ioctl( handle
, IOCTL_CONDRV_READ_OUTPUT
, ¶ms
, sizeof(params
), buffer
,
955 length
* sizeof(*buffer
), count
);
956 *count
/= sizeof(*buffer
);
961 /******************************************************************************
962 * ReadConsoleOutputA (kernelbase.@)
964 BOOL WINAPI DECLSPEC_HOTPATCH
ReadConsoleOutputA( HANDLE handle
, CHAR_INFO
*buffer
, COORD size
,
965 COORD coord
, SMALL_RECT
*region
)
970 ret
= ReadConsoleOutputW( handle
, buffer
, size
, coord
, region
);
971 if (ret
&& region
->Right
>= region
->Left
)
973 UINT cp
= GetConsoleOutputCP();
974 for (y
= 0; y
<= region
->Bottom
- region
->Top
; y
++)
975 char_info_WtoA( cp
, &buffer
[(coord
.Y
+ y
) * size
.X
+ coord
.X
], region
->Right
- region
->Left
+ 1 );
981 /******************************************************************************
982 * ReadConsoleOutputW (kernelbase.@)
984 BOOL WINAPI DECLSPEC_HOTPATCH
ReadConsoleOutputW( HANDLE handle
, CHAR_INFO
*buffer
, COORD size
,
985 COORD coord
, SMALL_RECT
*region
)
987 struct condrv_output_params params
;
988 unsigned int width
, height
, y
;
993 if (region
->Left
> region
->Right
|| region
->Top
> region
->Bottom
)
995 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
998 if (size
.X
<= coord
.X
|| size
.Y
<= coord
.Y
)
1000 region
->Right
= region
->Left
- 1;
1001 region
->Bottom
= region
->Top
- 1;
1002 SetLastError( ERROR_INVALID_FUNCTION
);
1005 width
= min( region
->Right
- region
->Left
+ 1, size
.X
- coord
.X
);
1006 height
= min( region
->Bottom
- region
->Top
+ 1, size
.Y
- coord
.Y
);
1007 region
->Right
= region
->Left
+ width
- 1;
1008 region
->Bottom
= region
->Top
+ height
- 1;
1010 count
= sizeof(*result
) + width
* height
* sizeof(*buffer
);
1011 if (!(result
= HeapAlloc( GetProcessHeap(), 0, count
)))
1013 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1017 params
.mode
= CHAR_INFO_MODE_TEXTATTR
;
1018 params
.x
= region
->Left
;
1019 params
.y
= region
->Top
;
1020 params
.width
= width
;
1021 if ((ret
= console_ioctl( handle
, IOCTL_CONDRV_READ_OUTPUT
, ¶ms
, sizeof(params
), result
, count
, &count
)) && count
)
1023 CHAR_INFO
*char_info
= (CHAR_INFO
*)(result
+ 1);
1025 width
= region
->Right
- region
->Left
+ 1;
1026 height
= region
->Bottom
- region
->Top
+ 1;
1027 for (y
= 0; y
< height
; y
++)
1028 memcpy( &buffer
[(y
+ coord
.Y
) * size
.X
+ coord
.X
], &char_info
[y
* width
], width
* sizeof(*buffer
) );
1030 HeapFree( GetProcessHeap(), 0, result
);
1035 /******************************************************************************
1036 * ScrollConsoleScreenBufferA (kernelbase.@)
1038 BOOL WINAPI DECLSPEC_HOTPATCH
ScrollConsoleScreenBufferA( HANDLE handle
, const SMALL_RECT
*scroll
,
1039 const SMALL_RECT
*clip
, COORD origin
, const CHAR_INFO
*fill
)
1043 ciW
.Attributes
= fill
->Attributes
;
1044 MultiByteToWideChar( GetConsoleOutputCP(), 0, &fill
->Char
.AsciiChar
, 1, &ciW
.Char
.UnicodeChar
, 1 );
1046 return ScrollConsoleScreenBufferW( handle
, scroll
, clip
, origin
, &ciW
);
1050 /******************************************************************************
1051 * ScrollConsoleScreenBufferW (kernelbase.@)
1053 BOOL WINAPI DECLSPEC_HOTPATCH
ScrollConsoleScreenBufferW( HANDLE handle
, const SMALL_RECT
*scroll
,
1054 const SMALL_RECT
*clip_rect
, COORD origin
,
1055 const CHAR_INFO
*fill
)
1057 struct condrv_scroll_params params
;
1060 TRACE( "(%p,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", handle
,
1061 scroll
->Left
, scroll
->Top
, scroll
->Right
, scroll
->Bottom
,
1062 clip_rect
->Left
, clip_rect
->Top
, clip_rect
->Right
, clip_rect
->Bottom
,
1063 origin
.X
, origin
.Y
, fill
);
1065 TRACE("(%p,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", handle
,
1066 scroll
->Left
, scroll
->Top
, scroll
->Right
, scroll
->Bottom
,
1067 origin
.X
, origin
.Y
, fill
);
1069 params
.scroll
= *scroll
;
1070 params
.origin
= origin
;
1071 params
.fill
.ch
= fill
->Char
.UnicodeChar
;
1072 params
.fill
.attr
= fill
->Attributes
;
1075 params
.clip
.Left
= params
.clip
.Top
= 0;
1076 params
.clip
.Right
= params
.clip
.Bottom
= SHRT_MAX
;
1078 else params
.clip
= *clip_rect
;
1079 return console_ioctl( handle
, IOCTL_CONDRV_SCROLL
, (void *)¶ms
, sizeof(params
), NULL
, 0, NULL
);
1083 /******************************************************************************
1084 * SetConsoleActiveScreenBuffer (kernelbase.@)
1086 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleActiveScreenBuffer( HANDLE handle
)
1088 TRACE( "(%p)\n", handle
);
1089 return console_ioctl( handle
, IOCTL_CONDRV_ACTIVATE
, NULL
, 0, NULL
, 0, NULL
);
1093 /******************************************************************************
1094 * SetConsoleCP (kernelbase.@)
1096 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleCP( UINT cp
)
1098 struct condrv_input_info_params params
= { SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE
};
1100 if (!IsValidCodePage( cp
))
1102 SetLastError( ERROR_INVALID_PARAMETER
);
1106 params
.info
.input_cp
= cp
;
1107 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
,
1108 IOCTL_CONDRV_SET_INPUT_INFO
, ¶ms
, sizeof(params
), NULL
, 0, NULL
);
1112 /******************************************************************************
1113 * SetConsoleCtrlHandler (kernelbase.@)
1115 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleCtrlHandler( PHANDLER_ROUTINE func
, BOOL add
)
1117 struct ctrl_handler
*handler
;
1120 TRACE( "(%p,%d)\n", func
, add
);
1122 RtlEnterCriticalSection( &console_section
);
1126 if (add
) NtCurrentTeb()->Peb
->ProcessParameters
->ConsoleFlags
|= 1;
1127 else NtCurrentTeb()->Peb
->ProcessParameters
->ConsoleFlags
&= ~1;
1132 if ((handler
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler
) )))
1134 handler
->func
= func
;
1135 handler
->next
= ctrl_handlers
;
1136 ctrl_handlers
= handler
;
1142 struct ctrl_handler
**p_handler
;
1144 for (p_handler
= &ctrl_handlers
; *p_handler
; p_handler
= &(*p_handler
)->next
)
1146 if ((*p_handler
)->func
== func
) break;
1148 if (*p_handler
&& *p_handler
!= &default_handler
)
1150 handler
= *p_handler
;
1151 *p_handler
= handler
->next
;
1152 RtlFreeHeap( GetProcessHeap(), 0, handler
);
1155 else SetLastError( ERROR_INVALID_PARAMETER
);
1158 RtlLeaveCriticalSection( &console_section
);
1163 /******************************************************************************
1164 * SetConsoleCursorInfo (kernelbase.@)
1166 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleCursorInfo( HANDLE handle
, CONSOLE_CURSOR_INFO
*info
)
1168 struct condrv_output_info_params params
= { SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM
};
1170 TRACE( "(%p,%d,%d)\n", handle
, info
->dwSize
, info
->bVisible
);
1172 params
.info
.cursor_size
= info
->dwSize
;
1173 params
.info
.cursor_visible
= info
->bVisible
;
1174 return console_ioctl( handle
, IOCTL_CONDRV_SET_OUTPUT_INFO
, ¶ms
, sizeof(params
),
1179 /******************************************************************************
1180 * SetConsoleCursorPosition (kernelbase.@)
1182 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleCursorPosition( HANDLE handle
, COORD pos
)
1184 struct condrv_output_info_params params
= { SET_CONSOLE_OUTPUT_INFO_CURSOR_POS
};
1185 CONSOLE_SCREEN_BUFFER_INFO info
;
1186 int w
, h
, do_move
= 0;
1188 TRACE( "%p %d %d\n", handle
, pos
.X
, pos
.Y
);
1190 params
.info
.cursor_x
= pos
.X
;
1191 params
.info
.cursor_y
= pos
.Y
;
1192 if (!console_ioctl( handle
, IOCTL_CONDRV_SET_OUTPUT_INFO
, ¶ms
, sizeof(params
), NULL
, 0, NULL
))
1195 if (!GetConsoleScreenBufferInfo( handle
, &info
)) return FALSE
;
1197 /* if cursor is no longer visible, scroll the visible window... */
1198 w
= info
.srWindow
.Right
- info
.srWindow
.Left
+ 1;
1199 h
= info
.srWindow
.Bottom
- info
.srWindow
.Top
+ 1;
1200 if (pos
.X
< info
.srWindow
.Left
)
1202 info
.srWindow
.Left
= min(pos
.X
, info
.dwSize
.X
- w
);
1205 else if (pos
.X
> info
.srWindow
.Right
)
1207 info
.srWindow
.Left
= max(pos
.X
, w
) - w
+ 1;
1210 info
.srWindow
.Right
= info
.srWindow
.Left
+ w
- 1;
1212 if (pos
.Y
< info
.srWindow
.Top
)
1214 info
.srWindow
.Top
= min(pos
.Y
, info
.dwSize
.Y
- h
);
1217 else if (pos
.Y
> info
.srWindow
.Bottom
)
1219 info
.srWindow
.Top
= max(pos
.Y
, h
) - h
+ 1;
1222 info
.srWindow
.Bottom
= info
.srWindow
.Top
+ h
- 1;
1224 return !do_move
|| SetConsoleWindowInfo( handle
, TRUE
, &info
.srWindow
);
1228 /******************************************************************************
1229 * SetConsoleInputExeNameA (kernelbase.@)
1231 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleInputExeNameA( LPCSTR name
)
1233 if (!name
|| !name
[0])
1235 SetLastError( ERROR_INVALID_PARAMETER
);
1238 RtlEnterCriticalSection( &console_section
);
1239 MultiByteToWideChar( CP_ACP
, 0, name
, -1, input_exe
, ARRAY_SIZE(input_exe
) );
1240 RtlLeaveCriticalSection( &console_section
);
1245 /******************************************************************************
1246 * SetConsoleInputExeNameW (kernelbase.@)
1248 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleInputExeNameW( LPCWSTR name
)
1250 if (!name
|| !name
[0])
1252 SetLastError( ERROR_INVALID_PARAMETER
);
1255 RtlEnterCriticalSection( &console_section
);
1256 lstrcpynW( input_exe
, name
, ARRAY_SIZE(input_exe
) );
1257 RtlLeaveCriticalSection( &console_section
);
1262 /******************************************************************************
1263 * SetConsoleMode (kernelbase.@)
1265 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleMode( HANDLE handle
, DWORD mode
)
1267 TRACE( "(%p,%x)\n", handle
, mode
);
1268 return console_ioctl( handle
, IOCTL_CONDRV_SET_MODE
, &mode
, sizeof(mode
), NULL
, 0, NULL
);
1272 /******************************************************************************
1273 * SetConsoleOutputCP (kernelbase.@)
1275 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleOutputCP( UINT cp
)
1277 struct condrv_input_info_params params
= { SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE
};
1279 if (!IsValidCodePage( cp
))
1281 SetLastError( ERROR_INVALID_PARAMETER
);
1285 params
.info
.output_cp
= cp
;
1286 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
,
1287 IOCTL_CONDRV_SET_INPUT_INFO
, ¶ms
, sizeof(params
), NULL
, 0, NULL
);
1291 /******************************************************************************
1292 * SetConsoleScreenBufferInfoEx (kernelbase.@)
1294 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleScreenBufferInfoEx( HANDLE handle
,
1295 CONSOLE_SCREEN_BUFFER_INFOEX
*info
)
1297 struct condrv_output_info_params params
=
1298 { SET_CONSOLE_OUTPUT_INFO_CURSOR_POS
| SET_CONSOLE_OUTPUT_INFO_SIZE
|
1299 SET_CONSOLE_OUTPUT_INFO_ATTR
| SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR
|
1300 SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW
| SET_CONSOLE_OUTPUT_INFO_MAX_SIZE
};
1302 TRACE("(%p, %p)\n", handle
, info
);
1304 if (info
->cbSize
!= sizeof(CONSOLE_SCREEN_BUFFER_INFOEX
))
1306 SetLastError(ERROR_INVALID_PARAMETER
);
1310 params
.info
.width
= info
->dwSize
.X
;
1311 params
.info
.height
= info
->dwSize
.Y
;
1312 params
.info
.cursor_x
= info
->dwCursorPosition
.X
;
1313 params
.info
.cursor_y
= info
->dwCursorPosition
.Y
;
1314 params
.info
.attr
= info
->wAttributes
;
1315 params
.info
.win_left
= info
->srWindow
.Left
;
1316 params
.info
.win_top
= info
->srWindow
.Top
;
1317 params
.info
.win_right
= info
->srWindow
.Right
;
1318 params
.info
.win_bottom
= info
->srWindow
.Bottom
;
1319 params
.info
.popup_attr
= info
->wPopupAttributes
;
1320 params
.info
.max_width
= min( info
->dwMaximumWindowSize
.X
, info
->dwSize
.X
);
1321 params
.info
.max_height
= min( info
->dwMaximumWindowSize
.Y
, info
->dwSize
.Y
);
1322 return console_ioctl( handle
, IOCTL_CONDRV_SET_OUTPUT_INFO
, ¶ms
, sizeof(params
), NULL
, 0, NULL
);
1326 /******************************************************************************
1327 * SetConsoleScreenBufferSize (kernelbase.@)
1329 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleScreenBufferSize( HANDLE handle
, COORD size
)
1331 struct condrv_output_info_params params
= { SET_CONSOLE_OUTPUT_INFO_SIZE
};
1333 TRACE( "(%p,(%d,%d))\n", handle
, size
.X
, size
.Y
);
1335 params
.info
.width
= size
.X
;
1336 params
.info
.height
= size
.Y
;
1337 return console_ioctl( handle
, IOCTL_CONDRV_SET_OUTPUT_INFO
, ¶ms
, sizeof(params
), NULL
, 0, NULL
);
1341 /******************************************************************************
1342 * SetConsoleTextAttribute (kernelbase.@)
1344 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleTextAttribute( HANDLE handle
, WORD attr
)
1346 struct condrv_output_info_params params
= { SET_CONSOLE_OUTPUT_INFO_ATTR
};
1348 TRACE( "(%p,%d)\n", handle
, attr
);
1350 params
.info
.attr
= attr
;
1351 return console_ioctl( handle
, IOCTL_CONDRV_SET_OUTPUT_INFO
, ¶ms
, sizeof(params
), NULL
, 0, NULL
);
1355 /******************************************************************************
1356 * SetConsoleTitleW (kernelbase.@)
1358 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleTitleW( LPCWSTR title
)
1360 TRACE( "%s\n", debugstr_w( title
));
1362 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
, IOCTL_CONDRV_SET_TITLE
,
1363 (void *)title
, lstrlenW(title
) * sizeof(WCHAR
), NULL
, 0, NULL
);
1367 /******************************************************************************
1368 * SetConsoleWindowInfo (kernelbase.@)
1370 BOOL WINAPI DECLSPEC_HOTPATCH
SetConsoleWindowInfo( HANDLE handle
, BOOL absolute
, SMALL_RECT
*window
)
1372 struct condrv_output_info_params params
= { SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW
};
1373 SMALL_RECT rect
= *window
;
1375 TRACE( "(%p,%d,(%d,%d-%d,%d))\n", handle
, absolute
, rect
.Left
, rect
.Top
, rect
.Right
, rect
.Bottom
);
1379 CONSOLE_SCREEN_BUFFER_INFO info
;
1381 if (!GetConsoleScreenBufferInfo( handle
, &info
)) return FALSE
;
1382 rect
.Left
+= info
.srWindow
.Left
;
1383 rect
.Top
+= info
.srWindow
.Top
;
1384 rect
.Right
+= info
.srWindow
.Right
;
1385 rect
.Bottom
+= info
.srWindow
.Bottom
;
1388 params
.info
.win_left
= rect
.Left
;
1389 params
.info
.win_top
= rect
.Top
;
1390 params
.info
.win_right
= rect
.Right
;
1391 params
.info
.win_bottom
= rect
.Bottom
;
1392 return console_ioctl( handle
, IOCTL_CONDRV_SET_OUTPUT_INFO
, ¶ms
, sizeof(params
), NULL
, 0, NULL
);
1396 /***********************************************************************
1397 * ReadConsoleInputA (kernelbase.@)
1399 BOOL WINAPI
ReadConsoleInputA( HANDLE handle
, INPUT_RECORD
*buffer
, DWORD length
, DWORD
*count
)
1403 if (!ReadConsoleInputW( handle
, buffer
, length
, &read
)) return FALSE
;
1404 input_records_WtoA( buffer
, read
);
1405 if (count
) *count
= read
;
1410 /***********************************************************************
1411 * ReadConsoleInputW (kernelbase.@)
1413 BOOL WINAPI
ReadConsoleInputW( HANDLE handle
, INPUT_RECORD
*buffer
, DWORD length
, DWORD
*count
)
1416 if (!console_ioctl( handle
, IOCTL_CONDRV_READ_INPUT
, &blocking
, sizeof(blocking
),
1417 buffer
, length
* sizeof(*buffer
), count
))
1419 *count
/= sizeof(*buffer
);
1424 /******************************************************************************
1425 * WriteConsoleInputA (kernelbase.@)
1427 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleInputA( HANDLE handle
, const INPUT_RECORD
*buffer
,
1428 DWORD count
, DWORD
*written
)
1430 INPUT_RECORD
*recW
= NULL
;
1437 SetLastError( ERROR_INVALID_ACCESS
);
1440 if (!(recW
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*recW
) )))
1442 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1445 memcpy( recW
, buffer
, count
* sizeof(*recW
) );
1446 input_records_AtoW( recW
, count
);
1448 ret
= WriteConsoleInputW( handle
, recW
, count
, written
);
1449 HeapFree( GetProcessHeap(), 0, recW
);
1454 /******************************************************************************
1455 * WriteConsoleInputW (kernelbase.@)
1457 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleInputW( HANDLE handle
, const INPUT_RECORD
*buffer
,
1458 DWORD count
, DWORD
*written
)
1460 TRACE( "(%p,%p,%d,%p)\n", handle
, buffer
, count
, written
);
1462 if (count
> 0 && !buffer
)
1464 SetLastError( ERROR_INVALID_ACCESS
);
1468 if (!DeviceIoControl( handle
, IOCTL_CONDRV_WRITE_INPUT
, (void *)buffer
, count
* sizeof(*buffer
), NULL
, 0, NULL
, NULL
))
1473 SetLastError( ERROR_INVALID_ACCESS
);
1481 /***********************************************************************
1482 * WriteConsoleOutputA (kernelbase.@)
1484 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleOutputA( HANDLE handle
, const CHAR_INFO
*buffer
,
1485 COORD size
, COORD coord
, SMALL_RECT
*region
)
1489 COORD new_size
, new_coord
;
1492 new_size
.X
= min( region
->Right
- region
->Left
+ 1, size
.X
- coord
.X
);
1493 new_size
.Y
= min( region
->Bottom
- region
->Top
+ 1, size
.Y
- coord
.Y
);
1495 if (new_size
.X
<= 0 || new_size
.Y
<= 0)
1497 region
->Bottom
= region
->Top
+ new_size
.Y
- 1;
1498 region
->Right
= region
->Left
+ new_size
.X
- 1;
1502 /* only copy the useful rectangle */
1503 if (!(ciW
= HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO
) * new_size
.X
* new_size
.Y
)))
1505 for (y
= 0; y
< new_size
.Y
; y
++)
1506 memcpy( &ciW
[y
* new_size
.X
], &buffer
[(y
+ coord
.Y
) * size
.X
+ coord
.X
],
1507 new_size
.X
* sizeof(CHAR_INFO
) );
1508 char_info_AtoW( ciW
, new_size
.X
* new_size
.Y
);
1509 new_coord
.X
= new_coord
.Y
= 0;
1510 ret
= WriteConsoleOutputW( handle
, ciW
, new_size
, new_coord
, region
);
1511 HeapFree( GetProcessHeap(), 0, ciW
);
1516 /***********************************************************************
1517 * WriteConsoleOutputW (kernelbase.@)
1519 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleOutputW( HANDLE handle
, const CHAR_INFO
*buffer
,
1520 COORD size
, COORD coord
, SMALL_RECT
*region
)
1522 struct condrv_output_params
*params
;
1523 unsigned int width
, height
, y
;
1527 TRACE( "(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n",
1528 handle
, buffer
, size
.X
, size
.Y
, coord
.X
, coord
.Y
,
1529 region
->Left
, region
->Top
, region
->Right
, region
->Bottom
);
1531 if (region
->Left
> region
->Right
|| region
->Top
> region
->Bottom
|| size
.X
<= coord
.X
|| size
.Y
<= coord
.Y
)
1533 SetLastError( ERROR_INVALID_PARAMETER
);
1537 width
= min( region
->Right
- region
->Left
+ 1, size
.X
- coord
.X
);
1538 height
= min( region
->Bottom
- region
->Top
+ 1, size
.Y
- coord
.Y
);
1539 region
->Right
= region
->Left
+ width
- 1;
1540 region
->Bottom
= region
->Top
+ height
- 1;
1542 params_size
= sizeof(*params
) + width
* height
* sizeof(*buffer
);
1543 if (!(params
= HeapAlloc( GetProcessHeap(), 0, params_size
)))
1545 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1549 params
->mode
= CHAR_INFO_MODE_TEXTATTR
;
1550 params
->x
= region
->Left
;
1551 params
->y
= region
->Top
;
1552 params
->width
= width
;
1554 for (y
= 0; y
< height
; y
++)
1555 memcpy( &((CHAR_INFO
*)(params
+ 1))[y
* width
], &buffer
[(y
+ coord
.Y
) * size
.X
+ coord
.X
], width
* sizeof(CHAR_INFO
) );
1557 ret
= console_ioctl( handle
, IOCTL_CONDRV_WRITE_OUTPUT
, params
, params_size
, region
, sizeof(*region
), NULL
);
1558 HeapFree( GetProcessHeap(), 0, params
);
1563 /******************************************************************************
1564 * WriteConsoleOutputAttribute (kernelbase.@)
1566 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleOutputAttribute( HANDLE handle
, const WORD
*attr
, DWORD length
,
1567 COORD coord
, DWORD
*written
)
1569 struct condrv_output_params
*params
;
1573 TRACE( "(%p,%p,%d,%dx%d,%p)\n", handle
, attr
, length
, coord
.X
, coord
.Y
, written
);
1575 if ((length
> 0 && !attr
) || !written
)
1577 SetLastError( ERROR_INVALID_ACCESS
);
1582 size
= sizeof(*params
) + length
* sizeof(WORD
);
1583 if (!(params
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
1584 params
->mode
= CHAR_INFO_MODE_ATTR
;
1585 params
->x
= coord
.X
;
1586 params
->y
= coord
.Y
;
1588 memcpy( params
+ 1, attr
, length
* sizeof(*attr
) );
1589 ret
= console_ioctl( handle
, IOCTL_CONDRV_WRITE_OUTPUT
, params
, size
, written
, sizeof(*written
), NULL
);
1590 HeapFree( GetProcessHeap(), 0, params
);
1595 /******************************************************************************
1596 * WriteConsoleOutputCharacterA (kernelbase.@)
1598 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleOutputCharacterA( HANDLE handle
, LPCSTR str
, DWORD length
,
1599 COORD coord
, DWORD
*written
)
1605 TRACE( "(%p,%s,%d,%dx%d,%p)\n", handle
, debugstr_an(str
, length
), length
, coord
.X
, coord
.Y
, written
);
1609 UINT cp
= GetConsoleOutputCP();
1612 SetLastError( ERROR_INVALID_ACCESS
);
1615 lenW
= MultiByteToWideChar( cp
, 0, str
, length
, NULL
, 0 );
1617 if (!(strW
= HeapAlloc( GetProcessHeap(), 0, lenW
* sizeof(WCHAR
) )))
1619 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1622 MultiByteToWideChar( cp
, 0, str
, length
, strW
, lenW
);
1624 ret
= WriteConsoleOutputCharacterW( handle
, strW
, lenW
, coord
, written
);
1625 HeapFree( GetProcessHeap(), 0, strW
);
1630 /******************************************************************************
1631 * WriteConsoleOutputCharacterW (kernelbase.@)
1633 BOOL WINAPI DECLSPEC_HOTPATCH
WriteConsoleOutputCharacterW( HANDLE handle
, LPCWSTR str
, DWORD length
,
1634 COORD coord
, DWORD
*written
)
1636 struct condrv_output_params
*params
;
1640 TRACE( "(%p,%s,%d,%dx%d,%p)\n", handle
, debugstr_wn(str
, length
), length
, coord
.X
, coord
.Y
, written
);
1642 if ((length
> 0 && !str
) || !written
)
1644 SetLastError( ERROR_INVALID_ACCESS
);
1649 size
= sizeof(*params
) + length
* sizeof(WCHAR
);
1650 if (!(params
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
1651 params
->mode
= CHAR_INFO_MODE_TEXT
;
1652 params
->x
= coord
.X
;
1653 params
->y
= coord
.Y
;
1655 memcpy( params
+ 1, str
, length
* sizeof(*str
) );
1656 ret
= console_ioctl( handle
, IOCTL_CONDRV_WRITE_OUTPUT
, params
, size
, written
, sizeof(*written
), NULL
);
1657 HeapFree( GetProcessHeap(), 0, params
);
1662 /***********************************************************************
1663 * Beep (kernelbase.@)
1665 BOOL WINAPI
Beep( DWORD frequency
, DWORD duration
)
1667 /* FIXME: we should not require a console to be attached */
1668 console_ioctl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
,
1669 IOCTL_CONDRV_BEEP
, NULL
, 0, NULL
, 0, NULL
);
1674 static HANDLE
create_pseudo_console( COORD size
, HANDLE input
, HANDLE output
, HANDLE signal
,
1675 DWORD flags
, HANDLE
*process
)
1677 WCHAR cmd
[MAX_PATH
], conhost_path
[MAX_PATH
];
1678 PROCESS_INFORMATION pi
;
1679 HANDLE server
, console
;
1684 if (!(server
= create_console_server())) return NULL
;
1686 console
= create_console_reference( server
);
1693 memset( &si
, 0, sizeof(si
) );
1694 si
.StartupInfo
.cb
= sizeof(STARTUPINFOEXW
);
1695 si
.StartupInfo
.hStdInput
= input
;
1696 si
.StartupInfo
.hStdOutput
= output
;
1697 si
.StartupInfo
.hStdError
= output
;
1698 si
.StartupInfo
.dwFlags
= STARTF_USESTDHANDLES
;
1699 swprintf( conhost_path
, ARRAY_SIZE(conhost_path
), L
"%s\\conhost.exe", system_dir
);
1702 swprintf( cmd
, ARRAY_SIZE(cmd
),
1703 L
"\"%s\" --headless %s--width %u --height %u --signal 0x%x --server 0x%x",
1704 conhost_path
, (flags
& PSEUDOCONSOLE_INHERIT_CURSOR
) ? L
"--inheritcursor " : L
"",
1705 size
.X
, size
.Y
, signal
, server
);
1709 swprintf( cmd
, ARRAY_SIZE(cmd
), L
"\"%s\" --unix --width %u --height %u --server 0x%x",
1710 conhost_path
, size
.X
, size
.Y
, server
);
1712 Wow64DisableWow64FsRedirection( &redir
);
1713 res
= CreateProcessW( conhost_path
, cmd
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
,
1714 &si
.StartupInfo
, &pi
);
1715 Wow64RevertWow64FsRedirection( redir
);
1723 NtClose( pi
.hThread
);
1724 *process
= pi
.hProcess
;
1728 /******************************************************************************
1729 * CreatePseudoConsole (kernelbase.@)
1731 HRESULT WINAPI
CreatePseudoConsole( COORD size
, HANDLE input
, HANDLE output
, DWORD flags
, HPCON
*ret
)
1733 SECURITY_ATTRIBUTES inherit_attr
= { sizeof(inherit_attr
), NULL
, TRUE
};
1734 struct pseudo_console
*pseudo_console
;
1735 HANDLE tty_input
= NULL
, tty_output
;
1736 HANDLE signal
= NULL
;
1737 WCHAR pipe_name
[64];
1739 TRACE( "(%u,%u) %p %p %x %p\n", size
.X
, size
.Y
, input
, output
, flags
, ret
);
1741 if (!size
.X
|| !size
.Y
|| !ret
) return E_INVALIDARG
;
1743 if (!(pseudo_console
= HeapAlloc( GetProcessHeap(), 0, HEAP_ZERO_MEMORY
))) return E_OUTOFMEMORY
;
1745 swprintf( pipe_name
, ARRAY_SIZE(pipe_name
), L
"\\\\.\\pipe\\wine_pty_signal_pipe%x",
1746 GetCurrentThreadId() );
1747 signal
= CreateNamedPipeW( pipe_name
, PIPE_ACCESS_INBOUND
| FILE_FLAG_OVERLAPPED
, PIPE_TYPE_BYTE
,
1748 PIPE_UNLIMITED_INSTANCES
, 4096, 4096, NMPWAIT_USE_DEFAULT_WAIT
, &inherit_attr
);
1749 if (signal
== INVALID_HANDLE_VALUE
)
1751 HeapFree( GetProcessHeap(), 0, pseudo_console
);
1752 return HRESULT_FROM_WIN32( GetLastError() );
1754 pseudo_console
->signal
= CreateFileW( pipe_name
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
1755 if (pseudo_console
->signal
!= INVALID_HANDLE_VALUE
&&
1756 DuplicateHandle( GetCurrentProcess(), input
, GetCurrentProcess(), &tty_input
, 0, TRUE
, DUPLICATE_SAME_ACCESS
) &&
1757 DuplicateHandle( GetCurrentProcess(), output
, GetCurrentProcess(), &tty_output
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1759 pseudo_console
->reference
= create_pseudo_console( size
, tty_input
, tty_output
, signal
, flags
,
1760 &pseudo_console
->process
);
1761 NtClose( tty_output
);
1763 NtClose( tty_input
);
1765 if (!pseudo_console
->reference
)
1767 ClosePseudoConsole( pseudo_console
);
1768 return HRESULT_FROM_WIN32( GetLastError() );
1771 *ret
= pseudo_console
;
1775 /******************************************************************************
1776 * ClosePseudoConsole (kernelbase.@)
1778 void WINAPI
ClosePseudoConsole( HPCON handle
)
1780 struct pseudo_console
*pseudo_console
= handle
;
1782 TRACE( "%p\n", handle
);
1784 if (!pseudo_console
) return;
1785 if (pseudo_console
->signal
) CloseHandle( pseudo_console
->signal
);
1786 if (pseudo_console
->process
)
1788 WaitForSingleObject( pseudo_console
->process
, INFINITE
);
1789 CloseHandle( pseudo_console
->process
);
1791 if (pseudo_console
->reference
) CloseHandle( pseudo_console
->reference
);
1794 /******************************************************************************
1795 * ResizePseudoConsole (kernelbase.@)
1797 HRESULT WINAPI
ResizePseudoConsole( HPCON handle
, COORD size
)
1799 FIXME( "%p (%u,%u)\n", handle
, size
.X
, size
.Y
);
1803 static BOOL
is_tty_handle( HANDLE handle
)
1805 return ((UINT_PTR
)handle
& 3) == 1;
1808 void init_console( void )
1810 RTL_USER_PROCESS_PARAMETERS
*params
= RtlGetCurrentPeb()->ProcessParameters
;
1812 if (params
->ConsoleHandle
== CONSOLE_HANDLE_SHELL
)
1814 HANDLE tty_in
= NULL
, tty_out
= NULL
, process
= NULL
;
1817 if (is_tty_handle( params
->hStdInput
))
1819 tty_in
= params
->hStdInput
;
1820 params
->hStdInput
= NULL
;
1822 if (is_tty_handle( params
->hStdOutput
))
1824 tty_out
= params
->hStdOutput
;
1825 params
->hStdOutput
= NULL
;
1827 if (is_tty_handle( params
->hStdError
))
1829 if (tty_out
) CloseHandle( params
->hStdError
);
1830 else tty_out
= params
->hStdError
;
1831 params
->hStdError
= NULL
;
1834 size
.X
= params
->dwXCountChars
;
1835 size
.Y
= params
->dwYCountChars
;
1836 TRACE( "creating unix console (size %u %u)\n", size
.X
, size
.Y
);
1837 params
->ConsoleHandle
= create_pseudo_console( size
, tty_in
, tty_out
, NULL
, 0, &process
);
1838 CloseHandle( process
);
1839 CloseHandle( tty_in
);
1840 CloseHandle( tty_out
);
1842 if (params
->ConsoleHandle
&& create_console_connection( params
->ConsoleHandle
))
1844 init_console_std_handles( FALSE
);
1848 else if (params
->ConsoleHandle
== CONSOLE_HANDLE_ALLOC
)
1850 HMODULE mod
= GetModuleHandleW( NULL
);
1851 params
->ConsoleHandle
= NULL
;
1852 if (RtlImageNtHeader( mod
)->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
)
1855 else if (params
->ConsoleHandle
) create_console_connection( params
->ConsoleHandle
);
1857 RtlAddVectoredExceptionHandler( FALSE
, handle_ctrl_c
);