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