Use argv[0] to locate the server executable.
[wine/multimedia.git] / loader / main.c
blobc3d7cc31ab2dfc00719e9d169d1e0b951e2660dc
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 /* store the program name */
62 argv0 = argv[0];
64 /* Create the initial process */
65 if (!PROCESS_Init()) return 0;
67 /* Initialize syslevel handling */
68 SYSLEVEL_Init();
70 /* Parse command line arguments */
71 MAIN_WineInit( argc, argv );
73 /* Load the configuration file */
74 if (!PROFILE_LoadWineIni()) return FALSE;
76 /* Initialise DOS drives */
77 if (!DRIVE_Init()) return FALSE;
79 /* Initialise DOS directories */
80 if (!DIR_Init()) return FALSE;
82 /* Registry initialisation */
83 SHELL_LoadRegistry();
85 /* Global boot finished, the rest is process-local */
86 CLIENT_BootDone( TRACE_ON(server) );
88 /* Initialize module loadorder */
89 if (!MODULE_InitLoadOrder()) return FALSE;
91 /* Initialize DOS memory */
92 if (!DOSMEM_Init(0)) return FALSE;
94 /* Initialize event handling */
95 if (!EVENT_Init()) return FALSE;
97 /* Initialize communications */
98 COMM_Init();
100 /* Initialize IO-port permissions */
101 IO_port_init();
103 /* Read DOS config.sys */
104 if (!DOSCONF_ReadConfig()) return FALSE;
106 /* Initialize KERNEL */
107 if (!LoadLibrary16( "KRNL386.EXE" )) return FALSE;
108 if (!LoadLibraryA( "KERNEL32" )) return FALSE;
110 return TRUE;
113 /***********************************************************************
114 * KERNEL initialisation routine
116 BOOL WINAPI MAIN_KernelInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
118 static BOOL initDone = FALSE;
120 HMODULE16 hModule;
122 if ( initDone ) return TRUE;
123 initDone = TRUE;
125 /* Initialize special KERNEL entry points */
126 hModule = GetModuleHandle16( "KERNEL" );
127 if ( hModule )
129 WORD cs, ds;
131 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
132 NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
134 /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
135 GET_CS(cs); GET_DS(ds);
136 NE_SetEntryPoint( hModule, 454, cs );
137 NE_SetEntryPoint( hModule, 455, ds );
139 /* Initialize KERNEL.THHOOK */
140 TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
141 (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
143 /* Initialize the real-mode selector entry points */
144 #define SET_ENTRY_POINT( num, addr ) \
145 NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
146 DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
147 FALSE, FALSE, FALSE, NULL ))
149 SET_ENTRY_POINT( 183, 0x00000 ); /* KERNEL.183: __0000H */
150 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
151 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
152 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
153 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
154 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
155 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
156 NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg ); /* KERNEL.173: __ROMBIOS */
157 NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
158 NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg ); /* KERNEL.194: __F000H */
159 #undef SET_ENTRY_POINT
162 /* Initialize relay code */
163 if (!RELAY_Init()) return FALSE;
165 return TRUE;
168 /***********************************************************************
169 * GDI initialisation routine
171 BOOL WINAPI MAIN_GdiInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
173 NE_MODULE *pModule;
175 if ( GDI_HeapSel ) return TRUE;
177 /* Create GDI heap */
178 pModule = NE_GetPtr( GetModuleHandle16( "GDI" ) );
179 if ( pModule )
181 GDI_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) +
182 pModule->dgroup - 1)->hSeg );
184 else
186 GDI_HeapSel = GlobalAlloc16( GMEM_FIXED, GDI_HEAP_SIZE );
187 LocalInit16( GDI_HeapSel, 0, GDI_HEAP_SIZE-1 );
190 if (!TWEAK_Init()) return FALSE;
192 /* GDI initialisation */
193 if(!GDI_Init()) return FALSE;
195 /* Create the Win16 printer driver */
196 if (!WIN16DRV_Init()) return FALSE;
198 /* PSDRV initialization */
199 if(!PSDRV_Init()) return FALSE;
201 return TRUE;
204 /***********************************************************************
205 * USER initialisation routine
207 BOOL WINAPI MAIN_UserInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
209 NE_MODULE *pModule;
210 int queueSize;
212 if ( USER_HeapSel ) return TRUE;
214 /* Create USER heap */
215 pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
216 if ( pModule )
218 USER_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) +
219 pModule->dgroup - 1)->hSeg );
221 else
223 USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 0x10000 );
224 LocalInit16( USER_HeapSel, 0, 0xffff );
227 /* Global atom table initialisation */
228 if (!ATOM_Init( USER_HeapSel )) return FALSE;
230 /* Initialize window handling (critical section) */
231 WIN_Init();
233 /* Initialize system colors and metrics*/
234 SYSMETRICS_Init();
235 SYSCOLOR_Init();
237 /* Create the DCEs */
238 DCE_Init();
240 /* Initialize timers */
241 if (!TIMER_Init()) return FALSE;
243 /* Initialize window procedures */
244 if (!WINPROC_Init()) return FALSE;
246 /* Initialize cursor/icons */
247 CURSORICON_Init();
249 /* Initialize built-in window classes */
250 if (!WIDGETS_Init()) return FALSE;
252 /* Initialize dialog manager */
253 if (!DIALOG_Init()) return FALSE;
255 /* Initialize menus */
256 if (!MENU_Init()) return FALSE;
258 /* Initialize message spying */
259 if (!SPY_Init()) return FALSE;
261 /* Check wine.conf for old/bad entries */
262 if (!TWEAK_CheckConfiguration()) return FALSE;
264 /* Create system message queue */
265 queueSize = GetProfileIntA( "windows", "TypeAhead", 120 );
266 if (!QUEUE_CreateSysMsgQueue( queueSize )) return FALSE;
268 /* Set double click time */
269 SetDoubleClickTime( GetProfileIntA("windows","DoubleClickSpeed",452) );
271 /* Create message queue of initial thread */
272 InitThreadInput16( 0, 0 );
274 /* Create desktop window */
275 if (!WIN_CreateDesktopWindow()) return FALSE;
277 /* Initialize keyboard driver */
278 KEYBOARD_Enable( keybd_event, InputKeyStateTable );
280 /* Initialize mouse driver */
281 MOUSE_Enable( mouse_event );
283 /* Start processing X events */
284 UserRepaintDisable16( FALSE );
286 return TRUE;
290 /***********************************************************************
291 * Winelib initialisation routine
293 HINSTANCE MAIN_WinelibInit( int *argc, char *argv[] )
295 WINE_MODREF *wm;
296 NE_MODULE *pModule;
297 HMODULE16 hModule;
299 /* Main initialization */
300 if (!MAIN_MainInit( argc, argv )) return 0;
302 /* Create and switch to initial task */
303 if (!(wm = ELF_CreateDummyModule( argv[0], argv[0] )))
304 return 0;
305 PROCESS_Current()->exe_modref = wm;
307 if ((hModule = MODULE_CreateDummyModule( wm->filename, 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 );