]> code.delx.au - refind/blob - refind/menu.c
Fixed right- vs. left-alignment issues for text-mode menus
[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 // 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
547 } // for
548
549 // initial painting
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]);
556 }
557 }
558
559 break;
560
561 case MENU_FUNCTION_CLEANUP:
562 // release temporary memory
563 for (i = 0; i <= State->MaxIndex; i++)
564 MyFreePool(DisplayStrings[i]);
565 MyFreePool(DisplayStrings);
566 break;
567
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);
575 else
576 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
577 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[i]);
578 }
579 }
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);
585 else
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);
590 else
591 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
592 break;
593
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]);
602 break;
603
604 case MENU_FUNCTION_PAINT_TIMEOUT:
605 if (ParamText[0] == 0) {
606 // clear message
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);
610 } else {
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);
616 }
617 break;
618
619 }
620 }
621
622 //
623 // graphical generic style
624 //
625
626
627 static VOID DrawMenuText(IN CHAR16 *Text, IN UINTN SelectedWidth, IN UINTN XPos, IN UINTN YPos)
628 {
629 if (TextBuffer == NULL)
630 TextBuffer = egCreateImage(LAYOUT_TEXT_WIDTH, TEXT_LINE_HEIGHT, FALSE);
631
632 egFillImage(TextBuffer, &MenuBackgroundPixel);
633 if (SelectedWidth > 0) {
634 // draw selection bar background
635 egFillImageArea(TextBuffer, 0, 0, SelectedWidth, TextBuffer->Height,
636 &SelectionBackgroundPixel);
637 }
638
639 // render the text
640 egRenderText(Text, TextBuffer, TEXT_XMARGIN, TEXT_YMARGIN);
641 BltImage(TextBuffer, XPos, YPos);
642 }
643
644 // Displays sub-menus
645 static VOID GraphicsMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
646 {
647 INTN i;
648 UINTN ItemWidth;
649 static UINTN MenuWidth, EntriesPosX, EntriesPosY, TimeoutPosY;
650
651 State->ScrollMode = SCROLL_MODE_TEXT;
652 switch (Function) {
653
654 case MENU_FUNCTION_INIT:
655 InitScroll(State, Screen->EntryCount, 0);
656
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;
663 }
664 for (i = 0; i <= State->MaxIndex; i++) {
665 ItemWidth = StrLen(Screen->Entries[i]->Title);
666 if (MenuWidth < ItemWidth)
667 MenuWidth = ItemWidth;
668 }
669 MenuWidth = TEXT_XMARGIN * 2 + MenuWidth * FONT_CELL_WIDTH;
670 if (MenuWidth > LAYOUT_TEXT_WIDTH)
671 MenuWidth = LAYOUT_TEXT_WIDTH;
672
673 if (Screen->TitleImage)
674 EntriesPosX = (UGAWidth + (Screen->TitleImage->Width + TITLEICON_SPACING) - MenuWidth) >> 1;
675 else
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;
679
680 // initial painting
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;
692 }
693 EntriesPosY += TEXT_LINE_HEIGHT; // also add a blank line
694 }
695
696 break;
697
698 case MENU_FUNCTION_CLEANUP:
699 // nothing to do
700 break;
701
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);
706 }
707 break;
708
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);
715 break;
716
717 case MENU_FUNCTION_PAINT_TIMEOUT:
718 DrawMenuText(ParamText, 0, EntriesPosX, TimeoutPosY);
719 break;
720
721 }
722 } // static VOID GraphicsMenuStyle()
723
724 //
725 // graphical main menu style
726 //
727
728 static VOID DrawMainMenuEntry(REFIT_MENU_ENTRY *Entry, BOOLEAN selected, UINTN XPos, UINTN YPos)
729 {
730 UINTN ImageNum;
731
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);
736 }
737
738 static VOID DrawMainMenuText(IN CHAR16 *Text, IN UINTN XPos, IN UINTN YPos)
739 {
740 UINTN TextWidth, TextPosX;
741
742 if (TextBuffer == NULL)
743 TextBuffer = egCreateImage(LAYOUT_TEXT_WIDTH, TEXT_LINE_HEIGHT, FALSE);
744
745 egFillImage(TextBuffer, &MenuBackgroundPixel);
746
747 // render the text
748 egMeasureText(Text, &TextWidth, NULL);
749 if (TextWidth > TextBuffer->Width)
750 TextPosX = 0;
751 else
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);
756 }
757
758 static VOID PaintAll(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
759 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
760 INTN i;
761
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);
769 } // if
770 } else {
771 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE, itemPosX[i], row1PosY);
772 }
773 }
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()
778
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;
783
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;
788 YPosPrev = row0PosY;
789 } else {
790 XSelectPrev = State->PreviousSelection;
791 YPosPrev = row1PosY;
792 } // if/else
793 if (Screen->Entries[State->CurrentSelection]->Row == 0) {
794 XSelectCur = State->CurrentSelection - State->FirstVisible;
795 YPosCur = row0PosY;
796 } else {
797 XSelectCur = State->CurrentSelection;
798 YPosCur = row1PosY;
799 } // if/else
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);
807 }
808 } // static VOID MoveSelection(VOID)
809
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;
818
819 if (FileExists(SelfDir, ExternalFilename))
820 Icon = egLoadIcon(SelfDir, ExternalFilename, 48);
821 if (Icon == NULL)
822 Icon = egPrepareEmbeddedImage(BuiltInIcon, TRUE);
823 if (Icon != NULL) {
824 if (Alignment == ALIGN_RIGHT)
825 PosX -= Icon->Width;
826 BltImageAlpha(Icon, PosX, PosY - (Icon->Height / 2), &MenuBackgroundPixel);
827 }
828 } // static VOID PaintIcon()
829
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)
832 {
833 INTN i;
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];
839
840 State->ScrollMode = SCROLL_MODE_ICONS;
841 switch (Function) {
842
843 case MENU_FUNCTION_INIT:
844 InitScroll(State, Screen->EntryCount, GlobalConfig.MaxTags);
845
846 // layout
847 row0Count = 0;
848 row1Count = 0;
849 row0Loaders = 0;
850 for (i = 0; i <= State->MaxIndex; i++) {
851 if (Screen->Entries[i]->Row == 1) {
852 row1Count++;
853 } else {
854 row0Loaders++;
855 if (row0Count < State->MaxVisible)
856 row0Count++;
857 }
858 }
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;
863 if (row1Count > 0)
864 textPosY = row1PosY + ROW1_TILESIZE + TILE_YSPACING;
865 else
866 textPosY = row1PosY;
867
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;
875 } else {
876 itemPosX[i] = row1PosXRunning;
877 row1PosXRunning += ROW1_TILESIZE + TILE_XSPACING;
878 }
879 }
880 // initial painting
881 InitSelection();
882 SwitchToGraphicsAndClear();
883 break;
884
885 case MENU_FUNCTION_CLEANUP:
886 MyFreePool(itemPosX);
887 break;
888
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);
899 } // if
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);
905 } // if
906 break;
907
908 case MENU_FUNCTION_PAINT_SELECTION:
909 PaintSelection(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
910 break;
911
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);
915 break;
916
917 }
918 } // VOID MainMenuStyle()
919
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) {
924 UINTN x_max, y_max;
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;
929
930 refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max);
931
932 if (!GlobalConfig.TextOnly)
933 SwitchToText(TRUE);
934
935 egClearScreen(&DarkBackgroundPixel);
936
937 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, y_max - 1);
938 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, message);
939
940 if (line_edit(MenuEntry->LoadOptions, &EditedOptions, x_max, 1)) {
941 MyFreePool(MenuEntry->LoadOptions);
942 MenuEntry->LoadOptions = EditedOptions;
943 retval = TRUE;
944 } // if
945 if (!GlobalConfig.TextOnly)
946 SwitchToGraphics();
947 return retval;
948 } // VOID EditOptions()
949
950 //
951 // user-callable dispatcher functions
952 //
953
954 UINTN RunMenu(IN REFIT_MENU_SCREEN *Screen, OUT REFIT_MENU_ENTRY **ChosenEntry)
955 {
956 MENU_STYLE_FUNC Style = TextMenuStyle;
957
958 if (AllowGraphicsMode)
959 Style = GraphicsMenuStyle;
960
961 return RunGenericMenu(Screen, Style, -1, ChosenEntry);
962 }
963
964 UINTN RunMainMenu(IN REFIT_MENU_SCREEN *Screen, IN CHAR16* DefaultSelection, OUT REFIT_MENU_ENTRY **ChosenEntry)
965 {
966 MENU_STYLE_FUNC Style = TextMenuStyle;
967 MENU_STYLE_FUNC MainStyle = TextMenuStyle;
968 REFIT_MENU_ENTRY *TempChosenEntry;
969 UINTN MenuExit = 0;
970 UINTN DefaultEntryIndex = -1;
971
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.
976 }
977
978 if (AllowGraphicsMode) {
979 Style = GraphicsMenuStyle;
980 MainStyle = MainMenuStyle;
981 }
982
983 while (!MenuExit) {
984 MenuExit = RunGenericMenu(Screen, MainStyle, DefaultEntryIndex, &TempChosenEntry);
985 Screen->TimeoutSeconds = 0;
986
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)
990 MenuExit = 0;
991 if (MenuExit == MENU_EXIT_DETAILS) {
992 if (!EditOptions((LOADER_ENTRY *) TempChosenEntry))
993 MenuExit = 0;
994 } // if
995 }
996 }
997
998 if (ChosenEntry)
999 *ChosenEntry = TempChosenEntry;
1000 return MenuExit;
1001 } /* UINTN RunMainMenu() */