]> code.delx.au - refind/blob - libeg/screen.c
Better reporting of SIP problems; minor code cleanup.
[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 * Modifications copyright (c) 2012-2014 Roderick W. Smith
38 *
39 * Modifications distributed under the terms of the GNU General Public
40 * License (GPL) version 3 (GPLv3), or (at your option) any later version.
41 *
42 */
43 /*
44 * This program is free software: you can redistribute it and/or modify
45 * it under the terms of the GNU General Public License as published by
46 * the Free Software Foundation, either version 3 of the License, or
47 * (at your option) any later version.
48 *
49 * This program is distributed in the hope that it will be useful,
50 * but WITHOUT ANY WARRANTY; without even the implied warranty of
51 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52 * GNU General Public License for more details.
53 *
54 * You should have received a copy of the GNU General Public License
55 * along with this program. If not, see <http://www.gnu.org/licenses/>.
56 */
57
58 #include "libegint.h"
59 #include "../refind/screen.h"
60 #include "../refind/lib.h"
61 #include "../include/refit_call_wrapper.h"
62 #include "libeg.h"
63 #include "../include/Handle.h"
64
65 #include <efiUgaDraw.h>
66 #include <efiConsoleControl.h>
67
68 #ifndef __MAKEWITH_GNUEFI
69 #define LibLocateProtocol EfiLibLocateProtocol
70 #endif
71
72 // Console defines and variables
73
74 static EFI_GUID ConsoleControlProtocolGuid = EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
75 static EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl = NULL;
76
77 static EFI_GUID UgaDrawProtocolGuid = EFI_UGA_DRAW_PROTOCOL_GUID;
78 static EFI_UGA_DRAW_PROTOCOL *UgaDraw = NULL;
79
80 static EFI_GUID GraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
81 static EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL;
82
83 static BOOLEAN egHasGraphics = FALSE;
84 static UINTN egScreenWidth = 800;
85 static UINTN egScreenHeight = 600;
86
87 //
88 // Screen handling
89 //
90
91 // Make the necessary system calls to identify the current graphics mode.
92 // Stores the results in the file-global variables egScreenWidth,
93 // egScreenHeight, and egHasGraphics. The first two of these will be
94 // unchanged if neither GraphicsOutput nor UgaDraw is a valid pointer.
95 static VOID egDetermineScreenSize(VOID) {
96 EFI_STATUS Status = EFI_SUCCESS;
97 UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate;
98
99 // get screen size
100 egHasGraphics = FALSE;
101 if (GraphicsOutput != NULL) {
102 egScreenWidth = GraphicsOutput->Mode->Info->HorizontalResolution;
103 egScreenHeight = GraphicsOutput->Mode->Info->VerticalResolution;
104 egHasGraphics = TRUE;
105 } else if (UgaDraw != NULL) {
106 Status = refit_call5_wrapper(UgaDraw->GetMode, UgaDraw, &UGAWidth, &UGAHeight, &UGADepth, &UGARefreshRate);
107 if (EFI_ERROR(Status)) {
108 UgaDraw = NULL; // graphics not available
109 } else {
110 egScreenWidth = UGAWidth;
111 egScreenHeight = UGAHeight;
112 egHasGraphics = TRUE;
113 }
114 }
115 } // static VOID egDetermineScreenSize()
116
117 VOID egGetScreenSize(OUT UINTN *ScreenWidth, OUT UINTN *ScreenHeight)
118 {
119 egDetermineScreenSize();
120
121 if (ScreenWidth != NULL)
122 *ScreenWidth = egScreenWidth;
123 if (ScreenHeight != NULL)
124 *ScreenHeight = egScreenHeight;
125 }
126
127 VOID egInitScreen(VOID)
128 {
129 EFI_STATUS Status = EFI_SUCCESS;
130
131 // get protocols
132 Status = LibLocateProtocol(&ConsoleControlProtocolGuid, (VOID **) &ConsoleControl);
133 if (EFI_ERROR(Status))
134 ConsoleControl = NULL;
135
136 Status = LibLocateProtocol(&UgaDrawProtocolGuid, (VOID **) &UgaDraw);
137 if (EFI_ERROR(Status))
138 UgaDraw = NULL;
139
140 Status = LibLocateProtocol(&GraphicsOutputProtocolGuid, (VOID **) &GraphicsOutput);
141 if (EFI_ERROR(Status))
142 GraphicsOutput = NULL;
143
144 egDetermineScreenSize();
145 }
146
147 // Convert a graphics mode (in *ModeWidth) to a width and height (returned in
148 // *ModeWidth and *Height, respectively).
149 // Returns TRUE if successful, FALSE if not (invalid mode, typically)
150 BOOLEAN egGetResFromMode(UINTN *ModeWidth, UINTN *Height) {
151 UINTN Size;
152 EFI_STATUS Status;
153 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info = NULL;
154
155 if ((ModeWidth != NULL) && (Height != NULL)) {
156 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, *ModeWidth, &Size, &Info);
157 if ((Status == EFI_SUCCESS) && (Info != NULL)) {
158 *ModeWidth = Info->HorizontalResolution;
159 *Height = Info->VerticalResolution;
160 return TRUE;
161 }
162 }
163 return FALSE;
164 } // BOOLEAN egGetResFromMode()
165
166 // Sets the screen resolution to the specified value, if possible. If *ScreenHeight
167 // is 0 and GOP mode is detected, assume that *ScreenWidth contains a GOP mode
168 // number rather than a horizontal resolution. If the specified resolution is not
169 // valid, displays a warning with the valid modes on GOP (UEFI) systems, or silently
170 // fails on UGA (EFI 1.x) systems. Note that this function attempts to set ANY screen
171 // resolution, even 0x0 or ridiculously large values.
172 // Upon success, returns actual screen resolution in *ScreenWidth and *ScreenHeight.
173 // These values are unchanged upon failure.
174 // Returns TRUE if successful, FALSE if not.
175 BOOLEAN egSetScreenSize(IN OUT UINTN *ScreenWidth, IN OUT UINTN *ScreenHeight) {
176 EFI_STATUS Status = EFI_SUCCESS;
177 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;
178 UINTN Size;
179 UINT32 ModeNum = 0;
180 UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate;
181 BOOLEAN ModeSet = FALSE;
182
183 if ((ScreenWidth == NULL) || (ScreenHeight == NULL))
184 return FALSE;
185
186 if (GraphicsOutput != NULL) { // GOP mode (UEFI)
187 if (*ScreenHeight == 0) { // User specified a mode number (stored in *ScreenWidth); use it directly
188 ModeNum = (UINT32) *ScreenWidth;
189 if (egGetResFromMode(ScreenWidth, ScreenHeight) &&
190 (refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum) == EFI_SUCCESS)) {
191 ModeSet = TRUE;
192 }
193
194 // User specified width & height; must find mode...
195 } else {
196 // Do a loop through the modes to see if the specified one is available;
197 // and if so, switch to it....
198 do {
199 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
200 if ((Status == EFI_SUCCESS) && (Size >= sizeof(*Info) && (Info != NULL)) &&
201 (Info->HorizontalResolution == *ScreenWidth) && (Info->VerticalResolution == *ScreenHeight)) {
202 Status = refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum);
203 ModeSet = (Status == EFI_SUCCESS);
204 } // if
205 } while ((++ModeNum < GraphicsOutput->Mode->MaxMode) && !ModeSet);
206 } // if/else
207
208 if (ModeSet) {
209 egScreenWidth = *ScreenWidth;
210 egScreenHeight = *ScreenHeight;
211 } else {// If unsuccessful, display an error message for the user....
212 SwitchToText(FALSE);
213 Print(L"Error setting graphics mode %d x %d; using default mode!\nAvailable modes are:\n", *ScreenWidth, *ScreenHeight);
214 ModeNum = 0;
215 do {
216 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
217 if ((Status == EFI_SUCCESS) && (Info != NULL)) {
218 Print(L"Mode %d: %d x %d\n", ModeNum, Info->HorizontalResolution, Info->VerticalResolution);
219 } // else
220 } while (++ModeNum < GraphicsOutput->Mode->MaxMode);
221 PauseForKey();
222 SwitchToGraphics();
223 } // if GOP mode (UEFI)
224
225 } else if (UgaDraw != NULL) { // UGA mode (EFI 1.x)
226 // Try to use current color depth & refresh rate for new mode. Maybe not the best choice
227 // in all cases, but I don't know how to probe for alternatives....
228 Status = refit_call5_wrapper(UgaDraw->GetMode, UgaDraw, &UGAWidth, &UGAHeight, &UGADepth, &UGARefreshRate);
229 Status = refit_call5_wrapper(UgaDraw->SetMode, UgaDraw, *ScreenWidth, *ScreenHeight, UGADepth, UGARefreshRate);
230 if (Status == EFI_SUCCESS) {
231 egScreenWidth = *ScreenWidth;
232 egScreenHeight = *ScreenHeight;
233 ModeSet = TRUE;
234 } else {
235 // TODO: Find a list of supported modes and display it.
236 // NOTE: Below doesn't actually appear unless we explicitly switch to text mode.
237 // This is just a placeholder until something better can be done....
238 Print(L"Error setting graphics mode %d x %d; unsupported mode!\n");
239 } // if/else
240 } // if/else if UGA mode (EFI 1.x)
241 return (ModeSet);
242 } // BOOLEAN egSetScreenSize()
243
244 // Set a text mode.
245 // Returns TRUE if the mode actually changed, FALSE otherwise.
246 // Note that a FALSE return value can mean either an error or no change
247 // necessary.
248 BOOLEAN egSetTextMode(UINT32 RequestedMode) {
249 UINTN i = 0, Width, Height;
250 EFI_STATUS Status;
251 BOOLEAN ChangedIt = FALSE;
252
253 if ((RequestedMode != DONT_CHANGE_TEXT_MODE) && (RequestedMode != ST->ConOut->Mode->Mode)) {
254 Status = refit_call2_wrapper(ST->ConOut->SetMode, ST->ConOut, RequestedMode);
255 if (Status == EFI_SUCCESS) {
256 ChangedIt = TRUE;
257 } else {
258 SwitchToText(FALSE);
259 Print(L"\nError setting text mode %d; available modes are:\n", RequestedMode);
260 do {
261 Status = refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, i, &Width, &Height);
262 if (Status == EFI_SUCCESS)
263 Print(L"Mode %d: %d x %d\n", i, Width, Height);
264 } while (++i < ST->ConOut->Mode->MaxMode);
265 Print(L"Mode %d: Use default mode\n", DONT_CHANGE_TEXT_MODE);
266
267 PauseForKey();
268 SwitchToGraphics();
269 } // if/else successful change
270 } // if need to change mode
271 return ChangedIt;
272 } // BOOLEAN egSetTextMode()
273
274 CHAR16 * egScreenDescription(VOID)
275 {
276 CHAR16 *GraphicsInfo, *TextInfo = NULL;
277
278 GraphicsInfo = AllocateZeroPool(256 * sizeof(CHAR16));
279 if (GraphicsInfo == NULL)
280 return L"memory allocation error";
281
282 if (egHasGraphics) {
283 if (GraphicsOutput != NULL) {
284 SPrint(GraphicsInfo, 255, L"Graphics Output (UEFI), %dx%d", egScreenWidth, egScreenHeight);
285 } else if (UgaDraw != NULL) {
286 GraphicsInfo = AllocateZeroPool(256 * sizeof(CHAR16));
287 SPrint(GraphicsInfo, 255, L"UGA Draw (EFI 1.10), %dx%d", egScreenWidth, egScreenHeight);
288 } else {
289 MyFreePool(GraphicsInfo);
290 MyFreePool(TextInfo);
291 return L"Internal Error";
292 }
293 if (!AllowGraphicsMode) { // graphics-capable HW, but in text mode
294 TextInfo = AllocateZeroPool(256 * sizeof(CHAR16));
295 SPrint(TextInfo, 255, L"(in %dx%d text mode)", ConWidth, ConHeight);
296 MergeStrings(&GraphicsInfo, TextInfo, L' ');
297 }
298 } else {
299 SPrint(GraphicsInfo, 255, L"Text-foo console, %dx%d", ConWidth, ConHeight);
300 }
301 MyFreePool(TextInfo);
302 return GraphicsInfo;
303 }
304
305 BOOLEAN egHasGraphicsMode(VOID)
306 {
307 return egHasGraphics;
308 }
309
310 BOOLEAN egIsGraphicsModeEnabled(VOID)
311 {
312 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
313
314 if (ConsoleControl != NULL) {
315 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
316 return (CurrentMode == EfiConsoleControlScreenGraphics) ? TRUE : FALSE;
317 }
318
319 return FALSE;
320 }
321
322 VOID egSetGraphicsModeEnabled(IN BOOLEAN Enable)
323 {
324 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
325 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode;
326
327 if (ConsoleControl != NULL) {
328 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
329
330 NewMode = Enable ? EfiConsoleControlScreenGraphics
331 : EfiConsoleControlScreenText;
332 if (CurrentMode != NewMode)
333 refit_call2_wrapper(ConsoleControl->SetMode, ConsoleControl, NewMode);
334 }
335 }
336
337 //
338 // Drawing to the screen
339 //
340
341 VOID egClearScreen(IN EG_PIXEL *Color)
342 {
343 EFI_UGA_PIXEL FillColor;
344
345 if (!egHasGraphics)
346 return;
347
348 if (Color != NULL) {
349 FillColor.Red = Color->r;
350 FillColor.Green = Color->g;
351 FillColor.Blue = Color->b;
352 } else {
353 FillColor.Red = 0x0;
354 FillColor.Green = 0x0;
355 FillColor.Blue = 0x0;
356 }
357 FillColor.Reserved = 0;
358
359 if (GraphicsOutput != NULL) {
360 // EFI_GRAPHICS_OUTPUT_BLT_PIXEL and EFI_UGA_PIXEL have the same
361 // layout, and the header from TianoCore actually defines them
362 // to be the same type.
363 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)&FillColor, EfiBltVideoFill,
364 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
365 } else if (UgaDraw != NULL) {
366 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, &FillColor, EfiUgaVideoFill, 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
367 }
368 }
369
370 VOID egDrawImage(IN EG_IMAGE *Image, IN UINTN ScreenPosX, IN UINTN ScreenPosY)
371 {
372 EG_IMAGE *CompImage = NULL;
373
374 // NOTE: Weird seemingly redundant tests because some placement code can "wrap around" and
375 // send "negative" values, which of course become very large unsigned ints that can then
376 // wrap around AGAIN if values are added to them.....
377 if (!egHasGraphics || ((ScreenPosX + Image->Width) > egScreenWidth) || ((ScreenPosY + Image->Height) > egScreenHeight) ||
378 (ScreenPosX > egScreenWidth) || (ScreenPosY > egScreenHeight))
379 return;
380
381 if ((GlobalConfig.ScreenBackground == NULL) || ((Image->Width == egScreenWidth) && (Image->Height == egScreenHeight))) {
382 CompImage = Image;
383 } else if (GlobalConfig.ScreenBackground == Image) {
384 CompImage = GlobalConfig.ScreenBackground;
385 } else {
386 CompImage = egCropImage(GlobalConfig.ScreenBackground, ScreenPosX, ScreenPosY, Image->Width, Image->Height);
387 if (CompImage == NULL) {
388 Print(L"Error! Can't crop image in egDrawImage()!\n");
389 return;
390 }
391 egComposeImage(CompImage, Image, 0, 0);
392 }
393
394 if (GraphicsOutput != NULL) {
395 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)CompImage->PixelData,
396 EfiBltBufferToVideo, 0, 0, ScreenPosX, ScreenPosY, CompImage->Width, CompImage->Height, 0);
397 } else if (UgaDraw != NULL) {
398 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)CompImage->PixelData, EfiUgaBltBufferToVideo,
399 0, 0, ScreenPosX, ScreenPosY, CompImage->Width, CompImage->Height, 0);
400 }
401 if ((CompImage != GlobalConfig.ScreenBackground) && (CompImage != Image))
402 egFreeImage(CompImage);
403 } /* VOID egDrawImage() */
404
405 // Display an unselected icon on the screen, so that the background image shows
406 // through the transparency areas. The BadgeImage may be NULL, in which case
407 // it's not composited in.
408 VOID egDrawImageWithTransparency(EG_IMAGE *Image, EG_IMAGE *BadgeImage, UINTN XPos, UINTN YPos, UINTN Width, UINTN Height) {
409 EG_IMAGE *Background;
410
411 Background = egCropImage(GlobalConfig.ScreenBackground, XPos, YPos, Width, Height);
412 if (Background != NULL) {
413 BltImageCompositeBadge(Background, Image, BadgeImage, XPos, YPos);
414 egFreeImage(Background);
415 }
416 } // VOID DrawImageWithTransparency()
417
418 VOID egDrawImageArea(IN EG_IMAGE *Image,
419 IN UINTN AreaPosX, IN UINTN AreaPosY,
420 IN UINTN AreaWidth, IN UINTN AreaHeight,
421 IN UINTN ScreenPosX, IN UINTN ScreenPosY)
422 {
423 if (!egHasGraphics)
424 return;
425
426 egRestrictImageArea(Image, AreaPosX, AreaPosY, &AreaWidth, &AreaHeight);
427 if (AreaWidth == 0)
428 return;
429
430 if (GraphicsOutput != NULL) {
431 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData,
432 EfiBltBufferToVideo, AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight,
433 Image->Width * 4);
434 } else if (UgaDraw != NULL) {
435 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaBltBufferToVideo,
436 AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight, Image->Width * 4);
437 }
438 }
439
440 // Display a message in the center of the screen, surrounded by a box of the
441 // specified color. For the moment, uses graphics calls only. (It still works
442 // in text mode on GOP/UEFI systems, but not on UGA/EFI 1.x systems.)
443 VOID egDisplayMessage(IN CHAR16 *Text, EG_PIXEL *BGColor) {
444 UINTN BoxWidth, BoxHeight;
445 EG_IMAGE *Box;
446
447 if ((Text != NULL) && (BGColor != NULL)) {
448 egMeasureText(Text, &BoxWidth, &BoxHeight);
449 BoxWidth += 14;
450 BoxHeight *= 2;
451 if (BoxWidth > egScreenWidth)
452 BoxWidth = egScreenWidth;
453 Box = egCreateFilledImage(BoxWidth, BoxHeight, FALSE, BGColor);
454 egRenderText(Text, Box, 7, BoxHeight / 4, (BGColor->r + BGColor->g + BGColor->b) / 3);
455 egDrawImage(Box, (egScreenWidth - BoxWidth) / 2, (egScreenHeight - BoxHeight) / 2);
456 } // if non-NULL inputs
457 } // VOID egDisplayMessage()
458
459 // Copy the current contents of the display into an EG_IMAGE....
460 // Returns pointer if successful, NULL if not.
461 EG_IMAGE * egCopyScreen(VOID) {
462 EG_IMAGE *Image = NULL;
463
464 if (!egHasGraphics)
465 return NULL;
466
467 // allocate a buffer for the whole screen
468 Image = egCreateImage(egScreenWidth, egScreenHeight, FALSE);
469 if (Image == NULL) {
470 return NULL;
471 }
472
473 // get full screen image
474 if (GraphicsOutput != NULL) {
475 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData,
476 EfiBltVideoToBltBuffer, 0, 0, 0, 0, Image->Width, Image->Height, 0);
477 } else if (UgaDraw != NULL) {
478 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaVideoToBltBuffer,
479 0, 0, 0, 0, Image->Width, Image->Height, 0);
480 }
481 return Image;
482 } // EG_IMAGE * egCopyScreen()
483
484 //
485 // Make a screenshot
486 //
487
488 VOID egScreenShot(VOID)
489 {
490 EFI_STATUS Status;
491 EG_IMAGE *Image;
492 UINT8 *FileData;
493 UINTN FileDataLength;
494 UINTN Index;
495 UINTN ssNum;
496 CHAR16 Filename[80];
497 EFI_FILE* BaseDir;
498
499 Image = egCopyScreen();
500 if (Image == NULL) {
501 Print(L"Error: Unable to take screen shot\n");
502 goto bailout_wait;
503 }
504
505 // encode as BMP
506 egEncodeBMP(Image, &FileData, &FileDataLength);
507 egFreeImage(Image);
508 if (FileData == NULL) {
509 Print(L"Error egEncodeBMP returned NULL\n");
510 goto bailout_wait;
511 }
512
513 Status = egFindESP(&BaseDir);
514 if (EFI_ERROR(Status))
515 return;
516
517 // Search for existing screen shot files; increment number to an unused value...
518 ssNum = 001;
519 do {
520 SPrint(Filename, 80, L"screenshot_%03d.bmp", ssNum++);
521 } while (FileExists(BaseDir, Filename));
522
523 // save to file on the ESP
524 Status = egSaveFile(BaseDir, Filename, FileData, FileDataLength);
525 FreePool(FileData);
526 if (CheckError(Status, L"in egSaveFile()")) {
527 goto bailout_wait;
528 }
529
530 return;
531
532 // DEBUG: switch to text mode
533 bailout_wait:
534 egSetGraphicsModeEnabled(FALSE);
535 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &Index);
536 }
537
538 /* EOF */