]> code.delx.au - refind/blob - libeg/screen.c
Fixed screen-clearing bug; display fs type & size rather than blank
[refind] / libeg / screen.c
1 /*
2 * libeg/screen.c
3 * Screen handling functions
4 *
5 * Copyright (c) 2006 Christoph Pfisterer
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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
18 * distribution.
19 *
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.
23 *
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.
35 */
36
37 #include "libegint.h"
38 #include "../refind/screen.h"
39 #include "../refind/lib.h"
40 #include "../include/refit_call_wrapper.h"
41
42 #include <efiUgaDraw.h>
43 #include <efiConsoleControl.h>
44
45 #ifndef __MAKEWITH_GNUEFI
46 #define LibLocateProtocol EfiLibLocateProtocol
47 #endif
48
49 // Console defines and variables
50
51 static EFI_GUID ConsoleControlProtocolGuid = EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
52 static EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl = NULL;
53
54 static EFI_GUID UgaDrawProtocolGuid = EFI_UGA_DRAW_PROTOCOL_GUID;
55 static EFI_UGA_DRAW_PROTOCOL *UgaDraw = NULL;
56
57 static EFI_GUID GraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
58 static EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL;
59
60 static BOOLEAN egHasGraphics = FALSE;
61 static UINTN egScreenWidth = 800;
62 static UINTN egScreenHeight = 600;
63
64 //
65 // Screen handling
66 //
67
68 VOID egInitScreen(VOID)
69 {
70 EFI_STATUS Status = EFI_SUCCESS;
71 UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate;
72
73 // get protocols
74 Status = LibLocateProtocol(&ConsoleControlProtocolGuid, (VOID **) &ConsoleControl);
75 if (EFI_ERROR(Status))
76 ConsoleControl = NULL;
77
78 Status = LibLocateProtocol(&UgaDrawProtocolGuid, (VOID **) &UgaDraw);
79 if (EFI_ERROR(Status))
80 UgaDraw = NULL;
81
82 Status = LibLocateProtocol(&GraphicsOutputProtocolGuid, (VOID **) &GraphicsOutput);
83 if (EFI_ERROR(Status))
84 GraphicsOutput = NULL;
85
86 // get screen size
87 egHasGraphics = FALSE;
88 if (GraphicsOutput != NULL) {
89 egScreenWidth = GraphicsOutput->Mode->Info->HorizontalResolution;
90 egScreenHeight = GraphicsOutput->Mode->Info->VerticalResolution;
91 egHasGraphics = TRUE;
92 } else if (UgaDraw != NULL) {
93 Status = refit_call5_wrapper(UgaDraw->GetMode, UgaDraw, &UGAWidth, &UGAHeight, &UGADepth, &UGARefreshRate);
94 if (EFI_ERROR(Status)) {
95 UgaDraw = NULL; // graphics not available
96 } else {
97 egScreenWidth = UGAWidth;
98 egScreenHeight = UGAHeight;
99 egHasGraphics = TRUE;
100 }
101 }
102 }
103
104 // Sets the screen resolution to the specified value, if possible. If *ScreenHeight
105 // is 0 and GOP mode is detected, assume that *ScreenWidth contains a GOP mode
106 // number rather than a horizontal resolution. If the specified resolution is not
107 // valid, displays a warning with the valid modes on GOP (UEFI) systems, or silently
108 // fails on UGA (EFI 1.x) systems. Note that this function attempts to set ANY screen
109 // resolution, even 0x0 or ridiculously large values.
110 // Upon success, returns actual screen resolution in *ScreenWidth and *ScreenHeight.
111 // These values are unchanged upon failure.
112 // Returns TRUE if successful, FALSE if not.
113 BOOLEAN egSetScreenSize(IN OUT UINTN *ScreenWidth, IN OUT UINTN *ScreenHeight) {
114 EFI_STATUS Status = EFI_SUCCESS;
115 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;
116 UINT32 ModeNum = 0;
117 UINTN Size;
118 BOOLEAN ModeSet = FALSE;
119 UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate;
120
121 if (GraphicsOutput != NULL) { // GOP mode (UEFI)
122 if (*ScreenHeight == 0) { // User specified a mode number (stored in *ScreenWidth); use it directly
123 if (*ScreenWidth == GraphicsOutput->Mode->Mode) { // user requested current mode; do nothing
124 ModeSet = TRUE;
125 } else {
126 ModeNum = (UINT32) *ScreenWidth;
127 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
128 if (Status == EFI_SUCCESS) {
129 Status = refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum);
130 if (Status == EFI_SUCCESS) {
131 ModeSet = TRUE;
132 *ScreenWidth = Info->HorizontalResolution;
133 *ScreenHeight = Info->VerticalResolution;
134 } // if set mode OK
135 } // if queried mode OK
136 } // if/else
137
138 // User specified width & height; must find mode -- but only if change is required....
139 } else {
140 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, GraphicsOutput->Mode->Mode, &Size, &Info);
141 if ((Status == EFI_SUCCESS) && (Info->HorizontalResolution == *ScreenWidth) &&
142 (Info->VerticalResolution == *ScreenHeight)) {
143 ModeSet = TRUE; // user requested current mode; do nothing
144 } else {
145 // Do a loop through the modes to see if the specified one is available;
146 // and if so, switch to it....
147 do {
148 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
149 if ((Status == EFI_SUCCESS) && (Size >= sizeof(*Info)) &&
150 (Info->HorizontalResolution == *ScreenWidth) && (Info->VerticalResolution == *ScreenHeight)) {
151 Status = refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum);
152 ModeSet = (Status == EFI_SUCCESS);
153 } // if
154 } while ((++ModeNum < GraphicsOutput->Mode->MaxMode) && !ModeSet);
155 } // if/else
156 } // if/else
157
158 if (ModeSet) {
159 egScreenWidth = *ScreenWidth;
160 egScreenHeight = *ScreenHeight;
161 } else {// If unsuccessful, display an error message for the user....
162 SwitchToText(FALSE);
163 Print(L"Error setting graphics mode %d x %d; using default mode!\nAvailable modes are:\n", *ScreenWidth, *ScreenHeight);
164 ModeNum = 0;
165 do {
166 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
167 if ((Status == EFI_SUCCESS) && (Info != NULL)) {
168 Print(L"Mode %d: %d x %d\n", ModeNum, Info->HorizontalResolution, Info->VerticalResolution);
169 } // else
170 } while (++ModeNum < GraphicsOutput->Mode->MaxMode);
171 PauseForKey();
172 SwitchToGraphics();
173 } // if()
174
175 } else if (UgaDraw != NULL) { // UGA mode (EFI 1.x)
176 // Try to use current color depth & refresh rate for new mode. Maybe not the best choice
177 // in all cases, but I don't know how to probe for alternatives....
178 Status = refit_call5_wrapper(UgaDraw->GetMode, UgaDraw, &UGAWidth, &UGAHeight, &UGADepth, &UGARefreshRate);
179 Status = refit_call5_wrapper(UgaDraw->SetMode, UgaDraw, *ScreenWidth, *ScreenHeight, UGADepth, UGARefreshRate);
180 if (Status == EFI_SUCCESS) {
181 egScreenWidth = *ScreenWidth;
182 egScreenHeight = *ScreenHeight;
183 ModeSet = TRUE;
184 } else {
185 // TODO: Find a list of supported modes and display it.
186 // NOTE: Below doesn't actually appear unless we explicitly switch to text mode.
187 // This is just a placeholder until something better can be done....
188 Print(L"Error setting graphics mode %d x %d; unsupported mode!\n");
189 } // if/else
190 } // if/else if
191 return (ModeSet);
192 } // BOOLEAN egSetScreenSize()
193
194 VOID egGetScreenSize(OUT UINTN *ScreenWidth, OUT UINTN *ScreenHeight)
195 {
196 if (ScreenWidth != NULL)
197 *ScreenWidth = egScreenWidth;
198 if (ScreenHeight != NULL)
199 *ScreenHeight = egScreenHeight;
200 }
201
202 // Set a text mode
203 // Returns the mode that was actually set.
204 UINT32 egSetTextMode(UINT32 RequestedMode) {
205 UINTN i = 0, Width, Height;
206 UINT32 UsedMode = ST->ConOut->Mode->Mode;
207 EFI_STATUS Status;
208
209 if (RequestedMode != ST->ConOut->Mode->Mode) {
210 Status = refit_call2_wrapper(ST->ConOut->SetMode, ST->ConOut, RequestedMode);
211 if (Status == EFI_SUCCESS) {
212 UsedMode = RequestedMode;
213 } else {
214 SwitchToText(FALSE);
215 Print(L"Error setting text mode %d; available modes are:\n", RequestedMode);
216 do {
217 Status = refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, i, &Width, &Height);
218 if (Status == EFI_SUCCESS)
219 Print(L"Mode %d: %d x %d\n", i, Width, Height);
220 } while (++i < ST->ConOut->Mode->MaxMode);
221
222 PauseForKey();
223 SwitchToGraphics();
224 } // if/else successful change
225 } // if need to change mode
226 return UsedMode;
227 } // UINT32 egSetTextMode()
228
229 CHAR16 * egScreenDescription(VOID)
230 {
231 CHAR16 *GraphicsInfo, *TextInfo = NULL;
232
233 GraphicsInfo = AllocateZeroPool(256 * sizeof(CHAR16));
234 if (GraphicsInfo == NULL)
235 return L"memory allocation error";
236
237 if (egHasGraphics) {
238 if (GraphicsOutput != NULL) {
239 SPrint(GraphicsInfo, 255, L"Graphics Output (UEFI), %dx%d", egScreenWidth, egScreenHeight);
240 } else if (UgaDraw != NULL) {
241 GraphicsInfo = AllocateZeroPool(256 * sizeof(CHAR16));
242 SPrint(GraphicsInfo, 255, L"UGA Draw (EFI 1.10), %dx%d", egScreenWidth, egScreenHeight);
243 } else {
244 MyFreePool(GraphicsInfo);
245 MyFreePool(TextInfo);
246 return L"Internal Error";
247 }
248 if (!AllowGraphicsMode) { // graphics-capable HW, but in text mode
249 TextInfo = AllocateZeroPool(256 * sizeof(CHAR16));
250 SPrint(TextInfo, 255, L"(in %dx%d text mode)", ConWidth, ConHeight);
251 MergeStrings(&GraphicsInfo, TextInfo, L' ');
252 }
253 } else {
254 SPrint(GraphicsInfo, 255, L"Text-foo console, %dx%d", ConWidth, ConHeight);
255 }
256 MyFreePool(TextInfo);
257 return GraphicsInfo;
258 }
259
260 BOOLEAN egHasGraphicsMode(VOID)
261 {
262 return egHasGraphics;
263 }
264
265 BOOLEAN egIsGraphicsModeEnabled(VOID)
266 {
267 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
268
269 if (ConsoleControl != NULL) {
270 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
271 return (CurrentMode == EfiConsoleControlScreenGraphics) ? TRUE : FALSE;
272 }
273
274 return FALSE;
275 }
276
277 VOID egSetGraphicsModeEnabled(IN BOOLEAN Enable)
278 {
279 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
280 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode;
281
282 if (ConsoleControl != NULL) {
283 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
284
285 NewMode = Enable ? EfiConsoleControlScreenGraphics
286 : EfiConsoleControlScreenText;
287 if (CurrentMode != NewMode)
288 refit_call2_wrapper(ConsoleControl->SetMode, ConsoleControl, NewMode);
289 }
290 }
291
292 //
293 // Drawing to the screen
294 //
295
296 VOID egClearScreen(IN EG_PIXEL *Color)
297 {
298 EFI_UGA_PIXEL FillColor;
299
300 if (!egHasGraphics)
301 return;
302
303 if (Color != NULL) {
304 FillColor.Red = Color->r;
305 FillColor.Green = Color->g;
306 FillColor.Blue = Color->b;
307 } else {
308 FillColor.Red = 0x0;
309 FillColor.Green = 0x0;
310 FillColor.Blue = 0x0;
311 }
312 FillColor.Reserved = 0;
313
314 if (GraphicsOutput != NULL) {
315 // EFI_GRAPHICS_OUTPUT_BLT_PIXEL and EFI_UGA_PIXEL have the same
316 // layout, and the header from TianoCore actually defines them
317 // to be the same type.
318 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)&FillColor, EfiBltVideoFill,
319 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
320 } else if (UgaDraw != NULL) {
321 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, &FillColor, EfiUgaVideoFill, 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
322 }
323 }
324
325 VOID egDrawImage(IN EG_IMAGE *Image, IN UINTN ScreenPosX, IN UINTN ScreenPosY)
326 {
327 if (!egHasGraphics)
328 return;
329
330 if (Image->HasAlpha) {
331 Image->HasAlpha = FALSE;
332 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
333 }
334
335 if (GraphicsOutput != NULL) {
336 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltBufferToVideo,
337 0, 0, ScreenPosX, ScreenPosY, Image->Width, Image->Height, 0);
338 } else if (UgaDraw != NULL) {
339 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaBltBufferToVideo,
340 0, 0, ScreenPosX, ScreenPosY, Image->Width, Image->Height, 0);
341 }
342 }
343
344 VOID egDrawImageArea(IN EG_IMAGE *Image,
345 IN UINTN AreaPosX, IN UINTN AreaPosY,
346 IN UINTN AreaWidth, IN UINTN AreaHeight,
347 IN UINTN ScreenPosX, IN UINTN ScreenPosY)
348 {
349 if (!egHasGraphics)
350 return;
351
352 egRestrictImageArea(Image, AreaPosX, AreaPosY, &AreaWidth, &AreaHeight);
353 if (AreaWidth == 0)
354 return;
355
356 if (Image->HasAlpha) {
357 Image->HasAlpha = FALSE;
358 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
359 }
360
361 if (GraphicsOutput != NULL) {
362 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltBufferToVideo,
363 AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight, Image->Width * 4);
364 } else if (UgaDraw != NULL) {
365 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaBltBufferToVideo,
366 AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight, Image->Width * 4);
367 }
368 }
369
370 // Display a message in the center of the screen, surrounded by a box of the
371 // specified color. For the moment, uses graphics calls only. (It still works
372 // in text mode on GOP/UEFI systems, but not on UGA/EFI 1.x systems.)
373 VOID egDisplayMessage(IN CHAR16 *Text, EG_PIXEL *BGColor) {
374 UINTN BoxWidth, BoxHeight;
375 EG_IMAGE *Box;
376
377 if ((Text != NULL) && (BGColor != NULL)) {
378 BoxWidth = (StrLen(Text) + 2) * FONT_CELL_WIDTH;
379 if (BoxWidth > egScreenWidth)
380 BoxWidth = egScreenWidth;
381 BoxHeight = 2 * FONT_CELL_HEIGHT;
382 Box = egCreateFilledImage(BoxWidth, BoxHeight, FALSE, BGColor);
383 egRenderText(Text, Box, FONT_CELL_WIDTH, FONT_CELL_HEIGHT / 2);
384 egDrawImage(Box, (egScreenWidth - BoxWidth) / 2, (egScreenHeight - BoxHeight) / 2);
385 } // if non-NULL inputs
386 } // VOID egDisplayMessage()
387
388 //
389 // Make a screenshot
390 //
391
392 VOID egScreenShot(VOID)
393 {
394 EFI_STATUS Status;
395 EG_IMAGE *Image;
396 UINT8 *FileData;
397 UINTN FileDataLength;
398 UINTN Index;
399
400 if (!egHasGraphics)
401 return;
402
403 // allocate a buffer for the whole screen
404 Image = egCreateImage(egScreenWidth, egScreenHeight, FALSE);
405 if (Image == NULL) {
406 Print(L"Error egCreateImage returned NULL\n");
407 goto bailout_wait;
408 }
409
410 // get full screen image
411 if (GraphicsOutput != NULL) {
412 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltVideoToBltBuffer,
413 0, 0, 0, 0, Image->Width, Image->Height, 0);
414 } else if (UgaDraw != NULL) {
415 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaVideoToBltBuffer,
416 0, 0, 0, 0, Image->Width, Image->Height, 0);
417 }
418
419 // encode as BMP
420 egEncodeBMP(Image, &FileData, &FileDataLength);
421 egFreeImage(Image);
422 if (FileData == NULL) {
423 Print(L"Error egEncodeBMP returned NULL\n");
424 goto bailout_wait;
425 }
426
427 // save to file on the ESP
428 Status = egSaveFile(NULL, L"screenshot.bmp", FileData, FileDataLength);
429 FreePool(FileData);
430 if (EFI_ERROR(Status)) {
431 Print(L"Error egSaveFile: %x\n", Status);
432 goto bailout_wait;
433 }
434
435 return;
436
437 // DEBUG: switch to text mode
438 bailout_wait:
439 egSetGraphicsModeEnabled(FALSE);
440 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &Index);
441 }
442
443 /* EOF */