Use Callout table instead of direct GetProcAddress.
[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
45 /***********************************************************************
46  *           Main loop of initial task
47  */
48 void MAIN_EmulatorRun( void )
49 {
50     extern void THUNK_InitCallout( void );
51     char startProg[256], defProg[256];
52     HINSTANCE32 handle;
53     int i;
54     MSG32 msg;
55
56     /* Load system DLLs into the initial process (and initialize them) */
57     if (   !LoadLibrary16("GDI.EXE" ) || !LoadLibrary32A("GDI32.DLL" )
58         || !LoadLibrary16("USER.EXE") || !LoadLibrary32A("USER32.DLL"))
59         ExitProcess( 1 );
60
61     /* Get pointers to USER routines called by KERNEL */
62     THUNK_InitCallout();
63
64     /* Add the Default Program if no program on the command line */
65     if (!MAIN_argv[1])
66     {
67         PROFILE_GetWineIniString( "programs", "Default", "",
68                                   defProg, sizeof(defProg) );
69         if (defProg[0]) MAIN_argv[MAIN_argc++] = defProg;
70     }
71     
72     /* Add the Startup Program to the run list */
73     PROFILE_GetWineIniString( "programs", "Startup", "", 
74                                startProg, sizeof(startProg) );
75     if (startProg[0]) MAIN_argv[MAIN_argc++] = startProg;
76
77     /* Abort if no executable on command line */
78     if (MAIN_argc <= 1) 
79     {
80         MAIN_Usage(MAIN_argv[0]);
81         exit(1);
82     }
83
84     /* Load and run executables given on command line */
85     for (i = 1; i < MAIN_argc; i++)
86         if ((handle = WinExec32( MAIN_argv[i], SW_SHOWNORMAL )) < 32)
87         {
88             MSG("wine: can't exec '%s': ", MAIN_argv[i]);
89             switch (handle)
90             {
91             case 2: MSG("file not found\n" ); break;
92             case 11: MSG("invalid exe file\n" ); break;
93             default: MSG("error=%d\n", handle ); break;
94             }
95         }
96
97     if (GetNumTasks() <= 1)
98     {
99         MSG("wine: no executable file found.\n" );
100         ExitProcess( 0 );
101     }
102
103
104     /* Start message loop for desktop window */
105
106     while ( GetNumTasks() > 1 && Callout.GetMessage32A( &msg, 0, 0, 0 ) )
107     {
108         Callout.TranslateMessage32( &msg );
109         Callout.DispatchMessage32A( &msg );
110     }
111
112     ExitProcess( 0 );
113 }
114
115
116 /**********************************************************************
117  *           main
118  */
119 int main( int argc, char *argv[] )
120 {
121     NE_MODULE *pModule;
122     HINSTANCE16 hInstance;
123     extern char * DEBUG_argv0;
124
125     __winelib = 0;  /* First of all, clear the Winelib flag */
126
127     /*
128      * Save this so that the internal debugger can get a hold of it if
129      * it needs to.
130      */
131     DEBUG_argv0 = argv[0];
132
133     /* Create the initial process */
134     if (!PROCESS_Init()) return FALSE;
135
136     /* Parse command-line */
137     if (!MAIN_WineInit( &argc, argv )) return 1;
138     MAIN_argc = argc; MAIN_argv = argv;
139
140     /* Handle -dll option (hack) */
141     if (Options.dllFlags)
142     {
143         if (!BUILTIN_ParseDLLOptions( Options.dllFlags ))
144         {
145             MSG("%s: Syntax: -dll +xxx,... or -dll -xxx,...\n",
146                      argv[0] );
147             BUILTIN_PrintDLLs();
148             exit(1);
149         }
150     }
151
152     /* Set up debugger callback routines */
153     ctx_debug_call = ctx_debug;
154     if (Options.debug) 
155         TASK_AddTaskEntryBreakpoint = DEBUG_AddTaskEntryBreakpoint;
156
157     /* Initialize everything */
158     if (!MAIN_EmulatorInit()) return 1;
159
160     /* Load kernel modules */
161     if (!LoadLibrary16(  "KERNEL" )) return 1;
162     if (!LoadLibrary32A( "KERNEL32" )) return 1;
163
164     /* Create initial task */
165     if ( !(pModule = NE_GetPtr( GetModuleHandle16( "KERNEL32" ) )) ) return 1;
166     hInstance = NE_CreateInstance( pModule, NULL, TRUE );
167     PROCESS_Current()->task = TASK_Create( THREAD_Current(), pModule, hInstance, 0, FALSE );
168
169     /* Initialize CALL32 routines */
170     /* This needs to be done just before switching stacks */
171     IF1632_CallLargeStack = (int (*)(int (*func)(), void *arg))CALL32_Init();
172
173     /* Switch to initial task */
174     CURRENT_STACK16->frame32->retaddr = (DWORD)MAIN_EmulatorRun;
175     TASK_StartTask( PROCESS_Current()->task );
176     MSG( "main: Should never happen: returned from TASK_StartTask()\n" );
177     return 0;
178 }
179