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, (LPBYTE) 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(CreateDesktopWindow);
128 GET_USER_FUNC(CreateWindow);
129 GET_USER_FUNC(DestroyWindow);
130 GET_USER_FUNC(GetDCEx);
131 GET_USER_FUNC(MsgWaitForMultipleObjectsEx);
132 GET_USER_FUNC(ReleaseDC);
133 GET_USER_FUNC(ScrollDC);
134 GET_USER_FUNC(SetFocus);
135 GET_USER_FUNC(SetParent);
136 GET_USER_FUNC(SetWindowPos);
137 GET_USER_FUNC(SetWindowRgn);
138 GET_USER_FUNC(SetWindowIcon);
139 GET_USER_FUNC(SetWindowStyle);
140 GET_USER_FUNC(SetWindowText);
141 GET_USER_FUNC(ShowWindow);
142 GET_USER_FUNC(SysCommandSizeMove);
143 GET_USER_FUNC(WindowFromDC);
144 GET_USER_FUNC(WindowMessage);
150 /***********************************************************************
155 _EnterSysLevel( &USER_SysLevel );
159 /***********************************************************************
162 void USER_Unlock(void)
164 _LeaveSysLevel( &USER_SysLevel );
168 /***********************************************************************
171 * Make sure that we don't hold the user lock.
173 void USER_CheckNotLock(void)
175 _CheckNotSysLevel( &USER_SysLevel );
179 /***********************************************************************
180 * UserSelectPalette (Not a Windows API)
182 static HPALETTE WINAPI UserSelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
184 WORD wBkgPalette = 1;
186 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
188 HWND hwnd = WindowFromDC( hDC );
191 HWND hForeground = GetForegroundWindow();
192 /* set primary palette if it's related to current active */
193 if (hForeground == hwnd || IsChild(hForeground,hwnd))
196 hPrimaryPalette = hPal;
200 return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
204 /***********************************************************************
205 * UserRealizePalette (USER32.@)
207 UINT WINAPI UserRealizePalette( HDC hDC )
209 UINT realized = pfnGDIRealizePalette( hDC );
211 /* do not send anything if no colors were changed */
212 if (realized && GetCurrentObject( hDC, OBJ_PAL ) == hPrimaryPalette)
214 /* send palette change notification */
215 HWND hWnd = WindowFromDC( hDC );
216 if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
217 SMTO_ABORTIFHUNG, 2000, NULL );
223 /***********************************************************************
226 * Patch the function pointers in GDI for SelectPalette and RealizePalette
228 static void palette_init(void)
231 HMODULE module = GetModuleHandleA( "gdi32" );
234 ERR( "cannot get GDI32 handle\n" );
237 if ((ptr = (void**)GetProcAddress( module, "pfnSelectPalette" )))
238 pfnGDISelectPalette = InterlockedExchangePointer( ptr, UserSelectPalette );
239 else ERR( "cannot find pfnSelectPalette in GDI32\n" );
240 if ((ptr = (void**)GetProcAddress( module, "pfnRealizePalette" )))
241 pfnGDIRealizePalette = InterlockedExchangePointer( ptr, UserRealizePalette );
242 else ERR( "cannot find pfnRealizePalette in GDI32\n" );
246 /***********************************************************************
247 * USER initialisation routine
249 static BOOL process_attach(void)
251 HINSTANCE16 instance;
253 /* Create USER heap */
254 if ((instance = LoadLibrary16( "USER.EXE" )) >= 32) USER_HeapSel = instance | 7;
257 USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 65536 );
258 LocalInit16( USER_HeapSel, 32, 65534 );
261 /* some Win9x dlls expect keyboard to be loaded */
262 if (GetVersion() & 0x80000000) LoadLibrary16( "keyboard.drv" );
264 /* Load the graphics driver */
265 if (!load_driver()) return FALSE;
267 /* Initialize system colors and metrics */
270 /* Setup palette function pointers */
273 /* Initialize built-in window classes */
274 CLASS_RegisterBuiltinClasses();
276 /* Initialize menus */
277 if (!MENU_Init()) return FALSE;
279 /* Initialize message spying */
280 if (!SPY_Init()) return FALSE;
286 /**********************************************************************
287 * USER_IsExitingThread
289 BOOL USER_IsExitingThread( DWORD tid )
291 return (tid == exiting_thread_id);
295 /**********************************************************************
298 static void thread_detach(void)
300 exiting_thread_id = GetCurrentThreadId();
302 WDML_NotifyThreadDetach();
304 WIN_DestroyThreadWindows( GetDesktopWindow() );
305 CloseHandle( get_user_thread_info()->server_queue );
307 exiting_thread_id = 0;
311 /**********************************************************************
314 static void process_detach(void)
316 memset(&USER_Driver, 0, sizeof(USER_Driver));
317 FreeLibrary(graphics_driver);
320 /***********************************************************************
321 * UserClientDllInitialize (USER32.@)
323 * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
325 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
330 case DLL_PROCESS_ATTACH:
331 user32_module = inst;
332 ret = process_attach();
334 case DLL_PROCESS_DETACH:
337 case DLL_THREAD_DETACH:
345 /***********************************************************************
346 * USER_GetProcessHandleList(Internal)
348 static HANDLE *USER_GetProcessHandleList(void)
356 hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
359 ERR("cannot create snapshot\n");
363 /* count the number of processes plus one */
364 for (count=0; ;count++)
366 pe.dwSize = sizeof pe;
368 r = Process32Next( hSnapshot, &pe );
370 r = Process32First( hSnapshot, &pe );
375 /* allocate memory make a list of the process handles */
376 list = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof(HANDLE) );
378 for (i=0; i<count; i++)
380 pe.dwSize = sizeof pe;
382 r = Process32Next( hSnapshot, &pe );
384 r = Process32First( hSnapshot, &pe );
388 /* don't kill ourselves */
389 if (GetCurrentProcessId() == pe.th32ProcessID )
392 /* open the process so we don't can track it */
393 list[n] = OpenProcess( PROCESS_QUERY_INFORMATION|
395 FALSE, pe.th32ProcessID );
397 /* check it didn't terminate already */
402 CloseHandle( hSnapshot );
405 ERR("Error enumerating processes\n");
407 TRACE("return %lu processes\n", n);
413 /***********************************************************************
414 * USER_KillProcesses (Internal)
416 static DWORD USER_KillProcesses(void)
420 const DWORD dwShutdownTimeout = 10000;
422 TRACE("terminating other processes\n");
424 /* kill it and add it to our list of object to wait on */
425 handles = USER_GetProcessHandleList();
426 for (n=0; handles && handles[n]; n++)
427 TerminateProcess( handles[n], 0 );
429 /* wait for processes to exit */
430 for (i=0; i<n; i+=MAXIMUM_WAIT_OBJECTS)
432 int n_objs = ((n-i)>MAXIMUM_WAIT_OBJECTS) ? MAXIMUM_WAIT_OBJECTS : (n-i);
433 r = WaitForMultipleObjects( n_objs, &handles[i], TRUE, dwShutdownTimeout );
435 ERR("wait failed!\n");
438 /* close the handles */
440 CloseHandle( handles[i] );
442 HeapFree( GetProcessHeap(), 0, handles );
448 /***********************************************************************
449 * USER_DoShutdown (Internal)
451 static void USER_DoShutdown(void)
454 const DWORD nRetries = 10;
456 for (i=0; i<nRetries; i++)
458 n = USER_KillProcesses();
459 TRACE("Killed %ld processes, attempt %ld\n", n, i);
466 /***********************************************************************
467 * ExitWindowsEx (USER32.@)
469 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reserved )
475 /* We have to build a list of all windows first, as in EnumWindows */
476 TRACE("(%x,%lx)\n", flags, reserved);
478 list = WIN_ListChildren( GetDesktopWindow() );
481 /* Send a WM_QUERYENDSESSION message to every window */
483 for (i = 0; list[i]; i++)
485 /* Make sure that the window still exists */
486 if (!IsWindow( list[i] )) continue;
487 if (!SendMessageW( list[i], WM_QUERYENDSESSION, 0, 0 )) break;
491 /* Now notify all windows that got a WM_QUERYENDSESSION of the result */
493 for (phwnd = list; i > 0; i--, phwnd++)
495 if (!IsWindow( *phwnd )) continue;
496 SendMessageW( *phwnd, WM_ENDSESSION, result, 0 );
498 HeapFree( GetProcessHeap(), 0, list );
500 if ( !(result || (flags & EWX_FORCE) ))
504 /* USER_DoShutdown will kill all processes except the current process */
507 if (flags & EWX_REBOOT)
509 WCHAR winebootW[] = { 'w','i','n','e','b','o','o','t',0 };
510 PROCESS_INFORMATION pi;
513 memset( &si, 0, sizeof si );
515 if (CreateProcessW( NULL, winebootW, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
517 CloseHandle( pi.hProcess );
518 CloseHandle( pi.hThread );
521 MESSAGE("wine: Failed to start wineboot\n");
524 if (result) ExitProcess(0);