]> code.delx.au - refind/blob - refind/menu.c
New "textmode" option to set the text-mode video mode. Also bug fixes.
[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 #define MENU_FUNCTION_PAINT_HINTS (5)
65
66 typedef VOID (*MENU_STYLE_FUNC)(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText);
67
68 static CHAR16 ArrowUp[2] = { ARROW_UP, 0 };
69 static CHAR16 ArrowDown[2] = { ARROW_DOWN, 0 };
70
71 #define TEXT_YMARGIN (2)
72 #define TEXT_XMARGIN (8)
73 #define TEXT_LINE_HEIGHT (FONT_CELL_HEIGHT + TEXT_YMARGIN * 2)
74 #define TITLEICON_SPACING (16)
75
76 #define ROW0_TILESIZE (144)
77 #define ROW1_TILESIZE (64)
78 #define TILE_XSPACING (8)
79 #define TILE_YSPACING (16)
80
81 // Alignment values for PaintIcon()
82 #define ALIGN_RIGHT 1
83 #define ALIGN_LEFT 0
84
85 static EG_IMAGE *SelectionImages[4] = { NULL, NULL, NULL, NULL };
86 static EG_PIXEL SelectionBackgroundPixel = { 0xff, 0xff, 0xff, 0 };
87 static EG_IMAGE *TextBuffer = NULL;
88
89 //
90 // Graphics helper functions
91 //
92
93 static VOID InitSelection(VOID)
94 {
95 UINTN x, y, src_x, src_y;
96 EG_PIXEL *DestPtr, *SrcPtr;
97
98 if (!AllowGraphicsMode)
99 return;
100 if (SelectionImages[0] != NULL)
101 return;
102
103 // load small selection image
104 if (GlobalConfig.SelectionSmallFileName != NULL) {
105 SelectionImages[2] = egLoadImage(SelfDir, GlobalConfig.SelectionSmallFileName, FALSE);
106 }
107 if (SelectionImages[2] == NULL)
108 SelectionImages[2] = egPrepareEmbeddedImage(&egemb_back_selected_small, FALSE);
109 SelectionImages[2] = egEnsureImageSize(SelectionImages[2],
110 ROW1_TILESIZE, ROW1_TILESIZE, &MenuBackgroundPixel);
111 if (SelectionImages[2] == NULL)
112 return;
113
114 // load big selection image
115 if (GlobalConfig.SelectionBigFileName != NULL) {
116 SelectionImages[0] = egLoadImage(SelfDir, GlobalConfig.SelectionBigFileName, FALSE);
117 SelectionImages[0] = egEnsureImageSize(SelectionImages[0],
118 ROW0_TILESIZE, ROW0_TILESIZE, &MenuBackgroundPixel);
119 }
120 if (SelectionImages[0] == NULL) {
121 // calculate big selection image from small one
122
123 SelectionImages[0] = egCreateImage(ROW0_TILESIZE, ROW0_TILESIZE, FALSE);
124 if (SelectionImages[0] == NULL) {
125 egFreeImage(SelectionImages[2]);
126 SelectionImages[2] = NULL;
127 return;
128 }
129
130 DestPtr = SelectionImages[0]->PixelData;
131 SrcPtr = SelectionImages[2]->PixelData;
132 for (y = 0; y < ROW0_TILESIZE; y++) {
133 if (y < (ROW1_TILESIZE >> 1))
134 src_y = y;
135 else if (y < (ROW0_TILESIZE - (ROW1_TILESIZE >> 1)))
136 src_y = (ROW1_TILESIZE >> 1);
137 else
138 src_y = y - (ROW0_TILESIZE - ROW1_TILESIZE);
139
140 for (x = 0; x < ROW0_TILESIZE; x++) {
141 if (x < (ROW1_TILESIZE >> 1))
142 src_x = x;
143 else if (x < (ROW0_TILESIZE - (ROW1_TILESIZE >> 1)))
144 src_x = (ROW1_TILESIZE >> 1);
145 else
146 src_x = x - (ROW0_TILESIZE - ROW1_TILESIZE);
147
148 *DestPtr++ = SrcPtr[src_y * ROW1_TILESIZE + src_x];
149 }
150 }
151 }
152
153 // non-selected background images
154 SelectionImages[1] = egCreateFilledImage(ROW0_TILESIZE, ROW0_TILESIZE, FALSE, &MenuBackgroundPixel);
155 SelectionImages[3] = egCreateFilledImage(ROW1_TILESIZE, ROW1_TILESIZE, FALSE, &MenuBackgroundPixel);
156 }
157
158 //
159 // Scrolling functions
160 //
161
162 static VOID InitScroll(OUT SCROLL_STATE *State, IN UINTN ItemCount, IN UINTN VisibleSpace)
163 {
164 State->PreviousSelection = State->CurrentSelection = 0;
165 State->MaxIndex = (INTN)ItemCount - 1;
166 State->FirstVisible = 0;
167 if (AllowGraphicsMode) {
168 State->MaxVisible = UGAWidth / (ROW0_TILESIZE + TILE_XSPACING) - 1;
169 } else
170 State->MaxVisible = ConHeight - 4;
171 if ((VisibleSpace > 0) && (VisibleSpace < State->MaxVisible))
172 State->MaxVisible = (INTN)VisibleSpace;
173 State->PaintAll = TRUE;
174 State->PaintSelection = FALSE;
175
176 State->LastVisible = State->FirstVisible + State->MaxVisible - 1;
177 }
178
179 // Adjust variables relating to the scrolling of tags, for when a selected icon isn't
180 // visible given the current scrolling condition....
181 static VOID AdjustScrollState(IN SCROLL_STATE *State) {
182 if (State->CurrentSelection > State->LastVisible) {
183 State->LastVisible = State->CurrentSelection;
184 State->FirstVisible = 1 + State->CurrentSelection - State->MaxVisible;
185 if (State->FirstVisible < 0) // shouldn't happen, but just in case....
186 State->FirstVisible = 0;
187 State->PaintAll = TRUE;
188 } // Scroll forward
189 if (State->CurrentSelection < State->FirstVisible) {
190 State->FirstVisible = State->CurrentSelection;
191 State->LastVisible = State->CurrentSelection + State->MaxVisible - 1;
192 State->PaintAll = TRUE;
193 } // Scroll backward
194 } // static VOID AdjustScrollState
195
196 static VOID UpdateScroll(IN OUT SCROLL_STATE *State, IN UINTN Movement)
197 {
198 State->PreviousSelection = State->CurrentSelection;
199
200 switch (Movement) {
201 case SCROLL_LINE_LEFT:
202 if (State->CurrentSelection > 0) {
203 State->CurrentSelection --;
204 }
205 break;
206
207 case SCROLL_LINE_RIGHT:
208 if (State->CurrentSelection < State->MaxIndex) {
209 State->CurrentSelection ++;
210 }
211 break;
212
213 case SCROLL_LINE_UP:
214 if (State->ScrollMode == SCROLL_MODE_ICONS) {
215 if (State->CurrentSelection >= State->InitialRow1) {
216 if (State->MaxIndex > State->InitialRow1) { // avoid division by 0!
217 State->CurrentSelection = State->FirstVisible + (State->LastVisible - State->FirstVisible) *
218 (State->CurrentSelection - State->InitialRow1) /
219 (State->MaxIndex - State->InitialRow1);
220 } else {
221 State->CurrentSelection = State->FirstVisible;
222 } // if/else
223 } // if in second row
224 } else {
225 if (State->CurrentSelection > 0)
226 State->CurrentSelection--;
227 } // if/else
228 break;
229
230 case SCROLL_LINE_DOWN:
231 if (State->ScrollMode == SCROLL_MODE_ICONS) {
232 if (State->CurrentSelection <= State->FinalRow0) {
233 if (State->LastVisible > State->FirstVisible) { // avoid division by 0!
234 State->CurrentSelection = State->InitialRow1 + (State->MaxIndex - State->InitialRow1) *
235 (State->CurrentSelection - State->FirstVisible) /
236 (State->LastVisible - State->FirstVisible);
237 } else {
238 State->CurrentSelection = State->InitialRow1;
239 } // if/else
240 } // if in first row
241 } else {
242 if (State->CurrentSelection < State->MaxIndex)
243 State->CurrentSelection++;
244 } // if/else
245 break;
246
247 case SCROLL_PAGE_UP:
248 if (State->CurrentSelection <= State->FinalRow0)
249 State->CurrentSelection -= State->MaxVisible;
250 else if (State->CurrentSelection == State->InitialRow1)
251 State->CurrentSelection = State->FinalRow0;
252 else
253 State->CurrentSelection = State->InitialRow1;
254 if (State->CurrentSelection < 0)
255 State->CurrentSelection = 0;
256 break;
257
258 case SCROLL_FIRST:
259 if (State->CurrentSelection > 0) {
260 State->PaintAll = TRUE;
261 State->CurrentSelection = 0;
262 }
263 break;
264
265 case SCROLL_PAGE_DOWN:
266 if (State->CurrentSelection < State->FinalRow0) {
267 State->CurrentSelection += State->MaxVisible;
268 if (State->CurrentSelection > State->FinalRow0)
269 State->CurrentSelection = State->FinalRow0;
270 } else if (State->CurrentSelection == State->FinalRow0) {
271 State->CurrentSelection++;
272 } else {
273 State->CurrentSelection = State->MaxIndex;
274 }
275 if (State->CurrentSelection > State->MaxIndex)
276 State->CurrentSelection = State->MaxIndex;
277 break;
278
279 case SCROLL_LAST:
280 if (State->CurrentSelection < State->MaxIndex) {
281 State->PaintAll = TRUE;
282 State->CurrentSelection = State->MaxIndex;
283 }
284 break;
285
286 case SCROLL_NONE:
287 break;
288
289 }
290 if (State->ScrollMode == SCROLL_MODE_TEXT)
291 AdjustScrollState(State);
292
293 if (!State->PaintAll && State->CurrentSelection != State->PreviousSelection)
294 State->PaintSelection = TRUE;
295 State->LastVisible = State->FirstVisible + State->MaxVisible - 1;
296 } // static VOID UpdateScroll()
297
298 //
299 // menu helper functions
300 //
301
302 VOID AddMenuInfoLine(IN REFIT_MENU_SCREEN *Screen, IN CHAR16 *InfoLine)
303 {
304 AddListElement((VOID ***) &(Screen->InfoLines), &(Screen->InfoLineCount), InfoLine);
305 }
306
307 VOID AddMenuEntry(IN REFIT_MENU_SCREEN *Screen, IN REFIT_MENU_ENTRY *Entry)
308 {
309 AddListElement((VOID ***) &(Screen->Entries), &(Screen->EntryCount), Entry);
310 }
311
312
313 static INTN FindMenuShortcutEntry(IN REFIT_MENU_SCREEN *Screen, IN CHAR16 *Shortcut)
314 {
315 UINTN i;
316
317 if (Shortcut == NULL)
318 return (-1);
319
320 if (StrLen(Shortcut) == 1) {
321 if (Shortcut[0] >= 'a' && Shortcut[0] <= 'z')
322 Shortcut[0] -= ('a' - 'A');
323 if (Shortcut[0]) {
324 for (i = 0; i < Screen->EntryCount; i++) {
325 if (Screen->Entries[i]->ShortcutDigit == Shortcut[0] || Screen->Entries[i]->ShortcutLetter == Shortcut[0]) {
326 return i;
327 } // if
328 } // for
329 } // if
330 } else if (StrLen(Shortcut) > 1) {
331 for (i = 0; i < Screen->EntryCount; i++) {
332 if (StriSubCmp(Shortcut, Screen->Entries[i]->Title))
333 return i;
334 } // for
335 }
336 return -1;
337 }
338
339 // Identify the end of row 0 and the beginning of row 1; store the results in the
340 // appropriate fields in State. Also reduce MaxVisible if that value is greater
341 // than the total number of row-0 tags and if we're in an icon-based screen
342 static VOID IdentifyRows(IN SCROLL_STATE *State, IN REFIT_MENU_SCREEN *Screen) {
343 UINTN i;
344
345 State->FinalRow0 = 0;
346 State->InitialRow1 = State->MaxIndex;
347 for (i = 0; i < State->MaxIndex; i++) {
348 if (Screen->Entries[i]->Row == 0) {
349 State->FinalRow0 = i;
350 } else if ((Screen->Entries[i]->Row == 1) && (State->InitialRow1 > i)) {
351 State->InitialRow1 = i;
352 } // if/else
353 } // for
354 if ((State->ScrollMode == SCROLL_MODE_ICONS) && (State->MaxVisible > (State->FinalRow0 + 1)))
355 State->MaxVisible = State->FinalRow0 + 1;
356 } // static VOID IdentifyRows()
357
358 //
359 // generic menu function
360 //
361 static UINTN RunGenericMenu(IN REFIT_MENU_SCREEN *Screen, IN MENU_STYLE_FUNC StyleFunc, IN INTN DefaultEntryIndex, OUT REFIT_MENU_ENTRY **ChosenEntry)
362 {
363 SCROLL_STATE State;
364 EFI_STATUS Status;
365 EFI_INPUT_KEY key;
366 UINTN index;
367 INTN ShortcutEntry;
368 BOOLEAN HaveTimeout = FALSE;
369 UINTN TimeoutCountdown = 0;
370 CHAR16 TimeoutMessage[256];
371 CHAR16 KeyAsString[2];
372 UINTN MenuExit;
373
374 if (Screen->TimeoutSeconds > 0) {
375 HaveTimeout = TRUE;
376 TimeoutCountdown = Screen->TimeoutSeconds * 10;
377 }
378 MenuExit = 0;
379
380 StyleFunc(Screen, &State, MENU_FUNCTION_INIT, NULL);
381 IdentifyRows(&State, Screen);
382 // override the starting selection with the default index, if any
383 if (DefaultEntryIndex >= 0 && DefaultEntryIndex <= State.MaxIndex) {
384 State.CurrentSelection = DefaultEntryIndex;
385 UpdateScroll(&State, SCROLL_NONE);
386 }
387 State.PaintAll = TRUE;
388
389 while (!MenuExit) {
390 // update the screen
391 if (State.PaintAll) {
392 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_ALL, NULL);
393 State.PaintAll = FALSE;
394 } else if (State.PaintSelection) {
395 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_SELECTION, NULL);
396 State.PaintSelection = FALSE;
397 }
398
399 if (HaveTimeout) {
400 SPrint(TimeoutMessage, 255, L"%s in %d seconds", Screen->TimeoutText, (TimeoutCountdown + 5) / 10);
401 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, TimeoutMessage);
402 }
403
404 // read key press (and wait for it if applicable)
405 Status = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
406 if (Status == EFI_NOT_READY) {
407 if (HaveTimeout && TimeoutCountdown == 0) {
408 // timeout expired
409 MenuExit = MENU_EXIT_TIMEOUT;
410 break;
411 } else if (HaveTimeout) {
412 refit_call1_wrapper(BS->Stall, 100000);
413 TimeoutCountdown--;
414 } else
415 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
416 continue;
417 }
418 if (HaveTimeout) {
419 // the user pressed a key, cancel the timeout
420 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, L"");
421 HaveTimeout = FALSE;
422 }
423
424 // react to key press
425 switch (key.ScanCode) {
426 case SCAN_UP:
427 UpdateScroll(&State, SCROLL_LINE_UP);
428 break;
429 case SCAN_LEFT:
430 UpdateScroll(&State, SCROLL_LINE_LEFT);
431 break;
432 case SCAN_DOWN:
433 UpdateScroll(&State, SCROLL_LINE_DOWN);
434 break;
435 case SCAN_RIGHT:
436 UpdateScroll(&State, SCROLL_LINE_RIGHT);
437 break;
438 case SCAN_HOME:
439 UpdateScroll(&State, SCROLL_FIRST);
440 break;
441 case SCAN_END:
442 UpdateScroll(&State, SCROLL_LAST);
443 break;
444 case SCAN_PAGE_UP:
445 UpdateScroll(&State, SCROLL_PAGE_UP);
446 break;
447 case SCAN_PAGE_DOWN:
448 UpdateScroll(&State, SCROLL_PAGE_DOWN);
449 break;
450 case SCAN_ESC:
451 MenuExit = MENU_EXIT_ESCAPE;
452 break;
453 case SCAN_INSERT:
454 case SCAN_F2:
455 MenuExit = MENU_EXIT_DETAILS;
456 break;
457 case SCAN_F10:
458 egScreenShot();
459 break;
460 case 0x0016: // F12
461 if (EjectMedia())
462 MenuExit = MENU_EXIT_ESCAPE;
463 break;
464 }
465 switch (key.UnicodeChar) {
466 case CHAR_LINEFEED:
467 case CHAR_CARRIAGE_RETURN:
468 case ' ':
469 MenuExit = MENU_EXIT_ENTER;
470 break;
471 case '+':
472 MenuExit = MENU_EXIT_DETAILS;
473 break;
474 default:
475 KeyAsString[0] = key.UnicodeChar;
476 KeyAsString[1] = 0;
477 ShortcutEntry = FindMenuShortcutEntry(Screen, KeyAsString);
478 if (ShortcutEntry >= 0) {
479 State.CurrentSelection = ShortcutEntry;
480 MenuExit = MENU_EXIT_ENTER;
481 }
482 break;
483 }
484 }
485
486 StyleFunc(Screen, &State, MENU_FUNCTION_CLEANUP, NULL);
487
488 if (ChosenEntry)
489 *ChosenEntry = Screen->Entries[State.CurrentSelection];
490 return MenuExit;
491 } /* static UINTN RunGenericMenu() */
492
493 //
494 // text-mode generic style
495 //
496
497 static VOID TextMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
498 {
499 INTN i;
500 UINTN MenuWidth, ItemWidth, MenuHeight;
501 static UINTN MenuPosY;
502 static CHAR16 **DisplayStrings;
503 CHAR16 TimeoutMessage[256];
504
505 State->ScrollMode = SCROLL_MODE_TEXT;
506 switch (Function) {
507
508 case MENU_FUNCTION_INIT:
509 // vertical layout
510 MenuPosY = 4;
511 if (Screen->InfoLineCount > 0)
512 MenuPosY += Screen->InfoLineCount + 1;
513 MenuHeight = ConHeight - MenuPosY - 2;
514 if (Screen->TimeoutSeconds > 0)
515 MenuHeight -= 2;
516 InitScroll(State, Screen->EntryCount, MenuHeight);
517
518 // determine width of the menu
519 MenuWidth = 20; // minimum
520 for (i = 0; i <= State->MaxIndex; i++) {
521 ItemWidth = StrLen(Screen->Entries[i]->Title);
522 if (MenuWidth < ItemWidth)
523 MenuWidth = ItemWidth;
524 }
525 MenuWidth += 2;
526 if (MenuWidth > ConWidth - 3)
527 MenuWidth = ConWidth - 3;
528
529 // prepare strings for display
530 DisplayStrings = AllocatePool(sizeof(CHAR16 *) * Screen->EntryCount);
531 for (i = 0; i <= State->MaxIndex; i++) {
532 // Note: Theoretically, SPrint() is a cleaner way to do this; but the
533 // description of the StrSize parameter to SPrint implies it's measured
534 // in characters, but in practice both TianoCore and GNU-EFI seem to
535 // use bytes instead, resulting in truncated displays. I could just
536 // double the size of the StrSize parameter, but that seems unsafe in
537 // case a future library change starts treating this as characters, so
538 // I'm doing it the hard way in this instance.
539 // TODO: Review the above and possibly change other uses of SPrint()
540 DisplayStrings[i] = AllocateZeroPool(2 * sizeof(CHAR16));
541 DisplayStrings[i][0] = L' ';
542 MergeStrings(&DisplayStrings[i], Screen->Entries[i]->Title, 0);
543 if (StrLen(DisplayStrings[i]) > MenuWidth)
544 DisplayStrings[i][MenuWidth - 1] = 0;
545 // DisplayStrings[i] = AllocateZeroPool(256 * sizeof(CHAR16));
546 // SPrint(DisplayStrings[i], ((MenuWidth < 255) ? MenuWidth : 255) * sizeof(CHAR16),
547 // L" %s ", Screen->Entries[i]->Title);
548 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
549 // TODO: account for double-width characters
550 } // for
551
552 // initial painting
553 BeginTextScreen(Screen->Title);
554 if (Screen->InfoLineCount > 0) {
555 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
556 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
557 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, 4 + i);
558 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->InfoLines[i]);
559 }
560 }
561
562 break;
563
564 case MENU_FUNCTION_CLEANUP:
565 // release temporary memory
566 for (i = 0; i <= State->MaxIndex; i++)
567 MyFreePool(DisplayStrings[i]);
568 MyFreePool(DisplayStrings);
569 break;
570
571 case MENU_FUNCTION_PAINT_ALL:
572 // paint the whole screen (initially and after scrolling)
573 for (i = 0; i <= State->MaxIndex; i++) {
574 if (i >= State->FirstVisible && i <= State->LastVisible) {
575 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (i - State->FirstVisible));
576 if (i == State->CurrentSelection)
577 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
578 else
579 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
580 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[i]);
581 }
582 }
583 // scrolling indicators
584 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_SCROLLARROW);
585 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY);
586 if (State->FirstVisible > 0)
587 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowUp);
588 else
589 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
590 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY + State->MaxVisible);
591 if (State->LastVisible < State->MaxIndex)
592 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowDown);
593 else
594 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
595 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
596 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 1);
597 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut,
598 L"arrow keys to move cursor; Enter to boot; Insert or F2 for more options");
599 }
600 break;
601
602 case MENU_FUNCTION_PAINT_SELECTION:
603 // redraw selection cursor
604 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (State->PreviousSelection - State->FirstVisible));
605 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
606 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->PreviousSelection]);
607 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (State->CurrentSelection - State->FirstVisible));
608 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
609 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->CurrentSelection]);
610 break;
611
612 case MENU_FUNCTION_PAINT_TIMEOUT:
613 if (ParamText[0] == 0) {
614 // clear message
615 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
616 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 2);
617 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, BlankLine + 1);
618 } else {
619 // paint or update message
620 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_ERROR);
621 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, ConHeight - 2);
622 SPrint(TimeoutMessage, 255, L"%s ", ParamText);
623 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, TimeoutMessage);
624 }
625 break;
626
627 }
628 }
629
630 //
631 // graphical generic style
632 //
633
634
635 static VOID DrawMenuText(IN CHAR16 *Text, IN UINTN SelectedWidth, IN UINTN XPos, IN UINTN YPos)
636 {
637 if (TextBuffer == NULL)
638 TextBuffer = egCreateImage(LAYOUT_TEXT_WIDTH, TEXT_LINE_HEIGHT, FALSE);
639
640 egFillImage(TextBuffer, &MenuBackgroundPixel);
641 if (SelectedWidth > 0) {
642 // draw selection bar background
643 egFillImageArea(TextBuffer, 0, 0, SelectedWidth, TextBuffer->Height,
644 &SelectionBackgroundPixel);
645 }
646
647 // render the text
648 egRenderText(Text, TextBuffer, TEXT_XMARGIN, TEXT_YMARGIN);
649 BltImage(TextBuffer, XPos, YPos);
650 }
651
652 // Displays sub-menus
653 static VOID GraphicsMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
654 {
655 INTN i;
656 UINTN ItemWidth;
657 static UINTN MenuWidth, EntriesPosX, EntriesPosY, TimeoutPosY;
658
659 State->ScrollMode = SCROLL_MODE_TEXT;
660 switch (Function) {
661
662 case MENU_FUNCTION_INIT:
663 InitScroll(State, Screen->EntryCount, 0);
664
665 // determine width of the menu
666 MenuWidth = 20; // minimum
667 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
668 ItemWidth = StrLen(Screen->InfoLines[i]);
669 if (MenuWidth < ItemWidth)
670 MenuWidth = ItemWidth;
671 }
672 for (i = 0; i <= State->MaxIndex; i++) {
673 ItemWidth = StrLen(Screen->Entries[i]->Title);
674 if (MenuWidth < ItemWidth)
675 MenuWidth = ItemWidth;
676 }
677 MenuWidth = TEXT_XMARGIN * 2 + MenuWidth * FONT_CELL_WIDTH;
678 if (MenuWidth > LAYOUT_TEXT_WIDTH)
679 MenuWidth = LAYOUT_TEXT_WIDTH;
680
681 if (Screen->TitleImage)
682 EntriesPosX = (UGAWidth + (Screen->TitleImage->Width + TITLEICON_SPACING) - MenuWidth) >> 1;
683 else
684 EntriesPosX = (UGAWidth - MenuWidth) >> 1;
685 EntriesPosY = ((UGAHeight - LAYOUT_TOTAL_HEIGHT) >> 1) + LAYOUT_BANNER_YOFFSET + TEXT_LINE_HEIGHT * 2;
686 TimeoutPosY = EntriesPosY + (Screen->EntryCount + 1) * TEXT_LINE_HEIGHT;
687
688 // initial painting
689 SwitchToGraphicsAndClear();
690 egMeasureText(Screen->Title, &ItemWidth, NULL);
691 DrawMenuText(Screen->Title, 0, ((UGAWidth - ItemWidth) >> 1) - TEXT_XMARGIN, EntriesPosY - TEXT_LINE_HEIGHT * 2);
692 if (Screen->TitleImage)
693 BltImageAlpha(Screen->TitleImage,
694 EntriesPosX - (Screen->TitleImage->Width + TITLEICON_SPACING), EntriesPosY,
695 &MenuBackgroundPixel);
696 if (Screen->InfoLineCount > 0) {
697 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
698 DrawMenuText(Screen->InfoLines[i], 0, EntriesPosX, EntriesPosY);
699 EntriesPosY += TEXT_LINE_HEIGHT;
700 }
701 EntriesPosY += TEXT_LINE_HEIGHT; // also add a blank line
702 }
703
704 break;
705
706 case MENU_FUNCTION_CLEANUP:
707 // nothing to do
708 break;
709
710 case MENU_FUNCTION_PAINT_ALL:
711 for (i = 0; i <= State->MaxIndex; i++) {
712 DrawMenuText(Screen->Entries[i]->Title, (i == State->CurrentSelection) ? MenuWidth : 0,
713 EntriesPosX, EntriesPosY + i * TEXT_LINE_HEIGHT);
714 }
715 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
716 DrawMenuText(L"arrow keys to move cursor; Enter to boot;", 0,
717 (UGAWidth - (41 * FONT_CELL_WIDTH)) / 2, UGAHeight - (FONT_CELL_HEIGHT * 3));
718 DrawMenuText(L"Insert or F2 to edit options", 0,
719 (UGAWidth - (28 * FONT_CELL_WIDTH)) / 2, UGAHeight - (FONT_CELL_HEIGHT * 2));
720 }
721 break;
722
723 case MENU_FUNCTION_PAINT_SELECTION:
724 // redraw selection cursor
725 DrawMenuText(Screen->Entries[State->PreviousSelection]->Title, 0,
726 EntriesPosX, EntriesPosY + State->PreviousSelection * TEXT_LINE_HEIGHT);
727 DrawMenuText(Screen->Entries[State->CurrentSelection]->Title, MenuWidth,
728 EntriesPosX, EntriesPosY + State->CurrentSelection * TEXT_LINE_HEIGHT);
729 break;
730
731 case MENU_FUNCTION_PAINT_TIMEOUT:
732 DrawMenuText(ParamText, 0, EntriesPosX, TimeoutPosY);
733 break;
734
735 }
736 } // static VOID GraphicsMenuStyle()
737
738 //
739 // graphical main menu style
740 //
741
742 static VOID DrawMainMenuEntry(REFIT_MENU_ENTRY *Entry, BOOLEAN selected, UINTN XPos, UINTN YPos)
743 {
744 UINTN ImageNum;
745
746 ImageNum = ((Entry->Row == 0) ? 0 : 2) + (selected ? 0 : 1);
747 if (SelectionImages != NULL)
748 BltImageCompositeBadge(SelectionImages[ImageNum],
749 Entry->Image, Entry->BadgeImage, XPos, YPos);
750 }
751
752 static VOID DrawMainMenuText(IN CHAR16 *Text, IN UINTN XPos, IN UINTN YPos)
753 {
754 UINTN TextWidth, TextPosX;
755
756 if (TextBuffer == NULL)
757 TextBuffer = egCreateImage(LAYOUT_TEXT_WIDTH, TEXT_LINE_HEIGHT, FALSE);
758
759 egFillImage(TextBuffer, &MenuBackgroundPixel);
760
761 // render the text
762 egMeasureText(Text, &TextWidth, NULL);
763 if (TextWidth > TextBuffer->Width)
764 TextPosX = 0;
765 else
766 TextPosX = (TextBuffer->Width - TextWidth) / 2;
767 egRenderText(Text, TextBuffer, TextPosX, 0);
768 // egRenderText(Text, TextBuffer, (TextBuffer->Width - TextWidth) >> 1, 0);
769 BltImage(TextBuffer, XPos, YPos);
770 }
771
772 static VOID PaintAll(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
773 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
774 INTN i;
775
776 if (Screen->Entries[State->CurrentSelection]->Row == 0)
777 AdjustScrollState(State);
778 for (i = State->FirstVisible; i <= State->MaxIndex; i++) {
779 if (Screen->Entries[i]->Row == 0) {
780 if (i <= State->LastVisible) {
781 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE,
782 itemPosX[i - State->FirstVisible], row0PosY);
783 } // if
784 } else {
785 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE, itemPosX[i], row1PosY);
786 }
787 }
788 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL))
789 DrawMainMenuText(Screen->Entries[State->CurrentSelection]->Title,
790 (UGAWidth - LAYOUT_TEXT_WIDTH) >> 1, textPosY);
791
792 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
793 DrawMainMenuText(L"arrow keys to move cursor; Enter to boot;",
794 (UGAWidth - LAYOUT_TEXT_WIDTH) / 2, UGAHeight - (FONT_CELL_HEIGHT * 3));
795 DrawMainMenuText(L"Insert or F2 for more options",
796 (UGAWidth - LAYOUT_TEXT_WIDTH) / 2, UGAHeight - (FONT_CELL_HEIGHT * 2));
797 } // if
798 } // static VOID PaintAll()
799
800 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
801 static VOID PaintSelection(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
802 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
803 UINTN XSelectPrev, XSelectCur, YPosPrev, YPosCur;
804
805 if (((State->CurrentSelection <= State->LastVisible) && (State->CurrentSelection >= State->FirstVisible)) ||
806 (State->CurrentSelection >= State->InitialRow1) ) {
807 if (Screen->Entries[State->PreviousSelection]->Row == 0) {
808 XSelectPrev = State->PreviousSelection - State->FirstVisible;
809 YPosPrev = row0PosY;
810 } else {
811 XSelectPrev = State->PreviousSelection;
812 YPosPrev = row1PosY;
813 } // if/else
814 if (Screen->Entries[State->CurrentSelection]->Row == 0) {
815 XSelectCur = State->CurrentSelection - State->FirstVisible;
816 YPosCur = row0PosY;
817 } else {
818 XSelectCur = State->CurrentSelection;
819 YPosCur = row1PosY;
820 } // if/else
821 DrawMainMenuEntry(Screen->Entries[State->PreviousSelection], FALSE, itemPosX[XSelectPrev], YPosPrev);
822 DrawMainMenuEntry(Screen->Entries[State->CurrentSelection], TRUE, itemPosX[XSelectCur], YPosCur);
823 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL))
824 DrawMainMenuText(Screen->Entries[State->CurrentSelection]->Title,
825 (UGAWidth - LAYOUT_TEXT_WIDTH) >> 1, textPosY);
826 } else { // Current selection not visible; must redraw the menu....
827 MainMenuStyle(Screen, State, MENU_FUNCTION_PAINT_ALL, NULL);
828 }
829 } // static VOID MoveSelection(VOID)
830
831 // Display an icon at the specified location. Uses the image specified by
832 // ExternalFilename if it's available, or BuiltInImage if it's not. The
833 // Y position is specified as the center value, and so is adjusted by half
834 // the icon's height. The X position is set along the icon's left
835 // edge if Alignment == ALIGN_LEFT, and along the right edge if
836 // Alignment == ALIGN_RIGHT
837 static VOID PaintIcon(IN EG_EMBEDDED_IMAGE *BuiltInIcon, IN CHAR16 *ExternalFilename, UINTN PosX, UINTN PosY, UINTN Alignment) {
838 EG_IMAGE *Icon = NULL;
839
840 if (FileExists(SelfDir, ExternalFilename))
841 Icon = egLoadIcon(SelfDir, ExternalFilename, 48);
842 if (Icon == NULL)
843 Icon = egPrepareEmbeddedImage(BuiltInIcon, TRUE);
844 if (Icon != NULL) {
845 if (Alignment == ALIGN_RIGHT)
846 PosX -= Icon->Width;
847 BltImageAlpha(Icon, PosX, PosY - (Icon->Height / 2), &MenuBackgroundPixel);
848 }
849 } // static VOID PaintIcon()
850
851 // Display main menu in graphics mode
852 VOID MainMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
853 {
854 INTN i;
855 static UINTN row0PosX, row0PosXRunning, row1PosY, row0Loaders;
856 UINTN row0Count, row1Count, row1PosX, row1PosXRunning;
857 static UINTN *itemPosX;
858 static UINTN row0PosY, textPosY;
859 CHAR16 FileName[256];
860
861 State->ScrollMode = SCROLL_MODE_ICONS;
862 switch (Function) {
863
864 case MENU_FUNCTION_INIT:
865 InitScroll(State, Screen->EntryCount, GlobalConfig.MaxTags);
866
867 // layout
868 row0Count = 0;
869 row1Count = 0;
870 row0Loaders = 0;
871 for (i = 0; i <= State->MaxIndex; i++) {
872 if (Screen->Entries[i]->Row == 1) {
873 row1Count++;
874 } else {
875 row0Loaders++;
876 if (row0Count < State->MaxVisible)
877 row0Count++;
878 }
879 }
880 row0PosX = (UGAWidth + TILE_XSPACING - (ROW0_TILESIZE + TILE_XSPACING) * row0Count) >> 1;
881 row0PosY = ((UGAHeight - LAYOUT_TOTAL_HEIGHT) >> 1) + LAYOUT_BANNER_YOFFSET;
882 row1PosX = (UGAWidth + TILE_XSPACING - (ROW1_TILESIZE + TILE_XSPACING) * row1Count) >> 1;
883 row1PosY = row0PosY + ROW0_TILESIZE + TILE_YSPACING;
884 if (row1Count > 0)
885 textPosY = row1PosY + ROW1_TILESIZE + TILE_YSPACING;
886 else
887 textPosY = row1PosY;
888
889 itemPosX = AllocatePool(sizeof(UINTN) * Screen->EntryCount);
890 row0PosXRunning = row0PosX;
891 row1PosXRunning = row1PosX;
892 for (i = 0; i <= State->MaxIndex; i++) {
893 if (Screen->Entries[i]->Row == 0) {
894 itemPosX[i] = row0PosXRunning;
895 row0PosXRunning += ROW0_TILESIZE + TILE_XSPACING;
896 } else {
897 itemPosX[i] = row1PosXRunning;
898 row1PosXRunning += ROW1_TILESIZE + TILE_XSPACING;
899 }
900 }
901 // initial painting
902 InitSelection();
903 SwitchToGraphicsAndClear();
904 break;
905
906 case MENU_FUNCTION_CLEANUP:
907 MyFreePool(itemPosX);
908 break;
909
910 case MENU_FUNCTION_PAINT_ALL:
911 BltClearScreen(TRUE);
912 PaintAll(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
913 // For PaintIcon() calls, the starting Y position is moved to the midpoint
914 // of the surrounding row; PaintIcon() adjusts this back up by half the
915 // icon's height to properly center it.
916 if ((State->FirstVisible > 0) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
917 SPrint(FileName, 255, L"%s\\arrow_left.icns", GlobalConfig.IconsDir ? GlobalConfig.IconsDir : DEFAULT_ICONS_DIR);
918 PaintIcon(&egemb_arrow_left, FileName, row0PosX - TILE_XSPACING,
919 row0PosY + (ROW0_TILESIZE / 2), ALIGN_RIGHT);
920 } // if
921 if ((State->LastVisible < (row0Loaders - 1)) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
922 SPrint(FileName, 255, L"%s\\arrow_right.icns", GlobalConfig.IconsDir ? GlobalConfig.IconsDir : DEFAULT_ICONS_DIR);
923 PaintIcon(&egemb_arrow_right, FileName,
924 (UGAWidth + (ROW0_TILESIZE + TILE_XSPACING) * State->MaxVisible) / 2 + TILE_XSPACING,
925 row0PosY + (ROW0_TILESIZE / 2), ALIGN_LEFT);
926 } // if
927 break;
928
929 case MENU_FUNCTION_PAINT_SELECTION:
930 PaintSelection(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
931 break;
932
933 case MENU_FUNCTION_PAINT_TIMEOUT:
934 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL))
935 DrawMainMenuText(ParamText, (UGAWidth - LAYOUT_TEXT_WIDTH) >> 1, textPosY + TEXT_LINE_HEIGHT);
936 break;
937
938 }
939 } // VOID MainMenuStyle()
940
941 // Enable the user to edit boot loader options.
942 // Returns TRUE if the user exited with edited options; FALSE if the user
943 // pressed Esc to terminate the edit.
944 static BOOLEAN EditOptions(LOADER_ENTRY *MenuEntry) {
945 UINTN x_max, y_max;
946 CHAR16 *EditedOptions;
947 BOOLEAN retval = FALSE;
948
949 refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max);
950
951 if (!GlobalConfig.TextOnly)
952 SwitchToText(TRUE);
953
954 if (line_edit(MenuEntry->LoadOptions, &EditedOptions, x_max)) {
955 MyFreePool(MenuEntry->LoadOptions);
956 MenuEntry->LoadOptions = EditedOptions;
957 retval = TRUE;
958 } // if
959 if (!GlobalConfig.TextOnly)
960 SwitchToGraphics();
961 return retval;
962 } // VOID EditOptions()
963
964 //
965 // user-callable dispatcher functions
966 //
967
968 UINTN RunMenu(IN REFIT_MENU_SCREEN *Screen, OUT REFIT_MENU_ENTRY **ChosenEntry)
969 {
970 MENU_STYLE_FUNC Style = TextMenuStyle;
971
972 if (AllowGraphicsMode)
973 Style = GraphicsMenuStyle;
974
975 return RunGenericMenu(Screen, Style, -1, ChosenEntry);
976 }
977
978 UINTN RunMainMenu(IN REFIT_MENU_SCREEN *Screen, IN CHAR16* DefaultSelection, OUT REFIT_MENU_ENTRY **ChosenEntry)
979 {
980 MENU_STYLE_FUNC Style = TextMenuStyle;
981 MENU_STYLE_FUNC MainStyle = TextMenuStyle;
982 REFIT_MENU_ENTRY *TempChosenEntry;
983 UINTN MenuExit = 0;
984 UINTN DefaultEntryIndex = -1;
985
986 if (DefaultSelection != NULL) {
987 // Find a menu entry that includes *DefaultSelection as a substring
988 DefaultEntryIndex = FindMenuShortcutEntry(Screen, DefaultSelection);
989 // If that didn't work, should we scan more characters? For now, no.
990 }
991
992 if (AllowGraphicsMode) {
993 Style = GraphicsMenuStyle;
994 MainStyle = MainMenuStyle;
995 }
996
997 while (!MenuExit) {
998 MenuExit = RunGenericMenu(Screen, MainStyle, DefaultEntryIndex, &TempChosenEntry);
999 Screen->TimeoutSeconds = 0;
1000
1001 if (MenuExit == MENU_EXIT_DETAILS) {
1002 if (TempChosenEntry->SubScreen != NULL) {
1003 MenuExit = RunGenericMenu(TempChosenEntry->SubScreen, Style, -1, &TempChosenEntry);
1004 if (MenuExit == MENU_EXIT_ESCAPE || TempChosenEntry->Tag == TAG_RETURN)
1005 MenuExit = 0;
1006 if (MenuExit == MENU_EXIT_DETAILS) {
1007 if (!EditOptions((LOADER_ENTRY *) TempChosenEntry))
1008 MenuExit = 0;
1009 } // if
1010 } else { // no sub-screen; ignore keypress
1011 MenuExit = 0;
1012 }
1013 } // Enter sub-screen
1014 }
1015
1016 if (ChosenEntry)
1017 *ChosenEntry = TempChosenEntry;
1018 return MenuExit;
1019 } /* UINTN RunMainMenu() */