2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.tterminal
;
31 import java
.io
.BufferedOutputStream
;
32 import java
.io
.CharArrayWriter
;
33 import java
.io
.InputStream
;
34 import java
.io
.InputStreamReader
;
35 import java
.io
.IOException
;
36 import java
.io
.OutputStream
;
37 import java
.io
.OutputStreamWriter
;
38 import java
.io
.PrintWriter
;
39 import java
.io
.Reader
;
40 import java
.io
.UnsupportedEncodingException
;
41 import java
.io
.Writer
;
42 import java
.util
.ArrayList
;
43 import java
.util
.Collections
;
44 import java
.util
.List
;
46 import jexer
.TKeypress
;
47 import jexer
.event
.TMouseEvent
;
48 import jexer
.bits
.Color
;
49 import jexer
.bits
.Cell
;
50 import jexer
.bits
.CellAttributes
;
51 import jexer
.io
.ReadTimeoutException
;
52 import jexer
.io
.TimeoutInputStream
;
53 import static jexer
.TKeypress
.*;
56 * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
57 * including a scrollback buffer.
60 * It currently implements VT100, VT102, VT220, and XTERM with the following
64 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
65 * failure with XTERM. This is due to vttest failing to decode the UTF-8
69 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
70 * from VT100 are not supported.
73 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
74 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
75 * because DECSCL is not supported, it will fail the last part of the
76 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
79 * - Numeric/application keys from the number pad are not supported because
80 * they are not exposed from the TKeypress API.
83 * - VT52 HOLD SCREEN mode is not supported.
86 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
87 * numerators) are not rendered correctly.
90 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
92 public class ECMA48
implements Runnable
{
94 // ------------------------------------------------------------------------
95 // Constants --------------------------------------------------------------
96 // ------------------------------------------------------------------------
99 * The emulator can emulate several kinds of terminals.
101 public enum DeviceType
{
103 * DEC VT100 but also including the three VT102 functions.
124 * Parser character scan states.
126 private enum ScanState
{
141 VT52_DIRECT_CURSOR_ADDRESS
145 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
146 * can't really use it in keypress() because we do not see number pad
147 * events from TKeypress.
149 private enum KeypadMode
{
155 * Arrow keys can emit three different sequences (DECCKM or VT52
158 private enum ArrowKeyMode
{
165 * Available character sets for GL, GR, G0, G1, G2, G3.
167 private enum CharacterSet
{
188 * Single-shift states used by the C1 control characters SS2 (0x8E) and
191 private enum Singleshift
{
198 * VT220+ lockshift states.
200 private enum LockshiftMode
{
210 * XTERM mouse reporting protocols.
212 private enum MouseProtocol
{
221 * XTERM mouse reporting encodings.
223 private enum MouseEncoding
{
229 // ------------------------------------------------------------------------
230 // Variables --------------------------------------------------------------
231 // ------------------------------------------------------------------------
234 * The enclosing listening object.
236 private DisplayListener displayListener
;
239 * When true, the reader thread is expected to exit.
241 private volatile boolean stopReaderThread
= false;
246 private Thread readerThread
= null;
249 * The type of emulator to be.
251 private DeviceType type
= DeviceType
.VT102
;
254 * The scrollback buffer characters + attributes.
256 private volatile ArrayList
<DisplayLine
> scrollback
;
259 * The raw display buffer characters + attributes.
261 private volatile ArrayList
<DisplayLine
> display
;
264 * The maximum number of lines in the scrollback buffer.
266 private int maxScrollback
= 10000;
269 * The terminal's input. For type == XTERM, this is an InputStreamReader
270 * with UTF-8 encoding.
272 private Reader input
;
275 * The terminal's raw InputStream. This is used for type != XTERM.
277 private volatile TimeoutInputStream inputStream
;
280 * The terminal's output. For type == XTERM, this wraps an
281 * OutputStreamWriter with UTF-8 encoding.
283 private Writer output
;
286 * The terminal's raw OutputStream. This is used for type != XTERM.
288 private OutputStream outputStream
;
291 * Current scanning state.
293 private ScanState scanState
;
296 * Which mouse protocol is active.
298 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
301 * Which mouse encoding is active.
303 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
306 * Physical display width. We start at 80x24, but the user can resize us
312 * Physical display height. We start at 80x24, but the user can resize
318 * Top margin of the scrolling region.
320 private int scrollRegionTop
;
323 * Bottom margin of the scrolling region.
325 private int scrollRegionBottom
;
328 * Right margin column number. This can be selected by the remote side
329 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
331 private int rightMargin
;
334 * Last character printed.
339 * VT100-style line wrapping: a character is placed in column 80 (or
340 * 132), but the line does NOT wrap until another character is written to
341 * column 1 of the next line, after which the cursor moves to column 2.
343 private boolean wrapLineFlag
;
346 * VT220 single shift flag.
348 private Singleshift singleshift
= Singleshift
.NONE
;
351 * true = insert characters, false = overwrite.
353 private boolean insertMode
= false;
356 * VT52 mode as selected by DECANM. True means VT52, false means
357 * ANSI. Default is ANSI.
359 private boolean vt52Mode
= false;
362 * Visible cursor (DECTCEM).
364 private boolean cursorVisible
= true;
367 * Screen title as set by the xterm OSC sequence. Lots of applications
368 * send a screenTitle regardless of whether it is an xterm client or not.
370 private String screenTitle
= "";
373 * Parameter characters being collected.
375 private List
<Integer
> csiParams
;
378 * Non-csi collect buffer.
380 private StringBuilder collectBuffer
;
383 * When true, use the G1 character set.
385 private boolean shiftOut
= false;
388 * Horizontal tab stop locations.
390 private List
<Integer
> tabStops
;
393 * S8C1T. True means 8bit controls, false means 7bit controls.
395 private boolean s8c1t
= false;
398 * Printer mode. True means send all output to printer, which discards
401 private boolean printerControllerMode
= false;
404 * LMN line mode. If true, linefeed() puts the cursor on the first
405 * column of the next line. If false, linefeed() puts the cursor one
406 * line down on the current line. The default is false.
408 private boolean newLineMode
= false;
411 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
413 private ArrowKeyMode arrowKeyMode
;
416 * Whether number pad keys send VT100 or VT52, application or numeric
419 @SuppressWarnings("unused")
420 private KeypadMode keypadMode
;
423 * When true, the terminal is in 132-column mode (DECCOLM).
425 private boolean columns132
= false;
428 * true = reverse video. Set by DECSCNM.
430 private boolean reverseVideo
= false;
433 * false = echo characters locally.
435 private boolean fullDuplex
= true;
438 * The current terminal state.
440 private SaveableState currentState
;
443 * The last saved terminal state.
445 private SaveableState savedState
;
448 * The 88- or 256-color support RGB colors.
450 private List
<Integer
> colors88
;
453 * DECSC/DECRC save/restore a subset of the total state. This class
454 * encapsulates those specific flags/modes.
456 private class SaveableState
{
459 * When true, cursor positions are relative to the scrolling region.
461 public boolean originMode
= false;
464 * The current editing X position.
466 public int cursorX
= 0;
469 * The current editing Y position.
471 public int cursorY
= 0;
474 * Which character set is currently selected in G0.
476 public CharacterSet g0Charset
= CharacterSet
.US
;
479 * Which character set is currently selected in G1.
481 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
484 * Which character set is currently selected in G2.
486 public CharacterSet g2Charset
= CharacterSet
.US
;
489 * Which character set is currently selected in G3.
491 public CharacterSet g3Charset
= CharacterSet
.US
;
494 * Which character set is currently selected in GR.
496 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
499 * The current drawing attributes.
501 public CellAttributes attr
;
506 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
511 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
516 public boolean lineWrap
= true;
521 public void reset() {
525 g0Charset
= CharacterSet
.US
;
526 g1Charset
= CharacterSet
.DRAWING
;
527 g2Charset
= CharacterSet
.US
;
528 g3Charset
= CharacterSet
.US
;
529 grCharset
= CharacterSet
.DRAWING
;
530 attr
= new CellAttributes();
531 glLockshift
= LockshiftMode
.NONE
;
532 grLockshift
= LockshiftMode
.NONE
;
537 * Copy attributes from another instance.
539 * @param that the other instance to match
541 public void setTo(final SaveableState that
) {
542 this.originMode
= that
.originMode
;
543 this.cursorX
= that
.cursorX
;
544 this.cursorY
= that
.cursorY
;
545 this.g0Charset
= that
.g0Charset
;
546 this.g1Charset
= that
.g1Charset
;
547 this.g2Charset
= that
.g2Charset
;
548 this.g3Charset
= that
.g3Charset
;
549 this.grCharset
= that
.grCharset
;
550 this.attr
= new CellAttributes();
551 this.attr
.setTo(that
.attr
);
552 this.glLockshift
= that
.glLockshift
;
553 this.grLockshift
= that
.grLockshift
;
554 this.lineWrap
= that
.lineWrap
;
558 * Public constructor.
560 public SaveableState() {
565 // ------------------------------------------------------------------------
566 // Constructors -----------------------------------------------------------
567 // ------------------------------------------------------------------------
570 * Public constructor.
572 * @param type one of the DeviceType constants to select VT100, VT102,
574 * @param inputStream an InputStream connected to the remote side. For
575 * type == XTERM, inputStream is converted to a Reader with UTF-8
577 * @param outputStream an OutputStream connected to the remote user. For
578 * type == XTERM, outputStream is converted to a Writer with UTF-8
580 * @param displayListener a callback to the outer display, or null for
581 * default VT100 behavior
582 * @throws UnsupportedEncodingException if an exception is thrown when
583 * creating the InputStreamReader
585 public ECMA48(final DeviceType type
, final InputStream inputStream
,
586 final OutputStream outputStream
, final DisplayListener displayListener
)
587 throws UnsupportedEncodingException
{
589 assert (inputStream
!= null);
590 assert (outputStream
!= null);
592 csiParams
= new ArrayList
<Integer
>();
593 tabStops
= new ArrayList
<Integer
>();
594 scrollback
= new ArrayList
<DisplayLine
>();
595 display
= new ArrayList
<DisplayLine
>();
598 if (inputStream
instanceof TimeoutInputStream
) {
599 this.inputStream
= (TimeoutInputStream
)inputStream
;
601 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
603 if (type
== DeviceType
.XTERM
) {
604 this.input
= new InputStreamReader(this.inputStream
, "UTF-8");
605 this.output
= new OutputStreamWriter(new
606 BufferedOutputStream(outputStream
), "UTF-8");
607 this.outputStream
= null;
610 this.outputStream
= new BufferedOutputStream(outputStream
);
612 this.displayListener
= displayListener
;
615 for (int i
= 0; i
< height
; i
++) {
616 display
.add(new DisplayLine(currentState
.attr
));
619 // Spin up the input reader
620 readerThread
= new Thread(this);
621 readerThread
.start();
624 // ------------------------------------------------------------------------
625 // Runnable ---------------------------------------------------------------
626 // ------------------------------------------------------------------------
629 * Read function runs on a separate thread.
631 public final void run() {
632 boolean utf8
= false;
633 boolean done
= false;
635 if (type
== DeviceType
.XTERM
) {
639 // available() will often return > 1, so we need to read in chunks to
641 char [] readBufferUTF8
= null;
642 byte [] readBuffer
= null;
644 readBufferUTF8
= new char[128];
646 readBuffer
= new byte[128];
649 while (!done
&& !stopReaderThread
) {
651 int n
= inputStream
.available();
653 // System.err.printf("available() %d\n", n); System.err.flush();
655 if (readBufferUTF8
.length
< n
) {
656 // The buffer wasn't big enough, make it huger
657 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
660 readBufferUTF8
= new char[newSizeHalf
* 2];
663 if (readBuffer
.length
< n
) {
664 // The buffer wasn't big enough, make it huger
665 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
666 readBuffer
= new byte[newSizeHalf
* 2];
672 } catch (InterruptedException e
) {
681 rc
= input
.read(readBufferUTF8
, 0,
682 readBufferUTF8
.length
);
684 rc
= inputStream
.read(readBuffer
, 0,
687 } catch (ReadTimeoutException e
) {
691 // System.err.printf("read() %d\n", rc); System.err.flush();
696 // Don't step on UI events
697 synchronized (this) {
698 for (int i
= 0; i
< rc
; i
++) {
701 ch
= readBufferUTF8
[i
];
709 // Permit my enclosing UI to know that I updated.
710 if (displayListener
!= null) {
711 displayListener
.displayChanged();
714 // System.err.println("end while loop"); System.err.flush();
715 } catch (IOException e
) {
718 // This is an unusual case. We want to see the stack trace,
719 // but it is related to the spawned process rather than the
720 // actual UI. We will generate the stack trace, and consume
721 // it as though it was emitted by the shell.
722 CharArrayWriter writer
= new CharArrayWriter();
723 // Send a ST and RIS to clear the emulator state.
725 writer
.write("\033\\\033c");
726 writer
.write("\n-----------------------------------\n");
727 e
.printStackTrace(new PrintWriter(writer
));
728 writer
.write("\n-----------------------------------\n");
729 } catch (IOException e2
) {
732 char [] stackTrace
= writer
.toCharArray();
733 for (int i
= 0; i
< stackTrace
.length
; i
++) {
734 if (stackTrace
[i
] == '\n') {
737 consume(stackTrace
[i
]);
741 } // while ((done == false) && (stopReaderThread == false))
743 // Let the rest of the world know that I am done.
744 stopReaderThread
= true;
747 inputStream
.cancelRead();
750 } catch (IOException e
) {
756 } catch (IOException e
) {
760 // Permit my enclosing UI to know that I updated.
761 if (displayListener
!= null) {
762 displayListener
.displayChanged();
765 // System.err.println("*** run() exiting..."); System.err.flush();
768 // ------------------------------------------------------------------------
769 // ECMA48 -----------------------------------------------------------------
770 // ------------------------------------------------------------------------
773 * Return the proper primary Device Attributes string.
775 * @return string to send to remote side that is appropriate for the
778 private String
deviceTypeResponse() {
781 // "I am a VT100 with advanced video option" (often VT102)
790 // "I am a VT220" - 7 bit version
792 return "\033[?62;1;6c";
794 // "I am a VT220" - 8 bit version
795 return "\u009b?62;1;6c";
797 throw new IllegalArgumentException("Invalid device type: " + type
);
802 * Return the proper TERM environment variable for this device type.
804 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
805 * @return "vt100", "xterm", etc.
807 public static String
deviceTypeTerm(final DeviceType deviceType
) {
808 switch (deviceType
) {
822 throw new IllegalArgumentException("Invalid device type: "
828 * Return the proper LANG for this device type. Only XTERM devices know
829 * about UTF-8, the others are defined by their standard to be either
830 * 7-bit or 8-bit characters only.
832 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
833 * @param baseLang a base language without UTF-8 flag such as "C" or
835 * @return "en_US", "en_US.UTF-8", etc.
837 public static String
deviceTypeLang(final DeviceType deviceType
,
838 final String baseLang
) {
840 switch (deviceType
) {
848 return baseLang
+ ".UTF-8";
851 throw new IllegalArgumentException("Invalid device type: "
857 * Write a string directly to the remote side.
859 * @param str string to send
861 public void writeRemote(final String str
) {
862 if (stopReaderThread
) {
863 // Reader hit EOF, bail out now.
868 // System.err.printf("writeRemote() '%s'\n", str);
874 if (outputStream
== null) {
878 outputStream
.flush();
879 for (int i
= 0; i
< str
.length(); i
++) {
880 outputStream
.write(str
.charAt(i
));
882 outputStream
.flush();
883 } catch (IOException e
) {
889 if (output
== null) {
896 } catch (IOException e
) {
902 throw new IllegalArgumentException("Invalid device type: " + type
);
907 * Close the input and output streams and stop the reader thread. Note
908 * that it is safe to call this multiple times.
910 public final void close() {
912 // Tell the reader thread to stop looking at input. It will close
913 // the input streams.
914 if (stopReaderThread
== false) {
915 stopReaderThread
= true;
917 readerThread
.join(1000);
918 } catch (InterruptedException e
) {
923 // Now close the output stream.
928 if (outputStream
!= null) {
930 outputStream
.close();
931 } catch (IOException e
) {
938 if (outputStream
!= null) {
940 outputStream
.close();
941 } catch (IOException e
) {
946 if (output
!= null) {
949 } catch (IOException e
) {
956 throw new IllegalArgumentException("Invalid device type: " +
962 * See if the reader thread is still running.
964 * @return if true, we are still connected to / reading from the remote
967 public final boolean isReading() {
968 return (!stopReaderThread
);
972 * Obtain a new blank display line for an external user
973 * (e.g. TTerminalWindow).
975 * @return new blank line
977 public final DisplayLine
getBlankDisplayLine() {
978 return new DisplayLine(currentState
.attr
);
982 * Get the scrollback buffer.
984 * @return the scrollback buffer
986 public final List
<DisplayLine
> getScrollbackBuffer() {
991 * Get the display buffer.
993 * @return the display buffer
995 public final List
<DisplayLine
> getDisplayBuffer() {
1000 * Get the display width.
1002 * @return the width (usually 80 or 132)
1004 public final int getWidth() {
1009 * Set the display width.
1011 * @param width the new width
1013 public final void setWidth(final int width
) {
1015 rightMargin
= width
- 1;
1016 if (currentState
.cursorX
>= width
) {
1017 currentState
.cursorX
= width
- 1;
1019 if (savedState
.cursorX
>= width
) {
1020 savedState
.cursorX
= width
- 1;
1025 * Get the display height.
1027 * @return the height (usually 24)
1029 public final int getHeight() {
1034 * Set the display height.
1036 * @param height the new height
1038 public final void setHeight(final int height
) {
1039 int delta
= height
- this.height
;
1040 this.height
= height
;
1041 scrollRegionBottom
+= delta
;
1042 if (scrollRegionBottom
< 0) {
1043 scrollRegionBottom
= height
;
1045 if (scrollRegionTop
>= scrollRegionBottom
) {
1046 scrollRegionTop
= 0;
1048 if (currentState
.cursorY
>= height
) {
1049 currentState
.cursorY
= height
- 1;
1051 if (savedState
.cursorY
>= height
) {
1052 savedState
.cursorY
= height
- 1;
1054 while (display
.size() < height
) {
1055 DisplayLine line
= new DisplayLine(currentState
.attr
);
1056 line
.setReverseColor(reverseVideo
);
1059 while (display
.size() > height
) {
1060 scrollback
.add(display
.remove(0));
1065 * Get visible cursor flag.
1067 * @return if true, the cursor is visible
1069 public final boolean isCursorVisible() {
1070 return cursorVisible
;
1074 * Get the screen title as set by the xterm OSC sequence. Lots of
1075 * applications send a screenTitle regardless of whether it is an xterm
1078 * @return screen title
1080 public final String
getScreenTitle() {
1085 * Get 132 columns value.
1087 * @return if true, the terminal is in 132 column mode
1089 public final boolean isColumns132() {
1094 * Clear the CSI parameters and flags.
1096 private void toGround() {
1098 collectBuffer
= new StringBuilder(8);
1099 scanState
= ScanState
.GROUND
;
1103 * Reset the tab stops list.
1105 private void resetTabStops() {
1107 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
1108 tabStops
.add(new Integer(i
* 8));
1113 * Reset the 88- or 256-colors.
1115 private void resetColors() {
1116 colors88
= new ArrayList
<Integer
>(256);
1117 for (int i
= 0; i
< 256; i
++) {
1121 // Set default system colors.
1122 colors88
.set(0, 0x00000000);
1123 colors88
.set(1, 0x00a80000);
1124 colors88
.set(2, 0x0000a800);
1125 colors88
.set(3, 0x00a85400);
1126 colors88
.set(4, 0x000000a8);
1127 colors88
.set(5, 0x00a800a8);
1128 colors88
.set(6, 0x0000a8a8);
1129 colors88
.set(7, 0x00a8a8a8);
1131 colors88
.set(8, 0x00545454);
1132 colors88
.set(9, 0x00fc5454);
1133 colors88
.set(10, 0x0054fc54);
1134 colors88
.set(11, 0x00fcfc54);
1135 colors88
.set(12, 0x005454fc);
1136 colors88
.set(13, 0x00fc54fc);
1137 colors88
.set(14, 0x0054fcfc);
1138 colors88
.set(15, 0x00fcfcfc);
1142 * Get the RGB value of one of the indexed colors.
1144 * @param index the color index
1145 * @return the RGB value
1147 private int get88Color(final int index
) {
1148 // System.err.print("get88Color: " + index);
1149 if ((index
< 0) || (index
> colors88
.size())) {
1150 // System.err.println(" -- UNKNOWN");
1153 // System.err.printf(" %08x\n", colors88.get(index));
1154 return colors88
.get(index
);
1158 * Set one of the indexed colors to a color specification.
1160 * @param index the color index
1161 * @param spec the specification, typically something like "rgb:aa/bb/cc"
1163 private void set88Color(final int index
, final String spec
) {
1164 // System.err.println("set88Color: " + index + " '" + spec + "'");
1166 if ((index
< 0) || (index
> colors88
.size())) {
1169 if (spec
.startsWith("rgb:")) {
1170 String
[] rgbTokens
= spec
.substring(4).split("/");
1171 if (rgbTokens
.length
== 3) {
1173 int rgb
= (Integer
.parseInt(rgbTokens
[0], 16) << 16);
1174 rgb
|= Integer
.parseInt(rgbTokens
[1], 16) << 8;
1175 rgb
|= Integer
.parseInt(rgbTokens
[2], 16);
1176 // System.err.printf(" set to %08x\n", rgb);
1177 colors88
.set(index
, rgb
);
1178 } catch (NumberFormatException e
) {
1185 if (spec
.toLowerCase().equals("black")) {
1186 colors88
.set(index
, 0x00000000);
1187 } else if (spec
.toLowerCase().equals("red")) {
1188 colors88
.set(index
, 0x00a80000);
1189 } else if (spec
.toLowerCase().equals("green")) {
1190 colors88
.set(index
, 0x0000a800);
1191 } else if (spec
.toLowerCase().equals("yellow")) {
1192 colors88
.set(index
, 0x00a85400);
1193 } else if (spec
.toLowerCase().equals("blue")) {
1194 colors88
.set(index
, 0x000000a8);
1195 } else if (spec
.toLowerCase().equals("magenta")) {
1196 colors88
.set(index
, 0x00a800a8);
1197 } else if (spec
.toLowerCase().equals("cyan")) {
1198 colors88
.set(index
, 0x0000a8a8);
1199 } else if (spec
.toLowerCase().equals("white")) {
1200 colors88
.set(index
, 0x00a8a8a8);
1206 * Reset the emulation state.
1208 private void reset() {
1210 currentState
= new SaveableState();
1211 savedState
= new SaveableState();
1212 scanState
= ScanState
.GROUND
;
1215 scrollRegionTop
= 0;
1216 scrollRegionBottom
= height
- 1;
1217 rightMargin
= width
- 1;
1218 newLineMode
= false;
1219 arrowKeyMode
= ArrowKeyMode
.ANSI
;
1220 keypadMode
= KeypadMode
.Numeric
;
1221 wrapLineFlag
= false;
1222 if (displayListener
!= null) {
1223 width
= displayListener
.getDisplayWidth();
1224 height
= displayListener
.getDisplayHeight();
1225 rightMargin
= width
- 1;
1233 newLineMode
= false;
1234 reverseVideo
= false;
1236 cursorVisible
= true;
1239 singleshift
= Singleshift
.NONE
;
1241 printerControllerMode
= false;
1244 mouseProtocol
= MouseProtocol
.OFF
;
1245 mouseEncoding
= MouseEncoding
.X10
;
1250 // Reset extra colors
1258 * Append a new line to the bottom of the display, adding lines off the
1259 * top to the scrollback buffer.
1261 private void newDisplayLine() {
1262 // Scroll the top line off into the scrollback buffer
1263 scrollback
.add(display
.get(0));
1264 if (scrollback
.size() > maxScrollback
) {
1265 scrollback
.remove(0);
1266 scrollback
.trimToSize();
1269 display
.trimToSize();
1270 DisplayLine line
= new DisplayLine(currentState
.attr
);
1271 line
.setReverseColor(reverseVideo
);
1276 * Wraps the current line.
1278 private void wrapCurrentLine() {
1279 if (currentState
.cursorY
== height
- 1) {
1282 if (currentState
.cursorY
< height
- 1) {
1283 currentState
.cursorY
++;
1285 currentState
.cursorX
= 0;
1289 * Handle a carriage return.
1291 private void carriageReturn() {
1292 currentState
.cursorX
= 0;
1293 wrapLineFlag
= false;
1297 * Reverse the color of the visible display.
1299 private void invertDisplayColors() {
1300 for (DisplayLine line
: display
) {
1301 line
.setReverseColor(!line
.isReverseColor());
1306 * Handle a linefeed.
1308 private void linefeed() {
1310 if (currentState
.cursorY
< scrollRegionBottom
) {
1311 // Increment screen y
1312 currentState
.cursorY
++;
1315 // Screen y does not increment
1318 * Two cases: either we're inside a scrolling region or not. If
1319 * the scrolling region bottom is the bottom of the screen, then
1320 * push the top line into the buffer. Else scroll the scrolling
1323 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1325 // We're at the bottom of the scroll region, AND the scroll
1326 // region is the entire screen.
1332 // We're at the bottom of the scroll region, AND the scroll
1333 // region is NOT the entire screen.
1334 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1339 currentState
.cursorX
= 0;
1341 wrapLineFlag
= false;
1345 * Prints one character to the display buffer.
1347 * @param ch character to display
1349 private void printCharacter(final char ch
) {
1350 int rightMargin
= this.rightMargin
;
1352 // Check if we have double-width, and if so chop at 40/66 instead of
1354 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1355 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1358 // Check the unusually-complicated line wrapping conditions...
1359 if (currentState
.cursorX
== rightMargin
) {
1361 if (currentState
.lineWrap
== true) {
1363 * This case happens when: the cursor was already on the
1364 * right margin (either through printing or by an explicit
1365 * placement command), and a character was printed.
1367 * The line wraps only when a new character arrives AND the
1368 * cursor is already on the right margin AND has placed a
1369 * character in its cell. Easier to see than to explain.
1371 if (wrapLineFlag
== false) {
1373 * This block marks the case that we are in the margin
1374 * and the first character has been received and printed.
1376 wrapLineFlag
= true;
1379 * This block marks the case that we are in the margin
1380 * and the second character has been received and
1383 wrapLineFlag
= false;
1387 } else if (currentState
.cursorX
<= rightMargin
) {
1389 * This is the normal case: a character came in and was printed
1390 * to the left of the right margin column.
1393 // Turn off VT100 special-case flag
1394 wrapLineFlag
= false;
1397 // "Print" the character
1398 Cell newCell
= new Cell(ch
);
1399 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1400 newCellAttributes
.setTo(currentState
.attr
);
1401 DisplayLine line
= display
.get(currentState
.cursorY
);
1402 // Insert mode special case
1403 if (insertMode
== true) {
1404 line
.insert(currentState
.cursorX
, newCell
);
1406 // Replace an existing character
1407 line
.replace(currentState
.cursorX
, newCell
);
1410 // Increment horizontal
1411 if (wrapLineFlag
== false) {
1412 currentState
.cursorX
++;
1413 if (currentState
.cursorX
> rightMargin
) {
1414 currentState
.cursorX
--;
1420 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1421 * send to the remote side.
1423 * @param mouse mouse event received from the local user
1425 public void mouse(final TMouseEvent mouse
) {
1428 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1429 mouseProtocol, mouseEncoding, mouse);
1432 if (mouseEncoding
== MouseEncoding
.X10
) {
1433 // We will support X10 but only for (160,94) and smaller.
1434 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1439 switch (mouseProtocol
) {
1446 // Only report button presses
1447 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1453 // Only report button presses and releases
1454 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1455 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1463 * Only report button presses, button releases, and motions that
1464 * have a button down (i.e. drag-and-drop).
1466 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1467 if (!mouse
.isMouse1()
1468 && !mouse
.isMouse2()
1469 && !mouse
.isMouse3()
1470 && !mouse
.isMouseWheelUp()
1471 && !mouse
.isMouseWheelDown()
1479 // Report everything
1483 // Now encode the event
1484 StringBuilder sb
= new StringBuilder(6);
1485 if (mouseEncoding
== MouseEncoding
.SGR
) {
1486 sb
.append((char) 0x1B);
1489 if (mouse
.isMouse1()) {
1490 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1495 } else if (mouse
.isMouse2()) {
1496 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1501 } else if (mouse
.isMouse3()) {
1502 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1507 } else if (mouse
.isMouseWheelUp()) {
1509 } else if (mouse
.isMouseWheelDown()) {
1512 // This is motion with no buttons down.
1516 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1519 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1526 // X10 and UTF8 encodings
1527 sb
.append((char) 0x1B);
1530 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1531 sb
.append((char) (0x03 + 32));
1532 } else if (mouse
.isMouse1()) {
1533 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1534 sb
.append((char) (0x00 + 32 + 32));
1536 sb
.append((char) (0x00 + 32));
1538 } else if (mouse
.isMouse2()) {
1539 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1540 sb
.append((char) (0x01 + 32 + 32));
1542 sb
.append((char) (0x01 + 32));
1544 } else if (mouse
.isMouse3()) {
1545 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1546 sb
.append((char) (0x02 + 32 + 32));
1548 sb
.append((char) (0x02 + 32));
1550 } else if (mouse
.isMouseWheelUp()) {
1551 sb
.append((char) (0x04 + 64));
1552 } else if (mouse
.isMouseWheelDown()) {
1553 sb
.append((char) (0x05 + 64));
1555 // This is motion with no buttons down.
1556 sb
.append((char) (0x03 + 32));
1559 sb
.append((char) (mouse
.getX() + 33));
1560 sb
.append((char) (mouse
.getY() + 33));
1563 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1564 writeRemote(sb
.toString());
1568 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1569 * send to the remote side.
1571 * @param keypress keypress received from the local user
1573 public void keypress(final TKeypress keypress
) {
1574 writeRemote(keypressToString(keypress
));
1578 * Build one of the complex xterm keystroke sequences, storing the result in
1579 * xterm_keystroke_buffer.
1581 * @param ss3 the prefix to use based on VT100 state.
1582 * @param first the first character, usually a number.
1583 * @param first the last character, one of the following: ~ A B C D F H
1584 * @param ctrl whether or not ctrl is down
1585 * @param alt whether or not alt is down
1586 * @param shift whether or not shift is down
1587 * @return the buffer with the full key sequence
1589 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1590 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1592 StringBuilder sb
= new StringBuilder(ss3
);
1593 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1597 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1599 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1601 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1603 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1605 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1607 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1609 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1614 return sb
.toString();
1618 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1620 * @param keypress keypress received from the local user
1621 * @return string to transmit to the remote side
1623 private String
keypressToString(final TKeypress keypress
) {
1625 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1627 * If this is a control character, process it like it came from
1630 if (keypress
.getChar() < 0x20) {
1631 handleControlChar(keypress
.getChar());
1633 // Local echo for everything else
1634 printCharacter(keypress
.getChar());
1638 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1643 // Handle control characters
1644 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1645 StringBuilder sb
= new StringBuilder();
1646 char ch
= keypress
.getChar();
1649 return sb
.toString();
1652 // Handle alt characters
1653 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1654 StringBuilder sb
= new StringBuilder("\033");
1655 char ch
= keypress
.getChar();
1657 return sb
.toString();
1660 if (keypress
.equals(kbBackspaceDel
)) {
1673 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1676 switch (arrowKeyMode
) {
1678 return xtermBuildKeySequence("\033[", '1', 'D',
1679 keypress
.isCtrl(), keypress
.isAlt(),
1680 keypress
.isShift());
1682 return xtermBuildKeySequence("\033", '1', 'D',
1683 keypress
.isCtrl(), keypress
.isAlt(),
1684 keypress
.isShift());
1686 return xtermBuildKeySequence("\033O", '1', 'D',
1687 keypress
.isCtrl(), keypress
.isAlt(),
1688 keypress
.isShift());
1691 switch (arrowKeyMode
) {
1702 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1705 switch (arrowKeyMode
) {
1707 return xtermBuildKeySequence("\033[", '1', 'C',
1708 keypress
.isCtrl(), keypress
.isAlt(),
1709 keypress
.isShift());
1711 return xtermBuildKeySequence("\033", '1', 'C',
1712 keypress
.isCtrl(), keypress
.isAlt(),
1713 keypress
.isShift());
1715 return xtermBuildKeySequence("\033O", '1', 'C',
1716 keypress
.isCtrl(), keypress
.isAlt(),
1717 keypress
.isShift());
1720 switch (arrowKeyMode
) {
1731 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1734 switch (arrowKeyMode
) {
1736 return xtermBuildKeySequence("\033[", '1', 'A',
1737 keypress
.isCtrl(), keypress
.isAlt(),
1738 keypress
.isShift());
1740 return xtermBuildKeySequence("\033", '1', 'A',
1741 keypress
.isCtrl(), keypress
.isAlt(),
1742 keypress
.isShift());
1744 return xtermBuildKeySequence("\033O", '1', 'A',
1745 keypress
.isCtrl(), keypress
.isAlt(),
1746 keypress
.isShift());
1749 switch (arrowKeyMode
) {
1760 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1763 switch (arrowKeyMode
) {
1765 return xtermBuildKeySequence("\033[", '1', 'B',
1766 keypress
.isCtrl(), keypress
.isAlt(),
1767 keypress
.isShift());
1769 return xtermBuildKeySequence("\033", '1', 'B',
1770 keypress
.isCtrl(), keypress
.isAlt(),
1771 keypress
.isShift());
1773 return xtermBuildKeySequence("\033O", '1', 'B',
1774 keypress
.isCtrl(), keypress
.isAlt(),
1775 keypress
.isShift());
1778 switch (arrowKeyMode
) {
1789 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1792 switch (arrowKeyMode
) {
1794 return xtermBuildKeySequence("\033[", '1', 'H',
1795 keypress
.isCtrl(), keypress
.isAlt(),
1796 keypress
.isShift());
1798 return xtermBuildKeySequence("\033", '1', 'H',
1799 keypress
.isCtrl(), keypress
.isAlt(),
1800 keypress
.isShift());
1802 return xtermBuildKeySequence("\033O", '1', 'H',
1803 keypress
.isCtrl(), keypress
.isAlt(),
1804 keypress
.isShift());
1807 switch (arrowKeyMode
) {
1818 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1821 switch (arrowKeyMode
) {
1823 return xtermBuildKeySequence("\033[", '1', 'F',
1824 keypress
.isCtrl(), keypress
.isAlt(),
1825 keypress
.isShift());
1827 return xtermBuildKeySequence("\033", '1', 'F',
1828 keypress
.isCtrl(), keypress
.isAlt(),
1829 keypress
.isShift());
1831 return xtermBuildKeySequence("\033O", '1', 'F',
1832 keypress
.isCtrl(), keypress
.isAlt(),
1833 keypress
.isShift());
1836 switch (arrowKeyMode
) {
1847 if (keypress
.equals(kbF1
)) {
1855 if (keypress
.equals(kbF2
)) {
1863 if (keypress
.equals(kbF3
)) {
1871 if (keypress
.equals(kbF4
)) {
1879 if (keypress
.equals(kbF5
)) {
1892 if (keypress
.equals(kbF6
)) {
1905 if (keypress
.equals(kbF7
)) {
1918 if (keypress
.equals(kbF8
)) {
1931 if (keypress
.equals(kbF9
)) {
1944 if (keypress
.equals(kbF10
)) {
1957 if (keypress
.equals(kbF11
)) {
1961 if (keypress
.equals(kbF12
)) {
1965 if (keypress
.equals(kbShiftF1
)) {
1970 if (type
== DeviceType
.XTERM
) {
1976 if (keypress
.equals(kbShiftF2
)) {
1981 if (type
== DeviceType
.XTERM
) {
1987 if (keypress
.equals(kbShiftF3
)) {
1992 if (type
== DeviceType
.XTERM
) {
1998 if (keypress
.equals(kbShiftF4
)) {
2003 if (type
== DeviceType
.XTERM
) {
2009 if (keypress
.equals(kbShiftF5
)) {
2011 return "\033[15;2~";
2014 if (keypress
.equals(kbShiftF6
)) {
2016 return "\033[17;2~";
2019 if (keypress
.equals(kbShiftF7
)) {
2021 return "\033[18;2~";
2024 if (keypress
.equals(kbShiftF8
)) {
2026 return "\033[19;2~";
2029 if (keypress
.equals(kbShiftF9
)) {
2031 return "\033[20;2~";
2034 if (keypress
.equals(kbShiftF10
)) {
2036 return "\033[21;2~";
2039 if (keypress
.equals(kbShiftF11
)) {
2041 return "\033[23;2~";
2044 if (keypress
.equals(kbShiftF12
)) {
2046 return "\033[24;2~";
2049 if (keypress
.equals(kbCtrlF1
)) {
2054 if (type
== DeviceType
.XTERM
) {
2060 if (keypress
.equals(kbCtrlF2
)) {
2065 if (type
== DeviceType
.XTERM
) {
2071 if (keypress
.equals(kbCtrlF3
)) {
2076 if (type
== DeviceType
.XTERM
) {
2082 if (keypress
.equals(kbCtrlF4
)) {
2087 if (type
== DeviceType
.XTERM
) {
2093 if (keypress
.equals(kbCtrlF5
)) {
2095 return "\033[15;5~";
2098 if (keypress
.equals(kbCtrlF6
)) {
2100 return "\033[17;5~";
2103 if (keypress
.equals(kbCtrlF7
)) {
2105 return "\033[18;5~";
2108 if (keypress
.equals(kbCtrlF8
)) {
2110 return "\033[19;5~";
2113 if (keypress
.equals(kbCtrlF9
)) {
2115 return "\033[20;5~";
2118 if (keypress
.equals(kbCtrlF10
)) {
2120 return "\033[21;5~";
2123 if (keypress
.equals(kbCtrlF11
)) {
2125 return "\033[23;5~";
2128 if (keypress
.equals(kbCtrlF12
)) {
2130 return "\033[24;5~";
2133 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2136 return xtermBuildKeySequence("\033[", '5', '~',
2137 keypress
.isCtrl(), keypress
.isAlt(),
2138 keypress
.isShift());
2144 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2147 return xtermBuildKeySequence("\033[", '6', '~',
2148 keypress
.isCtrl(), keypress
.isAlt(),
2149 keypress
.isShift());
2155 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2158 return xtermBuildKeySequence("\033[", '2', '~',
2159 keypress
.isCtrl(), keypress
.isAlt(),
2160 keypress
.isShift());
2166 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2169 return xtermBuildKeySequence("\033[", '3', '~',
2170 keypress
.isCtrl(), keypress
.isAlt(),
2171 keypress
.isShift());
2173 // Delete sends real delete for VTxxx
2178 if (keypress
.equals(kbEnter
)) {
2182 if (keypress
.equals(kbEsc
)) {
2186 if (keypress
.equals(kbAltEsc
)) {
2190 if (keypress
.equals(kbTab
)) {
2194 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2195 (keypress
.equals(kbShiftTab
))
2205 // Non-alt, non-ctrl characters
2206 if (!keypress
.isFnKey()) {
2207 StringBuilder sb
= new StringBuilder();
2208 sb
.append(keypress
.getChar());
2209 return sb
.toString();
2215 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2218 * @param ch 8-bit character from the remote side
2219 * @param charsetGl character set defined for GL
2220 * @param charsetGr character set defined for GR
2221 * @return character to display on the screen
2223 private char mapCharacterCharset(final char ch
,
2224 final CharacterSet charsetGl
,
2225 final CharacterSet charsetGr
) {
2227 int lookupChar
= ch
;
2228 CharacterSet lookupCharset
= charsetGl
;
2231 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2232 lookupCharset
= charsetGr
;
2236 switch (lookupCharset
) {
2239 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2242 return DECCharacterSets
.UK
[lookupChar
];
2245 return DECCharacterSets
.US_ASCII
[lookupChar
];
2248 return DECCharacterSets
.NL
[lookupChar
];
2251 return DECCharacterSets
.FI
[lookupChar
];
2254 return DECCharacterSets
.FR
[lookupChar
];
2257 return DECCharacterSets
.FR_CA
[lookupChar
];
2260 return DECCharacterSets
.DE
[lookupChar
];
2263 return DECCharacterSets
.IT
[lookupChar
];
2266 return DECCharacterSets
.NO
[lookupChar
];
2269 return DECCharacterSets
.ES
[lookupChar
];
2272 return DECCharacterSets
.SV
[lookupChar
];
2275 return DECCharacterSets
.SWISS
[lookupChar
];
2277 case DEC_SUPPLEMENTAL
:
2278 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2281 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2284 return DECCharacterSets
.US_ASCII
[lookupChar
];
2287 return DECCharacterSets
.US_ASCII
[lookupChar
];
2290 throw new IllegalArgumentException("Invalid character set value: "
2296 * Map an 8-bit byte into a printable character.
2298 * @param ch either 8-bit or Unicode character from the remote side
2299 * @return character to display on the screen
2301 private char mapCharacter(final char ch
) {
2303 // Unicode character, just return it
2307 CharacterSet charsetGl
= currentState
.g0Charset
;
2308 CharacterSet charsetGr
= currentState
.grCharset
;
2310 if (vt52Mode
== true) {
2311 if (shiftOut
== true) {
2312 // Shifted out character, pull from VT52 graphics
2313 charsetGl
= currentState
.g1Charset
;
2314 charsetGr
= CharacterSet
.US
;
2317 charsetGl
= currentState
.g0Charset
;
2318 charsetGr
= CharacterSet
.US
;
2321 // Pull the character
2322 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2326 if (shiftOut
== true) {
2327 // Shifted out character, pull from G1
2328 charsetGl
= currentState
.g1Charset
;
2329 charsetGr
= currentState
.grCharset
;
2331 // Pull the character
2332 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2336 if (singleshift
== Singleshift
.SS2
) {
2338 singleshift
= Singleshift
.NONE
;
2340 // Shifted out character, pull from G2
2341 charsetGl
= currentState
.g2Charset
;
2342 charsetGr
= currentState
.grCharset
;
2346 if (singleshift
== Singleshift
.SS3
) {
2348 singleshift
= Singleshift
.NONE
;
2350 // Shifted out character, pull from G3
2351 charsetGl
= currentState
.g3Charset
;
2352 charsetGr
= currentState
.grCharset
;
2355 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2356 // Check for locking shift
2358 switch (currentState
.glLockshift
) {
2371 charsetGl
= currentState
.g2Charset
;
2376 charsetGl
= currentState
.g3Charset
;
2381 charsetGl
= currentState
.g0Charset
;
2385 switch (currentState
.grLockshift
) {
2395 charsetGr
= currentState
.g1Charset
;
2400 charsetGr
= currentState
.g2Charset
;
2405 charsetGr
= currentState
.g3Charset
;
2410 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2417 // Pull the character
2418 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2422 * Scroll the text within a scrolling region up n lines.
2424 * @param regionTop top row of the scrolling region
2425 * @param regionBottom bottom row of the scrolling region
2426 * @param n number of lines to scroll
2428 private void scrollingRegionScrollUp(final int regionTop
,
2429 final int regionBottom
, final int n
) {
2431 if (regionTop
>= regionBottom
) {
2435 // Sanity check: see if there will be any characters left after the
2437 if (regionBottom
+ 1 - regionTop
<= n
) {
2438 // There won't be anything left in the region, so just call
2439 // eraseScreen() and return.
2440 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2444 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2445 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2446 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2448 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2449 - remaining
, regionBottom
+ 1);
2450 display
= new ArrayList
<DisplayLine
>(displayTop
);
2451 display
.addAll(displayMiddle
);
2452 for (int i
= 0; i
< n
; i
++) {
2453 DisplayLine line
= new DisplayLine(currentState
.attr
);
2454 line
.setReverseColor(reverseVideo
);
2457 display
.addAll(displayBottom
);
2459 assert (display
.size() == height
);
2463 * Scroll the text within a scrolling region down n lines.
2465 * @param regionTop top row of the scrolling region
2466 * @param regionBottom bottom row of the scrolling region
2467 * @param n number of lines to scroll
2469 private void scrollingRegionScrollDown(final int regionTop
,
2470 final int regionBottom
, final int n
) {
2472 if (regionTop
>= regionBottom
) {
2476 // Sanity check: see if there will be any characters left after the
2478 if (regionBottom
+ 1 - regionTop
<= n
) {
2479 // There won't be anything left in the region, so just call
2480 // eraseScreen() and return.
2481 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2485 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2486 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2487 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2489 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2490 regionTop
+ remaining
);
2491 display
= new ArrayList
<DisplayLine
>(displayTop
);
2492 for (int i
= 0; i
< n
; i
++) {
2493 DisplayLine line
= new DisplayLine(currentState
.attr
);
2494 line
.setReverseColor(reverseVideo
);
2497 display
.addAll(displayMiddle
);
2498 display
.addAll(displayBottom
);
2500 assert (display
.size() == height
);
2504 * Process a control character.
2506 * @param ch 8-bit character from the remote side
2508 private void handleControlChar(final char ch
) {
2509 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2520 // Transmit the answerback message.
2531 cursorLeft(1, false);
2536 advanceToNextTabStop();
2562 currentState
.glLockshift
= LockshiftMode
.NONE
;
2568 currentState
.glLockshift
= LockshiftMode
.NONE
;
2593 singleshift
= Singleshift
.SS2
;
2598 singleshift
= Singleshift
.SS3
;
2608 * Advance the cursor to the next tab stop.
2610 private void advanceToNextTabStop() {
2611 if (tabStops
.size() == 0) {
2612 // Go to the rightmost column
2613 cursorRight(rightMargin
- currentState
.cursorX
, false);
2616 for (Integer stop
: tabStops
) {
2617 if (stop
> currentState
.cursorX
) {
2618 cursorRight(stop
- currentState
.cursorX
, false);
2623 * We got here, meaning there isn't a tab stop beyond the current
2624 * cursor position. Place the cursor of the right-most edge of the
2627 cursorRight(rightMargin
- currentState
.cursorX
, false);
2631 * Save a character into the collect buffer.
2633 * @param ch character to save
2635 private void collect(final char ch
) {
2636 collectBuffer
.append(ch
);
2640 * Save a byte into the CSI parameters buffer.
2642 * @param ch byte to save
2644 private void param(final byte ch
) {
2645 if (csiParams
.size() == 0) {
2646 csiParams
.add(new Integer(0));
2648 Integer x
= csiParams
.get(csiParams
.size() - 1);
2649 if ((ch
>= '0') && (ch
<= '9')) {
2652 csiParams
.set(csiParams
.size() - 1, x
);
2656 csiParams
.add(new Integer(0));
2661 * Get a CSI parameter value, with a default.
2663 * @param position parameter index. 0 is the first parameter.
2664 * @param defaultValue value to use if csiParams[position] doesn't exist
2665 * @return parameter value
2667 private int getCsiParam(final int position
, final int defaultValue
) {
2668 if (csiParams
.size() < position
+ 1) {
2669 return defaultValue
;
2671 return csiParams
.get(position
).intValue();
2675 * Get a CSI parameter value, clamped to within min/max.
2677 * @param position parameter index. 0 is the first parameter.
2678 * @param defaultValue value to use if csiParams[position] doesn't exist
2679 * @param minValue minimum value inclusive
2680 * @param maxValue maximum value inclusive
2681 * @return parameter value
2683 private int getCsiParam(final int position
, final int defaultValue
,
2684 final int minValue
, final int maxValue
) {
2686 assert (minValue
<= maxValue
);
2687 int value
= getCsiParam(position
, defaultValue
);
2688 if (value
< minValue
) {
2691 if (value
> maxValue
) {
2698 * Set or unset a toggle.
2700 * @param value true for set ('h'), false for reset ('l')
2702 private void setToggle(final boolean value
) {
2703 boolean decPrivateModeFlag
= false;
2704 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2705 if (collectBuffer
.charAt(i
) == '?') {
2706 decPrivateModeFlag
= true;
2711 for (Integer i
: csiParams
) {
2716 if (decPrivateModeFlag
== true) {
2718 if (value
== true) {
2719 // Use application arrow keys
2720 arrowKeyMode
= ArrowKeyMode
.VT100
;
2722 // Use ANSI arrow keys
2723 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2728 if (decPrivateModeFlag
== true) {
2729 if (value
== false) {
2733 arrowKeyMode
= ArrowKeyMode
.VT52
;
2736 * From the VT102 docs: "You use ANSI mode to select
2737 * most terminal features; the terminal uses the same
2738 * features when it switches to VT52 mode. You
2739 * cannot, however, change most of these features in
2742 * In other words, do not reset any other attributes
2743 * when switching between VT52 submode and ANSI.
2745 * HOWEVER, the real vt100 does switch the character
2746 * set according to Usenet.
2748 currentState
.g0Charset
= CharacterSet
.US
;
2749 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2752 if ((type
== DeviceType
.VT220
)
2753 || (type
== DeviceType
.XTERM
)) {
2755 // VT52 mode is explicitly 7-bit
2757 singleshift
= Singleshift
.NONE
;
2762 if (value
== true) {
2763 // Turn off keyboard
2772 if (decPrivateModeFlag
== true) {
2774 if (value
== true) {
2781 if ((displayListener
!= null)
2782 && (type
== DeviceType
.XTERM
)
2784 // For xterms, reset to the actual width, not 80
2786 width
= displayListener
.getDisplayWidth();
2787 rightMargin
= width
- 1;
2790 width
= rightMargin
+ 1;
2793 // Entire screen is cleared, and scrolling region is
2795 eraseScreen(0, 0, height
- 1, width
- 1, false);
2796 scrollRegionTop
= 0;
2797 scrollRegionBottom
= height
- 1;
2798 // Also home the cursor
2799 cursorPosition(0, 0);
2803 if (decPrivateModeFlag
== true) {
2805 if (value
== true) {
2814 if (value
== true) {
2822 if (decPrivateModeFlag
== true) {
2824 if (value
== true) {
2826 * Set selects reverse screen, a white screen
2827 * background with black characters.
2829 if (reverseVideo
!= true) {
2831 * If in normal video, switch it back
2833 invertDisplayColors();
2835 reverseVideo
= true;
2838 * Reset selects normal screen, a black screen
2839 * background with white characters.
2841 if (reverseVideo
== true) {
2843 * If in reverse video already, switch it back
2845 invertDisplayColors();
2847 reverseVideo
= false;
2852 if (decPrivateModeFlag
== true) {
2854 if (value
== true) {
2855 // Origin is relative to scroll region cursor.
2856 // Cursor can NEVER leave scrolling region.
2857 currentState
.originMode
= true;
2858 cursorPosition(0, 0);
2860 // Origin is absolute to entire screen. Cursor can
2861 // leave the scrolling region via cup() and hvp().
2862 currentState
.originMode
= false;
2863 cursorPosition(0, 0);
2868 if (decPrivateModeFlag
== true) {
2870 if (value
== true) {
2872 currentState
.lineWrap
= true;
2874 // Turn linewrap off
2875 currentState
.lineWrap
= false;
2880 if (decPrivateModeFlag
== true) {
2882 if (value
== true) {
2883 // Keyboard auto-repeat on
2886 // Keyboard auto-repeat off
2892 if (decPrivateModeFlag
== false) {
2894 if (value
== true) {
2904 if (decPrivateModeFlag
== true) {
2910 if (decPrivateModeFlag
== true) {
2916 if (decPrivateModeFlag
== false) {
2918 if (value
== true) {
2920 * Set causes a received linefeed, form feed, or
2921 * vertical tab to move cursor to first column of
2922 * next line. RETURN transmits both a carriage return
2923 * and linefeed. This selection is also called new
2929 * Reset causes a received linefeed, form feed, or
2930 * vertical tab to move cursor to next line in
2931 * current column. RETURN transmits a carriage
2934 newLineMode
= false;
2940 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2941 if (decPrivateModeFlag
== true) {
2943 if (value
== true) {
2945 cursorVisible
= true;
2948 cursorVisible
= false;
2955 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2956 if (decPrivateModeFlag
== true) {
2958 if (value
== true) {
2959 // Select national mode NRC
2962 // Select multi-national mode
2971 if ((type
== DeviceType
.XTERM
)
2972 && (decPrivateModeFlag
== true)
2974 // Mouse: normal tracking mode
2975 if (value
== true) {
2976 mouseProtocol
= MouseProtocol
.NORMAL
;
2978 mouseProtocol
= MouseProtocol
.OFF
;
2984 if ((type
== DeviceType
.XTERM
)
2985 && (decPrivateModeFlag
== true)
2987 // Mouse: normal tracking mode
2988 if (value
== true) {
2989 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2991 mouseProtocol
= MouseProtocol
.OFF
;
2997 if ((type
== DeviceType
.XTERM
)
2998 && (decPrivateModeFlag
== true)
3000 // Mouse: Any-event tracking mode
3001 if (value
== true) {
3002 mouseProtocol
= MouseProtocol
.ANYEVENT
;
3004 mouseProtocol
= MouseProtocol
.OFF
;
3010 if ((type
== DeviceType
.XTERM
)
3011 && (decPrivateModeFlag
== true)
3013 // Mouse: UTF-8 coordinates
3014 if (value
== true) {
3015 mouseEncoding
= MouseEncoding
.UTF8
;
3017 mouseEncoding
= MouseEncoding
.X10
;
3023 if ((type
== DeviceType
.XTERM
)
3024 && (decPrivateModeFlag
== true)
3026 // Mouse: SGR coordinates
3027 if (value
== true) {
3028 mouseEncoding
= MouseEncoding
.SGR
;
3030 mouseEncoding
= MouseEncoding
.X10
;
3043 * DECSC - Save cursor.
3045 private void decsc() {
3046 savedState
.setTo(currentState
);
3050 * DECRC - Restore cursor.
3052 private void decrc() {
3053 currentState
.setTo(savedState
);
3059 private void ind() {
3060 // Move the cursor and scroll if necessary. If at the bottom line
3061 // already, a scroll up is supposed to be performed.
3062 if (currentState
.cursorY
== scrollRegionBottom
) {
3063 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3065 cursorDown(1, true);
3069 * RI - Reverse index.
3072 // Move the cursor and scroll if necessary. If at the top line
3073 // already, a scroll down is supposed to be performed.
3074 if (currentState
.cursorY
== scrollRegionTop
) {
3075 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3083 private void nel() {
3084 // Move the cursor and scroll if necessary. If at the bottom line
3085 // already, a scroll up is supposed to be performed.
3086 if (currentState
.cursorY
== scrollRegionBottom
) {
3087 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3089 cursorDown(1, true);
3091 // Reset to the beginning of the next line
3092 currentState
.cursorX
= 0;
3096 * DECKPAM - Keypad application mode.
3098 private void deckpam() {
3099 keypadMode
= KeypadMode
.Application
;
3103 * DECKPNM - Keypad numeric mode.
3105 private void deckpnm() {
3106 keypadMode
= KeypadMode
.Numeric
;
3112 * @param n number of spaces to move
3113 * @param honorScrollRegion if true, then do nothing if the cursor is
3114 * outside the scrolling region
3116 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3120 * Special case: if a user moves the cursor from the right margin, we
3121 * have to reset the VT100 right margin flag.
3124 wrapLineFlag
= false;
3127 for (int i
= 0; i
< n
; i
++) {
3128 if (honorScrollRegion
== true) {
3129 // Honor the scrolling region
3130 if ((currentState
.cursorY
< scrollRegionTop
)
3131 || (currentState
.cursorY
> scrollRegionBottom
)
3133 // Outside region, do nothing
3136 // Inside region, go up
3137 top
= scrollRegionTop
;
3139 // Non-scrolling case
3143 if (currentState
.cursorY
> top
) {
3144 currentState
.cursorY
--;
3150 * Move down n spaces.
3152 * @param n number of spaces to move
3153 * @param honorScrollRegion if true, then do nothing if the cursor is
3154 * outside the scrolling region
3156 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3160 * Special case: if a user moves the cursor from the right margin, we
3161 * have to reset the VT100 right margin flag.
3164 wrapLineFlag
= false;
3167 for (int i
= 0; i
< n
; i
++) {
3169 if (honorScrollRegion
== true) {
3170 // Honor the scrolling region
3171 if (currentState
.cursorY
> scrollRegionBottom
) {
3172 // Outside region, do nothing
3175 // Inside region, go down
3176 bottom
= scrollRegionBottom
;
3178 // Non-scrolling case
3179 bottom
= height
- 1;
3182 if (currentState
.cursorY
< bottom
) {
3183 currentState
.cursorY
++;
3189 * Move left n spaces.
3191 * @param n number of spaces to move
3192 * @param honorScrollRegion if true, then do nothing if the cursor is
3193 * outside the scrolling region
3195 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3197 * Special case: if a user moves the cursor from the right margin, we
3198 * have to reset the VT100 right margin flag.
3201 wrapLineFlag
= false;
3204 for (int i
= 0; i
< n
; i
++) {
3205 if (honorScrollRegion
== true) {
3206 // Honor the scrolling region
3207 if ((currentState
.cursorY
< scrollRegionTop
)
3208 || (currentState
.cursorY
> scrollRegionBottom
)
3210 // Outside region, do nothing
3215 if (currentState
.cursorX
> 0) {
3216 currentState
.cursorX
--;
3222 * Move right n spaces.
3224 * @param n number of spaces to move
3225 * @param honorScrollRegion if true, then do nothing if the cursor is
3226 * outside the scrolling region
3228 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3229 int rightMargin
= this.rightMargin
;
3232 * Special case: if a user moves the cursor from the right margin, we
3233 * have to reset the VT100 right margin flag.
3236 wrapLineFlag
= false;
3239 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3240 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3243 for (int i
= 0; i
< n
; i
++) {
3244 if (honorScrollRegion
== true) {
3245 // Honor the scrolling region
3246 if ((currentState
.cursorY
< scrollRegionTop
)
3247 || (currentState
.cursorY
> scrollRegionBottom
)
3249 // Outside region, do nothing
3254 if (currentState
.cursorX
< rightMargin
) {
3255 currentState
.cursorX
++;
3261 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3263 * @param row row to move to
3264 * @param col column to move to
3266 private void cursorPosition(int row
, final int col
) {
3267 int rightMargin
= this.rightMargin
;
3272 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3273 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3276 // Set column number
3277 currentState
.cursorX
= col
;
3279 // Sanity check, bring column back to margin.
3280 if (currentState
.cursorX
> rightMargin
) {
3281 currentState
.cursorX
= rightMargin
;
3285 if (currentState
.originMode
== true) {
3286 row
+= scrollRegionTop
;
3288 if (currentState
.cursorY
< row
) {
3289 cursorDown(row
- currentState
.cursorY
, false);
3290 } else if (currentState
.cursorY
> row
) {
3291 cursorUp(currentState
.cursorY
- row
, false);
3294 wrapLineFlag
= false;
3298 * HTS - Horizontal tabulation set.
3300 private void hts() {
3301 for (Integer stop
: tabStops
) {
3302 if (stop
== currentState
.cursorX
) {
3303 // Already have a tab stop here
3308 // Append a tab stop to the end of the array and resort them
3309 tabStops
.add(currentState
.cursorX
);
3310 Collections
.sort(tabStops
);
3314 * DECSWL - Single-width line.
3316 private void decswl() {
3317 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3318 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3322 * DECDWL - Double-width line.
3324 private void decdwl() {
3325 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3326 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3330 * DECHDL - Double-height + double-width line.
3332 * @param topHalf if true, this sets the row to be the top half row of a
3335 private void dechdl(final boolean topHalf
) {
3336 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3337 if (topHalf
== true) {
3338 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3340 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3345 * DECALN - Screen alignment display.
3347 private void decaln() {
3348 Cell newCell
= new Cell();
3349 newCell
.setChar('E');
3350 for (DisplayLine line
: display
) {
3351 for (int i
= 0; i
< line
.length(); i
++) {
3352 line
.replace(i
, newCell
);
3358 * DECSCL - Compatibility level.
3360 private void decscl() {
3361 int i
= getCsiParam(0, 0);
3362 int j
= getCsiParam(1, 0);
3366 currentState
.g0Charset
= CharacterSet
.US
;
3367 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3369 } else if (i
== 62) {
3371 if ((j
== 0) || (j
== 2)) {
3373 } else if (j
== 1) {
3380 * CUD - Cursor down.
3382 private void cud() {
3383 cursorDown(getCsiParam(0, 1, 1, height
), true);
3387 * CUF - Cursor forward.
3389 private void cuf() {
3390 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3394 * CUB - Cursor backward.
3396 private void cub() {
3397 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3403 private void cuu() {
3404 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3408 * CUP - Cursor position.
3410 private void cup() {
3411 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3412 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3416 * CNL - Cursor down and to column 1.
3418 private void cnl() {
3419 cursorDown(getCsiParam(0, 1, 1, height
), true);
3421 cursorLeft(currentState
.cursorX
, true);
3425 * CPL - Cursor up and to column 1.
3427 private void cpl() {
3428 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3430 cursorLeft(currentState
.cursorX
, true);
3434 * CHA - Cursor to column # in current row.
3436 private void cha() {
3437 cursorPosition(currentState
.cursorY
,
3438 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3442 * VPA - Cursor to row #, same column.
3444 private void vpa() {
3445 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3446 currentState
.cursorX
);
3450 * ED - Erase in display.
3453 boolean honorProtected
= false;
3454 boolean decPrivateModeFlag
= false;
3456 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3457 if (collectBuffer
.charAt(i
) == '?') {
3458 decPrivateModeFlag
= true;
3463 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3464 && (decPrivateModeFlag
== true)
3466 honorProtected
= true;
3469 int i
= getCsiParam(0, 0);
3472 // Erase from here to end of screen
3473 if (currentState
.cursorY
< height
- 1) {
3474 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3477 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3478 } else if (i
== 1) {
3479 // Erase from beginning of screen to here
3480 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3482 eraseLine(0, currentState
.cursorX
, honorProtected
);
3483 } else if (i
== 2) {
3484 // Erase entire screen
3485 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3490 * EL - Erase in line.
3493 boolean honorProtected
= false;
3494 boolean decPrivateModeFlag
= false;
3496 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3497 if (collectBuffer
.charAt(i
) == '?') {
3498 decPrivateModeFlag
= true;
3503 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3504 && (decPrivateModeFlag
== true)
3506 honorProtected
= true;
3509 int i
= getCsiParam(0, 0);
3512 // Erase from here to end of line
3513 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3514 } else if (i
== 1) {
3515 // Erase from beginning of line to here
3516 eraseLine(0, currentState
.cursorX
, honorProtected
);
3517 } else if (i
== 2) {
3518 // Erase entire line
3519 eraseLine(0, width
- 1, honorProtected
);
3524 * ECH - Erase # of characters in current row.
3526 private void ech() {
3527 int i
= getCsiParam(0, 1, 1, width
);
3529 // Erase from here to i characters
3530 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3537 int i
= getCsiParam(0, 1);
3539 if ((currentState
.cursorY
>= scrollRegionTop
)
3540 && (currentState
.cursorY
<= scrollRegionBottom
)
3543 // I can get the same effect with a scroll-down
3544 scrollingRegionScrollDown(currentState
.cursorY
,
3545 scrollRegionBottom
, i
);
3550 * DCH - Delete char.
3552 private void dch() {
3553 int n
= getCsiParam(0, 1);
3554 DisplayLine line
= display
.get(currentState
.cursorY
);
3555 Cell blank
= new Cell();
3556 for (int i
= 0; i
< n
; i
++) {
3557 line
.delete(currentState
.cursorX
, blank
);
3562 * ICH - Insert blank char at cursor.
3564 private void ich() {
3565 int n
= getCsiParam(0, 1);
3566 DisplayLine line
= display
.get(currentState
.cursorY
);
3567 Cell blank
= new Cell();
3568 for (int i
= 0; i
< n
; i
++) {
3569 line
.insert(currentState
.cursorX
, blank
);
3577 int i
= getCsiParam(0, 1);
3579 if ((currentState
.cursorY
>= scrollRegionTop
)
3580 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3582 // I can get the same effect with a scroll-down
3583 scrollingRegionScrollUp(currentState
.cursorY
,
3584 scrollRegionBottom
, i
);
3589 * HVP - Horizontal and vertical position.
3591 private void hvp() {
3596 * REP - Repeat character.
3598 private void rep() {
3599 int n
= getCsiParam(0, 1);
3600 for (int i
= 0; i
< n
; i
++) {
3601 printCharacter(repCh
);
3609 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3610 getCsiParam(0, 1, 1, height
));
3617 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3618 getCsiParam(0, 1, 1, height
));
3622 * CBT - Go back X tab stops.
3624 private void cbt() {
3625 int tabsToMove
= getCsiParam(0, 1);
3628 for (int i
= 0; i
< tabsToMove
; i
++) {
3629 int j
= currentState
.cursorX
;
3630 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3631 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3639 j
= tabStops
.get(tabI
);
3641 cursorPosition(currentState
.cursorY
, j
);
3646 * CHT - Advance X tab stops.
3648 private void cht() {
3649 int n
= getCsiParam(0, 1);
3650 for (int i
= 0; i
< n
; i
++) {
3651 advanceToNextTabStop();
3656 * SGR - Select graphics rendition.
3658 private void sgr() {
3660 if (csiParams
.size() == 0) {
3661 currentState
.attr
.reset();
3665 int sgrColorMode
= -1;
3666 boolean idx88Color
= false;
3667 boolean rgbColor
= false;
3671 for (Integer i
: csiParams
) {
3673 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
3675 assert (type
== DeviceType
.XTERM
);
3679 * Indexed color mode, we now have the index number.
3681 if (sgrColorMode
== 38) {
3682 currentState
.attr
.setForeColorRGB(get88Color(i
));
3684 assert (sgrColorMode
== 48);
3685 currentState
.attr
.setBackColorRGB(get88Color(i
));
3694 * RGB color mode, we are collecting tokens.
3698 } else if (rgbGreen
== -1) {
3699 rgbGreen
= i
& 0xFF;
3701 int rgb
= rgbRed
<< 16;
3702 rgb
|= rgbGreen
<< 8;
3705 // System.err.printf("RGB: %08x\n", rgb);
3707 if (sgrColorMode
== 38) {
3708 currentState
.attr
.setForeColorRGB(rgb
);
3710 assert (sgrColorMode
== 48);
3711 currentState
.attr
.setBackColorRGB(rgb
);
3732 * Indexed color mode.
3739 * This is neither indexed nor RGB color. Bail out.
3744 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
3750 currentState
.attr
.reset();
3755 currentState
.attr
.setBold(true);
3760 currentState
.attr
.setUnderline(true);
3765 currentState
.attr
.setBlink(true);
3770 currentState
.attr
.setReverse(true);
3777 if (type
== DeviceType
.XTERM
) {
3787 // Set black foreground
3788 currentState
.attr
.setForeColorRGB(get88Color(8));
3791 // Set red foreground
3792 currentState
.attr
.setForeColorRGB(get88Color(9));
3795 // Set green foreground
3796 currentState
.attr
.setForeColorRGB(get88Color(10));
3799 // Set yellow foreground
3800 currentState
.attr
.setForeColorRGB(get88Color(11));
3803 // Set blue foreground
3804 currentState
.attr
.setForeColorRGB(get88Color(12));
3807 // Set magenta foreground
3808 currentState
.attr
.setForeColorRGB(get88Color(13));
3811 // Set cyan foreground
3812 currentState
.attr
.setForeColorRGB(get88Color(14));
3815 // Set white foreground
3816 currentState
.attr
.setForeColorRGB(get88Color(15));
3820 // Set black background
3821 currentState
.attr
.setBackColorRGB(get88Color(8));
3824 // Set red background
3825 currentState
.attr
.setBackColorRGB(get88Color(9));
3828 // Set green background
3829 currentState
.attr
.setBackColorRGB(get88Color(10));
3832 // Set yellow background
3833 currentState
.attr
.setBackColorRGB(get88Color(11));
3836 // Set blue background
3837 currentState
.attr
.setBackColorRGB(get88Color(12));
3840 // Set magenta background
3841 currentState
.attr
.setBackColorRGB(get88Color(13));
3844 // Set cyan background
3845 currentState
.attr
.setBackColorRGB(get88Color(14));
3848 // Set white background
3849 currentState
.attr
.setBackColorRGB(get88Color(15));
3857 if ((type
== DeviceType
.VT220
)
3858 || (type
== DeviceType
.XTERM
)) {
3864 currentState
.attr
.setBold(false);
3869 currentState
.attr
.setUnderline(false);
3874 currentState
.attr
.setBlink(false);
3879 currentState
.attr
.setReverse(false);
3887 // A true VT100/102/220 does not support color, however everyone
3888 // is used to their terminal emulator supporting color so we will
3889 // unconditionally support color for all DeviceType's.
3894 // Set black foreground
3895 currentState
.attr
.setForeColor(Color
.BLACK
);
3896 currentState
.attr
.setForeColorRGB(-1);
3899 // Set red foreground
3900 currentState
.attr
.setForeColor(Color
.RED
);
3901 currentState
.attr
.setForeColorRGB(-1);
3904 // Set green foreground
3905 currentState
.attr
.setForeColor(Color
.GREEN
);
3906 currentState
.attr
.setForeColorRGB(-1);
3909 // Set yellow foreground
3910 currentState
.attr
.setForeColor(Color
.YELLOW
);
3911 currentState
.attr
.setForeColorRGB(-1);
3914 // Set blue foreground
3915 currentState
.attr
.setForeColor(Color
.BLUE
);
3916 currentState
.attr
.setForeColorRGB(-1);
3919 // Set magenta foreground
3920 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3921 currentState
.attr
.setForeColorRGB(-1);
3924 // Set cyan foreground
3925 currentState
.attr
.setForeColor(Color
.CYAN
);
3926 currentState
.attr
.setForeColorRGB(-1);
3929 // Set white foreground
3930 currentState
.attr
.setForeColor(Color
.WHITE
);
3931 currentState
.attr
.setForeColorRGB(-1);
3934 if (type
== DeviceType
.XTERM
) {
3936 * Xterm supports T.416 / ISO-8613-3 codes to select
3937 * either an indexed color or an RGB value. (It also
3938 * permits these ISO-8613-3 SGR sequences to be separated
3939 * by colons rather than semicolons.)
3941 * We will support only the following:
3943 * 1. Indexed color mode (88- or 256-color modes).
3947 * These cover most of the use cases in the real world.
3949 * HOWEVER, note that this is an awful broken "standard",
3950 * with no way to do it "right". See
3951 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
3952 * for a detailed discussion of the current state of RGB
3953 * in various terminals, the point of which is that none
3954 * of them really do the same thing despite all appearing
3958 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
3959 * where it is assumed that supporting just the "indexed
3960 * mode" of these sequences (which could align easily
3961 * with existing SGR colors) is assumed to mean full
3962 * support of 24-bit RGB. So it is all or nothing.
3964 * Finally, these sequences break the assumptions of
3965 * standard ECMA-48 style parsers as pointed out at
3966 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
3967 * Therefore in order to keep a clean display, we cannot
3968 * parse anything else in this sequence.
3973 // Underscore on, default foreground color
3974 currentState
.attr
.setUnderline(true);
3975 currentState
.attr
.setForeColor(Color
.WHITE
);
3979 // Underscore off, default foreground color
3980 currentState
.attr
.setUnderline(false);
3981 currentState
.attr
.setForeColor(Color
.WHITE
);
3982 currentState
.attr
.setForeColorRGB(-1);
3985 // Set black background
3986 currentState
.attr
.setBackColor(Color
.BLACK
);
3987 currentState
.attr
.setBackColorRGB(-1);
3990 // Set red background
3991 currentState
.attr
.setBackColor(Color
.RED
);
3992 currentState
.attr
.setBackColorRGB(-1);
3995 // Set green background
3996 currentState
.attr
.setBackColor(Color
.GREEN
);
3997 currentState
.attr
.setBackColorRGB(-1);
4000 // Set yellow background
4001 currentState
.attr
.setBackColor(Color
.YELLOW
);
4002 currentState
.attr
.setBackColorRGB(-1);
4005 // Set blue background
4006 currentState
.attr
.setBackColor(Color
.BLUE
);
4007 currentState
.attr
.setBackColorRGB(-1);
4010 // Set magenta background
4011 currentState
.attr
.setBackColor(Color
.MAGENTA
);
4012 currentState
.attr
.setBackColorRGB(-1);
4015 // Set cyan background
4016 currentState
.attr
.setBackColor(Color
.CYAN
);
4017 currentState
.attr
.setBackColorRGB(-1);
4020 // Set white background
4021 currentState
.attr
.setBackColor(Color
.WHITE
);
4022 currentState
.attr
.setBackColorRGB(-1);
4025 if (type
== DeviceType
.XTERM
) {
4027 * Xterm supports T.416 / ISO-8613-3 codes to select
4028 * either an indexed color or an RGB value. (It also
4029 * permits these ISO-8613-3 SGR sequences to be separated
4030 * by colons rather than semicolons.)
4032 * We will support only the following:
4034 * 1. Indexed color mode (88- or 256-color modes).
4038 * These cover most of the use cases in the real world.
4040 * HOWEVER, note that this is an awful broken "standard",
4041 * with no way to do it "right". See
4042 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4043 * for a detailed discussion of the current state of RGB
4044 * in various terminals, the point of which is that none
4045 * of them really do the same thing despite all appearing
4049 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4050 * where it is assumed that supporting just the "indexed
4051 * mode" of these sequences (which could align easily
4052 * with existing SGR colors) is assumed to mean full
4053 * support of 24-bit RGB. So it is all or nothing.
4055 * Finally, these sequences break the assumptions of
4056 * standard ECMA-48 style parsers as pointed out at
4057 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4058 * Therefore in order to keep a clean display, we cannot
4059 * parse anything else in this sequence.
4066 // Default background
4067 currentState
.attr
.setBackColor(Color
.BLACK
);
4068 currentState
.attr
.setBackColorRGB(-1);
4078 * DA - Device attributes.
4081 int extendedFlag
= 0;
4083 if (collectBuffer
.length() > 0) {
4084 String args
= collectBuffer
.substring(1);
4085 if (collectBuffer
.charAt(0) == '>') {
4087 if (collectBuffer
.length() >= 2) {
4088 i
= Integer
.parseInt(args
.toString());
4090 } else if (collectBuffer
.charAt(0) == '=') {
4092 if (collectBuffer
.length() >= 2) {
4093 i
= Integer
.parseInt(args
.toString());
4096 // Unknown code, bail out
4101 if ((i
!= 0) && (i
!= 1)) {
4105 if ((extendedFlag
== 0) && (i
== 0)) {
4106 // Send string directly to remote side
4107 writeRemote(deviceTypeResponse());
4111 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4113 if ((extendedFlag
== 1) && (i
== 0)) {
4115 * Request "What type of terminal are you, what is your
4116 * firmware version, and what hardware options do you have
4119 * Respond: "I am a VT220 (identification code of 1), my
4120 * firmware version is _____ (Pv), and I have _____ Po
4121 * options installed."
4127 if (s8c1t
== true) {
4128 writeRemote("\u009b>1;10;0c");
4130 writeRemote("\033[>1;10;0c");
4136 if ((extendedFlag
== 2) && (i
== 0)) {
4139 * Request "What is your unit ID?"
4141 * Respond: "I was manufactured at site 00 and have a unique ID
4145 writeRemote("\033P!|00010203\033\\");
4150 * DECSTBM - Set top and bottom margins.
4152 private void decstbm() {
4153 boolean decPrivateModeFlag
= false;
4155 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4156 if (collectBuffer
.charAt(i
) == '?') {
4157 decPrivateModeFlag
= true;
4161 if (decPrivateModeFlag
) {
4162 // This could be restore DEC private mode values.
4166 int top
= getCsiParam(0, 1, 1, height
) - 1;
4167 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4172 scrollRegionTop
= top
;
4173 scrollRegionBottom
= bottom
;
4176 cursorPosition(0, 0);
4181 * DECREQTPARM - Request terminal parameters.
4183 private void decreqtparm() {
4184 int i
= getCsiParam(0, 0);
4186 if ((i
!= 0) && (i
!= 1)) {
4193 * Request terminal parameters.
4197 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4198 * (CLoCk MULtiplier = 1, STP option flags = 0)
4202 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4205 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4207 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4213 * DECSCA - Select Character Attributes.
4215 private void decsca() {
4216 int i
= getCsiParam(0, 0);
4218 if ((i
== 0) || (i
== 2)) {
4220 currentState
.attr
.setProtect(false);
4224 currentState
.attr
.setProtect(true);
4229 * DECSTR - Soft Terminal Reset.
4231 private void decstr() {
4232 // Do exactly like RIS - Reset to initial state
4234 // Do I clear screen too? I think so...
4235 eraseScreen(0, 0, height
- 1, width
- 1, false);
4236 cursorPosition(0, 0);
4240 * DSR - Device status report.
4242 private void dsr() {
4243 boolean decPrivateModeFlag
= false;
4244 int row
= currentState
.cursorY
;
4246 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4247 if (collectBuffer
.charAt(i
) == '?') {
4248 decPrivateModeFlag
= true;
4253 int i
= getCsiParam(0, 0);
4258 // Request status report. Respond with "OK, no malfunction."
4260 // Send string directly to remote side
4261 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4264 writeRemote("\u009b0n");
4266 writeRemote("\033[0n");
4271 // Request cursor position. Respond with current position.
4272 if (currentState
.originMode
== true) {
4273 row
-= scrollRegionTop
;
4276 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4279 str
= String
.format("\u009b%d;%dR", row
+ 1,
4280 currentState
.cursorX
+ 1);
4282 str
= String
.format("\033[%d;%dR", row
+ 1,
4283 currentState
.cursorX
+ 1);
4286 // Send string directly to remote side
4291 if (decPrivateModeFlag
== true) {
4293 // Request printer status report. Respond with "Printer not
4296 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4297 && (s8c1t
== true)) {
4298 writeRemote("\u009b?13n");
4300 writeRemote("\033[?13n");
4306 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4307 && (decPrivateModeFlag
== true)
4310 // Request user-defined keys are locked or unlocked. Respond
4311 // with "User-defined keys are locked."
4313 if (s8c1t
== true) {
4314 writeRemote("\u009b?21n");
4316 writeRemote("\033[?21n");
4322 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4323 && (decPrivateModeFlag
== true)
4326 // Request keyboard language. Respond with "Keyboard
4327 // language is North American."
4329 if (s8c1t
== true) {
4330 writeRemote("\u009b?27;1n");
4332 writeRemote("\033[?27;1n");
4339 // Some other option, ignore
4345 * TBC - Tabulation clear.
4347 private void tbc() {
4348 int i
= getCsiParam(0, 0);
4350 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4351 for (Integer stop
: tabStops
) {
4352 if (stop
== currentState
.cursorX
) {
4357 tabStops
= newStops
;
4365 * Erase the characters in the current line from the start column to the
4366 * end column, inclusive.
4368 * @param start starting column to erase (between 0 and width - 1)
4369 * @param end ending column to erase (between 0 and width - 1)
4370 * @param honorProtected if true, do not erase characters with the
4371 * protected attribute set
4373 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4378 if (end
> width
- 1) {
4385 for (int i
= start
; i
<= end
; i
++) {
4386 DisplayLine line
= display
.get(currentState
.cursorY
);
4387 if ((!honorProtected
)
4388 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4395 * From the VT102 manual:
4397 * Erasing a character also erases any character
4398 * attribute of the character.
4404 * Erase with the current color a.k.a. back-color erase
4407 line
.setChar(i
, ' ');
4408 line
.setAttr(i
, currentState
.attr
);
4416 * Erase a rectangular section of the screen, inclusive. end column,
4419 * @param startRow starting row to erase (between 0 and height - 1)
4420 * @param startCol starting column to erase (between 0 and width - 1)
4421 * @param endRow ending row to erase (between 0 and height - 1)
4422 * @param endCol ending column to erase (between 0 and width - 1)
4423 * @param honorProtected if true, do not erase characters with the
4424 * protected attribute set
4426 private void eraseScreen(final int startRow
, final int startCol
,
4427 final int endRow
, final int endCol
, final boolean honorProtected
) {
4435 || (endRow
< startRow
)
4436 || (endCol
< startCol
)
4441 oldCursorY
= currentState
.cursorY
;
4442 for (int i
= startRow
; i
<= endRow
; i
++) {
4443 currentState
.cursorY
= i
;
4444 eraseLine(startCol
, endCol
, honorProtected
);
4446 // Erase display clears the double attributes
4447 display
.get(i
).setDoubleWidth(false);
4448 display
.get(i
).setDoubleHeight(0);
4450 currentState
.cursorY
= oldCursorY
;
4454 * VT220 printer functions. All of these are parsed, but won't do
4457 private void printerFunctions() {
4458 boolean decPrivateModeFlag
= false;
4459 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4460 if (collectBuffer
.charAt(i
) == '?') {
4461 decPrivateModeFlag
= true;
4466 int i
= getCsiParam(0, 0);
4471 if (decPrivateModeFlag
== false) {
4477 if (decPrivateModeFlag
== true) {
4478 // Print cursor line
4483 if (decPrivateModeFlag
== true) {
4484 // Auto print mode OFF
4486 // Printer controller OFF
4488 // Characters re-appear on the screen
4489 printerControllerMode
= false;
4494 if (decPrivateModeFlag
== true) {
4498 // Printer controller
4500 // Characters get sucked into oblivion
4501 printerControllerMode
= true;
4512 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4513 * of remote systems will send an XTerm title sequence even if TERM isn't
4516 * @param xtermChar the character received from the remote side
4518 private void oscPut(final char xtermChar
) {
4519 // System.err.println("oscPut: " + xtermChar);
4522 collectBuffer
.append(xtermChar
);
4525 if ((xtermChar
== 0x07)
4526 || (collectBuffer
.toString().endsWith("\033\\"))
4529 if (xtermChar
== 0x07) {
4530 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
4532 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4535 String
[] p
= args
.toString().split(";");
4537 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4544 if (p
[0].equals("4")) {
4545 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
4546 // Set a color index value
4548 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
4549 } catch (NumberFormatException e
) {
4556 // Go to SCAN_GROUND state
4563 * Run this input character through the ECMA48 state machine.
4565 * @param ch character from the remote side
4567 private void consume(char ch
) {
4570 // System.err.printf("%c", ch);
4572 // Special case for VT10x: 7-bit characters only
4573 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4574 ch
= (char)(ch
& 0x7F);
4577 // Special "anywhere" states
4579 // 18, 1A --> execute, then switch to SCAN_GROUND
4580 if ((ch
== 0x18) || (ch
== 0x1A)) {
4581 // CAN and SUB abort escape sequences
4586 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4590 if ((type
== DeviceType
.XTERM
)
4591 && (scanState
== ScanState
.OSC_STRING
)
4593 // Xterm can pass ESCAPE to its OSC sequence.
4594 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
4595 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4596 && (scanState
!= ScanState
.DCS_IGNORE
)
4597 && (scanState
!= ScanState
.DCS_PARAM
)
4598 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4601 scanState
= ScanState
.ESCAPE
;
4606 // 0x9B == CSI 8-bit sequence
4608 scanState
= ScanState
.CSI_ENTRY
;
4612 // 0x9D goes to ScanState.OSC_STRING
4614 scanState
= ScanState
.OSC_STRING
;
4618 // 0x90 goes to DCS_ENTRY
4620 scanState
= ScanState
.DCS_ENTRY
;
4624 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4625 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4626 scanState
= ScanState
.SOSPMAPC_STRING
;
4630 // 0x7F (DEL) is always discarded
4635 switch (scanState
) {
4638 // 00-17, 19, 1C-1F --> execute
4639 // 80-8F, 91-9A, 9C --> execute
4640 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4641 handleControlChar(ch
);
4645 if (((ch
>= 0x20) && (ch
<= 0x7F))
4649 // VT220 printer --> trash bin
4650 if (((type
== DeviceType
.VT220
)
4651 || (type
== DeviceType
.XTERM
))
4652 && (printerControllerMode
== true)
4657 // Hang onto this character
4658 repCh
= mapCharacter(ch
);
4660 // Print this character
4661 printCharacter(repCh
);
4666 // 00-17, 19, 1C-1F --> execute
4668 handleControlChar(ch
);
4672 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4673 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4675 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4679 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4680 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4691 // DECSC - Save cursor
4692 // Note this code overlaps both ANSI and VT52 mode
4697 // DECRC - Restore cursor
4698 // Note this code overlaps both ANSI and VT52 mode
4707 if (vt52Mode
== true) {
4708 // DECANM - Enter ANSI mode
4710 arrowKeyMode
= ArrowKeyMode
.VT100
;
4713 * From the VT102 docs: "You use ANSI mode to select
4714 * most terminal features; the terminal uses the same
4715 * features when it switches to VT52 mode. You
4716 * cannot, however, change most of these features in
4719 * In other words, do not reset any other attributes
4720 * when switching between VT52 submode and ANSI.
4724 currentState
.g0Charset
= CharacterSet
.US
;
4725 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4727 singleshift
= Singleshift
.NONE
;
4728 currentState
.glLockshift
= LockshiftMode
.NONE
;
4729 currentState
.grLockshift
= LockshiftMode
.NONE
;
4733 // DECKPAM - Keypad application mode
4734 // Note this code overlaps both ANSI and VT52 mode
4738 // DECKPNM - Keypad numeric mode
4739 // Note this code overlaps both ANSI and VT52 mode
4746 if (vt52Mode
== true) {
4747 // Cursor up, and stop at the top without scrolling
4752 if (vt52Mode
== true) {
4753 // Cursor down, and stop at the bottom without scrolling
4754 cursorDown(1, false);
4758 if (vt52Mode
== true) {
4759 // Cursor right, and stop at the right without scrolling
4760 cursorRight(1, false);
4764 if (vt52Mode
== true) {
4765 // Cursor left, and stop at the left without scrolling
4766 cursorLeft(1, false);
4773 if (vt52Mode
== true) {
4781 if (vt52Mode
== true) {
4782 // G0 --> Special graphics
4783 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
4787 if (vt52Mode
== true) {
4789 currentState
.g0Charset
= CharacterSet
.US
;
4793 if (vt52Mode
== true) {
4795 cursorPosition(0, 0);
4797 // HTS - Horizontal tabulation set
4802 if (vt52Mode
== true) {
4803 // Reverse line feed. Same as RI.
4808 if (vt52Mode
== true) {
4809 // Erase to end of screen
4810 eraseLine(currentState
.cursorX
, width
- 1, false);
4811 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4816 if (vt52Mode
== true) {
4817 // Erase to end of line
4818 eraseLine(currentState
.cursorX
, width
- 1, false);
4824 if (vt52Mode
== true) {
4827 // RI - Reverse index
4832 if (vt52Mode
== false) {
4834 singleshift
= Singleshift
.SS2
;
4838 if (vt52Mode
== false) {
4840 singleshift
= Singleshift
.SS3
;
4847 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4863 if (vt52Mode
== true) {
4864 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4872 if (vt52Mode
== true) {
4874 // Send string directly to remote side
4875 writeRemote("\033/Z");
4878 // Send string directly to remote side
4879 writeRemote(deviceTypeResponse());
4890 // VT52 cannot get to any of these other states
4891 if (vt52Mode
== true) {
4896 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4903 // RIS - Reset to initial state
4905 // Do I clear screen too? I think so...
4906 eraseScreen(0, 0, height
- 1, width
- 1, false);
4907 cursorPosition(0, 0);
4921 if ((type
== DeviceType
.VT220
)
4922 || (type
== DeviceType
.XTERM
)) {
4924 // VT220 lockshift G2 into GL
4925 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4930 if ((type
== DeviceType
.VT220
)
4931 || (type
== DeviceType
.XTERM
)) {
4933 // VT220 lockshift G3 into GL
4934 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4952 if ((type
== DeviceType
.VT220
)
4953 || (type
== DeviceType
.XTERM
)) {
4955 // VT220 lockshift G3 into GR
4956 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4961 if ((type
== DeviceType
.VT220
)
4962 || (type
== DeviceType
.XTERM
)) {
4964 // VT220 lockshift G2 into GR
4965 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4971 if ((type
== DeviceType
.VT220
)
4972 || (type
== DeviceType
.XTERM
)) {
4974 // VT220 lockshift G1 into GR
4975 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4985 // 0x5B goes to CSI_ENTRY
4987 scanState
= ScanState
.CSI_ENTRY
;
4990 // 0x5D goes to OSC_STRING
4992 scanState
= ScanState
.OSC_STRING
;
4995 // 0x50 goes to DCS_ENTRY
4997 scanState
= ScanState
.DCS_ENTRY
;
5000 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5001 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
5002 scanState
= ScanState
.SOSPMAPC_STRING
;
5007 case ESCAPE_INTERMEDIATE
:
5008 // 00-17, 19, 1C-1F --> execute
5010 handleControlChar(ch
);
5013 // 20-2F --> collect
5014 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5018 // 30-7E --> dispatch, then switch to GROUND
5019 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
5022 if ((collectBuffer
.length() == 1)
5023 && (collectBuffer
.charAt(0) == '(')) {
5024 // G0 --> Special graphics
5025 currentState
.g0Charset
= CharacterSet
.DRAWING
;
5027 if ((collectBuffer
.length() == 1)
5028 && (collectBuffer
.charAt(0) == ')')) {
5029 // G1 --> Special graphics
5030 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5032 if ((type
== DeviceType
.VT220
)
5033 || (type
== DeviceType
.XTERM
)) {
5035 if ((collectBuffer
.length() == 1)
5036 && (collectBuffer
.charAt(0) == '*')) {
5037 // G2 --> Special graphics
5038 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5040 if ((collectBuffer
.length() == 1)
5041 && (collectBuffer
.charAt(0) == '+')) {
5042 // G3 --> Special graphics
5043 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5048 if ((collectBuffer
.length() == 1)
5049 && (collectBuffer
.charAt(0) == '(')) {
5050 // G0 --> Alternate character ROM standard character set
5051 currentState
.g0Charset
= CharacterSet
.ROM
;
5053 if ((collectBuffer
.length() == 1)
5054 && (collectBuffer
.charAt(0) == ')')) {
5055 // G1 --> Alternate character ROM standard character set
5056 currentState
.g1Charset
= CharacterSet
.ROM
;
5060 if ((collectBuffer
.length() == 1)
5061 && (collectBuffer
.charAt(0) == '(')) {
5062 // G0 --> Alternate character ROM special graphics
5063 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5065 if ((collectBuffer
.length() == 1)
5066 && (collectBuffer
.charAt(0) == ')')) {
5067 // G1 --> Alternate character ROM special graphics
5068 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5072 if ((collectBuffer
.length() == 1)
5073 && (collectBuffer
.charAt(0) == '#')) {
5074 // DECDHL - Double-height line (top half)
5079 if ((collectBuffer
.length() == 1)
5080 && (collectBuffer
.charAt(0) == '#')) {
5081 // DECDHL - Double-height line (bottom half)
5084 if ((type
== DeviceType
.VT220
)
5085 || (type
== DeviceType
.XTERM
)) {
5087 if ((collectBuffer
.length() == 1)
5088 && (collectBuffer
.charAt(0) == '(')) {
5090 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5092 if ((collectBuffer
.length() == 1)
5093 && (collectBuffer
.charAt(0) == ')')) {
5095 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5097 if ((collectBuffer
.length() == 1)
5098 && (collectBuffer
.charAt(0) == '*')) {
5100 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5102 if ((collectBuffer
.length() == 1)
5103 && (collectBuffer
.charAt(0) == '+')) {
5105 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5110 if ((collectBuffer
.length() == 1)
5111 && (collectBuffer
.charAt(0) == '#')) {
5112 // DECSWL - Single-width line
5115 if ((type
== DeviceType
.VT220
)
5116 || (type
== DeviceType
.XTERM
)) {
5118 if ((collectBuffer
.length() == 1)
5119 && (collectBuffer
.charAt(0) == '(')) {
5121 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5123 if ((collectBuffer
.length() == 1)
5124 && (collectBuffer
.charAt(0) == ')')) {
5126 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5128 if ((collectBuffer
.length() == 1)
5129 && (collectBuffer
.charAt(0) == '*')) {
5131 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5133 if ((collectBuffer
.length() == 1)
5134 && (collectBuffer
.charAt(0) == '+')) {
5136 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5141 if ((collectBuffer
.length() == 1)
5142 && (collectBuffer
.charAt(0) == '#')) {
5143 // DECDWL - Double-width line
5146 if ((type
== DeviceType
.VT220
)
5147 || (type
== DeviceType
.XTERM
)) {
5149 if ((collectBuffer
.length() == 1)
5150 && (collectBuffer
.charAt(0) == '(')) {
5152 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5154 if ((collectBuffer
.length() == 1)
5155 && (collectBuffer
.charAt(0) == ')')) {
5157 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5159 if ((collectBuffer
.length() == 1)
5160 && (collectBuffer
.charAt(0) == '*')) {
5162 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5164 if ((collectBuffer
.length() == 1)
5165 && (collectBuffer
.charAt(0) == '+')) {
5167 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5172 if ((type
== DeviceType
.VT220
)
5173 || (type
== DeviceType
.XTERM
)) {
5175 if ((collectBuffer
.length() == 1)
5176 && (collectBuffer
.charAt(0) == '(')) {
5178 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5180 if ((collectBuffer
.length() == 1)
5181 && (collectBuffer
.charAt(0) == ')')) {
5183 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5185 if ((collectBuffer
.length() == 1)
5186 && (collectBuffer
.charAt(0) == '*')) {
5188 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5190 if ((collectBuffer
.length() == 1)
5191 && (collectBuffer
.charAt(0) == '+')) {
5193 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5198 if ((collectBuffer
.length() == 1)
5199 && (collectBuffer
.charAt(0) == '#')) {
5200 // DECALN - Screen alignment display
5209 if ((type
== DeviceType
.VT220
)
5210 || (type
== DeviceType
.XTERM
)) {
5212 if ((collectBuffer
.length() == 1)
5213 && (collectBuffer
.charAt(0) == '(')) {
5214 // G0 --> DEC_SUPPLEMENTAL
5215 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5217 if ((collectBuffer
.length() == 1)
5218 && (collectBuffer
.charAt(0) == ')')) {
5219 // G1 --> DEC_SUPPLEMENTAL
5220 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5222 if ((collectBuffer
.length() == 1)
5223 && (collectBuffer
.charAt(0) == '*')) {
5224 // G2 --> DEC_SUPPLEMENTAL
5225 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5227 if ((collectBuffer
.length() == 1)
5228 && (collectBuffer
.charAt(0) == '+')) {
5229 // G3 --> DEC_SUPPLEMENTAL
5230 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5235 if ((type
== DeviceType
.VT220
)
5236 || (type
== DeviceType
.XTERM
)) {
5238 if ((collectBuffer
.length() == 1)
5239 && (collectBuffer
.charAt(0) == '(')) {
5241 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5243 if ((collectBuffer
.length() == 1)
5244 && (collectBuffer
.charAt(0) == ')')) {
5246 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5248 if ((collectBuffer
.length() == 1)
5249 && (collectBuffer
.charAt(0) == '*')) {
5251 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5253 if ((collectBuffer
.length() == 1)
5254 && (collectBuffer
.charAt(0) == '+')) {
5256 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5265 if ((collectBuffer
.length() == 1)
5266 && (collectBuffer
.charAt(0) == '(')) {
5267 // G0 --> United Kingdom set
5268 currentState
.g0Charset
= CharacterSet
.UK
;
5270 if ((collectBuffer
.length() == 1)
5271 && (collectBuffer
.charAt(0) == ')')) {
5272 // G1 --> United Kingdom set
5273 currentState
.g1Charset
= CharacterSet
.UK
;
5275 if ((type
== DeviceType
.VT220
)
5276 || (type
== DeviceType
.XTERM
)) {
5278 if ((collectBuffer
.length() == 1)
5279 && (collectBuffer
.charAt(0) == '*')) {
5280 // G2 --> United Kingdom set
5281 currentState
.g2Charset
= CharacterSet
.UK
;
5283 if ((collectBuffer
.length() == 1)
5284 && (collectBuffer
.charAt(0) == '+')) {
5285 // G3 --> United Kingdom set
5286 currentState
.g3Charset
= CharacterSet
.UK
;
5291 if ((collectBuffer
.length() == 1)
5292 && (collectBuffer
.charAt(0) == '(')) {
5294 currentState
.g0Charset
= CharacterSet
.US
;
5296 if ((collectBuffer
.length() == 1)
5297 && (collectBuffer
.charAt(0) == ')')) {
5299 currentState
.g1Charset
= CharacterSet
.US
;
5301 if ((type
== DeviceType
.VT220
)
5302 || (type
== DeviceType
.XTERM
)) {
5304 if ((collectBuffer
.length() == 1)
5305 && (collectBuffer
.charAt(0) == '*')) {
5307 currentState
.g2Charset
= CharacterSet
.US
;
5309 if ((collectBuffer
.length() == 1)
5310 && (collectBuffer
.charAt(0) == '+')) {
5312 currentState
.g3Charset
= CharacterSet
.US
;
5317 if ((type
== DeviceType
.VT220
)
5318 || (type
== DeviceType
.XTERM
)) {
5320 if ((collectBuffer
.length() == 1)
5321 && (collectBuffer
.charAt(0) == '(')) {
5323 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5325 if ((collectBuffer
.length() == 1)
5326 && (collectBuffer
.charAt(0) == ')')) {
5328 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5330 if ((collectBuffer
.length() == 1)
5331 && (collectBuffer
.charAt(0) == '*')) {
5333 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5335 if ((collectBuffer
.length() == 1)
5336 && (collectBuffer
.charAt(0) == '+')) {
5338 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5345 if ((type
== DeviceType
.VT220
)
5346 || (type
== DeviceType
.XTERM
)) {
5348 if ((collectBuffer
.length() == 1)
5349 && (collectBuffer
.charAt(0) == '(')) {
5351 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5353 if ((collectBuffer
.length() == 1)
5354 && (collectBuffer
.charAt(0) == ')')) {
5356 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5358 if ((collectBuffer
.length() == 1)
5359 && (collectBuffer
.charAt(0) == '*')) {
5361 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5363 if ((collectBuffer
.length() == 1)
5364 && (collectBuffer
.charAt(0) == '+')) {
5366 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5371 if ((type
== DeviceType
.VT220
)
5372 || (type
== DeviceType
.XTERM
)) {
5374 if ((collectBuffer
.length() == 1)
5375 && (collectBuffer
.charAt(0) == ' ')) {
5382 if ((type
== DeviceType
.VT220
)
5383 || (type
== DeviceType
.XTERM
)) {
5385 if ((collectBuffer
.length() == 1)
5386 && (collectBuffer
.charAt(0) == ' ')) {
5393 if ((type
== DeviceType
.VT220
)
5394 || (type
== DeviceType
.XTERM
)) {
5396 if ((collectBuffer
.length() == 1)
5397 && (collectBuffer
.charAt(0) == '(')) {
5399 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5401 if ((collectBuffer
.length() == 1)
5402 && (collectBuffer
.charAt(0) == ')')) {
5404 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5406 if ((collectBuffer
.length() == 1)
5407 && (collectBuffer
.charAt(0) == '*')) {
5409 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5411 if ((collectBuffer
.length() == 1)
5412 && (collectBuffer
.charAt(0) == '+')) {
5414 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5422 if ((type
== DeviceType
.VT220
)
5423 || (type
== DeviceType
.XTERM
)) {
5425 if ((collectBuffer
.length() == 1)
5426 && (collectBuffer
.charAt(0) == '(')) {
5428 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5430 if ((collectBuffer
.length() == 1)
5431 && (collectBuffer
.charAt(0) == ')')) {
5433 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5435 if ((collectBuffer
.length() == 1)
5436 && (collectBuffer
.charAt(0) == '*')) {
5438 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5440 if ((collectBuffer
.length() == 1)
5441 && (collectBuffer
.charAt(0) == '+')) {
5443 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5454 if ((type
== DeviceType
.VT220
)
5455 || (type
== DeviceType
.XTERM
)) {
5457 if ((collectBuffer
.length() == 1)
5458 && (collectBuffer
.charAt(0) == '(')) {
5460 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5462 if ((collectBuffer
.length() == 1)
5463 && (collectBuffer
.charAt(0) == ')')) {
5465 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5467 if ((collectBuffer
.length() == 1)
5468 && (collectBuffer
.charAt(0) == '*')) {
5470 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5472 if ((collectBuffer
.length() == 1)
5473 && (collectBuffer
.charAt(0) == '+')) {
5475 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5480 if ((type
== DeviceType
.VT220
)
5481 || (type
== DeviceType
.XTERM
)) {
5483 if ((collectBuffer
.length() == 1)
5484 && (collectBuffer
.charAt(0) == '(')) {
5486 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5488 if ((collectBuffer
.length() == 1)
5489 && (collectBuffer
.charAt(0) == ')')) {
5491 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5493 if ((collectBuffer
.length() == 1)
5494 && (collectBuffer
.charAt(0) == '*')) {
5496 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5498 if ((collectBuffer
.length() == 1)
5499 && (collectBuffer
.charAt(0) == '+')) {
5501 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5513 if ((type
== DeviceType
.VT220
)
5514 || (type
== DeviceType
.XTERM
)) {
5516 if ((collectBuffer
.length() == 1)
5517 && (collectBuffer
.charAt(0) == '(')) {
5519 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5521 if ((collectBuffer
.length() == 1)
5522 && (collectBuffer
.charAt(0) == ')')) {
5524 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5526 if ((collectBuffer
.length() == 1)
5527 && (collectBuffer
.charAt(0) == '*')) {
5529 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5531 if ((collectBuffer
.length() == 1)
5532 && (collectBuffer
.charAt(0) == '+')) {
5534 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5539 if ((type
== DeviceType
.VT220
)
5540 || (type
== DeviceType
.XTERM
)) {
5542 if ((collectBuffer
.length() == 1)
5543 && (collectBuffer
.charAt(0) == '(')) {
5545 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5547 if ((collectBuffer
.length() == 1)
5548 && (collectBuffer
.charAt(0) == ')')) {
5550 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5552 if ((collectBuffer
.length() == 1)
5553 && (collectBuffer
.charAt(0) == '*')) {
5555 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5557 if ((collectBuffer
.length() == 1)
5558 && (collectBuffer
.charAt(0) == '+')) {
5560 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5607 // 0x9C goes to GROUND
5615 // 00-17, 19, 1C-1F --> execute
5617 handleControlChar(ch
);
5620 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5621 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5623 scanState
= ScanState
.CSI_INTERMEDIATE
;
5626 // 30-39, 3B --> param, then switch to CSI_PARAM
5627 if ((ch
>= '0') && (ch
<= '9')) {
5629 scanState
= ScanState
.CSI_PARAM
;
5633 scanState
= ScanState
.CSI_PARAM
;
5636 // 3C-3F --> collect, then switch to CSI_PARAM
5637 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5639 scanState
= ScanState
.CSI_PARAM
;
5642 // 40-7E --> dispatch, then switch to GROUND
5643 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5646 // ICH - Insert character
5654 // CUD - Cursor down
5658 // CUF - Cursor forward
5662 // CUB - Cursor backward
5666 // CNL - Cursor down and to column 1
5667 if (type
== DeviceType
.XTERM
) {
5672 // CPL - Cursor up and to column 1
5673 if (type
== DeviceType
.XTERM
) {
5678 // CHA - Cursor to column # in current row
5679 if (type
== DeviceType
.XTERM
) {
5684 // CUP - Cursor position
5688 // CHT - Cursor forward X tab stops (default 1)
5689 if (type
== DeviceType
.XTERM
) {
5694 // ED - Erase in display
5698 // EL - Erase in line
5713 // DCH - Delete character
5720 // Scroll up X lines (default 1)
5721 if (type
== DeviceType
.XTERM
) {
5726 // Scroll down X lines (default 1)
5727 if (type
== DeviceType
.XTERM
) {
5736 if ((type
== DeviceType
.VT220
)
5737 || (type
== DeviceType
.XTERM
)) {
5739 // ECH - Erase character
5746 // CBT - Cursor backward X tab stops (default 1)
5747 if (type
== DeviceType
.XTERM
) {
5758 // HPA - Cursor to column # in current row. Same as CHA
5759 if (type
== DeviceType
.XTERM
) {
5764 // HPR - Cursor right. Same as CUF
5765 if (type
== DeviceType
.XTERM
) {
5770 // REP - Repeat last char X times
5771 if (type
== DeviceType
.XTERM
) {
5776 // DA - Device attributes
5780 // VPA - Cursor to row, current column.
5781 if (type
== DeviceType
.XTERM
) {
5786 // VPR - Cursor down. Same as CUD
5787 if (type
== DeviceType
.XTERM
) {
5792 // HVP - Horizontal and vertical position
5796 // TBC - Tabulation clear
5800 // Sets an ANSI or DEC private toggle
5804 if ((type
== DeviceType
.VT220
)
5805 || (type
== DeviceType
.XTERM
)) {
5807 // Printer functions
5815 // Sets an ANSI or DEC private toggle
5819 // SGR - Select graphics rendition
5823 // DSR - Device status report
5830 // DECLL - Load leds
5834 // DECSTBM - Set top and bottom margins
5838 // Save cursor (ANSI.SYS)
5839 if (type
== DeviceType
.XTERM
) {
5840 savedState
.cursorX
= currentState
.cursorX
;
5841 savedState
.cursorY
= currentState
.cursorY
;
5847 // Restore cursor (ANSI.SYS)
5848 if (type
== DeviceType
.XTERM
) {
5849 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5856 // DECREQTPARM - Request terminal parameters
5872 // 0x9C goes to GROUND
5877 // 0x3A goes to CSI_IGNORE
5879 scanState
= ScanState
.CSI_IGNORE
;
5884 // 00-17, 19, 1C-1F --> execute
5886 handleControlChar(ch
);
5889 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5890 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5892 scanState
= ScanState
.CSI_INTERMEDIATE
;
5895 // 30-39, 3B --> param
5896 if ((ch
>= '0') && (ch
<= '9')) {
5903 // 0x3A goes to CSI_IGNORE
5905 scanState
= ScanState
.CSI_IGNORE
;
5907 // 0x3C-3F goes to CSI_IGNORE
5908 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5909 scanState
= ScanState
.CSI_IGNORE
;
5912 // 40-7E --> dispatch, then switch to GROUND
5913 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5916 // ICH - Insert character
5924 // CUD - Cursor down
5928 // CUF - Cursor forward
5932 // CUB - Cursor backward
5936 // CNL - Cursor down and to column 1
5937 if (type
== DeviceType
.XTERM
) {
5942 // CPL - Cursor up and to column 1
5943 if (type
== DeviceType
.XTERM
) {
5948 // CHA - Cursor to column # in current row
5949 if (type
== DeviceType
.XTERM
) {
5954 // CUP - Cursor position
5958 // CHT - Cursor forward X tab stops (default 1)
5959 if (type
== DeviceType
.XTERM
) {
5964 // ED - Erase in display
5968 // EL - Erase in line
5983 // DCH - Delete character
5990 // Scroll up X lines (default 1)
5991 if (type
== DeviceType
.XTERM
) {
5996 // Scroll down X lines (default 1)
5997 if (type
== DeviceType
.XTERM
) {
6006 if ((type
== DeviceType
.VT220
)
6007 || (type
== DeviceType
.XTERM
)) {
6009 // ECH - Erase character
6016 // CBT - Cursor backward X tab stops (default 1)
6017 if (type
== DeviceType
.XTERM
) {
6028 // HPA - Cursor to column # in current row. Same as CHA
6029 if (type
== DeviceType
.XTERM
) {
6034 // HPR - Cursor right. Same as CUF
6035 if (type
== DeviceType
.XTERM
) {
6040 // REP - Repeat last char X times
6041 if (type
== DeviceType
.XTERM
) {
6046 // DA - Device attributes
6050 // VPA - Cursor to row, current column.
6051 if (type
== DeviceType
.XTERM
) {
6056 // VPR - Cursor down. Same as CUD
6057 if (type
== DeviceType
.XTERM
) {
6062 // HVP - Horizontal and vertical position
6066 // TBC - Tabulation clear
6070 // Sets an ANSI or DEC private toggle
6074 if ((type
== DeviceType
.VT220
)
6075 || (type
== DeviceType
.XTERM
)) {
6077 // Printer functions
6085 // Sets an ANSI or DEC private toggle
6089 // SGR - Select graphics rendition
6093 // DSR - Device status report
6100 // DECLL - Load leds
6104 // DECSTBM - Set top and bottom margins
6114 // DECREQTPARM - Request terminal parameters
6131 case CSI_INTERMEDIATE
:
6132 // 00-17, 19, 1C-1F --> execute
6134 handleControlChar(ch
);
6137 // 20-2F --> collect
6138 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6142 // 0x30-3F goes to CSI_IGNORE
6143 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6144 scanState
= ScanState
.CSI_IGNORE
;
6147 // 40-7E --> dispatch, then switch to GROUND
6148 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6200 if (((type
== DeviceType
.VT220
)
6201 || (type
== DeviceType
.XTERM
))
6202 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6204 // DECSCL - compatibility level
6207 if ((type
== DeviceType
.XTERM
)
6208 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6210 // DECSTR - Soft terminal reset
6215 if (((type
== DeviceType
.VT220
)
6216 || (type
== DeviceType
.XTERM
))
6217 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6245 // 00-17, 19, 1C-1F --> execute
6247 handleControlChar(ch
);
6250 // 20-2F --> collect
6251 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6255 // 40-7E --> ignore, then switch to GROUND
6256 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6260 // 20-3F, 7F --> ignore
6266 // 0x9C goes to GROUND
6271 // 0x1B 0x5C goes to GROUND
6276 if ((collectBuffer
.length() > 0)
6277 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6283 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6284 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6286 scanState
= ScanState
.DCS_INTERMEDIATE
;
6289 // 30-39, 3B --> param, then switch to DCS_PARAM
6290 if ((ch
>= '0') && (ch
<= '9')) {
6292 scanState
= ScanState
.DCS_PARAM
;
6296 scanState
= ScanState
.DCS_PARAM
;
6299 // 3C-3F --> collect, then switch to DCS_PARAM
6300 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6302 scanState
= ScanState
.DCS_PARAM
;
6305 // 00-17, 19, 1C-1F, 7F --> ignore
6307 // 0x3A goes to DCS_IGNORE
6309 scanState
= ScanState
.DCS_IGNORE
;
6312 // 0x40-7E goes to DCS_PASSTHROUGH
6313 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6314 scanState
= ScanState
.DCS_PASSTHROUGH
;
6318 case DCS_INTERMEDIATE
:
6320 // 0x9C goes to GROUND
6325 // 0x1B 0x5C goes to GROUND
6330 if ((collectBuffer
.length() > 0)
6331 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6337 // 0x30-3F goes to DCS_IGNORE
6338 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6339 scanState
= ScanState
.DCS_IGNORE
;
6342 // 0x40-7E goes to DCS_PASSTHROUGH
6343 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6344 scanState
= ScanState
.DCS_PASSTHROUGH
;
6347 // 00-17, 19, 1C-1F, 7F --> ignore
6352 // 0x9C goes to GROUND
6357 // 0x1B 0x5C goes to GROUND
6362 if ((collectBuffer
.length() > 0)
6363 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6369 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6370 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6372 scanState
= ScanState
.DCS_INTERMEDIATE
;
6375 // 30-39, 3B --> param
6376 if ((ch
>= '0') && (ch
<= '9')) {
6383 // 00-17, 19, 1C-1F, 7F --> ignore
6385 // 0x3A, 3C-3F goes to DCS_IGNORE
6387 scanState
= ScanState
.DCS_IGNORE
;
6389 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6390 scanState
= ScanState
.DCS_IGNORE
;
6393 // 0x40-7E goes to DCS_PASSTHROUGH
6394 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6395 scanState
= ScanState
.DCS_PASSTHROUGH
;
6399 case DCS_PASSTHROUGH
:
6400 // 0x9C goes to GROUND
6405 // 0x1B 0x5C goes to GROUND
6410 if ((collectBuffer
.length() > 0)
6411 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6417 // 00-17, 19, 1C-1F, 20-7E --> put
6425 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6428 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6437 // 00-17, 19, 1C-1F, 20-7F --> ignore
6439 // 0x9C goes to GROUND
6446 case SOSPMAPC_STRING
:
6447 // 00-17, 19, 1C-1F, 20-7F --> ignore
6449 // 0x9C goes to GROUND
6457 // Special case for Xterm: OSC can pass control characters
6458 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
6462 // 00-17, 19, 1C-1F --> ignore
6464 // 20-7F --> osc_put
6465 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6469 // 0x9C goes to GROUND
6476 case VT52_DIRECT_CURSOR_ADDRESS
:
6477 // This is a special case for the VT52 sequence "ESC Y l c"
6478 if (collectBuffer
.length() == 0) {
6480 } else if (collectBuffer
.length() == 1) {
6481 // We've got the two characters, one in the buffer and the
6483 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
6492 * Expose current cursor X to outside world.
6494 * @return current cursor X
6496 public final int getCursorX() {
6497 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6498 return currentState
.cursorX
* 2;
6500 return currentState
.cursorX
;
6504 * Expose current cursor Y to outside world.
6506 * @return current cursor Y
6508 public final int getCursorY() {
6509 return currentState
.cursorY
;