]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/SetupBrowserDxe/InputHandler.c
Refine the logic about processing options for oneof opcode.
[mirror_edk2.git] / MdeModulePkg / Universal / SetupBrowserDxe / InputHandler.c
index d31a9b9ddc5d79a3c1c0074316cd2a13b48dbfd4..844590770a68d9a867bf4294dc189a48dad2dc67 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 Implementation for handling user input from the User Interfaces.\r
 \r
-Copyright (c) 2004 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2012, 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
@@ -20,7 +20,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
   @param  MenuOption        Pointer to the current input menu.\r
   @param  Prompt            The prompt string shown on popup window.\r
-  @param  StringPtr         Destination for use input string.\r
+  @param  StringPtr         Old user input and destination for use input string.\r
 \r
   @retval EFI_SUCCESS       If string input is read successfully\r
   @retval EFI_DEVICE_ERROR  If operation fails\r
@@ -28,9 +28,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 **/\r
 EFI_STATUS\r
 ReadString (\r
-  IN  UI_MENU_OPTION              *MenuOption,\r
-  IN  CHAR16                      *Prompt,\r
-  OUT CHAR16                      *StringPtr\r
+  IN     UI_MENU_OPTION              *MenuOption,\r
+  IN     CHAR16                      *Prompt,\r
+  IN OUT CHAR16                      *StringPtr\r
   )\r
 {\r
   EFI_STATUS              Status;\r
@@ -42,11 +42,13 @@ ReadString (
   CHAR16                  *TempString;\r
   CHAR16                  *BufferedString;\r
   UINTN                   Index;\r
+  UINTN                   Index2;\r
   UINTN                   Count;\r
   UINTN                   Start;\r
   UINTN                   Top;\r
   UINTN                   DimensionsWidth;\r
   UINTN                   DimensionsHeight;\r
+  UINTN                   CurrentCursor;\r
   BOOLEAN                 CursorVisible;\r
   UINTN                   Minimum;\r
   UINTN                   Maximum;\r
@@ -98,6 +100,40 @@ ReadString (
   CursorVisible = gST->ConOut->Mode->CursorVisible;\r
   gST->ConOut->EnableCursor (gST->ConOut, TRUE);\r
 \r
+  CurrentCursor = GetStringWidth (StringPtr) / 2 - 1;\r
+  if (CurrentCursor != 0) {\r
+    //\r
+    // Show the string which has beed saved before.\r
+    //\r
+    SetUnicodeMem (BufferedString, ScreenSize - 1, L' ');\r
+    PrintStringAt (Start + 1, Top + 3, BufferedString);\r
+\r
+    if ((GetStringWidth (StringPtr) / 2) > (DimensionsWidth - 2)) {\r
+      Index = (GetStringWidth (StringPtr) / 2) - DimensionsWidth + 2;\r
+    } else {\r
+      Index = 0;\r
+    }\r
+\r
+    if (IsPassword) {\r
+      gST->ConOut->SetCursorPosition (gST->ConOut, Start + 1, Top + 3);\r
+    }\r
+\r
+    for (Count = 0; Index + 1 < GetStringWidth (StringPtr) / 2; Index++, Count++) {\r
+      BufferedString[Count] = StringPtr[Index];\r
+\r
+      if (IsPassword) {\r
+        PrintChar (L'*');\r
+      }\r
+    }\r
+\r
+    if (!IsPassword) {\r
+      PrintStringAt (Start + 1, Top + 3, BufferedString);\r
+    }\r
+    \r
+    gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK));\r
+    gST->ConOut->SetCursorPosition (gST->ConOut, Start + GetStringWidth (StringPtr) / 2, Top + 3);\r
+  }\r
+  \r
   do {\r
     Status = WaitForKeyStroke (&Key);\r
     ASSERT_EFI_ERROR (Status);\r
@@ -107,9 +143,15 @@ ReadString (
     case CHAR_NULL:\r
       switch (Key.ScanCode) {\r
       case SCAN_LEFT:\r
+        if (CurrentCursor > 0) {\r
+          CurrentCursor--;\r
+        }\r
         break;\r
 \r
       case SCAN_RIGHT:\r
+        if (CurrentCursor < (GetStringWidth (StringPtr) / 2 - 1)) {\r
+          CurrentCursor++;\r
+        }\r
         break;\r
 \r
       case SCAN_ESC:\r
@@ -152,15 +194,22 @@ ReadString (
       break;\r
 \r
     case CHAR_BACKSPACE:\r
-      if (StringPtr[0] != CHAR_NULL) {\r
-        for (Index = 0; StringPtr[Index] != CHAR_NULL; Index++) {\r
+      if (StringPtr[0] != CHAR_NULL && CurrentCursor != 0) {\r
+        for (Index = 0; Index < CurrentCursor - 1; Index++) {\r
           TempString[Index] = StringPtr[Index];\r
         }\r
+        Count = GetStringWidth (StringPtr) / 2 - 1;\r
+        if (Count >= CurrentCursor) {\r
+          for (Index = CurrentCursor - 1, Index2 = CurrentCursor; Index2 < Count; Index++, Index2++) {\r
+            TempString[Index] = StringPtr[Index2];\r
+          }\r
+          TempString[Index] = CHAR_NULL;\r
+        }\r
         //\r
         // Effectively truncate string by 1 character\r
         //\r
-        TempString[Index - 1] = CHAR_NULL;\r
         StrCpy (StringPtr, TempString);\r
+        CurrentCursor --;\r
       }\r
 \r
     default:\r
@@ -169,12 +218,23 @@ ReadString (
       //\r
       if ((StringPtr[0] == CHAR_NULL) && (Key.UnicodeChar != CHAR_BACKSPACE)) {\r
         StrnCpy (StringPtr, &Key.UnicodeChar, 1);\r
-        StrnCpy (TempString, &Key.UnicodeChar, 1);\r
+        CurrentCursor++;\r
       } else if ((GetStringWidth (StringPtr) < ((Maximum + 1) * sizeof (CHAR16))) && (Key.UnicodeChar != CHAR_BACKSPACE)) {\r
         KeyPad[0] = Key.UnicodeChar;\r
         KeyPad[1] = CHAR_NULL;\r
-        StrCat (StringPtr, KeyPad);\r
-        StrCat (TempString, KeyPad);\r
+        Count = GetStringWidth (StringPtr) / 2 - 1;\r
+        if (CurrentCursor < Count) {\r
+          for (Index = 0; Index < CurrentCursor; Index++) {\r
+            TempString[Index] = StringPtr[Index];\r
+          }\r
+                 TempString[Index] = CHAR_NULL;\r
+          StrCat (TempString, KeyPad);\r
+          StrCat (TempString, StringPtr + CurrentCursor);\r
+          StrCpy (StringPtr, TempString);\r
+        } else {\r
+          StrCat (StringPtr, KeyPad);\r
+        }\r
+        CurrentCursor++;\r
       }\r
 \r
       //\r
@@ -209,11 +269,75 @@ ReadString (
     }\r
 \r
     gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK));\r
-    gST->ConOut->SetCursorPosition (gST->ConOut, Start + GetStringWidth (StringPtr) / 2, Top + 3);\r
+    gST->ConOut->SetCursorPosition (gST->ConOut, Start + CurrentCursor + 1, Top + 3);\r
   } while (TRUE);\r
 \r
 }\r
 \r
+/**\r
+  Adjust the value to the correct one. Rules follow the sample:\r
+  like:  Year change:  2012.02.29 -> 2013.02.29 -> 2013.02.01\r
+         Month change: 2013.03.29 -> 2013.02.29 -> 2013.02.28\r
+\r
+  @param  Question          Pointer to current question.\r
+  @param  Sequence          The sequence of the field in the question.\r
+**/\r
+VOID\r
+AdjustQuestionValue (\r
+  IN  FORM_BROWSER_STATEMENT  *Question,\r
+  IN  UINT8                   Sequence\r
+  )\r
+{\r
+  UINT8     Month;\r
+  UINT16    Year;\r
+  UINT8     Maximum;\r
+  UINT8     Minimum;\r
+\r
+  if (Question->Operand != EFI_IFR_DATE_OP) {\r
+    return;\r
+  }\r
+\r
+  Month   = Question->HiiValue.Value.date.Month;\r
+  Year    = Question->HiiValue.Value.date.Year;\r
+  Minimum = 1;\r
+\r
+  switch (Month) {\r
+  case 2:\r
+    if ((Year % 4) == 0 && ((Year % 100) != 0 || (Year % 400) == 0)) {\r
+      Maximum = 29;\r
+    } else {\r
+      Maximum = 28;\r
+    }\r
+    break;\r
+  case 4:\r
+  case 6:\r
+  case 9:\r
+  case 11:\r
+    Maximum = 30;\r
+    break;\r
+  default:\r
+    Maximum = 31;\r
+    break;\r
+  }\r
+\r
+  //\r
+  // Change the month area.\r
+  //\r
+  if (Sequence == 0) {\r
+    if (Question->HiiValue.Value.date.Day > Maximum) {\r
+      Question->HiiValue.Value.date.Day = Maximum;\r
+    }\r
+  }\r
+  \r
+  //\r
+  // Change the Year area.\r
+  //\r
+  if (Sequence == 2) {\r
+    if (Question->HiiValue.Value.date.Day > Maximum) {\r
+      Question->HiiValue.Value.date.Day = Minimum;\r
+    }\r
+  }\r
+}\r
 \r
 /**\r
   This routine reads a numeric value from the user input.\r
@@ -302,8 +426,8 @@ GetNumericInput (
       switch (QuestionValue->Value.date.Month) {\r
       case 2:\r
         if ((QuestionValue->Value.date.Year % 4) == 0  && \r
-            (QuestionValue->Value.date.Year % 100) != 0 && \r
-            (QuestionValue->Value.date.Year % 400) == 0) {\r
+            ((QuestionValue->Value.date.Year % 100) != 0 || \r
+            (QuestionValue->Value.date.Year % 400) == 0)) {\r
           Maximum = 29;\r
         } else {\r
           Maximum = 28;\r
@@ -602,6 +726,16 @@ TheKey2:
 EnterCarriageReturn:\r
 \r
     case CHAR_CARRIAGE_RETURN:\r
+      //\r
+      // Validate input value with Minimum value.\r
+      //\r
+      if (EditValue < Minimum) {\r
+        UpdateStatusBar (Selection, INPUT_ERROR, Question->QuestionFlags, TRUE);\r
+        break;\r
+      } else {\r
+        UpdateStatusBar (Selection, INPUT_ERROR, Question->QuestionFlags, FALSE);\r
+      }\r
+\r
       //\r
       // Store Edit value back to Question\r
       //\r
@@ -646,6 +780,16 @@ EnterCarriageReturn:
         QuestionValue->Value.u64 = EditValue;\r
       }\r
 \r
+      //\r
+      // Adjust the value to the correct one.\r
+      // Sample like: 2012.02.29 -> 2013.02.29 -> 2013.02.01\r
+      //              2013.03.29 -> 2013.02.29 -> 2013.02.28\r
+      //\r
+      if (Question->Operand == EFI_IFR_DATE_OP && \r
+        (MenuOption->Sequence == 0 || MenuOption->Sequence == 2)) {\r
+        AdjustQuestionValue (Question, (UINT8)MenuOption->Sequence);\r
+      }\r
+\r
       //\r
       // Check to see if the Value is something reasonable against consistency limitations.\r
       // If not, let's kick the error specified.\r
@@ -655,9 +799,9 @@ EnterCarriageReturn:
         //\r
         // Input value is not valid, restore Question Value\r
         //\r
-        GetQuestionValue (FormSet, Form, Question, TRUE);\r
+        GetQuestionValue (FormSet, Form, Question, GetSetValueWithEditBuffer);\r
       } else {\r
-        SetQuestionValue (FormSet, Form, Question, TRUE);\r
+        SetQuestionValue (FormSet, Form, Question, GetSetValueWithEditBuffer);\r
         if (!DateOrTime || (Question->Storage != NULL)) {\r
           //\r
           // NV flag is unnecessary for RTC type of Date/Time\r
@@ -797,6 +941,7 @@ GetSelectionInputPopUp (
   QUESTION_OPTION         *OneOfOption;\r
   QUESTION_OPTION         *CurrentOption;\r
   FORM_BROWSER_STATEMENT  *Question;\r
+  INTN                    Result;\r
 \r
   DimensionsWidth   = gScreenDimensions.RightColumn - gScreenDimensions.LeftColumn;\r
 \r
@@ -842,48 +987,49 @@ GetSelectionInputPopUp (
   }\r
 \r
   //\r
-  // Prepare HiiValue array\r
+  // Move valid Option to list head.\r
   //\r
-  HiiValueArray = AllocateZeroPool (OptionCount * sizeof (EFI_HII_VALUE));\r
-  ASSERT (HiiValueArray != NULL);\r
-  Link = GetFirstNode (&Question->OptionListHead);\r
-  for (Index = 0; Index < OptionCount; Index++) {\r
-    if (OrderedList) {\r
+  PopUpMenuLines = 0;\r
+  if (OrderedList) {\r
+    //\r
+    // Prepare HiiValue array\r
+    //  \r
+    HiiValueArray = AllocateZeroPool (OptionCount * sizeof (EFI_HII_VALUE));\r
+    ASSERT (HiiValueArray != NULL);\r
+    for (Index = 0; Index < OptionCount; Index++) {\r
       HiiValueArray[Index].Type = ValueType;\r
       HiiValueArray[Index].Value.u64 = GetArrayData (ValueArray, ValueType, Index);\r
-    } else {\r
-      OneOfOption = QUESTION_OPTION_FROM_LINK (Link);\r
-      CopyMem (&HiiValueArray[Index], &OneOfOption->Value, sizeof (EFI_HII_VALUE));\r
-      Link = GetNextNode (&Question->OptionListHead, Link);\r
     }\r
-  }\r
 \r
-  //\r
-  // Move Suppressed Option to list tail\r
-  //\r
-  PopUpMenuLines = 0;\r
-  for (Index = 0; Index < OptionCount; Index++) {\r
-    OneOfOption = ValueToOption (Question, &HiiValueArray[OptionCount - Index - 1]);\r
-    if (OneOfOption == NULL) {\r
-      return EFI_NOT_FOUND;\r
-    }\r
+    for (Index = 0; Index < OptionCount; Index++) {\r
+      OneOfOption = ValueToOption (Question, &HiiValueArray[OptionCount - Index - 1]);\r
+      if (OneOfOption == NULL) {\r
+        return EFI_NOT_FOUND;\r
+      }\r
 \r
-    RemoveEntryList (&OneOfOption->Link);\r
+      RemoveEntryList (&OneOfOption->Link);\r
 \r
-    if ((OneOfOption->SuppressExpression != NULL) &&\r
-        (OneOfOption->SuppressExpression->Result.Value.b)) {\r
-      //\r
-      // This option is suppressed, insert to tail\r
       //\r
-      InsertTailList (&Question->OptionListHead, &OneOfOption->Link);\r
-    } else {\r
-      //\r
-      // Insert to head\r
+      // Insert to head.\r
       //\r
       InsertHeadList (&Question->OptionListHead, &OneOfOption->Link);\r
 \r
       PopUpMenuLines++;\r
     }\r
+\r
+    FreePool (HiiValueArray);\r
+  } else {\r
+    Link = GetFirstNode (&Question->OptionListHead);\r
+    for (Index = 0; Index < OptionCount; Index++) {\r
+      OneOfOption = QUESTION_OPTION_FROM_LINK (Link);\r
+      Link = GetNextNode (&Question->OptionListHead, Link);\r
+      if ((OneOfOption->SuppressExpression != NULL) &&\r
+          EvaluateExpressionList(OneOfOption->SuppressExpression, FALSE, NULL, NULL) > ExpressFalse) {\r
+        continue;\r
+      } else {\r
+        PopUpMenuLines++;\r
+      }\r
+    }\r
   }\r
 \r
   //\r
@@ -894,6 +1040,13 @@ GetSelectionInputPopUp (
   Link = GetFirstNode (&Question->OptionListHead);\r
   for (Index = 0; Index < PopUpMenuLines; Index++) {\r
     OneOfOption = QUESTION_OPTION_FROM_LINK (Link);\r
+    Link = GetNextNode (&Question->OptionListHead, Link);\r
+\r
+    if (!OrderedList && (OneOfOption->SuppressExpression != NULL) &&\r
+        EvaluateExpressionList(OneOfOption->SuppressExpression, FALSE, NULL, NULL) > ExpressFalse) {\r
+      Index--;\r
+      continue;\r
+    }\r
 \r
     StringPtr = GetToken (OneOfOption->Text, MenuOption->Handle);\r
     if (StrLen (StringPtr) > PopUpWidth) {\r
@@ -901,14 +1054,12 @@ GetSelectionInputPopUp (
     }\r
     FreePool (StringPtr);\r
 \r
-    if (!OrderedList && CompareHiiValue (&Question->HiiValue, &OneOfOption->Value, NULL) == 0) {\r
+    if (!OrderedList && (CompareHiiValue (&Question->HiiValue, &OneOfOption->Value, &Result, NULL) == EFI_SUCCESS) && (Result == 0)) {\r
       //\r
       // Find current selected Option for OneOf\r
       //\r
       HighlightOptionIndex = Index;\r
     }\r
-\r
-    Link = GetNextNode (&Question->OptionListHead, Link);\r
   }\r
 \r
   //\r
@@ -926,7 +1077,7 @@ GetSelectionInputPopUp (
   Start  = (DimensionsWidth - PopUpWidth - POPUP_FRAME_WIDTH) / 2 + gScreenDimensions.LeftColumn;\r
   End    = Start + PopUpWidth + POPUP_FRAME_WIDTH;\r
   Top    = gScreenDimensions.TopRow + NONE_FRONT_PAGE_HEADER_HEIGHT;\r
-  Bottom = gScreenDimensions.BottomRow - STATUS_BAR_HEIGHT - FOOTER_HEIGHT - 1;\r
+  Bottom = gScreenDimensions.BottomRow - STATUS_BAR_HEIGHT - gFooterHeight - 1;\r
 \r
   MenuLinesInView = Bottom - Top - 1;\r
   if (MenuLinesInView >= PopUpMenuLines) {\r
@@ -977,6 +1128,13 @@ GetSelectionInputPopUp (
     Link = GetFirstNode (&Question->OptionListHead);\r
     for (Index = 0; Index < TopOptionIndex; Index++) {\r
       Link = GetNextNode (&Question->OptionListHead, Link);\r
+\r
+      OneOfOption = QUESTION_OPTION_FROM_LINK (Link);\r
+      if (!OrderedList && (OneOfOption->SuppressExpression != NULL) &&\r
+          EvaluateExpressionList(OneOfOption->SuppressExpression, FALSE, NULL, NULL) > ExpressFalse) {\r
+        Index--;\r
+        continue;\r
+      }\r
     }\r
 \r
     //\r
@@ -987,7 +1145,14 @@ GetSelectionInputPopUp (
       OneOfOption = QUESTION_OPTION_FROM_LINK (Link);\r
       Link = GetNextNode (&Question->OptionListHead, Link);\r
 \r
+      if (!OrderedList && (OneOfOption->SuppressExpression != NULL) &&\r
+          EvaluateExpressionList(OneOfOption->SuppressExpression, FALSE, NULL, NULL) > ExpressFalse) {\r
+        Index--;\r
+        continue;\r
+      }\r
+\r
       StringPtr = GetToken (OneOfOption->Text, MenuOption->Handle);\r
+      ASSERT (StringPtr != NULL);\r
       //\r
       // If the string occupies multiple lines, truncate it to fit in one line,\r
       // and append a "..." for indication.\r
@@ -1164,7 +1329,6 @@ TheKey:
           }\r
         }\r
 \r
-        FreePool (HiiValueArray);\r
         return EFI_DEVICE_ERROR;\r
 \r
       default:\r
@@ -1182,6 +1346,12 @@ TheKey:
         Link = GetFirstNode (&Question->OptionListHead);\r
         while (!IsNull (&Question->OptionListHead, Link)) {\r
           OneOfOption = QUESTION_OPTION_FROM_LINK (Link);\r
+          Link = GetNextNode (&Question->OptionListHead, Link);\r
+\r
+          if ((OneOfOption->SuppressExpression != NULL) &&\r
+              EvaluateExpressionList(OneOfOption->SuppressExpression, FALSE, NULL, NULL) != ExpressFalse) {\r
+            continue;\r
+          }\r
 \r
           SetArrayData (ValueArray, ValueType, Index, OneOfOption->Value.Value.u64);\r
 \r
@@ -1189,8 +1359,6 @@ TheKey:
           if (Index > Question->MaxContainers) {\r
             break;\r
           }\r
-\r
-          Link = GetNextNode (&Question->OptionListHead, Link);\r
         }\r
       } else {\r
         ASSERT (CurrentOption != NULL);\r
@@ -1198,16 +1366,15 @@ TheKey:
       }\r
 \r
       gST->ConOut->SetAttribute (gST->ConOut, SavedAttribute);\r
-      FreePool (HiiValueArray);\r
 \r
       Status = ValidateQuestion (Selection->FormSet, Selection->Form, Question, EFI_HII_EXPRESSION_INCONSISTENT_IF);\r
       if (EFI_ERROR (Status)) {\r
         //\r
         // Input value is not valid, restore Question Value\r
         //\r
-        GetQuestionValue (Selection->FormSet, Selection->Form, Question, TRUE);\r
+        GetQuestionValue (Selection->FormSet, Selection->Form, Question, GetSetValueWithEditBuffer);\r
       } else {\r
-        SetQuestionValue (Selection->FormSet, Selection->Form, Question, TRUE);\r
+        SetQuestionValue (Selection->FormSet, Selection->Form, Question, GetSetValueWithEditBuffer);\r
         UpdateStatusBar (Selection, NV_UPDATE_REQUIRED, Question->QuestionFlags, TRUE);\r
       }\r
 \r
@@ -1235,10 +1402,17 @@ WaitForKeyStroke (
 {\r
   EFI_STATUS  Status;\r
 \r
-  do {\r
-    UiWaitForSingleEvent (gST->ConIn->WaitForKey, 0, 0);\r
+  while (TRUE) {\r
     Status = gST->ConIn->ReadKeyStroke (gST->ConIn, Key);\r
-  } while (EFI_ERROR(Status));\r
+    if (!EFI_ERROR (Status)) {\r
+      break;\r
+    }\r
+\r
+    if (Status != EFI_NOT_READY) {\r
+      continue;\r
+    }\r
 \r
+    UiWaitForSingleEvent (gST->ConIn->WaitForKey, 0, 0);\r
+  }\r
   return Status;\r
 }\r