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 "LANG=en_US", "LANG=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
{
529 * Which mouse encoding is active.
531 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
534 * Physical display width. We start at 80x24, but the user can resize us
540 * Get the display width.
542 * @return the width (usually 80 or 132)
544 public final int getWidth() {
549 * Physical display height. We start at 80x24, but the user can resize
555 * Get the display height.
557 * @return the height (usually 24)
559 public final int getHeight() {
564 * Top margin of the scrolling region.
566 private int scrollRegionTop
;
569 * Bottom margin of the scrolling region.
571 private int scrollRegionBottom
;
574 * Right margin column number. This can be selected by the remote side
575 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
577 private int rightMargin
;
580 * Last character printed.
585 * VT100-style line wrapping: a character is placed in column 80 (or
586 * 132), but the line does NOT wrap until another character is written to
587 * column 1 of the next line, after which the cursor moves to column 2.
589 private boolean wrapLineFlag
;
592 * VT220 single shift flag.
594 private Singleshift singleshift
= Singleshift
.NONE
;
597 * true = insert characters, false = overwrite.
599 private boolean insertMode
= false;
602 * VT52 mode as selected by DECANM. True means VT52, false means
603 * ANSI. Default is ANSI.
605 private boolean vt52Mode
= false;
608 * Visible cursor (DECTCEM).
610 private boolean cursorVisible
= true;
613 * Get visible cursor flag.
615 * @return if true, the cursor is visible
617 public final boolean visibleCursor() {
618 return cursorVisible
;
622 * Screen title as set by the xterm OSC sequence. Lots of applications
623 * send a screenTitle regardless of whether it is an xterm client or not.
625 private String screenTitle
= "";
628 * Get the screen title as set by the xterm OSC sequence. Lots of
629 * applications send a screenTitle regardless of whether it is an xterm
632 * @return screen title
634 public final String
getScreenTitle() {
639 * Parameter characters being collected.
641 private List
<Integer
> csiParams
;
644 * Non-csi collect buffer.
646 private StringBuilder collectBuffer
;
649 * When true, use the G1 character set.
651 private boolean shiftOut
= false;
654 * Horizontal tab stop locations.
656 private List
<Integer
> tabStops
;
659 * S8C1T. True means 8bit controls, false means 7bit controls.
661 private boolean s8c1t
= false;
664 * Printer mode. True means send all output to printer, which discards
667 private boolean printerControllerMode
= false;
670 * LMN line mode. If true, linefeed() puts the cursor on the first
671 * column of the next line. If false, linefeed() puts the cursor one
672 * line down on the current line. The default is false.
674 private boolean newLineMode
= false;
677 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
679 private ArrowKeyMode arrowKeyMode
;
682 * Whether number pad keys send VT100 or VT52, application or numeric
685 private KeypadMode keypadMode
;
688 * When true, the terminal is in 132-column mode (DECCOLM).
690 private boolean columns132
= false;
693 * true = reverse video. Set by DECSCNM.
695 private boolean reverseVideo
= false;
698 * false = echo characters locally.
700 private boolean fullDuplex
= true;
703 * DECSC/DECRC save/restore a subset of the total state. This class
704 * encapsulates those specific flags/modes.
706 private class SaveableState
{
709 * When true, cursor positions are relative to the scrolling region.
711 public boolean originMode
= false;
714 * The current editing X position.
716 public int cursorX
= 0;
719 * The current editing Y position.
721 public int cursorY
= 0;
724 * Which character set is currently selected in G0.
726 public CharacterSet g0Charset
= CharacterSet
.US
;
729 * Which character set is currently selected in G1.
731 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
734 * Which character set is currently selected in G2.
736 public CharacterSet g2Charset
= CharacterSet
.US
;
739 * Which character set is currently selected in G3.
741 public CharacterSet g3Charset
= CharacterSet
.US
;
744 * Which character set is currently selected in GR.
746 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
749 * The current drawing attributes.
751 public CellAttributes attr
;
756 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
761 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
766 public boolean lineWrap
= true;
771 public void reset() {
775 g0Charset
= CharacterSet
.US
;
776 g1Charset
= CharacterSet
.DRAWING
;
777 g2Charset
= CharacterSet
.US
;
778 g3Charset
= CharacterSet
.US
;
779 grCharset
= CharacterSet
.DRAWING
;
780 attr
= new CellAttributes();
781 glLockshift
= LockshiftMode
.NONE
;
782 grLockshift
= LockshiftMode
.NONE
;
787 * Copy attributes from another instance.
789 * @param that the other instance to match
791 public void setTo(final SaveableState that
) {
792 this.originMode
= that
.originMode
;
793 this.cursorX
= that
.cursorX
;
794 this.cursorY
= that
.cursorY
;
795 this.g0Charset
= that
.g0Charset
;
796 this.g1Charset
= that
.g1Charset
;
797 this.g2Charset
= that
.g2Charset
;
798 this.g3Charset
= that
.g3Charset
;
799 this.grCharset
= that
.grCharset
;
800 this.attr
= new CellAttributes();
801 this.attr
.setTo(that
.attr
);
802 this.glLockshift
= that
.glLockshift
;
803 this.grLockshift
= that
.grLockshift
;
804 this.lineWrap
= that
.lineWrap
;
808 * Public constructor.
810 public SaveableState() {
816 * The current terminal state.
818 private SaveableState currentState
;
821 * The last saved terminal state.
823 private SaveableState savedState
;
826 * Clear the CSI parameters and flags.
828 private void toGround() {
830 collectBuffer
= new StringBuilder(8);
831 scanState
= ScanState
.GROUND
;
835 * Reset the tab stops list.
837 private void resetTabStops() {
839 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
840 tabStops
.add(new Integer(i
* 8));
845 * Reset the emulation state.
847 private void reset() {
849 currentState
= new SaveableState();
850 savedState
= new SaveableState();
851 scanState
= ScanState
.GROUND
;
855 scrollRegionBottom
= height
- 1;
856 rightMargin
= width
- 1;
858 arrowKeyMode
= ArrowKeyMode
.ANSI
;
859 keypadMode
= KeypadMode
.Numeric
;
860 wrapLineFlag
= false;
868 reverseVideo
= false;
870 cursorVisible
= true;
873 singleshift
= Singleshift
.NONE
;
875 printerControllerMode
= false;
878 mouseProtocol
= MouseProtocol
.OFF
;
879 mouseEncoding
= MouseEncoding
.X10
;
889 * Public constructor.
891 * @param type one of the DeviceType constants to select VT100, VT102,
893 * @param inputStream an InputStream connected to the remote side. For
894 * type == XTERM, inputStrem is converted to a Reader with UTF-8
896 * @param outputStream an OutputStream connected to the remote user. For
897 * type == XTERM, outputStream is converted to a Writer with UTF-8
899 * @throws UnsupportedEncodingException if an exception is thrown when
900 * creating the InputStreamReader
902 public ECMA48(final DeviceType type
, final InputStream inputStream
,
903 final OutputStream outputStream
) throws UnsupportedEncodingException
{
905 assert (inputStream
!= null);
906 assert (outputStream
!= null);
908 csiParams
= new ArrayList
<Integer
>();
909 tabStops
= new ArrayList
<Integer
>();
910 scrollback
= new LinkedList
<DisplayLine
>();
911 display
= new LinkedList
<DisplayLine
>();
914 this.inputStream
= inputStream
;
915 if (type
== DeviceType
.XTERM
) {
916 this.input
= new InputStreamReader(inputStream
, "UTF-8");
917 this.output
= new OutputStreamWriter(outputStream
, "UTF-8");
918 this.outputStream
= null;
921 this.outputStream
= outputStream
;
925 for (int i
= 0; i
< height
; i
++) {
926 display
.add(new DisplayLine(currentState
.attr
));
929 // Spin up the input reader
930 readerThread
= new Thread(this);
931 readerThread
.start();
935 * Append a new line to the bottom of the display, adding lines off the
936 * top to the scrollback buffer.
938 private void newDisplayLine() {
939 // Scroll the top line off into the scrollback buffer
940 scrollback
.add(display
.get(0));
942 DisplayLine line
= new DisplayLine(currentState
.attr
);
943 line
.setReverseColor(reverseVideo
);
948 * Wraps the current line.
950 private void wrapCurrentLine() {
951 if (currentState
.cursorY
== height
- 1) {
954 if (currentState
.cursorY
< height
- 1) {
955 currentState
.cursorY
++;
957 currentState
.cursorX
= 0;
961 * Handle a carriage return.
963 private void carriageReturn() {
964 currentState
.cursorX
= 0;
965 wrapLineFlag
= false;
969 * Reverse the color of the visible display.
971 private void invertDisplayColors() {
972 for (DisplayLine line
: display
) {
973 line
.setReverseColor(!line
.isReverseColor());
980 private void linefeed() {
983 if (currentState
.cursorY
< scrollRegionBottom
) {
984 // Increment screen y
985 currentState
.cursorY
++;
988 // Screen y does not increment
991 * Two cases: either we're inside a scrolling region or not. If
992 * the scrolling region bottom is the bottom of the screen, then
993 * push the top line into the buffer. Else scroll the scrolling
996 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
998 // We're at the bottom of the scroll region, AND the scroll
999 // region is the entire screen.
1005 // We're at the bottom of the scroll region, AND the scroll
1006 // region is NOT the entire screen.
1007 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1012 currentState
.cursorX
= 0;
1014 wrapLineFlag
= false;
1018 * Prints one character to the display buffer.
1020 * @param ch character to display
1022 private void printCharacter(final char ch
) {
1023 int rightMargin
= this.rightMargin
;
1025 // Check if we have double-width, and if so chop at 40/66 instead of
1027 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1028 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1031 // Check the unusually-complicated line wrapping conditions...
1032 if (currentState
.cursorX
== rightMargin
) {
1034 if (currentState
.lineWrap
== true) {
1036 * This case happens when: the cursor was already on the
1037 * right margin (either through printing or by an explicit
1038 * placement command), and a character was printed.
1040 * The line wraps only when a new character arrives AND the
1041 * cursor is already on the right margin AND has placed a
1042 * character in its cell. Easier to see than to explain.
1044 if (wrapLineFlag
== false) {
1046 * This block marks the case that we are in the margin
1047 * and the first character has been received and printed.
1049 wrapLineFlag
= true;
1052 * This block marks the case that we are in the margin
1053 * and the second character has been received and
1056 wrapLineFlag
= false;
1060 } else if (currentState
.cursorX
<= rightMargin
) {
1062 * This is the normal case: a character came in and was printed
1063 * to the left of the right margin column.
1066 // Turn off VT100 special-case flag
1067 wrapLineFlag
= false;
1070 // "Print" the character
1071 Cell newCell
= new Cell(ch
);
1072 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1073 newCellAttributes
.setTo(currentState
.attr
);
1074 DisplayLine line
= display
.get(currentState
.cursorY
);
1075 // Insert mode special case
1076 if (insertMode
== true) {
1077 line
.insert(currentState
.cursorX
, newCell
);
1079 // Replace an existing character
1080 line
.replace(currentState
.cursorX
, newCell
);
1083 // Increment horizontal
1084 if (wrapLineFlag
== false) {
1085 currentState
.cursorX
++;
1086 if (currentState
.cursorX
> rightMargin
) {
1087 currentState
.cursorX
--;
1093 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1094 * send to the remote side.
1096 * @param mouse mouse event received from the local user
1098 public void mouse(final TMouseEvent mouse
) {
1101 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1102 mouseProtocol, mouseEncoding, mouse);
1105 if (mouseEncoding
!= MouseEncoding
.UTF8
) {
1106 // We will support X10 but only for (160,94) and smaller.
1107 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1112 switch (mouseProtocol
) {
1119 // Only report button presses
1120 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1126 // Only report button presses and releases
1127 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1128 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1136 * Only report button presses, button releases, and motions that
1137 * have a button down (i.e. drag-and-drop).
1139 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1140 if (!mouse
.getMouse1()
1141 && !mouse
.getMouse2()
1142 && !mouse
.getMouse3()
1143 && !mouse
.getMouseWheelUp()
1144 && !mouse
.getMouseWheelDown()
1152 // Report everything
1156 // Now encode the event
1157 StringBuilder sb
= new StringBuilder(6);
1158 sb
.append((char) 0x1B);
1161 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1162 sb
.append((char) (0x03 + 32));
1163 } else if (mouse
.getMouse1()) {
1164 sb
.append((char) (0x00 + 32));
1165 } else if (mouse
.getMouse2()) {
1166 sb
.append((char) (0x01 + 32));
1167 } else if (mouse
.getMouse3()) {
1168 sb
.append((char) (0x02 + 32));
1169 } else if (mouse
.getMouseWheelUp()) {
1170 sb
.append((char) (0x04 + 64));
1171 } else if (mouse
.getMouseWheelDown()) {
1172 sb
.append((char) (0x05 + 64));
1174 sb
.append((char) (0x03 + 32));
1177 sb
.append((char) (mouse
.getX() + 33));
1178 sb
.append((char) (mouse
.getY() + 33));
1180 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1181 writeRemote(sb
.toString());
1185 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1186 * send to the remote side.
1188 * @param keypress keypress received from the local user
1190 public void keypress(final TKeypress keypress
) {
1191 writeRemote(keypressToString(keypress
));
1195 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1197 * @param keypress keypress received from the local user
1198 * @return string to transmit to the remote side
1200 private String
keypressToString(final TKeypress keypress
) {
1202 if ((fullDuplex
== false) && (!keypress
.getIsKey())) {
1204 * If this is a control character, process it like it came from
1207 if (keypress
.getCh() < 0x20) {
1208 handleControlChar(keypress
.getCh());
1210 // Local echo for everything else
1211 printCharacter(keypress
.getCh());
1215 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1220 // Handle control characters
1221 if ((keypress
.getCtrl()) && (!keypress
.getIsKey())) {
1222 StringBuilder sb
= new StringBuilder();
1223 char ch
= keypress
.getCh();
1226 return sb
.toString();
1229 // Handle alt characters
1230 if ((keypress
.getAlt()) && (!keypress
.getIsKey())) {
1231 StringBuilder sb
= new StringBuilder("\033");
1232 char ch
= keypress
.getCh();
1234 return sb
.toString();
1237 if (keypress
.equals(kbBackspace
)) {
1250 if (keypress
.equals(kbLeft
)) {
1251 switch (arrowKeyMode
) {
1261 if (keypress
.equals(kbRight
)) {
1262 switch (arrowKeyMode
) {
1272 if (keypress
.equals(kbUp
)) {
1273 switch (arrowKeyMode
) {
1283 if (keypress
.equals(kbDown
)) {
1284 switch (arrowKeyMode
) {
1294 if (keypress
.equals(kbHome
)) {
1295 switch (arrowKeyMode
) {
1305 if (keypress
.equals(kbEnd
)) {
1306 switch (arrowKeyMode
) {
1316 if (keypress
.equals(kbF1
)) {
1324 if (keypress
.equals(kbF2
)) {
1332 if (keypress
.equals(kbF3
)) {
1340 if (keypress
.equals(kbF4
)) {
1348 if (keypress
.equals(kbF5
)) {
1361 if (keypress
.equals(kbF6
)) {
1374 if (keypress
.equals(kbF7
)) {
1387 if (keypress
.equals(kbF8
)) {
1400 if (keypress
.equals(kbF9
)) {
1413 if (keypress
.equals(kbF10
)) {
1426 if (keypress
.equals(kbF11
)) {
1430 if (keypress
.equals(kbF12
)) {
1434 if (keypress
.equals(kbShiftF1
)) {
1442 if (keypress
.equals(kbShiftF2
)) {
1450 if (keypress
.equals(kbShiftF3
)) {
1458 if (keypress
.equals(kbShiftF4
)) {
1466 if (keypress
.equals(kbShiftF5
)) {
1468 return "\033[15;2~";
1471 if (keypress
.equals(kbShiftF6
)) {
1473 return "\033[17;2~";
1476 if (keypress
.equals(kbShiftF7
)) {
1478 return "\033[18;2~";
1481 if (keypress
.equals(kbShiftF8
)) {
1483 return "\033[19;2~";
1486 if (keypress
.equals(kbShiftF9
)) {
1488 return "\033[20;2~";
1491 if (keypress
.equals(kbShiftF10
)) {
1493 return "\033[21;2~";
1496 if (keypress
.equals(kbShiftF11
)) {
1498 return "\033[23;2~";
1501 if (keypress
.equals(kbShiftF12
)) {
1503 return "\033[24;2~";
1506 if (keypress
.equals(kbCtrlF1
)) {
1514 if (keypress
.equals(kbCtrlF2
)) {
1522 if (keypress
.equals(kbCtrlF3
)) {
1530 if (keypress
.equals(kbCtrlF4
)) {
1538 if (keypress
.equals(kbCtrlF5
)) {
1540 return "\033[15;5~";
1543 if (keypress
.equals(kbCtrlF6
)) {
1545 return "\033[17;5~";
1548 if (keypress
.equals(kbCtrlF7
)) {
1550 return "\033[18;5~";
1553 if (keypress
.equals(kbCtrlF8
)) {
1555 return "\033[19;5~";
1558 if (keypress
.equals(kbCtrlF9
)) {
1560 return "\033[20;5~";
1563 if (keypress
.equals(kbCtrlF10
)) {
1565 return "\033[21;5~";
1568 if (keypress
.equals(kbCtrlF11
)) {
1570 return "\033[23;5~";
1573 if (keypress
.equals(kbCtrlF12
)) {
1575 return "\033[24;5~";
1578 if (keypress
.equals(kbPgUp
)) {
1583 if (keypress
.equals(kbPgDn
)) {
1588 if (keypress
.equals(kbIns
)) {
1593 if (keypress
.equals(kbShiftIns
)) {
1594 // This is what xterm sends for SHIFT-INS
1596 // This is what xterm sends for CTRL-INS
1597 // return "\033[2;5~";
1600 if (keypress
.equals(kbShiftDel
)) {
1601 // This is what xterm sends for SHIFT-DEL
1603 // This is what xterm sends for CTRL-DEL
1604 // return "\033[3;5~";
1607 if (keypress
.equals(kbDel
)) {
1608 // Delete sends real delete for VTxxx
1610 // return "\033[3~";
1613 if (keypress
.equals(kbEnter
)) {
1617 if (keypress
.equals(kbEsc
)) {
1621 if (keypress
.equals(kbAltEsc
)) {
1625 if (keypress
.equals(kbTab
)) {
1629 // Non-alt, non-ctrl characters
1630 if (!keypress
.getIsKey()) {
1631 StringBuilder sb
= new StringBuilder();
1632 sb
.append(keypress
.getCh());
1633 return sb
.toString();
1639 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1642 * @param ch 8-bit character from the remote side
1643 * @param charsetGl character set defined for GL
1644 * @param charsetGr character set defined for GR
1645 * @return character to display on the screen
1647 private char mapCharacterCharset(final char ch
,
1648 final CharacterSet charsetGl
,
1649 final CharacterSet charsetGr
) {
1651 int lookupChar
= ch
;
1652 CharacterSet lookupCharset
= charsetGl
;
1655 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
1656 lookupCharset
= charsetGr
;
1660 switch (lookupCharset
) {
1663 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
1666 return DECCharacterSets
.UK
[lookupChar
];
1669 return DECCharacterSets
.US_ASCII
[lookupChar
];
1672 return DECCharacterSets
.NL
[lookupChar
];
1675 return DECCharacterSets
.FI
[lookupChar
];
1678 return DECCharacterSets
.FR
[lookupChar
];
1681 return DECCharacterSets
.FR_CA
[lookupChar
];
1684 return DECCharacterSets
.DE
[lookupChar
];
1687 return DECCharacterSets
.IT
[lookupChar
];
1690 return DECCharacterSets
.NO
[lookupChar
];
1693 return DECCharacterSets
.ES
[lookupChar
];
1696 return DECCharacterSets
.SV
[lookupChar
];
1699 return DECCharacterSets
.SWISS
[lookupChar
];
1701 case DEC_SUPPLEMENTAL
:
1702 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
1705 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
1708 return DECCharacterSets
.US_ASCII
[lookupChar
];
1711 return DECCharacterSets
.US_ASCII
[lookupChar
];
1714 throw new IllegalArgumentException("Invalid character set value: "
1720 * Map an 8-bit byte into a printable character.
1722 * @param ch either 8-bit or Unicode character from the remote side
1723 * @return character to display on the screen
1725 private char mapCharacter(final char ch
) {
1727 // Unicode character, just return it
1731 CharacterSet charsetGl
= currentState
.g0Charset
;
1732 CharacterSet charsetGr
= currentState
.grCharset
;
1734 if (vt52Mode
== true) {
1735 if (shiftOut
== true) {
1736 // Shifted out character, pull from VT52 graphics
1737 charsetGl
= currentState
.g1Charset
;
1738 charsetGr
= CharacterSet
.US
;
1741 charsetGl
= currentState
.g0Charset
;
1742 charsetGr
= CharacterSet
.US
;
1745 // Pull the character
1746 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1750 if (shiftOut
== true) {
1751 // Shifted out character, pull from G1
1752 charsetGl
= currentState
.g1Charset
;
1753 charsetGr
= currentState
.grCharset
;
1755 // Pull the character
1756 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1760 if (singleshift
== Singleshift
.SS2
) {
1762 singleshift
= Singleshift
.NONE
;
1764 // Shifted out character, pull from G2
1765 charsetGl
= currentState
.g2Charset
;
1766 charsetGr
= currentState
.grCharset
;
1770 if (singleshift
== Singleshift
.SS3
) {
1772 singleshift
= Singleshift
.NONE
;
1774 // Shifted out character, pull from G3
1775 charsetGl
= currentState
.g3Charset
;
1776 charsetGr
= currentState
.grCharset
;
1779 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
1780 // Check for locking shift
1782 switch (currentState
.glLockshift
) {
1795 charsetGl
= currentState
.g2Charset
;
1800 charsetGl
= currentState
.g3Charset
;
1805 charsetGl
= currentState
.g0Charset
;
1809 switch (currentState
.grLockshift
) {
1819 charsetGr
= currentState
.g1Charset
;
1824 charsetGr
= currentState
.g2Charset
;
1829 charsetGr
= currentState
.g3Charset
;
1834 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
1841 // Pull the character
1842 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1846 * Scroll the text within a scrolling region up n lines.
1848 * @param regionTop top row of the scrolling region
1849 * @param regionBottom bottom row of the scrolling region
1850 * @param n number of lines to scroll
1852 private void scrollingRegionScrollUp(final int regionTop
,
1853 final int regionBottom
, final int n
) {
1855 if (regionTop
>= regionBottom
) {
1859 // Sanity check: see if there will be any characters left after the
1861 if (regionBottom
+ 1 - regionTop
<= n
) {
1862 // There won't be anything left in the region, so just call
1863 // eraseScreen() and return.
1864 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1868 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1869 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1870 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1872 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
1873 - remaining
, regionBottom
+ 1);
1874 display
= new LinkedList
<DisplayLine
>(displayTop
);
1875 display
.addAll(displayMiddle
);
1876 for (int i
= 0; i
< n
; i
++) {
1877 DisplayLine line
= new DisplayLine(currentState
.attr
);
1878 line
.setReverseColor(reverseVideo
);
1881 display
.addAll(displayBottom
);
1883 assert (display
.size() == height
);
1887 * Scroll the text within a scrolling region down n lines.
1889 * @param regionTop top row of the scrolling region
1890 * @param regionBottom bottom row of the scrolling region
1891 * @param n number of lines to scroll
1893 private void scrollingRegionScrollDown(final int regionTop
,
1894 final int regionBottom
, final int n
) {
1896 if (regionTop
>= regionBottom
) {
1900 // Sanity check: see if there will be any characters left after the
1902 if (regionBottom
+ 1 - regionTop
<= n
) {
1903 // There won't be anything left in the region, so just call
1904 // eraseScreen() and return.
1905 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1909 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1910 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1911 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1913 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
1914 regionTop
+ remaining
);
1915 display
= new LinkedList
<DisplayLine
>(displayTop
);
1916 for (int i
= 0; i
< n
; i
++) {
1917 DisplayLine line
= new DisplayLine(currentState
.attr
);
1918 line
.setReverseColor(reverseVideo
);
1921 display
.addAll(displayMiddle
);
1922 display
.addAll(displayBottom
);
1924 assert (display
.size() == height
);
1928 * Process a control character.
1930 * @param ch 8-bit character from the remote side
1932 private void handleControlChar(final char ch
) {
1933 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
1944 // Transmit the answerback message.
1955 cursorLeft(1, false);
1960 advanceToNextTabStop();
1986 currentState
.glLockshift
= LockshiftMode
.NONE
;
1992 currentState
.glLockshift
= LockshiftMode
.NONE
;
2017 singleshift
= Singleshift
.SS2
;
2022 singleshift
= Singleshift
.SS3
;
2032 * Advance the cursor to the next tab stop.
2034 private void advanceToNextTabStop() {
2035 if (tabStops
.size() == 0) {
2036 // Go to the rightmost column
2037 cursorRight(rightMargin
- currentState
.cursorX
, false);
2040 for (Integer stop
: tabStops
) {
2041 if (stop
> currentState
.cursorX
) {
2042 cursorRight(stop
- currentState
.cursorX
, false);
2047 * We got here, meaning there isn't a tab stop beyond the current
2048 * cursor position. Place the cursor of the right-most edge of the
2051 cursorRight(rightMargin
- currentState
.cursorX
, false);
2055 * Save a character into the collect buffer.
2057 * @param ch character to save
2059 private void collect(final char ch
) {
2060 collectBuffer
.append(ch
);
2064 * Save a byte into the CSI parameters buffer.
2066 * @param ch byte to save
2068 private void param(final byte ch
) {
2069 if (csiParams
.size() == 0) {
2070 csiParams
.add(new Integer(0));
2072 Integer x
= csiParams
.get(csiParams
.size() - 1);
2073 if ((ch
>= '0') && (ch
<= '9')) {
2076 csiParams
.set(csiParams
.size() - 1, x
);
2080 csiParams
.add(new Integer(0));
2085 * Get a CSI parameter value, with a default.
2087 * @param position parameter index. 0 is the first parameter.
2088 * @param defaultValue value to use if csiParams[position] doesn't exist
2089 * @return parameter value
2091 private int getCsiParam(final int position
, final int defaultValue
) {
2092 if (csiParams
.size() < position
+ 1) {
2093 return defaultValue
;
2095 return csiParams
.get(position
).intValue();
2099 * Get a CSI parameter value, clamped to within min/max.
2101 * @param position parameter index. 0 is the first parameter.
2102 * @param defaultValue value to use if csiParams[position] doesn't exist
2103 * @param minValue minimum value inclusive
2104 * @param maxValue maximum value inclusive
2105 * @return parameter value
2107 private int getCsiParam(final int position
, final int defaultValue
,
2108 final int minValue
, final int maxValue
) {
2110 assert (minValue
<= maxValue
);
2111 int value
= getCsiParam(position
, defaultValue
);
2112 if (value
< minValue
) {
2115 if (value
> maxValue
) {
2122 * Set or unset a toggle. value is 'true' for set ('h'), false for reset
2125 private void setToggle(final boolean value
) {
2126 boolean decPrivateModeFlag
= false;
2127 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2128 if (collectBuffer
.charAt(i
) == '?') {
2129 decPrivateModeFlag
= true;
2134 for (Integer i
: csiParams
) {
2139 if (decPrivateModeFlag
== true) {
2141 if (value
== true) {
2142 // Use application arrow keys
2143 arrowKeyMode
= ArrowKeyMode
.VT100
;
2145 // Use ANSI arrow keys
2146 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2151 if (decPrivateModeFlag
== true) {
2152 if (value
== false) {
2156 arrowKeyMode
= ArrowKeyMode
.VT52
;
2159 * From the VT102 docs: "You use ANSI mode to select
2160 * most terminal features; the terminal uses the same
2161 * features when it switches to VT52 mode. You
2162 * cannot, however, change most of these features in
2165 * In other words, do not reset any other attributes
2166 * when switching between VT52 submode and ANSI.
2168 * HOWEVER, the real vt100 does switch the character
2169 * set according to Usenet.
2171 currentState
.g0Charset
= CharacterSet
.US
;
2172 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2175 if ((type
== DeviceType
.VT220
)
2176 || (type
== DeviceType
.XTERM
)) {
2178 // VT52 mode is explicitly 7-bit
2180 singleshift
= Singleshift
.NONE
;
2185 if (value
== true) {
2186 // Turn off keyboard
2195 if (decPrivateModeFlag
== true) {
2197 if (value
== true) {
2206 width
= rightMargin
+ 1;
2207 // Entire screen is cleared, and scrolling region is
2209 eraseScreen(0, 0, height
- 1, width
- 1, false);
2210 scrollRegionTop
= 0;
2211 scrollRegionBottom
= height
- 1;
2212 // Also home the cursor
2213 cursorPosition(0, 0);
2217 if (decPrivateModeFlag
== true) {
2219 if (value
== true) {
2228 if (value
== true) {
2236 if (decPrivateModeFlag
== true) {
2238 if (value
== true) {
2240 * Set selects reverse screen, a white screen
2241 * background with black characters.
2243 if (reverseVideo
!= true) {
2245 * If in normal video, switch it back
2247 invertDisplayColors();
2249 reverseVideo
= true;
2252 * Reset selects normal screen, a black screen
2253 * background with white characters.
2255 if (reverseVideo
== true) {
2257 * If in reverse video already, switch it back
2259 invertDisplayColors();
2261 reverseVideo
= false;
2266 if (decPrivateModeFlag
== true) {
2268 if (value
== true) {
2269 // Origin is relative to scroll region cursor.
2270 // Cursor can NEVER leave scrolling region.
2271 currentState
.originMode
= true;
2272 cursorPosition(0, 0);
2274 // Origin is absolute to entire screen. Cursor can
2275 // leave the scrolling region via cup() and hvp().
2276 currentState
.originMode
= false;
2277 cursorPosition(0, 0);
2282 if (decPrivateModeFlag
== true) {
2284 if (value
== true) {
2286 currentState
.lineWrap
= true;
2288 // Turn linewrap off
2289 currentState
.lineWrap
= false;
2294 if (decPrivateModeFlag
== true) {
2296 if (value
== true) {
2297 // Keyboard auto-repeat on
2300 // Keyboard auto-repeat off
2306 if (decPrivateModeFlag
== false) {
2308 if (value
== true) {
2318 if (decPrivateModeFlag
== true) {
2324 if (decPrivateModeFlag
== true) {
2330 if (decPrivateModeFlag
== false) {
2332 if (value
== true) {
2334 * Set causes a received linefeed, form feed, or
2335 * vertical tab to move cursor to first column of
2336 * next line. RETURN transmits both a carriage return
2337 * and linefeed. This selection is also called new
2343 * Reset causes a received linefeed, form feed, or
2344 * vertical tab to move cursor to next line in
2345 * current column. RETURN transmits a carriage
2348 newLineMode
= false;
2354 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2355 if (decPrivateModeFlag
== true) {
2357 if (value
== true) {
2359 cursorVisible
= true;
2362 cursorVisible
= false;
2369 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2370 if (decPrivateModeFlag
== true) {
2372 if (value
== true) {
2373 // Select national mode NRC
2376 // Select multi-national mode
2385 if ((type
== DeviceType
.XTERM
)
2386 && (decPrivateModeFlag
== true)
2388 // Mouse: normal tracking mode
2389 if (value
== true) {
2390 mouseProtocol
= MouseProtocol
.NORMAL
;
2392 mouseProtocol
= MouseProtocol
.OFF
;
2398 if ((type
== DeviceType
.XTERM
)
2399 && (decPrivateModeFlag
== true)
2401 // Mouse: normal tracking mode
2402 if (value
== true) {
2403 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2405 mouseProtocol
= MouseProtocol
.OFF
;
2411 if ((type
== DeviceType
.XTERM
)
2412 && (decPrivateModeFlag
== true)
2414 // Mouse: Any-event tracking mode
2415 if (value
== true) {
2416 mouseProtocol
= MouseProtocol
.ANYEVENT
;
2418 mouseProtocol
= MouseProtocol
.OFF
;
2424 if ((type
== DeviceType
.XTERM
)
2425 && (decPrivateModeFlag
== true)
2427 // Mouse: UTF-8 coordinates
2428 if (value
== true) {
2429 mouseEncoding
= MouseEncoding
.UTF8
;
2431 mouseEncoding
= MouseEncoding
.X10
;
2444 * DECSC - Save cursor.
2446 private void decsc() {
2447 savedState
.setTo(currentState
);
2451 * DECRC - Restore cursor.
2453 private void decrc() {
2454 currentState
.setTo(savedState
);
2460 private void ind() {
2461 // Move the cursor and scroll if necessary. If at the bottom line
2462 // already, a scroll up is supposed to be performed.
2463 if (currentState
.cursorY
== scrollRegionBottom
) {
2464 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2466 cursorDown(1, true);
2470 * RI - Reverse index.
2473 // Move the cursor and scroll if necessary. If at the top line
2474 // already, a scroll down is supposed to be performed.
2475 if (currentState
.cursorY
== scrollRegionTop
) {
2476 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2484 private void nel() {
2485 // Move the cursor and scroll if necessary. If at the bottom line
2486 // already, a scroll up is supposed to be performed.
2487 if (currentState
.cursorY
== scrollRegionBottom
) {
2488 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2490 cursorDown(1, true);
2492 // Reset to the beginning of the next line
2493 currentState
.cursorX
= 0;
2497 * DECKPAM - Keypad application mode.
2499 private void deckpam() {
2500 keypadMode
= KeypadMode
.Application
;
2504 * DECKPNM - Keypad numeric mode.
2506 private void deckpnm() {
2507 keypadMode
= KeypadMode
.Numeric
;
2513 * @param n number of spaces to move
2514 * @param honorScrollRegion if true, then do nothing if the cursor is
2515 * outside the scrolling region
2517 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2521 * Special case: if a user moves the cursor from the right margin, we
2522 * have to reset the VT100 right margin flag.
2525 wrapLineFlag
= false;
2528 for (int i
= 0; i
< n
; i
++) {
2529 if (honorScrollRegion
== true) {
2530 // Honor the scrolling region
2531 if ((currentState
.cursorY
< scrollRegionTop
)
2532 || (currentState
.cursorY
> scrollRegionBottom
)
2534 // Outside region, do nothing
2537 // Inside region, go up
2538 top
= scrollRegionTop
;
2540 // Non-scrolling case
2544 if (currentState
.cursorY
> top
) {
2545 currentState
.cursorY
--;
2551 * Move down n spaces.
2553 * @param n number of spaces to move
2554 * @param honorScrollRegion if true, then do nothing if the cursor is
2555 * outside the scrolling region
2557 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
2561 * Special case: if a user moves the cursor from the right margin, we
2562 * have to reset the VT100 right margin flag.
2565 wrapLineFlag
= false;
2568 for (int i
= 0; i
< n
; i
++) {
2570 if (honorScrollRegion
== true) {
2571 // Honor the scrolling region
2572 if (currentState
.cursorY
> scrollRegionBottom
) {
2573 // Outside region, do nothing
2576 // Inside region, go down
2577 bottom
= scrollRegionBottom
;
2579 // Non-scrolling case
2580 bottom
= height
- 1;
2583 if (currentState
.cursorY
< bottom
) {
2584 currentState
.cursorY
++;
2590 * Move left n spaces.
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 cursorLeft(final int n
, final boolean honorScrollRegion
) {
2598 * Special case: if a user moves the cursor from the right margin, we
2599 * have to reset the VT100 right margin flag.
2602 wrapLineFlag
= false;
2605 for (int i
= 0; i
< n
; i
++) {
2606 if (honorScrollRegion
== true) {
2607 // Honor the scrolling region
2608 if ((currentState
.cursorY
< scrollRegionTop
)
2609 || (currentState
.cursorY
> scrollRegionBottom
)
2611 // Outside region, do nothing
2616 if (currentState
.cursorX
> 0) {
2617 currentState
.cursorX
--;
2623 * Move right n spaces.
2625 * @param n number of spaces to move
2626 * @param honorScrollRegion if true, then do nothing if the cursor is
2627 * outside the scrolling region
2629 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
2630 int rightMargin
= this.rightMargin
;
2633 * Special case: if a user moves the cursor from the right margin, we
2634 * have to reset the VT100 right margin flag.
2637 wrapLineFlag
= false;
2640 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2641 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2644 for (int i
= 0; i
< n
; i
++) {
2645 if (honorScrollRegion
== true) {
2646 // Honor the scrolling region
2647 if ((currentState
.cursorY
< scrollRegionTop
)
2648 || (currentState
.cursorY
> scrollRegionBottom
)
2650 // Outside region, do nothing
2655 if (currentState
.cursorX
< rightMargin
) {
2656 currentState
.cursorX
++;
2662 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2664 * @param row row to move to
2665 * @param col column to move to
2667 private void cursorPosition(int row
, final int col
) {
2668 int rightMargin
= this.rightMargin
;
2673 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2674 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2677 // Set column number
2678 currentState
.cursorX
= col
;
2680 // Sanity check, bring column back to margin.
2681 if (currentState
.cursorX
> rightMargin
) {
2682 currentState
.cursorX
= rightMargin
;
2686 if (currentState
.originMode
== true) {
2687 row
+= scrollRegionTop
;
2689 if (currentState
.cursorY
< row
) {
2690 cursorDown(row
- currentState
.cursorY
, false);
2691 } else if (currentState
.cursorY
> row
) {
2692 cursorUp(currentState
.cursorY
- row
, false);
2695 wrapLineFlag
= false;
2699 * HTS - Horizontal tabulation set.
2701 private void hts() {
2702 for (Integer stop
: tabStops
) {
2703 if (stop
== currentState
.cursorX
) {
2704 // Already have a tab stop here
2709 // Append a tab stop to the end of the array and resort them
2710 tabStops
.add(currentState
.cursorX
);
2711 Collections
.sort(tabStops
);
2715 * DECSWL - Single-width line.
2717 private void decswl() {
2718 display
.get(currentState
.cursorY
).setDoubleWidth(false);
2719 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2723 * DECDWL - Double-width line.
2725 private void decdwl() {
2726 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2727 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2731 * DECHDL - Double-height + double-width line.
2733 * @param topHalf if true, this sets the row to be the top half row of a
2736 private void dechdl(final boolean topHalf
) {
2737 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2738 if (topHalf
== true) {
2739 display
.get(currentState
.cursorY
).setDoubleHeight(1);
2741 display
.get(currentState
.cursorY
).setDoubleHeight(2);
2746 * DECALN - Screen alignment display.
2748 private void decaln() {
2749 Cell newCell
= new Cell();
2750 newCell
.setChar('E');
2751 for (DisplayLine line
: display
) {
2752 for (int i
= 0; i
< line
.length(); i
++) {
2753 line
.replace(i
, newCell
);
2759 * DECSCL - Compatibility level.
2761 private void decscl() {
2762 int i
= getCsiParam(0, 0);
2763 int j
= getCsiParam(1, 0);
2767 currentState
.g0Charset
= CharacterSet
.US
;
2768 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2770 } else if (i
== 62) {
2772 if ((j
== 0) || (j
== 2)) {
2774 } else if (j
== 1) {
2781 * CUD - Cursor down.
2783 private void cud() {
2784 cursorDown(getCsiParam(0, 1, 1, height
), true);
2788 * CUF - Cursor forward.
2790 private void cuf() {
2791 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
2795 * CUB - Cursor backward.
2797 private void cub() {
2798 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
2804 private void cuu() {
2805 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2809 * CUP - Cursor position.
2811 private void cup() {
2812 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2813 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
2817 * CNL - Cursor down and to column 1.
2819 private void cnl() {
2820 cursorDown(getCsiParam(0, 1, 1, height
), true);
2822 cursorLeft(currentState
.cursorX
, true);
2826 * CPL - Cursor up and to column 1.
2828 private void cpl() {
2829 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2831 cursorLeft(currentState
.cursorX
, true);
2835 * CHA - Cursor to column # in current row.
2837 private void cha() {
2838 cursorPosition(currentState
.cursorY
,
2839 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
2843 * VPA - Cursor to row #, same column.
2845 private void vpa() {
2846 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2847 currentState
.cursorX
);
2851 * ED - Erase in display.
2854 boolean honorProtected
= false;
2855 boolean decPrivateModeFlag
= false;
2857 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2858 if (collectBuffer
.charAt(i
) == '?') {
2859 decPrivateModeFlag
= true;
2864 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2865 && (decPrivateModeFlag
== true)
2867 honorProtected
= true;
2870 int i
= getCsiParam(0, 0);
2873 // Erase from here to end of screen
2874 if (currentState
.cursorY
< height
- 1) {
2875 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
2878 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2879 } else if (i
== 1) {
2880 // Erase from beginning of screen to here
2881 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
2883 eraseLine(0, currentState
.cursorX
, honorProtected
);
2884 } else if (i
== 2) {
2885 // Erase entire screen
2886 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
2891 * EL - Erase in line.
2894 boolean honorProtected
= false;
2895 boolean decPrivateModeFlag
= false;
2897 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2898 if (collectBuffer
.charAt(i
) == '?') {
2899 decPrivateModeFlag
= true;
2904 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2905 && (decPrivateModeFlag
== true)
2907 honorProtected
= true;
2910 int i
= getCsiParam(0, 0);
2913 // Erase from here to end of line
2914 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2915 } else if (i
== 1) {
2916 // Erase from beginning of line to here
2917 eraseLine(0, currentState
.cursorX
, honorProtected
);
2918 } else if (i
== 2) {
2919 // Erase entire line
2920 eraseLine(0, width
- 1, honorProtected
);
2925 * ECH - Erase # of characters in current row.
2927 private void ech() {
2928 int i
= getCsiParam(0, 1, 1, width
);
2930 // Erase from here to i characters
2931 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
2938 int i
= getCsiParam(0, 1);
2940 if ((currentState
.cursorY
>= scrollRegionTop
)
2941 && (currentState
.cursorY
<= scrollRegionBottom
)
2944 // I can get the same effect with a scroll-down
2945 scrollingRegionScrollDown(currentState
.cursorY
,
2946 scrollRegionBottom
, i
);
2951 * DCH - Delete char.
2953 private void dch() {
2954 int n
= getCsiParam(0, 1);
2955 DisplayLine line
= display
.get(currentState
.cursorY
);
2956 Cell blank
= new Cell();
2957 for (int i
= 0; i
< n
; i
++) {
2958 line
.delete(currentState
.cursorX
, blank
);
2963 * ICH - Insert blank char at cursor.
2965 private void ich() {
2966 int n
= getCsiParam(0, 1);
2967 DisplayLine line
= display
.get(currentState
.cursorY
);
2968 Cell blank
= new Cell();
2969 for (int i
= 0; i
< n
; i
++) {
2970 line
.insert(currentState
.cursorX
, blank
);
2978 int i
= getCsiParam(0, 1);
2980 if ((currentState
.cursorY
>= scrollRegionTop
)
2981 && (currentState
.cursorY
<= scrollRegionBottom
)) {
2983 // I can get the same effect with a scroll-down
2984 scrollingRegionScrollUp(currentState
.cursorY
,
2985 scrollRegionBottom
, i
);
2990 * HVP - Horizontal and vertical position.
2992 private void hvp() {
2997 * REP - Repeat character.
2999 private void rep() {
3000 int n
= getCsiParam(0, 1);
3001 for (int i
= 0; i
< n
; i
++) {
3002 printCharacter(repCh
);
3010 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3011 getCsiParam(0, 1, 1, height
));
3018 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3019 getCsiParam(0, 1, 1, height
));
3023 * CBT - Go back X tab stops.
3025 private void cbt() {
3026 int tabsToMove
= getCsiParam(0, 1);
3029 for (int i
= 0; i
< tabsToMove
; i
++) {
3030 int j
= currentState
.cursorX
;
3031 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3032 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3040 j
= tabStops
.get(tabI
);
3042 cursorPosition(currentState
.cursorY
, j
);
3047 * CHT - Advance X tab stops.
3049 private void cht() {
3050 int n
= getCsiParam(0, 1);
3051 for (int i
= 0; i
< n
; i
++) {
3052 advanceToNextTabStop();
3057 * SGR - Select graphics rendition.
3059 private void sgr() {
3061 if (csiParams
.size() == 0) {
3062 currentState
.attr
.reset();
3066 for (Integer i
: csiParams
) {
3072 currentState
.attr
.reset();
3077 currentState
.attr
.setBold(true);
3082 currentState
.attr
.setUnderline(true);
3087 currentState
.attr
.setBlink(true);
3092 currentState
.attr
.setReverse(true);
3099 if (type
== DeviceType
.XTERM
) {
3113 if ((type
== DeviceType
.VT220
)
3114 || (type
== DeviceType
.XTERM
)) {
3120 currentState
.attr
.setBold(false);
3125 currentState
.attr
.setUnderline(false);
3130 currentState
.attr
.setBlink(false);
3135 currentState
.attr
.setReverse(false);
3143 // A true VT100/102/220 does not support color, however everyone
3144 // is used to their terminal emulator supporting color so we will
3145 // unconditionally support color for all DeviceType's.
3150 // Set black foreground
3151 currentState
.attr
.setForeColor(Color
.BLACK
);
3154 // Set red foreground
3155 currentState
.attr
.setForeColor(Color
.RED
);
3158 // Set green foreground
3159 currentState
.attr
.setForeColor(Color
.GREEN
);
3162 // Set yellow foreground
3163 currentState
.attr
.setForeColor(Color
.YELLOW
);
3166 // Set blue foreground
3167 currentState
.attr
.setForeColor(Color
.BLUE
);
3170 // Set magenta foreground
3171 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3174 // Set cyan foreground
3175 currentState
.attr
.setForeColor(Color
.CYAN
);
3178 // Set white foreground
3179 currentState
.attr
.setForeColor(Color
.WHITE
);
3182 // Underscore on, default foreground color
3183 currentState
.attr
.setUnderline(true);
3184 currentState
.attr
.setForeColor(Color
.WHITE
);
3187 // Underscore off, default foreground color
3188 currentState
.attr
.setUnderline(false);
3189 currentState
.attr
.setForeColor(Color
.WHITE
);
3192 // Set black background
3193 currentState
.attr
.setBackColor(Color
.BLACK
);
3196 // Set red background
3197 currentState
.attr
.setBackColor(Color
.RED
);
3200 // Set green background
3201 currentState
.attr
.setBackColor(Color
.GREEN
);
3204 // Set yellow background
3205 currentState
.attr
.setBackColor(Color
.YELLOW
);
3208 // Set blue background
3209 currentState
.attr
.setBackColor(Color
.BLUE
);
3212 // Set magenta background
3213 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3216 // Set cyan background
3217 currentState
.attr
.setBackColor(Color
.CYAN
);
3220 // Set white background
3221 currentState
.attr
.setBackColor(Color
.WHITE
);
3224 // Default background
3225 currentState
.attr
.setBackColor(Color
.BLACK
);
3235 * DA - Device attributes.
3238 int extendedFlag
= 0;
3240 if (collectBuffer
.length() > 0) {
3241 String args
= collectBuffer
.substring(1);
3242 if (collectBuffer
.charAt(0) == '>') {
3244 if (collectBuffer
.length() >= 2) {
3245 i
= Integer
.parseInt(args
.toString());
3247 } else if (collectBuffer
.charAt(0) == '=') {
3249 if (collectBuffer
.length() >= 2) {
3250 i
= Integer
.parseInt(args
.toString());
3253 // Unknown code, bail out
3258 if ((i
!= 0) && (i
!= 1)) {
3262 if ((extendedFlag
== 0) && (i
== 0)) {
3263 // Send string directly to remote side
3264 writeRemote(deviceTypeResponse());
3268 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3270 if ((extendedFlag
== 1) && (i
== 0)) {
3272 * Request "What type of terminal are you, what is your
3273 * firmware version, and what hardware options do you have
3276 * Respond: "I am a VT220 (identification code of 1), my
3277 * firmware version is _____ (Pv), and I have _____ Po
3278 * options installed."
3284 if (s8c1t
== true) {
3285 writeRemote("\u009b>1;10;0c");
3287 writeRemote("\033[>1;10;0c");
3293 if ((extendedFlag
== 2) && (i
== 0)) {
3296 * Request "What is your unit ID?"
3298 * Respond: "I was manufactured at site 00 and have a unique ID
3302 writeRemote("\033P!|00010203\033\\");
3307 * DECSTBM - Set top and bottom margins.
3309 private void decstbm() {
3310 boolean decPrivateModeFlag
= false;
3312 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3313 if (collectBuffer
.charAt(i
) == '?') {
3314 decPrivateModeFlag
= true;
3318 if (decPrivateModeFlag
) {
3319 // This could be restore DEC private mode values.
3323 int top
= getCsiParam(0, 1, 1, height
) - 1;
3324 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3329 scrollRegionTop
= top
;
3330 scrollRegionBottom
= bottom
;
3333 cursorPosition(0, 0);
3338 * DECREQTPARM - Request terminal parameters.
3340 private void decreqtparm() {
3341 int i
= getCsiParam(0, 0);
3343 if ((i
!= 0) && (i
!= 1)) {
3350 * Request terminal parameters.
3354 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3355 * (CLoCk MULtiplier = 1, STP option flags = 0)
3359 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3362 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3364 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3370 * DECSCA - Select Character Attributes.
3372 private void decsca() {
3373 int i
= getCsiParam(0, 0);
3375 if ((i
== 0) || (i
== 2)) {
3377 currentState
.attr
.setProtect(false);
3381 currentState
.attr
.setProtect(true);
3386 * DECSTR - Soft Terminal Reset.
3388 private void decstr() {
3389 // Do exactly like RIS - Reset to initial state
3391 // Do I clear screen too? I think so...
3392 eraseScreen(0, 0, height
- 1, width
- 1, false);
3393 cursorPosition(0, 0);
3397 * DSR - Device status report.
3399 private void dsr() {
3400 boolean decPrivateModeFlag
= false;
3402 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3403 if (collectBuffer
.charAt(i
) == '?') {
3404 decPrivateModeFlag
= true;
3409 int i
= getCsiParam(0, 0);
3414 // Request status report. Respond with "OK, no malfunction."
3416 // Send string directly to remote side
3417 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3420 writeRemote("\u009b0n");
3422 writeRemote("\033[0n");
3427 // Request cursor position. Respond with current position.
3429 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3432 str
= String
.format("\u009b%d;%dR",
3433 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3435 str
= String
.format("\033[%d;%dR",
3436 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3439 // Send string directly to remote side
3444 if (decPrivateModeFlag
== true) {
3446 // Request printer status report. Respond with "Printer not
3449 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3450 && (s8c1t
== true)) {
3451 writeRemote("\u009b?13n");
3453 writeRemote("\033[?13n");
3459 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3460 && (decPrivateModeFlag
== true)
3463 // Request user-defined keys are locked or unlocked. Respond
3464 // with "User-defined keys are locked."
3466 if (s8c1t
== true) {
3467 writeRemote("\u009b?21n");
3469 writeRemote("\033[?21n");
3475 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3476 && (decPrivateModeFlag
== true)
3479 // Request keyboard language. Respond with "Keyboard
3480 // language is North American."
3482 if (s8c1t
== true) {
3483 writeRemote("\u009b?27;1n");
3485 writeRemote("\033[?27;1n");
3492 // Some other option, ignore
3498 * TBC - Tabulation clear.
3500 private void tbc() {
3501 int i
= getCsiParam(0, 0);
3503 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3504 for (Integer stop
: tabStops
) {
3505 if (stop
== currentState
.cursorX
) {
3510 tabStops
= newStops
;
3518 * Erase the characters in the current line from the start column to the
3519 * end column, inclusive.
3521 * @param start starting column to erase (between 0 and width - 1)
3522 * @param end ending column to erase (between 0 and width - 1)
3523 * @param honorProtected if true, do not erase characters with the
3524 * protected attribute set
3526 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3531 if (end
> width
- 1) {
3538 for (int i
= start
; i
<= end
; i
++) {
3539 DisplayLine line
= display
.get(currentState
.cursorY
);
3540 if ((!honorProtected
)
3541 || ((honorProtected
) && (!line
.charAt(i
).getProtect()))) {
3548 * From the VT102 manual:
3550 * Erasing a character also erases any character
3551 * attribute of the character.
3557 * Erase with the current color a.k.a. back-color erase
3560 line
.setChar(i
, ' ');
3561 line
.setAttr(i
, currentState
.attr
);
3569 * Erase a rectangular section of the screen, inclusive. end column,
3572 * @param startRow starting row to erase (between 0 and height - 1)
3573 * @param startCol starting column to erase (between 0 and width - 1)
3574 * @param endRow ending row to erase (between 0 and height - 1)
3575 * @param endCol ending column to erase (between 0 and width - 1)
3576 * @param honorProtected if true, do not erase characters with the
3577 * protected attribute set
3579 private void eraseScreen(final int startRow
, final int startCol
,
3580 final int endRow
, final int endCol
, final boolean honorProtected
) {
3588 || (endRow
< startRow
)
3589 || (endCol
< startCol
)
3594 oldCursorY
= currentState
.cursorY
;
3595 for (int i
= startRow
; i
<= endRow
; i
++) {
3596 currentState
.cursorY
= i
;
3597 eraseLine(startCol
, endCol
, honorProtected
);
3599 // Erase display clears the double attributes
3600 display
.get(i
).setDoubleWidth(false);
3601 display
.get(i
).setDoubleHeight(0);
3603 currentState
.cursorY
= oldCursorY
;
3607 * VT220 printer functions. All of these are parsed, but won't do
3610 private void printerFunctions() {
3611 boolean decPrivateModeFlag
= false;
3612 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3613 if (collectBuffer
.charAt(i
) == '?') {
3614 decPrivateModeFlag
= true;
3619 int i
= getCsiParam(0, 0);
3624 if (decPrivateModeFlag
== false) {
3630 if (decPrivateModeFlag
== true) {
3631 // Print cursor line
3636 if (decPrivateModeFlag
== true) {
3637 // Auto print mode OFF
3639 // Printer controller OFF
3641 // Characters re-appear on the screen
3642 printerControllerMode
= false;
3647 if (decPrivateModeFlag
== true) {
3651 // Printer controller
3653 // Characters get sucked into oblivion
3654 printerControllerMode
= true;
3665 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
3666 * of remote systems will send an XTerm title sequence even if TERM isn't
3669 * @param xtermChar the character received from the remote side
3671 private void oscPut(final char xtermChar
) {
3673 collectBuffer
.append(xtermChar
);
3676 if (xtermChar
== 0x07) {
3677 String args
= collectBuffer
.substring(0,
3678 collectBuffer
.length() - 1);
3679 String
[] p
= args
.toString().split(";");
3681 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
3689 // Go to SCAN_GROUND state
3696 * Run this input character through the ECMA48 state machine.
3698 * @param ch character from the remote side
3700 public void consume(char ch
) {
3703 // System.err.printf("%c", ch);
3705 // Special case for VT10x: 7-bit characters only
3706 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
3707 ch
= (char)(ch
& 0x7F);
3710 // Special "anywhere" states
3712 // 18, 1A --> execute, then switch to SCAN_GROUND
3713 if ((ch
== 0x18) || (ch
== 0x1A)) {
3714 // CAN and SUB abort escape sequences
3719 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
3723 && (scanState
!= ScanState
.DCS_ENTRY
)
3724 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
3725 && (scanState
!= ScanState
.DCS_IGNORE
)
3726 && (scanState
!= ScanState
.DCS_PARAM
)
3727 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
3730 scanState
= ScanState
.ESCAPE
;
3734 // 0x9B == CSI 8-bit sequence
3736 scanState
= ScanState
.CSI_ENTRY
;
3740 // 0x9D goes to ScanState.OSC_STRING
3742 scanState
= ScanState
.OSC_STRING
;
3746 // 0x90 goes to DCS_ENTRY
3748 scanState
= ScanState
.DCS_ENTRY
;
3752 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
3753 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
3754 scanState
= ScanState
.SOSPMAPC_STRING
;
3758 // 0x7F (DEL) is always discarded
3763 switch (scanState
) {
3766 // 00-17, 19, 1C-1F --> execute
3767 // 80-8F, 91-9A, 9C --> execute
3768 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
3769 handleControlChar(ch
);
3773 if (((ch
>= 0x20) && (ch
<= 0x7F))
3777 // VT220 printer --> trash bin
3778 if (((type
== DeviceType
.VT220
)
3779 || (type
== DeviceType
.XTERM
))
3780 && (printerControllerMode
== true)
3785 // Hang onto this character
3786 repCh
= mapCharacter(ch
);
3788 // Print this character
3789 printCharacter(repCh
);
3794 // 00-17, 19, 1C-1F --> execute
3796 handleControlChar(ch
);
3800 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
3801 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
3803 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
3807 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
3808 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
3819 // DECSC - Save cursor
3820 // Note this code overlaps both ANSI and VT52 mode
3825 // DECRC - Restore cursor
3826 // Note this code overlaps both ANSI and VT52 mode
3835 if (vt52Mode
== true) {
3836 // DECANM - Enter ANSI mode
3838 arrowKeyMode
= ArrowKeyMode
.VT100
;
3841 * From the VT102 docs: "You use ANSI mode to select
3842 * most terminal features; the terminal uses the same
3843 * features when it switches to VT52 mode. You
3844 * cannot, however, change most of these features in
3847 * In other words, do not reset any other attributes
3848 * when switching between VT52 submode and ANSI.
3852 currentState
.g0Charset
= CharacterSet
.US
;
3853 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3855 singleshift
= Singleshift
.NONE
;
3856 currentState
.glLockshift
= LockshiftMode
.NONE
;
3857 currentState
.grLockshift
= LockshiftMode
.NONE
;
3861 // DECKPAM - Keypad application mode
3862 // Note this code overlaps both ANSI and VT52 mode
3866 // DECKPNM - Keypad numeric mode
3867 // Note this code overlaps both ANSI and VT52 mode
3874 if (vt52Mode
== true) {
3875 // Cursor up, and stop at the top without scrolling
3880 if (vt52Mode
== true) {
3881 // Cursor down, and stop at the bottom without scrolling
3882 cursorDown(1, false);
3886 if (vt52Mode
== true) {
3887 // Cursor right, and stop at the right without scrolling
3888 cursorRight(1, false);
3892 if (vt52Mode
== true) {
3893 // Cursor left, and stop at the left without scrolling
3894 cursorLeft(1, false);
3901 if (vt52Mode
== true) {
3909 if (vt52Mode
== true) {
3910 // G0 --> Special graphics
3911 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
3915 if (vt52Mode
== true) {
3917 currentState
.g0Charset
= CharacterSet
.US
;
3921 if (vt52Mode
== true) {
3923 cursorPosition(0, 0);
3925 // HTS - Horizontal tabulation set
3930 if (vt52Mode
== true) {
3931 // Reverse line feed. Same as RI.
3936 if (vt52Mode
== true) {
3937 // Erase to end of screen
3938 eraseLine(currentState
.cursorX
, width
- 1, false);
3939 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
3944 if (vt52Mode
== true) {
3945 // Erase to end of line
3946 eraseLine(currentState
.cursorX
, width
- 1, false);
3952 if (vt52Mode
== true) {
3955 // RI - Reverse index
3960 if (vt52Mode
== false) {
3962 singleshift
= Singleshift
.SS2
;
3966 if (vt52Mode
== false) {
3968 singleshift
= Singleshift
.SS3
;
3975 if ((ch
>= 0x51) && (ch
<= 0x57)) {
3991 if (vt52Mode
== true) {
3992 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4000 if (vt52Mode
== true) {
4002 // Send string directly to remote side
4003 writeRemote("\033/Z");
4006 // Send string directly to remote side
4007 writeRemote(deviceTypeResponse());
4018 // VT52 cannot get to any of these other states
4019 if (vt52Mode
== true) {
4024 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4031 // RIS - Reset to initial state
4033 // Do I clear screen too? I think so...
4034 eraseScreen(0, 0, height
- 1, width
- 1, false);
4035 cursorPosition(0, 0);
4049 if ((type
== DeviceType
.VT220
)
4050 || (type
== DeviceType
.XTERM
)) {
4052 // VT220 lockshift G2 into GL
4053 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4058 if ((type
== DeviceType
.VT220
)
4059 || (type
== DeviceType
.XTERM
)) {
4061 // VT220 lockshift G3 into GL
4062 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4080 if ((type
== DeviceType
.VT220
)
4081 || (type
== DeviceType
.XTERM
)) {
4083 // VT220 lockshift G3 into GR
4084 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4089 if ((type
== DeviceType
.VT220
)
4090 || (type
== DeviceType
.XTERM
)) {
4092 // VT220 lockshift G2 into GR
4093 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4099 if ((type
== DeviceType
.VT220
)
4100 || (type
== DeviceType
.XTERM
)) {
4102 // VT220 lockshift G1 into GR
4103 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4113 // 0x5B goes to CSI_ENTRY
4115 scanState
= ScanState
.CSI_ENTRY
;
4118 // 0x5D goes to OSC_STRING
4120 scanState
= ScanState
.OSC_STRING
;
4123 // 0x50 goes to DCS_ENTRY
4125 scanState
= ScanState
.DCS_ENTRY
;
4128 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4129 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4130 scanState
= ScanState
.SOSPMAPC_STRING
;
4135 case ESCAPE_INTERMEDIATE
:
4136 // 00-17, 19, 1C-1F --> execute
4138 handleControlChar(ch
);
4141 // 20-2F --> collect
4142 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4146 // 30-7E --> dispatch, then switch to GROUND
4147 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4150 if ((collectBuffer
.length() == 1)
4151 && (collectBuffer
.charAt(0) == '(')) {
4152 // G0 --> Special graphics
4153 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4155 if ((collectBuffer
.length() == 1)
4156 && (collectBuffer
.charAt(0) == ')')) {
4157 // G1 --> Special graphics
4158 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4160 if ((type
== DeviceType
.VT220
)
4161 || (type
== DeviceType
.XTERM
)) {
4163 if ((collectBuffer
.length() == 1)
4164 && (collectBuffer
.charAt(0) == '*')) {
4165 // G2 --> Special graphics
4166 currentState
.g2Charset
= CharacterSet
.DRAWING
;
4168 if ((collectBuffer
.length() == 1)
4169 && (collectBuffer
.charAt(0) == '+')) {
4170 // G3 --> Special graphics
4171 currentState
.g3Charset
= CharacterSet
.DRAWING
;
4176 if ((collectBuffer
.length() == 1)
4177 && (collectBuffer
.charAt(0) == '(')) {
4178 // G0 --> Alternate character ROM standard character set
4179 currentState
.g0Charset
= CharacterSet
.ROM
;
4181 if ((collectBuffer
.length() == 1)
4182 && (collectBuffer
.charAt(0) == ')')) {
4183 // G1 --> Alternate character ROM standard character set
4184 currentState
.g1Charset
= CharacterSet
.ROM
;
4188 if ((collectBuffer
.length() == 1)
4189 && (collectBuffer
.charAt(0) == '(')) {
4190 // G0 --> Alternate character ROM special graphics
4191 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4193 if ((collectBuffer
.length() == 1)
4194 && (collectBuffer
.charAt(0) == ')')) {
4195 // G1 --> Alternate character ROM special graphics
4196 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4200 if ((collectBuffer
.length() == 1)
4201 && (collectBuffer
.charAt(0) == '#')) {
4202 // DECDHL - Double-height line (top half)
4207 if ((collectBuffer
.length() == 1)
4208 && (collectBuffer
.charAt(0) == '#')) {
4209 // DECDHL - Double-height line (bottom half)
4212 if ((type
== DeviceType
.VT220
)
4213 || (type
== DeviceType
.XTERM
)) {
4215 if ((collectBuffer
.length() == 1)
4216 && (collectBuffer
.charAt(0) == '(')) {
4218 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4220 if ((collectBuffer
.length() == 1)
4221 && (collectBuffer
.charAt(0) == ')')) {
4223 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4225 if ((collectBuffer
.length() == 1)
4226 && (collectBuffer
.charAt(0) == '*')) {
4228 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4230 if ((collectBuffer
.length() == 1)
4231 && (collectBuffer
.charAt(0) == '+')) {
4233 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4238 if ((collectBuffer
.length() == 1)
4239 && (collectBuffer
.charAt(0) == '#')) {
4240 // DECSWL - Single-width line
4243 if ((type
== DeviceType
.VT220
)
4244 || (type
== DeviceType
.XTERM
)) {
4246 if ((collectBuffer
.length() == 1)
4247 && (collectBuffer
.charAt(0) == '(')) {
4249 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4251 if ((collectBuffer
.length() == 1)
4252 && (collectBuffer
.charAt(0) == ')')) {
4254 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4256 if ((collectBuffer
.length() == 1)
4257 && (collectBuffer
.charAt(0) == '*')) {
4259 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4261 if ((collectBuffer
.length() == 1)
4262 && (collectBuffer
.charAt(0) == '+')) {
4264 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4269 if ((collectBuffer
.length() == 1)
4270 && (collectBuffer
.charAt(0) == '#')) {
4271 // DECDWL - Double-width line
4274 if ((type
== DeviceType
.VT220
)
4275 || (type
== DeviceType
.XTERM
)) {
4277 if ((collectBuffer
.length() == 1)
4278 && (collectBuffer
.charAt(0) == '(')) {
4280 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4282 if ((collectBuffer
.length() == 1)
4283 && (collectBuffer
.charAt(0) == ')')) {
4285 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4287 if ((collectBuffer
.length() == 1)
4288 && (collectBuffer
.charAt(0) == '*')) {
4290 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4292 if ((collectBuffer
.length() == 1)
4293 && (collectBuffer
.charAt(0) == '+')) {
4295 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4300 if ((type
== DeviceType
.VT220
)
4301 || (type
== DeviceType
.XTERM
)) {
4303 if ((collectBuffer
.length() == 1)
4304 && (collectBuffer
.charAt(0) == '(')) {
4306 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4308 if ((collectBuffer
.length() == 1)
4309 && (collectBuffer
.charAt(0) == ')')) {
4311 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4313 if ((collectBuffer
.length() == 1)
4314 && (collectBuffer
.charAt(0) == '*')) {
4316 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4318 if ((collectBuffer
.length() == 1)
4319 && (collectBuffer
.charAt(0) == '+')) {
4321 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4326 if ((collectBuffer
.length() == 1)
4327 && (collectBuffer
.charAt(0) == '#')) {
4328 // DECALN - Screen alignment display
4337 if ((type
== DeviceType
.VT220
)
4338 || (type
== DeviceType
.XTERM
)) {
4340 if ((collectBuffer
.length() == 1)
4341 && (collectBuffer
.charAt(0) == '(')) {
4342 // G0 --> DEC_SUPPLEMENTAL
4343 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4345 if ((collectBuffer
.length() == 1)
4346 && (collectBuffer
.charAt(0) == ')')) {
4347 // G1 --> DEC_SUPPLEMENTAL
4348 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4350 if ((collectBuffer
.length() == 1)
4351 && (collectBuffer
.charAt(0) == '*')) {
4352 // G2 --> DEC_SUPPLEMENTAL
4353 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4355 if ((collectBuffer
.length() == 1)
4356 && (collectBuffer
.charAt(0) == '+')) {
4357 // G3 --> DEC_SUPPLEMENTAL
4358 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4363 if ((type
== DeviceType
.VT220
)
4364 || (type
== DeviceType
.XTERM
)) {
4366 if ((collectBuffer
.length() == 1)
4367 && (collectBuffer
.charAt(0) == '(')) {
4369 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4371 if ((collectBuffer
.length() == 1)
4372 && (collectBuffer
.charAt(0) == ')')) {
4374 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4376 if ((collectBuffer
.length() == 1)
4377 && (collectBuffer
.charAt(0) == '*')) {
4379 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4381 if ((collectBuffer
.length() == 1)
4382 && (collectBuffer
.charAt(0) == '+')) {
4384 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4393 if ((collectBuffer
.length() == 1)
4394 && (collectBuffer
.charAt(0) == '(')) {
4395 // G0 --> United Kingdom set
4396 currentState
.g0Charset
= CharacterSet
.UK
;
4398 if ((collectBuffer
.length() == 1)
4399 && (collectBuffer
.charAt(0) == ')')) {
4400 // G1 --> United Kingdom set
4401 currentState
.g1Charset
= CharacterSet
.UK
;
4403 if ((type
== DeviceType
.VT220
)
4404 || (type
== DeviceType
.XTERM
)) {
4406 if ((collectBuffer
.length() == 1)
4407 && (collectBuffer
.charAt(0) == '*')) {
4408 // G2 --> United Kingdom set
4409 currentState
.g2Charset
= CharacterSet
.UK
;
4411 if ((collectBuffer
.length() == 1)
4412 && (collectBuffer
.charAt(0) == '+')) {
4413 // G3 --> United Kingdom set
4414 currentState
.g3Charset
= CharacterSet
.UK
;
4419 if ((collectBuffer
.length() == 1)
4420 && (collectBuffer
.charAt(0) == '(')) {
4422 currentState
.g0Charset
= CharacterSet
.US
;
4424 if ((collectBuffer
.length() == 1)
4425 && (collectBuffer
.charAt(0) == ')')) {
4427 currentState
.g1Charset
= CharacterSet
.US
;
4429 if ((type
== DeviceType
.VT220
)
4430 || (type
== DeviceType
.XTERM
)) {
4432 if ((collectBuffer
.length() == 1)
4433 && (collectBuffer
.charAt(0) == '*')) {
4435 currentState
.g2Charset
= CharacterSet
.US
;
4437 if ((collectBuffer
.length() == 1)
4438 && (collectBuffer
.charAt(0) == '+')) {
4440 currentState
.g3Charset
= CharacterSet
.US
;
4445 if ((type
== DeviceType
.VT220
)
4446 || (type
== DeviceType
.XTERM
)) {
4448 if ((collectBuffer
.length() == 1)
4449 && (collectBuffer
.charAt(0) == '(')) {
4451 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4453 if ((collectBuffer
.length() == 1)
4454 && (collectBuffer
.charAt(0) == ')')) {
4456 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4458 if ((collectBuffer
.length() == 1)
4459 && (collectBuffer
.charAt(0) == '*')) {
4461 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4463 if ((collectBuffer
.length() == 1)
4464 && (collectBuffer
.charAt(0) == '+')) {
4466 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4473 if ((type
== DeviceType
.VT220
)
4474 || (type
== DeviceType
.XTERM
)) {
4476 if ((collectBuffer
.length() == 1)
4477 && (collectBuffer
.charAt(0) == '(')) {
4479 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4481 if ((collectBuffer
.length() == 1)
4482 && (collectBuffer
.charAt(0) == ')')) {
4484 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4486 if ((collectBuffer
.length() == 1)
4487 && (collectBuffer
.charAt(0) == '*')) {
4489 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4491 if ((collectBuffer
.length() == 1)
4492 && (collectBuffer
.charAt(0) == '+')) {
4494 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4499 if ((type
== DeviceType
.VT220
)
4500 || (type
== DeviceType
.XTERM
)) {
4502 if ((collectBuffer
.length() == 1)
4503 && (collectBuffer
.charAt(0) == ' ')) {
4510 if ((type
== DeviceType
.VT220
)
4511 || (type
== DeviceType
.XTERM
)) {
4513 if ((collectBuffer
.length() == 1)
4514 && (collectBuffer
.charAt(0) == ' ')) {
4521 if ((type
== DeviceType
.VT220
)
4522 || (type
== DeviceType
.XTERM
)) {
4524 if ((collectBuffer
.length() == 1)
4525 && (collectBuffer
.charAt(0) == '(')) {
4527 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4529 if ((collectBuffer
.length() == 1)
4530 && (collectBuffer
.charAt(0) == ')')) {
4532 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4534 if ((collectBuffer
.length() == 1)
4535 && (collectBuffer
.charAt(0) == '*')) {
4537 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4539 if ((collectBuffer
.length() == 1)
4540 && (collectBuffer
.charAt(0) == '+')) {
4542 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4550 if ((type
== DeviceType
.VT220
)
4551 || (type
== DeviceType
.XTERM
)) {
4553 if ((collectBuffer
.length() == 1)
4554 && (collectBuffer
.charAt(0) == '(')) {
4556 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4558 if ((collectBuffer
.length() == 1)
4559 && (collectBuffer
.charAt(0) == ')')) {
4561 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4563 if ((collectBuffer
.length() == 1)
4564 && (collectBuffer
.charAt(0) == '*')) {
4566 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4568 if ((collectBuffer
.length() == 1)
4569 && (collectBuffer
.charAt(0) == '+')) {
4571 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4582 if ((type
== DeviceType
.VT220
)
4583 || (type
== DeviceType
.XTERM
)) {
4585 if ((collectBuffer
.length() == 1)
4586 && (collectBuffer
.charAt(0) == '(')) {
4588 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4590 if ((collectBuffer
.length() == 1)
4591 && (collectBuffer
.charAt(0) == ')')) {
4593 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4595 if ((collectBuffer
.length() == 1)
4596 && (collectBuffer
.charAt(0) == '*')) {
4598 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4600 if ((collectBuffer
.length() == 1)
4601 && (collectBuffer
.charAt(0) == '+')) {
4603 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4608 if ((type
== DeviceType
.VT220
)
4609 || (type
== DeviceType
.XTERM
)) {
4611 if ((collectBuffer
.length() == 1)
4612 && (collectBuffer
.charAt(0) == '(')) {
4614 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4616 if ((collectBuffer
.length() == 1)
4617 && (collectBuffer
.charAt(0) == ')')) {
4619 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4621 if ((collectBuffer
.length() == 1)
4622 && (collectBuffer
.charAt(0) == '*')) {
4624 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
4626 if ((collectBuffer
.length() == 1)
4627 && (collectBuffer
.charAt(0) == '+')) {
4629 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
4641 if ((type
== DeviceType
.VT220
)
4642 || (type
== DeviceType
.XTERM
)) {
4644 if ((collectBuffer
.length() == 1)
4645 && (collectBuffer
.charAt(0) == '(')) {
4647 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
4649 if ((collectBuffer
.length() == 1)
4650 && (collectBuffer
.charAt(0) == ')')) {
4652 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
4654 if ((collectBuffer
.length() == 1)
4655 && (collectBuffer
.charAt(0) == '*')) {
4657 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
4659 if ((collectBuffer
.length() == 1)
4660 && (collectBuffer
.charAt(0) == '+')) {
4662 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
4667 if ((type
== DeviceType
.VT220
)
4668 || (type
== DeviceType
.XTERM
)) {
4670 if ((collectBuffer
.length() == 1)
4671 && (collectBuffer
.charAt(0) == '(')) {
4673 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
4675 if ((collectBuffer
.length() == 1)
4676 && (collectBuffer
.charAt(0) == ')')) {
4678 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
4680 if ((collectBuffer
.length() == 1)
4681 && (collectBuffer
.charAt(0) == '*')) {
4683 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
4685 if ((collectBuffer
.length() == 1)
4686 && (collectBuffer
.charAt(0) == '+')) {
4688 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
4735 // 0x9C goes to GROUND
4743 // 00-17, 19, 1C-1F --> execute
4745 handleControlChar(ch
);
4748 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
4749 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4751 scanState
= ScanState
.CSI_INTERMEDIATE
;
4754 // 30-39, 3B --> param, then switch to CSI_PARAM
4755 if ((ch
>= '0') && (ch
<= '9')) {
4757 scanState
= ScanState
.CSI_PARAM
;
4761 scanState
= ScanState
.CSI_PARAM
;
4764 // 3C-3F --> collect, then switch to CSI_PARAM
4765 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
4767 scanState
= ScanState
.CSI_PARAM
;
4770 // 40-7E --> dispatch, then switch to GROUND
4771 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
4774 // ICH - Insert character
4782 // CUD - Cursor down
4786 // CUF - Cursor forward
4790 // CUB - Cursor backward
4794 // CNL - Cursor down and to column 1
4795 if (type
== DeviceType
.XTERM
) {
4800 // CPL - Cursor up and to column 1
4801 if (type
== DeviceType
.XTERM
) {
4806 // CHA - Cursor to column # in current row
4807 if (type
== DeviceType
.XTERM
) {
4812 // CUP - Cursor position
4816 // CHT - Cursor forward X tab stops (default 1)
4817 if (type
== DeviceType
.XTERM
) {
4822 // ED - Erase in display
4826 // EL - Erase in line
4841 // DCH - Delete character
4848 // Scroll up X lines (default 1)
4849 if (type
== DeviceType
.XTERM
) {
4854 // Scroll down X lines (default 1)
4855 if (type
== DeviceType
.XTERM
) {
4864 if ((type
== DeviceType
.VT220
)
4865 || (type
== DeviceType
.XTERM
)) {
4867 // ECH - Erase character
4874 // CBT - Cursor backward X tab stops (default 1)
4875 if (type
== DeviceType
.XTERM
) {
4886 // HPA - Cursor to column # in current row. Same as CHA
4887 if (type
== DeviceType
.XTERM
) {
4892 // HPR - Cursor right. Same as CUF
4893 if (type
== DeviceType
.XTERM
) {
4898 // REP - Repeat last char X times
4899 if (type
== DeviceType
.XTERM
) {
4904 // DA - Device attributes
4908 // VPA - Cursor to row, current column.
4909 if (type
== DeviceType
.XTERM
) {
4914 // VPR - Cursor down. Same as CUD
4915 if (type
== DeviceType
.XTERM
) {
4920 // HVP - Horizontal and vertical position
4924 // TBC - Tabulation clear
4928 // Sets an ANSI or DEC private toggle
4932 if ((type
== DeviceType
.VT220
)
4933 || (type
== DeviceType
.XTERM
)) {
4935 // Printer functions
4943 // Sets an ANSI or DEC private toggle
4947 // SGR - Select graphics rendition
4951 // DSR - Device status report
4958 // DECLL - Load leds
4962 // DECSTBM - Set top and bottom margins
4966 // Save cursor (ANSI.SYS)
4967 if (type
== DeviceType
.XTERM
) {
4968 savedState
.cursorX
= currentState
.cursorX
;
4969 savedState
.cursorY
= currentState
.cursorY
;
4975 // Restore cursor (ANSI.SYS)
4976 if (type
== DeviceType
.XTERM
) {
4977 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
4984 // DECREQTPARM - Request terminal parameters
5000 // 0x9C goes to GROUND
5005 // 0x3A goes to CSI_IGNORE
5007 scanState
= ScanState
.CSI_IGNORE
;
5012 // 00-17, 19, 1C-1F --> execute
5014 handleControlChar(ch
);
5017 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5018 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5020 scanState
= ScanState
.CSI_INTERMEDIATE
;
5023 // 30-39, 3B --> param
5024 if ((ch
>= '0') && (ch
<= '9')) {
5031 // 0x3A goes to CSI_IGNORE
5033 scanState
= ScanState
.CSI_IGNORE
;
5035 // 0x3C-3F goes to CSI_IGNORE
5036 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5037 scanState
= ScanState
.CSI_IGNORE
;
5040 // 40-7E --> dispatch, then switch to GROUND
5041 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5044 // ICH - Insert character
5052 // CUD - Cursor down
5056 // CUF - Cursor forward
5060 // CUB - Cursor backward
5064 // CNL - Cursor down and to column 1
5065 if (type
== DeviceType
.XTERM
) {
5070 // CPL - Cursor up and to column 1
5071 if (type
== DeviceType
.XTERM
) {
5076 // CHA - Cursor to column # in current row
5077 if (type
== DeviceType
.XTERM
) {
5082 // CUP - Cursor position
5086 // CHT - Cursor forward X tab stops (default 1)
5087 if (type
== DeviceType
.XTERM
) {
5092 // ED - Erase in display
5096 // EL - Erase in line
5111 // DCH - Delete character
5118 // Scroll up X lines (default 1)
5119 if (type
== DeviceType
.XTERM
) {
5124 // Scroll down X lines (default 1)
5125 if (type
== DeviceType
.XTERM
) {
5134 if ((type
== DeviceType
.VT220
)
5135 || (type
== DeviceType
.XTERM
)) {
5137 // ECH - Erase character
5144 // CBT - Cursor backward X tab stops (default 1)
5145 if (type
== DeviceType
.XTERM
) {
5156 // HPA - Cursor to column # in current row. Same as CHA
5157 if (type
== DeviceType
.XTERM
) {
5162 // HPR - Cursor right. Same as CUF
5163 if (type
== DeviceType
.XTERM
) {
5168 // REP - Repeat last char X times
5169 if (type
== DeviceType
.XTERM
) {
5174 // DA - Device attributes
5178 // VPA - Cursor to row, current column.
5179 if (type
== DeviceType
.XTERM
) {
5184 // VPR - Cursor down. Same as CUD
5185 if (type
== DeviceType
.XTERM
) {
5190 // HVP - Horizontal and vertical position
5194 // TBC - Tabulation clear
5198 // Sets an ANSI or DEC private toggle
5202 if ((type
== DeviceType
.VT220
)
5203 || (type
== DeviceType
.XTERM
)) {
5205 // Printer functions
5213 // Sets an ANSI or DEC private toggle
5217 // SGR - Select graphics rendition
5221 // DSR - Device status report
5228 // DECLL - Load leds
5232 // DECSTBM - Set top and bottom margins
5242 // DECREQTPARM - Request terminal parameters
5259 case CSI_INTERMEDIATE
:
5260 // 00-17, 19, 1C-1F --> execute
5262 handleControlChar(ch
);
5265 // 20-2F --> collect
5266 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5270 // 0x30-3F goes to CSI_IGNORE
5271 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5272 scanState
= ScanState
.CSI_IGNORE
;
5275 // 40-7E --> dispatch, then switch to GROUND
5276 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5328 if (((type
== DeviceType
.VT220
)
5329 || (type
== DeviceType
.XTERM
))
5330 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5332 // DECSCL - compatibility level
5335 if ((type
== DeviceType
.XTERM
)
5336 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
5338 // DECSTR - Soft terminal reset
5343 if (((type
== DeviceType
.VT220
)
5344 || (type
== DeviceType
.XTERM
))
5345 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5373 // 00-17, 19, 1C-1F --> execute
5375 handleControlChar(ch
);
5378 // 20-2F --> collect
5379 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5383 // 40-7E --> ignore, then switch to GROUND
5384 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5388 // 20-3F, 7F --> ignore
5394 // 0x9C goes to GROUND
5399 // 0x1B 0x5C goes to GROUND
5404 if ((collectBuffer
.length() > 0)
5405 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5411 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5412 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5414 scanState
= ScanState
.DCS_INTERMEDIATE
;
5417 // 30-39, 3B --> param, then switch to DCS_PARAM
5418 if ((ch
>= '0') && (ch
<= '9')) {
5420 scanState
= ScanState
.DCS_PARAM
;
5424 scanState
= ScanState
.DCS_PARAM
;
5427 // 3C-3F --> collect, then switch to DCS_PARAM
5428 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5430 scanState
= ScanState
.DCS_PARAM
;
5433 // 00-17, 19, 1C-1F, 7F --> ignore
5435 // 0x3A goes to DCS_IGNORE
5437 scanState
= ScanState
.DCS_IGNORE
;
5440 // 0x40-7E goes to DCS_PASSTHROUGH
5441 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5442 scanState
= ScanState
.DCS_PASSTHROUGH
;
5446 case DCS_INTERMEDIATE
:
5448 // 0x9C goes to GROUND
5453 // 0x1B 0x5C goes to GROUND
5458 if ((collectBuffer
.length() > 0) &&
5459 (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5465 // 0x30-3F goes to DCS_IGNORE
5466 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5467 scanState
= ScanState
.DCS_IGNORE
;
5470 // 0x40-7E goes to DCS_PASSTHROUGH
5471 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5472 scanState
= ScanState
.DCS_PASSTHROUGH
;
5475 // 00-17, 19, 1C-1F, 7F --> ignore
5480 // 0x9C goes to GROUND
5485 // 0x1B 0x5C goes to GROUND
5490 if ((collectBuffer
.length() > 0) &&
5491 (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5497 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5498 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5500 scanState
= ScanState
.DCS_INTERMEDIATE
;
5503 // 30-39, 3B --> param
5504 if ((ch
>= '0') && (ch
<= '9')) {
5511 // 00-17, 19, 1C-1F, 7F --> ignore
5513 // 0x3A, 3C-3F goes to DCS_IGNORE
5515 scanState
= ScanState
.DCS_IGNORE
;
5517 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5518 scanState
= ScanState
.DCS_IGNORE
;
5521 // 0x40-7E goes to DCS_PASSTHROUGH
5522 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5523 scanState
= ScanState
.DCS_PASSTHROUGH
;
5527 case DCS_PASSTHROUGH
:
5528 // 0x9C goes to GROUND
5533 // 0x1B 0x5C goes to GROUND
5538 if ((collectBuffer
.length() > 0)
5539 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5545 // 00-17, 19, 1C-1F, 20-7E --> put
5553 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5556 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5565 // 00-17, 19, 1C-1F, 20-7F --> ignore
5567 // 0x9C goes to GROUND
5574 case SOSPMAPC_STRING
:
5575 // 00-17, 19, 1C-1F, 20-7F --> ignore
5577 // 0x9C goes to GROUND
5585 // Special case for Xterm: OSC can pass control characters
5586 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5590 // 00-17, 19, 1C-1F --> ignore
5592 // 20-7F --> osc_put
5593 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5597 // 0x9C goes to GROUND
5604 case VT52_DIRECT_CURSOR_ADDRESS
:
5605 // This is a special case for the VT52 sequence "ESC Y l c"
5606 if (collectBuffer
.length() == 0) {
5608 } else if (collectBuffer
.length() == 1) {
5609 // We've got the two characters, one in the buffer and the
5611 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
5620 * Expose current cursor X to outside world.
5622 * @return current cursor X
5624 public final int getCursorX() {
5625 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
5626 return currentState
.cursorX
* 2;
5628 return currentState
.cursorX
;
5632 * Expose current cursor Y to outside world.
5634 * @return current cursor Y
5636 public final int getCursorY() {
5637 return currentState
.cursorY
;
5641 * Read function runs on a separate thread.
5644 boolean utf8
= false;
5645 boolean done
= false;
5647 if (type
== DeviceType
.XTERM
) {
5651 // available() will often return > 1, so we need to read in chunks to
5653 char [] readBufferUTF8
= null;
5654 byte [] readBuffer
= null;
5656 readBufferUTF8
= new char[128];
5658 readBuffer
= new byte[128];
5661 while (!done
&& !stopReaderThread
) {
5663 int n
= inputStream
.available();
5664 // System.err.printf("available() %d\n", n); System.err.flush();
5666 if (readBufferUTF8
.length
< n
) {
5667 // The buffer wasn't big enough, make it huger
5668 int newSizeHalf
= Math
.max(readBufferUTF8
.length
, n
);
5670 readBufferUTF8
= new char[newSizeHalf
* 2];
5673 if (readBuffer
.length
< n
) {
5674 // The buffer wasn't big enough, make it huger
5675 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
5676 readBuffer
= new byte[newSizeHalf
* 2];
5682 rc
= input
.read(readBufferUTF8
, 0,
5683 readBufferUTF8
.length
);
5685 rc
= inputStream
.read(readBuffer
, 0,
5688 // System.err.printf("read() %d\n", rc); System.err.flush();
5693 for (int i
= 0; i
< rc
; i
++) {
5696 ch
= readBufferUTF8
[i
];
5700 // Don't step on UI events
5701 synchronized (this) {
5706 // System.err.println("end while loop"); System.err.flush();
5707 } catch (IOException e
) {
5708 e
.printStackTrace();
5711 } // while ((done == false) && (stopReaderThread == false))
5713 // Let the rest of the world know that I am done.
5714 stopReaderThread
= true;
5716 // System.err.println("*** run() exiting..."); System.err.flush();