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 Roderick W. Smith
39 * Modifications distributed under the terms of the GNU General Public
40 * License (GPL) version 3 (GPLv3), a copy of which must be distributed
41 * with this source code or binaries made from it.
52 #include "../include/refit_call_wrapper.h"
54 #include "../include/egemb_back_selected_small.h"
55 #include "../include/egemb_arrow_left.h"
56 #include "../include/egemb_arrow_right.h"
58 // other menu definitions
60 #define MENU_FUNCTION_INIT (0)
61 #define MENU_FUNCTION_CLEANUP (1)
62 #define MENU_FUNCTION_PAINT_ALL (2)
63 #define MENU_FUNCTION_PAINT_SELECTION (3)
64 #define MENU_FUNCTION_PAINT_TIMEOUT (4)
65 #define MENU_FUNCTION_PAINT_HINTS (5)
67 typedef VOID (*MENU_STYLE_FUNC
)(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
);
69 static CHAR16 ArrowUp
[2] = { ARROW_UP
, 0 };
70 static CHAR16 ArrowDown
[2] = { ARROW_DOWN
, 0 };
72 // Text and icon spacing constants....
73 #define TEXT_YMARGIN (2)
74 #define TEXT_XMARGIN (8)
75 #define TEXT_LINE_HEIGHT (FONT_CELL_HEIGHT + TEXT_YMARGIN * 2)
76 #define TITLEICON_SPACING (16)
78 #define ROW0_TILESIZE (144)
79 #define ROW1_TILESIZE (64)
80 #define TILE_XSPACING (8)
81 #define TILE_YSPACING (16)
83 // Alignment values for PaintIcon()
87 static EG_IMAGE
*SelectionImages
[4] = { NULL
, NULL
, NULL
, NULL
};
88 static EG_PIXEL SelectionBackgroundPixel
= { 0xff, 0xff, 0xff, 0 };
89 static EG_IMAGE
*TextBuffer
= NULL
;
92 // Graphics helper functions
95 static VOID
InitSelection(VOID
)
97 UINTN x
, y
, src_x
, src_y
;
98 EG_PIXEL
*DestPtr
, *SrcPtr
;
100 if (!AllowGraphicsMode
)
102 if (SelectionImages
[0] != NULL
)
105 // load small selection image
106 if (GlobalConfig
.SelectionSmallFileName
!= NULL
) {
107 SelectionImages
[2] = egLoadImage(SelfDir
, GlobalConfig
.SelectionSmallFileName
, FALSE
);
109 if (SelectionImages
[2] == NULL
)
110 SelectionImages
[2] = egPrepareEmbeddedImage(&egemb_back_selected_small
, FALSE
);
111 SelectionImages
[2] = egEnsureImageSize(SelectionImages
[2],
112 ROW1_TILESIZE
, ROW1_TILESIZE
, &MenuBackgroundPixel
);
113 if (SelectionImages
[2] == NULL
)
116 // load big selection image
117 if (GlobalConfig
.SelectionBigFileName
!= NULL
) {
118 SelectionImages
[0] = egLoadImage(SelfDir
, GlobalConfig
.SelectionBigFileName
, FALSE
);
119 SelectionImages
[0] = egEnsureImageSize(SelectionImages
[0],
120 ROW0_TILESIZE
, ROW0_TILESIZE
, &MenuBackgroundPixel
);
122 if (SelectionImages
[0] == NULL
) {
123 // calculate big selection image from small one
125 SelectionImages
[0] = egCreateImage(ROW0_TILESIZE
, ROW0_TILESIZE
, FALSE
);
126 if (SelectionImages
[0] == NULL
) {
127 egFreeImage(SelectionImages
[2]);
128 SelectionImages
[2] = NULL
;
132 DestPtr
= SelectionImages
[0]->PixelData
;
133 SrcPtr
= SelectionImages
[2]->PixelData
;
134 for (y
= 0; y
< ROW0_TILESIZE
; y
++) {
135 if (y
< (ROW1_TILESIZE
>> 1))
137 else if (y
< (ROW0_TILESIZE
- (ROW1_TILESIZE
>> 1)))
138 src_y
= (ROW1_TILESIZE
>> 1);
140 src_y
= y
- (ROW0_TILESIZE
- ROW1_TILESIZE
);
142 for (x
= 0; x
< ROW0_TILESIZE
; x
++) {
143 if (x
< (ROW1_TILESIZE
>> 1))
145 else if (x
< (ROW0_TILESIZE
- (ROW1_TILESIZE
>> 1)))
146 src_x
= (ROW1_TILESIZE
>> 1);
148 src_x
= x
- (ROW0_TILESIZE
- ROW1_TILESIZE
);
150 *DestPtr
++ = SrcPtr
[src_y
* ROW1_TILESIZE
+ src_x
];
155 // non-selected background images
156 SelectionImages
[1] = egCreateFilledImage(ROW0_TILESIZE
, ROW0_TILESIZE
, FALSE
, &MenuBackgroundPixel
);
157 SelectionImages
[3] = egCreateFilledImage(ROW1_TILESIZE
, ROW1_TILESIZE
, FALSE
, &MenuBackgroundPixel
);
161 // Scrolling functions
164 static VOID
InitScroll(OUT SCROLL_STATE
*State
, IN UINTN ItemCount
, IN UINTN VisibleSpace
)
166 State
->PreviousSelection
= State
->CurrentSelection
= 0;
167 State
->MaxIndex
= (INTN
)ItemCount
- 1;
168 State
->FirstVisible
= 0;
169 if (AllowGraphicsMode
) {
170 State
->MaxVisible
= UGAWidth
/ (ROW0_TILESIZE
+ TILE_XSPACING
) - 1;
172 State
->MaxVisible
= ConHeight
- 4;
173 if ((VisibleSpace
> 0) && (VisibleSpace
< State
->MaxVisible
))
174 State
->MaxVisible
= (INTN
)VisibleSpace
;
175 State
->PaintAll
= TRUE
;
176 State
->PaintSelection
= FALSE
;
178 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
181 // Adjust variables relating to the scrolling of tags, for when a selected icon isn't
182 // visible given the current scrolling condition....
183 static VOID
AdjustScrollState(IN SCROLL_STATE
*State
) {
184 if (State
->CurrentSelection
> State
->LastVisible
) {
185 State
->LastVisible
= State
->CurrentSelection
;
186 State
->FirstVisible
= 1 + State
->CurrentSelection
- State
->MaxVisible
;
187 if (State
->FirstVisible
< 0) // shouldn't happen, but just in case....
188 State
->FirstVisible
= 0;
189 State
->PaintAll
= TRUE
;
191 if (State
->CurrentSelection
< State
->FirstVisible
) {
192 State
->FirstVisible
= State
->CurrentSelection
;
193 State
->LastVisible
= State
->CurrentSelection
+ State
->MaxVisible
- 1;
194 State
->PaintAll
= TRUE
;
196 } // static VOID AdjustScrollState
198 static VOID
UpdateScroll(IN OUT SCROLL_STATE
*State
, IN UINTN Movement
)
200 State
->PreviousSelection
= State
->CurrentSelection
;
203 case SCROLL_LINE_LEFT
:
204 if (State
->CurrentSelection
> 0) {
205 State
->CurrentSelection
--;
209 case SCROLL_LINE_RIGHT
:
210 if (State
->CurrentSelection
< State
->MaxIndex
) {
211 State
->CurrentSelection
++;
216 if (State
->ScrollMode
== SCROLL_MODE_ICONS
) {
217 if (State
->CurrentSelection
>= State
->InitialRow1
) {
218 if (State
->MaxIndex
> State
->InitialRow1
) { // avoid division by 0!
219 State
->CurrentSelection
= State
->FirstVisible
+ (State
->LastVisible
- State
->FirstVisible
) *
220 (State
->CurrentSelection
- State
->InitialRow1
) /
221 (State
->MaxIndex
- State
->InitialRow1
);
223 State
->CurrentSelection
= State
->FirstVisible
;
225 } // if in second row
227 if (State
->CurrentSelection
> 0)
228 State
->CurrentSelection
--;
232 case SCROLL_LINE_DOWN
:
233 if (State
->ScrollMode
== SCROLL_MODE_ICONS
) {
234 if (State
->CurrentSelection
<= State
->FinalRow0
) {
235 if (State
->LastVisible
> State
->FirstVisible
) { // avoid division by 0!
236 State
->CurrentSelection
= State
->InitialRow1
+ (State
->MaxIndex
- State
->InitialRow1
) *
237 (State
->CurrentSelection
- State
->FirstVisible
) /
238 (State
->LastVisible
- State
->FirstVisible
);
240 State
->CurrentSelection
= State
->InitialRow1
;
244 if (State
->CurrentSelection
< State
->MaxIndex
)
245 State
->CurrentSelection
++;
250 if (State
->CurrentSelection
<= State
->FinalRow0
)
251 State
->CurrentSelection
-= State
->MaxVisible
;
252 else if (State
->CurrentSelection
== State
->InitialRow1
)
253 State
->CurrentSelection
= State
->FinalRow0
;
255 State
->CurrentSelection
= State
->InitialRow1
;
256 if (State
->CurrentSelection
< 0)
257 State
->CurrentSelection
= 0;
261 if (State
->CurrentSelection
> 0) {
262 State
->PaintAll
= TRUE
;
263 State
->CurrentSelection
= 0;
267 case SCROLL_PAGE_DOWN
:
268 if (State
->CurrentSelection
< State
->FinalRow0
) {
269 State
->CurrentSelection
+= State
->MaxVisible
;
270 if (State
->CurrentSelection
> State
->FinalRow0
)
271 State
->CurrentSelection
= State
->FinalRow0
;
272 } else if (State
->CurrentSelection
== State
->FinalRow0
) {
273 State
->CurrentSelection
++;
275 State
->CurrentSelection
= State
->MaxIndex
;
277 if (State
->CurrentSelection
> State
->MaxIndex
)
278 State
->CurrentSelection
= State
->MaxIndex
;
282 if (State
->CurrentSelection
< State
->MaxIndex
) {
283 State
->PaintAll
= TRUE
;
284 State
->CurrentSelection
= State
->MaxIndex
;
292 if (State
->ScrollMode
== SCROLL_MODE_TEXT
)
293 AdjustScrollState(State
);
295 if (!State
->PaintAll
&& State
->CurrentSelection
!= State
->PreviousSelection
)
296 State
->PaintSelection
= TRUE
;
297 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
298 } // static VOID UpdateScroll()
301 // menu helper functions
304 VOID
AddMenuInfoLine(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*InfoLine
)
306 AddListElement((VOID
***) &(Screen
->InfoLines
), &(Screen
->InfoLineCount
), InfoLine
);
309 VOID
AddMenuEntry(IN REFIT_MENU_SCREEN
*Screen
, IN REFIT_MENU_ENTRY
*Entry
)
311 AddListElement((VOID
***) &(Screen
->Entries
), &(Screen
->EntryCount
), Entry
);
315 static INTN
FindMenuShortcutEntry(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*Shortcut
)
319 if (Shortcut
== NULL
)
322 if (StrLen(Shortcut
) == 1) {
323 if (Shortcut
[0] >= 'a' && Shortcut
[0] <= 'z')
324 Shortcut
[0] -= ('a' - 'A');
326 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
327 if (Screen
->Entries
[i
]->ShortcutDigit
== Shortcut
[0] || Screen
->Entries
[i
]->ShortcutLetter
== Shortcut
[0]) {
332 } else if (StrLen(Shortcut
) > 1) {
333 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
334 if (StriSubCmp(Shortcut
, Screen
->Entries
[i
]->Title
))
341 // Identify the end of row 0 and the beginning of row 1; store the results in the
342 // appropriate fields in State. Also reduce MaxVisible if that value is greater
343 // than the total number of row-0 tags and if we're in an icon-based screen
344 static VOID
IdentifyRows(IN SCROLL_STATE
*State
, IN REFIT_MENU_SCREEN
*Screen
) {
347 State
->FinalRow0
= 0;
348 State
->InitialRow1
= State
->MaxIndex
;
349 for (i
= 0; i
< State
->MaxIndex
; i
++) {
350 if (Screen
->Entries
[i
]->Row
== 0) {
351 State
->FinalRow0
= i
;
352 } else if ((Screen
->Entries
[i
]->Row
== 1) && (State
->InitialRow1
> i
)) {
353 State
->InitialRow1
= i
;
356 if ((State
->ScrollMode
== SCROLL_MODE_ICONS
) && (State
->MaxVisible
> (State
->FinalRow0
+ 1)))
357 State
->MaxVisible
= State
->FinalRow0
+ 1;
358 } // static VOID IdentifyRows()
361 // generic menu function
363 static UINTN
RunGenericMenu(IN REFIT_MENU_SCREEN
*Screen
, IN MENU_STYLE_FUNC StyleFunc
, IN OUT INTN
*DefaultEntryIndex
,
364 OUT REFIT_MENU_ENTRY
**ChosenEntry
)
371 BOOLEAN HaveTimeout
= FALSE
;
372 UINTN TimeoutCountdown
= 0;
373 CHAR16 TimeoutMessage
[256];
374 CHAR16 KeyAsString
[2];
377 if (Screen
->TimeoutSeconds
> 0) {
379 TimeoutCountdown
= Screen
->TimeoutSeconds
* 10;
383 StyleFunc(Screen
, &State
, MENU_FUNCTION_INIT
, NULL
);
384 IdentifyRows(&State
, Screen
);
385 // override the starting selection with the default index, if any
386 if (*DefaultEntryIndex
>= 0 && *DefaultEntryIndex
<= State
.MaxIndex
) {
387 State
.CurrentSelection
= *DefaultEntryIndex
;
388 UpdateScroll(&State
, SCROLL_NONE
);
390 State
.PaintAll
= TRUE
;
394 if (State
.PaintAll
) {
395 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
396 State
.PaintAll
= FALSE
;
397 } else if (State
.PaintSelection
) {
398 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_SELECTION
, NULL
);
399 State
.PaintSelection
= FALSE
;
403 SPrint(TimeoutMessage
, 255, L
"%s in %d seconds", Screen
->TimeoutText
, (TimeoutCountdown
+ 5) / 10);
404 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, TimeoutMessage
);
407 // read key press (and wait for it if applicable)
408 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
409 if (Status
== EFI_NOT_READY
) {
410 if (HaveTimeout
&& TimeoutCountdown
== 0) {
412 MenuExit
= MENU_EXIT_TIMEOUT
;
414 } else if (HaveTimeout
) {
415 refit_call1_wrapper(BS
->Stall
, 100000);
418 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
422 // the user pressed a key, cancel the timeout
423 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, L
"");
427 // react to key press
428 switch (key
.ScanCode
) {
430 UpdateScroll(&State
, SCROLL_LINE_UP
);
433 UpdateScroll(&State
, SCROLL_LINE_LEFT
);
436 UpdateScroll(&State
, SCROLL_LINE_DOWN
);
439 UpdateScroll(&State
, SCROLL_LINE_RIGHT
);
442 UpdateScroll(&State
, SCROLL_FIRST
);
445 UpdateScroll(&State
, SCROLL_LAST
);
448 UpdateScroll(&State
, SCROLL_PAGE_UP
);
451 UpdateScroll(&State
, SCROLL_PAGE_DOWN
);
454 MenuExit
= MENU_EXIT_ESCAPE
;
458 MenuExit
= MENU_EXIT_DETAILS
;
465 MenuExit
= MENU_EXIT_ESCAPE
;
468 switch (key
.UnicodeChar
) {
470 case CHAR_CARRIAGE_RETURN
:
472 MenuExit
= MENU_EXIT_ENTER
;
475 MenuExit
= MENU_EXIT_DETAILS
;
478 KeyAsString
[0] = key
.UnicodeChar
;
480 ShortcutEntry
= FindMenuShortcutEntry(Screen
, KeyAsString
);
481 if (ShortcutEntry
>= 0) {
482 State
.CurrentSelection
= ShortcutEntry
;
483 MenuExit
= MENU_EXIT_ENTER
;
489 StyleFunc(Screen
, &State
, MENU_FUNCTION_CLEANUP
, NULL
);
492 *ChosenEntry
= Screen
->Entries
[State
.CurrentSelection
];
493 *DefaultEntryIndex
= State
.CurrentSelection
;
495 } /* static UINTN RunGenericMenu() */
498 // text-mode generic style
501 static VOID
TextMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
504 UINTN MenuWidth
, ItemWidth
, MenuHeight
;
505 static UINTN MenuPosY
;
506 static CHAR16
**DisplayStrings
;
507 CHAR16 TimeoutMessage
[256];
509 State
->ScrollMode
= SCROLL_MODE_TEXT
;
512 case MENU_FUNCTION_INIT
:
515 if (Screen
->InfoLineCount
> 0)
516 MenuPosY
+= Screen
->InfoLineCount
+ 1;
517 MenuHeight
= ConHeight
- MenuPosY
- 3;
518 if (Screen
->TimeoutSeconds
> 0)
520 InitScroll(State
, Screen
->EntryCount
, MenuHeight
);
522 // determine width of the menu
523 MenuWidth
= 20; // minimum
524 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
525 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
526 if (MenuWidth
< ItemWidth
)
527 MenuWidth
= ItemWidth
;
530 if (MenuWidth
> ConWidth
- 3)
531 MenuWidth
= ConWidth
- 3;
533 // prepare strings for display
534 DisplayStrings
= AllocatePool(sizeof(CHAR16
*) * Screen
->EntryCount
);
535 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
536 // Note: Theoretically, SPrint() is a cleaner way to do this; but the
537 // description of the StrSize parameter to SPrint implies it's measured
538 // in characters, but in practice both TianoCore and GNU-EFI seem to
539 // use bytes instead, resulting in truncated displays. I could just
540 // double the size of the StrSize parameter, but that seems unsafe in
541 // case a future library change starts treating this as characters, so
542 // I'm doing it the hard way in this instance.
543 // TODO: Review the above and possibly change other uses of SPrint()
544 DisplayStrings
[i
] = AllocateZeroPool(2 * sizeof(CHAR16
));
545 DisplayStrings
[i
][0] = L
' ';
546 MergeStrings(&DisplayStrings
[i
], Screen
->Entries
[i
]->Title
, 0);
547 if (StrLen(DisplayStrings
[i
]) > MenuWidth
)
548 DisplayStrings
[i
][MenuWidth
- 1] = 0;
549 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
550 // TODO: account for double-width characters
554 BeginTextScreen(Screen
->Title
);
555 if (Screen
->InfoLineCount
> 0) {
556 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
557 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
558 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 4 + i
);
559 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->InfoLines
[i
]);
565 case MENU_FUNCTION_CLEANUP
:
566 // release temporary memory
567 for (i
= 0; i
<= State
->MaxIndex
; i
++)
568 MyFreePool(DisplayStrings
[i
]);
569 MyFreePool(DisplayStrings
);
572 case MENU_FUNCTION_PAINT_ALL
:
573 // paint the whole screen (initially and after scrolling)
574 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
575 if (i
>= State
->FirstVisible
&& i
<= State
->LastVisible
) {
576 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (i
- State
->FirstVisible
));
577 if (i
== State
->CurrentSelection
)
578 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
580 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
581 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[i
]);
584 // scrolling indicators
585 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_SCROLLARROW
);
586 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
);
587 if (State
->FirstVisible
> 0)
588 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowUp
);
590 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
591 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
+ State
->MaxVisible
);
592 if (State
->LastVisible
< State
->MaxIndex
)
593 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowDown
);
595 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
596 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HINTS
)) {
597 if (Screen
->Hint1
!= NULL
) {
598 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 2);
599 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->Hint1
);
601 if (Screen
->Hint2
!= NULL
) {
602 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 1);
603 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->Hint2
);
608 case MENU_FUNCTION_PAINT_SELECTION
:
609 // redraw selection cursor
610 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2,
611 MenuPosY
+ (State
->PreviousSelection
- State
->FirstVisible
));
612 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
613 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->PreviousSelection
]);
614 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2,
615 MenuPosY
+ (State
->CurrentSelection
- State
->FirstVisible
));
616 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
617 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->CurrentSelection
]);
620 case MENU_FUNCTION_PAINT_TIMEOUT
:
621 if (ParamText
[0] == 0) {
623 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
624 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 3);
625 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, BlankLine
+ 1);
627 // paint or update message
628 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
629 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, ConHeight
- 3);
630 SPrint(TimeoutMessage
, 255, L
"%s ", ParamText
);
631 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, TimeoutMessage
);
639 // graphical generic style
642 // Display an unselected icon on the screen, so that the background image shows
643 // through the transparency areas. The BadgeImage may be NULL, in which case
644 // it's not composited in.
645 static VOID
DrawImageWithTransparency(EG_IMAGE
*Image
, EG_IMAGE
*BadgeImage
, UINTN XPos
, UINTN YPos
, UINTN Width
, UINTN Height
) {
646 EG_IMAGE
*Background
;
648 Background
= egCropImage(GlobalConfig
.ScreenBackground
, XPos
, YPos
, Width
, Height
);
649 if (Background
!= NULL
) {
650 BltImageCompositeBadge(Background
, Image
, BadgeImage
, XPos
, YPos
);
651 egFreeImage(Background
);
653 } // VOID DrawImageWithTransparency()
657 static VOID
DrawSubmenuText(IN CHAR16
*Text
, IN UINTN SelectedWidth
, IN UINTN XPos
, IN UINTN YPos
)
659 if (TextBuffer
== NULL
)
660 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, TRUE
);
662 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
663 if (SelectedWidth
> 0) {
664 // draw selection bar background
665 egFillImageArea(TextBuffer
, 0, 0, SelectedWidth
, TextBuffer
->Height
,
666 &SelectionBackgroundPixel
);
670 egRenderText(Text
, TextBuffer
, TEXT_XMARGIN
, TEXT_YMARGIN
);
671 DrawImageWithTransparency(TextBuffer
, NULL
, XPos
, YPos
, TextBuffer
->Width
, TextBuffer
->Height
);
672 // BltImage(TextBuffer, XPos, YPos);
675 // Displays sub-menus
676 static VOID
GraphicsMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
680 static UINTN MenuWidth
, EntriesPosX
, EntriesPosY
, TimeoutPosY
;
682 State
->ScrollMode
= SCROLL_MODE_TEXT
;
685 case MENU_FUNCTION_INIT
:
686 InitScroll(State
, Screen
->EntryCount
, 0);
688 // determine width of the menu
689 MenuWidth
= 20; // minimum
690 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
691 ItemWidth
= StrLen(Screen
->InfoLines
[i
]);
692 if (MenuWidth
< ItemWidth
)
693 MenuWidth
= ItemWidth
;
695 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
696 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
697 if (MenuWidth
< ItemWidth
)
698 MenuWidth
= ItemWidth
;
700 MenuWidth
= TEXT_XMARGIN
* 2 + MenuWidth
* FONT_CELL_WIDTH
;
701 if (MenuWidth
> LAYOUT_TEXT_WIDTH
)
702 MenuWidth
= LAYOUT_TEXT_WIDTH
;
704 if (Screen
->TitleImage
)
705 EntriesPosX
= (UGAWidth
+ (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
) - MenuWidth
) >> 1;
707 EntriesPosX
= (UGAWidth
- MenuWidth
) >> 1;
708 EntriesPosY
= ComputeRow0PosX() + TEXT_LINE_HEIGHT
* 2;
709 TimeoutPosY
= EntriesPosY
+ (Screen
->EntryCount
+ 1) * TEXT_LINE_HEIGHT
;
712 SwitchToGraphicsAndClear();
713 egMeasureText(Screen
->Title
, &ItemWidth
, NULL
);
714 DrawSubmenuText(Screen
->Title
, 0, ((UGAWidth
- ItemWidth
) >> 1) - TEXT_XMARGIN
, EntriesPosY
- TEXT_LINE_HEIGHT
* 2);
715 if (Screen
->TitleImage
)
716 BltImage(Screen
->TitleImage
, EntriesPosX
- (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
), EntriesPosY
);
717 if (Screen
->InfoLineCount
> 0) {
718 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
719 DrawSubmenuText(Screen
->InfoLines
[i
], 0, EntriesPosX
, EntriesPosY
);
720 EntriesPosY
+= TEXT_LINE_HEIGHT
;
722 EntriesPosY
+= TEXT_LINE_HEIGHT
; // also add a blank line
727 case MENU_FUNCTION_CLEANUP
:
731 case MENU_FUNCTION_PAINT_ALL
:
732 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
733 DrawSubmenuText(Screen
->Entries
[i
]->Title
, (i
== State
->CurrentSelection
) ? MenuWidth
: 0,
734 EntriesPosX
, EntriesPosY
+ i
* TEXT_LINE_HEIGHT
);
736 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HINTS
)) {
737 if ((Screen
->Hint1
!= NULL
) && (StrLen(Screen
->Hint1
) > 0)) {
738 DrawSubmenuText(Screen
->Hint1
, 0, (UGAWidth
- (StrLen(Screen
->Hint1
) * FONT_CELL_WIDTH
)) / 2,
739 UGAHeight
- (FONT_CELL_HEIGHT
* 3));
741 if ((Screen
->Hint2
!= NULL
) && (StrLen(Screen
->Hint2
) > 0)) {
742 DrawSubmenuText(Screen
->Hint2
, 0, (UGAWidth
- (StrLen(Screen
->Hint2
) * FONT_CELL_WIDTH
)) / 2,
743 UGAHeight
- (FONT_CELL_HEIGHT
* 2));
748 case MENU_FUNCTION_PAINT_SELECTION
:
749 // redraw selection cursor
750 DrawSubmenuText(Screen
->Entries
[State
->PreviousSelection
]->Title
, 0,
751 EntriesPosX
, EntriesPosY
+ State
->PreviousSelection
* TEXT_LINE_HEIGHT
);
752 DrawSubmenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
, MenuWidth
,
753 EntriesPosX
, EntriesPosY
+ State
->CurrentSelection
* TEXT_LINE_HEIGHT
);
756 case MENU_FUNCTION_PAINT_TIMEOUT
:
757 DrawSubmenuText(ParamText
, 0, EntriesPosX
, TimeoutPosY
);
761 } // static VOID GraphicsMenuStyle()
764 // graphical main menu style
767 static VOID
DrawMainMenuEntry(REFIT_MENU_ENTRY
*Entry
, BOOLEAN selected
, UINTN XPos
, UINTN YPos
)
771 ImageNum
= ((Entry
->Row
== 0) ? 0 : 2) + (selected
? 0 : 1);
772 if (SelectionImages
!= NULL
) {
773 if ((ImageNum
== 1) || (ImageNum
== 3)) { // Image not selected; copy background
774 DrawImageWithTransparency(Entry
->Image
, Entry
->BadgeImage
, XPos
, YPos
,
775 SelectionImages
[ImageNum
]->Width
, SelectionImages
[ImageNum
]->Height
);
777 BltImageCompositeBadge(SelectionImages
[ImageNum
], Entry
->Image
, Entry
->BadgeImage
, XPos
, YPos
);
780 } // VOID DrawMainMenuEntry()
782 static VOID
DrawMainMenuText(IN CHAR16
*Text
, IN UINTN XPos
, IN UINTN YPos
)
784 UINTN TextWidth
, TextPosX
;
786 TextBuffer
= egCropImage(GlobalConfig
.ScreenBackground
, XPos
, YPos
, LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
);
789 egMeasureText(Text
, &TextWidth
, NULL
);
790 if (TextWidth
> TextBuffer
->Width
)
793 TextPosX
= (TextBuffer
->Width
- TextWidth
) / 2;
794 egRenderText(Text
, TextBuffer
, TextPosX
, 0);
795 DrawImageWithTransparency(TextBuffer
, NULL
, XPos
, YPos
, TextBuffer
->Width
, TextBuffer
->Height
);
798 static VOID
PaintAll(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
799 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
802 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0)
803 AdjustScrollState(State
);
804 for (i
= State
->FirstVisible
; i
<= State
->MaxIndex
; i
++) {
805 if (Screen
->Entries
[i
]->Row
== 0) {
806 if (i
<= State
->LastVisible
) {
807 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
,
808 itemPosX
[i
- State
->FirstVisible
], row0PosY
);
811 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
, itemPosX
[i
], row1PosY
);
814 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
815 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
816 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
818 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HINTS
)) {
819 DrawMainMenuText(Screen
->Hint1
, (UGAWidth
- LAYOUT_TEXT_WIDTH
) / 2, UGAHeight
- (FONT_CELL_HEIGHT
* 3));
820 DrawMainMenuText(Screen
->Hint2
, (UGAWidth
- LAYOUT_TEXT_WIDTH
) / 2, UGAHeight
- (FONT_CELL_HEIGHT
* 2));
822 } // static VOID PaintAll()
824 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
825 static VOID
PaintSelection(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
826 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
827 UINTN XSelectPrev
, XSelectCur
, YPosPrev
, YPosCur
;
829 if (((State
->CurrentSelection
<= State
->LastVisible
) && (State
->CurrentSelection
>= State
->FirstVisible
)) ||
830 (State
->CurrentSelection
>= State
->InitialRow1
) ) {
831 if (Screen
->Entries
[State
->PreviousSelection
]->Row
== 0) {
832 XSelectPrev
= State
->PreviousSelection
- State
->FirstVisible
;
835 XSelectPrev
= State
->PreviousSelection
;
838 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0) {
839 XSelectCur
= State
->CurrentSelection
- State
->FirstVisible
;
842 XSelectCur
= State
->CurrentSelection
;
845 DrawMainMenuEntry(Screen
->Entries
[State
->PreviousSelection
], FALSE
, itemPosX
[XSelectPrev
], YPosPrev
);
846 DrawMainMenuEntry(Screen
->Entries
[State
->CurrentSelection
], TRUE
, itemPosX
[XSelectCur
], YPosCur
);
847 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
848 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
849 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
850 } else { // Current selection not visible; must redraw the menu....
851 MainMenuStyle(Screen
, State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
853 } // static VOID MoveSelection(VOID)
855 // Display an icon at the specified location. Uses the image specified by
856 // ExternalFilename if it's available, or BuiltInImage if it's not. The
857 // Y position is specified as the center value, and so is adjusted by half
858 // the icon's height. The X position is set along the icon's left
859 // edge if Alignment == ALIGN_LEFT, and along the right edge if
860 // Alignment == ALIGN_RIGHT
861 static VOID
PaintIcon(IN EG_EMBEDDED_IMAGE
*BuiltInIcon
, IN CHAR16
*ExternalFilename
, UINTN PosX
, UINTN PosY
, UINTN Alignment
) {
862 EG_IMAGE
*Icon
= NULL
;
864 if (FileExists(SelfDir
, ExternalFilename
))
865 Icon
= egLoadIcon(SelfDir
, ExternalFilename
, 48);
867 Icon
= egPrepareEmbeddedImage(BuiltInIcon
, TRUE
);
869 if (Alignment
== ALIGN_RIGHT
)
871 DrawImageWithTransparency(Icon
, NULL
, PosX
, PosY
- (Icon
->Height
/ 2), Icon
->Width
, Icon
->Height
);
873 } // static VOID PaintIcon()
875 inline UINTN
ComputeRow0PosX(VOID
) {
876 return ((UGAHeight
/ 2) - (5 * ROW0_TILESIZE
/ 6));
877 } // UINTN ComputeRow0PosX()
879 // Display main menu in graphics mode
880 VOID
MainMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
883 static UINTN row0PosX
, row0PosXRunning
, row1PosY
, row0Loaders
;
884 UINTN row0Count
, row1Count
, row1PosX
, row1PosXRunning
;
885 static UINTN
*itemPosX
;
886 static UINTN row0PosY
, textPosY
;
887 CHAR16 FileName
[256];
889 State
->ScrollMode
= SCROLL_MODE_ICONS
;
892 case MENU_FUNCTION_INIT
:
893 InitScroll(State
, Screen
->EntryCount
, GlobalConfig
.MaxTags
);
899 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
900 if (Screen
->Entries
[i
]->Row
== 1) {
904 if (row0Count
< State
->MaxVisible
)
908 row0PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW0_TILESIZE
+ TILE_XSPACING
) * row0Count
) >> 1;
909 row0PosY
= ComputeRow0PosX();
910 // row0PosY = ((UGAHeight - LAYOUT_TOTAL_HEIGHT) >> 1) + LAYOUT_BANNER_YOFFSET;
911 row1PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW1_TILESIZE
+ TILE_XSPACING
) * row1Count
) >> 1;
912 row1PosY
= row0PosY
+ ROW0_TILESIZE
+ TILE_YSPACING
;
914 textPosY
= row1PosY
+ ROW1_TILESIZE
+ TILE_YSPACING
;
918 itemPosX
= AllocatePool(sizeof(UINTN
) * Screen
->EntryCount
);
919 row0PosXRunning
= row0PosX
;
920 row1PosXRunning
= row1PosX
;
921 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
922 if (Screen
->Entries
[i
]->Row
== 0) {
923 itemPosX
[i
] = row0PosXRunning
;
924 row0PosXRunning
+= ROW0_TILESIZE
+ TILE_XSPACING
;
926 itemPosX
[i
] = row1PosXRunning
;
927 row1PosXRunning
+= ROW1_TILESIZE
+ TILE_XSPACING
;
932 SwitchToGraphicsAndClear();
935 case MENU_FUNCTION_CLEANUP
:
936 MyFreePool(itemPosX
);
939 case MENU_FUNCTION_PAINT_ALL
:
940 BltClearScreen(TRUE
);
941 PaintAll(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
942 // For PaintIcon() calls, the starting Y position is moved to the midpoint
943 // of the surrounding row; PaintIcon() adjusts this back up by half the
944 // icon's height to properly center it.
945 if ((State
->FirstVisible
> 0) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
946 SPrint(FileName
, 255, L
"%s\\arrow_left.icns", GlobalConfig
.IconsDir
? GlobalConfig
.IconsDir
: DEFAULT_ICONS_DIR
);
947 PaintIcon(&egemb_arrow_left
, FileName
, row0PosX
- TILE_XSPACING
,
948 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_RIGHT
);
950 if ((State
->LastVisible
< (row0Loaders
- 1)) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
951 SPrint(FileName
, 255, L
"%s\\arrow_right.icns", GlobalConfig
.IconsDir
? GlobalConfig
.IconsDir
: DEFAULT_ICONS_DIR
);
952 PaintIcon(&egemb_arrow_right
, FileName
,
953 (UGAWidth
+ (ROW0_TILESIZE
+ TILE_XSPACING
) * State
->MaxVisible
) / 2 + TILE_XSPACING
,
954 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_LEFT
);
958 case MENU_FUNCTION_PAINT_SELECTION
:
959 PaintSelection(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
962 case MENU_FUNCTION_PAINT_TIMEOUT
:
963 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
964 DrawMainMenuText(ParamText
, (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
+ TEXT_LINE_HEIGHT
);
968 } // VOID MainMenuStyle()
970 // Enable the user to edit boot loader options.
971 // Returns TRUE if the user exited with edited options; FALSE if the user
972 // pressed Esc to terminate the edit.
973 static BOOLEAN
EditOptions(LOADER_ENTRY
*MenuEntry
) {
975 CHAR16
*EditedOptions
;
976 BOOLEAN retval
= FALSE
;
978 if (GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_EDITOR
) {
982 refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &x_max
, &y_max
);
984 if (!GlobalConfig
.TextOnly
)
987 if (line_edit(MenuEntry
->LoadOptions
, &EditedOptions
, x_max
)) {
988 MyFreePool(MenuEntry
->LoadOptions
);
989 MenuEntry
->LoadOptions
= EditedOptions
;
992 if (!GlobalConfig
.TextOnly
)
995 } // VOID EditOptions()
998 // user-callable dispatcher functions
1001 UINTN
RunMenu(IN REFIT_MENU_SCREEN
*Screen
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
1003 INTN DefaultEntry
= -1;
1004 MENU_STYLE_FUNC Style
= TextMenuStyle
;
1006 if (AllowGraphicsMode
)
1007 Style
= GraphicsMenuStyle
;
1009 return RunGenericMenu(Screen
, Style
, &DefaultEntry
, ChosenEntry
);
1012 UINTN
RunMainMenu(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
* DefaultSelection
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
1014 MENU_STYLE_FUNC Style
= TextMenuStyle
;
1015 MENU_STYLE_FUNC MainStyle
= TextMenuStyle
;
1016 REFIT_MENU_ENTRY
*TempChosenEntry
;
1018 INTN DefaultEntryIndex
= -1;
1019 INTN DefaultSubmenuIndex
= -1;
1021 if (DefaultSelection
!= NULL
) {
1022 // Find a menu entry that includes *DefaultSelection as a substring
1023 DefaultEntryIndex
= FindMenuShortcutEntry(Screen
, DefaultSelection
);
1026 if (AllowGraphicsMode
) {
1027 Style
= GraphicsMenuStyle
;
1028 MainStyle
= MainMenuStyle
;
1032 MenuExit
= RunGenericMenu(Screen
, MainStyle
, &DefaultEntryIndex
, &TempChosenEntry
);
1033 Screen
->TimeoutSeconds
= 0;
1035 if (MenuExit
== MENU_EXIT_DETAILS
) {
1036 if (TempChosenEntry
->SubScreen
!= NULL
) {
1037 MenuExit
= RunGenericMenu(TempChosenEntry
->SubScreen
, Style
, &DefaultSubmenuIndex
, &TempChosenEntry
);
1038 if (MenuExit
== MENU_EXIT_ESCAPE
|| TempChosenEntry
->Tag
== TAG_RETURN
)
1040 if (MenuExit
== MENU_EXIT_DETAILS
) {
1041 if (!EditOptions((LOADER_ENTRY
*) TempChosenEntry
))
1044 } else { // no sub-screen; ignore keypress
1047 } // Enter sub-screen
1051 *ChosenEntry
= TempChosenEntry
;
1053 } /* UINTN RunMainMenu() */