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 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE
, "Software\\Wine\\Drivers", &hkey
))
72 DWORD type
, count
= sizeof(buffer
);
73 RegQueryValueExA( hkey
, "Graphics", 0, &type
, buffer
, &count
);
80 next
= strchr( name
, ',' );
81 if (next
) *next
++ = 0;
83 snprintf( libname
, sizeof(libname
), "wine%s.drv", name
);
84 if ((graphics_driver
= LoadLibraryA( libname
)) != 0) break;
89 MESSAGE( "wine: Could not load graphics driver '%s'.\n", buffer
);
90 if (!strcasecmp( buffer
, "x11" ))
91 MESSAGE( "Make sure that your X server is running and that $DISPLAY is set correctly.\n" );
95 GET_USER_FUNC(ActivateKeyboardLayout
);
97 GET_USER_FUNC(GetAsyncKeyState
);
98 GET_USER_FUNC(GetKeyNameText
);
99 GET_USER_FUNC(GetKeyboardLayout
);
100 GET_USER_FUNC(GetKeyboardLayoutList
);
101 GET_USER_FUNC(GetKeyboardLayoutName
);
102 GET_USER_FUNC(LoadKeyboardLayout
);
103 GET_USER_FUNC(MapVirtualKeyEx
);
104 GET_USER_FUNC(SendInput
);
105 GET_USER_FUNC(ToUnicodeEx
);
106 GET_USER_FUNC(UnloadKeyboardLayout
);
107 GET_USER_FUNC(VkKeyScanEx
);
108 GET_USER_FUNC(SetCursor
);
109 GET_USER_FUNC(GetCursorPos
);
110 GET_USER_FUNC(SetCursorPos
);
111 GET_USER_FUNC(GetScreenSaveActive
);
112 GET_USER_FUNC(SetScreenSaveActive
);
113 GET_USER_FUNC(AcquireClipboard
);
114 GET_USER_FUNC(EmptyClipboard
);
115 GET_USER_FUNC(SetClipboardData
);
116 GET_USER_FUNC(GetClipboardData
);
117 GET_USER_FUNC(CountClipboardFormats
);
118 GET_USER_FUNC(EnumClipboardFormats
);
119 GET_USER_FUNC(IsClipboardFormatAvailable
);
120 GET_USER_FUNC(RegisterClipboardFormat
);
121 GET_USER_FUNC(GetClipboardFormatName
);
122 GET_USER_FUNC(EndClipboardUpdate
);
123 GET_USER_FUNC(ResetSelectionOwner
);
124 GET_USER_FUNC(ChangeDisplaySettingsExW
);
125 GET_USER_FUNC(EnumDisplaySettingsExW
);
126 GET_USER_FUNC(CreateWindow
);
127 GET_USER_FUNC(DestroyWindow
);
128 GET_USER_FUNC(GetDCEx
);
129 GET_USER_FUNC(MsgWaitForMultipleObjectsEx
);
130 GET_USER_FUNC(ReleaseDC
);
131 GET_USER_FUNC(ScrollDC
);
132 GET_USER_FUNC(SetFocus
);
133 GET_USER_FUNC(SetParent
);
134 GET_USER_FUNC(SetWindowPos
);
135 GET_USER_FUNC(SetWindowRgn
);
136 GET_USER_FUNC(SetWindowIcon
);
137 GET_USER_FUNC(SetWindowStyle
);
138 GET_USER_FUNC(SetWindowText
);
139 GET_USER_FUNC(ShowWindow
);
140 GET_USER_FUNC(SysCommandSizeMove
);
141 GET_USER_FUNC(WindowFromDC
);
142 GET_USER_FUNC(WindowMessage
);
148 /***********************************************************************
153 _EnterSysLevel( &USER_SysLevel
);
157 /***********************************************************************
160 void USER_Unlock(void)
162 _LeaveSysLevel( &USER_SysLevel
);
166 /***********************************************************************
169 * Make sure that we don't hold the user lock.
171 void USER_CheckNotLock(void)
173 _CheckNotSysLevel( &USER_SysLevel
);
177 /***********************************************************************
178 * UserSelectPalette (Not a Windows API)
180 static HPALETTE WINAPI
UserSelectPalette( HDC hDC
, HPALETTE hPal
, BOOL bForceBackground
)
182 WORD wBkgPalette
= 1;
184 if (!bForceBackground
&& (hPal
!= GetStockObject(DEFAULT_PALETTE
)))
186 HWND hwnd
= WindowFromDC( hDC
);
189 HWND hForeground
= GetForegroundWindow();
190 /* set primary palette if it's related to current active */
191 if (hForeground
== hwnd
|| IsChild(hForeground
,hwnd
))
194 hPrimaryPalette
= hPal
;
198 return pfnGDISelectPalette( hDC
, hPal
, wBkgPalette
);
202 /***********************************************************************
203 * UserRealizePalette (USER32.@)
205 UINT WINAPI
UserRealizePalette( HDC hDC
)
207 UINT realized
= pfnGDIRealizePalette( hDC
);
209 /* do not send anything if no colors were changed */
210 if (realized
&& GetCurrentObject( hDC
, OBJ_PAL
) == hPrimaryPalette
)
212 /* send palette change notification */
213 HWND hWnd
= WindowFromDC( hDC
);
214 if (hWnd
) SendMessageTimeoutW( HWND_BROADCAST
, WM_PALETTECHANGED
, (WPARAM
)hWnd
, 0,
215 SMTO_ABORTIFHUNG
, 2000, NULL
);
221 /***********************************************************************
224 * Patch the function pointers in GDI for SelectPalette and RealizePalette
226 static void palette_init(void)
229 HMODULE module
= GetModuleHandleA( "gdi32" );
232 ERR( "cannot get GDI32 handle\n" );
235 if ((ptr
= (void**)GetProcAddress( module
, "pfnSelectPalette" )))
236 pfnGDISelectPalette
= InterlockedExchangePointer( ptr
, UserSelectPalette
);
237 else ERR( "cannot find pfnSelectPalette in GDI32\n" );
238 if ((ptr
= (void**)GetProcAddress( module
, "pfnRealizePalette" )))
239 pfnGDIRealizePalette
= InterlockedExchangePointer( ptr
, UserRealizePalette
);
240 else ERR( "cannot find pfnRealizePalette in GDI32\n" );
244 /***********************************************************************
245 * USER initialisation routine
247 static BOOL
process_attach(void)
249 HINSTANCE16 instance
;
251 /* Create USER heap */
252 if ((instance
= LoadLibrary16( "USER.EXE" )) >= 32) USER_HeapSel
= instance
| 7;
255 USER_HeapSel
= GlobalAlloc16( GMEM_FIXED
, 65536 );
256 LocalInit16( USER_HeapSel
, 32, 65534 );
259 /* some Win9x dlls expect keyboard to be loaded */
260 if (GetVersion() & 0x80000000) LoadLibrary16( "keyboard.drv" );
262 /* Load the graphics driver */
263 if (!load_driver()) return FALSE
;
265 /* Initialize system colors and metrics */
268 /* Setup palette function pointers */
271 /* Initialize built-in window classes */
272 CLASS_RegisterBuiltinClasses();
274 /* Initialize menus */
275 if (!MENU_Init()) return FALSE
;
277 /* Initialize message spying */
278 if (!SPY_Init()) return FALSE
;
280 /* Create desktop window */
281 if (!WIN_CreateDesktopWindow()) return FALSE
;
287 /**********************************************************************
288 * USER_IsExitingThread
290 BOOL
USER_IsExitingThread( DWORD tid
)
292 return (tid
== exiting_thread_id
);
296 /**********************************************************************
299 static void thread_detach(void)
301 exiting_thread_id
= GetCurrentThreadId();
303 WDML_NotifyThreadDetach();
305 WIN_DestroyThreadWindows( GetDesktopWindow() );
306 CloseHandle( get_user_thread_info()->server_queue
);
308 exiting_thread_id
= 0;
312 /**********************************************************************
315 static void process_detach(void)
317 memset(&USER_Driver
, 0, sizeof(USER_Driver
));
318 FreeLibrary(graphics_driver
);
321 /***********************************************************************
322 * UserClientDllInitialize (USER32.@)
324 * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
326 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, LPVOID reserved
)
331 case DLL_PROCESS_ATTACH
:
332 user32_module
= inst
;
333 ret
= process_attach();
335 case DLL_PROCESS_DETACH
:
338 case DLL_THREAD_DETACH
:
346 /***********************************************************************
347 * USER_GetProcessHandleList(Internal)
349 static HANDLE
*USER_GetProcessHandleList(void)
357 hSnapshot
= CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS
, 0 );
360 ERR("cannot create snapshot\n");
364 /* count the number of processes plus one */
365 for (count
=0; ;count
++)
367 pe
.dwSize
= sizeof pe
;
369 r
= Process32Next( hSnapshot
, &pe
);
371 r
= Process32First( hSnapshot
, &pe
);
376 /* allocate memory make a list of the process handles */
377 list
= HeapAlloc( GetProcessHeap(), 0, (count
+1)*sizeof(HANDLE
) );
379 for (i
=0; i
<count
; i
++)
381 pe
.dwSize
= sizeof pe
;
383 r
= Process32Next( hSnapshot
, &pe
);
385 r
= Process32First( hSnapshot
, &pe
);
389 /* don't kill ourselves */
390 if (GetCurrentProcessId() == pe
.th32ProcessID
)
393 /* open the process so we don't can track it */
394 list
[n
] = OpenProcess( PROCESS_QUERY_INFORMATION
|
396 FALSE
, pe
.th32ProcessID
);
398 /* check it didn't terminate already */
403 CloseHandle( hSnapshot
);
406 ERR("Error enumerating processes\n");
408 TRACE("return %lu processes\n", n
);
414 /***********************************************************************
415 * USER_KillProcesses (Internal)
417 static DWORD
USER_KillProcesses(void)
421 const DWORD dwShutdownTimeout
= 10000;
423 TRACE("terminating other processes\n");
425 /* kill it and add it to our list of object to wait on */
426 handles
= USER_GetProcessHandleList();
427 for (n
=0; handles
&& handles
[n
]; n
++)
428 TerminateProcess( handles
[n
], 0 );
430 /* wait for processes to exit */
431 for (i
=0; i
<n
; i
+=MAXIMUM_WAIT_OBJECTS
)
433 int n_objs
= ((n
-i
)>MAXIMUM_WAIT_OBJECTS
) ? MAXIMUM_WAIT_OBJECTS
: (n
-i
);
434 r
= WaitForMultipleObjects( n_objs
, &handles
[i
], TRUE
, dwShutdownTimeout
);
436 ERR("wait failed!\n");
439 /* close the handles */
441 CloseHandle( handles
[i
] );
443 HeapFree( GetProcessHeap(), 0, handles
);
449 /***********************************************************************
450 * USER_DoShutdown (Internal)
452 static void USER_DoShutdown(void)
455 const DWORD nRetries
= 10;
457 for (i
=0; i
<nRetries
; i
++)
459 n
= USER_KillProcesses();
460 TRACE("Killed %ld processes, attempt %ld\n", n
, i
);
467 /***********************************************************************
468 * ExitWindowsEx (USER32.@)
470 BOOL WINAPI
ExitWindowsEx( UINT flags
, DWORD reserved
)
476 /* We have to build a list of all windows first, as in EnumWindows */
477 TRACE("(%x,%lx)\n", flags
, reserved
);
479 list
= WIN_ListChildren( GetDesktopWindow() );
482 /* Send a WM_QUERYENDSESSION message to every window */
484 for (i
= 0; list
[i
]; i
++)
486 /* Make sure that the window still exists */
487 if (!IsWindow( list
[i
] )) continue;
488 if (!SendMessageW( list
[i
], WM_QUERYENDSESSION
, 0, 0 )) break;
492 /* Now notify all windows that got a WM_QUERYENDSESSION of the result */
494 for (phwnd
= list
; i
> 0; i
--, phwnd
++)
496 if (!IsWindow( *phwnd
)) continue;
497 SendMessageW( *phwnd
, WM_ENDSESSION
, result
, 0 );
499 HeapFree( GetProcessHeap(), 0, list
);
501 if ( !(result
|| (flags
& EWX_FORCE
) ))
505 /* USER_DoShutdown will kill all processes except the current process */
508 if (flags
& EWX_REBOOT
)
510 WCHAR winebootW
[] = { 'w','i','n','e','b','o','o','t',0 };
511 PROCESS_INFORMATION pi
;
514 memset( &si
, 0, sizeof si
);
516 if (CreateProcessW( NULL
, winebootW
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
))
518 CloseHandle( pi
.hProcess
);
519 CloseHandle( pi
.hThread
);
522 MESSAGE("wine: Failed to start wineboot\n");
525 if (result
) ExitProcess(0);