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