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.
51 #include "../include/refit_call_wrapper.h"
53 #include "../include/egemb_back_selected_small.h"
54 #include "../include/egemb_arrow_left.h"
55 #include "../include/egemb_arrow_right.h"
57 // other menu definitions
59 #define MENU_FUNCTION_INIT (0)
60 #define MENU_FUNCTION_CLEANUP (1)
61 #define MENU_FUNCTION_PAINT_ALL (2)
62 #define MENU_FUNCTION_PAINT_SELECTION (3)
63 #define MENU_FUNCTION_PAINT_TIMEOUT (4)
65 typedef VOID (*MENU_STYLE_FUNC
)(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
);
67 static CHAR16 ArrowUp
[2] = { ARROW_UP
, 0 };
68 static CHAR16 ArrowDown
[2] = { ARROW_DOWN
, 0 };
70 #define TEXT_YMARGIN (2)
71 #define TEXT_XMARGIN (8)
72 #define TEXT_LINE_HEIGHT (FONT_CELL_HEIGHT + TEXT_YMARGIN * 2)
73 #define TITLEICON_SPACING (16)
75 #define ROW0_TILESIZE (144)
76 #define ROW1_TILESIZE (64)
77 #define TILE_XSPACING (8)
78 #define TILE_YSPACING (16)
80 // Alignment values for PaintIcon()
84 static EG_IMAGE
*SelectionImages
[4] = { NULL
, NULL
, NULL
, NULL
};
85 static EG_PIXEL SelectionBackgroundPixel
= { 0xff, 0xff, 0xff, 0 };
86 static EG_IMAGE
*TextBuffer
= NULL
;
89 // Graphics helper functions
92 static VOID
InitSelection(VOID
)
94 UINTN x
, y
, src_x
, src_y
;
95 EG_PIXEL
*DestPtr
, *SrcPtr
;
97 if (!AllowGraphicsMode
)
99 if (SelectionImages
[0] != NULL
)
102 // load small selection image
103 if (GlobalConfig
.SelectionSmallFileName
!= NULL
) {
104 SelectionImages
[2] = egLoadImage(SelfDir
, GlobalConfig
.SelectionSmallFileName
, FALSE
);
106 if (SelectionImages
[2] == NULL
)
107 SelectionImages
[2] = egPrepareEmbeddedImage(&egemb_back_selected_small
, FALSE
);
108 SelectionImages
[2] = egEnsureImageSize(SelectionImages
[2],
109 ROW1_TILESIZE
, ROW1_TILESIZE
, &MenuBackgroundPixel
);
110 if (SelectionImages
[2] == NULL
)
113 // load big selection image
114 if (GlobalConfig
.SelectionBigFileName
!= NULL
) {
115 SelectionImages
[0] = egLoadImage(SelfDir
, GlobalConfig
.SelectionBigFileName
, FALSE
);
116 SelectionImages
[0] = egEnsureImageSize(SelectionImages
[0],
117 ROW0_TILESIZE
, ROW0_TILESIZE
, &MenuBackgroundPixel
);
119 if (SelectionImages
[0] == NULL
) {
120 // calculate big selection image from small one
122 SelectionImages
[0] = egCreateImage(ROW0_TILESIZE
, ROW0_TILESIZE
, FALSE
);
123 if (SelectionImages
[0] == NULL
) {
124 egFreeImage(SelectionImages
[2]);
125 SelectionImages
[2] = NULL
;
129 DestPtr
= SelectionImages
[0]->PixelData
;
130 SrcPtr
= SelectionImages
[2]->PixelData
;
131 for (y
= 0; y
< ROW0_TILESIZE
; y
++) {
132 if (y
< (ROW1_TILESIZE
>> 1))
134 else if (y
< (ROW0_TILESIZE
- (ROW1_TILESIZE
>> 1)))
135 src_y
= (ROW1_TILESIZE
>> 1);
137 src_y
= y
- (ROW0_TILESIZE
- ROW1_TILESIZE
);
139 for (x
= 0; x
< ROW0_TILESIZE
; x
++) {
140 if (x
< (ROW1_TILESIZE
>> 1))
142 else if (x
< (ROW0_TILESIZE
- (ROW1_TILESIZE
>> 1)))
143 src_x
= (ROW1_TILESIZE
>> 1);
145 src_x
= x
- (ROW0_TILESIZE
- ROW1_TILESIZE
);
147 *DestPtr
++ = SrcPtr
[src_y
* ROW1_TILESIZE
+ src_x
];
152 // non-selected background images
153 SelectionImages
[1] = egCreateFilledImage(ROW0_TILESIZE
, ROW0_TILESIZE
, FALSE
, &MenuBackgroundPixel
);
154 SelectionImages
[3] = egCreateFilledImage(ROW1_TILESIZE
, ROW1_TILESIZE
, FALSE
, &MenuBackgroundPixel
);
158 // Scrolling functions
161 static VOID
InitScroll(OUT SCROLL_STATE
*State
, IN UINTN ItemCount
, IN UINTN VisibleSpace
)
163 State
->PreviousSelection
= State
->CurrentSelection
= 0;
164 State
->MaxIndex
= (INTN
)ItemCount
- 1;
165 State
->FirstVisible
= 0;
166 if (AllowGraphicsMode
) {
167 State
->MaxVisible
= UGAWidth
/ (ROW0_TILESIZE
+ TILE_XSPACING
) - 1;
169 State
->MaxVisible
= ConHeight
- 4;
170 if ((VisibleSpace
> 0) && (VisibleSpace
< State
->MaxVisible
))
171 State
->MaxVisible
= (INTN
)VisibleSpace
;
172 State
->PaintAll
= TRUE
;
173 State
->PaintSelection
= FALSE
;
175 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
178 // Adjust variables relating to the scrolling of tags, for when a selected icon isn't
179 // visible given the current scrolling condition....
180 static VOID
AdjustScrollState(IN SCROLL_STATE
*State
) {
181 if (State
->CurrentSelection
> State
->LastVisible
) {
182 State
->LastVisible
= State
->CurrentSelection
;
183 State
->FirstVisible
= 1 + State
->CurrentSelection
- State
->MaxVisible
;
184 if (State
->FirstVisible
< 0) // shouldn't happen, but just in case....
185 State
->FirstVisible
= 0;
186 State
->PaintAll
= TRUE
;
188 if (State
->CurrentSelection
< State
->FirstVisible
) {
189 State
->FirstVisible
= State
->CurrentSelection
;
190 State
->LastVisible
= State
->CurrentSelection
+ State
->MaxVisible
- 1;
191 State
->PaintAll
= TRUE
;
193 } // static VOID AdjustScrollState
195 static VOID
UpdateScroll(IN OUT SCROLL_STATE
*State
, IN UINTN Movement
)
197 State
->PreviousSelection
= State
->CurrentSelection
;
200 case SCROLL_LINE_LEFT
:
201 if (State
->CurrentSelection
> 0) {
202 State
->CurrentSelection
--;
206 case SCROLL_LINE_RIGHT
:
207 if (State
->CurrentSelection
< State
->MaxIndex
) {
208 State
->CurrentSelection
++;
213 if (State
->ScrollMode
== SCROLL_MODE_ICONS
) {
214 if (State
->CurrentSelection
>= State
->InitialRow1
) {
215 if (State
->MaxIndex
> State
->InitialRow1
) { // avoid division by 0!
216 State
->CurrentSelection
= State
->FirstVisible
+ (State
->LastVisible
- State
->FirstVisible
) *
217 (State
->CurrentSelection
- State
->InitialRow1
) /
218 (State
->MaxIndex
- State
->InitialRow1
);
220 State
->CurrentSelection
= State
->FirstVisible
;
222 } // if in second row
224 if (State
->CurrentSelection
> 0)
225 State
->CurrentSelection
--;
229 case SCROLL_LINE_DOWN
:
230 if (State
->ScrollMode
== SCROLL_MODE_ICONS
) {
231 if (State
->CurrentSelection
<= State
->FinalRow0
) {
232 if (State
->LastVisible
> State
->FirstVisible
) { // avoid division by 0!
233 State
->CurrentSelection
= State
->InitialRow1
+ (State
->MaxIndex
- State
->InitialRow1
) *
234 (State
->CurrentSelection
- State
->FirstVisible
) /
235 (State
->LastVisible
- State
->FirstVisible
);
237 State
->CurrentSelection
= State
->InitialRow1
;
241 if (State
->CurrentSelection
< State
->MaxIndex
)
242 State
->CurrentSelection
++;
247 if (State
->CurrentSelection
<= State
->FinalRow0
)
248 State
->CurrentSelection
-= State
->MaxVisible
;
249 else if (State
->CurrentSelection
== State
->InitialRow1
)
250 State
->CurrentSelection
= State
->FinalRow0
;
252 State
->CurrentSelection
= State
->InitialRow1
;
253 if (State
->CurrentSelection
< 0)
254 State
->CurrentSelection
= 0;
258 if (State
->CurrentSelection
> 0) {
259 State
->PaintAll
= TRUE
;
260 State
->CurrentSelection
= 0;
264 case SCROLL_PAGE_DOWN
:
265 if (State
->CurrentSelection
< State
->FinalRow0
) {
266 State
->CurrentSelection
+= State
->MaxVisible
;
267 if (State
->CurrentSelection
> State
->FinalRow0
)
268 State
->CurrentSelection
= State
->FinalRow0
;
269 } else if (State
->CurrentSelection
== State
->FinalRow0
) {
270 State
->CurrentSelection
++;
272 State
->CurrentSelection
= State
->MaxIndex
;
274 if (State
->CurrentSelection
> State
->MaxIndex
)
275 State
->CurrentSelection
= State
->MaxIndex
;
279 if (State
->CurrentSelection
< State
->MaxIndex
) {
280 State
->PaintAll
= TRUE
;
281 State
->CurrentSelection
= State
->MaxIndex
;
289 if (State
->ScrollMode
== SCROLL_MODE_TEXT
)
290 AdjustScrollState(State
);
292 if (!State
->PaintAll
&& State
->CurrentSelection
!= State
->PreviousSelection
)
293 State
->PaintSelection
= TRUE
;
294 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
295 } // static VOID UpdateScroll()
298 // menu helper functions
301 VOID
AddMenuInfoLine(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*InfoLine
)
303 AddListElement((VOID
***) &(Screen
->InfoLines
), &(Screen
->InfoLineCount
), InfoLine
);
306 VOID
AddMenuEntry(IN REFIT_MENU_SCREEN
*Screen
, IN REFIT_MENU_ENTRY
*Entry
)
308 AddListElement((VOID
***) &(Screen
->Entries
), &(Screen
->EntryCount
), Entry
);
312 static INTN
FindMenuShortcutEntry(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*Shortcut
)
316 if (Shortcut
== NULL
)
319 if (StrLen(Shortcut
) == 1) {
320 if (Shortcut
[0] >= 'a' && Shortcut
[0] <= 'z')
321 Shortcut
[0] -= ('a' - 'A');
323 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
324 if (Screen
->Entries
[i
]->ShortcutDigit
== Shortcut
[0] || Screen
->Entries
[i
]->ShortcutLetter
== Shortcut
[0]) {
329 } else if (StrLen(Shortcut
) > 1) {
330 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
331 if (StriSubCmp(Shortcut
, Screen
->Entries
[i
]->Title
))
338 // Identify the end of row 0 and the beginning of row 1; store the results in the
339 // appropriate fields in State. Also reduce MaxVisible if that value is greater
340 // than the total number of row-0 tags and if we're in an icon-based screen
341 static VOID
IdentifyRows(IN SCROLL_STATE
*State
, IN REFIT_MENU_SCREEN
*Screen
) {
344 State
->FinalRow0
= 0;
345 State
->InitialRow1
= State
->MaxIndex
;
346 for (i
= 0; i
< State
->MaxIndex
; i
++) {
347 if (Screen
->Entries
[i
]->Row
== 0) {
348 State
->FinalRow0
= i
;
349 } else if ((Screen
->Entries
[i
]->Row
== 1) && (State
->InitialRow1
> i
)) {
350 State
->InitialRow1
= i
;
353 if ((State
->ScrollMode
== SCROLL_MODE_ICONS
) && (State
->MaxVisible
> (State
->FinalRow0
+ 1)))
354 State
->MaxVisible
= State
->FinalRow0
+ 1;
355 } // static VOID IdentifyRows()
358 // generic menu function
360 static UINTN
RunGenericMenu(IN REFIT_MENU_SCREEN
*Screen
, IN MENU_STYLE_FUNC StyleFunc
, IN INTN DefaultEntryIndex
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
367 BOOLEAN HaveTimeout
= FALSE
;
368 UINTN TimeoutCountdown
= 0;
369 CHAR16 TimeoutMessage
[256];
370 CHAR16 KeyAsString
[2];
373 if (Screen
->TimeoutSeconds
> 0) {
375 TimeoutCountdown
= Screen
->TimeoutSeconds
* 10;
379 StyleFunc(Screen
, &State
, MENU_FUNCTION_INIT
, NULL
);
380 IdentifyRows(&State
, Screen
);
381 // override the starting selection with the default index, if any
382 if (DefaultEntryIndex
>= 0 && DefaultEntryIndex
<= State
.MaxIndex
) {
383 State
.CurrentSelection
= DefaultEntryIndex
;
384 UpdateScroll(&State
, SCROLL_NONE
);
389 if (State
.PaintAll
) {
390 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
391 State
.PaintAll
= FALSE
;
392 } else if (State
.PaintSelection
) {
393 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_SELECTION
, NULL
);
394 State
.PaintSelection
= FALSE
;
398 SPrint(TimeoutMessage
, 255, L
"%s in %d seconds", Screen
->TimeoutText
, (TimeoutCountdown
+ 5) / 10);
399 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, TimeoutMessage
);
402 // read key press (and wait for it if applicable)
403 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
404 if (Status
== EFI_NOT_READY
) {
405 if (HaveTimeout
&& TimeoutCountdown
== 0) {
407 MenuExit
= MENU_EXIT_TIMEOUT
;
409 } else if (HaveTimeout
) {
410 refit_call1_wrapper(BS
->Stall
, 100000);
413 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
417 // the user pressed a key, cancel the timeout
418 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, L
"");
422 // react to key press
423 switch (key
.ScanCode
) {
425 UpdateScroll(&State
, SCROLL_LINE_UP
);
428 UpdateScroll(&State
, SCROLL_LINE_LEFT
);
431 UpdateScroll(&State
, SCROLL_LINE_DOWN
);
434 UpdateScroll(&State
, SCROLL_LINE_RIGHT
);
437 UpdateScroll(&State
, SCROLL_FIRST
);
440 UpdateScroll(&State
, SCROLL_LAST
);
443 UpdateScroll(&State
, SCROLL_PAGE_UP
);
446 UpdateScroll(&State
, SCROLL_PAGE_DOWN
);
449 MenuExit
= MENU_EXIT_ESCAPE
;
453 MenuExit
= MENU_EXIT_DETAILS
;
460 MenuExit
= MENU_EXIT_ESCAPE
;
463 switch (key
.UnicodeChar
) {
465 case CHAR_CARRIAGE_RETURN
:
467 MenuExit
= MENU_EXIT_ENTER
;
470 MenuExit
= MENU_EXIT_DETAILS
;
473 KeyAsString
[0] = key
.UnicodeChar
;
475 ShortcutEntry
= FindMenuShortcutEntry(Screen
, KeyAsString
);
476 if (ShortcutEntry
>= 0) {
477 State
.CurrentSelection
= ShortcutEntry
;
478 MenuExit
= MENU_EXIT_ENTER
;
484 StyleFunc(Screen
, &State
, MENU_FUNCTION_CLEANUP
, NULL
);
487 *ChosenEntry
= Screen
->Entries
[State
.CurrentSelection
];
489 } /* static UINTN RunGenericMenu() */
492 // text-mode generic style
495 static VOID
TextMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
498 UINTN MenuWidth
, ItemWidth
, MenuHeight
;
499 static UINTN MenuPosY
;
500 static CHAR16
**DisplayStrings
;
501 CHAR16 TimeoutMessage
[256];
503 State
->ScrollMode
= SCROLL_MODE_TEXT
;
506 case MENU_FUNCTION_INIT
:
509 if (Screen
->InfoLineCount
> 0)
510 MenuPosY
+= Screen
->InfoLineCount
+ 1;
511 MenuHeight
= ConHeight
- MenuPosY
;
512 if (Screen
->TimeoutSeconds
> 0)
514 InitScroll(State
, Screen
->EntryCount
, MenuHeight
);
516 // determine width of the menu
517 MenuWidth
= 20; // minimum
518 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
519 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
520 if (MenuWidth
< ItemWidth
)
521 MenuWidth
= ItemWidth
;
523 if (MenuWidth
> ConWidth
- 6)
524 MenuWidth
= ConWidth
- 6;
526 // prepare strings for display
527 DisplayStrings
= AllocatePool(sizeof(CHAR16
*) * Screen
->EntryCount
);
528 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
529 // Note: Theoretically, SPrint() is a cleaner way to do this; but the
530 // description of the StrSize parameter to SPrint implies it's measured
531 // in characters, but in practice both TianoCore and GNU-EFI seem to
532 // use bytes instead, resulting in truncated displays. I could just
533 // double the size of the StrSize parameter, but that seems unsafe in
534 // case a future library change starts treating this as characters, so
535 // I'm doing it the hard way in this instance.
536 // TODO: Review the above and possibly change other uses of SPrint()
537 DisplayStrings
[i
] = AllocateZeroPool(2 * sizeof(CHAR16
));
538 DisplayStrings
[i
][0] = L
' ';
539 MergeStrings(&DisplayStrings
[i
], Screen
->Entries
[i
]->Title
, 0);
540 if (StrLen(DisplayStrings
[i
]) > MenuWidth
)
541 DisplayStrings
[i
][MenuWidth
- 1] = 0;
542 // DisplayStrings[i] = AllocateZeroPool(256 * sizeof(CHAR16));
543 // SPrint(DisplayStrings[i], ((MenuWidth < 255) ? MenuWidth : 255) * sizeof(CHAR16),
544 // L" %s ", Screen->Entries[i]->Title);
545 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
546 // TODO: account for double-width characters
550 BeginTextScreen(Screen
->Title
);
551 if (Screen
->InfoLineCount
> 0) {
552 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
553 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
554 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 4 + i
);
555 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->InfoLines
[i
]);
561 case MENU_FUNCTION_CLEANUP
:
562 // release temporary memory
563 for (i
= 0; i
<= State
->MaxIndex
; i
++)
564 MyFreePool(DisplayStrings
[i
]);
565 MyFreePool(DisplayStrings
);
568 case MENU_FUNCTION_PAINT_ALL
:
569 // paint the whole screen (initially and after scrolling)
570 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
571 if (i
>= State
->FirstVisible
&& i
<= State
->LastVisible
) {
572 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (i
- State
->FirstVisible
));
573 if (i
== State
->CurrentSelection
)
574 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
576 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
577 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[i
]);
580 // scrolling indicators
581 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_SCROLLARROW
);
582 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
);
583 if (State
->FirstVisible
> 0)
584 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowUp
);
586 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
587 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
+ State
->MaxVisible
);
588 if (State
->LastVisible
< State
->MaxIndex
)
589 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowDown
);
591 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
594 case MENU_FUNCTION_PAINT_SELECTION
:
595 // redraw selection cursor
596 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (State
->PreviousSelection
- State
->FirstVisible
));
597 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
598 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->PreviousSelection
]);
599 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (State
->CurrentSelection
- State
->FirstVisible
));
600 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
601 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->CurrentSelection
]);
604 case MENU_FUNCTION_PAINT_TIMEOUT
:
605 if (ParamText
[0] == 0) {
607 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
608 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 1);
609 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, BlankLine
+ 1);
611 // paint or update message
612 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
613 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, ConHeight
- 1);
614 SPrint(TimeoutMessage
, 255, L
"%s ", ParamText
);
615 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, TimeoutMessage
);
623 // graphical generic style
627 static VOID
DrawMenuText(IN CHAR16
*Text
, IN UINTN SelectedWidth
, IN UINTN XPos
, IN UINTN YPos
)
629 if (TextBuffer
== NULL
)
630 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
632 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
633 if (SelectedWidth
> 0) {
634 // draw selection bar background
635 egFillImageArea(TextBuffer
, 0, 0, SelectedWidth
, TextBuffer
->Height
,
636 &SelectionBackgroundPixel
);
640 egRenderText(Text
, TextBuffer
, TEXT_XMARGIN
, TEXT_YMARGIN
);
641 BltImage(TextBuffer
, XPos
, YPos
);
644 // Displays sub-menus
645 static VOID
GraphicsMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
649 static UINTN MenuWidth
, EntriesPosX
, EntriesPosY
, TimeoutPosY
;
651 State
->ScrollMode
= SCROLL_MODE_TEXT
;
654 case MENU_FUNCTION_INIT
:
655 InitScroll(State
, Screen
->EntryCount
, 0);
657 // determine width of the menu
658 MenuWidth
= 20; // minimum
659 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
660 ItemWidth
= StrLen(Screen
->InfoLines
[i
]);
661 if (MenuWidth
< ItemWidth
)
662 MenuWidth
= ItemWidth
;
664 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
665 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
666 if (MenuWidth
< ItemWidth
)
667 MenuWidth
= ItemWidth
;
669 MenuWidth
= TEXT_XMARGIN
* 2 + MenuWidth
* FONT_CELL_WIDTH
;
670 if (MenuWidth
> LAYOUT_TEXT_WIDTH
)
671 MenuWidth
= LAYOUT_TEXT_WIDTH
;
673 if (Screen
->TitleImage
)
674 EntriesPosX
= (UGAWidth
+ (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
) - MenuWidth
) >> 1;
676 EntriesPosX
= (UGAWidth
- MenuWidth
) >> 1;
677 EntriesPosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
+ TEXT_LINE_HEIGHT
* 2;
678 TimeoutPosY
= EntriesPosY
+ (Screen
->EntryCount
+ 1) * TEXT_LINE_HEIGHT
;
681 SwitchToGraphicsAndClear();
682 egMeasureText(Screen
->Title
, &ItemWidth
, NULL
);
683 DrawMenuText(Screen
->Title
, 0, ((UGAWidth
- ItemWidth
) >> 1) - TEXT_XMARGIN
, EntriesPosY
- TEXT_LINE_HEIGHT
* 2);
684 if (Screen
->TitleImage
)
685 BltImageAlpha(Screen
->TitleImage
,
686 EntriesPosX
- (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
), EntriesPosY
,
687 &MenuBackgroundPixel
);
688 if (Screen
->InfoLineCount
> 0) {
689 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
690 DrawMenuText(Screen
->InfoLines
[i
], 0, EntriesPosX
, EntriesPosY
);
691 EntriesPosY
+= TEXT_LINE_HEIGHT
;
693 EntriesPosY
+= TEXT_LINE_HEIGHT
; // also add a blank line
698 case MENU_FUNCTION_CLEANUP
:
702 case MENU_FUNCTION_PAINT_ALL
:
703 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
704 DrawMenuText(Screen
->Entries
[i
]->Title
, (i
== State
->CurrentSelection
) ? MenuWidth
: 0,
705 EntriesPosX
, EntriesPosY
+ i
* TEXT_LINE_HEIGHT
);
709 case MENU_FUNCTION_PAINT_SELECTION
:
710 // redraw selection cursor
711 DrawMenuText(Screen
->Entries
[State
->PreviousSelection
]->Title
, 0,
712 EntriesPosX
, EntriesPosY
+ State
->PreviousSelection
* TEXT_LINE_HEIGHT
);
713 DrawMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
, MenuWidth
,
714 EntriesPosX
, EntriesPosY
+ State
->CurrentSelection
* TEXT_LINE_HEIGHT
);
717 case MENU_FUNCTION_PAINT_TIMEOUT
:
718 DrawMenuText(ParamText
, 0, EntriesPosX
, TimeoutPosY
);
722 } // static VOID GraphicsMenuStyle()
725 // graphical main menu style
728 static VOID
DrawMainMenuEntry(REFIT_MENU_ENTRY
*Entry
, BOOLEAN selected
, UINTN XPos
, UINTN YPos
)
732 ImageNum
= ((Entry
->Row
== 0) ? 0 : 2) + (selected
? 0 : 1);
733 if (SelectionImages
!= NULL
)
734 BltImageCompositeBadge(SelectionImages
[ImageNum
],
735 Entry
->Image
, Entry
->BadgeImage
, XPos
, YPos
);
738 static VOID
DrawMainMenuText(IN CHAR16
*Text
, IN UINTN XPos
, IN UINTN YPos
)
740 UINTN TextWidth
, TextPosX
;
742 if (TextBuffer
== NULL
)
743 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
745 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
748 egMeasureText(Text
, &TextWidth
, NULL
);
749 if (TextWidth
> TextBuffer
->Width
)
752 TextPosX
= (TextBuffer
->Width
- TextWidth
) / 2;
753 egRenderText(Text
, TextBuffer
, TextPosX
, 0);
754 // egRenderText(Text, TextBuffer, (TextBuffer->Width - TextWidth) >> 1, 0);
755 BltImage(TextBuffer
, XPos
, YPos
);
758 static VOID
PaintAll(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
759 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
762 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0)
763 AdjustScrollState(State
);
764 for (i
= State
->FirstVisible
; i
<= State
->MaxIndex
; i
++) {
765 if (Screen
->Entries
[i
]->Row
== 0) {
766 if (i
<= State
->LastVisible
) {
767 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
,
768 itemPosX
[i
- State
->FirstVisible
], row0PosY
);
771 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
, itemPosX
[i
], row1PosY
);
774 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
775 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
776 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
777 } // static VOID PaintAll()
779 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
780 static VOID
PaintSelection(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
781 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
782 UINTN XSelectPrev
, XSelectCur
, YPosPrev
, YPosCur
;
784 if (((State
->CurrentSelection
<= State
->LastVisible
) && (State
->CurrentSelection
>= State
->FirstVisible
)) ||
785 (State
->CurrentSelection
>= State
->InitialRow1
) ) {
786 if (Screen
->Entries
[State
->PreviousSelection
]->Row
== 0) {
787 XSelectPrev
= State
->PreviousSelection
- State
->FirstVisible
;
790 XSelectPrev
= State
->PreviousSelection
;
793 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0) {
794 XSelectCur
= State
->CurrentSelection
- State
->FirstVisible
;
797 XSelectCur
= State
->CurrentSelection
;
800 DrawMainMenuEntry(Screen
->Entries
[State
->PreviousSelection
], FALSE
, itemPosX
[XSelectPrev
], YPosPrev
);
801 DrawMainMenuEntry(Screen
->Entries
[State
->CurrentSelection
], TRUE
, itemPosX
[XSelectCur
], YPosCur
);
802 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
803 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
804 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
805 } else { // Current selection not visible; must redraw the menu....
806 MainMenuStyle(Screen
, State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
808 } // static VOID MoveSelection(VOID)
810 // Display an icon at the specified location. Uses the image specified by
811 // ExternalFilename if it's available, or BuiltInImage if it's not. The
812 // Y position is specified as the center value, and so is adjusted by half
813 // the icon's height. The X position is set along the icon's left
814 // edge if Alignment == ALIGN_LEFT, and along the right edge if
815 // Alignment == ALIGN_RIGHT
816 static VOID
PaintIcon(IN EG_EMBEDDED_IMAGE
*BuiltInIcon
, IN CHAR16
*ExternalFilename
, UINTN PosX
, UINTN PosY
, UINTN Alignment
) {
817 EG_IMAGE
*Icon
= NULL
;
819 if (FileExists(SelfDir
, ExternalFilename
))
820 Icon
= egLoadIcon(SelfDir
, ExternalFilename
, 48);
822 Icon
= egPrepareEmbeddedImage(BuiltInIcon
, TRUE
);
824 if (Alignment
== ALIGN_RIGHT
)
826 BltImageAlpha(Icon
, PosX
, PosY
- (Icon
->Height
/ 2), &MenuBackgroundPixel
);
828 } // static VOID PaintIcon()
830 // Display main menu in graphics mode
831 VOID
MainMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
834 static UINTN row0PosX
, row0PosXRunning
, row1PosY
, row0Loaders
;
835 UINTN row0Count
, row1Count
, row1PosX
, row1PosXRunning
;
836 static UINTN
*itemPosX
;
837 static UINTN row0PosY
, textPosY
;
838 CHAR16 FileName
[256];
840 State
->ScrollMode
= SCROLL_MODE_ICONS
;
843 case MENU_FUNCTION_INIT
:
844 InitScroll(State
, Screen
->EntryCount
, GlobalConfig
.MaxTags
);
850 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
851 if (Screen
->Entries
[i
]->Row
== 1) {
855 if (row0Count
< State
->MaxVisible
)
859 row0PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW0_TILESIZE
+ TILE_XSPACING
) * row0Count
) >> 1;
860 row0PosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
;
861 row1PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW1_TILESIZE
+ TILE_XSPACING
) * row1Count
) >> 1;
862 row1PosY
= row0PosY
+ ROW0_TILESIZE
+ TILE_YSPACING
;
864 textPosY
= row1PosY
+ ROW1_TILESIZE
+ TILE_YSPACING
;
868 itemPosX
= AllocatePool(sizeof(UINTN
) * Screen
->EntryCount
);
869 row0PosXRunning
= row0PosX
;
870 row1PosXRunning
= row1PosX
;
871 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
872 if (Screen
->Entries
[i
]->Row
== 0) {
873 itemPosX
[i
] = row0PosXRunning
;
874 row0PosXRunning
+= ROW0_TILESIZE
+ TILE_XSPACING
;
876 itemPosX
[i
] = row1PosXRunning
;
877 row1PosXRunning
+= ROW1_TILESIZE
+ TILE_XSPACING
;
882 SwitchToGraphicsAndClear();
885 case MENU_FUNCTION_CLEANUP
:
886 MyFreePool(itemPosX
);
889 case MENU_FUNCTION_PAINT_ALL
:
890 BltClearScreen(TRUE
);
891 PaintAll(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
892 // For PaintIcon() calls, the starting Y position is moved to the midpoint
893 // of the surrounding row; PaintIcon() adjusts this back up by half the
894 // icon's height to properly center it.
895 if ((State
->FirstVisible
> 0) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
896 SPrint(FileName
, 255, L
"%s\\arrow_left.icns", GlobalConfig
.IconsDir
? GlobalConfig
.IconsDir
: DEFAULT_ICONS_DIR
);
897 PaintIcon(&egemb_arrow_left
, FileName
, row0PosX
- TILE_XSPACING
,
898 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_RIGHT
);
900 if ((State
->LastVisible
< (row0Loaders
- 1)) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
901 SPrint(FileName
, 255, L
"%s\\arrow_right.icns", GlobalConfig
.IconsDir
? GlobalConfig
.IconsDir
: DEFAULT_ICONS_DIR
);
902 PaintIcon(&egemb_arrow_right
, FileName
,
903 (UGAWidth
+ (ROW0_TILESIZE
+ TILE_XSPACING
) * State
->MaxVisible
) / 2 + TILE_XSPACING
,
904 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_LEFT
);
908 case MENU_FUNCTION_PAINT_SELECTION
:
909 PaintSelection(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
912 case MENU_FUNCTION_PAINT_TIMEOUT
:
913 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
914 DrawMainMenuText(ParamText
, (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
+ TEXT_LINE_HEIGHT
);
918 } // VOID MainMenuStyle()
920 // Enable the user to edit boot loader options.
921 // Returns TRUE if the user exited with edited options; FALSE if the user
922 // pressed Esc to terminate the edit.
923 static BOOLEAN
EditOptions(LOADER_ENTRY
*MenuEntry
) {
925 CHAR16
*EditedOptions
;
926 CHAR16 message
[] = L
"Use cursor keys to edit, Esc to exit, Enter to boot with edited options";
927 EG_PIXEL DarkBackgroundPixel
= { 0x0, 0x0, 0x0, 0 };
928 BOOLEAN retval
= FALSE
;
930 refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &x_max
, &y_max
);
932 if (!GlobalConfig
.TextOnly
)
935 egClearScreen(&DarkBackgroundPixel
);
937 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, y_max
- 1);
938 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, message
);
940 if (line_edit(MenuEntry
->LoadOptions
, &EditedOptions
, x_max
, 1)) {
941 MyFreePool(MenuEntry
->LoadOptions
);
942 MenuEntry
->LoadOptions
= EditedOptions
;
945 if (!GlobalConfig
.TextOnly
)
948 } // VOID EditOptions()
951 // user-callable dispatcher functions
954 UINTN
RunMenu(IN REFIT_MENU_SCREEN
*Screen
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
956 MENU_STYLE_FUNC Style
= TextMenuStyle
;
958 if (AllowGraphicsMode
)
959 Style
= GraphicsMenuStyle
;
961 return RunGenericMenu(Screen
, Style
, -1, ChosenEntry
);
964 UINTN
RunMainMenu(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
* DefaultSelection
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
966 MENU_STYLE_FUNC Style
= TextMenuStyle
;
967 MENU_STYLE_FUNC MainStyle
= TextMenuStyle
;
968 REFIT_MENU_ENTRY
*TempChosenEntry
;
970 UINTN DefaultEntryIndex
= -1;
972 if (DefaultSelection
!= NULL
) {
973 // Find a menu entry that includes *DefaultSelection as a substring
974 DefaultEntryIndex
= FindMenuShortcutEntry(Screen
, DefaultSelection
);
975 // If that didn't work, should we scan more characters? For now, no.
978 if (AllowGraphicsMode
) {
979 Style
= GraphicsMenuStyle
;
980 MainStyle
= MainMenuStyle
;
984 MenuExit
= RunGenericMenu(Screen
, MainStyle
, DefaultEntryIndex
, &TempChosenEntry
);
985 Screen
->TimeoutSeconds
= 0;
987 if (MenuExit
== MENU_EXIT_DETAILS
&& TempChosenEntry
->SubScreen
!= NULL
) {
988 MenuExit
= RunGenericMenu(TempChosenEntry
->SubScreen
, Style
, -1, &TempChosenEntry
);
989 if (MenuExit
== MENU_EXIT_ESCAPE
|| TempChosenEntry
->Tag
== TAG_RETURN
)
991 if (MenuExit
== MENU_EXIT_DETAILS
) {
992 if (!EditOptions((LOADER_ENTRY
*) TempChosenEntry
))
999 *ChosenEntry
= TempChosenEntry
;
1001 } /* UINTN RunMainMenu() */