ntdll: Fix SMT CPU flag reporting.
[wine/zf.git] / dlls / kernel32 / console.c
blob3171dcd8e7afae0f358b332749775bd178456336
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 /******************************************************************************
49 * GetConsoleWindow [KERNEL32.@] Get hwnd of the console window.
51 * RETURNS
52 * Success: hwnd of the console window.
53 * Failure: NULL
55 HWND WINAPI GetConsoleWindow(void)
57 struct condrv_input_info info;
58 BOOL ret;
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.@)
69 * Undocumented
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.@)
95 * Undocumented
97 BOOL WINAPI VerifyConsoleIoHandle(HANDLE handle)
99 IO_STATUS_BLOCK io;
100 DWORD mode;
101 return !NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io, IOCTL_CONDRV_GET_MODE,
102 NULL, 0, &mode, sizeof(mode) );
105 /******************************************************************
106 * DuplicateConsoleHandle (KERNEL32.@)
108 * Undocumented
110 HANDLE WINAPI DuplicateConsoleHandle(HANDLE handle, DWORD access, BOOL inherit,
111 DWORD options)
113 HANDLE ret;
114 return DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(), &ret,
115 access, inherit, options) ? ret : INVALID_HANDLE_VALUE;
118 /******************************************************************
119 * CloseConsoleHandle (KERNEL32.@)
121 * Undocumented
123 BOOL WINAPI CloseConsoleHandle(HANDLE handle)
125 return CloseHandle(handle);
128 /******************************************************************
129 * GetConsoleInputWaitHandle (KERNEL32.@)
131 * Undocumented
133 HANDLE WINAPI GetConsoleInputWaitHandle(void)
135 return GetStdHandle( STD_INPUT_HANDLE );
139 /***********************************************************************
140 * SetConsoleTitleA (KERNEL32.@)
142 BOOL WINAPI SetConsoleTitleA( LPCSTR title )
144 LPWSTR titleW;
145 BOOL ret;
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);
152 return ret;
156 /***********************************************************************
157 * GetConsoleKeyboardLayoutNameA (KERNEL32.@)
159 BOOL WINAPI GetConsoleKeyboardLayoutNameA(LPSTR layoutName)
161 FIXME( "stub %p\n", layoutName);
162 return TRUE;
165 /***********************************************************************
166 * GetConsoleKeyboardLayoutNameW (KERNEL32.@)
168 BOOL WINAPI GetConsoleKeyboardLayoutNameW(LPWSTR layoutName)
170 static int once;
171 if (!once++)
172 FIXME( "stub %p\n", layoutName);
173 return TRUE;
176 /***********************************************************************
177 * GetConsoleTitleA (KERNEL32.@)
179 * See GetConsoleTitleW.
181 DWORD WINAPI GetConsoleTitleA(LPSTR title, DWORD size)
183 WCHAR *ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * size);
184 DWORD ret;
186 if (!ptr) return 0;
187 ret = GetConsoleTitleW( ptr, size );
188 if (ret)
190 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, ret + 1, title, size, NULL, NULL);
191 ret = strlen(title);
193 HeapFree(GetProcessHeap(), 0, ptr);
194 return ret;
198 /***********************************************************************
199 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
201 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
203 FIXME("(%p): stub\n", nrofbuttons);
204 *nrofbuttons = 2;
205 return TRUE;
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 */
216 *lpModeFlags = 0;
217 return TRUE;
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);
228 if (dwFlags == 1)
230 /* We cannot switch to fullscreen */
231 return FALSE;
233 return TRUE;
236 /******************************************************************
237 * GetConsoleAliasW
240 * RETURNS
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);
249 return 0;
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);
262 return 0;
265 return 0;
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);
290 return TRUE;
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);
299 return 0;
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);
308 return 0;
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);
317 return 0;
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);
326 return 0;
329 DWORD WINAPI GetConsoleAliasesLengthA(LPSTR unknown)
331 FIXME(": (%s) stub!\n", debugstr_a(unknown));
332 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
333 return 0;
336 DWORD WINAPI GetConsoleAliasesLengthW(LPWSTR unknown)
338 FIXME(": (%s) stub!\n", debugstr_w(unknown));
339 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
340 return 0;
343 DWORD WINAPI GetConsoleAliasExesLengthA(void)
345 FIXME(": stub!\n");
346 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
347 return 0;
350 DWORD WINAPI GetConsoleAliasExesLengthW(void)
352 FIXME(": stub!\n");
353 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
354 return 0;
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);
373 return FALSE;
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);
380 return FALSE;
384 BOOL WINAPI SetConsoleIcon(HICON icon)
386 FIXME(": (%p) stub!\n", icon);
387 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
388 return FALSE;
391 DWORD WINAPI GetNumberOfConsoleFonts(void)
393 return 1;
396 BOOL WINAPI SetConsoleFont(HANDLE hConsole, DWORD index)
398 FIXME("(%p, %u): stub!\n", hConsole, index);
399 SetLastError(LOWORD(E_NOTIMPL) /* win10 1709+ */);
400 return FALSE;
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);
407 return FALSE;
411 BOOL WINAPI GetCurrentConsoleFontEx(HANDLE hConsole, BOOL maxwindow, CONSOLE_FONT_INFOEX *fontinfo)
413 DWORD size;
414 struct
416 struct condrv_output_info info;
417 WCHAR face_name[LF_FACESIZE - 1];
418 } data;
420 if (fontinfo->cbSize != sizeof(CONSOLE_FONT_INFOEX))
422 SetLastError(ERROR_INVALID_PARAMETER);
423 return FALSE;
426 if (!DeviceIoControl( hConsole, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0,
427 &data, sizeof(data), &size, NULL ))
429 SetLastError( ERROR_INVALID_HANDLE );
430 return FALSE;
433 fontinfo->nFont = 0;
434 if (maxwindow)
436 fontinfo->dwFontSize.X = min( data.info.width, data.info.max_width );
437 fontinfo->dwFontSize.Y = min( data.info.height, data.info.max_height );
439 else
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;
449 return TRUE;
452 BOOL WINAPI GetCurrentConsoleFont(HANDLE hConsole, BOOL maxwindow, CONSOLE_FONT_INFO *fontinfo)
454 BOOL ret;
455 CONSOLE_FONT_INFOEX res;
457 res.cbSize = sizeof(CONSOLE_FONT_INFOEX);
459 ret = GetCurrentConsoleFontEx(hConsole, maxwindow, &res);
460 if(ret)
462 fontinfo->nFont = res.nFont;
463 fontinfo->dwFontSize.X = res.dwFontSize.X;
464 fontinfo->dwFontSize.Y = res.dwFontSize.Y;
466 return ret;
469 static COORD get_console_font_size(HANDLE hConsole, DWORD index)
471 struct condrv_output_info info;
472 COORD c = {0,0};
474 if (index >= GetNumberOfConsoleFonts())
476 SetLastError(ERROR_INVALID_PARAMETER);
477 return c;
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 );
486 return c;
489 #if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
490 #undef GetConsoleFontSize
491 DWORD WINAPI GetConsoleFontSize(HANDLE hConsole, DWORD index)
493 union {
494 COORD c;
495 DWORD w;
496 } x;
498 x.c = get_console_font_size(hConsole, index);
499 return x.w;
501 #else
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+ */);
512 return FALSE;
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);
519 return FALSE;