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
{
529 * Which mouse encoding is active.
531 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
534 * Physical display width. We start at 80x24, but the user can resize us
540 * Get the display width.
542 * @return the width (usually 80 or 132)
544 public final int getWidth() {
549 * Physical display height. We start at 80x24, but the user can resize
555 * Get the display height.
557 * @return the height (usually 24)
559 public final int getHeight() {
564 * Top margin of the scrolling region.
566 private int scrollRegionTop
;
569 * Bottom margin of the scrolling region.
571 private int scrollRegionBottom
;
574 * Right margin column number. This can be selected by the remote side
575 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
577 private int rightMargin
;
580 * Last character printed.
585 * VT100-style line wrapping: a character is placed in column 80 (or
586 * 132), but the line does NOT wrap until another character is written to
587 * column 1 of the next line, after which the cursor moves to column 2.
589 private boolean wrapLineFlag
;
592 * VT220 single shift flag.
594 private Singleshift singleshift
= Singleshift
.NONE
;
597 * true = insert characters, false = overwrite.
599 private boolean insertMode
= false;
602 * VT52 mode as selected by DECANM. True means VT52, false means
603 * ANSI. Default is ANSI.
605 private boolean vt52Mode
= false;
608 * Visible cursor (DECTCEM).
610 private boolean cursorVisible
= true;
613 * Get visible cursor flag.
615 * @return if true, the cursor is visible
617 public final boolean visibleCursor() {
618 return cursorVisible
;
622 * Screen title as set by the xterm OSC sequence. Lots of applications
623 * send a screenTitle regardless of whether it is an xterm client or not.
625 private String screenTitle
= "";
628 * Get the screen title as set by the xterm OSC sequence. Lots of
629 * applications send a screenTitle regardless of whether it is an xterm
632 * @return screen title
634 public final String
getScreenTitle() {
639 * Parameter characters being collected.
641 private List
<Integer
> csiParams
;
644 * Non-csi collect buffer.
646 private StringBuilder collectBuffer
;
649 * When true, use the G1 character set.
651 private boolean shiftOut
= false;
654 * Horizontal tab stop locations.
656 private List
<Integer
> tabStops
;
659 * S8C1T. True means 8bit controls, false means 7bit controls.
661 private boolean s8c1t
= false;
664 * Printer mode. True means send all output to printer, which discards
667 private boolean printerControllerMode
= false;
670 * LMN line mode. If true, linefeed() puts the cursor on the first
671 * column of the next line. If false, linefeed() puts the cursor one
672 * line down on the current line. The default is false.
674 private boolean newLineMode
= false;
677 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
679 private ArrowKeyMode arrowKeyMode
;
682 * Whether number pad keys send VT100 or VT52, application or numeric
685 private KeypadMode keypadMode
;
688 * When true, the terminal is in 132-column mode (DECCOLM).
690 private boolean columns132
= false;
693 * Get 132 columns value.
695 * @return if true, the terminal is in 132 column mode
697 public final boolean isColumns132() {
702 * true = reverse video. Set by DECSCNM.
704 private boolean reverseVideo
= false;
707 * false = echo characters locally.
709 private boolean fullDuplex
= true;
712 * DECSC/DECRC save/restore a subset of the total state. This class
713 * encapsulates those specific flags/modes.
715 private class SaveableState
{
718 * When true, cursor positions are relative to the scrolling region.
720 public boolean originMode
= false;
723 * The current editing X position.
725 public int cursorX
= 0;
728 * The current editing Y position.
730 public int cursorY
= 0;
733 * Which character set is currently selected in G0.
735 public CharacterSet g0Charset
= CharacterSet
.US
;
738 * Which character set is currently selected in G1.
740 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
743 * Which character set is currently selected in G2.
745 public CharacterSet g2Charset
= CharacterSet
.US
;
748 * Which character set is currently selected in G3.
750 public CharacterSet g3Charset
= CharacterSet
.US
;
753 * Which character set is currently selected in GR.
755 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
758 * The current drawing attributes.
760 public CellAttributes attr
;
765 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
770 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
775 public boolean lineWrap
= true;
780 public void reset() {
784 g0Charset
= CharacterSet
.US
;
785 g1Charset
= CharacterSet
.DRAWING
;
786 g2Charset
= CharacterSet
.US
;
787 g3Charset
= CharacterSet
.US
;
788 grCharset
= CharacterSet
.DRAWING
;
789 attr
= new CellAttributes();
790 glLockshift
= LockshiftMode
.NONE
;
791 grLockshift
= LockshiftMode
.NONE
;
796 * Copy attributes from another instance.
798 * @param that the other instance to match
800 public void setTo(final SaveableState that
) {
801 this.originMode
= that
.originMode
;
802 this.cursorX
= that
.cursorX
;
803 this.cursorY
= that
.cursorY
;
804 this.g0Charset
= that
.g0Charset
;
805 this.g1Charset
= that
.g1Charset
;
806 this.g2Charset
= that
.g2Charset
;
807 this.g3Charset
= that
.g3Charset
;
808 this.grCharset
= that
.grCharset
;
809 this.attr
= new CellAttributes();
810 this.attr
.setTo(that
.attr
);
811 this.glLockshift
= that
.glLockshift
;
812 this.grLockshift
= that
.grLockshift
;
813 this.lineWrap
= that
.lineWrap
;
817 * Public constructor.
819 public SaveableState() {
825 * The current terminal state.
827 private SaveableState currentState
;
830 * The last saved terminal state.
832 private SaveableState savedState
;
835 * Clear the CSI parameters and flags.
837 private void toGround() {
839 collectBuffer
= new StringBuilder(8);
840 scanState
= ScanState
.GROUND
;
844 * Reset the tab stops list.
846 private void resetTabStops() {
848 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
849 tabStops
.add(new Integer(i
* 8));
854 * Reset the emulation state.
856 private void reset() {
858 currentState
= new SaveableState();
859 savedState
= new SaveableState();
860 scanState
= ScanState
.GROUND
;
864 scrollRegionBottom
= height
- 1;
865 rightMargin
= width
- 1;
867 arrowKeyMode
= ArrowKeyMode
.ANSI
;
868 keypadMode
= KeypadMode
.Numeric
;
869 wrapLineFlag
= false;
877 reverseVideo
= false;
879 cursorVisible
= true;
882 singleshift
= Singleshift
.NONE
;
884 printerControllerMode
= false;
887 mouseProtocol
= MouseProtocol
.OFF
;
888 mouseEncoding
= MouseEncoding
.X10
;
898 * Public constructor.
900 * @param type one of the DeviceType constants to select VT100, VT102,
902 * @param inputStream an InputStream connected to the remote side. For
903 * type == XTERM, inputStream is converted to a Reader with UTF-8
905 * @param outputStream an OutputStream connected to the remote user. For
906 * type == XTERM, outputStream is converted to a Writer with UTF-8
908 * @throws UnsupportedEncodingException if an exception is thrown when
909 * creating the InputStreamReader
911 public ECMA48(final DeviceType type
, final InputStream inputStream
,
912 final OutputStream outputStream
) throws UnsupportedEncodingException
{
914 assert (inputStream
!= null);
915 assert (outputStream
!= null);
917 csiParams
= new ArrayList
<Integer
>();
918 tabStops
= new ArrayList
<Integer
>();
919 scrollback
= new LinkedList
<DisplayLine
>();
920 display
= new LinkedList
<DisplayLine
>();
923 this.inputStream
= inputStream
;
924 if (type
== DeviceType
.XTERM
) {
925 this.input
= new InputStreamReader(inputStream
, "UTF-8");
926 this.output
= new OutputStreamWriter(outputStream
, "UTF-8");
927 this.outputStream
= null;
930 this.outputStream
= outputStream
;
934 for (int i
= 0; i
< height
; i
++) {
935 display
.add(new DisplayLine(currentState
.attr
));
938 // Spin up the input reader
939 readerThread
= new Thread(this);
940 readerThread
.start();
944 * Append a new line to the bottom of the display, adding lines off the
945 * top to the scrollback buffer.
947 private void newDisplayLine() {
948 // Scroll the top line off into the scrollback buffer
949 scrollback
.add(display
.get(0));
951 DisplayLine line
= new DisplayLine(currentState
.attr
);
952 line
.setReverseColor(reverseVideo
);
957 * Wraps the current line.
959 private void wrapCurrentLine() {
960 if (currentState
.cursorY
== height
- 1) {
963 if (currentState
.cursorY
< height
- 1) {
964 currentState
.cursorY
++;
966 currentState
.cursorX
= 0;
970 * Handle a carriage return.
972 private void carriageReturn() {
973 currentState
.cursorX
= 0;
974 wrapLineFlag
= false;
978 * Reverse the color of the visible display.
980 private void invertDisplayColors() {
981 for (DisplayLine line
: display
) {
982 line
.setReverseColor(!line
.isReverseColor());
989 private void linefeed() {
991 if (currentState
.cursorY
< scrollRegionBottom
) {
992 // Increment screen y
993 currentState
.cursorY
++;
996 // Screen y does not increment
999 * Two cases: either we're inside a scrolling region or not. If
1000 * the scrolling region bottom is the bottom of the screen, then
1001 * push the top line into the buffer. Else scroll the scrolling
1004 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1006 // We're at the bottom of the scroll region, AND the scroll
1007 // region is the entire screen.
1013 // We're at the bottom of the scroll region, AND the scroll
1014 // region is NOT the entire screen.
1015 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1020 currentState
.cursorX
= 0;
1022 wrapLineFlag
= false;
1026 * Prints one character to the display buffer.
1028 * @param ch character to display
1030 private void printCharacter(final char ch
) {
1031 int rightMargin
= this.rightMargin
;
1033 // Check if we have double-width, and if so chop at 40/66 instead of
1035 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1036 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1039 // Check the unusually-complicated line wrapping conditions...
1040 if (currentState
.cursorX
== rightMargin
) {
1042 if (currentState
.lineWrap
== true) {
1044 * This case happens when: the cursor was already on the
1045 * right margin (either through printing or by an explicit
1046 * placement command), and a character was printed.
1048 * The line wraps only when a new character arrives AND the
1049 * cursor is already on the right margin AND has placed a
1050 * character in its cell. Easier to see than to explain.
1052 if (wrapLineFlag
== false) {
1054 * This block marks the case that we are in the margin
1055 * and the first character has been received and printed.
1057 wrapLineFlag
= true;
1060 * This block marks the case that we are in the margin
1061 * and the second character has been received and
1064 wrapLineFlag
= false;
1068 } else if (currentState
.cursorX
<= rightMargin
) {
1070 * This is the normal case: a character came in and was printed
1071 * to the left of the right margin column.
1074 // Turn off VT100 special-case flag
1075 wrapLineFlag
= false;
1078 // "Print" the character
1079 Cell newCell
= new Cell(ch
);
1080 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1081 newCellAttributes
.setTo(currentState
.attr
);
1082 DisplayLine line
= display
.get(currentState
.cursorY
);
1083 // Insert mode special case
1084 if (insertMode
== true) {
1085 line
.insert(currentState
.cursorX
, newCell
);
1087 // Replace an existing character
1088 line
.replace(currentState
.cursorX
, newCell
);
1091 // Increment horizontal
1092 if (wrapLineFlag
== false) {
1093 currentState
.cursorX
++;
1094 if (currentState
.cursorX
> rightMargin
) {
1095 currentState
.cursorX
--;
1101 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1102 * send to the remote side.
1104 * @param mouse mouse event received from the local user
1106 public void mouse(final TMouseEvent mouse
) {
1109 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1110 mouseProtocol, mouseEncoding, mouse);
1113 if (mouseEncoding
!= MouseEncoding
.UTF8
) {
1114 // We will support X10 but only for (160,94) and smaller.
1115 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1120 switch (mouseProtocol
) {
1127 // Only report button presses
1128 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1134 // Only report button presses and releases
1135 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1136 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1144 * Only report button presses, button releases, and motions that
1145 * have a button down (i.e. drag-and-drop).
1147 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1148 if (!mouse
.getMouse1()
1149 && !mouse
.getMouse2()
1150 && !mouse
.getMouse3()
1151 && !mouse
.getMouseWheelUp()
1152 && !mouse
.getMouseWheelDown()
1160 // Report everything
1164 // Now encode the event
1165 StringBuilder sb
= new StringBuilder(6);
1166 sb
.append((char) 0x1B);
1169 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1170 sb
.append((char) (0x03 + 32));
1171 } else if (mouse
.getMouse1()) {
1172 sb
.append((char) (0x00 + 32));
1173 } else if (mouse
.getMouse2()) {
1174 sb
.append((char) (0x01 + 32));
1175 } else if (mouse
.getMouse3()) {
1176 sb
.append((char) (0x02 + 32));
1177 } else if (mouse
.getMouseWheelUp()) {
1178 sb
.append((char) (0x04 + 64));
1179 } else if (mouse
.getMouseWheelDown()) {
1180 sb
.append((char) (0x05 + 64));
1182 sb
.append((char) (0x03 + 32));
1185 sb
.append((char) (mouse
.getX() + 33));
1186 sb
.append((char) (mouse
.getY() + 33));
1188 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1189 writeRemote(sb
.toString());
1193 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1194 * send to the remote side.
1196 * @param keypress keypress received from the local user
1198 public void keypress(final TKeypress keypress
) {
1199 writeRemote(keypressToString(keypress
));
1203 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1205 * @param keypress keypress received from the local user
1206 * @return string to transmit to the remote side
1208 private String
keypressToString(final TKeypress keypress
) {
1210 if ((fullDuplex
== false) && (!keypress
.getIsKey())) {
1212 * If this is a control character, process it like it came from
1215 if (keypress
.getCh() < 0x20) {
1216 handleControlChar(keypress
.getCh());
1218 // Local echo for everything else
1219 printCharacter(keypress
.getCh());
1223 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1228 // Handle control characters
1229 if ((keypress
.getCtrl()) && (!keypress
.getIsKey())) {
1230 StringBuilder sb
= new StringBuilder();
1231 char ch
= keypress
.getCh();
1234 return sb
.toString();
1237 // Handle alt characters
1238 if ((keypress
.getAlt()) && (!keypress
.getIsKey())) {
1239 StringBuilder sb
= new StringBuilder("\033");
1240 char ch
= keypress
.getCh();
1242 return sb
.toString();
1245 if (keypress
.equals(kbBackspace
)) {
1258 if (keypress
.equals(kbLeft
)) {
1259 switch (arrowKeyMode
) {
1269 if (keypress
.equals(kbRight
)) {
1270 switch (arrowKeyMode
) {
1280 if (keypress
.equals(kbUp
)) {
1281 switch (arrowKeyMode
) {
1291 if (keypress
.equals(kbDown
)) {
1292 switch (arrowKeyMode
) {
1302 if (keypress
.equals(kbHome
)) {
1303 switch (arrowKeyMode
) {
1313 if (keypress
.equals(kbEnd
)) {
1314 switch (arrowKeyMode
) {
1324 if (keypress
.equals(kbF1
)) {
1332 if (keypress
.equals(kbF2
)) {
1340 if (keypress
.equals(kbF3
)) {
1348 if (keypress
.equals(kbF4
)) {
1356 if (keypress
.equals(kbF5
)) {
1369 if (keypress
.equals(kbF6
)) {
1382 if (keypress
.equals(kbF7
)) {
1395 if (keypress
.equals(kbF8
)) {
1408 if (keypress
.equals(kbF9
)) {
1421 if (keypress
.equals(kbF10
)) {
1434 if (keypress
.equals(kbF11
)) {
1438 if (keypress
.equals(kbF12
)) {
1442 if (keypress
.equals(kbShiftF1
)) {
1450 if (keypress
.equals(kbShiftF2
)) {
1458 if (keypress
.equals(kbShiftF3
)) {
1466 if (keypress
.equals(kbShiftF4
)) {
1474 if (keypress
.equals(kbShiftF5
)) {
1476 return "\033[15;2~";
1479 if (keypress
.equals(kbShiftF6
)) {
1481 return "\033[17;2~";
1484 if (keypress
.equals(kbShiftF7
)) {
1486 return "\033[18;2~";
1489 if (keypress
.equals(kbShiftF8
)) {
1491 return "\033[19;2~";
1494 if (keypress
.equals(kbShiftF9
)) {
1496 return "\033[20;2~";
1499 if (keypress
.equals(kbShiftF10
)) {
1501 return "\033[21;2~";
1504 if (keypress
.equals(kbShiftF11
)) {
1506 return "\033[23;2~";
1509 if (keypress
.equals(kbShiftF12
)) {
1511 return "\033[24;2~";
1514 if (keypress
.equals(kbCtrlF1
)) {
1522 if (keypress
.equals(kbCtrlF2
)) {
1530 if (keypress
.equals(kbCtrlF3
)) {
1538 if (keypress
.equals(kbCtrlF4
)) {
1546 if (keypress
.equals(kbCtrlF5
)) {
1548 return "\033[15;5~";
1551 if (keypress
.equals(kbCtrlF6
)) {
1553 return "\033[17;5~";
1556 if (keypress
.equals(kbCtrlF7
)) {
1558 return "\033[18;5~";
1561 if (keypress
.equals(kbCtrlF8
)) {
1563 return "\033[19;5~";
1566 if (keypress
.equals(kbCtrlF9
)) {
1568 return "\033[20;5~";
1571 if (keypress
.equals(kbCtrlF10
)) {
1573 return "\033[21;5~";
1576 if (keypress
.equals(kbCtrlF11
)) {
1578 return "\033[23;5~";
1581 if (keypress
.equals(kbCtrlF12
)) {
1583 return "\033[24;5~";
1586 if (keypress
.equals(kbPgUp
)) {
1591 if (keypress
.equals(kbPgDn
)) {
1596 if (keypress
.equals(kbIns
)) {
1601 if (keypress
.equals(kbShiftIns
)) {
1602 // This is what xterm sends for SHIFT-INS
1604 // This is what xterm sends for CTRL-INS
1605 // return "\033[2;5~";
1608 if (keypress
.equals(kbShiftDel
)) {
1609 // This is what xterm sends for SHIFT-DEL
1611 // This is what xterm sends for CTRL-DEL
1612 // return "\033[3;5~";
1615 if (keypress
.equals(kbDel
)) {
1616 // Delete sends real delete for VTxxx
1618 // return "\033[3~";
1621 if (keypress
.equals(kbEnter
)) {
1625 if (keypress
.equals(kbEsc
)) {
1629 if (keypress
.equals(kbAltEsc
)) {
1633 if (keypress
.equals(kbTab
)) {
1637 // Non-alt, non-ctrl characters
1638 if (!keypress
.getIsKey()) {
1639 StringBuilder sb
= new StringBuilder();
1640 sb
.append(keypress
.getCh());
1641 return sb
.toString();
1647 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1650 * @param ch 8-bit character from the remote side
1651 * @param charsetGl character set defined for GL
1652 * @param charsetGr character set defined for GR
1653 * @return character to display on the screen
1655 private char mapCharacterCharset(final char ch
,
1656 final CharacterSet charsetGl
,
1657 final CharacterSet charsetGr
) {
1659 int lookupChar
= ch
;
1660 CharacterSet lookupCharset
= charsetGl
;
1663 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
1664 lookupCharset
= charsetGr
;
1668 switch (lookupCharset
) {
1671 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
1674 return DECCharacterSets
.UK
[lookupChar
];
1677 return DECCharacterSets
.US_ASCII
[lookupChar
];
1680 return DECCharacterSets
.NL
[lookupChar
];
1683 return DECCharacterSets
.FI
[lookupChar
];
1686 return DECCharacterSets
.FR
[lookupChar
];
1689 return DECCharacterSets
.FR_CA
[lookupChar
];
1692 return DECCharacterSets
.DE
[lookupChar
];
1695 return DECCharacterSets
.IT
[lookupChar
];
1698 return DECCharacterSets
.NO
[lookupChar
];
1701 return DECCharacterSets
.ES
[lookupChar
];
1704 return DECCharacterSets
.SV
[lookupChar
];
1707 return DECCharacterSets
.SWISS
[lookupChar
];
1709 case DEC_SUPPLEMENTAL
:
1710 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
1713 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
1716 return DECCharacterSets
.US_ASCII
[lookupChar
];
1719 return DECCharacterSets
.US_ASCII
[lookupChar
];
1722 throw new IllegalArgumentException("Invalid character set value: "
1728 * Map an 8-bit byte into a printable character.
1730 * @param ch either 8-bit or Unicode character from the remote side
1731 * @return character to display on the screen
1733 private char mapCharacter(final char ch
) {
1735 // Unicode character, just return it
1739 CharacterSet charsetGl
= currentState
.g0Charset
;
1740 CharacterSet charsetGr
= currentState
.grCharset
;
1742 if (vt52Mode
== true) {
1743 if (shiftOut
== true) {
1744 // Shifted out character, pull from VT52 graphics
1745 charsetGl
= currentState
.g1Charset
;
1746 charsetGr
= CharacterSet
.US
;
1749 charsetGl
= currentState
.g0Charset
;
1750 charsetGr
= CharacterSet
.US
;
1753 // Pull the character
1754 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1758 if (shiftOut
== true) {
1759 // Shifted out character, pull from G1
1760 charsetGl
= currentState
.g1Charset
;
1761 charsetGr
= currentState
.grCharset
;
1763 // Pull the character
1764 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1768 if (singleshift
== Singleshift
.SS2
) {
1770 singleshift
= Singleshift
.NONE
;
1772 // Shifted out character, pull from G2
1773 charsetGl
= currentState
.g2Charset
;
1774 charsetGr
= currentState
.grCharset
;
1778 if (singleshift
== Singleshift
.SS3
) {
1780 singleshift
= Singleshift
.NONE
;
1782 // Shifted out character, pull from G3
1783 charsetGl
= currentState
.g3Charset
;
1784 charsetGr
= currentState
.grCharset
;
1787 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
1788 // Check for locking shift
1790 switch (currentState
.glLockshift
) {
1803 charsetGl
= currentState
.g2Charset
;
1808 charsetGl
= currentState
.g3Charset
;
1813 charsetGl
= currentState
.g0Charset
;
1817 switch (currentState
.grLockshift
) {
1827 charsetGr
= currentState
.g1Charset
;
1832 charsetGr
= currentState
.g2Charset
;
1837 charsetGr
= currentState
.g3Charset
;
1842 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
1849 // Pull the character
1850 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1854 * Scroll the text within a scrolling region up n lines.
1856 * @param regionTop top row of the scrolling region
1857 * @param regionBottom bottom row of the scrolling region
1858 * @param n number of lines to scroll
1860 private void scrollingRegionScrollUp(final int regionTop
,
1861 final int regionBottom
, final int n
) {
1863 if (regionTop
>= regionBottom
) {
1867 // Sanity check: see if there will be any characters left after the
1869 if (regionBottom
+ 1 - regionTop
<= n
) {
1870 // There won't be anything left in the region, so just call
1871 // eraseScreen() and return.
1872 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1876 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1877 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1878 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1880 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
1881 - remaining
, regionBottom
+ 1);
1882 display
= new LinkedList
<DisplayLine
>(displayTop
);
1883 display
.addAll(displayMiddle
);
1884 for (int i
= 0; i
< n
; i
++) {
1885 DisplayLine line
= new DisplayLine(currentState
.attr
);
1886 line
.setReverseColor(reverseVideo
);
1889 display
.addAll(displayBottom
);
1891 assert (display
.size() == height
);
1895 * Scroll the text within a scrolling region down n lines.
1897 * @param regionTop top row of the scrolling region
1898 * @param regionBottom bottom row of the scrolling region
1899 * @param n number of lines to scroll
1901 private void scrollingRegionScrollDown(final int regionTop
,
1902 final int regionBottom
, final int n
) {
1904 if (regionTop
>= regionBottom
) {
1908 // Sanity check: see if there will be any characters left after the
1910 if (regionBottom
+ 1 - regionTop
<= n
) {
1911 // There won't be anything left in the region, so just call
1912 // eraseScreen() and return.
1913 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1917 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1918 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1919 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1921 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
1922 regionTop
+ remaining
);
1923 display
= new LinkedList
<DisplayLine
>(displayTop
);
1924 for (int i
= 0; i
< n
; i
++) {
1925 DisplayLine line
= new DisplayLine(currentState
.attr
);
1926 line
.setReverseColor(reverseVideo
);
1929 display
.addAll(displayMiddle
);
1930 display
.addAll(displayBottom
);
1932 assert (display
.size() == height
);
1936 * Process a control character.
1938 * @param ch 8-bit character from the remote side
1940 private void handleControlChar(final char ch
) {
1941 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
1952 // Transmit the answerback message.
1963 cursorLeft(1, false);
1968 advanceToNextTabStop();
1994 currentState
.glLockshift
= LockshiftMode
.NONE
;
2000 currentState
.glLockshift
= LockshiftMode
.NONE
;
2025 singleshift
= Singleshift
.SS2
;
2030 singleshift
= Singleshift
.SS3
;
2040 * Advance the cursor to the next tab stop.
2042 private void advanceToNextTabStop() {
2043 if (tabStops
.size() == 0) {
2044 // Go to the rightmost column
2045 cursorRight(rightMargin
- currentState
.cursorX
, false);
2048 for (Integer stop
: tabStops
) {
2049 if (stop
> currentState
.cursorX
) {
2050 cursorRight(stop
- currentState
.cursorX
, false);
2055 * We got here, meaning there isn't a tab stop beyond the current
2056 * cursor position. Place the cursor of the right-most edge of the
2059 cursorRight(rightMargin
- currentState
.cursorX
, false);
2063 * Save a character into the collect buffer.
2065 * @param ch character to save
2067 private void collect(final char ch
) {
2068 collectBuffer
.append(ch
);
2072 * Save a byte into the CSI parameters buffer.
2074 * @param ch byte to save
2076 private void param(final byte ch
) {
2077 if (csiParams
.size() == 0) {
2078 csiParams
.add(new Integer(0));
2080 Integer x
= csiParams
.get(csiParams
.size() - 1);
2081 if ((ch
>= '0') && (ch
<= '9')) {
2084 csiParams
.set(csiParams
.size() - 1, x
);
2088 csiParams
.add(new Integer(0));
2093 * Get a CSI parameter value, with a default.
2095 * @param position parameter index. 0 is the first parameter.
2096 * @param defaultValue value to use if csiParams[position] doesn't exist
2097 * @return parameter value
2099 private int getCsiParam(final int position
, final int defaultValue
) {
2100 if (csiParams
.size() < position
+ 1) {
2101 return defaultValue
;
2103 return csiParams
.get(position
).intValue();
2107 * Get a CSI parameter value, clamped to within min/max.
2109 * @param position parameter index. 0 is the first parameter.
2110 * @param defaultValue value to use if csiParams[position] doesn't exist
2111 * @param minValue minimum value inclusive
2112 * @param maxValue maximum value inclusive
2113 * @return parameter value
2115 private int getCsiParam(final int position
, final int defaultValue
,
2116 final int minValue
, final int maxValue
) {
2118 assert (minValue
<= maxValue
);
2119 int value
= getCsiParam(position
, defaultValue
);
2120 if (value
< minValue
) {
2123 if (value
> maxValue
) {
2130 * Set or unset a toggle. value is 'true' for set ('h'), false for reset
2133 private void setToggle(final boolean value
) {
2134 boolean decPrivateModeFlag
= false;
2135 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2136 if (collectBuffer
.charAt(i
) == '?') {
2137 decPrivateModeFlag
= true;
2142 for (Integer i
: csiParams
) {
2147 if (decPrivateModeFlag
== true) {
2149 if (value
== true) {
2150 // Use application arrow keys
2151 arrowKeyMode
= ArrowKeyMode
.VT100
;
2153 // Use ANSI arrow keys
2154 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2159 if (decPrivateModeFlag
== true) {
2160 if (value
== false) {
2164 arrowKeyMode
= ArrowKeyMode
.VT52
;
2167 * From the VT102 docs: "You use ANSI mode to select
2168 * most terminal features; the terminal uses the same
2169 * features when it switches to VT52 mode. You
2170 * cannot, however, change most of these features in
2173 * In other words, do not reset any other attributes
2174 * when switching between VT52 submode and ANSI.
2176 * HOWEVER, the real vt100 does switch the character
2177 * set according to Usenet.
2179 currentState
.g0Charset
= CharacterSet
.US
;
2180 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2183 if ((type
== DeviceType
.VT220
)
2184 || (type
== DeviceType
.XTERM
)) {
2186 // VT52 mode is explicitly 7-bit
2188 singleshift
= Singleshift
.NONE
;
2193 if (value
== true) {
2194 // Turn off keyboard
2203 if (decPrivateModeFlag
== true) {
2205 if (value
== true) {
2214 width
= rightMargin
+ 1;
2215 // Entire screen is cleared, and scrolling region is
2217 eraseScreen(0, 0, height
- 1, width
- 1, false);
2218 scrollRegionTop
= 0;
2219 scrollRegionBottom
= height
- 1;
2220 // Also home the cursor
2221 cursorPosition(0, 0);
2225 if (decPrivateModeFlag
== true) {
2227 if (value
== true) {
2236 if (value
== true) {
2244 if (decPrivateModeFlag
== true) {
2246 if (value
== true) {
2248 * Set selects reverse screen, a white screen
2249 * background with black characters.
2251 if (reverseVideo
!= true) {
2253 * If in normal video, switch it back
2255 invertDisplayColors();
2257 reverseVideo
= true;
2260 * Reset selects normal screen, a black screen
2261 * background with white characters.
2263 if (reverseVideo
== true) {
2265 * If in reverse video already, switch it back
2267 invertDisplayColors();
2269 reverseVideo
= false;
2274 if (decPrivateModeFlag
== true) {
2276 if (value
== true) {
2277 // Origin is relative to scroll region cursor.
2278 // Cursor can NEVER leave scrolling region.
2279 currentState
.originMode
= true;
2280 cursorPosition(0, 0);
2282 // Origin is absolute to entire screen. Cursor can
2283 // leave the scrolling region via cup() and hvp().
2284 currentState
.originMode
= false;
2285 cursorPosition(0, 0);
2290 if (decPrivateModeFlag
== true) {
2292 if (value
== true) {
2294 currentState
.lineWrap
= true;
2296 // Turn linewrap off
2297 currentState
.lineWrap
= false;
2302 if (decPrivateModeFlag
== true) {
2304 if (value
== true) {
2305 // Keyboard auto-repeat on
2308 // Keyboard auto-repeat off
2314 if (decPrivateModeFlag
== false) {
2316 if (value
== true) {
2326 if (decPrivateModeFlag
== true) {
2332 if (decPrivateModeFlag
== true) {
2338 if (decPrivateModeFlag
== false) {
2340 if (value
== true) {
2342 * Set causes a received linefeed, form feed, or
2343 * vertical tab to move cursor to first column of
2344 * next line. RETURN transmits both a carriage return
2345 * and linefeed. This selection is also called new
2351 * Reset causes a received linefeed, form feed, or
2352 * vertical tab to move cursor to next line in
2353 * current column. RETURN transmits a carriage
2356 newLineMode
= false;
2362 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2363 if (decPrivateModeFlag
== true) {
2365 if (value
== true) {
2367 cursorVisible
= true;
2370 cursorVisible
= false;
2377 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2378 if (decPrivateModeFlag
== true) {
2380 if (value
== true) {
2381 // Select national mode NRC
2384 // Select multi-national mode
2393 if ((type
== DeviceType
.XTERM
)
2394 && (decPrivateModeFlag
== true)
2396 // Mouse: normal tracking mode
2397 if (value
== true) {
2398 mouseProtocol
= MouseProtocol
.NORMAL
;
2400 mouseProtocol
= MouseProtocol
.OFF
;
2406 if ((type
== DeviceType
.XTERM
)
2407 && (decPrivateModeFlag
== true)
2409 // Mouse: normal tracking mode
2410 if (value
== true) {
2411 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2413 mouseProtocol
= MouseProtocol
.OFF
;
2419 if ((type
== DeviceType
.XTERM
)
2420 && (decPrivateModeFlag
== true)
2422 // Mouse: Any-event tracking mode
2423 if (value
== true) {
2424 mouseProtocol
= MouseProtocol
.ANYEVENT
;
2426 mouseProtocol
= MouseProtocol
.OFF
;
2432 if ((type
== DeviceType
.XTERM
)
2433 && (decPrivateModeFlag
== true)
2435 // Mouse: UTF-8 coordinates
2436 if (value
== true) {
2437 mouseEncoding
= MouseEncoding
.UTF8
;
2439 mouseEncoding
= MouseEncoding
.X10
;
2452 * DECSC - Save cursor.
2454 private void decsc() {
2455 savedState
.setTo(currentState
);
2459 * DECRC - Restore cursor.
2461 private void decrc() {
2462 currentState
.setTo(savedState
);
2468 private void ind() {
2469 // Move the cursor and scroll if necessary. If at the bottom line
2470 // already, a scroll up is supposed to be performed.
2471 if (currentState
.cursorY
== scrollRegionBottom
) {
2472 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2474 cursorDown(1, true);
2478 * RI - Reverse index.
2481 // Move the cursor and scroll if necessary. If at the top line
2482 // already, a scroll down is supposed to be performed.
2483 if (currentState
.cursorY
== scrollRegionTop
) {
2484 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2492 private void nel() {
2493 // Move the cursor and scroll if necessary. If at the bottom line
2494 // already, a scroll up is supposed to be performed.
2495 if (currentState
.cursorY
== scrollRegionBottom
) {
2496 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2498 cursorDown(1, true);
2500 // Reset to the beginning of the next line
2501 currentState
.cursorX
= 0;
2505 * DECKPAM - Keypad application mode.
2507 private void deckpam() {
2508 keypadMode
= KeypadMode
.Application
;
2512 * DECKPNM - Keypad numeric mode.
2514 private void deckpnm() {
2515 keypadMode
= KeypadMode
.Numeric
;
2521 * @param n number of spaces to move
2522 * @param honorScrollRegion if true, then do nothing if the cursor is
2523 * outside the scrolling region
2525 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2529 * Special case: if a user moves the cursor from the right margin, we
2530 * have to reset the VT100 right margin flag.
2533 wrapLineFlag
= false;
2536 for (int i
= 0; i
< n
; i
++) {
2537 if (honorScrollRegion
== true) {
2538 // Honor the scrolling region
2539 if ((currentState
.cursorY
< scrollRegionTop
)
2540 || (currentState
.cursorY
> scrollRegionBottom
)
2542 // Outside region, do nothing
2545 // Inside region, go up
2546 top
= scrollRegionTop
;
2548 // Non-scrolling case
2552 if (currentState
.cursorY
> top
) {
2553 currentState
.cursorY
--;
2559 * Move down n spaces.
2561 * @param n number of spaces to move
2562 * @param honorScrollRegion if true, then do nothing if the cursor is
2563 * outside the scrolling region
2565 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
2569 * Special case: if a user moves the cursor from the right margin, we
2570 * have to reset the VT100 right margin flag.
2573 wrapLineFlag
= false;
2576 for (int i
= 0; i
< n
; i
++) {
2578 if (honorScrollRegion
== true) {
2579 // Honor the scrolling region
2580 if (currentState
.cursorY
> scrollRegionBottom
) {
2581 // Outside region, do nothing
2584 // Inside region, go down
2585 bottom
= scrollRegionBottom
;
2587 // Non-scrolling case
2588 bottom
= height
- 1;
2591 if (currentState
.cursorY
< bottom
) {
2592 currentState
.cursorY
++;
2598 * Move left n spaces.
2600 * @param n number of spaces to move
2601 * @param honorScrollRegion if true, then do nothing if the cursor is
2602 * outside the scrolling region
2604 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
2606 * Special case: if a user moves the cursor from the right margin, we
2607 * have to reset the VT100 right margin flag.
2610 wrapLineFlag
= false;
2613 for (int i
= 0; i
< n
; i
++) {
2614 if (honorScrollRegion
== true) {
2615 // Honor the scrolling region
2616 if ((currentState
.cursorY
< scrollRegionTop
)
2617 || (currentState
.cursorY
> scrollRegionBottom
)
2619 // Outside region, do nothing
2624 if (currentState
.cursorX
> 0) {
2625 currentState
.cursorX
--;
2631 * Move right 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 cursorRight(final int n
, final boolean honorScrollRegion
) {
2638 int rightMargin
= this.rightMargin
;
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 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2649 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2652 for (int i
= 0; i
< n
; i
++) {
2653 if (honorScrollRegion
== true) {
2654 // Honor the scrolling region
2655 if ((currentState
.cursorY
< scrollRegionTop
)
2656 || (currentState
.cursorY
> scrollRegionBottom
)
2658 // Outside region, do nothing
2663 if (currentState
.cursorX
< rightMargin
) {
2664 currentState
.cursorX
++;
2670 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2672 * @param row row to move to
2673 * @param col column to move to
2675 private void cursorPosition(int row
, final int col
) {
2676 int rightMargin
= this.rightMargin
;
2681 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2682 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2685 // Set column number
2686 currentState
.cursorX
= col
;
2688 // Sanity check, bring column back to margin.
2689 if (currentState
.cursorX
> rightMargin
) {
2690 currentState
.cursorX
= rightMargin
;
2694 if (currentState
.originMode
== true) {
2695 row
+= scrollRegionTop
;
2697 if (currentState
.cursorY
< row
) {
2698 cursorDown(row
- currentState
.cursorY
, false);
2699 } else if (currentState
.cursorY
> row
) {
2700 cursorUp(currentState
.cursorY
- row
, false);
2703 wrapLineFlag
= false;
2707 * HTS - Horizontal tabulation set.
2709 private void hts() {
2710 for (Integer stop
: tabStops
) {
2711 if (stop
== currentState
.cursorX
) {
2712 // Already have a tab stop here
2717 // Append a tab stop to the end of the array and resort them
2718 tabStops
.add(currentState
.cursorX
);
2719 Collections
.sort(tabStops
);
2723 * DECSWL - Single-width line.
2725 private void decswl() {
2726 display
.get(currentState
.cursorY
).setDoubleWidth(false);
2727 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2731 * DECDWL - Double-width line.
2733 private void decdwl() {
2734 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2735 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2739 * DECHDL - Double-height + double-width line.
2741 * @param topHalf if true, this sets the row to be the top half row of a
2744 private void dechdl(final boolean topHalf
) {
2745 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2746 if (topHalf
== true) {
2747 display
.get(currentState
.cursorY
).setDoubleHeight(1);
2749 display
.get(currentState
.cursorY
).setDoubleHeight(2);
2754 * DECALN - Screen alignment display.
2756 private void decaln() {
2757 Cell newCell
= new Cell();
2758 newCell
.setChar('E');
2759 for (DisplayLine line
: display
) {
2760 for (int i
= 0; i
< line
.length(); i
++) {
2761 line
.replace(i
, newCell
);
2767 * DECSCL - Compatibility level.
2769 private void decscl() {
2770 int i
= getCsiParam(0, 0);
2771 int j
= getCsiParam(1, 0);
2775 currentState
.g0Charset
= CharacterSet
.US
;
2776 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2778 } else if (i
== 62) {
2780 if ((j
== 0) || (j
== 2)) {
2782 } else if (j
== 1) {
2789 * CUD - Cursor down.
2791 private void cud() {
2792 cursorDown(getCsiParam(0, 1, 1, height
), true);
2796 * CUF - Cursor forward.
2798 private void cuf() {
2799 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
2803 * CUB - Cursor backward.
2805 private void cub() {
2806 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
2812 private void cuu() {
2813 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2817 * CUP - Cursor position.
2819 private void cup() {
2820 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2821 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
2825 * CNL - Cursor down and to column 1.
2827 private void cnl() {
2828 cursorDown(getCsiParam(0, 1, 1, height
), true);
2830 cursorLeft(currentState
.cursorX
, true);
2834 * CPL - Cursor up and to column 1.
2836 private void cpl() {
2837 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2839 cursorLeft(currentState
.cursorX
, true);
2843 * CHA - Cursor to column # in current row.
2845 private void cha() {
2846 cursorPosition(currentState
.cursorY
,
2847 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
2851 * VPA - Cursor to row #, same column.
2853 private void vpa() {
2854 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2855 currentState
.cursorX
);
2859 * ED - Erase in display.
2862 boolean honorProtected
= false;
2863 boolean decPrivateModeFlag
= false;
2865 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2866 if (collectBuffer
.charAt(i
) == '?') {
2867 decPrivateModeFlag
= true;
2872 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2873 && (decPrivateModeFlag
== true)
2875 honorProtected
= true;
2878 int i
= getCsiParam(0, 0);
2881 // Erase from here to end of screen
2882 if (currentState
.cursorY
< height
- 1) {
2883 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
2886 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2887 } else if (i
== 1) {
2888 // Erase from beginning of screen to here
2889 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
2891 eraseLine(0, currentState
.cursorX
, honorProtected
);
2892 } else if (i
== 2) {
2893 // Erase entire screen
2894 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
2899 * EL - Erase in line.
2902 boolean honorProtected
= false;
2903 boolean decPrivateModeFlag
= false;
2905 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2906 if (collectBuffer
.charAt(i
) == '?') {
2907 decPrivateModeFlag
= true;
2912 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2913 && (decPrivateModeFlag
== true)
2915 honorProtected
= true;
2918 int i
= getCsiParam(0, 0);
2921 // Erase from here to end of line
2922 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2923 } else if (i
== 1) {
2924 // Erase from beginning of line to here
2925 eraseLine(0, currentState
.cursorX
, honorProtected
);
2926 } else if (i
== 2) {
2927 // Erase entire line
2928 eraseLine(0, width
- 1, honorProtected
);
2933 * ECH - Erase # of characters in current row.
2935 private void ech() {
2936 int i
= getCsiParam(0, 1, 1, width
);
2938 // Erase from here to i characters
2939 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
2946 int i
= getCsiParam(0, 1);
2948 if ((currentState
.cursorY
>= scrollRegionTop
)
2949 && (currentState
.cursorY
<= scrollRegionBottom
)
2952 // I can get the same effect with a scroll-down
2953 scrollingRegionScrollDown(currentState
.cursorY
,
2954 scrollRegionBottom
, i
);
2959 * DCH - Delete char.
2961 private void dch() {
2962 int n
= getCsiParam(0, 1);
2963 DisplayLine line
= display
.get(currentState
.cursorY
);
2964 Cell blank
= new Cell();
2965 for (int i
= 0; i
< n
; i
++) {
2966 line
.delete(currentState
.cursorX
, blank
);
2971 * ICH - Insert blank char at cursor.
2973 private void ich() {
2974 int n
= getCsiParam(0, 1);
2975 DisplayLine line
= display
.get(currentState
.cursorY
);
2976 Cell blank
= new Cell();
2977 for (int i
= 0; i
< n
; i
++) {
2978 line
.insert(currentState
.cursorX
, blank
);
2986 int i
= getCsiParam(0, 1);
2988 if ((currentState
.cursorY
>= scrollRegionTop
)
2989 && (currentState
.cursorY
<= scrollRegionBottom
)) {
2991 // I can get the same effect with a scroll-down
2992 scrollingRegionScrollUp(currentState
.cursorY
,
2993 scrollRegionBottom
, i
);
2998 * HVP - Horizontal and vertical position.
3000 private void hvp() {
3005 * REP - Repeat character.
3007 private void rep() {
3008 int n
= getCsiParam(0, 1);
3009 for (int i
= 0; i
< n
; i
++) {
3010 printCharacter(repCh
);
3018 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3019 getCsiParam(0, 1, 1, height
));
3026 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3027 getCsiParam(0, 1, 1, height
));
3031 * CBT - Go back X tab stops.
3033 private void cbt() {
3034 int tabsToMove
= getCsiParam(0, 1);
3037 for (int i
= 0; i
< tabsToMove
; i
++) {
3038 int j
= currentState
.cursorX
;
3039 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3040 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3048 j
= tabStops
.get(tabI
);
3050 cursorPosition(currentState
.cursorY
, j
);
3055 * CHT - Advance X tab stops.
3057 private void cht() {
3058 int n
= getCsiParam(0, 1);
3059 for (int i
= 0; i
< n
; i
++) {
3060 advanceToNextTabStop();
3065 * SGR - Select graphics rendition.
3067 private void sgr() {
3069 if (csiParams
.size() == 0) {
3070 currentState
.attr
.reset();
3074 for (Integer i
: csiParams
) {
3080 currentState
.attr
.reset();
3085 currentState
.attr
.setBold(true);
3090 currentState
.attr
.setUnderline(true);
3095 currentState
.attr
.setBlink(true);
3100 currentState
.attr
.setReverse(true);
3107 if (type
== DeviceType
.XTERM
) {
3121 if ((type
== DeviceType
.VT220
)
3122 || (type
== DeviceType
.XTERM
)) {
3128 currentState
.attr
.setBold(false);
3133 currentState
.attr
.setUnderline(false);
3138 currentState
.attr
.setBlink(false);
3143 currentState
.attr
.setReverse(false);
3151 // A true VT100/102/220 does not support color, however everyone
3152 // is used to their terminal emulator supporting color so we will
3153 // unconditionally support color for all DeviceType's.
3158 // Set black foreground
3159 currentState
.attr
.setForeColor(Color
.BLACK
);
3162 // Set red foreground
3163 currentState
.attr
.setForeColor(Color
.RED
);
3166 // Set green foreground
3167 currentState
.attr
.setForeColor(Color
.GREEN
);
3170 // Set yellow foreground
3171 currentState
.attr
.setForeColor(Color
.YELLOW
);
3174 // Set blue foreground
3175 currentState
.attr
.setForeColor(Color
.BLUE
);
3178 // Set magenta foreground
3179 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3182 // Set cyan foreground
3183 currentState
.attr
.setForeColor(Color
.CYAN
);
3186 // Set white foreground
3187 currentState
.attr
.setForeColor(Color
.WHITE
);
3190 // Underscore on, default foreground color
3191 currentState
.attr
.setUnderline(true);
3192 currentState
.attr
.setForeColor(Color
.WHITE
);
3195 // Underscore off, default foreground color
3196 currentState
.attr
.setUnderline(false);
3197 currentState
.attr
.setForeColor(Color
.WHITE
);
3200 // Set black background
3201 currentState
.attr
.setBackColor(Color
.BLACK
);
3204 // Set red background
3205 currentState
.attr
.setBackColor(Color
.RED
);
3208 // Set green background
3209 currentState
.attr
.setBackColor(Color
.GREEN
);
3212 // Set yellow background
3213 currentState
.attr
.setBackColor(Color
.YELLOW
);
3216 // Set blue background
3217 currentState
.attr
.setBackColor(Color
.BLUE
);
3220 // Set magenta background
3221 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3224 // Set cyan background
3225 currentState
.attr
.setBackColor(Color
.CYAN
);
3228 // Set white background
3229 currentState
.attr
.setBackColor(Color
.WHITE
);
3232 // Default background
3233 currentState
.attr
.setBackColor(Color
.BLACK
);
3243 * DA - Device attributes.
3246 int extendedFlag
= 0;
3248 if (collectBuffer
.length() > 0) {
3249 String args
= collectBuffer
.substring(1);
3250 if (collectBuffer
.charAt(0) == '>') {
3252 if (collectBuffer
.length() >= 2) {
3253 i
= Integer
.parseInt(args
.toString());
3255 } else if (collectBuffer
.charAt(0) == '=') {
3257 if (collectBuffer
.length() >= 2) {
3258 i
= Integer
.parseInt(args
.toString());
3261 // Unknown code, bail out
3266 if ((i
!= 0) && (i
!= 1)) {
3270 if ((extendedFlag
== 0) && (i
== 0)) {
3271 // Send string directly to remote side
3272 writeRemote(deviceTypeResponse());
3276 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3278 if ((extendedFlag
== 1) && (i
== 0)) {
3280 * Request "What type of terminal are you, what is your
3281 * firmware version, and what hardware options do you have
3284 * Respond: "I am a VT220 (identification code of 1), my
3285 * firmware version is _____ (Pv), and I have _____ Po
3286 * options installed."
3292 if (s8c1t
== true) {
3293 writeRemote("\u009b>1;10;0c");
3295 writeRemote("\033[>1;10;0c");
3301 if ((extendedFlag
== 2) && (i
== 0)) {
3304 * Request "What is your unit ID?"
3306 * Respond: "I was manufactured at site 00 and have a unique ID
3310 writeRemote("\033P!|00010203\033\\");
3315 * DECSTBM - Set top and bottom margins.
3317 private void decstbm() {
3318 boolean decPrivateModeFlag
= false;
3320 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3321 if (collectBuffer
.charAt(i
) == '?') {
3322 decPrivateModeFlag
= true;
3326 if (decPrivateModeFlag
) {
3327 // This could be restore DEC private mode values.
3331 int top
= getCsiParam(0, 1, 1, height
) - 1;
3332 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3337 scrollRegionTop
= top
;
3338 scrollRegionBottom
= bottom
;
3341 cursorPosition(0, 0);
3346 * DECREQTPARM - Request terminal parameters.
3348 private void decreqtparm() {
3349 int i
= getCsiParam(0, 0);
3351 if ((i
!= 0) && (i
!= 1)) {
3358 * Request terminal parameters.
3362 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3363 * (CLoCk MULtiplier = 1, STP option flags = 0)
3367 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3370 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3372 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3378 * DECSCA - Select Character Attributes.
3380 private void decsca() {
3381 int i
= getCsiParam(0, 0);
3383 if ((i
== 0) || (i
== 2)) {
3385 currentState
.attr
.setProtect(false);
3389 currentState
.attr
.setProtect(true);
3394 * DECSTR - Soft Terminal Reset.
3396 private void decstr() {
3397 // Do exactly like RIS - Reset to initial state
3399 // Do I clear screen too? I think so...
3400 eraseScreen(0, 0, height
- 1, width
- 1, false);
3401 cursorPosition(0, 0);
3405 * DSR - Device status report.
3407 private void dsr() {
3408 boolean decPrivateModeFlag
= false;
3410 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3411 if (collectBuffer
.charAt(i
) == '?') {
3412 decPrivateModeFlag
= true;
3417 int i
= getCsiParam(0, 0);
3422 // Request status report. Respond with "OK, no malfunction."
3424 // Send string directly to remote side
3425 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3428 writeRemote("\u009b0n");
3430 writeRemote("\033[0n");
3435 // Request cursor position. Respond with current position.
3437 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3440 str
= String
.format("\u009b%d;%dR",
3441 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3443 str
= String
.format("\033[%d;%dR",
3444 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3447 // Send string directly to remote side
3452 if (decPrivateModeFlag
== true) {
3454 // Request printer status report. Respond with "Printer not
3457 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3458 && (s8c1t
== true)) {
3459 writeRemote("\u009b?13n");
3461 writeRemote("\033[?13n");
3467 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3468 && (decPrivateModeFlag
== true)
3471 // Request user-defined keys are locked or unlocked. Respond
3472 // with "User-defined keys are locked."
3474 if (s8c1t
== true) {
3475 writeRemote("\u009b?21n");
3477 writeRemote("\033[?21n");
3483 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3484 && (decPrivateModeFlag
== true)
3487 // Request keyboard language. Respond with "Keyboard
3488 // language is North American."
3490 if (s8c1t
== true) {
3491 writeRemote("\u009b?27;1n");
3493 writeRemote("\033[?27;1n");
3500 // Some other option, ignore
3506 * TBC - Tabulation clear.
3508 private void tbc() {
3509 int i
= getCsiParam(0, 0);
3511 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3512 for (Integer stop
: tabStops
) {
3513 if (stop
== currentState
.cursorX
) {
3518 tabStops
= newStops
;
3526 * Erase the characters in the current line from the start column to the
3527 * end column, inclusive.
3529 * @param start starting column to erase (between 0 and width - 1)
3530 * @param end ending column to erase (between 0 and width - 1)
3531 * @param honorProtected if true, do not erase characters with the
3532 * protected attribute set
3534 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3539 if (end
> width
- 1) {
3546 for (int i
= start
; i
<= end
; i
++) {
3547 DisplayLine line
= display
.get(currentState
.cursorY
);
3548 if ((!honorProtected
)
3549 || ((honorProtected
) && (!line
.charAt(i
).getProtect()))) {
3556 * From the VT102 manual:
3558 * Erasing a character also erases any character
3559 * attribute of the character.
3565 * Erase with the current color a.k.a. back-color erase
3568 line
.setChar(i
, ' ');
3569 line
.setAttr(i
, currentState
.attr
);
3577 * Erase a rectangular section of the screen, inclusive. end column,
3580 * @param startRow starting row to erase (between 0 and height - 1)
3581 * @param startCol starting column to erase (between 0 and width - 1)
3582 * @param endRow ending row to erase (between 0 and height - 1)
3583 * @param endCol ending column to erase (between 0 and width - 1)
3584 * @param honorProtected if true, do not erase characters with the
3585 * protected attribute set
3587 private void eraseScreen(final int startRow
, final int startCol
,
3588 final int endRow
, final int endCol
, final boolean honorProtected
) {
3596 || (endRow
< startRow
)
3597 || (endCol
< startCol
)
3602 oldCursorY
= currentState
.cursorY
;
3603 for (int i
= startRow
; i
<= endRow
; i
++) {
3604 currentState
.cursorY
= i
;
3605 eraseLine(startCol
, endCol
, honorProtected
);
3607 // Erase display clears the double attributes
3608 display
.get(i
).setDoubleWidth(false);
3609 display
.get(i
).setDoubleHeight(0);
3611 currentState
.cursorY
= oldCursorY
;
3615 * VT220 printer functions. All of these are parsed, but won't do
3618 private void printerFunctions() {
3619 boolean decPrivateModeFlag
= false;
3620 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3621 if (collectBuffer
.charAt(i
) == '?') {
3622 decPrivateModeFlag
= true;
3627 int i
= getCsiParam(0, 0);
3632 if (decPrivateModeFlag
== false) {
3638 if (decPrivateModeFlag
== true) {
3639 // Print cursor line
3644 if (decPrivateModeFlag
== true) {
3645 // Auto print mode OFF
3647 // Printer controller OFF
3649 // Characters re-appear on the screen
3650 printerControllerMode
= false;
3655 if (decPrivateModeFlag
== true) {
3659 // Printer controller
3661 // Characters get sucked into oblivion
3662 printerControllerMode
= true;
3673 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
3674 * of remote systems will send an XTerm title sequence even if TERM isn't
3677 * @param xtermChar the character received from the remote side
3679 private void oscPut(final char xtermChar
) {
3681 collectBuffer
.append(xtermChar
);
3684 if (xtermChar
== 0x07) {
3685 String args
= collectBuffer
.substring(0,
3686 collectBuffer
.length() - 1);
3687 String
[] p
= args
.toString().split(";");
3689 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
3697 // Go to SCAN_GROUND state
3704 * Run this input character through the ECMA48 state machine.
3706 * @param ch character from the remote side
3708 private void consume(char ch
) {
3711 // System.err.printf("%c", ch);
3713 // Special case for VT10x: 7-bit characters only
3714 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
3715 ch
= (char)(ch
& 0x7F);
3718 // Special "anywhere" states
3720 // 18, 1A --> execute, then switch to SCAN_GROUND
3721 if ((ch
== 0x18) || (ch
== 0x1A)) {
3722 // CAN and SUB abort escape sequences
3727 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
3731 && (scanState
!= ScanState
.DCS_ENTRY
)
3732 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
3733 && (scanState
!= ScanState
.DCS_IGNORE
)
3734 && (scanState
!= ScanState
.DCS_PARAM
)
3735 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
3738 scanState
= ScanState
.ESCAPE
;
3742 // 0x9B == CSI 8-bit sequence
3744 scanState
= ScanState
.CSI_ENTRY
;
3748 // 0x9D goes to ScanState.OSC_STRING
3750 scanState
= ScanState
.OSC_STRING
;
3754 // 0x90 goes to DCS_ENTRY
3756 scanState
= ScanState
.DCS_ENTRY
;
3760 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
3761 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
3762 scanState
= ScanState
.SOSPMAPC_STRING
;
3766 // 0x7F (DEL) is always discarded
3771 switch (scanState
) {
3774 // 00-17, 19, 1C-1F --> execute
3775 // 80-8F, 91-9A, 9C --> execute
3776 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
3777 handleControlChar(ch
);
3781 if (((ch
>= 0x20) && (ch
<= 0x7F))
3785 // VT220 printer --> trash bin
3786 if (((type
== DeviceType
.VT220
)
3787 || (type
== DeviceType
.XTERM
))
3788 && (printerControllerMode
== true)
3793 // Hang onto this character
3794 repCh
= mapCharacter(ch
);
3796 // Print this character
3797 printCharacter(repCh
);
3802 // 00-17, 19, 1C-1F --> execute
3804 handleControlChar(ch
);
3808 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
3809 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
3811 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
3815 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
3816 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
3827 // DECSC - Save cursor
3828 // Note this code overlaps both ANSI and VT52 mode
3833 // DECRC - Restore cursor
3834 // Note this code overlaps both ANSI and VT52 mode
3843 if (vt52Mode
== true) {
3844 // DECANM - Enter ANSI mode
3846 arrowKeyMode
= ArrowKeyMode
.VT100
;
3849 * From the VT102 docs: "You use ANSI mode to select
3850 * most terminal features; the terminal uses the same
3851 * features when it switches to VT52 mode. You
3852 * cannot, however, change most of these features in
3855 * In other words, do not reset any other attributes
3856 * when switching between VT52 submode and ANSI.
3860 currentState
.g0Charset
= CharacterSet
.US
;
3861 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3863 singleshift
= Singleshift
.NONE
;
3864 currentState
.glLockshift
= LockshiftMode
.NONE
;
3865 currentState
.grLockshift
= LockshiftMode
.NONE
;
3869 // DECKPAM - Keypad application mode
3870 // Note this code overlaps both ANSI and VT52 mode
3874 // DECKPNM - Keypad numeric mode
3875 // Note this code overlaps both ANSI and VT52 mode
3882 if (vt52Mode
== true) {
3883 // Cursor up, and stop at the top without scrolling
3888 if (vt52Mode
== true) {
3889 // Cursor down, and stop at the bottom without scrolling
3890 cursorDown(1, false);
3894 if (vt52Mode
== true) {
3895 // Cursor right, and stop at the right without scrolling
3896 cursorRight(1, false);
3900 if (vt52Mode
== true) {
3901 // Cursor left, and stop at the left without scrolling
3902 cursorLeft(1, false);
3909 if (vt52Mode
== true) {
3917 if (vt52Mode
== true) {
3918 // G0 --> Special graphics
3919 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
3923 if (vt52Mode
== true) {
3925 currentState
.g0Charset
= CharacterSet
.US
;
3929 if (vt52Mode
== true) {
3931 cursorPosition(0, 0);
3933 // HTS - Horizontal tabulation set
3938 if (vt52Mode
== true) {
3939 // Reverse line feed. Same as RI.
3944 if (vt52Mode
== true) {
3945 // Erase to end of screen
3946 eraseLine(currentState
.cursorX
, width
- 1, false);
3947 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
3952 if (vt52Mode
== true) {
3953 // Erase to end of line
3954 eraseLine(currentState
.cursorX
, width
- 1, false);
3960 if (vt52Mode
== true) {
3963 // RI - Reverse index
3968 if (vt52Mode
== false) {
3970 singleshift
= Singleshift
.SS2
;
3974 if (vt52Mode
== false) {
3976 singleshift
= Singleshift
.SS3
;
3983 if ((ch
>= 0x51) && (ch
<= 0x57)) {
3999 if (vt52Mode
== true) {
4000 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4008 if (vt52Mode
== true) {
4010 // Send string directly to remote side
4011 writeRemote("\033/Z");
4014 // Send string directly to remote side
4015 writeRemote(deviceTypeResponse());
4026 // VT52 cannot get to any of these other states
4027 if (vt52Mode
== true) {
4032 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4039 // RIS - Reset to initial state
4041 // Do I clear screen too? I think so...
4042 eraseScreen(0, 0, height
- 1, width
- 1, false);
4043 cursorPosition(0, 0);
4057 if ((type
== DeviceType
.VT220
)
4058 || (type
== DeviceType
.XTERM
)) {
4060 // VT220 lockshift G2 into GL
4061 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4066 if ((type
== DeviceType
.VT220
)
4067 || (type
== DeviceType
.XTERM
)) {
4069 // VT220 lockshift G3 into GL
4070 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4088 if ((type
== DeviceType
.VT220
)
4089 || (type
== DeviceType
.XTERM
)) {
4091 // VT220 lockshift G3 into GR
4092 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4097 if ((type
== DeviceType
.VT220
)
4098 || (type
== DeviceType
.XTERM
)) {
4100 // VT220 lockshift G2 into GR
4101 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4107 if ((type
== DeviceType
.VT220
)
4108 || (type
== DeviceType
.XTERM
)) {
4110 // VT220 lockshift G1 into GR
4111 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4121 // 0x5B goes to CSI_ENTRY
4123 scanState
= ScanState
.CSI_ENTRY
;
4126 // 0x5D goes to OSC_STRING
4128 scanState
= ScanState
.OSC_STRING
;
4131 // 0x50 goes to DCS_ENTRY
4133 scanState
= ScanState
.DCS_ENTRY
;
4136 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4137 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4138 scanState
= ScanState
.SOSPMAPC_STRING
;
4143 case ESCAPE_INTERMEDIATE
:
4144 // 00-17, 19, 1C-1F --> execute
4146 handleControlChar(ch
);
4149 // 20-2F --> collect
4150 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4154 // 30-7E --> dispatch, then switch to GROUND
4155 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4158 if ((collectBuffer
.length() == 1)
4159 && (collectBuffer
.charAt(0) == '(')) {
4160 // G0 --> Special graphics
4161 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4163 if ((collectBuffer
.length() == 1)
4164 && (collectBuffer
.charAt(0) == ')')) {
4165 // G1 --> Special graphics
4166 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4168 if ((type
== DeviceType
.VT220
)
4169 || (type
== DeviceType
.XTERM
)) {
4171 if ((collectBuffer
.length() == 1)
4172 && (collectBuffer
.charAt(0) == '*')) {
4173 // G2 --> Special graphics
4174 currentState
.g2Charset
= CharacterSet
.DRAWING
;
4176 if ((collectBuffer
.length() == 1)
4177 && (collectBuffer
.charAt(0) == '+')) {
4178 // G3 --> Special graphics
4179 currentState
.g3Charset
= CharacterSet
.DRAWING
;
4184 if ((collectBuffer
.length() == 1)
4185 && (collectBuffer
.charAt(0) == '(')) {
4186 // G0 --> Alternate character ROM standard character set
4187 currentState
.g0Charset
= CharacterSet
.ROM
;
4189 if ((collectBuffer
.length() == 1)
4190 && (collectBuffer
.charAt(0) == ')')) {
4191 // G1 --> Alternate character ROM standard character set
4192 currentState
.g1Charset
= CharacterSet
.ROM
;
4196 if ((collectBuffer
.length() == 1)
4197 && (collectBuffer
.charAt(0) == '(')) {
4198 // G0 --> Alternate character ROM special graphics
4199 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4201 if ((collectBuffer
.length() == 1)
4202 && (collectBuffer
.charAt(0) == ')')) {
4203 // G1 --> Alternate character ROM special graphics
4204 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4208 if ((collectBuffer
.length() == 1)
4209 && (collectBuffer
.charAt(0) == '#')) {
4210 // DECDHL - Double-height line (top half)
4215 if ((collectBuffer
.length() == 1)
4216 && (collectBuffer
.charAt(0) == '#')) {
4217 // DECDHL - Double-height line (bottom half)
4220 if ((type
== DeviceType
.VT220
)
4221 || (type
== DeviceType
.XTERM
)) {
4223 if ((collectBuffer
.length() == 1)
4224 && (collectBuffer
.charAt(0) == '(')) {
4226 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4228 if ((collectBuffer
.length() == 1)
4229 && (collectBuffer
.charAt(0) == ')')) {
4231 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4233 if ((collectBuffer
.length() == 1)
4234 && (collectBuffer
.charAt(0) == '*')) {
4236 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4238 if ((collectBuffer
.length() == 1)
4239 && (collectBuffer
.charAt(0) == '+')) {
4241 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4246 if ((collectBuffer
.length() == 1)
4247 && (collectBuffer
.charAt(0) == '#')) {
4248 // DECSWL - Single-width line
4251 if ((type
== DeviceType
.VT220
)
4252 || (type
== DeviceType
.XTERM
)) {
4254 if ((collectBuffer
.length() == 1)
4255 && (collectBuffer
.charAt(0) == '(')) {
4257 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4259 if ((collectBuffer
.length() == 1)
4260 && (collectBuffer
.charAt(0) == ')')) {
4262 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4264 if ((collectBuffer
.length() == 1)
4265 && (collectBuffer
.charAt(0) == '*')) {
4267 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4269 if ((collectBuffer
.length() == 1)
4270 && (collectBuffer
.charAt(0) == '+')) {
4272 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4277 if ((collectBuffer
.length() == 1)
4278 && (collectBuffer
.charAt(0) == '#')) {
4279 // DECDWL - Double-width line
4282 if ((type
== DeviceType
.VT220
)
4283 || (type
== DeviceType
.XTERM
)) {
4285 if ((collectBuffer
.length() == 1)
4286 && (collectBuffer
.charAt(0) == '(')) {
4288 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4290 if ((collectBuffer
.length() == 1)
4291 && (collectBuffer
.charAt(0) == ')')) {
4293 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4295 if ((collectBuffer
.length() == 1)
4296 && (collectBuffer
.charAt(0) == '*')) {
4298 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4300 if ((collectBuffer
.length() == 1)
4301 && (collectBuffer
.charAt(0) == '+')) {
4303 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4308 if ((type
== DeviceType
.VT220
)
4309 || (type
== DeviceType
.XTERM
)) {
4311 if ((collectBuffer
.length() == 1)
4312 && (collectBuffer
.charAt(0) == '(')) {
4314 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4316 if ((collectBuffer
.length() == 1)
4317 && (collectBuffer
.charAt(0) == ')')) {
4319 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4321 if ((collectBuffer
.length() == 1)
4322 && (collectBuffer
.charAt(0) == '*')) {
4324 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4326 if ((collectBuffer
.length() == 1)
4327 && (collectBuffer
.charAt(0) == '+')) {
4329 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4334 if ((collectBuffer
.length() == 1)
4335 && (collectBuffer
.charAt(0) == '#')) {
4336 // DECALN - Screen alignment display
4345 if ((type
== DeviceType
.VT220
)
4346 || (type
== DeviceType
.XTERM
)) {
4348 if ((collectBuffer
.length() == 1)
4349 && (collectBuffer
.charAt(0) == '(')) {
4350 // G0 --> DEC_SUPPLEMENTAL
4351 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4353 if ((collectBuffer
.length() == 1)
4354 && (collectBuffer
.charAt(0) == ')')) {
4355 // G1 --> DEC_SUPPLEMENTAL
4356 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4358 if ((collectBuffer
.length() == 1)
4359 && (collectBuffer
.charAt(0) == '*')) {
4360 // G2 --> DEC_SUPPLEMENTAL
4361 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4363 if ((collectBuffer
.length() == 1)
4364 && (collectBuffer
.charAt(0) == '+')) {
4365 // G3 --> DEC_SUPPLEMENTAL
4366 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4371 if ((type
== DeviceType
.VT220
)
4372 || (type
== DeviceType
.XTERM
)) {
4374 if ((collectBuffer
.length() == 1)
4375 && (collectBuffer
.charAt(0) == '(')) {
4377 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4379 if ((collectBuffer
.length() == 1)
4380 && (collectBuffer
.charAt(0) == ')')) {
4382 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4384 if ((collectBuffer
.length() == 1)
4385 && (collectBuffer
.charAt(0) == '*')) {
4387 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4389 if ((collectBuffer
.length() == 1)
4390 && (collectBuffer
.charAt(0) == '+')) {
4392 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4401 if ((collectBuffer
.length() == 1)
4402 && (collectBuffer
.charAt(0) == '(')) {
4403 // G0 --> United Kingdom set
4404 currentState
.g0Charset
= CharacterSet
.UK
;
4406 if ((collectBuffer
.length() == 1)
4407 && (collectBuffer
.charAt(0) == ')')) {
4408 // G1 --> United Kingdom set
4409 currentState
.g1Charset
= CharacterSet
.UK
;
4411 if ((type
== DeviceType
.VT220
)
4412 || (type
== DeviceType
.XTERM
)) {
4414 if ((collectBuffer
.length() == 1)
4415 && (collectBuffer
.charAt(0) == '*')) {
4416 // G2 --> United Kingdom set
4417 currentState
.g2Charset
= CharacterSet
.UK
;
4419 if ((collectBuffer
.length() == 1)
4420 && (collectBuffer
.charAt(0) == '+')) {
4421 // G3 --> United Kingdom set
4422 currentState
.g3Charset
= CharacterSet
.UK
;
4427 if ((collectBuffer
.length() == 1)
4428 && (collectBuffer
.charAt(0) == '(')) {
4430 currentState
.g0Charset
= CharacterSet
.US
;
4432 if ((collectBuffer
.length() == 1)
4433 && (collectBuffer
.charAt(0) == ')')) {
4435 currentState
.g1Charset
= CharacterSet
.US
;
4437 if ((type
== DeviceType
.VT220
)
4438 || (type
== DeviceType
.XTERM
)) {
4440 if ((collectBuffer
.length() == 1)
4441 && (collectBuffer
.charAt(0) == '*')) {
4443 currentState
.g2Charset
= CharacterSet
.US
;
4445 if ((collectBuffer
.length() == 1)
4446 && (collectBuffer
.charAt(0) == '+')) {
4448 currentState
.g3Charset
= CharacterSet
.US
;
4453 if ((type
== DeviceType
.VT220
)
4454 || (type
== DeviceType
.XTERM
)) {
4456 if ((collectBuffer
.length() == 1)
4457 && (collectBuffer
.charAt(0) == '(')) {
4459 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4461 if ((collectBuffer
.length() == 1)
4462 && (collectBuffer
.charAt(0) == ')')) {
4464 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4466 if ((collectBuffer
.length() == 1)
4467 && (collectBuffer
.charAt(0) == '*')) {
4469 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4471 if ((collectBuffer
.length() == 1)
4472 && (collectBuffer
.charAt(0) == '+')) {
4474 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4481 if ((type
== DeviceType
.VT220
)
4482 || (type
== DeviceType
.XTERM
)) {
4484 if ((collectBuffer
.length() == 1)
4485 && (collectBuffer
.charAt(0) == '(')) {
4487 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4489 if ((collectBuffer
.length() == 1)
4490 && (collectBuffer
.charAt(0) == ')')) {
4492 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4494 if ((collectBuffer
.length() == 1)
4495 && (collectBuffer
.charAt(0) == '*')) {
4497 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4499 if ((collectBuffer
.length() == 1)
4500 && (collectBuffer
.charAt(0) == '+')) {
4502 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4507 if ((type
== DeviceType
.VT220
)
4508 || (type
== DeviceType
.XTERM
)) {
4510 if ((collectBuffer
.length() == 1)
4511 && (collectBuffer
.charAt(0) == ' ')) {
4518 if ((type
== DeviceType
.VT220
)
4519 || (type
== DeviceType
.XTERM
)) {
4521 if ((collectBuffer
.length() == 1)
4522 && (collectBuffer
.charAt(0) == ' ')) {
4529 if ((type
== DeviceType
.VT220
)
4530 || (type
== DeviceType
.XTERM
)) {
4532 if ((collectBuffer
.length() == 1)
4533 && (collectBuffer
.charAt(0) == '(')) {
4535 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4537 if ((collectBuffer
.length() == 1)
4538 && (collectBuffer
.charAt(0) == ')')) {
4540 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4542 if ((collectBuffer
.length() == 1)
4543 && (collectBuffer
.charAt(0) == '*')) {
4545 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4547 if ((collectBuffer
.length() == 1)
4548 && (collectBuffer
.charAt(0) == '+')) {
4550 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4558 if ((type
== DeviceType
.VT220
)
4559 || (type
== DeviceType
.XTERM
)) {
4561 if ((collectBuffer
.length() == 1)
4562 && (collectBuffer
.charAt(0) == '(')) {
4564 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4566 if ((collectBuffer
.length() == 1)
4567 && (collectBuffer
.charAt(0) == ')')) {
4569 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4571 if ((collectBuffer
.length() == 1)
4572 && (collectBuffer
.charAt(0) == '*')) {
4574 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4576 if ((collectBuffer
.length() == 1)
4577 && (collectBuffer
.charAt(0) == '+')) {
4579 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4590 if ((type
== DeviceType
.VT220
)
4591 || (type
== DeviceType
.XTERM
)) {
4593 if ((collectBuffer
.length() == 1)
4594 && (collectBuffer
.charAt(0) == '(')) {
4596 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4598 if ((collectBuffer
.length() == 1)
4599 && (collectBuffer
.charAt(0) == ')')) {
4601 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4603 if ((collectBuffer
.length() == 1)
4604 && (collectBuffer
.charAt(0) == '*')) {
4606 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4608 if ((collectBuffer
.length() == 1)
4609 && (collectBuffer
.charAt(0) == '+')) {
4611 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4616 if ((type
== DeviceType
.VT220
)
4617 || (type
== DeviceType
.XTERM
)) {
4619 if ((collectBuffer
.length() == 1)
4620 && (collectBuffer
.charAt(0) == '(')) {
4622 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4624 if ((collectBuffer
.length() == 1)
4625 && (collectBuffer
.charAt(0) == ')')) {
4627 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4629 if ((collectBuffer
.length() == 1)
4630 && (collectBuffer
.charAt(0) == '*')) {
4632 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
4634 if ((collectBuffer
.length() == 1)
4635 && (collectBuffer
.charAt(0) == '+')) {
4637 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
4649 if ((type
== DeviceType
.VT220
)
4650 || (type
== DeviceType
.XTERM
)) {
4652 if ((collectBuffer
.length() == 1)
4653 && (collectBuffer
.charAt(0) == '(')) {
4655 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
4657 if ((collectBuffer
.length() == 1)
4658 && (collectBuffer
.charAt(0) == ')')) {
4660 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
4662 if ((collectBuffer
.length() == 1)
4663 && (collectBuffer
.charAt(0) == '*')) {
4665 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
4667 if ((collectBuffer
.length() == 1)
4668 && (collectBuffer
.charAt(0) == '+')) {
4670 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
4675 if ((type
== DeviceType
.VT220
)
4676 || (type
== DeviceType
.XTERM
)) {
4678 if ((collectBuffer
.length() == 1)
4679 && (collectBuffer
.charAt(0) == '(')) {
4681 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
4683 if ((collectBuffer
.length() == 1)
4684 && (collectBuffer
.charAt(0) == ')')) {
4686 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
4688 if ((collectBuffer
.length() == 1)
4689 && (collectBuffer
.charAt(0) == '*')) {
4691 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
4693 if ((collectBuffer
.length() == 1)
4694 && (collectBuffer
.charAt(0) == '+')) {
4696 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
4743 // 0x9C goes to GROUND
4751 // 00-17, 19, 1C-1F --> execute
4753 handleControlChar(ch
);
4756 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
4757 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4759 scanState
= ScanState
.CSI_INTERMEDIATE
;
4762 // 30-39, 3B --> param, then switch to CSI_PARAM
4763 if ((ch
>= '0') && (ch
<= '9')) {
4765 scanState
= ScanState
.CSI_PARAM
;
4769 scanState
= ScanState
.CSI_PARAM
;
4772 // 3C-3F --> collect, then switch to CSI_PARAM
4773 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
4775 scanState
= ScanState
.CSI_PARAM
;
4778 // 40-7E --> dispatch, then switch to GROUND
4779 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
4782 // ICH - Insert character
4790 // CUD - Cursor down
4794 // CUF - Cursor forward
4798 // CUB - Cursor backward
4802 // CNL - Cursor down and to column 1
4803 if (type
== DeviceType
.XTERM
) {
4808 // CPL - Cursor up and to column 1
4809 if (type
== DeviceType
.XTERM
) {
4814 // CHA - Cursor to column # in current row
4815 if (type
== DeviceType
.XTERM
) {
4820 // CUP - Cursor position
4824 // CHT - Cursor forward X tab stops (default 1)
4825 if (type
== DeviceType
.XTERM
) {
4830 // ED - Erase in display
4834 // EL - Erase in line
4849 // DCH - Delete character
4856 // Scroll up X lines (default 1)
4857 if (type
== DeviceType
.XTERM
) {
4862 // Scroll down X lines (default 1)
4863 if (type
== DeviceType
.XTERM
) {
4872 if ((type
== DeviceType
.VT220
)
4873 || (type
== DeviceType
.XTERM
)) {
4875 // ECH - Erase character
4882 // CBT - Cursor backward X tab stops (default 1)
4883 if (type
== DeviceType
.XTERM
) {
4894 // HPA - Cursor to column # in current row. Same as CHA
4895 if (type
== DeviceType
.XTERM
) {
4900 // HPR - Cursor right. Same as CUF
4901 if (type
== DeviceType
.XTERM
) {
4906 // REP - Repeat last char X times
4907 if (type
== DeviceType
.XTERM
) {
4912 // DA - Device attributes
4916 // VPA - Cursor to row, current column.
4917 if (type
== DeviceType
.XTERM
) {
4922 // VPR - Cursor down. Same as CUD
4923 if (type
== DeviceType
.XTERM
) {
4928 // HVP - Horizontal and vertical position
4932 // TBC - Tabulation clear
4936 // Sets an ANSI or DEC private toggle
4940 if ((type
== DeviceType
.VT220
)
4941 || (type
== DeviceType
.XTERM
)) {
4943 // Printer functions
4951 // Sets an ANSI or DEC private toggle
4955 // SGR - Select graphics rendition
4959 // DSR - Device status report
4966 // DECLL - Load leds
4970 // DECSTBM - Set top and bottom margins
4974 // Save cursor (ANSI.SYS)
4975 if (type
== DeviceType
.XTERM
) {
4976 savedState
.cursorX
= currentState
.cursorX
;
4977 savedState
.cursorY
= currentState
.cursorY
;
4983 // Restore cursor (ANSI.SYS)
4984 if (type
== DeviceType
.XTERM
) {
4985 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
4992 // DECREQTPARM - Request terminal parameters
5008 // 0x9C goes to GROUND
5013 // 0x3A goes to CSI_IGNORE
5015 scanState
= ScanState
.CSI_IGNORE
;
5020 // 00-17, 19, 1C-1F --> execute
5022 handleControlChar(ch
);
5025 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5026 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5028 scanState
= ScanState
.CSI_INTERMEDIATE
;
5031 // 30-39, 3B --> param
5032 if ((ch
>= '0') && (ch
<= '9')) {
5039 // 0x3A goes to CSI_IGNORE
5041 scanState
= ScanState
.CSI_IGNORE
;
5043 // 0x3C-3F goes to CSI_IGNORE
5044 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5045 scanState
= ScanState
.CSI_IGNORE
;
5048 // 40-7E --> dispatch, then switch to GROUND
5049 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5052 // ICH - Insert character
5060 // CUD - Cursor down
5064 // CUF - Cursor forward
5068 // CUB - Cursor backward
5072 // CNL - Cursor down and to column 1
5073 if (type
== DeviceType
.XTERM
) {
5078 // CPL - Cursor up and to column 1
5079 if (type
== DeviceType
.XTERM
) {
5084 // CHA - Cursor to column # in current row
5085 if (type
== DeviceType
.XTERM
) {
5090 // CUP - Cursor position
5094 // CHT - Cursor forward X tab stops (default 1)
5095 if (type
== DeviceType
.XTERM
) {
5100 // ED - Erase in display
5104 // EL - Erase in line
5119 // DCH - Delete character
5126 // Scroll up X lines (default 1)
5127 if (type
== DeviceType
.XTERM
) {
5132 // Scroll down X lines (default 1)
5133 if (type
== DeviceType
.XTERM
) {
5142 if ((type
== DeviceType
.VT220
)
5143 || (type
== DeviceType
.XTERM
)) {
5145 // ECH - Erase character
5152 // CBT - Cursor backward X tab stops (default 1)
5153 if (type
== DeviceType
.XTERM
) {
5164 // HPA - Cursor to column # in current row. Same as CHA
5165 if (type
== DeviceType
.XTERM
) {
5170 // HPR - Cursor right. Same as CUF
5171 if (type
== DeviceType
.XTERM
) {
5176 // REP - Repeat last char X times
5177 if (type
== DeviceType
.XTERM
) {
5182 // DA - Device attributes
5186 // VPA - Cursor to row, current column.
5187 if (type
== DeviceType
.XTERM
) {
5192 // VPR - Cursor down. Same as CUD
5193 if (type
== DeviceType
.XTERM
) {
5198 // HVP - Horizontal and vertical position
5202 // TBC - Tabulation clear
5206 // Sets an ANSI or DEC private toggle
5210 if ((type
== DeviceType
.VT220
)
5211 || (type
== DeviceType
.XTERM
)) {
5213 // Printer functions
5221 // Sets an ANSI or DEC private toggle
5225 // SGR - Select graphics rendition
5229 // DSR - Device status report
5236 // DECLL - Load leds
5240 // DECSTBM - Set top and bottom margins
5250 // DECREQTPARM - Request terminal parameters
5267 case CSI_INTERMEDIATE
:
5268 // 00-17, 19, 1C-1F --> execute
5270 handleControlChar(ch
);
5273 // 20-2F --> collect
5274 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5278 // 0x30-3F goes to CSI_IGNORE
5279 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5280 scanState
= ScanState
.CSI_IGNORE
;
5283 // 40-7E --> dispatch, then switch to GROUND
5284 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5336 if (((type
== DeviceType
.VT220
)
5337 || (type
== DeviceType
.XTERM
))
5338 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5340 // DECSCL - compatibility level
5343 if ((type
== DeviceType
.XTERM
)
5344 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
5346 // DECSTR - Soft terminal reset
5351 if (((type
== DeviceType
.VT220
)
5352 || (type
== DeviceType
.XTERM
))
5353 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5381 // 00-17, 19, 1C-1F --> execute
5383 handleControlChar(ch
);
5386 // 20-2F --> collect
5387 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5391 // 40-7E --> ignore, then switch to GROUND
5392 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5396 // 20-3F, 7F --> ignore
5402 // 0x9C goes to GROUND
5407 // 0x1B 0x5C goes to GROUND
5412 if ((collectBuffer
.length() > 0)
5413 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5419 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5420 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5422 scanState
= ScanState
.DCS_INTERMEDIATE
;
5425 // 30-39, 3B --> param, then switch to DCS_PARAM
5426 if ((ch
>= '0') && (ch
<= '9')) {
5428 scanState
= ScanState
.DCS_PARAM
;
5432 scanState
= ScanState
.DCS_PARAM
;
5435 // 3C-3F --> collect, then switch to DCS_PARAM
5436 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5438 scanState
= ScanState
.DCS_PARAM
;
5441 // 00-17, 19, 1C-1F, 7F --> ignore
5443 // 0x3A goes to DCS_IGNORE
5445 scanState
= ScanState
.DCS_IGNORE
;
5448 // 0x40-7E goes to DCS_PASSTHROUGH
5449 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5450 scanState
= ScanState
.DCS_PASSTHROUGH
;
5454 case DCS_INTERMEDIATE
:
5456 // 0x9C goes to GROUND
5461 // 0x1B 0x5C goes to GROUND
5466 if ((collectBuffer
.length() > 0) &&
5467 (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5473 // 0x30-3F goes to DCS_IGNORE
5474 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5475 scanState
= ScanState
.DCS_IGNORE
;
5478 // 0x40-7E goes to DCS_PASSTHROUGH
5479 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5480 scanState
= ScanState
.DCS_PASSTHROUGH
;
5483 // 00-17, 19, 1C-1F, 7F --> ignore
5488 // 0x9C goes to GROUND
5493 // 0x1B 0x5C goes to GROUND
5498 if ((collectBuffer
.length() > 0) &&
5499 (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5505 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5506 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5508 scanState
= ScanState
.DCS_INTERMEDIATE
;
5511 // 30-39, 3B --> param
5512 if ((ch
>= '0') && (ch
<= '9')) {
5519 // 00-17, 19, 1C-1F, 7F --> ignore
5521 // 0x3A, 3C-3F goes to DCS_IGNORE
5523 scanState
= ScanState
.DCS_IGNORE
;
5525 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5526 scanState
= ScanState
.DCS_IGNORE
;
5529 // 0x40-7E goes to DCS_PASSTHROUGH
5530 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5531 scanState
= ScanState
.DCS_PASSTHROUGH
;
5535 case DCS_PASSTHROUGH
:
5536 // 0x9C goes to GROUND
5541 // 0x1B 0x5C goes to GROUND
5546 if ((collectBuffer
.length() > 0)
5547 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5553 // 00-17, 19, 1C-1F, 20-7E --> put
5561 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5564 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5573 // 00-17, 19, 1C-1F, 20-7F --> ignore
5575 // 0x9C goes to GROUND
5582 case SOSPMAPC_STRING
:
5583 // 00-17, 19, 1C-1F, 20-7F --> ignore
5585 // 0x9C goes to GROUND
5593 // Special case for Xterm: OSC can pass control characters
5594 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5598 // 00-17, 19, 1C-1F --> ignore
5600 // 20-7F --> osc_put
5601 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5605 // 0x9C goes to GROUND
5612 case VT52_DIRECT_CURSOR_ADDRESS
:
5613 // This is a special case for the VT52 sequence "ESC Y l c"
5614 if (collectBuffer
.length() == 0) {
5616 } else if (collectBuffer
.length() == 1) {
5617 // We've got the two characters, one in the buffer and the
5619 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
5628 * Expose current cursor X to outside world.
5630 * @return current cursor X
5632 public final int getCursorX() {
5633 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
5634 return currentState
.cursorX
* 2;
5636 return currentState
.cursorX
;
5640 * Expose current cursor Y to outside world.
5642 * @return current cursor Y
5644 public final int getCursorY() {
5645 return currentState
.cursorY
;
5649 * Read function runs on a separate thread.
5652 boolean utf8
= false;
5653 boolean done
= false;
5655 if (type
== DeviceType
.XTERM
) {
5659 // available() will often return > 1, so we need to read in chunks to
5661 char [] readBufferUTF8
= null;
5662 byte [] readBuffer
= null;
5664 readBufferUTF8
= new char[128];
5666 readBuffer
= new byte[128];
5669 while (!done
&& !stopReaderThread
) {
5671 int n
= inputStream
.available();
5672 // System.err.printf("available() %d\n", n); System.err.flush();
5674 if (readBufferUTF8
.length
< n
) {
5675 // The buffer wasn't big enough, make it huger
5676 int newSizeHalf
= Math
.max(readBufferUTF8
.length
, n
);
5678 readBufferUTF8
= new char[newSizeHalf
* 2];
5681 if (readBuffer
.length
< n
) {
5682 // The buffer wasn't big enough, make it huger
5683 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
5684 readBuffer
= new byte[newSizeHalf
* 2];
5690 rc
= input
.read(readBufferUTF8
, 0,
5691 readBufferUTF8
.length
);
5693 rc
= inputStream
.read(readBuffer
, 0,
5696 // System.err.printf("read() %d\n", rc); System.err.flush();
5701 for (int i
= 0; i
< rc
; i
++) {
5704 ch
= readBufferUTF8
[i
];
5708 // Don't step on UI events
5709 synchronized (this) {
5714 // System.err.println("end while loop"); System.err.flush();
5715 } catch (IOException e
) {
5716 e
.printStackTrace();
5719 } // while ((done == false) && (stopReaderThread == false))
5721 // Let the rest of the world know that I am done.
5722 stopReaderThread
= true;
5724 // System.err.println("*** run() exiting..."); System.err.flush();