Added regedit unit test, a couple minor changes to regedit.
[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 <string.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wingdi.h"
25 #include "winuser.h"
26 #include "winreg.h"
27 #include "wine/winbase16.h"
28 #include "wine/winuser16.h"
29
30 #include "controls.h"
31 #include "cursoricon.h"
32 #include "global.h"
33 #include "input.h"
34 #include "hook.h"
35 #include "message.h"
36 #include "queue.h"
37 #include "spy.h"
38 #include "sysmetrics.h"
39 #include "user.h"
40 #include "win.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
44
45 USER_DRIVER USER_Driver;
46
47 WINE_LOOK TWEAK_WineLook = WIN31_LOOK;
48
49 WORD USER_HeapSel = 0;  /* USER heap selector */
50
51 static HMODULE graphics_driver;
52 static DWORD exiting_thread_id;
53
54 extern void COMM_Init(void);
55 extern void WDML_NotifyThreadDetach(void);
56
57 #define GET_USER_FUNC(name) USER_Driver.p##name = (void*)GetProcAddress( graphics_driver, #name )
58
59 /* load the graphics driver */
60 static BOOL load_driver(void)
61 {
62     char buffer[MAX_PATH];
63     HKEY hkey;
64     DWORD type, count;
65
66     strcpy( buffer, "x11drv" );  /* default value */
67     if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine", &hkey ))
68     {
69         count = sizeof(buffer);
70         RegQueryValueExA( hkey, "GraphicsDriver", 0, &type, buffer, &count );
71         RegCloseKey( hkey );
72     }
73
74     if (!(graphics_driver = LoadLibraryA( buffer )))
75     {
76         MESSAGE( "Could not load graphics driver '%s'\n", buffer );
77         return FALSE;
78     }
79
80     GET_USER_FUNC(InitKeyboard);
81     GET_USER_FUNC(VkKeyScan);
82     GET_USER_FUNC(MapVirtualKey);
83     GET_USER_FUNC(GetKeyNameText);
84     GET_USER_FUNC(ToUnicode);
85     GET_USER_FUNC(Beep);
86     GET_USER_FUNC(InitMouse);
87     GET_USER_FUNC(SetCursor);
88     GET_USER_FUNC(GetCursorPos);
89     GET_USER_FUNC(SetCursorPos);
90     GET_USER_FUNC(GetScreenSaveActive);
91     GET_USER_FUNC(SetScreenSaveActive);
92     GET_USER_FUNC(AcquireClipboard);
93     GET_USER_FUNC(ReleaseClipboard);
94     GET_USER_FUNC(SetClipboardData);
95     GET_USER_FUNC(GetClipboardData);
96     GET_USER_FUNC(IsClipboardFormatAvailable);
97     GET_USER_FUNC(RegisterClipboardFormat);
98     GET_USER_FUNC(IsSelectionOwner);
99     GET_USER_FUNC(ResetSelectionOwner);
100     GET_USER_FUNC(CreateWindow);
101     GET_USER_FUNC(DestroyWindow);
102     GET_USER_FUNC(GetDC);
103     GET_USER_FUNC(ForceWindowRaise);
104     GET_USER_FUNC(MsgWaitForMultipleObjectsEx);
105     GET_USER_FUNC(ScrollDC);
106     GET_USER_FUNC(ScrollWindowEx);
107     GET_USER_FUNC(SetFocus);
108     GET_USER_FUNC(SetParent);
109     GET_USER_FUNC(SetWindowPos);
110     GET_USER_FUNC(SetWindowRgn);
111     GET_USER_FUNC(SetWindowIcon);
112     GET_USER_FUNC(SetWindowStyle);
113     GET_USER_FUNC(SetWindowText);
114     GET_USER_FUNC(ShowWindow);
115     GET_USER_FUNC(SysCommandSizeMove);
116
117     return TRUE;
118 }
119
120
121 /***********************************************************************
122  *           controls_init
123  *
124  * Register the classes for the builtin controls
125  */
126 static void controls_init(void)
127 {
128     extern const struct builtin_class_descr BUTTON_builtin_class;
129     extern const struct builtin_class_descr COMBO_builtin_class;
130     extern const struct builtin_class_descr COMBOLBOX_builtin_class;
131     extern const struct builtin_class_descr DIALOG_builtin_class;
132     extern const struct builtin_class_descr DESKTOP_builtin_class;
133     extern const struct builtin_class_descr EDIT_builtin_class;
134     extern const struct builtin_class_descr ICONTITLE_builtin_class;
135     extern const struct builtin_class_descr LISTBOX_builtin_class;
136     extern const struct builtin_class_descr MDICLIENT_builtin_class;
137     extern const struct builtin_class_descr MENU_builtin_class;
138     extern const struct builtin_class_descr SCROLL_builtin_class;
139     extern const struct builtin_class_descr STATIC_builtin_class;
140
141     CLASS_RegisterBuiltinClass( &BUTTON_builtin_class );
142     CLASS_RegisterBuiltinClass( &COMBO_builtin_class );
143     CLASS_RegisterBuiltinClass( &COMBOLBOX_builtin_class );
144     CLASS_RegisterBuiltinClass( &DIALOG_builtin_class );
145     CLASS_RegisterBuiltinClass( &DESKTOP_builtin_class );
146     CLASS_RegisterBuiltinClass( &EDIT_builtin_class );
147     CLASS_RegisterBuiltinClass( &ICONTITLE_builtin_class );
148     CLASS_RegisterBuiltinClass( &LISTBOX_builtin_class );
149     CLASS_RegisterBuiltinClass( &MDICLIENT_builtin_class );
150     CLASS_RegisterBuiltinClass( &MENU_builtin_class );
151     CLASS_RegisterBuiltinClass( &SCROLL_builtin_class );
152     CLASS_RegisterBuiltinClass( &STATIC_builtin_class );
153 }
154
155
156 /***********************************************************************
157  *           palette_init
158  *
159  * Patch the function pointers in GDI for SelectPalette and RealizePalette
160  */
161 static void palette_init(void)
162 {
163     void **ptr;
164     HMODULE module = GetModuleHandleA( "gdi32" );
165     if (!module)
166     {
167         ERR( "cannot get GDI32 handle\n" );
168         return;
169     }
170     if ((ptr = (void**)GetProcAddress( module, "pfnSelectPalette" ))) *ptr = SelectPalette16;
171     else ERR( "cannot find pfnSelectPalette in GDI32\n" );
172     if ((ptr = (void**)GetProcAddress( module, "pfnRealizePalette" ))) *ptr = UserRealizePalette;
173     else ERR( "cannot find pfnRealizePalette in GDI32\n" );
174 }
175
176
177 /***********************************************************************
178  *           tweak_init
179  */
180 static void tweak_init(void)
181 {
182     static const char *OS = "Win3.1";
183     char buffer[80];
184     HKEY hkey;
185     DWORD type, count = sizeof(buffer);
186
187     if (RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Tweak.Layout", &hkey ))
188         return;
189     if (RegQueryValueExA( hkey, "WineLook", 0, &type, buffer, &count ))
190         strcpy( buffer, "Win31" );  /* default value */
191     RegCloseKey( hkey );
192
193     /* WIN31_LOOK is default */
194     if (!strncasecmp( buffer, "Win95", 5 ))
195     {
196         TWEAK_WineLook = WIN95_LOOK;
197         OS = "Win95";
198     }
199     else if (!strncasecmp( buffer, "Win98", 5 ))
200     {
201         TWEAK_WineLook = WIN98_LOOK;
202         OS = "Win98";
203     }
204     TRACE("Using %s look and feel.\n", OS);
205 }
206
207
208 /***********************************************************************
209  *           USER initialisation routine
210  */
211 static BOOL process_attach(void)
212 {
213     HINSTANCE16 instance;
214
215     /* Create USER heap */
216     if ((instance = LoadLibrary16( "USER.EXE" )) < 32) return FALSE;
217     USER_HeapSel = instance | 7;
218
219      /* Global atom table initialisation */
220     if (!ATOM_Init( USER_HeapSel )) return FALSE;
221
222     /* Load the graphics driver */
223     tweak_init();
224     if (!load_driver()) return FALSE;
225
226     /* Initialize system colors and metrics */
227     SYSMETRICS_Init();
228     SYSCOLOR_Init();
229
230     /* Setup palette function pointers */
231     palette_init();
232
233     /* Initialize window procedures */
234     if (!WINPROC_Init()) return FALSE;
235
236     /* Initialize built-in window classes */
237     controls_init();
238
239     /* Initialize dialog manager */
240     if (!DIALOG_Init()) return FALSE;
241
242     /* Initialize menus */
243     if (!MENU_Init()) return FALSE;
244
245     /* Initialize message spying */
246     if (!SPY_Init()) return FALSE;
247
248     /* Create message queue of initial thread */
249     InitThreadInput16( 0, 0 );
250
251     /* Create desktop window */
252     if (!WIN_CreateDesktopWindow()) return FALSE;
253
254     /* Initialize keyboard driver */
255     if (USER_Driver.pInitKeyboard) USER_Driver.pInitKeyboard( InputKeyStateTable );
256
257     /* Initialize mouse driver */
258     if (USER_Driver.pInitMouse) USER_Driver.pInitMouse( InputKeyStateTable );
259
260     /* Initialize 16-bit serial communications */
261     COMM_Init();
262
263     return TRUE;
264 }
265
266
267 /**********************************************************************
268  *           USER_IsExitingThread
269  */
270 BOOL USER_IsExitingThread( DWORD tid )
271 {
272     return (tid == exiting_thread_id);
273 }
274
275
276 /**********************************************************************
277  *           thread_detach
278  */
279 static void thread_detach(void)
280 {
281     HQUEUE16 hQueue = GetThreadQueue16( 0 );
282
283     exiting_thread_id = GetCurrentThreadId();
284
285     WDML_NotifyThreadDetach();
286
287     if (hQueue)
288     {
289         TIMER_RemoveQueueTimers( hQueue );
290         HOOK_FreeQueueHooks();
291         WIN_DestroyThreadWindows( GetDesktopWindow() );
292         QUEUE_DeleteMsgQueue();
293     }
294
295     if (!(NtCurrentTeb()->tibflags & TEBF_WIN32))
296     {
297         HMODULE16 hModule = GetExePtr( MapHModuleLS(0) );
298
299         /* FIXME: maybe destroy menus (Windows only complains about them
300          * but does nothing);
301          */
302         if (GetModuleUsage16( hModule ) <= 1)
303         {
304             /* ModuleUnload() in "Internals" */
305             HOOK_FreeModuleHooks( hModule );
306             CLASS_FreeModuleClasses( hModule );
307             CURSORICON_FreeModuleIcons( hModule );
308         }
309     }
310     exiting_thread_id = 0;
311 }
312
313
314 /***********************************************************************
315  *           UserClientDllInitialize  (USER32.@)
316  *
317  * USER dll initialisation routine
318  */
319 BOOL WINAPI UserClientDllInitialize( HINSTANCE inst, DWORD reason, LPVOID reserved )
320 {
321     BOOL ret = TRUE;
322     switch(reason)
323     {
324     case DLL_PROCESS_ATTACH:
325         ret = process_attach();
326         break;
327     case DLL_THREAD_DETACH:
328         thread_detach();
329         break;
330     }
331     return ret;
332 }