Merged THDB and TEB structures.
[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 "server.h"
50 #include "cursoricon.h"
51 #include "loadorder.h"
52
53 DEFAULT_DEBUG_CHANNEL(server)
54
55 int __winelib = 1;  /* Winelib run-time flag */
56
57 /***********************************************************************
58  *           Main initialisation routine
59  */
60 BOOL MAIN_MainInit(void)
61 {
62     /* Set server debug level */
63     CLIENT_SetDebug( TRACE_ON(server) );
64
65     /* Initialize syslevel handling */
66     SYSLEVEL_Init();
67
68     /* Initialize signal handling */
69     if (!SIGNAL_Init()) return FALSE;
70
71     /* Load the configuration file */
72     if (!PROFILE_LoadWineIni()) return FALSE;
73
74     /* Initialize module loadorder */
75     if (!MODULE_InitLoadOrder()) return FALSE;
76
77       /* Initialize DOS memory */
78     if (!DOSMEM_Init(0)) return FALSE;
79
80     /* Initialise DOS drives */
81     if (!DRIVE_Init()) return FALSE;
82
83     /* Initialise DOS directories */
84     if (!DIR_Init()) return FALSE;
85
86     /* Initialize event handling */
87     if (!EVENT_Init()) return FALSE;
88
89     /* Initialise WINSOCK handling */
90     if (!WINSOCK_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     return TRUE;
105 }
106
107 /***********************************************************************
108  *           KERNEL initialisation routine
109  */
110 BOOL WINAPI MAIN_KernelInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
111 {
112     static BOOL initDone = FALSE;
113
114     HMODULE16 hModule;
115
116     if ( initDone ) return TRUE;
117     initDone = TRUE;
118
119     /* Initialize special KERNEL entry points */
120     hModule = GetModuleHandle16( "KERNEL" );
121     if ( hModule )
122     {
123         WORD cs, ds;
124
125         /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
126         NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
127
128         /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
129         GET_CS(cs); GET_DS(ds);
130         NE_SetEntryPoint( hModule, 454, cs );
131         NE_SetEntryPoint( hModule, 455, ds );
132
133         /* Initialize KERNEL.THHOOK */
134         TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
135                                   (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
136
137         /* Initialize the real-mode selector entry points */
138 #define SET_ENTRY_POINT( num, addr ) \
139         NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
140                           DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
141                           FALSE, FALSE, FALSE, NULL ))
142
143         SET_ENTRY_POINT( 183, 0x00000 );  /* KERNEL.183: __0000H */
144         SET_ENTRY_POINT( 174, 0xa0000 );  /* KERNEL.174: __A000H */
145         SET_ENTRY_POINT( 181, 0xb0000 );  /* KERNEL.181: __B000H */
146         SET_ENTRY_POINT( 182, 0xb8000 );  /* KERNEL.182: __B800H */
147         SET_ENTRY_POINT( 195, 0xc0000 );  /* KERNEL.195: __C000H */
148         SET_ENTRY_POINT( 179, 0xd0000 );  /* KERNEL.179: __D000H */
149         SET_ENTRY_POINT( 190, 0xe0000 );  /* KERNEL.190: __E000H */
150         NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg );  /* KERNEL.173: __ROMBIOS */
151         NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
152         NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg );  /* KERNEL.194: __F000H */
153 #undef SET_ENTRY_POINT
154     }
155     return TRUE;
156 }
157
158 /***********************************************************************
159  *           GDI initialisation routine
160  */
161 BOOL WINAPI MAIN_GdiInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
162 {
163     NE_MODULE *pModule;
164
165     if ( GDI_HeapSel ) return TRUE;
166
167     /* Create GDI heap */
168     pModule = NE_GetPtr( GetModuleHandle16( "GDI" ) );
169     if ( pModule )
170     {
171         GDI_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) + 
172                                           pModule->dgroup - 1)->hSeg );
173     }
174     else
175     {
176         GDI_HeapSel = GlobalAlloc16( GMEM_FIXED, GDI_HEAP_SIZE );
177         LocalInit16( GDI_HeapSel, 0, GDI_HEAP_SIZE-1 );
178     }
179
180     if (!TWEAK_Init()) return FALSE;
181
182     /* GDI initialisation */
183     if(!GDI_Init()) return FALSE;
184
185
186     /* PSDRV initialization */
187     if(!PSDRV_Init()) return FALSE;
188
189     return TRUE;
190 }
191
192 /***********************************************************************
193  *           USER initialisation routine
194  */
195 BOOL WINAPI MAIN_UserInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
196 {
197     NE_MODULE *pModule;
198     int queueSize;
199
200     if ( USER_HeapSel ) return TRUE;
201
202     /* Create USER heap */
203     pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
204     if ( pModule )
205     {
206         USER_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) + 
207                                            pModule->dgroup - 1)->hSeg );
208     }
209     else
210     {
211         USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 0x10000 );
212         LocalInit16( USER_HeapSel, 0, 0xffff );
213     }
214
215      /* Global atom table initialisation */
216     if (!ATOM_Init( USER_HeapSel )) return FALSE;
217
218     /* Initialize window handling (critical section) */
219     WIN_Init();
220
221     /* Initialize system colors and metrics*/
222     SYSMETRICS_Init();
223     SYSCOLOR_Init();
224
225     /* Create the DCEs */
226     DCE_Init();
227
228     /* Initialize timers */
229     if (!TIMER_Init()) return FALSE;
230     
231     /* Initialize window procedures */
232     if (!WINPROC_Init()) return FALSE;
233
234     /* Initialize built-in window classes */
235     if (!WIDGETS_Init()) return FALSE;
236
237     /* Initialize dialog manager */
238     if (!DIALOG_Init()) return FALSE;
239
240     /* Initialize menus */
241     if (!MENU_Init()) return FALSE;
242
243     /* Initialize cursor/icons */
244     CURSORICON_Init();
245
246     /* Initialize message spying */
247     if (!SPY_Init()) return FALSE;
248
249     /* Check wine.conf for old/bad entries */
250     if (!TWEAK_CheckConfiguration()) return FALSE;
251
252     /* Create system message queue */
253     queueSize = GetProfileIntA( "windows", "TypeAhead", 120 );
254     if (!QUEUE_CreateSysMsgQueue( queueSize )) return FALSE;
255
256     /* Set double click time */
257     SetDoubleClickTime( GetProfileIntA("windows","DoubleClickSpeed",452) );
258
259     /* Create task message queue for the initial task */
260     queueSize = GetProfileIntA( "windows", "DefaultQueueSize", 8 );
261     if (!SetMessageQueue( queueSize )) return FALSE;
262
263     /* Create desktop window */
264     if (!WIN_CreateDesktopWindow()) return FALSE;
265
266     /* Initialize keyboard driver */
267     KEYBOARD_Enable( keybd_event, InputKeyStateTable );
268
269     /* Initialize mouse driver */
270     MOUSE_Enable( mouse_event );
271
272     /* Start processing X events */
273     UserRepaintDisable16( FALSE );
274
275     return TRUE;
276 }
277
278
279 /***********************************************************************
280  *           Winelib initialisation routine
281  */
282 HINSTANCE MAIN_WinelibInit( int *argc, char *argv[] )
283 {
284     WINE_MODREF *wm;
285     NE_MODULE *pModule;
286     OFSTRUCT ofs;
287     HMODULE16 hModule;
288
289     /* Create the initial process */
290     if (!PROCESS_Init()) return 0;
291
292     /* Parse command line arguments */
293     MAIN_WineInit( argc, argv );
294
295     /* Main initialization */
296     if (!MAIN_MainInit()) return 0;
297
298     /* Initialize KERNEL */
299     if (!LoadLibraryA( "KERNEL32" )) 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, 0, 0, FALSE )) return 0;
313
314     /* Initialize GDI and USER */
315     if (!LoadLibraryA( "GDI32.DLL" )) return 0;
316     if (!LoadLibraryA( "USER32.DLL" )) return 0;
317
318     return wm->module;
319 }
320
321 /***********************************************************************
322  *           ExitKernel16 (KERNEL.2)
323  *
324  * Clean-up everything and exit the Wine process.
325  *
326  */
327 void WINAPI ExitKernel16( void )
328 {
329     /* Do the clean-up stuff */
330
331     WriteOutProfiles16();
332     SHELL_SaveRegistry();
333
334     TerminateProcess( GetCurrentProcess(), 0 );
335 }
336