Implemented new Wine startup sequence, separating startup into
[wine/wine-kai.git] / loader / main.c
blob7b1e84630be7a79848bef759279ce5c18ab267c6
1 /*
2 * Main initialization code
3 */
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"
45 int __winelib = 1; /* Winelib run-time flag */
47 /***********************************************************************
48 * Main initialisation routine
50 BOOL32 MAIN_MainInit(void)
52 /* Initialize syslevel handling */
53 SYSLEVEL_Init();
55 /* Initialize signal handling */
56 if (!SIGNAL_Init()) return FALSE;
58 /* Load the configuration file */
59 if (!PROFILE_LoadWineIni()) return FALSE;
61 /* Initialize DOS memory */
62 if (!DOSMEM_Init(0)) return FALSE;
64 /* Initialise DOS drives */
65 if (!DRIVE_Init()) return FALSE;
67 /* Initialise DOS directories */
68 if (!DIR_Init()) return FALSE;
70 /* Initialize event handling */
71 if (!EVENT_Init()) return FALSE;
73 /* Initialize communications */
74 COMM_Init();
76 /* Initialize IO-port permissions */
77 IO_port_init();
79 /* registry initialisation */
80 SHELL_LoadRegistry();
82 return TRUE;
85 /***********************************************************************
86 * KERNEL initialisation routine
88 BOOL32 WINAPI MAIN_KernelInit(HINSTANCE32 hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
90 static BOOL32 initDone = FALSE;
92 NE_MODULE *pModule;
93 HMODULE16 hModule;
95 if ( initDone ) return TRUE;
96 initDone = TRUE;
98 /* Create and switch to initial task */
99 pModule = NE_GetPtr( GetModuleHandle16( "KERNEL32" ) );
100 if ( pModule )
102 THDB *thdb = THREAD_Current();
103 HINSTANCE16 hInstance = NE_CreateInstance( pModule, NULL, TRUE );
104 thdb->process->task = TASK_Create( thdb, pModule, hInstance, 0, FALSE );
106 TASK_StartTask( thdb->process->task );
109 /* Initialize special KERNEL entry points */
110 hModule = GetModuleHandle16( "KERNEL" );
111 if ( hModule )
113 WORD cs, ds;
115 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
116 NE_SetEntryPoint( hModule, 178, GetWinFlags() );
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 );
123 /* Initialize KERNEL.THHOOK */
124 TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
125 (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
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 ))
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
146 return TRUE;
149 /***********************************************************************
150 * GDI initialisation routine
152 BOOL32 WINAPI MAIN_GdiInit(HINSTANCE32 hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
154 NE_MODULE *pModule;
156 if ( GDI_HeapSel ) return TRUE;
158 /* Create GDI heap */
159 pModule = NE_GetPtr( GetModuleHandle16( "GDI" ) );
160 if ( pModule )
162 GDI_HeapSel = GlobalHandleToSel( (NE_SEG_TABLE( pModule ) +
163 pModule->dgroup - 1)->hSeg );
165 else
167 GDI_HeapSel = GlobalAlloc16( GMEM_FIXED, GDI_HEAP_SIZE );
168 LocalInit( GDI_HeapSel, 0, GDI_HEAP_SIZE-1 );
171 /* GDI initialisation */
172 return GDI_Init();
175 /***********************************************************************
176 * USER initialisation routine
178 BOOL32 WINAPI MAIN_UserInit(HINSTANCE32 hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
180 NE_MODULE *pModule;
181 int queueSize;
183 if ( USER_HeapSel ) return TRUE;
185 /* Create USER heap */
186 pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
187 if ( pModule )
189 USER_HeapSel = GlobalHandleToSel( (NE_SEG_TABLE( pModule ) +
190 pModule->dgroup - 1)->hSeg );
192 else
194 USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 0x10000 );
195 LocalInit( USER_HeapSel, 0, 0xffff );
198 /* Initialize Wine tweaks */
199 if (!TWEAK_Init()) return FALSE;
201 /* Initialize OEM Bitmaps */
202 if (!OBM_Init()) return FALSE;
204 /* Global atom table initialisation */
205 if (!ATOM_Init( USER_HeapSel )) return FALSE;
207 /* Initialize system colors and metrics*/
208 SYSMETRICS_Init();
209 SYSCOLOR_Init();
211 /* Create the DCEs */
212 DCE_Init();
214 /* Initialize keyboard */
215 if (!KEYBOARD_Init()) return FALSE;
217 /* Initialize window procedures */
218 if (!WINPROC_Init()) return FALSE;
220 /* Initialize built-in window classes */
221 if (!WIDGETS_Init()) return FALSE;
223 /* Initialize dialog manager */
224 if (!DIALOG_Init()) return FALSE;
226 /* Initialize menus */
227 if (!MENU_Init()) return FALSE;
229 /* Initialize multimedia */
230 if (!MULTIMEDIA_Init()) return FALSE;
232 /* Create desktop window */
233 if (!WIN_CreateDesktopWindow()) return FALSE;
235 /* Initialize message spying */
236 if (!SPY_Init()) return FALSE;
238 /* Check wine.conf for old/bad entries */
239 if (!TWEAK_CheckConfiguration()) return FALSE;
241 /* Create system message queue */
242 queueSize = GetProfileInt32A( "windows", "TypeAhead", 120 );
243 if (!QUEUE_CreateSysMsgQueue( queueSize )) return FALSE;
245 /* Set double click time */
246 SetDoubleClickTime32( GetProfileInt32A("windows","DoubleClickSpeed",452) );
248 /* Create task message queue for the initial task */
249 if ( GetCurrentTask() )
251 queueSize = GetProfileInt32A( "windows", "DefaultQueueSize", 8 );
252 if (!SetMessageQueue32( queueSize )) return FALSE;
255 return TRUE;
259 /***********************************************************************
260 * Winelib initialisation routine
262 HINSTANCE32 MAIN_WinelibInit( int *argc, char *argv[] )
264 WINE_MODREF *wm;
265 NE_MODULE *pModule;
266 OFSTRUCT ofs;
267 HMODULE16 hModule;
268 HINSTANCE16 hInstance;
270 /* Create the initial process */
271 if (!PROCESS_Init()) return 0;
273 /* Parse command line arguments */
274 MAIN_WineInit( argc, argv );
276 /* Main initialization */
277 if (!MAIN_MainInit()) return 0;
279 /* Initialize KERNEL */
280 if (!MAIN_KernelInit(0, 0, NULL)) return 0;
282 /* Initialize GDI */
283 if (!MAIN_GdiInit(0, 0, NULL)) return 0;
285 /* Initialize USER */
286 if (!MAIN_UserInit(0, 0, NULL)) return 0;
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;
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;
299 hInstance = NE_CreateInstance( pModule, NULL, TRUE );
300 PROCESS_Current()->task = TASK_Create( THREAD_Current(), pModule, hInstance, 0, FALSE );
302 TASK_StartTask( PROCESS_Current()->task );
304 InitApp( hInstance );
306 return wm->module;