mf/session: Implement support for sinks that provide sample allocators.
[wine.git] / dlls / kernel32 / console.c
blobd55c589eaf7a78a59f79aed4ea0ecacb96331a57
1 /*
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
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <limits.h>
31 #define NONAMELESSUNION
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 "wine/condrv.h"
40 #include "wine/server.h"
41 #include "wine/exception.h"
42 #include "wine/debug.h"
43 #include "excpt.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.
54 * RETURNS
55 * Success: hwnd of the console window.
56 * Failure: NULL
58 HWND WINAPI GetConsoleWindow(void)
60 struct condrv_input_info info;
61 BOOL ret;
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.@)
72 * Undocumented
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.@)
98 * Undocumented
100 BOOL WINAPI VerifyConsoleIoHandle(HANDLE handle)
102 IO_STATUS_BLOCK io;
103 DWORD mode;
104 return !NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io, IOCTL_CONDRV_GET_MODE,
105 NULL, 0, &mode, sizeof(mode) );
108 /******************************************************************
109 * DuplicateConsoleHandle (KERNEL32.@)
111 * Undocumented
113 HANDLE WINAPI DuplicateConsoleHandle(HANDLE handle, DWORD access, BOOL inherit,
114 DWORD options)
116 HANDLE ret;
117 return DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(), &ret,
118 access, inherit, options) ? ret : INVALID_HANDLE_VALUE;
121 /******************************************************************
122 * CloseConsoleHandle (KERNEL32.@)
124 * Undocumented
126 BOOL WINAPI CloseConsoleHandle(HANDLE handle)
128 return CloseHandle(handle);
131 /******************************************************************
132 * GetConsoleInputWaitHandle (KERNEL32.@)
134 * Undocumented
136 HANDLE WINAPI GetConsoleInputWaitHandle(void)
138 return GetStdHandle( STD_INPUT_HANDLE );
142 /***********************************************************************
143 * SetConsoleTitleA (KERNEL32.@)
145 BOOL WINAPI SetConsoleTitleA( LPCSTR title )
147 LPWSTR titleW;
148 BOOL ret;
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);
155 return ret;
159 /***********************************************************************
160 * GetConsoleKeyboardLayoutNameA (KERNEL32.@)
162 BOOL WINAPI GetConsoleKeyboardLayoutNameA(LPSTR layoutName)
164 FIXME( "stub %p\n", layoutName);
165 return TRUE;
168 /***********************************************************************
169 * GetConsoleKeyboardLayoutNameW (KERNEL32.@)
171 BOOL WINAPI GetConsoleKeyboardLayoutNameW(LPWSTR layoutName)
173 static int once;
174 if (!once++)
175 FIXME( "stub %p\n", layoutName);
176 return TRUE;
179 /***********************************************************************
180 * GetConsoleTitleA (KERNEL32.@)
182 * See GetConsoleTitleW.
184 DWORD WINAPI GetConsoleTitleA(LPSTR title, DWORD size)
186 WCHAR *ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * size);
187 DWORD ret;
189 if (!ptr) return 0;
190 ret = GetConsoleTitleW( ptr, size );
191 if (ret)
193 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, ret + 1, title, size, NULL, NULL);
194 ret = strlen(title);
196 HeapFree(GetProcessHeap(), 0, ptr);
197 return ret;
201 /***********************************************************************
202 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
204 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
206 FIXME("(%p): stub\n", nrofbuttons);
207 *nrofbuttons = 2;
208 return TRUE;
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 */
219 *lpModeFlags = 0;
220 return TRUE;
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);
231 if (dwFlags == 1)
233 /* We cannot switch to fullscreen */
234 return FALSE;
236 return TRUE;
239 /******************************************************************
240 * GetConsoleAliasW
243 * RETURNS
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);
252 return 0;
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);
265 return 0;
268 return 0;
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);
293 return TRUE;
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);
302 return 0;
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);
311 return 0;
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);
320 return 0;
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);
329 return 0;
332 DWORD WINAPI GetConsoleAliasesLengthA(LPSTR unknown)
334 FIXME(": (%s) stub!\n", debugstr_a(unknown));
335 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
336 return 0;
339 DWORD WINAPI GetConsoleAliasesLengthW(LPWSTR unknown)
341 FIXME(": (%s) stub!\n", debugstr_w(unknown));
342 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
343 return 0;
346 DWORD WINAPI GetConsoleAliasExesLengthA(void)
348 FIXME(": stub!\n");
349 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
350 return 0;
353 DWORD WINAPI GetConsoleAliasExesLengthW(void)
355 FIXME(": stub!\n");
356 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
357 return 0;
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);
376 return FALSE;
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);
383 return FALSE;
387 BOOL WINAPI SetConsoleIcon(HICON icon)
389 FIXME(": (%p) stub!\n", icon);
390 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
391 return FALSE;
394 DWORD WINAPI GetNumberOfConsoleFonts(void)
396 return 1;
399 BOOL WINAPI SetConsoleFont(HANDLE hConsole, DWORD index)
401 FIXME("(%p, %u): stub!\n", hConsole, index);
402 SetLastError(LOWORD(E_NOTIMPL) /* win10 1709+ */);
403 return FALSE;
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);
410 return FALSE;
414 BOOL WINAPI GetCurrentConsoleFontEx(HANDLE hConsole, BOOL maxwindow, CONSOLE_FONT_INFOEX *fontinfo)
416 DWORD size;
417 struct
419 struct condrv_output_info info;
420 WCHAR face_name[LF_FACESIZE - 1];
421 } data;
423 if (fontinfo->cbSize != sizeof(CONSOLE_FONT_INFOEX))
425 SetLastError(ERROR_INVALID_PARAMETER);
426 return FALSE;
429 if (!DeviceIoControl( hConsole, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0,
430 &data, sizeof(data), &size, NULL ))
432 SetLastError( ERROR_INVALID_HANDLE );
433 return FALSE;
436 fontinfo->nFont = 0;
437 if (maxwindow)
439 fontinfo->dwFontSize.X = min( data.info.width, data.info.max_width );
440 fontinfo->dwFontSize.Y = min( data.info.height, data.info.max_height );
442 else
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;
452 return TRUE;
455 BOOL WINAPI GetCurrentConsoleFont(HANDLE hConsole, BOOL maxwindow, CONSOLE_FONT_INFO *fontinfo)
457 BOOL ret;
458 CONSOLE_FONT_INFOEX res;
460 res.cbSize = sizeof(CONSOLE_FONT_INFOEX);
462 ret = GetCurrentConsoleFontEx(hConsole, maxwindow, &res);
463 if(ret)
465 fontinfo->nFont = res.nFont;
466 fontinfo->dwFontSize.X = res.dwFontSize.X;
467 fontinfo->dwFontSize.Y = res.dwFontSize.Y;
469 return ret;
472 static COORD get_console_font_size(HANDLE hConsole, DWORD index)
474 struct condrv_output_info info;
475 COORD c = {0,0};
477 if (index >= GetNumberOfConsoleFonts())
479 SetLastError(ERROR_INVALID_PARAMETER);
480 return c;
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 );
489 return c;
492 #if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
493 #undef GetConsoleFontSize
494 DWORD WINAPI GetConsoleFontSize(HANDLE hConsole, DWORD index)
496 union {
497 COORD c;
498 DWORD w;
499 } x;
501 x.c = get_console_font_size(hConsole, index);
502 return x.w;
504 #else
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+ */);
515 return FALSE;
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);
522 return FALSE;