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 // Set the screen resolution and mode (text vs. graphics).
126 VOID
SetupScreen(VOID
)
128 UINTN NewWidth
, NewHeight
;
130 // Convert mode number to horizontal & vertical resolution values
131 if ((GlobalConfig
.RequestedScreenWidth
> 0) && (GlobalConfig
.RequestedScreenHeight
== 0))
132 egGetResFromMode(&(GlobalConfig
.RequestedScreenWidth
), &(GlobalConfig
.RequestedScreenHeight
));
134 // Set the believed-to-be current resolution to the LOWER of the current
135 // believed-to-be resolution and the requested resolution. This is done to
136 // enable setting a lower-than-default resolution.
137 if ((GlobalConfig
.RequestedScreenWidth
> 0) && (GlobalConfig
.RequestedScreenHeight
> 0)) {
138 UGAWidth
= (UGAWidth
< GlobalConfig
.RequestedScreenWidth
) ? UGAWidth
: GlobalConfig
.RequestedScreenWidth
;
139 UGAHeight
= (UGAHeight
< GlobalConfig
.RequestedScreenHeight
) ? UGAHeight
: GlobalConfig
.RequestedScreenHeight
;
142 // Set text mode. If this requires increasing the size of the graphics mode, do so.
143 if (egSetTextMode(GlobalConfig
.RequestedTextMode
)) {
144 egGetScreenSize(&NewWidth
, &NewHeight
);
145 if ((NewWidth
> UGAWidth
) || (NewHeight
> UGAHeight
)) {
147 UGAHeight
= NewHeight
;
149 if ((UGAWidth
> GlobalConfig
.RequestedScreenWidth
) || (UGAHeight
> GlobalConfig
.RequestedScreenHeight
)) {
150 // Requested text mode forces us to use a bigger graphics mode
151 GlobalConfig
.RequestedScreenWidth
= UGAWidth
;
152 GlobalConfig
.RequestedScreenHeight
= UGAHeight
;
156 if (GlobalConfig
.RequestedScreenWidth
> 0) {
157 egSetScreenSize(&(GlobalConfig
.RequestedScreenWidth
), &(GlobalConfig
.RequestedScreenHeight
));
158 egGetScreenSize(&UGAWidth
, &UGAHeight
);
159 } // if user requested a particular screen resolution
161 if (GlobalConfig
.TextOnly
) {
162 // switch to text mode if requested
163 AllowGraphicsMode
= FALSE
;
166 } else if (AllowGraphicsMode
) {
167 // clear screen and show banner
168 // (now we know we'll stay in graphics mode)
170 BltClearScreen(TRUE
);
172 } // VOID SetupScreen()
174 VOID
SwitchToText(IN BOOLEAN CursorEnabled
)
176 egSetGraphicsModeEnabled(FALSE
);
177 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, CursorEnabled
);
178 // get size of text console
179 if (refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &ConWidth
, &ConHeight
) != EFI_SUCCESS
) {
180 // use default values on error
187 VOID
SwitchToGraphics(VOID
)
189 if (AllowGraphicsMode
&& !egIsGraphicsModeEnabled()) {
190 egSetGraphicsModeEnabled(TRUE
);
191 GraphicsScreenDirty
= TRUE
;
196 // Screen control for running tools
199 VOID
BeginTextScreen(IN CHAR16
*Title
)
201 DrawScreenHeader(Title
);
208 VOID
FinishTextScreen(IN BOOLEAN WaitAlways
)
210 if (haveError
|| WaitAlways
) {
219 VOID
BeginExternalScreen(IN BOOLEAN UseGraphicsMode
, IN CHAR16
*Title
)
221 if (!AllowGraphicsMode
)
222 UseGraphicsMode
= FALSE
;
224 if (UseGraphicsMode
) {
226 BltClearScreen(FALSE
);
228 egClearScreen(&DarkBackgroundPixel
);
229 DrawScreenHeader(Title
);
237 VOID
FinishExternalScreen(VOID
)
239 // make sure we clean up later
240 GraphicsScreenDirty
= TRUE
;
247 // Reset the screen resolution, in case external program changed it....
254 VOID
TerminateScreen(VOID
)
257 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
258 refit_call1_wrapper(ST
->ConOut
->ClearScreen
, ST
->ConOut
);
261 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, TRUE
);
264 static VOID
DrawScreenHeader(IN CHAR16
*Title
)
268 // clear to black background
269 egClearScreen(&DarkBackgroundPixel
); // first clear in graphics mode
270 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
271 refit_call1_wrapper(ST
->ConOut
->ClearScreen
, ST
->ConOut
); // then clear in text mode
273 // paint header background
274 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BANNER
);
275 for (y
= 0; y
< 3; y
++) {
276 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, y
);
281 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 1);
282 Print(L
"rEFInd - %s", Title
);
285 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
286 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, 4);
293 static BOOLEAN
ReadAllKeyStrokes(VOID
)
295 BOOLEAN GotKeyStrokes
;
299 GotKeyStrokes
= FALSE
;
301 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
302 if (Status
== EFI_SUCCESS
) {
303 GotKeyStrokes
= TRUE
;
308 return GotKeyStrokes
;
311 VOID
PauseForKey(VOID
)
315 Print(L
"\n* Hit any key to continue *");
317 if (ReadAllKeyStrokes()) { // remove buffered key strokes
318 refit_call1_wrapper(BS
->Stall
, 5000000); // 5 seconds delay
319 ReadAllKeyStrokes(); // empty the buffer again
322 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
323 ReadAllKeyStrokes(); // empty the buffer to protect the menu
329 VOID
DebugPause(VOID
)
331 // show console and wait for key
340 VOID
EndlessIdleLoop(VOID
)
346 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
354 #ifdef __MAKEWITH_GNUEFI
355 BOOLEAN
CheckFatalError(IN EFI_STATUS Status
, IN CHAR16
*where
)
357 CHAR16 ErrorName
[64];
359 if (!EFI_ERROR(Status
))
362 StatusToString(ErrorName
, Status
);
363 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
364 Print(L
"Fatal Error: %s %s\n", ErrorName
, where
);
365 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
368 //BS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
373 BOOLEAN
CheckError(IN EFI_STATUS Status
, IN CHAR16
*where
)
375 CHAR16 ErrorName
[64];
377 if (!EFI_ERROR(Status
))
380 StatusToString(ErrorName
, Status
);
381 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
382 Print(L
"Error: %s %s\n", ErrorName
, where
);
383 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
389 BOOLEAN
CheckFatalError(IN EFI_STATUS Status
, IN CHAR16
*where
)
391 // CHAR16 ErrorName[64];
393 if (!EFI_ERROR(Status
))
396 // StatusToString(ErrorName, Status);
397 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_ERROR
);
398 Print(L
"Fatal Error: %r %s\n", Status
, where
);
399 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_BASIC
);
402 //gBS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
407 BOOLEAN
CheckError(IN EFI_STATUS Status
, IN CHAR16
*where
)
409 // CHAR16 ErrorName[64];
411 if (!EFI_ERROR(Status
))
414 // StatusToString(ErrorName, Status);
415 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_ERROR
);
416 Print(L
"Error: %r %s\n", Status
, where
);
417 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_BASIC
);
425 // Graphics functions
428 VOID
SwitchToGraphicsAndClear(VOID
)
431 if (GraphicsScreenDirty
)
432 BltClearScreen(TRUE
);
435 VOID
BltClearScreen(IN BOOLEAN ShowBanner
)
437 static EG_IMAGE
*Banner
= NULL
;
439 if (ShowBanner
&& !(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_BANNER
)) {
440 // load banner on first call
441 if (Banner
== NULL
) {
442 if (GlobalConfig
.BannerFileName
== NULL
)
443 Banner
= egPrepareEmbeddedImage(&egemb_refind_banner
, FALSE
);
445 Banner
= egLoadImage(SelfDir
, GlobalConfig
.BannerFileName
, FALSE
);
447 MenuBackgroundPixel
= Banner
->PixelData
[0];
450 // clear and draw banner
451 egClearScreen(&MenuBackgroundPixel
);
453 BltImage(Banner
, (UGAWidth
- Banner
->Width
) >> 1,
454 ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_HEIGHT
- Banner
->Height
);
457 // clear to standard background color
458 egClearScreen(&StdBackgroundPixel
);
461 GraphicsScreenDirty
= FALSE
;
464 VOID
BltImage(IN EG_IMAGE
*Image
, IN UINTN XPos
, IN UINTN YPos
)
466 egDrawImage(Image
, XPos
, YPos
);
467 GraphicsScreenDirty
= TRUE
;
470 VOID
BltImageAlpha(IN EG_IMAGE
*Image
, IN UINTN XPos
, IN UINTN YPos
, IN EG_PIXEL
*BackgroundPixel
)
474 // compose on background
475 CompImage
= egCreateFilledImage(Image
->Width
, Image
->Height
, FALSE
, BackgroundPixel
);
476 egComposeImage(CompImage
, Image
, 0, 0);
478 // blit to screen and clean up
479 egDrawImage(CompImage
, XPos
, YPos
);
480 egFreeImage(CompImage
);
481 GraphicsScreenDirty
= TRUE
;
484 // VOID BltImageComposite(IN EG_IMAGE *BaseImage, IN EG_IMAGE *TopImage, IN UINTN XPos, IN UINTN YPos)
486 // UINTN TotalWidth, TotalHeight, CompWidth, CompHeight, OffsetX, OffsetY;
487 // EG_IMAGE *CompImage;
489 // // initialize buffer with base image
490 // CompImage = egCopyImage(BaseImage);
491 // TotalWidth = BaseImage->Width;
492 // TotalHeight = BaseImage->Height;
494 // // place the top image
495 // CompWidth = TopImage->Width;
496 // if (CompWidth > TotalWidth)
497 // CompWidth = TotalWidth;
498 // OffsetX = (TotalWidth - CompWidth) >> 1;
499 // CompHeight = TopImage->Height;
500 // if (CompHeight > TotalHeight)
501 // CompHeight = TotalHeight;
502 // OffsetY = (TotalHeight - CompHeight) >> 1;
503 // egComposeImage(CompImage, TopImage, OffsetX, OffsetY);
505 // // blit to screen and clean up
506 // egDrawImage(CompImage, XPos, YPos);
507 // egFreeImage(CompImage);
508 // GraphicsScreenDirty = TRUE;
511 VOID
BltImageCompositeBadge(IN EG_IMAGE
*BaseImage
, IN EG_IMAGE
*TopImage
, IN EG_IMAGE
*BadgeImage
, IN UINTN XPos
, IN UINTN YPos
)
513 UINTN TotalWidth
= 0, TotalHeight
= 0, CompWidth
= 0, CompHeight
= 0, OffsetX
= 0, OffsetY
= 0;
514 EG_IMAGE
*CompImage
= NULL
;
516 // initialize buffer with base image
517 if (BaseImage
!= NULL
) {
518 CompImage
= egCopyImage(BaseImage
);
519 TotalWidth
= BaseImage
->Width
;
520 TotalHeight
= BaseImage
->Height
;
523 // place the top image
524 if ((TopImage
!= NULL
) && (CompImage
!= NULL
)) {
525 CompWidth
= TopImage
->Width
;
526 if (CompWidth
> TotalWidth
)
527 CompWidth
= TotalWidth
;
528 OffsetX
= (TotalWidth
- CompWidth
) >> 1;
529 CompHeight
= TopImage
->Height
;
530 if (CompHeight
> TotalHeight
)
531 CompHeight
= TotalHeight
;
532 OffsetY
= (TotalHeight
- CompHeight
) >> 1;
533 egComposeImage(CompImage
, TopImage
, OffsetX
, OffsetY
);
536 // place the badge image
537 if (BadgeImage
!= NULL
&& CompImage
!= NULL
&& (BadgeImage
->Width
+ 8) < CompWidth
&& (BadgeImage
->Height
+ 8) < CompHeight
) {
538 OffsetX
+= CompWidth
- 8 - BadgeImage
->Width
;
539 OffsetY
+= CompHeight
- 8 - BadgeImage
->Height
;
540 egComposeImage(CompImage
, BadgeImage
, OffsetX
, OffsetY
);
543 // blit to screen and clean up
544 egDrawImage(CompImage
, XPos
, YPos
);
545 egFreeImage(CompImage
);
546 GraphicsScreenDirty
= TRUE
;
549 // Line-editing functions borrowed from gummiboot (cursor_left(), cursor_right(), & line_edit()).
550 // gummiboot is copyright (c) 2012 by Kay Sievers <kay.sievers@vrfy.org> and Harald Hoyer
551 // <harald@redhat.com> and is licensed under the LGPL 2.1.
553 static void cursor_left(UINTN
*cursor
, UINTN
*first
)
557 else if ((*first
) > 0)
561 static void cursor_right(UINTN
*cursor
, UINTN
*first
, UINTN x_max
, UINTN len
)
563 if ((*cursor
)+2 < x_max
)
565 else if ((*first
) + (*cursor
) < len
)
569 BOOLEAN
line_edit(CHAR16
*line_in
, CHAR16
**line_out
, UINTN x_max
) {
580 DrawScreenHeader(L
"Line Editor");
581 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, (ConWidth
- 71) / 2, ConHeight
- 1);
582 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
,
583 L
"Use cursor keys to edit, Esc to exit, Enter to boot with edited options");
587 size
= StrLen(line_in
) + 1024;
588 line
= AllocatePool(size
* sizeof(CHAR16
));
589 StrCpy(line
, line_in
);
591 print
= AllocatePool(x_max
* sizeof(CHAR16
));
593 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, TRUE
);
608 CopyMem(print
, line
+ first
, i
* sizeof(CHAR16
));
612 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, y_pos
);
613 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, print
);
614 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
616 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
617 err
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
621 switch (key
.ScanCode
) {
631 if (cursor
>= x_max
) {
633 first
= len
- (x_max
-2);
637 while((first
+ cursor
) && line
[first
+ cursor
] == ' ')
638 cursor_left(&cursor
, &first
);
639 while((first
+ cursor
) && line
[first
+ cursor
] != ' ')
640 cursor_left(&cursor
, &first
);
641 while((first
+ cursor
) && line
[first
+ cursor
] == ' ')
642 cursor_left(&cursor
, &first
);
643 if (first
+ cursor
!= len
&& first
+ cursor
)
644 cursor_right(&cursor
, &first
, x_max
, len
);
645 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
648 while(line
[first
+ cursor
] && line
[first
+ cursor
] == ' ')
649 cursor_right(&cursor
, &first
, x_max
, len
);
650 while(line
[first
+ cursor
] && line
[first
+ cursor
] != ' ')
651 cursor_right(&cursor
, &first
, x_max
, len
);
652 while(line
[first
+ cursor
] && line
[first
+ cursor
] == ' ')
653 cursor_right(&cursor
, &first
, x_max
, len
);
654 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
657 if (first
+ cursor
== len
)
659 cursor_right(&cursor
, &first
, x_max
, len
);
660 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
663 cursor_left(&cursor
, &first
);
664 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
669 if (first
+ cursor
== len
)
671 for (i
= first
+ cursor
; i
< len
; i
++)
678 switch (key
.UnicodeChar
) {
680 case CHAR_CARRIAGE_RETURN
:
689 if (first
== 0 && cursor
== 0)
691 for (i
= first
+ cursor
-1; i
< len
; i
++)
696 if (cursor
> 0 || first
== 0)
698 /* show full line if it fits */
704 /* jump left to see what we delete */
715 case 0x80 ... 0xffff:
718 for (i
= len
; i
> first
+ cursor
; i
--)
720 line
[first
+ cursor
] = key
.UnicodeChar
;
723 if (cursor
+2 < x_max
)
725 else if (first
+ cursor
< len
)
731 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, FALSE
);
735 } /* BOOLEAN line_edit() */