2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.tterminal
;
31 import java
.io
.BufferedOutputStream
;
32 import java
.io
.InputStream
;
33 import java
.io
.InputStreamReader
;
34 import java
.io
.IOException
;
35 import java
.io
.OutputStream
;
36 import java
.io
.OutputStreamWriter
;
37 import java
.io
.Reader
;
38 import java
.io
.UnsupportedEncodingException
;
39 import java
.io
.Writer
;
40 import java
.util
.ArrayList
;
41 import java
.util
.Collections
;
42 import java
.util
.LinkedList
;
43 import java
.util
.List
;
45 import jexer
.TKeypress
;
46 import jexer
.event
.TMouseEvent
;
47 import jexer
.bits
.Color
;
48 import jexer
.bits
.Cell
;
49 import jexer
.bits
.CellAttributes
;
50 import jexer
.io
.ReadTimeoutException
;
51 import jexer
.io
.TimeoutInputStream
;
52 import static jexer
.TKeypress
.*;
55 * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
56 * including a scrollback buffer.
59 * It currently implements VT100, VT102, VT220, and XTERM with the following
63 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
64 * failure with XTERM. This is due to vttest failing to decode the UTF-8
68 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
69 * from VT100 are not supported.
72 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
73 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
74 * because DECSCL is not supported, it will fail the last part of the
75 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
78 * - Numeric/application keys from the number pad are not supported because
79 * they are not exposed from the TKeypress API.
82 * - VT52 HOLD SCREEN mode is not supported.
85 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
86 * numerators) are not rendered correctly.
89 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
91 public class ECMA48
implements Runnable
{
93 // ------------------------------------------------------------------------
94 // Constants --------------------------------------------------------------
95 // ------------------------------------------------------------------------
98 * The emulator can emulate several kinds of terminals.
100 public enum DeviceType
{
102 * DEC VT100 but also including the three VT102 functions.
123 * Parser character scan states.
125 private enum ScanState
{
140 VT52_DIRECT_CURSOR_ADDRESS
144 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
145 * can't really use it in keypress() because we do not see number pad
146 * events from TKeypress.
148 private enum KeypadMode
{
154 * Arrow keys can emit three different sequences (DECCKM or VT52
157 private enum ArrowKeyMode
{
164 * Available character sets for GL, GR, G0, G1, G2, G3.
166 private enum CharacterSet
{
187 * Single-shift states used by the C1 control characters SS2 (0x8E) and
190 private enum Singleshift
{
197 * VT220+ lockshift states.
199 private enum LockshiftMode
{
209 * XTERM mouse reporting protocols.
211 private enum MouseProtocol
{
220 * XTERM mouse reporting encodings.
222 private enum MouseEncoding
{
228 // ------------------------------------------------------------------------
229 // Variables --------------------------------------------------------------
230 // ------------------------------------------------------------------------
233 * The enclosing listening object.
235 private DisplayListener displayListener
;
238 * When true, the reader thread is expected to exit.
240 private volatile boolean stopReaderThread
= false;
245 private Thread readerThread
= null;
248 * The type of emulator to be.
250 private DeviceType type
= DeviceType
.VT102
;
253 * The scrollback buffer characters + attributes.
255 private volatile List
<DisplayLine
> scrollback
;
258 * The raw display buffer characters + attributes.
260 private volatile List
<DisplayLine
> display
;
263 * The terminal's input. For type == XTERM, this is an InputStreamReader
264 * with UTF-8 encoding.
266 private Reader input
;
269 * The terminal's raw InputStream. This is used for type != XTERM.
271 private volatile TimeoutInputStream inputStream
;
274 * The terminal's output. For type == XTERM, this wraps an
275 * OutputStreamWriter with UTF-8 encoding.
277 private Writer output
;
280 * The terminal's raw OutputStream. This is used for type != XTERM.
282 private OutputStream outputStream
;
285 * Current scanning state.
287 private ScanState scanState
;
290 * Which mouse protocol is active.
292 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
295 * Which mouse encoding is active.
297 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
300 * Physical display width. We start at 80x24, but the user can resize us
306 * Physical display height. We start at 80x24, but the user can resize
312 * Top margin of the scrolling region.
314 private int scrollRegionTop
;
317 * Bottom margin of the scrolling region.
319 private int scrollRegionBottom
;
322 * Right margin column number. This can be selected by the remote side
323 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
325 private int rightMargin
;
328 * Last character printed.
333 * VT100-style line wrapping: a character is placed in column 80 (or
334 * 132), but the line does NOT wrap until another character is written to
335 * column 1 of the next line, after which the cursor moves to column 2.
337 private boolean wrapLineFlag
;
340 * VT220 single shift flag.
342 private Singleshift singleshift
= Singleshift
.NONE
;
345 * true = insert characters, false = overwrite.
347 private boolean insertMode
= false;
350 * VT52 mode as selected by DECANM. True means VT52, false means
351 * ANSI. Default is ANSI.
353 private boolean vt52Mode
= false;
356 * Visible cursor (DECTCEM).
358 private boolean cursorVisible
= true;
361 * Screen title as set by the xterm OSC sequence. Lots of applications
362 * send a screenTitle regardless of whether it is an xterm client or not.
364 private String screenTitle
= "";
367 * Parameter characters being collected.
369 private List
<Integer
> csiParams
;
372 * Non-csi collect buffer.
374 private StringBuilder collectBuffer
;
377 * When true, use the G1 character set.
379 private boolean shiftOut
= false;
382 * Horizontal tab stop locations.
384 private List
<Integer
> tabStops
;
387 * S8C1T. True means 8bit controls, false means 7bit controls.
389 private boolean s8c1t
= false;
392 * Printer mode. True means send all output to printer, which discards
395 private boolean printerControllerMode
= false;
398 * LMN line mode. If true, linefeed() puts the cursor on the first
399 * column of the next line. If false, linefeed() puts the cursor one
400 * line down on the current line. The default is false.
402 private boolean newLineMode
= false;
405 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
407 private ArrowKeyMode arrowKeyMode
;
410 * Whether number pad keys send VT100 or VT52, application or numeric
413 @SuppressWarnings("unused")
414 private KeypadMode keypadMode
;
417 * When true, the terminal is in 132-column mode (DECCOLM).
419 private boolean columns132
= false;
422 * true = reverse video. Set by DECSCNM.
424 private boolean reverseVideo
= false;
427 * false = echo characters locally.
429 private boolean fullDuplex
= true;
432 * The current terminal state.
434 private SaveableState currentState
;
437 * The last saved terminal state.
439 private SaveableState savedState
;
442 * The 88- or 256-color support RGB colors.
444 private List
<Integer
> colors88
;
447 * DECSC/DECRC save/restore a subset of the total state. This class
448 * encapsulates those specific flags/modes.
450 private class SaveableState
{
453 * When true, cursor positions are relative to the scrolling region.
455 public boolean originMode
= false;
458 * The current editing X position.
460 public int cursorX
= 0;
463 * The current editing Y position.
465 public int cursorY
= 0;
468 * Which character set is currently selected in G0.
470 public CharacterSet g0Charset
= CharacterSet
.US
;
473 * Which character set is currently selected in G1.
475 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
478 * Which character set is currently selected in G2.
480 public CharacterSet g2Charset
= CharacterSet
.US
;
483 * Which character set is currently selected in G3.
485 public CharacterSet g3Charset
= CharacterSet
.US
;
488 * Which character set is currently selected in GR.
490 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
493 * The current drawing attributes.
495 public CellAttributes attr
;
500 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
505 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
510 public boolean lineWrap
= true;
515 public void reset() {
519 g0Charset
= CharacterSet
.US
;
520 g1Charset
= CharacterSet
.DRAWING
;
521 g2Charset
= CharacterSet
.US
;
522 g3Charset
= CharacterSet
.US
;
523 grCharset
= CharacterSet
.DRAWING
;
524 attr
= new CellAttributes();
525 glLockshift
= LockshiftMode
.NONE
;
526 grLockshift
= LockshiftMode
.NONE
;
531 * Copy attributes from another instance.
533 * @param that the other instance to match
535 public void setTo(final SaveableState that
) {
536 this.originMode
= that
.originMode
;
537 this.cursorX
= that
.cursorX
;
538 this.cursorY
= that
.cursorY
;
539 this.g0Charset
= that
.g0Charset
;
540 this.g1Charset
= that
.g1Charset
;
541 this.g2Charset
= that
.g2Charset
;
542 this.g3Charset
= that
.g3Charset
;
543 this.grCharset
= that
.grCharset
;
544 this.attr
= new CellAttributes();
545 this.attr
.setTo(that
.attr
);
546 this.glLockshift
= that
.glLockshift
;
547 this.grLockshift
= that
.grLockshift
;
548 this.lineWrap
= that
.lineWrap
;
552 * Public constructor.
554 public SaveableState() {
559 // ------------------------------------------------------------------------
560 // Constructors -----------------------------------------------------------
561 // ------------------------------------------------------------------------
564 * Public constructor.
566 * @param type one of the DeviceType constants to select VT100, VT102,
568 * @param inputStream an InputStream connected to the remote side. For
569 * type == XTERM, inputStream is converted to a Reader with UTF-8
571 * @param outputStream an OutputStream connected to the remote user. For
572 * type == XTERM, outputStream is converted to a Writer with UTF-8
574 * @param displayListener a callback to the outer display, or null for
575 * default VT100 behavior
576 * @throws UnsupportedEncodingException if an exception is thrown when
577 * creating the InputStreamReader
579 public ECMA48(final DeviceType type
, final InputStream inputStream
,
580 final OutputStream outputStream
, final DisplayListener displayListener
)
581 throws UnsupportedEncodingException
{
583 assert (inputStream
!= null);
584 assert (outputStream
!= null);
586 csiParams
= new ArrayList
<Integer
>();
587 tabStops
= new ArrayList
<Integer
>();
588 scrollback
= new LinkedList
<DisplayLine
>();
589 display
= new LinkedList
<DisplayLine
>();
592 if (inputStream
instanceof TimeoutInputStream
) {
593 this.inputStream
= (TimeoutInputStream
)inputStream
;
595 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
597 if (type
== DeviceType
.XTERM
) {
598 this.input
= new InputStreamReader(this.inputStream
, "UTF-8");
599 this.output
= new OutputStreamWriter(new
600 BufferedOutputStream(outputStream
), "UTF-8");
601 this.outputStream
= null;
604 this.outputStream
= new BufferedOutputStream(outputStream
);
606 this.displayListener
= displayListener
;
609 for (int i
= 0; i
< height
; i
++) {
610 display
.add(new DisplayLine(currentState
.attr
));
613 // Spin up the input reader
614 readerThread
= new Thread(this);
615 readerThread
.start();
618 // ------------------------------------------------------------------------
619 // Runnable ---------------------------------------------------------------
620 // ------------------------------------------------------------------------
623 * Read function runs on a separate thread.
625 public final void run() {
626 boolean utf8
= false;
627 boolean done
= false;
629 if (type
== DeviceType
.XTERM
) {
633 // available() will often return > 1, so we need to read in chunks to
635 char [] readBufferUTF8
= null;
636 byte [] readBuffer
= null;
638 readBufferUTF8
= new char[128];
640 readBuffer
= new byte[128];
643 while (!done
&& !stopReaderThread
) {
645 int n
= inputStream
.available();
647 // System.err.printf("available() %d\n", n); System.err.flush();
649 if (readBufferUTF8
.length
< n
) {
650 // The buffer wasn't big enough, make it huger
651 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
654 readBufferUTF8
= new char[newSizeHalf
* 2];
657 if (readBuffer
.length
< n
) {
658 // The buffer wasn't big enough, make it huger
659 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
660 readBuffer
= new byte[newSizeHalf
* 2];
666 } catch (InterruptedException e
) {
675 rc
= input
.read(readBufferUTF8
, 0,
676 readBufferUTF8
.length
);
678 rc
= inputStream
.read(readBuffer
, 0,
681 } catch (ReadTimeoutException e
) {
685 // System.err.printf("read() %d\n", rc); System.err.flush();
690 // Don't step on UI events
691 synchronized (this) {
692 for (int i
= 0; i
< rc
; i
++) {
695 ch
= readBufferUTF8
[i
];
703 // Permit my enclosing UI to know that I updated.
704 if (displayListener
!= null) {
705 displayListener
.displayChanged();
708 // System.err.println("end while loop"); System.err.flush();
709 } catch (IOException e
) {
714 } // while ((done == false) && (stopReaderThread == false))
716 // Let the rest of the world know that I am done.
717 stopReaderThread
= true;
720 inputStream
.cancelRead();
723 } catch (IOException e
) {
729 } catch (IOException e
) {
733 // Permit my enclosing UI to know that I updated.
734 if (displayListener
!= null) {
735 displayListener
.displayChanged();
738 // System.err.println("*** run() exiting..."); System.err.flush();
741 // ------------------------------------------------------------------------
742 // ECMA48 -----------------------------------------------------------------
743 // ------------------------------------------------------------------------
746 * Return the proper primary Device Attributes string.
748 * @return string to send to remote side that is appropriate for the
751 private String
deviceTypeResponse() {
754 // "I am a VT100 with advanced video option" (often VT102)
763 // "I am a VT220" - 7 bit version
765 return "\033[?62;1;6c";
767 // "I am a VT220" - 8 bit version
768 return "\u009b?62;1;6c";
770 throw new IllegalArgumentException("Invalid device type: " + type
);
775 * Return the proper TERM environment variable for this device type.
777 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
778 * @return "vt100", "xterm", etc.
780 public static String
deviceTypeTerm(final DeviceType deviceType
) {
781 switch (deviceType
) {
795 throw new IllegalArgumentException("Invalid device type: "
801 * Return the proper LANG for this device type. Only XTERM devices know
802 * about UTF-8, the others are defined by their standard to be either
803 * 7-bit or 8-bit characters only.
805 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
806 * @param baseLang a base language without UTF-8 flag such as "C" or
808 * @return "en_US", "en_US.UTF-8", etc.
810 public static String
deviceTypeLang(final DeviceType deviceType
,
811 final String baseLang
) {
813 switch (deviceType
) {
821 return baseLang
+ ".UTF-8";
824 throw new IllegalArgumentException("Invalid device type: "
830 * Write a string directly to the remote side.
832 * @param str string to send
834 public void writeRemote(final String str
) {
835 if (stopReaderThread
) {
836 // Reader hit EOF, bail out now.
841 // System.err.printf("writeRemote() '%s'\n", str);
847 if (outputStream
== null) {
851 outputStream
.flush();
852 for (int i
= 0; i
< str
.length(); i
++) {
853 outputStream
.write(str
.charAt(i
));
855 outputStream
.flush();
856 } catch (IOException e
) {
862 if (output
== null) {
869 } catch (IOException e
) {
875 throw new IllegalArgumentException("Invalid device type: " + type
);
880 * Close the input and output streams and stop the reader thread. Note
881 * that it is safe to call this multiple times.
883 public final void close() {
885 // Tell the reader thread to stop looking at input. It will close
886 // the input streams.
887 if (stopReaderThread
== false) {
888 stopReaderThread
= true;
890 readerThread
.join(1000);
891 } catch (InterruptedException e
) {
896 // Now close the output stream.
901 if (outputStream
!= null) {
903 outputStream
.close();
904 } catch (IOException e
) {
911 if (outputStream
!= null) {
913 outputStream
.close();
914 } catch (IOException e
) {
919 if (output
!= null) {
922 } catch (IOException e
) {
929 throw new IllegalArgumentException("Invalid device type: " +
935 * See if the reader thread is still running.
937 * @return if true, we are still connected to / reading from the remote
940 public final boolean isReading() {
941 return (!stopReaderThread
);
945 * Obtain a new blank display line for an external user
946 * (e.g. TTerminalWindow).
948 * @return new blank line
950 public final DisplayLine
getBlankDisplayLine() {
951 return new DisplayLine(currentState
.attr
);
955 * Get the scrollback buffer.
957 * @return the scrollback buffer
959 public final List
<DisplayLine
> getScrollbackBuffer() {
964 * Get the display buffer.
966 * @return the display buffer
968 public final List
<DisplayLine
> getDisplayBuffer() {
973 * Get the display width.
975 * @return the width (usually 80 or 132)
977 public final int getWidth() {
982 * Set the display width.
984 * @param width the new width
986 public final void setWidth(final int width
) {
988 rightMargin
= width
- 1;
989 if (currentState
.cursorX
>= width
) {
990 currentState
.cursorX
= width
- 1;
992 if (savedState
.cursorX
>= width
) {
993 savedState
.cursorX
= width
- 1;
998 * Get the display height.
1000 * @return the height (usually 24)
1002 public final int getHeight() {
1007 * Set the display height.
1009 * @param height the new height
1011 public final void setHeight(final int height
) {
1012 int delta
= height
- this.height
;
1013 this.height
= height
;
1014 scrollRegionBottom
+= delta
;
1015 if (scrollRegionBottom
< 0) {
1016 scrollRegionBottom
= height
;
1018 if (scrollRegionTop
>= scrollRegionBottom
) {
1019 scrollRegionTop
= 0;
1021 if (currentState
.cursorY
>= height
) {
1022 currentState
.cursorY
= height
- 1;
1024 if (savedState
.cursorY
>= height
) {
1025 savedState
.cursorY
= height
- 1;
1027 while (display
.size() < height
) {
1028 DisplayLine line
= new DisplayLine(currentState
.attr
);
1029 line
.setReverseColor(reverseVideo
);
1032 while (display
.size() > height
) {
1033 scrollback
.add(display
.remove(0));
1038 * Get visible cursor flag.
1040 * @return if true, the cursor is visible
1042 public final boolean isCursorVisible() {
1043 return cursorVisible
;
1047 * Get the screen title as set by the xterm OSC sequence. Lots of
1048 * applications send a screenTitle regardless of whether it is an xterm
1051 * @return screen title
1053 public final String
getScreenTitle() {
1058 * Get 132 columns value.
1060 * @return if true, the terminal is in 132 column mode
1062 public final boolean isColumns132() {
1067 * Clear the CSI parameters and flags.
1069 private void toGround() {
1071 collectBuffer
= new StringBuilder(8);
1072 scanState
= ScanState
.GROUND
;
1076 * Reset the tab stops list.
1078 private void resetTabStops() {
1080 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
1081 tabStops
.add(new Integer(i
* 8));
1086 * Reset the 88- or 256-colors.
1088 private void resetColors() {
1089 colors88
= new ArrayList
<Integer
>(256);
1090 for (int i
= 0; i
< 256; i
++) {
1094 // Set default system colors.
1095 colors88
.set(0, 0x00000000);
1096 colors88
.set(1, 0x00a80000);
1097 colors88
.set(2, 0x0000a800);
1098 colors88
.set(3, 0x00a85400);
1099 colors88
.set(4, 0x000000a8);
1100 colors88
.set(5, 0x00a800a8);
1101 colors88
.set(6, 0x0000a8a8);
1102 colors88
.set(7, 0x00a8a8a8);
1104 colors88
.set(8, 0x00545454);
1105 colors88
.set(9, 0x00fc5454);
1106 colors88
.set(10, 0x0054fc54);
1107 colors88
.set(11, 0x00fcfc54);
1108 colors88
.set(12, 0x005454fc);
1109 colors88
.set(13, 0x00fc54fc);
1110 colors88
.set(14, 0x0054fcfc);
1111 colors88
.set(15, 0x00fcfcfc);
1115 * Get the RGB value of one of the indexed colors.
1117 * @param index the color index
1118 * @return the RGB value
1120 private int get88Color(final int index
) {
1121 // System.err.print("get88Color: " + index);
1122 if ((index
< 0) || (index
> colors88
.size())) {
1123 // System.err.println(" -- UNKNOWN");
1126 // System.err.printf(" %08x\n", colors88.get(index));
1127 return colors88
.get(index
);
1131 * Set one of the indexed colors to a color specification.
1133 * @param index the color index
1134 * @param spec the specification, typically something like "rgb:aa/bb/cc"
1136 private void set88Color(final int index
, final String spec
) {
1137 // System.err.println("set88Color: " + index + " '" + spec + "'");
1139 if ((index
< 0) || (index
> colors88
.size())) {
1142 if (spec
.startsWith("rgb:")) {
1143 String
[] rgbTokens
= spec
.substring(4).split("/");
1144 if (rgbTokens
.length
== 3) {
1146 int rgb
= (Integer
.parseInt(rgbTokens
[0], 16) << 16);
1147 rgb
|= Integer
.parseInt(rgbTokens
[1], 16) << 8;
1148 rgb
|= Integer
.parseInt(rgbTokens
[2], 16);
1149 // System.err.printf(" set to %08x\n", rgb);
1150 colors88
.set(index
, rgb
);
1151 } catch (NumberFormatException e
) {
1158 if (spec
.toLowerCase().equals("black")) {
1159 colors88
.set(index
, 0x00000000);
1160 } else if (spec
.toLowerCase().equals("red")) {
1161 colors88
.set(index
, 0x00a80000);
1162 } else if (spec
.toLowerCase().equals("green")) {
1163 colors88
.set(index
, 0x0000a800);
1164 } else if (spec
.toLowerCase().equals("yellow")) {
1165 colors88
.set(index
, 0x00a85400);
1166 } else if (spec
.toLowerCase().equals("blue")) {
1167 colors88
.set(index
, 0x000000a8);
1168 } else if (spec
.toLowerCase().equals("magenta")) {
1169 colors88
.set(index
, 0x00a800a8);
1170 } else if (spec
.toLowerCase().equals("cyan")) {
1171 colors88
.set(index
, 0x0000a8a8);
1172 } else if (spec
.toLowerCase().equals("white")) {
1173 colors88
.set(index
, 0x00a8a8a8);
1179 * Reset the emulation state.
1181 private void reset() {
1183 currentState
= new SaveableState();
1184 savedState
= new SaveableState();
1185 scanState
= ScanState
.GROUND
;
1188 scrollRegionTop
= 0;
1189 scrollRegionBottom
= height
- 1;
1190 rightMargin
= width
- 1;
1191 newLineMode
= false;
1192 arrowKeyMode
= ArrowKeyMode
.ANSI
;
1193 keypadMode
= KeypadMode
.Numeric
;
1194 wrapLineFlag
= false;
1195 if (displayListener
!= null) {
1196 width
= displayListener
.getDisplayWidth();
1197 height
= displayListener
.getDisplayHeight();
1198 rightMargin
= width
- 1;
1206 newLineMode
= false;
1207 reverseVideo
= false;
1209 cursorVisible
= true;
1212 singleshift
= Singleshift
.NONE
;
1214 printerControllerMode
= false;
1217 mouseProtocol
= MouseProtocol
.OFF
;
1218 mouseEncoding
= MouseEncoding
.X10
;
1223 // Reset extra colors
1231 * Append a new line to the bottom of the display, adding lines off the
1232 * top to the scrollback buffer.
1234 private void newDisplayLine() {
1235 // Scroll the top line off into the scrollback buffer
1236 scrollback
.add(display
.get(0));
1238 DisplayLine line
= new DisplayLine(currentState
.attr
);
1239 line
.setReverseColor(reverseVideo
);
1244 * Wraps the current line.
1246 private void wrapCurrentLine() {
1247 if (currentState
.cursorY
== height
- 1) {
1250 if (currentState
.cursorY
< height
- 1) {
1251 currentState
.cursorY
++;
1253 currentState
.cursorX
= 0;
1257 * Handle a carriage return.
1259 private void carriageReturn() {
1260 currentState
.cursorX
= 0;
1261 wrapLineFlag
= false;
1265 * Reverse the color of the visible display.
1267 private void invertDisplayColors() {
1268 for (DisplayLine line
: display
) {
1269 line
.setReverseColor(!line
.isReverseColor());
1274 * Handle a linefeed.
1276 private void linefeed() {
1278 if (currentState
.cursorY
< scrollRegionBottom
) {
1279 // Increment screen y
1280 currentState
.cursorY
++;
1283 // Screen y does not increment
1286 * Two cases: either we're inside a scrolling region or not. If
1287 * the scrolling region bottom is the bottom of the screen, then
1288 * push the top line into the buffer. Else scroll the scrolling
1291 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1293 // We're at the bottom of the scroll region, AND the scroll
1294 // region is the entire screen.
1300 // We're at the bottom of the scroll region, AND the scroll
1301 // region is NOT the entire screen.
1302 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1307 currentState
.cursorX
= 0;
1309 wrapLineFlag
= false;
1313 * Prints one character to the display buffer.
1315 * @param ch character to display
1317 private void printCharacter(final char ch
) {
1318 int rightMargin
= this.rightMargin
;
1320 // Check if we have double-width, and if so chop at 40/66 instead of
1322 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1323 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1326 // Check the unusually-complicated line wrapping conditions...
1327 if (currentState
.cursorX
== rightMargin
) {
1329 if (currentState
.lineWrap
== true) {
1331 * This case happens when: the cursor was already on the
1332 * right margin (either through printing or by an explicit
1333 * placement command), and a character was printed.
1335 * The line wraps only when a new character arrives AND the
1336 * cursor is already on the right margin AND has placed a
1337 * character in its cell. Easier to see than to explain.
1339 if (wrapLineFlag
== false) {
1341 * This block marks the case that we are in the margin
1342 * and the first character has been received and printed.
1344 wrapLineFlag
= true;
1347 * This block marks the case that we are in the margin
1348 * and the second character has been received and
1351 wrapLineFlag
= false;
1355 } else if (currentState
.cursorX
<= rightMargin
) {
1357 * This is the normal case: a character came in and was printed
1358 * to the left of the right margin column.
1361 // Turn off VT100 special-case flag
1362 wrapLineFlag
= false;
1365 // "Print" the character
1366 Cell newCell
= new Cell(ch
);
1367 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1368 newCellAttributes
.setTo(currentState
.attr
);
1369 DisplayLine line
= display
.get(currentState
.cursorY
);
1370 // Insert mode special case
1371 if (insertMode
== true) {
1372 line
.insert(currentState
.cursorX
, newCell
);
1374 // Replace an existing character
1375 line
.replace(currentState
.cursorX
, newCell
);
1378 // Increment horizontal
1379 if (wrapLineFlag
== false) {
1380 currentState
.cursorX
++;
1381 if (currentState
.cursorX
> rightMargin
) {
1382 currentState
.cursorX
--;
1388 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1389 * send to the remote side.
1391 * @param mouse mouse event received from the local user
1393 public void mouse(final TMouseEvent mouse
) {
1396 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1397 mouseProtocol, mouseEncoding, mouse);
1400 if (mouseEncoding
== MouseEncoding
.X10
) {
1401 // We will support X10 but only for (160,94) and smaller.
1402 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1407 switch (mouseProtocol
) {
1414 // Only report button presses
1415 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1421 // Only report button presses and releases
1422 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1423 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1431 * Only report button presses, button releases, and motions that
1432 * have a button down (i.e. drag-and-drop).
1434 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1435 if (!mouse
.isMouse1()
1436 && !mouse
.isMouse2()
1437 && !mouse
.isMouse3()
1438 && !mouse
.isMouseWheelUp()
1439 && !mouse
.isMouseWheelDown()
1447 // Report everything
1451 // Now encode the event
1452 StringBuilder sb
= new StringBuilder(6);
1453 if (mouseEncoding
== MouseEncoding
.SGR
) {
1454 sb
.append((char) 0x1B);
1457 if (mouse
.isMouse1()) {
1458 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1463 } else if (mouse
.isMouse2()) {
1464 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1469 } else if (mouse
.isMouse3()) {
1470 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1475 } else if (mouse
.isMouseWheelUp()) {
1477 } else if (mouse
.isMouseWheelDown()) {
1480 // This is motion with no buttons down.
1484 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1487 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1494 // X10 and UTF8 encodings
1495 sb
.append((char) 0x1B);
1498 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1499 sb
.append((char) (0x03 + 32));
1500 } else if (mouse
.isMouse1()) {
1501 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1502 sb
.append((char) (0x00 + 32 + 32));
1504 sb
.append((char) (0x00 + 32));
1506 } else if (mouse
.isMouse2()) {
1507 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1508 sb
.append((char) (0x01 + 32 + 32));
1510 sb
.append((char) (0x01 + 32));
1512 } else if (mouse
.isMouse3()) {
1513 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1514 sb
.append((char) (0x02 + 32 + 32));
1516 sb
.append((char) (0x02 + 32));
1518 } else if (mouse
.isMouseWheelUp()) {
1519 sb
.append((char) (0x04 + 64));
1520 } else if (mouse
.isMouseWheelDown()) {
1521 sb
.append((char) (0x05 + 64));
1523 // This is motion with no buttons down.
1524 sb
.append((char) (0x03 + 32));
1527 sb
.append((char) (mouse
.getX() + 33));
1528 sb
.append((char) (mouse
.getY() + 33));
1531 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1532 writeRemote(sb
.toString());
1536 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1537 * send to the remote side.
1539 * @param keypress keypress received from the local user
1541 public void keypress(final TKeypress keypress
) {
1542 writeRemote(keypressToString(keypress
));
1546 * Build one of the complex xterm keystroke sequences, storing the result in
1547 * xterm_keystroke_buffer.
1549 * @param ss3 the prefix to use based on VT100 state.
1550 * @param first the first character, usually a number.
1551 * @param first the last character, one of the following: ~ A B C D F H
1552 * @param ctrl whether or not ctrl is down
1553 * @param alt whether or not alt is down
1554 * @param shift whether or not shift is down
1555 * @return the buffer with the full key sequence
1557 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1558 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1560 StringBuilder sb
= new StringBuilder(ss3
);
1561 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1565 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1567 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1569 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1571 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1573 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1575 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1577 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1582 return sb
.toString();
1586 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1588 * @param keypress keypress received from the local user
1589 * @return string to transmit to the remote side
1591 private String
keypressToString(final TKeypress keypress
) {
1593 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1595 * If this is a control character, process it like it came from
1598 if (keypress
.getChar() < 0x20) {
1599 handleControlChar(keypress
.getChar());
1601 // Local echo for everything else
1602 printCharacter(keypress
.getChar());
1606 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1611 // Handle control characters
1612 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1613 StringBuilder sb
= new StringBuilder();
1614 char ch
= keypress
.getChar();
1617 return sb
.toString();
1620 // Handle alt characters
1621 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1622 StringBuilder sb
= new StringBuilder("\033");
1623 char ch
= keypress
.getChar();
1625 return sb
.toString();
1628 if (keypress
.equals(kbBackspaceDel
)) {
1641 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1644 switch (arrowKeyMode
) {
1646 return xtermBuildKeySequence("\033[", '1', 'D',
1647 keypress
.isCtrl(), keypress
.isAlt(),
1648 keypress
.isShift());
1650 return xtermBuildKeySequence("\033", '1', 'D',
1651 keypress
.isCtrl(), keypress
.isAlt(),
1652 keypress
.isShift());
1654 return xtermBuildKeySequence("\033O", '1', 'D',
1655 keypress
.isCtrl(), keypress
.isAlt(),
1656 keypress
.isShift());
1659 switch (arrowKeyMode
) {
1670 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1673 switch (arrowKeyMode
) {
1675 return xtermBuildKeySequence("\033[", '1', 'C',
1676 keypress
.isCtrl(), keypress
.isAlt(),
1677 keypress
.isShift());
1679 return xtermBuildKeySequence("\033", '1', 'C',
1680 keypress
.isCtrl(), keypress
.isAlt(),
1681 keypress
.isShift());
1683 return xtermBuildKeySequence("\033O", '1', 'C',
1684 keypress
.isCtrl(), keypress
.isAlt(),
1685 keypress
.isShift());
1688 switch (arrowKeyMode
) {
1699 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1702 switch (arrowKeyMode
) {
1704 return xtermBuildKeySequence("\033[", '1', 'A',
1705 keypress
.isCtrl(), keypress
.isAlt(),
1706 keypress
.isShift());
1708 return xtermBuildKeySequence("\033", '1', 'A',
1709 keypress
.isCtrl(), keypress
.isAlt(),
1710 keypress
.isShift());
1712 return xtermBuildKeySequence("\033O", '1', 'A',
1713 keypress
.isCtrl(), keypress
.isAlt(),
1714 keypress
.isShift());
1717 switch (arrowKeyMode
) {
1728 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1731 switch (arrowKeyMode
) {
1733 return xtermBuildKeySequence("\033[", '1', 'B',
1734 keypress
.isCtrl(), keypress
.isAlt(),
1735 keypress
.isShift());
1737 return xtermBuildKeySequence("\033", '1', 'B',
1738 keypress
.isCtrl(), keypress
.isAlt(),
1739 keypress
.isShift());
1741 return xtermBuildKeySequence("\033O", '1', 'B',
1742 keypress
.isCtrl(), keypress
.isAlt(),
1743 keypress
.isShift());
1746 switch (arrowKeyMode
) {
1757 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1760 switch (arrowKeyMode
) {
1762 return xtermBuildKeySequence("\033[", '1', 'H',
1763 keypress
.isCtrl(), keypress
.isAlt(),
1764 keypress
.isShift());
1766 return xtermBuildKeySequence("\033", '1', 'H',
1767 keypress
.isCtrl(), keypress
.isAlt(),
1768 keypress
.isShift());
1770 return xtermBuildKeySequence("\033O", '1', 'H',
1771 keypress
.isCtrl(), keypress
.isAlt(),
1772 keypress
.isShift());
1775 switch (arrowKeyMode
) {
1786 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1789 switch (arrowKeyMode
) {
1791 return xtermBuildKeySequence("\033[", '1', 'F',
1792 keypress
.isCtrl(), keypress
.isAlt(),
1793 keypress
.isShift());
1795 return xtermBuildKeySequence("\033", '1', 'F',
1796 keypress
.isCtrl(), keypress
.isAlt(),
1797 keypress
.isShift());
1799 return xtermBuildKeySequence("\033O", '1', 'F',
1800 keypress
.isCtrl(), keypress
.isAlt(),
1801 keypress
.isShift());
1804 switch (arrowKeyMode
) {
1815 if (keypress
.equals(kbF1
)) {
1823 if (keypress
.equals(kbF2
)) {
1831 if (keypress
.equals(kbF3
)) {
1839 if (keypress
.equals(kbF4
)) {
1847 if (keypress
.equals(kbF5
)) {
1860 if (keypress
.equals(kbF6
)) {
1873 if (keypress
.equals(kbF7
)) {
1886 if (keypress
.equals(kbF8
)) {
1899 if (keypress
.equals(kbF9
)) {
1912 if (keypress
.equals(kbF10
)) {
1925 if (keypress
.equals(kbF11
)) {
1929 if (keypress
.equals(kbF12
)) {
1933 if (keypress
.equals(kbShiftF1
)) {
1938 if (type
== DeviceType
.XTERM
) {
1944 if (keypress
.equals(kbShiftF2
)) {
1949 if (type
== DeviceType
.XTERM
) {
1955 if (keypress
.equals(kbShiftF3
)) {
1960 if (type
== DeviceType
.XTERM
) {
1966 if (keypress
.equals(kbShiftF4
)) {
1971 if (type
== DeviceType
.XTERM
) {
1977 if (keypress
.equals(kbShiftF5
)) {
1979 return "\033[15;2~";
1982 if (keypress
.equals(kbShiftF6
)) {
1984 return "\033[17;2~";
1987 if (keypress
.equals(kbShiftF7
)) {
1989 return "\033[18;2~";
1992 if (keypress
.equals(kbShiftF8
)) {
1994 return "\033[19;2~";
1997 if (keypress
.equals(kbShiftF9
)) {
1999 return "\033[20;2~";
2002 if (keypress
.equals(kbShiftF10
)) {
2004 return "\033[21;2~";
2007 if (keypress
.equals(kbShiftF11
)) {
2009 return "\033[23;2~";
2012 if (keypress
.equals(kbShiftF12
)) {
2014 return "\033[24;2~";
2017 if (keypress
.equals(kbCtrlF1
)) {
2022 if (type
== DeviceType
.XTERM
) {
2028 if (keypress
.equals(kbCtrlF2
)) {
2033 if (type
== DeviceType
.XTERM
) {
2039 if (keypress
.equals(kbCtrlF3
)) {
2044 if (type
== DeviceType
.XTERM
) {
2050 if (keypress
.equals(kbCtrlF4
)) {
2055 if (type
== DeviceType
.XTERM
) {
2061 if (keypress
.equals(kbCtrlF5
)) {
2063 return "\033[15;5~";
2066 if (keypress
.equals(kbCtrlF6
)) {
2068 return "\033[17;5~";
2071 if (keypress
.equals(kbCtrlF7
)) {
2073 return "\033[18;5~";
2076 if (keypress
.equals(kbCtrlF8
)) {
2078 return "\033[19;5~";
2081 if (keypress
.equals(kbCtrlF9
)) {
2083 return "\033[20;5~";
2086 if (keypress
.equals(kbCtrlF10
)) {
2088 return "\033[21;5~";
2091 if (keypress
.equals(kbCtrlF11
)) {
2093 return "\033[23;5~";
2096 if (keypress
.equals(kbCtrlF12
)) {
2098 return "\033[24;5~";
2101 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2104 return xtermBuildKeySequence("\033[", '5', '~',
2105 keypress
.isCtrl(), keypress
.isAlt(),
2106 keypress
.isShift());
2112 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2115 return xtermBuildKeySequence("\033[", '6', '~',
2116 keypress
.isCtrl(), keypress
.isAlt(),
2117 keypress
.isShift());
2123 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2126 return xtermBuildKeySequence("\033[", '2', '~',
2127 keypress
.isCtrl(), keypress
.isAlt(),
2128 keypress
.isShift());
2134 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2137 return xtermBuildKeySequence("\033[", '3', '~',
2138 keypress
.isCtrl(), keypress
.isAlt(),
2139 keypress
.isShift());
2141 // Delete sends real delete for VTxxx
2146 if (keypress
.equals(kbEnter
)) {
2150 if (keypress
.equals(kbEsc
)) {
2154 if (keypress
.equals(kbAltEsc
)) {
2158 if (keypress
.equals(kbTab
)) {
2162 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2163 (keypress
.equals(kbShiftTab
))
2173 // Non-alt, non-ctrl characters
2174 if (!keypress
.isFnKey()) {
2175 StringBuilder sb
= new StringBuilder();
2176 sb
.append(keypress
.getChar());
2177 return sb
.toString();
2183 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2186 * @param ch 8-bit character from the remote side
2187 * @param charsetGl character set defined for GL
2188 * @param charsetGr character set defined for GR
2189 * @return character to display on the screen
2191 private char mapCharacterCharset(final char ch
,
2192 final CharacterSet charsetGl
,
2193 final CharacterSet charsetGr
) {
2195 int lookupChar
= ch
;
2196 CharacterSet lookupCharset
= charsetGl
;
2199 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2200 lookupCharset
= charsetGr
;
2204 switch (lookupCharset
) {
2207 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2210 return DECCharacterSets
.UK
[lookupChar
];
2213 return DECCharacterSets
.US_ASCII
[lookupChar
];
2216 return DECCharacterSets
.NL
[lookupChar
];
2219 return DECCharacterSets
.FI
[lookupChar
];
2222 return DECCharacterSets
.FR
[lookupChar
];
2225 return DECCharacterSets
.FR_CA
[lookupChar
];
2228 return DECCharacterSets
.DE
[lookupChar
];
2231 return DECCharacterSets
.IT
[lookupChar
];
2234 return DECCharacterSets
.NO
[lookupChar
];
2237 return DECCharacterSets
.ES
[lookupChar
];
2240 return DECCharacterSets
.SV
[lookupChar
];
2243 return DECCharacterSets
.SWISS
[lookupChar
];
2245 case DEC_SUPPLEMENTAL
:
2246 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2249 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2252 return DECCharacterSets
.US_ASCII
[lookupChar
];
2255 return DECCharacterSets
.US_ASCII
[lookupChar
];
2258 throw new IllegalArgumentException("Invalid character set value: "
2264 * Map an 8-bit byte into a printable character.
2266 * @param ch either 8-bit or Unicode character from the remote side
2267 * @return character to display on the screen
2269 private char mapCharacter(final char ch
) {
2271 // Unicode character, just return it
2275 CharacterSet charsetGl
= currentState
.g0Charset
;
2276 CharacterSet charsetGr
= currentState
.grCharset
;
2278 if (vt52Mode
== true) {
2279 if (shiftOut
== true) {
2280 // Shifted out character, pull from VT52 graphics
2281 charsetGl
= currentState
.g1Charset
;
2282 charsetGr
= CharacterSet
.US
;
2285 charsetGl
= currentState
.g0Charset
;
2286 charsetGr
= CharacterSet
.US
;
2289 // Pull the character
2290 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2294 if (shiftOut
== true) {
2295 // Shifted out character, pull from G1
2296 charsetGl
= currentState
.g1Charset
;
2297 charsetGr
= currentState
.grCharset
;
2299 // Pull the character
2300 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2304 if (singleshift
== Singleshift
.SS2
) {
2306 singleshift
= Singleshift
.NONE
;
2308 // Shifted out character, pull from G2
2309 charsetGl
= currentState
.g2Charset
;
2310 charsetGr
= currentState
.grCharset
;
2314 if (singleshift
== Singleshift
.SS3
) {
2316 singleshift
= Singleshift
.NONE
;
2318 // Shifted out character, pull from G3
2319 charsetGl
= currentState
.g3Charset
;
2320 charsetGr
= currentState
.grCharset
;
2323 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2324 // Check for locking shift
2326 switch (currentState
.glLockshift
) {
2339 charsetGl
= currentState
.g2Charset
;
2344 charsetGl
= currentState
.g3Charset
;
2349 charsetGl
= currentState
.g0Charset
;
2353 switch (currentState
.grLockshift
) {
2363 charsetGr
= currentState
.g1Charset
;
2368 charsetGr
= currentState
.g2Charset
;
2373 charsetGr
= currentState
.g3Charset
;
2378 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2385 // Pull the character
2386 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2390 * Scroll the text within a scrolling region up n lines.
2392 * @param regionTop top row of the scrolling region
2393 * @param regionBottom bottom row of the scrolling region
2394 * @param n number of lines to scroll
2396 private void scrollingRegionScrollUp(final int regionTop
,
2397 final int regionBottom
, final int n
) {
2399 if (regionTop
>= regionBottom
) {
2403 // Sanity check: see if there will be any characters left after the
2405 if (regionBottom
+ 1 - regionTop
<= n
) {
2406 // There won't be anything left in the region, so just call
2407 // eraseScreen() and return.
2408 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2412 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2413 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2414 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2416 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2417 - remaining
, regionBottom
+ 1);
2418 display
= new LinkedList
<DisplayLine
>(displayTop
);
2419 display
.addAll(displayMiddle
);
2420 for (int i
= 0; i
< n
; i
++) {
2421 DisplayLine line
= new DisplayLine(currentState
.attr
);
2422 line
.setReverseColor(reverseVideo
);
2425 display
.addAll(displayBottom
);
2427 assert (display
.size() == height
);
2431 * Scroll the text within a scrolling region down n lines.
2433 * @param regionTop top row of the scrolling region
2434 * @param regionBottom bottom row of the scrolling region
2435 * @param n number of lines to scroll
2437 private void scrollingRegionScrollDown(final int regionTop
,
2438 final int regionBottom
, final int n
) {
2440 if (regionTop
>= regionBottom
) {
2444 // Sanity check: see if there will be any characters left after the
2446 if (regionBottom
+ 1 - regionTop
<= n
) {
2447 // There won't be anything left in the region, so just call
2448 // eraseScreen() and return.
2449 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2453 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2454 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2455 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2457 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2458 regionTop
+ remaining
);
2459 display
= new LinkedList
<DisplayLine
>(displayTop
);
2460 for (int i
= 0; i
< n
; i
++) {
2461 DisplayLine line
= new DisplayLine(currentState
.attr
);
2462 line
.setReverseColor(reverseVideo
);
2465 display
.addAll(displayMiddle
);
2466 display
.addAll(displayBottom
);
2468 assert (display
.size() == height
);
2472 * Process a control character.
2474 * @param ch 8-bit character from the remote side
2476 private void handleControlChar(final char ch
) {
2477 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2488 // Transmit the answerback message.
2499 cursorLeft(1, false);
2504 advanceToNextTabStop();
2530 currentState
.glLockshift
= LockshiftMode
.NONE
;
2536 currentState
.glLockshift
= LockshiftMode
.NONE
;
2561 singleshift
= Singleshift
.SS2
;
2566 singleshift
= Singleshift
.SS3
;
2576 * Advance the cursor to the next tab stop.
2578 private void advanceToNextTabStop() {
2579 if (tabStops
.size() == 0) {
2580 // Go to the rightmost column
2581 cursorRight(rightMargin
- currentState
.cursorX
, false);
2584 for (Integer stop
: tabStops
) {
2585 if (stop
> currentState
.cursorX
) {
2586 cursorRight(stop
- currentState
.cursorX
, false);
2591 * We got here, meaning there isn't a tab stop beyond the current
2592 * cursor position. Place the cursor of the right-most edge of the
2595 cursorRight(rightMargin
- currentState
.cursorX
, false);
2599 * Save a character into the collect buffer.
2601 * @param ch character to save
2603 private void collect(final char ch
) {
2604 collectBuffer
.append(ch
);
2608 * Save a byte into the CSI parameters buffer.
2610 * @param ch byte to save
2612 private void param(final byte ch
) {
2613 if (csiParams
.size() == 0) {
2614 csiParams
.add(new Integer(0));
2616 Integer x
= csiParams
.get(csiParams
.size() - 1);
2617 if ((ch
>= '0') && (ch
<= '9')) {
2620 csiParams
.set(csiParams
.size() - 1, x
);
2624 csiParams
.add(new Integer(0));
2629 * Get a CSI parameter value, with a default.
2631 * @param position parameter index. 0 is the first parameter.
2632 * @param defaultValue value to use if csiParams[position] doesn't exist
2633 * @return parameter value
2635 private int getCsiParam(final int position
, final int defaultValue
) {
2636 if (csiParams
.size() < position
+ 1) {
2637 return defaultValue
;
2639 return csiParams
.get(position
).intValue();
2643 * Get a CSI parameter value, clamped to within min/max.
2645 * @param position parameter index. 0 is the first parameter.
2646 * @param defaultValue value to use if csiParams[position] doesn't exist
2647 * @param minValue minimum value inclusive
2648 * @param maxValue maximum value inclusive
2649 * @return parameter value
2651 private int getCsiParam(final int position
, final int defaultValue
,
2652 final int minValue
, final int maxValue
) {
2654 assert (minValue
<= maxValue
);
2655 int value
= getCsiParam(position
, defaultValue
);
2656 if (value
< minValue
) {
2659 if (value
> maxValue
) {
2666 * Set or unset a toggle.
2668 * @param value true for set ('h'), false for reset ('l')
2670 private void setToggle(final boolean value
) {
2671 boolean decPrivateModeFlag
= false;
2672 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2673 if (collectBuffer
.charAt(i
) == '?') {
2674 decPrivateModeFlag
= true;
2679 for (Integer i
: csiParams
) {
2684 if (decPrivateModeFlag
== true) {
2686 if (value
== true) {
2687 // Use application arrow keys
2688 arrowKeyMode
= ArrowKeyMode
.VT100
;
2690 // Use ANSI arrow keys
2691 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2696 if (decPrivateModeFlag
== true) {
2697 if (value
== false) {
2701 arrowKeyMode
= ArrowKeyMode
.VT52
;
2704 * From the VT102 docs: "You use ANSI mode to select
2705 * most terminal features; the terminal uses the same
2706 * features when it switches to VT52 mode. You
2707 * cannot, however, change most of these features in
2710 * In other words, do not reset any other attributes
2711 * when switching between VT52 submode and ANSI.
2713 * HOWEVER, the real vt100 does switch the character
2714 * set according to Usenet.
2716 currentState
.g0Charset
= CharacterSet
.US
;
2717 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2720 if ((type
== DeviceType
.VT220
)
2721 || (type
== DeviceType
.XTERM
)) {
2723 // VT52 mode is explicitly 7-bit
2725 singleshift
= Singleshift
.NONE
;
2730 if (value
== true) {
2731 // Turn off keyboard
2740 if (decPrivateModeFlag
== true) {
2742 if (value
== true) {
2749 if ((displayListener
!= null)
2750 && (type
== DeviceType
.XTERM
)
2752 // For xterms, reset to the actual width, not 80
2754 width
= displayListener
.getDisplayWidth();
2755 rightMargin
= width
- 1;
2758 width
= rightMargin
+ 1;
2761 // Entire screen is cleared, and scrolling region is
2763 eraseScreen(0, 0, height
- 1, width
- 1, false);
2764 scrollRegionTop
= 0;
2765 scrollRegionBottom
= height
- 1;
2766 // Also home the cursor
2767 cursorPosition(0, 0);
2771 if (decPrivateModeFlag
== true) {
2773 if (value
== true) {
2782 if (value
== true) {
2790 if (decPrivateModeFlag
== true) {
2792 if (value
== true) {
2794 * Set selects reverse screen, a white screen
2795 * background with black characters.
2797 if (reverseVideo
!= true) {
2799 * If in normal video, switch it back
2801 invertDisplayColors();
2803 reverseVideo
= true;
2806 * Reset selects normal screen, a black screen
2807 * background with white characters.
2809 if (reverseVideo
== true) {
2811 * If in reverse video already, switch it back
2813 invertDisplayColors();
2815 reverseVideo
= false;
2820 if (decPrivateModeFlag
== true) {
2822 if (value
== true) {
2823 // Origin is relative to scroll region cursor.
2824 // Cursor can NEVER leave scrolling region.
2825 currentState
.originMode
= true;
2826 cursorPosition(0, 0);
2828 // Origin is absolute to entire screen. Cursor can
2829 // leave the scrolling region via cup() and hvp().
2830 currentState
.originMode
= false;
2831 cursorPosition(0, 0);
2836 if (decPrivateModeFlag
== true) {
2838 if (value
== true) {
2840 currentState
.lineWrap
= true;
2842 // Turn linewrap off
2843 currentState
.lineWrap
= false;
2848 if (decPrivateModeFlag
== true) {
2850 if (value
== true) {
2851 // Keyboard auto-repeat on
2854 // Keyboard auto-repeat off
2860 if (decPrivateModeFlag
== false) {
2862 if (value
== true) {
2872 if (decPrivateModeFlag
== true) {
2878 if (decPrivateModeFlag
== true) {
2884 if (decPrivateModeFlag
== false) {
2886 if (value
== true) {
2888 * Set causes a received linefeed, form feed, or
2889 * vertical tab to move cursor to first column of
2890 * next line. RETURN transmits both a carriage return
2891 * and linefeed. This selection is also called new
2897 * Reset causes a received linefeed, form feed, or
2898 * vertical tab to move cursor to next line in
2899 * current column. RETURN transmits a carriage
2902 newLineMode
= false;
2908 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2909 if (decPrivateModeFlag
== true) {
2911 if (value
== true) {
2913 cursorVisible
= true;
2916 cursorVisible
= false;
2923 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2924 if (decPrivateModeFlag
== true) {
2926 if (value
== true) {
2927 // Select national mode NRC
2930 // Select multi-national mode
2939 if ((type
== DeviceType
.XTERM
)
2940 && (decPrivateModeFlag
== true)
2942 // Mouse: normal tracking mode
2943 if (value
== true) {
2944 mouseProtocol
= MouseProtocol
.NORMAL
;
2946 mouseProtocol
= MouseProtocol
.OFF
;
2952 if ((type
== DeviceType
.XTERM
)
2953 && (decPrivateModeFlag
== true)
2955 // Mouse: normal tracking mode
2956 if (value
== true) {
2957 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2959 mouseProtocol
= MouseProtocol
.OFF
;
2965 if ((type
== DeviceType
.XTERM
)
2966 && (decPrivateModeFlag
== true)
2968 // Mouse: Any-event tracking mode
2969 if (value
== true) {
2970 mouseProtocol
= MouseProtocol
.ANYEVENT
;
2972 mouseProtocol
= MouseProtocol
.OFF
;
2978 if ((type
== DeviceType
.XTERM
)
2979 && (decPrivateModeFlag
== true)
2981 // Mouse: UTF-8 coordinates
2982 if (value
== true) {
2983 mouseEncoding
= MouseEncoding
.UTF8
;
2985 mouseEncoding
= MouseEncoding
.X10
;
2991 if ((type
== DeviceType
.XTERM
)
2992 && (decPrivateModeFlag
== true)
2994 // Mouse: SGR coordinates
2995 if (value
== true) {
2996 mouseEncoding
= MouseEncoding
.SGR
;
2998 mouseEncoding
= MouseEncoding
.X10
;
3011 * DECSC - Save cursor.
3013 private void decsc() {
3014 savedState
.setTo(currentState
);
3018 * DECRC - Restore cursor.
3020 private void decrc() {
3021 currentState
.setTo(savedState
);
3027 private void ind() {
3028 // Move the cursor and scroll if necessary. If at the bottom line
3029 // already, a scroll up is supposed to be performed.
3030 if (currentState
.cursorY
== scrollRegionBottom
) {
3031 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3033 cursorDown(1, true);
3037 * RI - Reverse index.
3040 // Move the cursor and scroll if necessary. If at the top line
3041 // already, a scroll down is supposed to be performed.
3042 if (currentState
.cursorY
== scrollRegionTop
) {
3043 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3051 private void nel() {
3052 // Move the cursor and scroll if necessary. If at the bottom line
3053 // already, a scroll up is supposed to be performed.
3054 if (currentState
.cursorY
== scrollRegionBottom
) {
3055 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3057 cursorDown(1, true);
3059 // Reset to the beginning of the next line
3060 currentState
.cursorX
= 0;
3064 * DECKPAM - Keypad application mode.
3066 private void deckpam() {
3067 keypadMode
= KeypadMode
.Application
;
3071 * DECKPNM - Keypad numeric mode.
3073 private void deckpnm() {
3074 keypadMode
= KeypadMode
.Numeric
;
3080 * @param n number of spaces to move
3081 * @param honorScrollRegion if true, then do nothing if the cursor is
3082 * outside the scrolling region
3084 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3088 * Special case: if a user moves the cursor from the right margin, we
3089 * have to reset the VT100 right margin flag.
3092 wrapLineFlag
= false;
3095 for (int i
= 0; i
< n
; i
++) {
3096 if (honorScrollRegion
== true) {
3097 // Honor the scrolling region
3098 if ((currentState
.cursorY
< scrollRegionTop
)
3099 || (currentState
.cursorY
> scrollRegionBottom
)
3101 // Outside region, do nothing
3104 // Inside region, go up
3105 top
= scrollRegionTop
;
3107 // Non-scrolling case
3111 if (currentState
.cursorY
> top
) {
3112 currentState
.cursorY
--;
3118 * Move down n spaces.
3120 * @param n number of spaces to move
3121 * @param honorScrollRegion if true, then do nothing if the cursor is
3122 * outside the scrolling region
3124 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3128 * Special case: if a user moves the cursor from the right margin, we
3129 * have to reset the VT100 right margin flag.
3132 wrapLineFlag
= false;
3135 for (int i
= 0; i
< n
; i
++) {
3137 if (honorScrollRegion
== true) {
3138 // Honor the scrolling region
3139 if (currentState
.cursorY
> scrollRegionBottom
) {
3140 // Outside region, do nothing
3143 // Inside region, go down
3144 bottom
= scrollRegionBottom
;
3146 // Non-scrolling case
3147 bottom
= height
- 1;
3150 if (currentState
.cursorY
< bottom
) {
3151 currentState
.cursorY
++;
3157 * Move left n spaces.
3159 * @param n number of spaces to move
3160 * @param honorScrollRegion if true, then do nothing if the cursor is
3161 * outside the scrolling region
3163 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3165 * Special case: if a user moves the cursor from the right margin, we
3166 * have to reset the VT100 right margin flag.
3169 wrapLineFlag
= false;
3172 for (int i
= 0; i
< n
; i
++) {
3173 if (honorScrollRegion
== true) {
3174 // Honor the scrolling region
3175 if ((currentState
.cursorY
< scrollRegionTop
)
3176 || (currentState
.cursorY
> scrollRegionBottom
)
3178 // Outside region, do nothing
3183 if (currentState
.cursorX
> 0) {
3184 currentState
.cursorX
--;
3190 * Move right n spaces.
3192 * @param n number of spaces to move
3193 * @param honorScrollRegion if true, then do nothing if the cursor is
3194 * outside the scrolling region
3196 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3197 int rightMargin
= this.rightMargin
;
3200 * Special case: if a user moves the cursor from the right margin, we
3201 * have to reset the VT100 right margin flag.
3204 wrapLineFlag
= false;
3207 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3208 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3211 for (int i
= 0; i
< n
; i
++) {
3212 if (honorScrollRegion
== true) {
3213 // Honor the scrolling region
3214 if ((currentState
.cursorY
< scrollRegionTop
)
3215 || (currentState
.cursorY
> scrollRegionBottom
)
3217 // Outside region, do nothing
3222 if (currentState
.cursorX
< rightMargin
) {
3223 currentState
.cursorX
++;
3229 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3231 * @param row row to move to
3232 * @param col column to move to
3234 private void cursorPosition(int row
, final int col
) {
3235 int rightMargin
= this.rightMargin
;
3240 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3241 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3244 // Set column number
3245 currentState
.cursorX
= col
;
3247 // Sanity check, bring column back to margin.
3248 if (currentState
.cursorX
> rightMargin
) {
3249 currentState
.cursorX
= rightMargin
;
3253 if (currentState
.originMode
== true) {
3254 row
+= scrollRegionTop
;
3256 if (currentState
.cursorY
< row
) {
3257 cursorDown(row
- currentState
.cursorY
, false);
3258 } else if (currentState
.cursorY
> row
) {
3259 cursorUp(currentState
.cursorY
- row
, false);
3262 wrapLineFlag
= false;
3266 * HTS - Horizontal tabulation set.
3268 private void hts() {
3269 for (Integer stop
: tabStops
) {
3270 if (stop
== currentState
.cursorX
) {
3271 // Already have a tab stop here
3276 // Append a tab stop to the end of the array and resort them
3277 tabStops
.add(currentState
.cursorX
);
3278 Collections
.sort(tabStops
);
3282 * DECSWL - Single-width line.
3284 private void decswl() {
3285 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3286 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3290 * DECDWL - Double-width line.
3292 private void decdwl() {
3293 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3294 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3298 * DECHDL - Double-height + double-width line.
3300 * @param topHalf if true, this sets the row to be the top half row of a
3303 private void dechdl(final boolean topHalf
) {
3304 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3305 if (topHalf
== true) {
3306 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3308 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3313 * DECALN - Screen alignment display.
3315 private void decaln() {
3316 Cell newCell
= new Cell();
3317 newCell
.setChar('E');
3318 for (DisplayLine line
: display
) {
3319 for (int i
= 0; i
< line
.length(); i
++) {
3320 line
.replace(i
, newCell
);
3326 * DECSCL - Compatibility level.
3328 private void decscl() {
3329 int i
= getCsiParam(0, 0);
3330 int j
= getCsiParam(1, 0);
3334 currentState
.g0Charset
= CharacterSet
.US
;
3335 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3337 } else if (i
== 62) {
3339 if ((j
== 0) || (j
== 2)) {
3341 } else if (j
== 1) {
3348 * CUD - Cursor down.
3350 private void cud() {
3351 cursorDown(getCsiParam(0, 1, 1, height
), true);
3355 * CUF - Cursor forward.
3357 private void cuf() {
3358 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3362 * CUB - Cursor backward.
3364 private void cub() {
3365 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3371 private void cuu() {
3372 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3376 * CUP - Cursor position.
3378 private void cup() {
3379 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3380 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3384 * CNL - Cursor down and to column 1.
3386 private void cnl() {
3387 cursorDown(getCsiParam(0, 1, 1, height
), true);
3389 cursorLeft(currentState
.cursorX
, true);
3393 * CPL - Cursor up and to column 1.
3395 private void cpl() {
3396 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3398 cursorLeft(currentState
.cursorX
, true);
3402 * CHA - Cursor to column # in current row.
3404 private void cha() {
3405 cursorPosition(currentState
.cursorY
,
3406 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3410 * VPA - Cursor to row #, same column.
3412 private void vpa() {
3413 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3414 currentState
.cursorX
);
3418 * ED - Erase in display.
3421 boolean honorProtected
= false;
3422 boolean decPrivateModeFlag
= false;
3424 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3425 if (collectBuffer
.charAt(i
) == '?') {
3426 decPrivateModeFlag
= true;
3431 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3432 && (decPrivateModeFlag
== true)
3434 honorProtected
= true;
3437 int i
= getCsiParam(0, 0);
3440 // Erase from here to end of screen
3441 if (currentState
.cursorY
< height
- 1) {
3442 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3445 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3446 } else if (i
== 1) {
3447 // Erase from beginning of screen to here
3448 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3450 eraseLine(0, currentState
.cursorX
, honorProtected
);
3451 } else if (i
== 2) {
3452 // Erase entire screen
3453 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3458 * EL - Erase in line.
3461 boolean honorProtected
= false;
3462 boolean decPrivateModeFlag
= false;
3464 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3465 if (collectBuffer
.charAt(i
) == '?') {
3466 decPrivateModeFlag
= true;
3471 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3472 && (decPrivateModeFlag
== true)
3474 honorProtected
= true;
3477 int i
= getCsiParam(0, 0);
3480 // Erase from here to end of line
3481 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3482 } else if (i
== 1) {
3483 // Erase from beginning of line to here
3484 eraseLine(0, currentState
.cursorX
, honorProtected
);
3485 } else if (i
== 2) {
3486 // Erase entire line
3487 eraseLine(0, width
- 1, honorProtected
);
3492 * ECH - Erase # of characters in current row.
3494 private void ech() {
3495 int i
= getCsiParam(0, 1, 1, width
);
3497 // Erase from here to i characters
3498 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3505 int i
= getCsiParam(0, 1);
3507 if ((currentState
.cursorY
>= scrollRegionTop
)
3508 && (currentState
.cursorY
<= scrollRegionBottom
)
3511 // I can get the same effect with a scroll-down
3512 scrollingRegionScrollDown(currentState
.cursorY
,
3513 scrollRegionBottom
, i
);
3518 * DCH - Delete char.
3520 private void dch() {
3521 int n
= getCsiParam(0, 1);
3522 DisplayLine line
= display
.get(currentState
.cursorY
);
3523 Cell blank
= new Cell();
3524 for (int i
= 0; i
< n
; i
++) {
3525 line
.delete(currentState
.cursorX
, blank
);
3530 * ICH - Insert blank char at cursor.
3532 private void ich() {
3533 int n
= getCsiParam(0, 1);
3534 DisplayLine line
= display
.get(currentState
.cursorY
);
3535 Cell blank
= new Cell();
3536 for (int i
= 0; i
< n
; i
++) {
3537 line
.insert(currentState
.cursorX
, blank
);
3545 int i
= getCsiParam(0, 1);
3547 if ((currentState
.cursorY
>= scrollRegionTop
)
3548 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3550 // I can get the same effect with a scroll-down
3551 scrollingRegionScrollUp(currentState
.cursorY
,
3552 scrollRegionBottom
, i
);
3557 * HVP - Horizontal and vertical position.
3559 private void hvp() {
3564 * REP - Repeat character.
3566 private void rep() {
3567 int n
= getCsiParam(0, 1);
3568 for (int i
= 0; i
< n
; i
++) {
3569 printCharacter(repCh
);
3577 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3578 getCsiParam(0, 1, 1, height
));
3585 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3586 getCsiParam(0, 1, 1, height
));
3590 * CBT - Go back X tab stops.
3592 private void cbt() {
3593 int tabsToMove
= getCsiParam(0, 1);
3596 for (int i
= 0; i
< tabsToMove
; i
++) {
3597 int j
= currentState
.cursorX
;
3598 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3599 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3607 j
= tabStops
.get(tabI
);
3609 cursorPosition(currentState
.cursorY
, j
);
3614 * CHT - Advance X tab stops.
3616 private void cht() {
3617 int n
= getCsiParam(0, 1);
3618 for (int i
= 0; i
< n
; i
++) {
3619 advanceToNextTabStop();
3624 * SGR - Select graphics rendition.
3626 private void sgr() {
3628 if (csiParams
.size() == 0) {
3629 currentState
.attr
.reset();
3633 int sgrColorMode
= -1;
3634 boolean idx88Color
= false;
3635 boolean rgbColor
= false;
3639 for (Integer i
: csiParams
) {
3641 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
3643 assert (type
== DeviceType
.XTERM
);
3647 * Indexed color mode, we now have the index number.
3649 if (sgrColorMode
== 38) {
3650 currentState
.attr
.setForeColorRGB(get88Color(i
));
3652 assert (sgrColorMode
== 48);
3653 currentState
.attr
.setBackColorRGB(get88Color(i
));
3662 * RGB color mode, we are collecting tokens.
3666 } else if (rgbGreen
== -1) {
3667 rgbGreen
= i
& 0xFF;
3669 int rgb
= rgbRed
<< 16;
3670 rgb
|= rgbGreen
<< 8;
3673 // System.err.printf("RGB: %08x\n", rgb);
3675 if (sgrColorMode
== 38) {
3676 currentState
.attr
.setForeColorRGB(rgb
);
3678 assert (sgrColorMode
== 48);
3679 currentState
.attr
.setBackColorRGB(rgb
);
3700 * Indexed color mode.
3707 * This is neither indexed nor RGB color. Bail out.
3712 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
3718 currentState
.attr
.reset();
3723 currentState
.attr
.setBold(true);
3728 currentState
.attr
.setUnderline(true);
3733 currentState
.attr
.setBlink(true);
3738 currentState
.attr
.setReverse(true);
3745 if (type
== DeviceType
.XTERM
) {
3755 // Set black foreground
3756 currentState
.attr
.setForeColorRGB(get88Color(8));
3759 // Set red foreground
3760 currentState
.attr
.setForeColorRGB(get88Color(9));
3763 // Set green foreground
3764 currentState
.attr
.setForeColorRGB(get88Color(10));
3767 // Set yellow foreground
3768 currentState
.attr
.setForeColorRGB(get88Color(11));
3771 // Set blue foreground
3772 currentState
.attr
.setForeColorRGB(get88Color(12));
3775 // Set magenta foreground
3776 currentState
.attr
.setForeColorRGB(get88Color(13));
3779 // Set cyan foreground
3780 currentState
.attr
.setForeColorRGB(get88Color(14));
3783 // Set white foreground
3784 currentState
.attr
.setForeColorRGB(get88Color(15));
3788 // Set black background
3789 currentState
.attr
.setBackColorRGB(get88Color(8));
3792 // Set red background
3793 currentState
.attr
.setBackColorRGB(get88Color(9));
3796 // Set green background
3797 currentState
.attr
.setBackColorRGB(get88Color(10));
3800 // Set yellow background
3801 currentState
.attr
.setBackColorRGB(get88Color(11));
3804 // Set blue background
3805 currentState
.attr
.setBackColorRGB(get88Color(12));
3808 // Set magenta background
3809 currentState
.attr
.setBackColorRGB(get88Color(13));
3812 // Set cyan background
3813 currentState
.attr
.setBackColorRGB(get88Color(14));
3816 // Set white background
3817 currentState
.attr
.setBackColorRGB(get88Color(15));
3825 if ((type
== DeviceType
.VT220
)
3826 || (type
== DeviceType
.XTERM
)) {
3832 currentState
.attr
.setBold(false);
3837 currentState
.attr
.setUnderline(false);
3842 currentState
.attr
.setBlink(false);
3847 currentState
.attr
.setReverse(false);
3855 // A true VT100/102/220 does not support color, however everyone
3856 // is used to their terminal emulator supporting color so we will
3857 // unconditionally support color for all DeviceType's.
3862 // Set black foreground
3863 currentState
.attr
.setForeColor(Color
.BLACK
);
3864 currentState
.attr
.setForeColorRGB(-1);
3867 // Set red foreground
3868 currentState
.attr
.setForeColor(Color
.RED
);
3869 currentState
.attr
.setForeColorRGB(-1);
3872 // Set green foreground
3873 currentState
.attr
.setForeColor(Color
.GREEN
);
3874 currentState
.attr
.setForeColorRGB(-1);
3877 // Set yellow foreground
3878 currentState
.attr
.setForeColor(Color
.YELLOW
);
3879 currentState
.attr
.setForeColorRGB(-1);
3882 // Set blue foreground
3883 currentState
.attr
.setForeColor(Color
.BLUE
);
3884 currentState
.attr
.setForeColorRGB(-1);
3887 // Set magenta foreground
3888 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3889 currentState
.attr
.setForeColorRGB(-1);
3892 // Set cyan foreground
3893 currentState
.attr
.setForeColor(Color
.CYAN
);
3894 currentState
.attr
.setForeColorRGB(-1);
3897 // Set white foreground
3898 currentState
.attr
.setForeColor(Color
.WHITE
);
3899 currentState
.attr
.setForeColorRGB(-1);
3902 if (type
== DeviceType
.XTERM
) {
3904 * Xterm supports T.416 / ISO-8613-3 codes to select
3905 * either an indexed color or an RGB value. (It also
3906 * permits these ISO-8613-3 SGR sequences to be separated
3907 * by colons rather than semicolons.)
3909 * We will support only the following:
3911 * 1. Indexed color mode (88- or 256-color modes).
3915 * These cover most of the use cases in the real world.
3917 * HOWEVER, note that this is an awful broken "standard",
3918 * with no way to do it "right". See
3919 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
3920 * for a detailed discussion of the current state of RGB
3921 * in various terminals, the point of which is that none
3922 * of them really do the same thing despite all appearing
3926 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
3927 * where it is assumed that supporting just the "indexed
3928 * mode" of these sequences (which could align easily
3929 * with existing SGR colors) is assumed to mean full
3930 * support of 24-bit RGB. So it is all or nothing.
3932 * Finally, these sequences break the assumptions of
3933 * standard ECMA-48 style parsers as pointed out at
3934 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
3935 * Therefore in order to keep a clean display, we cannot
3936 * parse anything else in this sequence.
3941 // Underscore on, default foreground color
3942 currentState
.attr
.setUnderline(true);
3943 currentState
.attr
.setForeColor(Color
.WHITE
);
3947 // Underscore off, default foreground color
3948 currentState
.attr
.setUnderline(false);
3949 currentState
.attr
.setForeColor(Color
.WHITE
);
3950 currentState
.attr
.setForeColorRGB(-1);
3953 // Set black background
3954 currentState
.attr
.setBackColor(Color
.BLACK
);
3955 currentState
.attr
.setBackColorRGB(-1);
3958 // Set red background
3959 currentState
.attr
.setBackColor(Color
.RED
);
3960 currentState
.attr
.setBackColorRGB(-1);
3963 // Set green background
3964 currentState
.attr
.setBackColor(Color
.GREEN
);
3965 currentState
.attr
.setBackColorRGB(-1);
3968 // Set yellow background
3969 currentState
.attr
.setBackColor(Color
.YELLOW
);
3970 currentState
.attr
.setBackColorRGB(-1);
3973 // Set blue background
3974 currentState
.attr
.setBackColor(Color
.BLUE
);
3975 currentState
.attr
.setBackColorRGB(-1);
3978 // Set magenta background
3979 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3980 currentState
.attr
.setBackColorRGB(-1);
3983 // Set cyan background
3984 currentState
.attr
.setBackColor(Color
.CYAN
);
3985 currentState
.attr
.setBackColorRGB(-1);
3988 // Set white background
3989 currentState
.attr
.setBackColor(Color
.WHITE
);
3990 currentState
.attr
.setBackColorRGB(-1);
3993 if (type
== DeviceType
.XTERM
) {
3995 * Xterm supports T.416 / ISO-8613-3 codes to select
3996 * either an indexed color or an RGB value. (It also
3997 * permits these ISO-8613-3 SGR sequences to be separated
3998 * by colons rather than semicolons.)
4000 * We will support only the following:
4002 * 1. Indexed color mode (88- or 256-color modes).
4006 * These cover most of the use cases in the real world.
4008 * HOWEVER, note that this is an awful broken "standard",
4009 * with no way to do it "right". See
4010 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4011 * for a detailed discussion of the current state of RGB
4012 * in various terminals, the point of which is that none
4013 * of them really do the same thing despite all appearing
4017 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4018 * where it is assumed that supporting just the "indexed
4019 * mode" of these sequences (which could align easily
4020 * with existing SGR colors) is assumed to mean full
4021 * support of 24-bit RGB. So it is all or nothing.
4023 * Finally, these sequences break the assumptions of
4024 * standard ECMA-48 style parsers as pointed out at
4025 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4026 * Therefore in order to keep a clean display, we cannot
4027 * parse anything else in this sequence.
4034 // Default background
4035 currentState
.attr
.setBackColor(Color
.BLACK
);
4036 currentState
.attr
.setBackColorRGB(-1);
4046 * DA - Device attributes.
4049 int extendedFlag
= 0;
4051 if (collectBuffer
.length() > 0) {
4052 String args
= collectBuffer
.substring(1);
4053 if (collectBuffer
.charAt(0) == '>') {
4055 if (collectBuffer
.length() >= 2) {
4056 i
= Integer
.parseInt(args
.toString());
4058 } else if (collectBuffer
.charAt(0) == '=') {
4060 if (collectBuffer
.length() >= 2) {
4061 i
= Integer
.parseInt(args
.toString());
4064 // Unknown code, bail out
4069 if ((i
!= 0) && (i
!= 1)) {
4073 if ((extendedFlag
== 0) && (i
== 0)) {
4074 // Send string directly to remote side
4075 writeRemote(deviceTypeResponse());
4079 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4081 if ((extendedFlag
== 1) && (i
== 0)) {
4083 * Request "What type of terminal are you, what is your
4084 * firmware version, and what hardware options do you have
4087 * Respond: "I am a VT220 (identification code of 1), my
4088 * firmware version is _____ (Pv), and I have _____ Po
4089 * options installed."
4095 if (s8c1t
== true) {
4096 writeRemote("\u009b>1;10;0c");
4098 writeRemote("\033[>1;10;0c");
4104 if ((extendedFlag
== 2) && (i
== 0)) {
4107 * Request "What is your unit ID?"
4109 * Respond: "I was manufactured at site 00 and have a unique ID
4113 writeRemote("\033P!|00010203\033\\");
4118 * DECSTBM - Set top and bottom margins.
4120 private void decstbm() {
4121 boolean decPrivateModeFlag
= false;
4123 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4124 if (collectBuffer
.charAt(i
) == '?') {
4125 decPrivateModeFlag
= true;
4129 if (decPrivateModeFlag
) {
4130 // This could be restore DEC private mode values.
4134 int top
= getCsiParam(0, 1, 1, height
) - 1;
4135 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4140 scrollRegionTop
= top
;
4141 scrollRegionBottom
= bottom
;
4144 cursorPosition(0, 0);
4149 * DECREQTPARM - Request terminal parameters.
4151 private void decreqtparm() {
4152 int i
= getCsiParam(0, 0);
4154 if ((i
!= 0) && (i
!= 1)) {
4161 * Request terminal parameters.
4165 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4166 * (CLoCk MULtiplier = 1, STP option flags = 0)
4170 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4173 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4175 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4181 * DECSCA - Select Character Attributes.
4183 private void decsca() {
4184 int i
= getCsiParam(0, 0);
4186 if ((i
== 0) || (i
== 2)) {
4188 currentState
.attr
.setProtect(false);
4192 currentState
.attr
.setProtect(true);
4197 * DECSTR - Soft Terminal Reset.
4199 private void decstr() {
4200 // Do exactly like RIS - Reset to initial state
4202 // Do I clear screen too? I think so...
4203 eraseScreen(0, 0, height
- 1, width
- 1, false);
4204 cursorPosition(0, 0);
4208 * DSR - Device status report.
4210 private void dsr() {
4211 boolean decPrivateModeFlag
= false;
4212 int row
= currentState
.cursorY
;
4214 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4215 if (collectBuffer
.charAt(i
) == '?') {
4216 decPrivateModeFlag
= true;
4221 int i
= getCsiParam(0, 0);
4226 // Request status report. Respond with "OK, no malfunction."
4228 // Send string directly to remote side
4229 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4232 writeRemote("\u009b0n");
4234 writeRemote("\033[0n");
4239 // Request cursor position. Respond with current position.
4240 if (currentState
.originMode
== true) {
4241 row
-= scrollRegionTop
;
4244 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4247 str
= String
.format("\u009b%d;%dR", row
+ 1,
4248 currentState
.cursorX
+ 1);
4250 str
= String
.format("\033[%d;%dR", row
+ 1,
4251 currentState
.cursorX
+ 1);
4254 // Send string directly to remote side
4259 if (decPrivateModeFlag
== true) {
4261 // Request printer status report. Respond with "Printer not
4264 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4265 && (s8c1t
== true)) {
4266 writeRemote("\u009b?13n");
4268 writeRemote("\033[?13n");
4274 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4275 && (decPrivateModeFlag
== true)
4278 // Request user-defined keys are locked or unlocked. Respond
4279 // with "User-defined keys are locked."
4281 if (s8c1t
== true) {
4282 writeRemote("\u009b?21n");
4284 writeRemote("\033[?21n");
4290 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4291 && (decPrivateModeFlag
== true)
4294 // Request keyboard language. Respond with "Keyboard
4295 // language is North American."
4297 if (s8c1t
== true) {
4298 writeRemote("\u009b?27;1n");
4300 writeRemote("\033[?27;1n");
4307 // Some other option, ignore
4313 * TBC - Tabulation clear.
4315 private void tbc() {
4316 int i
= getCsiParam(0, 0);
4318 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4319 for (Integer stop
: tabStops
) {
4320 if (stop
== currentState
.cursorX
) {
4325 tabStops
= newStops
;
4333 * Erase the characters in the current line from the start column to the
4334 * end column, inclusive.
4336 * @param start starting column to erase (between 0 and width - 1)
4337 * @param end ending column to erase (between 0 and width - 1)
4338 * @param honorProtected if true, do not erase characters with the
4339 * protected attribute set
4341 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4346 if (end
> width
- 1) {
4353 for (int i
= start
; i
<= end
; i
++) {
4354 DisplayLine line
= display
.get(currentState
.cursorY
);
4355 if ((!honorProtected
)
4356 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4363 * From the VT102 manual:
4365 * Erasing a character also erases any character
4366 * attribute of the character.
4372 * Erase with the current color a.k.a. back-color erase
4375 line
.setChar(i
, ' ');
4376 line
.setAttr(i
, currentState
.attr
);
4384 * Erase a rectangular section of the screen, inclusive. end column,
4387 * @param startRow starting row to erase (between 0 and height - 1)
4388 * @param startCol starting column to erase (between 0 and width - 1)
4389 * @param endRow ending row to erase (between 0 and height - 1)
4390 * @param endCol ending column to erase (between 0 and width - 1)
4391 * @param honorProtected if true, do not erase characters with the
4392 * protected attribute set
4394 private void eraseScreen(final int startRow
, final int startCol
,
4395 final int endRow
, final int endCol
, final boolean honorProtected
) {
4403 || (endRow
< startRow
)
4404 || (endCol
< startCol
)
4409 oldCursorY
= currentState
.cursorY
;
4410 for (int i
= startRow
; i
<= endRow
; i
++) {
4411 currentState
.cursorY
= i
;
4412 eraseLine(startCol
, endCol
, honorProtected
);
4414 // Erase display clears the double attributes
4415 display
.get(i
).setDoubleWidth(false);
4416 display
.get(i
).setDoubleHeight(0);
4418 currentState
.cursorY
= oldCursorY
;
4422 * VT220 printer functions. All of these are parsed, but won't do
4425 private void printerFunctions() {
4426 boolean decPrivateModeFlag
= false;
4427 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4428 if (collectBuffer
.charAt(i
) == '?') {
4429 decPrivateModeFlag
= true;
4434 int i
= getCsiParam(0, 0);
4439 if (decPrivateModeFlag
== false) {
4445 if (decPrivateModeFlag
== true) {
4446 // Print cursor line
4451 if (decPrivateModeFlag
== true) {
4452 // Auto print mode OFF
4454 // Printer controller OFF
4456 // Characters re-appear on the screen
4457 printerControllerMode
= false;
4462 if (decPrivateModeFlag
== true) {
4466 // Printer controller
4468 // Characters get sucked into oblivion
4469 printerControllerMode
= true;
4480 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4481 * of remote systems will send an XTerm title sequence even if TERM isn't
4484 * @param xtermChar the character received from the remote side
4486 private void oscPut(final char xtermChar
) {
4487 // System.err.println("oscPut: " + xtermChar);
4490 collectBuffer
.append(xtermChar
);
4493 if ((xtermChar
== 0x07)
4494 || (collectBuffer
.toString().endsWith("\033\\"))
4497 if (xtermChar
== 0x07) {
4498 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
4500 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4503 String
[] p
= args
.toString().split(";");
4505 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4512 if (p
[0].equals("4")) {
4513 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
4514 // Set a color index value
4516 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
4517 } catch (NumberFormatException e
) {
4524 // Go to SCAN_GROUND state
4531 * Run this input character through the ECMA48 state machine.
4533 * @param ch character from the remote side
4535 private void consume(char ch
) {
4538 // System.err.printf("%c", ch);
4540 // Special case for VT10x: 7-bit characters only
4541 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4542 ch
= (char)(ch
& 0x7F);
4545 // Special "anywhere" states
4547 // 18, 1A --> execute, then switch to SCAN_GROUND
4548 if ((ch
== 0x18) || (ch
== 0x1A)) {
4549 // CAN and SUB abort escape sequences
4554 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4558 if ((type
== DeviceType
.XTERM
)
4559 && (scanState
== ScanState
.OSC_STRING
)
4561 // Xterm can pass ESCAPE to its OSC sequence.
4562 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
4563 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4564 && (scanState
!= ScanState
.DCS_IGNORE
)
4565 && (scanState
!= ScanState
.DCS_PARAM
)
4566 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4569 scanState
= ScanState
.ESCAPE
;
4574 // 0x9B == CSI 8-bit sequence
4576 scanState
= ScanState
.CSI_ENTRY
;
4580 // 0x9D goes to ScanState.OSC_STRING
4582 scanState
= ScanState
.OSC_STRING
;
4586 // 0x90 goes to DCS_ENTRY
4588 scanState
= ScanState
.DCS_ENTRY
;
4592 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4593 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4594 scanState
= ScanState
.SOSPMAPC_STRING
;
4598 // 0x7F (DEL) is always discarded
4603 switch (scanState
) {
4606 // 00-17, 19, 1C-1F --> execute
4607 // 80-8F, 91-9A, 9C --> execute
4608 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4609 handleControlChar(ch
);
4613 if (((ch
>= 0x20) && (ch
<= 0x7F))
4617 // VT220 printer --> trash bin
4618 if (((type
== DeviceType
.VT220
)
4619 || (type
== DeviceType
.XTERM
))
4620 && (printerControllerMode
== true)
4625 // Hang onto this character
4626 repCh
= mapCharacter(ch
);
4628 // Print this character
4629 printCharacter(repCh
);
4634 // 00-17, 19, 1C-1F --> execute
4636 handleControlChar(ch
);
4640 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4641 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4643 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4647 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4648 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4659 // DECSC - Save cursor
4660 // Note this code overlaps both ANSI and VT52 mode
4665 // DECRC - Restore cursor
4666 // Note this code overlaps both ANSI and VT52 mode
4675 if (vt52Mode
== true) {
4676 // DECANM - Enter ANSI mode
4678 arrowKeyMode
= ArrowKeyMode
.VT100
;
4681 * From the VT102 docs: "You use ANSI mode to select
4682 * most terminal features; the terminal uses the same
4683 * features when it switches to VT52 mode. You
4684 * cannot, however, change most of these features in
4687 * In other words, do not reset any other attributes
4688 * when switching between VT52 submode and ANSI.
4692 currentState
.g0Charset
= CharacterSet
.US
;
4693 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4695 singleshift
= Singleshift
.NONE
;
4696 currentState
.glLockshift
= LockshiftMode
.NONE
;
4697 currentState
.grLockshift
= LockshiftMode
.NONE
;
4701 // DECKPAM - Keypad application mode
4702 // Note this code overlaps both ANSI and VT52 mode
4706 // DECKPNM - Keypad numeric mode
4707 // Note this code overlaps both ANSI and VT52 mode
4714 if (vt52Mode
== true) {
4715 // Cursor up, and stop at the top without scrolling
4720 if (vt52Mode
== true) {
4721 // Cursor down, and stop at the bottom without scrolling
4722 cursorDown(1, false);
4726 if (vt52Mode
== true) {
4727 // Cursor right, and stop at the right without scrolling
4728 cursorRight(1, false);
4732 if (vt52Mode
== true) {
4733 // Cursor left, and stop at the left without scrolling
4734 cursorLeft(1, false);
4741 if (vt52Mode
== true) {
4749 if (vt52Mode
== true) {
4750 // G0 --> Special graphics
4751 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
4755 if (vt52Mode
== true) {
4757 currentState
.g0Charset
= CharacterSet
.US
;
4761 if (vt52Mode
== true) {
4763 cursorPosition(0, 0);
4765 // HTS - Horizontal tabulation set
4770 if (vt52Mode
== true) {
4771 // Reverse line feed. Same as RI.
4776 if (vt52Mode
== true) {
4777 // Erase to end of screen
4778 eraseLine(currentState
.cursorX
, width
- 1, false);
4779 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4784 if (vt52Mode
== true) {
4785 // Erase to end of line
4786 eraseLine(currentState
.cursorX
, width
- 1, false);
4792 if (vt52Mode
== true) {
4795 // RI - Reverse index
4800 if (vt52Mode
== false) {
4802 singleshift
= Singleshift
.SS2
;
4806 if (vt52Mode
== false) {
4808 singleshift
= Singleshift
.SS3
;
4815 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4831 if (vt52Mode
== true) {
4832 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4840 if (vt52Mode
== true) {
4842 // Send string directly to remote side
4843 writeRemote("\033/Z");
4846 // Send string directly to remote side
4847 writeRemote(deviceTypeResponse());
4858 // VT52 cannot get to any of these other states
4859 if (vt52Mode
== true) {
4864 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4871 // RIS - Reset to initial state
4873 // Do I clear screen too? I think so...
4874 eraseScreen(0, 0, height
- 1, width
- 1, false);
4875 cursorPosition(0, 0);
4889 if ((type
== DeviceType
.VT220
)
4890 || (type
== DeviceType
.XTERM
)) {
4892 // VT220 lockshift G2 into GL
4893 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4898 if ((type
== DeviceType
.VT220
)
4899 || (type
== DeviceType
.XTERM
)) {
4901 // VT220 lockshift G3 into GL
4902 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4920 if ((type
== DeviceType
.VT220
)
4921 || (type
== DeviceType
.XTERM
)) {
4923 // VT220 lockshift G3 into GR
4924 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4929 if ((type
== DeviceType
.VT220
)
4930 || (type
== DeviceType
.XTERM
)) {
4932 // VT220 lockshift G2 into GR
4933 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4939 if ((type
== DeviceType
.VT220
)
4940 || (type
== DeviceType
.XTERM
)) {
4942 // VT220 lockshift G1 into GR
4943 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4953 // 0x5B goes to CSI_ENTRY
4955 scanState
= ScanState
.CSI_ENTRY
;
4958 // 0x5D goes to OSC_STRING
4960 scanState
= ScanState
.OSC_STRING
;
4963 // 0x50 goes to DCS_ENTRY
4965 scanState
= ScanState
.DCS_ENTRY
;
4968 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4969 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4970 scanState
= ScanState
.SOSPMAPC_STRING
;
4975 case ESCAPE_INTERMEDIATE
:
4976 // 00-17, 19, 1C-1F --> execute
4978 handleControlChar(ch
);
4981 // 20-2F --> collect
4982 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4986 // 30-7E --> dispatch, then switch to GROUND
4987 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4990 if ((collectBuffer
.length() == 1)
4991 && (collectBuffer
.charAt(0) == '(')) {
4992 // G0 --> Special graphics
4993 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4995 if ((collectBuffer
.length() == 1)
4996 && (collectBuffer
.charAt(0) == ')')) {
4997 // G1 --> Special graphics
4998 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5000 if ((type
== DeviceType
.VT220
)
5001 || (type
== DeviceType
.XTERM
)) {
5003 if ((collectBuffer
.length() == 1)
5004 && (collectBuffer
.charAt(0) == '*')) {
5005 // G2 --> Special graphics
5006 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5008 if ((collectBuffer
.length() == 1)
5009 && (collectBuffer
.charAt(0) == '+')) {
5010 // G3 --> Special graphics
5011 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5016 if ((collectBuffer
.length() == 1)
5017 && (collectBuffer
.charAt(0) == '(')) {
5018 // G0 --> Alternate character ROM standard character set
5019 currentState
.g0Charset
= CharacterSet
.ROM
;
5021 if ((collectBuffer
.length() == 1)
5022 && (collectBuffer
.charAt(0) == ')')) {
5023 // G1 --> Alternate character ROM standard character set
5024 currentState
.g1Charset
= CharacterSet
.ROM
;
5028 if ((collectBuffer
.length() == 1)
5029 && (collectBuffer
.charAt(0) == '(')) {
5030 // G0 --> Alternate character ROM special graphics
5031 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5033 if ((collectBuffer
.length() == 1)
5034 && (collectBuffer
.charAt(0) == ')')) {
5035 // G1 --> Alternate character ROM special graphics
5036 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5040 if ((collectBuffer
.length() == 1)
5041 && (collectBuffer
.charAt(0) == '#')) {
5042 // DECDHL - Double-height line (top half)
5047 if ((collectBuffer
.length() == 1)
5048 && (collectBuffer
.charAt(0) == '#')) {
5049 // DECDHL - Double-height line (bottom half)
5052 if ((type
== DeviceType
.VT220
)
5053 || (type
== DeviceType
.XTERM
)) {
5055 if ((collectBuffer
.length() == 1)
5056 && (collectBuffer
.charAt(0) == '(')) {
5058 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5060 if ((collectBuffer
.length() == 1)
5061 && (collectBuffer
.charAt(0) == ')')) {
5063 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5065 if ((collectBuffer
.length() == 1)
5066 && (collectBuffer
.charAt(0) == '*')) {
5068 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5070 if ((collectBuffer
.length() == 1)
5071 && (collectBuffer
.charAt(0) == '+')) {
5073 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5078 if ((collectBuffer
.length() == 1)
5079 && (collectBuffer
.charAt(0) == '#')) {
5080 // DECSWL - Single-width line
5083 if ((type
== DeviceType
.VT220
)
5084 || (type
== DeviceType
.XTERM
)) {
5086 if ((collectBuffer
.length() == 1)
5087 && (collectBuffer
.charAt(0) == '(')) {
5089 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5091 if ((collectBuffer
.length() == 1)
5092 && (collectBuffer
.charAt(0) == ')')) {
5094 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5096 if ((collectBuffer
.length() == 1)
5097 && (collectBuffer
.charAt(0) == '*')) {
5099 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5101 if ((collectBuffer
.length() == 1)
5102 && (collectBuffer
.charAt(0) == '+')) {
5104 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5109 if ((collectBuffer
.length() == 1)
5110 && (collectBuffer
.charAt(0) == '#')) {
5111 // DECDWL - Double-width line
5114 if ((type
== DeviceType
.VT220
)
5115 || (type
== DeviceType
.XTERM
)) {
5117 if ((collectBuffer
.length() == 1)
5118 && (collectBuffer
.charAt(0) == '(')) {
5120 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5122 if ((collectBuffer
.length() == 1)
5123 && (collectBuffer
.charAt(0) == ')')) {
5125 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5127 if ((collectBuffer
.length() == 1)
5128 && (collectBuffer
.charAt(0) == '*')) {
5130 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5132 if ((collectBuffer
.length() == 1)
5133 && (collectBuffer
.charAt(0) == '+')) {
5135 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5140 if ((type
== DeviceType
.VT220
)
5141 || (type
== DeviceType
.XTERM
)) {
5143 if ((collectBuffer
.length() == 1)
5144 && (collectBuffer
.charAt(0) == '(')) {
5146 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5148 if ((collectBuffer
.length() == 1)
5149 && (collectBuffer
.charAt(0) == ')')) {
5151 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5153 if ((collectBuffer
.length() == 1)
5154 && (collectBuffer
.charAt(0) == '*')) {
5156 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5158 if ((collectBuffer
.length() == 1)
5159 && (collectBuffer
.charAt(0) == '+')) {
5161 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5166 if ((collectBuffer
.length() == 1)
5167 && (collectBuffer
.charAt(0) == '#')) {
5168 // DECALN - Screen alignment display
5177 if ((type
== DeviceType
.VT220
)
5178 || (type
== DeviceType
.XTERM
)) {
5180 if ((collectBuffer
.length() == 1)
5181 && (collectBuffer
.charAt(0) == '(')) {
5182 // G0 --> DEC_SUPPLEMENTAL
5183 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5185 if ((collectBuffer
.length() == 1)
5186 && (collectBuffer
.charAt(0) == ')')) {
5187 // G1 --> DEC_SUPPLEMENTAL
5188 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5190 if ((collectBuffer
.length() == 1)
5191 && (collectBuffer
.charAt(0) == '*')) {
5192 // G2 --> DEC_SUPPLEMENTAL
5193 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5195 if ((collectBuffer
.length() == 1)
5196 && (collectBuffer
.charAt(0) == '+')) {
5197 // G3 --> DEC_SUPPLEMENTAL
5198 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5203 if ((type
== DeviceType
.VT220
)
5204 || (type
== DeviceType
.XTERM
)) {
5206 if ((collectBuffer
.length() == 1)
5207 && (collectBuffer
.charAt(0) == '(')) {
5209 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5211 if ((collectBuffer
.length() == 1)
5212 && (collectBuffer
.charAt(0) == ')')) {
5214 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5216 if ((collectBuffer
.length() == 1)
5217 && (collectBuffer
.charAt(0) == '*')) {
5219 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5221 if ((collectBuffer
.length() == 1)
5222 && (collectBuffer
.charAt(0) == '+')) {
5224 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5233 if ((collectBuffer
.length() == 1)
5234 && (collectBuffer
.charAt(0) == '(')) {
5235 // G0 --> United Kingdom set
5236 currentState
.g0Charset
= CharacterSet
.UK
;
5238 if ((collectBuffer
.length() == 1)
5239 && (collectBuffer
.charAt(0) == ')')) {
5240 // G1 --> United Kingdom set
5241 currentState
.g1Charset
= CharacterSet
.UK
;
5243 if ((type
== DeviceType
.VT220
)
5244 || (type
== DeviceType
.XTERM
)) {
5246 if ((collectBuffer
.length() == 1)
5247 && (collectBuffer
.charAt(0) == '*')) {
5248 // G2 --> United Kingdom set
5249 currentState
.g2Charset
= CharacterSet
.UK
;
5251 if ((collectBuffer
.length() == 1)
5252 && (collectBuffer
.charAt(0) == '+')) {
5253 // G3 --> United Kingdom set
5254 currentState
.g3Charset
= CharacterSet
.UK
;
5259 if ((collectBuffer
.length() == 1)
5260 && (collectBuffer
.charAt(0) == '(')) {
5262 currentState
.g0Charset
= CharacterSet
.US
;
5264 if ((collectBuffer
.length() == 1)
5265 && (collectBuffer
.charAt(0) == ')')) {
5267 currentState
.g1Charset
= CharacterSet
.US
;
5269 if ((type
== DeviceType
.VT220
)
5270 || (type
== DeviceType
.XTERM
)) {
5272 if ((collectBuffer
.length() == 1)
5273 && (collectBuffer
.charAt(0) == '*')) {
5275 currentState
.g2Charset
= CharacterSet
.US
;
5277 if ((collectBuffer
.length() == 1)
5278 && (collectBuffer
.charAt(0) == '+')) {
5280 currentState
.g3Charset
= CharacterSet
.US
;
5285 if ((type
== DeviceType
.VT220
)
5286 || (type
== DeviceType
.XTERM
)) {
5288 if ((collectBuffer
.length() == 1)
5289 && (collectBuffer
.charAt(0) == '(')) {
5291 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5293 if ((collectBuffer
.length() == 1)
5294 && (collectBuffer
.charAt(0) == ')')) {
5296 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5298 if ((collectBuffer
.length() == 1)
5299 && (collectBuffer
.charAt(0) == '*')) {
5301 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5303 if ((collectBuffer
.length() == 1)
5304 && (collectBuffer
.charAt(0) == '+')) {
5306 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5313 if ((type
== DeviceType
.VT220
)
5314 || (type
== DeviceType
.XTERM
)) {
5316 if ((collectBuffer
.length() == 1)
5317 && (collectBuffer
.charAt(0) == '(')) {
5319 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5321 if ((collectBuffer
.length() == 1)
5322 && (collectBuffer
.charAt(0) == ')')) {
5324 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5326 if ((collectBuffer
.length() == 1)
5327 && (collectBuffer
.charAt(0) == '*')) {
5329 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5331 if ((collectBuffer
.length() == 1)
5332 && (collectBuffer
.charAt(0) == '+')) {
5334 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5339 if ((type
== DeviceType
.VT220
)
5340 || (type
== DeviceType
.XTERM
)) {
5342 if ((collectBuffer
.length() == 1)
5343 && (collectBuffer
.charAt(0) == ' ')) {
5350 if ((type
== DeviceType
.VT220
)
5351 || (type
== DeviceType
.XTERM
)) {
5353 if ((collectBuffer
.length() == 1)
5354 && (collectBuffer
.charAt(0) == ' ')) {
5361 if ((type
== DeviceType
.VT220
)
5362 || (type
== DeviceType
.XTERM
)) {
5364 if ((collectBuffer
.length() == 1)
5365 && (collectBuffer
.charAt(0) == '(')) {
5367 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5369 if ((collectBuffer
.length() == 1)
5370 && (collectBuffer
.charAt(0) == ')')) {
5372 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5374 if ((collectBuffer
.length() == 1)
5375 && (collectBuffer
.charAt(0) == '*')) {
5377 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5379 if ((collectBuffer
.length() == 1)
5380 && (collectBuffer
.charAt(0) == '+')) {
5382 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5390 if ((type
== DeviceType
.VT220
)
5391 || (type
== DeviceType
.XTERM
)) {
5393 if ((collectBuffer
.length() == 1)
5394 && (collectBuffer
.charAt(0) == '(')) {
5396 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5398 if ((collectBuffer
.length() == 1)
5399 && (collectBuffer
.charAt(0) == ')')) {
5401 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5403 if ((collectBuffer
.length() == 1)
5404 && (collectBuffer
.charAt(0) == '*')) {
5406 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5408 if ((collectBuffer
.length() == 1)
5409 && (collectBuffer
.charAt(0) == '+')) {
5411 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5422 if ((type
== DeviceType
.VT220
)
5423 || (type
== DeviceType
.XTERM
)) {
5425 if ((collectBuffer
.length() == 1)
5426 && (collectBuffer
.charAt(0) == '(')) {
5428 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5430 if ((collectBuffer
.length() == 1)
5431 && (collectBuffer
.charAt(0) == ')')) {
5433 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5435 if ((collectBuffer
.length() == 1)
5436 && (collectBuffer
.charAt(0) == '*')) {
5438 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5440 if ((collectBuffer
.length() == 1)
5441 && (collectBuffer
.charAt(0) == '+')) {
5443 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5448 if ((type
== DeviceType
.VT220
)
5449 || (type
== DeviceType
.XTERM
)) {
5451 if ((collectBuffer
.length() == 1)
5452 && (collectBuffer
.charAt(0) == '(')) {
5454 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5456 if ((collectBuffer
.length() == 1)
5457 && (collectBuffer
.charAt(0) == ')')) {
5459 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5461 if ((collectBuffer
.length() == 1)
5462 && (collectBuffer
.charAt(0) == '*')) {
5464 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5466 if ((collectBuffer
.length() == 1)
5467 && (collectBuffer
.charAt(0) == '+')) {
5469 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5481 if ((type
== DeviceType
.VT220
)
5482 || (type
== DeviceType
.XTERM
)) {
5484 if ((collectBuffer
.length() == 1)
5485 && (collectBuffer
.charAt(0) == '(')) {
5487 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5489 if ((collectBuffer
.length() == 1)
5490 && (collectBuffer
.charAt(0) == ')')) {
5492 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5494 if ((collectBuffer
.length() == 1)
5495 && (collectBuffer
.charAt(0) == '*')) {
5497 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5499 if ((collectBuffer
.length() == 1)
5500 && (collectBuffer
.charAt(0) == '+')) {
5502 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5507 if ((type
== DeviceType
.VT220
)
5508 || (type
== DeviceType
.XTERM
)) {
5510 if ((collectBuffer
.length() == 1)
5511 && (collectBuffer
.charAt(0) == '(')) {
5513 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5515 if ((collectBuffer
.length() == 1)
5516 && (collectBuffer
.charAt(0) == ')')) {
5518 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5520 if ((collectBuffer
.length() == 1)
5521 && (collectBuffer
.charAt(0) == '*')) {
5523 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5525 if ((collectBuffer
.length() == 1)
5526 && (collectBuffer
.charAt(0) == '+')) {
5528 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5575 // 0x9C goes to GROUND
5583 // 00-17, 19, 1C-1F --> execute
5585 handleControlChar(ch
);
5588 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5589 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5591 scanState
= ScanState
.CSI_INTERMEDIATE
;
5594 // 30-39, 3B --> param, then switch to CSI_PARAM
5595 if ((ch
>= '0') && (ch
<= '9')) {
5597 scanState
= ScanState
.CSI_PARAM
;
5601 scanState
= ScanState
.CSI_PARAM
;
5604 // 3C-3F --> collect, then switch to CSI_PARAM
5605 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5607 scanState
= ScanState
.CSI_PARAM
;
5610 // 40-7E --> dispatch, then switch to GROUND
5611 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5614 // ICH - Insert character
5622 // CUD - Cursor down
5626 // CUF - Cursor forward
5630 // CUB - Cursor backward
5634 // CNL - Cursor down and to column 1
5635 if (type
== DeviceType
.XTERM
) {
5640 // CPL - Cursor up and to column 1
5641 if (type
== DeviceType
.XTERM
) {
5646 // CHA - Cursor to column # in current row
5647 if (type
== DeviceType
.XTERM
) {
5652 // CUP - Cursor position
5656 // CHT - Cursor forward X tab stops (default 1)
5657 if (type
== DeviceType
.XTERM
) {
5662 // ED - Erase in display
5666 // EL - Erase in line
5681 // DCH - Delete character
5688 // Scroll up X lines (default 1)
5689 if (type
== DeviceType
.XTERM
) {
5694 // Scroll down X lines (default 1)
5695 if (type
== DeviceType
.XTERM
) {
5704 if ((type
== DeviceType
.VT220
)
5705 || (type
== DeviceType
.XTERM
)) {
5707 // ECH - Erase character
5714 // CBT - Cursor backward X tab stops (default 1)
5715 if (type
== DeviceType
.XTERM
) {
5726 // HPA - Cursor to column # in current row. Same as CHA
5727 if (type
== DeviceType
.XTERM
) {
5732 // HPR - Cursor right. Same as CUF
5733 if (type
== DeviceType
.XTERM
) {
5738 // REP - Repeat last char X times
5739 if (type
== DeviceType
.XTERM
) {
5744 // DA - Device attributes
5748 // VPA - Cursor to row, current column.
5749 if (type
== DeviceType
.XTERM
) {
5754 // VPR - Cursor down. Same as CUD
5755 if (type
== DeviceType
.XTERM
) {
5760 // HVP - Horizontal and vertical position
5764 // TBC - Tabulation clear
5768 // Sets an ANSI or DEC private toggle
5772 if ((type
== DeviceType
.VT220
)
5773 || (type
== DeviceType
.XTERM
)) {
5775 // Printer functions
5783 // Sets an ANSI or DEC private toggle
5787 // SGR - Select graphics rendition
5791 // DSR - Device status report
5798 // DECLL - Load leds
5802 // DECSTBM - Set top and bottom margins
5806 // Save cursor (ANSI.SYS)
5807 if (type
== DeviceType
.XTERM
) {
5808 savedState
.cursorX
= currentState
.cursorX
;
5809 savedState
.cursorY
= currentState
.cursorY
;
5815 // Restore cursor (ANSI.SYS)
5816 if (type
== DeviceType
.XTERM
) {
5817 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5824 // DECREQTPARM - Request terminal parameters
5840 // 0x9C goes to GROUND
5845 // 0x3A goes to CSI_IGNORE
5847 scanState
= ScanState
.CSI_IGNORE
;
5852 // 00-17, 19, 1C-1F --> execute
5854 handleControlChar(ch
);
5857 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5858 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5860 scanState
= ScanState
.CSI_INTERMEDIATE
;
5863 // 30-39, 3B --> param
5864 if ((ch
>= '0') && (ch
<= '9')) {
5871 // 0x3A goes to CSI_IGNORE
5873 scanState
= ScanState
.CSI_IGNORE
;
5875 // 0x3C-3F goes to CSI_IGNORE
5876 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5877 scanState
= ScanState
.CSI_IGNORE
;
5880 // 40-7E --> dispatch, then switch to GROUND
5881 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5884 // ICH - Insert character
5892 // CUD - Cursor down
5896 // CUF - Cursor forward
5900 // CUB - Cursor backward
5904 // CNL - Cursor down and to column 1
5905 if (type
== DeviceType
.XTERM
) {
5910 // CPL - Cursor up and to column 1
5911 if (type
== DeviceType
.XTERM
) {
5916 // CHA - Cursor to column # in current row
5917 if (type
== DeviceType
.XTERM
) {
5922 // CUP - Cursor position
5926 // CHT - Cursor forward X tab stops (default 1)
5927 if (type
== DeviceType
.XTERM
) {
5932 // ED - Erase in display
5936 // EL - Erase in line
5951 // DCH - Delete character
5958 // Scroll up X lines (default 1)
5959 if (type
== DeviceType
.XTERM
) {
5964 // Scroll down X lines (default 1)
5965 if (type
== DeviceType
.XTERM
) {
5974 if ((type
== DeviceType
.VT220
)
5975 || (type
== DeviceType
.XTERM
)) {
5977 // ECH - Erase character
5984 // CBT - Cursor backward X tab stops (default 1)
5985 if (type
== DeviceType
.XTERM
) {
5996 // HPA - Cursor to column # in current row. Same as CHA
5997 if (type
== DeviceType
.XTERM
) {
6002 // HPR - Cursor right. Same as CUF
6003 if (type
== DeviceType
.XTERM
) {
6008 // REP - Repeat last char X times
6009 if (type
== DeviceType
.XTERM
) {
6014 // DA - Device attributes
6018 // VPA - Cursor to row, current column.
6019 if (type
== DeviceType
.XTERM
) {
6024 // VPR - Cursor down. Same as CUD
6025 if (type
== DeviceType
.XTERM
) {
6030 // HVP - Horizontal and vertical position
6034 // TBC - Tabulation clear
6038 // Sets an ANSI or DEC private toggle
6042 if ((type
== DeviceType
.VT220
)
6043 || (type
== DeviceType
.XTERM
)) {
6045 // Printer functions
6053 // Sets an ANSI or DEC private toggle
6057 // SGR - Select graphics rendition
6061 // DSR - Device status report
6068 // DECLL - Load leds
6072 // DECSTBM - Set top and bottom margins
6082 // DECREQTPARM - Request terminal parameters
6099 case CSI_INTERMEDIATE
:
6100 // 00-17, 19, 1C-1F --> execute
6102 handleControlChar(ch
);
6105 // 20-2F --> collect
6106 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6110 // 0x30-3F goes to CSI_IGNORE
6111 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6112 scanState
= ScanState
.CSI_IGNORE
;
6115 // 40-7E --> dispatch, then switch to GROUND
6116 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6168 if (((type
== DeviceType
.VT220
)
6169 || (type
== DeviceType
.XTERM
))
6170 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6172 // DECSCL - compatibility level
6175 if ((type
== DeviceType
.XTERM
)
6176 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6178 // DECSTR - Soft terminal reset
6183 if (((type
== DeviceType
.VT220
)
6184 || (type
== DeviceType
.XTERM
))
6185 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6213 // 00-17, 19, 1C-1F --> execute
6215 handleControlChar(ch
);
6218 // 20-2F --> collect
6219 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6223 // 40-7E --> ignore, then switch to GROUND
6224 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6228 // 20-3F, 7F --> ignore
6234 // 0x9C goes to GROUND
6239 // 0x1B 0x5C goes to GROUND
6244 if ((collectBuffer
.length() > 0)
6245 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6251 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6252 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6254 scanState
= ScanState
.DCS_INTERMEDIATE
;
6257 // 30-39, 3B --> param, then switch to DCS_PARAM
6258 if ((ch
>= '0') && (ch
<= '9')) {
6260 scanState
= ScanState
.DCS_PARAM
;
6264 scanState
= ScanState
.DCS_PARAM
;
6267 // 3C-3F --> collect, then switch to DCS_PARAM
6268 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6270 scanState
= ScanState
.DCS_PARAM
;
6273 // 00-17, 19, 1C-1F, 7F --> ignore
6275 // 0x3A goes to DCS_IGNORE
6277 scanState
= ScanState
.DCS_IGNORE
;
6280 // 0x40-7E goes to DCS_PASSTHROUGH
6281 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6282 scanState
= ScanState
.DCS_PASSTHROUGH
;
6286 case DCS_INTERMEDIATE
:
6288 // 0x9C goes to GROUND
6293 // 0x1B 0x5C goes to GROUND
6298 if ((collectBuffer
.length() > 0)
6299 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6305 // 0x30-3F goes to DCS_IGNORE
6306 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6307 scanState
= ScanState
.DCS_IGNORE
;
6310 // 0x40-7E goes to DCS_PASSTHROUGH
6311 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6312 scanState
= ScanState
.DCS_PASSTHROUGH
;
6315 // 00-17, 19, 1C-1F, 7F --> ignore
6320 // 0x9C goes to GROUND
6325 // 0x1B 0x5C goes to GROUND
6330 if ((collectBuffer
.length() > 0)
6331 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6337 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6338 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6340 scanState
= ScanState
.DCS_INTERMEDIATE
;
6343 // 30-39, 3B --> param
6344 if ((ch
>= '0') && (ch
<= '9')) {
6351 // 00-17, 19, 1C-1F, 7F --> ignore
6353 // 0x3A, 3C-3F goes to DCS_IGNORE
6355 scanState
= ScanState
.DCS_IGNORE
;
6357 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6358 scanState
= ScanState
.DCS_IGNORE
;
6361 // 0x40-7E goes to DCS_PASSTHROUGH
6362 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6363 scanState
= ScanState
.DCS_PASSTHROUGH
;
6367 case DCS_PASSTHROUGH
:
6368 // 0x9C goes to GROUND
6373 // 0x1B 0x5C goes to GROUND
6378 if ((collectBuffer
.length() > 0)
6379 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6385 // 00-17, 19, 1C-1F, 20-7E --> put
6393 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6396 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6405 // 00-17, 19, 1C-1F, 20-7F --> ignore
6407 // 0x9C goes to GROUND
6414 case SOSPMAPC_STRING
:
6415 // 00-17, 19, 1C-1F, 20-7F --> ignore
6417 // 0x9C goes to GROUND
6425 // Special case for Xterm: OSC can pass control characters
6426 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
6430 // 00-17, 19, 1C-1F --> ignore
6432 // 20-7F --> osc_put
6433 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6437 // 0x9C goes to GROUND
6444 case VT52_DIRECT_CURSOR_ADDRESS
:
6445 // This is a special case for the VT52 sequence "ESC Y l c"
6446 if (collectBuffer
.length() == 0) {
6448 } else if (collectBuffer
.length() == 1) {
6449 // We've got the two characters, one in the buffer and the
6451 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
6460 * Expose current cursor X to outside world.
6462 * @return current cursor X
6464 public final int getCursorX() {
6465 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6466 return currentState
.cursorX
* 2;
6468 return currentState
.cursorX
;
6472 * Expose current cursor Y to outside world.
6474 * @return current cursor Y
6476 public final int getCursorY() {
6477 return currentState
.cursorY
;