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 // Close the input stream
257 if (inputStream
!= null) {
260 } catch (IOException e
) {
270 } catch (IOException e
) {
279 // Tell the reader thread to stop looking at input.
280 if (stopReaderThread
== false) {
281 stopReaderThread
= true;
284 } catch (InterruptedException e
) {
289 // Close the output stream.
294 if (outputStream
!= null) {
296 outputStream
.close();
297 } catch (IOException e
) {
304 if (output
!= null) {
307 } catch (IOException e
) {
314 throw new IllegalArgumentException("Invalid device type: "
317 } // synchronized (this)
321 * When true, the reader thread is expected to exit.
323 private volatile boolean stopReaderThread
= false;
328 private Thread readerThread
= null;
331 * See if the reader thread is still running.
333 * @return if true, we are still connected to / reading from the remote
336 public final boolean isReading() {
337 return (!stopReaderThread
);
341 * The type of emulator to be.
343 private DeviceType type
= DeviceType
.VT102
;
346 * Obtain a new blank display line for an external user
347 * (e.g. TTerminalWindow).
349 * @return new blank line
351 public final DisplayLine
getBlankDisplayLine() {
352 return new DisplayLine(currentState
.attr
);
356 * The scrollback buffer characters + attributes.
358 private volatile List
<DisplayLine
> scrollback
;
361 * Get the scrollback buffer.
363 * @return the scrollback buffer
365 public final List
<DisplayLine
> getScrollbackBuffer() {
370 * The raw display buffer characters + attributes.
372 private volatile List
<DisplayLine
> display
;
375 * Get the display buffer.
377 * @return the display buffer
379 public final List
<DisplayLine
> getDisplayBuffer() {
384 * The terminal's input. For type == XTERM, this is an InputStreamReader
385 * with UTF-8 encoding.
387 private Reader input
;
390 * The terminal's raw InputStream. This is used for type != XTERM.
392 private volatile InputStream inputStream
;
395 * The terminal's output. For type == XTERM, this wraps an
396 * OutputStreamWriter with UTF-8 encoding.
398 private Writer output
;
401 * The terminal's raw OutputStream. This is used for type != XTERM.
403 private OutputStream outputStream
;
406 * Parser character scan states.
423 VT52_DIRECT_CURSOR_ADDRESS
427 * Current scanning state.
429 private ScanState scanState
;
432 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
433 * can't really use it in keypress() because we do not see number pad
434 * events from TKeypress.
436 private enum KeypadMode
{
442 * Arrow keys can emit three different sequences (DECCKM or VT52
445 private enum ArrowKeyMode
{
452 * Available character sets for GL, GR, G0, G1, G2, G3.
454 private enum CharacterSet
{
475 * Single-shift states used by the C1 control characters SS2 (0x8E) and
478 private enum Singleshift
{
485 * VT220+ lockshift states.
487 private enum LockshiftMode
{
497 * Physical display width. We start at 80x24, but the user can resize us
503 * Get the display width.
505 * @return the width (usually 80 or 132)
507 public final int getWidth() {
512 * Physical display height. We start at 80x24, but the user can resize
518 * Get the display height.
520 * @return the height (usually 24)
522 public final int getHeight() {
527 * Top margin of the scrolling region.
529 private int scrollRegionTop
;
532 * Bottom margin of the scrolling region.
534 private int scrollRegionBottom
;
537 * Right margin column number. This can be selected by the remote side
538 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
540 private int rightMargin
;
543 * Last character printed.
548 * VT100-style line wrapping: a character is placed in column 80 (or
549 * 132), but the line does NOT wrap until another character is written to
550 * column 1 of the next line, after which the cursor moves to column 2.
552 private boolean wrapLineFlag
;
555 * VT220 single shift flag.
557 private Singleshift singleshift
= Singleshift
.NONE
;
560 * true = insert characters, false = overwrite.
562 private boolean insertMode
= false;
565 * VT52 mode as selected by DECANM. True means VT52, false means
566 * ANSI. Default is ANSI.
568 private boolean vt52Mode
= false;
571 * Visible cursor (DECTCEM).
573 private boolean cursorVisible
= true;
576 * Get visible cursor flag.
578 * @return if true, the cursor is visible
580 public final boolean visibleCursor() {
581 return cursorVisible
;
585 * Screen title as set by the xterm OSC sequence. Lots of applications
586 * send a screenTitle regardless of whether it is an xterm client or not.
588 private String screenTitle
= "";
591 * Get the screen title as set by the xterm OSC sequence. Lots of
592 * applications send a screenTitle regardless of whether it is an xterm
595 * @return screen title
597 public final String
getScreenTitle() {
602 * Array of flags that have come in, e.g. '?' (DEC private mode), '=',
605 private List
<Character
> csiFlags
;
608 * Parameter characters being collected.
610 private List
<Integer
> csiParams
;
613 * Non-csi collect buffer.
615 private List
<Character
> collectBuffer
;
618 * When true, use the G1 character set.
620 private boolean shiftOut
= false;
623 * Horizontal tab stop locations.
625 private List
<Integer
> tabStops
;
628 * S8C1T. True means 8bit controls, false means 7bit controls.
630 private boolean s8c1t
= false;
633 * Printer mode. True means send all output to printer, which discards
636 private boolean printerControllerMode
= false;
639 * LMN line mode. If true, linefeed() puts the cursor on the first
640 * column of the next line. If false, linefeed() puts the cursor one
641 * line down on the current line. The default is false.
643 private boolean newLineMode
= false;
646 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
648 private ArrowKeyMode arrowKeyMode
;
651 * Whether number pad keys send VT100 or VT52, application or numeric
654 private KeypadMode keypadMode
;
657 * When true, the terminal is in 132-column mode (DECCOLM).
659 private boolean columns132
= false;
662 * true = reverse video. Set by DECSCNM.
664 private boolean reverseVideo
= false;
667 * false = echo characters locally.
669 private boolean fullDuplex
= true;
672 * DECSC/DECRC save/restore a subset of the total state. This class
673 * encapsulates those specific flags/modes.
675 private class SaveableState
{
678 * When true, cursor positions are relative to the scrolling region.
680 public boolean originMode
= false;
683 * The current editing X position.
685 public int cursorX
= 0;
688 * The current editing Y position.
690 public int cursorY
= 0;
693 * Which character set is currently selected in G0.
695 public CharacterSet g0Charset
= CharacterSet
.US
;
698 * Which character set is currently selected in G1.
700 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
703 * Which character set is currently selected in G2.
705 public CharacterSet g2Charset
= CharacterSet
.US
;
708 * Which character set is currently selected in G3.
710 public CharacterSet g3Charset
= CharacterSet
.US
;
713 * Which character set is currently selected in GR.
715 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
718 * The current drawing attributes.
720 public CellAttributes attr
;
725 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
730 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
735 public boolean lineWrap
= true;
740 public void reset() {
744 g0Charset
= CharacterSet
.US
;
745 g1Charset
= CharacterSet
.DRAWING
;
746 g2Charset
= CharacterSet
.US
;
747 g3Charset
= CharacterSet
.US
;
748 grCharset
= CharacterSet
.DRAWING
;
749 attr
= new CellAttributes();
750 glLockshift
= LockshiftMode
.NONE
;
751 grLockshift
= LockshiftMode
.NONE
;
756 * Copy attributes from another instance.
758 * @param that the other instance to match
760 public void setTo(final SaveableState that
) {
761 this.originMode
= that
.originMode
;
762 this.cursorX
= that
.cursorX
;
763 this.cursorY
= that
.cursorY
;
764 this.g0Charset
= that
.g0Charset
;
765 this.g1Charset
= that
.g1Charset
;
766 this.g2Charset
= that
.g2Charset
;
767 this.g3Charset
= that
.g3Charset
;
768 this.grCharset
= that
.grCharset
;
769 this.attr
= new CellAttributes();
770 this.attr
.setTo(that
.attr
);
771 this.glLockshift
= that
.glLockshift
;
772 this.grLockshift
= that
.grLockshift
;
773 this.lineWrap
= that
.lineWrap
;
777 * Public constructor.
779 public SaveableState() {
785 * The current terminal state.
787 private SaveableState currentState
;
790 * The last saved terminal state.
792 private SaveableState savedState
;
795 * Clear the CSI parameters and flags.
797 private void toGround() {
800 collectBuffer
.clear();
801 scanState
= ScanState
.GROUND
;
805 * Reset the tab stops list.
807 private void resetTabStops() {
809 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
810 tabStops
.add(new Integer(i
* 8));
815 * Reset the emulation state.
817 private void reset() {
819 currentState
= new SaveableState();
820 savedState
= new SaveableState();
821 scanState
= ScanState
.GROUND
;
825 scrollRegionBottom
= height
- 1;
826 rightMargin
= width
- 1;
828 arrowKeyMode
= ArrowKeyMode
.ANSI
;
829 keypadMode
= KeypadMode
.Numeric
;
830 wrapLineFlag
= false;
838 reverseVideo
= false;
840 cursorVisible
= true;
843 singleshift
= Singleshift
.NONE
;
845 printerControllerMode
= false;
855 * Public constructor.
857 * @param type one of the DeviceType constants to select VT100, VT102,
859 * @param inputStream an InputStream connected to the remote side. For
860 * type == XTERM, inputStrem is converted to a Reader with UTF-8
862 * @param outputStream an OutputStream connected to the remote user. For
863 * type == XTERM, outputStream is converted to a Writer with UTF-8
865 * @throws UnsupportedEncodingException if an exception is thrown when
866 * creating the InputStreamReader
868 public ECMA48(final DeviceType type
, final InputStream inputStream
,
869 final OutputStream outputStream
) throws UnsupportedEncodingException
{
871 assert (inputStream
!= null);
872 assert (outputStream
!= null);
874 csiFlags
= new ArrayList
<Character
>();
875 csiParams
= new ArrayList
<Integer
>();
876 collectBuffer
= new ArrayList
<Character
>();
877 tabStops
= new ArrayList
<Integer
>();
878 scrollback
= new LinkedList
<DisplayLine
>();
879 display
= new LinkedList
<DisplayLine
>();
882 this.inputStream
= inputStream
;
883 if (type
== DeviceType
.XTERM
) {
884 this.input
= new InputStreamReader(inputStream
, "UTF-8");
885 this.output
= new OutputStreamWriter(outputStream
, "UTF-8");
886 this.outputStream
= null;
889 this.outputStream
= outputStream
;
893 for (int i
= 0; i
< height
; i
++) {
894 display
.add(new DisplayLine(currentState
.attr
));
897 // Spin up the input reader
898 readerThread
= new Thread(this);
899 readerThread
.start();
903 * Append a new line to the bottom of the display, adding lines off the
904 * top to the scrollback buffer.
906 private void newDisplayLine() {
907 // Scroll the top line off into the scrollback buffer
908 scrollback
.add(display
.get(0));
910 DisplayLine line
= new DisplayLine(currentState
.attr
);
911 line
.setReverseColor(reverseVideo
);
916 * Wraps the current line.
918 private void wrapCurrentLine() {
919 if (currentState
.cursorY
== height
- 1) {
922 if (currentState
.cursorY
< height
- 1) {
923 currentState
.cursorY
++;
925 currentState
.cursorX
= 0;
929 * Handle a carriage return.
931 private void carriageReturn() {
932 currentState
.cursorX
= 0;
933 wrapLineFlag
= false;
937 * Reverse the color of the visible display.
939 private void invertDisplayColors() {
940 for (DisplayLine line
: display
) {
941 line
.setReverseColor(!line
.isReverseColor());
948 private void linefeed() {
951 if (currentState
.cursorY
< scrollRegionBottom
) {
952 // Increment screen y
953 currentState
.cursorY
++;
956 // Screen y does not increment
959 * Two cases: either we're inside a scrolling region or not. If
960 * the scrolling region bottom is the bottom of the screen, then
961 * push the top line into the buffer. Else scroll the scrolling
964 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
966 // We're at the bottom of the scroll region, AND the scroll
967 // region is the entire screen.
973 // We're at the bottom of the scroll region, AND the scroll
974 // region is NOT the entire screen.
975 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
980 currentState
.cursorX
= 0;
982 wrapLineFlag
= false;
986 * Prints one character to the display buffer.
988 * @param ch character to display
990 private void printCharacter(final char ch
) {
991 int rightMargin
= this.rightMargin
;
993 // Check if we have double-width, and if so chop at 40/66 instead of
995 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
996 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
999 // Check the unusually-complicated line wrapping conditions...
1000 if (currentState
.cursorX
== rightMargin
) {
1002 if (currentState
.lineWrap
== true) {
1004 * This case happens when: the cursor was already on the
1005 * right margin (either through printing or by an explicit
1006 * placement command), and a character was printed.
1008 * The line wraps only when a new character arrives AND the
1009 * cursor is already on the right margin AND has placed a
1010 * character in its cell. Easier to see than to explain.
1012 if (wrapLineFlag
== false) {
1014 * This block marks the case that we are in the margin
1015 * and the first character has been received and printed.
1017 wrapLineFlag
= true;
1020 * This block marks the case that we are in the margin
1021 * and the second character has been received and
1024 wrapLineFlag
= false;
1028 } else if (currentState
.cursorX
<= rightMargin
) {
1030 * This is the normal case: a character came in and was printed
1031 * to the left of the right margin column.
1034 // Turn off VT100 special-case flag
1035 wrapLineFlag
= false;
1038 // "Print" the character
1039 Cell newCell
= new Cell(ch
);
1040 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1041 newCellAttributes
.setTo(currentState
.attr
);
1042 DisplayLine line
= display
.get(currentState
.cursorY
);
1043 // Insert mode special case
1044 if (insertMode
== true) {
1045 line
.insert(currentState
.cursorX
, newCell
);
1047 // Replace an existing character
1048 line
.replace(currentState
.cursorX
, newCell
);
1051 // Increment horizontal
1052 if (wrapLineFlag
== false) {
1053 currentState
.cursorX
++;
1054 if (currentState
.cursorX
> rightMargin
) {
1055 currentState
.cursorX
--;
1061 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1062 * send to the remote side.
1064 * @param keypress keypress received from the local user
1066 public void keypress(final TKeypress keypress
) {
1067 writeRemote(keypressToString(keypress
));
1071 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1073 * @param keypress keypress received from the local user
1074 * @return string to transmit to the remote side
1076 private String
keypressToString(final TKeypress keypress
) {
1078 if ((fullDuplex
== false) && (!keypress
.getIsKey())) {
1080 * If this is a control character, process it like it came from
1083 if (keypress
.getCh() < 0x20) {
1084 handleControlChar(keypress
.getCh());
1086 // Local echo for everything else
1087 printCharacter(keypress
.getCh());
1091 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1096 // Handle control characters
1097 if ((keypress
.getCtrl()) && (!keypress
.getIsKey())) {
1098 StringBuilder sb
= new StringBuilder();
1099 char ch
= keypress
.getCh();
1102 return sb
.toString();
1105 // Handle alt characters
1106 if ((keypress
.getAlt()) && (!keypress
.getIsKey())) {
1107 StringBuilder sb
= new StringBuilder("\033");
1108 char ch
= keypress
.getCh();
1110 return sb
.toString();
1113 if (keypress
.equals(kbBackspace
)) {
1126 if (keypress
.equals(kbLeft
)) {
1127 switch (arrowKeyMode
) {
1137 if (keypress
.equals(kbRight
)) {
1138 switch (arrowKeyMode
) {
1148 if (keypress
.equals(kbUp
)) {
1149 switch (arrowKeyMode
) {
1159 if (keypress
.equals(kbDown
)) {
1160 switch (arrowKeyMode
) {
1170 if (keypress
.equals(kbHome
)) {
1171 switch (arrowKeyMode
) {
1181 if (keypress
.equals(kbEnd
)) {
1182 switch (arrowKeyMode
) {
1192 if (keypress
.equals(kbF1
)) {
1200 if (keypress
.equals(kbF2
)) {
1208 if (keypress
.equals(kbF3
)) {
1216 if (keypress
.equals(kbF4
)) {
1224 if (keypress
.equals(kbF5
)) {
1237 if (keypress
.equals(kbF6
)) {
1250 if (keypress
.equals(kbF7
)) {
1263 if (keypress
.equals(kbF8
)) {
1276 if (keypress
.equals(kbF9
)) {
1289 if (keypress
.equals(kbF10
)) {
1302 if (keypress
.equals(kbF11
)) {
1306 if (keypress
.equals(kbF12
)) {
1310 if (keypress
.equals(kbShiftF1
)) {
1318 if (keypress
.equals(kbShiftF2
)) {
1326 if (keypress
.equals(kbShiftF3
)) {
1334 if (keypress
.equals(kbShiftF4
)) {
1342 if (keypress
.equals(kbShiftF5
)) {
1344 return "\033[15;2~";
1347 if (keypress
.equals(kbShiftF6
)) {
1349 return "\033[17;2~";
1352 if (keypress
.equals(kbShiftF7
)) {
1354 return "\033[18;2~";
1357 if (keypress
.equals(kbShiftF8
)) {
1359 return "\033[19;2~";
1362 if (keypress
.equals(kbShiftF9
)) {
1364 return "\033[20;2~";
1367 if (keypress
.equals(kbShiftF10
)) {
1369 return "\033[21;2~";
1372 if (keypress
.equals(kbShiftF11
)) {
1374 return "\033[23;2~";
1377 if (keypress
.equals(kbShiftF12
)) {
1379 return "\033[24;2~";
1382 if (keypress
.equals(kbCtrlF1
)) {
1390 if (keypress
.equals(kbCtrlF2
)) {
1398 if (keypress
.equals(kbCtrlF3
)) {
1406 if (keypress
.equals(kbCtrlF4
)) {
1414 if (keypress
.equals(kbCtrlF5
)) {
1416 return "\033[15;5~";
1419 if (keypress
.equals(kbCtrlF6
)) {
1421 return "\033[17;5~";
1424 if (keypress
.equals(kbCtrlF7
)) {
1426 return "\033[18;5~";
1429 if (keypress
.equals(kbCtrlF8
)) {
1431 return "\033[19;5~";
1434 if (keypress
.equals(kbCtrlF9
)) {
1436 return "\033[20;5~";
1439 if (keypress
.equals(kbCtrlF10
)) {
1441 return "\033[21;5~";
1444 if (keypress
.equals(kbCtrlF11
)) {
1446 return "\033[23;5~";
1449 if (keypress
.equals(kbCtrlF12
)) {
1451 return "\033[24;5~";
1454 if (keypress
.equals(kbPgUp
)) {
1459 if (keypress
.equals(kbPgDn
)) {
1464 if (keypress
.equals(kbIns
)) {
1469 if (keypress
.equals(kbShiftIns
)) {
1470 // This is what xterm sends for SHIFT-INS
1472 // This is what xterm sends for CTRL-INS
1473 // return "\033[2;5~";
1476 if (keypress
.equals(kbShiftDel
)) {
1477 // This is what xterm sends for SHIFT-DEL
1479 // This is what xterm sends for CTRL-DEL
1480 // return "\033[3;5~";
1483 if (keypress
.equals(kbDel
)) {
1484 // Delete sends real delete for VTxxx
1486 // return "\033[3~";
1489 if (keypress
.equals(kbEnter
)) {
1493 if (keypress
.equals(kbEsc
)) {
1497 if (keypress
.equals(kbAltEsc
)) {
1501 if (keypress
.equals(kbTab
)) {
1505 // Non-alt, non-ctrl characters
1506 if (!keypress
.getIsKey()) {
1507 StringBuilder sb
= new StringBuilder();
1508 sb
.append(keypress
.getCh());
1509 return sb
.toString();
1515 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1518 * @param ch 8-bit character from the remote side
1519 * @param charsetGl character set defined for GL
1520 * @param charsetGr character set defined for GR
1521 * @return character to display on the screen
1523 private char mapCharacterCharset(final char ch
,
1524 final CharacterSet charsetGl
,
1525 final CharacterSet charsetGr
) {
1527 int lookupChar
= ch
;
1528 CharacterSet lookupCharset
= charsetGl
;
1531 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
1532 lookupCharset
= charsetGr
;
1536 switch (lookupCharset
) {
1539 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
1542 return DECCharacterSets
.UK
[lookupChar
];
1545 return DECCharacterSets
.US_ASCII
[lookupChar
];
1548 return DECCharacterSets
.NL
[lookupChar
];
1551 return DECCharacterSets
.FI
[lookupChar
];
1554 return DECCharacterSets
.FR
[lookupChar
];
1557 return DECCharacterSets
.FR_CA
[lookupChar
];
1560 return DECCharacterSets
.DE
[lookupChar
];
1563 return DECCharacterSets
.IT
[lookupChar
];
1566 return DECCharacterSets
.NO
[lookupChar
];
1569 return DECCharacterSets
.ES
[lookupChar
];
1572 return DECCharacterSets
.SV
[lookupChar
];
1575 return DECCharacterSets
.SWISS
[lookupChar
];
1577 case DEC_SUPPLEMENTAL
:
1578 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
1581 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
1584 return DECCharacterSets
.US_ASCII
[lookupChar
];
1587 return DECCharacterSets
.US_ASCII
[lookupChar
];
1590 throw new IllegalArgumentException("Invalid character set value: "
1596 * Map an 8-bit byte into a printable character.
1598 * @param ch either 8-bit or Unicode character from the remote side
1599 * @return character to display on the screen
1601 private char mapCharacter(final char ch
) {
1603 // Unicode character, just return it
1607 CharacterSet charsetGl
= currentState
.g0Charset
;
1608 CharacterSet charsetGr
= currentState
.grCharset
;
1610 if (vt52Mode
== true) {
1611 if (shiftOut
== true) {
1612 // Shifted out character, pull from VT52 graphics
1613 charsetGl
= currentState
.g1Charset
;
1614 charsetGr
= CharacterSet
.US
;
1617 charsetGl
= currentState
.g0Charset
;
1618 charsetGr
= CharacterSet
.US
;
1621 // Pull the character
1622 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1626 if (shiftOut
== true) {
1627 // Shifted out character, pull from G1
1628 charsetGl
= currentState
.g1Charset
;
1629 charsetGr
= currentState
.grCharset
;
1631 // Pull the character
1632 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1636 if (singleshift
== Singleshift
.SS2
) {
1638 singleshift
= Singleshift
.NONE
;
1640 // Shifted out character, pull from G2
1641 charsetGl
= currentState
.g2Charset
;
1642 charsetGr
= currentState
.grCharset
;
1646 if (singleshift
== Singleshift
.SS3
) {
1648 singleshift
= Singleshift
.NONE
;
1650 // Shifted out character, pull from G3
1651 charsetGl
= currentState
.g3Charset
;
1652 charsetGr
= currentState
.grCharset
;
1655 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
1656 // Check for locking shift
1658 switch (currentState
.glLockshift
) {
1671 charsetGl
= currentState
.g2Charset
;
1676 charsetGl
= currentState
.g3Charset
;
1681 charsetGl
= currentState
.g0Charset
;
1685 switch (currentState
.grLockshift
) {
1695 charsetGr
= currentState
.g1Charset
;
1700 charsetGr
= currentState
.g2Charset
;
1705 charsetGr
= currentState
.g3Charset
;
1710 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
1717 // Pull the character
1718 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1722 * Scroll the text within a scrolling region up n lines.
1724 * @param regionTop top row of the scrolling region
1725 * @param regionBottom bottom row of the scrolling region
1726 * @param n number of lines to scroll
1728 private void scrollingRegionScrollUp(final int regionTop
,
1729 final int regionBottom
, final int n
) {
1731 if (regionTop
>= regionBottom
) {
1735 // Sanity check: see if there will be any characters left after the
1737 if (regionBottom
+ 1 - regionTop
<= n
) {
1738 // There won't be anything left in the region, so just call
1739 // eraseScreen() and return.
1740 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1744 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1745 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1746 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1748 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
1749 - remaining
, regionBottom
+ 1);
1750 display
= new LinkedList
<DisplayLine
>(displayTop
);
1751 display
.addAll(displayMiddle
);
1752 for (int i
= 0; i
< n
; i
++) {
1753 DisplayLine line
= new DisplayLine(currentState
.attr
);
1754 line
.setReverseColor(reverseVideo
);
1757 display
.addAll(displayBottom
);
1759 assert (display
.size() == height
);
1763 * Scroll the text within a scrolling region down n lines.
1765 * @param regionTop top row of the scrolling region
1766 * @param regionBottom bottom row of the scrolling region
1767 * @param n number of lines to scroll
1769 private void scrollingRegionScrollDown(final int regionTop
,
1770 final int regionBottom
, final int n
) {
1772 if (regionTop
>= regionBottom
) {
1776 // Sanity check: see if there will be any characters left after the
1778 if (regionBottom
+ 1 - regionTop
<= n
) {
1779 // There won't be anything left in the region, so just call
1780 // eraseScreen() and return.
1781 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1785 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1786 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1787 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1789 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
1790 regionTop
+ remaining
);
1791 display
= new LinkedList
<DisplayLine
>(displayTop
);
1792 for (int i
= 0; i
< n
; i
++) {
1793 DisplayLine line
= new DisplayLine(currentState
.attr
);
1794 line
.setReverseColor(reverseVideo
);
1797 display
.addAll(displayMiddle
);
1798 display
.addAll(displayBottom
);
1800 assert (display
.size() == height
);
1804 * Process a control character.
1806 * @param ch 8-bit character from the remote side
1808 private void handleControlChar(final char ch
) {
1809 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
1820 // Transmit the answerback message.
1831 cursorLeft(1, false);
1836 advanceToNextTabStop();
1862 currentState
.glLockshift
= LockshiftMode
.NONE
;
1868 currentState
.glLockshift
= LockshiftMode
.NONE
;
1893 singleshift
= Singleshift
.SS2
;
1898 singleshift
= Singleshift
.SS3
;
1908 * Advance the cursor to the next tab stop.
1910 private void advanceToNextTabStop() {
1911 if (tabStops
.size() == 0) {
1912 // Go to the rightmost column
1913 cursorRight(rightMargin
- currentState
.cursorX
, false);
1916 for (Integer stop
: tabStops
) {
1917 if (stop
> currentState
.cursorX
) {
1918 cursorRight(stop
- currentState
.cursorX
, false);
1923 * We got here, meaning there isn't a tab stop beyond the current
1924 * cursor position. Place the cursor of the right-most edge of the
1927 cursorRight(rightMargin
- currentState
.cursorX
, false);
1931 * Save a character into the collect buffer.
1933 * @param ch character to save
1935 private void collect(final char ch
) {
1936 collectBuffer
.add(new Character(ch
));
1940 * Save a byte into the CSI parameters buffer.
1942 * @param ch byte to save
1944 private void param(final byte ch
) {
1945 if (csiParams
.size() == 0) {
1946 csiParams
.add(new Integer(0));
1948 Integer x
= csiParams
.get(csiParams
.size() - 1);
1949 if ((ch
>= '0') && (ch
<= '9')) {
1952 csiParams
.set(csiParams
.size() - 1, x
);
1956 csiParams
.add(new Integer(0));
1961 * Get a CSI parameter value, with a default.
1963 * @param position parameter index. 0 is the first parameter.
1964 * @param defaultValue value to use if csiParams[position] doesn't exist
1965 * @return parameter value
1967 private int getCsiParam(final int position
, final int defaultValue
) {
1968 if (csiParams
.size() < position
+ 1) {
1969 return defaultValue
;
1971 return csiParams
.get(position
).intValue();
1975 * Get a CSI parameter value, clamped to within min/max.
1977 * @param position parameter index. 0 is the first parameter.
1978 * @param defaultValue value to use if csiParams[position] doesn't exist
1979 * @param minValue minimum value inclusive
1980 * @param maxValue maximum value inclusive
1981 * @return parameter value
1983 private int getCsiParam(final int position
, final int defaultValue
,
1984 final int minValue
, final int maxValue
) {
1986 assert (minValue
<= maxValue
);
1987 int value
= getCsiParam(position
, defaultValue
);
1988 if (value
< minValue
) {
1991 if (value
> maxValue
) {
1998 * Set or unset a toggle. value is 'true' for set ('h'), false for reset
2001 private void setToggle(final boolean value
) {
2002 boolean decPrivateModeFlag
= false;
2003 for (Character ch
: collectBuffer
) {
2005 decPrivateModeFlag
= true;
2009 for (Integer i
: csiParams
) {
2014 if (decPrivateModeFlag
== true) {
2016 if (value
== true) {
2017 // Use application arrow keys
2018 arrowKeyMode
= ArrowKeyMode
.VT100
;
2020 // Use ANSI arrow keys
2021 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2026 if (decPrivateModeFlag
== true) {
2027 if (value
== false) {
2031 arrowKeyMode
= ArrowKeyMode
.VT52
;
2034 * From the VT102 docs: "You use ANSI mode to select
2035 * most terminal features; the terminal uses the same
2036 * features when it switches to VT52 mode. You
2037 * cannot, however, change most of these features in
2040 * In other words, do not reset any other attributes
2041 * when switching between VT52 submode and ANSI.
2043 * HOWEVER, the real vt100 does switch the character
2044 * set according to Usenet.
2046 currentState
.g0Charset
= CharacterSet
.US
;
2047 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2050 if ((type
== DeviceType
.VT220
)
2051 || (type
== DeviceType
.XTERM
)) {
2053 // VT52 mode is explicitly 7-bit
2055 singleshift
= Singleshift
.NONE
;
2060 if (value
== true) {
2061 // Turn off keyboard
2070 if (decPrivateModeFlag
== true) {
2072 if (value
== true) {
2081 width
= rightMargin
+ 1;
2082 // Entire screen is cleared, and scrolling region is
2084 eraseScreen(0, 0, height
- 1, width
- 1, false);
2085 scrollRegionTop
= 0;
2086 scrollRegionBottom
= height
- 1;
2087 // Also home the cursor
2088 cursorPosition(0, 0);
2092 if (decPrivateModeFlag
== true) {
2094 if (value
== true) {
2103 if (value
== true) {
2111 if (decPrivateModeFlag
== true) {
2113 if (value
== true) {
2115 * Set selects reverse screen, a white screen
2116 * background with black characters.
2118 if (reverseVideo
!= true) {
2120 * If in normal video, switch it back
2122 invertDisplayColors();
2124 reverseVideo
= true;
2127 * Reset selects normal screen, a black screen
2128 * background with white characters.
2130 if (reverseVideo
== true) {
2132 * If in reverse video already, switch it back
2134 invertDisplayColors();
2136 reverseVideo
= false;
2141 if (decPrivateModeFlag
== true) {
2143 if (value
== true) {
2144 // Origin is relative to scroll region cursor.
2145 // Cursor can NEVER leave scrolling region.
2146 currentState
.originMode
= true;
2147 cursorPosition(0, 0);
2149 // Origin is absolute to entire screen. Cursor can
2150 // leave the scrolling region via cup() and hvp().
2151 currentState
.originMode
= false;
2152 cursorPosition(0, 0);
2157 if (decPrivateModeFlag
== true) {
2159 if (value
== true) {
2161 currentState
.lineWrap
= true;
2163 // Turn linewrap off
2164 currentState
.lineWrap
= false;
2169 if (decPrivateModeFlag
== true) {
2171 if (value
== true) {
2172 // Keyboard auto-repeat on
2175 // Keyboard auto-repeat off
2181 if (decPrivateModeFlag
== false) {
2183 if (value
== true) {
2193 if (decPrivateModeFlag
== true) {
2199 if (decPrivateModeFlag
== true) {
2205 if (decPrivateModeFlag
== false) {
2207 if (value
== true) {
2209 * Set causes a received linefeed, form feed, or
2210 * vertical tab to move cursor to first column of
2211 * next line. RETURN transmits both a carriage return
2212 * and linefeed. This selection is also called new
2218 * Reset causes a received linefeed, form feed, or
2219 * vertical tab to move cursor to next line in
2220 * current column. RETURN transmits a carriage
2223 newLineMode
= false;
2229 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2230 if (decPrivateModeFlag
== true) {
2232 if (value
== true) {
2234 cursorVisible
= true;
2237 cursorVisible
= false;
2244 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2245 if (decPrivateModeFlag
== true) {
2247 if (value
== true) {
2248 // Select national mode NRC
2251 // Select multi-national mode
2267 * DECSC - Save cursor.
2269 private void decsc() {
2270 savedState
.setTo(currentState
);
2274 * DECRC - Restore cursor.
2276 private void decrc() {
2277 currentState
.setTo(savedState
);
2283 private void ind() {
2284 // Move the cursor and scroll if necessary. If at the bottom line
2285 // already, a scroll up is supposed to be performed.
2286 if (currentState
.cursorY
== scrollRegionBottom
) {
2287 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2289 cursorDown(1, true);
2293 * RI - Reverse index.
2296 // Move the cursor and scroll if necessary. If at the top line
2297 // already, a scroll down is supposed to be performed.
2298 if (currentState
.cursorY
== scrollRegionTop
) {
2299 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2307 private void nel() {
2308 // Move the cursor and scroll if necessary. If at the bottom line
2309 // already, a scroll up is supposed to be performed.
2310 if (currentState
.cursorY
== scrollRegionBottom
) {
2311 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2313 cursorDown(1, true);
2315 // Reset to the beginning of the next line
2316 currentState
.cursorX
= 0;
2320 * DECKPAM - Keypad application mode.
2322 private void deckpam() {
2323 keypadMode
= KeypadMode
.Application
;
2327 * DECKPNM - Keypad numeric mode.
2329 private void deckpnm() {
2330 keypadMode
= KeypadMode
.Numeric
;
2336 * @param n number of spaces to move
2337 * @param honorScrollRegion if true, then do nothing if the cursor is
2338 * outside the scrolling region
2340 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2344 * Special case: if a user moves the cursor from the right margin, we
2345 * have to reset the VT100 right margin flag.
2348 wrapLineFlag
= false;
2351 for (int i
= 0; i
< n
; i
++) {
2352 if (honorScrollRegion
== true) {
2353 // Honor the scrolling region
2354 if ((currentState
.cursorY
< scrollRegionTop
)
2355 || (currentState
.cursorY
> scrollRegionBottom
)
2357 // Outside region, do nothing
2360 // Inside region, go up
2361 top
= scrollRegionTop
;
2363 // Non-scrolling case
2367 if (currentState
.cursorY
> top
) {
2368 currentState
.cursorY
--;
2374 * Move down n spaces.
2376 * @param n number of spaces to move
2377 * @param honorScrollRegion if true, then do nothing if the cursor is
2378 * outside the scrolling region
2380 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
2384 * Special case: if a user moves the cursor from the right margin, we
2385 * have to reset the VT100 right margin flag.
2388 wrapLineFlag
= false;
2391 for (int i
= 0; i
< n
; i
++) {
2393 if (honorScrollRegion
== true) {
2394 // Honor the scrolling region
2395 if (currentState
.cursorY
> scrollRegionBottom
) {
2396 // Outside region, do nothing
2399 // Inside region, go down
2400 bottom
= scrollRegionBottom
;
2402 // Non-scrolling case
2403 bottom
= height
- 1;
2406 if (currentState
.cursorY
< bottom
) {
2407 currentState
.cursorY
++;
2413 * Move left n spaces.
2415 * @param n number of spaces to move
2416 * @param honorScrollRegion if true, then do nothing if the cursor is
2417 * outside the scrolling region
2419 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
2421 * Special case: if a user moves the cursor from the right margin, we
2422 * have to reset the VT100 right margin flag.
2425 wrapLineFlag
= false;
2428 for (int i
= 0; i
< n
; i
++) {
2429 if (honorScrollRegion
== true) {
2430 // Honor the scrolling region
2431 if ((currentState
.cursorY
< scrollRegionTop
)
2432 || (currentState
.cursorY
> scrollRegionBottom
)
2434 // Outside region, do nothing
2439 if (currentState
.cursorX
> 0) {
2440 currentState
.cursorX
--;
2446 * Move right n spaces.
2448 * @param n number of spaces to move
2449 * @param honorScrollRegion if true, then do nothing if the cursor is
2450 * outside the scrolling region
2452 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
2453 int rightMargin
= this.rightMargin
;
2456 * Special case: if a user moves the cursor from the right margin, we
2457 * have to reset the VT100 right margin flag.
2460 wrapLineFlag
= false;
2463 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2464 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2467 for (int i
= 0; i
< n
; i
++) {
2468 if (honorScrollRegion
== true) {
2469 // Honor the scrolling region
2470 if ((currentState
.cursorY
< scrollRegionTop
)
2471 || (currentState
.cursorY
> scrollRegionBottom
)
2473 // Outside region, do nothing
2478 if (currentState
.cursorX
< rightMargin
) {
2479 currentState
.cursorX
++;
2485 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2487 * @param row row to move to
2488 * @param col column to move to
2490 private void cursorPosition(int row
, final int col
) {
2491 int rightMargin
= this.rightMargin
;
2496 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2497 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2500 // Set column number
2501 currentState
.cursorX
= col
;
2503 // Sanity check, bring column back to margin.
2504 if (currentState
.cursorX
> rightMargin
) {
2505 currentState
.cursorX
= rightMargin
;
2509 if (currentState
.originMode
== true) {
2510 row
+= scrollRegionTop
;
2512 if (currentState
.cursorY
< row
) {
2513 cursorDown(row
- currentState
.cursorY
, false);
2514 } else if (currentState
.cursorY
> row
) {
2515 cursorUp(currentState
.cursorY
- row
, false);
2518 wrapLineFlag
= false;
2522 * HTS - Horizontal tabulation set.
2524 private void hts() {
2525 for (Integer stop
: tabStops
) {
2526 if (stop
== currentState
.cursorX
) {
2527 // Already have a tab stop here
2532 // Append a tab stop to the end of the array and resort them
2533 tabStops
.add(currentState
.cursorX
);
2534 Collections
.sort(tabStops
);
2538 * DECSWL - Single-width line.
2540 private void decswl() {
2541 display
.get(currentState
.cursorY
).setDoubleWidth(false);
2542 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2546 * DECDWL - Double-width line.
2548 private void decdwl() {
2549 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2550 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2554 * DECHDL - Double-height + double-width line.
2556 * @param topHalf if true, this sets the row to be the top half row of a
2559 private void dechdl(final boolean topHalf
) {
2560 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2561 if (topHalf
== true) {
2562 display
.get(currentState
.cursorY
).setDoubleHeight(1);
2564 display
.get(currentState
.cursorY
).setDoubleHeight(2);
2569 * DECALN - Screen alignment display.
2571 private void decaln() {
2572 Cell newCell
= new Cell();
2573 newCell
.setChar('E');
2574 for (DisplayLine line
: display
) {
2575 for (int i
= 0; i
< line
.length(); i
++) {
2576 line
.replace(i
, newCell
);
2582 * DECSCL - Compatibility level.
2584 private void decscl() {
2585 int i
= getCsiParam(0, 0);
2586 int j
= getCsiParam(1, 0);
2590 currentState
.g0Charset
= CharacterSet
.US
;
2591 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2593 } else if (i
== 62) {
2595 if ((j
== 0) || (j
== 2)) {
2597 } else if (j
== 1) {
2604 * CUD - Cursor down.
2606 private void cud() {
2607 cursorDown(getCsiParam(0, 1, 1, height
), true);
2611 * CUF - Cursor forward.
2613 private void cuf() {
2614 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
2618 * CUB - Cursor backward.
2620 private void cub() {
2621 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
2627 private void cuu() {
2628 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2632 * CUP - Cursor position.
2634 private void cup() {
2635 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2636 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
2640 * CNL - Cursor down and to column 1.
2642 private void cnl() {
2643 cursorDown(getCsiParam(0, 1, 1, height
), true);
2645 cursorLeft(currentState
.cursorX
, true);
2649 * CPL - Cursor up and to column 1.
2651 private void cpl() {
2652 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2654 cursorLeft(currentState
.cursorX
, true);
2658 * CHA - Cursor to column # in current row.
2660 private void cha() {
2661 cursorPosition(currentState
.cursorY
,
2662 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
2666 * VPA - Cursor to row #, same column.
2668 private void vpa() {
2669 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2670 currentState
.cursorX
);
2674 * ED - Erase in display.
2677 boolean honorProtected
= false;
2678 boolean decPrivateModeFlag
= false;
2680 for (Character ch
: collectBuffer
) {
2682 decPrivateModeFlag
= true;
2686 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2687 && (decPrivateModeFlag
== true)
2689 honorProtected
= true;
2692 int i
= getCsiParam(0, 0);
2695 // Erase from here to end of screen
2696 if (currentState
.cursorY
< height
- 1) {
2697 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
2700 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2701 } else if (i
== 1) {
2702 // Erase from beginning of screen to here
2703 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
2705 eraseLine(0, currentState
.cursorX
, honorProtected
);
2706 } else if (i
== 2) {
2707 // Erase entire screen
2708 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
2713 * EL - Erase in line.
2716 boolean honorProtected
= false;
2717 boolean decPrivateModeFlag
= false;
2719 for (Character ch
: collectBuffer
) {
2721 decPrivateModeFlag
= true;
2725 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2726 && (decPrivateModeFlag
== true)
2728 honorProtected
= true;
2731 int i
= getCsiParam(0, 0);
2734 // Erase from here to end of line
2735 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2736 } else if (i
== 1) {
2737 // Erase from beginning of line to here
2738 eraseLine(0, currentState
.cursorX
, honorProtected
);
2739 } else if (i
== 2) {
2740 // Erase entire line
2741 eraseLine(0, width
- 1, honorProtected
);
2746 * ECH - Erase # of characters in current row.
2748 private void ech() {
2749 int i
= getCsiParam(0, 1, 1, width
);
2751 // Erase from here to i characters
2752 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
2759 int i
= getCsiParam(0, 1);
2761 if ((currentState
.cursorY
>= scrollRegionTop
)
2762 && (currentState
.cursorY
<= scrollRegionBottom
)
2765 // I can get the same effect with a scroll-down
2766 scrollingRegionScrollDown(currentState
.cursorY
,
2767 scrollRegionBottom
, i
);
2772 * DCH - Delete char.
2774 private void dch() {
2775 int n
= getCsiParam(0, 1);
2776 DisplayLine line
= display
.get(currentState
.cursorY
);
2777 Cell blank
= new Cell();
2778 for (int i
= 0; i
< n
; i
++) {
2779 line
.delete(currentState
.cursorX
, blank
);
2784 * ICH - Insert blank char at cursor.
2786 private void ich() {
2787 int n
= getCsiParam(0, 1);
2788 DisplayLine line
= display
.get(currentState
.cursorY
);
2789 Cell blank
= new Cell();
2790 for (int i
= 0; i
< n
; i
++) {
2791 line
.insert(currentState
.cursorX
, blank
);
2799 int i
= getCsiParam(0, 1);
2801 if ((currentState
.cursorY
>= scrollRegionTop
)
2802 && (currentState
.cursorY
<= scrollRegionBottom
)) {
2804 // I can get the same effect with a scroll-down
2805 scrollingRegionScrollUp(currentState
.cursorY
,
2806 scrollRegionBottom
, i
);
2811 * HVP - Horizontal and vertical position.
2813 private void hvp() {
2818 * REP - Repeat character.
2820 private void rep() {
2821 int n
= getCsiParam(0, 1);
2822 for (int i
= 0; i
< n
; i
++) {
2823 printCharacter(repCh
);
2831 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
2832 getCsiParam(0, 1, 1, height
));
2839 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
2840 getCsiParam(0, 1, 1, height
));
2844 * CBT - Go back X tab stops.
2846 private void cbt() {
2847 int tabsToMove
= getCsiParam(0, 1);
2850 for (int i
= 0; i
< tabsToMove
; i
++) {
2851 int j
= currentState
.cursorX
;
2852 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
2853 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
2861 j
= tabStops
.get(tabI
);
2863 cursorPosition(currentState
.cursorY
, j
);
2868 * CHT - Advance X tab stops.
2870 private void cht() {
2871 int n
= getCsiParam(0, 1);
2872 for (int i
= 0; i
< n
; i
++) {
2873 advanceToNextTabStop();
2878 * SGR - Select graphics rendition.
2880 private void sgr() {
2882 if (csiParams
.size() == 0) {
2883 currentState
.attr
.reset();
2887 for (Integer i
: csiParams
) {
2893 currentState
.attr
.reset();
2898 currentState
.attr
.setBold(true);
2903 currentState
.attr
.setUnderline(true);
2908 currentState
.attr
.setBlink(true);
2913 currentState
.attr
.setReverse(true);
2920 if (type
== DeviceType
.XTERM
) {
2934 if ((type
== DeviceType
.VT220
)
2935 || (type
== DeviceType
.XTERM
)) {
2941 currentState
.attr
.setBold(false);
2946 currentState
.attr
.setUnderline(false);
2951 currentState
.attr
.setBlink(false);
2956 currentState
.attr
.setReverse(false);
2964 // A true VT100/102/220 does not support color, however everyone
2965 // is used to their terminal emulator supporting color so we will
2966 // unconditionally support color for all DeviceType's.
2971 // Set black foreground
2972 currentState
.attr
.setForeColor(Color
.BLACK
);
2975 // Set red foreground
2976 currentState
.attr
.setForeColor(Color
.RED
);
2979 // Set green foreground
2980 currentState
.attr
.setForeColor(Color
.GREEN
);
2983 // Set yellow foreground
2984 currentState
.attr
.setForeColor(Color
.YELLOW
);
2987 // Set blue foreground
2988 currentState
.attr
.setForeColor(Color
.BLUE
);
2991 // Set magenta foreground
2992 currentState
.attr
.setForeColor(Color
.MAGENTA
);
2995 // Set cyan foreground
2996 currentState
.attr
.setForeColor(Color
.CYAN
);
2999 // Set white foreground
3000 currentState
.attr
.setForeColor(Color
.WHITE
);
3003 // Underscore on, default foreground color
3004 currentState
.attr
.setUnderline(true);
3005 currentState
.attr
.setForeColor(Color
.WHITE
);
3008 // Underscore off, default foreground color
3009 currentState
.attr
.setUnderline(false);
3010 currentState
.attr
.setForeColor(Color
.WHITE
);
3013 // Set black background
3014 currentState
.attr
.setBackColor(Color
.BLACK
);
3017 // Set red background
3018 currentState
.attr
.setBackColor(Color
.RED
);
3021 // Set green background
3022 currentState
.attr
.setBackColor(Color
.GREEN
);
3025 // Set yellow background
3026 currentState
.attr
.setBackColor(Color
.YELLOW
);
3029 // Set blue background
3030 currentState
.attr
.setBackColor(Color
.BLUE
);
3033 // Set magenta background
3034 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3037 // Set cyan background
3038 currentState
.attr
.setBackColor(Color
.CYAN
);
3041 // Set white background
3042 currentState
.attr
.setBackColor(Color
.WHITE
);
3045 // Default background
3046 currentState
.attr
.setBackColor(Color
.BLACK
);
3056 * DA - Device attributes.
3059 int extendedFlag
= 0;
3061 Character
[] chars
= collectBuffer
.toArray(new Character
[0]);
3062 StringBuilder args
= new StringBuilder();
3063 for (int j
= 1; j
< chars
.length
; j
++) {
3064 args
.append(chars
[j
]);
3067 if (chars
.length
> 0) {
3068 if (chars
[0] == '>') {
3070 if (chars
.length
>= 2) {
3071 i
= Integer
.parseInt(args
.toString());
3073 } else if (chars
[0] == '=') {
3075 if (chars
.length
>= 2) {
3076 i
= Integer
.parseInt(args
.toString());
3079 // Unknown code, bail out
3084 if ((i
!= 0) && (i
!= 1)) {
3088 if ((extendedFlag
== 0) && (i
== 0)) {
3089 // Send string directly to remote side
3090 writeRemote(deviceTypeResponse());
3094 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3096 if ((extendedFlag
== 1) && (i
== 0)) {
3098 * Request "What type of terminal are you, what is your
3099 * firmware version, and what hardware options do you have
3102 * Respond: "I am a VT220 (identification code of 1), my
3103 * firmware version is _____ (Pv), and I have _____ Po
3104 * options installed."
3110 if (s8c1t
== true) {
3111 writeRemote("\u009b>1;10;0c");
3113 writeRemote("\033[>1;10;0c");
3119 if ((extendedFlag
== 2) && (i
== 0)) {
3122 * Request "What is your unit ID?"
3124 * Respond: "I was manufactured at site 00 and have a unique ID
3128 writeRemote("\033P!|00010203\033\\");
3133 * DECSTBM - Set top and bottom margins.
3135 private void decstbm() {
3136 int top
= getCsiParam(0, 1, 1, height
) - 1;
3137 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3142 scrollRegionTop
= top
;
3143 scrollRegionBottom
= bottom
;
3146 cursorPosition(0, 0);
3150 * DECREQTPARM - Request terminal parameters.
3152 private void decreqtparm() {
3153 int i
= getCsiParam(0, 0);
3155 if ((i
!= 0) && (i
!= 1)) {
3162 * Request terminal parameters.
3166 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3167 * (CLoCk MULtiplier = 1, STP option flags = 0)
3171 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3174 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3176 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3182 * DECSCA - Select Character Attributes.
3184 private void decsca() {
3185 int i
= getCsiParam(0, 0);
3187 if ((i
== 0) || (i
== 2)) {
3189 currentState
.attr
.setProtect(false);
3193 currentState
.attr
.setProtect(true);
3198 * DECSTR - Soft Terminal Reset.
3200 private void decstr() {
3201 // Do exactly like RIS - Reset to initial state
3203 // Do I clear screen too? I think so...
3204 eraseScreen(0, 0, height
- 1, width
- 1, false);
3205 cursorPosition(0, 0);
3209 * DSR - Device status report.
3211 private void dsr() {
3212 boolean decPrivateModeFlag
= false;
3214 for (Character ch
: collectBuffer
) {
3216 decPrivateModeFlag
= true;
3220 int i
= getCsiParam(0, 0);
3225 // Request status report. Respond with "OK, no malfunction."
3227 // Send string directly to remote side
3228 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3231 writeRemote("\u009b0n");
3233 writeRemote("\033[0n");
3238 // Request cursor position. Respond with current position.
3240 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3243 str
= String
.format("\u009b%d;%dR",
3244 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3246 str
= String
.format("\033[%d;%dR",
3247 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3250 // Send string directly to remote side
3255 if (decPrivateModeFlag
== true) {
3257 // Request printer status report. Respond with "Printer not
3260 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3261 && (s8c1t
== true)) {
3262 writeRemote("\u009b?13n");
3264 writeRemote("\033[?13n");
3270 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3271 && (decPrivateModeFlag
== true)
3274 // Request user-defined keys are locked or unlocked. Respond
3275 // with "User-defined keys are locked."
3277 if (s8c1t
== true) {
3278 writeRemote("\u009b?21n");
3280 writeRemote("\033[?21n");
3286 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3287 && (decPrivateModeFlag
== true)
3290 // Request keyboard language. Respond with "Keyboard
3291 // language is North American."
3293 if (s8c1t
== true) {
3294 writeRemote("\u009b?27;1n");
3296 writeRemote("\033[?27;1n");
3303 // Some other option, ignore
3309 * TBC - Tabulation clear.
3311 private void tbc() {
3312 int i
= getCsiParam(0, 0);
3314 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3315 for (Integer stop
: tabStops
) {
3316 if (stop
== currentState
.cursorX
) {
3321 tabStops
= newStops
;
3329 * Erase the characters in the current line from the start column to the
3330 * end column, inclusive.
3332 * @param start starting column to erase (between 0 and width - 1)
3333 * @param end ending column to erase (between 0 and width - 1)
3334 * @param honorProtected if true, do not erase characters with the
3335 * protected attribute set
3337 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3342 if (end
> width
- 1) {
3349 for (int i
= start
; i
<= end
; i
++) {
3350 DisplayLine line
= display
.get(currentState
.cursorY
);
3351 if ((!honorProtected
)
3352 || ((honorProtected
) && (!line
.charAt(i
).getProtect()))) {
3359 * From the VT102 manual:
3361 * Erasing a character also erases any character
3362 * attribute of the character.
3368 * Erase with the current color a.k.a. back-color erase
3371 line
.setChar(i
, ' ');
3372 line
.setAttr(i
, currentState
.attr
);
3380 * Erase a rectangular section of the screen, inclusive. end column,
3383 * @param startRow starting row to erase (between 0 and height - 1)
3384 * @param startCol starting column to erase (between 0 and width - 1)
3385 * @param endRow ending row to erase (between 0 and height - 1)
3386 * @param endCol ending column to erase (between 0 and width - 1)
3387 * @param honorProtected if true, do not erase characters with the
3388 * protected attribute set
3390 private void eraseScreen(final int startRow
, final int startCol
,
3391 final int endRow
, final int endCol
, final boolean honorProtected
) {
3399 || (endRow
< startRow
)
3400 || (endCol
< startCol
)
3405 oldCursorY
= currentState
.cursorY
;
3406 for (int i
= startRow
; i
<= endRow
; i
++) {
3407 currentState
.cursorY
= i
;
3408 eraseLine(startCol
, endCol
, honorProtected
);
3410 // Erase display clears the double attributes
3411 display
.get(i
).setDoubleWidth(false);
3412 display
.get(i
).setDoubleHeight(0);
3414 currentState
.cursorY
= oldCursorY
;
3418 * VT220 printer functions. All of these are parsed, but won't do
3421 private void printerFunctions() {
3422 boolean decPrivateModeFlag
= false;
3423 for (Character ch
: collectBuffer
) {
3425 decPrivateModeFlag
= true;
3429 int i
= getCsiParam(0, 0);
3434 if (decPrivateModeFlag
== false) {
3440 if (decPrivateModeFlag
== true) {
3441 // Print cursor line
3446 if (decPrivateModeFlag
== true) {
3447 // Auto print mode OFF
3449 // Printer controller OFF
3451 // Characters re-appear on the screen
3452 printerControllerMode
= false;
3457 if (decPrivateModeFlag
== true) {
3461 // Printer controller
3463 // Characters get sucked into oblivion
3464 printerControllerMode
= true;
3475 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
3476 * of remote systems will send an XTerm title sequence even if TERM isn't
3479 * @param xtermChar the character received from the remote side
3481 private void oscPut(final char xtermChar
) {
3483 collectBuffer
.add(new Character(xtermChar
));
3486 if (xtermChar
== 0x07) {
3487 Character
[] chars
= collectBuffer
.toArray(new Character
[0]);
3488 StringBuilder args
= new StringBuilder();
3489 for (int j
= 0; j
< chars
.length
- 1; j
++) {
3490 args
.append(chars
[j
]);
3492 String
[] p
= args
.toString().split(";");
3494 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
3502 // Go to SCAN_GROUND state
3509 * Run this input character through the ECMA48 state machine.
3511 * @param ch character from the remote side
3513 public void consume(char ch
) {
3516 // System.err.printf("%c", ch);
3518 // Special case for VT10x: 7-bit characters only
3519 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
3520 ch
= (char)(ch
& 0x7F);
3523 // Special "anywhere" states
3525 // 18, 1A --> execute, then switch to SCAN_GROUND
3526 if ((ch
== 0x18) || (ch
== 0x1A)) {
3527 // CAN and SUB abort escape sequences
3532 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
3536 && (scanState
!= ScanState
.DCS_ENTRY
)
3537 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
3538 && (scanState
!= ScanState
.DCS_IGNORE
)
3539 && (scanState
!= ScanState
.DCS_PARAM
)
3540 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
3543 scanState
= ScanState
.ESCAPE
;
3547 // 0x9B == CSI 8-bit sequence
3549 scanState
= ScanState
.CSI_ENTRY
;
3553 // 0x9D goes to ScanState.OSC_STRING
3555 scanState
= ScanState
.OSC_STRING
;
3559 // 0x90 goes to DCS_ENTRY
3561 scanState
= ScanState
.DCS_ENTRY
;
3565 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
3566 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
3567 scanState
= ScanState
.SOSPMAPC_STRING
;
3571 // 0x7F (DEL) is always discarded
3576 switch (scanState
) {
3579 // 00-17, 19, 1C-1F --> execute
3580 // 80-8F, 91-9A, 9C --> execute
3581 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
3582 handleControlChar(ch
);
3586 if (((ch
>= 0x20) && (ch
<= 0x7F))
3590 // VT220 printer --> trash bin
3591 if (((type
== DeviceType
.VT220
)
3592 || (type
== DeviceType
.XTERM
))
3593 && (printerControllerMode
== true)
3598 // Hang onto this character
3599 repCh
= mapCharacter(ch
);
3601 // Print this character
3602 printCharacter(repCh
);
3607 // 00-17, 19, 1C-1F --> execute
3609 handleControlChar(ch
);
3613 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
3614 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
3616 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
3620 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
3621 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
3632 // DECSC - Save cursor
3633 // Note this code overlaps both ANSI and VT52 mode
3638 // DECRC - Restore cursor
3639 // Note this code overlaps both ANSI and VT52 mode
3648 if (vt52Mode
== true) {
3649 // DECANM - Enter ANSI mode
3651 arrowKeyMode
= ArrowKeyMode
.VT100
;
3654 * From the VT102 docs: "You use ANSI mode to select
3655 * most terminal features; the terminal uses the same
3656 * features when it switches to VT52 mode. You
3657 * cannot, however, change most of these features in
3660 * In other words, do not reset any other attributes
3661 * when switching between VT52 submode and ANSI.
3665 currentState
.g0Charset
= CharacterSet
.US
;
3666 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3668 singleshift
= Singleshift
.NONE
;
3669 currentState
.glLockshift
= LockshiftMode
.NONE
;
3670 currentState
.grLockshift
= LockshiftMode
.NONE
;
3674 // DECKPAM - Keypad application mode
3675 // Note this code overlaps both ANSI and VT52 mode
3679 // DECKPNM - Keypad numeric mode
3680 // Note this code overlaps both ANSI and VT52 mode
3687 if (vt52Mode
== true) {
3688 // Cursor up, and stop at the top without scrolling
3693 if (vt52Mode
== true) {
3694 // Cursor down, and stop at the bottom without scrolling
3695 cursorDown(1, false);
3699 if (vt52Mode
== true) {
3700 // Cursor right, and stop at the right without scrolling
3701 cursorRight(1, false);
3705 if (vt52Mode
== true) {
3706 // Cursor left, and stop at the left without scrolling
3707 cursorLeft(1, false);
3714 if (vt52Mode
== true) {
3722 if (vt52Mode
== true) {
3723 // G0 --> Special graphics
3724 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
3728 if (vt52Mode
== true) {
3730 currentState
.g0Charset
= CharacterSet
.US
;
3734 if (vt52Mode
== true) {
3736 cursorPosition(0, 0);
3738 // HTS - Horizontal tabulation set
3743 if (vt52Mode
== true) {
3744 // Reverse line feed. Same as RI.
3749 if (vt52Mode
== true) {
3750 // Erase to end of screen
3751 eraseLine(currentState
.cursorX
, width
- 1, false);
3752 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
3757 if (vt52Mode
== true) {
3758 // Erase to end of line
3759 eraseLine(currentState
.cursorX
, width
- 1, false);
3765 if (vt52Mode
== true) {
3768 // RI - Reverse index
3773 if (vt52Mode
== false) {
3775 singleshift
= Singleshift
.SS2
;
3779 if (vt52Mode
== false) {
3781 singleshift
= Singleshift
.SS3
;
3788 if ((ch
>= 0x51) && (ch
<= 0x57)) {
3804 if (vt52Mode
== true) {
3805 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
3813 if (vt52Mode
== true) {
3815 // Send string directly to remote side
3816 writeRemote("\033/Z");
3819 // Send string directly to remote side
3820 writeRemote(deviceTypeResponse());
3831 // VT52 cannot get to any of these other states
3832 if (vt52Mode
== true) {
3837 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
3844 // RIS - Reset to initial state
3846 // Do I clear screen too? I think so...
3847 eraseScreen(0, 0, height
- 1, width
- 1, false);
3848 cursorPosition(0, 0);
3862 if ((type
== DeviceType
.VT220
)
3863 || (type
== DeviceType
.XTERM
)) {
3865 // VT220 lockshift G2 into GL
3866 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
3871 if ((type
== DeviceType
.VT220
)
3872 || (type
== DeviceType
.XTERM
)) {
3874 // VT220 lockshift G3 into GL
3875 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
3893 if ((type
== DeviceType
.VT220
)
3894 || (type
== DeviceType
.XTERM
)) {
3896 // VT220 lockshift G3 into GR
3897 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
3902 if ((type
== DeviceType
.VT220
)
3903 || (type
== DeviceType
.XTERM
)) {
3905 // VT220 lockshift G2 into GR
3906 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
3912 if ((type
== DeviceType
.VT220
)
3913 || (type
== DeviceType
.XTERM
)) {
3915 // VT220 lockshift G1 into GR
3916 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
3926 // 0x5B goes to CSI_ENTRY
3928 scanState
= ScanState
.CSI_ENTRY
;
3931 // 0x5D goes to OSC_STRING
3933 scanState
= ScanState
.OSC_STRING
;
3936 // 0x50 goes to DCS_ENTRY
3938 scanState
= ScanState
.DCS_ENTRY
;
3941 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
3942 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
3943 scanState
= ScanState
.SOSPMAPC_STRING
;
3948 case ESCAPE_INTERMEDIATE
:
3949 // 00-17, 19, 1C-1F --> execute
3951 handleControlChar(ch
);
3954 // 20-2F --> collect
3955 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
3959 // 30-7E --> dispatch, then switch to GROUND
3960 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
3963 if ((collectBuffer
.size() == 1)
3964 && (collectBuffer
.get(0) == '(')) {
3965 // G0 --> Special graphics
3966 currentState
.g0Charset
= CharacterSet
.DRAWING
;
3968 if ((collectBuffer
.size() == 1)
3969 && (collectBuffer
.get(0) == ')')) {
3970 // G1 --> Special graphics
3971 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3973 if ((type
== DeviceType
.VT220
)
3974 || (type
== DeviceType
.XTERM
)) {
3976 if ((collectBuffer
.size() == 1)
3977 && (collectBuffer
.get(0) == '*')) {
3978 // G2 --> Special graphics
3979 currentState
.g2Charset
= CharacterSet
.DRAWING
;
3981 if ((collectBuffer
.size() == 1)
3982 && (collectBuffer
.get(0) == '+')) {
3983 // G3 --> Special graphics
3984 currentState
.g3Charset
= CharacterSet
.DRAWING
;
3989 if ((collectBuffer
.size() == 1)
3990 && (collectBuffer
.get(0) == '(')) {
3991 // G0 --> Alternate character ROM standard character set
3992 currentState
.g0Charset
= CharacterSet
.ROM
;
3994 if ((collectBuffer
.size() == 1)
3995 && (collectBuffer
.get(0) == ')')) {
3996 // G1 --> Alternate character ROM standard character set
3997 currentState
.g1Charset
= CharacterSet
.ROM
;
4001 if ((collectBuffer
.size() == 1)
4002 && (collectBuffer
.get(0) == '(')) {
4003 // G0 --> Alternate character ROM special graphics
4004 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4006 if ((collectBuffer
.size() == 1)
4007 && (collectBuffer
.get(0) == ')')) {
4008 // G1 --> Alternate character ROM special graphics
4009 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4013 if ((collectBuffer
.size() == 1)
4014 && (collectBuffer
.get(0) == '#')) {
4015 // DECDHL - Double-height line (top half)
4020 if ((collectBuffer
.size() == 1)
4021 && (collectBuffer
.get(0) == '#')) {
4022 // DECDHL - Double-height line (bottom half)
4025 if ((type
== DeviceType
.VT220
)
4026 || (type
== DeviceType
.XTERM
)) {
4028 if ((collectBuffer
.size() == 1)
4029 && (collectBuffer
.get(0) == '(')) {
4031 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4033 if ((collectBuffer
.size() == 1)
4034 && (collectBuffer
.get(0) == ')')) {
4036 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4038 if ((collectBuffer
.size() == 1)
4039 && (collectBuffer
.get(0) == '*')) {
4041 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4043 if ((collectBuffer
.size() == 1)
4044 && (collectBuffer
.get(0) == '+')) {
4046 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4051 if ((collectBuffer
.size() == 1)
4052 && (collectBuffer
.get(0) == '#')) {
4053 // DECSWL - Single-width line
4056 if ((type
== DeviceType
.VT220
)
4057 || (type
== DeviceType
.XTERM
)) {
4059 if ((collectBuffer
.size() == 1)
4060 && (collectBuffer
.get(0) == '(')) {
4062 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4064 if ((collectBuffer
.size() == 1)
4065 && (collectBuffer
.get(0) == ')')) {
4067 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4069 if ((collectBuffer
.size() == 1)
4070 && (collectBuffer
.get(0) == '*')) {
4072 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4074 if ((collectBuffer
.size() == 1)
4075 && (collectBuffer
.get(0) == '+')) {
4077 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4082 if ((collectBuffer
.size() == 1)
4083 && (collectBuffer
.get(0) == '#')) {
4084 // DECDWL - Double-width line
4087 if ((type
== DeviceType
.VT220
)
4088 || (type
== DeviceType
.XTERM
)) {
4090 if ((collectBuffer
.size() == 1)
4091 && (collectBuffer
.get(0) == '(')) {
4093 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4095 if ((collectBuffer
.size() == 1)
4096 && (collectBuffer
.get(0) == ')')) {
4098 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4100 if ((collectBuffer
.size() == 1)
4101 && (collectBuffer
.get(0) == '*')) {
4103 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4105 if ((collectBuffer
.size() == 1)
4106 && (collectBuffer
.get(0) == '+')) {
4108 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4113 if ((type
== DeviceType
.VT220
)
4114 || (type
== DeviceType
.XTERM
)) {
4116 if ((collectBuffer
.size() == 1)
4117 && (collectBuffer
.get(0) == '(')) {
4119 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4121 if ((collectBuffer
.size() == 1)
4122 && (collectBuffer
.get(0) == ')')) {
4124 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4126 if ((collectBuffer
.size() == 1)
4127 && (collectBuffer
.get(0) == '*')) {
4129 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4131 if ((collectBuffer
.size() == 1)
4132 && (collectBuffer
.get(0) == '+')) {
4134 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4139 if ((collectBuffer
.size() == 1)
4140 && (collectBuffer
.get(0) == '#')) {
4141 // DECALN - Screen alignment display
4150 if ((type
== DeviceType
.VT220
)
4151 || (type
== DeviceType
.XTERM
)) {
4153 if ((collectBuffer
.size() == 1)
4154 && (collectBuffer
.get(0) == '(')) {
4155 // G0 --> DEC_SUPPLEMENTAL
4156 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4158 if ((collectBuffer
.size() == 1)
4159 && (collectBuffer
.get(0) == ')')) {
4160 // G1 --> DEC_SUPPLEMENTAL
4161 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4163 if ((collectBuffer
.size() == 1)
4164 && (collectBuffer
.get(0) == '*')) {
4165 // G2 --> DEC_SUPPLEMENTAL
4166 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4168 if ((collectBuffer
.size() == 1)
4169 && (collectBuffer
.get(0) == '+')) {
4170 // G3 --> DEC_SUPPLEMENTAL
4171 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4176 if ((type
== DeviceType
.VT220
)
4177 || (type
== DeviceType
.XTERM
)) {
4179 if ((collectBuffer
.size() == 1)
4180 && (collectBuffer
.get(0) == '(')) {
4182 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4184 if ((collectBuffer
.size() == 1)
4185 && (collectBuffer
.get(0) == ')')) {
4187 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4189 if ((collectBuffer
.size() == 1)
4190 && (collectBuffer
.get(0) == '*')) {
4192 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4194 if ((collectBuffer
.size() == 1)
4195 && (collectBuffer
.get(0) == '+')) {
4197 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4206 if ((collectBuffer
.size() == 1)
4207 && (collectBuffer
.get(0) == '(')) {
4208 // G0 --> United Kingdom set
4209 currentState
.g0Charset
= CharacterSet
.UK
;
4211 if ((collectBuffer
.size() == 1)
4212 && (collectBuffer
.get(0) == ')')) {
4213 // G1 --> United Kingdom set
4214 currentState
.g1Charset
= CharacterSet
.UK
;
4216 if ((type
== DeviceType
.VT220
)
4217 || (type
== DeviceType
.XTERM
)) {
4219 if ((collectBuffer
.size() == 1)
4220 && (collectBuffer
.get(0) == '*')) {
4221 // G2 --> United Kingdom set
4222 currentState
.g2Charset
= CharacterSet
.UK
;
4224 if ((collectBuffer
.size() == 1)
4225 && (collectBuffer
.get(0) == '+')) {
4226 // G3 --> United Kingdom set
4227 currentState
.g3Charset
= CharacterSet
.UK
;
4232 if ((collectBuffer
.size() == 1)
4233 && (collectBuffer
.get(0) == '(')) {
4235 currentState
.g0Charset
= CharacterSet
.US
;
4237 if ((collectBuffer
.size() == 1)
4238 && (collectBuffer
.get(0) == ')')) {
4240 currentState
.g1Charset
= CharacterSet
.US
;
4242 if ((type
== DeviceType
.VT220
)
4243 || (type
== DeviceType
.XTERM
)) {
4245 if ((collectBuffer
.size() == 1)
4246 && (collectBuffer
.get(0) == '*')) {
4248 currentState
.g2Charset
= CharacterSet
.US
;
4250 if ((collectBuffer
.size() == 1)
4251 && (collectBuffer
.get(0) == '+')) {
4253 currentState
.g3Charset
= CharacterSet
.US
;
4258 if ((type
== DeviceType
.VT220
)
4259 || (type
== DeviceType
.XTERM
)) {
4261 if ((collectBuffer
.size() == 1)
4262 && (collectBuffer
.get(0) == '(')) {
4264 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4266 if ((collectBuffer
.size() == 1)
4267 && (collectBuffer
.get(0) == ')')) {
4269 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4271 if ((collectBuffer
.size() == 1)
4272 && (collectBuffer
.get(0) == '*')) {
4274 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4276 if ((collectBuffer
.size() == 1)
4277 && (collectBuffer
.get(0) == '+')) {
4279 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4286 if ((type
== DeviceType
.VT220
)
4287 || (type
== DeviceType
.XTERM
)) {
4289 if ((collectBuffer
.size() == 1)
4290 && (collectBuffer
.get(0) == '(')) {
4292 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4294 if ((collectBuffer
.size() == 1)
4295 && (collectBuffer
.get(0) == ')')) {
4297 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4299 if ((collectBuffer
.size() == 1)
4300 && (collectBuffer
.get(0) == '*')) {
4302 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4304 if ((collectBuffer
.size() == 1)
4305 && (collectBuffer
.get(0) == '+')) {
4307 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4312 if ((type
== DeviceType
.VT220
)
4313 || (type
== DeviceType
.XTERM
)) {
4315 if ((collectBuffer
.size() == 1)
4316 && (collectBuffer
.get(0) == ' ')) {
4323 if ((type
== DeviceType
.VT220
)
4324 || (type
== DeviceType
.XTERM
)) {
4326 if ((collectBuffer
.size() == 1)
4327 && (collectBuffer
.get(0) == ' ')) {
4334 if ((type
== DeviceType
.VT220
)
4335 || (type
== DeviceType
.XTERM
)) {
4337 if ((collectBuffer
.size() == 1)
4338 && (collectBuffer
.get(0) == '(')) {
4340 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4342 if ((collectBuffer
.size() == 1)
4343 && (collectBuffer
.get(0) == ')')) {
4345 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4347 if ((collectBuffer
.size() == 1)
4348 && (collectBuffer
.get(0) == '*')) {
4350 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4352 if ((collectBuffer
.size() == 1)
4353 && (collectBuffer
.get(0) == '+')) {
4355 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4363 if ((type
== DeviceType
.VT220
)
4364 || (type
== DeviceType
.XTERM
)) {
4366 if ((collectBuffer
.size() == 1)
4367 && (collectBuffer
.get(0) == '(')) {
4369 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4371 if ((collectBuffer
.size() == 1)
4372 && (collectBuffer
.get(0) == ')')) {
4374 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4376 if ((collectBuffer
.size() == 1)
4377 && (collectBuffer
.get(0) == '*')) {
4379 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4381 if ((collectBuffer
.size() == 1)
4382 && (collectBuffer
.get(0) == '+')) {
4384 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4395 if ((type
== DeviceType
.VT220
)
4396 || (type
== DeviceType
.XTERM
)) {
4398 if ((collectBuffer
.size() == 1)
4399 && (collectBuffer
.get(0) == '(')) {
4401 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4403 if ((collectBuffer
.size() == 1)
4404 && (collectBuffer
.get(0) == ')')) {
4406 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4408 if ((collectBuffer
.size() == 1)
4409 && (collectBuffer
.get(0) == '*')) {
4411 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4413 if ((collectBuffer
.size() == 1)
4414 && (collectBuffer
.get(0) == '+')) {
4416 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4421 if ((type
== DeviceType
.VT220
)
4422 || (type
== DeviceType
.XTERM
)) {
4424 if ((collectBuffer
.size() == 1)
4425 && (collectBuffer
.get(0) == '(')) {
4427 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4429 if ((collectBuffer
.size() == 1)
4430 && (collectBuffer
.get(0) == ')')) {
4432 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4434 if ((collectBuffer
.size() == 1)
4435 && (collectBuffer
.get(0) == '*')) {
4437 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
4439 if ((collectBuffer
.size() == 1)
4440 && (collectBuffer
.get(0) == '+')) {
4442 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
4454 if ((type
== DeviceType
.VT220
)
4455 || (type
== DeviceType
.XTERM
)) {
4457 if ((collectBuffer
.size() == 1)
4458 && (collectBuffer
.get(0) == '(')) {
4460 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
4462 if ((collectBuffer
.size() == 1)
4463 && (collectBuffer
.get(0) == ')')) {
4465 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
4467 if ((collectBuffer
.size() == 1)
4468 && (collectBuffer
.get(0) == '*')) {
4470 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
4472 if ((collectBuffer
.size() == 1)
4473 && (collectBuffer
.get(0) == '+')) {
4475 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
4480 if ((type
== DeviceType
.VT220
)
4481 || (type
== DeviceType
.XTERM
)) {
4483 if ((collectBuffer
.size() == 1)
4484 && (collectBuffer
.get(0) == '(')) {
4486 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
4488 if ((collectBuffer
.size() == 1)
4489 && (collectBuffer
.get(0) == ')')) {
4491 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
4493 if ((collectBuffer
.size() == 1)
4494 && (collectBuffer
.get(0) == '*')) {
4496 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
4498 if ((collectBuffer
.size() == 1)
4499 && (collectBuffer
.get(0) == '+')) {
4501 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
4548 // 0x9C goes to GROUND
4556 // 00-17, 19, 1C-1F --> execute
4558 handleControlChar(ch
);
4561 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
4562 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4564 scanState
= ScanState
.CSI_INTERMEDIATE
;
4567 // 30-39, 3B --> param, then switch to CSI_PARAM
4568 if ((ch
>= '0') && (ch
<= '9')) {
4570 scanState
= ScanState
.CSI_PARAM
;
4574 scanState
= ScanState
.CSI_PARAM
;
4577 // 3C-3F --> collect, then switch to CSI_PARAM
4578 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
4580 scanState
= ScanState
.CSI_PARAM
;
4583 // 40-7E --> dispatch, then switch to GROUND
4584 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
4587 // ICH - Insert character
4595 // CUD - Cursor down
4599 // CUF - Cursor forward
4603 // CUB - Cursor backward
4607 // CNL - Cursor down and to column 1
4608 if (type
== DeviceType
.XTERM
) {
4613 // CPL - Cursor up and to column 1
4614 if (type
== DeviceType
.XTERM
) {
4619 // CHA - Cursor to column # in current row
4620 if (type
== DeviceType
.XTERM
) {
4625 // CUP - Cursor position
4629 // CHT - Cursor forward X tab stops (default 1)
4630 if (type
== DeviceType
.XTERM
) {
4635 // ED - Erase in display
4639 // EL - Erase in line
4654 // DCH - Delete character
4661 // Scroll up X lines (default 1)
4662 if (type
== DeviceType
.XTERM
) {
4667 // Scroll down X lines (default 1)
4668 if (type
== DeviceType
.XTERM
) {
4677 if ((type
== DeviceType
.VT220
)
4678 || (type
== DeviceType
.XTERM
)) {
4680 // ECH - Erase character
4687 // CBT - Cursor backward X tab stops (default 1)
4688 if (type
== DeviceType
.XTERM
) {
4699 // HPA - Cursor to column # in current row. Same as CHA
4700 if (type
== DeviceType
.XTERM
) {
4705 // HPR - Cursor right. Same as CUF
4706 if (type
== DeviceType
.XTERM
) {
4711 // REP - Repeat last char X times
4712 if (type
== DeviceType
.XTERM
) {
4717 // DA - Device attributes
4721 // VPA - Cursor to row, current column.
4722 if (type
== DeviceType
.XTERM
) {
4727 // VPR - Cursor down. Same as CUD
4728 if (type
== DeviceType
.XTERM
) {
4733 // HVP - Horizontal and vertical position
4737 // TBC - Tabulation clear
4741 // Sets an ANSI or DEC private toggle
4745 if ((type
== DeviceType
.VT220
)
4746 || (type
== DeviceType
.XTERM
)) {
4748 // Printer functions
4756 // Sets an ANSI or DEC private toggle
4760 // SGR - Select graphics rendition
4764 // DSR - Device status report
4771 // DECLL - Load leds
4775 // DECSTBM - Set top and bottom margins
4779 // Save cursor (ANSI.SYS)
4780 if (type
== DeviceType
.XTERM
) {
4781 savedState
.cursorX
= currentState
.cursorX
;
4782 savedState
.cursorY
= currentState
.cursorY
;
4788 // Restore cursor (ANSI.SYS)
4789 if (type
== DeviceType
.XTERM
) {
4790 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
4797 // DECREQTPARM - Request terminal parameters
4813 // 0x9C goes to GROUND
4818 // 0x3A goes to CSI_IGNORE
4820 scanState
= ScanState
.CSI_IGNORE
;
4825 // 00-17, 19, 1C-1F --> execute
4827 handleControlChar(ch
);
4830 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
4831 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4833 scanState
= ScanState
.CSI_INTERMEDIATE
;
4836 // 30-39, 3B --> param
4837 if ((ch
>= '0') && (ch
<= '9')) {
4844 // 0x3A goes to CSI_IGNORE
4846 scanState
= ScanState
.CSI_IGNORE
;
4848 // 0x3C-3F goes to CSI_IGNORE
4849 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
4850 scanState
= ScanState
.CSI_IGNORE
;
4853 // 40-7E --> dispatch, then switch to GROUND
4854 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
4857 // ICH - Insert character
4865 // CUD - Cursor down
4869 // CUF - Cursor forward
4873 // CUB - Cursor backward
4877 // CNL - Cursor down and to column 1
4878 if (type
== DeviceType
.XTERM
) {
4883 // CPL - Cursor up and to column 1
4884 if (type
== DeviceType
.XTERM
) {
4889 // CHA - Cursor to column # in current row
4890 if (type
== DeviceType
.XTERM
) {
4895 // CUP - Cursor position
4899 // CHT - Cursor forward X tab stops (default 1)
4900 if (type
== DeviceType
.XTERM
) {
4905 // ED - Erase in display
4909 // EL - Erase in line
4924 // DCH - Delete character
4931 // Scroll up X lines (default 1)
4932 if (type
== DeviceType
.XTERM
) {
4937 // Scroll down X lines (default 1)
4938 if (type
== DeviceType
.XTERM
) {
4947 if ((type
== DeviceType
.VT220
)
4948 || (type
== DeviceType
.XTERM
)) {
4950 // ECH - Erase character
4957 // CBT - Cursor backward X tab stops (default 1)
4958 if (type
== DeviceType
.XTERM
) {
4969 // HPA - Cursor to column # in current row. Same as CHA
4970 if (type
== DeviceType
.XTERM
) {
4975 // HPR - Cursor right. Same as CUF
4976 if (type
== DeviceType
.XTERM
) {
4981 // REP - Repeat last char X times
4982 if (type
== DeviceType
.XTERM
) {
4987 // DA - Device attributes
4991 // VPA - Cursor to row, current column.
4992 if (type
== DeviceType
.XTERM
) {
4997 // VPR - Cursor down. Same as CUD
4998 if (type
== DeviceType
.XTERM
) {
5003 // HVP - Horizontal and vertical position
5007 // TBC - Tabulation clear
5011 // Sets an ANSI or DEC private toggle
5015 if ((type
== DeviceType
.VT220
)
5016 || (type
== DeviceType
.XTERM
)) {
5018 // Printer functions
5026 // Sets an ANSI or DEC private toggle
5030 // SGR - Select graphics rendition
5034 // DSR - Device status report
5041 // DECLL - Load leds
5045 // DECSTBM - Set top and bottom margins
5055 // DECREQTPARM - Request terminal parameters
5072 case CSI_INTERMEDIATE
:
5073 // 00-17, 19, 1C-1F --> execute
5075 handleControlChar(ch
);
5078 // 20-2F --> collect
5079 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5083 // 0x30-3F goes to CSI_IGNORE
5084 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5085 scanState
= ScanState
.CSI_IGNORE
;
5088 // 40-7E --> dispatch, then switch to GROUND
5089 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5141 if (((type
== DeviceType
.VT220
)
5142 || (type
== DeviceType
.XTERM
))
5143 && (collectBuffer
.get(collectBuffer
.size() - 1) == '\"')
5145 // DECSCL - compatibility level
5148 if ((type
== DeviceType
.XTERM
)
5149 && (collectBuffer
.get(collectBuffer
.size() - 1) == '!')
5151 // DECSTR - Soft terminal reset
5156 if (((type
== DeviceType
.VT220
)
5157 || (type
== DeviceType
.XTERM
))
5158 && (collectBuffer
.get(collectBuffer
.size() - 1) == '\"')
5186 // 00-17, 19, 1C-1F --> execute
5188 handleControlChar(ch
);
5191 // 20-2F --> collect
5192 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5196 // 40-7E --> ignore, then switch to GROUND
5197 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5201 // 20-3F, 7F --> ignore
5207 // 0x9C goes to GROUND
5212 // 0x1B 0x5C goes to GROUND
5217 if ((collectBuffer
.size() > 0)
5218 && (collectBuffer
.get(collectBuffer
.size() - 1) == 0x1B)) {
5223 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5224 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5226 scanState
= ScanState
.DCS_INTERMEDIATE
;
5229 // 30-39, 3B --> param, then switch to DCS_PARAM
5230 if ((ch
>= '0') && (ch
<= '9')) {
5232 scanState
= ScanState
.DCS_PARAM
;
5236 scanState
= ScanState
.DCS_PARAM
;
5239 // 3C-3F --> collect, then switch to DCS_PARAM
5240 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5242 scanState
= ScanState
.DCS_PARAM
;
5245 // 00-17, 19, 1C-1F, 7F --> ignore
5247 // 0x3A goes to DCS_IGNORE
5249 scanState
= ScanState
.DCS_IGNORE
;
5252 // 0x40-7E goes to DCS_PASSTHROUGH
5253 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5254 scanState
= ScanState
.DCS_PASSTHROUGH
;
5258 case DCS_INTERMEDIATE
:
5260 // 0x9C goes to GROUND
5265 // 0x1B 0x5C goes to GROUND
5270 if ((collectBuffer
.size() > 0) &&
5271 (collectBuffer
.get(collectBuffer
.size() - 1) == 0x1B)) {
5276 // 0x30-3F goes to DCS_IGNORE
5277 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5278 scanState
= ScanState
.DCS_IGNORE
;
5281 // 0x40-7E goes to DCS_PASSTHROUGH
5282 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5283 scanState
= ScanState
.DCS_PASSTHROUGH
;
5286 // 00-17, 19, 1C-1F, 7F --> ignore
5291 // 0x9C goes to GROUND
5296 // 0x1B 0x5C goes to GROUND
5301 if ((collectBuffer
.size() > 0) &&
5302 (collectBuffer
.get(collectBuffer
.size() - 1) == 0x1B)) {
5307 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5308 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5310 scanState
= ScanState
.DCS_INTERMEDIATE
;
5313 // 30-39, 3B --> param
5314 if ((ch
>= '0') && (ch
<= '9')) {
5321 // 00-17, 19, 1C-1F, 7F --> ignore
5323 // 0x3A, 3C-3F goes to DCS_IGNORE
5325 scanState
= ScanState
.DCS_IGNORE
;
5327 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5328 scanState
= ScanState
.DCS_IGNORE
;
5331 // 0x40-7E goes to DCS_PASSTHROUGH
5332 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5333 scanState
= ScanState
.DCS_PASSTHROUGH
;
5337 case DCS_PASSTHROUGH
:
5338 // 0x9C goes to GROUND
5343 // 0x1B 0x5C goes to GROUND
5348 if ((collectBuffer
.size() > 0)
5349 && (collectBuffer
.get(collectBuffer
.size() - 1) == 0x1B)
5355 // 00-17, 19, 1C-1F, 20-7E --> put
5363 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5366 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5375 // 00-17, 19, 1C-1F, 20-7F --> ignore
5377 // 0x9C goes to GROUND
5384 case SOSPMAPC_STRING
:
5385 // 00-17, 19, 1C-1F, 20-7F --> ignore
5387 // 0x9C goes to GROUND
5395 // Special case for Xterm: OSC can pass control characters
5396 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5400 // 00-17, 19, 1C-1F --> ignore
5402 // 20-7F --> osc_put
5403 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5407 // 0x9C goes to GROUND
5414 case VT52_DIRECT_CURSOR_ADDRESS
:
5415 // This is a special case for the VT52 sequence "ESC Y l c"
5416 if (collectBuffer
.size() == 0) {
5418 } else if (collectBuffer
.size() == 1) {
5419 // We've got the two characters, one in the buffer and the
5421 cursorPosition(collectBuffer
.get(0) - '\040', ch
- '\040');
5430 * Expose current cursor X to outside world.
5432 * @return current cursor X
5434 public final int getCursorX() {
5435 return currentState
.cursorX
;
5439 * Expose current cursor Y to outside world.
5441 * @return current cursor Y
5443 public final int getCursorY() {
5444 return currentState
.cursorY
;
5448 * Read function runs on a separate thread.
5451 boolean utf8
= false;
5452 boolean done
= false;
5454 if (type
== DeviceType
.XTERM
) {
5458 // available() will often return > 1, so we need to read in chunks to
5460 char [] readBufferUTF8
= null;
5461 byte [] readBuffer
= null;
5463 readBufferUTF8
= new char[128];
5465 readBuffer
= new byte[128];
5468 while (!done
&& !stopReaderThread
) {
5470 int n
= inputStream
.available();
5471 // System.err.printf("available() %d\n", n); System.err.flush();
5473 if (readBufferUTF8
.length
< n
) {
5474 // The buffer wasn't big enough, make it huger
5475 int newSizeHalf
= Math
.max(readBufferUTF8
.length
, n
);
5477 readBufferUTF8
= new char[newSizeHalf
* 2];
5480 if (readBuffer
.length
< n
) {
5481 // The buffer wasn't big enough, make it huger
5482 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
5483 readBuffer
= new byte[newSizeHalf
* 2];
5489 rc
= input
.read(readBufferUTF8
, 0,
5490 readBufferUTF8
.length
);
5492 rc
= inputStream
.read(readBuffer
, 0,
5495 // System.err.printf("read() %d\n", rc); System.err.flush();
5500 for (int i
= 0; i
< rc
; i
++) {
5503 ch
= readBufferUTF8
[i
];
5507 // Don't step on UI events
5508 synchronized (this) {
5513 // System.err.println("end while loop"); System.err.flush();
5514 } catch (IOException e
) {
5515 e
.printStackTrace();
5518 } // while ((done == false) && (stopReaderThread == false))
5520 // Let the rest of the world know that I am done.
5521 stopReaderThread
= true;
5523 // System.err.println("*** run() exiting..."); System.err.flush();