2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 package jexer
.tterminal
;
33 import java
.io
.InputStream
;
34 import java
.io
.InputStreamReader
;
35 import java
.io
.IOException
;
36 import java
.io
.OutputStream
;
37 import java
.io
.OutputStreamWriter
;
38 import java
.io
.Reader
;
39 import java
.io
.UnsupportedEncodingException
;
40 import java
.io
.Writer
;
41 import java
.util
.ArrayList
;
42 import java
.util
.Collections
;
43 import java
.util
.LinkedList
;
44 import java
.util
.List
;
46 import jexer
.TKeypress
;
47 import jexer
.event
.TMouseEvent
;
48 import jexer
.bits
.Color
;
49 import jexer
.bits
.Cell
;
50 import jexer
.bits
.CellAttributes
;
51 import static jexer
.TKeypress
.*;
54 * This implements a complex ANSI ECMA-48/ISO 6429/ANSI X3.64 type consoles,
55 * including a scrollback buffer.
58 * It currently implements VT100, VT102, VT220, and XTERM with the following
62 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
63 * failure with XTERM. This is due to vttest failing to decode the UTF-8
67 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
68 * from VT100 are not supported.
71 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
72 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
73 * because DECSCL is not supported, it will fail the last part of the
74 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
77 * - Numeric/application keys from the number pad are not supported because
78 * they are not exposed from the TKeypress API.
81 * - VT52 HOLD SCREEN mode is not supported.
84 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
85 * numerators) are not rendered correctly.
88 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
90 public class ECMA48
implements Runnable
{
93 * The emulator can emulate several kinds of terminals.
95 public enum DeviceType
{
97 * DEC VT100 but also including the three VT102 functions.
118 * Return the proper primary Device Attributes string.
120 * @return string to send to remote side that is appropriate for the
123 private String
deviceTypeResponse() {
126 // "I am a VT100 with advanced video option" (often VT102)
134 // "I am a VT220" - 7 bit version
136 return "\033[?62;1;6c";
138 // "I am a VT220" - 8 bit version
139 return "\u009b?62;1;6c";
141 // "I am a VT100 with advanced video option" (often VT102)
144 throw new IllegalArgumentException("Invalid device type: " + type
);
149 * Return the proper TERM environment variable for this device type.
151 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
152 * @return "vt100", "xterm", etc.
154 public static String
deviceTypeTerm(final DeviceType deviceType
) {
155 switch (deviceType
) {
169 throw new IllegalArgumentException("Invalid device type: "
175 * Return the proper LANG for this device type. Only XTERM devices know
176 * about UTF-8, the others are defined by their standard to be either
177 * 7-bit or 8-bit characters only.
179 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
180 * @param baseLang a base language without UTF-8 flag such as "C" or
182 * @return "en_US", "en_US.UTF-8", etc.
184 public static String
deviceTypeLang(final DeviceType deviceType
,
185 final String baseLang
) {
187 switch (deviceType
) {
195 return baseLang
+ ".UTF-8";
198 throw new IllegalArgumentException("Invalid device type: "
204 * Write a string directly to the remote side.
206 * @param str string to send
208 private void writeRemote(final String str
) {
209 if (stopReaderThread
) {
210 // Reader hit EOF, bail out now.
215 // System.err.printf("writeRemote() '%s'\n", str);
221 if (outputStream
== null) {
225 for (int i
= 0; i
< str
.length(); i
++) {
226 outputStream
.write(str
.charAt(i
));
228 outputStream
.flush();
229 } catch (IOException e
) {
235 if (output
== null) {
241 } catch (IOException e
) {
247 throw new IllegalArgumentException("Invalid device type: " + type
);
252 * Close the input and output streams and stop the reader thread. Note
253 * that it is safe to call this multiple times.
255 public final void close() {
257 // Synchronize so we don't stomp on the reader thread.
258 synchronized (this) {
260 // Close the input stream
265 if (inputStream
!= null) {
268 } catch (IOException e
) {
278 } catch (IOException e
) {
287 // Tell the reader thread to stop looking at input.
288 if (stopReaderThread
== false) {
289 stopReaderThread
= true;
292 } catch (InterruptedException e
) {
297 // Close the output stream.
302 if (outputStream
!= null) {
304 outputStream
.close();
305 } catch (IOException e
) {
312 if (output
!= null) {
315 } catch (IOException e
) {
322 throw new IllegalArgumentException("Invalid device type: "
325 } // synchronized (this)
329 * When true, the reader thread is expected to exit.
331 private volatile boolean stopReaderThread
= false;
336 private Thread readerThread
= null;
339 * See if the reader thread is still running.
341 * @return if true, we are still connected to / reading from the remote
344 public final boolean isReading() {
345 return (!stopReaderThread
);
349 * The type of emulator to be.
351 private DeviceType type
= DeviceType
.VT102
;
354 * Obtain a new blank display line for an external user
355 * (e.g. TTerminalWindow).
357 * @return new blank line
359 public final DisplayLine
getBlankDisplayLine() {
360 return new DisplayLine(currentState
.attr
);
364 * The scrollback buffer characters + attributes.
366 private volatile List
<DisplayLine
> scrollback
;
369 * Get the scrollback buffer.
371 * @return the scrollback buffer
373 public final List
<DisplayLine
> getScrollbackBuffer() {
378 * The raw display buffer characters + attributes.
380 private volatile List
<DisplayLine
> display
;
383 * Get the display buffer.
385 * @return the display buffer
387 public final List
<DisplayLine
> getDisplayBuffer() {
392 * The terminal's input. For type == XTERM, this is an InputStreamReader
393 * with UTF-8 encoding.
395 private Reader input
;
398 * The terminal's raw InputStream. This is used for type != XTERM.
400 private volatile InputStream inputStream
;
403 * The terminal's output. For type == XTERM, this wraps an
404 * OutputStreamWriter with UTF-8 encoding.
406 private Writer output
;
409 * The terminal's raw OutputStream. This is used for type != XTERM.
411 private OutputStream outputStream
;
414 * Parser character scan states.
431 VT52_DIRECT_CURSOR_ADDRESS
435 * Current scanning state.
437 private ScanState scanState
;
440 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
441 * can't really use it in keypress() because we do not see number pad
442 * events from TKeypress.
444 private enum KeypadMode
{
450 * Arrow keys can emit three different sequences (DECCKM or VT52
453 private enum ArrowKeyMode
{
460 * Available character sets for GL, GR, G0, G1, G2, G3.
462 private enum CharacterSet
{
483 * Single-shift states used by the C1 control characters SS2 (0x8E) and
486 private enum Singleshift
{
493 * VT220+ lockshift states.
495 private enum LockshiftMode
{
505 * XTERM mouse reporting protocols.
507 private enum MouseProtocol
{
516 * Which mouse protocol is active.
518 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
521 * XTERM mouse reporting encodings.
523 private enum MouseEncoding
{
530 * Which mouse encoding is active.
532 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
535 * Physical display width. We start at 80x24, but the user can resize us
541 * Get the display width.
543 * @return the width (usually 80 or 132)
545 public final int getWidth() {
550 * Physical display height. We start at 80x24, but the user can resize
556 * Get the display height.
558 * @return the height (usually 24)
560 public final int getHeight() {
565 * Top margin of the scrolling region.
567 private int scrollRegionTop
;
570 * Bottom margin of the scrolling region.
572 private int scrollRegionBottom
;
575 * Right margin column number. This can be selected by the remote side
576 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
578 private int rightMargin
;
581 * Last character printed.
586 * VT100-style line wrapping: a character is placed in column 80 (or
587 * 132), but the line does NOT wrap until another character is written to
588 * column 1 of the next line, after which the cursor moves to column 2.
590 private boolean wrapLineFlag
;
593 * VT220 single shift flag.
595 private Singleshift singleshift
= Singleshift
.NONE
;
598 * true = insert characters, false = overwrite.
600 private boolean insertMode
= false;
603 * VT52 mode as selected by DECANM. True means VT52, false means
604 * ANSI. Default is ANSI.
606 private boolean vt52Mode
= false;
609 * Visible cursor (DECTCEM).
611 private boolean cursorVisible
= true;
614 * Get visible cursor flag.
616 * @return if true, the cursor is visible
618 public final boolean isCursorVisible() {
619 return cursorVisible
;
623 * Screen title as set by the xterm OSC sequence. Lots of applications
624 * send a screenTitle regardless of whether it is an xterm client or not.
626 private String screenTitle
= "";
629 * Get the screen title as set by the xterm OSC sequence. Lots of
630 * applications send a screenTitle regardless of whether it is an xterm
633 * @return screen title
635 public final String
getScreenTitle() {
640 * Parameter characters being collected.
642 private List
<Integer
> csiParams
;
645 * Non-csi collect buffer.
647 private StringBuilder collectBuffer
;
650 * When true, use the G1 character set.
652 private boolean shiftOut
= false;
655 * Horizontal tab stop locations.
657 private List
<Integer
> tabStops
;
660 * S8C1T. True means 8bit controls, false means 7bit controls.
662 private boolean s8c1t
= false;
665 * Printer mode. True means send all output to printer, which discards
668 private boolean printerControllerMode
= false;
671 * LMN line mode. If true, linefeed() puts the cursor on the first
672 * column of the next line. If false, linefeed() puts the cursor one
673 * line down on the current line. The default is false.
675 private boolean newLineMode
= false;
678 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
680 private ArrowKeyMode arrowKeyMode
;
683 * Whether number pad keys send VT100 or VT52, application or numeric
686 @SuppressWarnings("unused")
687 private KeypadMode keypadMode
;
690 * When true, the terminal is in 132-column mode (DECCOLM).
692 private boolean columns132
= false;
695 * Get 132 columns value.
697 * @return if true, the terminal is in 132 column mode
699 public final boolean isColumns132() {
704 * true = reverse video. Set by DECSCNM.
706 private boolean reverseVideo
= false;
709 * false = echo characters locally.
711 private boolean fullDuplex
= true;
714 * DECSC/DECRC save/restore a subset of the total state. This class
715 * encapsulates those specific flags/modes.
717 private class SaveableState
{
720 * When true, cursor positions are relative to the scrolling region.
722 public boolean originMode
= false;
725 * The current editing X position.
727 public int cursorX
= 0;
730 * The current editing Y position.
732 public int cursorY
= 0;
735 * Which character set is currently selected in G0.
737 public CharacterSet g0Charset
= CharacterSet
.US
;
740 * Which character set is currently selected in G1.
742 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
745 * Which character set is currently selected in G2.
747 public CharacterSet g2Charset
= CharacterSet
.US
;
750 * Which character set is currently selected in G3.
752 public CharacterSet g3Charset
= CharacterSet
.US
;
755 * Which character set is currently selected in GR.
757 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
760 * The current drawing attributes.
762 public CellAttributes attr
;
767 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
772 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
777 public boolean lineWrap
= true;
782 public void reset() {
786 g0Charset
= CharacterSet
.US
;
787 g1Charset
= CharacterSet
.DRAWING
;
788 g2Charset
= CharacterSet
.US
;
789 g3Charset
= CharacterSet
.US
;
790 grCharset
= CharacterSet
.DRAWING
;
791 attr
= new CellAttributes();
792 glLockshift
= LockshiftMode
.NONE
;
793 grLockshift
= LockshiftMode
.NONE
;
798 * Copy attributes from another instance.
800 * @param that the other instance to match
802 public void setTo(final SaveableState that
) {
803 this.originMode
= that
.originMode
;
804 this.cursorX
= that
.cursorX
;
805 this.cursorY
= that
.cursorY
;
806 this.g0Charset
= that
.g0Charset
;
807 this.g1Charset
= that
.g1Charset
;
808 this.g2Charset
= that
.g2Charset
;
809 this.g3Charset
= that
.g3Charset
;
810 this.grCharset
= that
.grCharset
;
811 this.attr
= new CellAttributes();
812 this.attr
.setTo(that
.attr
);
813 this.glLockshift
= that
.glLockshift
;
814 this.grLockshift
= that
.grLockshift
;
815 this.lineWrap
= that
.lineWrap
;
819 * Public constructor.
821 public SaveableState() {
827 * The current terminal state.
829 private SaveableState currentState
;
832 * The last saved terminal state.
834 private SaveableState savedState
;
837 * Clear the CSI parameters and flags.
839 private void toGround() {
841 collectBuffer
= new StringBuilder(8);
842 scanState
= ScanState
.GROUND
;
846 * Reset the tab stops list.
848 private void resetTabStops() {
850 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
851 tabStops
.add(new Integer(i
* 8));
856 * Reset the emulation state.
858 private void reset() {
860 currentState
= new SaveableState();
861 savedState
= new SaveableState();
862 scanState
= ScanState
.GROUND
;
866 scrollRegionBottom
= height
- 1;
867 rightMargin
= width
- 1;
869 arrowKeyMode
= ArrowKeyMode
.ANSI
;
870 keypadMode
= KeypadMode
.Numeric
;
871 wrapLineFlag
= false;
879 reverseVideo
= false;
881 cursorVisible
= true;
884 singleshift
= Singleshift
.NONE
;
886 printerControllerMode
= false;
889 mouseProtocol
= MouseProtocol
.OFF
;
890 mouseEncoding
= MouseEncoding
.X10
;
900 * Public constructor.
902 * @param type one of the DeviceType constants to select VT100, VT102,
904 * @param inputStream an InputStream connected to the remote side. For
905 * type == XTERM, inputStream is converted to a Reader with UTF-8
907 * @param outputStream an OutputStream connected to the remote user. For
908 * type == XTERM, outputStream is converted to a Writer with UTF-8
910 * @throws UnsupportedEncodingException if an exception is thrown when
911 * creating the InputStreamReader
913 public ECMA48(final DeviceType type
, final InputStream inputStream
,
914 final OutputStream outputStream
) throws UnsupportedEncodingException
{
916 assert (inputStream
!= null);
917 assert (outputStream
!= null);
919 csiParams
= new ArrayList
<Integer
>();
920 tabStops
= new ArrayList
<Integer
>();
921 scrollback
= new LinkedList
<DisplayLine
>();
922 display
= new LinkedList
<DisplayLine
>();
925 this.inputStream
= inputStream
;
926 if (type
== DeviceType
.XTERM
) {
927 this.input
= new InputStreamReader(inputStream
, "UTF-8");
928 this.output
= new OutputStreamWriter(outputStream
, "UTF-8");
929 this.outputStream
= null;
932 this.outputStream
= outputStream
;
936 for (int i
= 0; i
< height
; i
++) {
937 display
.add(new DisplayLine(currentState
.attr
));
940 // Spin up the input reader
941 readerThread
= new Thread(this);
942 readerThread
.start();
946 * Append a new line to the bottom of the display, adding lines off the
947 * top to the scrollback buffer.
949 private void newDisplayLine() {
950 // Scroll the top line off into the scrollback buffer
951 scrollback
.add(display
.get(0));
953 DisplayLine line
= new DisplayLine(currentState
.attr
);
954 line
.setReverseColor(reverseVideo
);
959 * Wraps the current line.
961 private void wrapCurrentLine() {
962 if (currentState
.cursorY
== height
- 1) {
965 if (currentState
.cursorY
< height
- 1) {
966 currentState
.cursorY
++;
968 currentState
.cursorX
= 0;
972 * Handle a carriage return.
974 private void carriageReturn() {
975 currentState
.cursorX
= 0;
976 wrapLineFlag
= false;
980 * Reverse the color of the visible display.
982 private void invertDisplayColors() {
983 for (DisplayLine line
: display
) {
984 line
.setReverseColor(!line
.isReverseColor());
991 private void linefeed() {
993 if (currentState
.cursorY
< scrollRegionBottom
) {
994 // Increment screen y
995 currentState
.cursorY
++;
998 // Screen y does not increment
1001 * Two cases: either we're inside a scrolling region or not. If
1002 * the scrolling region bottom is the bottom of the screen, then
1003 * push the top line into the buffer. Else scroll the scrolling
1006 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1008 // We're at the bottom of the scroll region, AND the scroll
1009 // region is the entire screen.
1015 // We're at the bottom of the scroll region, AND the scroll
1016 // region is NOT the entire screen.
1017 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1022 currentState
.cursorX
= 0;
1024 wrapLineFlag
= false;
1028 * Prints one character to the display buffer.
1030 * @param ch character to display
1032 private void printCharacter(final char ch
) {
1033 int rightMargin
= this.rightMargin
;
1035 // Check if we have double-width, and if so chop at 40/66 instead of
1037 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1038 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1041 // Check the unusually-complicated line wrapping conditions...
1042 if (currentState
.cursorX
== rightMargin
) {
1044 if (currentState
.lineWrap
== true) {
1046 * This case happens when: the cursor was already on the
1047 * right margin (either through printing or by an explicit
1048 * placement command), and a character was printed.
1050 * The line wraps only when a new character arrives AND the
1051 * cursor is already on the right margin AND has placed a
1052 * character in its cell. Easier to see than to explain.
1054 if (wrapLineFlag
== false) {
1056 * This block marks the case that we are in the margin
1057 * and the first character has been received and printed.
1059 wrapLineFlag
= true;
1062 * This block marks the case that we are in the margin
1063 * and the second character has been received and
1066 wrapLineFlag
= false;
1070 } else if (currentState
.cursorX
<= rightMargin
) {
1072 * This is the normal case: a character came in and was printed
1073 * to the left of the right margin column.
1076 // Turn off VT100 special-case flag
1077 wrapLineFlag
= false;
1080 // "Print" the character
1081 Cell newCell
= new Cell(ch
);
1082 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1083 newCellAttributes
.setTo(currentState
.attr
);
1084 DisplayLine line
= display
.get(currentState
.cursorY
);
1085 // Insert mode special case
1086 if (insertMode
== true) {
1087 line
.insert(currentState
.cursorX
, newCell
);
1089 // Replace an existing character
1090 line
.replace(currentState
.cursorX
, newCell
);
1093 // Increment horizontal
1094 if (wrapLineFlag
== false) {
1095 currentState
.cursorX
++;
1096 if (currentState
.cursorX
> rightMargin
) {
1097 currentState
.cursorX
--;
1103 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1104 * send to the remote side.
1106 * @param mouse mouse event received from the local user
1108 public void mouse(final TMouseEvent mouse
) {
1111 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1112 mouseProtocol, mouseEncoding, mouse);
1115 if (mouseEncoding
== MouseEncoding
.X10
) {
1116 // We will support X10 but only for (160,94) and smaller.
1117 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1122 switch (mouseProtocol
) {
1129 // Only report button presses
1130 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1136 // Only report button presses and releases
1137 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1138 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1146 * Only report button presses, button releases, and motions that
1147 * have a button down (i.e. drag-and-drop).
1149 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1150 if (!mouse
.isMouse1()
1151 && !mouse
.isMouse2()
1152 && !mouse
.isMouse3()
1153 && !mouse
.isMouseWheelUp()
1154 && !mouse
.isMouseWheelDown()
1162 // Report everything
1166 // Now encode the event
1167 StringBuilder sb
= new StringBuilder(6);
1168 if (mouseEncoding
== MouseEncoding
.SGR
) {
1169 sb
.append((char) 0x1B);
1172 if (mouse
.isMouse1()) {
1173 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1178 } else if (mouse
.isMouse2()) {
1179 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1184 } else if (mouse
.isMouse3()) {
1185 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1190 } else if (mouse
.isMouseWheelUp()) {
1192 } else if (mouse
.isMouseWheelDown()) {
1195 // This is motion with no buttons down.
1199 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1202 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1209 // X10 and UTF8 encodings
1210 sb
.append((char) 0x1B);
1213 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1214 sb
.append((char) (0x03 + 32));
1215 } else if (mouse
.isMouse1()) {
1216 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1217 sb
.append((char) (0x00 + 32 + 32));
1219 sb
.append((char) (0x00 + 32));
1221 } else if (mouse
.isMouse2()) {
1222 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1223 sb
.append((char) (0x01 + 32 + 32));
1225 sb
.append((char) (0x01 + 32));
1227 } else if (mouse
.isMouse3()) {
1228 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1229 sb
.append((char) (0x02 + 32 + 32));
1231 sb
.append((char) (0x02 + 32));
1233 } else if (mouse
.isMouseWheelUp()) {
1234 sb
.append((char) (0x04 + 64));
1235 } else if (mouse
.isMouseWheelDown()) {
1236 sb
.append((char) (0x05 + 64));
1238 // This is motion with no buttons down.
1239 sb
.append((char) (0x03 + 32));
1242 sb
.append((char) (mouse
.getX() + 33));
1243 sb
.append((char) (mouse
.getY() + 33));
1246 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1247 writeRemote(sb
.toString());
1251 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1252 * send to the remote side.
1254 * @param keypress keypress received from the local user
1256 public void keypress(final TKeypress keypress
) {
1257 writeRemote(keypressToString(keypress
));
1261 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1263 * @param keypress keypress received from the local user
1264 * @return string to transmit to the remote side
1266 private String
keypressToString(final TKeypress keypress
) {
1268 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1270 * If this is a control character, process it like it came from
1273 if (keypress
.getChar() < 0x20) {
1274 handleControlChar(keypress
.getChar());
1276 // Local echo for everything else
1277 printCharacter(keypress
.getChar());
1281 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1286 // Handle control characters
1287 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1288 StringBuilder sb
= new StringBuilder();
1289 char ch
= keypress
.getChar();
1292 return sb
.toString();
1295 // Handle alt characters
1296 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1297 StringBuilder sb
= new StringBuilder("\033");
1298 char ch
= keypress
.getChar();
1300 return sb
.toString();
1303 if (keypress
.equals(kbBackspace
)) {
1316 if (keypress
.equals(kbLeft
)) {
1317 switch (arrowKeyMode
) {
1327 if (keypress
.equals(kbRight
)) {
1328 switch (arrowKeyMode
) {
1338 if (keypress
.equals(kbUp
)) {
1339 switch (arrowKeyMode
) {
1349 if (keypress
.equals(kbDown
)) {
1350 switch (arrowKeyMode
) {
1360 if (keypress
.equals(kbHome
)) {
1361 switch (arrowKeyMode
) {
1371 if (keypress
.equals(kbEnd
)) {
1372 switch (arrowKeyMode
) {
1382 if (keypress
.equals(kbF1
)) {
1390 if (keypress
.equals(kbF2
)) {
1398 if (keypress
.equals(kbF3
)) {
1406 if (keypress
.equals(kbF4
)) {
1414 if (keypress
.equals(kbF5
)) {
1427 if (keypress
.equals(kbF6
)) {
1440 if (keypress
.equals(kbF7
)) {
1453 if (keypress
.equals(kbF8
)) {
1466 if (keypress
.equals(kbF9
)) {
1479 if (keypress
.equals(kbF10
)) {
1492 if (keypress
.equals(kbF11
)) {
1496 if (keypress
.equals(kbF12
)) {
1500 if (keypress
.equals(kbShiftF1
)) {
1508 if (keypress
.equals(kbShiftF2
)) {
1516 if (keypress
.equals(kbShiftF3
)) {
1524 if (keypress
.equals(kbShiftF4
)) {
1532 if (keypress
.equals(kbShiftF5
)) {
1534 return "\033[15;2~";
1537 if (keypress
.equals(kbShiftF6
)) {
1539 return "\033[17;2~";
1542 if (keypress
.equals(kbShiftF7
)) {
1544 return "\033[18;2~";
1547 if (keypress
.equals(kbShiftF8
)) {
1549 return "\033[19;2~";
1552 if (keypress
.equals(kbShiftF9
)) {
1554 return "\033[20;2~";
1557 if (keypress
.equals(kbShiftF10
)) {
1559 return "\033[21;2~";
1562 if (keypress
.equals(kbShiftF11
)) {
1564 return "\033[23;2~";
1567 if (keypress
.equals(kbShiftF12
)) {
1569 return "\033[24;2~";
1572 if (keypress
.equals(kbCtrlF1
)) {
1580 if (keypress
.equals(kbCtrlF2
)) {
1588 if (keypress
.equals(kbCtrlF3
)) {
1596 if (keypress
.equals(kbCtrlF4
)) {
1604 if (keypress
.equals(kbCtrlF5
)) {
1606 return "\033[15;5~";
1609 if (keypress
.equals(kbCtrlF6
)) {
1611 return "\033[17;5~";
1614 if (keypress
.equals(kbCtrlF7
)) {
1616 return "\033[18;5~";
1619 if (keypress
.equals(kbCtrlF8
)) {
1621 return "\033[19;5~";
1624 if (keypress
.equals(kbCtrlF9
)) {
1626 return "\033[20;5~";
1629 if (keypress
.equals(kbCtrlF10
)) {
1631 return "\033[21;5~";
1634 if (keypress
.equals(kbCtrlF11
)) {
1636 return "\033[23;5~";
1639 if (keypress
.equals(kbCtrlF12
)) {
1641 return "\033[24;5~";
1644 if (keypress
.equals(kbPgUp
)) {
1649 if (keypress
.equals(kbPgDn
)) {
1654 if (keypress
.equals(kbIns
)) {
1659 if (keypress
.equals(kbShiftIns
)) {
1660 // This is what xterm sends for SHIFT-INS
1662 // This is what xterm sends for CTRL-INS
1663 // return "\033[2;5~";
1666 if (keypress
.equals(kbShiftDel
)) {
1667 // This is what xterm sends for SHIFT-DEL
1669 // This is what xterm sends for CTRL-DEL
1670 // return "\033[3;5~";
1673 if (keypress
.equals(kbDel
)) {
1674 // Delete sends real delete for VTxxx
1676 // return "\033[3~";
1679 if (keypress
.equals(kbEnter
)) {
1683 if (keypress
.equals(kbEsc
)) {
1687 if (keypress
.equals(kbAltEsc
)) {
1691 if (keypress
.equals(kbTab
)) {
1695 // Non-alt, non-ctrl characters
1696 if (!keypress
.isFnKey()) {
1697 StringBuilder sb
= new StringBuilder();
1698 sb
.append(keypress
.getChar());
1699 return sb
.toString();
1705 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1708 * @param ch 8-bit character from the remote side
1709 * @param charsetGl character set defined for GL
1710 * @param charsetGr character set defined for GR
1711 * @return character to display on the screen
1713 private char mapCharacterCharset(final char ch
,
1714 final CharacterSet charsetGl
,
1715 final CharacterSet charsetGr
) {
1717 int lookupChar
= ch
;
1718 CharacterSet lookupCharset
= charsetGl
;
1721 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
1722 lookupCharset
= charsetGr
;
1726 switch (lookupCharset
) {
1729 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
1732 return DECCharacterSets
.UK
[lookupChar
];
1735 return DECCharacterSets
.US_ASCII
[lookupChar
];
1738 return DECCharacterSets
.NL
[lookupChar
];
1741 return DECCharacterSets
.FI
[lookupChar
];
1744 return DECCharacterSets
.FR
[lookupChar
];
1747 return DECCharacterSets
.FR_CA
[lookupChar
];
1750 return DECCharacterSets
.DE
[lookupChar
];
1753 return DECCharacterSets
.IT
[lookupChar
];
1756 return DECCharacterSets
.NO
[lookupChar
];
1759 return DECCharacterSets
.ES
[lookupChar
];
1762 return DECCharacterSets
.SV
[lookupChar
];
1765 return DECCharacterSets
.SWISS
[lookupChar
];
1767 case DEC_SUPPLEMENTAL
:
1768 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
1771 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
1774 return DECCharacterSets
.US_ASCII
[lookupChar
];
1777 return DECCharacterSets
.US_ASCII
[lookupChar
];
1780 throw new IllegalArgumentException("Invalid character set value: "
1786 * Map an 8-bit byte into a printable character.
1788 * @param ch either 8-bit or Unicode character from the remote side
1789 * @return character to display on the screen
1791 private char mapCharacter(final char ch
) {
1793 // Unicode character, just return it
1797 CharacterSet charsetGl
= currentState
.g0Charset
;
1798 CharacterSet charsetGr
= currentState
.grCharset
;
1800 if (vt52Mode
== true) {
1801 if (shiftOut
== true) {
1802 // Shifted out character, pull from VT52 graphics
1803 charsetGl
= currentState
.g1Charset
;
1804 charsetGr
= CharacterSet
.US
;
1807 charsetGl
= currentState
.g0Charset
;
1808 charsetGr
= CharacterSet
.US
;
1811 // Pull the character
1812 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1816 if (shiftOut
== true) {
1817 // Shifted out character, pull from G1
1818 charsetGl
= currentState
.g1Charset
;
1819 charsetGr
= currentState
.grCharset
;
1821 // Pull the character
1822 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1826 if (singleshift
== Singleshift
.SS2
) {
1828 singleshift
= Singleshift
.NONE
;
1830 // Shifted out character, pull from G2
1831 charsetGl
= currentState
.g2Charset
;
1832 charsetGr
= currentState
.grCharset
;
1836 if (singleshift
== Singleshift
.SS3
) {
1838 singleshift
= Singleshift
.NONE
;
1840 // Shifted out character, pull from G3
1841 charsetGl
= currentState
.g3Charset
;
1842 charsetGr
= currentState
.grCharset
;
1845 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
1846 // Check for locking shift
1848 switch (currentState
.glLockshift
) {
1861 charsetGl
= currentState
.g2Charset
;
1866 charsetGl
= currentState
.g3Charset
;
1871 charsetGl
= currentState
.g0Charset
;
1875 switch (currentState
.grLockshift
) {
1885 charsetGr
= currentState
.g1Charset
;
1890 charsetGr
= currentState
.g2Charset
;
1895 charsetGr
= currentState
.g3Charset
;
1900 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
1907 // Pull the character
1908 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1912 * Scroll the text within a scrolling region up n lines.
1914 * @param regionTop top row of the scrolling region
1915 * @param regionBottom bottom row of the scrolling region
1916 * @param n number of lines to scroll
1918 private void scrollingRegionScrollUp(final int regionTop
,
1919 final int regionBottom
, final int n
) {
1921 if (regionTop
>= regionBottom
) {
1925 // Sanity check: see if there will be any characters left after the
1927 if (regionBottom
+ 1 - regionTop
<= n
) {
1928 // There won't be anything left in the region, so just call
1929 // eraseScreen() and return.
1930 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1934 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1935 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1936 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1938 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
1939 - remaining
, regionBottom
+ 1);
1940 display
= new LinkedList
<DisplayLine
>(displayTop
);
1941 display
.addAll(displayMiddle
);
1942 for (int i
= 0; i
< n
; i
++) {
1943 DisplayLine line
= new DisplayLine(currentState
.attr
);
1944 line
.setReverseColor(reverseVideo
);
1947 display
.addAll(displayBottom
);
1949 assert (display
.size() == height
);
1953 * Scroll the text within a scrolling region down n lines.
1955 * @param regionTop top row of the scrolling region
1956 * @param regionBottom bottom row of the scrolling region
1957 * @param n number of lines to scroll
1959 private void scrollingRegionScrollDown(final int regionTop
,
1960 final int regionBottom
, final int n
) {
1962 if (regionTop
>= regionBottom
) {
1966 // Sanity check: see if there will be any characters left after the
1968 if (regionBottom
+ 1 - regionTop
<= n
) {
1969 // There won't be anything left in the region, so just call
1970 // eraseScreen() and return.
1971 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1975 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1976 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1977 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1979 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
1980 regionTop
+ remaining
);
1981 display
= new LinkedList
<DisplayLine
>(displayTop
);
1982 for (int i
= 0; i
< n
; i
++) {
1983 DisplayLine line
= new DisplayLine(currentState
.attr
);
1984 line
.setReverseColor(reverseVideo
);
1987 display
.addAll(displayMiddle
);
1988 display
.addAll(displayBottom
);
1990 assert (display
.size() == height
);
1994 * Process a control character.
1996 * @param ch 8-bit character from the remote side
1998 private void handleControlChar(final char ch
) {
1999 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2010 // Transmit the answerback message.
2021 cursorLeft(1, false);
2026 advanceToNextTabStop();
2052 currentState
.glLockshift
= LockshiftMode
.NONE
;
2058 currentState
.glLockshift
= LockshiftMode
.NONE
;
2083 singleshift
= Singleshift
.SS2
;
2088 singleshift
= Singleshift
.SS3
;
2098 * Advance the cursor to the next tab stop.
2100 private void advanceToNextTabStop() {
2101 if (tabStops
.size() == 0) {
2102 // Go to the rightmost column
2103 cursorRight(rightMargin
- currentState
.cursorX
, false);
2106 for (Integer stop
: tabStops
) {
2107 if (stop
> currentState
.cursorX
) {
2108 cursorRight(stop
- currentState
.cursorX
, false);
2113 * We got here, meaning there isn't a tab stop beyond the current
2114 * cursor position. Place the cursor of the right-most edge of the
2117 cursorRight(rightMargin
- currentState
.cursorX
, false);
2121 * Save a character into the collect buffer.
2123 * @param ch character to save
2125 private void collect(final char ch
) {
2126 collectBuffer
.append(ch
);
2130 * Save a byte into the CSI parameters buffer.
2132 * @param ch byte to save
2134 private void param(final byte ch
) {
2135 if (csiParams
.size() == 0) {
2136 csiParams
.add(new Integer(0));
2138 Integer x
= csiParams
.get(csiParams
.size() - 1);
2139 if ((ch
>= '0') && (ch
<= '9')) {
2142 csiParams
.set(csiParams
.size() - 1, x
);
2146 csiParams
.add(new Integer(0));
2151 * Get a CSI parameter value, with a default.
2153 * @param position parameter index. 0 is the first parameter.
2154 * @param defaultValue value to use if csiParams[position] doesn't exist
2155 * @return parameter value
2157 private int getCsiParam(final int position
, final int defaultValue
) {
2158 if (csiParams
.size() < position
+ 1) {
2159 return defaultValue
;
2161 return csiParams
.get(position
).intValue();
2165 * Get a CSI parameter value, clamped to within min/max.
2167 * @param position parameter index. 0 is the first parameter.
2168 * @param defaultValue value to use if csiParams[position] doesn't exist
2169 * @param minValue minimum value inclusive
2170 * @param maxValue maximum value inclusive
2171 * @return parameter value
2173 private int getCsiParam(final int position
, final int defaultValue
,
2174 final int minValue
, final int maxValue
) {
2176 assert (minValue
<= maxValue
);
2177 int value
= getCsiParam(position
, defaultValue
);
2178 if (value
< minValue
) {
2181 if (value
> maxValue
) {
2188 * Set or unset a toggle.
2190 * @param value true for set ('h'), false for reset ('l')
2192 private void setToggle(final boolean value
) {
2193 boolean decPrivateModeFlag
= false;
2194 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2195 if (collectBuffer
.charAt(i
) == '?') {
2196 decPrivateModeFlag
= true;
2201 for (Integer i
: csiParams
) {
2206 if (decPrivateModeFlag
== true) {
2208 if (value
== true) {
2209 // Use application arrow keys
2210 arrowKeyMode
= ArrowKeyMode
.VT100
;
2212 // Use ANSI arrow keys
2213 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2218 if (decPrivateModeFlag
== true) {
2219 if (value
== false) {
2223 arrowKeyMode
= ArrowKeyMode
.VT52
;
2226 * From the VT102 docs: "You use ANSI mode to select
2227 * most terminal features; the terminal uses the same
2228 * features when it switches to VT52 mode. You
2229 * cannot, however, change most of these features in
2232 * In other words, do not reset any other attributes
2233 * when switching between VT52 submode and ANSI.
2235 * HOWEVER, the real vt100 does switch the character
2236 * set according to Usenet.
2238 currentState
.g0Charset
= CharacterSet
.US
;
2239 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2242 if ((type
== DeviceType
.VT220
)
2243 || (type
== DeviceType
.XTERM
)) {
2245 // VT52 mode is explicitly 7-bit
2247 singleshift
= Singleshift
.NONE
;
2252 if (value
== true) {
2253 // Turn off keyboard
2262 if (decPrivateModeFlag
== true) {
2264 if (value
== true) {
2273 width
= rightMargin
+ 1;
2274 // Entire screen is cleared, and scrolling region is
2276 eraseScreen(0, 0, height
- 1, width
- 1, false);
2277 scrollRegionTop
= 0;
2278 scrollRegionBottom
= height
- 1;
2279 // Also home the cursor
2280 cursorPosition(0, 0);
2284 if (decPrivateModeFlag
== true) {
2286 if (value
== true) {
2295 if (value
== true) {
2303 if (decPrivateModeFlag
== true) {
2305 if (value
== true) {
2307 * Set selects reverse screen, a white screen
2308 * background with black characters.
2310 if (reverseVideo
!= true) {
2312 * If in normal video, switch it back
2314 invertDisplayColors();
2316 reverseVideo
= true;
2319 * Reset selects normal screen, a black screen
2320 * background with white characters.
2322 if (reverseVideo
== true) {
2324 * If in reverse video already, switch it back
2326 invertDisplayColors();
2328 reverseVideo
= false;
2333 if (decPrivateModeFlag
== true) {
2335 if (value
== true) {
2336 // Origin is relative to scroll region cursor.
2337 // Cursor can NEVER leave scrolling region.
2338 currentState
.originMode
= true;
2339 cursorPosition(0, 0);
2341 // Origin is absolute to entire screen. Cursor can
2342 // leave the scrolling region via cup() and hvp().
2343 currentState
.originMode
= false;
2344 cursorPosition(0, 0);
2349 if (decPrivateModeFlag
== true) {
2351 if (value
== true) {
2353 currentState
.lineWrap
= true;
2355 // Turn linewrap off
2356 currentState
.lineWrap
= false;
2361 if (decPrivateModeFlag
== true) {
2363 if (value
== true) {
2364 // Keyboard auto-repeat on
2367 // Keyboard auto-repeat off
2373 if (decPrivateModeFlag
== false) {
2375 if (value
== true) {
2385 if (decPrivateModeFlag
== true) {
2391 if (decPrivateModeFlag
== true) {
2397 if (decPrivateModeFlag
== false) {
2399 if (value
== true) {
2401 * Set causes a received linefeed, form feed, or
2402 * vertical tab to move cursor to first column of
2403 * next line. RETURN transmits both a carriage return
2404 * and linefeed. This selection is also called new
2410 * Reset causes a received linefeed, form feed, or
2411 * vertical tab to move cursor to next line in
2412 * current column. RETURN transmits a carriage
2415 newLineMode
= false;
2421 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2422 if (decPrivateModeFlag
== true) {
2424 if (value
== true) {
2426 cursorVisible
= true;
2429 cursorVisible
= false;
2436 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2437 if (decPrivateModeFlag
== true) {
2439 if (value
== true) {
2440 // Select national mode NRC
2443 // Select multi-national mode
2452 if ((type
== DeviceType
.XTERM
)
2453 && (decPrivateModeFlag
== true)
2455 // Mouse: normal tracking mode
2456 if (value
== true) {
2457 mouseProtocol
= MouseProtocol
.NORMAL
;
2459 mouseProtocol
= MouseProtocol
.OFF
;
2465 if ((type
== DeviceType
.XTERM
)
2466 && (decPrivateModeFlag
== true)
2468 // Mouse: normal tracking mode
2469 if (value
== true) {
2470 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2472 mouseProtocol
= MouseProtocol
.OFF
;
2478 if ((type
== DeviceType
.XTERM
)
2479 && (decPrivateModeFlag
== true)
2481 // Mouse: Any-event tracking mode
2482 if (value
== true) {
2483 mouseProtocol
= MouseProtocol
.ANYEVENT
;
2485 mouseProtocol
= MouseProtocol
.OFF
;
2491 if ((type
== DeviceType
.XTERM
)
2492 && (decPrivateModeFlag
== true)
2494 // Mouse: UTF-8 coordinates
2495 if (value
== true) {
2496 mouseEncoding
= MouseEncoding
.UTF8
;
2498 mouseEncoding
= MouseEncoding
.X10
;
2504 if ((type
== DeviceType
.XTERM
)
2505 && (decPrivateModeFlag
== true)
2507 // Mouse: SGR coordinates
2508 if (value
== true) {
2509 mouseEncoding
= MouseEncoding
.SGR
;
2511 mouseEncoding
= MouseEncoding
.X10
;
2524 * DECSC - Save cursor.
2526 private void decsc() {
2527 savedState
.setTo(currentState
);
2531 * DECRC - Restore cursor.
2533 private void decrc() {
2534 currentState
.setTo(savedState
);
2540 private void ind() {
2541 // Move the cursor and scroll if necessary. If at the bottom line
2542 // already, a scroll up is supposed to be performed.
2543 if (currentState
.cursorY
== scrollRegionBottom
) {
2544 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2546 cursorDown(1, true);
2550 * RI - Reverse index.
2553 // Move the cursor and scroll if necessary. If at the top line
2554 // already, a scroll down is supposed to be performed.
2555 if (currentState
.cursorY
== scrollRegionTop
) {
2556 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2564 private void nel() {
2565 // Move the cursor and scroll if necessary. If at the bottom line
2566 // already, a scroll up is supposed to be performed.
2567 if (currentState
.cursorY
== scrollRegionBottom
) {
2568 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2570 cursorDown(1, true);
2572 // Reset to the beginning of the next line
2573 currentState
.cursorX
= 0;
2577 * DECKPAM - Keypad application mode.
2579 private void deckpam() {
2580 keypadMode
= KeypadMode
.Application
;
2584 * DECKPNM - Keypad numeric mode.
2586 private void deckpnm() {
2587 keypadMode
= KeypadMode
.Numeric
;
2593 * @param n number of spaces to move
2594 * @param honorScrollRegion if true, then do nothing if the cursor is
2595 * outside the scrolling region
2597 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2601 * Special case: if a user moves the cursor from the right margin, we
2602 * have to reset the VT100 right margin flag.
2605 wrapLineFlag
= false;
2608 for (int i
= 0; i
< n
; i
++) {
2609 if (honorScrollRegion
== true) {
2610 // Honor the scrolling region
2611 if ((currentState
.cursorY
< scrollRegionTop
)
2612 || (currentState
.cursorY
> scrollRegionBottom
)
2614 // Outside region, do nothing
2617 // Inside region, go up
2618 top
= scrollRegionTop
;
2620 // Non-scrolling case
2624 if (currentState
.cursorY
> top
) {
2625 currentState
.cursorY
--;
2631 * Move down n spaces.
2633 * @param n number of spaces to move
2634 * @param honorScrollRegion if true, then do nothing if the cursor is
2635 * outside the scrolling region
2637 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
2641 * Special case: if a user moves the cursor from the right margin, we
2642 * have to reset the VT100 right margin flag.
2645 wrapLineFlag
= false;
2648 for (int i
= 0; i
< n
; i
++) {
2650 if (honorScrollRegion
== true) {
2651 // Honor the scrolling region
2652 if (currentState
.cursorY
> scrollRegionBottom
) {
2653 // Outside region, do nothing
2656 // Inside region, go down
2657 bottom
= scrollRegionBottom
;
2659 // Non-scrolling case
2660 bottom
= height
- 1;
2663 if (currentState
.cursorY
< bottom
) {
2664 currentState
.cursorY
++;
2670 * Move left n spaces.
2672 * @param n number of spaces to move
2673 * @param honorScrollRegion if true, then do nothing if the cursor is
2674 * outside the scrolling region
2676 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
2678 * Special case: if a user moves the cursor from the right margin, we
2679 * have to reset the VT100 right margin flag.
2682 wrapLineFlag
= false;
2685 for (int i
= 0; i
< n
; i
++) {
2686 if (honorScrollRegion
== true) {
2687 // Honor the scrolling region
2688 if ((currentState
.cursorY
< scrollRegionTop
)
2689 || (currentState
.cursorY
> scrollRegionBottom
)
2691 // Outside region, do nothing
2696 if (currentState
.cursorX
> 0) {
2697 currentState
.cursorX
--;
2703 * Move right n spaces.
2705 * @param n number of spaces to move
2706 * @param honorScrollRegion if true, then do nothing if the cursor is
2707 * outside the scrolling region
2709 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
2710 int rightMargin
= this.rightMargin
;
2713 * Special case: if a user moves the cursor from the right margin, we
2714 * have to reset the VT100 right margin flag.
2717 wrapLineFlag
= false;
2720 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2721 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2724 for (int i
= 0; i
< n
; i
++) {
2725 if (honorScrollRegion
== true) {
2726 // Honor the scrolling region
2727 if ((currentState
.cursorY
< scrollRegionTop
)
2728 || (currentState
.cursorY
> scrollRegionBottom
)
2730 // Outside region, do nothing
2735 if (currentState
.cursorX
< rightMargin
) {
2736 currentState
.cursorX
++;
2742 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2744 * @param row row to move to
2745 * @param col column to move to
2747 private void cursorPosition(int row
, final int col
) {
2748 int rightMargin
= this.rightMargin
;
2753 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2754 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2757 // Set column number
2758 currentState
.cursorX
= col
;
2760 // Sanity check, bring column back to margin.
2761 if (currentState
.cursorX
> rightMargin
) {
2762 currentState
.cursorX
= rightMargin
;
2766 if (currentState
.originMode
== true) {
2767 row
+= scrollRegionTop
;
2769 if (currentState
.cursorY
< row
) {
2770 cursorDown(row
- currentState
.cursorY
, false);
2771 } else if (currentState
.cursorY
> row
) {
2772 cursorUp(currentState
.cursorY
- row
, false);
2775 wrapLineFlag
= false;
2779 * HTS - Horizontal tabulation set.
2781 private void hts() {
2782 for (Integer stop
: tabStops
) {
2783 if (stop
== currentState
.cursorX
) {
2784 // Already have a tab stop here
2789 // Append a tab stop to the end of the array and resort them
2790 tabStops
.add(currentState
.cursorX
);
2791 Collections
.sort(tabStops
);
2795 * DECSWL - Single-width line.
2797 private void decswl() {
2798 display
.get(currentState
.cursorY
).setDoubleWidth(false);
2799 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2803 * DECDWL - Double-width line.
2805 private void decdwl() {
2806 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2807 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2811 * DECHDL - Double-height + double-width line.
2813 * @param topHalf if true, this sets the row to be the top half row of a
2816 private void dechdl(final boolean topHalf
) {
2817 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2818 if (topHalf
== true) {
2819 display
.get(currentState
.cursorY
).setDoubleHeight(1);
2821 display
.get(currentState
.cursorY
).setDoubleHeight(2);
2826 * DECALN - Screen alignment display.
2828 private void decaln() {
2829 Cell newCell
= new Cell();
2830 newCell
.setChar('E');
2831 for (DisplayLine line
: display
) {
2832 for (int i
= 0; i
< line
.length(); i
++) {
2833 line
.replace(i
, newCell
);
2839 * DECSCL - Compatibility level.
2841 private void decscl() {
2842 int i
= getCsiParam(0, 0);
2843 int j
= getCsiParam(1, 0);
2847 currentState
.g0Charset
= CharacterSet
.US
;
2848 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2850 } else if (i
== 62) {
2852 if ((j
== 0) || (j
== 2)) {
2854 } else if (j
== 1) {
2861 * CUD - Cursor down.
2863 private void cud() {
2864 cursorDown(getCsiParam(0, 1, 1, height
), true);
2868 * CUF - Cursor forward.
2870 private void cuf() {
2871 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
2875 * CUB - Cursor backward.
2877 private void cub() {
2878 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
2884 private void cuu() {
2885 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2889 * CUP - Cursor position.
2891 private void cup() {
2892 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2893 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
2897 * CNL - Cursor down and to column 1.
2899 private void cnl() {
2900 cursorDown(getCsiParam(0, 1, 1, height
), true);
2902 cursorLeft(currentState
.cursorX
, true);
2906 * CPL - Cursor up and to column 1.
2908 private void cpl() {
2909 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2911 cursorLeft(currentState
.cursorX
, true);
2915 * CHA - Cursor to column # in current row.
2917 private void cha() {
2918 cursorPosition(currentState
.cursorY
,
2919 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
2923 * VPA - Cursor to row #, same column.
2925 private void vpa() {
2926 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2927 currentState
.cursorX
);
2931 * ED - Erase in display.
2934 boolean honorProtected
= false;
2935 boolean decPrivateModeFlag
= false;
2937 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2938 if (collectBuffer
.charAt(i
) == '?') {
2939 decPrivateModeFlag
= true;
2944 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2945 && (decPrivateModeFlag
== true)
2947 honorProtected
= true;
2950 int i
= getCsiParam(0, 0);
2953 // Erase from here to end of screen
2954 if (currentState
.cursorY
< height
- 1) {
2955 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
2958 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2959 } else if (i
== 1) {
2960 // Erase from beginning of screen to here
2961 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
2963 eraseLine(0, currentState
.cursorX
, honorProtected
);
2964 } else if (i
== 2) {
2965 // Erase entire screen
2966 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
2971 * EL - Erase in line.
2974 boolean honorProtected
= false;
2975 boolean decPrivateModeFlag
= false;
2977 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2978 if (collectBuffer
.charAt(i
) == '?') {
2979 decPrivateModeFlag
= true;
2984 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2985 && (decPrivateModeFlag
== true)
2987 honorProtected
= true;
2990 int i
= getCsiParam(0, 0);
2993 // Erase from here to end of line
2994 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2995 } else if (i
== 1) {
2996 // Erase from beginning of line to here
2997 eraseLine(0, currentState
.cursorX
, honorProtected
);
2998 } else if (i
== 2) {
2999 // Erase entire line
3000 eraseLine(0, width
- 1, honorProtected
);
3005 * ECH - Erase # of characters in current row.
3007 private void ech() {
3008 int i
= getCsiParam(0, 1, 1, width
);
3010 // Erase from here to i characters
3011 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3018 int i
= getCsiParam(0, 1);
3020 if ((currentState
.cursorY
>= scrollRegionTop
)
3021 && (currentState
.cursorY
<= scrollRegionBottom
)
3024 // I can get the same effect with a scroll-down
3025 scrollingRegionScrollDown(currentState
.cursorY
,
3026 scrollRegionBottom
, i
);
3031 * DCH - Delete char.
3033 private void dch() {
3034 int n
= getCsiParam(0, 1);
3035 DisplayLine line
= display
.get(currentState
.cursorY
);
3036 Cell blank
= new Cell();
3037 for (int i
= 0; i
< n
; i
++) {
3038 line
.delete(currentState
.cursorX
, blank
);
3043 * ICH - Insert blank char at cursor.
3045 private void ich() {
3046 int n
= getCsiParam(0, 1);
3047 DisplayLine line
= display
.get(currentState
.cursorY
);
3048 Cell blank
= new Cell();
3049 for (int i
= 0; i
< n
; i
++) {
3050 line
.insert(currentState
.cursorX
, blank
);
3058 int i
= getCsiParam(0, 1);
3060 if ((currentState
.cursorY
>= scrollRegionTop
)
3061 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3063 // I can get the same effect with a scroll-down
3064 scrollingRegionScrollUp(currentState
.cursorY
,
3065 scrollRegionBottom
, i
);
3070 * HVP - Horizontal and vertical position.
3072 private void hvp() {
3077 * REP - Repeat character.
3079 private void rep() {
3080 int n
= getCsiParam(0, 1);
3081 for (int i
= 0; i
< n
; i
++) {
3082 printCharacter(repCh
);
3090 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3091 getCsiParam(0, 1, 1, height
));
3098 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3099 getCsiParam(0, 1, 1, height
));
3103 * CBT - Go back X tab stops.
3105 private void cbt() {
3106 int tabsToMove
= getCsiParam(0, 1);
3109 for (int i
= 0; i
< tabsToMove
; i
++) {
3110 int j
= currentState
.cursorX
;
3111 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3112 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3120 j
= tabStops
.get(tabI
);
3122 cursorPosition(currentState
.cursorY
, j
);
3127 * CHT - Advance X tab stops.
3129 private void cht() {
3130 int n
= getCsiParam(0, 1);
3131 for (int i
= 0; i
< n
; i
++) {
3132 advanceToNextTabStop();
3137 * SGR - Select graphics rendition.
3139 private void sgr() {
3141 if (csiParams
.size() == 0) {
3142 currentState
.attr
.reset();
3146 for (Integer i
: csiParams
) {
3152 currentState
.attr
.reset();
3157 currentState
.attr
.setBold(true);
3162 currentState
.attr
.setUnderline(true);
3167 currentState
.attr
.setBlink(true);
3172 currentState
.attr
.setReverse(true);
3179 if (type
== DeviceType
.XTERM
) {
3193 if ((type
== DeviceType
.VT220
)
3194 || (type
== DeviceType
.XTERM
)) {
3200 currentState
.attr
.setBold(false);
3205 currentState
.attr
.setUnderline(false);
3210 currentState
.attr
.setBlink(false);
3215 currentState
.attr
.setReverse(false);
3223 // A true VT100/102/220 does not support color, however everyone
3224 // is used to their terminal emulator supporting color so we will
3225 // unconditionally support color for all DeviceType's.
3230 // Set black foreground
3231 currentState
.attr
.setForeColor(Color
.BLACK
);
3234 // Set red foreground
3235 currentState
.attr
.setForeColor(Color
.RED
);
3238 // Set green foreground
3239 currentState
.attr
.setForeColor(Color
.GREEN
);
3242 // Set yellow foreground
3243 currentState
.attr
.setForeColor(Color
.YELLOW
);
3246 // Set blue foreground
3247 currentState
.attr
.setForeColor(Color
.BLUE
);
3250 // Set magenta foreground
3251 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3254 // Set cyan foreground
3255 currentState
.attr
.setForeColor(Color
.CYAN
);
3258 // Set white foreground
3259 currentState
.attr
.setForeColor(Color
.WHITE
);
3262 // Underscore on, default foreground color
3263 currentState
.attr
.setUnderline(true);
3264 currentState
.attr
.setForeColor(Color
.WHITE
);
3267 // Underscore off, default foreground color
3268 currentState
.attr
.setUnderline(false);
3269 currentState
.attr
.setForeColor(Color
.WHITE
);
3272 // Set black background
3273 currentState
.attr
.setBackColor(Color
.BLACK
);
3276 // Set red background
3277 currentState
.attr
.setBackColor(Color
.RED
);
3280 // Set green background
3281 currentState
.attr
.setBackColor(Color
.GREEN
);
3284 // Set yellow background
3285 currentState
.attr
.setBackColor(Color
.YELLOW
);
3288 // Set blue background
3289 currentState
.attr
.setBackColor(Color
.BLUE
);
3292 // Set magenta background
3293 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3296 // Set cyan background
3297 currentState
.attr
.setBackColor(Color
.CYAN
);
3300 // Set white background
3301 currentState
.attr
.setBackColor(Color
.WHITE
);
3304 // Default background
3305 currentState
.attr
.setBackColor(Color
.BLACK
);
3315 * DA - Device attributes.
3318 int extendedFlag
= 0;
3320 if (collectBuffer
.length() > 0) {
3321 String args
= collectBuffer
.substring(1);
3322 if (collectBuffer
.charAt(0) == '>') {
3324 if (collectBuffer
.length() >= 2) {
3325 i
= Integer
.parseInt(args
.toString());
3327 } else if (collectBuffer
.charAt(0) == '=') {
3329 if (collectBuffer
.length() >= 2) {
3330 i
= Integer
.parseInt(args
.toString());
3333 // Unknown code, bail out
3338 if ((i
!= 0) && (i
!= 1)) {
3342 if ((extendedFlag
== 0) && (i
== 0)) {
3343 // Send string directly to remote side
3344 writeRemote(deviceTypeResponse());
3348 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3350 if ((extendedFlag
== 1) && (i
== 0)) {
3352 * Request "What type of terminal are you, what is your
3353 * firmware version, and what hardware options do you have
3356 * Respond: "I am a VT220 (identification code of 1), my
3357 * firmware version is _____ (Pv), and I have _____ Po
3358 * options installed."
3364 if (s8c1t
== true) {
3365 writeRemote("\u009b>1;10;0c");
3367 writeRemote("\033[>1;10;0c");
3373 if ((extendedFlag
== 2) && (i
== 0)) {
3376 * Request "What is your unit ID?"
3378 * Respond: "I was manufactured at site 00 and have a unique ID
3382 writeRemote("\033P!|00010203\033\\");
3387 * DECSTBM - Set top and bottom margins.
3389 private void decstbm() {
3390 boolean decPrivateModeFlag
= false;
3392 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3393 if (collectBuffer
.charAt(i
) == '?') {
3394 decPrivateModeFlag
= true;
3398 if (decPrivateModeFlag
) {
3399 // This could be restore DEC private mode values.
3403 int top
= getCsiParam(0, 1, 1, height
) - 1;
3404 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3409 scrollRegionTop
= top
;
3410 scrollRegionBottom
= bottom
;
3413 cursorPosition(0, 0);
3418 * DECREQTPARM - Request terminal parameters.
3420 private void decreqtparm() {
3421 int i
= getCsiParam(0, 0);
3423 if ((i
!= 0) && (i
!= 1)) {
3430 * Request terminal parameters.
3434 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3435 * (CLoCk MULtiplier = 1, STP option flags = 0)
3439 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3442 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3444 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3450 * DECSCA - Select Character Attributes.
3452 private void decsca() {
3453 int i
= getCsiParam(0, 0);
3455 if ((i
== 0) || (i
== 2)) {
3457 currentState
.attr
.setProtect(false);
3461 currentState
.attr
.setProtect(true);
3466 * DECSTR - Soft Terminal Reset.
3468 private void decstr() {
3469 // Do exactly like RIS - Reset to initial state
3471 // Do I clear screen too? I think so...
3472 eraseScreen(0, 0, height
- 1, width
- 1, false);
3473 cursorPosition(0, 0);
3477 * DSR - Device status report.
3479 private void dsr() {
3480 boolean decPrivateModeFlag
= false;
3482 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3483 if (collectBuffer
.charAt(i
) == '?') {
3484 decPrivateModeFlag
= true;
3489 int i
= getCsiParam(0, 0);
3494 // Request status report. Respond with "OK, no malfunction."
3496 // Send string directly to remote side
3497 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3500 writeRemote("\u009b0n");
3502 writeRemote("\033[0n");
3507 // Request cursor position. Respond with current position.
3509 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3512 str
= String
.format("\u009b%d;%dR",
3513 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3515 str
= String
.format("\033[%d;%dR",
3516 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3519 // Send string directly to remote side
3524 if (decPrivateModeFlag
== true) {
3526 // Request printer status report. Respond with "Printer not
3529 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3530 && (s8c1t
== true)) {
3531 writeRemote("\u009b?13n");
3533 writeRemote("\033[?13n");
3539 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3540 && (decPrivateModeFlag
== true)
3543 // Request user-defined keys are locked or unlocked. Respond
3544 // with "User-defined keys are locked."
3546 if (s8c1t
== true) {
3547 writeRemote("\u009b?21n");
3549 writeRemote("\033[?21n");
3555 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3556 && (decPrivateModeFlag
== true)
3559 // Request keyboard language. Respond with "Keyboard
3560 // language is North American."
3562 if (s8c1t
== true) {
3563 writeRemote("\u009b?27;1n");
3565 writeRemote("\033[?27;1n");
3572 // Some other option, ignore
3578 * TBC - Tabulation clear.
3580 private void tbc() {
3581 int i
= getCsiParam(0, 0);
3583 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3584 for (Integer stop
: tabStops
) {
3585 if (stop
== currentState
.cursorX
) {
3590 tabStops
= newStops
;
3598 * Erase the characters in the current line from the start column to the
3599 * end column, inclusive.
3601 * @param start starting column to erase (between 0 and width - 1)
3602 * @param end ending column to erase (between 0 and width - 1)
3603 * @param honorProtected if true, do not erase characters with the
3604 * protected attribute set
3606 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3611 if (end
> width
- 1) {
3618 for (int i
= start
; i
<= end
; i
++) {
3619 DisplayLine line
= display
.get(currentState
.cursorY
);
3620 if ((!honorProtected
)
3621 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
3628 * From the VT102 manual:
3630 * Erasing a character also erases any character
3631 * attribute of the character.
3637 * Erase with the current color a.k.a. back-color erase
3640 line
.setChar(i
, ' ');
3641 line
.setAttr(i
, currentState
.attr
);
3649 * Erase a rectangular section of the screen, inclusive. end column,
3652 * @param startRow starting row to erase (between 0 and height - 1)
3653 * @param startCol starting column to erase (between 0 and width - 1)
3654 * @param endRow ending row to erase (between 0 and height - 1)
3655 * @param endCol ending column to erase (between 0 and width - 1)
3656 * @param honorProtected if true, do not erase characters with the
3657 * protected attribute set
3659 private void eraseScreen(final int startRow
, final int startCol
,
3660 final int endRow
, final int endCol
, final boolean honorProtected
) {
3668 || (endRow
< startRow
)
3669 || (endCol
< startCol
)
3674 oldCursorY
= currentState
.cursorY
;
3675 for (int i
= startRow
; i
<= endRow
; i
++) {
3676 currentState
.cursorY
= i
;
3677 eraseLine(startCol
, endCol
, honorProtected
);
3679 // Erase display clears the double attributes
3680 display
.get(i
).setDoubleWidth(false);
3681 display
.get(i
).setDoubleHeight(0);
3683 currentState
.cursorY
= oldCursorY
;
3687 * VT220 printer functions. All of these are parsed, but won't do
3690 private void printerFunctions() {
3691 boolean decPrivateModeFlag
= false;
3692 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3693 if (collectBuffer
.charAt(i
) == '?') {
3694 decPrivateModeFlag
= true;
3699 int i
= getCsiParam(0, 0);
3704 if (decPrivateModeFlag
== false) {
3710 if (decPrivateModeFlag
== true) {
3711 // Print cursor line
3716 if (decPrivateModeFlag
== true) {
3717 // Auto print mode OFF
3719 // Printer controller OFF
3721 // Characters re-appear on the screen
3722 printerControllerMode
= false;
3727 if (decPrivateModeFlag
== true) {
3731 // Printer controller
3733 // Characters get sucked into oblivion
3734 printerControllerMode
= true;
3745 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
3746 * of remote systems will send an XTerm title sequence even if TERM isn't
3749 * @param xtermChar the character received from the remote side
3751 private void oscPut(final char xtermChar
) {
3753 collectBuffer
.append(xtermChar
);
3756 if (xtermChar
== 0x07) {
3757 String args
= collectBuffer
.substring(0,
3758 collectBuffer
.length() - 1);
3759 String
[] p
= args
.toString().split(";");
3761 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
3769 // Go to SCAN_GROUND state
3776 * Run this input character through the ECMA48 state machine.
3778 * @param ch character from the remote side
3780 private void consume(char ch
) {
3783 // System.err.printf("%c", ch);
3785 // Special case for VT10x: 7-bit characters only
3786 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
3787 ch
= (char)(ch
& 0x7F);
3790 // Special "anywhere" states
3792 // 18, 1A --> execute, then switch to SCAN_GROUND
3793 if ((ch
== 0x18) || (ch
== 0x1A)) {
3794 // CAN and SUB abort escape sequences
3799 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
3803 && (scanState
!= ScanState
.DCS_ENTRY
)
3804 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
3805 && (scanState
!= ScanState
.DCS_IGNORE
)
3806 && (scanState
!= ScanState
.DCS_PARAM
)
3807 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
3810 scanState
= ScanState
.ESCAPE
;
3814 // 0x9B == CSI 8-bit sequence
3816 scanState
= ScanState
.CSI_ENTRY
;
3820 // 0x9D goes to ScanState.OSC_STRING
3822 scanState
= ScanState
.OSC_STRING
;
3826 // 0x90 goes to DCS_ENTRY
3828 scanState
= ScanState
.DCS_ENTRY
;
3832 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
3833 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
3834 scanState
= ScanState
.SOSPMAPC_STRING
;
3838 // 0x7F (DEL) is always discarded
3843 switch (scanState
) {
3846 // 00-17, 19, 1C-1F --> execute
3847 // 80-8F, 91-9A, 9C --> execute
3848 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
3849 handleControlChar(ch
);
3853 if (((ch
>= 0x20) && (ch
<= 0x7F))
3857 // VT220 printer --> trash bin
3858 if (((type
== DeviceType
.VT220
)
3859 || (type
== DeviceType
.XTERM
))
3860 && (printerControllerMode
== true)
3865 // Hang onto this character
3866 repCh
= mapCharacter(ch
);
3868 // Print this character
3869 printCharacter(repCh
);
3874 // 00-17, 19, 1C-1F --> execute
3876 handleControlChar(ch
);
3880 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
3881 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
3883 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
3887 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
3888 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
3899 // DECSC - Save cursor
3900 // Note this code overlaps both ANSI and VT52 mode
3905 // DECRC - Restore cursor
3906 // Note this code overlaps both ANSI and VT52 mode
3915 if (vt52Mode
== true) {
3916 // DECANM - Enter ANSI mode
3918 arrowKeyMode
= ArrowKeyMode
.VT100
;
3921 * From the VT102 docs: "You use ANSI mode to select
3922 * most terminal features; the terminal uses the same
3923 * features when it switches to VT52 mode. You
3924 * cannot, however, change most of these features in
3927 * In other words, do not reset any other attributes
3928 * when switching between VT52 submode and ANSI.
3932 currentState
.g0Charset
= CharacterSet
.US
;
3933 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3935 singleshift
= Singleshift
.NONE
;
3936 currentState
.glLockshift
= LockshiftMode
.NONE
;
3937 currentState
.grLockshift
= LockshiftMode
.NONE
;
3941 // DECKPAM - Keypad application mode
3942 // Note this code overlaps both ANSI and VT52 mode
3946 // DECKPNM - Keypad numeric mode
3947 // Note this code overlaps both ANSI and VT52 mode
3954 if (vt52Mode
== true) {
3955 // Cursor up, and stop at the top without scrolling
3960 if (vt52Mode
== true) {
3961 // Cursor down, and stop at the bottom without scrolling
3962 cursorDown(1, false);
3966 if (vt52Mode
== true) {
3967 // Cursor right, and stop at the right without scrolling
3968 cursorRight(1, false);
3972 if (vt52Mode
== true) {
3973 // Cursor left, and stop at the left without scrolling
3974 cursorLeft(1, false);
3981 if (vt52Mode
== true) {
3989 if (vt52Mode
== true) {
3990 // G0 --> Special graphics
3991 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
3995 if (vt52Mode
== true) {
3997 currentState
.g0Charset
= CharacterSet
.US
;
4001 if (vt52Mode
== true) {
4003 cursorPosition(0, 0);
4005 // HTS - Horizontal tabulation set
4010 if (vt52Mode
== true) {
4011 // Reverse line feed. Same as RI.
4016 if (vt52Mode
== true) {
4017 // Erase to end of screen
4018 eraseLine(currentState
.cursorX
, width
- 1, false);
4019 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4024 if (vt52Mode
== true) {
4025 // Erase to end of line
4026 eraseLine(currentState
.cursorX
, width
- 1, false);
4032 if (vt52Mode
== true) {
4035 // RI - Reverse index
4040 if (vt52Mode
== false) {
4042 singleshift
= Singleshift
.SS2
;
4046 if (vt52Mode
== false) {
4048 singleshift
= Singleshift
.SS3
;
4055 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4071 if (vt52Mode
== true) {
4072 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4080 if (vt52Mode
== true) {
4082 // Send string directly to remote side
4083 writeRemote("\033/Z");
4086 // Send string directly to remote side
4087 writeRemote(deviceTypeResponse());
4098 // VT52 cannot get to any of these other states
4099 if (vt52Mode
== true) {
4104 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4111 // RIS - Reset to initial state
4113 // Do I clear screen too? I think so...
4114 eraseScreen(0, 0, height
- 1, width
- 1, false);
4115 cursorPosition(0, 0);
4129 if ((type
== DeviceType
.VT220
)
4130 || (type
== DeviceType
.XTERM
)) {
4132 // VT220 lockshift G2 into GL
4133 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4138 if ((type
== DeviceType
.VT220
)
4139 || (type
== DeviceType
.XTERM
)) {
4141 // VT220 lockshift G3 into GL
4142 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4160 if ((type
== DeviceType
.VT220
)
4161 || (type
== DeviceType
.XTERM
)) {
4163 // VT220 lockshift G3 into GR
4164 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4169 if ((type
== DeviceType
.VT220
)
4170 || (type
== DeviceType
.XTERM
)) {
4172 // VT220 lockshift G2 into GR
4173 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4179 if ((type
== DeviceType
.VT220
)
4180 || (type
== DeviceType
.XTERM
)) {
4182 // VT220 lockshift G1 into GR
4183 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4193 // 0x5B goes to CSI_ENTRY
4195 scanState
= ScanState
.CSI_ENTRY
;
4198 // 0x5D goes to OSC_STRING
4200 scanState
= ScanState
.OSC_STRING
;
4203 // 0x50 goes to DCS_ENTRY
4205 scanState
= ScanState
.DCS_ENTRY
;
4208 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4209 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4210 scanState
= ScanState
.SOSPMAPC_STRING
;
4215 case ESCAPE_INTERMEDIATE
:
4216 // 00-17, 19, 1C-1F --> execute
4218 handleControlChar(ch
);
4221 // 20-2F --> collect
4222 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4226 // 30-7E --> dispatch, then switch to GROUND
4227 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4230 if ((collectBuffer
.length() == 1)
4231 && (collectBuffer
.charAt(0) == '(')) {
4232 // G0 --> Special graphics
4233 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4235 if ((collectBuffer
.length() == 1)
4236 && (collectBuffer
.charAt(0) == ')')) {
4237 // G1 --> Special graphics
4238 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4240 if ((type
== DeviceType
.VT220
)
4241 || (type
== DeviceType
.XTERM
)) {
4243 if ((collectBuffer
.length() == 1)
4244 && (collectBuffer
.charAt(0) == '*')) {
4245 // G2 --> Special graphics
4246 currentState
.g2Charset
= CharacterSet
.DRAWING
;
4248 if ((collectBuffer
.length() == 1)
4249 && (collectBuffer
.charAt(0) == '+')) {
4250 // G3 --> Special graphics
4251 currentState
.g3Charset
= CharacterSet
.DRAWING
;
4256 if ((collectBuffer
.length() == 1)
4257 && (collectBuffer
.charAt(0) == '(')) {
4258 // G0 --> Alternate character ROM standard character set
4259 currentState
.g0Charset
= CharacterSet
.ROM
;
4261 if ((collectBuffer
.length() == 1)
4262 && (collectBuffer
.charAt(0) == ')')) {
4263 // G1 --> Alternate character ROM standard character set
4264 currentState
.g1Charset
= CharacterSet
.ROM
;
4268 if ((collectBuffer
.length() == 1)
4269 && (collectBuffer
.charAt(0) == '(')) {
4270 // G0 --> Alternate character ROM special graphics
4271 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4273 if ((collectBuffer
.length() == 1)
4274 && (collectBuffer
.charAt(0) == ')')) {
4275 // G1 --> Alternate character ROM special graphics
4276 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4280 if ((collectBuffer
.length() == 1)
4281 && (collectBuffer
.charAt(0) == '#')) {
4282 // DECDHL - Double-height line (top half)
4287 if ((collectBuffer
.length() == 1)
4288 && (collectBuffer
.charAt(0) == '#')) {
4289 // DECDHL - Double-height line (bottom half)
4292 if ((type
== DeviceType
.VT220
)
4293 || (type
== DeviceType
.XTERM
)) {
4295 if ((collectBuffer
.length() == 1)
4296 && (collectBuffer
.charAt(0) == '(')) {
4298 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4300 if ((collectBuffer
.length() == 1)
4301 && (collectBuffer
.charAt(0) == ')')) {
4303 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4305 if ((collectBuffer
.length() == 1)
4306 && (collectBuffer
.charAt(0) == '*')) {
4308 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4310 if ((collectBuffer
.length() == 1)
4311 && (collectBuffer
.charAt(0) == '+')) {
4313 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4318 if ((collectBuffer
.length() == 1)
4319 && (collectBuffer
.charAt(0) == '#')) {
4320 // DECSWL - Single-width line
4323 if ((type
== DeviceType
.VT220
)
4324 || (type
== DeviceType
.XTERM
)) {
4326 if ((collectBuffer
.length() == 1)
4327 && (collectBuffer
.charAt(0) == '(')) {
4329 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4331 if ((collectBuffer
.length() == 1)
4332 && (collectBuffer
.charAt(0) == ')')) {
4334 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4336 if ((collectBuffer
.length() == 1)
4337 && (collectBuffer
.charAt(0) == '*')) {
4339 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4341 if ((collectBuffer
.length() == 1)
4342 && (collectBuffer
.charAt(0) == '+')) {
4344 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4349 if ((collectBuffer
.length() == 1)
4350 && (collectBuffer
.charAt(0) == '#')) {
4351 // DECDWL - Double-width line
4354 if ((type
== DeviceType
.VT220
)
4355 || (type
== DeviceType
.XTERM
)) {
4357 if ((collectBuffer
.length() == 1)
4358 && (collectBuffer
.charAt(0) == '(')) {
4360 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4362 if ((collectBuffer
.length() == 1)
4363 && (collectBuffer
.charAt(0) == ')')) {
4365 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4367 if ((collectBuffer
.length() == 1)
4368 && (collectBuffer
.charAt(0) == '*')) {
4370 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4372 if ((collectBuffer
.length() == 1)
4373 && (collectBuffer
.charAt(0) == '+')) {
4375 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4380 if ((type
== DeviceType
.VT220
)
4381 || (type
== DeviceType
.XTERM
)) {
4383 if ((collectBuffer
.length() == 1)
4384 && (collectBuffer
.charAt(0) == '(')) {
4386 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4388 if ((collectBuffer
.length() == 1)
4389 && (collectBuffer
.charAt(0) == ')')) {
4391 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4393 if ((collectBuffer
.length() == 1)
4394 && (collectBuffer
.charAt(0) == '*')) {
4396 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4398 if ((collectBuffer
.length() == 1)
4399 && (collectBuffer
.charAt(0) == '+')) {
4401 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4406 if ((collectBuffer
.length() == 1)
4407 && (collectBuffer
.charAt(0) == '#')) {
4408 // DECALN - Screen alignment display
4417 if ((type
== DeviceType
.VT220
)
4418 || (type
== DeviceType
.XTERM
)) {
4420 if ((collectBuffer
.length() == 1)
4421 && (collectBuffer
.charAt(0) == '(')) {
4422 // G0 --> DEC_SUPPLEMENTAL
4423 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4425 if ((collectBuffer
.length() == 1)
4426 && (collectBuffer
.charAt(0) == ')')) {
4427 // G1 --> DEC_SUPPLEMENTAL
4428 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4430 if ((collectBuffer
.length() == 1)
4431 && (collectBuffer
.charAt(0) == '*')) {
4432 // G2 --> DEC_SUPPLEMENTAL
4433 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4435 if ((collectBuffer
.length() == 1)
4436 && (collectBuffer
.charAt(0) == '+')) {
4437 // G3 --> DEC_SUPPLEMENTAL
4438 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4443 if ((type
== DeviceType
.VT220
)
4444 || (type
== DeviceType
.XTERM
)) {
4446 if ((collectBuffer
.length() == 1)
4447 && (collectBuffer
.charAt(0) == '(')) {
4449 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4451 if ((collectBuffer
.length() == 1)
4452 && (collectBuffer
.charAt(0) == ')')) {
4454 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4456 if ((collectBuffer
.length() == 1)
4457 && (collectBuffer
.charAt(0) == '*')) {
4459 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4461 if ((collectBuffer
.length() == 1)
4462 && (collectBuffer
.charAt(0) == '+')) {
4464 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4473 if ((collectBuffer
.length() == 1)
4474 && (collectBuffer
.charAt(0) == '(')) {
4475 // G0 --> United Kingdom set
4476 currentState
.g0Charset
= CharacterSet
.UK
;
4478 if ((collectBuffer
.length() == 1)
4479 && (collectBuffer
.charAt(0) == ')')) {
4480 // G1 --> United Kingdom set
4481 currentState
.g1Charset
= CharacterSet
.UK
;
4483 if ((type
== DeviceType
.VT220
)
4484 || (type
== DeviceType
.XTERM
)) {
4486 if ((collectBuffer
.length() == 1)
4487 && (collectBuffer
.charAt(0) == '*')) {
4488 // G2 --> United Kingdom set
4489 currentState
.g2Charset
= CharacterSet
.UK
;
4491 if ((collectBuffer
.length() == 1)
4492 && (collectBuffer
.charAt(0) == '+')) {
4493 // G3 --> United Kingdom set
4494 currentState
.g3Charset
= CharacterSet
.UK
;
4499 if ((collectBuffer
.length() == 1)
4500 && (collectBuffer
.charAt(0) == '(')) {
4502 currentState
.g0Charset
= CharacterSet
.US
;
4504 if ((collectBuffer
.length() == 1)
4505 && (collectBuffer
.charAt(0) == ')')) {
4507 currentState
.g1Charset
= CharacterSet
.US
;
4509 if ((type
== DeviceType
.VT220
)
4510 || (type
== DeviceType
.XTERM
)) {
4512 if ((collectBuffer
.length() == 1)
4513 && (collectBuffer
.charAt(0) == '*')) {
4515 currentState
.g2Charset
= CharacterSet
.US
;
4517 if ((collectBuffer
.length() == 1)
4518 && (collectBuffer
.charAt(0) == '+')) {
4520 currentState
.g3Charset
= CharacterSet
.US
;
4525 if ((type
== DeviceType
.VT220
)
4526 || (type
== DeviceType
.XTERM
)) {
4528 if ((collectBuffer
.length() == 1)
4529 && (collectBuffer
.charAt(0) == '(')) {
4531 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4533 if ((collectBuffer
.length() == 1)
4534 && (collectBuffer
.charAt(0) == ')')) {
4536 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4538 if ((collectBuffer
.length() == 1)
4539 && (collectBuffer
.charAt(0) == '*')) {
4541 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4543 if ((collectBuffer
.length() == 1)
4544 && (collectBuffer
.charAt(0) == '+')) {
4546 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4553 if ((type
== DeviceType
.VT220
)
4554 || (type
== DeviceType
.XTERM
)) {
4556 if ((collectBuffer
.length() == 1)
4557 && (collectBuffer
.charAt(0) == '(')) {
4559 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4561 if ((collectBuffer
.length() == 1)
4562 && (collectBuffer
.charAt(0) == ')')) {
4564 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4566 if ((collectBuffer
.length() == 1)
4567 && (collectBuffer
.charAt(0) == '*')) {
4569 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4571 if ((collectBuffer
.length() == 1)
4572 && (collectBuffer
.charAt(0) == '+')) {
4574 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4579 if ((type
== DeviceType
.VT220
)
4580 || (type
== DeviceType
.XTERM
)) {
4582 if ((collectBuffer
.length() == 1)
4583 && (collectBuffer
.charAt(0) == ' ')) {
4590 if ((type
== DeviceType
.VT220
)
4591 || (type
== DeviceType
.XTERM
)) {
4593 if ((collectBuffer
.length() == 1)
4594 && (collectBuffer
.charAt(0) == ' ')) {
4601 if ((type
== DeviceType
.VT220
)
4602 || (type
== DeviceType
.XTERM
)) {
4604 if ((collectBuffer
.length() == 1)
4605 && (collectBuffer
.charAt(0) == '(')) {
4607 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4609 if ((collectBuffer
.length() == 1)
4610 && (collectBuffer
.charAt(0) == ')')) {
4612 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4614 if ((collectBuffer
.length() == 1)
4615 && (collectBuffer
.charAt(0) == '*')) {
4617 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4619 if ((collectBuffer
.length() == 1)
4620 && (collectBuffer
.charAt(0) == '+')) {
4622 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4630 if ((type
== DeviceType
.VT220
)
4631 || (type
== DeviceType
.XTERM
)) {
4633 if ((collectBuffer
.length() == 1)
4634 && (collectBuffer
.charAt(0) == '(')) {
4636 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4638 if ((collectBuffer
.length() == 1)
4639 && (collectBuffer
.charAt(0) == ')')) {
4641 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4643 if ((collectBuffer
.length() == 1)
4644 && (collectBuffer
.charAt(0) == '*')) {
4646 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4648 if ((collectBuffer
.length() == 1)
4649 && (collectBuffer
.charAt(0) == '+')) {
4651 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4662 if ((type
== DeviceType
.VT220
)
4663 || (type
== DeviceType
.XTERM
)) {
4665 if ((collectBuffer
.length() == 1)
4666 && (collectBuffer
.charAt(0) == '(')) {
4668 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4670 if ((collectBuffer
.length() == 1)
4671 && (collectBuffer
.charAt(0) == ')')) {
4673 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4675 if ((collectBuffer
.length() == 1)
4676 && (collectBuffer
.charAt(0) == '*')) {
4678 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4680 if ((collectBuffer
.length() == 1)
4681 && (collectBuffer
.charAt(0) == '+')) {
4683 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4688 if ((type
== DeviceType
.VT220
)
4689 || (type
== DeviceType
.XTERM
)) {
4691 if ((collectBuffer
.length() == 1)
4692 && (collectBuffer
.charAt(0) == '(')) {
4694 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4696 if ((collectBuffer
.length() == 1)
4697 && (collectBuffer
.charAt(0) == ')')) {
4699 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4701 if ((collectBuffer
.length() == 1)
4702 && (collectBuffer
.charAt(0) == '*')) {
4704 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
4706 if ((collectBuffer
.length() == 1)
4707 && (collectBuffer
.charAt(0) == '+')) {
4709 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
4721 if ((type
== DeviceType
.VT220
)
4722 || (type
== DeviceType
.XTERM
)) {
4724 if ((collectBuffer
.length() == 1)
4725 && (collectBuffer
.charAt(0) == '(')) {
4727 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
4729 if ((collectBuffer
.length() == 1)
4730 && (collectBuffer
.charAt(0) == ')')) {
4732 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
4734 if ((collectBuffer
.length() == 1)
4735 && (collectBuffer
.charAt(0) == '*')) {
4737 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
4739 if ((collectBuffer
.length() == 1)
4740 && (collectBuffer
.charAt(0) == '+')) {
4742 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
4747 if ((type
== DeviceType
.VT220
)
4748 || (type
== DeviceType
.XTERM
)) {
4750 if ((collectBuffer
.length() == 1)
4751 && (collectBuffer
.charAt(0) == '(')) {
4753 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
4755 if ((collectBuffer
.length() == 1)
4756 && (collectBuffer
.charAt(0) == ')')) {
4758 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
4760 if ((collectBuffer
.length() == 1)
4761 && (collectBuffer
.charAt(0) == '*')) {
4763 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
4765 if ((collectBuffer
.length() == 1)
4766 && (collectBuffer
.charAt(0) == '+')) {
4768 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
4815 // 0x9C goes to GROUND
4823 // 00-17, 19, 1C-1F --> execute
4825 handleControlChar(ch
);
4828 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
4829 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4831 scanState
= ScanState
.CSI_INTERMEDIATE
;
4834 // 30-39, 3B --> param, then switch to CSI_PARAM
4835 if ((ch
>= '0') && (ch
<= '9')) {
4837 scanState
= ScanState
.CSI_PARAM
;
4841 scanState
= ScanState
.CSI_PARAM
;
4844 // 3C-3F --> collect, then switch to CSI_PARAM
4845 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
4847 scanState
= ScanState
.CSI_PARAM
;
4850 // 40-7E --> dispatch, then switch to GROUND
4851 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
4854 // ICH - Insert character
4862 // CUD - Cursor down
4866 // CUF - Cursor forward
4870 // CUB - Cursor backward
4874 // CNL - Cursor down and to column 1
4875 if (type
== DeviceType
.XTERM
) {
4880 // CPL - Cursor up and to column 1
4881 if (type
== DeviceType
.XTERM
) {
4886 // CHA - Cursor to column # in current row
4887 if (type
== DeviceType
.XTERM
) {
4892 // CUP - Cursor position
4896 // CHT - Cursor forward X tab stops (default 1)
4897 if (type
== DeviceType
.XTERM
) {
4902 // ED - Erase in display
4906 // EL - Erase in line
4921 // DCH - Delete character
4928 // Scroll up X lines (default 1)
4929 if (type
== DeviceType
.XTERM
) {
4934 // Scroll down X lines (default 1)
4935 if (type
== DeviceType
.XTERM
) {
4944 if ((type
== DeviceType
.VT220
)
4945 || (type
== DeviceType
.XTERM
)) {
4947 // ECH - Erase character
4954 // CBT - Cursor backward X tab stops (default 1)
4955 if (type
== DeviceType
.XTERM
) {
4966 // HPA - Cursor to column # in current row. Same as CHA
4967 if (type
== DeviceType
.XTERM
) {
4972 // HPR - Cursor right. Same as CUF
4973 if (type
== DeviceType
.XTERM
) {
4978 // REP - Repeat last char X times
4979 if (type
== DeviceType
.XTERM
) {
4984 // DA - Device attributes
4988 // VPA - Cursor to row, current column.
4989 if (type
== DeviceType
.XTERM
) {
4994 // VPR - Cursor down. Same as CUD
4995 if (type
== DeviceType
.XTERM
) {
5000 // HVP - Horizontal and vertical position
5004 // TBC - Tabulation clear
5008 // Sets an ANSI or DEC private toggle
5012 if ((type
== DeviceType
.VT220
)
5013 || (type
== DeviceType
.XTERM
)) {
5015 // Printer functions
5023 // Sets an ANSI or DEC private toggle
5027 // SGR - Select graphics rendition
5031 // DSR - Device status report
5038 // DECLL - Load leds
5042 // DECSTBM - Set top and bottom margins
5046 // Save cursor (ANSI.SYS)
5047 if (type
== DeviceType
.XTERM
) {
5048 savedState
.cursorX
= currentState
.cursorX
;
5049 savedState
.cursorY
= currentState
.cursorY
;
5055 // Restore cursor (ANSI.SYS)
5056 if (type
== DeviceType
.XTERM
) {
5057 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5064 // DECREQTPARM - Request terminal parameters
5080 // 0x9C goes to GROUND
5085 // 0x3A goes to CSI_IGNORE
5087 scanState
= ScanState
.CSI_IGNORE
;
5092 // 00-17, 19, 1C-1F --> execute
5094 handleControlChar(ch
);
5097 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5098 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5100 scanState
= ScanState
.CSI_INTERMEDIATE
;
5103 // 30-39, 3B --> param
5104 if ((ch
>= '0') && (ch
<= '9')) {
5111 // 0x3A goes to CSI_IGNORE
5113 scanState
= ScanState
.CSI_IGNORE
;
5115 // 0x3C-3F goes to CSI_IGNORE
5116 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5117 scanState
= ScanState
.CSI_IGNORE
;
5120 // 40-7E --> dispatch, then switch to GROUND
5121 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5124 // ICH - Insert character
5132 // CUD - Cursor down
5136 // CUF - Cursor forward
5140 // CUB - Cursor backward
5144 // CNL - Cursor down and to column 1
5145 if (type
== DeviceType
.XTERM
) {
5150 // CPL - Cursor up and to column 1
5151 if (type
== DeviceType
.XTERM
) {
5156 // CHA - Cursor to column # in current row
5157 if (type
== DeviceType
.XTERM
) {
5162 // CUP - Cursor position
5166 // CHT - Cursor forward X tab stops (default 1)
5167 if (type
== DeviceType
.XTERM
) {
5172 // ED - Erase in display
5176 // EL - Erase in line
5191 // DCH - Delete character
5198 // Scroll up X lines (default 1)
5199 if (type
== DeviceType
.XTERM
) {
5204 // Scroll down X lines (default 1)
5205 if (type
== DeviceType
.XTERM
) {
5214 if ((type
== DeviceType
.VT220
)
5215 || (type
== DeviceType
.XTERM
)) {
5217 // ECH - Erase character
5224 // CBT - Cursor backward X tab stops (default 1)
5225 if (type
== DeviceType
.XTERM
) {
5236 // HPA - Cursor to column # in current row. Same as CHA
5237 if (type
== DeviceType
.XTERM
) {
5242 // HPR - Cursor right. Same as CUF
5243 if (type
== DeviceType
.XTERM
) {
5248 // REP - Repeat last char X times
5249 if (type
== DeviceType
.XTERM
) {
5254 // DA - Device attributes
5258 // VPA - Cursor to row, current column.
5259 if (type
== DeviceType
.XTERM
) {
5264 // VPR - Cursor down. Same as CUD
5265 if (type
== DeviceType
.XTERM
) {
5270 // HVP - Horizontal and vertical position
5274 // TBC - Tabulation clear
5278 // Sets an ANSI or DEC private toggle
5282 if ((type
== DeviceType
.VT220
)
5283 || (type
== DeviceType
.XTERM
)) {
5285 // Printer functions
5293 // Sets an ANSI or DEC private toggle
5297 // SGR - Select graphics rendition
5301 // DSR - Device status report
5308 // DECLL - Load leds
5312 // DECSTBM - Set top and bottom margins
5322 // DECREQTPARM - Request terminal parameters
5339 case CSI_INTERMEDIATE
:
5340 // 00-17, 19, 1C-1F --> execute
5342 handleControlChar(ch
);
5345 // 20-2F --> collect
5346 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5350 // 0x30-3F goes to CSI_IGNORE
5351 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5352 scanState
= ScanState
.CSI_IGNORE
;
5355 // 40-7E --> dispatch, then switch to GROUND
5356 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5408 if (((type
== DeviceType
.VT220
)
5409 || (type
== DeviceType
.XTERM
))
5410 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5412 // DECSCL - compatibility level
5415 if ((type
== DeviceType
.XTERM
)
5416 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
5418 // DECSTR - Soft terminal reset
5423 if (((type
== DeviceType
.VT220
)
5424 || (type
== DeviceType
.XTERM
))
5425 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5453 // 00-17, 19, 1C-1F --> execute
5455 handleControlChar(ch
);
5458 // 20-2F --> collect
5459 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5463 // 40-7E --> ignore, then switch to GROUND
5464 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5468 // 20-3F, 7F --> ignore
5474 // 0x9C goes to GROUND
5479 // 0x1B 0x5C goes to GROUND
5484 if ((collectBuffer
.length() > 0)
5485 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5491 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5492 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5494 scanState
= ScanState
.DCS_INTERMEDIATE
;
5497 // 30-39, 3B --> param, then switch to DCS_PARAM
5498 if ((ch
>= '0') && (ch
<= '9')) {
5500 scanState
= ScanState
.DCS_PARAM
;
5504 scanState
= ScanState
.DCS_PARAM
;
5507 // 3C-3F --> collect, then switch to DCS_PARAM
5508 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5510 scanState
= ScanState
.DCS_PARAM
;
5513 // 00-17, 19, 1C-1F, 7F --> ignore
5515 // 0x3A goes to DCS_IGNORE
5517 scanState
= ScanState
.DCS_IGNORE
;
5520 // 0x40-7E goes to DCS_PASSTHROUGH
5521 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5522 scanState
= ScanState
.DCS_PASSTHROUGH
;
5526 case DCS_INTERMEDIATE
:
5528 // 0x9C goes to GROUND
5533 // 0x1B 0x5C goes to GROUND
5538 if ((collectBuffer
.length() > 0)
5539 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5545 // 0x30-3F goes to DCS_IGNORE
5546 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5547 scanState
= ScanState
.DCS_IGNORE
;
5550 // 0x40-7E goes to DCS_PASSTHROUGH
5551 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5552 scanState
= ScanState
.DCS_PASSTHROUGH
;
5555 // 00-17, 19, 1C-1F, 7F --> ignore
5560 // 0x9C goes to GROUND
5565 // 0x1B 0x5C goes to GROUND
5570 if ((collectBuffer
.length() > 0)
5571 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5577 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5578 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5580 scanState
= ScanState
.DCS_INTERMEDIATE
;
5583 // 30-39, 3B --> param
5584 if ((ch
>= '0') && (ch
<= '9')) {
5591 // 00-17, 19, 1C-1F, 7F --> ignore
5593 // 0x3A, 3C-3F goes to DCS_IGNORE
5595 scanState
= ScanState
.DCS_IGNORE
;
5597 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5598 scanState
= ScanState
.DCS_IGNORE
;
5601 // 0x40-7E goes to DCS_PASSTHROUGH
5602 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5603 scanState
= ScanState
.DCS_PASSTHROUGH
;
5607 case DCS_PASSTHROUGH
:
5608 // 0x9C goes to GROUND
5613 // 0x1B 0x5C goes to GROUND
5618 if ((collectBuffer
.length() > 0)
5619 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5625 // 00-17, 19, 1C-1F, 20-7E --> put
5633 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5636 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5645 // 00-17, 19, 1C-1F, 20-7F --> ignore
5647 // 0x9C goes to GROUND
5654 case SOSPMAPC_STRING
:
5655 // 00-17, 19, 1C-1F, 20-7F --> ignore
5657 // 0x9C goes to GROUND
5665 // Special case for Xterm: OSC can pass control characters
5666 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5670 // 00-17, 19, 1C-1F --> ignore
5672 // 20-7F --> osc_put
5673 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5677 // 0x9C goes to GROUND
5684 case VT52_DIRECT_CURSOR_ADDRESS
:
5685 // This is a special case for the VT52 sequence "ESC Y l c"
5686 if (collectBuffer
.length() == 0) {
5688 } else if (collectBuffer
.length() == 1) {
5689 // We've got the two characters, one in the buffer and the
5691 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
5700 * Expose current cursor X to outside world.
5702 * @return current cursor X
5704 public final int getCursorX() {
5705 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
5706 return currentState
.cursorX
* 2;
5708 return currentState
.cursorX
;
5712 * Expose current cursor Y to outside world.
5714 * @return current cursor Y
5716 public final int getCursorY() {
5717 return currentState
.cursorY
;
5721 * Read function runs on a separate thread.
5723 public final void run() {
5724 boolean utf8
= false;
5725 boolean done
= false;
5727 if (type
== DeviceType
.XTERM
) {
5731 // available() will often return > 1, so we need to read in chunks to
5733 char [] readBufferUTF8
= null;
5734 byte [] readBuffer
= null;
5736 readBufferUTF8
= new char[128];
5738 readBuffer
= new byte[128];
5741 while (!done
&& !stopReaderThread
) {
5743 int n
= inputStream
.available();
5744 // System.err.printf("available() %d\n", n); System.err.flush();
5746 if (readBufferUTF8
.length
< n
) {
5747 // The buffer wasn't big enough, make it huger
5748 int newSizeHalf
= Math
.max(readBufferUTF8
.length
, n
);
5750 readBufferUTF8
= new char[newSizeHalf
* 2];
5753 if (readBuffer
.length
< n
) {
5754 // The buffer wasn't big enough, make it huger
5755 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
5756 readBuffer
= new byte[newSizeHalf
* 2];
5762 rc
= input
.read(readBufferUTF8
, 0,
5763 readBufferUTF8
.length
);
5765 rc
= inputStream
.read(readBuffer
, 0,
5768 // System.err.printf("read() %d\n", rc); System.err.flush();
5773 for (int i
= 0; i
< rc
; i
++) {
5776 ch
= readBufferUTF8
[i
];
5780 // Don't step on UI events
5781 synchronized (this) {
5786 // System.err.println("end while loop"); System.err.flush();
5787 } catch (IOException e
) {
5788 e
.printStackTrace();
5791 } // while ((done == false) && (stopReaderThread == false))
5793 // Let the rest of the world know that I am done.
5794 stopReaderThread
= true;
5796 // System.err.println("*** run() exiting..."); System.err.flush();