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