MdeModulePkg/TerminalDxe: Improve TtyTerm cursor position tracking

When we print the last character on a line, the terminal driver wraps
CursorRow/CursorColumn to the beginning of the next line.  But the
terminal itself doesn't wrap its cursor until the next character is
printed.  That throws off the driver's cursor position tracking.

So when we have printed the last character on a line, and are not in
the middle of outputing an escape sequence, synchronize the terminal
with the driver by outputing CR+LF.  This matches the expected
behavior, and the behavior of the VGA console driver.

Only change the behavior of TtyTerm, not the other terminal types.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Brian Johnson <bjohnson@sgi.com>
Cc: Feng Tian <feng.tian@intel.com>
Cc: Star Zeng <star.zeng@intel.com>
Tested-by: Ryan Harkin <ryan.harkin@linaro.org>
Reviewed-by: Feng Tian <feng.tian@intel.com>
diff --git a/MdeModulePkg/Universal/Console/TerminalDxe/TerminalConOut.c b/MdeModulePkg/Universal/Console/TerminalDxe/TerminalConOut.c
index 9fa952a..b11e83f 100644
--- a/MdeModulePkg/Universal/Console/TerminalDxe/TerminalConOut.c
+++ b/MdeModulePkg/Universal/Console/TerminalDxe/TerminalConOut.c
@@ -2,6 +2,7 @@
   Implementation for EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL protocol.

 

 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>

+Copyright (C) 2016 Silicon Graphics, Inc. All rights reserved.<BR>

 This program and the accompanying materials

 are licensed and made available under the terms and conditions of the BSD License

 which accompanies this distribution.  The full text of the license may be found at

@@ -313,6 +314,30 @@
           Mode->CursorRow++;

         }

 

+        if (TerminalDevice->TerminalType == TTYTERMTYPE &&

+            !TerminalDevice->OutputEscChar) {

+          //

+          // We've written the last character on the line.  The

+          // terminal doesn't actually wrap its cursor until we print

+          // the next character, but the driver thinks it has wrapped

+          // already.  Print CR LF to synchronize the terminal with

+          // the driver, but only if we're not in the middle of

+          // printing an escape sequence.

+          //

+          CHAR8 CrLfStr[] = {'\r', '\n'};

+

+          Length = sizeof(CrLfStr);

+

+          Status = TerminalDevice->SerialIo->Write (

+                                                TerminalDevice->SerialIo,

+                                                &Length,

+                                                CrLfStr

+                                                );

+

+          if (EFI_ERROR (Status)) {

+            goto OutputError;

+          }

+        }

       }

       break;