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"
24 /* VARIABLES ******************************************************************/
26 static HANDLE ConsoleInput
= INVALID_HANDLE_VALUE
;
27 static HANDLE ConsoleOutput
= INVALID_HANDLE_VALUE
;
28 static DWORD OrgConsoleInputMode
, OrgConsoleOutputMode
;
29 static BOOLEAN AcceptCommands
= TRUE
;
30 static HANDLE CommandThread
= NULL
;
32 static HMENU hConsoleMenu
= NULL
;
33 static INT VdmMenuPos
= -1;
34 static BOOLEAN ShowPointer
= FALSE
;
40 HANDLE VdmTaskEvent
= NULL
;
43 * Those menu helpers were taken from the GUI frontend in winsrv.dll
45 typedef struct _VDM_MENUITEM
48 const struct _VDM_MENUITEM
*SubMenu
;
50 } VDM_MENUITEM
, *PVDM_MENUITEM
;
52 static const VDM_MENUITEM VdmMenuItems
[] =
54 { IDS_VDM_DUMPMEM
, NULL
, ID_VDM_DUMPMEM
},
55 { IDS_VDM_QUIT
, NULL
, ID_VDM_QUIT
},
57 { 0, NULL
, 0 } /* End of list */
60 static const VDM_MENUITEM VdmMainMenuItems
[] =
62 { -1, NULL
, 0 }, /* Separator */
63 { IDS_HIDE_MOUSE
, NULL
, ID_SHOWHIDE_MOUSE
}, /* Hide mouse; can be renamed to Show mouse */
64 { IDS_VDM_MENU
, VdmMenuItems
, 0 }, /* ReactOS VDM Menu */
66 { 0, NULL
, 0 } /* End of list */
70 AppendMenuItems(HMENU hMenu
,
71 const VDM_MENUITEM
*Items
)
74 WCHAR szMenuString
[255];
79 if (Items
[i
].uID
!= (UINT
)-1)
81 if (LoadStringW(GetModuleHandle(NULL
),
84 sizeof(szMenuString
) / sizeof(szMenuString
[0])) > 0)
86 if (Items
[i
].SubMenu
!= NULL
)
88 hSubMenu
= CreatePopupMenu();
91 AppendMenuItems(hSubMenu
, Items
[i
].SubMenu
);
93 if (!AppendMenuW(hMenu
,
98 DestroyMenu(hSubMenu
);
119 } while (!(Items
[i
].uID
== 0 && Items
[i
].SubMenu
== NULL
&& Items
[i
].wCmdID
== 0));
123 CreateVdmMenu(HANDLE ConOutHandle
)
125 hConsoleMenu
= ConsoleMenuControl(ConOutHandle
,
128 if (hConsoleMenu
== NULL
) return;
130 VdmMenuPos
= GetMenuItemCount(hConsoleMenu
);
131 AppendMenuItems(hConsoleMenu
, VdmMainMenuItems
);
132 DrawMenuBar(GetConsoleWindow());
139 const VDM_MENUITEM
*Items
= VdmMainMenuItems
;
143 DeleteMenu(hConsoleMenu
, VdmMenuPos
, MF_BYPOSITION
);
145 } while (!(Items
[i
].uID
== 0 && Items
[i
].SubMenu
== NULL
&& Items
[i
].wCmdID
== 0));
147 DrawMenuBar(GetConsoleWindow());
150 static VOID
ShowHideMousePointer(HANDLE ConOutHandle
, BOOLEAN ShowPtr
)
152 WCHAR szMenuString
[255] = L
"";
156 /* Be sure the cursor will be shown */
157 while (ShowConsoleCursor(ConOutHandle
, TRUE
) < 0) ;
161 /* Be sure the cursor will be hidden */
162 while (ShowConsoleCursor(ConOutHandle
, FALSE
) >= 0) ;
165 if (LoadStringW(GetModuleHandle(NULL
),
166 (!ShowPtr
? IDS_SHOW_MOUSE
: IDS_HIDE_MOUSE
),
168 sizeof(szMenuString
) / sizeof(szMenuString
[0])) > 0)
170 ModifyMenu(hConsoleMenu
, ID_SHOWHIDE_MOUSE
,
171 MF_BYCOMMAND
, ID_SHOWHIDE_MOUSE
, szMenuString
);
175 /* PUBLIC FUNCTIONS ***********************************************************/
178 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
);
192 ConsoleCtrlHandler(DWORD ControlType
)
197 case CTRL_BREAK_EVENT
:
200 EmulatorInterrupt(0x23);
203 case CTRL_LAST_CLOSE_EVENT
:
205 if (WaitForSingleObject(VdmTaskEvent
, 0) == WAIT_TIMEOUT
)
207 /* Exit immediately */
208 if (CommandThread
) TerminateThread(CommandThread
, 0);
213 /* Stop accepting new commands */
214 AcceptCommands
= FALSE
;
221 /* Stop the VDM if the user logs out or closes the console */
231 CreateVdmMenu(ConsoleOutput
);
235 ConsoleCleanupUI(VOID
)
237 /* Display again properly the mouse pointer */
238 if (ShowPointer
) ShowHideMousePointer(ConsoleOutput
, ShowPointer
);
246 /* Save the original input and output console modes */
247 if (!GetConsoleMode(ConsoleInput
, &OrgConsoleInputMode
) ||
248 !GetConsoleMode(ConsoleOutput
, &OrgConsoleOutputMode
))
250 CloseHandle(ConsoleOutput
);
251 CloseHandle(ConsoleInput
);
252 wprintf(L
"FATAL: Cannot save console in/out modes\n");
256 /* Initialize the UI */
265 /* Restore the original input and output console modes */
266 SetConsoleMode(ConsoleOutput
, OrgConsoleOutputMode
);
267 SetConsoleMode(ConsoleInput
, OrgConsoleInputMode
);
276 /* Set the handler routine */
277 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
279 /* Enable the CTRL_LAST_CLOSE_EVENT */
280 SetLastConsoleEventActive();
283 * NOTE: The CONIN$ and CONOUT$ "virtual" files
284 * always point to non-redirected console handles.
287 /* Get the input handle to the real console, and check for success */
288 ConsoleInput
= CreateFileW(L
"CONIN$",
289 GENERIC_READ
| GENERIC_WRITE
,
290 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
295 if (ConsoleInput
== INVALID_HANDLE_VALUE
)
297 wprintf(L
"FATAL: Cannot retrieve a handle to the console input\n");
301 /* Get the output handle to the real console, and check for success */
302 ConsoleOutput
= CreateFileW(L
"CONOUT$",
303 GENERIC_READ
| GENERIC_WRITE
,
304 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
309 if (ConsoleOutput
== INVALID_HANDLE_VALUE
)
311 CloseHandle(ConsoleInput
);
312 wprintf(L
"FATAL: Cannot retrieve a handle to the console output\n");
316 /* Effectively attach to the console */
317 return ConsoleAttach();
323 /* Detach from the console */
326 /* Close the console handles */
327 if (ConsoleOutput
!= INVALID_HANDLE_VALUE
) CloseHandle(ConsoleOutput
);
328 if (ConsoleInput
!= INVALID_HANDLE_VALUE
) CloseHandle(ConsoleInput
);
333 PumpConsoleInput(LPVOID Parameter
)
335 HANDLE ConsoleInput
= (HANDLE
)Parameter
;
336 INPUT_RECORD InputRecord
;
341 /* Make sure the task event is signaled */
342 WaitForSingleObject(VdmTaskEvent
, INFINITE
);
344 /* Wait for an input record */
345 if (!ReadConsoleInput(ConsoleInput
, &InputRecord
, 1, &Count
))
347 DWORD LastError
= GetLastError();
348 DPRINT1("Error reading console input (0x%p, %lu) - Error %lu\n", ConsoleInput
, Count
, LastError
);
354 /* Check the event type */
355 switch (InputRecord
.EventType
)
359 /* Send it to the PS/2 controller */
360 PS2Dispatch(&InputRecord
);
365 switch (InputRecord
.Event
.MenuEvent
.dwCommandId
)
367 case ID_SHOWHIDE_MOUSE
:
368 ShowHideMousePointer(ConsoleOutput
, ShowPointer
);
369 ShowPointer
= !ShowPointer
;
399 CommandThreadProc(LPVOID Parameter
)
401 BOOLEAN First
= TRUE
;
403 VDM_COMMAND_INFO CommandInfo
;
404 CHAR CmdLine
[MAX_PATH
];
405 CHAR AppName
[MAX_PATH
];
406 CHAR PifFile
[MAX_PATH
];
407 CHAR Desktop
[MAX_PATH
];
408 CHAR Title
[MAX_PATH
];
410 PVOID Env
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, EnvSize
);
412 UNREFERENCED_PARAMETER(Parameter
);
417 /* Clear the structure */
418 ZeroMemory(&CommandInfo
, sizeof(CommandInfo
));
420 /* Initialize the structure members */
421 CommandInfo
.TaskId
= SessionId
;
422 CommandInfo
.VDMState
= VDM_FLAG_DOS
;
423 CommandInfo
.CmdLine
= CmdLine
;
424 CommandInfo
.CmdLen
= sizeof(CmdLine
);
425 CommandInfo
.AppName
= AppName
;
426 CommandInfo
.AppLen
= sizeof(AppName
);
427 CommandInfo
.PifFile
= PifFile
;
428 CommandInfo
.PifLen
= sizeof(PifFile
);
429 CommandInfo
.Desktop
= Desktop
;
430 CommandInfo
.DesktopLen
= sizeof(Desktop
);
431 CommandInfo
.Title
= Title
;
432 CommandInfo
.TitleLen
= sizeof(Title
);
433 CommandInfo
.Env
= Env
;
434 CommandInfo
.EnvLen
= EnvSize
;
436 if (First
) CommandInfo
.VDMState
|= VDM_FLAG_FIRST_TASK
;
438 if (!GetNextVDMCommand(&CommandInfo
))
440 if (CommandInfo
.EnvLen
> EnvSize
)
442 /* Expand the environment size */
443 EnvSize
= CommandInfo
.EnvLen
;
444 Env
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, Env
, EnvSize
);
452 /* Start the process from the command line */
453 DPRINT1("Starting '%s' ('%s')...\n", AppName
, CmdLine
);
454 Result
= DosStartProcess(AppName
, CmdLine
, Env
);
455 if (Result
!= ERROR_SUCCESS
)
457 DisplayMessage(L
"Could not start '%S'. Error: %u", AppName
, Result
);
464 while (AcceptCommands
);
466 HeapFree(GetProcessHeap(), 0, Env
);
472 wmain(INT argc
, WCHAR
*argv
[])
477 CHAR ApplicationName
[MAX_PATH
];
478 CHAR CommandLine
[DOS_CMDLINE_LENGTH
];
482 WideCharToMultiByte(CP_ACP
, 0, argv
[1], -1, ApplicationName
, sizeof(ApplicationName
), NULL
, NULL
);
484 if (argc
>= 3) WideCharToMultiByte(CP_ACP
, 0, argv
[2], -1, CommandLine
, sizeof(CommandLine
), NULL
, NULL
);
485 else strcpy(CommandLine
, "");
489 wprintf(L
"\nReactOS Virtual DOS Machine\n\n"
490 L
"Usage: NTVDM <executable> [<parameters>]\n");
499 /* Parse the command line arguments */
500 for (i
= 1; i
< argc
; i
++)
502 if (wcsncmp(argv
[i
], L
"-i", 2) == 0)
504 /* This is the session ID */
505 SessionId
= wcstoul(argv
[i
] + 2, &endptr
, 10);
507 /* The VDM hasn't been started from a console, so quit when the task is done */
508 AcceptCommands
= FALSE
;
514 DPRINT1("\n\n\nNTVDM - Starting...\n\n\n");
516 /* Create the task event */
517 VdmTaskEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
518 ASSERT(VdmTaskEvent
!= NULL
);
520 /* Initialize the console */
523 wprintf(L
"FATAL: A problem occurred when trying to initialize the console\n");
527 /* Initialize the emulator */
528 if (!EmulatorInitialize(ConsoleInput
, ConsoleOutput
))
530 wprintf(L
"FATAL: Failed to initialize the emulator\n");
534 /* Initialize the system BIOS */
535 if (!BiosInitialize(NULL
))
537 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
541 /* Initialize the VDM DOS kernel */
542 if (!DosInitialize(NULL
))
544 wprintf(L
"FATAL: Failed to initialize the VDM DOS kernel.\n");
550 /* Create the GetNextVDMCommand thread */
551 CommandThread
= CreateThread(NULL
, 0, &CommandThreadProc
, NULL
, 0, NULL
);
552 if (CommandThread
== NULL
)
554 wprintf(L
"FATAL: Failed to create the command processing thread: %d\n", GetLastError());
558 /* Wait for the command thread to exit */
559 WaitForSingleObject(CommandThread
, INFINITE
);
561 /* Close the thread handle */
562 CloseHandle(CommandThread
);
566 /* Start the process from the command line */
567 DPRINT1("Starting '%s' ('%s')...\n", ApplicationName
, CommandLine
);
568 Result
= DosStartProcess(ApplicationName
,
570 GetEnvironmentStrings());
571 if (Result
!= ERROR_SUCCESS
)
573 DisplayMessage(L
"Could not start '%S'. Error: %u", ApplicationName
, Result
);
589 DPRINT1("\n\n\nNTVDM - Exiting...\n\n\n");