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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "user_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
38 WORD USER_HeapSel = 0; /* USER heap selector */
39 HMODULE user32_module = 0;
41 static SYSLEVEL USER_SysLevel;
42 static CRITICAL_SECTION_DEBUG critsect_debug =
44 0, 0, &USER_SysLevel.crst,
45 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
46 0, 0, { 0, (DWORD)(__FILE__ ": USER_SysLevel") }
48 static SYSLEVEL USER_SysLevel = { { &critsect_debug, -1, 0, 0, 0, 0 }, 2 };
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 _EnterSysLevel( &USER_SysLevel );
68 /***********************************************************************
71 void USER_Unlock(void)
73 _LeaveSysLevel( &USER_SysLevel );
77 /***********************************************************************
80 * Make sure that we don't hold the user lock.
82 void USER_CheckNotLock(void)
84 _CheckNotSysLevel( &USER_SysLevel );
88 /***********************************************************************
89 * UserSelectPalette (Not a Windows API)
91 static HPALETTE WINAPI UserSelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
95 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
97 HWND hwnd = WindowFromDC( hDC );
100 HWND hForeground = GetForegroundWindow();
101 /* set primary palette if it's related to current active */
102 if (hForeground == hwnd || IsChild(hForeground,hwnd))
105 hPrimaryPalette = hPal;
109 return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
113 /***********************************************************************
114 * UserRealizePalette (USER32.@)
116 UINT WINAPI UserRealizePalette( HDC hDC )
118 UINT realized = pfnGDIRealizePalette( hDC );
120 /* do not send anything if no colors were changed */
121 if (realized && GetCurrentObject( hDC, OBJ_PAL ) == hPrimaryPalette)
123 /* send palette change notification */
124 HWND hWnd = WindowFromDC( hDC );
125 if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
126 SMTO_ABORTIFHUNG, 2000, NULL );
132 /***********************************************************************
135 * Patch the function pointers in GDI for SelectPalette and RealizePalette
137 static void palette_init(void)
140 HMODULE module = GetModuleHandleA( "gdi32" );
143 ERR( "cannot get GDI32 handle\n" );
146 if ((ptr = (void**)GetProcAddress( module, "pfnSelectPalette" )))
147 pfnGDISelectPalette = InterlockedExchangePointer( ptr, UserSelectPalette );
148 else ERR( "cannot find pfnSelectPalette in GDI32\n" );
149 if ((ptr = (void**)GetProcAddress( module, "pfnRealizePalette" )))
150 pfnGDIRealizePalette = InterlockedExchangePointer( ptr, UserRealizePalette );
151 else ERR( "cannot find pfnRealizePalette in GDI32\n" );
155 /***********************************************************************
156 * USER initialisation routine
158 static BOOL process_attach(void)
160 HINSTANCE16 instance;
162 /* Create USER heap */
163 if ((instance = LoadLibrary16( "USER.EXE" )) >= 32) USER_HeapSel = instance | 7;
166 USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 65536 );
167 LocalInit16( USER_HeapSel, 32, 65534 );
170 /* some Win9x dlls expect keyboard to be loaded */
171 if (GetVersion() & 0x80000000) LoadLibrary16( "keyboard.drv" );
173 /* Initialize system colors and metrics */
176 /* Setup palette function pointers */
179 /* Initialize built-in window classes */
180 CLASS_RegisterBuiltinClasses();
182 /* Initialize message spying */
183 if (!SPY_Init()) return FALSE;
189 /**********************************************************************
190 * USER_IsExitingThread
192 BOOL USER_IsExitingThread( DWORD tid )
194 return (tid == exiting_thread_id);
198 /**********************************************************************
201 static void thread_detach(void)
203 exiting_thread_id = GetCurrentThreadId();
205 WDML_NotifyThreadDetach();
207 WIN_DestroyThreadWindows( GetDesktopWindow() );
208 CloseHandle( get_user_thread_info()->server_queue );
210 exiting_thread_id = 0;
214 /***********************************************************************
215 * UserClientDllInitialize (USER32.@)
217 * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
219 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
224 case DLL_PROCESS_ATTACH:
225 user32_module = inst;
226 ret = process_attach();
228 case DLL_THREAD_DETACH:
231 case DLL_PROCESS_DETACH:
232 USER_unload_driver();
239 /***********************************************************************
240 * USER_GetProcessHandleList(Internal)
242 static HANDLE *USER_GetProcessHandleList(void)
250 hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
253 ERR("cannot create snapshot\n");
257 /* count the number of processes plus one */
258 for (count=0; ;count++)
260 pe.dwSize = sizeof pe;
262 r = Process32Next( hSnapshot, &pe );
264 r = Process32First( hSnapshot, &pe );
269 /* allocate memory make a list of the process handles */
270 list = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof(HANDLE) );
272 for (i=0; i<count; i++)
274 pe.dwSize = sizeof pe;
276 r = Process32Next( hSnapshot, &pe );
278 r = Process32First( hSnapshot, &pe );
282 /* don't kill ourselves */
283 if (GetCurrentProcessId() == pe.th32ProcessID )
286 /* open the process so we don't can track it */
287 list[n] = OpenProcess( PROCESS_QUERY_INFORMATION|
289 FALSE, pe.th32ProcessID );
291 /* check it didn't terminate already */
296 CloseHandle( hSnapshot );
299 ERR("Error enumerating processes\n");
301 TRACE("return %lu processes\n", n);
307 /***********************************************************************
308 * USER_KillProcesses (Internal)
310 static DWORD USER_KillProcesses(void)
314 const DWORD dwShutdownTimeout = 10000;
316 TRACE("terminating other processes\n");
318 /* kill it and add it to our list of object to wait on */
319 handles = USER_GetProcessHandleList();
320 for (n=0; handles && handles[n]; n++)
321 TerminateProcess( handles[n], 0 );
323 /* wait for processes to exit */
324 for (i=0; i<n; i+=MAXIMUM_WAIT_OBJECTS)
326 int n_objs = ((n-i)>MAXIMUM_WAIT_OBJECTS) ? MAXIMUM_WAIT_OBJECTS : (n-i);
327 r = WaitForMultipleObjects( n_objs, &handles[i], TRUE, dwShutdownTimeout );
329 ERR("wait failed!\n");
332 /* close the handles */
334 CloseHandle( handles[i] );
336 HeapFree( GetProcessHeap(), 0, handles );
342 /***********************************************************************
343 * USER_DoShutdown (Internal)
345 static void USER_DoShutdown(void)
348 const DWORD nRetries = 10;
350 for (i=0; i<nRetries; i++)
352 n = USER_KillProcesses();
353 TRACE("Killed %ld processes, attempt %ld\n", n, i);
360 /***********************************************************************
361 * ExitWindowsEx (USER32.@)
363 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reserved )
369 /* We have to build a list of all windows first, as in EnumWindows */
370 TRACE("(%x,%lx)\n", flags, reserved);
372 list = WIN_ListChildren( GetDesktopWindow() );
375 /* Send a WM_QUERYENDSESSION message to every window */
377 for (i = 0; list[i]; i++)
379 /* Make sure that the window still exists */
380 if (!IsWindow( list[i] )) continue;
381 if (!SendMessageW( list[i], WM_QUERYENDSESSION, 0, 0 )) break;
385 /* Now notify all windows that got a WM_QUERYENDSESSION of the result */
387 for (phwnd = list; i > 0; i--, phwnd++)
389 if (!IsWindow( *phwnd )) continue;
390 SendMessageW( *phwnd, WM_ENDSESSION, result, 0 );
392 HeapFree( GetProcessHeap(), 0, list );
394 if ( !(result || (flags & EWX_FORCE) ))
398 /* USER_DoShutdown will kill all processes except the current process */
401 if (flags & EWX_REBOOT)
403 WCHAR winebootW[] = { 'w','i','n','e','b','o','o','t',0 };
404 PROCESS_INFORMATION pi;
407 memset( &si, 0, sizeof si );
409 if (CreateProcessW( NULL, winebootW, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
411 CloseHandle( pi.hProcess );
412 CloseHandle( pi.hThread );
415 MESSAGE("wine: Failed to start wineboot\n");
418 if (result) ExitProcess(0);