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