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