blob: 31b4b203007b84c22a4bb0455dc9e91aad951567 [file] [log] [blame]
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +00001/*
2 * Emulator initialisation code
3 *
4 */
5
Ulrich Weigandc75d0e11998-11-01 17:59:35 +00006#include <assert.h>
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +00007#include "callback.h"
Alexandre Julliarda845b881998-06-01 10:44:35 +00008#include "debug.h"
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +00009#include "debugger.h"
Alexandre Julliarddadf78f1998-05-17 17:13:43 +000010#include "main.h"
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +000011#include "miscemu.h"
12#include "module.h"
13#include "options.h"
Alexandre Julliarda845b881998-06-01 10:44:35 +000014#include "process.h"
15#include "win16drv.h"
Ulrich Weigandc75d0e11998-11-01 17:59:35 +000016#include "thread.h"
Ulrich Weigande8b02741998-11-08 12:07:36 +000017#include "task.h"
18#include "stackframe.h"
Alexandre Julliarda845b881998-06-01 10:44:35 +000019#include "windows.h"
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +000020
Ulrich Weigande8b02741998-11-08 12:07:36 +000021static int MAIN_argc;
22static char **MAIN_argv;
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +000023
24/***********************************************************************
25 * Emulator initialisation
26 */
27BOOL32 MAIN_EmulatorInit(void)
28{
Ulrich Weigandc75d0e11998-11-01 17:59:35 +000029 /* Main initialization */
30 if (!MAIN_MainInit()) return FALSE;
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +000031
32 /* Initialize relay code */
33 if (!RELAY_Init()) return FALSE;
34
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +000035 /* Create the Win16 printer driver */
36 if (!WIN16DRV_Init()) return FALSE;
37
Ulrich Weigandc75d0e11998-11-01 17:59:35 +000038 return TRUE;
39}
40
Marcus Meissner773c7b71998-11-30 17:18:30 +000041
Ulrich Weigandc75d0e11998-11-01 17:59:35 +000042/***********************************************************************
43 * Main loop of initial task
44 */
45void MAIN_EmulatorRun( void )
46{
Ulrich Weigandff35e1f1998-12-24 15:13:54 +000047 extern void THUNK_InitCallout( void );
Ulrich Weigande8b02741998-11-08 12:07:36 +000048 char startProg[256], defProg[256];
Ulrich Weigande8b02741998-11-08 12:07:36 +000049 HINSTANCE32 handle;
Ulrich Weigandafac2e41998-11-14 18:45:25 +000050 int i;
Ulrich Weigandff35e1f1998-12-24 15:13:54 +000051 MSG32 msg;
Ulrich Weigandc75d0e11998-11-01 17:59:35 +000052
Ulrich Weigande8b02741998-11-08 12:07:36 +000053 /* Load system DLLs into the initial process (and initialize them) */
54 if ( !LoadLibrary16("GDI.EXE" ) || !LoadLibrary32A("GDI32.DLL" )
55 || !LoadLibrary16("USER.EXE") || !LoadLibrary32A("USER32.DLL"))
56 ExitProcess( 1 );
57
Ulrich Weigandff35e1f1998-12-24 15:13:54 +000058 /* Get pointers to USER routines called by KERNEL */
59 THUNK_InitCallout();
60
Ulrich Weigande8b02741998-11-08 12:07:36 +000061 /* Add the Default Program if no program on the command line */
62 if (!MAIN_argv[1])
63 {
64 PROFILE_GetWineIniString( "programs", "Default", "",
65 defProg, sizeof(defProg) );
66 if (defProg[0]) MAIN_argv[MAIN_argc++] = defProg;
67 }
68
69 /* Add the Startup Program to the run list */
70 PROFILE_GetWineIniString( "programs", "Startup", "",
71 startProg, sizeof(startProg) );
72 if (startProg[0]) MAIN_argv[MAIN_argc++] = startProg;
73
Ulrich Weigandafac2e41998-11-14 18:45:25 +000074 /* Abort if no executable on command line */
75 if (MAIN_argc <= 1)
Ulrich Weigande8b02741998-11-08 12:07:36 +000076 {
Ulrich Weigandafac2e41998-11-14 18:45:25 +000077 MAIN_Usage(MAIN_argv[0]);
78 exit(1);
79 }
80
81 /* Load and run executables given on command line */
82 for (i = 1; i < MAIN_argc; i++)
Ulrich Weigande8b02741998-11-08 12:07:36 +000083 if ((handle = WinExec32( MAIN_argv[i], SW_SHOWNORMAL )) < 32)
84 {
85 MSG("wine: can't exec '%s': ", MAIN_argv[i]);
86 switch (handle)
87 {
88 case 2: MSG("file not found\n" ); break;
89 case 11: MSG("invalid exe file\n" ); break;
Ulrich Weigande8b02741998-11-08 12:07:36 +000090 default: MSG("error=%d\n", handle ); break;
91 }
Ulrich Weigande8b02741998-11-08 12:07:36 +000092 }
Ulrich Weigande8b02741998-11-08 12:07:36 +000093
94 if (GetNumTasks() <= 1)
95 {
96 MSG("wine: no executable file found.\n" );
97 ExitProcess( 0 );
98 }
99
Ulrich Weigande8b02741998-11-08 12:07:36 +0000100
101 /* Start message loop for desktop window */
102
Ulrich Weigandff35e1f1998-12-24 15:13:54 +0000103 while ( GetNumTasks() > 1 && Callout.GetMessage32A( &msg, 0, 0, 0 ) )
Ulrich Weigandc75d0e11998-11-01 17:59:35 +0000104 {
Ulrich Weigandff35e1f1998-12-24 15:13:54 +0000105 Callout.TranslateMessage32( &msg );
106 Callout.DispatchMessage32A( &msg );
Ulrich Weigandc75d0e11998-11-01 17:59:35 +0000107 }
108
109 ExitProcess( 0 );
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000110}
111
112
113/**********************************************************************
114 * main
115 */
116int main( int argc, char *argv[] )
117{
Ulrich Weigande8b02741998-11-08 12:07:36 +0000118 NE_MODULE *pModule;
119 HINSTANCE16 hInstance;
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000120 extern char * DEBUG_argv0;
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000121
122 __winelib = 0; /* First of all, clear the Winelib flag */
123
124 /*
125 * Save this so that the internal debugger can get a hold of it if
126 * it needs to.
127 */
128 DEBUG_argv0 = argv[0];
129
Alexandre Julliard03468f71998-02-15 19:40:49 +0000130 /* Create the initial process */
131 if (!PROCESS_Init()) return FALSE;
132
133 /* Parse command-line */
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000134 if (!MAIN_WineInit( &argc, argv )) return 1;
Ulrich Weigande8b02741998-11-08 12:07:36 +0000135 MAIN_argc = argc; MAIN_argv = argv;
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000136
137 /* Handle -dll option (hack) */
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000138 if (Options.dllFlags)
139 {
140 if (!BUILTIN_ParseDLLOptions( Options.dllFlags ))
141 {
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000142 MSG("%s: Syntax: -dll +xxx,... or -dll -xxx,...\n",
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000143 argv[0] );
144 BUILTIN_PrintDLLs();
145 exit(1);
146 }
147 }
148
Marcus Meissner9faa7d71999-02-13 17:55:17 +0000149 /* Set up debugger/instruction emulation callback routines */
150 ctx_debug_call = ctx_debug;
151 fnWINE_Debugger = wine_debug;
152 fnINSTR_EmulateInstruction = INSTR_EmulateInstruction;
153
Ulrich Weigandafac2e41998-11-14 18:45:25 +0000154 if (Options.debug)
155 TASK_AddTaskEntryBreakpoint = DEBUG_AddTaskEntryBreakpoint;
156
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000157 /* Initialize everything */
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000158 if (!MAIN_EmulatorInit()) return 1;
159
Ulrich Weigande8b02741998-11-08 12:07:36 +0000160 /* 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
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000169 /* Initialize CALL32 routines */
Ulrich Weigande8b02741998-11-08 12:07:36 +0000170 /* This needs to be done just before switching stacks */
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000171 IF1632_CallLargeStack = (int (*)(int (*func)(), void *arg))CALL32_Init();
Ulrich Weigande8b02741998-11-08 12:07:36 +0000172
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" );
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000177 return 0;
178}