From 57fb8f2fe3c4edeabc1c77b18896d5724c63b773 Mon Sep 17 00:00:00 2001 From: Rhys Weatherley Date: Fri, 11 Mar 2016 06:16:41 +1000 Subject: [PATCH] Fixes to telnet window size handling --- libraries/Terminal/Terminal.cpp | 31 ++++++++++++++++--------------- libraries/Terminal/Terminal.h | 4 ++-- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/libraries/Terminal/Terminal.cpp b/libraries/Terminal/Terminal.cpp index d7095530..cfc51e31 100644 --- a/libraries/Terminal/Terminal.cpp +++ b/libraries/Terminal/Terminal.cpp @@ -679,7 +679,7 @@ int Terminal::readKey() break; } if (utf8len < sizeof(sb)) - sb[utf8len++] = 0xFF; + sb[utf8len++] = ch; break; case STATE_SB_IAC: @@ -699,17 +699,11 @@ int Terminal::readKey() width = ncols; if (!height) height = nrows; - - // Filter out obviously bogus values. - if (width >= 1 && height >= 1 && width <= 10000 && height <= 10000) { - if (width != ncols || height != nrows) { - // The window size has changed; notify the caller. - ncols = width; - nrows = height; - ucode = -1; - state = STATE_INIT; - return KEY_WINSIZE; - } + if (setWindowSize(width, height)) { + // The window size has changed; notify the caller. + ucode = -1; + state = STATE_INIT; + return KEY_WINSIZE; } } } @@ -781,6 +775,8 @@ size_t Terminal::writeUnicode(long code) * \param columns The number of columns between 1 and 10000. * \param rows The number of rows between 1 and 10000. * + * \return Returns true if the window size has changed. + * * This function should be used if the application has some information * about the actual window size. For serial ports, this usually isn't * available but telnet and ssh sessions can get the window size from @@ -794,7 +790,7 @@ size_t Terminal::writeUnicode(long code) * * \sa columns(), rows(), readKey() */ -void Terminal::setWindowSize(int columns, int rows) +bool Terminal::setWindowSize(int columns, int rows) { // Sanity-check the range first. if (columns < 1) @@ -805,8 +801,13 @@ void Terminal::setWindowSize(int columns, int rows) rows = 1; else if (rows > 10000) rows = 10000; - ncols = columns; - nrows = rows; + if (ncols != columns || nrows != rows) { + ncols = columns; + nrows = rows; + return true; + } else { + return false; + } } /** diff --git a/libraries/Terminal/Terminal.h b/libraries/Terminal/Terminal.h index d9e62448..2454f72f 100644 --- a/libraries/Terminal/Terminal.h +++ b/libraries/Terminal/Terminal.h @@ -71,7 +71,7 @@ public: int columns() const { return ncols; } int rows() const { return nrows; } - void setWindowSize(int columns, int rows); + bool setWindowSize(int columns, int rows); void clear(); void clearToEOL(); @@ -135,7 +135,7 @@ private: uint8_t state; uint8_t utf8len; uint8_t mod; - uint8_t sb[16]; + uint8_t sb[8]; uint8_t flags; int matchEscape(int ch);