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 static const WCHAR coninW
[] = {'C','O','N','I','N','$',0};
49 static const WCHAR conoutW
[] = {'C','O','N','O','U','T','$',0};
51 /******************************************************************************
52 * GetConsoleWindow [KERNEL32.@] Get hwnd of the console window.
55 * Success: hwnd of the console window.
58 HWND WINAPI
GetConsoleWindow(void)
60 struct condrv_input_info info
;
63 ret
= DeviceIoControl( RtlGetCurrentPeb()->ProcessParameters
->ConsoleHandle
,
64 IOCTL_CONDRV_GET_INPUT_INFO
, NULL
, 0, &info
, sizeof(info
), NULL
, NULL
);
65 return ret
? wine_server_ptr_handle(info
.win
) : NULL
;
69 /******************************************************************
70 * OpenConsoleW (KERNEL32.@)
73 * Open a handle to the current process console.
74 * Returns INVALID_HANDLE_VALUE on failure.
76 HANDLE WINAPI
OpenConsoleW(LPCWSTR name
, DWORD access
, BOOL inherit
, DWORD creation
)
78 SECURITY_ATTRIBUTES sa
;
80 TRACE("(%s, 0x%08x, %d, %u)\n", debugstr_w(name
), access
, inherit
, creation
);
82 if (!name
|| (wcsicmp( coninW
, name
) && wcsicmp( conoutW
, name
)) || creation
!= OPEN_EXISTING
)
84 SetLastError( ERROR_INVALID_PARAMETER
);
85 return INVALID_HANDLE_VALUE
;
88 sa
.nLength
= sizeof(sa
);
89 sa
.lpSecurityDescriptor
= NULL
;
90 sa
.bInheritHandle
= inherit
;
92 return CreateFileW( name
, access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, &sa
, creation
, 0, NULL
);
95 /******************************************************************
96 * VerifyConsoleIoHandle (KERNEL32.@)
100 BOOL WINAPI
VerifyConsoleIoHandle(HANDLE handle
)
104 return !NtDeviceIoControlFile( handle
, NULL
, NULL
, NULL
, &io
, IOCTL_CONDRV_GET_MODE
,
105 NULL
, 0, &mode
, sizeof(mode
) );
108 /******************************************************************
109 * DuplicateConsoleHandle (KERNEL32.@)
113 HANDLE WINAPI
DuplicateConsoleHandle(HANDLE handle
, DWORD access
, BOOL inherit
,
117 return DuplicateHandle(GetCurrentProcess(), handle
, GetCurrentProcess(), &ret
,
118 access
, inherit
, options
) ? ret
: INVALID_HANDLE_VALUE
;
121 /******************************************************************
122 * CloseConsoleHandle (KERNEL32.@)
126 BOOL WINAPI
CloseConsoleHandle(HANDLE handle
)
128 return CloseHandle(handle
);
131 /******************************************************************
132 * GetConsoleInputWaitHandle (KERNEL32.@)
136 HANDLE WINAPI
GetConsoleInputWaitHandle(void)
138 return GetStdHandle( STD_INPUT_HANDLE
);
142 /***********************************************************************
143 * SetConsoleTitleA (KERNEL32.@)
145 BOOL WINAPI
SetConsoleTitleA( LPCSTR title
)
150 DWORD len
= MultiByteToWideChar( GetConsoleOutputCP(), 0, title
, -1, NULL
, 0 );
151 if (!(titleW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
)))) return FALSE
;
152 MultiByteToWideChar( GetConsoleOutputCP(), 0, title
, -1, titleW
, len
);
153 ret
= SetConsoleTitleW(titleW
);
154 HeapFree(GetProcessHeap(), 0, titleW
);
159 /***********************************************************************
160 * GetConsoleKeyboardLayoutNameA (KERNEL32.@)
162 BOOL WINAPI
GetConsoleKeyboardLayoutNameA(LPSTR layoutName
)
164 FIXME( "stub %p\n", layoutName
);
168 /***********************************************************************
169 * GetConsoleKeyboardLayoutNameW (KERNEL32.@)
171 BOOL WINAPI
GetConsoleKeyboardLayoutNameW(LPWSTR layoutName
)
175 FIXME( "stub %p\n", layoutName
);
179 /***********************************************************************
180 * GetConsoleTitleA (KERNEL32.@)
182 * See GetConsoleTitleW.
184 DWORD WINAPI
GetConsoleTitleA(LPSTR title
, DWORD size
)
186 WCHAR
*ptr
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * size
);
190 ret
= GetConsoleTitleW( ptr
, size
);
193 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr
, ret
+ 1, title
, size
, NULL
, NULL
);
196 HeapFree(GetProcessHeap(), 0, ptr
);
201 /***********************************************************************
202 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
204 BOOL WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
206 FIXME("(%p): stub\n", nrofbuttons
);
212 /******************************************************************
213 * GetConsoleDisplayMode (KERNEL32.@)
215 BOOL WINAPI
GetConsoleDisplayMode(LPDWORD lpModeFlags
)
217 TRACE("semi-stub: %p\n", lpModeFlags
);
218 /* It is safe to successfully report windowed mode */
223 /******************************************************************
224 * SetConsoleDisplayMode (KERNEL32.@)
226 BOOL WINAPI
SetConsoleDisplayMode(HANDLE hConsoleOutput
, DWORD dwFlags
,
227 COORD
*lpNewScreenBufferDimensions
)
229 TRACE("(%p, %x, (%d, %d))\n", hConsoleOutput
, dwFlags
,
230 lpNewScreenBufferDimensions
->X
, lpNewScreenBufferDimensions
->Y
);
233 /* We cannot switch to fullscreen */
239 /******************************************************************
244 * 0 if an error occurred, non-zero for success
247 DWORD WINAPI
GetConsoleAliasW(LPWSTR lpSource
, LPWSTR lpTargetBuffer
,
248 DWORD TargetBufferLength
, LPWSTR lpExename
)
250 FIXME("(%s,%p,%d,%s): stub\n", debugstr_w(lpSource
), lpTargetBuffer
, TargetBufferLength
, debugstr_w(lpExename
));
251 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
255 /******************************************************************
256 * GetConsoleProcessList (KERNEL32.@)
258 DWORD WINAPI
GetConsoleProcessList(LPDWORD processlist
, DWORD processcount
)
260 FIXME("(%p,%d): stub\n", processlist
, processcount
);
262 if (!processlist
|| processcount
< 1)
264 SetLastError(ERROR_INVALID_PARAMETER
);
271 BOOL
CONSOLE_Init(RTL_USER_PROCESS_PARAMETERS
*params
)
273 /* convert value from server:
274 * + INVALID_HANDLE_VALUE => TEB: 0, STARTUPINFO: INVALID_HANDLE_VALUE
275 * + 0 => TEB: 0, STARTUPINFO: INVALID_HANDLE_VALUE
276 * + console handle needs to be mapped
278 if (!params
->hStdInput
|| params
->hStdInput
== INVALID_HANDLE_VALUE
)
279 params
->hStdInput
= 0;
280 else if (!is_console_handle(params
->hStdInput
) && VerifyConsoleIoHandle(params
->hStdInput
))
281 params
->hStdInput
= console_handle_map(params
->hStdInput
);
283 if (!params
->hStdOutput
|| params
->hStdOutput
== INVALID_HANDLE_VALUE
)
284 params
->hStdOutput
= 0;
285 else if (!is_console_handle(params
->hStdOutput
) && VerifyConsoleIoHandle(params
->hStdOutput
))
286 params
->hStdOutput
= console_handle_map(params
->hStdOutput
);
288 if (!params
->hStdError
|| params
->hStdError
== INVALID_HANDLE_VALUE
)
289 params
->hStdError
= 0;
290 else if (!is_console_handle(params
->hStdError
) && VerifyConsoleIoHandle(params
->hStdError
))
291 params
->hStdError
= console_handle_map(params
->hStdError
);
296 /* Undocumented, called by native doskey.exe */
297 /* FIXME: Should use CONSOLE_GetHistory() above for full implementation */
298 DWORD WINAPI
GetConsoleCommandHistoryA(DWORD unknown1
, DWORD unknown2
, DWORD unknown3
)
300 FIXME(": (0x%x, 0x%x, 0x%x) stub!\n", unknown1
, unknown2
, unknown3
);
301 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
305 /* Undocumented, called by native doskey.exe */
306 /* FIXME: Should use CONSOLE_GetHistory() above for full implementation */
307 DWORD WINAPI
GetConsoleCommandHistoryW(DWORD unknown1
, DWORD unknown2
, DWORD unknown3
)
309 FIXME(": (0x%x, 0x%x, 0x%x) stub!\n", unknown1
, unknown2
, unknown3
);
310 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
314 /* Undocumented, called by native doskey.exe */
315 /* FIXME: Should use CONSOLE_GetHistory() above for full implementation */
316 DWORD WINAPI
GetConsoleCommandHistoryLengthA(LPCSTR unknown
)
318 FIXME(": (%s) stub!\n", debugstr_a(unknown
));
319 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
323 /* Undocumented, called by native doskey.exe */
324 /* FIXME: Should use CONSOLE_GetHistory() above for full implementation */
325 DWORD WINAPI
GetConsoleCommandHistoryLengthW(LPCWSTR unknown
)
327 FIXME(": (%s) stub!\n", debugstr_w(unknown
));
328 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
332 DWORD WINAPI
GetConsoleAliasesLengthA(LPSTR unknown
)
334 FIXME(": (%s) stub!\n", debugstr_a(unknown
));
335 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
339 DWORD WINAPI
GetConsoleAliasesLengthW(LPWSTR unknown
)
341 FIXME(": (%s) stub!\n", debugstr_w(unknown
));
342 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
346 DWORD WINAPI
GetConsoleAliasExesLengthA(void)
349 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
353 DWORD WINAPI
GetConsoleAliasExesLengthW(void)
356 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
360 VOID WINAPI
ExpungeConsoleCommandHistoryA(LPCSTR unknown
)
362 FIXME(": (%s) stub!\n", debugstr_a(unknown
));
363 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
366 VOID WINAPI
ExpungeConsoleCommandHistoryW(LPCWSTR unknown
)
368 FIXME(": (%s) stub!\n", debugstr_w(unknown
));
369 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
372 BOOL WINAPI
AddConsoleAliasA(LPSTR source
, LPSTR target
, LPSTR exename
)
374 FIXME(": (%s, %s, %s) stub!\n", debugstr_a(source
), debugstr_a(target
), debugstr_a(exename
));
375 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
379 BOOL WINAPI
AddConsoleAliasW(LPWSTR source
, LPWSTR target
, LPWSTR exename
)
381 FIXME(": (%s, %s, %s) stub!\n", debugstr_w(source
), debugstr_w(target
), debugstr_w(exename
));
382 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
387 BOOL WINAPI
SetConsoleIcon(HICON icon
)
389 FIXME(": (%p) stub!\n", icon
);
390 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
394 DWORD WINAPI
GetNumberOfConsoleFonts(void)
399 BOOL WINAPI
SetConsoleFont(HANDLE hConsole
, DWORD index
)
401 FIXME("(%p, %u): stub!\n", hConsole
, index
);
402 SetLastError(LOWORD(E_NOTIMPL
) /* win10 1709+ */);
406 BOOL WINAPI
SetConsoleKeyShortcuts(BOOL set
, BYTE keys
, VOID
*a
, DWORD b
)
408 FIXME(": (%u %u %p %u) stub!\n", set
, keys
, a
, b
);
409 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
414 BOOL WINAPI
GetCurrentConsoleFontEx(HANDLE hConsole
, BOOL maxwindow
, CONSOLE_FONT_INFOEX
*fontinfo
)
419 struct condrv_output_info info
;
420 WCHAR face_name
[LF_FACESIZE
- 1];
423 if (fontinfo
->cbSize
!= sizeof(CONSOLE_FONT_INFOEX
))
425 SetLastError(ERROR_INVALID_PARAMETER
);
429 if (!DeviceIoControl( hConsole
, IOCTL_CONDRV_GET_OUTPUT_INFO
, NULL
, 0,
430 &data
, sizeof(data
), &size
, NULL
))
432 SetLastError( ERROR_INVALID_HANDLE
);
439 fontinfo
->dwFontSize
.X
= min( data
.info
.width
, data
.info
.max_width
);
440 fontinfo
->dwFontSize
.Y
= min( data
.info
.height
, data
.info
.max_height
);
444 fontinfo
->dwFontSize
.X
= data
.info
.win_right
- data
.info
.win_left
+ 1;
445 fontinfo
->dwFontSize
.Y
= data
.info
.win_bottom
- data
.info
.win_top
+ 1;
447 size
-= sizeof(data
.info
);
448 if (size
) memcpy( fontinfo
->FaceName
, data
.face_name
, size
);
449 fontinfo
->FaceName
[size
/ sizeof(WCHAR
)] = 0;
450 fontinfo
->FontFamily
= data
.info
.font_pitch_family
;
451 fontinfo
->FontWeight
= data
.info
.font_weight
;
455 BOOL WINAPI
GetCurrentConsoleFont(HANDLE hConsole
, BOOL maxwindow
, CONSOLE_FONT_INFO
*fontinfo
)
458 CONSOLE_FONT_INFOEX res
;
460 res
.cbSize
= sizeof(CONSOLE_FONT_INFOEX
);
462 ret
= GetCurrentConsoleFontEx(hConsole
, maxwindow
, &res
);
465 fontinfo
->nFont
= res
.nFont
;
466 fontinfo
->dwFontSize
.X
= res
.dwFontSize
.X
;
467 fontinfo
->dwFontSize
.Y
= res
.dwFontSize
.Y
;
472 static COORD
get_console_font_size(HANDLE hConsole
, DWORD index
)
474 struct condrv_output_info info
;
477 if (index
>= GetNumberOfConsoleFonts())
479 SetLastError(ERROR_INVALID_PARAMETER
);
483 if (DeviceIoControl( hConsole
, IOCTL_CONDRV_GET_OUTPUT_INFO
, NULL
, 0, &info
, sizeof(info
), NULL
, NULL
))
485 c
.X
= info
.font_width
;
486 c
.Y
= info
.font_height
;
488 else SetLastError( ERROR_INVALID_HANDLE
);
492 #if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
493 #undef GetConsoleFontSize
494 DWORD WINAPI
GetConsoleFontSize(HANDLE hConsole
, DWORD index
)
501 x
.c
= get_console_font_size(hConsole
, index
);
505 COORD WINAPI
GetConsoleFontSize(HANDLE hConsole
, DWORD index
)
507 return get_console_font_size(hConsole
, index
);
509 #endif /* !defined(__i386__) */
511 BOOL WINAPI
GetConsoleFontInfo(HANDLE hConsole
, BOOL maximize
, DWORD numfonts
, CONSOLE_FONT_INFO
*info
)
513 FIXME("(%p %d %u %p): stub!\n", hConsole
, maximize
, numfonts
, info
);
514 SetLastError(LOWORD(E_NOTIMPL
) /* win10 1709+ */);
518 BOOL WINAPI
SetCurrentConsoleFontEx(HANDLE hConsole
, BOOL maxwindow
, CONSOLE_FONT_INFOEX
*cfix
)
520 FIXME("(%p %d %p): stub!\n", hConsole
, maxwindow
, cfix
);
521 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);