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.
41 #include "../include/refit_call_wrapper.h"
43 #include "../include/egemb_refind_banner.h"
45 // Console defines and variables
51 static VOID
DrawScreenHeader(IN CHAR16
*Title
);
53 // UGA defines and variables
57 BOOLEAN AllowGraphicsMode
;
59 EG_PIXEL StdBackgroundPixel
= { 0xbf, 0xbf, 0xbf, 0 };
60 EG_PIXEL MenuBackgroundPixel
= { 0xbf, 0xbf, 0xbf, 0 };
62 static BOOLEAN GraphicsScreenDirty
;
64 // general defines and variables
66 static BOOLEAN haveError
= FALSE
;
69 // Screen initialization and switching
79 if (egHasGraphicsMode()) {
80 egGetScreenSize(&UGAWidth
, &UGAHeight
);
81 AllowGraphicsMode
= TRUE
;
83 AllowGraphicsMode
= FALSE
;
84 egSetGraphicsModeEnabled(FALSE
); // just to be sure we are in text mode
86 GraphicsScreenDirty
= TRUE
;
89 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, FALSE
);
91 // get size of text console
92 if (refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &ConWidth
, &ConHeight
) != EFI_SUCCESS
) {
93 // use default values on error
98 // make a buffer for a whole text line
99 BlankLine
= AllocatePool((ConWidth
+ 1) * sizeof(CHAR16
));
100 for (i
= 0; i
< ConWidth
; i
++)
104 // show the banner (even when in graphics mode)
105 DrawScreenHeader(L
"Initializing...");
108 VOID
SetupScreen(VOID
)
110 if (GlobalConfig
.TextOnly
) {
111 // switch to text mode if requested
112 AllowGraphicsMode
= FALSE
;
115 } else if (AllowGraphicsMode
) {
116 // clear screen and show banner
117 // (now we know we'll stay in graphics mode)
118 if ((GlobalConfig
.RequestedScreenWidth
> 0) && (GlobalConfig
.RequestedScreenHeight
> 0) &&
119 egSetScreenSize(GlobalConfig
.RequestedScreenWidth
, GlobalConfig
.RequestedScreenHeight
)) {
120 UGAWidth
= GlobalConfig
.RequestedScreenWidth
;
121 UGAHeight
= GlobalConfig
.RequestedScreenHeight
;
122 } // if user requested a particular screen resolution
124 BltClearScreen(TRUE
);
128 VOID
SwitchToText(IN BOOLEAN CursorEnabled
)
130 egSetGraphicsModeEnabled(FALSE
);
131 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, CursorEnabled
);
134 VOID
SwitchToGraphics(VOID
)
136 if (AllowGraphicsMode
&& !egIsGraphicsModeEnabled()) {
137 egSetGraphicsModeEnabled(TRUE
);
138 GraphicsScreenDirty
= TRUE
;
143 // Screen control for running tools
146 VOID
BeginTextScreen(IN CHAR16
*Title
)
148 DrawScreenHeader(Title
);
155 VOID
FinishTextScreen(IN BOOLEAN WaitAlways
)
157 if (haveError
|| WaitAlways
) {
166 VOID
BeginExternalScreen(IN BOOLEAN UseGraphicsMode
, IN CHAR16
*Title
)
168 EG_PIXEL DarkBackgroundPixel
= { 0x0, 0x0, 0x0, 0 };
170 if (!AllowGraphicsMode
)
171 UseGraphicsMode
= FALSE
;
173 if (UseGraphicsMode
) {
175 BltClearScreen(FALSE
);
177 egClearScreen(&DarkBackgroundPixel
);
178 DrawScreenHeader(Title
);
182 // DrawScreenHeader(Title);
184 if (!UseGraphicsMode
)
191 VOID
FinishExternalScreen(VOID
)
193 // make sure we clean up later
194 GraphicsScreenDirty
= TRUE
;
205 VOID
TerminateScreen(VOID
)
208 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
209 refit_call1_wrapper(ST
->ConOut
->ClearScreen
, ST
->ConOut
);
212 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, TRUE
);
215 static VOID
DrawScreenHeader(IN CHAR16
*Title
)
219 // clear to black background
220 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
221 refit_call1_wrapper(ST
->ConOut
->ClearScreen
, ST
->ConOut
);
223 // paint header background
224 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BANNER
);
225 for (y
= 0; y
< 3; y
++) {
226 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, y
);
231 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 1);
232 Print(L
"rEFInd - %s", Title
);
235 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
236 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, 4);
243 static BOOLEAN
ReadAllKeyStrokes(VOID
)
245 BOOLEAN GotKeyStrokes
;
249 GotKeyStrokes
= FALSE
;
251 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
252 if (Status
== EFI_SUCCESS
) {
253 GotKeyStrokes
= TRUE
;
258 return GotKeyStrokes
;
261 VOID
PauseForKey(VOID
)
265 Print(L
"\n* Hit any key to continue *");
267 if (ReadAllKeyStrokes()) { // remove buffered key strokes
268 refit_call1_wrapper(BS
->Stall
, 5000000); // 5 seconds delay
269 ReadAllKeyStrokes(); // empty the buffer again
272 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
273 ReadAllKeyStrokes(); // empty the buffer to protect the menu
279 VOID
DebugPause(VOID
)
281 // show console and wait for key
290 VOID
EndlessIdleLoop(VOID
)
296 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
304 #ifdef __MAKEWITH_GNUEFI
305 BOOLEAN
CheckFatalError(IN EFI_STATUS Status
, IN CHAR16
*where
)
307 CHAR16 ErrorName
[64];
309 if (!EFI_ERROR(Status
))
312 StatusToString(ErrorName
, Status
);
313 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
314 Print(L
"Fatal Error: %s %s\n", ErrorName
, where
);
315 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
318 //BS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
323 BOOLEAN
CheckError(IN EFI_STATUS Status
, IN CHAR16
*where
)
325 CHAR16 ErrorName
[64];
327 if (!EFI_ERROR(Status
))
330 StatusToString(ErrorName
, Status
);
331 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
332 Print(L
"Error: %s %s\n", ErrorName
, where
);
333 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
339 BOOLEAN
CheckFatalError(IN EFI_STATUS Status
, IN CHAR16
*where
)
341 // CHAR16 ErrorName[64];
343 if (!EFI_ERROR(Status
))
346 // StatusToString(ErrorName, Status);
347 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_ERROR
);
348 Print(L
"Fatal Error: %r %s\n", Status
, where
);
349 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_BASIC
);
352 //gBS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
357 BOOLEAN
CheckError(IN EFI_STATUS Status
, IN CHAR16
*where
)
359 // CHAR16 ErrorName[64];
361 if (!EFI_ERROR(Status
))
364 // StatusToString(ErrorName, Status);
365 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_ERROR
);
366 Print(L
"Error: %r %s\n", Status
, where
);
367 gST
->ConOut
->SetAttribute (gST
->ConOut
, ATTR_BASIC
);
375 // Graphics functions
378 VOID
SwitchToGraphicsAndClear(VOID
)
381 if (GraphicsScreenDirty
)
382 BltClearScreen(TRUE
);
385 VOID
BltClearScreen(IN BOOLEAN ShowBanner
)
387 static EG_IMAGE
*Banner
= NULL
;
389 if (ShowBanner
&& !(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_BANNER
)) {
390 // load banner on first call
391 if (Banner
== NULL
) {
392 if (GlobalConfig
.BannerFileName
== NULL
)
393 Banner
= egPrepareEmbeddedImage(&egemb_refind_banner
, FALSE
);
395 Banner
= egLoadImage(SelfDir
, GlobalConfig
.BannerFileName
, FALSE
);
397 MenuBackgroundPixel
= Banner
->PixelData
[0];
400 // clear and draw banner
401 egClearScreen(&MenuBackgroundPixel
);
403 BltImage(Banner
, (UGAWidth
- Banner
->Width
) >> 1,
404 ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_HEIGHT
- Banner
->Height
);
407 // clear to standard background color
408 egClearScreen(&StdBackgroundPixel
);
411 GraphicsScreenDirty
= FALSE
;
414 VOID
BltImage(IN EG_IMAGE
*Image
, IN UINTN XPos
, IN UINTN YPos
)
416 egDrawImage(Image
, XPos
, YPos
);
417 GraphicsScreenDirty
= TRUE
;
420 VOID
BltImageAlpha(IN EG_IMAGE
*Image
, IN UINTN XPos
, IN UINTN YPos
, IN EG_PIXEL
*BackgroundPixel
)
424 // compose on background
425 CompImage
= egCreateFilledImage(Image
->Width
, Image
->Height
, FALSE
, BackgroundPixel
);
426 egComposeImage(CompImage
, Image
, 0, 0);
428 // blit to screen and clean up
429 egDrawImage(CompImage
, XPos
, YPos
);
430 egFreeImage(CompImage
);
431 GraphicsScreenDirty
= TRUE
;
434 // VOID BltImageComposite(IN EG_IMAGE *BaseImage, IN EG_IMAGE *TopImage, IN UINTN XPos, IN UINTN YPos)
436 // UINTN TotalWidth, TotalHeight, CompWidth, CompHeight, OffsetX, OffsetY;
437 // EG_IMAGE *CompImage;
439 // // initialize buffer with base image
440 // CompImage = egCopyImage(BaseImage);
441 // TotalWidth = BaseImage->Width;
442 // TotalHeight = BaseImage->Height;
444 // // place the top image
445 // CompWidth = TopImage->Width;
446 // if (CompWidth > TotalWidth)
447 // CompWidth = TotalWidth;
448 // OffsetX = (TotalWidth - CompWidth) >> 1;
449 // CompHeight = TopImage->Height;
450 // if (CompHeight > TotalHeight)
451 // CompHeight = TotalHeight;
452 // OffsetY = (TotalHeight - CompHeight) >> 1;
453 // egComposeImage(CompImage, TopImage, OffsetX, OffsetY);
455 // // blit to screen and clean up
456 // egDrawImage(CompImage, XPos, YPos);
457 // egFreeImage(CompImage);
458 // GraphicsScreenDirty = TRUE;
461 VOID
BltImageCompositeBadge(IN EG_IMAGE
*BaseImage
, IN EG_IMAGE
*TopImage
, IN EG_IMAGE
*BadgeImage
, IN UINTN XPos
, IN UINTN YPos
)
463 UINTN TotalWidth
= 0, TotalHeight
= 0, CompWidth
= 0, CompHeight
= 0, OffsetX
= 0, OffsetY
= 0;
464 EG_IMAGE
*CompImage
= NULL
;
466 // initialize buffer with base image
467 if (BaseImage
!= NULL
) {
468 CompImage
= egCopyImage(BaseImage
);
469 TotalWidth
= BaseImage
->Width
;
470 TotalHeight
= BaseImage
->Height
;
473 // place the top image
474 if ((TopImage
!= NULL
) && (CompImage
!= NULL
)) {
475 CompWidth
= TopImage
->Width
;
476 if (CompWidth
> TotalWidth
)
477 CompWidth
= TotalWidth
;
478 OffsetX
= (TotalWidth
- CompWidth
) >> 1;
479 CompHeight
= TopImage
->Height
;
480 if (CompHeight
> TotalHeight
)
481 CompHeight
= TotalHeight
;
482 OffsetY
= (TotalHeight
- CompHeight
) >> 1;
483 egComposeImage(CompImage
, TopImage
, OffsetX
, OffsetY
);
486 // place the badge image
487 if (BadgeImage
!= NULL
&& CompImage
!= NULL
&& (BadgeImage
->Width
+ 8) < CompWidth
&& (BadgeImage
->Height
+ 8) < CompHeight
) {
488 OffsetX
+= CompWidth
- 8 - BadgeImage
->Width
;
489 OffsetY
+= CompHeight
- 8 - BadgeImage
->Height
;
490 egComposeImage(CompImage
, BadgeImage
, OffsetX
, OffsetY
);
493 // blit to screen and clean up
494 egDrawImage(CompImage
, XPos
, YPos
);
495 egFreeImage(CompImage
);
496 GraphicsScreenDirty
= TRUE
;
499 // Line-editing functions borrowed from gummiboot (cursor_left(), cursor_right(), & line_edit())
501 static void cursor_left(UINTN
*cursor
, UINTN
*first
)
505 else if ((*first
) > 0)
509 static void cursor_right(UINTN
*cursor
, UINTN
*first
, UINTN x_max
, UINTN len
)
511 if ((*cursor
)+2 < x_max
)
513 else if ((*first
) + (*cursor
) < len
)
517 BOOLEAN
line_edit(CHAR16
*line_in
, CHAR16
**line_out
, UINTN x_max
, UINTN y_pos
) {
529 size
= StrLen(line_in
) + 1024;
530 line
= AllocatePool(size
* sizeof(CHAR16
));
531 StrCpy(line
, line_in
);
533 print
= AllocatePool(x_max
* sizeof(CHAR16
));
535 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, TRUE
);
550 CopyMem(print
, line
+ first
, i
* sizeof(CHAR16
));
554 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, y_pos
);
555 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, print
);
556 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
558 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
559 err
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
563 switch (key
.ScanCode
) {
573 if (cursor
>= x_max
) {
575 first
= len
- (x_max
-2);
579 while((first
+ cursor
) && line
[first
+ cursor
] == ' ')
580 cursor_left(&cursor
, &first
);
581 while((first
+ cursor
) && line
[first
+ cursor
] != ' ')
582 cursor_left(&cursor
, &first
);
583 while((first
+ cursor
) && line
[first
+ cursor
] == ' ')
584 cursor_left(&cursor
, &first
);
585 if (first
+ cursor
!= len
&& first
+ cursor
)
586 cursor_right(&cursor
, &first
, x_max
, len
);
587 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
590 while(line
[first
+ cursor
] && line
[first
+ cursor
] == ' ')
591 cursor_right(&cursor
, &first
, x_max
, len
);
592 while(line
[first
+ cursor
] && line
[first
+ cursor
] != ' ')
593 cursor_right(&cursor
, &first
, x_max
, len
);
594 while(line
[first
+ cursor
] && line
[first
+ cursor
] == ' ')
595 cursor_right(&cursor
, &first
, x_max
, len
);
596 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
599 if (first
+ cursor
== len
)
601 cursor_right(&cursor
, &first
, x_max
, len
);
602 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
605 cursor_left(&cursor
, &first
);
606 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, cursor
, y_pos
);
611 if (first
+ cursor
== len
)
613 for (i
= first
+ cursor
; i
< len
; i
++)
620 switch (key
.UnicodeChar
) {
622 case CHAR_CARRIAGE_RETURN
:
623 if (StrCmp(line
, line_in
) != 0) {
633 if (first
== 0 && cursor
== 0)
635 for (i
= first
+ cursor
-1; i
< len
; i
++)
640 if (cursor
> 0 || first
== 0)
642 /* show full line if it fits */
648 /* jump left to see what we delete */
659 case 0x80 ... 0xffff:
662 for (i
= len
; i
> first
+ cursor
; i
--)
664 line
[first
+ cursor
] = key
.UnicodeChar
;
667 if (cursor
+2 < x_max
)
669 else if (first
+ cursor
< len
)
675 refit_call2_wrapper(ST
->ConOut
->EnableCursor
, ST
->ConOut
, FALSE
);
679 } /* BOOLEAN line_edit() */