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