Bugfix: really *do* switch to the initial task's stack.
[wine/testsucceed.git] / loader / main.c
blob1c76b0715cb07d7d43bb9ae16398cac2052b4104
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 "mouse.h"
30 #include "input.h"
31 #include "miscemu.h"
32 #include "options.h"
33 #include "process.h"
34 #include "spy.h"
35 #include "tweak.h"
36 #include "user.h"
37 #include "global.h"
38 #include "dce.h"
39 #include "shell.h"
40 #include "winproc.h"
41 #include "syslevel.h"
42 #include "thread.h"
43 #include "task.h"
44 #include "debug.h"
47 int __winelib = 1; /* Winelib run-time flag */
49 /***********************************************************************
50 * Main initialisation routine
52 BOOL32 MAIN_MainInit(void)
54 /* Initialize syslevel handling */
55 SYSLEVEL_Init();
57 /* Initialize signal handling */
58 if (!SIGNAL_Init()) return FALSE;
60 /* Load the configuration file */
61 if (!PROFILE_LoadWineIni()) return FALSE;
63 /* Initialize DOS memory */
64 if (!DOSMEM_Init(0)) return FALSE;
66 /* Initialise DOS drives */
67 if (!DRIVE_Init()) return FALSE;
69 /* Initialise DOS directories */
70 if (!DIR_Init()) return FALSE;
72 /* Initialize event handling */
73 if (!EVENT_Init()) return FALSE;
75 /* Initialize communications */
76 COMM_Init();
78 /* Initialize IO-port permissions */
79 IO_port_init();
81 /* registry initialisation */
82 SHELL_LoadRegistry();
84 return TRUE;
87 /***********************************************************************
88 * KERNEL initialisation routine
90 BOOL32 WINAPI MAIN_KernelInit(HINSTANCE32 hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
92 static BOOL32 initDone = FALSE;
94 HMODULE16 hModule;
96 if ( initDone ) return TRUE;
97 initDone = TRUE;
99 /* Initialize special KERNEL entry points */
100 hModule = GetModuleHandle16( "KERNEL" );
101 if ( hModule )
103 WORD cs, ds;
105 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
106 NE_SetEntryPoint( hModule, 178, GetWinFlags() );
108 /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
109 GET_CS(cs); GET_DS(ds);
110 NE_SetEntryPoint( hModule, 454, cs );
111 NE_SetEntryPoint( hModule, 455, ds );
113 /* Initialize KERNEL.THHOOK */
114 TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
115 (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
117 /* Initialize the real-mode selector entry points */
118 #define SET_ENTRY_POINT( num, addr ) \
119 NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
120 DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
121 FALSE, FALSE, FALSE, NULL ))
123 SET_ENTRY_POINT( 183, 0x00000 ); /* KERNEL.183: __0000H */
124 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
125 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
126 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
127 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
128 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
129 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
130 SET_ENTRY_POINT( 173, 0xf0000 ); /* KERNEL.173: __ROMBIOS */
131 SET_ENTRY_POINT( 194, 0xf0000 ); /* KERNEL.194: __F000H */
132 NE_SetEntryPoint( hModule, 193, DOSMEM_BiosSeg ); /* KERNEL.193: __0040H */
133 #undef SET_ENTRY_POINT
136 return TRUE;
139 /***********************************************************************
140 * GDI initialisation routine
142 BOOL32 WINAPI MAIN_GdiInit(HINSTANCE32 hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
144 NE_MODULE *pModule;
146 if ( GDI_HeapSel ) return TRUE;
148 /* Create GDI heap */
149 pModule = NE_GetPtr( GetModuleHandle16( "GDI" ) );
150 if ( pModule )
152 GDI_HeapSel = GlobalHandleToSel( (NE_SEG_TABLE( pModule ) +
153 pModule->dgroup - 1)->hSeg );
155 else
157 GDI_HeapSel = GlobalAlloc16( GMEM_FIXED, GDI_HEAP_SIZE );
158 LocalInit( GDI_HeapSel, 0, GDI_HEAP_SIZE-1 );
161 /* GDI initialisation */
162 return GDI_Init();
165 /***********************************************************************
166 * USER initialisation routine
168 BOOL32 WINAPI MAIN_UserInit(HINSTANCE32 hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
170 NE_MODULE *pModule;
171 int queueSize;
173 if ( USER_HeapSel ) return TRUE;
175 /* Create USER heap */
176 pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
177 if ( pModule )
179 USER_HeapSel = GlobalHandleToSel( (NE_SEG_TABLE( pModule ) +
180 pModule->dgroup - 1)->hSeg );
182 else
184 USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 0x10000 );
185 LocalInit( USER_HeapSel, 0, 0xffff );
188 /* Initialize Wine tweaks */
189 if (!TWEAK_Init()) return FALSE;
191 /* Initialize OEM Bitmaps */
192 if (!OBM_Init()) return FALSE;
194 /* Global atom table initialisation */
195 if (!ATOM_Init( USER_HeapSel )) return FALSE;
197 /* Initialize system colors and metrics*/
198 SYSMETRICS_Init();
199 SYSCOLOR_Init();
201 /* Create the DCEs */
202 DCE_Init();
204 /* Initialize window procedures */
205 if (!WINPROC_Init()) return FALSE;
207 /* Initialize built-in window classes */
208 if (!WIDGETS_Init()) return FALSE;
210 /* Initialize dialog manager */
211 if (!DIALOG_Init()) return FALSE;
213 /* Initialize menus */
214 if (!MENU_Init()) return FALSE;
216 /* Initialize multimedia */
217 if (!MULTIMEDIA_Init()) return FALSE;
219 /* Create desktop window */
220 if (!WIN_CreateDesktopWindow()) return FALSE;
222 /* Initialize message spying */
223 if (!SPY_Init()) return FALSE;
225 /* Check wine.conf for old/bad entries */
226 if (!TWEAK_CheckConfiguration()) return FALSE;
228 /* Create system message queue */
229 queueSize = GetProfileInt32A( "windows", "TypeAhead", 120 );
230 if (!QUEUE_CreateSysMsgQueue( queueSize )) return FALSE;
232 /* Set double click time */
233 SetDoubleClickTime32( GetProfileInt32A("windows","DoubleClickSpeed",452) );
235 /* Create task message queue for the initial task */
236 if ( GetCurrentTask() )
238 queueSize = GetProfileInt32A( "windows", "DefaultQueueSize", 8 );
239 if (!SetMessageQueue32( queueSize )) return FALSE;
242 /* Initialize keyboard driver */
243 KEYBOARD_Enable( keybd_event, InputKeyStateTable );
245 /* Initialize mouse driver */
246 MOUSE_Enable( mouse_event );
248 return TRUE;
252 /***********************************************************************
253 * Winelib initialisation routine
255 HINSTANCE32 MAIN_WinelibInit( int *argc, char *argv[] )
257 WINE_MODREF *wm;
258 NE_MODULE *pModule;
259 OFSTRUCT ofs;
260 HMODULE16 hModule;
261 HINSTANCE16 hInstance;
263 /* Create the initial process */
264 if (!PROCESS_Init()) return 0;
266 /* Parse command line arguments */
267 MAIN_WineInit( argc, argv );
269 /* Main initialization */
270 if (!MAIN_MainInit()) return 0;
272 /* Initialize KERNEL */
273 if (!MAIN_KernelInit(0, 0, NULL)) return 0;
275 /* Initialize GDI */
276 if (!MAIN_GdiInit(0, 0, NULL)) return 0;
278 /* Initialize USER */
279 if (!MAIN_UserInit(0, 0, NULL)) return 0;
281 /* Create and switch to initial task */
282 if (!(wm = ELF_CreateDummyModule( argv[0], argv[0], PROCESS_Current() )))
283 return 0;
284 PROCESS_Current()->exe_modref = wm;
286 strcpy( ofs.szPathName, wm->modname );
287 if ((hModule = MODULE_CreateDummyModule( &ofs )) < 32) return 0;
288 pModule = (NE_MODULE *)GlobalLock16( hModule );
289 pModule->flags = NE_FFLAGS_WIN32;
290 pModule->module32 = wm->module;
292 hInstance = NE_CreateInstance( pModule, NULL, TRUE );
293 PROCESS_Current()->task = TASK_Create( THREAD_Current(), pModule, hInstance, 0, FALSE );
295 TASK_StartTask( PROCESS_Current()->task );
297 InitApp( hInstance );
299 return wm->module;