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