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 USER_DRIVER USER_Driver
;
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, { 0, (DWORD
)(__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 HMODULE graphics_driver
;
57 static DWORD exiting_thread_id
;
59 extern void WDML_NotifyThreadDetach(void);
61 #define GET_USER_FUNC(name) USER_Driver.p##name = (void*)GetProcAddress( graphics_driver, #name )
63 /* load the graphics driver */
64 static BOOL
load_driver(void)
66 char buffer
[MAX_PATH
], libname
[32], *name
, *next
;
69 strcpy( buffer
, "x11,tty" ); /* default value */
70 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
71 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Drivers", &hkey
))
73 DWORD type
, count
= sizeof(buffer
);
74 RegQueryValueExA( hkey
, "Graphics", 0, &type
, buffer
, &count
);
81 next
= strchr( name
, ',' );
82 if (next
) *next
++ = 0;
84 snprintf( libname
, sizeof(libname
), "wine%s.drv", name
);
85 if ((graphics_driver
= LoadLibraryA( libname
)) != 0) break;
90 MESSAGE( "wine: Could not load graphics driver '%s'.\n", buffer
);
91 if (!strcasecmp( buffer
, "x11" ))
92 MESSAGE( "Make sure that your X server is running and that $DISPLAY is set correctly.\n" );
96 GET_USER_FUNC(ActivateKeyboardLayout
);
98 GET_USER_FUNC(GetAsyncKeyState
);
99 GET_USER_FUNC(GetKeyNameText
);
100 GET_USER_FUNC(GetKeyboardLayout
);
101 GET_USER_FUNC(GetKeyboardLayoutList
);
102 GET_USER_FUNC(GetKeyboardLayoutName
);
103 GET_USER_FUNC(LoadKeyboardLayout
);
104 GET_USER_FUNC(MapVirtualKeyEx
);
105 GET_USER_FUNC(SendInput
);
106 GET_USER_FUNC(ToUnicodeEx
);
107 GET_USER_FUNC(UnloadKeyboardLayout
);
108 GET_USER_FUNC(VkKeyScanEx
);
109 GET_USER_FUNC(SetCursor
);
110 GET_USER_FUNC(GetCursorPos
);
111 GET_USER_FUNC(SetCursorPos
);
112 GET_USER_FUNC(GetScreenSaveActive
);
113 GET_USER_FUNC(SetScreenSaveActive
);
114 GET_USER_FUNC(AcquireClipboard
);
115 GET_USER_FUNC(EmptyClipboard
);
116 GET_USER_FUNC(SetClipboardData
);
117 GET_USER_FUNC(GetClipboardData
);
118 GET_USER_FUNC(CountClipboardFormats
);
119 GET_USER_FUNC(EnumClipboardFormats
);
120 GET_USER_FUNC(IsClipboardFormatAvailable
);
121 GET_USER_FUNC(RegisterClipboardFormat
);
122 GET_USER_FUNC(GetClipboardFormatName
);
123 GET_USER_FUNC(EndClipboardUpdate
);
124 GET_USER_FUNC(ResetSelectionOwner
);
125 GET_USER_FUNC(ChangeDisplaySettingsExW
);
126 GET_USER_FUNC(EnumDisplaySettingsExW
);
127 GET_USER_FUNC(CreateWindow
);
128 GET_USER_FUNC(DestroyWindow
);
129 GET_USER_FUNC(GetDCEx
);
130 GET_USER_FUNC(MsgWaitForMultipleObjectsEx
);
131 GET_USER_FUNC(ReleaseDC
);
132 GET_USER_FUNC(ScrollDC
);
133 GET_USER_FUNC(SetFocus
);
134 GET_USER_FUNC(SetParent
);
135 GET_USER_FUNC(SetWindowPos
);
136 GET_USER_FUNC(SetWindowRgn
);
137 GET_USER_FUNC(SetWindowIcon
);
138 GET_USER_FUNC(SetWindowStyle
);
139 GET_USER_FUNC(SetWindowText
);
140 GET_USER_FUNC(ShowWindow
);
141 GET_USER_FUNC(SysCommandSizeMove
);
142 GET_USER_FUNC(WindowFromDC
);
143 GET_USER_FUNC(WindowMessage
);
149 /***********************************************************************
154 _EnterSysLevel( &USER_SysLevel
);
158 /***********************************************************************
161 void USER_Unlock(void)
163 _LeaveSysLevel( &USER_SysLevel
);
167 /***********************************************************************
170 * Make sure that we don't hold the user lock.
172 void USER_CheckNotLock(void)
174 _CheckNotSysLevel( &USER_SysLevel
);
178 /***********************************************************************
179 * UserSelectPalette (Not a Windows API)
181 static HPALETTE WINAPI
UserSelectPalette( HDC hDC
, HPALETTE hPal
, BOOL bForceBackground
)
183 WORD wBkgPalette
= 1;
185 if (!bForceBackground
&& (hPal
!= GetStockObject(DEFAULT_PALETTE
)))
187 HWND hwnd
= WindowFromDC( hDC
);
190 HWND hForeground
= GetForegroundWindow();
191 /* set primary palette if it's related to current active */
192 if (hForeground
== hwnd
|| IsChild(hForeground
,hwnd
))
195 hPrimaryPalette
= hPal
;
199 return pfnGDISelectPalette( hDC
, hPal
, wBkgPalette
);
203 /***********************************************************************
204 * UserRealizePalette (USER32.@)
206 UINT WINAPI
UserRealizePalette( HDC hDC
)
208 UINT realized
= pfnGDIRealizePalette( hDC
);
210 /* do not send anything if no colors were changed */
211 if (realized
&& GetCurrentObject( hDC
, OBJ_PAL
) == hPrimaryPalette
)
213 /* send palette change notification */
214 HWND hWnd
= WindowFromDC( hDC
);
215 if (hWnd
) SendMessageTimeoutW( HWND_BROADCAST
, WM_PALETTECHANGED
, (WPARAM
)hWnd
, 0,
216 SMTO_ABORTIFHUNG
, 2000, NULL
);
222 /***********************************************************************
225 * Patch the function pointers in GDI for SelectPalette and RealizePalette
227 static void palette_init(void)
230 HMODULE module
= GetModuleHandleA( "gdi32" );
233 ERR( "cannot get GDI32 handle\n" );
236 if ((ptr
= (void**)GetProcAddress( module
, "pfnSelectPalette" )))
237 pfnGDISelectPalette
= InterlockedExchangePointer( ptr
, UserSelectPalette
);
238 else ERR( "cannot find pfnSelectPalette in GDI32\n" );
239 if ((ptr
= (void**)GetProcAddress( module
, "pfnRealizePalette" )))
240 pfnGDIRealizePalette
= InterlockedExchangePointer( ptr
, UserRealizePalette
);
241 else ERR( "cannot find pfnRealizePalette in GDI32\n" );
245 /***********************************************************************
246 * USER initialisation routine
248 static BOOL
process_attach(void)
250 HINSTANCE16 instance
;
252 /* Create USER heap */
253 if ((instance
= LoadLibrary16( "USER.EXE" )) >= 32) USER_HeapSel
= instance
| 7;
256 USER_HeapSel
= GlobalAlloc16( GMEM_FIXED
, 65536 );
257 LocalInit16( USER_HeapSel
, 32, 65534 );
260 /* some Win9x dlls expect keyboard to be loaded */
261 if (GetVersion() & 0x80000000) LoadLibrary16( "keyboard.drv" );
263 /* Load the graphics driver */
264 if (!load_driver()) return FALSE
;
266 /* Initialize system colors and metrics */
269 /* Setup palette function pointers */
272 /* Initialize built-in window classes */
273 CLASS_RegisterBuiltinClasses();
275 /* Initialize menus */
276 if (!MENU_Init()) return FALSE
;
278 /* Initialize message spying */
279 if (!SPY_Init()) return FALSE
;
281 /* Create desktop window */
282 if (!WIN_CreateDesktopWindow()) return FALSE
;
288 /**********************************************************************
289 * USER_IsExitingThread
291 BOOL
USER_IsExitingThread( DWORD tid
)
293 return (tid
== exiting_thread_id
);
297 /**********************************************************************
300 static void thread_detach(void)
302 exiting_thread_id
= GetCurrentThreadId();
304 WDML_NotifyThreadDetach();
306 WIN_DestroyThreadWindows( GetDesktopWindow() );
307 CloseHandle( get_user_thread_info()->server_queue
);
309 exiting_thread_id
= 0;
313 /**********************************************************************
316 static void process_detach(void)
318 memset(&USER_Driver
, 0, sizeof(USER_Driver
));
319 FreeLibrary(graphics_driver
);
322 /***********************************************************************
323 * UserClientDllInitialize (USER32.@)
325 * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
327 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, LPVOID reserved
)
332 case DLL_PROCESS_ATTACH
:
333 user32_module
= inst
;
334 ret
= process_attach();
336 case DLL_PROCESS_DETACH
:
339 case DLL_THREAD_DETACH
:
347 /***********************************************************************
348 * USER_GetProcessHandleList(Internal)
350 static HANDLE
*USER_GetProcessHandleList(void)
358 hSnapshot
= CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS
, 0 );
361 ERR("cannot create snapshot\n");
365 /* count the number of processes plus one */
366 for (count
=0; ;count
++)
368 pe
.dwSize
= sizeof pe
;
370 r
= Process32Next( hSnapshot
, &pe
);
372 r
= Process32First( hSnapshot
, &pe
);
377 /* allocate memory make a list of the process handles */
378 list
= HeapAlloc( GetProcessHeap(), 0, (count
+1)*sizeof(HANDLE
) );
380 for (i
=0; i
<count
; i
++)
382 pe
.dwSize
= sizeof pe
;
384 r
= Process32Next( hSnapshot
, &pe
);
386 r
= Process32First( hSnapshot
, &pe
);
390 /* don't kill ourselves */
391 if (GetCurrentProcessId() == pe
.th32ProcessID
)
394 /* open the process so we don't can track it */
395 list
[n
] = OpenProcess( PROCESS_QUERY_INFORMATION
|
397 FALSE
, pe
.th32ProcessID
);
399 /* check it didn't terminate already */
404 CloseHandle( hSnapshot
);
407 ERR("Error enumerating processes\n");
409 TRACE("return %lu processes\n", n
);
415 /***********************************************************************
416 * USER_KillProcesses (Internal)
418 static DWORD
USER_KillProcesses(void)
422 const DWORD dwShutdownTimeout
= 10000;
424 TRACE("terminating other processes\n");
426 /* kill it and add it to our list of object to wait on */
427 handles
= USER_GetProcessHandleList();
428 for (n
=0; handles
&& handles
[n
]; n
++)
429 TerminateProcess( handles
[n
], 0 );
431 /* wait for processes to exit */
432 for (i
=0; i
<n
; i
+=MAXIMUM_WAIT_OBJECTS
)
434 int n_objs
= ((n
-i
)>MAXIMUM_WAIT_OBJECTS
) ? MAXIMUM_WAIT_OBJECTS
: (n
-i
);
435 r
= WaitForMultipleObjects( n_objs
, &handles
[i
], TRUE
, dwShutdownTimeout
);
437 ERR("wait failed!\n");
440 /* close the handles */
442 CloseHandle( handles
[i
] );
444 HeapFree( GetProcessHeap(), 0, handles
);
450 /***********************************************************************
451 * USER_DoShutdown (Internal)
453 static void USER_DoShutdown(void)
456 const DWORD nRetries
= 10;
458 for (i
=0; i
<nRetries
; i
++)
460 n
= USER_KillProcesses();
461 TRACE("Killed %ld processes, attempt %ld\n", n
, i
);
468 /***********************************************************************
469 * ExitWindowsEx (USER32.@)
471 BOOL WINAPI
ExitWindowsEx( UINT flags
, DWORD reserved
)
477 /* We have to build a list of all windows first, as in EnumWindows */
478 TRACE("(%x,%lx)\n", flags
, reserved
);
480 list
= WIN_ListChildren( GetDesktopWindow() );
483 /* Send a WM_QUERYENDSESSION message to every window */
485 for (i
= 0; list
[i
]; i
++)
487 /* Make sure that the window still exists */
488 if (!IsWindow( list
[i
] )) continue;
489 if (!SendMessageW( list
[i
], WM_QUERYENDSESSION
, 0, 0 )) break;
493 /* Now notify all windows that got a WM_QUERYENDSESSION of the result */
495 for (phwnd
= list
; i
> 0; i
--, phwnd
++)
497 if (!IsWindow( *phwnd
)) continue;
498 SendMessageW( *phwnd
, WM_ENDSESSION
, result
, 0 );
500 HeapFree( GetProcessHeap(), 0, list
);
502 if ( !(result
|| (flags
& EWX_FORCE
) ))
506 /* USER_DoShutdown will kill all processes except the current process */
509 if (flags
& EWX_REBOOT
)
511 WCHAR winebootW
[] = { 'w','i','n','e','b','o','o','t',0 };
512 PROCESS_INFORMATION pi
;
515 memset( &si
, 0, sizeof si
);
517 if (CreateProcessW( NULL
, winebootW
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
))
519 CloseHandle( pi
.hProcess
);
520 CloseHandle( pi
.hThread
);
523 MESSAGE("wine: Failed to start wineboot\n");
526 if (result
) ExitProcess(0);