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.
42 #include "../include/refit_call_wrapper.h"
44 #include "../include/egemb_refind_banner.h"
46 // Console defines and variables
50 CHAR16
*BlankLine
= NULL
;
52 static VOID
DrawScreenHeader(IN CHAR16
*Title
);
54 // UGA defines and variables
58 BOOLEAN AllowGraphicsMode
;
60 EG_PIXEL StdBackgroundPixel
= { 0xbf, 0xbf, 0xbf, 0 };
61 EG_PIXEL MenuBackgroundPixel
= { 0xbf, 0xbf, 0xbf, 0 };
62 EG_PIXEL DarkBackgroundPixel
= { 0x0, 0x0, 0x0, 0 };
64 static BOOLEAN GraphicsScreenDirty
;
66 // general defines and variables
68 static BOOLEAN haveError
= FALSE
;
70 static VOID
PrepareBlankLine(VOID
) {
73 MyFreePool(BlankLine
);
74 // make a buffer for a whole text line
75 BlankLine
= AllocatePool((ConWidth
+ 1) * sizeof(CHAR16
));
76 for (i
= 0; i
< ConWidth
; i
++)
82 // Screen initialization and switching
90 if (egHasGraphicsMode()) {
91 egGetScreenSize(&UGAWidth
, &UGAHeight
);
92 AllowGraphicsMode
= TRUE
;
94 AllowGraphicsMode
= FALSE
;
95 egSetGraphicsModeEnabled(FALSE
); // just to be sure we are in text mode
97 GraphicsScreenDirty
= TRUE
;
100 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, FALSE
);
102 // get size of text console
103 if (refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &ConWidth
, &ConHeight
) != EFI_SUCCESS
) {
104 // use default values on error
111 // show the banner (even when in graphics mode)
112 DrawScreenHeader(L
"Initializing...");
115 VOID
SetupScreen(VOID
)
117 GlobalConfig
.RequestedTextMode
= egSetTextMode(GlobalConfig
.RequestedTextMode
);
119 if ((GlobalConfig
.RequestedScreenWidth
> 0) &&
120 (egSetScreenSize(&GlobalConfig
.RequestedScreenWidth
, &GlobalConfig
.RequestedScreenHeight
))) {
121 UGAWidth
= GlobalConfig
.RequestedScreenWidth
;
122 UGAHeight
= GlobalConfig
.RequestedScreenHeight
;
123 } // if user requested a particular screen resolution
125 if (GlobalConfig
.TextOnly
) {
126 // switch to text mode if requested
127 AllowGraphicsMode
= FALSE
;
130 } else if (AllowGraphicsMode
) {
131 // clear screen and show banner
132 // (now we know we'll stay in graphics mode)
134 BltClearScreen(TRUE
);
138 VOID
SwitchToText(IN BOOLEAN CursorEnabled
)
140 egSetGraphicsModeEnabled(FALSE
);
141 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, CursorEnabled
);
142 // get size of text console
143 if (refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &ConWidth
, &ConHeight
) != EFI_SUCCESS
) {
144 // use default values on error
151 VOID
SwitchToGraphics(VOID
)
153 if (AllowGraphicsMode
&& !egIsGraphicsModeEnabled()) {
154 egSetGraphicsModeEnabled(TRUE
);
155 GraphicsScreenDirty
= TRUE
;
160 // Screen control for running tools
163 VOID
BeginTextScreen(IN CHAR16
*Title
)
165 DrawScreenHeader(Title
);
172 VOID
FinishTextScreen(IN BOOLEAN WaitAlways
)
174 if (haveError
|| WaitAlways
) {
183 VOID
BeginExternalScreen(IN BOOLEAN UseGraphicsMode
, IN CHAR16
*Title
)
185 if (!AllowGraphicsMode
)
186 UseGraphicsMode
= FALSE
;
188 if (UseGraphicsMode
) {
190 BltClearScreen(FALSE
);
192 egClearScreen(&DarkBackgroundPixel
);
193 DrawScreenHeader(Title
);
197 // DrawScreenHeader(Title);
199 if (!UseGraphicsMode
)
206 VOID
FinishExternalScreen(VOID
)
208 // Reset the screen resolution, in case external program changed it....
211 // make sure we clean up later
212 GraphicsScreenDirty
= TRUE
;
223 VOID
TerminateScreen(VOID
)
226 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
227 refit_call1_wrapper(ST
->ConOut
->ClearScreen
, ST
->ConOut
);
230 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, TRUE
);
233 static VOID
DrawScreenHeader(IN CHAR16
*Title
)
237 // clear to black background
238 egClearScreen(&DarkBackgroundPixel
); // first clear in graphics mode
239 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
240 refit_call1_wrapper(ST
->ConOut
->ClearScreen
, ST
->ConOut
); // then clear in text mode
242 // paint header background
243 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BANNER
);
244 for (y
= 0; y
< 3; y
++) {
245 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, y
);
250 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 1);
251 Print(L
"rEFInd - %s", Title
);
254 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
255 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, 4);
262 static BOOLEAN
ReadAllKeyStrokes(VOID
)
264 BOOLEAN GotKeyStrokes
;
268 GotKeyStrokes
= FALSE
;
270 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
271 if (Status
== EFI_SUCCESS
) {
272 GotKeyStrokes
= TRUE
;
277 return GotKeyStrokes
;
280 VOID
PauseForKey(VOID
)
284 Print(L
"\n* Hit any key to continue *");
286 if (ReadAllKeyStrokes()) { // remove buffered key strokes
287 refit_call1_wrapper(BS
->Stall
, 5000000); // 5 seconds delay
288 ReadAllKeyStrokes(); // empty the buffer again
291 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
292 ReadAllKeyStrokes(); // empty the buffer to protect the menu
298 VOID
DebugPause(VOID
)
300 // show console and wait for key
309 VOID
EndlessIdleLoop(VOID
)
315 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
323 #ifdef __MAKEWITH_GNUEFI
324 BOOLEAN
CheckFatalError(IN EFI_STATUS Status
, IN CHAR16
*where
)
326 CHAR16 ErrorName
[64];
328 if (!EFI_ERROR(Status
))
331 StatusToString(ErrorName
, Status
);
332 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
333 Print(L
"Fatal Error: %s %s\n", ErrorName
, where
);
334 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
337 //BS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
342 BOOLEAN
CheckError(IN EFI_STATUS Status
, IN CHAR16
*where
)
344 CHAR16 ErrorName
[64];
346 if (!EFI_ERROR(Status
))
349 StatusToString(ErrorName
, Status
);
350 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
351 Print(L
"Error: %s %s\n", ErrorName
, where
);
352 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
358 BOOLEAN
CheckFatalError(IN EFI_STATUS Status
, IN CHAR16
*where
)
360 // CHAR16 ErrorName[64];
362 if (!EFI_ERROR(Status
))
365 // StatusToString(ErrorName, Status);
366 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_ERROR
);
367 Print(L
"Fatal Error: %r %s\n", Status
, where
);
368 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_BASIC
);
371 //gBS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
376 BOOLEAN
CheckError(IN EFI_STATUS Status
, IN CHAR16
*where
)
378 // CHAR16 ErrorName[64];
380 if (!EFI_ERROR(Status
))
383 // StatusToString(ErrorName, Status);
384 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_ERROR
);
385 Print(L
"Error: %r %s\n", Status
, where
);
386 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_BASIC
);
394 // Graphics functions
397 VOID
SwitchToGraphicsAndClear(VOID
)
400 if (GraphicsScreenDirty
)
401 BltClearScreen(TRUE
);
404 VOID
BltClearScreen(IN BOOLEAN ShowBanner
)
406 static EG_IMAGE
*Banner
= NULL
;
408 if (ShowBanner
&& !(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_BANNER
)) {
409 // load banner on first call
410 if (Banner
== NULL
) {
411 if (GlobalConfig
.BannerFileName
== NULL
)
412 Banner
= egPrepareEmbeddedImage(&egemb_refind_banner
, FALSE
);
414 Banner
= egLoadImage(SelfDir
, GlobalConfig
.BannerFileName
, FALSE
);
416 MenuBackgroundPixel
= Banner
->PixelData
[0];
419 // clear and draw banner
420 egClearScreen(&MenuBackgroundPixel
);
422 BltImage(Banner
, (UGAWidth
- Banner
->Width
) >> 1,
423 ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_HEIGHT
- Banner
->Height
);
426 // clear to standard background color
427 egClearScreen(&StdBackgroundPixel
);
430 GraphicsScreenDirty
= FALSE
;
433 VOID
BltImage(IN EG_IMAGE
*Image
, IN UINTN XPos
, IN UINTN YPos
)
435 egDrawImage(Image
, XPos
, YPos
);
436 GraphicsScreenDirty
= TRUE
;
439 VOID
BltImageAlpha(IN EG_IMAGE
*Image
, IN UINTN XPos
, IN UINTN YPos
, IN EG_PIXEL
*BackgroundPixel
)
443 // compose on background
444 CompImage
= egCreateFilledImage(Image
->Width
, Image
->Height
, FALSE
, BackgroundPixel
);
445 egComposeImage(CompImage
, Image
, 0, 0);
447 // blit to screen and clean up
448 egDrawImage(CompImage
, XPos
, YPos
);
449 egFreeImage(CompImage
);
450 GraphicsScreenDirty
= TRUE
;
453 // VOID BltImageComposite(IN EG_IMAGE *BaseImage, IN EG_IMAGE *TopImage, IN UINTN XPos, IN UINTN YPos)
455 // UINTN TotalWidth, TotalHeight, CompWidth, CompHeight, OffsetX, OffsetY;
456 // EG_IMAGE *CompImage;
458 // // initialize buffer with base image
459 // CompImage = egCopyImage(BaseImage);
460 // TotalWidth = BaseImage->Width;
461 // TotalHeight = BaseImage->Height;
463 // // place the top image
464 // CompWidth = TopImage->Width;
465 // if (CompWidth > TotalWidth)
466 // CompWidth = TotalWidth;
467 // OffsetX = (TotalWidth - CompWidth) >> 1;
468 // CompHeight = TopImage->Height;
469 // if (CompHeight > TotalHeight)
470 // CompHeight = TotalHeight;
471 // OffsetY = (TotalHeight - CompHeight) >> 1;
472 // egComposeImage(CompImage, TopImage, OffsetX, OffsetY);
474 // // blit to screen and clean up
475 // egDrawImage(CompImage, XPos, YPos);
476 // egFreeImage(CompImage);
477 // GraphicsScreenDirty = TRUE;
480 VOID
BltImageCompositeBadge(IN EG_IMAGE
*BaseImage
, IN EG_IMAGE
*TopImage
, IN EG_IMAGE
*BadgeImage
, IN UINTN XPos
, IN UINTN YPos
)
482 UINTN TotalWidth
= 0, TotalHeight
= 0, CompWidth
= 0, CompHeight
= 0, OffsetX
= 0, OffsetY
= 0;
483 EG_IMAGE
*CompImage
= NULL
;
485 // initialize buffer with base image
486 if (BaseImage
!= NULL
) {
487 CompImage
= egCopyImage(BaseImage
);
488 TotalWidth
= BaseImage
->Width
;
489 TotalHeight
= BaseImage
->Height
;
492 // place the top image
493 if ((TopImage
!= NULL
) && (CompImage
!= NULL
)) {
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
);
505 // place the badge image
506 if (BadgeImage
!= NULL
&& CompImage
!= NULL
&& (BadgeImage
->Width
+ 8) < CompWidth
&& (BadgeImage
->Height
+ 8) < CompHeight
) {
507 OffsetX
+= CompWidth
- 8 - BadgeImage
->Width
;
508 OffsetY
+= CompHeight
- 8 - BadgeImage
->Height
;
509 egComposeImage(CompImage
, BadgeImage
, OffsetX
, OffsetY
);
512 // blit to screen and clean up
513 egDrawImage(CompImage
, XPos
, YPos
);
514 egFreeImage(CompImage
);
515 GraphicsScreenDirty
= TRUE
;
518 // Line-editing functions borrowed from gummiboot (cursor_left(), cursor_right(), & line_edit()).
519 // gummiboot is copyright (c) 2012 by Kay Sievers <kay.sievers@vrfy.org> and Harald Hoyer
520 // <harald@redhat.com> and is licensed under the LGPL 2.1.
522 static void cursor_left(UINTN
*cursor
, UINTN
*first
)
526 else if ((*first
) > 0)
530 static void cursor_right(UINTN
*cursor
, UINTN
*first
, UINTN x_max
, UINTN len
)
532 if ((*cursor
)+2 < x_max
)
534 else if ((*first
) + (*cursor
) < len
)
538 BOOLEAN
line_edit(CHAR16
*line_in
, CHAR16
**line_out
, UINTN x_max
) {
549 DrawScreenHeader(L
"Line Editor");
550 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, (ConWidth
- 71) / 2, ConHeight
- 1);
551 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
,
552 L
"Use cursor keys to edit, Esc to exit, Enter to boot with edited options");
556 size
= StrLen(line_in
) + 1024;
557 line
= AllocatePool(size
* sizeof(CHAR16
));
558 StrCpy(line
, line_in
);
560 print
= AllocatePool(x_max
* sizeof(CHAR16
));
562 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, TRUE
);
577 CopyMem(print
, line
+ first
, i
* sizeof(CHAR16
));
581 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, y_pos
);
582 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, print
);
583 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
585 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
586 err
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
590 switch (key
.ScanCode
) {
600 if (cursor
>= x_max
) {
602 first
= len
- (x_max
-2);
606 while((first
+ cursor
) && line
[first
+ cursor
] == ' ')
607 cursor_left(&cursor
, &first
);
608 while((first
+ cursor
) && line
[first
+ cursor
] != ' ')
609 cursor_left(&cursor
, &first
);
610 while((first
+ cursor
) && line
[first
+ cursor
] == ' ')
611 cursor_left(&cursor
, &first
);
612 if (first
+ cursor
!= len
&& first
+ cursor
)
613 cursor_right(&cursor
, &first
, x_max
, len
);
614 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
617 while(line
[first
+ cursor
] && line
[first
+ cursor
] == ' ')
618 cursor_right(&cursor
, &first
, x_max
, len
);
619 while(line
[first
+ cursor
] && line
[first
+ cursor
] != ' ')
620 cursor_right(&cursor
, &first
, x_max
, len
);
621 while(line
[first
+ cursor
] && line
[first
+ cursor
] == ' ')
622 cursor_right(&cursor
, &first
, x_max
, len
);
623 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
626 if (first
+ cursor
== len
)
628 cursor_right(&cursor
, &first
, x_max
, len
);
629 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
632 cursor_left(&cursor
, &first
);
633 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
638 if (first
+ cursor
== len
)
640 for (i
= first
+ cursor
; i
< len
; i
++)
647 switch (key
.UnicodeChar
) {
649 case CHAR_CARRIAGE_RETURN
:
658 if (first
== 0 && cursor
== 0)
660 for (i
= first
+ cursor
-1; i
< len
; i
++)
665 if (cursor
> 0 || first
== 0)
667 /* show full line if it fits */
673 /* jump left to see what we delete */
684 case 0x80 ... 0xffff:
687 for (i
= len
; i
> first
+ cursor
; i
--)
689 line
[first
+ cursor
] = key
.UnicodeChar
;
692 if (cursor
+2 < x_max
)
694 else if (first
+ cursor
< len
)
700 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, FALSE
);
704 } /* BOOLEAN line_edit() */