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