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 public 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 outputStream
.flush();
225 for (int i
= 0; i
< str
.length(); i
++) {
226 outputStream
.write(str
.charAt(i
));
228 outputStream
.flush();
229 } catch (IOException e
) {
235 if (output
== null) {
242 } catch (IOException e
) {
248 throw new IllegalArgumentException("Invalid device type: " + type
);
253 * Close the input and output streams and stop the reader thread. Note
254 * that it is safe to call this multiple times.
256 public final void close() {
258 // Tell the reader thread to stop looking at input. It will close
259 // the input streams.
260 if (stopReaderThread
== false) {
261 stopReaderThread
= true;
263 readerThread
.join(1000);
264 } catch (InterruptedException e
) {
269 // Now close the output stream.
274 if (outputStream
!= null) {
276 outputStream
.close();
277 } catch (IOException e
) {
284 if (outputStream
!= null) {
286 outputStream
.close();
287 } catch (IOException e
) {
292 if (output
!= null) {
295 } catch (IOException e
) {
302 throw new IllegalArgumentException("Invalid device type: " +
308 * The enclosing listening object.
310 private DisplayListener listener
;
313 * Set a listening object.
315 * @param listener the object that will have displayChanged() called
316 * after bytes are received from the remote terminal
318 public void setListener(final DisplayListener listener
) {
319 this.listener
= listener
;
323 * When true, the reader thread is expected to exit.
325 private volatile boolean stopReaderThread
= false;
330 private Thread readerThread
= null;
333 * See if the reader thread is still running.
335 * @return if true, we are still connected to / reading from the remote
338 public final boolean isReading() {
339 return (!stopReaderThread
);
343 * The type of emulator to be.
345 private DeviceType type
= DeviceType
.VT102
;
348 * Obtain a new blank display line for an external user
349 * (e.g. TTerminalWindow).
351 * @return new blank line
353 public final DisplayLine
getBlankDisplayLine() {
354 return new DisplayLine(currentState
.attr
);
358 * The scrollback buffer characters + attributes.
360 private volatile List
<DisplayLine
> scrollback
;
363 * Get the scrollback buffer.
365 * @return the scrollback buffer
367 public final List
<DisplayLine
> getScrollbackBuffer() {
372 * The raw display buffer characters + attributes.
374 private volatile List
<DisplayLine
> display
;
377 * Get the display buffer.
379 * @return the display buffer
381 public final List
<DisplayLine
> getDisplayBuffer() {
386 * The terminal's input. For type == XTERM, this is an InputStreamReader
387 * with UTF-8 encoding.
389 private Reader input
;
392 * The terminal's raw InputStream. This is used for type != XTERM.
394 private volatile TimeoutInputStream inputStream
;
397 * The terminal's output. For type == XTERM, this wraps an
398 * OutputStreamWriter with UTF-8 encoding.
400 private Writer output
;
403 * The terminal's raw OutputStream. This is used for type != XTERM.
405 private OutputStream outputStream
;
408 * Parser character scan states.
425 VT52_DIRECT_CURSOR_ADDRESS
429 * Current scanning state.
431 private ScanState scanState
;
434 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
435 * can't really use it in keypress() because we do not see number pad
436 * events from TKeypress.
438 private enum KeypadMode
{
444 * Arrow keys can emit three different sequences (DECCKM or VT52
447 private enum ArrowKeyMode
{
454 * Available character sets for GL, GR, G0, G1, G2, G3.
456 private enum CharacterSet
{
477 * Single-shift states used by the C1 control characters SS2 (0x8E) and
480 private enum Singleshift
{
487 * VT220+ lockshift states.
489 private enum LockshiftMode
{
499 * XTERM mouse reporting protocols.
501 private enum MouseProtocol
{
510 * Which mouse protocol is active.
512 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
515 * XTERM mouse reporting encodings.
517 private enum MouseEncoding
{
524 * Which mouse encoding is active.
526 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
529 * Physical display width. We start at 80x24, but the user can resize us
535 * Get the display width.
537 * @return the width (usually 80 or 132)
539 public final int getWidth() {
544 * Set the display width.
546 * @param width the new width
548 public final void setWidth(final int width
) {
550 rightMargin
= width
- 1;
551 if (currentState
.cursorX
>= width
) {
552 currentState
.cursorX
= width
- 1;
554 if (savedState
.cursorX
>= width
) {
555 savedState
.cursorX
= width
- 1;
560 * Physical display height. We start at 80x24, but the user can resize
566 * Get the display height.
568 * @return the height (usually 24)
570 public final int getHeight() {
575 * Set the display height.
577 * @param height the new height
579 public final void setHeight(final int height
) {
580 this.height
= height
;
581 scrollRegionBottom
= height
- 1;
582 if (scrollRegionTop
>= scrollRegionBottom
) {
585 if (currentState
.cursorY
>= height
) {
586 currentState
.cursorY
= height
- 1;
588 if (savedState
.cursorY
>= height
) {
589 savedState
.cursorY
= height
- 1;
591 while (display
.size() < height
) {
592 DisplayLine line
= new DisplayLine(currentState
.attr
);
593 line
.setReverseColor(reverseVideo
);
596 while (display
.size() > height
) {
597 scrollback
.add(display
.remove(0));
602 * Top margin of the scrolling region.
604 private int scrollRegionTop
;
607 * Bottom margin of the scrolling region.
609 private int scrollRegionBottom
;
612 * Right margin column number. This can be selected by the remote side
613 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
615 private int rightMargin
;
618 * Last character printed.
623 * VT100-style line wrapping: a character is placed in column 80 (or
624 * 132), but the line does NOT wrap until another character is written to
625 * column 1 of the next line, after which the cursor moves to column 2.
627 private boolean wrapLineFlag
;
630 * VT220 single shift flag.
632 private Singleshift singleshift
= Singleshift
.NONE
;
635 * true = insert characters, false = overwrite.
637 private boolean insertMode
= false;
640 * VT52 mode as selected by DECANM. True means VT52, false means
641 * ANSI. Default is ANSI.
643 private boolean vt52Mode
= false;
646 * Visible cursor (DECTCEM).
648 private boolean cursorVisible
= true;
651 * Get visible cursor flag.
653 * @return if true, the cursor is visible
655 public final boolean isCursorVisible() {
656 return cursorVisible
;
660 * Screen title as set by the xterm OSC sequence. Lots of applications
661 * send a screenTitle regardless of whether it is an xterm client or not.
663 private String screenTitle
= "";
666 * Get the screen title as set by the xterm OSC sequence. Lots of
667 * applications send a screenTitle regardless of whether it is an xterm
670 * @return screen title
672 public final String
getScreenTitle() {
677 * Parameter characters being collected.
679 private List
<Integer
> csiParams
;
682 * Non-csi collect buffer.
684 private StringBuilder collectBuffer
;
687 * When true, use the G1 character set.
689 private boolean shiftOut
= false;
692 * Horizontal tab stop locations.
694 private List
<Integer
> tabStops
;
697 * S8C1T. True means 8bit controls, false means 7bit controls.
699 private boolean s8c1t
= false;
702 * Printer mode. True means send all output to printer, which discards
705 private boolean printerControllerMode
= false;
708 * LMN line mode. If true, linefeed() puts the cursor on the first
709 * column of the next line. If false, linefeed() puts the cursor one
710 * line down on the current line. The default is false.
712 private boolean newLineMode
= false;
715 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
717 private ArrowKeyMode arrowKeyMode
;
720 * Whether number pad keys send VT100 or VT52, application or numeric
723 @SuppressWarnings("unused")
724 private KeypadMode keypadMode
;
727 * When true, the terminal is in 132-column mode (DECCOLM).
729 private boolean columns132
= false;
732 * Get 132 columns value.
734 * @return if true, the terminal is in 132 column mode
736 public final boolean isColumns132() {
741 * true = reverse video. Set by DECSCNM.
743 private boolean reverseVideo
= false;
746 * false = echo characters locally.
748 private boolean fullDuplex
= true;
751 * DECSC/DECRC save/restore a subset of the total state. This class
752 * encapsulates those specific flags/modes.
754 private class SaveableState
{
757 * When true, cursor positions are relative to the scrolling region.
759 public boolean originMode
= false;
762 * The current editing X position.
764 public int cursorX
= 0;
767 * The current editing Y position.
769 public int cursorY
= 0;
772 * Which character set is currently selected in G0.
774 public CharacterSet g0Charset
= CharacterSet
.US
;
777 * Which character set is currently selected in G1.
779 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
782 * Which character set is currently selected in G2.
784 public CharacterSet g2Charset
= CharacterSet
.US
;
787 * Which character set is currently selected in G3.
789 public CharacterSet g3Charset
= CharacterSet
.US
;
792 * Which character set is currently selected in GR.
794 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
797 * The current drawing attributes.
799 public CellAttributes attr
;
804 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
809 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
814 public boolean lineWrap
= true;
819 public void reset() {
823 g0Charset
= CharacterSet
.US
;
824 g1Charset
= CharacterSet
.DRAWING
;
825 g2Charset
= CharacterSet
.US
;
826 g3Charset
= CharacterSet
.US
;
827 grCharset
= CharacterSet
.DRAWING
;
828 attr
= new CellAttributes();
829 glLockshift
= LockshiftMode
.NONE
;
830 grLockshift
= LockshiftMode
.NONE
;
835 * Copy attributes from another instance.
837 * @param that the other instance to match
839 public void setTo(final SaveableState that
) {
840 this.originMode
= that
.originMode
;
841 this.cursorX
= that
.cursorX
;
842 this.cursorY
= that
.cursorY
;
843 this.g0Charset
= that
.g0Charset
;
844 this.g1Charset
= that
.g1Charset
;
845 this.g2Charset
= that
.g2Charset
;
846 this.g3Charset
= that
.g3Charset
;
847 this.grCharset
= that
.grCharset
;
848 this.attr
= new CellAttributes();
849 this.attr
.setTo(that
.attr
);
850 this.glLockshift
= that
.glLockshift
;
851 this.grLockshift
= that
.grLockshift
;
852 this.lineWrap
= that
.lineWrap
;
856 * Public constructor.
858 public SaveableState() {
864 * The current terminal state.
866 private SaveableState currentState
;
869 * The last saved terminal state.
871 private SaveableState savedState
;
874 * Clear the CSI parameters and flags.
876 private void toGround() {
878 collectBuffer
= new StringBuilder(8);
879 scanState
= ScanState
.GROUND
;
883 * Reset the tab stops list.
885 private void resetTabStops() {
887 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
888 tabStops
.add(new Integer(i
* 8));
893 * Reset the emulation state.
895 private void reset() {
897 currentState
= new SaveableState();
898 savedState
= new SaveableState();
899 scanState
= ScanState
.GROUND
;
903 scrollRegionBottom
= height
- 1;
904 rightMargin
= width
- 1;
906 arrowKeyMode
= ArrowKeyMode
.ANSI
;
907 keypadMode
= KeypadMode
.Numeric
;
908 wrapLineFlag
= false;
916 reverseVideo
= false;
918 cursorVisible
= true;
921 singleshift
= Singleshift
.NONE
;
923 printerControllerMode
= false;
926 mouseProtocol
= MouseProtocol
.OFF
;
927 mouseEncoding
= MouseEncoding
.X10
;
937 * Public constructor.
939 * @param type one of the DeviceType constants to select VT100, VT102,
941 * @param inputStream an InputStream connected to the remote side. For
942 * type == XTERM, inputStream is converted to a Reader with UTF-8
944 * @param outputStream an OutputStream connected to the remote user. For
945 * type == XTERM, outputStream is converted to a Writer with UTF-8
947 * @throws UnsupportedEncodingException if an exception is thrown when
948 * creating the InputStreamReader
950 public ECMA48(final DeviceType type
, final InputStream inputStream
,
951 final OutputStream outputStream
) throws UnsupportedEncodingException
{
953 assert (inputStream
!= null);
954 assert (outputStream
!= null);
956 csiParams
= new ArrayList
<Integer
>();
957 tabStops
= new ArrayList
<Integer
>();
958 scrollback
= new LinkedList
<DisplayLine
>();
959 display
= new LinkedList
<DisplayLine
>();
962 if (inputStream
instanceof TimeoutInputStream
) {
963 this.inputStream
= (TimeoutInputStream
)inputStream
;
965 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
967 if (type
== DeviceType
.XTERM
) {
968 this.input
= new InputStreamReader(this.inputStream
, "UTF-8");
969 this.output
= new OutputStreamWriter(new
970 BufferedOutputStream(outputStream
), "UTF-8");
971 this.outputStream
= null;
974 this.outputStream
= new BufferedOutputStream(outputStream
);
978 for (int i
= 0; i
< height
; i
++) {
979 display
.add(new DisplayLine(currentState
.attr
));
982 // Spin up the input reader
983 readerThread
= new Thread(this);
984 readerThread
.start();
988 * Append a new line to the bottom of the display, adding lines off the
989 * top to the scrollback buffer.
991 private void newDisplayLine() {
992 // Scroll the top line off into the scrollback buffer
993 scrollback
.add(display
.get(0));
995 DisplayLine line
= new DisplayLine(currentState
.attr
);
996 line
.setReverseColor(reverseVideo
);
1001 * Wraps the current line.
1003 private void wrapCurrentLine() {
1004 if (currentState
.cursorY
== height
- 1) {
1007 if (currentState
.cursorY
< height
- 1) {
1008 currentState
.cursorY
++;
1010 currentState
.cursorX
= 0;
1014 * Handle a carriage return.
1016 private void carriageReturn() {
1017 currentState
.cursorX
= 0;
1018 wrapLineFlag
= false;
1022 * Reverse the color of the visible display.
1024 private void invertDisplayColors() {
1025 for (DisplayLine line
: display
) {
1026 line
.setReverseColor(!line
.isReverseColor());
1031 * Handle a linefeed.
1033 private void linefeed() {
1035 if (currentState
.cursorY
< scrollRegionBottom
) {
1036 // Increment screen y
1037 currentState
.cursorY
++;
1040 // Screen y does not increment
1043 * Two cases: either we're inside a scrolling region or not. If
1044 * the scrolling region bottom is the bottom of the screen, then
1045 * push the top line into the buffer. Else scroll the scrolling
1048 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1050 // We're at the bottom of the scroll region, AND the scroll
1051 // region is the entire screen.
1057 // We're at the bottom of the scroll region, AND the scroll
1058 // region is NOT the entire screen.
1059 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1064 currentState
.cursorX
= 0;
1066 wrapLineFlag
= false;
1070 * Prints one character to the display buffer.
1072 * @param ch character to display
1074 private void printCharacter(final char ch
) {
1075 int rightMargin
= this.rightMargin
;
1077 // Check if we have double-width, and if so chop at 40/66 instead of
1079 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1080 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1083 // Check the unusually-complicated line wrapping conditions...
1084 if (currentState
.cursorX
== rightMargin
) {
1086 if (currentState
.lineWrap
== true) {
1088 * This case happens when: the cursor was already on the
1089 * right margin (either through printing or by an explicit
1090 * placement command), and a character was printed.
1092 * The line wraps only when a new character arrives AND the
1093 * cursor is already on the right margin AND has placed a
1094 * character in its cell. Easier to see than to explain.
1096 if (wrapLineFlag
== false) {
1098 * This block marks the case that we are in the margin
1099 * and the first character has been received and printed.
1101 wrapLineFlag
= true;
1104 * This block marks the case that we are in the margin
1105 * and the second character has been received and
1108 wrapLineFlag
= false;
1112 } else if (currentState
.cursorX
<= rightMargin
) {
1114 * This is the normal case: a character came in and was printed
1115 * to the left of the right margin column.
1118 // Turn off VT100 special-case flag
1119 wrapLineFlag
= false;
1122 // "Print" the character
1123 Cell newCell
= new Cell(ch
);
1124 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1125 newCellAttributes
.setTo(currentState
.attr
);
1126 DisplayLine line
= display
.get(currentState
.cursorY
);
1127 // Insert mode special case
1128 if (insertMode
== true) {
1129 line
.insert(currentState
.cursorX
, newCell
);
1131 // Replace an existing character
1132 line
.replace(currentState
.cursorX
, newCell
);
1135 // Increment horizontal
1136 if (wrapLineFlag
== false) {
1137 currentState
.cursorX
++;
1138 if (currentState
.cursorX
> rightMargin
) {
1139 currentState
.cursorX
--;
1145 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1146 * send to the remote side.
1148 * @param mouse mouse event received from the local user
1150 public void mouse(final TMouseEvent mouse
) {
1153 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1154 mouseProtocol, mouseEncoding, mouse);
1157 if (mouseEncoding
== MouseEncoding
.X10
) {
1158 // We will support X10 but only for (160,94) and smaller.
1159 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1164 switch (mouseProtocol
) {
1171 // Only report button presses
1172 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1178 // Only report button presses and releases
1179 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1180 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1188 * Only report button presses, button releases, and motions that
1189 * have a button down (i.e. drag-and-drop).
1191 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1192 if (!mouse
.isMouse1()
1193 && !mouse
.isMouse2()
1194 && !mouse
.isMouse3()
1195 && !mouse
.isMouseWheelUp()
1196 && !mouse
.isMouseWheelDown()
1204 // Report everything
1208 // Now encode the event
1209 StringBuilder sb
= new StringBuilder(6);
1210 if (mouseEncoding
== MouseEncoding
.SGR
) {
1211 sb
.append((char) 0x1B);
1214 if (mouse
.isMouse1()) {
1215 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1220 } else if (mouse
.isMouse2()) {
1221 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1226 } else if (mouse
.isMouse3()) {
1227 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1232 } else if (mouse
.isMouseWheelUp()) {
1234 } else if (mouse
.isMouseWheelDown()) {
1237 // This is motion with no buttons down.
1241 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1244 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1251 // X10 and UTF8 encodings
1252 sb
.append((char) 0x1B);
1255 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1256 sb
.append((char) (0x03 + 32));
1257 } else if (mouse
.isMouse1()) {
1258 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1259 sb
.append((char) (0x00 + 32 + 32));
1261 sb
.append((char) (0x00 + 32));
1263 } else if (mouse
.isMouse2()) {
1264 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1265 sb
.append((char) (0x01 + 32 + 32));
1267 sb
.append((char) (0x01 + 32));
1269 } else if (mouse
.isMouse3()) {
1270 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1271 sb
.append((char) (0x02 + 32 + 32));
1273 sb
.append((char) (0x02 + 32));
1275 } else if (mouse
.isMouseWheelUp()) {
1276 sb
.append((char) (0x04 + 64));
1277 } else if (mouse
.isMouseWheelDown()) {
1278 sb
.append((char) (0x05 + 64));
1280 // This is motion with no buttons down.
1281 sb
.append((char) (0x03 + 32));
1284 sb
.append((char) (mouse
.getX() + 33));
1285 sb
.append((char) (mouse
.getY() + 33));
1288 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1289 writeRemote(sb
.toString());
1293 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1294 * send to the remote side.
1296 * @param keypress keypress received from the local user
1298 public void keypress(final TKeypress keypress
) {
1299 writeRemote(keypressToString(keypress
));
1303 * Build one of the complex xterm keystroke sequences, storing the result in
1304 * xterm_keystroke_buffer.
1306 * @param ss3 the prefix to use based on VT100 state.
1307 * @param first the first character, usually a number.
1308 * @param first the last character, one of the following: ~ A B C D F H
1309 * @param ctrl whether or not ctrl is down
1310 * @param alt whether or not alt is down
1311 * @param shift whether or not shift is down
1312 * @return the buffer with the full key sequence
1314 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1315 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1317 StringBuilder sb
= new StringBuilder(ss3
);
1318 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1322 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1324 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1326 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1328 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1330 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1332 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1334 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1339 return sb
.toString();
1343 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1345 * @param keypress keypress received from the local user
1346 * @return string to transmit to the remote side
1348 private String
keypressToString(final TKeypress keypress
) {
1350 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1352 * If this is a control character, process it like it came from
1355 if (keypress
.getChar() < 0x20) {
1356 handleControlChar(keypress
.getChar());
1358 // Local echo for everything else
1359 printCharacter(keypress
.getChar());
1363 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1368 // Handle control characters
1369 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1370 StringBuilder sb
= new StringBuilder();
1371 char ch
= keypress
.getChar();
1374 return sb
.toString();
1377 // Handle alt characters
1378 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1379 StringBuilder sb
= new StringBuilder("\033");
1380 char ch
= keypress
.getChar();
1382 return sb
.toString();
1385 if (keypress
.equals(kbBackspace
)) {
1398 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1401 switch (arrowKeyMode
) {
1403 return xtermBuildKeySequence("\033[", '1', 'D',
1404 keypress
.isCtrl(), keypress
.isAlt(),
1405 keypress
.isShift());
1407 return xtermBuildKeySequence("\033", '1', 'D',
1408 keypress
.isCtrl(), keypress
.isAlt(),
1409 keypress
.isShift());
1411 return xtermBuildKeySequence("\033O", '1', 'D',
1412 keypress
.isCtrl(), keypress
.isAlt(),
1413 keypress
.isShift());
1416 switch (arrowKeyMode
) {
1427 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1430 switch (arrowKeyMode
) {
1432 return xtermBuildKeySequence("\033[", '1', 'C',
1433 keypress
.isCtrl(), keypress
.isAlt(),
1434 keypress
.isShift());
1436 return xtermBuildKeySequence("\033", '1', 'C',
1437 keypress
.isCtrl(), keypress
.isAlt(),
1438 keypress
.isShift());
1440 return xtermBuildKeySequence("\033O", '1', 'C',
1441 keypress
.isCtrl(), keypress
.isAlt(),
1442 keypress
.isShift());
1445 switch (arrowKeyMode
) {
1456 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1459 switch (arrowKeyMode
) {
1461 return xtermBuildKeySequence("\033[", '1', 'A',
1462 keypress
.isCtrl(), keypress
.isAlt(),
1463 keypress
.isShift());
1465 return xtermBuildKeySequence("\033", '1', 'A',
1466 keypress
.isCtrl(), keypress
.isAlt(),
1467 keypress
.isShift());
1469 return xtermBuildKeySequence("\033O", '1', 'A',
1470 keypress
.isCtrl(), keypress
.isAlt(),
1471 keypress
.isShift());
1474 switch (arrowKeyMode
) {
1485 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1488 switch (arrowKeyMode
) {
1490 return xtermBuildKeySequence("\033[", '1', 'B',
1491 keypress
.isCtrl(), keypress
.isAlt(),
1492 keypress
.isShift());
1494 return xtermBuildKeySequence("\033", '1', 'B',
1495 keypress
.isCtrl(), keypress
.isAlt(),
1496 keypress
.isShift());
1498 return xtermBuildKeySequence("\033O", '1', 'B',
1499 keypress
.isCtrl(), keypress
.isAlt(),
1500 keypress
.isShift());
1503 switch (arrowKeyMode
) {
1514 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1517 switch (arrowKeyMode
) {
1519 return xtermBuildKeySequence("\033[", '1', 'H',
1520 keypress
.isCtrl(), keypress
.isAlt(),
1521 keypress
.isShift());
1523 return xtermBuildKeySequence("\033", '1', 'H',
1524 keypress
.isCtrl(), keypress
.isAlt(),
1525 keypress
.isShift());
1527 return xtermBuildKeySequence("\033O", '1', 'H',
1528 keypress
.isCtrl(), keypress
.isAlt(),
1529 keypress
.isShift());
1532 switch (arrowKeyMode
) {
1543 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1546 switch (arrowKeyMode
) {
1548 return xtermBuildKeySequence("\033[", '1', 'F',
1549 keypress
.isCtrl(), keypress
.isAlt(),
1550 keypress
.isShift());
1552 return xtermBuildKeySequence("\033", '1', 'F',
1553 keypress
.isCtrl(), keypress
.isAlt(),
1554 keypress
.isShift());
1556 return xtermBuildKeySequence("\033O", '1', 'F',
1557 keypress
.isCtrl(), keypress
.isAlt(),
1558 keypress
.isShift());
1561 switch (arrowKeyMode
) {
1572 if (keypress
.equals(kbF1
)) {
1580 if (keypress
.equals(kbF2
)) {
1588 if (keypress
.equals(kbF3
)) {
1596 if (keypress
.equals(kbF4
)) {
1604 if (keypress
.equals(kbF5
)) {
1617 if (keypress
.equals(kbF6
)) {
1630 if (keypress
.equals(kbF7
)) {
1643 if (keypress
.equals(kbF8
)) {
1656 if (keypress
.equals(kbF9
)) {
1669 if (keypress
.equals(kbF10
)) {
1682 if (keypress
.equals(kbF11
)) {
1686 if (keypress
.equals(kbF12
)) {
1690 if (keypress
.equals(kbShiftF1
)) {
1695 if (type
== DeviceType
.XTERM
) {
1701 if (keypress
.equals(kbShiftF2
)) {
1706 if (type
== DeviceType
.XTERM
) {
1712 if (keypress
.equals(kbShiftF3
)) {
1717 if (type
== DeviceType
.XTERM
) {
1723 if (keypress
.equals(kbShiftF4
)) {
1728 if (type
== DeviceType
.XTERM
) {
1734 if (keypress
.equals(kbShiftF5
)) {
1736 return "\033[15;2~";
1739 if (keypress
.equals(kbShiftF6
)) {
1741 return "\033[17;2~";
1744 if (keypress
.equals(kbShiftF7
)) {
1746 return "\033[18;2~";
1749 if (keypress
.equals(kbShiftF8
)) {
1751 return "\033[19;2~";
1754 if (keypress
.equals(kbShiftF9
)) {
1756 return "\033[20;2~";
1759 if (keypress
.equals(kbShiftF10
)) {
1761 return "\033[21;2~";
1764 if (keypress
.equals(kbShiftF11
)) {
1766 return "\033[23;2~";
1769 if (keypress
.equals(kbShiftF12
)) {
1771 return "\033[24;2~";
1774 if (keypress
.equals(kbCtrlF1
)) {
1779 if (type
== DeviceType
.XTERM
) {
1785 if (keypress
.equals(kbCtrlF2
)) {
1790 if (type
== DeviceType
.XTERM
) {
1796 if (keypress
.equals(kbCtrlF3
)) {
1801 if (type
== DeviceType
.XTERM
) {
1807 if (keypress
.equals(kbCtrlF4
)) {
1812 if (type
== DeviceType
.XTERM
) {
1818 if (keypress
.equals(kbCtrlF5
)) {
1820 return "\033[15;5~";
1823 if (keypress
.equals(kbCtrlF6
)) {
1825 return "\033[17;5~";
1828 if (keypress
.equals(kbCtrlF7
)) {
1830 return "\033[18;5~";
1833 if (keypress
.equals(kbCtrlF8
)) {
1835 return "\033[19;5~";
1838 if (keypress
.equals(kbCtrlF9
)) {
1840 return "\033[20;5~";
1843 if (keypress
.equals(kbCtrlF10
)) {
1845 return "\033[21;5~";
1848 if (keypress
.equals(kbCtrlF11
)) {
1850 return "\033[23;5~";
1853 if (keypress
.equals(kbCtrlF12
)) {
1855 return "\033[24;5~";
1858 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
1861 return xtermBuildKeySequence("\033[", '5', '~',
1862 keypress
.isCtrl(), keypress
.isAlt(),
1863 keypress
.isShift());
1869 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
1872 return xtermBuildKeySequence("\033[", '6', '~',
1873 keypress
.isCtrl(), keypress
.isAlt(),
1874 keypress
.isShift());
1880 if (keypress
.equalsWithoutModifiers(kbIns
)) {
1883 return xtermBuildKeySequence("\033[", '2', '~',
1884 keypress
.isCtrl(), keypress
.isAlt(),
1885 keypress
.isShift());
1891 if (keypress
.equalsWithoutModifiers(kbDel
)) {
1894 return xtermBuildKeySequence("\033[", '3', '~',
1895 keypress
.isCtrl(), keypress
.isAlt(),
1896 keypress
.isShift());
1898 // Delete sends real delete for VTxxx
1903 if (keypress
.equals(kbEnter
)) {
1907 if (keypress
.equals(kbEsc
)) {
1911 if (keypress
.equals(kbAltEsc
)) {
1915 if (keypress
.equals(kbTab
)) {
1919 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
1920 (keypress
.equals(kbShiftTab
))
1930 // Non-alt, non-ctrl characters
1931 if (!keypress
.isFnKey()) {
1932 StringBuilder sb
= new StringBuilder();
1933 sb
.append(keypress
.getChar());
1934 return sb
.toString();
1940 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1943 * @param ch 8-bit character from the remote side
1944 * @param charsetGl character set defined for GL
1945 * @param charsetGr character set defined for GR
1946 * @return character to display on the screen
1948 private char mapCharacterCharset(final char ch
,
1949 final CharacterSet charsetGl
,
1950 final CharacterSet charsetGr
) {
1952 int lookupChar
= ch
;
1953 CharacterSet lookupCharset
= charsetGl
;
1956 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
1957 lookupCharset
= charsetGr
;
1961 switch (lookupCharset
) {
1964 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
1967 return DECCharacterSets
.UK
[lookupChar
];
1970 return DECCharacterSets
.US_ASCII
[lookupChar
];
1973 return DECCharacterSets
.NL
[lookupChar
];
1976 return DECCharacterSets
.FI
[lookupChar
];
1979 return DECCharacterSets
.FR
[lookupChar
];
1982 return DECCharacterSets
.FR_CA
[lookupChar
];
1985 return DECCharacterSets
.DE
[lookupChar
];
1988 return DECCharacterSets
.IT
[lookupChar
];
1991 return DECCharacterSets
.NO
[lookupChar
];
1994 return DECCharacterSets
.ES
[lookupChar
];
1997 return DECCharacterSets
.SV
[lookupChar
];
2000 return DECCharacterSets
.SWISS
[lookupChar
];
2002 case DEC_SUPPLEMENTAL
:
2003 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2006 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2009 return DECCharacterSets
.US_ASCII
[lookupChar
];
2012 return DECCharacterSets
.US_ASCII
[lookupChar
];
2015 throw new IllegalArgumentException("Invalid character set value: "
2021 * Map an 8-bit byte into a printable character.
2023 * @param ch either 8-bit or Unicode character from the remote side
2024 * @return character to display on the screen
2026 private char mapCharacter(final char ch
) {
2028 // Unicode character, just return it
2032 CharacterSet charsetGl
= currentState
.g0Charset
;
2033 CharacterSet charsetGr
= currentState
.grCharset
;
2035 if (vt52Mode
== true) {
2036 if (shiftOut
== true) {
2037 // Shifted out character, pull from VT52 graphics
2038 charsetGl
= currentState
.g1Charset
;
2039 charsetGr
= CharacterSet
.US
;
2042 charsetGl
= currentState
.g0Charset
;
2043 charsetGr
= CharacterSet
.US
;
2046 // Pull the character
2047 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2051 if (shiftOut
== true) {
2052 // Shifted out character, pull from G1
2053 charsetGl
= currentState
.g1Charset
;
2054 charsetGr
= currentState
.grCharset
;
2056 // Pull the character
2057 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2061 if (singleshift
== Singleshift
.SS2
) {
2063 singleshift
= Singleshift
.NONE
;
2065 // Shifted out character, pull from G2
2066 charsetGl
= currentState
.g2Charset
;
2067 charsetGr
= currentState
.grCharset
;
2071 if (singleshift
== Singleshift
.SS3
) {
2073 singleshift
= Singleshift
.NONE
;
2075 // Shifted out character, pull from G3
2076 charsetGl
= currentState
.g3Charset
;
2077 charsetGr
= currentState
.grCharset
;
2080 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2081 // Check for locking shift
2083 switch (currentState
.glLockshift
) {
2096 charsetGl
= currentState
.g2Charset
;
2101 charsetGl
= currentState
.g3Charset
;
2106 charsetGl
= currentState
.g0Charset
;
2110 switch (currentState
.grLockshift
) {
2120 charsetGr
= currentState
.g1Charset
;
2125 charsetGr
= currentState
.g2Charset
;
2130 charsetGr
= currentState
.g3Charset
;
2135 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2142 // Pull the character
2143 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2147 * Scroll the text within a scrolling region up n lines.
2149 * @param regionTop top row of the scrolling region
2150 * @param regionBottom bottom row of the scrolling region
2151 * @param n number of lines to scroll
2153 private void scrollingRegionScrollUp(final int regionTop
,
2154 final int regionBottom
, final int n
) {
2156 if (regionTop
>= regionBottom
) {
2160 // Sanity check: see if there will be any characters left after the
2162 if (regionBottom
+ 1 - regionTop
<= n
) {
2163 // There won't be anything left in the region, so just call
2164 // eraseScreen() and return.
2165 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2169 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2170 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2171 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2173 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2174 - remaining
, regionBottom
+ 1);
2175 display
= new LinkedList
<DisplayLine
>(displayTop
);
2176 display
.addAll(displayMiddle
);
2177 for (int i
= 0; i
< n
; i
++) {
2178 DisplayLine line
= new DisplayLine(currentState
.attr
);
2179 line
.setReverseColor(reverseVideo
);
2182 display
.addAll(displayBottom
);
2184 assert (display
.size() == height
);
2188 * Scroll the text within a scrolling region down n lines.
2190 * @param regionTop top row of the scrolling region
2191 * @param regionBottom bottom row of the scrolling region
2192 * @param n number of lines to scroll
2194 private void scrollingRegionScrollDown(final int regionTop
,
2195 final int regionBottom
, final int n
) {
2197 if (regionTop
>= regionBottom
) {
2201 // Sanity check: see if there will be any characters left after the
2203 if (regionBottom
+ 1 - regionTop
<= n
) {
2204 // There won't be anything left in the region, so just call
2205 // eraseScreen() and return.
2206 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2210 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2211 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2212 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2214 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2215 regionTop
+ remaining
);
2216 display
= new LinkedList
<DisplayLine
>(displayTop
);
2217 for (int i
= 0; i
< n
; i
++) {
2218 DisplayLine line
= new DisplayLine(currentState
.attr
);
2219 line
.setReverseColor(reverseVideo
);
2222 display
.addAll(displayMiddle
);
2223 display
.addAll(displayBottom
);
2225 assert (display
.size() == height
);
2229 * Process a control character.
2231 * @param ch 8-bit character from the remote side
2233 private void handleControlChar(final char ch
) {
2234 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2245 // Transmit the answerback message.
2256 cursorLeft(1, false);
2261 advanceToNextTabStop();
2287 currentState
.glLockshift
= LockshiftMode
.NONE
;
2293 currentState
.glLockshift
= LockshiftMode
.NONE
;
2318 singleshift
= Singleshift
.SS2
;
2323 singleshift
= Singleshift
.SS3
;
2333 * Advance the cursor to the next tab stop.
2335 private void advanceToNextTabStop() {
2336 if (tabStops
.size() == 0) {
2337 // Go to the rightmost column
2338 cursorRight(rightMargin
- currentState
.cursorX
, false);
2341 for (Integer stop
: tabStops
) {
2342 if (stop
> currentState
.cursorX
) {
2343 cursorRight(stop
- currentState
.cursorX
, false);
2348 * We got here, meaning there isn't a tab stop beyond the current
2349 * cursor position. Place the cursor of the right-most edge of the
2352 cursorRight(rightMargin
- currentState
.cursorX
, false);
2356 * Save a character into the collect buffer.
2358 * @param ch character to save
2360 private void collect(final char ch
) {
2361 collectBuffer
.append(ch
);
2365 * Save a byte into the CSI parameters buffer.
2367 * @param ch byte to save
2369 private void param(final byte ch
) {
2370 if (csiParams
.size() == 0) {
2371 csiParams
.add(new Integer(0));
2373 Integer x
= csiParams
.get(csiParams
.size() - 1);
2374 if ((ch
>= '0') && (ch
<= '9')) {
2377 csiParams
.set(csiParams
.size() - 1, x
);
2381 csiParams
.add(new Integer(0));
2386 * Get a CSI parameter value, with a default.
2388 * @param position parameter index. 0 is the first parameter.
2389 * @param defaultValue value to use if csiParams[position] doesn't exist
2390 * @return parameter value
2392 private int getCsiParam(final int position
, final int defaultValue
) {
2393 if (csiParams
.size() < position
+ 1) {
2394 return defaultValue
;
2396 return csiParams
.get(position
).intValue();
2400 * Get a CSI parameter value, clamped to within min/max.
2402 * @param position parameter index. 0 is the first parameter.
2403 * @param defaultValue value to use if csiParams[position] doesn't exist
2404 * @param minValue minimum value inclusive
2405 * @param maxValue maximum value inclusive
2406 * @return parameter value
2408 private int getCsiParam(final int position
, final int defaultValue
,
2409 final int minValue
, final int maxValue
) {
2411 assert (minValue
<= maxValue
);
2412 int value
= getCsiParam(position
, defaultValue
);
2413 if (value
< minValue
) {
2416 if (value
> maxValue
) {
2423 * Set or unset a toggle.
2425 * @param value true for set ('h'), false for reset ('l')
2427 private void setToggle(final boolean value
) {
2428 boolean decPrivateModeFlag
= false;
2429 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2430 if (collectBuffer
.charAt(i
) == '?') {
2431 decPrivateModeFlag
= true;
2436 for (Integer i
: csiParams
) {
2441 if (decPrivateModeFlag
== true) {
2443 if (value
== true) {
2444 // Use application arrow keys
2445 arrowKeyMode
= ArrowKeyMode
.VT100
;
2447 // Use ANSI arrow keys
2448 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2453 if (decPrivateModeFlag
== true) {
2454 if (value
== false) {
2458 arrowKeyMode
= ArrowKeyMode
.VT52
;
2461 * From the VT102 docs: "You use ANSI mode to select
2462 * most terminal features; the terminal uses the same
2463 * features when it switches to VT52 mode. You
2464 * cannot, however, change most of these features in
2467 * In other words, do not reset any other attributes
2468 * when switching between VT52 submode and ANSI.
2470 * HOWEVER, the real vt100 does switch the character
2471 * set according to Usenet.
2473 currentState
.g0Charset
= CharacterSet
.US
;
2474 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2477 if ((type
== DeviceType
.VT220
)
2478 || (type
== DeviceType
.XTERM
)) {
2480 // VT52 mode is explicitly 7-bit
2482 singleshift
= Singleshift
.NONE
;
2487 if (value
== true) {
2488 // Turn off keyboard
2497 if (decPrivateModeFlag
== true) {
2499 if (value
== true) {
2508 width
= rightMargin
+ 1;
2509 // Entire screen is cleared, and scrolling region is
2511 eraseScreen(0, 0, height
- 1, width
- 1, false);
2512 scrollRegionTop
= 0;
2513 scrollRegionBottom
= height
- 1;
2514 // Also home the cursor
2515 cursorPosition(0, 0);
2519 if (decPrivateModeFlag
== true) {
2521 if (value
== true) {
2530 if (value
== true) {
2538 if (decPrivateModeFlag
== true) {
2540 if (value
== true) {
2542 * Set selects reverse screen, a white screen
2543 * background with black characters.
2545 if (reverseVideo
!= true) {
2547 * If in normal video, switch it back
2549 invertDisplayColors();
2551 reverseVideo
= true;
2554 * Reset selects normal screen, a black screen
2555 * background with white characters.
2557 if (reverseVideo
== true) {
2559 * If in reverse video already, switch it back
2561 invertDisplayColors();
2563 reverseVideo
= false;
2568 if (decPrivateModeFlag
== true) {
2570 if (value
== true) {
2571 // Origin is relative to scroll region cursor.
2572 // Cursor can NEVER leave scrolling region.
2573 currentState
.originMode
= true;
2574 cursorPosition(0, 0);
2576 // Origin is absolute to entire screen. Cursor can
2577 // leave the scrolling region via cup() and hvp().
2578 currentState
.originMode
= false;
2579 cursorPosition(0, 0);
2584 if (decPrivateModeFlag
== true) {
2586 if (value
== true) {
2588 currentState
.lineWrap
= true;
2590 // Turn linewrap off
2591 currentState
.lineWrap
= false;
2596 if (decPrivateModeFlag
== true) {
2598 if (value
== true) {
2599 // Keyboard auto-repeat on
2602 // Keyboard auto-repeat off
2608 if (decPrivateModeFlag
== false) {
2610 if (value
== true) {
2620 if (decPrivateModeFlag
== true) {
2626 if (decPrivateModeFlag
== true) {
2632 if (decPrivateModeFlag
== false) {
2634 if (value
== true) {
2636 * Set causes a received linefeed, form feed, or
2637 * vertical tab to move cursor to first column of
2638 * next line. RETURN transmits both a carriage return
2639 * and linefeed. This selection is also called new
2645 * Reset causes a received linefeed, form feed, or
2646 * vertical tab to move cursor to next line in
2647 * current column. RETURN transmits a carriage
2650 newLineMode
= false;
2656 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2657 if (decPrivateModeFlag
== true) {
2659 if (value
== true) {
2661 cursorVisible
= true;
2664 cursorVisible
= false;
2671 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2672 if (decPrivateModeFlag
== true) {
2674 if (value
== true) {
2675 // Select national mode NRC
2678 // Select multi-national mode
2687 if ((type
== DeviceType
.XTERM
)
2688 && (decPrivateModeFlag
== true)
2690 // Mouse: normal tracking mode
2691 if (value
== true) {
2692 mouseProtocol
= MouseProtocol
.NORMAL
;
2694 mouseProtocol
= MouseProtocol
.OFF
;
2700 if ((type
== DeviceType
.XTERM
)
2701 && (decPrivateModeFlag
== true)
2703 // Mouse: normal tracking mode
2704 if (value
== true) {
2705 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2707 mouseProtocol
= MouseProtocol
.OFF
;
2713 if ((type
== DeviceType
.XTERM
)
2714 && (decPrivateModeFlag
== true)
2716 // Mouse: Any-event tracking mode
2717 if (value
== true) {
2718 mouseProtocol
= MouseProtocol
.ANYEVENT
;
2720 mouseProtocol
= MouseProtocol
.OFF
;
2726 if ((type
== DeviceType
.XTERM
)
2727 && (decPrivateModeFlag
== true)
2729 // Mouse: UTF-8 coordinates
2730 if (value
== true) {
2731 mouseEncoding
= MouseEncoding
.UTF8
;
2733 mouseEncoding
= MouseEncoding
.X10
;
2739 if ((type
== DeviceType
.XTERM
)
2740 && (decPrivateModeFlag
== true)
2742 // Mouse: SGR coordinates
2743 if (value
== true) {
2744 mouseEncoding
= MouseEncoding
.SGR
;
2746 mouseEncoding
= MouseEncoding
.X10
;
2759 * DECSC - Save cursor.
2761 private void decsc() {
2762 savedState
.setTo(currentState
);
2766 * DECRC - Restore cursor.
2768 private void decrc() {
2769 currentState
.setTo(savedState
);
2775 private void ind() {
2776 // Move the cursor and scroll if necessary. If at the bottom line
2777 // already, a scroll up is supposed to be performed.
2778 if (currentState
.cursorY
== scrollRegionBottom
) {
2779 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2781 cursorDown(1, true);
2785 * RI - Reverse index.
2788 // Move the cursor and scroll if necessary. If at the top line
2789 // already, a scroll down is supposed to be performed.
2790 if (currentState
.cursorY
== scrollRegionTop
) {
2791 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2799 private void nel() {
2800 // Move the cursor and scroll if necessary. If at the bottom line
2801 // already, a scroll up is supposed to be performed.
2802 if (currentState
.cursorY
== scrollRegionBottom
) {
2803 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2805 cursorDown(1, true);
2807 // Reset to the beginning of the next line
2808 currentState
.cursorX
= 0;
2812 * DECKPAM - Keypad application mode.
2814 private void deckpam() {
2815 keypadMode
= KeypadMode
.Application
;
2819 * DECKPNM - Keypad numeric mode.
2821 private void deckpnm() {
2822 keypadMode
= KeypadMode
.Numeric
;
2828 * @param n number of spaces to move
2829 * @param honorScrollRegion if true, then do nothing if the cursor is
2830 * outside the scrolling region
2832 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2836 * Special case: if a user moves the cursor from the right margin, we
2837 * have to reset the VT100 right margin flag.
2840 wrapLineFlag
= false;
2843 for (int i
= 0; i
< n
; i
++) {
2844 if (honorScrollRegion
== true) {
2845 // Honor the scrolling region
2846 if ((currentState
.cursorY
< scrollRegionTop
)
2847 || (currentState
.cursorY
> scrollRegionBottom
)
2849 // Outside region, do nothing
2852 // Inside region, go up
2853 top
= scrollRegionTop
;
2855 // Non-scrolling case
2859 if (currentState
.cursorY
> top
) {
2860 currentState
.cursorY
--;
2866 * Move down n spaces.
2868 * @param n number of spaces to move
2869 * @param honorScrollRegion if true, then do nothing if the cursor is
2870 * outside the scrolling region
2872 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
2876 * Special case: if a user moves the cursor from the right margin, we
2877 * have to reset the VT100 right margin flag.
2880 wrapLineFlag
= false;
2883 for (int i
= 0; i
< n
; i
++) {
2885 if (honorScrollRegion
== true) {
2886 // Honor the scrolling region
2887 if (currentState
.cursorY
> scrollRegionBottom
) {
2888 // Outside region, do nothing
2891 // Inside region, go down
2892 bottom
= scrollRegionBottom
;
2894 // Non-scrolling case
2895 bottom
= height
- 1;
2898 if (currentState
.cursorY
< bottom
) {
2899 currentState
.cursorY
++;
2905 * Move left n spaces.
2907 * @param n number of spaces to move
2908 * @param honorScrollRegion if true, then do nothing if the cursor is
2909 * outside the scrolling region
2911 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
2913 * Special case: if a user moves the cursor from the right margin, we
2914 * have to reset the VT100 right margin flag.
2917 wrapLineFlag
= false;
2920 for (int i
= 0; i
< n
; i
++) {
2921 if (honorScrollRegion
== true) {
2922 // Honor the scrolling region
2923 if ((currentState
.cursorY
< scrollRegionTop
)
2924 || (currentState
.cursorY
> scrollRegionBottom
)
2926 // Outside region, do nothing
2931 if (currentState
.cursorX
> 0) {
2932 currentState
.cursorX
--;
2938 * Move right n spaces.
2940 * @param n number of spaces to move
2941 * @param honorScrollRegion if true, then do nothing if the cursor is
2942 * outside the scrolling region
2944 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
2945 int rightMargin
= this.rightMargin
;
2948 * Special case: if a user moves the cursor from the right margin, we
2949 * have to reset the VT100 right margin flag.
2952 wrapLineFlag
= false;
2955 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2956 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2959 for (int i
= 0; i
< n
; i
++) {
2960 if (honorScrollRegion
== true) {
2961 // Honor the scrolling region
2962 if ((currentState
.cursorY
< scrollRegionTop
)
2963 || (currentState
.cursorY
> scrollRegionBottom
)
2965 // Outside region, do nothing
2970 if (currentState
.cursorX
< rightMargin
) {
2971 currentState
.cursorX
++;
2977 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2979 * @param row row to move to
2980 * @param col column to move to
2982 private void cursorPosition(int row
, final int col
) {
2983 int rightMargin
= this.rightMargin
;
2988 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2989 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2992 // Set column number
2993 currentState
.cursorX
= col
;
2995 // Sanity check, bring column back to margin.
2996 if (currentState
.cursorX
> rightMargin
) {
2997 currentState
.cursorX
= rightMargin
;
3001 if (currentState
.originMode
== true) {
3002 row
+= scrollRegionTop
;
3004 if (currentState
.cursorY
< row
) {
3005 cursorDown(row
- currentState
.cursorY
, false);
3006 } else if (currentState
.cursorY
> row
) {
3007 cursorUp(currentState
.cursorY
- row
, false);
3010 wrapLineFlag
= false;
3014 * HTS - Horizontal tabulation set.
3016 private void hts() {
3017 for (Integer stop
: tabStops
) {
3018 if (stop
== currentState
.cursorX
) {
3019 // Already have a tab stop here
3024 // Append a tab stop to the end of the array and resort them
3025 tabStops
.add(currentState
.cursorX
);
3026 Collections
.sort(tabStops
);
3030 * DECSWL - Single-width line.
3032 private void decswl() {
3033 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3034 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3038 * DECDWL - Double-width line.
3040 private void decdwl() {
3041 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3042 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3046 * DECHDL - Double-height + double-width line.
3048 * @param topHalf if true, this sets the row to be the top half row of a
3051 private void dechdl(final boolean topHalf
) {
3052 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3053 if (topHalf
== true) {
3054 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3056 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3061 * DECALN - Screen alignment display.
3063 private void decaln() {
3064 Cell newCell
= new Cell();
3065 newCell
.setChar('E');
3066 for (DisplayLine line
: display
) {
3067 for (int i
= 0; i
< line
.length(); i
++) {
3068 line
.replace(i
, newCell
);
3074 * DECSCL - Compatibility level.
3076 private void decscl() {
3077 int i
= getCsiParam(0, 0);
3078 int j
= getCsiParam(1, 0);
3082 currentState
.g0Charset
= CharacterSet
.US
;
3083 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3085 } else if (i
== 62) {
3087 if ((j
== 0) || (j
== 2)) {
3089 } else if (j
== 1) {
3096 * CUD - Cursor down.
3098 private void cud() {
3099 cursorDown(getCsiParam(0, 1, 1, height
), true);
3103 * CUF - Cursor forward.
3105 private void cuf() {
3106 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3110 * CUB - Cursor backward.
3112 private void cub() {
3113 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3119 private void cuu() {
3120 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3124 * CUP - Cursor position.
3126 private void cup() {
3127 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3128 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3132 * CNL - Cursor down and to column 1.
3134 private void cnl() {
3135 cursorDown(getCsiParam(0, 1, 1, height
), true);
3137 cursorLeft(currentState
.cursorX
, true);
3141 * CPL - Cursor up and to column 1.
3143 private void cpl() {
3144 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3146 cursorLeft(currentState
.cursorX
, true);
3150 * CHA - Cursor to column # in current row.
3152 private void cha() {
3153 cursorPosition(currentState
.cursorY
,
3154 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3158 * VPA - Cursor to row #, same column.
3160 private void vpa() {
3161 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3162 currentState
.cursorX
);
3166 * ED - Erase in display.
3169 boolean honorProtected
= false;
3170 boolean decPrivateModeFlag
= false;
3172 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3173 if (collectBuffer
.charAt(i
) == '?') {
3174 decPrivateModeFlag
= true;
3179 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3180 && (decPrivateModeFlag
== true)
3182 honorProtected
= true;
3185 int i
= getCsiParam(0, 0);
3188 // Erase from here to end of screen
3189 if (currentState
.cursorY
< height
- 1) {
3190 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3193 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3194 } else if (i
== 1) {
3195 // Erase from beginning of screen to here
3196 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3198 eraseLine(0, currentState
.cursorX
, honorProtected
);
3199 } else if (i
== 2) {
3200 // Erase entire screen
3201 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3206 * EL - Erase in line.
3209 boolean honorProtected
= false;
3210 boolean decPrivateModeFlag
= false;
3212 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3213 if (collectBuffer
.charAt(i
) == '?') {
3214 decPrivateModeFlag
= true;
3219 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3220 && (decPrivateModeFlag
== true)
3222 honorProtected
= true;
3225 int i
= getCsiParam(0, 0);
3228 // Erase from here to end of line
3229 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3230 } else if (i
== 1) {
3231 // Erase from beginning of line to here
3232 eraseLine(0, currentState
.cursorX
, honorProtected
);
3233 } else if (i
== 2) {
3234 // Erase entire line
3235 eraseLine(0, width
- 1, honorProtected
);
3240 * ECH - Erase # of characters in current row.
3242 private void ech() {
3243 int i
= getCsiParam(0, 1, 1, width
);
3245 // Erase from here to i characters
3246 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3253 int i
= getCsiParam(0, 1);
3255 if ((currentState
.cursorY
>= scrollRegionTop
)
3256 && (currentState
.cursorY
<= scrollRegionBottom
)
3259 // I can get the same effect with a scroll-down
3260 scrollingRegionScrollDown(currentState
.cursorY
,
3261 scrollRegionBottom
, i
);
3266 * DCH - Delete char.
3268 private void dch() {
3269 int n
= getCsiParam(0, 1);
3270 DisplayLine line
= display
.get(currentState
.cursorY
);
3271 Cell blank
= new Cell();
3272 for (int i
= 0; i
< n
; i
++) {
3273 line
.delete(currentState
.cursorX
, blank
);
3278 * ICH - Insert blank char at cursor.
3280 private void ich() {
3281 int n
= getCsiParam(0, 1);
3282 DisplayLine line
= display
.get(currentState
.cursorY
);
3283 Cell blank
= new Cell();
3284 for (int i
= 0; i
< n
; i
++) {
3285 line
.insert(currentState
.cursorX
, blank
);
3293 int i
= getCsiParam(0, 1);
3295 if ((currentState
.cursorY
>= scrollRegionTop
)
3296 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3298 // I can get the same effect with a scroll-down
3299 scrollingRegionScrollUp(currentState
.cursorY
,
3300 scrollRegionBottom
, i
);
3305 * HVP - Horizontal and vertical position.
3307 private void hvp() {
3312 * REP - Repeat character.
3314 private void rep() {
3315 int n
= getCsiParam(0, 1);
3316 for (int i
= 0; i
< n
; i
++) {
3317 printCharacter(repCh
);
3325 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3326 getCsiParam(0, 1, 1, height
));
3333 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3334 getCsiParam(0, 1, 1, height
));
3338 * CBT - Go back X tab stops.
3340 private void cbt() {
3341 int tabsToMove
= getCsiParam(0, 1);
3344 for (int i
= 0; i
< tabsToMove
; i
++) {
3345 int j
= currentState
.cursorX
;
3346 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3347 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3355 j
= tabStops
.get(tabI
);
3357 cursorPosition(currentState
.cursorY
, j
);
3362 * CHT - Advance X tab stops.
3364 private void cht() {
3365 int n
= getCsiParam(0, 1);
3366 for (int i
= 0; i
< n
; i
++) {
3367 advanceToNextTabStop();
3372 * SGR - Select graphics rendition.
3374 private void sgr() {
3376 if (csiParams
.size() == 0) {
3377 currentState
.attr
.reset();
3381 for (Integer i
: csiParams
) {
3387 currentState
.attr
.reset();
3392 currentState
.attr
.setBold(true);
3397 currentState
.attr
.setUnderline(true);
3402 currentState
.attr
.setBlink(true);
3407 currentState
.attr
.setReverse(true);
3414 if (type
== DeviceType
.XTERM
) {
3428 if ((type
== DeviceType
.VT220
)
3429 || (type
== DeviceType
.XTERM
)) {
3435 currentState
.attr
.setBold(false);
3440 currentState
.attr
.setUnderline(false);
3445 currentState
.attr
.setBlink(false);
3450 currentState
.attr
.setReverse(false);
3458 // A true VT100/102/220 does not support color, however everyone
3459 // is used to their terminal emulator supporting color so we will
3460 // unconditionally support color for all DeviceType's.
3465 // Set black foreground
3466 currentState
.attr
.setForeColor(Color
.BLACK
);
3469 // Set red foreground
3470 currentState
.attr
.setForeColor(Color
.RED
);
3473 // Set green foreground
3474 currentState
.attr
.setForeColor(Color
.GREEN
);
3477 // Set yellow foreground
3478 currentState
.attr
.setForeColor(Color
.YELLOW
);
3481 // Set blue foreground
3482 currentState
.attr
.setForeColor(Color
.BLUE
);
3485 // Set magenta foreground
3486 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3489 // Set cyan foreground
3490 currentState
.attr
.setForeColor(Color
.CYAN
);
3493 // Set white foreground
3494 currentState
.attr
.setForeColor(Color
.WHITE
);
3497 if (type
== DeviceType
.XTERM
) {
3499 * Xterm supports T.416 / ISO-8613-3 codes to select
3500 * either an indexed color or an RGB value. (It also
3501 * permits these ISO-8613-3 SGR sequences to be separated
3502 * by colons rather than semicolons.)
3504 * We will not support any of these additional color
3505 * codes at this time:
3507 * 1. http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
3508 * has a detailed discussion of the current state of
3509 * RGB in various terminals, the point of which is
3510 * that none of them really do the same thing despite
3511 * all appearing to be "xterm".
3514 * https://bugs.kde.org/show_bug.cgi?id=107487#c3,
3515 * even supporting just the "indexed mode" of these
3516 * sequences (which could align easily with existing
3517 * SGR colors) is assumed to mean full support of
3518 * 24-bit RGB. So it is all or nothing.
3520 * Finally, these sequences break the assumptions of
3521 * standard ECMA-48 style parsers as pointed out at
3522 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
3523 * Therefore in order to keep a clean display, we cannot
3524 * parse anything else in this sequence.
3528 // Underscore on, default foreground color
3529 currentState
.attr
.setUnderline(true);
3530 currentState
.attr
.setForeColor(Color
.WHITE
);
3534 // Underscore off, default foreground color
3535 currentState
.attr
.setUnderline(false);
3536 currentState
.attr
.setForeColor(Color
.WHITE
);
3539 // Set black background
3540 currentState
.attr
.setBackColor(Color
.BLACK
);
3543 // Set red background
3544 currentState
.attr
.setBackColor(Color
.RED
);
3547 // Set green background
3548 currentState
.attr
.setBackColor(Color
.GREEN
);
3551 // Set yellow background
3552 currentState
.attr
.setBackColor(Color
.YELLOW
);
3555 // Set blue background
3556 currentState
.attr
.setBackColor(Color
.BLUE
);
3559 // Set magenta background
3560 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3563 // Set cyan background
3564 currentState
.attr
.setBackColor(Color
.CYAN
);
3567 // Set white background
3568 currentState
.attr
.setBackColor(Color
.WHITE
);
3571 if (type
== DeviceType
.XTERM
) {
3573 * Xterm supports T.416 / ISO-8613-3 codes to select
3574 * either an indexed color or an RGB value. (It also
3575 * permits these ISO-8613-3 SGR sequences to be separated
3576 * by colons rather than semicolons.)
3578 * We will not support this at this time. Also, in order
3579 * to keep a clean display, we cannot parse anything else
3586 // Default background
3587 currentState
.attr
.setBackColor(Color
.BLACK
);
3597 * DA - Device attributes.
3600 int extendedFlag
= 0;
3602 if (collectBuffer
.length() > 0) {
3603 String args
= collectBuffer
.substring(1);
3604 if (collectBuffer
.charAt(0) == '>') {
3606 if (collectBuffer
.length() >= 2) {
3607 i
= Integer
.parseInt(args
.toString());
3609 } else if (collectBuffer
.charAt(0) == '=') {
3611 if (collectBuffer
.length() >= 2) {
3612 i
= Integer
.parseInt(args
.toString());
3615 // Unknown code, bail out
3620 if ((i
!= 0) && (i
!= 1)) {
3624 if ((extendedFlag
== 0) && (i
== 0)) {
3625 // Send string directly to remote side
3626 writeRemote(deviceTypeResponse());
3630 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3632 if ((extendedFlag
== 1) && (i
== 0)) {
3634 * Request "What type of terminal are you, what is your
3635 * firmware version, and what hardware options do you have
3638 * Respond: "I am a VT220 (identification code of 1), my
3639 * firmware version is _____ (Pv), and I have _____ Po
3640 * options installed."
3646 if (s8c1t
== true) {
3647 writeRemote("\u009b>1;10;0c");
3649 writeRemote("\033[>1;10;0c");
3655 if ((extendedFlag
== 2) && (i
== 0)) {
3658 * Request "What is your unit ID?"
3660 * Respond: "I was manufactured at site 00 and have a unique ID
3664 writeRemote("\033P!|00010203\033\\");
3669 * DECSTBM - Set top and bottom margins.
3671 private void decstbm() {
3672 boolean decPrivateModeFlag
= false;
3674 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3675 if (collectBuffer
.charAt(i
) == '?') {
3676 decPrivateModeFlag
= true;
3680 if (decPrivateModeFlag
) {
3681 // This could be restore DEC private mode values.
3685 int top
= getCsiParam(0, 1, 1, height
) - 1;
3686 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3691 scrollRegionTop
= top
;
3692 scrollRegionBottom
= bottom
;
3695 cursorPosition(0, 0);
3700 * DECREQTPARM - Request terminal parameters.
3702 private void decreqtparm() {
3703 int i
= getCsiParam(0, 0);
3705 if ((i
!= 0) && (i
!= 1)) {
3712 * Request terminal parameters.
3716 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3717 * (CLoCk MULtiplier = 1, STP option flags = 0)
3721 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3724 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3726 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3732 * DECSCA - Select Character Attributes.
3734 private void decsca() {
3735 int i
= getCsiParam(0, 0);
3737 if ((i
== 0) || (i
== 2)) {
3739 currentState
.attr
.setProtect(false);
3743 currentState
.attr
.setProtect(true);
3748 * DECSTR - Soft Terminal Reset.
3750 private void decstr() {
3751 // Do exactly like RIS - Reset to initial state
3753 // Do I clear screen too? I think so...
3754 eraseScreen(0, 0, height
- 1, width
- 1, false);
3755 cursorPosition(0, 0);
3759 * DSR - Device status report.
3761 private void dsr() {
3762 boolean decPrivateModeFlag
= false;
3763 int row
= currentState
.cursorY
;
3765 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3766 if (collectBuffer
.charAt(i
) == '?') {
3767 decPrivateModeFlag
= true;
3772 int i
= getCsiParam(0, 0);
3777 // Request status report. Respond with "OK, no malfunction."
3779 // Send string directly to remote side
3780 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3783 writeRemote("\u009b0n");
3785 writeRemote("\033[0n");
3790 // Request cursor position. Respond with current position.
3791 if (currentState
.originMode
== true) {
3792 row
-= scrollRegionTop
;
3795 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3798 str
= String
.format("\u009b%d;%dR", row
+ 1,
3799 currentState
.cursorX
+ 1);
3801 str
= String
.format("\033[%d;%dR", row
+ 1,
3802 currentState
.cursorX
+ 1);
3805 // Send string directly to remote side
3810 if (decPrivateModeFlag
== true) {
3812 // Request printer status report. Respond with "Printer not
3815 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3816 && (s8c1t
== true)) {
3817 writeRemote("\u009b?13n");
3819 writeRemote("\033[?13n");
3825 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3826 && (decPrivateModeFlag
== true)
3829 // Request user-defined keys are locked or unlocked. Respond
3830 // with "User-defined keys are locked."
3832 if (s8c1t
== true) {
3833 writeRemote("\u009b?21n");
3835 writeRemote("\033[?21n");
3841 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3842 && (decPrivateModeFlag
== true)
3845 // Request keyboard language. Respond with "Keyboard
3846 // language is North American."
3848 if (s8c1t
== true) {
3849 writeRemote("\u009b?27;1n");
3851 writeRemote("\033[?27;1n");
3858 // Some other option, ignore
3864 * TBC - Tabulation clear.
3866 private void tbc() {
3867 int i
= getCsiParam(0, 0);
3869 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3870 for (Integer stop
: tabStops
) {
3871 if (stop
== currentState
.cursorX
) {
3876 tabStops
= newStops
;
3884 * Erase the characters in the current line from the start column to the
3885 * end column, inclusive.
3887 * @param start starting column to erase (between 0 and width - 1)
3888 * @param end ending column to erase (between 0 and width - 1)
3889 * @param honorProtected if true, do not erase characters with the
3890 * protected attribute set
3892 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3897 if (end
> width
- 1) {
3904 for (int i
= start
; i
<= end
; i
++) {
3905 DisplayLine line
= display
.get(currentState
.cursorY
);
3906 if ((!honorProtected
)
3907 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
3914 * From the VT102 manual:
3916 * Erasing a character also erases any character
3917 * attribute of the character.
3923 * Erase with the current color a.k.a. back-color erase
3926 line
.setChar(i
, ' ');
3927 line
.setAttr(i
, currentState
.attr
);
3935 * Erase a rectangular section of the screen, inclusive. end column,
3938 * @param startRow starting row to erase (between 0 and height - 1)
3939 * @param startCol starting column to erase (between 0 and width - 1)
3940 * @param endRow ending row to erase (between 0 and height - 1)
3941 * @param endCol ending column to erase (between 0 and width - 1)
3942 * @param honorProtected if true, do not erase characters with the
3943 * protected attribute set
3945 private void eraseScreen(final int startRow
, final int startCol
,
3946 final int endRow
, final int endCol
, final boolean honorProtected
) {
3954 || (endRow
< startRow
)
3955 || (endCol
< startCol
)
3960 oldCursorY
= currentState
.cursorY
;
3961 for (int i
= startRow
; i
<= endRow
; i
++) {
3962 currentState
.cursorY
= i
;
3963 eraseLine(startCol
, endCol
, honorProtected
);
3965 // Erase display clears the double attributes
3966 display
.get(i
).setDoubleWidth(false);
3967 display
.get(i
).setDoubleHeight(0);
3969 currentState
.cursorY
= oldCursorY
;
3973 * VT220 printer functions. All of these are parsed, but won't do
3976 private void printerFunctions() {
3977 boolean decPrivateModeFlag
= false;
3978 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3979 if (collectBuffer
.charAt(i
) == '?') {
3980 decPrivateModeFlag
= true;
3985 int i
= getCsiParam(0, 0);
3990 if (decPrivateModeFlag
== false) {
3996 if (decPrivateModeFlag
== true) {
3997 // Print cursor line
4002 if (decPrivateModeFlag
== true) {
4003 // Auto print mode OFF
4005 // Printer controller OFF
4007 // Characters re-appear on the screen
4008 printerControllerMode
= false;
4013 if (decPrivateModeFlag
== true) {
4017 // Printer controller
4019 // Characters get sucked into oblivion
4020 printerControllerMode
= true;
4031 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4032 * of remote systems will send an XTerm title sequence even if TERM isn't
4035 * @param xtermChar the character received from the remote side
4037 private void oscPut(final char xtermChar
) {
4039 collectBuffer
.append(xtermChar
);
4042 if (xtermChar
== 0x07) {
4043 String args
= collectBuffer
.substring(0,
4044 collectBuffer
.length() - 1);
4045 String
[] p
= args
.toString().split(";");
4047 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4055 // Go to SCAN_GROUND state
4062 * Run this input character through the ECMA48 state machine.
4064 * @param ch character from the remote side
4066 private void consume(char ch
) {
4069 // System.err.printf("%c", ch);
4071 // Special case for VT10x: 7-bit characters only
4072 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4073 ch
= (char)(ch
& 0x7F);
4076 // Special "anywhere" states
4078 // 18, 1A --> execute, then switch to SCAN_GROUND
4079 if ((ch
== 0x18) || (ch
== 0x1A)) {
4080 // CAN and SUB abort escape sequences
4085 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4089 && (scanState
!= ScanState
.DCS_ENTRY
)
4090 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4091 && (scanState
!= ScanState
.DCS_IGNORE
)
4092 && (scanState
!= ScanState
.DCS_PARAM
)
4093 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4096 scanState
= ScanState
.ESCAPE
;
4100 // 0x9B == CSI 8-bit sequence
4102 scanState
= ScanState
.CSI_ENTRY
;
4106 // 0x9D goes to ScanState.OSC_STRING
4108 scanState
= ScanState
.OSC_STRING
;
4112 // 0x90 goes to DCS_ENTRY
4114 scanState
= ScanState
.DCS_ENTRY
;
4118 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4119 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4120 scanState
= ScanState
.SOSPMAPC_STRING
;
4124 // 0x7F (DEL) is always discarded
4129 switch (scanState
) {
4132 // 00-17, 19, 1C-1F --> execute
4133 // 80-8F, 91-9A, 9C --> execute
4134 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4135 handleControlChar(ch
);
4139 if (((ch
>= 0x20) && (ch
<= 0x7F))
4143 // VT220 printer --> trash bin
4144 if (((type
== DeviceType
.VT220
)
4145 || (type
== DeviceType
.XTERM
))
4146 && (printerControllerMode
== true)
4151 // Hang onto this character
4152 repCh
= mapCharacter(ch
);
4154 // Print this character
4155 printCharacter(repCh
);
4160 // 00-17, 19, 1C-1F --> execute
4162 handleControlChar(ch
);
4166 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4167 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4169 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4173 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4174 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4185 // DECSC - Save cursor
4186 // Note this code overlaps both ANSI and VT52 mode
4191 // DECRC - Restore cursor
4192 // Note this code overlaps both ANSI and VT52 mode
4201 if (vt52Mode
== true) {
4202 // DECANM - Enter ANSI mode
4204 arrowKeyMode
= ArrowKeyMode
.VT100
;
4207 * From the VT102 docs: "You use ANSI mode to select
4208 * most terminal features; the terminal uses the same
4209 * features when it switches to VT52 mode. You
4210 * cannot, however, change most of these features in
4213 * In other words, do not reset any other attributes
4214 * when switching between VT52 submode and ANSI.
4218 currentState
.g0Charset
= CharacterSet
.US
;
4219 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4221 singleshift
= Singleshift
.NONE
;
4222 currentState
.glLockshift
= LockshiftMode
.NONE
;
4223 currentState
.grLockshift
= LockshiftMode
.NONE
;
4227 // DECKPAM - Keypad application mode
4228 // Note this code overlaps both ANSI and VT52 mode
4232 // DECKPNM - Keypad numeric mode
4233 // Note this code overlaps both ANSI and VT52 mode
4240 if (vt52Mode
== true) {
4241 // Cursor up, and stop at the top without scrolling
4246 if (vt52Mode
== true) {
4247 // Cursor down, and stop at the bottom without scrolling
4248 cursorDown(1, false);
4252 if (vt52Mode
== true) {
4253 // Cursor right, and stop at the right without scrolling
4254 cursorRight(1, false);
4258 if (vt52Mode
== true) {
4259 // Cursor left, and stop at the left without scrolling
4260 cursorLeft(1, false);
4267 if (vt52Mode
== true) {
4275 if (vt52Mode
== true) {
4276 // G0 --> Special graphics
4277 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
4281 if (vt52Mode
== true) {
4283 currentState
.g0Charset
= CharacterSet
.US
;
4287 if (vt52Mode
== true) {
4289 cursorPosition(0, 0);
4291 // HTS - Horizontal tabulation set
4296 if (vt52Mode
== true) {
4297 // Reverse line feed. Same as RI.
4302 if (vt52Mode
== true) {
4303 // Erase to end of screen
4304 eraseLine(currentState
.cursorX
, width
- 1, false);
4305 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4310 if (vt52Mode
== true) {
4311 // Erase to end of line
4312 eraseLine(currentState
.cursorX
, width
- 1, false);
4318 if (vt52Mode
== true) {
4321 // RI - Reverse index
4326 if (vt52Mode
== false) {
4328 singleshift
= Singleshift
.SS2
;
4332 if (vt52Mode
== false) {
4334 singleshift
= Singleshift
.SS3
;
4341 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4357 if (vt52Mode
== true) {
4358 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4366 if (vt52Mode
== true) {
4368 // Send string directly to remote side
4369 writeRemote("\033/Z");
4372 // Send string directly to remote side
4373 writeRemote(deviceTypeResponse());
4384 // VT52 cannot get to any of these other states
4385 if (vt52Mode
== true) {
4390 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4397 // RIS - Reset to initial state
4399 // Do I clear screen too? I think so...
4400 eraseScreen(0, 0, height
- 1, width
- 1, false);
4401 cursorPosition(0, 0);
4415 if ((type
== DeviceType
.VT220
)
4416 || (type
== DeviceType
.XTERM
)) {
4418 // VT220 lockshift G2 into GL
4419 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4424 if ((type
== DeviceType
.VT220
)
4425 || (type
== DeviceType
.XTERM
)) {
4427 // VT220 lockshift G3 into GL
4428 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4446 if ((type
== DeviceType
.VT220
)
4447 || (type
== DeviceType
.XTERM
)) {
4449 // VT220 lockshift G3 into GR
4450 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4455 if ((type
== DeviceType
.VT220
)
4456 || (type
== DeviceType
.XTERM
)) {
4458 // VT220 lockshift G2 into GR
4459 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4465 if ((type
== DeviceType
.VT220
)
4466 || (type
== DeviceType
.XTERM
)) {
4468 // VT220 lockshift G1 into GR
4469 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4479 // 0x5B goes to CSI_ENTRY
4481 scanState
= ScanState
.CSI_ENTRY
;
4484 // 0x5D goes to OSC_STRING
4486 scanState
= ScanState
.OSC_STRING
;
4489 // 0x50 goes to DCS_ENTRY
4491 scanState
= ScanState
.DCS_ENTRY
;
4494 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4495 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4496 scanState
= ScanState
.SOSPMAPC_STRING
;
4501 case ESCAPE_INTERMEDIATE
:
4502 // 00-17, 19, 1C-1F --> execute
4504 handleControlChar(ch
);
4507 // 20-2F --> collect
4508 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4512 // 30-7E --> dispatch, then switch to GROUND
4513 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4516 if ((collectBuffer
.length() == 1)
4517 && (collectBuffer
.charAt(0) == '(')) {
4518 // G0 --> Special graphics
4519 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4521 if ((collectBuffer
.length() == 1)
4522 && (collectBuffer
.charAt(0) == ')')) {
4523 // G1 --> Special graphics
4524 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4526 if ((type
== DeviceType
.VT220
)
4527 || (type
== DeviceType
.XTERM
)) {
4529 if ((collectBuffer
.length() == 1)
4530 && (collectBuffer
.charAt(0) == '*')) {
4531 // G2 --> Special graphics
4532 currentState
.g2Charset
= CharacterSet
.DRAWING
;
4534 if ((collectBuffer
.length() == 1)
4535 && (collectBuffer
.charAt(0) == '+')) {
4536 // G3 --> Special graphics
4537 currentState
.g3Charset
= CharacterSet
.DRAWING
;
4542 if ((collectBuffer
.length() == 1)
4543 && (collectBuffer
.charAt(0) == '(')) {
4544 // G0 --> Alternate character ROM standard character set
4545 currentState
.g0Charset
= CharacterSet
.ROM
;
4547 if ((collectBuffer
.length() == 1)
4548 && (collectBuffer
.charAt(0) == ')')) {
4549 // G1 --> Alternate character ROM standard character set
4550 currentState
.g1Charset
= CharacterSet
.ROM
;
4554 if ((collectBuffer
.length() == 1)
4555 && (collectBuffer
.charAt(0) == '(')) {
4556 // G0 --> Alternate character ROM special graphics
4557 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4559 if ((collectBuffer
.length() == 1)
4560 && (collectBuffer
.charAt(0) == ')')) {
4561 // G1 --> Alternate character ROM special graphics
4562 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4566 if ((collectBuffer
.length() == 1)
4567 && (collectBuffer
.charAt(0) == '#')) {
4568 // DECDHL - Double-height line (top half)
4573 if ((collectBuffer
.length() == 1)
4574 && (collectBuffer
.charAt(0) == '#')) {
4575 // DECDHL - Double-height line (bottom half)
4578 if ((type
== DeviceType
.VT220
)
4579 || (type
== DeviceType
.XTERM
)) {
4581 if ((collectBuffer
.length() == 1)
4582 && (collectBuffer
.charAt(0) == '(')) {
4584 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4586 if ((collectBuffer
.length() == 1)
4587 && (collectBuffer
.charAt(0) == ')')) {
4589 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4591 if ((collectBuffer
.length() == 1)
4592 && (collectBuffer
.charAt(0) == '*')) {
4594 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4596 if ((collectBuffer
.length() == 1)
4597 && (collectBuffer
.charAt(0) == '+')) {
4599 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4604 if ((collectBuffer
.length() == 1)
4605 && (collectBuffer
.charAt(0) == '#')) {
4606 // DECSWL - Single-width line
4609 if ((type
== DeviceType
.VT220
)
4610 || (type
== DeviceType
.XTERM
)) {
4612 if ((collectBuffer
.length() == 1)
4613 && (collectBuffer
.charAt(0) == '(')) {
4615 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4617 if ((collectBuffer
.length() == 1)
4618 && (collectBuffer
.charAt(0) == ')')) {
4620 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4622 if ((collectBuffer
.length() == 1)
4623 && (collectBuffer
.charAt(0) == '*')) {
4625 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4627 if ((collectBuffer
.length() == 1)
4628 && (collectBuffer
.charAt(0) == '+')) {
4630 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4635 if ((collectBuffer
.length() == 1)
4636 && (collectBuffer
.charAt(0) == '#')) {
4637 // DECDWL - Double-width line
4640 if ((type
== DeviceType
.VT220
)
4641 || (type
== DeviceType
.XTERM
)) {
4643 if ((collectBuffer
.length() == 1)
4644 && (collectBuffer
.charAt(0) == '(')) {
4646 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4648 if ((collectBuffer
.length() == 1)
4649 && (collectBuffer
.charAt(0) == ')')) {
4651 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4653 if ((collectBuffer
.length() == 1)
4654 && (collectBuffer
.charAt(0) == '*')) {
4656 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4658 if ((collectBuffer
.length() == 1)
4659 && (collectBuffer
.charAt(0) == '+')) {
4661 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4666 if ((type
== DeviceType
.VT220
)
4667 || (type
== DeviceType
.XTERM
)) {
4669 if ((collectBuffer
.length() == 1)
4670 && (collectBuffer
.charAt(0) == '(')) {
4672 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4674 if ((collectBuffer
.length() == 1)
4675 && (collectBuffer
.charAt(0) == ')')) {
4677 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4679 if ((collectBuffer
.length() == 1)
4680 && (collectBuffer
.charAt(0) == '*')) {
4682 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4684 if ((collectBuffer
.length() == 1)
4685 && (collectBuffer
.charAt(0) == '+')) {
4687 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4692 if ((collectBuffer
.length() == 1)
4693 && (collectBuffer
.charAt(0) == '#')) {
4694 // DECALN - Screen alignment display
4703 if ((type
== DeviceType
.VT220
)
4704 || (type
== DeviceType
.XTERM
)) {
4706 if ((collectBuffer
.length() == 1)
4707 && (collectBuffer
.charAt(0) == '(')) {
4708 // G0 --> DEC_SUPPLEMENTAL
4709 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4711 if ((collectBuffer
.length() == 1)
4712 && (collectBuffer
.charAt(0) == ')')) {
4713 // G1 --> DEC_SUPPLEMENTAL
4714 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4716 if ((collectBuffer
.length() == 1)
4717 && (collectBuffer
.charAt(0) == '*')) {
4718 // G2 --> DEC_SUPPLEMENTAL
4719 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4721 if ((collectBuffer
.length() == 1)
4722 && (collectBuffer
.charAt(0) == '+')) {
4723 // G3 --> DEC_SUPPLEMENTAL
4724 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4729 if ((type
== DeviceType
.VT220
)
4730 || (type
== DeviceType
.XTERM
)) {
4732 if ((collectBuffer
.length() == 1)
4733 && (collectBuffer
.charAt(0) == '(')) {
4735 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4737 if ((collectBuffer
.length() == 1)
4738 && (collectBuffer
.charAt(0) == ')')) {
4740 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4742 if ((collectBuffer
.length() == 1)
4743 && (collectBuffer
.charAt(0) == '*')) {
4745 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4747 if ((collectBuffer
.length() == 1)
4748 && (collectBuffer
.charAt(0) == '+')) {
4750 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4759 if ((collectBuffer
.length() == 1)
4760 && (collectBuffer
.charAt(0) == '(')) {
4761 // G0 --> United Kingdom set
4762 currentState
.g0Charset
= CharacterSet
.UK
;
4764 if ((collectBuffer
.length() == 1)
4765 && (collectBuffer
.charAt(0) == ')')) {
4766 // G1 --> United Kingdom set
4767 currentState
.g1Charset
= CharacterSet
.UK
;
4769 if ((type
== DeviceType
.VT220
)
4770 || (type
== DeviceType
.XTERM
)) {
4772 if ((collectBuffer
.length() == 1)
4773 && (collectBuffer
.charAt(0) == '*')) {
4774 // G2 --> United Kingdom set
4775 currentState
.g2Charset
= CharacterSet
.UK
;
4777 if ((collectBuffer
.length() == 1)
4778 && (collectBuffer
.charAt(0) == '+')) {
4779 // G3 --> United Kingdom set
4780 currentState
.g3Charset
= CharacterSet
.UK
;
4785 if ((collectBuffer
.length() == 1)
4786 && (collectBuffer
.charAt(0) == '(')) {
4788 currentState
.g0Charset
= CharacterSet
.US
;
4790 if ((collectBuffer
.length() == 1)
4791 && (collectBuffer
.charAt(0) == ')')) {
4793 currentState
.g1Charset
= CharacterSet
.US
;
4795 if ((type
== DeviceType
.VT220
)
4796 || (type
== DeviceType
.XTERM
)) {
4798 if ((collectBuffer
.length() == 1)
4799 && (collectBuffer
.charAt(0) == '*')) {
4801 currentState
.g2Charset
= CharacterSet
.US
;
4803 if ((collectBuffer
.length() == 1)
4804 && (collectBuffer
.charAt(0) == '+')) {
4806 currentState
.g3Charset
= CharacterSet
.US
;
4811 if ((type
== DeviceType
.VT220
)
4812 || (type
== DeviceType
.XTERM
)) {
4814 if ((collectBuffer
.length() == 1)
4815 && (collectBuffer
.charAt(0) == '(')) {
4817 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4819 if ((collectBuffer
.length() == 1)
4820 && (collectBuffer
.charAt(0) == ')')) {
4822 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4824 if ((collectBuffer
.length() == 1)
4825 && (collectBuffer
.charAt(0) == '*')) {
4827 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4829 if ((collectBuffer
.length() == 1)
4830 && (collectBuffer
.charAt(0) == '+')) {
4832 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4839 if ((type
== DeviceType
.VT220
)
4840 || (type
== DeviceType
.XTERM
)) {
4842 if ((collectBuffer
.length() == 1)
4843 && (collectBuffer
.charAt(0) == '(')) {
4845 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4847 if ((collectBuffer
.length() == 1)
4848 && (collectBuffer
.charAt(0) == ')')) {
4850 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4852 if ((collectBuffer
.length() == 1)
4853 && (collectBuffer
.charAt(0) == '*')) {
4855 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4857 if ((collectBuffer
.length() == 1)
4858 && (collectBuffer
.charAt(0) == '+')) {
4860 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4865 if ((type
== DeviceType
.VT220
)
4866 || (type
== DeviceType
.XTERM
)) {
4868 if ((collectBuffer
.length() == 1)
4869 && (collectBuffer
.charAt(0) == ' ')) {
4876 if ((type
== DeviceType
.VT220
)
4877 || (type
== DeviceType
.XTERM
)) {
4879 if ((collectBuffer
.length() == 1)
4880 && (collectBuffer
.charAt(0) == ' ')) {
4887 if ((type
== DeviceType
.VT220
)
4888 || (type
== DeviceType
.XTERM
)) {
4890 if ((collectBuffer
.length() == 1)
4891 && (collectBuffer
.charAt(0) == '(')) {
4893 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4895 if ((collectBuffer
.length() == 1)
4896 && (collectBuffer
.charAt(0) == ')')) {
4898 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4900 if ((collectBuffer
.length() == 1)
4901 && (collectBuffer
.charAt(0) == '*')) {
4903 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4905 if ((collectBuffer
.length() == 1)
4906 && (collectBuffer
.charAt(0) == '+')) {
4908 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4916 if ((type
== DeviceType
.VT220
)
4917 || (type
== DeviceType
.XTERM
)) {
4919 if ((collectBuffer
.length() == 1)
4920 && (collectBuffer
.charAt(0) == '(')) {
4922 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4924 if ((collectBuffer
.length() == 1)
4925 && (collectBuffer
.charAt(0) == ')')) {
4927 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4929 if ((collectBuffer
.length() == 1)
4930 && (collectBuffer
.charAt(0) == '*')) {
4932 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4934 if ((collectBuffer
.length() == 1)
4935 && (collectBuffer
.charAt(0) == '+')) {
4937 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4948 if ((type
== DeviceType
.VT220
)
4949 || (type
== DeviceType
.XTERM
)) {
4951 if ((collectBuffer
.length() == 1)
4952 && (collectBuffer
.charAt(0) == '(')) {
4954 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4956 if ((collectBuffer
.length() == 1)
4957 && (collectBuffer
.charAt(0) == ')')) {
4959 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4961 if ((collectBuffer
.length() == 1)
4962 && (collectBuffer
.charAt(0) == '*')) {
4964 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4966 if ((collectBuffer
.length() == 1)
4967 && (collectBuffer
.charAt(0) == '+')) {
4969 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4974 if ((type
== DeviceType
.VT220
)
4975 || (type
== DeviceType
.XTERM
)) {
4977 if ((collectBuffer
.length() == 1)
4978 && (collectBuffer
.charAt(0) == '(')) {
4980 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4982 if ((collectBuffer
.length() == 1)
4983 && (collectBuffer
.charAt(0) == ')')) {
4985 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4987 if ((collectBuffer
.length() == 1)
4988 && (collectBuffer
.charAt(0) == '*')) {
4990 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
4992 if ((collectBuffer
.length() == 1)
4993 && (collectBuffer
.charAt(0) == '+')) {
4995 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5007 if ((type
== DeviceType
.VT220
)
5008 || (type
== DeviceType
.XTERM
)) {
5010 if ((collectBuffer
.length() == 1)
5011 && (collectBuffer
.charAt(0) == '(')) {
5013 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5015 if ((collectBuffer
.length() == 1)
5016 && (collectBuffer
.charAt(0) == ')')) {
5018 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5020 if ((collectBuffer
.length() == 1)
5021 && (collectBuffer
.charAt(0) == '*')) {
5023 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5025 if ((collectBuffer
.length() == 1)
5026 && (collectBuffer
.charAt(0) == '+')) {
5028 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5033 if ((type
== DeviceType
.VT220
)
5034 || (type
== DeviceType
.XTERM
)) {
5036 if ((collectBuffer
.length() == 1)
5037 && (collectBuffer
.charAt(0) == '(')) {
5039 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5041 if ((collectBuffer
.length() == 1)
5042 && (collectBuffer
.charAt(0) == ')')) {
5044 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5046 if ((collectBuffer
.length() == 1)
5047 && (collectBuffer
.charAt(0) == '*')) {
5049 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5051 if ((collectBuffer
.length() == 1)
5052 && (collectBuffer
.charAt(0) == '+')) {
5054 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5101 // 0x9C goes to GROUND
5109 // 00-17, 19, 1C-1F --> execute
5111 handleControlChar(ch
);
5114 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5115 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5117 scanState
= ScanState
.CSI_INTERMEDIATE
;
5120 // 30-39, 3B --> param, then switch to CSI_PARAM
5121 if ((ch
>= '0') && (ch
<= '9')) {
5123 scanState
= ScanState
.CSI_PARAM
;
5127 scanState
= ScanState
.CSI_PARAM
;
5130 // 3C-3F --> collect, then switch to CSI_PARAM
5131 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5133 scanState
= ScanState
.CSI_PARAM
;
5136 // 40-7E --> dispatch, then switch to GROUND
5137 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5140 // ICH - Insert character
5148 // CUD - Cursor down
5152 // CUF - Cursor forward
5156 // CUB - Cursor backward
5160 // CNL - Cursor down and to column 1
5161 if (type
== DeviceType
.XTERM
) {
5166 // CPL - Cursor up and to column 1
5167 if (type
== DeviceType
.XTERM
) {
5172 // CHA - Cursor to column # in current row
5173 if (type
== DeviceType
.XTERM
) {
5178 // CUP - Cursor position
5182 // CHT - Cursor forward X tab stops (default 1)
5183 if (type
== DeviceType
.XTERM
) {
5188 // ED - Erase in display
5192 // EL - Erase in line
5207 // DCH - Delete character
5214 // Scroll up X lines (default 1)
5215 if (type
== DeviceType
.XTERM
) {
5220 // Scroll down X lines (default 1)
5221 if (type
== DeviceType
.XTERM
) {
5230 if ((type
== DeviceType
.VT220
)
5231 || (type
== DeviceType
.XTERM
)) {
5233 // ECH - Erase character
5240 // CBT - Cursor backward X tab stops (default 1)
5241 if (type
== DeviceType
.XTERM
) {
5252 // HPA - Cursor to column # in current row. Same as CHA
5253 if (type
== DeviceType
.XTERM
) {
5258 // HPR - Cursor right. Same as CUF
5259 if (type
== DeviceType
.XTERM
) {
5264 // REP - Repeat last char X times
5265 if (type
== DeviceType
.XTERM
) {
5270 // DA - Device attributes
5274 // VPA - Cursor to row, current column.
5275 if (type
== DeviceType
.XTERM
) {
5280 // VPR - Cursor down. Same as CUD
5281 if (type
== DeviceType
.XTERM
) {
5286 // HVP - Horizontal and vertical position
5290 // TBC - Tabulation clear
5294 // Sets an ANSI or DEC private toggle
5298 if ((type
== DeviceType
.VT220
)
5299 || (type
== DeviceType
.XTERM
)) {
5301 // Printer functions
5309 // Sets an ANSI or DEC private toggle
5313 // SGR - Select graphics rendition
5317 // DSR - Device status report
5324 // DECLL - Load leds
5328 // DECSTBM - Set top and bottom margins
5332 // Save cursor (ANSI.SYS)
5333 if (type
== DeviceType
.XTERM
) {
5334 savedState
.cursorX
= currentState
.cursorX
;
5335 savedState
.cursorY
= currentState
.cursorY
;
5341 // Restore cursor (ANSI.SYS)
5342 if (type
== DeviceType
.XTERM
) {
5343 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5350 // DECREQTPARM - Request terminal parameters
5366 // 0x9C goes to GROUND
5371 // 0x3A goes to CSI_IGNORE
5373 scanState
= ScanState
.CSI_IGNORE
;
5378 // 00-17, 19, 1C-1F --> execute
5380 handleControlChar(ch
);
5383 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5384 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5386 scanState
= ScanState
.CSI_INTERMEDIATE
;
5389 // 30-39, 3B --> param
5390 if ((ch
>= '0') && (ch
<= '9')) {
5397 // 0x3A goes to CSI_IGNORE
5399 scanState
= ScanState
.CSI_IGNORE
;
5401 // 0x3C-3F goes to CSI_IGNORE
5402 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5403 scanState
= ScanState
.CSI_IGNORE
;
5406 // 40-7E --> dispatch, then switch to GROUND
5407 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5410 // ICH - Insert character
5418 // CUD - Cursor down
5422 // CUF - Cursor forward
5426 // CUB - Cursor backward
5430 // CNL - Cursor down and to column 1
5431 if (type
== DeviceType
.XTERM
) {
5436 // CPL - Cursor up and to column 1
5437 if (type
== DeviceType
.XTERM
) {
5442 // CHA - Cursor to column # in current row
5443 if (type
== DeviceType
.XTERM
) {
5448 // CUP - Cursor position
5452 // CHT - Cursor forward X tab stops (default 1)
5453 if (type
== DeviceType
.XTERM
) {
5458 // ED - Erase in display
5462 // EL - Erase in line
5477 // DCH - Delete character
5484 // Scroll up X lines (default 1)
5485 if (type
== DeviceType
.XTERM
) {
5490 // Scroll down X lines (default 1)
5491 if (type
== DeviceType
.XTERM
) {
5500 if ((type
== DeviceType
.VT220
)
5501 || (type
== DeviceType
.XTERM
)) {
5503 // ECH - Erase character
5510 // CBT - Cursor backward X tab stops (default 1)
5511 if (type
== DeviceType
.XTERM
) {
5522 // HPA - Cursor to column # in current row. Same as CHA
5523 if (type
== DeviceType
.XTERM
) {
5528 // HPR - Cursor right. Same as CUF
5529 if (type
== DeviceType
.XTERM
) {
5534 // REP - Repeat last char X times
5535 if (type
== DeviceType
.XTERM
) {
5540 // DA - Device attributes
5544 // VPA - Cursor to row, current column.
5545 if (type
== DeviceType
.XTERM
) {
5550 // VPR - Cursor down. Same as CUD
5551 if (type
== DeviceType
.XTERM
) {
5556 // HVP - Horizontal and vertical position
5560 // TBC - Tabulation clear
5564 // Sets an ANSI or DEC private toggle
5568 if ((type
== DeviceType
.VT220
)
5569 || (type
== DeviceType
.XTERM
)) {
5571 // Printer functions
5579 // Sets an ANSI or DEC private toggle
5583 // SGR - Select graphics rendition
5587 // DSR - Device status report
5594 // DECLL - Load leds
5598 // DECSTBM - Set top and bottom margins
5608 // DECREQTPARM - Request terminal parameters
5625 case CSI_INTERMEDIATE
:
5626 // 00-17, 19, 1C-1F --> execute
5628 handleControlChar(ch
);
5631 // 20-2F --> collect
5632 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5636 // 0x30-3F goes to CSI_IGNORE
5637 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5638 scanState
= ScanState
.CSI_IGNORE
;
5641 // 40-7E --> dispatch, then switch to GROUND
5642 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5694 if (((type
== DeviceType
.VT220
)
5695 || (type
== DeviceType
.XTERM
))
5696 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5698 // DECSCL - compatibility level
5701 if ((type
== DeviceType
.XTERM
)
5702 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
5704 // DECSTR - Soft terminal reset
5709 if (((type
== DeviceType
.VT220
)
5710 || (type
== DeviceType
.XTERM
))
5711 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5739 // 00-17, 19, 1C-1F --> execute
5741 handleControlChar(ch
);
5744 // 20-2F --> collect
5745 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5749 // 40-7E --> ignore, then switch to GROUND
5750 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5754 // 20-3F, 7F --> ignore
5760 // 0x9C goes to GROUND
5765 // 0x1B 0x5C goes to GROUND
5770 if ((collectBuffer
.length() > 0)
5771 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5777 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5778 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5780 scanState
= ScanState
.DCS_INTERMEDIATE
;
5783 // 30-39, 3B --> param, then switch to DCS_PARAM
5784 if ((ch
>= '0') && (ch
<= '9')) {
5786 scanState
= ScanState
.DCS_PARAM
;
5790 scanState
= ScanState
.DCS_PARAM
;
5793 // 3C-3F --> collect, then switch to DCS_PARAM
5794 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5796 scanState
= ScanState
.DCS_PARAM
;
5799 // 00-17, 19, 1C-1F, 7F --> ignore
5801 // 0x3A goes to DCS_IGNORE
5803 scanState
= ScanState
.DCS_IGNORE
;
5806 // 0x40-7E goes to DCS_PASSTHROUGH
5807 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5808 scanState
= ScanState
.DCS_PASSTHROUGH
;
5812 case DCS_INTERMEDIATE
:
5814 // 0x9C goes to GROUND
5819 // 0x1B 0x5C goes to GROUND
5824 if ((collectBuffer
.length() > 0)
5825 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5831 // 0x30-3F goes to DCS_IGNORE
5832 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5833 scanState
= ScanState
.DCS_IGNORE
;
5836 // 0x40-7E goes to DCS_PASSTHROUGH
5837 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5838 scanState
= ScanState
.DCS_PASSTHROUGH
;
5841 // 00-17, 19, 1C-1F, 7F --> ignore
5846 // 0x9C goes to GROUND
5851 // 0x1B 0x5C goes to GROUND
5856 if ((collectBuffer
.length() > 0)
5857 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5863 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5864 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5866 scanState
= ScanState
.DCS_INTERMEDIATE
;
5869 // 30-39, 3B --> param
5870 if ((ch
>= '0') && (ch
<= '9')) {
5877 // 00-17, 19, 1C-1F, 7F --> ignore
5879 // 0x3A, 3C-3F goes to DCS_IGNORE
5881 scanState
= ScanState
.DCS_IGNORE
;
5883 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5884 scanState
= ScanState
.DCS_IGNORE
;
5887 // 0x40-7E goes to DCS_PASSTHROUGH
5888 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5889 scanState
= ScanState
.DCS_PASSTHROUGH
;
5893 case DCS_PASSTHROUGH
:
5894 // 0x9C goes to GROUND
5899 // 0x1B 0x5C goes to GROUND
5904 if ((collectBuffer
.length() > 0)
5905 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5911 // 00-17, 19, 1C-1F, 20-7E --> put
5919 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5922 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5931 // 00-17, 19, 1C-1F, 20-7F --> ignore
5933 // 0x9C goes to GROUND
5940 case SOSPMAPC_STRING
:
5941 // 00-17, 19, 1C-1F, 20-7F --> ignore
5943 // 0x9C goes to GROUND
5951 // Special case for Xterm: OSC can pass control characters
5952 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5956 // 00-17, 19, 1C-1F --> ignore
5958 // 20-7F --> osc_put
5959 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5963 // 0x9C goes to GROUND
5970 case VT52_DIRECT_CURSOR_ADDRESS
:
5971 // This is a special case for the VT52 sequence "ESC Y l c"
5972 if (collectBuffer
.length() == 0) {
5974 } else if (collectBuffer
.length() == 1) {
5975 // We've got the two characters, one in the buffer and the
5977 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
5986 * Expose current cursor X to outside world.
5988 * @return current cursor X
5990 public final int getCursorX() {
5991 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
5992 return currentState
.cursorX
* 2;
5994 return currentState
.cursorX
;
5998 * Expose current cursor Y to outside world.
6000 * @return current cursor Y
6002 public final int getCursorY() {
6003 return currentState
.cursorY
;
6007 * Read function runs on a separate thread.
6009 public final void run() {
6010 boolean utf8
= false;
6011 boolean done
= false;
6013 if (type
== DeviceType
.XTERM
) {
6017 // available() will often return > 1, so we need to read in chunks to
6019 char [] readBufferUTF8
= null;
6020 byte [] readBuffer
= null;
6022 readBufferUTF8
= new char[128];
6024 readBuffer
= new byte[128];
6027 while (!done
&& !stopReaderThread
) {
6029 int n
= inputStream
.available();
6031 // System.err.printf("available() %d\n", n); System.err.flush();
6033 if (readBufferUTF8
.length
< n
) {
6034 // The buffer wasn't big enough, make it huger
6035 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
6038 readBufferUTF8
= new char[newSizeHalf
* 2];
6041 if (readBuffer
.length
< n
) {
6042 // The buffer wasn't big enough, make it huger
6043 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
6044 readBuffer
= new byte[newSizeHalf
* 2];
6050 } catch (InterruptedException e
) {
6059 rc
= input
.read(readBufferUTF8
, 0,
6060 readBufferUTF8
.length
);
6062 rc
= inputStream
.read(readBuffer
, 0,
6065 } catch (ReadTimeoutException e
) {
6069 // System.err.printf("read() %d\n", rc); System.err.flush();
6074 for (int i
= 0; i
< rc
; i
++) {
6077 ch
= readBufferUTF8
[i
];
6082 synchronized (this) {
6083 // Don't step on UI events
6087 // Permit my enclosing UI to know that I updated.
6088 if (listener
!= null) {
6089 listener
.displayChanged();
6092 // System.err.println("end while loop"); System.err.flush();
6093 } catch (IOException e
) {
6094 e
.printStackTrace();
6098 } // while ((done == false) && (stopReaderThread == false))
6100 // Let the rest of the world know that I am done.
6101 stopReaderThread
= true;
6104 inputStream
.cancelRead();
6105 inputStream
.close();
6107 } catch (IOException e
) {
6113 } catch (IOException e
) {
6117 // System.err.println("*** run() exiting..."); System.err.flush();