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"
44 #include "kernel_private.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(console
);
48 /******************************************************************************
49 * GetConsoleWindow [KERNEL32.@] Get hwnd of the console window.
52 * Success: hwnd of the console window.
55 HWND WINAPI
GetConsoleWindow(void)
57 struct condrv_input_info info
;
60 ret
= DeviceIoControl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
,
61 IOCTL_CONDRV_GET_INPUT_INFO
, NULL
, 0, &info
, sizeof(info
), NULL
, NULL
);
62 return ret
? wine_server_ptr_handle(info
.win
) : NULL
;
66 /******************************************************************
67 * OpenConsoleW (KERNEL32.@)
70 * Open a handle to the current process console.
71 * Returns INVALID_HANDLE_VALUE on failure.
73 HANDLE WINAPI
OpenConsoleW(LPCWSTR name
, DWORD access
, BOOL inherit
, DWORD creation
)
75 SECURITY_ATTRIBUTES sa
;
77 TRACE("(%s, 0x%08x, %d, %u)\n", debugstr_w(name
), access
, inherit
, creation
);
79 if (!name
|| (wcsicmp( L
"CONIN$", name
) && wcsicmp( L
"CONOUT$", name
)) || creation
!= OPEN_EXISTING
)
81 SetLastError( ERROR_INVALID_PARAMETER
);
82 return INVALID_HANDLE_VALUE
;
85 sa
.nLength
= sizeof(sa
);
86 sa
.lpSecurityDescriptor
= NULL
;
87 sa
.bInheritHandle
= inherit
;
89 return CreateFileW( name
, access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, &sa
, creation
, 0, NULL
);
92 /******************************************************************
93 * VerifyConsoleIoHandle (KERNEL32.@)
97 BOOL WINAPI
VerifyConsoleIoHandle(HANDLE handle
)
101 return !NtDeviceIoControlFile( handle
, NULL
, NULL
, NULL
, &io
, IOCTL_CONDRV_GET_MODE
,
102 NULL
, 0, &mode
, sizeof(mode
) );
105 /******************************************************************
106 * DuplicateConsoleHandle (KERNEL32.@)
110 HANDLE WINAPI
DuplicateConsoleHandle(HANDLE handle
, DWORD access
, BOOL inherit
,
114 return DuplicateHandle(GetCurrentProcess(), handle
, GetCurrentProcess(), &ret
,
115 access
, inherit
, options
) ? ret
: INVALID_HANDLE_VALUE
;
118 /******************************************************************
119 * CloseConsoleHandle (KERNEL32.@)
123 BOOL WINAPI
CloseConsoleHandle(HANDLE handle
)
125 return CloseHandle(handle
);
128 /******************************************************************
129 * GetConsoleInputWaitHandle (KERNEL32.@)
133 HANDLE WINAPI
GetConsoleInputWaitHandle(void)
135 return GetStdHandle( STD_INPUT_HANDLE
);
139 /***********************************************************************
140 * SetConsoleTitleA (KERNEL32.@)
142 BOOL WINAPI
SetConsoleTitleA( LPCSTR title
)
147 DWORD len
= MultiByteToWideChar( GetConsoleOutputCP(), 0, title
, -1, NULL
, 0 );
148 if (!(titleW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
)))) return FALSE
;
149 MultiByteToWideChar( GetConsoleOutputCP(), 0, title
, -1, titleW
, len
);
150 ret
= SetConsoleTitleW(titleW
);
151 HeapFree(GetProcessHeap(), 0, titleW
);
156 /***********************************************************************
157 * GetConsoleKeyboardLayoutNameA (KERNEL32.@)
159 BOOL WINAPI
GetConsoleKeyboardLayoutNameA(LPSTR layoutName
)
161 FIXME( "stub %p\n", layoutName
);
165 /***********************************************************************
166 * GetConsoleKeyboardLayoutNameW (KERNEL32.@)
168 BOOL WINAPI
GetConsoleKeyboardLayoutNameW(LPWSTR layoutName
)
172 FIXME( "stub %p\n", layoutName
);
176 /***********************************************************************
177 * GetConsoleTitleA (KERNEL32.@)
179 * See GetConsoleTitleW.
181 DWORD WINAPI
GetConsoleTitleA(LPSTR title
, DWORD size
)
183 WCHAR
*ptr
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * size
);
187 ret
= GetConsoleTitleW( ptr
, size
);
190 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr
, ret
+ 1, title
, size
, NULL
, NULL
);
193 HeapFree(GetProcessHeap(), 0, ptr
);
198 /***********************************************************************
199 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
201 BOOL WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
203 FIXME("(%p): stub\n", nrofbuttons
);
209 /******************************************************************
210 * GetConsoleDisplayMode (KERNEL32.@)
212 BOOL WINAPI
GetConsoleDisplayMode(LPDWORD lpModeFlags
)
214 TRACE("semi-stub: %p\n", lpModeFlags
);
215 /* It is safe to successfully report windowed mode */
220 /******************************************************************
221 * SetConsoleDisplayMode (KERNEL32.@)
223 BOOL WINAPI
SetConsoleDisplayMode(HANDLE hConsoleOutput
, DWORD dwFlags
,
224 COORD
*lpNewScreenBufferDimensions
)
226 TRACE("(%p, %x, (%d, %d))\n", hConsoleOutput
, dwFlags
,
227 lpNewScreenBufferDimensions
->X
, lpNewScreenBufferDimensions
->Y
);
230 /* We cannot switch to fullscreen */
236 /******************************************************************
241 * 0 if an error occurred, non-zero for success
244 DWORD WINAPI
GetConsoleAliasW(LPWSTR lpSource
, LPWSTR lpTargetBuffer
,
245 DWORD TargetBufferLength
, LPWSTR lpExename
)
247 FIXME("(%s,%p,%d,%s): stub\n", debugstr_w(lpSource
), lpTargetBuffer
, TargetBufferLength
, debugstr_w(lpExename
));
248 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
252 /******************************************************************
253 * GetConsoleProcessList (KERNEL32.@)
255 DWORD WINAPI
GetConsoleProcessList(LPDWORD processlist
, DWORD processcount
)
257 FIXME("(%p,%d): stub\n", processlist
, processcount
);
259 if (!processlist
|| processcount
< 1)
261 SetLastError(ERROR_INVALID_PARAMETER
);
268 BOOL
CONSOLE_Init(RTL_USER_PROCESS_PARAMETERS
*params
)
270 /* convert value from server:
271 * + INVALID_HANDLE_VALUE => TEB: 0, STARTUPINFO: INVALID_HANDLE_VALUE
272 * + 0 => TEB: 0, STARTUPINFO: INVALID_HANDLE_VALUE
273 * + console handle needs to be mapped
275 if (!params
->hStdInput
|| params
->hStdInput
== INVALID_HANDLE_VALUE
)
276 params
->hStdInput
= 0;
277 else if (!is_console_handle(params
->hStdInput
) && VerifyConsoleIoHandle(params
->hStdInput
))
278 params
->hStdInput
= console_handle_map(params
->hStdInput
);
280 if (!params
->hStdOutput
|| params
->hStdOutput
== INVALID_HANDLE_VALUE
)
281 params
->hStdOutput
= 0;
282 else if (!is_console_handle(params
->hStdOutput
) && VerifyConsoleIoHandle(params
->hStdOutput
))
283 params
->hStdOutput
= console_handle_map(params
->hStdOutput
);
285 if (!params
->hStdError
|| params
->hStdError
== INVALID_HANDLE_VALUE
)
286 params
->hStdError
= 0;
287 else if (!is_console_handle(params
->hStdError
) && VerifyConsoleIoHandle(params
->hStdError
))
288 params
->hStdError
= console_handle_map(params
->hStdError
);
293 /* Undocumented, called by native doskey.exe */
294 /* FIXME: Should use CONSOLE_GetHistory() above for full implementation */
295 DWORD WINAPI
GetConsoleCommandHistoryA(DWORD unknown1
, DWORD unknown2
, DWORD unknown3
)
297 FIXME(": (0x%x, 0x%x, 0x%x) stub!\n", unknown1
, unknown2
, unknown3
);
298 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
302 /* Undocumented, called by native doskey.exe */
303 /* FIXME: Should use CONSOLE_GetHistory() above for full implementation */
304 DWORD WINAPI
GetConsoleCommandHistoryW(DWORD unknown1
, DWORD unknown2
, DWORD unknown3
)
306 FIXME(": (0x%x, 0x%x, 0x%x) stub!\n", unknown1
, unknown2
, unknown3
);
307 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
311 /* Undocumented, called by native doskey.exe */
312 /* FIXME: Should use CONSOLE_GetHistory() above for full implementation */
313 DWORD WINAPI
GetConsoleCommandHistoryLengthA(LPCSTR unknown
)
315 FIXME(": (%s) stub!\n", debugstr_a(unknown
));
316 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
320 /* Undocumented, called by native doskey.exe */
321 /* FIXME: Should use CONSOLE_GetHistory() above for full implementation */
322 DWORD WINAPI
GetConsoleCommandHistoryLengthW(LPCWSTR unknown
)
324 FIXME(": (%s) stub!\n", debugstr_w(unknown
));
325 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
329 DWORD WINAPI
GetConsoleAliasesLengthA(LPSTR unknown
)
331 FIXME(": (%s) stub!\n", debugstr_a(unknown
));
332 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
336 DWORD WINAPI
GetConsoleAliasesLengthW(LPWSTR unknown
)
338 FIXME(": (%s) stub!\n", debugstr_w(unknown
));
339 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
343 DWORD WINAPI
GetConsoleAliasExesLengthA(void)
346 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
350 DWORD WINAPI
GetConsoleAliasExesLengthW(void)
353 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
357 VOID WINAPI
ExpungeConsoleCommandHistoryA(LPCSTR unknown
)
359 FIXME(": (%s) stub!\n", debugstr_a(unknown
));
360 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
363 VOID WINAPI
ExpungeConsoleCommandHistoryW(LPCWSTR unknown
)
365 FIXME(": (%s) stub!\n", debugstr_w(unknown
));
366 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
369 BOOL WINAPI
AddConsoleAliasA(LPSTR source
, LPSTR target
, LPSTR exename
)
371 FIXME(": (%s, %s, %s) stub!\n", debugstr_a(source
), debugstr_a(target
), debugstr_a(exename
));
372 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
376 BOOL WINAPI
AddConsoleAliasW(LPWSTR source
, LPWSTR target
, LPWSTR exename
)
378 FIXME(": (%s, %s, %s) stub!\n", debugstr_w(source
), debugstr_w(target
), debugstr_w(exename
));
379 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
384 BOOL WINAPI
SetConsoleIcon(HICON icon
)
386 FIXME(": (%p) stub!\n", icon
);
387 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
391 DWORD WINAPI
GetNumberOfConsoleFonts(void)
396 BOOL WINAPI
SetConsoleFont(HANDLE hConsole
, DWORD index
)
398 FIXME("(%p, %u): stub!\n", hConsole
, index
);
399 SetLastError(LOWORD(E_NOTIMPL
) /* win10 1709+ */);
403 BOOL WINAPI
SetConsoleKeyShortcuts(BOOL set
, BYTE keys
, VOID
*a
, DWORD b
)
405 FIXME(": (%u %u %p %u) stub!\n", set
, keys
, a
, b
);
406 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
411 BOOL WINAPI
GetCurrentConsoleFontEx(HANDLE hConsole
, BOOL maxwindow
, CONSOLE_FONT_INFOEX
*fontinfo
)
416 struct condrv_output_info info
;
417 WCHAR face_name
[LF_FACESIZE
- 1];
420 if (fontinfo
->cbSize
!= sizeof(CONSOLE_FONT_INFOEX
))
422 SetLastError(ERROR_INVALID_PARAMETER
);
426 if (!DeviceIoControl( hConsole
, IOCTL_CONDRV_GET_OUTPUT_INFO
, NULL
, 0,
427 &data
, sizeof(data
), &size
, NULL
))
429 SetLastError( ERROR_INVALID_HANDLE
);
436 fontinfo
->dwFontSize
.X
= min( data
.info
.width
, data
.info
.max_width
);
437 fontinfo
->dwFontSize
.Y
= min( data
.info
.height
, data
.info
.max_height
);
441 fontinfo
->dwFontSize
.X
= data
.info
.win_right
- data
.info
.win_left
+ 1;
442 fontinfo
->dwFontSize
.Y
= data
.info
.win_bottom
- data
.info
.win_top
+ 1;
444 size
-= sizeof(data
.info
);
445 if (size
) memcpy( fontinfo
->FaceName
, data
.face_name
, size
);
446 fontinfo
->FaceName
[size
/ sizeof(WCHAR
)] = 0;
447 fontinfo
->FontFamily
= data
.info
.font_pitch_family
;
448 fontinfo
->FontWeight
= data
.info
.font_weight
;
452 BOOL WINAPI
GetCurrentConsoleFont(HANDLE hConsole
, BOOL maxwindow
, CONSOLE_FONT_INFO
*fontinfo
)
455 CONSOLE_FONT_INFOEX res
;
457 res
.cbSize
= sizeof(CONSOLE_FONT_INFOEX
);
459 ret
= GetCurrentConsoleFontEx(hConsole
, maxwindow
, &res
);
462 fontinfo
->nFont
= res
.nFont
;
463 fontinfo
->dwFontSize
.X
= res
.dwFontSize
.X
;
464 fontinfo
->dwFontSize
.Y
= res
.dwFontSize
.Y
;
469 static COORD
get_console_font_size(HANDLE hConsole
, DWORD index
)
471 struct condrv_output_info info
;
474 if (index
>= GetNumberOfConsoleFonts())
476 SetLastError(ERROR_INVALID_PARAMETER
);
480 if (DeviceIoControl( hConsole
, IOCTL_CONDRV_GET_OUTPUT_INFO
, NULL
, 0, &info
, sizeof(info
), NULL
, NULL
))
482 c
.X
= info
.font_width
;
483 c
.Y
= info
.font_height
;
485 else SetLastError( ERROR_INVALID_HANDLE
);
489 #if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
490 #undef GetConsoleFontSize
491 DWORD WINAPI
GetConsoleFontSize(HANDLE hConsole
, DWORD index
)
498 x
.c
= get_console_font_size(hConsole
, index
);
502 COORD WINAPI
GetConsoleFontSize(HANDLE hConsole
, DWORD index
)
504 return get_console_font_size(hConsole
, index
);
506 #endif /* !defined(__i386__) */
508 BOOL WINAPI
GetConsoleFontInfo(HANDLE hConsole
, BOOL maximize
, DWORD numfonts
, CONSOLE_FONT_INFO
*info
)
510 FIXME("(%p %d %u %p): stub!\n", hConsole
, maximize
, numfonts
, info
);
511 SetLastError(LOWORD(E_NOTIMPL
) /* win10 1709+ */);
515 BOOL WINAPI
SetCurrentConsoleFontEx(HANDLE hConsole
, BOOL maxwindow
, CONSOLE_FONT_INFOEX
*cfix
)
517 FIXME("(%p %d %p): stub!\n", hConsole
, maxwindow
, cfix
);
518 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);