]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/SetupBrowserDxe/InputHandler.c
Coding style checked
[mirror_edk2.git] / MdeModulePkg / Universal / SetupBrowserDxe / InputHandler.c
1 /** @file
2 Implementation for handling user input from the User Interfaces.
3
4 Copyright (c) 2004 - 2007, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Ui.h"
16 #include "Setup.h"
17
18
19 /**
20 Get string or password input from user.
21
22 @param MenuOption Pointer to the current input menu.
23 @param Prompt The prompt string shown on popup window.
24 @param StringPtr Destination for use input string.
25
26 @retval EFI_SUCCESS If string input is read successfully
27 @retval EFI_DEVICE_ERROR If operation fails
28
29 **/
30 EFI_STATUS
31 ReadString (
32 IN UI_MENU_OPTION *MenuOption,
33 IN CHAR16 *Prompt,
34 OUT CHAR16 *StringPtr
35 )
36 {
37 EFI_STATUS Status;
38 EFI_INPUT_KEY Key;
39 CHAR16 NullCharacter;
40 UINTN ScreenSize;
41 CHAR16 Space[2];
42 CHAR16 KeyPad[2];
43 CHAR16 *TempString;
44 CHAR16 *BufferedString;
45 UINTN Index;
46 UINTN Count;
47 UINTN Start;
48 UINTN Top;
49 UINTN DimensionsWidth;
50 UINTN DimensionsHeight;
51 BOOLEAN CursorVisible;
52 UINTN Minimum;
53 UINTN Maximum;
54 FORM_BROWSER_STATEMENT *Question;
55 BOOLEAN IsPassword;
56
57 DimensionsWidth = gScreenDimensions.RightColumn - gScreenDimensions.LeftColumn;
58 DimensionsHeight = gScreenDimensions.BottomRow - gScreenDimensions.TopRow;
59
60 NullCharacter = CHAR_NULL;
61 ScreenSize = GetStringWidth (Prompt) / sizeof (CHAR16);
62 Space[0] = L' ';
63 Space[1] = CHAR_NULL;
64
65 Question = MenuOption->ThisTag;
66 Minimum = (UINTN) Question->Minimum;
67 Maximum = (UINTN) Question->Maximum;
68
69 if (Question->Operand == EFI_IFR_PASSWORD_OP) {
70 IsPassword = TRUE;
71 } else {
72 IsPassword = FALSE;
73 }
74
75 TempString = AllocateZeroPool ((Maximum + 1)* sizeof (CHAR16));
76 ASSERT (TempString);
77
78 if (ScreenSize < (Maximum + 1)) {
79 ScreenSize = Maximum + 1;
80 }
81
82 if ((ScreenSize + 2) > DimensionsWidth) {
83 ScreenSize = DimensionsWidth - 2;
84 }
85
86 BufferedString = AllocateZeroPool (ScreenSize * 2);
87 ASSERT (BufferedString);
88
89 Start = (DimensionsWidth - ScreenSize - 2) / 2 + gScreenDimensions.LeftColumn + 1;
90 Top = ((DimensionsHeight - 6) / 2) + gScreenDimensions.TopRow - 1;
91
92 //
93 // Display prompt for string
94 //
95 CreatePopUp (ScreenSize, 4, &NullCharacter, Prompt, Space, &NullCharacter);
96
97 gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_BLACK, EFI_LIGHTGRAY));
98
99 CursorVisible = gST->ConOut->Mode->CursorVisible;
100 gST->ConOut->EnableCursor (gST->ConOut, TRUE);
101
102 do {
103 Status = WaitForKeyStroke (&Key);
104 ASSERT_EFI_ERROR (Status);
105
106 gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_BLACK, EFI_LIGHTGRAY));
107 switch (Key.UnicodeChar) {
108 case CHAR_NULL:
109 switch (Key.ScanCode) {
110 case SCAN_LEFT:
111 break;
112
113 case SCAN_RIGHT:
114 break;
115
116 case SCAN_ESC:
117 FreePool (TempString);
118 FreePool (BufferedString);
119 gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK));
120 gST->ConOut->EnableCursor (gST->ConOut, CursorVisible);
121 return EFI_DEVICE_ERROR;
122
123 default:
124 break;
125 }
126
127 break;
128
129 case CHAR_CARRIAGE_RETURN:
130 if (GetStringWidth (StringPtr) >= ((Minimum + 1) * sizeof (CHAR16))) {
131
132 FreePool (TempString);
133 FreePool (BufferedString);
134 gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK));
135 gST->ConOut->EnableCursor (gST->ConOut, CursorVisible);
136 return EFI_SUCCESS;
137 } else {
138 //
139 // Simply create a popup to tell the user that they had typed in too few characters.
140 // To save code space, we can then treat this as an error and return back to the menu.
141 //
142 do {
143 CreateDialog (4, TRUE, 0, NULL, &Key, &NullCharacter, gMiniString, gPressEnter, &NullCharacter);
144 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);
145
146 FreePool (TempString);
147 FreePool (BufferedString);
148 gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK));
149 gST->ConOut->EnableCursor (gST->ConOut, CursorVisible);
150 return EFI_DEVICE_ERROR;
151 }
152
153 break;
154
155 case CHAR_BACKSPACE:
156 if (StringPtr[0] != CHAR_NULL) {
157 for (Index = 0; StringPtr[Index] != CHAR_NULL; Index++) {
158 TempString[Index] = StringPtr[Index];
159 }
160 //
161 // Effectively truncate string by 1 character
162 //
163 TempString[Index - 1] = CHAR_NULL;
164 StrCpy (StringPtr, TempString);
165 }
166
167 default:
168 //
169 // If it is the beginning of the string, don't worry about checking maximum limits
170 //
171 if ((StringPtr[0] == CHAR_NULL) && (Key.UnicodeChar != CHAR_BACKSPACE)) {
172 StrnCpy (StringPtr, &Key.UnicodeChar, 1);
173 StrnCpy (TempString, &Key.UnicodeChar, 1);
174 } else if ((GetStringWidth (StringPtr) < ((Maximum + 1) * sizeof (CHAR16))) && (Key.UnicodeChar != CHAR_BACKSPACE)) {
175 KeyPad[0] = Key.UnicodeChar;
176 KeyPad[1] = CHAR_NULL;
177 StrCat (StringPtr, KeyPad);
178 StrCat (TempString, KeyPad);
179 }
180
181 //
182 // If the width of the input string is now larger than the screen, we nee to
183 // adjust the index to start printing portions of the string
184 //
185 SetUnicodeMem (BufferedString, ScreenSize - 1, L' ');
186 PrintStringAt (Start + 1, Top + 3, BufferedString);
187
188 if ((GetStringWidth (StringPtr) / 2) > (DimensionsWidth - 2)) {
189 Index = (GetStringWidth (StringPtr) / 2) - DimensionsWidth + 2;
190 } else {
191 Index = 0;
192 }
193
194 if (IsPassword) {
195 gST->ConOut->SetCursorPosition (gST->ConOut, Start + 1, Top + 3);
196 }
197
198 for (Count = 0; Index + 1 < GetStringWidth (StringPtr) / 2; Index++, Count++) {
199 BufferedString[Count] = StringPtr[Index];
200
201 if (IsPassword) {
202 PrintChar (L'*');
203 }
204 }
205
206 if (!IsPassword) {
207 PrintStringAt (Start + 1, Top + 3, BufferedString);
208 }
209 break;
210 }
211
212 gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK));
213 gST->ConOut->SetCursorPosition (gST->ConOut, Start + GetStringWidth (StringPtr) / 2, Top + 3);
214 } while (TRUE);
215
216 }
217
218
219 /**
220 This routine reads a numeric value from the user input.
221
222 @param Selection Pointer to current selection.
223 @param MenuOption Pointer to the current input menu.
224
225 @retval EFI_SUCCESS If numerical input is read successfully
226 @retval EFI_DEVICE_ERROR If operation fails
227
228 **/
229 EFI_STATUS
230 GetNumericInput (
231 IN UI_MENU_SELECTION *Selection,
232 IN UI_MENU_OPTION *MenuOption
233 )
234 {
235 EFI_STATUS Status;
236 UINTN Column;
237 UINTN Row;
238 CHAR16 InputText[23];
239 CHAR16 FormattedNumber[22];
240 UINT64 PreviousNumber[20];
241 UINTN Count;
242 UINTN Loop;
243 BOOLEAN ManualInput;
244 BOOLEAN HexInput;
245 BOOLEAN DateOrTime;
246 UINTN InputWidth;
247 UINT64 EditValue;
248 UINT64 Step;
249 UINT64 Minimum;
250 UINT64 Maximum;
251 UINTN EraseLen;
252 UINT8 Digital;
253 EFI_INPUT_KEY Key;
254 EFI_HII_VALUE *QuestionValue;
255 FORM_BROWSER_FORM *Form;
256 FORM_BROWSER_FORMSET *FormSet;
257 FORM_BROWSER_STATEMENT *Question;
258
259 Column = MenuOption->OptCol;
260 Row = MenuOption->Row;
261 PreviousNumber[0] = 0;
262 Count = 0;
263 InputWidth = 0;
264 Digital = 0;
265
266 FormSet = Selection->FormSet;
267 Form = Selection->Form;
268 Question = MenuOption->ThisTag;
269 QuestionValue = &Question->HiiValue;
270 Step = Question->Step;
271 Minimum = Question->Minimum;
272 Maximum = Question->Maximum;
273
274 if ((Question->Operand == EFI_IFR_DATE_OP) || (Question->Operand == EFI_IFR_TIME_OP)) {
275 DateOrTime = TRUE;
276 } else {
277 DateOrTime = FALSE;
278 }
279
280 //
281 // Prepare Value to be edit
282 //
283 EraseLen = 0;
284 EditValue = 0;
285 if (Question->Operand == EFI_IFR_DATE_OP) {
286 Step = 1;
287 Minimum = 1;
288
289 switch (MenuOption->Sequence) {
290 case 0:
291 Maximum = 12;
292 EraseLen = 4;
293 EditValue = QuestionValue->Value.date.Month;
294 break;
295
296 case 1:
297 Maximum = 31;
298 EraseLen = 3;
299 EditValue = QuestionValue->Value.date.Day;
300 break;
301
302 case 2:
303 Maximum = 0xffff;
304 EraseLen = 5;
305 EditValue = QuestionValue->Value.date.Year;
306 break;
307
308 default:
309 break;
310 }
311 } else if (Question->Operand == EFI_IFR_TIME_OP) {
312 Step = 1;
313 Minimum = 0;
314
315 switch (MenuOption->Sequence) {
316 case 0:
317 Maximum = 23;
318 EraseLen = 4;
319 EditValue = QuestionValue->Value.time.Hour;
320 break;
321
322 case 1:
323 Maximum = 59;
324 EraseLen = 3;
325 EditValue = QuestionValue->Value.time.Minute;
326 break;
327
328 case 2:
329 Maximum = 59;
330 EraseLen = 3;
331 EditValue = QuestionValue->Value.time.Second;
332 break;
333
334 default:
335 break;
336 }
337 } else {
338 //
339 // Numeric
340 //
341 EraseLen = gOptionBlockWidth;
342 EditValue = QuestionValue->Value.u64;
343 if (Maximum == 0) {
344 Maximum = (UINT64) -1;
345 }
346 }
347
348 if (Step == 0) {
349 ManualInput = TRUE;
350 } else {
351 ManualInput = FALSE;
352 }
353
354 if ((Question->Operand == EFI_IFR_NUMERIC_OP) &&
355 ((Question->Flags & EFI_IFR_DISPLAY) == EFI_IFR_DISPLAY_UINT_HEX)) {
356 HexInput = TRUE;
357 } else {
358 HexInput = FALSE;
359 }
360
361 if (ManualInput) {
362 if (HexInput) {
363 InputWidth = Question->StorageWidth * 2;
364 } else {
365 switch (Question->StorageWidth) {
366 case 1:
367 InputWidth = 3;
368 break;
369
370 case 2:
371 InputWidth = 5;
372 break;
373
374 case 4:
375 InputWidth = 10;
376 break;
377
378 case 8:
379 InputWidth = 20;
380 break;
381
382 default:
383 InputWidth = 0;
384 break;
385 }
386 }
387
388 InputText[0] = LEFT_NUMERIC_DELIMITER;
389 SetUnicodeMem (InputText + 1, InputWidth, L' ');
390 InputText[InputWidth + 1] = RIGHT_NUMERIC_DELIMITER;
391 InputText[InputWidth + 2] = L'\0';
392
393 PrintAt (Column, Row, InputText);
394 Column++;
395 }
396
397 //
398 // First time we enter this handler, we need to check to see if
399 // we were passed an increment or decrement directive
400 //
401 do {
402 Key.UnicodeChar = CHAR_NULL;
403 if (gDirection != 0) {
404 Key.ScanCode = gDirection;
405 gDirection = 0;
406 goto TheKey2;
407 }
408
409 Status = WaitForKeyStroke (&Key);
410
411 TheKey2:
412 switch (Key.UnicodeChar) {
413
414 case '+':
415 case '-':
416 if (Key.UnicodeChar == '+') {
417 Key.ScanCode = SCAN_RIGHT;
418 } else {
419 Key.ScanCode = SCAN_LEFT;
420 }
421 Key.UnicodeChar = CHAR_NULL;
422 goto TheKey2;
423
424 case CHAR_NULL:
425 switch (Key.ScanCode) {
426 case SCAN_LEFT:
427 case SCAN_RIGHT:
428 if (DateOrTime) {
429 //
430 // By setting this value, we will return back to the caller.
431 // We need to do this since an auto-refresh will destroy the adjustment
432 // based on what the real-time-clock is showing. So we always commit
433 // upon changing the value.
434 //
435 gDirection = SCAN_DOWN;
436 }
437
438 if (!ManualInput) {
439 if (Key.ScanCode == SCAN_LEFT) {
440 if (EditValue > Step) {
441 EditValue = EditValue - Step;
442 } else {
443 EditValue = Minimum;
444 }
445 } else if (Key.ScanCode == SCAN_RIGHT) {
446 EditValue = EditValue + Step;
447 if (EditValue > Maximum) {
448 EditValue = Maximum;
449 }
450 }
451
452 ZeroMem (FormattedNumber, 21 * sizeof (CHAR16));
453 if (Question->Operand == EFI_IFR_DATE_OP) {
454 if (MenuOption->Sequence == 2) {
455 //
456 // Year
457 //
458 UnicodeSPrint (FormattedNumber, 21 * sizeof (CHAR16), L"%04d", EditValue);
459 } else {
460 //
461 // Month/Day
462 //
463 UnicodeSPrint (FormattedNumber, 21 * sizeof (CHAR16), L"%02d", EditValue);
464 }
465
466 if (MenuOption->Sequence == 0) {
467 FormattedNumber[EraseLen - 2] = DATE_SEPARATOR;
468 } else if (MenuOption->Sequence == 1) {
469 FormattedNumber[EraseLen - 1] = DATE_SEPARATOR;
470 }
471 } else if (Question->Operand == EFI_IFR_TIME_OP) {
472 UnicodeSPrint (FormattedNumber, 21 * sizeof (CHAR16), L"%02d", EditValue);
473
474 if (MenuOption->Sequence == 0) {
475 FormattedNumber[EraseLen - 2] = TIME_SEPARATOR;
476 } else if (MenuOption->Sequence == 1) {
477 FormattedNumber[EraseLen - 1] = TIME_SEPARATOR;
478 }
479 } else {
480 QuestionValue->Value.u64 = EditValue;
481 PrintFormattedNumber (Question, FormattedNumber, 21 * sizeof (CHAR16));
482 }
483
484 gST->ConOut->SetAttribute (gST->ConOut, FIELD_TEXT | FIELD_BACKGROUND);
485 for (Loop = 0; Loop < EraseLen; Loop++) {
486 PrintAt (MenuOption->OptCol + Loop, MenuOption->Row, L" ");
487 }
488 gST->ConOut->SetAttribute (gST->ConOut, FIELD_TEXT_HIGHLIGHT | FIELD_BACKGROUND_HIGHLIGHT);
489
490 if (MenuOption->Sequence == 0) {
491 PrintCharAt (MenuOption->OptCol, Row, LEFT_NUMERIC_DELIMITER);
492 Column = MenuOption->OptCol + 1;
493 }
494
495 PrintStringAt (Column, Row, FormattedNumber);
496
497 if (!DateOrTime || MenuOption->Sequence == 2) {
498 PrintChar (RIGHT_NUMERIC_DELIMITER);
499 }
500 }
501
502 goto EnterCarriageReturn;
503 break;
504
505 case SCAN_UP:
506 case SCAN_DOWN:
507 goto EnterCarriageReturn;
508
509 case SCAN_ESC:
510 return EFI_DEVICE_ERROR;
511
512 default:
513 break;
514 }
515
516 break;
517
518 EnterCarriageReturn:
519
520 case CHAR_CARRIAGE_RETURN:
521 //
522 // Store Edit value back to Question
523 //
524 if (Question->Operand == EFI_IFR_DATE_OP) {
525 switch (MenuOption->Sequence) {
526 case 0:
527 QuestionValue->Value.date.Month = (UINT8) EditValue;
528 break;
529
530 case 1:
531 QuestionValue->Value.date.Day = (UINT8) EditValue;
532 break;
533
534 case 2:
535 QuestionValue->Value.date.Year = (UINT16) EditValue;
536 break;
537
538 default:
539 break;
540 }
541 } else if (Question->Operand == EFI_IFR_TIME_OP) {
542 switch (MenuOption->Sequence) {
543 case 0:
544 QuestionValue->Value.time.Hour = (UINT8) EditValue;
545 break;
546
547 case 1:
548 QuestionValue->Value.time.Minute = (UINT8) EditValue;
549 break;
550
551 case 2:
552 QuestionValue->Value.time.Second = (UINT8) EditValue;
553 break;
554
555 default:
556 break;
557 }
558 } else {
559 //
560 // Numeric
561 //
562 QuestionValue->Value.u64 = EditValue;
563 }
564
565 //
566 // Check to see if the Value is something reasonable against consistency limitations.
567 // If not, let's kick the error specified.
568 //
569 Status = ValidateQuestion (FormSet, Form, Question, EFI_HII_EXPRESSION_INCONSISTENT_IF);
570 if (EFI_ERROR (Status)) {
571 //
572 // Input value is not valid, restore Question Value
573 //
574 GetQuestionValue (FormSet, Form, Question, TRUE);
575 } else {
576 SetQuestionValue (FormSet, Form, Question, TRUE);
577 if (!DateOrTime || (Question->Storage != NULL)) {
578 //
579 // NV flag is unnecessary for RTC type of Date/Time
580 //
581 UpdateStatusBar (NV_UPDATE_REQUIRED, Question->QuestionFlags, TRUE);
582 }
583 }
584
585 return Status;
586 break;
587
588 case CHAR_BACKSPACE:
589 if (ManualInput) {
590 if (Count == 0) {
591 break;
592 }
593 //
594 // Remove a character
595 //
596 EditValue = PreviousNumber[Count - 1];
597 UpdateStatusBar (INPUT_ERROR, Question->QuestionFlags, FALSE);
598 Count--;
599 Column--;
600 PrintAt (Column, Row, L" ");
601 }
602 break;
603
604 default:
605 if (ManualInput) {
606 if (HexInput) {
607 if (!IsHexDigit (&Digital, Key.UnicodeChar)) {
608 UpdateStatusBar (INPUT_ERROR, Question->QuestionFlags, TRUE);
609 break;
610 }
611 } else {
612 if (Key.UnicodeChar > L'9' || Key.UnicodeChar < L'0') {
613 UpdateStatusBar (INPUT_ERROR, Question->QuestionFlags, TRUE);
614 break;
615 }
616 }
617
618 //
619 // If Count exceed input width, there is no way more is valid
620 //
621 if (Count >= InputWidth) {
622 break;
623 }
624 //
625 // Someone typed something valid!
626 //
627 if (Count != 0) {
628 if (HexInput) {
629 EditValue = LShiftU64 (EditValue, 4) + Digital;
630 } else {
631 EditValue = MultU64x32 (EditValue, 10) + (Key.UnicodeChar - L'0');
632 }
633 } else {
634 if (HexInput) {
635 EditValue = Digital;
636 } else {
637 EditValue = Key.UnicodeChar - L'0';
638 }
639 }
640
641 if (EditValue > Maximum) {
642 UpdateStatusBar (INPUT_ERROR, Question->QuestionFlags, TRUE);
643 EditValue = PreviousNumber[Count];
644 break;
645 } else {
646 UpdateStatusBar (INPUT_ERROR, Question->QuestionFlags, FALSE);
647 }
648
649 Count++;
650 PreviousNumber[Count] = EditValue;
651
652 PrintCharAt (Column, Row, Key.UnicodeChar);
653 Column++;
654 }
655 break;
656 }
657 } while (TRUE);
658
659 }
660
661
662 /**
663 Get selection for OneOf and OrderedList (Left/Right will be ignored).
664
665 @param Selection Pointer to current selection.
666 @param MenuOption Pointer to the current input menu.
667
668 @retval EFI_SUCCESS If Option input is processed successfully
669 @retval EFI_DEVICE_ERROR If operation fails
670
671 **/
672 EFI_STATUS
673 GetSelectionInputPopUp (
674 IN UI_MENU_SELECTION *Selection,
675 IN UI_MENU_OPTION *MenuOption
676 )
677 {
678 EFI_STATUS Status;
679 EFI_INPUT_KEY Key;
680 UINTN Index;
681 CHAR16 *StringPtr;
682 CHAR16 *TempStringPtr;
683 UINTN Index2;
684 UINTN TopOptionIndex;
685 UINTN HighlightOptionIndex;
686 UINTN Start;
687 UINTN End;
688 UINTN Top;
689 UINTN Bottom;
690 UINTN PopUpMenuLines;
691 UINTN MenuLinesInView;
692 UINTN PopUpWidth;
693 CHAR16 Character;
694 INT32 SavedAttribute;
695 BOOLEAN ShowDownArrow;
696 BOOLEAN ShowUpArrow;
697 UINTN DimensionsWidth;
698 LIST_ENTRY *Link;
699 BOOLEAN OrderedList;
700 UINT8 *ValueArray;
701 EFI_HII_VALUE HiiValue;
702 EFI_HII_VALUE *HiiValueArray;
703 UINTN OptionCount;
704 QUESTION_OPTION *OneOfOption;
705 QUESTION_OPTION *CurrentOption;
706 FORM_BROWSER_STATEMENT *Question;
707
708 DimensionsWidth = gScreenDimensions.RightColumn - gScreenDimensions.LeftColumn;
709
710 ValueArray = NULL;
711 CurrentOption = NULL;
712 ShowDownArrow = FALSE;
713 ShowUpArrow = FALSE;
714
715 StringPtr = AllocateZeroPool ((gOptionBlockWidth + 1) * 2);
716 ASSERT (StringPtr);
717
718 Question = MenuOption->ThisTag;
719 if (Question->Operand == EFI_IFR_ORDERED_LIST_OP) {
720 ValueArray = Question->BufferValue;
721 OrderedList = TRUE;
722 } else {
723 OrderedList = FALSE;
724 }
725
726 //
727 // Calculate Option count
728 //
729 if (OrderedList) {
730 for (Index = 0; Index < Question->MaxContainers; Index++) {
731 if (ValueArray[Index] == 0) {
732 break;
733 }
734 }
735
736 OptionCount = Index;
737 } else {
738 OptionCount = 0;
739 Link = GetFirstNode (&Question->OptionListHead);
740 while (!IsNull (&Question->OptionListHead, Link)) {
741 OneOfOption = QUESTION_OPTION_FROM_LINK (Link);
742
743 OptionCount++;
744
745 Link = GetNextNode (&Question->OptionListHead, Link);
746 }
747 }
748
749 //
750 // Prepare HiiValue array
751 //
752 HiiValueArray = AllocateZeroPool (OptionCount * sizeof (EFI_HII_VALUE));
753 ASSERT (HiiValueArray != NULL);
754 Link = GetFirstNode (&Question->OptionListHead);
755 for (Index = 0; Index < OptionCount; Index++) {
756 if (OrderedList) {
757 HiiValueArray[Index].Type = EFI_IFR_TYPE_NUM_SIZE_8;
758 HiiValueArray[Index].Value.u8 = ValueArray[Index];
759 } else {
760 OneOfOption = QUESTION_OPTION_FROM_LINK (Link);
761 CopyMem (&HiiValueArray[Index], &OneOfOption->Value, sizeof (EFI_HII_VALUE));
762 Link = GetNextNode (&Question->OptionListHead, Link);
763 }
764 }
765
766 //
767 // Move Suppressed Option to list tail
768 //
769 PopUpMenuLines = 0;
770 for (Index = 0; Index < OptionCount; Index++) {
771 OneOfOption = ValueToOption (Question, &HiiValueArray[OptionCount - Index - 1]);
772 if (OneOfOption == NULL) {
773 return EFI_NOT_FOUND;
774 }
775
776 RemoveEntryList (&OneOfOption->Link);
777
778 if ((OneOfOption->SuppressExpression != NULL) &&
779 (OneOfOption->SuppressExpression->Result.Value.b)) {
780 //
781 // This option is suppressed, insert to tail
782 //
783 InsertTailList (&Question->OptionListHead, &OneOfOption->Link);
784 } else {
785 //
786 // Insert to head
787 //
788 InsertHeadList (&Question->OptionListHead, &OneOfOption->Link);
789
790 PopUpMenuLines++;
791 }
792 }
793
794 //
795 // Get the number of one of options present and its size
796 //
797 PopUpWidth = 0;
798 HighlightOptionIndex = 0;
799 Link = GetFirstNode (&Question->OptionListHead);
800 for (Index = 0; Index < PopUpMenuLines; Index++) {
801 OneOfOption = QUESTION_OPTION_FROM_LINK (Link);
802
803 StringPtr = GetToken (OneOfOption->Text, MenuOption->Handle);
804 if (StrLen (StringPtr) > PopUpWidth) {
805 PopUpWidth = StrLen (StringPtr);
806 }
807 FreePool (StringPtr);
808
809 if (!OrderedList && CompareHiiValue (&Question->HiiValue, &OneOfOption->Value, NULL) == 0) {
810 //
811 // Find current selected Option for OneOf
812 //
813 HighlightOptionIndex = Index;
814 }
815
816 Link = GetNextNode (&Question->OptionListHead, Link);
817 }
818
819 //
820 // Perform popup menu initialization.
821 //
822 PopUpWidth = PopUpWidth + POPUP_PAD_SPACE_COUNT;
823
824 SavedAttribute = gST->ConOut->Mode->Attribute;
825 gST->ConOut->SetAttribute (gST->ConOut, POPUP_TEXT | POPUP_BACKGROUND);
826
827 if ((PopUpWidth + POPUP_FRAME_WIDTH) > DimensionsWidth) {
828 PopUpWidth = DimensionsWidth - POPUP_FRAME_WIDTH;
829 }
830
831 Start = (DimensionsWidth - PopUpWidth - POPUP_FRAME_WIDTH) / 2 + gScreenDimensions.LeftColumn;
832 End = Start + PopUpWidth + POPUP_FRAME_WIDTH;
833 Top = gScreenDimensions.TopRow + NONE_FRONT_PAGE_HEADER_HEIGHT;
834 Bottom = gScreenDimensions.BottomRow - STATUS_BAR_HEIGHT - FOOTER_HEIGHT - 1;
835
836 MenuLinesInView = Bottom - Top - 1;
837 if (MenuLinesInView >= PopUpMenuLines) {
838 Top = Top + (MenuLinesInView - PopUpMenuLines) / 2;
839 Bottom = Top + PopUpMenuLines + 1;
840 } else {
841 ShowDownArrow = TRUE;
842 }
843
844 if (HighlightOptionIndex > (MenuLinesInView - 1)) {
845 TopOptionIndex = HighlightOptionIndex - MenuLinesInView + 1;
846 } else {
847 TopOptionIndex = 0;
848 }
849
850 do {
851 //
852 // Clear that portion of the screen
853 //
854 ClearLines (Start, End, Top, Bottom, POPUP_TEXT | POPUP_BACKGROUND);
855
856 //
857 // Draw "One of" pop-up menu
858 //
859 Character = BOXDRAW_DOWN_RIGHT;
860 PrintCharAt (Start, Top, Character);
861 for (Index = Start; Index + 2 < End; Index++) {
862 if ((ShowUpArrow) && ((Index + 1) == (Start + End) / 2)) {
863 Character = GEOMETRICSHAPE_UP_TRIANGLE;
864 } else {
865 Character = BOXDRAW_HORIZONTAL;
866 }
867
868 PrintChar (Character);
869 }
870
871 Character = BOXDRAW_DOWN_LEFT;
872 PrintChar (Character);
873 Character = BOXDRAW_VERTICAL;
874 for (Index = Top + 1; Index < Bottom; Index++) {
875 PrintCharAt (Start, Index, Character);
876 PrintCharAt (End - 1, Index, Character);
877 }
878
879 //
880 // Move to top Option
881 //
882 Link = GetFirstNode (&Question->OptionListHead);
883 for (Index = 0; Index < TopOptionIndex; Index++) {
884 Link = GetNextNode (&Question->OptionListHead, Link);
885 }
886
887 //
888 // Display the One of options
889 //
890 Index2 = Top + 1;
891 for (Index = TopOptionIndex; (Index < PopUpMenuLines) && (Index2 < Bottom); Index++) {
892 OneOfOption = QUESTION_OPTION_FROM_LINK (Link);
893 Link = GetNextNode (&Question->OptionListHead, Link);
894
895 StringPtr = GetToken (OneOfOption->Text, MenuOption->Handle);
896 //
897 // If the string occupies multiple lines, truncate it to fit in one line,
898 // and append a "..." for indication.
899 //
900 if (StrLen (StringPtr) > (PopUpWidth - 1)) {
901 TempStringPtr = AllocateZeroPool (sizeof (CHAR16) * (PopUpWidth - 1));
902 ASSERT ( TempStringPtr != NULL );
903 CopyMem (TempStringPtr, StringPtr, (sizeof (CHAR16) * (PopUpWidth - 5)));
904 FreePool (StringPtr);
905 StringPtr = TempStringPtr;
906 StrCat (StringPtr, L"...");
907 }
908
909 if (Index == HighlightOptionIndex) {
910 //
911 // Highlight the selected one
912 //
913 CurrentOption = OneOfOption;
914
915 gST->ConOut->SetAttribute (gST->ConOut, PICKLIST_HIGHLIGHT_TEXT | PICKLIST_HIGHLIGHT_BACKGROUND);
916 PrintStringAt (Start + 2, Index2, StringPtr);
917 gST->ConOut->SetAttribute (gST->ConOut, POPUP_TEXT | POPUP_BACKGROUND);
918 } else {
919 gST->ConOut->SetAttribute (gST->ConOut, POPUP_TEXT | POPUP_BACKGROUND);
920 PrintStringAt (Start + 2, Index2, StringPtr);
921 }
922
923 Index2++;
924 FreePool (StringPtr);
925 }
926
927 Character = BOXDRAW_UP_RIGHT;
928 PrintCharAt (Start, Bottom, Character);
929 for (Index = Start; Index + 2 < End; Index++) {
930 if ((ShowDownArrow) && ((Index + 1) == (Start + End) / 2)) {
931 Character = GEOMETRICSHAPE_DOWN_TRIANGLE;
932 } else {
933 Character = BOXDRAW_HORIZONTAL;
934 }
935
936 PrintChar (Character);
937 }
938
939 Character = BOXDRAW_UP_LEFT;
940 PrintChar (Character);
941
942 //
943 // Get User selection
944 //
945 Key.UnicodeChar = CHAR_NULL;
946 if ((gDirection == SCAN_UP) || (gDirection == SCAN_DOWN)) {
947 Key.ScanCode = gDirection;
948 gDirection = 0;
949 goto TheKey;
950 }
951
952 Status = WaitForKeyStroke (&Key);
953
954 TheKey:
955 switch (Key.UnicodeChar) {
956 case '+':
957 if (OrderedList) {
958 if ((TopOptionIndex > 0) && (TopOptionIndex == HighlightOptionIndex)) {
959 //
960 // Highlight reaches the top of the popup window, scroll one menu item.
961 //
962 TopOptionIndex--;
963 ShowDownArrow = TRUE;
964 }
965
966 if (TopOptionIndex == 0) {
967 ShowUpArrow = FALSE;
968 }
969
970 if (HighlightOptionIndex > 0) {
971 HighlightOptionIndex--;
972
973 SwapListEntries (CurrentOption->Link.BackLink, &CurrentOption->Link);
974 }
975 }
976 break;
977
978 case '-':
979 //
980 // If an ordered list op-code, we will allow for a popup of +/- keys
981 // to create an ordered list of items
982 //
983 if (OrderedList) {
984 if (((TopOptionIndex + MenuLinesInView) < PopUpMenuLines) &&
985 (HighlightOptionIndex == (TopOptionIndex + MenuLinesInView - 1))) {
986 //
987 // Highlight reaches the bottom of the popup window, scroll one menu item.
988 //
989 TopOptionIndex++;
990 ShowUpArrow = TRUE;
991 }
992
993 if ((TopOptionIndex + MenuLinesInView) == PopUpMenuLines) {
994 ShowDownArrow = FALSE;
995 }
996
997 if (HighlightOptionIndex < (PopUpMenuLines - 1)) {
998 HighlightOptionIndex++;
999
1000 SwapListEntries (&CurrentOption->Link, CurrentOption->Link.ForwardLink);
1001 }
1002 }
1003 break;
1004
1005 case CHAR_NULL:
1006 switch (Key.ScanCode) {
1007 case SCAN_UP:
1008 case SCAN_DOWN:
1009 if (Key.ScanCode == SCAN_UP) {
1010 if ((TopOptionIndex > 0) && (TopOptionIndex == HighlightOptionIndex)) {
1011 //
1012 // Highlight reaches the top of the popup window, scroll one menu item.
1013 //
1014 TopOptionIndex--;
1015 ShowDownArrow = TRUE;
1016 }
1017
1018 if (TopOptionIndex == 0) {
1019 ShowUpArrow = FALSE;
1020 }
1021
1022 if (HighlightOptionIndex > 0) {
1023 HighlightOptionIndex--;
1024 }
1025 } else {
1026 if (((TopOptionIndex + MenuLinesInView) < PopUpMenuLines) &&
1027 (HighlightOptionIndex == (TopOptionIndex + MenuLinesInView - 1))) {
1028 //
1029 // Highlight reaches the bottom of the popup window, scroll one menu item.
1030 //
1031 TopOptionIndex++;
1032 ShowUpArrow = TRUE;
1033 }
1034
1035 if ((TopOptionIndex + MenuLinesInView) == PopUpMenuLines) {
1036 ShowDownArrow = FALSE;
1037 }
1038
1039 if (HighlightOptionIndex < (PopUpMenuLines - 1)) {
1040 HighlightOptionIndex++;
1041 }
1042 }
1043 break;
1044
1045 case SCAN_ESC:
1046 gST->ConOut->SetAttribute (gST->ConOut, SavedAttribute);
1047
1048 //
1049 // Restore link list order for orderedlist
1050 //
1051 if (OrderedList) {
1052 HiiValue.Type = EFI_IFR_TYPE_NUM_SIZE_8;
1053 HiiValue.Value.u64 = 0;
1054 for (Index = 0; Index < Question->MaxContainers; Index++) {
1055 HiiValue.Value.u8 = ValueArray[Index];
1056 if (HiiValue.Value.u8) {
1057 break;
1058 }
1059
1060 OneOfOption = ValueToOption (Question, &HiiValue);
1061 if (OneOfOption == NULL) {
1062 return EFI_NOT_FOUND;
1063 }
1064
1065 RemoveEntryList (&OneOfOption->Link);
1066 InsertTailList (&Question->OptionListHead, &OneOfOption->Link);
1067 }
1068 }
1069
1070 FreePool (HiiValueArray);
1071 return EFI_DEVICE_ERROR;
1072
1073 default:
1074 break;
1075 }
1076
1077 break;
1078
1079 case CHAR_CARRIAGE_RETURN:
1080 //
1081 // return the current selection
1082 //
1083 if (OrderedList) {
1084 Index = 0;
1085 Link = GetFirstNode (&Question->OptionListHead);
1086 while (!IsNull (&Question->OptionListHead, Link)) {
1087 OneOfOption = QUESTION_OPTION_FROM_LINK (Link);
1088
1089 Question->BufferValue[Index] = OneOfOption->Value.Value.u8;
1090
1091 Index++;
1092 if (Index > Question->MaxContainers) {
1093 break;
1094 }
1095
1096 Link = GetNextNode (&Question->OptionListHead, Link);
1097 }
1098 } else {
1099 CopyMem (&Question->HiiValue, &CurrentOption->Value, sizeof (EFI_HII_VALUE));
1100 }
1101
1102 gST->ConOut->SetAttribute (gST->ConOut, SavedAttribute);
1103 FreePool (HiiValueArray);
1104
1105 Status = ValidateQuestion (Selection->FormSet, Selection->Form, Question, EFI_HII_EXPRESSION_INCONSISTENT_IF);
1106 if (EFI_ERROR (Status)) {
1107 //
1108 // Input value is not valid, restore Question Value
1109 //
1110 GetQuestionValue (Selection->FormSet, Selection->Form, Question, TRUE);
1111 } else {
1112 SetQuestionValue (Selection->FormSet, Selection->Form, Question, TRUE);
1113 UpdateStatusBar (NV_UPDATE_REQUIRED, Question->QuestionFlags, TRUE);
1114 }
1115
1116 return Status;
1117
1118 default:
1119 break;
1120 }
1121 } while (TRUE);
1122
1123 }
1124
1125 /**
1126 Wait for a key to be pressed by user.
1127
1128 @param Key The key which is pressed by user.
1129
1130 @retval EFI_SUCCESS The function always completed successfully.
1131
1132 **/
1133 EFI_STATUS
1134 WaitForKeyStroke (
1135 OUT EFI_INPUT_KEY *Key
1136 )
1137 {
1138 EFI_STATUS Status;
1139
1140 do {
1141 UiWaitForSingleEvent (gST->ConIn->WaitForKey, 0, 0);
1142 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, Key);
1143 } while (EFI_ERROR(Status));
1144
1145 return Status;
1146 }