user32: Generate WM_APPCOMMAND messages for browser and multimedia keys.
[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 #include "winreg.h"
29
30 #include "controls.h"
31 #include "user_private.h"
32 #include "win.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
37
38 #define DESKTOP_ALL_ACCESS 0x01ff
39
40 WORD USER_HeapSel = 0;  /* USER heap selector */
41 HMODULE user32_module = 0;
42
43 static SYSLEVEL USER_SysLevel;
44 static CRITICAL_SECTION_DEBUG critsect_debug =
45 {
46     0, 0, &USER_SysLevel.crst,
47     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
48       0, 0, { (DWORD_PTR)(__FILE__ ": USER_SysLevel") }
49 };
50 static SYSLEVEL USER_SysLevel = { { &critsect_debug, -1, 0, 0, 0, 0 }, 2 };
51
52 static HPALETTE (WINAPI *pfnGDISelectPalette)( HDC hdc, HPALETTE hpal, WORD bkgnd );
53 static UINT (WINAPI *pfnGDIRealizePalette)( HDC hdc );
54 static HPALETTE hPrimaryPalette;
55
56 static DWORD exiting_thread_id;
57
58 extern void WDML_NotifyThreadDetach(void);
59
60
61 /***********************************************************************
62  *           USER_Lock
63  */
64 void USER_Lock(void)
65 {
66     _EnterSysLevel( &USER_SysLevel );
67 }
68
69
70 /***********************************************************************
71  *           USER_Unlock
72  */
73 void USER_Unlock(void)
74 {
75     _LeaveSysLevel( &USER_SysLevel );
76 }
77
78
79 /***********************************************************************
80  *           USER_CheckNotLock
81  *
82  * Make sure that we don't hold the user lock.
83  */
84 void USER_CheckNotLock(void)
85 {
86     _CheckNotSysLevel( &USER_SysLevel );
87 }
88
89
90 /***********************************************************************
91  *              UserSelectPalette (Not a Windows API)
92  */
93 static HPALETTE WINAPI UserSelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
94 {
95     WORD wBkgPalette = 1;
96
97     if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
98     {
99         HWND hwnd = WindowFromDC( hDC );
100         if (hwnd)
101         {
102             HWND hForeground = GetForegroundWindow();
103             /* set primary palette if it's related to current active */
104             if (hForeground == hwnd || IsChild(hForeground,hwnd))
105             {
106                 wBkgPalette = 0;
107                 hPrimaryPalette = hPal;
108             }
109         }
110     }
111     return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
112 }
113
114
115 /***********************************************************************
116  *              UserRealizePalette (USER32.@)
117  */
118 UINT WINAPI UserRealizePalette( HDC hDC )
119 {
120     UINT realized = pfnGDIRealizePalette( hDC );
121
122     /* do not send anything if no colors were changed */
123     if (realized && GetCurrentObject( hDC, OBJ_PAL ) == hPrimaryPalette)
124     {
125         /* send palette change notification */
126         HWND hWnd = WindowFromDC( hDC );
127         if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
128                                        SMTO_ABORTIFHUNG, 2000, NULL );
129     }
130     return realized;
131 }
132
133
134 /***********************************************************************
135  *           palette_init
136  *
137  * Patch the function pointers in GDI for SelectPalette and RealizePalette
138  */
139 static void palette_init(void)
140 {
141     void **ptr;
142     HMODULE module = GetModuleHandleA( "gdi32" );
143     if (!module)
144     {
145         ERR( "cannot get GDI32 handle\n" );
146         return;
147     }
148     if ((ptr = (void**)GetProcAddress( module, "pfnSelectPalette" )))
149         pfnGDISelectPalette = InterlockedExchangePointer( ptr, UserSelectPalette );
150     else ERR( "cannot find pfnSelectPalette in GDI32\n" );
151     if ((ptr = (void**)GetProcAddress( module, "pfnRealizePalette" )))
152         pfnGDIRealizePalette = InterlockedExchangePointer( ptr, UserRealizePalette );
153     else ERR( "cannot find pfnRealizePalette in GDI32\n" );
154 }
155
156
157 /***********************************************************************
158  *           winstation_init
159  *
160  * Connect to the process window station and desktop.
161  */
162 static void winstation_init(void)
163 {
164     static const WCHAR WinSta0[] = {'W','i','n','S','t','a','0',0};
165     static const WCHAR Default[] = {'D','e','f','a','u','l','t',0};
166
167     STARTUPINFOW info;
168     WCHAR *winstation = NULL, *desktop = NULL, *buffer = NULL;
169     HANDLE handle;
170
171     GetStartupInfoW( &info );
172     if (info.lpDesktop && *info.lpDesktop)
173     {
174         buffer = HeapAlloc( GetProcessHeap(), 0, (strlenW(info.lpDesktop) + 1) * sizeof(WCHAR) );
175         strcpyW( buffer, info.lpDesktop );
176         if ((desktop = strchrW( buffer, '\\' )))
177         {
178             *desktop++ = 0;
179             winstation = buffer;
180         }
181         else desktop = buffer;
182     }
183
184     /* set winstation if explicitly specified, or if we don't have one yet */
185     if (buffer || !GetProcessWindowStation())
186     {
187         handle = CreateWindowStationW( winstation ? winstation : WinSta0, 0, WINSTA_ALL_ACCESS, NULL );
188         if (handle) SetProcessWindowStation( handle );
189     }
190     if (buffer || !GetThreadDesktop( GetCurrentThreadId() ))
191     {
192         handle = CreateDesktopW( desktop ? desktop : Default, NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
193         if (handle) SetThreadDesktop( handle );
194     }
195     HeapFree( GetProcessHeap(), 0, buffer );
196 }
197
198
199 /***********************************************************************
200  *           USER initialisation routine
201  */
202 static BOOL process_attach(void)
203 {
204     HINSTANCE16 instance;
205
206     /* Create USER heap */
207     if ((instance = LoadLibrary16( "USER.EXE" )) >= 32) USER_HeapSel = instance | 7;
208     else
209     {
210         USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 65536 );
211         LocalInit16( USER_HeapSel, 32, 65534 );
212     }
213
214     /* some Win9x dlls expect keyboard to be loaded */
215     if (GetVersion() & 0x80000000) LoadLibrary16( "keyboard.drv" );
216
217     winstation_init();
218
219     /* Initialize system colors and metrics */
220     SYSPARAMS_Init();
221
222     /* Setup palette function pointers */
223     palette_init();
224
225     /* Initialize built-in window classes */
226     CLASS_RegisterBuiltinClasses();
227
228     /* Initialize message spying */
229     if (!SPY_Init()) return FALSE;
230
231     return TRUE;
232 }
233
234
235 /**********************************************************************
236  *           USER_IsExitingThread
237  */
238 BOOL USER_IsExitingThread( DWORD tid )
239 {
240     return (tid == exiting_thread_id);
241 }
242
243
244 /**********************************************************************
245  *           thread_detach
246  */
247 static void thread_detach(void)
248 {
249     exiting_thread_id = GetCurrentThreadId();
250
251     WDML_NotifyThreadDetach();
252
253     WIN_DestroyThreadWindows( get_user_thread_info()->desktop );
254     CloseHandle( get_user_thread_info()->server_queue );
255
256     exiting_thread_id = 0;
257 }
258
259
260 /***********************************************************************
261  *           UserClientDllInitialize  (USER32.@)
262  *
263  * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
264  */
265 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
266 {
267     BOOL ret = TRUE;
268     switch(reason)
269     {
270     case DLL_PROCESS_ATTACH:
271         user32_module = inst;
272         ret = process_attach();
273         break;
274     case DLL_THREAD_DETACH:
275         thread_detach();
276         break;
277     case DLL_PROCESS_DETACH:
278         USER_unload_driver();
279         break;
280     }
281     return ret;
282 }
283
284
285 /***********************************************************************
286  *              ExitWindowsEx (USER32.@)
287  */
288 BOOL WINAPI ExitWindowsEx( UINT flags, DWORD reason )
289 {
290     static const WCHAR winebootW[]    = { '\\','w','i','n','e','b','o','o','t','.','e','x','e',0 };
291     static const WCHAR killW[]        = { ' ','-','-','k','i','l','l',0 };
292     static const WCHAR end_sessionW[] = { ' ','-','-','e','n','d','-','s','e','s','s','i','o','n',0 };
293     static const WCHAR forceW[]       = { ' ','-','-','f','o','r','c','e',0 };
294     static const WCHAR shutdownW[]    = { ' ','-','-','s','h','u','t','d','o','w','n',0 };
295
296     WCHAR cmdline[MAX_PATH + 64];
297     PROCESS_INFORMATION pi;
298     STARTUPINFOW si;
299
300     GetSystemDirectoryW( cmdline, MAX_PATH );
301     lstrcatW( cmdline, winebootW );
302
303     if (flags & EWX_FORCE) lstrcatW( cmdline, killW );
304     else
305     {
306         lstrcatW( cmdline, end_sessionW );
307         if (flags & EWX_FORCEIFHUNG) lstrcatW( cmdline, forceW );
308     }
309     if (!(flags & EWX_REBOOT)) lstrcatW( cmdline, shutdownW );
310
311     memset( &si, 0, sizeof si );
312     si.cb = sizeof si;
313     if (!CreateProcessW( NULL, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi ))
314     {
315         ERR( "Failed to run %s\n", debugstr_w(cmdline) );
316         return FALSE;
317     }
318     CloseHandle( pi.hProcess );
319     CloseHandle( pi.hThread );
320     return TRUE;
321 }