Implemented new Wine startup sequence, separating startup into
[wine] / miscemu / main.c
1 /*
2  * Emulator initialisation code
3  *
4  */
5
6 #include <assert.h>
7 #include "callback.h"
8 #include "debug.h"
9 #include "debugger.h"
10 #include "main.h"
11 #include "miscemu.h"
12 #include "module.h"
13 #include "options.h"
14 #include "process.h"
15 #include "win16drv.h"
16 #include "psdrv.h"
17 #include "thread.h"
18 #include "windows.h"
19
20
21 /***********************************************************************
22  *           Emulator initialisation
23  */
24 BOOL32 MAIN_EmulatorInit(void)
25 {
26     /* Main initialization */
27     if (!MAIN_MainInit()) return FALSE;
28
29     /* Initialize relay code */
30     if (!RELAY_Init()) return FALSE;
31
32     /* Initialize signal handling */
33     if (!SIGNAL_InitEmulator()) return FALSE;
34
35     /* Create the Win16 printer driver */
36     if (!WIN16DRV_Init()) return FALSE;
37
38     /* Create the Postscript printer driver (FIXME: should be in Winelib) */
39     if (!PSDRV_Init()) return FALSE;
40
41     /* Load system DLLs into the initial process (and initialize them) */
42     if (!LoadLibrary16(  "KERNEL" )) return FALSE;         /* always built-in */
43     if (!LoadLibrary32A( "KERNEL32" )) return FALSE;       /* always built-in */
44
45     if (!LoadLibrary16(  "GDI.EXE" )) return FALSE;
46     if (!LoadLibrary32A( "GDI32.DLL" )) return FALSE;
47
48     if (!LoadLibrary16(  "USER.EXE" )) return FALSE;
49     if (!LoadLibrary32A( "USER32.DLL" )) return FALSE;
50     
51     return TRUE;
52 }
53
54
55 /***********************************************************************
56  *           Main loop of initial task
57  */
58 void MAIN_EmulatorRun( void )
59 {
60     BOOL32 (*WINAPI pGetMessage)(MSG32* lpmsg,HWND32 hwnd,UINT32 min,UINT32 max);
61     BOOL32 (*WINAPI pTranslateMessage)( const MSG32* msg );
62     LONG   (*WINAPI pDispatchMessage)( const MSG32* msg );
63     MSG32 msg;
64
65     HMODULE32 hModule = GetModuleHandle32A( "USER32" );
66     pGetMessage       = GetProcAddress32( hModule, "GetMessageA" ); 
67     pTranslateMessage = GetProcAddress32( hModule, "TranslateMessage" ); 
68     pDispatchMessage  = GetProcAddress32( hModule, "DispatchMessageA" ); 
69
70     assert( pGetMessage );
71     assert( pTranslateMessage );
72     assert( pDispatchMessage );
73
74     while ( GetNumTasks() > 1 && pGetMessage( &msg, 0, 0, 0 ) )
75     {
76         pTranslateMessage( &msg );
77         pDispatchMessage( &msg );
78     }
79
80     ExitProcess( 0 );
81 }
82
83
84 /**********************************************************************
85  *           main
86  */
87 int main( int argc, char *argv[] )
88 {
89     extern char * DEBUG_argv0;
90     char startProg[256], defProg[256];
91     int i,loaded;
92     HINSTANCE32 handle;
93
94     __winelib = 0;  /* First of all, clear the Winelib flag */
95
96     /*
97      * Save this so that the internal debugger can get a hold of it if
98      * it needs to.
99      */
100     DEBUG_argv0 = argv[0];
101
102     /* Create the initial process */
103     if (!PROCESS_Init()) return FALSE;
104
105     /* Parse command-line */
106     if (!MAIN_WineInit( &argc, argv )) return 1;
107
108     /* Handle -dll option (hack) */
109
110     if (Options.dllFlags)
111     {
112         if (!BUILTIN_ParseDLLOptions( Options.dllFlags ))
113         {
114             MSG("%s: Syntax: -dll +xxx,... or -dll -xxx,...\n",
115                      argv[0] );
116             BUILTIN_PrintDLLs();
117             exit(1);
118         }
119     }
120
121     /* Initialize everything */
122
123     if (!MAIN_EmulatorInit()) return 1;
124
125     /* Initialize CALL32 routines */
126     /* This needs to be done just before task-switching starts */
127
128     loaded=0;
129     
130     /* Add the Default Program if no program on the command line */
131     if (!argv[1])
132     {
133         PROFILE_GetWineIniString( "programs", "Default", "",
134                                   defProg, sizeof(defProg) );
135         if (defProg[0]) argv[argc++] = defProg;
136     }
137     
138     /* Add the Startup Program to the run list */
139     PROFILE_GetWineIniString( "programs", "Startup", "", 
140                                startProg, sizeof(startProg) );
141     if (startProg[0]) argv[argc++] = startProg;
142
143     for (i = 1; i < argc; i++)
144     {
145         if ((handle = WinExec32( argv[i], SW_SHOWNORMAL )) < 32)
146         {
147             MSG("wine: can't exec '%s': ", argv[i]);
148             switch (handle)
149             {
150             case 2: MSG("file not found\n" ); break;
151             case 11: MSG("invalid exe file\n" ); break;
152             case 21: MSG("win32 executable\n" ); break; /* FIXME: Obsolete? */
153             default: MSG("error=%d\n", handle ); break;
154             }
155             return 1;
156         }
157         loaded++;
158     }
159
160     if (!loaded) { /* nothing loaded */
161         MAIN_Usage(argv[0]);
162         return 1;
163     }
164
165     if (!GetNumTasks())
166     {
167         MSG("wine: no executable file found.\n" );
168         return 0;
169     }
170
171     if (Options.debug) DEBUG_AddModuleBreakpoints();
172
173     ctx_debug_call = ctx_debug;
174 #if 0  /* FIXME!! */
175     IF1632_CallLargeStack = (int (*)(int (*func)(), void *arg))CALL32_Init();
176 #endif
177     MAIN_EmulatorRun();
178     MSG("WinMain: Should never happen: returned from Yield16()\n" );
179     return 0;
180 }