]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkPkg/Library/FrameworkUefiLib/Console.c
IntelFrameworkPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / IntelFrameworkPkg / Library / FrameworkUefiLib / Console.c
CommitLineData
79964ac8 1/** @file\r
bf428cb3 2 This module provide help function for displaying unicode string.\r
79964ac8 3\r
1c2f052d 4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
22a69a5e 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
79964ac8 6\r
79964ac8 7**/\r
8\r
4ea439fb 9\r
10\r
11\r
bf428cb3 12#include "UefiLibInternal.h"\r
13\r
79964ac8 14typedef struct {\r
15 CHAR16 WChar;\r
16 UINT32 Width;\r
17} UNICODE_WIDTH_ENTRY;\r
18\r
bf428cb3 19GLOBAL_REMOVE_IF_UNREFERENCED CONST UNICODE_WIDTH_ENTRY mUnicodeWidthTable[] = {\r
79964ac8 20 //\r
21 // General script area\r
22 //\r
23 {(CHAR16)0x1FFF, 1},\r
24 /*\r
1c2f052d 25 * Merge the blocks and replace them with the above entry as they fall to\r
79964ac8 26 * the same category and they are all narrow glyph. This will reduce search\r
27 * time and table size. The merge will omit the reserved code.\r
28 *\r
29 * Remove the above item if below is un-commented.\r
30 *\r
31 {(CHAR16)0x007F, 1}, // C0 controls and basic Latin. 0x0000-0x007F\r
32 {(CHAR16)0x00FF, 1}, // C1 controls and Latin-1 support. 0x0080-0x00FF\r
33 {(CHAR16)0x017F, 1}, // Latin extended-A. 0x0100-0x017F\r
34 {(CHAR16)0x024F, 1}, // Latin extended-B. 0x0180-0x024F\r
35 {(CHAR16)0x02AF, 1}, // IPA extensions. 0x0250-0x02AF\r
36 {(CHAR16)0x02FF, 1}, // Spacing modifier letters. 0x02B0-0x02FF\r
37 {(CHAR16)0x036F, 1}, // Combining diacritical marks. 0x0300-0x036F\r
38 {(CHAR16)0x03FF, 1}, // Greek. 0x0370-0x03FF\r
39 {(CHAR16)0x04FF, 1}, // Cyrillic. 0x0400-0x04FF\r
40 {(CHAR16)0x052F, 0}, // Unassigned. As Armenian in ver3.0. 0x0500-0x052F\r
41 {(CHAR16)0x058F, 1}, // Armenian. 0x0530-0x058F\r
42 {(CHAR16)0x05FF, 1}, // Hebrew. 0x0590-0x05FF\r
43 {(CHAR16)0x06FF, 1}, // Arabic. 0x0600-0x06FF\r
44 {(CHAR16)0x08FF, 0}, // Unassigned. 0x0700-0x08FF\r
45 {(CHAR16)0x097F, 1}, // Devanagari. 0x0900-0x097F\r
46 {(CHAR16)0x09FF, 1}, // Bengali. 0x0980-0x09FF\r
47 {(CHAR16)0x0A7F, 1}, // Gurmukhi. 0x0A00-0x0A7F\r
48 {(CHAR16)0x0AFF, 1}, // Gujarati. 0x0A80-0x0AFF\r
49 {(CHAR16)0x0B7F, 1}, // Oriya. 0x0B00-0x0B7F\r
50 {(CHAR16)0x0BFF, 1}, // Tamil. (See page 7-92). 0x0B80-0x0BFF\r
51 {(CHAR16)0x0C7F, 1}, // Telugu. 0x0C00-0x0C7F\r
52 {(CHAR16)0x0CFF, 1}, // Kannada. (See page 7-100). 0x0C80-0x0CFF\r
53 {(CHAR16)0x0D7F, 1}, // Malayalam (See page 7-104). 0x0D00-0x0D7F\r
54 {(CHAR16)0x0DFF, 0}, // Unassigned. 0x0D80-0x0DFF\r
55 {(CHAR16)0x0E7F, 1}, // Thai. 0x0E00-0x0E7F\r
56 {(CHAR16)0x0EFF, 1}, // Lao. 0x0E80-0x0EFF\r
57 {(CHAR16)0x0FBF, 1}, // Tibetan. 0x0F00-0x0FBF\r
58 {(CHAR16)0x109F, 0}, // Unassigned. 0x0FC0-0x109F\r
59 {(CHAR16)0x10FF, 1}, // Georgian. 0x10A0-0x10FF\r
60 {(CHAR16)0x11FF, 1}, // Hangul Jamo. 0x1100-0x11FF\r
61 {(CHAR16)0x1DFF, 0}, // Unassigned. 0x1200-0x1DFF\r
62 {(CHAR16)0x1EFF, 1}, // Latin extended additional. 0x1E00-0x1EFF\r
63 {(CHAR16)0x1FFF, 1}, // Greek extended. 0x1F00-0x1FFF\r
64 *\r
65 */\r
66\r
67 //\r
68 // Symbol area\r
69 //\r
70 {(CHAR16)0x2FFF, 1},\r
71 /*\r
1c2f052d 72 * Merge the blocks and replace them with the above entry as they fall to\r
79964ac8 73 * the same category and they are all narrow glyph. This will reduce search\r
74 * time and table size. The merge will omit the reserved code.\r
75 *\r
76 * Remove the above item if below is un-commented.\r
77 *\r
78 {(CHAR16)0x206F, 1}, // General punctuation. (See page7-154). 0x200-0x206F\r
79 {(CHAR16)0x209F, 1}, // Superscripts and subscripts. 0x2070-0x209F\r
80 {(CHAR16)0x20CF, 1}, // Currency symbols. 0x20A0-0x20CF\r
81 {(CHAR16)0x20FF, 1}, // Combining diacritical marks for symbols. 0x20D0-0x20FF\r
82 {(CHAR16)0x214F, 1}, // Letterlike sympbols. 0x2100-0x214F\r
83 {(CHAR16)0x218F, 1}, // Number forms. 0x2150-0x218F\r
84 {(CHAR16)0x21FF, 1}, // Arrows. 0x2190-0x21FF\r
85 {(CHAR16)0x22FF, 1}, // Mathematical operators. 0x2200-0x22FF\r
86 {(CHAR16)0x23FF, 1}, // Miscellaneous technical. 0x2300-0x23FF\r
87 {(CHAR16)0x243F, 1}, // Control pictures. 0x2400-0x243F\r
88 {(CHAR16)0x245F, 1}, // Optical character recognition. 0x2440-0x245F\r
89 {(CHAR16)0x24FF, 1}, // Enclosed alphanumerics. 0x2460-0x24FF\r
90 {(CHAR16)0x257F, 1}, // Box drawing. 0x2500-0x257F\r
91 {(CHAR16)0x259F, 1}, // Block elements. 0x2580-0x259F\r
92 {(CHAR16)0x25FF, 1}, // Geometric shapes. 0x25A0-0x25FF\r
93 {(CHAR16)0x26FF, 1}, // Miscellaneous symbols. 0x2600-0x26FF\r
94 {(CHAR16)0x27BF, 1}, // Dingbats. 0x2700-0x27BF\r
95 {(CHAR16)0x2FFF, 0}, // Reserved. 0x27C0-0x2FFF\r
96 *\r
97 */\r
98\r
99 //\r
100 // CJK phonetics and symbol area\r
101 //\r
102 {(CHAR16)0x33FF, 2},\r
103 /*\r
1c2f052d 104 * Merge the blocks and replace them with the above entry as they fall to\r
79964ac8 105 * the same category and they are all wide glyph. This will reduce search\r
106 * time and table size. The merge will omit the reserved code.\r
107 *\r
108 * Remove the above item if below is un-commented.\r
109 *\r
110 {(CHAR16)0x303F, 2}, // CJK symbols and punctuation. 0x3000-0x303F\r
111 {(CHAR16)0x309F, 2}, // Hiragana. 0x3040-0x309F\r
112 {(CHAR16)0x30FF, 2}, // Katakana. 0x30A0-0x30FF\r
113 {(CHAR16)0x312F, 2}, // Bopomofo. 0x3100-0x312F\r
114 {(CHAR16)0x318F, 2}, // Hangul compatibility jamo. 0x3130-0x318F\r
115 {(CHAR16)0x319F, 2}, // Kanbun. 0x3190-0x319F\r
116 {(CHAR16)0x31FF, 0}, // Reserved. As Bopomofo extended in ver3.0. 0x31A0-0x31FF\r
117 {(CHAR16)0x32FF, 2}, // Enclosed CJK letters and months. 0x3200-0x32FF\r
118 {(CHAR16)0x33FF, 2}, // CJK compatibility. 0x3300-0x33FF\r
119 *\r
120 */\r
121\r
122 //\r
123 // CJK ideograph area\r
124 //\r
125 {(CHAR16)0x9FFF, 2},\r
126 /*\r
1c2f052d 127 * Merge the blocks and replace them with the above entry as they fall to\r
79964ac8 128 * the same category and they are all wide glyph. This will reduce search\r
129 * time and table size. The merge will omit the reserved code.\r
130 *\r
131 * Remove the above item if below is un-commented.\r
132 *\r
1c2f052d 133 {(CHAR16)0x4DFF, 0}, // Reserved. 0x3400-0x4DBF as CJK unified ideographs\r
79964ac8 134 // extension A in ver3.0. 0x3400-0x4DFF\r
135 {(CHAR16)0x9FFF, 2}, // CJK unified ideographs. 0x4E00-0x9FFF\r
136 *\r
137 */\r
138\r
139 //\r
140 // Reserved\r
141 //\r
142 {(CHAR16)0xABFF, 0}, // Reserved. 0xA000-0xA490 as Yi syllables. 0xA490-0xA4D0\r
143 // as Yi radicals in ver3.0. 0xA000-0xABFF\r
144 //\r
145 // Hangul syllables\r
146 //\r
147 {(CHAR16)0xD7FF, 2},\r
148 /*\r
1c2f052d 149 * Merge the blocks and replace them with the above entry as they fall to\r
79964ac8 150 * the same category and they are all wide glyph. This will reduce search\r
151 * time and table size. The merge will omit the reserved code.\r
152 *\r
153 * Remove the above item if below is un-commented.\r
154 *\r
155 {(CHAR16)0xD7A3, 2}, // Hangul syllables. 0xAC00-0xD7A3\r
156 {(CHAR16)0xD7FF, 0}, // Reserved. 0xD7A3-0xD7FF\r
157 *\r
158 */\r
159\r
160 //\r
161 // Surrogates area\r
162 //\r
163 {(CHAR16)0xDFFF, 0}, // Surrogates, not used now. 0xD800-0xDFFF\r
164\r
165 //\r
166 // Private use area\r
167 //\r
168 {(CHAR16)0xF8FF, 0}, // Private use area. 0xE000-0xF8FF\r
169\r
170 //\r
171 // Compatibility area and specials\r
172 //\r
173 {(CHAR16)0xFAFF, 2}, // CJK compatibility ideographs. 0xF900-0xFAFF\r
174 {(CHAR16)0xFB4F, 1}, // Alphabetic presentation forms. 0xFB00-0xFB4F\r
175 {(CHAR16)0xFDFF, 1}, // Arabic presentation forms-A. 0xFB50-0xFDFF\r
176 {(CHAR16)0xFE1F, 0}, // Reserved. As variation selectors in ver3.0. 0xFE00-0xFE1F\r
177 {(CHAR16)0xFE2F, 1}, // Combining half marks. 0xFE20-0xFE2F\r
178 {(CHAR16)0xFE4F, 2}, // CJK compatibility forms. 0xFE30-0xFE4F\r
179 {(CHAR16)0xFE6F, 1}, // Small Form Variants. 0xFE50-0xFE6F\r
180 {(CHAR16)0xFEFF, 1}, // Arabic presentation forms-B. 0xFE70-0xFEFF\r
181 {(CHAR16)0xFFEF, 1}, // Half width and full width forms. 0xFF00-0xFFEF\r
182 {(CHAR16)0xFFFF, 0}, // Speicials. 0xFFF0-0xFFFF\r
183};\r
184\r
185/**\r
bf428cb3 186 Retrieves the width of a Unicode character.\r
187\r
188 This function computes and returns the width of the Unicode character specified\r
189 by UnicodeChar.\r
79964ac8 190\r
191 @param UnicodeChar A Unicode character.\r
192\r
193 @retval 0 The width if UnicodeChar could not be determined.\r
194 @retval 1 UnicodeChar is a narrow glyph.\r
195 @retval 2 UnicodeChar is a wide glyph.\r
4ea439fb 196\r
79964ac8 197**/\r
198UINTN\r
199EFIAPI\r
200GetGlyphWidth (\r
409f118c 201 IN CHAR16 UnicodeChar\r
79964ac8 202 )\r
203{\r
409f118c 204 UINTN Index;\r
205 UINTN Low;\r
206 UINTN High;\r
207 CONST UNICODE_WIDTH_ENTRY *Item;\r
79964ac8 208\r
209 Item = NULL;\r
210 Low = 0;\r
211 High = (sizeof (mUnicodeWidthTable)) / (sizeof (UNICODE_WIDTH_ENTRY)) - 1;\r
1c2f052d 212 while (Low <= High) {\r
79964ac8 213 Index = (Low + High) >> 1;\r
214 Item = &(mUnicodeWidthTable[Index]);\r
215 if (Index == 0) {\r
216 if (UnicodeChar <= Item->WChar) {\r
217 break;\r
218 }\r
4ea439fb 219\r
79964ac8 220 return 0;\r
221 }\r
222\r
223 if (UnicodeChar > Item->WChar) {\r
224 Low = Index + 1;\r
225 } else if (UnicodeChar <= mUnicodeWidthTable[Index - 1].WChar) {\r
226 High = Index - 1;\r
227 } else {\r
228 //\r
229 // Index - 1 < UnicodeChar <= Index. Found\r
230 //\r
231 break;\r
232 }\r
233 }\r
234\r
235 if (Low <= High) {\r
236 return Item->Width;\r
237 }\r
238\r
239 return 0;\r
240}\r
241\r
242/**\r
bf428cb3 243 Computes the display length of a Null-terminated Unicode String.\r
244\r
245 This function computes and returns the display length of the Null-terminated Unicode\r
246 string specified by String. If String is NULL then 0 is returned. If any of the widths\r
247 of the Unicode characters in String can not be determined, then 0 is returned. The display\r
248 width of String can be computed by summing the display widths of each Unicode character\r
249 in String. Unicode characters that are narrow glyphs have a width of 1, and Unicode\r
1c2f052d 250 characters that are width glyphs have a width of 2.\r
bf428cb3 251 If String is not aligned on a 16-bit boundary, then ASSERT().\r
79964ac8 252\r
409f118c 253 @param String A pointer to a Null-terminated Unicode string.\r
79964ac8 254\r
409f118c 255 @return The display length of the Null-terminated Unicode string specified by String.\r
1c2f052d 256\r
79964ac8 257**/\r
258UINTN\r
259EFIAPI\r
260UnicodeStringDisplayLength (\r
409f118c 261 IN CONST CHAR16 *String\r
79964ac8 262 )\r
263{\r
409f118c 264 UINTN Length;\r
265 UINTN Width;\r
79964ac8 266\r
267 if (String == NULL) {\r
268 return 0;\r
269 }\r
270\r
271 Length = 0;\r
272 while (*String != 0) {\r
273 Width = GetGlyphWidth (*String);\r
274 if (Width == 0) {\r
275 return 0;\r
276 }\r
277\r
278 Length += Width;\r
279 String++;\r
280 }\r
281\r
282 return Length;\r
283}\r
4ea439fb 284\r
285/**\r
1c2f052d 286 Draws a dialog box to the console output device specified by\r
4ea439fb 287 ConOut defined in the EFI_SYSTEM_TABLE and waits for a keystroke\r
1c2f052d 288 from the console input device specified by ConIn defined in the\r
4ea439fb 289 EFI_SYSTEM_TABLE.\r
290\r
291 If there are no strings in the variable argument list, then ASSERT().\r
292 If all the strings in the variable argument list are empty, then ASSERT().\r
293\r
294 @param[in] Attribute Specifies the foreground and background color of the popup.\r
1c2f052d 295 @param[out] Key A pointer to the EFI_KEY value of the key that was\r
4ea439fb 296 pressed. This is an optional parameter that may be NULL.\r
297 If it is NULL then no wait for a keypress will be performed.\r
298 @param[in] ... The variable argument list that contains pointers to Null-\r
1c2f052d 299 terminated Unicode strings to display in the dialog box.\r
4ea439fb 300 The variable argument list is terminated by a NULL.\r
301\r
302**/\r
303VOID\r
304EFIAPI\r
305CreatePopUp (\r
1c2f052d 306 IN UINTN Attribute,\r
4ea439fb 307 OUT EFI_INPUT_KEY *Key, OPTIONAL\r
308 ...\r
309 )\r
310{\r
abe4c459 311 EFI_STATUS Status;\r
4ea439fb 312 VA_LIST Args;\r
313 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ConOut;\r
314 EFI_SIMPLE_TEXT_OUTPUT_MODE SavedConsoleMode;\r
315 UINTN Columns;\r
316 UINTN Rows;\r
317 UINTN Column;\r
318 UINTN Row;\r
319 UINTN NumberOfLines;\r
320 UINTN MaxLength;\r
321 CHAR16 *String;\r
322 UINTN Length;\r
323 CHAR16 *Line;\r
324 UINTN EventIndex;\r
325\r
326 //\r
1c2f052d 327 // Determine the length of the longest line in the popup and the the total\r
4ea439fb 328 // number of lines in the popup\r
329 //\r
330 VA_START (Args, Key);\r
331 MaxLength = 0;\r
332 NumberOfLines = 0;\r
333 while ((String = VA_ARG (Args, CHAR16 *)) != NULL) {\r
334 MaxLength = MAX (MaxLength, StrLen (String));\r
335 NumberOfLines++;\r
336 }\r
337 VA_END (Args);\r
338\r
339 //\r
340 // If the total number of lines in the popup is zero, then ASSERT()\r
341 //\r
342 ASSERT (NumberOfLines != 0);\r
343\r
344 //\r
345 // If the maximum length of all the strings is zero, then ASSERT()\r
346 //\r
347 ASSERT (MaxLength != 0);\r
348\r
349 //\r
350 // Cache a pointer to the Simple Text Output Protocol in the EFI System Table\r
351 //\r
352 ConOut = gST->ConOut;\r
1c2f052d 353\r
4ea439fb 354 //\r
355 // Save the current console cursor position and attributes\r
356 //\r
357 CopyMem (&SavedConsoleMode, ConOut->Mode, sizeof (SavedConsoleMode));\r
358\r
359 //\r
360 // Retrieve the number of columns and rows in the current console mode\r
361 //\r
362 ConOut->QueryMode (ConOut, SavedConsoleMode.Mode, &Columns, &Rows);\r
363\r
364 //\r
365 // Disable cursor and set the foreground and background colors specified by Attribute\r
366 //\r
367 ConOut->EnableCursor (ConOut, FALSE);\r
368 ConOut->SetAttribute (ConOut, Attribute);\r
369\r
370 //\r
371 // Limit NumberOfLines to height of the screen minus 3 rows for the box itself\r
372 //\r
373 NumberOfLines = MIN (NumberOfLines, Rows - 3);\r
374\r
375 //\r
376 // Limit MaxLength to width of the screen minus 2 columns for the box itself\r
377 //\r
378 MaxLength = MIN (MaxLength, Columns - 2);\r
379\r
380 //\r
381 // Compute the starting row and starting column for the popup\r
382 //\r
383 Row = (Rows - (NumberOfLines + 3)) / 2;\r
384 Column = (Columns - (MaxLength + 2)) / 2;\r
385\r
386 //\r
387 // Allocate a buffer for a single line of the popup with borders and a Null-terminator\r
388 //\r
389 Line = AllocateZeroPool ((MaxLength + 3) * sizeof (CHAR16));\r
390 ASSERT (Line != NULL);\r
391\r
392 //\r
1c2f052d 393 // Draw top of popup box\r
4ea439fb 394 //\r
395 SetMem16 (Line, (MaxLength + 2) * 2, BOXDRAW_HORIZONTAL);\r
396 Line[0] = BOXDRAW_DOWN_RIGHT;\r
397 Line[MaxLength + 1] = BOXDRAW_DOWN_LEFT;\r
398 Line[MaxLength + 2] = L'\0';\r
399 ConOut->SetCursorPosition (ConOut, Column, Row++);\r
400 ConOut->OutputString (ConOut, Line);\r
401\r
402 //\r
403 // Draw middle of the popup with strings\r
404 //\r
405 VA_START (Args, Key);\r
406 while ((String = VA_ARG (Args, CHAR16 *)) != NULL && NumberOfLines > 0) {\r
407 Length = StrLen (String);\r
408 SetMem16 (Line, (MaxLength + 2) * 2, L' ');\r
409 if (Length <= MaxLength) {\r
410 //\r
411 // Length <= MaxLength\r
412 //\r
413 CopyMem (Line + 1 + (MaxLength - Length) / 2, String , Length * sizeof (CHAR16));\r
414 } else {\r
415 //\r
416 // Length > MaxLength\r
417 //\r
418 CopyMem (Line + 1, String + (Length - MaxLength) / 2 , MaxLength * sizeof (CHAR16));\r
419 }\r
420 Line[0] = BOXDRAW_VERTICAL;\r
421 Line[MaxLength + 1] = BOXDRAW_VERTICAL;\r
422 Line[MaxLength + 2] = L'\0';\r
423 ConOut->SetCursorPosition (ConOut, Column, Row++);\r
424 ConOut->OutputString (ConOut, Line);\r
425 NumberOfLines--;\r
426 }\r
427 VA_END (Args);\r
428\r
429 //\r
430 // Draw bottom of popup box\r
431 //\r
432 SetMem16 (Line, (MaxLength + 2) * 2, BOXDRAW_HORIZONTAL);\r
433 Line[0] = BOXDRAW_UP_RIGHT;\r
434 Line[MaxLength + 1] = BOXDRAW_UP_LEFT;\r
435 Line[MaxLength + 2] = L'\0';\r
436 ConOut->SetCursorPosition (ConOut, Column, Row++);\r
437 ConOut->OutputString (ConOut, Line);\r
438\r
439 //\r
440 // Free the allocated line buffer\r
441 //\r
442 FreePool (Line);\r
443\r
444 //\r
445 // Restore the cursor visibility, position, and attributes\r
446 //\r
447 ConOut->EnableCursor (ConOut, SavedConsoleMode.CursorVisible);\r
448 ConOut->SetCursorPosition (ConOut, SavedConsoleMode.CursorColumn, SavedConsoleMode.CursorRow);\r
449 ConOut->SetAttribute (ConOut, SavedConsoleMode.Attribute);\r
450\r
451 //\r
452 // Wait for a keystroke\r
453 //\r
454 if (Key != NULL) {\r
abe4c459
RN
455 while (TRUE) {\r
456 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, Key);\r
457 if (!EFI_ERROR (Status)) {\r
458 break;\r
459 }\r
460\r
461 //\r
462 // If we encounter error, continue to read another key in.\r
463 //\r
464 if (Status != EFI_NOT_READY) {\r
465 continue;\r
466 }\r
467 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
468 }\r
4ea439fb 469 }\r
470}\r