blob: 8d413c85e657c817244029aad27cc82bc43d3851 [file] [log] [blame]
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +00001/*
2 * Emulator initialisation code
3 *
4 */
5
David Luyeree517e81999-02-28 12:27:56 +00006#include <stdlib.h>
Ulrich Weigandc75d0e11998-11-01 17:59:35 +00007#include <assert.h>
Marcus Meissner317af321999-02-17 13:51:06 +00008#include "wine/winbase16.h"
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +00009#include "callback.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"
Ulrich Weigandc75d0e11998-11-01 17:59:35 +000015#include "thread.h"
Ulrich Weigande8b02741998-11-08 12:07:36 +000016#include "task.h"
17#include "stackframe.h"
Alexandre Julliard410ae4f1999-06-18 18:23:11 +000018#include "wine/exception.h"
Alexandre Julliard61fece01999-06-26 19:09:08 +000019#include "debugtools.h"
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +000020
Alexandre Julliardfe085682000-03-18 21:56:10 +000021extern DWORD DEBUG_WinExec(LPCSTR lpCmdLine, int sw);
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +000022
Alexandre Julliardfe085682000-03-18 21:56:10 +000023
24static BOOL exec_program( LPCSTR cmdline )
25{
26 HINSTANCE handle;
27
28 if (Options.debug)
29 handle = DEBUG_WinExec( cmdline, SW_SHOWNORMAL );
30 else
31 handle = WinExec( cmdline, SW_SHOWNORMAL );
32
33 if (handle < 32)
34 {
35 MESSAGE( "%s: can't exec '%s': ", argv0, cmdline );
36 switch (handle)
37 {
38 case 2: MESSAGE("file not found\n" ); break;
39 case 11: MESSAGE("invalid exe file\n" ); break;
40 default: MESSAGE("error=%d\n", handle ); break;
41 }
42 }
43 return (handle >= 32);
44}
Alexandre Julliardc42dfdd1999-06-20 15:09:32 +000045
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +000046/***********************************************************************
Ulrich Weigandc75d0e11998-11-01 17:59:35 +000047 * Main loop of initial task
48 */
49void MAIN_EmulatorRun( void )
50{
Ulrich Weigande8b02741998-11-08 12:07:36 +000051 char startProg[256], defProg[256];
Marcus Meissner40978651999-02-14 11:32:18 +000052 int i, tasks = 0;
Alexandre Julliarda3960291999-02-26 11:11:13 +000053 MSG msg;
Ulrich Weigandc75d0e11998-11-01 17:59:35 +000054
Ulrich Weigande8b02741998-11-08 12:07:36 +000055 /* Load system DLLs into the initial process (and initialize them) */
Alexandre Julliarda3960291999-02-26 11:11:13 +000056 if ( !LoadLibrary16("GDI.EXE" ) || !LoadLibraryA("GDI32.DLL" )
57 || !LoadLibrary16("USER.EXE") || !LoadLibraryA("USER32.DLL"))
Ulrich Weigande8b02741998-11-08 12:07:36 +000058 ExitProcess( 1 );
59
Ulrich Weigandff35e1f1998-12-24 15:13:54 +000060 /* Get pointers to USER routines called by KERNEL */
61 THUNK_InitCallout();
62
Ulrich Weigand98c30531999-07-27 17:10:06 +000063 /* Call FinalUserInit routine */
64 Callout.FinalUserInit16();
65
Ulrich Weigand73663071999-04-10 16:32:45 +000066 /* Call InitApp for initial task */
67 Callout.InitApp16( MapHModuleLS( 0 ) );
68
Ulrich Weigande8b02741998-11-08 12:07:36 +000069 /* Add the Startup Program to the run list */
70 PROFILE_GetWineIniString( "programs", "Startup", "",
71 startProg, sizeof(startProg) );
Alexandre Julliardfe085682000-03-18 21:56:10 +000072 if (startProg[0]) tasks += exec_program( startProg );
Ulrich Weigande8b02741998-11-08 12:07:36 +000073
Alexandre Julliardfe085682000-03-18 21:56:10 +000074 /* Add the Default Program if no program on the command line */
75 if (!Options.argv[1])
Ulrich Weigande8b02741998-11-08 12:07:36 +000076 {
Alexandre Julliardfe085682000-03-18 21:56:10 +000077 PROFILE_GetWineIniString( "programs", "Default", "",
78 defProg, sizeof(defProg) );
79 if (defProg[0]) tasks += exec_program( defProg );
80 else if (!tasks && !startProg[0]) OPTIONS_Usage();
Ulrich Weigandafac2e41998-11-14 18:45:25 +000081 }
Alexandre Julliardfe085682000-03-18 21:56:10 +000082 else
Marcus Meissner40978651999-02-14 11:32:18 +000083 {
Alexandre Julliardfe085682000-03-18 21:56:10 +000084 /* Load and run executables given on command line */
85 for (i = 1; Options.argv[i]; i++)
86 {
87 tasks += exec_program( Options.argv[i] );
Ulrich Weigande8b02741998-11-08 12:07:36 +000088 }
Marcus Meissner40978651999-02-14 11:32:18 +000089 }
Alexandre Julliardfe085682000-03-18 21:56:10 +000090 if (!tasks) ExitProcess( 0 );
Ulrich Weigande8b02741998-11-08 12:07:36 +000091
Ulrich Weigande8b02741998-11-08 12:07:36 +000092 /* Start message loop for desktop window */
93
Ulrich Weiganda1957c41999-05-24 08:10:46 +000094 while ( GetNumTasks16() > 1 && Callout.GetMessageA( &msg, 0, 0, 0 ) )
Ulrich Weigandc75d0e11998-11-01 17:59:35 +000095 {
Alexandre Julliarda3960291999-02-26 11:11:13 +000096 Callout.TranslateMessage( &msg );
97 Callout.DispatchMessageA( &msg );
Ulrich Weiganda1957c41999-05-24 08:10:46 +000098 }
Ulrich Weigandc75d0e11998-11-01 17:59:35 +000099
100 ExitProcess( 0 );
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000101}
102
103
104/**********************************************************************
105 * main
106 */
107int main( int argc, char *argv[] )
108{
Ulrich Weigande8b02741998-11-08 12:07:36 +0000109 NE_MODULE *pModule;
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000110
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000111 /* Initialize everything */
Ulrich Weigand7d43bce2000-01-30 21:16:45 +0000112 if (!MAIN_MainInit( &argc, argv, FALSE )) return 1;
Ulrich Weigande8b02741998-11-08 12:07:36 +0000113
114 /* Create initial task */
Alexandre Julliard8d267a61999-05-02 19:09:07 +0000115 if ( !(pModule = NE_GetPtr( GetModuleHandle16( "KERNEL" ) )) ) return 1;
Ulrich Weigandfbea5301999-07-03 12:26:29 +0000116 if ( !TASK_Create( pModule, FALSE ) ) return 1;
Ulrich Weigande8b02741998-11-08 12:07:36 +0000117
Ulrich Weigande8b02741998-11-08 12:07:36 +0000118 /* Switch to initial task */
Ulrich Weigandbba76ee1999-06-06 14:49:55 +0000119 PostEvent16( PROCESS_Current()->task );
120 TASK_Reschedule();
121
122 /* Switch stacks and jump to MAIN_EmulatorRun */
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000123 CALL32_Init( &IF1632_CallLargeStack, MAIN_EmulatorRun, NtCurrentTeb()->stack_top );
Ulrich Weigandbba76ee1999-06-06 14:49:55 +0000124
Alexandre Julliard61fece01999-06-26 19:09:08 +0000125 MESSAGE( "main: Should never happen: returned from CALL32_Init()\n" );
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000126 return 0;
127}