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