]> code.delx.au - refind/blob - refind/menu.c
NTFS driver support, minor filesystem code tweaks, and changes to
[refind] / refind / menu.c
1 /*
2 * refit/menu.c
3 * Menu functions
4 *
5 * Copyright (c) 2006 Christoph Pfisterer
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * Neither the name of Christoph Pfisterer nor the names of the
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 /*
37 * Modifications copyright (c) 2012 Roderick W. Smith
38 *
39 * Modifications distributed under the terms of the GNU General Public
40 * License (GPL) version 3 (GPLv3), a copy of which must be distributed
41 * with this source code or binaries made from it.
42 *
43 */
44
45 #include "global.h"
46 #include "screen.h"
47 #include "lib.h"
48 #include "menu.h"
49 #include "config.h"
50 #include "libeg.h"
51 #include "libegint.h"
52 #include "../include/refit_call_wrapper.h"
53
54 #include "../include/egemb_back_selected_small.h"
55 #include "../include/egemb_back_selected_big.h"
56 #include "../include/egemb_arrow_left.h"
57 #include "../include/egemb_arrow_right.h"
58
59 // other menu definitions
60
61 #define MENU_FUNCTION_INIT (0)
62 #define MENU_FUNCTION_CLEANUP (1)
63 #define MENU_FUNCTION_PAINT_ALL (2)
64 #define MENU_FUNCTION_PAINT_SELECTION (3)
65 #define MENU_FUNCTION_PAINT_TIMEOUT (4)
66 #define MENU_FUNCTION_PAINT_HINTS (5)
67
68 typedef VOID (*MENU_STYLE_FUNC)(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText);
69
70 static CHAR16 ArrowUp[2] = { ARROW_UP, 0 };
71 static CHAR16 ArrowDown[2] = { ARROW_DOWN, 0 };
72 static UINTN TileSizes[2] = { 144, 64 };
73
74 // Text and icon spacing constants....
75 #define TEXT_YMARGIN (2)
76 #define TITLEICON_SPACING (16)
77
78 #define TILE_XSPACING (8)
79 #define TILE_YSPACING (16)
80
81 // Alignment values for PaintIcon()
82 #define ALIGN_RIGHT 1
83 #define ALIGN_LEFT 0
84
85 static EG_IMAGE *SelectionImages[2] = { NULL, NULL };
86 static EG_PIXEL SelectionBackgroundPixel = { 0xff, 0xff, 0xff, 0 };
87
88 //
89 // Graphics helper functions
90 //
91
92 static VOID InitSelection(VOID)
93 {
94 EG_IMAGE *TempSmallImage = NULL, *TempBigImage = NULL;
95 BOOLEAN LoadedSmallImage = FALSE;
96
97 if (!AllowGraphicsMode)
98 return;
99 if (SelectionImages[0] != NULL)
100 return;
101
102 // load small selection image
103 if (GlobalConfig.SelectionSmallFileName != NULL) {
104 TempSmallImage = egLoadImage(SelfDir, GlobalConfig.SelectionSmallFileName, TRUE);
105 }
106 if (TempSmallImage == NULL)
107 TempSmallImage = egPrepareEmbeddedImage(&egemb_back_selected_small, TRUE);
108 else
109 LoadedSmallImage = TRUE;
110 SelectionImages[1] = egScaleImage(TempSmallImage, TileSizes[1], TileSizes[1]);
111
112 // load big selection image
113 if (GlobalConfig.SelectionBigFileName != NULL) {
114 TempBigImage = egLoadImage(SelfDir, GlobalConfig.SelectionBigFileName, TRUE);
115 }
116 if (TempBigImage == NULL) {
117 if (LoadedSmallImage) {
118 // calculate big selection image from small one
119 TempBigImage = egCopyImage(TempSmallImage);
120 } else {
121 TempBigImage = egPrepareEmbeddedImage(&egemb_back_selected_big, TRUE);
122 }
123 }
124 SelectionImages[0] = egScaleImage(TempBigImage, TileSizes[0], TileSizes[0]);
125
126 if (TempSmallImage)
127 egFreeImage(TempSmallImage);
128 if (TempBigImage)
129 egFreeImage(TempBigImage);
130 } // VOID InitSelection()
131
132 //
133 // Scrolling functions
134 //
135
136 static VOID InitScroll(OUT SCROLL_STATE *State, IN UINTN ItemCount, IN UINTN VisibleSpace)
137 {
138 State->PreviousSelection = State->CurrentSelection = 0;
139 State->MaxIndex = (INTN)ItemCount - 1;
140 State->FirstVisible = 0;
141 if (AllowGraphicsMode) {
142 State->MaxVisible = UGAWidth / (TileSizes[0] + TILE_XSPACING) - 1;
143 } else
144 State->MaxVisible = ConHeight - 4;
145 if ((VisibleSpace > 0) && (VisibleSpace < State->MaxVisible))
146 State->MaxVisible = (INTN)VisibleSpace;
147 State->PaintAll = TRUE;
148 State->PaintSelection = FALSE;
149
150 State->LastVisible = State->FirstVisible + State->MaxVisible - 1;
151 }
152
153 // Adjust variables relating to the scrolling of tags, for when a selected icon isn't
154 // visible given the current scrolling condition....
155 static VOID AdjustScrollState(IN SCROLL_STATE *State) {
156 if (State->CurrentSelection > State->LastVisible) {
157 State->LastVisible = State->CurrentSelection;
158 State->FirstVisible = 1 + State->CurrentSelection - State->MaxVisible;
159 if (State->FirstVisible < 0) // shouldn't happen, but just in case....
160 State->FirstVisible = 0;
161 State->PaintAll = TRUE;
162 } // Scroll forward
163 if (State->CurrentSelection < State->FirstVisible) {
164 State->FirstVisible = State->CurrentSelection;
165 State->LastVisible = State->CurrentSelection + State->MaxVisible - 1;
166 State->PaintAll = TRUE;
167 } // Scroll backward
168 } // static VOID AdjustScrollState
169
170 static VOID UpdateScroll(IN OUT SCROLL_STATE *State, IN UINTN Movement)
171 {
172 State->PreviousSelection = State->CurrentSelection;
173
174 switch (Movement) {
175 case SCROLL_LINE_LEFT:
176 if (State->CurrentSelection > 0) {
177 State->CurrentSelection --;
178 }
179 break;
180
181 case SCROLL_LINE_RIGHT:
182 if (State->CurrentSelection < State->MaxIndex) {
183 State->CurrentSelection ++;
184 }
185 break;
186
187 case SCROLL_LINE_UP:
188 if (State->ScrollMode == SCROLL_MODE_ICONS) {
189 if (State->CurrentSelection >= State->InitialRow1) {
190 if (State->MaxIndex > State->InitialRow1) { // avoid division by 0!
191 State->CurrentSelection = State->FirstVisible + (State->LastVisible - State->FirstVisible) *
192 (State->CurrentSelection - State->InitialRow1) /
193 (State->MaxIndex - State->InitialRow1);
194 } else {
195 State->CurrentSelection = State->FirstVisible;
196 } // if/else
197 } // if in second row
198 } else {
199 if (State->CurrentSelection > 0)
200 State->CurrentSelection--;
201 } // if/else
202 break;
203
204 case SCROLL_LINE_DOWN:
205 if (State->ScrollMode == SCROLL_MODE_ICONS) {
206 if (State->CurrentSelection <= State->FinalRow0) {
207 if (State->LastVisible > State->FirstVisible) { // avoid division by 0!
208 State->CurrentSelection = State->InitialRow1 + (State->MaxIndex - State->InitialRow1) *
209 (State->CurrentSelection - State->FirstVisible) /
210 (State->LastVisible - State->FirstVisible);
211 } else {
212 State->CurrentSelection = State->InitialRow1;
213 } // if/else
214 } // if in first row
215 } else {
216 if (State->CurrentSelection < State->MaxIndex)
217 State->CurrentSelection++;
218 } // if/else
219 break;
220
221 case SCROLL_PAGE_UP:
222 if (State->CurrentSelection <= State->FinalRow0)
223 State->CurrentSelection -= State->MaxVisible;
224 else if (State->CurrentSelection == State->InitialRow1)
225 State->CurrentSelection = State->FinalRow0;
226 else
227 State->CurrentSelection = State->InitialRow1;
228 if (State->CurrentSelection < 0)
229 State->CurrentSelection = 0;
230 break;
231
232 case SCROLL_FIRST:
233 if (State->CurrentSelection > 0) {
234 State->PaintAll = TRUE;
235 State->CurrentSelection = 0;
236 }
237 break;
238
239 case SCROLL_PAGE_DOWN:
240 if (State->CurrentSelection < State->FinalRow0) {
241 State->CurrentSelection += State->MaxVisible;
242 if (State->CurrentSelection > State->FinalRow0)
243 State->CurrentSelection = State->FinalRow0;
244 } else if (State->CurrentSelection == State->FinalRow0) {
245 State->CurrentSelection++;
246 } else {
247 State->CurrentSelection = State->MaxIndex;
248 }
249 if (State->CurrentSelection > State->MaxIndex)
250 State->CurrentSelection = State->MaxIndex;
251 break;
252
253 case SCROLL_LAST:
254 if (State->CurrentSelection < State->MaxIndex) {
255 State->PaintAll = TRUE;
256 State->CurrentSelection = State->MaxIndex;
257 }
258 break;
259
260 case SCROLL_NONE:
261 break;
262
263 }
264 if (State->ScrollMode == SCROLL_MODE_TEXT)
265 AdjustScrollState(State);
266
267 if (!State->PaintAll && State->CurrentSelection != State->PreviousSelection)
268 State->PaintSelection = TRUE;
269 State->LastVisible = State->FirstVisible + State->MaxVisible - 1;
270 } // static VOID UpdateScroll()
271
272 //
273 // menu helper functions
274 //
275
276 VOID AddMenuInfoLine(IN REFIT_MENU_SCREEN *Screen, IN CHAR16 *InfoLine)
277 {
278 AddListElement((VOID ***) &(Screen->InfoLines), &(Screen->InfoLineCount), InfoLine);
279 }
280
281 VOID AddMenuEntry(IN REFIT_MENU_SCREEN *Screen, IN REFIT_MENU_ENTRY *Entry)
282 {
283 AddListElement((VOID ***) &(Screen->Entries), &(Screen->EntryCount), Entry);
284 }
285
286
287 static INTN FindMenuShortcutEntry(IN REFIT_MENU_SCREEN *Screen, IN CHAR16 *Defaults)
288 {
289 UINTN i, j = 0;
290 CHAR16 *Shortcut;
291
292 while ((Shortcut = FindCommaDelimited(Defaults, j)) != NULL) {
293 if (StrLen(Shortcut) == 1) {
294 if (Shortcut[0] >= 'a' && Shortcut[0] <= 'z')
295 Shortcut[0] -= ('a' - 'A');
296 if (Shortcut[0]) {
297 for (i = 0; i < Screen->EntryCount; i++) {
298 if (Screen->Entries[i]->ShortcutDigit == Shortcut[0] || Screen->Entries[i]->ShortcutLetter == Shortcut[0]) {
299 MyFreePool(Shortcut);
300 return i;
301 } // if
302 } // for
303 } // if
304 } else if (StrLen(Shortcut) > 1) {
305 for (i = 0; i < Screen->EntryCount; i++) {
306 if (StriSubCmp(Shortcut, Screen->Entries[i]->Title)) {
307 MyFreePool(Shortcut);
308 return i;
309 } // if
310 } // for
311 }
312 MyFreePool(Shortcut);
313 j++;
314 } // while()
315 return -1;
316 }
317
318 // Identify the end of row 0 and the beginning of row 1; store the results in the
319 // appropriate fields in State. Also reduce MaxVisible if that value is greater
320 // than the total number of row-0 tags and if we're in an icon-based screen
321 static VOID IdentifyRows(IN SCROLL_STATE *State, IN REFIT_MENU_SCREEN *Screen) {
322 UINTN i;
323
324 State->FinalRow0 = 0;
325 State->InitialRow1 = State->MaxIndex;
326 for (i = 0; i <= State->MaxIndex; i++) {
327 if (Screen->Entries[i]->Row == 0) {
328 State->FinalRow0 = i;
329 } else if ((Screen->Entries[i]->Row == 1) && (State->InitialRow1 > i)) {
330 State->InitialRow1 = i;
331 } // if/else
332 } // for
333 if ((State->ScrollMode == SCROLL_MODE_ICONS) && (State->MaxVisible > (State->FinalRow0 + 1)))
334 State->MaxVisible = State->FinalRow0 + 1;
335 } // static VOID IdentifyRows()
336
337 // Blank the screen, wait for a keypress, and restore banner/background.
338 // Screen may still require redrawing of text and icons on return.
339 // TODO: Support more sophisticated screen savers, such as power-saving
340 // mode and dynamic images.
341 static VOID SaveScreen(VOID) {
342 UINTN index;
343 EG_PIXEL Black = { 0x0, 0x0, 0x0, 0 };
344
345 egClearScreen(&Black);
346 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
347 if (AllowGraphicsMode)
348 SwitchToGraphicsAndClear();
349 ReadAllKeyStrokes();
350 } // VOID SaveScreen()
351
352 //
353 // generic menu function
354 //
355 static UINTN RunGenericMenu(IN REFIT_MENU_SCREEN *Screen, IN MENU_STYLE_FUNC StyleFunc, IN OUT INTN *DefaultEntryIndex,
356 OUT REFIT_MENU_ENTRY **ChosenEntry)
357 {
358 SCROLL_STATE State;
359 EFI_STATUS Status;
360 EFI_INPUT_KEY key;
361 UINTN index;
362 INTN ShortcutEntry;
363 BOOLEAN HaveTimeout = FALSE;
364 BOOLEAN WaitForRelease = FALSE;
365 UINTN TimeoutCountdown = 0;
366 INTN PreviousTime = -1, CurrentTime, TimeSinceKeystroke = 0;
367 CHAR16 TimeoutMessage[256];
368 CHAR16 KeyAsString[2];
369 UINTN MenuExit;
370
371 if (Screen->TimeoutSeconds > 0) {
372 HaveTimeout = TRUE;
373 TimeoutCountdown = Screen->TimeoutSeconds * 10;
374 }
375 MenuExit = 0;
376
377 StyleFunc(Screen, &State, MENU_FUNCTION_INIT, NULL);
378 IdentifyRows(&State, Screen);
379 // override the starting selection with the default index, if any
380 if (*DefaultEntryIndex >= 0 && *DefaultEntryIndex <= State.MaxIndex) {
381 State.CurrentSelection = *DefaultEntryIndex;
382 if (GlobalConfig.ScreensaverTime != -1)
383 UpdateScroll(&State, SCROLL_NONE);
384 }
385
386 if (Screen->TimeoutSeconds == -1) {
387 Status = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
388 if (Status == EFI_NOT_READY) {
389 MenuExit = MENU_EXIT_TIMEOUT;
390 } else {
391 KeyAsString[0] = key.UnicodeChar;
392 KeyAsString[1] = 0;
393 ShortcutEntry = FindMenuShortcutEntry(Screen, KeyAsString);
394 if (ShortcutEntry >= 0) {
395 State.CurrentSelection = ShortcutEntry;
396 MenuExit = MENU_EXIT_ENTER;
397 } else {
398 WaitForRelease = TRUE;
399 HaveTimeout = FALSE;
400 }
401 }
402 }
403
404 if (GlobalConfig.ScreensaverTime != -1)
405 State.PaintAll = TRUE;
406
407 while (!MenuExit) {
408 // update the screen
409 if (State.PaintAll && (GlobalConfig.ScreensaverTime != -1)) {
410 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_ALL, NULL);
411 State.PaintAll = FALSE;
412 } else if (State.PaintSelection) {
413 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_SELECTION, NULL);
414 State.PaintSelection = FALSE;
415 }
416
417 if (WaitForRelease) {
418 Status = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
419 if (Status == EFI_SUCCESS) {
420 // reset, because otherwise the buffer gets queued with keystrokes
421 refit_call2_wrapper(ST->ConIn->Reset, ST->ConIn, FALSE);
422 refit_call1_wrapper(BS->Stall, 100000);
423 } else {
424 WaitForRelease = FALSE;
425 refit_call2_wrapper(ST->ConIn->Reset, ST->ConIn, TRUE);
426 }
427 continue;
428 }
429
430 if (HaveTimeout) {
431 CurrentTime = (TimeoutCountdown + 5) / 10;
432 if (CurrentTime != PreviousTime) {
433 SPrint(TimeoutMessage, 255, L"%s in %d seconds", Screen->TimeoutText, CurrentTime);
434 if (GlobalConfig.ScreensaverTime != -1)
435 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, TimeoutMessage);
436 PreviousTime = CurrentTime;
437 }
438 }
439
440 // read key press (and wait for it if applicable)
441 Status = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
442 if (Status != EFI_SUCCESS) {
443 if (HaveTimeout && TimeoutCountdown == 0) {
444 // timeout expired
445 MenuExit = MENU_EXIT_TIMEOUT;
446 break;
447 } else if (HaveTimeout || GlobalConfig.ScreensaverTime > 0) {
448 EFI_EVENT TimerEvent;
449 UINTN ElapsCount = 1;
450
451 Status = refit_call5_wrapper(BS->CreateEvent, EVT_TIMER, 0, NULL, NULL, &TimerEvent);
452 if (EFI_ERROR(Status)) {
453 refit_call1_wrapper(BS->Stall, 100000); // Pause for 100 ms
454 } else {
455 EFI_EVENT WaitList[2];
456 UINTN Index;
457
458 refit_call3_wrapper(BS->SetTimer, TimerEvent, TimerRelative, 10000000); // 1s Timeout
459 WaitList[0] = ST->ConIn->WaitForKey;
460 WaitList[1] = TimerEvent;
461 Status = refit_call3_wrapper(BS->WaitForEvent, 2, WaitList, &Index);
462 refit_call1_wrapper(BS->CloseEvent, TimerEvent);
463 if (EFI_ERROR(Status))
464 refit_call1_wrapper(BS->Stall, 100000); // Pause for 100 ms
465 else if(Index == 0)
466 continue;
467 else
468 ElapsCount = 10; // always counted as 1s to end of the timeout
469 }
470 TimeSinceKeystroke += ElapsCount;
471 if(HaveTimeout) {
472 TimeoutCountdown = TimeoutCountdown <= ElapsCount ? 0 : TimeoutCountdown - ElapsCount;
473 } else if (GlobalConfig.ScreensaverTime > 0 &&
474 TimeSinceKeystroke > (GlobalConfig.ScreensaverTime * 10))
475 {
476 SaveScreen();
477 State.PaintAll = TRUE;
478 TimeSinceKeystroke = 0;
479 } // if
480 } else {
481 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
482 }
483 continue;
484 } else {
485 TimeSinceKeystroke = 0;
486 } // if/else !read keystroke
487
488 if (HaveTimeout) {
489 // the user pressed a key, cancel the timeout
490 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, L"");
491 HaveTimeout = FALSE;
492 if (GlobalConfig.ScreensaverTime == -1) { // cancel start-with-blank-screen coding
493 GlobalConfig.ScreensaverTime = 0;
494 if (!GlobalConfig.TextOnly)
495 BltClearScreen(TRUE);
496 }
497 }
498
499 // react to key press
500 switch (key.ScanCode) {
501 case SCAN_UP:
502 UpdateScroll(&State, SCROLL_LINE_UP);
503 break;
504 case SCAN_LEFT:
505 UpdateScroll(&State, SCROLL_LINE_LEFT);
506 break;
507 case SCAN_DOWN:
508 UpdateScroll(&State, SCROLL_LINE_DOWN);
509 break;
510 case SCAN_RIGHT:
511 UpdateScroll(&State, SCROLL_LINE_RIGHT);
512 break;
513 case SCAN_HOME:
514 UpdateScroll(&State, SCROLL_FIRST);
515 break;
516 case SCAN_END:
517 UpdateScroll(&State, SCROLL_LAST);
518 break;
519 case SCAN_PAGE_UP:
520 UpdateScroll(&State, SCROLL_PAGE_UP);
521 break;
522 case SCAN_PAGE_DOWN:
523 UpdateScroll(&State, SCROLL_PAGE_DOWN);
524 break;
525 case SCAN_ESC:
526 MenuExit = MENU_EXIT_ESCAPE;
527 break;
528 case SCAN_INSERT:
529 case SCAN_F2:
530 MenuExit = MENU_EXIT_DETAILS;
531 break;
532 case SCAN_F10:
533 egScreenShot();
534 break;
535 case 0x0016: // F12
536 if (EjectMedia())
537 MenuExit = MENU_EXIT_ESCAPE;
538 break;
539 }
540 switch (key.UnicodeChar) {
541 case CHAR_LINEFEED:
542 case CHAR_CARRIAGE_RETURN:
543 case ' ':
544 MenuExit = MENU_EXIT_ENTER;
545 break;
546 case '+':
547 MenuExit = MENU_EXIT_DETAILS;
548 break;
549 default:
550 KeyAsString[0] = key.UnicodeChar;
551 KeyAsString[1] = 0;
552 ShortcutEntry = FindMenuShortcutEntry(Screen, KeyAsString);
553 if (ShortcutEntry >= 0) {
554 State.CurrentSelection = ShortcutEntry;
555 MenuExit = MENU_EXIT_ENTER;
556 }
557 break;
558 }
559 }
560
561 StyleFunc(Screen, &State, MENU_FUNCTION_CLEANUP, NULL);
562
563 if (ChosenEntry)
564 *ChosenEntry = Screen->Entries[State.CurrentSelection];
565 *DefaultEntryIndex = State.CurrentSelection;
566 return MenuExit;
567 } /* static UINTN RunGenericMenu() */
568
569 //
570 // text-mode generic style
571 //
572
573 // Show information lines in text mode.
574 static VOID ShowTextInfoLines(IN REFIT_MENU_SCREEN *Screen) {
575 INTN i;
576
577 BeginTextScreen(Screen->Title);
578 if (Screen->InfoLineCount > 0) {
579 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
580 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
581 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, 4 + i);
582 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->InfoLines[i]);
583 }
584 }
585 } // VOID ShowTextInfoLines()
586
587 // Do most of the work for text-based menus....
588 static VOID TextMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
589 {
590 INTN i;
591 UINTN MenuWidth, ItemWidth, MenuHeight;
592 static UINTN MenuPosY;
593 static CHAR16 **DisplayStrings;
594 CHAR16 TimeoutMessage[256];
595
596 State->ScrollMode = SCROLL_MODE_TEXT;
597 switch (Function) {
598
599 case MENU_FUNCTION_INIT:
600 // vertical layout
601 MenuPosY = 4;
602 if (Screen->InfoLineCount > 0)
603 MenuPosY += Screen->InfoLineCount + 1;
604 MenuHeight = ConHeight - MenuPosY - 3;
605 if (Screen->TimeoutSeconds > 0)
606 MenuHeight -= 2;
607 InitScroll(State, Screen->EntryCount, MenuHeight);
608
609 // determine width of the menu
610 MenuWidth = 20; // minimum
611 for (i = 0; i <= State->MaxIndex; i++) {
612 ItemWidth = StrLen(Screen->Entries[i]->Title);
613 if (MenuWidth < ItemWidth)
614 MenuWidth = ItemWidth;
615 }
616 MenuWidth += 2;
617 if (MenuWidth > ConWidth - 3)
618 MenuWidth = ConWidth - 3;
619
620 // prepare strings for display
621 DisplayStrings = AllocatePool(sizeof(CHAR16 *) * Screen->EntryCount);
622 for (i = 0; i <= State->MaxIndex; i++) {
623 // Note: Theoretically, SPrint() is a cleaner way to do this; but the
624 // description of the StrSize parameter to SPrint implies it's measured
625 // in characters, but in practice both TianoCore and GNU-EFI seem to
626 // use bytes instead, resulting in truncated displays. I could just
627 // double the size of the StrSize parameter, but that seems unsafe in
628 // case a future library change starts treating this as characters, so
629 // I'm doing it the hard way in this instance.
630 // TODO: Review the above and possibly change other uses of SPrint()
631 DisplayStrings[i] = AllocateZeroPool(2 * sizeof(CHAR16));
632 DisplayStrings[i][0] = L' ';
633 MergeStrings(&DisplayStrings[i], Screen->Entries[i]->Title, 0);
634 if (StrLen(DisplayStrings[i]) > MenuWidth)
635 DisplayStrings[i][MenuWidth - 1] = 0;
636 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
637 // TODO: account for double-width characters
638 } // for
639
640 break;
641
642 case MENU_FUNCTION_CLEANUP:
643 // release temporary memory
644 for (i = 0; i <= State->MaxIndex; i++)
645 MyFreePool(DisplayStrings[i]);
646 MyFreePool(DisplayStrings);
647 break;
648
649 case MENU_FUNCTION_PAINT_ALL:
650 // paint the whole screen (initially and after scrolling)
651
652 ShowTextInfoLines(Screen);
653 for (i = 0; i <= State->MaxIndex; i++) {
654 if (i >= State->FirstVisible && i <= State->LastVisible) {
655 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (i - State->FirstVisible));
656 if (i == State->CurrentSelection)
657 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
658 else
659 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
660 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[i]);
661 }
662 }
663 // scrolling indicators
664 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_SCROLLARROW);
665 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY);
666 if (State->FirstVisible > 0)
667 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowUp);
668 else
669 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
670 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY + State->MaxVisible);
671 if (State->LastVisible < State->MaxIndex)
672 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowDown);
673 else
674 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
675 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
676 if (Screen->Hint1 != NULL) {
677 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 2);
678 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->Hint1);
679 }
680 if (Screen->Hint2 != NULL) {
681 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 1);
682 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->Hint2);
683 }
684 }
685 break;
686
687 case MENU_FUNCTION_PAINT_SELECTION:
688 // redraw selection cursor
689 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2,
690 MenuPosY + (State->PreviousSelection - State->FirstVisible));
691 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
692 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->PreviousSelection]);
693 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2,
694 MenuPosY + (State->CurrentSelection - State->FirstVisible));
695 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
696 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->CurrentSelection]);
697 break;
698
699 case MENU_FUNCTION_PAINT_TIMEOUT:
700 if (ParamText[0] == 0) {
701 // clear message
702 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
703 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 3);
704 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, BlankLine + 1);
705 } else {
706 // paint or update message
707 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_ERROR);
708 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, ConHeight - 3);
709 SPrint(TimeoutMessage, 255, L"%s ", ParamText);
710 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, TimeoutMessage);
711 }
712 break;
713
714 }
715 }
716
717 //
718 // graphical generic style
719 //
720
721 inline static UINTN TextLineHeight(VOID) {
722 return egGetFontHeight() + TEXT_YMARGIN * 2;
723 } // UINTN TextLineHeight()
724
725 //
726 // Display a submenu
727 //
728
729 // Display text with a solid background (MenuBackgroundPixel or SelectionBackgroundPixel).
730 // Indents text by one character and placed TEXT_YMARGIN pixels down from the
731 // specified XPos and YPos locations.
732 static VOID DrawText(IN CHAR16 *Text, IN BOOLEAN Selected, IN UINTN FieldWidth, IN UINTN XPos, IN UINTN YPos)
733 {
734 EG_IMAGE *TextBuffer;
735 EG_PIXEL Bg;
736
737 TextBuffer = egCreateImage(FieldWidth, TextLineHeight(), FALSE);
738
739 egFillImage(TextBuffer, &MenuBackgroundPixel);
740 Bg = MenuBackgroundPixel;
741 if (Selected) {
742 // draw selection bar background
743 egFillImageArea(TextBuffer, 0, 0, FieldWidth, TextBuffer->Height, &SelectionBackgroundPixel);
744 Bg = SelectionBackgroundPixel;
745 }
746
747 // render the text
748 egRenderText(Text, TextBuffer, egGetFontCellWidth(), TEXT_YMARGIN, (Bg.r + Bg.g + Bg.b) / 3);
749 egDrawImageWithTransparency(TextBuffer, NULL, XPos, YPos, TextBuffer->Width, TextBuffer->Height);
750 // BltImage(TextBuffer, XPos, YPos);
751 }
752
753 // Finds the average brightness of the input Image.
754 // NOTE: Passing an Image that covers the whole screen can strain the
755 // capacity of a UINTN on a 32-bit system with a very large display.
756 // Using UINT64 instead is unworkable, since the code won't compile
757 // on a 32-bit system. As the intended use for this function is to handle
758 // a single text string's background, this shouldn't be a problem, but it
759 // may need addressing if it's applied more broadly....
760 static UINT8 AverageBrightness(EG_IMAGE *Image) {
761 UINTN i;
762 UINTN Sum = 0;
763
764 if (Image != NULL) {
765 for (i = 0; i < (Image->Width * Image->Height); i++) {
766 Sum += (Image->PixelData[i].r + Image->PixelData[i].g + Image->PixelData[i].b);
767 }
768 } // if
769 return (UINT8) (Sum / (Image->Width * Image->Height * 3));
770 } // UINT8 AverageBrightness()
771
772 // Display text against the screen's background image. Special case: If Text is NULL
773 // or 0-length, clear the line. Does NOT indent the text or reposition it relative
774 // to the specified XPos and YPos values.
775 static VOID DrawTextWithTransparency(IN CHAR16 *Text, IN UINTN XPos, IN UINTN YPos)
776 {
777 UINTN TextWidth;
778 EG_IMAGE *TextBuffer = NULL;
779
780 if (Text == NULL)
781 Text = L"";
782
783 egMeasureText(Text, &TextWidth, NULL);
784 if (TextWidth == 0) {
785 TextWidth = UGAWidth;
786 XPos = 0;
787 }
788
789 TextBuffer = egCropImage(GlobalConfig.ScreenBackground, XPos, YPos, TextWidth, TextLineHeight());
790 if (TextBuffer == NULL)
791 return;
792
793 // render the text
794 egRenderText(Text, TextBuffer, 0, 0, AverageBrightness(TextBuffer));
795 egDrawImageWithTransparency(TextBuffer, NULL, XPos, YPos, TextBuffer->Width, TextBuffer->Height);
796 egFreeImage(TextBuffer);
797 }
798
799 // Compute the size & position of the window that will hold a subscreen's information.
800 static VOID ComputeSubScreenWindowSize(REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *XPos, UINTN *YPos,
801 UINTN *Width, UINTN *Height, UINTN *LineWidth) {
802 UINTN i, ItemWidth, HintTop, BannerBottomEdge, TitleWidth;
803 UINTN FontCellWidth = egGetFontCellWidth();
804 UINTN FontCellHeight = egGetFontHeight();
805
806 *Width = 20;
807 *Height = 5;
808 TitleWidth = egComputeTextWidth(Screen->Title);
809
810 for (i = 0; i < Screen->InfoLineCount; i++) {
811 ItemWidth = StrLen(Screen->InfoLines[i]);
812 if (*Width < ItemWidth) {
813 *Width = ItemWidth;
814 }
815 (*Height)++;
816 }
817 for (i = 0; i <= State->MaxIndex; i++) {
818 ItemWidth = StrLen(Screen->Entries[i]->Title);
819 if (*Width < ItemWidth) {
820 *Width = ItemWidth;
821 }
822 (*Height)++;
823 }
824 *Width = (*Width + 2) * FontCellWidth;
825 *LineWidth = *Width;
826 if (Screen->TitleImage)
827 *Width += (Screen->TitleImage->Width + TITLEICON_SPACING * 2 + FontCellWidth);
828 else
829 *Width += FontCellWidth;
830
831 if (*Width < TitleWidth)
832 *Width = TitleWidth + 2 * FontCellWidth;
833
834 // Keep it within the bounds of the screen, or 2/3 of the screen's width
835 // for screens over 800 pixels wide
836 if (*Width > UGAWidth)
837 *Width = UGAWidth;
838
839 *XPos = (UGAWidth - *Width) / 2;
840
841 HintTop = UGAHeight - (FontCellHeight * 3); // top of hint text
842 *Height *= TextLineHeight();
843 if (Screen->TitleImage && (*Height < (Screen->TitleImage->Height + TextLineHeight() * 4)))
844 *Height = Screen->TitleImage->Height + TextLineHeight() * 4;
845
846 if (GlobalConfig.BannerBottomEdge >= HintTop) { // probably a full-screen image; treat it as an empty banner
847 BannerBottomEdge = 0;
848 } else {
849 BannerBottomEdge = GlobalConfig.BannerBottomEdge;
850 }
851 if (*Height > (HintTop - BannerBottomEdge - FontCellHeight * 2)) {
852 BannerBottomEdge = 0;
853 }
854 if (*Height > (HintTop - BannerBottomEdge - FontCellHeight * 2)) {
855 // TODO: Implement scrolling in text screen.
856 *Height = (HintTop - BannerBottomEdge - FontCellHeight * 2);
857 }
858
859 *YPos = ((UGAHeight - *Height) / 2);
860 if (*YPos < BannerBottomEdge)
861 *YPos = BannerBottomEdge + FontCellHeight + (HintTop - BannerBottomEdge - *Height) / 2;
862 } // VOID ComputeSubScreenWindowSize()
863
864 // Displays sub-menus
865 static VOID GraphicsMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
866 {
867 INTN i;
868 UINTN ItemWidth;
869 static UINTN LineWidth, MenuWidth, MenuHeight, EntriesPosX, TitlePosX, EntriesPosY, TimeoutPosY, CharWidth;
870 EG_IMAGE *Window;
871 EG_PIXEL *BackgroundPixel = &(GlobalConfig.ScreenBackground->PixelData[0]);
872
873 CharWidth = egGetFontCellWidth();
874 State->ScrollMode = SCROLL_MODE_TEXT;
875 switch (Function) {
876
877 case MENU_FUNCTION_INIT:
878 InitScroll(State, Screen->EntryCount, 0);
879 ComputeSubScreenWindowSize(Screen, State, &EntriesPosX, &EntriesPosY, &MenuWidth, &MenuHeight, &LineWidth);
880 TimeoutPosY = EntriesPosY + (Screen->EntryCount + 1) * TextLineHeight();
881
882 // initial painting
883 SwitchToGraphicsAndClear();
884 Window = egCreateFilledImage(MenuWidth, MenuHeight, FALSE, BackgroundPixel);
885 egDrawImage(Window, EntriesPosX, EntriesPosY);
886 ItemWidth = egComputeTextWidth(Screen->Title);
887 if (MenuWidth > ItemWidth) {
888 TitlePosX = EntriesPosX + (MenuWidth - ItemWidth) / 2 - CharWidth;
889 } else {
890 TitlePosX = EntriesPosX;
891 if (CharWidth > 0) {
892 i = MenuWidth / CharWidth - 2;
893 if (i > 0)
894 Screen->Title[i] = 0;
895 } // if
896 } // if/else
897 break;
898
899 case MENU_FUNCTION_CLEANUP:
900 // nothing to do
901 break;
902
903 case MENU_FUNCTION_PAINT_ALL:
904 ComputeSubScreenWindowSize(Screen, State, &EntriesPosX, &EntriesPosY, &MenuWidth, &MenuHeight, &LineWidth);
905 DrawText(Screen->Title, FALSE, (StrLen(Screen->Title) + 2) * CharWidth, TitlePosX, EntriesPosY += TextLineHeight());
906 if (Screen->TitleImage) {
907 BltImageAlpha(Screen->TitleImage, EntriesPosX + TITLEICON_SPACING, EntriesPosY + TextLineHeight() * 2,
908 BackgroundPixel);
909 EntriesPosX += (Screen->TitleImage->Width + TITLEICON_SPACING * 2);
910 }
911 EntriesPosY += (TextLineHeight() * 2);
912 if (Screen->InfoLineCount > 0) {
913 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
914 DrawText(Screen->InfoLines[i], FALSE, LineWidth, EntriesPosX, EntriesPosY);
915 EntriesPosY += TextLineHeight();
916 }
917 EntriesPosY += TextLineHeight(); // also add a blank line
918 }
919
920 for (i = 0; i <= State->MaxIndex; i++) {
921 DrawText(Screen->Entries[i]->Title, (i == State->CurrentSelection), LineWidth, EntriesPosX,
922 EntriesPosY + i * TextLineHeight());
923 }
924 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
925 if ((Screen->Hint1 != NULL) && (StrLen(Screen->Hint1) > 0))
926 DrawTextWithTransparency(Screen->Hint1, (UGAWidth - egComputeTextWidth(Screen->Hint1)) / 2,
927 UGAHeight - (egGetFontHeight() * 3));
928 if ((Screen->Hint2 != NULL) && (StrLen(Screen->Hint2) > 0))
929 DrawTextWithTransparency(Screen->Hint2, (UGAWidth - egComputeTextWidth(Screen->Hint2)) / 2,
930 UGAHeight - (egGetFontHeight() * 2));
931 } // if
932 break;
933
934 case MENU_FUNCTION_PAINT_SELECTION:
935 // redraw selection cursor
936 DrawText(Screen->Entries[State->PreviousSelection]->Title, FALSE, LineWidth,
937 EntriesPosX, EntriesPosY + State->PreviousSelection * TextLineHeight());
938 DrawText(Screen->Entries[State->CurrentSelection]->Title, TRUE, LineWidth,
939 EntriesPosX, EntriesPosY + State->CurrentSelection * TextLineHeight());
940 break;
941
942 case MENU_FUNCTION_PAINT_TIMEOUT:
943 DrawText(ParamText, FALSE, LineWidth, EntriesPosX, TimeoutPosY);
944 break;
945
946 }
947 } // static VOID GraphicsMenuStyle()
948
949 //
950 // graphical main menu style
951 //
952
953 static VOID DrawMainMenuEntry(REFIT_MENU_ENTRY *Entry, BOOLEAN selected, UINTN XPos, UINTN YPos)
954 {
955 EG_IMAGE *Background;
956
957 if (SelectionImages != NULL) {
958 if (selected) {
959 Background = egCropImage(GlobalConfig.ScreenBackground, XPos, YPos,
960 SelectionImages[Entry->Row]->Width, SelectionImages[Entry->Row]->Height);
961 egComposeImage(Background, SelectionImages[Entry->Row], 0, 0);
962 BltImageCompositeBadge(Background, Entry->Image, Entry->BadgeImage, XPos, YPos);
963 } else { // Image not selected; copy background
964 egDrawImageWithTransparency(Entry->Image, Entry->BadgeImage, XPos, YPos,
965 SelectionImages[Entry->Row]->Width, SelectionImages[Entry->Row]->Height);
966 } // if/else
967 } // if
968 } // VOID DrawMainMenuEntry()
969
970 static VOID PaintAll(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
971 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
972 INTN i;
973
974 if (Screen->Entries[State->CurrentSelection]->Row == 0)
975 AdjustScrollState(State);
976 for (i = State->FirstVisible; i <= State->MaxIndex; i++) {
977 if (Screen->Entries[i]->Row == 0) {
978 if (i <= State->LastVisible) {
979 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE,
980 itemPosX[i - State->FirstVisible], row0PosY);
981 } // if
982 } else {
983 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE, itemPosX[i], row1PosY);
984 }
985 }
986 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL)) {
987 DrawTextWithTransparency(L"", 0, textPosY);
988 DrawTextWithTransparency(Screen->Entries[State->CurrentSelection]->Title,
989 (UGAWidth - egComputeTextWidth(Screen->Entries[State->CurrentSelection]->Title)) >> 1,
990 textPosY);
991 }
992
993 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
994 DrawTextWithTransparency(Screen->Hint1, (UGAWidth - egComputeTextWidth(Screen->Hint1)) / 2,
995 UGAHeight - (egGetFontHeight() * 3));
996 DrawTextWithTransparency(Screen->Hint2, (UGAWidth - egComputeTextWidth(Screen->Hint2)) / 2,
997 UGAHeight - (egGetFontHeight() * 2));
998 } // if
999 } // static VOID PaintAll()
1000
1001 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
1002 static VOID PaintSelection(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
1003 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
1004 UINTN XSelectPrev, XSelectCur, YPosPrev, YPosCur;
1005
1006 if (((State->CurrentSelection <= State->LastVisible) && (State->CurrentSelection >= State->FirstVisible)) ||
1007 (State->CurrentSelection >= State->InitialRow1) ) {
1008 if (Screen->Entries[State->PreviousSelection]->Row == 0) {
1009 XSelectPrev = State->PreviousSelection - State->FirstVisible;
1010 YPosPrev = row0PosY;
1011 } else {
1012 XSelectPrev = State->PreviousSelection;
1013 YPosPrev = row1PosY;
1014 } // if/else
1015 if (Screen->Entries[State->CurrentSelection]->Row == 0) {
1016 XSelectCur = State->CurrentSelection - State->FirstVisible;
1017 YPosCur = row0PosY;
1018 } else {
1019 XSelectCur = State->CurrentSelection;
1020 YPosCur = row1PosY;
1021 } // if/else
1022 DrawMainMenuEntry(Screen->Entries[State->PreviousSelection], FALSE, itemPosX[XSelectPrev], YPosPrev);
1023 DrawMainMenuEntry(Screen->Entries[State->CurrentSelection], TRUE, itemPosX[XSelectCur], YPosCur);
1024 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL)) {
1025 DrawTextWithTransparency(L"", 0, textPosY);
1026 DrawTextWithTransparency(Screen->Entries[State->CurrentSelection]->Title,
1027 (UGAWidth - egComputeTextWidth(Screen->Entries[State->CurrentSelection]->Title)) >> 1,
1028 textPosY);
1029 }
1030 } else { // Current selection not visible; must redraw the menu....
1031 MainMenuStyle(Screen, State, MENU_FUNCTION_PAINT_ALL, NULL);
1032 }
1033 } // static VOID MoveSelection(VOID)
1034
1035 // Display a 48x48 icon at the specified location. Uses the image specified by
1036 // ExternalFilename if it's available, or BuiltInImage if it's not. The
1037 // Y position is specified as the center value, and so is adjusted by half
1038 // the icon's height. The X position is set along the icon's left
1039 // edge if Alignment == ALIGN_LEFT, and along the right edge if
1040 // Alignment == ALIGN_RIGHT
1041 static VOID PaintIcon(IN EG_EMBEDDED_IMAGE *BuiltInIcon, IN CHAR16 *ExternalFilename, UINTN PosX, UINTN PosY, UINTN Alignment) {
1042 EG_IMAGE *Icon = NULL;
1043
1044 Icon = egFindIcon(ExternalFilename, GlobalConfig.IconSizes[ICON_SIZE_SMALL]);
1045 if (Icon == NULL)
1046 Icon = egPrepareEmbeddedImage(BuiltInIcon, TRUE);
1047 if (Icon != NULL) {
1048 if (Alignment == ALIGN_RIGHT)
1049 PosX -= Icon->Width;
1050 egDrawImageWithTransparency(Icon, NULL, PosX, PosY - (Icon->Height / 2), Icon->Width, Icon->Height);
1051 }
1052 } // static VOID ()
1053
1054 inline UINTN ComputeRow0PosY(VOID) {
1055 return ((UGAHeight / 2) - TileSizes[0] / 2);
1056 } // UINTN ComputeRow0PosY()
1057
1058 // Display (or erase) the arrow icons to the left and right of an icon's row,
1059 // as appropriate.
1060 static VOID PaintArrows(SCROLL_STATE *State, UINTN PosX, UINTN PosY, UINTN row0Loaders) {
1061 EG_IMAGE *TempImage;
1062 UINTN Width, Height, RightX, AdjPosY;
1063
1064 // NOTE: Assume that left and right arrows are of the same size....
1065 Width = egemb_arrow_left.Width;
1066 Height = egemb_arrow_left.Height;
1067 RightX = (UGAWidth + (TileSizes[0] + TILE_XSPACING) * State->MaxVisible) / 2 + TILE_XSPACING;
1068 AdjPosY = PosY - (Height / 2);
1069
1070 // For PaintIcon() calls, the starting Y position is moved to the midpoint
1071 // of the surrounding row; PaintIcon() adjusts this back up by half the
1072 // icon's height to properly center it.
1073 if ((State->FirstVisible > 0) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
1074 PaintIcon(&egemb_arrow_left, L"arrow_left", PosX, PosY, ALIGN_RIGHT);
1075 } else {
1076 TempImage = egCropImage(GlobalConfig.ScreenBackground, PosX - Width, AdjPosY, Width, Height);
1077 BltImage(TempImage, PosX - Width, AdjPosY);
1078 egFreeImage(TempImage);
1079 } // if/else
1080
1081 if ((State->LastVisible < (row0Loaders - 1)) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
1082 PaintIcon(&egemb_arrow_right, L"arrow_right", RightX, PosY, ALIGN_LEFT);
1083 } else {
1084 TempImage = egCropImage(GlobalConfig.ScreenBackground, RightX, AdjPosY, Width, Height);
1085 BltImage(TempImage, RightX, AdjPosY);
1086 egFreeImage(TempImage);
1087 } // if/else
1088 } // VOID PaintArrows()
1089
1090 // Display main menu in graphics mode
1091 VOID MainMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
1092 {
1093 INTN i;
1094 static UINTN row0PosX, row0PosXRunning, row1PosY, row0Loaders;
1095 UINTN row0Count, row1Count, row1PosX, row1PosXRunning;
1096 static UINTN *itemPosX;
1097 static UINTN row0PosY, textPosY;
1098
1099 State->ScrollMode = SCROLL_MODE_ICONS;
1100 switch (Function) {
1101
1102 case MENU_FUNCTION_INIT:
1103 InitScroll(State, Screen->EntryCount, GlobalConfig.MaxTags);
1104
1105 // layout
1106 row0Count = 0;
1107 row1Count = 0;
1108 row0Loaders = 0;
1109 for (i = 0; i <= State->MaxIndex; i++) {
1110 if (Screen->Entries[i]->Row == 1) {
1111 row1Count++;
1112 } else {
1113 row0Loaders++;
1114 if (row0Count < State->MaxVisible)
1115 row0Count++;
1116 }
1117 }
1118 row0PosX = (UGAWidth + TILE_XSPACING - (TileSizes[0] + TILE_XSPACING) * row0Count) >> 1;
1119 row0PosY = ComputeRow0PosY();
1120 row1PosX = (UGAWidth + TILE_XSPACING - (TileSizes[1] + TILE_XSPACING) * row1Count) >> 1;
1121 row1PosY = row0PosY + TileSizes[0] + TILE_YSPACING;
1122 if (row1Count > 0)
1123 textPosY = row1PosY + TileSizes[1] + TILE_YSPACING;
1124 else
1125 textPosY = row1PosY;
1126
1127 itemPosX = AllocatePool(sizeof(UINTN) * Screen->EntryCount);
1128 row0PosXRunning = row0PosX;
1129 row1PosXRunning = row1PosX;
1130 for (i = 0; i <= State->MaxIndex; i++) {
1131 if (Screen->Entries[i]->Row == 0) {
1132 itemPosX[i] = row0PosXRunning;
1133 row0PosXRunning += TileSizes[0] + TILE_XSPACING;
1134 } else {
1135 itemPosX[i] = row1PosXRunning;
1136 row1PosXRunning += TileSizes[1] + TILE_XSPACING;
1137 }
1138 }
1139 // initial painting
1140 InitSelection();
1141 SwitchToGraphicsAndClear();
1142 break;
1143
1144 case MENU_FUNCTION_CLEANUP:
1145 MyFreePool(itemPosX);
1146 break;
1147
1148 case MENU_FUNCTION_PAINT_ALL:
1149 PaintAll(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
1150 // For PaintArrows(), the starting Y position is moved to the midpoint
1151 // of the surrounding row; PaintIcon() adjusts this back up by half the
1152 // icon's height to properly center it.
1153 PaintArrows(State, row0PosX - TILE_XSPACING, row0PosY + (TileSizes[0] / 2), row0Loaders);
1154 break;
1155
1156 case MENU_FUNCTION_PAINT_SELECTION:
1157 PaintSelection(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
1158 break;
1159
1160 case MENU_FUNCTION_PAINT_TIMEOUT:
1161 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL)) {
1162 DrawTextWithTransparency(L"", 0, textPosY + TextLineHeight());
1163 DrawTextWithTransparency(ParamText, (UGAWidth - egComputeTextWidth(ParamText)) >> 1, textPosY + TextLineHeight());
1164 }
1165 break;
1166
1167 }
1168 } // VOID MainMenuStyle()
1169
1170 // Enable the user to edit boot loader options.
1171 // Returns TRUE if the user exited with edited options; FALSE if the user
1172 // pressed Esc to terminate the edit.
1173 static BOOLEAN EditOptions(LOADER_ENTRY *MenuEntry) {
1174 UINTN x_max, y_max;
1175 CHAR16 *EditedOptions;
1176 BOOLEAN retval = FALSE;
1177
1178 if (GlobalConfig.HideUIFlags & HIDEUI_FLAG_EDITOR) {
1179 return FALSE;
1180 }
1181
1182 refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max);
1183
1184 if (!GlobalConfig.TextOnly)
1185 SwitchToText(TRUE);
1186
1187 if (line_edit(MenuEntry->LoadOptions, &EditedOptions, x_max)) {
1188 MyFreePool(MenuEntry->LoadOptions);
1189 MenuEntry->LoadOptions = EditedOptions;
1190 retval = TRUE;
1191 } // if
1192 if (!GlobalConfig.TextOnly)
1193 SwitchToGraphics();
1194 return retval;
1195 } // VOID EditOptions()
1196
1197 //
1198 // user-callable dispatcher functions
1199 //
1200
1201 UINTN RunMenu(IN REFIT_MENU_SCREEN *Screen, OUT REFIT_MENU_ENTRY **ChosenEntry)
1202 {
1203 INTN DefaultEntry = -1;
1204 MENU_STYLE_FUNC Style = TextMenuStyle;
1205
1206 if (AllowGraphicsMode)
1207 Style = GraphicsMenuStyle;
1208
1209 return RunGenericMenu(Screen, Style, &DefaultEntry, ChosenEntry);
1210 }
1211
1212 UINTN RunMainMenu(REFIT_MENU_SCREEN *Screen, CHAR16** DefaultSelection, REFIT_MENU_ENTRY **ChosenEntry)
1213 {
1214 MENU_STYLE_FUNC Style = TextMenuStyle;
1215 MENU_STYLE_FUNC MainStyle = TextMenuStyle;
1216 REFIT_MENU_ENTRY *TempChosenEntry;
1217 CHAR16 *MenuTitle;
1218 UINTN MenuExit = 0;
1219 INTN DefaultEntryIndex = -1;
1220 INTN DefaultSubmenuIndex = -1;
1221
1222 TileSizes[0] = (GlobalConfig.IconSizes[ICON_SIZE_BIG] * 9) / 8;
1223 TileSizes[1] = (GlobalConfig.IconSizes[ICON_SIZE_SMALL] * 4) / 3;
1224
1225 if ((DefaultSelection != NULL) && (*DefaultSelection != NULL)) {
1226 // Find a menu entry that includes *DefaultSelection as a substring
1227 DefaultEntryIndex = FindMenuShortcutEntry(Screen, *DefaultSelection);
1228 }
1229
1230 if (AllowGraphicsMode) {
1231 Style = GraphicsMenuStyle;
1232 MainStyle = MainMenuStyle;
1233 }
1234
1235 while (!MenuExit) {
1236 MenuExit = RunGenericMenu(Screen, MainStyle, &DefaultEntryIndex, &TempChosenEntry);
1237 Screen->TimeoutSeconds = 0;
1238
1239 MenuTitle = StrDuplicate(TempChosenEntry->Title);
1240 if (MenuExit == MENU_EXIT_DETAILS) {
1241 if (TempChosenEntry->SubScreen != NULL) {
1242 MenuExit = RunGenericMenu(TempChosenEntry->SubScreen, Style, &DefaultSubmenuIndex, &TempChosenEntry);
1243 if (MenuExit == MENU_EXIT_ESCAPE || TempChosenEntry->Tag == TAG_RETURN)
1244 MenuExit = 0;
1245 if (MenuExit == MENU_EXIT_DETAILS) {
1246 if (!EditOptions((LOADER_ENTRY *) TempChosenEntry))
1247 MenuExit = 0;
1248 } // if
1249 } else { // no sub-screen; ignore keypress
1250 MenuExit = 0;
1251 }
1252 } // Enter sub-screen
1253 }
1254
1255 if (ChosenEntry)
1256 *ChosenEntry = TempChosenEntry;
1257 if (DefaultSelection) {
1258 MyFreePool(*DefaultSelection);
1259 *DefaultSelection = MenuTitle;
1260 } // if
1261 return MenuExit;
1262 } /* UINTN RunMainMenu() */