]> code.delx.au - refind/blob - refind/screen.c
Added support for "-1" value to "screensaver" token. Modified
[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 && (GlobalConfig.ScreensaverTime != -1))
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 if (GlobalConfig.ScreensaverTime != -1) {
172 BltClearScreen(TRUE);
173 } else { // start with screen blanked
174 GraphicsScreenDirty = TRUE;
175 }
176 }
177 } // VOID SetupScreen()
178
179 VOID SwitchToText(IN BOOLEAN CursorEnabled)
180 {
181 egSetGraphicsModeEnabled(FALSE);
182 refit_call2_wrapper(ST->ConOut->EnableCursor, ST->ConOut, CursorEnabled);
183 // get size of text console
184 if (refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, ST->ConOut->Mode->Mode, &ConWidth, &ConHeight) != EFI_SUCCESS) {
185 // use default values on error
186 ConWidth = 80;
187 ConHeight = 25;
188 }
189 PrepareBlankLine();
190 }
191
192 VOID SwitchToGraphics(VOID)
193 {
194 if (AllowGraphicsMode && !egIsGraphicsModeEnabled()) {
195 egSetGraphicsModeEnabled(TRUE);
196 GraphicsScreenDirty = TRUE;
197 }
198 }
199
200 //
201 // Screen control for running tools
202 //
203
204 VOID BeginTextScreen(IN CHAR16 *Title)
205 {
206 DrawScreenHeader(Title);
207 SwitchToText(FALSE);
208
209 // reset error flag
210 haveError = FALSE;
211 }
212
213 VOID FinishTextScreen(IN BOOLEAN WaitAlways)
214 {
215 if (haveError || WaitAlways) {
216 PauseForKey();
217 SwitchToText(FALSE);
218 }
219
220 // reset error flag
221 haveError = FALSE;
222 }
223
224 VOID BeginExternalScreen(IN BOOLEAN UseGraphicsMode, IN CHAR16 *Title)
225 {
226 if (!AllowGraphicsMode)
227 UseGraphicsMode = FALSE;
228
229 if (UseGraphicsMode) {
230 SwitchToGraphics();
231 BltClearScreen(FALSE);
232 } else {
233 egClearScreen(&DarkBackgroundPixel);
234 DrawScreenHeader(Title);
235 SwitchToText(TRUE);
236 }
237
238 // reset error flag
239 haveError = FALSE;
240 }
241
242 VOID FinishExternalScreen(VOID)
243 {
244 // make sure we clean up later
245 GraphicsScreenDirty = TRUE;
246
247 if (haveError) {
248 SwitchToText(FALSE);
249 PauseForKey();
250 }
251
252 // Reset the screen resolution, in case external program changed it....
253 SetupScreen();
254
255 // reset error flag
256 haveError = FALSE;
257 }
258
259 VOID TerminateScreen(VOID)
260 {
261 // clear text screen
262 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
263 refit_call1_wrapper(ST->ConOut->ClearScreen, ST->ConOut);
264
265 // enable cursor
266 refit_call2_wrapper(ST->ConOut->EnableCursor, ST->ConOut, TRUE);
267 }
268
269 static VOID DrawScreenHeader(IN CHAR16 *Title)
270 {
271 UINTN y;
272
273 // clear to black background
274 egClearScreen(&DarkBackgroundPixel); // first clear in graphics mode
275 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
276 refit_call1_wrapper(ST->ConOut->ClearScreen, ST->ConOut); // then clear in text mode
277
278 // paint header background
279 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BANNER);
280 for (y = 0; y < 3; y++) {
281 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, y);
282 Print(BlankLine);
283 }
284
285 // print header text
286 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, 1);
287 Print(L"rEFInd - %s", Title);
288
289 // reposition cursor
290 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
291 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, 4);
292 }
293
294 //
295 // Keyboard input
296 //
297
298 BOOLEAN ReadAllKeyStrokes(VOID)
299 {
300 BOOLEAN GotKeyStrokes;
301 EFI_STATUS Status;
302 EFI_INPUT_KEY key;
303
304 GotKeyStrokes = FALSE;
305 for (;;) {
306 Status = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
307 if (Status == EFI_SUCCESS) {
308 GotKeyStrokes = TRUE;
309 continue;
310 }
311 break;
312 }
313 return GotKeyStrokes;
314 }
315
316 VOID PauseForKey(VOID)
317 {
318 UINTN index;
319
320 Print(L"\n* Hit any key to continue *");
321
322 if (ReadAllKeyStrokes()) { // remove buffered key strokes
323 refit_call1_wrapper(BS->Stall, 5000000); // 5 seconds delay
324 ReadAllKeyStrokes(); // empty the buffer again
325 }
326
327 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
328 ReadAllKeyStrokes(); // empty the buffer to protect the menu
329
330 Print(L"\n");
331 }
332
333 #if REFIT_DEBUG > 0
334 VOID DebugPause(VOID)
335 {
336 // show console and wait for key
337 SwitchToText(FALSE);
338 PauseForKey();
339
340 // reset error flag
341 haveError = FALSE;
342 }
343 #endif
344
345 VOID EndlessIdleLoop(VOID)
346 {
347 UINTN index;
348
349 for (;;) {
350 ReadAllKeyStrokes();
351 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
352 }
353 }
354
355 //
356 // Error handling
357 //
358
359 #ifdef __MAKEWITH_GNUEFI
360 BOOLEAN CheckFatalError(IN EFI_STATUS Status, IN CHAR16 *where)
361 {
362 CHAR16 ErrorName[64];
363
364 if (!EFI_ERROR(Status))
365 return FALSE;
366
367 StatusToString(ErrorName, Status);
368 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_ERROR);
369 Print(L"Fatal Error: %s %s\n", ErrorName, where);
370 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
371 haveError = TRUE;
372
373 //BS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
374
375 return TRUE;
376 }
377
378 BOOLEAN CheckError(IN EFI_STATUS Status, IN CHAR16 *where)
379 {
380 CHAR16 ErrorName[64];
381
382 if (!EFI_ERROR(Status))
383 return FALSE;
384
385 StatusToString(ErrorName, Status);
386 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_ERROR);
387 Print(L"Error: %s %s\n", ErrorName, where);
388 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
389 haveError = TRUE;
390
391 return TRUE;
392 }
393 #else
394 BOOLEAN CheckFatalError(IN EFI_STATUS Status, IN CHAR16 *where)
395 {
396 // CHAR16 ErrorName[64];
397
398 if (!EFI_ERROR(Status))
399 return FALSE;
400
401 gST->ConOut->SetAttribute (gST->ConOut, ATTR_ERROR);
402 Print(L"Fatal Error: %r %s\n", Status, where);
403 gST->ConOut->SetAttribute (gST->ConOut, ATTR_BASIC);
404 haveError = TRUE;
405
406 //gBS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
407
408 return TRUE;
409 }
410
411 BOOLEAN CheckError(IN EFI_STATUS Status, IN CHAR16 *where)
412 {
413 if (!EFI_ERROR(Status))
414 return FALSE;
415
416 gST->ConOut->SetAttribute (gST->ConOut, ATTR_ERROR);
417 Print(L"Error: %r %s\n", Status, where);
418 gST->ConOut->SetAttribute (gST->ConOut, ATTR_BASIC);
419 haveError = TRUE;
420
421 return TRUE;
422 }
423 #endif
424
425 //
426 // Graphics functions
427 //
428
429 VOID SwitchToGraphicsAndClear(VOID)
430 {
431 SwitchToGraphics();
432 if (GraphicsScreenDirty)
433 BltClearScreen(TRUE);
434 }
435
436 VOID BltClearScreen(IN BOOLEAN ShowBanner)
437 {
438 static EG_IMAGE *Banner = NULL, *CroppedBanner;
439 INTN BannerPosX, BannerPosY;
440 EG_PIXEL Black = { 0x0, 0x0, 0x0, 0 };
441
442 if (ShowBanner && !(GlobalConfig.HideUIFlags & HIDEUI_FLAG_BANNER)) {
443 // load banner on first call
444 if (Banner == NULL) {
445 if (GlobalConfig.BannerFileName == NULL) {
446 Banner = egPrepareEmbeddedImage(&egemb_refind_banner, FALSE);
447 } else {
448 Banner = egLoadImage(SelfDir, GlobalConfig.BannerFileName, FALSE);
449 if (Banner && ((Banner->Width > UGAWidth) || (Banner->Height > UGAHeight))) {
450 CroppedBanner = egCropImage(Banner, 0, 0, (Banner->Width > UGAWidth) ? UGAWidth : Banner->Width,
451 (Banner->Height > UGAHeight) ? UGAHeight : Banner->Height);
452 MyFreePool(Banner);
453 Banner = CroppedBanner;
454 } // if image too big
455 if (Banner == NULL) {
456 Banner = egPrepareEmbeddedImage(&egemb_refind_banner, FALSE);
457 } // if unusable image
458 }
459 if (Banner != NULL)
460 MenuBackgroundPixel = Banner->PixelData[0];
461 }
462
463 // clear and draw banner
464 if (GlobalConfig.ScreensaverTime != -1)
465 egClearScreen(&MenuBackgroundPixel);
466 else
467 egClearScreen(&Black);
468
469 if (Banner != NULL) {
470 BannerPosX = (Banner->Width < UGAWidth) ? ((UGAWidth - Banner->Width) / 2) : 0;
471 BannerPosY = (INTN) (ComputeRow0PosY() / 2) - (INTN) Banner->Height;
472 if (BannerPosY < 0)
473 BannerPosY = 0;
474 GlobalConfig.BannerBottomEdge = BannerPosY + Banner->Height;
475 if (GlobalConfig.ScreensaverTime != -1)
476 BltImage(Banner, (UINTN) BannerPosX, (UINTN) BannerPosY);
477 }
478
479 } else {
480 // clear to menu background color
481 egClearScreen(&MenuBackgroundPixel);
482 }
483
484 GraphicsScreenDirty = FALSE;
485 egFreeImage(GlobalConfig.ScreenBackground);
486 GlobalConfig.ScreenBackground = egCopyScreen();
487 } /* VOID BltClearScreen() */
488
489 VOID BltImage(IN EG_IMAGE *Image, IN UINTN XPos, IN UINTN YPos)
490 {
491 egDrawImage(Image, XPos, YPos);
492 GraphicsScreenDirty = TRUE;
493 }
494
495 VOID BltImageAlpha(IN EG_IMAGE *Image, IN UINTN XPos, IN UINTN YPos, IN EG_PIXEL *BackgroundPixel)
496 {
497 EG_IMAGE *CompImage;
498
499 // compose on background
500 CompImage = egCreateFilledImage(Image->Width, Image->Height, FALSE, BackgroundPixel);
501 egComposeImage(CompImage, Image, 0, 0);
502
503 // blit to screen and clean up
504 egDrawImage(CompImage, XPos, YPos);
505 egFreeImage(CompImage);
506 GraphicsScreenDirty = TRUE;
507 }
508
509 // VOID BltImageComposite(IN EG_IMAGE *BaseImage, IN EG_IMAGE *TopImage, IN UINTN XPos, IN UINTN YPos)
510 // {
511 // UINTN TotalWidth, TotalHeight, CompWidth, CompHeight, OffsetX, OffsetY;
512 // EG_IMAGE *CompImage;
513 //
514 // // initialize buffer with base image
515 // CompImage = egCopyImage(BaseImage);
516 // TotalWidth = BaseImage->Width;
517 // TotalHeight = BaseImage->Height;
518 //
519 // // place the top image
520 // CompWidth = TopImage->Width;
521 // if (CompWidth > TotalWidth)
522 // CompWidth = TotalWidth;
523 // OffsetX = (TotalWidth - CompWidth) >> 1;
524 // CompHeight = TopImage->Height;
525 // if (CompHeight > TotalHeight)
526 // CompHeight = TotalHeight;
527 // OffsetY = (TotalHeight - CompHeight) >> 1;
528 // egComposeImage(CompImage, TopImage, OffsetX, OffsetY);
529 //
530 // // blit to screen and clean up
531 // egDrawImage(CompImage, XPos, YPos);
532 // egFreeImage(CompImage);
533 // GraphicsScreenDirty = TRUE;
534 // }
535
536 VOID BltImageCompositeBadge(IN EG_IMAGE *BaseImage, IN EG_IMAGE *TopImage, IN EG_IMAGE *BadgeImage, IN UINTN XPos, IN UINTN YPos)
537 {
538 UINTN TotalWidth = 0, TotalHeight = 0, CompWidth = 0, CompHeight = 0, OffsetX = 0, OffsetY = 0;
539 EG_IMAGE *CompImage = NULL;
540
541 // initialize buffer with base image
542 if (BaseImage != NULL) {
543 CompImage = egCopyImage(BaseImage);
544 TotalWidth = BaseImage->Width;
545 TotalHeight = BaseImage->Height;
546 }
547
548 // place the top image
549 if ((TopImage != NULL) && (CompImage != NULL)) {
550 CompWidth = TopImage->Width;
551 if (CompWidth > TotalWidth)
552 CompWidth = TotalWidth;
553 OffsetX = (TotalWidth - CompWidth) >> 1;
554 CompHeight = TopImage->Height;
555 if (CompHeight > TotalHeight)
556 CompHeight = TotalHeight;
557 OffsetY = (TotalHeight - CompHeight) >> 1;
558 egComposeImage(CompImage, TopImage, OffsetX, OffsetY);
559 }
560
561 // place the badge image
562 if (BadgeImage != NULL && CompImage != NULL && (BadgeImage->Width + 8) < CompWidth && (BadgeImage->Height + 8) < CompHeight) {
563 OffsetX += CompWidth - 8 - BadgeImage->Width;
564 OffsetY += CompHeight - 8 - BadgeImage->Height;
565 egComposeImage(CompImage, BadgeImage, OffsetX, OffsetY);
566 }
567
568 // blit to screen and clean up
569 if (CompImage->HasAlpha)
570 egDrawImageWithTransparency(CompImage, NULL, XPos, YPos, CompImage->Width, CompImage->Height);
571 else
572 egDrawImage(CompImage, XPos, YPos);
573 egFreeImage(CompImage);
574 GraphicsScreenDirty = TRUE;
575 }
576
577 // Line-editing functions borrowed from gummiboot (cursor_left(), cursor_right(), & line_edit()).
578 // gummiboot is copyright (c) 2012 by Kay Sievers <kay.sievers@vrfy.org> and Harald Hoyer
579 // <harald@redhat.com> and is licensed under the LGPL 2.1.
580
581 static void cursor_left(UINTN *cursor, UINTN *first)
582 {
583 if ((*cursor) > 0)
584 (*cursor)--;
585 else if ((*first) > 0)
586 (*first)--;
587 }
588
589 static void cursor_right(UINTN *cursor, UINTN *first, UINTN x_max, UINTN len)
590 {
591 if ((*cursor)+2 < x_max)
592 (*cursor)++;
593 else if ((*first) + (*cursor) < len)
594 (*first)++;
595 }
596
597 BOOLEAN line_edit(CHAR16 *line_in, CHAR16 **line_out, UINTN x_max) {
598 CHAR16 *line;
599 UINTN size;
600 UINTN len;
601 UINTN first;
602 UINTN y_pos = 3;
603 CHAR16 *print;
604 UINTN cursor;
605 BOOLEAN exit;
606 BOOLEAN enter;
607
608 DrawScreenHeader(L"Line Editor");
609 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, (ConWidth - 71) / 2, ConHeight - 1);
610 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut,
611 L"Use cursor keys to edit, Esc to exit, Enter to boot with edited options");
612
613 if (!line_in)
614 line_in = L"";
615 size = StrLen(line_in) + 1024;
616 line = AllocatePool(size * sizeof(CHAR16));
617 StrCpy(line, line_in);
618 len = StrLen(line);
619 print = AllocatePool(x_max * sizeof(CHAR16));
620
621 refit_call2_wrapper(ST->ConOut->EnableCursor, ST->ConOut, TRUE);
622
623 first = 0;
624 cursor = 0;
625 enter = FALSE;
626 exit = FALSE;
627 while (!exit) {
628 UINTN index;
629 EFI_STATUS err;
630 EFI_INPUT_KEY key;
631 UINTN i;
632
633 i = len - first;
634 if (i >= x_max-2)
635 i = x_max-2;
636 CopyMem(print, line + first, i * sizeof(CHAR16));
637 print[i++] = ' ';
638 print[i] = '\0';
639
640 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, y_pos);
641 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, print);
642 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, cursor, y_pos);
643
644 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
645 err = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
646 if (EFI_ERROR(err))
647 continue;
648
649 switch (key.ScanCode) {
650 case SCAN_ESC:
651 exit = TRUE;
652 break;
653 case SCAN_HOME:
654 cursor = 0;
655 first = 0;
656 continue;
657 case SCAN_END:
658 cursor = len;
659 if (cursor >= x_max) {
660 cursor = x_max-2;
661 first = len - (x_max-2);
662 }
663 continue;
664 case SCAN_UP:
665 while((first + cursor) && line[first + cursor] == ' ')
666 cursor_left(&cursor, &first);
667 while((first + cursor) && line[first + cursor] != ' ')
668 cursor_left(&cursor, &first);
669 while((first + cursor) && line[first + cursor] == ' ')
670 cursor_left(&cursor, &first);
671 if (first + cursor != len && first + cursor)
672 cursor_right(&cursor, &first, x_max, len);
673 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, cursor, y_pos);
674 continue;
675 case SCAN_DOWN:
676 while(line[first + cursor] && line[first + cursor] == ' ')
677 cursor_right(&cursor, &first, x_max, len);
678 while(line[first + cursor] && line[first + cursor] != ' ')
679 cursor_right(&cursor, &first, x_max, len);
680 while(line[first + cursor] && line[first + cursor] == ' ')
681 cursor_right(&cursor, &first, x_max, len);
682 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, cursor, y_pos);
683 continue;
684 case SCAN_RIGHT:
685 if (first + cursor == len)
686 continue;
687 cursor_right(&cursor, &first, x_max, len);
688 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, cursor, y_pos);
689 continue;
690 case SCAN_LEFT:
691 cursor_left(&cursor, &first);
692 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, cursor, y_pos);
693 continue;
694 case SCAN_DELETE:
695 if (len == 0)
696 continue;
697 if (first + cursor == len)
698 continue;
699 for (i = first + cursor; i < len; i++)
700 line[i] = line[i+1];
701 line[len-1] = ' ';
702 len--;
703 continue;
704 }
705
706 switch (key.UnicodeChar) {
707 case CHAR_LINEFEED:
708 case CHAR_CARRIAGE_RETURN:
709 *line_out = line;
710 line = NULL;
711 enter = TRUE;
712 exit = TRUE;
713 break;
714 case CHAR_BACKSPACE:
715 if (len == 0)
716 continue;
717 if (first == 0 && cursor == 0)
718 continue;
719 for (i = first + cursor-1; i < len; i++)
720 line[i] = line[i+1];
721 len--;
722 if (cursor > 0)
723 cursor--;
724 if (cursor > 0 || first == 0)
725 continue;
726 /* show full line if it fits */
727 if (len < x_max-2) {
728 cursor = first;
729 first = 0;
730 continue;
731 }
732 /* jump left to see what we delete */
733 if (first > 10) {
734 first -= 10;
735 cursor = 10;
736 } else {
737 cursor = first;
738 first = 0;
739 }
740 continue;
741 case '\t':
742 case ' ' ... '~':
743 case 0x80 ... 0xffff:
744 if (len+1 == size)
745 continue;
746 for (i = len; i > first + cursor; i--)
747 line[i] = line[i-1];
748 line[first + cursor] = key.UnicodeChar;
749 len++;
750 line[len] = '\0';
751 if (cursor+2 < x_max)
752 cursor++;
753 else if (first + cursor < len)
754 first++;
755 continue;
756 }
757 }
758
759 refit_call2_wrapper(ST->ConOut->EnableCursor, ST->ConOut, FALSE);
760 FreePool(print);
761 FreePool(line);
762 return enter;
763 } /* BOOLEAN line_edit() */