2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
5 * PURPOSE: Virtual DOS Machine
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
9 /* INCLUDES *******************************************************************/
17 #include "hardware/ps2.h"
18 #include "hardware/vga.h"
19 #include "bios/bios.h"
25 * Activate this line if you want to run NTVDM in standalone mode with:
30 /* VARIABLES ******************************************************************/
32 static HANDLE ConsoleInput
= INVALID_HANDLE_VALUE
;
33 static HANDLE ConsoleOutput
= INVALID_HANDLE_VALUE
;
34 static DWORD OrgConsoleInputMode
, OrgConsoleOutputMode
;
35 static CONSOLE_CURSOR_INFO OrgConsoleCursorInfo
;
36 static CONSOLE_SCREEN_BUFFER_INFO OrgConsoleBufferInfo
;
37 static BOOLEAN AcceptCommands
= TRUE
;
38 static HANDLE CommandThread
= NULL
;
40 static HMENU hConsoleMenu
= NULL
;
41 static INT VdmMenuPos
= -1;
42 static BOOLEAN ShowPointer
= FALSE
;
45 * Those menu helpers were taken from the GUI frontend in winsrv.dll
47 typedef struct _VDM_MENUITEM
50 const struct _VDM_MENUITEM
*SubMenu
;
52 } VDM_MENUITEM
, *PVDM_MENUITEM
;
54 static const VDM_MENUITEM VdmMenuItems
[] =
56 { IDS_VDM_QUIT
, NULL
, ID_VDM_QUIT
},
58 { 0, NULL
, 0 } /* End of list */
61 static const VDM_MENUITEM VdmMainMenuItems
[] =
63 { -1, NULL
, 0 }, /* Separator */
64 { IDS_HIDE_MOUSE
, NULL
, ID_SHOWHIDE_MOUSE
}, /* Hide mouse; can be renamed to Show mouse */
65 { IDS_VDM_MENU
, VdmMenuItems
, 0 }, /* ReactOS VDM Menu */
67 { 0, NULL
, 0 } /* End of list */
71 AppendMenuItems(HMENU hMenu
,
72 const VDM_MENUITEM
*Items
)
75 WCHAR szMenuString
[255];
80 if (Items
[i
].uID
!= (UINT
)-1)
82 if (LoadStringW(GetModuleHandle(NULL
),
85 sizeof(szMenuString
) / sizeof(szMenuString
[0])) > 0)
87 if (Items
[i
].SubMenu
!= NULL
)
89 hSubMenu
= CreatePopupMenu();
92 AppendMenuItems(hSubMenu
, Items
[i
].SubMenu
);
94 if (!AppendMenuW(hMenu
,
99 DestroyMenu(hSubMenu
);
120 } while (!(Items
[i
].uID
== 0 && Items
[i
].SubMenu
== NULL
&& Items
[i
].wCmdID
== 0));
124 CreateVdmMenu(HANDLE ConOutHandle
)
126 hConsoleMenu
= ConsoleMenuControl(ConsoleOutput
,
129 if (hConsoleMenu
== NULL
) return;
131 VdmMenuPos
= GetMenuItemCount(hConsoleMenu
);
132 AppendMenuItems(hConsoleMenu
, VdmMainMenuItems
);
133 DrawMenuBar(GetConsoleWindow());
140 const VDM_MENUITEM
*Items
= VdmMainMenuItems
;
144 DeleteMenu(hConsoleMenu
, VdmMenuPos
, MF_BYPOSITION
);
146 } while (!(Items
[i
].uID
== 0 && Items
[i
].SubMenu
== NULL
&& Items
[i
].wCmdID
== 0));
148 DrawMenuBar(GetConsoleWindow());
151 static VOID
ShowHideMousePointer(HANDLE ConOutHandle
, BOOLEAN ShowPtr
)
153 WCHAR szMenuString
[255] = L
"";
157 /* Be sure the cursor will be shown */
158 while (ShowConsoleCursor(ConOutHandle
, TRUE
) < 0) ;
162 /* Be sure the cursor will be hidden */
163 while (ShowConsoleCursor(ConOutHandle
, FALSE
) >= 0) ;
166 if (LoadStringW(GetModuleHandle(NULL
),
167 (!ShowPtr
? IDS_SHOW_MOUSE
: IDS_HIDE_MOUSE
),
169 sizeof(szMenuString
) / sizeof(szMenuString
[0])) > 0)
171 ModifyMenu(hConsoleMenu
, ID_SHOWHIDE_MOUSE
,
172 MF_BYCOMMAND
, ID_SHOWHIDE_MOUSE
, szMenuString
);
176 /* PUBLIC FUNCTIONS ***********************************************************/
178 VOID
DisplayMessage(LPCWSTR Format
, ...)
183 va_start(Parameters
, Format
);
184 _vsnwprintf(Buffer
, 256, Format
, Parameters
);
185 DPRINT1("\n\nNTVDM Subsystem\n%S\n\n", Buffer
);
186 MessageBoxW(NULL
, Buffer
, L
"NTVDM Subsystem", MB_OK
);
190 BOOL WINAPI
ConsoleCtrlHandler(DWORD ControlType
)
195 case CTRL_BREAK_EVENT
:
198 EmulatorInterrupt(0x23);
201 case CTRL_LAST_CLOSE_EVENT
:
205 /* Exit immediately */
206 if (CommandThread
) TerminateThread(CommandThread
, 0);
210 /* Stop accepting new commands */
211 AcceptCommands
= FALSE
;
218 /* Stop the VDM if the user logs out or closes the console */
225 VOID
ConsoleInitUI(VOID
)
227 CreateVdmMenu(ConsoleOutput
);
230 VOID
ConsoleCleanupUI(VOID
)
232 /* Display again properly the mouse pointer */
233 if (ShowPointer
) ShowHideMousePointer(ConsoleOutput
, ShowPointer
);
238 DWORD WINAPI
PumpConsoleInput(LPVOID Parameter
)
240 HANDLE ConsoleInput
= (HANDLE
)Parameter
;
241 INPUT_RECORD InputRecord
;
246 /* Wait for an input record */
247 if (!ReadConsoleInput(ConsoleInput
, &InputRecord
, 1, &Count
))
249 DWORD LastError
= GetLastError();
250 DPRINT1("Error reading console input (0x%p, %lu) - Error %lu\n", ConsoleInput
, Count
, LastError
);
256 /* Check the event type */
257 switch (InputRecord
.EventType
)
261 /* Send it to the PS/2 controller */
262 PS2Dispatch(&InputRecord
);
267 switch (InputRecord
.Event
.MenuEvent
.dwCommandId
)
269 case ID_SHOWHIDE_MOUSE
:
270 ShowHideMousePointer(ConsoleOutput
, ShowPointer
);
271 ShowPointer
= !ShowPointer
;
294 BOOL
ConsoleInit(VOID
)
296 /* Set the handler routine */
297 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
299 /* Enable the CTRL_LAST_CLOSE_EVENT */
300 SetLastConsoleEventActive();
302 /* Get the input handle to the real console, and check for success */
303 ConsoleInput
= CreateFileW(L
"CONIN$",
304 GENERIC_READ
| GENERIC_WRITE
,
305 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
310 if (ConsoleInput
== INVALID_HANDLE_VALUE
)
312 wprintf(L
"FATAL: Cannot retrieve a handle to the console input\n");
316 /* Get the output handle to the real console, and check for success */
317 ConsoleOutput
= CreateFileW(L
"CONOUT$",
318 GENERIC_READ
| GENERIC_WRITE
,
319 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
324 if (ConsoleOutput
== INVALID_HANDLE_VALUE
)
326 CloseHandle(ConsoleInput
);
327 wprintf(L
"FATAL: Cannot retrieve a handle to the console output\n");
331 /* Save the original input and output console modes */
332 if (!GetConsoleMode(ConsoleInput
, &OrgConsoleInputMode
) ||
333 !GetConsoleMode(ConsoleOutput
, &OrgConsoleOutputMode
))
335 CloseHandle(ConsoleOutput
);
336 CloseHandle(ConsoleInput
);
337 wprintf(L
"FATAL: Cannot save console in/out modes\n");
341 /* Save the original cursor and console screen buffer information */
342 if (!GetConsoleCursorInfo(ConsoleOutput
, &OrgConsoleCursorInfo
) ||
343 !GetConsoleScreenBufferInfo(ConsoleOutput
, &OrgConsoleBufferInfo
))
345 CloseHandle(ConsoleOutput
);
346 CloseHandle(ConsoleInput
);
347 wprintf(L
"FATAL: Cannot save console cursor/screen-buffer info\n");
351 /* Initialize the UI */
357 VOID
ConsoleCleanup(VOID
)
361 /* Restore the old screen buffer */
362 SetConsoleActiveScreenBuffer(ConsoleOutput
);
364 /* Restore the original console size */
367 ConRect
.Right
= ConRect
.Left
+ OrgConsoleBufferInfo
.srWindow
.Right
- OrgConsoleBufferInfo
.srWindow
.Left
;
368 ConRect
.Bottom
= ConRect
.Top
+ OrgConsoleBufferInfo
.srWindow
.Bottom
- OrgConsoleBufferInfo
.srWindow
.Top
;
370 * See the following trick explanation in vga.c:VgaEnterTextMode() .
372 SetConsoleScreenBufferSize(ConsoleOutput
, OrgConsoleBufferInfo
.dwSize
);
373 SetConsoleWindowInfo(ConsoleOutput
, TRUE
, &ConRect
);
374 SetConsoleScreenBufferSize(ConsoleOutput
, OrgConsoleBufferInfo
.dwSize
);
376 /* Restore the original cursor shape */
377 SetConsoleCursorInfo(ConsoleOutput
, &OrgConsoleCursorInfo
);
379 /* Restore the original input and output console modes */
380 SetConsoleMode(ConsoleOutput
, OrgConsoleOutputMode
);
381 SetConsoleMode(ConsoleInput
, OrgConsoleInputMode
);
386 /* Close the console handles */
387 if (ConsoleOutput
!= INVALID_HANDLE_VALUE
) CloseHandle(ConsoleOutput
);
388 if (ConsoleInput
!= INVALID_HANDLE_VALUE
) CloseHandle(ConsoleInput
);
391 DWORD WINAPI
CommandThreadProc(LPVOID Parameter
)
393 BOOLEAN First
= TRUE
;
395 VDM_COMMAND_INFO CommandInfo
;
396 CHAR CmdLine
[MAX_PATH
];
397 CHAR AppName
[MAX_PATH
];
398 CHAR PifFile
[MAX_PATH
];
399 CHAR Desktop
[MAX_PATH
];
400 CHAR Title
[MAX_PATH
];
403 UNREFERENCED_PARAMETER(Parameter
);
405 while (AcceptCommands
)
407 /* Clear the structure */
408 ZeroMemory(&CommandInfo
, sizeof(CommandInfo
));
410 /* Initialize the structure members */
411 CommandInfo
.VDMState
= VDM_FLAG_DOS
;
412 CommandInfo
.CmdLine
= CmdLine
;
413 CommandInfo
.CmdLen
= sizeof(CmdLine
);
414 CommandInfo
.AppName
= AppName
;
415 CommandInfo
.AppLen
= sizeof(AppName
);
416 CommandInfo
.PifFile
= PifFile
;
417 CommandInfo
.PifLen
= sizeof(PifFile
);
418 CommandInfo
.Desktop
= Desktop
;
419 CommandInfo
.DesktopLen
= sizeof(Desktop
);
420 CommandInfo
.Title
= Title
;
421 CommandInfo
.TitleLen
= sizeof(Title
);
422 CommandInfo
.Env
= Env
;
423 CommandInfo
.EnvLen
= sizeof(Env
);
427 CommandInfo
.VDMState
|= VDM_FLAG_FIRST_TASK
;
431 /* Wait for the next available VDM */
432 if (!GetNextVDMCommand(&CommandInfo
)) break;
434 /* Start the process from the command line */
435 DPRINT1("Starting '%s'...\n", AppName
);
437 Result
= DosLoadExecutable(DOS_LOAD_AND_EXECUTE
, AppName
, CmdLine
, Env
, NULL
, NULL
);
438 if (Result
!= ERROR_SUCCESS
)
440 DisplayMessage(L
"Could not start '%S'. Error: %u", AppName
, Result
);
444 /* Start simulation */
447 /* Perform another screen refresh */
454 INT
wmain(INT argc
, WCHAR
*argv
[])
459 CHAR ApplicationName
[MAX_PATH
];
460 CHAR CommandLine
[DOS_CMDLINE_LENGTH
];
464 WideCharToMultiByte(CP_ACP
, 0, argv
[1], -1, ApplicationName
, sizeof(ApplicationName
), NULL
, NULL
);
466 if (argc
>= 3) WideCharToMultiByte(CP_ACP
, 0, argv
[2], -1, CommandLine
, sizeof(CommandLine
), NULL
, NULL
);
467 else strcpy(CommandLine
, "");
471 wprintf(L
"\nReactOS Virtual DOS Machine\n\n"
472 L
"Usage: NTVDM <executable> [<parameters>]\n");
478 DPRINT1("\n\n\nNTVDM - Starting...\n\n\n");
480 /* Initialize the console */
483 wprintf(L
"FATAL: A problem occurred when trying to initialize the console\n");
487 /* Initialize the emulator */
488 if (!EmulatorInitialize(ConsoleInput
, ConsoleOutput
))
490 wprintf(L
"FATAL: Failed to initialize the emulator\n");
494 /* Initialize the system BIOS */
495 if (!BiosInitialize(NULL
))
497 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
501 /* Initialize the VDM DOS kernel */
502 if (!DosInitialize(NULL
))
504 wprintf(L
"FATAL: Failed to initialize the VDM DOS kernel.\n");
510 /* Create the GetNextVDMCommand thread */
511 CommandThread
= CreateThread(NULL
, 0, &CommandThreadProc
, NULL
, 0, NULL
);
512 if (CommandThread
== NULL
)
514 wprintf(L
"FATAL: Failed to create the command processing thread: %d\n", GetLastError());
518 /* Wait for the command thread to exit */
519 WaitForSingleObject(CommandThread
, INFINITE
);
521 /* Close the thread handle */
522 CloseHandle(CommandThread
);
526 /* Start the process from the command line */
527 DPRINT1("Starting '%s'...\n", ApplicationName
);
529 Result
= DosLoadExecutable(DOS_LOAD_AND_EXECUTE
,
532 GetEnvironmentStrings(),
535 if (Result
!= ERROR_SUCCESS
)
537 DisplayMessage(L
"Could not start '%S'. Error: %u", ApplicationName
, Result
);
541 /* Start simulation */
544 /* Perform another screen refresh */
559 DPRINT1("\n\n\nNTVDM - Exiting...\n\n\n");