Use position-independent code in assembly glue when building .so.
[wine/wine64.git] / miscemu / main.c
blob8c8ce20909795f32cc54aab229c7f72ac40c3b1d
1 /*
2 * Emulator initialisation code
4 */
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 "win16drv.h"
14 #include "module.h"
15 #include "options.h"
16 #include "process.h"
17 #include "thread.h"
18 #include "task.h"
19 #include "stackframe.h"
20 #include "wine/exception.h"
21 #include "debugtools.h"
23 static int MAIN_argc;
24 static char **MAIN_argv;
27 /***********************************************************************
28 * Emulator initialisation
30 BOOL MAIN_EmulatorInit(void)
32 /* Main initialization */
33 if (!MAIN_MainInit()) return FALSE;
35 /* Initialize relay code */
36 if (!RELAY_Init()) return FALSE;
38 /* Create the Win16 printer driver */
39 if (!WIN16DRV_Init()) return FALSE;
41 return TRUE;
45 /***********************************************************************
46 * Main loop of initial task
48 void MAIN_EmulatorRun( void )
50 extern void THUNK_InitCallout( void );
51 char startProg[256], defProg[256];
52 HINSTANCE handle;
53 int i, tasks = 0;
54 MSG msg;
56 /* Load system DLLs into the initial process (and initialize them) */
57 if ( !LoadLibrary16("GDI.EXE" ) || !LoadLibraryA("GDI32.DLL" )
58 || !LoadLibrary16("USER.EXE") || !LoadLibraryA("USER32.DLL"))
59 ExitProcess( 1 );
61 /* Get pointers to USER routines called by KERNEL */
62 THUNK_InitCallout();
64 /* Call InitApp for initial task */
65 Callout.InitApp16( MapHModuleLS( 0 ) );
67 /* Add the Default Program if no program on the command line */
68 if (!MAIN_argv[1])
70 PROFILE_GetWineIniString( "programs", "Default", "",
71 defProg, sizeof(defProg) );
72 if (defProg[0]) MAIN_argv[MAIN_argc++] = defProg;
75 /* Add the Startup Program to the run list */
76 PROFILE_GetWineIniString( "programs", "Startup", "",
77 startProg, sizeof(startProg) );
78 if (startProg[0]) MAIN_argv[MAIN_argc++] = startProg;
80 /* Abort if no executable on command line */
81 if (MAIN_argc <= 1)
83 MAIN_Usage(MAIN_argv[0]);
84 ExitProcess( 1 );
87 /* Load and run executables given on command line */
88 for (i = 1; i < MAIN_argc; i++)
90 if ((handle = WinExec( MAIN_argv[i], SW_SHOWNORMAL )) < 32)
92 MESSAGE("wine: can't exec '%s': ", MAIN_argv[i]);
93 switch (handle)
95 case 2: MESSAGE("file not found\n" ); break;
96 case 11: MESSAGE("invalid exe file\n" ); break;
97 default: MESSAGE("error=%d\n", handle ); break;
100 else tasks++;
103 if (!tasks)
105 MESSAGE("wine: no executable file found.\n" );
106 ExitProcess( 0 );
109 /* Start message loop for desktop window */
111 while ( GetNumTasks16() > 1 && Callout.GetMessageA( &msg, 0, 0, 0 ) )
113 Callout.TranslateMessage( &msg );
114 Callout.DispatchMessageA( &msg );
117 ExitProcess( 0 );
121 /**********************************************************************
122 * main
124 int main( int argc, char *argv[] )
126 NE_MODULE *pModule;
127 extern char * DEBUG_argv0;
129 __winelib = 0; /* First of all, clear the Winelib flag */
132 * Save this so that the internal debugger can get a hold of it if
133 * it needs to.
135 DEBUG_argv0 = argv[0];
137 /* Create the initial process */
138 if (!PROCESS_Init()) return FALSE;
140 /* Parse command-line */
141 if (!MAIN_WineInit( &argc, argv )) return 1;
142 MAIN_argc = argc; MAIN_argv = argv;
144 /* Set up debugger hook */
145 EXC_SetDebugEventHook( wine_debugger );
147 if (Options.debug)
148 TASK_AddTaskEntryBreakpoint = DEBUG_AddTaskEntryBreakpoint;
150 /* Initialize everything */
151 if (!MAIN_EmulatorInit()) return 1;
153 /* Load kernel modules */
154 if (!LoadLibrary16( "KERNEL" )) return 1;
155 if (!LoadLibraryA( "KERNEL32" )) return 1;
157 /* Create initial task */
158 if ( !(pModule = NE_GetPtr( GetModuleHandle16( "KERNEL" ) )) ) return 1;
159 if ( !TASK_Create( pModule, FALSE ) ) return 1;
161 /* Switch to initial task */
162 PostEvent16( PROCESS_Current()->task );
163 TASK_Reschedule();
165 /* Switch stacks and jump to MAIN_EmulatorRun */
166 CALL32_Init( &IF1632_CallLargeStack, MAIN_EmulatorRun, NtCurrentTeb()->stack_top );
168 MESSAGE( "main: Should never happen: returned from CALL32_Init()\n" );
169 return 0;