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 egSetTextMode(GlobalConfig
.RequestedTextMode
);
104 egSetGraphicsModeEnabled(FALSE
); // just to be sure we are in text mode
106 GraphicsScreenDirty
= TRUE
;
109 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, FALSE
);
111 // get size of text console
112 if (refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &ConWidth
, &ConHeight
) != EFI_SUCCESS
) {
113 // use default values on error
120 // show the banner if in text mode
121 if (GlobalConfig
.TextOnly
)
122 DrawScreenHeader(L
"Initializing...");
125 VOID
SetupScreen(VOID
)
127 UINTN NewWidth
, NewHeight
;
129 // Convert mode number to horizontal & vertical resolution values
130 if ((GlobalConfig
.RequestedScreenWidth
> 0) && (GlobalConfig
.RequestedScreenHeight
== 0))
131 egGetResFromMode(&(GlobalConfig
.RequestedScreenWidth
), &(GlobalConfig
.RequestedScreenHeight
));
133 // Set the believed-to-be current resolution to the LOWER of the current
134 // believed-to-be resolution and the requested resolution. This is done to
135 // enable setting a lower-than-default resolution.
136 if ((GlobalConfig
.RequestedScreenWidth
> 0) && (GlobalConfig
.RequestedScreenHeight
> 0)) {
137 UGAWidth
= (UGAWidth
< GlobalConfig
.RequestedScreenWidth
) ? UGAWidth
: GlobalConfig
.RequestedScreenWidth
;
138 UGAHeight
= (UGAHeight
< GlobalConfig
.RequestedScreenHeight
) ? UGAHeight
: GlobalConfig
.RequestedScreenHeight
;
141 // Set text mode. If this requires increasing the size of the graphics mode, do so.
142 if (egSetTextMode(GlobalConfig
.RequestedTextMode
)) {
143 egGetScreenSize(&NewWidth
, &NewHeight
);
144 if ((NewWidth
> UGAWidth
) || (NewHeight
> UGAHeight
)) {
146 UGAHeight
= NewHeight
;
148 if ((UGAWidth
> GlobalConfig
.RequestedScreenWidth
) || (UGAHeight
> GlobalConfig
.RequestedScreenHeight
)) {
149 // Requested text mode forces us to use a bigger graphics mode
150 GlobalConfig
.RequestedScreenWidth
= UGAWidth
;
151 GlobalConfig
.RequestedScreenHeight
= UGAHeight
;
155 if (GlobalConfig
.RequestedScreenWidth
> 0) {
156 egSetScreenSize(&GlobalConfig
.RequestedScreenWidth
, &GlobalConfig
.RequestedScreenHeight
);
157 egGetScreenSize(&UGAWidth
, &UGAHeight
);
158 } // if user requested a particular screen resolution
160 if (GlobalConfig
.TextOnly
) {
161 // switch to text mode if requested
162 AllowGraphicsMode
= FALSE
;
165 } else if (AllowGraphicsMode
) {
166 // clear screen and show banner
167 // (now we know we'll stay in graphics mode)
169 BltClearScreen(TRUE
);
171 } // VOID SetupScreen()
173 VOID
SwitchToText(IN BOOLEAN CursorEnabled
)
175 egSetGraphicsModeEnabled(FALSE
);
176 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, CursorEnabled
);
177 // get size of text console
178 if (refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &ConWidth
, &ConHeight
) != EFI_SUCCESS
) {
179 // use default values on error
186 VOID
SwitchToGraphics(VOID
)
188 if (AllowGraphicsMode
&& !egIsGraphicsModeEnabled()) {
189 egSetGraphicsModeEnabled(TRUE
);
190 GraphicsScreenDirty
= TRUE
;
195 // Screen control for running tools
198 VOID
BeginTextScreen(IN CHAR16
*Title
)
200 DrawScreenHeader(Title
);
207 VOID
FinishTextScreen(IN BOOLEAN WaitAlways
)
209 if (haveError
|| WaitAlways
) {
218 VOID
BeginExternalScreen(IN BOOLEAN UseGraphicsMode
, IN CHAR16
*Title
)
220 if (!AllowGraphicsMode
)
221 UseGraphicsMode
= FALSE
;
223 if (UseGraphicsMode
) {
225 BltClearScreen(FALSE
);
227 egClearScreen(&DarkBackgroundPixel
);
228 DrawScreenHeader(Title
);
236 VOID
FinishExternalScreen(VOID
)
238 // make sure we clean up later
239 GraphicsScreenDirty
= TRUE
;
246 // Reset the screen resolution, in case external program changed it....
253 VOID
TerminateScreen(VOID
)
256 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
257 refit_call1_wrapper(ST
->ConOut
->ClearScreen
, ST
->ConOut
);
260 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, TRUE
);
263 static VOID
DrawScreenHeader(IN CHAR16
*Title
)
267 // clear to black background
268 egClearScreen(&DarkBackgroundPixel
); // first clear in graphics mode
269 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
270 refit_call1_wrapper(ST
->ConOut
->ClearScreen
, ST
->ConOut
); // then clear in text mode
272 // paint header background
273 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BANNER
);
274 for (y
= 0; y
< 3; y
++) {
275 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, y
);
280 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 1);
281 Print(L
"rEFInd - %s", Title
);
284 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
285 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, 4);
292 static BOOLEAN
ReadAllKeyStrokes(VOID
)
294 BOOLEAN GotKeyStrokes
;
298 GotKeyStrokes
= FALSE
;
300 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
301 if (Status
== EFI_SUCCESS
) {
302 GotKeyStrokes
= TRUE
;
307 return GotKeyStrokes
;
310 VOID
PauseForKey(VOID
)
314 Print(L
"\n* Hit any key to continue *");
316 if (ReadAllKeyStrokes()) { // remove buffered key strokes
317 refit_call1_wrapper(BS
->Stall
, 5000000); // 5 seconds delay
318 ReadAllKeyStrokes(); // empty the buffer again
321 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
322 ReadAllKeyStrokes(); // empty the buffer to protect the menu
328 VOID
DebugPause(VOID
)
330 // show console and wait for key
339 VOID
EndlessIdleLoop(VOID
)
345 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
353 #ifdef __MAKEWITH_GNUEFI
354 BOOLEAN
CheckFatalError(IN EFI_STATUS Status
, IN CHAR16
*where
)
356 CHAR16 ErrorName
[64];
358 if (!EFI_ERROR(Status
))
361 StatusToString(ErrorName
, Status
);
362 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
363 Print(L
"Fatal Error: %s %s\n", ErrorName
, where
);
364 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
367 //BS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
372 BOOLEAN
CheckError(IN EFI_STATUS Status
, IN CHAR16
*where
)
374 CHAR16 ErrorName
[64];
376 if (!EFI_ERROR(Status
))
379 StatusToString(ErrorName
, Status
);
380 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
381 Print(L
"Error: %s %s\n", ErrorName
, where
);
382 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
388 BOOLEAN
CheckFatalError(IN EFI_STATUS Status
, IN CHAR16
*where
)
390 // CHAR16 ErrorName[64];
392 if (!EFI_ERROR(Status
))
395 // StatusToString(ErrorName, Status);
396 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_ERROR
);
397 Print(L
"Fatal Error: %r %s\n", Status
, where
);
398 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_BASIC
);
401 //gBS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
406 BOOLEAN
CheckError(IN EFI_STATUS Status
, IN CHAR16
*where
)
408 // CHAR16 ErrorName[64];
410 if (!EFI_ERROR(Status
))
413 // StatusToString(ErrorName, Status);
414 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_ERROR
);
415 Print(L
"Error: %r %s\n", Status
, where
);
416 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_BASIC
);
424 // Graphics functions
427 VOID
SwitchToGraphicsAndClear(VOID
)
430 if (GraphicsScreenDirty
)
431 BltClearScreen(TRUE
);
434 VOID
BltClearScreen(IN BOOLEAN ShowBanner
)
436 static EG_IMAGE
*Banner
= NULL
;
438 if (ShowBanner
&& !(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_BANNER
)) {
439 // load banner on first call
440 if (Banner
== NULL
) {
441 if (GlobalConfig
.BannerFileName
== NULL
)
442 Banner
= egPrepareEmbeddedImage(&egemb_refind_banner
, FALSE
);
444 Banner
= egLoadImage(SelfDir
, GlobalConfig
.BannerFileName
, FALSE
);
446 MenuBackgroundPixel
= Banner
->PixelData
[0];
449 // clear and draw banner
450 egClearScreen(&MenuBackgroundPixel
);
452 BltImage(Banner
, (UGAWidth
- Banner
->Width
) >> 1,
453 ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_HEIGHT
- Banner
->Height
);
456 // clear to standard background color
457 egClearScreen(&StdBackgroundPixel
);
460 GraphicsScreenDirty
= FALSE
;
463 VOID
BltImage(IN EG_IMAGE
*Image
, IN UINTN XPos
, IN UINTN YPos
)
465 egDrawImage(Image
, XPos
, YPos
);
466 GraphicsScreenDirty
= TRUE
;
469 VOID
BltImageAlpha(IN EG_IMAGE
*Image
, IN UINTN XPos
, IN UINTN YPos
, IN EG_PIXEL
*BackgroundPixel
)
473 // compose on background
474 CompImage
= egCreateFilledImage(Image
->Width
, Image
->Height
, FALSE
, BackgroundPixel
);
475 egComposeImage(CompImage
, Image
, 0, 0);
477 // blit to screen and clean up
478 egDrawImage(CompImage
, XPos
, YPos
);
479 egFreeImage(CompImage
);
480 GraphicsScreenDirty
= TRUE
;
483 // VOID BltImageComposite(IN EG_IMAGE *BaseImage, IN EG_IMAGE *TopImage, IN UINTN XPos, IN UINTN YPos)
485 // UINTN TotalWidth, TotalHeight, CompWidth, CompHeight, OffsetX, OffsetY;
486 // EG_IMAGE *CompImage;
488 // // initialize buffer with base image
489 // CompImage = egCopyImage(BaseImage);
490 // TotalWidth = BaseImage->Width;
491 // TotalHeight = BaseImage->Height;
493 // // place the top image
494 // CompWidth = TopImage->Width;
495 // if (CompWidth > TotalWidth)
496 // CompWidth = TotalWidth;
497 // OffsetX = (TotalWidth - CompWidth) >> 1;
498 // CompHeight = TopImage->Height;
499 // if (CompHeight > TotalHeight)
500 // CompHeight = TotalHeight;
501 // OffsetY = (TotalHeight - CompHeight) >> 1;
502 // egComposeImage(CompImage, TopImage, OffsetX, OffsetY);
504 // // blit to screen and clean up
505 // egDrawImage(CompImage, XPos, YPos);
506 // egFreeImage(CompImage);
507 // GraphicsScreenDirty = TRUE;
510 VOID
BltImageCompositeBadge(IN EG_IMAGE
*BaseImage
, IN EG_IMAGE
*TopImage
, IN EG_IMAGE
*BadgeImage
, IN UINTN XPos
, IN UINTN YPos
)
512 UINTN TotalWidth
= 0, TotalHeight
= 0, CompWidth
= 0, CompHeight
= 0, OffsetX
= 0, OffsetY
= 0;
513 EG_IMAGE
*CompImage
= NULL
;
515 // initialize buffer with base image
516 if (BaseImage
!= NULL
) {
517 CompImage
= egCopyImage(BaseImage
);
518 TotalWidth
= BaseImage
->Width
;
519 TotalHeight
= BaseImage
->Height
;
522 // place the top image
523 if ((TopImage
!= NULL
) && (CompImage
!= NULL
)) {
524 CompWidth
= TopImage
->Width
;
525 if (CompWidth
> TotalWidth
)
526 CompWidth
= TotalWidth
;
527 OffsetX
= (TotalWidth
- CompWidth
) >> 1;
528 CompHeight
= TopImage
->Height
;
529 if (CompHeight
> TotalHeight
)
530 CompHeight
= TotalHeight
;
531 OffsetY
= (TotalHeight
- CompHeight
) >> 1;
532 egComposeImage(CompImage
, TopImage
, OffsetX
, OffsetY
);
535 // place the badge image
536 if (BadgeImage
!= NULL
&& CompImage
!= NULL
&& (BadgeImage
->Width
+ 8) < CompWidth
&& (BadgeImage
->Height
+ 8) < CompHeight
) {
537 OffsetX
+= CompWidth
- 8 - BadgeImage
->Width
;
538 OffsetY
+= CompHeight
- 8 - BadgeImage
->Height
;
539 egComposeImage(CompImage
, BadgeImage
, OffsetX
, OffsetY
);
542 // blit to screen and clean up
543 egDrawImage(CompImage
, XPos
, YPos
);
544 egFreeImage(CompImage
);
545 GraphicsScreenDirty
= TRUE
;
548 // Line-editing functions borrowed from gummiboot (cursor_left(), cursor_right(), & line_edit()).
549 // gummiboot is copyright (c) 2012 by Kay Sievers <kay.sievers@vrfy.org> and Harald Hoyer
550 // <harald@redhat.com> and is licensed under the LGPL 2.1.
552 static void cursor_left(UINTN
*cursor
, UINTN
*first
)
556 else if ((*first
) > 0)
560 static void cursor_right(UINTN
*cursor
, UINTN
*first
, UINTN x_max
, UINTN len
)
562 if ((*cursor
)+2 < x_max
)
564 else if ((*first
) + (*cursor
) < len
)
568 BOOLEAN
line_edit(CHAR16
*line_in
, CHAR16
**line_out
, UINTN x_max
) {
579 DrawScreenHeader(L
"Line Editor");
580 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, (ConWidth
- 71) / 2, ConHeight
- 1);
581 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
,
582 L
"Use cursor keys to edit, Esc to exit, Enter to boot with edited options");
586 size
= StrLen(line_in
) + 1024;
587 line
= AllocatePool(size
* sizeof(CHAR16
));
588 StrCpy(line
, line_in
);
590 print
= AllocatePool(x_max
* sizeof(CHAR16
));
592 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, TRUE
);
607 CopyMem(print
, line
+ first
, i
* sizeof(CHAR16
));
611 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, y_pos
);
612 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, print
);
613 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
615 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
616 err
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
620 switch (key
.ScanCode
) {
630 if (cursor
>= x_max
) {
632 first
= len
- (x_max
-2);
636 while((first
+ cursor
) && line
[first
+ cursor
] == ' ')
637 cursor_left(&cursor
, &first
);
638 while((first
+ cursor
) && line
[first
+ cursor
] != ' ')
639 cursor_left(&cursor
, &first
);
640 while((first
+ cursor
) && line
[first
+ cursor
] == ' ')
641 cursor_left(&cursor
, &first
);
642 if (first
+ cursor
!= len
&& first
+ cursor
)
643 cursor_right(&cursor
, &first
, x_max
, len
);
644 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
647 while(line
[first
+ cursor
] && line
[first
+ cursor
] == ' ')
648 cursor_right(&cursor
, &first
, x_max
, len
);
649 while(line
[first
+ cursor
] && line
[first
+ cursor
] != ' ')
650 cursor_right(&cursor
, &first
, x_max
, len
);
651 while(line
[first
+ cursor
] && line
[first
+ cursor
] == ' ')
652 cursor_right(&cursor
, &first
, x_max
, len
);
653 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
656 if (first
+ cursor
== len
)
658 cursor_right(&cursor
, &first
, x_max
, len
);
659 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
662 cursor_left(&cursor
, &first
);
663 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
668 if (first
+ cursor
== len
)
670 for (i
= first
+ cursor
; i
< len
; i
++)
677 switch (key
.UnicodeChar
) {
679 case CHAR_CARRIAGE_RETURN
:
688 if (first
== 0 && cursor
== 0)
690 for (i
= first
+ cursor
-1; i
< len
; i
++)
695 if (cursor
> 0 || first
== 0)
697 /* show full line if it fits */
703 /* jump left to see what we delete */
714 case 0x80 ... 0xffff:
717 for (i
= len
; i
> first
+ cursor
; i
--)
719 line
[first
+ cursor
] = key
.UnicodeChar
;
722 if (cursor
+2 < x_max
)
724 else if (first
+ cursor
< len
)
730 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, FALSE
);
734 } /* BOOLEAN line_edit() */