Added LdrDisableThreadCalloutsForDll.
[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 "message.h"
20 #include "queue.h"
21 #include "spy.h"
22 #include "sysmetrics.h"
23 #include "user.h"
24 #include "win.h"
25 #include "debugtools.h"
26
27 DEFAULT_DEBUG_CHANNEL(graphics);
28
29 USER_DRIVER USER_Driver;
30
31 WINE_LOOK TWEAK_WineLook = WIN31_LOOK;
32
33 WORD USER_HeapSel = 0;  /* USER heap selector */
34
35 static HMODULE graphics_driver;
36 static DWORD exiting_thread_id;
37
38 extern void COMM_Init(void);
39 extern void WDML_NotifyThreadDetach(void);
40
41 #define GET_USER_FUNC(name) USER_Driver.p##name = (void*)GetProcAddress( graphics_driver, #name )
42
43 /* load the graphics driver */
44 static BOOL load_driver(void)
45 {
46     char buffer[MAX_PATH];
47     HKEY hkey;
48     DWORD type, count;
49
50     strcpy( buffer, "x11drv" );  /* default value */
51     if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine", &hkey ))
52     {
53         count = sizeof(buffer);
54         RegQueryValueExA( hkey, "GraphicsDriver", 0, &type, buffer, &count );
55         RegCloseKey( hkey );
56     }
57
58     if (!(graphics_driver = LoadLibraryA( buffer )))
59     {
60         MESSAGE( "Could not load graphics driver '%s'\n", buffer );
61         return FALSE;
62     }
63
64     GET_USER_FUNC(InitKeyboard);
65     GET_USER_FUNC(VkKeyScan);
66     GET_USER_FUNC(MapVirtualKey);
67     GET_USER_FUNC(GetKeyNameText);
68     GET_USER_FUNC(ToUnicode);
69     GET_USER_FUNC(Beep);
70     GET_USER_FUNC(InitMouse);
71     GET_USER_FUNC(SetCursor);
72     GET_USER_FUNC(GetCursorPos);
73     GET_USER_FUNC(SetCursorPos);
74     GET_USER_FUNC(GetScreenSaveActive);
75     GET_USER_FUNC(SetScreenSaveActive);
76     GET_USER_FUNC(AcquireClipboard);
77     GET_USER_FUNC(ReleaseClipboard);
78     GET_USER_FUNC(SetClipboardData);
79     GET_USER_FUNC(GetClipboardData);
80     GET_USER_FUNC(IsClipboardFormatAvailable);
81     GET_USER_FUNC(RegisterClipboardFormat);
82     GET_USER_FUNC(IsSelectionOwner);
83     GET_USER_FUNC(ResetSelectionOwner);
84     GET_USER_FUNC(CreateWindow);
85     GET_USER_FUNC(DestroyWindow);
86     GET_USER_FUNC(GetDC);
87     GET_USER_FUNC(ForceWindowRaise);
88     GET_USER_FUNC(MsgWaitForMultipleObjectsEx);
89     GET_USER_FUNC(ScrollDC);
90     GET_USER_FUNC(ScrollWindowEx);
91     GET_USER_FUNC(SetFocus);
92     GET_USER_FUNC(SetParent);
93     GET_USER_FUNC(SetWindowPos);
94     GET_USER_FUNC(SetWindowRgn);
95     GET_USER_FUNC(SetWindowIcon);
96     GET_USER_FUNC(SetWindowStyle);
97     GET_USER_FUNC(SetWindowText);
98     GET_USER_FUNC(ShowWindow);
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
199     /* Create USER heap */
200     if ((instance = LoadLibrary16( "USER.EXE" )) < 32) return FALSE;
201     USER_HeapSel = instance | 7;
202
203      /* Global atom table initialisation */
204     if (!ATOM_Init( USER_HeapSel )) return FALSE;
205
206     /* Load the graphics driver */
207     tweak_init();
208     if (!load_driver()) return FALSE;
209
210     /* Initialize system colors and metrics*/
211     SYSMETRICS_Init();
212     SYSCOLOR_Init();
213
214     /* Setup palette function pointers */
215     palette_init();
216
217     /* Initialize window procedures */
218     if (!WINPROC_Init()) return FALSE;
219
220     /* Initialize built-in window classes */
221     controls_init();
222
223     /* Initialize dialog manager */
224     if (!DIALOG_Init()) return FALSE;
225
226     /* Initialize menus */
227     if (!MENU_Init()) return FALSE;
228
229     /* Initialize message spying */
230     if (!SPY_Init()) return FALSE;
231
232     /* Create message queue of initial thread */
233     InitThreadInput16( 0, 0 );
234
235     /* Create desktop window */
236     if (!WIN_CreateDesktopWindow()) return FALSE;
237
238     /* Initialize keyboard driver */
239     if (USER_Driver.pInitKeyboard) USER_Driver.pInitKeyboard( InputKeyStateTable );
240
241     /* Initialize mouse driver */
242     if (USER_Driver.pInitMouse) USER_Driver.pInitMouse( InputKeyStateTable );
243
244     /* Initialize 16-bit serial communications */
245     COMM_Init();
246
247     return TRUE;
248 }
249
250
251 /**********************************************************************
252  *           USER_IsExitingThread
253  */
254 BOOL USER_IsExitingThread( DWORD tid )
255 {
256     return (tid == exiting_thread_id);
257 }
258
259
260 /**********************************************************************
261  *           thread_detach
262  */
263 static void thread_detach(void)
264 {
265     HQUEUE16 hQueue = GetThreadQueue16( 0 );
266
267     exiting_thread_id = GetCurrentThreadId();
268
269     WDML_NotifyThreadDetach();
270
271     if (hQueue)
272     {
273         TIMER_RemoveQueueTimers( hQueue );
274         HOOK_FreeQueueHooks();
275         WIN_DestroyThreadWindows( GetDesktopWindow() );
276         QUEUE_DeleteMsgQueue();
277     }
278
279     if (!(NtCurrentTeb()->tibflags & TEBF_WIN32))
280     {
281         HMODULE16 hModule = GetExePtr( MapHModuleLS(0) );
282
283         /* FIXME: maybe destroy menus (Windows only complains about them
284          * but does nothing);
285          */
286         if (GetModuleUsage16( hModule ) <= 1)
287         {
288             /* ModuleUnload() in "Internals" */
289             HOOK_FreeModuleHooks( hModule );
290             CLASS_FreeModuleClasses( hModule );
291             CURSORICON_FreeModuleIcons( hModule );
292         }
293     }
294     exiting_thread_id = 0;
295 }
296
297
298 /***********************************************************************
299  *           UserClientDllInitialize  (USER32.@)
300  *
301  * USER dll initialisation routine
302  */
303 BOOL WINAPI UserClientDllInitialize( HINSTANCE inst, DWORD reason, LPVOID reserved )
304 {
305     BOOL ret = TRUE;
306     switch(reason)
307     {
308     case DLL_PROCESS_ATTACH:
309         ret = process_attach();
310         break;
311     case DLL_THREAD_DETACH:
312         thread_detach();
313         break;
314     }
315     return ret;
316 }