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 *******************************************************************/
18 #include "hardware/ps2.h"
19 #include "hardware/vga.h"
20 #include "bios/bios.h"
25 /* VARIABLES ******************************************************************/
27 static HANDLE ConsoleInput
= INVALID_HANDLE_VALUE
;
28 static HANDLE ConsoleOutput
= INVALID_HANDLE_VALUE
;
29 static DWORD OrgConsoleInputMode
, OrgConsoleOutputMode
;
33 BOOLEAN AcceptCommands
= TRUE
;
34 HANDLE CommandThread
= NULL
;
38 HANDLE VdmTaskEvent
= NULL
;
40 // Command line of NTVDM
45 static HMENU hConsoleMenu
= NULL
;
46 static INT VdmMenuPos
= -1;
47 static BOOLEAN ShowPointer
= FALSE
;
50 * Those menu helpers were taken from the GUI frontend in winsrv.dll
52 typedef struct _VDM_MENUITEM
55 const struct _VDM_MENUITEM
*SubMenu
;
57 } VDM_MENUITEM
, *PVDM_MENUITEM
;
59 static const VDM_MENUITEM VdmMenuItems
[] =
61 { IDS_VDM_DUMPMEM_TXT
, NULL
, ID_VDM_DUMPMEM_TXT
},
62 { IDS_VDM_DUMPMEM_BIN
, NULL
, ID_VDM_DUMPMEM_BIN
},
63 { IDS_VDM_QUIT
, NULL
, ID_VDM_QUIT
},
65 { 0, NULL
, 0 } /* End of list */
68 static const VDM_MENUITEM VdmMainMenuItems
[] =
70 { -1, NULL
, 0 }, /* Separator */
71 { IDS_HIDE_MOUSE
, NULL
, ID_SHOWHIDE_MOUSE
}, /* Hide mouse; can be renamed to Show mouse */
72 { IDS_VDM_MENU
, VdmMenuItems
, 0 }, /* ReactOS VDM Menu */
74 { 0, NULL
, 0 } /* End of list */
78 AppendMenuItems(HMENU hMenu
,
79 const VDM_MENUITEM
*Items
)
82 WCHAR szMenuString
[255];
87 if (Items
[i
].uID
!= (UINT
)-1)
89 if (LoadStringW(GetModuleHandle(NULL
),
92 sizeof(szMenuString
) / sizeof(szMenuString
[0])) > 0)
94 if (Items
[i
].SubMenu
!= NULL
)
96 hSubMenu
= CreatePopupMenu();
99 AppendMenuItems(hSubMenu
, Items
[i
].SubMenu
);
101 if (!AppendMenuW(hMenu
,
102 MF_STRING
| MF_POPUP
,
106 DestroyMenu(hSubMenu
);
127 } while (!(Items
[i
].uID
== 0 && Items
[i
].SubMenu
== NULL
&& Items
[i
].wCmdID
== 0));
131 CreateVdmMenu(HANDLE ConOutHandle
)
133 hConsoleMenu
= ConsoleMenuControl(ConOutHandle
,
136 if (hConsoleMenu
== NULL
) return;
138 VdmMenuPos
= GetMenuItemCount(hConsoleMenu
);
139 AppendMenuItems(hConsoleMenu
, VdmMainMenuItems
);
140 DrawMenuBar(GetConsoleWindow());
147 const VDM_MENUITEM
*Items
= VdmMainMenuItems
;
151 DeleteMenu(hConsoleMenu
, VdmMenuPos
, MF_BYPOSITION
);
153 } while (!(Items
[i
].uID
== 0 && Items
[i
].SubMenu
== NULL
&& Items
[i
].wCmdID
== 0));
155 DrawMenuBar(GetConsoleWindow());
158 static VOID
ShowHideMousePointer(HANDLE ConOutHandle
, BOOLEAN ShowPtr
)
160 WCHAR szMenuString
[255] = L
"";
164 /* Be sure the cursor will be shown */
165 while (ShowConsoleCursor(ConOutHandle
, TRUE
) < 0) ;
169 /* Be sure the cursor will be hidden */
170 while (ShowConsoleCursor(ConOutHandle
, FALSE
) >= 0) ;
173 if (LoadStringW(GetModuleHandle(NULL
),
174 (!ShowPtr
? IDS_SHOW_MOUSE
: IDS_HIDE_MOUSE
),
176 sizeof(szMenuString
) / sizeof(szMenuString
[0])) > 0)
178 ModifyMenu(hConsoleMenu
, ID_SHOWHIDE_MOUSE
,
179 MF_BYCOMMAND
, ID_SHOWHIDE_MOUSE
, szMenuString
);
183 /* PUBLIC FUNCTIONS ***********************************************************/
186 DisplayMessage(LPCWSTR Format
, ...)
188 #ifndef WIN2K_COMPLIANT
189 WCHAR StaticBuffer
[256];
190 LPWSTR Buffer
= StaticBuffer
; // Use the static buffer by default.
192 WCHAR Buffer
[2048]; // Large enough. If not, increase it by hand.
197 va_start(Parameters
, Format
);
199 #ifndef WIN2K_COMPLIANT
201 * Retrieve the message length and if it is too long, allocate
202 * an auxiliary buffer; otherwise use the static buffer.
204 MsgLen
= _vscwprintf(Format
, Parameters
) + 1; // NULL-terminated
205 if (MsgLen
> sizeof(StaticBuffer
)/sizeof(StaticBuffer
[0]))
207 Buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, MsgLen
* sizeof(WCHAR
));
210 /* Allocation failed, use the static buffer and display a suitable error message */
211 Buffer
= StaticBuffer
;
212 Format
= L
"DisplayMessage()\nOriginal message is too long and allocating an auxiliary buffer failed.";
213 MsgLen
= wcslen(Format
);
217 MsgLen
= sizeof(Buffer
)/sizeof(Buffer
[0]);
220 /* Display the message */
221 _vsnwprintf(Buffer
, MsgLen
, Format
, Parameters
);
222 DPRINT1("\n\nNTVDM Subsystem\n%S\n\n", Buffer
);
223 MessageBoxW(NULL
, Buffer
, L
"NTVDM Subsystem", MB_OK
);
225 #ifndef WIN2K_COMPLIANT
226 /* Free the buffer if needed */
227 if (Buffer
!= StaticBuffer
) HeapFree(GetProcessHeap(), 0, Buffer
);
235 ConsoleCtrlHandler(DWORD ControlType
)
240 case CTRL_BREAK_EVENT
:
242 /* HACK: Stop the VDM */
243 DPRINT1("Ctrl-C/Break: Stop the VDM\n");
247 case CTRL_LAST_CLOSE_EVENT
:
249 if (WaitForSingleObject(VdmTaskEvent
, 0) == WAIT_TIMEOUT
)
251 /* Exit immediately */
253 if (CommandThread
) TerminateThread(CommandThread
, 0);
260 /* Stop accepting new commands */
261 AcceptCommands
= FALSE
;
269 /* Stop the VDM if the user logs out or closes the console */
279 CreateVdmMenu(ConsoleOutput
);
283 ConsoleCleanupUI(VOID
)
285 /* Display again properly the mouse pointer */
286 if (ShowPointer
) ShowHideMousePointer(ConsoleOutput
, ShowPointer
);
294 /* Save the original input and output console modes */
295 if (!GetConsoleMode(ConsoleInput
, &OrgConsoleInputMode
) ||
296 !GetConsoleMode(ConsoleOutput
, &OrgConsoleOutputMode
))
298 CloseHandle(ConsoleOutput
);
299 CloseHandle(ConsoleInput
);
300 wprintf(L
"FATAL: Cannot save console in/out modes\n");
304 /* Initialize the UI */
313 /* Restore the original input and output console modes */
314 SetConsoleMode(ConsoleOutput
, OrgConsoleOutputMode
);
315 SetConsoleMode(ConsoleInput
, OrgConsoleInputMode
);
324 /* Set the handler routine */
325 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
327 /* Enable the CTRL_LAST_CLOSE_EVENT */
328 SetLastConsoleEventActive();
331 * NOTE: The CONIN$ and CONOUT$ "virtual" files
332 * always point to non-redirected console handles.
335 /* Get the input handle to the real console, and check for success */
336 ConsoleInput
= CreateFileW(L
"CONIN$",
337 GENERIC_READ
| GENERIC_WRITE
,
338 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
343 if (ConsoleInput
== INVALID_HANDLE_VALUE
)
345 wprintf(L
"FATAL: Cannot retrieve a handle to the console input\n");
349 /* Get the output handle to the real console, and check for success */
350 ConsoleOutput
= CreateFileW(L
"CONOUT$",
351 GENERIC_READ
| GENERIC_WRITE
,
352 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
357 if (ConsoleOutput
== INVALID_HANDLE_VALUE
)
359 CloseHandle(ConsoleInput
);
360 wprintf(L
"FATAL: Cannot retrieve a handle to the console output\n");
364 /* Effectively attach to the console */
365 return ConsoleAttach();
371 /* Detach from the console */
374 /* Close the console handles */
375 if (ConsoleOutput
!= INVALID_HANDLE_VALUE
) CloseHandle(ConsoleOutput
);
376 if (ConsoleInput
!= INVALID_HANDLE_VALUE
) CloseHandle(ConsoleInput
);
379 VOID
MenuEventHandler(PMENU_EVENT_RECORD MenuEvent
)
381 switch (MenuEvent
->dwCommandId
)
383 case ID_SHOWHIDE_MOUSE
:
384 ShowHideMousePointer(ConsoleOutput
, ShowPointer
);
385 ShowPointer
= !ShowPointer
;
388 case ID_VDM_DUMPMEM_TXT
:
392 case ID_VDM_DUMPMEM_BIN
:
406 VOID
FocusEventHandler(PFOCUS_EVENT_RECORD FocusEvent
)
408 DPRINT1("Focus events not handled\n");
412 wmain(INT argc
, WCHAR
*argv
[])
418 wprintf(L
"\nReactOS Virtual DOS Machine\n\n"
419 L
"Usage: NTVDM <executable> [<parameters>]\n");
428 /* Parse the command line arguments */
429 for (i
= 1; i
< argc
; i
++)
431 if (wcsncmp(argv
[i
], L
"-i", 2) == 0)
433 /* This is the session ID */
434 SessionId
= wcstoul(argv
[i
] + 2, &endptr
, 10);
436 /* The VDM hasn't been started from a console, so quit when the task is done */
437 AcceptCommands
= FALSE
;
446 DPRINT1("\n\n\nNTVDM - Starting...\n\n\n");
448 /* Create the task event */
449 VdmTaskEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
450 ASSERT(VdmTaskEvent
!= NULL
);
452 /* Initialize the console */
455 wprintf(L
"FATAL: A problem occurred when trying to initialize the console\n");
459 /* Initialize the emulator */
460 if (!EmulatorInitialize(ConsoleInput
, ConsoleOutput
))
462 wprintf(L
"FATAL: Failed to initialize the emulator\n");
466 /* Initialize the system BIOS */
467 if (!BiosInitialize(NULL
))
469 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
473 /* Let's go! Start simulation */
486 DPRINT1("\n\n\nNTVDM - Exiting...\n\n\n");