1 /*
2 * USER initialization code
3 *
4 * Copyright 2000 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28
29 #include "controls.h"
30 #include "user_private.h"
31 #include "win.h"
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
36
37 #define DESKTOP_ALL_ACCESS 0x01ff
38
39 HMODULE user32_module = 0;
40
41 static CRITICAL_SECTION user_section;
42 static CRITICAL_SECTION_DEBUG critsect_debug =
43 {
44 0, 0, &user_section,
45 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
46 0, 0, { (DWORD_PTR)(__FILE__ ": user_section") }
47 };
48 static CRITICAL_SECTION user_section = { &critsect_debug, -1, 0, 0, 0, 0 };
49
50 static HPALETTE (WINAPI *pfnGDISelectPalette)( HDC hdc, HPALETTE hpal, WORD bkgnd );
51 static UINT (WINAPI *pfnGDIRealizePalette)( HDC hdc );
52 static HPALETTE hPrimaryPalette;
53
54 static DWORD exiting_thread_id;
55
56 extern void WDML_NotifyThreadDetach(void);
57
58
59 /***********************************************************************
60 * USER_Lock
61 */
62 void USER_Lock(void)
63 {
64 EnterCriticalSection( &user_section );
65 }
66
67
68 /***********************************************************************
69 * USER_Unlock
70 */
71 void USER_Unlock(void)
72 {
73 LeaveCriticalSection( &user_section );
74 }
75
76
77 /***********************************************************************
78 * USER_CheckNotLock
79 *
80 * Make sure that we don't hold the user lock.
81 */
82 void USER_CheckNotLock(void)
83 {
84 if (user_section.OwningThread == ULongToHandle(GetCurrentThreadId()) && user_section.RecursionCount)
85 {
86 ERR( "BUG: holding USER lock\n" );
87 DebugBreak();
88 }
89 }
90
91
92 /***********************************************************************
93 * UserSelectPalette (Not a Windows API)
94 */
95 static HPALETTE WINAPI UserSelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
96 {
97 WORD wBkgPalette = 1;
98
99 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
100 {
101 HWND hwnd = WindowFromDC( hDC );
102 if (hwnd)
103 {
104 HWND hForeground = GetForegroundWindow();
105 /* set primary palette if it's related to current active */
106 if (hForeground == hwnd || IsChild(hForeground,hwnd))
107 {
108 wBkgPalette = 0;
109 hPrimaryPalette = hPal;
110 }
111 }
112 }
113 return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
114 }
115
116
117 /***********************************************************************
118 * UserRealizePalette (USER32.@)
119 */
120 UINT WINAPI UserRealizePalette( HDC hDC )
121 {
122 UINT realized = pfnGDIRealizePalette( hDC );
123
124 /* do not send anything if no colors were changed */
125 if (realized && GetCurrentObject( hDC, OBJ_PAL ) == hPrimaryPalette)
126 {
127 /* send palette change notification */
128 HWND hWnd = WindowFromDC( hDC );
129 if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
130 SMTO_ABORTIFHUNG, 2000, NULL );
131 }
132 return realized;
133 }
134
135
136 /***********************************************************************
137 * palette_init
138 *
139 * Patch the function pointers in GDI for SelectPalette and RealizePalette
140 */
141 static void palette_init(void)
142 {
143 void **ptr;
144 HMODULE module = GetModuleHandleA( "gdi32" );
145 if (!module)
146 {
147 ERR( "cannot get GDI32 handle\n" );
148 return;
149 }
150 if ((ptr = (void**)GetProcAddress( module, "pfnSelectPalette" )))
151 pfnGDISelectPalette = InterlockedExchangePointer( ptr, UserSelectPalette );
152 else ERR( "cannot find pfnSelectPalette in GDI32\n" );
153 if ((ptr = (void**)GetProcAddress( module, "pfnRealizePalette" )))
154 pfnGDIRealizePalette = InterlockedExchangePointer( ptr, UserRealizePalette );
155 else ERR( "cannot find pfnRealizePalette in GDI32\n" );
156 }
157
158
159 /***********************************************************************
160 * get_default_desktop
161 *
162 * Get the name of the desktop to use for this app if not specified explicitly.
163 */
164 static const WCHAR *get_default_desktop(void)
165 {
166 static const WCHAR defaultW[] = {'D','e','f','a','u','l','t',0};
167 static const WCHAR desktopW[] = {'D','e','s','k','t','o','p',0};
168 static const WCHAR explorerW[] = {'\\','E','x','p','l','o','r','e','r',0};
169 static const WCHAR app_defaultsW[] = {'S','o','f','t','w','a','r','e','\\',
170 'W','i','n','e','\\',
171 'A','p','p','D','e','f','a','u','l','t','s',0};
172 static WCHAR buffer[MAX_PATH + sizeof(explorerW)/sizeof(WCHAR)];
173 WCHAR *p, *appname = buffer;
174 const WCHAR *ret = defaultW;
175 DWORD len;
176 HKEY tmpkey, appkey;
177
178 len = (GetModuleFileNameW( 0, buffer, MAX_PATH ));
179 if (!len || len >= MAX_PATH) return ret;
180 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
181 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
182 p = appname + strlenW(appname);
183 strcpyW( p, explorerW );
184
185 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Explorer */
186 if (!RegOpenKeyW( HKEY_CURRENT_USER, app_defaultsW, &tmpkey ))
187 {
188 if (RegOpenKeyW( tmpkey, appname, &appkey )) appkey = 0;
189 RegCloseKey( tmpkey );
190 if (appkey)
191 {
192 len = sizeof(buffer);
193 if (!RegQueryValueExW( appkey, desktopW, 0, NULL, (LPBYTE)buffer, &len )) ret = buffer;
194 RegCloseKey( appkey );
195 if (ret && strcmpiW( ret, defaultW )) return ret;
196 ret = defaultW;
197 }
198 }
199
200 memcpy( buffer, app_defaultsW, 13 * sizeof(WCHAR) ); /* copy only software\\wine */
201 strcpyW( buffer + 13, explorerW );
202
203 /* @@ Wine registry key: HKCU\Software\Wine\Explorer */
204 if (!RegOpenKeyW( HKEY_CURRENT_USER, buffer, &appkey ))
205 {
206 len = sizeof(buffer);
207 if (!RegQueryValueExW( appkey, desktopW, 0, NULL, (LPBYTE)buffer, &len )) ret = buffer;
208 RegCloseKey( appkey );
209 }
210 return ret;
211 }
212
213
214 /***********************************************************************
215 * winstation_init
216 *
217 * Connect to the process window station and desktop.
218 */
219 static void winstation_init(void)
220 {
221 static const WCHAR WinSta0[] = {'W','i','n','S','t','a','',0};
222
223 STARTUPINFOW info;
224 WCHAR *winstation = NULL, *desktop = NULL, *buffer = NULL;
225 HANDLE handle;
226
227 GetStartupInfoW( &info );
228 if (info.lpDesktop && *info.lpDesktop)
229 {
230 buffer = HeapAlloc( GetProcessHeap(), 0, (strlenW(info.lpDesktop) + 1) * sizeof(WCHAR) );
231 strcpyW( buffer, info.lpDesktop );
232 if ((desktop = strchrW( buffer, '\\' )))
233 {
234 *desktop++ = 0;
235 winstation = buffer;
236 }
237 else desktop = buffer;
238 }
239
240 /* set winstation if explicitly specified, or if we don't have one yet */
241 if (buffer || !GetProcessWindowStation())
242 {
243 handle = CreateWindowStationW( winstation ? winstation : WinSta0, 0, WINSTA_ALL_ACCESS, NULL );
244 if (handle)
245 {
246 SetProcessWindowStation( handle );
247 /* only WinSta0 is visible */
248 if (!winstation || !strcmpiW( winstation, WinSta0 ))
249 {
250 USEROBJECTFLAGS flags;
251 flags.fInherit = FALSE;
252 flags.fReserved = FALSE;
253 flags.dwFlags = WSF_VISIBLE;
254 SetUserObjectInformationW( handle, UOI_FLAGS, &flags, sizeof(flags) );
255 }
256 }
257 }
258 if (buffer || !GetThreadDesktop( GetCurrentThreadId() ))
259 {
260 handle = CreateDesktopW( desktop ? desktop : get_default_desktop(),
261 NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
262 if (handle) SetThreadDesktop( handle );
263 }
264 HeapFree( GetProcessHeap(), 0, buffer );
265 }
266
267
268 /***********************************************************************
269 * USER initialisation routine
270 */
271 static BOOL process_attach(void)
272 {
273 winstation_init();
274
275 /* Initialize system colors and metrics */
276 SYSPARAMS_Init();
277
278 /* Setup palette function pointers */
279 palette_init();
280
281 /* Initialize built-in window classes */
282 CLASS_RegisterBuiltinClasses();
283
284 /* Initialize message spying */
285 if (!SPY_Init()) return FALSE;
286
287 return TRUE;
288 }
289
290
291 /**********************************************************************
292 * USER_IsExitingThread
293 */
294 BOOL USER_IsExitingThread( DWORD tid )
295 {
296 return (tid == exiting_thread_id);
297 }
298
299
300 /**********************************************************************
301 * thread_detach
302 */
303 static void thread_detach(void)
304 {
305 struct user_thread_info *thread_info = get_user_thread_info();
306
307 exiting_thread_id = GetCurrentThreadId();
308
309 WDML_NotifyThreadDetach();
310
311 if (thread_info->top_window) WIN_DestroyThreadWindows( thread_info->top_window );
312 if (thread_info->msg_window) WIN_DestroyThreadWindows( thread_info->msg_window );
313 CloseHandle( thread_info->server_queue );
314 HeapFree( GetProcessHeap(), 0, thread_info->wmchar_data );
315
316 exiting_thread_id = 0;
317 }
318
319
320 /***********************************************************************
321 * UserClientDllInitialize (USER32.@)
322 *
323 * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
324 */
325 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
326 {
327 BOOL ret = TRUE;
328 switch(reason)
329 {
330 case DLL_PROCESS_ATTACH:
331 user32_module = inst;
332 ret = process_attach();
333 break;
334 case DLL_THREAD_DETACH:
335 thread_detach();
336 break;
337 case DLL_PROCESS_DETACH:
338 USER_unload_driver();
339 break;
340 }
341 return ret;
342 }
343
344
345 /***********************************************************************
346 * ExitWindowsEx (USER32.@)
347 */
348 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reason )
349 {
350 static const WCHAR winebootW[] = { '\\','w','i','n','e','b','o','o','t','.','e','x','e',0 };
351 static const WCHAR killW[] = { ' ','-','-','k','i','l','l',0 };
352 static const WCHAR end_sessionW[] = { ' ','-','-','e','n','d','-','s','e','s','s','i','o','n',0 };
353 static const WCHAR forceW[] = { ' ','-','-','f','o','r','c','e',0 };
354 static const WCHAR shutdownW[] = { ' ','-','-','s','h','u','t','d','o','w','n',0 };
355
356 WCHAR app[MAX_PATH];
357 WCHAR cmdline[MAX_PATH + 64];
358 PROCESS_INFORMATION pi;
359 STARTUPINFOW si;
360 void *redir;
361
362 GetSystemDirectoryW( app, MAX_PATH - sizeof(winebootW)/sizeof(WCHAR) );
363 strcatW( app, winebootW );
364 strcpyW( cmdline, app );
365
366 if (flags & EWX_FORCE) lstrcatW( cmdline, killW );
367 else
368 {
369 lstrcatW( cmdline, end_sessionW );
370 if (flags & EWX_FORCEIFHUNG) lstrcatW( cmdline, forceW );
371 }
372 if (!(flags & EWX_REBOOT)) lstrcatW( cmdline, shutdownW );
373
374 memset( &si, 0, sizeof si );
375 si.cb = sizeof si;
376 Wow64DisableWow64FsRedirection( &redir );
377 if (!CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi ))
378 {
379 Wow64RevertWow64FsRedirection( redir );
380 ERR( "Failed to run %s\n", debugstr_w(cmdline) );
381 return FALSE;
382 }
383 Wow64RevertWow64FsRedirection( redir );
384 CloseHandle( pi.hProcess );
385 CloseHandle( pi.hThread );
386 return TRUE;
387 }
388
389 /***********************************************************************
390 * LockWorkStation (USER32.@)
391 */
392 BOOL WINAPI LockWorkStation(void)
393 {
394 TRACE(": stub\n");
395 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
396 return FALSE;
397 }
398
399 /***********************************************************************
400 * RegisterServicesProcess (USER32.@)
401 */
402 int WINAPI RegisterServicesProcess(DWORD ServicesProcessId)
403 {
404 FIXME("(0x%x): stub\n", ServicesProcessId);
405 return 0;
406 }
407
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.