]> code.delx.au - refind/blobdiff - refind/screen.c
Added support for "-1" value to "screensaver" token. Modified
[refind] / refind / screen.c
index 9f0d48303444d17e52bc5a77bf094f81ba260dad..1676cb6118eab111e61c6dada73af9057da14a48 100644 (file)
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
+/*
+ * Modifications copyright (c) 2012 Roderick W. Smith
+ *
+ * Modifications distributed under the terms of the GNU General Public
+ * License (GPL) version 3 (GPLv3), a copy of which must be distributed
+ * with this source code or binaries made from it.
+ *
+ */
 
 #include "global.h"
 #include "screen.h"
 #include "config.h"
 #include "libegint.h"
+#include "lib.h"
+#include "menu.h"
 #include "../include/refit_call_wrapper.h"
 
 #include "../include/egemb_refind_banner.h"
@@ -46,7 +56,7 @@
 
 UINTN ConWidth;
 UINTN ConHeight;
-CHAR16 *BlankLine;
+CHAR16 *BlankLine = NULL;
 
 static VOID DrawScreenHeader(IN CHAR16 *Title);
 
@@ -58,6 +68,7 @@ BOOLEAN AllowGraphicsMode;
 
 EG_PIXEL StdBackgroundPixel  = { 0xbf, 0xbf, 0xbf, 0 };
 EG_PIXEL MenuBackgroundPixel = { 0xbf, 0xbf, 0xbf, 0 };
+EG_PIXEL DarkBackgroundPixel = { 0x0, 0x0, 0x0, 0 };
 
 static BOOLEAN GraphicsScreenDirty;
 
@@ -65,14 +76,23 @@ static BOOLEAN GraphicsScreenDirty;
 
 static BOOLEAN haveError = FALSE;
 
+static VOID PrepareBlankLine(VOID) {
+    UINTN i;
+
+    MyFreePool(BlankLine);
+    // make a buffer for a whole text line
+    BlankLine = AllocatePool((ConWidth + 1) * sizeof(CHAR16));
+    for (i = 0; i < ConWidth; i++)
+        BlankLine[i] = ' ';
+    BlankLine[i] = 0;
+}
+
 //
 // Screen initialization and switching
 //
 
 VOID InitScreen(VOID)
 {
-    UINTN i;
-
     // initialize libeg
     egInitScreen();
 
@@ -81,6 +101,7 @@ VOID InitScreen(VOID)
         AllowGraphicsMode = TRUE;
     } else {
         AllowGraphicsMode = FALSE;
+        egSetTextMode(GlobalConfig.RequestedTextMode);
         egSetGraphicsModeEnabled(FALSE);   // just to be sure we are in text mode
     }
     GraphicsScreenDirty = TRUE;
@@ -95,22 +116,47 @@ VOID InitScreen(VOID)
         ConHeight = 25;
     }
 
-    // make a buffer for a whole text line
-    BlankLine = AllocatePool((ConWidth + 1) * sizeof(CHAR16));
-    for (i = 0; i < ConWidth; i++)
-        BlankLine[i] = ' ';
-    BlankLine[i] = 0;
+    PrepareBlankLine();
 
-    // show the banner (even when in graphics mode)
-    DrawScreenHeader(L"Initializing...");
+    // show the banner if in text mode
+    if (GlobalConfig.TextOnly && (GlobalConfig.ScreensaverTime != -1))
+       DrawScreenHeader(L"Initializing...");
 }
 
+// Set the screen resolution and mode (text vs. graphics).
 VOID SetupScreen(VOID)
 {
-    if ((GlobalConfig.RequestedScreenWidth > 0) && (GlobalConfig.RequestedScreenHeight > 0) &&
-        egSetScreenSize(GlobalConfig.RequestedScreenWidth, GlobalConfig.RequestedScreenHeight)) {
-          UGAWidth = GlobalConfig.RequestedScreenWidth;
-          UGAHeight = GlobalConfig.RequestedScreenHeight;
+    UINTN NewWidth, NewHeight;
+
+    // Convert mode number to horizontal & vertical resolution values
+    if ((GlobalConfig.RequestedScreenWidth > 0) && (GlobalConfig.RequestedScreenHeight == 0))
+       egGetResFromMode(&(GlobalConfig.RequestedScreenWidth), &(GlobalConfig.RequestedScreenHeight));
+
+    // Set the believed-to-be current resolution to the LOWER of the current
+    // believed-to-be resolution and the requested resolution. This is done to
+    // enable setting a lower-than-default resolution.
+    if ((GlobalConfig.RequestedScreenWidth > 0) && (GlobalConfig.RequestedScreenHeight > 0)) {
+       UGAWidth = (UGAWidth < GlobalConfig.RequestedScreenWidth) ? UGAWidth : GlobalConfig.RequestedScreenWidth;
+       UGAHeight = (UGAHeight < GlobalConfig.RequestedScreenHeight) ? UGAHeight : GlobalConfig.RequestedScreenHeight;
+    }
+
+    // Set text mode. If this requires increasing the size of the graphics mode, do so.
+    if (egSetTextMode(GlobalConfig.RequestedTextMode)) {
+       egGetScreenSize(&NewWidth, &NewHeight);
+       if ((NewWidth > UGAWidth) || (NewHeight > UGAHeight)) {
+          UGAWidth = NewWidth;
+          UGAHeight = NewHeight;
+       }
+       if ((UGAWidth > GlobalConfig.RequestedScreenWidth) || (UGAHeight > GlobalConfig.RequestedScreenHeight)) {
+           // Requested text mode forces us to use a bigger graphics mode
+           GlobalConfig.RequestedScreenWidth = UGAWidth;
+           GlobalConfig.RequestedScreenHeight = UGAHeight;
+       } // if
+    }
+
+    if (GlobalConfig.RequestedScreenWidth > 0) {
+       egSetScreenSize(&(GlobalConfig.RequestedScreenWidth), &(GlobalConfig.RequestedScreenHeight));
+       egGetScreenSize(&UGAWidth, &UGAHeight);
     } // if user requested a particular screen resolution
 
     if (GlobalConfig.TextOnly) {
@@ -122,9 +168,13 @@ VOID SetupScreen(VOID)
         // clear screen and show banner
         // (now we know we'll stay in graphics mode)
         SwitchToGraphics();
-        BltClearScreen(TRUE);
+        if (GlobalConfig.ScreensaverTime != -1) {
+           BltClearScreen(TRUE);
+        } else { // start with screen blanked
+           GraphicsScreenDirty = TRUE;
+        }
     }
-}
+} // VOID SetupScreen()
 
 VOID SwitchToText(IN BOOLEAN CursorEnabled)
 {
@@ -136,6 +186,7 @@ VOID SwitchToText(IN BOOLEAN CursorEnabled)
         ConWidth = 80;
         ConHeight = 25;
     }
+    PrepareBlankLine();
 }
 
 VOID SwitchToGraphics(VOID)
@@ -162,8 +213,8 @@ VOID BeginTextScreen(IN CHAR16 *Title)
 VOID FinishTextScreen(IN BOOLEAN WaitAlways)
 {
     if (haveError || WaitAlways) {
-        SwitchToText(FALSE);
-        PauseForKey();
+       PauseForKey();
+       SwitchToText(FALSE);
     }
 
     // reset error flag
@@ -172,8 +223,6 @@ VOID FinishTextScreen(IN BOOLEAN WaitAlways)
 
 VOID BeginExternalScreen(IN BOOLEAN UseGraphicsMode, IN CHAR16 *Title)
 {
-    EG_PIXEL DarkBackgroundPixel  = { 0x0, 0x0, 0x0, 0 };
-
     if (!AllowGraphicsMode)
         UseGraphicsMode = FALSE;
 
@@ -181,15 +230,10 @@ VOID BeginExternalScreen(IN BOOLEAN UseGraphicsMode, IN CHAR16 *Title)
         SwitchToGraphics();
         BltClearScreen(FALSE);
     } else {
-       egClearScreen(&DarkBackgroundPixel);
-       DrawScreenHeader(Title);
-    } // if/else
-
-    // show the header
-//    DrawScreenHeader(Title);
-
-    if (!UseGraphicsMode)
+        egClearScreen(&DarkBackgroundPixel);
+        DrawScreenHeader(Title);
         SwitchToText(TRUE);
+    }
 
     // reset error flag
     haveError = FALSE;
@@ -205,6 +249,9 @@ VOID FinishExternalScreen(VOID)
         PauseForKey();
     }
 
+    // Reset the screen resolution, in case external program changed it....
+    SetupScreen();
+
     // reset error flag
     haveError = FALSE;
 }
@@ -224,8 +271,9 @@ static VOID DrawScreenHeader(IN CHAR16 *Title)
     UINTN y;
 
     // clear to black background
+    egClearScreen(&DarkBackgroundPixel); // first clear in graphics mode
     refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
-    refit_call1_wrapper(ST->ConOut->ClearScreen, ST->ConOut);
+    refit_call1_wrapper(ST->ConOut->ClearScreen, ST->ConOut); // then clear in text mode
 
     // paint header background
     refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BANNER);
@@ -247,7 +295,7 @@ static VOID DrawScreenHeader(IN CHAR16 *Title)
 // Keyboard input
 //
 
-static BOOLEAN ReadAllKeyStrokes(VOID)
+BOOLEAN ReadAllKeyStrokes(VOID)
 {
     BOOLEAN       GotKeyStrokes;
     EFI_STATUS    Status;
@@ -350,7 +398,6 @@ BOOLEAN CheckFatalError(IN EFI_STATUS Status, IN CHAR16 *where)
     if (!EFI_ERROR(Status))
         return FALSE;
 
-//    StatusToString(ErrorName, Status);
     gST->ConOut->SetAttribute (gST->ConOut, ATTR_ERROR);
     Print(L"Fatal Error: %r %s\n", Status, where);
     gST->ConOut->SetAttribute (gST->ConOut, ATTR_BASIC);
@@ -363,12 +410,9 @@ BOOLEAN CheckFatalError(IN EFI_STATUS Status, IN CHAR16 *where)
 
 BOOLEAN CheckError(IN EFI_STATUS Status, IN CHAR16 *where)
 {
-//    CHAR16 ErrorName[64];
-
     if (!EFI_ERROR(Status))
         return FALSE;
 
-//    StatusToString(ErrorName, Status);
     gST->ConOut->SetAttribute (gST->ConOut, ATTR_ERROR);
     Print(L"Error: %r %s\n", Status, where);
     gST->ConOut->SetAttribute (gST->ConOut, ATTR_BASIC);
@@ -391,32 +435,56 @@ VOID SwitchToGraphicsAndClear(VOID)
 
 VOID BltClearScreen(IN BOOLEAN ShowBanner)
 {
-    static EG_IMAGE *Banner = NULL;
+    static EG_IMAGE *Banner = NULL, *CroppedBanner;
+    INTN BannerPosX, BannerPosY;
+    EG_PIXEL Black = { 0x0, 0x0, 0x0, 0 };
 
     if (ShowBanner && !(GlobalConfig.HideUIFlags & HIDEUI_FLAG_BANNER)) {
         // load banner on first call
         if (Banner == NULL) {
-            if (GlobalConfig.BannerFileName == NULL)
+            if (GlobalConfig.BannerFileName == NULL) {
                 Banner = egPrepareEmbeddedImage(&egemb_refind_banner, FALSE);
-            else
+            } else {
                 Banner = egLoadImage(SelfDir, GlobalConfig.BannerFileName, FALSE);
+                if (Banner && ((Banner->Width > UGAWidth) || (Banner->Height > UGAHeight))) {
+                   CroppedBanner = egCropImage(Banner, 0, 0, (Banner->Width > UGAWidth) ? UGAWidth : Banner->Width,
+                                               (Banner->Height > UGAHeight) ? UGAHeight : Banner->Height);
+                   MyFreePool(Banner);
+                   Banner = CroppedBanner;
+                } // if image too big
+                if (Banner == NULL) {
+                   Banner = egPrepareEmbeddedImage(&egemb_refind_banner, FALSE);
+                } // if unusable image
+            }
             if (Banner != NULL)
-                MenuBackgroundPixel = Banner->PixelData[0];
+               MenuBackgroundPixel = Banner->PixelData[0];
         }
 
         // clear and draw banner
-        egClearScreen(&MenuBackgroundPixel);
-        if (Banner != NULL)
-            BltImage(Banner, (UGAWidth - Banner->Width) >> 1,
-                     ((UGAHeight - LAYOUT_TOTAL_HEIGHT) >> 1) + LAYOUT_BANNER_HEIGHT - Banner->Height);
+        if (GlobalConfig.ScreensaverTime != -1)
+           egClearScreen(&MenuBackgroundPixel);
+        else
+           egClearScreen(&Black);
+
+        if (Banner != NULL) {
+            BannerPosX = (Banner->Width < UGAWidth) ? ((UGAWidth - Banner->Width) / 2) : 0;
+            BannerPosY = (INTN) (ComputeRow0PosY() / 2) - (INTN) Banner->Height;
+            if (BannerPosY < 0)
+               BannerPosY = 0;
+            GlobalConfig.BannerBottomEdge = BannerPosY + Banner->Height;
+            if (GlobalConfig.ScreensaverTime != -1)
+               BltImage(Banner, (UINTN) BannerPosX, (UINTN) BannerPosY);
+        }
 
     } else {
-        // clear to standard background color
-        egClearScreen(&StdBackgroundPixel);
+        // clear to menu background color
+        egClearScreen(&MenuBackgroundPixel);
     }
 
     GraphicsScreenDirty = FALSE;
-}
+    egFreeImage(GlobalConfig.ScreenBackground);
+    GlobalConfig.ScreenBackground = egCopyScreen();
+} /* VOID BltClearScreen() */
 
 VOID BltImage(IN EG_IMAGE *Image, IN UINTN XPos, IN UINTN YPos)
 {
@@ -498,7 +566,10 @@ VOID BltImageCompositeBadge(IN EG_IMAGE *BaseImage, IN EG_IMAGE *TopImage, IN EG
      }
 
      // blit to screen and clean up
-     egDrawImage(CompImage, XPos, YPos);
+     if (CompImage->HasAlpha)
+        egDrawImageWithTransparency(CompImage, NULL, XPos, YPos, CompImage->Width, CompImage->Height);
+     else
+        egDrawImage(CompImage, XPos, YPos);
      egFreeImage(CompImage);
      GraphicsScreenDirty = TRUE;
 }
@@ -523,16 +594,22 @@ static void cursor_right(UINTN *cursor, UINTN *first, UINTN x_max, UINTN len)
       (*first)++;
 }
 
-BOOLEAN line_edit(CHAR16 *line_in, CHAR16 **line_out, UINTN x_max, UINTN y_pos) {
+BOOLEAN line_edit(CHAR16 *line_in, CHAR16 **line_out, UINTN x_max) {
    CHAR16 *line;
    UINTN size;
    UINTN len;
    UINTN first;
+   UINTN y_pos = 3;
    CHAR16 *print;
    UINTN cursor;
    BOOLEAN exit;
    BOOLEAN enter;
 
+   DrawScreenHeader(L"Line Editor");
+   refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, (ConWidth - 71) / 2, ConHeight - 1);
+   refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut,
+                       L"Use cursor keys to edit, Esc to exit, Enter to boot with edited options");
+
    if (!line_in)
       line_in = L"";
    size = StrLen(line_in) + 1024;