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