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
.bits
.Color
;
47 import jexer
.bits
.Cell
;
48 import jexer
.bits
.CellAttributes
;
49 import jexer
.TKeypress
;
50 import static jexer
.TKeypress
.*;
53 * This implements a complex ANSI ECMA-48/ISO 6429/ANSI X3.64 type consoles,
54 * including a scrollback buffer.
57 * It currently implements VT100, VT102, VT220, and XTERM with the following
61 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
62 * from VT100 are not supported.
65 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
66 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
67 * because DECSCL is not supported, it will fail the last part of the
68 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
71 * - Numeric/application keys from the number pad are not supported because
72 * they are not exposed from the TKeypress API.
75 * - VT52 HOLD SCREEN mode is not supported.
78 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
79 * numerators) are not rendered correctly.
82 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
84 public class ECMA48
implements Runnable
{
87 * The emulator can emulate several kinds of terminals.
89 public enum DeviceType
{
91 * DEC VT100 but also including the three VT102 functions.
112 * Return the proper primary Device Attributes string.
114 * @return string to send to remote side that is appropriate for the
117 private String
deviceTypeResponse() {
120 // "I am a VT100 with advanced video option" (often VT102)
128 // "I am a VT220" - 7 bit version
130 return "\033[?62;1;6c";
132 // "I am a VT220" - 8 bit version
133 return "\u009b?62;1;6c";
135 // "I am a VT100 with advanced video option" (often VT102)
138 throw new IllegalArgumentException("Invalid device type: " + type
);
143 * Return the proper TERM environment variable for this device type.
145 * @return "TERM=vt100", "TERM=xterm", etc.
147 public String
deviceTypeTerm() {
162 throw new IllegalArgumentException("Invalid device type: " + type
);
167 * Return the proper LANG for this device type. Only XTERM devices know
168 * about UTF-8, the others are defined by their standard to be either
169 * 7-bit or 8-bit characters only.
171 * @param baseLang a base language without UTF-8 flag such as "C" or
173 * @return "LANG=en_US", "LANG=en_US.UTF-8", etc.
175 public String
deviceTypeLang(final String baseLang
) {
179 return "LANG=" + baseLang
;
182 return "LANG=" + baseLang
;
185 return "LANG=" + baseLang
;
188 return "LANG=" + baseLang
+ ".UTF-8";
191 throw new IllegalArgumentException("Invalid device type: " + type
);
196 * Write a string directly to the remote side.
198 * @param str string to send
200 private void writeRemote(final String str
) {
201 if (stopReaderThread
) {
202 // Reader hit EOF, bail out now.
207 // System.err.printf("writeRemote() '%s'\n", str);
213 if (outputStream
== null) {
217 for (int i
= 0; i
< str
.length(); i
++) {
218 outputStream
.write(str
.charAt(i
));
220 outputStream
.flush();
221 } catch (IOException e
) {
227 if (output
== null) {
233 } catch (IOException e
) {
239 throw new IllegalArgumentException("Invalid device type: " + type
);
244 * Close the input and output streams and stop the reader thread. Note
245 * that it is safe to call this multiple times.
247 public final void close() {
249 // Synchronize so we don't stomp on the reader thread.
250 synchronized (this) {
252 // Tell the reader thread to stop looking at input. It will
253 // close the input stream as it exits.
254 if (stopReaderThread
== false) {
255 stopReaderThread
= true;
258 } catch (InterruptedException e
) {
263 // Close the output stream.
268 if (outputStream
!= null) {
270 outputStream
.close();
271 } catch (IOException e
) {
278 if (output
!= null) {
281 } catch (IOException e
) {
288 throw new IllegalArgumentException("Invalid device type: "
291 } // synchronized (this)
295 * When true, the reader thread is expected to exit.
297 private boolean stopReaderThread
= false;
302 private Thread readerThread
= null;
305 * See if the reader thread is still running.
307 * @return if true, we are still connected to / reading from the remote
310 public final boolean isReading() {
311 return (!stopReaderThread
);
315 * The type of emulator to be.
317 private DeviceType type
= DeviceType
.VT102
;
320 * Obtain a new blank display line for an external user
321 * (e.g. TTerminalWindow).
323 * @return new blank line
325 public final DisplayLine
getBlankDisplayLine() {
326 return new DisplayLine(currentState
.attr
);
330 * The scrollback buffer characters + attributes.
332 private List
<DisplayLine
> scrollback
;
335 * Get the scrollback buffer.
337 * @return the scrollback buffer
339 public final List
<DisplayLine
> getScrollbackBuffer() {
344 * The raw display buffer characters + attributes.
346 private List
<DisplayLine
> display
;
349 * Get the display buffer.
351 * @return the display buffer
353 public final List
<DisplayLine
> getDisplayBuffer() {
358 * The terminal's input. For type == XTERM, this is an InputStreamReader
359 * with UTF-8 encoding.
361 private Reader input
;
364 * The terminal's raw InputStream. This is used for type != XTERM.
366 private volatile InputStream inputStream
;
369 * The terminal's output. For type == XTERM, this wraps an
370 * OutputStreamWriter with UTF-8 encoding.
372 private Writer output
;
375 * The terminal's raw OutputStream. This is used for type != XTERM.
377 private OutputStream outputStream
;
380 * Parser character scan states.
397 VT52_DIRECT_CURSOR_ADDRESS
401 * Current scanning state.
403 private ScanState scanState
;
406 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
407 * can't really use it in keypress() because we do not see number pad
408 * events from TKeypress.
410 private enum KeypadMode
{
416 * Arrow keys can emit three different sequences (DECCKM or VT52
419 private enum ArrowKeyMode
{
426 * Available character sets for GL, GR, G0, G1, G2, G3.
428 private enum CharacterSet
{
449 * Single-shift states used by the C1 control characters SS2 (0x8E) and
452 private enum Singleshift
{
459 * VT220+ lockshift states.
461 private enum LockshiftMode
{
471 * Physical display width. We start at 80x24, but the user can resize us
477 * Get the display width.
479 * @return the width (usually 80 or 132)
481 public final int getWidth() {
486 * Physical display height. We start at 80x24, but the user can resize
492 * Get the display height.
494 * @return the height (usually 24)
496 public final int getHeight() {
501 * Top margin of the scrolling region.
503 private int scrollRegionTop
;
506 * Bottom margin of the scrolling region.
508 private int scrollRegionBottom
;
511 * Right margin column number. This can be selected by the remote side
512 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
514 private int rightMargin
;
517 * Last character printed.
522 * VT100-style line wrapping: a character is placed in column 80 (or
523 * 132), but the line does NOT wrap until another character is written to
524 * column 1 of the next line, after which the cursor moves to column 2.
526 private boolean wrapLineFlag
;
529 * VT220 single shift flag.
531 private Singleshift singleshift
= Singleshift
.NONE
;
534 * true = insert characters, false = overwrite.
536 private boolean insertMode
= false;
539 * VT52 mode as selected by DECANM. True means VT52, false means
540 * ANSI. Default is ANSI.
542 private boolean vt52Mode
= false;
545 * Visible cursor (DECTCEM).
547 private boolean cursorVisible
= true;
550 * Get visible cursor flag.
552 * @return if true, the cursor is visible
554 public final boolean visibleCursor() {
555 return cursorVisible
;
559 * Screen title as set by the xterm OSC sequence. Lots of applications
560 * send a screenTitle regardless of whether it is an xterm client or not.
562 private String screenTitle
= "";
565 * Get the screen title as set by the xterm OSC sequence. Lots of
566 * applications send a screenTitle regardless of whether it is an xterm
569 * @return screen title
571 public final String
getScreenTitle() {
576 * Array of flags that have come in, e.g. '?' (DEC private mode), '=',
579 private List
<Character
> csiFlags
;
582 * Parameter characters being collected.
584 private List
<Integer
> csiParams
;
587 * Non-csi collect buffer.
589 private List
<Character
> collectBuffer
;
592 * When true, use the G1 character set.
594 private boolean shiftOut
= false;
597 * Horizontal tab stop locations.
599 private List
<Integer
> tabStops
;
602 * S8C1T. True means 8bit controls, false means 7bit controls.
604 private boolean s8c1t
= false;
607 * Printer mode. True means send all output to printer, which discards
610 private boolean printerControllerMode
= false;
613 * LMN line mode. If true, linefeed() puts the cursor on the first
614 * column of the next line. If false, linefeed() puts the cursor one
615 * line down on the current line. The default is false.
617 private boolean newLineMode
= false;
620 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
622 private ArrowKeyMode arrowKeyMode
;
625 * Whether number pad keys send VT100 or VT52, application or numeric
628 private KeypadMode keypadMode
;
631 * When true, the terminal is in 132-column mode (DECCOLM).
633 private boolean columns132
= false;
636 * true = reverse video. Set by DECSCNM.
638 private boolean reverseVideo
= false;
641 * false = echo characters locally.
643 private boolean fullDuplex
= true;
646 * DECSC/DECRC save/restore a subset of the total state. This class
647 * encapsulates those specific flags/modes.
649 private class SaveableState
{
652 * When true, cursor positions are relative to the scrolling region.
654 public boolean originMode
= false;
657 * The current editing X position.
659 public int cursorX
= 0;
662 * The current editing Y position.
664 public int cursorY
= 0;
667 * Which character set is currently selected in G0.
669 public CharacterSet g0Charset
= CharacterSet
.US
;
672 * Which character set is currently selected in G1.
674 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
677 * Which character set is currently selected in G2.
679 public CharacterSet g2Charset
= CharacterSet
.US
;
682 * Which character set is currently selected in G3.
684 public CharacterSet g3Charset
= CharacterSet
.US
;
687 * Which character set is currently selected in GR.
689 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
692 * The current drawing attributes.
694 public CellAttributes attr
;
699 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
704 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
709 public boolean lineWrap
= true;
714 public void reset() {
718 g0Charset
= CharacterSet
.US
;
719 g1Charset
= CharacterSet
.DRAWING
;
720 g2Charset
= CharacterSet
.US
;
721 g3Charset
= CharacterSet
.US
;
722 grCharset
= CharacterSet
.DRAWING
;
723 attr
= new CellAttributes();
724 glLockshift
= LockshiftMode
.NONE
;
725 grLockshift
= LockshiftMode
.NONE
;
730 * Copy attributes from another instance.
732 * @param that the other instance to match
734 public void setTo(final SaveableState that
) {
735 this.originMode
= that
.originMode
;
736 this.cursorX
= that
.cursorX
;
737 this.cursorY
= that
.cursorY
;
738 this.g0Charset
= that
.g0Charset
;
739 this.g1Charset
= that
.g1Charset
;
740 this.g2Charset
= that
.g2Charset
;
741 this.g3Charset
= that
.g3Charset
;
742 this.grCharset
= that
.grCharset
;
743 this.attr
= new CellAttributes();
744 this.attr
.setTo(that
.attr
);
745 this.glLockshift
= that
.glLockshift
;
746 this.grLockshift
= that
.grLockshift
;
747 this.lineWrap
= that
.lineWrap
;
751 * Public constructor.
753 public SaveableState() {
759 * The current terminal state.
761 private SaveableState currentState
;
764 * The last saved terminal state.
766 private SaveableState savedState
;
769 * Clear the CSI parameters and flags.
771 private void toGround() {
774 collectBuffer
.clear();
775 scanState
= ScanState
.GROUND
;
779 * Reset the tab stops list.
781 private void resetTabStops() {
783 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
784 tabStops
.add(new Integer(i
* 8));
789 * Reset the emulation state.
791 private void reset() {
793 currentState
= new SaveableState();
794 savedState
= new SaveableState();
795 scanState
= ScanState
.GROUND
;
799 scrollRegionBottom
= height
- 1;
800 rightMargin
= width
- 1;
802 arrowKeyMode
= ArrowKeyMode
.ANSI
;
803 keypadMode
= KeypadMode
.Numeric
;
804 wrapLineFlag
= false;
812 reverseVideo
= false;
814 cursorVisible
= true;
817 singleshift
= Singleshift
.NONE
;
819 printerControllerMode
= false;
829 * Public constructor.
831 * @param type one of the DeviceType constants to select VT100, VT102,
833 * @param inputStream an InputStream connected to the remote side. For
834 * type == XTERM, inputStrem is converted to a Reader with UTF-8
836 * @param outputStream an OutputStream connected to the remote user. For
837 * type == XTERM, outputStream is converted to a Writer with UTF-8
839 * @throws UnsupportedEncodingException if an exception is thrown when
840 * creating the InputStreamReader
842 public ECMA48(final DeviceType type
, final InputStream inputStream
,
843 final OutputStream outputStream
) throws UnsupportedEncodingException
{
845 assert (inputStream
!= null);
846 assert (outputStream
!= null);
848 csiFlags
= new ArrayList
<Character
>();
849 csiParams
= new ArrayList
<Integer
>();
850 collectBuffer
= new ArrayList
<Character
>();
851 tabStops
= new ArrayList
<Integer
>();
852 scrollback
= new LinkedList
<DisplayLine
>();
853 display
= new LinkedList
<DisplayLine
>();
856 this.inputStream
= inputStream
;
857 if (type
== DeviceType
.XTERM
) {
858 this.input
= new InputStreamReader(inputStream
, "UTF-8");
859 this.output
= new OutputStreamWriter(outputStream
, "UTF-8");
860 this.outputStream
= null;
863 this.outputStream
= outputStream
;
867 for (int i
= 0; i
< height
; i
++) {
868 display
.add(new DisplayLine(currentState
.attr
));
871 // Spin up the input reader
872 readerThread
= new Thread(this);
873 readerThread
.start();
877 * Append a new line to the bottom of the display, adding lines off the
878 * top to the scrollback buffer.
880 private void newDisplayLine() {
881 // Scroll the top line off into the scrollback buffer
882 scrollback
.add(display
.get(0));
884 DisplayLine line
= new DisplayLine(currentState
.attr
);
885 line
.setReverseColor(reverseVideo
);
890 * Wraps the current line.
892 private void wrapCurrentLine() {
893 if (currentState
.cursorY
== height
- 1) {
896 if (currentState
.cursorY
< height
- 1) {
897 currentState
.cursorY
++;
899 currentState
.cursorX
= 0;
903 * Handle a carriage return.
905 private void carriageReturn() {
906 currentState
.cursorX
= 0;
907 wrapLineFlag
= false;
911 * Reverse the color of the visible display.
913 private void invertDisplayColors() {
914 for (DisplayLine line
: display
) {
915 line
.setReverseColor(!line
.isReverseColor());
922 private void linefeed() {
925 if (currentState
.cursorY
< scrollRegionBottom
) {
926 // Increment screen y
927 currentState
.cursorY
++;
930 // Screen y does not increment
933 * Two cases: either we're inside a scrolling region or not. If
934 * the scrolling region bottom is the bottom of the screen, then
935 * push the top line into the buffer. Else scroll the scrolling
938 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
940 // We're at the bottom of the scroll region, AND the scroll
941 // region is the entire screen.
947 // We're at the bottom of the scroll region, AND the scroll
948 // region is NOT the entire screen.
949 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
954 currentState
.cursorX
= 0;
956 wrapLineFlag
= false;
960 * Prints one character to the display buffer.
962 * @param ch character to display
964 private void printCharacter(final char ch
) {
965 int rightMargin
= this.rightMargin
;
967 // Check if we have double-width, and if so chop at 40/66 instead of
969 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
970 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
973 // Check the unusually-complicated line wrapping conditions...
974 if (currentState
.cursorX
== rightMargin
) {
976 if (currentState
.lineWrap
== true) {
978 * This case happens when: the cursor was already on the
979 * right margin (either through printing or by an explicit
980 * placement command), and a character was printed.
982 * The line wraps only when a new character arrives AND the
983 * cursor is already on the right margin AND has placed a
984 * character in its cell. Easier to see than to explain.
986 if (wrapLineFlag
== false) {
988 * This block marks the case that we are in the margin
989 * and the first character has been received and printed.
994 * This block marks the case that we are in the margin
995 * and the second character has been received and
998 wrapLineFlag
= false;
1002 } else if (currentState
.cursorX
<= rightMargin
) {
1004 * This is the normal case: a character came in and was printed
1005 * to the left of the right margin column.
1008 // Turn off VT100 special-case flag
1009 wrapLineFlag
= false;
1012 // "Print" the character
1013 Cell newCell
= new Cell(ch
);
1014 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1015 newCellAttributes
.setTo(currentState
.attr
);
1016 DisplayLine line
= display
.get(currentState
.cursorY
);
1017 // Insert mode special case
1018 if (insertMode
== true) {
1019 line
.insert(currentState
.cursorX
, newCell
);
1021 // Replace an existing character
1022 line
.replace(currentState
.cursorX
, newCell
);
1025 // Increment horizontal
1026 if (wrapLineFlag
== false) {
1027 currentState
.cursorX
++;
1028 if (currentState
.cursorX
> rightMargin
) {
1029 currentState
.cursorX
--;
1035 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1036 * send to the remote side.
1038 * @param keypress keypress received from the local user
1040 public void keypress(final TKeypress keypress
) {
1041 writeRemote(keypressToString(keypress
));
1045 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1047 * @param keypress keypress received from the local user
1048 * @return string to transmit to the remote side
1050 private String
keypressToString(final TKeypress keypress
) {
1052 if ((fullDuplex
== false) && (!keypress
.getIsKey())) {
1054 * If this is a control character, process it like it came from
1057 if (keypress
.getCh() < 0x20) {
1058 handleControlChar(keypress
.getCh());
1060 // Local echo for everything else
1061 printCharacter(keypress
.getCh());
1065 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1070 // Handle control characters
1071 if ((keypress
.getCtrl()) && (!keypress
.getIsKey())) {
1072 StringBuilder sb
= new StringBuilder();
1073 char ch
= keypress
.getCh();
1076 return sb
.toString();
1079 // Handle alt characters
1080 if ((keypress
.getAlt()) && (!keypress
.getIsKey())) {
1081 StringBuilder sb
= new StringBuilder("\033");
1082 char ch
= keypress
.getCh();
1084 return sb
.toString();
1087 if (keypress
.equals(kbBackspace
)) {
1100 if (keypress
.equals(kbLeft
)) {
1101 switch (arrowKeyMode
) {
1111 if (keypress
.equals(kbRight
)) {
1112 switch (arrowKeyMode
) {
1122 if (keypress
.equals(kbUp
)) {
1123 switch (arrowKeyMode
) {
1133 if (keypress
.equals(kbDown
)) {
1134 switch (arrowKeyMode
) {
1144 if (keypress
.equals(kbHome
)) {
1145 switch (arrowKeyMode
) {
1155 if (keypress
.equals(kbEnd
)) {
1156 switch (arrowKeyMode
) {
1166 if (keypress
.equals(kbF1
)) {
1174 if (keypress
.equals(kbF2
)) {
1182 if (keypress
.equals(kbF3
)) {
1190 if (keypress
.equals(kbF4
)) {
1198 if (keypress
.equals(kbF5
)) {
1211 if (keypress
.equals(kbF6
)) {
1224 if (keypress
.equals(kbF7
)) {
1237 if (keypress
.equals(kbF8
)) {
1250 if (keypress
.equals(kbF9
)) {
1263 if (keypress
.equals(kbF10
)) {
1276 if (keypress
.equals(kbF11
)) {
1280 if (keypress
.equals(kbF12
)) {
1284 if (keypress
.equals(kbShiftF1
)) {
1292 if (keypress
.equals(kbShiftF2
)) {
1300 if (keypress
.equals(kbShiftF3
)) {
1308 if (keypress
.equals(kbShiftF4
)) {
1316 if (keypress
.equals(kbShiftF5
)) {
1318 return "\033[15;2~";
1321 if (keypress
.equals(kbShiftF6
)) {
1323 return "\033[17;2~";
1326 if (keypress
.equals(kbShiftF7
)) {
1328 return "\033[18;2~";
1331 if (keypress
.equals(kbShiftF8
)) {
1333 return "\033[19;2~";
1336 if (keypress
.equals(kbShiftF9
)) {
1338 return "\033[20;2~";
1341 if (keypress
.equals(kbShiftF10
)) {
1343 return "\033[21;2~";
1346 if (keypress
.equals(kbShiftF11
)) {
1348 return "\033[23;2~";
1351 if (keypress
.equals(kbShiftF12
)) {
1353 return "\033[24;2~";
1356 if (keypress
.equals(kbCtrlF1
)) {
1364 if (keypress
.equals(kbCtrlF2
)) {
1372 if (keypress
.equals(kbCtrlF3
)) {
1380 if (keypress
.equals(kbCtrlF4
)) {
1388 if (keypress
.equals(kbCtrlF5
)) {
1390 return "\033[15;5~";
1393 if (keypress
.equals(kbCtrlF6
)) {
1395 return "\033[17;5~";
1398 if (keypress
.equals(kbCtrlF7
)) {
1400 return "\033[18;5~";
1403 if (keypress
.equals(kbCtrlF8
)) {
1405 return "\033[19;5~";
1408 if (keypress
.equals(kbCtrlF9
)) {
1410 return "\033[20;5~";
1413 if (keypress
.equals(kbCtrlF10
)) {
1415 return "\033[21;5~";
1418 if (keypress
.equals(kbCtrlF11
)) {
1420 return "\033[23;5~";
1423 if (keypress
.equals(kbCtrlF12
)) {
1425 return "\033[24;5~";
1428 if (keypress
.equals(kbPgUp
)) {
1433 if (keypress
.equals(kbPgDn
)) {
1438 if (keypress
.equals(kbIns
)) {
1443 if (keypress
.equals(kbShiftIns
)) {
1444 // This is what xterm sends for SHIFT-INS
1446 // This is what xterm sends for CTRL-INS
1447 // return "\033[2;5~";
1450 if (keypress
.equals(kbShiftDel
)) {
1451 // This is what xterm sends for SHIFT-DEL
1453 // This is what xterm sends for CTRL-DEL
1454 // return "\033[3;5~";
1457 if (keypress
.equals(kbDel
)) {
1458 // Delete sends real delete for VTxxx
1460 // return "\033[3~";
1463 if (keypress
.equals(kbEnter
)) {
1467 if (keypress
.equals(kbEsc
)) {
1471 if (keypress
.equals(kbAltEsc
)) {
1475 if (keypress
.equals(kbTab
)) {
1479 // Non-alt, non-ctrl characters
1480 if (!keypress
.getIsKey()) {
1481 StringBuilder sb
= new StringBuilder();
1482 sb
.append(keypress
.getCh());
1483 return sb
.toString();
1489 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1492 * @param ch 8-bit character from the remote side
1493 * @param charsetGl character set defined for GL
1494 * @param charsetGr character set defined for GR
1495 * @return character to display on the screen
1497 private char mapCharacterCharset(final char ch
,
1498 final CharacterSet charsetGl
,
1499 final CharacterSet charsetGr
) {
1501 int lookupChar
= ch
;
1502 CharacterSet lookupCharset
= charsetGl
;
1505 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
1506 lookupCharset
= charsetGr
;
1510 switch (lookupCharset
) {
1513 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
1516 return DECCharacterSets
.UK
[lookupChar
];
1519 return DECCharacterSets
.US_ASCII
[lookupChar
];
1522 return DECCharacterSets
.NL
[lookupChar
];
1525 return DECCharacterSets
.FI
[lookupChar
];
1528 return DECCharacterSets
.FR
[lookupChar
];
1531 return DECCharacterSets
.FR_CA
[lookupChar
];
1534 return DECCharacterSets
.DE
[lookupChar
];
1537 return DECCharacterSets
.IT
[lookupChar
];
1540 return DECCharacterSets
.NO
[lookupChar
];
1543 return DECCharacterSets
.ES
[lookupChar
];
1546 return DECCharacterSets
.SV
[lookupChar
];
1549 return DECCharacterSets
.SWISS
[lookupChar
];
1551 case DEC_SUPPLEMENTAL
:
1552 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
1555 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
1558 return DECCharacterSets
.US_ASCII
[lookupChar
];
1561 return DECCharacterSets
.US_ASCII
[lookupChar
];
1564 throw new IllegalArgumentException("Invalid character set value: "
1570 * Map an 8-bit byte into a printable character.
1572 * @param ch either 8-bit or Unicode character from the remote side
1573 * @return character to display on the screen
1575 private char mapCharacter(final char ch
) {
1577 // Unicode character, just return it
1581 CharacterSet charsetGl
= currentState
.g0Charset
;
1582 CharacterSet charsetGr
= currentState
.grCharset
;
1584 if (vt52Mode
== true) {
1585 if (shiftOut
== true) {
1586 // Shifted out character, pull from VT52 graphics
1587 charsetGl
= currentState
.g1Charset
;
1588 charsetGr
= CharacterSet
.US
;
1591 charsetGl
= currentState
.g0Charset
;
1592 charsetGr
= CharacterSet
.US
;
1595 // Pull the character
1596 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1600 if (shiftOut
== true) {
1601 // Shifted out character, pull from G1
1602 charsetGl
= currentState
.g1Charset
;
1603 charsetGr
= currentState
.grCharset
;
1605 // Pull the character
1606 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1610 if (singleshift
== Singleshift
.SS2
) {
1612 singleshift
= Singleshift
.NONE
;
1614 // Shifted out character, pull from G2
1615 charsetGl
= currentState
.g2Charset
;
1616 charsetGr
= currentState
.grCharset
;
1620 if (singleshift
== Singleshift
.SS3
) {
1622 singleshift
= Singleshift
.NONE
;
1624 // Shifted out character, pull from G3
1625 charsetGl
= currentState
.g3Charset
;
1626 charsetGr
= currentState
.grCharset
;
1629 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
1630 // Check for locking shift
1632 switch (currentState
.glLockshift
) {
1645 charsetGl
= currentState
.g2Charset
;
1650 charsetGl
= currentState
.g3Charset
;
1655 charsetGl
= currentState
.g0Charset
;
1659 switch (currentState
.grLockshift
) {
1669 charsetGr
= currentState
.g1Charset
;
1674 charsetGr
= currentState
.g2Charset
;
1679 charsetGr
= currentState
.g3Charset
;
1684 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
1691 // Pull the character
1692 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1696 * Scroll the text within a scrolling region up n lines.
1698 * @param regionTop top row of the scrolling region
1699 * @param regionBottom bottom row of the scrolling region
1700 * @param n number of lines to scroll
1702 private void scrollingRegionScrollUp(final int regionTop
,
1703 final int regionBottom
, final int n
) {
1705 if (regionTop
>= regionBottom
) {
1709 // Sanity check: see if there will be any characters left after the
1711 if (regionBottom
+ 1 - regionTop
<= n
) {
1712 // There won't be anything left in the region, so just call
1713 // eraseScreen() and return.
1714 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1718 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1719 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1720 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1722 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
1723 - remaining
, regionBottom
+ 1);
1724 display
= new LinkedList
<DisplayLine
>(displayTop
);
1725 display
.addAll(displayMiddle
);
1726 for (int i
= 0; i
< n
; i
++) {
1727 DisplayLine line
= new DisplayLine(currentState
.attr
);
1728 line
.setReverseColor(reverseVideo
);
1731 display
.addAll(displayBottom
);
1733 assert (display
.size() == height
);
1737 * Scroll the text within a scrolling region down n lines.
1739 * @param regionTop top row of the scrolling region
1740 * @param regionBottom bottom row of the scrolling region
1741 * @param n number of lines to scroll
1743 private void scrollingRegionScrollDown(final int regionTop
,
1744 final int regionBottom
, final int n
) {
1746 if (regionTop
>= regionBottom
) {
1750 // Sanity check: see if there will be any characters left after the
1752 if (regionBottom
+ 1 - regionTop
<= n
) {
1753 // There won't be anything left in the region, so just call
1754 // eraseScreen() and return.
1755 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1759 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1760 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1761 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1763 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
1764 regionTop
+ remaining
);
1765 display
= new LinkedList
<DisplayLine
>(displayTop
);
1766 for (int i
= 0; i
< n
; i
++) {
1767 DisplayLine line
= new DisplayLine(currentState
.attr
);
1768 line
.setReverseColor(reverseVideo
);
1771 display
.addAll(displayMiddle
);
1772 display
.addAll(displayBottom
);
1774 assert (display
.size() == height
);
1778 * Process a control character.
1780 * @param ch 8-bit character from the remote side
1782 private void handleControlChar(final char ch
) {
1783 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
1794 // Transmit the answerback message.
1805 cursorLeft(1, false);
1810 advanceToNextTabStop();
1836 currentState
.glLockshift
= LockshiftMode
.NONE
;
1842 currentState
.glLockshift
= LockshiftMode
.NONE
;
1867 singleshift
= Singleshift
.SS2
;
1872 singleshift
= Singleshift
.SS3
;
1882 * Advance the cursor to the next tab stop.
1884 private void advanceToNextTabStop() {
1885 if (tabStops
.size() == 0) {
1886 // Go to the rightmost column
1887 cursorRight(rightMargin
- currentState
.cursorX
, false);
1890 for (Integer stop
: tabStops
) {
1891 if (stop
> currentState
.cursorX
) {
1892 cursorRight(stop
- currentState
.cursorX
, false);
1897 * We got here, meaning there isn't a tab stop beyond the current
1898 * cursor position. Place the cursor of the right-most edge of the
1901 cursorRight(rightMargin
- currentState
.cursorX
, false);
1905 * Save a character into the collect buffer.
1907 * @param ch character to save
1909 private void collect(final char ch
) {
1910 collectBuffer
.add(new Character(ch
));
1914 * Save a byte into the CSI parameters buffer.
1916 * @param ch byte to save
1918 private void param(final byte ch
) {
1919 if (csiParams
.size() == 0) {
1920 csiParams
.add(new Integer(0));
1922 Integer x
= csiParams
.get(csiParams
.size() - 1);
1923 if ((ch
>= '0') && (ch
<= '9')) {
1926 csiParams
.set(csiParams
.size() - 1, x
);
1930 csiParams
.add(new Integer(0));
1935 * Get a CSI parameter value, with a default.
1937 * @param position parameter index. 0 is the first parameter.
1938 * @param defaultValue value to use if csiParams[position] doesn't exist
1939 * @return parameter value
1941 private int getCsiParam(final int position
, final int defaultValue
) {
1942 if (csiParams
.size() < position
+ 1) {
1943 return defaultValue
;
1945 return csiParams
.get(position
).intValue();
1949 * Get a CSI parameter value, clamped to within min/max.
1951 * @param position parameter index. 0 is the first parameter.
1952 * @param defaultValue value to use if csiParams[position] doesn't exist
1953 * @param minValue minimum value inclusive
1954 * @param maxValue maximum value inclusive
1955 * @return parameter value
1957 private int getCsiParam(final int position
, final int defaultValue
,
1958 final int minValue
, final int maxValue
) {
1960 assert (minValue
<= maxValue
);
1961 int value
= getCsiParam(position
, defaultValue
);
1962 if (value
< minValue
) {
1965 if (value
> maxValue
) {
1972 * Set or unset a toggle. value is 'true' for set ('h'), false for reset
1975 private void setToggle(final boolean value
) {
1976 boolean decPrivateModeFlag
= false;
1977 for (Character ch
: collectBuffer
) {
1979 decPrivateModeFlag
= true;
1983 for (Integer i
: csiParams
) {
1988 if (decPrivateModeFlag
== true) {
1990 if (value
== true) {
1991 // Use application arrow keys
1992 arrowKeyMode
= ArrowKeyMode
.VT100
;
1994 // Use ANSI arrow keys
1995 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2000 if (decPrivateModeFlag
== true) {
2001 if (value
== false) {
2005 arrowKeyMode
= ArrowKeyMode
.VT52
;
2008 * From the VT102 docs: "You use ANSI mode to select
2009 * most terminal features; the terminal uses the same
2010 * features when it switches to VT52 mode. You
2011 * cannot, however, change most of these features in
2014 * In other words, do not reset any other attributes
2015 * when switching between VT52 submode and ANSI.
2017 * HOWEVER, the real vt100 does switch the character
2018 * set according to Usenet.
2020 currentState
.g0Charset
= CharacterSet
.US
;
2021 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2024 if ((type
== DeviceType
.VT220
)
2025 || (type
== DeviceType
.XTERM
)) {
2027 // VT52 mode is explicitly 7-bit
2029 singleshift
= Singleshift
.NONE
;
2034 if (value
== true) {
2035 // Turn off keyboard
2044 if (decPrivateModeFlag
== true) {
2046 if (value
== true) {
2055 width
= rightMargin
+ 1;
2056 // Entire screen is cleared, and scrolling region is
2058 eraseScreen(0, 0, height
- 1, width
- 1, false);
2059 scrollRegionTop
= 0;
2060 scrollRegionBottom
= height
- 1;
2061 // Also home the cursor
2062 cursorPosition(0, 0);
2066 if (decPrivateModeFlag
== true) {
2068 if (value
== true) {
2077 if (value
== true) {
2085 if (decPrivateModeFlag
== true) {
2087 if (value
== true) {
2089 * Set selects reverse screen, a white screen
2090 * background with black characters.
2092 if (reverseVideo
!= true) {
2094 * If in normal video, switch it back
2096 invertDisplayColors();
2098 reverseVideo
= true;
2101 * Reset selects normal screen, a black screen
2102 * background with white characters.
2104 if (reverseVideo
== true) {
2106 * If in reverse video already, switch it back
2108 invertDisplayColors();
2110 reverseVideo
= false;
2115 if (decPrivateModeFlag
== true) {
2117 if (value
== true) {
2118 // Origin is relative to scroll region cursor.
2119 // Cursor can NEVER leave scrolling region.
2120 currentState
.originMode
= true;
2121 cursorPosition(0, 0);
2123 // Origin is absolute to entire screen. Cursor can
2124 // leave the scrolling region via cup() and hvp().
2125 currentState
.originMode
= false;
2126 cursorPosition(0, 0);
2131 if (decPrivateModeFlag
== true) {
2133 if (value
== true) {
2135 currentState
.lineWrap
= true;
2137 // Turn linewrap off
2138 currentState
.lineWrap
= false;
2143 if (decPrivateModeFlag
== true) {
2145 if (value
== true) {
2146 // Keyboard auto-repeat on
2149 // Keyboard auto-repeat off
2155 if (decPrivateModeFlag
== false) {
2157 if (value
== true) {
2167 if (decPrivateModeFlag
== true) {
2173 if (decPrivateModeFlag
== true) {
2179 if (decPrivateModeFlag
== false) {
2181 if (value
== true) {
2183 * Set causes a received linefeed, form feed, or
2184 * vertical tab to move cursor to first column of
2185 * next line. RETURN transmits both a carriage return
2186 * and linefeed. This selection is also called new
2192 * Reset causes a received linefeed, form feed, or
2193 * vertical tab to move cursor to next line in
2194 * current column. RETURN transmits a carriage
2197 newLineMode
= false;
2203 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2204 if (decPrivateModeFlag
== true) {
2206 if (value
== true) {
2208 cursorVisible
= true;
2211 cursorVisible
= false;
2218 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2219 if (decPrivateModeFlag
== true) {
2221 if (value
== true) {
2222 // Select national mode NRC
2225 // Select multi-national mode
2241 * DECSC - Save cursor.
2243 private void decsc() {
2244 savedState
.setTo(currentState
);
2248 * DECRC - Restore cursor.
2250 private void decrc() {
2251 currentState
.setTo(savedState
);
2257 private void ind() {
2258 // Move the cursor and scroll if necessary. If at the bottom line
2259 // already, a scroll up is supposed to be performed.
2260 if (currentState
.cursorY
== scrollRegionBottom
) {
2261 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2263 cursorDown(1, true);
2267 * RI - Reverse index.
2270 // Move the cursor and scroll if necessary. If at the top line
2271 // already, a scroll down is supposed to be performed.
2272 if (currentState
.cursorY
== scrollRegionTop
) {
2273 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2281 private void nel() {
2282 // Move the cursor and scroll if necessary. If at the bottom line
2283 // already, a scroll up is supposed to be performed.
2284 if (currentState
.cursorY
== scrollRegionBottom
) {
2285 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2287 cursorDown(1, true);
2289 // Reset to the beginning of the next line
2290 currentState
.cursorX
= 0;
2294 * DECKPAM - Keypad application mode.
2296 private void deckpam() {
2297 keypadMode
= KeypadMode
.Application
;
2301 * DECKPNM - Keypad numeric mode.
2303 private void deckpnm() {
2304 keypadMode
= KeypadMode
.Numeric
;
2310 * @param n number of spaces to move
2311 * @param honorScrollRegion if true, then do nothing if the cursor is
2312 * outside the scrolling region
2314 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2318 * Special case: if a user moves the cursor from the right margin, we
2319 * have to reset the VT100 right margin flag.
2322 wrapLineFlag
= false;
2325 for (int i
= 0; i
< n
; i
++) {
2326 if (honorScrollRegion
== true) {
2327 // Honor the scrolling region
2328 if ((currentState
.cursorY
< scrollRegionTop
)
2329 || (currentState
.cursorY
> scrollRegionBottom
)
2331 // Outside region, do nothing
2334 // Inside region, go up
2335 top
= scrollRegionTop
;
2337 // Non-scrolling case
2341 if (currentState
.cursorY
> top
) {
2342 currentState
.cursorY
--;
2348 * Move down n spaces.
2350 * @param n number of spaces to move
2351 * @param honorScrollRegion if true, then do nothing if the cursor is
2352 * outside the scrolling region
2354 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
2358 * Special case: if a user moves the cursor from the right margin, we
2359 * have to reset the VT100 right margin flag.
2362 wrapLineFlag
= false;
2365 for (int i
= 0; i
< n
; i
++) {
2367 if (honorScrollRegion
== true) {
2368 // Honor the scrolling region
2369 if (currentState
.cursorY
> scrollRegionBottom
) {
2370 // Outside region, do nothing
2373 // Inside region, go down
2374 bottom
= scrollRegionBottom
;
2376 // Non-scrolling case
2377 bottom
= height
- 1;
2380 if (currentState
.cursorY
< bottom
) {
2381 currentState
.cursorY
++;
2387 * Move left n spaces.
2389 * @param n number of spaces to move
2390 * @param honorScrollRegion if true, then do nothing if the cursor is
2391 * outside the scrolling region
2393 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
2395 * Special case: if a user moves the cursor from the right margin, we
2396 * have to reset the VT100 right margin flag.
2399 wrapLineFlag
= false;
2402 for (int i
= 0; i
< n
; i
++) {
2403 if (honorScrollRegion
== true) {
2404 // Honor the scrolling region
2405 if ((currentState
.cursorY
< scrollRegionTop
)
2406 || (currentState
.cursorY
> scrollRegionBottom
)
2408 // Outside region, do nothing
2413 if (currentState
.cursorX
> 0) {
2414 currentState
.cursorX
--;
2420 * Move right n spaces.
2422 * @param n number of spaces to move
2423 * @param honorScrollRegion if true, then do nothing if the cursor is
2424 * outside the scrolling region
2426 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
2427 int rightMargin
= this.rightMargin
;
2430 * Special case: if a user moves the cursor from the right margin, we
2431 * have to reset the VT100 right margin flag.
2434 wrapLineFlag
= false;
2437 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2438 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2441 for (int i
= 0; i
< n
; i
++) {
2442 if (honorScrollRegion
== true) {
2443 // Honor the scrolling region
2444 if ((currentState
.cursorY
< scrollRegionTop
)
2445 || (currentState
.cursorY
> scrollRegionBottom
)
2447 // Outside region, do nothing
2452 if (currentState
.cursorX
< rightMargin
) {
2453 currentState
.cursorX
++;
2459 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2461 * @param row row to move to
2462 * @param col column to move to
2464 private void cursorPosition(int row
, final int col
) {
2465 int rightMargin
= this.rightMargin
;
2470 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2471 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2474 // Set column number
2475 currentState
.cursorX
= col
;
2477 // Sanity check, bring column back to margin.
2478 if (currentState
.cursorX
> rightMargin
) {
2479 currentState
.cursorX
= rightMargin
;
2483 if (currentState
.originMode
== true) {
2484 row
+= scrollRegionTop
;
2486 if (currentState
.cursorY
< row
) {
2487 cursorDown(row
- currentState
.cursorY
, false);
2488 } else if (currentState
.cursorY
> row
) {
2489 cursorUp(currentState
.cursorY
- row
, false);
2492 wrapLineFlag
= false;
2496 * HTS - Horizontal tabulation set.
2498 private void hts() {
2499 for (Integer stop
: tabStops
) {
2500 if (stop
== currentState
.cursorX
) {
2501 // Already have a tab stop here
2506 // Append a tab stop to the end of the array and resort them
2507 tabStops
.add(currentState
.cursorX
);
2508 Collections
.sort(tabStops
);
2512 * DECSWL - Single-width line.
2514 private void decswl() {
2515 display
.get(currentState
.cursorY
).setDoubleWidth(false);
2516 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2520 * DECDWL - Double-width line.
2522 private void decdwl() {
2523 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2524 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2528 * DECHDL - Double-height + double-width line.
2530 * @param topHalf if true, this sets the row to be the top half row of a
2533 private void dechdl(final boolean topHalf
) {
2534 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2535 if (topHalf
== true) {
2536 display
.get(currentState
.cursorY
).setDoubleHeight(1);
2538 display
.get(currentState
.cursorY
).setDoubleHeight(2);
2543 * DECALN - Screen alignment display.
2545 private void decaln() {
2546 Cell newCell
= new Cell();
2547 newCell
.setChar('E');
2548 for (DisplayLine line
: display
) {
2549 for (int i
= 0; i
< line
.length(); i
++) {
2550 line
.replace(i
, newCell
);
2556 * DECSCL - Compatibility level.
2558 private void decscl() {
2559 int i
= getCsiParam(0, 0);
2560 int j
= getCsiParam(1, 0);
2564 currentState
.g0Charset
= CharacterSet
.US
;
2565 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2567 } else if (i
== 62) {
2569 if ((j
== 0) || (j
== 2)) {
2571 } else if (j
== 1) {
2578 * CUD - Cursor down.
2580 private void cud() {
2581 cursorDown(getCsiParam(0, 1, 1, height
), true);
2585 * CUF - Cursor forward.
2587 private void cuf() {
2588 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
2592 * CUB - Cursor backward.
2594 private void cub() {
2595 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
2601 private void cuu() {
2602 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2606 * CUP - Cursor position.
2608 private void cup() {
2609 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2610 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
2614 * CNL - Cursor down and to column 1.
2616 private void cnl() {
2617 cursorDown(getCsiParam(0, 1, 1, height
), true);
2619 cursorLeft(currentState
.cursorX
, true);
2623 * CPL - Cursor up and to column 1.
2625 private void cpl() {
2626 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2628 cursorLeft(currentState
.cursorX
, true);
2632 * CHA - Cursor to column # in current row.
2634 private void cha() {
2635 cursorPosition(currentState
.cursorY
,
2636 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
2640 * VPA - Cursor to row #, same column.
2642 private void vpa() {
2643 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2644 currentState
.cursorX
);
2648 * ED - Erase in display.
2651 boolean honorProtected
= false;
2652 boolean decPrivateModeFlag
= false;
2654 for (Character ch
: collectBuffer
) {
2656 decPrivateModeFlag
= true;
2660 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2661 && (decPrivateModeFlag
== true)
2663 honorProtected
= true;
2666 int i
= getCsiParam(0, 0);
2669 // Erase from here to end of screen
2670 if (currentState
.cursorY
< height
- 1) {
2671 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
2674 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2675 } else if (i
== 1) {
2676 // Erase from beginning of screen to here
2677 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
2679 eraseLine(0, currentState
.cursorX
, honorProtected
);
2680 } else if (i
== 2) {
2681 // Erase entire screen
2682 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
2687 * EL - Erase in line.
2690 boolean honorProtected
= false;
2691 boolean decPrivateModeFlag
= false;
2693 for (Character ch
: collectBuffer
) {
2695 decPrivateModeFlag
= true;
2699 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2700 && (decPrivateModeFlag
== true)
2702 honorProtected
= true;
2705 int i
= getCsiParam(0, 0);
2708 // Erase from here to end of line
2709 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2710 } else if (i
== 1) {
2711 // Erase from beginning of line to here
2712 eraseLine(0, currentState
.cursorX
, honorProtected
);
2713 } else if (i
== 2) {
2714 // Erase entire line
2715 eraseLine(0, width
- 1, honorProtected
);
2720 * ECH - Erase # of characters in current row.
2722 private void ech() {
2723 int i
= getCsiParam(0, 1, 1, width
);
2725 // Erase from here to i characters
2726 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
2733 int i
= getCsiParam(0, 1);
2735 if ((currentState
.cursorY
>= scrollRegionTop
)
2736 && (currentState
.cursorY
<= scrollRegionBottom
)
2739 // I can get the same effect with a scroll-down
2740 scrollingRegionScrollDown(currentState
.cursorY
,
2741 scrollRegionBottom
, i
);
2746 * DCH - Delete char.
2748 private void dch() {
2749 int n
= getCsiParam(0, 1);
2750 DisplayLine line
= display
.get(currentState
.cursorY
);
2751 Cell blank
= new Cell();
2752 for (int i
= 0; i
< n
; i
++) {
2753 line
.delete(currentState
.cursorX
, blank
);
2758 * ICH - Insert blank char at cursor.
2760 private void ich() {
2761 int n
= getCsiParam(0, 1);
2762 DisplayLine line
= display
.get(currentState
.cursorY
);
2763 Cell blank
= new Cell();
2764 for (int i
= 0; i
< n
; i
++) {
2765 line
.insert(currentState
.cursorX
, blank
);
2773 int i
= getCsiParam(0, 1);
2775 if ((currentState
.cursorY
>= scrollRegionTop
)
2776 && (currentState
.cursorY
<= scrollRegionBottom
)) {
2778 // I can get the same effect with a scroll-down
2779 scrollingRegionScrollUp(currentState
.cursorY
,
2780 scrollRegionBottom
, i
);
2785 * HVP - Horizontal and vertical position.
2787 private void hvp() {
2792 * REP - Repeat character.
2794 private void rep() {
2795 int n
= getCsiParam(0, 1);
2796 for (int i
= 0; i
< n
; i
++) {
2797 printCharacter(repCh
);
2805 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
2806 getCsiParam(0, 1, 1, height
));
2813 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
2814 getCsiParam(0, 1, 1, height
));
2818 * CBT - Go back X tab stops.
2820 private void cbt() {
2821 int tabsToMove
= getCsiParam(0, 1);
2824 for (int i
= 0; i
< tabsToMove
; i
++) {
2825 int j
= currentState
.cursorX
;
2826 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
2827 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
2835 j
= tabStops
.get(tabI
);
2837 cursorPosition(currentState
.cursorY
, j
);
2842 * CHT - Advance X tab stops.
2844 private void cht() {
2845 int n
= getCsiParam(0, 1);
2846 for (int i
= 0; i
< n
; i
++) {
2847 advanceToNextTabStop();
2852 * SGR - Select graphics rendition.
2854 private void sgr() {
2856 if (csiParams
.size() == 0) {
2857 currentState
.attr
.reset();
2861 for (Integer i
: csiParams
) {
2867 currentState
.attr
.reset();
2872 currentState
.attr
.setBold(true);
2877 currentState
.attr
.setUnderline(true);
2882 currentState
.attr
.setBlink(true);
2887 currentState
.attr
.setReverse(true);
2894 if (type
== DeviceType
.XTERM
) {
2908 if ((type
== DeviceType
.VT220
)
2909 || (type
== DeviceType
.XTERM
)) {
2915 currentState
.attr
.setBold(false);
2920 currentState
.attr
.setUnderline(false);
2925 currentState
.attr
.setBlink(false);
2930 currentState
.attr
.setReverse(false);
2938 // A true VT100/102/220 does not support color, however everyone
2939 // is used to their terminal emulator supporting color so we will
2940 // unconditionally support color for all DeviceType's.
2945 // Set black foreground
2946 currentState
.attr
.setForeColor(Color
.BLACK
);
2949 // Set red foreground
2950 currentState
.attr
.setForeColor(Color
.RED
);
2953 // Set green foreground
2954 currentState
.attr
.setForeColor(Color
.GREEN
);
2957 // Set yellow foreground
2958 currentState
.attr
.setForeColor(Color
.YELLOW
);
2961 // Set blue foreground
2962 currentState
.attr
.setForeColor(Color
.BLUE
);
2965 // Set magenta foreground
2966 currentState
.attr
.setForeColor(Color
.MAGENTA
);
2969 // Set cyan foreground
2970 currentState
.attr
.setForeColor(Color
.CYAN
);
2973 // Set white foreground
2974 currentState
.attr
.setForeColor(Color
.WHITE
);
2977 // Underscore on, default foreground color
2978 currentState
.attr
.setUnderline(true);
2979 currentState
.attr
.setForeColor(Color
.WHITE
);
2982 // Underscore off, default foreground color
2983 currentState
.attr
.setUnderline(false);
2984 currentState
.attr
.setForeColor(Color
.WHITE
);
2987 // Set black background
2988 currentState
.attr
.setBackColor(Color
.BLACK
);
2991 // Set red background
2992 currentState
.attr
.setBackColor(Color
.RED
);
2995 // Set green background
2996 currentState
.attr
.setBackColor(Color
.GREEN
);
2999 // Set yellow background
3000 currentState
.attr
.setBackColor(Color
.YELLOW
);
3003 // Set blue background
3004 currentState
.attr
.setBackColor(Color
.BLUE
);
3007 // Set magenta background
3008 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3011 // Set cyan background
3012 currentState
.attr
.setBackColor(Color
.CYAN
);
3015 // Set white background
3016 currentState
.attr
.setBackColor(Color
.WHITE
);
3019 // Default background
3020 currentState
.attr
.setBackColor(Color
.BLACK
);
3030 * DA - Device attributes.
3033 int extendedFlag
= 0;
3035 Character
[] chars
= collectBuffer
.toArray(new Character
[0]);
3036 StringBuilder args
= new StringBuilder();
3037 for (int j
= 1; j
< chars
.length
; j
++) {
3038 args
.append(chars
[j
]);
3041 if (chars
.length
> 0) {
3042 if (chars
[0] == '>') {
3044 if (chars
.length
>= 2) {
3045 i
= Integer
.parseInt(args
.toString());
3047 } else if (chars
[0] == '=') {
3049 if (chars
.length
>= 2) {
3050 i
= Integer
.parseInt(args
.toString());
3053 // Unknown code, bail out
3058 if ((i
!= 0) && (i
!= 1)) {
3062 if ((extendedFlag
== 0) && (i
== 0)) {
3063 // Send string directly to remote side
3064 writeRemote(deviceTypeResponse());
3068 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3070 if ((extendedFlag
== 1) && (i
== 0)) {
3072 * Request "What type of terminal are you, what is your
3073 * firmware version, and what hardware options do you have
3076 * Respond: "I am a VT220 (identification code of 1), my
3077 * firmware version is _____ (Pv), and I have _____ Po
3078 * options installed."
3084 if (s8c1t
== true) {
3085 writeRemote("\u009b>1;10;0c");
3087 writeRemote("\033[>1;10;0c");
3093 if ((extendedFlag
== 2) && (i
== 0)) {
3096 * Request "What is your unit ID?"
3098 * Respond: "I was manufactured at site 00 and have a unique ID
3102 writeRemote("\033P!|00010203\033\\");
3107 * DECSTBM - Set top and bottom margins.
3109 private void decstbm() {
3110 int top
= getCsiParam(0, 1, 1, height
) - 1;
3111 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3116 scrollRegionTop
= top
;
3117 scrollRegionBottom
= bottom
;
3120 cursorPosition(0, 0);
3124 * DECREQTPARM - Request terminal parameters.
3126 private void decreqtparm() {
3127 int i
= getCsiParam(0, 0);
3129 if ((i
!= 0) && (i
!= 1)) {
3136 * Request terminal parameters.
3140 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3141 * (CLoCk MULtiplier = 1, STP option flags = 0)
3145 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3148 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3150 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3156 * DECSCA - Select Character Attributes.
3158 private void decsca() {
3159 int i
= getCsiParam(0, 0);
3161 if ((i
== 0) || (i
== 2)) {
3163 currentState
.attr
.setProtect(false);
3167 currentState
.attr
.setProtect(true);
3172 * DECSTR - Soft Terminal Reset.
3174 private void decstr() {
3175 // Do exactly like RIS - Reset to initial state
3177 // Do I clear screen too? I think so...
3178 eraseScreen(0, 0, height
- 1, width
- 1, false);
3179 cursorPosition(0, 0);
3183 * DSR - Device status report.
3185 private void dsr() {
3186 boolean decPrivateModeFlag
= false;
3188 for (Character ch
: collectBuffer
) {
3190 decPrivateModeFlag
= true;
3194 int i
= getCsiParam(0, 0);
3199 // Request status report. Respond with "OK, no malfunction."
3201 // Send string directly to remote side
3202 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3205 writeRemote("\u009b0n");
3207 writeRemote("\033[0n");
3212 // Request cursor position. Respond with current position.
3214 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3217 str
= String
.format("\u009b%d;%dR",
3218 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3220 str
= String
.format("\033[%d;%dR",
3221 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3224 // Send string directly to remote side
3229 if (decPrivateModeFlag
== true) {
3231 // Request printer status report. Respond with "Printer not
3234 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3235 && (s8c1t
== true)) {
3236 writeRemote("\u009b?13n");
3238 writeRemote("\033[?13n");
3244 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3245 && (decPrivateModeFlag
== true)
3248 // Request user-defined keys are locked or unlocked. Respond
3249 // with "User-defined keys are locked."
3251 if (s8c1t
== true) {
3252 writeRemote("\u009b?21n");
3254 writeRemote("\033[?21n");
3260 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3261 && (decPrivateModeFlag
== true)
3264 // Request keyboard language. Respond with "Keyboard
3265 // language is North American."
3267 if (s8c1t
== true) {
3268 writeRemote("\u009b?27;1n");
3270 writeRemote("\033[?27;1n");
3277 // Some other option, ignore
3283 * TBC - Tabulation clear.
3285 private void tbc() {
3286 int i
= getCsiParam(0, 0);
3288 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3289 for (Integer stop
: tabStops
) {
3290 if (stop
== currentState
.cursorX
) {
3295 tabStops
= newStops
;
3303 * Erase the characters in the current line from the start column to the
3304 * end column, inclusive.
3306 * @param start starting column to erase (between 0 and width - 1)
3307 * @param end ending column to erase (between 0 and width - 1)
3308 * @param honorProtected if true, do not erase characters with the
3309 * protected attribute set
3311 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3316 if (end
> width
- 1) {
3323 for (int i
= start
; i
<= end
; i
++) {
3324 DisplayLine line
= display
.get(currentState
.cursorY
);
3325 if ((!honorProtected
)
3326 || ((honorProtected
) && (!line
.charAt(i
).getProtect()))) {
3333 * From the VT102 manual:
3335 * Erasing a character also erases any character
3336 * attribute of the character.
3342 * Erase with the current color a.k.a. back-color erase
3345 line
.setChar(i
, ' ');
3346 line
.setAttr(i
, currentState
.attr
);
3354 * Erase a rectangular section of the screen, inclusive. end column,
3357 * @param startRow starting row to erase (between 0 and height - 1)
3358 * @param startCol starting column to erase (between 0 and width - 1)
3359 * @param endRow ending row to erase (between 0 and height - 1)
3360 * @param endCol ending column to erase (between 0 and width - 1)
3361 * @param honorProtected if true, do not erase characters with the
3362 * protected attribute set
3364 private void eraseScreen(final int startRow
, final int startCol
,
3365 final int endRow
, final int endCol
, final boolean honorProtected
) {
3373 || (endRow
< startRow
)
3374 || (endCol
< startCol
)
3379 oldCursorY
= currentState
.cursorY
;
3380 for (int i
= startRow
; i
<= endRow
; i
++) {
3381 currentState
.cursorY
= i
;
3382 eraseLine(startCol
, endCol
, honorProtected
);
3384 // Erase display clears the double attributes
3385 display
.get(i
).setDoubleWidth(false);
3386 display
.get(i
).setDoubleHeight(0);
3388 currentState
.cursorY
= oldCursorY
;
3392 * VT220 printer functions. All of these are parsed, but won't do
3395 private void printerFunctions() {
3396 boolean decPrivateModeFlag
= false;
3397 for (Character ch
: collectBuffer
) {
3399 decPrivateModeFlag
= true;
3403 int i
= getCsiParam(0, 0);
3408 if (decPrivateModeFlag
== false) {
3414 if (decPrivateModeFlag
== true) {
3415 // Print cursor line
3420 if (decPrivateModeFlag
== true) {
3421 // Auto print mode OFF
3423 // Printer controller OFF
3425 // Characters re-appear on the screen
3426 printerControllerMode
= false;
3431 if (decPrivateModeFlag
== true) {
3435 // Printer controller
3437 // Characters get sucked into oblivion
3438 printerControllerMode
= true;
3449 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
3450 * of remote systems will send an XTerm title sequence even if TERM isn't
3453 * @param xtermChar the character received from the remote side
3455 private void oscPut(final char xtermChar
) {
3457 collectBuffer
.add(new Character(xtermChar
));
3460 if (xtermChar
== 0x07) {
3461 Character
[] chars
= collectBuffer
.toArray(new Character
[0]);
3462 StringBuilder args
= new StringBuilder();
3463 for (int j
= 0; j
< chars
.length
- 1; j
++) {
3464 args
.append(chars
[j
]);
3466 String
[] p
= args
.toString().split(";");
3468 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
3476 // Go to SCAN_GROUND state
3483 * Run this input character through the ECMA48 state machine.
3485 * @param ch character from the remote side
3487 public void consume(char ch
) {
3490 // System.err.printf("%c", ch);
3492 // Special case for VT10x: 7-bit characters only
3493 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
3494 ch
= (char)(ch
& 0x7F);
3497 // Special "anywhere" states
3499 // 18, 1A --> execute, then switch to SCAN_GROUND
3500 if ((ch
== 0x18) || (ch
== 0x1A)) {
3501 // CAN and SUB abort escape sequences
3506 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
3510 && (scanState
!= ScanState
.DCS_ENTRY
)
3511 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
3512 && (scanState
!= ScanState
.DCS_IGNORE
)
3513 && (scanState
!= ScanState
.DCS_PARAM
)
3514 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
3517 scanState
= ScanState
.ESCAPE
;
3521 // 0x9B == CSI 8-bit sequence
3523 scanState
= ScanState
.CSI_ENTRY
;
3527 // 0x9D goes to ScanState.OSC_STRING
3529 scanState
= ScanState
.OSC_STRING
;
3533 // 0x90 goes to DCS_ENTRY
3535 scanState
= ScanState
.DCS_ENTRY
;
3539 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
3540 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
3541 scanState
= ScanState
.SOSPMAPC_STRING
;
3545 // 0x7F (DEL) is always discarded
3550 switch (scanState
) {
3553 // 00-17, 19, 1C-1F --> execute
3554 // 80-8F, 91-9A, 9C --> execute
3555 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
3556 handleControlChar(ch
);
3560 if (((ch
>= 0x20) && (ch
<= 0x7F))
3564 // VT220 printer --> trash bin
3565 if (((type
== DeviceType
.VT220
)
3566 || (type
== DeviceType
.XTERM
))
3567 && (printerControllerMode
== true)
3572 // Hang onto this character
3573 repCh
= mapCharacter(ch
);
3575 // Print this character
3576 printCharacter(repCh
);
3581 // 00-17, 19, 1C-1F --> execute
3583 handleControlChar(ch
);
3587 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
3588 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
3590 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
3594 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
3595 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
3606 // DECSC - Save cursor
3607 // Note this code overlaps both ANSI and VT52 mode
3612 // DECRC - Restore cursor
3613 // Note this code overlaps both ANSI and VT52 mode
3622 if (vt52Mode
== true) {
3623 // DECANM - Enter ANSI mode
3625 arrowKeyMode
= ArrowKeyMode
.VT100
;
3628 * From the VT102 docs: "You use ANSI mode to select
3629 * most terminal features; the terminal uses the same
3630 * features when it switches to VT52 mode. You
3631 * cannot, however, change most of these features in
3634 * In other words, do not reset any other attributes
3635 * when switching between VT52 submode and ANSI.
3639 currentState
.g0Charset
= CharacterSet
.US
;
3640 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3642 singleshift
= Singleshift
.NONE
;
3643 currentState
.glLockshift
= LockshiftMode
.NONE
;
3644 currentState
.grLockshift
= LockshiftMode
.NONE
;
3648 // DECKPAM - Keypad application mode
3649 // Note this code overlaps both ANSI and VT52 mode
3653 // DECKPNM - Keypad numeric mode
3654 // Note this code overlaps both ANSI and VT52 mode
3661 if (vt52Mode
== true) {
3662 // Cursor up, and stop at the top without scrolling
3667 if (vt52Mode
== true) {
3668 // Cursor down, and stop at the bottom without scrolling
3669 cursorDown(1, false);
3673 if (vt52Mode
== true) {
3674 // Cursor right, and stop at the right without scrolling
3675 cursorRight(1, false);
3679 if (vt52Mode
== true) {
3680 // Cursor left, and stop at the left without scrolling
3681 cursorLeft(1, false);
3688 if (vt52Mode
== true) {
3696 if (vt52Mode
== true) {
3697 // G0 --> Special graphics
3698 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
3702 if (vt52Mode
== true) {
3704 currentState
.g0Charset
= CharacterSet
.US
;
3708 if (vt52Mode
== true) {
3710 cursorPosition(0, 0);
3712 // HTS - Horizontal tabulation set
3717 if (vt52Mode
== true) {
3718 // Reverse line feed. Same as RI.
3723 if (vt52Mode
== true) {
3724 // Erase to end of screen
3725 eraseLine(currentState
.cursorX
, width
- 1, false);
3726 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
3731 if (vt52Mode
== true) {
3732 // Erase to end of line
3733 eraseLine(currentState
.cursorX
, width
- 1, false);
3739 if (vt52Mode
== true) {
3742 // RI - Reverse index
3747 if (vt52Mode
== false) {
3749 singleshift
= Singleshift
.SS2
;
3753 if (vt52Mode
== false) {
3755 singleshift
= Singleshift
.SS3
;
3762 if ((ch
>= 0x51) && (ch
<= 0x57)) {
3778 if (vt52Mode
== true) {
3779 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
3787 if (vt52Mode
== true) {
3789 // Send string directly to remote side
3790 writeRemote("\033/Z");
3793 // Send string directly to remote side
3794 writeRemote(deviceTypeResponse());
3805 // VT52 cannot get to any of these other states
3806 if (vt52Mode
== true) {
3811 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
3818 // RIS - Reset to initial state
3820 // Do I clear screen too? I think so...
3821 eraseScreen(0, 0, height
- 1, width
- 1, false);
3822 cursorPosition(0, 0);
3836 if ((type
== DeviceType
.VT220
)
3837 || (type
== DeviceType
.XTERM
)) {
3839 // VT220 lockshift G2 into GL
3840 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
3845 if ((type
== DeviceType
.VT220
)
3846 || (type
== DeviceType
.XTERM
)) {
3848 // VT220 lockshift G3 into GL
3849 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
3867 if ((type
== DeviceType
.VT220
)
3868 || (type
== DeviceType
.XTERM
)) {
3870 // VT220 lockshift G3 into GR
3871 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
3876 if ((type
== DeviceType
.VT220
)
3877 || (type
== DeviceType
.XTERM
)) {
3879 // VT220 lockshift G2 into GR
3880 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
3886 if ((type
== DeviceType
.VT220
)
3887 || (type
== DeviceType
.XTERM
)) {
3889 // VT220 lockshift G1 into GR
3890 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
3900 // 0x5B goes to CSI_ENTRY
3902 scanState
= ScanState
.CSI_ENTRY
;
3905 // 0x5D goes to OSC_STRING
3907 scanState
= ScanState
.OSC_STRING
;
3910 // 0x50 goes to DCS_ENTRY
3912 scanState
= ScanState
.DCS_ENTRY
;
3915 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
3916 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
3917 scanState
= ScanState
.SOSPMAPC_STRING
;
3922 case ESCAPE_INTERMEDIATE
:
3923 // 00-17, 19, 1C-1F --> execute
3925 handleControlChar(ch
);
3928 // 20-2F --> collect
3929 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
3933 // 30-7E --> dispatch, then switch to GROUND
3934 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
3937 if ((collectBuffer
.size() == 1)
3938 && (collectBuffer
.get(0) == '(')) {
3939 // G0 --> Special graphics
3940 currentState
.g0Charset
= CharacterSet
.DRAWING
;
3942 if ((collectBuffer
.size() == 1)
3943 && (collectBuffer
.get(0) == ')')) {
3944 // G1 --> Special graphics
3945 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3947 if ((type
== DeviceType
.VT220
)
3948 || (type
== DeviceType
.XTERM
)) {
3950 if ((collectBuffer
.size() == 1)
3951 && (collectBuffer
.get(0) == '*')) {
3952 // G2 --> Special graphics
3953 currentState
.g2Charset
= CharacterSet
.DRAWING
;
3955 if ((collectBuffer
.size() == 1)
3956 && (collectBuffer
.get(0) == '+')) {
3957 // G3 --> Special graphics
3958 currentState
.g3Charset
= CharacterSet
.DRAWING
;
3963 if ((collectBuffer
.size() == 1)
3964 && (collectBuffer
.get(0) == '(')) {
3965 // G0 --> Alternate character ROM standard character set
3966 currentState
.g0Charset
= CharacterSet
.ROM
;
3968 if ((collectBuffer
.size() == 1)
3969 && (collectBuffer
.get(0) == ')')) {
3970 // G1 --> Alternate character ROM standard character set
3971 currentState
.g1Charset
= CharacterSet
.ROM
;
3975 if ((collectBuffer
.size() == 1)
3976 && (collectBuffer
.get(0) == '(')) {
3977 // G0 --> Alternate character ROM special graphics
3978 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
3980 if ((collectBuffer
.size() == 1)
3981 && (collectBuffer
.get(0) == ')')) {
3982 // G1 --> Alternate character ROM special graphics
3983 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
3987 if ((collectBuffer
.size() == 1)
3988 && (collectBuffer
.get(0) == '#')) {
3989 // DECDHL - Double-height line (top half)
3994 if ((collectBuffer
.size() == 1)
3995 && (collectBuffer
.get(0) == '#')) {
3996 // DECDHL - Double-height line (bottom half)
3999 if ((type
== DeviceType
.VT220
)
4000 || (type
== DeviceType
.XTERM
)) {
4002 if ((collectBuffer
.size() == 1)
4003 && (collectBuffer
.get(0) == '(')) {
4005 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4007 if ((collectBuffer
.size() == 1)
4008 && (collectBuffer
.get(0) == ')')) {
4010 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4012 if ((collectBuffer
.size() == 1)
4013 && (collectBuffer
.get(0) == '*')) {
4015 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4017 if ((collectBuffer
.size() == 1)
4018 && (collectBuffer
.get(0) == '+')) {
4020 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4025 if ((collectBuffer
.size() == 1)
4026 && (collectBuffer
.get(0) == '#')) {
4027 // DECSWL - Single-width line
4030 if ((type
== DeviceType
.VT220
)
4031 || (type
== DeviceType
.XTERM
)) {
4033 if ((collectBuffer
.size() == 1)
4034 && (collectBuffer
.get(0) == '(')) {
4036 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4038 if ((collectBuffer
.size() == 1)
4039 && (collectBuffer
.get(0) == ')')) {
4041 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4043 if ((collectBuffer
.size() == 1)
4044 && (collectBuffer
.get(0) == '*')) {
4046 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4048 if ((collectBuffer
.size() == 1)
4049 && (collectBuffer
.get(0) == '+')) {
4051 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4056 if ((collectBuffer
.size() == 1)
4057 && (collectBuffer
.get(0) == '#')) {
4058 // DECDWL - Double-width line
4061 if ((type
== DeviceType
.VT220
)
4062 || (type
== DeviceType
.XTERM
)) {
4064 if ((collectBuffer
.size() == 1)
4065 && (collectBuffer
.get(0) == '(')) {
4067 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4069 if ((collectBuffer
.size() == 1)
4070 && (collectBuffer
.get(0) == ')')) {
4072 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4074 if ((collectBuffer
.size() == 1)
4075 && (collectBuffer
.get(0) == '*')) {
4077 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4079 if ((collectBuffer
.size() == 1)
4080 && (collectBuffer
.get(0) == '+')) {
4082 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4087 if ((type
== DeviceType
.VT220
)
4088 || (type
== DeviceType
.XTERM
)) {
4090 if ((collectBuffer
.size() == 1)
4091 && (collectBuffer
.get(0) == '(')) {
4093 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4095 if ((collectBuffer
.size() == 1)
4096 && (collectBuffer
.get(0) == ')')) {
4098 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4100 if ((collectBuffer
.size() == 1)
4101 && (collectBuffer
.get(0) == '*')) {
4103 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4105 if ((collectBuffer
.size() == 1)
4106 && (collectBuffer
.get(0) == '+')) {
4108 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4113 if ((collectBuffer
.size() == 1)
4114 && (collectBuffer
.get(0) == '#')) {
4115 // DECALN - Screen alignment display
4124 if ((type
== DeviceType
.VT220
)
4125 || (type
== DeviceType
.XTERM
)) {
4127 if ((collectBuffer
.size() == 1)
4128 && (collectBuffer
.get(0) == '(')) {
4129 // G0 --> DEC_SUPPLEMENTAL
4130 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4132 if ((collectBuffer
.size() == 1)
4133 && (collectBuffer
.get(0) == ')')) {
4134 // G1 --> DEC_SUPPLEMENTAL
4135 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4137 if ((collectBuffer
.size() == 1)
4138 && (collectBuffer
.get(0) == '*')) {
4139 // G2 --> DEC_SUPPLEMENTAL
4140 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4142 if ((collectBuffer
.size() == 1)
4143 && (collectBuffer
.get(0) == '+')) {
4144 // G3 --> DEC_SUPPLEMENTAL
4145 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4150 if ((type
== DeviceType
.VT220
)
4151 || (type
== DeviceType
.XTERM
)) {
4153 if ((collectBuffer
.size() == 1)
4154 && (collectBuffer
.get(0) == '(')) {
4156 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4158 if ((collectBuffer
.size() == 1)
4159 && (collectBuffer
.get(0) == ')')) {
4161 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4163 if ((collectBuffer
.size() == 1)
4164 && (collectBuffer
.get(0) == '*')) {
4166 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4168 if ((collectBuffer
.size() == 1)
4169 && (collectBuffer
.get(0) == '+')) {
4171 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4180 if ((collectBuffer
.size() == 1)
4181 && (collectBuffer
.get(0) == '(')) {
4182 // G0 --> United Kingdom set
4183 currentState
.g0Charset
= CharacterSet
.UK
;
4185 if ((collectBuffer
.size() == 1)
4186 && (collectBuffer
.get(0) == ')')) {
4187 // G1 --> United Kingdom set
4188 currentState
.g1Charset
= CharacterSet
.UK
;
4190 if ((type
== DeviceType
.VT220
)
4191 || (type
== DeviceType
.XTERM
)) {
4193 if ((collectBuffer
.size() == 1)
4194 && (collectBuffer
.get(0) == '*')) {
4195 // G2 --> United Kingdom set
4196 currentState
.g2Charset
= CharacterSet
.UK
;
4198 if ((collectBuffer
.size() == 1)
4199 && (collectBuffer
.get(0) == '+')) {
4200 // G3 --> United Kingdom set
4201 currentState
.g3Charset
= CharacterSet
.UK
;
4206 if ((collectBuffer
.size() == 1)
4207 && (collectBuffer
.get(0) == '(')) {
4209 currentState
.g0Charset
= CharacterSet
.US
;
4211 if ((collectBuffer
.size() == 1)
4212 && (collectBuffer
.get(0) == ')')) {
4214 currentState
.g1Charset
= CharacterSet
.US
;
4216 if ((type
== DeviceType
.VT220
)
4217 || (type
== DeviceType
.XTERM
)) {
4219 if ((collectBuffer
.size() == 1)
4220 && (collectBuffer
.get(0) == '*')) {
4222 currentState
.g2Charset
= CharacterSet
.US
;
4224 if ((collectBuffer
.size() == 1)
4225 && (collectBuffer
.get(0) == '+')) {
4227 currentState
.g3Charset
= CharacterSet
.US
;
4232 if ((type
== DeviceType
.VT220
)
4233 || (type
== DeviceType
.XTERM
)) {
4235 if ((collectBuffer
.size() == 1)
4236 && (collectBuffer
.get(0) == '(')) {
4238 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4240 if ((collectBuffer
.size() == 1)
4241 && (collectBuffer
.get(0) == ')')) {
4243 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4245 if ((collectBuffer
.size() == 1)
4246 && (collectBuffer
.get(0) == '*')) {
4248 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4250 if ((collectBuffer
.size() == 1)
4251 && (collectBuffer
.get(0) == '+')) {
4253 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4260 if ((type
== DeviceType
.VT220
)
4261 || (type
== DeviceType
.XTERM
)) {
4263 if ((collectBuffer
.size() == 1)
4264 && (collectBuffer
.get(0) == '(')) {
4266 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4268 if ((collectBuffer
.size() == 1)
4269 && (collectBuffer
.get(0) == ')')) {
4271 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4273 if ((collectBuffer
.size() == 1)
4274 && (collectBuffer
.get(0) == '*')) {
4276 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4278 if ((collectBuffer
.size() == 1)
4279 && (collectBuffer
.get(0) == '+')) {
4281 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4286 if ((type
== DeviceType
.VT220
)
4287 || (type
== DeviceType
.XTERM
)) {
4289 if ((collectBuffer
.size() == 1)
4290 && (collectBuffer
.get(0) == ' ')) {
4297 if ((type
== DeviceType
.VT220
)
4298 || (type
== DeviceType
.XTERM
)) {
4300 if ((collectBuffer
.size() == 1)
4301 && (collectBuffer
.get(0) == ' ')) {
4308 if ((type
== DeviceType
.VT220
)
4309 || (type
== DeviceType
.XTERM
)) {
4311 if ((collectBuffer
.size() == 1)
4312 && (collectBuffer
.get(0) == '(')) {
4314 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4316 if ((collectBuffer
.size() == 1)
4317 && (collectBuffer
.get(0) == ')')) {
4319 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4321 if ((collectBuffer
.size() == 1)
4322 && (collectBuffer
.get(0) == '*')) {
4324 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4326 if ((collectBuffer
.size() == 1)
4327 && (collectBuffer
.get(0) == '+')) {
4329 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4337 if ((type
== DeviceType
.VT220
)
4338 || (type
== DeviceType
.XTERM
)) {
4340 if ((collectBuffer
.size() == 1)
4341 && (collectBuffer
.get(0) == '(')) {
4343 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4345 if ((collectBuffer
.size() == 1)
4346 && (collectBuffer
.get(0) == ')')) {
4348 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4350 if ((collectBuffer
.size() == 1)
4351 && (collectBuffer
.get(0) == '*')) {
4353 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4355 if ((collectBuffer
.size() == 1)
4356 && (collectBuffer
.get(0) == '+')) {
4358 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4369 if ((type
== DeviceType
.VT220
)
4370 || (type
== DeviceType
.XTERM
)) {
4372 if ((collectBuffer
.size() == 1)
4373 && (collectBuffer
.get(0) == '(')) {
4375 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4377 if ((collectBuffer
.size() == 1)
4378 && (collectBuffer
.get(0) == ')')) {
4380 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4382 if ((collectBuffer
.size() == 1)
4383 && (collectBuffer
.get(0) == '*')) {
4385 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4387 if ((collectBuffer
.size() == 1)
4388 && (collectBuffer
.get(0) == '+')) {
4390 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4395 if ((type
== DeviceType
.VT220
)
4396 || (type
== DeviceType
.XTERM
)) {
4398 if ((collectBuffer
.size() == 1)
4399 && (collectBuffer
.get(0) == '(')) {
4401 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4403 if ((collectBuffer
.size() == 1)
4404 && (collectBuffer
.get(0) == ')')) {
4406 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4408 if ((collectBuffer
.size() == 1)
4409 && (collectBuffer
.get(0) == '*')) {
4411 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
4413 if ((collectBuffer
.size() == 1)
4414 && (collectBuffer
.get(0) == '+')) {
4416 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
4428 if ((type
== DeviceType
.VT220
)
4429 || (type
== DeviceType
.XTERM
)) {
4431 if ((collectBuffer
.size() == 1)
4432 && (collectBuffer
.get(0) == '(')) {
4434 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
4436 if ((collectBuffer
.size() == 1)
4437 && (collectBuffer
.get(0) == ')')) {
4439 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
4441 if ((collectBuffer
.size() == 1)
4442 && (collectBuffer
.get(0) == '*')) {
4444 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
4446 if ((collectBuffer
.size() == 1)
4447 && (collectBuffer
.get(0) == '+')) {
4449 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
4454 if ((type
== DeviceType
.VT220
)
4455 || (type
== DeviceType
.XTERM
)) {
4457 if ((collectBuffer
.size() == 1)
4458 && (collectBuffer
.get(0) == '(')) {
4460 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
4462 if ((collectBuffer
.size() == 1)
4463 && (collectBuffer
.get(0) == ')')) {
4465 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
4467 if ((collectBuffer
.size() == 1)
4468 && (collectBuffer
.get(0) == '*')) {
4470 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
4472 if ((collectBuffer
.size() == 1)
4473 && (collectBuffer
.get(0) == '+')) {
4475 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
4522 // 0x9C goes to GROUND
4530 // 00-17, 19, 1C-1F --> execute
4532 handleControlChar(ch
);
4535 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
4536 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4538 scanState
= ScanState
.CSI_INTERMEDIATE
;
4541 // 30-39, 3B --> param, then switch to CSI_PARAM
4542 if ((ch
>= '0') && (ch
<= '9')) {
4544 scanState
= ScanState
.CSI_PARAM
;
4548 scanState
= ScanState
.CSI_PARAM
;
4551 // 3C-3F --> collect, then switch to CSI_PARAM
4552 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
4554 scanState
= ScanState
.CSI_PARAM
;
4557 // 40-7E --> dispatch, then switch to GROUND
4558 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
4561 // ICH - Insert character
4569 // CUD - Cursor down
4573 // CUF - Cursor forward
4577 // CUB - Cursor backward
4581 // CNL - Cursor down and to column 1
4582 if (type
== DeviceType
.XTERM
) {
4587 // CPL - Cursor up and to column 1
4588 if (type
== DeviceType
.XTERM
) {
4593 // CHA - Cursor to column # in current row
4594 if (type
== DeviceType
.XTERM
) {
4599 // CUP - Cursor position
4603 // CHT - Cursor forward X tab stops (default 1)
4604 if (type
== DeviceType
.XTERM
) {
4609 // ED - Erase in display
4613 // EL - Erase in line
4628 // DCH - Delete character
4635 // Scroll up X lines (default 1)
4636 if (type
== DeviceType
.XTERM
) {
4641 // Scroll down X lines (default 1)
4642 if (type
== DeviceType
.XTERM
) {
4651 if ((type
== DeviceType
.VT220
)
4652 || (type
== DeviceType
.XTERM
)) {
4654 // ECH - Erase character
4661 // CBT - Cursor backward X tab stops (default 1)
4662 if (type
== DeviceType
.XTERM
) {
4673 // HPA - Cursor to column # in current row. Same as CHA
4674 if (type
== DeviceType
.XTERM
) {
4679 // HPR - Cursor right. Same as CUF
4680 if (type
== DeviceType
.XTERM
) {
4685 // REP - Repeat last char X times
4686 if (type
== DeviceType
.XTERM
) {
4691 // DA - Device attributes
4695 // VPA - Cursor to row, current column.
4696 if (type
== DeviceType
.XTERM
) {
4701 // VPR - Cursor down. Same as CUD
4702 if (type
== DeviceType
.XTERM
) {
4707 // HVP - Horizontal and vertical position
4711 // TBC - Tabulation clear
4715 // Sets an ANSI or DEC private toggle
4719 if ((type
== DeviceType
.VT220
)
4720 || (type
== DeviceType
.XTERM
)) {
4722 // Printer functions
4730 // Sets an ANSI or DEC private toggle
4734 // SGR - Select graphics rendition
4738 // DSR - Device status report
4745 // DECLL - Load leds
4749 // DECSTBM - Set top and bottom margins
4753 // Save cursor (ANSI.SYS)
4754 if (type
== DeviceType
.XTERM
) {
4755 savedState
.cursorX
= currentState
.cursorX
;
4756 savedState
.cursorY
= currentState
.cursorY
;
4762 // Restore cursor (ANSI.SYS)
4763 if (type
== DeviceType
.XTERM
) {
4764 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
4771 // DECREQTPARM - Request terminal parameters
4787 // 0x9C goes to GROUND
4792 // 0x3A goes to CSI_IGNORE
4794 scanState
= ScanState
.CSI_IGNORE
;
4799 // 00-17, 19, 1C-1F --> execute
4801 handleControlChar(ch
);
4804 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
4805 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4807 scanState
= ScanState
.CSI_INTERMEDIATE
;
4810 // 30-39, 3B --> param
4811 if ((ch
>= '0') && (ch
<= '9')) {
4818 // 0x3A goes to CSI_IGNORE
4820 scanState
= ScanState
.CSI_IGNORE
;
4822 // 0x3C-3F goes to CSI_IGNORE
4823 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
4824 scanState
= ScanState
.CSI_IGNORE
;
4827 // 40-7E --> dispatch, then switch to GROUND
4828 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
4831 // ICH - Insert character
4839 // CUD - Cursor down
4843 // CUF - Cursor forward
4847 // CUB - Cursor backward
4851 // CNL - Cursor down and to column 1
4852 if (type
== DeviceType
.XTERM
) {
4857 // CPL - Cursor up and to column 1
4858 if (type
== DeviceType
.XTERM
) {
4863 // CHA - Cursor to column # in current row
4864 if (type
== DeviceType
.XTERM
) {
4869 // CUP - Cursor position
4873 // CHT - Cursor forward X tab stops (default 1)
4874 if (type
== DeviceType
.XTERM
) {
4879 // ED - Erase in display
4883 // EL - Erase in line
4898 // DCH - Delete character
4905 // Scroll up X lines (default 1)
4906 if (type
== DeviceType
.XTERM
) {
4911 // Scroll down X lines (default 1)
4912 if (type
== DeviceType
.XTERM
) {
4921 if ((type
== DeviceType
.VT220
)
4922 || (type
== DeviceType
.XTERM
)) {
4924 // ECH - Erase character
4931 // CBT - Cursor backward X tab stops (default 1)
4932 if (type
== DeviceType
.XTERM
) {
4943 // HPA - Cursor to column # in current row. Same as CHA
4944 if (type
== DeviceType
.XTERM
) {
4949 // HPR - Cursor right. Same as CUF
4950 if (type
== DeviceType
.XTERM
) {
4955 // REP - Repeat last char X times
4956 if (type
== DeviceType
.XTERM
) {
4961 // DA - Device attributes
4965 // VPA - Cursor to row, current column.
4966 if (type
== DeviceType
.XTERM
) {
4971 // VPR - Cursor down. Same as CUD
4972 if (type
== DeviceType
.XTERM
) {
4977 // HVP - Horizontal and vertical position
4981 // TBC - Tabulation clear
4985 // Sets an ANSI or DEC private toggle
4989 if ((type
== DeviceType
.VT220
)
4990 || (type
== DeviceType
.XTERM
)) {
4992 // Printer functions
5000 // Sets an ANSI or DEC private toggle
5004 // SGR - Select graphics rendition
5008 // DSR - Device status report
5015 // DECLL - Load leds
5019 // DECSTBM - Set top and bottom margins
5029 // DECREQTPARM - Request terminal parameters
5046 case CSI_INTERMEDIATE
:
5047 // 00-17, 19, 1C-1F --> execute
5049 handleControlChar(ch
);
5052 // 20-2F --> collect
5053 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5057 // 0x30-3F goes to CSI_IGNORE
5058 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5059 scanState
= ScanState
.CSI_IGNORE
;
5062 // 40-7E --> dispatch, then switch to GROUND
5063 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5115 if (((type
== DeviceType
.VT220
)
5116 || (type
== DeviceType
.XTERM
))
5117 && (collectBuffer
.get(collectBuffer
.size() - 1) == '\"')
5119 // DECSCL - compatibility level
5122 if ((type
== DeviceType
.XTERM
)
5123 && (collectBuffer
.get(collectBuffer
.size() - 1) == '!')
5125 // DECSTR - Soft terminal reset
5130 if (((type
== DeviceType
.VT220
)
5131 || (type
== DeviceType
.XTERM
))
5132 && (collectBuffer
.get(collectBuffer
.size() - 1) == '\"')
5160 // 00-17, 19, 1C-1F --> execute
5162 handleControlChar(ch
);
5165 // 20-2F --> collect
5166 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5170 // 40-7E --> ignore, then switch to GROUND
5171 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5175 // 20-3F, 7F --> ignore
5181 // 0x9C goes to GROUND
5186 // 0x1B 0x5C goes to GROUND
5191 if ((collectBuffer
.size() > 0)
5192 && (collectBuffer
.get(collectBuffer
.size() - 1) == 0x1B)) {
5197 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5198 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5200 scanState
= ScanState
.DCS_INTERMEDIATE
;
5203 // 30-39, 3B --> param, then switch to DCS_PARAM
5204 if ((ch
>= '0') && (ch
<= '9')) {
5206 scanState
= ScanState
.DCS_PARAM
;
5210 scanState
= ScanState
.DCS_PARAM
;
5213 // 3C-3F --> collect, then switch to DCS_PARAM
5214 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5216 scanState
= ScanState
.DCS_PARAM
;
5219 // 00-17, 19, 1C-1F, 7F --> ignore
5221 // 0x3A goes to DCS_IGNORE
5223 scanState
= ScanState
.DCS_IGNORE
;
5226 // 0x40-7E goes to DCS_PASSTHROUGH
5227 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5228 scanState
= ScanState
.DCS_PASSTHROUGH
;
5232 case DCS_INTERMEDIATE
:
5234 // 0x9C goes to GROUND
5239 // 0x1B 0x5C goes to GROUND
5244 if ((collectBuffer
.size() > 0) &&
5245 (collectBuffer
.get(collectBuffer
.size() - 1) == 0x1B)) {
5250 // 0x30-3F goes to DCS_IGNORE
5251 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5252 scanState
= ScanState
.DCS_IGNORE
;
5255 // 0x40-7E goes to DCS_PASSTHROUGH
5256 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5257 scanState
= ScanState
.DCS_PASSTHROUGH
;
5260 // 00-17, 19, 1C-1F, 7F --> ignore
5265 // 0x9C goes to GROUND
5270 // 0x1B 0x5C goes to GROUND
5275 if ((collectBuffer
.size() > 0) &&
5276 (collectBuffer
.get(collectBuffer
.size() - 1) == 0x1B)) {
5281 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5282 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5284 scanState
= ScanState
.DCS_INTERMEDIATE
;
5287 // 30-39, 3B --> param
5288 if ((ch
>= '0') && (ch
<= '9')) {
5295 // 00-17, 19, 1C-1F, 7F --> ignore
5297 // 0x3A, 3C-3F goes to DCS_IGNORE
5299 scanState
= ScanState
.DCS_IGNORE
;
5301 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5302 scanState
= ScanState
.DCS_IGNORE
;
5305 // 0x40-7E goes to DCS_PASSTHROUGH
5306 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5307 scanState
= ScanState
.DCS_PASSTHROUGH
;
5311 case DCS_PASSTHROUGH
:
5312 // 0x9C goes to GROUND
5317 // 0x1B 0x5C goes to GROUND
5322 if ((collectBuffer
.size() > 0)
5323 && (collectBuffer
.get(collectBuffer
.size() - 1) == 0x1B)
5329 // 00-17, 19, 1C-1F, 20-7E --> put
5337 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5340 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5349 // 00-17, 19, 1C-1F, 20-7F --> ignore
5351 // 0x9C goes to GROUND
5358 case SOSPMAPC_STRING
:
5359 // 00-17, 19, 1C-1F, 20-7F --> ignore
5361 // 0x9C goes to GROUND
5369 // Special case for Xterm: OSC can pass control characters
5370 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5374 // 00-17, 19, 1C-1F --> ignore
5376 // 20-7F --> osc_put
5377 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5381 // 0x9C goes to GROUND
5388 case VT52_DIRECT_CURSOR_ADDRESS
:
5389 // This is a special case for the VT52 sequence "ESC Y l c"
5390 if (collectBuffer
.size() == 0) {
5392 } else if (collectBuffer
.size() == 1) {
5393 // We've got the two characters, one in the buffer and the
5395 cursorPosition(collectBuffer
.get(0) - '\040', ch
- '\040');
5404 * Expose current cursor X to outside world.
5406 * @return current cursor X
5408 public final int getCursorX() {
5409 return currentState
.cursorX
;
5413 * Expose current cursor Y to outside world.
5415 * @return current cursor Y
5417 public final int getCursorY() {
5418 return currentState
.cursorY
;
5422 * Read function runs on a separate thread.
5425 boolean utf8
= false;
5426 boolean done
= false;
5428 if (type
== DeviceType
.XTERM
) {
5432 // available() will often return > 1, so we need to read in chunks to
5434 char [] readBufferUTF8
= null;
5435 byte [] readBuffer
= null;
5437 readBufferUTF8
= new char[128];
5439 readBuffer
= new byte[128];
5442 while (!done
&& !stopReaderThread
) {
5444 // We assume that if inputStream has bytes available, then
5445 // input won't block on read().
5446 int n
= inputStream
.available();
5448 // System.err.printf("available() %d\n", n); System.err.flush();
5450 if (readBufferUTF8
.length
< n
) {
5451 // The buffer wasn't big enough, make it huger
5452 int newSize
= Math
.max(readBufferUTF8
.length
* 2, n
);
5454 readBufferUTF8
= new char[newSize
];
5457 if (readBuffer
.length
< n
) {
5458 // The buffer wasn't big enough, make it huger
5459 int newSize
= Math
.max(readBuffer
.length
* 2, n
);
5460 readBuffer
= new byte[newSize
];
5466 rc
= input
.read(readBufferUTF8
, 0, n
);
5468 rc
= inputStream
.read(readBuffer
, 0, n
);
5470 // System.err.printf("read() %d\n", rc); System.err.flush();
5475 for (int i
= 0; i
< rc
; i
++) {
5478 ch
= readBufferUTF8
[i
];
5482 // Don't step on UI events
5483 synchronized (this) {
5489 // Wait 10 millis for more data
5492 // System.err.println("end while loop"); System.err.flush();
5493 } catch (InterruptedException e
) {
5495 } catch (IOException e
) {
5496 e
.printStackTrace();
5499 } // while ((done == false) && (stopReaderThread == false))
5501 // Close the input stream
5508 inputStream
.close();
5511 } catch (IOException e
) {
5512 e
.printStackTrace();
5515 // Let the rest of the world know that I am done.
5516 stopReaderThread
= true;
5518 // System.err.println("*** run() exiting..."); System.err.flush();