Release 991114.
[wine/multimedia.git] / loader / main.c
blob7bf2f2938a0e729e123661c4d4d00264d22003b6
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 "cursoricon.h"
53 #include "loadorder.h"
55 DEFAULT_DEBUG_CHANNEL(server)
57 /***********************************************************************
58 * Main initialisation routine
60 BOOL MAIN_MainInit( int *argc, char *argv[] )
62 /* Create the initial process */
63 if (!PROCESS_Init()) return 0;
65 /* Initialize syslevel handling */
66 SYSLEVEL_Init();
68 /* Parse command line arguments */
69 MAIN_WineInit( argc, argv );
71 /* Set server debug level */
72 CLIENT_SetDebug( TRACE_ON(server) );
74 /* Load the configuration file */
75 if (!PROFILE_LoadWineIni()) return FALSE;
77 /* Initialize module loadorder */
78 if (!MODULE_InitLoadOrder()) return FALSE;
80 /* Initialize DOS memory */
81 if (!DOSMEM_Init(0)) return FALSE;
83 /* Initialise DOS drives */
84 if (!DRIVE_Init()) return FALSE;
86 /* Initialise DOS directories */
87 if (!DIR_Init()) return FALSE;
89 /* Initialize event handling */
90 if (!EVENT_Init()) return FALSE;
92 /* Initialize communications */
93 COMM_Init();
95 /* Initialize IO-port permissions */
96 IO_port_init();
98 /* registry initialisation */
99 SHELL_LoadRegistry();
101 /* Read DOS config.sys */
102 if (!DOSCONF_ReadConfig()) return FALSE;
104 /* Initialize KERNEL */
105 if (!LoadLibrary16( "KRNL386.EXE" )) return FALSE;
106 if (!LoadLibraryA( "KERNEL32" )) return FALSE;
108 return TRUE;
111 /***********************************************************************
112 * KERNEL initialisation routine
114 BOOL WINAPI MAIN_KernelInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
116 static BOOL initDone = FALSE;
118 HMODULE16 hModule;
120 if ( initDone ) return TRUE;
121 initDone = TRUE;
123 /* Initialize special KERNEL entry points */
124 hModule = GetModuleHandle16( "KERNEL" );
125 if ( hModule )
127 WORD cs, ds;
129 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
130 NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
132 /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
133 GET_CS(cs); GET_DS(ds);
134 NE_SetEntryPoint( hModule, 454, cs );
135 NE_SetEntryPoint( hModule, 455, ds );
137 /* Initialize KERNEL.THHOOK */
138 TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
139 (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
141 /* Initialize the real-mode selector entry points */
142 #define SET_ENTRY_POINT( num, addr ) \
143 NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
144 DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
145 FALSE, FALSE, FALSE, NULL ))
147 SET_ENTRY_POINT( 183, 0x00000 ); /* KERNEL.183: __0000H */
148 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
149 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
150 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
151 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
152 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
153 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
154 NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg ); /* KERNEL.173: __ROMBIOS */
155 NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
156 NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg ); /* KERNEL.194: __F000H */
157 #undef SET_ENTRY_POINT
160 /* Initialize relay code */
161 if (!RELAY_Init()) return FALSE;
163 return TRUE;
166 /***********************************************************************
167 * GDI initialisation routine
169 BOOL WINAPI MAIN_GdiInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
171 NE_MODULE *pModule;
173 if ( GDI_HeapSel ) return TRUE;
175 /* Create GDI heap */
176 pModule = NE_GetPtr( GetModuleHandle16( "GDI" ) );
177 if ( pModule )
179 GDI_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) +
180 pModule->dgroup - 1)->hSeg );
182 else
184 GDI_HeapSel = GlobalAlloc16( GMEM_FIXED, GDI_HEAP_SIZE );
185 LocalInit16( GDI_HeapSel, 0, GDI_HEAP_SIZE-1 );
188 if (!TWEAK_Init()) return FALSE;
190 /* GDI initialisation */
191 if(!GDI_Init()) return FALSE;
193 /* Create the Win16 printer driver */
194 if (!WIN16DRV_Init()) return FALSE;
196 /* PSDRV initialization */
197 if(!PSDRV_Init()) return FALSE;
199 return TRUE;
202 /***********************************************************************
203 * USER initialisation routine
205 BOOL WINAPI MAIN_UserInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
207 NE_MODULE *pModule;
208 int queueSize;
210 if ( USER_HeapSel ) return TRUE;
212 /* Create USER heap */
213 pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
214 if ( pModule )
216 USER_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) +
217 pModule->dgroup - 1)->hSeg );
219 else
221 USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 0x10000 );
222 LocalInit16( USER_HeapSel, 0, 0xffff );
225 /* Global atom table initialisation */
226 if (!ATOM_Init( USER_HeapSel )) return FALSE;
228 /* Initialize window handling (critical section) */
229 WIN_Init();
231 /* Initialize system colors and metrics*/
232 SYSMETRICS_Init();
233 SYSCOLOR_Init();
235 /* Create the DCEs */
236 DCE_Init();
238 /* Initialize timers */
239 if (!TIMER_Init()) return FALSE;
241 /* Initialize window procedures */
242 if (!WINPROC_Init()) return FALSE;
244 /* Initialize cursor/icons */
245 CURSORICON_Init();
247 /* Initialize built-in window classes */
248 if (!WIDGETS_Init()) return FALSE;
250 /* Initialize dialog manager */
251 if (!DIALOG_Init()) return FALSE;
253 /* Initialize menus */
254 if (!MENU_Init()) return FALSE;
256 /* Initialize message spying */
257 if (!SPY_Init()) return FALSE;
259 /* Check wine.conf for old/bad entries */
260 if (!TWEAK_CheckConfiguration()) return FALSE;
262 /* Create system message queue */
263 queueSize = GetProfileIntA( "windows", "TypeAhead", 120 );
264 if (!QUEUE_CreateSysMsgQueue( queueSize )) return FALSE;
266 /* Set double click time */
267 SetDoubleClickTime( GetProfileIntA("windows","DoubleClickSpeed",452) );
269 /* Create message queue of initial thread */
270 InitThreadInput16( 0, 0 );
272 /* Create desktop window */
273 if (!WIN_CreateDesktopWindow()) return FALSE;
275 /* Initialize keyboard driver */
276 KEYBOARD_Enable( keybd_event, InputKeyStateTable );
278 /* Initialize mouse driver */
279 MOUSE_Enable( mouse_event );
281 /* Start processing X events */
282 UserRepaintDisable16( FALSE );
284 return TRUE;
288 /***********************************************************************
289 * Winelib initialisation routine
291 HINSTANCE MAIN_WinelibInit( int *argc, char *argv[] )
293 WINE_MODREF *wm;
294 NE_MODULE *pModule;
295 OFSTRUCT ofs;
296 HMODULE16 hModule;
298 /* Main initialization */
299 if (!MAIN_MainInit( argc, argv )) return 0;
301 /* Create and switch to initial task */
302 if (!(wm = ELF_CreateDummyModule( argv[0], argv[0] )))
303 return 0;
304 PROCESS_Current()->exe_modref = wm;
306 strcpy( ofs.szPathName, wm->modname );
307 if ((hModule = MODULE_CreateDummyModule( &ofs, NULL, 0 )) < 32) return 0;
308 pModule = (NE_MODULE *)GlobalLock16( hModule );
309 pModule->flags = NE_FFLAGS_WIN32;
310 pModule->module32 = wm->module;
312 if (!TASK_Create( pModule, FALSE )) return 0;
314 /* Load system DLLs into the initial process (and initialize them) */
315 if ( !LoadLibrary16("GDI.EXE" ) || !LoadLibraryA("GDI32.DLL" )
316 || !LoadLibrary16("USER.EXE") || !LoadLibraryA("USER32.DLL"))
317 ExitProcess( 1 );
319 /* Get pointers to USER routines called by KERNEL */
320 THUNK_InitCallout();
322 return wm->module;
325 /***********************************************************************
326 * ExitKernel16 (KERNEL.2)
328 * Clean-up everything and exit the Wine process.
331 void WINAPI ExitKernel16( void )
333 /* Do the clean-up stuff */
335 WriteOutProfiles16();
336 SHELL_SaveRegistry();
338 TerminateProcess( GetCurrentProcess(), 0 );