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 * The enclosing listening object.
308 private DisplayListener listener
;
311 * Set a listening object.
313 * @param listener the object that will have displayChanged() called
314 * after bytes are received from the remote terminal
316 public void setListener(final DisplayListener listener
) {
317 this.listener
= listener
;
321 * When true, the reader thread is expected to exit.
323 private volatile boolean stopReaderThread
= false;
328 private Thread readerThread
= null;
331 * See if the reader thread is still running.
333 * @return if true, we are still connected to / reading from the remote
336 public final boolean isReading() {
337 return (!stopReaderThread
);
341 * The type of emulator to be.
343 private DeviceType type
= DeviceType
.VT102
;
346 * Obtain a new blank display line for an external user
347 * (e.g. TTerminalWindow).
349 * @return new blank line
351 public final DisplayLine
getBlankDisplayLine() {
352 return new DisplayLine(currentState
.attr
);
356 * The scrollback buffer characters + attributes.
358 private volatile List
<DisplayLine
> scrollback
;
361 * Get the scrollback buffer.
363 * @return the scrollback buffer
365 public final List
<DisplayLine
> getScrollbackBuffer() {
370 * The raw display buffer characters + attributes.
372 private volatile List
<DisplayLine
> display
;
375 * Get the display buffer.
377 * @return the display buffer
379 public final List
<DisplayLine
> getDisplayBuffer() {
384 * The terminal's input. For type == XTERM, this is an InputStreamReader
385 * with UTF-8 encoding.
387 private Reader input
;
390 * The terminal's raw InputStream. This is used for type != XTERM.
392 private volatile TimeoutInputStream inputStream
;
395 * The terminal's output. For type == XTERM, this wraps an
396 * OutputStreamWriter with UTF-8 encoding.
398 private Writer output
;
401 * The terminal's raw OutputStream. This is used for type != XTERM.
403 private OutputStream outputStream
;
406 * Parser character scan states.
423 VT52_DIRECT_CURSOR_ADDRESS
427 * Current scanning state.
429 private ScanState scanState
;
432 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
433 * can't really use it in keypress() because we do not see number pad
434 * events from TKeypress.
436 private enum KeypadMode
{
442 * Arrow keys can emit three different sequences (DECCKM or VT52
445 private enum ArrowKeyMode
{
452 * Available character sets for GL, GR, G0, G1, G2, G3.
454 private enum CharacterSet
{
475 * Single-shift states used by the C1 control characters SS2 (0x8E) and
478 private enum Singleshift
{
485 * VT220+ lockshift states.
487 private enum LockshiftMode
{
497 * XTERM mouse reporting protocols.
499 private enum MouseProtocol
{
508 * Which mouse protocol is active.
510 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
513 * XTERM mouse reporting encodings.
515 private enum MouseEncoding
{
522 * Which mouse encoding is active.
524 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
527 * Physical display width. We start at 80x24, but the user can resize us
533 * Get the display width.
535 * @return the width (usually 80 or 132)
537 public final int getWidth() {
542 * Physical display height. We start at 80x24, but the user can resize
548 * Get the display height.
550 * @return the height (usually 24)
552 public final int getHeight() {
557 * Top margin of the scrolling region.
559 private int scrollRegionTop
;
562 * Bottom margin of the scrolling region.
564 private int scrollRegionBottom
;
567 * Right margin column number. This can be selected by the remote side
568 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
570 private int rightMargin
;
573 * Last character printed.
578 * VT100-style line wrapping: a character is placed in column 80 (or
579 * 132), but the line does NOT wrap until another character is written to
580 * column 1 of the next line, after which the cursor moves to column 2.
582 private boolean wrapLineFlag
;
585 * VT220 single shift flag.
587 private Singleshift singleshift
= Singleshift
.NONE
;
590 * true = insert characters, false = overwrite.
592 private boolean insertMode
= false;
595 * VT52 mode as selected by DECANM. True means VT52, false means
596 * ANSI. Default is ANSI.
598 private boolean vt52Mode
= false;
601 * Visible cursor (DECTCEM).
603 private boolean cursorVisible
= true;
606 * Get visible cursor flag.
608 * @return if true, the cursor is visible
610 public final boolean isCursorVisible() {
611 return cursorVisible
;
615 * Screen title as set by the xterm OSC sequence. Lots of applications
616 * send a screenTitle regardless of whether it is an xterm client or not.
618 private String screenTitle
= "";
621 * Get the screen title as set by the xterm OSC sequence. Lots of
622 * applications send a screenTitle regardless of whether it is an xterm
625 * @return screen title
627 public final String
getScreenTitle() {
632 * Parameter characters being collected.
634 private List
<Integer
> csiParams
;
637 * Non-csi collect buffer.
639 private StringBuilder collectBuffer
;
642 * When true, use the G1 character set.
644 private boolean shiftOut
= false;
647 * Horizontal tab stop locations.
649 private List
<Integer
> tabStops
;
652 * S8C1T. True means 8bit controls, false means 7bit controls.
654 private boolean s8c1t
= false;
657 * Printer mode. True means send all output to printer, which discards
660 private boolean printerControllerMode
= false;
663 * LMN line mode. If true, linefeed() puts the cursor on the first
664 * column of the next line. If false, linefeed() puts the cursor one
665 * line down on the current line. The default is false.
667 private boolean newLineMode
= false;
670 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
672 private ArrowKeyMode arrowKeyMode
;
675 * Whether number pad keys send VT100 or VT52, application or numeric
678 @SuppressWarnings("unused")
679 private KeypadMode keypadMode
;
682 * When true, the terminal is in 132-column mode (DECCOLM).
684 private boolean columns132
= false;
687 * Get 132 columns value.
689 * @return if true, the terminal is in 132 column mode
691 public final boolean isColumns132() {
696 * true = reverse video. Set by DECSCNM.
698 private boolean reverseVideo
= false;
701 * false = echo characters locally.
703 private boolean fullDuplex
= true;
706 * DECSC/DECRC save/restore a subset of the total state. This class
707 * encapsulates those specific flags/modes.
709 private class SaveableState
{
712 * When true, cursor positions are relative to the scrolling region.
714 public boolean originMode
= false;
717 * The current editing X position.
719 public int cursorX
= 0;
722 * The current editing Y position.
724 public int cursorY
= 0;
727 * Which character set is currently selected in G0.
729 public CharacterSet g0Charset
= CharacterSet
.US
;
732 * Which character set is currently selected in G1.
734 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
737 * Which character set is currently selected in G2.
739 public CharacterSet g2Charset
= CharacterSet
.US
;
742 * Which character set is currently selected in G3.
744 public CharacterSet g3Charset
= CharacterSet
.US
;
747 * Which character set is currently selected in GR.
749 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
752 * The current drawing attributes.
754 public CellAttributes attr
;
759 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
764 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
769 public boolean lineWrap
= true;
774 public void reset() {
778 g0Charset
= CharacterSet
.US
;
779 g1Charset
= CharacterSet
.DRAWING
;
780 g2Charset
= CharacterSet
.US
;
781 g3Charset
= CharacterSet
.US
;
782 grCharset
= CharacterSet
.DRAWING
;
783 attr
= new CellAttributes();
784 glLockshift
= LockshiftMode
.NONE
;
785 grLockshift
= LockshiftMode
.NONE
;
790 * Copy attributes from another instance.
792 * @param that the other instance to match
794 public void setTo(final SaveableState that
) {
795 this.originMode
= that
.originMode
;
796 this.cursorX
= that
.cursorX
;
797 this.cursorY
= that
.cursorY
;
798 this.g0Charset
= that
.g0Charset
;
799 this.g1Charset
= that
.g1Charset
;
800 this.g2Charset
= that
.g2Charset
;
801 this.g3Charset
= that
.g3Charset
;
802 this.grCharset
= that
.grCharset
;
803 this.attr
= new CellAttributes();
804 this.attr
.setTo(that
.attr
);
805 this.glLockshift
= that
.glLockshift
;
806 this.grLockshift
= that
.grLockshift
;
807 this.lineWrap
= that
.lineWrap
;
811 * Public constructor.
813 public SaveableState() {
819 * The current terminal state.
821 private SaveableState currentState
;
824 * The last saved terminal state.
826 private SaveableState savedState
;
829 * Clear the CSI parameters and flags.
831 private void toGround() {
833 collectBuffer
= new StringBuilder(8);
834 scanState
= ScanState
.GROUND
;
838 * Reset the tab stops list.
840 private void resetTabStops() {
842 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
843 tabStops
.add(new Integer(i
* 8));
848 * Reset the emulation state.
850 private void reset() {
852 currentState
= new SaveableState();
853 savedState
= new SaveableState();
854 scanState
= ScanState
.GROUND
;
858 scrollRegionBottom
= height
- 1;
859 rightMargin
= width
- 1;
861 arrowKeyMode
= ArrowKeyMode
.ANSI
;
862 keypadMode
= KeypadMode
.Numeric
;
863 wrapLineFlag
= false;
871 reverseVideo
= false;
873 cursorVisible
= true;
876 singleshift
= Singleshift
.NONE
;
878 printerControllerMode
= false;
881 mouseProtocol
= MouseProtocol
.OFF
;
882 mouseEncoding
= MouseEncoding
.X10
;
892 * Public constructor.
894 * @param type one of the DeviceType constants to select VT100, VT102,
896 * @param inputStream an InputStream connected to the remote side. For
897 * type == XTERM, inputStream is converted to a Reader with UTF-8
899 * @param outputStream an OutputStream connected to the remote user. For
900 * type == XTERM, outputStream is converted to a Writer with UTF-8
902 * @throws UnsupportedEncodingException if an exception is thrown when
903 * creating the InputStreamReader
905 public ECMA48(final DeviceType type
, final InputStream inputStream
,
906 final OutputStream outputStream
) throws UnsupportedEncodingException
{
908 assert (inputStream
!= null);
909 assert (outputStream
!= null);
911 csiParams
= new ArrayList
<Integer
>();
912 tabStops
= new ArrayList
<Integer
>();
913 scrollback
= new LinkedList
<DisplayLine
>();
914 display
= new LinkedList
<DisplayLine
>();
917 if (inputStream
instanceof TimeoutInputStream
) {
918 this.inputStream
= (TimeoutInputStream
)inputStream
;
920 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
922 if (type
== DeviceType
.XTERM
) {
923 this.input
= new InputStreamReader(this.inputStream
, "UTF-8");
924 this.output
= new OutputStreamWriter(new
925 BufferedOutputStream(outputStream
), "UTF-8");
926 this.outputStream
= null;
929 this.outputStream
= new BufferedOutputStream(outputStream
);
933 for (int i
= 0; i
< height
; i
++) {
934 display
.add(new DisplayLine(currentState
.attr
));
937 // Spin up the input reader
938 readerThread
= new Thread(this);
939 readerThread
.start();
943 * Append a new line to the bottom of the display, adding lines off the
944 * top to the scrollback buffer.
946 private void newDisplayLine() {
947 // Scroll the top line off into the scrollback buffer
948 scrollback
.add(display
.get(0));
950 DisplayLine line
= new DisplayLine(currentState
.attr
);
951 line
.setReverseColor(reverseVideo
);
956 * Wraps the current line.
958 private void wrapCurrentLine() {
959 if (currentState
.cursorY
== height
- 1) {
962 if (currentState
.cursorY
< height
- 1) {
963 currentState
.cursorY
++;
965 currentState
.cursorX
= 0;
969 * Handle a carriage return.
971 private void carriageReturn() {
972 currentState
.cursorX
= 0;
973 wrapLineFlag
= false;
977 * Reverse the color of the visible display.
979 private void invertDisplayColors() {
980 for (DisplayLine line
: display
) {
981 line
.setReverseColor(!line
.isReverseColor());
988 private void linefeed() {
990 if (currentState
.cursorY
< scrollRegionBottom
) {
991 // Increment screen y
992 currentState
.cursorY
++;
995 // Screen y does not increment
998 * Two cases: either we're inside a scrolling region or not. If
999 * the scrolling region bottom is the bottom of the screen, then
1000 * push the top line into the buffer. Else scroll the scrolling
1003 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1005 // We're at the bottom of the scroll region, AND the scroll
1006 // region is the entire screen.
1012 // We're at the bottom of the scroll region, AND the scroll
1013 // region is NOT the entire screen.
1014 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1019 currentState
.cursorX
= 0;
1021 wrapLineFlag
= false;
1025 * Prints one character to the display buffer.
1027 * @param ch character to display
1029 private void printCharacter(final char ch
) {
1030 int rightMargin
= this.rightMargin
;
1032 // Check if we have double-width, and if so chop at 40/66 instead of
1034 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1035 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1038 // Check the unusually-complicated line wrapping conditions...
1039 if (currentState
.cursorX
== rightMargin
) {
1041 if (currentState
.lineWrap
== true) {
1043 * This case happens when: the cursor was already on the
1044 * right margin (either through printing or by an explicit
1045 * placement command), and a character was printed.
1047 * The line wraps only when a new character arrives AND the
1048 * cursor is already on the right margin AND has placed a
1049 * character in its cell. Easier to see than to explain.
1051 if (wrapLineFlag
== false) {
1053 * This block marks the case that we are in the margin
1054 * and the first character has been received and printed.
1056 wrapLineFlag
= true;
1059 * This block marks the case that we are in the margin
1060 * and the second character has been received and
1063 wrapLineFlag
= false;
1067 } else if (currentState
.cursorX
<= rightMargin
) {
1069 * This is the normal case: a character came in and was printed
1070 * to the left of the right margin column.
1073 // Turn off VT100 special-case flag
1074 wrapLineFlag
= false;
1077 // "Print" the character
1078 Cell newCell
= new Cell(ch
);
1079 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1080 newCellAttributes
.setTo(currentState
.attr
);
1081 DisplayLine line
= display
.get(currentState
.cursorY
);
1082 // Insert mode special case
1083 if (insertMode
== true) {
1084 line
.insert(currentState
.cursorX
, newCell
);
1086 // Replace an existing character
1087 line
.replace(currentState
.cursorX
, newCell
);
1090 // Increment horizontal
1091 if (wrapLineFlag
== false) {
1092 currentState
.cursorX
++;
1093 if (currentState
.cursorX
> rightMargin
) {
1094 currentState
.cursorX
--;
1100 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1101 * send to the remote side.
1103 * @param mouse mouse event received from the local user
1105 public void mouse(final TMouseEvent mouse
) {
1108 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1109 mouseProtocol, mouseEncoding, mouse);
1112 if (mouseEncoding
== MouseEncoding
.X10
) {
1113 // We will support X10 but only for (160,94) and smaller.
1114 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1119 switch (mouseProtocol
) {
1126 // Only report button presses
1127 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1133 // Only report button presses and releases
1134 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1135 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1143 * Only report button presses, button releases, and motions that
1144 * have a button down (i.e. drag-and-drop).
1146 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1147 if (!mouse
.isMouse1()
1148 && !mouse
.isMouse2()
1149 && !mouse
.isMouse3()
1150 && !mouse
.isMouseWheelUp()
1151 && !mouse
.isMouseWheelDown()
1159 // Report everything
1163 // Now encode the event
1164 StringBuilder sb
= new StringBuilder(6);
1165 if (mouseEncoding
== MouseEncoding
.SGR
) {
1166 sb
.append((char) 0x1B);
1169 if (mouse
.isMouse1()) {
1170 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1175 } else if (mouse
.isMouse2()) {
1176 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1181 } else if (mouse
.isMouse3()) {
1182 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1187 } else if (mouse
.isMouseWheelUp()) {
1189 } else if (mouse
.isMouseWheelDown()) {
1192 // This is motion with no buttons down.
1196 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1199 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1206 // X10 and UTF8 encodings
1207 sb
.append((char) 0x1B);
1210 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1211 sb
.append((char) (0x03 + 32));
1212 } else if (mouse
.isMouse1()) {
1213 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1214 sb
.append((char) (0x00 + 32 + 32));
1216 sb
.append((char) (0x00 + 32));
1218 } else if (mouse
.isMouse2()) {
1219 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1220 sb
.append((char) (0x01 + 32 + 32));
1222 sb
.append((char) (0x01 + 32));
1224 } else if (mouse
.isMouse3()) {
1225 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1226 sb
.append((char) (0x02 + 32 + 32));
1228 sb
.append((char) (0x02 + 32));
1230 } else if (mouse
.isMouseWheelUp()) {
1231 sb
.append((char) (0x04 + 64));
1232 } else if (mouse
.isMouseWheelDown()) {
1233 sb
.append((char) (0x05 + 64));
1235 // This is motion with no buttons down.
1236 sb
.append((char) (0x03 + 32));
1239 sb
.append((char) (mouse
.getX() + 33));
1240 sb
.append((char) (mouse
.getY() + 33));
1243 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1244 writeRemote(sb
.toString());
1248 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1249 * send to the remote side.
1251 * @param keypress keypress received from the local user
1253 public void keypress(final TKeypress keypress
) {
1254 writeRemote(keypressToString(keypress
));
1258 * Build one of the complex xterm keystroke sequences, storing the result in
1259 * xterm_keystroke_buffer.
1261 * @param ss3 the prefix to use based on VT100 state.
1262 * @param first the first character, usually a number.
1263 * @param first the last character, one of the following: ~ A B C D F H
1264 * @param ctrl whether or not ctrl is down
1265 * @param alt whether or not alt is down
1266 * @param shift whether or not shift is down
1267 * @return the buffer with the full key sequence
1269 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1270 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1272 StringBuilder sb
= new StringBuilder(ss3
);
1273 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1277 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1279 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1281 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1283 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1285 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1287 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1289 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1294 return sb
.toString();
1298 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1300 * @param keypress keypress received from the local user
1301 * @return string to transmit to the remote side
1303 private String
keypressToString(final TKeypress keypress
) {
1305 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1307 * If this is a control character, process it like it came from
1310 if (keypress
.getChar() < 0x20) {
1311 handleControlChar(keypress
.getChar());
1313 // Local echo for everything else
1314 printCharacter(keypress
.getChar());
1318 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1323 // Handle control characters
1324 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1325 StringBuilder sb
= new StringBuilder();
1326 char ch
= keypress
.getChar();
1329 return sb
.toString();
1332 // Handle alt characters
1333 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1334 StringBuilder sb
= new StringBuilder("\033");
1335 char ch
= keypress
.getChar();
1337 return sb
.toString();
1340 if (keypress
.equals(kbBackspace
)) {
1353 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1356 switch (arrowKeyMode
) {
1358 return xtermBuildKeySequence("\033[", '1', 'D',
1359 keypress
.isCtrl(), keypress
.isAlt(),
1360 keypress
.isShift());
1362 return xtermBuildKeySequence("\033", '1', 'D',
1363 keypress
.isCtrl(), keypress
.isAlt(),
1364 keypress
.isShift());
1366 return xtermBuildKeySequence("\033O", '1', 'D',
1367 keypress
.isCtrl(), keypress
.isAlt(),
1368 keypress
.isShift());
1371 switch (arrowKeyMode
) {
1382 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1385 switch (arrowKeyMode
) {
1387 return xtermBuildKeySequence("\033[", '1', 'C',
1388 keypress
.isCtrl(), keypress
.isAlt(),
1389 keypress
.isShift());
1391 return xtermBuildKeySequence("\033", '1', 'C',
1392 keypress
.isCtrl(), keypress
.isAlt(),
1393 keypress
.isShift());
1395 return xtermBuildKeySequence("\033O", '1', 'C',
1396 keypress
.isCtrl(), keypress
.isAlt(),
1397 keypress
.isShift());
1400 switch (arrowKeyMode
) {
1411 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1414 switch (arrowKeyMode
) {
1416 return xtermBuildKeySequence("\033[", '1', 'A',
1417 keypress
.isCtrl(), keypress
.isAlt(),
1418 keypress
.isShift());
1420 return xtermBuildKeySequence("\033", '1', 'A',
1421 keypress
.isCtrl(), keypress
.isAlt(),
1422 keypress
.isShift());
1424 return xtermBuildKeySequence("\033O", '1', 'A',
1425 keypress
.isCtrl(), keypress
.isAlt(),
1426 keypress
.isShift());
1429 switch (arrowKeyMode
) {
1440 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1443 switch (arrowKeyMode
) {
1445 return xtermBuildKeySequence("\033[", '1', 'B',
1446 keypress
.isCtrl(), keypress
.isAlt(),
1447 keypress
.isShift());
1449 return xtermBuildKeySequence("\033", '1', 'B',
1450 keypress
.isCtrl(), keypress
.isAlt(),
1451 keypress
.isShift());
1453 return xtermBuildKeySequence("\033O", '1', 'B',
1454 keypress
.isCtrl(), keypress
.isAlt(),
1455 keypress
.isShift());
1458 switch (arrowKeyMode
) {
1469 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1472 switch (arrowKeyMode
) {
1474 return xtermBuildKeySequence("\033[", '1', 'H',
1475 keypress
.isCtrl(), keypress
.isAlt(),
1476 keypress
.isShift());
1478 return xtermBuildKeySequence("\033", '1', 'H',
1479 keypress
.isCtrl(), keypress
.isAlt(),
1480 keypress
.isShift());
1482 return xtermBuildKeySequence("\033O", '1', 'H',
1483 keypress
.isCtrl(), keypress
.isAlt(),
1484 keypress
.isShift());
1487 switch (arrowKeyMode
) {
1498 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1501 switch (arrowKeyMode
) {
1503 return xtermBuildKeySequence("\033[", '1', 'F',
1504 keypress
.isCtrl(), keypress
.isAlt(),
1505 keypress
.isShift());
1507 return xtermBuildKeySequence("\033", '1', 'F',
1508 keypress
.isCtrl(), keypress
.isAlt(),
1509 keypress
.isShift());
1511 return xtermBuildKeySequence("\033O", '1', 'F',
1512 keypress
.isCtrl(), keypress
.isAlt(),
1513 keypress
.isShift());
1516 switch (arrowKeyMode
) {
1527 if (keypress
.equals(kbF1
)) {
1535 if (keypress
.equals(kbF2
)) {
1543 if (keypress
.equals(kbF3
)) {
1551 if (keypress
.equals(kbF4
)) {
1559 if (keypress
.equals(kbF5
)) {
1572 if (keypress
.equals(kbF6
)) {
1585 if (keypress
.equals(kbF7
)) {
1598 if (keypress
.equals(kbF8
)) {
1611 if (keypress
.equals(kbF9
)) {
1624 if (keypress
.equals(kbF10
)) {
1637 if (keypress
.equals(kbF11
)) {
1641 if (keypress
.equals(kbF12
)) {
1645 if (keypress
.equals(kbShiftF1
)) {
1650 if (type
== DeviceType
.XTERM
) {
1656 if (keypress
.equals(kbShiftF2
)) {
1661 if (type
== DeviceType
.XTERM
) {
1667 if (keypress
.equals(kbShiftF3
)) {
1672 if (type
== DeviceType
.XTERM
) {
1678 if (keypress
.equals(kbShiftF4
)) {
1683 if (type
== DeviceType
.XTERM
) {
1689 if (keypress
.equals(kbShiftF5
)) {
1691 return "\033[15;2~";
1694 if (keypress
.equals(kbShiftF6
)) {
1696 return "\033[17;2~";
1699 if (keypress
.equals(kbShiftF7
)) {
1701 return "\033[18;2~";
1704 if (keypress
.equals(kbShiftF8
)) {
1706 return "\033[19;2~";
1709 if (keypress
.equals(kbShiftF9
)) {
1711 return "\033[20;2~";
1714 if (keypress
.equals(kbShiftF10
)) {
1716 return "\033[21;2~";
1719 if (keypress
.equals(kbShiftF11
)) {
1721 return "\033[23;2~";
1724 if (keypress
.equals(kbShiftF12
)) {
1726 return "\033[24;2~";
1729 if (keypress
.equals(kbCtrlF1
)) {
1734 if (type
== DeviceType
.XTERM
) {
1740 if (keypress
.equals(kbCtrlF2
)) {
1745 if (type
== DeviceType
.XTERM
) {
1751 if (keypress
.equals(kbCtrlF3
)) {
1756 if (type
== DeviceType
.XTERM
) {
1762 if (keypress
.equals(kbCtrlF4
)) {
1767 if (type
== DeviceType
.XTERM
) {
1773 if (keypress
.equals(kbCtrlF5
)) {
1775 return "\033[15;5~";
1778 if (keypress
.equals(kbCtrlF6
)) {
1780 return "\033[17;5~";
1783 if (keypress
.equals(kbCtrlF7
)) {
1785 return "\033[18;5~";
1788 if (keypress
.equals(kbCtrlF8
)) {
1790 return "\033[19;5~";
1793 if (keypress
.equals(kbCtrlF9
)) {
1795 return "\033[20;5~";
1798 if (keypress
.equals(kbCtrlF10
)) {
1800 return "\033[21;5~";
1803 if (keypress
.equals(kbCtrlF11
)) {
1805 return "\033[23;5~";
1808 if (keypress
.equals(kbCtrlF12
)) {
1810 return "\033[24;5~";
1813 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
1816 return xtermBuildKeySequence("\033[", '5', '~',
1817 keypress
.isCtrl(), keypress
.isAlt(),
1818 keypress
.isShift());
1824 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
1827 return xtermBuildKeySequence("\033[", '6', '~',
1828 keypress
.isCtrl(), keypress
.isAlt(),
1829 keypress
.isShift());
1835 if (keypress
.equalsWithoutModifiers(kbIns
)) {
1838 return xtermBuildKeySequence("\033[", '2', '~',
1839 keypress
.isCtrl(), keypress
.isAlt(),
1840 keypress
.isShift());
1846 if (keypress
.equalsWithoutModifiers(kbDel
)) {
1849 return xtermBuildKeySequence("\033[", '3', '~',
1850 keypress
.isCtrl(), keypress
.isAlt(),
1851 keypress
.isShift());
1853 // Delete sends real delete for VTxxx
1858 if (keypress
.equals(kbEnter
)) {
1862 if (keypress
.equals(kbEsc
)) {
1866 if (keypress
.equals(kbAltEsc
)) {
1870 if (keypress
.equals(kbTab
)) {
1874 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
1875 (keypress
.equals(kbShiftTab
))
1885 // Non-alt, non-ctrl characters
1886 if (!keypress
.isFnKey()) {
1887 StringBuilder sb
= new StringBuilder();
1888 sb
.append(keypress
.getChar());
1889 return sb
.toString();
1895 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1898 * @param ch 8-bit character from the remote side
1899 * @param charsetGl character set defined for GL
1900 * @param charsetGr character set defined for GR
1901 * @return character to display on the screen
1903 private char mapCharacterCharset(final char ch
,
1904 final CharacterSet charsetGl
,
1905 final CharacterSet charsetGr
) {
1907 int lookupChar
= ch
;
1908 CharacterSet lookupCharset
= charsetGl
;
1911 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
1912 lookupCharset
= charsetGr
;
1916 switch (lookupCharset
) {
1919 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
1922 return DECCharacterSets
.UK
[lookupChar
];
1925 return DECCharacterSets
.US_ASCII
[lookupChar
];
1928 return DECCharacterSets
.NL
[lookupChar
];
1931 return DECCharacterSets
.FI
[lookupChar
];
1934 return DECCharacterSets
.FR
[lookupChar
];
1937 return DECCharacterSets
.FR_CA
[lookupChar
];
1940 return DECCharacterSets
.DE
[lookupChar
];
1943 return DECCharacterSets
.IT
[lookupChar
];
1946 return DECCharacterSets
.NO
[lookupChar
];
1949 return DECCharacterSets
.ES
[lookupChar
];
1952 return DECCharacterSets
.SV
[lookupChar
];
1955 return DECCharacterSets
.SWISS
[lookupChar
];
1957 case DEC_SUPPLEMENTAL
:
1958 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
1961 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
1964 return DECCharacterSets
.US_ASCII
[lookupChar
];
1967 return DECCharacterSets
.US_ASCII
[lookupChar
];
1970 throw new IllegalArgumentException("Invalid character set value: "
1976 * Map an 8-bit byte into a printable character.
1978 * @param ch either 8-bit or Unicode character from the remote side
1979 * @return character to display on the screen
1981 private char mapCharacter(final char ch
) {
1983 // Unicode character, just return it
1987 CharacterSet charsetGl
= currentState
.g0Charset
;
1988 CharacterSet charsetGr
= currentState
.grCharset
;
1990 if (vt52Mode
== true) {
1991 if (shiftOut
== true) {
1992 // Shifted out character, pull from VT52 graphics
1993 charsetGl
= currentState
.g1Charset
;
1994 charsetGr
= CharacterSet
.US
;
1997 charsetGl
= currentState
.g0Charset
;
1998 charsetGr
= CharacterSet
.US
;
2001 // Pull the character
2002 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2006 if (shiftOut
== true) {
2007 // Shifted out character, pull from G1
2008 charsetGl
= currentState
.g1Charset
;
2009 charsetGr
= currentState
.grCharset
;
2011 // Pull the character
2012 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2016 if (singleshift
== Singleshift
.SS2
) {
2018 singleshift
= Singleshift
.NONE
;
2020 // Shifted out character, pull from G2
2021 charsetGl
= currentState
.g2Charset
;
2022 charsetGr
= currentState
.grCharset
;
2026 if (singleshift
== Singleshift
.SS3
) {
2028 singleshift
= Singleshift
.NONE
;
2030 // Shifted out character, pull from G3
2031 charsetGl
= currentState
.g3Charset
;
2032 charsetGr
= currentState
.grCharset
;
2035 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2036 // Check for locking shift
2038 switch (currentState
.glLockshift
) {
2051 charsetGl
= currentState
.g2Charset
;
2056 charsetGl
= currentState
.g3Charset
;
2061 charsetGl
= currentState
.g0Charset
;
2065 switch (currentState
.grLockshift
) {
2075 charsetGr
= currentState
.g1Charset
;
2080 charsetGr
= currentState
.g2Charset
;
2085 charsetGr
= currentState
.g3Charset
;
2090 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2097 // Pull the character
2098 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2102 * Scroll the text within a scrolling region up n lines.
2104 * @param regionTop top row of the scrolling region
2105 * @param regionBottom bottom row of the scrolling region
2106 * @param n number of lines to scroll
2108 private void scrollingRegionScrollUp(final int regionTop
,
2109 final int regionBottom
, final int n
) {
2111 if (regionTop
>= regionBottom
) {
2115 // Sanity check: see if there will be any characters left after the
2117 if (regionBottom
+ 1 - regionTop
<= n
) {
2118 // There won't be anything left in the region, so just call
2119 // eraseScreen() and return.
2120 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2124 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2125 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2126 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2128 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2129 - remaining
, regionBottom
+ 1);
2130 display
= new LinkedList
<DisplayLine
>(displayTop
);
2131 display
.addAll(displayMiddle
);
2132 for (int i
= 0; i
< n
; i
++) {
2133 DisplayLine line
= new DisplayLine(currentState
.attr
);
2134 line
.setReverseColor(reverseVideo
);
2137 display
.addAll(displayBottom
);
2139 assert (display
.size() == height
);
2143 * Scroll the text within a scrolling region down n lines.
2145 * @param regionTop top row of the scrolling region
2146 * @param regionBottom bottom row of the scrolling region
2147 * @param n number of lines to scroll
2149 private void scrollingRegionScrollDown(final int regionTop
,
2150 final int regionBottom
, final int n
) {
2152 if (regionTop
>= regionBottom
) {
2156 // Sanity check: see if there will be any characters left after the
2158 if (regionBottom
+ 1 - regionTop
<= n
) {
2159 // There won't be anything left in the region, so just call
2160 // eraseScreen() and return.
2161 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2165 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2166 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2167 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2169 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2170 regionTop
+ remaining
);
2171 display
= new LinkedList
<DisplayLine
>(displayTop
);
2172 for (int i
= 0; i
< n
; i
++) {
2173 DisplayLine line
= new DisplayLine(currentState
.attr
);
2174 line
.setReverseColor(reverseVideo
);
2177 display
.addAll(displayMiddle
);
2178 display
.addAll(displayBottom
);
2180 assert (display
.size() == height
);
2184 * Process a control character.
2186 * @param ch 8-bit character from the remote side
2188 private void handleControlChar(final char ch
) {
2189 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2200 // Transmit the answerback message.
2211 cursorLeft(1, false);
2216 advanceToNextTabStop();
2242 currentState
.glLockshift
= LockshiftMode
.NONE
;
2248 currentState
.glLockshift
= LockshiftMode
.NONE
;
2273 singleshift
= Singleshift
.SS2
;
2278 singleshift
= Singleshift
.SS3
;
2288 * Advance the cursor to the next tab stop.
2290 private void advanceToNextTabStop() {
2291 if (tabStops
.size() == 0) {
2292 // Go to the rightmost column
2293 cursorRight(rightMargin
- currentState
.cursorX
, false);
2296 for (Integer stop
: tabStops
) {
2297 if (stop
> currentState
.cursorX
) {
2298 cursorRight(stop
- currentState
.cursorX
, false);
2303 * We got here, meaning there isn't a tab stop beyond the current
2304 * cursor position. Place the cursor of the right-most edge of the
2307 cursorRight(rightMargin
- currentState
.cursorX
, false);
2311 * Save a character into the collect buffer.
2313 * @param ch character to save
2315 private void collect(final char ch
) {
2316 collectBuffer
.append(ch
);
2320 * Save a byte into the CSI parameters buffer.
2322 * @param ch byte to save
2324 private void param(final byte ch
) {
2325 if (csiParams
.size() == 0) {
2326 csiParams
.add(new Integer(0));
2328 Integer x
= csiParams
.get(csiParams
.size() - 1);
2329 if ((ch
>= '0') && (ch
<= '9')) {
2332 csiParams
.set(csiParams
.size() - 1, x
);
2336 csiParams
.add(new Integer(0));
2341 * Get a CSI parameter value, with a default.
2343 * @param position parameter index. 0 is the first parameter.
2344 * @param defaultValue value to use if csiParams[position] doesn't exist
2345 * @return parameter value
2347 private int getCsiParam(final int position
, final int defaultValue
) {
2348 if (csiParams
.size() < position
+ 1) {
2349 return defaultValue
;
2351 return csiParams
.get(position
).intValue();
2355 * Get a CSI parameter value, clamped to within min/max.
2357 * @param position parameter index. 0 is the first parameter.
2358 * @param defaultValue value to use if csiParams[position] doesn't exist
2359 * @param minValue minimum value inclusive
2360 * @param maxValue maximum value inclusive
2361 * @return parameter value
2363 private int getCsiParam(final int position
, final int defaultValue
,
2364 final int minValue
, final int maxValue
) {
2366 assert (minValue
<= maxValue
);
2367 int value
= getCsiParam(position
, defaultValue
);
2368 if (value
< minValue
) {
2371 if (value
> maxValue
) {
2378 * Set or unset a toggle.
2380 * @param value true for set ('h'), false for reset ('l')
2382 private void setToggle(final boolean value
) {
2383 boolean decPrivateModeFlag
= false;
2384 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2385 if (collectBuffer
.charAt(i
) == '?') {
2386 decPrivateModeFlag
= true;
2391 for (Integer i
: csiParams
) {
2396 if (decPrivateModeFlag
== true) {
2398 if (value
== true) {
2399 // Use application arrow keys
2400 arrowKeyMode
= ArrowKeyMode
.VT100
;
2402 // Use ANSI arrow keys
2403 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2408 if (decPrivateModeFlag
== true) {
2409 if (value
== false) {
2413 arrowKeyMode
= ArrowKeyMode
.VT52
;
2416 * From the VT102 docs: "You use ANSI mode to select
2417 * most terminal features; the terminal uses the same
2418 * features when it switches to VT52 mode. You
2419 * cannot, however, change most of these features in
2422 * In other words, do not reset any other attributes
2423 * when switching between VT52 submode and ANSI.
2425 * HOWEVER, the real vt100 does switch the character
2426 * set according to Usenet.
2428 currentState
.g0Charset
= CharacterSet
.US
;
2429 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2432 if ((type
== DeviceType
.VT220
)
2433 || (type
== DeviceType
.XTERM
)) {
2435 // VT52 mode is explicitly 7-bit
2437 singleshift
= Singleshift
.NONE
;
2442 if (value
== true) {
2443 // Turn off keyboard
2452 if (decPrivateModeFlag
== true) {
2454 if (value
== true) {
2463 width
= rightMargin
+ 1;
2464 // Entire screen is cleared, and scrolling region is
2466 eraseScreen(0, 0, height
- 1, width
- 1, false);
2467 scrollRegionTop
= 0;
2468 scrollRegionBottom
= height
- 1;
2469 // Also home the cursor
2470 cursorPosition(0, 0);
2474 if (decPrivateModeFlag
== true) {
2476 if (value
== true) {
2485 if (value
== true) {
2493 if (decPrivateModeFlag
== true) {
2495 if (value
== true) {
2497 * Set selects reverse screen, a white screen
2498 * background with black characters.
2500 if (reverseVideo
!= true) {
2502 * If in normal video, switch it back
2504 invertDisplayColors();
2506 reverseVideo
= true;
2509 * Reset selects normal screen, a black screen
2510 * background with white characters.
2512 if (reverseVideo
== true) {
2514 * If in reverse video already, switch it back
2516 invertDisplayColors();
2518 reverseVideo
= false;
2523 if (decPrivateModeFlag
== true) {
2525 if (value
== true) {
2526 // Origin is relative to scroll region cursor.
2527 // Cursor can NEVER leave scrolling region.
2528 currentState
.originMode
= true;
2529 cursorPosition(0, 0);
2531 // Origin is absolute to entire screen. Cursor can
2532 // leave the scrolling region via cup() and hvp().
2533 currentState
.originMode
= false;
2534 cursorPosition(0, 0);
2539 if (decPrivateModeFlag
== true) {
2541 if (value
== true) {
2543 currentState
.lineWrap
= true;
2545 // Turn linewrap off
2546 currentState
.lineWrap
= false;
2551 if (decPrivateModeFlag
== true) {
2553 if (value
== true) {
2554 // Keyboard auto-repeat on
2557 // Keyboard auto-repeat off
2563 if (decPrivateModeFlag
== false) {
2565 if (value
== true) {
2575 if (decPrivateModeFlag
== true) {
2581 if (decPrivateModeFlag
== true) {
2587 if (decPrivateModeFlag
== false) {
2589 if (value
== true) {
2591 * Set causes a received linefeed, form feed, or
2592 * vertical tab to move cursor to first column of
2593 * next line. RETURN transmits both a carriage return
2594 * and linefeed. This selection is also called new
2600 * Reset causes a received linefeed, form feed, or
2601 * vertical tab to move cursor to next line in
2602 * current column. RETURN transmits a carriage
2605 newLineMode
= false;
2611 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2612 if (decPrivateModeFlag
== true) {
2614 if (value
== true) {
2616 cursorVisible
= true;
2619 cursorVisible
= false;
2626 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2627 if (decPrivateModeFlag
== true) {
2629 if (value
== true) {
2630 // Select national mode NRC
2633 // Select multi-national mode
2642 if ((type
== DeviceType
.XTERM
)
2643 && (decPrivateModeFlag
== true)
2645 // Mouse: normal tracking mode
2646 if (value
== true) {
2647 mouseProtocol
= MouseProtocol
.NORMAL
;
2649 mouseProtocol
= MouseProtocol
.OFF
;
2655 if ((type
== DeviceType
.XTERM
)
2656 && (decPrivateModeFlag
== true)
2658 // Mouse: normal tracking mode
2659 if (value
== true) {
2660 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2662 mouseProtocol
= MouseProtocol
.OFF
;
2668 if ((type
== DeviceType
.XTERM
)
2669 && (decPrivateModeFlag
== true)
2671 // Mouse: Any-event tracking mode
2672 if (value
== true) {
2673 mouseProtocol
= MouseProtocol
.ANYEVENT
;
2675 mouseProtocol
= MouseProtocol
.OFF
;
2681 if ((type
== DeviceType
.XTERM
)
2682 && (decPrivateModeFlag
== true)
2684 // Mouse: UTF-8 coordinates
2685 if (value
== true) {
2686 mouseEncoding
= MouseEncoding
.UTF8
;
2688 mouseEncoding
= MouseEncoding
.X10
;
2694 if ((type
== DeviceType
.XTERM
)
2695 && (decPrivateModeFlag
== true)
2697 // Mouse: SGR coordinates
2698 if (value
== true) {
2699 mouseEncoding
= MouseEncoding
.SGR
;
2701 mouseEncoding
= MouseEncoding
.X10
;
2714 * DECSC - Save cursor.
2716 private void decsc() {
2717 savedState
.setTo(currentState
);
2721 * DECRC - Restore cursor.
2723 private void decrc() {
2724 currentState
.setTo(savedState
);
2730 private void ind() {
2731 // Move the cursor and scroll if necessary. If at the bottom line
2732 // already, a scroll up is supposed to be performed.
2733 if (currentState
.cursorY
== scrollRegionBottom
) {
2734 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2736 cursorDown(1, true);
2740 * RI - Reverse index.
2743 // Move the cursor and scroll if necessary. If at the top line
2744 // already, a scroll down is supposed to be performed.
2745 if (currentState
.cursorY
== scrollRegionTop
) {
2746 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2754 private void nel() {
2755 // Move the cursor and scroll if necessary. If at the bottom line
2756 // already, a scroll up is supposed to be performed.
2757 if (currentState
.cursorY
== scrollRegionBottom
) {
2758 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2760 cursorDown(1, true);
2762 // Reset to the beginning of the next line
2763 currentState
.cursorX
= 0;
2767 * DECKPAM - Keypad application mode.
2769 private void deckpam() {
2770 keypadMode
= KeypadMode
.Application
;
2774 * DECKPNM - Keypad numeric mode.
2776 private void deckpnm() {
2777 keypadMode
= KeypadMode
.Numeric
;
2783 * @param n number of spaces to move
2784 * @param honorScrollRegion if true, then do nothing if the cursor is
2785 * outside the scrolling region
2787 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2791 * Special case: if a user moves the cursor from the right margin, we
2792 * have to reset the VT100 right margin flag.
2795 wrapLineFlag
= false;
2798 for (int i
= 0; i
< n
; i
++) {
2799 if (honorScrollRegion
== true) {
2800 // Honor the scrolling region
2801 if ((currentState
.cursorY
< scrollRegionTop
)
2802 || (currentState
.cursorY
> scrollRegionBottom
)
2804 // Outside region, do nothing
2807 // Inside region, go up
2808 top
= scrollRegionTop
;
2810 // Non-scrolling case
2814 if (currentState
.cursorY
> top
) {
2815 currentState
.cursorY
--;
2821 * Move down n spaces.
2823 * @param n number of spaces to move
2824 * @param honorScrollRegion if true, then do nothing if the cursor is
2825 * outside the scrolling region
2827 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
2831 * Special case: if a user moves the cursor from the right margin, we
2832 * have to reset the VT100 right margin flag.
2835 wrapLineFlag
= false;
2838 for (int i
= 0; i
< n
; i
++) {
2840 if (honorScrollRegion
== true) {
2841 // Honor the scrolling region
2842 if (currentState
.cursorY
> scrollRegionBottom
) {
2843 // Outside region, do nothing
2846 // Inside region, go down
2847 bottom
= scrollRegionBottom
;
2849 // Non-scrolling case
2850 bottom
= height
- 1;
2853 if (currentState
.cursorY
< bottom
) {
2854 currentState
.cursorY
++;
2860 * Move left n spaces.
2862 * @param n number of spaces to move
2863 * @param honorScrollRegion if true, then do nothing if the cursor is
2864 * outside the scrolling region
2866 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
2868 * Special case: if a user moves the cursor from the right margin, we
2869 * have to reset the VT100 right margin flag.
2872 wrapLineFlag
= false;
2875 for (int i
= 0; i
< n
; i
++) {
2876 if (honorScrollRegion
== true) {
2877 // Honor the scrolling region
2878 if ((currentState
.cursorY
< scrollRegionTop
)
2879 || (currentState
.cursorY
> scrollRegionBottom
)
2881 // Outside region, do nothing
2886 if (currentState
.cursorX
> 0) {
2887 currentState
.cursorX
--;
2893 * Move right n spaces.
2895 * @param n number of spaces to move
2896 * @param honorScrollRegion if true, then do nothing if the cursor is
2897 * outside the scrolling region
2899 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
2900 int rightMargin
= this.rightMargin
;
2903 * Special case: if a user moves the cursor from the right margin, we
2904 * have to reset the VT100 right margin flag.
2907 wrapLineFlag
= false;
2910 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2911 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2914 for (int i
= 0; i
< n
; i
++) {
2915 if (honorScrollRegion
== true) {
2916 // Honor the scrolling region
2917 if ((currentState
.cursorY
< scrollRegionTop
)
2918 || (currentState
.cursorY
> scrollRegionBottom
)
2920 // Outside region, do nothing
2925 if (currentState
.cursorX
< rightMargin
) {
2926 currentState
.cursorX
++;
2932 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2934 * @param row row to move to
2935 * @param col column to move to
2937 private void cursorPosition(int row
, final int col
) {
2938 int rightMargin
= this.rightMargin
;
2943 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2944 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2947 // Set column number
2948 currentState
.cursorX
= col
;
2950 // Sanity check, bring column back to margin.
2951 if (currentState
.cursorX
> rightMargin
) {
2952 currentState
.cursorX
= rightMargin
;
2956 if (currentState
.originMode
== true) {
2957 row
+= scrollRegionTop
;
2959 if (currentState
.cursorY
< row
) {
2960 cursorDown(row
- currentState
.cursorY
, false);
2961 } else if (currentState
.cursorY
> row
) {
2962 cursorUp(currentState
.cursorY
- row
, false);
2965 wrapLineFlag
= false;
2969 * HTS - Horizontal tabulation set.
2971 private void hts() {
2972 for (Integer stop
: tabStops
) {
2973 if (stop
== currentState
.cursorX
) {
2974 // Already have a tab stop here
2979 // Append a tab stop to the end of the array and resort them
2980 tabStops
.add(currentState
.cursorX
);
2981 Collections
.sort(tabStops
);
2985 * DECSWL - Single-width line.
2987 private void decswl() {
2988 display
.get(currentState
.cursorY
).setDoubleWidth(false);
2989 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2993 * DECDWL - Double-width line.
2995 private void decdwl() {
2996 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2997 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3001 * DECHDL - Double-height + double-width line.
3003 * @param topHalf if true, this sets the row to be the top half row of a
3006 private void dechdl(final boolean topHalf
) {
3007 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3008 if (topHalf
== true) {
3009 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3011 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3016 * DECALN - Screen alignment display.
3018 private void decaln() {
3019 Cell newCell
= new Cell();
3020 newCell
.setChar('E');
3021 for (DisplayLine line
: display
) {
3022 for (int i
= 0; i
< line
.length(); i
++) {
3023 line
.replace(i
, newCell
);
3029 * DECSCL - Compatibility level.
3031 private void decscl() {
3032 int i
= getCsiParam(0, 0);
3033 int j
= getCsiParam(1, 0);
3037 currentState
.g0Charset
= CharacterSet
.US
;
3038 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3040 } else if (i
== 62) {
3042 if ((j
== 0) || (j
== 2)) {
3044 } else if (j
== 1) {
3051 * CUD - Cursor down.
3053 private void cud() {
3054 cursorDown(getCsiParam(0, 1, 1, height
), true);
3058 * CUF - Cursor forward.
3060 private void cuf() {
3061 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3065 * CUB - Cursor backward.
3067 private void cub() {
3068 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3074 private void cuu() {
3075 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3079 * CUP - Cursor position.
3081 private void cup() {
3082 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3083 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3087 * CNL - Cursor down and to column 1.
3089 private void cnl() {
3090 cursorDown(getCsiParam(0, 1, 1, height
), true);
3092 cursorLeft(currentState
.cursorX
, true);
3096 * CPL - Cursor up and to column 1.
3098 private void cpl() {
3099 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3101 cursorLeft(currentState
.cursorX
, true);
3105 * CHA - Cursor to column # in current row.
3107 private void cha() {
3108 cursorPosition(currentState
.cursorY
,
3109 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3113 * VPA - Cursor to row #, same column.
3115 private void vpa() {
3116 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3117 currentState
.cursorX
);
3121 * ED - Erase in display.
3124 boolean honorProtected
= false;
3125 boolean decPrivateModeFlag
= false;
3127 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3128 if (collectBuffer
.charAt(i
) == '?') {
3129 decPrivateModeFlag
= true;
3134 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3135 && (decPrivateModeFlag
== true)
3137 honorProtected
= true;
3140 int i
= getCsiParam(0, 0);
3143 // Erase from here to end of screen
3144 if (currentState
.cursorY
< height
- 1) {
3145 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3148 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3149 } else if (i
== 1) {
3150 // Erase from beginning of screen to here
3151 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3153 eraseLine(0, currentState
.cursorX
, honorProtected
);
3154 } else if (i
== 2) {
3155 // Erase entire screen
3156 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3161 * EL - Erase in line.
3164 boolean honorProtected
= false;
3165 boolean decPrivateModeFlag
= false;
3167 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3168 if (collectBuffer
.charAt(i
) == '?') {
3169 decPrivateModeFlag
= true;
3174 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3175 && (decPrivateModeFlag
== true)
3177 honorProtected
= true;
3180 int i
= getCsiParam(0, 0);
3183 // Erase from here to end of line
3184 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3185 } else if (i
== 1) {
3186 // Erase from beginning of line to here
3187 eraseLine(0, currentState
.cursorX
, honorProtected
);
3188 } else if (i
== 2) {
3189 // Erase entire line
3190 eraseLine(0, width
- 1, honorProtected
);
3195 * ECH - Erase # of characters in current row.
3197 private void ech() {
3198 int i
= getCsiParam(0, 1, 1, width
);
3200 // Erase from here to i characters
3201 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3208 int i
= getCsiParam(0, 1);
3210 if ((currentState
.cursorY
>= scrollRegionTop
)
3211 && (currentState
.cursorY
<= scrollRegionBottom
)
3214 // I can get the same effect with a scroll-down
3215 scrollingRegionScrollDown(currentState
.cursorY
,
3216 scrollRegionBottom
, i
);
3221 * DCH - Delete char.
3223 private void dch() {
3224 int n
= getCsiParam(0, 1);
3225 DisplayLine line
= display
.get(currentState
.cursorY
);
3226 Cell blank
= new Cell();
3227 for (int i
= 0; i
< n
; i
++) {
3228 line
.delete(currentState
.cursorX
, blank
);
3233 * ICH - Insert blank char at cursor.
3235 private void ich() {
3236 int n
= getCsiParam(0, 1);
3237 DisplayLine line
= display
.get(currentState
.cursorY
);
3238 Cell blank
= new Cell();
3239 for (int i
= 0; i
< n
; i
++) {
3240 line
.insert(currentState
.cursorX
, blank
);
3248 int i
= getCsiParam(0, 1);
3250 if ((currentState
.cursorY
>= scrollRegionTop
)
3251 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3253 // I can get the same effect with a scroll-down
3254 scrollingRegionScrollUp(currentState
.cursorY
,
3255 scrollRegionBottom
, i
);
3260 * HVP - Horizontal and vertical position.
3262 private void hvp() {
3267 * REP - Repeat character.
3269 private void rep() {
3270 int n
= getCsiParam(0, 1);
3271 for (int i
= 0; i
< n
; i
++) {
3272 printCharacter(repCh
);
3280 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3281 getCsiParam(0, 1, 1, height
));
3288 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3289 getCsiParam(0, 1, 1, height
));
3293 * CBT - Go back X tab stops.
3295 private void cbt() {
3296 int tabsToMove
= getCsiParam(0, 1);
3299 for (int i
= 0; i
< tabsToMove
; i
++) {
3300 int j
= currentState
.cursorX
;
3301 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3302 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3310 j
= tabStops
.get(tabI
);
3312 cursorPosition(currentState
.cursorY
, j
);
3317 * CHT - Advance X tab stops.
3319 private void cht() {
3320 int n
= getCsiParam(0, 1);
3321 for (int i
= 0; i
< n
; i
++) {
3322 advanceToNextTabStop();
3327 * SGR - Select graphics rendition.
3329 private void sgr() {
3331 if (csiParams
.size() == 0) {
3332 currentState
.attr
.reset();
3336 for (Integer i
: csiParams
) {
3342 currentState
.attr
.reset();
3347 currentState
.attr
.setBold(true);
3352 currentState
.attr
.setUnderline(true);
3357 currentState
.attr
.setBlink(true);
3362 currentState
.attr
.setReverse(true);
3369 if (type
== DeviceType
.XTERM
) {
3383 if ((type
== DeviceType
.VT220
)
3384 || (type
== DeviceType
.XTERM
)) {
3390 currentState
.attr
.setBold(false);
3395 currentState
.attr
.setUnderline(false);
3400 currentState
.attr
.setBlink(false);
3405 currentState
.attr
.setReverse(false);
3413 // A true VT100/102/220 does not support color, however everyone
3414 // is used to their terminal emulator supporting color so we will
3415 // unconditionally support color for all DeviceType's.
3420 // Set black foreground
3421 currentState
.attr
.setForeColor(Color
.BLACK
);
3424 // Set red foreground
3425 currentState
.attr
.setForeColor(Color
.RED
);
3428 // Set green foreground
3429 currentState
.attr
.setForeColor(Color
.GREEN
);
3432 // Set yellow foreground
3433 currentState
.attr
.setForeColor(Color
.YELLOW
);
3436 // Set blue foreground
3437 currentState
.attr
.setForeColor(Color
.BLUE
);
3440 // Set magenta foreground
3441 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3444 // Set cyan foreground
3445 currentState
.attr
.setForeColor(Color
.CYAN
);
3448 // Set white foreground
3449 currentState
.attr
.setForeColor(Color
.WHITE
);
3452 if (type
== DeviceType
.XTERM
) {
3454 * Xterm supports T.416 / ISO-8613-3 codes to select
3455 * either an indexed color or an RGB value. (It also
3456 * permits these ISO-8613-3 SGR sequences to be separated
3457 * by colons rather than semicolons.)
3459 * We will not support any of these additional color
3460 * codes at this time:
3462 * 1. http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
3463 * has a detailed discussion of the current state of
3464 * RGB in various terminals, the point of which is
3465 * that none of them really do the same thing despite
3466 * all appearing to be "xterm".
3469 * https://bugs.kde.org/show_bug.cgi?id=107487#c3,
3470 * even supporting just the "indexed mode" of these
3471 * sequences (which could align easily with existing
3472 * SGR colors) is assumed to mean full support of
3473 * 24-bit RGB. So it is all or nothing.
3475 * Finally, these sequences break the assumptions of
3476 * standard ECMA-48 style parsers as pointed out at
3477 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
3478 * Therefore in order to keep a clean display, we cannot
3479 * parse anything else in this sequence.
3483 // Underscore on, default foreground color
3484 currentState
.attr
.setUnderline(true);
3485 currentState
.attr
.setForeColor(Color
.WHITE
);
3489 // Underscore off, default foreground color
3490 currentState
.attr
.setUnderline(false);
3491 currentState
.attr
.setForeColor(Color
.WHITE
);
3494 // Set black background
3495 currentState
.attr
.setBackColor(Color
.BLACK
);
3498 // Set red background
3499 currentState
.attr
.setBackColor(Color
.RED
);
3502 // Set green background
3503 currentState
.attr
.setBackColor(Color
.GREEN
);
3506 // Set yellow background
3507 currentState
.attr
.setBackColor(Color
.YELLOW
);
3510 // Set blue background
3511 currentState
.attr
.setBackColor(Color
.BLUE
);
3514 // Set magenta background
3515 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3518 // Set cyan background
3519 currentState
.attr
.setBackColor(Color
.CYAN
);
3522 // Set white background
3523 currentState
.attr
.setBackColor(Color
.WHITE
);
3526 if (type
== DeviceType
.XTERM
) {
3528 * Xterm supports T.416 / ISO-8613-3 codes to select
3529 * either an indexed color or an RGB value. (It also
3530 * permits these ISO-8613-3 SGR sequences to be separated
3531 * by colons rather than semicolons.)
3533 * We will not support this at this time. Also, in order
3534 * to keep a clean display, we cannot parse anything else
3541 // Default background
3542 currentState
.attr
.setBackColor(Color
.BLACK
);
3552 * DA - Device attributes.
3555 int extendedFlag
= 0;
3557 if (collectBuffer
.length() > 0) {
3558 String args
= collectBuffer
.substring(1);
3559 if (collectBuffer
.charAt(0) == '>') {
3561 if (collectBuffer
.length() >= 2) {
3562 i
= Integer
.parseInt(args
.toString());
3564 } else if (collectBuffer
.charAt(0) == '=') {
3566 if (collectBuffer
.length() >= 2) {
3567 i
= Integer
.parseInt(args
.toString());
3570 // Unknown code, bail out
3575 if ((i
!= 0) && (i
!= 1)) {
3579 if ((extendedFlag
== 0) && (i
== 0)) {
3580 // Send string directly to remote side
3581 writeRemote(deviceTypeResponse());
3585 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3587 if ((extendedFlag
== 1) && (i
== 0)) {
3589 * Request "What type of terminal are you, what is your
3590 * firmware version, and what hardware options do you have
3593 * Respond: "I am a VT220 (identification code of 1), my
3594 * firmware version is _____ (Pv), and I have _____ Po
3595 * options installed."
3601 if (s8c1t
== true) {
3602 writeRemote("\u009b>1;10;0c");
3604 writeRemote("\033[>1;10;0c");
3610 if ((extendedFlag
== 2) && (i
== 0)) {
3613 * Request "What is your unit ID?"
3615 * Respond: "I was manufactured at site 00 and have a unique ID
3619 writeRemote("\033P!|00010203\033\\");
3624 * DECSTBM - Set top and bottom margins.
3626 private void decstbm() {
3627 boolean decPrivateModeFlag
= false;
3629 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3630 if (collectBuffer
.charAt(i
) == '?') {
3631 decPrivateModeFlag
= true;
3635 if (decPrivateModeFlag
) {
3636 // This could be restore DEC private mode values.
3640 int top
= getCsiParam(0, 1, 1, height
) - 1;
3641 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3646 scrollRegionTop
= top
;
3647 scrollRegionBottom
= bottom
;
3650 cursorPosition(0, 0);
3655 * DECREQTPARM - Request terminal parameters.
3657 private void decreqtparm() {
3658 int i
= getCsiParam(0, 0);
3660 if ((i
!= 0) && (i
!= 1)) {
3667 * Request terminal parameters.
3671 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3672 * (CLoCk MULtiplier = 1, STP option flags = 0)
3676 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3679 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3681 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3687 * DECSCA - Select Character Attributes.
3689 private void decsca() {
3690 int i
= getCsiParam(0, 0);
3692 if ((i
== 0) || (i
== 2)) {
3694 currentState
.attr
.setProtect(false);
3698 currentState
.attr
.setProtect(true);
3703 * DECSTR - Soft Terminal Reset.
3705 private void decstr() {
3706 // Do exactly like RIS - Reset to initial state
3708 // Do I clear screen too? I think so...
3709 eraseScreen(0, 0, height
- 1, width
- 1, false);
3710 cursorPosition(0, 0);
3714 * DSR - Device status report.
3716 private void dsr() {
3717 boolean decPrivateModeFlag
= false;
3718 int row
= currentState
.cursorY
;
3720 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3721 if (collectBuffer
.charAt(i
) == '?') {
3722 decPrivateModeFlag
= true;
3727 int i
= getCsiParam(0, 0);
3732 // Request status report. Respond with "OK, no malfunction."
3734 // Send string directly to remote side
3735 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3738 writeRemote("\u009b0n");
3740 writeRemote("\033[0n");
3745 // Request cursor position. Respond with current position.
3746 if (currentState
.originMode
== true) {
3747 row
-= scrollRegionTop
;
3750 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3753 str
= String
.format("\u009b%d;%dR", row
+ 1,
3754 currentState
.cursorX
+ 1);
3756 str
= String
.format("\033[%d;%dR", row
+ 1,
3757 currentState
.cursorX
+ 1);
3760 // Send string directly to remote side
3765 if (decPrivateModeFlag
== true) {
3767 // Request printer status report. Respond with "Printer not
3770 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3771 && (s8c1t
== true)) {
3772 writeRemote("\u009b?13n");
3774 writeRemote("\033[?13n");
3780 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3781 && (decPrivateModeFlag
== true)
3784 // Request user-defined keys are locked or unlocked. Respond
3785 // with "User-defined keys are locked."
3787 if (s8c1t
== true) {
3788 writeRemote("\u009b?21n");
3790 writeRemote("\033[?21n");
3796 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3797 && (decPrivateModeFlag
== true)
3800 // Request keyboard language. Respond with "Keyboard
3801 // language is North American."
3803 if (s8c1t
== true) {
3804 writeRemote("\u009b?27;1n");
3806 writeRemote("\033[?27;1n");
3813 // Some other option, ignore
3819 * TBC - Tabulation clear.
3821 private void tbc() {
3822 int i
= getCsiParam(0, 0);
3824 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3825 for (Integer stop
: tabStops
) {
3826 if (stop
== currentState
.cursorX
) {
3831 tabStops
= newStops
;
3839 * Erase the characters in the current line from the start column to the
3840 * end column, inclusive.
3842 * @param start starting column to erase (between 0 and width - 1)
3843 * @param end ending column to erase (between 0 and width - 1)
3844 * @param honorProtected if true, do not erase characters with the
3845 * protected attribute set
3847 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3852 if (end
> width
- 1) {
3859 for (int i
= start
; i
<= end
; i
++) {
3860 DisplayLine line
= display
.get(currentState
.cursorY
);
3861 if ((!honorProtected
)
3862 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
3869 * From the VT102 manual:
3871 * Erasing a character also erases any character
3872 * attribute of the character.
3878 * Erase with the current color a.k.a. back-color erase
3881 line
.setChar(i
, ' ');
3882 line
.setAttr(i
, currentState
.attr
);
3890 * Erase a rectangular section of the screen, inclusive. end column,
3893 * @param startRow starting row to erase (between 0 and height - 1)
3894 * @param startCol starting column to erase (between 0 and width - 1)
3895 * @param endRow ending row to erase (between 0 and height - 1)
3896 * @param endCol ending column to erase (between 0 and width - 1)
3897 * @param honorProtected if true, do not erase characters with the
3898 * protected attribute set
3900 private void eraseScreen(final int startRow
, final int startCol
,
3901 final int endRow
, final int endCol
, final boolean honorProtected
) {
3909 || (endRow
< startRow
)
3910 || (endCol
< startCol
)
3915 oldCursorY
= currentState
.cursorY
;
3916 for (int i
= startRow
; i
<= endRow
; i
++) {
3917 currentState
.cursorY
= i
;
3918 eraseLine(startCol
, endCol
, honorProtected
);
3920 // Erase display clears the double attributes
3921 display
.get(i
).setDoubleWidth(false);
3922 display
.get(i
).setDoubleHeight(0);
3924 currentState
.cursorY
= oldCursorY
;
3928 * VT220 printer functions. All of these are parsed, but won't do
3931 private void printerFunctions() {
3932 boolean decPrivateModeFlag
= false;
3933 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3934 if (collectBuffer
.charAt(i
) == '?') {
3935 decPrivateModeFlag
= true;
3940 int i
= getCsiParam(0, 0);
3945 if (decPrivateModeFlag
== false) {
3951 if (decPrivateModeFlag
== true) {
3952 // Print cursor line
3957 if (decPrivateModeFlag
== true) {
3958 // Auto print mode OFF
3960 // Printer controller OFF
3962 // Characters re-appear on the screen
3963 printerControllerMode
= false;
3968 if (decPrivateModeFlag
== true) {
3972 // Printer controller
3974 // Characters get sucked into oblivion
3975 printerControllerMode
= true;
3986 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
3987 * of remote systems will send an XTerm title sequence even if TERM isn't
3990 * @param xtermChar the character received from the remote side
3992 private void oscPut(final char xtermChar
) {
3994 collectBuffer
.append(xtermChar
);
3997 if (xtermChar
== 0x07) {
3998 String args
= collectBuffer
.substring(0,
3999 collectBuffer
.length() - 1);
4000 String
[] p
= args
.toString().split(";");
4002 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4010 // Go to SCAN_GROUND state
4017 * Run this input character through the ECMA48 state machine.
4019 * @param ch character from the remote side
4021 private void consume(char ch
) {
4024 // System.err.printf("%c", ch);
4026 // Special case for VT10x: 7-bit characters only
4027 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4028 ch
= (char)(ch
& 0x7F);
4031 // Special "anywhere" states
4033 // 18, 1A --> execute, then switch to SCAN_GROUND
4034 if ((ch
== 0x18) || (ch
== 0x1A)) {
4035 // CAN and SUB abort escape sequences
4040 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4044 && (scanState
!= ScanState
.DCS_ENTRY
)
4045 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4046 && (scanState
!= ScanState
.DCS_IGNORE
)
4047 && (scanState
!= ScanState
.DCS_PARAM
)
4048 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4051 scanState
= ScanState
.ESCAPE
;
4055 // 0x9B == CSI 8-bit sequence
4057 scanState
= ScanState
.CSI_ENTRY
;
4061 // 0x9D goes to ScanState.OSC_STRING
4063 scanState
= ScanState
.OSC_STRING
;
4067 // 0x90 goes to DCS_ENTRY
4069 scanState
= ScanState
.DCS_ENTRY
;
4073 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4074 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4075 scanState
= ScanState
.SOSPMAPC_STRING
;
4079 // 0x7F (DEL) is always discarded
4084 switch (scanState
) {
4087 // 00-17, 19, 1C-1F --> execute
4088 // 80-8F, 91-9A, 9C --> execute
4089 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4090 handleControlChar(ch
);
4094 if (((ch
>= 0x20) && (ch
<= 0x7F))
4098 // VT220 printer --> trash bin
4099 if (((type
== DeviceType
.VT220
)
4100 || (type
== DeviceType
.XTERM
))
4101 && (printerControllerMode
== true)
4106 // Hang onto this character
4107 repCh
= mapCharacter(ch
);
4109 // Print this character
4110 printCharacter(repCh
);
4115 // 00-17, 19, 1C-1F --> execute
4117 handleControlChar(ch
);
4121 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4122 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4124 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4128 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4129 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4140 // DECSC - Save cursor
4141 // Note this code overlaps both ANSI and VT52 mode
4146 // DECRC - Restore cursor
4147 // Note this code overlaps both ANSI and VT52 mode
4156 if (vt52Mode
== true) {
4157 // DECANM - Enter ANSI mode
4159 arrowKeyMode
= ArrowKeyMode
.VT100
;
4162 * From the VT102 docs: "You use ANSI mode to select
4163 * most terminal features; the terminal uses the same
4164 * features when it switches to VT52 mode. You
4165 * cannot, however, change most of these features in
4168 * In other words, do not reset any other attributes
4169 * when switching between VT52 submode and ANSI.
4173 currentState
.g0Charset
= CharacterSet
.US
;
4174 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4176 singleshift
= Singleshift
.NONE
;
4177 currentState
.glLockshift
= LockshiftMode
.NONE
;
4178 currentState
.grLockshift
= LockshiftMode
.NONE
;
4182 // DECKPAM - Keypad application mode
4183 // Note this code overlaps both ANSI and VT52 mode
4187 // DECKPNM - Keypad numeric mode
4188 // Note this code overlaps both ANSI and VT52 mode
4195 if (vt52Mode
== true) {
4196 // Cursor up, and stop at the top without scrolling
4201 if (vt52Mode
== true) {
4202 // Cursor down, and stop at the bottom without scrolling
4203 cursorDown(1, false);
4207 if (vt52Mode
== true) {
4208 // Cursor right, and stop at the right without scrolling
4209 cursorRight(1, false);
4213 if (vt52Mode
== true) {
4214 // Cursor left, and stop at the left without scrolling
4215 cursorLeft(1, false);
4222 if (vt52Mode
== true) {
4230 if (vt52Mode
== true) {
4231 // G0 --> Special graphics
4232 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
4236 if (vt52Mode
== true) {
4238 currentState
.g0Charset
= CharacterSet
.US
;
4242 if (vt52Mode
== true) {
4244 cursorPosition(0, 0);
4246 // HTS - Horizontal tabulation set
4251 if (vt52Mode
== true) {
4252 // Reverse line feed. Same as RI.
4257 if (vt52Mode
== true) {
4258 // Erase to end of screen
4259 eraseLine(currentState
.cursorX
, width
- 1, false);
4260 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4265 if (vt52Mode
== true) {
4266 // Erase to end of line
4267 eraseLine(currentState
.cursorX
, width
- 1, false);
4273 if (vt52Mode
== true) {
4276 // RI - Reverse index
4281 if (vt52Mode
== false) {
4283 singleshift
= Singleshift
.SS2
;
4287 if (vt52Mode
== false) {
4289 singleshift
= Singleshift
.SS3
;
4296 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4312 if (vt52Mode
== true) {
4313 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4321 if (vt52Mode
== true) {
4323 // Send string directly to remote side
4324 writeRemote("\033/Z");
4327 // Send string directly to remote side
4328 writeRemote(deviceTypeResponse());
4339 // VT52 cannot get to any of these other states
4340 if (vt52Mode
== true) {
4345 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4352 // RIS - Reset to initial state
4354 // Do I clear screen too? I think so...
4355 eraseScreen(0, 0, height
- 1, width
- 1, false);
4356 cursorPosition(0, 0);
4370 if ((type
== DeviceType
.VT220
)
4371 || (type
== DeviceType
.XTERM
)) {
4373 // VT220 lockshift G2 into GL
4374 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4379 if ((type
== DeviceType
.VT220
)
4380 || (type
== DeviceType
.XTERM
)) {
4382 // VT220 lockshift G3 into GL
4383 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4401 if ((type
== DeviceType
.VT220
)
4402 || (type
== DeviceType
.XTERM
)) {
4404 // VT220 lockshift G3 into GR
4405 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4410 if ((type
== DeviceType
.VT220
)
4411 || (type
== DeviceType
.XTERM
)) {
4413 // VT220 lockshift G2 into GR
4414 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4420 if ((type
== DeviceType
.VT220
)
4421 || (type
== DeviceType
.XTERM
)) {
4423 // VT220 lockshift G1 into GR
4424 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4434 // 0x5B goes to CSI_ENTRY
4436 scanState
= ScanState
.CSI_ENTRY
;
4439 // 0x5D goes to OSC_STRING
4441 scanState
= ScanState
.OSC_STRING
;
4444 // 0x50 goes to DCS_ENTRY
4446 scanState
= ScanState
.DCS_ENTRY
;
4449 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4450 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4451 scanState
= ScanState
.SOSPMAPC_STRING
;
4456 case ESCAPE_INTERMEDIATE
:
4457 // 00-17, 19, 1C-1F --> execute
4459 handleControlChar(ch
);
4462 // 20-2F --> collect
4463 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4467 // 30-7E --> dispatch, then switch to GROUND
4468 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4471 if ((collectBuffer
.length() == 1)
4472 && (collectBuffer
.charAt(0) == '(')) {
4473 // G0 --> Special graphics
4474 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4476 if ((collectBuffer
.length() == 1)
4477 && (collectBuffer
.charAt(0) == ')')) {
4478 // G1 --> Special graphics
4479 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4481 if ((type
== DeviceType
.VT220
)
4482 || (type
== DeviceType
.XTERM
)) {
4484 if ((collectBuffer
.length() == 1)
4485 && (collectBuffer
.charAt(0) == '*')) {
4486 // G2 --> Special graphics
4487 currentState
.g2Charset
= CharacterSet
.DRAWING
;
4489 if ((collectBuffer
.length() == 1)
4490 && (collectBuffer
.charAt(0) == '+')) {
4491 // G3 --> Special graphics
4492 currentState
.g3Charset
= CharacterSet
.DRAWING
;
4497 if ((collectBuffer
.length() == 1)
4498 && (collectBuffer
.charAt(0) == '(')) {
4499 // G0 --> Alternate character ROM standard character set
4500 currentState
.g0Charset
= CharacterSet
.ROM
;
4502 if ((collectBuffer
.length() == 1)
4503 && (collectBuffer
.charAt(0) == ')')) {
4504 // G1 --> Alternate character ROM standard character set
4505 currentState
.g1Charset
= CharacterSet
.ROM
;
4509 if ((collectBuffer
.length() == 1)
4510 && (collectBuffer
.charAt(0) == '(')) {
4511 // G0 --> Alternate character ROM special graphics
4512 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4514 if ((collectBuffer
.length() == 1)
4515 && (collectBuffer
.charAt(0) == ')')) {
4516 // G1 --> Alternate character ROM special graphics
4517 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4521 if ((collectBuffer
.length() == 1)
4522 && (collectBuffer
.charAt(0) == '#')) {
4523 // DECDHL - Double-height line (top half)
4528 if ((collectBuffer
.length() == 1)
4529 && (collectBuffer
.charAt(0) == '#')) {
4530 // DECDHL - Double-height line (bottom half)
4533 if ((type
== DeviceType
.VT220
)
4534 || (type
== DeviceType
.XTERM
)) {
4536 if ((collectBuffer
.length() == 1)
4537 && (collectBuffer
.charAt(0) == '(')) {
4539 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4541 if ((collectBuffer
.length() == 1)
4542 && (collectBuffer
.charAt(0) == ')')) {
4544 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4546 if ((collectBuffer
.length() == 1)
4547 && (collectBuffer
.charAt(0) == '*')) {
4549 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4551 if ((collectBuffer
.length() == 1)
4552 && (collectBuffer
.charAt(0) == '+')) {
4554 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4559 if ((collectBuffer
.length() == 1)
4560 && (collectBuffer
.charAt(0) == '#')) {
4561 // DECSWL - Single-width line
4564 if ((type
== DeviceType
.VT220
)
4565 || (type
== DeviceType
.XTERM
)) {
4567 if ((collectBuffer
.length() == 1)
4568 && (collectBuffer
.charAt(0) == '(')) {
4570 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4572 if ((collectBuffer
.length() == 1)
4573 && (collectBuffer
.charAt(0) == ')')) {
4575 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4577 if ((collectBuffer
.length() == 1)
4578 && (collectBuffer
.charAt(0) == '*')) {
4580 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4582 if ((collectBuffer
.length() == 1)
4583 && (collectBuffer
.charAt(0) == '+')) {
4585 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4590 if ((collectBuffer
.length() == 1)
4591 && (collectBuffer
.charAt(0) == '#')) {
4592 // DECDWL - Double-width line
4595 if ((type
== DeviceType
.VT220
)
4596 || (type
== DeviceType
.XTERM
)) {
4598 if ((collectBuffer
.length() == 1)
4599 && (collectBuffer
.charAt(0) == '(')) {
4601 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4603 if ((collectBuffer
.length() == 1)
4604 && (collectBuffer
.charAt(0) == ')')) {
4606 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4608 if ((collectBuffer
.length() == 1)
4609 && (collectBuffer
.charAt(0) == '*')) {
4611 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4613 if ((collectBuffer
.length() == 1)
4614 && (collectBuffer
.charAt(0) == '+')) {
4616 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4621 if ((type
== DeviceType
.VT220
)
4622 || (type
== DeviceType
.XTERM
)) {
4624 if ((collectBuffer
.length() == 1)
4625 && (collectBuffer
.charAt(0) == '(')) {
4627 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4629 if ((collectBuffer
.length() == 1)
4630 && (collectBuffer
.charAt(0) == ')')) {
4632 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4634 if ((collectBuffer
.length() == 1)
4635 && (collectBuffer
.charAt(0) == '*')) {
4637 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4639 if ((collectBuffer
.length() == 1)
4640 && (collectBuffer
.charAt(0) == '+')) {
4642 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4647 if ((collectBuffer
.length() == 1)
4648 && (collectBuffer
.charAt(0) == '#')) {
4649 // DECALN - Screen alignment display
4658 if ((type
== DeviceType
.VT220
)
4659 || (type
== DeviceType
.XTERM
)) {
4661 if ((collectBuffer
.length() == 1)
4662 && (collectBuffer
.charAt(0) == '(')) {
4663 // G0 --> DEC_SUPPLEMENTAL
4664 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4666 if ((collectBuffer
.length() == 1)
4667 && (collectBuffer
.charAt(0) == ')')) {
4668 // G1 --> DEC_SUPPLEMENTAL
4669 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4671 if ((collectBuffer
.length() == 1)
4672 && (collectBuffer
.charAt(0) == '*')) {
4673 // G2 --> DEC_SUPPLEMENTAL
4674 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4676 if ((collectBuffer
.length() == 1)
4677 && (collectBuffer
.charAt(0) == '+')) {
4678 // G3 --> DEC_SUPPLEMENTAL
4679 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4684 if ((type
== DeviceType
.VT220
)
4685 || (type
== DeviceType
.XTERM
)) {
4687 if ((collectBuffer
.length() == 1)
4688 && (collectBuffer
.charAt(0) == '(')) {
4690 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4692 if ((collectBuffer
.length() == 1)
4693 && (collectBuffer
.charAt(0) == ')')) {
4695 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4697 if ((collectBuffer
.length() == 1)
4698 && (collectBuffer
.charAt(0) == '*')) {
4700 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4702 if ((collectBuffer
.length() == 1)
4703 && (collectBuffer
.charAt(0) == '+')) {
4705 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4714 if ((collectBuffer
.length() == 1)
4715 && (collectBuffer
.charAt(0) == '(')) {
4716 // G0 --> United Kingdom set
4717 currentState
.g0Charset
= CharacterSet
.UK
;
4719 if ((collectBuffer
.length() == 1)
4720 && (collectBuffer
.charAt(0) == ')')) {
4721 // G1 --> United Kingdom set
4722 currentState
.g1Charset
= CharacterSet
.UK
;
4724 if ((type
== DeviceType
.VT220
)
4725 || (type
== DeviceType
.XTERM
)) {
4727 if ((collectBuffer
.length() == 1)
4728 && (collectBuffer
.charAt(0) == '*')) {
4729 // G2 --> United Kingdom set
4730 currentState
.g2Charset
= CharacterSet
.UK
;
4732 if ((collectBuffer
.length() == 1)
4733 && (collectBuffer
.charAt(0) == '+')) {
4734 // G3 --> United Kingdom set
4735 currentState
.g3Charset
= CharacterSet
.UK
;
4740 if ((collectBuffer
.length() == 1)
4741 && (collectBuffer
.charAt(0) == '(')) {
4743 currentState
.g0Charset
= CharacterSet
.US
;
4745 if ((collectBuffer
.length() == 1)
4746 && (collectBuffer
.charAt(0) == ')')) {
4748 currentState
.g1Charset
= CharacterSet
.US
;
4750 if ((type
== DeviceType
.VT220
)
4751 || (type
== DeviceType
.XTERM
)) {
4753 if ((collectBuffer
.length() == 1)
4754 && (collectBuffer
.charAt(0) == '*')) {
4756 currentState
.g2Charset
= CharacterSet
.US
;
4758 if ((collectBuffer
.length() == 1)
4759 && (collectBuffer
.charAt(0) == '+')) {
4761 currentState
.g3Charset
= CharacterSet
.US
;
4766 if ((type
== DeviceType
.VT220
)
4767 || (type
== DeviceType
.XTERM
)) {
4769 if ((collectBuffer
.length() == 1)
4770 && (collectBuffer
.charAt(0) == '(')) {
4772 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4774 if ((collectBuffer
.length() == 1)
4775 && (collectBuffer
.charAt(0) == ')')) {
4777 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4779 if ((collectBuffer
.length() == 1)
4780 && (collectBuffer
.charAt(0) == '*')) {
4782 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4784 if ((collectBuffer
.length() == 1)
4785 && (collectBuffer
.charAt(0) == '+')) {
4787 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4794 if ((type
== DeviceType
.VT220
)
4795 || (type
== DeviceType
.XTERM
)) {
4797 if ((collectBuffer
.length() == 1)
4798 && (collectBuffer
.charAt(0) == '(')) {
4800 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4802 if ((collectBuffer
.length() == 1)
4803 && (collectBuffer
.charAt(0) == ')')) {
4805 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4807 if ((collectBuffer
.length() == 1)
4808 && (collectBuffer
.charAt(0) == '*')) {
4810 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4812 if ((collectBuffer
.length() == 1)
4813 && (collectBuffer
.charAt(0) == '+')) {
4815 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4820 if ((type
== DeviceType
.VT220
)
4821 || (type
== DeviceType
.XTERM
)) {
4823 if ((collectBuffer
.length() == 1)
4824 && (collectBuffer
.charAt(0) == ' ')) {
4831 if ((type
== DeviceType
.VT220
)
4832 || (type
== DeviceType
.XTERM
)) {
4834 if ((collectBuffer
.length() == 1)
4835 && (collectBuffer
.charAt(0) == ' ')) {
4842 if ((type
== DeviceType
.VT220
)
4843 || (type
== DeviceType
.XTERM
)) {
4845 if ((collectBuffer
.length() == 1)
4846 && (collectBuffer
.charAt(0) == '(')) {
4848 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4850 if ((collectBuffer
.length() == 1)
4851 && (collectBuffer
.charAt(0) == ')')) {
4853 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4855 if ((collectBuffer
.length() == 1)
4856 && (collectBuffer
.charAt(0) == '*')) {
4858 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4860 if ((collectBuffer
.length() == 1)
4861 && (collectBuffer
.charAt(0) == '+')) {
4863 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4871 if ((type
== DeviceType
.VT220
)
4872 || (type
== DeviceType
.XTERM
)) {
4874 if ((collectBuffer
.length() == 1)
4875 && (collectBuffer
.charAt(0) == '(')) {
4877 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4879 if ((collectBuffer
.length() == 1)
4880 && (collectBuffer
.charAt(0) == ')')) {
4882 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4884 if ((collectBuffer
.length() == 1)
4885 && (collectBuffer
.charAt(0) == '*')) {
4887 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4889 if ((collectBuffer
.length() == 1)
4890 && (collectBuffer
.charAt(0) == '+')) {
4892 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4903 if ((type
== DeviceType
.VT220
)
4904 || (type
== DeviceType
.XTERM
)) {
4906 if ((collectBuffer
.length() == 1)
4907 && (collectBuffer
.charAt(0) == '(')) {
4909 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4911 if ((collectBuffer
.length() == 1)
4912 && (collectBuffer
.charAt(0) == ')')) {
4914 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4916 if ((collectBuffer
.length() == 1)
4917 && (collectBuffer
.charAt(0) == '*')) {
4919 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4921 if ((collectBuffer
.length() == 1)
4922 && (collectBuffer
.charAt(0) == '+')) {
4924 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4929 if ((type
== DeviceType
.VT220
)
4930 || (type
== DeviceType
.XTERM
)) {
4932 if ((collectBuffer
.length() == 1)
4933 && (collectBuffer
.charAt(0) == '(')) {
4935 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4937 if ((collectBuffer
.length() == 1)
4938 && (collectBuffer
.charAt(0) == ')')) {
4940 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4942 if ((collectBuffer
.length() == 1)
4943 && (collectBuffer
.charAt(0) == '*')) {
4945 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
4947 if ((collectBuffer
.length() == 1)
4948 && (collectBuffer
.charAt(0) == '+')) {
4950 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
4962 if ((type
== DeviceType
.VT220
)
4963 || (type
== DeviceType
.XTERM
)) {
4965 if ((collectBuffer
.length() == 1)
4966 && (collectBuffer
.charAt(0) == '(')) {
4968 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
4970 if ((collectBuffer
.length() == 1)
4971 && (collectBuffer
.charAt(0) == ')')) {
4973 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
4975 if ((collectBuffer
.length() == 1)
4976 && (collectBuffer
.charAt(0) == '*')) {
4978 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
4980 if ((collectBuffer
.length() == 1)
4981 && (collectBuffer
.charAt(0) == '+')) {
4983 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
4988 if ((type
== DeviceType
.VT220
)
4989 || (type
== DeviceType
.XTERM
)) {
4991 if ((collectBuffer
.length() == 1)
4992 && (collectBuffer
.charAt(0) == '(')) {
4994 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
4996 if ((collectBuffer
.length() == 1)
4997 && (collectBuffer
.charAt(0) == ')')) {
4999 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5001 if ((collectBuffer
.length() == 1)
5002 && (collectBuffer
.charAt(0) == '*')) {
5004 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5006 if ((collectBuffer
.length() == 1)
5007 && (collectBuffer
.charAt(0) == '+')) {
5009 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5056 // 0x9C goes to GROUND
5064 // 00-17, 19, 1C-1F --> execute
5066 handleControlChar(ch
);
5069 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5070 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5072 scanState
= ScanState
.CSI_INTERMEDIATE
;
5075 // 30-39, 3B --> param, then switch to CSI_PARAM
5076 if ((ch
>= '0') && (ch
<= '9')) {
5078 scanState
= ScanState
.CSI_PARAM
;
5082 scanState
= ScanState
.CSI_PARAM
;
5085 // 3C-3F --> collect, then switch to CSI_PARAM
5086 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5088 scanState
= ScanState
.CSI_PARAM
;
5091 // 40-7E --> dispatch, then switch to GROUND
5092 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5095 // ICH - Insert character
5103 // CUD - Cursor down
5107 // CUF - Cursor forward
5111 // CUB - Cursor backward
5115 // CNL - Cursor down and to column 1
5116 if (type
== DeviceType
.XTERM
) {
5121 // CPL - Cursor up and to column 1
5122 if (type
== DeviceType
.XTERM
) {
5127 // CHA - Cursor to column # in current row
5128 if (type
== DeviceType
.XTERM
) {
5133 // CUP - Cursor position
5137 // CHT - Cursor forward X tab stops (default 1)
5138 if (type
== DeviceType
.XTERM
) {
5143 // ED - Erase in display
5147 // EL - Erase in line
5162 // DCH - Delete character
5169 // Scroll up X lines (default 1)
5170 if (type
== DeviceType
.XTERM
) {
5175 // Scroll down X lines (default 1)
5176 if (type
== DeviceType
.XTERM
) {
5185 if ((type
== DeviceType
.VT220
)
5186 || (type
== DeviceType
.XTERM
)) {
5188 // ECH - Erase character
5195 // CBT - Cursor backward X tab stops (default 1)
5196 if (type
== DeviceType
.XTERM
) {
5207 // HPA - Cursor to column # in current row. Same as CHA
5208 if (type
== DeviceType
.XTERM
) {
5213 // HPR - Cursor right. Same as CUF
5214 if (type
== DeviceType
.XTERM
) {
5219 // REP - Repeat last char X times
5220 if (type
== DeviceType
.XTERM
) {
5225 // DA - Device attributes
5229 // VPA - Cursor to row, current column.
5230 if (type
== DeviceType
.XTERM
) {
5235 // VPR - Cursor down. Same as CUD
5236 if (type
== DeviceType
.XTERM
) {
5241 // HVP - Horizontal and vertical position
5245 // TBC - Tabulation clear
5249 // Sets an ANSI or DEC private toggle
5253 if ((type
== DeviceType
.VT220
)
5254 || (type
== DeviceType
.XTERM
)) {
5256 // Printer functions
5264 // Sets an ANSI or DEC private toggle
5268 // SGR - Select graphics rendition
5272 // DSR - Device status report
5279 // DECLL - Load leds
5283 // DECSTBM - Set top and bottom margins
5287 // Save cursor (ANSI.SYS)
5288 if (type
== DeviceType
.XTERM
) {
5289 savedState
.cursorX
= currentState
.cursorX
;
5290 savedState
.cursorY
= currentState
.cursorY
;
5296 // Restore cursor (ANSI.SYS)
5297 if (type
== DeviceType
.XTERM
) {
5298 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5305 // DECREQTPARM - Request terminal parameters
5321 // 0x9C goes to GROUND
5326 // 0x3A goes to CSI_IGNORE
5328 scanState
= ScanState
.CSI_IGNORE
;
5333 // 00-17, 19, 1C-1F --> execute
5335 handleControlChar(ch
);
5338 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5339 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5341 scanState
= ScanState
.CSI_INTERMEDIATE
;
5344 // 30-39, 3B --> param
5345 if ((ch
>= '0') && (ch
<= '9')) {
5352 // 0x3A goes to CSI_IGNORE
5354 scanState
= ScanState
.CSI_IGNORE
;
5356 // 0x3C-3F goes to CSI_IGNORE
5357 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5358 scanState
= ScanState
.CSI_IGNORE
;
5361 // 40-7E --> dispatch, then switch to GROUND
5362 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5365 // ICH - Insert character
5373 // CUD - Cursor down
5377 // CUF - Cursor forward
5381 // CUB - Cursor backward
5385 // CNL - Cursor down and to column 1
5386 if (type
== DeviceType
.XTERM
) {
5391 // CPL - Cursor up and to column 1
5392 if (type
== DeviceType
.XTERM
) {
5397 // CHA - Cursor to column # in current row
5398 if (type
== DeviceType
.XTERM
) {
5403 // CUP - Cursor position
5407 // CHT - Cursor forward X tab stops (default 1)
5408 if (type
== DeviceType
.XTERM
) {
5413 // ED - Erase in display
5417 // EL - Erase in line
5432 // DCH - Delete character
5439 // Scroll up X lines (default 1)
5440 if (type
== DeviceType
.XTERM
) {
5445 // Scroll down X lines (default 1)
5446 if (type
== DeviceType
.XTERM
) {
5455 if ((type
== DeviceType
.VT220
)
5456 || (type
== DeviceType
.XTERM
)) {
5458 // ECH - Erase character
5465 // CBT - Cursor backward X tab stops (default 1)
5466 if (type
== DeviceType
.XTERM
) {
5477 // HPA - Cursor to column # in current row. Same as CHA
5478 if (type
== DeviceType
.XTERM
) {
5483 // HPR - Cursor right. Same as CUF
5484 if (type
== DeviceType
.XTERM
) {
5489 // REP - Repeat last char X times
5490 if (type
== DeviceType
.XTERM
) {
5495 // DA - Device attributes
5499 // VPA - Cursor to row, current column.
5500 if (type
== DeviceType
.XTERM
) {
5505 // VPR - Cursor down. Same as CUD
5506 if (type
== DeviceType
.XTERM
) {
5511 // HVP - Horizontal and vertical position
5515 // TBC - Tabulation clear
5519 // Sets an ANSI or DEC private toggle
5523 if ((type
== DeviceType
.VT220
)
5524 || (type
== DeviceType
.XTERM
)) {
5526 // Printer functions
5534 // Sets an ANSI or DEC private toggle
5538 // SGR - Select graphics rendition
5542 // DSR - Device status report
5549 // DECLL - Load leds
5553 // DECSTBM - Set top and bottom margins
5563 // DECREQTPARM - Request terminal parameters
5580 case CSI_INTERMEDIATE
:
5581 // 00-17, 19, 1C-1F --> execute
5583 handleControlChar(ch
);
5586 // 20-2F --> collect
5587 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5591 // 0x30-3F goes to CSI_IGNORE
5592 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5593 scanState
= ScanState
.CSI_IGNORE
;
5596 // 40-7E --> dispatch, then switch to GROUND
5597 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5649 if (((type
== DeviceType
.VT220
)
5650 || (type
== DeviceType
.XTERM
))
5651 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5653 // DECSCL - compatibility level
5656 if ((type
== DeviceType
.XTERM
)
5657 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
5659 // DECSTR - Soft terminal reset
5664 if (((type
== DeviceType
.VT220
)
5665 || (type
== DeviceType
.XTERM
))
5666 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5694 // 00-17, 19, 1C-1F --> execute
5696 handleControlChar(ch
);
5699 // 20-2F --> collect
5700 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5704 // 40-7E --> ignore, then switch to GROUND
5705 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5709 // 20-3F, 7F --> ignore
5715 // 0x9C goes to GROUND
5720 // 0x1B 0x5C goes to GROUND
5725 if ((collectBuffer
.length() > 0)
5726 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5732 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5733 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5735 scanState
= ScanState
.DCS_INTERMEDIATE
;
5738 // 30-39, 3B --> param, then switch to DCS_PARAM
5739 if ((ch
>= '0') && (ch
<= '9')) {
5741 scanState
= ScanState
.DCS_PARAM
;
5745 scanState
= ScanState
.DCS_PARAM
;
5748 // 3C-3F --> collect, then switch to DCS_PARAM
5749 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5751 scanState
= ScanState
.DCS_PARAM
;
5754 // 00-17, 19, 1C-1F, 7F --> ignore
5756 // 0x3A goes to DCS_IGNORE
5758 scanState
= ScanState
.DCS_IGNORE
;
5761 // 0x40-7E goes to DCS_PASSTHROUGH
5762 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5763 scanState
= ScanState
.DCS_PASSTHROUGH
;
5767 case DCS_INTERMEDIATE
:
5769 // 0x9C goes to GROUND
5774 // 0x1B 0x5C goes to GROUND
5779 if ((collectBuffer
.length() > 0)
5780 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5786 // 0x30-3F goes to DCS_IGNORE
5787 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5788 scanState
= ScanState
.DCS_IGNORE
;
5791 // 0x40-7E goes to DCS_PASSTHROUGH
5792 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5793 scanState
= ScanState
.DCS_PASSTHROUGH
;
5796 // 00-17, 19, 1C-1F, 7F --> ignore
5801 // 0x9C goes to GROUND
5806 // 0x1B 0x5C goes to GROUND
5811 if ((collectBuffer
.length() > 0)
5812 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5818 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5819 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5821 scanState
= ScanState
.DCS_INTERMEDIATE
;
5824 // 30-39, 3B --> param
5825 if ((ch
>= '0') && (ch
<= '9')) {
5832 // 00-17, 19, 1C-1F, 7F --> ignore
5834 // 0x3A, 3C-3F goes to DCS_IGNORE
5836 scanState
= ScanState
.DCS_IGNORE
;
5838 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5839 scanState
= ScanState
.DCS_IGNORE
;
5842 // 0x40-7E goes to DCS_PASSTHROUGH
5843 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5844 scanState
= ScanState
.DCS_PASSTHROUGH
;
5848 case DCS_PASSTHROUGH
:
5849 // 0x9C goes to GROUND
5854 // 0x1B 0x5C goes to GROUND
5859 if ((collectBuffer
.length() > 0)
5860 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5866 // 00-17, 19, 1C-1F, 20-7E --> put
5874 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5877 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5886 // 00-17, 19, 1C-1F, 20-7F --> ignore
5888 // 0x9C goes to GROUND
5895 case SOSPMAPC_STRING
:
5896 // 00-17, 19, 1C-1F, 20-7F --> ignore
5898 // 0x9C goes to GROUND
5906 // Special case for Xterm: OSC can pass control characters
5907 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5911 // 00-17, 19, 1C-1F --> ignore
5913 // 20-7F --> osc_put
5914 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5918 // 0x9C goes to GROUND
5925 case VT52_DIRECT_CURSOR_ADDRESS
:
5926 // This is a special case for the VT52 sequence "ESC Y l c"
5927 if (collectBuffer
.length() == 0) {
5929 } else if (collectBuffer
.length() == 1) {
5930 // We've got the two characters, one in the buffer and the
5932 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
5941 * Expose current cursor X to outside world.
5943 * @return current cursor X
5945 public final int getCursorX() {
5946 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
5947 return currentState
.cursorX
* 2;
5949 return currentState
.cursorX
;
5953 * Expose current cursor Y to outside world.
5955 * @return current cursor Y
5957 public final int getCursorY() {
5958 return currentState
.cursorY
;
5962 * Read function runs on a separate thread.
5964 public final void run() {
5965 boolean utf8
= false;
5966 boolean done
= false;
5968 if (type
== DeviceType
.XTERM
) {
5972 // available() will often return > 1, so we need to read in chunks to
5974 char [] readBufferUTF8
= null;
5975 byte [] readBuffer
= null;
5977 readBufferUTF8
= new char[128];
5979 readBuffer
= new byte[128];
5982 while (!done
&& !stopReaderThread
) {
5984 int n
= inputStream
.available();
5986 // System.err.printf("available() %d\n", n); System.err.flush();
5988 if (readBufferUTF8
.length
< n
) {
5989 // The buffer wasn't big enough, make it huger
5990 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
5993 readBufferUTF8
= new char[newSizeHalf
* 2];
5996 if (readBuffer
.length
< n
) {
5997 // The buffer wasn't big enough, make it huger
5998 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
5999 readBuffer
= new byte[newSizeHalf
* 2];
6005 } catch (InterruptedException e
) {
6014 rc
= input
.read(readBufferUTF8
, 0,
6015 readBufferUTF8
.length
);
6017 rc
= inputStream
.read(readBuffer
, 0,
6020 } catch (ReadTimeoutException e
) {
6024 // System.err.printf("read() %d\n", rc); System.err.flush();
6029 for (int i
= 0; i
< rc
; i
++) {
6032 ch
= readBufferUTF8
[i
];
6037 synchronized (this) {
6038 // Don't step on UI events
6042 // Permit my enclosing UI to know that I updated.
6043 if (listener
!= null) {
6044 listener
.displayChanged();
6047 // System.err.println("end while loop"); System.err.flush();
6048 } catch (IOException e
) {
6049 e
.printStackTrace();
6053 } // while ((done == false) && (stopReaderThread == false))
6055 // Let the rest of the world know that I am done.
6056 stopReaderThread
= true;
6059 inputStream
.cancelRead();
6060 inputStream
.close();
6062 } catch (IOException e
) {
6068 } catch (IOException e
) {
6072 // System.err.println("*** run() exiting..."); System.err.flush();