Fixed -debugmsg +server.
[wine] / loader / main.c
1 /*
2  * Main initialization code
3  */
4
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"
54
55 DEFAULT_DEBUG_CHANNEL(server)
56
57 /***********************************************************************
58  *           Main initialisation routine
59  */
60 BOOL MAIN_MainInit( int *argc, char *argv[] )
61 {
62     /* Create the initial process */
63     if (!PROCESS_Init()) return 0;
64
65     /* Initialize syslevel handling */
66     SYSLEVEL_Init();
67
68     /* Parse command line arguments */
69     MAIN_WineInit( argc, argv );
70
71     /* Set server debug level */
72     CLIENT_SetDebug( TRACE_ON(server) );
73
74     /* Load the configuration file */
75     if (!PROFILE_LoadWineIni()) return FALSE;
76
77     /* Initialize module loadorder */
78     if (!MODULE_InitLoadOrder()) return FALSE;
79
80       /* Initialize DOS memory */
81     if (!DOSMEM_Init(0)) return FALSE;
82
83     /* Initialise DOS drives */
84     if (!DRIVE_Init()) return FALSE;
85
86     /* Initialise DOS directories */
87     if (!DIR_Init()) return FALSE;
88
89     /* Initialize event handling */
90     if (!EVENT_Init()) return FALSE;
91
92     /* Initialize communications */
93     COMM_Init();
94
95     /* Initialize IO-port permissions */
96     IO_port_init();
97
98     /* registry initialisation */
99     SHELL_LoadRegistry();
100     
101     /* Read DOS config.sys */
102     if (!DOSCONF_ReadConfig()) return FALSE;
103
104     /* Initialize KERNEL */
105     if (!LoadLibrary16( "KRNL386.EXE" )) return FALSE;
106     if (!LoadLibraryA( "KERNEL32" )) return FALSE;
107
108     return TRUE;
109 }
110
111 /***********************************************************************
112  *           KERNEL initialisation routine
113  */
114 BOOL WINAPI MAIN_KernelInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
115 {
116     static BOOL initDone = FALSE;
117
118     HMODULE16 hModule;
119
120     if ( initDone ) return TRUE;
121     initDone = TRUE;
122
123     /* Initialize special KERNEL entry points */
124     hModule = GetModuleHandle16( "KERNEL" );
125     if ( hModule )
126     {
127         WORD cs, ds;
128
129         /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
130         NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
131
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 );
136
137         /* Initialize KERNEL.THHOOK */
138         TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
139                                   (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
140
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 ))
146
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
158     }
159
160     /* Initialize relay code */
161     if (!RELAY_Init()) return FALSE;
162
163     return TRUE;
164 }
165
166 /***********************************************************************
167  *           GDI initialisation routine
168  */
169 BOOL WINAPI MAIN_GdiInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
170 {
171     NE_MODULE *pModule;
172
173     if ( GDI_HeapSel ) return TRUE;
174
175     /* Create GDI heap */
176     pModule = NE_GetPtr( GetModuleHandle16( "GDI" ) );
177     if ( pModule )
178     {
179         GDI_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) + 
180                                           pModule->dgroup - 1)->hSeg );
181     }
182     else
183     {
184         GDI_HeapSel = GlobalAlloc16( GMEM_FIXED, GDI_HEAP_SIZE );
185         LocalInit16( GDI_HeapSel, 0, GDI_HEAP_SIZE-1 );
186     }
187
188     if (!TWEAK_Init()) return FALSE;
189
190     /* GDI initialisation */
191     if(!GDI_Init()) return FALSE;
192
193     /* Create the Win16 printer driver */
194     if (!WIN16DRV_Init()) return FALSE;
195
196     /* PSDRV initialization */
197     if(!PSDRV_Init()) return FALSE;
198
199     return TRUE;
200 }
201
202 /***********************************************************************
203  *           USER initialisation routine
204  */
205 BOOL WINAPI MAIN_UserInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
206 {
207     NE_MODULE *pModule;
208     int queueSize;
209
210     if ( USER_HeapSel ) return TRUE;
211
212     /* Create USER heap */
213     pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
214     if ( pModule )
215     {
216         USER_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) + 
217                                            pModule->dgroup - 1)->hSeg );
218     }
219     else
220     {
221         USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 0x10000 );
222         LocalInit16( USER_HeapSel, 0, 0xffff );
223     }
224
225      /* Global atom table initialisation */
226     if (!ATOM_Init( USER_HeapSel )) return FALSE;
227
228     /* Initialize window handling (critical section) */
229     WIN_Init();
230
231     /* Initialize system colors and metrics*/
232     SYSMETRICS_Init();
233     SYSCOLOR_Init();
234
235     /* Create the DCEs */
236     DCE_Init();
237
238     /* Initialize timers */
239     if (!TIMER_Init()) return FALSE;
240     
241     /* Initialize window procedures */
242     if (!WINPROC_Init()) return FALSE;
243
244     /* Initialize cursor/icons */
245     CURSORICON_Init();
246
247     /* Initialize built-in window classes */
248     if (!WIDGETS_Init()) return FALSE;
249
250     /* Initialize dialog manager */
251     if (!DIALOG_Init()) return FALSE;
252
253     /* Initialize menus */
254     if (!MENU_Init()) return FALSE;
255
256     /* Initialize message spying */
257     if (!SPY_Init()) return FALSE;
258
259     /* Check wine.conf for old/bad entries */
260     if (!TWEAK_CheckConfiguration()) return FALSE;
261
262     /* Create system message queue */
263     queueSize = GetProfileIntA( "windows", "TypeAhead", 120 );
264     if (!QUEUE_CreateSysMsgQueue( queueSize )) return FALSE;
265
266     /* Set double click time */
267     SetDoubleClickTime( GetProfileIntA("windows","DoubleClickSpeed",452) );
268
269     /* Create message queue of initial thread */
270     InitThreadInput16( 0, 0 );
271
272     /* Create desktop window */
273     if (!WIN_CreateDesktopWindow()) return FALSE;
274
275     /* Initialize keyboard driver */
276     KEYBOARD_Enable( keybd_event, InputKeyStateTable );
277
278     /* Initialize mouse driver */
279     MOUSE_Enable( mouse_event );
280
281     /* Start processing X events */
282     UserRepaintDisable16( FALSE );
283
284     return TRUE;
285 }
286
287
288 /***********************************************************************
289  *           Winelib initialisation routine
290  */
291 HINSTANCE MAIN_WinelibInit( int *argc, char *argv[] )
292 {
293     WINE_MODREF *wm;
294     NE_MODULE *pModule;
295     OFSTRUCT ofs;
296     HMODULE16 hModule;
297
298     /* Main initialization */
299     if (!MAIN_MainInit( argc, argv )) return 0;
300
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;
305
306     strcpy( ofs.szPathName, wm->modname );
307     if ((hModule = MODULE_CreateDummyModule( &ofs, NULL )) < 32) return 0;
308     pModule = (NE_MODULE *)GlobalLock16( hModule );
309     pModule->flags = NE_FFLAGS_WIN32;
310     pModule->module32 = wm->module;
311
312     if (!TASK_Create( pModule, FALSE )) return 0;
313
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 );
318
319     /* Get pointers to USER routines called by KERNEL */
320     THUNK_InitCallout();
321
322     return wm->module;
323 }
324
325 /***********************************************************************
326  *           ExitKernel16 (KERNEL.2)
327  *
328  * Clean-up everything and exit the Wine process.
329  *
330  */
331 void WINAPI ExitKernel16( void )
332 {
333     /* Do the clean-up stuff */
334
335     WriteOutProfiles16();
336     SHELL_SaveRegistry();
337
338     TerminateProcess( GetCurrentProcess(), 0 );
339 }
340