3 * Screen handling functions
5 * Copyright (c) 2006 Christoph Pfisterer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
37 * Modifications copyright (c) 2012 Roderick W. Smith
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.
50 #include "../include/refit_call_wrapper.h"
52 #include "../include/egemb_refind_banner.h"
54 // Console defines and variables
58 CHAR16
*BlankLine
= NULL
;
60 static VOID
DrawScreenHeader(IN CHAR16
*Title
);
62 // UGA defines and variables
66 BOOLEAN AllowGraphicsMode
;
68 EG_PIXEL StdBackgroundPixel
= { 0xbf, 0xbf, 0xbf, 0 };
69 EG_PIXEL MenuBackgroundPixel
= { 0xbf, 0xbf, 0xbf, 0 };
70 EG_PIXEL DarkBackgroundPixel
= { 0x0, 0x0, 0x0, 0 };
72 static BOOLEAN GraphicsScreenDirty
;
74 // general defines and variables
76 static BOOLEAN haveError
= FALSE
;
78 static VOID
PrepareBlankLine(VOID
) {
81 MyFreePool(BlankLine
);
82 // make a buffer for a whole text line
83 BlankLine
= AllocatePool((ConWidth
+ 1) * sizeof(CHAR16
));
84 for (i
= 0; i
< ConWidth
; i
++)
90 // Screen initialization and switching
98 if (egHasGraphicsMode()) {
99 egGetScreenSize(&UGAWidth
, &UGAHeight
);
100 AllowGraphicsMode
= TRUE
;
102 AllowGraphicsMode
= FALSE
;
103 egSetGraphicsModeEnabled(FALSE
); // just to be sure we are in text mode
105 GraphicsScreenDirty
= TRUE
;
108 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, FALSE
);
110 // get size of text console
111 if (refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &ConWidth
, &ConHeight
) != EFI_SUCCESS
) {
112 // use default values on error
119 // show the banner (even when in graphics mode)
120 DrawScreenHeader(L
"Initializing...");
123 VOID
SetupScreen(VOID
)
125 GlobalConfig
.RequestedTextMode
= egSetTextMode(GlobalConfig
.RequestedTextMode
);
127 if ((GlobalConfig
.RequestedScreenWidth
> 0) &&
128 (egSetScreenSize(&GlobalConfig
.RequestedScreenWidth
, &GlobalConfig
.RequestedScreenHeight
))) {
129 UGAWidth
= GlobalConfig
.RequestedScreenWidth
;
130 UGAHeight
= GlobalConfig
.RequestedScreenHeight
;
131 } // if user requested a particular screen resolution
133 if (GlobalConfig
.TextOnly
) {
134 // switch to text mode if requested
135 AllowGraphicsMode
= FALSE
;
138 } else if (AllowGraphicsMode
) {
139 // clear screen and show banner
140 // (now we know we'll stay in graphics mode)
142 BltClearScreen(TRUE
);
146 VOID
SwitchToText(IN BOOLEAN CursorEnabled
)
148 egSetGraphicsModeEnabled(FALSE
);
149 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, CursorEnabled
);
150 // get size of text console
151 if (refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &ConWidth
, &ConHeight
) != EFI_SUCCESS
) {
152 // use default values on error
159 VOID
SwitchToGraphics(VOID
)
161 if (AllowGraphicsMode
&& !egIsGraphicsModeEnabled()) {
162 egSetGraphicsModeEnabled(TRUE
);
163 GraphicsScreenDirty
= TRUE
;
168 // Screen control for running tools
171 VOID
BeginTextScreen(IN CHAR16
*Title
)
173 DrawScreenHeader(Title
);
180 VOID
FinishTextScreen(IN BOOLEAN WaitAlways
)
182 if (haveError
|| WaitAlways
) {
191 VOID
BeginExternalScreen(IN BOOLEAN UseGraphicsMode
, IN CHAR16
*Title
)
193 if (!AllowGraphicsMode
)
194 UseGraphicsMode
= FALSE
;
196 if (UseGraphicsMode
) {
198 BltClearScreen(FALSE
);
200 egClearScreen(&DarkBackgroundPixel
);
201 DrawScreenHeader(Title
);
204 if (!UseGraphicsMode
)
211 VOID
FinishExternalScreen(VOID
)
213 // make sure we clean up later
214 GraphicsScreenDirty
= TRUE
;
221 // Reset the screen resolution, in case external program changed it....
228 VOID
TerminateScreen(VOID
)
231 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
232 refit_call1_wrapper(ST
->ConOut
->ClearScreen
, ST
->ConOut
);
235 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, TRUE
);
238 static VOID
DrawScreenHeader(IN CHAR16
*Title
)
242 // clear to black background
243 egClearScreen(&DarkBackgroundPixel
); // first clear in graphics mode
244 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
245 refit_call1_wrapper(ST
->ConOut
->ClearScreen
, ST
->ConOut
); // then clear in text mode
247 // paint header background
248 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BANNER
);
249 for (y
= 0; y
< 3; y
++) {
250 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, y
);
255 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 1);
256 Print(L
"rEFInd - %s", Title
);
259 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
260 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, 4);
267 static BOOLEAN
ReadAllKeyStrokes(VOID
)
269 BOOLEAN GotKeyStrokes
;
273 GotKeyStrokes
= FALSE
;
275 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
276 if (Status
== EFI_SUCCESS
) {
277 GotKeyStrokes
= TRUE
;
282 return GotKeyStrokes
;
285 VOID
PauseForKey(VOID
)
289 Print(L
"\n* Hit any key to continue *");
291 if (ReadAllKeyStrokes()) { // remove buffered key strokes
292 refit_call1_wrapper(BS
->Stall
, 5000000); // 5 seconds delay
293 ReadAllKeyStrokes(); // empty the buffer again
296 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
297 ReadAllKeyStrokes(); // empty the buffer to protect the menu
303 VOID
DebugPause(VOID
)
305 // show console and wait for key
314 VOID
EndlessIdleLoop(VOID
)
320 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
328 #ifdef __MAKEWITH_GNUEFI
329 BOOLEAN
CheckFatalError(IN EFI_STATUS Status
, IN CHAR16
*where
)
331 CHAR16 ErrorName
[64];
333 if (!EFI_ERROR(Status
))
336 StatusToString(ErrorName
, Status
);
337 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
338 Print(L
"Fatal Error: %s %s\n", ErrorName
, where
);
339 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
342 //BS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
347 BOOLEAN
CheckError(IN EFI_STATUS Status
, IN CHAR16
*where
)
349 CHAR16 ErrorName
[64];
351 if (!EFI_ERROR(Status
))
354 StatusToString(ErrorName
, Status
);
355 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
356 Print(L
"Error: %s %s\n", ErrorName
, where
);
357 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
363 BOOLEAN
CheckFatalError(IN EFI_STATUS Status
, IN CHAR16
*where
)
365 // CHAR16 ErrorName[64];
367 if (!EFI_ERROR(Status
))
370 // StatusToString(ErrorName, Status);
371 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_ERROR
);
372 Print(L
"Fatal Error: %r %s\n", Status
, where
);
373 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_BASIC
);
376 //gBS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
381 BOOLEAN
CheckError(IN EFI_STATUS Status
, IN CHAR16
*where
)
383 // CHAR16 ErrorName[64];
385 if (!EFI_ERROR(Status
))
388 // StatusToString(ErrorName, Status);
389 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_ERROR
);
390 Print(L
"Error: %r %s\n", Status
, where
);
391 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_BASIC
);
399 // Graphics functions
402 VOID
SwitchToGraphicsAndClear(VOID
)
405 if (GraphicsScreenDirty
)
406 BltClearScreen(TRUE
);
409 VOID
BltClearScreen(IN BOOLEAN ShowBanner
)
411 static EG_IMAGE
*Banner
= NULL
;
413 if (ShowBanner
&& !(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_BANNER
)) {
414 // load banner on first call
415 if (Banner
== NULL
) {
416 if (GlobalConfig
.BannerFileName
== NULL
)
417 Banner
= egPrepareEmbeddedImage(&egemb_refind_banner
, FALSE
);
419 Banner
= egLoadImage(SelfDir
, GlobalConfig
.BannerFileName
, FALSE
);
421 MenuBackgroundPixel
= Banner
->PixelData
[0];
424 // clear and draw banner
425 egClearScreen(&MenuBackgroundPixel
);
427 BltImage(Banner
, (UGAWidth
- Banner
->Width
) >> 1,
428 ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_HEIGHT
- Banner
->Height
);
431 // clear to standard background color
432 egClearScreen(&StdBackgroundPixel
);
435 GraphicsScreenDirty
= FALSE
;
438 VOID
BltImage(IN EG_IMAGE
*Image
, IN UINTN XPos
, IN UINTN YPos
)
440 egDrawImage(Image
, XPos
, YPos
);
441 GraphicsScreenDirty
= TRUE
;
444 VOID
BltImageAlpha(IN EG_IMAGE
*Image
, IN UINTN XPos
, IN UINTN YPos
, IN EG_PIXEL
*BackgroundPixel
)
448 // compose on background
449 CompImage
= egCreateFilledImage(Image
->Width
, Image
->Height
, FALSE
, BackgroundPixel
);
450 egComposeImage(CompImage
, Image
, 0, 0);
452 // blit to screen and clean up
453 egDrawImage(CompImage
, XPos
, YPos
);
454 egFreeImage(CompImage
);
455 GraphicsScreenDirty
= TRUE
;
458 // VOID BltImageComposite(IN EG_IMAGE *BaseImage, IN EG_IMAGE *TopImage, IN UINTN XPos, IN UINTN YPos)
460 // UINTN TotalWidth, TotalHeight, CompWidth, CompHeight, OffsetX, OffsetY;
461 // EG_IMAGE *CompImage;
463 // // initialize buffer with base image
464 // CompImage = egCopyImage(BaseImage);
465 // TotalWidth = BaseImage->Width;
466 // TotalHeight = BaseImage->Height;
468 // // place the top image
469 // CompWidth = TopImage->Width;
470 // if (CompWidth > TotalWidth)
471 // CompWidth = TotalWidth;
472 // OffsetX = (TotalWidth - CompWidth) >> 1;
473 // CompHeight = TopImage->Height;
474 // if (CompHeight > TotalHeight)
475 // CompHeight = TotalHeight;
476 // OffsetY = (TotalHeight - CompHeight) >> 1;
477 // egComposeImage(CompImage, TopImage, OffsetX, OffsetY);
479 // // blit to screen and clean up
480 // egDrawImage(CompImage, XPos, YPos);
481 // egFreeImage(CompImage);
482 // GraphicsScreenDirty = TRUE;
485 VOID
BltImageCompositeBadge(IN EG_IMAGE
*BaseImage
, IN EG_IMAGE
*TopImage
, IN EG_IMAGE
*BadgeImage
, IN UINTN XPos
, IN UINTN YPos
)
487 UINTN TotalWidth
= 0, TotalHeight
= 0, CompWidth
= 0, CompHeight
= 0, OffsetX
= 0, OffsetY
= 0;
488 EG_IMAGE
*CompImage
= NULL
;
490 // initialize buffer with base image
491 if (BaseImage
!= NULL
) {
492 CompImage
= egCopyImage(BaseImage
);
493 TotalWidth
= BaseImage
->Width
;
494 TotalHeight
= BaseImage
->Height
;
497 // place the top image
498 if ((TopImage
!= NULL
) && (CompImage
!= NULL
)) {
499 CompWidth
= TopImage
->Width
;
500 if (CompWidth
> TotalWidth
)
501 CompWidth
= TotalWidth
;
502 OffsetX
= (TotalWidth
- CompWidth
) >> 1;
503 CompHeight
= TopImage
->Height
;
504 if (CompHeight
> TotalHeight
)
505 CompHeight
= TotalHeight
;
506 OffsetY
= (TotalHeight
- CompHeight
) >> 1;
507 egComposeImage(CompImage
, TopImage
, OffsetX
, OffsetY
);
510 // place the badge image
511 if (BadgeImage
!= NULL
&& CompImage
!= NULL
&& (BadgeImage
->Width
+ 8) < CompWidth
&& (BadgeImage
->Height
+ 8) < CompHeight
) {
512 OffsetX
+= CompWidth
- 8 - BadgeImage
->Width
;
513 OffsetY
+= CompHeight
- 8 - BadgeImage
->Height
;
514 egComposeImage(CompImage
, BadgeImage
, OffsetX
, OffsetY
);
517 // blit to screen and clean up
518 egDrawImage(CompImage
, XPos
, YPos
);
519 egFreeImage(CompImage
);
520 GraphicsScreenDirty
= TRUE
;
523 // Line-editing functions borrowed from gummiboot (cursor_left(), cursor_right(), & line_edit()).
524 // gummiboot is copyright (c) 2012 by Kay Sievers <kay.sievers@vrfy.org> and Harald Hoyer
525 // <harald@redhat.com> and is licensed under the LGPL 2.1.
527 static void cursor_left(UINTN
*cursor
, UINTN
*first
)
531 else if ((*first
) > 0)
535 static void cursor_right(UINTN
*cursor
, UINTN
*first
, UINTN x_max
, UINTN len
)
537 if ((*cursor
)+2 < x_max
)
539 else if ((*first
) + (*cursor
) < len
)
543 BOOLEAN
line_edit(CHAR16
*line_in
, CHAR16
**line_out
, UINTN x_max
) {
554 DrawScreenHeader(L
"Line Editor");
555 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, (ConWidth
- 71) / 2, ConHeight
- 1);
556 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
,
557 L
"Use cursor keys to edit, Esc to exit, Enter to boot with edited options");
561 size
= StrLen(line_in
) + 1024;
562 line
= AllocatePool(size
* sizeof(CHAR16
));
563 StrCpy(line
, line_in
);
565 print
= AllocatePool(x_max
* sizeof(CHAR16
));
567 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, TRUE
);
582 CopyMem(print
, line
+ first
, i
* sizeof(CHAR16
));
586 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, y_pos
);
587 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, print
);
588 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
590 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
591 err
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
595 switch (key
.ScanCode
) {
605 if (cursor
>= x_max
) {
607 first
= len
- (x_max
-2);
611 while((first
+ cursor
) && line
[first
+ cursor
] == ' ')
612 cursor_left(&cursor
, &first
);
613 while((first
+ cursor
) && line
[first
+ cursor
] != ' ')
614 cursor_left(&cursor
, &first
);
615 while((first
+ cursor
) && line
[first
+ cursor
] == ' ')
616 cursor_left(&cursor
, &first
);
617 if (first
+ cursor
!= len
&& first
+ cursor
)
618 cursor_right(&cursor
, &first
, x_max
, len
);
619 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
622 while(line
[first
+ cursor
] && line
[first
+ cursor
] == ' ')
623 cursor_right(&cursor
, &first
, x_max
, len
);
624 while(line
[first
+ cursor
] && line
[first
+ cursor
] != ' ')
625 cursor_right(&cursor
, &first
, x_max
, len
);
626 while(line
[first
+ cursor
] && line
[first
+ cursor
] == ' ')
627 cursor_right(&cursor
, &first
, x_max
, len
);
628 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
631 if (first
+ cursor
== len
)
633 cursor_right(&cursor
, &first
, x_max
, len
);
634 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
637 cursor_left(&cursor
, &first
);
638 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
643 if (first
+ cursor
== len
)
645 for (i
= first
+ cursor
; i
< len
; i
++)
652 switch (key
.UnicodeChar
) {
654 case CHAR_CARRIAGE_RETURN
:
663 if (first
== 0 && cursor
== 0)
665 for (i
= first
+ cursor
-1; i
< len
; i
++)
670 if (cursor
> 0 || first
== 0)
672 /* show full line if it fits */
678 /* jump left to see what we delete */
689 case 0x80 ... 0xffff:
692 for (i
= len
; i
> first
+ cursor
; i
--)
694 line
[first
+ cursor
] = key
.UnicodeChar
;
697 if (cursor
+2 < x_max
)
699 else if (first
+ cursor
< len
)
705 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, FALSE
);
709 } /* BOOLEAN line_edit() */