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 int top
= getCsiParam(0, 1, 1, height
) - 1;
3311 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3316 scrollRegionTop
= top
;
3317 scrollRegionBottom
= bottom
;
3320 cursorPosition(0, 0);
3324 * DECREQTPARM - Request terminal parameters.
3326 private void decreqtparm() {
3327 int i
= getCsiParam(0, 0);
3329 if ((i
!= 0) && (i
!= 1)) {
3336 * Request terminal parameters.
3340 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3341 * (CLoCk MULtiplier = 1, STP option flags = 0)
3345 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3348 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3350 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3356 * DECSCA - Select Character Attributes.
3358 private void decsca() {
3359 int i
= getCsiParam(0, 0);
3361 if ((i
== 0) || (i
== 2)) {
3363 currentState
.attr
.setProtect(false);
3367 currentState
.attr
.setProtect(true);
3372 * DECSTR - Soft Terminal Reset.
3374 private void decstr() {
3375 // Do exactly like RIS - Reset to initial state
3377 // Do I clear screen too? I think so...
3378 eraseScreen(0, 0, height
- 1, width
- 1, false);
3379 cursorPosition(0, 0);
3383 * DSR - Device status report.
3385 private void dsr() {
3386 boolean decPrivateModeFlag
= false;
3388 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3389 if (collectBuffer
.charAt(i
) == '?') {
3390 decPrivateModeFlag
= true;
3395 int i
= getCsiParam(0, 0);
3400 // Request status report. Respond with "OK, no malfunction."
3402 // Send string directly to remote side
3403 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3406 writeRemote("\u009b0n");
3408 writeRemote("\033[0n");
3413 // Request cursor position. Respond with current position.
3415 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3418 str
= String
.format("\u009b%d;%dR",
3419 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3421 str
= String
.format("\033[%d;%dR",
3422 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3425 // Send string directly to remote side
3430 if (decPrivateModeFlag
== true) {
3432 // Request printer status report. Respond with "Printer not
3435 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3436 && (s8c1t
== true)) {
3437 writeRemote("\u009b?13n");
3439 writeRemote("\033[?13n");
3445 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3446 && (decPrivateModeFlag
== true)
3449 // Request user-defined keys are locked or unlocked. Respond
3450 // with "User-defined keys are locked."
3452 if (s8c1t
== true) {
3453 writeRemote("\u009b?21n");
3455 writeRemote("\033[?21n");
3461 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3462 && (decPrivateModeFlag
== true)
3465 // Request keyboard language. Respond with "Keyboard
3466 // language is North American."
3468 if (s8c1t
== true) {
3469 writeRemote("\u009b?27;1n");
3471 writeRemote("\033[?27;1n");
3478 // Some other option, ignore
3484 * TBC - Tabulation clear.
3486 private void tbc() {
3487 int i
= getCsiParam(0, 0);
3489 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3490 for (Integer stop
: tabStops
) {
3491 if (stop
== currentState
.cursorX
) {
3496 tabStops
= newStops
;
3504 * Erase the characters in the current line from the start column to the
3505 * end column, inclusive.
3507 * @param start starting column to erase (between 0 and width - 1)
3508 * @param end ending column to erase (between 0 and width - 1)
3509 * @param honorProtected if true, do not erase characters with the
3510 * protected attribute set
3512 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3517 if (end
> width
- 1) {
3524 for (int i
= start
; i
<= end
; i
++) {
3525 DisplayLine line
= display
.get(currentState
.cursorY
);
3526 if ((!honorProtected
)
3527 || ((honorProtected
) && (!line
.charAt(i
).getProtect()))) {
3534 * From the VT102 manual:
3536 * Erasing a character also erases any character
3537 * attribute of the character.
3543 * Erase with the current color a.k.a. back-color erase
3546 line
.setChar(i
, ' ');
3547 line
.setAttr(i
, currentState
.attr
);
3555 * Erase a rectangular section of the screen, inclusive. end column,
3558 * @param startRow starting row to erase (between 0 and height - 1)
3559 * @param startCol starting column to erase (between 0 and width - 1)
3560 * @param endRow ending row to erase (between 0 and height - 1)
3561 * @param endCol ending column to erase (between 0 and width - 1)
3562 * @param honorProtected if true, do not erase characters with the
3563 * protected attribute set
3565 private void eraseScreen(final int startRow
, final int startCol
,
3566 final int endRow
, final int endCol
, final boolean honorProtected
) {
3574 || (endRow
< startRow
)
3575 || (endCol
< startCol
)
3580 oldCursorY
= currentState
.cursorY
;
3581 for (int i
= startRow
; i
<= endRow
; i
++) {
3582 currentState
.cursorY
= i
;
3583 eraseLine(startCol
, endCol
, honorProtected
);
3585 // Erase display clears the double attributes
3586 display
.get(i
).setDoubleWidth(false);
3587 display
.get(i
).setDoubleHeight(0);
3589 currentState
.cursorY
= oldCursorY
;
3593 * VT220 printer functions. All of these are parsed, but won't do
3596 private void printerFunctions() {
3597 boolean decPrivateModeFlag
= false;
3598 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3599 if (collectBuffer
.charAt(i
) == '?') {
3600 decPrivateModeFlag
= true;
3605 int i
= getCsiParam(0, 0);
3610 if (decPrivateModeFlag
== false) {
3616 if (decPrivateModeFlag
== true) {
3617 // Print cursor line
3622 if (decPrivateModeFlag
== true) {
3623 // Auto print mode OFF
3625 // Printer controller OFF
3627 // Characters re-appear on the screen
3628 printerControllerMode
= false;
3633 if (decPrivateModeFlag
== true) {
3637 // Printer controller
3639 // Characters get sucked into oblivion
3640 printerControllerMode
= true;
3651 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
3652 * of remote systems will send an XTerm title sequence even if TERM isn't
3655 * @param xtermChar the character received from the remote side
3657 private void oscPut(final char xtermChar
) {
3659 collectBuffer
.append(xtermChar
);
3662 if (xtermChar
== 0x07) {
3663 String args
= collectBuffer
.substring(0,
3664 collectBuffer
.length() - 1);
3665 String
[] p
= args
.toString().split(";");
3667 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
3675 // Go to SCAN_GROUND state
3682 * Run this input character through the ECMA48 state machine.
3684 * @param ch character from the remote side
3686 public void consume(char ch
) {
3689 // System.err.printf("%c", ch);
3691 // Special case for VT10x: 7-bit characters only
3692 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
3693 ch
= (char)(ch
& 0x7F);
3696 // Special "anywhere" states
3698 // 18, 1A --> execute, then switch to SCAN_GROUND
3699 if ((ch
== 0x18) || (ch
== 0x1A)) {
3700 // CAN and SUB abort escape sequences
3705 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
3709 && (scanState
!= ScanState
.DCS_ENTRY
)
3710 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
3711 && (scanState
!= ScanState
.DCS_IGNORE
)
3712 && (scanState
!= ScanState
.DCS_PARAM
)
3713 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
3716 scanState
= ScanState
.ESCAPE
;
3720 // 0x9B == CSI 8-bit sequence
3722 scanState
= ScanState
.CSI_ENTRY
;
3726 // 0x9D goes to ScanState.OSC_STRING
3728 scanState
= ScanState
.OSC_STRING
;
3732 // 0x90 goes to DCS_ENTRY
3734 scanState
= ScanState
.DCS_ENTRY
;
3738 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
3739 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
3740 scanState
= ScanState
.SOSPMAPC_STRING
;
3744 // 0x7F (DEL) is always discarded
3749 switch (scanState
) {
3752 // 00-17, 19, 1C-1F --> execute
3753 // 80-8F, 91-9A, 9C --> execute
3754 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
3755 handleControlChar(ch
);
3759 if (((ch
>= 0x20) && (ch
<= 0x7F))
3763 // VT220 printer --> trash bin
3764 if (((type
== DeviceType
.VT220
)
3765 || (type
== DeviceType
.XTERM
))
3766 && (printerControllerMode
== true)
3771 // Hang onto this character
3772 repCh
= mapCharacter(ch
);
3774 // Print this character
3775 printCharacter(repCh
);
3780 // 00-17, 19, 1C-1F --> execute
3782 handleControlChar(ch
);
3786 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
3787 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
3789 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
3793 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
3794 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
3805 // DECSC - Save cursor
3806 // Note this code overlaps both ANSI and VT52 mode
3811 // DECRC - Restore cursor
3812 // Note this code overlaps both ANSI and VT52 mode
3821 if (vt52Mode
== true) {
3822 // DECANM - Enter ANSI mode
3824 arrowKeyMode
= ArrowKeyMode
.VT100
;
3827 * From the VT102 docs: "You use ANSI mode to select
3828 * most terminal features; the terminal uses the same
3829 * features when it switches to VT52 mode. You
3830 * cannot, however, change most of these features in
3833 * In other words, do not reset any other attributes
3834 * when switching between VT52 submode and ANSI.
3838 currentState
.g0Charset
= CharacterSet
.US
;
3839 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3841 singleshift
= Singleshift
.NONE
;
3842 currentState
.glLockshift
= LockshiftMode
.NONE
;
3843 currentState
.grLockshift
= LockshiftMode
.NONE
;
3847 // DECKPAM - Keypad application mode
3848 // Note this code overlaps both ANSI and VT52 mode
3852 // DECKPNM - Keypad numeric mode
3853 // Note this code overlaps both ANSI and VT52 mode
3860 if (vt52Mode
== true) {
3861 // Cursor up, and stop at the top without scrolling
3866 if (vt52Mode
== true) {
3867 // Cursor down, and stop at the bottom without scrolling
3868 cursorDown(1, false);
3872 if (vt52Mode
== true) {
3873 // Cursor right, and stop at the right without scrolling
3874 cursorRight(1, false);
3878 if (vt52Mode
== true) {
3879 // Cursor left, and stop at the left without scrolling
3880 cursorLeft(1, false);
3887 if (vt52Mode
== true) {
3895 if (vt52Mode
== true) {
3896 // G0 --> Special graphics
3897 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
3901 if (vt52Mode
== true) {
3903 currentState
.g0Charset
= CharacterSet
.US
;
3907 if (vt52Mode
== true) {
3909 cursorPosition(0, 0);
3911 // HTS - Horizontal tabulation set
3916 if (vt52Mode
== true) {
3917 // Reverse line feed. Same as RI.
3922 if (vt52Mode
== true) {
3923 // Erase to end of screen
3924 eraseLine(currentState
.cursorX
, width
- 1, false);
3925 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
3930 if (vt52Mode
== true) {
3931 // Erase to end of line
3932 eraseLine(currentState
.cursorX
, width
- 1, false);
3938 if (vt52Mode
== true) {
3941 // RI - Reverse index
3946 if (vt52Mode
== false) {
3948 singleshift
= Singleshift
.SS2
;
3952 if (vt52Mode
== false) {
3954 singleshift
= Singleshift
.SS3
;
3961 if ((ch
>= 0x51) && (ch
<= 0x57)) {
3977 if (vt52Mode
== true) {
3978 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
3986 if (vt52Mode
== true) {
3988 // Send string directly to remote side
3989 writeRemote("\033/Z");
3992 // Send string directly to remote side
3993 writeRemote(deviceTypeResponse());
4004 // VT52 cannot get to any of these other states
4005 if (vt52Mode
== true) {
4010 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4017 // RIS - Reset to initial state
4019 // Do I clear screen too? I think so...
4020 eraseScreen(0, 0, height
- 1, width
- 1, false);
4021 cursorPosition(0, 0);
4035 if ((type
== DeviceType
.VT220
)
4036 || (type
== DeviceType
.XTERM
)) {
4038 // VT220 lockshift G2 into GL
4039 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4044 if ((type
== DeviceType
.VT220
)
4045 || (type
== DeviceType
.XTERM
)) {
4047 // VT220 lockshift G3 into GL
4048 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4066 if ((type
== DeviceType
.VT220
)
4067 || (type
== DeviceType
.XTERM
)) {
4069 // VT220 lockshift G3 into GR
4070 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4075 if ((type
== DeviceType
.VT220
)
4076 || (type
== DeviceType
.XTERM
)) {
4078 // VT220 lockshift G2 into GR
4079 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4085 if ((type
== DeviceType
.VT220
)
4086 || (type
== DeviceType
.XTERM
)) {
4088 // VT220 lockshift G1 into GR
4089 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4099 // 0x5B goes to CSI_ENTRY
4101 scanState
= ScanState
.CSI_ENTRY
;
4104 // 0x5D goes to OSC_STRING
4106 scanState
= ScanState
.OSC_STRING
;
4109 // 0x50 goes to DCS_ENTRY
4111 scanState
= ScanState
.DCS_ENTRY
;
4114 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4115 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4116 scanState
= ScanState
.SOSPMAPC_STRING
;
4121 case ESCAPE_INTERMEDIATE
:
4122 // 00-17, 19, 1C-1F --> execute
4124 handleControlChar(ch
);
4127 // 20-2F --> collect
4128 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4132 // 30-7E --> dispatch, then switch to GROUND
4133 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4136 if ((collectBuffer
.length() == 1)
4137 && (collectBuffer
.charAt(0) == '(')) {
4138 // G0 --> Special graphics
4139 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4141 if ((collectBuffer
.length() == 1)
4142 && (collectBuffer
.charAt(0) == ')')) {
4143 // G1 --> Special graphics
4144 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4146 if ((type
== DeviceType
.VT220
)
4147 || (type
== DeviceType
.XTERM
)) {
4149 if ((collectBuffer
.length() == 1)
4150 && (collectBuffer
.charAt(0) == '*')) {
4151 // G2 --> Special graphics
4152 currentState
.g2Charset
= CharacterSet
.DRAWING
;
4154 if ((collectBuffer
.length() == 1)
4155 && (collectBuffer
.charAt(0) == '+')) {
4156 // G3 --> Special graphics
4157 currentState
.g3Charset
= CharacterSet
.DRAWING
;
4162 if ((collectBuffer
.length() == 1)
4163 && (collectBuffer
.charAt(0) == '(')) {
4164 // G0 --> Alternate character ROM standard character set
4165 currentState
.g0Charset
= CharacterSet
.ROM
;
4167 if ((collectBuffer
.length() == 1)
4168 && (collectBuffer
.charAt(0) == ')')) {
4169 // G1 --> Alternate character ROM standard character set
4170 currentState
.g1Charset
= CharacterSet
.ROM
;
4174 if ((collectBuffer
.length() == 1)
4175 && (collectBuffer
.charAt(0) == '(')) {
4176 // G0 --> Alternate character ROM special graphics
4177 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4179 if ((collectBuffer
.length() == 1)
4180 && (collectBuffer
.charAt(0) == ')')) {
4181 // G1 --> Alternate character ROM special graphics
4182 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4186 if ((collectBuffer
.length() == 1)
4187 && (collectBuffer
.charAt(0) == '#')) {
4188 // DECDHL - Double-height line (top half)
4193 if ((collectBuffer
.length() == 1)
4194 && (collectBuffer
.charAt(0) == '#')) {
4195 // DECDHL - Double-height line (bottom half)
4198 if ((type
== DeviceType
.VT220
)
4199 || (type
== DeviceType
.XTERM
)) {
4201 if ((collectBuffer
.length() == 1)
4202 && (collectBuffer
.charAt(0) == '(')) {
4204 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4206 if ((collectBuffer
.length() == 1)
4207 && (collectBuffer
.charAt(0) == ')')) {
4209 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4211 if ((collectBuffer
.length() == 1)
4212 && (collectBuffer
.charAt(0) == '*')) {
4214 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4216 if ((collectBuffer
.length() == 1)
4217 && (collectBuffer
.charAt(0) == '+')) {
4219 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4224 if ((collectBuffer
.length() == 1)
4225 && (collectBuffer
.charAt(0) == '#')) {
4226 // DECSWL - Single-width line
4229 if ((type
== DeviceType
.VT220
)
4230 || (type
== DeviceType
.XTERM
)) {
4232 if ((collectBuffer
.length() == 1)
4233 && (collectBuffer
.charAt(0) == '(')) {
4235 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4237 if ((collectBuffer
.length() == 1)
4238 && (collectBuffer
.charAt(0) == ')')) {
4240 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4242 if ((collectBuffer
.length() == 1)
4243 && (collectBuffer
.charAt(0) == '*')) {
4245 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4247 if ((collectBuffer
.length() == 1)
4248 && (collectBuffer
.charAt(0) == '+')) {
4250 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4255 if ((collectBuffer
.length() == 1)
4256 && (collectBuffer
.charAt(0) == '#')) {
4257 // DECDWL - Double-width line
4260 if ((type
== DeviceType
.VT220
)
4261 || (type
== DeviceType
.XTERM
)) {
4263 if ((collectBuffer
.length() == 1)
4264 && (collectBuffer
.charAt(0) == '(')) {
4266 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4268 if ((collectBuffer
.length() == 1)
4269 && (collectBuffer
.charAt(0) == ')')) {
4271 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4273 if ((collectBuffer
.length() == 1)
4274 && (collectBuffer
.charAt(0) == '*')) {
4276 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4278 if ((collectBuffer
.length() == 1)
4279 && (collectBuffer
.charAt(0) == '+')) {
4281 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4286 if ((type
== DeviceType
.VT220
)
4287 || (type
== DeviceType
.XTERM
)) {
4289 if ((collectBuffer
.length() == 1)
4290 && (collectBuffer
.charAt(0) == '(')) {
4292 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4294 if ((collectBuffer
.length() == 1)
4295 && (collectBuffer
.charAt(0) == ')')) {
4297 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4299 if ((collectBuffer
.length() == 1)
4300 && (collectBuffer
.charAt(0) == '*')) {
4302 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4304 if ((collectBuffer
.length() == 1)
4305 && (collectBuffer
.charAt(0) == '+')) {
4307 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4312 if ((collectBuffer
.length() == 1)
4313 && (collectBuffer
.charAt(0) == '#')) {
4314 // DECALN - Screen alignment display
4323 if ((type
== DeviceType
.VT220
)
4324 || (type
== DeviceType
.XTERM
)) {
4326 if ((collectBuffer
.length() == 1)
4327 && (collectBuffer
.charAt(0) == '(')) {
4328 // G0 --> DEC_SUPPLEMENTAL
4329 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4331 if ((collectBuffer
.length() == 1)
4332 && (collectBuffer
.charAt(0) == ')')) {
4333 // G1 --> DEC_SUPPLEMENTAL
4334 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4336 if ((collectBuffer
.length() == 1)
4337 && (collectBuffer
.charAt(0) == '*')) {
4338 // G2 --> DEC_SUPPLEMENTAL
4339 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4341 if ((collectBuffer
.length() == 1)
4342 && (collectBuffer
.charAt(0) == '+')) {
4343 // G3 --> DEC_SUPPLEMENTAL
4344 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4349 if ((type
== DeviceType
.VT220
)
4350 || (type
== DeviceType
.XTERM
)) {
4352 if ((collectBuffer
.length() == 1)
4353 && (collectBuffer
.charAt(0) == '(')) {
4355 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4357 if ((collectBuffer
.length() == 1)
4358 && (collectBuffer
.charAt(0) == ')')) {
4360 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4362 if ((collectBuffer
.length() == 1)
4363 && (collectBuffer
.charAt(0) == '*')) {
4365 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4367 if ((collectBuffer
.length() == 1)
4368 && (collectBuffer
.charAt(0) == '+')) {
4370 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4379 if ((collectBuffer
.length() == 1)
4380 && (collectBuffer
.charAt(0) == '(')) {
4381 // G0 --> United Kingdom set
4382 currentState
.g0Charset
= CharacterSet
.UK
;
4384 if ((collectBuffer
.length() == 1)
4385 && (collectBuffer
.charAt(0) == ')')) {
4386 // G1 --> United Kingdom set
4387 currentState
.g1Charset
= CharacterSet
.UK
;
4389 if ((type
== DeviceType
.VT220
)
4390 || (type
== DeviceType
.XTERM
)) {
4392 if ((collectBuffer
.length() == 1)
4393 && (collectBuffer
.charAt(0) == '*')) {
4394 // G2 --> United Kingdom set
4395 currentState
.g2Charset
= CharacterSet
.UK
;
4397 if ((collectBuffer
.length() == 1)
4398 && (collectBuffer
.charAt(0) == '+')) {
4399 // G3 --> United Kingdom set
4400 currentState
.g3Charset
= CharacterSet
.UK
;
4405 if ((collectBuffer
.length() == 1)
4406 && (collectBuffer
.charAt(0) == '(')) {
4408 currentState
.g0Charset
= CharacterSet
.US
;
4410 if ((collectBuffer
.length() == 1)
4411 && (collectBuffer
.charAt(0) == ')')) {
4413 currentState
.g1Charset
= CharacterSet
.US
;
4415 if ((type
== DeviceType
.VT220
)
4416 || (type
== DeviceType
.XTERM
)) {
4418 if ((collectBuffer
.length() == 1)
4419 && (collectBuffer
.charAt(0) == '*')) {
4421 currentState
.g2Charset
= CharacterSet
.US
;
4423 if ((collectBuffer
.length() == 1)
4424 && (collectBuffer
.charAt(0) == '+')) {
4426 currentState
.g3Charset
= CharacterSet
.US
;
4431 if ((type
== DeviceType
.VT220
)
4432 || (type
== DeviceType
.XTERM
)) {
4434 if ((collectBuffer
.length() == 1)
4435 && (collectBuffer
.charAt(0) == '(')) {
4437 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4439 if ((collectBuffer
.length() == 1)
4440 && (collectBuffer
.charAt(0) == ')')) {
4442 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4444 if ((collectBuffer
.length() == 1)
4445 && (collectBuffer
.charAt(0) == '*')) {
4447 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4449 if ((collectBuffer
.length() == 1)
4450 && (collectBuffer
.charAt(0) == '+')) {
4452 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4459 if ((type
== DeviceType
.VT220
)
4460 || (type
== DeviceType
.XTERM
)) {
4462 if ((collectBuffer
.length() == 1)
4463 && (collectBuffer
.charAt(0) == '(')) {
4465 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4467 if ((collectBuffer
.length() == 1)
4468 && (collectBuffer
.charAt(0) == ')')) {
4470 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4472 if ((collectBuffer
.length() == 1)
4473 && (collectBuffer
.charAt(0) == '*')) {
4475 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4477 if ((collectBuffer
.length() == 1)
4478 && (collectBuffer
.charAt(0) == '+')) {
4480 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4485 if ((type
== DeviceType
.VT220
)
4486 || (type
== DeviceType
.XTERM
)) {
4488 if ((collectBuffer
.length() == 1)
4489 && (collectBuffer
.charAt(0) == ' ')) {
4496 if ((type
== DeviceType
.VT220
)
4497 || (type
== DeviceType
.XTERM
)) {
4499 if ((collectBuffer
.length() == 1)
4500 && (collectBuffer
.charAt(0) == ' ')) {
4507 if ((type
== DeviceType
.VT220
)
4508 || (type
== DeviceType
.XTERM
)) {
4510 if ((collectBuffer
.length() == 1)
4511 && (collectBuffer
.charAt(0) == '(')) {
4513 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4515 if ((collectBuffer
.length() == 1)
4516 && (collectBuffer
.charAt(0) == ')')) {
4518 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4520 if ((collectBuffer
.length() == 1)
4521 && (collectBuffer
.charAt(0) == '*')) {
4523 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4525 if ((collectBuffer
.length() == 1)
4526 && (collectBuffer
.charAt(0) == '+')) {
4528 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4536 if ((type
== DeviceType
.VT220
)
4537 || (type
== DeviceType
.XTERM
)) {
4539 if ((collectBuffer
.length() == 1)
4540 && (collectBuffer
.charAt(0) == '(')) {
4542 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4544 if ((collectBuffer
.length() == 1)
4545 && (collectBuffer
.charAt(0) == ')')) {
4547 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4549 if ((collectBuffer
.length() == 1)
4550 && (collectBuffer
.charAt(0) == '*')) {
4552 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4554 if ((collectBuffer
.length() == 1)
4555 && (collectBuffer
.charAt(0) == '+')) {
4557 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4568 if ((type
== DeviceType
.VT220
)
4569 || (type
== DeviceType
.XTERM
)) {
4571 if ((collectBuffer
.length() == 1)
4572 && (collectBuffer
.charAt(0) == '(')) {
4574 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4576 if ((collectBuffer
.length() == 1)
4577 && (collectBuffer
.charAt(0) == ')')) {
4579 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4581 if ((collectBuffer
.length() == 1)
4582 && (collectBuffer
.charAt(0) == '*')) {
4584 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4586 if ((collectBuffer
.length() == 1)
4587 && (collectBuffer
.charAt(0) == '+')) {
4589 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4594 if ((type
== DeviceType
.VT220
)
4595 || (type
== DeviceType
.XTERM
)) {
4597 if ((collectBuffer
.length() == 1)
4598 && (collectBuffer
.charAt(0) == '(')) {
4600 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4602 if ((collectBuffer
.length() == 1)
4603 && (collectBuffer
.charAt(0) == ')')) {
4605 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4607 if ((collectBuffer
.length() == 1)
4608 && (collectBuffer
.charAt(0) == '*')) {
4610 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
4612 if ((collectBuffer
.length() == 1)
4613 && (collectBuffer
.charAt(0) == '+')) {
4615 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
4627 if ((type
== DeviceType
.VT220
)
4628 || (type
== DeviceType
.XTERM
)) {
4630 if ((collectBuffer
.length() == 1)
4631 && (collectBuffer
.charAt(0) == '(')) {
4633 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
4635 if ((collectBuffer
.length() == 1)
4636 && (collectBuffer
.charAt(0) == ')')) {
4638 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
4640 if ((collectBuffer
.length() == 1)
4641 && (collectBuffer
.charAt(0) == '*')) {
4643 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
4645 if ((collectBuffer
.length() == 1)
4646 && (collectBuffer
.charAt(0) == '+')) {
4648 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
4653 if ((type
== DeviceType
.VT220
)
4654 || (type
== DeviceType
.XTERM
)) {
4656 if ((collectBuffer
.length() == 1)
4657 && (collectBuffer
.charAt(0) == '(')) {
4659 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
4661 if ((collectBuffer
.length() == 1)
4662 && (collectBuffer
.charAt(0) == ')')) {
4664 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
4666 if ((collectBuffer
.length() == 1)
4667 && (collectBuffer
.charAt(0) == '*')) {
4669 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
4671 if ((collectBuffer
.length() == 1)
4672 && (collectBuffer
.charAt(0) == '+')) {
4674 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
4721 // 0x9C goes to GROUND
4729 // 00-17, 19, 1C-1F --> execute
4731 handleControlChar(ch
);
4734 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
4735 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4737 scanState
= ScanState
.CSI_INTERMEDIATE
;
4740 // 30-39, 3B --> param, then switch to CSI_PARAM
4741 if ((ch
>= '0') && (ch
<= '9')) {
4743 scanState
= ScanState
.CSI_PARAM
;
4747 scanState
= ScanState
.CSI_PARAM
;
4750 // 3C-3F --> collect, then switch to CSI_PARAM
4751 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
4753 scanState
= ScanState
.CSI_PARAM
;
4756 // 40-7E --> dispatch, then switch to GROUND
4757 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
4760 // ICH - Insert character
4768 // CUD - Cursor down
4772 // CUF - Cursor forward
4776 // CUB - Cursor backward
4780 // CNL - Cursor down and to column 1
4781 if (type
== DeviceType
.XTERM
) {
4786 // CPL - Cursor up and to column 1
4787 if (type
== DeviceType
.XTERM
) {
4792 // CHA - Cursor to column # in current row
4793 if (type
== DeviceType
.XTERM
) {
4798 // CUP - Cursor position
4802 // CHT - Cursor forward X tab stops (default 1)
4803 if (type
== DeviceType
.XTERM
) {
4808 // ED - Erase in display
4812 // EL - Erase in line
4827 // DCH - Delete character
4834 // Scroll up X lines (default 1)
4835 if (type
== DeviceType
.XTERM
) {
4840 // Scroll down X lines (default 1)
4841 if (type
== DeviceType
.XTERM
) {
4850 if ((type
== DeviceType
.VT220
)
4851 || (type
== DeviceType
.XTERM
)) {
4853 // ECH - Erase character
4860 // CBT - Cursor backward X tab stops (default 1)
4861 if (type
== DeviceType
.XTERM
) {
4872 // HPA - Cursor to column # in current row. Same as CHA
4873 if (type
== DeviceType
.XTERM
) {
4878 // HPR - Cursor right. Same as CUF
4879 if (type
== DeviceType
.XTERM
) {
4884 // REP - Repeat last char X times
4885 if (type
== DeviceType
.XTERM
) {
4890 // DA - Device attributes
4894 // VPA - Cursor to row, current column.
4895 if (type
== DeviceType
.XTERM
) {
4900 // VPR - Cursor down. Same as CUD
4901 if (type
== DeviceType
.XTERM
) {
4906 // HVP - Horizontal and vertical position
4910 // TBC - Tabulation clear
4914 // Sets an ANSI or DEC private toggle
4918 if ((type
== DeviceType
.VT220
)
4919 || (type
== DeviceType
.XTERM
)) {
4921 // Printer functions
4929 // Sets an ANSI or DEC private toggle
4933 // SGR - Select graphics rendition
4937 // DSR - Device status report
4944 // DECLL - Load leds
4948 // DECSTBM - Set top and bottom margins
4952 // Save cursor (ANSI.SYS)
4953 if (type
== DeviceType
.XTERM
) {
4954 savedState
.cursorX
= currentState
.cursorX
;
4955 savedState
.cursorY
= currentState
.cursorY
;
4961 // Restore cursor (ANSI.SYS)
4962 if (type
== DeviceType
.XTERM
) {
4963 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
4970 // DECREQTPARM - Request terminal parameters
4986 // 0x9C goes to GROUND
4991 // 0x3A goes to CSI_IGNORE
4993 scanState
= ScanState
.CSI_IGNORE
;
4998 // 00-17, 19, 1C-1F --> execute
5000 handleControlChar(ch
);
5003 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5004 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5006 scanState
= ScanState
.CSI_INTERMEDIATE
;
5009 // 30-39, 3B --> param
5010 if ((ch
>= '0') && (ch
<= '9')) {
5017 // 0x3A goes to CSI_IGNORE
5019 scanState
= ScanState
.CSI_IGNORE
;
5021 // 0x3C-3F goes to CSI_IGNORE
5022 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5023 scanState
= ScanState
.CSI_IGNORE
;
5026 // 40-7E --> dispatch, then switch to GROUND
5027 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5030 // ICH - Insert character
5038 // CUD - Cursor down
5042 // CUF - Cursor forward
5046 // CUB - Cursor backward
5050 // CNL - Cursor down and to column 1
5051 if (type
== DeviceType
.XTERM
) {
5056 // CPL - Cursor up and to column 1
5057 if (type
== DeviceType
.XTERM
) {
5062 // CHA - Cursor to column # in current row
5063 if (type
== DeviceType
.XTERM
) {
5068 // CUP - Cursor position
5072 // CHT - Cursor forward X tab stops (default 1)
5073 if (type
== DeviceType
.XTERM
) {
5078 // ED - Erase in display
5082 // EL - Erase in line
5097 // DCH - Delete character
5104 // Scroll up X lines (default 1)
5105 if (type
== DeviceType
.XTERM
) {
5110 // Scroll down X lines (default 1)
5111 if (type
== DeviceType
.XTERM
) {
5120 if ((type
== DeviceType
.VT220
)
5121 || (type
== DeviceType
.XTERM
)) {
5123 // ECH - Erase character
5130 // CBT - Cursor backward X tab stops (default 1)
5131 if (type
== DeviceType
.XTERM
) {
5142 // HPA - Cursor to column # in current row. Same as CHA
5143 if (type
== DeviceType
.XTERM
) {
5148 // HPR - Cursor right. Same as CUF
5149 if (type
== DeviceType
.XTERM
) {
5154 // REP - Repeat last char X times
5155 if (type
== DeviceType
.XTERM
) {
5160 // DA - Device attributes
5164 // VPA - Cursor to row, current column.
5165 if (type
== DeviceType
.XTERM
) {
5170 // VPR - Cursor down. Same as CUD
5171 if (type
== DeviceType
.XTERM
) {
5176 // HVP - Horizontal and vertical position
5180 // TBC - Tabulation clear
5184 // Sets an ANSI or DEC private toggle
5188 if ((type
== DeviceType
.VT220
)
5189 || (type
== DeviceType
.XTERM
)) {
5191 // Printer functions
5199 // Sets an ANSI or DEC private toggle
5203 // SGR - Select graphics rendition
5207 // DSR - Device status report
5214 // DECLL - Load leds
5218 // DECSTBM - Set top and bottom margins
5228 // DECREQTPARM - Request terminal parameters
5245 case CSI_INTERMEDIATE
:
5246 // 00-17, 19, 1C-1F --> execute
5248 handleControlChar(ch
);
5251 // 20-2F --> collect
5252 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5256 // 0x30-3F goes to CSI_IGNORE
5257 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5258 scanState
= ScanState
.CSI_IGNORE
;
5261 // 40-7E --> dispatch, then switch to GROUND
5262 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5314 if (((type
== DeviceType
.VT220
)
5315 || (type
== DeviceType
.XTERM
))
5316 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5318 // DECSCL - compatibility level
5321 if ((type
== DeviceType
.XTERM
)
5322 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
5324 // DECSTR - Soft terminal reset
5329 if (((type
== DeviceType
.VT220
)
5330 || (type
== DeviceType
.XTERM
))
5331 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5359 // 00-17, 19, 1C-1F --> execute
5361 handleControlChar(ch
);
5364 // 20-2F --> collect
5365 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5369 // 40-7E --> ignore, then switch to GROUND
5370 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5374 // 20-3F, 7F --> ignore
5380 // 0x9C goes to GROUND
5385 // 0x1B 0x5C goes to GROUND
5390 if ((collectBuffer
.length() > 0)
5391 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5397 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5398 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5400 scanState
= ScanState
.DCS_INTERMEDIATE
;
5403 // 30-39, 3B --> param, then switch to DCS_PARAM
5404 if ((ch
>= '0') && (ch
<= '9')) {
5406 scanState
= ScanState
.DCS_PARAM
;
5410 scanState
= ScanState
.DCS_PARAM
;
5413 // 3C-3F --> collect, then switch to DCS_PARAM
5414 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5416 scanState
= ScanState
.DCS_PARAM
;
5419 // 00-17, 19, 1C-1F, 7F --> ignore
5421 // 0x3A goes to DCS_IGNORE
5423 scanState
= ScanState
.DCS_IGNORE
;
5426 // 0x40-7E goes to DCS_PASSTHROUGH
5427 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5428 scanState
= ScanState
.DCS_PASSTHROUGH
;
5432 case DCS_INTERMEDIATE
:
5434 // 0x9C goes to GROUND
5439 // 0x1B 0x5C goes to GROUND
5444 if ((collectBuffer
.length() > 0) &&
5445 (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5451 // 0x30-3F goes to DCS_IGNORE
5452 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5453 scanState
= ScanState
.DCS_IGNORE
;
5456 // 0x40-7E goes to DCS_PASSTHROUGH
5457 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5458 scanState
= ScanState
.DCS_PASSTHROUGH
;
5461 // 00-17, 19, 1C-1F, 7F --> ignore
5466 // 0x9C goes to GROUND
5471 // 0x1B 0x5C goes to GROUND
5476 if ((collectBuffer
.length() > 0) &&
5477 (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5483 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5484 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5486 scanState
= ScanState
.DCS_INTERMEDIATE
;
5489 // 30-39, 3B --> param
5490 if ((ch
>= '0') && (ch
<= '9')) {
5497 // 00-17, 19, 1C-1F, 7F --> ignore
5499 // 0x3A, 3C-3F goes to DCS_IGNORE
5501 scanState
= ScanState
.DCS_IGNORE
;
5503 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5504 scanState
= ScanState
.DCS_IGNORE
;
5507 // 0x40-7E goes to DCS_PASSTHROUGH
5508 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5509 scanState
= ScanState
.DCS_PASSTHROUGH
;
5513 case DCS_PASSTHROUGH
:
5514 // 0x9C goes to GROUND
5519 // 0x1B 0x5C goes to GROUND
5524 if ((collectBuffer
.length() > 0)
5525 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5531 // 00-17, 19, 1C-1F, 20-7E --> put
5539 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5542 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5551 // 00-17, 19, 1C-1F, 20-7F --> ignore
5553 // 0x9C goes to GROUND
5560 case SOSPMAPC_STRING
:
5561 // 00-17, 19, 1C-1F, 20-7F --> ignore
5563 // 0x9C goes to GROUND
5571 // Special case for Xterm: OSC can pass control characters
5572 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5576 // 00-17, 19, 1C-1F --> ignore
5578 // 20-7F --> osc_put
5579 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5583 // 0x9C goes to GROUND
5590 case VT52_DIRECT_CURSOR_ADDRESS
:
5591 // This is a special case for the VT52 sequence "ESC Y l c"
5592 if (collectBuffer
.length() == 0) {
5594 } else if (collectBuffer
.length() == 1) {
5595 // We've got the two characters, one in the buffer and the
5597 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
5606 * Expose current cursor X to outside world.
5608 * @return current cursor X
5610 public final int getCursorX() {
5611 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
5612 return currentState
.cursorX
* 2;
5614 return currentState
.cursorX
;
5618 * Expose current cursor Y to outside world.
5620 * @return current cursor Y
5622 public final int getCursorY() {
5623 return currentState
.cursorY
;
5627 * Read function runs on a separate thread.
5630 boolean utf8
= false;
5631 boolean done
= false;
5633 if (type
== DeviceType
.XTERM
) {
5637 // available() will often return > 1, so we need to read in chunks to
5639 char [] readBufferUTF8
= null;
5640 byte [] readBuffer
= null;
5642 readBufferUTF8
= new char[128];
5644 readBuffer
= new byte[128];
5647 while (!done
&& !stopReaderThread
) {
5649 int n
= inputStream
.available();
5650 // System.err.printf("available() %d\n", n); System.err.flush();
5652 if (readBufferUTF8
.length
< n
) {
5653 // The buffer wasn't big enough, make it huger
5654 int newSizeHalf
= Math
.max(readBufferUTF8
.length
, n
);
5656 readBufferUTF8
= new char[newSizeHalf
* 2];
5659 if (readBuffer
.length
< n
) {
5660 // The buffer wasn't big enough, make it huger
5661 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
5662 readBuffer
= new byte[newSizeHalf
* 2];
5668 rc
= input
.read(readBufferUTF8
, 0,
5669 readBufferUTF8
.length
);
5671 rc
= inputStream
.read(readBuffer
, 0,
5674 // System.err.printf("read() %d\n", rc); System.err.flush();
5679 for (int i
= 0; i
< rc
; i
++) {
5682 ch
= readBufferUTF8
[i
];
5686 // Don't step on UI events
5687 synchronized (this) {
5692 // System.err.println("end while loop"); System.err.flush();
5693 } catch (IOException e
) {
5694 e
.printStackTrace();
5697 } // while ((done == false) && (stopReaderThread == false))
5699 // Let the rest of the world know that I am done.
5700 stopReaderThread
= true;
5702 // System.err.println("*** run() exiting..."); System.err.flush();