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
30 #include "user_private.h"
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(graphics
);
37 #define DESKTOP_ALL_ACCESS 0x01ff
39 HMODULE user32_module
= 0;
41 static CRITICAL_SECTION user_section
;
42 static CRITICAL_SECTION_DEBUG critsect_debug
=
45 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
46 0, 0, { (DWORD_PTR
)(__FILE__
": user_section") }
48 static CRITICAL_SECTION user_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
50 static HPALETTE (WINAPI
*pfnGDISelectPalette
)( HDC hdc
, HPALETTE hpal
, WORD bkgnd
);
51 static UINT (WINAPI
*pfnGDIRealizePalette
)( HDC hdc
);
52 static HPALETTE hPrimaryPalette
;
54 static DWORD exiting_thread_id
;
56 extern void WDML_NotifyThreadDetach(void);
59 /***********************************************************************
64 EnterCriticalSection( &user_section
);
68 /***********************************************************************
71 void USER_Unlock(void)
73 LeaveCriticalSection( &user_section
);
77 /***********************************************************************
80 * Make sure that we don't hold the user lock.
82 void USER_CheckNotLock(void)
84 if (RtlIsCriticalSectionLockedByThread(&user_section
))
86 ERR( "BUG: holding USER lock\n" );
92 /***********************************************************************
93 * UserSelectPalette (Not a Windows API)
95 static HPALETTE WINAPI
UserSelectPalette( HDC hDC
, HPALETTE hPal
, BOOL bForceBackground
)
99 if (!bForceBackground
&& (hPal
!= GetStockObject(DEFAULT_PALETTE
)))
101 HWND hwnd
= WindowFromDC( hDC
);
104 HWND hForeground
= GetForegroundWindow();
105 /* set primary palette if it's related to current active */
106 if (hForeground
== hwnd
|| IsChild(hForeground
,hwnd
))
109 hPrimaryPalette
= hPal
;
113 return pfnGDISelectPalette( hDC
, hPal
, wBkgPalette
);
117 /***********************************************************************
118 * UserRealizePalette (USER32.@)
120 UINT WINAPI
UserRealizePalette( HDC hDC
)
122 UINT realized
= pfnGDIRealizePalette( hDC
);
124 /* do not send anything if no colors were changed */
125 if (realized
&& GetCurrentObject( hDC
, OBJ_PAL
) == hPrimaryPalette
)
127 /* send palette change notification */
128 HWND hWnd
= WindowFromDC( hDC
);
129 if (hWnd
) SendMessageTimeoutW( HWND_BROADCAST
, WM_PALETTECHANGED
, (WPARAM
)hWnd
, 0,
130 SMTO_ABORTIFHUNG
, 2000, NULL
);
136 /***********************************************************************
139 * Patch the function pointers in GDI for SelectPalette and RealizePalette
141 static void palette_init(void)
144 HMODULE module
= GetModuleHandleA( "gdi32" );
147 ERR( "cannot get GDI32 handle\n" );
150 if ((ptr
= (void**)GetProcAddress( module
, "pfnSelectPalette" )))
151 pfnGDISelectPalette
= InterlockedExchangePointer( ptr
, UserSelectPalette
);
152 else ERR( "cannot find pfnSelectPalette in GDI32\n" );
153 if ((ptr
= (void**)GetProcAddress( module
, "pfnRealizePalette" )))
154 pfnGDIRealizePalette
= InterlockedExchangePointer( ptr
, UserRealizePalette
);
155 else ERR( "cannot find pfnRealizePalette in GDI32\n" );
159 /***********************************************************************
160 * get_default_desktop
162 * Get the name of the desktop to use for this app if not specified explicitly.
164 static const WCHAR
*get_default_desktop(void)
166 static const WCHAR defaultW
[] = {'D','e','f','a','u','l','t',0};
167 static const WCHAR desktopW
[] = {'D','e','s','k','t','o','p',0};
168 static const WCHAR explorerW
[] = {'\\','E','x','p','l','o','r','e','r',0};
169 static const WCHAR app_defaultsW
[] = {'S','o','f','t','w','a','r','e','\\',
170 'W','i','n','e','\\',
171 'A','p','p','D','e','f','a','u','l','t','s',0};
172 static WCHAR buffer
[MAX_PATH
+ sizeof(explorerW
)/sizeof(WCHAR
)];
173 WCHAR
*p
, *appname
= buffer
;
174 const WCHAR
*ret
= defaultW
;
178 len
= (GetModuleFileNameW( 0, buffer
, MAX_PATH
));
179 if (!len
|| len
>= MAX_PATH
) return ret
;
180 if ((p
= strrchrW( appname
, '/' ))) appname
= p
+ 1;
181 if ((p
= strrchrW( appname
, '\\' ))) appname
= p
+ 1;
182 p
= appname
+ strlenW(appname
);
183 strcpyW( p
, explorerW
);
185 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Explorer */
186 if (!RegOpenKeyW( HKEY_CURRENT_USER
, app_defaultsW
, &tmpkey
))
188 if (RegOpenKeyW( tmpkey
, appname
, &appkey
)) appkey
= 0;
189 RegCloseKey( tmpkey
);
192 len
= sizeof(buffer
);
193 if (!RegQueryValueExW( appkey
, desktopW
, 0, NULL
, (LPBYTE
)buffer
, &len
)) ret
= buffer
;
194 RegCloseKey( appkey
);
195 if (ret
&& strcmpiW( ret
, defaultW
)) return ret
;
200 memcpy( buffer
, app_defaultsW
, 13 * sizeof(WCHAR
) ); /* copy only software\\wine */
201 strcpyW( buffer
+ 13, explorerW
);
203 /* @@ Wine registry key: HKCU\Software\Wine\Explorer */
204 if (!RegOpenKeyW( HKEY_CURRENT_USER
, buffer
, &appkey
))
206 len
= sizeof(buffer
);
207 if (!RegQueryValueExW( appkey
, desktopW
, 0, NULL
, (LPBYTE
)buffer
, &len
)) ret
= buffer
;
208 RegCloseKey( appkey
);
214 /***********************************************************************
217 * Initialize the DPI awareness style.
219 static void dpiaware_init(void)
223 static const WCHAR dpiAwareW
[] = {'d','p','i','A','w','a','r','e',0};
224 static const WCHAR dpiAwarenessW
[] = {'d','p','i','A','w','a','r','e','n','e','s','s',0};
225 static const WCHAR namespace2005W
[] = {'h','t','t','p',':','/','/','s','c','h','e','m','a','s','.','m','i','c','r','o','s','o','f','t','.','c','o','m','/','S','M','I','/','2','0','0','5','/','W','i','n','d','o','w','s','S','e','t','t','i','n','g','s',0};
226 static const WCHAR namespace2016W
[] = {'h','t','t','p',':','/','/','s','c','h','e','m','a','s','.','m','i','c','r','o','s','o','f','t','.','c','o','m','/','S','M','I','/','2','0','1','6','/','W','i','n','d','o','w','s','S','e','t','t','i','n','g','s',0};
228 if (!LdrQueryImageFileExecutionOptions( &NtCurrentTeb()->Peb
->ProcessParameters
->ImagePathName
,
229 dpiAwarenessW
, REG_DWORD
, &option
, sizeof(option
), NULL
))
231 TRACE( "got option %x\n", option
);
234 SetProcessDpiAwarenessContext( (DPI_AWARENESS_CONTEXT
)~(ULONG_PTR
)option
);
239 if (QueryActCtxSettingsW( 0, NULL
, namespace2016W
, dpiAwarenessW
, buffer
, ARRAY_SIZE(buffer
), NULL
))
241 static const WCHAR unawareW
[] = {'u','n','a','w','a','r','e',0};
242 static const WCHAR systemW
[] = {'s','y','s','t','e','m',0};
243 static const WCHAR permonW
[] = {'p','e','r','m','o','n','i','t','o','r',0};
244 static const WCHAR permonv2W
[] = {'p','e','r','m','o','n','i','t','o','r','v','2',0};
245 static const WCHAR spacesW
[] = {' ','\t','\r','\n',0};
246 static const WCHAR
* const types
[] = { unawareW
, systemW
, permonW
, permonv2W
};
247 WCHAR
*p
, *start
= buffer
, *end
;
250 TRACE( "got dpiAwareness=%s\n", debugstr_w(buffer
) );
251 for (start
= buffer
; *start
; start
= end
)
253 start
+= strspnW( start
, spacesW
);
254 if (!(end
= strchrW( start
, ',' ))) end
= start
+ strlenW(start
);
256 if ((p
= strpbrkW( start
, spacesW
))) *p
= 0;
257 for (i
= 0; i
< ARRAY_SIZE(types
); i
++)
259 if (strcmpiW( start
, types
[i
] )) continue;
260 SetProcessDpiAwarenessContext( (DPI_AWARENESS_CONTEXT
)~i
);
265 else if (QueryActCtxSettingsW( 0, NULL
, namespace2005W
, dpiAwareW
, buffer
, ARRAY_SIZE(buffer
), NULL
))
267 static const WCHAR trueW
[] = {'t','r','u','e',0};
268 static const WCHAR truepmW
[] = {'t','r','u','e','/','p','m',0};
269 static const WCHAR permonW
[] = {'p','e','r',' ','m','o','n','i','t','o','r',0};
271 TRACE( "got dpiAware=%s\n", debugstr_w(buffer
) );
272 if (!strcmpiW( buffer
, trueW
))
273 SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT_SYSTEM_AWARE
);
274 else if (!strcmpiW( buffer
, truepmW
) || !strcmpiW( buffer
, permonW
))
275 SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE
);
277 SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT_UNAWARE
);
282 /***********************************************************************
285 * Connect to the process window station and desktop.
287 static void winstation_init(void)
289 static const WCHAR WinSta0
[] = {'W','i','n','S','t','a','0',0};
292 WCHAR
*winstation
= NULL
, *desktop
= NULL
, *buffer
= NULL
;
295 GetStartupInfoW( &info
);
296 if (info
.lpDesktop
&& *info
.lpDesktop
)
298 buffer
= HeapAlloc( GetProcessHeap(), 0, (strlenW(info
.lpDesktop
) + 1) * sizeof(WCHAR
) );
299 strcpyW( buffer
, info
.lpDesktop
);
300 if ((desktop
= strchrW( buffer
, '\\' )))
305 else desktop
= buffer
;
308 /* set winstation if explicitly specified, or if we don't have one yet */
309 if (buffer
|| !GetProcessWindowStation())
311 handle
= CreateWindowStationW( winstation
? winstation
: WinSta0
, 0, WINSTA_ALL_ACCESS
, NULL
);
314 SetProcessWindowStation( handle
);
315 /* only WinSta0 is visible */
316 if (!winstation
|| !strcmpiW( winstation
, WinSta0
))
318 USEROBJECTFLAGS flags
;
319 flags
.fInherit
= FALSE
;
320 flags
.fReserved
= FALSE
;
321 flags
.dwFlags
= WSF_VISIBLE
;
322 SetUserObjectInformationW( handle
, UOI_FLAGS
, &flags
, sizeof(flags
) );
326 if (buffer
|| !GetThreadDesktop( GetCurrentThreadId() ))
328 handle
= CreateDesktopW( desktop
? desktop
: get_default_desktop(),
329 NULL
, NULL
, 0, DESKTOP_ALL_ACCESS
, NULL
);
330 if (handle
) SetThreadDesktop( handle
);
332 HeapFree( GetProcessHeap(), 0, buffer
);
334 register_desktop_class();
338 /***********************************************************************
339 * USER initialisation routine
341 static BOOL
process_attach(void)
346 /* Initialize system colors and metrics */
349 /* Setup palette function pointers */
356 /**********************************************************************
357 * USER_IsExitingThread
359 BOOL
USER_IsExitingThread( DWORD tid
)
361 return (tid
== exiting_thread_id
);
365 /**********************************************************************
368 static void thread_detach(void)
370 struct user_thread_info
*thread_info
= get_user_thread_info();
372 exiting_thread_id
= GetCurrentThreadId();
374 WDML_NotifyThreadDetach();
375 USER_Driver
->pThreadDetach();
377 destroy_thread_windows();
378 CloseHandle( thread_info
->server_queue
);
379 HeapFree( GetProcessHeap(), 0, thread_info
->wmchar_data
);
380 HeapFree( GetProcessHeap(), 0, thread_info
->key_state
);
381 HeapFree( GetProcessHeap(), 0, thread_info
->rawinput
);
383 exiting_thread_id
= 0;
387 /***********************************************************************
388 * UserClientDllInitialize (USER32.@)
390 * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
392 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, LPVOID reserved
)
394 static const WCHAR imm32_dllW
[] = {'i','m','m','3','2','.','d','l','l',0};
395 static HMODULE imm32_module
;
400 case DLL_PROCESS_ATTACH
:
401 user32_module
= inst
;
402 ret
= process_attach();
404 imm32_module
= LoadLibraryW(imm32_dllW
);
406 case DLL_THREAD_DETACH
:
409 case DLL_PROCESS_DETACH
:
410 USER_unload_driver();
411 FreeLibrary(imm32_module
);
412 DeleteCriticalSection(&user_section
);
419 /***********************************************************************
420 * ExitWindowsEx (USER32.@)
422 BOOL WINAPI
ExitWindowsEx( UINT flags
, DWORD reason
)
424 static const WCHAR winebootW
[] = { '\\','w','i','n','e','b','o','o','t','.','e','x','e',0 };
425 static const WCHAR killW
[] = { ' ','-','-','k','i','l','l',0 };
426 static const WCHAR end_sessionW
[] = { ' ','-','-','e','n','d','-','s','e','s','s','i','o','n',0 };
427 static const WCHAR forceW
[] = { ' ','-','-','f','o','r','c','e',0 };
428 static const WCHAR shutdownW
[] = { ' ','-','-','s','h','u','t','d','o','w','n',0 };
431 WCHAR cmdline
[MAX_PATH
+ 64];
432 PROCESS_INFORMATION pi
;
436 GetSystemDirectoryW( app
, MAX_PATH
- sizeof(winebootW
)/sizeof(WCHAR
) );
437 strcatW( app
, winebootW
);
438 strcpyW( cmdline
, app
);
440 if (flags
& EWX_FORCE
) lstrcatW( cmdline
, killW
);
443 lstrcatW( cmdline
, end_sessionW
);
444 if (flags
& EWX_FORCEIFHUNG
) lstrcatW( cmdline
, forceW
);
446 if (!(flags
& EWX_REBOOT
)) lstrcatW( cmdline
, shutdownW
);
448 memset( &si
, 0, sizeof si
);
450 Wow64DisableWow64FsRedirection( &redir
);
451 if (!CreateProcessW( app
, cmdline
, NULL
, NULL
, FALSE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
453 Wow64RevertWow64FsRedirection( redir
);
454 ERR( "Failed to run %s\n", debugstr_w(cmdline
) );
457 Wow64RevertWow64FsRedirection( redir
);
458 CloseHandle( pi
.hProcess
);
459 CloseHandle( pi
.hThread
);
463 /***********************************************************************
464 * LockWorkStation (USER32.@)
466 BOOL WINAPI
LockWorkStation(void)
469 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
473 /***********************************************************************
474 * RegisterServicesProcess (USER32.@)
476 int WINAPI
RegisterServicesProcess(DWORD ServicesProcessId
)
478 FIXME("(0x%x): stub\n", ServicesProcessId
);
482 /***********************************************************************
483 * ShutdownBlockReasonCreate (USER32.@)
485 BOOL WINAPI
ShutdownBlockReasonCreate(HWND hwnd
, LPCWSTR reason
)
487 FIXME("(%p, %s): stub\n", hwnd
, debugstr_w(reason
));
488 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
492 /***********************************************************************
493 * ShutdownBlockReasonDestroy (USER32.@)
495 BOOL WINAPI
ShutdownBlockReasonDestroy(HWND hwnd
)
497 FIXME("(%p): stub\n", hwnd
);
498 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);