[BASESRV][KERNEL32][NTVDM]
[reactos.git] / subsystems / ntvdm / ntvdm.c
index 004c42e..c6cc701 100644 (file)
 #include "ntvdm.h"
 #include "emulator.h"
 
-#include "bios/bios.h"
-#include "dos/dem.h"
-#include "hardware/cmos.h"
+#include "clock.h"
 #include "hardware/ps2.h"
-#include "hardware/timer.h"
 #include "hardware/vga.h"
+#include "bios/bios.h"
+#include "dos/dem.h"
+
+#include "resource.h"
 
 /*
- * Activate this line if you want to be able to test NTVDM with:
+ * Activate this line if you want to run NTVDM in standalone mode with:
  * ntvdm.exe <program>
  */
-#define TESTING
+// #define STANDALONE
 
-#define IPS_DISPLAY
-
-/* PUBLIC VARIABLES ***********************************************************/
+/* VARIABLES ******************************************************************/
 
 static HANDLE ConsoleInput  = INVALID_HANDLE_VALUE;
 static HANDLE ConsoleOutput = INVALID_HANDLE_VALUE;
 static DWORD  OrgConsoleInputMode, OrgConsoleOutputMode;
 static CONSOLE_CURSOR_INFO         OrgConsoleCursorInfo;
 static CONSOLE_SCREEN_BUFFER_INFO  OrgConsoleBufferInfo;
+static BOOLEAN AcceptCommands = TRUE;
+static HANDLE CommandThread = NULL;
+
+static HMENU hConsoleMenu  = NULL;
+static INT   VdmMenuPos    = -1;
+static BOOLEAN ShowPointer = FALSE;
+
+/*
+ * Those menu helpers were taken from the GUI frontend in winsrv.dll
+ */
+typedef struct _VDM_MENUITEM
+{
+    UINT uID;
+    const struct _VDM_MENUITEM *SubMenu;
+    WORD wCmdID;
+} VDM_MENUITEM, *PVDM_MENUITEM;
+
+static const VDM_MENUITEM VdmMenuItems[] =
+{
+    { IDS_VDM_QUIT, NULL, ID_VDM_QUIT },
+
+    { 0, NULL, 0 }      /* End of list */
+};
+
+static const VDM_MENUITEM VdmMainMenuItems[] =
+{
+    { -1, NULL, 0 },    /* Separator */
+    { IDS_HIDE_MOUSE,   NULL, ID_SHOWHIDE_MOUSE },  /* Hide mouse; can be renamed to Show mouse */
+    { IDS_VDM_MENU  ,   VdmMenuItems,         0 },  /* ReactOS VDM Menu */
+
+    { 0, NULL, 0 }      /* End of list */
+};
+
+static VOID
+AppendMenuItems(HMENU hMenu,
+                const VDM_MENUITEM *Items)
+{
+    UINT i = 0;
+    WCHAR szMenuString[255];
+    HMENU hSubMenu;
+
+    do
+    {
+        if (Items[i].uID != (UINT)-1)
+        {
+            if (LoadStringW(GetModuleHandle(NULL),
+                            Items[i].uID,
+                            szMenuString,
+                            sizeof(szMenuString) / sizeof(szMenuString[0])) > 0)
+            {
+                if (Items[i].SubMenu != NULL)
+                {
+                    hSubMenu = CreatePopupMenu();
+                    if (hSubMenu != NULL)
+                    {
+                        AppendMenuItems(hSubMenu, Items[i].SubMenu);
+
+                        if (!AppendMenuW(hMenu,
+                                         MF_STRING | MF_POPUP,
+                                         (UINT_PTR)hSubMenu,
+                                         szMenuString))
+                        {
+                            DestroyMenu(hSubMenu);
+                        }
+                    }
+                }
+                else
+                {
+                    AppendMenuW(hMenu,
+                                MF_STRING,
+                                Items[i].wCmdID,
+                                szMenuString);
+                }
+            }
+        }
+        else
+        {
+            AppendMenuW(hMenu,
+                        MF_SEPARATOR,
+                        0,
+                        NULL);
+        }
+        i++;
+    } while (!(Items[i].uID == 0 && Items[i].SubMenu == NULL && Items[i].wCmdID == 0));
+}
+
+static VOID
+CreateVdmMenu(HANDLE ConOutHandle)
+{
+    hConsoleMenu = ConsoleMenuControl(ConsoleOutput,
+                                      ID_SHOWHIDE_MOUSE,
+                                      ID_VDM_QUIT);
+    if (hConsoleMenu == NULL) return;
+
+    VdmMenuPos = GetMenuItemCount(hConsoleMenu);
+    AppendMenuItems(hConsoleMenu, VdmMainMenuItems);
+    DrawMenuBar(GetConsoleWindow());
+}
+
+static VOID
+DestroyVdmMenu(VOID)
+{
+    UINT i = 0;
+    const VDM_MENUITEM *Items = VdmMainMenuItems;
+
+    do
+    {
+        DeleteMenu(hConsoleMenu, VdmMenuPos, MF_BYPOSITION);
+        i++;
+    } while (!(Items[i].uID == 0 && Items[i].SubMenu == NULL && Items[i].wCmdID == 0));
+
+    DrawMenuBar(GetConsoleWindow());
+}
+
+static VOID ShowHideMousePointer(HANDLE ConOutHandle, BOOLEAN ShowPtr)
+{
+    WCHAR szMenuString[255] = L"";
+
+    if (ShowPtr)
+    {
+        /* Be sure the cursor will be shown */
+        while (ShowConsoleCursor(ConOutHandle, TRUE) < 0) ;
+    }
+    else
+    {
+        /* Be sure the cursor will be hidden */
+        while (ShowConsoleCursor(ConOutHandle, FALSE) >= 0) ;
+    }
+
+    if (LoadStringW(GetModuleHandle(NULL),
+                    (!ShowPtr ? IDS_SHOW_MOUSE : IDS_HIDE_MOUSE),
+                    szMenuString,
+                    sizeof(szMenuString) / sizeof(szMenuString[0])) > 0)
+    {
+        ModifyMenu(hConsoleMenu, ID_SHOWHIDE_MOUSE,
+                   MF_BYCOMMAND, ID_SHOWHIDE_MOUSE, szMenuString);
+    }
+}
 
 /* PUBLIC FUNCTIONS ***********************************************************/
 
@@ -61,20 +198,107 @@ BOOL WINAPI ConsoleCtrlHandler(DWORD ControlType)
             EmulatorInterrupt(0x23);
             break;
         }
+        case CTRL_LAST_CLOSE_EVENT:
+        {
+            if (!VdmRunning)
+            {
+                /* Exit immediately */
+                if (CommandThread) TerminateThread(CommandThread, 0);
+            }
+            else
+            {
+                /* Stop accepting new commands */
+                AcceptCommands = FALSE;
+            }
+
+            break;
+        }
         default:
         {
             /* Stop the VDM if the user logs out or closes the console */
-            VdmRunning = FALSE;
+            EmulatorTerminate();
         }
     }
     return TRUE;
 }
 
+VOID ConsoleInitUI(VOID)
+{
+    CreateVdmMenu(ConsoleOutput);
+}
+
+VOID ConsoleCleanupUI(VOID)
+{
+    /* Display again properly the mouse pointer */
+    if (ShowPointer) ShowHideMousePointer(ConsoleOutput, ShowPointer);
+
+    DestroyVdmMenu();
+}
+
+DWORD WINAPI PumpConsoleInput(LPVOID Parameter)
+{
+    HANDLE ConsoleInput = (HANDLE)Parameter;
+    INPUT_RECORD InputRecord;
+    DWORD Count;
+
+    while (VdmRunning)
+    {
+        /* Wait for an input record */
+        if (!ReadConsoleInput(ConsoleInput, &InputRecord, 1, &Count))
+        {
+            DWORD LastError = GetLastError();
+            DPRINT1("Error reading console input (0x%p, %lu) - Error %lu\n", ConsoleInput, Count, LastError);
+            return LastError;
+        }
+
+        ASSERT(Count != 0);
+
+        /* Check the event type */
+        switch (InputRecord.EventType)
+        {
+            case KEY_EVENT:
+            case MOUSE_EVENT:
+                /* Send it to the PS/2 controller */
+                PS2Dispatch(&InputRecord);
+                break;
+
+            case MENU_EVENT:
+            {
+                switch (InputRecord.Event.MenuEvent.dwCommandId)
+                {
+                    case ID_SHOWHIDE_MOUSE:
+                        ShowHideMousePointer(ConsoleOutput, ShowPointer);
+                        ShowPointer = !ShowPointer;
+                        break;
+
+                    case ID_VDM_QUIT:
+                        /* Stop the VDM */
+                        EmulatorTerminate();
+                        break;
+
+                    default:
+                        break;
+                }
+
+                break;
+            }
+
+            default:
+                break;
+        }
+    }
+
+    return 0;
+}
+
 BOOL ConsoleInit(VOID)
 {
     /* Set the handler routine */
     SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
 
+    /* Enable the CTRL_LAST_CLOSE_EVENT */
+    SetLastConsoleEventActive();
+
     /* Get the input handle to the real console, and check for success */
     ConsoleInput = CreateFileW(L"CONIN$",
                                GENERIC_READ | GENERIC_WRITE,
@@ -124,29 +348,29 @@ BOOL ConsoleInit(VOID)
         return FALSE;
     }
 
+    /* Initialize the UI */
+    ConsoleInitUI();
+
     return TRUE;
 }
 
 VOID ConsoleCleanup(VOID)
 {
     SMALL_RECT ConRect;
-    CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo;
 
     /* Restore the old screen buffer */
     SetConsoleActiveScreenBuffer(ConsoleOutput);
 
     /* Restore the original console size */
-    GetConsoleScreenBufferInfo(ConsoleOutput, &ConsoleInfo);
-    ConRect.Left = 0; // OrgConsoleBufferInfo.srWindow.Left;
-    // ConRect.Top  = ConsoleInfo.dwCursorPosition.Y / (OrgConsoleBufferInfo.srWindow.Bottom - OrgConsoleBufferInfo.srWindow.Top + 1);
-    // ConRect.Top *= (OrgConsoleBufferInfo.srWindow.Bottom - OrgConsoleBufferInfo.srWindow.Top + 1);
-    ConRect.Top    = ConsoleInfo.dwCursorPosition.Y;
+    ConRect.Left   = 0;
+    ConRect.Top    = 0;
     ConRect.Right  = ConRect.Left + OrgConsoleBufferInfo.srWindow.Right  - OrgConsoleBufferInfo.srWindow.Left;
     ConRect.Bottom = ConRect.Top  + OrgConsoleBufferInfo.srWindow.Bottom - OrgConsoleBufferInfo.srWindow.Top ;
-    /* See the following trick explanation in vga.c:VgaEnterTextMode() */
+    /*
+     * See the following trick explanation in vga.c:VgaEnterTextMode() .
+     */
     SetConsoleScreenBufferSize(ConsoleOutput, OrgConsoleBufferInfo.dwSize);
     SetConsoleWindowInfo(ConsoleOutput, TRUE, &ConRect);
-    // SetConsoleWindowInfo(ConsoleOutput, TRUE, &OrgConsoleBufferInfo.srWindow);
     SetConsoleScreenBufferSize(ConsoleOutput, OrgConsoleBufferInfo.dwSize);
 
     /* Restore the original cursor shape */
@@ -156,47 +380,102 @@ VOID ConsoleCleanup(VOID)
     SetConsoleMode(ConsoleOutput, OrgConsoleOutputMode);
     SetConsoleMode(ConsoleInput , OrgConsoleInputMode );
 
+    /* Cleanup the UI */
+    ConsoleCleanupUI();
+
     /* Close the console handles */
     if (ConsoleOutput != INVALID_HANDLE_VALUE) CloseHandle(ConsoleOutput);
     if (ConsoleInput  != INVALID_HANDLE_VALUE) CloseHandle(ConsoleInput);
 }
 
+DWORD WINAPI CommandThreadProc(LPVOID Parameter)
+{
+    BOOLEAN First = TRUE;
+    DWORD Result;
+    VDM_COMMAND_INFO CommandInfo;
+    CHAR CmdLine[MAX_PATH];
+    CHAR AppName[MAX_PATH];
+    CHAR PifFile[MAX_PATH];
+    CHAR Desktop[MAX_PATH];
+    CHAR Title[MAX_PATH];
+    CHAR Env[MAX_PATH];
+
+    UNREFERENCED_PARAMETER(Parameter);
+
+    while (AcceptCommands)
+    {
+        /* Clear the structure */
+        ZeroMemory(&CommandInfo, sizeof(CommandInfo));
+
+        /* Initialize the structure members */
+        CommandInfo.VDMState = VDM_FLAG_DOS;
+        CommandInfo.CmdLine = CmdLine;
+        CommandInfo.CmdLen = sizeof(CmdLine);
+        CommandInfo.AppName = AppName;
+        CommandInfo.AppLen = sizeof(AppName);
+        CommandInfo.PifFile = PifFile;
+        CommandInfo.PifLen = sizeof(PifFile);
+        CommandInfo.Desktop = Desktop;
+        CommandInfo.DesktopLen = sizeof(Desktop);
+        CommandInfo.Title = Title;
+        CommandInfo.TitleLen = sizeof(Title);
+        CommandInfo.Env = Env;
+        CommandInfo.EnvLen = sizeof(Env);
+
+        if (First)
+        {
+            CommandInfo.VDMState |= VDM_FLAG_FIRST_TASK;
+            First = FALSE;
+        }
+
+        /* Wait for the next available VDM */
+        if (!GetNextVDMCommand(&CommandInfo)) break;
+
+        /* Start the process from the command line */
+        DPRINT1("Starting '%s'...\n", AppName);
+
+        Result = DosLoadExecutable(DOS_LOAD_AND_EXECUTE, AppName, CmdLine, Env, NULL, NULL);
+        if (Result != ERROR_SUCCESS)
+        {
+            DisplayMessage(L"Could not start '%S'. Error: %u", AppName, Result);
+            break;
+        }
+
+        /* Start simulation */
+        EmulatorSimulate();
+
+        /* Perform another screen refresh */
+        VgaRefreshDisplay();
+    }
+
+    return 0;
+}
+
 INT wmain(INT argc, WCHAR *argv[])
 {
-    INT i;
-    CHAR CommandLine[DOS_CMDLINE_LENGTH];
-    LARGE_INTEGER StartPerfCount;
-    LARGE_INTEGER Frequency, LastTimerTick, LastRtcTick, Counter;
-    LONGLONG TimerTicks;
-    DWORD StartTickCount, CurrentTickCount;
-    DWORD LastClockUpdate;
-    DWORD LastVerticalRefresh;
-#ifdef IPS_DISPLAY
-    DWORD LastCyclePrintout;
-    DWORD Cycles = 0;
-#endif
-    INT KeyboardIntCounter = 0;
+#ifdef STANDALONE
 
-#ifndef TESTING
-    UNREFERENCED_PARAMETER(argc);
-    UNREFERENCED_PARAMETER(argv);
+    DWORD Result;
+    CHAR ApplicationName[MAX_PATH];
+    CHAR CommandLine[DOS_CMDLINE_LENGTH];
 
-    /* The DOS command line must be ASCII */
-    WideCharToMultiByte(CP_ACP, 0, GetCommandLine(), -1, CommandLine, sizeof(CommandLine), NULL, NULL);
-#else
-    if (argc == 2 && argv[1] != NULL)
+    if (argc >= 2)
     {
-        WideCharToMultiByte(CP_ACP, 0, argv[1], -1, CommandLine, sizeof(CommandLine), NULL, NULL);
+        WideCharToMultiByte(CP_ACP, 0, argv[1], -1, ApplicationName, sizeof(ApplicationName), NULL, NULL);
+
+        if (argc >= 3) WideCharToMultiByte(CP_ACP, 0, argv[2], -1, CommandLine, sizeof(CommandLine), NULL, NULL);
+        else strcpy(CommandLine, "");
     }
     else
     {
         wprintf(L"\nReactOS Virtual DOS Machine\n\n"
-                L"Usage: NTVDM <executable>\n");
+                L"Usage: NTVDM <executable> [<parameters>]\n");
         return 0;
     }
+
 #endif
 
-    DPRINT1("\n\n\nNTVDM - Starting '%s'...\n\n\n", CommandLine);
+    DPRINT1("\n\n\nNTVDM - Starting...\n\n\n");
 
     /* Initialize the console */
     if (!ConsoleInit())
@@ -212,15 +491,8 @@ INT wmain(INT argc, WCHAR *argv[])
         goto Cleanup;
     }
 
-    /* Initialize the performance counter (needed for hardware timers) */
-    if (!QueryPerformanceFrequency(&Frequency))
-    {
-        wprintf(L"FATAL: Performance counter not available\n");
-        goto Cleanup;
-    }
-
     /* Initialize the system BIOS */
-    if (!BiosInitialize(ConsoleInput, ConsoleOutput))
+    if (!BiosInitialize(NULL))
     {
         wprintf(L"FATAL: Failed to initialize the VDM BIOS.\n");
         goto Cleanup;
@@ -233,119 +505,57 @@ INT wmain(INT argc, WCHAR *argv[])
         goto Cleanup;
     }
 
-    /* Start the process from the command line */
-    if (!DosCreateProcess(CommandLine, 0))
+#ifndef STANDALONE
+
+    /* Create the GetNextVDMCommand thread */
+    CommandThread = CreateThread(NULL, 0, &CommandThreadProc, NULL, 0, NULL);
+    if (CommandThread == NULL)
     {
-        DisplayMessage(L"Could not start program: %S", CommandLine);
+        wprintf(L"FATAL: Failed to create the command processing thread: %d\n", GetLastError());
         goto Cleanup;
     }
 
-    /* Find the starting performance and tick count */
-    StartTickCount = GetTickCount();
-    QueryPerformanceCounter(&StartPerfCount);
-
-    /* Set the different last counts to the starting count */
-    LastClockUpdate = LastVerticalRefresh =
-#ifdef IPS_DISPLAY
-    LastCyclePrintout =
-#endif
-    StartTickCount;
+    /* Wait for the command thread to exit */
+    WaitForSingleObject(CommandThread, INFINITE);
 
-    /* Set the last timer ticks to the current time */
-    LastTimerTick = LastRtcTick = StartPerfCount;
+    /* Close the thread handle */
+    CloseHandle(CommandThread);
 
-    /* Main loop */
-    while (VdmRunning)
-    {
-        DWORD PitResolution = PitGetResolution();
-        DWORD RtcFrequency = RtcGetTicksPerSecond();
-
-        /* Get the current number of ticks */
-        CurrentTickCount = GetTickCount();
-
-        if ((PitResolution <= 1000) && (RtcFrequency <= 1000))
-        {
-            /* Calculate the approximate performance counter value instead */
-            Counter.QuadPart = StartPerfCount.QuadPart
-                               + (CurrentTickCount - StartTickCount)
-                               * (Frequency.QuadPart / 1000);
-        }
-        else
-        {
-            /* Get the current performance counter value */
-            QueryPerformanceCounter(&Counter);
-        }
-
-        /* Get the number of PIT ticks that have passed */
-        TimerTicks = ((Counter.QuadPart - LastTimerTick.QuadPart)
-                     * PIT_BASE_FREQUENCY) / Frequency.QuadPart;
-
-        /* Update the PIT */
-        if (TimerTicks > 0)
-        {
-            PitClock(TimerTicks);
-            LastTimerTick = Counter;
-        }
-
-        /* Check for RTC update */
-        if ((CurrentTickCount - LastClockUpdate) >= 1000)
-        {
-            RtcTimeUpdate();
-            LastClockUpdate = CurrentTickCount;
-        }
-
-        /* Check for RTC periodic tick */
-        if ((Counter.QuadPart - LastRtcTick.QuadPart)
-            >= (Frequency.QuadPart / (LONGLONG)RtcFrequency))
-        {
-            RtcPeriodicTick();
-            LastRtcTick = Counter;
-        }
-
-        /* Check for vertical retrace */
-        if ((CurrentTickCount - LastVerticalRefresh) >= 15)
-        {
-            VgaRefreshDisplay();
-            LastVerticalRefresh = CurrentTickCount;
-        }
-
-        KeyboardIntCounter++;
-        if (KeyboardIntCounter == KBD_INT_CYCLES)
-        {
-            GenerateKeyboardInterrupts();
-            KeyboardIntCounter = 0;
-        }
-
-        /* Horizontal retrace occurs as fast as possible */
-        VgaHorizontalRetrace();
+#else
 
-        /* Continue CPU emulation */
-        for (i = 0; (i < STEPS_PER_CYCLE) && VdmRunning; i++)
-        {
-            EmulatorStep();
-#ifdef IPS_DISPLAY
-            Cycles++;
-#endif
-        }
+    /* Start the process from the command line */
+    DPRINT1("Starting '%s'...\n", ApplicationName);
 
-#ifdef IPS_DISPLAY
-        if ((CurrentTickCount - LastCyclePrintout) >= 1000)
-        {
-            DPRINT1("NTVDM: %lu Instructions Per Second; TimerTicks = %lu\n", Cycles, TimerTicks);
-            LastCyclePrintout = CurrentTickCount;
-            Cycles = 0;
-        }
-#endif
+    Result = DosLoadExecutable(DOS_LOAD_AND_EXECUTE,
+                               ApplicationName,
+                               CommandLine,
+                               GetEnvironmentStrings(),
+                               NULL,
+                               NULL);
+    if (Result != ERROR_SUCCESS)
+    {
+        DisplayMessage(L"Could not start '%S'. Error: %u", ApplicationName, Result);
+        goto Cleanup;
     }
 
+    /* Start simulation */
+    EmulatorSimulate();
+
     /* Perform another screen refresh */
     VgaRefreshDisplay();
 
+#endif
+
 Cleanup:
     BiosCleanup();
     EmulatorCleanup();
     ConsoleCleanup();
 
+#ifndef STANDALONE
+    ExitVDM(FALSE, 0);
+#endif
+
+    /* Quit the VDM */
     DPRINT1("\n\n\nNTVDM - Exiting...\n\n\n");
 
     return 0;