Reorganization of the loader to correctly load and free libraries and
[wine] / loader / main.c
1 /*
2  * Main initialization code
3  */
4
5 #include <stdlib.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <errno.h>
12 #include "wine/winbase16.h"
13 #include "wine/winuser16.h"
14 #include "bitmap.h"
15 #include "comm.h"
16 #include "neexe.h"
17 #include "main.h"
18 #include "menu.h"
19 #include "message.h"
20 #include "multimedia.h"
21 #include "dialog.h"
22 #include "drive.h"
23 #include "queue.h"
24 #include "sysmetrics.h"
25 #include "file.h"
26 #include "heap.h"
27 #include "keyboard.h"
28 #include "mouse.h"
29 #include "input.h"
30 #include "miscemu.h"
31 #include "options.h"
32 #include "process.h"
33 #include "spy.h"
34 #include "tweak.h"
35 #include "user.h"
36 #include "cursoricon.h"
37 #include "global.h"
38 #include "dce.h"
39 #include "shell.h"
40 #include "winproc.h"
41 #include "syslevel.h"
42 #include "services.h"
43 #include "thread.h"
44 #include "task.h"
45 #include "debug.h"
46 #include "psdrv.h"
47 #include "server.h"
48 #include "cursoricon.h"
49 #include "loadorder.h"
50
51 int __winelib = 1;  /* Winelib run-time flag */
52
53 /***********************************************************************
54  *           Main initialisation routine
55  */
56 BOOL MAIN_MainInit(void)
57 {
58     /* Set server debug level */
59     /* To fool make_debug: TRACE(server) */
60     CLIENT_SetDebug( TRACE_ON(server) );
61
62     /* Initialize syslevel handling */
63     SYSLEVEL_Init();
64
65     /* Initialize signal handling */
66     if (!SIGNAL_Init()) return FALSE;
67
68     /* Initialize kernel services thread */
69     if (!SERVICE_Init()) return FALSE;
70
71     /* Load the configuration file */
72     if (!PROFILE_LoadWineIni()) return FALSE;
73
74     /* Initialize module loadorder */
75     if (!MODULE_InitLoadOrder()) return FALSE;
76
77       /* Initialize DOS memory */
78     if (!DOSMEM_Init(0)) return FALSE;
79
80     /* Initialise DOS drives */
81     if (!DRIVE_Init()) return FALSE;
82
83     /* Initialise DOS directories */
84     if (!DIR_Init()) return FALSE;
85
86       /* Initialize event handling */
87     if (!EVENT_Init()) return FALSE;
88
89     /* Initialize communications */
90     COMM_Init();
91
92     /* Initialize IO-port permissions */
93     IO_port_init();
94
95     /* registry initialisation */
96     SHELL_LoadRegistry();
97     
98     /* Read DOS config.sys */
99     if (!DOSCONF_ReadConfig()) return FALSE;
100
101     return TRUE;
102 }
103
104 /***********************************************************************
105  *           KERNEL initialisation routine
106  */
107 BOOL WINAPI MAIN_KernelInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
108 {
109     static BOOL initDone = FALSE;
110
111     HMODULE16 hModule;
112
113     if ( initDone ) return TRUE;
114     initDone = TRUE;
115
116     /* Initialize special KERNEL entry points */
117     hModule = GetModuleHandle16( "KERNEL" );
118     if ( hModule )
119     {
120         WORD cs, ds;
121
122         /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
123         NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
124
125         /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
126         GET_CS(cs); GET_DS(ds);
127         NE_SetEntryPoint( hModule, 454, cs );
128         NE_SetEntryPoint( hModule, 455, ds );
129
130         /* Initialize KERNEL.THHOOK */
131         TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
132                                   (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
133
134         /* Initialize the real-mode selector entry points */
135 #define SET_ENTRY_POINT( num, addr ) \
136         NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
137                           DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
138                           FALSE, FALSE, FALSE, NULL ))
139
140         SET_ENTRY_POINT( 183, 0x00000 );  /* KERNEL.183: __0000H */
141         SET_ENTRY_POINT( 174, 0xa0000 );  /* KERNEL.174: __A000H */
142         SET_ENTRY_POINT( 181, 0xb0000 );  /* KERNEL.181: __B000H */
143         SET_ENTRY_POINT( 182, 0xb8000 );  /* KERNEL.182: __B800H */
144         SET_ENTRY_POINT( 195, 0xc0000 );  /* KERNEL.195: __C000H */
145         SET_ENTRY_POINT( 179, 0xd0000 );  /* KERNEL.179: __D000H */
146         SET_ENTRY_POINT( 190, 0xe0000 );  /* KERNEL.190: __E000H */
147         NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg );  /* KERNEL.173: __ROMBIOS */
148         NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
149         NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg );  /* KERNEL.194: __F000H */
150 #undef SET_ENTRY_POINT
151     }
152     return TRUE;
153 }
154
155 /***********************************************************************
156  *           GDI initialisation routine
157  */
158 BOOL WINAPI MAIN_GdiInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
159 {
160     NE_MODULE *pModule;
161
162     if ( GDI_HeapSel ) return TRUE;
163
164     /* Create GDI heap */
165     pModule = NE_GetPtr( GetModuleHandle16( "GDI" ) );
166     if ( pModule )
167     {
168         GDI_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) + 
169                                           pModule->dgroup - 1)->hSeg );
170     }
171     else
172     {
173         GDI_HeapSel = GlobalAlloc16( GMEM_FIXED, GDI_HEAP_SIZE );
174         LocalInit16( GDI_HeapSel, 0, GDI_HEAP_SIZE-1 );
175     }
176
177     if (!TWEAK_Init()) return FALSE;
178
179     /* GDI initialisation */
180     if(!GDI_Init()) return FALSE;
181
182
183     /* PSDRV initialization */
184     if(!PSDRV_Init()) return FALSE;
185
186     return TRUE;
187 }
188
189 /***********************************************************************
190  *           USER initialisation routine
191  */
192 BOOL WINAPI MAIN_UserInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
193 {
194     NE_MODULE *pModule;
195     int queueSize;
196
197     if ( USER_HeapSel ) return TRUE;
198
199     /* Create USER heap */
200     pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
201     if ( pModule )
202     {
203         USER_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) + 
204                                            pModule->dgroup - 1)->hSeg );
205     }
206     else
207     {
208         USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 0x10000 );
209         LocalInit16( USER_HeapSel, 0, 0xffff );
210     }
211
212      /* Global atom table initialisation */
213     if (!ATOM_Init( USER_HeapSel )) return FALSE;
214
215     /* Initialize system colors and metrics*/
216     SYSMETRICS_Init();
217     SYSCOLOR_Init();
218
219     /* Create the DCEs */
220     DCE_Init();
221
222     /* Initialize timers */
223     if (!TIMER_Init()) return FALSE;
224     
225     /* Initialize window procedures */
226     if (!WINPROC_Init()) return FALSE;
227
228     /* Initialize built-in window classes */
229     if (!WIDGETS_Init()) return FALSE;
230
231     /* Initialize dialog manager */
232     if (!DIALOG_Init()) return FALSE;
233
234     /* Initialize menus */
235     if (!MENU_Init()) return FALSE;
236
237     /* Initialize cursor/icons */
238     CURSORICON_Init();
239
240     /* Initialize multimedia */
241     if (!MULTIMEDIA_Init()) return FALSE;
242
243     /* Initialize message spying */
244     if (!SPY_Init()) return FALSE;
245
246     /* Check wine.conf for old/bad entries */
247     if (!TWEAK_CheckConfiguration()) return FALSE;
248
249     /* Create system message queue */
250     queueSize = GetProfileIntA( "windows", "TypeAhead", 120 );
251     if (!QUEUE_CreateSysMsgQueue( queueSize )) return FALSE;
252
253     /* Set double click time */
254     SetDoubleClickTime( GetProfileIntA("windows","DoubleClickSpeed",452) );
255
256     /* Create task message queue for the initial task */
257     queueSize = GetProfileIntA( "windows", "DefaultQueueSize", 8 );
258     if (!SetMessageQueue( queueSize )) return FALSE;
259
260     /* Create desktop window */
261     if (!WIN_CreateDesktopWindow()) return FALSE;
262
263     /* Initialize keyboard driver */
264     KEYBOARD_Enable( keybd_event, InputKeyStateTable );
265
266     /* Initialize mouse driver */
267     MOUSE_Enable( mouse_event );
268
269     return TRUE;
270 }
271
272
273 /***********************************************************************
274  *           Winelib initialisation routine
275  */
276 HINSTANCE MAIN_WinelibInit( int *argc, char *argv[] )
277 {
278     WINE_MODREF *wm;
279     NE_MODULE *pModule;
280     OFSTRUCT ofs;
281     HMODULE16 hModule;
282
283     /* Create the initial process */
284     if (!PROCESS_Init()) return 0;
285
286     /* Parse command line arguments */
287     MAIN_WineInit( argc, argv );
288
289     /* Main initialization */
290     if (!MAIN_MainInit()) return 0;
291
292     /* Initialize KERNEL */
293     if (!MAIN_KernelInit(0, 0, NULL)) return 0;
294
295     /* Create and switch to initial task */
296     if (!(wm = ELF_CreateDummyModule( argv[0], argv[0] )))
297         return 0;
298     PROCESS_Current()->exe_modref = wm;
299
300     strcpy( ofs.szPathName, wm->modname );
301     if ((hModule = MODULE_CreateDummyModule( &ofs, NULL )) < 32) return 0;
302     pModule = (NE_MODULE *)GlobalLock16( hModule );
303     pModule->flags = NE_FFLAGS_WIN32;
304     pModule->module32 = wm->module;
305
306     if (!TASK_Create( THREAD_Current(), pModule, 0, 0, FALSE )) return 0;
307     TASK_StartTask( PROCESS_Current()->task );
308
309     /* Initialize GDI and USER */
310     if (!MAIN_GdiInit(0, 0, NULL)) return 0;
311     if (!MAIN_UserInit(0, 0, NULL)) return 0;
312
313     return wm->module;
314 }
315