user32: Fix user_thread_info for 64-bits
[wine/wine64.git] / dlls / user32 / user_main.c
blob8421180ad09f2da30d3eb07e1f07ed773a56e609
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 <assert.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
30 #include "controls.h"
31 #include "user_private.h"
32 #include "win.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
38 #define DESKTOP_ALL_ACCESS 0x01ff
40 WORD USER_HeapSel = 0; /* USER heap selector */
41 HMODULE user32_module = 0;
43 static SYSLEVEL USER_SysLevel;
44 static CRITICAL_SECTION_DEBUG critsect_debug =
46 0, 0, &USER_SysLevel.crst,
47 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
48 0, 0, { (DWORD_PTR)(__FILE__ ": USER_SysLevel") }
50 static SYSLEVEL USER_SysLevel = { { &critsect_debug, -1, 0, 0, 0, 0 }, 2 };
52 static HPALETTE (WINAPI *pfnGDISelectPalette)( HDC hdc, HPALETTE hpal, WORD bkgnd );
53 static UINT (WINAPI *pfnGDIRealizePalette)( HDC hdc );
54 static HPALETTE hPrimaryPalette;
56 static DWORD exiting_thread_id;
58 extern void WDML_NotifyThreadDetach(void);
61 /***********************************************************************
62 * USER_Lock
64 void USER_Lock(void)
66 _EnterSysLevel( &USER_SysLevel );
70 /***********************************************************************
71 * USER_Unlock
73 void USER_Unlock(void)
75 _LeaveSysLevel( &USER_SysLevel );
79 /***********************************************************************
80 * USER_CheckNotLock
82 * Make sure that we don't hold the user lock.
84 void USER_CheckNotLock(void)
86 _CheckNotSysLevel( &USER_SysLevel );
90 /***********************************************************************
91 * UserSelectPalette (Not a Windows API)
93 static HPALETTE WINAPI UserSelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
95 WORD wBkgPalette = 1;
97 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
99 HWND hwnd = WindowFromDC( hDC );
100 if (hwnd)
102 HWND hForeground = GetForegroundWindow();
103 /* set primary palette if it's related to current active */
104 if (hForeground == hwnd || IsChild(hForeground,hwnd))
106 wBkgPalette = 0;
107 hPrimaryPalette = hPal;
111 return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
115 /***********************************************************************
116 * UserRealizePalette (USER32.@)
118 UINT WINAPI UserRealizePalette( HDC hDC )
120 UINT realized = pfnGDIRealizePalette( hDC );
122 /* do not send anything if no colors were changed */
123 if (realized && GetCurrentObject( hDC, OBJ_PAL ) == hPrimaryPalette)
125 /* send palette change notification */
126 HWND hWnd = WindowFromDC( hDC );
127 if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
128 SMTO_ABORTIFHUNG, 2000, NULL );
130 return realized;
134 /***********************************************************************
135 * palette_init
137 * Patch the function pointers in GDI for SelectPalette and RealizePalette
139 static void palette_init(void)
141 void **ptr;
142 HMODULE module = GetModuleHandleA( "gdi32" );
143 if (!module)
145 ERR( "cannot get GDI32 handle\n" );
146 return;
148 if ((ptr = (void**)GetProcAddress( module, "pfnSelectPalette" )))
149 pfnGDISelectPalette = InterlockedExchangePointer( ptr, UserSelectPalette );
150 else ERR( "cannot find pfnSelectPalette in GDI32\n" );
151 if ((ptr = (void**)GetProcAddress( module, "pfnRealizePalette" )))
152 pfnGDIRealizePalette = InterlockedExchangePointer( ptr, UserRealizePalette );
153 else ERR( "cannot find pfnRealizePalette in GDI32\n" );
157 /***********************************************************************
158 * get_default_desktop
160 * Get the name of the desktop to use for this app if not specified explicitly.
162 static const WCHAR *get_default_desktop(void)
164 static const WCHAR defaultW[] = {'D','e','f','a','u','l','t',0};
165 static const WCHAR desktopW[] = {'D','e','s','k','t','o','p',0};
166 static const WCHAR explorerW[] = {'\\','E','x','p','l','o','r','e','r',0};
167 static const WCHAR app_defaultsW[] = {'S','o','f','t','w','a','r','e','\\',
168 'W','i','n','e','\\',
169 'A','p','p','D','e','f','a','u','l','t','s',0};
170 static WCHAR buffer[MAX_PATH + sizeof(explorerW)/sizeof(WCHAR)];
171 WCHAR *p, *appname = buffer;
172 const WCHAR *ret = defaultW;
173 DWORD len;
174 HKEY tmpkey, appkey;
176 len = (GetModuleFileNameW( 0, buffer, MAX_PATH ));
177 if (!len || len >= MAX_PATH) return ret;
178 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
179 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
180 p = appname + strlenW(appname);
181 strcpyW( p, explorerW );
183 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Explorer */
184 if (!RegOpenKeyW( HKEY_CURRENT_USER, app_defaultsW, &tmpkey ))
186 if (RegOpenKeyW( tmpkey, appname, &appkey )) appkey = 0;
187 RegCloseKey( tmpkey );
188 if (appkey)
190 len = sizeof(buffer);
191 if (!RegQueryValueExW( appkey, desktopW, 0, NULL, (LPBYTE)buffer, &len )) ret = buffer;
192 RegCloseKey( appkey );
193 if (ret && strcmpiW( ret, defaultW )) return ret;
194 ret = defaultW;
198 memcpy( buffer, app_defaultsW, 13 * sizeof(WCHAR) ); /* copy only software\\wine */
199 strcpyW( buffer + 13, explorerW );
201 /* @@ Wine registry key: HKCU\Software\Wine\Explorer */
202 if (!RegOpenKeyW( HKEY_CURRENT_USER, buffer, &appkey ))
204 len = sizeof(buffer);
205 if (!RegQueryValueExW( appkey, desktopW, 0, NULL, (LPBYTE)buffer, &len )) ret = buffer;
206 RegCloseKey( appkey );
208 return ret;
212 /***********************************************************************
213 * winstation_init
215 * Connect to the process window station and desktop.
217 static void winstation_init(void)
219 static const WCHAR WinSta0[] = {'W','i','n','S','t','a','0',0};
221 STARTUPINFOW info;
222 WCHAR *winstation = NULL, *desktop = NULL, *buffer = NULL;
223 HANDLE handle;
225 GetStartupInfoW( &info );
226 if (info.lpDesktop && *info.lpDesktop)
228 buffer = HeapAlloc( GetProcessHeap(), 0, (strlenW(info.lpDesktop) + 1) * sizeof(WCHAR) );
229 strcpyW( buffer, info.lpDesktop );
230 if ((desktop = strchrW( buffer, '\\' )))
232 *desktop++ = 0;
233 winstation = buffer;
235 else desktop = buffer;
238 /* set winstation if explicitly specified, or if we don't have one yet */
239 if (buffer || !GetProcessWindowStation())
241 handle = CreateWindowStationW( winstation ? winstation : WinSta0, 0, WINSTA_ALL_ACCESS, NULL );
242 if (handle)
244 SetProcessWindowStation( handle );
245 /* only WinSta0 is visible */
246 if (!winstation || !strcmpiW( winstation, WinSta0 ))
248 USEROBJECTFLAGS flags;
249 flags.fInherit = FALSE;
250 flags.fReserved = FALSE;
251 flags.dwFlags = WSF_VISIBLE;
252 SetUserObjectInformationW( handle, UOI_FLAGS, &flags, sizeof(flags) );
256 if (buffer || !GetThreadDesktop( GetCurrentThreadId() ))
258 handle = CreateDesktopW( desktop ? desktop : get_default_desktop(),
259 NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
260 if (handle) SetThreadDesktop( handle );
262 HeapFree( GetProcessHeap(), 0, buffer );
266 /***********************************************************************
267 * USER initialisation routine
269 static BOOL process_attach(void)
271 #ifdef _WIN32
272 HINSTANCE16 instance;
274 /* Create USER heap */
275 if ((instance = LoadLibrary16( "USER.EXE" )) >= 32) USER_HeapSel = instance | 7;
276 else
278 USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 65536 );
279 LocalInit16( USER_HeapSel, 32, 65534 );
282 /* some Win9x dlls expect keyboard to be loaded */
283 if (GetVersion() & 0x80000000) LoadLibrary16( "keyboard.drv" );
284 #else
285 /* Ignore heap, not needed for win64*/
286 #endif
288 winstation_init();
290 /* Initialize system colors and metrics */
291 SYSPARAMS_Init();
293 /* Setup palette function pointers */
294 palette_init();
296 /* Initialize built-in window classes */
297 CLASS_RegisterBuiltinClasses();
299 /* Initialize message spying */
300 if (!SPY_Init()) return FALSE;
302 return TRUE;
306 /**********************************************************************
307 * USER_IsExitingThread
309 BOOL USER_IsExitingThread( DWORD tid )
311 return (tid == exiting_thread_id);
315 /**********************************************************************
316 * thread_detach
318 static void thread_detach(void)
320 struct user_thread_info *thread_info = get_user_thread_info();
322 exiting_thread_id = GetCurrentThreadId();
324 WDML_NotifyThreadDetach();
326 if (thread_info->top_window) WIN_DestroyThreadWindows( thread_info->top_window );
327 if (thread_info->msg_window) WIN_DestroyThreadWindows( thread_info->msg_window );
328 CloseHandle( thread_info->server_queue );
329 HeapFree( GetProcessHeap(), 0, thread_info->wmchar_data );
331 exiting_thread_id = 0;
335 /***********************************************************************
336 * UserClientDllInitialize (USER32.@)
338 * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
340 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
342 BOOL ret = TRUE;
343 switch(reason)
345 case DLL_PROCESS_ATTACH:
346 user32_module = inst;
347 /* In win64 mode, the structure is always aligned to pointer size */
348 assert(sizeof(struct user_thread_info) - sizeof(void *) + sizeof(int) == sizeof(NtCurrentTeb()->Win32ClientInfo));
349 ret = process_attach();
350 break;
351 case DLL_THREAD_DETACH:
352 thread_detach();
353 break;
354 case DLL_PROCESS_DETACH:
355 USER_unload_driver();
356 break;
358 return ret;
362 /***********************************************************************
363 * ExitWindowsEx (USER32.@)
365 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reason )
367 static const WCHAR winebootW[] = { '\\','w','i','n','e','b','o','o','t','.','e','x','e',0 };
368 static const WCHAR killW[] = { ' ','-','-','k','i','l','l',0 };
369 static const WCHAR end_sessionW[] = { ' ','-','-','e','n','d','-','s','e','s','s','i','o','n',0 };
370 static const WCHAR forceW[] = { ' ','-','-','f','o','r','c','e',0 };
371 static const WCHAR shutdownW[] = { ' ','-','-','s','h','u','t','d','o','w','n',0 };
373 WCHAR cmdline[MAX_PATH + 64];
374 PROCESS_INFORMATION pi;
375 STARTUPINFOW si;
377 GetSystemDirectoryW( cmdline, MAX_PATH );
378 lstrcatW( cmdline, winebootW );
380 if (flags & EWX_FORCE) lstrcatW( cmdline, killW );
381 else
383 lstrcatW( cmdline, end_sessionW );
384 if (flags & EWX_FORCEIFHUNG) lstrcatW( cmdline, forceW );
386 if (!(flags & EWX_REBOOT)) lstrcatW( cmdline, shutdownW );
388 memset( &si, 0, sizeof si );
389 si.cb = sizeof si;
390 if (!CreateProcessW( NULL, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi ))
392 ERR( "Failed to run %s\n", debugstr_w(cmdline) );
393 return FALSE;
395 CloseHandle( pi.hProcess );
396 CloseHandle( pi.hThread );
397 return TRUE;
400 /***********************************************************************
401 * LockWorkStation (USER32.@)
403 BOOL WINAPI LockWorkStation(void)
405 TRACE(": stub\n");
406 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
407 return FALSE;
410 /***********************************************************************
411 * RegisterServicesProcess (USER32.@)
413 int WINAPI RegisterServicesProcess(DWORD ServicesProcessId)
415 FIXME("(0x%x): stub\n", ServicesProcessId);
416 return 0;