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_TXT
, NULL
, ID_VDM_DUMPMEM_TXT
},
55 { IDS_VDM_DUMPMEM_BIN
, NULL
, ID_VDM_DUMPMEM_BIN
},
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(ConOutHandle
,
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 ***********************************************************/
179 DisplayMessage(LPCWSTR Format
, ...)
184 va_start(Parameters
, Format
);
185 _vsnwprintf(Buffer
, 256, Format
, Parameters
);
186 DPRINT1("\n\nNTVDM Subsystem\n%S\n\n", Buffer
);
187 MessageBoxW(NULL
, Buffer
, L
"NTVDM Subsystem", MB_OK
);
193 ConsoleCtrlHandler(DWORD ControlType
)
198 case CTRL_BREAK_EVENT
:
201 EmulatorInterrupt(0x23);
204 case CTRL_LAST_CLOSE_EVENT
:
206 if (WaitForSingleObject(VdmTaskEvent
, 0) == WAIT_TIMEOUT
)
208 /* Exit immediately */
209 if (CommandThread
) TerminateThread(CommandThread
, 0);
214 /* Stop accepting new commands */
215 AcceptCommands
= FALSE
;
222 /* Stop the VDM if the user logs out or closes the console */
232 CreateVdmMenu(ConsoleOutput
);
236 ConsoleCleanupUI(VOID
)
238 /* Display again properly the mouse pointer */
239 if (ShowPointer
) ShowHideMousePointer(ConsoleOutput
, ShowPointer
);
247 /* Save the original input and output console modes */
248 if (!GetConsoleMode(ConsoleInput
, &OrgConsoleInputMode
) ||
249 !GetConsoleMode(ConsoleOutput
, &OrgConsoleOutputMode
))
251 CloseHandle(ConsoleOutput
);
252 CloseHandle(ConsoleInput
);
253 wprintf(L
"FATAL: Cannot save console in/out modes\n");
257 /* Initialize the UI */
266 /* Restore the original input and output console modes */
267 SetConsoleMode(ConsoleOutput
, OrgConsoleOutputMode
);
268 SetConsoleMode(ConsoleInput
, OrgConsoleInputMode
);
277 /* Set the handler routine */
278 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
280 /* Enable the CTRL_LAST_CLOSE_EVENT */
281 SetLastConsoleEventActive();
284 * NOTE: The CONIN$ and CONOUT$ "virtual" files
285 * always point to non-redirected console handles.
288 /* Get the input handle to the real console, and check for success */
289 ConsoleInput
= CreateFileW(L
"CONIN$",
290 GENERIC_READ
| GENERIC_WRITE
,
291 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
296 if (ConsoleInput
== INVALID_HANDLE_VALUE
)
298 wprintf(L
"FATAL: Cannot retrieve a handle to the console input\n");
302 /* Get the output handle to the real console, and check for success */
303 ConsoleOutput
= CreateFileW(L
"CONOUT$",
304 GENERIC_READ
| GENERIC_WRITE
,
305 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
310 if (ConsoleOutput
== INVALID_HANDLE_VALUE
)
312 CloseHandle(ConsoleInput
);
313 wprintf(L
"FATAL: Cannot retrieve a handle to the console output\n");
317 /* Effectively attach to the console */
318 return ConsoleAttach();
324 /* Detach from the console */
327 /* Close the console handles */
328 if (ConsoleOutput
!= INVALID_HANDLE_VALUE
) CloseHandle(ConsoleOutput
);
329 if (ConsoleInput
!= INVALID_HANDLE_VALUE
) CloseHandle(ConsoleInput
);
332 VOID
MenuEventHandler(PMENU_EVENT_RECORD MenuEvent
)
334 switch (MenuEvent
->dwCommandId
)
336 case ID_SHOWHIDE_MOUSE
:
337 ShowHideMousePointer(ConsoleOutput
, ShowPointer
);
338 ShowPointer
= !ShowPointer
;
341 case ID_VDM_DUMPMEM_TXT
:
345 case ID_VDM_DUMPMEM_BIN
:
359 VOID
FocusEventHandler(PFOCUS_EVENT_RECORD FocusEvent
)
361 DPRINT1("Focus events not handled\n");
367 CommandThreadProc(LPVOID Parameter
)
369 BOOLEAN First
= TRUE
;
371 VDM_COMMAND_INFO CommandInfo
;
372 CHAR CmdLine
[MAX_PATH
];
373 CHAR AppName
[MAX_PATH
];
374 CHAR PifFile
[MAX_PATH
];
375 CHAR Desktop
[MAX_PATH
];
376 CHAR Title
[MAX_PATH
];
378 PVOID Env
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, EnvSize
);
380 UNREFERENCED_PARAMETER(Parameter
);
385 /* Clear the structure */
386 ZeroMemory(&CommandInfo
, sizeof(CommandInfo
));
388 /* Initialize the structure members */
389 CommandInfo
.TaskId
= SessionId
;
390 CommandInfo
.VDMState
= VDM_FLAG_DOS
;
391 CommandInfo
.CmdLine
= CmdLine
;
392 CommandInfo
.CmdLen
= sizeof(CmdLine
);
393 CommandInfo
.AppName
= AppName
;
394 CommandInfo
.AppLen
= sizeof(AppName
);
395 CommandInfo
.PifFile
= PifFile
;
396 CommandInfo
.PifLen
= sizeof(PifFile
);
397 CommandInfo
.Desktop
= Desktop
;
398 CommandInfo
.DesktopLen
= sizeof(Desktop
);
399 CommandInfo
.Title
= Title
;
400 CommandInfo
.TitleLen
= sizeof(Title
);
401 CommandInfo
.Env
= Env
;
402 CommandInfo
.EnvLen
= EnvSize
;
404 if (First
) CommandInfo
.VDMState
|= VDM_FLAG_FIRST_TASK
;
407 if (!GetNextVDMCommand(&CommandInfo
))
409 if (CommandInfo
.EnvLen
> EnvSize
)
411 /* Expand the environment size */
412 EnvSize
= CommandInfo
.EnvLen
;
413 Env
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, Env
, EnvSize
);
415 /* Repeat the request */
422 /* Start the process from the command line */
423 DPRINT1("Starting '%s' ('%s')...\n", AppName
, CmdLine
);
424 Result
= DosStartProcess(AppName
, CmdLine
, Env
);
425 if (Result
!= ERROR_SUCCESS
)
427 DisplayMessage(L
"Could not start '%S'. Error: %u", AppName
, Result
);
434 while (AcceptCommands
);
436 HeapFree(GetProcessHeap(), 0, Env
);
442 wmain(INT argc
, WCHAR
*argv
[])
447 CHAR ApplicationName
[MAX_PATH
];
448 CHAR CommandLine
[DOS_CMDLINE_LENGTH
];
452 WideCharToMultiByte(CP_ACP
, 0, argv
[1], -1, ApplicationName
, sizeof(ApplicationName
), NULL
, NULL
);
454 if (argc
>= 3) WideCharToMultiByte(CP_ACP
, 0, argv
[2], -1, CommandLine
, sizeof(CommandLine
), NULL
, NULL
);
455 else strcpy(CommandLine
, "");
459 wprintf(L
"\nReactOS Virtual DOS Machine\n\n"
460 L
"Usage: NTVDM <executable> [<parameters>]\n");
469 /* Parse the command line arguments */
470 for (i
= 1; i
< argc
; i
++)
472 if (wcsncmp(argv
[i
], L
"-i", 2) == 0)
474 /* This is the session ID */
475 SessionId
= wcstoul(argv
[i
] + 2, &endptr
, 10);
477 /* The VDM hasn't been started from a console, so quit when the task is done */
478 AcceptCommands
= FALSE
;
484 DPRINT1("\n\n\nNTVDM - Starting...\n\n\n");
486 /* Create the task event */
487 VdmTaskEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
488 ASSERT(VdmTaskEvent
!= NULL
);
490 /* Initialize the console */
493 wprintf(L
"FATAL: A problem occurred when trying to initialize the console\n");
497 /* Initialize the emulator */
498 if (!EmulatorInitialize(ConsoleInput
, ConsoleOutput
))
500 wprintf(L
"FATAL: Failed to initialize the emulator\n");
504 /* Initialize the system BIOS */
505 if (!BiosInitialize(NULL
))
507 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
511 /* Initialize the VDM DOS kernel */
512 if (!DosInitialize(NULL
))
514 wprintf(L
"FATAL: Failed to initialize the VDM DOS kernel.\n");
520 /* Create the GetNextVDMCommand thread */
521 CommandThread
= CreateThread(NULL
, 0, &CommandThreadProc
, NULL
, 0, NULL
);
522 if (CommandThread
== NULL
)
524 wprintf(L
"FATAL: Failed to create the command processing thread: %d\n", GetLastError());
528 /* Wait for the command thread to exit */
529 WaitForSingleObject(CommandThread
, INFINITE
);
531 /* Close the thread handle */
532 CloseHandle(CommandThread
);
536 /* Start the process from the command line */
537 DPRINT1("Starting '%s' ('%s')...\n", ApplicationName
, CommandLine
);
538 Result
= DosStartProcess(ApplicationName
,
540 GetEnvironmentStrings());
541 if (Result
!= ERROR_SUCCESS
)
543 DisplayMessage(L
"Could not start '%S'. Error: %u", ApplicationName
, Result
);
559 DPRINT1("\n\n\nNTVDM - Exiting...\n\n\n");