2442e62a |
1 | /** @file\r |
2 | Implements inputbar interface functions.\r |
3 | \r |
4 | Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved. <BR>\r |
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 "EditInputBar.h"\r |
16 | #include "UefiShellDebug1CommandsLib.h"\r |
17 | \r |
18 | CHAR16 *mPrompt; // Input bar mPrompt string.\r |
19 | CHAR16 *mReturnString; // The returned string.\r |
20 | UINTN StringSize; // Size of mReturnString space size.\r |
21 | \r |
22 | /**\r |
23 | Initialize the input bar.\r |
24 | **/\r |
25 | VOID\r |
26 | EFIAPI\r |
27 | InputBarInit (\r |
28 | VOID\r |
29 | )\r |
30 | {\r |
31 | mPrompt = NULL;\r |
32 | mReturnString = NULL;\r |
33 | StringSize = 0;\r |
34 | }\r |
35 | \r |
36 | /**\r |
37 | Cleanup function for input bar.\r |
38 | **/\r |
39 | VOID\r |
40 | EFIAPI\r |
41 | InputBarCleanup (\r |
42 | VOID\r |
43 | )\r |
44 | {\r |
45 | //\r |
46 | // free input bar's prompt and input string\r |
47 | //\r |
48 | SHELL_FREE_NON_NULL (mPrompt);\r |
49 | SHELL_FREE_NON_NULL (mReturnString);\r |
50 | mPrompt = NULL;\r |
51 | mReturnString = NULL;\r |
52 | }\r |
53 | \r |
54 | /**\r |
55 | Display the prompt.\r |
56 | Do the requesting of input.\r |
57 | \r |
58 | @param[in] LastColumn The last printable column.\r |
59 | @param[in] LastRow The last printable row.\r |
60 | **/\r |
61 | VOID\r |
62 | EFIAPI\r |
63 | InputBarPrintInput (\r |
64 | IN UINTN LastColumn,\r |
65 | IN UINTN LastRow\r |
66 | )\r |
67 | {\r |
68 | UINTN Limit;\r |
69 | UINTN Size;\r |
70 | CHAR16 *Buffer;\r |
71 | UINTN Index;\r |
72 | UINTN mPromptLen;\r |
73 | \r |
74 | mPromptLen = StrLen (mPrompt);\r |
75 | Limit = LastColumn - mPromptLen - 1;\r |
76 | Size = StrLen (mReturnString);\r |
77 | \r |
78 | //\r |
79 | // check whether the mPrompt length and input length will\r |
80 | // exceed limit\r |
81 | //\r |
82 | if (Size <= Limit) {\r |
83 | Buffer = mReturnString;\r |
84 | } else {\r |
85 | Buffer = mReturnString + Size - Limit;\r |
86 | }\r |
87 | \r |
88 | gST->ConOut->EnableCursor (gST->ConOut, FALSE);\r |
89 | \r |
5a2beb74 |
90 | ShellPrintEx (((INT32)mPromptLen), ((INT32)LastRow) - 1, L"%s", Buffer);\r |
2442e62a |
91 | Size = StrLen (Buffer);\r |
92 | \r |
93 | //\r |
94 | // print " " after mPrompt\r |
95 | //\r |
96 | for (Index = Size; Index < Limit; Index++) {\r |
5a2beb74 |
97 | ShellPrintEx ((INT32)(mPromptLen + Size), ((INT32)LastRow) - 1, L" ");\r |
2442e62a |
98 | }\r |
99 | \r |
100 | gST->ConOut->EnableCursor (gST->ConOut, TRUE);\r |
5a2beb74 |
101 | gST->ConOut->SetCursorPosition (gST->ConOut, Size + mPromptLen, LastRow - 1);\r |
2442e62a |
102 | }\r |
103 | \r |
104 | typedef struct {\r |
105 | UINT32 Foreground : 4;\r |
106 | UINT32 Background : 4;\r |
107 | } INPUT_BAR_COLOR_ATTRIBUTES;\r |
108 | \r |
109 | typedef union {\r |
110 | INPUT_BAR_COLOR_ATTRIBUTES Colors;\r |
111 | UINTN Data;\r |
112 | } INPUT_BAR_COLOR_UNION;\r |
113 | \r |
114 | \r |
115 | /**\r |
116 | The refresh function for InputBar, it will wait for user input\r |
117 | \r |
118 | @param[in] LastRow The last printable row.\r |
119 | @param[in] LastColumn The last printable column.\r |
120 | \r |
121 | @retval EFI_SUCCESS The operation was successful.\r |
122 | **/\r |
123 | EFI_STATUS\r |
124 | EFIAPI\r |
125 | InputBarRefresh (\r |
126 | UINTN LastRow,\r |
127 | UINTN LastColumn\r |
128 | )\r |
129 | {\r |
130 | INPUT_BAR_COLOR_UNION Orig;\r |
131 | INPUT_BAR_COLOR_UNION New;\r |
132 | EFI_INPUT_KEY Key;\r |
133 | UINTN Size;\r |
134 | EFI_STATUS Status;\r |
135 | BOOLEAN NoDisplay;\r |
2442e62a |
136 | UINTN EventIndex;\r |
137 | UINTN CursorRow;\r |
138 | UINTN CursorCol;\r |
139 | \r |
140 | //\r |
141 | // variable initialization\r |
142 | //\r |
143 | Size = 0;\r |
144 | Status = EFI_SUCCESS;\r |
145 | \r |
146 | //\r |
147 | // back up the old screen attributes\r |
148 | //\r |
149 | CursorCol = gST->ConOut->Mode->CursorColumn;\r |
150 | CursorRow = gST->ConOut->Mode->CursorRow;\r |
151 | Orig.Data = gST->ConOut->Mode->Attribute;\r |
152 | New.Colors.Foreground = Orig.Colors.Background;\r |
153 | New.Colors.Background = Orig.Colors.Foreground;\r |
154 | \r |
155 | gST->ConOut->SetAttribute (gST->ConOut, New.Data);\r |
156 | \r |
157 | //\r |
158 | // clear input bar\r |
159 | //\r |
5a2beb74 |
160 | EditorClearLine (LastRow , LastColumn, LastRow);\r |
2442e62a |
161 | \r |
5a2beb74 |
162 | gST->ConOut->SetCursorPosition (gST->ConOut, 0, LastRow - 1);\r |
2442e62a |
163 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_EDIT_LIBINPUTBAR_MAININPUTBAR), gShellDebug1HiiHandle, mPrompt);\r |
164 | \r |
2442e62a |
165 | //\r |
166 | // this is a selection mPrompt, cursor will stay in edit area\r |
167 | // actually this is for search , search/replace\r |
168 | //\r |
169 | if (StrStr (mPrompt, L"Yes/No")) {\r |
170 | NoDisplay = TRUE;\r |
171 | gST->ConOut->SetCursorPosition (gST->ConOut, CursorCol, CursorRow);\r |
172 | gST->ConOut->SetAttribute (gST->ConOut, Orig.Data);\r |
173 | } else {\r |
174 | NoDisplay = FALSE;\r |
175 | }\r |
176 | //\r |
177 | // wait for user input\r |
178 | //\r |
179 | for (;;) {\r |
180 | gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r |
181 | Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r |
182 | if (EFI_ERROR (Status)) {\r |
183 | continue;\r |
184 | }\r |
185 | //\r |
186 | // pressed ESC\r |
187 | //\r |
188 | if (Key.ScanCode == SCAN_ESC) {\r |
189 | Size = 0;\r |
190 | Status = EFI_NOT_READY;\r |
191 | break;\r |
192 | }\r |
193 | //\r |
194 | // return pressed\r |
195 | //\r |
196 | if (Key.UnicodeChar == CHAR_LINEFEED || Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r |
197 | break;\r |
198 | } else if (Key.UnicodeChar == CHAR_BACKSPACE) {\r |
199 | //\r |
200 | // backspace\r |
201 | //\r |
202 | if (Size > 0) {\r |
203 | Size--;\r |
204 | mReturnString[Size] = CHAR_NULL;\r |
205 | if (!NoDisplay) {\r |
206 | \r |
207 | InputBarPrintInput (LastColumn, LastRow);\r |
208 | \r |
209 | }\r |
210 | }\r |
211 | } else if (Key.UnicodeChar <= 127 && Key.UnicodeChar >= 32) {\r |
212 | //\r |
213 | // VALID ASCII char pressed\r |
214 | //\r |
215 | mReturnString[Size] = Key.UnicodeChar;\r |
216 | \r |
217 | //\r |
218 | // should be less than specified length\r |
219 | //\r |
220 | if (Size >= StringSize) {\r |
221 | continue;\r |
222 | }\r |
223 | \r |
224 | Size++;\r |
225 | \r |
226 | mReturnString[Size] = CHAR_NULL;\r |
227 | \r |
228 | if (!NoDisplay) {\r |
229 | \r |
230 | InputBarPrintInput (LastColumn, LastRow);\r |
231 | \r |
232 | } else {\r |
233 | //\r |
234 | // if just choose yes/no\r |
235 | //\r |
236 | break;\r |
237 | }\r |
238 | \r |
239 | }\r |
240 | }\r |
241 | \r |
242 | mReturnString[Size] = CHAR_NULL;\r |
243 | \r |
244 | \r |
245 | //\r |
246 | // restore screen attributes\r |
247 | //\r |
248 | gST->ConOut->SetCursorPosition (gST->ConOut, CursorCol, CursorRow);\r |
249 | gST->ConOut->SetAttribute (gST->ConOut, Orig.Data);\r |
250 | \r |
251 | return Status;\r |
252 | }\r |
253 | \r |
254 | /**\r |
255 | SetPrompt and wait for input.\r |
256 | \r |
257 | @param[in] Str The prompt string.\r |
258 | \r |
259 | @retval EFI_SUCCESS The operation was successful.\r |
260 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r |
261 | **/\r |
262 | EFI_STATUS\r |
263 | EFIAPI\r |
264 | InputBarSetPrompt (\r |
265 | IN CONST CHAR16 *Str\r |
266 | )\r |
267 | {\r |
268 | //\r |
269 | // FREE the old mPrompt string\r |
270 | //\r |
271 | SHELL_FREE_NON_NULL (mPrompt);\r |
272 | \r |
273 | mPrompt = CatSPrint (NULL, L"%s ", Str);\r |
274 | if (mPrompt == NULL) {\r |
275 | return EFI_OUT_OF_RESOURCES;\r |
276 | }\r |
277 | \r |
278 | return EFI_SUCCESS;\r |
279 | }\r |
280 | \r |
281 | /**\r |
282 | Set the size of the string in characters.\r |
283 | \r |
284 | @param[in] Size The max number of characters to accept.\r |
285 | \r |
286 | @retval EFI_SUCCESS The operation was successful.\r |
287 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r |
288 | **/\r |
289 | EFI_STATUS\r |
290 | EFIAPI\r |
291 | InputBarSetStringSize (\r |
292 | UINTN Size\r |
293 | )\r |
294 | {\r |
295 | //\r |
296 | // free the old ReturnStirng\r |
297 | //\r |
298 | SHELL_FREE_NON_NULL (mReturnString);\r |
299 | \r |
300 | StringSize = Size;\r |
301 | mReturnString = AllocateZeroPool ((StringSize + 1) * sizeof(mReturnString[0]));\r |
302 | if (mReturnString == NULL) {\r |
303 | return EFI_OUT_OF_RESOURCES;\r |
304 | }\r |
305 | \r |
306 | return EFI_SUCCESS;\r |
307 | }\r |
308 | \r |
309 | /**\r |
310 | Function to retrieve the input from the user.\r |
311 | \r |
312 | @retval NULL No input has been received.\r |
313 | @return The string that was input.\r |
314 | **/\r |
315 | CONST CHAR16*\r |
316 | EFIAPI\r |
317 | InputBarGetString (\r |
318 | VOID\r |
319 | )\r |
320 | {\r |
321 | return (mReturnString);\r |
322 | }\r |