Removed input.h, queue.h, spy.h and sysmetrics.h and moved their
[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 "message.h"
34 #include "user.h"
35 #include "thread.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 COMM_Init(void);
54 extern void WDML_NotifyThreadDetach(void);
55
56 #define GET_USER_FUNC(name) USER_Driver.p##name = (void*)GetProcAddress( graphics_driver, #name )
57
58 /* load the graphics driver */
59 static BOOL load_driver(void)
60 {
61     char buffer[MAX_PATH];
62     HKEY hkey;
63     DWORD type, count;
64
65     strcpy( buffer, "x11drv" );  /* default value */
66     if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Wine", &hkey ))
67     {
68         count = sizeof(buffer);
69         RegQueryValueExA( hkey, "GraphicsDriver", 0, &type, buffer, &count );
70         RegCloseKey( hkey );
71     }
72
73     if (!(graphics_driver = LoadLibraryA( buffer )))
74     {
75         MESSAGE( "Could not load graphics driver '%s'\n", buffer );
76         return FALSE;
77     }
78
79     GET_USER_FUNC(InitKeyboard);
80     GET_USER_FUNC(VkKeyScan);
81     GET_USER_FUNC(MapVirtualKey);
82     GET_USER_FUNC(GetKeyNameText);
83     GET_USER_FUNC(ToUnicode);
84     GET_USER_FUNC(Beep);
85     GET_USER_FUNC(InitMouse);
86     GET_USER_FUNC(SetCursor);
87     GET_USER_FUNC(GetCursorPos);
88     GET_USER_FUNC(SetCursorPos);
89     GET_USER_FUNC(GetScreenSaveActive);
90     GET_USER_FUNC(SetScreenSaveActive);
91     GET_USER_FUNC(AcquireClipboard);
92     GET_USER_FUNC(ReleaseClipboard);
93     GET_USER_FUNC(SetClipboardData);
94     GET_USER_FUNC(GetClipboardData);
95     GET_USER_FUNC(IsClipboardFormatAvailable);
96     GET_USER_FUNC(RegisterClipboardFormat);
97     GET_USER_FUNC(GetClipboardFormatName);
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(ReleaseDC);
106     GET_USER_FUNC(ScrollDC);
107     GET_USER_FUNC(ScrollWindowEx);
108     GET_USER_FUNC(SetFocus);
109     GET_USER_FUNC(SetParent);
110     GET_USER_FUNC(SetWindowPos);
111     GET_USER_FUNC(SetWindowRgn);
112     GET_USER_FUNC(SetWindowIcon);
113     GET_USER_FUNC(SetWindowStyle);
114     GET_USER_FUNC(SetWindowText);
115     GET_USER_FUNC(ShowWindow);
116     GET_USER_FUNC(SysCommandSizeMove);
117
118     return TRUE;
119 }
120
121
122 /***********************************************************************
123  *           controls_init
124  *
125  * Register the classes for the builtin controls
126  */
127 static void controls_init(void)
128 {
129     extern const struct builtin_class_descr BUTTON_builtin_class;
130     extern const struct builtin_class_descr COMBO_builtin_class;
131     extern const struct builtin_class_descr COMBOLBOX_builtin_class;
132     extern const struct builtin_class_descr DIALOG_builtin_class;
133     extern const struct builtin_class_descr DESKTOP_builtin_class;
134     extern const struct builtin_class_descr EDIT_builtin_class;
135     extern const struct builtin_class_descr ICONTITLE_builtin_class;
136     extern const struct builtin_class_descr LISTBOX_builtin_class;
137     extern const struct builtin_class_descr MDICLIENT_builtin_class;
138     extern const struct builtin_class_descr MENU_builtin_class;
139     extern const struct builtin_class_descr SCROLL_builtin_class;
140     extern const struct builtin_class_descr STATIC_builtin_class;
141
142     CLASS_RegisterBuiltinClass( &BUTTON_builtin_class );
143     CLASS_RegisterBuiltinClass( &COMBO_builtin_class );
144     CLASS_RegisterBuiltinClass( &COMBOLBOX_builtin_class );
145     CLASS_RegisterBuiltinClass( &DIALOG_builtin_class );
146     CLASS_RegisterBuiltinClass( &DESKTOP_builtin_class );
147     CLASS_RegisterBuiltinClass( &EDIT_builtin_class );
148     CLASS_RegisterBuiltinClass( &ICONTITLE_builtin_class );
149     CLASS_RegisterBuiltinClass( &LISTBOX_builtin_class );
150     CLASS_RegisterBuiltinClass( &MDICLIENT_builtin_class );
151     CLASS_RegisterBuiltinClass( &MENU_builtin_class );
152     CLASS_RegisterBuiltinClass( &SCROLL_builtin_class );
153     CLASS_RegisterBuiltinClass( &STATIC_builtin_class );
154 }
155
156
157 /***********************************************************************
158  *           palette_init
159  *
160  * Patch the function pointers in GDI for SelectPalette and RealizePalette
161  */
162 static void palette_init(void)
163 {
164     void **ptr;
165     HMODULE module = GetModuleHandleA( "gdi32" );
166     if (!module)
167     {
168         ERR( "cannot get GDI32 handle\n" );
169         return;
170     }
171     if ((ptr = (void**)GetProcAddress( module, "pfnSelectPalette" )))
172         pfnGDISelectPalette = InterlockedExchangePointer( ptr, SelectPalette );
173     else ERR( "cannot find pfnSelectPalette in GDI32\n" );
174     if ((ptr = (void**)GetProcAddress( module, "pfnRealizePalette" )))
175         pfnGDIRealizePalette = InterlockedExchangePointer( ptr, UserRealizePalette );
176     else ERR( "cannot find pfnRealizePalette in GDI32\n" );
177 }
178
179
180 /***********************************************************************
181  *           tweak_init
182  */
183 static void tweak_init(void)
184 {
185     static const char *OS = "Win3.1";
186     char buffer[80];
187     HKEY hkey;
188     DWORD type, count = sizeof(buffer);
189
190     if (RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Tweak.Layout", &hkey ))
191         return;
192     if (RegQueryValueExA( hkey, "WineLook", 0, &type, buffer, &count ))
193         strcpy( buffer, "Win31" );  /* default value */
194     RegCloseKey( hkey );
195
196     /* WIN31_LOOK is default */
197     if (!strncasecmp( buffer, "Win95", 5 ))
198     {
199         TWEAK_WineLook = WIN95_LOOK;
200         OS = "Win95";
201     }
202     else if (!strncasecmp( buffer, "Win98", 5 ))
203     {
204         TWEAK_WineLook = WIN98_LOOK;
205         OS = "Win98";
206     }
207     TRACE("Using %s look and feel.\n", OS);
208 }
209
210
211 /***********************************************************************
212  *           USER initialisation routine
213  */
214 static BOOL process_attach(void)
215 {
216     HINSTANCE16 instance;
217
218     /* Create USER heap */
219     if ((instance = LoadLibrary16( "USER.EXE" )) >= 32) USER_HeapSel = instance | 7;
220     else
221     {
222         USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 65536 );
223         LocalInit16( USER_HeapSel, 32, 65534 );
224     }
225
226     /* Load the graphics driver */
227     tweak_init();
228     if (!load_driver()) return FALSE;
229
230     /* Initialize system colors and metrics */
231     SYSMETRICS_Init();
232     SYSCOLOR_Init();
233
234     /* Setup palette function pointers */
235     palette_init();
236
237     /* Initialize window procedures */
238     if (!WINPROC_Init()) return FALSE;
239
240     /* Initialize built-in window classes */
241     controls_init();
242
243     /* Initialize dialog manager */
244     if (!DIALOG_Init()) return FALSE;
245
246     /* Initialize menus */
247     if (!MENU_Init()) return FALSE;
248
249     /* Initialize message spying */
250     if (!SPY_Init()) return FALSE;
251
252     /* Create message queue of initial thread */
253     InitThreadInput16( 0, 0 );
254
255     /* Create desktop window */
256     if (!WIN_CreateDesktopWindow()) return FALSE;
257
258     /* Initialize keyboard driver */
259     if (USER_Driver.pInitKeyboard) USER_Driver.pInitKeyboard( InputKeyStateTable );
260
261     /* Initialize mouse driver */
262     if (USER_Driver.pInitMouse) USER_Driver.pInitMouse( InputKeyStateTable );
263
264     /* Initialize 16-bit serial communications */
265     COMM_Init();
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     if (!(NtCurrentTeb()->tibflags & TEBF_WIN32))
294     {
295         HMODULE16 hModule = GetExePtr( MapHModuleLS(0) );
296
297         /* FIXME: maybe destroy menus (Windows only complains about them
298          * but does nothing);
299          */
300         if (GetModuleUsage16( hModule ) <= 1)
301         {
302             /* ModuleUnload() in "Internals" */
303             /* HOOK_FreeModuleHooks( hModule ); */
304             CLASS_FreeModuleClasses( hModule );
305             CURSORICON_FreeModuleIcons( hModule );
306         }
307     }
308     exiting_thread_id = 0;
309 }
310
311
312 /***********************************************************************
313  *           UserClientDllInitialize  (USER32.@)
314  *
315  * USER dll initialisation routine
316  */
317 BOOL WINAPI UserClientDllInitialize( HINSTANCE inst, DWORD reason, LPVOID reserved )
318 {
319     BOOL ret = TRUE;
320     switch(reason)
321     {
322     case DLL_PROCESS_ATTACH:
323         ret = process_attach();
324         break;
325     case DLL_THREAD_DETACH:
326         thread_detach();
327         break;
328     }
329     return ret;
330 }