]> code.delx.au - refind/blob - refind/screen.c
0.6.10 release; adds screen saver code.
[refind] / refind / screen.c
1 /*
2 * refind/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 * Modifications copyright (c) 2012 Roderick W. Smith
38 *
39 * Modifications distributed under the terms of the GNU General Public
40 * License (GPL) version 3 (GPLv3), a copy of which must be distributed
41 * with this source code or binaries made from it.
42 *
43 */
44
45 #include "global.h"
46 #include "screen.h"
47 #include "config.h"
48 #include "libegint.h"
49 #include "lib.h"
50 #include "menu.h"
51 #include "../include/refit_call_wrapper.h"
52
53 #include "../include/egemb_refind_banner.h"
54
55 // Console defines and variables
56
57 UINTN ConWidth;
58 UINTN ConHeight;
59 CHAR16 *BlankLine = NULL;
60
61 static VOID DrawScreenHeader(IN CHAR16 *Title);
62
63 // UGA defines and variables
64
65 UINTN UGAWidth;
66 UINTN UGAHeight;
67 BOOLEAN AllowGraphicsMode;
68
69 EG_PIXEL StdBackgroundPixel = { 0xbf, 0xbf, 0xbf, 0 };
70 EG_PIXEL MenuBackgroundPixel = { 0xbf, 0xbf, 0xbf, 0 };
71 EG_PIXEL DarkBackgroundPixel = { 0x0, 0x0, 0x0, 0 };
72
73 static BOOLEAN GraphicsScreenDirty;
74
75 // general defines and variables
76
77 static BOOLEAN haveError = FALSE;
78
79 static VOID PrepareBlankLine(VOID) {
80 UINTN i;
81
82 MyFreePool(BlankLine);
83 // make a buffer for a whole text line
84 BlankLine = AllocatePool((ConWidth + 1) * sizeof(CHAR16));
85 for (i = 0; i < ConWidth; i++)
86 BlankLine[i] = ' ';
87 BlankLine[i] = 0;
88 }
89
90 //
91 // Screen initialization and switching
92 //
93
94 VOID InitScreen(VOID)
95 {
96 // initialize libeg
97 egInitScreen();
98
99 if (egHasGraphicsMode()) {
100 egGetScreenSize(&UGAWidth, &UGAHeight);
101 AllowGraphicsMode = TRUE;
102 } else {
103 AllowGraphicsMode = FALSE;
104 egSetTextMode(GlobalConfig.RequestedTextMode);
105 egSetGraphicsModeEnabled(FALSE); // just to be sure we are in text mode
106 }
107 GraphicsScreenDirty = TRUE;
108
109 // disable cursor
110 refit_call2_wrapper(ST->ConOut->EnableCursor, ST->ConOut, FALSE);
111
112 // get size of text console
113 if (refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, ST->ConOut->Mode->Mode, &ConWidth, &ConHeight) != EFI_SUCCESS) {
114 // use default values on error
115 ConWidth = 80;
116 ConHeight = 25;
117 }
118
119 PrepareBlankLine();
120
121 // show the banner if in text mode
122 if (GlobalConfig.TextOnly)
123 DrawScreenHeader(L"Initializing...");
124 }
125
126 // Set the screen resolution and mode (text vs. graphics).
127 VOID SetupScreen(VOID)
128 {
129 UINTN NewWidth, NewHeight;
130
131 // Convert mode number to horizontal & vertical resolution values
132 if ((GlobalConfig.RequestedScreenWidth > 0) && (GlobalConfig.RequestedScreenHeight == 0))
133 egGetResFromMode(&(GlobalConfig.RequestedScreenWidth), &(GlobalConfig.RequestedScreenHeight));
134
135 // Set the believed-to-be current resolution to the LOWER of the current
136 // believed-to-be resolution and the requested resolution. This is done to
137 // enable setting a lower-than-default resolution.
138 if ((GlobalConfig.RequestedScreenWidth > 0) && (GlobalConfig.RequestedScreenHeight > 0)) {
139 UGAWidth = (UGAWidth < GlobalConfig.RequestedScreenWidth) ? UGAWidth : GlobalConfig.RequestedScreenWidth;
140 UGAHeight = (UGAHeight < GlobalConfig.RequestedScreenHeight) ? UGAHeight : GlobalConfig.RequestedScreenHeight;
141 }
142
143 // Set text mode. If this requires increasing the size of the graphics mode, do so.
144 if (egSetTextMode(GlobalConfig.RequestedTextMode)) {
145 egGetScreenSize(&NewWidth, &NewHeight);
146 if ((NewWidth > UGAWidth) || (NewHeight > UGAHeight)) {
147 UGAWidth = NewWidth;
148 UGAHeight = NewHeight;
149 }
150 if ((UGAWidth > GlobalConfig.RequestedScreenWidth) || (UGAHeight > GlobalConfig.RequestedScreenHeight)) {
151 // Requested text mode forces us to use a bigger graphics mode
152 GlobalConfig.RequestedScreenWidth = UGAWidth;
153 GlobalConfig.RequestedScreenHeight = UGAHeight;
154 } // if
155 }
156
157 if (GlobalConfig.RequestedScreenWidth > 0) {
158 egSetScreenSize(&(GlobalConfig.RequestedScreenWidth), &(GlobalConfig.RequestedScreenHeight));
159 egGetScreenSize(&UGAWidth, &UGAHeight);
160 } // if user requested a particular screen resolution
161
162 if (GlobalConfig.TextOnly) {
163 // switch to text mode if requested
164 AllowGraphicsMode = FALSE;
165 SwitchToText(FALSE);
166
167 } else if (AllowGraphicsMode) {
168 // clear screen and show banner
169 // (now we know we'll stay in graphics mode)
170 SwitchToGraphics();
171 BltClearScreen(TRUE);
172 }
173 } // VOID SetupScreen()
174
175 VOID SwitchToText(IN BOOLEAN CursorEnabled)
176 {
177 egSetGraphicsModeEnabled(FALSE);
178 refit_call2_wrapper(ST->ConOut->EnableCursor, ST->ConOut, CursorEnabled);
179 // get size of text console
180 if (refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, ST->ConOut->Mode->Mode, &ConWidth, &ConHeight) != EFI_SUCCESS) {
181 // use default values on error
182 ConWidth = 80;
183 ConHeight = 25;
184 }
185 PrepareBlankLine();
186 }
187
188 VOID SwitchToGraphics(VOID)
189 {
190 if (AllowGraphicsMode && !egIsGraphicsModeEnabled()) {
191 egSetGraphicsModeEnabled(TRUE);
192 GraphicsScreenDirty = TRUE;
193 }
194 }
195
196 //
197 // Screen control for running tools
198 //
199
200 VOID BeginTextScreen(IN CHAR16 *Title)
201 {
202 DrawScreenHeader(Title);
203 SwitchToText(FALSE);
204
205 // reset error flag
206 haveError = FALSE;
207 }
208
209 VOID FinishTextScreen(IN BOOLEAN WaitAlways)
210 {
211 if (haveError || WaitAlways) {
212 PauseForKey();
213 SwitchToText(FALSE);
214 }
215
216 // reset error flag
217 haveError = FALSE;
218 }
219
220 VOID BeginExternalScreen(IN BOOLEAN UseGraphicsMode, IN CHAR16 *Title)
221 {
222 if (!AllowGraphicsMode)
223 UseGraphicsMode = FALSE;
224
225 if (UseGraphicsMode) {
226 SwitchToGraphics();
227 BltClearScreen(FALSE);
228 } else {
229 egClearScreen(&DarkBackgroundPixel);
230 DrawScreenHeader(Title);
231 SwitchToText(TRUE);
232 }
233
234 // reset error flag
235 haveError = FALSE;
236 }
237
238 VOID FinishExternalScreen(VOID)
239 {
240 // make sure we clean up later
241 GraphicsScreenDirty = TRUE;
242
243 if (haveError) {
244 SwitchToText(FALSE);
245 PauseForKey();
246 }
247
248 // Reset the screen resolution, in case external program changed it....
249 SetupScreen();
250
251 // reset error flag
252 haveError = FALSE;
253 }
254
255 VOID TerminateScreen(VOID)
256 {
257 // clear text screen
258 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
259 refit_call1_wrapper(ST->ConOut->ClearScreen, ST->ConOut);
260
261 // enable cursor
262 refit_call2_wrapper(ST->ConOut->EnableCursor, ST->ConOut, TRUE);
263 }
264
265 static VOID DrawScreenHeader(IN CHAR16 *Title)
266 {
267 UINTN y;
268
269 // clear to black background
270 egClearScreen(&DarkBackgroundPixel); // first clear in graphics mode
271 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
272 refit_call1_wrapper(ST->ConOut->ClearScreen, ST->ConOut); // then clear in text mode
273
274 // paint header background
275 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BANNER);
276 for (y = 0; y < 3; y++) {
277 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, y);
278 Print(BlankLine);
279 }
280
281 // print header text
282 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, 1);
283 Print(L"rEFInd - %s", Title);
284
285 // reposition cursor
286 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
287 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, 4);
288 }
289
290 //
291 // Keyboard input
292 //
293
294 BOOLEAN ReadAllKeyStrokes(VOID)
295 {
296 BOOLEAN GotKeyStrokes;
297 EFI_STATUS Status;
298 EFI_INPUT_KEY key;
299
300 GotKeyStrokes = FALSE;
301 for (;;) {
302 Status = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
303 if (Status == EFI_SUCCESS) {
304 GotKeyStrokes = TRUE;
305 continue;
306 }
307 break;
308 }
309 return GotKeyStrokes;
310 }
311
312 VOID PauseForKey(VOID)
313 {
314 UINTN index;
315
316 Print(L"\n* Hit any key to continue *");
317
318 if (ReadAllKeyStrokes()) { // remove buffered key strokes
319 refit_call1_wrapper(BS->Stall, 5000000); // 5 seconds delay
320 ReadAllKeyStrokes(); // empty the buffer again
321 }
322
323 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
324 ReadAllKeyStrokes(); // empty the buffer to protect the menu
325
326 Print(L"\n");
327 }
328
329 #if REFIT_DEBUG > 0
330 VOID DebugPause(VOID)
331 {
332 // show console and wait for key
333 SwitchToText(FALSE);
334 PauseForKey();
335
336 // reset error flag
337 haveError = FALSE;
338 }
339 #endif
340
341 VOID EndlessIdleLoop(VOID)
342 {
343 UINTN index;
344
345 for (;;) {
346 ReadAllKeyStrokes();
347 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
348 }
349 }
350
351 //
352 // Error handling
353 //
354
355 #ifdef __MAKEWITH_GNUEFI
356 BOOLEAN CheckFatalError(IN EFI_STATUS Status, IN CHAR16 *where)
357 {
358 CHAR16 ErrorName[64];
359
360 if (!EFI_ERROR(Status))
361 return FALSE;
362
363 StatusToString(ErrorName, Status);
364 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_ERROR);
365 Print(L"Fatal Error: %s %s\n", ErrorName, where);
366 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
367 haveError = TRUE;
368
369 //BS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
370
371 return TRUE;
372 }
373
374 BOOLEAN CheckError(IN EFI_STATUS Status, IN CHAR16 *where)
375 {
376 CHAR16 ErrorName[64];
377
378 if (!EFI_ERROR(Status))
379 return FALSE;
380
381 StatusToString(ErrorName, Status);
382 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_ERROR);
383 Print(L"Error: %s %s\n", ErrorName, where);
384 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
385 haveError = TRUE;
386
387 return TRUE;
388 }
389 #else
390 BOOLEAN CheckFatalError(IN EFI_STATUS Status, IN CHAR16 *where)
391 {
392 // CHAR16 ErrorName[64];
393
394 if (!EFI_ERROR(Status))
395 return FALSE;
396
397 gST->ConOut->SetAttribute (gST->ConOut, ATTR_ERROR);
398 Print(L"Fatal Error: %r %s\n", Status, where);
399 gST->ConOut->SetAttribute (gST->ConOut, ATTR_BASIC);
400 haveError = TRUE;
401
402 //gBS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
403
404 return TRUE;
405 }
406
407 BOOLEAN CheckError(IN EFI_STATUS Status, IN CHAR16 *where)
408 {
409 if (!EFI_ERROR(Status))
410 return FALSE;
411
412 gST->ConOut->SetAttribute (gST->ConOut, ATTR_ERROR);
413 Print(L"Error: %r %s\n", Status, where);
414 gST->ConOut->SetAttribute (gST->ConOut, ATTR_BASIC);
415 haveError = TRUE;
416
417 return TRUE;
418 }
419 #endif
420
421 //
422 // Graphics functions
423 //
424
425 VOID SwitchToGraphicsAndClear(VOID)
426 {
427 SwitchToGraphics();
428 if (GraphicsScreenDirty)
429 BltClearScreen(TRUE);
430 }
431
432 VOID BltClearScreen(IN BOOLEAN ShowBanner)
433 {
434 static EG_IMAGE *Banner = NULL, *CroppedBanner;
435 INTN BannerPosX, BannerPosY;
436
437 if (ShowBanner && !(GlobalConfig.HideUIFlags & HIDEUI_FLAG_BANNER)) {
438 // load banner on first call
439 if (Banner == NULL) {
440 if (GlobalConfig.BannerFileName == NULL) {
441 Banner = egPrepareEmbeddedImage(&egemb_refind_banner, FALSE);
442 } else {
443 Banner = egLoadImage(SelfDir, GlobalConfig.BannerFileName, FALSE);
444 if (Banner && ((Banner->Width > UGAWidth) || (Banner->Height > UGAHeight))) {
445 CroppedBanner = egCropImage(Banner, 0, 0, (Banner->Width > UGAWidth) ? UGAWidth : Banner->Width,
446 (Banner->Height > UGAHeight) ? UGAHeight : Banner->Height);
447 MyFreePool(Banner);
448 Banner = CroppedBanner;
449 } // if image too big
450 if (Banner == NULL) {
451 Banner = egPrepareEmbeddedImage(&egemb_refind_banner, FALSE);
452 } // if unusable image
453 }
454 if (Banner != NULL)
455 MenuBackgroundPixel = Banner->PixelData[0];
456 }
457
458 // clear and draw banner
459 egClearScreen(&MenuBackgroundPixel);
460 if (Banner != NULL) {
461 BannerPosX = (Banner->Width < UGAWidth) ? ((UGAWidth - Banner->Width) / 2) : 0;
462 BannerPosY = (INTN) (ComputeRow0PosY() / 2) - (INTN) Banner->Height;
463 if (BannerPosY < 0)
464 BannerPosY = 0;
465 GlobalConfig.BannerBottomEdge = BannerPosY + Banner->Height;
466 BltImage(Banner, (UINTN) BannerPosX, (UINTN) BannerPosY);
467 }
468
469 } else {
470 // clear to standard background color
471 egClearScreen(&StdBackgroundPixel);
472 }
473
474 GraphicsScreenDirty = FALSE;
475 egFreeImage(GlobalConfig.ScreenBackground);
476 GlobalConfig.ScreenBackground = egCopyScreen();
477 } /* VOID BltClearScreen() */
478
479 VOID BltImage(IN EG_IMAGE *Image, IN UINTN XPos, IN UINTN YPos)
480 {
481 egDrawImage(Image, XPos, YPos);
482 GraphicsScreenDirty = TRUE;
483 }
484
485 VOID BltImageAlpha(IN EG_IMAGE *Image, IN UINTN XPos, IN UINTN YPos, IN EG_PIXEL *BackgroundPixel)
486 {
487 EG_IMAGE *CompImage;
488
489 // compose on background
490 CompImage = egCreateFilledImage(Image->Width, Image->Height, FALSE, BackgroundPixel);
491 egComposeImage(CompImage, Image, 0, 0);
492
493 // blit to screen and clean up
494 egDrawImage(CompImage, XPos, YPos);
495 egFreeImage(CompImage);
496 GraphicsScreenDirty = TRUE;
497 }
498
499 // VOID BltImageComposite(IN EG_IMAGE *BaseImage, IN EG_IMAGE *TopImage, IN UINTN XPos, IN UINTN YPos)
500 // {
501 // UINTN TotalWidth, TotalHeight, CompWidth, CompHeight, OffsetX, OffsetY;
502 // EG_IMAGE *CompImage;
503 //
504 // // initialize buffer with base image
505 // CompImage = egCopyImage(BaseImage);
506 // TotalWidth = BaseImage->Width;
507 // TotalHeight = BaseImage->Height;
508 //
509 // // place the top image
510 // CompWidth = TopImage->Width;
511 // if (CompWidth > TotalWidth)
512 // CompWidth = TotalWidth;
513 // OffsetX = (TotalWidth - CompWidth) >> 1;
514 // CompHeight = TopImage->Height;
515 // if (CompHeight > TotalHeight)
516 // CompHeight = TotalHeight;
517 // OffsetY = (TotalHeight - CompHeight) >> 1;
518 // egComposeImage(CompImage, TopImage, OffsetX, OffsetY);
519 //
520 // // blit to screen and clean up
521 // egDrawImage(CompImage, XPos, YPos);
522 // egFreeImage(CompImage);
523 // GraphicsScreenDirty = TRUE;
524 // }
525
526 VOID BltImageCompositeBadge(IN EG_IMAGE *BaseImage, IN EG_IMAGE *TopImage, IN EG_IMAGE *BadgeImage, IN UINTN XPos, IN UINTN YPos)
527 {
528 UINTN TotalWidth = 0, TotalHeight = 0, CompWidth = 0, CompHeight = 0, OffsetX = 0, OffsetY = 0;
529 EG_IMAGE *CompImage = NULL;
530
531 // initialize buffer with base image
532 if (BaseImage != NULL) {
533 CompImage = egCopyImage(BaseImage);
534 TotalWidth = BaseImage->Width;
535 TotalHeight = BaseImage->Height;
536 }
537
538 // place the top image
539 if ((TopImage != NULL) && (CompImage != NULL)) {
540 CompWidth = TopImage->Width;
541 if (CompWidth > TotalWidth)
542 CompWidth = TotalWidth;
543 OffsetX = (TotalWidth - CompWidth) >> 1;
544 CompHeight = TopImage->Height;
545 if (CompHeight > TotalHeight)
546 CompHeight = TotalHeight;
547 OffsetY = (TotalHeight - CompHeight) >> 1;
548 egComposeImage(CompImage, TopImage, OffsetX, OffsetY);
549 }
550
551 // place the badge image
552 if (BadgeImage != NULL && CompImage != NULL && (BadgeImage->Width + 8) < CompWidth && (BadgeImage->Height + 8) < CompHeight) {
553 OffsetX += CompWidth - 8 - BadgeImage->Width;
554 OffsetY += CompHeight - 8 - BadgeImage->Height;
555 egComposeImage(CompImage, BadgeImage, OffsetX, OffsetY);
556 }
557
558 // blit to screen and clean up
559 if (CompImage->HasAlpha)
560 egDrawImageWithTransparency(CompImage, NULL, XPos, YPos, CompImage->Width, CompImage->Height);
561 else
562 egDrawImage(CompImage, XPos, YPos);
563 egFreeImage(CompImage);
564 GraphicsScreenDirty = TRUE;
565 }
566
567 // Line-editing functions borrowed from gummiboot (cursor_left(), cursor_right(), & line_edit()).
568 // gummiboot is copyright (c) 2012 by Kay Sievers <kay.sievers@vrfy.org> and Harald Hoyer
569 // <harald@redhat.com> and is licensed under the LGPL 2.1.
570
571 static void cursor_left(UINTN *cursor, UINTN *first)
572 {
573 if ((*cursor) > 0)
574 (*cursor)--;
575 else if ((*first) > 0)
576 (*first)--;
577 }
578
579 static void cursor_right(UINTN *cursor, UINTN *first, UINTN x_max, UINTN len)
580 {
581 if ((*cursor)+2 < x_max)
582 (*cursor)++;
583 else if ((*first) + (*cursor) < len)
584 (*first)++;
585 }
586
587 BOOLEAN line_edit(CHAR16 *line_in, CHAR16 **line_out, UINTN x_max) {
588 CHAR16 *line;
589 UINTN size;
590 UINTN len;
591 UINTN first;
592 UINTN y_pos = 3;
593 CHAR16 *print;
594 UINTN cursor;
595 BOOLEAN exit;
596 BOOLEAN enter;
597
598 DrawScreenHeader(L"Line Editor");
599 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, (ConWidth - 71) / 2, ConHeight - 1);
600 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut,
601 L"Use cursor keys to edit, Esc to exit, Enter to boot with edited options");
602
603 if (!line_in)
604 line_in = L"";
605 size = StrLen(line_in) + 1024;
606 line = AllocatePool(size * sizeof(CHAR16));
607 StrCpy(line, line_in);
608 len = StrLen(line);
609 print = AllocatePool(x_max * sizeof(CHAR16));
610
611 refit_call2_wrapper(ST->ConOut->EnableCursor, ST->ConOut, TRUE);
612
613 first = 0;
614 cursor = 0;
615 enter = FALSE;
616 exit = FALSE;
617 while (!exit) {
618 UINTN index;
619 EFI_STATUS err;
620 EFI_INPUT_KEY key;
621 UINTN i;
622
623 i = len - first;
624 if (i >= x_max-2)
625 i = x_max-2;
626 CopyMem(print, line + first, i * sizeof(CHAR16));
627 print[i++] = ' ';
628 print[i] = '\0';
629
630 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, y_pos);
631 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, print);
632 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, cursor, y_pos);
633
634 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
635 err = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
636 if (EFI_ERROR(err))
637 continue;
638
639 switch (key.ScanCode) {
640 case SCAN_ESC:
641 exit = TRUE;
642 break;
643 case SCAN_HOME:
644 cursor = 0;
645 first = 0;
646 continue;
647 case SCAN_END:
648 cursor = len;
649 if (cursor >= x_max) {
650 cursor = x_max-2;
651 first = len - (x_max-2);
652 }
653 continue;
654 case SCAN_UP:
655 while((first + cursor) && line[first + cursor] == ' ')
656 cursor_left(&cursor, &first);
657 while((first + cursor) && line[first + cursor] != ' ')
658 cursor_left(&cursor, &first);
659 while((first + cursor) && line[first + cursor] == ' ')
660 cursor_left(&cursor, &first);
661 if (first + cursor != len && first + cursor)
662 cursor_right(&cursor, &first, x_max, len);
663 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, cursor, y_pos);
664 continue;
665 case SCAN_DOWN:
666 while(line[first + cursor] && line[first + cursor] == ' ')
667 cursor_right(&cursor, &first, x_max, len);
668 while(line[first + cursor] && line[first + cursor] != ' ')
669 cursor_right(&cursor, &first, x_max, len);
670 while(line[first + cursor] && line[first + cursor] == ' ')
671 cursor_right(&cursor, &first, x_max, len);
672 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, cursor, y_pos);
673 continue;
674 case SCAN_RIGHT:
675 if (first + cursor == len)
676 continue;
677 cursor_right(&cursor, &first, x_max, len);
678 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, cursor, y_pos);
679 continue;
680 case SCAN_LEFT:
681 cursor_left(&cursor, &first);
682 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, cursor, y_pos);
683 continue;
684 case SCAN_DELETE:
685 if (len == 0)
686 continue;
687 if (first + cursor == len)
688 continue;
689 for (i = first + cursor; i < len; i++)
690 line[i] = line[i+1];
691 line[len-1] = ' ';
692 len--;
693 continue;
694 }
695
696 switch (key.UnicodeChar) {
697 case CHAR_LINEFEED:
698 case CHAR_CARRIAGE_RETURN:
699 *line_out = line;
700 line = NULL;
701 enter = TRUE;
702 exit = TRUE;
703 break;
704 case CHAR_BACKSPACE:
705 if (len == 0)
706 continue;
707 if (first == 0 && cursor == 0)
708 continue;
709 for (i = first + cursor-1; i < len; i++)
710 line[i] = line[i+1];
711 len--;
712 if (cursor > 0)
713 cursor--;
714 if (cursor > 0 || first == 0)
715 continue;
716 /* show full line if it fits */
717 if (len < x_max-2) {
718 cursor = first;
719 first = 0;
720 continue;
721 }
722 /* jump left to see what we delete */
723 if (first > 10) {
724 first -= 10;
725 cursor = 10;
726 } else {
727 cursor = first;
728 first = 0;
729 }
730 continue;
731 case '\t':
732 case ' ' ... '~':
733 case 0x80 ... 0xffff:
734 if (len+1 == size)
735 continue;
736 for (i = len; i > first + cursor; i--)
737 line[i] = line[i-1];
738 line[first + cursor] = key.UnicodeChar;
739 len++;
740 line[len] = '\0';
741 if (cursor+2 < x_max)
742 cursor++;
743 else if (first + cursor < len)
744 first++;
745 continue;
746 }
747 }
748
749 refit_call2_wrapper(ST->ConOut->EnableCursor, ST->ConOut, FALSE);
750 FreePool(print);
751 FreePool(line);
752 return enter;
753 } /* BOOLEAN line_edit() */