2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.tterminal
;
31 import java
.io
.BufferedOutputStream
;
32 import java
.io
.InputStream
;
33 import java
.io
.InputStreamReader
;
34 import java
.io
.IOException
;
35 import java
.io
.OutputStream
;
36 import java
.io
.OutputStreamWriter
;
37 import java
.io
.Reader
;
38 import java
.io
.UnsupportedEncodingException
;
39 import java
.io
.Writer
;
40 import java
.util
.ArrayList
;
41 import java
.util
.Collections
;
42 import java
.util
.LinkedList
;
43 import java
.util
.List
;
45 import jexer
.TKeypress
;
46 import jexer
.event
.TMouseEvent
;
47 import jexer
.bits
.Color
;
48 import jexer
.bits
.Cell
;
49 import jexer
.bits
.CellAttributes
;
50 import jexer
.io
.ReadTimeoutException
;
51 import jexer
.io
.TimeoutInputStream
;
52 import static jexer
.TKeypress
.*;
55 * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
56 * including a scrollback buffer.
59 * It currently implements VT100, VT102, VT220, and XTERM with the following
63 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
64 * failure with XTERM. This is due to vttest failing to decode the UTF-8
68 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
69 * from VT100 are not supported.
72 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
73 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
74 * because DECSCL is not supported, it will fail the last part of the
75 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
78 * - Numeric/application keys from the number pad are not supported because
79 * they are not exposed from the TKeypress API.
82 * - VT52 HOLD SCREEN mode is not supported.
85 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
86 * numerators) are not rendered correctly.
89 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
91 public class ECMA48
implements Runnable
{
94 * The emulator can emulate several kinds of terminals.
96 public enum DeviceType
{
98 * DEC VT100 but also including the three VT102 functions.
119 * Return the proper primary Device Attributes string.
121 * @return string to send to remote side that is appropriate for the
124 private String
deviceTypeResponse() {
127 // "I am a VT100 with advanced video option" (often VT102)
136 // "I am a VT220" - 7 bit version
138 return "\033[?62;1;6c";
140 // "I am a VT220" - 8 bit version
141 return "\u009b?62;1;6c";
143 throw new IllegalArgumentException("Invalid device type: " + type
);
148 * Return the proper TERM environment variable for this device type.
150 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
151 * @return "vt100", "xterm", etc.
153 public static String
deviceTypeTerm(final DeviceType deviceType
) {
154 switch (deviceType
) {
168 throw new IllegalArgumentException("Invalid device type: "
174 * Return the proper LANG for this device type. Only XTERM devices know
175 * about UTF-8, the others are defined by their standard to be either
176 * 7-bit or 8-bit characters only.
178 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
179 * @param baseLang a base language without UTF-8 flag such as "C" or
181 * @return "en_US", "en_US.UTF-8", etc.
183 public static String
deviceTypeLang(final DeviceType deviceType
,
184 final String baseLang
) {
186 switch (deviceType
) {
194 return baseLang
+ ".UTF-8";
197 throw new IllegalArgumentException("Invalid device type: "
203 * Write a string directly to the remote side.
205 * @param str string to send
207 public void writeRemote(final String str
) {
208 if (stopReaderThread
) {
209 // Reader hit EOF, bail out now.
214 // System.err.printf("writeRemote() '%s'\n", str);
220 if (outputStream
== null) {
224 outputStream
.flush();
225 for (int i
= 0; i
< str
.length(); i
++) {
226 outputStream
.write(str
.charAt(i
));
228 outputStream
.flush();
229 } catch (IOException e
) {
235 if (output
== null) {
242 } catch (IOException e
) {
248 throw new IllegalArgumentException("Invalid device type: " + type
);
253 * Close the input and output streams and stop the reader thread. Note
254 * that it is safe to call this multiple times.
256 public final void close() {
258 // Tell the reader thread to stop looking at input. It will close
259 // the input streams.
260 if (stopReaderThread
== false) {
261 stopReaderThread
= true;
263 readerThread
.join(1000);
264 } catch (InterruptedException e
) {
269 // Now close the output stream.
274 if (outputStream
!= null) {
276 outputStream
.close();
277 } catch (IOException e
) {
284 if (outputStream
!= null) {
286 outputStream
.close();
287 } catch (IOException e
) {
292 if (output
!= null) {
295 } catch (IOException e
) {
302 throw new IllegalArgumentException("Invalid device type: " +
308 * The enclosing listening object.
310 private DisplayListener displayListener
;
313 * When true, the reader thread is expected to exit.
315 private volatile boolean stopReaderThread
= false;
320 private Thread readerThread
= null;
323 * See if the reader thread is still running.
325 * @return if true, we are still connected to / reading from the remote
328 public final boolean isReading() {
329 return (!stopReaderThread
);
333 * The type of emulator to be.
335 private DeviceType type
= DeviceType
.VT102
;
338 * Obtain a new blank display line for an external user
339 * (e.g. TTerminalWindow).
341 * @return new blank line
343 public final DisplayLine
getBlankDisplayLine() {
344 return new DisplayLine(currentState
.attr
);
348 * The scrollback buffer characters + attributes.
350 private volatile List
<DisplayLine
> scrollback
;
353 * Get the scrollback buffer.
355 * @return the scrollback buffer
357 public final List
<DisplayLine
> getScrollbackBuffer() {
362 * The raw display buffer characters + attributes.
364 private volatile List
<DisplayLine
> display
;
367 * Get the display buffer.
369 * @return the display buffer
371 public final List
<DisplayLine
> getDisplayBuffer() {
376 * The terminal's input. For type == XTERM, this is an InputStreamReader
377 * with UTF-8 encoding.
379 private Reader input
;
382 * The terminal's raw InputStream. This is used for type != XTERM.
384 private volatile TimeoutInputStream inputStream
;
387 * The terminal's output. For type == XTERM, this wraps an
388 * OutputStreamWriter with UTF-8 encoding.
390 private Writer output
;
393 * The terminal's raw OutputStream. This is used for type != XTERM.
395 private OutputStream outputStream
;
398 * Parser character scan states.
415 VT52_DIRECT_CURSOR_ADDRESS
419 * Current scanning state.
421 private ScanState scanState
;
424 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
425 * can't really use it in keypress() because we do not see number pad
426 * events from TKeypress.
428 private enum KeypadMode
{
434 * Arrow keys can emit three different sequences (DECCKM or VT52
437 private enum ArrowKeyMode
{
444 * Available character sets for GL, GR, G0, G1, G2, G3.
446 private enum CharacterSet
{
467 * Single-shift states used by the C1 control characters SS2 (0x8E) and
470 private enum Singleshift
{
477 * VT220+ lockshift states.
479 private enum LockshiftMode
{
489 * XTERM mouse reporting protocols.
491 private enum MouseProtocol
{
500 * Which mouse protocol is active.
502 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
505 * XTERM mouse reporting encodings.
507 private enum MouseEncoding
{
514 * Which mouse encoding is active.
516 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
519 * Physical display width. We start at 80x24, but the user can resize us
525 * Get the display width.
527 * @return the width (usually 80 or 132)
529 public final int getWidth() {
534 * Set the display width.
536 * @param width the new width
538 public final void setWidth(final int width
) {
540 rightMargin
= width
- 1;
541 if (currentState
.cursorX
>= width
) {
542 currentState
.cursorX
= width
- 1;
544 if (savedState
.cursorX
>= width
) {
545 savedState
.cursorX
= width
- 1;
550 * Physical display height. We start at 80x24, but the user can resize
556 * Get the display height.
558 * @return the height (usually 24)
560 public final int getHeight() {
565 * Set the display height.
567 * @param height the new height
569 public final void setHeight(final int height
) {
570 this.height
= height
;
571 if (scrollRegionBottom
>= height
) {
572 scrollRegionBottom
= height
- 1;
574 if (scrollRegionTop
>= scrollRegionBottom
) {
577 if (currentState
.cursorY
>= height
) {
578 currentState
.cursorY
= height
- 1;
580 if (savedState
.cursorY
>= height
) {
581 savedState
.cursorY
= height
- 1;
583 while (display
.size() < height
) {
584 DisplayLine line
= new DisplayLine(currentState
.attr
);
585 line
.setReverseColor(reverseVideo
);
588 while (display
.size() > height
) {
589 scrollback
.add(display
.remove(0));
594 * Top margin of the scrolling region.
596 private int scrollRegionTop
;
599 * Bottom margin of the scrolling region.
601 private int scrollRegionBottom
;
604 * Right margin column number. This can be selected by the remote side
605 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
607 private int rightMargin
;
610 * Last character printed.
615 * VT100-style line wrapping: a character is placed in column 80 (or
616 * 132), but the line does NOT wrap until another character is written to
617 * column 1 of the next line, after which the cursor moves to column 2.
619 private boolean wrapLineFlag
;
622 * VT220 single shift flag.
624 private Singleshift singleshift
= Singleshift
.NONE
;
627 * true = insert characters, false = overwrite.
629 private boolean insertMode
= false;
632 * VT52 mode as selected by DECANM. True means VT52, false means
633 * ANSI. Default is ANSI.
635 private boolean vt52Mode
= false;
638 * Visible cursor (DECTCEM).
640 private boolean cursorVisible
= true;
643 * Get visible cursor flag.
645 * @return if true, the cursor is visible
647 public final boolean isCursorVisible() {
648 return cursorVisible
;
652 * Screen title as set by the xterm OSC sequence. Lots of applications
653 * send a screenTitle regardless of whether it is an xterm client or not.
655 private String screenTitle
= "";
658 * Get the screen title as set by the xterm OSC sequence. Lots of
659 * applications send a screenTitle regardless of whether it is an xterm
662 * @return screen title
664 public final String
getScreenTitle() {
669 * Parameter characters being collected.
671 private List
<Integer
> csiParams
;
674 * Non-csi collect buffer.
676 private StringBuilder collectBuffer
;
679 * When true, use the G1 character set.
681 private boolean shiftOut
= false;
684 * Horizontal tab stop locations.
686 private List
<Integer
> tabStops
;
689 * S8C1T. True means 8bit controls, false means 7bit controls.
691 private boolean s8c1t
= false;
694 * Printer mode. True means send all output to printer, which discards
697 private boolean printerControllerMode
= false;
700 * LMN line mode. If true, linefeed() puts the cursor on the first
701 * column of the next line. If false, linefeed() puts the cursor one
702 * line down on the current line. The default is false.
704 private boolean newLineMode
= false;
707 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
709 private ArrowKeyMode arrowKeyMode
;
712 * Whether number pad keys send VT100 or VT52, application or numeric
715 @SuppressWarnings("unused")
716 private KeypadMode keypadMode
;
719 * When true, the terminal is in 132-column mode (DECCOLM).
721 private boolean columns132
= false;
724 * Get 132 columns value.
726 * @return if true, the terminal is in 132 column mode
728 public final boolean isColumns132() {
733 * true = reverse video. Set by DECSCNM.
735 private boolean reverseVideo
= false;
738 * false = echo characters locally.
740 private boolean fullDuplex
= true;
743 * DECSC/DECRC save/restore a subset of the total state. This class
744 * encapsulates those specific flags/modes.
746 private class SaveableState
{
749 * When true, cursor positions are relative to the scrolling region.
751 public boolean originMode
= false;
754 * The current editing X position.
756 public int cursorX
= 0;
759 * The current editing Y position.
761 public int cursorY
= 0;
764 * Which character set is currently selected in G0.
766 public CharacterSet g0Charset
= CharacterSet
.US
;
769 * Which character set is currently selected in G1.
771 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
774 * Which character set is currently selected in G2.
776 public CharacterSet g2Charset
= CharacterSet
.US
;
779 * Which character set is currently selected in G3.
781 public CharacterSet g3Charset
= CharacterSet
.US
;
784 * Which character set is currently selected in GR.
786 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
789 * The current drawing attributes.
791 public CellAttributes attr
;
796 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
801 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
806 public boolean lineWrap
= true;
811 public void reset() {
815 g0Charset
= CharacterSet
.US
;
816 g1Charset
= CharacterSet
.DRAWING
;
817 g2Charset
= CharacterSet
.US
;
818 g3Charset
= CharacterSet
.US
;
819 grCharset
= CharacterSet
.DRAWING
;
820 attr
= new CellAttributes();
821 glLockshift
= LockshiftMode
.NONE
;
822 grLockshift
= LockshiftMode
.NONE
;
827 * Copy attributes from another instance.
829 * @param that the other instance to match
831 public void setTo(final SaveableState that
) {
832 this.originMode
= that
.originMode
;
833 this.cursorX
= that
.cursorX
;
834 this.cursorY
= that
.cursorY
;
835 this.g0Charset
= that
.g0Charset
;
836 this.g1Charset
= that
.g1Charset
;
837 this.g2Charset
= that
.g2Charset
;
838 this.g3Charset
= that
.g3Charset
;
839 this.grCharset
= that
.grCharset
;
840 this.attr
= new CellAttributes();
841 this.attr
.setTo(that
.attr
);
842 this.glLockshift
= that
.glLockshift
;
843 this.grLockshift
= that
.grLockshift
;
844 this.lineWrap
= that
.lineWrap
;
848 * Public constructor.
850 public SaveableState() {
856 * The current terminal state.
858 private SaveableState currentState
;
861 * The last saved terminal state.
863 private SaveableState savedState
;
866 * Clear the CSI parameters and flags.
868 private void toGround() {
870 collectBuffer
= new StringBuilder(8);
871 scanState
= ScanState
.GROUND
;
875 * Reset the tab stops list.
877 private void resetTabStops() {
879 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
880 tabStops
.add(new Integer(i
* 8));
885 * Reset the emulation state.
887 private void reset() {
889 currentState
= new SaveableState();
890 savedState
= new SaveableState();
891 scanState
= ScanState
.GROUND
;
895 scrollRegionBottom
= height
- 1;
896 rightMargin
= width
- 1;
898 arrowKeyMode
= ArrowKeyMode
.ANSI
;
899 keypadMode
= KeypadMode
.Numeric
;
900 wrapLineFlag
= false;
901 if (displayListener
!= null) {
902 width
= displayListener
.getDisplayWidth();
903 height
= displayListener
.getDisplayHeight();
904 rightMargin
= width
- 1;
913 reverseVideo
= false;
915 cursorVisible
= true;
918 singleshift
= Singleshift
.NONE
;
920 printerControllerMode
= false;
923 mouseProtocol
= MouseProtocol
.OFF
;
924 mouseEncoding
= MouseEncoding
.X10
;
934 * Public constructor.
936 * @param type one of the DeviceType constants to select VT100, VT102,
938 * @param inputStream an InputStream connected to the remote side. For
939 * type == XTERM, inputStream is converted to a Reader with UTF-8
941 * @param outputStream an OutputStream connected to the remote user. For
942 * type == XTERM, outputStream is converted to a Writer with UTF-8
944 * @param displayListener a callback to the outer display, or null for
945 * default VT100 behavior
946 * @throws UnsupportedEncodingException if an exception is thrown when
947 * creating the InputStreamReader
949 public ECMA48(final DeviceType type
, final InputStream inputStream
,
950 final OutputStream outputStream
, final DisplayListener displayListener
)
951 throws UnsupportedEncodingException
{
953 assert (inputStream
!= null);
954 assert (outputStream
!= null);
956 csiParams
= new ArrayList
<Integer
>();
957 tabStops
= new ArrayList
<Integer
>();
958 scrollback
= new LinkedList
<DisplayLine
>();
959 display
= new LinkedList
<DisplayLine
>();
962 if (inputStream
instanceof TimeoutInputStream
) {
963 this.inputStream
= (TimeoutInputStream
)inputStream
;
965 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
967 if (type
== DeviceType
.XTERM
) {
968 this.input
= new InputStreamReader(this.inputStream
, "UTF-8");
969 this.output
= new OutputStreamWriter(new
970 BufferedOutputStream(outputStream
), "UTF-8");
971 this.outputStream
= null;
974 this.outputStream
= new BufferedOutputStream(outputStream
);
976 this.displayListener
= displayListener
;
979 for (int i
= 0; i
< height
; i
++) {
980 display
.add(new DisplayLine(currentState
.attr
));
983 // Spin up the input reader
984 readerThread
= new Thread(this);
985 readerThread
.start();
989 * Append a new line to the bottom of the display, adding lines off the
990 * top to the scrollback buffer.
992 private void newDisplayLine() {
993 // Scroll the top line off into the scrollback buffer
994 scrollback
.add(display
.get(0));
996 DisplayLine line
= new DisplayLine(currentState
.attr
);
997 line
.setReverseColor(reverseVideo
);
1002 * Wraps the current line.
1004 private void wrapCurrentLine() {
1005 if (currentState
.cursorY
== height
- 1) {
1008 if (currentState
.cursorY
< height
- 1) {
1009 currentState
.cursorY
++;
1011 currentState
.cursorX
= 0;
1015 * Handle a carriage return.
1017 private void carriageReturn() {
1018 currentState
.cursorX
= 0;
1019 wrapLineFlag
= false;
1023 * Reverse the color of the visible display.
1025 private void invertDisplayColors() {
1026 for (DisplayLine line
: display
) {
1027 line
.setReverseColor(!line
.isReverseColor());
1032 * Handle a linefeed.
1034 private void linefeed() {
1036 if (currentState
.cursorY
< scrollRegionBottom
) {
1037 // Increment screen y
1038 currentState
.cursorY
++;
1041 // Screen y does not increment
1044 * Two cases: either we're inside a scrolling region or not. If
1045 * the scrolling region bottom is the bottom of the screen, then
1046 * push the top line into the buffer. Else scroll the scrolling
1049 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1051 // We're at the bottom of the scroll region, AND the scroll
1052 // region is the entire screen.
1058 // We're at the bottom of the scroll region, AND the scroll
1059 // region is NOT the entire screen.
1060 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1065 currentState
.cursorX
= 0;
1067 wrapLineFlag
= false;
1071 * Prints one character to the display buffer.
1073 * @param ch character to display
1075 private void printCharacter(final char ch
) {
1076 int rightMargin
= this.rightMargin
;
1078 // Check if we have double-width, and if so chop at 40/66 instead of
1080 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1081 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1084 // Check the unusually-complicated line wrapping conditions...
1085 if (currentState
.cursorX
== rightMargin
) {
1087 if (currentState
.lineWrap
== true) {
1089 * This case happens when: the cursor was already on the
1090 * right margin (either through printing or by an explicit
1091 * placement command), and a character was printed.
1093 * The line wraps only when a new character arrives AND the
1094 * cursor is already on the right margin AND has placed a
1095 * character in its cell. Easier to see than to explain.
1097 if (wrapLineFlag
== false) {
1099 * This block marks the case that we are in the margin
1100 * and the first character has been received and printed.
1102 wrapLineFlag
= true;
1105 * This block marks the case that we are in the margin
1106 * and the second character has been received and
1109 wrapLineFlag
= false;
1113 } else if (currentState
.cursorX
<= rightMargin
) {
1115 * This is the normal case: a character came in and was printed
1116 * to the left of the right margin column.
1119 // Turn off VT100 special-case flag
1120 wrapLineFlag
= false;
1123 // "Print" the character
1124 Cell newCell
= new Cell(ch
);
1125 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1126 newCellAttributes
.setTo(currentState
.attr
);
1127 DisplayLine line
= display
.get(currentState
.cursorY
);
1128 // Insert mode special case
1129 if (insertMode
== true) {
1130 line
.insert(currentState
.cursorX
, newCell
);
1132 // Replace an existing character
1133 line
.replace(currentState
.cursorX
, newCell
);
1136 // Increment horizontal
1137 if (wrapLineFlag
== false) {
1138 currentState
.cursorX
++;
1139 if (currentState
.cursorX
> rightMargin
) {
1140 currentState
.cursorX
--;
1146 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1147 * send to the remote side.
1149 * @param mouse mouse event received from the local user
1151 public void mouse(final TMouseEvent mouse
) {
1154 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1155 mouseProtocol, mouseEncoding, mouse);
1158 if (mouseEncoding
== MouseEncoding
.X10
) {
1159 // We will support X10 but only for (160,94) and smaller.
1160 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1165 switch (mouseProtocol
) {
1172 // Only report button presses
1173 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1179 // Only report button presses and releases
1180 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1181 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1189 * Only report button presses, button releases, and motions that
1190 * have a button down (i.e. drag-and-drop).
1192 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1193 if (!mouse
.isMouse1()
1194 && !mouse
.isMouse2()
1195 && !mouse
.isMouse3()
1196 && !mouse
.isMouseWheelUp()
1197 && !mouse
.isMouseWheelDown()
1205 // Report everything
1209 // Now encode the event
1210 StringBuilder sb
= new StringBuilder(6);
1211 if (mouseEncoding
== MouseEncoding
.SGR
) {
1212 sb
.append((char) 0x1B);
1215 if (mouse
.isMouse1()) {
1216 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1221 } else if (mouse
.isMouse2()) {
1222 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1227 } else if (mouse
.isMouse3()) {
1228 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1233 } else if (mouse
.isMouseWheelUp()) {
1235 } else if (mouse
.isMouseWheelDown()) {
1238 // This is motion with no buttons down.
1242 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1245 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1252 // X10 and UTF8 encodings
1253 sb
.append((char) 0x1B);
1256 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1257 sb
.append((char) (0x03 + 32));
1258 } else if (mouse
.isMouse1()) {
1259 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1260 sb
.append((char) (0x00 + 32 + 32));
1262 sb
.append((char) (0x00 + 32));
1264 } else if (mouse
.isMouse2()) {
1265 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1266 sb
.append((char) (0x01 + 32 + 32));
1268 sb
.append((char) (0x01 + 32));
1270 } else if (mouse
.isMouse3()) {
1271 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1272 sb
.append((char) (0x02 + 32 + 32));
1274 sb
.append((char) (0x02 + 32));
1276 } else if (mouse
.isMouseWheelUp()) {
1277 sb
.append((char) (0x04 + 64));
1278 } else if (mouse
.isMouseWheelDown()) {
1279 sb
.append((char) (0x05 + 64));
1281 // This is motion with no buttons down.
1282 sb
.append((char) (0x03 + 32));
1285 sb
.append((char) (mouse
.getX() + 33));
1286 sb
.append((char) (mouse
.getY() + 33));
1289 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1290 writeRemote(sb
.toString());
1294 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1295 * send to the remote side.
1297 * @param keypress keypress received from the local user
1299 public void keypress(final TKeypress keypress
) {
1300 writeRemote(keypressToString(keypress
));
1304 * Build one of the complex xterm keystroke sequences, storing the result in
1305 * xterm_keystroke_buffer.
1307 * @param ss3 the prefix to use based on VT100 state.
1308 * @param first the first character, usually a number.
1309 * @param first the last character, one of the following: ~ A B C D F H
1310 * @param ctrl whether or not ctrl is down
1311 * @param alt whether or not alt is down
1312 * @param shift whether or not shift is down
1313 * @return the buffer with the full key sequence
1315 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1316 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1318 StringBuilder sb
= new StringBuilder(ss3
);
1319 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1323 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1325 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1327 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1329 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1331 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1333 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1335 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1340 return sb
.toString();
1344 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1346 * @param keypress keypress received from the local user
1347 * @return string to transmit to the remote side
1349 private String
keypressToString(final TKeypress keypress
) {
1351 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1353 * If this is a control character, process it like it came from
1356 if (keypress
.getChar() < 0x20) {
1357 handleControlChar(keypress
.getChar());
1359 // Local echo for everything else
1360 printCharacter(keypress
.getChar());
1364 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1369 // Handle control characters
1370 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1371 StringBuilder sb
= new StringBuilder();
1372 char ch
= keypress
.getChar();
1375 return sb
.toString();
1378 // Handle alt characters
1379 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1380 StringBuilder sb
= new StringBuilder("\033");
1381 char ch
= keypress
.getChar();
1383 return sb
.toString();
1386 if (keypress
.equals(kbBackspace
)) {
1399 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1402 switch (arrowKeyMode
) {
1404 return xtermBuildKeySequence("\033[", '1', 'D',
1405 keypress
.isCtrl(), keypress
.isAlt(),
1406 keypress
.isShift());
1408 return xtermBuildKeySequence("\033", '1', 'D',
1409 keypress
.isCtrl(), keypress
.isAlt(),
1410 keypress
.isShift());
1412 return xtermBuildKeySequence("\033O", '1', 'D',
1413 keypress
.isCtrl(), keypress
.isAlt(),
1414 keypress
.isShift());
1417 switch (arrowKeyMode
) {
1428 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1431 switch (arrowKeyMode
) {
1433 return xtermBuildKeySequence("\033[", '1', 'C',
1434 keypress
.isCtrl(), keypress
.isAlt(),
1435 keypress
.isShift());
1437 return xtermBuildKeySequence("\033", '1', 'C',
1438 keypress
.isCtrl(), keypress
.isAlt(),
1439 keypress
.isShift());
1441 return xtermBuildKeySequence("\033O", '1', 'C',
1442 keypress
.isCtrl(), keypress
.isAlt(),
1443 keypress
.isShift());
1446 switch (arrowKeyMode
) {
1457 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1460 switch (arrowKeyMode
) {
1462 return xtermBuildKeySequence("\033[", '1', 'A',
1463 keypress
.isCtrl(), keypress
.isAlt(),
1464 keypress
.isShift());
1466 return xtermBuildKeySequence("\033", '1', 'A',
1467 keypress
.isCtrl(), keypress
.isAlt(),
1468 keypress
.isShift());
1470 return xtermBuildKeySequence("\033O", '1', 'A',
1471 keypress
.isCtrl(), keypress
.isAlt(),
1472 keypress
.isShift());
1475 switch (arrowKeyMode
) {
1486 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1489 switch (arrowKeyMode
) {
1491 return xtermBuildKeySequence("\033[", '1', 'B',
1492 keypress
.isCtrl(), keypress
.isAlt(),
1493 keypress
.isShift());
1495 return xtermBuildKeySequence("\033", '1', 'B',
1496 keypress
.isCtrl(), keypress
.isAlt(),
1497 keypress
.isShift());
1499 return xtermBuildKeySequence("\033O", '1', 'B',
1500 keypress
.isCtrl(), keypress
.isAlt(),
1501 keypress
.isShift());
1504 switch (arrowKeyMode
) {
1515 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1518 switch (arrowKeyMode
) {
1520 return xtermBuildKeySequence("\033[", '1', 'H',
1521 keypress
.isCtrl(), keypress
.isAlt(),
1522 keypress
.isShift());
1524 return xtermBuildKeySequence("\033", '1', 'H',
1525 keypress
.isCtrl(), keypress
.isAlt(),
1526 keypress
.isShift());
1528 return xtermBuildKeySequence("\033O", '1', 'H',
1529 keypress
.isCtrl(), keypress
.isAlt(),
1530 keypress
.isShift());
1533 switch (arrowKeyMode
) {
1544 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1547 switch (arrowKeyMode
) {
1549 return xtermBuildKeySequence("\033[", '1', 'F',
1550 keypress
.isCtrl(), keypress
.isAlt(),
1551 keypress
.isShift());
1553 return xtermBuildKeySequence("\033", '1', 'F',
1554 keypress
.isCtrl(), keypress
.isAlt(),
1555 keypress
.isShift());
1557 return xtermBuildKeySequence("\033O", '1', 'F',
1558 keypress
.isCtrl(), keypress
.isAlt(),
1559 keypress
.isShift());
1562 switch (arrowKeyMode
) {
1573 if (keypress
.equals(kbF1
)) {
1581 if (keypress
.equals(kbF2
)) {
1589 if (keypress
.equals(kbF3
)) {
1597 if (keypress
.equals(kbF4
)) {
1605 if (keypress
.equals(kbF5
)) {
1618 if (keypress
.equals(kbF6
)) {
1631 if (keypress
.equals(kbF7
)) {
1644 if (keypress
.equals(kbF8
)) {
1657 if (keypress
.equals(kbF9
)) {
1670 if (keypress
.equals(kbF10
)) {
1683 if (keypress
.equals(kbF11
)) {
1687 if (keypress
.equals(kbF12
)) {
1691 if (keypress
.equals(kbShiftF1
)) {
1696 if (type
== DeviceType
.XTERM
) {
1702 if (keypress
.equals(kbShiftF2
)) {
1707 if (type
== DeviceType
.XTERM
) {
1713 if (keypress
.equals(kbShiftF3
)) {
1718 if (type
== DeviceType
.XTERM
) {
1724 if (keypress
.equals(kbShiftF4
)) {
1729 if (type
== DeviceType
.XTERM
) {
1735 if (keypress
.equals(kbShiftF5
)) {
1737 return "\033[15;2~";
1740 if (keypress
.equals(kbShiftF6
)) {
1742 return "\033[17;2~";
1745 if (keypress
.equals(kbShiftF7
)) {
1747 return "\033[18;2~";
1750 if (keypress
.equals(kbShiftF8
)) {
1752 return "\033[19;2~";
1755 if (keypress
.equals(kbShiftF9
)) {
1757 return "\033[20;2~";
1760 if (keypress
.equals(kbShiftF10
)) {
1762 return "\033[21;2~";
1765 if (keypress
.equals(kbShiftF11
)) {
1767 return "\033[23;2~";
1770 if (keypress
.equals(kbShiftF12
)) {
1772 return "\033[24;2~";
1775 if (keypress
.equals(kbCtrlF1
)) {
1780 if (type
== DeviceType
.XTERM
) {
1786 if (keypress
.equals(kbCtrlF2
)) {
1791 if (type
== DeviceType
.XTERM
) {
1797 if (keypress
.equals(kbCtrlF3
)) {
1802 if (type
== DeviceType
.XTERM
) {
1808 if (keypress
.equals(kbCtrlF4
)) {
1813 if (type
== DeviceType
.XTERM
) {
1819 if (keypress
.equals(kbCtrlF5
)) {
1821 return "\033[15;5~";
1824 if (keypress
.equals(kbCtrlF6
)) {
1826 return "\033[17;5~";
1829 if (keypress
.equals(kbCtrlF7
)) {
1831 return "\033[18;5~";
1834 if (keypress
.equals(kbCtrlF8
)) {
1836 return "\033[19;5~";
1839 if (keypress
.equals(kbCtrlF9
)) {
1841 return "\033[20;5~";
1844 if (keypress
.equals(kbCtrlF10
)) {
1846 return "\033[21;5~";
1849 if (keypress
.equals(kbCtrlF11
)) {
1851 return "\033[23;5~";
1854 if (keypress
.equals(kbCtrlF12
)) {
1856 return "\033[24;5~";
1859 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
1862 return xtermBuildKeySequence("\033[", '5', '~',
1863 keypress
.isCtrl(), keypress
.isAlt(),
1864 keypress
.isShift());
1870 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
1873 return xtermBuildKeySequence("\033[", '6', '~',
1874 keypress
.isCtrl(), keypress
.isAlt(),
1875 keypress
.isShift());
1881 if (keypress
.equalsWithoutModifiers(kbIns
)) {
1884 return xtermBuildKeySequence("\033[", '2', '~',
1885 keypress
.isCtrl(), keypress
.isAlt(),
1886 keypress
.isShift());
1892 if (keypress
.equalsWithoutModifiers(kbDel
)) {
1895 return xtermBuildKeySequence("\033[", '3', '~',
1896 keypress
.isCtrl(), keypress
.isAlt(),
1897 keypress
.isShift());
1899 // Delete sends real delete for VTxxx
1904 if (keypress
.equals(kbEnter
)) {
1908 if (keypress
.equals(kbEsc
)) {
1912 if (keypress
.equals(kbAltEsc
)) {
1916 if (keypress
.equals(kbTab
)) {
1920 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
1921 (keypress
.equals(kbShiftTab
))
1931 // Non-alt, non-ctrl characters
1932 if (!keypress
.isFnKey()) {
1933 StringBuilder sb
= new StringBuilder();
1934 sb
.append(keypress
.getChar());
1935 return sb
.toString();
1941 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1944 * @param ch 8-bit character from the remote side
1945 * @param charsetGl character set defined for GL
1946 * @param charsetGr character set defined for GR
1947 * @return character to display on the screen
1949 private char mapCharacterCharset(final char ch
,
1950 final CharacterSet charsetGl
,
1951 final CharacterSet charsetGr
) {
1953 int lookupChar
= ch
;
1954 CharacterSet lookupCharset
= charsetGl
;
1957 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
1958 lookupCharset
= charsetGr
;
1962 switch (lookupCharset
) {
1965 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
1968 return DECCharacterSets
.UK
[lookupChar
];
1971 return DECCharacterSets
.US_ASCII
[lookupChar
];
1974 return DECCharacterSets
.NL
[lookupChar
];
1977 return DECCharacterSets
.FI
[lookupChar
];
1980 return DECCharacterSets
.FR
[lookupChar
];
1983 return DECCharacterSets
.FR_CA
[lookupChar
];
1986 return DECCharacterSets
.DE
[lookupChar
];
1989 return DECCharacterSets
.IT
[lookupChar
];
1992 return DECCharacterSets
.NO
[lookupChar
];
1995 return DECCharacterSets
.ES
[lookupChar
];
1998 return DECCharacterSets
.SV
[lookupChar
];
2001 return DECCharacterSets
.SWISS
[lookupChar
];
2003 case DEC_SUPPLEMENTAL
:
2004 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2007 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2010 return DECCharacterSets
.US_ASCII
[lookupChar
];
2013 return DECCharacterSets
.US_ASCII
[lookupChar
];
2016 throw new IllegalArgumentException("Invalid character set value: "
2022 * Map an 8-bit byte into a printable character.
2024 * @param ch either 8-bit or Unicode character from the remote side
2025 * @return character to display on the screen
2027 private char mapCharacter(final char ch
) {
2029 // Unicode character, just return it
2033 CharacterSet charsetGl
= currentState
.g0Charset
;
2034 CharacterSet charsetGr
= currentState
.grCharset
;
2036 if (vt52Mode
== true) {
2037 if (shiftOut
== true) {
2038 // Shifted out character, pull from VT52 graphics
2039 charsetGl
= currentState
.g1Charset
;
2040 charsetGr
= CharacterSet
.US
;
2043 charsetGl
= currentState
.g0Charset
;
2044 charsetGr
= CharacterSet
.US
;
2047 // Pull the character
2048 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2052 if (shiftOut
== true) {
2053 // Shifted out character, pull from G1
2054 charsetGl
= currentState
.g1Charset
;
2055 charsetGr
= currentState
.grCharset
;
2057 // Pull the character
2058 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2062 if (singleshift
== Singleshift
.SS2
) {
2064 singleshift
= Singleshift
.NONE
;
2066 // Shifted out character, pull from G2
2067 charsetGl
= currentState
.g2Charset
;
2068 charsetGr
= currentState
.grCharset
;
2072 if (singleshift
== Singleshift
.SS3
) {
2074 singleshift
= Singleshift
.NONE
;
2076 // Shifted out character, pull from G3
2077 charsetGl
= currentState
.g3Charset
;
2078 charsetGr
= currentState
.grCharset
;
2081 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2082 // Check for locking shift
2084 switch (currentState
.glLockshift
) {
2097 charsetGl
= currentState
.g2Charset
;
2102 charsetGl
= currentState
.g3Charset
;
2107 charsetGl
= currentState
.g0Charset
;
2111 switch (currentState
.grLockshift
) {
2121 charsetGr
= currentState
.g1Charset
;
2126 charsetGr
= currentState
.g2Charset
;
2131 charsetGr
= currentState
.g3Charset
;
2136 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2143 // Pull the character
2144 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2148 * Scroll the text within a scrolling region up n lines.
2150 * @param regionTop top row of the scrolling region
2151 * @param regionBottom bottom row of the scrolling region
2152 * @param n number of lines to scroll
2154 private void scrollingRegionScrollUp(final int regionTop
,
2155 final int regionBottom
, final int n
) {
2157 if (regionTop
>= regionBottom
) {
2161 // Sanity check: see if there will be any characters left after the
2163 if (regionBottom
+ 1 - regionTop
<= n
) {
2164 // There won't be anything left in the region, so just call
2165 // eraseScreen() and return.
2166 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2170 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2171 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2172 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2174 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2175 - remaining
, regionBottom
+ 1);
2176 display
= new LinkedList
<DisplayLine
>(displayTop
);
2177 display
.addAll(displayMiddle
);
2178 for (int i
= 0; i
< n
; i
++) {
2179 DisplayLine line
= new DisplayLine(currentState
.attr
);
2180 line
.setReverseColor(reverseVideo
);
2183 display
.addAll(displayBottom
);
2185 assert (display
.size() == height
);
2189 * Scroll the text within a scrolling region down n lines.
2191 * @param regionTop top row of the scrolling region
2192 * @param regionBottom bottom row of the scrolling region
2193 * @param n number of lines to scroll
2195 private void scrollingRegionScrollDown(final int regionTop
,
2196 final int regionBottom
, final int n
) {
2198 if (regionTop
>= regionBottom
) {
2202 // Sanity check: see if there will be any characters left after the
2204 if (regionBottom
+ 1 - regionTop
<= n
) {
2205 // There won't be anything left in the region, so just call
2206 // eraseScreen() and return.
2207 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2211 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2212 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2213 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2215 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2216 regionTop
+ remaining
);
2217 display
= new LinkedList
<DisplayLine
>(displayTop
);
2218 for (int i
= 0; i
< n
; i
++) {
2219 DisplayLine line
= new DisplayLine(currentState
.attr
);
2220 line
.setReverseColor(reverseVideo
);
2223 display
.addAll(displayMiddle
);
2224 display
.addAll(displayBottom
);
2226 assert (display
.size() == height
);
2230 * Process a control character.
2232 * @param ch 8-bit character from the remote side
2234 private void handleControlChar(final char ch
) {
2235 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2246 // Transmit the answerback message.
2257 cursorLeft(1, false);
2262 advanceToNextTabStop();
2288 currentState
.glLockshift
= LockshiftMode
.NONE
;
2294 currentState
.glLockshift
= LockshiftMode
.NONE
;
2319 singleshift
= Singleshift
.SS2
;
2324 singleshift
= Singleshift
.SS3
;
2334 * Advance the cursor to the next tab stop.
2336 private void advanceToNextTabStop() {
2337 if (tabStops
.size() == 0) {
2338 // Go to the rightmost column
2339 cursorRight(rightMargin
- currentState
.cursorX
, false);
2342 for (Integer stop
: tabStops
) {
2343 if (stop
> currentState
.cursorX
) {
2344 cursorRight(stop
- currentState
.cursorX
, false);
2349 * We got here, meaning there isn't a tab stop beyond the current
2350 * cursor position. Place the cursor of the right-most edge of the
2353 cursorRight(rightMargin
- currentState
.cursorX
, false);
2357 * Save a character into the collect buffer.
2359 * @param ch character to save
2361 private void collect(final char ch
) {
2362 collectBuffer
.append(ch
);
2366 * Save a byte into the CSI parameters buffer.
2368 * @param ch byte to save
2370 private void param(final byte ch
) {
2371 if (csiParams
.size() == 0) {
2372 csiParams
.add(new Integer(0));
2374 Integer x
= csiParams
.get(csiParams
.size() - 1);
2375 if ((ch
>= '0') && (ch
<= '9')) {
2378 csiParams
.set(csiParams
.size() - 1, x
);
2382 csiParams
.add(new Integer(0));
2387 * Get a CSI parameter value, with a default.
2389 * @param position parameter index. 0 is the first parameter.
2390 * @param defaultValue value to use if csiParams[position] doesn't exist
2391 * @return parameter value
2393 private int getCsiParam(final int position
, final int defaultValue
) {
2394 if (csiParams
.size() < position
+ 1) {
2395 return defaultValue
;
2397 return csiParams
.get(position
).intValue();
2401 * Get a CSI parameter value, clamped to within min/max.
2403 * @param position parameter index. 0 is the first parameter.
2404 * @param defaultValue value to use if csiParams[position] doesn't exist
2405 * @param minValue minimum value inclusive
2406 * @param maxValue maximum value inclusive
2407 * @return parameter value
2409 private int getCsiParam(final int position
, final int defaultValue
,
2410 final int minValue
, final int maxValue
) {
2412 assert (minValue
<= maxValue
);
2413 int value
= getCsiParam(position
, defaultValue
);
2414 if (value
< minValue
) {
2417 if (value
> maxValue
) {
2424 * Set or unset a toggle.
2426 * @param value true for set ('h'), false for reset ('l')
2428 private void setToggle(final boolean value
) {
2429 boolean decPrivateModeFlag
= false;
2430 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2431 if (collectBuffer
.charAt(i
) == '?') {
2432 decPrivateModeFlag
= true;
2437 for (Integer i
: csiParams
) {
2442 if (decPrivateModeFlag
== true) {
2444 if (value
== true) {
2445 // Use application arrow keys
2446 arrowKeyMode
= ArrowKeyMode
.VT100
;
2448 // Use ANSI arrow keys
2449 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2454 if (decPrivateModeFlag
== true) {
2455 if (value
== false) {
2459 arrowKeyMode
= ArrowKeyMode
.VT52
;
2462 * From the VT102 docs: "You use ANSI mode to select
2463 * most terminal features; the terminal uses the same
2464 * features when it switches to VT52 mode. You
2465 * cannot, however, change most of these features in
2468 * In other words, do not reset any other attributes
2469 * when switching between VT52 submode and ANSI.
2471 * HOWEVER, the real vt100 does switch the character
2472 * set according to Usenet.
2474 currentState
.g0Charset
= CharacterSet
.US
;
2475 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2478 if ((type
== DeviceType
.VT220
)
2479 || (type
== DeviceType
.XTERM
)) {
2481 // VT52 mode is explicitly 7-bit
2483 singleshift
= Singleshift
.NONE
;
2488 if (value
== true) {
2489 // Turn off keyboard
2498 if (decPrivateModeFlag
== true) {
2500 if (value
== true) {
2507 if ((displayListener
!= null)
2508 && (type
== DeviceType
.XTERM
)
2510 // For xterms, reset to the actual width, not 80
2512 width
= displayListener
.getDisplayWidth();
2513 rightMargin
= width
- 1;
2516 width
= rightMargin
+ 1;
2519 // Entire screen is cleared, and scrolling region is
2521 eraseScreen(0, 0, height
- 1, width
- 1, false);
2522 scrollRegionTop
= 0;
2523 scrollRegionBottom
= height
- 1;
2524 // Also home the cursor
2525 cursorPosition(0, 0);
2529 if (decPrivateModeFlag
== true) {
2531 if (value
== true) {
2540 if (value
== true) {
2548 if (decPrivateModeFlag
== true) {
2550 if (value
== true) {
2552 * Set selects reverse screen, a white screen
2553 * background with black characters.
2555 if (reverseVideo
!= true) {
2557 * If in normal video, switch it back
2559 invertDisplayColors();
2561 reverseVideo
= true;
2564 * Reset selects normal screen, a black screen
2565 * background with white characters.
2567 if (reverseVideo
== true) {
2569 * If in reverse video already, switch it back
2571 invertDisplayColors();
2573 reverseVideo
= false;
2578 if (decPrivateModeFlag
== true) {
2580 if (value
== true) {
2581 // Origin is relative to scroll region cursor.
2582 // Cursor can NEVER leave scrolling region.
2583 currentState
.originMode
= true;
2584 cursorPosition(0, 0);
2586 // Origin is absolute to entire screen. Cursor can
2587 // leave the scrolling region via cup() and hvp().
2588 currentState
.originMode
= false;
2589 cursorPosition(0, 0);
2594 if (decPrivateModeFlag
== true) {
2596 if (value
== true) {
2598 currentState
.lineWrap
= true;
2600 // Turn linewrap off
2601 currentState
.lineWrap
= false;
2606 if (decPrivateModeFlag
== true) {
2608 if (value
== true) {
2609 // Keyboard auto-repeat on
2612 // Keyboard auto-repeat off
2618 if (decPrivateModeFlag
== false) {
2620 if (value
== true) {
2630 if (decPrivateModeFlag
== true) {
2636 if (decPrivateModeFlag
== true) {
2642 if (decPrivateModeFlag
== false) {
2644 if (value
== true) {
2646 * Set causes a received linefeed, form feed, or
2647 * vertical tab to move cursor to first column of
2648 * next line. RETURN transmits both a carriage return
2649 * and linefeed. This selection is also called new
2655 * Reset causes a received linefeed, form feed, or
2656 * vertical tab to move cursor to next line in
2657 * current column. RETURN transmits a carriage
2660 newLineMode
= false;
2666 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2667 if (decPrivateModeFlag
== true) {
2669 if (value
== true) {
2671 cursorVisible
= true;
2674 cursorVisible
= false;
2681 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2682 if (decPrivateModeFlag
== true) {
2684 if (value
== true) {
2685 // Select national mode NRC
2688 // Select multi-national mode
2697 if ((type
== DeviceType
.XTERM
)
2698 && (decPrivateModeFlag
== true)
2700 // Mouse: normal tracking mode
2701 if (value
== true) {
2702 mouseProtocol
= MouseProtocol
.NORMAL
;
2704 mouseProtocol
= MouseProtocol
.OFF
;
2710 if ((type
== DeviceType
.XTERM
)
2711 && (decPrivateModeFlag
== true)
2713 // Mouse: normal tracking mode
2714 if (value
== true) {
2715 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2717 mouseProtocol
= MouseProtocol
.OFF
;
2723 if ((type
== DeviceType
.XTERM
)
2724 && (decPrivateModeFlag
== true)
2726 // Mouse: Any-event tracking mode
2727 if (value
== true) {
2728 mouseProtocol
= MouseProtocol
.ANYEVENT
;
2730 mouseProtocol
= MouseProtocol
.OFF
;
2736 if ((type
== DeviceType
.XTERM
)
2737 && (decPrivateModeFlag
== true)
2739 // Mouse: UTF-8 coordinates
2740 if (value
== true) {
2741 mouseEncoding
= MouseEncoding
.UTF8
;
2743 mouseEncoding
= MouseEncoding
.X10
;
2749 if ((type
== DeviceType
.XTERM
)
2750 && (decPrivateModeFlag
== true)
2752 // Mouse: SGR coordinates
2753 if (value
== true) {
2754 mouseEncoding
= MouseEncoding
.SGR
;
2756 mouseEncoding
= MouseEncoding
.X10
;
2769 * DECSC - Save cursor.
2771 private void decsc() {
2772 savedState
.setTo(currentState
);
2776 * DECRC - Restore cursor.
2778 private void decrc() {
2779 currentState
.setTo(savedState
);
2785 private void ind() {
2786 // Move the cursor and scroll if necessary. If at the bottom line
2787 // already, a scroll up is supposed to be performed.
2788 if (currentState
.cursorY
== scrollRegionBottom
) {
2789 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2791 cursorDown(1, true);
2795 * RI - Reverse index.
2798 // Move the cursor and scroll if necessary. If at the top line
2799 // already, a scroll down is supposed to be performed.
2800 if (currentState
.cursorY
== scrollRegionTop
) {
2801 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2809 private void nel() {
2810 // Move the cursor and scroll if necessary. If at the bottom line
2811 // already, a scroll up is supposed to be performed.
2812 if (currentState
.cursorY
== scrollRegionBottom
) {
2813 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2815 cursorDown(1, true);
2817 // Reset to the beginning of the next line
2818 currentState
.cursorX
= 0;
2822 * DECKPAM - Keypad application mode.
2824 private void deckpam() {
2825 keypadMode
= KeypadMode
.Application
;
2829 * DECKPNM - Keypad numeric mode.
2831 private void deckpnm() {
2832 keypadMode
= KeypadMode
.Numeric
;
2838 * @param n number of spaces to move
2839 * @param honorScrollRegion if true, then do nothing if the cursor is
2840 * outside the scrolling region
2842 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2846 * Special case: if a user moves the cursor from the right margin, we
2847 * have to reset the VT100 right margin flag.
2850 wrapLineFlag
= false;
2853 for (int i
= 0; i
< n
; i
++) {
2854 if (honorScrollRegion
== true) {
2855 // Honor the scrolling region
2856 if ((currentState
.cursorY
< scrollRegionTop
)
2857 || (currentState
.cursorY
> scrollRegionBottom
)
2859 // Outside region, do nothing
2862 // Inside region, go up
2863 top
= scrollRegionTop
;
2865 // Non-scrolling case
2869 if (currentState
.cursorY
> top
) {
2870 currentState
.cursorY
--;
2876 * Move down n spaces.
2878 * @param n number of spaces to move
2879 * @param honorScrollRegion if true, then do nothing if the cursor is
2880 * outside the scrolling region
2882 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
2886 * Special case: if a user moves the cursor from the right margin, we
2887 * have to reset the VT100 right margin flag.
2890 wrapLineFlag
= false;
2893 for (int i
= 0; i
< n
; i
++) {
2895 if (honorScrollRegion
== true) {
2896 // Honor the scrolling region
2897 if (currentState
.cursorY
> scrollRegionBottom
) {
2898 // Outside region, do nothing
2901 // Inside region, go down
2902 bottom
= scrollRegionBottom
;
2904 // Non-scrolling case
2905 bottom
= height
- 1;
2908 if (currentState
.cursorY
< bottom
) {
2909 currentState
.cursorY
++;
2915 * Move left n spaces.
2917 * @param n number of spaces to move
2918 * @param honorScrollRegion if true, then do nothing if the cursor is
2919 * outside the scrolling region
2921 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
2923 * Special case: if a user moves the cursor from the right margin, we
2924 * have to reset the VT100 right margin flag.
2927 wrapLineFlag
= false;
2930 for (int i
= 0; i
< n
; i
++) {
2931 if (honorScrollRegion
== true) {
2932 // Honor the scrolling region
2933 if ((currentState
.cursorY
< scrollRegionTop
)
2934 || (currentState
.cursorY
> scrollRegionBottom
)
2936 // Outside region, do nothing
2941 if (currentState
.cursorX
> 0) {
2942 currentState
.cursorX
--;
2948 * Move right n spaces.
2950 * @param n number of spaces to move
2951 * @param honorScrollRegion if true, then do nothing if the cursor is
2952 * outside the scrolling region
2954 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
2955 int rightMargin
= this.rightMargin
;
2958 * Special case: if a user moves the cursor from the right margin, we
2959 * have to reset the VT100 right margin flag.
2962 wrapLineFlag
= false;
2965 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2966 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2969 for (int i
= 0; i
< n
; i
++) {
2970 if (honorScrollRegion
== true) {
2971 // Honor the scrolling region
2972 if ((currentState
.cursorY
< scrollRegionTop
)
2973 || (currentState
.cursorY
> scrollRegionBottom
)
2975 // Outside region, do nothing
2980 if (currentState
.cursorX
< rightMargin
) {
2981 currentState
.cursorX
++;
2987 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2989 * @param row row to move to
2990 * @param col column to move to
2992 private void cursorPosition(int row
, final int col
) {
2993 int rightMargin
= this.rightMargin
;
2998 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2999 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3002 // Set column number
3003 currentState
.cursorX
= col
;
3005 // Sanity check, bring column back to margin.
3006 if (currentState
.cursorX
> rightMargin
) {
3007 currentState
.cursorX
= rightMargin
;
3011 if (currentState
.originMode
== true) {
3012 row
+= scrollRegionTop
;
3014 if (currentState
.cursorY
< row
) {
3015 cursorDown(row
- currentState
.cursorY
, false);
3016 } else if (currentState
.cursorY
> row
) {
3017 cursorUp(currentState
.cursorY
- row
, false);
3020 wrapLineFlag
= false;
3024 * HTS - Horizontal tabulation set.
3026 private void hts() {
3027 for (Integer stop
: tabStops
) {
3028 if (stop
== currentState
.cursorX
) {
3029 // Already have a tab stop here
3034 // Append a tab stop to the end of the array and resort them
3035 tabStops
.add(currentState
.cursorX
);
3036 Collections
.sort(tabStops
);
3040 * DECSWL - Single-width line.
3042 private void decswl() {
3043 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3044 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3048 * DECDWL - Double-width line.
3050 private void decdwl() {
3051 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3052 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3056 * DECHDL - Double-height + double-width line.
3058 * @param topHalf if true, this sets the row to be the top half row of a
3061 private void dechdl(final boolean topHalf
) {
3062 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3063 if (topHalf
== true) {
3064 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3066 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3071 * DECALN - Screen alignment display.
3073 private void decaln() {
3074 Cell newCell
= new Cell();
3075 newCell
.setChar('E');
3076 for (DisplayLine line
: display
) {
3077 for (int i
= 0; i
< line
.length(); i
++) {
3078 line
.replace(i
, newCell
);
3084 * DECSCL - Compatibility level.
3086 private void decscl() {
3087 int i
= getCsiParam(0, 0);
3088 int j
= getCsiParam(1, 0);
3092 currentState
.g0Charset
= CharacterSet
.US
;
3093 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3095 } else if (i
== 62) {
3097 if ((j
== 0) || (j
== 2)) {
3099 } else if (j
== 1) {
3106 * CUD - Cursor down.
3108 private void cud() {
3109 cursorDown(getCsiParam(0, 1, 1, height
), true);
3113 * CUF - Cursor forward.
3115 private void cuf() {
3116 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3120 * CUB - Cursor backward.
3122 private void cub() {
3123 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3129 private void cuu() {
3130 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3134 * CUP - Cursor position.
3136 private void cup() {
3137 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3138 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3142 * CNL - Cursor down and to column 1.
3144 private void cnl() {
3145 cursorDown(getCsiParam(0, 1, 1, height
), true);
3147 cursorLeft(currentState
.cursorX
, true);
3151 * CPL - Cursor up and to column 1.
3153 private void cpl() {
3154 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3156 cursorLeft(currentState
.cursorX
, true);
3160 * CHA - Cursor to column # in current row.
3162 private void cha() {
3163 cursorPosition(currentState
.cursorY
,
3164 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3168 * VPA - Cursor to row #, same column.
3170 private void vpa() {
3171 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3172 currentState
.cursorX
);
3176 * ED - Erase in display.
3179 boolean honorProtected
= false;
3180 boolean decPrivateModeFlag
= false;
3182 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3183 if (collectBuffer
.charAt(i
) == '?') {
3184 decPrivateModeFlag
= true;
3189 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3190 && (decPrivateModeFlag
== true)
3192 honorProtected
= true;
3195 int i
= getCsiParam(0, 0);
3198 // Erase from here to end of screen
3199 if (currentState
.cursorY
< height
- 1) {
3200 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3203 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3204 } else if (i
== 1) {
3205 // Erase from beginning of screen to here
3206 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3208 eraseLine(0, currentState
.cursorX
, honorProtected
);
3209 } else if (i
== 2) {
3210 // Erase entire screen
3211 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3216 * EL - Erase in line.
3219 boolean honorProtected
= false;
3220 boolean decPrivateModeFlag
= false;
3222 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3223 if (collectBuffer
.charAt(i
) == '?') {
3224 decPrivateModeFlag
= true;
3229 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3230 && (decPrivateModeFlag
== true)
3232 honorProtected
= true;
3235 int i
= getCsiParam(0, 0);
3238 // Erase from here to end of line
3239 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3240 } else if (i
== 1) {
3241 // Erase from beginning of line to here
3242 eraseLine(0, currentState
.cursorX
, honorProtected
);
3243 } else if (i
== 2) {
3244 // Erase entire line
3245 eraseLine(0, width
- 1, honorProtected
);
3250 * ECH - Erase # of characters in current row.
3252 private void ech() {
3253 int i
= getCsiParam(0, 1, 1, width
);
3255 // Erase from here to i characters
3256 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3263 int i
= getCsiParam(0, 1);
3265 if ((currentState
.cursorY
>= scrollRegionTop
)
3266 && (currentState
.cursorY
<= scrollRegionBottom
)
3269 // I can get the same effect with a scroll-down
3270 scrollingRegionScrollDown(currentState
.cursorY
,
3271 scrollRegionBottom
, i
);
3276 * DCH - Delete char.
3278 private void dch() {
3279 int n
= getCsiParam(0, 1);
3280 DisplayLine line
= display
.get(currentState
.cursorY
);
3281 Cell blank
= new Cell();
3282 for (int i
= 0; i
< n
; i
++) {
3283 line
.delete(currentState
.cursorX
, blank
);
3288 * ICH - Insert blank char at cursor.
3290 private void ich() {
3291 int n
= getCsiParam(0, 1);
3292 DisplayLine line
= display
.get(currentState
.cursorY
);
3293 Cell blank
= new Cell();
3294 for (int i
= 0; i
< n
; i
++) {
3295 line
.insert(currentState
.cursorX
, blank
);
3303 int i
= getCsiParam(0, 1);
3305 if ((currentState
.cursorY
>= scrollRegionTop
)
3306 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3308 // I can get the same effect with a scroll-down
3309 scrollingRegionScrollUp(currentState
.cursorY
,
3310 scrollRegionBottom
, i
);
3315 * HVP - Horizontal and vertical position.
3317 private void hvp() {
3322 * REP - Repeat character.
3324 private void rep() {
3325 int n
= getCsiParam(0, 1);
3326 for (int i
= 0; i
< n
; i
++) {
3327 printCharacter(repCh
);
3335 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3336 getCsiParam(0, 1, 1, height
));
3343 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3344 getCsiParam(0, 1, 1, height
));
3348 * CBT - Go back X tab stops.
3350 private void cbt() {
3351 int tabsToMove
= getCsiParam(0, 1);
3354 for (int i
= 0; i
< tabsToMove
; i
++) {
3355 int j
= currentState
.cursorX
;
3356 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3357 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3365 j
= tabStops
.get(tabI
);
3367 cursorPosition(currentState
.cursorY
, j
);
3372 * CHT - Advance X tab stops.
3374 private void cht() {
3375 int n
= getCsiParam(0, 1);
3376 for (int i
= 0; i
< n
; i
++) {
3377 advanceToNextTabStop();
3382 * SGR - Select graphics rendition.
3384 private void sgr() {
3386 if (csiParams
.size() == 0) {
3387 currentState
.attr
.reset();
3391 for (Integer i
: csiParams
) {
3397 currentState
.attr
.reset();
3402 currentState
.attr
.setBold(true);
3407 currentState
.attr
.setUnderline(true);
3412 currentState
.attr
.setBlink(true);
3417 currentState
.attr
.setReverse(true);
3424 if (type
== DeviceType
.XTERM
) {
3438 if ((type
== DeviceType
.VT220
)
3439 || (type
== DeviceType
.XTERM
)) {
3445 currentState
.attr
.setBold(false);
3450 currentState
.attr
.setUnderline(false);
3455 currentState
.attr
.setBlink(false);
3460 currentState
.attr
.setReverse(false);
3468 // A true VT100/102/220 does not support color, however everyone
3469 // is used to their terminal emulator supporting color so we will
3470 // unconditionally support color for all DeviceType's.
3475 // Set black foreground
3476 currentState
.attr
.setForeColor(Color
.BLACK
);
3479 // Set red foreground
3480 currentState
.attr
.setForeColor(Color
.RED
);
3483 // Set green foreground
3484 currentState
.attr
.setForeColor(Color
.GREEN
);
3487 // Set yellow foreground
3488 currentState
.attr
.setForeColor(Color
.YELLOW
);
3491 // Set blue foreground
3492 currentState
.attr
.setForeColor(Color
.BLUE
);
3495 // Set magenta foreground
3496 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3499 // Set cyan foreground
3500 currentState
.attr
.setForeColor(Color
.CYAN
);
3503 // Set white foreground
3504 currentState
.attr
.setForeColor(Color
.WHITE
);
3507 if (type
== DeviceType
.XTERM
) {
3509 * Xterm supports T.416 / ISO-8613-3 codes to select
3510 * either an indexed color or an RGB value. (It also
3511 * permits these ISO-8613-3 SGR sequences to be separated
3512 * by colons rather than semicolons.)
3514 * We will not support any of these additional color
3515 * codes at this time:
3517 * 1. http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
3518 * has a detailed discussion of the current state of
3519 * RGB in various terminals, the point of which is
3520 * that none of them really do the same thing despite
3521 * all appearing to be "xterm".
3524 * https://bugs.kde.org/show_bug.cgi?id=107487#c3,
3525 * even supporting just the "indexed mode" of these
3526 * sequences (which could align easily with existing
3527 * SGR colors) is assumed to mean full support of
3528 * 24-bit RGB. So it is all or nothing.
3530 * Finally, these sequences break the assumptions of
3531 * standard ECMA-48 style parsers as pointed out at
3532 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
3533 * Therefore in order to keep a clean display, we cannot
3534 * parse anything else in this sequence.
3538 // Underscore on, default foreground color
3539 currentState
.attr
.setUnderline(true);
3540 currentState
.attr
.setForeColor(Color
.WHITE
);
3544 // Underscore off, default foreground color
3545 currentState
.attr
.setUnderline(false);
3546 currentState
.attr
.setForeColor(Color
.WHITE
);
3549 // Set black background
3550 currentState
.attr
.setBackColor(Color
.BLACK
);
3553 // Set red background
3554 currentState
.attr
.setBackColor(Color
.RED
);
3557 // Set green background
3558 currentState
.attr
.setBackColor(Color
.GREEN
);
3561 // Set yellow background
3562 currentState
.attr
.setBackColor(Color
.YELLOW
);
3565 // Set blue background
3566 currentState
.attr
.setBackColor(Color
.BLUE
);
3569 // Set magenta background
3570 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3573 // Set cyan background
3574 currentState
.attr
.setBackColor(Color
.CYAN
);
3577 // Set white background
3578 currentState
.attr
.setBackColor(Color
.WHITE
);
3581 if (type
== DeviceType
.XTERM
) {
3583 * Xterm supports T.416 / ISO-8613-3 codes to select
3584 * either an indexed color or an RGB value. (It also
3585 * permits these ISO-8613-3 SGR sequences to be separated
3586 * by colons rather than semicolons.)
3588 * We will not support this at this time. Also, in order
3589 * to keep a clean display, we cannot parse anything else
3596 // Default background
3597 currentState
.attr
.setBackColor(Color
.BLACK
);
3607 * DA - Device attributes.
3610 int extendedFlag
= 0;
3612 if (collectBuffer
.length() > 0) {
3613 String args
= collectBuffer
.substring(1);
3614 if (collectBuffer
.charAt(0) == '>') {
3616 if (collectBuffer
.length() >= 2) {
3617 i
= Integer
.parseInt(args
.toString());
3619 } else if (collectBuffer
.charAt(0) == '=') {
3621 if (collectBuffer
.length() >= 2) {
3622 i
= Integer
.parseInt(args
.toString());
3625 // Unknown code, bail out
3630 if ((i
!= 0) && (i
!= 1)) {
3634 if ((extendedFlag
== 0) && (i
== 0)) {
3635 // Send string directly to remote side
3636 writeRemote(deviceTypeResponse());
3640 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3642 if ((extendedFlag
== 1) && (i
== 0)) {
3644 * Request "What type of terminal are you, what is your
3645 * firmware version, and what hardware options do you have
3648 * Respond: "I am a VT220 (identification code of 1), my
3649 * firmware version is _____ (Pv), and I have _____ Po
3650 * options installed."
3656 if (s8c1t
== true) {
3657 writeRemote("\u009b>1;10;0c");
3659 writeRemote("\033[>1;10;0c");
3665 if ((extendedFlag
== 2) && (i
== 0)) {
3668 * Request "What is your unit ID?"
3670 * Respond: "I was manufactured at site 00 and have a unique ID
3674 writeRemote("\033P!|00010203\033\\");
3679 * DECSTBM - Set top and bottom margins.
3681 private void decstbm() {
3682 boolean decPrivateModeFlag
= false;
3684 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3685 if (collectBuffer
.charAt(i
) == '?') {
3686 decPrivateModeFlag
= true;
3690 if (decPrivateModeFlag
) {
3691 // This could be restore DEC private mode values.
3695 int top
= getCsiParam(0, 1, 1, height
) - 1;
3696 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3701 scrollRegionTop
= top
;
3702 scrollRegionBottom
= bottom
;
3705 cursorPosition(0, 0);
3710 * DECREQTPARM - Request terminal parameters.
3712 private void decreqtparm() {
3713 int i
= getCsiParam(0, 0);
3715 if ((i
!= 0) && (i
!= 1)) {
3722 * Request terminal parameters.
3726 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3727 * (CLoCk MULtiplier = 1, STP option flags = 0)
3731 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3734 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3736 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3742 * DECSCA - Select Character Attributes.
3744 private void decsca() {
3745 int i
= getCsiParam(0, 0);
3747 if ((i
== 0) || (i
== 2)) {
3749 currentState
.attr
.setProtect(false);
3753 currentState
.attr
.setProtect(true);
3758 * DECSTR - Soft Terminal Reset.
3760 private void decstr() {
3761 // Do exactly like RIS - Reset to initial state
3763 // Do I clear screen too? I think so...
3764 eraseScreen(0, 0, height
- 1, width
- 1, false);
3765 cursorPosition(0, 0);
3769 * DSR - Device status report.
3771 private void dsr() {
3772 boolean decPrivateModeFlag
= false;
3773 int row
= currentState
.cursorY
;
3775 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3776 if (collectBuffer
.charAt(i
) == '?') {
3777 decPrivateModeFlag
= true;
3782 int i
= getCsiParam(0, 0);
3787 // Request status report. Respond with "OK, no malfunction."
3789 // Send string directly to remote side
3790 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3793 writeRemote("\u009b0n");
3795 writeRemote("\033[0n");
3800 // Request cursor position. Respond with current position.
3801 if (currentState
.originMode
== true) {
3802 row
-= scrollRegionTop
;
3805 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3808 str
= String
.format("\u009b%d;%dR", row
+ 1,
3809 currentState
.cursorX
+ 1);
3811 str
= String
.format("\033[%d;%dR", row
+ 1,
3812 currentState
.cursorX
+ 1);
3815 // Send string directly to remote side
3820 if (decPrivateModeFlag
== true) {
3822 // Request printer status report. Respond with "Printer not
3825 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3826 && (s8c1t
== true)) {
3827 writeRemote("\u009b?13n");
3829 writeRemote("\033[?13n");
3835 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3836 && (decPrivateModeFlag
== true)
3839 // Request user-defined keys are locked or unlocked. Respond
3840 // with "User-defined keys are locked."
3842 if (s8c1t
== true) {
3843 writeRemote("\u009b?21n");
3845 writeRemote("\033[?21n");
3851 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3852 && (decPrivateModeFlag
== true)
3855 // Request keyboard language. Respond with "Keyboard
3856 // language is North American."
3858 if (s8c1t
== true) {
3859 writeRemote("\u009b?27;1n");
3861 writeRemote("\033[?27;1n");
3868 // Some other option, ignore
3874 * TBC - Tabulation clear.
3876 private void tbc() {
3877 int i
= getCsiParam(0, 0);
3879 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3880 for (Integer stop
: tabStops
) {
3881 if (stop
== currentState
.cursorX
) {
3886 tabStops
= newStops
;
3894 * Erase the characters in the current line from the start column to the
3895 * end column, inclusive.
3897 * @param start starting column to erase (between 0 and width - 1)
3898 * @param end ending column to erase (between 0 and width - 1)
3899 * @param honorProtected if true, do not erase characters with the
3900 * protected attribute set
3902 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3907 if (end
> width
- 1) {
3914 for (int i
= start
; i
<= end
; i
++) {
3915 DisplayLine line
= display
.get(currentState
.cursorY
);
3916 if ((!honorProtected
)
3917 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
3924 * From the VT102 manual:
3926 * Erasing a character also erases any character
3927 * attribute of the character.
3933 * Erase with the current color a.k.a. back-color erase
3936 line
.setChar(i
, ' ');
3937 line
.setAttr(i
, currentState
.attr
);
3945 * Erase a rectangular section of the screen, inclusive. end column,
3948 * @param startRow starting row to erase (between 0 and height - 1)
3949 * @param startCol starting column to erase (between 0 and width - 1)
3950 * @param endRow ending row to erase (between 0 and height - 1)
3951 * @param endCol ending column to erase (between 0 and width - 1)
3952 * @param honorProtected if true, do not erase characters with the
3953 * protected attribute set
3955 private void eraseScreen(final int startRow
, final int startCol
,
3956 final int endRow
, final int endCol
, final boolean honorProtected
) {
3964 || (endRow
< startRow
)
3965 || (endCol
< startCol
)
3970 oldCursorY
= currentState
.cursorY
;
3971 for (int i
= startRow
; i
<= endRow
; i
++) {
3972 currentState
.cursorY
= i
;
3973 eraseLine(startCol
, endCol
, honorProtected
);
3975 // Erase display clears the double attributes
3976 display
.get(i
).setDoubleWidth(false);
3977 display
.get(i
).setDoubleHeight(0);
3979 currentState
.cursorY
= oldCursorY
;
3983 * VT220 printer functions. All of these are parsed, but won't do
3986 private void printerFunctions() {
3987 boolean decPrivateModeFlag
= false;
3988 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3989 if (collectBuffer
.charAt(i
) == '?') {
3990 decPrivateModeFlag
= true;
3995 int i
= getCsiParam(0, 0);
4000 if (decPrivateModeFlag
== false) {
4006 if (decPrivateModeFlag
== true) {
4007 // Print cursor line
4012 if (decPrivateModeFlag
== true) {
4013 // Auto print mode OFF
4015 // Printer controller OFF
4017 // Characters re-appear on the screen
4018 printerControllerMode
= false;
4023 if (decPrivateModeFlag
== true) {
4027 // Printer controller
4029 // Characters get sucked into oblivion
4030 printerControllerMode
= true;
4041 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4042 * of remote systems will send an XTerm title sequence even if TERM isn't
4045 * @param xtermChar the character received from the remote side
4047 private void oscPut(final char xtermChar
) {
4049 collectBuffer
.append(xtermChar
);
4052 if (xtermChar
== 0x07) {
4053 String args
= collectBuffer
.substring(0,
4054 collectBuffer
.length() - 1);
4055 String
[] p
= args
.toString().split(";");
4057 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4065 // Go to SCAN_GROUND state
4072 * Run this input character through the ECMA48 state machine.
4074 * @param ch character from the remote side
4076 private void consume(char ch
) {
4079 // System.err.printf("%c", ch);
4081 // Special case for VT10x: 7-bit characters only
4082 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4083 ch
= (char)(ch
& 0x7F);
4086 // Special "anywhere" states
4088 // 18, 1A --> execute, then switch to SCAN_GROUND
4089 if ((ch
== 0x18) || (ch
== 0x1A)) {
4090 // CAN and SUB abort escape sequences
4095 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4099 && (scanState
!= ScanState
.DCS_ENTRY
)
4100 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4101 && (scanState
!= ScanState
.DCS_IGNORE
)
4102 && (scanState
!= ScanState
.DCS_PARAM
)
4103 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4106 scanState
= ScanState
.ESCAPE
;
4110 // 0x9B == CSI 8-bit sequence
4112 scanState
= ScanState
.CSI_ENTRY
;
4116 // 0x9D goes to ScanState.OSC_STRING
4118 scanState
= ScanState
.OSC_STRING
;
4122 // 0x90 goes to DCS_ENTRY
4124 scanState
= ScanState
.DCS_ENTRY
;
4128 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4129 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4130 scanState
= ScanState
.SOSPMAPC_STRING
;
4134 // 0x7F (DEL) is always discarded
4139 switch (scanState
) {
4142 // 00-17, 19, 1C-1F --> execute
4143 // 80-8F, 91-9A, 9C --> execute
4144 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4145 handleControlChar(ch
);
4149 if (((ch
>= 0x20) && (ch
<= 0x7F))
4153 // VT220 printer --> trash bin
4154 if (((type
== DeviceType
.VT220
)
4155 || (type
== DeviceType
.XTERM
))
4156 && (printerControllerMode
== true)
4161 // Hang onto this character
4162 repCh
= mapCharacter(ch
);
4164 // Print this character
4165 printCharacter(repCh
);
4170 // 00-17, 19, 1C-1F --> execute
4172 handleControlChar(ch
);
4176 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4177 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4179 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4183 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4184 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4195 // DECSC - Save cursor
4196 // Note this code overlaps both ANSI and VT52 mode
4201 // DECRC - Restore cursor
4202 // Note this code overlaps both ANSI and VT52 mode
4211 if (vt52Mode
== true) {
4212 // DECANM - Enter ANSI mode
4214 arrowKeyMode
= ArrowKeyMode
.VT100
;
4217 * From the VT102 docs: "You use ANSI mode to select
4218 * most terminal features; the terminal uses the same
4219 * features when it switches to VT52 mode. You
4220 * cannot, however, change most of these features in
4223 * In other words, do not reset any other attributes
4224 * when switching between VT52 submode and ANSI.
4228 currentState
.g0Charset
= CharacterSet
.US
;
4229 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4231 singleshift
= Singleshift
.NONE
;
4232 currentState
.glLockshift
= LockshiftMode
.NONE
;
4233 currentState
.grLockshift
= LockshiftMode
.NONE
;
4237 // DECKPAM - Keypad application mode
4238 // Note this code overlaps both ANSI and VT52 mode
4242 // DECKPNM - Keypad numeric mode
4243 // Note this code overlaps both ANSI and VT52 mode
4250 if (vt52Mode
== true) {
4251 // Cursor up, and stop at the top without scrolling
4256 if (vt52Mode
== true) {
4257 // Cursor down, and stop at the bottom without scrolling
4258 cursorDown(1, false);
4262 if (vt52Mode
== true) {
4263 // Cursor right, and stop at the right without scrolling
4264 cursorRight(1, false);
4268 if (vt52Mode
== true) {
4269 // Cursor left, and stop at the left without scrolling
4270 cursorLeft(1, false);
4277 if (vt52Mode
== true) {
4285 if (vt52Mode
== true) {
4286 // G0 --> Special graphics
4287 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
4291 if (vt52Mode
== true) {
4293 currentState
.g0Charset
= CharacterSet
.US
;
4297 if (vt52Mode
== true) {
4299 cursorPosition(0, 0);
4301 // HTS - Horizontal tabulation set
4306 if (vt52Mode
== true) {
4307 // Reverse line feed. Same as RI.
4312 if (vt52Mode
== true) {
4313 // Erase to end of screen
4314 eraseLine(currentState
.cursorX
, width
- 1, false);
4315 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4320 if (vt52Mode
== true) {
4321 // Erase to end of line
4322 eraseLine(currentState
.cursorX
, width
- 1, false);
4328 if (vt52Mode
== true) {
4331 // RI - Reverse index
4336 if (vt52Mode
== false) {
4338 singleshift
= Singleshift
.SS2
;
4342 if (vt52Mode
== false) {
4344 singleshift
= Singleshift
.SS3
;
4351 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4367 if (vt52Mode
== true) {
4368 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4376 if (vt52Mode
== true) {
4378 // Send string directly to remote side
4379 writeRemote("\033/Z");
4382 // Send string directly to remote side
4383 writeRemote(deviceTypeResponse());
4394 // VT52 cannot get to any of these other states
4395 if (vt52Mode
== true) {
4400 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4407 // RIS - Reset to initial state
4409 // Do I clear screen too? I think so...
4410 eraseScreen(0, 0, height
- 1, width
- 1, false);
4411 cursorPosition(0, 0);
4425 if ((type
== DeviceType
.VT220
)
4426 || (type
== DeviceType
.XTERM
)) {
4428 // VT220 lockshift G2 into GL
4429 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4434 if ((type
== DeviceType
.VT220
)
4435 || (type
== DeviceType
.XTERM
)) {
4437 // VT220 lockshift G3 into GL
4438 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4456 if ((type
== DeviceType
.VT220
)
4457 || (type
== DeviceType
.XTERM
)) {
4459 // VT220 lockshift G3 into GR
4460 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4465 if ((type
== DeviceType
.VT220
)
4466 || (type
== DeviceType
.XTERM
)) {
4468 // VT220 lockshift G2 into GR
4469 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4475 if ((type
== DeviceType
.VT220
)
4476 || (type
== DeviceType
.XTERM
)) {
4478 // VT220 lockshift G1 into GR
4479 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4489 // 0x5B goes to CSI_ENTRY
4491 scanState
= ScanState
.CSI_ENTRY
;
4494 // 0x5D goes to OSC_STRING
4496 scanState
= ScanState
.OSC_STRING
;
4499 // 0x50 goes to DCS_ENTRY
4501 scanState
= ScanState
.DCS_ENTRY
;
4504 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4505 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4506 scanState
= ScanState
.SOSPMAPC_STRING
;
4511 case ESCAPE_INTERMEDIATE
:
4512 // 00-17, 19, 1C-1F --> execute
4514 handleControlChar(ch
);
4517 // 20-2F --> collect
4518 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4522 // 30-7E --> dispatch, then switch to GROUND
4523 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4526 if ((collectBuffer
.length() == 1)
4527 && (collectBuffer
.charAt(0) == '(')) {
4528 // G0 --> Special graphics
4529 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4531 if ((collectBuffer
.length() == 1)
4532 && (collectBuffer
.charAt(0) == ')')) {
4533 // G1 --> Special graphics
4534 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4536 if ((type
== DeviceType
.VT220
)
4537 || (type
== DeviceType
.XTERM
)) {
4539 if ((collectBuffer
.length() == 1)
4540 && (collectBuffer
.charAt(0) == '*')) {
4541 // G2 --> Special graphics
4542 currentState
.g2Charset
= CharacterSet
.DRAWING
;
4544 if ((collectBuffer
.length() == 1)
4545 && (collectBuffer
.charAt(0) == '+')) {
4546 // G3 --> Special graphics
4547 currentState
.g3Charset
= CharacterSet
.DRAWING
;
4552 if ((collectBuffer
.length() == 1)
4553 && (collectBuffer
.charAt(0) == '(')) {
4554 // G0 --> Alternate character ROM standard character set
4555 currentState
.g0Charset
= CharacterSet
.ROM
;
4557 if ((collectBuffer
.length() == 1)
4558 && (collectBuffer
.charAt(0) == ')')) {
4559 // G1 --> Alternate character ROM standard character set
4560 currentState
.g1Charset
= CharacterSet
.ROM
;
4564 if ((collectBuffer
.length() == 1)
4565 && (collectBuffer
.charAt(0) == '(')) {
4566 // G0 --> Alternate character ROM special graphics
4567 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4569 if ((collectBuffer
.length() == 1)
4570 && (collectBuffer
.charAt(0) == ')')) {
4571 // G1 --> Alternate character ROM special graphics
4572 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4576 if ((collectBuffer
.length() == 1)
4577 && (collectBuffer
.charAt(0) == '#')) {
4578 // DECDHL - Double-height line (top half)
4583 if ((collectBuffer
.length() == 1)
4584 && (collectBuffer
.charAt(0) == '#')) {
4585 // DECDHL - Double-height line (bottom half)
4588 if ((type
== DeviceType
.VT220
)
4589 || (type
== DeviceType
.XTERM
)) {
4591 if ((collectBuffer
.length() == 1)
4592 && (collectBuffer
.charAt(0) == '(')) {
4594 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4596 if ((collectBuffer
.length() == 1)
4597 && (collectBuffer
.charAt(0) == ')')) {
4599 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4601 if ((collectBuffer
.length() == 1)
4602 && (collectBuffer
.charAt(0) == '*')) {
4604 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4606 if ((collectBuffer
.length() == 1)
4607 && (collectBuffer
.charAt(0) == '+')) {
4609 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4614 if ((collectBuffer
.length() == 1)
4615 && (collectBuffer
.charAt(0) == '#')) {
4616 // DECSWL - Single-width line
4619 if ((type
== DeviceType
.VT220
)
4620 || (type
== DeviceType
.XTERM
)) {
4622 if ((collectBuffer
.length() == 1)
4623 && (collectBuffer
.charAt(0) == '(')) {
4625 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4627 if ((collectBuffer
.length() == 1)
4628 && (collectBuffer
.charAt(0) == ')')) {
4630 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4632 if ((collectBuffer
.length() == 1)
4633 && (collectBuffer
.charAt(0) == '*')) {
4635 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4637 if ((collectBuffer
.length() == 1)
4638 && (collectBuffer
.charAt(0) == '+')) {
4640 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4645 if ((collectBuffer
.length() == 1)
4646 && (collectBuffer
.charAt(0) == '#')) {
4647 // DECDWL - Double-width line
4650 if ((type
== DeviceType
.VT220
)
4651 || (type
== DeviceType
.XTERM
)) {
4653 if ((collectBuffer
.length() == 1)
4654 && (collectBuffer
.charAt(0) == '(')) {
4656 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4658 if ((collectBuffer
.length() == 1)
4659 && (collectBuffer
.charAt(0) == ')')) {
4661 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4663 if ((collectBuffer
.length() == 1)
4664 && (collectBuffer
.charAt(0) == '*')) {
4666 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4668 if ((collectBuffer
.length() == 1)
4669 && (collectBuffer
.charAt(0) == '+')) {
4671 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4676 if ((type
== DeviceType
.VT220
)
4677 || (type
== DeviceType
.XTERM
)) {
4679 if ((collectBuffer
.length() == 1)
4680 && (collectBuffer
.charAt(0) == '(')) {
4682 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4684 if ((collectBuffer
.length() == 1)
4685 && (collectBuffer
.charAt(0) == ')')) {
4687 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4689 if ((collectBuffer
.length() == 1)
4690 && (collectBuffer
.charAt(0) == '*')) {
4692 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4694 if ((collectBuffer
.length() == 1)
4695 && (collectBuffer
.charAt(0) == '+')) {
4697 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4702 if ((collectBuffer
.length() == 1)
4703 && (collectBuffer
.charAt(0) == '#')) {
4704 // DECALN - Screen alignment display
4713 if ((type
== DeviceType
.VT220
)
4714 || (type
== DeviceType
.XTERM
)) {
4716 if ((collectBuffer
.length() == 1)
4717 && (collectBuffer
.charAt(0) == '(')) {
4718 // G0 --> DEC_SUPPLEMENTAL
4719 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4721 if ((collectBuffer
.length() == 1)
4722 && (collectBuffer
.charAt(0) == ')')) {
4723 // G1 --> DEC_SUPPLEMENTAL
4724 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4726 if ((collectBuffer
.length() == 1)
4727 && (collectBuffer
.charAt(0) == '*')) {
4728 // G2 --> DEC_SUPPLEMENTAL
4729 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4731 if ((collectBuffer
.length() == 1)
4732 && (collectBuffer
.charAt(0) == '+')) {
4733 // G3 --> DEC_SUPPLEMENTAL
4734 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4739 if ((type
== DeviceType
.VT220
)
4740 || (type
== DeviceType
.XTERM
)) {
4742 if ((collectBuffer
.length() == 1)
4743 && (collectBuffer
.charAt(0) == '(')) {
4745 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4747 if ((collectBuffer
.length() == 1)
4748 && (collectBuffer
.charAt(0) == ')')) {
4750 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4752 if ((collectBuffer
.length() == 1)
4753 && (collectBuffer
.charAt(0) == '*')) {
4755 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4757 if ((collectBuffer
.length() == 1)
4758 && (collectBuffer
.charAt(0) == '+')) {
4760 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4769 if ((collectBuffer
.length() == 1)
4770 && (collectBuffer
.charAt(0) == '(')) {
4771 // G0 --> United Kingdom set
4772 currentState
.g0Charset
= CharacterSet
.UK
;
4774 if ((collectBuffer
.length() == 1)
4775 && (collectBuffer
.charAt(0) == ')')) {
4776 // G1 --> United Kingdom set
4777 currentState
.g1Charset
= CharacterSet
.UK
;
4779 if ((type
== DeviceType
.VT220
)
4780 || (type
== DeviceType
.XTERM
)) {
4782 if ((collectBuffer
.length() == 1)
4783 && (collectBuffer
.charAt(0) == '*')) {
4784 // G2 --> United Kingdom set
4785 currentState
.g2Charset
= CharacterSet
.UK
;
4787 if ((collectBuffer
.length() == 1)
4788 && (collectBuffer
.charAt(0) == '+')) {
4789 // G3 --> United Kingdom set
4790 currentState
.g3Charset
= CharacterSet
.UK
;
4795 if ((collectBuffer
.length() == 1)
4796 && (collectBuffer
.charAt(0) == '(')) {
4798 currentState
.g0Charset
= CharacterSet
.US
;
4800 if ((collectBuffer
.length() == 1)
4801 && (collectBuffer
.charAt(0) == ')')) {
4803 currentState
.g1Charset
= CharacterSet
.US
;
4805 if ((type
== DeviceType
.VT220
)
4806 || (type
== DeviceType
.XTERM
)) {
4808 if ((collectBuffer
.length() == 1)
4809 && (collectBuffer
.charAt(0) == '*')) {
4811 currentState
.g2Charset
= CharacterSet
.US
;
4813 if ((collectBuffer
.length() == 1)
4814 && (collectBuffer
.charAt(0) == '+')) {
4816 currentState
.g3Charset
= CharacterSet
.US
;
4821 if ((type
== DeviceType
.VT220
)
4822 || (type
== DeviceType
.XTERM
)) {
4824 if ((collectBuffer
.length() == 1)
4825 && (collectBuffer
.charAt(0) == '(')) {
4827 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4829 if ((collectBuffer
.length() == 1)
4830 && (collectBuffer
.charAt(0) == ')')) {
4832 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4834 if ((collectBuffer
.length() == 1)
4835 && (collectBuffer
.charAt(0) == '*')) {
4837 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4839 if ((collectBuffer
.length() == 1)
4840 && (collectBuffer
.charAt(0) == '+')) {
4842 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4849 if ((type
== DeviceType
.VT220
)
4850 || (type
== DeviceType
.XTERM
)) {
4852 if ((collectBuffer
.length() == 1)
4853 && (collectBuffer
.charAt(0) == '(')) {
4855 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4857 if ((collectBuffer
.length() == 1)
4858 && (collectBuffer
.charAt(0) == ')')) {
4860 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4862 if ((collectBuffer
.length() == 1)
4863 && (collectBuffer
.charAt(0) == '*')) {
4865 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4867 if ((collectBuffer
.length() == 1)
4868 && (collectBuffer
.charAt(0) == '+')) {
4870 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4875 if ((type
== DeviceType
.VT220
)
4876 || (type
== DeviceType
.XTERM
)) {
4878 if ((collectBuffer
.length() == 1)
4879 && (collectBuffer
.charAt(0) == ' ')) {
4886 if ((type
== DeviceType
.VT220
)
4887 || (type
== DeviceType
.XTERM
)) {
4889 if ((collectBuffer
.length() == 1)
4890 && (collectBuffer
.charAt(0) == ' ')) {
4897 if ((type
== DeviceType
.VT220
)
4898 || (type
== DeviceType
.XTERM
)) {
4900 if ((collectBuffer
.length() == 1)
4901 && (collectBuffer
.charAt(0) == '(')) {
4903 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4905 if ((collectBuffer
.length() == 1)
4906 && (collectBuffer
.charAt(0) == ')')) {
4908 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4910 if ((collectBuffer
.length() == 1)
4911 && (collectBuffer
.charAt(0) == '*')) {
4913 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4915 if ((collectBuffer
.length() == 1)
4916 && (collectBuffer
.charAt(0) == '+')) {
4918 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4926 if ((type
== DeviceType
.VT220
)
4927 || (type
== DeviceType
.XTERM
)) {
4929 if ((collectBuffer
.length() == 1)
4930 && (collectBuffer
.charAt(0) == '(')) {
4932 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4934 if ((collectBuffer
.length() == 1)
4935 && (collectBuffer
.charAt(0) == ')')) {
4937 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4939 if ((collectBuffer
.length() == 1)
4940 && (collectBuffer
.charAt(0) == '*')) {
4942 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4944 if ((collectBuffer
.length() == 1)
4945 && (collectBuffer
.charAt(0) == '+')) {
4947 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4958 if ((type
== DeviceType
.VT220
)
4959 || (type
== DeviceType
.XTERM
)) {
4961 if ((collectBuffer
.length() == 1)
4962 && (collectBuffer
.charAt(0) == '(')) {
4964 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4966 if ((collectBuffer
.length() == 1)
4967 && (collectBuffer
.charAt(0) == ')')) {
4969 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4971 if ((collectBuffer
.length() == 1)
4972 && (collectBuffer
.charAt(0) == '*')) {
4974 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4976 if ((collectBuffer
.length() == 1)
4977 && (collectBuffer
.charAt(0) == '+')) {
4979 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4984 if ((type
== DeviceType
.VT220
)
4985 || (type
== DeviceType
.XTERM
)) {
4987 if ((collectBuffer
.length() == 1)
4988 && (collectBuffer
.charAt(0) == '(')) {
4990 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4992 if ((collectBuffer
.length() == 1)
4993 && (collectBuffer
.charAt(0) == ')')) {
4995 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4997 if ((collectBuffer
.length() == 1)
4998 && (collectBuffer
.charAt(0) == '*')) {
5000 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5002 if ((collectBuffer
.length() == 1)
5003 && (collectBuffer
.charAt(0) == '+')) {
5005 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5017 if ((type
== DeviceType
.VT220
)
5018 || (type
== DeviceType
.XTERM
)) {
5020 if ((collectBuffer
.length() == 1)
5021 && (collectBuffer
.charAt(0) == '(')) {
5023 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5025 if ((collectBuffer
.length() == 1)
5026 && (collectBuffer
.charAt(0) == ')')) {
5028 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5030 if ((collectBuffer
.length() == 1)
5031 && (collectBuffer
.charAt(0) == '*')) {
5033 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5035 if ((collectBuffer
.length() == 1)
5036 && (collectBuffer
.charAt(0) == '+')) {
5038 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5043 if ((type
== DeviceType
.VT220
)
5044 || (type
== DeviceType
.XTERM
)) {
5046 if ((collectBuffer
.length() == 1)
5047 && (collectBuffer
.charAt(0) == '(')) {
5049 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5051 if ((collectBuffer
.length() == 1)
5052 && (collectBuffer
.charAt(0) == ')')) {
5054 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5056 if ((collectBuffer
.length() == 1)
5057 && (collectBuffer
.charAt(0) == '*')) {
5059 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5061 if ((collectBuffer
.length() == 1)
5062 && (collectBuffer
.charAt(0) == '+')) {
5064 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5111 // 0x9C goes to GROUND
5119 // 00-17, 19, 1C-1F --> execute
5121 handleControlChar(ch
);
5124 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5125 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5127 scanState
= ScanState
.CSI_INTERMEDIATE
;
5130 // 30-39, 3B --> param, then switch to CSI_PARAM
5131 if ((ch
>= '0') && (ch
<= '9')) {
5133 scanState
= ScanState
.CSI_PARAM
;
5137 scanState
= ScanState
.CSI_PARAM
;
5140 // 3C-3F --> collect, then switch to CSI_PARAM
5141 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5143 scanState
= ScanState
.CSI_PARAM
;
5146 // 40-7E --> dispatch, then switch to GROUND
5147 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5150 // ICH - Insert character
5158 // CUD - Cursor down
5162 // CUF - Cursor forward
5166 // CUB - Cursor backward
5170 // CNL - Cursor down and to column 1
5171 if (type
== DeviceType
.XTERM
) {
5176 // CPL - Cursor up and to column 1
5177 if (type
== DeviceType
.XTERM
) {
5182 // CHA - Cursor to column # in current row
5183 if (type
== DeviceType
.XTERM
) {
5188 // CUP - Cursor position
5192 // CHT - Cursor forward X tab stops (default 1)
5193 if (type
== DeviceType
.XTERM
) {
5198 // ED - Erase in display
5202 // EL - Erase in line
5217 // DCH - Delete character
5224 // Scroll up X lines (default 1)
5225 if (type
== DeviceType
.XTERM
) {
5230 // Scroll down X lines (default 1)
5231 if (type
== DeviceType
.XTERM
) {
5240 if ((type
== DeviceType
.VT220
)
5241 || (type
== DeviceType
.XTERM
)) {
5243 // ECH - Erase character
5250 // CBT - Cursor backward X tab stops (default 1)
5251 if (type
== DeviceType
.XTERM
) {
5262 // HPA - Cursor to column # in current row. Same as CHA
5263 if (type
== DeviceType
.XTERM
) {
5268 // HPR - Cursor right. Same as CUF
5269 if (type
== DeviceType
.XTERM
) {
5274 // REP - Repeat last char X times
5275 if (type
== DeviceType
.XTERM
) {
5280 // DA - Device attributes
5284 // VPA - Cursor to row, current column.
5285 if (type
== DeviceType
.XTERM
) {
5290 // VPR - Cursor down. Same as CUD
5291 if (type
== DeviceType
.XTERM
) {
5296 // HVP - Horizontal and vertical position
5300 // TBC - Tabulation clear
5304 // Sets an ANSI or DEC private toggle
5308 if ((type
== DeviceType
.VT220
)
5309 || (type
== DeviceType
.XTERM
)) {
5311 // Printer functions
5319 // Sets an ANSI or DEC private toggle
5323 // SGR - Select graphics rendition
5327 // DSR - Device status report
5334 // DECLL - Load leds
5338 // DECSTBM - Set top and bottom margins
5342 // Save cursor (ANSI.SYS)
5343 if (type
== DeviceType
.XTERM
) {
5344 savedState
.cursorX
= currentState
.cursorX
;
5345 savedState
.cursorY
= currentState
.cursorY
;
5351 // Restore cursor (ANSI.SYS)
5352 if (type
== DeviceType
.XTERM
) {
5353 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5360 // DECREQTPARM - Request terminal parameters
5376 // 0x9C goes to GROUND
5381 // 0x3A goes to CSI_IGNORE
5383 scanState
= ScanState
.CSI_IGNORE
;
5388 // 00-17, 19, 1C-1F --> execute
5390 handleControlChar(ch
);
5393 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5394 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5396 scanState
= ScanState
.CSI_INTERMEDIATE
;
5399 // 30-39, 3B --> param
5400 if ((ch
>= '0') && (ch
<= '9')) {
5407 // 0x3A goes to CSI_IGNORE
5409 scanState
= ScanState
.CSI_IGNORE
;
5411 // 0x3C-3F goes to CSI_IGNORE
5412 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5413 scanState
= ScanState
.CSI_IGNORE
;
5416 // 40-7E --> dispatch, then switch to GROUND
5417 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5420 // ICH - Insert character
5428 // CUD - Cursor down
5432 // CUF - Cursor forward
5436 // CUB - Cursor backward
5440 // CNL - Cursor down and to column 1
5441 if (type
== DeviceType
.XTERM
) {
5446 // CPL - Cursor up and to column 1
5447 if (type
== DeviceType
.XTERM
) {
5452 // CHA - Cursor to column # in current row
5453 if (type
== DeviceType
.XTERM
) {
5458 // CUP - Cursor position
5462 // CHT - Cursor forward X tab stops (default 1)
5463 if (type
== DeviceType
.XTERM
) {
5468 // ED - Erase in display
5472 // EL - Erase in line
5487 // DCH - Delete character
5494 // Scroll up X lines (default 1)
5495 if (type
== DeviceType
.XTERM
) {
5500 // Scroll down X lines (default 1)
5501 if (type
== DeviceType
.XTERM
) {
5510 if ((type
== DeviceType
.VT220
)
5511 || (type
== DeviceType
.XTERM
)) {
5513 // ECH - Erase character
5520 // CBT - Cursor backward X tab stops (default 1)
5521 if (type
== DeviceType
.XTERM
) {
5532 // HPA - Cursor to column # in current row. Same as CHA
5533 if (type
== DeviceType
.XTERM
) {
5538 // HPR - Cursor right. Same as CUF
5539 if (type
== DeviceType
.XTERM
) {
5544 // REP - Repeat last char X times
5545 if (type
== DeviceType
.XTERM
) {
5550 // DA - Device attributes
5554 // VPA - Cursor to row, current column.
5555 if (type
== DeviceType
.XTERM
) {
5560 // VPR - Cursor down. Same as CUD
5561 if (type
== DeviceType
.XTERM
) {
5566 // HVP - Horizontal and vertical position
5570 // TBC - Tabulation clear
5574 // Sets an ANSI or DEC private toggle
5578 if ((type
== DeviceType
.VT220
)
5579 || (type
== DeviceType
.XTERM
)) {
5581 // Printer functions
5589 // Sets an ANSI or DEC private toggle
5593 // SGR - Select graphics rendition
5597 // DSR - Device status report
5604 // DECLL - Load leds
5608 // DECSTBM - Set top and bottom margins
5618 // DECREQTPARM - Request terminal parameters
5635 case CSI_INTERMEDIATE
:
5636 // 00-17, 19, 1C-1F --> execute
5638 handleControlChar(ch
);
5641 // 20-2F --> collect
5642 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5646 // 0x30-3F goes to CSI_IGNORE
5647 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5648 scanState
= ScanState
.CSI_IGNORE
;
5651 // 40-7E --> dispatch, then switch to GROUND
5652 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5704 if (((type
== DeviceType
.VT220
)
5705 || (type
== DeviceType
.XTERM
))
5706 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5708 // DECSCL - compatibility level
5711 if ((type
== DeviceType
.XTERM
)
5712 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
5714 // DECSTR - Soft terminal reset
5719 if (((type
== DeviceType
.VT220
)
5720 || (type
== DeviceType
.XTERM
))
5721 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5749 // 00-17, 19, 1C-1F --> execute
5751 handleControlChar(ch
);
5754 // 20-2F --> collect
5755 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5759 // 40-7E --> ignore, then switch to GROUND
5760 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5764 // 20-3F, 7F --> ignore
5770 // 0x9C goes to GROUND
5775 // 0x1B 0x5C goes to GROUND
5780 if ((collectBuffer
.length() > 0)
5781 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5787 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5788 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5790 scanState
= ScanState
.DCS_INTERMEDIATE
;
5793 // 30-39, 3B --> param, then switch to DCS_PARAM
5794 if ((ch
>= '0') && (ch
<= '9')) {
5796 scanState
= ScanState
.DCS_PARAM
;
5800 scanState
= ScanState
.DCS_PARAM
;
5803 // 3C-3F --> collect, then switch to DCS_PARAM
5804 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5806 scanState
= ScanState
.DCS_PARAM
;
5809 // 00-17, 19, 1C-1F, 7F --> ignore
5811 // 0x3A goes to DCS_IGNORE
5813 scanState
= ScanState
.DCS_IGNORE
;
5816 // 0x40-7E goes to DCS_PASSTHROUGH
5817 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5818 scanState
= ScanState
.DCS_PASSTHROUGH
;
5822 case DCS_INTERMEDIATE
:
5824 // 0x9C goes to GROUND
5829 // 0x1B 0x5C goes to GROUND
5834 if ((collectBuffer
.length() > 0)
5835 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5841 // 0x30-3F goes to DCS_IGNORE
5842 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5843 scanState
= ScanState
.DCS_IGNORE
;
5846 // 0x40-7E goes to DCS_PASSTHROUGH
5847 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5848 scanState
= ScanState
.DCS_PASSTHROUGH
;
5851 // 00-17, 19, 1C-1F, 7F --> ignore
5856 // 0x9C goes to GROUND
5861 // 0x1B 0x5C goes to GROUND
5866 if ((collectBuffer
.length() > 0)
5867 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5873 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5874 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5876 scanState
= ScanState
.DCS_INTERMEDIATE
;
5879 // 30-39, 3B --> param
5880 if ((ch
>= '0') && (ch
<= '9')) {
5887 // 00-17, 19, 1C-1F, 7F --> ignore
5889 // 0x3A, 3C-3F goes to DCS_IGNORE
5891 scanState
= ScanState
.DCS_IGNORE
;
5893 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5894 scanState
= ScanState
.DCS_IGNORE
;
5897 // 0x40-7E goes to DCS_PASSTHROUGH
5898 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5899 scanState
= ScanState
.DCS_PASSTHROUGH
;
5903 case DCS_PASSTHROUGH
:
5904 // 0x9C goes to GROUND
5909 // 0x1B 0x5C goes to GROUND
5914 if ((collectBuffer
.length() > 0)
5915 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5921 // 00-17, 19, 1C-1F, 20-7E --> put
5929 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5932 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5941 // 00-17, 19, 1C-1F, 20-7F --> ignore
5943 // 0x9C goes to GROUND
5950 case SOSPMAPC_STRING
:
5951 // 00-17, 19, 1C-1F, 20-7F --> ignore
5953 // 0x9C goes to GROUND
5961 // Special case for Xterm: OSC can pass control characters
5962 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5966 // 00-17, 19, 1C-1F --> ignore
5968 // 20-7F --> osc_put
5969 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5973 // 0x9C goes to GROUND
5980 case VT52_DIRECT_CURSOR_ADDRESS
:
5981 // This is a special case for the VT52 sequence "ESC Y l c"
5982 if (collectBuffer
.length() == 0) {
5984 } else if (collectBuffer
.length() == 1) {
5985 // We've got the two characters, one in the buffer and the
5987 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
5996 * Expose current cursor X to outside world.
5998 * @return current cursor X
6000 public final int getCursorX() {
6001 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6002 return currentState
.cursorX
* 2;
6004 return currentState
.cursorX
;
6008 * Expose current cursor Y to outside world.
6010 * @return current cursor Y
6012 public final int getCursorY() {
6013 return currentState
.cursorY
;
6017 * Read function runs on a separate thread.
6019 public final void run() {
6020 boolean utf8
= false;
6021 boolean done
= false;
6023 if (type
== DeviceType
.XTERM
) {
6027 // available() will often return > 1, so we need to read in chunks to
6029 char [] readBufferUTF8
= null;
6030 byte [] readBuffer
= null;
6032 readBufferUTF8
= new char[128];
6034 readBuffer
= new byte[128];
6037 while (!done
&& !stopReaderThread
) {
6039 int n
= inputStream
.available();
6041 // System.err.printf("available() %d\n", n); System.err.flush();
6043 if (readBufferUTF8
.length
< n
) {
6044 // The buffer wasn't big enough, make it huger
6045 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
6048 readBufferUTF8
= new char[newSizeHalf
* 2];
6051 if (readBuffer
.length
< n
) {
6052 // The buffer wasn't big enough, make it huger
6053 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
6054 readBuffer
= new byte[newSizeHalf
* 2];
6060 } catch (InterruptedException e
) {
6069 rc
= input
.read(readBufferUTF8
, 0,
6070 readBufferUTF8
.length
);
6072 rc
= inputStream
.read(readBuffer
, 0,
6075 } catch (ReadTimeoutException e
) {
6079 // System.err.printf("read() %d\n", rc); System.err.flush();
6084 for (int i
= 0; i
< rc
; i
++) {
6087 ch
= readBufferUTF8
[i
];
6092 synchronized (this) {
6093 // Don't step on UI events
6097 // Permit my enclosing UI to know that I updated.
6098 if (displayListener
!= null) {
6099 displayListener
.displayChanged();
6102 // System.err.println("end while loop"); System.err.flush();
6103 } catch (IOException e
) {
6104 e
.printStackTrace();
6108 } // while ((done == false) && (stopReaderThread == false))
6110 // Let the rest of the world know that I am done.
6111 stopReaderThread
= true;
6114 inputStream
.cancelRead();
6115 inputStream
.close();
6117 } catch (IOException e
) {
6123 } catch (IOException e
) {
6127 // System.err.println("*** run() exiting..."); System.err.flush();