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 WORD USER_HeapSel = 0; /* USER heap selector */
40 HMODULE user32_module = 0;
42 static SYSLEVEL USER_SysLevel;
43 static CRITICAL_SECTION_DEBUG critsect_debug =
45 0, 0, &USER_SysLevel.crst,
46 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
47 0, 0, { (DWORD_PTR)(__FILE__ ": USER_SysLevel") }
49 static SYSLEVEL USER_SysLevel = { { &critsect_debug, -1, 0, 0, 0, 0 }, 2 };
51 static HPALETTE (WINAPI *pfnGDISelectPalette)( HDC hdc, HPALETTE hpal, WORD bkgnd );
52 static UINT (WINAPI *pfnGDIRealizePalette)( HDC hdc );
53 static HPALETTE hPrimaryPalette;
55 static DWORD exiting_thread_id;
57 extern void WDML_NotifyThreadDetach(void);
60 /***********************************************************************
65 _EnterSysLevel( &USER_SysLevel );
69 /***********************************************************************
72 void USER_Unlock(void)
74 _LeaveSysLevel( &USER_SysLevel );
78 /***********************************************************************
81 * Make sure that we don't hold the user lock.
83 void USER_CheckNotLock(void)
85 _CheckNotSysLevel( &USER_SysLevel );
89 /***********************************************************************
90 * UserSelectPalette (Not a Windows API)
92 static HPALETTE WINAPI UserSelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
96 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
98 HWND hwnd = WindowFromDC( hDC );
101 HWND hForeground = GetForegroundWindow();
102 /* set primary palette if it's related to current active */
103 if (hForeground == hwnd || IsChild(hForeground,hwnd))
106 hPrimaryPalette = hPal;
110 return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
114 /***********************************************************************
115 * UserRealizePalette (USER32.@)
117 UINT WINAPI UserRealizePalette( HDC hDC )
119 UINT realized = pfnGDIRealizePalette( hDC );
121 /* do not send anything if no colors were changed */
122 if (realized && GetCurrentObject( hDC, OBJ_PAL ) == hPrimaryPalette)
124 /* send palette change notification */
125 HWND hWnd = WindowFromDC( hDC );
126 if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
127 SMTO_ABORTIFHUNG, 2000, NULL );
133 /***********************************************************************
136 * Patch the function pointers in GDI for SelectPalette and RealizePalette
138 static void palette_init(void)
141 HMODULE module = GetModuleHandleA( "gdi32" );
144 ERR( "cannot get GDI32 handle\n" );
147 if ((ptr = (void**)GetProcAddress( module, "pfnSelectPalette" )))
148 pfnGDISelectPalette = InterlockedExchangePointer( ptr, UserSelectPalette );
149 else ERR( "cannot find pfnSelectPalette in GDI32\n" );
150 if ((ptr = (void**)GetProcAddress( module, "pfnRealizePalette" )))
151 pfnGDIRealizePalette = InterlockedExchangePointer( ptr, UserRealizePalette );
152 else ERR( "cannot find pfnRealizePalette in GDI32\n" );
156 /***********************************************************************
159 * Connect to the process window station and desktop.
161 static void winstation_init(void)
163 static const WCHAR WinSta0[] = {'W','i','n','S','t','a','0',0};
164 static const WCHAR Default[] = {'D','e','f','a','u','l','t',0};
167 WCHAR *winstation = NULL, *desktop = NULL, *buffer = NULL;
170 GetStartupInfoW( &info );
171 if (info.lpDesktop && *info.lpDesktop)
173 buffer = HeapAlloc( GetProcessHeap(), 0, (strlenW(info.lpDesktop) + 1) * sizeof(WCHAR) );
174 strcpyW( buffer, info.lpDesktop );
175 if ((desktop = strchrW( buffer, '\\' )))
180 else desktop = buffer;
183 /* set winstation if explicitly specified, or if we don't have one yet */
184 if (buffer || !GetProcessWindowStation())
186 handle = CreateWindowStationW( winstation ? winstation : WinSta0, 0, WINSTA_ALL_ACCESS, NULL );
189 SetProcessWindowStation( handle );
190 /* only WinSta0 is visible */
191 if (!winstation || !strcmpiW( winstation, WinSta0 ))
193 USEROBJECTFLAGS flags;
194 flags.fInherit = FALSE;
195 flags.fReserved = FALSE;
196 flags.dwFlags = WSF_VISIBLE;
197 SetUserObjectInformationW( handle, UOI_FLAGS, &flags, sizeof(flags) );
201 if (buffer || !GetThreadDesktop( GetCurrentThreadId() ))
203 handle = CreateDesktopW( desktop ? desktop : Default, NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
204 if (handle) SetThreadDesktop( handle );
206 HeapFree( GetProcessHeap(), 0, buffer );
210 /***********************************************************************
211 * USER initialisation routine
213 static BOOL process_attach(void)
215 HINSTANCE16 instance;
217 /* Create USER heap */
218 if ((instance = LoadLibrary16( "USER.EXE" )) >= 32) USER_HeapSel = instance | 7;
221 USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 65536 );
222 LocalInit16( USER_HeapSel, 32, 65534 );
225 /* some Win9x dlls expect keyboard to be loaded */
226 if (GetVersion() & 0x80000000) LoadLibrary16( "keyboard.drv" );
230 /* Initialize system colors and metrics */
233 /* Setup palette function pointers */
236 /* Initialize built-in window classes */
237 CLASS_RegisterBuiltinClasses();
239 /* Initialize message spying */
240 if (!SPY_Init()) return FALSE;
246 /**********************************************************************
247 * USER_IsExitingThread
249 BOOL USER_IsExitingThread( DWORD tid )
251 return (tid == exiting_thread_id);
255 /**********************************************************************
258 static void thread_detach(void)
260 exiting_thread_id = GetCurrentThreadId();
262 WDML_NotifyThreadDetach();
264 WIN_DestroyThreadWindows( get_user_thread_info()->desktop );
265 CloseHandle( get_user_thread_info()->server_queue );
266 HeapFree( GetProcessHeap(), 0, get_user_thread_info()->wmchar_data );
268 exiting_thread_id = 0;
272 /***********************************************************************
273 * UserClientDllInitialize (USER32.@)
275 * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
277 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
282 case DLL_PROCESS_ATTACH:
283 user32_module = inst;
284 ret = process_attach();
286 case DLL_THREAD_DETACH:
289 case DLL_PROCESS_DETACH:
290 USER_unload_driver();
297 /***********************************************************************
298 * ExitWindowsEx (USER32.@)
300 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reason )
302 static const WCHAR winebootW[] = { '\\','w','i','n','e','b','o','o','t','.','e','x','e',0 };
303 static const WCHAR killW[] = { ' ','-','-','k','i','l','l',0 };
304 static const WCHAR end_sessionW[] = { ' ','-','-','e','n','d','-','s','e','s','s','i','o','n',0 };
305 static const WCHAR forceW[] = { ' ','-','-','f','o','r','c','e',0 };
306 static const WCHAR shutdownW[] = { ' ','-','-','s','h','u','t','d','o','w','n',0 };
308 WCHAR cmdline[MAX_PATH + 64];
309 PROCESS_INFORMATION pi;
312 GetSystemDirectoryW( cmdline, MAX_PATH );
313 lstrcatW( cmdline, winebootW );
315 if (flags & EWX_FORCE) lstrcatW( cmdline, killW );
318 lstrcatW( cmdline, end_sessionW );
319 if (flags & EWX_FORCEIFHUNG) lstrcatW( cmdline, forceW );
321 if (!(flags & EWX_REBOOT)) lstrcatW( cmdline, shutdownW );
323 memset( &si, 0, sizeof si );
325 if (!CreateProcessW( NULL, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi ))
327 ERR( "Failed to run %s\n", debugstr_w(cmdline) );
330 CloseHandle( pi.hProcess );
331 CloseHandle( pi.hThread );
335 /***********************************************************************
336 * RegisterServicesProcess (USER32.@)
338 int WINAPI RegisterServicesProcess(DWORD ServicesProcessId)
340 FIXME("(0x%x): stub\n", ServicesProcessId);