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