Fixes error handling (SetLastError() and return value).
[wine/multimedia.git] / miscemu / main.c
blobe737976f7cba9c4351a0925aa606fdec27ce16f1
1 /*
2 * Emulator initialisation code
4 */
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 "task.h"
19 #include "stackframe.h"
20 #include "windows.h"
22 static int MAIN_argc;
23 static char **MAIN_argv;
25 /***********************************************************************
26 * Emulator initialisation
28 BOOL32 MAIN_EmulatorInit(void)
30 /* Main initialization */
31 if (!MAIN_MainInit()) return FALSE;
33 /* Initialize relay code */
34 if (!RELAY_Init()) return FALSE;
36 /* Initialize signal handling */
37 if (!SIGNAL_InitEmulator()) return FALSE;
39 /* Create the Win16 printer driver */
40 if (!WIN16DRV_Init()) return FALSE;
42 /* Create the Postscript printer driver (FIXME: should be in Winelib) */
43 if (!PSDRV_Init()) return FALSE;
45 return TRUE;
48 /***********************************************************************
49 * Main loop of initial task
51 void MAIN_EmulatorRun( void )
53 char startProg[256], defProg[256];
54 HINSTANCE32 handle;
55 int i;
57 BOOL32 (*WINAPI pGetMessage)(MSG32* lpmsg,HWND32 hwnd,UINT32 min,UINT32 max);
58 BOOL32 (*WINAPI pTranslateMessage)( const MSG32* msg );
59 LONG (*WINAPI pDispatchMessage)( const MSG32* msg );
60 HMODULE32 hModule;
61 MSG32 msg;
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 );
68 /* Add the Default Program if no program on the command line */
69 if (!MAIN_argv[1])
71 PROFILE_GetWineIniString( "programs", "Default", "",
72 defProg, sizeof(defProg) );
73 if (defProg[0]) MAIN_argv[MAIN_argc++] = defProg;
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;
81 /* Abort if no executable on command line */
82 if (MAIN_argc <= 1)
84 MAIN_Usage(MAIN_argv[0]);
85 exit(1);
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)
92 MSG("wine: can't exec '%s': ", MAIN_argv[i]);
93 switch (handle)
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;
101 if (GetNumTasks() <= 1)
103 MSG("wine: no executable file found.\n" );
104 ExitProcess( 0 );
108 /* Start message loop for desktop window */
110 hModule = GetModuleHandle32A( "USER32" );
111 pGetMessage = GetProcAddress32( hModule, "GetMessageA" );
112 pTranslateMessage = GetProcAddress32( hModule, "TranslateMessage" );
113 pDispatchMessage = GetProcAddress32( hModule, "DispatchMessageA" );
115 assert( pGetMessage );
116 assert( pTranslateMessage );
117 assert( pDispatchMessage );
119 while ( GetNumTasks() > 1 && pGetMessage( &msg, 0, 0, 0 ) )
121 pTranslateMessage( &msg );
122 pDispatchMessage( &msg );
125 ExitProcess( 0 );
129 /**********************************************************************
130 * main
132 int main( int argc, char *argv[] )
134 NE_MODULE *pModule;
135 HINSTANCE16 hInstance;
136 extern char * DEBUG_argv0;
138 __winelib = 0; /* First of all, clear the Winelib flag */
141 * Save this so that the internal debugger can get a hold of it if
142 * it needs to.
144 DEBUG_argv0 = argv[0];
146 /* Create the initial process */
147 if (!PROCESS_Init()) return FALSE;
149 /* Parse command-line */
150 if (!MAIN_WineInit( &argc, argv )) return 1;
151 MAIN_argc = argc; MAIN_argv = argv;
153 /* Handle -dll option (hack) */
154 if (Options.dllFlags)
156 if (!BUILTIN_ParseDLLOptions( Options.dllFlags ))
158 MSG("%s: Syntax: -dll +xxx,... or -dll -xxx,...\n",
159 argv[0] );
160 BUILTIN_PrintDLLs();
161 exit(1);
165 /* Set up debugger callback routines */
166 ctx_debug_call = ctx_debug;
167 if (Options.debug)
168 TASK_AddTaskEntryBreakpoint = DEBUG_AddTaskEntryBreakpoint;
170 /* Initialize everything */
171 if (!MAIN_EmulatorInit()) return 1;
173 /* Load kernel modules */
174 if (!LoadLibrary16( "KERNEL" )) return 1;
175 if (!LoadLibrary32A( "KERNEL32" )) return 1;
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 );
182 /* Initialize CALL32 routines */
183 /* This needs to be done just before switching stacks */
184 IF1632_CallLargeStack = (int (*)(int (*func)(), void *arg))CALL32_Init();
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;