TianoCore EDK2 master
Loading...
Searching...
No Matches
Console.c
Go to the documentation of this file.
1
9#include "UefiLibInternal.h"
10
11typedef struct {
12 CHAR16 WChar;
13 UINT32 Width;
15
16#define NARROW_CHAR 0xFFF0
17#define WIDE_CHAR 0xFFF1
18
20 //
21 // General script area
22 //
23 { (CHAR16)0x1FFF, 1 },
24
25 /*
26 * Merge the blocks and replace them with the above entry as they fall to
27 * the same category and they are all narrow glyph. This will reduce search
28 * time and table size. The merge will omit the reserved code.
29 *
30 * Remove the above item if below is un-commented.
31 *
32 {(CHAR16)0x007F, 1}, // C0 controls and basic Latin. 0x0000-0x007F
33 {(CHAR16)0x00FF, 1}, // C1 controls and Latin-1 support. 0x0080-0x00FF
34 {(CHAR16)0x017F, 1}, // Latin extended-A. 0x0100-0x017F
35 {(CHAR16)0x024F, 1}, // Latin extended-B. 0x0180-0x024F
36 {(CHAR16)0x02AF, 1}, // IPA extensions. 0x0250-0x02AF
37 {(CHAR16)0x02FF, 1}, // Spacing modifier letters. 0x02B0-0x02FF
38 {(CHAR16)0x036F, 1}, // Combining diacritical marks. 0x0300-0x036F
39 {(CHAR16)0x03FF, 1}, // Greek. 0x0370-0x03FF
40 {(CHAR16)0x04FF, 1}, // Cyrillic. 0x0400-0x04FF
41 {(CHAR16)0x052F, 0}, // Unassigned. As Armenian in ver3.0. 0x0500-0x052F
42 {(CHAR16)0x058F, 1}, // Armenian. 0x0530-0x058F
43 {(CHAR16)0x05FF, 1}, // Hebrew. 0x0590-0x05FF
44 {(CHAR16)0x06FF, 1}, // Arabic. 0x0600-0x06FF
45 {(CHAR16)0x08FF, 0}, // Unassigned. 0x0700-0x08FF
46 {(CHAR16)0x097F, 1}, // Devanagari. 0x0900-0x097F
47 {(CHAR16)0x09FF, 1}, // Bengali. 0x0980-0x09FF
48 {(CHAR16)0x0A7F, 1}, // Gurmukhi. 0x0A00-0x0A7F
49 {(CHAR16)0x0AFF, 1}, // Gujarati. 0x0A80-0x0AFF
50 {(CHAR16)0x0B7F, 1}, // Oriya. 0x0B00-0x0B7F
51 {(CHAR16)0x0BFF, 1}, // Tamil. (See page 7-92). 0x0B80-0x0BFF
52 {(CHAR16)0x0C7F, 1}, // Telugu. 0x0C00-0x0C7F
53 {(CHAR16)0x0CFF, 1}, // Kannada. (See page 7-100). 0x0C80-0x0CFF
54 {(CHAR16)0x0D7F, 1}, // Malayalam (See page 7-104). 0x0D00-0x0D7F
55 {(CHAR16)0x0DFF, 0}, // Unassigned. 0x0D80-0x0DFF
56 {(CHAR16)0x0E7F, 1}, // Thai. 0x0E00-0x0E7F
57 {(CHAR16)0x0EFF, 1}, // Lao. 0x0E80-0x0EFF
58 {(CHAR16)0x0FBF, 1}, // Tibetan. 0x0F00-0x0FBF
59 {(CHAR16)0x109F, 0}, // Unassigned. 0x0FC0-0x109F
60 {(CHAR16)0x10FF, 1}, // Georgian. 0x10A0-0x10FF
61 {(CHAR16)0x11FF, 1}, // Hangul Jamo. 0x1100-0x11FF
62 {(CHAR16)0x1DFF, 0}, // Unassigned. 0x1200-0x1DFF
63 {(CHAR16)0x1EFF, 1}, // Latin extended additional. 0x1E00-0x1EFF
64 {(CHAR16)0x1FFF, 1}, // Greek extended. 0x1F00-0x1FFF
65 *
66 */
67
68 //
69 // Symbol area
70 //
71 { (CHAR16)0x2FFF, 1 },
72
73 /*
74 * Merge the blocks and replace them with the above entry as they fall to
75 * the same category and they are all narrow glyph. This will reduce search
76 * time and table size. The merge will omit the reserved code.
77 *
78 * Remove the above item if below is un-commented.
79 *
80 {(CHAR16)0x206F, 1}, // General punctuation. (See page7-154). 0x200-0x206F
81 {(CHAR16)0x209F, 1}, // Superscripts and subscripts. 0x2070-0x209F
82 {(CHAR16)0x20CF, 1}, // Currency symbols. 0x20A0-0x20CF
83 {(CHAR16)0x20FF, 1}, // Combining diacritical marks for symbols. 0x20D0-0x20FF
84 {(CHAR16)0x214F, 1}, // Letterlike sympbols. 0x2100-0x214F
85 {(CHAR16)0x218F, 1}, // Number forms. 0x2150-0x218F
86 {(CHAR16)0x21FF, 1}, // Arrows. 0x2190-0x21FF
87 {(CHAR16)0x22FF, 1}, // Mathematical operators. 0x2200-0x22FF
88 {(CHAR16)0x23FF, 1}, // Miscellaneous technical. 0x2300-0x23FF
89 {(CHAR16)0x243F, 1}, // Control pictures. 0x2400-0x243F
90 {(CHAR16)0x245F, 1}, // Optical character recognition. 0x2440-0x245F
91 {(CHAR16)0x24FF, 1}, // Enclosed alphanumerics. 0x2460-0x24FF
92 {(CHAR16)0x257F, 1}, // Box drawing. 0x2500-0x257F
93 {(CHAR16)0x259F, 1}, // Block elements. 0x2580-0x259F
94 {(CHAR16)0x25FF, 1}, // Geometric shapes. 0x25A0-0x25FF
95 {(CHAR16)0x26FF, 1}, // Miscellaneous symbols. 0x2600-0x26FF
96 {(CHAR16)0x27BF, 1}, // Dingbats. 0x2700-0x27BF
97 {(CHAR16)0x2FFF, 0}, // Reserved. 0x27C0-0x2FFF
98 *
99 */
100
101 //
102 // CJK phonetics and symbol area
103 //
104 { (CHAR16)0x33FF, 2 },
105
106 /*
107 * Merge the blocks and replace them with the above entry as they fall to
108 * the same category and they are all wide glyph. This will reduce search
109 * time and table size. The merge will omit the reserved code.
110 *
111 * Remove the above item if below is un-commented.
112 *
113 {(CHAR16)0x303F, 2}, // CJK symbols and punctuation. 0x3000-0x303F
114 {(CHAR16)0x309F, 2}, // Hiragana. 0x3040-0x309F
115 {(CHAR16)0x30FF, 2}, // Katakana. 0x30A0-0x30FF
116 {(CHAR16)0x312F, 2}, // Bopomofo. 0x3100-0x312F
117 {(CHAR16)0x318F, 2}, // Hangul compatibility jamo. 0x3130-0x318F
118 {(CHAR16)0x319F, 2}, // Kanbun. 0x3190-0x319F
119 {(CHAR16)0x31FF, 0}, // Reserved. As Bopomofo extended in ver3.0. 0x31A0-0x31FF
120 {(CHAR16)0x32FF, 2}, // Enclosed CJK letters and months. 0x3200-0x32FF
121 {(CHAR16)0x33FF, 2}, // CJK compatibility. 0x3300-0x33FF
122 *
123 */
124
125 //
126 // CJK ideograph area
127 //
128 { (CHAR16)0x9FFF, 2 },
129
130 /*
131 * Merge the blocks and replace them with the above entry as they fall to
132 * the same category and they are all wide glyph. This will reduce search
133 * time and table size. The merge will omit the reserved code.
134 *
135 * Remove the above item if below is un-commented.
136 *
137 {(CHAR16)0x4DFF, 0}, // Reserved. 0x3400-0x4DBF as CJK unified ideographs
138 // extension A in ver3.0. 0x3400-0x4DFF
139 {(CHAR16)0x9FFF, 2}, // CJK unified ideographs. 0x4E00-0x9FFF
140 *
141 */
142
143 //
144 // Reserved
145 //
146 { (CHAR16)0xABFF, 0 }, // Reserved. 0xA000-0xA490 as Yi syllables. 0xA490-0xA4D0
147 // as Yi radicals in ver3.0. 0xA000-0xABFF
148 //
149 // Hangul syllables
150 //
151 { (CHAR16)0xD7FF, 2 },
152
153 /*
154 * Merge the blocks and replace them with the above entry as they fall to
155 * the same category and they are all wide glyph. This will reduce search
156 * time and table size. The merge will omit the reserved code.
157 *
158 * Remove the above item if below is un-commented.
159 *
160 {(CHAR16)0xD7A3, 2}, // Hangul syllables. 0xAC00-0xD7A3
161 {(CHAR16)0xD7FF, 0}, // Reserved. 0xD7A3-0xD7FF
162 *
163 */
164
165 //
166 // Surrogates area
167 //
168 { (CHAR16)0xDFFF, 0 }, // Surrogates, not used now. 0xD800-0xDFFF
169
170 //
171 // Private use area
172 //
173 { (CHAR16)0xF8FF, 0 }, // Private use area. 0xE000-0xF8FF
174
175 //
176 // Compatibility area and specials
177 //
178 { (CHAR16)0xFAFF, 2 }, // CJK compatibility ideographs. 0xF900-0xFAFF
179 { (CHAR16)0xFB4F, 1 }, // Alphabetic presentation forms. 0xFB00-0xFB4F
180 { (CHAR16)0xFDFF, 1 }, // Arabic presentation forms-A. 0xFB50-0xFDFF
181 { (CHAR16)0xFE1F, 0 }, // Reserved. As variation selectors in ver3.0. 0xFE00-0xFE1F
182 { (CHAR16)0xFE2F, 1 }, // Combining half marks. 0xFE20-0xFE2F
183 { (CHAR16)0xFE4F, 2 }, // CJK compatibility forms. 0xFE30-0xFE4F
184 { (CHAR16)0xFE6F, 1 }, // Small Form Variants. 0xFE50-0xFE6F
185 { (CHAR16)0xFEFF, 1 }, // Arabic presentation forms-B. 0xFE70-0xFEFF
186 { (CHAR16)0xFFEF, 1 }, // Half width and full width forms. 0xFF00-0xFFEF
187 { (CHAR16)0xFFFF, 0 }, // Speicials. 0xFFF0-0xFFFF
188};
189
203UINTN
204EFIAPI
206 IN CHAR16 UnicodeChar
207 )
208{
209 UINTN Index;
210 UINTN Low;
211 UINTN High;
213
214 Item = NULL;
215 Low = 0;
216 High = (sizeof (mUnicodeWidthTable)) / (sizeof (UNICODE_WIDTH_ENTRY)) - 1;
217 while (Low <= High) {
218 Index = (Low + High) >> 1;
219 Item = &(mUnicodeWidthTable[Index]);
220 if (Index == 0) {
221 if (UnicodeChar <= Item->WChar) {
222 break;
223 }
224
225 return 0;
226 }
227
228 if (UnicodeChar > Item->WChar) {
229 Low = Index + 1;
230 } else if (UnicodeChar <= mUnicodeWidthTable[Index - 1].WChar) {
231 High = Index - 1;
232 } else {
233 //
234 // Index - 1 < UnicodeChar <= Index. Found
235 //
236 break;
237 }
238 }
239
240 if (Low <= High) {
241 return Item->Width;
242 }
243
244 return 0;
245}
246
263UINTN
264EFIAPI
266 IN CONST CHAR16 *String
267 )
268{
269 UINTN Length;
270 UINTN Width;
271
272 if (String == NULL) {
273 return 0;
274 }
275
276 Length = 0;
277 while (*String != 0) {
278 Width = GetGlyphWidth (*String);
279 if (Width == 0) {
280 return 0;
281 }
282
283 Length += Width;
284 String++;
285 }
286
287 return Length;
288}
289
307UINTN
309 IN CHAR16 *String,
310 IN BOOLEAN LimitLen,
311 IN UINTN MaxWidth,
312 OUT UINTN *Offset
313 )
314{
315 UINTN Index;
316 UINTN Count;
317 UINTN IncrementValue;
318
319 if (String == NULL) {
320 return 0;
321 }
322
323 Index = 0;
324 Count = 0;
325 IncrementValue = 1;
326
327 do {
328 //
329 // Advance to the null-terminator or to the first width directive
330 //
331 for ( ; (String[Index] != NARROW_CHAR) && (String[Index] != WIDE_CHAR) && (String[Index] != 0); Index++) {
332 Count = Count + IncrementValue;
333
334 if (LimitLen && (Count > MaxWidth)) {
335 break;
336 }
337 }
338
339 //
340 // We hit the null-terminator, we now have a count
341 //
342 if (String[Index] == 0) {
343 break;
344 }
345
346 if (LimitLen && (Count > MaxWidth)) {
347 *Offset = Index;
348 break;
349 }
350
351 //
352 // We encountered a narrow directive - strip it from the size calculation since it doesn't get printed
353 // and also set the flag that determines what we increment by.(if narrow, increment by 1, if wide increment by 2)
354 //
355 if (String[Index] == NARROW_CHAR) {
356 //
357 // Skip to the next character
358 //
359 Index++;
360 IncrementValue = 1;
361 } else {
362 //
363 // Skip to the next character
364 //
365 Index++;
366 IncrementValue = 2;
367 }
368 } while (String[Index] != 0);
369
370 return Count * sizeof (CHAR16);
371}
372
391VOID
392EFIAPI
394 IN UINTN Attribute,
395 OUT EFI_INPUT_KEY *Key OPTIONAL,
396 ...
397 )
398{
399 EFI_STATUS Status;
400 VA_LIST Args;
402 EFI_SIMPLE_TEXT_OUTPUT_MODE SavedConsoleMode;
403 UINTN Columns;
404 UINTN Rows;
405 UINTN Column;
406 UINTN Row;
407 UINTN NumberOfLines;
408 UINTN MaxLength;
409 CHAR16 *String;
410 UINTN Length;
411 CHAR16 *Line;
412 UINTN EventIndex;
413 CHAR16 *TmpString;
414
415 //
416 // Determine the length of the longest line in the popup and the the total
417 // number of lines in the popup
418 //
419 VA_START (Args, Key);
420 MaxLength = 0;
421 NumberOfLines = 0;
422 while ((String = VA_ARG (Args, CHAR16 *)) != NULL) {
423 MaxLength = MAX (MaxLength, UefiLibGetStringWidth (String, FALSE, 0, NULL) / 2);
424 NumberOfLines++;
425 }
426
427 VA_END (Args);
428
429 //
430 // If the total number of lines in the popup is zero, then ASSERT()
431 //
432 ASSERT (NumberOfLines != 0);
433
434 //
435 // If the maximum length of all the strings is zero, then ASSERT()
436 //
437 ASSERT (MaxLength != 0);
438
439 //
440 // Cache a pointer to the Simple Text Output Protocol in the EFI System Table
441 //
442 ConOut = gST->ConOut;
443
444 //
445 // Save the current console cursor position and attributes
446 //
447 CopyMem (&SavedConsoleMode, ConOut->Mode, sizeof (SavedConsoleMode));
448
449 //
450 // Retrieve the number of columns and rows in the current console mode
451 //
452 ConOut->QueryMode (ConOut, SavedConsoleMode.Mode, &Columns, &Rows);
453
454 //
455 // Disable cursor and set the foreground and background colors specified by Attribute
456 //
457 ConOut->EnableCursor (ConOut, FALSE);
458 ConOut->SetAttribute (ConOut, Attribute);
459
460 //
461 // Limit NumberOfLines to height of the screen minus 3 rows for the box itself
462 //
463 NumberOfLines = MIN (NumberOfLines, Rows - 3);
464
465 //
466 // Limit MaxLength to width of the screen minus 2 columns for the box itself
467 //
468 MaxLength = MIN (MaxLength, Columns - 2);
469
470 //
471 // Compute the starting row and starting column for the popup
472 //
473 Row = (Rows - (NumberOfLines + 3)) / 2;
474 Column = (Columns - (MaxLength + 2)) / 2;
475
476 //
477 // Allocate a buffer for a single line of the popup with borders and a Null-terminator
478 //
479 Line = AllocateZeroPool ((MaxLength + 3) * sizeof (CHAR16));
480
481 if (Line == NULL) {
482 ASSERT (Line != NULL);
483 return;
484 }
485
486 //
487 // Draw top of popup box
488 //
489 SetMem16 (Line, (MaxLength + 2) * 2, BOXDRAW_HORIZONTAL);
490 Line[0] = BOXDRAW_DOWN_RIGHT;
491 Line[MaxLength + 1] = BOXDRAW_DOWN_LEFT;
492 Line[MaxLength + 2] = L'\0';
493 ConOut->SetCursorPosition (ConOut, Column, Row++);
494 ConOut->OutputString (ConOut, Line);
495
496 //
497 // Draw middle of the popup with strings
498 //
499 VA_START (Args, Key);
500 while ((String = VA_ARG (Args, CHAR16 *)) != NULL && NumberOfLines > 0) {
501 SetMem16 (Line, (MaxLength + 2) * 2, L' ');
502 Line[0] = BOXDRAW_VERTICAL;
503 Line[MaxLength + 1] = BOXDRAW_VERTICAL;
504 Line[MaxLength + 2] = L'\0';
505 ConOut->SetCursorPosition (ConOut, Column, Row);
506 ConOut->OutputString (ConOut, Line);
507 Length = UefiLibGetStringWidth (String, FALSE, 0, NULL) / 2;
508 if (Length <= MaxLength) {
509 //
510 // Length <= MaxLength
511 //
512 ConOut->SetCursorPosition (ConOut, Column + 1 + (MaxLength - Length) / 2, Row++);
513 ConOut->OutputString (ConOut, String);
514 } else {
515 //
516 // Length > MaxLength
517 //
518 UefiLibGetStringWidth (String, TRUE, MaxLength, &Length);
519 TmpString = AllocateZeroPool ((Length + 1) * sizeof (CHAR16));
520
521 if (TmpString == NULL) {
522 ASSERT (TmpString != NULL);
523 break;
524 }
525
526 StrnCpyS (TmpString, Length + 1, String, Length - 3);
527 StrCatS (TmpString, Length + 1, L"...");
528
529 ConOut->SetCursorPosition (ConOut, Column + 1, Row++);
530 ConOut->OutputString (ConOut, TmpString);
531 FreePool (TmpString);
532 }
533
534 NumberOfLines--;
535 }
536
537 VA_END (Args);
538
539 //
540 // Draw bottom of popup box
541 //
542 SetMem16 (Line, (MaxLength + 2) * 2, BOXDRAW_HORIZONTAL);
543 Line[0] = BOXDRAW_UP_RIGHT;
544 Line[MaxLength + 1] = BOXDRAW_UP_LEFT;
545 Line[MaxLength + 2] = L'\0';
546 ConOut->SetCursorPosition (ConOut, Column, Row++);
547 ConOut->OutputString (ConOut, Line);
548
549 //
550 // Free the allocated line buffer
551 //
552 FreePool (Line);
553
554 //
555 // Restore the cursor visibility, position, and attributes
556 //
557 ConOut->EnableCursor (ConOut, SavedConsoleMode.CursorVisible);
558 ConOut->SetCursorPosition (ConOut, SavedConsoleMode.CursorColumn, SavedConsoleMode.CursorRow);
559 ConOut->SetAttribute (ConOut, SavedConsoleMode.Attribute);
560
561 //
562 // Wait for a keystroke
563 //
564 if (Key != NULL) {
565 while (TRUE) {
566 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, Key);
567 if (!EFI_ERROR (Status)) {
568 break;
569 }
570
571 //
572 // If we encounter error, continue to read another key in.
573 //
574 if (Status != EFI_NOT_READY) {
575 continue;
576 }
577
578 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
579 }
580 }
581}
UINT64 UINTN
RETURN_STATUS EFIAPI StrCatS(IN OUT CHAR16 *Destination, IN UINTN DestMax, IN CONST CHAR16 *Source)
Definition: SafeString.c:405
RETURN_STATUS EFIAPI StrnCpyS(OUT CHAR16 *Destination, IN UINTN DestMax, IN CONST CHAR16 *Source, IN UINTN Length)
Definition: SafeString.c:310
VOID *EFIAPI SetMem16(OUT VOID *Buffer, IN UINTN Length, IN UINT16 Value)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
UINTN UefiLibGetStringWidth(IN CHAR16 *String, IN BOOLEAN LimitLen, IN UINTN MaxWidth, OUT UINTN *Offset)
Definition: Console.c:308
UINTN EFIAPI UnicodeStringDisplayLength(IN CONST CHAR16 *String)
Definition: Console.c:265
UINTN EFIAPI GetGlyphWidth(IN CHAR16 UnicodeChar)
Definition: Console.c:205
VOID EFIAPI CreatePopUp(IN UINTN Attribute, OUT EFI_INPUT_KEY *Key OPTIONAL,...)
Definition: Console.c:393
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define MIN(a, b)
Definition: Base.h:1007
#define VA_ARG(Marker, TYPE)
Definition: Base.h:679
#define VA_START(Marker, Parameter)
Definition: Base.h:661
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
CHAR8 * VA_LIST
Definition: Base.h:643
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define GLOBAL_REMOVE_IF_UNREFERENCED
Definition: Base.h:48
#define MAX(a, b)
Definition: Base.h:992
#define VA_END(Marker)
Definition: Base.h:691
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
EFI_SYSTEM_TABLE * gST
EFI_BOOT_SERVICES * gBS
EFI_SIMPLE_TEXT_INPUT_PROTOCOL * ConIn
Definition: UefiSpec.h:2053
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * ConOut
Definition: UefiSpec.h:2064