2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 package jexer
.tterminal
;
33 import java
.io
.InputStream
;
34 import java
.io
.InputStreamReader
;
35 import java
.io
.IOException
;
36 import java
.io
.OutputStream
;
37 import java
.io
.OutputStreamWriter
;
38 import java
.io
.Reader
;
39 import java
.io
.UnsupportedEncodingException
;
40 import java
.io
.Writer
;
41 import java
.util
.ArrayList
;
42 import java
.util
.Collections
;
43 import java
.util
.LinkedList
;
44 import java
.util
.List
;
46 import jexer
.TKeypress
;
47 import jexer
.event
.TMouseEvent
;
48 import jexer
.bits
.Color
;
49 import jexer
.bits
.Cell
;
50 import jexer
.bits
.CellAttributes
;
51 import static jexer
.TKeypress
.*;
54 * This implements a complex ANSI ECMA-48/ISO 6429/ANSI X3.64 type consoles,
55 * including a scrollback buffer.
58 * It currently implements VT100, VT102, VT220, and XTERM with the following
62 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
63 * failure with XTERM. This is due to vttest failing to decode the UTF-8
67 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
68 * from VT100 are not supported.
71 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
72 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
73 * because DECSCL is not supported, it will fail the last part of the
74 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
77 * - Numeric/application keys from the number pad are not supported because
78 * they are not exposed from the TKeypress API.
81 * - VT52 HOLD SCREEN mode is not supported.
84 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
85 * numerators) are not rendered correctly.
88 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
90 public class ECMA48
implements Runnable
{
93 * The emulator can emulate several kinds of terminals.
95 public enum DeviceType
{
97 * DEC VT100 but also including the three VT102 functions.
118 * Return the proper primary Device Attributes string.
120 * @return string to send to remote side that is appropriate for the
123 private String
deviceTypeResponse() {
126 // "I am a VT100 with advanced video option" (often VT102)
134 // "I am a VT220" - 7 bit version
136 return "\033[?62;1;6c";
138 // "I am a VT220" - 8 bit version
139 return "\u009b?62;1;6c";
141 // "I am a VT100 with advanced video option" (often VT102)
144 throw new IllegalArgumentException("Invalid device type: " + type
);
149 * Return the proper TERM environment variable for this device type.
151 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
152 * @return "vt100", "xterm", etc.
154 public static String
deviceTypeTerm(final DeviceType deviceType
) {
155 switch (deviceType
) {
169 throw new IllegalArgumentException("Invalid device type: "
175 * Return the proper LANG for this device type. Only XTERM devices know
176 * about UTF-8, the others are defined by their standard to be either
177 * 7-bit or 8-bit characters only.
179 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
180 * @param baseLang a base language without UTF-8 flag such as "C" or
182 * @return "en_US", "en_US.UTF-8", etc.
184 public static String
deviceTypeLang(final DeviceType deviceType
,
185 final String baseLang
) {
187 switch (deviceType
) {
195 return baseLang
+ ".UTF-8";
198 throw new IllegalArgumentException("Invalid device type: "
204 * Write a string directly to the remote side.
206 * @param str string to send
208 private void writeRemote(final String str
) {
209 if (stopReaderThread
) {
210 // Reader hit EOF, bail out now.
215 // System.err.printf("writeRemote() '%s'\n", str);
221 if (outputStream
== null) {
225 for (int i
= 0; i
< str
.length(); i
++) {
226 outputStream
.write(str
.charAt(i
));
228 outputStream
.flush();
229 } catch (IOException e
) {
235 if (output
== null) {
241 } catch (IOException e
) {
247 throw new IllegalArgumentException("Invalid device type: " + type
);
252 * Close the input and output streams and stop the reader thread. Note
253 * that it is safe to call this multiple times.
255 public final void close() {
257 // Synchronize so we don't stomp on the reader thread.
258 synchronized (this) {
260 // Close the input stream
265 if (inputStream
!= null) {
268 } catch (IOException e
) {
278 } catch (IOException e
) {
287 // Tell the reader thread to stop looking at input.
288 if (stopReaderThread
== false) {
289 stopReaderThread
= true;
292 } catch (InterruptedException e
) {
297 // Close the output stream.
302 if (outputStream
!= null) {
304 outputStream
.close();
305 } catch (IOException e
) {
312 if (output
!= null) {
315 } catch (IOException e
) {
322 throw new IllegalArgumentException("Invalid device type: "
325 } // synchronized (this)
329 * When true, the reader thread is expected to exit.
331 private volatile boolean stopReaderThread
= false;
336 private Thread readerThread
= null;
339 * See if the reader thread is still running.
341 * @return if true, we are still connected to / reading from the remote
344 public final boolean isReading() {
345 return (!stopReaderThread
);
349 * The type of emulator to be.
351 private DeviceType type
= DeviceType
.VT102
;
354 * Obtain a new blank display line for an external user
355 * (e.g. TTerminalWindow).
357 * @return new blank line
359 public final DisplayLine
getBlankDisplayLine() {
360 return new DisplayLine(currentState
.attr
);
364 * The scrollback buffer characters + attributes.
366 private volatile List
<DisplayLine
> scrollback
;
369 * Get the scrollback buffer.
371 * @return the scrollback buffer
373 public final List
<DisplayLine
> getScrollbackBuffer() {
378 * The raw display buffer characters + attributes.
380 private volatile List
<DisplayLine
> display
;
383 * Get the display buffer.
385 * @return the display buffer
387 public final List
<DisplayLine
> getDisplayBuffer() {
392 * The terminal's input. For type == XTERM, this is an InputStreamReader
393 * with UTF-8 encoding.
395 private Reader input
;
398 * The terminal's raw InputStream. This is used for type != XTERM.
400 private volatile InputStream inputStream
;
403 * The terminal's output. For type == XTERM, this wraps an
404 * OutputStreamWriter with UTF-8 encoding.
406 private Writer output
;
409 * The terminal's raw OutputStream. This is used for type != XTERM.
411 private OutputStream outputStream
;
414 * Parser character scan states.
431 VT52_DIRECT_CURSOR_ADDRESS
435 * Current scanning state.
437 private ScanState scanState
;
440 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
441 * can't really use it in keypress() because we do not see number pad
442 * events from TKeypress.
444 private enum KeypadMode
{
450 * Arrow keys can emit three different sequences (DECCKM or VT52
453 private enum ArrowKeyMode
{
460 * Available character sets for GL, GR, G0, G1, G2, G3.
462 private enum CharacterSet
{
483 * Single-shift states used by the C1 control characters SS2 (0x8E) and
486 private enum Singleshift
{
493 * VT220+ lockshift states.
495 private enum LockshiftMode
{
505 * XTERM mouse reporting protocols.
507 private enum MouseProtocol
{
516 * Which mouse protocol is active.
518 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
521 * XTERM mouse reporting encodings.
523 private enum MouseEncoding
{
530 * Which mouse encoding is active.
532 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
535 * Physical display width. We start at 80x24, but the user can resize us
541 * Get the display width.
543 * @return the width (usually 80 or 132)
545 public final int getWidth() {
550 * Physical display height. We start at 80x24, but the user can resize
556 * Get the display height.
558 * @return the height (usually 24)
560 public final int getHeight() {
565 * Top margin of the scrolling region.
567 private int scrollRegionTop
;
570 * Bottom margin of the scrolling region.
572 private int scrollRegionBottom
;
575 * Right margin column number. This can be selected by the remote side
576 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
578 private int rightMargin
;
581 * Last character printed.
586 * VT100-style line wrapping: a character is placed in column 80 (or
587 * 132), but the line does NOT wrap until another character is written to
588 * column 1 of the next line, after which the cursor moves to column 2.
590 private boolean wrapLineFlag
;
593 * VT220 single shift flag.
595 private Singleshift singleshift
= Singleshift
.NONE
;
598 * true = insert characters, false = overwrite.
600 private boolean insertMode
= false;
603 * VT52 mode as selected by DECANM. True means VT52, false means
604 * ANSI. Default is ANSI.
606 private boolean vt52Mode
= false;
609 * Visible cursor (DECTCEM).
611 private boolean cursorVisible
= true;
614 * Get visible cursor flag.
616 * @return if true, the cursor is visible
618 public final boolean isCursorVisible() {
619 return cursorVisible
;
623 * Screen title as set by the xterm OSC sequence. Lots of applications
624 * send a screenTitle regardless of whether it is an xterm client or not.
626 private String screenTitle
= "";
629 * Get the screen title as set by the xterm OSC sequence. Lots of
630 * applications send a screenTitle regardless of whether it is an xterm
633 * @return screen title
635 public final String
getScreenTitle() {
640 * Parameter characters being collected.
642 private List
<Integer
> csiParams
;
645 * Non-csi collect buffer.
647 private StringBuilder collectBuffer
;
650 * When true, use the G1 character set.
652 private boolean shiftOut
= false;
655 * Horizontal tab stop locations.
657 private List
<Integer
> tabStops
;
660 * S8C1T. True means 8bit controls, false means 7bit controls.
662 private boolean s8c1t
= false;
665 * Printer mode. True means send all output to printer, which discards
668 private boolean printerControllerMode
= false;
671 * LMN line mode. If true, linefeed() puts the cursor on the first
672 * column of the next line. If false, linefeed() puts the cursor one
673 * line down on the current line. The default is false.
675 private boolean newLineMode
= false;
678 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
680 private ArrowKeyMode arrowKeyMode
;
683 * Whether number pad keys send VT100 or VT52, application or numeric
686 @SuppressWarnings("unused")
687 private KeypadMode keypadMode
;
690 * When true, the terminal is in 132-column mode (DECCOLM).
692 private boolean columns132
= false;
695 * Get 132 columns value.
697 * @return if true, the terminal is in 132 column mode
699 public final boolean isColumns132() {
704 * true = reverse video. Set by DECSCNM.
706 private boolean reverseVideo
= false;
709 * false = echo characters locally.
711 private boolean fullDuplex
= true;
714 * DECSC/DECRC save/restore a subset of the total state. This class
715 * encapsulates those specific flags/modes.
717 private class SaveableState
{
720 * When true, cursor positions are relative to the scrolling region.
722 public boolean originMode
= false;
725 * The current editing X position.
727 public int cursorX
= 0;
730 * The current editing Y position.
732 public int cursorY
= 0;
735 * Which character set is currently selected in G0.
737 public CharacterSet g0Charset
= CharacterSet
.US
;
740 * Which character set is currently selected in G1.
742 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
745 * Which character set is currently selected in G2.
747 public CharacterSet g2Charset
= CharacterSet
.US
;
750 * Which character set is currently selected in G3.
752 public CharacterSet g3Charset
= CharacterSet
.US
;
755 * Which character set is currently selected in GR.
757 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
760 * The current drawing attributes.
762 public CellAttributes attr
;
767 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
772 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
777 public boolean lineWrap
= true;
782 public void reset() {
786 g0Charset
= CharacterSet
.US
;
787 g1Charset
= CharacterSet
.DRAWING
;
788 g2Charset
= CharacterSet
.US
;
789 g3Charset
= CharacterSet
.US
;
790 grCharset
= CharacterSet
.DRAWING
;
791 attr
= new CellAttributes();
792 glLockshift
= LockshiftMode
.NONE
;
793 grLockshift
= LockshiftMode
.NONE
;
798 * Copy attributes from another instance.
800 * @param that the other instance to match
802 public void setTo(final SaveableState that
) {
803 this.originMode
= that
.originMode
;
804 this.cursorX
= that
.cursorX
;
805 this.cursorY
= that
.cursorY
;
806 this.g0Charset
= that
.g0Charset
;
807 this.g1Charset
= that
.g1Charset
;
808 this.g2Charset
= that
.g2Charset
;
809 this.g3Charset
= that
.g3Charset
;
810 this.grCharset
= that
.grCharset
;
811 this.attr
= new CellAttributes();
812 this.attr
.setTo(that
.attr
);
813 this.glLockshift
= that
.glLockshift
;
814 this.grLockshift
= that
.grLockshift
;
815 this.lineWrap
= that
.lineWrap
;
819 * Public constructor.
821 public SaveableState() {
827 * The current terminal state.
829 private SaveableState currentState
;
832 * The last saved terminal state.
834 private SaveableState savedState
;
837 * Clear the CSI parameters and flags.
839 private void toGround() {
841 collectBuffer
= new StringBuilder(8);
842 scanState
= ScanState
.GROUND
;
846 * Reset the tab stops list.
848 private void resetTabStops() {
850 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
851 tabStops
.add(new Integer(i
* 8));
856 * Reset the emulation state.
858 private void reset() {
860 currentState
= new SaveableState();
861 savedState
= new SaveableState();
862 scanState
= ScanState
.GROUND
;
866 scrollRegionBottom
= height
- 1;
867 rightMargin
= width
- 1;
869 arrowKeyMode
= ArrowKeyMode
.ANSI
;
870 keypadMode
= KeypadMode
.Numeric
;
871 wrapLineFlag
= false;
879 reverseVideo
= false;
881 cursorVisible
= true;
884 singleshift
= Singleshift
.NONE
;
886 printerControllerMode
= false;
889 mouseProtocol
= MouseProtocol
.OFF
;
890 mouseEncoding
= MouseEncoding
.X10
;
900 * Public constructor.
902 * @param type one of the DeviceType constants to select VT100, VT102,
904 * @param inputStream an InputStream connected to the remote side. For
905 * type == XTERM, inputStream is converted to a Reader with UTF-8
907 * @param outputStream an OutputStream connected to the remote user. For
908 * type == XTERM, outputStream is converted to a Writer with UTF-8
910 * @throws UnsupportedEncodingException if an exception is thrown when
911 * creating the InputStreamReader
913 public ECMA48(final DeviceType type
, final InputStream inputStream
,
914 final OutputStream outputStream
) throws UnsupportedEncodingException
{
916 assert (inputStream
!= null);
917 assert (outputStream
!= null);
919 csiParams
= new ArrayList
<Integer
>();
920 tabStops
= new ArrayList
<Integer
>();
921 scrollback
= new LinkedList
<DisplayLine
>();
922 display
= new LinkedList
<DisplayLine
>();
925 this.inputStream
= inputStream
;
926 if (type
== DeviceType
.XTERM
) {
927 this.input
= new InputStreamReader(inputStream
, "UTF-8");
928 this.output
= new OutputStreamWriter(outputStream
, "UTF-8");
929 this.outputStream
= null;
932 this.outputStream
= outputStream
;
936 for (int i
= 0; i
< height
; i
++) {
937 display
.add(new DisplayLine(currentState
.attr
));
940 // Spin up the input reader
941 readerThread
= new Thread(this);
942 readerThread
.start();
946 * Append a new line to the bottom of the display, adding lines off the
947 * top to the scrollback buffer.
949 private void newDisplayLine() {
950 // Scroll the top line off into the scrollback buffer
951 scrollback
.add(display
.get(0));
953 DisplayLine line
= new DisplayLine(currentState
.attr
);
954 line
.setReverseColor(reverseVideo
);
959 * Wraps the current line.
961 private void wrapCurrentLine() {
962 if (currentState
.cursorY
== height
- 1) {
965 if (currentState
.cursorY
< height
- 1) {
966 currentState
.cursorY
++;
968 currentState
.cursorX
= 0;
972 * Handle a carriage return.
974 private void carriageReturn() {
975 currentState
.cursorX
= 0;
976 wrapLineFlag
= false;
980 * Reverse the color of the visible display.
982 private void invertDisplayColors() {
983 for (DisplayLine line
: display
) {
984 line
.setReverseColor(!line
.isReverseColor());
991 private void linefeed() {
993 if (currentState
.cursorY
< scrollRegionBottom
) {
994 // Increment screen y
995 currentState
.cursorY
++;
998 // Screen y does not increment
1001 * Two cases: either we're inside a scrolling region or not. If
1002 * the scrolling region bottom is the bottom of the screen, then
1003 * push the top line into the buffer. Else scroll the scrolling
1006 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1008 // We're at the bottom of the scroll region, AND the scroll
1009 // region is the entire screen.
1015 // We're at the bottom of the scroll region, AND the scroll
1016 // region is NOT the entire screen.
1017 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1022 currentState
.cursorX
= 0;
1024 wrapLineFlag
= false;
1028 * Prints one character to the display buffer.
1030 * @param ch character to display
1032 private void printCharacter(final char ch
) {
1033 int rightMargin
= this.rightMargin
;
1035 // Check if we have double-width, and if so chop at 40/66 instead of
1037 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1038 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1041 // Check the unusually-complicated line wrapping conditions...
1042 if (currentState
.cursorX
== rightMargin
) {
1044 if (currentState
.lineWrap
== true) {
1046 * This case happens when: the cursor was already on the
1047 * right margin (either through printing or by an explicit
1048 * placement command), and a character was printed.
1050 * The line wraps only when a new character arrives AND the
1051 * cursor is already on the right margin AND has placed a
1052 * character in its cell. Easier to see than to explain.
1054 if (wrapLineFlag
== false) {
1056 * This block marks the case that we are in the margin
1057 * and the first character has been received and printed.
1059 wrapLineFlag
= true;
1062 * This block marks the case that we are in the margin
1063 * and the second character has been received and
1066 wrapLineFlag
= false;
1070 } else if (currentState
.cursorX
<= rightMargin
) {
1072 * This is the normal case: a character came in and was printed
1073 * to the left of the right margin column.
1076 // Turn off VT100 special-case flag
1077 wrapLineFlag
= false;
1080 // "Print" the character
1081 Cell newCell
= new Cell(ch
);
1082 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1083 newCellAttributes
.setTo(currentState
.attr
);
1084 DisplayLine line
= display
.get(currentState
.cursorY
);
1085 // Insert mode special case
1086 if (insertMode
== true) {
1087 line
.insert(currentState
.cursorX
, newCell
);
1089 // Replace an existing character
1090 line
.replace(currentState
.cursorX
, newCell
);
1093 // Increment horizontal
1094 if (wrapLineFlag
== false) {
1095 currentState
.cursorX
++;
1096 if (currentState
.cursorX
> rightMargin
) {
1097 currentState
.cursorX
--;
1103 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1104 * send to the remote side.
1106 * @param mouse mouse event received from the local user
1108 public void mouse(final TMouseEvent mouse
) {
1111 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1112 mouseProtocol, mouseEncoding, mouse);
1115 if (mouseEncoding
== MouseEncoding
.X10
) {
1116 // We will support X10 but only for (160,94) and smaller.
1117 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1122 switch (mouseProtocol
) {
1129 // Only report button presses
1130 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1136 // Only report button presses and releases
1137 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1138 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1146 * Only report button presses, button releases, and motions that
1147 * have a button down (i.e. drag-and-drop).
1149 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1150 if (!mouse
.isMouse1()
1151 && !mouse
.isMouse2()
1152 && !mouse
.isMouse3()
1153 && !mouse
.isMouseWheelUp()
1154 && !mouse
.isMouseWheelDown()
1162 // Report everything
1166 // Now encode the event
1167 StringBuilder sb
= new StringBuilder(6);
1168 if (mouseEncoding
== MouseEncoding
.SGR
) {
1169 sb
.append((char) 0x1B);
1172 if (mouse
.isMouse1()) {
1173 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1178 } else if (mouse
.isMouse2()) {
1179 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1184 } else if (mouse
.isMouse3()) {
1185 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1190 } else if (mouse
.isMouseWheelUp()) {
1192 } else if (mouse
.isMouseWheelDown()) {
1195 // This is motion with no buttons down.
1199 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1202 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1209 // X10 and UTF8 encodings
1210 sb
.append((char) 0x1B);
1213 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1214 sb
.append((char) (0x03 + 32));
1215 } else if (mouse
.isMouse1()) {
1216 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1217 sb
.append((char) (0x00 + 32 + 32));
1219 sb
.append((char) (0x00 + 32));
1221 } else if (mouse
.isMouse2()) {
1222 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1223 sb
.append((char) (0x01 + 32 + 32));
1225 sb
.append((char) (0x01 + 32));
1227 } else if (mouse
.isMouse3()) {
1228 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1229 sb
.append((char) (0x02 + 32 + 32));
1231 sb
.append((char) (0x02 + 32));
1233 } else if (mouse
.isMouseWheelUp()) {
1234 sb
.append((char) (0x04 + 64));
1235 } else if (mouse
.isMouseWheelDown()) {
1236 sb
.append((char) (0x05 + 64));
1238 // This is motion with no buttons down.
1239 sb
.append((char) (0x03 + 32));
1242 sb
.append((char) (mouse
.getX() + 33));
1243 sb
.append((char) (mouse
.getY() + 33));
1246 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1247 writeRemote(sb
.toString());
1251 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1252 * send to the remote side.
1254 * @param keypress keypress received from the local user
1256 public void keypress(final TKeypress keypress
) {
1257 writeRemote(keypressToString(keypress
));
1261 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1263 * @param keypress keypress received from the local user
1264 * @return string to transmit to the remote side
1266 private String
keypressToString(final TKeypress keypress
) {
1268 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1270 * If this is a control character, process it like it came from
1273 if (keypress
.getChar() < 0x20) {
1274 handleControlChar(keypress
.getChar());
1276 // Local echo for everything else
1277 printCharacter(keypress
.getChar());
1281 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1286 // Handle control characters
1287 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1288 StringBuilder sb
= new StringBuilder();
1289 char ch
= keypress
.getChar();
1292 return sb
.toString();
1295 // Handle alt characters
1296 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1297 StringBuilder sb
= new StringBuilder("\033");
1298 char ch
= keypress
.getChar();
1300 return sb
.toString();
1303 if (keypress
.equals(kbBackspace
)) {
1316 if (keypress
.equals(kbLeft
)) {
1317 switch (arrowKeyMode
) {
1327 if (keypress
.equals(kbRight
)) {
1328 switch (arrowKeyMode
) {
1338 if (keypress
.equals(kbUp
)) {
1339 switch (arrowKeyMode
) {
1349 if (keypress
.equals(kbDown
)) {
1350 switch (arrowKeyMode
) {
1360 if (keypress
.equals(kbHome
)) {
1361 switch (arrowKeyMode
) {
1371 if (keypress
.equals(kbEnd
)) {
1372 switch (arrowKeyMode
) {
1382 if (keypress
.equals(kbF1
)) {
1390 if (keypress
.equals(kbF2
)) {
1398 if (keypress
.equals(kbF3
)) {
1406 if (keypress
.equals(kbF4
)) {
1414 if (keypress
.equals(kbF5
)) {
1427 if (keypress
.equals(kbF6
)) {
1440 if (keypress
.equals(kbF7
)) {
1453 if (keypress
.equals(kbF8
)) {
1466 if (keypress
.equals(kbF9
)) {
1479 if (keypress
.equals(kbF10
)) {
1492 if (keypress
.equals(kbF11
)) {
1496 if (keypress
.equals(kbF12
)) {
1500 if (keypress
.equals(kbShiftF1
)) {
1508 if (keypress
.equals(kbShiftF2
)) {
1516 if (keypress
.equals(kbShiftF3
)) {
1524 if (keypress
.equals(kbShiftF4
)) {
1532 if (keypress
.equals(kbShiftF5
)) {
1534 return "\033[15;2~";
1537 if (keypress
.equals(kbShiftF6
)) {
1539 return "\033[17;2~";
1542 if (keypress
.equals(kbShiftF7
)) {
1544 return "\033[18;2~";
1547 if (keypress
.equals(kbShiftF8
)) {
1549 return "\033[19;2~";
1552 if (keypress
.equals(kbShiftF9
)) {
1554 return "\033[20;2~";
1557 if (keypress
.equals(kbShiftF10
)) {
1559 return "\033[21;2~";
1562 if (keypress
.equals(kbShiftF11
)) {
1564 return "\033[23;2~";
1567 if (keypress
.equals(kbShiftF12
)) {
1569 return "\033[24;2~";
1572 if (keypress
.equals(kbCtrlF1
)) {
1580 if (keypress
.equals(kbCtrlF2
)) {
1588 if (keypress
.equals(kbCtrlF3
)) {
1596 if (keypress
.equals(kbCtrlF4
)) {
1604 if (keypress
.equals(kbCtrlF5
)) {
1606 return "\033[15;5~";
1609 if (keypress
.equals(kbCtrlF6
)) {
1611 return "\033[17;5~";
1614 if (keypress
.equals(kbCtrlF7
)) {
1616 return "\033[18;5~";
1619 if (keypress
.equals(kbCtrlF8
)) {
1621 return "\033[19;5~";
1624 if (keypress
.equals(kbCtrlF9
)) {
1626 return "\033[20;5~";
1629 if (keypress
.equals(kbCtrlF10
)) {
1631 return "\033[21;5~";
1634 if (keypress
.equals(kbCtrlF11
)) {
1636 return "\033[23;5~";
1639 if (keypress
.equals(kbCtrlF12
)) {
1641 return "\033[24;5~";
1644 if (keypress
.equals(kbPgUp
)) {
1649 if (keypress
.equals(kbPgDn
)) {
1654 if (keypress
.equals(kbIns
)) {
1659 if (keypress
.equals(kbShiftIns
)) {
1660 // This is what xterm sends for SHIFT-INS
1662 // This is what xterm sends for CTRL-INS
1663 // return "\033[2;5~";
1666 if (keypress
.equals(kbShiftDel
)) {
1667 // This is what xterm sends for SHIFT-DEL
1669 // This is what xterm sends for CTRL-DEL
1670 // return "\033[3;5~";
1673 if (keypress
.equals(kbDel
)) {
1674 // Delete sends real delete for VTxxx
1676 // return "\033[3~";
1679 if (keypress
.equals(kbEnter
)) {
1683 if (keypress
.equals(kbEsc
)) {
1687 if (keypress
.equals(kbAltEsc
)) {
1691 if (keypress
.equals(kbTab
)) {
1695 // Non-alt, non-ctrl characters
1696 if (!keypress
.isFnKey()) {
1697 StringBuilder sb
= new StringBuilder();
1698 sb
.append(keypress
.getChar());
1699 return sb
.toString();
1705 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1708 * @param ch 8-bit character from the remote side
1709 * @param charsetGl character set defined for GL
1710 * @param charsetGr character set defined for GR
1711 * @return character to display on the screen
1713 private char mapCharacterCharset(final char ch
,
1714 final CharacterSet charsetGl
,
1715 final CharacterSet charsetGr
) {
1717 int lookupChar
= ch
;
1718 CharacterSet lookupCharset
= charsetGl
;
1721 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
1722 lookupCharset
= charsetGr
;
1726 switch (lookupCharset
) {
1729 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
1732 return DECCharacterSets
.UK
[lookupChar
];
1735 return DECCharacterSets
.US_ASCII
[lookupChar
];
1738 return DECCharacterSets
.NL
[lookupChar
];
1741 return DECCharacterSets
.FI
[lookupChar
];
1744 return DECCharacterSets
.FR
[lookupChar
];
1747 return DECCharacterSets
.FR_CA
[lookupChar
];
1750 return DECCharacterSets
.DE
[lookupChar
];
1753 return DECCharacterSets
.IT
[lookupChar
];
1756 return DECCharacterSets
.NO
[lookupChar
];
1759 return DECCharacterSets
.ES
[lookupChar
];
1762 return DECCharacterSets
.SV
[lookupChar
];
1765 return DECCharacterSets
.SWISS
[lookupChar
];
1767 case DEC_SUPPLEMENTAL
:
1768 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
1771 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
1774 return DECCharacterSets
.US_ASCII
[lookupChar
];
1777 return DECCharacterSets
.US_ASCII
[lookupChar
];
1780 throw new IllegalArgumentException("Invalid character set value: "
1786 * Map an 8-bit byte into a printable character.
1788 * @param ch either 8-bit or Unicode character from the remote side
1789 * @return character to display on the screen
1791 private char mapCharacter(final char ch
) {
1793 // Unicode character, just return it
1797 CharacterSet charsetGl
= currentState
.g0Charset
;
1798 CharacterSet charsetGr
= currentState
.grCharset
;
1800 if (vt52Mode
== true) {
1801 if (shiftOut
== true) {
1802 // Shifted out character, pull from VT52 graphics
1803 charsetGl
= currentState
.g1Charset
;
1804 charsetGr
= CharacterSet
.US
;
1807 charsetGl
= currentState
.g0Charset
;
1808 charsetGr
= CharacterSet
.US
;
1811 // Pull the character
1812 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1816 if (shiftOut
== true) {
1817 // Shifted out character, pull from G1
1818 charsetGl
= currentState
.g1Charset
;
1819 charsetGr
= currentState
.grCharset
;
1821 // Pull the character
1822 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1826 if (singleshift
== Singleshift
.SS2
) {
1828 singleshift
= Singleshift
.NONE
;
1830 // Shifted out character, pull from G2
1831 charsetGl
= currentState
.g2Charset
;
1832 charsetGr
= currentState
.grCharset
;
1836 if (singleshift
== Singleshift
.SS3
) {
1838 singleshift
= Singleshift
.NONE
;
1840 // Shifted out character, pull from G3
1841 charsetGl
= currentState
.g3Charset
;
1842 charsetGr
= currentState
.grCharset
;
1845 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
1846 // Check for locking shift
1848 switch (currentState
.glLockshift
) {
1861 charsetGl
= currentState
.g2Charset
;
1866 charsetGl
= currentState
.g3Charset
;
1871 charsetGl
= currentState
.g0Charset
;
1875 switch (currentState
.grLockshift
) {
1885 charsetGr
= currentState
.g1Charset
;
1890 charsetGr
= currentState
.g2Charset
;
1895 charsetGr
= currentState
.g3Charset
;
1900 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
1907 // Pull the character
1908 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1912 * Scroll the text within a scrolling region up n lines.
1914 * @param regionTop top row of the scrolling region
1915 * @param regionBottom bottom row of the scrolling region
1916 * @param n number of lines to scroll
1918 private void scrollingRegionScrollUp(final int regionTop
,
1919 final int regionBottom
, final int n
) {
1921 if (regionTop
>= regionBottom
) {
1925 // Sanity check: see if there will be any characters left after the
1927 if (regionBottom
+ 1 - regionTop
<= n
) {
1928 // There won't be anything left in the region, so just call
1929 // eraseScreen() and return.
1930 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1934 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1935 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1936 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1938 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
1939 - remaining
, regionBottom
+ 1);
1940 display
= new LinkedList
<DisplayLine
>(displayTop
);
1941 display
.addAll(displayMiddle
);
1942 for (int i
= 0; i
< n
; i
++) {
1943 DisplayLine line
= new DisplayLine(currentState
.attr
);
1944 line
.setReverseColor(reverseVideo
);
1947 display
.addAll(displayBottom
);
1949 assert (display
.size() == height
);
1953 * Scroll the text within a scrolling region down n lines.
1955 * @param regionTop top row of the scrolling region
1956 * @param regionBottom bottom row of the scrolling region
1957 * @param n number of lines to scroll
1959 private void scrollingRegionScrollDown(final int regionTop
,
1960 final int regionBottom
, final int n
) {
1962 if (regionTop
>= regionBottom
) {
1966 // Sanity check: see if there will be any characters left after the
1968 if (regionBottom
+ 1 - regionTop
<= n
) {
1969 // There won't be anything left in the region, so just call
1970 // eraseScreen() and return.
1971 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1975 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1976 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1977 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1979 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
1980 regionTop
+ remaining
);
1981 display
= new LinkedList
<DisplayLine
>(displayTop
);
1982 for (int i
= 0; i
< n
; i
++) {
1983 DisplayLine line
= new DisplayLine(currentState
.attr
);
1984 line
.setReverseColor(reverseVideo
);
1987 display
.addAll(displayMiddle
);
1988 display
.addAll(displayBottom
);
1990 assert (display
.size() == height
);
1994 * Process a control character.
1996 * @param ch 8-bit character from the remote side
1998 private void handleControlChar(final char ch
) {
1999 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2010 // Transmit the answerback message.
2021 cursorLeft(1, false);
2026 advanceToNextTabStop();
2052 currentState
.glLockshift
= LockshiftMode
.NONE
;
2058 currentState
.glLockshift
= LockshiftMode
.NONE
;
2083 singleshift
= Singleshift
.SS2
;
2088 singleshift
= Singleshift
.SS3
;
2098 * Advance the cursor to the next tab stop.
2100 private void advanceToNextTabStop() {
2101 if (tabStops
.size() == 0) {
2102 // Go to the rightmost column
2103 cursorRight(rightMargin
- currentState
.cursorX
, false);
2106 for (Integer stop
: tabStops
) {
2107 if (stop
> currentState
.cursorX
) {
2108 cursorRight(stop
- currentState
.cursorX
, false);
2113 * We got here, meaning there isn't a tab stop beyond the current
2114 * cursor position. Place the cursor of the right-most edge of the
2117 cursorRight(rightMargin
- currentState
.cursorX
, false);
2121 * Save a character into the collect buffer.
2123 * @param ch character to save
2125 private void collect(final char ch
) {
2126 collectBuffer
.append(ch
);
2130 * Save a byte into the CSI parameters buffer.
2132 * @param ch byte to save
2134 private void param(final byte ch
) {
2135 if (csiParams
.size() == 0) {
2136 csiParams
.add(new Integer(0));
2138 Integer x
= csiParams
.get(csiParams
.size() - 1);
2139 if ((ch
>= '0') && (ch
<= '9')) {
2142 csiParams
.set(csiParams
.size() - 1, x
);
2146 csiParams
.add(new Integer(0));
2151 * Get a CSI parameter value, with a default.
2153 * @param position parameter index. 0 is the first parameter.
2154 * @param defaultValue value to use if csiParams[position] doesn't exist
2155 * @return parameter value
2157 private int getCsiParam(final int position
, final int defaultValue
) {
2158 if (csiParams
.size() < position
+ 1) {
2159 return defaultValue
;
2161 return csiParams
.get(position
).intValue();
2165 * Get a CSI parameter value, clamped to within min/max.
2167 * @param position parameter index. 0 is the first parameter.
2168 * @param defaultValue value to use if csiParams[position] doesn't exist
2169 * @param minValue minimum value inclusive
2170 * @param maxValue maximum value inclusive
2171 * @return parameter value
2173 private int getCsiParam(final int position
, final int defaultValue
,
2174 final int minValue
, final int maxValue
) {
2176 assert (minValue
<= maxValue
);
2177 int value
= getCsiParam(position
, defaultValue
);
2178 if (value
< minValue
) {
2181 if (value
> maxValue
) {
2188 * Set or unset a toggle. value is 'true' for set ('h'), false for reset
2191 private void setToggle(final boolean value
) {
2192 boolean decPrivateModeFlag
= false;
2193 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2194 if (collectBuffer
.charAt(i
) == '?') {
2195 decPrivateModeFlag
= true;
2200 for (Integer i
: csiParams
) {
2205 if (decPrivateModeFlag
== true) {
2207 if (value
== true) {
2208 // Use application arrow keys
2209 arrowKeyMode
= ArrowKeyMode
.VT100
;
2211 // Use ANSI arrow keys
2212 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2217 if (decPrivateModeFlag
== true) {
2218 if (value
== false) {
2222 arrowKeyMode
= ArrowKeyMode
.VT52
;
2225 * From the VT102 docs: "You use ANSI mode to select
2226 * most terminal features; the terminal uses the same
2227 * features when it switches to VT52 mode. You
2228 * cannot, however, change most of these features in
2231 * In other words, do not reset any other attributes
2232 * when switching between VT52 submode and ANSI.
2234 * HOWEVER, the real vt100 does switch the character
2235 * set according to Usenet.
2237 currentState
.g0Charset
= CharacterSet
.US
;
2238 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2241 if ((type
== DeviceType
.VT220
)
2242 || (type
== DeviceType
.XTERM
)) {
2244 // VT52 mode is explicitly 7-bit
2246 singleshift
= Singleshift
.NONE
;
2251 if (value
== true) {
2252 // Turn off keyboard
2261 if (decPrivateModeFlag
== true) {
2263 if (value
== true) {
2272 width
= rightMargin
+ 1;
2273 // Entire screen is cleared, and scrolling region is
2275 eraseScreen(0, 0, height
- 1, width
- 1, false);
2276 scrollRegionTop
= 0;
2277 scrollRegionBottom
= height
- 1;
2278 // Also home the cursor
2279 cursorPosition(0, 0);
2283 if (decPrivateModeFlag
== true) {
2285 if (value
== true) {
2294 if (value
== true) {
2302 if (decPrivateModeFlag
== true) {
2304 if (value
== true) {
2306 * Set selects reverse screen, a white screen
2307 * background with black characters.
2309 if (reverseVideo
!= true) {
2311 * If in normal video, switch it back
2313 invertDisplayColors();
2315 reverseVideo
= true;
2318 * Reset selects normal screen, a black screen
2319 * background with white characters.
2321 if (reverseVideo
== true) {
2323 * If in reverse video already, switch it back
2325 invertDisplayColors();
2327 reverseVideo
= false;
2332 if (decPrivateModeFlag
== true) {
2334 if (value
== true) {
2335 // Origin is relative to scroll region cursor.
2336 // Cursor can NEVER leave scrolling region.
2337 currentState
.originMode
= true;
2338 cursorPosition(0, 0);
2340 // Origin is absolute to entire screen. Cursor can
2341 // leave the scrolling region via cup() and hvp().
2342 currentState
.originMode
= false;
2343 cursorPosition(0, 0);
2348 if (decPrivateModeFlag
== true) {
2350 if (value
== true) {
2352 currentState
.lineWrap
= true;
2354 // Turn linewrap off
2355 currentState
.lineWrap
= false;
2360 if (decPrivateModeFlag
== true) {
2362 if (value
== true) {
2363 // Keyboard auto-repeat on
2366 // Keyboard auto-repeat off
2372 if (decPrivateModeFlag
== false) {
2374 if (value
== true) {
2384 if (decPrivateModeFlag
== true) {
2390 if (decPrivateModeFlag
== true) {
2396 if (decPrivateModeFlag
== false) {
2398 if (value
== true) {
2400 * Set causes a received linefeed, form feed, or
2401 * vertical tab to move cursor to first column of
2402 * next line. RETURN transmits both a carriage return
2403 * and linefeed. This selection is also called new
2409 * Reset causes a received linefeed, form feed, or
2410 * vertical tab to move cursor to next line in
2411 * current column. RETURN transmits a carriage
2414 newLineMode
= false;
2420 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2421 if (decPrivateModeFlag
== true) {
2423 if (value
== true) {
2425 cursorVisible
= true;
2428 cursorVisible
= false;
2435 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2436 if (decPrivateModeFlag
== true) {
2438 if (value
== true) {
2439 // Select national mode NRC
2442 // Select multi-national mode
2451 if ((type
== DeviceType
.XTERM
)
2452 && (decPrivateModeFlag
== true)
2454 // Mouse: normal tracking mode
2455 if (value
== true) {
2456 mouseProtocol
= MouseProtocol
.NORMAL
;
2458 mouseProtocol
= MouseProtocol
.OFF
;
2464 if ((type
== DeviceType
.XTERM
)
2465 && (decPrivateModeFlag
== true)
2467 // Mouse: normal tracking mode
2468 if (value
== true) {
2469 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2471 mouseProtocol
= MouseProtocol
.OFF
;
2477 if ((type
== DeviceType
.XTERM
)
2478 && (decPrivateModeFlag
== true)
2480 // Mouse: Any-event tracking mode
2481 if (value
== true) {
2482 mouseProtocol
= MouseProtocol
.ANYEVENT
;
2484 mouseProtocol
= MouseProtocol
.OFF
;
2490 if ((type
== DeviceType
.XTERM
)
2491 && (decPrivateModeFlag
== true)
2493 // Mouse: UTF-8 coordinates
2494 if (value
== true) {
2495 mouseEncoding
= MouseEncoding
.UTF8
;
2497 mouseEncoding
= MouseEncoding
.X10
;
2503 if ((type
== DeviceType
.XTERM
)
2504 && (decPrivateModeFlag
== true)
2506 // Mouse: SGR coordinates
2507 if (value
== true) {
2508 mouseEncoding
= MouseEncoding
.SGR
;
2510 mouseEncoding
= MouseEncoding
.X10
;
2523 * DECSC - Save cursor.
2525 private void decsc() {
2526 savedState
.setTo(currentState
);
2530 * DECRC - Restore cursor.
2532 private void decrc() {
2533 currentState
.setTo(savedState
);
2539 private void ind() {
2540 // Move the cursor and scroll if necessary. If at the bottom line
2541 // already, a scroll up is supposed to be performed.
2542 if (currentState
.cursorY
== scrollRegionBottom
) {
2543 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2545 cursorDown(1, true);
2549 * RI - Reverse index.
2552 // Move the cursor and scroll if necessary. If at the top line
2553 // already, a scroll down is supposed to be performed.
2554 if (currentState
.cursorY
== scrollRegionTop
) {
2555 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2563 private void nel() {
2564 // Move the cursor and scroll if necessary. If at the bottom line
2565 // already, a scroll up is supposed to be performed.
2566 if (currentState
.cursorY
== scrollRegionBottom
) {
2567 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2569 cursorDown(1, true);
2571 // Reset to the beginning of the next line
2572 currentState
.cursorX
= 0;
2576 * DECKPAM - Keypad application mode.
2578 private void deckpam() {
2579 keypadMode
= KeypadMode
.Application
;
2583 * DECKPNM - Keypad numeric mode.
2585 private void deckpnm() {
2586 keypadMode
= KeypadMode
.Numeric
;
2592 * @param n number of spaces to move
2593 * @param honorScrollRegion if true, then do nothing if the cursor is
2594 * outside the scrolling region
2596 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2600 * Special case: if a user moves the cursor from the right margin, we
2601 * have to reset the VT100 right margin flag.
2604 wrapLineFlag
= false;
2607 for (int i
= 0; i
< n
; i
++) {
2608 if (honorScrollRegion
== true) {
2609 // Honor the scrolling region
2610 if ((currentState
.cursorY
< scrollRegionTop
)
2611 || (currentState
.cursorY
> scrollRegionBottom
)
2613 // Outside region, do nothing
2616 // Inside region, go up
2617 top
= scrollRegionTop
;
2619 // Non-scrolling case
2623 if (currentState
.cursorY
> top
) {
2624 currentState
.cursorY
--;
2630 * Move down n spaces.
2632 * @param n number of spaces to move
2633 * @param honorScrollRegion if true, then do nothing if the cursor is
2634 * outside the scrolling region
2636 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
2640 * Special case: if a user moves the cursor from the right margin, we
2641 * have to reset the VT100 right margin flag.
2644 wrapLineFlag
= false;
2647 for (int i
= 0; i
< n
; i
++) {
2649 if (honorScrollRegion
== true) {
2650 // Honor the scrolling region
2651 if (currentState
.cursorY
> scrollRegionBottom
) {
2652 // Outside region, do nothing
2655 // Inside region, go down
2656 bottom
= scrollRegionBottom
;
2658 // Non-scrolling case
2659 bottom
= height
- 1;
2662 if (currentState
.cursorY
< bottom
) {
2663 currentState
.cursorY
++;
2669 * Move left n spaces.
2671 * @param n number of spaces to move
2672 * @param honorScrollRegion if true, then do nothing if the cursor is
2673 * outside the scrolling region
2675 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
2677 * Special case: if a user moves the cursor from the right margin, we
2678 * have to reset the VT100 right margin flag.
2681 wrapLineFlag
= false;
2684 for (int i
= 0; i
< n
; i
++) {
2685 if (honorScrollRegion
== true) {
2686 // Honor the scrolling region
2687 if ((currentState
.cursorY
< scrollRegionTop
)
2688 || (currentState
.cursorY
> scrollRegionBottom
)
2690 // Outside region, do nothing
2695 if (currentState
.cursorX
> 0) {
2696 currentState
.cursorX
--;
2702 * Move right n spaces.
2704 * @param n number of spaces to move
2705 * @param honorScrollRegion if true, then do nothing if the cursor is
2706 * outside the scrolling region
2708 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
2709 int rightMargin
= this.rightMargin
;
2712 * Special case: if a user moves the cursor from the right margin, we
2713 * have to reset the VT100 right margin flag.
2716 wrapLineFlag
= false;
2719 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2720 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2723 for (int i
= 0; i
< n
; i
++) {
2724 if (honorScrollRegion
== true) {
2725 // Honor the scrolling region
2726 if ((currentState
.cursorY
< scrollRegionTop
)
2727 || (currentState
.cursorY
> scrollRegionBottom
)
2729 // Outside region, do nothing
2734 if (currentState
.cursorX
< rightMargin
) {
2735 currentState
.cursorX
++;
2741 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2743 * @param row row to move to
2744 * @param col column to move to
2746 private void cursorPosition(int row
, final int col
) {
2747 int rightMargin
= this.rightMargin
;
2752 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2753 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2756 // Set column number
2757 currentState
.cursorX
= col
;
2759 // Sanity check, bring column back to margin.
2760 if (currentState
.cursorX
> rightMargin
) {
2761 currentState
.cursorX
= rightMargin
;
2765 if (currentState
.originMode
== true) {
2766 row
+= scrollRegionTop
;
2768 if (currentState
.cursorY
< row
) {
2769 cursorDown(row
- currentState
.cursorY
, false);
2770 } else if (currentState
.cursorY
> row
) {
2771 cursorUp(currentState
.cursorY
- row
, false);
2774 wrapLineFlag
= false;
2778 * HTS - Horizontal tabulation set.
2780 private void hts() {
2781 for (Integer stop
: tabStops
) {
2782 if (stop
== currentState
.cursorX
) {
2783 // Already have a tab stop here
2788 // Append a tab stop to the end of the array and resort them
2789 tabStops
.add(currentState
.cursorX
);
2790 Collections
.sort(tabStops
);
2794 * DECSWL - Single-width line.
2796 private void decswl() {
2797 display
.get(currentState
.cursorY
).setDoubleWidth(false);
2798 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2802 * DECDWL - Double-width line.
2804 private void decdwl() {
2805 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2806 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2810 * DECHDL - Double-height + double-width line.
2812 * @param topHalf if true, this sets the row to be the top half row of a
2815 private void dechdl(final boolean topHalf
) {
2816 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2817 if (topHalf
== true) {
2818 display
.get(currentState
.cursorY
).setDoubleHeight(1);
2820 display
.get(currentState
.cursorY
).setDoubleHeight(2);
2825 * DECALN - Screen alignment display.
2827 private void decaln() {
2828 Cell newCell
= new Cell();
2829 newCell
.setChar('E');
2830 for (DisplayLine line
: display
) {
2831 for (int i
= 0; i
< line
.length(); i
++) {
2832 line
.replace(i
, newCell
);
2838 * DECSCL - Compatibility level.
2840 private void decscl() {
2841 int i
= getCsiParam(0, 0);
2842 int j
= getCsiParam(1, 0);
2846 currentState
.g0Charset
= CharacterSet
.US
;
2847 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2849 } else if (i
== 62) {
2851 if ((j
== 0) || (j
== 2)) {
2853 } else if (j
== 1) {
2860 * CUD - Cursor down.
2862 private void cud() {
2863 cursorDown(getCsiParam(0, 1, 1, height
), true);
2867 * CUF - Cursor forward.
2869 private void cuf() {
2870 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
2874 * CUB - Cursor backward.
2876 private void cub() {
2877 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
2883 private void cuu() {
2884 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2888 * CUP - Cursor position.
2890 private void cup() {
2891 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2892 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
2896 * CNL - Cursor down and to column 1.
2898 private void cnl() {
2899 cursorDown(getCsiParam(0, 1, 1, height
), true);
2901 cursorLeft(currentState
.cursorX
, true);
2905 * CPL - Cursor up and to column 1.
2907 private void cpl() {
2908 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2910 cursorLeft(currentState
.cursorX
, true);
2914 * CHA - Cursor to column # in current row.
2916 private void cha() {
2917 cursorPosition(currentState
.cursorY
,
2918 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
2922 * VPA - Cursor to row #, same column.
2924 private void vpa() {
2925 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2926 currentState
.cursorX
);
2930 * ED - Erase in display.
2933 boolean honorProtected
= false;
2934 boolean decPrivateModeFlag
= false;
2936 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2937 if (collectBuffer
.charAt(i
) == '?') {
2938 decPrivateModeFlag
= true;
2943 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2944 && (decPrivateModeFlag
== true)
2946 honorProtected
= true;
2949 int i
= getCsiParam(0, 0);
2952 // Erase from here to end of screen
2953 if (currentState
.cursorY
< height
- 1) {
2954 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
2957 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2958 } else if (i
== 1) {
2959 // Erase from beginning of screen to here
2960 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
2962 eraseLine(0, currentState
.cursorX
, honorProtected
);
2963 } else if (i
== 2) {
2964 // Erase entire screen
2965 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
2970 * EL - Erase in line.
2973 boolean honorProtected
= false;
2974 boolean decPrivateModeFlag
= false;
2976 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2977 if (collectBuffer
.charAt(i
) == '?') {
2978 decPrivateModeFlag
= true;
2983 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2984 && (decPrivateModeFlag
== true)
2986 honorProtected
= true;
2989 int i
= getCsiParam(0, 0);
2992 // Erase from here to end of line
2993 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2994 } else if (i
== 1) {
2995 // Erase from beginning of line to here
2996 eraseLine(0, currentState
.cursorX
, honorProtected
);
2997 } else if (i
== 2) {
2998 // Erase entire line
2999 eraseLine(0, width
- 1, honorProtected
);
3004 * ECH - Erase # of characters in current row.
3006 private void ech() {
3007 int i
= getCsiParam(0, 1, 1, width
);
3009 // Erase from here to i characters
3010 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3017 int i
= getCsiParam(0, 1);
3019 if ((currentState
.cursorY
>= scrollRegionTop
)
3020 && (currentState
.cursorY
<= scrollRegionBottom
)
3023 // I can get the same effect with a scroll-down
3024 scrollingRegionScrollDown(currentState
.cursorY
,
3025 scrollRegionBottom
, i
);
3030 * DCH - Delete char.
3032 private void dch() {
3033 int n
= getCsiParam(0, 1);
3034 DisplayLine line
= display
.get(currentState
.cursorY
);
3035 Cell blank
= new Cell();
3036 for (int i
= 0; i
< n
; i
++) {
3037 line
.delete(currentState
.cursorX
, blank
);
3042 * ICH - Insert blank char at cursor.
3044 private void ich() {
3045 int n
= getCsiParam(0, 1);
3046 DisplayLine line
= display
.get(currentState
.cursorY
);
3047 Cell blank
= new Cell();
3048 for (int i
= 0; i
< n
; i
++) {
3049 line
.insert(currentState
.cursorX
, blank
);
3057 int i
= getCsiParam(0, 1);
3059 if ((currentState
.cursorY
>= scrollRegionTop
)
3060 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3062 // I can get the same effect with a scroll-down
3063 scrollingRegionScrollUp(currentState
.cursorY
,
3064 scrollRegionBottom
, i
);
3069 * HVP - Horizontal and vertical position.
3071 private void hvp() {
3076 * REP - Repeat character.
3078 private void rep() {
3079 int n
= getCsiParam(0, 1);
3080 for (int i
= 0; i
< n
; i
++) {
3081 printCharacter(repCh
);
3089 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3090 getCsiParam(0, 1, 1, height
));
3097 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3098 getCsiParam(0, 1, 1, height
));
3102 * CBT - Go back X tab stops.
3104 private void cbt() {
3105 int tabsToMove
= getCsiParam(0, 1);
3108 for (int i
= 0; i
< tabsToMove
; i
++) {
3109 int j
= currentState
.cursorX
;
3110 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3111 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3119 j
= tabStops
.get(tabI
);
3121 cursorPosition(currentState
.cursorY
, j
);
3126 * CHT - Advance X tab stops.
3128 private void cht() {
3129 int n
= getCsiParam(0, 1);
3130 for (int i
= 0; i
< n
; i
++) {
3131 advanceToNextTabStop();
3136 * SGR - Select graphics rendition.
3138 private void sgr() {
3140 if (csiParams
.size() == 0) {
3141 currentState
.attr
.reset();
3145 for (Integer i
: csiParams
) {
3151 currentState
.attr
.reset();
3156 currentState
.attr
.setBold(true);
3161 currentState
.attr
.setUnderline(true);
3166 currentState
.attr
.setBlink(true);
3171 currentState
.attr
.setReverse(true);
3178 if (type
== DeviceType
.XTERM
) {
3192 if ((type
== DeviceType
.VT220
)
3193 || (type
== DeviceType
.XTERM
)) {
3199 currentState
.attr
.setBold(false);
3204 currentState
.attr
.setUnderline(false);
3209 currentState
.attr
.setBlink(false);
3214 currentState
.attr
.setReverse(false);
3222 // A true VT100/102/220 does not support color, however everyone
3223 // is used to their terminal emulator supporting color so we will
3224 // unconditionally support color for all DeviceType's.
3229 // Set black foreground
3230 currentState
.attr
.setForeColor(Color
.BLACK
);
3233 // Set red foreground
3234 currentState
.attr
.setForeColor(Color
.RED
);
3237 // Set green foreground
3238 currentState
.attr
.setForeColor(Color
.GREEN
);
3241 // Set yellow foreground
3242 currentState
.attr
.setForeColor(Color
.YELLOW
);
3245 // Set blue foreground
3246 currentState
.attr
.setForeColor(Color
.BLUE
);
3249 // Set magenta foreground
3250 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3253 // Set cyan foreground
3254 currentState
.attr
.setForeColor(Color
.CYAN
);
3257 // Set white foreground
3258 currentState
.attr
.setForeColor(Color
.WHITE
);
3261 // Underscore on, default foreground color
3262 currentState
.attr
.setUnderline(true);
3263 currentState
.attr
.setForeColor(Color
.WHITE
);
3266 // Underscore off, default foreground color
3267 currentState
.attr
.setUnderline(false);
3268 currentState
.attr
.setForeColor(Color
.WHITE
);
3271 // Set black background
3272 currentState
.attr
.setBackColor(Color
.BLACK
);
3275 // Set red background
3276 currentState
.attr
.setBackColor(Color
.RED
);
3279 // Set green background
3280 currentState
.attr
.setBackColor(Color
.GREEN
);
3283 // Set yellow background
3284 currentState
.attr
.setBackColor(Color
.YELLOW
);
3287 // Set blue background
3288 currentState
.attr
.setBackColor(Color
.BLUE
);
3291 // Set magenta background
3292 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3295 // Set cyan background
3296 currentState
.attr
.setBackColor(Color
.CYAN
);
3299 // Set white background
3300 currentState
.attr
.setBackColor(Color
.WHITE
);
3303 // Default background
3304 currentState
.attr
.setBackColor(Color
.BLACK
);
3314 * DA - Device attributes.
3317 int extendedFlag
= 0;
3319 if (collectBuffer
.length() > 0) {
3320 String args
= collectBuffer
.substring(1);
3321 if (collectBuffer
.charAt(0) == '>') {
3323 if (collectBuffer
.length() >= 2) {
3324 i
= Integer
.parseInt(args
.toString());
3326 } else if (collectBuffer
.charAt(0) == '=') {
3328 if (collectBuffer
.length() >= 2) {
3329 i
= Integer
.parseInt(args
.toString());
3332 // Unknown code, bail out
3337 if ((i
!= 0) && (i
!= 1)) {
3341 if ((extendedFlag
== 0) && (i
== 0)) {
3342 // Send string directly to remote side
3343 writeRemote(deviceTypeResponse());
3347 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3349 if ((extendedFlag
== 1) && (i
== 0)) {
3351 * Request "What type of terminal are you, what is your
3352 * firmware version, and what hardware options do you have
3355 * Respond: "I am a VT220 (identification code of 1), my
3356 * firmware version is _____ (Pv), and I have _____ Po
3357 * options installed."
3363 if (s8c1t
== true) {
3364 writeRemote("\u009b>1;10;0c");
3366 writeRemote("\033[>1;10;0c");
3372 if ((extendedFlag
== 2) && (i
== 0)) {
3375 * Request "What is your unit ID?"
3377 * Respond: "I was manufactured at site 00 and have a unique ID
3381 writeRemote("\033P!|00010203\033\\");
3386 * DECSTBM - Set top and bottom margins.
3388 private void decstbm() {
3389 boolean decPrivateModeFlag
= false;
3391 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3392 if (collectBuffer
.charAt(i
) == '?') {
3393 decPrivateModeFlag
= true;
3397 if (decPrivateModeFlag
) {
3398 // This could be restore DEC private mode values.
3402 int top
= getCsiParam(0, 1, 1, height
) - 1;
3403 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3408 scrollRegionTop
= top
;
3409 scrollRegionBottom
= bottom
;
3412 cursorPosition(0, 0);
3417 * DECREQTPARM - Request terminal parameters.
3419 private void decreqtparm() {
3420 int i
= getCsiParam(0, 0);
3422 if ((i
!= 0) && (i
!= 1)) {
3429 * Request terminal parameters.
3433 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3434 * (CLoCk MULtiplier = 1, STP option flags = 0)
3438 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3441 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3443 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3449 * DECSCA - Select Character Attributes.
3451 private void decsca() {
3452 int i
= getCsiParam(0, 0);
3454 if ((i
== 0) || (i
== 2)) {
3456 currentState
.attr
.setProtect(false);
3460 currentState
.attr
.setProtect(true);
3465 * DECSTR - Soft Terminal Reset.
3467 private void decstr() {
3468 // Do exactly like RIS - Reset to initial state
3470 // Do I clear screen too? I think so...
3471 eraseScreen(0, 0, height
- 1, width
- 1, false);
3472 cursorPosition(0, 0);
3476 * DSR - Device status report.
3478 private void dsr() {
3479 boolean decPrivateModeFlag
= false;
3481 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3482 if (collectBuffer
.charAt(i
) == '?') {
3483 decPrivateModeFlag
= true;
3488 int i
= getCsiParam(0, 0);
3493 // Request status report. Respond with "OK, no malfunction."
3495 // Send string directly to remote side
3496 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3499 writeRemote("\u009b0n");
3501 writeRemote("\033[0n");
3506 // Request cursor position. Respond with current position.
3508 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3511 str
= String
.format("\u009b%d;%dR",
3512 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3514 str
= String
.format("\033[%d;%dR",
3515 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3518 // Send string directly to remote side
3523 if (decPrivateModeFlag
== true) {
3525 // Request printer status report. Respond with "Printer not
3528 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3529 && (s8c1t
== true)) {
3530 writeRemote("\u009b?13n");
3532 writeRemote("\033[?13n");
3538 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3539 && (decPrivateModeFlag
== true)
3542 // Request user-defined keys are locked or unlocked. Respond
3543 // with "User-defined keys are locked."
3545 if (s8c1t
== true) {
3546 writeRemote("\u009b?21n");
3548 writeRemote("\033[?21n");
3554 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3555 && (decPrivateModeFlag
== true)
3558 // Request keyboard language. Respond with "Keyboard
3559 // language is North American."
3561 if (s8c1t
== true) {
3562 writeRemote("\u009b?27;1n");
3564 writeRemote("\033[?27;1n");
3571 // Some other option, ignore
3577 * TBC - Tabulation clear.
3579 private void tbc() {
3580 int i
= getCsiParam(0, 0);
3582 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3583 for (Integer stop
: tabStops
) {
3584 if (stop
== currentState
.cursorX
) {
3589 tabStops
= newStops
;
3597 * Erase the characters in the current line from the start column to the
3598 * end column, inclusive.
3600 * @param start starting column to erase (between 0 and width - 1)
3601 * @param end ending column to erase (between 0 and width - 1)
3602 * @param honorProtected if true, do not erase characters with the
3603 * protected attribute set
3605 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3610 if (end
> width
- 1) {
3617 for (int i
= start
; i
<= end
; i
++) {
3618 DisplayLine line
= display
.get(currentState
.cursorY
);
3619 if ((!honorProtected
)
3620 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
3627 * From the VT102 manual:
3629 * Erasing a character also erases any character
3630 * attribute of the character.
3636 * Erase with the current color a.k.a. back-color erase
3639 line
.setChar(i
, ' ');
3640 line
.setAttr(i
, currentState
.attr
);
3648 * Erase a rectangular section of the screen, inclusive. end column,
3651 * @param startRow starting row to erase (between 0 and height - 1)
3652 * @param startCol starting column to erase (between 0 and width - 1)
3653 * @param endRow ending row to erase (between 0 and height - 1)
3654 * @param endCol ending column to erase (between 0 and width - 1)
3655 * @param honorProtected if true, do not erase characters with the
3656 * protected attribute set
3658 private void eraseScreen(final int startRow
, final int startCol
,
3659 final int endRow
, final int endCol
, final boolean honorProtected
) {
3667 || (endRow
< startRow
)
3668 || (endCol
< startCol
)
3673 oldCursorY
= currentState
.cursorY
;
3674 for (int i
= startRow
; i
<= endRow
; i
++) {
3675 currentState
.cursorY
= i
;
3676 eraseLine(startCol
, endCol
, honorProtected
);
3678 // Erase display clears the double attributes
3679 display
.get(i
).setDoubleWidth(false);
3680 display
.get(i
).setDoubleHeight(0);
3682 currentState
.cursorY
= oldCursorY
;
3686 * VT220 printer functions. All of these are parsed, but won't do
3689 private void printerFunctions() {
3690 boolean decPrivateModeFlag
= false;
3691 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3692 if (collectBuffer
.charAt(i
) == '?') {
3693 decPrivateModeFlag
= true;
3698 int i
= getCsiParam(0, 0);
3703 if (decPrivateModeFlag
== false) {
3709 if (decPrivateModeFlag
== true) {
3710 // Print cursor line
3715 if (decPrivateModeFlag
== true) {
3716 // Auto print mode OFF
3718 // Printer controller OFF
3720 // Characters re-appear on the screen
3721 printerControllerMode
= false;
3726 if (decPrivateModeFlag
== true) {
3730 // Printer controller
3732 // Characters get sucked into oblivion
3733 printerControllerMode
= true;
3744 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
3745 * of remote systems will send an XTerm title sequence even if TERM isn't
3748 * @param xtermChar the character received from the remote side
3750 private void oscPut(final char xtermChar
) {
3752 collectBuffer
.append(xtermChar
);
3755 if (xtermChar
== 0x07) {
3756 String args
= collectBuffer
.substring(0,
3757 collectBuffer
.length() - 1);
3758 String
[] p
= args
.toString().split(";");
3760 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
3768 // Go to SCAN_GROUND state
3775 * Run this input character through the ECMA48 state machine.
3777 * @param ch character from the remote side
3779 private void consume(char ch
) {
3782 // System.err.printf("%c", ch);
3784 // Special case for VT10x: 7-bit characters only
3785 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
3786 ch
= (char)(ch
& 0x7F);
3789 // Special "anywhere" states
3791 // 18, 1A --> execute, then switch to SCAN_GROUND
3792 if ((ch
== 0x18) || (ch
== 0x1A)) {
3793 // CAN and SUB abort escape sequences
3798 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
3802 && (scanState
!= ScanState
.DCS_ENTRY
)
3803 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
3804 && (scanState
!= ScanState
.DCS_IGNORE
)
3805 && (scanState
!= ScanState
.DCS_PARAM
)
3806 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
3809 scanState
= ScanState
.ESCAPE
;
3813 // 0x9B == CSI 8-bit sequence
3815 scanState
= ScanState
.CSI_ENTRY
;
3819 // 0x9D goes to ScanState.OSC_STRING
3821 scanState
= ScanState
.OSC_STRING
;
3825 // 0x90 goes to DCS_ENTRY
3827 scanState
= ScanState
.DCS_ENTRY
;
3831 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
3832 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
3833 scanState
= ScanState
.SOSPMAPC_STRING
;
3837 // 0x7F (DEL) is always discarded
3842 switch (scanState
) {
3845 // 00-17, 19, 1C-1F --> execute
3846 // 80-8F, 91-9A, 9C --> execute
3847 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
3848 handleControlChar(ch
);
3852 if (((ch
>= 0x20) && (ch
<= 0x7F))
3856 // VT220 printer --> trash bin
3857 if (((type
== DeviceType
.VT220
)
3858 || (type
== DeviceType
.XTERM
))
3859 && (printerControllerMode
== true)
3864 // Hang onto this character
3865 repCh
= mapCharacter(ch
);
3867 // Print this character
3868 printCharacter(repCh
);
3873 // 00-17, 19, 1C-1F --> execute
3875 handleControlChar(ch
);
3879 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
3880 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
3882 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
3886 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
3887 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
3898 // DECSC - Save cursor
3899 // Note this code overlaps both ANSI and VT52 mode
3904 // DECRC - Restore cursor
3905 // Note this code overlaps both ANSI and VT52 mode
3914 if (vt52Mode
== true) {
3915 // DECANM - Enter ANSI mode
3917 arrowKeyMode
= ArrowKeyMode
.VT100
;
3920 * From the VT102 docs: "You use ANSI mode to select
3921 * most terminal features; the terminal uses the same
3922 * features when it switches to VT52 mode. You
3923 * cannot, however, change most of these features in
3926 * In other words, do not reset any other attributes
3927 * when switching between VT52 submode and ANSI.
3931 currentState
.g0Charset
= CharacterSet
.US
;
3932 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3934 singleshift
= Singleshift
.NONE
;
3935 currentState
.glLockshift
= LockshiftMode
.NONE
;
3936 currentState
.grLockshift
= LockshiftMode
.NONE
;
3940 // DECKPAM - Keypad application mode
3941 // Note this code overlaps both ANSI and VT52 mode
3945 // DECKPNM - Keypad numeric mode
3946 // Note this code overlaps both ANSI and VT52 mode
3953 if (vt52Mode
== true) {
3954 // Cursor up, and stop at the top without scrolling
3959 if (vt52Mode
== true) {
3960 // Cursor down, and stop at the bottom without scrolling
3961 cursorDown(1, false);
3965 if (vt52Mode
== true) {
3966 // Cursor right, and stop at the right without scrolling
3967 cursorRight(1, false);
3971 if (vt52Mode
== true) {
3972 // Cursor left, and stop at the left without scrolling
3973 cursorLeft(1, false);
3980 if (vt52Mode
== true) {
3988 if (vt52Mode
== true) {
3989 // G0 --> Special graphics
3990 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
3994 if (vt52Mode
== true) {
3996 currentState
.g0Charset
= CharacterSet
.US
;
4000 if (vt52Mode
== true) {
4002 cursorPosition(0, 0);
4004 // HTS - Horizontal tabulation set
4009 if (vt52Mode
== true) {
4010 // Reverse line feed. Same as RI.
4015 if (vt52Mode
== true) {
4016 // Erase to end of screen
4017 eraseLine(currentState
.cursorX
, width
- 1, false);
4018 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4023 if (vt52Mode
== true) {
4024 // Erase to end of line
4025 eraseLine(currentState
.cursorX
, width
- 1, false);
4031 if (vt52Mode
== true) {
4034 // RI - Reverse index
4039 if (vt52Mode
== false) {
4041 singleshift
= Singleshift
.SS2
;
4045 if (vt52Mode
== false) {
4047 singleshift
= Singleshift
.SS3
;
4054 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4070 if (vt52Mode
== true) {
4071 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4079 if (vt52Mode
== true) {
4081 // Send string directly to remote side
4082 writeRemote("\033/Z");
4085 // Send string directly to remote side
4086 writeRemote(deviceTypeResponse());
4097 // VT52 cannot get to any of these other states
4098 if (vt52Mode
== true) {
4103 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4110 // RIS - Reset to initial state
4112 // Do I clear screen too? I think so...
4113 eraseScreen(0, 0, height
- 1, width
- 1, false);
4114 cursorPosition(0, 0);
4128 if ((type
== DeviceType
.VT220
)
4129 || (type
== DeviceType
.XTERM
)) {
4131 // VT220 lockshift G2 into GL
4132 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4137 if ((type
== DeviceType
.VT220
)
4138 || (type
== DeviceType
.XTERM
)) {
4140 // VT220 lockshift G3 into GL
4141 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4159 if ((type
== DeviceType
.VT220
)
4160 || (type
== DeviceType
.XTERM
)) {
4162 // VT220 lockshift G3 into GR
4163 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4168 if ((type
== DeviceType
.VT220
)
4169 || (type
== DeviceType
.XTERM
)) {
4171 // VT220 lockshift G2 into GR
4172 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4178 if ((type
== DeviceType
.VT220
)
4179 || (type
== DeviceType
.XTERM
)) {
4181 // VT220 lockshift G1 into GR
4182 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4192 // 0x5B goes to CSI_ENTRY
4194 scanState
= ScanState
.CSI_ENTRY
;
4197 // 0x5D goes to OSC_STRING
4199 scanState
= ScanState
.OSC_STRING
;
4202 // 0x50 goes to DCS_ENTRY
4204 scanState
= ScanState
.DCS_ENTRY
;
4207 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4208 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4209 scanState
= ScanState
.SOSPMAPC_STRING
;
4214 case ESCAPE_INTERMEDIATE
:
4215 // 00-17, 19, 1C-1F --> execute
4217 handleControlChar(ch
);
4220 // 20-2F --> collect
4221 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4225 // 30-7E --> dispatch, then switch to GROUND
4226 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4229 if ((collectBuffer
.length() == 1)
4230 && (collectBuffer
.charAt(0) == '(')) {
4231 // G0 --> Special graphics
4232 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4234 if ((collectBuffer
.length() == 1)
4235 && (collectBuffer
.charAt(0) == ')')) {
4236 // G1 --> Special graphics
4237 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4239 if ((type
== DeviceType
.VT220
)
4240 || (type
== DeviceType
.XTERM
)) {
4242 if ((collectBuffer
.length() == 1)
4243 && (collectBuffer
.charAt(0) == '*')) {
4244 // G2 --> Special graphics
4245 currentState
.g2Charset
= CharacterSet
.DRAWING
;
4247 if ((collectBuffer
.length() == 1)
4248 && (collectBuffer
.charAt(0) == '+')) {
4249 // G3 --> Special graphics
4250 currentState
.g3Charset
= CharacterSet
.DRAWING
;
4255 if ((collectBuffer
.length() == 1)
4256 && (collectBuffer
.charAt(0) == '(')) {
4257 // G0 --> Alternate character ROM standard character set
4258 currentState
.g0Charset
= CharacterSet
.ROM
;
4260 if ((collectBuffer
.length() == 1)
4261 && (collectBuffer
.charAt(0) == ')')) {
4262 // G1 --> Alternate character ROM standard character set
4263 currentState
.g1Charset
= CharacterSet
.ROM
;
4267 if ((collectBuffer
.length() == 1)
4268 && (collectBuffer
.charAt(0) == '(')) {
4269 // G0 --> Alternate character ROM special graphics
4270 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4272 if ((collectBuffer
.length() == 1)
4273 && (collectBuffer
.charAt(0) == ')')) {
4274 // G1 --> Alternate character ROM special graphics
4275 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4279 if ((collectBuffer
.length() == 1)
4280 && (collectBuffer
.charAt(0) == '#')) {
4281 // DECDHL - Double-height line (top half)
4286 if ((collectBuffer
.length() == 1)
4287 && (collectBuffer
.charAt(0) == '#')) {
4288 // DECDHL - Double-height line (bottom half)
4291 if ((type
== DeviceType
.VT220
)
4292 || (type
== DeviceType
.XTERM
)) {
4294 if ((collectBuffer
.length() == 1)
4295 && (collectBuffer
.charAt(0) == '(')) {
4297 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4299 if ((collectBuffer
.length() == 1)
4300 && (collectBuffer
.charAt(0) == ')')) {
4302 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4304 if ((collectBuffer
.length() == 1)
4305 && (collectBuffer
.charAt(0) == '*')) {
4307 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4309 if ((collectBuffer
.length() == 1)
4310 && (collectBuffer
.charAt(0) == '+')) {
4312 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4317 if ((collectBuffer
.length() == 1)
4318 && (collectBuffer
.charAt(0) == '#')) {
4319 // DECSWL - Single-width line
4322 if ((type
== DeviceType
.VT220
)
4323 || (type
== DeviceType
.XTERM
)) {
4325 if ((collectBuffer
.length() == 1)
4326 && (collectBuffer
.charAt(0) == '(')) {
4328 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4330 if ((collectBuffer
.length() == 1)
4331 && (collectBuffer
.charAt(0) == ')')) {
4333 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4335 if ((collectBuffer
.length() == 1)
4336 && (collectBuffer
.charAt(0) == '*')) {
4338 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4340 if ((collectBuffer
.length() == 1)
4341 && (collectBuffer
.charAt(0) == '+')) {
4343 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4348 if ((collectBuffer
.length() == 1)
4349 && (collectBuffer
.charAt(0) == '#')) {
4350 // DECDWL - Double-width line
4353 if ((type
== DeviceType
.VT220
)
4354 || (type
== DeviceType
.XTERM
)) {
4356 if ((collectBuffer
.length() == 1)
4357 && (collectBuffer
.charAt(0) == '(')) {
4359 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4361 if ((collectBuffer
.length() == 1)
4362 && (collectBuffer
.charAt(0) == ')')) {
4364 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4366 if ((collectBuffer
.length() == 1)
4367 && (collectBuffer
.charAt(0) == '*')) {
4369 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4371 if ((collectBuffer
.length() == 1)
4372 && (collectBuffer
.charAt(0) == '+')) {
4374 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4379 if ((type
== DeviceType
.VT220
)
4380 || (type
== DeviceType
.XTERM
)) {
4382 if ((collectBuffer
.length() == 1)
4383 && (collectBuffer
.charAt(0) == '(')) {
4385 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4387 if ((collectBuffer
.length() == 1)
4388 && (collectBuffer
.charAt(0) == ')')) {
4390 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4392 if ((collectBuffer
.length() == 1)
4393 && (collectBuffer
.charAt(0) == '*')) {
4395 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4397 if ((collectBuffer
.length() == 1)
4398 && (collectBuffer
.charAt(0) == '+')) {
4400 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4405 if ((collectBuffer
.length() == 1)
4406 && (collectBuffer
.charAt(0) == '#')) {
4407 // DECALN - Screen alignment display
4416 if ((type
== DeviceType
.VT220
)
4417 || (type
== DeviceType
.XTERM
)) {
4419 if ((collectBuffer
.length() == 1)
4420 && (collectBuffer
.charAt(0) == '(')) {
4421 // G0 --> DEC_SUPPLEMENTAL
4422 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4424 if ((collectBuffer
.length() == 1)
4425 && (collectBuffer
.charAt(0) == ')')) {
4426 // G1 --> DEC_SUPPLEMENTAL
4427 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4429 if ((collectBuffer
.length() == 1)
4430 && (collectBuffer
.charAt(0) == '*')) {
4431 // G2 --> DEC_SUPPLEMENTAL
4432 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4434 if ((collectBuffer
.length() == 1)
4435 && (collectBuffer
.charAt(0) == '+')) {
4436 // G3 --> DEC_SUPPLEMENTAL
4437 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4442 if ((type
== DeviceType
.VT220
)
4443 || (type
== DeviceType
.XTERM
)) {
4445 if ((collectBuffer
.length() == 1)
4446 && (collectBuffer
.charAt(0) == '(')) {
4448 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4450 if ((collectBuffer
.length() == 1)
4451 && (collectBuffer
.charAt(0) == ')')) {
4453 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4455 if ((collectBuffer
.length() == 1)
4456 && (collectBuffer
.charAt(0) == '*')) {
4458 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4460 if ((collectBuffer
.length() == 1)
4461 && (collectBuffer
.charAt(0) == '+')) {
4463 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4472 if ((collectBuffer
.length() == 1)
4473 && (collectBuffer
.charAt(0) == '(')) {
4474 // G0 --> United Kingdom set
4475 currentState
.g0Charset
= CharacterSet
.UK
;
4477 if ((collectBuffer
.length() == 1)
4478 && (collectBuffer
.charAt(0) == ')')) {
4479 // G1 --> United Kingdom set
4480 currentState
.g1Charset
= CharacterSet
.UK
;
4482 if ((type
== DeviceType
.VT220
)
4483 || (type
== DeviceType
.XTERM
)) {
4485 if ((collectBuffer
.length() == 1)
4486 && (collectBuffer
.charAt(0) == '*')) {
4487 // G2 --> United Kingdom set
4488 currentState
.g2Charset
= CharacterSet
.UK
;
4490 if ((collectBuffer
.length() == 1)
4491 && (collectBuffer
.charAt(0) == '+')) {
4492 // G3 --> United Kingdom set
4493 currentState
.g3Charset
= CharacterSet
.UK
;
4498 if ((collectBuffer
.length() == 1)
4499 && (collectBuffer
.charAt(0) == '(')) {
4501 currentState
.g0Charset
= CharacterSet
.US
;
4503 if ((collectBuffer
.length() == 1)
4504 && (collectBuffer
.charAt(0) == ')')) {
4506 currentState
.g1Charset
= CharacterSet
.US
;
4508 if ((type
== DeviceType
.VT220
)
4509 || (type
== DeviceType
.XTERM
)) {
4511 if ((collectBuffer
.length() == 1)
4512 && (collectBuffer
.charAt(0) == '*')) {
4514 currentState
.g2Charset
= CharacterSet
.US
;
4516 if ((collectBuffer
.length() == 1)
4517 && (collectBuffer
.charAt(0) == '+')) {
4519 currentState
.g3Charset
= CharacterSet
.US
;
4524 if ((type
== DeviceType
.VT220
)
4525 || (type
== DeviceType
.XTERM
)) {
4527 if ((collectBuffer
.length() == 1)
4528 && (collectBuffer
.charAt(0) == '(')) {
4530 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4532 if ((collectBuffer
.length() == 1)
4533 && (collectBuffer
.charAt(0) == ')')) {
4535 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4537 if ((collectBuffer
.length() == 1)
4538 && (collectBuffer
.charAt(0) == '*')) {
4540 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4542 if ((collectBuffer
.length() == 1)
4543 && (collectBuffer
.charAt(0) == '+')) {
4545 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4552 if ((type
== DeviceType
.VT220
)
4553 || (type
== DeviceType
.XTERM
)) {
4555 if ((collectBuffer
.length() == 1)
4556 && (collectBuffer
.charAt(0) == '(')) {
4558 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4560 if ((collectBuffer
.length() == 1)
4561 && (collectBuffer
.charAt(0) == ')')) {
4563 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4565 if ((collectBuffer
.length() == 1)
4566 && (collectBuffer
.charAt(0) == '*')) {
4568 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4570 if ((collectBuffer
.length() == 1)
4571 && (collectBuffer
.charAt(0) == '+')) {
4573 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4578 if ((type
== DeviceType
.VT220
)
4579 || (type
== DeviceType
.XTERM
)) {
4581 if ((collectBuffer
.length() == 1)
4582 && (collectBuffer
.charAt(0) == ' ')) {
4589 if ((type
== DeviceType
.VT220
)
4590 || (type
== DeviceType
.XTERM
)) {
4592 if ((collectBuffer
.length() == 1)
4593 && (collectBuffer
.charAt(0) == ' ')) {
4600 if ((type
== DeviceType
.VT220
)
4601 || (type
== DeviceType
.XTERM
)) {
4603 if ((collectBuffer
.length() == 1)
4604 && (collectBuffer
.charAt(0) == '(')) {
4606 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4608 if ((collectBuffer
.length() == 1)
4609 && (collectBuffer
.charAt(0) == ')')) {
4611 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4613 if ((collectBuffer
.length() == 1)
4614 && (collectBuffer
.charAt(0) == '*')) {
4616 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4618 if ((collectBuffer
.length() == 1)
4619 && (collectBuffer
.charAt(0) == '+')) {
4621 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4629 if ((type
== DeviceType
.VT220
)
4630 || (type
== DeviceType
.XTERM
)) {
4632 if ((collectBuffer
.length() == 1)
4633 && (collectBuffer
.charAt(0) == '(')) {
4635 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4637 if ((collectBuffer
.length() == 1)
4638 && (collectBuffer
.charAt(0) == ')')) {
4640 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4642 if ((collectBuffer
.length() == 1)
4643 && (collectBuffer
.charAt(0) == '*')) {
4645 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4647 if ((collectBuffer
.length() == 1)
4648 && (collectBuffer
.charAt(0) == '+')) {
4650 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4661 if ((type
== DeviceType
.VT220
)
4662 || (type
== DeviceType
.XTERM
)) {
4664 if ((collectBuffer
.length() == 1)
4665 && (collectBuffer
.charAt(0) == '(')) {
4667 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4669 if ((collectBuffer
.length() == 1)
4670 && (collectBuffer
.charAt(0) == ')')) {
4672 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4674 if ((collectBuffer
.length() == 1)
4675 && (collectBuffer
.charAt(0) == '*')) {
4677 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4679 if ((collectBuffer
.length() == 1)
4680 && (collectBuffer
.charAt(0) == '+')) {
4682 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4687 if ((type
== DeviceType
.VT220
)
4688 || (type
== DeviceType
.XTERM
)) {
4690 if ((collectBuffer
.length() == 1)
4691 && (collectBuffer
.charAt(0) == '(')) {
4693 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4695 if ((collectBuffer
.length() == 1)
4696 && (collectBuffer
.charAt(0) == ')')) {
4698 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4700 if ((collectBuffer
.length() == 1)
4701 && (collectBuffer
.charAt(0) == '*')) {
4703 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
4705 if ((collectBuffer
.length() == 1)
4706 && (collectBuffer
.charAt(0) == '+')) {
4708 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
4720 if ((type
== DeviceType
.VT220
)
4721 || (type
== DeviceType
.XTERM
)) {
4723 if ((collectBuffer
.length() == 1)
4724 && (collectBuffer
.charAt(0) == '(')) {
4726 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
4728 if ((collectBuffer
.length() == 1)
4729 && (collectBuffer
.charAt(0) == ')')) {
4731 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
4733 if ((collectBuffer
.length() == 1)
4734 && (collectBuffer
.charAt(0) == '*')) {
4736 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
4738 if ((collectBuffer
.length() == 1)
4739 && (collectBuffer
.charAt(0) == '+')) {
4741 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
4746 if ((type
== DeviceType
.VT220
)
4747 || (type
== DeviceType
.XTERM
)) {
4749 if ((collectBuffer
.length() == 1)
4750 && (collectBuffer
.charAt(0) == '(')) {
4752 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
4754 if ((collectBuffer
.length() == 1)
4755 && (collectBuffer
.charAt(0) == ')')) {
4757 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
4759 if ((collectBuffer
.length() == 1)
4760 && (collectBuffer
.charAt(0) == '*')) {
4762 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
4764 if ((collectBuffer
.length() == 1)
4765 && (collectBuffer
.charAt(0) == '+')) {
4767 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
4814 // 0x9C goes to GROUND
4822 // 00-17, 19, 1C-1F --> execute
4824 handleControlChar(ch
);
4827 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
4828 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4830 scanState
= ScanState
.CSI_INTERMEDIATE
;
4833 // 30-39, 3B --> param, then switch to CSI_PARAM
4834 if ((ch
>= '0') && (ch
<= '9')) {
4836 scanState
= ScanState
.CSI_PARAM
;
4840 scanState
= ScanState
.CSI_PARAM
;
4843 // 3C-3F --> collect, then switch to CSI_PARAM
4844 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
4846 scanState
= ScanState
.CSI_PARAM
;
4849 // 40-7E --> dispatch, then switch to GROUND
4850 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
4853 // ICH - Insert character
4861 // CUD - Cursor down
4865 // CUF - Cursor forward
4869 // CUB - Cursor backward
4873 // CNL - Cursor down and to column 1
4874 if (type
== DeviceType
.XTERM
) {
4879 // CPL - Cursor up and to column 1
4880 if (type
== DeviceType
.XTERM
) {
4885 // CHA - Cursor to column # in current row
4886 if (type
== DeviceType
.XTERM
) {
4891 // CUP - Cursor position
4895 // CHT - Cursor forward X tab stops (default 1)
4896 if (type
== DeviceType
.XTERM
) {
4901 // ED - Erase in display
4905 // EL - Erase in line
4920 // DCH - Delete character
4927 // Scroll up X lines (default 1)
4928 if (type
== DeviceType
.XTERM
) {
4933 // Scroll down X lines (default 1)
4934 if (type
== DeviceType
.XTERM
) {
4943 if ((type
== DeviceType
.VT220
)
4944 || (type
== DeviceType
.XTERM
)) {
4946 // ECH - Erase character
4953 // CBT - Cursor backward X tab stops (default 1)
4954 if (type
== DeviceType
.XTERM
) {
4965 // HPA - Cursor to column # in current row. Same as CHA
4966 if (type
== DeviceType
.XTERM
) {
4971 // HPR - Cursor right. Same as CUF
4972 if (type
== DeviceType
.XTERM
) {
4977 // REP - Repeat last char X times
4978 if (type
== DeviceType
.XTERM
) {
4983 // DA - Device attributes
4987 // VPA - Cursor to row, current column.
4988 if (type
== DeviceType
.XTERM
) {
4993 // VPR - Cursor down. Same as CUD
4994 if (type
== DeviceType
.XTERM
) {
4999 // HVP - Horizontal and vertical position
5003 // TBC - Tabulation clear
5007 // Sets an ANSI or DEC private toggle
5011 if ((type
== DeviceType
.VT220
)
5012 || (type
== DeviceType
.XTERM
)) {
5014 // Printer functions
5022 // Sets an ANSI or DEC private toggle
5026 // SGR - Select graphics rendition
5030 // DSR - Device status report
5037 // DECLL - Load leds
5041 // DECSTBM - Set top and bottom margins
5045 // Save cursor (ANSI.SYS)
5046 if (type
== DeviceType
.XTERM
) {
5047 savedState
.cursorX
= currentState
.cursorX
;
5048 savedState
.cursorY
= currentState
.cursorY
;
5054 // Restore cursor (ANSI.SYS)
5055 if (type
== DeviceType
.XTERM
) {
5056 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5063 // DECREQTPARM - Request terminal parameters
5079 // 0x9C goes to GROUND
5084 // 0x3A goes to CSI_IGNORE
5086 scanState
= ScanState
.CSI_IGNORE
;
5091 // 00-17, 19, 1C-1F --> execute
5093 handleControlChar(ch
);
5096 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5097 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5099 scanState
= ScanState
.CSI_INTERMEDIATE
;
5102 // 30-39, 3B --> param
5103 if ((ch
>= '0') && (ch
<= '9')) {
5110 // 0x3A goes to CSI_IGNORE
5112 scanState
= ScanState
.CSI_IGNORE
;
5114 // 0x3C-3F goes to CSI_IGNORE
5115 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5116 scanState
= ScanState
.CSI_IGNORE
;
5119 // 40-7E --> dispatch, then switch to GROUND
5120 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5123 // ICH - Insert character
5131 // CUD - Cursor down
5135 // CUF - Cursor forward
5139 // CUB - Cursor backward
5143 // CNL - Cursor down and to column 1
5144 if (type
== DeviceType
.XTERM
) {
5149 // CPL - Cursor up and to column 1
5150 if (type
== DeviceType
.XTERM
) {
5155 // CHA - Cursor to column # in current row
5156 if (type
== DeviceType
.XTERM
) {
5161 // CUP - Cursor position
5165 // CHT - Cursor forward X tab stops (default 1)
5166 if (type
== DeviceType
.XTERM
) {
5171 // ED - Erase in display
5175 // EL - Erase in line
5190 // DCH - Delete character
5197 // Scroll up X lines (default 1)
5198 if (type
== DeviceType
.XTERM
) {
5203 // Scroll down X lines (default 1)
5204 if (type
== DeviceType
.XTERM
) {
5213 if ((type
== DeviceType
.VT220
)
5214 || (type
== DeviceType
.XTERM
)) {
5216 // ECH - Erase character
5223 // CBT - Cursor backward X tab stops (default 1)
5224 if (type
== DeviceType
.XTERM
) {
5235 // HPA - Cursor to column # in current row. Same as CHA
5236 if (type
== DeviceType
.XTERM
) {
5241 // HPR - Cursor right. Same as CUF
5242 if (type
== DeviceType
.XTERM
) {
5247 // REP - Repeat last char X times
5248 if (type
== DeviceType
.XTERM
) {
5253 // DA - Device attributes
5257 // VPA - Cursor to row, current column.
5258 if (type
== DeviceType
.XTERM
) {
5263 // VPR - Cursor down. Same as CUD
5264 if (type
== DeviceType
.XTERM
) {
5269 // HVP - Horizontal and vertical position
5273 // TBC - Tabulation clear
5277 // Sets an ANSI or DEC private toggle
5281 if ((type
== DeviceType
.VT220
)
5282 || (type
== DeviceType
.XTERM
)) {
5284 // Printer functions
5292 // Sets an ANSI or DEC private toggle
5296 // SGR - Select graphics rendition
5300 // DSR - Device status report
5307 // DECLL - Load leds
5311 // DECSTBM - Set top and bottom margins
5321 // DECREQTPARM - Request terminal parameters
5338 case CSI_INTERMEDIATE
:
5339 // 00-17, 19, 1C-1F --> execute
5341 handleControlChar(ch
);
5344 // 20-2F --> collect
5345 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5349 // 0x30-3F goes to CSI_IGNORE
5350 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5351 scanState
= ScanState
.CSI_IGNORE
;
5354 // 40-7E --> dispatch, then switch to GROUND
5355 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5407 if (((type
== DeviceType
.VT220
)
5408 || (type
== DeviceType
.XTERM
))
5409 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5411 // DECSCL - compatibility level
5414 if ((type
== DeviceType
.XTERM
)
5415 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
5417 // DECSTR - Soft terminal reset
5422 if (((type
== DeviceType
.VT220
)
5423 || (type
== DeviceType
.XTERM
))
5424 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5452 // 00-17, 19, 1C-1F --> execute
5454 handleControlChar(ch
);
5457 // 20-2F --> collect
5458 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5462 // 40-7E --> ignore, then switch to GROUND
5463 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5467 // 20-3F, 7F --> ignore
5473 // 0x9C goes to GROUND
5478 // 0x1B 0x5C goes to GROUND
5483 if ((collectBuffer
.length() > 0)
5484 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5490 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5491 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5493 scanState
= ScanState
.DCS_INTERMEDIATE
;
5496 // 30-39, 3B --> param, then switch to DCS_PARAM
5497 if ((ch
>= '0') && (ch
<= '9')) {
5499 scanState
= ScanState
.DCS_PARAM
;
5503 scanState
= ScanState
.DCS_PARAM
;
5506 // 3C-3F --> collect, then switch to DCS_PARAM
5507 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5509 scanState
= ScanState
.DCS_PARAM
;
5512 // 00-17, 19, 1C-1F, 7F --> ignore
5514 // 0x3A goes to DCS_IGNORE
5516 scanState
= ScanState
.DCS_IGNORE
;
5519 // 0x40-7E goes to DCS_PASSTHROUGH
5520 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5521 scanState
= ScanState
.DCS_PASSTHROUGH
;
5525 case DCS_INTERMEDIATE
:
5527 // 0x9C goes to GROUND
5532 // 0x1B 0x5C goes to GROUND
5537 if ((collectBuffer
.length() > 0) &&
5538 (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5544 // 0x30-3F goes to DCS_IGNORE
5545 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5546 scanState
= ScanState
.DCS_IGNORE
;
5549 // 0x40-7E goes to DCS_PASSTHROUGH
5550 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5551 scanState
= ScanState
.DCS_PASSTHROUGH
;
5554 // 00-17, 19, 1C-1F, 7F --> ignore
5559 // 0x9C goes to GROUND
5564 // 0x1B 0x5C goes to GROUND
5569 if ((collectBuffer
.length() > 0) &&
5570 (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5576 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5577 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5579 scanState
= ScanState
.DCS_INTERMEDIATE
;
5582 // 30-39, 3B --> param
5583 if ((ch
>= '0') && (ch
<= '9')) {
5590 // 00-17, 19, 1C-1F, 7F --> ignore
5592 // 0x3A, 3C-3F goes to DCS_IGNORE
5594 scanState
= ScanState
.DCS_IGNORE
;
5596 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5597 scanState
= ScanState
.DCS_IGNORE
;
5600 // 0x40-7E goes to DCS_PASSTHROUGH
5601 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5602 scanState
= ScanState
.DCS_PASSTHROUGH
;
5606 case DCS_PASSTHROUGH
:
5607 // 0x9C goes to GROUND
5612 // 0x1B 0x5C goes to GROUND
5617 if ((collectBuffer
.length() > 0)
5618 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5624 // 00-17, 19, 1C-1F, 20-7E --> put
5632 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5635 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5644 // 00-17, 19, 1C-1F, 20-7F --> ignore
5646 // 0x9C goes to GROUND
5653 case SOSPMAPC_STRING
:
5654 // 00-17, 19, 1C-1F, 20-7F --> ignore
5656 // 0x9C goes to GROUND
5664 // Special case for Xterm: OSC can pass control characters
5665 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5669 // 00-17, 19, 1C-1F --> ignore
5671 // 20-7F --> osc_put
5672 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5676 // 0x9C goes to GROUND
5683 case VT52_DIRECT_CURSOR_ADDRESS
:
5684 // This is a special case for the VT52 sequence "ESC Y l c"
5685 if (collectBuffer
.length() == 0) {
5687 } else if (collectBuffer
.length() == 1) {
5688 // We've got the two characters, one in the buffer and the
5690 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
5699 * Expose current cursor X to outside world.
5701 * @return current cursor X
5703 public final int getCursorX() {
5704 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
5705 return currentState
.cursorX
* 2;
5707 return currentState
.cursorX
;
5711 * Expose current cursor Y to outside world.
5713 * @return current cursor Y
5715 public final int getCursorY() {
5716 return currentState
.cursorY
;
5720 * Read function runs on a separate thread.
5723 boolean utf8
= false;
5724 boolean done
= false;
5726 if (type
== DeviceType
.XTERM
) {
5730 // available() will often return > 1, so we need to read in chunks to
5732 char [] readBufferUTF8
= null;
5733 byte [] readBuffer
= null;
5735 readBufferUTF8
= new char[128];
5737 readBuffer
= new byte[128];
5740 while (!done
&& !stopReaderThread
) {
5742 int n
= inputStream
.available();
5743 // System.err.printf("available() %d\n", n); System.err.flush();
5745 if (readBufferUTF8
.length
< n
) {
5746 // The buffer wasn't big enough, make it huger
5747 int newSizeHalf
= Math
.max(readBufferUTF8
.length
, n
);
5749 readBufferUTF8
= new char[newSizeHalf
* 2];
5752 if (readBuffer
.length
< n
) {
5753 // The buffer wasn't big enough, make it huger
5754 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
5755 readBuffer
= new byte[newSizeHalf
* 2];
5761 rc
= input
.read(readBufferUTF8
, 0,
5762 readBufferUTF8
.length
);
5764 rc
= inputStream
.read(readBuffer
, 0,
5767 // System.err.printf("read() %d\n", rc); System.err.flush();
5772 for (int i
= 0; i
< rc
; i
++) {
5775 ch
= readBufferUTF8
[i
];
5779 // Don't step on UI events
5780 synchronized (this) {
5785 // System.err.println("end while loop"); System.err.flush();
5786 } catch (IOException e
) {
5787 e
.printStackTrace();
5790 } // while ((done == false) && (stopReaderThread == false))
5792 // Let the rest of the world know that I am done.
5793 stopReaderThread
= true;
5795 // System.err.println("*** run() exiting..."); System.err.flush();