[NTVDM][KERNEL32]
[reactos.git] / subsystems / ntvdm / ntvdm.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: ntvdm.c
5 * PURPOSE: Virtual DOS Machine
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #define NDEBUG
12
13 #include "ntvdm.h"
14 #include "emulator.h"
15
16 #include "clock.h"
17 #include "hardware/ps2.h"
18 #include "hardware/vga.h"
19 #include "bios/bios.h"
20 #include "dos/dem.h"
21
22 #include "resource.h"
23
24 /*
25 * Activate this line if you want to run NTVDM in standalone mode with:
26 * ntvdm.exe <program>
27 */
28 // #define STANDALONE
29
30 /* VARIABLES ******************************************************************/
31
32 static HANDLE ConsoleInput = INVALID_HANDLE_VALUE;
33 static HANDLE ConsoleOutput = INVALID_HANDLE_VALUE;
34 static DWORD OrgConsoleInputMode, OrgConsoleOutputMode;
35 static BOOLEAN AcceptCommands = TRUE;
36 static HANDLE CommandThread = NULL;
37
38 static HMENU hConsoleMenu = NULL;
39 static INT VdmMenuPos = -1;
40 static BOOLEAN ShowPointer = FALSE;
41
42 ULONG SessionId = 0;
43 HANDLE VdmTaskEvent = NULL;
44
45 /*
46 * Those menu helpers were taken from the GUI frontend in winsrv.dll
47 */
48 typedef struct _VDM_MENUITEM
49 {
50 UINT uID;
51 const struct _VDM_MENUITEM *SubMenu;
52 WORD wCmdID;
53 } VDM_MENUITEM, *PVDM_MENUITEM;
54
55 static const VDM_MENUITEM VdmMenuItems[] =
56 {
57 { IDS_VDM_QUIT, NULL, ID_VDM_QUIT },
58
59 { 0, NULL, 0 } /* End of list */
60 };
61
62 static const VDM_MENUITEM VdmMainMenuItems[] =
63 {
64 { -1, NULL, 0 }, /* Separator */
65 { IDS_HIDE_MOUSE, NULL, ID_SHOWHIDE_MOUSE }, /* Hide mouse; can be renamed to Show mouse */
66 { IDS_VDM_MENU , VdmMenuItems, 0 }, /* ReactOS VDM Menu */
67
68 { 0, NULL, 0 } /* End of list */
69 };
70
71 static VOID
72 AppendMenuItems(HMENU hMenu,
73 const VDM_MENUITEM *Items)
74 {
75 UINT i = 0;
76 WCHAR szMenuString[255];
77 HMENU hSubMenu;
78
79 do
80 {
81 if (Items[i].uID != (UINT)-1)
82 {
83 if (LoadStringW(GetModuleHandle(NULL),
84 Items[i].uID,
85 szMenuString,
86 sizeof(szMenuString) / sizeof(szMenuString[0])) > 0)
87 {
88 if (Items[i].SubMenu != NULL)
89 {
90 hSubMenu = CreatePopupMenu();
91 if (hSubMenu != NULL)
92 {
93 AppendMenuItems(hSubMenu, Items[i].SubMenu);
94
95 if (!AppendMenuW(hMenu,
96 MF_STRING | MF_POPUP,
97 (UINT_PTR)hSubMenu,
98 szMenuString))
99 {
100 DestroyMenu(hSubMenu);
101 }
102 }
103 }
104 else
105 {
106 AppendMenuW(hMenu,
107 MF_STRING,
108 Items[i].wCmdID,
109 szMenuString);
110 }
111 }
112 }
113 else
114 {
115 AppendMenuW(hMenu,
116 MF_SEPARATOR,
117 0,
118 NULL);
119 }
120 i++;
121 } while (!(Items[i].uID == 0 && Items[i].SubMenu == NULL && Items[i].wCmdID == 0));
122 }
123
124 static VOID
125 CreateVdmMenu(HANDLE ConOutHandle)
126 {
127 hConsoleMenu = ConsoleMenuControl(ConsoleOutput,
128 ID_SHOWHIDE_MOUSE,
129 ID_VDM_QUIT);
130 if (hConsoleMenu == NULL) return;
131
132 VdmMenuPos = GetMenuItemCount(hConsoleMenu);
133 AppendMenuItems(hConsoleMenu, VdmMainMenuItems);
134 DrawMenuBar(GetConsoleWindow());
135 }
136
137 static VOID
138 DestroyVdmMenu(VOID)
139 {
140 UINT i = 0;
141 const VDM_MENUITEM *Items = VdmMainMenuItems;
142
143 do
144 {
145 DeleteMenu(hConsoleMenu, VdmMenuPos, MF_BYPOSITION);
146 i++;
147 } while (!(Items[i].uID == 0 && Items[i].SubMenu == NULL && Items[i].wCmdID == 0));
148
149 DrawMenuBar(GetConsoleWindow());
150 }
151
152 static VOID ShowHideMousePointer(HANDLE ConOutHandle, BOOLEAN ShowPtr)
153 {
154 WCHAR szMenuString[255] = L"";
155
156 if (ShowPtr)
157 {
158 /* Be sure the cursor will be shown */
159 while (ShowConsoleCursor(ConOutHandle, TRUE) < 0) ;
160 }
161 else
162 {
163 /* Be sure the cursor will be hidden */
164 while (ShowConsoleCursor(ConOutHandle, FALSE) >= 0) ;
165 }
166
167 if (LoadStringW(GetModuleHandle(NULL),
168 (!ShowPtr ? IDS_SHOW_MOUSE : IDS_HIDE_MOUSE),
169 szMenuString,
170 sizeof(szMenuString) / sizeof(szMenuString[0])) > 0)
171 {
172 ModifyMenu(hConsoleMenu, ID_SHOWHIDE_MOUSE,
173 MF_BYCOMMAND, ID_SHOWHIDE_MOUSE, szMenuString);
174 }
175 }
176
177 /* PUBLIC FUNCTIONS ***********************************************************/
178
179 VOID DisplayMessage(LPCWSTR Format, ...)
180 {
181 WCHAR Buffer[256];
182 va_list Parameters;
183
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);
188 va_end(Parameters);
189 }
190
191 BOOL WINAPI ConsoleCtrlHandler(DWORD ControlType)
192 {
193 switch (ControlType)
194 {
195 case CTRL_C_EVENT:
196 case CTRL_BREAK_EVENT:
197 {
198 /* Call INT 23h */
199 EmulatorInterrupt(0x23);
200 break;
201 }
202 case CTRL_LAST_CLOSE_EVENT:
203 {
204 if (WaitForSingleObject(VdmTaskEvent, 0) == WAIT_TIMEOUT)
205 {
206 /* Exit immediately */
207 if (CommandThread) TerminateThread(CommandThread, 0);
208 EmulatorTerminate();
209 }
210 else
211 {
212 /* Stop accepting new commands */
213 AcceptCommands = FALSE;
214 }
215
216 break;
217 }
218 default:
219 {
220 /* Stop the VDM if the user logs out or closes the console */
221 EmulatorTerminate();
222 }
223 }
224 return TRUE;
225 }
226
227 VOID ConsoleInitUI(VOID)
228 {
229 CreateVdmMenu(ConsoleOutput);
230 }
231
232 VOID ConsoleCleanupUI(VOID)
233 {
234 /* Display again properly the mouse pointer */
235 if (ShowPointer) ShowHideMousePointer(ConsoleOutput, ShowPointer);
236
237 DestroyVdmMenu();
238 }
239
240 DWORD WINAPI PumpConsoleInput(LPVOID Parameter)
241 {
242 HANDLE ConsoleInput = (HANDLE)Parameter;
243 INPUT_RECORD InputRecord;
244 DWORD Count;
245
246 while (VdmRunning)
247 {
248 /* Make sure the task event is signaled */
249 WaitForSingleObject(VdmTaskEvent, INFINITE);
250
251 /* Wait for an input record */
252 if (!ReadConsoleInput(ConsoleInput, &InputRecord, 1, &Count))
253 {
254 DWORD LastError = GetLastError();
255 DPRINT1("Error reading console input (0x%p, %lu) - Error %lu\n", ConsoleInput, Count, LastError);
256 return LastError;
257 }
258
259 ASSERT(Count != 0);
260
261 /* Check the event type */
262 switch (InputRecord.EventType)
263 {
264 case KEY_EVENT:
265 case MOUSE_EVENT:
266 /* Send it to the PS/2 controller */
267 PS2Dispatch(&InputRecord);
268 break;
269
270 case MENU_EVENT:
271 {
272 switch (InputRecord.Event.MenuEvent.dwCommandId)
273 {
274 case ID_SHOWHIDE_MOUSE:
275 ShowHideMousePointer(ConsoleOutput, ShowPointer);
276 ShowPointer = !ShowPointer;
277 break;
278
279 case ID_VDM_QUIT:
280 /* Stop the VDM */
281 EmulatorTerminate();
282 break;
283
284 default:
285 break;
286 }
287
288 break;
289 }
290
291 default:
292 break;
293 }
294 }
295
296 return 0;
297 }
298
299 BOOL ConsoleInit(VOID)
300 {
301 /* Set the handler routine */
302 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
303
304 /* Enable the CTRL_LAST_CLOSE_EVENT */
305 SetLastConsoleEventActive();
306
307 /* Get the input handle to the real console, and check for success */
308 ConsoleInput = CreateFileW(L"CONIN$",
309 GENERIC_READ | GENERIC_WRITE,
310 FILE_SHARE_READ | FILE_SHARE_WRITE,
311 NULL,
312 OPEN_EXISTING,
313 0,
314 NULL);
315 if (ConsoleInput == INVALID_HANDLE_VALUE)
316 {
317 wprintf(L"FATAL: Cannot retrieve a handle to the console input\n");
318 return FALSE;
319 }
320
321 /* Get the output handle to the real console, and check for success */
322 ConsoleOutput = CreateFileW(L"CONOUT$",
323 GENERIC_READ | GENERIC_WRITE,
324 FILE_SHARE_READ | FILE_SHARE_WRITE,
325 NULL,
326 OPEN_EXISTING,
327 0,
328 NULL);
329 if (ConsoleOutput == INVALID_HANDLE_VALUE)
330 {
331 CloseHandle(ConsoleInput);
332 wprintf(L"FATAL: Cannot retrieve a handle to the console output\n");
333 return FALSE;
334 }
335
336 /* Save the original input and output console modes */
337 if (!GetConsoleMode(ConsoleInput , &OrgConsoleInputMode ) ||
338 !GetConsoleMode(ConsoleOutput, &OrgConsoleOutputMode))
339 {
340 CloseHandle(ConsoleOutput);
341 CloseHandle(ConsoleInput);
342 wprintf(L"FATAL: Cannot save console in/out modes\n");
343 return FALSE;
344 }
345
346 /* Initialize the UI */
347 ConsoleInitUI();
348
349 return TRUE;
350 }
351
352 VOID ConsoleCleanup(VOID)
353 {
354 /* Restore the original input and output console modes */
355 SetConsoleMode(ConsoleOutput, OrgConsoleOutputMode);
356 SetConsoleMode(ConsoleInput , OrgConsoleInputMode );
357
358 /* Cleanup the UI */
359 ConsoleCleanupUI();
360
361 /* Close the console handles */
362 if (ConsoleOutput != INVALID_HANDLE_VALUE) CloseHandle(ConsoleOutput);
363 if (ConsoleInput != INVALID_HANDLE_VALUE) CloseHandle(ConsoleInput);
364 }
365
366 DWORD WINAPI CommandThreadProc(LPVOID Parameter)
367 {
368 BOOLEAN First = TRUE;
369 DWORD Result;
370 VDM_COMMAND_INFO CommandInfo;
371 CHAR CmdLine[MAX_PATH];
372 CHAR AppName[MAX_PATH];
373 CHAR PifFile[MAX_PATH];
374 CHAR Desktop[MAX_PATH];
375 CHAR Title[MAX_PATH];
376 CHAR Env[MAX_PATH];
377
378 UNREFERENCED_PARAMETER(Parameter);
379
380 while (AcceptCommands)
381 {
382 /* Clear the structure */
383 ZeroMemory(&CommandInfo, sizeof(CommandInfo));
384
385 /* Initialize the structure members */
386 CommandInfo.TaskId = SessionId;
387 CommandInfo.VDMState = VDM_FLAG_DOS;
388 CommandInfo.CmdLine = CmdLine;
389 CommandInfo.CmdLen = sizeof(CmdLine);
390 CommandInfo.AppName = AppName;
391 CommandInfo.AppLen = sizeof(AppName);
392 CommandInfo.PifFile = PifFile;
393 CommandInfo.PifLen = sizeof(PifFile);
394 CommandInfo.Desktop = Desktop;
395 CommandInfo.DesktopLen = sizeof(Desktop);
396 CommandInfo.Title = Title;
397 CommandInfo.TitleLen = sizeof(Title);
398 CommandInfo.Env = Env;
399 CommandInfo.EnvLen = sizeof(Env);
400
401 if (First) CommandInfo.VDMState |= VDM_FLAG_FIRST_TASK;
402
403 /* Wait for the next available VDM */
404 if (!GetNextVDMCommand(&CommandInfo)) break;
405
406 /* Start the process from the command line */
407 DPRINT1("Starting '%s'...\n", AppName);
408
409 Result = DosLoadExecutable(DOS_LOAD_AND_EXECUTE, AppName, CmdLine, Env, NULL, NULL);
410 if (Result != ERROR_SUCCESS)
411 {
412 DisplayMessage(L"Could not start '%S'. Error: %u", AppName, Result);
413 break;
414 }
415
416 /* Attach to the console */
417 if (!First) VgaAttachToConsole();
418
419 /* Perform a screen refresh */
420 VgaRefreshDisplay();
421
422 /* Start simulation */
423 SetEvent(VdmTaskEvent);
424 EmulatorSimulate();
425
426 /* Perform another screen refresh */
427 VgaRefreshDisplay();
428
429 /* Detach from the console */
430 VgaDetachFromConsole(FALSE);
431
432 First = FALSE;
433 }
434
435 return 0;
436 }
437
438 INT wmain(INT argc, WCHAR *argv[])
439 {
440 #ifdef STANDALONE
441
442 DWORD Result;
443 CHAR ApplicationName[MAX_PATH];
444 CHAR CommandLine[DOS_CMDLINE_LENGTH];
445
446 if (argc >= 2)
447 {
448 WideCharToMultiByte(CP_ACP, 0, argv[1], -1, ApplicationName, sizeof(ApplicationName), NULL, NULL);
449
450 if (argc >= 3) WideCharToMultiByte(CP_ACP, 0, argv[2], -1, CommandLine, sizeof(CommandLine), NULL, NULL);
451 else strcpy(CommandLine, "");
452 }
453 else
454 {
455 wprintf(L"\nReactOS Virtual DOS Machine\n\n"
456 L"Usage: NTVDM <executable> [<parameters>]\n");
457 return 0;
458 }
459
460 #else
461 INT i;
462 WCHAR *endptr;
463
464 /* Parse the command line arguments */
465 for (i = 1; i < argc; i++)
466 {
467 if (wcsncmp(argv[i], L"-i", 2) == 0)
468 {
469 /* This is the session ID */
470 SessionId = wcstoul(argv[i] + 2, &endptr, 10);
471 }
472 }
473
474 #endif
475
476 DPRINT1("\n\n\nNTVDM - Starting...\n\n\n");
477
478 /* Create the task event */
479 VdmTaskEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
480 ASSERT(VdmTaskEvent != NULL);
481
482 /* Initialize the console */
483 if (!ConsoleInit())
484 {
485 wprintf(L"FATAL: A problem occurred when trying to initialize the console\n");
486 goto Cleanup;
487 }
488
489 /* Initialize the emulator */
490 if (!EmulatorInitialize(ConsoleInput, ConsoleOutput))
491 {
492 wprintf(L"FATAL: Failed to initialize the emulator\n");
493 goto Cleanup;
494 }
495
496 /* Initialize the system BIOS */
497 if (!BiosInitialize(NULL))
498 {
499 wprintf(L"FATAL: Failed to initialize the VDM BIOS.\n");
500 goto Cleanup;
501 }
502
503 /* Initialize the VDM DOS kernel */
504 if (!DosInitialize(NULL))
505 {
506 wprintf(L"FATAL: Failed to initialize the VDM DOS kernel.\n");
507 goto Cleanup;
508 }
509
510 #ifndef STANDALONE
511
512 /* Create the GetNextVDMCommand thread */
513 CommandThread = CreateThread(NULL, 0, &CommandThreadProc, NULL, 0, NULL);
514 if (CommandThread == NULL)
515 {
516 wprintf(L"FATAL: Failed to create the command processing thread: %d\n", GetLastError());
517 goto Cleanup;
518 }
519
520 /* Wait for the command thread to exit */
521 WaitForSingleObject(CommandThread, INFINITE);
522
523 /* Close the thread handle */
524 CloseHandle(CommandThread);
525
526 #else
527
528 /* Start the process from the command line */
529 DPRINT1("Starting '%s'...\n", ApplicationName);
530
531 Result = DosLoadExecutable(DOS_LOAD_AND_EXECUTE,
532 ApplicationName,
533 CommandLine,
534 GetEnvironmentStrings(),
535 NULL,
536 NULL);
537 if (Result != ERROR_SUCCESS)
538 {
539 DisplayMessage(L"Could not start '%S'. Error: %u", ApplicationName, Result);
540 goto Cleanup;
541 }
542
543 /* Start simulation */
544 SetEvent(VdmTaskEvent);
545 EmulatorSimulate();
546
547 /* Perform another screen refresh */
548 VgaRefreshDisplay();
549
550 #endif
551
552 Cleanup:
553 BiosCleanup();
554 EmulatorCleanup();
555 ConsoleCleanup();
556
557 #ifndef STANDALONE
558 ExitVDM(FALSE, 0);
559 #endif
560
561 /* Quit the VDM */
562 DPRINT1("\n\n\nNTVDM - Exiting...\n\n\n");
563
564 return 0;
565 }
566
567 /* EOF */