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