Added FinalUserInit16/FinalGdiInit16 stubs.
[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 FinalUserInit routine */
45     Callout.FinalUserInit16();
46
47     /* Call InitApp for initial task */
48     Callout.InitApp16( MapHModuleLS( 0 ) );
49
50     /* Add the Default Program if no program on the command line */
51     if (!MAIN_argv[1])
52     {
53         PROFILE_GetWineIniString( "programs", "Default", "",
54                                   defProg, sizeof(defProg) );
55         if (defProg[0]) MAIN_argv[MAIN_argc++] = defProg;
56     }
57     
58     /* Add the Startup Program to the run list */
59     PROFILE_GetWineIniString( "programs", "Startup", "", 
60                                startProg, sizeof(startProg) );
61     if (startProg[0]) MAIN_argv[MAIN_argc++] = startProg;
62
63     /* Abort if no executable on command line */
64     if (MAIN_argc <= 1) 
65     {
66         MAIN_Usage(MAIN_argv[0]);
67         ExitProcess( 1 );
68     }
69
70     /* Load and run executables given on command line */
71     for (i = 1; i < MAIN_argc; i++)
72     {
73         if ((handle = WinExec( MAIN_argv[i], SW_SHOWNORMAL )) < 32)
74         {
75             MESSAGE("wine: can't exec '%s': ", MAIN_argv[i]);
76             switch (handle)
77             {
78             case 2: MESSAGE("file not found\n" ); break;
79             case 11: MESSAGE("invalid exe file\n" ); break;
80             default: MESSAGE("error=%d\n", handle ); break;
81             }
82         }
83         else tasks++;
84     }
85
86     if (!tasks)
87     {
88         MESSAGE("wine: no executable file found.\n" );
89         ExitProcess( 0 );
90     }
91
92     /* Start message loop for desktop window */
93
94     while ( GetNumTasks16() > 1  && Callout.GetMessageA( &msg, 0, 0, 0 ) )
95     {
96         Callout.TranslateMessage( &msg );
97         Callout.DispatchMessageA( &msg );
98     }
99
100     ExitProcess( 0 );
101 }
102
103
104 /**********************************************************************
105  *           main
106  */
107 int main( int argc, char *argv[] )
108 {
109     NE_MODULE *pModule;
110     extern char * DEBUG_argv0;
111
112     __winelib = 0;  /* First of all, clear the Winelib flag */
113
114     /*
115      * Save this so that the internal debugger can get a hold of it if
116      * it needs to.
117      */
118     DEBUG_argv0 = argv[0];
119
120     /* Create the initial process */
121     if (!PROCESS_Init()) return FALSE;
122
123     /* Parse command-line */
124     if (!MAIN_WineInit( &argc, argv )) return 1;
125     MAIN_argc = argc; MAIN_argv = argv;
126
127     /* Set up debugger hook */
128     EXC_SetDebugEventHook( wine_debugger );
129
130     if (Options.debug) 
131         TASK_AddTaskEntryBreakpoint = DEBUG_AddTaskEntryBreakpoint;
132
133     /* Initialize everything */
134     if (!MAIN_MainInit()) return 1;
135
136     /* Load kernel modules */
137     if (!LoadLibrary16(  "KERNEL" )) return 1;
138     if (!LoadLibraryA( "KERNEL32" )) return 1;
139
140     /* Create initial task */
141     if ( !(pModule = NE_GetPtr( GetModuleHandle16( "KERNEL" ) )) ) return 1;
142     if ( !TASK_Create( pModule, FALSE ) ) return 1;
143
144     /* Switch to initial task */
145     PostEvent16( PROCESS_Current()->task );
146     TASK_Reschedule();
147
148     /* Switch stacks and jump to MAIN_EmulatorRun */
149     CALL32_Init( &IF1632_CallLargeStack, MAIN_EmulatorRun, NtCurrentTeb()->stack_top );
150
151     MESSAGE( "main: Should never happen: returned from CALL32_Init()\n" );
152     return 0;
153 }