]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/MainHexEditor.c
ShellPkg/[hex]edit: use SimpleTextInEx to read console
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / HexEdit / MainHexEditor.c
index 09213d897d0f42931c438027074e71192958420a..065f8e95a706673e0693e6fb0ae67ccf861bf9b9 100644 (file)
@@ -4,7 +4,7 @@
      - Instances of the other objects of the editor\r
      - Main Interfaces\r
   \r
-  Copyright (c) 2005 - 2012, Intel Corporation. All rights reserved. <BR>\r
+  Copyright (c) 2005 - 2018, 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
@@ -56,6 +56,7 @@ HEFI_EDITOR_GLOBAL_EDITOR       HMainEditorConst = {
     0,\r
     0\r
   },\r
+  NULL,\r
   FALSE,\r
   NULL,\r
   0,\r
@@ -105,22 +106,53 @@ HMainCommandDisplayHelp (
   VOID\r
   )\r
 {\r
-  INT32    CurrentLine;\r
-  CHAR16 * InfoString;\r
-  EFI_INPUT_KEY  Key;\r
-\r
-  CurrentLine = 0;\r
+  INT32           CurrentLine;\r
+  CHAR16          *InfoString;\r
+  EFI_KEY_DATA    KeyData;\r
+  EFI_STATUS      Status;\r
+  UINTN           EventIndex;\r
+  \r
+  //\r
   // print helpInfo      \r
+  //\r
   for (CurrentLine = 0; 0 != HexMainMenuHelpInfo[CurrentLine]; CurrentLine++) {\r
     InfoString = HiiGetString(gShellDebug1HiiHandle, HexMainMenuHelpInfo[CurrentLine]\r
 , NULL);\r
     ShellPrintEx (0,CurrentLine+1,L"%E%s%N",InfoString);        \r
   }\r
-  \r
+\r
+  //\r
   // scan for ctrl+w\r
-  do {\r
-    gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
-  } while(SCAN_CONTROL_W != Key.UnicodeChar); \r
+  //\r
+  while (TRUE) {\r
+    Status = gBS->WaitForEvent (1, &HMainEditor.TextInputEx->WaitForKeyEx, &EventIndex);\r
+    if (EFI_ERROR (Status) || (EventIndex != 0)) {\r
+      continue;\r
+    }\r
+    Status = HMainEditor.TextInputEx->ReadKeyStrokeEx (HMainEditor.TextInputEx, &KeyData);\r
+    if (EFI_ERROR (Status)) {\r
+      continue;\r
+    }\r
+\r
+    if ((KeyData.KeyState.KeyShiftState & EFI_SHIFT_STATE_VALID) == 0) {\r
+      //\r
+      // For consoles that don't support shift state reporting,\r
+      // CTRL+W is translated to L'W' - L'A' + 1.\r
+      //\r
+      if (KeyData.Key.UnicodeChar == L'W' - L'A' + 1) {\r
+        break;\r
+      }\r
+    } else if (((KeyData.KeyState.KeyShiftState & (EFI_LEFT_CONTROL_PRESSED | EFI_RIGHT_CONTROL_PRESSED)) != 0) &&\r
+               ((KeyData.KeyState.KeyShiftState & ~(EFI_SHIFT_STATE_VALID | EFI_LEFT_CONTROL_PRESSED | EFI_RIGHT_CONTROL_PRESSED)) == 0)) {\r
+      //\r
+      // For consoles that supports shift state reporting,\r
+      // make sure that only CONTROL shift key is pressed.\r
+      //\r
+      if ((KeyData.Key.UnicodeChar == 'w') || (KeyData.Key.UnicodeChar == 'W')) {\r
+        break;\r
+      }\r
+    }\r
+  }\r
 \r
   // update screen with buffer's info\r
   HBufferImageNeedRefresh = TRUE;\r
@@ -290,7 +322,6 @@ HMainCommandSaveBuffer (
         // the file won't be saved\r
         //\r
         return EFI_SUCCESS;\r
-        break;\r
 \r
       case L'c':\r
       case L'C':\r
@@ -598,7 +629,6 @@ HMainCommandCut (
   )\r
 {\r
   UINTN             Index;\r
-  HEFI_EDITOR_LINE  *Line;\r
   LIST_ENTRY    *Link;\r
   UINT8             *Buffer;\r
   UINTN             Count;\r
@@ -623,8 +653,6 @@ HMainCommandCut (
     Link = Link->ForwardLink;\r
   }\r
 \r
-  Line    = CR (Link, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);\r
-\r
   Count   = HMainEditor.SelectEnd - HMainEditor.SelectStart + 1;\r
   Buffer  = AllocateZeroPool (Count);\r
   if (Buffer == NULL) {\r
@@ -1637,6 +1665,19 @@ HMainEditorInit (
         &(HMainEditor.ScreenSize.Row)\r
         );\r
 \r
+  //\r
+  // Find TextInEx in System Table ConsoleInHandle\r
+  // Per UEFI Spec, TextInEx is required for a console capable platform.\r
+  //\r
+  Status = gBS->HandleProtocol (\r
+              gST->ConsoleInHandle,\r
+              &gEfiSimpleTextInputExProtocolGuid,\r
+              (VOID**)&HMainEditor.TextInputEx\r
+              );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
   //\r
   // Find mouse in System Table ConsoleInHandle\r
   //\r
@@ -1710,7 +1751,7 @@ HMainEditorInit (
     return EFI_LOAD_ERROR;\r
   }\r
 \r
-  InputBarInit ();\r
+  InputBarInit (HMainEditor.TextInputEx);\r
 \r
   Status = HBufferImageInit ();\r
   if (EFI_ERROR (Status)) {\r
@@ -2062,9 +2103,11 @@ HMainEditorKeyInput (
   VOID\r
   )\r
 {\r
-  EFI_INPUT_KEY             Key;\r
+  EFI_KEY_DATA              KeyData;\r
   EFI_STATUS                Status;\r
   EFI_SIMPLE_POINTER_STATE  MouseState;\r
+  UINTN                     EventIndex;\r
+  BOOLEAN                   NoShiftState;\r
   BOOLEAN                   LengthChange;\r
   UINTN                     Size;\r
   UINTN                     OldSize;\r
@@ -2223,84 +2266,94 @@ HMainEditorKeyInput (
       }\r
     }\r
 \r
-    Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
-    if (!EFI_ERROR (Status)) {\r
-      //\r
-      // dispatch to different components' key handling function\r
-      // so not everywhere has to set this variable\r
-      //\r
-      HBufferImageMouseNeedRefresh = TRUE;\r
-\r
-      //\r
-      // clear previous status string\r
-      //\r
-      StatusBarSetRefresh();\r
-      if (EFI_SUCCESS == MenuBarDispatchControlHotKey(&Key)) {\r
-        Status = EFI_SUCCESS;\r
-      } else if (Key.ScanCode == SCAN_NULL) {\r
-        Status = HBufferImageHandleInput (&Key);\r
-      } else if (((Key.ScanCode >= SCAN_UP) && (Key.ScanCode <= SCAN_PAGE_DOWN))) {\r
-        Status = HBufferImageHandleInput (&Key);\r
-      } else if (((Key.ScanCode >= SCAN_F1) && Key.ScanCode <= (SCAN_F12))) {\r
-        Status = MenuBarDispatchFunctionKey (&Key);\r
-      } else {\r
-        StatusBarSetStatusString (L"Unknown Command");\r
-\r
-        HBufferImageMouseNeedRefresh = FALSE;\r
-      }\r
+    Status = gBS->WaitForEvent (1, &HMainEditor.TextInputEx->WaitForKeyEx, &EventIndex);\r
+    if (!EFI_ERROR (Status) && EventIndex == 0) {\r
+      Status = HMainEditor.TextInputEx->ReadKeyStrokeEx (HMainEditor.TextInputEx, &KeyData);\r
+      if (!EFI_ERROR (Status)) {\r
+        //\r
+        // dispatch to different components' key handling function\r
+        // so not everywhere has to set this variable\r
+        //\r
+        HBufferImageMouseNeedRefresh = TRUE;\r
 \r
-      if (Status != EFI_SUCCESS && Status != EFI_OUT_OF_RESOURCES) {\r
         //\r
-        // not already has some error status\r
+        // clear previous status string\r
+        //\r
+        StatusBarSetRefresh();\r
+        //\r
+        // NoShiftState: TRUE when no shift key is pressed.\r
+        //\r
+        NoShiftState = ((KeyData.KeyState.KeyShiftState & EFI_SHIFT_STATE_VALID) == 0) || (KeyData.KeyState.KeyShiftState == EFI_SHIFT_STATE_VALID);\r
         //\r
-        if (StrCmp (L"", StatusBarGetString()) == 0) {\r
-          StatusBarSetStatusString (L"Disk Error. Try Again");\r
+        // dispatch to different components' key handling function\r
+        //\r
+        if (EFI_SUCCESS == MenuBarDispatchControlHotKey(&KeyData)) {\r
+          Status = EFI_SUCCESS;\r
+        } else if (NoShiftState && KeyData.Key.ScanCode == SCAN_NULL) {\r
+          Status = HBufferImageHandleInput (&KeyData.Key);\r
+        } else if (NoShiftState && ((KeyData.Key.ScanCode >= SCAN_UP) && (KeyData.Key.ScanCode <= SCAN_PAGE_DOWN))) {\r
+          Status = HBufferImageHandleInput (&KeyData.Key);\r
+        } else if (NoShiftState && ((KeyData.Key.ScanCode >= SCAN_F1) && KeyData.Key.ScanCode <= SCAN_F12)) {\r
+          Status = MenuBarDispatchFunctionKey (&KeyData.Key);\r
+        } else {\r
+          StatusBarSetStatusString (L"Unknown Command");\r
+\r
+          HBufferImageMouseNeedRefresh = FALSE;\r
+        }\r
+\r
+        if (Status != EFI_SUCCESS && Status != EFI_OUT_OF_RESOURCES) {\r
+          //\r
+          // not already has some error status\r
+          //\r
+          if (StrCmp (L"", StatusBarGetString()) == 0) {\r
+            StatusBarSetStatusString (L"Disk Error. Try Again");\r
+          }\r
         }\r
       }\r
-    }\r
-    //\r
-    // decide if has to set length warning\r
-    //\r
-    if (HBufferImage.BufferType != HBufferImageBackupVar.BufferType) {\r
-      LengthChange = FALSE;\r
-    } else {\r
       //\r
-      // still the old buffer\r
+      // decide if has to set length warning\r
       //\r
-      if (HBufferImage.BufferType != FileTypeFileBuffer) {\r
-        Size = HBufferImageGetTotalSize ();\r
+      if (HBufferImage.BufferType != HBufferImageBackupVar.BufferType) {\r
+        LengthChange = FALSE;\r
+      } else {\r
+        //\r
+        // still the old buffer\r
+        //\r
+        if (HBufferImage.BufferType != FileTypeFileBuffer) {\r
+          Size = HBufferImageGetTotalSize ();\r
 \r
-        switch (HBufferImage.BufferType) {\r
-        case FileTypeDiskBuffer:\r
-          OldSize = HBufferImage.DiskImage->Size * HBufferImage.DiskImage->BlockSize;\r
-          break;\r
+          switch (HBufferImage.BufferType) {\r
+          case FileTypeDiskBuffer:\r
+            OldSize = HBufferImage.DiskImage->Size * HBufferImage.DiskImage->BlockSize;\r
+            break;\r
 \r
-        case FileTypeMemBuffer:\r
-          OldSize = HBufferImage.MemImage->Size;\r
-          break;\r
+          case FileTypeMemBuffer:\r
+            OldSize = HBufferImage.MemImage->Size;\r
+            break;\r
         \r
-        default:\r
-          OldSize = 0;\r
-          break;\r
-        }\r
+          default:\r
+            OldSize = 0;\r
+            break;\r
+          }\r
 \r
-        if (!LengthChange) {\r
-          if (OldSize != Size) {\r
-            StatusBarSetStatusString (L"Disk/Mem Buffer Length should not be changed");\r
+          if (!LengthChange) {\r
+            if (OldSize != Size) {\r
+              StatusBarSetStatusString (L"Disk/Mem Buffer Length should not be changed");\r
+            }\r
           }\r
-        }\r
 \r
-        if (OldSize != Size) {\r
-          LengthChange = TRUE;\r
-        } else {\r
-          LengthChange = FALSE;\r
+          if (OldSize != Size) {\r
+            LengthChange = TRUE;\r
+          } else {\r
+            LengthChange = FALSE;\r
+          }\r
         }\r
       }\r
+      //\r
+      // after handling, refresh editor\r
+      //\r
+      HMainEditorRefresh ();\r
     }\r
-    //\r
-    // after handling, refresh editor\r
-    //\r
-    HMainEditorRefresh ();\r
 \r
   } while (Status != EFI_OUT_OF_RESOURCES && !HEditorExit);\r
 \r
@@ -2311,7 +2364,6 @@ HMainEditorKeyInput (
   Backup function for MainEditor.\r
 **/\r
 VOID\r
-EFIAPI\r
 HMainEditorBackup (\r
   VOID\r
   )\r