]> code.delx.au - refind/blob - libeg/screen.c
bc097b551d645250af8c3e05ce41fced551f6f74
[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 ModeNum = (UINT32) *ScreenWidth;
124 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
125 if (Status == EFI_SUCCESS) {
126 Status = refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum);
127 if (Status == EFI_SUCCESS) {
128 ModeSet = TRUE;
129 *ScreenWidth = Info->HorizontalResolution;
130 *ScreenHeight = Info->VerticalResolution;
131 }
132 }
133 } else { // User specified width & height; must find mode
134 // Do a loop through the modes to see if the specified one is available;
135 // and if so, switch to it....
136 do {
137 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
138 if ((Status == EFI_SUCCESS) && (Size >= sizeof(*Info)) &&
139 (Info->HorizontalResolution == *ScreenWidth) && (Info->VerticalResolution == *ScreenHeight)) {
140 Status = refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum);
141 ModeSet = (Status == EFI_SUCCESS);
142 } // if
143 } while (((ModeNum++ < 10) || (Status == EFI_SUCCESS)) && !ModeSet);
144 // while ((Status == EFI_SUCCESS) && (!ModeSet)) {
145 // Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
146 // if ((Status == EFI_SUCCESS) && (Size >= sizeof(*Info)) &&
147 // (Info->HorizontalResolution == *ScreenWidth) && (Info->VerticalResolution == *ScreenHeight)) {
148 // Status = refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum);
149 // ModeSet = (Status == EFI_SUCCESS);
150 // } // if
151 // ModeNum++;
152 // } // while()
153 } // if/else
154
155 if (ModeSet) {
156 egScreenWidth = *ScreenWidth;
157 egScreenHeight = *ScreenHeight;
158 } else {// If unsuccessful, display an error message for the user....
159 SwitchToText(FALSE);
160 Print(L"Error setting graphics mode %d x %d; using default mode!\nAvailable modes are:\n", *ScreenWidth, *ScreenHeight);
161 ModeNum = 0;
162 do {
163 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
164 if ((Status == EFI_SUCCESS) && (Info != NULL)) {
165 Print(L"Mode %d: %d x %d\n", ModeNum, Info->HorizontalResolution, Info->VerticalResolution);
166 } // else
167 } while ((ModeNum++ < 10) || (Status == EFI_SUCCESS));
168 // Status = EFI_SUCCESS;
169 // while (Status == EFI_SUCCESS) {
170 // Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
171 // if ((Status == EFI_SUCCESS) && (Size >= sizeof(*Info))) {
172 // Print(L"Mode %d: %d x %d\n", ModeNum, Info->HorizontalResolution, Info->VerticalResolution);
173 // } // else
174 // ModeNum++;
175 // } // while()
176 PauseForKey();
177 SwitchToGraphics();
178 } // if()
179 } else if (UgaDraw != NULL) { // UGA mode (EFI 1.x)
180 // Try to use current color depth & refresh rate for new mode. Maybe not the best choice
181 // in all cases, but I don't know how to probe for alternatives....
182 Status = refit_call5_wrapper(UgaDraw->GetMode, UgaDraw, &UGAWidth, &UGAHeight, &UGADepth, &UGARefreshRate);
183 Status = refit_call5_wrapper(UgaDraw->SetMode, UgaDraw, *ScreenWidth, *ScreenHeight, UGADepth, UGARefreshRate);
184 if (Status == EFI_SUCCESS) {
185 egScreenWidth = *ScreenWidth;
186 egScreenHeight = *ScreenHeight;
187 ModeSet = TRUE;
188 } else {
189 // TODO: Find a list of supported modes and display it.
190 // NOTE: Below doesn't actually appear unless we explicitly switch to text mode.
191 // This is just a placeholder until something better can be done....
192 Print(L"Error setting graphics mode %d x %d; unsupported mode!\n");
193 } // if/else
194 } // if/else if
195 return (ModeSet);
196 } // BOOLEAN egSetScreenSize()
197
198 VOID egGetScreenSize(OUT UINTN *ScreenWidth, OUT UINTN *ScreenHeight)
199 {
200 if (ScreenWidth != NULL)
201 *ScreenWidth = egScreenWidth;
202 if (ScreenHeight != NULL)
203 *ScreenHeight = egScreenHeight;
204 }
205
206 // Set a text mode
207 // Returns the mode that was actually set.
208 UINT32 egSetTextMode(UINT32 RequestedMode) {
209 UINTN i = 0, Width, Height;
210 UINT32 UsedMode = ST->ConOut->Mode->Mode;
211 EFI_STATUS Status;
212
213 if (RequestedMode != ST->ConOut->Mode->Mode) {
214 Status = refit_call2_wrapper(ST->ConOut->SetMode, ST->ConOut, RequestedMode);
215 if (Status == EFI_SUCCESS) {
216 UsedMode = RequestedMode;
217 } else {
218 SwitchToText(FALSE);
219 Print(L"Error setting text mode %d; available modes are:\n", RequestedMode);
220 do {
221 Status = refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, i, &Width, &Height);
222 if (Status == EFI_SUCCESS)
223 Print(L"Mode: %d: %d x %d\n", i, Width, Height);
224 } while ((i++ < 2) || (Status == EFI_SUCCESS));
225
226 PauseForKey();
227 SwitchToGraphics();
228 } // if/else successful change
229 } // if need to change mode
230 return UsedMode;
231 } // UINT32 egSetTextMode()
232
233 CHAR16 * egScreenDescription(VOID)
234 {
235 CHAR16 *GraphicsInfo, *TextInfo = NULL;
236
237 GraphicsInfo = AllocateZeroPool(256 * sizeof(CHAR16));
238 if (GraphicsInfo == NULL)
239 return L"memory allocation error";
240
241 if (egHasGraphics) {
242 if (GraphicsOutput != NULL) {
243 SPrint(GraphicsInfo, 255, L"Graphics Output (UEFI), %dx%d", egScreenWidth, egScreenHeight);
244 } else if (UgaDraw != NULL) {
245 GraphicsInfo = AllocateZeroPool(256 * sizeof(CHAR16));
246 SPrint(GraphicsInfo, 255, L"UGA Draw (EFI 1.10), %dx%d", egScreenWidth, egScreenHeight);
247 } else {
248 MyFreePool(GraphicsInfo);
249 MyFreePool(TextInfo);
250 return L"Internal Error";
251 }
252 if (!AllowGraphicsMode) { // graphics-capable HW, but in text mode
253 TextInfo = AllocateZeroPool(256 * sizeof(CHAR16));
254 SPrint(TextInfo, 255, L"(in %dx%d text mode)", ConWidth, ConHeight);
255 MergeStrings(&GraphicsInfo, TextInfo, L' ');
256 }
257 } else {
258 SPrint(GraphicsInfo, 255, L"Text-foo console, %dx%d", ConWidth, ConHeight);
259 }
260 MyFreePool(TextInfo);
261 return GraphicsInfo;
262 }
263
264 BOOLEAN egHasGraphicsMode(VOID)
265 {
266 return egHasGraphics;
267 }
268
269 BOOLEAN egIsGraphicsModeEnabled(VOID)
270 {
271 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
272
273 if (ConsoleControl != NULL) {
274 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
275 return (CurrentMode == EfiConsoleControlScreenGraphics) ? TRUE : FALSE;
276 }
277
278 return FALSE;
279 }
280
281 VOID egSetGraphicsModeEnabled(IN BOOLEAN Enable)
282 {
283 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
284 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode;
285
286 if (ConsoleControl != NULL) {
287 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
288
289 NewMode = Enable ? EfiConsoleControlScreenGraphics
290 : EfiConsoleControlScreenText;
291 if (CurrentMode != NewMode)
292 refit_call2_wrapper(ConsoleControl->SetMode, ConsoleControl, NewMode);
293 }
294 }
295
296 //
297 // Drawing to the screen
298 //
299
300 VOID egClearScreen(IN EG_PIXEL *Color)
301 {
302 EFI_UGA_PIXEL FillColor;
303
304 if (!egHasGraphics)
305 return;
306
307 FillColor.Red = Color->r;
308 FillColor.Green = Color->g;
309 FillColor.Blue = Color->b;
310 FillColor.Reserved = 0;
311
312 if (GraphicsOutput != NULL) {
313 // EFI_GRAPHICS_OUTPUT_BLT_PIXEL and EFI_UGA_PIXEL have the same
314 // layout, and the header from TianoCore actually defines them
315 // to be the same type.
316 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)&FillColor, EfiBltVideoFill,
317 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
318 } else if (UgaDraw != NULL) {
319 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, &FillColor, EfiUgaVideoFill,
320 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
321 }
322 }
323
324 VOID egDrawImage(IN EG_IMAGE *Image, IN UINTN ScreenPosX, IN UINTN ScreenPosY)
325 {
326 if (!egHasGraphics)
327 return;
328
329 if (Image->HasAlpha) {
330 Image->HasAlpha = FALSE;
331 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
332 }
333
334 if (GraphicsOutput != NULL) {
335 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltBufferToVideo,
336 0, 0, ScreenPosX, ScreenPosY, Image->Width, Image->Height, 0);
337 } else if (UgaDraw != NULL) {
338 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaBltBufferToVideo,
339 0, 0, ScreenPosX, ScreenPosY, Image->Width, Image->Height, 0);
340 }
341 }
342
343 VOID egDrawImageArea(IN EG_IMAGE *Image,
344 IN UINTN AreaPosX, IN UINTN AreaPosY,
345 IN UINTN AreaWidth, IN UINTN AreaHeight,
346 IN UINTN ScreenPosX, IN UINTN ScreenPosY)
347 {
348 if (!egHasGraphics)
349 return;
350
351 egRestrictImageArea(Image, AreaPosX, AreaPosY, &AreaWidth, &AreaHeight);
352 if (AreaWidth == 0)
353 return;
354
355 if (Image->HasAlpha) {
356 Image->HasAlpha = FALSE;
357 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
358 }
359
360 if (GraphicsOutput != NULL) {
361 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltBufferToVideo,
362 AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight, Image->Width * 4);
363 } else if (UgaDraw != NULL) {
364 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaBltBufferToVideo,
365 AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight, Image->Width * 4);
366 }
367 }
368
369 // Display a message in the center of the screen, surrounded by a box of the
370 // specified color. For the moment, uses graphics calls only. (It still works
371 // in text mode on GOP/UEFI systems, but not on UGA/EFI 1.x systems.)
372 VOID egDisplayMessage(IN CHAR16 *Text, EG_PIXEL *BGColor) {
373 UINTN BoxWidth, BoxHeight;
374 EG_IMAGE *Box;
375
376 if ((Text != NULL) && (BGColor != NULL)) {
377 BoxWidth = (StrLen(Text) + 2) * FONT_CELL_WIDTH;
378 if (BoxWidth > egScreenWidth)
379 BoxWidth = egScreenWidth;
380 BoxHeight = 2 * FONT_CELL_HEIGHT;
381 Box = egCreateFilledImage(BoxWidth, BoxHeight, FALSE, BGColor);
382 egRenderText(Text, Box, FONT_CELL_WIDTH, FONT_CELL_HEIGHT / 2);
383 egDrawImage(Box, (egScreenWidth - BoxWidth) / 2, (egScreenHeight - BoxHeight) / 2);
384 } // if non-NULL inputs
385 } // VOID egDisplayMessage()
386
387 //
388 // Make a screenshot
389 //
390
391 VOID egScreenShot(VOID)
392 {
393 EFI_STATUS Status;
394 EG_IMAGE *Image;
395 UINT8 *FileData;
396 UINTN FileDataLength;
397 UINTN Index;
398
399 if (!egHasGraphics)
400 return;
401
402 // allocate a buffer for the whole screen
403 Image = egCreateImage(egScreenWidth, egScreenHeight, FALSE);
404 if (Image == NULL) {
405 Print(L"Error egCreateImage returned NULL\n");
406 goto bailout_wait;
407 }
408
409 // get full screen image
410 if (GraphicsOutput != NULL) {
411 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltVideoToBltBuffer,
412 0, 0, 0, 0, Image->Width, Image->Height, 0);
413 } else if (UgaDraw != NULL) {
414 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaVideoToBltBuffer,
415 0, 0, 0, 0, Image->Width, Image->Height, 0);
416 }
417
418 // encode as BMP
419 egEncodeBMP(Image, &FileData, &FileDataLength);
420 egFreeImage(Image);
421 if (FileData == NULL) {
422 Print(L"Error egEncodeBMP returned NULL\n");
423 goto bailout_wait;
424 }
425
426 // save to file on the ESP
427 Status = egSaveFile(NULL, L"screenshot.bmp", FileData, FileDataLength);
428 FreePool(FileData);
429 if (EFI_ERROR(Status)) {
430 Print(L"Error egSaveFile: %x\n", Status);
431 goto bailout_wait;
432 }
433
434 return;
435
436 // DEBUG: switch to text mode
437 bailout_wait:
438 egSetGraphicsModeEnabled(FALSE);
439 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &Index);
440 }
441
442 /* EOF */