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/unicode.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(graphics
);
39 #define DESKTOP_ALL_ACCESS 0x01ff
41 WORD USER_HeapSel
= 0; /* USER heap selector */
42 HMODULE user32_module
= 0;
44 static SYSLEVEL USER_SysLevel
;
45 static CRITICAL_SECTION_DEBUG critsect_debug
=
47 0, 0, &USER_SysLevel
.crst
,
48 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
49 0, 0, { (DWORD_PTR
)(__FILE__
": USER_SysLevel") }
51 static SYSLEVEL USER_SysLevel
= { { &critsect_debug
, -1, 0, 0, 0, 0 }, 2 };
53 static HPALETTE (WINAPI
*pfnGDISelectPalette
)( HDC hdc
, HPALETTE hpal
, WORD bkgnd
);
54 static UINT (WINAPI
*pfnGDIRealizePalette
)( HDC hdc
);
55 static HPALETTE hPrimaryPalette
;
57 static DWORD exiting_thread_id
;
59 extern void WDML_NotifyThreadDetach(void);
62 /***********************************************************************
67 _EnterSysLevel( &USER_SysLevel
);
71 /***********************************************************************
74 void USER_Unlock(void)
76 _LeaveSysLevel( &USER_SysLevel
);
80 /***********************************************************************
83 * Make sure that we don't hold the user lock.
85 void USER_CheckNotLock(void)
87 _CheckNotSysLevel( &USER_SysLevel
);
91 /***********************************************************************
92 * UserSelectPalette (Not a Windows API)
94 static HPALETTE WINAPI
UserSelectPalette( HDC hDC
, HPALETTE hPal
, BOOL bForceBackground
)
98 if (!bForceBackground
&& (hPal
!= GetStockObject(DEFAULT_PALETTE
)))
100 HWND hwnd
= WindowFromDC( hDC
);
103 HWND hForeground
= GetForegroundWindow();
104 /* set primary palette if it's related to current active */
105 if (hForeground
== hwnd
|| IsChild(hForeground
,hwnd
))
108 hPrimaryPalette
= hPal
;
112 return pfnGDISelectPalette( hDC
, hPal
, wBkgPalette
);
116 /***********************************************************************
117 * UserRealizePalette (USER32.@)
119 UINT WINAPI
UserRealizePalette( HDC hDC
)
121 UINT realized
= pfnGDIRealizePalette( hDC
);
123 /* do not send anything if no colors were changed */
124 if (realized
&& GetCurrentObject( hDC
, OBJ_PAL
) == hPrimaryPalette
)
126 /* send palette change notification */
127 HWND hWnd
= WindowFromDC( hDC
);
128 if (hWnd
) SendMessageTimeoutW( HWND_BROADCAST
, WM_PALETTECHANGED
, (WPARAM
)hWnd
, 0,
129 SMTO_ABORTIFHUNG
, 2000, NULL
);
135 /***********************************************************************
138 * Patch the function pointers in GDI for SelectPalette and RealizePalette
140 static void palette_init(void)
143 HMODULE module
= GetModuleHandleA( "gdi32" );
146 ERR( "cannot get GDI32 handle\n" );
149 if ((ptr
= (void**)GetProcAddress( module
, "pfnSelectPalette" )))
150 pfnGDISelectPalette
= InterlockedExchangePointer( ptr
, UserSelectPalette
);
151 else ERR( "cannot find pfnSelectPalette in GDI32\n" );
152 if ((ptr
= (void**)GetProcAddress( module
, "pfnRealizePalette" )))
153 pfnGDIRealizePalette
= InterlockedExchangePointer( ptr
, UserRealizePalette
);
154 else ERR( "cannot find pfnRealizePalette in GDI32\n" );
158 /***********************************************************************
161 * Connect to the process window station and desktop.
163 static void winstation_init(void)
165 static const WCHAR WinSta0
[] = {'W','i','n','S','t','a','0',0};
166 static const WCHAR Default
[] = {'D','e','f','a','u','l','t',0};
169 WCHAR
*winstation
= NULL
, *desktop
= NULL
, *buffer
= NULL
;
172 GetStartupInfoW( &info
);
173 if (info
.lpDesktop
&& *info
.lpDesktop
)
175 buffer
= HeapAlloc( GetProcessHeap(), 0, (strlenW(info
.lpDesktop
) + 1) * sizeof(WCHAR
) );
176 strcpyW( buffer
, info
.lpDesktop
);
177 if ((desktop
= strchrW( buffer
, '\\' )))
182 else desktop
= buffer
;
185 /* set winstation if explicitly specified, or if we don't have one yet */
186 if (buffer
|| !GetProcessWindowStation())
188 handle
= CreateWindowStationW( winstation
? winstation
: WinSta0
, 0, WINSTA_ALL_ACCESS
, NULL
);
189 if (handle
) SetProcessWindowStation( handle
);
191 if (buffer
|| !GetThreadDesktop( GetCurrentThreadId() ))
193 handle
= CreateDesktopW( desktop
? desktop
: Default
, NULL
, NULL
, 0, DESKTOP_ALL_ACCESS
, NULL
);
194 if (handle
) SetThreadDesktop( handle
);
196 HeapFree( GetProcessHeap(), 0, buffer
);
200 /***********************************************************************
201 * USER initialisation routine
203 static BOOL
process_attach(void)
205 HINSTANCE16 instance
;
207 /* Create USER heap */
208 if ((instance
= LoadLibrary16( "USER.EXE" )) >= 32) USER_HeapSel
= instance
| 7;
211 USER_HeapSel
= GlobalAlloc16( GMEM_FIXED
, 65536 );
212 LocalInit16( USER_HeapSel
, 32, 65534 );
215 /* some Win9x dlls expect keyboard to be loaded */
216 if (GetVersion() & 0x80000000) LoadLibrary16( "keyboard.drv" );
220 /* Initialize system colors and metrics */
223 /* Setup palette function pointers */
226 /* Initialize built-in window classes */
227 CLASS_RegisterBuiltinClasses();
229 /* Initialize message spying */
230 if (!SPY_Init()) return FALSE
;
236 /**********************************************************************
237 * USER_IsExitingThread
239 BOOL
USER_IsExitingThread( DWORD tid
)
241 return (tid
== exiting_thread_id
);
245 /**********************************************************************
248 static void thread_detach(void)
250 exiting_thread_id
= GetCurrentThreadId();
252 WDML_NotifyThreadDetach();
254 WIN_DestroyThreadWindows( get_user_thread_info()->desktop
);
255 CloseHandle( get_user_thread_info()->server_queue
);
257 exiting_thread_id
= 0;
261 /***********************************************************************
262 * UserClientDllInitialize (USER32.@)
264 * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
266 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, LPVOID reserved
)
271 case DLL_PROCESS_ATTACH
:
272 user32_module
= inst
;
273 ret
= process_attach();
275 case DLL_THREAD_DETACH
:
278 case DLL_PROCESS_DETACH
:
279 USER_unload_driver();
286 /***********************************************************************
287 * USER_GetProcessHandleList(Internal)
289 static HANDLE
*USER_GetProcessHandleList(void)
297 hSnapshot
= CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS
, 0 );
300 ERR("cannot create snapshot\n");
304 /* count the number of processes plus one */
305 for (count
=0; ;count
++)
307 pe
.dwSize
= sizeof pe
;
309 r
= Process32Next( hSnapshot
, &pe
);
311 r
= Process32First( hSnapshot
, &pe
);
316 /* allocate memory make a list of the process handles */
317 list
= HeapAlloc( GetProcessHeap(), 0, (count
+1)*sizeof(HANDLE
) );
319 for (i
=0; i
<count
; i
++)
321 pe
.dwSize
= sizeof pe
;
323 r
= Process32Next( hSnapshot
, &pe
);
325 r
= Process32First( hSnapshot
, &pe
);
329 /* don't kill ourselves */
330 if (GetCurrentProcessId() == pe
.th32ProcessID
)
333 /* open the process so we don't can track it */
334 list
[n
] = OpenProcess( PROCESS_QUERY_INFORMATION
|
336 FALSE
, pe
.th32ProcessID
);
338 /* check it didn't terminate already */
343 CloseHandle( hSnapshot
);
346 ERR("Error enumerating processes\n");
348 TRACE("return %lu processes\n", n
);
354 /***********************************************************************
355 * USER_KillProcesses (Internal)
357 static DWORD
USER_KillProcesses(void)
361 const DWORD dwShutdownTimeout
= 10000;
363 TRACE("terminating other processes\n");
365 /* kill it and add it to our list of object to wait on */
366 handles
= USER_GetProcessHandleList();
367 for (n
=0; handles
&& handles
[n
]; n
++)
368 TerminateProcess( handles
[n
], 0 );
370 /* wait for processes to exit */
371 for (i
=0; i
<n
; i
+=MAXIMUM_WAIT_OBJECTS
)
373 int n_objs
= ((n
-i
)>MAXIMUM_WAIT_OBJECTS
) ? MAXIMUM_WAIT_OBJECTS
: (n
-i
);
374 r
= WaitForMultipleObjects( n_objs
, &handles
[i
], TRUE
, dwShutdownTimeout
);
376 ERR("wait failed!\n");
379 /* close the handles */
381 CloseHandle( handles
[i
] );
383 HeapFree( GetProcessHeap(), 0, handles
);
389 /***********************************************************************
390 * USER_DoShutdown (Internal)
392 static void USER_DoShutdown(void)
395 const DWORD nRetries
= 10;
397 for (i
=0; i
<nRetries
; i
++)
399 n
= USER_KillProcesses();
400 TRACE("Killed %ld processes, attempt %ld\n", n
, i
);
407 /***********************************************************************
408 * ExitWindowsEx (USER32.@)
410 BOOL WINAPI
ExitWindowsEx( UINT flags
, DWORD reason
)
412 TRACE("(%x,%lx)\n", flags
, reason
);
414 if ((flags
& EWX_FORCE
) == 0)
418 /* We have to build a list of all windows first, as in EnumWindows */
419 list
= WIN_ListChildren( GetDesktopWindow() );
426 /* Send a WM_QUERYENDSESSION / WM_ENDSESSION message pair to
427 * each window. Note: it might be better to send all the
428 * WM_QUERYENDSESSION messages, aggregate the results and then
429 * send all the WM_ENDSESSION messages with the results but
430 * that's not what Windows does.
432 send_flags
=(flags
& EWX_FORCEIFHUNG
) ? SMTO_ABORTIFHUNG
: SMTO_NORMAL
;
433 for (phwnd
= list
; *phwnd
; phwnd
++)
435 /* Make sure that the window still exists */
436 if (!IsWindow( *phwnd
)) continue;
437 if (SendMessageTimeoutW( *phwnd
, WM_QUERYENDSESSION
, 0, 0, send_flags
, 0, &result
))
440 SendMessageTimeoutW( *phwnd
, WM_ENDSESSION
, result
, 0, send_flags
, 0, &dummy
);
444 HeapFree( GetProcessHeap(), 0, list
);
451 /* USER_DoShutdown will kill all processes except the current process */
454 if (flags
& EWX_REBOOT
)
456 WCHAR winebootW
[] = { 'w','i','n','e','b','o','o','t',0 };
457 PROCESS_INFORMATION pi
;
460 memset( &si
, 0, sizeof si
);
462 if (CreateProcessW( NULL
, winebootW
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
))
464 CloseHandle( pi
.hProcess
);
465 CloseHandle( pi
.hThread
);
468 MESSAGE("wine: Failed to start wineboot\n");