]> code.delx.au - refind/blob - refind/screen.c
Improvements to icon- and banner-positioning code. Fixes crash if
[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 static 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 // StatusToString(ErrorName, Status);
398 gST->ConOut->SetAttribute (gST->ConOut, ATTR_ERROR);
399 Print(L"Fatal Error: %r %s\n", Status, where);
400 gST->ConOut->SetAttribute (gST->ConOut, ATTR_BASIC);
401 haveError = TRUE;
402
403 //gBS->Exit(ImageHandle, ExitStatus, ExitDataSize, ExitData);
404
405 return TRUE;
406 }
407
408 BOOLEAN CheckError(IN EFI_STATUS Status, IN CHAR16 *where)
409 {
410 // CHAR16 ErrorName[64];
411
412 if (!EFI_ERROR(Status))
413 return FALSE;
414
415 // StatusToString(ErrorName, Status);
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;
439 INTN BannerPosX, BannerPosY;
440
441 if (ShowBanner && !(GlobalConfig.HideUIFlags & HIDEUI_FLAG_BANNER)) {
442 // load banner on first call
443 if (Banner == NULL) {
444 if (GlobalConfig.BannerFileName == NULL) {
445 Banner = egPrepareEmbeddedImage(&egemb_refind_banner, FALSE);
446 } else {
447 Banner = egLoadImage(SelfDir, GlobalConfig.BannerFileName, FALSE);
448 if ((Banner == NULL) || (Banner->Width > UGAWidth) || (Banner->Height > UGAHeight)) {
449 MyFreePool(Banner);
450 Banner = egPrepareEmbeddedImage(&egemb_refind_banner, FALSE);
451 } // if unusable image
452 }
453 if (Banner != NULL)
454 MenuBackgroundPixel = Banner->PixelData[0];
455 }
456
457 // clear and draw banner
458 egClearScreen(&MenuBackgroundPixel);
459 if (Banner != NULL) {
460 BannerPosX = (Banner->Width < UGAWidth) ? ((UGAWidth - Banner->Width) / 2) : 0;
461 BannerPosY = ComputeRow0PosX() - Banner->Height - LAYOUT_BANNER_YGAP;
462 if (BannerPosY < 0)
463 BannerPosY = 0;
464 BltImage(Banner, (UINTN) BannerPosX, (UINTN) BannerPosY);
465 }
466
467 } else {
468 // clear to standard background color
469 egClearScreen(&StdBackgroundPixel);
470 }
471
472 GraphicsScreenDirty = FALSE;
473 }
474
475 VOID BltImage(IN EG_IMAGE *Image, IN UINTN XPos, IN UINTN YPos)
476 {
477 egDrawImage(Image, XPos, YPos);
478 GraphicsScreenDirty = TRUE;
479 }
480
481 VOID BltImageAlpha(IN EG_IMAGE *Image, IN UINTN XPos, IN UINTN YPos, IN EG_PIXEL *BackgroundPixel)
482 {
483 EG_IMAGE *CompImage;
484
485 // compose on background
486 CompImage = egCreateFilledImage(Image->Width, Image->Height, FALSE, BackgroundPixel);
487 egComposeImage(CompImage, Image, 0, 0);
488
489 // blit to screen and clean up
490 egDrawImage(CompImage, XPos, YPos);
491 egFreeImage(CompImage);
492 GraphicsScreenDirty = TRUE;
493 }
494
495 // VOID BltImageComposite(IN EG_IMAGE *BaseImage, IN EG_IMAGE *TopImage, IN UINTN XPos, IN UINTN YPos)
496 // {
497 // UINTN TotalWidth, TotalHeight, CompWidth, CompHeight, OffsetX, OffsetY;
498 // EG_IMAGE *CompImage;
499 //
500 // // initialize buffer with base image
501 // CompImage = egCopyImage(BaseImage);
502 // TotalWidth = BaseImage->Width;
503 // TotalHeight = BaseImage->Height;
504 //
505 // // place the top image
506 // CompWidth = TopImage->Width;
507 // if (CompWidth > TotalWidth)
508 // CompWidth = TotalWidth;
509 // OffsetX = (TotalWidth - CompWidth) >> 1;
510 // CompHeight = TopImage->Height;
511 // if (CompHeight > TotalHeight)
512 // CompHeight = TotalHeight;
513 // OffsetY = (TotalHeight - CompHeight) >> 1;
514 // egComposeImage(CompImage, TopImage, OffsetX, OffsetY);
515 //
516 // // blit to screen and clean up
517 // egDrawImage(CompImage, XPos, YPos);
518 // egFreeImage(CompImage);
519 // GraphicsScreenDirty = TRUE;
520 // }
521
522 VOID BltImageCompositeBadge(IN EG_IMAGE *BaseImage, IN EG_IMAGE *TopImage, IN EG_IMAGE *BadgeImage, IN UINTN XPos, IN UINTN YPos)
523 {
524 UINTN TotalWidth = 0, TotalHeight = 0, CompWidth = 0, CompHeight = 0, OffsetX = 0, OffsetY = 0;
525 EG_IMAGE *CompImage = NULL;
526
527 // initialize buffer with base image
528 if (BaseImage != NULL) {
529 CompImage = egCopyImage(BaseImage);
530 TotalWidth = BaseImage->Width;
531 TotalHeight = BaseImage->Height;
532 }
533
534 // place the top image
535 if ((TopImage != NULL) && (CompImage != NULL)) {
536 CompWidth = TopImage->Width;
537 if (CompWidth > TotalWidth)
538 CompWidth = TotalWidth;
539 OffsetX = (TotalWidth - CompWidth) >> 1;
540 CompHeight = TopImage->Height;
541 if (CompHeight > TotalHeight)
542 CompHeight = TotalHeight;
543 OffsetY = (TotalHeight - CompHeight) >> 1;
544 egComposeImage(CompImage, TopImage, OffsetX, OffsetY);
545 }
546
547 // place the badge image
548 if (BadgeImage != NULL && CompImage != NULL && (BadgeImage->Width + 8) < CompWidth && (BadgeImage->Height + 8) < CompHeight) {
549 OffsetX += CompWidth - 8 - BadgeImage->Width;
550 OffsetY += CompHeight - 8 - BadgeImage->Height;
551 egComposeImage(CompImage, BadgeImage, OffsetX, OffsetY);
552 }
553
554 // blit to screen and clean up
555 egDrawImage(CompImage, XPos, YPos);
556 egFreeImage(CompImage);
557 GraphicsScreenDirty = TRUE;
558 }
559
560 // Line-editing functions borrowed from gummiboot (cursor_left(), cursor_right(), & line_edit()).
561 // gummiboot is copyright (c) 2012 by Kay Sievers <kay.sievers@vrfy.org> and Harald Hoyer
562 // <harald@redhat.com> and is licensed under the LGPL 2.1.
563
564 static void cursor_left(UINTN *cursor, UINTN *first)
565 {
566 if ((*cursor) > 0)
567 (*cursor)--;
568 else if ((*first) > 0)
569 (*first)--;
570 }
571
572 static void cursor_right(UINTN *cursor, UINTN *first, UINTN x_max, UINTN len)
573 {
574 if ((*cursor)+2 < x_max)
575 (*cursor)++;
576 else if ((*first) + (*cursor) < len)
577 (*first)++;
578 }
579
580 BOOLEAN line_edit(CHAR16 *line_in, CHAR16 **line_out, UINTN x_max) {
581 CHAR16 *line;
582 UINTN size;
583 UINTN len;
584 UINTN first;
585 UINTN y_pos = 3;
586 CHAR16 *print;
587 UINTN cursor;
588 BOOLEAN exit;
589 BOOLEAN enter;
590
591 DrawScreenHeader(L"Line Editor");
592 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, (ConWidth - 71) / 2, ConHeight - 1);
593 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut,
594 L"Use cursor keys to edit, Esc to exit, Enter to boot with edited options");
595
596 if (!line_in)
597 line_in = L"";
598 size = StrLen(line_in) + 1024;
599 line = AllocatePool(size * sizeof(CHAR16));
600 StrCpy(line, line_in);
601 len = StrLen(line);
602 print = AllocatePool(x_max * sizeof(CHAR16));
603
604 refit_call2_wrapper(ST->ConOut->EnableCursor, ST->ConOut, TRUE);
605
606 first = 0;
607 cursor = 0;
608 enter = FALSE;
609 exit = FALSE;
610 while (!exit) {
611 UINTN index;
612 EFI_STATUS err;
613 EFI_INPUT_KEY key;
614 UINTN i;
615
616 i = len - first;
617 if (i >= x_max-2)
618 i = x_max-2;
619 CopyMem(print, line + first, i * sizeof(CHAR16));
620 print[i++] = ' ';
621 print[i] = '\0';
622
623 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, y_pos);
624 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, print);
625 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, cursor, y_pos);
626
627 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
628 err = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
629 if (EFI_ERROR(err))
630 continue;
631
632 switch (key.ScanCode) {
633 case SCAN_ESC:
634 exit = TRUE;
635 break;
636 case SCAN_HOME:
637 cursor = 0;
638 first = 0;
639 continue;
640 case SCAN_END:
641 cursor = len;
642 if (cursor >= x_max) {
643 cursor = x_max-2;
644 first = len - (x_max-2);
645 }
646 continue;
647 case SCAN_UP:
648 while((first + cursor) && line[first + cursor] == ' ')
649 cursor_left(&cursor, &first);
650 while((first + cursor) && line[first + cursor] != ' ')
651 cursor_left(&cursor, &first);
652 while((first + cursor) && line[first + cursor] == ' ')
653 cursor_left(&cursor, &first);
654 if (first + cursor != len && first + cursor)
655 cursor_right(&cursor, &first, x_max, len);
656 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, cursor, y_pos);
657 continue;
658 case SCAN_DOWN:
659 while(line[first + cursor] && line[first + cursor] == ' ')
660 cursor_right(&cursor, &first, x_max, len);
661 while(line[first + cursor] && line[first + cursor] != ' ')
662 cursor_right(&cursor, &first, x_max, len);
663 while(line[first + cursor] && line[first + cursor] == ' ')
664 cursor_right(&cursor, &first, x_max, len);
665 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, cursor, y_pos);
666 continue;
667 case SCAN_RIGHT:
668 if (first + cursor == len)
669 continue;
670 cursor_right(&cursor, &first, x_max, len);
671 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, cursor, y_pos);
672 continue;
673 case SCAN_LEFT:
674 cursor_left(&cursor, &first);
675 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, cursor, y_pos);
676 continue;
677 case SCAN_DELETE:
678 if (len == 0)
679 continue;
680 if (first + cursor == len)
681 continue;
682 for (i = first + cursor; i < len; i++)
683 line[i] = line[i+1];
684 line[len-1] = ' ';
685 len--;
686 continue;
687 }
688
689 switch (key.UnicodeChar) {
690 case CHAR_LINEFEED:
691 case CHAR_CARRIAGE_RETURN:
692 *line_out = line;
693 line = NULL;
694 enter = TRUE;
695 exit = TRUE;
696 break;
697 case CHAR_BACKSPACE:
698 if (len == 0)
699 continue;
700 if (first == 0 && cursor == 0)
701 continue;
702 for (i = first + cursor-1; i < len; i++)
703 line[i] = line[i+1];
704 len--;
705 if (cursor > 0)
706 cursor--;
707 if (cursor > 0 || first == 0)
708 continue;
709 /* show full line if it fits */
710 if (len < x_max-2) {
711 cursor = first;
712 first = 0;
713 continue;
714 }
715 /* jump left to see what we delete */
716 if (first > 10) {
717 first -= 10;
718 cursor = 10;
719 } else {
720 cursor = first;
721 first = 0;
722 }
723 continue;
724 case '\t':
725 case ' ' ... '~':
726 case 0x80 ... 0xffff:
727 if (len+1 == size)
728 continue;
729 for (i = len; i > first + cursor; i--)
730 line[i] = line[i-1];
731 line[first + cursor] = key.UnicodeChar;
732 len++;
733 line[len] = '\0';
734 if (cursor+2 < x_max)
735 cursor++;
736 else if (first + cursor < len)
737 first++;
738 continue;
739 }
740 }
741
742 refit_call2_wrapper(ST->ConOut->EnableCursor, ST->ConOut, FALSE);
743 FreePool(print);
744 FreePool(line);
745 return enter;
746 } /* BOOLEAN line_edit() */