ShellPkg/[hex]edit: use SimpleTextInEx to read console
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / EditMenuBar.c
CommitLineData
2442e62a 1/** @file\r
2 implements menubar interface functions.\r
3\r
5563281f 4 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved. <BR>\r
2442e62a 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "EditMenuBar.h"\r
16#include "UefiShellDebug1CommandsLib.h"\r
17#include "EditStatusBar.h"\r
18\r
5a2beb74 19EDITOR_MENU_ITEM *MenuItems;\r
20MENU_ITEM_FUNCTION *ControlBasedMenuFunctions;\r
2442e62a 21UINTN NumItems;\r
22\r
23/**\r
24 Cleanup function for a menu bar. frees all allocated memory.\r
25**/\r
26VOID\r
2442e62a 27MenuBarCleanup (\r
28 VOID\r
29 )\r
30{\r
31 SHELL_FREE_NON_NULL(MenuItems);\r
32}\r
33\r
34/**\r
5a2beb74 35 Initialize the menu bar with the specified items.\r
2442e62a 36\r
37 @param[in] Items The items to display and their functions.\r
38\r
39 @retval EFI_SUCCESS The initialization was correct.\r
40 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
41**/\r
42EFI_STATUS\r
2442e62a 43MenuBarInit (\r
44 IN CONST EDITOR_MENU_ITEM *Items\r
45 )\r
46{\r
47 CONST EDITOR_MENU_ITEM *ItemsWalker;\r
48\r
49 for (NumItems = 0, ItemsWalker = Items ; ItemsWalker != NULL && ItemsWalker->Function != NULL ; ItemsWalker++,NumItems++);\r
50 \r
51 MenuItems = AllocateZeroPool((NumItems+1) * sizeof(EDITOR_MENU_ITEM));\r
52 if (MenuItems == NULL) {\r
53 return EFI_OUT_OF_RESOURCES;\r
54 }\r
55 CopyMem(MenuItems, Items, (NumItems+1) * sizeof(EDITOR_MENU_ITEM));\r
56 return EFI_SUCCESS;\r
57}\r
58\r
5a2beb74 59/**\r
60 Initialize the control hot-key with the specified items.\r
61\r
62 @param[in] Items The hot-key functions.\r
63\r
64 @retval EFI_SUCCESS The initialization was correct.\r
65**/\r
66EFI_STATUS\r
5a2beb74 67ControlHotKeyInit (\r
68 IN MENU_ITEM_FUNCTION *Items\r
69 )\r
70{\r
71 ControlBasedMenuFunctions = Items;\r
72 return EFI_SUCCESS; \r
73}\r
2442e62a 74/**\r
75 Refresh function for the menu bar.\r
76\r
77 @param[in] LastRow The last printable row.\r
78 @param[in] LastCol The last printable column.\r
79\r
80 @retval EFI_SUCCESS The refresh was successful.\r
81**/\r
82EFI_STATUS\r
2442e62a 83MenuBarRefresh (\r
84 IN CONST UINTN LastRow,\r
85 IN CONST UINTN LastCol\r
86 )\r
87{\r
88 EDITOR_MENU_ITEM *Item;\r
89 UINTN Col;\r
90 UINTN Row;\r
91 UINTN Width;\r
92 CHAR16 *NameString;\r
93 CHAR16 *FunctionKeyString;\r
94\r
95 //\r
96 // variable initialization\r
97 //\r
98 Col = 1;\r
99 Row = (LastRow - 2);\r
100\r
101 //\r
102 // clear menu bar rows\r
103 //\r
104 EditorClearLine (LastRow - 2, LastCol, LastRow);\r
105 EditorClearLine (LastRow - 1, LastCol, LastRow);\r
106 EditorClearLine (LastRow , LastCol, LastRow);\r
107\r
108\r
109 //\r
110 // print out the menu items\r
111 //\r
112 for (Item = MenuItems; Item != NULL && Item->Function != NULL; Item++) {\r
113\r
114\r
115 NameString = HiiGetString(gShellDebug1HiiHandle, Item->NameToken, NULL);\r
116\r
117\r
118 Width = MAX ((StrLen (NameString) + 6), 20);\r
119 if (((Col + Width) > LastCol)) {\r
120 Row++;\r
121 Col = 1;\r
122 }\r
123\r
124 FunctionKeyString = HiiGetString(gShellDebug1HiiHandle, Item->FunctionKeyToken, NULL);\r
125\r
126 ShellPrintEx ((INT32)(Col) - 1, (INT32)(Row) - 1, L"%E%s%N %H%s%N ", FunctionKeyString, NameString);\r
127\r
128 FreePool (NameString);\r
129 FreePool (FunctionKeyString);\r
130 Col += Width;\r
131 }\r
132\r
133 return EFI_SUCCESS;\r
134}\r
135\r
136/**\r
137 Function to dispatch the correct function based on a function key (F1...)\r
138\r
139 @param[in] Key The pressed key.\r
140\r
141 @retval EFI_NOT_FOUND The key was not a valid function key \r
142 (an error was sent to the status bar).\r
143 @return The return value from the called dispatch function.\r
144**/\r
145EFI_STATUS\r
2442e62a 146MenuBarDispatchFunctionKey (\r
147 IN CONST EFI_INPUT_KEY *Key\r
148 )\r
149{\r
150 UINTN Index;\r
151\r
152 Index = Key->ScanCode - SCAN_F1;\r
153\r
154 //\r
155 // check whether in range\r
156 //\r
157 if (Index > (NumItems - 1)) {\r
158 StatusBarSetStatusString (L"Unknown Command");\r
159 return EFI_SUCCESS;\r
160 }\r
161\r
162 return (MenuItems[Index].Function ());\r
163}\r
164\r
5a2beb74 165/**\r
166 Function to dispatch the correct function based on a control-based key (ctrl+o...)\r
167\r
5563281f 168 @param[in] KeyData The pressed key.\r
5a2beb74 169\r
170 @retval EFI_NOT_FOUND The key was not a valid control-based key \r
171 (an error was sent to the status bar).\r
172 @return EFI_SUCCESS.\r
173**/\r
174EFI_STATUS\r
5a2beb74 175MenuBarDispatchControlHotKey (\r
5563281f 176 IN CONST EFI_KEY_DATA *KeyData\r
5a2beb74 177 )\r
178{\r
5563281f
RN
179 UINT16 ControlIndex;\r
180\r
181 //\r
182 // Set to invalid value first.\r
183 //\r
184 ControlIndex = MAX_UINT16;\r
185\r
186 if ((KeyData->KeyState.KeyShiftState & EFI_SHIFT_STATE_VALID) == 0) {\r
187 //\r
188 // For those console devices that cannot report the CONTROL state,\r
189 // Ctrl+A is translated to 1 (UnicodeChar).\r
190 //\r
191 ControlIndex = KeyData->Key.UnicodeChar;\r
192 } else if (((KeyData->KeyState.KeyShiftState & (EFI_RIGHT_CONTROL_PRESSED | EFI_LEFT_CONTROL_PRESSED)) != 0) &&\r
193 ((KeyData->KeyState.KeyShiftState & ~(EFI_SHIFT_STATE_VALID | EFI_RIGHT_CONTROL_PRESSED | EFI_LEFT_CONTROL_PRESSED)) == 0)) {\r
194 //\r
195 // For those console devices that can report the CONTROL state,\r
196 // make sure only CONTROL is pressed.\r
197 //\r
198 if ((KeyData->Key.UnicodeChar >= L'A') && (KeyData->Key.UnicodeChar <= L'Z')) {\r
199 ControlIndex = KeyData->Key.UnicodeChar - L'A' + 1;\r
200 } else if ((KeyData->Key.UnicodeChar >= L'a') && (KeyData->Key.UnicodeChar <= L'z')) {\r
201 ControlIndex = KeyData->Key.UnicodeChar - L'a' + 1;\r
202 }\r
203 }\r
204 if ((SCAN_CONTROL_Z < ControlIndex)\r
205 ||(NULL == ControlBasedMenuFunctions[ControlIndex]))\r
5a2beb74 206 {\r
207 return EFI_NOT_FOUND;\r
208 }\r
209\r
5563281f 210 ControlBasedMenuFunctions[ControlIndex]();\r
5a2beb74 211 return EFI_SUCCESS;\r
212}\r
213\r
214\r