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
);
196 if (!UseGraphicsMode
)
203 VOID
FinishExternalScreen(VOID
)
205 // make sure we clean up later
206 GraphicsScreenDirty
= TRUE
;
213 // Reset the screen resolution, in case external program changed it....
220 VOID
TerminateScreen(VOID
)
223 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
224 refit_call1_wrapper(ST
->ConOut
->ClearScreen
, ST
->ConOut
);
227 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, TRUE
);
230 static VOID
DrawScreenHeader(IN CHAR16
*Title
)
234 // clear to black background
235 egClearScreen(&DarkBackgroundPixel
); // first clear in graphics mode
236 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
237 refit_call1_wrapper(ST
->ConOut
->ClearScreen
, ST
->ConOut
); // then clear in text mode
239 // paint header background
240 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BANNER
);
241 for (y
= 0; y
< 3; y
++) {
242 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, y
);
247 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 1);
248 Print(L
"rEFInd - %s", Title
);
251 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
252 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, 4);
259 static BOOLEAN
ReadAllKeyStrokes(VOID
)
261 BOOLEAN GotKeyStrokes
;
265 GotKeyStrokes
= FALSE
;
267 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
268 if (Status
== EFI_SUCCESS
) {
269 GotKeyStrokes
= TRUE
;
274 return GotKeyStrokes
;
277 VOID
PauseForKey(VOID
)
281 Print(L
"\n* Hit any key to continue *");
283 if (ReadAllKeyStrokes()) { // remove buffered key strokes
284 refit_call1_wrapper(BS
->Stall
, 5000000); // 5 seconds delay
285 ReadAllKeyStrokes(); // empty the buffer again
288 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
289 ReadAllKeyStrokes(); // empty the buffer to protect the menu
295 VOID
DebugPause(VOID
)
297 // show console and wait for key
306 VOID
EndlessIdleLoop(VOID
)
312 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
320 #ifdef __MAKEWITH_GNUEFI
321 BOOLEAN
CheckFatalError(IN EFI_STATUS Status
, IN CHAR16
*where
)
323 CHAR16 ErrorName
[64];
325 if (!EFI_ERROR(Status
))
328 StatusToString(ErrorName
, Status
);
329 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
330 Print(L
"Fatal Error: %s %s\n", ErrorName
, where
);
331 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
334 //BS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
339 BOOLEAN
CheckError(IN EFI_STATUS Status
, IN CHAR16
*where
)
341 CHAR16 ErrorName
[64];
343 if (!EFI_ERROR(Status
))
346 StatusToString(ErrorName
, Status
);
347 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
348 Print(L
"Error: %s %s\n", ErrorName
, where
);
349 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
355 BOOLEAN
CheckFatalError(IN EFI_STATUS Status
, IN CHAR16
*where
)
357 // CHAR16 ErrorName[64];
359 if (!EFI_ERROR(Status
))
362 // StatusToString(ErrorName, Status);
363 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_ERROR
);
364 Print(L
"Fatal Error: %r %s\n", Status
, where
);
365 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_BASIC
);
368 //gBS->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 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_ERROR
);
382 Print(L
"Error: %r %s\n", Status
, where
);
383 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_BASIC
);
391 // Graphics functions
394 VOID
SwitchToGraphicsAndClear(VOID
)
397 if (GraphicsScreenDirty
)
398 BltClearScreen(TRUE
);
401 VOID
BltClearScreen(IN BOOLEAN ShowBanner
)
403 static EG_IMAGE
*Banner
= NULL
;
405 if (ShowBanner
&& !(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_BANNER
)) {
406 // load banner on first call
407 if (Banner
== NULL
) {
408 if (GlobalConfig
.BannerFileName
== NULL
)
409 Banner
= egPrepareEmbeddedImage(&egemb_refind_banner
, FALSE
);
411 Banner
= egLoadImage(SelfDir
, GlobalConfig
.BannerFileName
, FALSE
);
413 MenuBackgroundPixel
= Banner
->PixelData
[0];
416 // clear and draw banner
417 egClearScreen(&MenuBackgroundPixel
);
419 BltImage(Banner
, (UGAWidth
- Banner
->Width
) >> 1,
420 ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_HEIGHT
- Banner
->Height
);
423 // clear to standard background color
424 egClearScreen(&StdBackgroundPixel
);
427 GraphicsScreenDirty
= FALSE
;
430 VOID
BltImage(IN EG_IMAGE
*Image
, IN UINTN XPos
, IN UINTN YPos
)
432 egDrawImage(Image
, XPos
, YPos
);
433 GraphicsScreenDirty
= TRUE
;
436 VOID
BltImageAlpha(IN EG_IMAGE
*Image
, IN UINTN XPos
, IN UINTN YPos
, IN EG_PIXEL
*BackgroundPixel
)
440 // compose on background
441 CompImage
= egCreateFilledImage(Image
->Width
, Image
->Height
, FALSE
, BackgroundPixel
);
442 egComposeImage(CompImage
, Image
, 0, 0);
444 // blit to screen and clean up
445 egDrawImage(CompImage
, XPos
, YPos
);
446 egFreeImage(CompImage
);
447 GraphicsScreenDirty
= TRUE
;
450 // VOID BltImageComposite(IN EG_IMAGE *BaseImage, IN EG_IMAGE *TopImage, IN UINTN XPos, IN UINTN YPos)
452 // UINTN TotalWidth, TotalHeight, CompWidth, CompHeight, OffsetX, OffsetY;
453 // EG_IMAGE *CompImage;
455 // // initialize buffer with base image
456 // CompImage = egCopyImage(BaseImage);
457 // TotalWidth = BaseImage->Width;
458 // TotalHeight = BaseImage->Height;
460 // // place the top image
461 // CompWidth = TopImage->Width;
462 // if (CompWidth > TotalWidth)
463 // CompWidth = TotalWidth;
464 // OffsetX = (TotalWidth - CompWidth) >> 1;
465 // CompHeight = TopImage->Height;
466 // if (CompHeight > TotalHeight)
467 // CompHeight = TotalHeight;
468 // OffsetY = (TotalHeight - CompHeight) >> 1;
469 // egComposeImage(CompImage, TopImage, OffsetX, OffsetY);
471 // // blit to screen and clean up
472 // egDrawImage(CompImage, XPos, YPos);
473 // egFreeImage(CompImage);
474 // GraphicsScreenDirty = TRUE;
477 VOID
BltImageCompositeBadge(IN EG_IMAGE
*BaseImage
, IN EG_IMAGE
*TopImage
, IN EG_IMAGE
*BadgeImage
, IN UINTN XPos
, IN UINTN YPos
)
479 UINTN TotalWidth
= 0, TotalHeight
= 0, CompWidth
= 0, CompHeight
= 0, OffsetX
= 0, OffsetY
= 0;
480 EG_IMAGE
*CompImage
= NULL
;
482 // initialize buffer with base image
483 if (BaseImage
!= NULL
) {
484 CompImage
= egCopyImage(BaseImage
);
485 TotalWidth
= BaseImage
->Width
;
486 TotalHeight
= BaseImage
->Height
;
489 // place the top image
490 if ((TopImage
!= NULL
) && (CompImage
!= NULL
)) {
491 CompWidth
= TopImage
->Width
;
492 if (CompWidth
> TotalWidth
)
493 CompWidth
= TotalWidth
;
494 OffsetX
= (TotalWidth
- CompWidth
) >> 1;
495 CompHeight
= TopImage
->Height
;
496 if (CompHeight
> TotalHeight
)
497 CompHeight
= TotalHeight
;
498 OffsetY
= (TotalHeight
- CompHeight
) >> 1;
499 egComposeImage(CompImage
, TopImage
, OffsetX
, OffsetY
);
502 // place the badge image
503 if (BadgeImage
!= NULL
&& CompImage
!= NULL
&& (BadgeImage
->Width
+ 8) < CompWidth
&& (BadgeImage
->Height
+ 8) < CompHeight
) {
504 OffsetX
+= CompWidth
- 8 - BadgeImage
->Width
;
505 OffsetY
+= CompHeight
- 8 - BadgeImage
->Height
;
506 egComposeImage(CompImage
, BadgeImage
, OffsetX
, OffsetY
);
509 // blit to screen and clean up
510 egDrawImage(CompImage
, XPos
, YPos
);
511 egFreeImage(CompImage
);
512 GraphicsScreenDirty
= TRUE
;
515 // Line-editing functions borrowed from gummiboot (cursor_left(), cursor_right(), & line_edit()).
516 // gummiboot is copyright (c) 2012 by Kay Sievers <kay.sievers@vrfy.org> and Harald Hoyer
517 // <harald@redhat.com> and is licensed under the LGPL 2.1.
519 static void cursor_left(UINTN
*cursor
, UINTN
*first
)
523 else if ((*first
) > 0)
527 static void cursor_right(UINTN
*cursor
, UINTN
*first
, UINTN x_max
, UINTN len
)
529 if ((*cursor
)+2 < x_max
)
531 else if ((*first
) + (*cursor
) < len
)
535 BOOLEAN
line_edit(CHAR16
*line_in
, CHAR16
**line_out
, UINTN x_max
) {
546 DrawScreenHeader(L
"Line Editor");
547 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, (ConWidth
- 71) / 2, ConHeight
- 1);
548 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
,
549 L
"Use cursor keys to edit, Esc to exit, Enter to boot with edited options");
553 size
= StrLen(line_in
) + 1024;
554 line
= AllocatePool(size
* sizeof(CHAR16
));
555 StrCpy(line
, line_in
);
557 print
= AllocatePool(x_max
* sizeof(CHAR16
));
559 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, TRUE
);
574 CopyMem(print
, line
+ first
, i
* sizeof(CHAR16
));
578 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, y_pos
);
579 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, print
);
580 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
582 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
583 err
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
587 switch (key
.ScanCode
) {
597 if (cursor
>= x_max
) {
599 first
= len
- (x_max
-2);
603 while((first
+ cursor
) && line
[first
+ cursor
] == ' ')
604 cursor_left(&cursor
, &first
);
605 while((first
+ cursor
) && line
[first
+ cursor
] != ' ')
606 cursor_left(&cursor
, &first
);
607 while((first
+ cursor
) && line
[first
+ cursor
] == ' ')
608 cursor_left(&cursor
, &first
);
609 if (first
+ cursor
!= len
&& first
+ cursor
)
610 cursor_right(&cursor
, &first
, x_max
, len
);
611 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
614 while(line
[first
+ cursor
] && line
[first
+ cursor
] == ' ')
615 cursor_right(&cursor
, &first
, x_max
, len
);
616 while(line
[first
+ cursor
] && line
[first
+ cursor
] != ' ')
617 cursor_right(&cursor
, &first
, x_max
, len
);
618 while(line
[first
+ cursor
] && line
[first
+ cursor
] == ' ')
619 cursor_right(&cursor
, &first
, x_max
, len
);
620 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
623 if (first
+ cursor
== len
)
625 cursor_right(&cursor
, &first
, x_max
, len
);
626 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
629 cursor_left(&cursor
, &first
);
630 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
635 if (first
+ cursor
== len
)
637 for (i
= first
+ cursor
; i
< len
; i
++)
644 switch (key
.UnicodeChar
) {
646 case CHAR_CARRIAGE_RETURN
:
655 if (first
== 0 && cursor
== 0)
657 for (i
= first
+ cursor
-1; i
< len
; i
++)
662 if (cursor
> 0 || first
== 0)
664 /* show full line if it fits */
670 /* jump left to see what we delete */
681 case 0x80 ... 0xffff:
684 for (i
= len
; i
> first
+ cursor
; i
--)
686 line
[first
+ cursor
] = key
.UnicodeChar
;
689 if (cursor
+2 < x_max
)
691 else if (first
+ cursor
< len
)
697 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, FALSE
);
701 } /* BOOLEAN line_edit() */