2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 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
.BufferedOutputStream
;
32 import java
.io
.InputStream
;
33 import java
.io
.InputStreamReader
;
34 import java
.io
.IOException
;
35 import java
.io
.OutputStream
;
36 import java
.io
.OutputStreamWriter
;
37 import java
.io
.Reader
;
38 import java
.io
.UnsupportedEncodingException
;
39 import java
.io
.Writer
;
40 import java
.util
.ArrayList
;
41 import java
.util
.Collections
;
42 import java
.util
.LinkedList
;
43 import java
.util
.List
;
45 import jexer
.TKeypress
;
46 import jexer
.event
.TMouseEvent
;
47 import jexer
.bits
.Color
;
48 import jexer
.bits
.Cell
;
49 import jexer
.bits
.CellAttributes
;
50 import jexer
.io
.ReadTimeoutException
;
51 import jexer
.io
.TimeoutInputStream
;
52 import static jexer
.TKeypress
.*;
55 * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
56 * including a scrollback buffer.
59 * It currently implements VT100, VT102, VT220, and XTERM with the following
63 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
64 * failure with XTERM. This is due to vttest failing to decode the UTF-8
68 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
69 * from VT100 are not supported.
72 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
73 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
74 * because DECSCL is not supported, it will fail the last part of the
75 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
78 * - Numeric/application keys from the number pad are not supported because
79 * they are not exposed from the TKeypress API.
82 * - VT52 HOLD SCREEN mode is not supported.
85 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
86 * numerators) are not rendered correctly.
89 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
91 public class ECMA48
implements Runnable
{
94 * The emulator can emulate several kinds of terminals.
96 public enum DeviceType
{
98 * DEC VT100 but also including the three VT102 functions.
119 * Return the proper primary Device Attributes string.
121 * @return string to send to remote side that is appropriate for the
124 private String
deviceTypeResponse() {
127 // "I am a VT100 with advanced video option" (often VT102)
136 // "I am a VT220" - 7 bit version
138 return "\033[?62;1;6c";
140 // "I am a VT220" - 8 bit version
141 return "\u009b?62;1;6c";
143 throw new IllegalArgumentException("Invalid device type: " + type
);
148 * Return the proper TERM environment variable for this device type.
150 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
151 * @return "vt100", "xterm", etc.
153 public static String
deviceTypeTerm(final DeviceType deviceType
) {
154 switch (deviceType
) {
168 throw new IllegalArgumentException("Invalid device type: "
174 * Return the proper LANG for this device type. Only XTERM devices know
175 * about UTF-8, the others are defined by their standard to be either
176 * 7-bit or 8-bit characters only.
178 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
179 * @param baseLang a base language without UTF-8 flag such as "C" or
181 * @return "en_US", "en_US.UTF-8", etc.
183 public static String
deviceTypeLang(final DeviceType deviceType
,
184 final String baseLang
) {
186 switch (deviceType
) {
194 return baseLang
+ ".UTF-8";
197 throw new IllegalArgumentException("Invalid device type: "
203 * Write a string directly to the remote side.
205 * @param str string to send
207 private void writeRemote(final String str
) {
208 if (stopReaderThread
) {
209 // Reader hit EOF, bail out now.
214 // System.err.printf("writeRemote() '%s'\n", str);
220 if (outputStream
== null) {
224 for (int i
= 0; i
< str
.length(); i
++) {
225 outputStream
.write(str
.charAt(i
));
227 outputStream
.flush();
228 } catch (IOException e
) {
234 if (output
== null) {
240 } catch (IOException e
) {
246 throw new IllegalArgumentException("Invalid device type: " + type
);
251 * Close the input and output streams and stop the reader thread. Note
252 * that it is safe to call this multiple times.
254 public final void close() {
256 // Tell the reader thread to stop looking at input. It will close
257 // the input streams.
258 if (stopReaderThread
== false) {
259 stopReaderThread
= true;
261 readerThread
.join(1000);
262 } catch (InterruptedException e
) {
267 // Now close the output stream.
272 if (outputStream
!= null) {
274 outputStream
.close();
275 } catch (IOException e
) {
282 if (outputStream
!= null) {
284 outputStream
.close();
285 } catch (IOException e
) {
290 if (output
!= null) {
293 } catch (IOException e
) {
300 throw new IllegalArgumentException("Invalid device type: " +
306 * When true, the reader thread is expected to exit.
308 private volatile boolean stopReaderThread
= false;
313 private Thread readerThread
= null;
316 * See if the reader thread is still running.
318 * @return if true, we are still connected to / reading from the remote
321 public final boolean isReading() {
322 return (!stopReaderThread
);
326 * The type of emulator to be.
328 private DeviceType type
= DeviceType
.VT102
;
331 * Obtain a new blank display line for an external user
332 * (e.g. TTerminalWindow).
334 * @return new blank line
336 public final DisplayLine
getBlankDisplayLine() {
337 return new DisplayLine(currentState
.attr
);
341 * The scrollback buffer characters + attributes.
343 private volatile List
<DisplayLine
> scrollback
;
346 * Get the scrollback buffer.
348 * @return the scrollback buffer
350 public final List
<DisplayLine
> getScrollbackBuffer() {
355 * The raw display buffer characters + attributes.
357 private volatile List
<DisplayLine
> display
;
360 * Get the display buffer.
362 * @return the display buffer
364 public final List
<DisplayLine
> getDisplayBuffer() {
369 * The terminal's input. For type == XTERM, this is an InputStreamReader
370 * with UTF-8 encoding.
372 private Reader input
;
375 * The terminal's raw InputStream. This is used for type != XTERM.
377 private volatile TimeoutInputStream inputStream
;
380 * The terminal's output. For type == XTERM, this wraps an
381 * OutputStreamWriter with UTF-8 encoding.
383 private Writer output
;
386 * The terminal's raw OutputStream. This is used for type != XTERM.
388 private OutputStream outputStream
;
391 * Parser character scan states.
408 VT52_DIRECT_CURSOR_ADDRESS
412 * Current scanning state.
414 private ScanState scanState
;
417 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
418 * can't really use it in keypress() because we do not see number pad
419 * events from TKeypress.
421 private enum KeypadMode
{
427 * Arrow keys can emit three different sequences (DECCKM or VT52
430 private enum ArrowKeyMode
{
437 * Available character sets for GL, GR, G0, G1, G2, G3.
439 private enum CharacterSet
{
460 * Single-shift states used by the C1 control characters SS2 (0x8E) and
463 private enum Singleshift
{
470 * VT220+ lockshift states.
472 private enum LockshiftMode
{
482 * XTERM mouse reporting protocols.
484 private enum MouseProtocol
{
493 * Which mouse protocol is active.
495 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
498 * XTERM mouse reporting encodings.
500 private enum MouseEncoding
{
507 * Which mouse encoding is active.
509 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
512 * Physical display width. We start at 80x24, but the user can resize us
518 * Get the display width.
520 * @return the width (usually 80 or 132)
522 public final int getWidth() {
527 * Physical display height. We start at 80x24, but the user can resize
533 * Get the display height.
535 * @return the height (usually 24)
537 public final int getHeight() {
542 * Top margin of the scrolling region.
544 private int scrollRegionTop
;
547 * Bottom margin of the scrolling region.
549 private int scrollRegionBottom
;
552 * Right margin column number. This can be selected by the remote side
553 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
555 private int rightMargin
;
558 * Last character printed.
563 * VT100-style line wrapping: a character is placed in column 80 (or
564 * 132), but the line does NOT wrap until another character is written to
565 * column 1 of the next line, after which the cursor moves to column 2.
567 private boolean wrapLineFlag
;
570 * VT220 single shift flag.
572 private Singleshift singleshift
= Singleshift
.NONE
;
575 * true = insert characters, false = overwrite.
577 private boolean insertMode
= false;
580 * VT52 mode as selected by DECANM. True means VT52, false means
581 * ANSI. Default is ANSI.
583 private boolean vt52Mode
= false;
586 * Visible cursor (DECTCEM).
588 private boolean cursorVisible
= true;
591 * Get visible cursor flag.
593 * @return if true, the cursor is visible
595 public final boolean isCursorVisible() {
596 return cursorVisible
;
600 * Screen title as set by the xterm OSC sequence. Lots of applications
601 * send a screenTitle regardless of whether it is an xterm client or not.
603 private String screenTitle
= "";
606 * Get the screen title as set by the xterm OSC sequence. Lots of
607 * applications send a screenTitle regardless of whether it is an xterm
610 * @return screen title
612 public final String
getScreenTitle() {
617 * Parameter characters being collected.
619 private List
<Integer
> csiParams
;
622 * Non-csi collect buffer.
624 private StringBuilder collectBuffer
;
627 * When true, use the G1 character set.
629 private boolean shiftOut
= false;
632 * Horizontal tab stop locations.
634 private List
<Integer
> tabStops
;
637 * S8C1T. True means 8bit controls, false means 7bit controls.
639 private boolean s8c1t
= false;
642 * Printer mode. True means send all output to printer, which discards
645 private boolean printerControllerMode
= false;
648 * LMN line mode. If true, linefeed() puts the cursor on the first
649 * column of the next line. If false, linefeed() puts the cursor one
650 * line down on the current line. The default is false.
652 private boolean newLineMode
= false;
655 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
657 private ArrowKeyMode arrowKeyMode
;
660 * Whether number pad keys send VT100 or VT52, application or numeric
663 @SuppressWarnings("unused")
664 private KeypadMode keypadMode
;
667 * When true, the terminal is in 132-column mode (DECCOLM).
669 private boolean columns132
= false;
672 * Get 132 columns value.
674 * @return if true, the terminal is in 132 column mode
676 public final boolean isColumns132() {
681 * true = reverse video. Set by DECSCNM.
683 private boolean reverseVideo
= false;
686 * false = echo characters locally.
688 private boolean fullDuplex
= true;
691 * DECSC/DECRC save/restore a subset of the total state. This class
692 * encapsulates those specific flags/modes.
694 private class SaveableState
{
697 * When true, cursor positions are relative to the scrolling region.
699 public boolean originMode
= false;
702 * The current editing X position.
704 public int cursorX
= 0;
707 * The current editing Y position.
709 public int cursorY
= 0;
712 * Which character set is currently selected in G0.
714 public CharacterSet g0Charset
= CharacterSet
.US
;
717 * Which character set is currently selected in G1.
719 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
722 * Which character set is currently selected in G2.
724 public CharacterSet g2Charset
= CharacterSet
.US
;
727 * Which character set is currently selected in G3.
729 public CharacterSet g3Charset
= CharacterSet
.US
;
732 * Which character set is currently selected in GR.
734 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
737 * The current drawing attributes.
739 public CellAttributes attr
;
744 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
749 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
754 public boolean lineWrap
= true;
759 public void reset() {
763 g0Charset
= CharacterSet
.US
;
764 g1Charset
= CharacterSet
.DRAWING
;
765 g2Charset
= CharacterSet
.US
;
766 g3Charset
= CharacterSet
.US
;
767 grCharset
= CharacterSet
.DRAWING
;
768 attr
= new CellAttributes();
769 glLockshift
= LockshiftMode
.NONE
;
770 grLockshift
= LockshiftMode
.NONE
;
775 * Copy attributes from another instance.
777 * @param that the other instance to match
779 public void setTo(final SaveableState that
) {
780 this.originMode
= that
.originMode
;
781 this.cursorX
= that
.cursorX
;
782 this.cursorY
= that
.cursorY
;
783 this.g0Charset
= that
.g0Charset
;
784 this.g1Charset
= that
.g1Charset
;
785 this.g2Charset
= that
.g2Charset
;
786 this.g3Charset
= that
.g3Charset
;
787 this.grCharset
= that
.grCharset
;
788 this.attr
= new CellAttributes();
789 this.attr
.setTo(that
.attr
);
790 this.glLockshift
= that
.glLockshift
;
791 this.grLockshift
= that
.grLockshift
;
792 this.lineWrap
= that
.lineWrap
;
796 * Public constructor.
798 public SaveableState() {
804 * The current terminal state.
806 private SaveableState currentState
;
809 * The last saved terminal state.
811 private SaveableState savedState
;
814 * Clear the CSI parameters and flags.
816 private void toGround() {
818 collectBuffer
= new StringBuilder(8);
819 scanState
= ScanState
.GROUND
;
823 * Reset the tab stops list.
825 private void resetTabStops() {
827 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
828 tabStops
.add(new Integer(i
* 8));
833 * Reset the emulation state.
835 private void reset() {
837 currentState
= new SaveableState();
838 savedState
= new SaveableState();
839 scanState
= ScanState
.GROUND
;
843 scrollRegionBottom
= height
- 1;
844 rightMargin
= width
- 1;
846 arrowKeyMode
= ArrowKeyMode
.ANSI
;
847 keypadMode
= KeypadMode
.Numeric
;
848 wrapLineFlag
= false;
856 reverseVideo
= false;
858 cursorVisible
= true;
861 singleshift
= Singleshift
.NONE
;
863 printerControllerMode
= false;
866 mouseProtocol
= MouseProtocol
.OFF
;
867 mouseEncoding
= MouseEncoding
.X10
;
877 * Public constructor.
879 * @param type one of the DeviceType constants to select VT100, VT102,
881 * @param inputStream an InputStream connected to the remote side. For
882 * type == XTERM, inputStream is converted to a Reader with UTF-8
884 * @param outputStream an OutputStream connected to the remote user. For
885 * type == XTERM, outputStream is converted to a Writer with UTF-8
887 * @throws UnsupportedEncodingException if an exception is thrown when
888 * creating the InputStreamReader
890 public ECMA48(final DeviceType type
, final InputStream inputStream
,
891 final OutputStream outputStream
) throws UnsupportedEncodingException
{
893 assert (inputStream
!= null);
894 assert (outputStream
!= null);
896 csiParams
= new ArrayList
<Integer
>();
897 tabStops
= new ArrayList
<Integer
>();
898 scrollback
= new LinkedList
<DisplayLine
>();
899 display
= new LinkedList
<DisplayLine
>();
902 if (inputStream
instanceof TimeoutInputStream
) {
903 this.inputStream
= (TimeoutInputStream
)inputStream
;
905 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
907 if (type
== DeviceType
.XTERM
) {
908 this.input
= new InputStreamReader(this.inputStream
, "UTF-8");
909 this.output
= new OutputStreamWriter(new
910 BufferedOutputStream(outputStream
), "UTF-8");
911 this.outputStream
= null;
914 this.outputStream
= new BufferedOutputStream(outputStream
);
918 for (int i
= 0; i
< height
; i
++) {
919 display
.add(new DisplayLine(currentState
.attr
));
922 // Spin up the input reader
923 readerThread
= new Thread(this);
924 readerThread
.start();
928 * Append a new line to the bottom of the display, adding lines off the
929 * top to the scrollback buffer.
931 private void newDisplayLine() {
932 // Scroll the top line off into the scrollback buffer
933 scrollback
.add(display
.get(0));
935 DisplayLine line
= new DisplayLine(currentState
.attr
);
936 line
.setReverseColor(reverseVideo
);
941 * Wraps the current line.
943 private void wrapCurrentLine() {
944 if (currentState
.cursorY
== height
- 1) {
947 if (currentState
.cursorY
< height
- 1) {
948 currentState
.cursorY
++;
950 currentState
.cursorX
= 0;
954 * Handle a carriage return.
956 private void carriageReturn() {
957 currentState
.cursorX
= 0;
958 wrapLineFlag
= false;
962 * Reverse the color of the visible display.
964 private void invertDisplayColors() {
965 for (DisplayLine line
: display
) {
966 line
.setReverseColor(!line
.isReverseColor());
973 private void linefeed() {
975 if (currentState
.cursorY
< scrollRegionBottom
) {
976 // Increment screen y
977 currentState
.cursorY
++;
980 // Screen y does not increment
983 * Two cases: either we're inside a scrolling region or not. If
984 * the scrolling region bottom is the bottom of the screen, then
985 * push the top line into the buffer. Else scroll the scrolling
988 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
990 // We're at the bottom of the scroll region, AND the scroll
991 // region is the entire screen.
997 // We're at the bottom of the scroll region, AND the scroll
998 // region is NOT the entire screen.
999 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1004 currentState
.cursorX
= 0;
1006 wrapLineFlag
= false;
1010 * Prints one character to the display buffer.
1012 * @param ch character to display
1014 private void printCharacter(final char ch
) {
1015 int rightMargin
= this.rightMargin
;
1017 // Check if we have double-width, and if so chop at 40/66 instead of
1019 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1020 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1023 // Check the unusually-complicated line wrapping conditions...
1024 if (currentState
.cursorX
== rightMargin
) {
1026 if (currentState
.lineWrap
== true) {
1028 * This case happens when: the cursor was already on the
1029 * right margin (either through printing or by an explicit
1030 * placement command), and a character was printed.
1032 * The line wraps only when a new character arrives AND the
1033 * cursor is already on the right margin AND has placed a
1034 * character in its cell. Easier to see than to explain.
1036 if (wrapLineFlag
== false) {
1038 * This block marks the case that we are in the margin
1039 * and the first character has been received and printed.
1041 wrapLineFlag
= true;
1044 * This block marks the case that we are in the margin
1045 * and the second character has been received and
1048 wrapLineFlag
= false;
1052 } else if (currentState
.cursorX
<= rightMargin
) {
1054 * This is the normal case: a character came in and was printed
1055 * to the left of the right margin column.
1058 // Turn off VT100 special-case flag
1059 wrapLineFlag
= false;
1062 // "Print" the character
1063 Cell newCell
= new Cell(ch
);
1064 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1065 newCellAttributes
.setTo(currentState
.attr
);
1066 DisplayLine line
= display
.get(currentState
.cursorY
);
1067 // Insert mode special case
1068 if (insertMode
== true) {
1069 line
.insert(currentState
.cursorX
, newCell
);
1071 // Replace an existing character
1072 line
.replace(currentState
.cursorX
, newCell
);
1075 // Increment horizontal
1076 if (wrapLineFlag
== false) {
1077 currentState
.cursorX
++;
1078 if (currentState
.cursorX
> rightMargin
) {
1079 currentState
.cursorX
--;
1085 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1086 * send to the remote side.
1088 * @param mouse mouse event received from the local user
1090 public void mouse(final TMouseEvent mouse
) {
1093 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1094 mouseProtocol, mouseEncoding, mouse);
1097 if (mouseEncoding
== MouseEncoding
.X10
) {
1098 // We will support X10 but only for (160,94) and smaller.
1099 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1104 switch (mouseProtocol
) {
1111 // Only report button presses
1112 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1118 // Only report button presses and releases
1119 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1120 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1128 * Only report button presses, button releases, and motions that
1129 * have a button down (i.e. drag-and-drop).
1131 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1132 if (!mouse
.isMouse1()
1133 && !mouse
.isMouse2()
1134 && !mouse
.isMouse3()
1135 && !mouse
.isMouseWheelUp()
1136 && !mouse
.isMouseWheelDown()
1144 // Report everything
1148 // Now encode the event
1149 StringBuilder sb
= new StringBuilder(6);
1150 if (mouseEncoding
== MouseEncoding
.SGR
) {
1151 sb
.append((char) 0x1B);
1154 if (mouse
.isMouse1()) {
1155 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1160 } else if (mouse
.isMouse2()) {
1161 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1166 } else if (mouse
.isMouse3()) {
1167 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1172 } else if (mouse
.isMouseWheelUp()) {
1174 } else if (mouse
.isMouseWheelDown()) {
1177 // This is motion with no buttons down.
1181 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1184 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1191 // X10 and UTF8 encodings
1192 sb
.append((char) 0x1B);
1195 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1196 sb
.append((char) (0x03 + 32));
1197 } else if (mouse
.isMouse1()) {
1198 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1199 sb
.append((char) (0x00 + 32 + 32));
1201 sb
.append((char) (0x00 + 32));
1203 } else if (mouse
.isMouse2()) {
1204 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1205 sb
.append((char) (0x01 + 32 + 32));
1207 sb
.append((char) (0x01 + 32));
1209 } else if (mouse
.isMouse3()) {
1210 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1211 sb
.append((char) (0x02 + 32 + 32));
1213 sb
.append((char) (0x02 + 32));
1215 } else if (mouse
.isMouseWheelUp()) {
1216 sb
.append((char) (0x04 + 64));
1217 } else if (mouse
.isMouseWheelDown()) {
1218 sb
.append((char) (0x05 + 64));
1220 // This is motion with no buttons down.
1221 sb
.append((char) (0x03 + 32));
1224 sb
.append((char) (mouse
.getX() + 33));
1225 sb
.append((char) (mouse
.getY() + 33));
1228 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1229 writeRemote(sb
.toString());
1233 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1234 * send to the remote side.
1236 * @param keypress keypress received from the local user
1238 public void keypress(final TKeypress keypress
) {
1239 writeRemote(keypressToString(keypress
));
1243 * Build one of the complex xterm keystroke sequences, storing the result in
1244 * xterm_keystroke_buffer.
1246 * @param ss3 the prefix to use based on VT100 state.
1247 * @param first the first character, usually a number.
1248 * @param first the last character, one of the following: ~ A B C D F H
1249 * @param ctrl whether or not ctrl is down
1250 * @param alt whether or not alt is down
1251 * @param shift whether or not shift is down
1252 * @return the buffer with the full key sequence
1254 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1255 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1257 StringBuilder sb
= new StringBuilder(ss3
);
1258 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1262 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1264 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1266 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1268 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1270 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1272 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1274 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1279 return sb
.toString();
1283 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1285 * @param keypress keypress received from the local user
1286 * @return string to transmit to the remote side
1288 private String
keypressToString(final TKeypress keypress
) {
1290 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1292 * If this is a control character, process it like it came from
1295 if (keypress
.getChar() < 0x20) {
1296 handleControlChar(keypress
.getChar());
1298 // Local echo for everything else
1299 printCharacter(keypress
.getChar());
1303 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1308 // Handle control characters
1309 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1310 StringBuilder sb
= new StringBuilder();
1311 char ch
= keypress
.getChar();
1314 return sb
.toString();
1317 // Handle alt characters
1318 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1319 StringBuilder sb
= new StringBuilder("\033");
1320 char ch
= keypress
.getChar();
1322 return sb
.toString();
1325 if (keypress
.equals(kbBackspace
)) {
1338 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1341 switch (arrowKeyMode
) {
1343 return xtermBuildKeySequence("\033[", '1', 'D',
1344 keypress
.isCtrl(), keypress
.isAlt(),
1345 keypress
.isShift());
1347 return xtermBuildKeySequence("\033", '1', 'D',
1348 keypress
.isCtrl(), keypress
.isAlt(),
1349 keypress
.isShift());
1351 return xtermBuildKeySequence("\033O", '1', 'D',
1352 keypress
.isCtrl(), keypress
.isAlt(),
1353 keypress
.isShift());
1356 switch (arrowKeyMode
) {
1367 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1370 switch (arrowKeyMode
) {
1372 return xtermBuildKeySequence("\033[", '1', 'C',
1373 keypress
.isCtrl(), keypress
.isAlt(),
1374 keypress
.isShift());
1376 return xtermBuildKeySequence("\033", '1', 'C',
1377 keypress
.isCtrl(), keypress
.isAlt(),
1378 keypress
.isShift());
1380 return xtermBuildKeySequence("\033O", '1', 'C',
1381 keypress
.isCtrl(), keypress
.isAlt(),
1382 keypress
.isShift());
1385 switch (arrowKeyMode
) {
1396 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1399 switch (arrowKeyMode
) {
1401 return xtermBuildKeySequence("\033[", '1', 'A',
1402 keypress
.isCtrl(), keypress
.isAlt(),
1403 keypress
.isShift());
1405 return xtermBuildKeySequence("\033", '1', 'A',
1406 keypress
.isCtrl(), keypress
.isAlt(),
1407 keypress
.isShift());
1409 return xtermBuildKeySequence("\033O", '1', 'A',
1410 keypress
.isCtrl(), keypress
.isAlt(),
1411 keypress
.isShift());
1414 switch (arrowKeyMode
) {
1425 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1428 switch (arrowKeyMode
) {
1430 return xtermBuildKeySequence("\033[", '1', 'B',
1431 keypress
.isCtrl(), keypress
.isAlt(),
1432 keypress
.isShift());
1434 return xtermBuildKeySequence("\033", '1', 'B',
1435 keypress
.isCtrl(), keypress
.isAlt(),
1436 keypress
.isShift());
1438 return xtermBuildKeySequence("\033O", '1', 'B',
1439 keypress
.isCtrl(), keypress
.isAlt(),
1440 keypress
.isShift());
1443 switch (arrowKeyMode
) {
1454 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1457 switch (arrowKeyMode
) {
1459 return xtermBuildKeySequence("\033[", '1', 'H',
1460 keypress
.isCtrl(), keypress
.isAlt(),
1461 keypress
.isShift());
1463 return xtermBuildKeySequence("\033", '1', 'H',
1464 keypress
.isCtrl(), keypress
.isAlt(),
1465 keypress
.isShift());
1467 return xtermBuildKeySequence("\033O", '1', 'H',
1468 keypress
.isCtrl(), keypress
.isAlt(),
1469 keypress
.isShift());
1472 switch (arrowKeyMode
) {
1483 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1486 switch (arrowKeyMode
) {
1488 return xtermBuildKeySequence("\033[", '1', 'F',
1489 keypress
.isCtrl(), keypress
.isAlt(),
1490 keypress
.isShift());
1492 return xtermBuildKeySequence("\033", '1', 'F',
1493 keypress
.isCtrl(), keypress
.isAlt(),
1494 keypress
.isShift());
1496 return xtermBuildKeySequence("\033O", '1', 'F',
1497 keypress
.isCtrl(), keypress
.isAlt(),
1498 keypress
.isShift());
1501 switch (arrowKeyMode
) {
1512 if (keypress
.equals(kbF1
)) {
1520 if (keypress
.equals(kbF2
)) {
1528 if (keypress
.equals(kbF3
)) {
1536 if (keypress
.equals(kbF4
)) {
1544 if (keypress
.equals(kbF5
)) {
1557 if (keypress
.equals(kbF6
)) {
1570 if (keypress
.equals(kbF7
)) {
1583 if (keypress
.equals(kbF8
)) {
1596 if (keypress
.equals(kbF9
)) {
1609 if (keypress
.equals(kbF10
)) {
1622 if (keypress
.equals(kbF11
)) {
1626 if (keypress
.equals(kbF12
)) {
1630 if (keypress
.equals(kbShiftF1
)) {
1635 if (type
== DeviceType
.XTERM
) {
1641 if (keypress
.equals(kbShiftF2
)) {
1646 if (type
== DeviceType
.XTERM
) {
1652 if (keypress
.equals(kbShiftF3
)) {
1657 if (type
== DeviceType
.XTERM
) {
1663 if (keypress
.equals(kbShiftF4
)) {
1668 if (type
== DeviceType
.XTERM
) {
1674 if (keypress
.equals(kbShiftF5
)) {
1676 return "\033[15;2~";
1679 if (keypress
.equals(kbShiftF6
)) {
1681 return "\033[17;2~";
1684 if (keypress
.equals(kbShiftF7
)) {
1686 return "\033[18;2~";
1689 if (keypress
.equals(kbShiftF8
)) {
1691 return "\033[19;2~";
1694 if (keypress
.equals(kbShiftF9
)) {
1696 return "\033[20;2~";
1699 if (keypress
.equals(kbShiftF10
)) {
1701 return "\033[21;2~";
1704 if (keypress
.equals(kbShiftF11
)) {
1706 return "\033[23;2~";
1709 if (keypress
.equals(kbShiftF12
)) {
1711 return "\033[24;2~";
1714 if (keypress
.equals(kbCtrlF1
)) {
1719 if (type
== DeviceType
.XTERM
) {
1725 if (keypress
.equals(kbCtrlF2
)) {
1730 if (type
== DeviceType
.XTERM
) {
1736 if (keypress
.equals(kbCtrlF3
)) {
1741 if (type
== DeviceType
.XTERM
) {
1747 if (keypress
.equals(kbCtrlF4
)) {
1752 if (type
== DeviceType
.XTERM
) {
1758 if (keypress
.equals(kbCtrlF5
)) {
1760 return "\033[15;5~";
1763 if (keypress
.equals(kbCtrlF6
)) {
1765 return "\033[17;5~";
1768 if (keypress
.equals(kbCtrlF7
)) {
1770 return "\033[18;5~";
1773 if (keypress
.equals(kbCtrlF8
)) {
1775 return "\033[19;5~";
1778 if (keypress
.equals(kbCtrlF9
)) {
1780 return "\033[20;5~";
1783 if (keypress
.equals(kbCtrlF10
)) {
1785 return "\033[21;5~";
1788 if (keypress
.equals(kbCtrlF11
)) {
1790 return "\033[23;5~";
1793 if (keypress
.equals(kbCtrlF12
)) {
1795 return "\033[24;5~";
1798 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
1801 return xtermBuildKeySequence("\033[", '5', '~',
1802 keypress
.isCtrl(), keypress
.isAlt(),
1803 keypress
.isShift());
1809 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
1812 return xtermBuildKeySequence("\033[", '6', '~',
1813 keypress
.isCtrl(), keypress
.isAlt(),
1814 keypress
.isShift());
1820 if (keypress
.equalsWithoutModifiers(kbIns
)) {
1823 return xtermBuildKeySequence("\033[", '2', '~',
1824 keypress
.isCtrl(), keypress
.isAlt(),
1825 keypress
.isShift());
1831 if (keypress
.equalsWithoutModifiers(kbDel
)) {
1834 return xtermBuildKeySequence("\033[", '3', '~',
1835 keypress
.isCtrl(), keypress
.isAlt(),
1836 keypress
.isShift());
1838 // Delete sends real delete for VTxxx
1843 if (keypress
.equals(kbEnter
)) {
1847 if (keypress
.equals(kbEsc
)) {
1851 if (keypress
.equals(kbAltEsc
)) {
1855 if (keypress
.equals(kbTab
)) {
1859 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
1860 (keypress
.equals(kbShiftTab
))
1870 // Non-alt, non-ctrl characters
1871 if (!keypress
.isFnKey()) {
1872 StringBuilder sb
= new StringBuilder();
1873 sb
.append(keypress
.getChar());
1874 return sb
.toString();
1880 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1883 * @param ch 8-bit character from the remote side
1884 * @param charsetGl character set defined for GL
1885 * @param charsetGr character set defined for GR
1886 * @return character to display on the screen
1888 private char mapCharacterCharset(final char ch
,
1889 final CharacterSet charsetGl
,
1890 final CharacterSet charsetGr
) {
1892 int lookupChar
= ch
;
1893 CharacterSet lookupCharset
= charsetGl
;
1896 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
1897 lookupCharset
= charsetGr
;
1901 switch (lookupCharset
) {
1904 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
1907 return DECCharacterSets
.UK
[lookupChar
];
1910 return DECCharacterSets
.US_ASCII
[lookupChar
];
1913 return DECCharacterSets
.NL
[lookupChar
];
1916 return DECCharacterSets
.FI
[lookupChar
];
1919 return DECCharacterSets
.FR
[lookupChar
];
1922 return DECCharacterSets
.FR_CA
[lookupChar
];
1925 return DECCharacterSets
.DE
[lookupChar
];
1928 return DECCharacterSets
.IT
[lookupChar
];
1931 return DECCharacterSets
.NO
[lookupChar
];
1934 return DECCharacterSets
.ES
[lookupChar
];
1937 return DECCharacterSets
.SV
[lookupChar
];
1940 return DECCharacterSets
.SWISS
[lookupChar
];
1942 case DEC_SUPPLEMENTAL
:
1943 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
1946 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
1949 return DECCharacterSets
.US_ASCII
[lookupChar
];
1952 return DECCharacterSets
.US_ASCII
[lookupChar
];
1955 throw new IllegalArgumentException("Invalid character set value: "
1961 * Map an 8-bit byte into a printable character.
1963 * @param ch either 8-bit or Unicode character from the remote side
1964 * @return character to display on the screen
1966 private char mapCharacter(final char ch
) {
1968 // Unicode character, just return it
1972 CharacterSet charsetGl
= currentState
.g0Charset
;
1973 CharacterSet charsetGr
= currentState
.grCharset
;
1975 if (vt52Mode
== true) {
1976 if (shiftOut
== true) {
1977 // Shifted out character, pull from VT52 graphics
1978 charsetGl
= currentState
.g1Charset
;
1979 charsetGr
= CharacterSet
.US
;
1982 charsetGl
= currentState
.g0Charset
;
1983 charsetGr
= CharacterSet
.US
;
1986 // Pull the character
1987 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1991 if (shiftOut
== true) {
1992 // Shifted out character, pull from G1
1993 charsetGl
= currentState
.g1Charset
;
1994 charsetGr
= currentState
.grCharset
;
1996 // Pull the character
1997 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2001 if (singleshift
== Singleshift
.SS2
) {
2003 singleshift
= Singleshift
.NONE
;
2005 // Shifted out character, pull from G2
2006 charsetGl
= currentState
.g2Charset
;
2007 charsetGr
= currentState
.grCharset
;
2011 if (singleshift
== Singleshift
.SS3
) {
2013 singleshift
= Singleshift
.NONE
;
2015 // Shifted out character, pull from G3
2016 charsetGl
= currentState
.g3Charset
;
2017 charsetGr
= currentState
.grCharset
;
2020 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2021 // Check for locking shift
2023 switch (currentState
.glLockshift
) {
2036 charsetGl
= currentState
.g2Charset
;
2041 charsetGl
= currentState
.g3Charset
;
2046 charsetGl
= currentState
.g0Charset
;
2050 switch (currentState
.grLockshift
) {
2060 charsetGr
= currentState
.g1Charset
;
2065 charsetGr
= currentState
.g2Charset
;
2070 charsetGr
= currentState
.g3Charset
;
2075 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2082 // Pull the character
2083 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2087 * Scroll the text within a scrolling region up n lines.
2089 * @param regionTop top row of the scrolling region
2090 * @param regionBottom bottom row of the scrolling region
2091 * @param n number of lines to scroll
2093 private void scrollingRegionScrollUp(final int regionTop
,
2094 final int regionBottom
, final int n
) {
2096 if (regionTop
>= regionBottom
) {
2100 // Sanity check: see if there will be any characters left after the
2102 if (regionBottom
+ 1 - regionTop
<= n
) {
2103 // There won't be anything left in the region, so just call
2104 // eraseScreen() and return.
2105 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2109 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2110 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2111 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2113 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2114 - remaining
, regionBottom
+ 1);
2115 display
= new LinkedList
<DisplayLine
>(displayTop
);
2116 display
.addAll(displayMiddle
);
2117 for (int i
= 0; i
< n
; i
++) {
2118 DisplayLine line
= new DisplayLine(currentState
.attr
);
2119 line
.setReverseColor(reverseVideo
);
2122 display
.addAll(displayBottom
);
2124 assert (display
.size() == height
);
2128 * Scroll the text within a scrolling region down n lines.
2130 * @param regionTop top row of the scrolling region
2131 * @param regionBottom bottom row of the scrolling region
2132 * @param n number of lines to scroll
2134 private void scrollingRegionScrollDown(final int regionTop
,
2135 final int regionBottom
, final int n
) {
2137 if (regionTop
>= regionBottom
) {
2141 // Sanity check: see if there will be any characters left after the
2143 if (regionBottom
+ 1 - regionTop
<= n
) {
2144 // There won't be anything left in the region, so just call
2145 // eraseScreen() and return.
2146 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2150 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2151 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2152 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2154 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2155 regionTop
+ remaining
);
2156 display
= new LinkedList
<DisplayLine
>(displayTop
);
2157 for (int i
= 0; i
< n
; i
++) {
2158 DisplayLine line
= new DisplayLine(currentState
.attr
);
2159 line
.setReverseColor(reverseVideo
);
2162 display
.addAll(displayMiddle
);
2163 display
.addAll(displayBottom
);
2165 assert (display
.size() == height
);
2169 * Process a control character.
2171 * @param ch 8-bit character from the remote side
2173 private void handleControlChar(final char ch
) {
2174 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2185 // Transmit the answerback message.
2196 cursorLeft(1, false);
2201 advanceToNextTabStop();
2227 currentState
.glLockshift
= LockshiftMode
.NONE
;
2233 currentState
.glLockshift
= LockshiftMode
.NONE
;
2258 singleshift
= Singleshift
.SS2
;
2263 singleshift
= Singleshift
.SS3
;
2273 * Advance the cursor to the next tab stop.
2275 private void advanceToNextTabStop() {
2276 if (tabStops
.size() == 0) {
2277 // Go to the rightmost column
2278 cursorRight(rightMargin
- currentState
.cursorX
, false);
2281 for (Integer stop
: tabStops
) {
2282 if (stop
> currentState
.cursorX
) {
2283 cursorRight(stop
- currentState
.cursorX
, false);
2288 * We got here, meaning there isn't a tab stop beyond the current
2289 * cursor position. Place the cursor of the right-most edge of the
2292 cursorRight(rightMargin
- currentState
.cursorX
, false);
2296 * Save a character into the collect buffer.
2298 * @param ch character to save
2300 private void collect(final char ch
) {
2301 collectBuffer
.append(ch
);
2305 * Save a byte into the CSI parameters buffer.
2307 * @param ch byte to save
2309 private void param(final byte ch
) {
2310 if (csiParams
.size() == 0) {
2311 csiParams
.add(new Integer(0));
2313 Integer x
= csiParams
.get(csiParams
.size() - 1);
2314 if ((ch
>= '0') && (ch
<= '9')) {
2317 csiParams
.set(csiParams
.size() - 1, x
);
2321 csiParams
.add(new Integer(0));
2326 * Get a CSI parameter value, with a default.
2328 * @param position parameter index. 0 is the first parameter.
2329 * @param defaultValue value to use if csiParams[position] doesn't exist
2330 * @return parameter value
2332 private int getCsiParam(final int position
, final int defaultValue
) {
2333 if (csiParams
.size() < position
+ 1) {
2334 return defaultValue
;
2336 return csiParams
.get(position
).intValue();
2340 * Get a CSI parameter value, clamped to within min/max.
2342 * @param position parameter index. 0 is the first parameter.
2343 * @param defaultValue value to use if csiParams[position] doesn't exist
2344 * @param minValue minimum value inclusive
2345 * @param maxValue maximum value inclusive
2346 * @return parameter value
2348 private int getCsiParam(final int position
, final int defaultValue
,
2349 final int minValue
, final int maxValue
) {
2351 assert (minValue
<= maxValue
);
2352 int value
= getCsiParam(position
, defaultValue
);
2353 if (value
< minValue
) {
2356 if (value
> maxValue
) {
2363 * Set or unset a toggle.
2365 * @param value true for set ('h'), false for reset ('l')
2367 private void setToggle(final boolean value
) {
2368 boolean decPrivateModeFlag
= false;
2369 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2370 if (collectBuffer
.charAt(i
) == '?') {
2371 decPrivateModeFlag
= true;
2376 for (Integer i
: csiParams
) {
2381 if (decPrivateModeFlag
== true) {
2383 if (value
== true) {
2384 // Use application arrow keys
2385 arrowKeyMode
= ArrowKeyMode
.VT100
;
2387 // Use ANSI arrow keys
2388 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2393 if (decPrivateModeFlag
== true) {
2394 if (value
== false) {
2398 arrowKeyMode
= ArrowKeyMode
.VT52
;
2401 * From the VT102 docs: "You use ANSI mode to select
2402 * most terminal features; the terminal uses the same
2403 * features when it switches to VT52 mode. You
2404 * cannot, however, change most of these features in
2407 * In other words, do not reset any other attributes
2408 * when switching between VT52 submode and ANSI.
2410 * HOWEVER, the real vt100 does switch the character
2411 * set according to Usenet.
2413 currentState
.g0Charset
= CharacterSet
.US
;
2414 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2417 if ((type
== DeviceType
.VT220
)
2418 || (type
== DeviceType
.XTERM
)) {
2420 // VT52 mode is explicitly 7-bit
2422 singleshift
= Singleshift
.NONE
;
2427 if (value
== true) {
2428 // Turn off keyboard
2437 if (decPrivateModeFlag
== true) {
2439 if (value
== true) {
2448 width
= rightMargin
+ 1;
2449 // Entire screen is cleared, and scrolling region is
2451 eraseScreen(0, 0, height
- 1, width
- 1, false);
2452 scrollRegionTop
= 0;
2453 scrollRegionBottom
= height
- 1;
2454 // Also home the cursor
2455 cursorPosition(0, 0);
2459 if (decPrivateModeFlag
== true) {
2461 if (value
== true) {
2470 if (value
== true) {
2478 if (decPrivateModeFlag
== true) {
2480 if (value
== true) {
2482 * Set selects reverse screen, a white screen
2483 * background with black characters.
2485 if (reverseVideo
!= true) {
2487 * If in normal video, switch it back
2489 invertDisplayColors();
2491 reverseVideo
= true;
2494 * Reset selects normal screen, a black screen
2495 * background with white characters.
2497 if (reverseVideo
== true) {
2499 * If in reverse video already, switch it back
2501 invertDisplayColors();
2503 reverseVideo
= false;
2508 if (decPrivateModeFlag
== true) {
2510 if (value
== true) {
2511 // Origin is relative to scroll region cursor.
2512 // Cursor can NEVER leave scrolling region.
2513 currentState
.originMode
= true;
2514 cursorPosition(0, 0);
2516 // Origin is absolute to entire screen. Cursor can
2517 // leave the scrolling region via cup() and hvp().
2518 currentState
.originMode
= false;
2519 cursorPosition(0, 0);
2524 if (decPrivateModeFlag
== true) {
2526 if (value
== true) {
2528 currentState
.lineWrap
= true;
2530 // Turn linewrap off
2531 currentState
.lineWrap
= false;
2536 if (decPrivateModeFlag
== true) {
2538 if (value
== true) {
2539 // Keyboard auto-repeat on
2542 // Keyboard auto-repeat off
2548 if (decPrivateModeFlag
== false) {
2550 if (value
== true) {
2560 if (decPrivateModeFlag
== true) {
2566 if (decPrivateModeFlag
== true) {
2572 if (decPrivateModeFlag
== false) {
2574 if (value
== true) {
2576 * Set causes a received linefeed, form feed, or
2577 * vertical tab to move cursor to first column of
2578 * next line. RETURN transmits both a carriage return
2579 * and linefeed. This selection is also called new
2585 * Reset causes a received linefeed, form feed, or
2586 * vertical tab to move cursor to next line in
2587 * current column. RETURN transmits a carriage
2590 newLineMode
= false;
2596 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2597 if (decPrivateModeFlag
== true) {
2599 if (value
== true) {
2601 cursorVisible
= true;
2604 cursorVisible
= false;
2611 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2612 if (decPrivateModeFlag
== true) {
2614 if (value
== true) {
2615 // Select national mode NRC
2618 // Select multi-national mode
2627 if ((type
== DeviceType
.XTERM
)
2628 && (decPrivateModeFlag
== true)
2630 // Mouse: normal tracking mode
2631 if (value
== true) {
2632 mouseProtocol
= MouseProtocol
.NORMAL
;
2634 mouseProtocol
= MouseProtocol
.OFF
;
2640 if ((type
== DeviceType
.XTERM
)
2641 && (decPrivateModeFlag
== true)
2643 // Mouse: normal tracking mode
2644 if (value
== true) {
2645 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2647 mouseProtocol
= MouseProtocol
.OFF
;
2653 if ((type
== DeviceType
.XTERM
)
2654 && (decPrivateModeFlag
== true)
2656 // Mouse: Any-event tracking mode
2657 if (value
== true) {
2658 mouseProtocol
= MouseProtocol
.ANYEVENT
;
2660 mouseProtocol
= MouseProtocol
.OFF
;
2666 if ((type
== DeviceType
.XTERM
)
2667 && (decPrivateModeFlag
== true)
2669 // Mouse: UTF-8 coordinates
2670 if (value
== true) {
2671 mouseEncoding
= MouseEncoding
.UTF8
;
2673 mouseEncoding
= MouseEncoding
.X10
;
2679 if ((type
== DeviceType
.XTERM
)
2680 && (decPrivateModeFlag
== true)
2682 // Mouse: SGR coordinates
2683 if (value
== true) {
2684 mouseEncoding
= MouseEncoding
.SGR
;
2686 mouseEncoding
= MouseEncoding
.X10
;
2699 * DECSC - Save cursor.
2701 private void decsc() {
2702 savedState
.setTo(currentState
);
2706 * DECRC - Restore cursor.
2708 private void decrc() {
2709 currentState
.setTo(savedState
);
2715 private void ind() {
2716 // Move the cursor and scroll if necessary. If at the bottom line
2717 // already, a scroll up is supposed to be performed.
2718 if (currentState
.cursorY
== scrollRegionBottom
) {
2719 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2721 cursorDown(1, true);
2725 * RI - Reverse index.
2728 // Move the cursor and scroll if necessary. If at the top line
2729 // already, a scroll down is supposed to be performed.
2730 if (currentState
.cursorY
== scrollRegionTop
) {
2731 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2739 private void nel() {
2740 // Move the cursor and scroll if necessary. If at the bottom line
2741 // already, a scroll up is supposed to be performed.
2742 if (currentState
.cursorY
== scrollRegionBottom
) {
2743 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2745 cursorDown(1, true);
2747 // Reset to the beginning of the next line
2748 currentState
.cursorX
= 0;
2752 * DECKPAM - Keypad application mode.
2754 private void deckpam() {
2755 keypadMode
= KeypadMode
.Application
;
2759 * DECKPNM - Keypad numeric mode.
2761 private void deckpnm() {
2762 keypadMode
= KeypadMode
.Numeric
;
2768 * @param n number of spaces to move
2769 * @param honorScrollRegion if true, then do nothing if the cursor is
2770 * outside the scrolling region
2772 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2776 * Special case: if a user moves the cursor from the right margin, we
2777 * have to reset the VT100 right margin flag.
2780 wrapLineFlag
= false;
2783 for (int i
= 0; i
< n
; i
++) {
2784 if (honorScrollRegion
== true) {
2785 // Honor the scrolling region
2786 if ((currentState
.cursorY
< scrollRegionTop
)
2787 || (currentState
.cursorY
> scrollRegionBottom
)
2789 // Outside region, do nothing
2792 // Inside region, go up
2793 top
= scrollRegionTop
;
2795 // Non-scrolling case
2799 if (currentState
.cursorY
> top
) {
2800 currentState
.cursorY
--;
2806 * Move down n spaces.
2808 * @param n number of spaces to move
2809 * @param honorScrollRegion if true, then do nothing if the cursor is
2810 * outside the scrolling region
2812 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
2816 * Special case: if a user moves the cursor from the right margin, we
2817 * have to reset the VT100 right margin flag.
2820 wrapLineFlag
= false;
2823 for (int i
= 0; i
< n
; i
++) {
2825 if (honorScrollRegion
== true) {
2826 // Honor the scrolling region
2827 if (currentState
.cursorY
> scrollRegionBottom
) {
2828 // Outside region, do nothing
2831 // Inside region, go down
2832 bottom
= scrollRegionBottom
;
2834 // Non-scrolling case
2835 bottom
= height
- 1;
2838 if (currentState
.cursorY
< bottom
) {
2839 currentState
.cursorY
++;
2845 * Move left n spaces.
2847 * @param n number of spaces to move
2848 * @param honorScrollRegion if true, then do nothing if the cursor is
2849 * outside the scrolling region
2851 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
2853 * Special case: if a user moves the cursor from the right margin, we
2854 * have to reset the VT100 right margin flag.
2857 wrapLineFlag
= false;
2860 for (int i
= 0; i
< n
; i
++) {
2861 if (honorScrollRegion
== true) {
2862 // Honor the scrolling region
2863 if ((currentState
.cursorY
< scrollRegionTop
)
2864 || (currentState
.cursorY
> scrollRegionBottom
)
2866 // Outside region, do nothing
2871 if (currentState
.cursorX
> 0) {
2872 currentState
.cursorX
--;
2878 * Move right n spaces.
2880 * @param n number of spaces to move
2881 * @param honorScrollRegion if true, then do nothing if the cursor is
2882 * outside the scrolling region
2884 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
2885 int rightMargin
= this.rightMargin
;
2888 * Special case: if a user moves the cursor from the right margin, we
2889 * have to reset the VT100 right margin flag.
2892 wrapLineFlag
= false;
2895 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2896 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2899 for (int i
= 0; i
< n
; i
++) {
2900 if (honorScrollRegion
== true) {
2901 // Honor the scrolling region
2902 if ((currentState
.cursorY
< scrollRegionTop
)
2903 || (currentState
.cursorY
> scrollRegionBottom
)
2905 // Outside region, do nothing
2910 if (currentState
.cursorX
< rightMargin
) {
2911 currentState
.cursorX
++;
2917 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2919 * @param row row to move to
2920 * @param col column to move to
2922 private void cursorPosition(int row
, final int col
) {
2923 int rightMargin
= this.rightMargin
;
2928 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2929 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2932 // Set column number
2933 currentState
.cursorX
= col
;
2935 // Sanity check, bring column back to margin.
2936 if (currentState
.cursorX
> rightMargin
) {
2937 currentState
.cursorX
= rightMargin
;
2941 if (currentState
.originMode
== true) {
2942 row
+= scrollRegionTop
;
2944 if (currentState
.cursorY
< row
) {
2945 cursorDown(row
- currentState
.cursorY
, false);
2946 } else if (currentState
.cursorY
> row
) {
2947 cursorUp(currentState
.cursorY
- row
, false);
2950 wrapLineFlag
= false;
2954 * HTS - Horizontal tabulation set.
2956 private void hts() {
2957 for (Integer stop
: tabStops
) {
2958 if (stop
== currentState
.cursorX
) {
2959 // Already have a tab stop here
2964 // Append a tab stop to the end of the array and resort them
2965 tabStops
.add(currentState
.cursorX
);
2966 Collections
.sort(tabStops
);
2970 * DECSWL - Single-width line.
2972 private void decswl() {
2973 display
.get(currentState
.cursorY
).setDoubleWidth(false);
2974 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2978 * DECDWL - Double-width line.
2980 private void decdwl() {
2981 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2982 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2986 * DECHDL - Double-height + double-width line.
2988 * @param topHalf if true, this sets the row to be the top half row of a
2991 private void dechdl(final boolean topHalf
) {
2992 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2993 if (topHalf
== true) {
2994 display
.get(currentState
.cursorY
).setDoubleHeight(1);
2996 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3001 * DECALN - Screen alignment display.
3003 private void decaln() {
3004 Cell newCell
= new Cell();
3005 newCell
.setChar('E');
3006 for (DisplayLine line
: display
) {
3007 for (int i
= 0; i
< line
.length(); i
++) {
3008 line
.replace(i
, newCell
);
3014 * DECSCL - Compatibility level.
3016 private void decscl() {
3017 int i
= getCsiParam(0, 0);
3018 int j
= getCsiParam(1, 0);
3022 currentState
.g0Charset
= CharacterSet
.US
;
3023 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3025 } else if (i
== 62) {
3027 if ((j
== 0) || (j
== 2)) {
3029 } else if (j
== 1) {
3036 * CUD - Cursor down.
3038 private void cud() {
3039 cursorDown(getCsiParam(0, 1, 1, height
), true);
3043 * CUF - Cursor forward.
3045 private void cuf() {
3046 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3050 * CUB - Cursor backward.
3052 private void cub() {
3053 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3059 private void cuu() {
3060 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3064 * CUP - Cursor position.
3066 private void cup() {
3067 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3068 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3072 * CNL - Cursor down and to column 1.
3074 private void cnl() {
3075 cursorDown(getCsiParam(0, 1, 1, height
), true);
3077 cursorLeft(currentState
.cursorX
, true);
3081 * CPL - Cursor up and to column 1.
3083 private void cpl() {
3084 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3086 cursorLeft(currentState
.cursorX
, true);
3090 * CHA - Cursor to column # in current row.
3092 private void cha() {
3093 cursorPosition(currentState
.cursorY
,
3094 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3098 * VPA - Cursor to row #, same column.
3100 private void vpa() {
3101 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3102 currentState
.cursorX
);
3106 * ED - Erase in display.
3109 boolean honorProtected
= false;
3110 boolean decPrivateModeFlag
= false;
3112 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3113 if (collectBuffer
.charAt(i
) == '?') {
3114 decPrivateModeFlag
= true;
3119 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3120 && (decPrivateModeFlag
== true)
3122 honorProtected
= true;
3125 int i
= getCsiParam(0, 0);
3128 // Erase from here to end of screen
3129 if (currentState
.cursorY
< height
- 1) {
3130 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3133 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3134 } else if (i
== 1) {
3135 // Erase from beginning of screen to here
3136 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3138 eraseLine(0, currentState
.cursorX
, honorProtected
);
3139 } else if (i
== 2) {
3140 // Erase entire screen
3141 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3146 * EL - Erase in line.
3149 boolean honorProtected
= false;
3150 boolean decPrivateModeFlag
= false;
3152 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3153 if (collectBuffer
.charAt(i
) == '?') {
3154 decPrivateModeFlag
= true;
3159 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3160 && (decPrivateModeFlag
== true)
3162 honorProtected
= true;
3165 int i
= getCsiParam(0, 0);
3168 // Erase from here to end of line
3169 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3170 } else if (i
== 1) {
3171 // Erase from beginning of line to here
3172 eraseLine(0, currentState
.cursorX
, honorProtected
);
3173 } else if (i
== 2) {
3174 // Erase entire line
3175 eraseLine(0, width
- 1, honorProtected
);
3180 * ECH - Erase # of characters in current row.
3182 private void ech() {
3183 int i
= getCsiParam(0, 1, 1, width
);
3185 // Erase from here to i characters
3186 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3193 int i
= getCsiParam(0, 1);
3195 if ((currentState
.cursorY
>= scrollRegionTop
)
3196 && (currentState
.cursorY
<= scrollRegionBottom
)
3199 // I can get the same effect with a scroll-down
3200 scrollingRegionScrollDown(currentState
.cursorY
,
3201 scrollRegionBottom
, i
);
3206 * DCH - Delete char.
3208 private void dch() {
3209 int n
= getCsiParam(0, 1);
3210 DisplayLine line
= display
.get(currentState
.cursorY
);
3211 Cell blank
= new Cell();
3212 for (int i
= 0; i
< n
; i
++) {
3213 line
.delete(currentState
.cursorX
, blank
);
3218 * ICH - Insert blank char at cursor.
3220 private void ich() {
3221 int n
= getCsiParam(0, 1);
3222 DisplayLine line
= display
.get(currentState
.cursorY
);
3223 Cell blank
= new Cell();
3224 for (int i
= 0; i
< n
; i
++) {
3225 line
.insert(currentState
.cursorX
, blank
);
3233 int i
= getCsiParam(0, 1);
3235 if ((currentState
.cursorY
>= scrollRegionTop
)
3236 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3238 // I can get the same effect with a scroll-down
3239 scrollingRegionScrollUp(currentState
.cursorY
,
3240 scrollRegionBottom
, i
);
3245 * HVP - Horizontal and vertical position.
3247 private void hvp() {
3252 * REP - Repeat character.
3254 private void rep() {
3255 int n
= getCsiParam(0, 1);
3256 for (int i
= 0; i
< n
; i
++) {
3257 printCharacter(repCh
);
3265 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3266 getCsiParam(0, 1, 1, height
));
3273 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3274 getCsiParam(0, 1, 1, height
));
3278 * CBT - Go back X tab stops.
3280 private void cbt() {
3281 int tabsToMove
= getCsiParam(0, 1);
3284 for (int i
= 0; i
< tabsToMove
; i
++) {
3285 int j
= currentState
.cursorX
;
3286 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3287 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3295 j
= tabStops
.get(tabI
);
3297 cursorPosition(currentState
.cursorY
, j
);
3302 * CHT - Advance X tab stops.
3304 private void cht() {
3305 int n
= getCsiParam(0, 1);
3306 for (int i
= 0; i
< n
; i
++) {
3307 advanceToNextTabStop();
3312 * SGR - Select graphics rendition.
3314 private void sgr() {
3316 if (csiParams
.size() == 0) {
3317 currentState
.attr
.reset();
3321 for (Integer i
: csiParams
) {
3327 currentState
.attr
.reset();
3332 currentState
.attr
.setBold(true);
3337 currentState
.attr
.setUnderline(true);
3342 currentState
.attr
.setBlink(true);
3347 currentState
.attr
.setReverse(true);
3354 if (type
== DeviceType
.XTERM
) {
3368 if ((type
== DeviceType
.VT220
)
3369 || (type
== DeviceType
.XTERM
)) {
3375 currentState
.attr
.setBold(false);
3380 currentState
.attr
.setUnderline(false);
3385 currentState
.attr
.setBlink(false);
3390 currentState
.attr
.setReverse(false);
3398 // A true VT100/102/220 does not support color, however everyone
3399 // is used to their terminal emulator supporting color so we will
3400 // unconditionally support color for all DeviceType's.
3405 // Set black foreground
3406 currentState
.attr
.setForeColor(Color
.BLACK
);
3409 // Set red foreground
3410 currentState
.attr
.setForeColor(Color
.RED
);
3413 // Set green foreground
3414 currentState
.attr
.setForeColor(Color
.GREEN
);
3417 // Set yellow foreground
3418 currentState
.attr
.setForeColor(Color
.YELLOW
);
3421 // Set blue foreground
3422 currentState
.attr
.setForeColor(Color
.BLUE
);
3425 // Set magenta foreground
3426 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3429 // Set cyan foreground
3430 currentState
.attr
.setForeColor(Color
.CYAN
);
3433 // Set white foreground
3434 currentState
.attr
.setForeColor(Color
.WHITE
);
3437 if (type
== DeviceType
.XTERM
) {
3439 * Xterm supports T.416 / ISO-8613-3 codes to select
3440 * either an indexed color or an RGB value. (It also
3441 * permits these ISO-8613-3 SGR sequences to be separated
3442 * by colons rather than semicolons.)
3444 * We will not support any of these additional color
3445 * codes at this time:
3447 * 1. http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
3448 * has a detailed discussion of the current state of
3449 * RGB in various terminals, the point of which is
3450 * that none of them really do the same thing despite
3451 * all appearing to be "xterm".
3454 * https://bugs.kde.org/show_bug.cgi?id=107487#c3,
3455 * even supporting just the "indexed mode" of these
3456 * sequences (which could align easily with existing
3457 * SGR colors) is assumed to mean full support of
3458 * 24-bit RGB. So it is all or nothing.
3460 * Finally, these sequences break the assumptions of
3461 * standard ECMA-48 style parsers as pointed out at
3462 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
3463 * Therefore in order to keep a clean display, we cannot
3464 * parse anything else in this sequence.
3468 // Underscore on, default foreground color
3469 currentState
.attr
.setUnderline(true);
3470 currentState
.attr
.setForeColor(Color
.WHITE
);
3474 // Underscore off, default foreground color
3475 currentState
.attr
.setUnderline(false);
3476 currentState
.attr
.setForeColor(Color
.WHITE
);
3479 // Set black background
3480 currentState
.attr
.setBackColor(Color
.BLACK
);
3483 // Set red background
3484 currentState
.attr
.setBackColor(Color
.RED
);
3487 // Set green background
3488 currentState
.attr
.setBackColor(Color
.GREEN
);
3491 // Set yellow background
3492 currentState
.attr
.setBackColor(Color
.YELLOW
);
3495 // Set blue background
3496 currentState
.attr
.setBackColor(Color
.BLUE
);
3499 // Set magenta background
3500 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3503 // Set cyan background
3504 currentState
.attr
.setBackColor(Color
.CYAN
);
3507 // Set white background
3508 currentState
.attr
.setBackColor(Color
.WHITE
);
3511 if (type
== DeviceType
.XTERM
) {
3513 * Xterm supports T.416 / ISO-8613-3 codes to select
3514 * either an indexed color or an RGB value. (It also
3515 * permits these ISO-8613-3 SGR sequences to be separated
3516 * by colons rather than semicolons.)
3518 * We will not support this at this time. Also, in order
3519 * to keep a clean display, we cannot parse anything else
3526 // Default background
3527 currentState
.attr
.setBackColor(Color
.BLACK
);
3537 * DA - Device attributes.
3540 int extendedFlag
= 0;
3542 if (collectBuffer
.length() > 0) {
3543 String args
= collectBuffer
.substring(1);
3544 if (collectBuffer
.charAt(0) == '>') {
3546 if (collectBuffer
.length() >= 2) {
3547 i
= Integer
.parseInt(args
.toString());
3549 } else if (collectBuffer
.charAt(0) == '=') {
3551 if (collectBuffer
.length() >= 2) {
3552 i
= Integer
.parseInt(args
.toString());
3555 // Unknown code, bail out
3560 if ((i
!= 0) && (i
!= 1)) {
3564 if ((extendedFlag
== 0) && (i
== 0)) {
3565 // Send string directly to remote side
3566 writeRemote(deviceTypeResponse());
3570 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3572 if ((extendedFlag
== 1) && (i
== 0)) {
3574 * Request "What type of terminal are you, what is your
3575 * firmware version, and what hardware options do you have
3578 * Respond: "I am a VT220 (identification code of 1), my
3579 * firmware version is _____ (Pv), and I have _____ Po
3580 * options installed."
3586 if (s8c1t
== true) {
3587 writeRemote("\u009b>1;10;0c");
3589 writeRemote("\033[>1;10;0c");
3595 if ((extendedFlag
== 2) && (i
== 0)) {
3598 * Request "What is your unit ID?"
3600 * Respond: "I was manufactured at site 00 and have a unique ID
3604 writeRemote("\033P!|00010203\033\\");
3609 * DECSTBM - Set top and bottom margins.
3611 private void decstbm() {
3612 boolean decPrivateModeFlag
= false;
3614 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3615 if (collectBuffer
.charAt(i
) == '?') {
3616 decPrivateModeFlag
= true;
3620 if (decPrivateModeFlag
) {
3621 // This could be restore DEC private mode values.
3625 int top
= getCsiParam(0, 1, 1, height
) - 1;
3626 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3631 scrollRegionTop
= top
;
3632 scrollRegionBottom
= bottom
;
3635 cursorPosition(0, 0);
3640 * DECREQTPARM - Request terminal parameters.
3642 private void decreqtparm() {
3643 int i
= getCsiParam(0, 0);
3645 if ((i
!= 0) && (i
!= 1)) {
3652 * Request terminal parameters.
3656 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3657 * (CLoCk MULtiplier = 1, STP option flags = 0)
3661 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3664 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3666 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3672 * DECSCA - Select Character Attributes.
3674 private void decsca() {
3675 int i
= getCsiParam(0, 0);
3677 if ((i
== 0) || (i
== 2)) {
3679 currentState
.attr
.setProtect(false);
3683 currentState
.attr
.setProtect(true);
3688 * DECSTR - Soft Terminal Reset.
3690 private void decstr() {
3691 // Do exactly like RIS - Reset to initial state
3693 // Do I clear screen too? I think so...
3694 eraseScreen(0, 0, height
- 1, width
- 1, false);
3695 cursorPosition(0, 0);
3699 * DSR - Device status report.
3701 private void dsr() {
3702 boolean decPrivateModeFlag
= false;
3703 int row
= currentState
.cursorY
;
3705 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3706 if (collectBuffer
.charAt(i
) == '?') {
3707 decPrivateModeFlag
= true;
3712 int i
= getCsiParam(0, 0);
3717 // Request status report. Respond with "OK, no malfunction."
3719 // Send string directly to remote side
3720 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3723 writeRemote("\u009b0n");
3725 writeRemote("\033[0n");
3730 // Request cursor position. Respond with current position.
3731 if (currentState
.originMode
== true) {
3732 row
-= scrollRegionTop
;
3735 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3738 str
= String
.format("\u009b%d;%dR", row
+ 1,
3739 currentState
.cursorX
+ 1);
3741 str
= String
.format("\033[%d;%dR", row
+ 1,
3742 currentState
.cursorX
+ 1);
3745 // Send string directly to remote side
3750 if (decPrivateModeFlag
== true) {
3752 // Request printer status report. Respond with "Printer not
3755 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3756 && (s8c1t
== true)) {
3757 writeRemote("\u009b?13n");
3759 writeRemote("\033[?13n");
3765 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3766 && (decPrivateModeFlag
== true)
3769 // Request user-defined keys are locked or unlocked. Respond
3770 // with "User-defined keys are locked."
3772 if (s8c1t
== true) {
3773 writeRemote("\u009b?21n");
3775 writeRemote("\033[?21n");
3781 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3782 && (decPrivateModeFlag
== true)
3785 // Request keyboard language. Respond with "Keyboard
3786 // language is North American."
3788 if (s8c1t
== true) {
3789 writeRemote("\u009b?27;1n");
3791 writeRemote("\033[?27;1n");
3798 // Some other option, ignore
3804 * TBC - Tabulation clear.
3806 private void tbc() {
3807 int i
= getCsiParam(0, 0);
3809 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3810 for (Integer stop
: tabStops
) {
3811 if (stop
== currentState
.cursorX
) {
3816 tabStops
= newStops
;
3824 * Erase the characters in the current line from the start column to the
3825 * end column, inclusive.
3827 * @param start starting column to erase (between 0 and width - 1)
3828 * @param end ending column to erase (between 0 and width - 1)
3829 * @param honorProtected if true, do not erase characters with the
3830 * protected attribute set
3832 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3837 if (end
> width
- 1) {
3844 for (int i
= start
; i
<= end
; i
++) {
3845 DisplayLine line
= display
.get(currentState
.cursorY
);
3846 if ((!honorProtected
)
3847 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
3854 * From the VT102 manual:
3856 * Erasing a character also erases any character
3857 * attribute of the character.
3863 * Erase with the current color a.k.a. back-color erase
3866 line
.setChar(i
, ' ');
3867 line
.setAttr(i
, currentState
.attr
);
3875 * Erase a rectangular section of the screen, inclusive. end column,
3878 * @param startRow starting row to erase (between 0 and height - 1)
3879 * @param startCol starting column to erase (between 0 and width - 1)
3880 * @param endRow ending row to erase (between 0 and height - 1)
3881 * @param endCol ending column to erase (between 0 and width - 1)
3882 * @param honorProtected if true, do not erase characters with the
3883 * protected attribute set
3885 private void eraseScreen(final int startRow
, final int startCol
,
3886 final int endRow
, final int endCol
, final boolean honorProtected
) {
3894 || (endRow
< startRow
)
3895 || (endCol
< startCol
)
3900 oldCursorY
= currentState
.cursorY
;
3901 for (int i
= startRow
; i
<= endRow
; i
++) {
3902 currentState
.cursorY
= i
;
3903 eraseLine(startCol
, endCol
, honorProtected
);
3905 // Erase display clears the double attributes
3906 display
.get(i
).setDoubleWidth(false);
3907 display
.get(i
).setDoubleHeight(0);
3909 currentState
.cursorY
= oldCursorY
;
3913 * VT220 printer functions. All of these are parsed, but won't do
3916 private void printerFunctions() {
3917 boolean decPrivateModeFlag
= false;
3918 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3919 if (collectBuffer
.charAt(i
) == '?') {
3920 decPrivateModeFlag
= true;
3925 int i
= getCsiParam(0, 0);
3930 if (decPrivateModeFlag
== false) {
3936 if (decPrivateModeFlag
== true) {
3937 // Print cursor line
3942 if (decPrivateModeFlag
== true) {
3943 // Auto print mode OFF
3945 // Printer controller OFF
3947 // Characters re-appear on the screen
3948 printerControllerMode
= false;
3953 if (decPrivateModeFlag
== true) {
3957 // Printer controller
3959 // Characters get sucked into oblivion
3960 printerControllerMode
= true;
3971 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
3972 * of remote systems will send an XTerm title sequence even if TERM isn't
3975 * @param xtermChar the character received from the remote side
3977 private void oscPut(final char xtermChar
) {
3979 collectBuffer
.append(xtermChar
);
3982 if (xtermChar
== 0x07) {
3983 String args
= collectBuffer
.substring(0,
3984 collectBuffer
.length() - 1);
3985 String
[] p
= args
.toString().split(";");
3987 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
3995 // Go to SCAN_GROUND state
4002 * Run this input character through the ECMA48 state machine.
4004 * @param ch character from the remote side
4006 private void consume(char ch
) {
4009 // System.err.printf("%c", ch);
4011 // Special case for VT10x: 7-bit characters only
4012 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4013 ch
= (char)(ch
& 0x7F);
4016 // Special "anywhere" states
4018 // 18, 1A --> execute, then switch to SCAN_GROUND
4019 if ((ch
== 0x18) || (ch
== 0x1A)) {
4020 // CAN and SUB abort escape sequences
4025 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4029 && (scanState
!= ScanState
.DCS_ENTRY
)
4030 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4031 && (scanState
!= ScanState
.DCS_IGNORE
)
4032 && (scanState
!= ScanState
.DCS_PARAM
)
4033 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4036 scanState
= ScanState
.ESCAPE
;
4040 // 0x9B == CSI 8-bit sequence
4042 scanState
= ScanState
.CSI_ENTRY
;
4046 // 0x9D goes to ScanState.OSC_STRING
4048 scanState
= ScanState
.OSC_STRING
;
4052 // 0x90 goes to DCS_ENTRY
4054 scanState
= ScanState
.DCS_ENTRY
;
4058 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4059 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4060 scanState
= ScanState
.SOSPMAPC_STRING
;
4064 // 0x7F (DEL) is always discarded
4069 switch (scanState
) {
4072 // 00-17, 19, 1C-1F --> execute
4073 // 80-8F, 91-9A, 9C --> execute
4074 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4075 handleControlChar(ch
);
4079 if (((ch
>= 0x20) && (ch
<= 0x7F))
4083 // VT220 printer --> trash bin
4084 if (((type
== DeviceType
.VT220
)
4085 || (type
== DeviceType
.XTERM
))
4086 && (printerControllerMode
== true)
4091 // Hang onto this character
4092 repCh
= mapCharacter(ch
);
4094 // Print this character
4095 printCharacter(repCh
);
4100 // 00-17, 19, 1C-1F --> execute
4102 handleControlChar(ch
);
4106 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4107 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4109 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4113 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4114 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4125 // DECSC - Save cursor
4126 // Note this code overlaps both ANSI and VT52 mode
4131 // DECRC - Restore cursor
4132 // Note this code overlaps both ANSI and VT52 mode
4141 if (vt52Mode
== true) {
4142 // DECANM - Enter ANSI mode
4144 arrowKeyMode
= ArrowKeyMode
.VT100
;
4147 * From the VT102 docs: "You use ANSI mode to select
4148 * most terminal features; the terminal uses the same
4149 * features when it switches to VT52 mode. You
4150 * cannot, however, change most of these features in
4153 * In other words, do not reset any other attributes
4154 * when switching between VT52 submode and ANSI.
4158 currentState
.g0Charset
= CharacterSet
.US
;
4159 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4161 singleshift
= Singleshift
.NONE
;
4162 currentState
.glLockshift
= LockshiftMode
.NONE
;
4163 currentState
.grLockshift
= LockshiftMode
.NONE
;
4167 // DECKPAM - Keypad application mode
4168 // Note this code overlaps both ANSI and VT52 mode
4172 // DECKPNM - Keypad numeric mode
4173 // Note this code overlaps both ANSI and VT52 mode
4180 if (vt52Mode
== true) {
4181 // Cursor up, and stop at the top without scrolling
4186 if (vt52Mode
== true) {
4187 // Cursor down, and stop at the bottom without scrolling
4188 cursorDown(1, false);
4192 if (vt52Mode
== true) {
4193 // Cursor right, and stop at the right without scrolling
4194 cursorRight(1, false);
4198 if (vt52Mode
== true) {
4199 // Cursor left, and stop at the left without scrolling
4200 cursorLeft(1, false);
4207 if (vt52Mode
== true) {
4215 if (vt52Mode
== true) {
4216 // G0 --> Special graphics
4217 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
4221 if (vt52Mode
== true) {
4223 currentState
.g0Charset
= CharacterSet
.US
;
4227 if (vt52Mode
== true) {
4229 cursorPosition(0, 0);
4231 // HTS - Horizontal tabulation set
4236 if (vt52Mode
== true) {
4237 // Reverse line feed. Same as RI.
4242 if (vt52Mode
== true) {
4243 // Erase to end of screen
4244 eraseLine(currentState
.cursorX
, width
- 1, false);
4245 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4250 if (vt52Mode
== true) {
4251 // Erase to end of line
4252 eraseLine(currentState
.cursorX
, width
- 1, false);
4258 if (vt52Mode
== true) {
4261 // RI - Reverse index
4266 if (vt52Mode
== false) {
4268 singleshift
= Singleshift
.SS2
;
4272 if (vt52Mode
== false) {
4274 singleshift
= Singleshift
.SS3
;
4281 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4297 if (vt52Mode
== true) {
4298 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4306 if (vt52Mode
== true) {
4308 // Send string directly to remote side
4309 writeRemote("\033/Z");
4312 // Send string directly to remote side
4313 writeRemote(deviceTypeResponse());
4324 // VT52 cannot get to any of these other states
4325 if (vt52Mode
== true) {
4330 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4337 // RIS - Reset to initial state
4339 // Do I clear screen too? I think so...
4340 eraseScreen(0, 0, height
- 1, width
- 1, false);
4341 cursorPosition(0, 0);
4355 if ((type
== DeviceType
.VT220
)
4356 || (type
== DeviceType
.XTERM
)) {
4358 // VT220 lockshift G2 into GL
4359 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4364 if ((type
== DeviceType
.VT220
)
4365 || (type
== DeviceType
.XTERM
)) {
4367 // VT220 lockshift G3 into GL
4368 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4386 if ((type
== DeviceType
.VT220
)
4387 || (type
== DeviceType
.XTERM
)) {
4389 // VT220 lockshift G3 into GR
4390 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4395 if ((type
== DeviceType
.VT220
)
4396 || (type
== DeviceType
.XTERM
)) {
4398 // VT220 lockshift G2 into GR
4399 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4405 if ((type
== DeviceType
.VT220
)
4406 || (type
== DeviceType
.XTERM
)) {
4408 // VT220 lockshift G1 into GR
4409 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4419 // 0x5B goes to CSI_ENTRY
4421 scanState
= ScanState
.CSI_ENTRY
;
4424 // 0x5D goes to OSC_STRING
4426 scanState
= ScanState
.OSC_STRING
;
4429 // 0x50 goes to DCS_ENTRY
4431 scanState
= ScanState
.DCS_ENTRY
;
4434 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4435 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4436 scanState
= ScanState
.SOSPMAPC_STRING
;
4441 case ESCAPE_INTERMEDIATE
:
4442 // 00-17, 19, 1C-1F --> execute
4444 handleControlChar(ch
);
4447 // 20-2F --> collect
4448 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4452 // 30-7E --> dispatch, then switch to GROUND
4453 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4456 if ((collectBuffer
.length() == 1)
4457 && (collectBuffer
.charAt(0) == '(')) {
4458 // G0 --> Special graphics
4459 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4461 if ((collectBuffer
.length() == 1)
4462 && (collectBuffer
.charAt(0) == ')')) {
4463 // G1 --> Special graphics
4464 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4466 if ((type
== DeviceType
.VT220
)
4467 || (type
== DeviceType
.XTERM
)) {
4469 if ((collectBuffer
.length() == 1)
4470 && (collectBuffer
.charAt(0) == '*')) {
4471 // G2 --> Special graphics
4472 currentState
.g2Charset
= CharacterSet
.DRAWING
;
4474 if ((collectBuffer
.length() == 1)
4475 && (collectBuffer
.charAt(0) == '+')) {
4476 // G3 --> Special graphics
4477 currentState
.g3Charset
= CharacterSet
.DRAWING
;
4482 if ((collectBuffer
.length() == 1)
4483 && (collectBuffer
.charAt(0) == '(')) {
4484 // G0 --> Alternate character ROM standard character set
4485 currentState
.g0Charset
= CharacterSet
.ROM
;
4487 if ((collectBuffer
.length() == 1)
4488 && (collectBuffer
.charAt(0) == ')')) {
4489 // G1 --> Alternate character ROM standard character set
4490 currentState
.g1Charset
= CharacterSet
.ROM
;
4494 if ((collectBuffer
.length() == 1)
4495 && (collectBuffer
.charAt(0) == '(')) {
4496 // G0 --> Alternate character ROM special graphics
4497 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4499 if ((collectBuffer
.length() == 1)
4500 && (collectBuffer
.charAt(0) == ')')) {
4501 // G1 --> Alternate character ROM special graphics
4502 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4506 if ((collectBuffer
.length() == 1)
4507 && (collectBuffer
.charAt(0) == '#')) {
4508 // DECDHL - Double-height line (top half)
4513 if ((collectBuffer
.length() == 1)
4514 && (collectBuffer
.charAt(0) == '#')) {
4515 // DECDHL - Double-height line (bottom half)
4518 if ((type
== DeviceType
.VT220
)
4519 || (type
== DeviceType
.XTERM
)) {
4521 if ((collectBuffer
.length() == 1)
4522 && (collectBuffer
.charAt(0) == '(')) {
4524 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4526 if ((collectBuffer
.length() == 1)
4527 && (collectBuffer
.charAt(0) == ')')) {
4529 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4531 if ((collectBuffer
.length() == 1)
4532 && (collectBuffer
.charAt(0) == '*')) {
4534 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4536 if ((collectBuffer
.length() == 1)
4537 && (collectBuffer
.charAt(0) == '+')) {
4539 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4544 if ((collectBuffer
.length() == 1)
4545 && (collectBuffer
.charAt(0) == '#')) {
4546 // DECSWL - Single-width line
4549 if ((type
== DeviceType
.VT220
)
4550 || (type
== DeviceType
.XTERM
)) {
4552 if ((collectBuffer
.length() == 1)
4553 && (collectBuffer
.charAt(0) == '(')) {
4555 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4557 if ((collectBuffer
.length() == 1)
4558 && (collectBuffer
.charAt(0) == ')')) {
4560 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4562 if ((collectBuffer
.length() == 1)
4563 && (collectBuffer
.charAt(0) == '*')) {
4565 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4567 if ((collectBuffer
.length() == 1)
4568 && (collectBuffer
.charAt(0) == '+')) {
4570 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4575 if ((collectBuffer
.length() == 1)
4576 && (collectBuffer
.charAt(0) == '#')) {
4577 // DECDWL - Double-width line
4580 if ((type
== DeviceType
.VT220
)
4581 || (type
== DeviceType
.XTERM
)) {
4583 if ((collectBuffer
.length() == 1)
4584 && (collectBuffer
.charAt(0) == '(')) {
4586 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4588 if ((collectBuffer
.length() == 1)
4589 && (collectBuffer
.charAt(0) == ')')) {
4591 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4593 if ((collectBuffer
.length() == 1)
4594 && (collectBuffer
.charAt(0) == '*')) {
4596 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4598 if ((collectBuffer
.length() == 1)
4599 && (collectBuffer
.charAt(0) == '+')) {
4601 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4606 if ((type
== DeviceType
.VT220
)
4607 || (type
== DeviceType
.XTERM
)) {
4609 if ((collectBuffer
.length() == 1)
4610 && (collectBuffer
.charAt(0) == '(')) {
4612 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4614 if ((collectBuffer
.length() == 1)
4615 && (collectBuffer
.charAt(0) == ')')) {
4617 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4619 if ((collectBuffer
.length() == 1)
4620 && (collectBuffer
.charAt(0) == '*')) {
4622 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4624 if ((collectBuffer
.length() == 1)
4625 && (collectBuffer
.charAt(0) == '+')) {
4627 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4632 if ((collectBuffer
.length() == 1)
4633 && (collectBuffer
.charAt(0) == '#')) {
4634 // DECALN - Screen alignment display
4643 if ((type
== DeviceType
.VT220
)
4644 || (type
== DeviceType
.XTERM
)) {
4646 if ((collectBuffer
.length() == 1)
4647 && (collectBuffer
.charAt(0) == '(')) {
4648 // G0 --> DEC_SUPPLEMENTAL
4649 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4651 if ((collectBuffer
.length() == 1)
4652 && (collectBuffer
.charAt(0) == ')')) {
4653 // G1 --> DEC_SUPPLEMENTAL
4654 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4656 if ((collectBuffer
.length() == 1)
4657 && (collectBuffer
.charAt(0) == '*')) {
4658 // G2 --> DEC_SUPPLEMENTAL
4659 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4661 if ((collectBuffer
.length() == 1)
4662 && (collectBuffer
.charAt(0) == '+')) {
4663 // G3 --> DEC_SUPPLEMENTAL
4664 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4669 if ((type
== DeviceType
.VT220
)
4670 || (type
== DeviceType
.XTERM
)) {
4672 if ((collectBuffer
.length() == 1)
4673 && (collectBuffer
.charAt(0) == '(')) {
4675 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4677 if ((collectBuffer
.length() == 1)
4678 && (collectBuffer
.charAt(0) == ')')) {
4680 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4682 if ((collectBuffer
.length() == 1)
4683 && (collectBuffer
.charAt(0) == '*')) {
4685 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4687 if ((collectBuffer
.length() == 1)
4688 && (collectBuffer
.charAt(0) == '+')) {
4690 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4699 if ((collectBuffer
.length() == 1)
4700 && (collectBuffer
.charAt(0) == '(')) {
4701 // G0 --> United Kingdom set
4702 currentState
.g0Charset
= CharacterSet
.UK
;
4704 if ((collectBuffer
.length() == 1)
4705 && (collectBuffer
.charAt(0) == ')')) {
4706 // G1 --> United Kingdom set
4707 currentState
.g1Charset
= CharacterSet
.UK
;
4709 if ((type
== DeviceType
.VT220
)
4710 || (type
== DeviceType
.XTERM
)) {
4712 if ((collectBuffer
.length() == 1)
4713 && (collectBuffer
.charAt(0) == '*')) {
4714 // G2 --> United Kingdom set
4715 currentState
.g2Charset
= CharacterSet
.UK
;
4717 if ((collectBuffer
.length() == 1)
4718 && (collectBuffer
.charAt(0) == '+')) {
4719 // G3 --> United Kingdom set
4720 currentState
.g3Charset
= CharacterSet
.UK
;
4725 if ((collectBuffer
.length() == 1)
4726 && (collectBuffer
.charAt(0) == '(')) {
4728 currentState
.g0Charset
= CharacterSet
.US
;
4730 if ((collectBuffer
.length() == 1)
4731 && (collectBuffer
.charAt(0) == ')')) {
4733 currentState
.g1Charset
= CharacterSet
.US
;
4735 if ((type
== DeviceType
.VT220
)
4736 || (type
== DeviceType
.XTERM
)) {
4738 if ((collectBuffer
.length() == 1)
4739 && (collectBuffer
.charAt(0) == '*')) {
4741 currentState
.g2Charset
= CharacterSet
.US
;
4743 if ((collectBuffer
.length() == 1)
4744 && (collectBuffer
.charAt(0) == '+')) {
4746 currentState
.g3Charset
= CharacterSet
.US
;
4751 if ((type
== DeviceType
.VT220
)
4752 || (type
== DeviceType
.XTERM
)) {
4754 if ((collectBuffer
.length() == 1)
4755 && (collectBuffer
.charAt(0) == '(')) {
4757 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4759 if ((collectBuffer
.length() == 1)
4760 && (collectBuffer
.charAt(0) == ')')) {
4762 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4764 if ((collectBuffer
.length() == 1)
4765 && (collectBuffer
.charAt(0) == '*')) {
4767 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4769 if ((collectBuffer
.length() == 1)
4770 && (collectBuffer
.charAt(0) == '+')) {
4772 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4779 if ((type
== DeviceType
.VT220
)
4780 || (type
== DeviceType
.XTERM
)) {
4782 if ((collectBuffer
.length() == 1)
4783 && (collectBuffer
.charAt(0) == '(')) {
4785 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4787 if ((collectBuffer
.length() == 1)
4788 && (collectBuffer
.charAt(0) == ')')) {
4790 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4792 if ((collectBuffer
.length() == 1)
4793 && (collectBuffer
.charAt(0) == '*')) {
4795 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4797 if ((collectBuffer
.length() == 1)
4798 && (collectBuffer
.charAt(0) == '+')) {
4800 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4805 if ((type
== DeviceType
.VT220
)
4806 || (type
== DeviceType
.XTERM
)) {
4808 if ((collectBuffer
.length() == 1)
4809 && (collectBuffer
.charAt(0) == ' ')) {
4816 if ((type
== DeviceType
.VT220
)
4817 || (type
== DeviceType
.XTERM
)) {
4819 if ((collectBuffer
.length() == 1)
4820 && (collectBuffer
.charAt(0) == ' ')) {
4827 if ((type
== DeviceType
.VT220
)
4828 || (type
== DeviceType
.XTERM
)) {
4830 if ((collectBuffer
.length() == 1)
4831 && (collectBuffer
.charAt(0) == '(')) {
4833 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4835 if ((collectBuffer
.length() == 1)
4836 && (collectBuffer
.charAt(0) == ')')) {
4838 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4840 if ((collectBuffer
.length() == 1)
4841 && (collectBuffer
.charAt(0) == '*')) {
4843 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4845 if ((collectBuffer
.length() == 1)
4846 && (collectBuffer
.charAt(0) == '+')) {
4848 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4856 if ((type
== DeviceType
.VT220
)
4857 || (type
== DeviceType
.XTERM
)) {
4859 if ((collectBuffer
.length() == 1)
4860 && (collectBuffer
.charAt(0) == '(')) {
4862 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4864 if ((collectBuffer
.length() == 1)
4865 && (collectBuffer
.charAt(0) == ')')) {
4867 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4869 if ((collectBuffer
.length() == 1)
4870 && (collectBuffer
.charAt(0) == '*')) {
4872 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4874 if ((collectBuffer
.length() == 1)
4875 && (collectBuffer
.charAt(0) == '+')) {
4877 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4888 if ((type
== DeviceType
.VT220
)
4889 || (type
== DeviceType
.XTERM
)) {
4891 if ((collectBuffer
.length() == 1)
4892 && (collectBuffer
.charAt(0) == '(')) {
4894 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4896 if ((collectBuffer
.length() == 1)
4897 && (collectBuffer
.charAt(0) == ')')) {
4899 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4901 if ((collectBuffer
.length() == 1)
4902 && (collectBuffer
.charAt(0) == '*')) {
4904 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4906 if ((collectBuffer
.length() == 1)
4907 && (collectBuffer
.charAt(0) == '+')) {
4909 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4914 if ((type
== DeviceType
.VT220
)
4915 || (type
== DeviceType
.XTERM
)) {
4917 if ((collectBuffer
.length() == 1)
4918 && (collectBuffer
.charAt(0) == '(')) {
4920 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4922 if ((collectBuffer
.length() == 1)
4923 && (collectBuffer
.charAt(0) == ')')) {
4925 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4927 if ((collectBuffer
.length() == 1)
4928 && (collectBuffer
.charAt(0) == '*')) {
4930 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
4932 if ((collectBuffer
.length() == 1)
4933 && (collectBuffer
.charAt(0) == '+')) {
4935 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
4947 if ((type
== DeviceType
.VT220
)
4948 || (type
== DeviceType
.XTERM
)) {
4950 if ((collectBuffer
.length() == 1)
4951 && (collectBuffer
.charAt(0) == '(')) {
4953 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
4955 if ((collectBuffer
.length() == 1)
4956 && (collectBuffer
.charAt(0) == ')')) {
4958 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
4960 if ((collectBuffer
.length() == 1)
4961 && (collectBuffer
.charAt(0) == '*')) {
4963 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
4965 if ((collectBuffer
.length() == 1)
4966 && (collectBuffer
.charAt(0) == '+')) {
4968 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
4973 if ((type
== DeviceType
.VT220
)
4974 || (type
== DeviceType
.XTERM
)) {
4976 if ((collectBuffer
.length() == 1)
4977 && (collectBuffer
.charAt(0) == '(')) {
4979 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
4981 if ((collectBuffer
.length() == 1)
4982 && (collectBuffer
.charAt(0) == ')')) {
4984 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
4986 if ((collectBuffer
.length() == 1)
4987 && (collectBuffer
.charAt(0) == '*')) {
4989 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
4991 if ((collectBuffer
.length() == 1)
4992 && (collectBuffer
.charAt(0) == '+')) {
4994 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5041 // 0x9C goes to GROUND
5049 // 00-17, 19, 1C-1F --> execute
5051 handleControlChar(ch
);
5054 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5055 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5057 scanState
= ScanState
.CSI_INTERMEDIATE
;
5060 // 30-39, 3B --> param, then switch to CSI_PARAM
5061 if ((ch
>= '0') && (ch
<= '9')) {
5063 scanState
= ScanState
.CSI_PARAM
;
5067 scanState
= ScanState
.CSI_PARAM
;
5070 // 3C-3F --> collect, then switch to CSI_PARAM
5071 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5073 scanState
= ScanState
.CSI_PARAM
;
5076 // 40-7E --> dispatch, then switch to GROUND
5077 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5080 // ICH - Insert character
5088 // CUD - Cursor down
5092 // CUF - Cursor forward
5096 // CUB - Cursor backward
5100 // CNL - Cursor down and to column 1
5101 if (type
== DeviceType
.XTERM
) {
5106 // CPL - Cursor up and to column 1
5107 if (type
== DeviceType
.XTERM
) {
5112 // CHA - Cursor to column # in current row
5113 if (type
== DeviceType
.XTERM
) {
5118 // CUP - Cursor position
5122 // CHT - Cursor forward X tab stops (default 1)
5123 if (type
== DeviceType
.XTERM
) {
5128 // ED - Erase in display
5132 // EL - Erase in line
5147 // DCH - Delete character
5154 // Scroll up X lines (default 1)
5155 if (type
== DeviceType
.XTERM
) {
5160 // Scroll down X lines (default 1)
5161 if (type
== DeviceType
.XTERM
) {
5170 if ((type
== DeviceType
.VT220
)
5171 || (type
== DeviceType
.XTERM
)) {
5173 // ECH - Erase character
5180 // CBT - Cursor backward X tab stops (default 1)
5181 if (type
== DeviceType
.XTERM
) {
5192 // HPA - Cursor to column # in current row. Same as CHA
5193 if (type
== DeviceType
.XTERM
) {
5198 // HPR - Cursor right. Same as CUF
5199 if (type
== DeviceType
.XTERM
) {
5204 // REP - Repeat last char X times
5205 if (type
== DeviceType
.XTERM
) {
5210 // DA - Device attributes
5214 // VPA - Cursor to row, current column.
5215 if (type
== DeviceType
.XTERM
) {
5220 // VPR - Cursor down. Same as CUD
5221 if (type
== DeviceType
.XTERM
) {
5226 // HVP - Horizontal and vertical position
5230 // TBC - Tabulation clear
5234 // Sets an ANSI or DEC private toggle
5238 if ((type
== DeviceType
.VT220
)
5239 || (type
== DeviceType
.XTERM
)) {
5241 // Printer functions
5249 // Sets an ANSI or DEC private toggle
5253 // SGR - Select graphics rendition
5257 // DSR - Device status report
5264 // DECLL - Load leds
5268 // DECSTBM - Set top and bottom margins
5272 // Save cursor (ANSI.SYS)
5273 if (type
== DeviceType
.XTERM
) {
5274 savedState
.cursorX
= currentState
.cursorX
;
5275 savedState
.cursorY
= currentState
.cursorY
;
5281 // Restore cursor (ANSI.SYS)
5282 if (type
== DeviceType
.XTERM
) {
5283 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5290 // DECREQTPARM - Request terminal parameters
5306 // 0x9C goes to GROUND
5311 // 0x3A goes to CSI_IGNORE
5313 scanState
= ScanState
.CSI_IGNORE
;
5318 // 00-17, 19, 1C-1F --> execute
5320 handleControlChar(ch
);
5323 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5324 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5326 scanState
= ScanState
.CSI_INTERMEDIATE
;
5329 // 30-39, 3B --> param
5330 if ((ch
>= '0') && (ch
<= '9')) {
5337 // 0x3A goes to CSI_IGNORE
5339 scanState
= ScanState
.CSI_IGNORE
;
5341 // 0x3C-3F goes to CSI_IGNORE
5342 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5343 scanState
= ScanState
.CSI_IGNORE
;
5346 // 40-7E --> dispatch, then switch to GROUND
5347 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5350 // ICH - Insert character
5358 // CUD - Cursor down
5362 // CUF - Cursor forward
5366 // CUB - Cursor backward
5370 // CNL - Cursor down and to column 1
5371 if (type
== DeviceType
.XTERM
) {
5376 // CPL - Cursor up and to column 1
5377 if (type
== DeviceType
.XTERM
) {
5382 // CHA - Cursor to column # in current row
5383 if (type
== DeviceType
.XTERM
) {
5388 // CUP - Cursor position
5392 // CHT - Cursor forward X tab stops (default 1)
5393 if (type
== DeviceType
.XTERM
) {
5398 // ED - Erase in display
5402 // EL - Erase in line
5417 // DCH - Delete character
5424 // Scroll up X lines (default 1)
5425 if (type
== DeviceType
.XTERM
) {
5430 // Scroll down X lines (default 1)
5431 if (type
== DeviceType
.XTERM
) {
5440 if ((type
== DeviceType
.VT220
)
5441 || (type
== DeviceType
.XTERM
)) {
5443 // ECH - Erase character
5450 // CBT - Cursor backward X tab stops (default 1)
5451 if (type
== DeviceType
.XTERM
) {
5462 // HPA - Cursor to column # in current row. Same as CHA
5463 if (type
== DeviceType
.XTERM
) {
5468 // HPR - Cursor right. Same as CUF
5469 if (type
== DeviceType
.XTERM
) {
5474 // REP - Repeat last char X times
5475 if (type
== DeviceType
.XTERM
) {
5480 // DA - Device attributes
5484 // VPA - Cursor to row, current column.
5485 if (type
== DeviceType
.XTERM
) {
5490 // VPR - Cursor down. Same as CUD
5491 if (type
== DeviceType
.XTERM
) {
5496 // HVP - Horizontal and vertical position
5500 // TBC - Tabulation clear
5504 // Sets an ANSI or DEC private toggle
5508 if ((type
== DeviceType
.VT220
)
5509 || (type
== DeviceType
.XTERM
)) {
5511 // Printer functions
5519 // Sets an ANSI or DEC private toggle
5523 // SGR - Select graphics rendition
5527 // DSR - Device status report
5534 // DECLL - Load leds
5538 // DECSTBM - Set top and bottom margins
5548 // DECREQTPARM - Request terminal parameters
5565 case CSI_INTERMEDIATE
:
5566 // 00-17, 19, 1C-1F --> execute
5568 handleControlChar(ch
);
5571 // 20-2F --> collect
5572 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5576 // 0x30-3F goes to CSI_IGNORE
5577 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5578 scanState
= ScanState
.CSI_IGNORE
;
5581 // 40-7E --> dispatch, then switch to GROUND
5582 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5634 if (((type
== DeviceType
.VT220
)
5635 || (type
== DeviceType
.XTERM
))
5636 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5638 // DECSCL - compatibility level
5641 if ((type
== DeviceType
.XTERM
)
5642 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
5644 // DECSTR - Soft terminal reset
5649 if (((type
== DeviceType
.VT220
)
5650 || (type
== DeviceType
.XTERM
))
5651 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5679 // 00-17, 19, 1C-1F --> execute
5681 handleControlChar(ch
);
5684 // 20-2F --> collect
5685 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5689 // 40-7E --> ignore, then switch to GROUND
5690 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5694 // 20-3F, 7F --> ignore
5700 // 0x9C goes to GROUND
5705 // 0x1B 0x5C goes to GROUND
5710 if ((collectBuffer
.length() > 0)
5711 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5717 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5718 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5720 scanState
= ScanState
.DCS_INTERMEDIATE
;
5723 // 30-39, 3B --> param, then switch to DCS_PARAM
5724 if ((ch
>= '0') && (ch
<= '9')) {
5726 scanState
= ScanState
.DCS_PARAM
;
5730 scanState
= ScanState
.DCS_PARAM
;
5733 // 3C-3F --> collect, then switch to DCS_PARAM
5734 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5736 scanState
= ScanState
.DCS_PARAM
;
5739 // 00-17, 19, 1C-1F, 7F --> ignore
5741 // 0x3A goes to DCS_IGNORE
5743 scanState
= ScanState
.DCS_IGNORE
;
5746 // 0x40-7E goes to DCS_PASSTHROUGH
5747 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5748 scanState
= ScanState
.DCS_PASSTHROUGH
;
5752 case DCS_INTERMEDIATE
:
5754 // 0x9C goes to GROUND
5759 // 0x1B 0x5C goes to GROUND
5764 if ((collectBuffer
.length() > 0)
5765 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5771 // 0x30-3F goes to DCS_IGNORE
5772 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5773 scanState
= ScanState
.DCS_IGNORE
;
5776 // 0x40-7E goes to DCS_PASSTHROUGH
5777 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5778 scanState
= ScanState
.DCS_PASSTHROUGH
;
5781 // 00-17, 19, 1C-1F, 7F --> ignore
5786 // 0x9C goes to GROUND
5791 // 0x1B 0x5C goes to GROUND
5796 if ((collectBuffer
.length() > 0)
5797 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5803 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5804 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5806 scanState
= ScanState
.DCS_INTERMEDIATE
;
5809 // 30-39, 3B --> param
5810 if ((ch
>= '0') && (ch
<= '9')) {
5817 // 00-17, 19, 1C-1F, 7F --> ignore
5819 // 0x3A, 3C-3F goes to DCS_IGNORE
5821 scanState
= ScanState
.DCS_IGNORE
;
5823 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5824 scanState
= ScanState
.DCS_IGNORE
;
5827 // 0x40-7E goes to DCS_PASSTHROUGH
5828 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5829 scanState
= ScanState
.DCS_PASSTHROUGH
;
5833 case DCS_PASSTHROUGH
:
5834 // 0x9C goes to GROUND
5839 // 0x1B 0x5C goes to GROUND
5844 if ((collectBuffer
.length() > 0)
5845 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5851 // 00-17, 19, 1C-1F, 20-7E --> put
5859 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5862 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5871 // 00-17, 19, 1C-1F, 20-7F --> ignore
5873 // 0x9C goes to GROUND
5880 case SOSPMAPC_STRING
:
5881 // 00-17, 19, 1C-1F, 20-7F --> ignore
5883 // 0x9C goes to GROUND
5891 // Special case for Xterm: OSC can pass control characters
5892 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5896 // 00-17, 19, 1C-1F --> ignore
5898 // 20-7F --> osc_put
5899 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5903 // 0x9C goes to GROUND
5910 case VT52_DIRECT_CURSOR_ADDRESS
:
5911 // This is a special case for the VT52 sequence "ESC Y l c"
5912 if (collectBuffer
.length() == 0) {
5914 } else if (collectBuffer
.length() == 1) {
5915 // We've got the two characters, one in the buffer and the
5917 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
5926 * Expose current cursor X to outside world.
5928 * @return current cursor X
5930 public final int getCursorX() {
5931 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
5932 return currentState
.cursorX
* 2;
5934 return currentState
.cursorX
;
5938 * Expose current cursor Y to outside world.
5940 * @return current cursor Y
5942 public final int getCursorY() {
5943 return currentState
.cursorY
;
5947 * Read function runs on a separate thread.
5949 public final void run() {
5950 boolean utf8
= false;
5951 boolean done
= false;
5953 if (type
== DeviceType
.XTERM
) {
5957 // available() will often return > 1, so we need to read in chunks to
5959 char [] readBufferUTF8
= null;
5960 byte [] readBuffer
= null;
5962 readBufferUTF8
= new char[128];
5964 readBuffer
= new byte[128];
5967 while (!done
&& !stopReaderThread
) {
5969 int n
= inputStream
.available();
5971 // System.err.printf("available() %d\n", n); System.err.flush();
5973 if (readBufferUTF8
.length
< n
) {
5974 // The buffer wasn't big enough, make it huger
5975 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
5978 readBufferUTF8
= new char[newSizeHalf
* 2];
5981 if (readBuffer
.length
< n
) {
5982 // The buffer wasn't big enough, make it huger
5983 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
5984 readBuffer
= new byte[newSizeHalf
* 2];
5990 } catch (InterruptedException e
) {
5999 rc
= input
.read(readBufferUTF8
, 0,
6000 readBufferUTF8
.length
);
6002 rc
= inputStream
.read(readBuffer
, 0,
6005 } catch (ReadTimeoutException e
) {
6009 // System.err.printf("read() %d\n", rc); System.err.flush();
6014 for (int i
= 0; i
< rc
; i
++) {
6017 ch
= readBufferUTF8
[i
];
6022 synchronized (this) {
6023 // Don't step on UI events
6028 // System.err.println("end while loop"); System.err.flush();
6029 } catch (IOException e
) {
6030 e
.printStackTrace();
6034 } // while ((done == false) && (stopReaderThread == false))
6036 // Let the rest of the world know that I am done.
6037 stopReaderThread
= true;
6040 inputStream
.cancelRead();
6041 inputStream
.close();
6043 } catch (IOException e
) {
6049 } catch (IOException e
) {
6053 // System.err.println("*** run() exiting..."); System.err.flush();