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 int delta
= height
- this.height
;
571 this.height
= height
;
572 scrollRegionBottom
+= delta
;
573 if (scrollRegionBottom
< 0) {
574 scrollRegionBottom
= height
;
576 if (scrollRegionTop
>= scrollRegionBottom
) {
579 if (currentState
.cursorY
>= height
) {
580 currentState
.cursorY
= height
- 1;
582 if (savedState
.cursorY
>= height
) {
583 savedState
.cursorY
= height
- 1;
585 while (display
.size() < height
) {
586 DisplayLine line
= new DisplayLine(currentState
.attr
);
587 line
.setReverseColor(reverseVideo
);
590 while (display
.size() > height
) {
591 scrollback
.add(display
.remove(0));
596 * Top margin of the scrolling region.
598 private int scrollRegionTop
;
601 * Bottom margin of the scrolling region.
603 private int scrollRegionBottom
;
606 * Right margin column number. This can be selected by the remote side
607 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
609 private int rightMargin
;
612 * Last character printed.
617 * VT100-style line wrapping: a character is placed in column 80 (or
618 * 132), but the line does NOT wrap until another character is written to
619 * column 1 of the next line, after which the cursor moves to column 2.
621 private boolean wrapLineFlag
;
624 * VT220 single shift flag.
626 private Singleshift singleshift
= Singleshift
.NONE
;
629 * true = insert characters, false = overwrite.
631 private boolean insertMode
= false;
634 * VT52 mode as selected by DECANM. True means VT52, false means
635 * ANSI. Default is ANSI.
637 private boolean vt52Mode
= false;
640 * Visible cursor (DECTCEM).
642 private boolean cursorVisible
= true;
645 * Get visible cursor flag.
647 * @return if true, the cursor is visible
649 public final boolean isCursorVisible() {
650 return cursorVisible
;
654 * Screen title as set by the xterm OSC sequence. Lots of applications
655 * send a screenTitle regardless of whether it is an xterm client or not.
657 private String screenTitle
= "";
660 * Get the screen title as set by the xterm OSC sequence. Lots of
661 * applications send a screenTitle regardless of whether it is an xterm
664 * @return screen title
666 public final String
getScreenTitle() {
671 * Parameter characters being collected.
673 private List
<Integer
> csiParams
;
676 * Non-csi collect buffer.
678 private StringBuilder collectBuffer
;
681 * When true, use the G1 character set.
683 private boolean shiftOut
= false;
686 * Horizontal tab stop locations.
688 private List
<Integer
> tabStops
;
691 * S8C1T. True means 8bit controls, false means 7bit controls.
693 private boolean s8c1t
= false;
696 * Printer mode. True means send all output to printer, which discards
699 private boolean printerControllerMode
= false;
702 * LMN line mode. If true, linefeed() puts the cursor on the first
703 * column of the next line. If false, linefeed() puts the cursor one
704 * line down on the current line. The default is false.
706 private boolean newLineMode
= false;
709 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
711 private ArrowKeyMode arrowKeyMode
;
714 * Whether number pad keys send VT100 or VT52, application or numeric
717 @SuppressWarnings("unused")
718 private KeypadMode keypadMode
;
721 * When true, the terminal is in 132-column mode (DECCOLM).
723 private boolean columns132
= false;
726 * Get 132 columns value.
728 * @return if true, the terminal is in 132 column mode
730 public final boolean isColumns132() {
735 * true = reverse video. Set by DECSCNM.
737 private boolean reverseVideo
= false;
740 * false = echo characters locally.
742 private boolean fullDuplex
= true;
745 * DECSC/DECRC save/restore a subset of the total state. This class
746 * encapsulates those specific flags/modes.
748 private class SaveableState
{
751 * When true, cursor positions are relative to the scrolling region.
753 public boolean originMode
= false;
756 * The current editing X position.
758 public int cursorX
= 0;
761 * The current editing Y position.
763 public int cursorY
= 0;
766 * Which character set is currently selected in G0.
768 public CharacterSet g0Charset
= CharacterSet
.US
;
771 * Which character set is currently selected in G1.
773 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
776 * Which character set is currently selected in G2.
778 public CharacterSet g2Charset
= CharacterSet
.US
;
781 * Which character set is currently selected in G3.
783 public CharacterSet g3Charset
= CharacterSet
.US
;
786 * Which character set is currently selected in GR.
788 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
791 * The current drawing attributes.
793 public CellAttributes attr
;
798 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
803 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
808 public boolean lineWrap
= true;
813 public void reset() {
817 g0Charset
= CharacterSet
.US
;
818 g1Charset
= CharacterSet
.DRAWING
;
819 g2Charset
= CharacterSet
.US
;
820 g3Charset
= CharacterSet
.US
;
821 grCharset
= CharacterSet
.DRAWING
;
822 attr
= new CellAttributes();
823 glLockshift
= LockshiftMode
.NONE
;
824 grLockshift
= LockshiftMode
.NONE
;
829 * Copy attributes from another instance.
831 * @param that the other instance to match
833 public void setTo(final SaveableState that
) {
834 this.originMode
= that
.originMode
;
835 this.cursorX
= that
.cursorX
;
836 this.cursorY
= that
.cursorY
;
837 this.g0Charset
= that
.g0Charset
;
838 this.g1Charset
= that
.g1Charset
;
839 this.g2Charset
= that
.g2Charset
;
840 this.g3Charset
= that
.g3Charset
;
841 this.grCharset
= that
.grCharset
;
842 this.attr
= new CellAttributes();
843 this.attr
.setTo(that
.attr
);
844 this.glLockshift
= that
.glLockshift
;
845 this.grLockshift
= that
.grLockshift
;
846 this.lineWrap
= that
.lineWrap
;
850 * Public constructor.
852 public SaveableState() {
858 * The current terminal state.
860 private SaveableState currentState
;
863 * The last saved terminal state.
865 private SaveableState savedState
;
868 * Clear the CSI parameters and flags.
870 private void toGround() {
872 collectBuffer
= new StringBuilder(8);
873 scanState
= ScanState
.GROUND
;
877 * Reset the tab stops list.
879 private void resetTabStops() {
881 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
882 tabStops
.add(new Integer(i
* 8));
887 * Reset the emulation state.
889 private void reset() {
891 currentState
= new SaveableState();
892 savedState
= new SaveableState();
893 scanState
= ScanState
.GROUND
;
897 scrollRegionBottom
= height
- 1;
898 rightMargin
= width
- 1;
900 arrowKeyMode
= ArrowKeyMode
.ANSI
;
901 keypadMode
= KeypadMode
.Numeric
;
902 wrapLineFlag
= false;
903 if (displayListener
!= null) {
904 width
= displayListener
.getDisplayWidth();
905 height
= displayListener
.getDisplayHeight();
906 rightMargin
= width
- 1;
915 reverseVideo
= false;
917 cursorVisible
= true;
920 singleshift
= Singleshift
.NONE
;
922 printerControllerMode
= false;
925 mouseProtocol
= MouseProtocol
.OFF
;
926 mouseEncoding
= MouseEncoding
.X10
;
936 * Public constructor.
938 * @param type one of the DeviceType constants to select VT100, VT102,
940 * @param inputStream an InputStream connected to the remote side. For
941 * type == XTERM, inputStream is converted to a Reader with UTF-8
943 * @param outputStream an OutputStream connected to the remote user. For
944 * type == XTERM, outputStream is converted to a Writer with UTF-8
946 * @param displayListener a callback to the outer display, or null for
947 * default VT100 behavior
948 * @throws UnsupportedEncodingException if an exception is thrown when
949 * creating the InputStreamReader
951 public ECMA48(final DeviceType type
, final InputStream inputStream
,
952 final OutputStream outputStream
, final DisplayListener displayListener
)
953 throws UnsupportedEncodingException
{
955 assert (inputStream
!= null);
956 assert (outputStream
!= null);
958 csiParams
= new ArrayList
<Integer
>();
959 tabStops
= new ArrayList
<Integer
>();
960 scrollback
= new LinkedList
<DisplayLine
>();
961 display
= new LinkedList
<DisplayLine
>();
964 if (inputStream
instanceof TimeoutInputStream
) {
965 this.inputStream
= (TimeoutInputStream
)inputStream
;
967 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
969 if (type
== DeviceType
.XTERM
) {
970 this.input
= new InputStreamReader(this.inputStream
, "UTF-8");
971 this.output
= new OutputStreamWriter(new
972 BufferedOutputStream(outputStream
), "UTF-8");
973 this.outputStream
= null;
976 this.outputStream
= new BufferedOutputStream(outputStream
);
978 this.displayListener
= displayListener
;
981 for (int i
= 0; i
< height
; i
++) {
982 display
.add(new DisplayLine(currentState
.attr
));
985 // Spin up the input reader
986 readerThread
= new Thread(this);
987 readerThread
.start();
991 * Append a new line to the bottom of the display, adding lines off the
992 * top to the scrollback buffer.
994 private void newDisplayLine() {
995 // Scroll the top line off into the scrollback buffer
996 scrollback
.add(display
.get(0));
998 DisplayLine line
= new DisplayLine(currentState
.attr
);
999 line
.setReverseColor(reverseVideo
);
1004 * Wraps the current line.
1006 private void wrapCurrentLine() {
1007 if (currentState
.cursorY
== height
- 1) {
1010 if (currentState
.cursorY
< height
- 1) {
1011 currentState
.cursorY
++;
1013 currentState
.cursorX
= 0;
1017 * Handle a carriage return.
1019 private void carriageReturn() {
1020 currentState
.cursorX
= 0;
1021 wrapLineFlag
= false;
1025 * Reverse the color of the visible display.
1027 private void invertDisplayColors() {
1028 for (DisplayLine line
: display
) {
1029 line
.setReverseColor(!line
.isReverseColor());
1034 * Handle a linefeed.
1036 private void linefeed() {
1038 if (currentState
.cursorY
< scrollRegionBottom
) {
1039 // Increment screen y
1040 currentState
.cursorY
++;
1043 // Screen y does not increment
1046 * Two cases: either we're inside a scrolling region or not. If
1047 * the scrolling region bottom is the bottom of the screen, then
1048 * push the top line into the buffer. Else scroll the scrolling
1051 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1053 // We're at the bottom of the scroll region, AND the scroll
1054 // region is the entire screen.
1060 // We're at the bottom of the scroll region, AND the scroll
1061 // region is NOT the entire screen.
1062 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1067 currentState
.cursorX
= 0;
1069 wrapLineFlag
= false;
1073 * Prints one character to the display buffer.
1075 * @param ch character to display
1077 private void printCharacter(final char ch
) {
1078 int rightMargin
= this.rightMargin
;
1080 // Check if we have double-width, and if so chop at 40/66 instead of
1082 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1083 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1086 // Check the unusually-complicated line wrapping conditions...
1087 if (currentState
.cursorX
== rightMargin
) {
1089 if (currentState
.lineWrap
== true) {
1091 * This case happens when: the cursor was already on the
1092 * right margin (either through printing or by an explicit
1093 * placement command), and a character was printed.
1095 * The line wraps only when a new character arrives AND the
1096 * cursor is already on the right margin AND has placed a
1097 * character in its cell. Easier to see than to explain.
1099 if (wrapLineFlag
== false) {
1101 * This block marks the case that we are in the margin
1102 * and the first character has been received and printed.
1104 wrapLineFlag
= true;
1107 * This block marks the case that we are in the margin
1108 * and the second character has been received and
1111 wrapLineFlag
= false;
1115 } else if (currentState
.cursorX
<= rightMargin
) {
1117 * This is the normal case: a character came in and was printed
1118 * to the left of the right margin column.
1121 // Turn off VT100 special-case flag
1122 wrapLineFlag
= false;
1125 // "Print" the character
1126 Cell newCell
= new Cell(ch
);
1127 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1128 newCellAttributes
.setTo(currentState
.attr
);
1129 DisplayLine line
= display
.get(currentState
.cursorY
);
1130 // Insert mode special case
1131 if (insertMode
== true) {
1132 line
.insert(currentState
.cursorX
, newCell
);
1134 // Replace an existing character
1135 line
.replace(currentState
.cursorX
, newCell
);
1138 // Increment horizontal
1139 if (wrapLineFlag
== false) {
1140 currentState
.cursorX
++;
1141 if (currentState
.cursorX
> rightMargin
) {
1142 currentState
.cursorX
--;
1148 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1149 * send to the remote side.
1151 * @param mouse mouse event received from the local user
1153 public void mouse(final TMouseEvent mouse
) {
1156 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1157 mouseProtocol, mouseEncoding, mouse);
1160 if (mouseEncoding
== MouseEncoding
.X10
) {
1161 // We will support X10 but only for (160,94) and smaller.
1162 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1167 switch (mouseProtocol
) {
1174 // Only report button presses
1175 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1181 // Only report button presses and releases
1182 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1183 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1191 * Only report button presses, button releases, and motions that
1192 * have a button down (i.e. drag-and-drop).
1194 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1195 if (!mouse
.isMouse1()
1196 && !mouse
.isMouse2()
1197 && !mouse
.isMouse3()
1198 && !mouse
.isMouseWheelUp()
1199 && !mouse
.isMouseWheelDown()
1207 // Report everything
1211 // Now encode the event
1212 StringBuilder sb
= new StringBuilder(6);
1213 if (mouseEncoding
== MouseEncoding
.SGR
) {
1214 sb
.append((char) 0x1B);
1217 if (mouse
.isMouse1()) {
1218 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1223 } else if (mouse
.isMouse2()) {
1224 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1229 } else if (mouse
.isMouse3()) {
1230 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1235 } else if (mouse
.isMouseWheelUp()) {
1237 } else if (mouse
.isMouseWheelDown()) {
1240 // This is motion with no buttons down.
1244 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1247 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1254 // X10 and UTF8 encodings
1255 sb
.append((char) 0x1B);
1258 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1259 sb
.append((char) (0x03 + 32));
1260 } else if (mouse
.isMouse1()) {
1261 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1262 sb
.append((char) (0x00 + 32 + 32));
1264 sb
.append((char) (0x00 + 32));
1266 } else if (mouse
.isMouse2()) {
1267 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1268 sb
.append((char) (0x01 + 32 + 32));
1270 sb
.append((char) (0x01 + 32));
1272 } else if (mouse
.isMouse3()) {
1273 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1274 sb
.append((char) (0x02 + 32 + 32));
1276 sb
.append((char) (0x02 + 32));
1278 } else if (mouse
.isMouseWheelUp()) {
1279 sb
.append((char) (0x04 + 64));
1280 } else if (mouse
.isMouseWheelDown()) {
1281 sb
.append((char) (0x05 + 64));
1283 // This is motion with no buttons down.
1284 sb
.append((char) (0x03 + 32));
1287 sb
.append((char) (mouse
.getX() + 33));
1288 sb
.append((char) (mouse
.getY() + 33));
1291 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1292 writeRemote(sb
.toString());
1296 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1297 * send to the remote side.
1299 * @param keypress keypress received from the local user
1301 public void keypress(final TKeypress keypress
) {
1302 writeRemote(keypressToString(keypress
));
1306 * Build one of the complex xterm keystroke sequences, storing the result in
1307 * xterm_keystroke_buffer.
1309 * @param ss3 the prefix to use based on VT100 state.
1310 * @param first the first character, usually a number.
1311 * @param first the last character, one of the following: ~ A B C D F H
1312 * @param ctrl whether or not ctrl is down
1313 * @param alt whether or not alt is down
1314 * @param shift whether or not shift is down
1315 * @return the buffer with the full key sequence
1317 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1318 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1320 StringBuilder sb
= new StringBuilder(ss3
);
1321 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1325 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1327 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1329 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1331 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1333 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1335 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1337 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1342 return sb
.toString();
1346 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1348 * @param keypress keypress received from the local user
1349 * @return string to transmit to the remote side
1351 private String
keypressToString(final TKeypress keypress
) {
1353 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1355 * If this is a control character, process it like it came from
1358 if (keypress
.getChar() < 0x20) {
1359 handleControlChar(keypress
.getChar());
1361 // Local echo for everything else
1362 printCharacter(keypress
.getChar());
1366 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1371 // Handle control characters
1372 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1373 StringBuilder sb
= new StringBuilder();
1374 char ch
= keypress
.getChar();
1377 return sb
.toString();
1380 // Handle alt characters
1381 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1382 StringBuilder sb
= new StringBuilder("\033");
1383 char ch
= keypress
.getChar();
1385 return sb
.toString();
1388 if (keypress
.equals(kbBackspace
)) {
1401 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1404 switch (arrowKeyMode
) {
1406 return xtermBuildKeySequence("\033[", '1', 'D',
1407 keypress
.isCtrl(), keypress
.isAlt(),
1408 keypress
.isShift());
1410 return xtermBuildKeySequence("\033", '1', 'D',
1411 keypress
.isCtrl(), keypress
.isAlt(),
1412 keypress
.isShift());
1414 return xtermBuildKeySequence("\033O", '1', 'D',
1415 keypress
.isCtrl(), keypress
.isAlt(),
1416 keypress
.isShift());
1419 switch (arrowKeyMode
) {
1430 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1433 switch (arrowKeyMode
) {
1435 return xtermBuildKeySequence("\033[", '1', 'C',
1436 keypress
.isCtrl(), keypress
.isAlt(),
1437 keypress
.isShift());
1439 return xtermBuildKeySequence("\033", '1', 'C',
1440 keypress
.isCtrl(), keypress
.isAlt(),
1441 keypress
.isShift());
1443 return xtermBuildKeySequence("\033O", '1', 'C',
1444 keypress
.isCtrl(), keypress
.isAlt(),
1445 keypress
.isShift());
1448 switch (arrowKeyMode
) {
1459 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1462 switch (arrowKeyMode
) {
1464 return xtermBuildKeySequence("\033[", '1', 'A',
1465 keypress
.isCtrl(), keypress
.isAlt(),
1466 keypress
.isShift());
1468 return xtermBuildKeySequence("\033", '1', 'A',
1469 keypress
.isCtrl(), keypress
.isAlt(),
1470 keypress
.isShift());
1472 return xtermBuildKeySequence("\033O", '1', 'A',
1473 keypress
.isCtrl(), keypress
.isAlt(),
1474 keypress
.isShift());
1477 switch (arrowKeyMode
) {
1488 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1491 switch (arrowKeyMode
) {
1493 return xtermBuildKeySequence("\033[", '1', 'B',
1494 keypress
.isCtrl(), keypress
.isAlt(),
1495 keypress
.isShift());
1497 return xtermBuildKeySequence("\033", '1', 'B',
1498 keypress
.isCtrl(), keypress
.isAlt(),
1499 keypress
.isShift());
1501 return xtermBuildKeySequence("\033O", '1', 'B',
1502 keypress
.isCtrl(), keypress
.isAlt(),
1503 keypress
.isShift());
1506 switch (arrowKeyMode
) {
1517 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1520 switch (arrowKeyMode
) {
1522 return xtermBuildKeySequence("\033[", '1', 'H',
1523 keypress
.isCtrl(), keypress
.isAlt(),
1524 keypress
.isShift());
1526 return xtermBuildKeySequence("\033", '1', 'H',
1527 keypress
.isCtrl(), keypress
.isAlt(),
1528 keypress
.isShift());
1530 return xtermBuildKeySequence("\033O", '1', 'H',
1531 keypress
.isCtrl(), keypress
.isAlt(),
1532 keypress
.isShift());
1535 switch (arrowKeyMode
) {
1546 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1549 switch (arrowKeyMode
) {
1551 return xtermBuildKeySequence("\033[", '1', 'F',
1552 keypress
.isCtrl(), keypress
.isAlt(),
1553 keypress
.isShift());
1555 return xtermBuildKeySequence("\033", '1', 'F',
1556 keypress
.isCtrl(), keypress
.isAlt(),
1557 keypress
.isShift());
1559 return xtermBuildKeySequence("\033O", '1', 'F',
1560 keypress
.isCtrl(), keypress
.isAlt(),
1561 keypress
.isShift());
1564 switch (arrowKeyMode
) {
1575 if (keypress
.equals(kbF1
)) {
1583 if (keypress
.equals(kbF2
)) {
1591 if (keypress
.equals(kbF3
)) {
1599 if (keypress
.equals(kbF4
)) {
1607 if (keypress
.equals(kbF5
)) {
1620 if (keypress
.equals(kbF6
)) {
1633 if (keypress
.equals(kbF7
)) {
1646 if (keypress
.equals(kbF8
)) {
1659 if (keypress
.equals(kbF9
)) {
1672 if (keypress
.equals(kbF10
)) {
1685 if (keypress
.equals(kbF11
)) {
1689 if (keypress
.equals(kbF12
)) {
1693 if (keypress
.equals(kbShiftF1
)) {
1698 if (type
== DeviceType
.XTERM
) {
1704 if (keypress
.equals(kbShiftF2
)) {
1709 if (type
== DeviceType
.XTERM
) {
1715 if (keypress
.equals(kbShiftF3
)) {
1720 if (type
== DeviceType
.XTERM
) {
1726 if (keypress
.equals(kbShiftF4
)) {
1731 if (type
== DeviceType
.XTERM
) {
1737 if (keypress
.equals(kbShiftF5
)) {
1739 return "\033[15;2~";
1742 if (keypress
.equals(kbShiftF6
)) {
1744 return "\033[17;2~";
1747 if (keypress
.equals(kbShiftF7
)) {
1749 return "\033[18;2~";
1752 if (keypress
.equals(kbShiftF8
)) {
1754 return "\033[19;2~";
1757 if (keypress
.equals(kbShiftF9
)) {
1759 return "\033[20;2~";
1762 if (keypress
.equals(kbShiftF10
)) {
1764 return "\033[21;2~";
1767 if (keypress
.equals(kbShiftF11
)) {
1769 return "\033[23;2~";
1772 if (keypress
.equals(kbShiftF12
)) {
1774 return "\033[24;2~";
1777 if (keypress
.equals(kbCtrlF1
)) {
1782 if (type
== DeviceType
.XTERM
) {
1788 if (keypress
.equals(kbCtrlF2
)) {
1793 if (type
== DeviceType
.XTERM
) {
1799 if (keypress
.equals(kbCtrlF3
)) {
1804 if (type
== DeviceType
.XTERM
) {
1810 if (keypress
.equals(kbCtrlF4
)) {
1815 if (type
== DeviceType
.XTERM
) {
1821 if (keypress
.equals(kbCtrlF5
)) {
1823 return "\033[15;5~";
1826 if (keypress
.equals(kbCtrlF6
)) {
1828 return "\033[17;5~";
1831 if (keypress
.equals(kbCtrlF7
)) {
1833 return "\033[18;5~";
1836 if (keypress
.equals(kbCtrlF8
)) {
1838 return "\033[19;5~";
1841 if (keypress
.equals(kbCtrlF9
)) {
1843 return "\033[20;5~";
1846 if (keypress
.equals(kbCtrlF10
)) {
1848 return "\033[21;5~";
1851 if (keypress
.equals(kbCtrlF11
)) {
1853 return "\033[23;5~";
1856 if (keypress
.equals(kbCtrlF12
)) {
1858 return "\033[24;5~";
1861 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
1864 return xtermBuildKeySequence("\033[", '5', '~',
1865 keypress
.isCtrl(), keypress
.isAlt(),
1866 keypress
.isShift());
1872 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
1875 return xtermBuildKeySequence("\033[", '6', '~',
1876 keypress
.isCtrl(), keypress
.isAlt(),
1877 keypress
.isShift());
1883 if (keypress
.equalsWithoutModifiers(kbIns
)) {
1886 return xtermBuildKeySequence("\033[", '2', '~',
1887 keypress
.isCtrl(), keypress
.isAlt(),
1888 keypress
.isShift());
1894 if (keypress
.equalsWithoutModifiers(kbDel
)) {
1897 return xtermBuildKeySequence("\033[", '3', '~',
1898 keypress
.isCtrl(), keypress
.isAlt(),
1899 keypress
.isShift());
1901 // Delete sends real delete for VTxxx
1906 if (keypress
.equals(kbEnter
)) {
1910 if (keypress
.equals(kbEsc
)) {
1914 if (keypress
.equals(kbAltEsc
)) {
1918 if (keypress
.equals(kbTab
)) {
1922 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
1923 (keypress
.equals(kbShiftTab
))
1933 // Non-alt, non-ctrl characters
1934 if (!keypress
.isFnKey()) {
1935 StringBuilder sb
= new StringBuilder();
1936 sb
.append(keypress
.getChar());
1937 return sb
.toString();
1943 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1946 * @param ch 8-bit character from the remote side
1947 * @param charsetGl character set defined for GL
1948 * @param charsetGr character set defined for GR
1949 * @return character to display on the screen
1951 private char mapCharacterCharset(final char ch
,
1952 final CharacterSet charsetGl
,
1953 final CharacterSet charsetGr
) {
1955 int lookupChar
= ch
;
1956 CharacterSet lookupCharset
= charsetGl
;
1959 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
1960 lookupCharset
= charsetGr
;
1964 switch (lookupCharset
) {
1967 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
1970 return DECCharacterSets
.UK
[lookupChar
];
1973 return DECCharacterSets
.US_ASCII
[lookupChar
];
1976 return DECCharacterSets
.NL
[lookupChar
];
1979 return DECCharacterSets
.FI
[lookupChar
];
1982 return DECCharacterSets
.FR
[lookupChar
];
1985 return DECCharacterSets
.FR_CA
[lookupChar
];
1988 return DECCharacterSets
.DE
[lookupChar
];
1991 return DECCharacterSets
.IT
[lookupChar
];
1994 return DECCharacterSets
.NO
[lookupChar
];
1997 return DECCharacterSets
.ES
[lookupChar
];
2000 return DECCharacterSets
.SV
[lookupChar
];
2003 return DECCharacterSets
.SWISS
[lookupChar
];
2005 case DEC_SUPPLEMENTAL
:
2006 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2009 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2012 return DECCharacterSets
.US_ASCII
[lookupChar
];
2015 return DECCharacterSets
.US_ASCII
[lookupChar
];
2018 throw new IllegalArgumentException("Invalid character set value: "
2024 * Map an 8-bit byte into a printable character.
2026 * @param ch either 8-bit or Unicode character from the remote side
2027 * @return character to display on the screen
2029 private char mapCharacter(final char ch
) {
2031 // Unicode character, just return it
2035 CharacterSet charsetGl
= currentState
.g0Charset
;
2036 CharacterSet charsetGr
= currentState
.grCharset
;
2038 if (vt52Mode
== true) {
2039 if (shiftOut
== true) {
2040 // Shifted out character, pull from VT52 graphics
2041 charsetGl
= currentState
.g1Charset
;
2042 charsetGr
= CharacterSet
.US
;
2045 charsetGl
= currentState
.g0Charset
;
2046 charsetGr
= CharacterSet
.US
;
2049 // Pull the character
2050 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2054 if (shiftOut
== true) {
2055 // Shifted out character, pull from G1
2056 charsetGl
= currentState
.g1Charset
;
2057 charsetGr
= currentState
.grCharset
;
2059 // Pull the character
2060 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2064 if (singleshift
== Singleshift
.SS2
) {
2066 singleshift
= Singleshift
.NONE
;
2068 // Shifted out character, pull from G2
2069 charsetGl
= currentState
.g2Charset
;
2070 charsetGr
= currentState
.grCharset
;
2074 if (singleshift
== Singleshift
.SS3
) {
2076 singleshift
= Singleshift
.NONE
;
2078 // Shifted out character, pull from G3
2079 charsetGl
= currentState
.g3Charset
;
2080 charsetGr
= currentState
.grCharset
;
2083 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2084 // Check for locking shift
2086 switch (currentState
.glLockshift
) {
2099 charsetGl
= currentState
.g2Charset
;
2104 charsetGl
= currentState
.g3Charset
;
2109 charsetGl
= currentState
.g0Charset
;
2113 switch (currentState
.grLockshift
) {
2123 charsetGr
= currentState
.g1Charset
;
2128 charsetGr
= currentState
.g2Charset
;
2133 charsetGr
= currentState
.g3Charset
;
2138 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2145 // Pull the character
2146 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2150 * Scroll the text within a scrolling region up n lines.
2152 * @param regionTop top row of the scrolling region
2153 * @param regionBottom bottom row of the scrolling region
2154 * @param n number of lines to scroll
2156 private void scrollingRegionScrollUp(final int regionTop
,
2157 final int regionBottom
, final int n
) {
2159 if (regionTop
>= regionBottom
) {
2163 // Sanity check: see if there will be any characters left after the
2165 if (regionBottom
+ 1 - regionTop
<= n
) {
2166 // There won't be anything left in the region, so just call
2167 // eraseScreen() and return.
2168 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2172 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2173 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2174 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2176 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2177 - remaining
, regionBottom
+ 1);
2178 display
= new LinkedList
<DisplayLine
>(displayTop
);
2179 display
.addAll(displayMiddle
);
2180 for (int i
= 0; i
< n
; i
++) {
2181 DisplayLine line
= new DisplayLine(currentState
.attr
);
2182 line
.setReverseColor(reverseVideo
);
2185 display
.addAll(displayBottom
);
2187 assert (display
.size() == height
);
2191 * Scroll the text within a scrolling region down n lines.
2193 * @param regionTop top row of the scrolling region
2194 * @param regionBottom bottom row of the scrolling region
2195 * @param n number of lines to scroll
2197 private void scrollingRegionScrollDown(final int regionTop
,
2198 final int regionBottom
, final int n
) {
2200 if (regionTop
>= regionBottom
) {
2204 // Sanity check: see if there will be any characters left after the
2206 if (regionBottom
+ 1 - regionTop
<= n
) {
2207 // There won't be anything left in the region, so just call
2208 // eraseScreen() and return.
2209 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2213 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2214 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2215 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2217 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2218 regionTop
+ remaining
);
2219 display
= new LinkedList
<DisplayLine
>(displayTop
);
2220 for (int i
= 0; i
< n
; i
++) {
2221 DisplayLine line
= new DisplayLine(currentState
.attr
);
2222 line
.setReverseColor(reverseVideo
);
2225 display
.addAll(displayMiddle
);
2226 display
.addAll(displayBottom
);
2228 assert (display
.size() == height
);
2232 * Process a control character.
2234 * @param ch 8-bit character from the remote side
2236 private void handleControlChar(final char ch
) {
2237 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2248 // Transmit the answerback message.
2259 cursorLeft(1, false);
2264 advanceToNextTabStop();
2290 currentState
.glLockshift
= LockshiftMode
.NONE
;
2296 currentState
.glLockshift
= LockshiftMode
.NONE
;
2321 singleshift
= Singleshift
.SS2
;
2326 singleshift
= Singleshift
.SS3
;
2336 * Advance the cursor to the next tab stop.
2338 private void advanceToNextTabStop() {
2339 if (tabStops
.size() == 0) {
2340 // Go to the rightmost column
2341 cursorRight(rightMargin
- currentState
.cursorX
, false);
2344 for (Integer stop
: tabStops
) {
2345 if (stop
> currentState
.cursorX
) {
2346 cursorRight(stop
- currentState
.cursorX
, false);
2351 * We got here, meaning there isn't a tab stop beyond the current
2352 * cursor position. Place the cursor of the right-most edge of the
2355 cursorRight(rightMargin
- currentState
.cursorX
, false);
2359 * Save a character into the collect buffer.
2361 * @param ch character to save
2363 private void collect(final char ch
) {
2364 collectBuffer
.append(ch
);
2368 * Save a byte into the CSI parameters buffer.
2370 * @param ch byte to save
2372 private void param(final byte ch
) {
2373 if (csiParams
.size() == 0) {
2374 csiParams
.add(new Integer(0));
2376 Integer x
= csiParams
.get(csiParams
.size() - 1);
2377 if ((ch
>= '0') && (ch
<= '9')) {
2380 csiParams
.set(csiParams
.size() - 1, x
);
2384 csiParams
.add(new Integer(0));
2389 * Get a CSI parameter value, with a default.
2391 * @param position parameter index. 0 is the first parameter.
2392 * @param defaultValue value to use if csiParams[position] doesn't exist
2393 * @return parameter value
2395 private int getCsiParam(final int position
, final int defaultValue
) {
2396 if (csiParams
.size() < position
+ 1) {
2397 return defaultValue
;
2399 return csiParams
.get(position
).intValue();
2403 * Get a CSI parameter value, clamped to within min/max.
2405 * @param position parameter index. 0 is the first parameter.
2406 * @param defaultValue value to use if csiParams[position] doesn't exist
2407 * @param minValue minimum value inclusive
2408 * @param maxValue maximum value inclusive
2409 * @return parameter value
2411 private int getCsiParam(final int position
, final int defaultValue
,
2412 final int minValue
, final int maxValue
) {
2414 assert (minValue
<= maxValue
);
2415 int value
= getCsiParam(position
, defaultValue
);
2416 if (value
< minValue
) {
2419 if (value
> maxValue
) {
2426 * Set or unset a toggle.
2428 * @param value true for set ('h'), false for reset ('l')
2430 private void setToggle(final boolean value
) {
2431 boolean decPrivateModeFlag
= false;
2432 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2433 if (collectBuffer
.charAt(i
) == '?') {
2434 decPrivateModeFlag
= true;
2439 for (Integer i
: csiParams
) {
2444 if (decPrivateModeFlag
== true) {
2446 if (value
== true) {
2447 // Use application arrow keys
2448 arrowKeyMode
= ArrowKeyMode
.VT100
;
2450 // Use ANSI arrow keys
2451 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2456 if (decPrivateModeFlag
== true) {
2457 if (value
== false) {
2461 arrowKeyMode
= ArrowKeyMode
.VT52
;
2464 * From the VT102 docs: "You use ANSI mode to select
2465 * most terminal features; the terminal uses the same
2466 * features when it switches to VT52 mode. You
2467 * cannot, however, change most of these features in
2470 * In other words, do not reset any other attributes
2471 * when switching between VT52 submode and ANSI.
2473 * HOWEVER, the real vt100 does switch the character
2474 * set according to Usenet.
2476 currentState
.g0Charset
= CharacterSet
.US
;
2477 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2480 if ((type
== DeviceType
.VT220
)
2481 || (type
== DeviceType
.XTERM
)) {
2483 // VT52 mode is explicitly 7-bit
2485 singleshift
= Singleshift
.NONE
;
2490 if (value
== true) {
2491 // Turn off keyboard
2500 if (decPrivateModeFlag
== true) {
2502 if (value
== true) {
2509 if ((displayListener
!= null)
2510 && (type
== DeviceType
.XTERM
)
2512 // For xterms, reset to the actual width, not 80
2514 width
= displayListener
.getDisplayWidth();
2515 rightMargin
= width
- 1;
2518 width
= rightMargin
+ 1;
2521 // Entire screen is cleared, and scrolling region is
2523 eraseScreen(0, 0, height
- 1, width
- 1, false);
2524 scrollRegionTop
= 0;
2525 scrollRegionBottom
= height
- 1;
2526 // Also home the cursor
2527 cursorPosition(0, 0);
2531 if (decPrivateModeFlag
== true) {
2533 if (value
== true) {
2542 if (value
== true) {
2550 if (decPrivateModeFlag
== true) {
2552 if (value
== true) {
2554 * Set selects reverse screen, a white screen
2555 * background with black characters.
2557 if (reverseVideo
!= true) {
2559 * If in normal video, switch it back
2561 invertDisplayColors();
2563 reverseVideo
= true;
2566 * Reset selects normal screen, a black screen
2567 * background with white characters.
2569 if (reverseVideo
== true) {
2571 * If in reverse video already, switch it back
2573 invertDisplayColors();
2575 reverseVideo
= false;
2580 if (decPrivateModeFlag
== true) {
2582 if (value
== true) {
2583 // Origin is relative to scroll region cursor.
2584 // Cursor can NEVER leave scrolling region.
2585 currentState
.originMode
= true;
2586 cursorPosition(0, 0);
2588 // Origin is absolute to entire screen. Cursor can
2589 // leave the scrolling region via cup() and hvp().
2590 currentState
.originMode
= false;
2591 cursorPosition(0, 0);
2596 if (decPrivateModeFlag
== true) {
2598 if (value
== true) {
2600 currentState
.lineWrap
= true;
2602 // Turn linewrap off
2603 currentState
.lineWrap
= false;
2608 if (decPrivateModeFlag
== true) {
2610 if (value
== true) {
2611 // Keyboard auto-repeat on
2614 // Keyboard auto-repeat off
2620 if (decPrivateModeFlag
== false) {
2622 if (value
== true) {
2632 if (decPrivateModeFlag
== true) {
2638 if (decPrivateModeFlag
== true) {
2644 if (decPrivateModeFlag
== false) {
2646 if (value
== true) {
2648 * Set causes a received linefeed, form feed, or
2649 * vertical tab to move cursor to first column of
2650 * next line. RETURN transmits both a carriage return
2651 * and linefeed. This selection is also called new
2657 * Reset causes a received linefeed, form feed, or
2658 * vertical tab to move cursor to next line in
2659 * current column. RETURN transmits a carriage
2662 newLineMode
= false;
2668 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2669 if (decPrivateModeFlag
== true) {
2671 if (value
== true) {
2673 cursorVisible
= true;
2676 cursorVisible
= false;
2683 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2684 if (decPrivateModeFlag
== true) {
2686 if (value
== true) {
2687 // Select national mode NRC
2690 // Select multi-national mode
2699 if ((type
== DeviceType
.XTERM
)
2700 && (decPrivateModeFlag
== true)
2702 // Mouse: normal tracking mode
2703 if (value
== true) {
2704 mouseProtocol
= MouseProtocol
.NORMAL
;
2706 mouseProtocol
= MouseProtocol
.OFF
;
2712 if ((type
== DeviceType
.XTERM
)
2713 && (decPrivateModeFlag
== true)
2715 // Mouse: normal tracking mode
2716 if (value
== true) {
2717 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2719 mouseProtocol
= MouseProtocol
.OFF
;
2725 if ((type
== DeviceType
.XTERM
)
2726 && (decPrivateModeFlag
== true)
2728 // Mouse: Any-event tracking mode
2729 if (value
== true) {
2730 mouseProtocol
= MouseProtocol
.ANYEVENT
;
2732 mouseProtocol
= MouseProtocol
.OFF
;
2738 if ((type
== DeviceType
.XTERM
)
2739 && (decPrivateModeFlag
== true)
2741 // Mouse: UTF-8 coordinates
2742 if (value
== true) {
2743 mouseEncoding
= MouseEncoding
.UTF8
;
2745 mouseEncoding
= MouseEncoding
.X10
;
2751 if ((type
== DeviceType
.XTERM
)
2752 && (decPrivateModeFlag
== true)
2754 // Mouse: SGR coordinates
2755 if (value
== true) {
2756 mouseEncoding
= MouseEncoding
.SGR
;
2758 mouseEncoding
= MouseEncoding
.X10
;
2771 * DECSC - Save cursor.
2773 private void decsc() {
2774 savedState
.setTo(currentState
);
2778 * DECRC - Restore cursor.
2780 private void decrc() {
2781 currentState
.setTo(savedState
);
2787 private void ind() {
2788 // Move the cursor and scroll if necessary. If at the bottom line
2789 // already, a scroll up is supposed to be performed.
2790 if (currentState
.cursorY
== scrollRegionBottom
) {
2791 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2793 cursorDown(1, true);
2797 * RI - Reverse index.
2800 // Move the cursor and scroll if necessary. If at the top line
2801 // already, a scroll down is supposed to be performed.
2802 if (currentState
.cursorY
== scrollRegionTop
) {
2803 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2811 private void nel() {
2812 // Move the cursor and scroll if necessary. If at the bottom line
2813 // already, a scroll up is supposed to be performed.
2814 if (currentState
.cursorY
== scrollRegionBottom
) {
2815 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2817 cursorDown(1, true);
2819 // Reset to the beginning of the next line
2820 currentState
.cursorX
= 0;
2824 * DECKPAM - Keypad application mode.
2826 private void deckpam() {
2827 keypadMode
= KeypadMode
.Application
;
2831 * DECKPNM - Keypad numeric mode.
2833 private void deckpnm() {
2834 keypadMode
= KeypadMode
.Numeric
;
2840 * @param n number of spaces to move
2841 * @param honorScrollRegion if true, then do nothing if the cursor is
2842 * outside the scrolling region
2844 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2848 * Special case: if a user moves the cursor from the right margin, we
2849 * have to reset the VT100 right margin flag.
2852 wrapLineFlag
= false;
2855 for (int i
= 0; i
< n
; i
++) {
2856 if (honorScrollRegion
== true) {
2857 // Honor the scrolling region
2858 if ((currentState
.cursorY
< scrollRegionTop
)
2859 || (currentState
.cursorY
> scrollRegionBottom
)
2861 // Outside region, do nothing
2864 // Inside region, go up
2865 top
= scrollRegionTop
;
2867 // Non-scrolling case
2871 if (currentState
.cursorY
> top
) {
2872 currentState
.cursorY
--;
2878 * Move down n spaces.
2880 * @param n number of spaces to move
2881 * @param honorScrollRegion if true, then do nothing if the cursor is
2882 * outside the scrolling region
2884 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
2888 * Special case: if a user moves the cursor from the right margin, we
2889 * have to reset the VT100 right margin flag.
2892 wrapLineFlag
= false;
2895 for (int i
= 0; i
< n
; i
++) {
2897 if (honorScrollRegion
== true) {
2898 // Honor the scrolling region
2899 if (currentState
.cursorY
> scrollRegionBottom
) {
2900 // Outside region, do nothing
2903 // Inside region, go down
2904 bottom
= scrollRegionBottom
;
2906 // Non-scrolling case
2907 bottom
= height
- 1;
2910 if (currentState
.cursorY
< bottom
) {
2911 currentState
.cursorY
++;
2917 * Move left n spaces.
2919 * @param n number of spaces to move
2920 * @param honorScrollRegion if true, then do nothing if the cursor is
2921 * outside the scrolling region
2923 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
2925 * Special case: if a user moves the cursor from the right margin, we
2926 * have to reset the VT100 right margin flag.
2929 wrapLineFlag
= false;
2932 for (int i
= 0; i
< n
; i
++) {
2933 if (honorScrollRegion
== true) {
2934 // Honor the scrolling region
2935 if ((currentState
.cursorY
< scrollRegionTop
)
2936 || (currentState
.cursorY
> scrollRegionBottom
)
2938 // Outside region, do nothing
2943 if (currentState
.cursorX
> 0) {
2944 currentState
.cursorX
--;
2950 * Move right n spaces.
2952 * @param n number of spaces to move
2953 * @param honorScrollRegion if true, then do nothing if the cursor is
2954 * outside the scrolling region
2956 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
2957 int rightMargin
= this.rightMargin
;
2960 * Special case: if a user moves the cursor from the right margin, we
2961 * have to reset the VT100 right margin flag.
2964 wrapLineFlag
= false;
2967 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2968 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2971 for (int i
= 0; i
< n
; i
++) {
2972 if (honorScrollRegion
== true) {
2973 // Honor the scrolling region
2974 if ((currentState
.cursorY
< scrollRegionTop
)
2975 || (currentState
.cursorY
> scrollRegionBottom
)
2977 // Outside region, do nothing
2982 if (currentState
.cursorX
< rightMargin
) {
2983 currentState
.cursorX
++;
2989 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2991 * @param row row to move to
2992 * @param col column to move to
2994 private void cursorPosition(int row
, final int col
) {
2995 int rightMargin
= this.rightMargin
;
3000 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3001 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3004 // Set column number
3005 currentState
.cursorX
= col
;
3007 // Sanity check, bring column back to margin.
3008 if (currentState
.cursorX
> rightMargin
) {
3009 currentState
.cursorX
= rightMargin
;
3013 if (currentState
.originMode
== true) {
3014 row
+= scrollRegionTop
;
3016 if (currentState
.cursorY
< row
) {
3017 cursorDown(row
- currentState
.cursorY
, false);
3018 } else if (currentState
.cursorY
> row
) {
3019 cursorUp(currentState
.cursorY
- row
, false);
3022 wrapLineFlag
= false;
3026 * HTS - Horizontal tabulation set.
3028 private void hts() {
3029 for (Integer stop
: tabStops
) {
3030 if (stop
== currentState
.cursorX
) {
3031 // Already have a tab stop here
3036 // Append a tab stop to the end of the array and resort them
3037 tabStops
.add(currentState
.cursorX
);
3038 Collections
.sort(tabStops
);
3042 * DECSWL - Single-width line.
3044 private void decswl() {
3045 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3046 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3050 * DECDWL - Double-width line.
3052 private void decdwl() {
3053 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3054 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3058 * DECHDL - Double-height + double-width line.
3060 * @param topHalf if true, this sets the row to be the top half row of a
3063 private void dechdl(final boolean topHalf
) {
3064 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3065 if (topHalf
== true) {
3066 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3068 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3073 * DECALN - Screen alignment display.
3075 private void decaln() {
3076 Cell newCell
= new Cell();
3077 newCell
.setChar('E');
3078 for (DisplayLine line
: display
) {
3079 for (int i
= 0; i
< line
.length(); i
++) {
3080 line
.replace(i
, newCell
);
3086 * DECSCL - Compatibility level.
3088 private void decscl() {
3089 int i
= getCsiParam(0, 0);
3090 int j
= getCsiParam(1, 0);
3094 currentState
.g0Charset
= CharacterSet
.US
;
3095 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3097 } else if (i
== 62) {
3099 if ((j
== 0) || (j
== 2)) {
3101 } else if (j
== 1) {
3108 * CUD - Cursor down.
3110 private void cud() {
3111 cursorDown(getCsiParam(0, 1, 1, height
), true);
3115 * CUF - Cursor forward.
3117 private void cuf() {
3118 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3122 * CUB - Cursor backward.
3124 private void cub() {
3125 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3131 private void cuu() {
3132 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3136 * CUP - Cursor position.
3138 private void cup() {
3139 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3140 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3144 * CNL - Cursor down and to column 1.
3146 private void cnl() {
3147 cursorDown(getCsiParam(0, 1, 1, height
), true);
3149 cursorLeft(currentState
.cursorX
, true);
3153 * CPL - Cursor up and to column 1.
3155 private void cpl() {
3156 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3158 cursorLeft(currentState
.cursorX
, true);
3162 * CHA - Cursor to column # in current row.
3164 private void cha() {
3165 cursorPosition(currentState
.cursorY
,
3166 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3170 * VPA - Cursor to row #, same column.
3172 private void vpa() {
3173 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3174 currentState
.cursorX
);
3178 * ED - Erase in display.
3181 boolean honorProtected
= false;
3182 boolean decPrivateModeFlag
= false;
3184 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3185 if (collectBuffer
.charAt(i
) == '?') {
3186 decPrivateModeFlag
= true;
3191 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3192 && (decPrivateModeFlag
== true)
3194 honorProtected
= true;
3197 int i
= getCsiParam(0, 0);
3200 // Erase from here to end of screen
3201 if (currentState
.cursorY
< height
- 1) {
3202 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3205 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3206 } else if (i
== 1) {
3207 // Erase from beginning of screen to here
3208 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3210 eraseLine(0, currentState
.cursorX
, honorProtected
);
3211 } else if (i
== 2) {
3212 // Erase entire screen
3213 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3218 * EL - Erase in line.
3221 boolean honorProtected
= false;
3222 boolean decPrivateModeFlag
= false;
3224 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3225 if (collectBuffer
.charAt(i
) == '?') {
3226 decPrivateModeFlag
= true;
3231 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3232 && (decPrivateModeFlag
== true)
3234 honorProtected
= true;
3237 int i
= getCsiParam(0, 0);
3240 // Erase from here to end of line
3241 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3242 } else if (i
== 1) {
3243 // Erase from beginning of line to here
3244 eraseLine(0, currentState
.cursorX
, honorProtected
);
3245 } else if (i
== 2) {
3246 // Erase entire line
3247 eraseLine(0, width
- 1, honorProtected
);
3252 * ECH - Erase # of characters in current row.
3254 private void ech() {
3255 int i
= getCsiParam(0, 1, 1, width
);
3257 // Erase from here to i characters
3258 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3265 int i
= getCsiParam(0, 1);
3267 if ((currentState
.cursorY
>= scrollRegionTop
)
3268 && (currentState
.cursorY
<= scrollRegionBottom
)
3271 // I can get the same effect with a scroll-down
3272 scrollingRegionScrollDown(currentState
.cursorY
,
3273 scrollRegionBottom
, i
);
3278 * DCH - Delete char.
3280 private void dch() {
3281 int n
= getCsiParam(0, 1);
3282 DisplayLine line
= display
.get(currentState
.cursorY
);
3283 Cell blank
= new Cell();
3284 for (int i
= 0; i
< n
; i
++) {
3285 line
.delete(currentState
.cursorX
, blank
);
3290 * ICH - Insert blank char at cursor.
3292 private void ich() {
3293 int n
= getCsiParam(0, 1);
3294 DisplayLine line
= display
.get(currentState
.cursorY
);
3295 Cell blank
= new Cell();
3296 for (int i
= 0; i
< n
; i
++) {
3297 line
.insert(currentState
.cursorX
, blank
);
3305 int i
= getCsiParam(0, 1);
3307 if ((currentState
.cursorY
>= scrollRegionTop
)
3308 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3310 // I can get the same effect with a scroll-down
3311 scrollingRegionScrollUp(currentState
.cursorY
,
3312 scrollRegionBottom
, i
);
3317 * HVP - Horizontal and vertical position.
3319 private void hvp() {
3324 * REP - Repeat character.
3326 private void rep() {
3327 int n
= getCsiParam(0, 1);
3328 for (int i
= 0; i
< n
; i
++) {
3329 printCharacter(repCh
);
3337 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3338 getCsiParam(0, 1, 1, height
));
3345 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3346 getCsiParam(0, 1, 1, height
));
3350 * CBT - Go back X tab stops.
3352 private void cbt() {
3353 int tabsToMove
= getCsiParam(0, 1);
3356 for (int i
= 0; i
< tabsToMove
; i
++) {
3357 int j
= currentState
.cursorX
;
3358 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3359 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3367 j
= tabStops
.get(tabI
);
3369 cursorPosition(currentState
.cursorY
, j
);
3374 * CHT - Advance X tab stops.
3376 private void cht() {
3377 int n
= getCsiParam(0, 1);
3378 for (int i
= 0; i
< n
; i
++) {
3379 advanceToNextTabStop();
3384 * SGR - Select graphics rendition.
3386 private void sgr() {
3388 if (csiParams
.size() == 0) {
3389 currentState
.attr
.reset();
3393 for (Integer i
: csiParams
) {
3399 currentState
.attr
.reset();
3404 currentState
.attr
.setBold(true);
3409 currentState
.attr
.setUnderline(true);
3414 currentState
.attr
.setBlink(true);
3419 currentState
.attr
.setReverse(true);
3426 if (type
== DeviceType
.XTERM
) {
3440 if ((type
== DeviceType
.VT220
)
3441 || (type
== DeviceType
.XTERM
)) {
3447 currentState
.attr
.setBold(false);
3452 currentState
.attr
.setUnderline(false);
3457 currentState
.attr
.setBlink(false);
3462 currentState
.attr
.setReverse(false);
3470 // A true VT100/102/220 does not support color, however everyone
3471 // is used to their terminal emulator supporting color so we will
3472 // unconditionally support color for all DeviceType's.
3477 // Set black foreground
3478 currentState
.attr
.setForeColor(Color
.BLACK
);
3481 // Set red foreground
3482 currentState
.attr
.setForeColor(Color
.RED
);
3485 // Set green foreground
3486 currentState
.attr
.setForeColor(Color
.GREEN
);
3489 // Set yellow foreground
3490 currentState
.attr
.setForeColor(Color
.YELLOW
);
3493 // Set blue foreground
3494 currentState
.attr
.setForeColor(Color
.BLUE
);
3497 // Set magenta foreground
3498 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3501 // Set cyan foreground
3502 currentState
.attr
.setForeColor(Color
.CYAN
);
3505 // Set white foreground
3506 currentState
.attr
.setForeColor(Color
.WHITE
);
3509 if (type
== DeviceType
.XTERM
) {
3511 * Xterm supports T.416 / ISO-8613-3 codes to select
3512 * either an indexed color or an RGB value. (It also
3513 * permits these ISO-8613-3 SGR sequences to be separated
3514 * by colons rather than semicolons.)
3516 * We will not support any of these additional color
3517 * codes at this time:
3519 * 1. http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
3520 * has a detailed discussion of the current state of
3521 * RGB in various terminals, the point of which is
3522 * that none of them really do the same thing despite
3523 * all appearing to be "xterm".
3526 * https://bugs.kde.org/show_bug.cgi?id=107487#c3,
3527 * even supporting just the "indexed mode" of these
3528 * sequences (which could align easily with existing
3529 * SGR colors) is assumed to mean full support of
3530 * 24-bit RGB. So it is all or nothing.
3532 * Finally, these sequences break the assumptions of
3533 * standard ECMA-48 style parsers as pointed out at
3534 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
3535 * Therefore in order to keep a clean display, we cannot
3536 * parse anything else in this sequence.
3540 // Underscore on, default foreground color
3541 currentState
.attr
.setUnderline(true);
3542 currentState
.attr
.setForeColor(Color
.WHITE
);
3546 // Underscore off, default foreground color
3547 currentState
.attr
.setUnderline(false);
3548 currentState
.attr
.setForeColor(Color
.WHITE
);
3551 // Set black background
3552 currentState
.attr
.setBackColor(Color
.BLACK
);
3555 // Set red background
3556 currentState
.attr
.setBackColor(Color
.RED
);
3559 // Set green background
3560 currentState
.attr
.setBackColor(Color
.GREEN
);
3563 // Set yellow background
3564 currentState
.attr
.setBackColor(Color
.YELLOW
);
3567 // Set blue background
3568 currentState
.attr
.setBackColor(Color
.BLUE
);
3571 // Set magenta background
3572 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3575 // Set cyan background
3576 currentState
.attr
.setBackColor(Color
.CYAN
);
3579 // Set white background
3580 currentState
.attr
.setBackColor(Color
.WHITE
);
3583 if (type
== DeviceType
.XTERM
) {
3585 * Xterm supports T.416 / ISO-8613-3 codes to select
3586 * either an indexed color or an RGB value. (It also
3587 * permits these ISO-8613-3 SGR sequences to be separated
3588 * by colons rather than semicolons.)
3590 * We will not support this at this time. Also, in order
3591 * to keep a clean display, we cannot parse anything else
3598 // Default background
3599 currentState
.attr
.setBackColor(Color
.BLACK
);
3609 * DA - Device attributes.
3612 int extendedFlag
= 0;
3614 if (collectBuffer
.length() > 0) {
3615 String args
= collectBuffer
.substring(1);
3616 if (collectBuffer
.charAt(0) == '>') {
3618 if (collectBuffer
.length() >= 2) {
3619 i
= Integer
.parseInt(args
.toString());
3621 } else if (collectBuffer
.charAt(0) == '=') {
3623 if (collectBuffer
.length() >= 2) {
3624 i
= Integer
.parseInt(args
.toString());
3627 // Unknown code, bail out
3632 if ((i
!= 0) && (i
!= 1)) {
3636 if ((extendedFlag
== 0) && (i
== 0)) {
3637 // Send string directly to remote side
3638 writeRemote(deviceTypeResponse());
3642 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3644 if ((extendedFlag
== 1) && (i
== 0)) {
3646 * Request "What type of terminal are you, what is your
3647 * firmware version, and what hardware options do you have
3650 * Respond: "I am a VT220 (identification code of 1), my
3651 * firmware version is _____ (Pv), and I have _____ Po
3652 * options installed."
3658 if (s8c1t
== true) {
3659 writeRemote("\u009b>1;10;0c");
3661 writeRemote("\033[>1;10;0c");
3667 if ((extendedFlag
== 2) && (i
== 0)) {
3670 * Request "What is your unit ID?"
3672 * Respond: "I was manufactured at site 00 and have a unique ID
3676 writeRemote("\033P!|00010203\033\\");
3681 * DECSTBM - Set top and bottom margins.
3683 private void decstbm() {
3684 boolean decPrivateModeFlag
= false;
3686 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3687 if (collectBuffer
.charAt(i
) == '?') {
3688 decPrivateModeFlag
= true;
3692 if (decPrivateModeFlag
) {
3693 // This could be restore DEC private mode values.
3697 int top
= getCsiParam(0, 1, 1, height
) - 1;
3698 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3703 scrollRegionTop
= top
;
3704 scrollRegionBottom
= bottom
;
3707 cursorPosition(0, 0);
3712 * DECREQTPARM - Request terminal parameters.
3714 private void decreqtparm() {
3715 int i
= getCsiParam(0, 0);
3717 if ((i
!= 0) && (i
!= 1)) {
3724 * Request terminal parameters.
3728 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3729 * (CLoCk MULtiplier = 1, STP option flags = 0)
3733 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3736 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3738 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3744 * DECSCA - Select Character Attributes.
3746 private void decsca() {
3747 int i
= getCsiParam(0, 0);
3749 if ((i
== 0) || (i
== 2)) {
3751 currentState
.attr
.setProtect(false);
3755 currentState
.attr
.setProtect(true);
3760 * DECSTR - Soft Terminal Reset.
3762 private void decstr() {
3763 // Do exactly like RIS - Reset to initial state
3765 // Do I clear screen too? I think so...
3766 eraseScreen(0, 0, height
- 1, width
- 1, false);
3767 cursorPosition(0, 0);
3771 * DSR - Device status report.
3773 private void dsr() {
3774 boolean decPrivateModeFlag
= false;
3775 int row
= currentState
.cursorY
;
3777 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3778 if (collectBuffer
.charAt(i
) == '?') {
3779 decPrivateModeFlag
= true;
3784 int i
= getCsiParam(0, 0);
3789 // Request status report. Respond with "OK, no malfunction."
3791 // Send string directly to remote side
3792 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3795 writeRemote("\u009b0n");
3797 writeRemote("\033[0n");
3802 // Request cursor position. Respond with current position.
3803 if (currentState
.originMode
== true) {
3804 row
-= scrollRegionTop
;
3807 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3810 str
= String
.format("\u009b%d;%dR", row
+ 1,
3811 currentState
.cursorX
+ 1);
3813 str
= String
.format("\033[%d;%dR", row
+ 1,
3814 currentState
.cursorX
+ 1);
3817 // Send string directly to remote side
3822 if (decPrivateModeFlag
== true) {
3824 // Request printer status report. Respond with "Printer not
3827 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3828 && (s8c1t
== true)) {
3829 writeRemote("\u009b?13n");
3831 writeRemote("\033[?13n");
3837 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3838 && (decPrivateModeFlag
== true)
3841 // Request user-defined keys are locked or unlocked. Respond
3842 // with "User-defined keys are locked."
3844 if (s8c1t
== true) {
3845 writeRemote("\u009b?21n");
3847 writeRemote("\033[?21n");
3853 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3854 && (decPrivateModeFlag
== true)
3857 // Request keyboard language. Respond with "Keyboard
3858 // language is North American."
3860 if (s8c1t
== true) {
3861 writeRemote("\u009b?27;1n");
3863 writeRemote("\033[?27;1n");
3870 // Some other option, ignore
3876 * TBC - Tabulation clear.
3878 private void tbc() {
3879 int i
= getCsiParam(0, 0);
3881 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3882 for (Integer stop
: tabStops
) {
3883 if (stop
== currentState
.cursorX
) {
3888 tabStops
= newStops
;
3896 * Erase the characters in the current line from the start column to the
3897 * end column, inclusive.
3899 * @param start starting column to erase (between 0 and width - 1)
3900 * @param end ending column to erase (between 0 and width - 1)
3901 * @param honorProtected if true, do not erase characters with the
3902 * protected attribute set
3904 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3909 if (end
> width
- 1) {
3916 for (int i
= start
; i
<= end
; i
++) {
3917 DisplayLine line
= display
.get(currentState
.cursorY
);
3918 if ((!honorProtected
)
3919 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
3926 * From the VT102 manual:
3928 * Erasing a character also erases any character
3929 * attribute of the character.
3935 * Erase with the current color a.k.a. back-color erase
3938 line
.setChar(i
, ' ');
3939 line
.setAttr(i
, currentState
.attr
);
3947 * Erase a rectangular section of the screen, inclusive. end column,
3950 * @param startRow starting row to erase (between 0 and height - 1)
3951 * @param startCol starting column to erase (between 0 and width - 1)
3952 * @param endRow ending row to erase (between 0 and height - 1)
3953 * @param endCol ending column to erase (between 0 and width - 1)
3954 * @param honorProtected if true, do not erase characters with the
3955 * protected attribute set
3957 private void eraseScreen(final int startRow
, final int startCol
,
3958 final int endRow
, final int endCol
, final boolean honorProtected
) {
3966 || (endRow
< startRow
)
3967 || (endCol
< startCol
)
3972 oldCursorY
= currentState
.cursorY
;
3973 for (int i
= startRow
; i
<= endRow
; i
++) {
3974 currentState
.cursorY
= i
;
3975 eraseLine(startCol
, endCol
, honorProtected
);
3977 // Erase display clears the double attributes
3978 display
.get(i
).setDoubleWidth(false);
3979 display
.get(i
).setDoubleHeight(0);
3981 currentState
.cursorY
= oldCursorY
;
3985 * VT220 printer functions. All of these are parsed, but won't do
3988 private void printerFunctions() {
3989 boolean decPrivateModeFlag
= false;
3990 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3991 if (collectBuffer
.charAt(i
) == '?') {
3992 decPrivateModeFlag
= true;
3997 int i
= getCsiParam(0, 0);
4002 if (decPrivateModeFlag
== false) {
4008 if (decPrivateModeFlag
== true) {
4009 // Print cursor line
4014 if (decPrivateModeFlag
== true) {
4015 // Auto print mode OFF
4017 // Printer controller OFF
4019 // Characters re-appear on the screen
4020 printerControllerMode
= false;
4025 if (decPrivateModeFlag
== true) {
4029 // Printer controller
4031 // Characters get sucked into oblivion
4032 printerControllerMode
= true;
4043 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4044 * of remote systems will send an XTerm title sequence even if TERM isn't
4047 * @param xtermChar the character received from the remote side
4049 private void oscPut(final char xtermChar
) {
4051 collectBuffer
.append(xtermChar
);
4054 if (xtermChar
== 0x07) {
4055 String args
= collectBuffer
.substring(0,
4056 collectBuffer
.length() - 1);
4057 String
[] p
= args
.toString().split(";");
4059 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4067 // Go to SCAN_GROUND state
4074 * Run this input character through the ECMA48 state machine.
4076 * @param ch character from the remote side
4078 private void consume(char ch
) {
4081 // System.err.printf("%c", ch);
4083 // Special case for VT10x: 7-bit characters only
4084 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4085 ch
= (char)(ch
& 0x7F);
4088 // Special "anywhere" states
4090 // 18, 1A --> execute, then switch to SCAN_GROUND
4091 if ((ch
== 0x18) || (ch
== 0x1A)) {
4092 // CAN and SUB abort escape sequences
4097 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4101 && (scanState
!= ScanState
.DCS_ENTRY
)
4102 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4103 && (scanState
!= ScanState
.DCS_IGNORE
)
4104 && (scanState
!= ScanState
.DCS_PARAM
)
4105 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4108 scanState
= ScanState
.ESCAPE
;
4112 // 0x9B == CSI 8-bit sequence
4114 scanState
= ScanState
.CSI_ENTRY
;
4118 // 0x9D goes to ScanState.OSC_STRING
4120 scanState
= ScanState
.OSC_STRING
;
4124 // 0x90 goes to DCS_ENTRY
4126 scanState
= ScanState
.DCS_ENTRY
;
4130 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4131 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4132 scanState
= ScanState
.SOSPMAPC_STRING
;
4136 // 0x7F (DEL) is always discarded
4141 switch (scanState
) {
4144 // 00-17, 19, 1C-1F --> execute
4145 // 80-8F, 91-9A, 9C --> execute
4146 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4147 handleControlChar(ch
);
4151 if (((ch
>= 0x20) && (ch
<= 0x7F))
4155 // VT220 printer --> trash bin
4156 if (((type
== DeviceType
.VT220
)
4157 || (type
== DeviceType
.XTERM
))
4158 && (printerControllerMode
== true)
4163 // Hang onto this character
4164 repCh
= mapCharacter(ch
);
4166 // Print this character
4167 printCharacter(repCh
);
4172 // 00-17, 19, 1C-1F --> execute
4174 handleControlChar(ch
);
4178 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4179 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4181 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4185 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4186 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4197 // DECSC - Save cursor
4198 // Note this code overlaps both ANSI and VT52 mode
4203 // DECRC - Restore cursor
4204 // Note this code overlaps both ANSI and VT52 mode
4213 if (vt52Mode
== true) {
4214 // DECANM - Enter ANSI mode
4216 arrowKeyMode
= ArrowKeyMode
.VT100
;
4219 * From the VT102 docs: "You use ANSI mode to select
4220 * most terminal features; the terminal uses the same
4221 * features when it switches to VT52 mode. You
4222 * cannot, however, change most of these features in
4225 * In other words, do not reset any other attributes
4226 * when switching between VT52 submode and ANSI.
4230 currentState
.g0Charset
= CharacterSet
.US
;
4231 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4233 singleshift
= Singleshift
.NONE
;
4234 currentState
.glLockshift
= LockshiftMode
.NONE
;
4235 currentState
.grLockshift
= LockshiftMode
.NONE
;
4239 // DECKPAM - Keypad application mode
4240 // Note this code overlaps both ANSI and VT52 mode
4244 // DECKPNM - Keypad numeric mode
4245 // Note this code overlaps both ANSI and VT52 mode
4252 if (vt52Mode
== true) {
4253 // Cursor up, and stop at the top without scrolling
4258 if (vt52Mode
== true) {
4259 // Cursor down, and stop at the bottom without scrolling
4260 cursorDown(1, false);
4264 if (vt52Mode
== true) {
4265 // Cursor right, and stop at the right without scrolling
4266 cursorRight(1, false);
4270 if (vt52Mode
== true) {
4271 // Cursor left, and stop at the left without scrolling
4272 cursorLeft(1, false);
4279 if (vt52Mode
== true) {
4287 if (vt52Mode
== true) {
4288 // G0 --> Special graphics
4289 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
4293 if (vt52Mode
== true) {
4295 currentState
.g0Charset
= CharacterSet
.US
;
4299 if (vt52Mode
== true) {
4301 cursorPosition(0, 0);
4303 // HTS - Horizontal tabulation set
4308 if (vt52Mode
== true) {
4309 // Reverse line feed. Same as RI.
4314 if (vt52Mode
== true) {
4315 // Erase to end of screen
4316 eraseLine(currentState
.cursorX
, width
- 1, false);
4317 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4322 if (vt52Mode
== true) {
4323 // Erase to end of line
4324 eraseLine(currentState
.cursorX
, width
- 1, false);
4330 if (vt52Mode
== true) {
4333 // RI - Reverse index
4338 if (vt52Mode
== false) {
4340 singleshift
= Singleshift
.SS2
;
4344 if (vt52Mode
== false) {
4346 singleshift
= Singleshift
.SS3
;
4353 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4369 if (vt52Mode
== true) {
4370 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4378 if (vt52Mode
== true) {
4380 // Send string directly to remote side
4381 writeRemote("\033/Z");
4384 // Send string directly to remote side
4385 writeRemote(deviceTypeResponse());
4396 // VT52 cannot get to any of these other states
4397 if (vt52Mode
== true) {
4402 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4409 // RIS - Reset to initial state
4411 // Do I clear screen too? I think so...
4412 eraseScreen(0, 0, height
- 1, width
- 1, false);
4413 cursorPosition(0, 0);
4427 if ((type
== DeviceType
.VT220
)
4428 || (type
== DeviceType
.XTERM
)) {
4430 // VT220 lockshift G2 into GL
4431 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4436 if ((type
== DeviceType
.VT220
)
4437 || (type
== DeviceType
.XTERM
)) {
4439 // VT220 lockshift G3 into GL
4440 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4458 if ((type
== DeviceType
.VT220
)
4459 || (type
== DeviceType
.XTERM
)) {
4461 // VT220 lockshift G3 into GR
4462 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4467 if ((type
== DeviceType
.VT220
)
4468 || (type
== DeviceType
.XTERM
)) {
4470 // VT220 lockshift G2 into GR
4471 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4477 if ((type
== DeviceType
.VT220
)
4478 || (type
== DeviceType
.XTERM
)) {
4480 // VT220 lockshift G1 into GR
4481 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4491 // 0x5B goes to CSI_ENTRY
4493 scanState
= ScanState
.CSI_ENTRY
;
4496 // 0x5D goes to OSC_STRING
4498 scanState
= ScanState
.OSC_STRING
;
4501 // 0x50 goes to DCS_ENTRY
4503 scanState
= ScanState
.DCS_ENTRY
;
4506 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4507 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4508 scanState
= ScanState
.SOSPMAPC_STRING
;
4513 case ESCAPE_INTERMEDIATE
:
4514 // 00-17, 19, 1C-1F --> execute
4516 handleControlChar(ch
);
4519 // 20-2F --> collect
4520 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4524 // 30-7E --> dispatch, then switch to GROUND
4525 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4528 if ((collectBuffer
.length() == 1)
4529 && (collectBuffer
.charAt(0) == '(')) {
4530 // G0 --> Special graphics
4531 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4533 if ((collectBuffer
.length() == 1)
4534 && (collectBuffer
.charAt(0) == ')')) {
4535 // G1 --> Special graphics
4536 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4538 if ((type
== DeviceType
.VT220
)
4539 || (type
== DeviceType
.XTERM
)) {
4541 if ((collectBuffer
.length() == 1)
4542 && (collectBuffer
.charAt(0) == '*')) {
4543 // G2 --> Special graphics
4544 currentState
.g2Charset
= CharacterSet
.DRAWING
;
4546 if ((collectBuffer
.length() == 1)
4547 && (collectBuffer
.charAt(0) == '+')) {
4548 // G3 --> Special graphics
4549 currentState
.g3Charset
= CharacterSet
.DRAWING
;
4554 if ((collectBuffer
.length() == 1)
4555 && (collectBuffer
.charAt(0) == '(')) {
4556 // G0 --> Alternate character ROM standard character set
4557 currentState
.g0Charset
= CharacterSet
.ROM
;
4559 if ((collectBuffer
.length() == 1)
4560 && (collectBuffer
.charAt(0) == ')')) {
4561 // G1 --> Alternate character ROM standard character set
4562 currentState
.g1Charset
= CharacterSet
.ROM
;
4566 if ((collectBuffer
.length() == 1)
4567 && (collectBuffer
.charAt(0) == '(')) {
4568 // G0 --> Alternate character ROM special graphics
4569 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4571 if ((collectBuffer
.length() == 1)
4572 && (collectBuffer
.charAt(0) == ')')) {
4573 // G1 --> Alternate character ROM special graphics
4574 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4578 if ((collectBuffer
.length() == 1)
4579 && (collectBuffer
.charAt(0) == '#')) {
4580 // DECDHL - Double-height line (top half)
4585 if ((collectBuffer
.length() == 1)
4586 && (collectBuffer
.charAt(0) == '#')) {
4587 // DECDHL - Double-height line (bottom half)
4590 if ((type
== DeviceType
.VT220
)
4591 || (type
== DeviceType
.XTERM
)) {
4593 if ((collectBuffer
.length() == 1)
4594 && (collectBuffer
.charAt(0) == '(')) {
4596 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4598 if ((collectBuffer
.length() == 1)
4599 && (collectBuffer
.charAt(0) == ')')) {
4601 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4603 if ((collectBuffer
.length() == 1)
4604 && (collectBuffer
.charAt(0) == '*')) {
4606 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4608 if ((collectBuffer
.length() == 1)
4609 && (collectBuffer
.charAt(0) == '+')) {
4611 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4616 if ((collectBuffer
.length() == 1)
4617 && (collectBuffer
.charAt(0) == '#')) {
4618 // DECSWL - Single-width line
4621 if ((type
== DeviceType
.VT220
)
4622 || (type
== DeviceType
.XTERM
)) {
4624 if ((collectBuffer
.length() == 1)
4625 && (collectBuffer
.charAt(0) == '(')) {
4627 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4629 if ((collectBuffer
.length() == 1)
4630 && (collectBuffer
.charAt(0) == ')')) {
4632 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4634 if ((collectBuffer
.length() == 1)
4635 && (collectBuffer
.charAt(0) == '*')) {
4637 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4639 if ((collectBuffer
.length() == 1)
4640 && (collectBuffer
.charAt(0) == '+')) {
4642 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4647 if ((collectBuffer
.length() == 1)
4648 && (collectBuffer
.charAt(0) == '#')) {
4649 // DECDWL - Double-width line
4652 if ((type
== DeviceType
.VT220
)
4653 || (type
== DeviceType
.XTERM
)) {
4655 if ((collectBuffer
.length() == 1)
4656 && (collectBuffer
.charAt(0) == '(')) {
4658 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4660 if ((collectBuffer
.length() == 1)
4661 && (collectBuffer
.charAt(0) == ')')) {
4663 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4665 if ((collectBuffer
.length() == 1)
4666 && (collectBuffer
.charAt(0) == '*')) {
4668 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4670 if ((collectBuffer
.length() == 1)
4671 && (collectBuffer
.charAt(0) == '+')) {
4673 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4678 if ((type
== DeviceType
.VT220
)
4679 || (type
== DeviceType
.XTERM
)) {
4681 if ((collectBuffer
.length() == 1)
4682 && (collectBuffer
.charAt(0) == '(')) {
4684 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4686 if ((collectBuffer
.length() == 1)
4687 && (collectBuffer
.charAt(0) == ')')) {
4689 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4691 if ((collectBuffer
.length() == 1)
4692 && (collectBuffer
.charAt(0) == '*')) {
4694 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4696 if ((collectBuffer
.length() == 1)
4697 && (collectBuffer
.charAt(0) == '+')) {
4699 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4704 if ((collectBuffer
.length() == 1)
4705 && (collectBuffer
.charAt(0) == '#')) {
4706 // DECALN - Screen alignment display
4715 if ((type
== DeviceType
.VT220
)
4716 || (type
== DeviceType
.XTERM
)) {
4718 if ((collectBuffer
.length() == 1)
4719 && (collectBuffer
.charAt(0) == '(')) {
4720 // G0 --> DEC_SUPPLEMENTAL
4721 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4723 if ((collectBuffer
.length() == 1)
4724 && (collectBuffer
.charAt(0) == ')')) {
4725 // G1 --> DEC_SUPPLEMENTAL
4726 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4728 if ((collectBuffer
.length() == 1)
4729 && (collectBuffer
.charAt(0) == '*')) {
4730 // G2 --> DEC_SUPPLEMENTAL
4731 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4733 if ((collectBuffer
.length() == 1)
4734 && (collectBuffer
.charAt(0) == '+')) {
4735 // G3 --> DEC_SUPPLEMENTAL
4736 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4741 if ((type
== DeviceType
.VT220
)
4742 || (type
== DeviceType
.XTERM
)) {
4744 if ((collectBuffer
.length() == 1)
4745 && (collectBuffer
.charAt(0) == '(')) {
4747 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4749 if ((collectBuffer
.length() == 1)
4750 && (collectBuffer
.charAt(0) == ')')) {
4752 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4754 if ((collectBuffer
.length() == 1)
4755 && (collectBuffer
.charAt(0) == '*')) {
4757 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4759 if ((collectBuffer
.length() == 1)
4760 && (collectBuffer
.charAt(0) == '+')) {
4762 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4771 if ((collectBuffer
.length() == 1)
4772 && (collectBuffer
.charAt(0) == '(')) {
4773 // G0 --> United Kingdom set
4774 currentState
.g0Charset
= CharacterSet
.UK
;
4776 if ((collectBuffer
.length() == 1)
4777 && (collectBuffer
.charAt(0) == ')')) {
4778 // G1 --> United Kingdom set
4779 currentState
.g1Charset
= CharacterSet
.UK
;
4781 if ((type
== DeviceType
.VT220
)
4782 || (type
== DeviceType
.XTERM
)) {
4784 if ((collectBuffer
.length() == 1)
4785 && (collectBuffer
.charAt(0) == '*')) {
4786 // G2 --> United Kingdom set
4787 currentState
.g2Charset
= CharacterSet
.UK
;
4789 if ((collectBuffer
.length() == 1)
4790 && (collectBuffer
.charAt(0) == '+')) {
4791 // G3 --> United Kingdom set
4792 currentState
.g3Charset
= CharacterSet
.UK
;
4797 if ((collectBuffer
.length() == 1)
4798 && (collectBuffer
.charAt(0) == '(')) {
4800 currentState
.g0Charset
= CharacterSet
.US
;
4802 if ((collectBuffer
.length() == 1)
4803 && (collectBuffer
.charAt(0) == ')')) {
4805 currentState
.g1Charset
= CharacterSet
.US
;
4807 if ((type
== DeviceType
.VT220
)
4808 || (type
== DeviceType
.XTERM
)) {
4810 if ((collectBuffer
.length() == 1)
4811 && (collectBuffer
.charAt(0) == '*')) {
4813 currentState
.g2Charset
= CharacterSet
.US
;
4815 if ((collectBuffer
.length() == 1)
4816 && (collectBuffer
.charAt(0) == '+')) {
4818 currentState
.g3Charset
= CharacterSet
.US
;
4823 if ((type
== DeviceType
.VT220
)
4824 || (type
== DeviceType
.XTERM
)) {
4826 if ((collectBuffer
.length() == 1)
4827 && (collectBuffer
.charAt(0) == '(')) {
4829 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4831 if ((collectBuffer
.length() == 1)
4832 && (collectBuffer
.charAt(0) == ')')) {
4834 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4836 if ((collectBuffer
.length() == 1)
4837 && (collectBuffer
.charAt(0) == '*')) {
4839 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4841 if ((collectBuffer
.length() == 1)
4842 && (collectBuffer
.charAt(0) == '+')) {
4844 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4851 if ((type
== DeviceType
.VT220
)
4852 || (type
== DeviceType
.XTERM
)) {
4854 if ((collectBuffer
.length() == 1)
4855 && (collectBuffer
.charAt(0) == '(')) {
4857 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4859 if ((collectBuffer
.length() == 1)
4860 && (collectBuffer
.charAt(0) == ')')) {
4862 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4864 if ((collectBuffer
.length() == 1)
4865 && (collectBuffer
.charAt(0) == '*')) {
4867 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4869 if ((collectBuffer
.length() == 1)
4870 && (collectBuffer
.charAt(0) == '+')) {
4872 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4877 if ((type
== DeviceType
.VT220
)
4878 || (type
== DeviceType
.XTERM
)) {
4880 if ((collectBuffer
.length() == 1)
4881 && (collectBuffer
.charAt(0) == ' ')) {
4888 if ((type
== DeviceType
.VT220
)
4889 || (type
== DeviceType
.XTERM
)) {
4891 if ((collectBuffer
.length() == 1)
4892 && (collectBuffer
.charAt(0) == ' ')) {
4899 if ((type
== DeviceType
.VT220
)
4900 || (type
== DeviceType
.XTERM
)) {
4902 if ((collectBuffer
.length() == 1)
4903 && (collectBuffer
.charAt(0) == '(')) {
4905 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4907 if ((collectBuffer
.length() == 1)
4908 && (collectBuffer
.charAt(0) == ')')) {
4910 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4912 if ((collectBuffer
.length() == 1)
4913 && (collectBuffer
.charAt(0) == '*')) {
4915 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4917 if ((collectBuffer
.length() == 1)
4918 && (collectBuffer
.charAt(0) == '+')) {
4920 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4928 if ((type
== DeviceType
.VT220
)
4929 || (type
== DeviceType
.XTERM
)) {
4931 if ((collectBuffer
.length() == 1)
4932 && (collectBuffer
.charAt(0) == '(')) {
4934 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4936 if ((collectBuffer
.length() == 1)
4937 && (collectBuffer
.charAt(0) == ')')) {
4939 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4941 if ((collectBuffer
.length() == 1)
4942 && (collectBuffer
.charAt(0) == '*')) {
4944 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4946 if ((collectBuffer
.length() == 1)
4947 && (collectBuffer
.charAt(0) == '+')) {
4949 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4960 if ((type
== DeviceType
.VT220
)
4961 || (type
== DeviceType
.XTERM
)) {
4963 if ((collectBuffer
.length() == 1)
4964 && (collectBuffer
.charAt(0) == '(')) {
4966 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4968 if ((collectBuffer
.length() == 1)
4969 && (collectBuffer
.charAt(0) == ')')) {
4971 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4973 if ((collectBuffer
.length() == 1)
4974 && (collectBuffer
.charAt(0) == '*')) {
4976 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4978 if ((collectBuffer
.length() == 1)
4979 && (collectBuffer
.charAt(0) == '+')) {
4981 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4986 if ((type
== DeviceType
.VT220
)
4987 || (type
== DeviceType
.XTERM
)) {
4989 if ((collectBuffer
.length() == 1)
4990 && (collectBuffer
.charAt(0) == '(')) {
4992 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4994 if ((collectBuffer
.length() == 1)
4995 && (collectBuffer
.charAt(0) == ')')) {
4997 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4999 if ((collectBuffer
.length() == 1)
5000 && (collectBuffer
.charAt(0) == '*')) {
5002 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5004 if ((collectBuffer
.length() == 1)
5005 && (collectBuffer
.charAt(0) == '+')) {
5007 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5019 if ((type
== DeviceType
.VT220
)
5020 || (type
== DeviceType
.XTERM
)) {
5022 if ((collectBuffer
.length() == 1)
5023 && (collectBuffer
.charAt(0) == '(')) {
5025 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5027 if ((collectBuffer
.length() == 1)
5028 && (collectBuffer
.charAt(0) == ')')) {
5030 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5032 if ((collectBuffer
.length() == 1)
5033 && (collectBuffer
.charAt(0) == '*')) {
5035 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5037 if ((collectBuffer
.length() == 1)
5038 && (collectBuffer
.charAt(0) == '+')) {
5040 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5045 if ((type
== DeviceType
.VT220
)
5046 || (type
== DeviceType
.XTERM
)) {
5048 if ((collectBuffer
.length() == 1)
5049 && (collectBuffer
.charAt(0) == '(')) {
5051 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5053 if ((collectBuffer
.length() == 1)
5054 && (collectBuffer
.charAt(0) == ')')) {
5056 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5058 if ((collectBuffer
.length() == 1)
5059 && (collectBuffer
.charAt(0) == '*')) {
5061 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5063 if ((collectBuffer
.length() == 1)
5064 && (collectBuffer
.charAt(0) == '+')) {
5066 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5113 // 0x9C goes to GROUND
5121 // 00-17, 19, 1C-1F --> execute
5123 handleControlChar(ch
);
5126 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5127 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5129 scanState
= ScanState
.CSI_INTERMEDIATE
;
5132 // 30-39, 3B --> param, then switch to CSI_PARAM
5133 if ((ch
>= '0') && (ch
<= '9')) {
5135 scanState
= ScanState
.CSI_PARAM
;
5139 scanState
= ScanState
.CSI_PARAM
;
5142 // 3C-3F --> collect, then switch to CSI_PARAM
5143 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5145 scanState
= ScanState
.CSI_PARAM
;
5148 // 40-7E --> dispatch, then switch to GROUND
5149 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5152 // ICH - Insert character
5160 // CUD - Cursor down
5164 // CUF - Cursor forward
5168 // CUB - Cursor backward
5172 // CNL - Cursor down and to column 1
5173 if (type
== DeviceType
.XTERM
) {
5178 // CPL - Cursor up and to column 1
5179 if (type
== DeviceType
.XTERM
) {
5184 // CHA - Cursor to column # in current row
5185 if (type
== DeviceType
.XTERM
) {
5190 // CUP - Cursor position
5194 // CHT - Cursor forward X tab stops (default 1)
5195 if (type
== DeviceType
.XTERM
) {
5200 // ED - Erase in display
5204 // EL - Erase in line
5219 // DCH - Delete character
5226 // Scroll up X lines (default 1)
5227 if (type
== DeviceType
.XTERM
) {
5232 // Scroll down X lines (default 1)
5233 if (type
== DeviceType
.XTERM
) {
5242 if ((type
== DeviceType
.VT220
)
5243 || (type
== DeviceType
.XTERM
)) {
5245 // ECH - Erase character
5252 // CBT - Cursor backward X tab stops (default 1)
5253 if (type
== DeviceType
.XTERM
) {
5264 // HPA - Cursor to column # in current row. Same as CHA
5265 if (type
== DeviceType
.XTERM
) {
5270 // HPR - Cursor right. Same as CUF
5271 if (type
== DeviceType
.XTERM
) {
5276 // REP - Repeat last char X times
5277 if (type
== DeviceType
.XTERM
) {
5282 // DA - Device attributes
5286 // VPA - Cursor to row, current column.
5287 if (type
== DeviceType
.XTERM
) {
5292 // VPR - Cursor down. Same as CUD
5293 if (type
== DeviceType
.XTERM
) {
5298 // HVP - Horizontal and vertical position
5302 // TBC - Tabulation clear
5306 // Sets an ANSI or DEC private toggle
5310 if ((type
== DeviceType
.VT220
)
5311 || (type
== DeviceType
.XTERM
)) {
5313 // Printer functions
5321 // Sets an ANSI or DEC private toggle
5325 // SGR - Select graphics rendition
5329 // DSR - Device status report
5336 // DECLL - Load leds
5340 // DECSTBM - Set top and bottom margins
5344 // Save cursor (ANSI.SYS)
5345 if (type
== DeviceType
.XTERM
) {
5346 savedState
.cursorX
= currentState
.cursorX
;
5347 savedState
.cursorY
= currentState
.cursorY
;
5353 // Restore cursor (ANSI.SYS)
5354 if (type
== DeviceType
.XTERM
) {
5355 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5362 // DECREQTPARM - Request terminal parameters
5378 // 0x9C goes to GROUND
5383 // 0x3A goes to CSI_IGNORE
5385 scanState
= ScanState
.CSI_IGNORE
;
5390 // 00-17, 19, 1C-1F --> execute
5392 handleControlChar(ch
);
5395 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5396 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5398 scanState
= ScanState
.CSI_INTERMEDIATE
;
5401 // 30-39, 3B --> param
5402 if ((ch
>= '0') && (ch
<= '9')) {
5409 // 0x3A goes to CSI_IGNORE
5411 scanState
= ScanState
.CSI_IGNORE
;
5413 // 0x3C-3F goes to CSI_IGNORE
5414 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5415 scanState
= ScanState
.CSI_IGNORE
;
5418 // 40-7E --> dispatch, then switch to GROUND
5419 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5422 // ICH - Insert character
5430 // CUD - Cursor down
5434 // CUF - Cursor forward
5438 // CUB - Cursor backward
5442 // CNL - Cursor down and to column 1
5443 if (type
== DeviceType
.XTERM
) {
5448 // CPL - Cursor up and to column 1
5449 if (type
== DeviceType
.XTERM
) {
5454 // CHA - Cursor to column # in current row
5455 if (type
== DeviceType
.XTERM
) {
5460 // CUP - Cursor position
5464 // CHT - Cursor forward X tab stops (default 1)
5465 if (type
== DeviceType
.XTERM
) {
5470 // ED - Erase in display
5474 // EL - Erase in line
5489 // DCH - Delete character
5496 // Scroll up X lines (default 1)
5497 if (type
== DeviceType
.XTERM
) {
5502 // Scroll down X lines (default 1)
5503 if (type
== DeviceType
.XTERM
) {
5512 if ((type
== DeviceType
.VT220
)
5513 || (type
== DeviceType
.XTERM
)) {
5515 // ECH - Erase character
5522 // CBT - Cursor backward X tab stops (default 1)
5523 if (type
== DeviceType
.XTERM
) {
5534 // HPA - Cursor to column # in current row. Same as CHA
5535 if (type
== DeviceType
.XTERM
) {
5540 // HPR - Cursor right. Same as CUF
5541 if (type
== DeviceType
.XTERM
) {
5546 // REP - Repeat last char X times
5547 if (type
== DeviceType
.XTERM
) {
5552 // DA - Device attributes
5556 // VPA - Cursor to row, current column.
5557 if (type
== DeviceType
.XTERM
) {
5562 // VPR - Cursor down. Same as CUD
5563 if (type
== DeviceType
.XTERM
) {
5568 // HVP - Horizontal and vertical position
5572 // TBC - Tabulation clear
5576 // Sets an ANSI or DEC private toggle
5580 if ((type
== DeviceType
.VT220
)
5581 || (type
== DeviceType
.XTERM
)) {
5583 // Printer functions
5591 // Sets an ANSI or DEC private toggle
5595 // SGR - Select graphics rendition
5599 // DSR - Device status report
5606 // DECLL - Load leds
5610 // DECSTBM - Set top and bottom margins
5620 // DECREQTPARM - Request terminal parameters
5637 case CSI_INTERMEDIATE
:
5638 // 00-17, 19, 1C-1F --> execute
5640 handleControlChar(ch
);
5643 // 20-2F --> collect
5644 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5648 // 0x30-3F goes to CSI_IGNORE
5649 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5650 scanState
= ScanState
.CSI_IGNORE
;
5653 // 40-7E --> dispatch, then switch to GROUND
5654 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5706 if (((type
== DeviceType
.VT220
)
5707 || (type
== DeviceType
.XTERM
))
5708 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5710 // DECSCL - compatibility level
5713 if ((type
== DeviceType
.XTERM
)
5714 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
5716 // DECSTR - Soft terminal reset
5721 if (((type
== DeviceType
.VT220
)
5722 || (type
== DeviceType
.XTERM
))
5723 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5751 // 00-17, 19, 1C-1F --> execute
5753 handleControlChar(ch
);
5756 // 20-2F --> collect
5757 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5761 // 40-7E --> ignore, then switch to GROUND
5762 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5766 // 20-3F, 7F --> ignore
5772 // 0x9C goes to GROUND
5777 // 0x1B 0x5C goes to GROUND
5782 if ((collectBuffer
.length() > 0)
5783 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5789 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5790 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5792 scanState
= ScanState
.DCS_INTERMEDIATE
;
5795 // 30-39, 3B --> param, then switch to DCS_PARAM
5796 if ((ch
>= '0') && (ch
<= '9')) {
5798 scanState
= ScanState
.DCS_PARAM
;
5802 scanState
= ScanState
.DCS_PARAM
;
5805 // 3C-3F --> collect, then switch to DCS_PARAM
5806 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5808 scanState
= ScanState
.DCS_PARAM
;
5811 // 00-17, 19, 1C-1F, 7F --> ignore
5813 // 0x3A goes to DCS_IGNORE
5815 scanState
= ScanState
.DCS_IGNORE
;
5818 // 0x40-7E goes to DCS_PASSTHROUGH
5819 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5820 scanState
= ScanState
.DCS_PASSTHROUGH
;
5824 case DCS_INTERMEDIATE
:
5826 // 0x9C goes to GROUND
5831 // 0x1B 0x5C goes to GROUND
5836 if ((collectBuffer
.length() > 0)
5837 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5843 // 0x30-3F goes to DCS_IGNORE
5844 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5845 scanState
= ScanState
.DCS_IGNORE
;
5848 // 0x40-7E goes to DCS_PASSTHROUGH
5849 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5850 scanState
= ScanState
.DCS_PASSTHROUGH
;
5853 // 00-17, 19, 1C-1F, 7F --> ignore
5858 // 0x9C goes to GROUND
5863 // 0x1B 0x5C goes to GROUND
5868 if ((collectBuffer
.length() > 0)
5869 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5875 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5876 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5878 scanState
= ScanState
.DCS_INTERMEDIATE
;
5881 // 30-39, 3B --> param
5882 if ((ch
>= '0') && (ch
<= '9')) {
5889 // 00-17, 19, 1C-1F, 7F --> ignore
5891 // 0x3A, 3C-3F goes to DCS_IGNORE
5893 scanState
= ScanState
.DCS_IGNORE
;
5895 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5896 scanState
= ScanState
.DCS_IGNORE
;
5899 // 0x40-7E goes to DCS_PASSTHROUGH
5900 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5901 scanState
= ScanState
.DCS_PASSTHROUGH
;
5905 case DCS_PASSTHROUGH
:
5906 // 0x9C goes to GROUND
5911 // 0x1B 0x5C goes to GROUND
5916 if ((collectBuffer
.length() > 0)
5917 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5923 // 00-17, 19, 1C-1F, 20-7E --> put
5931 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5934 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5943 // 00-17, 19, 1C-1F, 20-7F --> ignore
5945 // 0x9C goes to GROUND
5952 case SOSPMAPC_STRING
:
5953 // 00-17, 19, 1C-1F, 20-7F --> ignore
5955 // 0x9C goes to GROUND
5963 // Special case for Xterm: OSC can pass control characters
5964 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5968 // 00-17, 19, 1C-1F --> ignore
5970 // 20-7F --> osc_put
5971 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5975 // 0x9C goes to GROUND
5982 case VT52_DIRECT_CURSOR_ADDRESS
:
5983 // This is a special case for the VT52 sequence "ESC Y l c"
5984 if (collectBuffer
.length() == 0) {
5986 } else if (collectBuffer
.length() == 1) {
5987 // We've got the two characters, one in the buffer and the
5989 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
5998 * Expose current cursor X to outside world.
6000 * @return current cursor X
6002 public final int getCursorX() {
6003 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6004 return currentState
.cursorX
* 2;
6006 return currentState
.cursorX
;
6010 * Expose current cursor Y to outside world.
6012 * @return current cursor Y
6014 public final int getCursorY() {
6015 return currentState
.cursorY
;
6019 * Read function runs on a separate thread.
6021 public final void run() {
6022 boolean utf8
= false;
6023 boolean done
= false;
6025 if (type
== DeviceType
.XTERM
) {
6029 // available() will often return > 1, so we need to read in chunks to
6031 char [] readBufferUTF8
= null;
6032 byte [] readBuffer
= null;
6034 readBufferUTF8
= new char[128];
6036 readBuffer
= new byte[128];
6039 while (!done
&& !stopReaderThread
) {
6041 int n
= inputStream
.available();
6043 // System.err.printf("available() %d\n", n); System.err.flush();
6045 if (readBufferUTF8
.length
< n
) {
6046 // The buffer wasn't big enough, make it huger
6047 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
6050 readBufferUTF8
= new char[newSizeHalf
* 2];
6053 if (readBuffer
.length
< n
) {
6054 // The buffer wasn't big enough, make it huger
6055 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
6056 readBuffer
= new byte[newSizeHalf
* 2];
6062 } catch (InterruptedException e
) {
6071 rc
= input
.read(readBufferUTF8
, 0,
6072 readBufferUTF8
.length
);
6074 rc
= inputStream
.read(readBuffer
, 0,
6077 } catch (ReadTimeoutException e
) {
6081 // System.err.printf("read() %d\n", rc); System.err.flush();
6086 for (int i
= 0; i
< rc
; i
++) {
6089 ch
= readBufferUTF8
[i
];
6094 synchronized (this) {
6095 // Don't step on UI events
6099 // Permit my enclosing UI to know that I updated.
6100 if (displayListener
!= null) {
6101 displayListener
.displayChanged();
6104 // System.err.println("end while loop"); System.err.flush();
6105 } catch (IOException e
) {
6106 e
.printStackTrace();
6110 } // while ((done == false) && (stopReaderThread == false))
6112 // Let the rest of the world know that I am done.
6113 stopReaderThread
= true;
6116 inputStream
.cancelRead();
6117 inputStream
.close();
6119 } catch (IOException e
) {
6125 } catch (IOException e
) {
6129 // System.err.println("*** run() exiting..."); System.err.flush();