ntdll: Pass the NtQueryDirectoryFile info class down into the various helper functions.
[wine] / dlls / user32 / user_main.c
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 SYSLEVEL USER_SysLevel;
42 static CRITICAL_SECTION_DEBUG critsect_debug =
43 {
44     0, 0, &USER_SysLevel.crst,
45     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
46       0, 0, { (DWORD_PTR)(__FILE__ ": USER_SysLevel") }
47 };
48 static SYSLEVEL USER_SysLevel = { { &critsect_debug, -1, 0, 0, 0, 0 }, 2 };
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     _EnterSysLevel( &USER_SysLevel );
65 }
66
67
68 /***********************************************************************
69  *           USER_Unlock
70  */
71 void USER_Unlock(void)
72 {
73     _LeaveSysLevel( &USER_SysLevel );
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     _CheckNotSysLevel( &USER_SysLevel );
85 }
86
87
88 /***********************************************************************
89  *              UserSelectPalette (Not a Windows API)
90  */
91 static HPALETTE WINAPI UserSelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
92 {
93     WORD wBkgPalette = 1;
94
95     if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
96     {
97         HWND hwnd = WindowFromDC( hDC );
98         if (hwnd)
99         {
100             HWND hForeground = GetForegroundWindow();
101             /* set primary palette if it's related to current active */
102             if (hForeground == hwnd || IsChild(hForeground,hwnd))
103             {
104                 wBkgPalette = 0;
105                 hPrimaryPalette = hPal;
106             }
107         }
108     }
109     return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
110 }
111
112
113 /***********************************************************************
114  *              UserRealizePalette (USER32.@)
115  */
116 UINT WINAPI UserRealizePalette( HDC hDC )
117 {
118     UINT realized = pfnGDIRealizePalette( hDC );
119
120     /* do not send anything if no colors were changed */
121     if (realized && GetCurrentObject( hDC, OBJ_PAL ) == hPrimaryPalette)
122     {
123         /* send palette change notification */
124         HWND hWnd = WindowFromDC( hDC );
125         if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
126                                        SMTO_ABORTIFHUNG, 2000, NULL );
127     }
128     return realized;
129 }
130
131
132 /***********************************************************************
133  *           palette_init
134  *
135  * Patch the function pointers in GDI for SelectPalette and RealizePalette
136  */
137 static void palette_init(void)
138 {
139     void **ptr;
140     HMODULE module = GetModuleHandleA( "gdi32" );
141     if (!module)
142     {
143         ERR( "cannot get GDI32 handle\n" );
144         return;
145     }
146     if ((ptr = (void**)GetProcAddress( module, "pfnSelectPalette" )))
147         pfnGDISelectPalette = InterlockedExchangePointer( ptr, UserSelectPalette );
148     else ERR( "cannot find pfnSelectPalette in GDI32\n" );
149     if ((ptr = (void**)GetProcAddress( module, "pfnRealizePalette" )))
150         pfnGDIRealizePalette = InterlockedExchangePointer( ptr, UserRealizePalette );
151     else ERR( "cannot find pfnRealizePalette in GDI32\n" );
152 }
153
154
155 /***********************************************************************
156  *           get_default_desktop
157  *
158  * Get the name of the desktop to use for this app if not specified explicitly.
159  */
160 static const WCHAR *get_default_desktop(void)
161 {
162     static const WCHAR defaultW[] = {'D','e','f','a','u','l','t',0};
163     static const WCHAR desktopW[] = {'D','e','s','k','t','o','p',0};
164     static const WCHAR explorerW[] = {'\\','E','x','p','l','o','r','e','r',0};
165     static const WCHAR app_defaultsW[] = {'S','o','f','t','w','a','r','e','\\',
166                                           'W','i','n','e','\\',
167                                           'A','p','p','D','e','f','a','u','l','t','s',0};
168     static WCHAR buffer[MAX_PATH + sizeof(explorerW)/sizeof(WCHAR)];
169     WCHAR *p, *appname = buffer;
170     const WCHAR *ret = defaultW;
171     DWORD len;
172     HKEY tmpkey, appkey;
173
174     len = (GetModuleFileNameW( 0, buffer, MAX_PATH ));
175     if (!len || len >= MAX_PATH) return ret;
176     if ((p = strrchrW( appname, '/' ))) appname = p + 1;
177     if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
178     p = appname + strlenW(appname);
179     strcpyW( p, explorerW );
180
181     /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Explorer */
182     if (!RegOpenKeyW( HKEY_CURRENT_USER, app_defaultsW, &tmpkey ))
183     {
184         if (RegOpenKeyW( tmpkey, appname, &appkey )) appkey = 0;
185         RegCloseKey( tmpkey );
186         if (appkey)
187         {
188             len = sizeof(buffer);
189             if (!RegQueryValueExW( appkey, desktopW, 0, NULL, (LPBYTE)buffer, &len )) ret = buffer;
190             RegCloseKey( appkey );
191             if (ret && strcmpiW( ret, defaultW )) return ret;
192             ret = defaultW;
193         }
194     }
195
196     memcpy( buffer, app_defaultsW, 13 * sizeof(WCHAR) );  /* copy only software\\wine */
197     strcpyW( buffer + 13, explorerW );
198
199     /* @@ Wine registry key: HKCU\Software\Wine\Explorer */
200     if (!RegOpenKeyW( HKEY_CURRENT_USER, buffer, &appkey ))
201     {
202         len = sizeof(buffer);
203         if (!RegQueryValueExW( appkey, desktopW, 0, NULL, (LPBYTE)buffer, &len )) ret = buffer;
204         RegCloseKey( appkey );
205     }
206     return ret;
207 }
208
209
210 /***********************************************************************
211  *           winstation_init
212  *
213  * Connect to the process window station and desktop.
214  */
215 static void winstation_init(void)
216 {
217     static const WCHAR WinSta0[] = {'W','i','n','S','t','a','0',0};
218
219     STARTUPINFOW info;
220     WCHAR *winstation = NULL, *desktop = NULL, *buffer = NULL;
221     HANDLE handle;
222
223     GetStartupInfoW( &info );
224     if (info.lpDesktop && *info.lpDesktop)
225     {
226         buffer = HeapAlloc( GetProcessHeap(), 0, (strlenW(info.lpDesktop) + 1) * sizeof(WCHAR) );
227         strcpyW( buffer, info.lpDesktop );
228         if ((desktop = strchrW( buffer, '\\' )))
229         {
230             *desktop++ = 0;
231             winstation = buffer;
232         }
233         else desktop = buffer;
234     }
235
236     /* set winstation if explicitly specified, or if we don't have one yet */
237     if (buffer || !GetProcessWindowStation())
238     {
239         handle = CreateWindowStationW( winstation ? winstation : WinSta0, 0, WINSTA_ALL_ACCESS, NULL );
240         if (handle)
241         {
242             SetProcessWindowStation( handle );
243             /* only WinSta0 is visible */
244             if (!winstation || !strcmpiW( winstation, WinSta0 ))
245             {
246                 USEROBJECTFLAGS flags;
247                 flags.fInherit  = FALSE;
248                 flags.fReserved = FALSE;
249                 flags.dwFlags   = WSF_VISIBLE;
250                 SetUserObjectInformationW( handle, UOI_FLAGS, &flags, sizeof(flags) );
251             }
252         }
253     }
254     if (buffer || !GetThreadDesktop( GetCurrentThreadId() ))
255     {
256         handle = CreateDesktopW( desktop ? desktop : get_default_desktop(),
257                                  NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
258         if (handle) SetThreadDesktop( handle );
259     }
260     HeapFree( GetProcessHeap(), 0, buffer );
261 }
262
263
264 /***********************************************************************
265  *           USER initialisation routine
266  */
267 static BOOL process_attach(void)
268 {
269     LoadLibrary16( "user.exe" );
270
271     /* some Win9x dlls expect keyboard to be loaded */
272     if (GetVersion() & 0x80000000) LoadLibrary16( "keyboard.drv" );
273
274     winstation_init();
275
276     /* Initialize system colors and metrics */
277     SYSPARAMS_Init();
278
279     /* Setup palette function pointers */
280     palette_init();
281
282     /* Initialize built-in window classes */
283     CLASS_RegisterBuiltinClasses();
284
285     /* Initialize message spying */
286     if (!SPY_Init()) return FALSE;
287
288     return TRUE;
289 }
290
291
292 /**********************************************************************
293  *           USER_IsExitingThread
294  */
295 BOOL USER_IsExitingThread( DWORD tid )
296 {
297     return (tid == exiting_thread_id);
298 }
299
300
301 /**********************************************************************
302  *           thread_detach
303  */
304 static void thread_detach(void)
305 {
306     struct user_thread_info *thread_info = get_user_thread_info();
307
308     exiting_thread_id = GetCurrentThreadId();
309
310     WDML_NotifyThreadDetach();
311
312     if (thread_info->top_window) WIN_DestroyThreadWindows( thread_info->top_window );
313     if (thread_info->msg_window) WIN_DestroyThreadWindows( thread_info->msg_window );
314     CloseHandle( thread_info->server_queue );
315     HeapFree( GetProcessHeap(), 0, thread_info->wmchar_data );
316
317     exiting_thread_id = 0;
318 }
319
320
321 /***********************************************************************
322  *           UserClientDllInitialize  (USER32.@)
323  *
324  * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
325  */
326 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
327 {
328     BOOL ret = TRUE;
329     switch(reason)
330     {
331     case DLL_PROCESS_ATTACH:
332         user32_module = inst;
333         ret = process_attach();
334         break;
335     case DLL_THREAD_DETACH:
336         thread_detach();
337         break;
338     case DLL_PROCESS_DETACH:
339         USER_unload_driver();
340         break;
341     }
342     return ret;
343 }
344
345
346 /***********************************************************************
347  *              ExitWindowsEx (USER32.@)
348  */
349 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reason )
350 {
351     static const WCHAR winebootW[]    = { '\\','w','i','n','e','b','o','o','t','.','e','x','e',0 };
352     static const WCHAR killW[]        = { ' ','-','-','k','i','l','l',0 };
353     static const WCHAR end_sessionW[] = { ' ','-','-','e','n','d','-','s','e','s','s','i','o','n',0 };
354     static const WCHAR forceW[]       = { ' ','-','-','f','o','r','c','e',0 };
355     static const WCHAR shutdownW[]    = { ' ','-','-','s','h','u','t','d','o','w','n',0 };
356
357     WCHAR app[MAX_PATH];
358     WCHAR cmdline[MAX_PATH + 64];
359     PROCESS_INFORMATION pi;
360     STARTUPINFOW si;
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     if (!CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi ))
377     {
378         ERR( "Failed to run %s\n", debugstr_w(cmdline) );
379         return FALSE;
380     }
381     CloseHandle( pi.hProcess );
382     CloseHandle( pi.hThread );
383     return TRUE;
384 }
385
386 /***********************************************************************
387  *              LockWorkStation (USER32.@)
388  */
389 BOOL WINAPI LockWorkStation(void)
390 {
391     TRACE(": stub\n");
392     SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
393     return FALSE;
394 }
395
396 /***********************************************************************
397  *              RegisterServicesProcess (USER32.@)
398  */
399 int WINAPI RegisterServicesProcess(DWORD ServicesProcessId)
400 {
401     FIXME("(0x%x): stub\n", ServicesProcessId);
402     return 0;
403 }