hidclass.sys: Return STATUS_INVALID_USER_BUFFER if buffer_len is 0.
[wine.git] / dlls / user32 / user_main.c
blob303f19accfcecbcea982b8d634a74311a783acb2
1 /*
2 * USER initialization code
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
29 #include "controls.h"
30 #include "user_private.h"
31 #include "win.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
36 #define DESKTOP_ALL_ACCESS 0x01ff
38 HMODULE user32_module = 0;
40 static CRITICAL_SECTION user_section;
41 static CRITICAL_SECTION_DEBUG critsect_debug =
43 0, 0, &user_section,
44 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
45 0, 0, { (DWORD_PTR)(__FILE__ ": user_section") }
47 static CRITICAL_SECTION user_section = { &critsect_debug, -1, 0, 0, 0, 0 };
49 static HPALETTE (WINAPI *pfnGDISelectPalette)( HDC hdc, HPALETTE hpal, WORD bkgnd );
50 static UINT (WINAPI *pfnGDIRealizePalette)( HDC hdc );
51 static HPALETTE hPrimaryPalette;
53 static DWORD exiting_thread_id;
55 extern void WDML_NotifyThreadDetach(void);
57 #ifdef __MINGW32__
58 /* work around a Mingw build issue where _wassert causes a duplicate reference to MessageBoxW */
59 void __cdecl _wassert( const WCHAR *msg, const WCHAR *file, unsigned line) { abort(); }
60 #endif
62 /***********************************************************************
63 * USER_Lock
65 void USER_Lock(void)
67 EnterCriticalSection( &user_section );
71 /***********************************************************************
72 * USER_Unlock
74 void USER_Unlock(void)
76 LeaveCriticalSection( &user_section );
80 /***********************************************************************
81 * USER_CheckNotLock
83 * Make sure that we don't hold the user lock.
85 void USER_CheckNotLock(void)
87 if (RtlIsCriticalSectionLockedByThread(&user_section))
89 ERR( "BUG: holding USER lock\n" );
90 DebugBreak();
95 /***********************************************************************
96 * UserSelectPalette (Not a Windows API)
98 static HPALETTE WINAPI UserSelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
100 WORD wBkgPalette = 1;
102 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
104 HWND hwnd = WindowFromDC( hDC );
105 if (hwnd)
107 HWND hForeground = GetForegroundWindow();
108 /* set primary palette if it's related to current active */
109 if (hForeground == hwnd || IsChild(hForeground,hwnd))
111 wBkgPalette = 0;
112 hPrimaryPalette = hPal;
116 return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
120 /***********************************************************************
121 * UserRealizePalette (USER32.@)
123 UINT WINAPI UserRealizePalette( HDC hDC )
125 UINT realized = pfnGDIRealizePalette( hDC );
127 /* do not send anything if no colors were changed */
128 if (realized && GetCurrentObject( hDC, OBJ_PAL ) == hPrimaryPalette)
130 /* send palette change notification */
131 HWND hWnd = WindowFromDC( hDC );
132 if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
133 SMTO_ABORTIFHUNG, 2000, NULL );
135 return realized;
139 /***********************************************************************
140 * palette_init
142 * Patch the function pointers in GDI for SelectPalette and RealizePalette
144 static void palette_init(void)
146 void **ptr;
147 HMODULE module = GetModuleHandleA( "gdi32" );
148 if (!module)
150 ERR( "cannot get GDI32 handle\n" );
151 return;
153 if ((ptr = (void**)GetProcAddress( module, "pfnSelectPalette" )))
154 pfnGDISelectPalette = InterlockedExchangePointer( ptr, UserSelectPalette );
155 else ERR( "cannot find pfnSelectPalette in GDI32\n" );
156 if ((ptr = (void**)GetProcAddress( module, "pfnRealizePalette" )))
157 pfnGDIRealizePalette = InterlockedExchangePointer( ptr, UserRealizePalette );
158 else ERR( "cannot find pfnRealizePalette in GDI32\n" );
162 /***********************************************************************
163 * get_default_desktop
165 * Get the name of the desktop to use for this app if not specified explicitly.
167 static const WCHAR *get_default_desktop(void)
169 static WCHAR buffer[MAX_PATH + ARRAY_SIZE(L"\\Explorer")];
170 WCHAR *p, *appname = buffer;
171 const WCHAR *ret = NULL;
172 DWORD len;
173 HKEY tmpkey, appkey;
175 len = (GetModuleFileNameW( 0, buffer, MAX_PATH ));
176 if (!len || len >= MAX_PATH) return L"Default";
177 if ((p = wcsrchr( appname, '/' ))) appname = p + 1;
178 if ((p = wcsrchr( appname, '\\' ))) appname = p + 1;
179 p = appname + lstrlenW(appname);
180 lstrcpyW( p, L"\\Explorer" );
182 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Explorer */
183 if (!RegOpenKeyW( HKEY_CURRENT_USER, L"Software\\Wine\\AppDefaults", &tmpkey ))
185 if (RegOpenKeyW( tmpkey, appname, &appkey )) appkey = 0;
186 RegCloseKey( tmpkey );
187 if (appkey)
189 len = sizeof(buffer);
190 if (!RegQueryValueExW( appkey, L"Desktop", 0, NULL, (LPBYTE)buffer, &len )) ret = buffer;
191 RegCloseKey( appkey );
192 if (ret && *ret) return ret;
193 ret = NULL;
197 /* @@ Wine registry key: HKCU\Software\Wine\Explorer */
198 if (!RegOpenKeyW( HKEY_CURRENT_USER, L"Software\\Wine\\Explorer", &appkey ))
200 len = sizeof(buffer);
201 if (!RegQueryValueExW( appkey, L"Desktop", 0, NULL, (LPBYTE)buffer, &len )) ret = buffer;
202 RegCloseKey( appkey );
203 if (ret && *ret) return ret;
205 return L"Default";
209 /***********************************************************************
210 * dpiaware_init
212 * Initialize the DPI awareness style.
214 static void dpiaware_init(void)
216 WCHAR buffer[256];
217 DWORD option;
219 if (!LdrQueryImageFileExecutionOptions( &NtCurrentTeb()->Peb->ProcessParameters->ImagePathName,
220 L"dpiAwareness", REG_DWORD, &option, sizeof(option), NULL ))
222 TRACE( "got option %x\n", option );
223 if (option <= 2)
225 SetProcessDpiAwarenessContext( (DPI_AWARENESS_CONTEXT)~(ULONG_PTR)option );
226 return;
230 if (QueryActCtxSettingsW( 0, NULL, L"http://schemas.microsoft.com/SMI/2016/WindowsSettings",
231 L"dpiAwareness", buffer, ARRAY_SIZE(buffer), NULL ))
233 static const WCHAR * const types[] = { L"unaware", L"system", L"permonitor", L"permonitorv2" };
234 WCHAR *p, *start, *end;
235 ULONG_PTR i;
237 TRACE( "got dpiAwareness=%s\n", debugstr_w(buffer) );
238 for (start = buffer; *start; start = end)
240 start += wcsspn( start, L" \t\r\n" );
241 if (!(end = wcschr( start, ',' ))) end = start + lstrlenW(start);
242 else *end++ = 0;
243 if ((p = wcspbrk( start, L" \t\r\n" ))) *p = 0;
244 for (i = 0; i < ARRAY_SIZE(types); i++)
246 if (wcsicmp( start, types[i] )) continue;
247 SetProcessDpiAwarenessContext( (DPI_AWARENESS_CONTEXT)~i );
248 return;
252 else if (QueryActCtxSettingsW( 0, NULL, L"http://schemas.microsoft.com/SMI/2005/WindowsSettings",
253 L"dpiAware", buffer, ARRAY_SIZE(buffer), NULL ))
255 TRACE( "got dpiAware=%s\n", debugstr_w(buffer) );
256 if (!wcsicmp( buffer, L"true" ))
257 SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT_SYSTEM_AWARE );
258 else if (!wcsicmp( buffer, L"true/pm" ) || !wcsicmp( buffer, L"per monitor" ))
259 SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE );
260 else
261 SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT_UNAWARE );
266 /***********************************************************************
267 * winstation_init
269 * Connect to the process window station and desktop.
271 static void winstation_init(void)
273 STARTUPINFOW info;
274 WCHAR *winstation = NULL, *desktop = NULL, *buffer = NULL;
275 HANDLE handle;
277 GetStartupInfoW( &info );
278 if (info.lpDesktop && *info.lpDesktop)
280 buffer = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(info.lpDesktop) + 1) * sizeof(WCHAR) );
281 lstrcpyW( buffer, info.lpDesktop );
282 if ((desktop = wcschr( buffer, '\\' )))
284 *desktop++ = 0;
285 winstation = buffer;
287 else desktop = buffer;
290 /* set winstation if explicitly specified, or if we don't have one yet */
291 if (buffer || !GetProcessWindowStation())
293 handle = CreateWindowStationW( winstation ? winstation : L"WinSta0", 0, WINSTA_ALL_ACCESS, NULL );
294 if (handle)
296 SetProcessWindowStation( handle );
297 /* only WinSta0 is visible */
298 if (!winstation || !wcsicmp( winstation, L"WinSta0" ))
300 USEROBJECTFLAGS flags;
301 flags.fInherit = FALSE;
302 flags.fReserved = FALSE;
303 flags.dwFlags = WSF_VISIBLE;
304 SetUserObjectInformationW( handle, UOI_FLAGS, &flags, sizeof(flags) );
308 if (buffer || !GetThreadDesktop( GetCurrentThreadId() ))
310 handle = CreateDesktopW( desktop ? desktop : get_default_desktop(),
311 NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
312 if (handle) SetThreadDesktop( handle );
314 HeapFree( GetProcessHeap(), 0, buffer );
316 register_desktop_class();
320 /***********************************************************************
321 * USER initialisation routine
323 static BOOL process_attach(void)
325 dpiaware_init();
326 winstation_init();
328 /* Initialize system colors and metrics */
329 SYSPARAMS_Init();
331 /* Setup palette function pointers */
332 palette_init();
334 keyboard_init();
335 return TRUE;
339 /**********************************************************************
340 * USER_IsExitingThread
342 BOOL USER_IsExitingThread( DWORD tid )
344 return (tid == exiting_thread_id);
348 /**********************************************************************
349 * thread_detach
351 static void thread_detach(void)
353 struct user_thread_info *thread_info = get_user_thread_info();
355 exiting_thread_id = GetCurrentThreadId();
357 WDML_NotifyThreadDetach();
358 USER_Driver->pThreadDetach();
360 destroy_thread_windows();
361 CloseHandle( thread_info->server_queue );
362 HeapFree( GetProcessHeap(), 0, thread_info->wmchar_data );
363 HeapFree( GetProcessHeap(), 0, thread_info->key_state );
364 HeapFree( GetProcessHeap(), 0, thread_info->rawinput );
366 exiting_thread_id = 0;
370 /***********************************************************************
371 * UserClientDllInitialize (USER32.@)
373 * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
375 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
377 static HMODULE imm32_module;
378 BOOL ret = TRUE;
380 switch(reason)
382 case DLL_PROCESS_ATTACH:
383 user32_module = inst;
384 ret = process_attach();
385 if(ret)
386 imm32_module = LoadLibraryW(L"imm32.dll");
387 break;
388 case DLL_THREAD_DETACH:
389 thread_detach();
390 break;
391 case DLL_PROCESS_DETACH:
392 USER_unload_driver();
393 FreeLibrary(imm32_module);
394 DeleteCriticalSection(&user_section);
395 break;
397 return ret;
401 /***********************************************************************
402 * ExitWindowsEx (USER32.@)
404 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reason )
406 WCHAR app[MAX_PATH];
407 WCHAR cmdline[MAX_PATH + 64];
408 PROCESS_INFORMATION pi;
409 STARTUPINFOW si;
410 void *redir;
412 GetSystemDirectoryW( app, MAX_PATH - ARRAY_SIZE( L"\\wineboot.exe" ));
413 lstrcatW( app, L"\\wineboot.exe" );
414 lstrcpyW( cmdline, app );
416 if (flags & EWX_FORCE) lstrcatW( cmdline, L" --kill" );
417 else
419 lstrcatW( cmdline, L" --end-session" );
420 if (flags & EWX_FORCEIFHUNG) lstrcatW( cmdline, L" --force" );
422 if (!(flags & EWX_REBOOT)) lstrcatW( cmdline, L" --shutdown" );
424 memset( &si, 0, sizeof si );
425 si.cb = sizeof si;
426 Wow64DisableWow64FsRedirection( &redir );
427 if (!CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi ))
429 Wow64RevertWow64FsRedirection( redir );
430 ERR( "Failed to run %s\n", debugstr_w(cmdline) );
431 return FALSE;
433 Wow64RevertWow64FsRedirection( redir );
434 CloseHandle( pi.hProcess );
435 CloseHandle( pi.hThread );
436 return TRUE;
439 /***********************************************************************
440 * LockWorkStation (USER32.@)
442 BOOL WINAPI LockWorkStation(void)
444 TRACE(": stub\n");
445 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
446 return FALSE;
449 /***********************************************************************
450 * RegisterServicesProcess (USER32.@)
452 int WINAPI RegisterServicesProcess(DWORD ServicesProcessId)
454 FIXME("(0x%x): stub\n", ServicesProcessId);
455 return 0;
458 /***********************************************************************
459 * ShutdownBlockReasonCreate (USER32.@)
461 BOOL WINAPI ShutdownBlockReasonCreate(HWND hwnd, LPCWSTR reason)
463 FIXME("(%p, %s): stub\n", hwnd, debugstr_w(reason));
464 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
465 return FALSE;
468 /***********************************************************************
469 * ShutdownBlockReasonDestroy (USER32.@)
471 BOOL WINAPI ShutdownBlockReasonDestroy(HWND hwnd)
473 FIXME("(%p): stub\n", hwnd);
474 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
475 return FALSE;