Removed some unaligned accesses.
[wine/multimedia.git] / loader / main.c
blob7e801b5e5a18ddac0b96016c47b31a8c170bf822
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 "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 "dialog.h"
21 #include "drive.h"
22 #include "queue.h"
23 #include "sysmetrics.h"
24 #include "file.h"
25 #include "heap.h"
26 #include "keyboard.h"
27 #include "mouse.h"
28 #include "input.h"
29 #include "display.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 "win.h"
41 #include "winproc.h"
42 #include "syslevel.h"
43 #include "services.h"
44 #include "winsock.h"
45 #include "thread.h"
46 #include "task.h"
47 #include "debugtools.h"
48 #include "psdrv.h"
49 #include "win16drv.h"
50 #include "callback.h"
51 #include "server.h"
52 #include "loadorder.h"
54 DEFAULT_DEBUG_CHANNEL(server)
56 /***********************************************************************
57 * Main initialisation routine
59 BOOL MAIN_MainInit( int *argc, char *argv[] )
61 /* Create the initial process */
62 if (!PROCESS_Init()) return 0;
64 /* Initialize syslevel handling */
65 SYSLEVEL_Init();
67 /* Parse command line arguments */
68 MAIN_WineInit( argc, argv );
70 /* Set server debug level */
71 CLIENT_SetDebug( TRACE_ON(server) );
73 /* Load the configuration file */
74 if (!PROFILE_LoadWineIni()) return FALSE;
76 /* Initialize module loadorder */
77 if (!MODULE_InitLoadOrder()) return FALSE;
79 /* Initialize DOS memory */
80 if (!DOSMEM_Init(0)) return FALSE;
82 /* Initialise DOS drives */
83 if (!DRIVE_Init()) return FALSE;
85 /* Initialise DOS directories */
86 if (!DIR_Init()) return FALSE;
88 /* Initialize event handling */
89 if (!EVENT_Init()) return FALSE;
91 /* Initialize communications */
92 COMM_Init();
94 /* Initialize IO-port permissions */
95 IO_port_init();
97 /* registry initialisation */
98 SHELL_LoadRegistry();
100 /* Read DOS config.sys */
101 if (!DOSCONF_ReadConfig()) return FALSE;
103 /* Initialize KERNEL */
104 if (!LoadLibrary16( "KRNL386.EXE" )) return FALSE;
105 if (!LoadLibraryA( "KERNEL32" )) return FALSE;
107 return TRUE;
110 /***********************************************************************
111 * KERNEL initialisation routine
113 BOOL WINAPI MAIN_KernelInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
115 static BOOL initDone = FALSE;
117 HMODULE16 hModule;
119 if ( initDone ) return TRUE;
120 initDone = TRUE;
122 /* Initialize special KERNEL entry points */
123 hModule = GetModuleHandle16( "KERNEL" );
124 if ( hModule )
126 WORD cs, ds;
128 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
129 NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
131 /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
132 GET_CS(cs); GET_DS(ds);
133 NE_SetEntryPoint( hModule, 454, cs );
134 NE_SetEntryPoint( hModule, 455, ds );
136 /* Initialize KERNEL.THHOOK */
137 TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
138 (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
140 /* Initialize the real-mode selector entry points */
141 #define SET_ENTRY_POINT( num, addr ) \
142 NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
143 DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
144 FALSE, FALSE, FALSE, NULL ))
146 SET_ENTRY_POINT( 183, 0x00000 ); /* KERNEL.183: __0000H */
147 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
148 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
149 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
150 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
151 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
152 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
153 NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg ); /* KERNEL.173: __ROMBIOS */
154 NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
155 NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg ); /* KERNEL.194: __F000H */
156 #undef SET_ENTRY_POINT
159 /* Initialize relay code */
160 if (!RELAY_Init()) return FALSE;
162 return TRUE;
165 /***********************************************************************
166 * GDI initialisation routine
168 BOOL WINAPI MAIN_GdiInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
170 NE_MODULE *pModule;
172 if ( GDI_HeapSel ) return TRUE;
174 /* Create GDI heap */
175 pModule = NE_GetPtr( GetModuleHandle16( "GDI" ) );
176 if ( pModule )
178 GDI_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) +
179 pModule->dgroup - 1)->hSeg );
181 else
183 GDI_HeapSel = GlobalAlloc16( GMEM_FIXED, GDI_HEAP_SIZE );
184 LocalInit16( GDI_HeapSel, 0, GDI_HEAP_SIZE-1 );
187 if (!TWEAK_Init()) return FALSE;
189 /* GDI initialisation */
190 if(!GDI_Init()) return FALSE;
192 /* Create the Win16 printer driver */
193 if (!WIN16DRV_Init()) return FALSE;
195 /* PSDRV initialization */
196 if(!PSDRV_Init()) return FALSE;
198 return TRUE;
201 /***********************************************************************
202 * USER initialisation routine
204 BOOL WINAPI MAIN_UserInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
206 NE_MODULE *pModule;
207 int queueSize;
209 if ( USER_HeapSel ) return TRUE;
211 /* Create USER heap */
212 pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
213 if ( pModule )
215 USER_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) +
216 pModule->dgroup - 1)->hSeg );
218 else
220 USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 0x10000 );
221 LocalInit16( USER_HeapSel, 0, 0xffff );
224 /* Global atom table initialisation */
225 if (!ATOM_Init( USER_HeapSel )) return FALSE;
227 /* Initialize window handling (critical section) */
228 WIN_Init();
230 /* Initialize system colors and metrics*/
231 SYSMETRICS_Init();
232 SYSCOLOR_Init();
234 /* Create the DCEs */
235 DCE_Init();
237 /* Initialize timers */
238 if (!TIMER_Init()) return FALSE;
240 /* Initialize window procedures */
241 if (!WINPROC_Init()) return FALSE;
243 /* Initialize cursor/icons */
244 CURSORICON_Init();
246 /* Initialize built-in window classes */
247 if (!WIDGETS_Init()) return FALSE;
249 /* Initialize dialog manager */
250 if (!DIALOG_Init()) return FALSE;
252 /* Initialize menus */
253 if (!MENU_Init()) return FALSE;
255 /* Initialize message spying */
256 if (!SPY_Init()) return FALSE;
258 /* Check wine.conf for old/bad entries */
259 if (!TWEAK_CheckConfiguration()) return FALSE;
261 /* Create system message queue */
262 queueSize = GetProfileIntA( "windows", "TypeAhead", 120 );
263 if (!QUEUE_CreateSysMsgQueue( queueSize )) return FALSE;
265 /* Set double click time */
266 SetDoubleClickTime( GetProfileIntA("windows","DoubleClickSpeed",452) );
268 /* Create message queue of initial thread */
269 InitThreadInput16( 0, 0 );
271 /* Create desktop window */
272 if (!WIN_CreateDesktopWindow()) return FALSE;
274 /* Initialize keyboard driver */
275 KEYBOARD_Enable( keybd_event, InputKeyStateTable );
277 /* Initialize mouse driver */
278 MOUSE_Enable( mouse_event );
280 /* Start processing X events */
281 UserRepaintDisable16( FALSE );
283 return TRUE;
287 /***********************************************************************
288 * Winelib initialisation routine
290 HINSTANCE MAIN_WinelibInit( int *argc, char *argv[] )
292 WINE_MODREF *wm;
293 NE_MODULE *pModule;
294 OFSTRUCT ofs;
295 HMODULE16 hModule;
297 /* Main initialization */
298 if (!MAIN_MainInit( argc, argv )) return 0;
300 /* Create and switch to initial task */
301 if (!(wm = ELF_CreateDummyModule( argv[0], argv[0] )))
302 return 0;
303 PROCESS_Current()->exe_modref = wm;
305 strcpy( ofs.szPathName, wm->modname );
306 if ((hModule = MODULE_CreateDummyModule( &ofs, NULL, 0 )) < 32) return 0;
307 pModule = (NE_MODULE *)GlobalLock16( hModule );
308 pModule->flags = NE_FFLAGS_WIN32;
309 pModule->module32 = wm->module;
311 if (!TASK_Create( pModule, FALSE )) return 0;
313 /* Load system DLLs into the initial process (and initialize them) */
314 if ( !LoadLibrary16("GDI.EXE" ) || !LoadLibraryA("GDI32.DLL" )
315 || !LoadLibrary16("USER.EXE") || !LoadLibraryA("USER32.DLL"))
316 ExitProcess( 1 );
318 /* Get pointers to USER routines called by KERNEL */
319 THUNK_InitCallout();
321 return wm->module;
324 /***********************************************************************
325 * ExitKernel16 (KERNEL.2)
327 * Clean-up everything and exit the Wine process.
330 void WINAPI ExitKernel16( void )
332 /* Do the clean-up stuff */
334 WriteOutProfiles16();
335 SHELL_SaveRegistry();
337 TerminateProcess( GetCurrentProcess(), 0 );