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.
38 #include "../refind/screen.h"
39 #include "../include/refit_call_wrapper.h"
41 #include <efiUgaDraw.h>
42 #include <efiConsoleControl.h>
44 #ifndef __MAKEWITH_GNUEFI
45 #define LibLocateProtocol EfiLibLocateProtocol
48 // Console defines and variables
50 static EFI_GUID ConsoleControlProtocolGuid
= EFI_CONSOLE_CONTROL_PROTOCOL_GUID
;
51 static EFI_CONSOLE_CONTROL_PROTOCOL
*ConsoleControl
= NULL
;
53 static EFI_GUID UgaDrawProtocolGuid
= EFI_UGA_DRAW_PROTOCOL_GUID
;
54 static EFI_UGA_DRAW_PROTOCOL
*UgaDraw
= NULL
;
56 static EFI_GUID GraphicsOutputProtocolGuid
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
57 static EFI_GRAPHICS_OUTPUT_PROTOCOL
*GraphicsOutput
= NULL
;
59 static BOOLEAN egHasGraphics
= FALSE
;
60 static UINTN egScreenWidth
= 800;
61 static UINTN egScreenHeight
= 600;
67 VOID
egInitScreen(VOID
)
69 EFI_STATUS Status
= EFI_SUCCESS
;
70 UINT32 UGAWidth
, UGAHeight
, UGADepth
, UGARefreshRate
;
73 Status
= LibLocateProtocol(&ConsoleControlProtocolGuid
, (VOID
**) &ConsoleControl
);
74 if (EFI_ERROR(Status
))
75 ConsoleControl
= NULL
;
77 Status
= LibLocateProtocol(&UgaDrawProtocolGuid
, (VOID
**) &UgaDraw
);
78 if (EFI_ERROR(Status
))
81 Status
= LibLocateProtocol(&GraphicsOutputProtocolGuid
, (VOID
**) &GraphicsOutput
);
82 if (EFI_ERROR(Status
))
83 GraphicsOutput
= NULL
;
86 egHasGraphics
= FALSE
;
87 if (GraphicsOutput
!= NULL
) {
88 egScreenWidth
= GraphicsOutput
->Mode
->Info
->HorizontalResolution
;
89 egScreenHeight
= GraphicsOutput
->Mode
->Info
->VerticalResolution
;
91 } else if (UgaDraw
!= NULL
) {
92 Status
= refit_call5_wrapper(UgaDraw
->GetMode
, UgaDraw
, &UGAWidth
, &UGAHeight
, &UGADepth
, &UGARefreshRate
);
93 if (EFI_ERROR(Status
)) {
94 UgaDraw
= NULL
; // graphics not available
96 egScreenWidth
= UGAWidth
;
97 egScreenHeight
= UGAHeight
;
103 // Sets the screen resolution to the specified value, if possible.
104 // If the specified value is not valid, displays a warning with the valid
105 // modes on UEFI systems, or silently fails on EFI 1.x systems. Note that
106 // this function attempts to set ANY screen resolution, even 0x0 or
107 // ridiculously large values.
108 // Returns TRUE if successful, FALSE if not.
109 BOOLEAN
egSetScreenSize(IN UINTN ScreenWidth
, IN UINTN ScreenHeight
) {
110 EFI_STATUS Status
= EFI_SUCCESS
;
111 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
*Info
;
114 BOOLEAN ModeSet
= FALSE
;
115 UINT32 UGAWidth
, UGAHeight
, UGADepth
, UGARefreshRate
;
117 if (GraphicsOutput
!= NULL
) { // GOP mode (UEFI)
118 // Do a loop through the modes to see if the specified one is available;
119 // and if so, switch to it....
120 while ((Status
== EFI_SUCCESS
) && (!ModeSet
)) {
121 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, ModeNum
, &Size
, &Info
);
122 if ((Status
== EFI_SUCCESS
) && (Size
>= sizeof(*Info
)) &&
123 (Info
->HorizontalResolution
== ScreenWidth
) && (Info
->VerticalResolution
== ScreenHeight
)) {
124 Status
= refit_call2_wrapper(GraphicsOutput
->SetMode
, GraphicsOutput
, ModeNum
);
125 ModeSet
= (Status
== EFI_SUCCESS
);
131 egScreenWidth
= ScreenWidth
;
132 egScreenHeight
= ScreenHeight
;
133 } else {// If unsuccessful, display an error message for the user....
134 Print(L
"Error setting mode %d x %d; using default mode!\nAvailable modes are:\n", ScreenWidth
, ScreenHeight
);
136 Status
= EFI_SUCCESS
;
137 while (Status
== EFI_SUCCESS
) {
138 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, ModeNum
, &Size
, &Info
);
139 if ((Status
== EFI_SUCCESS
) && (Size
>= sizeof(*Info
))) {
140 Print(L
"Mode %d: %d x %d\n", ModeNum
, Info
->HorizontalResolution
, Info
->VerticalResolution
);
146 } else if (UgaDraw
!= NULL
) { // UGA mode (EFI 1.x)
147 // Try to use current color depth & refresh rate for new mode. Maybe not the best choice
148 // in all cases, but I don't know how to probe for alternatives....
149 Status
= refit_call5_wrapper(UgaDraw
->GetMode
, UgaDraw
, &UGAWidth
, &UGAHeight
, &UGADepth
, &UGARefreshRate
);
150 Status
= refit_call5_wrapper(UgaDraw
->SetMode
, UgaDraw
, ScreenWidth
, ScreenHeight
, UGADepth
, UGARefreshRate
);
151 if (Status
== EFI_SUCCESS
) {
152 egScreenWidth
= ScreenWidth
;
153 egScreenHeight
= ScreenHeight
;
156 // TODO: Find a list of supported modes and display it.
157 // NOTE: Below doesn't actually appear unless we explicitly switch to text mode.
158 // This is just a placeholder until something better can be done....
159 Print(L
"Error setting mode %d x %d; unsupported mode!\n");
163 } // BOOLEAN egSetScreenSize()
165 VOID
egGetScreenSize(OUT UINTN
*ScreenWidth
, OUT UINTN
*ScreenHeight
)
167 if (ScreenWidth
!= NULL
)
168 *ScreenWidth
= egScreenWidth
;
169 if (ScreenHeight
!= NULL
)
170 *ScreenHeight
= egScreenHeight
;
173 CHAR16
* egScreenDescription(VOID
)
178 if (GraphicsOutput
!= NULL
) {
179 Temp
= AllocateZeroPool(256 * sizeof(CHAR16
));
180 SPrint(Temp
, 255, L
"Graphics Output (UEFI), %dx%d", egScreenWidth
, egScreenHeight
);
182 } else if (UgaDraw
!= NULL
) {
183 Temp
= AllocateZeroPool(256 * sizeof(CHAR16
));
184 SPrint(Temp
, 255, L
"UGA Draw (EFI 1.10), %dx%d", egScreenWidth
, egScreenHeight
);
187 return L
"Internal Error";
190 return L
"Text Console";
194 BOOLEAN
egHasGraphicsMode(VOID
)
196 return egHasGraphics
;
199 BOOLEAN
egIsGraphicsModeEnabled(VOID
)
201 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode
;
203 if (ConsoleControl
!= NULL
) {
204 refit_call4_wrapper(ConsoleControl
->GetMode
, ConsoleControl
, &CurrentMode
, NULL
, NULL
);
205 return (CurrentMode
== EfiConsoleControlScreenGraphics
) ? TRUE
: FALSE
;
211 VOID
egSetGraphicsModeEnabled(IN BOOLEAN Enable
)
213 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode
;
214 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode
;
216 if (ConsoleControl
!= NULL
) {
217 refit_call4_wrapper(ConsoleControl
->GetMode
, ConsoleControl
, &CurrentMode
, NULL
, NULL
);
219 NewMode
= Enable
? EfiConsoleControlScreenGraphics
220 : EfiConsoleControlScreenText
;
221 if (CurrentMode
!= NewMode
)
222 refit_call2_wrapper(ConsoleControl
->SetMode
, ConsoleControl
, NewMode
);
227 // Drawing to the screen
230 VOID
egClearScreen(IN EG_PIXEL
*Color
)
232 EFI_UGA_PIXEL FillColor
;
237 FillColor
.Red
= Color
->r
;
238 FillColor
.Green
= Color
->g
;
239 FillColor
.Blue
= Color
->b
;
240 FillColor
.Reserved
= 0;
242 if (GraphicsOutput
!= NULL
) {
243 // EFI_GRAPHICS_OUTPUT_BLT_PIXEL and EFI_UGA_PIXEL have the same
244 // layout, and the header from TianoCore actually defines them
245 // to be the same type.
246 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)&FillColor
, EfiBltVideoFill
,
247 0, 0, 0, 0, egScreenWidth
, egScreenHeight
, 0);
248 } else if (UgaDraw
!= NULL
) {
249 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, &FillColor
, EfiUgaVideoFill
,
250 0, 0, 0, 0, egScreenWidth
, egScreenHeight
, 0);
254 VOID
egDrawImage(IN EG_IMAGE
*Image
, IN UINTN ScreenPosX
, IN UINTN ScreenPosY
)
259 if (Image
->HasAlpha
) {
260 Image
->HasAlpha
= FALSE
;
261 egSetPlane(PLPTR(Image
, a
), 0, Image
->Width
* Image
->Height
);
264 if (GraphicsOutput
!= NULL
) {
265 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltBufferToVideo
,
266 0, 0, ScreenPosX
, ScreenPosY
, Image
->Width
, Image
->Height
, 0);
267 } else if (UgaDraw
!= NULL
) {
268 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaBltBufferToVideo
,
269 0, 0, ScreenPosX
, ScreenPosY
, Image
->Width
, Image
->Height
, 0);
273 VOID
egDrawImageArea(IN EG_IMAGE
*Image
,
274 IN UINTN AreaPosX
, IN UINTN AreaPosY
,
275 IN UINTN AreaWidth
, IN UINTN AreaHeight
,
276 IN UINTN ScreenPosX
, IN UINTN ScreenPosY
)
281 egRestrictImageArea(Image
, AreaPosX
, AreaPosY
, &AreaWidth
, &AreaHeight
);
285 if (Image
->HasAlpha
) {
286 Image
->HasAlpha
= FALSE
;
287 egSetPlane(PLPTR(Image
, a
), 0, Image
->Width
* Image
->Height
);
290 if (GraphicsOutput
!= NULL
) {
291 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltBufferToVideo
,
292 AreaPosX
, AreaPosY
, ScreenPosX
, ScreenPosY
, AreaWidth
, AreaHeight
, Image
->Width
* 4);
293 } else if (UgaDraw
!= NULL
) {
294 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaBltBufferToVideo
,
295 AreaPosX
, AreaPosY
, ScreenPosX
, ScreenPosY
, AreaWidth
, AreaHeight
, Image
->Width
* 4);
299 // Display a message in the center of the screen, surrounded by a box of the
300 // specified color. For the moment, uses graphics calls only. (It still works
301 // in text mode on GOP/UEFI systems, but not on UGA/EFI 1.x systems.)
302 VOID
egDisplayMessage(IN CHAR16
*Text
, EG_PIXEL
*BGColor
) {
303 UINTN BoxWidth
, BoxHeight
;
306 if ((Text
!= NULL
) && (BGColor
!= NULL
)) {
307 BoxWidth
= (StrLen(Text
) + 2) * FONT_CELL_WIDTH
;
308 if (BoxWidth
> egScreenWidth
)
309 BoxWidth
= egScreenWidth
;
310 BoxHeight
= 2 * FONT_CELL_HEIGHT
;
311 Box
= egCreateFilledImage(BoxWidth
, BoxHeight
, FALSE
, BGColor
);
312 egRenderText(Text
, Box
, FONT_CELL_WIDTH
, FONT_CELL_HEIGHT
/ 2);
313 egDrawImage(Box
, (egScreenWidth
- BoxWidth
) / 2, (egScreenHeight
- BoxHeight
) / 2);
314 } // if non-NULL inputs
315 } // VOID egDisplayMessage()
321 VOID
egScreenShot(VOID
)
326 UINTN FileDataLength
;
332 // allocate a buffer for the whole screen
333 Image
= egCreateImage(egScreenWidth
, egScreenHeight
, FALSE
);
335 Print(L
"Error egCreateImage returned NULL\n");
339 // get full screen image
340 if (GraphicsOutput
!= NULL
) {
341 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltVideoToBltBuffer
,
342 0, 0, 0, 0, Image
->Width
, Image
->Height
, 0);
343 } else if (UgaDraw
!= NULL
) {
344 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaVideoToBltBuffer
,
345 0, 0, 0, 0, Image
->Width
, Image
->Height
, 0);
349 egEncodeBMP(Image
, &FileData
, &FileDataLength
);
351 if (FileData
== NULL
) {
352 Print(L
"Error egEncodeBMP returned NULL\n");
356 // save to file on the ESP
357 Status
= egSaveFile(NULL
, L
"screenshot.bmp", FileData
, FileDataLength
);
359 if (EFI_ERROR(Status
)) {
360 Print(L
"Error egSaveFile: %x\n", Status
);
366 // DEBUG: switch to text mode
368 egSetGraphicsModeEnabled(FALSE
);
369 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &Index
);