]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/UefiLib/Console.c
MdePkg/ProcessorBind: add defines for page allocation granularity
[mirror_edk2.git] / MdePkg / Library / UefiLib / Console.c
index 6157dac450332d93018ed6dc4488a39872554689..ecaf425a0e937e08c8b16a3fbf185d7e654ec299 100644 (file)
@@ -1,11 +1,11 @@
 /** @file\r
   This module provide help function for displaying unicode string.\r
 \r
-  Copyright (c) 2006 - 2008, Intel Corporation<BR>\r
-  All rights reserved. This program and the accompanying materials                          \r
+  Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
+  This program and the accompanying materials                          \r
   are licensed and made available under the terms and conditions of the BSD License         \r
   which accompanies this distribution.  The full text of the license may be found at        \r
-  http://opensource.org/licenses/bsd-license.php                                            \r
+  http://opensource.org/licenses/bsd-license.php.                                            \r
 \r
   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
@@ -22,6 +22,9 @@ typedef struct {
   UINT32  Width;\r
 } UNICODE_WIDTH_ENTRY;\r
 \r
+#define NARROW_CHAR         0xFFF0\r
+#define WIDE_CHAR           0xFFF1\r
+\r
 GLOBAL_REMOVE_IF_UNREFERENCED CONST UNICODE_WIDTH_ENTRY mUnicodeWidthTable[] = {\r
   //\r
   // General script area\r
@@ -248,13 +251,13 @@ GetGlyphWidth (
 /**\r
   Computes the display length of a Null-terminated Unicode String.\r
 \r
-  This function computes and returns the display length of the Null-terminated Unicode\r
-  string specified by String.  If String is NULL then 0 is returned. If any of the widths\r
-  of the Unicode characters in String can not be determined, then 0 is returned. The display\r
-  width of String can be computed by summing the display widths of each Unicode character\r
-  in String.  Unicode characters that are narrow glyphs have a width of 1, and Unicode\r
-  characters that are width glyphs have a width of 2. \r
-  If String is not aligned on a 16-bit boundary, then ASSERT().\r
+  This function computes and returns the display length of the Null-terminated \r
+  Unicode string specified by String.  If String is NULL then 0 is returned. If \r
+  any of the widths of the Unicode characters in String can not be determined, \r
+  then 0 is returned. The display width of String can be computed by summing the \r
+  display widths of each Unicode character in String.  Unicode characters that \r
+  are narrow glyphs have a width of 1, and Unicode characters that are width glyphs \r
+  have a width of 2.  If String is not aligned on a 16-bit boundary, then ASSERT().\r
 \r
   @param  String      A pointer to a Null-terminated Unicode string.\r
 \r
@@ -288,6 +291,89 @@ UnicodeStringDisplayLength (
   return Length;\r
 }\r
 \r
+/**\r
+  Count the storage space of a Unicode string. \r
+\r
+  This function handles the Unicode string with NARROW_CHAR\r
+  and WIDE_CHAR control characters. NARROW_HCAR and WIDE_CHAR\r
+  does not count in the resultant output. If a WIDE_CHAR is\r
+  hit, then 2 Unicode character will consume an output storage\r
+  space with size of CHAR16 till a NARROW_CHAR is hit.\r
+\r
+  @param String          The input string to be counted.\r
+  @param LimitLen        Whether need to limit the string length.\r
+  @param MaxWidth        The max length this function supported.\r
+  @param Offset          The max index of the string can be show out. \r
+\r
+  @return Storage space for the input string.\r
+\r
+**/\r
+UINTN\r
+UefiLibGetStringWidth (\r
+  IN  CHAR16               *String,\r
+  IN  BOOLEAN              LimitLen,\r
+  IN  UINTN                MaxWidth,\r
+  OUT UINTN                *Offset\r
+  )\r
+{\r
+  UINTN Index;\r
+  UINTN Count;\r
+  UINTN IncrementValue;\r
+\r
+  if (String == NULL) {\r
+    return 0;\r
+  }\r
+\r
+  Index           = 0;\r
+  Count           = 0;\r
+  IncrementValue  = 1;\r
+\r
+  do {\r
+    //\r
+    // Advance to the null-terminator or to the first width directive\r
+    //\r
+    for (;(String[Index] != NARROW_CHAR) && (String[Index] != WIDE_CHAR) && (String[Index] != 0); Index++) {\r
+      Count = Count + IncrementValue;\r
+\r
+      if (LimitLen && Count > MaxWidth) {\r
+        break;\r
+      }\r
+    }\r
+\r
+    //\r
+    // We hit the null-terminator, we now have a count\r
+    //\r
+    if (String[Index] == 0) {\r
+      break;\r
+    }\r
+\r
+    if (LimitLen && Count > MaxWidth) {\r
+      *Offset = Index;\r
+      break;\r
+    }\r
+\r
+    //\r
+    // We encountered a narrow directive - strip it from the size calculation since it doesn't get printed\r
+    // and also set the flag that determines what we increment by.(if narrow, increment by 1, if wide increment by 2)\r
+    //\r
+    if (String[Index] == NARROW_CHAR) {\r
+      //\r
+      // Skip to the next character\r
+      //\r
+      Index++;\r
+      IncrementValue = 1;\r
+    } else {\r
+      //\r
+      // Skip to the next character\r
+      //\r
+      Index++;\r
+      IncrementValue = 2;\r
+    }\r
+  } while (String[Index] != 0);\r
+\r
+  return Count * sizeof (CHAR16);\r
+}\r
+\r
 /**\r
   Draws a dialog box to the console output device specified by \r
   ConOut defined in the EFI_SYSTEM_TABLE and waits for a keystroke\r
@@ -314,6 +400,7 @@ CreatePopUp (
   ...\r
   )\r
 {\r
+  EFI_STATUS                       Status;\r
   VA_LIST                          Args;\r
   EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *ConOut;\r
   EFI_SIMPLE_TEXT_OUTPUT_MODE      SavedConsoleMode;\r
@@ -327,6 +414,7 @@ CreatePopUp (
   UINTN                            Length;\r
   CHAR16                           *Line;\r
   UINTN                            EventIndex;\r
+  CHAR16                           *TmpString;\r
 \r
   //\r
   // Determine the length of the longest line in the popup and the the total \r
@@ -336,7 +424,7 @@ CreatePopUp (
   MaxLength = 0;\r
   NumberOfLines = 0;\r
   while ((String = VA_ARG (Args, CHAR16 *)) != NULL) {\r
-    MaxLength = MAX (MaxLength, StrLen (String));\r
+    MaxLength = MAX (MaxLength, UefiLibGetStringWidth (String, FALSE, 0, NULL) / 2);\r
     NumberOfLines++;\r
   }\r
   VA_END (Args);\r
@@ -392,6 +480,7 @@ CreatePopUp (
   // Allocate a buffer for a single line of the popup with borders and a Null-terminator\r
   //\r
   Line = AllocateZeroPool ((MaxLength + 3) * sizeof (CHAR16));\r
+  ASSERT (Line != NULL);\r
 \r
   //\r
   // Draw top of popup box   \r
@@ -408,24 +497,33 @@ CreatePopUp (
   //\r
   VA_START (Args, Key);\r
   while ((String = VA_ARG (Args, CHAR16 *)) != NULL && NumberOfLines > 0) {\r
-    Length = StrLen (String);\r
     SetMem16 (Line, (MaxLength + 2) * 2, L' ');\r
+    Line[0]             = BOXDRAW_VERTICAL;\r
+    Line[MaxLength + 1] = BOXDRAW_VERTICAL;\r
+    Line[MaxLength + 2] = L'\0';\r
+    ConOut->SetCursorPosition (ConOut, Column, Row);\r
+    ConOut->OutputString (ConOut, Line);\r
+    Length = UefiLibGetStringWidth (String, FALSE, 0, NULL) / 2;\r
     if (Length <= MaxLength) {\r
       //\r
       // Length <= MaxLength\r
       //\r
-      CopyMem (Line + 1 + (MaxLength - Length) / 2, String , Length * sizeof (CHAR16));\r
+      ConOut->SetCursorPosition (ConOut, Column + 1 + (MaxLength - Length) / 2, Row++);\r
+      ConOut->OutputString (ConOut, String);\r
     } else {\r
       //\r
       // Length > MaxLength\r
       //\r
-      CopyMem (Line + 1, String + (Length - MaxLength) / 2 , MaxLength * sizeof (CHAR16));\r
+      UefiLibGetStringWidth (String, TRUE, MaxLength, &Length);\r
+      TmpString = AllocateZeroPool ((Length + 1) * sizeof (CHAR16));\r
+      ASSERT (TmpString != NULL);\r
+      StrnCpyS (TmpString, Length + 1, String, Length - 3);\r
+      StrCatS (TmpString, Length + 1, L"...");\r
+\r
+      ConOut->SetCursorPosition (ConOut, Column + 1, Row++);\r
+      ConOut->OutputString (ConOut, TmpString);\r
+      FreePool (TmpString);\r
     }\r
-    Line[0]             = BOXDRAW_VERTICAL;\r
-    Line[MaxLength + 1] = BOXDRAW_VERTICAL;\r
-    Line[MaxLength + 2] = L'\0';\r
-    ConOut->SetCursorPosition (ConOut, Column, Row++);\r
-    ConOut->OutputString (ConOut, Line);\r
     NumberOfLines--;\r
   }\r
   VA_END (Args);\r
@@ -456,7 +554,19 @@ CreatePopUp (
   // Wait for a keystroke\r
   //\r
   if (Key != NULL) {\r
-    gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
-    gST->ConIn->ReadKeyStroke (gST->ConIn, Key);\r
+    while (TRUE) {\r
+      Status = gST->ConIn->ReadKeyStroke (gST->ConIn, Key);\r
+      if (!EFI_ERROR (Status)) {\r
+        break;\r
+      }\r
+\r
+      //\r
+      // If we encounter error, continue to read another key in.\r
+      //\r
+      if (Status != EFI_NOT_READY) {\r
+        continue;\r
+      }\r
+      gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
+    }\r
   }\r
 }\r