Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Emulator initialisation code |
| 3 | * |
| 4 | */ |
| 5 | |
Ulrich Weigand | c75d0e1 | 1998-11-01 17:59:35 +0000 | [diff] [blame] | 6 | #include <assert.h> |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 7 | #include "callback.h" |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 8 | #include "debug.h" |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 9 | #include "debugger.h" |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 10 | #include "main.h" |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 11 | #include "miscemu.h" |
| 12 | #include "module.h" |
| 13 | #include "options.h" |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 14 | #include "process.h" |
| 15 | #include "win16drv.h" |
Ulrich Weigand | c75d0e1 | 1998-11-01 17:59:35 +0000 | [diff] [blame] | 16 | #include "thread.h" |
Ulrich Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 17 | #include "task.h" |
| 18 | #include "stackframe.h" |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 19 | #include "windows.h" |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 20 | |
Ulrich Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 21 | static int MAIN_argc; |
| 22 | static char **MAIN_argv; |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 23 | |
| 24 | /*********************************************************************** |
| 25 | * Emulator initialisation |
| 26 | */ |
| 27 | BOOL32 MAIN_EmulatorInit(void) |
| 28 | { |
Ulrich Weigand | c75d0e1 | 1998-11-01 17:59:35 +0000 | [diff] [blame] | 29 | /* Main initialization */ |
| 30 | if (!MAIN_MainInit()) return FALSE; |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 31 | |
| 32 | /* Initialize relay code */ |
| 33 | if (!RELAY_Init()) return FALSE; |
| 34 | |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 35 | /* Create the Win16 printer driver */ |
| 36 | if (!WIN16DRV_Init()) return FALSE; |
| 37 | |
Ulrich Weigand | c75d0e1 | 1998-11-01 17:59:35 +0000 | [diff] [blame] | 38 | return TRUE; |
| 39 | } |
| 40 | |
Marcus Meissner | 773c7b7 | 1998-11-30 17:18:30 +0000 | [diff] [blame] | 41 | |
Ulrich Weigand | c75d0e1 | 1998-11-01 17:59:35 +0000 | [diff] [blame] | 42 | /*********************************************************************** |
| 43 | * Main loop of initial task |
| 44 | */ |
| 45 | void MAIN_EmulatorRun( void ) |
| 46 | { |
Ulrich Weigand | ff35e1f | 1998-12-24 15:13:54 +0000 | [diff] [blame] | 47 | extern void THUNK_InitCallout( void ); |
Ulrich Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 48 | char startProg[256], defProg[256]; |
Ulrich Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 49 | HINSTANCE32 handle; |
Ulrich Weigand | afac2e4 | 1998-11-14 18:45:25 +0000 | [diff] [blame] | 50 | int i; |
Ulrich Weigand | ff35e1f | 1998-12-24 15:13:54 +0000 | [diff] [blame] | 51 | MSG32 msg; |
Ulrich Weigand | c75d0e1 | 1998-11-01 17:59:35 +0000 | [diff] [blame] | 52 | |
Ulrich Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 53 | /* 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 Weigand | ff35e1f | 1998-12-24 15:13:54 +0000 | [diff] [blame] | 58 | /* Get pointers to USER routines called by KERNEL */ |
| 59 | THUNK_InitCallout(); |
| 60 | |
Ulrich Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 61 | /* 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 Weigand | afac2e4 | 1998-11-14 18:45:25 +0000 | [diff] [blame] | 74 | /* Abort if no executable on command line */ |
| 75 | if (MAIN_argc <= 1) |
Ulrich Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 76 | { |
Ulrich Weigand | afac2e4 | 1998-11-14 18:45:25 +0000 | [diff] [blame] | 77 | 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 Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 83 | 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 Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 90 | default: MSG("error=%d\n", handle ); break; |
| 91 | } |
Ulrich Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 92 | } |
Ulrich Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 93 | |
| 94 | if (GetNumTasks() <= 1) |
| 95 | { |
| 96 | MSG("wine: no executable file found.\n" ); |
| 97 | ExitProcess( 0 ); |
| 98 | } |
| 99 | |
Ulrich Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 100 | |
| 101 | /* Start message loop for desktop window */ |
| 102 | |
Ulrich Weigand | ff35e1f | 1998-12-24 15:13:54 +0000 | [diff] [blame] | 103 | while ( GetNumTasks() > 1 && Callout.GetMessage32A( &msg, 0, 0, 0 ) ) |
Ulrich Weigand | c75d0e1 | 1998-11-01 17:59:35 +0000 | [diff] [blame] | 104 | { |
Ulrich Weigand | ff35e1f | 1998-12-24 15:13:54 +0000 | [diff] [blame] | 105 | Callout.TranslateMessage32( &msg ); |
| 106 | Callout.DispatchMessage32A( &msg ); |
Ulrich Weigand | c75d0e1 | 1998-11-01 17:59:35 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | ExitProcess( 0 ); |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
| 113 | /********************************************************************** |
| 114 | * main |
| 115 | */ |
| 116 | int main( int argc, char *argv[] ) |
| 117 | { |
Ulrich Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 118 | NE_MODULE *pModule; |
| 119 | HINSTANCE16 hInstance; |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 120 | extern char * DEBUG_argv0; |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 121 | |
| 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 Julliard | 03468f7 | 1998-02-15 19:40:49 +0000 | [diff] [blame] | 130 | /* Create the initial process */ |
| 131 | if (!PROCESS_Init()) return FALSE; |
| 132 | |
| 133 | /* Parse command-line */ |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 134 | if (!MAIN_WineInit( &argc, argv )) return 1; |
Ulrich Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 135 | MAIN_argc = argc; MAIN_argv = argv; |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 136 | |
| 137 | /* Handle -dll option (hack) */ |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 138 | if (Options.dllFlags) |
| 139 | { |
| 140 | if (!BUILTIN_ParseDLLOptions( Options.dllFlags )) |
| 141 | { |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 142 | MSG("%s: Syntax: -dll +xxx,... or -dll -xxx,...\n", |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 143 | argv[0] ); |
| 144 | BUILTIN_PrintDLLs(); |
| 145 | exit(1); |
| 146 | } |
| 147 | } |
| 148 | |
Marcus Meissner | 9faa7d7 | 1999-02-13 17:55:17 +0000 | [diff] [blame] | 149 | /* 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 Weigand | afac2e4 | 1998-11-14 18:45:25 +0000 | [diff] [blame] | 154 | if (Options.debug) |
| 155 | TASK_AddTaskEntryBreakpoint = DEBUG_AddTaskEntryBreakpoint; |
| 156 | |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 157 | /* Initialize everything */ |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 158 | if (!MAIN_EmulatorInit()) return 1; |
| 159 | |
Ulrich Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 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 | |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 169 | /* Initialize CALL32 routines */ |
Ulrich Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 170 | /* This needs to be done just before switching stacks */ |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 171 | IF1632_CallLargeStack = (int (*)(int (*func)(), void *arg))CALL32_Init(); |
Ulrich Weigand | e8b0274 | 1998-11-08 12:07:36 +0000 | [diff] [blame] | 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" ); |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 177 | return 0; |
| 178 | } |