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