Reactos
at master 2389 lines 79 kB view raw
1/* 2 * Win32 console functions 3 * 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 11 * 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. 16 * 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. 21 * 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 25 */ 26 27#include <stdarg.h> 28#include <stdio.h> 29#include <string.h> 30#include <limits.h> 31 32#include "ntstatus.h" 33#define WIN32_NO_STATUS 34#include "windef.h" 35#include "winbase.h" 36#include "winnls.h" 37#include "winerror.h" 38#include "wincon.h" 39#include "winternl.h" 40#include "wine/condrv.h" 41#include "wine/exception.h" 42#include "wine/debug.h" 43#include "kernelbase.h" 44 45WINE_DEFAULT_DEBUG_CHANNEL(console); 46 47 48static CRITICAL_SECTION console_section; 49static CRITICAL_SECTION_DEBUG critsect_debug = 50{ 51 0, 0, &console_section, 52 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList }, 53 0, 0, { (DWORD_PTR)(__FILE__ ": console_section") } 54}; 55static CRITICAL_SECTION console_section = { &critsect_debug, -1, 0, 0, 0, 0 }; 56 57static HANDLE console_connection; 58static unsigned int console_flags; 59 60#define CONSOLE_INPUT_HANDLE 0x01 61#define CONSOLE_OUTPUT_HANDLE 0x02 62#define CONSOLE_ERROR_HANDLE 0x04 63 64static WCHAR input_exe[MAX_PATH + 1]; 65 66struct ctrl_handler 67{ 68 PHANDLER_ROUTINE func; 69 struct ctrl_handler *next; 70}; 71 72static BOOL WINAPI default_ctrl_handler( DWORD type ) 73{ 74 FIXME( "Terminating process %lx on event %lx\n", GetCurrentProcessId(), type ); 75 if (type == CTRL_C_EVENT || type == CTRL_BREAK_EVENT) 76 RtlExitUserProcess( STATUS_CONTROL_C_EXIT ); 77 else 78 RtlExitUserProcess( 0 ); 79 return TRUE; 80} 81 82static struct ctrl_handler default_handler = { default_ctrl_handler, NULL }; 83static struct ctrl_handler *ctrl_handlers = &default_handler; 84 85static BOOL console_ioctl( HANDLE handle, DWORD code, void *in_buff, DWORD in_count, 86 void *out_buff, DWORD out_count, DWORD *read ) 87{ 88 IO_STATUS_BLOCK io; 89 NTSTATUS status; 90 91 if (handle == CONSOLE_HANDLE_SHELL_NO_WINDOW) 92 { 93 WARN("Incorrect access to Shell-no-window console (ioctl=%lx)\n", code); 94 SetLastError( ERROR_INVALID_ACCESS ); 95 return FALSE; 96 } 97 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io, code, in_buff, in_count, 98 out_buff, out_count ); 99 switch( status ) 100 { 101 case STATUS_SUCCESS: 102 if (read) *read = io.Information; 103 return TRUE; 104 case STATUS_INVALID_PARAMETER: 105 break; 106 default: 107 status = STATUS_INVALID_HANDLE; 108 break; 109 } 110 if (read) *read = 0; 111 return set_ntstatus( status ); 112} 113 114/* map input records to ASCII */ 115static void input_records_WtoA( INPUT_RECORD *buffer, int count ) 116{ 117 UINT cp = GetConsoleCP(); 118 int i; 119 char ch; 120 121 for (i = 0; i < count; i++) 122 { 123 if (buffer[i].EventType != KEY_EVENT) continue; 124 WideCharToMultiByte( cp, 0, &buffer[i].Event.KeyEvent.uChar.UnicodeChar, 1, &ch, 1, NULL, NULL ); 125 buffer[i].Event.KeyEvent.uChar.AsciiChar = ch; 126 } 127} 128 129/* map input records to Unicode */ 130static void input_records_AtoW( INPUT_RECORD *buffer, int count ) 131{ 132 UINT cp = GetConsoleCP(); 133 int i; 134 WCHAR ch; 135 136 for (i = 0; i < count; i++) 137 { 138 if (buffer[i].EventType != KEY_EVENT) continue; 139 MultiByteToWideChar( cp, 0, &buffer[i].Event.KeyEvent.uChar.AsciiChar, 1, &ch, 1 ); 140 buffer[i].Event.KeyEvent.uChar.UnicodeChar = ch; 141 } 142} 143 144/* map char infos to ASCII */ 145static void char_info_WtoA( UINT cp, CHAR_INFO *buffer, int count ) 146{ 147 char ch; 148 149 while (count-- > 0) 150 { 151 WideCharToMultiByte( cp, 0, &buffer->Char.UnicodeChar, 1, &ch, 1, NULL, NULL ); 152 buffer->Char.AsciiChar = ch; 153 buffer++; 154 } 155} 156 157/* map char infos to Unicode */ 158static void char_info_AtoW( CHAR_INFO *buffer, int count ) 159{ 160 UINT cp = GetConsoleOutputCP(); 161 WCHAR ch; 162 163 while (count-- > 0) 164 { 165 MultiByteToWideChar( cp, 0, &buffer->Char.AsciiChar, 1, &ch, 1 ); 166 buffer->Char.UnicodeChar = ch; 167 buffer++; 168 } 169} 170 171/* helper function for GetLargestConsoleWindowSize */ 172static COORD get_largest_console_window_size( HANDLE handle ) 173{ 174 struct condrv_output_info info; 175 COORD c = { 0, 0 }; 176 177 if (!console_ioctl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &info, sizeof(info), NULL )) 178 return c; 179 180 c.X = info.max_width; 181 c.Y = info.max_height; 182 TRACE( "(%p), returning %dx%d\n", handle, c.X, c.Y ); 183 return c; 184} 185 186/* helper function for GetConsoleFontSize */ 187static COORD get_console_font_size( HANDLE handle, DWORD index ) 188{ 189 struct condrv_output_info info; 190 COORD c = {0,0}; 191 192 if (index >= 1 /* number of console fonts */) 193 { 194 SetLastError(ERROR_INVALID_PARAMETER); 195 return c; 196 } 197 198 if (DeviceIoControl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &info, sizeof(info), NULL, NULL )) 199 { 200 c.X = info.font_width; 201 c.Y = info.font_height; 202 } 203 else SetLastError( ERROR_INVALID_HANDLE ); 204 return c; 205} 206 207/* helper function for GetConsoleTitle and GetConsoleOriginalTitle */ 208static DWORD get_console_title( WCHAR *title, DWORD size, BOOL current_title ) 209{ 210 struct condrv_title_params *params; 211 size_t max_size = sizeof(*params) + (size - 1) * sizeof(WCHAR); 212 213 if (!title || !size) return 0; 214 215 if (!(params = HeapAlloc( GetProcessHeap(), 0, max_size ))) 216 return 0; 217 218 if (console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, IOCTL_CONDRV_GET_TITLE, 219 &current_title, sizeof(current_title), params, max_size, &size ) && 220 size >= sizeof(*params)) 221 { 222 size -= sizeof(*params); 223 memcpy( title, params->buffer, size ); 224 title[ size / sizeof(WCHAR) ] = 0; 225 size = params->title_len; 226 } 227 else size = 0; 228 229 HeapFree( GetProcessHeap(), 0, params ); 230 return size; 231} 232 233static HANDLE create_console_server( void ) 234{ 235 OBJECT_ATTRIBUTES attr = {sizeof(attr)}; 236 UNICODE_STRING string = RTL_CONSTANT_STRING( L"\\Device\\ConDrv\\Server" ); 237 IO_STATUS_BLOCK iosb; 238 HANDLE handle; 239 NTSTATUS status; 240 241 attr.ObjectName = &string; 242 attr.Attributes = OBJ_INHERIT; 243 status = NtCreateFile( &handle, FILE_WRITE_PROPERTIES | FILE_READ_PROPERTIES | SYNCHRONIZE, 244 &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, 245 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 ); 246 return set_ntstatus( status ) ? handle : NULL; 247} 248 249static HANDLE create_console_reference( HANDLE root ) 250{ 251 OBJECT_ATTRIBUTES attr = {sizeof(attr)}; 252 UNICODE_STRING string = RTL_CONSTANT_STRING( L"Reference" ); 253 IO_STATUS_BLOCK iosb; 254 HANDLE handle; 255 NTSTATUS status; 256 257 attr.RootDirectory = root; 258 attr.ObjectName = &string; 259 status = NtCreateFile( &handle, FILE_READ_DATA | FILE_WRITE_DATA | FILE_WRITE_PROPERTIES | 260 FILE_READ_PROPERTIES | SYNCHRONIZE, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 261 0, FILE_OPEN, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 ); 262 return set_ntstatus( status ) ? handle : NULL; 263} 264 265static BOOL create_console_connection( HANDLE root ) 266{ 267 OBJECT_ATTRIBUTES attr = {sizeof(attr)}; 268 UNICODE_STRING string; 269 IO_STATUS_BLOCK iosb; 270 NTSTATUS status; 271 272 RtlInitUnicodeString( &string, root ? L"Connection" : L"\\Device\\ConDrv\\Connection" ); 273 attr.RootDirectory = root; 274 attr.ObjectName = &string; 275 status = NtCreateFile( &console_connection, FILE_WRITE_PROPERTIES | FILE_READ_PROPERTIES | SYNCHRONIZE, &attr, 276 &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_NON_DIRECTORY_FILE, NULL, 0 ); 277 return set_ntstatus( status ); 278} 279 280static BOOL init_console_std_handles( BOOL override_all ) 281{ 282 HANDLE std_out = NULL, std_err = NULL, handle; 283 OBJECT_ATTRIBUTES attr = {sizeof(attr)}; 284 IO_STATUS_BLOCK iosb; 285 UNICODE_STRING name; 286 NTSTATUS status; 287 288 attr.ObjectName = &name; 289 attr.Attributes = OBJ_INHERIT; 290 291 if (override_all || !GetStdHandle( STD_INPUT_HANDLE )) 292 { 293 RtlInitUnicodeString( &name, L"\\Device\\ConDrv\\Input" ); 294 status = NtCreateFile( &handle, FILE_READ_DATA | FILE_WRITE_DATA | SYNCHRONIZE | FILE_READ_ATTRIBUTES | 295 FILE_WRITE_ATTRIBUTES, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 296 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_CREATE, 297 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 ); 298 if (!set_ntstatus( status )) return FALSE; 299 console_flags |= CONSOLE_INPUT_HANDLE; 300 SetStdHandle( STD_INPUT_HANDLE, handle ); 301 } 302 303 if (!override_all) 304 { 305 std_out = GetStdHandle( STD_OUTPUT_HANDLE ); 306 std_err = GetStdHandle( STD_ERROR_HANDLE ); 307 if (std_out && std_err) return TRUE; 308 } 309 310 RtlInitUnicodeString( &name, L"\\Device\\ConDrv\\Output" ); 311 status = NtCreateFile( &handle, FILE_READ_DATA | FILE_WRITE_DATA | SYNCHRONIZE | FILE_READ_ATTRIBUTES | 312 FILE_WRITE_ATTRIBUTES, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 313 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_CREATE, 314 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 ); 315 if (!set_ntstatus( status )) return FALSE; 316 if (!std_out) 317 { 318 console_flags |= CONSOLE_OUTPUT_HANDLE; 319 SetStdHandle( STD_OUTPUT_HANDLE, handle ); 320 } 321 322 if (!std_err) 323 { 324 if (!std_out && !DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(), 325 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS )) 326 return FALSE; 327 console_flags |= CONSOLE_ERROR_HANDLE; 328 SetStdHandle( STD_ERROR_HANDLE, handle ); 329 } 330 331 return TRUE; 332} 333 334 335/****************************************************************** 336 * AddConsoleAliasA (kernelbase.@) 337 */ 338BOOL WINAPI AddConsoleAliasA( LPSTR source, LPSTR target, LPSTR exename ) 339{ 340 FIXME( ": (%s, %s, %s) stub!\n", debugstr_a(source), debugstr_a(target), debugstr_a(exename) ); 341 SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); 342 return FALSE; 343} 344 345 346/****************************************************************** 347 * AddConsoleAliasW (kernelbase.@) 348 */ 349BOOL WINAPI AddConsoleAliasW( LPWSTR source, LPWSTR target, LPWSTR exename ) 350{ 351 FIXME( ": (%s, %s, %s) stub!\n", debugstr_w(source), debugstr_w(target), debugstr_w(exename) ); 352 SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); 353 return FALSE; 354} 355 356 357/****************************************************************** 358 * AttachConsole (kernelbase.@) 359 */ 360BOOL WINAPI DECLSPEC_HOTPATCH AttachConsole( DWORD pid ) 361{ 362 BOOL ret; 363 364 TRACE( "(%lx)\n", pid ); 365 366 RtlEnterCriticalSection( &console_section ); 367 368 if (RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle) 369 { 370 RtlLeaveCriticalSection( &console_section ); 371 WARN( "console already attached\n" ); 372 SetLastError( ERROR_ACCESS_DENIED ); 373 return FALSE; 374 } 375 376 ret = create_console_connection( NULL ) && 377 console_ioctl( console_connection, IOCTL_CONDRV_BIND_PID, &pid, sizeof(pid), NULL, 0, NULL ); 378 if (ret) 379 { 380 RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle = create_console_reference( console_connection ); 381 if (RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle) 382 { 383 STARTUPINFOW si; 384 GetStartupInfoW( &si ); 385 init_console_std_handles( !(si.dwFlags & STARTF_USESTDHANDLES) ); 386 } 387 else ret = FALSE; 388 } 389 390 if (!ret) FreeConsole(); 391 RtlLeaveCriticalSection( &console_section ); 392 return ret; 393} 394 395 396static BOOL alloc_console( BOOL headless ) 397{ 398 SECURITY_ATTRIBUTES inheritable_attr = { sizeof(inheritable_attr), NULL, TRUE }; 399 STARTUPINFOEXW console_si; 400 STARTUPINFOW app_si; 401 HANDLE server, console = NULL; 402 WCHAR buffer[1024], cmd[256], conhost_path[MAX_PATH]; 403 PROCESS_INFORMATION pi; 404 SIZE_T size; 405 void *redir; 406 BOOL ret; 407 408 TRACE("()\n"); 409 410 RtlEnterCriticalSection( &console_section ); 411 412 if (RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle) 413 { 414 /* we already have a console opened on this process, don't create a new one */ 415 RtlLeaveCriticalSection( &console_section ); 416 SetLastError( ERROR_ACCESS_DENIED ); 417 return FALSE; 418 } 419 420 memset( &console_si, 0, sizeof(console_si) ); 421 console_si.StartupInfo.cb = sizeof(console_si); 422 InitializeProcThreadAttributeList( NULL, 1, 0, &size ); 423 if (!(console_si.lpAttributeList = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE; 424 InitializeProcThreadAttributeList( console_si.lpAttributeList, 1, 0, &size ); 425 426 if (!(server = create_console_server()) || !(console = create_console_reference( server ))) goto error; 427 428 GetStartupInfoW(&app_si); 429 430 /* setup a view arguments for conhost (it'll use them as default values) */ 431 if (app_si.dwFlags & STARTF_USECOUNTCHARS) 432 { 433 console_si.StartupInfo.dwFlags |= STARTF_USECOUNTCHARS; 434 console_si.StartupInfo.dwXCountChars = app_si.dwXCountChars; 435 console_si.StartupInfo.dwYCountChars = app_si.dwYCountChars; 436 } 437 if (app_si.dwFlags & STARTF_USEFILLATTRIBUTE) 438 { 439 console_si.StartupInfo.dwFlags |= STARTF_USEFILLATTRIBUTE; 440 console_si.StartupInfo.dwFillAttribute = app_si.dwFillAttribute; 441 } 442 if (app_si.dwFlags & STARTF_USESHOWWINDOW) 443 { 444 console_si.StartupInfo.dwFlags |= STARTF_USESHOWWINDOW; 445 console_si.StartupInfo.wShowWindow = app_si.wShowWindow; 446 } 447 if (app_si.lpTitle) 448 console_si.StartupInfo.lpTitle = app_si.lpTitle; 449 else if (GetModuleFileNameW(0, buffer, ARRAY_SIZE(buffer))) 450 { 451 buffer[ARRAY_SIZE(buffer) - 1] = 0; 452 console_si.StartupInfo.lpTitle = buffer; 453 } 454 455 456 UpdateProcThreadAttribute( console_si.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, 457 &server, sizeof(server), NULL, NULL ); 458 swprintf( conhost_path, ARRAY_SIZE(conhost_path), L"%s\\conhost.exe", system_dir ); 459 swprintf( cmd, ARRAY_SIZE(cmd), L"\"%s\" --server 0x%x", conhost_path, condrv_handle( server )); 460 if (headless) wcscat( cmd, L" --headless" ); 461 Wow64DisableWow64FsRedirection( &redir ); 462 ret = CreateProcessW( conhost_path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS | EXTENDED_STARTUPINFO_PRESENT, 463 NULL, NULL, &console_si.StartupInfo, &pi ); 464 Wow64RevertWow64FsRedirection( redir ); 465 466 if (!ret || !create_console_connection( console)) goto error; 467 if (!init_console_std_handles( !(app_si.dwFlags & STARTF_USESTDHANDLES) )) goto error; 468 469 RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle = console; 470 TRACE( "Started conhost pid=%08lx tid=%08lx\n", pi.dwProcessId, pi.dwThreadId ); 471 472 HeapFree( GetProcessHeap(), 0, console_si.lpAttributeList ); 473 CloseHandle( server ); 474 RtlLeaveCriticalSection( &console_section ); 475 SetLastError( ERROR_SUCCESS ); 476 return TRUE; 477 478error: 479 ERR("Can't allocate console\n"); 480 HeapFree( GetProcessHeap(), 0, console_si.lpAttributeList ); 481 NtClose( console ); 482 NtClose( server ); 483 FreeConsole(); 484 RtlLeaveCriticalSection( &console_section ); 485 return FALSE; 486} 487 488 489/****************************************************************** 490 * AllocConsole (kernelbase.@) 491 */ 492BOOL WINAPI AllocConsole(void) 493{ 494 return alloc_console( FALSE ); 495} 496 497 498/****************************************************************************** 499 * CreateConsoleScreenBuffer (kernelbase.@) 500 */ 501HANDLE WINAPI DECLSPEC_HOTPATCH CreateConsoleScreenBuffer( DWORD access, DWORD share, 502 SECURITY_ATTRIBUTES *sa, DWORD flags, 503 void *data ) 504{ 505 OBJECT_ATTRIBUTES attr = {sizeof(attr)}; 506 IO_STATUS_BLOCK iosb; 507 UNICODE_STRING name = RTL_CONSTANT_STRING( L"\\Device\\ConDrv\\ScreenBuffer" ); 508 HANDLE handle; 509 NTSTATUS status; 510 511 TRACE( "(%lx,%lx,%p,%lx,%p)\n", access, share, sa, flags, data ); 512 513 if (flags != CONSOLE_TEXTMODE_BUFFER || data) 514 { 515 SetLastError( ERROR_INVALID_PARAMETER ); 516 return INVALID_HANDLE_VALUE; 517 } 518 519 attr.ObjectName = &name; 520 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL; 521 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT; 522 status = NtCreateFile( &handle, access, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, 523 FILE_NON_DIRECTORY_FILE, NULL, 0 ); 524 return set_ntstatus( status ) ? handle : INVALID_HANDLE_VALUE; 525} 526 527 528/****************************************************************************** 529 * CtrlRoutine (kernelbase.@) 530 */ 531DWORD WINAPI CtrlRoutine( void *arg ) 532{ 533 DWORD_PTR event = (DWORD_PTR)arg; 534 struct ctrl_handler *handler; 535 536 if (event == CTRL_C_EVENT) 537 { 538 BOOL caught_by_dbg = TRUE; 539 /* First, try to pass the ctrl-C event to the debugger (if any) 540 * If it continues, there's nothing more to do 541 * Otherwise, we need to send the ctrl-C event to the handlers 542 */ 543 __TRY 544 { 545 RaiseException( DBG_CONTROL_C, 0, 0, NULL ); 546 } 547 __EXCEPT_ALL 548 { 549 caught_by_dbg = FALSE; 550 } 551 __ENDTRY 552 if (caught_by_dbg) return 0; 553 } 554 555 if (NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags & 1) return 0; 556 557 RtlEnterCriticalSection( &console_section ); 558 for (handler = ctrl_handlers; handler; handler = handler->next) 559 { 560 if (handler->func( event )) break; 561 } 562 RtlLeaveCriticalSection( &console_section ); 563 return 1; 564} 565 566 567/****************************************************************** 568 * ExpungeConsoleCommandHistoryA (kernelbase.@) 569 */ 570void WINAPI ExpungeConsoleCommandHistoryA( LPCSTR exename ) 571{ 572 FIXME( ": (%s) stub!\n", debugstr_a(exename) ); 573 SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); 574} 575 576 577/****************************************************************** 578 * ExpungeConsoleCommandHistoryW (kernelbase.@) 579 */ 580void WINAPI ExpungeConsoleCommandHistoryW( LPCWSTR exename ) 581{ 582 FIXME( ": (%s) stub!\n", debugstr_w(exename) ); 583 SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); 584} 585 586 587/****************************************************************************** 588 * FillConsoleOutputAttribute (kernelbase.@) 589 */ 590BOOL WINAPI DECLSPEC_HOTPATCH FillConsoleOutputAttribute( HANDLE handle, WORD attr, DWORD length, 591 COORD coord, DWORD *written ) 592{ 593 struct condrv_fill_output_params params; 594 595 TRACE( "(%p,%d,%ld,(%dx%d),%p)\n", handle, attr, length, coord.X, coord.Y, written ); 596 597 if (!written) 598 { 599 SetLastError( ERROR_INVALID_ACCESS ); 600 return FALSE; 601 } 602 603 *written = 0; 604 605 params.mode = CHAR_INFO_MODE_ATTR; 606 params.x = coord.X; 607 params.y = coord.Y; 608 params.count = length; 609 params.wrap = TRUE; 610 params.ch = 0; 611 params.attr = attr; 612 return console_ioctl( handle, IOCTL_CONDRV_FILL_OUTPUT, &params, sizeof(params), 613 written, sizeof(*written), NULL ); 614} 615 616 617/****************************************************************************** 618 * FillConsoleOutputCharacterA (kernelbase.@) 619 */ 620BOOL WINAPI DECLSPEC_HOTPATCH FillConsoleOutputCharacterA( HANDLE handle, CHAR ch, DWORD length, 621 COORD coord, DWORD *written ) 622{ 623 WCHAR wch; 624 625 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch, 1, &wch, 1 ); 626 return FillConsoleOutputCharacterW( handle, wch, length, coord, written ); 627} 628 629 630/****************************************************************************** 631 * FillConsoleOutputCharacterW (kernelbase.@) 632 */ 633BOOL WINAPI DECLSPEC_HOTPATCH FillConsoleOutputCharacterW( HANDLE handle, WCHAR ch, DWORD length, 634 COORD coord, DWORD *written ) 635{ 636 struct condrv_fill_output_params params; 637 638 TRACE( "(%p,%s,%ld,(%dx%d),%p)\n", handle, debugstr_wn(&ch, 1), length, coord.X, coord.Y, written ); 639 640 if (!written) 641 { 642 SetLastError( ERROR_INVALID_ACCESS ); 643 return FALSE; 644 } 645 646 *written = 0; 647 648 params.mode = CHAR_INFO_MODE_TEXT; 649 params.x = coord.X; 650 params.y = coord.Y; 651 params.count = length; 652 params.wrap = TRUE; 653 params.ch = ch; 654 params.attr = 0; 655 return console_ioctl( handle, IOCTL_CONDRV_FILL_OUTPUT, &params, sizeof(params), 656 written, sizeof(*written), NULL ); 657} 658 659 660/*********************************************************************** 661 * FreeConsole (kernelbase.@) 662 */ 663BOOL WINAPI DECLSPEC_HOTPATCH FreeConsole(void) 664{ 665 RtlEnterCriticalSection( &console_section ); 666 667 if (RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle != CONSOLE_HANDLE_SHELL_NO_WINDOW) 668 { 669 NtClose( console_connection ); 670 console_connection = NULL; 671 672 NtClose( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle ); 673 } 674 RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle = NULL; 675 676 if (console_flags & CONSOLE_INPUT_HANDLE) NtClose( GetStdHandle( STD_INPUT_HANDLE )); 677 if (console_flags & CONSOLE_OUTPUT_HANDLE) NtClose( GetStdHandle( STD_OUTPUT_HANDLE )); 678 if (console_flags & CONSOLE_ERROR_HANDLE) NtClose( GetStdHandle( STD_ERROR_HANDLE )); 679 console_flags = 0; 680 681 RtlLeaveCriticalSection( &console_section ); 682 return TRUE; 683} 684 685 686/****************************************************************************** 687 * GenerateConsoleCtrlEvent (kernelbase.@) 688 */ 689BOOL WINAPI DECLSPEC_HOTPATCH GenerateConsoleCtrlEvent( DWORD event, DWORD group ) 690{ 691 struct condrv_ctrl_event ctrl_event; 692 693 TRACE( "(%ld, %lx)\n", event, group ); 694 695 if (event != CTRL_C_EVENT && event != CTRL_BREAK_EVENT) 696 { 697 ERR( "Invalid event %ld for PGID %lx\n", event, group ); 698 return FALSE; 699 } 700 701 ctrl_event.event = event; 702 ctrl_event.group_id = group; 703 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, 704 IOCTL_CONDRV_CTRL_EVENT, &ctrl_event, sizeof(ctrl_event), NULL, 0, NULL ); 705} 706 707 708/****************************************************************** 709 * GetConsoleAliasA (kernelbase.@) 710 */ 711DWORD WINAPI GetConsoleAliasA( LPSTR source, LPSTR buffer, DWORD len, LPSTR exename ) 712{ 713 FIXME( "(%s,%p,%ld,%s): stub\n", debugstr_a(source), buffer, len, debugstr_a(exename) ); 714 SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); 715 return 0; 716} 717 718 719/****************************************************************** 720 * GetConsoleAliasW (kernelbase.@) 721 */ 722DWORD WINAPI GetConsoleAliasW( LPWSTR source, LPWSTR buffer, DWORD len, LPWSTR exename ) 723{ 724 FIXME( "(%s,%p,%ld,%s): stub\n", debugstr_w(source), buffer, len, debugstr_w(exename) ); 725 SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); 726 return 0; 727} 728 729 730/****************************************************************** 731 * GetConsoleAliasExesLengthA (kernelbase.@) 732 */ 733DWORD WINAPI GetConsoleAliasExesLengthA(void) 734{ 735 FIXME( ": stub\n" ); 736 SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); 737 return 0; 738} 739 740 741/****************************************************************** 742 * GetConsoleAliasExesLengthW (kernelbase.@) 743 */ 744DWORD WINAPI GetConsoleAliasExesLengthW(void) 745{ 746 FIXME( ": stub\n" ); 747 SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); 748 return 0; 749} 750 751 752/****************************************************************** 753 * GetConsoleAliasesLengthA (kernelbase.@) 754 */ 755DWORD WINAPI GetConsoleAliasesLengthA( LPSTR unknown ) 756{ 757 FIXME( ": (%s) stub!\n", debugstr_a(unknown) ); 758 SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); 759 return 0; 760} 761 762 763/****************************************************************** 764 * GetConsoleAliasesLengthW (kernelbase.@) 765 */ 766DWORD WINAPI GetConsoleAliasesLengthW( LPWSTR unknown ) 767{ 768 FIXME( ": (%s) stub!\n", debugstr_w(unknown) ); 769 SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); 770 return 0; 771} 772 773 774/****************************************************************** 775 * GetConsoleCommandHistoryA (kernelbase.@) 776 */ 777DWORD WINAPI GetConsoleCommandHistoryA( LPSTR buffer, DWORD len, LPCSTR exename ) 778{ 779 FIXME( ": (%p, 0x%lx, %s) stub\n", buffer, len, debugstr_a(exename) ); 780 SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); 781 return 0; 782} 783 784 785/****************************************************************** 786 * GetConsoleCommandHistoryW (kernelbase.@) 787 */ 788DWORD WINAPI GetConsoleCommandHistoryW( LPWSTR buffer, DWORD len, LPCWSTR exename ) 789{ 790 FIXME( ": (%p, 0x%lx, %s) stub\n", buffer, len, debugstr_w(exename) ); 791 SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); 792 return 0; 793} 794 795 796/****************************************************************** 797 * GetConsoleCommandHistoryLengthA (kernelbase.@) 798 */ 799DWORD WINAPI GetConsoleCommandHistoryLengthA( LPCSTR exename ) 800{ 801 FIXME( ": (%s) stub!\n", debugstr_a(exename) ); 802 SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); 803 return 0; 804} 805 806 807/****************************************************************** 808 * GetConsoleCommandHistoryLengthW (kernelbase.@) 809 */ 810DWORD WINAPI GetConsoleCommandHistoryLengthW( LPCWSTR exename ) 811{ 812 FIXME( ": (%s) stub!\n", debugstr_w(exename) ); 813 SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); 814 return 0; 815} 816 817 818/****************************************************************************** 819 * GetConsoleCP (kernelbase.@) 820 */ 821UINT WINAPI DECLSPEC_HOTPATCH GetConsoleCP(void) 822{ 823 struct condrv_input_info info; 824 825 if (!console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, 826 IOCTL_CONDRV_GET_INPUT_INFO, NULL, 0, &info, sizeof(info), NULL )) 827 return 0; 828 return info.input_cp; 829} 830 831 832/****************************************************************************** 833 * GetConsoleCursorInfo (kernelbase.@) 834 */ 835BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleCursorInfo( HANDLE handle, CONSOLE_CURSOR_INFO *info ) 836{ 837 struct condrv_output_info condrv_info; 838 839 if (!console_ioctl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &condrv_info, sizeof(condrv_info), NULL )) 840 return FALSE; 841 842 if (!info) 843 { 844 SetLastError( ERROR_INVALID_ACCESS ); 845 return FALSE; 846 } 847 848 info->dwSize = condrv_info.cursor_size; 849 info->bVisible = condrv_info.cursor_visible; 850 TRACE("(%p) returning (%ld,%d)\n", handle, info->dwSize, info->bVisible); 851 return TRUE; 852} 853 854 855/*********************************************************************** 856 * GetConsoleDisplayMode (kernelbase.@) 857 */ 858BOOL WINAPI GetConsoleDisplayMode( LPDWORD flags ) 859{ 860 TRACE( "semi-stub: %p\n", flags ); 861 /* It is safe to successfully report windowed mode */ 862 *flags = 0; 863 return TRUE; 864} 865 866 867/*********************************************************************** 868 * GetConsoleFontSize (kernelbase.@) 869 */ 870#if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER) 871#undef GetConsoleFontSize 872DWORD WINAPI GetConsoleFontSize( HANDLE handle, DWORD index ) 873{ 874 union { 875 COORD c; 876 DWORD w; 877 } x; 878 879 x.c = get_console_font_size( handle, index ); 880 return x.w; 881} 882#else 883COORD WINAPI GetConsoleFontSize( HANDLE handle, DWORD index ) 884{ 885 return get_console_font_size( handle, index ); 886} 887#endif /* !defined(__i386__) */ 888 889 890/*********************************************************************** 891 * GetConsoleInputExeNameA (kernelbase.@) 892 */ 893BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleInputExeNameA( DWORD len, LPSTR buffer ) 894{ 895 RtlEnterCriticalSection( &console_section ); 896 if (WideCharToMultiByte( CP_ACP, 0, input_exe, -1, NULL, 0, NULL, NULL ) <= len) 897 WideCharToMultiByte( CP_ACP, 0, input_exe, -1, buffer, len, NULL, NULL ); 898 else SetLastError(ERROR_BUFFER_OVERFLOW); 899 RtlLeaveCriticalSection( &console_section ); 900 return TRUE; 901} 902 903 904/*********************************************************************** 905 * GetConsoleInputExeNameW (kernelbase.@) 906 */ 907BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleInputExeNameW( DWORD len, LPWSTR buffer ) 908{ 909 RtlEnterCriticalSection( &console_section ); 910 if (len > lstrlenW(input_exe)) lstrcpyW( buffer, input_exe ); 911 else SetLastError( ERROR_BUFFER_OVERFLOW ); 912 RtlLeaveCriticalSection( &console_section ); 913 return TRUE; 914} 915 916 917/*********************************************************************** 918 * GetConsoleMode (kernelbase.@) 919 */ 920BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleMode( HANDLE handle, DWORD *mode ) 921{ 922 return console_ioctl( handle, IOCTL_CONDRV_GET_MODE, NULL, 0, mode, sizeof(*mode), NULL ); 923} 924 925 926/*********************************************************************** 927 * GetConsoleOriginalTitleA (kernelbase.@) 928 */ 929DWORD WINAPI DECLSPEC_HOTPATCH GetConsoleOriginalTitleA( LPSTR title, DWORD size ) 930{ 931 WCHAR *ptr = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ); 932 DWORD ret; 933 934 if (!ptr) return 0; 935 936 ret = GetConsoleOriginalTitleW( ptr, size ); 937 if (ret) 938 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, -1, title, size, NULL, NULL); 939 940 HeapFree( GetProcessHeap(), 0, ptr ); 941 return ret; 942} 943 944 945/*********************************************************************** 946 * GetConsoleOriginalTitleW (kernelbase.@) 947 */ 948DWORD WINAPI DECLSPEC_HOTPATCH GetConsoleOriginalTitleW( LPWSTR title, DWORD size ) 949{ 950 return get_console_title( title, size, FALSE ); 951} 952 953 954/*********************************************************************** 955 * GetConsoleOutputCP (kernelbase.@) 956 */ 957UINT WINAPI DECLSPEC_HOTPATCH GetConsoleOutputCP(void) 958{ 959 struct condrv_input_info info; 960 961 if (!console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, 962 IOCTL_CONDRV_GET_INPUT_INFO, NULL, 0, &info, sizeof(info), NULL )) 963 return 0; 964 return info.output_cp; 965} 966 967 968/*********************************************************************** 969 * GetConsoleProcessList (kernelbase.@) 970 */ 971DWORD WINAPI GetConsoleProcessList( DWORD *list, DWORD count ) 972{ 973 DWORD saved; 974 NTSTATUS status; 975 IO_STATUS_BLOCK io; 976 977 TRACE( "(%p,%ld)\n", list, count); 978 979 if (!list || count < 1) 980 { 981 SetLastError(ERROR_INVALID_PARAMETER); 982 return 0; 983 } 984 985 saved = *list; 986 status = NtDeviceIoControlFile( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, 987 NULL, NULL, NULL, &io, IOCTL_CONDRV_GET_PROCESS_LIST, 988 NULL, 0, list, count * sizeof(DWORD) ); 989 990 if (!status) return io.Information / sizeof(DWORD); 991 if (status == STATUS_BUFFER_TOO_SMALL) 992 { 993 DWORD ret = *list; 994 *list = saved; 995 return ret; 996 } 997 998 *list = saved; 999 set_ntstatus( status ); 1000 return 0; 1001} 1002 1003 1004/*********************************************************************** 1005 * GetConsoleScreenBufferInfo (kernelbase.@) 1006 */ 1007BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleScreenBufferInfo( HANDLE handle, CONSOLE_SCREEN_BUFFER_INFO *info ) 1008{ 1009 struct condrv_output_info condrv_info; 1010 1011 if (!console_ioctl( handle , IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, 1012 &condrv_info, sizeof(condrv_info), NULL )) 1013 return FALSE; 1014 1015 info->dwSize.X = condrv_info.width; 1016 info->dwSize.Y = condrv_info.height; 1017 info->dwCursorPosition.X = condrv_info.cursor_x; 1018 info->dwCursorPosition.Y = condrv_info.cursor_y; 1019 info->wAttributes = condrv_info.attr; 1020 info->srWindow.Left = condrv_info.win_left; 1021 info->srWindow.Right = condrv_info.win_right; 1022 info->srWindow.Top = condrv_info.win_top; 1023 info->srWindow.Bottom = condrv_info.win_bottom; 1024 info->dwMaximumWindowSize.X = min(condrv_info.width, condrv_info.max_width); 1025 info->dwMaximumWindowSize.Y = min(condrv_info.height, condrv_info.max_height); 1026 1027 TRACE( "(%p,(%d,%d) (%d,%d) %d (%d,%d-%d,%d) (%d,%d)\n", handle, 1028 info->dwSize.X, info->dwSize.Y, info->dwCursorPosition.X, info->dwCursorPosition.Y, 1029 info->wAttributes, info->srWindow.Left, info->srWindow.Top, info->srWindow.Right, 1030 info->srWindow.Bottom, info->dwMaximumWindowSize.X, info->dwMaximumWindowSize.Y ); 1031 return TRUE; 1032} 1033 1034 1035/*********************************************************************** 1036 * GetConsoleScreenBufferInfoEx (kernelbase.@) 1037 */ 1038BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleScreenBufferInfoEx( HANDLE handle, 1039 CONSOLE_SCREEN_BUFFER_INFOEX *info ) 1040{ 1041 struct condrv_output_info condrv_info; 1042 1043 if (info->cbSize != sizeof(CONSOLE_SCREEN_BUFFER_INFOEX)) 1044 { 1045 SetLastError( ERROR_INVALID_PARAMETER ); 1046 return FALSE; 1047 } 1048 1049 if (!console_ioctl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &condrv_info, 1050 sizeof(condrv_info), NULL )) 1051 return FALSE; 1052 1053 info->dwSize.X = condrv_info.width; 1054 info->dwSize.Y = condrv_info.height; 1055 info->dwCursorPosition.X = condrv_info.cursor_x; 1056 info->dwCursorPosition.Y = condrv_info.cursor_y; 1057 info->wAttributes = condrv_info.attr; 1058 info->srWindow.Left = condrv_info.win_left; 1059 info->srWindow.Top = condrv_info.win_top; 1060 info->srWindow.Right = condrv_info.win_right; 1061 info->srWindow.Bottom = condrv_info.win_bottom; 1062 info->dwMaximumWindowSize.X = min( condrv_info.width, condrv_info.max_width ); 1063 info->dwMaximumWindowSize.Y = min( condrv_info.height, condrv_info.max_height ); 1064 info->wPopupAttributes = condrv_info.popup_attr; 1065 info->bFullscreenSupported = FALSE; 1066 memcpy( info->ColorTable, condrv_info.color_map, sizeof(info->ColorTable) ); 1067 return TRUE; 1068} 1069 1070 1071/****************************************************************************** 1072 * GetConsoleTitleA (kernelbase.@) 1073 */ 1074DWORD WINAPI DECLSPEC_HOTPATCH GetConsoleTitleA( LPSTR title, DWORD size ) 1075{ 1076 WCHAR *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR) * size ); 1077 DWORD ret; 1078 1079 if (!ptr) return 0; 1080 1081 ret = GetConsoleTitleW( ptr, size ); 1082 if (ret) 1083 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, -1, title, size, NULL, NULL); 1084 1085 HeapFree( GetProcessHeap(), 0, ptr ); 1086 return ret; 1087} 1088 1089 1090/****************************************************************************** 1091 * GetConsoleTitleW (kernelbase.@) 1092 */ 1093DWORD WINAPI DECLSPEC_HOTPATCH GetConsoleTitleW( LPWSTR title, DWORD size ) 1094{ 1095 return get_console_title( title, size, TRUE ); 1096} 1097 1098 1099/****************************************************************************** 1100 * GetConsoleWindow (kernelbase.@) 1101 */ 1102HWND WINAPI GetConsoleWindow(void) 1103{ 1104 condrv_handle_t win; 1105 BOOL ret; 1106 1107 ret = DeviceIoControl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, 1108 IOCTL_CONDRV_GET_WINDOW, NULL, 0, &win, sizeof(win), NULL, NULL ); 1109 return ret ? LongToHandle( win ) : NULL; 1110} 1111 1112 1113/*********************************************************************** 1114 * GetCurrentConsoleFontEx (kernelbase.@) 1115 */ 1116BOOL WINAPI GetCurrentConsoleFontEx( HANDLE handle, BOOL maxwindow, CONSOLE_FONT_INFOEX *info ) 1117{ 1118 DWORD size; 1119 struct 1120 { 1121 struct condrv_output_info info; 1122 WCHAR face_name[LF_FACESIZE - 1]; 1123 } data; 1124 1125 if (info->cbSize != sizeof(CONSOLE_FONT_INFOEX)) 1126 { 1127 SetLastError(ERROR_INVALID_PARAMETER); 1128 return FALSE; 1129 } 1130 1131 if (!DeviceIoControl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, 1132 &data, sizeof(data), &size, NULL )) 1133 { 1134 SetLastError( ERROR_INVALID_HANDLE ); 1135 return FALSE; 1136 } 1137 1138 info->nFont = 0; 1139 if (maxwindow) 1140 { 1141 info->dwFontSize.X = min( data.info.width, data.info.max_width ); 1142 info->dwFontSize.Y = min( data.info.height, data.info.max_height ); 1143 } 1144 else 1145 { 1146 info->dwFontSize.X = data.info.font_width; 1147 info->dwFontSize.Y = data.info.font_height; 1148 } 1149 size -= sizeof(data.info); 1150 if (size) memcpy( info->FaceName, data.face_name, size ); 1151 info->FaceName[size / sizeof(WCHAR)] = 0; 1152 info->FontFamily = data.info.font_pitch_family; 1153 info->FontWeight = data.info.font_weight; 1154 return TRUE; 1155} 1156 1157 1158/*********************************************************************** 1159 * GetCurrentConsoleFont (kernelbase.@) 1160 */ 1161BOOL WINAPI GetCurrentConsoleFont( HANDLE handle, BOOL maxwindow, CONSOLE_FONT_INFO *info ) 1162{ 1163 BOOL ret; 1164 CONSOLE_FONT_INFOEX res; 1165 1166 res.cbSize = sizeof(CONSOLE_FONT_INFOEX); 1167 1168 ret = GetCurrentConsoleFontEx( handle, maxwindow, &res ); 1169 if (ret) 1170 { 1171 info->nFont = res.nFont; 1172 info->dwFontSize.X = res.dwFontSize.X; 1173 info->dwFontSize.Y = res.dwFontSize.Y; 1174 } 1175 return ret; 1176} 1177 1178 1179/*********************************************************************** 1180 * GetLargestConsoleWindowSize (kernelbase.@) 1181 */ 1182#if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER) 1183#undef GetLargestConsoleWindowSize 1184DWORD WINAPI DECLSPEC_HOTPATCH GetLargestConsoleWindowSize( HANDLE handle ) 1185{ 1186 union { 1187 COORD c; 1188 DWORD w; 1189 } x; 1190 x.c = get_largest_console_window_size( handle ); 1191 return x.w; 1192} 1193 1194#else 1195 1196COORD WINAPI DECLSPEC_HOTPATCH GetLargestConsoleWindowSize( HANDLE handle ) 1197{ 1198 return get_largest_console_window_size( handle ); 1199} 1200 1201#endif /* !defined(__i386__) */ 1202 1203 1204/*********************************************************************** 1205 * GetNumberOfConsoleInputEvents (kernelbase.@) 1206 */ 1207BOOL WINAPI DECLSPEC_HOTPATCH GetNumberOfConsoleInputEvents( HANDLE handle, DWORD *count ) 1208{ 1209 return console_ioctl( handle, IOCTL_CONDRV_GET_INPUT_COUNT, NULL, 0, 1210 count, sizeof(*count), NULL ); 1211} 1212 1213 1214/*********************************************************************** 1215 * GetNumberOfConsoleMouseButtons (kernelbase.@) 1216 */ 1217BOOL WINAPI GetNumberOfConsoleMouseButtons( DWORD *count ) 1218{ 1219 FIXME( "(%p): stub\n", count ); 1220 *count = 2; 1221 return TRUE; 1222} 1223 1224 1225/*********************************************************************** 1226 * PeekConsoleInputA (kernelbase.@) 1227 */ 1228BOOL WINAPI DECLSPEC_HOTPATCH PeekConsoleInputA( HANDLE handle, INPUT_RECORD *buffer, 1229 DWORD length, DWORD *count ) 1230{ 1231 DWORD read; 1232 1233 if (!PeekConsoleInputW( handle, buffer, length, &read )) return FALSE; 1234 input_records_WtoA( buffer, read ); 1235 if (count) *count = read; 1236 return TRUE; 1237} 1238 1239 1240/*********************************************************************** 1241 * PeekConsoleInputW (kernelbase.@) 1242 */ 1243BOOL WINAPI DECLSPEC_HOTPATCH PeekConsoleInputW( HANDLE handle, INPUT_RECORD *buffer, 1244 DWORD length, DWORD *count ) 1245{ 1246 DWORD read; 1247 if (!console_ioctl( handle, IOCTL_CONDRV_PEEK, NULL, 0, buffer, length * sizeof(*buffer), &read )) 1248 return FALSE; 1249 if (count) *count = read / sizeof(*buffer); 1250 return TRUE; 1251} 1252 1253 1254/****************************************************************************** 1255 * ReadConsoleOutputAttribute (kernelbase.@) 1256 */ 1257BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputAttribute( HANDLE handle, WORD *attr, DWORD length, 1258 COORD coord, DWORD *count ) 1259{ 1260 struct condrv_output_params params; 1261 BOOL ret; 1262 1263 TRACE( "(%p,%p,%ld,%dx%d,%p)\n", handle, attr, length, coord.X, coord.Y, count ); 1264 1265 if (!count) 1266 { 1267 SetLastError( ERROR_INVALID_ACCESS ); 1268 return FALSE; 1269 } 1270 1271 params.mode = CHAR_INFO_MODE_ATTR; 1272 params.x = coord.X; 1273 params.y = coord.Y; 1274 params.width = 0; 1275 ret = console_ioctl( handle, IOCTL_CONDRV_READ_OUTPUT, &params, sizeof(params), 1276 attr, length * sizeof(*attr), count ); 1277 *count /= sizeof(*attr); 1278 return ret; 1279} 1280 1281 1282/****************************************************************************** 1283 * ReadConsoleOutputCharacterA (kernelbase.@) 1284 */ 1285BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputCharacterA( HANDLE handle, LPSTR buffer, DWORD length, 1286 COORD coord, DWORD *count ) 1287{ 1288 DWORD read; 1289 BOOL ret; 1290 LPWSTR wptr; 1291 1292 if (!count) 1293 { 1294 SetLastError( ERROR_INVALID_ACCESS ); 1295 return FALSE; 1296 } 1297 1298 *count = 0; 1299 if (!(wptr = HeapAlloc( GetProcessHeap(), 0, length * sizeof(WCHAR) ))) 1300 { 1301 SetLastError( ERROR_NOT_ENOUGH_MEMORY ); 1302 return FALSE; 1303 } 1304 if ((ret = ReadConsoleOutputCharacterW( handle, wptr, length, coord, &read ))) 1305 { 1306 read = WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr, read, buffer, length, NULL, NULL); 1307 *count = read; 1308 } 1309 HeapFree( GetProcessHeap(), 0, wptr ); 1310 return ret; 1311} 1312 1313 1314/****************************************************************************** 1315 * ReadConsoleOutputCharacterW (kernelbase.@) 1316 */ 1317BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputCharacterW( HANDLE handle, LPWSTR buffer, DWORD length, 1318 COORD coord, DWORD *count ) 1319{ 1320 struct condrv_output_params params; 1321 BOOL ret; 1322 1323 TRACE( "(%p,%p,%ld,%dx%d,%p)\n", handle, buffer, length, coord.X, coord.Y, count ); 1324 1325 if (!count) 1326 { 1327 SetLastError( ERROR_INVALID_ACCESS ); 1328 return FALSE; 1329 } 1330 1331 params.mode = CHAR_INFO_MODE_TEXT; 1332 params.x = coord.X; 1333 params.y = coord.Y; 1334 params.width = 0; 1335 ret = console_ioctl( handle, IOCTL_CONDRV_READ_OUTPUT, &params, sizeof(params), buffer, 1336 length * sizeof(*buffer), count ); 1337 *count /= sizeof(*buffer); 1338 return ret; 1339} 1340 1341 1342/****************************************************************************** 1343 * ReadConsoleOutputA (kernelbase.@) 1344 */ 1345BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputA( HANDLE handle, CHAR_INFO *buffer, COORD size, 1346 COORD coord, SMALL_RECT *region ) 1347{ 1348 BOOL ret; 1349 int y; 1350 1351 ret = ReadConsoleOutputW( handle, buffer, size, coord, region ); 1352 if (ret && region->Right >= region->Left) 1353 { 1354 UINT cp = GetConsoleOutputCP(); 1355 for (y = 0; y <= region->Bottom - region->Top; y++) 1356 char_info_WtoA( cp, &buffer[(coord.Y + y) * size.X + coord.X], region->Right - region->Left + 1 ); 1357 } 1358 return ret; 1359} 1360 1361 1362/****************************************************************************** 1363 * ReadConsoleOutputW (kernelbase.@) 1364 */ 1365BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputW( HANDLE handle, CHAR_INFO *buffer, COORD size, 1366 COORD coord, SMALL_RECT *region ) 1367{ 1368 struct condrv_output_params params; 1369 unsigned int width, height, y; 1370 SMALL_RECT *result; 1371 DWORD count; 1372 BOOL ret; 1373 1374 if (region->Left > region->Right || region->Top > region->Bottom) 1375 { 1376 SetLastError( ERROR_NOT_ENOUGH_MEMORY ); 1377 return FALSE; 1378 } 1379 if (size.X <= coord.X || size.Y <= coord.Y) 1380 { 1381 region->Right = region->Left - 1; 1382 region->Bottom = region->Top - 1; 1383 SetLastError( ERROR_INVALID_FUNCTION ); 1384 return FALSE; 1385 } 1386 width = min( region->Right - region->Left + 1, size.X - coord.X ); 1387 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y ); 1388 region->Right = region->Left + width - 1; 1389 region->Bottom = region->Top + height - 1; 1390 1391 count = sizeof(*result) + width * height * sizeof(*buffer); 1392 if (!(result = HeapAlloc( GetProcessHeap(), 0, count ))) 1393 { 1394 SetLastError( ERROR_NOT_ENOUGH_MEMORY ); 1395 return FALSE; 1396 } 1397 1398 params.mode = CHAR_INFO_MODE_TEXTATTR; 1399 params.x = region->Left; 1400 params.y = region->Top; 1401 params.width = width; 1402 if ((ret = console_ioctl( handle, IOCTL_CONDRV_READ_OUTPUT, &params, sizeof(params), result, count, &count )) && count) 1403 { 1404 CHAR_INFO *char_info = (CHAR_INFO *)(result + 1); 1405 *region = *result; 1406 width = region->Right - region->Left + 1; 1407 height = region->Bottom - region->Top + 1; 1408 for (y = 0; y < height; y++) 1409 memcpy( &buffer[(y + coord.Y) * size.X + coord.X], &char_info[y * width], width * sizeof(*buffer) ); 1410 } 1411 HeapFree( GetProcessHeap(), 0, result ); 1412 return ret; 1413} 1414 1415 1416/****************************************************************************** 1417 * ScrollConsoleScreenBufferA (kernelbase.@) 1418 */ 1419BOOL WINAPI DECLSPEC_HOTPATCH ScrollConsoleScreenBufferA( HANDLE handle, const SMALL_RECT *scroll, 1420 const SMALL_RECT *clip, COORD origin, const CHAR_INFO *fill ) 1421{ 1422 CHAR_INFO ciW; 1423 1424 ciW.Attributes = fill->Attributes; 1425 MultiByteToWideChar( GetConsoleOutputCP(), 0, &fill->Char.AsciiChar, 1, &ciW.Char.UnicodeChar, 1 ); 1426 1427 return ScrollConsoleScreenBufferW( handle, scroll, clip, origin, &ciW ); 1428} 1429 1430 1431/****************************************************************************** 1432 * ScrollConsoleScreenBufferW (kernelbase.@) 1433 */ 1434BOOL WINAPI DECLSPEC_HOTPATCH ScrollConsoleScreenBufferW( HANDLE handle, const SMALL_RECT *scroll, 1435 const SMALL_RECT *clip_rect, COORD origin, 1436 const CHAR_INFO *fill ) 1437{ 1438 struct condrv_scroll_params params; 1439 1440 if (clip_rect) 1441 TRACE( "(%p,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", handle, 1442 scroll->Left, scroll->Top, scroll->Right, scroll->Bottom, 1443 clip_rect->Left, clip_rect->Top, clip_rect->Right, clip_rect->Bottom, 1444 origin.X, origin.Y, fill ); 1445 else 1446 TRACE("(%p,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", handle, 1447 scroll->Left, scroll->Top, scroll->Right, scroll->Bottom, 1448 origin.X, origin.Y, fill ); 1449 1450 params.scroll = *scroll; 1451 params.origin = origin; 1452 params.fill.ch = fill->Char.UnicodeChar; 1453 params.fill.attr = fill->Attributes; 1454 if (!clip_rect) 1455 { 1456 params.clip.Left = params.clip.Top = 0; 1457 params.clip.Right = params.clip.Bottom = SHRT_MAX; 1458 } 1459 else params.clip = *clip_rect; 1460 return console_ioctl( handle, IOCTL_CONDRV_SCROLL, (void *)&params, sizeof(params), NULL, 0, NULL ); 1461} 1462 1463 1464/****************************************************************************** 1465 * SetConsoleActiveScreenBuffer (kernelbase.@) 1466 */ 1467BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleActiveScreenBuffer( HANDLE handle ) 1468{ 1469 TRACE( "(%p)\n", handle ); 1470 return console_ioctl( handle, IOCTL_CONDRV_ACTIVATE, NULL, 0, NULL, 0, NULL ); 1471} 1472 1473 1474/****************************************************************************** 1475 * SetConsoleCP (kernelbase.@) 1476 */ 1477BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCP( UINT cp ) 1478{ 1479 struct condrv_input_info_params params = { SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE }; 1480 1481 params.info.input_cp = cp; 1482 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, 1483 IOCTL_CONDRV_SET_INPUT_INFO, &params, sizeof(params), NULL, 0, NULL ); 1484} 1485 1486 1487/****************************************************************************** 1488 * SetConsoleCtrlHandler (kernelbase.@) 1489 */ 1490BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCtrlHandler( PHANDLER_ROUTINE func, BOOL add ) 1491{ 1492 struct ctrl_handler *handler; 1493 BOOL ret = FALSE; 1494 1495 TRACE( "(%p,%d)\n", func, add ); 1496 1497 RtlEnterCriticalSection( &console_section ); 1498 1499 if (!func) 1500 { 1501 if (add) NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags |= 1; 1502 else NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags &= ~1; 1503 ret = TRUE; 1504 } 1505 else if (add) 1506 { 1507 if ((handler = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler) ))) 1508 { 1509 handler->func = func; 1510 handler->next = ctrl_handlers; 1511 ctrl_handlers = handler; 1512 ret = TRUE; 1513 } 1514 } 1515 else 1516 { 1517 struct ctrl_handler **p_handler; 1518 1519 for (p_handler = &ctrl_handlers; *p_handler; p_handler = &(*p_handler)->next) 1520 { 1521 if ((*p_handler)->func == func) break; 1522 } 1523 if (*p_handler && *p_handler != &default_handler) 1524 { 1525 handler = *p_handler; 1526 *p_handler = handler->next; 1527 RtlFreeHeap( GetProcessHeap(), 0, handler ); 1528 ret = TRUE; 1529 } 1530 else SetLastError( ERROR_INVALID_PARAMETER ); 1531 } 1532 1533 RtlLeaveCriticalSection( &console_section ); 1534 return ret; 1535} 1536 1537 1538/****************************************************************************** 1539 * SetConsoleCursorInfo (kernelbase.@) 1540 */ 1541BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCursorInfo( HANDLE handle, CONSOLE_CURSOR_INFO *info ) 1542{ 1543 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM }; 1544 1545 TRACE( "(%p,%ld,%d)\n", handle, info->dwSize, info->bVisible); 1546 1547 params.info.cursor_size = info->dwSize; 1548 params.info.cursor_visible = info->bVisible; 1549 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), 1550 NULL, 0, NULL ); 1551} 1552 1553 1554/****************************************************************************** 1555 * SetConsoleCursorPosition (kernelbase.@) 1556 */ 1557BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCursorPosition( HANDLE handle, COORD pos ) 1558{ 1559 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_CURSOR_POS }; 1560 1561 TRACE( "%p %d %d\n", handle, pos.X, pos.Y ); 1562 1563 params.info.cursor_x = pos.X; 1564 params.info.cursor_y = pos.Y; 1565 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL ); 1566} 1567 1568 1569/*********************************************************************** 1570 * SetConsoleDisplayMode (kernelbase.@) 1571 */ 1572BOOL WINAPI SetConsoleDisplayMode( HANDLE handle, DWORD flags, COORD *size ) 1573{ 1574 TRACE( "(%p, %lx, (%d, %d))\n", handle, flags, size->X, size->Y ); 1575 if (flags == 1) 1576 { 1577 /* We cannot switch to fullscreen */ 1578 return FALSE; 1579 } 1580 return TRUE; 1581} 1582 1583 1584/****************************************************************************** 1585 * SetConsoleInputExeNameA (kernelbase.@) 1586 */ 1587BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleInputExeNameA( LPCSTR name ) 1588{ 1589 if (!name || !name[0]) 1590 { 1591 SetLastError( ERROR_INVALID_PARAMETER ); 1592 return FALSE; 1593 } 1594 RtlEnterCriticalSection( &console_section ); 1595 MultiByteToWideChar( CP_ACP, 0, name, -1, input_exe, ARRAY_SIZE(input_exe) ); 1596 RtlLeaveCriticalSection( &console_section ); 1597 return TRUE; 1598} 1599 1600 1601/****************************************************************************** 1602 * SetConsoleInputExeNameW (kernelbase.@) 1603 */ 1604BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleInputExeNameW( LPCWSTR name ) 1605{ 1606 if (!name || !name[0]) 1607 { 1608 SetLastError( ERROR_INVALID_PARAMETER ); 1609 return FALSE; 1610 } 1611 RtlEnterCriticalSection( &console_section ); 1612 lstrcpynW( input_exe, name, ARRAY_SIZE(input_exe) ); 1613 RtlLeaveCriticalSection( &console_section ); 1614 return TRUE; 1615} 1616 1617 1618/****************************************************************************** 1619 * SetConsoleMode (kernelbase.@) 1620 */ 1621BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleMode( HANDLE handle, DWORD mode ) 1622{ 1623 TRACE( "(%p,%lx)\n", handle, mode ); 1624 return console_ioctl( handle, IOCTL_CONDRV_SET_MODE, &mode, sizeof(mode), NULL, 0, NULL ); 1625} 1626 1627 1628/****************************************************************************** 1629 * SetConsoleOutputCP (kernelbase.@) 1630 */ 1631BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleOutputCP( UINT cp ) 1632{ 1633 struct condrv_input_info_params params = { SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE }; 1634 1635 params.info.output_cp = cp; 1636 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, 1637 IOCTL_CONDRV_SET_INPUT_INFO, &params, sizeof(params), NULL, 0, NULL ); 1638} 1639 1640 1641/****************************************************************************** 1642 * SetConsoleScreenBufferInfoEx (kernelbase.@) 1643 */ 1644BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleScreenBufferInfoEx( HANDLE handle, 1645 CONSOLE_SCREEN_BUFFER_INFOEX *info ) 1646{ 1647 struct condrv_output_info_params params = 1648 { SET_CONSOLE_OUTPUT_INFO_CURSOR_POS | SET_CONSOLE_OUTPUT_INFO_SIZE | 1649 SET_CONSOLE_OUTPUT_INFO_ATTR | SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR | 1650 SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW | SET_CONSOLE_OUTPUT_INFO_MAX_SIZE }; 1651 1652 TRACE("(%p, %p)\n", handle, info); 1653 1654 if (info->cbSize != sizeof(CONSOLE_SCREEN_BUFFER_INFOEX)) 1655 { 1656 SetLastError(ERROR_INVALID_PARAMETER); 1657 return FALSE; 1658 } 1659 1660 params.info.width = info->dwSize.X; 1661 params.info.height = info->dwSize.Y; 1662 params.info.cursor_x = info->dwCursorPosition.X; 1663 params.info.cursor_y = info->dwCursorPosition.Y; 1664 params.info.attr = info->wAttributes; 1665 params.info.win_left = info->srWindow.Left; 1666 params.info.win_top = info->srWindow.Top; 1667 params.info.win_right = info->srWindow.Right; 1668 params.info.win_bottom = info->srWindow.Bottom; 1669 params.info.popup_attr = info->wPopupAttributes; 1670 params.info.max_width = min( info->dwMaximumWindowSize.X, info->dwSize.X ); 1671 params.info.max_height = min( info->dwMaximumWindowSize.Y, info->dwSize.Y ); 1672 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL ); 1673} 1674 1675 1676/****************************************************************************** 1677 * SetConsoleScreenBufferSize (kernelbase.@) 1678 */ 1679BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleScreenBufferSize( HANDLE handle, COORD size ) 1680{ 1681 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_SIZE }; 1682 1683 TRACE( "(%p,(%d,%d))\n", handle, size.X, size.Y ); 1684 1685 params.info.width = size.X; 1686 params.info.height = size.Y; 1687 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL ); 1688} 1689 1690 1691/****************************************************************************** 1692 * SetConsoleTextAttribute (kernelbase.@) 1693 */ 1694BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleTextAttribute( HANDLE handle, WORD attr ) 1695{ 1696 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_ATTR }; 1697 1698 TRACE( "(%p,%d)\n", handle, attr ); 1699 1700 params.info.attr = attr; 1701 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL ); 1702} 1703 1704 1705/****************************************************************************** 1706 * SetConsoleTitleA (kernelbase.@) 1707 */ 1708BOOL WINAPI SetConsoleTitleA( LPCSTR title ) 1709{ 1710 LPWSTR titleW; 1711 BOOL ret; 1712 DWORD len = MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, NULL, 0 ); 1713 if (!(titleW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE; 1714 MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, titleW, len ); 1715 ret = SetConsoleTitleW(titleW); 1716 HeapFree( GetProcessHeap(), 0, titleW ); 1717 return ret; 1718} 1719 1720 1721/****************************************************************************** 1722 * SetConsoleTitleW (kernelbase.@) 1723 */ 1724BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleTitleW( LPCWSTR title ) 1725{ 1726 TRACE( "%s\n", debugstr_w( title )); 1727 1728 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, IOCTL_CONDRV_SET_TITLE, 1729 (void *)title, lstrlenW(title) * sizeof(WCHAR), NULL, 0, NULL ); 1730} 1731 1732 1733/****************************************************************************** 1734 * SetConsoleWindowInfo (kernelbase.@) 1735 */ 1736BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleWindowInfo( HANDLE handle, BOOL absolute, SMALL_RECT *window ) 1737{ 1738 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW }; 1739 SMALL_RECT rect = *window; 1740 1741 TRACE( "(%p,%d,(%d,%d-%d,%d))\n", handle, absolute, rect.Left, rect.Top, rect.Right, rect.Bottom ); 1742 1743 if (!absolute) 1744 { 1745 CONSOLE_SCREEN_BUFFER_INFO info; 1746 1747 if (!GetConsoleScreenBufferInfo( handle, &info )) return FALSE; 1748 rect.Left += info.srWindow.Left; 1749 rect.Top += info.srWindow.Top; 1750 rect.Right += info.srWindow.Right; 1751 rect.Bottom += info.srWindow.Bottom; 1752 } 1753 1754 params.info.win_left = rect.Left; 1755 params.info.win_top = rect.Top; 1756 params.info.win_right = rect.Right; 1757 params.info.win_bottom = rect.Bottom; 1758 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL ); 1759} 1760 1761 1762/****************************************************************************** 1763 * SetCurrentConsoleFontEx (kernelbase.@) 1764 */ 1765BOOL WINAPI SetCurrentConsoleFontEx( HANDLE handle, BOOL maxwindow, CONSOLE_FONT_INFOEX *info ) 1766{ 1767 struct 1768 { 1769 struct condrv_output_info_params params; 1770 WCHAR face_name[LF_FACESIZE]; 1771 } data; 1772 1773 size_t size; 1774 1775 TRACE( "(%p %d %p)\n", handle, maxwindow, info ); 1776 1777 if (info->cbSize != sizeof(*info)) 1778 { 1779 SetLastError(ERROR_INVALID_PARAMETER); 1780 return FALSE; 1781 } 1782 1783 data.params.mask = SET_CONSOLE_OUTPUT_INFO_FONT; 1784 1785 data.params.info.font_width = info->dwFontSize.X; 1786 data.params.info.font_height = info->dwFontSize.Y; 1787 data.params.info.font_pitch_family = info->FontFamily; 1788 data.params.info.font_weight = info->FontWeight; 1789 1790 size = wcsnlen( info->FaceName, LF_FACESIZE - 1 ) * sizeof(WCHAR); 1791 memcpy( data.face_name, info->FaceName, size ); 1792 1793 size += sizeof(struct condrv_output_info_params); 1794 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &data, size, NULL, 0, NULL ); 1795} 1796 1797 1798/*********************************************************************** 1799 * ReadConsoleInputA (kernelbase.@) 1800 */ 1801BOOL WINAPI ReadConsoleInputA( HANDLE handle, INPUT_RECORD *buffer, DWORD length, DWORD *count ) 1802{ 1803 DWORD read; 1804 1805 if (!ReadConsoleInputW( handle, buffer, length, &read )) return FALSE; 1806 input_records_WtoA( buffer, read ); 1807 if (count) *count = read; 1808 return TRUE; 1809} 1810 1811 1812/*********************************************************************** 1813 * ReadConsoleInputW (kernelbase.@) 1814 */ 1815BOOL WINAPI ReadConsoleInputW( HANDLE handle, INPUT_RECORD *buffer, DWORD length, DWORD *count ) 1816{ 1817 if (!console_ioctl( handle, IOCTL_CONDRV_READ_INPUT, NULL, 0, 1818 buffer, length * sizeof(*buffer), count )) 1819 return FALSE; 1820 *count /= sizeof(*buffer); 1821 return TRUE; 1822} 1823 1824 1825/****************************************************************************** 1826 * WriteConsoleInputA (kernelbase.@) 1827 */ 1828BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleInputA( HANDLE handle, const INPUT_RECORD *buffer, 1829 DWORD count, DWORD *written ) 1830{ 1831 INPUT_RECORD *recW = NULL; 1832 BOOL ret; 1833 1834 if (count > 0) 1835 { 1836 if (!buffer) 1837 { 1838 SetLastError( ERROR_INVALID_ACCESS ); 1839 return FALSE; 1840 } 1841 if (!(recW = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*recW) ))) 1842 { 1843 SetLastError( ERROR_NOT_ENOUGH_MEMORY ); 1844 return FALSE; 1845 } 1846 memcpy( recW, buffer, count * sizeof(*recW) ); 1847 input_records_AtoW( recW, count ); 1848 } 1849 ret = WriteConsoleInputW( handle, recW, count, written ); 1850 HeapFree( GetProcessHeap(), 0, recW ); 1851 return ret; 1852} 1853 1854 1855/****************************************************************************** 1856 * WriteConsoleInputW (kernelbase.@) 1857 */ 1858BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleInputW( HANDLE handle, const INPUT_RECORD *buffer, 1859 DWORD count, DWORD *written ) 1860{ 1861 TRACE( "(%p,%p,%ld,%p)\n", handle, buffer, count, written ); 1862 1863 if (count > 0 && !buffer) 1864 { 1865 SetLastError( ERROR_INVALID_ACCESS ); 1866 return FALSE; 1867 } 1868 1869 if (!DeviceIoControl( handle, IOCTL_CONDRV_WRITE_INPUT, (void *)buffer, count * sizeof(*buffer), NULL, 0, NULL, NULL )) 1870 return FALSE; 1871 1872 if (!written) 1873 { 1874 SetLastError( ERROR_INVALID_ACCESS ); 1875 return FALSE; 1876 } 1877 *written = count; 1878 return TRUE; 1879} 1880 1881 1882/*********************************************************************** 1883 * WriteConsoleOutputA (kernelbase.@) 1884 */ 1885BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputA( HANDLE handle, const CHAR_INFO *buffer, 1886 COORD size, COORD coord, SMALL_RECT *region ) 1887{ 1888 int y; 1889 BOOL ret; 1890 COORD new_size, new_coord; 1891 CHAR_INFO *ciW; 1892 1893 new_size.X = min( region->Right - region->Left + 1, size.X - coord.X ); 1894 new_size.Y = min( region->Bottom - region->Top + 1, size.Y - coord.Y ); 1895 1896 if (new_size.X <= 0 || new_size.Y <= 0) 1897 { 1898 region->Bottom = region->Top + new_size.Y - 1; 1899 region->Right = region->Left + new_size.X - 1; 1900 return TRUE; 1901 } 1902 1903 /* only copy the useful rectangle */ 1904 if (!(ciW = HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO) * new_size.X * new_size.Y ))) 1905 return FALSE; 1906 for (y = 0; y < new_size.Y; y++) 1907 memcpy( &ciW[y * new_size.X], &buffer[(y + coord.Y) * size.X + coord.X], 1908 new_size.X * sizeof(CHAR_INFO) ); 1909 char_info_AtoW( ciW, new_size.X * new_size.Y ); 1910 new_coord.X = new_coord.Y = 0; 1911 ret = WriteConsoleOutputW( handle, ciW, new_size, new_coord, region ); 1912 HeapFree( GetProcessHeap(), 0, ciW ); 1913 return ret; 1914} 1915 1916 1917/*********************************************************************** 1918 * WriteConsoleOutputW (kernelbase.@) 1919 */ 1920BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputW( HANDLE handle, const CHAR_INFO *buffer, 1921 COORD size, COORD coord, SMALL_RECT *region ) 1922{ 1923 struct condrv_output_params *params; 1924 unsigned int width, height, y; 1925 size_t params_size; 1926 BOOL ret; 1927 1928 TRACE( "(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n", 1929 handle, buffer, size.X, size.Y, coord.X, coord.Y, 1930 region->Left, region->Top, region->Right, region->Bottom ); 1931 1932 if (region->Left > region->Right || region->Top > region->Bottom || size.X <= coord.X || size.Y <= coord.Y) 1933 { 1934 SetLastError( ERROR_INVALID_PARAMETER ); 1935 return FALSE; 1936 } 1937 1938 width = min( region->Right - region->Left + 1, size.X - coord.X ); 1939 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y ); 1940 region->Right = region->Left + width - 1; 1941 region->Bottom = region->Top + height - 1; 1942 1943 params_size = sizeof(*params) + width * height * sizeof(*buffer); 1944 if (!(params = HeapAlloc( GetProcessHeap(), 0, params_size ))) 1945 { 1946 SetLastError( ERROR_NOT_ENOUGH_MEMORY ); 1947 return FALSE; 1948 } 1949 1950 params->mode = CHAR_INFO_MODE_TEXTATTR; 1951 params->x = region->Left; 1952 params->y = region->Top; 1953 params->width = width; 1954 1955 for (y = 0; y < height; y++) 1956 memcpy( &((CHAR_INFO *)(params + 1))[y * width], &buffer[(y + coord.Y) * size.X + coord.X], width * sizeof(CHAR_INFO) ); 1957 1958 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_OUTPUT, params, params_size, region, sizeof(*region), NULL ); 1959 HeapFree( GetProcessHeap(), 0, params ); 1960 return ret; 1961} 1962 1963 1964/****************************************************************************** 1965 * WriteConsoleOutputAttribute (kernelbase.@) 1966 */ 1967BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputAttribute( HANDLE handle, const WORD *attr, DWORD length, 1968 COORD coord, DWORD *written ) 1969{ 1970 struct condrv_output_params *params; 1971 size_t size; 1972 BOOL ret; 1973 1974 TRACE( "(%p,%p,%ld,%dx%d,%p)\n", handle, attr, length, coord.X, coord.Y, written ); 1975 1976 if ((length > 0 && !attr) || !written) 1977 { 1978 SetLastError( ERROR_INVALID_ACCESS ); 1979 return FALSE; 1980 } 1981 1982 *written = 0; 1983 size = sizeof(*params) + length * sizeof(WORD); 1984 if (!(params = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE; 1985 params->mode = CHAR_INFO_MODE_ATTR; 1986 params->x = coord.X; 1987 params->y = coord.Y; 1988 params->width = 0; 1989 memcpy( params + 1, attr, length * sizeof(*attr) ); 1990 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_OUTPUT, params, size, written, sizeof(*written), NULL ); 1991 HeapFree( GetProcessHeap(), 0, params ); 1992 return ret; 1993} 1994 1995 1996/****************************************************************************** 1997 * WriteConsoleOutputCharacterA (kernelbase.@) 1998 */ 1999BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputCharacterA( HANDLE handle, LPCSTR str, DWORD length, 2000 COORD coord, DWORD *written ) 2001{ 2002 BOOL ret; 2003 LPWSTR strW = NULL; 2004 DWORD lenW = 0; 2005 2006 TRACE( "(%p,%s,%ld,%dx%d,%p)\n", handle, debugstr_an(str, length), length, coord.X, coord.Y, written ); 2007 2008 if (length > 0) 2009 { 2010 UINT cp = GetConsoleOutputCP(); 2011 if (!str) 2012 { 2013 SetLastError( ERROR_INVALID_ACCESS ); 2014 return FALSE; 2015 } 2016 lenW = MultiByteToWideChar( cp, 0, str, length, NULL, 0 ); 2017 2018 if (!(strW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) ))) 2019 { 2020 SetLastError( ERROR_NOT_ENOUGH_MEMORY ); 2021 return FALSE; 2022 } 2023 MultiByteToWideChar( cp, 0, str, length, strW, lenW ); 2024 } 2025 ret = WriteConsoleOutputCharacterW( handle, strW, lenW, coord, written ); 2026 HeapFree( GetProcessHeap(), 0, strW ); 2027 return ret; 2028} 2029 2030 2031/****************************************************************************** 2032 * WriteConsoleOutputCharacterW (kernelbase.@) 2033 */ 2034BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputCharacterW( HANDLE handle, LPCWSTR str, DWORD length, 2035 COORD coord, DWORD *written ) 2036{ 2037 struct condrv_output_params *params; 2038 size_t size; 2039 BOOL ret; 2040 2041 TRACE( "(%p,%s,%ld,%dx%d,%p)\n", handle, debugstr_wn(str, length), length, coord.X, coord.Y, written ); 2042 2043 if ((length > 0 && !str) || !written) 2044 { 2045 SetLastError( ERROR_INVALID_ACCESS ); 2046 return FALSE; 2047 } 2048 2049 *written = 0; 2050 size = sizeof(*params) + length * sizeof(WCHAR); 2051 if (!(params = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE; 2052 params->mode = CHAR_INFO_MODE_TEXT; 2053 params->x = coord.X; 2054 params->y = coord.Y; 2055 params->width = 0; 2056 memcpy( params + 1, str, length * sizeof(*str) ); 2057 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_OUTPUT, params, size, written, sizeof(*written), NULL ); 2058 HeapFree( GetProcessHeap(), 0, params ); 2059 return ret; 2060} 2061 2062 2063/*********************************************************************** 2064 * ReadConsoleA (kernelbase.@) 2065 */ 2066BOOL WINAPI ReadConsoleA( HANDLE handle, void *buffer, DWORD length, DWORD *count, void *reserved ) 2067{ 2068 if (length > INT_MAX) 2069 { 2070 SetLastError( ERROR_NOT_ENOUGH_MEMORY ); 2071 return FALSE; 2072 } 2073 2074 return console_ioctl( handle, IOCTL_CONDRV_READ_FILE, NULL, 0, buffer, length, count ); 2075} 2076 2077 2078/*********************************************************************** 2079 * ReadConsoleW (kernelbase.@) 2080 */ 2081BOOL WINAPI ReadConsoleW( HANDLE handle, void *buffer, DWORD length, DWORD *count, void *reserved ) 2082{ 2083 BOOL ret; 2084 2085 TRACE( "(%p,%p,%ld,%p,%p)\n", handle, buffer, length, count, reserved ); 2086 2087 if (length > INT_MAX) 2088 { 2089 SetLastError(ERROR_NOT_ENOUGH_MEMORY); 2090 return FALSE; 2091 } 2092 2093 if (reserved) 2094 { 2095 CONSOLE_READCONSOLE_CONTROL* crc = reserved; 2096 char *tmp; 2097 2098 if (crc->nLength != sizeof(*crc) || crc->nInitialChars >= length) 2099 { 2100 SetLastError( ERROR_INVALID_PARAMETER ); 2101 return FALSE; 2102 } 2103 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD) + length * sizeof(WCHAR) ))) 2104 { 2105 SetLastError( ERROR_NOT_ENOUGH_MEMORY ); 2106 return FALSE; 2107 } 2108 2109 memcpy( tmp, &crc->dwCtrlWakeupMask, sizeof(DWORD) ); 2110 memcpy( tmp + sizeof(DWORD), buffer, crc->nInitialChars * sizeof(WCHAR) ); 2111 ret = console_ioctl( handle, IOCTL_CONDRV_READ_CONSOLE_CONTROL, 2112 tmp, sizeof(DWORD) + crc->nInitialChars * sizeof(WCHAR), 2113 tmp, sizeof(DWORD) + length * sizeof(WCHAR), count ); 2114 if (ret) 2115 { 2116 memcpy( &crc->dwConsoleKeyState, tmp, sizeof(DWORD) ); 2117 *count -= sizeof(DWORD); 2118 memcpy( buffer, tmp + sizeof(DWORD), *count ); 2119 } 2120 HeapFree( GetProcessHeap(), 0, tmp ); 2121 } 2122 else 2123 { 2124 ret = console_ioctl( handle, IOCTL_CONDRV_READ_CONSOLE, NULL, 0, buffer, 2125 length * sizeof(WCHAR), count ); 2126 } 2127 if (ret) *count /= sizeof(WCHAR); 2128 return ret; 2129} 2130 2131 2132/*********************************************************************** 2133 * WriteConsoleA (kernelbase.@) 2134 */ 2135BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleA( HANDLE handle, const void *buffer, DWORD length, 2136 DWORD *written, void *reserved ) 2137{ 2138 BOOL ret; 2139 2140 TRACE( "(%p,%s,%ld,%p,%p)\n", handle, debugstr_an(buffer, length), length, written, reserved ); 2141 2142 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_FILE, (void *)buffer, length, NULL, 0, NULL ); 2143 if (written) *written = ret ? length : 0; 2144 return ret; 2145} 2146 2147 2148/*********************************************************************** 2149 * WriteConsoleW (kernelbase.@) 2150 */ 2151BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleW( HANDLE handle, const void *buffer, DWORD length, 2152 DWORD *written, void *reserved ) 2153{ 2154 BOOL ret; 2155 2156 TRACE( "(%p,%s,%ld,%p,%p)\n", handle, debugstr_wn(buffer, length), length, written, reserved ); 2157 2158 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_CONSOLE, (void *)buffer, 2159 length * sizeof(WCHAR), NULL, 0, NULL ); 2160 if (written) *written = ret ? length : 0; 2161 return ret; 2162} 2163 2164 2165/*********************************************************************** 2166 * FlushConsoleInputBuffer (kernelbase.@) 2167 */ 2168BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle ) 2169{ 2170 return console_ioctl( handle, IOCTL_CONDRV_FLUSH, NULL, 0, NULL, 0, NULL ); 2171} 2172 2173 2174/*********************************************************************** 2175 * Beep (kernelbase.@) 2176 */ 2177BOOL WINAPI Beep( DWORD frequency, DWORD duration ) 2178{ 2179 /* FIXME: we should not require a console to be attached */ 2180 console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, 2181 IOCTL_CONDRV_BEEP, NULL, 0, NULL, 0, NULL ); 2182 return TRUE; 2183} 2184 2185 2186static HANDLE create_pseudo_console( COORD size, HANDLE input, HANDLE output, HANDLE signal, 2187 DWORD flags, HANDLE *process ) 2188{ 2189 WCHAR cmd[MAX_PATH], conhost_path[MAX_PATH]; 2190 unsigned int inherit_count; 2191 PROCESS_INFORMATION pi; 2192 HANDLE server, console; 2193 HANDLE inherit[2]; 2194 STARTUPINFOEXW si; 2195 SIZE_T attr_size; 2196 void *redir; 2197 BOOL res; 2198 2199 if (!(server = create_console_server())) return NULL; 2200 2201 console = create_console_reference( server ); 2202 if (!console) 2203 { 2204 NtClose( server ); 2205 return NULL; 2206 } 2207 2208 memset( &si, 0, sizeof(si) ); 2209 si.StartupInfo.cb = sizeof(STARTUPINFOEXW); 2210 si.StartupInfo.hStdInput = input; 2211 si.StartupInfo.hStdOutput = output; 2212 si.StartupInfo.hStdError = output; 2213 si.StartupInfo.dwFlags = STARTF_USESTDHANDLES; 2214 2215 inherit[0] = server; 2216 inherit[1] = signal; 2217 inherit_count = signal ? 2 : 1; 2218 InitializeProcThreadAttributeList( NULL, inherit_count, 0, &attr_size ); 2219 if (!(si.lpAttributeList = HeapAlloc( GetProcessHeap(), 0, attr_size ))) 2220 { 2221 NtClose( console ); 2222 NtClose( server ); 2223 return FALSE; 2224 } 2225 InitializeProcThreadAttributeList( si.lpAttributeList, inherit_count, 0, &attr_size ); 2226 UpdateProcThreadAttribute( si.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, 2227 inherit, sizeof(*inherit) * inherit_count, NULL, NULL ); 2228 2229 swprintf( conhost_path, ARRAY_SIZE(conhost_path), L"%s\\conhost.exe", system_dir ); 2230 if (signal) 2231 { 2232 swprintf( cmd, ARRAY_SIZE(cmd), 2233 L"\"%s\" --headless %s--width %u --height %u --signal 0x%x --server 0x%x", 2234 conhost_path, (flags & PSEUDOCONSOLE_INHERIT_CURSOR) ? L"--inheritcursor " : L"", 2235 size.X, size.Y, signal, server ); 2236 } 2237 else 2238 { 2239 swprintf( cmd, ARRAY_SIZE(cmd), L"\"%s\" --unix --width %u --height %u --server 0x%x", 2240 conhost_path, size.X, size.Y, server ); 2241 } 2242 Wow64DisableWow64FsRedirection( &redir ); 2243 res = CreateProcessW( conhost_path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS | EXTENDED_STARTUPINFO_PRESENT, 2244 NULL, NULL, &si.StartupInfo, &pi ); 2245 HeapFree( GetProcessHeap(), 0, si.lpAttributeList ); 2246 Wow64RevertWow64FsRedirection( redir ); 2247 NtClose( server ); 2248 if (!res) 2249 { 2250 NtClose( console ); 2251 return NULL; 2252 } 2253 2254 NtClose( pi.hThread ); 2255 *process = pi.hProcess; 2256 return console; 2257} 2258 2259/****************************************************************************** 2260 * CreatePseudoConsole (kernelbase.@) 2261 */ 2262HRESULT WINAPI CreatePseudoConsole( COORD size, HANDLE input, HANDLE output, DWORD flags, HPCON *ret ) 2263{ 2264 SECURITY_ATTRIBUTES inherit_attr = { sizeof(inherit_attr), NULL, TRUE }; 2265 struct pseudo_console *pseudo_console; 2266 HANDLE tty_input = NULL, tty_output; 2267 HANDLE signal = NULL; 2268 WCHAR pipe_name[64]; 2269 2270 TRACE( "(%u,%u) %p %p %lx %p\n", size.X, size.Y, input, output, flags, ret ); 2271 2272 if (!size.X || !size.Y || !ret) return E_INVALIDARG; 2273 2274 if (!(pseudo_console = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pseudo_console) ))) return E_OUTOFMEMORY; 2275 2276 swprintf( pipe_name, ARRAY_SIZE(pipe_name), L"\\\\.\\pipe\\wine_pty_signal_pipe%x", 2277 GetCurrentThreadId() ); 2278 signal = CreateNamedPipeW( pipe_name, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 2279 PIPE_UNLIMITED_INSTANCES, 4096, 4096, NMPWAIT_USE_DEFAULT_WAIT, &inherit_attr ); 2280 if (signal == INVALID_HANDLE_VALUE) 2281 { 2282 HeapFree( GetProcessHeap(), 0, pseudo_console ); 2283 return HRESULT_FROM_WIN32( GetLastError() ); 2284 } 2285 pseudo_console->signal = CreateFileW( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL ); 2286 if (pseudo_console->signal != INVALID_HANDLE_VALUE && 2287 DuplicateHandle( GetCurrentProcess(), input, GetCurrentProcess(), &tty_input, 0, TRUE, DUPLICATE_SAME_ACCESS) && 2288 DuplicateHandle( GetCurrentProcess(), output, GetCurrentProcess(), &tty_output, 0, TRUE, DUPLICATE_SAME_ACCESS)) 2289 { 2290 pseudo_console->reference = create_pseudo_console( size, tty_input, tty_output, signal, flags, 2291 &pseudo_console->process ); 2292 NtClose( tty_output ); 2293 } 2294 NtClose( tty_input ); 2295 NtClose( signal ); 2296 if (!pseudo_console->reference) 2297 { 2298 ClosePseudoConsole( pseudo_console ); 2299 return HRESULT_FROM_WIN32( GetLastError() ); 2300 } 2301 2302 *ret = pseudo_console; 2303 return S_OK; 2304} 2305 2306/****************************************************************************** 2307 * ClosePseudoConsole (kernelbase.@) 2308 */ 2309void WINAPI ClosePseudoConsole( HPCON handle ) 2310{ 2311 struct pseudo_console *pseudo_console = handle; 2312 2313 TRACE( "%p\n", handle ); 2314 2315 if (!pseudo_console) return; 2316 if (pseudo_console->signal) CloseHandle( pseudo_console->signal ); 2317 if (pseudo_console->process) 2318 { 2319 WaitForSingleObject( pseudo_console->process, INFINITE ); 2320 CloseHandle( pseudo_console->process ); 2321 } 2322 if (pseudo_console->reference) CloseHandle( pseudo_console->reference ); 2323} 2324 2325/****************************************************************************** 2326 * ResizePseudoConsole (kernelbase.@) 2327 */ 2328HRESULT WINAPI ResizePseudoConsole( HPCON handle, COORD size ) 2329{ 2330 FIXME( "%p (%u,%u)\n", handle, size.X, size.Y ); 2331 return E_NOTIMPL; 2332} 2333 2334static BOOL is_tty_handle( HANDLE handle ) 2335{ 2336 return ((UINT_PTR)handle & 3) == 1; 2337} 2338 2339void init_console( void ) 2340{ 2341 RTL_USER_PROCESS_PARAMETERS *params = RtlGetCurrentPeb()->ProcessParameters; 2342 2343 if (params->ConsoleHandle == CONSOLE_HANDLE_SHELL) 2344 { 2345 HANDLE tty_in = NULL, tty_out = NULL, process = NULL; 2346 COORD size; 2347 2348 if (is_tty_handle( params->hStdInput )) 2349 { 2350 tty_in = params->hStdInput; 2351 params->hStdInput = NULL; 2352 } 2353 if (is_tty_handle( params->hStdOutput )) 2354 { 2355 tty_out = params->hStdOutput; 2356 params->hStdOutput = NULL; 2357 } 2358 if (is_tty_handle( params->hStdError )) 2359 { 2360 if (tty_out) CloseHandle( params->hStdError ); 2361 else tty_out = params->hStdError; 2362 params->hStdError = NULL; 2363 } 2364 2365 size.X = params->dwXCountChars; 2366 size.Y = params->dwYCountChars; 2367 TRACE( "creating unix console (size %u %u)\n", size.X, size.Y ); 2368 params->ConsoleHandle = create_pseudo_console( size, tty_in, tty_out, NULL, 0, &process ); 2369 CloseHandle( process ); 2370 CloseHandle( tty_in ); 2371 CloseHandle( tty_out ); 2372 2373 if (params->ConsoleHandle && create_console_connection( params->ConsoleHandle )) 2374 { 2375 init_console_std_handles( FALSE ); 2376 } 2377 } 2378 else if (params->ConsoleHandle == CONSOLE_HANDLE_ALLOC || 2379 params->ConsoleHandle == CONSOLE_HANDLE_ALLOC_NO_WINDOW) 2380 { 2381 BOOL no_window = params->ConsoleHandle == CONSOLE_HANDLE_ALLOC_NO_WINDOW; 2382 HMODULE mod = GetModuleHandleW( NULL ); 2383 params->ConsoleHandle = NULL; 2384 if (RtlImageNtHeader( mod )->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI) 2385 alloc_console( no_window ); 2386 } 2387 else if (params->ConsoleHandle && params->ConsoleHandle != CONSOLE_HANDLE_SHELL_NO_WINDOW) 2388 create_console_connection( params->ConsoleHandle ); 2389}