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