dinput/tests: Don't consider extra IOCTL_HID_GET_STRING an error.
[wine.git] / dlls / kernel32 / console.c
blob80f3419cd7aad6ce75b86addee5d05caf12e7dcf
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 "winternl.h"
37 #include "winnls.h"
38 #include "winerror.h"
39 #include "wincon.h"
40 #include "wine/condrv.h"
41 #include "wine/debug.h"
42 #include "kernel_private.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(console);
46 /******************************************************************************
47 * GetConsoleWindow [KERNEL32.@] Get hwnd of the console window.
49 * RETURNS
50 * Success: hwnd of the console window.
51 * Failure: NULL
53 HWND WINAPI GetConsoleWindow(void)
55 condrv_handle_t win;
56 BOOL ret;
58 ret = DeviceIoControl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
59 IOCTL_CONDRV_GET_WINDOW, NULL, 0, &win, sizeof(win), NULL, NULL );
60 return ret ? LongToHandle( win ) : NULL;
64 /******************************************************************
65 * OpenConsoleW (KERNEL32.@)
67 * Undocumented
68 * Open a handle to the current process console.
69 * Returns INVALID_HANDLE_VALUE on failure.
71 HANDLE WINAPI OpenConsoleW(LPCWSTR name, DWORD access, BOOL inherit, DWORD creation)
73 SECURITY_ATTRIBUTES sa;
75 TRACE("(%s, 0x%08x, %d, %u)\n", debugstr_w(name), access, inherit, creation);
77 if (!name || (wcsicmp( L"CONIN$", name ) && wcsicmp( L"CONOUT$", name )) || creation != OPEN_EXISTING)
79 SetLastError( ERROR_INVALID_PARAMETER );
80 return INVALID_HANDLE_VALUE;
83 sa.nLength = sizeof(sa);
84 sa.lpSecurityDescriptor = NULL;
85 sa.bInheritHandle = inherit;
87 return CreateFileW( name, access, FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, creation, 0, NULL );
90 /******************************************************************
91 * VerifyConsoleIoHandle (KERNEL32.@)
93 * Undocumented
95 BOOL WINAPI VerifyConsoleIoHandle(HANDLE handle)
97 IO_STATUS_BLOCK io;
98 DWORD mode;
99 return !NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io, IOCTL_CONDRV_GET_MODE,
100 NULL, 0, &mode, sizeof(mode) );
103 /******************************************************************
104 * DuplicateConsoleHandle (KERNEL32.@)
106 * Undocumented
108 HANDLE WINAPI DuplicateConsoleHandle(HANDLE handle, DWORD access, BOOL inherit,
109 DWORD options)
111 HANDLE ret;
112 return DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(), &ret,
113 access, inherit, options) ? ret : INVALID_HANDLE_VALUE;
116 /******************************************************************
117 * CloseConsoleHandle (KERNEL32.@)
119 * Undocumented
121 BOOL WINAPI CloseConsoleHandle(HANDLE handle)
123 return CloseHandle(handle);
126 /******************************************************************
127 * GetConsoleInputWaitHandle (KERNEL32.@)
129 * Undocumented
131 HANDLE WINAPI GetConsoleInputWaitHandle(void)
133 return GetStdHandle( STD_INPUT_HANDLE );
137 /***********************************************************************
138 * SetConsoleTitleA (KERNEL32.@)
140 BOOL WINAPI SetConsoleTitleA( LPCSTR title )
142 LPWSTR titleW;
143 BOOL ret;
145 DWORD len = MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, NULL, 0 );
146 if (!(titleW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
147 MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, titleW, len );
148 ret = SetConsoleTitleW(titleW);
149 HeapFree(GetProcessHeap(), 0, titleW);
150 return ret;
154 /***********************************************************************
155 * GetConsoleKeyboardLayoutNameA (KERNEL32.@)
157 BOOL WINAPI GetConsoleKeyboardLayoutNameA(LPSTR layoutName)
159 FIXME( "stub %p\n", layoutName);
160 return TRUE;
163 /***********************************************************************
164 * GetConsoleKeyboardLayoutNameW (KERNEL32.@)
166 BOOL WINAPI GetConsoleKeyboardLayoutNameW(LPWSTR layoutName)
168 static int once;
169 if (!once++)
170 FIXME( "stub %p\n", layoutName);
171 return TRUE;
174 /***********************************************************************
175 * GetConsoleTitleA (KERNEL32.@)
177 * See GetConsoleTitleW.
179 DWORD WINAPI GetConsoleTitleA(LPSTR title, DWORD size)
181 WCHAR *ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * size);
182 DWORD ret;
184 if (!ptr) return 0;
185 ret = GetConsoleTitleW( ptr, size );
186 if (ret)
188 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, ret + 1, title, size, NULL, NULL);
189 ret = strlen(title);
191 HeapFree(GetProcessHeap(), 0, ptr);
192 return ret;
196 /***********************************************************************
197 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
199 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
201 FIXME("(%p): stub\n", nrofbuttons);
202 *nrofbuttons = 2;
203 return TRUE;
207 /******************************************************************
208 * GetConsoleDisplayMode (KERNEL32.@)
210 BOOL WINAPI GetConsoleDisplayMode(LPDWORD lpModeFlags)
212 TRACE("semi-stub: %p\n", lpModeFlags);
213 /* It is safe to successfully report windowed mode */
214 *lpModeFlags = 0;
215 return TRUE;
218 /******************************************************************
219 * SetConsoleDisplayMode (KERNEL32.@)
221 BOOL WINAPI SetConsoleDisplayMode(HANDLE hConsoleOutput, DWORD dwFlags,
222 COORD *lpNewScreenBufferDimensions)
224 TRACE("(%p, %x, (%d, %d))\n", hConsoleOutput, dwFlags,
225 lpNewScreenBufferDimensions->X, lpNewScreenBufferDimensions->Y);
226 if (dwFlags == 1)
228 /* We cannot switch to fullscreen */
229 return FALSE;
231 return TRUE;
234 /******************************************************************
235 * GetConsoleAliasW
238 * RETURNS
239 * 0 if an error occurred, non-zero for success
242 DWORD WINAPI GetConsoleAliasW(LPWSTR lpSource, LPWSTR lpTargetBuffer,
243 DWORD TargetBufferLength, LPWSTR lpExename)
245 FIXME("(%s,%p,%d,%s): stub\n", debugstr_w(lpSource), lpTargetBuffer, TargetBufferLength, debugstr_w(lpExename));
246 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
247 return 0;
250 /******************************************************************
251 * GetConsoleProcessList (KERNEL32.@)
253 DWORD WINAPI GetConsoleProcessList(LPDWORD processlist, DWORD processcount)
255 FIXME("(%p,%d): stub\n", processlist, processcount);
257 if (!processlist || processcount < 1)
259 SetLastError(ERROR_INVALID_PARAMETER);
260 return 0;
263 return 0;
266 /* Undocumented, called by native doskey.exe */
267 DWORD WINAPI GetConsoleCommandHistoryA(DWORD unknown1, DWORD unknown2, DWORD unknown3)
269 FIXME(": (0x%x, 0x%x, 0x%x) stub!\n", unknown1, unknown2, unknown3);
270 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
271 return 0;
274 /* Undocumented, called by native doskey.exe */
275 DWORD WINAPI GetConsoleCommandHistoryW(DWORD unknown1, DWORD unknown2, DWORD unknown3)
277 FIXME(": (0x%x, 0x%x, 0x%x) stub!\n", unknown1, unknown2, unknown3);
278 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
279 return 0;
282 /* Undocumented, called by native doskey.exe */
283 DWORD WINAPI GetConsoleCommandHistoryLengthA(LPCSTR unknown)
285 FIXME(": (%s) stub!\n", debugstr_a(unknown));
286 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
287 return 0;
290 /* Undocumented, called by native doskey.exe */
291 DWORD WINAPI GetConsoleCommandHistoryLengthW(LPCWSTR unknown)
293 FIXME(": (%s) stub!\n", debugstr_w(unknown));
294 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
295 return 0;
298 DWORD WINAPI GetConsoleAliasesLengthA(LPSTR unknown)
300 FIXME(": (%s) stub!\n", debugstr_a(unknown));
301 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
302 return 0;
305 DWORD WINAPI GetConsoleAliasesLengthW(LPWSTR unknown)
307 FIXME(": (%s) stub!\n", debugstr_w(unknown));
308 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
309 return 0;
312 DWORD WINAPI GetConsoleAliasExesLengthA(void)
314 FIXME(": stub!\n");
315 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
316 return 0;
319 DWORD WINAPI GetConsoleAliasExesLengthW(void)
321 FIXME(": stub!\n");
322 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
323 return 0;
326 VOID WINAPI ExpungeConsoleCommandHistoryA(LPCSTR unknown)
328 FIXME(": (%s) stub!\n", debugstr_a(unknown));
329 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
332 VOID WINAPI ExpungeConsoleCommandHistoryW(LPCWSTR unknown)
334 FIXME(": (%s) stub!\n", debugstr_w(unknown));
335 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
338 BOOL WINAPI AddConsoleAliasA(LPSTR source, LPSTR target, LPSTR exename)
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;
345 BOOL WINAPI AddConsoleAliasW(LPWSTR source, LPWSTR target, LPWSTR exename)
347 FIXME(": (%s, %s, %s) stub!\n", debugstr_w(source), debugstr_w(target), debugstr_w(exename));
348 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
349 return FALSE;
353 BOOL WINAPI SetConsoleIcon(HICON icon)
355 FIXME(": (%p) stub!\n", icon);
356 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
357 return FALSE;
360 DWORD WINAPI GetNumberOfConsoleFonts(void)
362 return 1;
365 BOOL WINAPI SetConsoleFont(HANDLE hConsole, DWORD index)
367 FIXME("(%p, %u): stub!\n", hConsole, index);
368 SetLastError(LOWORD(E_NOTIMPL) /* win10 1709+ */);
369 return FALSE;
372 BOOL WINAPI SetConsoleKeyShortcuts(BOOL set, BYTE keys, VOID *a, DWORD b)
374 FIXME(": (%u %u %p %u) stub!\n", set, keys, a, b);
375 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
376 return FALSE;
380 BOOL WINAPI GetCurrentConsoleFontEx(HANDLE hConsole, BOOL maxwindow, CONSOLE_FONT_INFOEX *fontinfo)
382 DWORD size;
383 struct
385 struct condrv_output_info info;
386 WCHAR face_name[LF_FACESIZE - 1];
387 } data;
389 if (fontinfo->cbSize != sizeof(CONSOLE_FONT_INFOEX))
391 SetLastError(ERROR_INVALID_PARAMETER);
392 return FALSE;
395 if (!DeviceIoControl( hConsole, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0,
396 &data, sizeof(data), &size, NULL ))
398 SetLastError( ERROR_INVALID_HANDLE );
399 return FALSE;
402 fontinfo->nFont = 0;
403 if (maxwindow)
405 fontinfo->dwFontSize.X = min( data.info.width, data.info.max_width );
406 fontinfo->dwFontSize.Y = min( data.info.height, data.info.max_height );
408 else
410 fontinfo->dwFontSize.X = data.info.font_width;
411 fontinfo->dwFontSize.Y = data.info.font_height;
413 size -= sizeof(data.info);
414 if (size) memcpy( fontinfo->FaceName, data.face_name, size );
415 fontinfo->FaceName[size / sizeof(WCHAR)] = 0;
416 fontinfo->FontFamily = data.info.font_pitch_family;
417 fontinfo->FontWeight = data.info.font_weight;
418 return TRUE;
421 BOOL WINAPI GetCurrentConsoleFont(HANDLE hConsole, BOOL maxwindow, CONSOLE_FONT_INFO *fontinfo)
423 BOOL ret;
424 CONSOLE_FONT_INFOEX res;
426 res.cbSize = sizeof(CONSOLE_FONT_INFOEX);
428 ret = GetCurrentConsoleFontEx(hConsole, maxwindow, &res);
429 if(ret)
431 fontinfo->nFont = res.nFont;
432 fontinfo->dwFontSize.X = res.dwFontSize.X;
433 fontinfo->dwFontSize.Y = res.dwFontSize.Y;
435 return ret;
438 static COORD get_console_font_size(HANDLE hConsole, DWORD index)
440 struct condrv_output_info info;
441 COORD c = {0,0};
443 if (index >= GetNumberOfConsoleFonts())
445 SetLastError(ERROR_INVALID_PARAMETER);
446 return c;
449 if (DeviceIoControl( hConsole, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &info, sizeof(info), NULL, NULL ))
451 c.X = info.font_width;
452 c.Y = info.font_height;
454 else SetLastError( ERROR_INVALID_HANDLE );
455 return c;
458 #if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
459 #undef GetConsoleFontSize
460 DWORD WINAPI GetConsoleFontSize(HANDLE hConsole, DWORD index)
462 union {
463 COORD c;
464 DWORD w;
465 } x;
467 x.c = get_console_font_size(hConsole, index);
468 return x.w;
470 #else
471 COORD WINAPI GetConsoleFontSize(HANDLE hConsole, DWORD index)
473 return get_console_font_size(hConsole, index);
475 #endif /* !defined(__i386__) */
477 BOOL WINAPI GetConsoleFontInfo(HANDLE hConsole, BOOL maximize, DWORD numfonts, CONSOLE_FONT_INFO *info)
479 FIXME("(%p %d %u %p): stub!\n", hConsole, maximize, numfonts, info);
480 SetLastError(LOWORD(E_NOTIMPL) /* win10 1709+ */);
481 return FALSE;
484 BOOL WINAPI SetCurrentConsoleFontEx(HANDLE hConsole, BOOL maxwindow, CONSOLE_FONT_INFOEX *cfix)
486 FIXME("(%p %d %p): stub!\n", hConsole, maxwindow, cfix);
487 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
488 return FALSE;