If using the default values, also set dwType to REG_SZ as our default
[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 "cursoricon.h"
33 #include "global.h"
34 #include "message.h"
35 #include "user.h"
36 #include "win.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
40
41 USER_DRIVER USER_Driver;
42
43 WINE_LOOK TWEAK_WineLook = WIN31_LOOK;
44
45 WORD USER_HeapSel = 0;  /* USER heap selector */
46
47 extern HPALETTE (WINAPI *pfnGDISelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd );
48 extern UINT (WINAPI *pfnGDIRealizePalette)(HDC hdc);
49
50 static HMODULE graphics_driver;
51 static DWORD exiting_thread_id;
52
53 extern void WDML_NotifyThreadDetach(void);
54
55 #define GET_USER_FUNC(name) USER_Driver.p##name = (void*)GetProcAddress( graphics_driver, #name )
56
57 /* load the graphics driver */
58 static BOOL load_driver(void)
59 {
60     char buffer[MAX_PATH];
61     HKEY hkey;
62     DWORD type, count;
63
64     strcpy( buffer, "x11drv" );  /* default value */
65     if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine", &hkey ))
66     {
67         count = sizeof(buffer);
68         RegQueryValueExA( hkey, "GraphicsDriver", 0, &type, buffer, &count );
69         RegCloseKey( hkey );
70     }
71
72     if (!(graphics_driver = LoadLibraryA( buffer )))
73     {
74         MESSAGE( "Could not load graphics driver '%s'\n", buffer );
75         return FALSE;
76     }
77
78     GET_USER_FUNC(InitKeyboard);
79     GET_USER_FUNC(VkKeyScanEx);
80     GET_USER_FUNC(MapVirtualKeyEx);
81     GET_USER_FUNC(GetKeyNameText);
82     GET_USER_FUNC(ToUnicodeEx);
83     GET_USER_FUNC(GetKeyboardLayoutList);
84     GET_USER_FUNC(GetKeyboardLayout);
85     GET_USER_FUNC(GetKeyboardLayoutName);
86     GET_USER_FUNC(LoadKeyboardLayout);
87     GET_USER_FUNC(ActivateKeyboardLayout);
88     GET_USER_FUNC(UnloadKeyboardLayout);
89     GET_USER_FUNC(Beep);
90     GET_USER_FUNC(InitMouse);
91     GET_USER_FUNC(SetCursor);
92     GET_USER_FUNC(GetCursorPos);
93     GET_USER_FUNC(SetCursorPos);
94     GET_USER_FUNC(GetScreenSaveActive);
95     GET_USER_FUNC(SetScreenSaveActive);
96     GET_USER_FUNC(AcquireClipboard);
97     GET_USER_FUNC(EmptyClipboard);
98     GET_USER_FUNC(SetClipboardData);
99     GET_USER_FUNC(GetClipboardData);
100     GET_USER_FUNC(CountClipboardFormats);
101     GET_USER_FUNC(EnumClipboardFormats);
102     GET_USER_FUNC(IsClipboardFormatAvailable);
103     GET_USER_FUNC(RegisterClipboardFormat);
104     GET_USER_FUNC(GetClipboardFormatName);
105     GET_USER_FUNC(EndClipboardUpdate);
106     GET_USER_FUNC(ResetSelectionOwner);
107     GET_USER_FUNC(ChangeDisplaySettingsExW);
108     GET_USER_FUNC(EnumDisplaySettingsExW);
109     GET_USER_FUNC(CreateWindow);
110     GET_USER_FUNC(DestroyWindow);
111     GET_USER_FUNC(GetDC);
112     GET_USER_FUNC(ForceWindowRaise);
113     GET_USER_FUNC(MsgWaitForMultipleObjectsEx);
114     GET_USER_FUNC(ReleaseDC);
115     GET_USER_FUNC(ScrollDC);
116     GET_USER_FUNC(ScrollWindowEx);
117     GET_USER_FUNC(SetFocus);
118     GET_USER_FUNC(SetParent);
119     GET_USER_FUNC(SetWindowPos);
120     GET_USER_FUNC(SetWindowRgn);
121     GET_USER_FUNC(SetWindowIcon);
122     GET_USER_FUNC(SetWindowStyle);
123     GET_USER_FUNC(SetWindowText);
124     GET_USER_FUNC(ShowWindow);
125     GET_USER_FUNC(SysCommandSizeMove);
126
127     return TRUE;
128 }
129
130
131 /***********************************************************************
132  *           controls_init
133  *
134  * Register the classes for the builtin controls
135  */
136 static void controls_init(void)
137 {
138     extern const struct builtin_class_descr BUTTON_builtin_class;
139     extern const struct builtin_class_descr COMBO_builtin_class;
140     extern const struct builtin_class_descr COMBOLBOX_builtin_class;
141     extern const struct builtin_class_descr DIALOG_builtin_class;
142     extern const struct builtin_class_descr DESKTOP_builtin_class;
143     extern const struct builtin_class_descr EDIT_builtin_class;
144     extern const struct builtin_class_descr ICONTITLE_builtin_class;
145     extern const struct builtin_class_descr LISTBOX_builtin_class;
146     extern const struct builtin_class_descr MDICLIENT_builtin_class;
147     extern const struct builtin_class_descr MENU_builtin_class;
148     extern const struct builtin_class_descr SCROLL_builtin_class;
149     extern const struct builtin_class_descr STATIC_builtin_class;
150
151     CLASS_RegisterBuiltinClass( &BUTTON_builtin_class );
152     CLASS_RegisterBuiltinClass( &COMBO_builtin_class );
153     CLASS_RegisterBuiltinClass( &COMBOLBOX_builtin_class );
154     CLASS_RegisterBuiltinClass( &DIALOG_builtin_class );
155     CLASS_RegisterBuiltinClass( &DESKTOP_builtin_class );
156     CLASS_RegisterBuiltinClass( &EDIT_builtin_class );
157     CLASS_RegisterBuiltinClass( &ICONTITLE_builtin_class );
158     CLASS_RegisterBuiltinClass( &LISTBOX_builtin_class );
159     CLASS_RegisterBuiltinClass( &MDICLIENT_builtin_class );
160     CLASS_RegisterBuiltinClass( &MENU_builtin_class );
161     CLASS_RegisterBuiltinClass( &SCROLL_builtin_class );
162     CLASS_RegisterBuiltinClass( &STATIC_builtin_class );
163 }
164
165
166 /***********************************************************************
167  *           palette_init
168  *
169  * Patch the function pointers in GDI for SelectPalette and RealizePalette
170  */
171 static void palette_init(void)
172 {
173     void **ptr;
174     HMODULE module = GetModuleHandleA( "gdi32" );
175     if (!module)
176     {
177         ERR( "cannot get GDI32 handle\n" );
178         return;
179     }
180     if ((ptr = (void**)GetProcAddress( module, "pfnSelectPalette" )))
181         pfnGDISelectPalette = InterlockedExchangePointer( ptr, SelectPalette );
182     else ERR( "cannot find pfnSelectPalette in GDI32\n" );
183     if ((ptr = (void**)GetProcAddress( module, "pfnRealizePalette" )))
184         pfnGDIRealizePalette = InterlockedExchangePointer( ptr, UserRealizePalette );
185     else ERR( "cannot find pfnRealizePalette in GDI32\n" );
186 }
187
188
189 /***********************************************************************
190  *           tweak_init
191  */
192 static void tweak_init(void)
193 {
194     static const char *OS = "Win3.1";
195     char buffer[80];
196     HKEY hkey;
197     DWORD type, count = sizeof(buffer);
198
199     if (RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Tweak.Layout", &hkey ))
200         return;
201     if (RegQueryValueExA( hkey, "WineLook", 0, &type, buffer, &count ))
202         strcpy( buffer, "Win31" );  /* default value */
203     RegCloseKey( hkey );
204
205     /* WIN31_LOOK is default */
206     if (!strncasecmp( buffer, "Win95", 5 ))
207     {
208         TWEAK_WineLook = WIN95_LOOK;
209         OS = "Win95";
210     }
211     else if (!strncasecmp( buffer, "Win98", 5 ))
212     {
213         TWEAK_WineLook = WIN98_LOOK;
214         OS = "Win98";
215     }
216     TRACE("Using %s look and feel.\n", OS);
217 }
218
219
220 /***********************************************************************
221  *           USER initialisation routine
222  */
223 static BOOL process_attach(void)
224 {
225     HINSTANCE16 instance;
226
227     /* Create USER heap */
228     if ((instance = LoadLibrary16( "USER.EXE" )) >= 32) USER_HeapSel = instance | 7;
229     else
230     {
231         USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 65536 );
232         LocalInit16( USER_HeapSel, 32, 65534 );
233     }
234
235     /* Load the graphics driver */
236     tweak_init();
237     if (!load_driver()) return FALSE;
238
239     /* Initialize system colors and metrics */
240     SYSMETRICS_Init();
241     SYSCOLOR_Init();
242
243     /* Setup palette function pointers */
244     palette_init();
245
246     /* Initialize built-in window classes */
247     controls_init();
248
249     /* Initialize menus */
250     if (!MENU_Init()) return FALSE;
251
252     /* Initialize message spying */
253     if (!SPY_Init()) return FALSE;
254
255     /* Create message queue of initial thread */
256     InitThreadInput16( 0, 0 );
257
258     /* Create desktop window */
259     if (!WIN_CreateDesktopWindow()) return FALSE;
260
261     /* Initialize keyboard driver */
262     if (USER_Driver.pInitKeyboard) USER_Driver.pInitKeyboard( InputKeyStateTable );
263
264     /* Initialize mouse driver */
265     if (USER_Driver.pInitMouse) USER_Driver.pInitMouse( InputKeyStateTable );
266
267     return TRUE;
268 }
269
270
271 /**********************************************************************
272  *           USER_IsExitingThread
273  */
274 BOOL USER_IsExitingThread( DWORD tid )
275 {
276     return (tid == exiting_thread_id);
277 }
278
279
280 /**********************************************************************
281  *           thread_detach
282  */
283 static void thread_detach(void)
284 {
285     exiting_thread_id = GetCurrentThreadId();
286
287     WDML_NotifyThreadDetach();
288
289     TIMER_RemoveThreadTimers();
290     WIN_DestroyThreadWindows( GetDesktopWindow() );
291     QUEUE_DeleteMsgQueue();
292
293     exiting_thread_id = 0;
294 }
295
296
297 /***********************************************************************
298  *           UserClientDllInitialize  (USER32.@)
299  *
300  * USER dll initialisation routine (exported as UserClientDllInitialize for compatibility).
301  */
302 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
303 {
304     BOOL ret = TRUE;
305     switch(reason)
306     {
307     case DLL_PROCESS_ATTACH:
308         ret = process_attach();
309         break;
310     case DLL_THREAD_DETACH:
311         thread_detach();
312         break;
313     }
314     return ret;
315 }