2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2016 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.tterminal
;
31 import java
.io
.InputStream
;
32 import java
.io
.InputStreamReader
;
33 import java
.io
.IOException
;
34 import java
.io
.OutputStream
;
35 import java
.io
.OutputStreamWriter
;
36 import java
.io
.Reader
;
37 import java
.io
.UnsupportedEncodingException
;
38 import java
.io
.Writer
;
39 import java
.util
.ArrayList
;
40 import java
.util
.Collections
;
41 import java
.util
.LinkedList
;
42 import java
.util
.List
;
44 import jexer
.TKeypress
;
45 import jexer
.event
.TMouseEvent
;
46 import jexer
.bits
.Color
;
47 import jexer
.bits
.Cell
;
48 import jexer
.bits
.CellAttributes
;
49 import static jexer
.TKeypress
.*;
52 * This implements a complex ANSI ECMA-48/ISO 6429/ANSI X3.64 type consoles,
53 * including a scrollback buffer.
56 * It currently implements VT100, VT102, VT220, and XTERM with the following
60 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
61 * failure with XTERM. This is due to vttest failing to decode the UTF-8
65 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
66 * from VT100 are not supported.
69 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
70 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
71 * because DECSCL is not supported, it will fail the last part of the
72 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
75 * - Numeric/application keys from the number pad are not supported because
76 * they are not exposed from the TKeypress API.
79 * - VT52 HOLD SCREEN mode is not supported.
82 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
83 * numerators) are not rendered correctly.
86 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
88 public class ECMA48
implements Runnable
{
91 * The emulator can emulate several kinds of terminals.
93 public enum DeviceType
{
95 * DEC VT100 but also including the three VT102 functions.
116 * Return the proper primary Device Attributes string.
118 * @return string to send to remote side that is appropriate for the
121 private String
deviceTypeResponse() {
124 // "I am a VT100 with advanced video option" (often VT102)
132 // "I am a VT220" - 7 bit version
134 return "\033[?62;1;6c";
136 // "I am a VT220" - 8 bit version
137 return "\u009b?62;1;6c";
139 // "I am a VT100 with advanced video option" (often VT102)
142 throw new IllegalArgumentException("Invalid device type: " + type
);
147 * Return the proper TERM environment variable for this device type.
149 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
150 * @return "vt100", "xterm", etc.
152 public static String
deviceTypeTerm(final DeviceType deviceType
) {
153 switch (deviceType
) {
167 throw new IllegalArgumentException("Invalid device type: "
173 * Return the proper LANG for this device type. Only XTERM devices know
174 * about UTF-8, the others are defined by their standard to be either
175 * 7-bit or 8-bit characters only.
177 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
178 * @param baseLang a base language without UTF-8 flag such as "C" or
180 * @return "en_US", "en_US.UTF-8", etc.
182 public static String
deviceTypeLang(final DeviceType deviceType
,
183 final String baseLang
) {
185 switch (deviceType
) {
193 return baseLang
+ ".UTF-8";
196 throw new IllegalArgumentException("Invalid device type: "
202 * Write a string directly to the remote side.
204 * @param str string to send
206 private void writeRemote(final String str
) {
207 if (stopReaderThread
) {
208 // Reader hit EOF, bail out now.
213 // System.err.printf("writeRemote() '%s'\n", str);
219 if (outputStream
== null) {
223 for (int i
= 0; i
< str
.length(); i
++) {
224 outputStream
.write(str
.charAt(i
));
226 outputStream
.flush();
227 } catch (IOException e
) {
233 if (output
== null) {
239 } catch (IOException e
) {
245 throw new IllegalArgumentException("Invalid device type: " + type
);
250 * Close the input and output streams and stop the reader thread. Note
251 * that it is safe to call this multiple times.
253 public final void close() {
255 // Synchronize so we don't stomp on the reader thread.
256 synchronized (this) {
258 // Close the input stream
263 if (inputStream
!= null) {
266 } catch (IOException e
) {
276 } catch (IOException e
) {
285 // Tell the reader thread to stop looking at input.
286 if (stopReaderThread
== false) {
287 stopReaderThread
= true;
290 } catch (InterruptedException e
) {
295 // Close the output stream.
300 if (outputStream
!= null) {
302 outputStream
.close();
303 } catch (IOException e
) {
310 if (output
!= null) {
313 } catch (IOException e
) {
320 throw new IllegalArgumentException("Invalid device type: "
323 } // synchronized (this)
327 * When true, the reader thread is expected to exit.
329 private volatile boolean stopReaderThread
= false;
334 private Thread readerThread
= null;
337 * See if the reader thread is still running.
339 * @return if true, we are still connected to / reading from the remote
342 public final boolean isReading() {
343 return (!stopReaderThread
);
347 * The type of emulator to be.
349 private DeviceType type
= DeviceType
.VT102
;
352 * Obtain a new blank display line for an external user
353 * (e.g. TTerminalWindow).
355 * @return new blank line
357 public final DisplayLine
getBlankDisplayLine() {
358 return new DisplayLine(currentState
.attr
);
362 * The scrollback buffer characters + attributes.
364 private volatile List
<DisplayLine
> scrollback
;
367 * Get the scrollback buffer.
369 * @return the scrollback buffer
371 public final List
<DisplayLine
> getScrollbackBuffer() {
376 * The raw display buffer characters + attributes.
378 private volatile List
<DisplayLine
> display
;
381 * Get the display buffer.
383 * @return the display buffer
385 public final List
<DisplayLine
> getDisplayBuffer() {
390 * The terminal's input. For type == XTERM, this is an InputStreamReader
391 * with UTF-8 encoding.
393 private Reader input
;
396 * The terminal's raw InputStream. This is used for type != XTERM.
398 private volatile InputStream inputStream
;
401 * The terminal's output. For type == XTERM, this wraps an
402 * OutputStreamWriter with UTF-8 encoding.
404 private Writer output
;
407 * The terminal's raw OutputStream. This is used for type != XTERM.
409 private OutputStream outputStream
;
412 * Parser character scan states.
429 VT52_DIRECT_CURSOR_ADDRESS
433 * Current scanning state.
435 private ScanState scanState
;
438 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
439 * can't really use it in keypress() because we do not see number pad
440 * events from TKeypress.
442 private enum KeypadMode
{
448 * Arrow keys can emit three different sequences (DECCKM or VT52
451 private enum ArrowKeyMode
{
458 * Available character sets for GL, GR, G0, G1, G2, G3.
460 private enum CharacterSet
{
481 * Single-shift states used by the C1 control characters SS2 (0x8E) and
484 private enum Singleshift
{
491 * VT220+ lockshift states.
493 private enum LockshiftMode
{
503 * XTERM mouse reporting protocols.
505 private enum MouseProtocol
{
514 * Which mouse protocol is active.
516 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
519 * XTERM mouse reporting encodings.
521 private enum MouseEncoding
{
528 * Which mouse encoding is active.
530 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
533 * Physical display width. We start at 80x24, but the user can resize us
539 * Get the display width.
541 * @return the width (usually 80 or 132)
543 public final int getWidth() {
548 * Physical display height. We start at 80x24, but the user can resize
554 * Get the display height.
556 * @return the height (usually 24)
558 public final int getHeight() {
563 * Top margin of the scrolling region.
565 private int scrollRegionTop
;
568 * Bottom margin of the scrolling region.
570 private int scrollRegionBottom
;
573 * Right margin column number. This can be selected by the remote side
574 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
576 private int rightMargin
;
579 * Last character printed.
584 * VT100-style line wrapping: a character is placed in column 80 (or
585 * 132), but the line does NOT wrap until another character is written to
586 * column 1 of the next line, after which the cursor moves to column 2.
588 private boolean wrapLineFlag
;
591 * VT220 single shift flag.
593 private Singleshift singleshift
= Singleshift
.NONE
;
596 * true = insert characters, false = overwrite.
598 private boolean insertMode
= false;
601 * VT52 mode as selected by DECANM. True means VT52, false means
602 * ANSI. Default is ANSI.
604 private boolean vt52Mode
= false;
607 * Visible cursor (DECTCEM).
609 private boolean cursorVisible
= true;
612 * Get visible cursor flag.
614 * @return if true, the cursor is visible
616 public final boolean isCursorVisible() {
617 return cursorVisible
;
621 * Screen title as set by the xterm OSC sequence. Lots of applications
622 * send a screenTitle regardless of whether it is an xterm client or not.
624 private String screenTitle
= "";
627 * Get the screen title as set by the xterm OSC sequence. Lots of
628 * applications send a screenTitle regardless of whether it is an xterm
631 * @return screen title
633 public final String
getScreenTitle() {
638 * Parameter characters being collected.
640 private List
<Integer
> csiParams
;
643 * Non-csi collect buffer.
645 private StringBuilder collectBuffer
;
648 * When true, use the G1 character set.
650 private boolean shiftOut
= false;
653 * Horizontal tab stop locations.
655 private List
<Integer
> tabStops
;
658 * S8C1T. True means 8bit controls, false means 7bit controls.
660 private boolean s8c1t
= false;
663 * Printer mode. True means send all output to printer, which discards
666 private boolean printerControllerMode
= false;
669 * LMN line mode. If true, linefeed() puts the cursor on the first
670 * column of the next line. If false, linefeed() puts the cursor one
671 * line down on the current line. The default is false.
673 private boolean newLineMode
= false;
676 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
678 private ArrowKeyMode arrowKeyMode
;
681 * Whether number pad keys send VT100 or VT52, application or numeric
684 @SuppressWarnings("unused")
685 private KeypadMode keypadMode
;
688 * When true, the terminal is in 132-column mode (DECCOLM).
690 private boolean columns132
= false;
693 * Get 132 columns value.
695 * @return if true, the terminal is in 132 column mode
697 public final boolean isColumns132() {
702 * true = reverse video. Set by DECSCNM.
704 private boolean reverseVideo
= false;
707 * false = echo characters locally.
709 private boolean fullDuplex
= true;
712 * DECSC/DECRC save/restore a subset of the total state. This class
713 * encapsulates those specific flags/modes.
715 private class SaveableState
{
718 * When true, cursor positions are relative to the scrolling region.
720 public boolean originMode
= false;
723 * The current editing X position.
725 public int cursorX
= 0;
728 * The current editing Y position.
730 public int cursorY
= 0;
733 * Which character set is currently selected in G0.
735 public CharacterSet g0Charset
= CharacterSet
.US
;
738 * Which character set is currently selected in G1.
740 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
743 * Which character set is currently selected in G2.
745 public CharacterSet g2Charset
= CharacterSet
.US
;
748 * Which character set is currently selected in G3.
750 public CharacterSet g3Charset
= CharacterSet
.US
;
753 * Which character set is currently selected in GR.
755 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
758 * The current drawing attributes.
760 public CellAttributes attr
;
765 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
770 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
775 public boolean lineWrap
= true;
780 public void reset() {
784 g0Charset
= CharacterSet
.US
;
785 g1Charset
= CharacterSet
.DRAWING
;
786 g2Charset
= CharacterSet
.US
;
787 g3Charset
= CharacterSet
.US
;
788 grCharset
= CharacterSet
.DRAWING
;
789 attr
= new CellAttributes();
790 glLockshift
= LockshiftMode
.NONE
;
791 grLockshift
= LockshiftMode
.NONE
;
796 * Copy attributes from another instance.
798 * @param that the other instance to match
800 public void setTo(final SaveableState that
) {
801 this.originMode
= that
.originMode
;
802 this.cursorX
= that
.cursorX
;
803 this.cursorY
= that
.cursorY
;
804 this.g0Charset
= that
.g0Charset
;
805 this.g1Charset
= that
.g1Charset
;
806 this.g2Charset
= that
.g2Charset
;
807 this.g3Charset
= that
.g3Charset
;
808 this.grCharset
= that
.grCharset
;
809 this.attr
= new CellAttributes();
810 this.attr
.setTo(that
.attr
);
811 this.glLockshift
= that
.glLockshift
;
812 this.grLockshift
= that
.grLockshift
;
813 this.lineWrap
= that
.lineWrap
;
817 * Public constructor.
819 public SaveableState() {
825 * The current terminal state.
827 private SaveableState currentState
;
830 * The last saved terminal state.
832 private SaveableState savedState
;
835 * Clear the CSI parameters and flags.
837 private void toGround() {
839 collectBuffer
= new StringBuilder(8);
840 scanState
= ScanState
.GROUND
;
844 * Reset the tab stops list.
846 private void resetTabStops() {
848 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
849 tabStops
.add(new Integer(i
* 8));
854 * Reset the emulation state.
856 private void reset() {
858 currentState
= new SaveableState();
859 savedState
= new SaveableState();
860 scanState
= ScanState
.GROUND
;
864 scrollRegionBottom
= height
- 1;
865 rightMargin
= width
- 1;
867 arrowKeyMode
= ArrowKeyMode
.ANSI
;
868 keypadMode
= KeypadMode
.Numeric
;
869 wrapLineFlag
= false;
877 reverseVideo
= false;
879 cursorVisible
= true;
882 singleshift
= Singleshift
.NONE
;
884 printerControllerMode
= false;
887 mouseProtocol
= MouseProtocol
.OFF
;
888 mouseEncoding
= MouseEncoding
.X10
;
898 * Public constructor.
900 * @param type one of the DeviceType constants to select VT100, VT102,
902 * @param inputStream an InputStream connected to the remote side. For
903 * type == XTERM, inputStream is converted to a Reader with UTF-8
905 * @param outputStream an OutputStream connected to the remote user. For
906 * type == XTERM, outputStream is converted to a Writer with UTF-8
908 * @throws UnsupportedEncodingException if an exception is thrown when
909 * creating the InputStreamReader
911 public ECMA48(final DeviceType type
, final InputStream inputStream
,
912 final OutputStream outputStream
) throws UnsupportedEncodingException
{
914 assert (inputStream
!= null);
915 assert (outputStream
!= null);
917 csiParams
= new ArrayList
<Integer
>();
918 tabStops
= new ArrayList
<Integer
>();
919 scrollback
= new LinkedList
<DisplayLine
>();
920 display
= new LinkedList
<DisplayLine
>();
923 this.inputStream
= inputStream
;
924 if (type
== DeviceType
.XTERM
) {
925 this.input
= new InputStreamReader(inputStream
, "UTF-8");
926 this.output
= new OutputStreamWriter(outputStream
, "UTF-8");
927 this.outputStream
= null;
930 this.outputStream
= outputStream
;
934 for (int i
= 0; i
< height
; i
++) {
935 display
.add(new DisplayLine(currentState
.attr
));
938 // Spin up the input reader
939 readerThread
= new Thread(this);
940 readerThread
.start();
944 * Append a new line to the bottom of the display, adding lines off the
945 * top to the scrollback buffer.
947 private void newDisplayLine() {
948 // Scroll the top line off into the scrollback buffer
949 scrollback
.add(display
.get(0));
951 DisplayLine line
= new DisplayLine(currentState
.attr
);
952 line
.setReverseColor(reverseVideo
);
957 * Wraps the current line.
959 private void wrapCurrentLine() {
960 if (currentState
.cursorY
== height
- 1) {
963 if (currentState
.cursorY
< height
- 1) {
964 currentState
.cursorY
++;
966 currentState
.cursorX
= 0;
970 * Handle a carriage return.
972 private void carriageReturn() {
973 currentState
.cursorX
= 0;
974 wrapLineFlag
= false;
978 * Reverse the color of the visible display.
980 private void invertDisplayColors() {
981 for (DisplayLine line
: display
) {
982 line
.setReverseColor(!line
.isReverseColor());
989 private void linefeed() {
991 if (currentState
.cursorY
< scrollRegionBottom
) {
992 // Increment screen y
993 currentState
.cursorY
++;
996 // Screen y does not increment
999 * Two cases: either we're inside a scrolling region or not. If
1000 * the scrolling region bottom is the bottom of the screen, then
1001 * push the top line into the buffer. Else scroll the scrolling
1004 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1006 // We're at the bottom of the scroll region, AND the scroll
1007 // region is the entire screen.
1013 // We're at the bottom of the scroll region, AND the scroll
1014 // region is NOT the entire screen.
1015 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1020 currentState
.cursorX
= 0;
1022 wrapLineFlag
= false;
1026 * Prints one character to the display buffer.
1028 * @param ch character to display
1030 private void printCharacter(final char ch
) {
1031 int rightMargin
= this.rightMargin
;
1033 // Check if we have double-width, and if so chop at 40/66 instead of
1035 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1036 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1039 // Check the unusually-complicated line wrapping conditions...
1040 if (currentState
.cursorX
== rightMargin
) {
1042 if (currentState
.lineWrap
== true) {
1044 * This case happens when: the cursor was already on the
1045 * right margin (either through printing or by an explicit
1046 * placement command), and a character was printed.
1048 * The line wraps only when a new character arrives AND the
1049 * cursor is already on the right margin AND has placed a
1050 * character in its cell. Easier to see than to explain.
1052 if (wrapLineFlag
== false) {
1054 * This block marks the case that we are in the margin
1055 * and the first character has been received and printed.
1057 wrapLineFlag
= true;
1060 * This block marks the case that we are in the margin
1061 * and the second character has been received and
1064 wrapLineFlag
= false;
1068 } else if (currentState
.cursorX
<= rightMargin
) {
1070 * This is the normal case: a character came in and was printed
1071 * to the left of the right margin column.
1074 // Turn off VT100 special-case flag
1075 wrapLineFlag
= false;
1078 // "Print" the character
1079 Cell newCell
= new Cell(ch
);
1080 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1081 newCellAttributes
.setTo(currentState
.attr
);
1082 DisplayLine line
= display
.get(currentState
.cursorY
);
1083 // Insert mode special case
1084 if (insertMode
== true) {
1085 line
.insert(currentState
.cursorX
, newCell
);
1087 // Replace an existing character
1088 line
.replace(currentState
.cursorX
, newCell
);
1091 // Increment horizontal
1092 if (wrapLineFlag
== false) {
1093 currentState
.cursorX
++;
1094 if (currentState
.cursorX
> rightMargin
) {
1095 currentState
.cursorX
--;
1101 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1102 * send to the remote side.
1104 * @param mouse mouse event received from the local user
1106 public void mouse(final TMouseEvent mouse
) {
1109 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1110 mouseProtocol, mouseEncoding, mouse);
1113 if (mouseEncoding
== MouseEncoding
.X10
) {
1114 // We will support X10 but only for (160,94) and smaller.
1115 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1120 switch (mouseProtocol
) {
1127 // Only report button presses
1128 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1134 // Only report button presses and releases
1135 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1136 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1144 * Only report button presses, button releases, and motions that
1145 * have a button down (i.e. drag-and-drop).
1147 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1148 if (!mouse
.isMouse1()
1149 && !mouse
.isMouse2()
1150 && !mouse
.isMouse3()
1151 && !mouse
.isMouseWheelUp()
1152 && !mouse
.isMouseWheelDown()
1160 // Report everything
1164 // Now encode the event
1165 StringBuilder sb
= new StringBuilder(6);
1166 if (mouseEncoding
== MouseEncoding
.SGR
) {
1167 sb
.append((char) 0x1B);
1170 if (mouse
.isMouse1()) {
1171 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1176 } else if (mouse
.isMouse2()) {
1177 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1182 } else if (mouse
.isMouse3()) {
1183 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1188 } else if (mouse
.isMouseWheelUp()) {
1190 } else if (mouse
.isMouseWheelDown()) {
1193 // This is motion with no buttons down.
1197 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1200 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1207 // X10 and UTF8 encodings
1208 sb
.append((char) 0x1B);
1211 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1212 sb
.append((char) (0x03 + 32));
1213 } else if (mouse
.isMouse1()) {
1214 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1215 sb
.append((char) (0x00 + 32 + 32));
1217 sb
.append((char) (0x00 + 32));
1219 } else if (mouse
.isMouse2()) {
1220 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1221 sb
.append((char) (0x01 + 32 + 32));
1223 sb
.append((char) (0x01 + 32));
1225 } else if (mouse
.isMouse3()) {
1226 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1227 sb
.append((char) (0x02 + 32 + 32));
1229 sb
.append((char) (0x02 + 32));
1231 } else if (mouse
.isMouseWheelUp()) {
1232 sb
.append((char) (0x04 + 64));
1233 } else if (mouse
.isMouseWheelDown()) {
1234 sb
.append((char) (0x05 + 64));
1236 // This is motion with no buttons down.
1237 sb
.append((char) (0x03 + 32));
1240 sb
.append((char) (mouse
.getX() + 33));
1241 sb
.append((char) (mouse
.getY() + 33));
1244 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1245 writeRemote(sb
.toString());
1249 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1250 * send to the remote side.
1252 * @param keypress keypress received from the local user
1254 public void keypress(final TKeypress keypress
) {
1255 writeRemote(keypressToString(keypress
));
1259 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1261 * @param keypress keypress received from the local user
1262 * @return string to transmit to the remote side
1264 private String
keypressToString(final TKeypress keypress
) {
1266 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1268 * If this is a control character, process it like it came from
1271 if (keypress
.getChar() < 0x20) {
1272 handleControlChar(keypress
.getChar());
1274 // Local echo for everything else
1275 printCharacter(keypress
.getChar());
1279 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1284 // Handle control characters
1285 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1286 StringBuilder sb
= new StringBuilder();
1287 char ch
= keypress
.getChar();
1290 return sb
.toString();
1293 // Handle alt characters
1294 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1295 StringBuilder sb
= new StringBuilder("\033");
1296 char ch
= keypress
.getChar();
1298 return sb
.toString();
1301 if (keypress
.equals(kbBackspace
)) {
1314 if (keypress
.equals(kbLeft
)) {
1315 switch (arrowKeyMode
) {
1325 if (keypress
.equals(kbRight
)) {
1326 switch (arrowKeyMode
) {
1336 if (keypress
.equals(kbUp
)) {
1337 switch (arrowKeyMode
) {
1347 if (keypress
.equals(kbDown
)) {
1348 switch (arrowKeyMode
) {
1358 if (keypress
.equals(kbHome
)) {
1359 switch (arrowKeyMode
) {
1369 if (keypress
.equals(kbEnd
)) {
1370 switch (arrowKeyMode
) {
1380 if (keypress
.equals(kbF1
)) {
1388 if (keypress
.equals(kbF2
)) {
1396 if (keypress
.equals(kbF3
)) {
1404 if (keypress
.equals(kbF4
)) {
1412 if (keypress
.equals(kbF5
)) {
1425 if (keypress
.equals(kbF6
)) {
1438 if (keypress
.equals(kbF7
)) {
1451 if (keypress
.equals(kbF8
)) {
1464 if (keypress
.equals(kbF9
)) {
1477 if (keypress
.equals(kbF10
)) {
1490 if (keypress
.equals(kbF11
)) {
1494 if (keypress
.equals(kbF12
)) {
1498 if (keypress
.equals(kbShiftF1
)) {
1506 if (keypress
.equals(kbShiftF2
)) {
1514 if (keypress
.equals(kbShiftF3
)) {
1522 if (keypress
.equals(kbShiftF4
)) {
1530 if (keypress
.equals(kbShiftF5
)) {
1532 return "\033[15;2~";
1535 if (keypress
.equals(kbShiftF6
)) {
1537 return "\033[17;2~";
1540 if (keypress
.equals(kbShiftF7
)) {
1542 return "\033[18;2~";
1545 if (keypress
.equals(kbShiftF8
)) {
1547 return "\033[19;2~";
1550 if (keypress
.equals(kbShiftF9
)) {
1552 return "\033[20;2~";
1555 if (keypress
.equals(kbShiftF10
)) {
1557 return "\033[21;2~";
1560 if (keypress
.equals(kbShiftF11
)) {
1562 return "\033[23;2~";
1565 if (keypress
.equals(kbShiftF12
)) {
1567 return "\033[24;2~";
1570 if (keypress
.equals(kbCtrlF1
)) {
1578 if (keypress
.equals(kbCtrlF2
)) {
1586 if (keypress
.equals(kbCtrlF3
)) {
1594 if (keypress
.equals(kbCtrlF4
)) {
1602 if (keypress
.equals(kbCtrlF5
)) {
1604 return "\033[15;5~";
1607 if (keypress
.equals(kbCtrlF6
)) {
1609 return "\033[17;5~";
1612 if (keypress
.equals(kbCtrlF7
)) {
1614 return "\033[18;5~";
1617 if (keypress
.equals(kbCtrlF8
)) {
1619 return "\033[19;5~";
1622 if (keypress
.equals(kbCtrlF9
)) {
1624 return "\033[20;5~";
1627 if (keypress
.equals(kbCtrlF10
)) {
1629 return "\033[21;5~";
1632 if (keypress
.equals(kbCtrlF11
)) {
1634 return "\033[23;5~";
1637 if (keypress
.equals(kbCtrlF12
)) {
1639 return "\033[24;5~";
1642 if (keypress
.equals(kbPgUp
)) {
1647 if (keypress
.equals(kbPgDn
)) {
1652 if (keypress
.equals(kbIns
)) {
1657 if (keypress
.equals(kbShiftIns
)) {
1658 // This is what xterm sends for SHIFT-INS
1660 // This is what xterm sends for CTRL-INS
1661 // return "\033[2;5~";
1664 if (keypress
.equals(kbShiftDel
)) {
1665 // This is what xterm sends for SHIFT-DEL
1667 // This is what xterm sends for CTRL-DEL
1668 // return "\033[3;5~";
1671 if (keypress
.equals(kbDel
)) {
1672 // Delete sends real delete for VTxxx
1674 // return "\033[3~";
1677 if (keypress
.equals(kbEnter
)) {
1681 if (keypress
.equals(kbEsc
)) {
1685 if (keypress
.equals(kbAltEsc
)) {
1689 if (keypress
.equals(kbTab
)) {
1693 // Non-alt, non-ctrl characters
1694 if (!keypress
.isFnKey()) {
1695 StringBuilder sb
= new StringBuilder();
1696 sb
.append(keypress
.getChar());
1697 return sb
.toString();
1703 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1706 * @param ch 8-bit character from the remote side
1707 * @param charsetGl character set defined for GL
1708 * @param charsetGr character set defined for GR
1709 * @return character to display on the screen
1711 private char mapCharacterCharset(final char ch
,
1712 final CharacterSet charsetGl
,
1713 final CharacterSet charsetGr
) {
1715 int lookupChar
= ch
;
1716 CharacterSet lookupCharset
= charsetGl
;
1719 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
1720 lookupCharset
= charsetGr
;
1724 switch (lookupCharset
) {
1727 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
1730 return DECCharacterSets
.UK
[lookupChar
];
1733 return DECCharacterSets
.US_ASCII
[lookupChar
];
1736 return DECCharacterSets
.NL
[lookupChar
];
1739 return DECCharacterSets
.FI
[lookupChar
];
1742 return DECCharacterSets
.FR
[lookupChar
];
1745 return DECCharacterSets
.FR_CA
[lookupChar
];
1748 return DECCharacterSets
.DE
[lookupChar
];
1751 return DECCharacterSets
.IT
[lookupChar
];
1754 return DECCharacterSets
.NO
[lookupChar
];
1757 return DECCharacterSets
.ES
[lookupChar
];
1760 return DECCharacterSets
.SV
[lookupChar
];
1763 return DECCharacterSets
.SWISS
[lookupChar
];
1765 case DEC_SUPPLEMENTAL
:
1766 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
1769 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
1772 return DECCharacterSets
.US_ASCII
[lookupChar
];
1775 return DECCharacterSets
.US_ASCII
[lookupChar
];
1778 throw new IllegalArgumentException("Invalid character set value: "
1784 * Map an 8-bit byte into a printable character.
1786 * @param ch either 8-bit or Unicode character from the remote side
1787 * @return character to display on the screen
1789 private char mapCharacter(final char ch
) {
1791 // Unicode character, just return it
1795 CharacterSet charsetGl
= currentState
.g0Charset
;
1796 CharacterSet charsetGr
= currentState
.grCharset
;
1798 if (vt52Mode
== true) {
1799 if (shiftOut
== true) {
1800 // Shifted out character, pull from VT52 graphics
1801 charsetGl
= currentState
.g1Charset
;
1802 charsetGr
= CharacterSet
.US
;
1805 charsetGl
= currentState
.g0Charset
;
1806 charsetGr
= CharacterSet
.US
;
1809 // Pull the character
1810 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1814 if (shiftOut
== true) {
1815 // Shifted out character, pull from G1
1816 charsetGl
= currentState
.g1Charset
;
1817 charsetGr
= currentState
.grCharset
;
1819 // Pull the character
1820 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1824 if (singleshift
== Singleshift
.SS2
) {
1826 singleshift
= Singleshift
.NONE
;
1828 // Shifted out character, pull from G2
1829 charsetGl
= currentState
.g2Charset
;
1830 charsetGr
= currentState
.grCharset
;
1834 if (singleshift
== Singleshift
.SS3
) {
1836 singleshift
= Singleshift
.NONE
;
1838 // Shifted out character, pull from G3
1839 charsetGl
= currentState
.g3Charset
;
1840 charsetGr
= currentState
.grCharset
;
1843 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
1844 // Check for locking shift
1846 switch (currentState
.glLockshift
) {
1859 charsetGl
= currentState
.g2Charset
;
1864 charsetGl
= currentState
.g3Charset
;
1869 charsetGl
= currentState
.g0Charset
;
1873 switch (currentState
.grLockshift
) {
1883 charsetGr
= currentState
.g1Charset
;
1888 charsetGr
= currentState
.g2Charset
;
1893 charsetGr
= currentState
.g3Charset
;
1898 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
1905 // Pull the character
1906 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
1910 * Scroll the text within a scrolling region up n lines.
1912 * @param regionTop top row of the scrolling region
1913 * @param regionBottom bottom row of the scrolling region
1914 * @param n number of lines to scroll
1916 private void scrollingRegionScrollUp(final int regionTop
,
1917 final int regionBottom
, final int n
) {
1919 if (regionTop
>= regionBottom
) {
1923 // Sanity check: see if there will be any characters left after the
1925 if (regionBottom
+ 1 - regionTop
<= n
) {
1926 // There won't be anything left in the region, so just call
1927 // eraseScreen() and return.
1928 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1932 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1933 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1934 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1936 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
1937 - remaining
, regionBottom
+ 1);
1938 display
= new LinkedList
<DisplayLine
>(displayTop
);
1939 display
.addAll(displayMiddle
);
1940 for (int i
= 0; i
< n
; i
++) {
1941 DisplayLine line
= new DisplayLine(currentState
.attr
);
1942 line
.setReverseColor(reverseVideo
);
1945 display
.addAll(displayBottom
);
1947 assert (display
.size() == height
);
1951 * Scroll the text within a scrolling region down n lines.
1953 * @param regionTop top row of the scrolling region
1954 * @param regionBottom bottom row of the scrolling region
1955 * @param n number of lines to scroll
1957 private void scrollingRegionScrollDown(final int regionTop
,
1958 final int regionBottom
, final int n
) {
1960 if (regionTop
>= regionBottom
) {
1964 // Sanity check: see if there will be any characters left after the
1966 if (regionBottom
+ 1 - regionTop
<= n
) {
1967 // There won't be anything left in the region, so just call
1968 // eraseScreen() and return.
1969 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
1973 int remaining
= regionBottom
+ 1 - regionTop
- n
;
1974 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
1975 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
1977 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
1978 regionTop
+ remaining
);
1979 display
= new LinkedList
<DisplayLine
>(displayTop
);
1980 for (int i
= 0; i
< n
; i
++) {
1981 DisplayLine line
= new DisplayLine(currentState
.attr
);
1982 line
.setReverseColor(reverseVideo
);
1985 display
.addAll(displayMiddle
);
1986 display
.addAll(displayBottom
);
1988 assert (display
.size() == height
);
1992 * Process a control character.
1994 * @param ch 8-bit character from the remote side
1996 private void handleControlChar(final char ch
) {
1997 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2008 // Transmit the answerback message.
2019 cursorLeft(1, false);
2024 advanceToNextTabStop();
2050 currentState
.glLockshift
= LockshiftMode
.NONE
;
2056 currentState
.glLockshift
= LockshiftMode
.NONE
;
2081 singleshift
= Singleshift
.SS2
;
2086 singleshift
= Singleshift
.SS3
;
2096 * Advance the cursor to the next tab stop.
2098 private void advanceToNextTabStop() {
2099 if (tabStops
.size() == 0) {
2100 // Go to the rightmost column
2101 cursorRight(rightMargin
- currentState
.cursorX
, false);
2104 for (Integer stop
: tabStops
) {
2105 if (stop
> currentState
.cursorX
) {
2106 cursorRight(stop
- currentState
.cursorX
, false);
2111 * We got here, meaning there isn't a tab stop beyond the current
2112 * cursor position. Place the cursor of the right-most edge of the
2115 cursorRight(rightMargin
- currentState
.cursorX
, false);
2119 * Save a character into the collect buffer.
2121 * @param ch character to save
2123 private void collect(final char ch
) {
2124 collectBuffer
.append(ch
);
2128 * Save a byte into the CSI parameters buffer.
2130 * @param ch byte to save
2132 private void param(final byte ch
) {
2133 if (csiParams
.size() == 0) {
2134 csiParams
.add(new Integer(0));
2136 Integer x
= csiParams
.get(csiParams
.size() - 1);
2137 if ((ch
>= '0') && (ch
<= '9')) {
2140 csiParams
.set(csiParams
.size() - 1, x
);
2144 csiParams
.add(new Integer(0));
2149 * Get a CSI parameter value, with a default.
2151 * @param position parameter index. 0 is the first parameter.
2152 * @param defaultValue value to use if csiParams[position] doesn't exist
2153 * @return parameter value
2155 private int getCsiParam(final int position
, final int defaultValue
) {
2156 if (csiParams
.size() < position
+ 1) {
2157 return defaultValue
;
2159 return csiParams
.get(position
).intValue();
2163 * Get a CSI parameter value, clamped to within min/max.
2165 * @param position parameter index. 0 is the first parameter.
2166 * @param defaultValue value to use if csiParams[position] doesn't exist
2167 * @param minValue minimum value inclusive
2168 * @param maxValue maximum value inclusive
2169 * @return parameter value
2171 private int getCsiParam(final int position
, final int defaultValue
,
2172 final int minValue
, final int maxValue
) {
2174 assert (minValue
<= maxValue
);
2175 int value
= getCsiParam(position
, defaultValue
);
2176 if (value
< minValue
) {
2179 if (value
> maxValue
) {
2186 * Set or unset a toggle.
2188 * @param value true for set ('h'), false for reset ('l')
2190 private void setToggle(final boolean value
) {
2191 boolean decPrivateModeFlag
= false;
2192 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2193 if (collectBuffer
.charAt(i
) == '?') {
2194 decPrivateModeFlag
= true;
2199 for (Integer i
: csiParams
) {
2204 if (decPrivateModeFlag
== true) {
2206 if (value
== true) {
2207 // Use application arrow keys
2208 arrowKeyMode
= ArrowKeyMode
.VT100
;
2210 // Use ANSI arrow keys
2211 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2216 if (decPrivateModeFlag
== true) {
2217 if (value
== false) {
2221 arrowKeyMode
= ArrowKeyMode
.VT52
;
2224 * From the VT102 docs: "You use ANSI mode to select
2225 * most terminal features; the terminal uses the same
2226 * features when it switches to VT52 mode. You
2227 * cannot, however, change most of these features in
2230 * In other words, do not reset any other attributes
2231 * when switching between VT52 submode and ANSI.
2233 * HOWEVER, the real vt100 does switch the character
2234 * set according to Usenet.
2236 currentState
.g0Charset
= CharacterSet
.US
;
2237 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2240 if ((type
== DeviceType
.VT220
)
2241 || (type
== DeviceType
.XTERM
)) {
2243 // VT52 mode is explicitly 7-bit
2245 singleshift
= Singleshift
.NONE
;
2250 if (value
== true) {
2251 // Turn off keyboard
2260 if (decPrivateModeFlag
== true) {
2262 if (value
== true) {
2271 width
= rightMargin
+ 1;
2272 // Entire screen is cleared, and scrolling region is
2274 eraseScreen(0, 0, height
- 1, width
- 1, false);
2275 scrollRegionTop
= 0;
2276 scrollRegionBottom
= height
- 1;
2277 // Also home the cursor
2278 cursorPosition(0, 0);
2282 if (decPrivateModeFlag
== true) {
2284 if (value
== true) {
2293 if (value
== true) {
2301 if (decPrivateModeFlag
== true) {
2303 if (value
== true) {
2305 * Set selects reverse screen, a white screen
2306 * background with black characters.
2308 if (reverseVideo
!= true) {
2310 * If in normal video, switch it back
2312 invertDisplayColors();
2314 reverseVideo
= true;
2317 * Reset selects normal screen, a black screen
2318 * background with white characters.
2320 if (reverseVideo
== true) {
2322 * If in reverse video already, switch it back
2324 invertDisplayColors();
2326 reverseVideo
= false;
2331 if (decPrivateModeFlag
== true) {
2333 if (value
== true) {
2334 // Origin is relative to scroll region cursor.
2335 // Cursor can NEVER leave scrolling region.
2336 currentState
.originMode
= true;
2337 cursorPosition(0, 0);
2339 // Origin is absolute to entire screen. Cursor can
2340 // leave the scrolling region via cup() and hvp().
2341 currentState
.originMode
= false;
2342 cursorPosition(0, 0);
2347 if (decPrivateModeFlag
== true) {
2349 if (value
== true) {
2351 currentState
.lineWrap
= true;
2353 // Turn linewrap off
2354 currentState
.lineWrap
= false;
2359 if (decPrivateModeFlag
== true) {
2361 if (value
== true) {
2362 // Keyboard auto-repeat on
2365 // Keyboard auto-repeat off
2371 if (decPrivateModeFlag
== false) {
2373 if (value
== true) {
2383 if (decPrivateModeFlag
== true) {
2389 if (decPrivateModeFlag
== true) {
2395 if (decPrivateModeFlag
== false) {
2397 if (value
== true) {
2399 * Set causes a received linefeed, form feed, or
2400 * vertical tab to move cursor to first column of
2401 * next line. RETURN transmits both a carriage return
2402 * and linefeed. This selection is also called new
2408 * Reset causes a received linefeed, form feed, or
2409 * vertical tab to move cursor to next line in
2410 * current column. RETURN transmits a carriage
2413 newLineMode
= false;
2419 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2420 if (decPrivateModeFlag
== true) {
2422 if (value
== true) {
2424 cursorVisible
= true;
2427 cursorVisible
= false;
2434 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2435 if (decPrivateModeFlag
== true) {
2437 if (value
== true) {
2438 // Select national mode NRC
2441 // Select multi-national mode
2450 if ((type
== DeviceType
.XTERM
)
2451 && (decPrivateModeFlag
== true)
2453 // Mouse: normal tracking mode
2454 if (value
== true) {
2455 mouseProtocol
= MouseProtocol
.NORMAL
;
2457 mouseProtocol
= MouseProtocol
.OFF
;
2463 if ((type
== DeviceType
.XTERM
)
2464 && (decPrivateModeFlag
== true)
2466 // Mouse: normal tracking mode
2467 if (value
== true) {
2468 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2470 mouseProtocol
= MouseProtocol
.OFF
;
2476 if ((type
== DeviceType
.XTERM
)
2477 && (decPrivateModeFlag
== true)
2479 // Mouse: Any-event tracking mode
2480 if (value
== true) {
2481 mouseProtocol
= MouseProtocol
.ANYEVENT
;
2483 mouseProtocol
= MouseProtocol
.OFF
;
2489 if ((type
== DeviceType
.XTERM
)
2490 && (decPrivateModeFlag
== true)
2492 // Mouse: UTF-8 coordinates
2493 if (value
== true) {
2494 mouseEncoding
= MouseEncoding
.UTF8
;
2496 mouseEncoding
= MouseEncoding
.X10
;
2502 if ((type
== DeviceType
.XTERM
)
2503 && (decPrivateModeFlag
== true)
2505 // Mouse: SGR coordinates
2506 if (value
== true) {
2507 mouseEncoding
= MouseEncoding
.SGR
;
2509 mouseEncoding
= MouseEncoding
.X10
;
2522 * DECSC - Save cursor.
2524 private void decsc() {
2525 savedState
.setTo(currentState
);
2529 * DECRC - Restore cursor.
2531 private void decrc() {
2532 currentState
.setTo(savedState
);
2538 private void ind() {
2539 // Move the cursor and scroll if necessary. If at the bottom line
2540 // already, a scroll up is supposed to be performed.
2541 if (currentState
.cursorY
== scrollRegionBottom
) {
2542 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2544 cursorDown(1, true);
2548 * RI - Reverse index.
2551 // Move the cursor and scroll if necessary. If at the top line
2552 // already, a scroll down is supposed to be performed.
2553 if (currentState
.cursorY
== scrollRegionTop
) {
2554 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2562 private void nel() {
2563 // Move the cursor and scroll if necessary. If at the bottom line
2564 // already, a scroll up is supposed to be performed.
2565 if (currentState
.cursorY
== scrollRegionBottom
) {
2566 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2568 cursorDown(1, true);
2570 // Reset to the beginning of the next line
2571 currentState
.cursorX
= 0;
2575 * DECKPAM - Keypad application mode.
2577 private void deckpam() {
2578 keypadMode
= KeypadMode
.Application
;
2582 * DECKPNM - Keypad numeric mode.
2584 private void deckpnm() {
2585 keypadMode
= KeypadMode
.Numeric
;
2591 * @param n number of spaces to move
2592 * @param honorScrollRegion if true, then do nothing if the cursor is
2593 * outside the scrolling region
2595 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2599 * Special case: if a user moves the cursor from the right margin, we
2600 * have to reset the VT100 right margin flag.
2603 wrapLineFlag
= false;
2606 for (int i
= 0; i
< n
; i
++) {
2607 if (honorScrollRegion
== true) {
2608 // Honor the scrolling region
2609 if ((currentState
.cursorY
< scrollRegionTop
)
2610 || (currentState
.cursorY
> scrollRegionBottom
)
2612 // Outside region, do nothing
2615 // Inside region, go up
2616 top
= scrollRegionTop
;
2618 // Non-scrolling case
2622 if (currentState
.cursorY
> top
) {
2623 currentState
.cursorY
--;
2629 * Move down n spaces.
2631 * @param n number of spaces to move
2632 * @param honorScrollRegion if true, then do nothing if the cursor is
2633 * outside the scrolling region
2635 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
2639 * Special case: if a user moves the cursor from the right margin, we
2640 * have to reset the VT100 right margin flag.
2643 wrapLineFlag
= false;
2646 for (int i
= 0; i
< n
; i
++) {
2648 if (honorScrollRegion
== true) {
2649 // Honor the scrolling region
2650 if (currentState
.cursorY
> scrollRegionBottom
) {
2651 // Outside region, do nothing
2654 // Inside region, go down
2655 bottom
= scrollRegionBottom
;
2657 // Non-scrolling case
2658 bottom
= height
- 1;
2661 if (currentState
.cursorY
< bottom
) {
2662 currentState
.cursorY
++;
2668 * Move left n spaces.
2670 * @param n number of spaces to move
2671 * @param honorScrollRegion if true, then do nothing if the cursor is
2672 * outside the scrolling region
2674 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
2676 * Special case: if a user moves the cursor from the right margin, we
2677 * have to reset the VT100 right margin flag.
2680 wrapLineFlag
= false;
2683 for (int i
= 0; i
< n
; i
++) {
2684 if (honorScrollRegion
== true) {
2685 // Honor the scrolling region
2686 if ((currentState
.cursorY
< scrollRegionTop
)
2687 || (currentState
.cursorY
> scrollRegionBottom
)
2689 // Outside region, do nothing
2694 if (currentState
.cursorX
> 0) {
2695 currentState
.cursorX
--;
2701 * Move right n spaces.
2703 * @param n number of spaces to move
2704 * @param honorScrollRegion if true, then do nothing if the cursor is
2705 * outside the scrolling region
2707 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
2708 int rightMargin
= this.rightMargin
;
2711 * Special case: if a user moves the cursor from the right margin, we
2712 * have to reset the VT100 right margin flag.
2715 wrapLineFlag
= false;
2718 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2719 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2722 for (int i
= 0; i
< n
; i
++) {
2723 if (honorScrollRegion
== true) {
2724 // Honor the scrolling region
2725 if ((currentState
.cursorY
< scrollRegionTop
)
2726 || (currentState
.cursorY
> scrollRegionBottom
)
2728 // Outside region, do nothing
2733 if (currentState
.cursorX
< rightMargin
) {
2734 currentState
.cursorX
++;
2740 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2742 * @param row row to move to
2743 * @param col column to move to
2745 private void cursorPosition(int row
, final int col
) {
2746 int rightMargin
= this.rightMargin
;
2751 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
2752 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
2755 // Set column number
2756 currentState
.cursorX
= col
;
2758 // Sanity check, bring column back to margin.
2759 if (currentState
.cursorX
> rightMargin
) {
2760 currentState
.cursorX
= rightMargin
;
2764 if (currentState
.originMode
== true) {
2765 row
+= scrollRegionTop
;
2767 if (currentState
.cursorY
< row
) {
2768 cursorDown(row
- currentState
.cursorY
, false);
2769 } else if (currentState
.cursorY
> row
) {
2770 cursorUp(currentState
.cursorY
- row
, false);
2773 wrapLineFlag
= false;
2777 * HTS - Horizontal tabulation set.
2779 private void hts() {
2780 for (Integer stop
: tabStops
) {
2781 if (stop
== currentState
.cursorX
) {
2782 // Already have a tab stop here
2787 // Append a tab stop to the end of the array and resort them
2788 tabStops
.add(currentState
.cursorX
);
2789 Collections
.sort(tabStops
);
2793 * DECSWL - Single-width line.
2795 private void decswl() {
2796 display
.get(currentState
.cursorY
).setDoubleWidth(false);
2797 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2801 * DECDWL - Double-width line.
2803 private void decdwl() {
2804 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2805 display
.get(currentState
.cursorY
).setDoubleHeight(0);
2809 * DECHDL - Double-height + double-width line.
2811 * @param topHalf if true, this sets the row to be the top half row of a
2814 private void dechdl(final boolean topHalf
) {
2815 display
.get(currentState
.cursorY
).setDoubleWidth(true);
2816 if (topHalf
== true) {
2817 display
.get(currentState
.cursorY
).setDoubleHeight(1);
2819 display
.get(currentState
.cursorY
).setDoubleHeight(2);
2824 * DECALN - Screen alignment display.
2826 private void decaln() {
2827 Cell newCell
= new Cell();
2828 newCell
.setChar('E');
2829 for (DisplayLine line
: display
) {
2830 for (int i
= 0; i
< line
.length(); i
++) {
2831 line
.replace(i
, newCell
);
2837 * DECSCL - Compatibility level.
2839 private void decscl() {
2840 int i
= getCsiParam(0, 0);
2841 int j
= getCsiParam(1, 0);
2845 currentState
.g0Charset
= CharacterSet
.US
;
2846 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2848 } else if (i
== 62) {
2850 if ((j
== 0) || (j
== 2)) {
2852 } else if (j
== 1) {
2859 * CUD - Cursor down.
2861 private void cud() {
2862 cursorDown(getCsiParam(0, 1, 1, height
), true);
2866 * CUF - Cursor forward.
2868 private void cuf() {
2869 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
2873 * CUB - Cursor backward.
2875 private void cub() {
2876 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
2882 private void cuu() {
2883 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2887 * CUP - Cursor position.
2889 private void cup() {
2890 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2891 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
2895 * CNL - Cursor down and to column 1.
2897 private void cnl() {
2898 cursorDown(getCsiParam(0, 1, 1, height
), true);
2900 cursorLeft(currentState
.cursorX
, true);
2904 * CPL - Cursor up and to column 1.
2906 private void cpl() {
2907 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
2909 cursorLeft(currentState
.cursorX
, true);
2913 * CHA - Cursor to column # in current row.
2915 private void cha() {
2916 cursorPosition(currentState
.cursorY
,
2917 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
2921 * VPA - Cursor to row #, same column.
2923 private void vpa() {
2924 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
2925 currentState
.cursorX
);
2929 * ED - Erase in display.
2932 boolean honorProtected
= false;
2933 boolean decPrivateModeFlag
= false;
2935 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2936 if (collectBuffer
.charAt(i
) == '?') {
2937 decPrivateModeFlag
= true;
2942 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2943 && (decPrivateModeFlag
== true)
2945 honorProtected
= true;
2948 int i
= getCsiParam(0, 0);
2951 // Erase from here to end of screen
2952 if (currentState
.cursorY
< height
- 1) {
2953 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
2956 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2957 } else if (i
== 1) {
2958 // Erase from beginning of screen to here
2959 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
2961 eraseLine(0, currentState
.cursorX
, honorProtected
);
2962 } else if (i
== 2) {
2963 // Erase entire screen
2964 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
2969 * EL - Erase in line.
2972 boolean honorProtected
= false;
2973 boolean decPrivateModeFlag
= false;
2975 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2976 if (collectBuffer
.charAt(i
) == '?') {
2977 decPrivateModeFlag
= true;
2982 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
2983 && (decPrivateModeFlag
== true)
2985 honorProtected
= true;
2988 int i
= getCsiParam(0, 0);
2991 // Erase from here to end of line
2992 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
2993 } else if (i
== 1) {
2994 // Erase from beginning of line to here
2995 eraseLine(0, currentState
.cursorX
, honorProtected
);
2996 } else if (i
== 2) {
2997 // Erase entire line
2998 eraseLine(0, width
- 1, honorProtected
);
3003 * ECH - Erase # of characters in current row.
3005 private void ech() {
3006 int i
= getCsiParam(0, 1, 1, width
);
3008 // Erase from here to i characters
3009 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3016 int i
= getCsiParam(0, 1);
3018 if ((currentState
.cursorY
>= scrollRegionTop
)
3019 && (currentState
.cursorY
<= scrollRegionBottom
)
3022 // I can get the same effect with a scroll-down
3023 scrollingRegionScrollDown(currentState
.cursorY
,
3024 scrollRegionBottom
, i
);
3029 * DCH - Delete char.
3031 private void dch() {
3032 int n
= getCsiParam(0, 1);
3033 DisplayLine line
= display
.get(currentState
.cursorY
);
3034 Cell blank
= new Cell();
3035 for (int i
= 0; i
< n
; i
++) {
3036 line
.delete(currentState
.cursorX
, blank
);
3041 * ICH - Insert blank char at cursor.
3043 private void ich() {
3044 int n
= getCsiParam(0, 1);
3045 DisplayLine line
= display
.get(currentState
.cursorY
);
3046 Cell blank
= new Cell();
3047 for (int i
= 0; i
< n
; i
++) {
3048 line
.insert(currentState
.cursorX
, blank
);
3056 int i
= getCsiParam(0, 1);
3058 if ((currentState
.cursorY
>= scrollRegionTop
)
3059 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3061 // I can get the same effect with a scroll-down
3062 scrollingRegionScrollUp(currentState
.cursorY
,
3063 scrollRegionBottom
, i
);
3068 * HVP - Horizontal and vertical position.
3070 private void hvp() {
3075 * REP - Repeat character.
3077 private void rep() {
3078 int n
= getCsiParam(0, 1);
3079 for (int i
= 0; i
< n
; i
++) {
3080 printCharacter(repCh
);
3088 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3089 getCsiParam(0, 1, 1, height
));
3096 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3097 getCsiParam(0, 1, 1, height
));
3101 * CBT - Go back X tab stops.
3103 private void cbt() {
3104 int tabsToMove
= getCsiParam(0, 1);
3107 for (int i
= 0; i
< tabsToMove
; i
++) {
3108 int j
= currentState
.cursorX
;
3109 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3110 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3118 j
= tabStops
.get(tabI
);
3120 cursorPosition(currentState
.cursorY
, j
);
3125 * CHT - Advance X tab stops.
3127 private void cht() {
3128 int n
= getCsiParam(0, 1);
3129 for (int i
= 0; i
< n
; i
++) {
3130 advanceToNextTabStop();
3135 * SGR - Select graphics rendition.
3137 private void sgr() {
3139 if (csiParams
.size() == 0) {
3140 currentState
.attr
.reset();
3144 for (Integer i
: csiParams
) {
3150 currentState
.attr
.reset();
3155 currentState
.attr
.setBold(true);
3160 currentState
.attr
.setUnderline(true);
3165 currentState
.attr
.setBlink(true);
3170 currentState
.attr
.setReverse(true);
3177 if (type
== DeviceType
.XTERM
) {
3191 if ((type
== DeviceType
.VT220
)
3192 || (type
== DeviceType
.XTERM
)) {
3198 currentState
.attr
.setBold(false);
3203 currentState
.attr
.setUnderline(false);
3208 currentState
.attr
.setBlink(false);
3213 currentState
.attr
.setReverse(false);
3221 // A true VT100/102/220 does not support color, however everyone
3222 // is used to their terminal emulator supporting color so we will
3223 // unconditionally support color for all DeviceType's.
3228 // Set black foreground
3229 currentState
.attr
.setForeColor(Color
.BLACK
);
3232 // Set red foreground
3233 currentState
.attr
.setForeColor(Color
.RED
);
3236 // Set green foreground
3237 currentState
.attr
.setForeColor(Color
.GREEN
);
3240 // Set yellow foreground
3241 currentState
.attr
.setForeColor(Color
.YELLOW
);
3244 // Set blue foreground
3245 currentState
.attr
.setForeColor(Color
.BLUE
);
3248 // Set magenta foreground
3249 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3252 // Set cyan foreground
3253 currentState
.attr
.setForeColor(Color
.CYAN
);
3256 // Set white foreground
3257 currentState
.attr
.setForeColor(Color
.WHITE
);
3260 if (type
== DeviceType
.XTERM
) {
3262 * Xterm supports T.416 / ISO-8613-3 codes to select
3263 * either an indexed color or an RGB value. (It also
3264 * permits these ISO-8613-3 SGR sequences to be separated
3265 * by colons rather than semicolons.)
3267 * We will not support any of these additional color
3268 * codes at this time:
3270 * 1. http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
3271 * has a detailed discussion of the current state of
3272 * RGB in various terminals, the point of which is
3273 * that none of them really do the same thing despite
3274 * all appearing to be "xterm".
3277 * https://bugs.kde.org/show_bug.cgi?id=107487#c3,
3278 * even supporting just the "indexed mode" of these
3279 * sequences (which could align easily with existing
3280 * SGR colors) is assumed to mean full support of
3281 * 24-bit RGB. So it is all or nothing.
3283 * Finally, these sequences break the assumptions of
3284 * standard ECMA-48 style parsers as pointed out at
3285 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
3286 * Therefore in order to keep a clean display, we cannot
3287 * parse anything else in this sequence.
3291 // Underscore on, default foreground color
3292 currentState
.attr
.setUnderline(true);
3293 currentState
.attr
.setForeColor(Color
.WHITE
);
3297 // Underscore off, default foreground color
3298 currentState
.attr
.setUnderline(false);
3299 currentState
.attr
.setForeColor(Color
.WHITE
);
3302 // Set black background
3303 currentState
.attr
.setBackColor(Color
.BLACK
);
3306 // Set red background
3307 currentState
.attr
.setBackColor(Color
.RED
);
3310 // Set green background
3311 currentState
.attr
.setBackColor(Color
.GREEN
);
3314 // Set yellow background
3315 currentState
.attr
.setBackColor(Color
.YELLOW
);
3318 // Set blue background
3319 currentState
.attr
.setBackColor(Color
.BLUE
);
3322 // Set magenta background
3323 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3326 // Set cyan background
3327 currentState
.attr
.setBackColor(Color
.CYAN
);
3330 // Set white background
3331 currentState
.attr
.setBackColor(Color
.WHITE
);
3334 if (type
== DeviceType
.XTERM
) {
3336 * Xterm supports T.416 / ISO-8613-3 codes to select
3337 * either an indexed color or an RGB value. (It also
3338 * permits these ISO-8613-3 SGR sequences to be separated
3339 * by colons rather than semicolons.)
3341 * We will not support this at this time. Also, in order
3342 * to keep a clean display, we cannot parse anything else
3349 // Default background
3350 currentState
.attr
.setBackColor(Color
.BLACK
);
3360 * DA - Device attributes.
3363 int extendedFlag
= 0;
3365 if (collectBuffer
.length() > 0) {
3366 String args
= collectBuffer
.substring(1);
3367 if (collectBuffer
.charAt(0) == '>') {
3369 if (collectBuffer
.length() >= 2) {
3370 i
= Integer
.parseInt(args
.toString());
3372 } else if (collectBuffer
.charAt(0) == '=') {
3374 if (collectBuffer
.length() >= 2) {
3375 i
= Integer
.parseInt(args
.toString());
3378 // Unknown code, bail out
3383 if ((i
!= 0) && (i
!= 1)) {
3387 if ((extendedFlag
== 0) && (i
== 0)) {
3388 // Send string directly to remote side
3389 writeRemote(deviceTypeResponse());
3393 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3395 if ((extendedFlag
== 1) && (i
== 0)) {
3397 * Request "What type of terminal are you, what is your
3398 * firmware version, and what hardware options do you have
3401 * Respond: "I am a VT220 (identification code of 1), my
3402 * firmware version is _____ (Pv), and I have _____ Po
3403 * options installed."
3409 if (s8c1t
== true) {
3410 writeRemote("\u009b>1;10;0c");
3412 writeRemote("\033[>1;10;0c");
3418 if ((extendedFlag
== 2) && (i
== 0)) {
3421 * Request "What is your unit ID?"
3423 * Respond: "I was manufactured at site 00 and have a unique ID
3427 writeRemote("\033P!|00010203\033\\");
3432 * DECSTBM - Set top and bottom margins.
3434 private void decstbm() {
3435 boolean decPrivateModeFlag
= false;
3437 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3438 if (collectBuffer
.charAt(i
) == '?') {
3439 decPrivateModeFlag
= true;
3443 if (decPrivateModeFlag
) {
3444 // This could be restore DEC private mode values.
3448 int top
= getCsiParam(0, 1, 1, height
) - 1;
3449 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3454 scrollRegionTop
= top
;
3455 scrollRegionBottom
= bottom
;
3458 cursorPosition(0, 0);
3463 * DECREQTPARM - Request terminal parameters.
3465 private void decreqtparm() {
3466 int i
= getCsiParam(0, 0);
3468 if ((i
!= 0) && (i
!= 1)) {
3475 * Request terminal parameters.
3479 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3480 * (CLoCk MULtiplier = 1, STP option flags = 0)
3484 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3487 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3489 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3495 * DECSCA - Select Character Attributes.
3497 private void decsca() {
3498 int i
= getCsiParam(0, 0);
3500 if ((i
== 0) || (i
== 2)) {
3502 currentState
.attr
.setProtect(false);
3506 currentState
.attr
.setProtect(true);
3511 * DECSTR - Soft Terminal Reset.
3513 private void decstr() {
3514 // Do exactly like RIS - Reset to initial state
3516 // Do I clear screen too? I think so...
3517 eraseScreen(0, 0, height
- 1, width
- 1, false);
3518 cursorPosition(0, 0);
3522 * DSR - Device status report.
3524 private void dsr() {
3525 boolean decPrivateModeFlag
= false;
3527 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3528 if (collectBuffer
.charAt(i
) == '?') {
3529 decPrivateModeFlag
= true;
3534 int i
= getCsiParam(0, 0);
3539 // Request status report. Respond with "OK, no malfunction."
3541 // Send string directly to remote side
3542 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3545 writeRemote("\u009b0n");
3547 writeRemote("\033[0n");
3552 // Request cursor position. Respond with current position.
3554 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3557 str
= String
.format("\u009b%d;%dR",
3558 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3560 str
= String
.format("\033[%d;%dR",
3561 currentState
.cursorY
+ 1, currentState
.cursorX
+ 1);
3564 // Send string directly to remote side
3569 if (decPrivateModeFlag
== true) {
3571 // Request printer status report. Respond with "Printer not
3574 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3575 && (s8c1t
== true)) {
3576 writeRemote("\u009b?13n");
3578 writeRemote("\033[?13n");
3584 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3585 && (decPrivateModeFlag
== true)
3588 // Request user-defined keys are locked or unlocked. Respond
3589 // with "User-defined keys are locked."
3591 if (s8c1t
== true) {
3592 writeRemote("\u009b?21n");
3594 writeRemote("\033[?21n");
3600 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3601 && (decPrivateModeFlag
== true)
3604 // Request keyboard language. Respond with "Keyboard
3605 // language is North American."
3607 if (s8c1t
== true) {
3608 writeRemote("\u009b?27;1n");
3610 writeRemote("\033[?27;1n");
3617 // Some other option, ignore
3623 * TBC - Tabulation clear.
3625 private void tbc() {
3626 int i
= getCsiParam(0, 0);
3628 List
<Integer
> newStops
= new ArrayList
<Integer
>();
3629 for (Integer stop
: tabStops
) {
3630 if (stop
== currentState
.cursorX
) {
3635 tabStops
= newStops
;
3643 * Erase the characters in the current line from the start column to the
3644 * end column, inclusive.
3646 * @param start starting column to erase (between 0 and width - 1)
3647 * @param end ending column to erase (between 0 and width - 1)
3648 * @param honorProtected if true, do not erase characters with the
3649 * protected attribute set
3651 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
3656 if (end
> width
- 1) {
3663 for (int i
= start
; i
<= end
; i
++) {
3664 DisplayLine line
= display
.get(currentState
.cursorY
);
3665 if ((!honorProtected
)
3666 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
3673 * From the VT102 manual:
3675 * Erasing a character also erases any character
3676 * attribute of the character.
3682 * Erase with the current color a.k.a. back-color erase
3685 line
.setChar(i
, ' ');
3686 line
.setAttr(i
, currentState
.attr
);
3694 * Erase a rectangular section of the screen, inclusive. end column,
3697 * @param startRow starting row to erase (between 0 and height - 1)
3698 * @param startCol starting column to erase (between 0 and width - 1)
3699 * @param endRow ending row to erase (between 0 and height - 1)
3700 * @param endCol ending column to erase (between 0 and width - 1)
3701 * @param honorProtected if true, do not erase characters with the
3702 * protected attribute set
3704 private void eraseScreen(final int startRow
, final int startCol
,
3705 final int endRow
, final int endCol
, final boolean honorProtected
) {
3713 || (endRow
< startRow
)
3714 || (endCol
< startCol
)
3719 oldCursorY
= currentState
.cursorY
;
3720 for (int i
= startRow
; i
<= endRow
; i
++) {
3721 currentState
.cursorY
= i
;
3722 eraseLine(startCol
, endCol
, honorProtected
);
3724 // Erase display clears the double attributes
3725 display
.get(i
).setDoubleWidth(false);
3726 display
.get(i
).setDoubleHeight(0);
3728 currentState
.cursorY
= oldCursorY
;
3732 * VT220 printer functions. All of these are parsed, but won't do
3735 private void printerFunctions() {
3736 boolean decPrivateModeFlag
= false;
3737 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3738 if (collectBuffer
.charAt(i
) == '?') {
3739 decPrivateModeFlag
= true;
3744 int i
= getCsiParam(0, 0);
3749 if (decPrivateModeFlag
== false) {
3755 if (decPrivateModeFlag
== true) {
3756 // Print cursor line
3761 if (decPrivateModeFlag
== true) {
3762 // Auto print mode OFF
3764 // Printer controller OFF
3766 // Characters re-appear on the screen
3767 printerControllerMode
= false;
3772 if (decPrivateModeFlag
== true) {
3776 // Printer controller
3778 // Characters get sucked into oblivion
3779 printerControllerMode
= true;
3790 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
3791 * of remote systems will send an XTerm title sequence even if TERM isn't
3794 * @param xtermChar the character received from the remote side
3796 private void oscPut(final char xtermChar
) {
3798 collectBuffer
.append(xtermChar
);
3801 if (xtermChar
== 0x07) {
3802 String args
= collectBuffer
.substring(0,
3803 collectBuffer
.length() - 1);
3804 String
[] p
= args
.toString().split(";");
3806 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
3814 // Go to SCAN_GROUND state
3821 * Run this input character through the ECMA48 state machine.
3823 * @param ch character from the remote side
3825 private void consume(char ch
) {
3828 // System.err.printf("%c", ch);
3830 // Special case for VT10x: 7-bit characters only
3831 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
3832 ch
= (char)(ch
& 0x7F);
3835 // Special "anywhere" states
3837 // 18, 1A --> execute, then switch to SCAN_GROUND
3838 if ((ch
== 0x18) || (ch
== 0x1A)) {
3839 // CAN and SUB abort escape sequences
3844 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
3848 && (scanState
!= ScanState
.DCS_ENTRY
)
3849 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
3850 && (scanState
!= ScanState
.DCS_IGNORE
)
3851 && (scanState
!= ScanState
.DCS_PARAM
)
3852 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
3855 scanState
= ScanState
.ESCAPE
;
3859 // 0x9B == CSI 8-bit sequence
3861 scanState
= ScanState
.CSI_ENTRY
;
3865 // 0x9D goes to ScanState.OSC_STRING
3867 scanState
= ScanState
.OSC_STRING
;
3871 // 0x90 goes to DCS_ENTRY
3873 scanState
= ScanState
.DCS_ENTRY
;
3877 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
3878 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
3879 scanState
= ScanState
.SOSPMAPC_STRING
;
3883 // 0x7F (DEL) is always discarded
3888 switch (scanState
) {
3891 // 00-17, 19, 1C-1F --> execute
3892 // 80-8F, 91-9A, 9C --> execute
3893 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
3894 handleControlChar(ch
);
3898 if (((ch
>= 0x20) && (ch
<= 0x7F))
3902 // VT220 printer --> trash bin
3903 if (((type
== DeviceType
.VT220
)
3904 || (type
== DeviceType
.XTERM
))
3905 && (printerControllerMode
== true)
3910 // Hang onto this character
3911 repCh
= mapCharacter(ch
);
3913 // Print this character
3914 printCharacter(repCh
);
3919 // 00-17, 19, 1C-1F --> execute
3921 handleControlChar(ch
);
3925 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
3926 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
3928 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
3932 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
3933 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
3944 // DECSC - Save cursor
3945 // Note this code overlaps both ANSI and VT52 mode
3950 // DECRC - Restore cursor
3951 // Note this code overlaps both ANSI and VT52 mode
3960 if (vt52Mode
== true) {
3961 // DECANM - Enter ANSI mode
3963 arrowKeyMode
= ArrowKeyMode
.VT100
;
3966 * From the VT102 docs: "You use ANSI mode to select
3967 * most terminal features; the terminal uses the same
3968 * features when it switches to VT52 mode. You
3969 * cannot, however, change most of these features in
3972 * In other words, do not reset any other attributes
3973 * when switching between VT52 submode and ANSI.
3977 currentState
.g0Charset
= CharacterSet
.US
;
3978 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3980 singleshift
= Singleshift
.NONE
;
3981 currentState
.glLockshift
= LockshiftMode
.NONE
;
3982 currentState
.grLockshift
= LockshiftMode
.NONE
;
3986 // DECKPAM - Keypad application mode
3987 // Note this code overlaps both ANSI and VT52 mode
3991 // DECKPNM - Keypad numeric mode
3992 // Note this code overlaps both ANSI and VT52 mode
3999 if (vt52Mode
== true) {
4000 // Cursor up, and stop at the top without scrolling
4005 if (vt52Mode
== true) {
4006 // Cursor down, and stop at the bottom without scrolling
4007 cursorDown(1, false);
4011 if (vt52Mode
== true) {
4012 // Cursor right, and stop at the right without scrolling
4013 cursorRight(1, false);
4017 if (vt52Mode
== true) {
4018 // Cursor left, and stop at the left without scrolling
4019 cursorLeft(1, false);
4026 if (vt52Mode
== true) {
4034 if (vt52Mode
== true) {
4035 // G0 --> Special graphics
4036 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
4040 if (vt52Mode
== true) {
4042 currentState
.g0Charset
= CharacterSet
.US
;
4046 if (vt52Mode
== true) {
4048 cursorPosition(0, 0);
4050 // HTS - Horizontal tabulation set
4055 if (vt52Mode
== true) {
4056 // Reverse line feed. Same as RI.
4061 if (vt52Mode
== true) {
4062 // Erase to end of screen
4063 eraseLine(currentState
.cursorX
, width
- 1, false);
4064 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4069 if (vt52Mode
== true) {
4070 // Erase to end of line
4071 eraseLine(currentState
.cursorX
, width
- 1, false);
4077 if (vt52Mode
== true) {
4080 // RI - Reverse index
4085 if (vt52Mode
== false) {
4087 singleshift
= Singleshift
.SS2
;
4091 if (vt52Mode
== false) {
4093 singleshift
= Singleshift
.SS3
;
4100 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4116 if (vt52Mode
== true) {
4117 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4125 if (vt52Mode
== true) {
4127 // Send string directly to remote side
4128 writeRemote("\033/Z");
4131 // Send string directly to remote side
4132 writeRemote(deviceTypeResponse());
4143 // VT52 cannot get to any of these other states
4144 if (vt52Mode
== true) {
4149 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4156 // RIS - Reset to initial state
4158 // Do I clear screen too? I think so...
4159 eraseScreen(0, 0, height
- 1, width
- 1, false);
4160 cursorPosition(0, 0);
4174 if ((type
== DeviceType
.VT220
)
4175 || (type
== DeviceType
.XTERM
)) {
4177 // VT220 lockshift G2 into GL
4178 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4183 if ((type
== DeviceType
.VT220
)
4184 || (type
== DeviceType
.XTERM
)) {
4186 // VT220 lockshift G3 into GL
4187 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4205 if ((type
== DeviceType
.VT220
)
4206 || (type
== DeviceType
.XTERM
)) {
4208 // VT220 lockshift G3 into GR
4209 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4214 if ((type
== DeviceType
.VT220
)
4215 || (type
== DeviceType
.XTERM
)) {
4217 // VT220 lockshift G2 into GR
4218 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4224 if ((type
== DeviceType
.VT220
)
4225 || (type
== DeviceType
.XTERM
)) {
4227 // VT220 lockshift G1 into GR
4228 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4238 // 0x5B goes to CSI_ENTRY
4240 scanState
= ScanState
.CSI_ENTRY
;
4243 // 0x5D goes to OSC_STRING
4245 scanState
= ScanState
.OSC_STRING
;
4248 // 0x50 goes to DCS_ENTRY
4250 scanState
= ScanState
.DCS_ENTRY
;
4253 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4254 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4255 scanState
= ScanState
.SOSPMAPC_STRING
;
4260 case ESCAPE_INTERMEDIATE
:
4261 // 00-17, 19, 1C-1F --> execute
4263 handleControlChar(ch
);
4266 // 20-2F --> collect
4267 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4271 // 30-7E --> dispatch, then switch to GROUND
4272 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4275 if ((collectBuffer
.length() == 1)
4276 && (collectBuffer
.charAt(0) == '(')) {
4277 // G0 --> Special graphics
4278 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4280 if ((collectBuffer
.length() == 1)
4281 && (collectBuffer
.charAt(0) == ')')) {
4282 // G1 --> Special graphics
4283 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4285 if ((type
== DeviceType
.VT220
)
4286 || (type
== DeviceType
.XTERM
)) {
4288 if ((collectBuffer
.length() == 1)
4289 && (collectBuffer
.charAt(0) == '*')) {
4290 // G2 --> Special graphics
4291 currentState
.g2Charset
= CharacterSet
.DRAWING
;
4293 if ((collectBuffer
.length() == 1)
4294 && (collectBuffer
.charAt(0) == '+')) {
4295 // G3 --> Special graphics
4296 currentState
.g3Charset
= CharacterSet
.DRAWING
;
4301 if ((collectBuffer
.length() == 1)
4302 && (collectBuffer
.charAt(0) == '(')) {
4303 // G0 --> Alternate character ROM standard character set
4304 currentState
.g0Charset
= CharacterSet
.ROM
;
4306 if ((collectBuffer
.length() == 1)
4307 && (collectBuffer
.charAt(0) == ')')) {
4308 // G1 --> Alternate character ROM standard character set
4309 currentState
.g1Charset
= CharacterSet
.ROM
;
4313 if ((collectBuffer
.length() == 1)
4314 && (collectBuffer
.charAt(0) == '(')) {
4315 // G0 --> Alternate character ROM special graphics
4316 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4318 if ((collectBuffer
.length() == 1)
4319 && (collectBuffer
.charAt(0) == ')')) {
4320 // G1 --> Alternate character ROM special graphics
4321 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4325 if ((collectBuffer
.length() == 1)
4326 && (collectBuffer
.charAt(0) == '#')) {
4327 // DECDHL - Double-height line (top half)
4332 if ((collectBuffer
.length() == 1)
4333 && (collectBuffer
.charAt(0) == '#')) {
4334 // DECDHL - Double-height line (bottom half)
4337 if ((type
== DeviceType
.VT220
)
4338 || (type
== DeviceType
.XTERM
)) {
4340 if ((collectBuffer
.length() == 1)
4341 && (collectBuffer
.charAt(0) == '(')) {
4343 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4345 if ((collectBuffer
.length() == 1)
4346 && (collectBuffer
.charAt(0) == ')')) {
4348 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4350 if ((collectBuffer
.length() == 1)
4351 && (collectBuffer
.charAt(0) == '*')) {
4353 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4355 if ((collectBuffer
.length() == 1)
4356 && (collectBuffer
.charAt(0) == '+')) {
4358 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4363 if ((collectBuffer
.length() == 1)
4364 && (collectBuffer
.charAt(0) == '#')) {
4365 // DECSWL - Single-width line
4368 if ((type
== DeviceType
.VT220
)
4369 || (type
== DeviceType
.XTERM
)) {
4371 if ((collectBuffer
.length() == 1)
4372 && (collectBuffer
.charAt(0) == '(')) {
4374 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4376 if ((collectBuffer
.length() == 1)
4377 && (collectBuffer
.charAt(0) == ')')) {
4379 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4381 if ((collectBuffer
.length() == 1)
4382 && (collectBuffer
.charAt(0) == '*')) {
4384 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4386 if ((collectBuffer
.length() == 1)
4387 && (collectBuffer
.charAt(0) == '+')) {
4389 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4394 if ((collectBuffer
.length() == 1)
4395 && (collectBuffer
.charAt(0) == '#')) {
4396 // DECDWL - Double-width line
4399 if ((type
== DeviceType
.VT220
)
4400 || (type
== DeviceType
.XTERM
)) {
4402 if ((collectBuffer
.length() == 1)
4403 && (collectBuffer
.charAt(0) == '(')) {
4405 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4407 if ((collectBuffer
.length() == 1)
4408 && (collectBuffer
.charAt(0) == ')')) {
4410 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4412 if ((collectBuffer
.length() == 1)
4413 && (collectBuffer
.charAt(0) == '*')) {
4415 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4417 if ((collectBuffer
.length() == 1)
4418 && (collectBuffer
.charAt(0) == '+')) {
4420 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4425 if ((type
== DeviceType
.VT220
)
4426 || (type
== DeviceType
.XTERM
)) {
4428 if ((collectBuffer
.length() == 1)
4429 && (collectBuffer
.charAt(0) == '(')) {
4431 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4433 if ((collectBuffer
.length() == 1)
4434 && (collectBuffer
.charAt(0) == ')')) {
4436 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4438 if ((collectBuffer
.length() == 1)
4439 && (collectBuffer
.charAt(0) == '*')) {
4441 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4443 if ((collectBuffer
.length() == 1)
4444 && (collectBuffer
.charAt(0) == '+')) {
4446 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4451 if ((collectBuffer
.length() == 1)
4452 && (collectBuffer
.charAt(0) == '#')) {
4453 // DECALN - Screen alignment display
4462 if ((type
== DeviceType
.VT220
)
4463 || (type
== DeviceType
.XTERM
)) {
4465 if ((collectBuffer
.length() == 1)
4466 && (collectBuffer
.charAt(0) == '(')) {
4467 // G0 --> DEC_SUPPLEMENTAL
4468 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4470 if ((collectBuffer
.length() == 1)
4471 && (collectBuffer
.charAt(0) == ')')) {
4472 // G1 --> DEC_SUPPLEMENTAL
4473 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4475 if ((collectBuffer
.length() == 1)
4476 && (collectBuffer
.charAt(0) == '*')) {
4477 // G2 --> DEC_SUPPLEMENTAL
4478 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4480 if ((collectBuffer
.length() == 1)
4481 && (collectBuffer
.charAt(0) == '+')) {
4482 // G3 --> DEC_SUPPLEMENTAL
4483 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4488 if ((type
== DeviceType
.VT220
)
4489 || (type
== DeviceType
.XTERM
)) {
4491 if ((collectBuffer
.length() == 1)
4492 && (collectBuffer
.charAt(0) == '(')) {
4494 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4496 if ((collectBuffer
.length() == 1)
4497 && (collectBuffer
.charAt(0) == ')')) {
4499 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4501 if ((collectBuffer
.length() == 1)
4502 && (collectBuffer
.charAt(0) == '*')) {
4504 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4506 if ((collectBuffer
.length() == 1)
4507 && (collectBuffer
.charAt(0) == '+')) {
4509 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4518 if ((collectBuffer
.length() == 1)
4519 && (collectBuffer
.charAt(0) == '(')) {
4520 // G0 --> United Kingdom set
4521 currentState
.g0Charset
= CharacterSet
.UK
;
4523 if ((collectBuffer
.length() == 1)
4524 && (collectBuffer
.charAt(0) == ')')) {
4525 // G1 --> United Kingdom set
4526 currentState
.g1Charset
= CharacterSet
.UK
;
4528 if ((type
== DeviceType
.VT220
)
4529 || (type
== DeviceType
.XTERM
)) {
4531 if ((collectBuffer
.length() == 1)
4532 && (collectBuffer
.charAt(0) == '*')) {
4533 // G2 --> United Kingdom set
4534 currentState
.g2Charset
= CharacterSet
.UK
;
4536 if ((collectBuffer
.length() == 1)
4537 && (collectBuffer
.charAt(0) == '+')) {
4538 // G3 --> United Kingdom set
4539 currentState
.g3Charset
= CharacterSet
.UK
;
4544 if ((collectBuffer
.length() == 1)
4545 && (collectBuffer
.charAt(0) == '(')) {
4547 currentState
.g0Charset
= CharacterSet
.US
;
4549 if ((collectBuffer
.length() == 1)
4550 && (collectBuffer
.charAt(0) == ')')) {
4552 currentState
.g1Charset
= CharacterSet
.US
;
4554 if ((type
== DeviceType
.VT220
)
4555 || (type
== DeviceType
.XTERM
)) {
4557 if ((collectBuffer
.length() == 1)
4558 && (collectBuffer
.charAt(0) == '*')) {
4560 currentState
.g2Charset
= CharacterSet
.US
;
4562 if ((collectBuffer
.length() == 1)
4563 && (collectBuffer
.charAt(0) == '+')) {
4565 currentState
.g3Charset
= CharacterSet
.US
;
4570 if ((type
== DeviceType
.VT220
)
4571 || (type
== DeviceType
.XTERM
)) {
4573 if ((collectBuffer
.length() == 1)
4574 && (collectBuffer
.charAt(0) == '(')) {
4576 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4578 if ((collectBuffer
.length() == 1)
4579 && (collectBuffer
.charAt(0) == ')')) {
4581 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4583 if ((collectBuffer
.length() == 1)
4584 && (collectBuffer
.charAt(0) == '*')) {
4586 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4588 if ((collectBuffer
.length() == 1)
4589 && (collectBuffer
.charAt(0) == '+')) {
4591 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4598 if ((type
== DeviceType
.VT220
)
4599 || (type
== DeviceType
.XTERM
)) {
4601 if ((collectBuffer
.length() == 1)
4602 && (collectBuffer
.charAt(0) == '(')) {
4604 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4606 if ((collectBuffer
.length() == 1)
4607 && (collectBuffer
.charAt(0) == ')')) {
4609 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4611 if ((collectBuffer
.length() == 1)
4612 && (collectBuffer
.charAt(0) == '*')) {
4614 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4616 if ((collectBuffer
.length() == 1)
4617 && (collectBuffer
.charAt(0) == '+')) {
4619 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4624 if ((type
== DeviceType
.VT220
)
4625 || (type
== DeviceType
.XTERM
)) {
4627 if ((collectBuffer
.length() == 1)
4628 && (collectBuffer
.charAt(0) == ' ')) {
4635 if ((type
== DeviceType
.VT220
)
4636 || (type
== DeviceType
.XTERM
)) {
4638 if ((collectBuffer
.length() == 1)
4639 && (collectBuffer
.charAt(0) == ' ')) {
4646 if ((type
== DeviceType
.VT220
)
4647 || (type
== DeviceType
.XTERM
)) {
4649 if ((collectBuffer
.length() == 1)
4650 && (collectBuffer
.charAt(0) == '(')) {
4652 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4654 if ((collectBuffer
.length() == 1)
4655 && (collectBuffer
.charAt(0) == ')')) {
4657 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4659 if ((collectBuffer
.length() == 1)
4660 && (collectBuffer
.charAt(0) == '*')) {
4662 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4664 if ((collectBuffer
.length() == 1)
4665 && (collectBuffer
.charAt(0) == '+')) {
4667 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4675 if ((type
== DeviceType
.VT220
)
4676 || (type
== DeviceType
.XTERM
)) {
4678 if ((collectBuffer
.length() == 1)
4679 && (collectBuffer
.charAt(0) == '(')) {
4681 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
4683 if ((collectBuffer
.length() == 1)
4684 && (collectBuffer
.charAt(0) == ')')) {
4686 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
4688 if ((collectBuffer
.length() == 1)
4689 && (collectBuffer
.charAt(0) == '*')) {
4691 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
4693 if ((collectBuffer
.length() == 1)
4694 && (collectBuffer
.charAt(0) == '+')) {
4696 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
4707 if ((type
== DeviceType
.VT220
)
4708 || (type
== DeviceType
.XTERM
)) {
4710 if ((collectBuffer
.length() == 1)
4711 && (collectBuffer
.charAt(0) == '(')) {
4713 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
4715 if ((collectBuffer
.length() == 1)
4716 && (collectBuffer
.charAt(0) == ')')) {
4718 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
4720 if ((collectBuffer
.length() == 1)
4721 && (collectBuffer
.charAt(0) == '*')) {
4723 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
4725 if ((collectBuffer
.length() == 1)
4726 && (collectBuffer
.charAt(0) == '+')) {
4728 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
4733 if ((type
== DeviceType
.VT220
)
4734 || (type
== DeviceType
.XTERM
)) {
4736 if ((collectBuffer
.length() == 1)
4737 && (collectBuffer
.charAt(0) == '(')) {
4739 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
4741 if ((collectBuffer
.length() == 1)
4742 && (collectBuffer
.charAt(0) == ')')) {
4744 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
4746 if ((collectBuffer
.length() == 1)
4747 && (collectBuffer
.charAt(0) == '*')) {
4749 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
4751 if ((collectBuffer
.length() == 1)
4752 && (collectBuffer
.charAt(0) == '+')) {
4754 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
4766 if ((type
== DeviceType
.VT220
)
4767 || (type
== DeviceType
.XTERM
)) {
4769 if ((collectBuffer
.length() == 1)
4770 && (collectBuffer
.charAt(0) == '(')) {
4772 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
4774 if ((collectBuffer
.length() == 1)
4775 && (collectBuffer
.charAt(0) == ')')) {
4777 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
4779 if ((collectBuffer
.length() == 1)
4780 && (collectBuffer
.charAt(0) == '*')) {
4782 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
4784 if ((collectBuffer
.length() == 1)
4785 && (collectBuffer
.charAt(0) == '+')) {
4787 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
4792 if ((type
== DeviceType
.VT220
)
4793 || (type
== DeviceType
.XTERM
)) {
4795 if ((collectBuffer
.length() == 1)
4796 && (collectBuffer
.charAt(0) == '(')) {
4798 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
4800 if ((collectBuffer
.length() == 1)
4801 && (collectBuffer
.charAt(0) == ')')) {
4803 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
4805 if ((collectBuffer
.length() == 1)
4806 && (collectBuffer
.charAt(0) == '*')) {
4808 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
4810 if ((collectBuffer
.length() == 1)
4811 && (collectBuffer
.charAt(0) == '+')) {
4813 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
4860 // 0x9C goes to GROUND
4868 // 00-17, 19, 1C-1F --> execute
4870 handleControlChar(ch
);
4873 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
4874 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4876 scanState
= ScanState
.CSI_INTERMEDIATE
;
4879 // 30-39, 3B --> param, then switch to CSI_PARAM
4880 if ((ch
>= '0') && (ch
<= '9')) {
4882 scanState
= ScanState
.CSI_PARAM
;
4886 scanState
= ScanState
.CSI_PARAM
;
4889 // 3C-3F --> collect, then switch to CSI_PARAM
4890 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
4892 scanState
= ScanState
.CSI_PARAM
;
4895 // 40-7E --> dispatch, then switch to GROUND
4896 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
4899 // ICH - Insert character
4907 // CUD - Cursor down
4911 // CUF - Cursor forward
4915 // CUB - Cursor backward
4919 // CNL - Cursor down and to column 1
4920 if (type
== DeviceType
.XTERM
) {
4925 // CPL - Cursor up and to column 1
4926 if (type
== DeviceType
.XTERM
) {
4931 // CHA - Cursor to column # in current row
4932 if (type
== DeviceType
.XTERM
) {
4937 // CUP - Cursor position
4941 // CHT - Cursor forward X tab stops (default 1)
4942 if (type
== DeviceType
.XTERM
) {
4947 // ED - Erase in display
4951 // EL - Erase in line
4966 // DCH - Delete character
4973 // Scroll up X lines (default 1)
4974 if (type
== DeviceType
.XTERM
) {
4979 // Scroll down X lines (default 1)
4980 if (type
== DeviceType
.XTERM
) {
4989 if ((type
== DeviceType
.VT220
)
4990 || (type
== DeviceType
.XTERM
)) {
4992 // ECH - Erase character
4999 // CBT - Cursor backward X tab stops (default 1)
5000 if (type
== DeviceType
.XTERM
) {
5011 // HPA - Cursor to column # in current row. Same as CHA
5012 if (type
== DeviceType
.XTERM
) {
5017 // HPR - Cursor right. Same as CUF
5018 if (type
== DeviceType
.XTERM
) {
5023 // REP - Repeat last char X times
5024 if (type
== DeviceType
.XTERM
) {
5029 // DA - Device attributes
5033 // VPA - Cursor to row, current column.
5034 if (type
== DeviceType
.XTERM
) {
5039 // VPR - Cursor down. Same as CUD
5040 if (type
== DeviceType
.XTERM
) {
5045 // HVP - Horizontal and vertical position
5049 // TBC - Tabulation clear
5053 // Sets an ANSI or DEC private toggle
5057 if ((type
== DeviceType
.VT220
)
5058 || (type
== DeviceType
.XTERM
)) {
5060 // Printer functions
5068 // Sets an ANSI or DEC private toggle
5072 // SGR - Select graphics rendition
5076 // DSR - Device status report
5083 // DECLL - Load leds
5087 // DECSTBM - Set top and bottom margins
5091 // Save cursor (ANSI.SYS)
5092 if (type
== DeviceType
.XTERM
) {
5093 savedState
.cursorX
= currentState
.cursorX
;
5094 savedState
.cursorY
= currentState
.cursorY
;
5100 // Restore cursor (ANSI.SYS)
5101 if (type
== DeviceType
.XTERM
) {
5102 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5109 // DECREQTPARM - Request terminal parameters
5125 // 0x9C goes to GROUND
5130 // 0x3A goes to CSI_IGNORE
5132 scanState
= ScanState
.CSI_IGNORE
;
5137 // 00-17, 19, 1C-1F --> execute
5139 handleControlChar(ch
);
5142 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5143 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5145 scanState
= ScanState
.CSI_INTERMEDIATE
;
5148 // 30-39, 3B --> param
5149 if ((ch
>= '0') && (ch
<= '9')) {
5156 // 0x3A goes to CSI_IGNORE
5158 scanState
= ScanState
.CSI_IGNORE
;
5160 // 0x3C-3F goes to CSI_IGNORE
5161 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5162 scanState
= ScanState
.CSI_IGNORE
;
5165 // 40-7E --> dispatch, then switch to GROUND
5166 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5169 // ICH - Insert character
5177 // CUD - Cursor down
5181 // CUF - Cursor forward
5185 // CUB - Cursor backward
5189 // CNL - Cursor down and to column 1
5190 if (type
== DeviceType
.XTERM
) {
5195 // CPL - Cursor up and to column 1
5196 if (type
== DeviceType
.XTERM
) {
5201 // CHA - Cursor to column # in current row
5202 if (type
== DeviceType
.XTERM
) {
5207 // CUP - Cursor position
5211 // CHT - Cursor forward X tab stops (default 1)
5212 if (type
== DeviceType
.XTERM
) {
5217 // ED - Erase in display
5221 // EL - Erase in line
5236 // DCH - Delete character
5243 // Scroll up X lines (default 1)
5244 if (type
== DeviceType
.XTERM
) {
5249 // Scroll down X lines (default 1)
5250 if (type
== DeviceType
.XTERM
) {
5259 if ((type
== DeviceType
.VT220
)
5260 || (type
== DeviceType
.XTERM
)) {
5262 // ECH - Erase character
5269 // CBT - Cursor backward X tab stops (default 1)
5270 if (type
== DeviceType
.XTERM
) {
5281 // HPA - Cursor to column # in current row. Same as CHA
5282 if (type
== DeviceType
.XTERM
) {
5287 // HPR - Cursor right. Same as CUF
5288 if (type
== DeviceType
.XTERM
) {
5293 // REP - Repeat last char X times
5294 if (type
== DeviceType
.XTERM
) {
5299 // DA - Device attributes
5303 // VPA - Cursor to row, current column.
5304 if (type
== DeviceType
.XTERM
) {
5309 // VPR - Cursor down. Same as CUD
5310 if (type
== DeviceType
.XTERM
) {
5315 // HVP - Horizontal and vertical position
5319 // TBC - Tabulation clear
5323 // Sets an ANSI or DEC private toggle
5327 if ((type
== DeviceType
.VT220
)
5328 || (type
== DeviceType
.XTERM
)) {
5330 // Printer functions
5338 // Sets an ANSI or DEC private toggle
5342 // SGR - Select graphics rendition
5346 // DSR - Device status report
5353 // DECLL - Load leds
5357 // DECSTBM - Set top and bottom margins
5367 // DECREQTPARM - Request terminal parameters
5384 case CSI_INTERMEDIATE
:
5385 // 00-17, 19, 1C-1F --> execute
5387 handleControlChar(ch
);
5390 // 20-2F --> collect
5391 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5395 // 0x30-3F goes to CSI_IGNORE
5396 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5397 scanState
= ScanState
.CSI_IGNORE
;
5400 // 40-7E --> dispatch, then switch to GROUND
5401 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5453 if (((type
== DeviceType
.VT220
)
5454 || (type
== DeviceType
.XTERM
))
5455 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5457 // DECSCL - compatibility level
5460 if ((type
== DeviceType
.XTERM
)
5461 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
5463 // DECSTR - Soft terminal reset
5468 if (((type
== DeviceType
.VT220
)
5469 || (type
== DeviceType
.XTERM
))
5470 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5498 // 00-17, 19, 1C-1F --> execute
5500 handleControlChar(ch
);
5503 // 20-2F --> collect
5504 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5508 // 40-7E --> ignore, then switch to GROUND
5509 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5513 // 20-3F, 7F --> ignore
5519 // 0x9C goes to GROUND
5524 // 0x1B 0x5C goes to GROUND
5529 if ((collectBuffer
.length() > 0)
5530 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5536 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5537 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5539 scanState
= ScanState
.DCS_INTERMEDIATE
;
5542 // 30-39, 3B --> param, then switch to DCS_PARAM
5543 if ((ch
>= '0') && (ch
<= '9')) {
5545 scanState
= ScanState
.DCS_PARAM
;
5549 scanState
= ScanState
.DCS_PARAM
;
5552 // 3C-3F --> collect, then switch to DCS_PARAM
5553 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5555 scanState
= ScanState
.DCS_PARAM
;
5558 // 00-17, 19, 1C-1F, 7F --> ignore
5560 // 0x3A goes to DCS_IGNORE
5562 scanState
= ScanState
.DCS_IGNORE
;
5565 // 0x40-7E goes to DCS_PASSTHROUGH
5566 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5567 scanState
= ScanState
.DCS_PASSTHROUGH
;
5571 case DCS_INTERMEDIATE
:
5573 // 0x9C goes to GROUND
5578 // 0x1B 0x5C goes to GROUND
5583 if ((collectBuffer
.length() > 0)
5584 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5590 // 0x30-3F goes to DCS_IGNORE
5591 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5592 scanState
= ScanState
.DCS_IGNORE
;
5595 // 0x40-7E goes to DCS_PASSTHROUGH
5596 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5597 scanState
= ScanState
.DCS_PASSTHROUGH
;
5600 // 00-17, 19, 1C-1F, 7F --> ignore
5605 // 0x9C goes to GROUND
5610 // 0x1B 0x5C goes to GROUND
5615 if ((collectBuffer
.length() > 0)
5616 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5622 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5623 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5625 scanState
= ScanState
.DCS_INTERMEDIATE
;
5628 // 30-39, 3B --> param
5629 if ((ch
>= '0') && (ch
<= '9')) {
5636 // 00-17, 19, 1C-1F, 7F --> ignore
5638 // 0x3A, 3C-3F goes to DCS_IGNORE
5640 scanState
= ScanState
.DCS_IGNORE
;
5642 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5643 scanState
= ScanState
.DCS_IGNORE
;
5646 // 0x40-7E goes to DCS_PASSTHROUGH
5647 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5648 scanState
= ScanState
.DCS_PASSTHROUGH
;
5652 case DCS_PASSTHROUGH
:
5653 // 0x9C goes to GROUND
5658 // 0x1B 0x5C goes to GROUND
5663 if ((collectBuffer
.length() > 0)
5664 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5670 // 00-17, 19, 1C-1F, 20-7E --> put
5678 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
5681 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
5690 // 00-17, 19, 1C-1F, 20-7F --> ignore
5692 // 0x9C goes to GROUND
5699 case SOSPMAPC_STRING
:
5700 // 00-17, 19, 1C-1F, 20-7F --> ignore
5702 // 0x9C goes to GROUND
5710 // Special case for Xterm: OSC can pass control characters
5711 if ((ch
== 0x9C) || (ch
<= 0x07)) {
5715 // 00-17, 19, 1C-1F --> ignore
5717 // 20-7F --> osc_put
5718 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
5722 // 0x9C goes to GROUND
5729 case VT52_DIRECT_CURSOR_ADDRESS
:
5730 // This is a special case for the VT52 sequence "ESC Y l c"
5731 if (collectBuffer
.length() == 0) {
5733 } else if (collectBuffer
.length() == 1) {
5734 // We've got the two characters, one in the buffer and the
5736 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
5745 * Expose current cursor X to outside world.
5747 * @return current cursor X
5749 public final int getCursorX() {
5750 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
5751 return currentState
.cursorX
* 2;
5753 return currentState
.cursorX
;
5757 * Expose current cursor Y to outside world.
5759 * @return current cursor Y
5761 public final int getCursorY() {
5762 return currentState
.cursorY
;
5766 * Read function runs on a separate thread.
5768 public final void run() {
5769 boolean utf8
= false;
5770 boolean done
= false;
5772 if (type
== DeviceType
.XTERM
) {
5776 // available() will often return > 1, so we need to read in chunks to
5778 char [] readBufferUTF8
= null;
5779 byte [] readBuffer
= null;
5781 readBufferUTF8
= new char[128];
5783 readBuffer
= new byte[128];
5786 while (!done
&& !stopReaderThread
) {
5788 int n
= inputStream
.available();
5789 // System.err.printf("available() %d\n", n); System.err.flush();
5791 if (readBufferUTF8
.length
< n
) {
5792 // The buffer wasn't big enough, make it huger
5793 int newSizeHalf
= Math
.max(readBufferUTF8
.length
, n
);
5795 readBufferUTF8
= new char[newSizeHalf
* 2];
5798 if (readBuffer
.length
< n
) {
5799 // The buffer wasn't big enough, make it huger
5800 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
5801 readBuffer
= new byte[newSizeHalf
* 2];
5807 rc
= input
.read(readBufferUTF8
, 0,
5808 readBufferUTF8
.length
);
5810 rc
= inputStream
.read(readBuffer
, 0,
5813 // System.err.printf("read() %d\n", rc); System.err.flush();
5818 for (int i
= 0; i
< rc
; i
++) {
5821 ch
= readBufferUTF8
[i
];
5825 // Don't step on UI events
5826 synchronized (this) {
5831 // System.err.println("end while loop"); System.err.flush();
5832 } catch (IOException e
) {
5833 e
.printStackTrace();
5836 } // while ((done == false) && (stopReaderThread == false))
5838 // Let the rest of the world know that I am done.
5839 stopReaderThread
= true;
5841 // System.err.println("*** run() exiting..."); System.err.flush();