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 *******************************************************************/
16 #include "bios/bios.h"
21 /* VARIABLES ******************************************************************/
23 static HANDLE ConsoleInput
= INVALID_HANDLE_VALUE
;
24 static HANDLE ConsoleOutput
= INVALID_HANDLE_VALUE
;
25 static DWORD OrgConsoleInputMode
, OrgConsoleOutputMode
;
27 HANDLE VdmTaskEvent
= NULL
;
29 // Command line of NTVDM
34 static HMENU hConsoleMenu
= NULL
;
35 static INT VdmMenuPos
= -1;
36 static BOOLEAN ShowPointer
= FALSE
;
39 * Those menu helpers were taken from the GUI frontend in winsrv.dll
41 typedef struct _VDM_MENUITEM
44 const struct _VDM_MENUITEM
*SubMenu
;
46 } VDM_MENUITEM
, *PVDM_MENUITEM
;
48 static const VDM_MENUITEM VdmMenuItems
[] =
50 { IDS_VDM_DUMPMEM_TXT
, NULL
, ID_VDM_DUMPMEM_TXT
},
51 { IDS_VDM_DUMPMEM_BIN
, NULL
, ID_VDM_DUMPMEM_BIN
},
52 { IDS_VDM_QUIT
, NULL
, ID_VDM_QUIT
},
54 { 0, NULL
, 0 } /* End of list */
57 static const VDM_MENUITEM VdmMainMenuItems
[] =
59 { -1, NULL
, 0 }, /* Separator */
60 { IDS_HIDE_MOUSE
, NULL
, ID_SHOWHIDE_MOUSE
}, /* Hide mouse; can be renamed to Show mouse */
61 { IDS_VDM_MENU
, VdmMenuItems
, 0 }, /* ReactOS VDM Menu */
63 { 0, NULL
, 0 } /* End of list */
67 AppendMenuItems(HMENU hMenu
,
68 const VDM_MENUITEM
*Items
)
71 WCHAR szMenuString
[255];
76 if (Items
[i
].uID
!= (UINT
)-1)
78 if (LoadStringW(GetModuleHandle(NULL
),
81 ARRAYSIZE(szMenuString
)) > 0)
83 if (Items
[i
].SubMenu
!= NULL
)
85 hSubMenu
= CreatePopupMenu();
88 AppendMenuItems(hSubMenu
, Items
[i
].SubMenu
);
90 if (!AppendMenuW(hMenu
,
95 DestroyMenu(hSubMenu
);
116 } while (!(Items
[i
].uID
== 0 && Items
[i
].SubMenu
== NULL
&& Items
[i
].wCmdID
== 0));
120 VdmMenuExists(HMENU hConsoleMenu
)
123 MenuPos
= GetMenuItemCount(hConsoleMenu
);
125 /* Check for the presence of one of the VDM menu items */
126 for (i
= 0; i
<= MenuPos
; i
++)
128 if (GetMenuItemID(hConsoleMenu
, i
) == ID_SHOWHIDE_MOUSE
)
135 CreateVdmMenu(HANDLE ConOutHandle
)
137 hConsoleMenu
= ConsoleMenuControl(ConOutHandle
,
140 if (hConsoleMenu
== NULL
) return;
142 /* Get the position where we are going to insert our menu items */
143 VdmMenuPos
= GetMenuItemCount(hConsoleMenu
);
144 // FIXME: What happens if the menu already exist?
145 // VdmMenuPos points *after* the already existing menu!
147 /* Really add the menu if it doesn't already exist (in case eg. NTVDM crashed) */
148 if (!VdmMenuExists(hConsoleMenu
))
150 AppendMenuItems(hConsoleMenu
, VdmMainMenuItems
);
151 DrawMenuBar(GetConsoleWindow());
159 const VDM_MENUITEM
*Items
= VdmMainMenuItems
;
163 DeleteMenu(hConsoleMenu
, VdmMenuPos
, MF_BYPOSITION
);
165 } while (!(Items
[i
].uID
== 0 && Items
[i
].SubMenu
== NULL
&& Items
[i
].wCmdID
== 0));
167 DrawMenuBar(GetConsoleWindow());
170 static VOID
ShowHideMousePointer(HANDLE ConOutHandle
, BOOLEAN ShowPtr
)
172 WCHAR szMenuString
[255] = L
"";
176 /* Be sure the cursor will be shown */
177 while (ShowConsoleCursor(ConOutHandle
, TRUE
) < 0) ;
181 /* Be sure the cursor will be hidden */
182 while (ShowConsoleCursor(ConOutHandle
, FALSE
) >= 0) ;
185 if (LoadStringW(GetModuleHandle(NULL
),
186 (!ShowPtr
? IDS_SHOW_MOUSE
: IDS_HIDE_MOUSE
),
188 ARRAYSIZE(szMenuString
)) > 0)
190 ModifyMenu(hConsoleMenu
, ID_SHOWHIDE_MOUSE
,
191 MF_BYCOMMAND
, ID_SHOWHIDE_MOUSE
, szMenuString
);
195 static VOID
EnableExtraHardware(HANDLE ConsoleInput
)
199 if (GetConsoleMode(ConsoleInput
, &ConInMode
))
202 // GetNumberOfConsoleMouseButtons();
203 // GetSystemMetrics(SM_CMOUSEBUTTONS);
204 // GetSystemMetrics(SM_MOUSEPRESENT);
208 /* Support mouse input events if there is a mouse on the system */
209 ConInMode
|= ENABLE_MOUSE_INPUT
;
214 /* Do not support mouse input events if there is no mouse on the system */
215 ConInMode
&= ~ENABLE_MOUSE_INPUT
;
219 SetConsoleMode(ConsoleInput
, ConInMode
);
223 /* PUBLIC FUNCTIONS ***********************************************************/
226 DisplayMessage(LPCWSTR Format
, ...)
228 #ifndef WIN2K_COMPLIANT
229 WCHAR StaticBuffer
[256];
230 LPWSTR Buffer
= StaticBuffer
; // Use the static buffer by default.
232 WCHAR Buffer
[2048]; // Large enough. If not, increase it by hand.
237 va_start(Parameters
, Format
);
239 #ifndef WIN2K_COMPLIANT
241 * Retrieve the message length and if it is too long, allocate
242 * an auxiliary buffer; otherwise use the static buffer.
244 MsgLen
= _vscwprintf(Format
, Parameters
) + 1; // NULL-terminated
245 if (MsgLen
> ARRAYSIZE(StaticBuffer
))
247 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY
, MsgLen
* sizeof(WCHAR
));
250 /* Allocation failed, use the static buffer and display a suitable error message */
251 Buffer
= StaticBuffer
;
252 Format
= L
"DisplayMessage()\nOriginal message is too long and allocating an auxiliary buffer failed.";
253 MsgLen
= wcslen(Format
);
257 MsgLen
= ARRAYSIZE(Buffer
);
260 /* Display the message */
261 _vsnwprintf(Buffer
, MsgLen
, Format
, Parameters
);
262 DPRINT1("\n\nNTVDM Subsystem\n%S\n\n", Buffer
);
263 MessageBoxW(NULL
, Buffer
, L
"NTVDM Subsystem", MB_OK
);
265 #ifndef WIN2K_COMPLIANT
266 /* Free the buffer if needed */
267 if (Buffer
!= StaticBuffer
) RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
275 ConsoleCtrlHandler(DWORD ControlType
)
277 // HACK: Should be removed!
279 extern BOOLEAN AcceptCommands
;
280 extern HANDLE CommandThread
;
285 case CTRL_LAST_CLOSE_EVENT
:
287 if (WaitForSingleObject(VdmTaskEvent
, 0) == WAIT_TIMEOUT
)
289 /* Nothing runs, so exit immediately */
291 if (CommandThread
) TerminateThread(CommandThread
, 0);
298 /* A command is running, let it run, but stop accepting new commands */
299 AcceptCommands
= FALSE
;
308 /* Stop the VDM if the user logs out or closes the console */
318 CreateVdmMenu(ConsoleOutput
);
322 ConsoleCleanupUI(VOID
)
324 /* Display again properly the mouse pointer */
325 if (ShowPointer
) ShowHideMousePointer(ConsoleOutput
, ShowPointer
);
333 /* Save the original input and output console modes */
334 if (!GetConsoleMode(ConsoleInput
, &OrgConsoleInputMode
) ||
335 !GetConsoleMode(ConsoleOutput
, &OrgConsoleOutputMode
))
337 CloseHandle(ConsoleOutput
);
338 CloseHandle(ConsoleInput
);
339 wprintf(L
"FATAL: Cannot save console in/out modes\n");
343 /* Set the console input mode */
344 // FIXME: Activate ENABLE_WINDOW_INPUT when we will want to perform actions
345 // upon console window events (screen buffer resize, ...).
346 SetConsoleMode(ConsoleInput
, 0 /* | ENABLE_WINDOW_INPUT */);
347 EnableExtraHardware(ConsoleInput
);
349 /* Set the console output mode */
350 // SetConsoleMode(ConsoleOutput, ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
352 /* Initialize the UI */
361 /* Restore the original input and output console modes */
362 SetConsoleMode(ConsoleOutput
, OrgConsoleOutputMode
);
363 SetConsoleMode(ConsoleInput
, OrgConsoleInputMode
);
372 /* Set the handler routine */
373 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
375 /* Enable the CTRL_LAST_CLOSE_EVENT */
376 SetLastConsoleEventActive();
379 * NOTE: The CONIN$ and CONOUT$ "virtual" files
380 * always point to non-redirected console handles.
383 /* Get the input handle to the real console, and check for success */
384 ConsoleInput
= CreateFileW(L
"CONIN$",
385 GENERIC_READ
| GENERIC_WRITE
,
386 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
391 if (ConsoleInput
== INVALID_HANDLE_VALUE
)
393 wprintf(L
"FATAL: Cannot retrieve a handle to the console input\n");
397 /* Get the output handle to the real console, and check for success */
398 ConsoleOutput
= CreateFileW(L
"CONOUT$",
399 GENERIC_READ
| GENERIC_WRITE
,
400 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
405 if (ConsoleOutput
== INVALID_HANDLE_VALUE
)
407 CloseHandle(ConsoleInput
);
408 wprintf(L
"FATAL: Cannot retrieve a handle to the console output\n");
412 /* Effectively attach to the console */
413 return ConsoleAttach();
419 /* Detach from the console */
422 /* Close the console handles */
423 if (ConsoleOutput
!= INVALID_HANDLE_VALUE
) CloseHandle(ConsoleOutput
);
424 if (ConsoleInput
!= INVALID_HANDLE_VALUE
) CloseHandle(ConsoleInput
);
427 VOID
MenuEventHandler(PMENU_EVENT_RECORD MenuEvent
)
429 switch (MenuEvent
->dwCommandId
)
431 case ID_SHOWHIDE_MOUSE
:
432 ShowHideMousePointer(ConsoleOutput
, ShowPointer
);
433 ShowPointer
= !ShowPointer
;
436 case ID_VDM_DUMPMEM_TXT
:
440 case ID_VDM_DUMPMEM_BIN
:
454 VOID
FocusEventHandler(PFOCUS_EVENT_RECORD FocusEvent
)
456 DPRINT1("Focus events not handled\n");
460 LoadGlobalSettings(VOID
)
462 // FIXME: These strings should be localized.
463 #define ERROR_MEMORYVDD L"Insufficient memory to load installable Virtual Device Drivers."
464 #define ERROR_REGVDD L"Virtual Device Driver format in the registry is invalid."
465 #define ERROR_LOADVDD L"An installable Virtual Device Driver failed Dll initialization."
471 LPCWSTR NTVDMKeyName
= L
"SYSTEM\\CurrentControlSet\\Control\\NTVDM";
473 /* Try to open the NTVDM registry key */
474 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
479 if (Error
== ERROR_FILE_NOT_FOUND
)
481 /* If the key just doesn't exist, don't do anything else */
484 else if (Error
!= ERROR_SUCCESS
)
486 /* The key exists but there was an access error: display an error and quit */
487 DisplayMessage(ERROR_REGVDD
);
503 RegCloseKey(hNTVDMKey
);
508 wmain(INT argc
, WCHAR
*argv
[])
517 wprintf(L
"\nReactOS Virtual DOS Machine\n\n"
518 L
"Usage: NTVDM <executable> [<parameters>]\n");
524 /* Load global VDM settings */
525 LoadGlobalSettings();
527 DPRINT1("\n\n\nNTVDM - Starting...\n\n\n");
529 /* Create the task event */
530 VdmTaskEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
531 ASSERT(VdmTaskEvent
!= NULL
);
533 /* Initialize the console */
536 wprintf(L
"FATAL: A problem occurred when trying to initialize the console\n");
540 /* Initialize the emulator */
541 if (!EmulatorInitialize(ConsoleInput
, ConsoleOutput
))
543 wprintf(L
"FATAL: Failed to initialize the emulator\n");
547 /* Initialize the system BIOS */
548 if (!BiosInitialize(NULL
))
550 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
554 /* Let's go! Start simulation */
567 DPRINT1("\n\n\nNTVDM - Exiting...\n\n\n");
568 /* Some VDDs rely on the fact that NTVDM calls ExitProcess on Windows */