2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2016 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.tterminal
;
31 import java
.io
.InputStream
;
32 import java
.io
.InputStreamReader
;
33 import java
.io
.IOException
;
34 import java
.io
.OutputStream
;
35 import java
.io
.OutputStreamWriter
;
36 import java
.io
.Reader
;
37 import java
.io
.UnsupportedEncodingException
;
38 import java
.io
.Writer
;
39 import java
.util
.ArrayList
;
40 import java
.util
.Collections
;
41 import java
.util
.LinkedList
;
42 import java
.util
.List
;
44 import jexer
.TKeypress
;
45 import jexer
.event
.TMouseEvent
;
46 import jexer
.bits
.Color
;
47 import jexer
.bits
.Cell
;
48 import jexer
.bits
.CellAttributes
;
49 import jexer
.io
.ReadTimeoutException
;
50 import jexer
.io
.TimeoutInputStream
;
51 import static jexer
.TKeypress
.*;
54 * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
55 * including a scrollback buffer.
58 * It currently implements VT100, VT102, VT220, and XTERM with the following
62 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
63 * failure with XTERM. This is due to vttest failing to decode the UTF-8
67 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
68 * from VT100 are not supported.
71 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
72 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
73 * because DECSCL is not supported, it will fail the last part of the
74 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
77 * - Numeric/application keys from the number pad are not supported because
78 * they are not exposed from the TKeypress API.
81 * - VT52 HOLD SCREEN mode is not supported.
84 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
85 * numerators) are not rendered correctly.
88 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
90 public class ECMA48
implements Runnable
{
93 * The emulator can emulate several kinds of terminals.
95 public enum DeviceType
{
97 * DEC VT100 but also including the three VT102 functions.
118 * Return the proper primary Device Attributes string.
120 * @return string to send to remote side that is appropriate for the
123 private String
deviceTypeResponse() {
126 // "I am a VT100 with advanced video option" (often VT102)
135 // "I am a VT220" - 7 bit version
137 return "\033[?62;1;6c";
139 // "I am a VT220" - 8 bit version
140 return "\u009b?62;1;6c";
142 throw new IllegalArgumentException("Invalid device type: " + type
);
147 * Return the proper TERM environment variable for this device type.
149 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
150 * @return "vt100", "xterm", etc.
152 public static String
deviceTypeTerm(final DeviceType deviceType
) {
153 switch (deviceType
) {
167 throw new IllegalArgumentException("Invalid device type: "
173 * Return the proper LANG for this device type. Only XTERM devices know
174 * about UTF-8, the others are defined by their standard to be either
175 * 7-bit or 8-bit characters only.
177 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
178 * @param baseLang a base language without UTF-8 flag such as "C" or
180 * @return "en_US", "en_US.UTF-8", etc.
182 public static String
deviceTypeLang(final DeviceType deviceType
,
183 final String baseLang
) {
185 switch (deviceType
) {
193 return baseLang
+ ".UTF-8";
196 throw new IllegalArgumentException("Invalid device type: "
202 * Write a string directly to the remote side.
204 * @param str string to send
206 private void writeRemote(final String str
) {
207 if (stopReaderThread
) {
208 // Reader hit EOF, bail out now.
213 // System.err.printf("writeRemote() '%s'\n", str);
219 if (outputStream
== null) {
223 for (int i
= 0; i
< str
.length(); i
++) {
224 outputStream
.write(str
.charAt(i
));
226 outputStream
.flush();
227 } catch (IOException e
) {
233 if (output
== null) {
239 } catch (IOException e
) {
245 throw new IllegalArgumentException("Invalid device type: " + type
);
250 * Close the input and output streams and stop the reader thread. Note
251 * that it is safe to call this multiple times.
253 public final void close() {
255 // Tell the reader thread to stop looking at input. It will close
256 // the input streams.
257 if (stopReaderThread
== false) {
258 stopReaderThread
= true;
260 readerThread
.join(1000);
261 } catch (InterruptedException e
) {
266 // Now close the output stream.
271 if (outputStream
!= null) {
273 outputStream
.close();
274 } catch (IOException e
) {
281 if (outputStream
!= null) {
283 outputStream
.close();
284 } catch (IOException e
) {
289 if (output
!= null) {
292 } catch (IOException e
) {
299 throw new IllegalArgumentException("Invalid device type: " +
305 * When true, the reader thread is expected to exit.
307 private volatile boolean stopReaderThread
= false;
312 private Thread readerThread
= null;
315 * See if the reader thread is still running.
317 * @return if true, we are still connected to / reading from the remote
320 public final boolean isReading() {
321 return (!stopReaderThread
);
325 * The type of emulator to be.
327 private DeviceType type
= DeviceType
.VT102
;
330 * Obtain a new blank display line for an external user
331 * (e.g. TTerminalWindow).
333 * @return new blank line
335 public final DisplayLine
getBlankDisplayLine() {
336 return new DisplayLine(currentState
.attr
);
340 * The scrollback buffer characters + attributes.
342 private volatile List
<DisplayLine
> scrollback
;
345 * Get the scrollback buffer.
347 * @return the scrollback buffer
349 public final List
<DisplayLine
> getScrollbackBuffer() {
354 * The raw display buffer characters + attributes.
356 private volatile List
<DisplayLine
> display
;
359 * Get the display buffer.
361 * @return the display buffer
363 public final List
<DisplayLine
> getDisplayBuffer() {
368 * The terminal's input. For type == XTERM, this is an InputStreamReader
369 * with UTF-8 encoding.
371 private Reader input
;
374 * The terminal's raw InputStream. This is used for type != XTERM.
376 private volatile TimeoutInputStream inputStream
;
379 * The terminal's output. For type == XTERM, this wraps an
380 * OutputStreamWriter with UTF-8 encoding.
382 private Writer output
;
385 * The terminal's raw OutputStream. This is used for type != XTERM.
387 private OutputStream outputStream
;
390 * Parser character scan states.
407 VT52_DIRECT_CURSOR_ADDRESS
411 * Current scanning state.
413 private ScanState scanState
;
416 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
417 * can't really use it in keypress() because we do not see number pad
418 * events from TKeypress.
420 private enum KeypadMode
{
426 * Arrow keys can emit three different sequences (DECCKM or VT52
429 private enum ArrowKeyMode
{
436 * Available character sets for GL, GR, G0, G1, G2, G3.
438 private enum CharacterSet
{
459 * Single-shift states used by the C1 control characters SS2 (0x8E) and
462 private enum Singleshift
{
469 * VT220+ lockshift states.
471 private enum LockshiftMode
{
481 * XTERM mouse reporting protocols.
483 private enum MouseProtocol
{
492 * Which mouse protocol is active.
494 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
497 * XTERM mouse reporting encodings.
499 private enum MouseEncoding
{
506 * Which mouse encoding is active.
508 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
511 * Physical display width. We start at 80x24, but the user can resize us
517 * Get the display width.
519 * @return the width (usually 80 or 132)
521 public final int getWidth() {
526 * Physical display height. We start at 80x24, but the user can resize
532 * Get the display height.
534 * @return the height (usually 24)
536 public final int getHeight() {
541 * Top margin of the scrolling region.
543 private int scrollRegionTop
;
546 * Bottom margin of the scrolling region.
548 private int scrollRegionBottom
;
551 * Right margin column number. This can be selected by the remote side
552 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
554 private int rightMargin
;
557 * Last character printed.
562 * VT100-style line wrapping: a character is placed in column 80 (or
563 * 132), but the line does NOT wrap until another character is written to
564 * column 1 of the next line, after which the cursor moves to column 2.
566 private boolean wrapLineFlag
;
569 * VT220 single shift flag.
571 private Singleshift singleshift
= Singleshift
.NONE
;
574 * true = insert characters, false = overwrite.
576 private boolean insertMode
= false;
579 * VT52 mode as selected by DECANM. True means VT52, false means
580 * ANSI. Default is ANSI.
582 private boolean vt52Mode
= false;
585 * Visible cursor (DECTCEM).
587 private boolean cursorVisible
= true;
590 * Get visible cursor flag.
592 * @return if true, the cursor is visible
594 public final boolean isCursorVisible() {
595 return cursorVisible
;
599 * Screen title as set by the xterm OSC sequence. Lots of applications
600 * send a screenTitle regardless of whether it is an xterm client or not.
602 private String screenTitle
= "";
605 * Get the screen title as set by the xterm OSC sequence. Lots of
606 * applications send a screenTitle regardless of whether it is an xterm
609 * @return screen title
611 public final String
getScreenTitle() {
616 * Parameter characters being collected.
618 private List
<Integer
> csiParams
;
621 * Non-csi collect buffer.
623 private StringBuilder collectBuffer
;
626 * When true, use the G1 character set.
628 private boolean shiftOut
= false;
631 * Horizontal tab stop locations.
633 private List
<Integer
> tabStops
;
636 * S8C1T. True means 8bit controls, false means 7bit controls.
638 private boolean s8c1t
= false;
641 * Printer mode. True means send all output to printer, which discards
644 private boolean printerControllerMode
= false;
647 * LMN line mode. If true, linefeed() puts the cursor on the first
648 * column of the next line. If false, linefeed() puts the cursor one
649 * line down on the current line. The default is false.
651 private boolean newLineMode
= false;
654 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
656 private ArrowKeyMode arrowKeyMode
;
659 * Whether number pad keys send VT100 or VT52, application or numeric
662 @SuppressWarnings("unused")
663 private KeypadMode keypadMode
;
666 * When true, the terminal is in 132-column mode (DECCOLM).
668 private boolean columns132
= false;
671 * Get 132 columns value.
673 * @return if true, the terminal is in 132 column mode
675 public final boolean isColumns132() {
680 * true = reverse video. Set by DECSCNM.
682 private boolean reverseVideo
= false;
685 * false = echo characters locally.
687 private boolean fullDuplex
= true;
690 * DECSC/DECRC save/restore a subset of the total state. This class
691 * encapsulates those specific flags/modes.
693 private class SaveableState
{
696 * When true, cursor positions are relative to the scrolling region.
698 public boolean originMode
= false;
701 * The current editing X position.
703 public int cursorX
= 0;
706 * The current editing Y position.
708 public int cursorY
= 0;
711 * Which character set is currently selected in G0.
713 public CharacterSet g0Charset
= CharacterSet
.US
;
716 * Which character set is currently selected in G1.
718 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
721 * Which character set is currently selected in G2.
723 public CharacterSet g2Charset
= CharacterSet
.US
;
726 * Which character set is currently selected in G3.
728 public CharacterSet g3Charset
= CharacterSet
.US
;
731 * Which character set is currently selected in GR.
733 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
736 * The current drawing attributes.
738 public CellAttributes attr
;
743 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
748 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
753 public boolean lineWrap
= true;
758 public void reset() {
762 g0Charset
= CharacterSet
.US
;
763 g1Charset
= CharacterSet
.DRAWING
;
764 g2Charset
= CharacterSet
.US
;
765 g3Charset
= CharacterSet
.US
;
766 grCharset
= CharacterSet
.DRAWING
;
767 attr
= new CellAttributes();
768 glLockshift
= LockshiftMode
.NONE
;
769 grLockshift
= LockshiftMode
.NONE
;
774 * Copy attributes from another instance.
776 * @param that the other instance to match
778 public void setTo(final SaveableState that
) {
779 this.originMode
= that
.originMode
;
780 this.cursorX
= that
.cursorX
;
781 this.cursorY
= that
.cursorY
;
782 this.g0Charset
= that
.g0Charset
;
783 this.g1Charset
= that
.g1Charset
;
784 this.g2Charset
= that
.g2Charset
;
785 this.g3Charset
= that
.g3Charset
;
786 this.grCharset
= that
.grCharset
;
787 this.attr
= new CellAttributes();
788 this.attr
.setTo(that
.attr
);
789 this.glLockshift
= that
.glLockshift
;
790 this.grLockshift
= that
.grLockshift
;
791 this.lineWrap
= that
.lineWrap
;
795 * Public constructor.
797 public SaveableState() {
803 * The current terminal state.
805 private SaveableState currentState
;
808 * The last saved terminal state.
810 private SaveableState savedState
;
813 * Clear the CSI parameters and flags.
815 private void toGround() {
817 collectBuffer
= new StringBuilder(8);
818 scanState
= ScanState
.GROUND
;
822 * Reset the tab stops list.
824 private void resetTabStops() {
826 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
827 tabStops
.add(new Integer(i
* 8));
832 * Reset the emulation state.
834 private void reset() {
836 currentState
= new SaveableState();
837 savedState
= new SaveableState();
838 scanState
= ScanState
.GROUND
;
842 scrollRegionBottom
= height
- 1;
843 rightMargin
= width
- 1;
845 arrowKeyMode
= ArrowKeyMode
.ANSI
;
846 keypadMode
= KeypadMode
.Numeric
;
847 wrapLineFlag
= false;
855 reverseVideo
= false;
857 cursorVisible
= true;
860 singleshift
= Singleshift
.NONE
;
862 printerControllerMode
= false;
865 mouseProtocol
= MouseProtocol
.OFF
;
866 mouseEncoding
= MouseEncoding
.X10
;
876 * Public constructor.
878 * @param type one of the DeviceType constants to select VT100, VT102,
880 * @param inputStream an InputStream connected to the remote side. For
881 * type == XTERM, inputStream is converted to a Reader with UTF-8
883 * @param outputStream an OutputStream connected to the remote user. For
884 * type == XTERM, outputStream is converted to a Writer with UTF-8
886 * @throws UnsupportedEncodingException if an exception is thrown when
887 * creating the InputStreamReader
889 public ECMA48(final DeviceType type
, final InputStream inputStream
,
890 final OutputStream outputStream
) throws UnsupportedEncodingException
{
892 assert (inputStream
!= null);
893 assert (outputStream
!= null);
895 csiParams
= new ArrayList
<Integer
>();
896 tabStops
= new ArrayList
<Integer
>();
897 scrollback
= new LinkedList
<DisplayLine
>();
898 display
= new LinkedList
<DisplayLine
>();
901 if (inputStream
instanceof TimeoutInputStream
) {
902 this.inputStream
= (TimeoutInputStream
)inputStream
;
904 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
906 if (type
== DeviceType
.XTERM
) {
907 this.input
= new InputStreamReader(this.inputStream
, "UTF-8");
908 this.output
= new OutputStreamWriter(outputStream
, "UTF-8");
909 this.outputStream
= null;
912 this.outputStream
= outputStream
;
916 for (int i
= 0; i
< height
; i
++) {
917 display
.add(new DisplayLine(currentState
.attr
));
920 // Spin up the input reader
921 readerThread
= new Thread(this);
922 readerThread
.start();
926 * Append a new line to the bottom of the display, adding lines off the
927 * top to the scrollback buffer.
929 private void newDisplayLine() {
930 // Scroll the top line off into the scrollback buffer
931 scrollback
.add(display
.get(0));
933 DisplayLine line
= new DisplayLine(currentState
.attr
);
934 line
.setReverseColor(reverseVideo
);
939 * Wraps the current line.
941 private void wrapCurrentLine() {
942 if (currentState
.cursorY
== height
- 1) {
945 if (currentState
.cursorY
< height
- 1) {
946 currentState
.cursorY
++;
948 currentState
.cursorX
= 0;
952 * Handle a carriage return.
954 private void carriageReturn() {
955 currentState
.cursorX
= 0;
956 wrapLineFlag
= false;
960 * Reverse the color of the visible display.
962 private void invertDisplayColors() {
963 for (DisplayLine line
: display
) {
964 line
.setReverseColor(!line
.isReverseColor());
971 private void linefeed() {
973 if (currentState
.cursorY
< scrollRegionBottom
) {
974 // Increment screen y
975 currentState
.cursorY
++;
978 // Screen y does not increment
981 * Two cases: either we're inside a scrolling region or not. If
982 * the scrolling region bottom is the bottom of the screen, then
983 * push the top line into the buffer. Else scroll the scrolling
986 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
988 // We're at the bottom of the scroll region, AND the scroll
989 // region is the entire screen.
995 // We're at the bottom of the scroll region, AND the scroll
996 // region is NOT the entire screen.
997 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1002 currentState
.cursorX
= 0;
1004 wrapLineFlag
= false;
1008 * Prints one character to the display buffer.
1010 * @param ch character to display
1012 private void printCharacter(final char ch
) {
1013 int rightMargin
= this.rightMargin
;
1015 // Check if we have double-width, and if so chop at 40/66 instead of
1017 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1018 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1021 // Check the unusually-complicated line wrapping conditions...
1022 if (currentState
.cursorX
== rightMargin
) {
1024 if (currentState
.lineWrap
== true) {
1026 * This case happens when: the cursor was already on the
1027 * right margin (either through printing or by an explicit
1028 * placement command), and a character was printed.
1030 * The line wraps only when a new character arrives AND the
1031 * cursor is already on the right margin AND has placed a
1032 * character in its cell. Easier to see than to explain.
1034 if (wrapLineFlag
== false) {
1036 * This block marks the case that we are in the margin
1037 * and the first character has been received and printed.
1039 wrapLineFlag
= true;
1042 * This block marks the case that we are in the margin
1043 * and the second character has been received and
1046 wrapLineFlag
= false;
1050 } else if (currentState
.cursorX
<= rightMargin
) {
1052 * This is the normal case: a character came in and was printed
1053 * to the left of the right margin column.
1056 // Turn off VT100 special-case flag
1057 wrapLineFlag
= false;
1060 // "Print" the character
1061 Cell newCell
= new Cell(ch
);
1062 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1063 newCellAttributes
.setTo(currentState
.attr
);
1064 DisplayLine line
= display
.get(currentState
.cursorY
);
1065 // Insert mode special case
1066 if (insertMode
== true) {
1067 line
.insert(currentState
.cursorX
, newCell
);
1069 // Replace an existing character
1070 line
.replace(currentState
.cursorX
, newCell
);
1073 // Increment horizontal
1074 if (wrapLineFlag
== false) {
1075 currentState
.cursorX
++;
1076 if (currentState
.cursorX
> rightMargin
) {
1077 currentState
.cursorX
--;
1083 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1084 * send to the remote side.
1086 * @param mouse mouse event received from the local user
1088 public void mouse(final TMouseEvent mouse
) {
1091 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1092 mouseProtocol, mouseEncoding, mouse);
1095 if (mouseEncoding
== MouseEncoding
.X10
) {
1096 // We will support X10 but only for (160,94) and smaller.
1097 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1102 switch (mouseProtocol
) {
1109 // Only report button presses
1110 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1116 // Only report button presses and releases
1117 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1118 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1126 * Only report button presses, button releases, and motions that
1127 * have a button down (i.e. drag-and-drop).
1129 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1130 if (!mouse
.isMouse1()
1131 && !mouse
.isMouse2()
1132 && !mouse
.isMouse3()
1133 && !mouse
.isMouseWheelUp()
1134 && !mouse
.isMouseWheelDown()
1142 // Report everything
1146 // Now encode the event
1147 StringBuilder sb
= new StringBuilder(6);
1148 if (mouseEncoding
== MouseEncoding
.SGR
) {
1149 sb
.append((char) 0x1B);
1152 if (mouse
.isMouse1()) {
1153 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1158 } else if (mouse
.isMouse2()) {
1159 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1164 } else if (mouse
.isMouse3()) {
1165 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1170 } else if (mouse
.isMouseWheelUp()) {
1172 } else if (mouse
.isMouseWheelDown()) {
1175 // This is motion with no buttons down.
1179 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1182 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1189 // X10 and UTF8 encodings
1190 sb
.append((char) 0x1B);
1193 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1194 sb
.append((char) (0x03 + 32));
1195 } else if (mouse
.isMouse1()) {
1196 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1197 sb
.append((char) (0x00 + 32 + 32));
1199 sb
.append((char) (0x00 + 32));
1201 } else if (mouse
.isMouse2()) {
1202 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1203 sb
.append((char) (0x01 + 32 + 32));
1205 sb
.append((char) (0x01 + 32));
1207 } else if (mouse
.isMouse3()) {
1208 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1209 sb
.append((char) (0x02 + 32 + 32));
1211 sb
.append((char) (0x02 + 32));
1213 } else if (mouse
.isMouseWheelUp()) {
1214 sb
.append((char) (0x04 + 64));
1215 } else if (mouse
.isMouseWheelDown()) {
1216 sb
.append((char) (0x05 + 64));
1218 // This is motion with no buttons down.
1219 sb
.append((char) (0x03 + 32));
1222 sb
.append((char) (mouse
.getX() + 33));
1223 sb
.append((char) (mouse
.getY() + 33));
1226 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1227 writeRemote(sb
.toString());
1231 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1232 * send to the remote side.
1234 * @param keypress keypress received from the local user
1236 public void keypress(final TKeypress keypress
) {
1237 writeRemote(keypressToString(keypress
));
1241 * Build one of the complex xterm keystroke sequences, storing the result in
1242 * xterm_keystroke_buffer.
1244 * @param ss3 the prefix to use based on VT100 state.
1245 * @param first the first character, usually a number.
1246 * @param first the last character, one of the following: ~ A B C D F H
1247 * @param ctrl whether or not ctrl is down
1248 * @param alt whether or not alt is down
1249 * @param shift whether or not shift is down
1250 * @return the buffer with the full key sequence
1252 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1253 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1255 StringBuilder sb
= new StringBuilder(ss3
);
1256 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1260 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1262 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1264 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1266 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1268 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1270 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1272 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1277 return sb
.toString();
1281 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1283 * @param keypress keypress received from the local user
1284 * @return string to transmit to the remote side
1286 private String
keypressToString(final TKeypress keypress
) {
1288 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1290 * If this is a control character, process it like it came from
1293 if (keypress
.getChar() < 0x20) {
1294 handleControlChar(keypress
.getChar());
1296 // Local echo for everything else
1297 printCharacter(keypress
.getChar());
1301 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1306 // Handle control characters
1307 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1308 StringBuilder sb
= new StringBuilder();
1309 char ch
= keypress
.getChar();
1312 return sb
.toString();
1315 // Handle alt characters
1316 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1317 StringBuilder sb
= new StringBuilder("\033");
1318 char ch
= keypress
.getChar();
1320 return sb
.toString();
1323 if (keypress
.equals(kbBackspace
)) {
1336 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1339 switch (arrowKeyMode
) {
1341 return xtermBuildKeySequence("\033[", '1', 'D',
1342 keypress
.isCtrl(), keypress
.isAlt(),
1343 keypress
.isShift());
1345 return xtermBuildKeySequence("\033", '1', 'D',
1346 keypress
.isCtrl(), keypress
.isAlt(),
1347 keypress
.isShift());
1349 return xtermBuildKeySequence("\033O", '1', 'D',
1350 keypress
.isCtrl(), keypress
.isAlt(),
1351 keypress
.isShift());
1354 switch (arrowKeyMode
) {
1365 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1368 switch (arrowKeyMode
) {
1370 return xtermBuildKeySequence("\033[", '1', 'C',
1371 keypress
.isCtrl(), keypress
.isAlt(),
1372 keypress
.isShift());
1374 return xtermBuildKeySequence("\033", '1', 'C',
1375 keypress
.isCtrl(), keypress
.isAlt(),
1376 keypress
.isShift());
1378 return xtermBuildKeySequence("\033O", '1', 'C',
1379 keypress
.isCtrl(), keypress
.isAlt(),
1380 keypress
.isShift());
1383 switch (arrowKeyMode
) {
1394 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1397 switch (arrowKeyMode
) {
1399 return xtermBuildKeySequence("\033[", '1', 'A',
1400 keypress
.isCtrl(), keypress
.isAlt(),
1401 keypress
.isShift());
1403 return xtermBuildKeySequence("\033", '1', 'A',
1404 keypress
.isCtrl(), keypress
.isAlt(),
1405 keypress
.isShift());
1407 return xtermBuildKeySequence("\033O", '1', 'A',
1408 keypress
.isCtrl(), keypress
.isAlt(),
1409 keypress
.isShift());
1412 switch (arrowKeyMode
) {
1423 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1426 switch (arrowKeyMode
) {
1428 return xtermBuildKeySequence("\033[", '1', 'B',
1429 keypress
.isCtrl(), keypress
.isAlt(),
1430 keypress
.isShift());
1432 return xtermBuildKeySequence("\033", '1', 'B',
1433 keypress
.isCtrl(), keypress
.isAlt(),
1434 keypress
.isShift());
1436 return xtermBuildKeySequence("\033O", '1', 'B',
1437 keypress
.isCtrl(), keypress
.isAlt(),
1438 keypress
.isShift());
1441 switch (arrowKeyMode
) {
1452 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1455 switch (arrowKeyMode
) {
1457 return xtermBuildKeySequence("\033[", '1', 'H',
1458 keypress
.isCtrl(), keypress
.isAlt(),
1459 keypress
.isShift());
1461 return xtermBuildKeySequence("\033", '1', 'H',
1462 keypress
.isCtrl(), keypress
.isAlt(),
1463 keypress
.isShift());
1465 return xtermBuildKeySequence("\033O", '1', 'H',
1466 keypress
.isCtrl(), keypress
.isAlt(),
1467 keypress
.isShift());
1470 switch (arrowKeyMode
) {
1481 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1484 switch (arrowKeyMode
) {
1486 return xtermBuildKeySequence("\033[", '1', 'F',
1487 keypress
.isCtrl(), keypress
.isAlt(),
1488 keypress
.isShift());
1490 return xtermBuildKeySequence("\033", '1', 'F',
1491 keypress
.isCtrl(), keypress
.isAlt(),
1492 keypress
.isShift());
1494 return xtermBuildKeySequence("\033O", '1', 'F',
1495 keypress
.isCtrl(), keypress
.isAlt(),
1496 keypress
.isShift());
1499 switch (arrowKeyMode
) {
1510 if (keypress
.equals(kbF1
)) {
1518 if (keypress
.equals(kbF2
)) {
1526 if (keypress
.equals(kbF3
)) {
1534 if (keypress
.equals(kbF4
)) {
1542 if (keypress
.equals(kbF5
)) {
1555 if (keypress
.equals(kbF6
)) {
1568 if (keypress
.equals(kbF7
)) {
1581 if (keypress
.equals(kbF8
)) {
1594 if (keypress
.equals(kbF9
)) {
1607 if (keypress
.equals(kbF10
)) {
1620 if (keypress
.equals(kbF11
)) {
1624 if (keypress
.equals(kbF12
)) {
1628 if (keypress
.equals(kbShiftF1
)) {
1633 if (type
== DeviceType
.XTERM
) {
1639 if (keypress
.equals(kbShiftF2
)) {
1644 if (type
== DeviceType
.XTERM
) {
1650 if (keypress
.equals(kbShiftF3
)) {
1655 if (type
== DeviceType
.XTERM
) {
1661 if (keypress
.equals(kbShiftF4
)) {
1666 if (type
== DeviceType
.XTERM
) {
1672 if (keypress
.equals(kbShiftF5
)) {
1674 return "\033[15;2~";
1677 if (keypress
.equals(kbShiftF6
)) {
1679 return "\033[17;2~";
1682 if (keypress
.equals(kbShiftF7
)) {
1684 return "\033[18;2~";
1687 if (keypress
.equals(kbShiftF8
)) {
1689 return "\033[19;2~";
1692 if (keypress
.equals(kbShiftF9
)) {
1694 return "\033[20;2~";
1697 if (keypress
.equals(kbShiftF10
)) {
1699 return "\033[21;2~";
1702 if (keypress
.equals(kbShiftF11
)) {
1704 return "\033[23;2~";
1707 if (keypress
.equals(kbShiftF12
)) {
1709 return "\033[24;2~";
1712 if (keypress
.equals(kbCtrlF1
)) {
1717 if (type
== DeviceType
.XTERM
) {
1723 if (keypress
.equals(kbCtrlF2
)) {
1728 if (type
== DeviceType
.XTERM
) {
1734 if (keypress
.equals(kbCtrlF3
)) {
1739 if (type
== DeviceType
.XTERM
) {
1745 if (keypress
.equals(kbCtrlF4
)) {
1750 if (type
== DeviceType
.XTERM
) {
1756 if (keypress
.equals(kbCtrlF5
)) {
1758 return "\033[15;5~";
1761 if (keypress
.equals(kbCtrlF6
)) {
1763 return "\033[17;5~";
1766 if (keypress
.equals(kbCtrlF7
)) {
1768 return "\033[18;5~";
1771 if (keypress
.equals(kbCtrlF8
)) {
1773 return "\033[19;5~";
1776 if (keypress
.equals(kbCtrlF9
)) {
1778 return "\033[20;5~";
1781 if (keypress
.equals(kbCtrlF10
)) {
1783 return "\033[21;5~";
1786 if (keypress
.equals(kbCtrlF11
)) {
1788 return "\033[23;5~";
1791 if (keypress
.equals(kbCtrlF12
)) {
1793 return "\033[24;5~";
1796 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
1799 switch (arrowKeyMode
) {
1801 return xtermBuildKeySequence("\033[", '5', '~',
1802 keypress
.isCtrl(), keypress
.isAlt(),
1803 keypress
.isShift());
1805 return xtermBuildKeySequence("\033", '5', '~',
1806 keypress
.isCtrl(), keypress
.isAlt(),
1807 keypress
.isShift());
1809 return xtermBuildKeySequence("\033O", '5', '~',
1810 keypress
.isCtrl(), keypress
.isAlt(),
1811 keypress
.isShift());
1818 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
1821 switch (arrowKeyMode
) {
1823 return xtermBuildKeySequence("\033[", '6', '~',
1824 keypress
.isCtrl(), keypress
.isAlt(),
1825 keypress
.isShift());
1827 return xtermBuildKeySequence("\033", '6', '~',
1828 keypress
.isCtrl(), keypress
.isAlt(),
1829 keypress
.isShift());
1831 return xtermBuildKeySequence("\033O", '6', '~',
1832 keypress
.isCtrl(), keypress
.isAlt(),
1833 keypress
.isShift());
1840 if (keypress
.equalsWithoutModifiers(kbIns
)) {
1843 switch (arrowKeyMode
) {
1845 return xtermBuildKeySequence("\033[", '2', '~',
1846 keypress
.isCtrl(), keypress
.isAlt(),
1847 keypress
.isShift());
1849 return xtermBuildKeySequence("\033", '2', '~',
1850 keypress
.isCtrl(), keypress
.isAlt(),
1851 keypress
.isShift());
1853 return xtermBuildKeySequence("\033O", '2', '~',
1854 keypress
.isCtrl(), keypress
.isAlt(),
1855 keypress
.isShift());
1862 if (keypress
.equalsWithoutModifiers(kbDel
)) {
1865 switch (arrowKeyMode
) {
1867 return xtermBuildKeySequence("\033[", '3', '~',
1868 keypress
.isCtrl(), keypress
.isAlt(),
1869 keypress
.isShift());
1871 return xtermBuildKeySequence("\033", '3', '~',
1872 keypress
.isCtrl(), keypress
.isAlt(),
1873 keypress
.isShift());
1875 return xtermBuildKeySequence("\033O", '3', '~',
1876 keypress
.isCtrl(), keypress
.isAlt(),
1877 keypress
.isShift());
1880 // Delete sends real delete for VTxxx
1885 if (keypress
.equals(kbEnter
)) {
1889 if (keypress
.equals(kbEsc
)) {
1893 if (keypress
.equals(kbAltEsc
)) {
1897 if (keypress
.equals(kbTab
)) {
1901 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
1902 (keypress
.equals(kbShiftTab
))
1912 // Non-alt, non-ctrl characters
1913 if (!keypress
.isFnKey()) {
1914 StringBuilder sb
= new StringBuilder();
1915 sb
.append(keypress
.getChar());
1916 return sb
.toString();
1922 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1925 * @param ch 8-bit character from the remote side
1926 * @param charsetGl character set defined for GL
1927 * @param charsetGr character set defined for GR
1928 * @return character to display on the screen
1930 private char mapCharacterCharset(final char ch
,
1931 final CharacterSet charsetGl
,
1932 final CharacterSet charsetGr
) {
1934 int lookupChar
= ch
;
1935 CharacterSet lookupCharset
= charsetGl
;
1938 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
1939 lookupCharset
= charsetGr
;
1943 switch (lookupCharset
) {
1946 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
1949 return DECCharacterSets
.UK
[lookupChar
];
1952 return DECCharacterSets
.US_ASCII
[lookupChar
];
1955 return DECCharacterSets
.NL
[lookupChar
];
1958 return DECCharacterSets
.FI
[lookupChar
];
1961 return DECCharacterSets
.FR
[lookupChar
];
1964 return DECCharacterSets
.FR_CA
[lookupChar
];
1967 return DECCharacterSets
.DE
[lookupChar
];
1970 return DECCharacterSets
.IT
[lookupChar
];
1973 return DECCharacterSets
.NO
[lookupChar
];
1976 return DECCharacterSets
.ES
[lookupChar
];
1979 return DECCharacterSets
.SV
[lookupChar
];
1982 return DECCharacterSets
.SWISS
[lookupChar
];
1984 case DEC_SUPPLEMENTAL
:
1985 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
1988 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
1991 return DECCharacterSets
.US_ASCII
[lookupChar
];
1994 return DECCharacterSets
.US_ASCII
[lookupChar
];
1997 throw new IllegalArgumentException("Invalid character set value: "
2003 * Map an 8-bit byte into a printable character.
2005 * @param ch either 8-bit or Unicode character from the remote side
2006 * @return character to display on the screen
2008 private char mapCharacter(final char ch
) {
2010 // Unicode character, just return it
2014 CharacterSet charsetGl
= currentState
.g0Charset
;
2015 CharacterSet charsetGr
= currentState
.grCharset
;
2017 if (vt52Mode
== true) {
2018 if (shiftOut
== true) {
2019 // Shifted out character, pull from VT52 graphics
2020 charsetGl
= currentState
.g1Charset
;
2021 charsetGr
= CharacterSet
.US
;
2024 charsetGl
= currentState
.g0Charset
;
2025 charsetGr
= CharacterSet
.US
;
2028 // Pull the character
2029 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2033 if (shiftOut
== true) {
2034 // Shifted out character, pull from G1
2035 charsetGl
= currentState
.g1Charset
;
2036 charsetGr
= currentState
.grCharset
;
2038 // Pull the character
2039 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2043 if (singleshift
== Singleshift
.SS2
) {
2045 singleshift
= Singleshift
.NONE
;
2047 // Shifted out character, pull from G2
2048 charsetGl
= currentState
.g2Charset
;
2049 charsetGr
= currentState
.grCharset
;
2053 if (singleshift
== Singleshift
.SS3
) {
2055 singleshift
= Singleshift
.NONE
;
2057 // Shifted out character, pull from G3
2058 charsetGl
= currentState
.g3Charset
;
2059 charsetGr
= currentState
.grCharset
;
2062 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2063 // Check for locking shift
2065 switch (currentState
.glLockshift
) {
2078 charsetGl
= currentState
.g2Charset
;
2083 charsetGl
= currentState
.g3Charset
;
2088 charsetGl
= currentState
.g0Charset
;
2092 switch (currentState
.grLockshift
) {
2102 charsetGr
= currentState
.g1Charset
;
2107 charsetGr
= currentState
.g2Charset
;
2112 charsetGr
= currentState
.g3Charset
;
2117 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2124 // Pull the character
2125 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2129 * Scroll the text within a scrolling region up n lines.
2131 * @param regionTop top row of the scrolling region
2132 * @param regionBottom bottom row of the scrolling region
2133 * @param n number of lines to scroll
2135 private void scrollingRegionScrollUp(final int regionTop
,
2136 final int regionBottom
, final int n
) {
2138 if (regionTop
>= regionBottom
) {
2142 // Sanity check: see if there will be any characters left after the
2144 if (regionBottom
+ 1 - regionTop
<= n
) {
2145 // There won't be anything left in the region, so just call
2146 // eraseScreen() and return.
2147 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2151 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2152 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2153 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2155 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2156 - remaining
, regionBottom
+ 1);
2157 display
= new LinkedList
<DisplayLine
>(displayTop
);
2158 display
.addAll(displayMiddle
);
2159 for (int i
= 0; i
< n
; i
++) {
2160 DisplayLine line
= new DisplayLine(currentState
.attr
);
2161 line
.setReverseColor(reverseVideo
);
2164 display
.addAll(displayBottom
);
2166 assert (display
.size() == height
);
2170 * Scroll the text within a scrolling region down n lines.
2172 * @param regionTop top row of the scrolling region
2173 * @param regionBottom bottom row of the scrolling region
2174 * @param n number of lines to scroll
2176 private void scrollingRegionScrollDown(final int regionTop
,
2177 final int regionBottom
, final int n
) {
2179 if (regionTop
>= regionBottom
) {
2183 // Sanity check: see if there will be any characters left after the
2185 if (regionBottom
+ 1 - regionTop
<= n
) {
2186 // There won't be anything left in the region, so just call
2187 // eraseScreen() and return.
2188 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2192 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2193 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2194 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2196 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2197 regionTop
+ remaining
);
2198 display
= new LinkedList
<DisplayLine
>(displayTop
);
2199 for (int i
= 0; i
< n
; i
++) {
2200 DisplayLine line
= new DisplayLine(currentState
.attr
);
2201 line
.setReverseColor(reverseVideo
);
2204 display
.addAll(displayMiddle
);
2205 display
.addAll(displayBottom
);
2207 assert (display
.size() == height
);
2211 * Process a control character.
2213 * @param ch 8-bit character from the remote side
2215 private void handleControlChar(final char ch
) {
2216 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2227 // Transmit the answerback message.
2238 cursorLeft(1, false);
2243 advanceToNextTabStop();
2269 currentState
.glLockshift
= LockshiftMode
.NONE
;
2275 currentState
.glLockshift
= LockshiftMode
.NONE
;
2300 singleshift
= Singleshift
.SS2
;
2305 singleshift
= Singleshift
.SS3
;
2315 * Advance the cursor to the next tab stop.
2317 private void advanceToNextTabStop() {
2318 if (tabStops
.size() == 0) {
2319 // Go to the rightmost column
2320 cursorRight(rightMargin
- currentState
.cursorX
, false);
2323 for (Integer stop
: tabStops
) {
2324 if (stop
> currentState
.cursorX
) {
2325 cursorRight(stop
- currentState
.cursorX
, false);
2330 * We got here, meaning there isn't a tab stop beyond the current
2331 * cursor position. Place the cursor of the right-most edge of the
2334 cursorRight(rightMargin
- currentState
.cursorX
, false);
2338 * Save a character into the collect buffer.
2340 * @param ch character to save
2342 private void collect(final char ch
) {
2343 collectBuffer
.append(ch
);
2347 * Save a byte into the CSI parameters buffer.
2349 * @param ch byte to save
2351 private void param(final byte ch
) {
2352 if (csiParams
.size() == 0) {
2353 csiParams
.add(new Integer(0));
2355 Integer x
= csiParams
.get(csiParams
.size() - 1);
2356 if ((ch
>= '0') && (ch
<= '9')) {
2359 csiParams
.set(csiParams
.size() - 1, x
);
2363 csiParams
.add(new Integer(0));
2368 * Get a CSI parameter value, with a default.
2370 * @param position parameter index. 0 is the first parameter.
2371 * @param defaultValue value to use if csiParams[position] doesn't exist
2372 * @return parameter value
2374 private int getCsiParam(final int position
, final int defaultValue
) {
2375 if (csiParams
.size() < position
+ 1) {
2376 return defaultValue
;
2378 return csiParams
.get(position
).intValue();
2382 * Get a CSI parameter value, clamped to within min/max.
2384 * @param position parameter index. 0 is the first parameter.
2385 * @param defaultValue value to use if csiParams[position] doesn't exist
2386 * @param minValue minimum value inclusive
2387 * @param maxValue maximum value inclusive
2388 * @return parameter value
2390 private int getCsiParam(final int position
, final int defaultValue
,
2391 final int minValue
, final int maxValue
) {
2393 assert (minValue
<= maxValue
);
2394 int value
= getCsiParam(position
, defaultValue
);
2395 if (value
< minValue
) {
2398 if (value
> maxValue
) {
2405 * Set or unset a toggle.
2407 * @param value true for set ('h'), false for reset ('l')
2409 private void setToggle(final boolean value
) {
2410 boolean decPrivateModeFlag
= false;
2411 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2412 if (collectBuffer
.charAt(i
) == '?') {
2413 decPrivateModeFlag
= true;
2418 for (Integer i
: csiParams
) {
2423 if (decPrivateModeFlag
== true) {
2425 if (value
== true) {
2426 // Use application arrow keys
2427 arrowKeyMode
= ArrowKeyMode
.VT100
;
2429 // Use ANSI arrow keys
2430 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2435 if (decPrivateModeFlag
== true) {
2436 if (value
== false) {
2440 arrowKeyMode
= ArrowKeyMode
.VT52
;
2443 * From the VT102 docs: "You use ANSI mode to select
2444 * most terminal features; the terminal uses the same
2445 * features when it switches to VT52 mode. You
2446 * cannot, however, change most of these features in
2449 * In other words, do not reset any other attributes
2450 * when switching between VT52 submode and ANSI.
2452 * HOWEVER, the real vt100 does switch the character
2453 * set according to Usenet.
2455 currentState
.g0Charset
= CharacterSet
.US
;
2456 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2459 if ((type
== DeviceType
.VT220
)
2460 || (type
== DeviceType
.XTERM
)) {
2462 // VT52 mode is explicitly 7-bit
2464 singleshift
= Singleshift
.NONE
;
2469 if (value
== true) {
2470 // Turn off keyboard
2479 if (decPrivateModeFlag
== true) {
2481 if (value
== true) {
2490 width
= rightMargin
+ 1;
2491 // Entire screen is cleared, and scrolling region is
2493 eraseScreen(0, 0, height
- 1, width
- 1, false);
2494 scrollRegionTop
= 0;
2495 scrollRegionBottom
= height
- 1;
2496 // Also home the cursor
2497 cursorPosition(0, 0);
2501 if (decPrivateModeFlag
== true) {
2503 if (value
== true) {
2512 if (value
== true) {
2520 if (decPrivateModeFlag
== true) {
2522 if (value
== true) {
2524 * Set selects reverse screen, a white screen
2525 * background with black characters.
2527 if (reverseVideo
!= true) {
2529 * If in normal video, switch it back
2531 invertDisplayColors();
2533 reverseVideo
= true;
2536 * Reset selects normal screen, a black screen
2537 * background with white characters.
2539 if (reverseVideo
== true) {
2541 * If in reverse video already, switch it back
2543 invertDisplayColors();
2545 reverseVideo
= false;
2550 if (decPrivateModeFlag
== true) {
2552 if (value
== true) {
2553 // Origin is relative to scroll region cursor.
2554 // Cursor can NEVER leave scrolling region.
2555 currentState
.originMode
= true;
2556 cursorPosition(0, 0);
2558 // Origin is absolute to entire screen. Cursor can
2559 // leave the scrolling region via cup() and hvp().
2560 currentState
.originMode
= false;
2561 cursorPosition(0, 0);
2566 if (decPrivateModeFlag
== true) {
2568 if (value
== true) {
2570 currentState
.lineWrap
= true;
2572 // Turn linewrap off
2573 currentState
.lineWrap
= false;
2578 if (decPrivateModeFlag
== true) {
2580 if (value
== true) {
2581 // Keyboard auto-repeat on
2584 // Keyboard auto-repeat off
2590 if (decPrivateModeFlag
== false) {
2592 if (value
== true) {
2602 if (decPrivateModeFlag
== true) {
2608 if (decPrivateModeFlag
== true) {
2614 if (decPrivateModeFlag
== false) {
2616 if (value
== true) {
2618 * Set causes a received linefeed, form feed, or
2619 * vertical tab to move cursor to first column of
2620 * next line. RETURN transmits both a carriage return
2621 * and linefeed. This selection is also called new
2627 * Reset causes a received linefeed, form feed, or
2628 * vertical tab to move cursor to next line in
2629 * current column. RETURN transmits a carriage
2632 newLineMode
= false;
2638 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2639 if (decPrivateModeFlag
== true) {
2641 if (value
== true) {
2643 cursorVisible
= true;
2646 cursorVisible
= false;
2653 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2654 if (decPrivateModeFlag
== true) {
2656 if (value
== true) {
2657 // Select national mode NRC
2660 // Select multi-national mode
2669 if ((type
== DeviceType
.XTERM
)
2670 && (decPrivateModeFlag
== true)
2672 // Mouse: normal tracking mode
2673 if (value
== true) {
2674 mouseProtocol
= MouseProtocol
.NORMAL
;
2676 mouseProtocol
= MouseProtocol
.OFF
;
2682 if ((type
== DeviceType
.XTERM
)
2683 && (decPrivateModeFlag
== true)
2685 // Mouse: normal tracking mode
2686 if (value
== true) {
2687 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2689 mouseProtocol
= MouseProtocol
.OFF
;
2695 if ((type
== DeviceType
.XTERM
)
2696 && (decPrivateModeFlag
== true)
2698 // Mouse: Any-event tracking mode
2699 if (value
== true) {
2700 mouseProtocol
= MouseProtocol
.ANYEVENT
;
2702 mouseProtocol
= MouseProtocol
.OFF
;
2708 if ((type
== DeviceType
.XTERM
)
2709 && (decPrivateModeFlag
== true)
2711 // Mouse: UTF-8 coordinates
2712 if (value
== true) {
2713 mouseEncoding
= MouseEncoding
.UTF8
;
2715 mouseEncoding
= MouseEncoding
.X10
;
2721 if ((type
== DeviceType
.XTERM
)
2722 && (decPrivateModeFlag
== true)
2724 // Mouse: SGR coordinates
2725 if (value
== true) {
2726 mouseEncoding
= MouseEncoding
.SGR
;
2728 mouseEncoding
= MouseEncoding
.X10
;
2741 * DECSC - Save cursor.
2743 private void decsc() {
2744 savedState
.setTo(currentState
);
2748 * DECRC - Restore cursor.
2750 private void decrc() {
2751 currentState
.setTo(savedState
);
2757 private void ind() {
2758 // Move the cursor and scroll if necessary. If at the bottom line
2759 // already, a scroll up is supposed to be performed.
2760 if (currentState
.cursorY
== scrollRegionBottom
) {
2761 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2763 cursorDown(1, true);
2767 * RI - Reverse index.
2770 // Move the cursor and scroll if necessary. If at the top line
2771 // already, a scroll down is supposed to be performed.
2772 if (currentState
.cursorY
== scrollRegionTop
) {
2773 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2781 private void nel() {
2782 // Move the cursor and scroll if necessary. If at the bottom line
2783 // already, a scroll up is supposed to be performed.
2784 if (currentState
.cursorY
== scrollRegionBottom
) {
2785 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2787 cursorDown(1, true);
2789 // Reset to the beginning of the next line
2790 currentState
.cursorX
= 0;
2794 * DECKPAM - Keypad application mode.
2796 private void deckpam() {
2797 keypadMode
= KeypadMode
.Application
;
2801 * DECKPNM - Keypad numeric mode.
2803 private void deckpnm() {
2804 keypadMode
= KeypadMode
.Numeric
;
2810 * @param n number of spaces to move
2811 * @param honorScrollRegion if true, then do nothing if the cursor is
2812 * outside the scrolling region
2814 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2818 * Special case: if a user moves the cursor from the right margin, we
2819 * have to reset the VT100 right margin flag.
2822 wrapLineFlag
= false;
2825 for (int i
= 0; i
< n
; i
++) {
2826 if (honorScrollRegion
== true) {
2827 // Honor the scrolling region
2828 if ((currentState
.cursorY
< scrollRegionTop
)
2829 || (currentState
.cursorY
> scrollRegionBottom
)
2831 // Outside region, do nothing
2834 // Inside region, go up
2835 top
= scrollRegionTop
;
2837 // Non-scrolling case
2841 if (currentState
.cursorY
> top
) {
2842 currentState
.cursorY
--;
2848 * Move down n spaces.
2850 * @param n number of spaces to move
2851 * @param honorScrollRegion if true, then do nothing if the cursor is
2852 * outside the scrolling region
2854 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
2858 * Special case: if a user moves the cursor from the right margin, we
2859 * have to reset the VT100 right margin flag.
2862 wrapLineFlag
= false;
2865 for (int i
= 0; i
< n
; i
++) {
2867 if (honorScrollRegion
== true) {
2868 // Honor the scrolling region
2869 if (currentState
.cursorY
> scrollRegionBottom
) {
2870 // Outside region, do nothing
2873 // Inside region, go down
2874 bottom
= scrollRegionBottom
;
2876 // Non-scrolling case
2877 bottom
= height
- 1;
2880 if (currentState
.cursorY
< bottom
) {
2881 currentState
.cursorY
++;
2887 * Move left n spaces.
2889 * @param n number of spaces to move
2890 * @param honorScrollRegion if true, then do nothing if the cursor is
2891 * outside the scrolling region
2893 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
2895 * Special case: if a user moves the cursor from the right margin, we
2896 * have to reset the VT100 right margin flag.
2899 wrapLineFlag
= false;
2902 for (int i
= 0; i
< n
; i
++) {
2903 if (honorScrollRegion
== true) {
2904 // Honor the scrolling region
2905 if ((currentState
.cursorY
< scrollRegionTop
)
2906 || (currentState
.cursorY
> scrollRegionBottom
)
2908 // Outside region, do nothing
2913 if (currentState
.cursorX
> 0) {
2914 currentState
.cursorX
--;
2920 * Move right n spaces.
2922 * @param n number of spaces to move
2923 * @param honorScrollRegion if true, then do nothing if the cursor is
2924 * outside the scrolling region
2926 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
2927 int rightMargin
= this.rightMargin
;
2930 * Special case: if a user moves the cursor from the right margin, we
2931 * have to reset the VT100 right margin flag.
2934 wrapLineFlag
= false;
2937 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2938 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2941 for (int i
= 0; i
< n
; i
++) {
2942 if (honorScrollRegion
== true) {
2943 // Honor the scrolling region
2944 if ((currentState
.cursorY
< scrollRegionTop
)
2945 || (currentState
.cursorY
> scrollRegionBottom
)
2947 // Outside region, do nothing
2952 if (currentState
.cursorX
< rightMargin
) {
2953 currentState
.cursorX
++;
2959 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2961 * @param row row to move to
2962 * @param col column to move to
2964 private void cursorPosition(int row
, final int col
) {
2965 int rightMargin
= this.rightMargin
;
2970 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2971 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2974 // Set column number
2975 currentState
.cursorX
= col
;
2977 // Sanity check, bring column back to margin.
2978 if (currentState
.cursorX
> rightMargin
) {
2979 currentState
.cursorX
= rightMargin
;
2983 if (currentState
.originMode
== true) {
2984 row
+= scrollRegionTop
;
2986 if (currentState
.cursorY
< row
) {
2987 cursorDown(row
- currentState
.cursorY
, false);
2988 } else if (currentState
.cursorY
> row
) {
2989 cursorUp(currentState
.cursorY
- row
, false);
2992 wrapLineFlag
= false;
2996 * HTS - Horizontal tabulation set.
2998 private void hts() {
2999 for (Integer stop
: tabStops
) {
3000 if (stop
== currentState
.cursorX
) {
3001 // Already have a tab stop here
3006 // Append a tab stop to the end of the array and resort them
3007 tabStops
.add(currentState
.cursorX
);
3008 Collections
.sort(tabStops
);
3012 * DECSWL - Single-width line.
3014 private void decswl() {
3015 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3016 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3020 * DECDWL - Double-width line.
3022 private void decdwl() {
3023 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3024 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3028 * DECHDL - Double-height + double-width line.
3030 * @param topHalf if true, this sets the row to be the top half row of a
3033 private void dechdl(final boolean topHalf
) {
3034 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3035 if (topHalf
== true) {
3036 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3038 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3043 * DECALN - Screen alignment display.
3045 private void decaln() {
3046 Cell newCell
= new Cell();
3047 newCell
.setChar('E');
3048 for (DisplayLine line
: display
) {
3049 for (int i
= 0; i
< line
.length(); i
++) {
3050 line
.replace(i
, newCell
);
3056 * DECSCL - Compatibility level.
3058 private void decscl() {
3059 int i
= getCsiParam(0, 0);
3060 int j
= getCsiParam(1, 0);
3064 currentState
.g0Charset
= CharacterSet
.US
;
3065 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3067 } else if (i
== 62) {
3069 if ((j
== 0) || (j
== 2)) {
3071 } else if (j
== 1) {
3078 * CUD - Cursor down.
3080 private void cud() {
3081 cursorDown(getCsiParam(0, 1, 1, height
), true);
3085 * CUF - Cursor forward.
3087 private void cuf() {
3088 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3092 * CUB - Cursor backward.
3094 private void cub() {
3095 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3101 private void cuu() {
3102 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3106 * CUP - Cursor position.
3108 private void cup() {
3109 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3110 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3114 * CNL - Cursor down and to column 1.
3116 private void cnl() {
3117 cursorDown(getCsiParam(0, 1, 1, height
), true);
3119 cursorLeft(currentState
.cursorX
, true);
3123 * CPL - Cursor up and to column 1.
3125 private void cpl() {
3126 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3128 cursorLeft(currentState
.cursorX
, true);
3132 * CHA - Cursor to column # in current row.
3134 private void cha() {
3135 cursorPosition(currentState
.cursorY
,
3136 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3140 * VPA - Cursor to row #, same column.
3142 private void vpa() {
3143 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3144 currentState
.cursorX
);
3148 * ED - Erase in display.
3151 boolean honorProtected
= false;
3152 boolean decPrivateModeFlag
= false;
3154 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3155 if (collectBuffer
.charAt(i
) == '?') {
3156 decPrivateModeFlag
= true;
3161 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3162 && (decPrivateModeFlag
== true)
3164 honorProtected
= true;
3167 int i
= getCsiParam(0, 0);
3170 // Erase from here to end of screen
3171 if (currentState
.cursorY
< height
- 1) {
3172 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3175 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3176 } else if (i
== 1) {
3177 // Erase from beginning of screen to here
3178 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3180 eraseLine(0, currentState
.cursorX
, honorProtected
);
3181 } else if (i
== 2) {
3182 // Erase entire screen
3183 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3188 * EL - Erase in line.
3191 boolean honorProtected
= false;
3192 boolean decPrivateModeFlag
= false;
3194 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3195 if (collectBuffer
.charAt(i
) == '?') {
3196 decPrivateModeFlag
= true;
3201 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3202 && (decPrivateModeFlag
== true)
3204 honorProtected
= true;
3207 int i
= getCsiParam(0, 0);
3210 // Erase from here to end of line
3211 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3212 } else if (i
== 1) {
3213 // Erase from beginning of line to here
3214 eraseLine(0, currentState
.cursorX
, honorProtected
);
3215 } else if (i
== 2) {
3216 // Erase entire line
3217 eraseLine(0, width
- 1, honorProtected
);
3222 * ECH - Erase # of characters in current row.
3224 private void ech() {
3225 int i
= getCsiParam(0, 1, 1, width
);
3227 // Erase from here to i characters
3228 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3235 int i
= getCsiParam(0, 1);
3237 if ((currentState
.cursorY
>= scrollRegionTop
)
3238 && (currentState
.cursorY
<= scrollRegionBottom
)
3241 // I can get the same effect with a scroll-down
3242 scrollingRegionScrollDown(currentState
.cursorY
,
3243 scrollRegionBottom
, i
);
3248 * DCH - Delete char.
3250 private void dch() {
3251 int n
= getCsiParam(0, 1);
3252 DisplayLine line
= display
.get(currentState
.cursorY
);
3253 Cell blank
= new Cell();
3254 for (int i
= 0; i
< n
; i
++) {
3255 line
.delete(currentState
.cursorX
, blank
);
3260 * ICH - Insert blank char at cursor.
3262 private void ich() {
3263 int n
= getCsiParam(0, 1);
3264 DisplayLine line
= display
.get(currentState
.cursorY
);
3265 Cell blank
= new Cell();
3266 for (int i
= 0; i
< n
; i
++) {
3267 line
.insert(currentState
.cursorX
, blank
);
3275 int i
= getCsiParam(0, 1);
3277 if ((currentState
.cursorY
>= scrollRegionTop
)
3278 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3280 // I can get the same effect with a scroll-down
3281 scrollingRegionScrollUp(currentState
.cursorY
,
3282 scrollRegionBottom
, i
);
3287 * HVP - Horizontal and vertical position.
3289 private void hvp() {
3294 * REP - Repeat character.
3296 private void rep() {
3297 int n
= getCsiParam(0, 1);
3298 for (int i
= 0; i
< n
; i
++) {
3299 printCharacter(repCh
);
3307 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3308 getCsiParam(0, 1, 1, height
));
3315 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3316 getCsiParam(0, 1, 1, height
));
3320 * CBT - Go back X tab stops.
3322 private void cbt() {
3323 int tabsToMove
= getCsiParam(0, 1);
3326 for (int i
= 0; i
< tabsToMove
; i
++) {
3327 int j
= currentState
.cursorX
;
3328 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3329 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3337 j
= tabStops
.get(tabI
);
3339 cursorPosition(currentState
.cursorY
, j
);
3344 * CHT - Advance X tab stops.
3346 private void cht() {
3347 int n
= getCsiParam(0, 1);
3348 for (int i
= 0; i
< n
; i
++) {
3349 advanceToNextTabStop();
3354 * SGR - Select graphics rendition.
3356 private void sgr() {
3358 if (csiParams
.size() == 0) {
3359 currentState
.attr
.reset();
3363 for (Integer i
: csiParams
) {
3369 currentState
.attr
.reset();
3374 currentState
.attr
.setBold(true);
3379 currentState
.attr
.setUnderline(true);
3384 currentState
.attr
.setBlink(true);
3389 currentState
.attr
.setReverse(true);
3396 if (type
== DeviceType
.XTERM
) {
3410 if ((type
== DeviceType
.VT220
)
3411 || (type
== DeviceType
.XTERM
)) {
3417 currentState
.attr
.setBold(false);
3422 currentState
.attr
.setUnderline(false);
3427 currentState
.attr
.setBlink(false);
3432 currentState
.attr
.setReverse(false);
3440 // A true VT100/102/220 does not support color, however everyone
3441 // is used to their terminal emulator supporting color so we will
3442 // unconditionally support color for all DeviceType's.
3447 // Set black foreground
3448 currentState
.attr
.setForeColor(Color
.BLACK
);
3451 // Set red foreground
3452 currentState
.attr
.setForeColor(Color
.RED
);
3455 // Set green foreground
3456 currentState
.attr
.setForeColor(Color
.GREEN
);
3459 // Set yellow foreground
3460 currentState
.attr
.setForeColor(Color
.YELLOW
);
3463 // Set blue foreground
3464 currentState
.attr
.setForeColor(Color
.BLUE
);
3467 // Set magenta foreground
3468 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3471 // Set cyan foreground
3472 currentState
.attr
.setForeColor(Color
.CYAN
);
3475 // Set white foreground
3476 currentState
.attr
.setForeColor(Color
.WHITE
);
3479 if (type
== DeviceType
.XTERM
) {
3481 * Xterm supports T.416 / ISO-8613-3 codes to select
3482 * either an indexed color or an RGB value. (It also
3483 * permits these ISO-8613-3 SGR sequences to be separated
3484 * by colons rather than semicolons.)
3486 * We will not support any of these additional color
3487 * codes at this time:
3489 * 1. http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
3490 * has a detailed discussion of the current state of
3491 * RGB in various terminals, the point of which is
3492 * that none of them really do the same thing despite
3493 * all appearing to be "xterm".
3496 * https://bugs.kde.org/show_bug.cgi?id=107487#c3,
3497 * even supporting just the "indexed mode" of these
3498 * sequences (which could align easily with existing
3499 * SGR colors) is assumed to mean full support of
3500 * 24-bit RGB. So it is all or nothing.
3502 * Finally, these sequences break the assumptions of
3503 * standard ECMA-48 style parsers as pointed out at
3504 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
3505 * Therefore in order to keep a clean display, we cannot
3506 * parse anything else in this sequence.
3510 // Underscore on, default foreground color
3511 currentState
.attr
.setUnderline(true);
3512 currentState
.attr
.setForeColor(Color
.WHITE
);
3516 // Underscore off, default foreground color
3517 currentState
.attr
.setUnderline(false);
3518 currentState
.attr
.setForeColor(Color
.WHITE
);
3521 // Set black background
3522 currentState
.attr
.setBackColor(Color
.BLACK
);
3525 // Set red background
3526 currentState
.attr
.setBackColor(Color
.RED
);
3529 // Set green background
3530 currentState
.attr
.setBackColor(Color
.GREEN
);
3533 // Set yellow background
3534 currentState
.attr
.setBackColor(Color
.YELLOW
);
3537 // Set blue background
3538 currentState
.attr
.setBackColor(Color
.BLUE
);
3541 // Set magenta background
3542 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3545 // Set cyan background
3546 currentState
.attr
.setBackColor(Color
.CYAN
);
3549 // Set white background
3550 currentState
.attr
.setBackColor(Color
.WHITE
);
3553 if (type
== DeviceType
.XTERM
) {
3555 * Xterm supports T.416 / ISO-8613-3 codes to select
3556 * either an indexed color or an RGB value. (It also
3557 * permits these ISO-8613-3 SGR sequences to be separated
3558 * by colons rather than semicolons.)
3560 * We will not support this at this time. Also, in order
3561 * to keep a clean display, we cannot parse anything else
3568 // Default background
3569 currentState
.attr
.setBackColor(Color
.BLACK
);
3579 * DA - Device attributes.
3582 int extendedFlag
= 0;
3584 if (collectBuffer
.length() > 0) {
3585 String args
= collectBuffer
.substring(1);
3586 if (collectBuffer
.charAt(0) == '>') {
3588 if (collectBuffer
.length() >= 2) {
3589 i
= Integer
.parseInt(args
.toString());
3591 } else if (collectBuffer
.charAt(0) == '=') {
3593 if (collectBuffer
.length() >= 2) {
3594 i
= Integer
.parseInt(args
.toString());
3597 // Unknown code, bail out
3602 if ((i
!= 0) && (i
!= 1)) {
3606 if ((extendedFlag
== 0) && (i
== 0)) {
3607 // Send string directly to remote side
3608 writeRemote(deviceTypeResponse());
3612 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3614 if ((extendedFlag
== 1) && (i
== 0)) {
3616 * Request "What type of terminal are you, what is your
3617 * firmware version, and what hardware options do you have
3620 * Respond: "I am a VT220 (identification code of 1), my
3621 * firmware version is _____ (Pv), and I have _____ Po
3622 * options installed."
3628 if (s8c1t
== true) {
3629 writeRemote("\u009b>1;10;0c");
3631 writeRemote("\033[>1;10;0c");
3637 if ((extendedFlag
== 2) && (i
== 0)) {
3640 * Request "What is your unit ID?"
3642 * Respond: "I was manufactured at site 00 and have a unique ID
3646 writeRemote("\033P!|00010203\033\\");
3651 * DECSTBM - Set top and bottom margins.
3653 private void decstbm() {
3654 boolean decPrivateModeFlag
= false;
3656 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3657 if (collectBuffer
.charAt(i
) == '?') {
3658 decPrivateModeFlag
= true;
3662 if (decPrivateModeFlag
) {
3663 // This could be restore DEC private mode values.
3667 int top
= getCsiParam(0, 1, 1, height
) - 1;
3668 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3673 scrollRegionTop
= top
;
3674 scrollRegionBottom
= bottom
;
3677 cursorPosition(0, 0);
3682 * DECREQTPARM - Request terminal parameters.
3684 private void decreqtparm() {
3685 int i
= getCsiParam(0, 0);
3687 if ((i
!= 0) && (i
!= 1)) {
3694 * Request terminal parameters.
3698 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3699 * (CLoCk MULtiplier = 1, STP option flags = 0)
3703 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3706 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3708 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3714 * DECSCA - Select Character Attributes.
3716 private void decsca() {
3717 int i
= getCsiParam(0, 0);
3719 if ((i
== 0) || (i
== 2)) {
3721 currentState
.attr
.setProtect(false);
3725 currentState
.attr
.setProtect(true);
3730 * DECSTR - Soft Terminal Reset.
3732 private void decstr() {
3733 // Do exactly like RIS - Reset to initial state
3735 // Do I clear screen too? I think so...
3736 eraseScreen(0, 0, height
- 1, width
- 1, false);
3737 cursorPosition(0, 0);
3741 * DSR - Device status report.
3743 private void dsr() {
3744 boolean decPrivateModeFlag
= false;
3745 int row
= currentState
.cursorY
;
3747 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3748 if (collectBuffer
.charAt(i
) == '?') {
3749 decPrivateModeFlag
= true;
3754 int i
= getCsiParam(0, 0);
3759 // Request status report. Respond with "OK, no malfunction."
3761 // Send string directly to remote side
3762 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3765 writeRemote("\u009b0n");
3767 writeRemote("\033[0n");
3772 // Request cursor position. Respond with current position.
3773 if (currentState
.originMode
== true) {
3774 row
-= scrollRegionTop
;
3777 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3780 str
= String
.format("\u009b%d;%dR", row
+ 1,
3781 currentState
.cursorX
+ 1);
3783 str
= String
.format("\033[%d;%dR", row
+ 1,
3784 currentState
.cursorX
+ 1);
3787 // Send string directly to remote side
3792 if (decPrivateModeFlag
== true) {
3794 // Request printer status report. Respond with "Printer not
3797 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3798 && (s8c1t
== true)) {
3799 writeRemote("\u009b?13n");
3801 writeRemote("\033[?13n");
3807 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3808 && (decPrivateModeFlag
== true)
3811 // Request user-defined keys are locked or unlocked. Respond
3812 // with "User-defined keys are locked."
3814 if (s8c1t
== true) {
3815 writeRemote("\u009b?21n");
3817 writeRemote("\033[?21n");
3823 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3824 && (decPrivateModeFlag
== true)
3827 // Request keyboard language. Respond with "Keyboard
3828 // language is North American."
3830 if (s8c1t
== true) {
3831 writeRemote("\u009b?27;1n");
3833 writeRemote("\033[?27;1n");
3840 // Some other option, ignore
3846 * TBC - Tabulation clear.
3848 private void tbc() {
3849 int i
= getCsiParam(0, 0);
3851 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3852 for (Integer stop
: tabStops
) {
3853 if (stop
== currentState
.cursorX
) {
3858 tabStops
= newStops
;
3866 * Erase the characters in the current line from the start column to the
3867 * end column, inclusive.
3869 * @param start starting column to erase (between 0 and width - 1)
3870 * @param end ending column to erase (between 0 and width - 1)
3871 * @param honorProtected if true, do not erase characters with the
3872 * protected attribute set
3874 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3879 if (end
> width
- 1) {
3886 for (int i
= start
; i
<= end
; i
++) {
3887 DisplayLine line
= display
.get(currentState
.cursorY
);
3888 if ((!honorProtected
)
3889 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
3896 * From the VT102 manual:
3898 * Erasing a character also erases any character
3899 * attribute of the character.
3905 * Erase with the current color a.k.a. back-color erase
3908 line
.setChar(i
, ' ');
3909 line
.setAttr(i
, currentState
.attr
);
3917 * Erase a rectangular section of the screen, inclusive. end column,
3920 * @param startRow starting row to erase (between 0 and height - 1)
3921 * @param startCol starting column to erase (between 0 and width - 1)
3922 * @param endRow ending row to erase (between 0 and height - 1)
3923 * @param endCol ending column to erase (between 0 and width - 1)
3924 * @param honorProtected if true, do not erase characters with the
3925 * protected attribute set
3927 private void eraseScreen(final int startRow
, final int startCol
,
3928 final int endRow
, final int endCol
, final boolean honorProtected
) {
3936 || (endRow
< startRow
)
3937 || (endCol
< startCol
)
3942 oldCursorY
= currentState
.cursorY
;
3943 for (int i
= startRow
; i
<= endRow
; i
++) {
3944 currentState
.cursorY
= i
;
3945 eraseLine(startCol
, endCol
, honorProtected
);
3947 // Erase display clears the double attributes
3948 display
.get(i
).setDoubleWidth(false);
3949 display
.get(i
).setDoubleHeight(0);
3951 currentState
.cursorY
= oldCursorY
;
3955 * VT220 printer functions. All of these are parsed, but won't do
3958 private void printerFunctions() {
3959 boolean decPrivateModeFlag
= false;
3960 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3961 if (collectBuffer
.charAt(i
) == '?') {
3962 decPrivateModeFlag
= true;
3967 int i
= getCsiParam(0, 0);
3972 if (decPrivateModeFlag
== false) {
3978 if (decPrivateModeFlag
== true) {
3979 // Print cursor line
3984 if (decPrivateModeFlag
== true) {
3985 // Auto print mode OFF
3987 // Printer controller OFF
3989 // Characters re-appear on the screen
3990 printerControllerMode
= false;
3995 if (decPrivateModeFlag
== true) {
3999 // Printer controller
4001 // Characters get sucked into oblivion
4002 printerControllerMode
= true;
4013 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4014 * of remote systems will send an XTerm title sequence even if TERM isn't
4017 * @param xtermChar the character received from the remote side
4019 private void oscPut(final char xtermChar
) {
4021 collectBuffer
.append(xtermChar
);
4024 if (xtermChar
== 0x07) {
4025 String args
= collectBuffer
.substring(0,
4026 collectBuffer
.length() - 1);
4027 String
[] p
= args
.toString().split(";");
4029 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4037 // Go to SCAN_GROUND state
4044 * Run this input character through the ECMA48 state machine.
4046 * @param ch character from the remote side
4048 private void consume(char ch
) {
4051 // System.err.printf("%c", ch);
4053 // Special case for VT10x: 7-bit characters only
4054 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4055 ch
= (char)(ch
& 0x7F);
4058 // Special "anywhere" states
4060 // 18, 1A --> execute, then switch to SCAN_GROUND
4061 if ((ch
== 0x18) || (ch
== 0x1A)) {
4062 // CAN and SUB abort escape sequences
4067 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4071 && (scanState
!= ScanState
.DCS_ENTRY
)
4072 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4073 && (scanState
!= ScanState
.DCS_IGNORE
)
4074 && (scanState
!= ScanState
.DCS_PARAM
)
4075 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4078 scanState
= ScanState
.ESCAPE
;
4082 // 0x9B == CSI 8-bit sequence
4084 scanState
= ScanState
.CSI_ENTRY
;
4088 // 0x9D goes to ScanState.OSC_STRING
4090 scanState
= ScanState
.OSC_STRING
;
4094 // 0x90 goes to DCS_ENTRY
4096 scanState
= ScanState
.DCS_ENTRY
;
4100 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4101 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4102 scanState
= ScanState
.SOSPMAPC_STRING
;
4106 // 0x7F (DEL) is always discarded
4111 switch (scanState
) {
4114 // 00-17, 19, 1C-1F --> execute
4115 // 80-8F, 91-9A, 9C --> execute
4116 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4117 handleControlChar(ch
);
4121 if (((ch
>= 0x20) && (ch
<= 0x7F))
4125 // VT220 printer --> trash bin
4126 if (((type
== DeviceType
.VT220
)
4127 || (type
== DeviceType
.XTERM
))
4128 && (printerControllerMode
== true)
4133 // Hang onto this character
4134 repCh
= mapCharacter(ch
);
4136 // Print this character
4137 printCharacter(repCh
);
4142 // 00-17, 19, 1C-1F --> execute
4144 handleControlChar(ch
);
4148 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4149 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4151 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4155 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4156 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4167 // DECSC - Save cursor
4168 // Note this code overlaps both ANSI and VT52 mode
4173 // DECRC - Restore cursor
4174 // Note this code overlaps both ANSI and VT52 mode
4183 if (vt52Mode
== true) {
4184 // DECANM - Enter ANSI mode
4186 arrowKeyMode
= ArrowKeyMode
.VT100
;
4189 * From the VT102 docs: "You use ANSI mode to select
4190 * most terminal features; the terminal uses the same
4191 * features when it switches to VT52 mode. You
4192 * cannot, however, change most of these features in
4195 * In other words, do not reset any other attributes
4196 * when switching between VT52 submode and ANSI.
4200 currentState
.g0Charset
= CharacterSet
.US
;
4201 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4203 singleshift
= Singleshift
.NONE
;
4204 currentState
.glLockshift
= LockshiftMode
.NONE
;
4205 currentState
.grLockshift
= LockshiftMode
.NONE
;
4209 // DECKPAM - Keypad application mode
4210 // Note this code overlaps both ANSI and VT52 mode
4214 // DECKPNM - Keypad numeric mode
4215 // Note this code overlaps both ANSI and VT52 mode
4222 if (vt52Mode
== true) {
4223 // Cursor up, and stop at the top without scrolling
4228 if (vt52Mode
== true) {
4229 // Cursor down, and stop at the bottom without scrolling
4230 cursorDown(1, false);
4234 if (vt52Mode
== true) {
4235 // Cursor right, and stop at the right without scrolling
4236 cursorRight(1, false);
4240 if (vt52Mode
== true) {
4241 // Cursor left, and stop at the left without scrolling
4242 cursorLeft(1, false);
4249 if (vt52Mode
== true) {
4257 if (vt52Mode
== true) {
4258 // G0 --> Special graphics
4259 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
4263 if (vt52Mode
== true) {
4265 currentState
.g0Charset
= CharacterSet
.US
;
4269 if (vt52Mode
== true) {
4271 cursorPosition(0, 0);
4273 // HTS - Horizontal tabulation set
4278 if (vt52Mode
== true) {
4279 // Reverse line feed. Same as RI.
4284 if (vt52Mode
== true) {
4285 // Erase to end of screen
4286 eraseLine(currentState
.cursorX
, width
- 1, false);
4287 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4292 if (vt52Mode
== true) {
4293 // Erase to end of line
4294 eraseLine(currentState
.cursorX
, width
- 1, false);
4300 if (vt52Mode
== true) {
4303 // RI - Reverse index
4308 if (vt52Mode
== false) {
4310 singleshift
= Singleshift
.SS2
;
4314 if (vt52Mode
== false) {
4316 singleshift
= Singleshift
.SS3
;
4323 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4339 if (vt52Mode
== true) {
4340 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4348 if (vt52Mode
== true) {
4350 // Send string directly to remote side
4351 writeRemote("\033/Z");
4354 // Send string directly to remote side
4355 writeRemote(deviceTypeResponse());
4366 // VT52 cannot get to any of these other states
4367 if (vt52Mode
== true) {
4372 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4379 // RIS - Reset to initial state
4381 // Do I clear screen too? I think so...
4382 eraseScreen(0, 0, height
- 1, width
- 1, false);
4383 cursorPosition(0, 0);
4397 if ((type
== DeviceType
.VT220
)
4398 || (type
== DeviceType
.XTERM
)) {
4400 // VT220 lockshift G2 into GL
4401 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4406 if ((type
== DeviceType
.VT220
)
4407 || (type
== DeviceType
.XTERM
)) {
4409 // VT220 lockshift G3 into GL
4410 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4428 if ((type
== DeviceType
.VT220
)
4429 || (type
== DeviceType
.XTERM
)) {
4431 // VT220 lockshift G3 into GR
4432 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4437 if ((type
== DeviceType
.VT220
)
4438 || (type
== DeviceType
.XTERM
)) {
4440 // VT220 lockshift G2 into GR
4441 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4447 if ((type
== DeviceType
.VT220
)
4448 || (type
== DeviceType
.XTERM
)) {
4450 // VT220 lockshift G1 into GR
4451 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4461 // 0x5B goes to CSI_ENTRY
4463 scanState
= ScanState
.CSI_ENTRY
;
4466 // 0x5D goes to OSC_STRING
4468 scanState
= ScanState
.OSC_STRING
;
4471 // 0x50 goes to DCS_ENTRY
4473 scanState
= ScanState
.DCS_ENTRY
;
4476 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4477 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4478 scanState
= ScanState
.SOSPMAPC_STRING
;
4483 case ESCAPE_INTERMEDIATE
:
4484 // 00-17, 19, 1C-1F --> execute
4486 handleControlChar(ch
);
4489 // 20-2F --> collect
4490 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4494 // 30-7E --> dispatch, then switch to GROUND
4495 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4498 if ((collectBuffer
.length() == 1)
4499 && (collectBuffer
.charAt(0) == '(')) {
4500 // G0 --> Special graphics
4501 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4503 if ((collectBuffer
.length() == 1)
4504 && (collectBuffer
.charAt(0) == ')')) {
4505 // G1 --> Special graphics
4506 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4508 if ((type
== DeviceType
.VT220
)
4509 || (type
== DeviceType
.XTERM
)) {
4511 if ((collectBuffer
.length() == 1)
4512 && (collectBuffer
.charAt(0) == '*')) {
4513 // G2 --> Special graphics
4514 currentState
.g2Charset
= CharacterSet
.DRAWING
;
4516 if ((collectBuffer
.length() == 1)
4517 && (collectBuffer
.charAt(0) == '+')) {
4518 // G3 --> Special graphics
4519 currentState
.g3Charset
= CharacterSet
.DRAWING
;
4524 if ((collectBuffer
.length() == 1)
4525 && (collectBuffer
.charAt(0) == '(')) {
4526 // G0 --> Alternate character ROM standard character set
4527 currentState
.g0Charset
= CharacterSet
.ROM
;
4529 if ((collectBuffer
.length() == 1)
4530 && (collectBuffer
.charAt(0) == ')')) {
4531 // G1 --> Alternate character ROM standard character set
4532 currentState
.g1Charset
= CharacterSet
.ROM
;
4536 if ((collectBuffer
.length() == 1)
4537 && (collectBuffer
.charAt(0) == '(')) {
4538 // G0 --> Alternate character ROM special graphics
4539 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4541 if ((collectBuffer
.length() == 1)
4542 && (collectBuffer
.charAt(0) == ')')) {
4543 // G1 --> Alternate character ROM special graphics
4544 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4548 if ((collectBuffer
.length() == 1)
4549 && (collectBuffer
.charAt(0) == '#')) {
4550 // DECDHL - Double-height line (top half)
4555 if ((collectBuffer
.length() == 1)
4556 && (collectBuffer
.charAt(0) == '#')) {
4557 // DECDHL - Double-height line (bottom half)
4560 if ((type
== DeviceType
.VT220
)
4561 || (type
== DeviceType
.XTERM
)) {
4563 if ((collectBuffer
.length() == 1)
4564 && (collectBuffer
.charAt(0) == '(')) {
4566 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4568 if ((collectBuffer
.length() == 1)
4569 && (collectBuffer
.charAt(0) == ')')) {
4571 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4573 if ((collectBuffer
.length() == 1)
4574 && (collectBuffer
.charAt(0) == '*')) {
4576 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4578 if ((collectBuffer
.length() == 1)
4579 && (collectBuffer
.charAt(0) == '+')) {
4581 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4586 if ((collectBuffer
.length() == 1)
4587 && (collectBuffer
.charAt(0) == '#')) {
4588 // DECSWL - Single-width line
4591 if ((type
== DeviceType
.VT220
)
4592 || (type
== DeviceType
.XTERM
)) {
4594 if ((collectBuffer
.length() == 1)
4595 && (collectBuffer
.charAt(0) == '(')) {
4597 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4599 if ((collectBuffer
.length() == 1)
4600 && (collectBuffer
.charAt(0) == ')')) {
4602 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4604 if ((collectBuffer
.length() == 1)
4605 && (collectBuffer
.charAt(0) == '*')) {
4607 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4609 if ((collectBuffer
.length() == 1)
4610 && (collectBuffer
.charAt(0) == '+')) {
4612 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4617 if ((collectBuffer
.length() == 1)
4618 && (collectBuffer
.charAt(0) == '#')) {
4619 // DECDWL - Double-width line
4622 if ((type
== DeviceType
.VT220
)
4623 || (type
== DeviceType
.XTERM
)) {
4625 if ((collectBuffer
.length() == 1)
4626 && (collectBuffer
.charAt(0) == '(')) {
4628 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4630 if ((collectBuffer
.length() == 1)
4631 && (collectBuffer
.charAt(0) == ')')) {
4633 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4635 if ((collectBuffer
.length() == 1)
4636 && (collectBuffer
.charAt(0) == '*')) {
4638 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4640 if ((collectBuffer
.length() == 1)
4641 && (collectBuffer
.charAt(0) == '+')) {
4643 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4648 if ((type
== DeviceType
.VT220
)
4649 || (type
== DeviceType
.XTERM
)) {
4651 if ((collectBuffer
.length() == 1)
4652 && (collectBuffer
.charAt(0) == '(')) {
4654 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4656 if ((collectBuffer
.length() == 1)
4657 && (collectBuffer
.charAt(0) == ')')) {
4659 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4661 if ((collectBuffer
.length() == 1)
4662 && (collectBuffer
.charAt(0) == '*')) {
4664 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4666 if ((collectBuffer
.length() == 1)
4667 && (collectBuffer
.charAt(0) == '+')) {
4669 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4674 if ((collectBuffer
.length() == 1)
4675 && (collectBuffer
.charAt(0) == '#')) {
4676 // DECALN - Screen alignment display
4685 if ((type
== DeviceType
.VT220
)
4686 || (type
== DeviceType
.XTERM
)) {
4688 if ((collectBuffer
.length() == 1)
4689 && (collectBuffer
.charAt(0) == '(')) {
4690 // G0 --> DEC_SUPPLEMENTAL
4691 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4693 if ((collectBuffer
.length() == 1)
4694 && (collectBuffer
.charAt(0) == ')')) {
4695 // G1 --> DEC_SUPPLEMENTAL
4696 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4698 if ((collectBuffer
.length() == 1)
4699 && (collectBuffer
.charAt(0) == '*')) {
4700 // G2 --> DEC_SUPPLEMENTAL
4701 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4703 if ((collectBuffer
.length() == 1)
4704 && (collectBuffer
.charAt(0) == '+')) {
4705 // G3 --> DEC_SUPPLEMENTAL
4706 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4711 if ((type
== DeviceType
.VT220
)
4712 || (type
== DeviceType
.XTERM
)) {
4714 if ((collectBuffer
.length() == 1)
4715 && (collectBuffer
.charAt(0) == '(')) {
4717 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4719 if ((collectBuffer
.length() == 1)
4720 && (collectBuffer
.charAt(0) == ')')) {
4722 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4724 if ((collectBuffer
.length() == 1)
4725 && (collectBuffer
.charAt(0) == '*')) {
4727 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4729 if ((collectBuffer
.length() == 1)
4730 && (collectBuffer
.charAt(0) == '+')) {
4732 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4741 if ((collectBuffer
.length() == 1)
4742 && (collectBuffer
.charAt(0) == '(')) {
4743 // G0 --> United Kingdom set
4744 currentState
.g0Charset
= CharacterSet
.UK
;
4746 if ((collectBuffer
.length() == 1)
4747 && (collectBuffer
.charAt(0) == ')')) {
4748 // G1 --> United Kingdom set
4749 currentState
.g1Charset
= CharacterSet
.UK
;
4751 if ((type
== DeviceType
.VT220
)
4752 || (type
== DeviceType
.XTERM
)) {
4754 if ((collectBuffer
.length() == 1)
4755 && (collectBuffer
.charAt(0) == '*')) {
4756 // G2 --> United Kingdom set
4757 currentState
.g2Charset
= CharacterSet
.UK
;
4759 if ((collectBuffer
.length() == 1)
4760 && (collectBuffer
.charAt(0) == '+')) {
4761 // G3 --> United Kingdom set
4762 currentState
.g3Charset
= CharacterSet
.UK
;
4767 if ((collectBuffer
.length() == 1)
4768 && (collectBuffer
.charAt(0) == '(')) {
4770 currentState
.g0Charset
= CharacterSet
.US
;
4772 if ((collectBuffer
.length() == 1)
4773 && (collectBuffer
.charAt(0) == ')')) {
4775 currentState
.g1Charset
= CharacterSet
.US
;
4777 if ((type
== DeviceType
.VT220
)
4778 || (type
== DeviceType
.XTERM
)) {
4780 if ((collectBuffer
.length() == 1)
4781 && (collectBuffer
.charAt(0) == '*')) {
4783 currentState
.g2Charset
= CharacterSet
.US
;
4785 if ((collectBuffer
.length() == 1)
4786 && (collectBuffer
.charAt(0) == '+')) {
4788 currentState
.g3Charset
= CharacterSet
.US
;
4793 if ((type
== DeviceType
.VT220
)
4794 || (type
== DeviceType
.XTERM
)) {
4796 if ((collectBuffer
.length() == 1)
4797 && (collectBuffer
.charAt(0) == '(')) {
4799 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4801 if ((collectBuffer
.length() == 1)
4802 && (collectBuffer
.charAt(0) == ')')) {
4804 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4806 if ((collectBuffer
.length() == 1)
4807 && (collectBuffer
.charAt(0) == '*')) {
4809 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4811 if ((collectBuffer
.length() == 1)
4812 && (collectBuffer
.charAt(0) == '+')) {
4814 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4821 if ((type
== DeviceType
.VT220
)
4822 || (type
== DeviceType
.XTERM
)) {
4824 if ((collectBuffer
.length() == 1)
4825 && (collectBuffer
.charAt(0) == '(')) {
4827 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4829 if ((collectBuffer
.length() == 1)
4830 && (collectBuffer
.charAt(0) == ')')) {
4832 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4834 if ((collectBuffer
.length() == 1)
4835 && (collectBuffer
.charAt(0) == '*')) {
4837 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4839 if ((collectBuffer
.length() == 1)
4840 && (collectBuffer
.charAt(0) == '+')) {
4842 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4847 if ((type
== DeviceType
.VT220
)
4848 || (type
== DeviceType
.XTERM
)) {
4850 if ((collectBuffer
.length() == 1)
4851 && (collectBuffer
.charAt(0) == ' ')) {
4858 if ((type
== DeviceType
.VT220
)
4859 || (type
== DeviceType
.XTERM
)) {
4861 if ((collectBuffer
.length() == 1)
4862 && (collectBuffer
.charAt(0) == ' ')) {
4869 if ((type
== DeviceType
.VT220
)
4870 || (type
== DeviceType
.XTERM
)) {
4872 if ((collectBuffer
.length() == 1)
4873 && (collectBuffer
.charAt(0) == '(')) {
4875 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4877 if ((collectBuffer
.length() == 1)
4878 && (collectBuffer
.charAt(0) == ')')) {
4880 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4882 if ((collectBuffer
.length() == 1)
4883 && (collectBuffer
.charAt(0) == '*')) {
4885 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4887 if ((collectBuffer
.length() == 1)
4888 && (collectBuffer
.charAt(0) == '+')) {
4890 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4898 if ((type
== DeviceType
.VT220
)
4899 || (type
== DeviceType
.XTERM
)) {
4901 if ((collectBuffer
.length() == 1)
4902 && (collectBuffer
.charAt(0) == '(')) {
4904 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4906 if ((collectBuffer
.length() == 1)
4907 && (collectBuffer
.charAt(0) == ')')) {
4909 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4911 if ((collectBuffer
.length() == 1)
4912 && (collectBuffer
.charAt(0) == '*')) {
4914 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4916 if ((collectBuffer
.length() == 1)
4917 && (collectBuffer
.charAt(0) == '+')) {
4919 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4930 if ((type
== DeviceType
.VT220
)
4931 || (type
== DeviceType
.XTERM
)) {
4933 if ((collectBuffer
.length() == 1)
4934 && (collectBuffer
.charAt(0) == '(')) {
4936 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4938 if ((collectBuffer
.length() == 1)
4939 && (collectBuffer
.charAt(0) == ')')) {
4941 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4943 if ((collectBuffer
.length() == 1)
4944 && (collectBuffer
.charAt(0) == '*')) {
4946 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4948 if ((collectBuffer
.length() == 1)
4949 && (collectBuffer
.charAt(0) == '+')) {
4951 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4956 if ((type
== DeviceType
.VT220
)
4957 || (type
== DeviceType
.XTERM
)) {
4959 if ((collectBuffer
.length() == 1)
4960 && (collectBuffer
.charAt(0) == '(')) {
4962 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4964 if ((collectBuffer
.length() == 1)
4965 && (collectBuffer
.charAt(0) == ')')) {
4967 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4969 if ((collectBuffer
.length() == 1)
4970 && (collectBuffer
.charAt(0) == '*')) {
4972 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
4974 if ((collectBuffer
.length() == 1)
4975 && (collectBuffer
.charAt(0) == '+')) {
4977 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
4989 if ((type
== DeviceType
.VT220
)
4990 || (type
== DeviceType
.XTERM
)) {
4992 if ((collectBuffer
.length() == 1)
4993 && (collectBuffer
.charAt(0) == '(')) {
4995 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
4997 if ((collectBuffer
.length() == 1)
4998 && (collectBuffer
.charAt(0) == ')')) {
5000 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5002 if ((collectBuffer
.length() == 1)
5003 && (collectBuffer
.charAt(0) == '*')) {
5005 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5007 if ((collectBuffer
.length() == 1)
5008 && (collectBuffer
.charAt(0) == '+')) {
5010 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5015 if ((type
== DeviceType
.VT220
)
5016 || (type
== DeviceType
.XTERM
)) {
5018 if ((collectBuffer
.length() == 1)
5019 && (collectBuffer
.charAt(0) == '(')) {
5021 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5023 if ((collectBuffer
.length() == 1)
5024 && (collectBuffer
.charAt(0) == ')')) {
5026 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5028 if ((collectBuffer
.length() == 1)
5029 && (collectBuffer
.charAt(0) == '*')) {
5031 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5033 if ((collectBuffer
.length() == 1)
5034 && (collectBuffer
.charAt(0) == '+')) {
5036 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5083 // 0x9C goes to GROUND
5091 // 00-17, 19, 1C-1F --> execute
5093 handleControlChar(ch
);
5096 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5097 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5099 scanState
= ScanState
.CSI_INTERMEDIATE
;
5102 // 30-39, 3B --> param, then switch to CSI_PARAM
5103 if ((ch
>= '0') && (ch
<= '9')) {
5105 scanState
= ScanState
.CSI_PARAM
;
5109 scanState
= ScanState
.CSI_PARAM
;
5112 // 3C-3F --> collect, then switch to CSI_PARAM
5113 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5115 scanState
= ScanState
.CSI_PARAM
;
5118 // 40-7E --> dispatch, then switch to GROUND
5119 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5122 // ICH - Insert character
5130 // CUD - Cursor down
5134 // CUF - Cursor forward
5138 // CUB - Cursor backward
5142 // CNL - Cursor down and to column 1
5143 if (type
== DeviceType
.XTERM
) {
5148 // CPL - Cursor up and to column 1
5149 if (type
== DeviceType
.XTERM
) {
5154 // CHA - Cursor to column # in current row
5155 if (type
== DeviceType
.XTERM
) {
5160 // CUP - Cursor position
5164 // CHT - Cursor forward X tab stops (default 1)
5165 if (type
== DeviceType
.XTERM
) {
5170 // ED - Erase in display
5174 // EL - Erase in line
5189 // DCH - Delete character
5196 // Scroll up X lines (default 1)
5197 if (type
== DeviceType
.XTERM
) {
5202 // Scroll down X lines (default 1)
5203 if (type
== DeviceType
.XTERM
) {
5212 if ((type
== DeviceType
.VT220
)
5213 || (type
== DeviceType
.XTERM
)) {
5215 // ECH - Erase character
5222 // CBT - Cursor backward X tab stops (default 1)
5223 if (type
== DeviceType
.XTERM
) {
5234 // HPA - Cursor to column # in current row. Same as CHA
5235 if (type
== DeviceType
.XTERM
) {
5240 // HPR - Cursor right. Same as CUF
5241 if (type
== DeviceType
.XTERM
) {
5246 // REP - Repeat last char X times
5247 if (type
== DeviceType
.XTERM
) {
5252 // DA - Device attributes
5256 // VPA - Cursor to row, current column.
5257 if (type
== DeviceType
.XTERM
) {
5262 // VPR - Cursor down. Same as CUD
5263 if (type
== DeviceType
.XTERM
) {
5268 // HVP - Horizontal and vertical position
5272 // TBC - Tabulation clear
5276 // Sets an ANSI or DEC private toggle
5280 if ((type
== DeviceType
.VT220
)
5281 || (type
== DeviceType
.XTERM
)) {
5283 // Printer functions
5291 // Sets an ANSI or DEC private toggle
5295 // SGR - Select graphics rendition
5299 // DSR - Device status report
5306 // DECLL - Load leds
5310 // DECSTBM - Set top and bottom margins
5314 // Save cursor (ANSI.SYS)
5315 if (type
== DeviceType
.XTERM
) {
5316 savedState
.cursorX
= currentState
.cursorX
;
5317 savedState
.cursorY
= currentState
.cursorY
;
5323 // Restore cursor (ANSI.SYS)
5324 if (type
== DeviceType
.XTERM
) {
5325 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5332 // DECREQTPARM - Request terminal parameters
5348 // 0x9C goes to GROUND
5353 // 0x3A goes to CSI_IGNORE
5355 scanState
= ScanState
.CSI_IGNORE
;
5360 // 00-17, 19, 1C-1F --> execute
5362 handleControlChar(ch
);
5365 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5366 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5368 scanState
= ScanState
.CSI_INTERMEDIATE
;
5371 // 30-39, 3B --> param
5372 if ((ch
>= '0') && (ch
<= '9')) {
5379 // 0x3A goes to CSI_IGNORE
5381 scanState
= ScanState
.CSI_IGNORE
;
5383 // 0x3C-3F goes to CSI_IGNORE
5384 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5385 scanState
= ScanState
.CSI_IGNORE
;
5388 // 40-7E --> dispatch, then switch to GROUND
5389 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5392 // ICH - Insert character
5400 // CUD - Cursor down
5404 // CUF - Cursor forward
5408 // CUB - Cursor backward
5412 // CNL - Cursor down and to column 1
5413 if (type
== DeviceType
.XTERM
) {
5418 // CPL - Cursor up and to column 1
5419 if (type
== DeviceType
.XTERM
) {
5424 // CHA - Cursor to column # in current row
5425 if (type
== DeviceType
.XTERM
) {
5430 // CUP - Cursor position
5434 // CHT - Cursor forward X tab stops (default 1)
5435 if (type
== DeviceType
.XTERM
) {
5440 // ED - Erase in display
5444 // EL - Erase in line
5459 // DCH - Delete character
5466 // Scroll up X lines (default 1)
5467 if (type
== DeviceType
.XTERM
) {
5472 // Scroll down X lines (default 1)
5473 if (type
== DeviceType
.XTERM
) {
5482 if ((type
== DeviceType
.VT220
)
5483 || (type
== DeviceType
.XTERM
)) {
5485 // ECH - Erase character
5492 // CBT - Cursor backward X tab stops (default 1)
5493 if (type
== DeviceType
.XTERM
) {
5504 // HPA - Cursor to column # in current row. Same as CHA
5505 if (type
== DeviceType
.XTERM
) {
5510 // HPR - Cursor right. Same as CUF
5511 if (type
== DeviceType
.XTERM
) {
5516 // REP - Repeat last char X times
5517 if (type
== DeviceType
.XTERM
) {
5522 // DA - Device attributes
5526 // VPA - Cursor to row, current column.
5527 if (type
== DeviceType
.XTERM
) {
5532 // VPR - Cursor down. Same as CUD
5533 if (type
== DeviceType
.XTERM
) {
5538 // HVP - Horizontal and vertical position
5542 // TBC - Tabulation clear
5546 // Sets an ANSI or DEC private toggle
5550 if ((type
== DeviceType
.VT220
)
5551 || (type
== DeviceType
.XTERM
)) {
5553 // Printer functions
5561 // Sets an ANSI or DEC private toggle
5565 // SGR - Select graphics rendition
5569 // DSR - Device status report
5576 // DECLL - Load leds
5580 // DECSTBM - Set top and bottom margins
5590 // DECREQTPARM - Request terminal parameters
5607 case CSI_INTERMEDIATE
:
5608 // 00-17, 19, 1C-1F --> execute
5610 handleControlChar(ch
);
5613 // 20-2F --> collect
5614 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5618 // 0x30-3F goes to CSI_IGNORE
5619 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5620 scanState
= ScanState
.CSI_IGNORE
;
5623 // 40-7E --> dispatch, then switch to GROUND
5624 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5676 if (((type
== DeviceType
.VT220
)
5677 || (type
== DeviceType
.XTERM
))
5678 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5680 // DECSCL - compatibility level
5683 if ((type
== DeviceType
.XTERM
)
5684 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
5686 // DECSTR - Soft terminal reset
5691 if (((type
== DeviceType
.VT220
)
5692 || (type
== DeviceType
.XTERM
))
5693 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5721 // 00-17, 19, 1C-1F --> execute
5723 handleControlChar(ch
);
5726 // 20-2F --> collect
5727 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5731 // 40-7E --> ignore, then switch to GROUND
5732 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5736 // 20-3F, 7F --> ignore
5742 // 0x9C goes to GROUND
5747 // 0x1B 0x5C goes to GROUND
5752 if ((collectBuffer
.length() > 0)
5753 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5759 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5760 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5762 scanState
= ScanState
.DCS_INTERMEDIATE
;
5765 // 30-39, 3B --> param, then switch to DCS_PARAM
5766 if ((ch
>= '0') && (ch
<= '9')) {
5768 scanState
= ScanState
.DCS_PARAM
;
5772 scanState
= ScanState
.DCS_PARAM
;
5775 // 3C-3F --> collect, then switch to DCS_PARAM
5776 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5778 scanState
= ScanState
.DCS_PARAM
;
5781 // 00-17, 19, 1C-1F, 7F --> ignore
5783 // 0x3A goes to DCS_IGNORE
5785 scanState
= ScanState
.DCS_IGNORE
;
5788 // 0x40-7E goes to DCS_PASSTHROUGH
5789 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5790 scanState
= ScanState
.DCS_PASSTHROUGH
;
5794 case DCS_INTERMEDIATE
:
5796 // 0x9C goes to GROUND
5801 // 0x1B 0x5C goes to GROUND
5806 if ((collectBuffer
.length() > 0)
5807 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5813 // 0x30-3F goes to DCS_IGNORE
5814 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5815 scanState
= ScanState
.DCS_IGNORE
;
5818 // 0x40-7E goes to DCS_PASSTHROUGH
5819 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5820 scanState
= ScanState
.DCS_PASSTHROUGH
;
5823 // 00-17, 19, 1C-1F, 7F --> ignore
5828 // 0x9C goes to GROUND
5833 // 0x1B 0x5C goes to GROUND
5838 if ((collectBuffer
.length() > 0)
5839 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5845 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5846 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5848 scanState
= ScanState
.DCS_INTERMEDIATE
;
5851 // 30-39, 3B --> param
5852 if ((ch
>= '0') && (ch
<= '9')) {
5859 // 00-17, 19, 1C-1F, 7F --> ignore
5861 // 0x3A, 3C-3F goes to DCS_IGNORE
5863 scanState
= ScanState
.DCS_IGNORE
;
5865 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5866 scanState
= ScanState
.DCS_IGNORE
;
5869 // 0x40-7E goes to DCS_PASSTHROUGH
5870 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5871 scanState
= ScanState
.DCS_PASSTHROUGH
;
5875 case DCS_PASSTHROUGH
:
5876 // 0x9C goes to GROUND
5881 // 0x1B 0x5C goes to GROUND
5886 if ((collectBuffer
.length() > 0)
5887 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5893 // 00-17, 19, 1C-1F, 20-7E --> put
5901 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5904 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5913 // 00-17, 19, 1C-1F, 20-7F --> ignore
5915 // 0x9C goes to GROUND
5922 case SOSPMAPC_STRING
:
5923 // 00-17, 19, 1C-1F, 20-7F --> ignore
5925 // 0x9C goes to GROUND
5933 // Special case for Xterm: OSC can pass control characters
5934 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5938 // 00-17, 19, 1C-1F --> ignore
5940 // 20-7F --> osc_put
5941 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5945 // 0x9C goes to GROUND
5952 case VT52_DIRECT_CURSOR_ADDRESS
:
5953 // This is a special case for the VT52 sequence "ESC Y l c"
5954 if (collectBuffer
.length() == 0) {
5956 } else if (collectBuffer
.length() == 1) {
5957 // We've got the two characters, one in the buffer and the
5959 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
5968 * Expose current cursor X to outside world.
5970 * @return current cursor X
5972 public final int getCursorX() {
5973 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
5974 return currentState
.cursorX
* 2;
5976 return currentState
.cursorX
;
5980 * Expose current cursor Y to outside world.
5982 * @return current cursor Y
5984 public final int getCursorY() {
5985 return currentState
.cursorY
;
5989 * Read function runs on a separate thread.
5991 public final void run() {
5992 boolean utf8
= false;
5993 boolean done
= false;
5995 if (type
== DeviceType
.XTERM
) {
5999 // available() will often return > 1, so we need to read in chunks to
6001 char [] readBufferUTF8
= null;
6002 byte [] readBuffer
= null;
6004 readBufferUTF8
= new char[128];
6006 readBuffer
= new byte[128];
6009 while (!done
&& !stopReaderThread
) {
6011 int n
= inputStream
.available();
6013 // System.err.printf("available() %d\n", n); System.err.flush();
6015 if (readBufferUTF8
.length
< n
) {
6016 // The buffer wasn't big enough, make it huger
6017 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
6020 readBufferUTF8
= new char[newSizeHalf
* 2];
6023 if (readBuffer
.length
< n
) {
6024 // The buffer wasn't big enough, make it huger
6025 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
6026 readBuffer
= new byte[newSizeHalf
* 2];
6032 } catch (InterruptedException e
) {
6041 rc
= input
.read(readBufferUTF8
, 0,
6042 readBufferUTF8
.length
);
6044 rc
= inputStream
.read(readBuffer
, 0,
6047 } catch (ReadTimeoutException e
) {
6051 // System.err.printf("read() %d\n", rc); System.err.flush();
6056 for (int i
= 0; i
< rc
; i
++) {
6059 ch
= readBufferUTF8
[i
];
6064 synchronized (this) {
6065 // Don't step on UI events
6070 // System.err.println("end while loop"); System.err.flush();
6071 } catch (IOException e
) {
6072 e
.printStackTrace();
6076 } // while ((done == false) && (stopReaderThread == false))
6078 // Let the rest of the world know that I am done.
6079 stopReaderThread
= true;
6082 inputStream
.cancelRead();
6083 inputStream
.close();
6085 } catch (IOException e
) {
6091 } catch (IOException e
) {
6095 // System.err.println("*** run() exiting..."); System.err.flush();