+/** @file\r
+ Implements inputbar interface functions.\r
+\r
+ Copyright (c) 2005 - 2011, 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
+\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
+\r
+**/\r
+\r
+#include "EditInputBar.h"\r
+#include "UefiShellDebug1CommandsLib.h"\r
+\r
+CHAR16 *mPrompt; // Input bar mPrompt string.\r
+CHAR16 *mReturnString; // The returned string.\r
+UINTN StringSize; // Size of mReturnString space size.\r
+\r
+/**\r
+ Initialize the input bar.\r
+**/\r
+VOID\r
+EFIAPI\r
+InputBarInit (\r
+ VOID\r
+ )\r
+{\r
+ mPrompt = NULL;\r
+ mReturnString = NULL;\r
+ StringSize = 0;\r
+}\r
+\r
+/**\r
+ Cleanup function for input bar.\r
+**/\r
+VOID\r
+EFIAPI\r
+InputBarCleanup (\r
+ VOID\r
+ )\r
+{\r
+ //\r
+ // free input bar's prompt and input string\r
+ //\r
+ SHELL_FREE_NON_NULL (mPrompt);\r
+ SHELL_FREE_NON_NULL (mReturnString);\r
+ mPrompt = NULL;\r
+ mReturnString = NULL;\r
+}\r
+\r
+/**\r
+ Display the prompt.\r
+ Do the requesting of input.\r
+\r
+ @param[in] LastColumn The last printable column.\r
+ @param[in] LastRow The last printable row.\r
+**/\r
+VOID\r
+EFIAPI\r
+InputBarPrintInput (\r
+ IN UINTN LastColumn,\r
+ IN UINTN LastRow\r
+ )\r
+{\r
+ UINTN Limit;\r
+ UINTN Size;\r
+ CHAR16 *Buffer;\r
+ UINTN Index;\r
+ UINTN mPromptLen;\r
+\r
+ mPromptLen = StrLen (mPrompt);\r
+ Limit = LastColumn - mPromptLen - 1;\r
+ Size = StrLen (mReturnString);\r
+\r
+ //\r
+ // check whether the mPrompt length and input length will\r
+ // exceed limit\r
+ //\r
+ if (Size <= Limit) {\r
+ Buffer = mReturnString;\r
+ } else {\r
+ Buffer = mReturnString + Size - Limit;\r
+ }\r
+\r
+ gST->ConOut->EnableCursor (gST->ConOut, FALSE);\r
+\r
+ ShellPrintEx (((INT32)mPromptLen), ((INT32)LastRow) - 4, L"%s", Buffer);\r
+ Size = StrLen (Buffer);\r
+\r
+ //\r
+ // print " " after mPrompt\r
+ //\r
+ for (Index = Size; Index < Limit; Index++) {\r
+ ShellPrintEx ((INT32)(mPromptLen + Size), ((INT32)LastRow) - 4, L" ");\r
+ }\r
+\r
+ gST->ConOut->EnableCursor (gST->ConOut, TRUE);\r
+ gST->ConOut->SetCursorPosition (gST->ConOut, Size + mPromptLen, LastRow - 4);\r
+}\r
+\r
+typedef struct {\r
+ UINT32 Foreground : 4;\r
+ UINT32 Background : 4;\r
+} INPUT_BAR_COLOR_ATTRIBUTES;\r
+\r
+typedef union {\r
+ INPUT_BAR_COLOR_ATTRIBUTES Colors;\r
+ UINTN Data;\r
+} INPUT_BAR_COLOR_UNION;\r
+\r
+\r
+/**\r
+ The refresh function for InputBar, it will wait for user input\r
+\r
+ @param[in] LastRow The last printable row.\r
+ @param[in] LastColumn The last printable column.\r
+\r
+ @retval EFI_SUCCESS The operation was successful.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+InputBarRefresh (\r
+ UINTN LastRow,\r
+ UINTN LastColumn\r
+ )\r
+{\r
+ INPUT_BAR_COLOR_UNION Orig;\r
+ INPUT_BAR_COLOR_UNION New;\r
+ EFI_INPUT_KEY Key;\r
+ UINTN Size;\r
+ EFI_STATUS Status;\r
+ BOOLEAN NoDisplay;\r
+ UINTN Limit;\r
+ UINTN mPromptLen;\r
+ UINTN EventIndex;\r
+ UINTN CursorRow;\r
+ UINTN CursorCol;\r
+\r
+ //\r
+ // variable initialization\r
+ //\r
+ Size = 0;\r
+ Status = EFI_SUCCESS;\r
+\r
+ //\r
+ // back up the old screen attributes\r
+ //\r
+ CursorCol = gST->ConOut->Mode->CursorColumn;\r
+ CursorRow = gST->ConOut->Mode->CursorRow;\r
+ Orig.Data = gST->ConOut->Mode->Attribute;\r
+ New.Colors.Foreground = Orig.Colors.Background;\r
+ New.Colors.Background = Orig.Colors.Foreground;\r
+\r
+ gST->ConOut->SetAttribute (gST->ConOut, New.Data);\r
+\r
+ //\r
+ // clear input bar\r
+ //\r
+ EditorClearLine (LastRow - 3, LastColumn, LastRow);\r
+\r
+ gST->ConOut->SetCursorPosition (gST->ConOut, 0, LastRow - 4);\r
+ ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_EDIT_LIBINPUTBAR_MAININPUTBAR), gShellDebug1HiiHandle, mPrompt);\r
+\r
+ //\r
+ // that's the maximum input length that can be displayed on screen\r
+ //\r
+ mPromptLen = StrLen (mPrompt);\r
+ Limit = LastColumn - mPromptLen;\r
+\r
+ //\r
+ // this is a selection mPrompt, cursor will stay in edit area\r
+ // actually this is for search , search/replace\r
+ //\r
+ if (StrStr (mPrompt, L"Yes/No")) {\r
+ NoDisplay = TRUE;\r
+ gST->ConOut->SetCursorPosition (gST->ConOut, CursorCol, CursorRow);\r
+ gST->ConOut->SetAttribute (gST->ConOut, Orig.Data);\r
+ } else {\r
+ NoDisplay = FALSE;\r
+ }\r
+ //\r
+ // wait for user input\r
+ //\r
+ for (;;) {\r
+ gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
+ Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
+ if (EFI_ERROR (Status)) {\r
+ continue;\r
+ }\r
+ //\r
+ // pressed ESC\r
+ //\r
+ if (Key.ScanCode == SCAN_ESC) {\r
+ Size = 0;\r
+ Status = EFI_NOT_READY;\r
+ break;\r
+ }\r
+ //\r
+ // return pressed\r
+ //\r
+ if (Key.UnicodeChar == CHAR_LINEFEED || Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
+ break;\r
+ } else if (Key.UnicodeChar == CHAR_BACKSPACE) {\r
+ //\r
+ // backspace\r
+ //\r
+ if (Size > 0) {\r
+ Size--;\r
+ mReturnString[Size] = CHAR_NULL;\r
+ if (!NoDisplay) {\r
+\r
+ InputBarPrintInput (LastColumn, LastRow);\r
+\r
+ }\r
+ }\r
+ } else if (Key.UnicodeChar <= 127 && Key.UnicodeChar >= 32) {\r
+ //\r
+ // VALID ASCII char pressed\r
+ //\r
+ mReturnString[Size] = Key.UnicodeChar;\r
+\r
+ //\r
+ // should be less than specified length\r
+ //\r
+ if (Size >= StringSize) {\r
+ continue;\r
+ }\r
+\r
+ Size++;\r
+\r
+ mReturnString[Size] = CHAR_NULL;\r
+\r
+ if (!NoDisplay) {\r
+\r
+ InputBarPrintInput (LastColumn, LastRow);\r
+\r
+ } else {\r
+ //\r
+ // if just choose yes/no\r
+ //\r
+ break;\r
+ }\r
+\r
+ }\r
+ }\r
+\r
+ mReturnString[Size] = CHAR_NULL;\r
+ \r
+\r
+ //\r
+ // restore screen attributes\r
+ //\r
+ gST->ConOut->SetCursorPosition (gST->ConOut, CursorCol, CursorRow);\r
+ gST->ConOut->SetAttribute (gST->ConOut, Orig.Data);\r
+\r
+ return Status;\r
+}\r
+\r
+/**\r
+ SetPrompt and wait for input.\r
+\r
+ @param[in] Str The prompt string.\r
+\r
+ @retval EFI_SUCCESS The operation was successful.\r
+ @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+InputBarSetPrompt (\r
+ IN CONST CHAR16 *Str\r
+ )\r
+{\r
+ //\r
+ // FREE the old mPrompt string\r
+ //\r
+ SHELL_FREE_NON_NULL (mPrompt);\r
+\r
+ mPrompt = CatSPrint (NULL, L"%s ", Str);\r
+ if (mPrompt == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Set the size of the string in characters.\r
+\r
+ @param[in] Size The max number of characters to accept.\r
+\r
+ @retval EFI_SUCCESS The operation was successful.\r
+ @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+InputBarSetStringSize (\r
+ UINTN Size\r
+ )\r
+{\r
+ //\r
+ // free the old ReturnStirng\r
+ //\r
+ SHELL_FREE_NON_NULL (mReturnString);\r
+\r
+ StringSize = Size;\r
+ mReturnString = AllocateZeroPool ((StringSize + 1) * sizeof(mReturnString[0]));\r
+ if (mReturnString == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Function to retrieve the input from the user.\r
+\r
+ @retval NULL No input has been received.\r
+ @return The string that was input.\r
+**/\r
+CONST CHAR16*\r
+EFIAPI\r
+InputBarGetString (\r
+ VOID\r
+ )\r
+{\r
+ return (mReturnString);\r
+}\r