Partial stub implementation of undocumented KERNEL routine PK16FNF,
[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 "bitmap.h"
14 #include "comm.h"
15 #include "neexe.h"
16 #include "win.h"
17 #include "main.h"
18 #include "menu.h"
19 #include "message.h"
20 #include "multimedia.h"
21 #include "dialog.h"
22 #include "drive.h"
23 #include "queue.h"
24 #include "sysmetrics.h"
25 #include "file.h"
26 #include "gdi.h"
27 #include "heap.h"
28 #include "keyboard.h"
29 #include "mouse.h"
30 #include "input.h"
31 #include "miscemu.h"
32 #include "options.h"
33 #include "process.h"
34 #include "spy.h"
35 #include "tweak.h"
36 #include "user.h"
37 #include "global.h"
38 #include "dce.h"
39 #include "shell.h"
40 #include "winproc.h"
41 #include "syslevel.h"
42 #include "thread.h"
43 #include "task.h"
44 #include "debug.h"
45 #include "psdrv.h"
46 #include "server.h"
47
48 int __winelib = 1;  /* Winelib run-time flag */
49
50 /***********************************************************************
51  *           Main initialisation routine
52  */
53 BOOL32 MAIN_MainInit(void)
54 {
55     /* Set server debug level */
56     /* To fool make_debug: TRACE(server) */
57     CLIENT_SetDebug( TRACE_ON(server) );
58
59     /* Initialize syslevel handling */
60     SYSLEVEL_Init();
61
62     /* Initialize signal handling */
63     if (!SIGNAL_Init()) return FALSE;
64
65     /* Load the configuration file */
66     if (!PROFILE_LoadWineIni()) return FALSE;
67
68       /* Initialize DOS memory */
69     if (!DOSMEM_Init(0)) return FALSE;
70
71     /* Initialise DOS drives */
72     if (!DRIVE_Init()) return FALSE;
73
74     /* Initialise DOS directories */
75     if (!DIR_Init()) return FALSE;
76
77       /* Initialize event handling */
78     if (!EVENT_Init()) return FALSE;
79
80     /* Initialize communications */
81     COMM_Init();
82
83     /* Initialize IO-port permissions */
84     IO_port_init();
85
86     /* registry initialisation */
87     SHELL_LoadRegistry();
88     
89     /* Read DOS config.sys */
90     if (!DOSCONF_ReadConfig()) return FALSE;
91
92     return TRUE;
93 }
94
95 /***********************************************************************
96  *           KERNEL initialisation routine
97  */
98 BOOL32 WINAPI MAIN_KernelInit(HINSTANCE32 hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
99 {
100     static BOOL32 initDone = FALSE;
101
102     HMODULE16 hModule;
103
104     if ( initDone ) return TRUE;
105     initDone = TRUE;
106
107     /* Initialize special KERNEL entry points */
108     hModule = GetModuleHandle16( "KERNEL" );
109     if ( hModule )
110     {
111         WORD cs, ds;
112
113         /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
114         NE_SetEntryPoint( hModule, 178, GetWinFlags() );
115
116         /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
117         GET_CS(cs); GET_DS(ds);
118         NE_SetEntryPoint( hModule, 454, cs );
119         NE_SetEntryPoint( hModule, 455, ds );
120
121         /* Initialize KERNEL.THHOOK */
122         TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
123                                   (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
124
125         /* Initialize the real-mode selector entry points */
126 #define SET_ENTRY_POINT( num, addr ) \
127         NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
128                           DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
129                           FALSE, FALSE, FALSE, NULL ))
130
131         SET_ENTRY_POINT( 183, 0x00000 );  /* KERNEL.183: __0000H */
132         SET_ENTRY_POINT( 174, 0xa0000 );  /* KERNEL.174: __A000H */
133         SET_ENTRY_POINT( 181, 0xb0000 );  /* KERNEL.181: __B000H */
134         SET_ENTRY_POINT( 182, 0xb8000 );  /* KERNEL.182: __B800H */
135         SET_ENTRY_POINT( 195, 0xc0000 );  /* KERNEL.195: __C000H */
136         SET_ENTRY_POINT( 179, 0xd0000 );  /* KERNEL.179: __D000H */
137         SET_ENTRY_POINT( 190, 0xe0000 );  /* KERNEL.190: __E000H */
138         NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg );  /* KERNEL.173: __ROMBIOS */
139         NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
140         NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg );  /* KERNEL.194: __F000H */
141 #undef SET_ENTRY_POINT
142     }
143
144     return TRUE;
145 }
146
147 /***********************************************************************
148  *           GDI initialisation routine
149  */
150 BOOL32 WINAPI MAIN_GdiInit(HINSTANCE32 hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
151 {
152     NE_MODULE *pModule;
153
154     if ( GDI_HeapSel ) return TRUE;
155
156     /* Create GDI heap */
157     pModule = NE_GetPtr( GetModuleHandle16( "GDI" ) );
158     if ( pModule )
159     {
160         GDI_HeapSel = GlobalHandleToSel( (NE_SEG_TABLE( pModule ) + 
161                                           pModule->dgroup - 1)->hSeg );
162     }
163     else
164     {
165         GDI_HeapSel = GlobalAlloc16( GMEM_FIXED, GDI_HEAP_SIZE );
166         LocalInit( GDI_HeapSel, 0, GDI_HEAP_SIZE-1 );
167     }
168
169     if (!TWEAK_Init()) return FALSE;
170
171     /* GDI initialisation */
172     if(!GDI_Init()) return FALSE;
173
174
175     /* PSDRV initialization */
176     if(!PSDRV_Init()) return FALSE;
177
178     return TRUE;
179 }
180
181 /***********************************************************************
182  *           USER initialisation routine
183  */
184 BOOL32 WINAPI MAIN_UserInit(HINSTANCE32 hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
185 {
186     NE_MODULE *pModule;
187     int queueSize;
188
189     if ( USER_HeapSel ) return TRUE;
190
191     /* Create USER heap */
192     pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
193     if ( pModule )
194     {
195         USER_HeapSel = GlobalHandleToSel( (NE_SEG_TABLE( pModule ) + 
196                                            pModule->dgroup - 1)->hSeg );
197     }
198     else
199     {
200         USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 0x10000 );
201         LocalInit( USER_HeapSel, 0, 0xffff );
202     }
203
204      /* Global atom table initialisation */
205     if (!ATOM_Init( USER_HeapSel )) return FALSE;
206
207     /* Initialize system colors and metrics*/
208     SYSMETRICS_Init();
209     SYSCOLOR_Init();
210
211     /* Create the DCEs */
212     DCE_Init();
213
214     /* Initialize window procedures */
215     if (!WINPROC_Init()) return FALSE;
216
217     /* Initialize built-in window classes */
218     if (!WIDGETS_Init()) return FALSE;
219
220     /* Initialize dialog manager */
221     if (!DIALOG_Init()) return FALSE;
222
223     /* Initialize menus */
224     if (!MENU_Init()) return FALSE;
225
226     /* Initialize multimedia */
227     if (!MULTIMEDIA_Init()) return FALSE;
228
229     /* Initialize message spying */
230     if (!SPY_Init()) return FALSE;
231
232     /* Check wine.conf for old/bad entries */
233     if (!TWEAK_CheckConfiguration()) return FALSE;
234
235     /* Create system message queue */
236     queueSize = GetProfileInt32A( "windows", "TypeAhead", 120 );
237     if (!QUEUE_CreateSysMsgQueue( queueSize )) return FALSE;
238
239     /* Set double click time */
240     SetDoubleClickTime32( GetProfileInt32A("windows","DoubleClickSpeed",452) );
241
242     /* Create task message queue for the initial task */
243     queueSize = GetProfileInt32A( "windows", "DefaultQueueSize", 8 );
244     if (!SetMessageQueue32( queueSize )) return FALSE;
245
246     /* Create desktop window */
247     if (!WIN_CreateDesktopWindow()) return FALSE;
248
249     /* Install default USER Signal Handler */
250     SetTaskSignalProc( 0, (FARPROC16)USER_SignalProc );
251
252     /* Initialize keyboard driver */
253     KEYBOARD_Enable( keybd_event, InputKeyStateTable );
254
255     /* Initialize mouse driver */
256     MOUSE_Enable( mouse_event );
257
258     return TRUE;
259 }
260
261
262 /***********************************************************************
263  *           Winelib initialisation routine
264  */
265 HINSTANCE32 MAIN_WinelibInit( int *argc, char *argv[] )
266 {
267     WINE_MODREF *wm;
268     NE_MODULE *pModule;
269     OFSTRUCT ofs;
270     HMODULE16 hModule;
271     HINSTANCE16 hInstance;
272
273     /* Create the initial process */
274     if (!PROCESS_Init()) return 0;
275
276     /* Parse command line arguments */
277     MAIN_WineInit( argc, argv );
278
279     /* Main initialization */
280     if (!MAIN_MainInit()) return 0;
281
282     /* Initialize KERNEL */
283     if (!MAIN_KernelInit(0, 0, NULL)) return 0;
284
285     /* Create and switch to initial task */
286     if (!(wm = ELF_CreateDummyModule( argv[0], argv[0], PROCESS_Current() )))
287         return 0;
288     PROCESS_Current()->exe_modref = wm;
289
290     strcpy( ofs.szPathName, wm->modname );
291     if ((hModule = MODULE_CreateDummyModule( &ofs, NULL )) < 32) return 0;
292     pModule = (NE_MODULE *)GlobalLock16( hModule );
293     pModule->flags = NE_FFLAGS_WIN32;
294     pModule->module32 = wm->module;
295
296     hInstance = NE_CreateInstance( pModule, NULL, TRUE );
297     PROCESS_Current()->task = TASK_Create( THREAD_Current(), pModule, hInstance, 0, FALSE );
298
299     TASK_StartTask( PROCESS_Current()->task );
300
301     /* Initialize GDI and USER */
302     if (!MAIN_GdiInit(0, 0, NULL)) return 0;
303     if (!MAIN_UserInit(0, 0, NULL)) return 0;
304
305     return wm->module;
306 }
307