]> code.delx.au - refind/blob - refind/menu.c
d35cd11c4ab46d71a18007cab2f874f7f83c4818
[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 "refit_call_wrapper.h"
52
53 #include "egemb_back_selected_small.h"
54 #include "egemb_arrow_left.h"
55 #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 VOID FreeMenu(IN REFIT_MENU_SCREEN *Screen)
312 {
313 if (Screen->Entries)
314 FreePool(Screen->Entries);
315 }
316
317 static INTN FindMenuShortcutEntry(IN REFIT_MENU_SCREEN *Screen, IN CHAR16 *Shortcut)
318 {
319 UINTN i;
320
321 if (Shortcut == NULL)
322 return (-1);
323
324 if (StrLen(Shortcut) == 1) {
325 if (Shortcut[0] >= 'a' && Shortcut[0] <= 'z')
326 Shortcut[0] -= ('a' - 'A');
327 if (Shortcut[0]) {
328 for (i = 0; i < Screen->EntryCount; i++) {
329 if (Screen->Entries[i]->ShortcutDigit == Shortcut[0] || Screen->Entries[i]->ShortcutLetter == Shortcut[0]) {
330 return i;
331 } // if
332 } // for
333 } // if
334 } else if (StrLen(Shortcut) > 1) {
335 for (i = 0; i < Screen->EntryCount; i++) {
336 if (StriSubCmp(Shortcut, Screen->Entries[i]->Title))
337 return i;
338 } // for
339 }
340 return -1;
341 }
342
343 // Identify the end of row 0 and the beginning of row 1; store the results in the
344 // appropriate fields in State. Also reduce MaxVisible if that value is greater
345 // than the total number of row-0 tags and if we're in an icon-based screen
346 static VOID IdentifyRows(IN SCROLL_STATE *State, IN REFIT_MENU_SCREEN *Screen) {
347 UINTN i;
348
349 State->FinalRow0 = 0;
350 State->InitialRow1 = State->MaxIndex;
351 for (i = 0; i < State->MaxIndex; i++) {
352 if (Screen->Entries[i]->Row == 0) {
353 State->FinalRow0 = i;
354 } else if ((Screen->Entries[i]->Row == 1) && (State->InitialRow1 > i)) {
355 State->InitialRow1 = i;
356 } // if/else
357 } // for
358 if ((State->ScrollMode == SCROLL_MODE_ICONS) && (State->MaxVisible > (State->FinalRow0 + 1)))
359 State->MaxVisible = State->FinalRow0 + 1;
360 } // static VOID IdentifyRows()
361
362 //
363 // generic menu function
364 //
365 static UINTN RunGenericMenu(IN REFIT_MENU_SCREEN *Screen, IN MENU_STYLE_FUNC StyleFunc, IN INTN DefaultEntryIndex, OUT REFIT_MENU_ENTRY **ChosenEntry)
366 {
367 SCROLL_STATE State;
368 EFI_STATUS Status;
369 EFI_INPUT_KEY key;
370 UINTN index;
371 INTN ShortcutEntry;
372 BOOLEAN HaveTimeout = FALSE;
373 UINTN TimeoutCountdown = 0;
374 CHAR16 *TimeoutMessage;
375 CHAR16 KeyAsString[2];
376 UINTN MenuExit;
377
378 if (Screen->TimeoutSeconds > 0) {
379 HaveTimeout = TRUE;
380 TimeoutCountdown = Screen->TimeoutSeconds * 10;
381 }
382 MenuExit = 0;
383
384 StyleFunc(Screen, &State, MENU_FUNCTION_INIT, NULL);
385 IdentifyRows(&State, Screen);
386 // override the starting selection with the default index, if any
387 if (DefaultEntryIndex >= 0 && DefaultEntryIndex <= State.MaxIndex) {
388 State.CurrentSelection = DefaultEntryIndex;
389 UpdateScroll(&State, SCROLL_NONE);
390 }
391
392 while (!MenuExit) {
393 // update the screen
394 if (State.PaintAll) {
395 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_ALL, NULL);
396 State.PaintAll = FALSE;
397 } else if (State.PaintSelection) {
398 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_SELECTION, NULL);
399 State.PaintSelection = FALSE;
400 }
401
402 if (HaveTimeout) {
403 TimeoutMessage = PoolPrint(L"%s in %d seconds", Screen->TimeoutText, (TimeoutCountdown + 5) / 10);
404 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, TimeoutMessage);
405 FreePool(TimeoutMessage);
406 }
407
408 // read key press (and wait for it if applicable)
409 Status = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
410 if (Status == EFI_NOT_READY) {
411 if (HaveTimeout && TimeoutCountdown == 0) {
412 // timeout expired
413 MenuExit = MENU_EXIT_TIMEOUT;
414 break;
415 } else if (HaveTimeout) {
416 refit_call1_wrapper(BS->Stall, 100000);
417 TimeoutCountdown--;
418 } else
419 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
420 continue;
421 }
422 if (HaveTimeout) {
423 // the user pressed a key, cancel the timeout
424 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, L"");
425 HaveTimeout = FALSE;
426 }
427
428 // react to key press
429 switch (key.ScanCode) {
430 case SCAN_UP:
431 UpdateScroll(&State, SCROLL_LINE_UP);
432 break;
433 case SCAN_LEFT:
434 UpdateScroll(&State, SCROLL_LINE_LEFT);
435 break;
436 case SCAN_DOWN:
437 UpdateScroll(&State, SCROLL_LINE_DOWN);
438 break;
439 case SCAN_RIGHT:
440 UpdateScroll(&State, SCROLL_LINE_RIGHT);
441 break;
442 case SCAN_HOME:
443 UpdateScroll(&State, SCROLL_FIRST);
444 break;
445 case SCAN_END:
446 UpdateScroll(&State, SCROLL_LAST);
447 break;
448 case SCAN_PAGE_UP:
449 UpdateScroll(&State, SCROLL_PAGE_UP);
450 break;
451 case SCAN_PAGE_DOWN:
452 UpdateScroll(&State, SCROLL_PAGE_DOWN);
453 break;
454 case SCAN_ESC:
455 MenuExit = MENU_EXIT_ESCAPE;
456 break;
457 case SCAN_INSERT:
458 case SCAN_F2:
459 MenuExit = MENU_EXIT_DETAILS;
460 break;
461 case SCAN_F10:
462 egScreenShot();
463 break;
464 case 0x0016: // F12
465 if (EjectMedia())
466 MenuExit = MENU_EXIT_ESCAPE;
467 break;
468 }
469 switch (key.UnicodeChar) {
470 case CHAR_LINEFEED:
471 case CHAR_CARRIAGE_RETURN:
472 case ' ':
473 MenuExit = MENU_EXIT_ENTER;
474 break;
475 case '+':
476 MenuExit = MENU_EXIT_DETAILS;
477 break;
478 default:
479 KeyAsString[0] = key.UnicodeChar;
480 KeyAsString[1] = 0;
481 ShortcutEntry = FindMenuShortcutEntry(Screen, KeyAsString);
482 if (ShortcutEntry >= 0) {
483 State.CurrentSelection = ShortcutEntry;
484 MenuExit = MENU_EXIT_ENTER;
485 }
486 break;
487 }
488 }
489
490 StyleFunc(Screen, &State, MENU_FUNCTION_CLEANUP, NULL);
491
492 if (ChosenEntry)
493 *ChosenEntry = Screen->Entries[State.CurrentSelection];
494 return MenuExit;
495 } /* static UINTN RunGenericMenu( */
496
497 //
498 // text-mode generic style
499 //
500
501 static VOID TextMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
502 {
503 INTN i;
504 UINTN MenuWidth, ItemWidth, MenuHeight;
505 static UINTN MenuPosY;
506 static CHAR16 **DisplayStrings;
507 CHAR16 *TimeoutMessage;
508
509 State->ScrollMode = SCROLL_MODE_TEXT;
510 switch (Function) {
511
512 case MENU_FUNCTION_INIT:
513 // vertical layout
514 MenuPosY = 4;
515 if (Screen->InfoLineCount > 0)
516 MenuPosY += Screen->InfoLineCount + 1;
517 MenuHeight = ConHeight - MenuPosY;
518 if (Screen->TimeoutSeconds > 0)
519 MenuHeight -= 2;
520 InitScroll(State, Screen->EntryCount, MenuHeight);
521
522 // determine width of the menu
523 MenuWidth = 20; // minimum
524 for (i = 0; i <= State->MaxIndex; i++) {
525 ItemWidth = StrLen(Screen->Entries[i]->Title);
526 if (MenuWidth < ItemWidth)
527 MenuWidth = ItemWidth;
528 }
529 if (MenuWidth > ConWidth - 6)
530 MenuWidth = ConWidth - 6;
531
532 // prepare strings for display
533 DisplayStrings = AllocatePool(sizeof(CHAR16 *) * Screen->EntryCount);
534 for (i = 0; i <= State->MaxIndex; i++)
535 DisplayStrings[i] = PoolPrint(L" %-.*s ", MenuWidth, Screen->Entries[i]->Title);
536 // TODO: shorten strings that are too long (PoolPrint doesn't do that...)
537 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
538 // TODO: account for double-width characters
539
540 // initial painting
541 BeginTextScreen(Screen->Title);
542 if (Screen->InfoLineCount > 0) {
543 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
544 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
545 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, 4 + i);
546 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->InfoLines[i]);
547 }
548 }
549
550 break;
551
552 case MENU_FUNCTION_CLEANUP:
553 // release temporary memory
554 for (i = 0; i <= State->MaxIndex; i++)
555 FreePool(DisplayStrings[i]);
556 FreePool(DisplayStrings);
557 break;
558
559 case MENU_FUNCTION_PAINT_ALL:
560 // paint the whole screen (initially and after scrolling)
561 for (i = 0; i <= State->MaxIndex; i++) {
562 if (i >= State->FirstVisible && i <= State->LastVisible) {
563 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (i - State->FirstVisible));
564 if (i == State->CurrentSelection)
565 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
566 else
567 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
568 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[i]);
569 }
570 }
571 // scrolling indicators
572 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_SCROLLARROW);
573 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY);
574 if (State->FirstVisible > 0)
575 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowUp);
576 else
577 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
578 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY + State->MaxVisible);
579 if (State->LastVisible < State->MaxIndex)
580 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowDown);
581 else
582 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
583 break;
584
585 case MENU_FUNCTION_PAINT_SELECTION:
586 // redraw selection cursor
587 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (State->PreviousSelection - State->FirstVisible));
588 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
589 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->PreviousSelection]);
590 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (State->CurrentSelection - State->FirstVisible));
591 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
592 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->CurrentSelection]);
593 break;
594
595 case MENU_FUNCTION_PAINT_TIMEOUT:
596 if (ParamText[0] == 0) {
597 // clear message
598 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
599 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 1);
600 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, BlankLine + 1);
601 } else {
602 // paint or update message
603 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_ERROR);
604 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, ConHeight - 1);
605 TimeoutMessage = PoolPrint(L"%s ", ParamText);
606 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, TimeoutMessage);
607 FreePool(TimeoutMessage);
608 }
609 break;
610
611 }
612 }
613
614 //
615 // graphical generic style
616 //
617
618
619 static VOID DrawMenuText(IN CHAR16 *Text, IN UINTN SelectedWidth, IN UINTN XPos, IN UINTN YPos)
620 {
621 if (TextBuffer == NULL)
622 TextBuffer = egCreateImage(LAYOUT_TEXT_WIDTH, TEXT_LINE_HEIGHT, FALSE);
623
624 egFillImage(TextBuffer, &MenuBackgroundPixel);
625 if (SelectedWidth > 0) {
626 // draw selection bar background
627 egFillImageArea(TextBuffer, 0, 0, SelectedWidth, TextBuffer->Height,
628 &SelectionBackgroundPixel);
629 }
630
631 // render the text
632 egRenderText(Text, TextBuffer, TEXT_XMARGIN, TEXT_YMARGIN);
633 BltImage(TextBuffer, XPos, YPos);
634 }
635
636 // Displays sub-menus
637 static VOID GraphicsMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
638 {
639 INTN i;
640 UINTN ItemWidth;
641 static UINTN MenuWidth, EntriesPosX, EntriesPosY, TimeoutPosY;
642
643 State->ScrollMode = SCROLL_MODE_TEXT;
644 switch (Function) {
645
646 case MENU_FUNCTION_INIT:
647 InitScroll(State, Screen->EntryCount, 0);
648
649 // determine width of the menu
650 MenuWidth = 20; // minimum
651 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
652 ItemWidth = StrLen(Screen->InfoLines[i]);
653 if (MenuWidth < ItemWidth)
654 MenuWidth = ItemWidth;
655 }
656 for (i = 0; i <= State->MaxIndex; i++) {
657 ItemWidth = StrLen(Screen->Entries[i]->Title);
658 if (MenuWidth < ItemWidth)
659 MenuWidth = ItemWidth;
660 }
661 MenuWidth = TEXT_XMARGIN * 2 + MenuWidth * FONT_CELL_WIDTH;
662 if (MenuWidth > LAYOUT_TEXT_WIDTH)
663 MenuWidth = LAYOUT_TEXT_WIDTH;
664
665 if (Screen->TitleImage)
666 EntriesPosX = (UGAWidth + (Screen->TitleImage->Width + TITLEICON_SPACING) - MenuWidth) >> 1;
667 else
668 EntriesPosX = (UGAWidth - MenuWidth) >> 1;
669 EntriesPosY = ((UGAHeight - LAYOUT_TOTAL_HEIGHT) >> 1) + LAYOUT_BANNER_YOFFSET + TEXT_LINE_HEIGHT * 2;
670 TimeoutPosY = EntriesPosY + (Screen->EntryCount + 1) * TEXT_LINE_HEIGHT;
671
672 // initial painting
673 SwitchToGraphicsAndClear();
674 egMeasureText(Screen->Title, &ItemWidth, NULL);
675 DrawMenuText(Screen->Title, 0, ((UGAWidth - ItemWidth) >> 1) - TEXT_XMARGIN, EntriesPosY - TEXT_LINE_HEIGHT * 2);
676 if (Screen->TitleImage)
677 BltImageAlpha(Screen->TitleImage,
678 EntriesPosX - (Screen->TitleImage->Width + TITLEICON_SPACING), EntriesPosY,
679 &MenuBackgroundPixel);
680 if (Screen->InfoLineCount > 0) {
681 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
682 DrawMenuText(Screen->InfoLines[i], 0, EntriesPosX, EntriesPosY);
683 EntriesPosY += TEXT_LINE_HEIGHT;
684 }
685 EntriesPosY += TEXT_LINE_HEIGHT; // also add a blank line
686 }
687
688 break;
689
690 case MENU_FUNCTION_CLEANUP:
691 // nothing to do
692 break;
693
694 case MENU_FUNCTION_PAINT_ALL:
695 for (i = 0; i <= State->MaxIndex; i++) {
696 DrawMenuText(Screen->Entries[i]->Title, (i == State->CurrentSelection) ? MenuWidth : 0,
697 EntriesPosX, EntriesPosY + i * TEXT_LINE_HEIGHT);
698 }
699 break;
700
701 case MENU_FUNCTION_PAINT_SELECTION:
702 // redraw selection cursor
703 DrawMenuText(Screen->Entries[State->PreviousSelection]->Title, 0,
704 EntriesPosX, EntriesPosY + State->PreviousSelection * TEXT_LINE_HEIGHT);
705 DrawMenuText(Screen->Entries[State->CurrentSelection]->Title, MenuWidth,
706 EntriesPosX, EntriesPosY + State->CurrentSelection * TEXT_LINE_HEIGHT);
707 break;
708
709 case MENU_FUNCTION_PAINT_TIMEOUT:
710 DrawMenuText(ParamText, 0, EntriesPosX, TimeoutPosY);
711 break;
712
713 }
714 }
715
716 //
717 // graphical main menu style
718 //
719
720 static VOID DrawMainMenuEntry(REFIT_MENU_ENTRY *Entry, BOOLEAN selected, UINTN XPos, UINTN YPos)
721 {
722 UINTN ImageNum;
723
724 ImageNum = ((Entry->Row == 0) ? 0 : 2) + (selected ? 0 : 1);
725 if (SelectionImages != NULL)
726 BltImageCompositeBadge(SelectionImages[ImageNum],
727 Entry->Image, Entry->BadgeImage, XPos, YPos);
728 }
729
730 static VOID DrawMainMenuText(IN CHAR16 *Text, IN UINTN XPos, IN UINTN YPos)
731 {
732 UINTN TextWidth, TextPosX;
733
734 if (TextBuffer == NULL)
735 TextBuffer = egCreateImage(LAYOUT_TEXT_WIDTH, TEXT_LINE_HEIGHT, FALSE);
736
737 egFillImage(TextBuffer, &MenuBackgroundPixel);
738
739 // render the text
740 egMeasureText(Text, &TextWidth, NULL);
741 if (TextWidth > TextBuffer->Width)
742 TextPosX = 0;
743 else
744 TextPosX = (TextBuffer->Width - TextWidth) / 2;
745 egRenderText(Text, TextBuffer, TextPosX, 0);
746 // egRenderText(Text, TextBuffer, (TextBuffer->Width - TextWidth) >> 1, 0);
747 BltImage(TextBuffer, XPos, YPos);
748 }
749
750 static VOID PaintAll(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
751 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
752 INTN i;
753
754 if (Screen->Entries[State->CurrentSelection]->Row == 0)
755 AdjustScrollState(State);
756 for (i = State->FirstVisible; i <= State->MaxIndex; i++) {
757 if (Screen->Entries[i]->Row == 0) {
758 if (i <= State->LastVisible) {
759 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE,
760 itemPosX[i - State->FirstVisible], row0PosY);
761 } // if
762 } else {
763 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE, itemPosX[i], row1PosY);
764 }
765 }
766 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL))
767 DrawMainMenuText(Screen->Entries[State->CurrentSelection]->Title,
768 (UGAWidth - LAYOUT_TEXT_WIDTH) >> 1, textPosY);
769 } // static VOID PaintAll()
770
771 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
772 static VOID PaintSelection(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
773 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
774 UINTN XSelectPrev, XSelectCur, YPosPrev, YPosCur;
775
776 if (((State->CurrentSelection <= State->LastVisible) && (State->CurrentSelection >= State->FirstVisible)) ||
777 (State->CurrentSelection >= State->InitialRow1) ) {
778 if (Screen->Entries[State->PreviousSelection]->Row == 0) {
779 XSelectPrev = State->PreviousSelection - State->FirstVisible;
780 YPosPrev = row0PosY;
781 } else {
782 XSelectPrev = State->PreviousSelection;
783 YPosPrev = row1PosY;
784 } // if/else
785 if (Screen->Entries[State->CurrentSelection]->Row == 0) {
786 XSelectCur = State->CurrentSelection - State->FirstVisible;
787 YPosCur = row0PosY;
788 } else {
789 XSelectCur = State->CurrentSelection;
790 YPosCur = row1PosY;
791 } // if/else
792 DrawMainMenuEntry(Screen->Entries[State->PreviousSelection], FALSE, itemPosX[XSelectPrev], YPosPrev);
793 DrawMainMenuEntry(Screen->Entries[State->CurrentSelection], TRUE, itemPosX[XSelectCur], YPosCur);
794 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL))
795 DrawMainMenuText(Screen->Entries[State->CurrentSelection]->Title,
796 (UGAWidth - LAYOUT_TEXT_WIDTH) >> 1, textPosY);
797 } else { // Current selection not visible; must redraw the menu....
798 MainMenuStyle(Screen, State, MENU_FUNCTION_PAINT_ALL, NULL);
799 }
800 } // static VOID MoveSelection(VOID)
801
802 // Display an icon at the specified location. Uses the image specified by
803 // ExternalFilename if it's available, or BuiltInImage if it's not. The
804 // Y position is specified as the center value, and so is adjusted by half
805 // the icon's height. The X position is set along the icon's left
806 // edge if Alignment == ALIGN_LEFT, and along the right edge if
807 // Alignment == ALIGN_RIGHT
808 static VOID PaintIcon(IN EG_EMBEDDED_IMAGE *BuiltInIcon, IN CHAR16 *ExternalFilename, UINTN PosX, UINTN PosY, UINTN Alignment) {
809 EG_IMAGE *Icon = NULL;
810
811 if (FileExists(SelfDir, ExternalFilename))
812 Icon = egLoadIcon(SelfDir, ExternalFilename, 48);
813 if (Icon == NULL)
814 Icon = egPrepareEmbeddedImage(BuiltInIcon, TRUE);
815 if (Icon != NULL) {
816 if (Alignment == ALIGN_RIGHT)
817 PosX -= Icon->Width;
818 BltImageAlpha(Icon, PosX, PosY - (Icon->Height / 2), &MenuBackgroundPixel);
819 }
820 } // static VOID PaintIcon()
821
822 // Display main menu in graphics mode
823 VOID MainMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
824 {
825 INTN i;
826 static UINTN row0PosX, row0PosXRunning, row1PosY, row0Loaders;
827 UINTN row0Count, row1Count, row1PosX, row1PosXRunning;
828 static UINTN *itemPosX;
829 static UINTN row0PosY, textPosY;
830 CHAR16 FileName[256];
831
832 State->ScrollMode = SCROLL_MODE_ICONS;
833 switch (Function) {
834
835 case MENU_FUNCTION_INIT:
836 InitScroll(State, Screen->EntryCount, GlobalConfig.MaxTags);
837
838 // layout
839 row0Count = 0;
840 row1Count = 0;
841 row0Loaders = 0;
842 for (i = 0; i <= State->MaxIndex; i++) {
843 if (Screen->Entries[i]->Row == 1) {
844 row1Count++;
845 } else {
846 row0Loaders++;
847 if (row0Count < State->MaxVisible)
848 row0Count++;
849 }
850 }
851 row0PosX = (UGAWidth + TILE_XSPACING - (ROW0_TILESIZE + TILE_XSPACING) * row0Count) >> 1;
852 row0PosY = ((UGAHeight - LAYOUT_TOTAL_HEIGHT) >> 1) + LAYOUT_BANNER_YOFFSET;
853 row1PosX = (UGAWidth + TILE_XSPACING - (ROW1_TILESIZE + TILE_XSPACING) * row1Count) >> 1;
854 row1PosY = row0PosY + ROW0_TILESIZE + TILE_YSPACING;
855 if (row1Count > 0)
856 textPosY = row1PosY + ROW1_TILESIZE + TILE_YSPACING;
857 else
858 textPosY = row1PosY;
859
860 itemPosX = AllocatePool(sizeof(UINTN) * Screen->EntryCount);
861 row0PosXRunning = row0PosX;
862 row1PosXRunning = row1PosX;
863 for (i = 0; i <= State->MaxIndex; i++) {
864 if (Screen->Entries[i]->Row == 0) {
865 itemPosX[i] = row0PosXRunning;
866 row0PosXRunning += ROW0_TILESIZE + TILE_XSPACING;
867 } else {
868 itemPosX[i] = row1PosXRunning;
869 row1PosXRunning += ROW1_TILESIZE + TILE_XSPACING;
870 }
871 }
872 // initial painting
873 InitSelection();
874 SwitchToGraphicsAndClear();
875 break;
876
877 case MENU_FUNCTION_CLEANUP:
878 FreePool(itemPosX);
879 break;
880
881 case MENU_FUNCTION_PAINT_ALL:
882 BltClearScreen(TRUE);
883 PaintAll(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
884 // For PaintIcon() calls, the starting Y position is moved to the midpoint
885 // of the surrounding row; PaintIcon() adjusts this back up by half the
886 // icon's height to properly center it.
887 if ((State->FirstVisible > 0) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
888 SPrint(FileName, 255, L"%s\\arrow_left.icns", GlobalConfig.IconsDir ? GlobalConfig.IconsDir : DEFAULT_ICONS_DIR);
889 PaintIcon(&egemb_arrow_left, FileName, row0PosX - TILE_XSPACING,
890 row0PosY + (ROW0_TILESIZE / 2), ALIGN_RIGHT);
891 } // if
892 if ((State->LastVisible < (row0Loaders - 1)) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
893 SPrint(FileName, 255, L"%s\\arrow_right.icns", GlobalConfig.IconsDir ? GlobalConfig.IconsDir : DEFAULT_ICONS_DIR);
894 PaintIcon(&egemb_arrow_right, FileName,
895 (UGAWidth + (ROW0_TILESIZE + TILE_XSPACING) * State->MaxVisible) / 2 + TILE_XSPACING,
896 row0PosY + (ROW0_TILESIZE / 2), ALIGN_LEFT);
897 } // if
898 break;
899
900 case MENU_FUNCTION_PAINT_SELECTION:
901 PaintSelection(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
902 break;
903
904 case MENU_FUNCTION_PAINT_TIMEOUT:
905 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL))
906 DrawMainMenuText(ParamText, (UGAWidth - LAYOUT_TEXT_WIDTH) >> 1, textPosY + TEXT_LINE_HEIGHT);
907 break;
908
909 }
910 }
911
912 //
913 // user-callable dispatcher functions
914 //
915
916 UINTN RunMenu(IN REFIT_MENU_SCREEN *Screen, OUT REFIT_MENU_ENTRY **ChosenEntry)
917 {
918 MENU_STYLE_FUNC Style = TextMenuStyle;
919
920 if (AllowGraphicsMode)
921 Style = GraphicsMenuStyle;
922
923 return RunGenericMenu(Screen, Style, -1, ChosenEntry);
924 }
925
926 UINTN RunMainMenu(IN REFIT_MENU_SCREEN *Screen, IN CHAR16* DefaultSelection, OUT REFIT_MENU_ENTRY **ChosenEntry)
927 {
928 MENU_STYLE_FUNC Style = TextMenuStyle;
929 MENU_STYLE_FUNC MainStyle = TextMenuStyle;
930 REFIT_MENU_ENTRY *TempChosenEntry;
931 UINTN MenuExit = 0;
932 UINTN DefaultEntryIndex = -1;
933
934 if (DefaultSelection != NULL) {
935 // Find a menu entry that includes *DefaultSelection as a substring
936 DefaultEntryIndex = FindMenuShortcutEntry(Screen, DefaultSelection);
937 // If that didn't work, should we scan more characters? For now, no.
938 }
939
940 if (AllowGraphicsMode) {
941 Style = GraphicsMenuStyle;
942 MainStyle = MainMenuStyle;
943 }
944
945 while (!MenuExit) {
946 MenuExit = RunGenericMenu(Screen, MainStyle, DefaultEntryIndex, &TempChosenEntry);
947 Screen->TimeoutSeconds = 0;
948
949 if (MenuExit == MENU_EXIT_DETAILS && TempChosenEntry->SubScreen != NULL) {
950 MenuExit = RunGenericMenu(TempChosenEntry->SubScreen, Style, -1, &TempChosenEntry);
951 if (MenuExit == MENU_EXIT_ESCAPE || TempChosenEntry->Tag == TAG_RETURN)
952 MenuExit = 0;
953 }
954 }
955
956 if (ChosenEntry)
957 *ChosenEntry = TempChosenEntry;
958 return MenuExit;
959 } /* UINTN RunMainMenu() */