Fix a couple of problems with FreeLDR portability.
[reactos.git] / reactos / boot / freeldr / freeldr / ui / tui.c
index a125ef7..ff9d73f 100644 (file)
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-       
-#include <freeldr.h>
-#include <ui.h>
-#include "tui.h"
-#include "keycodes.h"
-#include <rtl.h>
-#include <mm.h>
-#include <debug.h>
-#include <inifile.h>
-#include <version.h>
-#include <video.h>
-#include <machine.h>
 
+#include <freeldr.h>
 
 PVOID  TextVideoBuffer = NULL;
 
-BOOL TuiInitialize(VOID)
+/*
+ * printf() - prints formatted text to stdout
+ * originally from GRUB
+ */
+int TuiPrintf(const char *format, ... )
+{
+       va_list ap;
+       va_start(ap,format);
+       char c, *ptr, str[16];
+
+       while ((c = *(format++)))
+       {
+               if (c != '%')
+               {
+                       MachConsPutChar(c);
+               }
+               else
+               {
+                       switch (c = *(format++))
+                       {
+                       case 'd': case 'u': case 'x':
+                if (c == 'x')
+                    _itoa(va_arg(ap, unsigned long), str, 16);
+                else
+                    _itoa(va_arg(ap, unsigned long), str, 10);
+
+                               ptr = str;
+
+                               while (*ptr)
+                               {
+                                       MachConsPutChar(*(ptr++));
+                               }
+                               break;
+
+                       case 'c': MachConsPutChar((va_arg(ap,int))&0xff); break;
+
+                       case 's':
+                               ptr = va_arg(ap,char *);
+
+                               while ((c = *(ptr++)))
+                               {
+                                       MachConsPutChar(c);
+                               }
+                               break;
+                       case '%':
+                               MachConsPutChar(c);
+                               break;
+                       default:
+                               printf("\nprintf() invalid format specifier - %%%c\n", c);
+                               break;
+                       }
+               }
+       }
+
+       va_end(ap);
+
+       return 0;
+}
+
+BOOLEAN TuiInitialize(VOID)
 {
        MachVideoClearScreen(ATTR(COLOR_WHITE, COLOR_BLACK));
        MachVideoHideShowTextCursor(FALSE);
@@ -142,19 +190,18 @@ VOID TuiFillArea(ULONG Left, ULONG Top, ULONG Right, ULONG Bottom, CHAR FillChar
        ULONG           i, j;
 
        // Clip the area to the screen
-       // FIXME: This code seems to have problems... Uncomment and view ;-)
-       /*if ((Left >= UiScreenWidth) || (Top >= UiScreenHeight))
+       if ((Left >= UiScreenWidth) || (Top >= UiScreenHeight))
        {
                return;
        }
-       if ((Left + Right) >= UiScreenWidth)
+       if (Right >= UiScreenWidth)
        {
-               Right = UiScreenWidth - Left;
+               Right = UiScreenWidth - 1;
        }
-       if ((Top + Bottom) >= UiScreenHeight)
+       if (Bottom >= UiScreenHeight)
        {
-               Bottom = UiScreenHeight - Top;
-       }*/
+               Bottom = UiScreenHeight - 1;
+       }
 
        // Loop through each line and fill it in
        for (i=Top; i<=Bottom; i++)
@@ -232,7 +279,7 @@ VOID TuiDrawShadow(ULONG Left, ULONG Top, ULONG Right, ULONG Bottom)
  * DrawBox()
  * This function assumes coordinates are zero-based
  */
-VOID TuiDrawBox(ULONG Left, ULONG Top, ULONG Right, ULONG Bottom, UCHAR VertStyle, UCHAR HorzStyle, BOOL Fill, BOOL Shadow, UCHAR Attr)
+VOID TuiDrawBox(ULONG Left, ULONG Top, ULONG Right, ULONG Bottom, UCHAR VertStyle, UCHAR HorzStyle, BOOLEAN Fill, BOOLEAN Shadow, UCHAR Attr)
 {
        UCHAR   ULCorner, URCorner, LLCorner, LRCorner;
 
@@ -304,7 +351,7 @@ VOID TuiDrawBox(ULONG Left, ULONG Top, ULONG Right, ULONG Bottom, UCHAR VertStyl
  * DrawText()
  * This function assumes coordinates are zero-based
  */
-VOID TuiDrawText(ULONG X, ULONG Y, PCHAR Text, UCHAR Attr)
+VOID TuiDrawText(ULONG X, ULONG Y, PCSTR Text, UCHAR Attr)
 {
        PUCHAR  ScreenMemory = (PUCHAR)TextVideoBuffer;
        ULONG           i, j;
@@ -317,7 +364,7 @@ VOID TuiDrawText(ULONG X, ULONG Y, PCHAR Text, UCHAR Attr)
        }
 }
 
-VOID TuiDrawCenteredText(ULONG Left, ULONG Top, ULONG Right, ULONG Bottom, PCHAR TextString, UCHAR Attr)
+VOID TuiDrawCenteredText(ULONG Left, ULONG Top, ULONG Right, ULONG Bottom, PCSTR TextString, UCHAR Attr)
 {
        ULONG           TextLength;
        ULONG           BoxWidth;
@@ -378,7 +425,7 @@ VOID TuiDrawCenteredText(ULONG Left, ULONG Top, ULONG Right, ULONG Bottom, PCHAR
        }
 }
 
-VOID TuiDrawStatusText(PCHAR StatusText)
+VOID TuiDrawStatusText(PCSTR StatusText)
 {
        ULONG           i;
 
@@ -400,13 +447,22 @@ VOID TuiUpdateDateTime(VOID)
        CHAR    DateString[40];
        CHAR    TimeString[40];
        CHAR    TempString[20];
-       BOOL    PMHour = FALSE;
+       BOOLEAN PMHour = FALSE;
+
+    /* Don't draw the time if this has been disabled */
+    if (!UiDrawTime) return;
 
        MachRTCGetCurrentDateTime(&Year, &Month, &Day, &Hour, &Minute, &Second);
+       if (Year < 1 || 9999 < Year || Month < 1 || 12 < Month || Day < 1 ||
+            31 < Day || 23 < Hour || 59 < Minute || 59 < Second)
+       {
+               /* This happens on QEmu sometimes. We just skip updating */
+               return;
+       }
        // Get the month name
        strcpy(DateString, UiMonthNames[Month - 1]);
        // Get the day
-       itoa(Day, TempString, 10);
+       _itoa(Day, TempString, 10);
        // Get the day postfix
        if (1 == Day || 21 == Day || 31 == Day)
        {
@@ -430,7 +486,7 @@ VOID TuiUpdateDateTime(VOID)
        strcat(DateString, " ");
 
        // Get the year and add it to the date
-       itoa(Year, TempString, 10);
+       _itoa(Year, TempString, 10);
        strcat(DateString, TempString);
 
        // Draw the date
@@ -446,18 +502,18 @@ VOID TuiUpdateDateTime(VOID)
        {
                Hour = 12;
        }
-       itoa(Hour, TempString, 10);
+       _itoa(Hour, TempString, 10);
        strcpy(TimeString, "    ");
        strcat(TimeString, TempString);
        strcat(TimeString, ":");
-       itoa(Minute, TempString, 10);
+       _itoa(Minute, TempString, 10);
        if (Minute < 10)
        {
                strcat(TimeString, "0");
        }
        strcat(TimeString, TempString);
        strcat(TimeString, ":");
-       itoa(Second, TempString, 10);
+       _itoa(Second, TempString, 10);
        if (Second < 10)
        {
                strcat(TimeString, "0");
@@ -498,12 +554,12 @@ VOID TuiRestoreScreen(PUCHAR Buffer)
        }
 }
 
-VOID TuiMessageBox(PCHAR MessageText)
+VOID TuiMessageBox(PCSTR MessageText)
 {
        PVOID   ScreenBuffer;
 
        // Save the screen contents
-       ScreenBuffer = MmAllocateMemory(UiScreenWidth * UiScreenHeight * 2);
+       ScreenBuffer = MmHeapAlloc(UiScreenWidth * UiScreenHeight * 2);
        TuiSaveScreen(ScreenBuffer);
 
        // Display the message box
@@ -511,15 +567,16 @@ VOID TuiMessageBox(PCHAR MessageText)
 
        // Restore the screen contents
        TuiRestoreScreen(ScreenBuffer);
-       MmFreeMemory(ScreenBuffer);
+       MmHeapFree(ScreenBuffer);
 }
 
-VOID TuiMessageBoxCritical(PCHAR MessageText)
+VOID TuiMessageBoxCritical(PCSTR MessageText)
 {
        int             width = 8;
-       int             height = 1;
+       unsigned int    height = 1;
        int             curline = 0;
-       int             i , j, k;
+       int             k;
+       size_t          i , j;
        int             x1, x2, y1, y2;
        char    temp[260];
        char    key;
@@ -634,8 +691,9 @@ VOID TuiDrawProgressBar(ULONG Left, ULONG Top, ULONG Right, ULONG Bottom, ULONG
        // Draw the box
        TuiDrawBox(Left, Top, Right, Bottom, VERT, HORZ, TRUE, TRUE, ATTR(UiMenuFgColor, UiMenuBgColor));
 
-       // Draw the "Loading..." text
-       //TuiDrawText(70/2, Top+1, "Loading...", ATTR(UiTextColor, UiMenuBgColor));
+       //
+       //  Draw the "Loading..." text
+       //
        TuiDrawCenteredText(Left + 2, Top + 2, Right - 2, Top + 2, ProgressText, ATTR(UiTextColor, UiMenuBgColor));
 
        // Draw the percent complete
@@ -644,66 +702,65 @@ VOID TuiDrawProgressBar(ULONG Left, ULONG Top, ULONG Right, ULONG Bottom, ULONG
                TuiDrawText(Left+2+i, Top+2, "\xDB", ATTR(UiTextColor, UiMenuBgColor));
        }
 
-       // Draw the rest
+       // Draw the shadow
        for (; i<ProgressBarWidth; i++)
        {
                TuiDrawText(Left+2+i, Top+2, "\xB2", ATTR(UiTextColor, UiMenuBgColor));
        }
 
        TuiUpdateDateTime();
-
        VideoCopyOffScreenBufferToVRAM();
 }
 
-UCHAR TuiTextToColor(PCHAR ColorText)
+UCHAR TuiTextToColor(PCSTR ColorText)
 {
-       if (stricmp(ColorText, "Black") == 0)
+       if (_stricmp(ColorText, "Black") == 0)
                return COLOR_BLACK;
-       else if (stricmp(ColorText, "Blue") == 0)
+       else if (_stricmp(ColorText, "Blue") == 0)
                return COLOR_BLUE;
-       else if (stricmp(ColorText, "Green") == 0)
+       else if (_stricmp(ColorText, "Green") == 0)
                return COLOR_GREEN;
-       else if (stricmp(ColorText, "Cyan") == 0)
+       else if (_stricmp(ColorText, "Cyan") == 0)
                return COLOR_CYAN;
-       else if (stricmp(ColorText, "Red") == 0)
+       else if (_stricmp(ColorText, "Red") == 0)
                return COLOR_RED;
-       else if (stricmp(ColorText, "Magenta") == 0)
+       else if (_stricmp(ColorText, "Magenta") == 0)
                return COLOR_MAGENTA;
-       else if (stricmp(ColorText, "Brown") == 0)
+       else if (_stricmp(ColorText, "Brown") == 0)
                return COLOR_BROWN;
-       else if (stricmp(ColorText, "Gray") == 0)
+       else if (_stricmp(ColorText, "Gray") == 0)
                return COLOR_GRAY;
-       else if (stricmp(ColorText, "DarkGray") == 0)
+       else if (_stricmp(ColorText, "DarkGray") == 0)
                return COLOR_DARKGRAY;
-       else if (stricmp(ColorText, "LightBlue") == 0)
+       else if (_stricmp(ColorText, "LightBlue") == 0)
                return COLOR_LIGHTBLUE;
-       else if (stricmp(ColorText, "LightGreen") == 0)
+       else if (_stricmp(ColorText, "LightGreen") == 0)
                return COLOR_LIGHTGREEN;
-       else if (stricmp(ColorText, "LightCyan") == 0)
+       else if (_stricmp(ColorText, "LightCyan") == 0)
                return COLOR_LIGHTCYAN;
-       else if (stricmp(ColorText, "LightRed") == 0)
+       else if (_stricmp(ColorText, "LightRed") == 0)
                return COLOR_LIGHTRED;
-       else if (stricmp(ColorText, "LightMagenta") == 0)
+       else if (_stricmp(ColorText, "LightMagenta") == 0)
                return COLOR_LIGHTMAGENTA;
-       else if (stricmp(ColorText, "Yellow") == 0)
+       else if (_stricmp(ColorText, "Yellow") == 0)
                return COLOR_YELLOW;
-       else if (stricmp(ColorText, "White") == 0)
+       else if (_stricmp(ColorText, "White") == 0)
                return COLOR_WHITE;
 
        return COLOR_BLACK;
 }
 
-UCHAR TuiTextToFillStyle(PCHAR FillStyleText)
+UCHAR TuiTextToFillStyle(PCSTR FillStyleText)
 {
-       if (stricmp(FillStyleText, "Light") == 0)
+       if (_stricmp(FillStyleText, "Light") == 0)
        {
                return LIGHT_FILL;
        }
-       else if (stricmp(FillStyleText, "Medium") == 0)
+       else if (_stricmp(FillStyleText, "Medium") == 0)
        {
                return MEDIUM_FILL;
        }
-       else if (stricmp(FillStyleText, "Dark") == 0)
+       else if (_stricmp(FillStyleText, "Dark") == 0)
        {
                return DARK_FILL;
        }
@@ -717,7 +774,7 @@ VOID TuiFadeInBackdrop(VOID)
 
        if (UiUseSpecialEffects && ! MachVideoIsPaletteFixed())
        {
-               TuiFadePalette = (PPALETTE_ENTRY)MmAllocateMemory(sizeof(PALETTE_ENTRY) * 64);
+               TuiFadePalette = (PPALETTE_ENTRY)MmHeapAlloc(sizeof(PALETTE_ENTRY) * 64);
 
                if (TuiFadePalette != NULL)
                {
@@ -732,7 +789,7 @@ VOID TuiFadeInBackdrop(VOID)
        if (UiUseSpecialEffects && ! MachVideoIsPaletteFixed() && TuiFadePalette != NULL)
        {
                VideoFadeIn(TuiFadePalette, 64);
-               MmFreeMemory(TuiFadePalette);
+               MmHeapFree(TuiFadePalette);
        }
 }
 
@@ -742,7 +799,7 @@ VOID TuiFadeOut(VOID)
 
        if (UiUseSpecialEffects && ! MachVideoIsPaletteFixed())
        {
-               TuiFadePalette = (PPALETTE_ENTRY)MmAllocateMemory(sizeof(PALETTE_ENTRY) * 64);
+               TuiFadePalette = (PPALETTE_ENTRY)MmHeapAlloc(sizeof(PALETTE_ENTRY) * 64);
 
                if (TuiFadePalette != NULL)
                {
@@ -760,30 +817,31 @@ VOID TuiFadeOut(VOID)
        if (UiUseSpecialEffects && ! MachVideoIsPaletteFixed() && TuiFadePalette != NULL)
        {
                VideoRestorePaletteState(TuiFadePalette, 64);
-               MmFreeMemory(TuiFadePalette);
+               MmHeapFree(TuiFadePalette);
        }
 
 }
 
-BOOL TuiEditBox(PCHAR MessageText, PCHAR EditTextBuffer, ULONG Length)
+BOOLEAN TuiEditBox(PCSTR MessageText, PCHAR EditTextBuffer, ULONG Length)
 {
        int             width = 8;
-       int             height = 1;
+       unsigned int    height = 1;
        int             curline = 0;
-       int             i , j, k;
+       int             k;
+       size_t          i , j;
        int             x1, x2, y1, y2;
        char    temp[260];
        char    key;
        int             EditBoxLine;
-       int             EditBoxStartX, EditBoxEndX;
+       ULONG           EditBoxStartX, EditBoxEndX;
        int             EditBoxCursorX;
-       int             EditBoxTextCount;
+       unsigned int    EditBoxTextCount;
        int             EditBoxTextDisplayIndex;
-       BOOL    ReturnCode;
+       BOOLEAN ReturnCode;
        PVOID   ScreenBuffer;
 
        // Save the screen contents
-       ScreenBuffer = MmAllocateMemory(UiScreenWidth * UiScreenHeight * 2);
+       ScreenBuffer = MmHeapAlloc(UiScreenWidth * UiScreenHeight * 2);
        TuiSaveScreen(ScreenBuffer);
 
        // Find the height
@@ -877,7 +935,7 @@ BOOL TuiEditBox(PCHAR MessageText, PCHAR EditTextBuffer, ULONG Length)
                                }
                                else
                                {
-                                       beep();
+                                       MachBeep();
                                }
                        }
                        else // Add this key to the buffer
@@ -890,7 +948,7 @@ BOOL TuiEditBox(PCHAR MessageText, PCHAR EditTextBuffer, ULONG Length)
                                }
                                else
                                {
-                                       beep();
+                                       MachBeep();
                                }
                        }
                }
@@ -924,7 +982,32 @@ BOOL TuiEditBox(PCHAR MessageText, PCHAR EditTextBuffer, ULONG Length)
 
        // Restore the screen contents
        TuiRestoreScreen(ScreenBuffer);
-       MmFreeMemory(ScreenBuffer);
+       MmHeapFree(ScreenBuffer);
 
        return ReturnCode;
 }
+
+const UIVTBL TuiVtbl =
+{
+       TuiInitialize,
+       TuiUnInitialize,
+       TuiDrawBackdrop,
+       TuiFillArea,
+       TuiDrawShadow,
+       TuiDrawBox,
+       TuiDrawText,
+       TuiDrawCenteredText,
+       TuiDrawStatusText,
+       TuiUpdateDateTime,
+       TuiMessageBox,
+       TuiMessageBoxCritical,
+       TuiDrawProgressBarCenter,
+       TuiDrawProgressBar,
+       TuiEditBox,
+       TuiTextToColor,
+       TuiTextToFillStyle,
+       TuiFadeInBackdrop,
+       TuiFadeOut,
+       TuiDisplayMenu,
+       TuiDrawMenu,
+};