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
) {
1102 * - Parse the mouse requests from the remote side regarding protocol
1105 * - Send mouse events to the other side.
1107 * - Handle the cursor (double invert).
1110 // System.err.printf("Mouse: %s\n", mouse);
1113 if (mouseEncoding != MouseEncoding.UTF8) {
1114 // We only support UTF8 encoding, bail out now.
1119 switch (mouseProtocol
) {
1126 // Only report button presses
1127 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1133 // Only report button presses and releases
1134 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1135 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1143 * Only report button presses, button releases, and motions that
1144 * have a button down (i.e. drag-and-drop).
1146 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1147 if (!mouse
.getMouse1()
1148 && !mouse
.getMouse2()
1149 && !mouse
.getMouse3()
1150 && !mouse
.getMouseWheelUp()
1151 && !mouse
.getMouseWheelDown()
1159 // Report everything
1163 // Now encode the event
1164 StringBuilder sb
= new StringBuilder(6);
1165 sb
.append((char) 0x1B);
1168 if (mouse
.getMouse1()) {
1169 sb
.append((char) (0x00 + 32));
1170 } else if (mouse
.getMouse2()) {
1171 sb
.append((char) (0x01 + 32));
1172 } else if (mouse
.getMouse3()) {
1173 sb
.append((char) (0x02 + 32));
1174 } else if (mouse
.getMouseWheelUp()) {
1175 sb
.append((char) (0x04 + 64));
1176 } else if (mouse
.getMouseWheelDown()) {
1177 sb
.append((char) (0x05 + 64));
1179 sb
.append((char) (0x03 + 32));
1182 sb
.append((char) (mouse
.getX() + 33));
1183 sb
.append((char) (mouse
.getY() + 33));
1185 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1186 writeRemote(sb
.toString());
1190 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1191 * send to the remote side.
1193 * @param keypress keypress received from the local user
1195 public void keypress(final TKeypress keypress
) {
1196 writeRemote(keypressToString(keypress
));
1200 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1202 * @param keypress keypress received from the local user
1203 * @return string to transmit to the remote side
1205 private String
keypressToString(final TKeypress keypress
) {
1207 if ((fullDuplex
== false) && (!keypress
.getIsKey())) {
1209 * If this is a control character, process it like it came from
1212 if (keypress
.getCh() < 0x20) {
1213 handleControlChar(keypress
.getCh());
1215 // Local echo for everything else
1216 printCharacter(keypress
.getCh());
1220 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1225 // Handle control characters
1226 if ((keypress
.getCtrl()) && (!keypress
.getIsKey())) {
1227 StringBuilder sb
= new StringBuilder();
1228 char ch
= keypress
.getCh();
1231 return sb
.toString();
1234 // Handle alt characters
1235 if ((keypress
.getAlt()) && (!keypress
.getIsKey())) {
1236 StringBuilder sb
= new StringBuilder("\033");
1237 char ch
= keypress
.getCh();
1239 return sb
.toString();
1242 if (keypress
.equals(kbBackspace
)) {
1255 if (keypress
.equals(kbLeft
)) {
1256 switch (arrowKeyMode
) {
1266 if (keypress
.equals(kbRight
)) {
1267 switch (arrowKeyMode
) {
1277 if (keypress
.equals(kbUp
)) {
1278 switch (arrowKeyMode
) {
1288 if (keypress
.equals(kbDown
)) {
1289 switch (arrowKeyMode
) {
1299 if (keypress
.equals(kbHome
)) {
1300 switch (arrowKeyMode
) {
1310 if (keypress
.equals(kbEnd
)) {
1311 switch (arrowKeyMode
) {
1321 if (keypress
.equals(kbF1
)) {
1329 if (keypress
.equals(kbF2
)) {
1337 if (keypress
.equals(kbF3
)) {
1345 if (keypress
.equals(kbF4
)) {
1353 if (keypress
.equals(kbF5
)) {
1366 if (keypress
.equals(kbF6
)) {
1379 if (keypress
.equals(kbF7
)) {
1392 if (keypress
.equals(kbF8
)) {
1405 if (keypress
.equals(kbF9
)) {
1418 if (keypress
.equals(kbF10
)) {
1431 if (keypress
.equals(kbF11
)) {
1435 if (keypress
.equals(kbF12
)) {
1439 if (keypress
.equals(kbShiftF1
)) {
1447 if (keypress
.equals(kbShiftF2
)) {
1455 if (keypress
.equals(kbShiftF3
)) {
1463 if (keypress
.equals(kbShiftF4
)) {
1471 if (keypress
.equals(kbShiftF5
)) {
1473 return "\033[15;2~";
1476 if (keypress
.equals(kbShiftF6
)) {
1478 return "\033[17;2~";
1481 if (keypress
.equals(kbShiftF7
)) {
1483 return "\033[18;2~";
1486 if (keypress
.equals(kbShiftF8
)) {
1488 return "\033[19;2~";
1491 if (keypress
.equals(kbShiftF9
)) {
1493 return "\033[20;2~";
1496 if (keypress
.equals(kbShiftF10
)) {
1498 return "\033[21;2~";
1501 if (keypress
.equals(kbShiftF11
)) {
1503 return "\033[23;2~";
1506 if (keypress
.equals(kbShiftF12
)) {
1508 return "\033[24;2~";
1511 if (keypress
.equals(kbCtrlF1
)) {
1519 if (keypress
.equals(kbCtrlF2
)) {
1527 if (keypress
.equals(kbCtrlF3
)) {
1535 if (keypress
.equals(kbCtrlF4
)) {
1543 if (keypress
.equals(kbCtrlF5
)) {
1545 return "\033[15;5~";
1548 if (keypress
.equals(kbCtrlF6
)) {
1550 return "\033[17;5~";
1553 if (keypress
.equals(kbCtrlF7
)) {
1555 return "\033[18;5~";
1558 if (keypress
.equals(kbCtrlF8
)) {
1560 return "\033[19;5~";
1563 if (keypress
.equals(kbCtrlF9
)) {
1565 return "\033[20;5~";
1568 if (keypress
.equals(kbCtrlF10
)) {
1570 return "\033[21;5~";
1573 if (keypress
.equals(kbCtrlF11
)) {
1575 return "\033[23;5~";
1578 if (keypress
.equals(kbCtrlF12
)) {
1580 return "\033[24;5~";
1583 if (keypress
.equals(kbPgUp
)) {
1588 if (keypress
.equals(kbPgDn
)) {
1593 if (keypress
.equals(kbIns
)) {
1598 if (keypress
.equals(kbShiftIns
)) {
1599 // This is what xterm sends for SHIFT-INS
1601 // This is what xterm sends for CTRL-INS
1602 // return "\033[2;5~";
1605 if (keypress
.equals(kbShiftDel
)) {
1606 // This is what xterm sends for SHIFT-DEL
1608 // This is what xterm sends for CTRL-DEL
1609 // return "\033[3;5~";
1612 if (keypress
.equals(kbDel
)) {
1613 // Delete sends real delete for VTxxx
1615 // return "\033[3~";
1618 if (keypress
.equals(kbEnter
)) {
1622 if (keypress
.equals(kbEsc
)) {
1626 if (keypress
.equals(kbAltEsc
)) {
1630 if (keypress
.equals(kbTab
)) {
1634 // Non-alt, non-ctrl characters
1635 if (!keypress
.getIsKey()) {
1636 StringBuilder sb
= new StringBuilder();
1637 sb
.append(keypress
.getCh());
1638 return sb
.toString();
1644 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1647 * @param ch 8-bit character from the remote side
1648 * @param charsetGl character set defined for GL
1649 * @param charsetGr character set defined for GR
1650 * @return character to display on the screen
1652 private char mapCharacterCharset(final char ch
,
1653 final CharacterSet charsetGl
,
1654 final CharacterSet charsetGr
) {
1656 int lookupChar
= ch
;
1657 CharacterSet lookupCharset
= charsetGl
;
1660 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
1661 lookupCharset
= charsetGr
;
1665 switch (lookupCharset
) {
1668 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
1671 return DECCharacterSets
.UK
[lookupChar
];
1674 return DECCharacterSets
.US_ASCII
[lookupChar
];
1677 return DECCharacterSets
.NL
[lookupChar
];
1680 return DECCharacterSets
.FI
[lookupChar
];
1683 return DECCharacterSets
.FR
[lookupChar
];
1686 return DECCharacterSets
.FR_CA
[lookupChar
];
1689 return DECCharacterSets
.DE
[lookupChar
];
1692 return DECCharacterSets
.IT
[lookupChar
];
1695 return DECCharacterSets
.NO
[lookupChar
];
1698 return DECCharacterSets
.ES
[lookupChar
];
1701 return DECCharacterSets
.SV
[lookupChar
];
1704 return DECCharacterSets
.SWISS
[lookupChar
];
1706 case DEC_SUPPLEMENTAL
:
1707 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
1710 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
1713 return DECCharacterSets
.US_ASCII
[lookupChar
];
1716 return DECCharacterSets
.US_ASCII
[lookupChar
];
1719 throw new IllegalArgumentException("Invalid character set value: "
1725 * Map an 8-bit byte into a printable character.
1727 * @param ch either 8-bit or Unicode character from the remote side
1728 * @return character to display on the screen
1730 private char mapCharacter(final char ch
) {
1732 // Unicode character, just return it
1736 CharacterSet charsetGl
= currentState
.g0Charset
;
1737 CharacterSet charsetGr
= currentState
.grCharset
;
1739 if (vt52Mode
== true) {
1740 if (shiftOut
== true) {
1741 // Shifted out character, pull from VT52 graphics
1742 charsetGl
= currentState
.g1Charset
;
1743 charsetGr
= CharacterSet
.US
;
1746 charsetGl
= currentState
.g0Charset
;
1747 charsetGr
= CharacterSet
.US
;
1750 // Pull the character
1751 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1755 if (shiftOut
== true) {
1756 // Shifted out character, pull from G1
1757 charsetGl
= currentState
.g1Charset
;
1758 charsetGr
= currentState
.grCharset
;
1760 // Pull the character
1761 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1765 if (singleshift
== Singleshift
.SS2
) {
1767 singleshift
= Singleshift
.NONE
;
1769 // Shifted out character, pull from G2
1770 charsetGl
= currentState
.g2Charset
;
1771 charsetGr
= currentState
.grCharset
;
1775 if (singleshift
== Singleshift
.SS3
) {
1777 singleshift
= Singleshift
.NONE
;
1779 // Shifted out character, pull from G3
1780 charsetGl
= currentState
.g3Charset
;
1781 charsetGr
= currentState
.grCharset
;
1784 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
1785 // Check for locking shift
1787 switch (currentState
.glLockshift
) {
1800 charsetGl
= currentState
.g2Charset
;
1805 charsetGl
= currentState
.g3Charset
;
1810 charsetGl
= currentState
.g0Charset
;
1814 switch (currentState
.grLockshift
) {
1824 charsetGr
= currentState
.g1Charset
;
1829 charsetGr
= currentState
.g2Charset
;
1834 charsetGr
= currentState
.g3Charset
;
1839 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
1846 // Pull the character
1847 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1851 * Scroll the text within a scrolling region up n lines.
1853 * @param regionTop top row of the scrolling region
1854 * @param regionBottom bottom row of the scrolling region
1855 * @param n number of lines to scroll
1857 private void scrollingRegionScrollUp(final int regionTop
,
1858 final int regionBottom
, final int n
) {
1860 if (regionTop
>= regionBottom
) {
1864 // Sanity check: see if there will be any characters left after the
1866 if (regionBottom
+ 1 - regionTop
<= n
) {
1867 // There won't be anything left in the region, so just call
1868 // eraseScreen() and return.
1869 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1873 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1874 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1875 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1877 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
1878 - remaining
, regionBottom
+ 1);
1879 display
= new LinkedList
<DisplayLine
>(displayTop
);
1880 display
.addAll(displayMiddle
);
1881 for (int i
= 0; i
< n
; i
++) {
1882 DisplayLine line
= new DisplayLine(currentState
.attr
);
1883 line
.setReverseColor(reverseVideo
);
1886 display
.addAll(displayBottom
);
1888 assert (display
.size() == height
);
1892 * Scroll the text within a scrolling region down n lines.
1894 * @param regionTop top row of the scrolling region
1895 * @param regionBottom bottom row of the scrolling region
1896 * @param n number of lines to scroll
1898 private void scrollingRegionScrollDown(final int regionTop
,
1899 final int regionBottom
, final int n
) {
1901 if (regionTop
>= regionBottom
) {
1905 // Sanity check: see if there will be any characters left after the
1907 if (regionBottom
+ 1 - regionTop
<= n
) {
1908 // There won't be anything left in the region, so just call
1909 // eraseScreen() and return.
1910 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1914 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1915 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1916 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1918 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
1919 regionTop
+ remaining
);
1920 display
= new LinkedList
<DisplayLine
>(displayTop
);
1921 for (int i
= 0; i
< n
; i
++) {
1922 DisplayLine line
= new DisplayLine(currentState
.attr
);
1923 line
.setReverseColor(reverseVideo
);
1926 display
.addAll(displayMiddle
);
1927 display
.addAll(displayBottom
);
1929 assert (display
.size() == height
);
1933 * Process a control character.
1935 * @param ch 8-bit character from the remote side
1937 private void handleControlChar(final char ch
) {
1938 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
1949 // Transmit the answerback message.
1960 cursorLeft(1, false);
1965 advanceToNextTabStop();
1991 currentState
.glLockshift
= LockshiftMode
.NONE
;
1997 currentState
.glLockshift
= LockshiftMode
.NONE
;
2022 singleshift
= Singleshift
.SS2
;
2027 singleshift
= Singleshift
.SS3
;
2037 * Advance the cursor to the next tab stop.
2039 private void advanceToNextTabStop() {
2040 if (tabStops
.size() == 0) {
2041 // Go to the rightmost column
2042 cursorRight(rightMargin
- currentState
.cursorX
, false);
2045 for (Integer stop
: tabStops
) {
2046 if (stop
> currentState
.cursorX
) {
2047 cursorRight(stop
- currentState
.cursorX
, false);
2052 * We got here, meaning there isn't a tab stop beyond the current
2053 * cursor position. Place the cursor of the right-most edge of the
2056 cursorRight(rightMargin
- currentState
.cursorX
, false);
2060 * Save a character into the collect buffer.
2062 * @param ch character to save
2064 private void collect(final char ch
) {
2065 collectBuffer
.append(ch
);
2069 * Save a byte into the CSI parameters buffer.
2071 * @param ch byte to save
2073 private void param(final byte ch
) {
2074 if (csiParams
.size() == 0) {
2075 csiParams
.add(new Integer(0));
2077 Integer x
= csiParams
.get(csiParams
.size() - 1);
2078 if ((ch
>= '0') && (ch
<= '9')) {
2081 csiParams
.set(csiParams
.size() - 1, x
);
2085 csiParams
.add(new Integer(0));
2090 * Get a CSI parameter value, with a default.
2092 * @param position parameter index. 0 is the first parameter.
2093 * @param defaultValue value to use if csiParams[position] doesn't exist
2094 * @return parameter value
2096 private int getCsiParam(final int position
, final int defaultValue
) {
2097 if (csiParams
.size() < position
+ 1) {
2098 return defaultValue
;
2100 return csiParams
.get(position
).intValue();
2104 * Get a CSI parameter value, clamped to within min/max.
2106 * @param position parameter index. 0 is the first parameter.
2107 * @param defaultValue value to use if csiParams[position] doesn't exist
2108 * @param minValue minimum value inclusive
2109 * @param maxValue maximum value inclusive
2110 * @return parameter value
2112 private int getCsiParam(final int position
, final int defaultValue
,
2113 final int minValue
, final int maxValue
) {
2115 assert (minValue
<= maxValue
);
2116 int value
= getCsiParam(position
, defaultValue
);
2117 if (value
< minValue
) {
2120 if (value
> maxValue
) {
2127 * Set or unset a toggle. value is 'true' for set ('h'), false for reset
2130 private void setToggle(final boolean value
) {
2131 boolean decPrivateModeFlag
= false;
2132 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2133 if (collectBuffer
.charAt(i
) == '?') {
2134 decPrivateModeFlag
= true;
2139 for (Integer i
: csiParams
) {
2144 if (decPrivateModeFlag
== true) {
2146 if (value
== true) {
2147 // Use application arrow keys
2148 arrowKeyMode
= ArrowKeyMode
.VT100
;
2150 // Use ANSI arrow keys
2151 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2156 if (decPrivateModeFlag
== true) {
2157 if (value
== false) {
2161 arrowKeyMode
= ArrowKeyMode
.VT52
;
2164 * From the VT102 docs: "You use ANSI mode to select
2165 * most terminal features; the terminal uses the same
2166 * features when it switches to VT52 mode. You
2167 * cannot, however, change most of these features in
2170 * In other words, do not reset any other attributes
2171 * when switching between VT52 submode and ANSI.
2173 * HOWEVER, the real vt100 does switch the character
2174 * set according to Usenet.
2176 currentState
.g0Charset
= CharacterSet
.US
;
2177 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2180 if ((type
== DeviceType
.VT220
)
2181 || (type
== DeviceType
.XTERM
)) {
2183 // VT52 mode is explicitly 7-bit
2185 singleshift
= Singleshift
.NONE
;
2190 if (value
== true) {
2191 // Turn off keyboard
2200 if (decPrivateModeFlag
== true) {
2202 if (value
== true) {
2211 width
= rightMargin
+ 1;
2212 // Entire screen is cleared, and scrolling region is
2214 eraseScreen(0, 0, height
- 1, width
- 1, false);
2215 scrollRegionTop
= 0;
2216 scrollRegionBottom
= height
- 1;
2217 // Also home the cursor
2218 cursorPosition(0, 0);
2222 if (decPrivateModeFlag
== true) {
2224 if (value
== true) {
2233 if (value
== true) {
2241 if (decPrivateModeFlag
== true) {
2243 if (value
== true) {
2245 * Set selects reverse screen, a white screen
2246 * background with black characters.
2248 if (reverseVideo
!= true) {
2250 * If in normal video, switch it back
2252 invertDisplayColors();
2254 reverseVideo
= true;
2257 * Reset selects normal screen, a black screen
2258 * background with white characters.
2260 if (reverseVideo
== true) {
2262 * If in reverse video already, switch it back
2264 invertDisplayColors();
2266 reverseVideo
= false;
2271 if (decPrivateModeFlag
== true) {
2273 if (value
== true) {
2274 // Origin is relative to scroll region cursor.
2275 // Cursor can NEVER leave scrolling region.
2276 currentState
.originMode
= true;
2277 cursorPosition(0, 0);
2279 // Origin is absolute to entire screen. Cursor can
2280 // leave the scrolling region via cup() and hvp().
2281 currentState
.originMode
= false;
2282 cursorPosition(0, 0);
2287 if (decPrivateModeFlag
== true) {
2289 if (value
== true) {
2291 currentState
.lineWrap
= true;
2293 // Turn linewrap off
2294 currentState
.lineWrap
= false;
2299 if (decPrivateModeFlag
== true) {
2301 if (value
== true) {
2302 // Keyboard auto-repeat on
2305 // Keyboard auto-repeat off
2311 if (decPrivateModeFlag
== false) {
2313 if (value
== true) {
2323 if (decPrivateModeFlag
== true) {
2329 if (decPrivateModeFlag
== true) {
2335 if (decPrivateModeFlag
== false) {
2337 if (value
== true) {
2339 * Set causes a received linefeed, form feed, or
2340 * vertical tab to move cursor to first column of
2341 * next line. RETURN transmits both a carriage return
2342 * and linefeed. This selection is also called new
2348 * Reset causes a received linefeed, form feed, or
2349 * vertical tab to move cursor to next line in
2350 * current column. RETURN transmits a carriage
2353 newLineMode
= false;
2359 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2360 if (decPrivateModeFlag
== true) {
2362 if (value
== true) {
2364 cursorVisible
= true;
2367 cursorVisible
= false;
2374 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2375 if (decPrivateModeFlag
== true) {
2377 if (value
== true) {
2378 // Select national mode NRC
2381 // Select multi-national mode
2390 if ((type
== DeviceType
.XTERM
)
2391 && (decPrivateModeFlag
== true)
2393 // Mouse: normal tracking mode
2394 if (value
== true) {
2395 mouseProtocol
= MouseProtocol
.NORMAL
;
2397 mouseProtocol
= MouseProtocol
.OFF
;
2403 if ((type
== DeviceType
.XTERM
)
2404 && (decPrivateModeFlag
== true)
2406 // Mouse: normal tracking mode
2407 if (value
== true) {
2408 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2410 mouseProtocol
= MouseProtocol
.OFF
;
2416 if ((type
== DeviceType
.XTERM
)
2417 && (decPrivateModeFlag
== true)
2419 // Mouse: Any-event tracking mode
2420 if (value
== true) {
2421 mouseProtocol
= MouseProtocol
.ANYEVENT
;
2423 mouseProtocol
= MouseProtocol
.OFF
;
2429 if ((type
== DeviceType
.XTERM
)
2430 && (decPrivateModeFlag
== true)
2432 // Mouse: UTF-8 coordinates
2433 if (value
== true) {
2434 mouseEncoding
= MouseEncoding
.UTF8
;
2436 mouseEncoding
= MouseEncoding
.X10
;
2449 * DECSC - Save cursor.
2451 private void decsc() {
2452 savedState
.setTo(currentState
);
2456 * DECRC - Restore cursor.
2458 private void decrc() {
2459 currentState
.setTo(savedState
);
2465 private void ind() {
2466 // Move the cursor and scroll if necessary. If at the bottom line
2467 // already, a scroll up is supposed to be performed.
2468 if (currentState
.cursorY
== scrollRegionBottom
) {
2469 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2471 cursorDown(1, true);
2475 * RI - Reverse index.
2478 // Move the cursor and scroll if necessary. If at the top line
2479 // already, a scroll down is supposed to be performed.
2480 if (currentState
.cursorY
== scrollRegionTop
) {
2481 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2489 private void nel() {
2490 // Move the cursor and scroll if necessary. If at the bottom line
2491 // already, a scroll up is supposed to be performed.
2492 if (currentState
.cursorY
== scrollRegionBottom
) {
2493 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2495 cursorDown(1, true);
2497 // Reset to the beginning of the next line
2498 currentState
.cursorX
= 0;
2502 * DECKPAM - Keypad application mode.
2504 private void deckpam() {
2505 keypadMode
= KeypadMode
.Application
;
2509 * DECKPNM - Keypad numeric mode.
2511 private void deckpnm() {
2512 keypadMode
= KeypadMode
.Numeric
;
2518 * @param n number of spaces to move
2519 * @param honorScrollRegion if true, then do nothing if the cursor is
2520 * outside the scrolling region
2522 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2526 * Special case: if a user moves the cursor from the right margin, we
2527 * have to reset the VT100 right margin flag.
2530 wrapLineFlag
= false;
2533 for (int i
= 0; i
< n
; i
++) {
2534 if (honorScrollRegion
== true) {
2535 // Honor the scrolling region
2536 if ((currentState
.cursorY
< scrollRegionTop
)
2537 || (currentState
.cursorY
> scrollRegionBottom
)
2539 // Outside region, do nothing
2542 // Inside region, go up
2543 top
= scrollRegionTop
;
2545 // Non-scrolling case
2549 if (currentState
.cursorY
> top
) {
2550 currentState
.cursorY
--;
2556 * Move down n spaces.
2558 * @param n number of spaces to move
2559 * @param honorScrollRegion if true, then do nothing if the cursor is
2560 * outside the scrolling region
2562 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
2566 * Special case: if a user moves the cursor from the right margin, we
2567 * have to reset the VT100 right margin flag.
2570 wrapLineFlag
= false;
2573 for (int i
= 0; i
< n
; i
++) {
2575 if (honorScrollRegion
== true) {
2576 // Honor the scrolling region
2577 if (currentState
.cursorY
> scrollRegionBottom
) {
2578 // Outside region, do nothing
2581 // Inside region, go down
2582 bottom
= scrollRegionBottom
;
2584 // Non-scrolling case
2585 bottom
= height
- 1;
2588 if (currentState
.cursorY
< bottom
) {
2589 currentState
.cursorY
++;
2595 * Move left n spaces.
2597 * @param n number of spaces to move
2598 * @param honorScrollRegion if true, then do nothing if the cursor is
2599 * outside the scrolling region
2601 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
2603 * Special case: if a user moves the cursor from the right margin, we
2604 * have to reset the VT100 right margin flag.
2607 wrapLineFlag
= false;
2610 for (int i
= 0; i
< n
; i
++) {
2611 if (honorScrollRegion
== true) {
2612 // Honor the scrolling region
2613 if ((currentState
.cursorY
< scrollRegionTop
)
2614 || (currentState
.cursorY
> scrollRegionBottom
)
2616 // Outside region, do nothing
2621 if (currentState
.cursorX
> 0) {
2622 currentState
.cursorX
--;
2628 * Move right n spaces.
2630 * @param n number of spaces to move
2631 * @param honorScrollRegion if true, then do nothing if the cursor is
2632 * outside the scrolling region
2634 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
2635 int rightMargin
= this.rightMargin
;
2638 * Special case: if a user moves the cursor from the right margin, we
2639 * have to reset the VT100 right margin flag.
2642 wrapLineFlag
= false;
2645 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2646 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2649 for (int i
= 0; i
< n
; i
++) {
2650 if (honorScrollRegion
== true) {
2651 // Honor the scrolling region
2652 if ((currentState
.cursorY
< scrollRegionTop
)
2653 || (currentState
.cursorY
> scrollRegionBottom
)
2655 // Outside region, do nothing
2660 if (currentState
.cursorX
< rightMargin
) {
2661 currentState
.cursorX
++;
2667 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2669 * @param row row to move to
2670 * @param col column to move to
2672 private void cursorPosition(int row
, final int col
) {
2673 int rightMargin
= this.rightMargin
;
2678 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2679 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2682 // Set column number
2683 currentState
.cursorX
= col
;
2685 // Sanity check, bring column back to margin.
2686 if (currentState
.cursorX
> rightMargin
) {
2687 currentState
.cursorX
= rightMargin
;
2691 if (currentState
.originMode
== true) {
2692 row
+= scrollRegionTop
;
2694 if (currentState
.cursorY
< row
) {
2695 cursorDown(row
- currentState
.cursorY
, false);
2696 } else if (currentState
.cursorY
> row
) {
2697 cursorUp(currentState
.cursorY
- row
, false);
2700 wrapLineFlag
= false;
2704 * HTS - Horizontal tabulation set.
2706 private void hts() {
2707 for (Integer stop
: tabStops
) {
2708 if (stop
== currentState
.cursorX
) {
2709 // Already have a tab stop here
2714 // Append a tab stop to the end of the array and resort them
2715 tabStops
.add(currentState
.cursorX
);
2716 Collections
.sort(tabStops
);
2720 * DECSWL - Single-width line.
2722 private void decswl() {
2723 display
.get(currentState
.cursorY
).setDoubleWidth(false);
2724 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2728 * DECDWL - Double-width line.
2730 private void decdwl() {
2731 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2732 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2736 * DECHDL - Double-height + double-width line.
2738 * @param topHalf if true, this sets the row to be the top half row of a
2741 private void dechdl(final boolean topHalf
) {
2742 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2743 if (topHalf
== true) {
2744 display
.get(currentState
.cursorY
).setDoubleHeight(1);
2746 display
.get(currentState
.cursorY
).setDoubleHeight(2);
2751 * DECALN - Screen alignment display.
2753 private void decaln() {
2754 Cell newCell
= new Cell();
2755 newCell
.setChar('E');
2756 for (DisplayLine line
: display
) {
2757 for (int i
= 0; i
< line
.length(); i
++) {
2758 line
.replace(i
, newCell
);
2764 * DECSCL - Compatibility level.
2766 private void decscl() {
2767 int i
= getCsiParam(0, 0);
2768 int j
= getCsiParam(1, 0);
2772 currentState
.g0Charset
= CharacterSet
.US
;
2773 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2775 } else if (i
== 62) {
2777 if ((j
== 0) || (j
== 2)) {
2779 } else if (j
== 1) {
2786 * CUD - Cursor down.
2788 private void cud() {
2789 cursorDown(getCsiParam(0, 1, 1, height
), true);
2793 * CUF - Cursor forward.
2795 private void cuf() {
2796 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
2800 * CUB - Cursor backward.
2802 private void cub() {
2803 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
2809 private void cuu() {
2810 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2814 * CUP - Cursor position.
2816 private void cup() {
2817 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2818 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
2822 * CNL - Cursor down and to column 1.
2824 private void cnl() {
2825 cursorDown(getCsiParam(0, 1, 1, height
), true);
2827 cursorLeft(currentState
.cursorX
, true);
2831 * CPL - Cursor up and to column 1.
2833 private void cpl() {
2834 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2836 cursorLeft(currentState
.cursorX
, true);
2840 * CHA - Cursor to column # in current row.
2842 private void cha() {
2843 cursorPosition(currentState
.cursorY
,
2844 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
2848 * VPA - Cursor to row #, same column.
2850 private void vpa() {
2851 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2852 currentState
.cursorX
);
2856 * ED - Erase in display.
2859 boolean honorProtected
= false;
2860 boolean decPrivateModeFlag
= false;
2862 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2863 if (collectBuffer
.charAt(i
) == '?') {
2864 decPrivateModeFlag
= true;
2869 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2870 && (decPrivateModeFlag
== true)
2872 honorProtected
= true;
2875 int i
= getCsiParam(0, 0);
2878 // Erase from here to end of screen
2879 if (currentState
.cursorY
< height
- 1) {
2880 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
2883 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2884 } else if (i
== 1) {
2885 // Erase from beginning of screen to here
2886 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
2888 eraseLine(0, currentState
.cursorX
, honorProtected
);
2889 } else if (i
== 2) {
2890 // Erase entire screen
2891 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
2896 * EL - Erase in line.
2899 boolean honorProtected
= false;
2900 boolean decPrivateModeFlag
= false;
2902 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2903 if (collectBuffer
.charAt(i
) == '?') {
2904 decPrivateModeFlag
= true;
2909 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2910 && (decPrivateModeFlag
== true)
2912 honorProtected
= true;
2915 int i
= getCsiParam(0, 0);
2918 // Erase from here to end of line
2919 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2920 } else if (i
== 1) {
2921 // Erase from beginning of line to here
2922 eraseLine(0, currentState
.cursorX
, honorProtected
);
2923 } else if (i
== 2) {
2924 // Erase entire line
2925 eraseLine(0, width
- 1, honorProtected
);
2930 * ECH - Erase # of characters in current row.
2932 private void ech() {
2933 int i
= getCsiParam(0, 1, 1, width
);
2935 // Erase from here to i characters
2936 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
2943 int i
= getCsiParam(0, 1);
2945 if ((currentState
.cursorY
>= scrollRegionTop
)
2946 && (currentState
.cursorY
<= scrollRegionBottom
)
2949 // I can get the same effect with a scroll-down
2950 scrollingRegionScrollDown(currentState
.cursorY
,
2951 scrollRegionBottom
, i
);
2956 * DCH - Delete char.
2958 private void dch() {
2959 int n
= getCsiParam(0, 1);
2960 DisplayLine line
= display
.get(currentState
.cursorY
);
2961 Cell blank
= new Cell();
2962 for (int i
= 0; i
< n
; i
++) {
2963 line
.delete(currentState
.cursorX
, blank
);
2968 * ICH - Insert blank char at cursor.
2970 private void ich() {
2971 int n
= getCsiParam(0, 1);
2972 DisplayLine line
= display
.get(currentState
.cursorY
);
2973 Cell blank
= new Cell();
2974 for (int i
= 0; i
< n
; i
++) {
2975 line
.insert(currentState
.cursorX
, blank
);
2983 int i
= getCsiParam(0, 1);
2985 if ((currentState
.cursorY
>= scrollRegionTop
)
2986 && (currentState
.cursorY
<= scrollRegionBottom
)) {
2988 // I can get the same effect with a scroll-down
2989 scrollingRegionScrollUp(currentState
.cursorY
,
2990 scrollRegionBottom
, i
);
2995 * HVP - Horizontal and vertical position.
2997 private void hvp() {
3002 * REP - Repeat character.
3004 private void rep() {
3005 int n
= getCsiParam(0, 1);
3006 for (int i
= 0; i
< n
; i
++) {
3007 printCharacter(repCh
);
3015 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3016 getCsiParam(0, 1, 1, height
));
3023 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3024 getCsiParam(0, 1, 1, height
));
3028 * CBT - Go back X tab stops.
3030 private void cbt() {
3031 int tabsToMove
= getCsiParam(0, 1);
3034 for (int i
= 0; i
< tabsToMove
; i
++) {
3035 int j
= currentState
.cursorX
;
3036 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3037 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3045 j
= tabStops
.get(tabI
);
3047 cursorPosition(currentState
.cursorY
, j
);
3052 * CHT - Advance X tab stops.
3054 private void cht() {
3055 int n
= getCsiParam(0, 1);
3056 for (int i
= 0; i
< n
; i
++) {
3057 advanceToNextTabStop();
3062 * SGR - Select graphics rendition.
3064 private void sgr() {
3066 if (csiParams
.size() == 0) {
3067 currentState
.attr
.reset();
3071 for (Integer i
: csiParams
) {
3077 currentState
.attr
.reset();
3082 currentState
.attr
.setBold(true);
3087 currentState
.attr
.setUnderline(true);
3092 currentState
.attr
.setBlink(true);
3097 currentState
.attr
.setReverse(true);
3104 if (type
== DeviceType
.XTERM
) {
3118 if ((type
== DeviceType
.VT220
)
3119 || (type
== DeviceType
.XTERM
)) {
3125 currentState
.attr
.setBold(false);
3130 currentState
.attr
.setUnderline(false);
3135 currentState
.attr
.setBlink(false);
3140 currentState
.attr
.setReverse(false);
3148 // A true VT100/102/220 does not support color, however everyone
3149 // is used to their terminal emulator supporting color so we will
3150 // unconditionally support color for all DeviceType's.
3155 // Set black foreground
3156 currentState
.attr
.setForeColor(Color
.BLACK
);
3159 // Set red foreground
3160 currentState
.attr
.setForeColor(Color
.RED
);
3163 // Set green foreground
3164 currentState
.attr
.setForeColor(Color
.GREEN
);
3167 // Set yellow foreground
3168 currentState
.attr
.setForeColor(Color
.YELLOW
);
3171 // Set blue foreground
3172 currentState
.attr
.setForeColor(Color
.BLUE
);
3175 // Set magenta foreground
3176 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3179 // Set cyan foreground
3180 currentState
.attr
.setForeColor(Color
.CYAN
);
3183 // Set white foreground
3184 currentState
.attr
.setForeColor(Color
.WHITE
);
3187 // Underscore on, default foreground color
3188 currentState
.attr
.setUnderline(true);
3189 currentState
.attr
.setForeColor(Color
.WHITE
);
3192 // Underscore off, default foreground color
3193 currentState
.attr
.setUnderline(false);
3194 currentState
.attr
.setForeColor(Color
.WHITE
);
3197 // Set black background
3198 currentState
.attr
.setBackColor(Color
.BLACK
);
3201 // Set red background
3202 currentState
.attr
.setBackColor(Color
.RED
);
3205 // Set green background
3206 currentState
.attr
.setBackColor(Color
.GREEN
);
3209 // Set yellow background
3210 currentState
.attr
.setBackColor(Color
.YELLOW
);
3213 // Set blue background
3214 currentState
.attr
.setBackColor(Color
.BLUE
);
3217 // Set magenta background
3218 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3221 // Set cyan background
3222 currentState
.attr
.setBackColor(Color
.CYAN
);
3225 // Set white background
3226 currentState
.attr
.setBackColor(Color
.WHITE
);
3229 // Default background
3230 currentState
.attr
.setBackColor(Color
.BLACK
);
3240 * DA - Device attributes.
3243 int extendedFlag
= 0;
3245 if (collectBuffer
.length() > 0) {
3246 String args
= collectBuffer
.substring(1);
3247 if (collectBuffer
.charAt(0) == '>') {
3249 if (collectBuffer
.length() >= 2) {
3250 i
= Integer
.parseInt(args
.toString());
3252 } else if (collectBuffer
.charAt(0) == '=') {
3254 if (collectBuffer
.length() >= 2) {
3255 i
= Integer
.parseInt(args
.toString());
3258 // Unknown code, bail out
3263 if ((i
!= 0) && (i
!= 1)) {
3267 if ((extendedFlag
== 0) && (i
== 0)) {
3268 // Send string directly to remote side
3269 writeRemote(deviceTypeResponse());
3273 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3275 if ((extendedFlag
== 1) && (i
== 0)) {
3277 * Request "What type of terminal are you, what is your
3278 * firmware version, and what hardware options do you have
3281 * Respond: "I am a VT220 (identification code of 1), my
3282 * firmware version is _____ (Pv), and I have _____ Po
3283 * options installed."
3289 if (s8c1t
== true) {
3290 writeRemote("\u009b>1;10;0c");
3292 writeRemote("\033[>1;10;0c");
3298 if ((extendedFlag
== 2) && (i
== 0)) {
3301 * Request "What is your unit ID?"
3303 * Respond: "I was manufactured at site 00 and have a unique ID
3307 writeRemote("\033P!|00010203\033\\");
3312 * DECSTBM - Set top and bottom margins.
3314 private void decstbm() {
3315 int top
= getCsiParam(0, 1, 1, height
) - 1;
3316 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3321 scrollRegionTop
= top
;
3322 scrollRegionBottom
= bottom
;
3325 cursorPosition(0, 0);
3329 * DECREQTPARM - Request terminal parameters.
3331 private void decreqtparm() {
3332 int i
= getCsiParam(0, 0);
3334 if ((i
!= 0) && (i
!= 1)) {
3341 * Request terminal parameters.
3345 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3346 * (CLoCk MULtiplier = 1, STP option flags = 0)
3350 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3353 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3355 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3361 * DECSCA - Select Character Attributes.
3363 private void decsca() {
3364 int i
= getCsiParam(0, 0);
3366 if ((i
== 0) || (i
== 2)) {
3368 currentState
.attr
.setProtect(false);
3372 currentState
.attr
.setProtect(true);
3377 * DECSTR - Soft Terminal Reset.
3379 private void decstr() {
3380 // Do exactly like RIS - Reset to initial state
3382 // Do I clear screen too? I think so...
3383 eraseScreen(0, 0, height
- 1, width
- 1, false);
3384 cursorPosition(0, 0);
3388 * DSR - Device status report.
3390 private void dsr() {
3391 boolean decPrivateModeFlag
= false;
3393 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3394 if (collectBuffer
.charAt(i
) == '?') {
3395 decPrivateModeFlag
= true;
3400 int i
= getCsiParam(0, 0);
3405 // Request status report. Respond with "OK, no malfunction."
3407 // Send string directly to remote side
3408 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3411 writeRemote("\u009b0n");
3413 writeRemote("\033[0n");
3418 // Request cursor position. Respond with current position.
3420 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3423 str
= String
.format("\u009b%d;%dR",
3424 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3426 str
= String
.format("\033[%d;%dR",
3427 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3430 // Send string directly to remote side
3435 if (decPrivateModeFlag
== true) {
3437 // Request printer status report. Respond with "Printer not
3440 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3441 && (s8c1t
== true)) {
3442 writeRemote("\u009b?13n");
3444 writeRemote("\033[?13n");
3450 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3451 && (decPrivateModeFlag
== true)
3454 // Request user-defined keys are locked or unlocked. Respond
3455 // with "User-defined keys are locked."
3457 if (s8c1t
== true) {
3458 writeRemote("\u009b?21n");
3460 writeRemote("\033[?21n");
3466 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3467 && (decPrivateModeFlag
== true)
3470 // Request keyboard language. Respond with "Keyboard
3471 // language is North American."
3473 if (s8c1t
== true) {
3474 writeRemote("\u009b?27;1n");
3476 writeRemote("\033[?27;1n");
3483 // Some other option, ignore
3489 * TBC - Tabulation clear.
3491 private void tbc() {
3492 int i
= getCsiParam(0, 0);
3494 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3495 for (Integer stop
: tabStops
) {
3496 if (stop
== currentState
.cursorX
) {
3501 tabStops
= newStops
;
3509 * Erase the characters in the current line from the start column to the
3510 * end column, inclusive.
3512 * @param start starting column to erase (between 0 and width - 1)
3513 * @param end ending column to erase (between 0 and width - 1)
3514 * @param honorProtected if true, do not erase characters with the
3515 * protected attribute set
3517 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3522 if (end
> width
- 1) {
3529 for (int i
= start
; i
<= end
; i
++) {
3530 DisplayLine line
= display
.get(currentState
.cursorY
);
3531 if ((!honorProtected
)
3532 || ((honorProtected
) && (!line
.charAt(i
).getProtect()))) {
3539 * From the VT102 manual:
3541 * Erasing a character also erases any character
3542 * attribute of the character.
3548 * Erase with the current color a.k.a. back-color erase
3551 line
.setChar(i
, ' ');
3552 line
.setAttr(i
, currentState
.attr
);
3560 * Erase a rectangular section of the screen, inclusive. end column,
3563 * @param startRow starting row to erase (between 0 and height - 1)
3564 * @param startCol starting column to erase (between 0 and width - 1)
3565 * @param endRow ending row to erase (between 0 and height - 1)
3566 * @param endCol ending column to erase (between 0 and width - 1)
3567 * @param honorProtected if true, do not erase characters with the
3568 * protected attribute set
3570 private void eraseScreen(final int startRow
, final int startCol
,
3571 final int endRow
, final int endCol
, final boolean honorProtected
) {
3579 || (endRow
< startRow
)
3580 || (endCol
< startCol
)
3585 oldCursorY
= currentState
.cursorY
;
3586 for (int i
= startRow
; i
<= endRow
; i
++) {
3587 currentState
.cursorY
= i
;
3588 eraseLine(startCol
, endCol
, honorProtected
);
3590 // Erase display clears the double attributes
3591 display
.get(i
).setDoubleWidth(false);
3592 display
.get(i
).setDoubleHeight(0);
3594 currentState
.cursorY
= oldCursorY
;
3598 * VT220 printer functions. All of these are parsed, but won't do
3601 private void printerFunctions() {
3602 boolean decPrivateModeFlag
= false;
3603 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3604 if (collectBuffer
.charAt(i
) == '?') {
3605 decPrivateModeFlag
= true;
3610 int i
= getCsiParam(0, 0);
3615 if (decPrivateModeFlag
== false) {
3621 if (decPrivateModeFlag
== true) {
3622 // Print cursor line
3627 if (decPrivateModeFlag
== true) {
3628 // Auto print mode OFF
3630 // Printer controller OFF
3632 // Characters re-appear on the screen
3633 printerControllerMode
= false;
3638 if (decPrivateModeFlag
== true) {
3642 // Printer controller
3644 // Characters get sucked into oblivion
3645 printerControllerMode
= true;
3656 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
3657 * of remote systems will send an XTerm title sequence even if TERM isn't
3660 * @param xtermChar the character received from the remote side
3662 private void oscPut(final char xtermChar
) {
3664 collectBuffer
.append(xtermChar
);
3667 if (xtermChar
== 0x07) {
3668 String args
= collectBuffer
.substring(0,
3669 collectBuffer
.length() - 1);
3670 String
[] p
= args
.toString().split(";");
3672 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
3680 // Go to SCAN_GROUND state
3687 * Run this input character through the ECMA48 state machine.
3689 * @param ch character from the remote side
3691 public void consume(char ch
) {
3694 // System.err.printf("%c", ch);
3696 // Special case for VT10x: 7-bit characters only
3697 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
3698 ch
= (char)(ch
& 0x7F);
3701 // Special "anywhere" states
3703 // 18, 1A --> execute, then switch to SCAN_GROUND
3704 if ((ch
== 0x18) || (ch
== 0x1A)) {
3705 // CAN and SUB abort escape sequences
3710 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
3714 && (scanState
!= ScanState
.DCS_ENTRY
)
3715 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
3716 && (scanState
!= ScanState
.DCS_IGNORE
)
3717 && (scanState
!= ScanState
.DCS_PARAM
)
3718 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
3721 scanState
= ScanState
.ESCAPE
;
3725 // 0x9B == CSI 8-bit sequence
3727 scanState
= ScanState
.CSI_ENTRY
;
3731 // 0x9D goes to ScanState.OSC_STRING
3733 scanState
= ScanState
.OSC_STRING
;
3737 // 0x90 goes to DCS_ENTRY
3739 scanState
= ScanState
.DCS_ENTRY
;
3743 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
3744 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
3745 scanState
= ScanState
.SOSPMAPC_STRING
;
3749 // 0x7F (DEL) is always discarded
3754 switch (scanState
) {
3757 // 00-17, 19, 1C-1F --> execute
3758 // 80-8F, 91-9A, 9C --> execute
3759 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
3760 handleControlChar(ch
);
3764 if (((ch
>= 0x20) && (ch
<= 0x7F))
3768 // VT220 printer --> trash bin
3769 if (((type
== DeviceType
.VT220
)
3770 || (type
== DeviceType
.XTERM
))
3771 && (printerControllerMode
== true)
3776 // Hang onto this character
3777 repCh
= mapCharacter(ch
);
3779 // Print this character
3780 printCharacter(repCh
);
3785 // 00-17, 19, 1C-1F --> execute
3787 handleControlChar(ch
);
3791 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
3792 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
3794 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
3798 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
3799 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
3810 // DECSC - Save cursor
3811 // Note this code overlaps both ANSI and VT52 mode
3816 // DECRC - Restore cursor
3817 // Note this code overlaps both ANSI and VT52 mode
3826 if (vt52Mode
== true) {
3827 // DECANM - Enter ANSI mode
3829 arrowKeyMode
= ArrowKeyMode
.VT100
;
3832 * From the VT102 docs: "You use ANSI mode to select
3833 * most terminal features; the terminal uses the same
3834 * features when it switches to VT52 mode. You
3835 * cannot, however, change most of these features in
3838 * In other words, do not reset any other attributes
3839 * when switching between VT52 submode and ANSI.
3843 currentState
.g0Charset
= CharacterSet
.US
;
3844 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3846 singleshift
= Singleshift
.NONE
;
3847 currentState
.glLockshift
= LockshiftMode
.NONE
;
3848 currentState
.grLockshift
= LockshiftMode
.NONE
;
3852 // DECKPAM - Keypad application mode
3853 // Note this code overlaps both ANSI and VT52 mode
3857 // DECKPNM - Keypad numeric mode
3858 // Note this code overlaps both ANSI and VT52 mode
3865 if (vt52Mode
== true) {
3866 // Cursor up, and stop at the top without scrolling
3871 if (vt52Mode
== true) {
3872 // Cursor down, and stop at the bottom without scrolling
3873 cursorDown(1, false);
3877 if (vt52Mode
== true) {
3878 // Cursor right, and stop at the right without scrolling
3879 cursorRight(1, false);
3883 if (vt52Mode
== true) {
3884 // Cursor left, and stop at the left without scrolling
3885 cursorLeft(1, false);
3892 if (vt52Mode
== true) {
3900 if (vt52Mode
== true) {
3901 // G0 --> Special graphics
3902 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
3906 if (vt52Mode
== true) {
3908 currentState
.g0Charset
= CharacterSet
.US
;
3912 if (vt52Mode
== true) {
3914 cursorPosition(0, 0);
3916 // HTS - Horizontal tabulation set
3921 if (vt52Mode
== true) {
3922 // Reverse line feed. Same as RI.
3927 if (vt52Mode
== true) {
3928 // Erase to end of screen
3929 eraseLine(currentState
.cursorX
, width
- 1, false);
3930 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
3935 if (vt52Mode
== true) {
3936 // Erase to end of line
3937 eraseLine(currentState
.cursorX
, width
- 1, false);
3943 if (vt52Mode
== true) {
3946 // RI - Reverse index
3951 if (vt52Mode
== false) {
3953 singleshift
= Singleshift
.SS2
;
3957 if (vt52Mode
== false) {
3959 singleshift
= Singleshift
.SS3
;
3966 if ((ch
>= 0x51) && (ch
<= 0x57)) {
3982 if (vt52Mode
== true) {
3983 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
3991 if (vt52Mode
== true) {
3993 // Send string directly to remote side
3994 writeRemote("\033/Z");
3997 // Send string directly to remote side
3998 writeRemote(deviceTypeResponse());
4009 // VT52 cannot get to any of these other states
4010 if (vt52Mode
== true) {
4015 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4022 // RIS - Reset to initial state
4024 // Do I clear screen too? I think so...
4025 eraseScreen(0, 0, height
- 1, width
- 1, false);
4026 cursorPosition(0, 0);
4040 if ((type
== DeviceType
.VT220
)
4041 || (type
== DeviceType
.XTERM
)) {
4043 // VT220 lockshift G2 into GL
4044 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4049 if ((type
== DeviceType
.VT220
)
4050 || (type
== DeviceType
.XTERM
)) {
4052 // VT220 lockshift G3 into GL
4053 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4071 if ((type
== DeviceType
.VT220
)
4072 || (type
== DeviceType
.XTERM
)) {
4074 // VT220 lockshift G3 into GR
4075 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4080 if ((type
== DeviceType
.VT220
)
4081 || (type
== DeviceType
.XTERM
)) {
4083 // VT220 lockshift G2 into GR
4084 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4090 if ((type
== DeviceType
.VT220
)
4091 || (type
== DeviceType
.XTERM
)) {
4093 // VT220 lockshift G1 into GR
4094 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4104 // 0x5B goes to CSI_ENTRY
4106 scanState
= ScanState
.CSI_ENTRY
;
4109 // 0x5D goes to OSC_STRING
4111 scanState
= ScanState
.OSC_STRING
;
4114 // 0x50 goes to DCS_ENTRY
4116 scanState
= ScanState
.DCS_ENTRY
;
4119 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4120 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4121 scanState
= ScanState
.SOSPMAPC_STRING
;
4126 case ESCAPE_INTERMEDIATE
:
4127 // 00-17, 19, 1C-1F --> execute
4129 handleControlChar(ch
);
4132 // 20-2F --> collect
4133 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4137 // 30-7E --> dispatch, then switch to GROUND
4138 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4141 if ((collectBuffer
.length() == 1)
4142 && (collectBuffer
.charAt(0) == '(')) {
4143 // G0 --> Special graphics
4144 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4146 if ((collectBuffer
.length() == 1)
4147 && (collectBuffer
.charAt(0) == ')')) {
4148 // G1 --> Special graphics
4149 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4151 if ((type
== DeviceType
.VT220
)
4152 || (type
== DeviceType
.XTERM
)) {
4154 if ((collectBuffer
.length() == 1)
4155 && (collectBuffer
.charAt(0) == '*')) {
4156 // G2 --> Special graphics
4157 currentState
.g2Charset
= CharacterSet
.DRAWING
;
4159 if ((collectBuffer
.length() == 1)
4160 && (collectBuffer
.charAt(0) == '+')) {
4161 // G3 --> Special graphics
4162 currentState
.g3Charset
= CharacterSet
.DRAWING
;
4167 if ((collectBuffer
.length() == 1)
4168 && (collectBuffer
.charAt(0) == '(')) {
4169 // G0 --> Alternate character ROM standard character set
4170 currentState
.g0Charset
= CharacterSet
.ROM
;
4172 if ((collectBuffer
.length() == 1)
4173 && (collectBuffer
.charAt(0) == ')')) {
4174 // G1 --> Alternate character ROM standard character set
4175 currentState
.g1Charset
= CharacterSet
.ROM
;
4179 if ((collectBuffer
.length() == 1)
4180 && (collectBuffer
.charAt(0) == '(')) {
4181 // G0 --> Alternate character ROM special graphics
4182 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4184 if ((collectBuffer
.length() == 1)
4185 && (collectBuffer
.charAt(0) == ')')) {
4186 // G1 --> Alternate character ROM special graphics
4187 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4191 if ((collectBuffer
.length() == 1)
4192 && (collectBuffer
.charAt(0) == '#')) {
4193 // DECDHL - Double-height line (top half)
4198 if ((collectBuffer
.length() == 1)
4199 && (collectBuffer
.charAt(0) == '#')) {
4200 // DECDHL - Double-height line (bottom half)
4203 if ((type
== DeviceType
.VT220
)
4204 || (type
== DeviceType
.XTERM
)) {
4206 if ((collectBuffer
.length() == 1)
4207 && (collectBuffer
.charAt(0) == '(')) {
4209 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4211 if ((collectBuffer
.length() == 1)
4212 && (collectBuffer
.charAt(0) == ')')) {
4214 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4216 if ((collectBuffer
.length() == 1)
4217 && (collectBuffer
.charAt(0) == '*')) {
4219 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4221 if ((collectBuffer
.length() == 1)
4222 && (collectBuffer
.charAt(0) == '+')) {
4224 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4229 if ((collectBuffer
.length() == 1)
4230 && (collectBuffer
.charAt(0) == '#')) {
4231 // DECSWL - Single-width line
4234 if ((type
== DeviceType
.VT220
)
4235 || (type
== DeviceType
.XTERM
)) {
4237 if ((collectBuffer
.length() == 1)
4238 && (collectBuffer
.charAt(0) == '(')) {
4240 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4242 if ((collectBuffer
.length() == 1)
4243 && (collectBuffer
.charAt(0) == ')')) {
4245 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4247 if ((collectBuffer
.length() == 1)
4248 && (collectBuffer
.charAt(0) == '*')) {
4250 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4252 if ((collectBuffer
.length() == 1)
4253 && (collectBuffer
.charAt(0) == '+')) {
4255 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4260 if ((collectBuffer
.length() == 1)
4261 && (collectBuffer
.charAt(0) == '#')) {
4262 // DECDWL - Double-width line
4265 if ((type
== DeviceType
.VT220
)
4266 || (type
== DeviceType
.XTERM
)) {
4268 if ((collectBuffer
.length() == 1)
4269 && (collectBuffer
.charAt(0) == '(')) {
4271 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4273 if ((collectBuffer
.length() == 1)
4274 && (collectBuffer
.charAt(0) == ')')) {
4276 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4278 if ((collectBuffer
.length() == 1)
4279 && (collectBuffer
.charAt(0) == '*')) {
4281 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4283 if ((collectBuffer
.length() == 1)
4284 && (collectBuffer
.charAt(0) == '+')) {
4286 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4291 if ((type
== DeviceType
.VT220
)
4292 || (type
== DeviceType
.XTERM
)) {
4294 if ((collectBuffer
.length() == 1)
4295 && (collectBuffer
.charAt(0) == '(')) {
4297 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4299 if ((collectBuffer
.length() == 1)
4300 && (collectBuffer
.charAt(0) == ')')) {
4302 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4304 if ((collectBuffer
.length() == 1)
4305 && (collectBuffer
.charAt(0) == '*')) {
4307 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4309 if ((collectBuffer
.length() == 1)
4310 && (collectBuffer
.charAt(0) == '+')) {
4312 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4317 if ((collectBuffer
.length() == 1)
4318 && (collectBuffer
.charAt(0) == '#')) {
4319 // DECALN - Screen alignment display
4328 if ((type
== DeviceType
.VT220
)
4329 || (type
== DeviceType
.XTERM
)) {
4331 if ((collectBuffer
.length() == 1)
4332 && (collectBuffer
.charAt(0) == '(')) {
4333 // G0 --> DEC_SUPPLEMENTAL
4334 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4336 if ((collectBuffer
.length() == 1)
4337 && (collectBuffer
.charAt(0) == ')')) {
4338 // G1 --> DEC_SUPPLEMENTAL
4339 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4341 if ((collectBuffer
.length() == 1)
4342 && (collectBuffer
.charAt(0) == '*')) {
4343 // G2 --> DEC_SUPPLEMENTAL
4344 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4346 if ((collectBuffer
.length() == 1)
4347 && (collectBuffer
.charAt(0) == '+')) {
4348 // G3 --> DEC_SUPPLEMENTAL
4349 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4354 if ((type
== DeviceType
.VT220
)
4355 || (type
== DeviceType
.XTERM
)) {
4357 if ((collectBuffer
.length() == 1)
4358 && (collectBuffer
.charAt(0) == '(')) {
4360 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4362 if ((collectBuffer
.length() == 1)
4363 && (collectBuffer
.charAt(0) == ')')) {
4365 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4367 if ((collectBuffer
.length() == 1)
4368 && (collectBuffer
.charAt(0) == '*')) {
4370 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4372 if ((collectBuffer
.length() == 1)
4373 && (collectBuffer
.charAt(0) == '+')) {
4375 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4384 if ((collectBuffer
.length() == 1)
4385 && (collectBuffer
.charAt(0) == '(')) {
4386 // G0 --> United Kingdom set
4387 currentState
.g0Charset
= CharacterSet
.UK
;
4389 if ((collectBuffer
.length() == 1)
4390 && (collectBuffer
.charAt(0) == ')')) {
4391 // G1 --> United Kingdom set
4392 currentState
.g1Charset
= CharacterSet
.UK
;
4394 if ((type
== DeviceType
.VT220
)
4395 || (type
== DeviceType
.XTERM
)) {
4397 if ((collectBuffer
.length() == 1)
4398 && (collectBuffer
.charAt(0) == '*')) {
4399 // G2 --> United Kingdom set
4400 currentState
.g2Charset
= CharacterSet
.UK
;
4402 if ((collectBuffer
.length() == 1)
4403 && (collectBuffer
.charAt(0) == '+')) {
4404 // G3 --> United Kingdom set
4405 currentState
.g3Charset
= CharacterSet
.UK
;
4410 if ((collectBuffer
.length() == 1)
4411 && (collectBuffer
.charAt(0) == '(')) {
4413 currentState
.g0Charset
= CharacterSet
.US
;
4415 if ((collectBuffer
.length() == 1)
4416 && (collectBuffer
.charAt(0) == ')')) {
4418 currentState
.g1Charset
= CharacterSet
.US
;
4420 if ((type
== DeviceType
.VT220
)
4421 || (type
== DeviceType
.XTERM
)) {
4423 if ((collectBuffer
.length() == 1)
4424 && (collectBuffer
.charAt(0) == '*')) {
4426 currentState
.g2Charset
= CharacterSet
.US
;
4428 if ((collectBuffer
.length() == 1)
4429 && (collectBuffer
.charAt(0) == '+')) {
4431 currentState
.g3Charset
= CharacterSet
.US
;
4436 if ((type
== DeviceType
.VT220
)
4437 || (type
== DeviceType
.XTERM
)) {
4439 if ((collectBuffer
.length() == 1)
4440 && (collectBuffer
.charAt(0) == '(')) {
4442 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4444 if ((collectBuffer
.length() == 1)
4445 && (collectBuffer
.charAt(0) == ')')) {
4447 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4449 if ((collectBuffer
.length() == 1)
4450 && (collectBuffer
.charAt(0) == '*')) {
4452 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4454 if ((collectBuffer
.length() == 1)
4455 && (collectBuffer
.charAt(0) == '+')) {
4457 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4464 if ((type
== DeviceType
.VT220
)
4465 || (type
== DeviceType
.XTERM
)) {
4467 if ((collectBuffer
.length() == 1)
4468 && (collectBuffer
.charAt(0) == '(')) {
4470 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4472 if ((collectBuffer
.length() == 1)
4473 && (collectBuffer
.charAt(0) == ')')) {
4475 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4477 if ((collectBuffer
.length() == 1)
4478 && (collectBuffer
.charAt(0) == '*')) {
4480 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4482 if ((collectBuffer
.length() == 1)
4483 && (collectBuffer
.charAt(0) == '+')) {
4485 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4490 if ((type
== DeviceType
.VT220
)
4491 || (type
== DeviceType
.XTERM
)) {
4493 if ((collectBuffer
.length() == 1)
4494 && (collectBuffer
.charAt(0) == ' ')) {
4501 if ((type
== DeviceType
.VT220
)
4502 || (type
== DeviceType
.XTERM
)) {
4504 if ((collectBuffer
.length() == 1)
4505 && (collectBuffer
.charAt(0) == ' ')) {
4512 if ((type
== DeviceType
.VT220
)
4513 || (type
== DeviceType
.XTERM
)) {
4515 if ((collectBuffer
.length() == 1)
4516 && (collectBuffer
.charAt(0) == '(')) {
4518 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4520 if ((collectBuffer
.length() == 1)
4521 && (collectBuffer
.charAt(0) == ')')) {
4523 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4525 if ((collectBuffer
.length() == 1)
4526 && (collectBuffer
.charAt(0) == '*')) {
4528 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4530 if ((collectBuffer
.length() == 1)
4531 && (collectBuffer
.charAt(0) == '+')) {
4533 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4541 if ((type
== DeviceType
.VT220
)
4542 || (type
== DeviceType
.XTERM
)) {
4544 if ((collectBuffer
.length() == 1)
4545 && (collectBuffer
.charAt(0) == '(')) {
4547 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4549 if ((collectBuffer
.length() == 1)
4550 && (collectBuffer
.charAt(0) == ')')) {
4552 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4554 if ((collectBuffer
.length() == 1)
4555 && (collectBuffer
.charAt(0) == '*')) {
4557 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4559 if ((collectBuffer
.length() == 1)
4560 && (collectBuffer
.charAt(0) == '+')) {
4562 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4573 if ((type
== DeviceType
.VT220
)
4574 || (type
== DeviceType
.XTERM
)) {
4576 if ((collectBuffer
.length() == 1)
4577 && (collectBuffer
.charAt(0) == '(')) {
4579 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4581 if ((collectBuffer
.length() == 1)
4582 && (collectBuffer
.charAt(0) == ')')) {
4584 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4586 if ((collectBuffer
.length() == 1)
4587 && (collectBuffer
.charAt(0) == '*')) {
4589 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4591 if ((collectBuffer
.length() == 1)
4592 && (collectBuffer
.charAt(0) == '+')) {
4594 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4599 if ((type
== DeviceType
.VT220
)
4600 || (type
== DeviceType
.XTERM
)) {
4602 if ((collectBuffer
.length() == 1)
4603 && (collectBuffer
.charAt(0) == '(')) {
4605 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4607 if ((collectBuffer
.length() == 1)
4608 && (collectBuffer
.charAt(0) == ')')) {
4610 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4612 if ((collectBuffer
.length() == 1)
4613 && (collectBuffer
.charAt(0) == '*')) {
4615 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
4617 if ((collectBuffer
.length() == 1)
4618 && (collectBuffer
.charAt(0) == '+')) {
4620 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
4632 if ((type
== DeviceType
.VT220
)
4633 || (type
== DeviceType
.XTERM
)) {
4635 if ((collectBuffer
.length() == 1)
4636 && (collectBuffer
.charAt(0) == '(')) {
4638 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
4640 if ((collectBuffer
.length() == 1)
4641 && (collectBuffer
.charAt(0) == ')')) {
4643 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
4645 if ((collectBuffer
.length() == 1)
4646 && (collectBuffer
.charAt(0) == '*')) {
4648 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
4650 if ((collectBuffer
.length() == 1)
4651 && (collectBuffer
.charAt(0) == '+')) {
4653 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
4658 if ((type
== DeviceType
.VT220
)
4659 || (type
== DeviceType
.XTERM
)) {
4661 if ((collectBuffer
.length() == 1)
4662 && (collectBuffer
.charAt(0) == '(')) {
4664 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
4666 if ((collectBuffer
.length() == 1)
4667 && (collectBuffer
.charAt(0) == ')')) {
4669 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
4671 if ((collectBuffer
.length() == 1)
4672 && (collectBuffer
.charAt(0) == '*')) {
4674 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
4676 if ((collectBuffer
.length() == 1)
4677 && (collectBuffer
.charAt(0) == '+')) {
4679 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
4726 // 0x9C goes to GROUND
4734 // 00-17, 19, 1C-1F --> execute
4736 handleControlChar(ch
);
4739 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
4740 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4742 scanState
= ScanState
.CSI_INTERMEDIATE
;
4745 // 30-39, 3B --> param, then switch to CSI_PARAM
4746 if ((ch
>= '0') && (ch
<= '9')) {
4748 scanState
= ScanState
.CSI_PARAM
;
4752 scanState
= ScanState
.CSI_PARAM
;
4755 // 3C-3F --> collect, then switch to CSI_PARAM
4756 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
4758 scanState
= ScanState
.CSI_PARAM
;
4761 // 40-7E --> dispatch, then switch to GROUND
4762 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
4765 // ICH - Insert character
4773 // CUD - Cursor down
4777 // CUF - Cursor forward
4781 // CUB - Cursor backward
4785 // CNL - Cursor down and to column 1
4786 if (type
== DeviceType
.XTERM
) {
4791 // CPL - Cursor up and to column 1
4792 if (type
== DeviceType
.XTERM
) {
4797 // CHA - Cursor to column # in current row
4798 if (type
== DeviceType
.XTERM
) {
4803 // CUP - Cursor position
4807 // CHT - Cursor forward X tab stops (default 1)
4808 if (type
== DeviceType
.XTERM
) {
4813 // ED - Erase in display
4817 // EL - Erase in line
4832 // DCH - Delete character
4839 // Scroll up X lines (default 1)
4840 if (type
== DeviceType
.XTERM
) {
4845 // Scroll down X lines (default 1)
4846 if (type
== DeviceType
.XTERM
) {
4855 if ((type
== DeviceType
.VT220
)
4856 || (type
== DeviceType
.XTERM
)) {
4858 // ECH - Erase character
4865 // CBT - Cursor backward X tab stops (default 1)
4866 if (type
== DeviceType
.XTERM
) {
4877 // HPA - Cursor to column # in current row. Same as CHA
4878 if (type
== DeviceType
.XTERM
) {
4883 // HPR - Cursor right. Same as CUF
4884 if (type
== DeviceType
.XTERM
) {
4889 // REP - Repeat last char X times
4890 if (type
== DeviceType
.XTERM
) {
4895 // DA - Device attributes
4899 // VPA - Cursor to row, current column.
4900 if (type
== DeviceType
.XTERM
) {
4905 // VPR - Cursor down. Same as CUD
4906 if (type
== DeviceType
.XTERM
) {
4911 // HVP - Horizontal and vertical position
4915 // TBC - Tabulation clear
4919 // Sets an ANSI or DEC private toggle
4923 if ((type
== DeviceType
.VT220
)
4924 || (type
== DeviceType
.XTERM
)) {
4926 // Printer functions
4934 // Sets an ANSI or DEC private toggle
4938 // SGR - Select graphics rendition
4942 // DSR - Device status report
4949 // DECLL - Load leds
4953 // DECSTBM - Set top and bottom margins
4957 // Save cursor (ANSI.SYS)
4958 if (type
== DeviceType
.XTERM
) {
4959 savedState
.cursorX
= currentState
.cursorX
;
4960 savedState
.cursorY
= currentState
.cursorY
;
4966 // Restore cursor (ANSI.SYS)
4967 if (type
== DeviceType
.XTERM
) {
4968 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
4975 // DECREQTPARM - Request terminal parameters
4991 // 0x9C goes to GROUND
4996 // 0x3A goes to CSI_IGNORE
4998 scanState
= ScanState
.CSI_IGNORE
;
5003 // 00-17, 19, 1C-1F --> execute
5005 handleControlChar(ch
);
5008 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5009 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5011 scanState
= ScanState
.CSI_INTERMEDIATE
;
5014 // 30-39, 3B --> param
5015 if ((ch
>= '0') && (ch
<= '9')) {
5022 // 0x3A goes to CSI_IGNORE
5024 scanState
= ScanState
.CSI_IGNORE
;
5026 // 0x3C-3F goes to CSI_IGNORE
5027 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5028 scanState
= ScanState
.CSI_IGNORE
;
5031 // 40-7E --> dispatch, then switch to GROUND
5032 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5035 // ICH - Insert character
5043 // CUD - Cursor down
5047 // CUF - Cursor forward
5051 // CUB - Cursor backward
5055 // CNL - Cursor down and to column 1
5056 if (type
== DeviceType
.XTERM
) {
5061 // CPL - Cursor up and to column 1
5062 if (type
== DeviceType
.XTERM
) {
5067 // CHA - Cursor to column # in current row
5068 if (type
== DeviceType
.XTERM
) {
5073 // CUP - Cursor position
5077 // CHT - Cursor forward X tab stops (default 1)
5078 if (type
== DeviceType
.XTERM
) {
5083 // ED - Erase in display
5087 // EL - Erase in line
5102 // DCH - Delete character
5109 // Scroll up X lines (default 1)
5110 if (type
== DeviceType
.XTERM
) {
5115 // Scroll down X lines (default 1)
5116 if (type
== DeviceType
.XTERM
) {
5125 if ((type
== DeviceType
.VT220
)
5126 || (type
== DeviceType
.XTERM
)) {
5128 // ECH - Erase character
5135 // CBT - Cursor backward X tab stops (default 1)
5136 if (type
== DeviceType
.XTERM
) {
5147 // HPA - Cursor to column # in current row. Same as CHA
5148 if (type
== DeviceType
.XTERM
) {
5153 // HPR - Cursor right. Same as CUF
5154 if (type
== DeviceType
.XTERM
) {
5159 // REP - Repeat last char X times
5160 if (type
== DeviceType
.XTERM
) {
5165 // DA - Device attributes
5169 // VPA - Cursor to row, current column.
5170 if (type
== DeviceType
.XTERM
) {
5175 // VPR - Cursor down. Same as CUD
5176 if (type
== DeviceType
.XTERM
) {
5181 // HVP - Horizontal and vertical position
5185 // TBC - Tabulation clear
5189 // Sets an ANSI or DEC private toggle
5193 if ((type
== DeviceType
.VT220
)
5194 || (type
== DeviceType
.XTERM
)) {
5196 // Printer functions
5204 // Sets an ANSI or DEC private toggle
5208 // SGR - Select graphics rendition
5212 // DSR - Device status report
5219 // DECLL - Load leds
5223 // DECSTBM - Set top and bottom margins
5233 // DECREQTPARM - Request terminal parameters
5250 case CSI_INTERMEDIATE
:
5251 // 00-17, 19, 1C-1F --> execute
5253 handleControlChar(ch
);
5256 // 20-2F --> collect
5257 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5261 // 0x30-3F goes to CSI_IGNORE
5262 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5263 scanState
= ScanState
.CSI_IGNORE
;
5266 // 40-7E --> dispatch, then switch to GROUND
5267 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5319 if (((type
== DeviceType
.VT220
)
5320 || (type
== DeviceType
.XTERM
))
5321 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5323 // DECSCL - compatibility level
5326 if ((type
== DeviceType
.XTERM
)
5327 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
5329 // DECSTR - Soft terminal reset
5334 if (((type
== DeviceType
.VT220
)
5335 || (type
== DeviceType
.XTERM
))
5336 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5364 // 00-17, 19, 1C-1F --> execute
5366 handleControlChar(ch
);
5369 // 20-2F --> collect
5370 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5374 // 40-7E --> ignore, then switch to GROUND
5375 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5379 // 20-3F, 7F --> ignore
5385 // 0x9C goes to GROUND
5390 // 0x1B 0x5C goes to GROUND
5395 if ((collectBuffer
.length() > 0)
5396 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5402 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5403 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5405 scanState
= ScanState
.DCS_INTERMEDIATE
;
5408 // 30-39, 3B --> param, then switch to DCS_PARAM
5409 if ((ch
>= '0') && (ch
<= '9')) {
5411 scanState
= ScanState
.DCS_PARAM
;
5415 scanState
= ScanState
.DCS_PARAM
;
5418 // 3C-3F --> collect, then switch to DCS_PARAM
5419 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5421 scanState
= ScanState
.DCS_PARAM
;
5424 // 00-17, 19, 1C-1F, 7F --> ignore
5426 // 0x3A goes to DCS_IGNORE
5428 scanState
= ScanState
.DCS_IGNORE
;
5431 // 0x40-7E goes to DCS_PASSTHROUGH
5432 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5433 scanState
= ScanState
.DCS_PASSTHROUGH
;
5437 case DCS_INTERMEDIATE
:
5439 // 0x9C goes to GROUND
5444 // 0x1B 0x5C goes to GROUND
5449 if ((collectBuffer
.length() > 0) &&
5450 (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5456 // 0x30-3F goes to DCS_IGNORE
5457 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5458 scanState
= ScanState
.DCS_IGNORE
;
5461 // 0x40-7E goes to DCS_PASSTHROUGH
5462 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5463 scanState
= ScanState
.DCS_PASSTHROUGH
;
5466 // 00-17, 19, 1C-1F, 7F --> ignore
5471 // 0x9C goes to GROUND
5476 // 0x1B 0x5C goes to GROUND
5481 if ((collectBuffer
.length() > 0) &&
5482 (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5488 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5489 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5491 scanState
= ScanState
.DCS_INTERMEDIATE
;
5494 // 30-39, 3B --> param
5495 if ((ch
>= '0') && (ch
<= '9')) {
5502 // 00-17, 19, 1C-1F, 7F --> ignore
5504 // 0x3A, 3C-3F goes to DCS_IGNORE
5506 scanState
= ScanState
.DCS_IGNORE
;
5508 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5509 scanState
= ScanState
.DCS_IGNORE
;
5512 // 0x40-7E goes to DCS_PASSTHROUGH
5513 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5514 scanState
= ScanState
.DCS_PASSTHROUGH
;
5518 case DCS_PASSTHROUGH
:
5519 // 0x9C goes to GROUND
5524 // 0x1B 0x5C goes to GROUND
5529 if ((collectBuffer
.length() > 0)
5530 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5536 // 00-17, 19, 1C-1F, 20-7E --> put
5544 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5547 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5556 // 00-17, 19, 1C-1F, 20-7F --> ignore
5558 // 0x9C goes to GROUND
5565 case SOSPMAPC_STRING
:
5566 // 00-17, 19, 1C-1F, 20-7F --> ignore
5568 // 0x9C goes to GROUND
5576 // Special case for Xterm: OSC can pass control characters
5577 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5581 // 00-17, 19, 1C-1F --> ignore
5583 // 20-7F --> osc_put
5584 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5588 // 0x9C goes to GROUND
5595 case VT52_DIRECT_CURSOR_ADDRESS
:
5596 // This is a special case for the VT52 sequence "ESC Y l c"
5597 if (collectBuffer
.length() == 0) {
5599 } else if (collectBuffer
.length() == 1) {
5600 // We've got the two characters, one in the buffer and the
5602 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
5611 * Expose current cursor X to outside world.
5613 * @return current cursor X
5615 public final int getCursorX() {
5616 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
5617 return currentState
.cursorX
* 2;
5619 return currentState
.cursorX
;
5623 * Expose current cursor Y to outside world.
5625 * @return current cursor Y
5627 public final int getCursorY() {
5628 return currentState
.cursorY
;
5632 * Read function runs on a separate thread.
5635 boolean utf8
= false;
5636 boolean done
= false;
5638 if (type
== DeviceType
.XTERM
) {
5642 // available() will often return > 1, so we need to read in chunks to
5644 char [] readBufferUTF8
= null;
5645 byte [] readBuffer
= null;
5647 readBufferUTF8
= new char[128];
5649 readBuffer
= new byte[128];
5652 while (!done
&& !stopReaderThread
) {
5654 int n
= inputStream
.available();
5655 // System.err.printf("available() %d\n", n); System.err.flush();
5657 if (readBufferUTF8
.length
< n
) {
5658 // The buffer wasn't big enough, make it huger
5659 int newSizeHalf
= Math
.max(readBufferUTF8
.length
, n
);
5661 readBufferUTF8
= new char[newSizeHalf
* 2];
5664 if (readBuffer
.length
< n
) {
5665 // The buffer wasn't big enough, make it huger
5666 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
5667 readBuffer
= new byte[newSizeHalf
* 2];
5673 rc
= input
.read(readBufferUTF8
, 0,
5674 readBufferUTF8
.length
);
5676 rc
= inputStream
.read(readBuffer
, 0,
5679 // System.err.printf("read() %d\n", rc); System.err.flush();
5684 for (int i
= 0; i
< rc
; i
++) {
5687 ch
= readBufferUTF8
[i
];
5691 // Don't step on UI events
5692 synchronized (this) {
5697 // System.err.println("end while loop"); System.err.flush();
5698 } catch (IOException e
) {
5699 e
.printStackTrace();
5702 } // while ((done == false) && (stopReaderThread == false))
5704 // Let the rest of the world know that I am done.
5705 stopReaderThread
= true;
5707 // System.err.println("*** run() exiting..."); System.err.flush();