2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2016 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.tterminal
;
31 import java
.io
.InputStream
;
32 import java
.io
.InputStreamReader
;
33 import java
.io
.IOException
;
34 import java
.io
.OutputStream
;
35 import java
.io
.OutputStreamWriter
;
36 import java
.io
.Reader
;
37 import java
.io
.UnsupportedEncodingException
;
38 import java
.io
.Writer
;
39 import java
.util
.ArrayList
;
40 import java
.util
.Collections
;
41 import java
.util
.LinkedList
;
42 import java
.util
.List
;
44 import jexer
.TKeypress
;
45 import jexer
.event
.TMouseEvent
;
46 import jexer
.bits
.Color
;
47 import jexer
.bits
.Cell
;
48 import jexer
.bits
.CellAttributes
;
49 import static jexer
.TKeypress
.*;
52 * This implements a complex ANSI ECMA-48/ISO 6429/ANSI X3.64 type consoles,
53 * including a scrollback buffer.
56 * It currently implements VT100, VT102, VT220, and XTERM with the following
60 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
61 * failure with XTERM. This is due to vttest failing to decode the UTF-8
65 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
66 * from VT100 are not supported.
69 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
70 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
71 * because DECSCL is not supported, it will fail the last part of the
72 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
75 * - Numeric/application keys from the number pad are not supported because
76 * they are not exposed from the TKeypress API.
79 * - VT52 HOLD SCREEN mode is not supported.
82 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
83 * numerators) are not rendered correctly.
86 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
88 public class ECMA48
implements Runnable
{
91 * The emulator can emulate several kinds of terminals.
93 public enum DeviceType
{
95 * DEC VT100 but also including the three VT102 functions.
116 * Return the proper primary Device Attributes string.
118 * @return string to send to remote side that is appropriate for the
121 private String
deviceTypeResponse() {
124 // "I am a VT100 with advanced video option" (often VT102)
133 // "I am a VT220" - 7 bit version
135 return "\033[?62;1;6c";
137 // "I am a VT220" - 8 bit version
138 return "\u009b?62;1;6c";
140 throw new IllegalArgumentException("Invalid device type: " + type
);
145 * Return the proper TERM environment variable for this device type.
147 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
148 * @return "vt100", "xterm", etc.
150 public static String
deviceTypeTerm(final DeviceType deviceType
) {
151 switch (deviceType
) {
165 throw new IllegalArgumentException("Invalid device type: "
171 * Return the proper LANG for this device type. Only XTERM devices know
172 * about UTF-8, the others are defined by their standard to be either
173 * 7-bit or 8-bit characters only.
175 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
176 * @param baseLang a base language without UTF-8 flag such as "C" or
178 * @return "en_US", "en_US.UTF-8", etc.
180 public static String
deviceTypeLang(final DeviceType deviceType
,
181 final String baseLang
) {
183 switch (deviceType
) {
191 return baseLang
+ ".UTF-8";
194 throw new IllegalArgumentException("Invalid device type: "
200 * Write a string directly to the remote side.
202 * @param str string to send
204 private void writeRemote(final String str
) {
205 if (stopReaderThread
) {
206 // Reader hit EOF, bail out now.
211 // System.err.printf("writeRemote() '%s'\n", str);
217 if (outputStream
== null) {
221 for (int i
= 0; i
< str
.length(); i
++) {
222 outputStream
.write(str
.charAt(i
));
224 outputStream
.flush();
225 } catch (IOException e
) {
231 if (output
== null) {
237 } catch (IOException e
) {
243 throw new IllegalArgumentException("Invalid device type: " + type
);
248 * Close the input and output streams and stop the reader thread. Note
249 * that it is safe to call this multiple times.
251 public final void close() {
253 // Synchronize so we don't stomp on the reader thread.
254 synchronized (this) {
256 // Close the input stream
261 if (inputStream
!= null) {
264 } catch (IOException e
) {
274 } catch (IOException e
) {
283 // Tell the reader thread to stop looking at input.
284 if (stopReaderThread
== false) {
285 stopReaderThread
= true;
288 } catch (InterruptedException e
) {
293 // Close the output stream.
298 if (outputStream
!= null) {
300 outputStream
.close();
301 } catch (IOException e
) {
308 if (output
!= null) {
311 } catch (IOException e
) {
318 throw new IllegalArgumentException("Invalid device type: "
321 } // synchronized (this)
325 * When true, the reader thread is expected to exit.
327 private volatile boolean stopReaderThread
= false;
332 private Thread readerThread
= null;
335 * See if the reader thread is still running.
337 * @return if true, we are still connected to / reading from the remote
340 public final boolean isReading() {
341 return (!stopReaderThread
);
345 * The type of emulator to be.
347 private DeviceType type
= DeviceType
.VT102
;
350 * Obtain a new blank display line for an external user
351 * (e.g. TTerminalWindow).
353 * @return new blank line
355 public final DisplayLine
getBlankDisplayLine() {
356 return new DisplayLine(currentState
.attr
);
360 * The scrollback buffer characters + attributes.
362 private volatile List
<DisplayLine
> scrollback
;
365 * Get the scrollback buffer.
367 * @return the scrollback buffer
369 public final List
<DisplayLine
> getScrollbackBuffer() {
374 * The raw display buffer characters + attributes.
376 private volatile List
<DisplayLine
> display
;
379 * Get the display buffer.
381 * @return the display buffer
383 public final List
<DisplayLine
> getDisplayBuffer() {
388 * The terminal's input. For type == XTERM, this is an InputStreamReader
389 * with UTF-8 encoding.
391 private Reader input
;
394 * The terminal's raw InputStream. This is used for type != XTERM.
396 private volatile InputStream inputStream
;
399 * The terminal's output. For type == XTERM, this wraps an
400 * OutputStreamWriter with UTF-8 encoding.
402 private Writer output
;
405 * The terminal's raw OutputStream. This is used for type != XTERM.
407 private OutputStream outputStream
;
410 * Parser character scan states.
427 VT52_DIRECT_CURSOR_ADDRESS
431 * Current scanning state.
433 private ScanState scanState
;
436 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
437 * can't really use it in keypress() because we do not see number pad
438 * events from TKeypress.
440 private enum KeypadMode
{
446 * Arrow keys can emit three different sequences (DECCKM or VT52
449 private enum ArrowKeyMode
{
456 * Available character sets for GL, GR, G0, G1, G2, G3.
458 private enum CharacterSet
{
479 * Single-shift states used by the C1 control characters SS2 (0x8E) and
482 private enum Singleshift
{
489 * VT220+ lockshift states.
491 private enum LockshiftMode
{
501 * XTERM mouse reporting protocols.
503 private enum MouseProtocol
{
512 * Which mouse protocol is active.
514 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
517 * XTERM mouse reporting encodings.
519 private enum MouseEncoding
{
526 * Which mouse encoding is active.
528 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
531 * Physical display width. We start at 80x24, but the user can resize us
537 * Get the display width.
539 * @return the width (usually 80 or 132)
541 public final int getWidth() {
546 * Physical display height. We start at 80x24, but the user can resize
552 * Get the display height.
554 * @return the height (usually 24)
556 public final int getHeight() {
561 * Top margin of the scrolling region.
563 private int scrollRegionTop
;
566 * Bottom margin of the scrolling region.
568 private int scrollRegionBottom
;
571 * Right margin column number. This can be selected by the remote side
572 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
574 private int rightMargin
;
577 * Last character printed.
582 * VT100-style line wrapping: a character is placed in column 80 (or
583 * 132), but the line does NOT wrap until another character is written to
584 * column 1 of the next line, after which the cursor moves to column 2.
586 private boolean wrapLineFlag
;
589 * VT220 single shift flag.
591 private Singleshift singleshift
= Singleshift
.NONE
;
594 * true = insert characters, false = overwrite.
596 private boolean insertMode
= false;
599 * VT52 mode as selected by DECANM. True means VT52, false means
600 * ANSI. Default is ANSI.
602 private boolean vt52Mode
= false;
605 * Visible cursor (DECTCEM).
607 private boolean cursorVisible
= true;
610 * Get visible cursor flag.
612 * @return if true, the cursor is visible
614 public final boolean isCursorVisible() {
615 return cursorVisible
;
619 * Screen title as set by the xterm OSC sequence. Lots of applications
620 * send a screenTitle regardless of whether it is an xterm client or not.
622 private String screenTitle
= "";
625 * Get the screen title as set by the xterm OSC sequence. Lots of
626 * applications send a screenTitle regardless of whether it is an xterm
629 * @return screen title
631 public final String
getScreenTitle() {
636 * Parameter characters being collected.
638 private List
<Integer
> csiParams
;
641 * Non-csi collect buffer.
643 private StringBuilder collectBuffer
;
646 * When true, use the G1 character set.
648 private boolean shiftOut
= false;
651 * Horizontal tab stop locations.
653 private List
<Integer
> tabStops
;
656 * S8C1T. True means 8bit controls, false means 7bit controls.
658 private boolean s8c1t
= false;
661 * Printer mode. True means send all output to printer, which discards
664 private boolean printerControllerMode
= false;
667 * LMN line mode. If true, linefeed() puts the cursor on the first
668 * column of the next line. If false, linefeed() puts the cursor one
669 * line down on the current line. The default is false.
671 private boolean newLineMode
= false;
674 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
676 private ArrowKeyMode arrowKeyMode
;
679 * Whether number pad keys send VT100 or VT52, application or numeric
682 @SuppressWarnings("unused")
683 private KeypadMode keypadMode
;
686 * When true, the terminal is in 132-column mode (DECCOLM).
688 private boolean columns132
= false;
691 * Get 132 columns value.
693 * @return if true, the terminal is in 132 column mode
695 public final boolean isColumns132() {
700 * true = reverse video. Set by DECSCNM.
702 private boolean reverseVideo
= false;
705 * false = echo characters locally.
707 private boolean fullDuplex
= true;
710 * DECSC/DECRC save/restore a subset of the total state. This class
711 * encapsulates those specific flags/modes.
713 private class SaveableState
{
716 * When true, cursor positions are relative to the scrolling region.
718 public boolean originMode
= false;
721 * The current editing X position.
723 public int cursorX
= 0;
726 * The current editing Y position.
728 public int cursorY
= 0;
731 * Which character set is currently selected in G0.
733 public CharacterSet g0Charset
= CharacterSet
.US
;
736 * Which character set is currently selected in G1.
738 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
741 * Which character set is currently selected in G2.
743 public CharacterSet g2Charset
= CharacterSet
.US
;
746 * Which character set is currently selected in G3.
748 public CharacterSet g3Charset
= CharacterSet
.US
;
751 * Which character set is currently selected in GR.
753 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
756 * The current drawing attributes.
758 public CellAttributes attr
;
763 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
768 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
773 public boolean lineWrap
= true;
778 public void reset() {
782 g0Charset
= CharacterSet
.US
;
783 g1Charset
= CharacterSet
.DRAWING
;
784 g2Charset
= CharacterSet
.US
;
785 g3Charset
= CharacterSet
.US
;
786 grCharset
= CharacterSet
.DRAWING
;
787 attr
= new CellAttributes();
788 glLockshift
= LockshiftMode
.NONE
;
789 grLockshift
= LockshiftMode
.NONE
;
794 * Copy attributes from another instance.
796 * @param that the other instance to match
798 public void setTo(final SaveableState that
) {
799 this.originMode
= that
.originMode
;
800 this.cursorX
= that
.cursorX
;
801 this.cursorY
= that
.cursorY
;
802 this.g0Charset
= that
.g0Charset
;
803 this.g1Charset
= that
.g1Charset
;
804 this.g2Charset
= that
.g2Charset
;
805 this.g3Charset
= that
.g3Charset
;
806 this.grCharset
= that
.grCharset
;
807 this.attr
= new CellAttributes();
808 this.attr
.setTo(that
.attr
);
809 this.glLockshift
= that
.glLockshift
;
810 this.grLockshift
= that
.grLockshift
;
811 this.lineWrap
= that
.lineWrap
;
815 * Public constructor.
817 public SaveableState() {
823 * The current terminal state.
825 private SaveableState currentState
;
828 * The last saved terminal state.
830 private SaveableState savedState
;
833 * Clear the CSI parameters and flags.
835 private void toGround() {
837 collectBuffer
= new StringBuilder(8);
838 scanState
= ScanState
.GROUND
;
842 * Reset the tab stops list.
844 private void resetTabStops() {
846 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
847 tabStops
.add(new Integer(i
* 8));
852 * Reset the emulation state.
854 private void reset() {
856 currentState
= new SaveableState();
857 savedState
= new SaveableState();
858 scanState
= ScanState
.GROUND
;
862 scrollRegionBottom
= height
- 1;
863 rightMargin
= width
- 1;
865 arrowKeyMode
= ArrowKeyMode
.ANSI
;
866 keypadMode
= KeypadMode
.Numeric
;
867 wrapLineFlag
= false;
875 reverseVideo
= false;
877 cursorVisible
= true;
880 singleshift
= Singleshift
.NONE
;
882 printerControllerMode
= false;
885 mouseProtocol
= MouseProtocol
.OFF
;
886 mouseEncoding
= MouseEncoding
.X10
;
896 * Public constructor.
898 * @param type one of the DeviceType constants to select VT100, VT102,
900 * @param inputStream an InputStream connected to the remote side. For
901 * type == XTERM, inputStream is converted to a Reader with UTF-8
903 * @param outputStream an OutputStream connected to the remote user. For
904 * type == XTERM, outputStream is converted to a Writer with UTF-8
906 * @throws UnsupportedEncodingException if an exception is thrown when
907 * creating the InputStreamReader
909 public ECMA48(final DeviceType type
, final InputStream inputStream
,
910 final OutputStream outputStream
) throws UnsupportedEncodingException
{
912 assert (inputStream
!= null);
913 assert (outputStream
!= null);
915 csiParams
= new ArrayList
<Integer
>();
916 tabStops
= new ArrayList
<Integer
>();
917 scrollback
= new LinkedList
<DisplayLine
>();
918 display
= new LinkedList
<DisplayLine
>();
921 this.inputStream
= inputStream
;
922 if (type
== DeviceType
.XTERM
) {
923 this.input
= new InputStreamReader(inputStream
, "UTF-8");
924 this.output
= new OutputStreamWriter(outputStream
, "UTF-8");
925 this.outputStream
= null;
928 this.outputStream
= outputStream
;
932 for (int i
= 0; i
< height
; i
++) {
933 display
.add(new DisplayLine(currentState
.attr
));
936 // Spin up the input reader
937 readerThread
= new Thread(this);
938 readerThread
.start();
942 * Append a new line to the bottom of the display, adding lines off the
943 * top to the scrollback buffer.
945 private void newDisplayLine() {
946 // Scroll the top line off into the scrollback buffer
947 scrollback
.add(display
.get(0));
949 DisplayLine line
= new DisplayLine(currentState
.attr
);
950 line
.setReverseColor(reverseVideo
);
955 * Wraps the current line.
957 private void wrapCurrentLine() {
958 if (currentState
.cursorY
== height
- 1) {
961 if (currentState
.cursorY
< height
- 1) {
962 currentState
.cursorY
++;
964 currentState
.cursorX
= 0;
968 * Handle a carriage return.
970 private void carriageReturn() {
971 currentState
.cursorX
= 0;
972 wrapLineFlag
= false;
976 * Reverse the color of the visible display.
978 private void invertDisplayColors() {
979 for (DisplayLine line
: display
) {
980 line
.setReverseColor(!line
.isReverseColor());
987 private void linefeed() {
989 if (currentState
.cursorY
< scrollRegionBottom
) {
990 // Increment screen y
991 currentState
.cursorY
++;
994 // Screen y does not increment
997 * Two cases: either we're inside a scrolling region or not. If
998 * the scrolling region bottom is the bottom of the screen, then
999 * push the top line into the buffer. Else scroll the scrolling
1002 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1004 // We're at the bottom of the scroll region, AND the scroll
1005 // region is the entire screen.
1011 // We're at the bottom of the scroll region, AND the scroll
1012 // region is NOT the entire screen.
1013 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1018 currentState
.cursorX
= 0;
1020 wrapLineFlag
= false;
1024 * Prints one character to the display buffer.
1026 * @param ch character to display
1028 private void printCharacter(final char ch
) {
1029 int rightMargin
= this.rightMargin
;
1031 // Check if we have double-width, and if so chop at 40/66 instead of
1033 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1034 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1037 // Check the unusually-complicated line wrapping conditions...
1038 if (currentState
.cursorX
== rightMargin
) {
1040 if (currentState
.lineWrap
== true) {
1042 * This case happens when: the cursor was already on the
1043 * right margin (either through printing or by an explicit
1044 * placement command), and a character was printed.
1046 * The line wraps only when a new character arrives AND the
1047 * cursor is already on the right margin AND has placed a
1048 * character in its cell. Easier to see than to explain.
1050 if (wrapLineFlag
== false) {
1052 * This block marks the case that we are in the margin
1053 * and the first character has been received and printed.
1055 wrapLineFlag
= true;
1058 * This block marks the case that we are in the margin
1059 * and the second character has been received and
1062 wrapLineFlag
= false;
1066 } else if (currentState
.cursorX
<= rightMargin
) {
1068 * This is the normal case: a character came in and was printed
1069 * to the left of the right margin column.
1072 // Turn off VT100 special-case flag
1073 wrapLineFlag
= false;
1076 // "Print" the character
1077 Cell newCell
= new Cell(ch
);
1078 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1079 newCellAttributes
.setTo(currentState
.attr
);
1080 DisplayLine line
= display
.get(currentState
.cursorY
);
1081 // Insert mode special case
1082 if (insertMode
== true) {
1083 line
.insert(currentState
.cursorX
, newCell
);
1085 // Replace an existing character
1086 line
.replace(currentState
.cursorX
, newCell
);
1089 // Increment horizontal
1090 if (wrapLineFlag
== false) {
1091 currentState
.cursorX
++;
1092 if (currentState
.cursorX
> rightMargin
) {
1093 currentState
.cursorX
--;
1099 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1100 * send to the remote side.
1102 * @param mouse mouse event received from the local user
1104 public void mouse(final TMouseEvent mouse
) {
1107 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1108 mouseProtocol, mouseEncoding, mouse);
1111 if (mouseEncoding
== MouseEncoding
.X10
) {
1112 // We will support X10 but only for (160,94) and smaller.
1113 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1118 switch (mouseProtocol
) {
1125 // Only report button presses
1126 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1132 // Only report button presses and releases
1133 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1134 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1142 * Only report button presses, button releases, and motions that
1143 * have a button down (i.e. drag-and-drop).
1145 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1146 if (!mouse
.isMouse1()
1147 && !mouse
.isMouse2()
1148 && !mouse
.isMouse3()
1149 && !mouse
.isMouseWheelUp()
1150 && !mouse
.isMouseWheelDown()
1158 // Report everything
1162 // Now encode the event
1163 StringBuilder sb
= new StringBuilder(6);
1164 if (mouseEncoding
== MouseEncoding
.SGR
) {
1165 sb
.append((char) 0x1B);
1168 if (mouse
.isMouse1()) {
1169 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1174 } else if (mouse
.isMouse2()) {
1175 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1180 } else if (mouse
.isMouse3()) {
1181 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1186 } else if (mouse
.isMouseWheelUp()) {
1188 } else if (mouse
.isMouseWheelDown()) {
1191 // This is motion with no buttons down.
1195 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1198 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1205 // X10 and UTF8 encodings
1206 sb
.append((char) 0x1B);
1209 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1210 sb
.append((char) (0x03 + 32));
1211 } else if (mouse
.isMouse1()) {
1212 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1213 sb
.append((char) (0x00 + 32 + 32));
1215 sb
.append((char) (0x00 + 32));
1217 } else if (mouse
.isMouse2()) {
1218 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1219 sb
.append((char) (0x01 + 32 + 32));
1221 sb
.append((char) (0x01 + 32));
1223 } else if (mouse
.isMouse3()) {
1224 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1225 sb
.append((char) (0x02 + 32 + 32));
1227 sb
.append((char) (0x02 + 32));
1229 } else if (mouse
.isMouseWheelUp()) {
1230 sb
.append((char) (0x04 + 64));
1231 } else if (mouse
.isMouseWheelDown()) {
1232 sb
.append((char) (0x05 + 64));
1234 // This is motion with no buttons down.
1235 sb
.append((char) (0x03 + 32));
1238 sb
.append((char) (mouse
.getX() + 33));
1239 sb
.append((char) (mouse
.getY() + 33));
1242 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1243 writeRemote(sb
.toString());
1247 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1248 * send to the remote side.
1250 * @param keypress keypress received from the local user
1252 public void keypress(final TKeypress keypress
) {
1253 writeRemote(keypressToString(keypress
));
1257 * Build one of the complex xterm keystroke sequences, storing the result in
1258 * xterm_keystroke_buffer.
1260 * @param ss3 the prefix to use based on VT100 state.
1261 * @param first the first character, usually a number.
1262 * @param first the last character, one of the following: ~ A B C D F H
1263 * @param ctrl whether or not ctrl is down
1264 * @param alt whether or not alt is down
1265 * @param shift whether or not shift is down
1266 * @return the buffer with the full key sequence
1268 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1269 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1271 StringBuilder sb
= new StringBuilder(ss3
);
1272 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1276 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1278 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1280 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1282 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1284 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1286 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1288 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1293 return sb
.toString();
1297 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1299 * @param keypress keypress received from the local user
1300 * @return string to transmit to the remote side
1302 private String
keypressToString(final TKeypress keypress
) {
1304 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1306 * If this is a control character, process it like it came from
1309 if (keypress
.getChar() < 0x20) {
1310 handleControlChar(keypress
.getChar());
1312 // Local echo for everything else
1313 printCharacter(keypress
.getChar());
1317 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1322 // Handle control characters
1323 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1324 StringBuilder sb
= new StringBuilder();
1325 char ch
= keypress
.getChar();
1328 return sb
.toString();
1331 // Handle alt characters
1332 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1333 StringBuilder sb
= new StringBuilder("\033");
1334 char ch
= keypress
.getChar();
1336 return sb
.toString();
1339 if (keypress
.equals(kbBackspace
)) {
1352 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1355 switch (arrowKeyMode
) {
1357 return xtermBuildKeySequence("\033[", '1', 'D',
1358 keypress
.isCtrl(), keypress
.isAlt(),
1359 keypress
.isShift());
1361 return xtermBuildKeySequence("\033", '1', 'D',
1362 keypress
.isCtrl(), keypress
.isAlt(),
1363 keypress
.isShift());
1365 return xtermBuildKeySequence("\033O", '1', 'D',
1366 keypress
.isCtrl(), keypress
.isAlt(),
1367 keypress
.isShift());
1370 switch (arrowKeyMode
) {
1381 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1384 switch (arrowKeyMode
) {
1386 return xtermBuildKeySequence("\033[", '1', 'C',
1387 keypress
.isCtrl(), keypress
.isAlt(),
1388 keypress
.isShift());
1390 return xtermBuildKeySequence("\033", '1', 'C',
1391 keypress
.isCtrl(), keypress
.isAlt(),
1392 keypress
.isShift());
1394 return xtermBuildKeySequence("\033O", '1', 'C',
1395 keypress
.isCtrl(), keypress
.isAlt(),
1396 keypress
.isShift());
1399 switch (arrowKeyMode
) {
1410 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1413 switch (arrowKeyMode
) {
1415 return xtermBuildKeySequence("\033[", '1', 'A',
1416 keypress
.isCtrl(), keypress
.isAlt(),
1417 keypress
.isShift());
1419 return xtermBuildKeySequence("\033", '1', 'A',
1420 keypress
.isCtrl(), keypress
.isAlt(),
1421 keypress
.isShift());
1423 return xtermBuildKeySequence("\033O", '1', 'A',
1424 keypress
.isCtrl(), keypress
.isAlt(),
1425 keypress
.isShift());
1428 switch (arrowKeyMode
) {
1439 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1442 switch (arrowKeyMode
) {
1444 return xtermBuildKeySequence("\033[", '1', 'B',
1445 keypress
.isCtrl(), keypress
.isAlt(),
1446 keypress
.isShift());
1448 return xtermBuildKeySequence("\033", '1', 'B',
1449 keypress
.isCtrl(), keypress
.isAlt(),
1450 keypress
.isShift());
1452 return xtermBuildKeySequence("\033O", '1', 'B',
1453 keypress
.isCtrl(), keypress
.isAlt(),
1454 keypress
.isShift());
1457 switch (arrowKeyMode
) {
1468 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1471 switch (arrowKeyMode
) {
1473 return xtermBuildKeySequence("\033[", '1', 'H',
1474 keypress
.isCtrl(), keypress
.isAlt(),
1475 keypress
.isShift());
1477 return xtermBuildKeySequence("\033", '1', 'H',
1478 keypress
.isCtrl(), keypress
.isAlt(),
1479 keypress
.isShift());
1481 return xtermBuildKeySequence("\033O", '1', 'H',
1482 keypress
.isCtrl(), keypress
.isAlt(),
1483 keypress
.isShift());
1486 switch (arrowKeyMode
) {
1497 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1500 switch (arrowKeyMode
) {
1502 return xtermBuildKeySequence("\033[", '1', 'F',
1503 keypress
.isCtrl(), keypress
.isAlt(),
1504 keypress
.isShift());
1506 return xtermBuildKeySequence("\033", '1', 'F',
1507 keypress
.isCtrl(), keypress
.isAlt(),
1508 keypress
.isShift());
1510 return xtermBuildKeySequence("\033O", '1', 'F',
1511 keypress
.isCtrl(), keypress
.isAlt(),
1512 keypress
.isShift());
1515 switch (arrowKeyMode
) {
1526 if (keypress
.equals(kbF1
)) {
1534 if (keypress
.equals(kbF2
)) {
1542 if (keypress
.equals(kbF3
)) {
1550 if (keypress
.equals(kbF4
)) {
1558 if (keypress
.equals(kbF5
)) {
1571 if (keypress
.equals(kbF6
)) {
1584 if (keypress
.equals(kbF7
)) {
1597 if (keypress
.equals(kbF8
)) {
1610 if (keypress
.equals(kbF9
)) {
1623 if (keypress
.equals(kbF10
)) {
1636 if (keypress
.equals(kbF11
)) {
1640 if (keypress
.equals(kbF12
)) {
1644 if (keypress
.equals(kbShiftF1
)) {
1649 if (type
== DeviceType
.XTERM
) {
1655 if (keypress
.equals(kbShiftF2
)) {
1660 if (type
== DeviceType
.XTERM
) {
1666 if (keypress
.equals(kbShiftF3
)) {
1671 if (type
== DeviceType
.XTERM
) {
1677 if (keypress
.equals(kbShiftF4
)) {
1682 if (type
== DeviceType
.XTERM
) {
1688 if (keypress
.equals(kbShiftF5
)) {
1690 return "\033[15;2~";
1693 if (keypress
.equals(kbShiftF6
)) {
1695 return "\033[17;2~";
1698 if (keypress
.equals(kbShiftF7
)) {
1700 return "\033[18;2~";
1703 if (keypress
.equals(kbShiftF8
)) {
1705 return "\033[19;2~";
1708 if (keypress
.equals(kbShiftF9
)) {
1710 return "\033[20;2~";
1713 if (keypress
.equals(kbShiftF10
)) {
1715 return "\033[21;2~";
1718 if (keypress
.equals(kbShiftF11
)) {
1720 return "\033[23;2~";
1723 if (keypress
.equals(kbShiftF12
)) {
1725 return "\033[24;2~";
1728 if (keypress
.equals(kbCtrlF1
)) {
1733 if (type
== DeviceType
.XTERM
) {
1739 if (keypress
.equals(kbCtrlF2
)) {
1744 if (type
== DeviceType
.XTERM
) {
1750 if (keypress
.equals(kbCtrlF3
)) {
1755 if (type
== DeviceType
.XTERM
) {
1761 if (keypress
.equals(kbCtrlF4
)) {
1766 if (type
== DeviceType
.XTERM
) {
1772 if (keypress
.equals(kbCtrlF5
)) {
1774 return "\033[15;5~";
1777 if (keypress
.equals(kbCtrlF6
)) {
1779 return "\033[17;5~";
1782 if (keypress
.equals(kbCtrlF7
)) {
1784 return "\033[18;5~";
1787 if (keypress
.equals(kbCtrlF8
)) {
1789 return "\033[19;5~";
1792 if (keypress
.equals(kbCtrlF9
)) {
1794 return "\033[20;5~";
1797 if (keypress
.equals(kbCtrlF10
)) {
1799 return "\033[21;5~";
1802 if (keypress
.equals(kbCtrlF11
)) {
1804 return "\033[23;5~";
1807 if (keypress
.equals(kbCtrlF12
)) {
1809 return "\033[24;5~";
1812 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
1815 switch (arrowKeyMode
) {
1817 return xtermBuildKeySequence("\033[", '5', '~',
1818 keypress
.isCtrl(), keypress
.isAlt(),
1819 keypress
.isShift());
1821 return xtermBuildKeySequence("\033", '5', '~',
1822 keypress
.isCtrl(), keypress
.isAlt(),
1823 keypress
.isShift());
1825 return xtermBuildKeySequence("\033O", '5', '~',
1826 keypress
.isCtrl(), keypress
.isAlt(),
1827 keypress
.isShift());
1834 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
1837 switch (arrowKeyMode
) {
1839 return xtermBuildKeySequence("\033[", '6', '~',
1840 keypress
.isCtrl(), keypress
.isAlt(),
1841 keypress
.isShift());
1843 return xtermBuildKeySequence("\033", '6', '~',
1844 keypress
.isCtrl(), keypress
.isAlt(),
1845 keypress
.isShift());
1847 return xtermBuildKeySequence("\033O", '6', '~',
1848 keypress
.isCtrl(), keypress
.isAlt(),
1849 keypress
.isShift());
1856 if (keypress
.equalsWithoutModifiers(kbIns
)) {
1859 switch (arrowKeyMode
) {
1861 return xtermBuildKeySequence("\033[", '2', '~',
1862 keypress
.isCtrl(), keypress
.isAlt(),
1863 keypress
.isShift());
1865 return xtermBuildKeySequence("\033", '2', '~',
1866 keypress
.isCtrl(), keypress
.isAlt(),
1867 keypress
.isShift());
1869 return xtermBuildKeySequence("\033O", '2', '~',
1870 keypress
.isCtrl(), keypress
.isAlt(),
1871 keypress
.isShift());
1878 if (keypress
.equalsWithoutModifiers(kbDel
)) {
1881 switch (arrowKeyMode
) {
1883 return xtermBuildKeySequence("\033[", '3', '~',
1884 keypress
.isCtrl(), keypress
.isAlt(),
1885 keypress
.isShift());
1887 return xtermBuildKeySequence("\033", '3', '~',
1888 keypress
.isCtrl(), keypress
.isAlt(),
1889 keypress
.isShift());
1891 return xtermBuildKeySequence("\033O", '3', '~',
1892 keypress
.isCtrl(), keypress
.isAlt(),
1893 keypress
.isShift());
1896 // Delete sends real delete for VTxxx
1901 if (keypress
.equals(kbEnter
)) {
1905 if (keypress
.equals(kbEsc
)) {
1909 if (keypress
.equals(kbAltEsc
)) {
1913 if (keypress
.equals(kbTab
)) {
1917 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
1918 (keypress
.equals(kbShiftTab
))
1928 // Non-alt, non-ctrl characters
1929 if (!keypress
.isFnKey()) {
1930 StringBuilder sb
= new StringBuilder();
1931 sb
.append(keypress
.getChar());
1932 return sb
.toString();
1938 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1941 * @param ch 8-bit character from the remote side
1942 * @param charsetGl character set defined for GL
1943 * @param charsetGr character set defined for GR
1944 * @return character to display on the screen
1946 private char mapCharacterCharset(final char ch
,
1947 final CharacterSet charsetGl
,
1948 final CharacterSet charsetGr
) {
1950 int lookupChar
= ch
;
1951 CharacterSet lookupCharset
= charsetGl
;
1954 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
1955 lookupCharset
= charsetGr
;
1959 switch (lookupCharset
) {
1962 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
1965 return DECCharacterSets
.UK
[lookupChar
];
1968 return DECCharacterSets
.US_ASCII
[lookupChar
];
1971 return DECCharacterSets
.NL
[lookupChar
];
1974 return DECCharacterSets
.FI
[lookupChar
];
1977 return DECCharacterSets
.FR
[lookupChar
];
1980 return DECCharacterSets
.FR_CA
[lookupChar
];
1983 return DECCharacterSets
.DE
[lookupChar
];
1986 return DECCharacterSets
.IT
[lookupChar
];
1989 return DECCharacterSets
.NO
[lookupChar
];
1992 return DECCharacterSets
.ES
[lookupChar
];
1995 return DECCharacterSets
.SV
[lookupChar
];
1998 return DECCharacterSets
.SWISS
[lookupChar
];
2000 case DEC_SUPPLEMENTAL
:
2001 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2004 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2007 return DECCharacterSets
.US_ASCII
[lookupChar
];
2010 return DECCharacterSets
.US_ASCII
[lookupChar
];
2013 throw new IllegalArgumentException("Invalid character set value: "
2019 * Map an 8-bit byte into a printable character.
2021 * @param ch either 8-bit or Unicode character from the remote side
2022 * @return character to display on the screen
2024 private char mapCharacter(final char ch
) {
2026 // Unicode character, just return it
2030 CharacterSet charsetGl
= currentState
.g0Charset
;
2031 CharacterSet charsetGr
= currentState
.grCharset
;
2033 if (vt52Mode
== true) {
2034 if (shiftOut
== true) {
2035 // Shifted out character, pull from VT52 graphics
2036 charsetGl
= currentState
.g1Charset
;
2037 charsetGr
= CharacterSet
.US
;
2040 charsetGl
= currentState
.g0Charset
;
2041 charsetGr
= CharacterSet
.US
;
2044 // Pull the character
2045 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2049 if (shiftOut
== true) {
2050 // Shifted out character, pull from G1
2051 charsetGl
= currentState
.g1Charset
;
2052 charsetGr
= currentState
.grCharset
;
2054 // Pull the character
2055 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2059 if (singleshift
== Singleshift
.SS2
) {
2061 singleshift
= Singleshift
.NONE
;
2063 // Shifted out character, pull from G2
2064 charsetGl
= currentState
.g2Charset
;
2065 charsetGr
= currentState
.grCharset
;
2069 if (singleshift
== Singleshift
.SS3
) {
2071 singleshift
= Singleshift
.NONE
;
2073 // Shifted out character, pull from G3
2074 charsetGl
= currentState
.g3Charset
;
2075 charsetGr
= currentState
.grCharset
;
2078 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2079 // Check for locking shift
2081 switch (currentState
.glLockshift
) {
2094 charsetGl
= currentState
.g2Charset
;
2099 charsetGl
= currentState
.g3Charset
;
2104 charsetGl
= currentState
.g0Charset
;
2108 switch (currentState
.grLockshift
) {
2118 charsetGr
= currentState
.g1Charset
;
2123 charsetGr
= currentState
.g2Charset
;
2128 charsetGr
= currentState
.g3Charset
;
2133 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2140 // Pull the character
2141 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2145 * Scroll the text within a scrolling region up n lines.
2147 * @param regionTop top row of the scrolling region
2148 * @param regionBottom bottom row of the scrolling region
2149 * @param n number of lines to scroll
2151 private void scrollingRegionScrollUp(final int regionTop
,
2152 final int regionBottom
, final int n
) {
2154 if (regionTop
>= regionBottom
) {
2158 // Sanity check: see if there will be any characters left after the
2160 if (regionBottom
+ 1 - regionTop
<= n
) {
2161 // There won't be anything left in the region, so just call
2162 // eraseScreen() and return.
2163 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2167 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2168 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2169 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2171 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2172 - remaining
, regionBottom
+ 1);
2173 display
= new LinkedList
<DisplayLine
>(displayTop
);
2174 display
.addAll(displayMiddle
);
2175 for (int i
= 0; i
< n
; i
++) {
2176 DisplayLine line
= new DisplayLine(currentState
.attr
);
2177 line
.setReverseColor(reverseVideo
);
2180 display
.addAll(displayBottom
);
2182 assert (display
.size() == height
);
2186 * Scroll the text within a scrolling region down n lines.
2188 * @param regionTop top row of the scrolling region
2189 * @param regionBottom bottom row of the scrolling region
2190 * @param n number of lines to scroll
2192 private void scrollingRegionScrollDown(final int regionTop
,
2193 final int regionBottom
, final int n
) {
2195 if (regionTop
>= regionBottom
) {
2199 // Sanity check: see if there will be any characters left after the
2201 if (regionBottom
+ 1 - regionTop
<= n
) {
2202 // There won't be anything left in the region, so just call
2203 // eraseScreen() and return.
2204 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2208 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2209 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2210 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2212 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2213 regionTop
+ remaining
);
2214 display
= new LinkedList
<DisplayLine
>(displayTop
);
2215 for (int i
= 0; i
< n
; i
++) {
2216 DisplayLine line
= new DisplayLine(currentState
.attr
);
2217 line
.setReverseColor(reverseVideo
);
2220 display
.addAll(displayMiddle
);
2221 display
.addAll(displayBottom
);
2223 assert (display
.size() == height
);
2227 * Process a control character.
2229 * @param ch 8-bit character from the remote side
2231 private void handleControlChar(final char ch
) {
2232 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2243 // Transmit the answerback message.
2254 cursorLeft(1, false);
2259 advanceToNextTabStop();
2285 currentState
.glLockshift
= LockshiftMode
.NONE
;
2291 currentState
.glLockshift
= LockshiftMode
.NONE
;
2316 singleshift
= Singleshift
.SS2
;
2321 singleshift
= Singleshift
.SS3
;
2331 * Advance the cursor to the next tab stop.
2333 private void advanceToNextTabStop() {
2334 if (tabStops
.size() == 0) {
2335 // Go to the rightmost column
2336 cursorRight(rightMargin
- currentState
.cursorX
, false);
2339 for (Integer stop
: tabStops
) {
2340 if (stop
> currentState
.cursorX
) {
2341 cursorRight(stop
- currentState
.cursorX
, false);
2346 * We got here, meaning there isn't a tab stop beyond the current
2347 * cursor position. Place the cursor of the right-most edge of the
2350 cursorRight(rightMargin
- currentState
.cursorX
, false);
2354 * Save a character into the collect buffer.
2356 * @param ch character to save
2358 private void collect(final char ch
) {
2359 collectBuffer
.append(ch
);
2363 * Save a byte into the CSI parameters buffer.
2365 * @param ch byte to save
2367 private void param(final byte ch
) {
2368 if (csiParams
.size() == 0) {
2369 csiParams
.add(new Integer(0));
2371 Integer x
= csiParams
.get(csiParams
.size() - 1);
2372 if ((ch
>= '0') && (ch
<= '9')) {
2375 csiParams
.set(csiParams
.size() - 1, x
);
2379 csiParams
.add(new Integer(0));
2384 * Get a CSI parameter value, with a default.
2386 * @param position parameter index. 0 is the first parameter.
2387 * @param defaultValue value to use if csiParams[position] doesn't exist
2388 * @return parameter value
2390 private int getCsiParam(final int position
, final int defaultValue
) {
2391 if (csiParams
.size() < position
+ 1) {
2392 return defaultValue
;
2394 return csiParams
.get(position
).intValue();
2398 * Get a CSI parameter value, clamped to within min/max.
2400 * @param position parameter index. 0 is the first parameter.
2401 * @param defaultValue value to use if csiParams[position] doesn't exist
2402 * @param minValue minimum value inclusive
2403 * @param maxValue maximum value inclusive
2404 * @return parameter value
2406 private int getCsiParam(final int position
, final int defaultValue
,
2407 final int minValue
, final int maxValue
) {
2409 assert (minValue
<= maxValue
);
2410 int value
= getCsiParam(position
, defaultValue
);
2411 if (value
< minValue
) {
2414 if (value
> maxValue
) {
2421 * Set or unset a toggle.
2423 * @param value true for set ('h'), false for reset ('l')
2425 private void setToggle(final boolean value
) {
2426 boolean decPrivateModeFlag
= false;
2427 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2428 if (collectBuffer
.charAt(i
) == '?') {
2429 decPrivateModeFlag
= true;
2434 for (Integer i
: csiParams
) {
2439 if (decPrivateModeFlag
== true) {
2441 if (value
== true) {
2442 // Use application arrow keys
2443 arrowKeyMode
= ArrowKeyMode
.VT100
;
2445 // Use ANSI arrow keys
2446 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2451 if (decPrivateModeFlag
== true) {
2452 if (value
== false) {
2456 arrowKeyMode
= ArrowKeyMode
.VT52
;
2459 * From the VT102 docs: "You use ANSI mode to select
2460 * most terminal features; the terminal uses the same
2461 * features when it switches to VT52 mode. You
2462 * cannot, however, change most of these features in
2465 * In other words, do not reset any other attributes
2466 * when switching between VT52 submode and ANSI.
2468 * HOWEVER, the real vt100 does switch the character
2469 * set according to Usenet.
2471 currentState
.g0Charset
= CharacterSet
.US
;
2472 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2475 if ((type
== DeviceType
.VT220
)
2476 || (type
== DeviceType
.XTERM
)) {
2478 // VT52 mode is explicitly 7-bit
2480 singleshift
= Singleshift
.NONE
;
2485 if (value
== true) {
2486 // Turn off keyboard
2495 if (decPrivateModeFlag
== true) {
2497 if (value
== true) {
2506 width
= rightMargin
+ 1;
2507 // Entire screen is cleared, and scrolling region is
2509 eraseScreen(0, 0, height
- 1, width
- 1, false);
2510 scrollRegionTop
= 0;
2511 scrollRegionBottom
= height
- 1;
2512 // Also home the cursor
2513 cursorPosition(0, 0);
2517 if (decPrivateModeFlag
== true) {
2519 if (value
== true) {
2528 if (value
== true) {
2536 if (decPrivateModeFlag
== true) {
2538 if (value
== true) {
2540 * Set selects reverse screen, a white screen
2541 * background with black characters.
2543 if (reverseVideo
!= true) {
2545 * If in normal video, switch it back
2547 invertDisplayColors();
2549 reverseVideo
= true;
2552 * Reset selects normal screen, a black screen
2553 * background with white characters.
2555 if (reverseVideo
== true) {
2557 * If in reverse video already, switch it back
2559 invertDisplayColors();
2561 reverseVideo
= false;
2566 if (decPrivateModeFlag
== true) {
2568 if (value
== true) {
2569 // Origin is relative to scroll region cursor.
2570 // Cursor can NEVER leave scrolling region.
2571 currentState
.originMode
= true;
2572 cursorPosition(0, 0);
2574 // Origin is absolute to entire screen. Cursor can
2575 // leave the scrolling region via cup() and hvp().
2576 currentState
.originMode
= false;
2577 cursorPosition(0, 0);
2582 if (decPrivateModeFlag
== true) {
2584 if (value
== true) {
2586 currentState
.lineWrap
= true;
2588 // Turn linewrap off
2589 currentState
.lineWrap
= false;
2594 if (decPrivateModeFlag
== true) {
2596 if (value
== true) {
2597 // Keyboard auto-repeat on
2600 // Keyboard auto-repeat off
2606 if (decPrivateModeFlag
== false) {
2608 if (value
== true) {
2618 if (decPrivateModeFlag
== true) {
2624 if (decPrivateModeFlag
== true) {
2630 if (decPrivateModeFlag
== false) {
2632 if (value
== true) {
2634 * Set causes a received linefeed, form feed, or
2635 * vertical tab to move cursor to first column of
2636 * next line. RETURN transmits both a carriage return
2637 * and linefeed. This selection is also called new
2643 * Reset causes a received linefeed, form feed, or
2644 * vertical tab to move cursor to next line in
2645 * current column. RETURN transmits a carriage
2648 newLineMode
= false;
2654 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2655 if (decPrivateModeFlag
== true) {
2657 if (value
== true) {
2659 cursorVisible
= true;
2662 cursorVisible
= false;
2669 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2670 if (decPrivateModeFlag
== true) {
2672 if (value
== true) {
2673 // Select national mode NRC
2676 // Select multi-national mode
2685 if ((type
== DeviceType
.XTERM
)
2686 && (decPrivateModeFlag
== true)
2688 // Mouse: normal tracking mode
2689 if (value
== true) {
2690 mouseProtocol
= MouseProtocol
.NORMAL
;
2692 mouseProtocol
= MouseProtocol
.OFF
;
2698 if ((type
== DeviceType
.XTERM
)
2699 && (decPrivateModeFlag
== true)
2701 // Mouse: normal tracking mode
2702 if (value
== true) {
2703 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2705 mouseProtocol
= MouseProtocol
.OFF
;
2711 if ((type
== DeviceType
.XTERM
)
2712 && (decPrivateModeFlag
== true)
2714 // Mouse: Any-event tracking mode
2715 if (value
== true) {
2716 mouseProtocol
= MouseProtocol
.ANYEVENT
;
2718 mouseProtocol
= MouseProtocol
.OFF
;
2724 if ((type
== DeviceType
.XTERM
)
2725 && (decPrivateModeFlag
== true)
2727 // Mouse: UTF-8 coordinates
2728 if (value
== true) {
2729 mouseEncoding
= MouseEncoding
.UTF8
;
2731 mouseEncoding
= MouseEncoding
.X10
;
2737 if ((type
== DeviceType
.XTERM
)
2738 && (decPrivateModeFlag
== true)
2740 // Mouse: SGR coordinates
2741 if (value
== true) {
2742 mouseEncoding
= MouseEncoding
.SGR
;
2744 mouseEncoding
= MouseEncoding
.X10
;
2757 * DECSC - Save cursor.
2759 private void decsc() {
2760 savedState
.setTo(currentState
);
2764 * DECRC - Restore cursor.
2766 private void decrc() {
2767 currentState
.setTo(savedState
);
2773 private void ind() {
2774 // Move the cursor and scroll if necessary. If at the bottom line
2775 // already, a scroll up is supposed to be performed.
2776 if (currentState
.cursorY
== scrollRegionBottom
) {
2777 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2779 cursorDown(1, true);
2783 * RI - Reverse index.
2786 // Move the cursor and scroll if necessary. If at the top line
2787 // already, a scroll down is supposed to be performed.
2788 if (currentState
.cursorY
== scrollRegionTop
) {
2789 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2797 private void nel() {
2798 // Move the cursor and scroll if necessary. If at the bottom line
2799 // already, a scroll up is supposed to be performed.
2800 if (currentState
.cursorY
== scrollRegionBottom
) {
2801 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2803 cursorDown(1, true);
2805 // Reset to the beginning of the next line
2806 currentState
.cursorX
= 0;
2810 * DECKPAM - Keypad application mode.
2812 private void deckpam() {
2813 keypadMode
= KeypadMode
.Application
;
2817 * DECKPNM - Keypad numeric mode.
2819 private void deckpnm() {
2820 keypadMode
= KeypadMode
.Numeric
;
2826 * @param n number of spaces to move
2827 * @param honorScrollRegion if true, then do nothing if the cursor is
2828 * outside the scrolling region
2830 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2834 * Special case: if a user moves the cursor from the right margin, we
2835 * have to reset the VT100 right margin flag.
2838 wrapLineFlag
= false;
2841 for (int i
= 0; i
< n
; i
++) {
2842 if (honorScrollRegion
== true) {
2843 // Honor the scrolling region
2844 if ((currentState
.cursorY
< scrollRegionTop
)
2845 || (currentState
.cursorY
> scrollRegionBottom
)
2847 // Outside region, do nothing
2850 // Inside region, go up
2851 top
= scrollRegionTop
;
2853 // Non-scrolling case
2857 if (currentState
.cursorY
> top
) {
2858 currentState
.cursorY
--;
2864 * Move down n spaces.
2866 * @param n number of spaces to move
2867 * @param honorScrollRegion if true, then do nothing if the cursor is
2868 * outside the scrolling region
2870 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
2874 * Special case: if a user moves the cursor from the right margin, we
2875 * have to reset the VT100 right margin flag.
2878 wrapLineFlag
= false;
2881 for (int i
= 0; i
< n
; i
++) {
2883 if (honorScrollRegion
== true) {
2884 // Honor the scrolling region
2885 if (currentState
.cursorY
> scrollRegionBottom
) {
2886 // Outside region, do nothing
2889 // Inside region, go down
2890 bottom
= scrollRegionBottom
;
2892 // Non-scrolling case
2893 bottom
= height
- 1;
2896 if (currentState
.cursorY
< bottom
) {
2897 currentState
.cursorY
++;
2903 * Move left n spaces.
2905 * @param n number of spaces to move
2906 * @param honorScrollRegion if true, then do nothing if the cursor is
2907 * outside the scrolling region
2909 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
2911 * Special case: if a user moves the cursor from the right margin, we
2912 * have to reset the VT100 right margin flag.
2915 wrapLineFlag
= false;
2918 for (int i
= 0; i
< n
; i
++) {
2919 if (honorScrollRegion
== true) {
2920 // Honor the scrolling region
2921 if ((currentState
.cursorY
< scrollRegionTop
)
2922 || (currentState
.cursorY
> scrollRegionBottom
)
2924 // Outside region, do nothing
2929 if (currentState
.cursorX
> 0) {
2930 currentState
.cursorX
--;
2936 * Move right n spaces.
2938 * @param n number of spaces to move
2939 * @param honorScrollRegion if true, then do nothing if the cursor is
2940 * outside the scrolling region
2942 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
2943 int rightMargin
= this.rightMargin
;
2946 * Special case: if a user moves the cursor from the right margin, we
2947 * have to reset the VT100 right margin flag.
2950 wrapLineFlag
= false;
2953 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2954 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2957 for (int i
= 0; i
< n
; i
++) {
2958 if (honorScrollRegion
== true) {
2959 // Honor the scrolling region
2960 if ((currentState
.cursorY
< scrollRegionTop
)
2961 || (currentState
.cursorY
> scrollRegionBottom
)
2963 // Outside region, do nothing
2968 if (currentState
.cursorX
< rightMargin
) {
2969 currentState
.cursorX
++;
2975 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2977 * @param row row to move to
2978 * @param col column to move to
2980 private void cursorPosition(int row
, final int col
) {
2981 int rightMargin
= this.rightMargin
;
2986 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2987 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2990 // Set column number
2991 currentState
.cursorX
= col
;
2993 // Sanity check, bring column back to margin.
2994 if (currentState
.cursorX
> rightMargin
) {
2995 currentState
.cursorX
= rightMargin
;
2999 if (currentState
.originMode
== true) {
3000 row
+= scrollRegionTop
;
3002 if (currentState
.cursorY
< row
) {
3003 cursorDown(row
- currentState
.cursorY
, false);
3004 } else if (currentState
.cursorY
> row
) {
3005 cursorUp(currentState
.cursorY
- row
, false);
3008 wrapLineFlag
= false;
3012 * HTS - Horizontal tabulation set.
3014 private void hts() {
3015 for (Integer stop
: tabStops
) {
3016 if (stop
== currentState
.cursorX
) {
3017 // Already have a tab stop here
3022 // Append a tab stop to the end of the array and resort them
3023 tabStops
.add(currentState
.cursorX
);
3024 Collections
.sort(tabStops
);
3028 * DECSWL - Single-width line.
3030 private void decswl() {
3031 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3032 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3036 * DECDWL - Double-width line.
3038 private void decdwl() {
3039 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3040 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3044 * DECHDL - Double-height + double-width line.
3046 * @param topHalf if true, this sets the row to be the top half row of a
3049 private void dechdl(final boolean topHalf
) {
3050 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3051 if (topHalf
== true) {
3052 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3054 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3059 * DECALN - Screen alignment display.
3061 private void decaln() {
3062 Cell newCell
= new Cell();
3063 newCell
.setChar('E');
3064 for (DisplayLine line
: display
) {
3065 for (int i
= 0; i
< line
.length(); i
++) {
3066 line
.replace(i
, newCell
);
3072 * DECSCL - Compatibility level.
3074 private void decscl() {
3075 int i
= getCsiParam(0, 0);
3076 int j
= getCsiParam(1, 0);
3080 currentState
.g0Charset
= CharacterSet
.US
;
3081 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3083 } else if (i
== 62) {
3085 if ((j
== 0) || (j
== 2)) {
3087 } else if (j
== 1) {
3094 * CUD - Cursor down.
3096 private void cud() {
3097 cursorDown(getCsiParam(0, 1, 1, height
), true);
3101 * CUF - Cursor forward.
3103 private void cuf() {
3104 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3108 * CUB - Cursor backward.
3110 private void cub() {
3111 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3117 private void cuu() {
3118 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3122 * CUP - Cursor position.
3124 private void cup() {
3125 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3126 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3130 * CNL - Cursor down and to column 1.
3132 private void cnl() {
3133 cursorDown(getCsiParam(0, 1, 1, height
), true);
3135 cursorLeft(currentState
.cursorX
, true);
3139 * CPL - Cursor up and to column 1.
3141 private void cpl() {
3142 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3144 cursorLeft(currentState
.cursorX
, true);
3148 * CHA - Cursor to column # in current row.
3150 private void cha() {
3151 cursorPosition(currentState
.cursorY
,
3152 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3156 * VPA - Cursor to row #, same column.
3158 private void vpa() {
3159 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3160 currentState
.cursorX
);
3164 * ED - Erase in display.
3167 boolean honorProtected
= false;
3168 boolean decPrivateModeFlag
= false;
3170 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3171 if (collectBuffer
.charAt(i
) == '?') {
3172 decPrivateModeFlag
= true;
3177 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3178 && (decPrivateModeFlag
== true)
3180 honorProtected
= true;
3183 int i
= getCsiParam(0, 0);
3186 // Erase from here to end of screen
3187 if (currentState
.cursorY
< height
- 1) {
3188 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3191 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3192 } else if (i
== 1) {
3193 // Erase from beginning of screen to here
3194 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3196 eraseLine(0, currentState
.cursorX
, honorProtected
);
3197 } else if (i
== 2) {
3198 // Erase entire screen
3199 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3204 * EL - Erase in line.
3207 boolean honorProtected
= false;
3208 boolean decPrivateModeFlag
= false;
3210 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3211 if (collectBuffer
.charAt(i
) == '?') {
3212 decPrivateModeFlag
= true;
3217 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3218 && (decPrivateModeFlag
== true)
3220 honorProtected
= true;
3223 int i
= getCsiParam(0, 0);
3226 // Erase from here to end of line
3227 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3228 } else if (i
== 1) {
3229 // Erase from beginning of line to here
3230 eraseLine(0, currentState
.cursorX
, honorProtected
);
3231 } else if (i
== 2) {
3232 // Erase entire line
3233 eraseLine(0, width
- 1, honorProtected
);
3238 * ECH - Erase # of characters in current row.
3240 private void ech() {
3241 int i
= getCsiParam(0, 1, 1, width
);
3243 // Erase from here to i characters
3244 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3251 int i
= getCsiParam(0, 1);
3253 if ((currentState
.cursorY
>= scrollRegionTop
)
3254 && (currentState
.cursorY
<= scrollRegionBottom
)
3257 // I can get the same effect with a scroll-down
3258 scrollingRegionScrollDown(currentState
.cursorY
,
3259 scrollRegionBottom
, i
);
3264 * DCH - Delete char.
3266 private void dch() {
3267 int n
= getCsiParam(0, 1);
3268 DisplayLine line
= display
.get(currentState
.cursorY
);
3269 Cell blank
= new Cell();
3270 for (int i
= 0; i
< n
; i
++) {
3271 line
.delete(currentState
.cursorX
, blank
);
3276 * ICH - Insert blank char at cursor.
3278 private void ich() {
3279 int n
= getCsiParam(0, 1);
3280 DisplayLine line
= display
.get(currentState
.cursorY
);
3281 Cell blank
= new Cell();
3282 for (int i
= 0; i
< n
; i
++) {
3283 line
.insert(currentState
.cursorX
, blank
);
3291 int i
= getCsiParam(0, 1);
3293 if ((currentState
.cursorY
>= scrollRegionTop
)
3294 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3296 // I can get the same effect with a scroll-down
3297 scrollingRegionScrollUp(currentState
.cursorY
,
3298 scrollRegionBottom
, i
);
3303 * HVP - Horizontal and vertical position.
3305 private void hvp() {
3310 * REP - Repeat character.
3312 private void rep() {
3313 int n
= getCsiParam(0, 1);
3314 for (int i
= 0; i
< n
; i
++) {
3315 printCharacter(repCh
);
3323 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3324 getCsiParam(0, 1, 1, height
));
3331 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3332 getCsiParam(0, 1, 1, height
));
3336 * CBT - Go back X tab stops.
3338 private void cbt() {
3339 int tabsToMove
= getCsiParam(0, 1);
3342 for (int i
= 0; i
< tabsToMove
; i
++) {
3343 int j
= currentState
.cursorX
;
3344 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3345 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3353 j
= tabStops
.get(tabI
);
3355 cursorPosition(currentState
.cursorY
, j
);
3360 * CHT - Advance X tab stops.
3362 private void cht() {
3363 int n
= getCsiParam(0, 1);
3364 for (int i
= 0; i
< n
; i
++) {
3365 advanceToNextTabStop();
3370 * SGR - Select graphics rendition.
3372 private void sgr() {
3374 if (csiParams
.size() == 0) {
3375 currentState
.attr
.reset();
3379 for (Integer i
: csiParams
) {
3385 currentState
.attr
.reset();
3390 currentState
.attr
.setBold(true);
3395 currentState
.attr
.setUnderline(true);
3400 currentState
.attr
.setBlink(true);
3405 currentState
.attr
.setReverse(true);
3412 if (type
== DeviceType
.XTERM
) {
3426 if ((type
== DeviceType
.VT220
)
3427 || (type
== DeviceType
.XTERM
)) {
3433 currentState
.attr
.setBold(false);
3438 currentState
.attr
.setUnderline(false);
3443 currentState
.attr
.setBlink(false);
3448 currentState
.attr
.setReverse(false);
3456 // A true VT100/102/220 does not support color, however everyone
3457 // is used to their terminal emulator supporting color so we will
3458 // unconditionally support color for all DeviceType's.
3463 // Set black foreground
3464 currentState
.attr
.setForeColor(Color
.BLACK
);
3467 // Set red foreground
3468 currentState
.attr
.setForeColor(Color
.RED
);
3471 // Set green foreground
3472 currentState
.attr
.setForeColor(Color
.GREEN
);
3475 // Set yellow foreground
3476 currentState
.attr
.setForeColor(Color
.YELLOW
);
3479 // Set blue foreground
3480 currentState
.attr
.setForeColor(Color
.BLUE
);
3483 // Set magenta foreground
3484 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3487 // Set cyan foreground
3488 currentState
.attr
.setForeColor(Color
.CYAN
);
3491 // Set white foreground
3492 currentState
.attr
.setForeColor(Color
.WHITE
);
3495 if (type
== DeviceType
.XTERM
) {
3497 * Xterm supports T.416 / ISO-8613-3 codes to select
3498 * either an indexed color or an RGB value. (It also
3499 * permits these ISO-8613-3 SGR sequences to be separated
3500 * by colons rather than semicolons.)
3502 * We will not support any of these additional color
3503 * codes at this time:
3505 * 1. http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
3506 * has a detailed discussion of the current state of
3507 * RGB in various terminals, the point of which is
3508 * that none of them really do the same thing despite
3509 * all appearing to be "xterm".
3512 * https://bugs.kde.org/show_bug.cgi?id=107487#c3,
3513 * even supporting just the "indexed mode" of these
3514 * sequences (which could align easily with existing
3515 * SGR colors) is assumed to mean full support of
3516 * 24-bit RGB. So it is all or nothing.
3518 * Finally, these sequences break the assumptions of
3519 * standard ECMA-48 style parsers as pointed out at
3520 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
3521 * Therefore in order to keep a clean display, we cannot
3522 * parse anything else in this sequence.
3526 // Underscore on, default foreground color
3527 currentState
.attr
.setUnderline(true);
3528 currentState
.attr
.setForeColor(Color
.WHITE
);
3532 // Underscore off, default foreground color
3533 currentState
.attr
.setUnderline(false);
3534 currentState
.attr
.setForeColor(Color
.WHITE
);
3537 // Set black background
3538 currentState
.attr
.setBackColor(Color
.BLACK
);
3541 // Set red background
3542 currentState
.attr
.setBackColor(Color
.RED
);
3545 // Set green background
3546 currentState
.attr
.setBackColor(Color
.GREEN
);
3549 // Set yellow background
3550 currentState
.attr
.setBackColor(Color
.YELLOW
);
3553 // Set blue background
3554 currentState
.attr
.setBackColor(Color
.BLUE
);
3557 // Set magenta background
3558 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3561 // Set cyan background
3562 currentState
.attr
.setBackColor(Color
.CYAN
);
3565 // Set white background
3566 currentState
.attr
.setBackColor(Color
.WHITE
);
3569 if (type
== DeviceType
.XTERM
) {
3571 * Xterm supports T.416 / ISO-8613-3 codes to select
3572 * either an indexed color or an RGB value. (It also
3573 * permits these ISO-8613-3 SGR sequences to be separated
3574 * by colons rather than semicolons.)
3576 * We will not support this at this time. Also, in order
3577 * to keep a clean display, we cannot parse anything else
3584 // Default background
3585 currentState
.attr
.setBackColor(Color
.BLACK
);
3595 * DA - Device attributes.
3598 int extendedFlag
= 0;
3600 if (collectBuffer
.length() > 0) {
3601 String args
= collectBuffer
.substring(1);
3602 if (collectBuffer
.charAt(0) == '>') {
3604 if (collectBuffer
.length() >= 2) {
3605 i
= Integer
.parseInt(args
.toString());
3607 } else if (collectBuffer
.charAt(0) == '=') {
3609 if (collectBuffer
.length() >= 2) {
3610 i
= Integer
.parseInt(args
.toString());
3613 // Unknown code, bail out
3618 if ((i
!= 0) && (i
!= 1)) {
3622 if ((extendedFlag
== 0) && (i
== 0)) {
3623 // Send string directly to remote side
3624 writeRemote(deviceTypeResponse());
3628 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3630 if ((extendedFlag
== 1) && (i
== 0)) {
3632 * Request "What type of terminal are you, what is your
3633 * firmware version, and what hardware options do you have
3636 * Respond: "I am a VT220 (identification code of 1), my
3637 * firmware version is _____ (Pv), and I have _____ Po
3638 * options installed."
3644 if (s8c1t
== true) {
3645 writeRemote("\u009b>1;10;0c");
3647 writeRemote("\033[>1;10;0c");
3653 if ((extendedFlag
== 2) && (i
== 0)) {
3656 * Request "What is your unit ID?"
3658 * Respond: "I was manufactured at site 00 and have a unique ID
3662 writeRemote("\033P!|00010203\033\\");
3667 * DECSTBM - Set top and bottom margins.
3669 private void decstbm() {
3670 boolean decPrivateModeFlag
= false;
3672 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3673 if (collectBuffer
.charAt(i
) == '?') {
3674 decPrivateModeFlag
= true;
3678 if (decPrivateModeFlag
) {
3679 // This could be restore DEC private mode values.
3683 int top
= getCsiParam(0, 1, 1, height
) - 1;
3684 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3689 scrollRegionTop
= top
;
3690 scrollRegionBottom
= bottom
;
3693 cursorPosition(0, 0);
3698 * DECREQTPARM - Request terminal parameters.
3700 private void decreqtparm() {
3701 int i
= getCsiParam(0, 0);
3703 if ((i
!= 0) && (i
!= 1)) {
3710 * Request terminal parameters.
3714 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3715 * (CLoCk MULtiplier = 1, STP option flags = 0)
3719 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3722 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3724 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3730 * DECSCA - Select Character Attributes.
3732 private void decsca() {
3733 int i
= getCsiParam(0, 0);
3735 if ((i
== 0) || (i
== 2)) {
3737 currentState
.attr
.setProtect(false);
3741 currentState
.attr
.setProtect(true);
3746 * DECSTR - Soft Terminal Reset.
3748 private void decstr() {
3749 // Do exactly like RIS - Reset to initial state
3751 // Do I clear screen too? I think so...
3752 eraseScreen(0, 0, height
- 1, width
- 1, false);
3753 cursorPosition(0, 0);
3757 * DSR - Device status report.
3759 private void dsr() {
3760 boolean decPrivateModeFlag
= false;
3761 int row
= currentState
.cursorY
;
3763 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3764 if (collectBuffer
.charAt(i
) == '?') {
3765 decPrivateModeFlag
= true;
3770 int i
= getCsiParam(0, 0);
3775 // Request status report. Respond with "OK, no malfunction."
3777 // Send string directly to remote side
3778 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3781 writeRemote("\u009b0n");
3783 writeRemote("\033[0n");
3788 // Request cursor position. Respond with current position.
3789 if (currentState
.originMode
== true) {
3790 row
-= scrollRegionTop
;
3793 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3796 str
= String
.format("\u009b%d;%dR", row
+ 1,
3797 currentState
.cursorX
+ 1);
3799 str
= String
.format("\033[%d;%dR", row
+ 1,
3800 currentState
.cursorX
+ 1);
3803 // Send string directly to remote side
3808 if (decPrivateModeFlag
== true) {
3810 // Request printer status report. Respond with "Printer not
3813 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3814 && (s8c1t
== true)) {
3815 writeRemote("\u009b?13n");
3817 writeRemote("\033[?13n");
3823 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3824 && (decPrivateModeFlag
== true)
3827 // Request user-defined keys are locked or unlocked. Respond
3828 // with "User-defined keys are locked."
3830 if (s8c1t
== true) {
3831 writeRemote("\u009b?21n");
3833 writeRemote("\033[?21n");
3839 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3840 && (decPrivateModeFlag
== true)
3843 // Request keyboard language. Respond with "Keyboard
3844 // language is North American."
3846 if (s8c1t
== true) {
3847 writeRemote("\u009b?27;1n");
3849 writeRemote("\033[?27;1n");
3856 // Some other option, ignore
3862 * TBC - Tabulation clear.
3864 private void tbc() {
3865 int i
= getCsiParam(0, 0);
3867 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3868 for (Integer stop
: tabStops
) {
3869 if (stop
== currentState
.cursorX
) {
3874 tabStops
= newStops
;
3882 * Erase the characters in the current line from the start column to the
3883 * end column, inclusive.
3885 * @param start starting column to erase (between 0 and width - 1)
3886 * @param end ending column to erase (between 0 and width - 1)
3887 * @param honorProtected if true, do not erase characters with the
3888 * protected attribute set
3890 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3895 if (end
> width
- 1) {
3902 for (int i
= start
; i
<= end
; i
++) {
3903 DisplayLine line
= display
.get(currentState
.cursorY
);
3904 if ((!honorProtected
)
3905 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
3912 * From the VT102 manual:
3914 * Erasing a character also erases any character
3915 * attribute of the character.
3921 * Erase with the current color a.k.a. back-color erase
3924 line
.setChar(i
, ' ');
3925 line
.setAttr(i
, currentState
.attr
);
3933 * Erase a rectangular section of the screen, inclusive. end column,
3936 * @param startRow starting row to erase (between 0 and height - 1)
3937 * @param startCol starting column to erase (between 0 and width - 1)
3938 * @param endRow ending row to erase (between 0 and height - 1)
3939 * @param endCol ending column to erase (between 0 and width - 1)
3940 * @param honorProtected if true, do not erase characters with the
3941 * protected attribute set
3943 private void eraseScreen(final int startRow
, final int startCol
,
3944 final int endRow
, final int endCol
, final boolean honorProtected
) {
3952 || (endRow
< startRow
)
3953 || (endCol
< startCol
)
3958 oldCursorY
= currentState
.cursorY
;
3959 for (int i
= startRow
; i
<= endRow
; i
++) {
3960 currentState
.cursorY
= i
;
3961 eraseLine(startCol
, endCol
, honorProtected
);
3963 // Erase display clears the double attributes
3964 display
.get(i
).setDoubleWidth(false);
3965 display
.get(i
).setDoubleHeight(0);
3967 currentState
.cursorY
= oldCursorY
;
3971 * VT220 printer functions. All of these are parsed, but won't do
3974 private void printerFunctions() {
3975 boolean decPrivateModeFlag
= false;
3976 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3977 if (collectBuffer
.charAt(i
) == '?') {
3978 decPrivateModeFlag
= true;
3983 int i
= getCsiParam(0, 0);
3988 if (decPrivateModeFlag
== false) {
3994 if (decPrivateModeFlag
== true) {
3995 // Print cursor line
4000 if (decPrivateModeFlag
== true) {
4001 // Auto print mode OFF
4003 // Printer controller OFF
4005 // Characters re-appear on the screen
4006 printerControllerMode
= false;
4011 if (decPrivateModeFlag
== true) {
4015 // Printer controller
4017 // Characters get sucked into oblivion
4018 printerControllerMode
= true;
4029 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4030 * of remote systems will send an XTerm title sequence even if TERM isn't
4033 * @param xtermChar the character received from the remote side
4035 private void oscPut(final char xtermChar
) {
4037 collectBuffer
.append(xtermChar
);
4040 if (xtermChar
== 0x07) {
4041 String args
= collectBuffer
.substring(0,
4042 collectBuffer
.length() - 1);
4043 String
[] p
= args
.toString().split(";");
4045 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4053 // Go to SCAN_GROUND state
4060 * Run this input character through the ECMA48 state machine.
4062 * @param ch character from the remote side
4064 private void consume(char ch
) {
4067 // System.err.printf("%c", ch);
4069 // Special case for VT10x: 7-bit characters only
4070 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4071 ch
= (char)(ch
& 0x7F);
4074 // Special "anywhere" states
4076 // 18, 1A --> execute, then switch to SCAN_GROUND
4077 if ((ch
== 0x18) || (ch
== 0x1A)) {
4078 // CAN and SUB abort escape sequences
4083 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4087 && (scanState
!= ScanState
.DCS_ENTRY
)
4088 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4089 && (scanState
!= ScanState
.DCS_IGNORE
)
4090 && (scanState
!= ScanState
.DCS_PARAM
)
4091 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4094 scanState
= ScanState
.ESCAPE
;
4098 // 0x9B == CSI 8-bit sequence
4100 scanState
= ScanState
.CSI_ENTRY
;
4104 // 0x9D goes to ScanState.OSC_STRING
4106 scanState
= ScanState
.OSC_STRING
;
4110 // 0x90 goes to DCS_ENTRY
4112 scanState
= ScanState
.DCS_ENTRY
;
4116 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4117 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4118 scanState
= ScanState
.SOSPMAPC_STRING
;
4122 // 0x7F (DEL) is always discarded
4127 switch (scanState
) {
4130 // 00-17, 19, 1C-1F --> execute
4131 // 80-8F, 91-9A, 9C --> execute
4132 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4133 handleControlChar(ch
);
4137 if (((ch
>= 0x20) && (ch
<= 0x7F))
4141 // VT220 printer --> trash bin
4142 if (((type
== DeviceType
.VT220
)
4143 || (type
== DeviceType
.XTERM
))
4144 && (printerControllerMode
== true)
4149 // Hang onto this character
4150 repCh
= mapCharacter(ch
);
4152 // Print this character
4153 printCharacter(repCh
);
4158 // 00-17, 19, 1C-1F --> execute
4160 handleControlChar(ch
);
4164 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4165 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4167 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4171 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4172 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4183 // DECSC - Save cursor
4184 // Note this code overlaps both ANSI and VT52 mode
4189 // DECRC - Restore cursor
4190 // Note this code overlaps both ANSI and VT52 mode
4199 if (vt52Mode
== true) {
4200 // DECANM - Enter ANSI mode
4202 arrowKeyMode
= ArrowKeyMode
.VT100
;
4205 * From the VT102 docs: "You use ANSI mode to select
4206 * most terminal features; the terminal uses the same
4207 * features when it switches to VT52 mode. You
4208 * cannot, however, change most of these features in
4211 * In other words, do not reset any other attributes
4212 * when switching between VT52 submode and ANSI.
4216 currentState
.g0Charset
= CharacterSet
.US
;
4217 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4219 singleshift
= Singleshift
.NONE
;
4220 currentState
.glLockshift
= LockshiftMode
.NONE
;
4221 currentState
.grLockshift
= LockshiftMode
.NONE
;
4225 // DECKPAM - Keypad application mode
4226 // Note this code overlaps both ANSI and VT52 mode
4230 // DECKPNM - Keypad numeric mode
4231 // Note this code overlaps both ANSI and VT52 mode
4238 if (vt52Mode
== true) {
4239 // Cursor up, and stop at the top without scrolling
4244 if (vt52Mode
== true) {
4245 // Cursor down, and stop at the bottom without scrolling
4246 cursorDown(1, false);
4250 if (vt52Mode
== true) {
4251 // Cursor right, and stop at the right without scrolling
4252 cursorRight(1, false);
4256 if (vt52Mode
== true) {
4257 // Cursor left, and stop at the left without scrolling
4258 cursorLeft(1, false);
4265 if (vt52Mode
== true) {
4273 if (vt52Mode
== true) {
4274 // G0 --> Special graphics
4275 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
4279 if (vt52Mode
== true) {
4281 currentState
.g0Charset
= CharacterSet
.US
;
4285 if (vt52Mode
== true) {
4287 cursorPosition(0, 0);
4289 // HTS - Horizontal tabulation set
4294 if (vt52Mode
== true) {
4295 // Reverse line feed. Same as RI.
4300 if (vt52Mode
== true) {
4301 // Erase to end of screen
4302 eraseLine(currentState
.cursorX
, width
- 1, false);
4303 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4308 if (vt52Mode
== true) {
4309 // Erase to end of line
4310 eraseLine(currentState
.cursorX
, width
- 1, false);
4316 if (vt52Mode
== true) {
4319 // RI - Reverse index
4324 if (vt52Mode
== false) {
4326 singleshift
= Singleshift
.SS2
;
4330 if (vt52Mode
== false) {
4332 singleshift
= Singleshift
.SS3
;
4339 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4355 if (vt52Mode
== true) {
4356 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4364 if (vt52Mode
== true) {
4366 // Send string directly to remote side
4367 writeRemote("\033/Z");
4370 // Send string directly to remote side
4371 writeRemote(deviceTypeResponse());
4382 // VT52 cannot get to any of these other states
4383 if (vt52Mode
== true) {
4388 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4395 // RIS - Reset to initial state
4397 // Do I clear screen too? I think so...
4398 eraseScreen(0, 0, height
- 1, width
- 1, false);
4399 cursorPosition(0, 0);
4413 if ((type
== DeviceType
.VT220
)
4414 || (type
== DeviceType
.XTERM
)) {
4416 // VT220 lockshift G2 into GL
4417 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4422 if ((type
== DeviceType
.VT220
)
4423 || (type
== DeviceType
.XTERM
)) {
4425 // VT220 lockshift G3 into GL
4426 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4444 if ((type
== DeviceType
.VT220
)
4445 || (type
== DeviceType
.XTERM
)) {
4447 // VT220 lockshift G3 into GR
4448 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4453 if ((type
== DeviceType
.VT220
)
4454 || (type
== DeviceType
.XTERM
)) {
4456 // VT220 lockshift G2 into GR
4457 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4463 if ((type
== DeviceType
.VT220
)
4464 || (type
== DeviceType
.XTERM
)) {
4466 // VT220 lockshift G1 into GR
4467 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4477 // 0x5B goes to CSI_ENTRY
4479 scanState
= ScanState
.CSI_ENTRY
;
4482 // 0x5D goes to OSC_STRING
4484 scanState
= ScanState
.OSC_STRING
;
4487 // 0x50 goes to DCS_ENTRY
4489 scanState
= ScanState
.DCS_ENTRY
;
4492 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4493 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4494 scanState
= ScanState
.SOSPMAPC_STRING
;
4499 case ESCAPE_INTERMEDIATE
:
4500 // 00-17, 19, 1C-1F --> execute
4502 handleControlChar(ch
);
4505 // 20-2F --> collect
4506 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4510 // 30-7E --> dispatch, then switch to GROUND
4511 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4514 if ((collectBuffer
.length() == 1)
4515 && (collectBuffer
.charAt(0) == '(')) {
4516 // G0 --> Special graphics
4517 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4519 if ((collectBuffer
.length() == 1)
4520 && (collectBuffer
.charAt(0) == ')')) {
4521 // G1 --> Special graphics
4522 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4524 if ((type
== DeviceType
.VT220
)
4525 || (type
== DeviceType
.XTERM
)) {
4527 if ((collectBuffer
.length() == 1)
4528 && (collectBuffer
.charAt(0) == '*')) {
4529 // G2 --> Special graphics
4530 currentState
.g2Charset
= CharacterSet
.DRAWING
;
4532 if ((collectBuffer
.length() == 1)
4533 && (collectBuffer
.charAt(0) == '+')) {
4534 // G3 --> Special graphics
4535 currentState
.g3Charset
= CharacterSet
.DRAWING
;
4540 if ((collectBuffer
.length() == 1)
4541 && (collectBuffer
.charAt(0) == '(')) {
4542 // G0 --> Alternate character ROM standard character set
4543 currentState
.g0Charset
= CharacterSet
.ROM
;
4545 if ((collectBuffer
.length() == 1)
4546 && (collectBuffer
.charAt(0) == ')')) {
4547 // G1 --> Alternate character ROM standard character set
4548 currentState
.g1Charset
= CharacterSet
.ROM
;
4552 if ((collectBuffer
.length() == 1)
4553 && (collectBuffer
.charAt(0) == '(')) {
4554 // G0 --> Alternate character ROM special graphics
4555 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4557 if ((collectBuffer
.length() == 1)
4558 && (collectBuffer
.charAt(0) == ')')) {
4559 // G1 --> Alternate character ROM special graphics
4560 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4564 if ((collectBuffer
.length() == 1)
4565 && (collectBuffer
.charAt(0) == '#')) {
4566 // DECDHL - Double-height line (top half)
4571 if ((collectBuffer
.length() == 1)
4572 && (collectBuffer
.charAt(0) == '#')) {
4573 // DECDHL - Double-height line (bottom half)
4576 if ((type
== DeviceType
.VT220
)
4577 || (type
== DeviceType
.XTERM
)) {
4579 if ((collectBuffer
.length() == 1)
4580 && (collectBuffer
.charAt(0) == '(')) {
4582 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4584 if ((collectBuffer
.length() == 1)
4585 && (collectBuffer
.charAt(0) == ')')) {
4587 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4589 if ((collectBuffer
.length() == 1)
4590 && (collectBuffer
.charAt(0) == '*')) {
4592 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4594 if ((collectBuffer
.length() == 1)
4595 && (collectBuffer
.charAt(0) == '+')) {
4597 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4602 if ((collectBuffer
.length() == 1)
4603 && (collectBuffer
.charAt(0) == '#')) {
4604 // DECSWL - Single-width line
4607 if ((type
== DeviceType
.VT220
)
4608 || (type
== DeviceType
.XTERM
)) {
4610 if ((collectBuffer
.length() == 1)
4611 && (collectBuffer
.charAt(0) == '(')) {
4613 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4615 if ((collectBuffer
.length() == 1)
4616 && (collectBuffer
.charAt(0) == ')')) {
4618 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4620 if ((collectBuffer
.length() == 1)
4621 && (collectBuffer
.charAt(0) == '*')) {
4623 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4625 if ((collectBuffer
.length() == 1)
4626 && (collectBuffer
.charAt(0) == '+')) {
4628 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4633 if ((collectBuffer
.length() == 1)
4634 && (collectBuffer
.charAt(0) == '#')) {
4635 // DECDWL - Double-width line
4638 if ((type
== DeviceType
.VT220
)
4639 || (type
== DeviceType
.XTERM
)) {
4641 if ((collectBuffer
.length() == 1)
4642 && (collectBuffer
.charAt(0) == '(')) {
4644 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4646 if ((collectBuffer
.length() == 1)
4647 && (collectBuffer
.charAt(0) == ')')) {
4649 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4651 if ((collectBuffer
.length() == 1)
4652 && (collectBuffer
.charAt(0) == '*')) {
4654 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4656 if ((collectBuffer
.length() == 1)
4657 && (collectBuffer
.charAt(0) == '+')) {
4659 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4664 if ((type
== DeviceType
.VT220
)
4665 || (type
== DeviceType
.XTERM
)) {
4667 if ((collectBuffer
.length() == 1)
4668 && (collectBuffer
.charAt(0) == '(')) {
4670 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4672 if ((collectBuffer
.length() == 1)
4673 && (collectBuffer
.charAt(0) == ')')) {
4675 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4677 if ((collectBuffer
.length() == 1)
4678 && (collectBuffer
.charAt(0) == '*')) {
4680 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4682 if ((collectBuffer
.length() == 1)
4683 && (collectBuffer
.charAt(0) == '+')) {
4685 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4690 if ((collectBuffer
.length() == 1)
4691 && (collectBuffer
.charAt(0) == '#')) {
4692 // DECALN - Screen alignment display
4701 if ((type
== DeviceType
.VT220
)
4702 || (type
== DeviceType
.XTERM
)) {
4704 if ((collectBuffer
.length() == 1)
4705 && (collectBuffer
.charAt(0) == '(')) {
4706 // G0 --> DEC_SUPPLEMENTAL
4707 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4709 if ((collectBuffer
.length() == 1)
4710 && (collectBuffer
.charAt(0) == ')')) {
4711 // G1 --> DEC_SUPPLEMENTAL
4712 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4714 if ((collectBuffer
.length() == 1)
4715 && (collectBuffer
.charAt(0) == '*')) {
4716 // G2 --> DEC_SUPPLEMENTAL
4717 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4719 if ((collectBuffer
.length() == 1)
4720 && (collectBuffer
.charAt(0) == '+')) {
4721 // G3 --> DEC_SUPPLEMENTAL
4722 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4727 if ((type
== DeviceType
.VT220
)
4728 || (type
== DeviceType
.XTERM
)) {
4730 if ((collectBuffer
.length() == 1)
4731 && (collectBuffer
.charAt(0) == '(')) {
4733 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4735 if ((collectBuffer
.length() == 1)
4736 && (collectBuffer
.charAt(0) == ')')) {
4738 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4740 if ((collectBuffer
.length() == 1)
4741 && (collectBuffer
.charAt(0) == '*')) {
4743 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4745 if ((collectBuffer
.length() == 1)
4746 && (collectBuffer
.charAt(0) == '+')) {
4748 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4757 if ((collectBuffer
.length() == 1)
4758 && (collectBuffer
.charAt(0) == '(')) {
4759 // G0 --> United Kingdom set
4760 currentState
.g0Charset
= CharacterSet
.UK
;
4762 if ((collectBuffer
.length() == 1)
4763 && (collectBuffer
.charAt(0) == ')')) {
4764 // G1 --> United Kingdom set
4765 currentState
.g1Charset
= CharacterSet
.UK
;
4767 if ((type
== DeviceType
.VT220
)
4768 || (type
== DeviceType
.XTERM
)) {
4770 if ((collectBuffer
.length() == 1)
4771 && (collectBuffer
.charAt(0) == '*')) {
4772 // G2 --> United Kingdom set
4773 currentState
.g2Charset
= CharacterSet
.UK
;
4775 if ((collectBuffer
.length() == 1)
4776 && (collectBuffer
.charAt(0) == '+')) {
4777 // G3 --> United Kingdom set
4778 currentState
.g3Charset
= CharacterSet
.UK
;
4783 if ((collectBuffer
.length() == 1)
4784 && (collectBuffer
.charAt(0) == '(')) {
4786 currentState
.g0Charset
= CharacterSet
.US
;
4788 if ((collectBuffer
.length() == 1)
4789 && (collectBuffer
.charAt(0) == ')')) {
4791 currentState
.g1Charset
= CharacterSet
.US
;
4793 if ((type
== DeviceType
.VT220
)
4794 || (type
== DeviceType
.XTERM
)) {
4796 if ((collectBuffer
.length() == 1)
4797 && (collectBuffer
.charAt(0) == '*')) {
4799 currentState
.g2Charset
= CharacterSet
.US
;
4801 if ((collectBuffer
.length() == 1)
4802 && (collectBuffer
.charAt(0) == '+')) {
4804 currentState
.g3Charset
= CharacterSet
.US
;
4809 if ((type
== DeviceType
.VT220
)
4810 || (type
== DeviceType
.XTERM
)) {
4812 if ((collectBuffer
.length() == 1)
4813 && (collectBuffer
.charAt(0) == '(')) {
4815 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4817 if ((collectBuffer
.length() == 1)
4818 && (collectBuffer
.charAt(0) == ')')) {
4820 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4822 if ((collectBuffer
.length() == 1)
4823 && (collectBuffer
.charAt(0) == '*')) {
4825 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4827 if ((collectBuffer
.length() == 1)
4828 && (collectBuffer
.charAt(0) == '+')) {
4830 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4837 if ((type
== DeviceType
.VT220
)
4838 || (type
== DeviceType
.XTERM
)) {
4840 if ((collectBuffer
.length() == 1)
4841 && (collectBuffer
.charAt(0) == '(')) {
4843 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4845 if ((collectBuffer
.length() == 1)
4846 && (collectBuffer
.charAt(0) == ')')) {
4848 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4850 if ((collectBuffer
.length() == 1)
4851 && (collectBuffer
.charAt(0) == '*')) {
4853 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4855 if ((collectBuffer
.length() == 1)
4856 && (collectBuffer
.charAt(0) == '+')) {
4858 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4863 if ((type
== DeviceType
.VT220
)
4864 || (type
== DeviceType
.XTERM
)) {
4866 if ((collectBuffer
.length() == 1)
4867 && (collectBuffer
.charAt(0) == ' ')) {
4874 if ((type
== DeviceType
.VT220
)
4875 || (type
== DeviceType
.XTERM
)) {
4877 if ((collectBuffer
.length() == 1)
4878 && (collectBuffer
.charAt(0) == ' ')) {
4885 if ((type
== DeviceType
.VT220
)
4886 || (type
== DeviceType
.XTERM
)) {
4888 if ((collectBuffer
.length() == 1)
4889 && (collectBuffer
.charAt(0) == '(')) {
4891 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4893 if ((collectBuffer
.length() == 1)
4894 && (collectBuffer
.charAt(0) == ')')) {
4896 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4898 if ((collectBuffer
.length() == 1)
4899 && (collectBuffer
.charAt(0) == '*')) {
4901 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4903 if ((collectBuffer
.length() == 1)
4904 && (collectBuffer
.charAt(0) == '+')) {
4906 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4914 if ((type
== DeviceType
.VT220
)
4915 || (type
== DeviceType
.XTERM
)) {
4917 if ((collectBuffer
.length() == 1)
4918 && (collectBuffer
.charAt(0) == '(')) {
4920 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4922 if ((collectBuffer
.length() == 1)
4923 && (collectBuffer
.charAt(0) == ')')) {
4925 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4927 if ((collectBuffer
.length() == 1)
4928 && (collectBuffer
.charAt(0) == '*')) {
4930 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4932 if ((collectBuffer
.length() == 1)
4933 && (collectBuffer
.charAt(0) == '+')) {
4935 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4946 if ((type
== DeviceType
.VT220
)
4947 || (type
== DeviceType
.XTERM
)) {
4949 if ((collectBuffer
.length() == 1)
4950 && (collectBuffer
.charAt(0) == '(')) {
4952 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4954 if ((collectBuffer
.length() == 1)
4955 && (collectBuffer
.charAt(0) == ')')) {
4957 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4959 if ((collectBuffer
.length() == 1)
4960 && (collectBuffer
.charAt(0) == '*')) {
4962 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4964 if ((collectBuffer
.length() == 1)
4965 && (collectBuffer
.charAt(0) == '+')) {
4967 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4972 if ((type
== DeviceType
.VT220
)
4973 || (type
== DeviceType
.XTERM
)) {
4975 if ((collectBuffer
.length() == 1)
4976 && (collectBuffer
.charAt(0) == '(')) {
4978 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4980 if ((collectBuffer
.length() == 1)
4981 && (collectBuffer
.charAt(0) == ')')) {
4983 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4985 if ((collectBuffer
.length() == 1)
4986 && (collectBuffer
.charAt(0) == '*')) {
4988 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
4990 if ((collectBuffer
.length() == 1)
4991 && (collectBuffer
.charAt(0) == '+')) {
4993 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5005 if ((type
== DeviceType
.VT220
)
5006 || (type
== DeviceType
.XTERM
)) {
5008 if ((collectBuffer
.length() == 1)
5009 && (collectBuffer
.charAt(0) == '(')) {
5011 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5013 if ((collectBuffer
.length() == 1)
5014 && (collectBuffer
.charAt(0) == ')')) {
5016 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5018 if ((collectBuffer
.length() == 1)
5019 && (collectBuffer
.charAt(0) == '*')) {
5021 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5023 if ((collectBuffer
.length() == 1)
5024 && (collectBuffer
.charAt(0) == '+')) {
5026 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5031 if ((type
== DeviceType
.VT220
)
5032 || (type
== DeviceType
.XTERM
)) {
5034 if ((collectBuffer
.length() == 1)
5035 && (collectBuffer
.charAt(0) == '(')) {
5037 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5039 if ((collectBuffer
.length() == 1)
5040 && (collectBuffer
.charAt(0) == ')')) {
5042 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5044 if ((collectBuffer
.length() == 1)
5045 && (collectBuffer
.charAt(0) == '*')) {
5047 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5049 if ((collectBuffer
.length() == 1)
5050 && (collectBuffer
.charAt(0) == '+')) {
5052 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5099 // 0x9C goes to GROUND
5107 // 00-17, 19, 1C-1F --> execute
5109 handleControlChar(ch
);
5112 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5113 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5115 scanState
= ScanState
.CSI_INTERMEDIATE
;
5118 // 30-39, 3B --> param, then switch to CSI_PARAM
5119 if ((ch
>= '0') && (ch
<= '9')) {
5121 scanState
= ScanState
.CSI_PARAM
;
5125 scanState
= ScanState
.CSI_PARAM
;
5128 // 3C-3F --> collect, then switch to CSI_PARAM
5129 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5131 scanState
= ScanState
.CSI_PARAM
;
5134 // 40-7E --> dispatch, then switch to GROUND
5135 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5138 // ICH - Insert character
5146 // CUD - Cursor down
5150 // CUF - Cursor forward
5154 // CUB - Cursor backward
5158 // CNL - Cursor down and to column 1
5159 if (type
== DeviceType
.XTERM
) {
5164 // CPL - Cursor up and to column 1
5165 if (type
== DeviceType
.XTERM
) {
5170 // CHA - Cursor to column # in current row
5171 if (type
== DeviceType
.XTERM
) {
5176 // CUP - Cursor position
5180 // CHT - Cursor forward X tab stops (default 1)
5181 if (type
== DeviceType
.XTERM
) {
5186 // ED - Erase in display
5190 // EL - Erase in line
5205 // DCH - Delete character
5212 // Scroll up X lines (default 1)
5213 if (type
== DeviceType
.XTERM
) {
5218 // Scroll down X lines (default 1)
5219 if (type
== DeviceType
.XTERM
) {
5228 if ((type
== DeviceType
.VT220
)
5229 || (type
== DeviceType
.XTERM
)) {
5231 // ECH - Erase character
5238 // CBT - Cursor backward X tab stops (default 1)
5239 if (type
== DeviceType
.XTERM
) {
5250 // HPA - Cursor to column # in current row. Same as CHA
5251 if (type
== DeviceType
.XTERM
) {
5256 // HPR - Cursor right. Same as CUF
5257 if (type
== DeviceType
.XTERM
) {
5262 // REP - Repeat last char X times
5263 if (type
== DeviceType
.XTERM
) {
5268 // DA - Device attributes
5272 // VPA - Cursor to row, current column.
5273 if (type
== DeviceType
.XTERM
) {
5278 // VPR - Cursor down. Same as CUD
5279 if (type
== DeviceType
.XTERM
) {
5284 // HVP - Horizontal and vertical position
5288 // TBC - Tabulation clear
5292 // Sets an ANSI or DEC private toggle
5296 if ((type
== DeviceType
.VT220
)
5297 || (type
== DeviceType
.XTERM
)) {
5299 // Printer functions
5307 // Sets an ANSI or DEC private toggle
5311 // SGR - Select graphics rendition
5315 // DSR - Device status report
5322 // DECLL - Load leds
5326 // DECSTBM - Set top and bottom margins
5330 // Save cursor (ANSI.SYS)
5331 if (type
== DeviceType
.XTERM
) {
5332 savedState
.cursorX
= currentState
.cursorX
;
5333 savedState
.cursorY
= currentState
.cursorY
;
5339 // Restore cursor (ANSI.SYS)
5340 if (type
== DeviceType
.XTERM
) {
5341 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5348 // DECREQTPARM - Request terminal parameters
5364 // 0x9C goes to GROUND
5369 // 0x3A goes to CSI_IGNORE
5371 scanState
= ScanState
.CSI_IGNORE
;
5376 // 00-17, 19, 1C-1F --> execute
5378 handleControlChar(ch
);
5381 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5382 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5384 scanState
= ScanState
.CSI_INTERMEDIATE
;
5387 // 30-39, 3B --> param
5388 if ((ch
>= '0') && (ch
<= '9')) {
5395 // 0x3A goes to CSI_IGNORE
5397 scanState
= ScanState
.CSI_IGNORE
;
5399 // 0x3C-3F goes to CSI_IGNORE
5400 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5401 scanState
= ScanState
.CSI_IGNORE
;
5404 // 40-7E --> dispatch, then switch to GROUND
5405 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5408 // ICH - Insert character
5416 // CUD - Cursor down
5420 // CUF - Cursor forward
5424 // CUB - Cursor backward
5428 // CNL - Cursor down and to column 1
5429 if (type
== DeviceType
.XTERM
) {
5434 // CPL - Cursor up and to column 1
5435 if (type
== DeviceType
.XTERM
) {
5440 // CHA - Cursor to column # in current row
5441 if (type
== DeviceType
.XTERM
) {
5446 // CUP - Cursor position
5450 // CHT - Cursor forward X tab stops (default 1)
5451 if (type
== DeviceType
.XTERM
) {
5456 // ED - Erase in display
5460 // EL - Erase in line
5475 // DCH - Delete character
5482 // Scroll up X lines (default 1)
5483 if (type
== DeviceType
.XTERM
) {
5488 // Scroll down X lines (default 1)
5489 if (type
== DeviceType
.XTERM
) {
5498 if ((type
== DeviceType
.VT220
)
5499 || (type
== DeviceType
.XTERM
)) {
5501 // ECH - Erase character
5508 // CBT - Cursor backward X tab stops (default 1)
5509 if (type
== DeviceType
.XTERM
) {
5520 // HPA - Cursor to column # in current row. Same as CHA
5521 if (type
== DeviceType
.XTERM
) {
5526 // HPR - Cursor right. Same as CUF
5527 if (type
== DeviceType
.XTERM
) {
5532 // REP - Repeat last char X times
5533 if (type
== DeviceType
.XTERM
) {
5538 // DA - Device attributes
5542 // VPA - Cursor to row, current column.
5543 if (type
== DeviceType
.XTERM
) {
5548 // VPR - Cursor down. Same as CUD
5549 if (type
== DeviceType
.XTERM
) {
5554 // HVP - Horizontal and vertical position
5558 // TBC - Tabulation clear
5562 // Sets an ANSI or DEC private toggle
5566 if ((type
== DeviceType
.VT220
)
5567 || (type
== DeviceType
.XTERM
)) {
5569 // Printer functions
5577 // Sets an ANSI or DEC private toggle
5581 // SGR - Select graphics rendition
5585 // DSR - Device status report
5592 // DECLL - Load leds
5596 // DECSTBM - Set top and bottom margins
5606 // DECREQTPARM - Request terminal parameters
5623 case CSI_INTERMEDIATE
:
5624 // 00-17, 19, 1C-1F --> execute
5626 handleControlChar(ch
);
5629 // 20-2F --> collect
5630 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5634 // 0x30-3F goes to CSI_IGNORE
5635 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5636 scanState
= ScanState
.CSI_IGNORE
;
5639 // 40-7E --> dispatch, then switch to GROUND
5640 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5692 if (((type
== DeviceType
.VT220
)
5693 || (type
== DeviceType
.XTERM
))
5694 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5696 // DECSCL - compatibility level
5699 if ((type
== DeviceType
.XTERM
)
5700 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
5702 // DECSTR - Soft terminal reset
5707 if (((type
== DeviceType
.VT220
)
5708 || (type
== DeviceType
.XTERM
))
5709 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5737 // 00-17, 19, 1C-1F --> execute
5739 handleControlChar(ch
);
5742 // 20-2F --> collect
5743 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5747 // 40-7E --> ignore, then switch to GROUND
5748 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5752 // 20-3F, 7F --> ignore
5758 // 0x9C goes to GROUND
5763 // 0x1B 0x5C goes to GROUND
5768 if ((collectBuffer
.length() > 0)
5769 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5775 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5776 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5778 scanState
= ScanState
.DCS_INTERMEDIATE
;
5781 // 30-39, 3B --> param, then switch to DCS_PARAM
5782 if ((ch
>= '0') && (ch
<= '9')) {
5784 scanState
= ScanState
.DCS_PARAM
;
5788 scanState
= ScanState
.DCS_PARAM
;
5791 // 3C-3F --> collect, then switch to DCS_PARAM
5792 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5794 scanState
= ScanState
.DCS_PARAM
;
5797 // 00-17, 19, 1C-1F, 7F --> ignore
5799 // 0x3A goes to DCS_IGNORE
5801 scanState
= ScanState
.DCS_IGNORE
;
5804 // 0x40-7E goes to DCS_PASSTHROUGH
5805 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5806 scanState
= ScanState
.DCS_PASSTHROUGH
;
5810 case DCS_INTERMEDIATE
:
5812 // 0x9C goes to GROUND
5817 // 0x1B 0x5C goes to GROUND
5822 if ((collectBuffer
.length() > 0)
5823 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5829 // 0x30-3F goes to DCS_IGNORE
5830 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5831 scanState
= ScanState
.DCS_IGNORE
;
5834 // 0x40-7E goes to DCS_PASSTHROUGH
5835 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5836 scanState
= ScanState
.DCS_PASSTHROUGH
;
5839 // 00-17, 19, 1C-1F, 7F --> ignore
5844 // 0x9C goes to GROUND
5849 // 0x1B 0x5C goes to GROUND
5854 if ((collectBuffer
.length() > 0)
5855 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5861 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5862 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5864 scanState
= ScanState
.DCS_INTERMEDIATE
;
5867 // 30-39, 3B --> param
5868 if ((ch
>= '0') && (ch
<= '9')) {
5875 // 00-17, 19, 1C-1F, 7F --> ignore
5877 // 0x3A, 3C-3F goes to DCS_IGNORE
5879 scanState
= ScanState
.DCS_IGNORE
;
5881 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5882 scanState
= ScanState
.DCS_IGNORE
;
5885 // 0x40-7E goes to DCS_PASSTHROUGH
5886 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5887 scanState
= ScanState
.DCS_PASSTHROUGH
;
5891 case DCS_PASSTHROUGH
:
5892 // 0x9C goes to GROUND
5897 // 0x1B 0x5C goes to GROUND
5902 if ((collectBuffer
.length() > 0)
5903 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5909 // 00-17, 19, 1C-1F, 20-7E --> put
5917 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5920 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5929 // 00-17, 19, 1C-1F, 20-7F --> ignore
5931 // 0x9C goes to GROUND
5938 case SOSPMAPC_STRING
:
5939 // 00-17, 19, 1C-1F, 20-7F --> ignore
5941 // 0x9C goes to GROUND
5949 // Special case for Xterm: OSC can pass control characters
5950 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5954 // 00-17, 19, 1C-1F --> ignore
5956 // 20-7F --> osc_put
5957 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5961 // 0x9C goes to GROUND
5968 case VT52_DIRECT_CURSOR_ADDRESS
:
5969 // This is a special case for the VT52 sequence "ESC Y l c"
5970 if (collectBuffer
.length() == 0) {
5972 } else if (collectBuffer
.length() == 1) {
5973 // We've got the two characters, one in the buffer and the
5975 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
5984 * Expose current cursor X to outside world.
5986 * @return current cursor X
5988 public final int getCursorX() {
5989 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
5990 return currentState
.cursorX
* 2;
5992 return currentState
.cursorX
;
5996 * Expose current cursor Y to outside world.
5998 * @return current cursor Y
6000 public final int getCursorY() {
6001 return currentState
.cursorY
;
6005 * Read function runs on a separate thread.
6007 public final void run() {
6008 boolean utf8
= false;
6009 boolean done
= false;
6011 if (type
== DeviceType
.XTERM
) {
6015 // available() will often return > 1, so we need to read in chunks to
6017 char [] readBufferUTF8
= null;
6018 byte [] readBuffer
= null;
6020 readBufferUTF8
= new char[128];
6022 readBuffer
= new byte[128];
6025 while (!done
&& !stopReaderThread
) {
6027 int n
= inputStream
.available();
6028 // System.err.printf("available() %d\n", n); System.err.flush();
6030 if (readBufferUTF8
.length
< n
) {
6031 // The buffer wasn't big enough, make it huger
6032 int newSizeHalf
= Math
.max(readBufferUTF8
.length
, n
);
6034 readBufferUTF8
= new char[newSizeHalf
* 2];
6037 if (readBuffer
.length
< n
) {
6038 // The buffer wasn't big enough, make it huger
6039 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
6040 readBuffer
= new byte[newSizeHalf
* 2];
6046 rc
= input
.read(readBufferUTF8
, 0,
6047 readBufferUTF8
.length
);
6049 rc
= inputStream
.read(readBuffer
, 0,
6052 // System.err.printf("read() %d\n", rc); System.err.flush();
6057 for (int i
= 0; i
< rc
; i
++) {
6060 ch
= readBufferUTF8
[i
];
6064 // Don't step on UI events
6065 synchronized (this) {
6070 // System.err.println("end while loop"); System.err.flush();
6071 } catch (IOException e
) {
6072 e
.printStackTrace();
6075 } // while ((done == false) && (stopReaderThread == false))
6077 // Let the rest of the world know that I am done.
6078 stopReaderThread
= true;
6080 // System.err.println("*** run() exiting..."); System.err.flush();