5 * Copyright (c) 2006 Christoph Pfisterer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
37 * Modifications copyright (c) 2012-2015 Roderick W. Smith
39 * Modifications distributed under the terms of the GNU General Public
40 * License (GPL) version 3 (GPLv3), or (at your option) any later version.
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.
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.
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/>.
65 #include "line_edit.h"
66 #include "mystrings.h"
67 #include "../include/refit_call_wrapper.h"
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"
74 // other menu definitions
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)
83 typedef VOID (*MENU_STYLE_FUNC
)(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
);
85 static CHAR16 ArrowUp
[2] = { ARROW_UP
, 0 };
86 static CHAR16 ArrowDown
[2] = { ARROW_DOWN
, 0 };
87 static UINTN TileSizes
[2] = { 144, 64 };
89 // Text and icon spacing constants....
90 #define TEXT_YMARGIN (2)
91 #define TITLEICON_SPACING (16)
93 #define TILE_XSPACING (8)
94 #define TILE_YSPACING (16)
96 // Alignment values for PaintIcon()
100 static EG_IMAGE
*SelectionImages
[2] = { NULL
, NULL
};
101 static EG_PIXEL SelectionBackgroundPixel
= { 0xff, 0xff, 0xff, 0 };
104 EFI_ABSOLUTE_POINTER_PROTOCOL
*TouchProtocol
= NULL
;
105 EFI_GUID TouchGuid
= EFI_ABSOLUTE_POINTER_PROTOCOL_GUID
;
106 BOOLEAN TouchEnabled
= FALSE
;
107 BOOLEAN TouchActive
= FALSE
;
110 // Graphics helper functions
113 static VOID
InitSelection(VOID
)
115 EG_IMAGE
*TempSmallImage
= NULL
, *TempBigImage
= NULL
;
116 BOOLEAN LoadedSmallImage
= FALSE
;
118 if (!AllowGraphicsMode
)
120 if (SelectionImages
[0] != NULL
)
123 // load small selection image
124 if (GlobalConfig
.SelectionSmallFileName
!= NULL
) {
125 TempSmallImage
= egLoadImage(SelfDir
, GlobalConfig
.SelectionSmallFileName
, TRUE
);
127 if (TempSmallImage
== NULL
)
128 TempSmallImage
= egPrepareEmbeddedImage(&egemb_back_selected_small
, TRUE
);
130 LoadedSmallImage
= TRUE
;
131 SelectionImages
[1] = egScaleImage(TempSmallImage
, TileSizes
[1], TileSizes
[1]);
133 // load big selection image
134 if (GlobalConfig
.SelectionBigFileName
!= NULL
) {
135 TempBigImage
= egLoadImage(SelfDir
, GlobalConfig
.SelectionBigFileName
, TRUE
);
137 if (TempBigImage
== NULL
) {
138 if (LoadedSmallImage
) {
139 // calculate big selection image from small one
140 TempBigImage
= egCopyImage(TempSmallImage
);
142 TempBigImage
= egPrepareEmbeddedImage(&egemb_back_selected_big
, TRUE
);
145 SelectionImages
[0] = egScaleImage(TempBigImage
, TileSizes
[0], TileSizes
[0]);
148 egFreeImage(TempSmallImage
);
150 egFreeImage(TempBigImage
);
151 } // VOID InitSelection()
154 // Scrolling functions
157 static VOID
InitScroll(OUT SCROLL_STATE
*State
, IN UINTN ItemCount
, IN UINTN VisibleSpace
)
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;
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
;
171 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
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
;
184 if (State
->CurrentSelection
< State
->FirstVisible
) {
185 State
->FirstVisible
= State
->CurrentSelection
;
186 State
->LastVisible
= State
->CurrentSelection
+ State
->MaxVisible
- 1;
187 State
->PaintAll
= TRUE
;
189 } // static VOID AdjustScrollState
191 static VOID
UpdateScroll(IN OUT SCROLL_STATE
*State
, IN UINTN Movement
)
193 State
->PreviousSelection
= State
->CurrentSelection
;
196 case SCROLL_LINE_LEFT
:
197 if (State
->CurrentSelection
> 0) {
198 State
->CurrentSelection
--;
202 case SCROLL_LINE_RIGHT
:
203 if (State
->CurrentSelection
< State
->MaxIndex
) {
204 State
->CurrentSelection
++;
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
);
216 State
->CurrentSelection
= State
->FirstVisible
;
218 } // if in second row
220 if (State
->CurrentSelection
> 0)
221 State
->CurrentSelection
--;
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
);
233 State
->CurrentSelection
= State
->InitialRow1
;
237 if (State
->CurrentSelection
< State
->MaxIndex
)
238 State
->CurrentSelection
++;
243 if (State
->CurrentSelection
<= State
->FinalRow0
)
244 State
->CurrentSelection
-= State
->MaxVisible
;
245 else if (State
->CurrentSelection
== State
->InitialRow1
)
246 State
->CurrentSelection
= State
->FinalRow0
;
248 State
->CurrentSelection
= State
->InitialRow1
;
249 if (State
->CurrentSelection
< 0)
250 State
->CurrentSelection
= 0;
254 if (State
->CurrentSelection
> 0) {
255 State
->PaintAll
= TRUE
;
256 State
->CurrentSelection
= 0;
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
++;
268 State
->CurrentSelection
= State
->MaxIndex
;
270 if (State
->CurrentSelection
> State
->MaxIndex
)
271 State
->CurrentSelection
= State
->MaxIndex
;
275 if (State
->CurrentSelection
< State
->MaxIndex
) {
276 State
->PaintAll
= TRUE
;
277 State
->CurrentSelection
= State
->MaxIndex
;
285 if (State
->ScrollMode
== SCROLL_MODE_TEXT
)
286 AdjustScrollState(State
);
288 if (!State
->PaintAll
&& State
->CurrentSelection
!= State
->PreviousSelection
)
289 State
->PaintSelection
= TRUE
;
290 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
291 } // static VOID UpdateScroll()
294 // menu helper functions
297 VOID
AddMenuInfoLine(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*InfoLine
)
299 AddListElement((VOID
***) &(Screen
->InfoLines
), &(Screen
->InfoLineCount
), InfoLine
);
302 VOID
AddMenuEntry(IN REFIT_MENU_SCREEN
*Screen
, IN REFIT_MENU_ENTRY
*Entry
)
304 AddListElement((VOID
***) &(Screen
->Entries
), &(Screen
->EntryCount
), Entry
);
308 static INTN
FindMenuShortcutEntry(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*Defaults
)
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');
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
);
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
);
333 MyFreePool(Shortcut
);
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
) {
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
;
354 if ((State
->ScrollMode
== SCROLL_MODE_ICONS
) && (State
->MaxVisible
> (State
->FinalRow0
+ 1)))
355 State
->MaxVisible
= State
->FinalRow0
+ 1;
356 } // static VOID IdentifyRows()
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
) {
364 EG_PIXEL Black
= { 0x0, 0x0, 0x0, 0 };
365 EFI_EVENT WaitList
[2] = { ST
->ConIn
->WaitForKey
, NULL
};
367 egClearScreen(&Black
);
369 WaitList
[1] = TouchProtocol
->WaitForInput
;
370 refit_call3_wrapper(BS
->WaitForEvent
, 2, WaitList
, &index
);
372 refit_call3_wrapper(BS
->WaitForEvent
, 1, WaitList
, &index
);
374 if (AllowGraphicsMode
)
375 SwitchToGraphicsAndClear();
377 } // VOID SaveScreen()
380 // generic menu function
382 static UINTN
RunGenericMenu(IN REFIT_MENU_SCREEN
*Screen
, IN MENU_STYLE_FUNC StyleFunc
, IN OUT INTN
*DefaultEntryIndex
,
383 OUT REFIT_MENU_ENTRY
**ChosenEntry
)
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];
398 EFI_STATUS TouchStatus
= EFI_NOT_READY
;
399 EFI_ABSOLUTE_POINTER_STATE
*TouchState
= NULL
;
401 if (Screen
->TimeoutSeconds
> 0) {
403 TimeoutCountdown
= Screen
->TimeoutSeconds
* 10;
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
);
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
;
421 KeyAsString
[0] = key
.UnicodeChar
;
423 ShortcutEntry
= FindMenuShortcutEntry(Screen
, KeyAsString
);
424 if (ShortcutEntry
>= 0) {
425 State
.CurrentSelection
= ShortcutEntry
;
426 MenuExit
= MENU_EXIT_ENTER
;
428 WaitForRelease
= TRUE
;
434 if (GlobalConfig
.ScreensaverTime
!= -1)
435 State
.PaintAll
= TRUE
;
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
;
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);
454 WaitForRelease
= FALSE
;
455 refit_call2_wrapper(ST
->ConIn
->Reset
, ST
->ConIn
, TRUE
);
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
;
470 // read key press or touch event (and wait for them if applicable)
472 TouchStatus
= refit_call2_wrapper(TouchProtocol
->GetState
, TouchProtocol
, TouchState
);
474 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
476 if(Status
== EFI_SUCCESS
) {
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
;
488 TimeSinceKeystroke
= 0;
490 EFI_EVENT WaitList
[3] = { ST
->ConIn
->WaitForKey
, NULL
, NULL
};
491 if (HaveTimeout
&& TimeoutCountdown
== 0) {
493 MenuExit
= MENU_EXIT_TIMEOUT
;
495 } else if (HaveTimeout
|| GlobalConfig
.ScreensaverTime
> 0) {
496 EFI_EVENT TimerEvent
;
497 UINTN ElapsCount
= 1;
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
505 refit_call3_wrapper(BS
->SetTimer
, TimerEvent
, TimerRelative
, 10000000); // 1s Timeout
508 WaitList
[1] = TouchProtocol
->WaitForInput
;
509 WaitList
[2] = TimerEvent
;
510 Status
= refit_call3_wrapper(BS
->WaitForEvent
, 3, WaitList
, &Index
);
512 WaitList
[1] = TimerEvent
;
513 Status
= refit_call3_wrapper(BS
->WaitForEvent
, 2, WaitList
, &Index
);
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))
522 ElapsCount
= 10; // always counted as 1s to end of the timeout
524 TimeSinceKeystroke
+= ElapsCount
;
526 TimeoutCountdown
= TimeoutCountdown
<= ElapsCount
? 0 : TimeoutCountdown
- ElapsCount
;
527 } else if (GlobalConfig
.ScreensaverTime
> 0 &&
528 TimeSinceKeystroke
> (GlobalConfig
.ScreensaverTime
* 10))
531 State
.PaintAll
= TRUE
;
532 TimeSinceKeystroke
= 0;
536 WaitList
[1] = TouchProtocol
->WaitForInput
;
537 refit_call3_wrapper(BS
->WaitForEvent
, 2, WaitList
, &index
);
539 refit_call3_wrapper(BS
->WaitForEvent
, 1, WaitList
, &index
);
543 } // if/else !read keystroke
546 // the user pressed a key, cancel the timeout
547 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, L
"");
549 if (GlobalConfig
.ScreensaverTime
== -1) { // cancel start-with-blank-screen coding
550 GlobalConfig
.ScreensaverTime
= 0;
551 if (!GlobalConfig
.TextOnly
)
552 BltClearScreen(TRUE
);
556 if(!TouchActive
) { // react to key press
557 switch (key
.ScanCode
) {
559 UpdateScroll(&State
, SCROLL_LINE_UP
);
562 UpdateScroll(&State
, SCROLL_LINE_LEFT
);
565 UpdateScroll(&State
, SCROLL_LINE_DOWN
);
568 UpdateScroll(&State
, SCROLL_LINE_RIGHT
);
571 UpdateScroll(&State
, SCROLL_FIRST
);
574 UpdateScroll(&State
, SCROLL_LAST
);
577 UpdateScroll(&State
, SCROLL_PAGE_UP
);
580 UpdateScroll(&State
, SCROLL_PAGE_DOWN
);
583 MenuExit
= MENU_EXIT_ESCAPE
;
587 MenuExit
= MENU_EXIT_DETAILS
;
594 MenuExit
= MENU_EXIT_ESCAPE
;
597 switch (key
.UnicodeChar
) {
599 case CHAR_CARRIAGE_RETURN
:
601 MenuExit
= MENU_EXIT_ENTER
;
604 MenuExit
= MENU_EXIT_DETAILS
;
607 KeyAsString
[0] = key
.UnicodeChar
;
609 ShortcutEntry
= FindMenuShortcutEntry(Screen
, KeyAsString
);
610 if (ShortcutEntry
>= 0) {
611 State
.CurrentSelection
= ShortcutEntry
;
612 MenuExit
= MENU_EXIT_ENTER
;
616 } else { //react to touch event
617 UINTN Item
= FindMainMenuItem(Screen
, &State
, TouchState
->CurrentX
, TouchState
->CurrentY
);
623 case TOUCH_LEFT_ARROW
:
624 UpdateScroll(&State
, SCROLL_PAGE_UP
);
626 case TOUCH_RIGHT_ARROW
:
627 UpdateScroll(&State
, SCROLL_PAGE_DOWN
);
630 State
.CurrentSelection
= Item
;
631 MenuExit
= MENU_EXIT_ENTER
;
637 StyleFunc(Screen
, &State
, MENU_FUNCTION_CLEANUP
, NULL
);
640 *ChosenEntry
= Screen
->Entries
[State
.CurrentSelection
];
641 *DefaultEntryIndex
= State
.CurrentSelection
;
643 } /* static UINTN RunGenericMenu() */
646 // text-mode generic style
649 // Show information lines in text mode.
650 static VOID
ShowTextInfoLines(IN REFIT_MENU_SCREEN
*Screen
) {
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
]);
661 } // VOID ShowTextInfoLines()
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
)
667 UINTN MenuWidth
, ItemWidth
, MenuHeight
;
668 static UINTN MenuPosY
;
669 static CHAR16
**DisplayStrings
;
670 CHAR16 TimeoutMessage
[256];
672 State
->ScrollMode
= SCROLL_MODE_TEXT
;
675 case MENU_FUNCTION_INIT
:
678 if (Screen
->InfoLineCount
> 0)
679 MenuPosY
+= Screen
->InfoLineCount
+ 1;
680 MenuHeight
= ConHeight
- MenuPosY
- 3;
681 if (Screen
->TimeoutSeconds
> 0)
683 InitScroll(State
, Screen
->EntryCount
, MenuHeight
);
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
;
693 if (MenuWidth
> ConWidth
- 3)
694 MenuWidth
= ConWidth
- 3;
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
718 case MENU_FUNCTION_CLEANUP
:
719 // release temporary memory
720 for (i
= 0; i
<= State
->MaxIndex
; i
++)
721 MyFreePool(DisplayStrings
[i
]);
722 MyFreePool(DisplayStrings
);
725 case MENU_FUNCTION_PAINT_ALL
:
726 // paint the whole screen (initially and after scrolling)
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
);
735 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
736 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[i
]);
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
);
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
);
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
);
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
);
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
]);
775 case MENU_FUNCTION_PAINT_TIMEOUT
:
776 if (ParamText
[0] == 0) {
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);
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
);
794 // graphical generic style
797 inline static UINTN
TextLineHeight(VOID
) {
798 return egGetFontHeight() + TEXT_YMARGIN
* 2;
799 } // UINTN TextLineHeight()
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
)
810 EG_IMAGE
*TextBuffer
;
813 TextBuffer
= egCreateImage(FieldWidth
, TextLineHeight(), FALSE
);
815 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
816 Bg
= MenuBackgroundPixel
;
818 // draw selection bar background
819 egFillImageArea(TextBuffer
, 0, 0, FieldWidth
, TextBuffer
->Height
, &SelectionBackgroundPixel
);
820 Bg
= SelectionBackgroundPixel
;
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);
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
) {
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
);
844 Sum
/= (Image
->Width
* Image
->Height
* 3);
847 } // UINT8 AverageBrightness()
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
)
855 EG_IMAGE
*TextBuffer
= NULL
;
860 egMeasureText(Text
, &TextWidth
, NULL
);
861 if (TextWidth
== 0) {
862 TextWidth
= UGAWidth
;
866 TextBuffer
= egCropImage(GlobalConfig
.ScreenBackground
, XPos
, YPos
, TextWidth
, TextLineHeight());
867 if (TextBuffer
== NULL
)
871 egRenderText(Text
, TextBuffer
, 0, 0, AverageBrightness(TextBuffer
));
872 egDrawImageWithTransparency(TextBuffer
, NULL
, XPos
, YPos
, TextBuffer
->Width
, TextBuffer
->Height
);
873 egFreeImage(TextBuffer
);
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();
885 TitleWidth
= egComputeTextWidth(Screen
->Title
);
887 for (i
= 0; i
< Screen
->InfoLineCount
; i
++) {
888 ItemWidth
= StrLen(Screen
->InfoLines
[i
]);
889 if (*Width
< ItemWidth
) {
894 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
895 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
896 if (*Width
< ItemWidth
) {
901 *Width
= (*Width
+ 2) * FontCellWidth
;
903 if (Screen
->TitleImage
)
904 *Width
+= (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
* 2 + FontCellWidth
);
906 *Width
+= FontCellWidth
;
908 if (*Width
< TitleWidth
)
909 *Width
= TitleWidth
+ 2 * FontCellWidth
;
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
)
916 *XPos
= (UGAWidth
- *Width
) / 2;
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;
923 if (GlobalConfig
.BannerBottomEdge
>= HintTop
) { // probably a full-screen image; treat it as an empty banner
924 BannerBottomEdge
= 0;
926 BannerBottomEdge
= GlobalConfig
.BannerBottomEdge
;
928 if (*Height
> (HintTop
- BannerBottomEdge
- FontCellHeight
* 2)) {
929 BannerBottomEdge
= 0;
931 if (*Height
> (HintTop
- BannerBottomEdge
- FontCellHeight
* 2)) {
932 // TODO: Implement scrolling in text screen.
933 *Height
= (HintTop
- BannerBottomEdge
- FontCellHeight
* 2);
936 *YPos
= ((UGAHeight
- *Height
) / 2);
937 if (*YPos
< BannerBottomEdge
)
938 *YPos
= BannerBottomEdge
+ FontCellHeight
+ (HintTop
- BannerBottomEdge
- *Height
) / 2;
939 } // VOID ComputeSubScreenWindowSize()
941 // Displays sub-menus
942 static VOID
GraphicsMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
946 static UINTN LineWidth
, MenuWidth
, MenuHeight
, EntriesPosX
, TitlePosX
, EntriesPosY
, TimeoutPosY
, CharWidth
;
948 EG_PIXEL
*BackgroundPixel
= &(GlobalConfig
.ScreenBackground
->PixelData
[0]);
950 CharWidth
= egGetFontCellWidth();
951 State
->ScrollMode
= SCROLL_MODE_TEXT
;
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();
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
;
967 TitlePosX
= EntriesPosX
;
969 i
= MenuWidth
/ CharWidth
- 2;
971 Screen
->Title
[i
] = 0;
976 case MENU_FUNCTION_CLEANUP
:
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,
986 EntriesPosX
+= (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
* 2);
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();
994 EntriesPosY
+= TextLineHeight(); // also add a blank line
997 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
998 DrawText(Screen
->Entries
[i
]->Title
, (i
== State
->CurrentSelection
), LineWidth
, EntriesPosX
,
999 EntriesPosY
+ i
* TextLineHeight());
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));
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());
1019 case MENU_FUNCTION_PAINT_TIMEOUT
:
1020 DrawText(ParamText
, FALSE
, LineWidth
, EntriesPosX
, TimeoutPosY
);
1024 } // static VOID GraphicsMenuStyle()
1027 // graphical main menu style
1030 static VOID
DrawMainMenuEntry(REFIT_MENU_ENTRY
*Entry
, BOOLEAN selected
, UINTN XPos
, UINTN YPos
)
1032 EG_IMAGE
*Background
;
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
);
1044 } // VOID DrawMainMenuEntry()
1046 static VOID
PaintAll(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
1047 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
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
);
1059 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
, itemPosX
[i
], row1PosY
);
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,
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));
1075 } // static VOID PaintAll()
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
;
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
;
1088 XSelectPrev
= State
->PreviousSelection
;
1089 YPosPrev
= row1PosY
;
1091 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0) {
1092 XSelectCur
= State
->CurrentSelection
- State
->FirstVisible
;
1095 XSelectCur
= State
->CurrentSelection
;
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,
1106 } else { // Current selection not visible; must redraw the menu....
1107 MainMenuStyle(Screen
, State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
1109 } // static VOID MoveSelection(VOID)
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
;
1120 Icon
= egFindIcon(ExternalFilename
, GlobalConfig
.IconSizes
[ICON_SIZE_SMALL
]);
1122 Icon
= egPrepareEmbeddedImage(BuiltInIcon
, TRUE
);
1124 if (Alignment
== ALIGN_RIGHT
)
1125 PosX
-= Icon
->Width
;
1126 egDrawImageWithTransparency(Icon
, NULL
, PosX
, PosY
- (Icon
->Height
/ 2), Icon
->Width
, Icon
->Height
);
1130 UINTN
ComputeRow0PosY(VOID
) {
1131 return ((UGAHeight
/ 2) - TileSizes
[0] / 2);
1132 } // UINTN ComputeRow0PosY()
1134 // Display (or erase) the arrow icons to the left and right of an icon's row,
1136 static VOID
PaintArrows(SCROLL_STATE
*State
, UINTN PosX
, UINTN PosY
, UINTN row0Loaders
) {
1137 EG_IMAGE
*TempImage
;
1138 UINTN Width
, Height
, RightX
, AdjPosY
;
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);
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
);
1152 TempImage
= egCropImage(GlobalConfig
.ScreenBackground
, PosX
- Width
, AdjPosY
, Width
, Height
);
1153 BltImage(TempImage
, PosX
- Width
, AdjPosY
);
1154 egFreeImage(TempImage
);
1157 if ((State
->LastVisible
< (row0Loaders
- 1)) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
1158 PaintIcon(&egemb_arrow_right
, L
"arrow_right", RightX
, PosY
, ALIGN_LEFT
);
1160 TempImage
= egCropImage(GlobalConfig
.ScreenBackground
, RightX
, AdjPosY
, Width
, Height
);
1161 BltImage(TempImage
, RightX
, AdjPosY
);
1162 egFreeImage(TempImage
);
1164 } // VOID PaintArrows()
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
)
1170 static UINTN row0PosX
, row0PosXRunning
, row1PosY
, row0Loaders
;
1171 UINTN row0Count
, row1Count
, row1PosX
, row1PosXRunning
;
1172 static UINTN
*itemPosX
;
1173 static UINTN row0PosY
, textPosY
;
1175 State
->ScrollMode
= SCROLL_MODE_ICONS
;
1178 case MENU_FUNCTION_INIT
:
1179 InitScroll(State
, Screen
->EntryCount
, GlobalConfig
.MaxTags
);
1185 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
1186 if (Screen
->Entries
[i
]->Row
== 1) {
1190 if (row0Count
< State
->MaxVisible
)
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
;
1199 textPosY
= row1PosY
+ TileSizes
[1] + TILE_YSPACING
;
1201 textPosY
= row1PosY
;
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
;
1211 itemPosX
[i
] = row1PosXRunning
;
1212 row1PosXRunning
+= TileSizes
[1] + TILE_XSPACING
;
1217 SwitchToGraphicsAndClear();
1220 case MENU_FUNCTION_CLEANUP
:
1221 MyFreePool(itemPosX
);
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
);
1232 case MENU_FUNCTION_PAINT_SELECTION
:
1233 PaintSelection(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
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());
1244 } // VOID MainMenuStyle()
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
)
1250 static UINTN row0PosX
, row0PosXRunning
, row1PosY
, row0Loaders
;
1251 UINTN row0Count
, row1Count
, row1PosX
, row1PosXRunning
;
1252 static UINTN
*itemPosX
;
1253 static UINTN row0PosY
;
1259 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
1260 if (Screen
->Entries
[i
]->Row
== 1) {
1264 if (row0Count
< State
->MaxVisible
)
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
;
1273 if(PosY
>= row0PosY
&& PosY
<= row0PosY
+ TileSizes
[0]) {
1275 if(PosX
<= row0PosX
) {
1276 return TOUCH_LEFT_ARROW
;
1278 else if(PosX
>= (UGAWidth
- row0PosX
)) {
1279 return TOUCH_RIGHT_ARROW
;
1281 } else if(PosY
>= row1PosY
&& PosY
<= row1PosY
+ TileSizes
[1]) {
1283 } else { // Y coordinate is outside of either row
1284 return TOUCH_NO_ITEM
;
1287 UINTN ItemIndex
= TOUCH_NO_ITEM
;
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
;
1297 itemPosX
[i
] = row1PosXRunning
;
1298 row1PosXRunning
+= TileSizes
[1] + TILE_XSPACING
;
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]) {
1310 } else if (Screen
->Entries
[i
]->Row
== 1 && itemRow
== 1) {
1311 if(PosX
>= itemPosX
[i
] && PosX
<= itemPosX
[i
] + TileSizes
[1]) {
1318 MyFreePool(itemPosX
);
1321 } // VOID FindMainMenuItem()
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
) {
1328 CHAR16
*EditedOptions
;
1329 BOOLEAN retval
= FALSE
;
1331 if (GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_EDITOR
) {
1335 refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &x_max
, &y_max
);
1337 if (!GlobalConfig
.TextOnly
)
1340 if (line_edit(MenuEntry
->LoadOptions
, &EditedOptions
, x_max
)) {
1341 MyFreePool(MenuEntry
->LoadOptions
);
1342 MenuEntry
->LoadOptions
= EditedOptions
;
1345 if (!GlobalConfig
.TextOnly
)
1348 } // VOID EditOptions()
1351 // user-callable dispatcher functions
1354 UINTN
RunMenu(IN REFIT_MENU_SCREEN
*Screen
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
1356 INTN DefaultEntry
= -1;
1357 MENU_STYLE_FUNC Style
= TextMenuStyle
;
1359 if (AllowGraphicsMode
)
1360 Style
= GraphicsMenuStyle
;
1362 return RunGenericMenu(Screen
, Style
, &DefaultEntry
, ChosenEntry
);
1365 UINTN
RunMainMenu(REFIT_MENU_SCREEN
*Screen
, CHAR16
** DefaultSelection
, REFIT_MENU_ENTRY
**ChosenEntry
)
1367 MENU_STYLE_FUNC Style
= TextMenuStyle
;
1368 MENU_STYLE_FUNC MainStyle
= TextMenuStyle
;
1369 REFIT_MENU_ENTRY
*TempChosenEntry
;
1372 INTN DefaultEntryIndex
= -1;
1373 INTN DefaultSubmenuIndex
= -1;
1375 TileSizes
[0] = (GlobalConfig
.IconSizes
[ICON_SIZE_BIG
] * 9) / 8;
1376 TileSizes
[1] = (GlobalConfig
.IconSizes
[ICON_SIZE_SMALL
] * 4) / 3;
1378 if ((DefaultSelection
!= NULL
) && (*DefaultSelection
!= NULL
)) {
1379 // Find a menu entry that includes *DefaultSelection as a substring
1380 DefaultEntryIndex
= FindMenuShortcutEntry(Screen
, *DefaultSelection
);
1383 if (AllowGraphicsMode
) {
1384 Style
= GraphicsMenuStyle
;
1385 MainStyle
= MainMenuStyle
;
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
;
1397 MenuExit
= RunGenericMenu(Screen
, MainStyle
, &DefaultEntryIndex
, &TempChosenEntry
);
1398 Screen
->TimeoutSeconds
= 0;
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
)
1406 if (MenuExit
== MENU_EXIT_DETAILS
) {
1407 if (!EditOptions((LOADER_ENTRY
*) TempChosenEntry
))
1410 } else { // no sub-screen; ignore keypress
1413 } // Enter sub-screen
1417 *ChosenEntry
= TempChosenEntry
;
1418 if (DefaultSelection
) {
1419 MyFreePool(*DefaultSelection
);
1420 *DefaultSelection
= MenuTitle
;
1423 } /* UINTN RunMainMenu() */