]> code.delx.au - refind/blob - libeg/screen.c
fd441febe9e5821d439a9f473786b31c167b9694
[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 "refit_call_wrapper.h"
40
41 #include <efiUgaDraw.h>
42 #include <efiConsoleControl.h>
43
44 // Console defines and variables
45
46 static EFI_GUID ConsoleControlProtocolGuid = EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
47 static EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl = NULL;
48
49 static EFI_GUID UgaDrawProtocolGuid = EFI_UGA_DRAW_PROTOCOL_GUID;
50 static EFI_UGA_DRAW_PROTOCOL *UgaDraw = NULL;
51
52 static EFI_GUID GraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
53 static EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL;
54
55 static BOOLEAN egHasGraphics = FALSE;
56 static UINTN egScreenWidth = 800;
57 static UINTN egScreenHeight = 600;
58
59 //
60 // Screen handling
61 //
62
63 VOID egInitScreen(VOID)
64 {
65 EFI_STATUS Status = EFI_SUCCESS;
66 UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate;
67
68 // get protocols
69 Status = LibLocateProtocol(&ConsoleControlProtocolGuid, (VOID **) &ConsoleControl);
70 if (EFI_ERROR(Status))
71 ConsoleControl = NULL;
72
73 Status = LibLocateProtocol(&UgaDrawProtocolGuid, (VOID **) &UgaDraw);
74 if (EFI_ERROR(Status))
75 UgaDraw = NULL;
76
77 Status = LibLocateProtocol(&GraphicsOutputProtocolGuid, (VOID **) &GraphicsOutput);
78 if (EFI_ERROR(Status))
79 GraphicsOutput = NULL;
80
81 // get screen size
82 egHasGraphics = FALSE;
83 if (GraphicsOutput != NULL) {
84 egScreenWidth = GraphicsOutput->Mode->Info->HorizontalResolution;
85 egScreenHeight = GraphicsOutput->Mode->Info->VerticalResolution;
86 egHasGraphics = TRUE;
87 } else if (UgaDraw != NULL) {
88 Status = refit_call5_wrapper(UgaDraw->GetMode, UgaDraw, &UGAWidth, &UGAHeight, &UGADepth, &UGARefreshRate);
89 if (EFI_ERROR(Status)) {
90 UgaDraw = NULL; // graphics not available
91 } else {
92 egScreenWidth = UGAWidth;
93 egScreenHeight = UGAHeight;
94 egHasGraphics = TRUE;
95 }
96 }
97 }
98
99 // Sets the screen resolution to the specified value, if possible.
100 // If the specified value is not valid, displays a warning with the valid
101 // modes on UEFI systems, or silently fails on EFI 1.x systems. Note that
102 // this function attempts to set ANY screen resolution, even 0x0 or
103 // ridiculously large values.
104 // Returns TRUE if successful, FALSE if not.
105 BOOLEAN egSetScreenSize(IN UINTN ScreenWidth, IN UINTN ScreenHeight) {
106 EFI_STATUS Status = EFI_SUCCESS;
107 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;
108 UINT32 ModeNum = 0;
109 UINTN Size;
110 BOOLEAN ModeSet = FALSE;
111 UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate;
112
113 if (GraphicsOutput != NULL) { // GOP mode (UEFI)
114 // Do a loop through the modes to see if the specified one is available;
115 // and if so, switch to it....
116 while ((Status == EFI_SUCCESS) && (!ModeSet)) {
117 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
118 if ((Status == EFI_SUCCESS) && (Size >= sizeof(*Info)) &&
119 (Info->HorizontalResolution == ScreenWidth) && (Info->VerticalResolution == ScreenHeight)) {
120 Status = refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum);
121 ModeSet = (Status == EFI_SUCCESS);
122 } // if
123 ModeNum++;
124 } // while()
125
126 if (ModeSet) {
127 egScreenWidth = ScreenWidth;
128 egScreenHeight = ScreenHeight;
129 } else {// If unsuccessful, display an error message for the user....
130 Print(L"Error setting mode %d x %d; using default mode!\nAvailable modes are:\n", ScreenWidth, ScreenHeight);
131 ModeNum = 0;
132 Status = EFI_SUCCESS;
133 while (Status == EFI_SUCCESS) {
134 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
135 if ((Status == EFI_SUCCESS) && (Size >= sizeof(*Info))) {
136 Print(L"Mode %d: %d x %d\n", ModeNum, Info->HorizontalResolution, Info->VerticalResolution);
137 } // else
138 ModeNum++;
139 } // while()
140 PauseForKey();
141 } // if()
142 } else if (UgaDraw != NULL) { // UGA mode (EFI 1.x)
143 // Try to use current color depth & refresh rate for new mode. Maybe not the best choice
144 // in all cases, but I don't know how to probe for alternatives....
145 Status = refit_call5_wrapper(UgaDraw->GetMode, UgaDraw, &UGAWidth, &UGAHeight, &UGADepth, &UGARefreshRate);
146 Status = refit_call5_wrapper(UgaDraw->SetMode, UgaDraw, ScreenWidth, ScreenHeight, UGADepth, UGARefreshRate);
147 if (Status == EFI_SUCCESS) {
148 egScreenWidth = ScreenWidth;
149 egScreenHeight = ScreenHeight;
150 ModeSet = TRUE;
151 } else {
152 // TODO: Find a list of supported modes and display it.
153 // NOTE: Below doesn't actually appear unless we explicitly switch to text mode.
154 // This is just a placeholder until something better can be done....
155 Print(L"Error setting mode %d x %d; unsupported mode!\n");
156 } // if/else
157 } // if/else if
158 return (ModeSet);
159 } // BOOLEAN egSetScreenSize()
160
161 VOID egGetScreenSize(OUT UINTN *ScreenWidth, OUT UINTN *ScreenHeight)
162 {
163 if (ScreenWidth != NULL)
164 *ScreenWidth = egScreenWidth;
165 if (ScreenHeight != NULL)
166 *ScreenHeight = egScreenHeight;
167 }
168
169 CHAR16 * egScreenDescription(VOID)
170 {
171 if (egHasGraphics) {
172 if (GraphicsOutput != NULL) {
173 return PoolPrint(L"Graphics Output (UEFI), %dx%d",
174 egScreenWidth, egScreenHeight);
175 } else if (UgaDraw != NULL) {
176 return PoolPrint(L"UGA Draw (EFI 1.10), %dx%d",
177 egScreenWidth, egScreenHeight);
178 } else {
179 return L"Internal Error";
180 }
181 } else {
182 return L"Text Console";
183 }
184 }
185
186 BOOLEAN egHasGraphicsMode(VOID)
187 {
188 return egHasGraphics;
189 }
190
191 BOOLEAN egIsGraphicsModeEnabled(VOID)
192 {
193 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
194
195 if (ConsoleControl != NULL) {
196 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
197 return (CurrentMode == EfiConsoleControlScreenGraphics) ? TRUE : FALSE;
198 }
199
200 return FALSE;
201 }
202
203 VOID egSetGraphicsModeEnabled(IN BOOLEAN Enable)
204 {
205 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
206 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode;
207
208 if (ConsoleControl != NULL) {
209 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
210
211 NewMode = Enable ? EfiConsoleControlScreenGraphics
212 : EfiConsoleControlScreenText;
213 if (CurrentMode != NewMode)
214 refit_call2_wrapper(ConsoleControl->SetMode, ConsoleControl, NewMode);
215 }
216 }
217
218 //
219 // Drawing to the screen
220 //
221
222 VOID egClearScreen(IN EG_PIXEL *Color)
223 {
224 EFI_UGA_PIXEL FillColor;
225
226 if (!egHasGraphics)
227 return;
228
229 FillColor.Red = Color->r;
230 FillColor.Green = Color->g;
231 FillColor.Blue = Color->b;
232 FillColor.Reserved = 0;
233
234 if (GraphicsOutput != NULL) {
235 // EFI_GRAPHICS_OUTPUT_BLT_PIXEL and EFI_UGA_PIXEL have the same
236 // layout, and the header from TianoCore actually defines them
237 // to be the same type.
238 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)&FillColor, EfiBltVideoFill,
239 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
240 } else if (UgaDraw != NULL) {
241 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, &FillColor, EfiUgaVideoFill,
242 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
243 }
244 }
245
246 VOID egDrawImage(IN EG_IMAGE *Image, IN UINTN ScreenPosX, IN UINTN ScreenPosY)
247 {
248 if (!egHasGraphics)
249 return;
250
251 if (Image->HasAlpha) {
252 Image->HasAlpha = FALSE;
253 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
254 }
255
256 if (GraphicsOutput != NULL) {
257 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltBufferToVideo,
258 0, 0, ScreenPosX, ScreenPosY, Image->Width, Image->Height, 0);
259 } else if (UgaDraw != NULL) {
260 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaBltBufferToVideo,
261 0, 0, ScreenPosX, ScreenPosY, Image->Width, Image->Height, 0);
262 }
263 }
264
265 VOID egDrawImageArea(IN EG_IMAGE *Image,
266 IN UINTN AreaPosX, IN UINTN AreaPosY,
267 IN UINTN AreaWidth, IN UINTN AreaHeight,
268 IN UINTN ScreenPosX, IN UINTN ScreenPosY)
269 {
270 if (!egHasGraphics)
271 return;
272
273 egRestrictImageArea(Image, AreaPosX, AreaPosY, &AreaWidth, &AreaHeight);
274 if (AreaWidth == 0)
275 return;
276
277 if (Image->HasAlpha) {
278 Image->HasAlpha = FALSE;
279 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
280 }
281
282 if (GraphicsOutput != NULL) {
283 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltBufferToVideo,
284 AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight, Image->Width * 4);
285 } else if (UgaDraw != NULL) {
286 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaBltBufferToVideo,
287 AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight, Image->Width * 4);
288 }
289 }
290
291 // Display a message in the center of the screen, surrounded by a box of the
292 // specified color. For the moment, uses graphics calls only. (It still works
293 // in text mode on GOP/UEFI systems, but not on UGA/EFI 1.x systems.)
294 VOID egDisplayMessage(IN CHAR16 *Text, EG_PIXEL *BGColor) {
295 UINTN BoxWidth, BoxHeight;
296 EG_IMAGE *Box;
297
298 if ((Text != NULL) && (BGColor != NULL)) {
299 BoxWidth = (StrLen(Text) + 2) * FONT_CELL_WIDTH;
300 if (BoxWidth > egScreenWidth)
301 BoxWidth = egScreenWidth;
302 BoxHeight = 2 * FONT_CELL_HEIGHT;
303 Box = egCreateFilledImage(BoxWidth, BoxHeight, FALSE, BGColor);
304 egRenderText(Text, Box, FONT_CELL_WIDTH, FONT_CELL_HEIGHT / 2);
305 egDrawImage(Box, (egScreenWidth - BoxWidth) / 2, (egScreenHeight - BoxHeight) / 2);
306 } // if non-NULL inputs
307 } // VOID egDisplayMessage()
308
309 //
310 // Make a screenshot
311 //
312
313 VOID egScreenShot(VOID)
314 {
315 EFI_STATUS Status;
316 EG_IMAGE *Image;
317 UINT8 *FileData;
318 UINTN FileDataLength;
319 UINTN Index;
320
321 if (!egHasGraphics)
322 return;
323
324 // allocate a buffer for the whole screen
325 Image = egCreateImage(egScreenWidth, egScreenHeight, FALSE);
326 if (Image == NULL) {
327 Print(L"Error egCreateImage returned NULL\n");
328 goto bailout_wait;
329 }
330
331 // get full screen image
332 if (GraphicsOutput != NULL) {
333 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltVideoToBltBuffer,
334 0, 0, 0, 0, Image->Width, Image->Height, 0);
335 } else if (UgaDraw != NULL) {
336 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaVideoToBltBuffer,
337 0, 0, 0, 0, Image->Width, Image->Height, 0);
338 }
339
340 // encode as BMP
341 egEncodeBMP(Image, &FileData, &FileDataLength);
342 egFreeImage(Image);
343 if (FileData == NULL) {
344 Print(L"Error egEncodeBMP returned NULL\n");
345 goto bailout_wait;
346 }
347
348 // save to file on the ESP
349 Status = egSaveFile(NULL, L"screenshot.bmp", FileData, FileDataLength);
350 FreePool(FileData);
351 if (EFI_ERROR(Status)) {
352 Print(L"Error egSaveFile: %x\n", Status);
353 goto bailout_wait;
354 }
355
356 return;
357
358 // DEBUG: switch to text mode
359 bailout_wait:
360 egSetGraphicsModeEnabled(FALSE);
361 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &Index);
362 }
363
364 /* EOF */