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
)
130 /* set VdmMenuPos to the position of the existing menu */
139 CreateVdmMenu(HANDLE ConOutHandle
)
141 hConsoleMenu
= ConsoleMenuControl(ConOutHandle
,
144 if (hConsoleMenu
== NULL
) return;
146 /* Get the position where we are going to insert our menu items */
147 VdmMenuPos
= GetMenuItemCount(hConsoleMenu
);
149 /* Really add the menu if it doesn't already exist (in case eg. NTVDM crashed) */
150 if (!VdmMenuExists(hConsoleMenu
))
152 AppendMenuItems(hConsoleMenu
, VdmMainMenuItems
);
153 DrawMenuBar(GetConsoleWindow());
161 const VDM_MENUITEM
*Items
= VdmMainMenuItems
;
165 DeleteMenu(hConsoleMenu
, VdmMenuPos
, MF_BYPOSITION
);
167 } while (!(Items
[i
].uID
== 0 && Items
[i
].SubMenu
== NULL
&& Items
[i
].wCmdID
== 0));
169 DrawMenuBar(GetConsoleWindow());
172 static VOID
ShowHideMousePointer(HANDLE ConOutHandle
, BOOLEAN ShowPtr
)
174 WCHAR szMenuString
[255] = L
"";
178 /* Be sure the cursor will be shown */
179 while (ShowConsoleCursor(ConOutHandle
, TRUE
) < 0) ;
183 /* Be sure the cursor will be hidden */
184 while (ShowConsoleCursor(ConOutHandle
, FALSE
) >= 0) ;
187 if (LoadStringW(GetModuleHandle(NULL
),
188 (!ShowPtr
? IDS_SHOW_MOUSE
: IDS_HIDE_MOUSE
),
190 ARRAYSIZE(szMenuString
)) > 0)
192 ModifyMenu(hConsoleMenu
, ID_SHOWHIDE_MOUSE
,
193 MF_BYCOMMAND
, ID_SHOWHIDE_MOUSE
, szMenuString
);
197 static VOID
EnableExtraHardware(HANDLE ConsoleInput
)
201 if (GetConsoleMode(ConsoleInput
, &ConInMode
))
204 // GetNumberOfConsoleMouseButtons();
205 // GetSystemMetrics(SM_CMOUSEBUTTONS);
206 // GetSystemMetrics(SM_MOUSEPRESENT);
210 /* Support mouse input events if there is a mouse on the system */
211 ConInMode
|= ENABLE_MOUSE_INPUT
;
216 /* Do not support mouse input events if there is no mouse on the system */
217 ConInMode
&= ~ENABLE_MOUSE_INPUT
;
221 SetConsoleMode(ConsoleInput
, ConInMode
);
225 /* PUBLIC FUNCTIONS ***********************************************************/
228 DisplayMessage(LPCWSTR Format
, ...)
230 #ifndef WIN2K_COMPLIANT
231 WCHAR StaticBuffer
[256];
232 LPWSTR Buffer
= StaticBuffer
; // Use the static buffer by default.
234 WCHAR Buffer
[2048]; // Large enough. If not, increase it by hand.
239 va_start(Parameters
, Format
);
241 #ifndef WIN2K_COMPLIANT
243 * Retrieve the message length and if it is too long, allocate
244 * an auxiliary buffer; otherwise use the static buffer.
246 MsgLen
= _vscwprintf(Format
, Parameters
) + 1; // NULL-terminated
247 if (MsgLen
> ARRAYSIZE(StaticBuffer
))
249 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY
, MsgLen
* sizeof(WCHAR
));
252 /* Allocation failed, use the static buffer and display a suitable error message */
253 Buffer
= StaticBuffer
;
254 Format
= L
"DisplayMessage()\nOriginal message is too long and allocating an auxiliary buffer failed.";
255 MsgLen
= wcslen(Format
);
259 MsgLen
= ARRAYSIZE(Buffer
);
262 /* Display the message */
263 _vsnwprintf(Buffer
, MsgLen
, Format
, Parameters
);
264 DPRINT1("\n\nNTVDM Subsystem\n%S\n\n", Buffer
);
265 MessageBoxW(NULL
, Buffer
, L
"NTVDM Subsystem", MB_OK
);
267 #ifndef WIN2K_COMPLIANT
268 /* Free the buffer if needed */
269 if (Buffer
!= StaticBuffer
) RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
277 ConsoleCtrlHandler(DWORD ControlType
)
279 // HACK: Should be removed!
281 extern BOOLEAN AcceptCommands
;
282 extern HANDLE CommandThread
;
287 case CTRL_LAST_CLOSE_EVENT
:
289 if (WaitForSingleObject(VdmTaskEvent
, 0) == WAIT_TIMEOUT
)
291 /* Nothing runs, so exit immediately */
293 if (CommandThread
) TerminateThread(CommandThread
, 0);
300 /* A command is running, let it run, but stop accepting new commands */
301 AcceptCommands
= FALSE
;
310 /* Stop the VDM if the user logs out or closes the console */
320 CreateVdmMenu(ConsoleOutput
);
324 ConsoleCleanupUI(VOID
)
326 /* Display again properly the mouse pointer */
327 if (ShowPointer
) ShowHideMousePointer(ConsoleOutput
, ShowPointer
);
335 /* Save the original input and output console modes */
336 if (!GetConsoleMode(ConsoleInput
, &OrgConsoleInputMode
) ||
337 !GetConsoleMode(ConsoleOutput
, &OrgConsoleOutputMode
))
339 CloseHandle(ConsoleOutput
);
340 CloseHandle(ConsoleInput
);
341 wprintf(L
"FATAL: Cannot save console in/out modes\n");
345 /* Set the console input mode */
346 // FIXME: Activate ENABLE_WINDOW_INPUT when we will want to perform actions
347 // upon console window events (screen buffer resize, ...).
348 SetConsoleMode(ConsoleInput
, 0 /* | ENABLE_WINDOW_INPUT */);
349 EnableExtraHardware(ConsoleInput
);
351 /* Set the console output mode */
352 // SetConsoleMode(ConsoleOutput, ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
354 /* Initialize the UI */
363 /* Restore the original input and output console modes */
364 SetConsoleMode(ConsoleOutput
, OrgConsoleOutputMode
);
365 SetConsoleMode(ConsoleInput
, OrgConsoleInputMode
);
374 /* Set the handler routine */
375 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
377 /* Enable the CTRL_LAST_CLOSE_EVENT */
378 SetLastConsoleEventActive();
381 * NOTE: The CONIN$ and CONOUT$ "virtual" files
382 * always point to non-redirected console handles.
385 /* Get the input handle to the real console, and check for success */
386 ConsoleInput
= CreateFileW(L
"CONIN$",
387 GENERIC_READ
| GENERIC_WRITE
,
388 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
393 if (ConsoleInput
== INVALID_HANDLE_VALUE
)
395 wprintf(L
"FATAL: Cannot retrieve a handle to the console input\n");
399 /* Get the output handle to the real console, and check for success */
400 ConsoleOutput
= CreateFileW(L
"CONOUT$",
401 GENERIC_READ
| GENERIC_WRITE
,
402 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
407 if (ConsoleOutput
== INVALID_HANDLE_VALUE
)
409 CloseHandle(ConsoleInput
);
410 wprintf(L
"FATAL: Cannot retrieve a handle to the console output\n");
414 /* Effectively attach to the console */
415 return ConsoleAttach();
421 /* Detach from the console */
424 /* Close the console handles */
425 if (ConsoleOutput
!= INVALID_HANDLE_VALUE
) CloseHandle(ConsoleOutput
);
426 if (ConsoleInput
!= INVALID_HANDLE_VALUE
) CloseHandle(ConsoleInput
);
429 VOID
MenuEventHandler(PMENU_EVENT_RECORD MenuEvent
)
431 switch (MenuEvent
->dwCommandId
)
433 case ID_SHOWHIDE_MOUSE
:
434 ShowHideMousePointer(ConsoleOutput
, ShowPointer
);
435 ShowPointer
= !ShowPointer
;
438 case ID_VDM_DUMPMEM_TXT
:
442 case ID_VDM_DUMPMEM_BIN
:
456 VOID
FocusEventHandler(PFOCUS_EVENT_RECORD FocusEvent
)
458 DPRINT1("Focus events not handled\n");
462 LoadGlobalSettings(VOID
)
464 // FIXME: These strings should be localized.
465 #define ERROR_MEMORYVDD L"Insufficient memory to load installable Virtual Device Drivers."
466 #define ERROR_REGVDD L"Virtual Device Driver format in the registry is invalid."
467 #define ERROR_LOADVDD L"An installable Virtual Device Driver failed Dll initialization."
473 LPCWSTR NTVDMKeyName
= L
"SYSTEM\\CurrentControlSet\\Control\\NTVDM";
475 /* Try to open the NTVDM registry key */
476 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
481 if (Error
== ERROR_FILE_NOT_FOUND
)
483 /* If the key just doesn't exist, don't do anything else */
486 else if (Error
!= ERROR_SUCCESS
)
488 /* The key exists but there was an access error: display an error and quit */
489 DisplayMessage(ERROR_REGVDD
);
505 RegCloseKey(hNTVDMKey
);
510 wmain(INT argc
, WCHAR
*argv
[])
519 wprintf(L
"\nReactOS Virtual DOS Machine\n\n"
520 L
"Usage: NTVDM <executable> [<parameters>]\n");
526 /* Load global VDM settings */
527 LoadGlobalSettings();
529 DPRINT1("\n\n\nNTVDM - Starting...\n\n\n");
531 /* Create the task event */
532 VdmTaskEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
533 ASSERT(VdmTaskEvent
!= NULL
);
535 /* Initialize the console */
538 wprintf(L
"FATAL: A problem occurred when trying to initialize the console\n");
542 /* Initialize the emulator */
543 if (!EmulatorInitialize(ConsoleInput
, ConsoleOutput
))
545 wprintf(L
"FATAL: Failed to initialize the emulator\n");
549 /* Initialize the system BIOS */
550 if (!BiosInitialize(NULL
))
552 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
556 /* Let's go! Start simulation */
569 DPRINT1("\n\n\nNTVDM - Exiting...\n\n\n");
570 /* Some VDDs rely on the fact that NTVDM calls ExitProcess on Windows */