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-2017 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];
397 EFI_STATUS TouchStatus
= EFI_NOT_READY
;
398 EFI_ABSOLUTE_POINTER_STATE TouchState
;
399 UINT32 TouchScreenPosX
;
400 UINT32 TouchScreenPosY
;
403 if (Screen
->TimeoutSeconds
> 0) {
405 TimeoutCountdown
= Screen
->TimeoutSeconds
* 10;
409 StyleFunc(Screen
, &State
, MENU_FUNCTION_INIT
, NULL
);
410 IdentifyRows(&State
, Screen
);
411 // override the starting selection with the default index, if any
412 if (*DefaultEntryIndex
>= 0 && *DefaultEntryIndex
<= State
.MaxIndex
) {
413 State
.CurrentSelection
= *DefaultEntryIndex
;
414 if (GlobalConfig
.ScreensaverTime
!= -1)
415 UpdateScroll(&State
, SCROLL_NONE
);
418 if (Screen
->TimeoutSeconds
== -1) {
419 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
420 if (Status
== EFI_NOT_READY
) {
421 MenuExit
= MENU_EXIT_TIMEOUT
;
423 KeyAsString
[0] = key
.UnicodeChar
;
425 ShortcutEntry
= FindMenuShortcutEntry(Screen
, KeyAsString
);
426 if (ShortcutEntry
>= 0) {
427 State
.CurrentSelection
= ShortcutEntry
;
428 MenuExit
= MENU_EXIT_ENTER
;
430 WaitForRelease
= TRUE
;
436 if (GlobalConfig
.ScreensaverTime
!= -1)
437 State
.PaintAll
= TRUE
;
441 if (State
.PaintAll
&& (GlobalConfig
.ScreensaverTime
!= -1)) {
442 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
443 State
.PaintAll
= FALSE
;
444 } else if (State
.PaintSelection
) {
445 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_SELECTION
, NULL
);
446 State
.PaintSelection
= FALSE
;
449 if (WaitForRelease
) {
450 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
451 if (Status
== EFI_SUCCESS
) {
452 // reset, because otherwise the buffer gets queued with keystrokes
453 refit_call2_wrapper(ST
->ConIn
->Reset
, ST
->ConIn
, FALSE
);
454 refit_call1_wrapper(BS
->Stall
, 100000);
456 WaitForRelease
= FALSE
;
457 refit_call2_wrapper(ST
->ConIn
->Reset
, ST
->ConIn
, TRUE
);
463 CurrentTime
= (TimeoutCountdown
+ 5) / 10;
464 if (CurrentTime
!= PreviousTime
) {
465 SPrint(TimeoutMessage
, 255, L
"%s in %d seconds", Screen
->TimeoutText
, CurrentTime
);
466 if (GlobalConfig
.ScreensaverTime
!= -1)
467 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, TimeoutMessage
);
468 PreviousTime
= CurrentTime
;
472 // read key press or touch event (and wait for them if applicable)
474 TouchStatus
= refit_call2_wrapper(TouchProtocol
->GetState
, TouchProtocol
, &TouchState
);
476 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
478 if(Status
== EFI_SUCCESS
) {
480 TimeSinceKeystroke
= 0;
481 } else if (TouchStatus
== EFI_SUCCESS
) {
482 if (StyleFunc
!= MainMenuStyle
) {
483 // prevent user from getting stuck on submenus
484 // (the only one currently reachable without a keyboard is the about screen)
485 MenuExit
= MENU_EXIT_ENTER
;
490 TimeSinceKeystroke
= 0;
492 EFI_EVENT WaitList
[3] = { ST
->ConIn
->WaitForKey
, NULL
, NULL
};
493 if (HaveTimeout
&& TimeoutCountdown
== 0) {
495 MenuExit
= MENU_EXIT_TIMEOUT
;
497 } else if (HaveTimeout
|| GlobalConfig
.ScreensaverTime
> 0) {
498 EFI_EVENT TimerEvent
;
499 UINTN ElapsCount
= 1;
501 Status
= refit_call5_wrapper(BS
->CreateEvent
, EVT_TIMER
, 0, NULL
, NULL
, &TimerEvent
);
502 if (EFI_ERROR(Status
)) {
503 refit_call1_wrapper(BS
->Stall
, 100000); // Pause for 100 ms
507 refit_call3_wrapper(BS
->SetTimer
, TimerEvent
, TimerRelative
, 10000000); // 1s Timeout
510 WaitList
[1] = TouchProtocol
->WaitForInput
;
511 WaitList
[2] = TimerEvent
;
512 Status
= refit_call3_wrapper(BS
->WaitForEvent
, 3, WaitList
, &Index
);
514 WaitList
[1] = TimerEvent
;
515 Status
= refit_call3_wrapper(BS
->WaitForEvent
, 2, WaitList
, &Index
);
518 refit_call1_wrapper(BS
->CloseEvent
, TimerEvent
);
519 if (EFI_ERROR(Status
))
520 refit_call1_wrapper(BS
->Stall
, 100000); // Pause for 100 ms
521 else if(Index
== 0 || (TouchEnabled
&& Index
== 1))
524 ElapsCount
= 10; // always counted as 1s to end of the timeout
526 TimeSinceKeystroke
+= ElapsCount
;
528 TimeoutCountdown
= TimeoutCountdown
<= ElapsCount
? 0 : TimeoutCountdown
- ElapsCount
;
529 } else if (GlobalConfig
.ScreensaverTime
> 0 &&
530 TimeSinceKeystroke
> (GlobalConfig
.ScreensaverTime
* 10))
533 State
.PaintAll
= TRUE
;
534 TimeSinceKeystroke
= 0;
538 WaitList
[1] = TouchProtocol
->WaitForInput
;
539 refit_call3_wrapper(BS
->WaitForEvent
, 2, WaitList
, &index
);
541 refit_call3_wrapper(BS
->WaitForEvent
, 1, WaitList
, &index
);
545 } // if/else !read keystroke
548 // the user pressed a key, cancel the timeout
549 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, L
"");
551 if (GlobalConfig
.ScreensaverTime
== -1) { // cancel start-with-blank-screen coding
552 GlobalConfig
.ScreensaverTime
= 0;
553 if (!GlobalConfig
.TextOnly
)
554 BltClearScreen(TRUE
);
558 if(!TouchActive
) { // react to key press
559 switch (key
.ScanCode
) {
561 UpdateScroll(&State
, SCROLL_LINE_UP
);
564 UpdateScroll(&State
, SCROLL_LINE_LEFT
);
567 UpdateScroll(&State
, SCROLL_LINE_DOWN
);
570 UpdateScroll(&State
, SCROLL_LINE_RIGHT
);
573 UpdateScroll(&State
, SCROLL_FIRST
);
576 UpdateScroll(&State
, SCROLL_LAST
);
579 UpdateScroll(&State
, SCROLL_PAGE_UP
);
582 UpdateScroll(&State
, SCROLL_PAGE_DOWN
);
585 MenuExit
= MENU_EXIT_ESCAPE
;
589 MenuExit
= MENU_EXIT_DETAILS
;
596 MenuExit
= MENU_EXIT_ESCAPE
;
599 switch (key
.UnicodeChar
) {
601 case CHAR_CARRIAGE_RETURN
:
603 MenuExit
= MENU_EXIT_ENTER
;
606 MenuExit
= MENU_EXIT_ESCAPE
;
610 MenuExit
= MENU_EXIT_DETAILS
;
613 KeyAsString
[0] = key
.UnicodeChar
;
615 ShortcutEntry
= FindMenuShortcutEntry(Screen
, KeyAsString
);
616 if (ShortcutEntry
>= 0) {
617 State
.CurrentSelection
= ShortcutEntry
;
618 MenuExit
= MENU_EXIT_ENTER
;
622 } else { //react to touch event
623 //the TouchProtocol min/max may not match the screen size
624 TouchScreenPosX
= ((UINT32
) TouchState
.CurrentX
* UGAWidth
) / (UINT32
) TouchProtocol
->Mode
->AbsoluteMaxX
;
625 TouchScreenPosY
= ((UINT32
) TouchState
.CurrentY
* UGAHeight
) / (UINT32
) TouchProtocol
->Mode
->AbsoluteMaxY
;
626 Item
= FindMainMenuItem(Screen
, &State
, TouchScreenPosX
, TouchScreenPosY
);
631 case TOUCH_LEFT_ARROW
:
632 UpdateScroll(&State
, SCROLL_PAGE_UP
);
634 case TOUCH_RIGHT_ARROW
:
635 UpdateScroll(&State
, SCROLL_PAGE_DOWN
);
638 State
.CurrentSelection
= Item
;
639 MenuExit
= MENU_EXIT_ENTER
;
645 StyleFunc(Screen
, &State
, MENU_FUNCTION_CLEANUP
, NULL
);
648 *ChosenEntry
= Screen
->Entries
[State
.CurrentSelection
];
649 *DefaultEntryIndex
= State
.CurrentSelection
;
651 } /* static UINTN RunGenericMenu() */
654 // text-mode generic style
657 // Show information lines in text mode.
658 static VOID
ShowTextInfoLines(IN REFIT_MENU_SCREEN
*Screen
) {
661 BeginTextScreen(Screen
->Title
);
662 if (Screen
->InfoLineCount
> 0) {
663 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
664 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
665 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 4 + i
);
666 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->InfoLines
[i
]);
669 } // VOID ShowTextInfoLines()
671 // Do most of the work for text-based menus....
672 static VOID
TextMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
675 UINTN MenuWidth
, ItemWidth
, MenuHeight
;
676 static UINTN MenuPosY
;
677 static CHAR16
**DisplayStrings
;
678 CHAR16 TimeoutMessage
[256];
680 State
->ScrollMode
= SCROLL_MODE_TEXT
;
683 case MENU_FUNCTION_INIT
:
686 if (Screen
->InfoLineCount
> 0)
687 MenuPosY
+= Screen
->InfoLineCount
+ 1;
688 MenuHeight
= ConHeight
- MenuPosY
- 3;
689 if (Screen
->TimeoutSeconds
> 0)
691 InitScroll(State
, Screen
->EntryCount
, MenuHeight
);
693 // determine width of the menu
694 MenuWidth
= 20; // minimum
695 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
696 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
697 if (MenuWidth
< ItemWidth
)
698 MenuWidth
= ItemWidth
;
701 if (MenuWidth
> ConWidth
- 3)
702 MenuWidth
= ConWidth
- 3;
704 // prepare strings for display
705 DisplayStrings
= AllocatePool(sizeof(CHAR16
*) * Screen
->EntryCount
);
706 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
707 // Note: Theoretically, SPrint() is a cleaner way to do this; but the
708 // description of the StrSize parameter to SPrint implies it's measured
709 // in characters, but in practice both TianoCore and GNU-EFI seem to
710 // use bytes instead, resulting in truncated displays. I could just
711 // double the size of the StrSize parameter, but that seems unsafe in
712 // case a future library change starts treating this as characters, so
713 // I'm doing it the hard way in this instance.
714 // TODO: Review the above and possibly change other uses of SPrint()
715 DisplayStrings
[i
] = AllocateZeroPool(2 * sizeof(CHAR16
));
716 DisplayStrings
[i
][0] = L
' ';
717 MergeStrings(&DisplayStrings
[i
], Screen
->Entries
[i
]->Title
, 0);
718 if (StrLen(DisplayStrings
[i
]) > MenuWidth
)
719 DisplayStrings
[i
][MenuWidth
- 1] = 0;
720 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
721 // TODO: account for double-width characters
726 case MENU_FUNCTION_CLEANUP
:
727 // release temporary memory
728 for (i
= 0; i
<= State
->MaxIndex
; i
++)
729 MyFreePool(DisplayStrings
[i
]);
730 MyFreePool(DisplayStrings
);
733 case MENU_FUNCTION_PAINT_ALL
:
734 // paint the whole screen (initially and after scrolling)
736 ShowTextInfoLines(Screen
);
737 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
738 if (i
>= State
->FirstVisible
&& i
<= State
->LastVisible
) {
739 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (i
- State
->FirstVisible
));
740 if (i
== State
->CurrentSelection
)
741 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
743 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
744 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[i
]);
747 // scrolling indicators
748 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_SCROLLARROW
);
749 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
);
750 if (State
->FirstVisible
> 0)
751 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowUp
);
753 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
754 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
+ State
->MaxVisible
);
755 if (State
->LastVisible
< State
->MaxIndex
)
756 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowDown
);
758 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
759 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HINTS
)) {
760 if (Screen
->Hint1
!= NULL
) {
761 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 2);
762 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->Hint1
);
764 if (Screen
->Hint2
!= NULL
) {
765 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 1);
766 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->Hint2
);
771 case MENU_FUNCTION_PAINT_SELECTION
:
772 // redraw selection cursor
773 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2,
774 MenuPosY
+ (State
->PreviousSelection
- State
->FirstVisible
));
775 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
776 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->PreviousSelection
]);
777 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2,
778 MenuPosY
+ (State
->CurrentSelection
- State
->FirstVisible
));
779 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
780 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->CurrentSelection
]);
783 case MENU_FUNCTION_PAINT_TIMEOUT
:
784 if (ParamText
[0] == 0) {
786 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
787 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 3);
788 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, BlankLine
+ 1);
790 // paint or update message
791 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
792 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, ConHeight
- 3);
793 SPrint(TimeoutMessage
, 255, L
"%s ", ParamText
);
794 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, TimeoutMessage
);
802 // graphical generic style
805 inline static UINTN
TextLineHeight(VOID
) {
806 return egGetFontHeight() + TEXT_YMARGIN
* 2;
807 } // UINTN TextLineHeight()
813 // Display text with a solid background (MenuBackgroundPixel or SelectionBackgroundPixel).
814 // Indents text by one character and placed TEXT_YMARGIN pixels down from the
815 // specified XPos and YPos locations.
816 static VOID
DrawText(IN CHAR16
*Text
, IN BOOLEAN Selected
, IN UINTN FieldWidth
, IN UINTN XPos
, IN UINTN YPos
)
818 EG_IMAGE
*TextBuffer
;
821 TextBuffer
= egCreateImage(FieldWidth
, TextLineHeight(), FALSE
);
823 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
824 Bg
= MenuBackgroundPixel
;
826 // draw selection bar background
827 egFillImageArea(TextBuffer
, 0, 0, FieldWidth
, TextBuffer
->Height
, &SelectionBackgroundPixel
);
828 Bg
= SelectionBackgroundPixel
;
832 egRenderText(Text
, TextBuffer
, egGetFontCellWidth(), TEXT_YMARGIN
, (Bg
.r
+ Bg
.g
+ Bg
.b
) / 3);
833 egDrawImageWithTransparency(TextBuffer
, NULL
, XPos
, YPos
, TextBuffer
->Width
, TextBuffer
->Height
);
834 // BltImage(TextBuffer, XPos, YPos);
837 // Finds the average brightness of the input Image.
838 // NOTE: Passing an Image that covers the whole screen can strain the
839 // capacity of a UINTN on a 32-bit system with a very large display.
840 // Using UINT64 instead is unworkable, since the code won't compile
841 // on a 32-bit system. As the intended use for this function is to handle
842 // a single text string's background, this shouldn't be a problem, but it
843 // may need addressing if it's applied more broadly....
844 static UINT8
AverageBrightness(EG_IMAGE
*Image
) {
848 if ((Image
!= NULL
) && ((Image
->Width
* Image
->Height
) != 0)) {
849 for (i
= 0; i
< (Image
->Width
* Image
->Height
); i
++) {
850 Sum
+= (Image
->PixelData
[i
].r
+ Image
->PixelData
[i
].g
+ Image
->PixelData
[i
].b
);
852 Sum
/= (Image
->Width
* Image
->Height
* 3);
855 } // UINT8 AverageBrightness()
857 // Display text against the screen's background image. Special case: If Text is NULL
858 // or 0-length, clear the line. Does NOT indent the text or reposition it relative
859 // to the specified XPos and YPos values.
860 static VOID
DrawTextWithTransparency(IN CHAR16
*Text
, IN UINTN XPos
, IN UINTN YPos
)
863 EG_IMAGE
*TextBuffer
= NULL
;
868 egMeasureText(Text
, &TextWidth
, NULL
);
869 if (TextWidth
== 0) {
870 TextWidth
= UGAWidth
;
874 TextBuffer
= egCropImage(GlobalConfig
.ScreenBackground
, XPos
, YPos
, TextWidth
, TextLineHeight());
875 if (TextBuffer
== NULL
)
879 egRenderText(Text
, TextBuffer
, 0, 0, AverageBrightness(TextBuffer
));
880 egDrawImageWithTransparency(TextBuffer
, NULL
, XPos
, YPos
, TextBuffer
->Width
, TextBuffer
->Height
);
881 egFreeImage(TextBuffer
);
884 // Compute the size & position of the window that will hold a subscreen's information.
885 static VOID
ComputeSubScreenWindowSize(REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*XPos
, UINTN
*YPos
,
886 UINTN
*Width
, UINTN
*Height
, UINTN
*LineWidth
) {
887 UINTN i
, ItemWidth
, HintTop
, BannerBottomEdge
, TitleWidth
;
888 UINTN FontCellWidth
= egGetFontCellWidth();
889 UINTN FontCellHeight
= egGetFontHeight();
893 TitleWidth
= egComputeTextWidth(Screen
->Title
);
895 for (i
= 0; i
< Screen
->InfoLineCount
; i
++) {
896 ItemWidth
= StrLen(Screen
->InfoLines
[i
]);
897 if (*Width
< ItemWidth
) {
902 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
903 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
904 if (*Width
< ItemWidth
) {
909 *Width
= (*Width
+ 2) * FontCellWidth
;
911 if (Screen
->TitleImage
)
912 *Width
+= (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
* 2 + FontCellWidth
);
914 *Width
+= FontCellWidth
;
916 if (*Width
< TitleWidth
)
917 *Width
= TitleWidth
+ 2 * FontCellWidth
;
919 // Keep it within the bounds of the screen, or 2/3 of the screen's width
920 // for screens over 800 pixels wide
921 if (*Width
> UGAWidth
)
924 *XPos
= (UGAWidth
- *Width
) / 2;
926 HintTop
= UGAHeight
- (FontCellHeight
* 3); // top of hint text
927 *Height
*= TextLineHeight();
928 if (Screen
->TitleImage
&& (*Height
< (Screen
->TitleImage
->Height
+ TextLineHeight() * 4)))
929 *Height
= Screen
->TitleImage
->Height
+ TextLineHeight() * 4;
931 if (GlobalConfig
.BannerBottomEdge
>= HintTop
) { // probably a full-screen image; treat it as an empty banner
932 BannerBottomEdge
= 0;
934 BannerBottomEdge
= GlobalConfig
.BannerBottomEdge
;
936 if (*Height
> (HintTop
- BannerBottomEdge
- FontCellHeight
* 2)) {
937 BannerBottomEdge
= 0;
939 if (*Height
> (HintTop
- BannerBottomEdge
- FontCellHeight
* 2)) {
940 // TODO: Implement scrolling in text screen.
941 *Height
= (HintTop
- BannerBottomEdge
- FontCellHeight
* 2);
944 *YPos
= ((UGAHeight
- *Height
) / 2);
945 if (*YPos
< BannerBottomEdge
)
946 *YPos
= BannerBottomEdge
+ FontCellHeight
+ (HintTop
- BannerBottomEdge
- *Height
) / 2;
947 } // VOID ComputeSubScreenWindowSize()
949 // Displays sub-menus
950 static VOID
GraphicsMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
954 static UINTN LineWidth
, MenuWidth
, MenuHeight
, EntriesPosX
, TitlePosX
, EntriesPosY
, TimeoutPosY
, CharWidth
;
956 EG_PIXEL
*BackgroundPixel
= &(GlobalConfig
.ScreenBackground
->PixelData
[0]);
958 CharWidth
= egGetFontCellWidth();
959 State
->ScrollMode
= SCROLL_MODE_TEXT
;
962 case MENU_FUNCTION_INIT
:
963 InitScroll(State
, Screen
->EntryCount
, 0);
964 ComputeSubScreenWindowSize(Screen
, State
, &EntriesPosX
, &EntriesPosY
, &MenuWidth
, &MenuHeight
, &LineWidth
);
965 TimeoutPosY
= EntriesPosY
+ (Screen
->EntryCount
+ 1) * TextLineHeight();
968 SwitchToGraphicsAndClear();
969 Window
= egCreateFilledImage(MenuWidth
, MenuHeight
, FALSE
, BackgroundPixel
);
970 egDrawImage(Window
, EntriesPosX
, EntriesPosY
);
971 ItemWidth
= egComputeTextWidth(Screen
->Title
);
972 if (MenuWidth
> ItemWidth
) {
973 TitlePosX
= EntriesPosX
+ (MenuWidth
- ItemWidth
) / 2 - CharWidth
;
975 TitlePosX
= EntriesPosX
;
977 i
= MenuWidth
/ CharWidth
- 2;
979 Screen
->Title
[i
] = 0;
984 case MENU_FUNCTION_CLEANUP
:
988 case MENU_FUNCTION_PAINT_ALL
:
989 ComputeSubScreenWindowSize(Screen
, State
, &EntriesPosX
, &EntriesPosY
, &MenuWidth
, &MenuHeight
, &LineWidth
);
990 DrawText(Screen
->Title
, FALSE
, (StrLen(Screen
->Title
) + 2) * CharWidth
, TitlePosX
, EntriesPosY
+= TextLineHeight());
991 if (Screen
->TitleImage
) {
992 BltImageAlpha(Screen
->TitleImage
, EntriesPosX
+ TITLEICON_SPACING
, EntriesPosY
+ TextLineHeight() * 2,
994 EntriesPosX
+= (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
* 2);
996 EntriesPosY
+= (TextLineHeight() * 2);
997 if (Screen
->InfoLineCount
> 0) {
998 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
999 DrawText(Screen
->InfoLines
[i
], FALSE
, LineWidth
, EntriesPosX
, EntriesPosY
);
1000 EntriesPosY
+= TextLineHeight();
1002 EntriesPosY
+= TextLineHeight(); // also add a blank line
1005 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
1006 DrawText(Screen
->Entries
[i
]->Title
, (i
== State
->CurrentSelection
), LineWidth
, EntriesPosX
,
1007 EntriesPosY
+ i
* TextLineHeight());
1009 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HINTS
)) {
1010 if ((Screen
->Hint1
!= NULL
) && (StrLen(Screen
->Hint1
) > 0))
1011 DrawTextWithTransparency(Screen
->Hint1
, (UGAWidth
- egComputeTextWidth(Screen
->Hint1
)) / 2,
1012 UGAHeight
- (egGetFontHeight() * 3));
1013 if ((Screen
->Hint2
!= NULL
) && (StrLen(Screen
->Hint2
) > 0))
1014 DrawTextWithTransparency(Screen
->Hint2
, (UGAWidth
- egComputeTextWidth(Screen
->Hint2
)) / 2,
1015 UGAHeight
- (egGetFontHeight() * 2));
1019 case MENU_FUNCTION_PAINT_SELECTION
:
1020 // redraw selection cursor
1021 DrawText(Screen
->Entries
[State
->PreviousSelection
]->Title
, FALSE
, LineWidth
,
1022 EntriesPosX
, EntriesPosY
+ State
->PreviousSelection
* TextLineHeight());
1023 DrawText(Screen
->Entries
[State
->CurrentSelection
]->Title
, TRUE
, LineWidth
,
1024 EntriesPosX
, EntriesPosY
+ State
->CurrentSelection
* TextLineHeight());
1027 case MENU_FUNCTION_PAINT_TIMEOUT
:
1028 DrawText(ParamText
, FALSE
, LineWidth
, EntriesPosX
, TimeoutPosY
);
1032 } // static VOID GraphicsMenuStyle()
1035 // graphical main menu style
1038 static VOID
DrawMainMenuEntry(REFIT_MENU_ENTRY
*Entry
, BOOLEAN selected
, UINTN XPos
, UINTN YPos
)
1040 EG_IMAGE
*Background
;
1042 // don't draw selection image if using touch
1043 if (selected
&& !TouchActive
) {
1044 Background
= egCropImage(GlobalConfig
.ScreenBackground
, XPos
, YPos
,
1045 SelectionImages
[Entry
->Row
]->Width
, SelectionImages
[Entry
->Row
]->Height
);
1046 egComposeImage(Background
, SelectionImages
[Entry
->Row
], 0, 0);
1047 BltImageCompositeBadge(Background
, Entry
->Image
, Entry
->BadgeImage
, XPos
, YPos
);
1048 } else { // Image not selected; copy background
1049 egDrawImageWithTransparency(Entry
->Image
, Entry
->BadgeImage
, XPos
, YPos
,
1050 SelectionImages
[Entry
->Row
]->Width
, SelectionImages
[Entry
->Row
]->Height
);
1052 } // VOID DrawMainMenuEntry()
1054 static VOID
PaintAll(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
1055 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
1058 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0)
1059 AdjustScrollState(State
);
1060 for (i
= State
->FirstVisible
; i
<= State
->MaxIndex
; i
++) {
1061 if (Screen
->Entries
[i
]->Row
== 0) {
1062 if (i
<= State
->LastVisible
) {
1063 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
,
1064 itemPosX
[i
- State
->FirstVisible
], row0PosY
);
1067 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
, itemPosX
[i
], row1PosY
);
1070 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
)) {
1071 DrawTextWithTransparency(L
"", 0, textPosY
);
1072 DrawTextWithTransparency(Screen
->Entries
[State
->CurrentSelection
]->Title
,
1073 (UGAWidth
- egComputeTextWidth(Screen
->Entries
[State
->CurrentSelection
]->Title
)) >> 1,
1077 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HINTS
)) {
1078 DrawTextWithTransparency(Screen
->Hint1
, (UGAWidth
- egComputeTextWidth(Screen
->Hint1
)) / 2,
1079 UGAHeight
- (egGetFontHeight() * 3));
1080 DrawTextWithTransparency(Screen
->Hint2
, (UGAWidth
- egComputeTextWidth(Screen
->Hint2
)) / 2,
1081 UGAHeight
- (egGetFontHeight() * 2));
1083 } // static VOID PaintAll()
1085 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
1086 static VOID
PaintSelection(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
1087 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
1088 UINTN XSelectPrev
, XSelectCur
, YPosPrev
, YPosCur
;
1090 if (((State
->CurrentSelection
<= State
->LastVisible
) && (State
->CurrentSelection
>= State
->FirstVisible
)) ||
1091 (State
->CurrentSelection
>= State
->InitialRow1
) ) {
1092 if (Screen
->Entries
[State
->PreviousSelection
]->Row
== 0) {
1093 XSelectPrev
= State
->PreviousSelection
- State
->FirstVisible
;
1094 YPosPrev
= row0PosY
;
1096 XSelectPrev
= State
->PreviousSelection
;
1097 YPosPrev
= row1PosY
;
1099 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0) {
1100 XSelectCur
= State
->CurrentSelection
- State
->FirstVisible
;
1103 XSelectCur
= State
->CurrentSelection
;
1106 DrawMainMenuEntry(Screen
->Entries
[State
->PreviousSelection
], FALSE
, itemPosX
[XSelectPrev
], YPosPrev
);
1107 DrawMainMenuEntry(Screen
->Entries
[State
->CurrentSelection
], TRUE
, itemPosX
[XSelectCur
], YPosCur
);
1108 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
)) {
1109 DrawTextWithTransparency(L
"", 0, textPosY
);
1110 DrawTextWithTransparency(Screen
->Entries
[State
->CurrentSelection
]->Title
,
1111 (UGAWidth
- egComputeTextWidth(Screen
->Entries
[State
->CurrentSelection
]->Title
)) >> 1,
1114 } else { // Current selection not visible; must redraw the menu....
1115 MainMenuStyle(Screen
, State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
1117 } // static VOID MoveSelection(VOID)
1119 // Display a 48x48 icon at the specified location. Uses the image specified by
1120 // ExternalFilename if it's available, or BuiltInImage if it's not. The
1121 // Y position is specified as the center value, and so is adjusted by half
1122 // the icon's height. The X position is set along the icon's left
1123 // edge if Alignment == ALIGN_LEFT, and along the right edge if
1124 // Alignment == ALIGN_RIGHT
1125 static VOID
PaintIcon(IN EG_EMBEDDED_IMAGE
*BuiltInIcon
, IN CHAR16
*ExternalFilename
, UINTN PosX
, UINTN PosY
, UINTN Alignment
) {
1126 EG_IMAGE
*Icon
= NULL
;
1128 Icon
= egFindIcon(ExternalFilename
, GlobalConfig
.IconSizes
[ICON_SIZE_SMALL
]);
1130 Icon
= egPrepareEmbeddedImage(BuiltInIcon
, TRUE
);
1132 if (Alignment
== ALIGN_RIGHT
)
1133 PosX
-= Icon
->Width
;
1134 egDrawImageWithTransparency(Icon
, NULL
, PosX
, PosY
- (Icon
->Height
/ 2), Icon
->Width
, Icon
->Height
);
1138 UINTN
ComputeRow0PosY(VOID
) {
1139 return ((UGAHeight
/ 2) - TileSizes
[0] / 2);
1140 } // UINTN ComputeRow0PosY()
1142 // Display (or erase) the arrow icons to the left and right of an icon's row,
1144 static VOID
PaintArrows(SCROLL_STATE
*State
, UINTN PosX
, UINTN PosY
, UINTN row0Loaders
) {
1145 EG_IMAGE
*TempImage
;
1146 UINTN Width
, Height
, RightX
, AdjPosY
;
1148 // NOTE: Assume that left and right arrows are of the same size....
1149 Width
= egemb_arrow_left
.Width
;
1150 Height
= egemb_arrow_left
.Height
;
1151 RightX
= (UGAWidth
+ (TileSizes
[0] + TILE_XSPACING
) * State
->MaxVisible
) / 2 + TILE_XSPACING
;
1152 AdjPosY
= PosY
- (Height
/ 2);
1154 // For PaintIcon() calls, the starting Y position is moved to the midpoint
1155 // of the surrounding row; PaintIcon() adjusts this back up by half the
1156 // icon's height to properly center it.
1157 if ((State
->FirstVisible
> 0) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
1158 PaintIcon(&egemb_arrow_left
, L
"arrow_left", PosX
, PosY
, ALIGN_RIGHT
);
1160 TempImage
= egCropImage(GlobalConfig
.ScreenBackground
, PosX
- Width
, AdjPosY
, Width
, Height
);
1161 BltImage(TempImage
, PosX
- Width
, AdjPosY
);
1162 egFreeImage(TempImage
);
1165 if ((State
->LastVisible
< (row0Loaders
- 1)) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
1166 PaintIcon(&egemb_arrow_right
, L
"arrow_right", RightX
, PosY
, ALIGN_LEFT
);
1168 TempImage
= egCropImage(GlobalConfig
.ScreenBackground
, RightX
, AdjPosY
, Width
, Height
);
1169 BltImage(TempImage
, RightX
, AdjPosY
);
1170 egFreeImage(TempImage
);
1172 } // VOID PaintArrows()
1174 // Display main menu in graphics mode
1175 VOID
MainMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
1178 static UINTN row0PosX
, row0PosXRunning
, row1PosY
, row0Loaders
;
1179 UINTN row0Count
, row1Count
, row1PosX
, row1PosXRunning
;
1180 static UINTN
*itemPosX
;
1181 static UINTN row0PosY
, textPosY
;
1183 State
->ScrollMode
= SCROLL_MODE_ICONS
;
1186 case MENU_FUNCTION_INIT
:
1187 InitScroll(State
, Screen
->EntryCount
, GlobalConfig
.MaxTags
);
1193 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
1194 if (Screen
->Entries
[i
]->Row
== 1) {
1198 if (row0Count
< State
->MaxVisible
)
1202 row0PosX
= (UGAWidth
+ TILE_XSPACING
- (TileSizes
[0] + TILE_XSPACING
) * row0Count
) >> 1;
1203 row0PosY
= ComputeRow0PosY();
1204 row1PosX
= (UGAWidth
+ TILE_XSPACING
- (TileSizes
[1] + TILE_XSPACING
) * row1Count
) >> 1;
1205 row1PosY
= row0PosY
+ TileSizes
[0] + TILE_YSPACING
;
1207 textPosY
= row1PosY
+ TileSizes
[1] + TILE_YSPACING
;
1209 textPosY
= row1PosY
;
1211 itemPosX
= AllocatePool(sizeof(UINTN
) * Screen
->EntryCount
);
1212 row0PosXRunning
= row0PosX
;
1213 row1PosXRunning
= row1PosX
;
1214 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
1215 if (Screen
->Entries
[i
]->Row
== 0) {
1216 itemPosX
[i
] = row0PosXRunning
;
1217 row0PosXRunning
+= TileSizes
[0] + TILE_XSPACING
;
1219 itemPosX
[i
] = row1PosXRunning
;
1220 row1PosXRunning
+= TileSizes
[1] + TILE_XSPACING
;
1225 SwitchToGraphicsAndClear();
1228 case MENU_FUNCTION_CLEANUP
:
1229 MyFreePool(itemPosX
);
1232 case MENU_FUNCTION_PAINT_ALL
:
1233 PaintAll(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
1234 // For PaintArrows(), the starting Y position is moved to the midpoint
1235 // of the surrounding row; PaintIcon() adjusts this back up by half the
1236 // icon's height to properly center it.
1237 PaintArrows(State
, row0PosX
- TILE_XSPACING
, row0PosY
+ (TileSizes
[0] / 2), row0Loaders
);
1240 case MENU_FUNCTION_PAINT_SELECTION
:
1241 PaintSelection(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
1244 case MENU_FUNCTION_PAINT_TIMEOUT
:
1245 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
)) {
1246 DrawTextWithTransparency(L
"", 0, textPosY
+ TextLineHeight());
1247 DrawTextWithTransparency(ParamText
, (UGAWidth
- egComputeTextWidth(ParamText
)) >> 1, textPosY
+ TextLineHeight());
1252 } // VOID MainMenuStyle()
1254 // Determines the index of the main menu item at the given coordinates.
1255 UINTN
FindMainMenuItem(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINT64 PosX
, IN UINT64 PosY
)
1258 static UINTN row0PosX
, row0PosXRunning
, row1PosY
, row0Loaders
;
1259 UINTN row0Count
, row1Count
, row1PosX
, row1PosXRunning
;
1260 static UINTN
*itemPosX
;
1261 static UINTN row0PosY
;
1267 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
1268 if (Screen
->Entries
[i
]->Row
== 1) {
1272 if (row0Count
< State
->MaxVisible
)
1276 row0PosX
= (UGAWidth
+ TILE_XSPACING
- (TileSizes
[0] + TILE_XSPACING
) * row0Count
) >> 1;
1277 row0PosY
= ComputeRow0PosY();
1278 row1PosX
= (UGAWidth
+ TILE_XSPACING
- (TileSizes
[1] + TILE_XSPACING
) * row1Count
) >> 1;
1279 row1PosY
= row0PosY
+ TileSizes
[0] + TILE_YSPACING
;
1281 if(PosY
>= row0PosY
&& PosY
<= row0PosY
+ TileSizes
[0]) {
1283 if(PosX
<= row0PosX
) {
1284 return TOUCH_LEFT_ARROW
;
1286 else if(PosX
>= (UGAWidth
- row0PosX
)) {
1287 return TOUCH_RIGHT_ARROW
;
1289 } else if(PosY
>= row1PosY
&& PosY
<= row1PosY
+ TileSizes
[1]) {
1291 } else { // Y coordinate is outside of either row
1292 return TOUCH_NO_ITEM
;
1295 UINTN ItemIndex
= TOUCH_NO_ITEM
;
1297 itemPosX
= AllocatePool(sizeof(UINTN
) * Screen
->EntryCount
);
1298 row0PosXRunning
= row0PosX
;
1299 row1PosXRunning
= row1PosX
;
1300 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
1301 if (Screen
->Entries
[i
]->Row
== 0) {
1302 itemPosX
[i
] = row0PosXRunning
;
1303 row0PosXRunning
+= TileSizes
[0] + TILE_XSPACING
;
1305 itemPosX
[i
] = row1PosXRunning
;
1306 row1PosXRunning
+= TileSizes
[1] + TILE_XSPACING
;
1310 for (i
= State
->FirstVisible
; i
<= State
->MaxIndex
; i
++) {
1311 if (Screen
->Entries
[i
]->Row
== 0 && itemRow
== 0) {
1312 if (i
<= State
->LastVisible
) {
1313 if(PosX
>= itemPosX
[i
- State
->FirstVisible
] && PosX
<= itemPosX
[i
- State
->FirstVisible
] + TileSizes
[0]) {
1318 } else if (Screen
->Entries
[i
]->Row
== 1 && itemRow
== 1) {
1319 if(PosX
>= itemPosX
[i
] && PosX
<= itemPosX
[i
] + TileSizes
[1]) {
1326 MyFreePool(itemPosX
);
1329 } // VOID FindMainMenuItem()
1331 // Enable the user to edit boot loader options.
1332 // Returns TRUE if the user exited with edited options; FALSE if the user
1333 // pressed Esc to terminate the edit.
1334 static BOOLEAN
EditOptions(LOADER_ENTRY
*MenuEntry
) {
1336 CHAR16
*EditedOptions
;
1337 BOOLEAN retval
= FALSE
;
1339 if (GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_EDITOR
) {
1343 refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &x_max
, &y_max
);
1345 if (!GlobalConfig
.TextOnly
)
1348 if (line_edit(MenuEntry
->LoadOptions
, &EditedOptions
, x_max
)) {
1349 MyFreePool(MenuEntry
->LoadOptions
);
1350 MenuEntry
->LoadOptions
= EditedOptions
;
1353 if (!GlobalConfig
.TextOnly
)
1356 } // VOID EditOptions()
1359 // user-callable dispatcher functions
1362 UINTN
RunMenu(IN REFIT_MENU_SCREEN
*Screen
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
1364 INTN DefaultEntry
= -1;
1365 MENU_STYLE_FUNC Style
= TextMenuStyle
;
1367 if (AllowGraphicsMode
)
1368 Style
= GraphicsMenuStyle
;
1370 return RunGenericMenu(Screen
, Style
, &DefaultEntry
, ChosenEntry
);
1373 UINTN
RunMainMenu(REFIT_MENU_SCREEN
*Screen
, CHAR16
** DefaultSelection
, REFIT_MENU_ENTRY
**ChosenEntry
)
1375 MENU_STYLE_FUNC Style
= TextMenuStyle
;
1376 MENU_STYLE_FUNC MainStyle
= TextMenuStyle
;
1377 REFIT_MENU_ENTRY
*TempChosenEntry
;
1380 INTN DefaultEntryIndex
= -1;
1381 INTN DefaultSubmenuIndex
= -1;
1383 TileSizes
[0] = (GlobalConfig
.IconSizes
[ICON_SIZE_BIG
] * 9) / 8;
1384 TileSizes
[1] = (GlobalConfig
.IconSizes
[ICON_SIZE_SMALL
] * 4) / 3;
1386 if ((DefaultSelection
!= NULL
) && (*DefaultSelection
!= NULL
)) {
1387 // Find a menu entry that includes *DefaultSelection as a substring
1388 DefaultEntryIndex
= FindMenuShortcutEntry(Screen
, *DefaultSelection
);
1391 if (AllowGraphicsMode
) {
1392 Style
= GraphicsMenuStyle
;
1393 MainStyle
= MainMenuStyle
;
1394 if (GlobalConfig
.EnableTouch
) {
1395 // Check for touch availability
1396 EFI_STATUS status
= refit_call3_wrapper(BS
->LocateProtocol
, &TouchGuid
, NULL
, (VOID
**) &TouchProtocol
);
1397 if (status
== EFI_SUCCESS
) {
1398 TouchEnabled
= TouchActive
= TRUE
;
1404 MenuExit
= RunGenericMenu(Screen
, MainStyle
, &DefaultEntryIndex
, &TempChosenEntry
);
1405 Screen
->TimeoutSeconds
= 0;
1407 MenuTitle
= StrDuplicate(TempChosenEntry
->Title
);
1408 if (MenuExit
== MENU_EXIT_DETAILS
) {
1409 if (TempChosenEntry
->SubScreen
!= NULL
) {
1410 MenuExit
= RunGenericMenu(TempChosenEntry
->SubScreen
, Style
, &DefaultSubmenuIndex
, &TempChosenEntry
);
1411 if (MenuExit
== MENU_EXIT_ESCAPE
|| TempChosenEntry
->Tag
== TAG_RETURN
)
1413 if (MenuExit
== MENU_EXIT_DETAILS
) {
1414 if (!EditOptions((LOADER_ENTRY
*) TempChosenEntry
))
1417 } else { // no sub-screen; ignore keypress
1420 } // Enter sub-screen
1424 *ChosenEntry
= TempChosenEntry
;
1425 if (DefaultSelection
) {
1426 MyFreePool(*DefaultSelection
);
1427 *DefaultSelection
= MenuTitle
;
1430 } /* UINTN RunMainMenu() */