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