Handle a number as a parameter for custom action 19.
[wine] / dlls / user / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22 #include <string.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "wine/winbase16.h"
29 #include "wine/winuser16.h"
30
31 #include "controls.h"
32 #include "message.h"
33 #include "user_private.h"
34 #include "win.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
38
39 USER_DRIVER USER_Driver;
40
41 WORD USER_HeapSel = 0;  /* USER heap selector */
42 HMODULE user32_module = 0;
43
44 static HPALETTE (WINAPI *pfnGDISelectPalette)( HDC hdc, HPALETTE hpal, WORD bkgnd );
45 static UINT (WINAPI *pfnGDIRealizePalette)( HDC hdc );
46 static HPALETTE hPrimaryPalette;
47
48 static HMODULE graphics_driver;
49 static DWORD exiting_thread_id;
50
51 extern void WDML_NotifyThreadDetach(void);
52
53 #define GET_USER_FUNC(name) USER_Driver.p##name = (void*)GetProcAddress( graphics_driver, #name )
54
55 /* load the graphics driver */
56 static BOOL load_driver(void)
57 {
58     char buffer[MAX_PATH], *name, *next;
59     HKEY hkey;
60
61     strcpy( buffer, "x11drv,ttydrv" );  /* default value */
62     if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine", &hkey ))
63     {
64         DWORD type, count = sizeof(buffer);
65         RegQueryValueExA( hkey, "GraphicsDriver", 0, &type, buffer, &count );
66         RegCloseKey( hkey );
67     }
68
69     name = buffer;
70     while (name)
71     {
72         next = strchr( name, ',' );
73         if (next) *next++ = 0;
74
75         if ((graphics_driver = LoadLibraryA( name )) != 0) break;
76         name = next;
77     }
78     if (!graphics_driver)
79     {
80         MESSAGE( "wine: Could not load graphics driver '%s'.\n", buffer );
81         if (!strcasecmp( buffer, "x11drv" ))
82             MESSAGE( "Make sure that your X server is running and that $DISPLAY is set correctly.\n" );
83         ExitProcess(1);
84     }
85
86     GET_USER_FUNC(InitKeyboard);
87     GET_USER_FUNC(VkKeyScanEx);
88     GET_USER_FUNC(MapVirtualKeyEx);
89     GET_USER_FUNC(GetKeyNameText);
90     GET_USER_FUNC(ToUnicodeEx);
91     GET_USER_FUNC(GetKeyboardLayoutList);
92     GET_USER_FUNC(GetKeyboardLayout);
93     GET_USER_FUNC(GetKeyboardLayoutName);
94     GET_USER_FUNC(LoadKeyboardLayout);
95     GET_USER_FUNC(ActivateKeyboardLayout);
96     GET_USER_FUNC(UnloadKeyboardLayout);
97     GET_USER_FUNC(Beep);
98     GET_USER_FUNC(InitMouse);
99     GET_USER_FUNC(SetCursor);
100     GET_USER_FUNC(GetCursorPos);
101     GET_USER_FUNC(SetCursorPos);
102     GET_USER_FUNC(GetScreenSaveActive);
103     GET_USER_FUNC(SetScreenSaveActive);
104     GET_USER_FUNC(AcquireClipboard);
105     GET_USER_FUNC(EmptyClipboard);
106     GET_USER_FUNC(SetClipboardData);
107     GET_USER_FUNC(GetClipboardData);
108     GET_USER_FUNC(CountClipboardFormats);
109     GET_USER_FUNC(EnumClipboardFormats);
110     GET_USER_FUNC(IsClipboardFormatAvailable);
111     GET_USER_FUNC(RegisterClipboardFormat);
112     GET_USER_FUNC(GetClipboardFormatName);
113     GET_USER_FUNC(EndClipboardUpdate);
114     GET_USER_FUNC(ResetSelectionOwner);
115     GET_USER_FUNC(ChangeDisplaySettingsExW);
116     GET_USER_FUNC(EnumDisplaySettingsExW);
117     GET_USER_FUNC(CreateWindow);
118     GET_USER_FUNC(DestroyWindow);
119     GET_USER_FUNC(GetDC);
120     GET_USER_FUNC(MsgWaitForMultipleObjectsEx);
121     GET_USER_FUNC(ReleaseDC);
122     GET_USER_FUNC(ScrollDC);
123     GET_USER_FUNC(SetFocus);
124     GET_USER_FUNC(SetParent);
125     GET_USER_FUNC(SetWindowPos);
126     GET_USER_FUNC(SetWindowRgn);
127     GET_USER_FUNC(SetWindowIcon);
128     GET_USER_FUNC(SetWindowStyle);
129     GET_USER_FUNC(SetWindowText);
130     GET_USER_FUNC(ShowWindow);
131     GET_USER_FUNC(SysCommandSizeMove);
132
133     return TRUE;
134 }
135
136
137 /***********************************************************************
138  *              UserSelectPalette (Not a Windows API)
139  */
140 static HPALETTE WINAPI UserSelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
141 {
142     WORD wBkgPalette = 1;
143
144     if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
145     {
146         HWND hwnd = WindowFromDC( hDC );
147         if (hwnd)
148         {
149             HWND hForeground = GetForegroundWindow();
150             /* set primary palette if it's related to current active */
151             if (hForeground == hwnd || IsChild(hForeground,hwnd))
152             {
153                 wBkgPalette = 0;
154                 hPrimaryPalette = hPal;
155             }
156         }
157     }
158     return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
159 }
160
161
162 /***********************************************************************
163  *              UserRealizePalette (USER32.@)
164  */
165 UINT WINAPI UserRealizePalette( HDC hDC )
166 {
167     UINT realized = pfnGDIRealizePalette( hDC );
168
169     /* do not send anything if no colors were changed */
170     if (realized && GetCurrentObject( hDC, OBJ_PAL ) == hPrimaryPalette)
171     {
172         /* send palette change notification */
173         HWND hWnd = WindowFromDC( hDC );
174         if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
175                                        SMTO_ABORTIFHUNG, 2000, NULL );
176     }
177     return realized;
178 }
179
180
181 /***********************************************************************
182  *           palette_init
183  *
184  * Patch the function pointers in GDI for SelectPalette and RealizePalette
185  */
186 static void palette_init(void)
187 {
188     void **ptr;
189     HMODULE module = GetModuleHandleA( "gdi32" );
190     if (!module)
191     {
192         ERR( "cannot get GDI32 handle\n" );
193         return;
194     }
195     if ((ptr = (void**)GetProcAddress( module, "pfnSelectPalette" )))
196         pfnGDISelectPalette = InterlockedExchangePointer( ptr, UserSelectPalette );
197     else ERR( "cannot find pfnSelectPalette in GDI32\n" );
198     if ((ptr = (void**)GetProcAddress( module, "pfnRealizePalette" )))
199         pfnGDIRealizePalette = InterlockedExchangePointer( ptr, UserRealizePalette );
200     else ERR( "cannot find pfnRealizePalette in GDI32\n" );
201 }
202
203
204 /***********************************************************************
205  *           USER initialisation routine
206  */
207 static BOOL process_attach(void)
208 {
209     HINSTANCE16 instance;
210
211     /* Create USER heap */
212     if ((instance = LoadLibrary16( "USER.EXE" )) >= 32) USER_HeapSel = instance | 7;
213     else
214     {
215         USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 65536 );
216         LocalInit16( USER_HeapSel, 32, 65534 );
217     }
218
219     /* some Win9x dlls expect keyboard to be loaded */
220     if (GetVersion() & 0x80000000) LoadLibrary16( "keyboard.drv" );
221
222     /* Load the graphics driver */
223     if (!load_driver()) return FALSE;
224
225     /* Initialize system colors and metrics */
226     SYSPARAMS_Init();
227
228     /* Setup palette function pointers */
229     palette_init();
230
231     /* Initialize built-in window classes */
232     CLASS_RegisterBuiltinClasses();
233
234     /* Initialize menus */
235     if (!MENU_Init()) return FALSE;
236
237     /* Initialize message spying */
238     if (!SPY_Init()) return FALSE;
239
240     /* Create message queue of initial thread */
241     InitThreadInput16( 0, 0 );
242
243     /* Create desktop window */
244     if (!WIN_CreateDesktopWindow()) return FALSE;
245
246     /* Initialize keyboard driver */
247     if (USER_Driver.pInitKeyboard) USER_Driver.pInitKeyboard( InputKeyStateTable );
248
249     /* Initialize mouse driver */
250     if (USER_Driver.pInitMouse) USER_Driver.pInitMouse( InputKeyStateTable );
251
252     return TRUE;
253 }
254
255
256 /**********************************************************************
257  *           USER_IsExitingThread
258  */
259 BOOL USER_IsExitingThread( DWORD tid )
260 {
261     return (tid == exiting_thread_id);
262 }
263
264
265 /**********************************************************************
266  *           thread_detach
267  */
268 static void thread_detach(void)
269 {
270     exiting_thread_id = GetCurrentThreadId();
271
272     WDML_NotifyThreadDetach();
273
274     WIN_DestroyThreadWindows( GetDesktopWindow() );
275     QUEUE_DeleteMsgQueue();
276
277     exiting_thread_id = 0;
278 }
279
280
281 /**********************************************************************
282  *           process_detach
283  */
284 static void process_detach(void)
285 {
286     memset(&USER_Driver, 0, sizeof(USER_Driver));
287     FreeLibrary(graphics_driver);
288 }
289
290 /***********************************************************************
291  *           UserClientDllInitialize  (USER32.@)
292  *
293  * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
294  */
295 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
296 {
297     BOOL ret = TRUE;
298     switch(reason)
299     {
300     case DLL_PROCESS_ATTACH:
301         user32_module = inst;
302         ret = process_attach();
303         break;
304     case DLL_PROCESS_DETACH:
305         process_detach();
306         break;
307     case DLL_THREAD_DETACH:
308         thread_detach();
309         break;
310     }
311     return ret;
312 }