]> code.delx.au - refind/blob - refind/menu.c
Improved scan_delay feature & new ability to edit boot options
[refind] / refind / menu.c
1 /*
2 * refit/menu.c
3 * Menu functions
4 *
5 * Copyright (c) 2006 Christoph Pfisterer
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * Neither the name of Christoph Pfisterer nor the names of the
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 /*
37 * Modifications copyright (c) 2012 Roderick W. Smith
38 *
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.
42 *
43 */
44
45 #include "global.h"
46 #include "screen.h"
47 #include "lib.h"
48 #include "menu.h"
49 #include "config.h"
50 #include "libeg.h"
51 #include "../include/refit_call_wrapper.h"
52
53 #include "../include/egemb_back_selected_small.h"
54 #include "../include/egemb_arrow_left.h"
55 #include "../include/egemb_arrow_right.h"
56
57 // other menu definitions
58
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)
64
65 typedef VOID (*MENU_STYLE_FUNC)(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText);
66
67 static CHAR16 ArrowUp[2] = { ARROW_UP, 0 };
68 static CHAR16 ArrowDown[2] = { ARROW_DOWN, 0 };
69
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)
74
75 #define ROW0_TILESIZE (144)
76 #define ROW1_TILESIZE (64)
77 #define TILE_XSPACING (8)
78 #define TILE_YSPACING (16)
79
80 // Alignment values for PaintIcon()
81 #define ALIGN_RIGHT 1
82 #define ALIGN_LEFT 0
83
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;
87
88 //
89 // Graphics helper functions
90 //
91
92 static VOID InitSelection(VOID)
93 {
94 UINTN x, y, src_x, src_y;
95 EG_PIXEL *DestPtr, *SrcPtr;
96
97 if (!AllowGraphicsMode)
98 return;
99 if (SelectionImages[0] != NULL)
100 return;
101
102 // load small selection image
103 if (GlobalConfig.SelectionSmallFileName != NULL) {
104 SelectionImages[2] = egLoadImage(SelfDir, GlobalConfig.SelectionSmallFileName, FALSE);
105 }
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)
111 return;
112
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);
118 }
119 if (SelectionImages[0] == NULL) {
120 // calculate big selection image from small one
121
122 SelectionImages[0] = egCreateImage(ROW0_TILESIZE, ROW0_TILESIZE, FALSE);
123 if (SelectionImages[0] == NULL) {
124 egFreeImage(SelectionImages[2]);
125 SelectionImages[2] = NULL;
126 return;
127 }
128
129 DestPtr = SelectionImages[0]->PixelData;
130 SrcPtr = SelectionImages[2]->PixelData;
131 for (y = 0; y < ROW0_TILESIZE; y++) {
132 if (y < (ROW1_TILESIZE >> 1))
133 src_y = y;
134 else if (y < (ROW0_TILESIZE - (ROW1_TILESIZE >> 1)))
135 src_y = (ROW1_TILESIZE >> 1);
136 else
137 src_y = y - (ROW0_TILESIZE - ROW1_TILESIZE);
138
139 for (x = 0; x < ROW0_TILESIZE; x++) {
140 if (x < (ROW1_TILESIZE >> 1))
141 src_x = x;
142 else if (x < (ROW0_TILESIZE - (ROW1_TILESIZE >> 1)))
143 src_x = (ROW1_TILESIZE >> 1);
144 else
145 src_x = x - (ROW0_TILESIZE - ROW1_TILESIZE);
146
147 *DestPtr++ = SrcPtr[src_y * ROW1_TILESIZE + src_x];
148 }
149 }
150 }
151
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);
155 }
156
157 //
158 // Scrolling functions
159 //
160
161 static VOID InitScroll(OUT SCROLL_STATE *State, IN UINTN ItemCount, IN UINTN VisibleSpace)
162 {
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;
168 } else
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;
174
175 State->LastVisible = State->FirstVisible + State->MaxVisible - 1;
176 }
177
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;
187 } // Scroll forward
188 if (State->CurrentSelection < State->FirstVisible) {
189 State->FirstVisible = State->CurrentSelection;
190 State->LastVisible = State->CurrentSelection + State->MaxVisible - 1;
191 State->PaintAll = TRUE;
192 } // Scroll backward
193 } // static VOID AdjustScrollState
194
195 static VOID UpdateScroll(IN OUT SCROLL_STATE *State, IN UINTN Movement)
196 {
197 State->PreviousSelection = State->CurrentSelection;
198
199 switch (Movement) {
200 case SCROLL_LINE_LEFT:
201 if (State->CurrentSelection > 0) {
202 State->CurrentSelection --;
203 }
204 break;
205
206 case SCROLL_LINE_RIGHT:
207 if (State->CurrentSelection < State->MaxIndex) {
208 State->CurrentSelection ++;
209 }
210 break;
211
212 case SCROLL_LINE_UP:
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);
219 } else {
220 State->CurrentSelection = State->FirstVisible;
221 } // if/else
222 } // if in second row
223 } else {
224 if (State->CurrentSelection > 0)
225 State->CurrentSelection--;
226 } // if/else
227 break;
228
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);
236 } else {
237 State->CurrentSelection = State->InitialRow1;
238 } // if/else
239 } // if in first row
240 } else {
241 if (State->CurrentSelection < State->MaxIndex)
242 State->CurrentSelection++;
243 } // if/else
244 break;
245
246 case SCROLL_PAGE_UP:
247 if (State->CurrentSelection <= State->FinalRow0)
248 State->CurrentSelection -= State->MaxVisible;
249 else if (State->CurrentSelection == State->InitialRow1)
250 State->CurrentSelection = State->FinalRow0;
251 else
252 State->CurrentSelection = State->InitialRow1;
253 if (State->CurrentSelection < 0)
254 State->CurrentSelection = 0;
255 break;
256
257 case SCROLL_FIRST:
258 if (State->CurrentSelection > 0) {
259 State->PaintAll = TRUE;
260 State->CurrentSelection = 0;
261 }
262 break;
263
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++;
271 } else {
272 State->CurrentSelection = State->MaxIndex;
273 }
274 if (State->CurrentSelection > State->MaxIndex)
275 State->CurrentSelection = State->MaxIndex;
276 break;
277
278 case SCROLL_LAST:
279 if (State->CurrentSelection < State->MaxIndex) {
280 State->PaintAll = TRUE;
281 State->CurrentSelection = State->MaxIndex;
282 }
283 break;
284
285 case SCROLL_NONE:
286 break;
287
288 }
289 if (State->ScrollMode == SCROLL_MODE_TEXT)
290 AdjustScrollState(State);
291
292 if (!State->PaintAll && State->CurrentSelection != State->PreviousSelection)
293 State->PaintSelection = TRUE;
294 State->LastVisible = State->FirstVisible + State->MaxVisible - 1;
295 } // static VOID UpdateScroll()
296
297 //
298 // menu helper functions
299 //
300
301 VOID AddMenuInfoLine(IN REFIT_MENU_SCREEN *Screen, IN CHAR16 *InfoLine)
302 {
303 AddListElement((VOID ***) &(Screen->InfoLines), &(Screen->InfoLineCount), InfoLine);
304 }
305
306 VOID AddMenuEntry(IN REFIT_MENU_SCREEN *Screen, IN REFIT_MENU_ENTRY *Entry)
307 {
308 AddListElement((VOID ***) &(Screen->Entries), &(Screen->EntryCount), Entry);
309 }
310
311
312 static INTN FindMenuShortcutEntry(IN REFIT_MENU_SCREEN *Screen, IN CHAR16 *Shortcut)
313 {
314 UINTN i;
315
316 if (Shortcut == NULL)
317 return (-1);
318
319 if (StrLen(Shortcut) == 1) {
320 if (Shortcut[0] >= 'a' && Shortcut[0] <= 'z')
321 Shortcut[0] -= ('a' - 'A');
322 if (Shortcut[0]) {
323 for (i = 0; i < Screen->EntryCount; i++) {
324 if (Screen->Entries[i]->ShortcutDigit == Shortcut[0] || Screen->Entries[i]->ShortcutLetter == Shortcut[0]) {
325 return i;
326 } // if
327 } // for
328 } // if
329 } else if (StrLen(Shortcut) > 1) {
330 for (i = 0; i < Screen->EntryCount; i++) {
331 if (StriSubCmp(Shortcut, Screen->Entries[i]->Title))
332 return i;
333 } // for
334 }
335 return -1;
336 }
337
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) {
342 UINTN i;
343
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;
351 } // if/else
352 } // for
353 if ((State->ScrollMode == SCROLL_MODE_ICONS) && (State->MaxVisible > (State->FinalRow0 + 1)))
354 State->MaxVisible = State->FinalRow0 + 1;
355 } // static VOID IdentifyRows()
356
357 //
358 // generic menu function
359 //
360 static UINTN RunGenericMenu(IN REFIT_MENU_SCREEN *Screen, IN MENU_STYLE_FUNC StyleFunc, IN INTN DefaultEntryIndex, OUT REFIT_MENU_ENTRY **ChosenEntry)
361 {
362 SCROLL_STATE State;
363 EFI_STATUS Status;
364 EFI_INPUT_KEY key;
365 UINTN index;
366 INTN ShortcutEntry;
367 BOOLEAN HaveTimeout = FALSE;
368 UINTN TimeoutCountdown = 0;
369 CHAR16 TimeoutMessage[256];
370 CHAR16 KeyAsString[2];
371 UINTN MenuExit;
372
373 if (Screen->TimeoutSeconds > 0) {
374 HaveTimeout = TRUE;
375 TimeoutCountdown = Screen->TimeoutSeconds * 10;
376 }
377 MenuExit = 0;
378
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);
385 }
386
387 while (!MenuExit) {
388 // update the screen
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;
395 }
396
397 if (HaveTimeout) {
398 SPrint(TimeoutMessage, 255, L"%s in %d seconds", Screen->TimeoutText, (TimeoutCountdown + 5) / 10);
399 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, TimeoutMessage);
400 }
401
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) {
406 // timeout expired
407 MenuExit = MENU_EXIT_TIMEOUT;
408 break;
409 } else if (HaveTimeout) {
410 refit_call1_wrapper(BS->Stall, 100000);
411 TimeoutCountdown--;
412 } else
413 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
414 continue;
415 }
416 if (HaveTimeout) {
417 // the user pressed a key, cancel the timeout
418 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, L"");
419 HaveTimeout = FALSE;
420 }
421
422 // react to key press
423 switch (key.ScanCode) {
424 case SCAN_UP:
425 UpdateScroll(&State, SCROLL_LINE_UP);
426 break;
427 case SCAN_LEFT:
428 UpdateScroll(&State, SCROLL_LINE_LEFT);
429 break;
430 case SCAN_DOWN:
431 UpdateScroll(&State, SCROLL_LINE_DOWN);
432 break;
433 case SCAN_RIGHT:
434 UpdateScroll(&State, SCROLL_LINE_RIGHT);
435 break;
436 case SCAN_HOME:
437 UpdateScroll(&State, SCROLL_FIRST);
438 break;
439 case SCAN_END:
440 UpdateScroll(&State, SCROLL_LAST);
441 break;
442 case SCAN_PAGE_UP:
443 UpdateScroll(&State, SCROLL_PAGE_UP);
444 break;
445 case SCAN_PAGE_DOWN:
446 UpdateScroll(&State, SCROLL_PAGE_DOWN);
447 break;
448 case SCAN_ESC:
449 MenuExit = MENU_EXIT_ESCAPE;
450 break;
451 case SCAN_INSERT:
452 case SCAN_F2:
453 MenuExit = MENU_EXIT_DETAILS;
454 break;
455 case SCAN_F10:
456 egScreenShot();
457 break;
458 case 0x0016: // F12
459 if (EjectMedia())
460 MenuExit = MENU_EXIT_ESCAPE;
461 break;
462 }
463 switch (key.UnicodeChar) {
464 case CHAR_LINEFEED:
465 case CHAR_CARRIAGE_RETURN:
466 case ' ':
467 MenuExit = MENU_EXIT_ENTER;
468 break;
469 case '+':
470 MenuExit = MENU_EXIT_DETAILS;
471 break;
472 default:
473 KeyAsString[0] = key.UnicodeChar;
474 KeyAsString[1] = 0;
475 ShortcutEntry = FindMenuShortcutEntry(Screen, KeyAsString);
476 if (ShortcutEntry >= 0) {
477 State.CurrentSelection = ShortcutEntry;
478 MenuExit = MENU_EXIT_ENTER;
479 }
480 break;
481 }
482 }
483
484 StyleFunc(Screen, &State, MENU_FUNCTION_CLEANUP, NULL);
485
486 if (ChosenEntry)
487 *ChosenEntry = Screen->Entries[State.CurrentSelection];
488 return MenuExit;
489 } /* static UINTN RunGenericMenu() */
490
491 //
492 // text-mode generic style
493 //
494
495 static VOID TextMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
496 {
497 INTN i;
498 UINTN MenuWidth, ItemWidth, MenuHeight;
499 static UINTN MenuPosY;
500 static CHAR16 **DisplayStrings;
501 CHAR16 TimeoutMessage[256];
502
503 State->ScrollMode = SCROLL_MODE_TEXT;
504 switch (Function) {
505
506 case MENU_FUNCTION_INIT:
507 // vertical layout
508 MenuPosY = 4;
509 if (Screen->InfoLineCount > 0)
510 MenuPosY += Screen->InfoLineCount + 1;
511 MenuHeight = ConHeight - MenuPosY;
512 if (Screen->TimeoutSeconds > 0)
513 MenuHeight -= 2;
514 InitScroll(State, Screen->EntryCount, MenuHeight);
515
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;
522 }
523 if (MenuWidth > ConWidth - 6)
524 MenuWidth = ConWidth - 6;
525
526 // prepare strings for display
527 DisplayStrings = AllocatePool(sizeof(CHAR16 *) * Screen->EntryCount);
528 for (i = 0; i <= State->MaxIndex; i++) {
529 DisplayStrings[i] = AllocateZeroPool(256 * sizeof(CHAR16));
530 SPrint(DisplayStrings[i], 255, L" %-.*s ", MenuWidth, Screen->Entries[i]->Title);
531 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
532 // TODO: account for double-width characters
533 } // for
534
535 // initial painting
536 BeginTextScreen(Screen->Title);
537 if (Screen->InfoLineCount > 0) {
538 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
539 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
540 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, 4 + i);
541 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->InfoLines[i]);
542 }
543 }
544
545 break;
546
547 case MENU_FUNCTION_CLEANUP:
548 // release temporary memory
549 for (i = 0; i <= State->MaxIndex; i++)
550 MyFreePool(DisplayStrings[i]);
551 MyFreePool(DisplayStrings);
552 break;
553
554 case MENU_FUNCTION_PAINT_ALL:
555 // paint the whole screen (initially and after scrolling)
556 for (i = 0; i <= State->MaxIndex; i++) {
557 if (i >= State->FirstVisible && i <= State->LastVisible) {
558 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (i - State->FirstVisible));
559 if (i == State->CurrentSelection)
560 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
561 else
562 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
563 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[i]);
564 }
565 }
566 // scrolling indicators
567 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_SCROLLARROW);
568 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY);
569 if (State->FirstVisible > 0)
570 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowUp);
571 else
572 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
573 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY + State->MaxVisible);
574 if (State->LastVisible < State->MaxIndex)
575 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowDown);
576 else
577 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
578 break;
579
580 case MENU_FUNCTION_PAINT_SELECTION:
581 // redraw selection cursor
582 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (State->PreviousSelection - State->FirstVisible));
583 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
584 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->PreviousSelection]);
585 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (State->CurrentSelection - State->FirstVisible));
586 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
587 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->CurrentSelection]);
588 break;
589
590 case MENU_FUNCTION_PAINT_TIMEOUT:
591 if (ParamText[0] == 0) {
592 // clear message
593 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
594 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 1);
595 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, BlankLine + 1);
596 } else {
597 // paint or update message
598 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_ERROR);
599 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, ConHeight - 1);
600 SPrint(TimeoutMessage, 255, L"%s ", ParamText);
601 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, TimeoutMessage);
602 }
603 break;
604
605 }
606 }
607
608 //
609 // graphical generic style
610 //
611
612
613 static VOID DrawMenuText(IN CHAR16 *Text, IN UINTN SelectedWidth, IN UINTN XPos, IN UINTN YPos)
614 {
615 if (TextBuffer == NULL)
616 TextBuffer = egCreateImage(LAYOUT_TEXT_WIDTH, TEXT_LINE_HEIGHT, FALSE);
617
618 egFillImage(TextBuffer, &MenuBackgroundPixel);
619 if (SelectedWidth > 0) {
620 // draw selection bar background
621 egFillImageArea(TextBuffer, 0, 0, SelectedWidth, TextBuffer->Height,
622 &SelectionBackgroundPixel);
623 }
624
625 // render the text
626 egRenderText(Text, TextBuffer, TEXT_XMARGIN, TEXT_YMARGIN);
627 BltImage(TextBuffer, XPos, YPos);
628 }
629
630 // Displays sub-menus
631 static VOID GraphicsMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
632 {
633 INTN i;
634 UINTN ItemWidth;
635 static UINTN MenuWidth, EntriesPosX, EntriesPosY, TimeoutPosY;
636
637 State->ScrollMode = SCROLL_MODE_TEXT;
638 switch (Function) {
639
640 case MENU_FUNCTION_INIT:
641 InitScroll(State, Screen->EntryCount, 0);
642
643 // determine width of the menu
644 MenuWidth = 20; // minimum
645 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
646 ItemWidth = StrLen(Screen->InfoLines[i]);
647 if (MenuWidth < ItemWidth)
648 MenuWidth = ItemWidth;
649 }
650 for (i = 0; i <= State->MaxIndex; i++) {
651 ItemWidth = StrLen(Screen->Entries[i]->Title);
652 if (MenuWidth < ItemWidth)
653 MenuWidth = ItemWidth;
654 }
655 MenuWidth = TEXT_XMARGIN * 2 + MenuWidth * FONT_CELL_WIDTH;
656 if (MenuWidth > LAYOUT_TEXT_WIDTH)
657 MenuWidth = LAYOUT_TEXT_WIDTH;
658
659 if (Screen->TitleImage)
660 EntriesPosX = (UGAWidth + (Screen->TitleImage->Width + TITLEICON_SPACING) - MenuWidth) >> 1;
661 else
662 EntriesPosX = (UGAWidth - MenuWidth) >> 1;
663 EntriesPosY = ((UGAHeight - LAYOUT_TOTAL_HEIGHT) >> 1) + LAYOUT_BANNER_YOFFSET + TEXT_LINE_HEIGHT * 2;
664 TimeoutPosY = EntriesPosY + (Screen->EntryCount + 1) * TEXT_LINE_HEIGHT;
665
666 // initial painting
667 SwitchToGraphicsAndClear();
668 egMeasureText(Screen->Title, &ItemWidth, NULL);
669 DrawMenuText(Screen->Title, 0, ((UGAWidth - ItemWidth) >> 1) - TEXT_XMARGIN, EntriesPosY - TEXT_LINE_HEIGHT * 2);
670 if (Screen->TitleImage)
671 BltImageAlpha(Screen->TitleImage,
672 EntriesPosX - (Screen->TitleImage->Width + TITLEICON_SPACING), EntriesPosY,
673 &MenuBackgroundPixel);
674 if (Screen->InfoLineCount > 0) {
675 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
676 DrawMenuText(Screen->InfoLines[i], 0, EntriesPosX, EntriesPosY);
677 EntriesPosY += TEXT_LINE_HEIGHT;
678 }
679 EntriesPosY += TEXT_LINE_HEIGHT; // also add a blank line
680 }
681
682 break;
683
684 case MENU_FUNCTION_CLEANUP:
685 // nothing to do
686 break;
687
688 case MENU_FUNCTION_PAINT_ALL:
689 for (i = 0; i <= State->MaxIndex; i++) {
690 DrawMenuText(Screen->Entries[i]->Title, (i == State->CurrentSelection) ? MenuWidth : 0,
691 EntriesPosX, EntriesPosY + i * TEXT_LINE_HEIGHT);
692 }
693 break;
694
695 case MENU_FUNCTION_PAINT_SELECTION:
696 // redraw selection cursor
697 DrawMenuText(Screen->Entries[State->PreviousSelection]->Title, 0,
698 EntriesPosX, EntriesPosY + State->PreviousSelection * TEXT_LINE_HEIGHT);
699 DrawMenuText(Screen->Entries[State->CurrentSelection]->Title, MenuWidth,
700 EntriesPosX, EntriesPosY + State->CurrentSelection * TEXT_LINE_HEIGHT);
701 break;
702
703 case MENU_FUNCTION_PAINT_TIMEOUT:
704 DrawMenuText(ParamText, 0, EntriesPosX, TimeoutPosY);
705 break;
706
707 }
708 } // static VOID GraphicsMenuStyle()
709
710 //
711 // graphical main menu style
712 //
713
714 static VOID DrawMainMenuEntry(REFIT_MENU_ENTRY *Entry, BOOLEAN selected, UINTN XPos, UINTN YPos)
715 {
716 UINTN ImageNum;
717
718 ImageNum = ((Entry->Row == 0) ? 0 : 2) + (selected ? 0 : 1);
719 if (SelectionImages != NULL)
720 BltImageCompositeBadge(SelectionImages[ImageNum],
721 Entry->Image, Entry->BadgeImage, XPos, YPos);
722 }
723
724 static VOID DrawMainMenuText(IN CHAR16 *Text, IN UINTN XPos, IN UINTN YPos)
725 {
726 UINTN TextWidth, TextPosX;
727
728 if (TextBuffer == NULL)
729 TextBuffer = egCreateImage(LAYOUT_TEXT_WIDTH, TEXT_LINE_HEIGHT, FALSE);
730
731 egFillImage(TextBuffer, &MenuBackgroundPixel);
732
733 // render the text
734 egMeasureText(Text, &TextWidth, NULL);
735 if (TextWidth > TextBuffer->Width)
736 TextPosX = 0;
737 else
738 TextPosX = (TextBuffer->Width - TextWidth) / 2;
739 egRenderText(Text, TextBuffer, TextPosX, 0);
740 // egRenderText(Text, TextBuffer, (TextBuffer->Width - TextWidth) >> 1, 0);
741 BltImage(TextBuffer, XPos, YPos);
742 }
743
744 static VOID PaintAll(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
745 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
746 INTN i;
747
748 if (Screen->Entries[State->CurrentSelection]->Row == 0)
749 AdjustScrollState(State);
750 for (i = State->FirstVisible; i <= State->MaxIndex; i++) {
751 if (Screen->Entries[i]->Row == 0) {
752 if (i <= State->LastVisible) {
753 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE,
754 itemPosX[i - State->FirstVisible], row0PosY);
755 } // if
756 } else {
757 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE, itemPosX[i], row1PosY);
758 }
759 }
760 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL))
761 DrawMainMenuText(Screen->Entries[State->CurrentSelection]->Title,
762 (UGAWidth - LAYOUT_TEXT_WIDTH) >> 1, textPosY);
763 } // static VOID PaintAll()
764
765 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
766 static VOID PaintSelection(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
767 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
768 UINTN XSelectPrev, XSelectCur, YPosPrev, YPosCur;
769
770 if (((State->CurrentSelection <= State->LastVisible) && (State->CurrentSelection >= State->FirstVisible)) ||
771 (State->CurrentSelection >= State->InitialRow1) ) {
772 if (Screen->Entries[State->PreviousSelection]->Row == 0) {
773 XSelectPrev = State->PreviousSelection - State->FirstVisible;
774 YPosPrev = row0PosY;
775 } else {
776 XSelectPrev = State->PreviousSelection;
777 YPosPrev = row1PosY;
778 } // if/else
779 if (Screen->Entries[State->CurrentSelection]->Row == 0) {
780 XSelectCur = State->CurrentSelection - State->FirstVisible;
781 YPosCur = row0PosY;
782 } else {
783 XSelectCur = State->CurrentSelection;
784 YPosCur = row1PosY;
785 } // if/else
786 DrawMainMenuEntry(Screen->Entries[State->PreviousSelection], FALSE, itemPosX[XSelectPrev], YPosPrev);
787 DrawMainMenuEntry(Screen->Entries[State->CurrentSelection], TRUE, itemPosX[XSelectCur], YPosCur);
788 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL))
789 DrawMainMenuText(Screen->Entries[State->CurrentSelection]->Title,
790 (UGAWidth - LAYOUT_TEXT_WIDTH) >> 1, textPosY);
791 } else { // Current selection not visible; must redraw the menu....
792 MainMenuStyle(Screen, State, MENU_FUNCTION_PAINT_ALL, NULL);
793 }
794 } // static VOID MoveSelection(VOID)
795
796 // Display an icon at the specified location. Uses the image specified by
797 // ExternalFilename if it's available, or BuiltInImage if it's not. The
798 // Y position is specified as the center value, and so is adjusted by half
799 // the icon's height. The X position is set along the icon's left
800 // edge if Alignment == ALIGN_LEFT, and along the right edge if
801 // Alignment == ALIGN_RIGHT
802 static VOID PaintIcon(IN EG_EMBEDDED_IMAGE *BuiltInIcon, IN CHAR16 *ExternalFilename, UINTN PosX, UINTN PosY, UINTN Alignment) {
803 EG_IMAGE *Icon = NULL;
804
805 if (FileExists(SelfDir, ExternalFilename))
806 Icon = egLoadIcon(SelfDir, ExternalFilename, 48);
807 if (Icon == NULL)
808 Icon = egPrepareEmbeddedImage(BuiltInIcon, TRUE);
809 if (Icon != NULL) {
810 if (Alignment == ALIGN_RIGHT)
811 PosX -= Icon->Width;
812 BltImageAlpha(Icon, PosX, PosY - (Icon->Height / 2), &MenuBackgroundPixel);
813 }
814 } // static VOID PaintIcon()
815
816 // Display main menu in graphics mode
817 VOID MainMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
818 {
819 INTN i;
820 static UINTN row0PosX, row0PosXRunning, row1PosY, row0Loaders;
821 UINTN row0Count, row1Count, row1PosX, row1PosXRunning;
822 static UINTN *itemPosX;
823 static UINTN row0PosY, textPosY;
824 CHAR16 FileName[256];
825
826 State->ScrollMode = SCROLL_MODE_ICONS;
827 switch (Function) {
828
829 case MENU_FUNCTION_INIT:
830 InitScroll(State, Screen->EntryCount, GlobalConfig.MaxTags);
831
832 // layout
833 row0Count = 0;
834 row1Count = 0;
835 row0Loaders = 0;
836 for (i = 0; i <= State->MaxIndex; i++) {
837 if (Screen->Entries[i]->Row == 1) {
838 row1Count++;
839 } else {
840 row0Loaders++;
841 if (row0Count < State->MaxVisible)
842 row0Count++;
843 }
844 }
845 row0PosX = (UGAWidth + TILE_XSPACING - (ROW0_TILESIZE + TILE_XSPACING) * row0Count) >> 1;
846 row0PosY = ((UGAHeight - LAYOUT_TOTAL_HEIGHT) >> 1) + LAYOUT_BANNER_YOFFSET;
847 row1PosX = (UGAWidth + TILE_XSPACING - (ROW1_TILESIZE + TILE_XSPACING) * row1Count) >> 1;
848 row1PosY = row0PosY + ROW0_TILESIZE + TILE_YSPACING;
849 if (row1Count > 0)
850 textPosY = row1PosY + ROW1_TILESIZE + TILE_YSPACING;
851 else
852 textPosY = row1PosY;
853
854 itemPosX = AllocatePool(sizeof(UINTN) * Screen->EntryCount);
855 row0PosXRunning = row0PosX;
856 row1PosXRunning = row1PosX;
857 for (i = 0; i <= State->MaxIndex; i++) {
858 if (Screen->Entries[i]->Row == 0) {
859 itemPosX[i] = row0PosXRunning;
860 row0PosXRunning += ROW0_TILESIZE + TILE_XSPACING;
861 } else {
862 itemPosX[i] = row1PosXRunning;
863 row1PosXRunning += ROW1_TILESIZE + TILE_XSPACING;
864 }
865 }
866 // initial painting
867 InitSelection();
868 SwitchToGraphicsAndClear();
869 break;
870
871 case MENU_FUNCTION_CLEANUP:
872 MyFreePool(itemPosX);
873 break;
874
875 case MENU_FUNCTION_PAINT_ALL:
876 BltClearScreen(TRUE);
877 PaintAll(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
878 // For PaintIcon() calls, the starting Y position is moved to the midpoint
879 // of the surrounding row; PaintIcon() adjusts this back up by half the
880 // icon's height to properly center it.
881 if ((State->FirstVisible > 0) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
882 SPrint(FileName, 255, L"%s\\arrow_left.icns", GlobalConfig.IconsDir ? GlobalConfig.IconsDir : DEFAULT_ICONS_DIR);
883 PaintIcon(&egemb_arrow_left, FileName, row0PosX - TILE_XSPACING,
884 row0PosY + (ROW0_TILESIZE / 2), ALIGN_RIGHT);
885 } // if
886 if ((State->LastVisible < (row0Loaders - 1)) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
887 SPrint(FileName, 255, L"%s\\arrow_right.icns", GlobalConfig.IconsDir ? GlobalConfig.IconsDir : DEFAULT_ICONS_DIR);
888 PaintIcon(&egemb_arrow_right, FileName,
889 (UGAWidth + (ROW0_TILESIZE + TILE_XSPACING) * State->MaxVisible) / 2 + TILE_XSPACING,
890 row0PosY + (ROW0_TILESIZE / 2), ALIGN_LEFT);
891 } // if
892 break;
893
894 case MENU_FUNCTION_PAINT_SELECTION:
895 PaintSelection(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
896 break;
897
898 case MENU_FUNCTION_PAINT_TIMEOUT:
899 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL))
900 DrawMainMenuText(ParamText, (UGAWidth - LAYOUT_TEXT_WIDTH) >> 1, textPosY + TEXT_LINE_HEIGHT);
901 break;
902
903 }
904 } // VOID MainMenuStyle()
905
906 // Enable the user to edit boot loader options.
907 // Returns TRUE if the user exited with edited options; FALSE if the user
908 // pressed Esc to terminate the edit.
909 static BOOLEAN EditOptions(LOADER_ENTRY *MenuEntry) {
910 UINTN x_max, y_max;
911 CHAR16 *EditedOptions;
912 CHAR16 message[] = L"Use cursor keys to edit, Esc to exit, Enter to boot with edited options";
913 EG_PIXEL DarkBackgroundPixel = { 0x0, 0x0, 0x0, 0 };
914 BOOLEAN retval = FALSE;
915
916 refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max);
917
918 if (!GlobalConfig.TextOnly)
919 SwitchToText(TRUE);
920
921 egClearScreen(&DarkBackgroundPixel);
922
923 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, y_max - 1);
924 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, message);
925
926 if (line_edit(MenuEntry->LoadOptions, &EditedOptions, x_max, 1)) {
927 MyFreePool(MenuEntry->LoadOptions);
928 MenuEntry->LoadOptions = EditedOptions;
929 retval = TRUE;
930 } // if
931 if (!GlobalConfig.TextOnly)
932 SwitchToGraphics();
933 return retval;
934 } // VOID EditOptions()
935
936 //
937 // user-callable dispatcher functions
938 //
939
940 UINTN RunMenu(IN REFIT_MENU_SCREEN *Screen, OUT REFIT_MENU_ENTRY **ChosenEntry)
941 {
942 MENU_STYLE_FUNC Style = TextMenuStyle;
943
944 if (AllowGraphicsMode)
945 Style = GraphicsMenuStyle;
946
947 return RunGenericMenu(Screen, Style, -1, ChosenEntry);
948 }
949
950 UINTN RunMainMenu(IN REFIT_MENU_SCREEN *Screen, IN CHAR16* DefaultSelection, OUT REFIT_MENU_ENTRY **ChosenEntry)
951 {
952 MENU_STYLE_FUNC Style = TextMenuStyle;
953 MENU_STYLE_FUNC MainStyle = TextMenuStyle;
954 REFIT_MENU_ENTRY *TempChosenEntry;
955 UINTN MenuExit = 0;
956 UINTN DefaultEntryIndex = -1;
957
958 if (DefaultSelection != NULL) {
959 // Find a menu entry that includes *DefaultSelection as a substring
960 DefaultEntryIndex = FindMenuShortcutEntry(Screen, DefaultSelection);
961 // If that didn't work, should we scan more characters? For now, no.
962 }
963
964 if (AllowGraphicsMode) {
965 Style = GraphicsMenuStyle;
966 MainStyle = MainMenuStyle;
967 }
968
969 while (!MenuExit) {
970 MenuExit = RunGenericMenu(Screen, MainStyle, DefaultEntryIndex, &TempChosenEntry);
971 Screen->TimeoutSeconds = 0;
972
973 if (MenuExit == MENU_EXIT_DETAILS && TempChosenEntry->SubScreen != NULL) {
974 MenuExit = RunGenericMenu(TempChosenEntry->SubScreen, Style, -1, &TempChosenEntry);
975 if (MenuExit == MENU_EXIT_ESCAPE || TempChosenEntry->Tag == TAG_RETURN)
976 MenuExit = 0;
977 if (MenuExit == MENU_EXIT_DETAILS) {
978 if (!EditOptions((LOADER_ENTRY *) TempChosenEntry))
979 MenuExit = 0;
980 } // if
981 }
982 }
983
984 if (ChosenEntry)
985 *ChosenEntry = TempChosenEntry;
986 return MenuExit;
987 } /* UINTN RunMainMenu() */