Unify startup sequence for Wine and WineLib.
[wine] / miscemu / main.c
1 /*
2  * Emulator initialisation code
3  *
4  */
5
6 #include <stdlib.h>
7 #include <assert.h>
8 #include "wine/winbase16.h"
9 #include "callback.h"
10 #include "debugger.h"
11 #include "main.h"
12 #include "miscemu.h"
13 #include "module.h"
14 #include "options.h"
15 #include "process.h"
16 #include "thread.h"
17 #include "task.h"
18 #include "stackframe.h"
19 #include "wine/exception.h"
20 #include "debugtools.h"
21
22 static int MAIN_argc;
23 static char **MAIN_argv;
24
25
26 /***********************************************************************
27  *           Main loop of initial task
28  */
29 void MAIN_EmulatorRun( void )
30 {
31     char startProg[256], defProg[256];
32     HINSTANCE handle;
33     int i, tasks = 0;
34     MSG msg;
35
36     /* Load system DLLs into the initial process (and initialize them) */
37     if (   !LoadLibrary16("GDI.EXE" ) || !LoadLibraryA("GDI32.DLL" )
38         || !LoadLibrary16("USER.EXE") || !LoadLibraryA("USER32.DLL"))
39         ExitProcess( 1 );
40
41     /* Get pointers to USER routines called by KERNEL */
42     THUNK_InitCallout();
43
44     /* Call InitApp for initial task */
45     Callout.InitApp16( MapHModuleLS( 0 ) );
46
47     /* Add the Default Program if no program on the command line */
48     if (!MAIN_argv[1])
49     {
50         PROFILE_GetWineIniString( "programs", "Default", "",
51                                   defProg, sizeof(defProg) );
52         if (defProg[0]) MAIN_argv[MAIN_argc++] = defProg;
53     }
54     
55     /* Add the Startup Program to the run list */
56     PROFILE_GetWineIniString( "programs", "Startup", "", 
57                                startProg, sizeof(startProg) );
58     if (startProg[0]) MAIN_argv[MAIN_argc++] = startProg;
59
60     /* Abort if no executable on command line */
61     if (MAIN_argc <= 1) 
62     {
63         MAIN_Usage(MAIN_argv[0]);
64         ExitProcess( 1 );
65     }
66
67     /* Load and run executables given on command line */
68     for (i = 1; i < MAIN_argc; i++)
69     {
70         if ((handle = WinExec( MAIN_argv[i], SW_SHOWNORMAL )) < 32)
71         {
72             MESSAGE("wine: can't exec '%s': ", MAIN_argv[i]);
73             switch (handle)
74             {
75             case 2: MESSAGE("file not found\n" ); break;
76             case 11: MESSAGE("invalid exe file\n" ); break;
77             default: MESSAGE("error=%d\n", handle ); break;
78             }
79         }
80         else tasks++;
81     }
82
83     if (!tasks)
84     {
85         MESSAGE("wine: no executable file found.\n" );
86         ExitProcess( 0 );
87     }
88
89     /* Start message loop for desktop window */
90
91     while ( GetNumTasks16() > 1  && Callout.GetMessageA( &msg, 0, 0, 0 ) )
92     {
93         Callout.TranslateMessage( &msg );
94         Callout.DispatchMessageA( &msg );
95     }
96
97     ExitProcess( 0 );
98 }
99
100
101 /**********************************************************************
102  *           main
103  */
104 int main( int argc, char *argv[] )
105 {
106     NE_MODULE *pModule;
107     extern char * DEBUG_argv0;
108
109     __winelib = 0;  /* First of all, clear the Winelib flag */
110
111     /*
112      * Save this so that the internal debugger can get a hold of it if
113      * it needs to.
114      */
115     DEBUG_argv0 = argv[0];
116
117     /* Create the initial process */
118     if (!PROCESS_Init()) return FALSE;
119
120     /* Parse command-line */
121     if (!MAIN_WineInit( &argc, argv )) return 1;
122     MAIN_argc = argc; MAIN_argv = argv;
123
124     /* Set up debugger hook */
125     EXC_SetDebugEventHook( wine_debugger );
126
127     if (Options.debug) 
128         TASK_AddTaskEntryBreakpoint = DEBUG_AddTaskEntryBreakpoint;
129
130     /* Initialize everything */
131     if (!MAIN_MainInit()) return 1;
132
133     /* Load kernel modules */
134     if (!LoadLibrary16(  "KERNEL" )) return 1;
135     if (!LoadLibraryA( "KERNEL32" )) return 1;
136
137     /* Create initial task */
138     if ( !(pModule = NE_GetPtr( GetModuleHandle16( "KERNEL" ) )) ) return 1;
139     if ( !TASK_Create( pModule, FALSE ) ) return 1;
140
141     /* Switch to initial task */
142     PostEvent16( PROCESS_Current()->task );
143     TASK_Reschedule();
144
145     /* Switch stacks and jump to MAIN_EmulatorRun */
146     CALL32_Init( &IF1632_CallLargeStack, MAIN_EmulatorRun, NtCurrentTeb()->stack_top );
147
148     MESSAGE( "main: Should never happen: returned from CALL32_Init()\n" );
149     return 0;
150 }