]> code.delx.au - refind/blob - refind/menu.c
Pre-allocate TouchState on the stack and pass it's address to GetState
[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 UINTN Item = FindMainMenuItem(Screen, &State, TouchState.CurrentX, TouchState.CurrentY);
618 switch (Item) {
619 case TOUCH_NO_ITEM:
620 //do nothing
621 break;
622 case TOUCH_LEFT_ARROW:
623 UpdateScroll(&State, SCROLL_PAGE_UP);
624 break;
625 case TOUCH_RIGHT_ARROW:
626 UpdateScroll(&State, SCROLL_PAGE_DOWN);
627 break;
628 default:
629 State.CurrentSelection = Item;
630 MenuExit = MENU_EXIT_ENTER;
631 break;
632 }
633 }
634 }
635
636 StyleFunc(Screen, &State, MENU_FUNCTION_CLEANUP, NULL);
637
638 if (ChosenEntry)
639 *ChosenEntry = Screen->Entries[State.CurrentSelection];
640 *DefaultEntryIndex = State.CurrentSelection;
641 return MenuExit;
642 } /* static UINTN RunGenericMenu() */
643
644 //
645 // text-mode generic style
646 //
647
648 // Show information lines in text mode.
649 static VOID ShowTextInfoLines(IN REFIT_MENU_SCREEN *Screen) {
650 INTN i;
651
652 BeginTextScreen(Screen->Title);
653 if (Screen->InfoLineCount > 0) {
654 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
655 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
656 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, 4 + i);
657 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->InfoLines[i]);
658 }
659 }
660 } // VOID ShowTextInfoLines()
661
662 // Do most of the work for text-based menus....
663 static VOID TextMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
664 {
665 INTN i;
666 UINTN MenuWidth, ItemWidth, MenuHeight;
667 static UINTN MenuPosY;
668 static CHAR16 **DisplayStrings;
669 CHAR16 TimeoutMessage[256];
670
671 State->ScrollMode = SCROLL_MODE_TEXT;
672 switch (Function) {
673
674 case MENU_FUNCTION_INIT:
675 // vertical layout
676 MenuPosY = 4;
677 if (Screen->InfoLineCount > 0)
678 MenuPosY += Screen->InfoLineCount + 1;
679 MenuHeight = ConHeight - MenuPosY - 3;
680 if (Screen->TimeoutSeconds > 0)
681 MenuHeight -= 2;
682 InitScroll(State, Screen->EntryCount, MenuHeight);
683
684 // determine width of the menu
685 MenuWidth = 20; // minimum
686 for (i = 0; i <= State->MaxIndex; i++) {
687 ItemWidth = StrLen(Screen->Entries[i]->Title);
688 if (MenuWidth < ItemWidth)
689 MenuWidth = ItemWidth;
690 }
691 MenuWidth += 2;
692 if (MenuWidth > ConWidth - 3)
693 MenuWidth = ConWidth - 3;
694
695 // prepare strings for display
696 DisplayStrings = AllocatePool(sizeof(CHAR16 *) * Screen->EntryCount);
697 for (i = 0; i <= State->MaxIndex; i++) {
698 // Note: Theoretically, SPrint() is a cleaner way to do this; but the
699 // description of the StrSize parameter to SPrint implies it's measured
700 // in characters, but in practice both TianoCore and GNU-EFI seem to
701 // use bytes instead, resulting in truncated displays. I could just
702 // double the size of the StrSize parameter, but that seems unsafe in
703 // case a future library change starts treating this as characters, so
704 // I'm doing it the hard way in this instance.
705 // TODO: Review the above and possibly change other uses of SPrint()
706 DisplayStrings[i] = AllocateZeroPool(2 * sizeof(CHAR16));
707 DisplayStrings[i][0] = L' ';
708 MergeStrings(&DisplayStrings[i], Screen->Entries[i]->Title, 0);
709 if (StrLen(DisplayStrings[i]) > MenuWidth)
710 DisplayStrings[i][MenuWidth - 1] = 0;
711 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
712 // TODO: account for double-width characters
713 } // for
714
715 break;
716
717 case MENU_FUNCTION_CLEANUP:
718 // release temporary memory
719 for (i = 0; i <= State->MaxIndex; i++)
720 MyFreePool(DisplayStrings[i]);
721 MyFreePool(DisplayStrings);
722 break;
723
724 case MENU_FUNCTION_PAINT_ALL:
725 // paint the whole screen (initially and after scrolling)
726
727 ShowTextInfoLines(Screen);
728 for (i = 0; i <= State->MaxIndex; i++) {
729 if (i >= State->FirstVisible && i <= State->LastVisible) {
730 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (i - State->FirstVisible));
731 if (i == State->CurrentSelection)
732 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
733 else
734 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
735 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[i]);
736 }
737 }
738 // scrolling indicators
739 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_SCROLLARROW);
740 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY);
741 if (State->FirstVisible > 0)
742 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowUp);
743 else
744 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
745 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY + State->MaxVisible);
746 if (State->LastVisible < State->MaxIndex)
747 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowDown);
748 else
749 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
750 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
751 if (Screen->Hint1 != NULL) {
752 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 2);
753 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->Hint1);
754 }
755 if (Screen->Hint2 != NULL) {
756 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 1);
757 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->Hint2);
758 }
759 }
760 break;
761
762 case MENU_FUNCTION_PAINT_SELECTION:
763 // redraw selection cursor
764 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2,
765 MenuPosY + (State->PreviousSelection - State->FirstVisible));
766 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
767 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->PreviousSelection]);
768 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2,
769 MenuPosY + (State->CurrentSelection - State->FirstVisible));
770 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
771 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->CurrentSelection]);
772 break;
773
774 case MENU_FUNCTION_PAINT_TIMEOUT:
775 if (ParamText[0] == 0) {
776 // clear message
777 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
778 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 3);
779 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, BlankLine + 1);
780 } else {
781 // paint or update message
782 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_ERROR);
783 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, ConHeight - 3);
784 SPrint(TimeoutMessage, 255, L"%s ", ParamText);
785 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, TimeoutMessage);
786 }
787 break;
788
789 }
790 }
791
792 //
793 // graphical generic style
794 //
795
796 inline static UINTN TextLineHeight(VOID) {
797 return egGetFontHeight() + TEXT_YMARGIN * 2;
798 } // UINTN TextLineHeight()
799
800 //
801 // Display a submenu
802 //
803
804 // Display text with a solid background (MenuBackgroundPixel or SelectionBackgroundPixel).
805 // Indents text by one character and placed TEXT_YMARGIN pixels down from the
806 // specified XPos and YPos locations.
807 static VOID DrawText(IN CHAR16 *Text, IN BOOLEAN Selected, IN UINTN FieldWidth, IN UINTN XPos, IN UINTN YPos)
808 {
809 EG_IMAGE *TextBuffer;
810 EG_PIXEL Bg;
811
812 TextBuffer = egCreateImage(FieldWidth, TextLineHeight(), FALSE);
813
814 egFillImage(TextBuffer, &MenuBackgroundPixel);
815 Bg = MenuBackgroundPixel;
816 if (Selected) {
817 // draw selection bar background
818 egFillImageArea(TextBuffer, 0, 0, FieldWidth, TextBuffer->Height, &SelectionBackgroundPixel);
819 Bg = SelectionBackgroundPixel;
820 }
821
822 // render the text
823 egRenderText(Text, TextBuffer, egGetFontCellWidth(), TEXT_YMARGIN, (Bg.r + Bg.g + Bg.b) / 3);
824 egDrawImageWithTransparency(TextBuffer, NULL, XPos, YPos, TextBuffer->Width, TextBuffer->Height);
825 // BltImage(TextBuffer, XPos, YPos);
826 }
827
828 // Finds the average brightness of the input Image.
829 // NOTE: Passing an Image that covers the whole screen can strain the
830 // capacity of a UINTN on a 32-bit system with a very large display.
831 // Using UINT64 instead is unworkable, since the code won't compile
832 // on a 32-bit system. As the intended use for this function is to handle
833 // a single text string's background, this shouldn't be a problem, but it
834 // may need addressing if it's applied more broadly....
835 static UINT8 AverageBrightness(EG_IMAGE *Image) {
836 UINTN i;
837 UINTN Sum = 0;
838
839 if ((Image != NULL) && ((Image->Width * Image->Height) != 0)) {
840 for (i = 0; i < (Image->Width * Image->Height); i++) {
841 Sum += (Image->PixelData[i].r + Image->PixelData[i].g + Image->PixelData[i].b);
842 }
843 Sum /= (Image->Width * Image->Height * 3);
844 } // if
845 return (UINT8) Sum;
846 } // UINT8 AverageBrightness()
847
848 // Display text against the screen's background image. Special case: If Text is NULL
849 // or 0-length, clear the line. Does NOT indent the text or reposition it relative
850 // to the specified XPos and YPos values.
851 static VOID DrawTextWithTransparency(IN CHAR16 *Text, IN UINTN XPos, IN UINTN YPos)
852 {
853 UINTN TextWidth;
854 EG_IMAGE *TextBuffer = NULL;
855
856 if (Text == NULL)
857 Text = L"";
858
859 egMeasureText(Text, &TextWidth, NULL);
860 if (TextWidth == 0) {
861 TextWidth = UGAWidth;
862 XPos = 0;
863 }
864
865 TextBuffer = egCropImage(GlobalConfig.ScreenBackground, XPos, YPos, TextWidth, TextLineHeight());
866 if (TextBuffer == NULL)
867 return;
868
869 // render the text
870 egRenderText(Text, TextBuffer, 0, 0, AverageBrightness(TextBuffer));
871 egDrawImageWithTransparency(TextBuffer, NULL, XPos, YPos, TextBuffer->Width, TextBuffer->Height);
872 egFreeImage(TextBuffer);
873 }
874
875 // Compute the size & position of the window that will hold a subscreen's information.
876 static VOID ComputeSubScreenWindowSize(REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *XPos, UINTN *YPos,
877 UINTN *Width, UINTN *Height, UINTN *LineWidth) {
878 UINTN i, ItemWidth, HintTop, BannerBottomEdge, TitleWidth;
879 UINTN FontCellWidth = egGetFontCellWidth();
880 UINTN FontCellHeight = egGetFontHeight();
881
882 *Width = 20;
883 *Height = 5;
884 TitleWidth = egComputeTextWidth(Screen->Title);
885
886 for (i = 0; i < Screen->InfoLineCount; i++) {
887 ItemWidth = StrLen(Screen->InfoLines[i]);
888 if (*Width < ItemWidth) {
889 *Width = ItemWidth;
890 }
891 (*Height)++;
892 }
893 for (i = 0; i <= State->MaxIndex; i++) {
894 ItemWidth = StrLen(Screen->Entries[i]->Title);
895 if (*Width < ItemWidth) {
896 *Width = ItemWidth;
897 }
898 (*Height)++;
899 }
900 *Width = (*Width + 2) * FontCellWidth;
901 *LineWidth = *Width;
902 if (Screen->TitleImage)
903 *Width += (Screen->TitleImage->Width + TITLEICON_SPACING * 2 + FontCellWidth);
904 else
905 *Width += FontCellWidth;
906
907 if (*Width < TitleWidth)
908 *Width = TitleWidth + 2 * FontCellWidth;
909
910 // Keep it within the bounds of the screen, or 2/3 of the screen's width
911 // for screens over 800 pixels wide
912 if (*Width > UGAWidth)
913 *Width = UGAWidth;
914
915 *XPos = (UGAWidth - *Width) / 2;
916
917 HintTop = UGAHeight - (FontCellHeight * 3); // top of hint text
918 *Height *= TextLineHeight();
919 if (Screen->TitleImage && (*Height < (Screen->TitleImage->Height + TextLineHeight() * 4)))
920 *Height = Screen->TitleImage->Height + TextLineHeight() * 4;
921
922 if (GlobalConfig.BannerBottomEdge >= HintTop) { // probably a full-screen image; treat it as an empty banner
923 BannerBottomEdge = 0;
924 } else {
925 BannerBottomEdge = GlobalConfig.BannerBottomEdge;
926 }
927 if (*Height > (HintTop - BannerBottomEdge - FontCellHeight * 2)) {
928 BannerBottomEdge = 0;
929 }
930 if (*Height > (HintTop - BannerBottomEdge - FontCellHeight * 2)) {
931 // TODO: Implement scrolling in text screen.
932 *Height = (HintTop - BannerBottomEdge - FontCellHeight * 2);
933 }
934
935 *YPos = ((UGAHeight - *Height) / 2);
936 if (*YPos < BannerBottomEdge)
937 *YPos = BannerBottomEdge + FontCellHeight + (HintTop - BannerBottomEdge - *Height) / 2;
938 } // VOID ComputeSubScreenWindowSize()
939
940 // Displays sub-menus
941 static VOID GraphicsMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
942 {
943 INTN i;
944 UINTN ItemWidth;
945 static UINTN LineWidth, MenuWidth, MenuHeight, EntriesPosX, TitlePosX, EntriesPosY, TimeoutPosY, CharWidth;
946 EG_IMAGE *Window;
947 EG_PIXEL *BackgroundPixel = &(GlobalConfig.ScreenBackground->PixelData[0]);
948
949 CharWidth = egGetFontCellWidth();
950 State->ScrollMode = SCROLL_MODE_TEXT;
951 switch (Function) {
952
953 case MENU_FUNCTION_INIT:
954 InitScroll(State, Screen->EntryCount, 0);
955 ComputeSubScreenWindowSize(Screen, State, &EntriesPosX, &EntriesPosY, &MenuWidth, &MenuHeight, &LineWidth);
956 TimeoutPosY = EntriesPosY + (Screen->EntryCount + 1) * TextLineHeight();
957
958 // initial painting
959 SwitchToGraphicsAndClear();
960 Window = egCreateFilledImage(MenuWidth, MenuHeight, FALSE, BackgroundPixel);
961 egDrawImage(Window, EntriesPosX, EntriesPosY);
962 ItemWidth = egComputeTextWidth(Screen->Title);
963 if (MenuWidth > ItemWidth) {
964 TitlePosX = EntriesPosX + (MenuWidth - ItemWidth) / 2 - CharWidth;
965 } else {
966 TitlePosX = EntriesPosX;
967 if (CharWidth > 0) {
968 i = MenuWidth / CharWidth - 2;
969 if (i > 0)
970 Screen->Title[i] = 0;
971 } // if
972 } // if/else
973 break;
974
975 case MENU_FUNCTION_CLEANUP:
976 // nothing to do
977 break;
978
979 case MENU_FUNCTION_PAINT_ALL:
980 ComputeSubScreenWindowSize(Screen, State, &EntriesPosX, &EntriesPosY, &MenuWidth, &MenuHeight, &LineWidth);
981 DrawText(Screen->Title, FALSE, (StrLen(Screen->Title) + 2) * CharWidth, TitlePosX, EntriesPosY += TextLineHeight());
982 if (Screen->TitleImage) {
983 BltImageAlpha(Screen->TitleImage, EntriesPosX + TITLEICON_SPACING, EntriesPosY + TextLineHeight() * 2,
984 BackgroundPixel);
985 EntriesPosX += (Screen->TitleImage->Width + TITLEICON_SPACING * 2);
986 }
987 EntriesPosY += (TextLineHeight() * 2);
988 if (Screen->InfoLineCount > 0) {
989 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
990 DrawText(Screen->InfoLines[i], FALSE, LineWidth, EntriesPosX, EntriesPosY);
991 EntriesPosY += TextLineHeight();
992 }
993 EntriesPosY += TextLineHeight(); // also add a blank line
994 }
995
996 for (i = 0; i <= State->MaxIndex; i++) {
997 DrawText(Screen->Entries[i]->Title, (i == State->CurrentSelection), LineWidth, EntriesPosX,
998 EntriesPosY + i * TextLineHeight());
999 }
1000 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
1001 if ((Screen->Hint1 != NULL) && (StrLen(Screen->Hint1) > 0))
1002 DrawTextWithTransparency(Screen->Hint1, (UGAWidth - egComputeTextWidth(Screen->Hint1)) / 2,
1003 UGAHeight - (egGetFontHeight() * 3));
1004 if ((Screen->Hint2 != NULL) && (StrLen(Screen->Hint2) > 0))
1005 DrawTextWithTransparency(Screen->Hint2, (UGAWidth - egComputeTextWidth(Screen->Hint2)) / 2,
1006 UGAHeight - (egGetFontHeight() * 2));
1007 } // if
1008 break;
1009
1010 case MENU_FUNCTION_PAINT_SELECTION:
1011 // redraw selection cursor
1012 DrawText(Screen->Entries[State->PreviousSelection]->Title, FALSE, LineWidth,
1013 EntriesPosX, EntriesPosY + State->PreviousSelection * TextLineHeight());
1014 DrawText(Screen->Entries[State->CurrentSelection]->Title, TRUE, LineWidth,
1015 EntriesPosX, EntriesPosY + State->CurrentSelection * TextLineHeight());
1016 break;
1017
1018 case MENU_FUNCTION_PAINT_TIMEOUT:
1019 DrawText(ParamText, FALSE, LineWidth, EntriesPosX, TimeoutPosY);
1020 break;
1021
1022 }
1023 } // static VOID GraphicsMenuStyle()
1024
1025 //
1026 // graphical main menu style
1027 //
1028
1029 static VOID DrawMainMenuEntry(REFIT_MENU_ENTRY *Entry, BOOLEAN selected, UINTN XPos, UINTN YPos)
1030 {
1031 EG_IMAGE *Background;
1032
1033 // don't draw selection image if using touch
1034 if (selected && !TouchActive) {
1035 Background = egCropImage(GlobalConfig.ScreenBackground, XPos, YPos,
1036 SelectionImages[Entry->Row]->Width, SelectionImages[Entry->Row]->Height);
1037 egComposeImage(Background, SelectionImages[Entry->Row], 0, 0);
1038 BltImageCompositeBadge(Background, Entry->Image, Entry->BadgeImage, XPos, YPos);
1039 } else { // Image not selected; copy background
1040 egDrawImageWithTransparency(Entry->Image, Entry->BadgeImage, XPos, YPos,
1041 SelectionImages[Entry->Row]->Width, SelectionImages[Entry->Row]->Height);
1042 } // if/else
1043 } // VOID DrawMainMenuEntry()
1044
1045 static VOID PaintAll(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
1046 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
1047 INTN i;
1048
1049 if (Screen->Entries[State->CurrentSelection]->Row == 0)
1050 AdjustScrollState(State);
1051 for (i = State->FirstVisible; i <= State->MaxIndex; i++) {
1052 if (Screen->Entries[i]->Row == 0) {
1053 if (i <= State->LastVisible) {
1054 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE,
1055 itemPosX[i - State->FirstVisible], row0PosY);
1056 } // if
1057 } else {
1058 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE, itemPosX[i], row1PosY);
1059 }
1060 }
1061 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL)) {
1062 DrawTextWithTransparency(L"", 0, textPosY);
1063 DrawTextWithTransparency(Screen->Entries[State->CurrentSelection]->Title,
1064 (UGAWidth - egComputeTextWidth(Screen->Entries[State->CurrentSelection]->Title)) >> 1,
1065 textPosY);
1066 }
1067
1068 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
1069 DrawTextWithTransparency(Screen->Hint1, (UGAWidth - egComputeTextWidth(Screen->Hint1)) / 2,
1070 UGAHeight - (egGetFontHeight() * 3));
1071 DrawTextWithTransparency(Screen->Hint2, (UGAWidth - egComputeTextWidth(Screen->Hint2)) / 2,
1072 UGAHeight - (egGetFontHeight() * 2));
1073 } // if
1074 } // static VOID PaintAll()
1075
1076 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
1077 static VOID PaintSelection(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
1078 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
1079 UINTN XSelectPrev, XSelectCur, YPosPrev, YPosCur;
1080
1081 if (((State->CurrentSelection <= State->LastVisible) && (State->CurrentSelection >= State->FirstVisible)) ||
1082 (State->CurrentSelection >= State->InitialRow1) ) {
1083 if (Screen->Entries[State->PreviousSelection]->Row == 0) {
1084 XSelectPrev = State->PreviousSelection - State->FirstVisible;
1085 YPosPrev = row0PosY;
1086 } else {
1087 XSelectPrev = State->PreviousSelection;
1088 YPosPrev = row1PosY;
1089 } // if/else
1090 if (Screen->Entries[State->CurrentSelection]->Row == 0) {
1091 XSelectCur = State->CurrentSelection - State->FirstVisible;
1092 YPosCur = row0PosY;
1093 } else {
1094 XSelectCur = State->CurrentSelection;
1095 YPosCur = row1PosY;
1096 } // if/else
1097 DrawMainMenuEntry(Screen->Entries[State->PreviousSelection], FALSE, itemPosX[XSelectPrev], YPosPrev);
1098 DrawMainMenuEntry(Screen->Entries[State->CurrentSelection], TRUE, itemPosX[XSelectCur], YPosCur);
1099 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL)) {
1100 DrawTextWithTransparency(L"", 0, textPosY);
1101 DrawTextWithTransparency(Screen->Entries[State->CurrentSelection]->Title,
1102 (UGAWidth - egComputeTextWidth(Screen->Entries[State->CurrentSelection]->Title)) >> 1,
1103 textPosY);
1104 }
1105 } else { // Current selection not visible; must redraw the menu....
1106 MainMenuStyle(Screen, State, MENU_FUNCTION_PAINT_ALL, NULL);
1107 }
1108 } // static VOID MoveSelection(VOID)
1109
1110 // Display a 48x48 icon at the specified location. Uses the image specified by
1111 // ExternalFilename if it's available, or BuiltInImage if it's not. The
1112 // Y position is specified as the center value, and so is adjusted by half
1113 // the icon's height. The X position is set along the icon's left
1114 // edge if Alignment == ALIGN_LEFT, and along the right edge if
1115 // Alignment == ALIGN_RIGHT
1116 static VOID PaintIcon(IN EG_EMBEDDED_IMAGE *BuiltInIcon, IN CHAR16 *ExternalFilename, UINTN PosX, UINTN PosY, UINTN Alignment) {
1117 EG_IMAGE *Icon = NULL;
1118
1119 Icon = egFindIcon(ExternalFilename, GlobalConfig.IconSizes[ICON_SIZE_SMALL]);
1120 if (Icon == NULL)
1121 Icon = egPrepareEmbeddedImage(BuiltInIcon, TRUE);
1122 if (Icon != NULL) {
1123 if (Alignment == ALIGN_RIGHT)
1124 PosX -= Icon->Width;
1125 egDrawImageWithTransparency(Icon, NULL, PosX, PosY - (Icon->Height / 2), Icon->Width, Icon->Height);
1126 }
1127 } // static VOID ()
1128
1129 UINTN ComputeRow0PosY(VOID) {
1130 return ((UGAHeight / 2) - TileSizes[0] / 2);
1131 } // UINTN ComputeRow0PosY()
1132
1133 // Display (or erase) the arrow icons to the left and right of an icon's row,
1134 // as appropriate.
1135 static VOID PaintArrows(SCROLL_STATE *State, UINTN PosX, UINTN PosY, UINTN row0Loaders) {
1136 EG_IMAGE *TempImage;
1137 UINTN Width, Height, RightX, AdjPosY;
1138
1139 // NOTE: Assume that left and right arrows are of the same size....
1140 Width = egemb_arrow_left.Width;
1141 Height = egemb_arrow_left.Height;
1142 RightX = (UGAWidth + (TileSizes[0] + TILE_XSPACING) * State->MaxVisible) / 2 + TILE_XSPACING;
1143 AdjPosY = PosY - (Height / 2);
1144
1145 // For PaintIcon() calls, the starting Y position is moved to the midpoint
1146 // of the surrounding row; PaintIcon() adjusts this back up by half the
1147 // icon's height to properly center it.
1148 if ((State->FirstVisible > 0) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
1149 PaintIcon(&egemb_arrow_left, L"arrow_left", PosX, PosY, ALIGN_RIGHT);
1150 } else {
1151 TempImage = egCropImage(GlobalConfig.ScreenBackground, PosX - Width, AdjPosY, Width, Height);
1152 BltImage(TempImage, PosX - Width, AdjPosY);
1153 egFreeImage(TempImage);
1154 } // if/else
1155
1156 if ((State->LastVisible < (row0Loaders - 1)) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
1157 PaintIcon(&egemb_arrow_right, L"arrow_right", RightX, PosY, ALIGN_LEFT);
1158 } else {
1159 TempImage = egCropImage(GlobalConfig.ScreenBackground, RightX, AdjPosY, Width, Height);
1160 BltImage(TempImage, RightX, AdjPosY);
1161 egFreeImage(TempImage);
1162 } // if/else
1163 } // VOID PaintArrows()
1164
1165 // Display main menu in graphics mode
1166 VOID MainMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
1167 {
1168 INTN i;
1169 static UINTN row0PosX, row0PosXRunning, row1PosY, row0Loaders;
1170 UINTN row0Count, row1Count, row1PosX, row1PosXRunning;
1171 static UINTN *itemPosX;
1172 static UINTN row0PosY, textPosY;
1173
1174 State->ScrollMode = SCROLL_MODE_ICONS;
1175 switch (Function) {
1176
1177 case MENU_FUNCTION_INIT:
1178 InitScroll(State, Screen->EntryCount, GlobalConfig.MaxTags);
1179
1180 // layout
1181 row0Count = 0;
1182 row1Count = 0;
1183 row0Loaders = 0;
1184 for (i = 0; i <= State->MaxIndex; i++) {
1185 if (Screen->Entries[i]->Row == 1) {
1186 row1Count++;
1187 } else {
1188 row0Loaders++;
1189 if (row0Count < State->MaxVisible)
1190 row0Count++;
1191 }
1192 }
1193 row0PosX = (UGAWidth + TILE_XSPACING - (TileSizes[0] + TILE_XSPACING) * row0Count) >> 1;
1194 row0PosY = ComputeRow0PosY();
1195 row1PosX = (UGAWidth + TILE_XSPACING - (TileSizes[1] + TILE_XSPACING) * row1Count) >> 1;
1196 row1PosY = row0PosY + TileSizes[0] + TILE_YSPACING;
1197 if (row1Count > 0)
1198 textPosY = row1PosY + TileSizes[1] + TILE_YSPACING;
1199 else
1200 textPosY = row1PosY;
1201
1202 itemPosX = AllocatePool(sizeof(UINTN) * Screen->EntryCount);
1203 row0PosXRunning = row0PosX;
1204 row1PosXRunning = row1PosX;
1205 for (i = 0; i <= State->MaxIndex; i++) {
1206 if (Screen->Entries[i]->Row == 0) {
1207 itemPosX[i] = row0PosXRunning;
1208 row0PosXRunning += TileSizes[0] + TILE_XSPACING;
1209 } else {
1210 itemPosX[i] = row1PosXRunning;
1211 row1PosXRunning += TileSizes[1] + TILE_XSPACING;
1212 }
1213 }
1214 // initial painting
1215 InitSelection();
1216 SwitchToGraphicsAndClear();
1217 break;
1218
1219 case MENU_FUNCTION_CLEANUP:
1220 MyFreePool(itemPosX);
1221 break;
1222
1223 case MENU_FUNCTION_PAINT_ALL:
1224 PaintAll(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
1225 // For PaintArrows(), the starting Y position is moved to the midpoint
1226 // of the surrounding row; PaintIcon() adjusts this back up by half the
1227 // icon's height to properly center it.
1228 PaintArrows(State, row0PosX - TILE_XSPACING, row0PosY + (TileSizes[0] / 2), row0Loaders);
1229 break;
1230
1231 case MENU_FUNCTION_PAINT_SELECTION:
1232 PaintSelection(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
1233 break;
1234
1235 case MENU_FUNCTION_PAINT_TIMEOUT:
1236 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL)) {
1237 DrawTextWithTransparency(L"", 0, textPosY + TextLineHeight());
1238 DrawTextWithTransparency(ParamText, (UGAWidth - egComputeTextWidth(ParamText)) >> 1, textPosY + TextLineHeight());
1239 }
1240 break;
1241
1242 }
1243 } // VOID MainMenuStyle()
1244
1245 // Determines the index of the main menu item at the given coordinates.
1246 UINTN FindMainMenuItem(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINT64 PosX, IN UINT64 PosY)
1247 {
1248 UINTN i;
1249 static UINTN row0PosX, row0PosXRunning, row1PosY, row0Loaders;
1250 UINTN row0Count, row1Count, row1PosX, row1PosXRunning;
1251 static UINTN *itemPosX;
1252 static UINTN row0PosY;
1253 UINTN itemRow;
1254
1255 row0Count = 0;
1256 row1Count = 0;
1257 row0Loaders = 0;
1258 for (i = 0; i <= State->MaxIndex; i++) {
1259 if (Screen->Entries[i]->Row == 1) {
1260 row1Count++;
1261 } else {
1262 row0Loaders++;
1263 if (row0Count < State->MaxVisible)
1264 row0Count++;
1265 }
1266 }
1267 row0PosX = (UGAWidth + TILE_XSPACING - (TileSizes[0] + TILE_XSPACING) * row0Count) >> 1;
1268 row0PosY = ComputeRow0PosY();
1269 row1PosX = (UGAWidth + TILE_XSPACING - (TileSizes[1] + TILE_XSPACING) * row1Count) >> 1;
1270 row1PosY = row0PosY + TileSizes[0] + TILE_YSPACING;
1271
1272 if(PosY >= row0PosY && PosY <= row0PosY + TileSizes[0]) {
1273 itemRow = 0;
1274 if(PosX <= row0PosX) {
1275 return TOUCH_LEFT_ARROW;
1276 }
1277 else if(PosX >= (UGAWidth - row0PosX)) {
1278 return TOUCH_RIGHT_ARROW;
1279 }
1280 } else if(PosY >= row1PosY && PosY <= row1PosY + TileSizes[1]) {
1281 itemRow = 1;
1282 } else { // Y coordinate is outside of either row
1283 return TOUCH_NO_ITEM;
1284 }
1285
1286 UINTN ItemIndex = TOUCH_NO_ITEM;
1287
1288 itemPosX = AllocatePool(sizeof(UINTN) * Screen->EntryCount);
1289 row0PosXRunning = row0PosX;
1290 row1PosXRunning = row1PosX;
1291 for (i = 0; i <= State->MaxIndex; i++) {
1292 if (Screen->Entries[i]->Row == 0) {
1293 itemPosX[i] = row0PosXRunning;
1294 row0PosXRunning += TileSizes[0] + TILE_XSPACING;
1295 } else {
1296 itemPosX[i] = row1PosXRunning;
1297 row1PosXRunning += TileSizes[1] + TILE_XSPACING;
1298 }
1299 }
1300
1301 for (i = State->FirstVisible; i <= State->MaxIndex; i++) {
1302 if (Screen->Entries[i]->Row == 0 && itemRow == 0) {
1303 if (i <= State->LastVisible) {
1304 if(PosX >= itemPosX[i - State->FirstVisible] && PosX <= itemPosX[i - State->FirstVisible] + TileSizes[0]) {
1305 ItemIndex = i;
1306 break;
1307 }
1308 } // if
1309 } else if (Screen->Entries[i]->Row == 1 && itemRow == 1) {
1310 if(PosX >= itemPosX[i] && PosX <= itemPosX[i] + TileSizes[1]) {
1311 ItemIndex = i;
1312 break;
1313 }
1314 }
1315 }
1316
1317 MyFreePool(itemPosX);
1318
1319 return ItemIndex;
1320 } // VOID FindMainMenuItem()
1321
1322 // Enable the user to edit boot loader options.
1323 // Returns TRUE if the user exited with edited options; FALSE if the user
1324 // pressed Esc to terminate the edit.
1325 static BOOLEAN EditOptions(LOADER_ENTRY *MenuEntry) {
1326 UINTN x_max, y_max;
1327 CHAR16 *EditedOptions;
1328 BOOLEAN retval = FALSE;
1329
1330 if (GlobalConfig.HideUIFlags & HIDEUI_FLAG_EDITOR) {
1331 return FALSE;
1332 }
1333
1334 refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max);
1335
1336 if (!GlobalConfig.TextOnly)
1337 SwitchToText(TRUE);
1338
1339 if (line_edit(MenuEntry->LoadOptions, &EditedOptions, x_max)) {
1340 MyFreePool(MenuEntry->LoadOptions);
1341 MenuEntry->LoadOptions = EditedOptions;
1342 retval = TRUE;
1343 } // if
1344 if (!GlobalConfig.TextOnly)
1345 SwitchToGraphics();
1346 return retval;
1347 } // VOID EditOptions()
1348
1349 //
1350 // user-callable dispatcher functions
1351 //
1352
1353 UINTN RunMenu(IN REFIT_MENU_SCREEN *Screen, OUT REFIT_MENU_ENTRY **ChosenEntry)
1354 {
1355 INTN DefaultEntry = -1;
1356 MENU_STYLE_FUNC Style = TextMenuStyle;
1357
1358 if (AllowGraphicsMode)
1359 Style = GraphicsMenuStyle;
1360
1361 return RunGenericMenu(Screen, Style, &DefaultEntry, ChosenEntry);
1362 }
1363
1364 UINTN RunMainMenu(REFIT_MENU_SCREEN *Screen, CHAR16** DefaultSelection, REFIT_MENU_ENTRY **ChosenEntry)
1365 {
1366 MENU_STYLE_FUNC Style = TextMenuStyle;
1367 MENU_STYLE_FUNC MainStyle = TextMenuStyle;
1368 REFIT_MENU_ENTRY *TempChosenEntry;
1369 CHAR16 *MenuTitle;
1370 UINTN MenuExit = 0;
1371 INTN DefaultEntryIndex = -1;
1372 INTN DefaultSubmenuIndex = -1;
1373
1374 TileSizes[0] = (GlobalConfig.IconSizes[ICON_SIZE_BIG] * 9) / 8;
1375 TileSizes[1] = (GlobalConfig.IconSizes[ICON_SIZE_SMALL] * 4) / 3;
1376
1377 if ((DefaultSelection != NULL) && (*DefaultSelection != NULL)) {
1378 // Find a menu entry that includes *DefaultSelection as a substring
1379 DefaultEntryIndex = FindMenuShortcutEntry(Screen, *DefaultSelection);
1380 }
1381
1382 if (AllowGraphicsMode) {
1383 Style = GraphicsMenuStyle;
1384 MainStyle = MainMenuStyle;
1385 if (GlobalConfig.EnableTouch) {
1386 // Check for touch availability
1387 EFI_STATUS status = refit_call3_wrapper(BS->LocateProtocol, &TouchGuid, NULL, (VOID **) &TouchProtocol);
1388 if (status == EFI_SUCCESS) {
1389 TouchEnabled = TouchActive = TRUE;
1390 }
1391 }
1392 }
1393
1394 while (!MenuExit) {
1395 MenuExit = RunGenericMenu(Screen, MainStyle, &DefaultEntryIndex, &TempChosenEntry);
1396 Screen->TimeoutSeconds = 0;
1397
1398 MenuTitle = StrDuplicate(TempChosenEntry->Title);
1399 if (MenuExit == MENU_EXIT_DETAILS) {
1400 if (TempChosenEntry->SubScreen != NULL) {
1401 MenuExit = RunGenericMenu(TempChosenEntry->SubScreen, Style, &DefaultSubmenuIndex, &TempChosenEntry);
1402 if (MenuExit == MENU_EXIT_ESCAPE || TempChosenEntry->Tag == TAG_RETURN)
1403 MenuExit = 0;
1404 if (MenuExit == MENU_EXIT_DETAILS) {
1405 if (!EditOptions((LOADER_ENTRY *) TempChosenEntry))
1406 MenuExit = 0;
1407 } // if
1408 } else { // no sub-screen; ignore keypress
1409 MenuExit = 0;
1410 }
1411 } // Enter sub-screen
1412 }
1413
1414 if (ChosenEntry)
1415 *ChosenEntry = TempChosenEntry;
1416 if (DefaultSelection) {
1417 MyFreePool(*DefaultSelection);
1418 *DefaultSelection = MenuTitle;
1419 } // if
1420 return MenuExit;
1421 } /* UINTN RunMainMenu() */