Fix warnings in miscemu/main.c.
[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 "thread.h"
17 #include "task.h"
18 #include "stackframe.h"
19 #include "windows.h"
20
21 static int MAIN_argc;
22 static char **MAIN_argv;
23
24 /***********************************************************************
25  *           Emulator initialisation
26  */
27 BOOL32 MAIN_EmulatorInit(void)
28 {
29     /* Main initialization */
30     if (!MAIN_MainInit()) return FALSE;
31
32     /* Initialize relay code */
33     if (!RELAY_Init()) return FALSE;
34
35     /* Initialize signal handling */
36     if (!SIGNAL_InitEmulator()) return FALSE;
37
38     /* Create the Win16 printer driver */
39     if (!WIN16DRV_Init()) return FALSE;
40
41     return TRUE;
42 }
43
44 typedef BOOL32 (*WINAPI tGetMessage)(MSG32* lpmsg,HWND32 hwnd,UINT32 min,UINT32 max);
45 typedef BOOL32 (*WINAPI tTranslateMessage)(const MSG32* msg);
46 typedef LONG   (*WINAPI tDispatchMessage)( const MSG32* msg );
47
48 /***********************************************************************
49  *           Main loop of initial task
50  */
51 void MAIN_EmulatorRun( void )
52 {
53     char startProg[256], defProg[256];
54     HINSTANCE32 handle;
55     int i;
56
57     tGetMessage         pGetMessage;
58     tTranslateMessage   pTranslateMessage;
59     tDispatchMessage    pDispatchMessage;
60     HMODULE32           hModule;
61     MSG32               msg;
62
63     /* Load system DLLs into the initial process (and initialize them) */
64     if (   !LoadLibrary16("GDI.EXE" ) || !LoadLibrary32A("GDI32.DLL" )
65         || !LoadLibrary16("USER.EXE") || !LoadLibrary32A("USER32.DLL"))
66         ExitProcess( 1 );
67
68     /* Add the Default Program if no program on the command line */
69     if (!MAIN_argv[1])
70     {
71         PROFILE_GetWineIniString( "programs", "Default", "",
72                                   defProg, sizeof(defProg) );
73         if (defProg[0]) MAIN_argv[MAIN_argc++] = defProg;
74     }
75     
76     /* Add the Startup Program to the run list */
77     PROFILE_GetWineIniString( "programs", "Startup", "", 
78                                startProg, sizeof(startProg) );
79     if (startProg[0]) MAIN_argv[MAIN_argc++] = startProg;
80
81     /* Abort if no executable on command line */
82     if (MAIN_argc <= 1) 
83     {
84         MAIN_Usage(MAIN_argv[0]);
85         exit(1);
86     }
87
88     /* Load and run executables given on command line */
89     for (i = 1; i < MAIN_argc; i++)
90         if ((handle = WinExec32( MAIN_argv[i], SW_SHOWNORMAL )) < 32)
91         {
92             MSG("wine: can't exec '%s': ", MAIN_argv[i]);
93             switch (handle)
94             {
95             case 2: MSG("file not found\n" ); break;
96             case 11: MSG("invalid exe file\n" ); break;
97             default: MSG("error=%d\n", handle ); break;
98             }
99         }
100
101     if (GetNumTasks() <= 1)
102     {
103         MSG("wine: no executable file found.\n" );
104         ExitProcess( 0 );
105     }
106
107
108     /* Start message loop for desktop window */
109
110     hModule = GetModuleHandle32A( "USER32" );
111     pGetMessage       = (tGetMessage)GetProcAddress32( hModule, "GetMessageA" ); 
112     pTranslateMessage = (tTranslateMessage)GetProcAddress32( hModule, "TranslateMessage" ); 
113     pDispatchMessage  = (tDispatchMessage)GetProcAddress32( hModule, "DispatchMessageA" ); 
114
115     assert( pGetMessage );
116     assert( pTranslateMessage );
117     assert( pDispatchMessage );
118
119     while ( GetNumTasks() > 1 && pGetMessage( &msg, 0, 0, 0 ) )
120     {
121         pTranslateMessage( &msg );
122         pDispatchMessage( &msg );
123     }
124
125     ExitProcess( 0 );
126 }
127
128
129 /**********************************************************************
130  *           main
131  */
132 int main( int argc, char *argv[] )
133 {
134     NE_MODULE *pModule;
135     HINSTANCE16 hInstance;
136     extern char * DEBUG_argv0;
137
138     __winelib = 0;  /* First of all, clear the Winelib flag */
139
140     /*
141      * Save this so that the internal debugger can get a hold of it if
142      * it needs to.
143      */
144     DEBUG_argv0 = argv[0];
145
146     /* Create the initial process */
147     if (!PROCESS_Init()) return FALSE;
148
149     /* Parse command-line */
150     if (!MAIN_WineInit( &argc, argv )) return 1;
151     MAIN_argc = argc; MAIN_argv = argv;
152
153     /* Handle -dll option (hack) */
154     if (Options.dllFlags)
155     {
156         if (!BUILTIN_ParseDLLOptions( Options.dllFlags ))
157         {
158             MSG("%s: Syntax: -dll +xxx,... or -dll -xxx,...\n",
159                      argv[0] );
160             BUILTIN_PrintDLLs();
161             exit(1);
162         }
163     }
164
165     /* Set up debugger callback routines */
166     ctx_debug_call = ctx_debug;
167     if (Options.debug) 
168         TASK_AddTaskEntryBreakpoint = DEBUG_AddTaskEntryBreakpoint;
169
170     /* Initialize everything */
171     if (!MAIN_EmulatorInit()) return 1;
172
173     /* Load kernel modules */
174     if (!LoadLibrary16(  "KERNEL" )) return 1;
175     if (!LoadLibrary32A( "KERNEL32" )) return 1;
176
177     /* Create initial task */
178     if ( !(pModule = NE_GetPtr( GetModuleHandle16( "KERNEL32" ) )) ) return 1;
179     hInstance = NE_CreateInstance( pModule, NULL, TRUE );
180     PROCESS_Current()->task = TASK_Create( THREAD_Current(), pModule, hInstance, 0, FALSE );
181
182     /* Initialize CALL32 routines */
183     /* This needs to be done just before switching stacks */
184     IF1632_CallLargeStack = (int (*)(int (*func)(), void *arg))CALL32_Init();
185
186     /* Switch to initial task */
187     CURRENT_STACK16->frame32->retaddr = (DWORD)MAIN_EmulatorRun;
188     TASK_StartTask( PROCESS_Current()->task );
189     MSG( "main: Should never happen: returned from TASK_StartTask()\n" );
190     return 0;
191 }
192