]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/MainTextEditor.c
ShellPkg/[hex]edit: Fix mouse freeze issue
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / Edit / MainTextEditor.c
index 6e91719ade39228e5035567942973534e3da27a2..98e1331ac453267334d1766b9c621d43952192b6 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implements editor interface functions.\r
 \r
-  Copyright (c) 2005 - 2016, 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
@@ -40,7 +40,6 @@ extern BOOLEAN                FileBufferMouseNeedRefresh;
 extern EFI_EDITOR_FILE_BUFFER FileBufferBackupVar;\r
 \r
 EFI_EDITOR_GLOBAL_EDITOR      MainEditor;\r
-EFI_EDITOR_GLOBAL_EDITOR      MainEditorBackupVar;\r
 \r
 \r
 /**\r
@@ -1363,7 +1362,9 @@ MainCommandDisplayHelp (
 {\r
   INT32           CurrentLine;\r
   CHAR16          *InfoString;\r
-  EFI_INPUT_KEY   Key;\r
+  EFI_KEY_DATA    KeyData;\r
+  EFI_STATUS      Status;\r
+  UINTN           EventIndex;\r
   \r
   //\r
   // print helpInfo      \r
@@ -1372,14 +1373,41 @@ MainCommandDisplayHelp (
     InfoString = HiiGetString(gShellDebug1HiiHandle, MainMenuHelpInfo[CurrentLine], NULL);\r
     ShellPrintEx (0, CurrentLine+1, L"%E%s%N", InfoString);        \r
   }\r
-  \r
+\r
   //\r
   // scan for ctrl+w\r
   //\r
-  do {\r
-    gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
-  } while(SCAN_CONTROL_W != Key.UnicodeChar); \r
+  while (TRUE) {\r
+    Status = gBS->WaitForEvent (1, &MainEditor.TextInputEx->WaitForKeyEx, &EventIndex);\r
+    if (EFI_ERROR (Status) || (EventIndex != 0)) {\r
+      continue;\r
+    }\r
+    Status = MainEditor.TextInputEx->ReadKeyStrokeEx (MainEditor.TextInputEx, &KeyData);\r
+    if (EFI_ERROR (Status)) {\r
+      continue;\r
+    }\r
 \r
+    if (((KeyData.KeyState.KeyShiftState & EFI_SHIFT_STATE_VALID) == 0) ||\r
+        (KeyData.KeyState.KeyShiftState == EFI_SHIFT_STATE_VALID)) {\r
+      //\r
+      // For consoles that don't support/report shift state,\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_SHIFT_STATE_VALID) != 0) &&\r
+               ((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/reports shift state,\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 file buffer's info\r
   //\r
@@ -1408,6 +1436,7 @@ EFI_EDITOR_GLOBAL_EDITOR      MainEditorConst = {
     0\r
   },\r
   NULL,\r
+  NULL,\r
   FALSE,\r
   NULL\r
 };\r
@@ -1419,7 +1448,6 @@ EFI_EDITOR_GLOBAL_EDITOR      MainEditorConst = {
   @retval EFI_LOAD_ERROR          A load error occured.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 MainEditorInit (\r
   VOID\r
   )\r
@@ -1454,11 +1482,24 @@ MainEditorInit (
         &(MainEditor.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**)&MainEditor.TextInputEx\r
+              );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
   //\r
   // Find mouse in System Table ConsoleInHandle\r
   //\r
   Status = gBS->HandleProtocol (\r
-                gST->ConIn,\r
+                gST->ConsoleInHandle,\r
                 &gEfiSimplePointerProtocolGuid,\r
                 (VOID**)&MainEditor.MouseInterface\r
                 );\r
@@ -1523,7 +1564,7 @@ MainEditorInit (
     return EFI_LOAD_ERROR;\r
   }\r
 \r
-  InputBarInit ();\r
+  InputBarInit (MainEditor.TextInputEx);\r
 \r
   Status = FileBufferInit ();\r
   if (EFI_ERROR (Status)) {\r
@@ -1553,7 +1594,6 @@ MainEditorInit (
   @retval EFI_LOAD_ERROR          A load error occured.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 MainEditorCleanup (\r
   VOID\r
   )\r
@@ -1597,7 +1637,6 @@ MainEditorCleanup (
   Refresh the main editor component.\r
 **/\r
 VOID\r
-EFIAPI\r
 MainEditorRefresh (\r
   VOID\r
   )\r
@@ -1650,7 +1689,6 @@ MainEditorRefresh (
   @return The X location of the mouse.\r
 **/\r
 INT32\r
-EFIAPI\r
 GetTextX (\r
   IN INT32 GuidX\r
   )\r
@@ -1672,7 +1710,6 @@ GetTextX (
   @return The Y location of the mouse.\r
 **/\r
 INT32\r
-EFIAPI\r
 GetTextY (\r
   IN INT32 GuidY\r
   )\r
@@ -1690,17 +1727,14 @@ GetTextY (
 /**\r
   Support mouse movement.  Move the cursor.\r
 \r
-  @param[in]   MouseState             The current mouse state.\r
-  @param[out]  BeforeLeftButtonDown   TRUE if the left button down. Helps with selections.\r
+  @param[in] MouseState     The current mouse state.\r
 \r
   @retval EFI_SUCCESS       The operation was successful.\r
   @retval EFI_NOT_FOUND     There was no mouse support found.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 MainEditorHandleMouseInput (\r
-  IN  EFI_SIMPLE_POINTER_STATE        MouseState,\r
-  OUT BOOLEAN                         *BeforeLeftButtonDown\r
+  IN EFI_SIMPLE_POINTER_STATE       MouseState\r
   )\r
 {\r
 \r
@@ -1708,8 +1742,10 @@ MainEditorHandleMouseInput (
   INT32           TextY;\r
   UINTN           FRow;\r
   UINTN           FCol;\r
-  LIST_ENTRY      *Link;\r
+\r
+  LIST_ENTRY  *Link;\r
   EFI_EDITOR_LINE *Line;\r
+\r
   UINTN           Index;\r
   BOOLEAN         Action;\r
 \r
@@ -1762,27 +1798,10 @@ MainEditorHandleMouseInput (
     Line = CR (Link, EFI_EDITOR_LINE, Link, LINE_LIST_SIGNATURE);\r
 \r
     //\r
-    // dragging\r
     // beyond the line's column length\r
     //\r
-    if (FCol > Line->Size ) {\r
-      if (*BeforeLeftButtonDown) {\r
-\r
-        if (Line->Size == 0) {\r
-          if (FRow > 1) {\r
-            FRow--;\r
-            FCol = SHELL_EDIT_MAX_LINE_SIZE;\r
-          } else {\r
-            FRow  = 1;\r
-            FCol  = 1;\r
-          }\r
-\r
-        } else {\r
-          FCol = Line->Size ;\r
-        }\r
-      } else {\r
-        FCol      = Line->Size + 1;\r
-      }\r
+    if (FCol > Line->Size + 1) {\r
+      FCol = Line->Size + 1;\r
     }\r
 \r
     FileBufferMovePosition (FRow, FCol);\r
@@ -1791,24 +1810,8 @@ MainEditorHandleMouseInput (
 \r
     MainEditor.FileBuffer->MousePosition.Column = MainEditor.FileBuffer->DisplayPosition.Column;\r
 \r
-    *BeforeLeftButtonDown                       = TRUE;\r
-\r
     Action = TRUE;\r
-  } else {\r
-    //\r
-    // else of if LButton\r
-    //\r
-    // release LButton\r
-    //\r
-    if (*BeforeLeftButtonDown) {\r
-      Action = TRUE;\r
-    }\r
-    //\r
-    // mouse up\r
-    //\r
-    *BeforeLeftButtonDown = FALSE;\r
   }\r
-\r
   //\r
   // mouse has action\r
   //\r
@@ -1830,31 +1833,14 @@ MainEditorHandleMouseInput (
   @retval EFI_OUT_OF_RESOURCES    A memory allocation failed.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 MainEditorKeyInput (\r
   VOID\r
   )\r
 {\r
-  EFI_INPUT_KEY             Key;\r
+  EFI_KEY_DATA              KeyData;\r
   EFI_STATUS                Status;\r
   EFI_SIMPLE_POINTER_STATE  MouseState;\r
-  BOOLEAN                   BeforeMouseIsDown;\r
-  BOOLEAN                   MouseIsDown;\r
-  BOOLEAN                   FirstDown;\r
-  BOOLEAN                   MouseDrag;\r
-  UINTN                     FRow;\r
-  UINTN                     FCol;\r
-  UINTN                     SelectStartBackup;\r
-  UINTN                     SelectEndBackup;\r
-\r
-  //\r
-  // variable initialization\r
-  //\r
-  FRow          = 0;\r
-  FCol          = 0;\r
-  MouseIsDown   = FALSE;\r
-  FirstDown     = FALSE;\r
-  MouseDrag     = FALSE;\r
+  BOOLEAN                   NoShiftState;\r
 \r
   do {\r
 \r
@@ -1877,105 +1863,10 @@ MainEditorKeyInput (
                                             &MouseState\r
                                             );\r
       if (!EFI_ERROR (Status)) {\r
-        BeforeMouseIsDown = MouseIsDown;\r
 \r
-        Status            = MainEditorHandleMouseInput (MouseState, &MouseIsDown);\r
+        Status = MainEditorHandleMouseInput (MouseState);\r
 \r
         if (!EFI_ERROR (Status)) {\r
-          if (!BeforeMouseIsDown) {\r
-            //\r
-            // mouse down\r
-            //\r
-            if (MouseIsDown) {\r
-              FRow              = FileBuffer.FilePosition.Row;\r
-              FCol              = FileBuffer.FilePosition.Column;\r
-              SelectStartBackup = MainEditor.SelectStart;\r
-              SelectEndBackup   = MainEditor.SelectEnd;\r
-\r
-              FirstDown         = TRUE;\r
-            }\r
-          } else {\r
-\r
-            SelectStartBackup = MainEditor.SelectStart;\r
-            SelectEndBackup   = MainEditor.SelectEnd;\r
-\r
-            //\r
-            // begin to drag\r
-            //\r
-            if (MouseIsDown) {\r
-              if (FirstDown) {\r
-                if (MouseState.RelativeMovementX || MouseState.RelativeMovementY) {\r
-                  MainEditor.SelectStart = 0;\r
-                  MainEditor.SelectEnd   = 0;\r
-                  MainEditor.SelectStart = (FRow - 1) * SHELL_EDIT_MAX_LINE_SIZE + FCol;\r
-\r
-                  MouseDrag               = TRUE;\r
-                  FirstDown               = FALSE;\r
-                }\r
-              } else {\r
-                if ((\r
-                      (FileBuffer.FilePosition.Row - 1) *\r
-                      SHELL_EDIT_MAX_LINE_SIZE +\r
-                      FileBuffer.FilePosition.Column\r
-                    ) >= MainEditor.SelectStart\r
-                   ) {\r
-                  MainEditor.SelectEnd = (FileBuffer.FilePosition.Row - 1) *\r
-                                          SHELL_EDIT_MAX_LINE_SIZE +\r
-                                          FileBuffer.FilePosition.Column;\r
-                } else {\r
-                  MainEditor.SelectEnd = 0;\r
-                }\r
-              }\r
-              //\r
-              // end of if RelativeX/Y\r
-              //\r
-            } else {\r
-              //\r
-              // mouse is up\r
-              //\r
-              if (MouseDrag) {\r
-                if (FileBufferGetTotalSize () == 0) {\r
-                  MainEditor.SelectStart = 0;\r
-                  MainEditor.SelectEnd   = 0;\r
-                  FirstDown               = FALSE;\r
-                  MouseDrag               = FALSE;\r
-                }\r
-\r
-                if ((\r
-                      (FileBuffer.FilePosition.Row - 1) *\r
-                      SHELL_EDIT_MAX_LINE_SIZE +\r
-                      FileBuffer.FilePosition.Column\r
-                    ) >= MainEditor.SelectStart\r
-                   ) {\r
-                  MainEditor.SelectEnd = (FileBuffer.FilePosition.Row - 1) *\r
-                                          SHELL_EDIT_MAX_LINE_SIZE +\r
-                                          FileBuffer.FilePosition.Column;\r
-                } else {\r
-                  MainEditor.SelectEnd = 0;\r
-                }\r
-\r
-                if (MainEditor.SelectEnd == 0) {\r
-                  MainEditor.SelectStart = 0;\r
-                }\r
-              }\r
-\r
-              FirstDown = FALSE;\r
-              MouseDrag = FALSE;\r
-            }\r
-\r
-            if (SelectStartBackup != MainEditor.SelectStart || SelectEndBackup != MainEditor.SelectEnd) {\r
-              if ((SelectStartBackup - 1) / SHELL_EDIT_MAX_LINE_SIZE != (MainEditor.SelectStart - 1) / SHELL_EDIT_MAX_LINE_SIZE) {\r
-                FileBufferNeedRefresh = TRUE;\r
-              } else {\r
-                if ((SelectEndBackup - 1) / SHELL_EDIT_MAX_LINE_SIZE != (MainEditor.SelectEnd - 1) / SHELL_EDIT_MAX_LINE_SIZE) {\r
-                  FileBufferNeedRefresh = TRUE;\r
-                } else {\r
-                  FileBufferOnlyLineNeedRefresh = TRUE;\r
-                }\r
-              }\r
-            }\r
-          }\r
-\r
           EditorMouseAction           = TRUE;\r
           FileBufferMouseNeedRefresh  = TRUE;\r
         } else if (Status == EFI_LOAD_ERROR) {\r
@@ -1984,41 +1875,50 @@ MainEditorKeyInput (
       }\r
     }\r
 \r
-    Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
+    //\r
+    // CheckEvent() returns Success when non-partial key is pressed.\r
+    //\r
+    Status = gBS->CheckEvent (MainEditor.TextInputEx->WaitForKeyEx);\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
-      FileBufferMouseNeedRefresh = TRUE;\r
-      //\r
-      // clear previous status string\r
-      //\r
-      StatusBarSetRefresh();\r
-\r
-      //\r
-      // dispatch to different components' key handling function\r
-      //\r
-      if (EFI_NOT_FOUND != MenuBarDispatchControlHotKey(&Key)) {\r
-        Status = EFI_SUCCESS;\r
-      } else if ((Key.ScanCode == SCAN_NULL) || ((Key.ScanCode >= SCAN_UP) && (Key.ScanCode <= SCAN_PAGE_DOWN))) {\r
-        Status = FileBufferHandleInput (&Key);\r
-      } else if ((Key.ScanCode >= SCAN_F1) && (Key.ScanCode <= SCAN_F12)) {\r
-        Status = MenuBarDispatchFunctionKey (&Key);\r
-      } else {\r
-        StatusBarSetStatusString (L"Unknown Command");\r
-        FileBufferMouseNeedRefresh = FALSE;  \r
-      }\r
-      \r
-      if (Status != EFI_SUCCESS && Status != EFI_OUT_OF_RESOURCES) {\r
+      Status = MainEditor.TextInputEx->ReadKeyStrokeEx (MainEditor.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
+        FileBufferMouseNeedRefresh = TRUE;\r
+        //\r
+        // clear previous status string\r
+        //\r
+        StatusBarSetRefresh();\r
         //\r
-        // not already has some error status\r
+        // NoShiftState: TRUE when no shift key is pressed.\r
         //\r
-        if (StatusBarGetString() != NULL && StrCmp (L"", StatusBarGetString()) == 0) {\r
-          StatusBarSetStatusString (L"Disk Error. Try Again");\r
+        NoShiftState = ((KeyData.KeyState.KeyShiftState & EFI_SHIFT_STATE_VALID) == 0) || (KeyData.KeyState.KeyShiftState == EFI_SHIFT_STATE_VALID);\r
+        //\r
+        // dispatch to different components' key handling function\r
+        //\r
+        if (EFI_NOT_FOUND != MenuBarDispatchControlHotKey(&KeyData)) {\r
+          Status = EFI_SUCCESS;\r
+        } else if (NoShiftState && ((KeyData.Key.ScanCode == SCAN_NULL) || ((KeyData.Key.ScanCode >= SCAN_UP) && (KeyData.Key.ScanCode <= SCAN_PAGE_DOWN)))) {\r
+          Status = FileBufferHandleInput (&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
+          FileBufferMouseNeedRefresh = FALSE;  \r
+        }\r
+        \r
+        if (Status != EFI_SUCCESS && Status != EFI_OUT_OF_RESOURCES) {\r
+          //\r
+          // not already has some error status\r
+          //\r
+          if (StatusBarGetString() != NULL && StrCmp (L"", StatusBarGetString()) == 0) {\r
+            StatusBarSetStatusString (L"Disk Error. Try Again");\r
+          }\r
         }\r
-      }\r
 \r
+      }\r
     }\r
     //\r
     // after handling, refresh editor\r
@@ -2039,7 +1939,6 @@ MainEditorKeyInput (
   @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 MainEditorSetCutLine (\r
   EFI_EDITOR_LINE *Line\r
   )\r
@@ -2071,16 +1970,11 @@ MainEditorSetCutLine (
   @retval EFI_SUCCESS The operation was successful.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 MainEditorBackup (\r
   VOID\r
   )\r
 {\r
-  MainEditorBackupVar.SelectStart  = MainEditor.SelectStart;\r
-  MainEditorBackupVar.SelectEnd    = MainEditor.SelectEnd;\r
   FileBufferBackup ();\r
   \r
   return EFI_SUCCESS;\r
 }\r
-\r
-\r