2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.tterminal
;
31 import java
.io
.BufferedOutputStream
;
32 import java
.io
.CharArrayWriter
;
33 import java
.io
.InputStream
;
34 import java
.io
.InputStreamReader
;
35 import java
.io
.IOException
;
36 import java
.io
.OutputStream
;
37 import java
.io
.OutputStreamWriter
;
38 import java
.io
.PrintWriter
;
39 import java
.io
.Reader
;
40 import java
.io
.UnsupportedEncodingException
;
41 import java
.io
.Writer
;
42 import java
.util
.ArrayList
;
43 import java
.util
.Collections
;
44 import java
.util
.List
;
46 import jexer
.TKeypress
;
47 import jexer
.event
.TMouseEvent
;
48 import jexer
.bits
.Color
;
49 import jexer
.bits
.Cell
;
50 import jexer
.bits
.CellAttributes
;
51 import jexer
.io
.ReadTimeoutException
;
52 import jexer
.io
.TimeoutInputStream
;
53 import static jexer
.TKeypress
.*;
56 * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
57 * including a scrollback buffer.
60 * It currently implements VT100, VT102, VT220, and XTERM with the following
64 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
65 * failure with XTERM. This is due to vttest failing to decode the UTF-8
69 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
70 * from VT100 are not supported.
73 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
74 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
75 * because DECSCL is not supported, it will fail the last part of the
76 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
79 * - Numeric/application keys from the number pad are not supported because
80 * they are not exposed from the TKeypress API.
83 * - VT52 HOLD SCREEN mode is not supported.
86 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
87 * numerators) are not rendered correctly.
90 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
92 public class ECMA48
implements Runnable
{
94 // ------------------------------------------------------------------------
95 // Constants --------------------------------------------------------------
96 // ------------------------------------------------------------------------
99 * The emulator can emulate several kinds of terminals.
101 public enum DeviceType
{
103 * DEC VT100 but also including the three VT102 functions.
124 * Parser character scan states.
126 private enum ScanState
{
141 VT52_DIRECT_CURSOR_ADDRESS
145 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
146 * can't really use it in keypress() because we do not see number pad
147 * events from TKeypress.
149 private enum KeypadMode
{
155 * Arrow keys can emit three different sequences (DECCKM or VT52
158 private enum ArrowKeyMode
{
165 * Available character sets for GL, GR, G0, G1, G2, G3.
167 private enum CharacterSet
{
188 * Single-shift states used by the C1 control characters SS2 (0x8E) and
191 private enum Singleshift
{
198 * VT220+ lockshift states.
200 private enum LockshiftMode
{
210 * XTERM mouse reporting protocols.
212 private enum MouseProtocol
{
221 * XTERM mouse reporting encodings.
223 private enum MouseEncoding
{
229 // ------------------------------------------------------------------------
230 // Variables --------------------------------------------------------------
231 // ------------------------------------------------------------------------
234 * The enclosing listening object.
236 private DisplayListener displayListener
;
239 * When true, the reader thread is expected to exit.
241 private volatile boolean stopReaderThread
= false;
246 private Thread readerThread
= null;
249 * The type of emulator to be.
251 private DeviceType type
= DeviceType
.VT102
;
254 * The scrollback buffer characters + attributes.
256 private volatile ArrayList
<DisplayLine
> scrollback
;
259 * The raw display buffer characters + attributes.
261 private volatile ArrayList
<DisplayLine
> display
;
264 * The maximum number of lines in the scrollback buffer.
266 private int maxScrollback
= 10000;
269 * The terminal's input. For type == XTERM, this is an InputStreamReader
270 * with UTF-8 encoding.
272 private Reader input
;
275 * The terminal's raw InputStream. This is used for type != XTERM.
277 private volatile TimeoutInputStream inputStream
;
280 * The terminal's output. For type == XTERM, this wraps an
281 * OutputStreamWriter with UTF-8 encoding.
283 private Writer output
;
286 * The terminal's raw OutputStream. This is used for type != XTERM.
288 private OutputStream outputStream
;
291 * Current scanning state.
293 private ScanState scanState
;
296 * Which mouse protocol is active.
298 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
301 * Which mouse encoding is active.
303 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
306 * A terminal may request that the mouse pointer be hidden using a
307 * Privacy Message containing either "hideMousePointer" or
308 * "showMousePointer". This is currently only used within Jexer by
309 * TTerminalWindow so that only the bottom-most instance of nested
310 * Jexer's draws the mouse within its application window.
312 private boolean hideMousePointer
= false;
315 * Physical display width. We start at 80x24, but the user can resize us
321 * Physical display height. We start at 80x24, but the user can resize
327 * Top margin of the scrolling region.
329 private int scrollRegionTop
;
332 * Bottom margin of the scrolling region.
334 private int scrollRegionBottom
;
337 * Right margin column number. This can be selected by the remote side
338 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
340 private int rightMargin
;
343 * Last character printed.
348 * VT100-style line wrapping: a character is placed in column 80 (or
349 * 132), but the line does NOT wrap until another character is written to
350 * column 1 of the next line, after which the cursor moves to column 2.
352 private boolean wrapLineFlag
;
355 * VT220 single shift flag.
357 private Singleshift singleshift
= Singleshift
.NONE
;
360 * true = insert characters, false = overwrite.
362 private boolean insertMode
= false;
365 * VT52 mode as selected by DECANM. True means VT52, false means
366 * ANSI. Default is ANSI.
368 private boolean vt52Mode
= false;
371 * Visible cursor (DECTCEM).
373 private boolean cursorVisible
= true;
376 * Screen title as set by the xterm OSC sequence. Lots of applications
377 * send a screenTitle regardless of whether it is an xterm client or not.
379 private String screenTitle
= "";
382 * Parameter characters being collected.
384 private List
<Integer
> csiParams
;
387 * Non-csi collect buffer.
389 private StringBuilder collectBuffer
;
392 * When true, use the G1 character set.
394 private boolean shiftOut
= false;
397 * Horizontal tab stop locations.
399 private List
<Integer
> tabStops
;
402 * S8C1T. True means 8bit controls, false means 7bit controls.
404 private boolean s8c1t
= false;
407 * Printer mode. True means send all output to printer, which discards
410 private boolean printerControllerMode
= false;
413 * LMN line mode. If true, linefeed() puts the cursor on the first
414 * column of the next line. If false, linefeed() puts the cursor one
415 * line down on the current line. The default is false.
417 private boolean newLineMode
= false;
420 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
422 private ArrowKeyMode arrowKeyMode
;
425 * Whether number pad keys send VT100 or VT52, application or numeric
428 @SuppressWarnings("unused")
429 private KeypadMode keypadMode
;
432 * When true, the terminal is in 132-column mode (DECCOLM).
434 private boolean columns132
= false;
437 * true = reverse video. Set by DECSCNM.
439 private boolean reverseVideo
= false;
442 * false = echo characters locally.
444 private boolean fullDuplex
= true;
447 * The current terminal state.
449 private SaveableState currentState
;
452 * The last saved terminal state.
454 private SaveableState savedState
;
457 * The 88- or 256-color support RGB colors.
459 private List
<Integer
> colors88
;
462 * DECSC/DECRC save/restore a subset of the total state. This class
463 * encapsulates those specific flags/modes.
465 private class SaveableState
{
468 * When true, cursor positions are relative to the scrolling region.
470 public boolean originMode
= false;
473 * The current editing X position.
475 public int cursorX
= 0;
478 * The current editing Y position.
480 public int cursorY
= 0;
483 * Which character set is currently selected in G0.
485 public CharacterSet g0Charset
= CharacterSet
.US
;
488 * Which character set is currently selected in G1.
490 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
493 * Which character set is currently selected in G2.
495 public CharacterSet g2Charset
= CharacterSet
.US
;
498 * Which character set is currently selected in G3.
500 public CharacterSet g3Charset
= CharacterSet
.US
;
503 * Which character set is currently selected in GR.
505 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
508 * The current drawing attributes.
510 public CellAttributes attr
;
515 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
520 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
525 public boolean lineWrap
= true;
530 public void reset() {
534 g0Charset
= CharacterSet
.US
;
535 g1Charset
= CharacterSet
.DRAWING
;
536 g2Charset
= CharacterSet
.US
;
537 g3Charset
= CharacterSet
.US
;
538 grCharset
= CharacterSet
.DRAWING
;
539 attr
= new CellAttributes();
540 glLockshift
= LockshiftMode
.NONE
;
541 grLockshift
= LockshiftMode
.NONE
;
546 * Copy attributes from another instance.
548 * @param that the other instance to match
550 public void setTo(final SaveableState that
) {
551 this.originMode
= that
.originMode
;
552 this.cursorX
= that
.cursorX
;
553 this.cursorY
= that
.cursorY
;
554 this.g0Charset
= that
.g0Charset
;
555 this.g1Charset
= that
.g1Charset
;
556 this.g2Charset
= that
.g2Charset
;
557 this.g3Charset
= that
.g3Charset
;
558 this.grCharset
= that
.grCharset
;
559 this.attr
= new CellAttributes();
560 this.attr
.setTo(that
.attr
);
561 this.glLockshift
= that
.glLockshift
;
562 this.grLockshift
= that
.grLockshift
;
563 this.lineWrap
= that
.lineWrap
;
567 * Public constructor.
569 public SaveableState() {
574 // ------------------------------------------------------------------------
575 // Constructors -----------------------------------------------------------
576 // ------------------------------------------------------------------------
579 * Public constructor.
581 * @param type one of the DeviceType constants to select VT100, VT102,
583 * @param inputStream an InputStream connected to the remote side. For
584 * type == XTERM, inputStream is converted to a Reader with UTF-8
586 * @param outputStream an OutputStream connected to the remote user. For
587 * type == XTERM, outputStream is converted to a Writer with UTF-8
589 * @param displayListener a callback to the outer display, or null for
590 * default VT100 behavior
591 * @throws UnsupportedEncodingException if an exception is thrown when
592 * creating the InputStreamReader
594 public ECMA48(final DeviceType type
, final InputStream inputStream
,
595 final OutputStream outputStream
, final DisplayListener displayListener
)
596 throws UnsupportedEncodingException
{
598 assert (inputStream
!= null);
599 assert (outputStream
!= null);
601 csiParams
= new ArrayList
<Integer
>();
602 tabStops
= new ArrayList
<Integer
>();
603 scrollback
= new ArrayList
<DisplayLine
>();
604 display
= new ArrayList
<DisplayLine
>();
607 if (inputStream
instanceof TimeoutInputStream
) {
608 this.inputStream
= (TimeoutInputStream
)inputStream
;
610 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
612 if (type
== DeviceType
.XTERM
) {
613 this.input
= new InputStreamReader(this.inputStream
, "UTF-8");
614 this.output
= new OutputStreamWriter(new
615 BufferedOutputStream(outputStream
), "UTF-8");
616 this.outputStream
= null;
619 this.outputStream
= new BufferedOutputStream(outputStream
);
621 this.displayListener
= displayListener
;
624 for (int i
= 0; i
< height
; i
++) {
625 display
.add(new DisplayLine(currentState
.attr
));
628 // Spin up the input reader
629 readerThread
= new Thread(this);
630 readerThread
.start();
633 // ------------------------------------------------------------------------
634 // Runnable ---------------------------------------------------------------
635 // ------------------------------------------------------------------------
638 * Read function runs on a separate thread.
640 public final void run() {
641 boolean utf8
= false;
642 boolean done
= false;
644 if (type
== DeviceType
.XTERM
) {
648 // available() will often return > 1, so we need to read in chunks to
650 char [] readBufferUTF8
= null;
651 byte [] readBuffer
= null;
653 readBufferUTF8
= new char[128];
655 readBuffer
= new byte[128];
658 while (!done
&& !stopReaderThread
) {
660 int n
= inputStream
.available();
662 // System.err.printf("available() %d\n", n); System.err.flush();
664 if (readBufferUTF8
.length
< n
) {
665 // The buffer wasn't big enough, make it huger
666 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
669 readBufferUTF8
= new char[newSizeHalf
* 2];
672 if (readBuffer
.length
< n
) {
673 // The buffer wasn't big enough, make it huger
674 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
675 readBuffer
= new byte[newSizeHalf
* 2];
681 } catch (InterruptedException e
) {
690 rc
= input
.read(readBufferUTF8
, 0,
691 readBufferUTF8
.length
);
693 rc
= inputStream
.read(readBuffer
, 0,
696 } catch (ReadTimeoutException e
) {
700 // System.err.printf("read() %d\n", rc); System.err.flush();
705 // Don't step on UI events
706 synchronized (this) {
707 for (int i
= 0; i
< rc
; i
++) {
710 ch
= readBufferUTF8
[i
];
718 // Permit my enclosing UI to know that I updated.
719 if (displayListener
!= null) {
720 displayListener
.displayChanged();
723 // System.err.println("end while loop"); System.err.flush();
724 } catch (IOException e
) {
727 // This is an unusual case. We want to see the stack trace,
728 // but it is related to the spawned process rather than the
729 // actual UI. We will generate the stack trace, and consume
730 // it as though it was emitted by the shell.
731 CharArrayWriter writer
= new CharArrayWriter();
732 // Send a ST and RIS to clear the emulator state.
734 writer
.write("\033\\\033c");
735 writer
.write("\n-----------------------------------\n");
736 e
.printStackTrace(new PrintWriter(writer
));
737 writer
.write("\n-----------------------------------\n");
738 } catch (IOException e2
) {
741 char [] stackTrace
= writer
.toCharArray();
742 for (int i
= 0; i
< stackTrace
.length
; i
++) {
743 if (stackTrace
[i
] == '\n') {
746 consume(stackTrace
[i
]);
750 } // while ((done == false) && (stopReaderThread == false))
752 // Let the rest of the world know that I am done.
753 stopReaderThread
= true;
756 inputStream
.cancelRead();
759 } catch (IOException e
) {
765 } catch (IOException e
) {
769 // Permit my enclosing UI to know that I updated.
770 if (displayListener
!= null) {
771 displayListener
.displayChanged();
774 // System.err.println("*** run() exiting..."); System.err.flush();
777 // ------------------------------------------------------------------------
778 // ECMA48 -----------------------------------------------------------------
779 // ------------------------------------------------------------------------
782 * Return the proper primary Device Attributes string.
784 * @return string to send to remote side that is appropriate for the
787 private String
deviceTypeResponse() {
790 // "I am a VT100 with advanced video option" (often VT102)
799 // "I am a VT220" - 7 bit version
801 return "\033[?62;1;6c";
803 // "I am a VT220" - 8 bit version
804 return "\u009b?62;1;6c";
806 throw new IllegalArgumentException("Invalid device type: " + type
);
811 * Return the proper TERM environment variable for this device type.
813 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
814 * @return "vt100", "xterm", etc.
816 public static String
deviceTypeTerm(final DeviceType deviceType
) {
817 switch (deviceType
) {
831 throw new IllegalArgumentException("Invalid device type: "
837 * Return the proper LANG for this device type. Only XTERM devices know
838 * about UTF-8, the others are defined by their standard to be either
839 * 7-bit or 8-bit characters only.
841 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
842 * @param baseLang a base language without UTF-8 flag such as "C" or
844 * @return "en_US", "en_US.UTF-8", etc.
846 public static String
deviceTypeLang(final DeviceType deviceType
,
847 final String baseLang
) {
849 switch (deviceType
) {
857 return baseLang
+ ".UTF-8";
860 throw new IllegalArgumentException("Invalid device type: "
866 * Write a string directly to the remote side.
868 * @param str string to send
870 public void writeRemote(final String str
) {
871 if (stopReaderThread
) {
872 // Reader hit EOF, bail out now.
877 // System.err.printf("writeRemote() '%s'\n", str);
883 if (outputStream
== null) {
887 outputStream
.flush();
888 for (int i
= 0; i
< str
.length(); i
++) {
889 outputStream
.write(str
.charAt(i
));
891 outputStream
.flush();
892 } catch (IOException e
) {
898 if (output
== null) {
905 } catch (IOException e
) {
911 throw new IllegalArgumentException("Invalid device type: " + type
);
916 * Close the input and output streams and stop the reader thread. Note
917 * that it is safe to call this multiple times.
919 public final void close() {
921 // Tell the reader thread to stop looking at input. It will close
922 // the input streams.
923 if (stopReaderThread
== false) {
924 stopReaderThread
= true;
926 readerThread
.join(1000);
927 } catch (InterruptedException e
) {
932 // Now close the output stream.
937 if (outputStream
!= null) {
939 outputStream
.close();
940 } catch (IOException e
) {
947 if (outputStream
!= null) {
949 outputStream
.close();
950 } catch (IOException e
) {
955 if (output
!= null) {
958 } catch (IOException e
) {
965 throw new IllegalArgumentException("Invalid device type: " +
971 * See if the reader thread is still running.
973 * @return if true, we are still connected to / reading from the remote
976 public final boolean isReading() {
977 return (!stopReaderThread
);
981 * Obtain a new blank display line for an external user
982 * (e.g. TTerminalWindow).
984 * @return new blank line
986 public final DisplayLine
getBlankDisplayLine() {
987 return new DisplayLine(currentState
.attr
);
991 * Get the scrollback buffer.
993 * @return the scrollback buffer
995 public final List
<DisplayLine
> getScrollbackBuffer() {
1000 * Get the display buffer.
1002 * @return the display buffer
1004 public final List
<DisplayLine
> getDisplayBuffer() {
1009 * Get the display width.
1011 * @return the width (usually 80 or 132)
1013 public final int getWidth() {
1018 * Set the display width.
1020 * @param width the new width
1022 public final void setWidth(final int width
) {
1024 rightMargin
= width
- 1;
1025 if (currentState
.cursorX
>= width
) {
1026 currentState
.cursorX
= width
- 1;
1028 if (savedState
.cursorX
>= width
) {
1029 savedState
.cursorX
= width
- 1;
1034 * Get the display height.
1036 * @return the height (usually 24)
1038 public final int getHeight() {
1043 * Set the display height.
1045 * @param height the new height
1047 public final void setHeight(final int height
) {
1048 int delta
= height
- this.height
;
1049 this.height
= height
;
1050 scrollRegionBottom
+= delta
;
1051 if (scrollRegionBottom
< 0) {
1052 scrollRegionBottom
= height
;
1054 if (scrollRegionTop
>= scrollRegionBottom
) {
1055 scrollRegionTop
= 0;
1057 if (currentState
.cursorY
>= height
) {
1058 currentState
.cursorY
= height
- 1;
1060 if (savedState
.cursorY
>= height
) {
1061 savedState
.cursorY
= height
- 1;
1063 while (display
.size() < height
) {
1064 DisplayLine line
= new DisplayLine(currentState
.attr
);
1065 line
.setReverseColor(reverseVideo
);
1068 while (display
.size() > height
) {
1069 scrollback
.add(display
.remove(0));
1074 * Get visible cursor flag.
1076 * @return if true, the cursor is visible
1078 public final boolean isCursorVisible() {
1079 return cursorVisible
;
1083 * Get the screen title as set by the xterm OSC sequence. Lots of
1084 * applications send a screenTitle regardless of whether it is an xterm
1087 * @return screen title
1089 public final String
getScreenTitle() {
1094 * Get 132 columns value.
1096 * @return if true, the terminal is in 132 column mode
1098 public final boolean isColumns132() {
1103 * Clear the CSI parameters and flags.
1105 private void toGround() {
1107 collectBuffer
= new StringBuilder(8);
1108 scanState
= ScanState
.GROUND
;
1112 * Reset the tab stops list.
1114 private void resetTabStops() {
1116 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
1117 tabStops
.add(new Integer(i
* 8));
1122 * Reset the 88- or 256-colors.
1124 private void resetColors() {
1125 colors88
= new ArrayList
<Integer
>(256);
1126 for (int i
= 0; i
< 256; i
++) {
1130 // Set default system colors.
1131 colors88
.set(0, 0x00000000);
1132 colors88
.set(1, 0x00a80000);
1133 colors88
.set(2, 0x0000a800);
1134 colors88
.set(3, 0x00a85400);
1135 colors88
.set(4, 0x000000a8);
1136 colors88
.set(5, 0x00a800a8);
1137 colors88
.set(6, 0x0000a8a8);
1138 colors88
.set(7, 0x00a8a8a8);
1140 colors88
.set(8, 0x00545454);
1141 colors88
.set(9, 0x00fc5454);
1142 colors88
.set(10, 0x0054fc54);
1143 colors88
.set(11, 0x00fcfc54);
1144 colors88
.set(12, 0x005454fc);
1145 colors88
.set(13, 0x00fc54fc);
1146 colors88
.set(14, 0x0054fcfc);
1147 colors88
.set(15, 0x00fcfcfc);
1151 * Get the RGB value of one of the indexed colors.
1153 * @param index the color index
1154 * @return the RGB value
1156 private int get88Color(final int index
) {
1157 // System.err.print("get88Color: " + index);
1158 if ((index
< 0) || (index
> colors88
.size())) {
1159 // System.err.println(" -- UNKNOWN");
1162 // System.err.printf(" %08x\n", colors88.get(index));
1163 return colors88
.get(index
);
1167 * Set one of the indexed colors to a color specification.
1169 * @param index the color index
1170 * @param spec the specification, typically something like "rgb:aa/bb/cc"
1172 private void set88Color(final int index
, final String spec
) {
1173 // System.err.println("set88Color: " + index + " '" + spec + "'");
1175 if ((index
< 0) || (index
> colors88
.size())) {
1178 if (spec
.startsWith("rgb:")) {
1179 String
[] rgbTokens
= spec
.substring(4).split("/");
1180 if (rgbTokens
.length
== 3) {
1182 int rgb
= (Integer
.parseInt(rgbTokens
[0], 16) << 16);
1183 rgb
|= Integer
.parseInt(rgbTokens
[1], 16) << 8;
1184 rgb
|= Integer
.parseInt(rgbTokens
[2], 16);
1185 // System.err.printf(" set to %08x\n", rgb);
1186 colors88
.set(index
, rgb
);
1187 } catch (NumberFormatException e
) {
1194 if (spec
.toLowerCase().equals("black")) {
1195 colors88
.set(index
, 0x00000000);
1196 } else if (spec
.toLowerCase().equals("red")) {
1197 colors88
.set(index
, 0x00a80000);
1198 } else if (spec
.toLowerCase().equals("green")) {
1199 colors88
.set(index
, 0x0000a800);
1200 } else if (spec
.toLowerCase().equals("yellow")) {
1201 colors88
.set(index
, 0x00a85400);
1202 } else if (spec
.toLowerCase().equals("blue")) {
1203 colors88
.set(index
, 0x000000a8);
1204 } else if (spec
.toLowerCase().equals("magenta")) {
1205 colors88
.set(index
, 0x00a800a8);
1206 } else if (spec
.toLowerCase().equals("cyan")) {
1207 colors88
.set(index
, 0x0000a8a8);
1208 } else if (spec
.toLowerCase().equals("white")) {
1209 colors88
.set(index
, 0x00a8a8a8);
1215 * Reset the emulation state.
1217 private void reset() {
1219 currentState
= new SaveableState();
1220 savedState
= new SaveableState();
1221 scanState
= ScanState
.GROUND
;
1224 scrollRegionTop
= 0;
1225 scrollRegionBottom
= height
- 1;
1226 rightMargin
= width
- 1;
1227 newLineMode
= false;
1228 arrowKeyMode
= ArrowKeyMode
.ANSI
;
1229 keypadMode
= KeypadMode
.Numeric
;
1230 wrapLineFlag
= false;
1231 if (displayListener
!= null) {
1232 width
= displayListener
.getDisplayWidth();
1233 height
= displayListener
.getDisplayHeight();
1234 rightMargin
= width
- 1;
1242 newLineMode
= false;
1243 reverseVideo
= false;
1245 cursorVisible
= true;
1248 singleshift
= Singleshift
.NONE
;
1250 printerControllerMode
= false;
1253 mouseProtocol
= MouseProtocol
.OFF
;
1254 mouseEncoding
= MouseEncoding
.X10
;
1259 // Reset extra colors
1267 * Append a new line to the bottom of the display, adding lines off the
1268 * top to the scrollback buffer.
1270 private void newDisplayLine() {
1271 // Scroll the top line off into the scrollback buffer
1272 scrollback
.add(display
.get(0));
1273 if (scrollback
.size() > maxScrollback
) {
1274 scrollback
.remove(0);
1275 scrollback
.trimToSize();
1278 display
.trimToSize();
1279 DisplayLine line
= new DisplayLine(currentState
.attr
);
1280 line
.setReverseColor(reverseVideo
);
1285 * Wraps the current line.
1287 private void wrapCurrentLine() {
1288 if (currentState
.cursorY
== height
- 1) {
1291 if (currentState
.cursorY
< height
- 1) {
1292 currentState
.cursorY
++;
1294 currentState
.cursorX
= 0;
1298 * Handle a carriage return.
1300 private void carriageReturn() {
1301 currentState
.cursorX
= 0;
1302 wrapLineFlag
= false;
1306 * Reverse the color of the visible display.
1308 private void invertDisplayColors() {
1309 for (DisplayLine line
: display
) {
1310 line
.setReverseColor(!line
.isReverseColor());
1315 * Handle a linefeed.
1317 private void linefeed() {
1319 if (currentState
.cursorY
< scrollRegionBottom
) {
1320 // Increment screen y
1321 currentState
.cursorY
++;
1324 // Screen y does not increment
1327 * Two cases: either we're inside a scrolling region or not. If
1328 * the scrolling region bottom is the bottom of the screen, then
1329 * push the top line into the buffer. Else scroll the scrolling
1332 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1334 // We're at the bottom of the scroll region, AND the scroll
1335 // region is the entire screen.
1341 // We're at the bottom of the scroll region, AND the scroll
1342 // region is NOT the entire screen.
1343 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1348 currentState
.cursorX
= 0;
1350 wrapLineFlag
= false;
1354 * Prints one character to the display buffer.
1356 * @param ch character to display
1358 private void printCharacter(final char ch
) {
1359 int rightMargin
= this.rightMargin
;
1361 // Check if we have double-width, and if so chop at 40/66 instead of
1363 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1364 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1367 // Check the unusually-complicated line wrapping conditions...
1368 if (currentState
.cursorX
== rightMargin
) {
1370 if (currentState
.lineWrap
== true) {
1372 * This case happens when: the cursor was already on the
1373 * right margin (either through printing or by an explicit
1374 * placement command), and a character was printed.
1376 * The line wraps only when a new character arrives AND the
1377 * cursor is already on the right margin AND has placed a
1378 * character in its cell. Easier to see than to explain.
1380 if (wrapLineFlag
== false) {
1382 * This block marks the case that we are in the margin
1383 * and the first character has been received and printed.
1385 wrapLineFlag
= true;
1388 * This block marks the case that we are in the margin
1389 * and the second character has been received and
1392 wrapLineFlag
= false;
1396 } else if (currentState
.cursorX
<= rightMargin
) {
1398 * This is the normal case: a character came in and was printed
1399 * to the left of the right margin column.
1402 // Turn off VT100 special-case flag
1403 wrapLineFlag
= false;
1406 // "Print" the character
1407 Cell newCell
= new Cell(ch
);
1408 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1409 newCellAttributes
.setTo(currentState
.attr
);
1410 DisplayLine line
= display
.get(currentState
.cursorY
);
1411 // Insert mode special case
1412 if (insertMode
== true) {
1413 line
.insert(currentState
.cursorX
, newCell
);
1415 // Replace an existing character
1416 line
.replace(currentState
.cursorX
, newCell
);
1419 // Increment horizontal
1420 if (wrapLineFlag
== false) {
1421 currentState
.cursorX
++;
1422 if (currentState
.cursorX
> rightMargin
) {
1423 currentState
.cursorX
--;
1429 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1430 * send to the remote side.
1432 * @param mouse mouse event received from the local user
1434 public void mouse(final TMouseEvent mouse
) {
1437 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1438 mouseProtocol, mouseEncoding, mouse);
1441 if (mouseEncoding
== MouseEncoding
.X10
) {
1442 // We will support X10 but only for (160,94) and smaller.
1443 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1448 switch (mouseProtocol
) {
1455 // Only report button presses
1456 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1462 // Only report button presses and releases
1463 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1464 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1472 * Only report button presses, button releases, and motions that
1473 * have a button down (i.e. drag-and-drop).
1475 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1476 if (!mouse
.isMouse1()
1477 && !mouse
.isMouse2()
1478 && !mouse
.isMouse3()
1479 && !mouse
.isMouseWheelUp()
1480 && !mouse
.isMouseWheelDown()
1488 // Report everything
1492 // Now encode the event
1493 StringBuilder sb
= new StringBuilder(6);
1494 if (mouseEncoding
== MouseEncoding
.SGR
) {
1495 sb
.append((char) 0x1B);
1498 if (mouse
.isMouse1()) {
1499 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1504 } else if (mouse
.isMouse2()) {
1505 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1510 } else if (mouse
.isMouse3()) {
1511 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1516 } else if (mouse
.isMouseWheelUp()) {
1518 } else if (mouse
.isMouseWheelDown()) {
1521 // This is motion with no buttons down.
1525 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1528 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1535 // X10 and UTF8 encodings
1536 sb
.append((char) 0x1B);
1539 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1540 sb
.append((char) (0x03 + 32));
1541 } else if (mouse
.isMouse1()) {
1542 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1543 sb
.append((char) (0x00 + 32 + 32));
1545 sb
.append((char) (0x00 + 32));
1547 } else if (mouse
.isMouse2()) {
1548 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1549 sb
.append((char) (0x01 + 32 + 32));
1551 sb
.append((char) (0x01 + 32));
1553 } else if (mouse
.isMouse3()) {
1554 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1555 sb
.append((char) (0x02 + 32 + 32));
1557 sb
.append((char) (0x02 + 32));
1559 } else if (mouse
.isMouseWheelUp()) {
1560 sb
.append((char) (0x04 + 64));
1561 } else if (mouse
.isMouseWheelDown()) {
1562 sb
.append((char) (0x05 + 64));
1564 // This is motion with no buttons down.
1565 sb
.append((char) (0x03 + 32));
1568 sb
.append((char) (mouse
.getX() + 33));
1569 sb
.append((char) (mouse
.getY() + 33));
1572 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1573 writeRemote(sb
.toString());
1577 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1578 * send to the remote side.
1580 * @param keypress keypress received from the local user
1582 public void keypress(final TKeypress keypress
) {
1583 writeRemote(keypressToString(keypress
));
1587 * Build one of the complex xterm keystroke sequences, storing the result in
1588 * xterm_keystroke_buffer.
1590 * @param ss3 the prefix to use based on VT100 state.
1591 * @param first the first character, usually a number.
1592 * @param first the last character, one of the following: ~ A B C D F H
1593 * @param ctrl whether or not ctrl is down
1594 * @param alt whether or not alt is down
1595 * @param shift whether or not shift is down
1596 * @return the buffer with the full key sequence
1598 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1599 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1601 StringBuilder sb
= new StringBuilder(ss3
);
1602 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1606 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1608 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1610 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1612 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1614 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1616 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1618 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1623 return sb
.toString();
1627 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1629 * @param keypress keypress received from the local user
1630 * @return string to transmit to the remote side
1632 private String
keypressToString(final TKeypress keypress
) {
1634 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1636 * If this is a control character, process it like it came from
1639 if (keypress
.getChar() < 0x20) {
1640 handleControlChar(keypress
.getChar());
1642 // Local echo for everything else
1643 printCharacter(keypress
.getChar());
1647 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1652 // Handle control characters
1653 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1654 StringBuilder sb
= new StringBuilder();
1655 char ch
= keypress
.getChar();
1658 return sb
.toString();
1661 // Handle alt characters
1662 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1663 StringBuilder sb
= new StringBuilder("\033");
1664 char ch
= keypress
.getChar();
1666 return sb
.toString();
1669 if (keypress
.equals(kbBackspaceDel
)) {
1682 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1685 switch (arrowKeyMode
) {
1687 return xtermBuildKeySequence("\033[", '1', 'D',
1688 keypress
.isCtrl(), keypress
.isAlt(),
1689 keypress
.isShift());
1691 return xtermBuildKeySequence("\033", '1', 'D',
1692 keypress
.isCtrl(), keypress
.isAlt(),
1693 keypress
.isShift());
1695 return xtermBuildKeySequence("\033O", '1', 'D',
1696 keypress
.isCtrl(), keypress
.isAlt(),
1697 keypress
.isShift());
1700 switch (arrowKeyMode
) {
1711 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1714 switch (arrowKeyMode
) {
1716 return xtermBuildKeySequence("\033[", '1', 'C',
1717 keypress
.isCtrl(), keypress
.isAlt(),
1718 keypress
.isShift());
1720 return xtermBuildKeySequence("\033", '1', 'C',
1721 keypress
.isCtrl(), keypress
.isAlt(),
1722 keypress
.isShift());
1724 return xtermBuildKeySequence("\033O", '1', 'C',
1725 keypress
.isCtrl(), keypress
.isAlt(),
1726 keypress
.isShift());
1729 switch (arrowKeyMode
) {
1740 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1743 switch (arrowKeyMode
) {
1745 return xtermBuildKeySequence("\033[", '1', 'A',
1746 keypress
.isCtrl(), keypress
.isAlt(),
1747 keypress
.isShift());
1749 return xtermBuildKeySequence("\033", '1', 'A',
1750 keypress
.isCtrl(), keypress
.isAlt(),
1751 keypress
.isShift());
1753 return xtermBuildKeySequence("\033O", '1', 'A',
1754 keypress
.isCtrl(), keypress
.isAlt(),
1755 keypress
.isShift());
1758 switch (arrowKeyMode
) {
1769 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1772 switch (arrowKeyMode
) {
1774 return xtermBuildKeySequence("\033[", '1', 'B',
1775 keypress
.isCtrl(), keypress
.isAlt(),
1776 keypress
.isShift());
1778 return xtermBuildKeySequence("\033", '1', 'B',
1779 keypress
.isCtrl(), keypress
.isAlt(),
1780 keypress
.isShift());
1782 return xtermBuildKeySequence("\033O", '1', 'B',
1783 keypress
.isCtrl(), keypress
.isAlt(),
1784 keypress
.isShift());
1787 switch (arrowKeyMode
) {
1798 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1801 switch (arrowKeyMode
) {
1803 return xtermBuildKeySequence("\033[", '1', 'H',
1804 keypress
.isCtrl(), keypress
.isAlt(),
1805 keypress
.isShift());
1807 return xtermBuildKeySequence("\033", '1', 'H',
1808 keypress
.isCtrl(), keypress
.isAlt(),
1809 keypress
.isShift());
1811 return xtermBuildKeySequence("\033O", '1', 'H',
1812 keypress
.isCtrl(), keypress
.isAlt(),
1813 keypress
.isShift());
1816 switch (arrowKeyMode
) {
1827 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1830 switch (arrowKeyMode
) {
1832 return xtermBuildKeySequence("\033[", '1', 'F',
1833 keypress
.isCtrl(), keypress
.isAlt(),
1834 keypress
.isShift());
1836 return xtermBuildKeySequence("\033", '1', 'F',
1837 keypress
.isCtrl(), keypress
.isAlt(),
1838 keypress
.isShift());
1840 return xtermBuildKeySequence("\033O", '1', 'F',
1841 keypress
.isCtrl(), keypress
.isAlt(),
1842 keypress
.isShift());
1845 switch (arrowKeyMode
) {
1856 if (keypress
.equals(kbF1
)) {
1864 if (keypress
.equals(kbF2
)) {
1872 if (keypress
.equals(kbF3
)) {
1880 if (keypress
.equals(kbF4
)) {
1888 if (keypress
.equals(kbF5
)) {
1901 if (keypress
.equals(kbF6
)) {
1914 if (keypress
.equals(kbF7
)) {
1927 if (keypress
.equals(kbF8
)) {
1940 if (keypress
.equals(kbF9
)) {
1953 if (keypress
.equals(kbF10
)) {
1966 if (keypress
.equals(kbF11
)) {
1970 if (keypress
.equals(kbF12
)) {
1974 if (keypress
.equals(kbShiftF1
)) {
1979 if (type
== DeviceType
.XTERM
) {
1985 if (keypress
.equals(kbShiftF2
)) {
1990 if (type
== DeviceType
.XTERM
) {
1996 if (keypress
.equals(kbShiftF3
)) {
2001 if (type
== DeviceType
.XTERM
) {
2007 if (keypress
.equals(kbShiftF4
)) {
2012 if (type
== DeviceType
.XTERM
) {
2018 if (keypress
.equals(kbShiftF5
)) {
2020 return "\033[15;2~";
2023 if (keypress
.equals(kbShiftF6
)) {
2025 return "\033[17;2~";
2028 if (keypress
.equals(kbShiftF7
)) {
2030 return "\033[18;2~";
2033 if (keypress
.equals(kbShiftF8
)) {
2035 return "\033[19;2~";
2038 if (keypress
.equals(kbShiftF9
)) {
2040 return "\033[20;2~";
2043 if (keypress
.equals(kbShiftF10
)) {
2045 return "\033[21;2~";
2048 if (keypress
.equals(kbShiftF11
)) {
2050 return "\033[23;2~";
2053 if (keypress
.equals(kbShiftF12
)) {
2055 return "\033[24;2~";
2058 if (keypress
.equals(kbCtrlF1
)) {
2063 if (type
== DeviceType
.XTERM
) {
2069 if (keypress
.equals(kbCtrlF2
)) {
2074 if (type
== DeviceType
.XTERM
) {
2080 if (keypress
.equals(kbCtrlF3
)) {
2085 if (type
== DeviceType
.XTERM
) {
2091 if (keypress
.equals(kbCtrlF4
)) {
2096 if (type
== DeviceType
.XTERM
) {
2102 if (keypress
.equals(kbCtrlF5
)) {
2104 return "\033[15;5~";
2107 if (keypress
.equals(kbCtrlF6
)) {
2109 return "\033[17;5~";
2112 if (keypress
.equals(kbCtrlF7
)) {
2114 return "\033[18;5~";
2117 if (keypress
.equals(kbCtrlF8
)) {
2119 return "\033[19;5~";
2122 if (keypress
.equals(kbCtrlF9
)) {
2124 return "\033[20;5~";
2127 if (keypress
.equals(kbCtrlF10
)) {
2129 return "\033[21;5~";
2132 if (keypress
.equals(kbCtrlF11
)) {
2134 return "\033[23;5~";
2137 if (keypress
.equals(kbCtrlF12
)) {
2139 return "\033[24;5~";
2142 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2145 return xtermBuildKeySequence("\033[", '5', '~',
2146 keypress
.isCtrl(), keypress
.isAlt(),
2147 keypress
.isShift());
2153 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2156 return xtermBuildKeySequence("\033[", '6', '~',
2157 keypress
.isCtrl(), keypress
.isAlt(),
2158 keypress
.isShift());
2164 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2167 return xtermBuildKeySequence("\033[", '2', '~',
2168 keypress
.isCtrl(), keypress
.isAlt(),
2169 keypress
.isShift());
2175 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2178 return xtermBuildKeySequence("\033[", '3', '~',
2179 keypress
.isCtrl(), keypress
.isAlt(),
2180 keypress
.isShift());
2182 // Delete sends real delete for VTxxx
2187 if (keypress
.equals(kbEnter
)) {
2191 if (keypress
.equals(kbEsc
)) {
2195 if (keypress
.equals(kbAltEsc
)) {
2199 if (keypress
.equals(kbTab
)) {
2203 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2204 (keypress
.equals(kbShiftTab
))
2214 // Non-alt, non-ctrl characters
2215 if (!keypress
.isFnKey()) {
2216 StringBuilder sb
= new StringBuilder();
2217 sb
.append(keypress
.getChar());
2218 return sb
.toString();
2224 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2227 * @param ch 8-bit character from the remote side
2228 * @param charsetGl character set defined for GL
2229 * @param charsetGr character set defined for GR
2230 * @return character to display on the screen
2232 private char mapCharacterCharset(final char ch
,
2233 final CharacterSet charsetGl
,
2234 final CharacterSet charsetGr
) {
2236 int lookupChar
= ch
;
2237 CharacterSet lookupCharset
= charsetGl
;
2240 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2241 lookupCharset
= charsetGr
;
2245 switch (lookupCharset
) {
2248 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2251 return DECCharacterSets
.UK
[lookupChar
];
2254 return DECCharacterSets
.US_ASCII
[lookupChar
];
2257 return DECCharacterSets
.NL
[lookupChar
];
2260 return DECCharacterSets
.FI
[lookupChar
];
2263 return DECCharacterSets
.FR
[lookupChar
];
2266 return DECCharacterSets
.FR_CA
[lookupChar
];
2269 return DECCharacterSets
.DE
[lookupChar
];
2272 return DECCharacterSets
.IT
[lookupChar
];
2275 return DECCharacterSets
.NO
[lookupChar
];
2278 return DECCharacterSets
.ES
[lookupChar
];
2281 return DECCharacterSets
.SV
[lookupChar
];
2284 return DECCharacterSets
.SWISS
[lookupChar
];
2286 case DEC_SUPPLEMENTAL
:
2287 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2290 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2293 return DECCharacterSets
.US_ASCII
[lookupChar
];
2296 return DECCharacterSets
.US_ASCII
[lookupChar
];
2299 throw new IllegalArgumentException("Invalid character set value: "
2305 * Map an 8-bit byte into a printable character.
2307 * @param ch either 8-bit or Unicode character from the remote side
2308 * @return character to display on the screen
2310 private char mapCharacter(final char ch
) {
2312 // Unicode character, just return it
2316 CharacterSet charsetGl
= currentState
.g0Charset
;
2317 CharacterSet charsetGr
= currentState
.grCharset
;
2319 if (vt52Mode
== true) {
2320 if (shiftOut
== true) {
2321 // Shifted out character, pull from VT52 graphics
2322 charsetGl
= currentState
.g1Charset
;
2323 charsetGr
= CharacterSet
.US
;
2326 charsetGl
= currentState
.g0Charset
;
2327 charsetGr
= CharacterSet
.US
;
2330 // Pull the character
2331 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2335 if (shiftOut
== true) {
2336 // Shifted out character, pull from G1
2337 charsetGl
= currentState
.g1Charset
;
2338 charsetGr
= currentState
.grCharset
;
2340 // Pull the character
2341 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2345 if (singleshift
== Singleshift
.SS2
) {
2347 singleshift
= Singleshift
.NONE
;
2349 // Shifted out character, pull from G2
2350 charsetGl
= currentState
.g2Charset
;
2351 charsetGr
= currentState
.grCharset
;
2355 if (singleshift
== Singleshift
.SS3
) {
2357 singleshift
= Singleshift
.NONE
;
2359 // Shifted out character, pull from G3
2360 charsetGl
= currentState
.g3Charset
;
2361 charsetGr
= currentState
.grCharset
;
2364 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2365 // Check for locking shift
2367 switch (currentState
.glLockshift
) {
2380 charsetGl
= currentState
.g2Charset
;
2385 charsetGl
= currentState
.g3Charset
;
2390 charsetGl
= currentState
.g0Charset
;
2394 switch (currentState
.grLockshift
) {
2404 charsetGr
= currentState
.g1Charset
;
2409 charsetGr
= currentState
.g2Charset
;
2414 charsetGr
= currentState
.g3Charset
;
2419 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2426 // Pull the character
2427 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2431 * Scroll the text within a scrolling region up 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 scrollingRegionScrollUp(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(regionBottom
+ 1
2458 - remaining
, regionBottom
+ 1);
2459 display
= new ArrayList
<DisplayLine
>(displayTop
);
2460 display
.addAll(displayMiddle
);
2461 for (int i
= 0; i
< n
; i
++) {
2462 DisplayLine line
= new DisplayLine(currentState
.attr
);
2463 line
.setReverseColor(reverseVideo
);
2466 display
.addAll(displayBottom
);
2468 assert (display
.size() == height
);
2472 * Scroll the text within a scrolling region down n lines.
2474 * @param regionTop top row of the scrolling region
2475 * @param regionBottom bottom row of the scrolling region
2476 * @param n number of lines to scroll
2478 private void scrollingRegionScrollDown(final int regionTop
,
2479 final int regionBottom
, final int n
) {
2481 if (regionTop
>= regionBottom
) {
2485 // Sanity check: see if there will be any characters left after the
2487 if (regionBottom
+ 1 - regionTop
<= n
) {
2488 // There won't be anything left in the region, so just call
2489 // eraseScreen() and return.
2490 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2494 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2495 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2496 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2498 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2499 regionTop
+ remaining
);
2500 display
= new ArrayList
<DisplayLine
>(displayTop
);
2501 for (int i
= 0; i
< n
; i
++) {
2502 DisplayLine line
= new DisplayLine(currentState
.attr
);
2503 line
.setReverseColor(reverseVideo
);
2506 display
.addAll(displayMiddle
);
2507 display
.addAll(displayBottom
);
2509 assert (display
.size() == height
);
2513 * Process a control character.
2515 * @param ch 8-bit character from the remote side
2517 private void handleControlChar(final char ch
) {
2518 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2529 // Transmit the answerback message.
2540 cursorLeft(1, false);
2545 advanceToNextTabStop();
2571 currentState
.glLockshift
= LockshiftMode
.NONE
;
2577 currentState
.glLockshift
= LockshiftMode
.NONE
;
2602 singleshift
= Singleshift
.SS2
;
2607 singleshift
= Singleshift
.SS3
;
2617 * Advance the cursor to the next tab stop.
2619 private void advanceToNextTabStop() {
2620 if (tabStops
.size() == 0) {
2621 // Go to the rightmost column
2622 cursorRight(rightMargin
- currentState
.cursorX
, false);
2625 for (Integer stop
: tabStops
) {
2626 if (stop
> currentState
.cursorX
) {
2627 cursorRight(stop
- currentState
.cursorX
, false);
2632 * We got here, meaning there isn't a tab stop beyond the current
2633 * cursor position. Place the cursor of the right-most edge of the
2636 cursorRight(rightMargin
- currentState
.cursorX
, false);
2640 * Save a character into the collect buffer.
2642 * @param ch character to save
2644 private void collect(final char ch
) {
2645 collectBuffer
.append(ch
);
2649 * Save a byte into the CSI parameters buffer.
2651 * @param ch byte to save
2653 private void param(final byte ch
) {
2654 if (csiParams
.size() == 0) {
2655 csiParams
.add(new Integer(0));
2657 Integer x
= csiParams
.get(csiParams
.size() - 1);
2658 if ((ch
>= '0') && (ch
<= '9')) {
2661 csiParams
.set(csiParams
.size() - 1, x
);
2665 csiParams
.add(new Integer(0));
2670 * Get a CSI parameter value, with a default.
2672 * @param position parameter index. 0 is the first parameter.
2673 * @param defaultValue value to use if csiParams[position] doesn't exist
2674 * @return parameter value
2676 private int getCsiParam(final int position
, final int defaultValue
) {
2677 if (csiParams
.size() < position
+ 1) {
2678 return defaultValue
;
2680 return csiParams
.get(position
).intValue();
2684 * Get a CSI parameter value, clamped to within min/max.
2686 * @param position parameter index. 0 is the first parameter.
2687 * @param defaultValue value to use if csiParams[position] doesn't exist
2688 * @param minValue minimum value inclusive
2689 * @param maxValue maximum value inclusive
2690 * @return parameter value
2692 private int getCsiParam(final int position
, final int defaultValue
,
2693 final int minValue
, final int maxValue
) {
2695 assert (minValue
<= maxValue
);
2696 int value
= getCsiParam(position
, defaultValue
);
2697 if (value
< minValue
) {
2700 if (value
> maxValue
) {
2707 * Set or unset a toggle.
2709 * @param value true for set ('h'), false for reset ('l')
2711 private void setToggle(final boolean value
) {
2712 boolean decPrivateModeFlag
= false;
2713 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2714 if (collectBuffer
.charAt(i
) == '?') {
2715 decPrivateModeFlag
= true;
2720 for (Integer i
: csiParams
) {
2725 if (decPrivateModeFlag
== true) {
2727 if (value
== true) {
2728 // Use application arrow keys
2729 arrowKeyMode
= ArrowKeyMode
.VT100
;
2731 // Use ANSI arrow keys
2732 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2737 if (decPrivateModeFlag
== true) {
2738 if (value
== false) {
2742 arrowKeyMode
= ArrowKeyMode
.VT52
;
2745 * From the VT102 docs: "You use ANSI mode to select
2746 * most terminal features; the terminal uses the same
2747 * features when it switches to VT52 mode. You
2748 * cannot, however, change most of these features in
2751 * In other words, do not reset any other attributes
2752 * when switching between VT52 submode and ANSI.
2754 * HOWEVER, the real vt100 does switch the character
2755 * set according to Usenet.
2757 currentState
.g0Charset
= CharacterSet
.US
;
2758 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2761 if ((type
== DeviceType
.VT220
)
2762 || (type
== DeviceType
.XTERM
)) {
2764 // VT52 mode is explicitly 7-bit
2766 singleshift
= Singleshift
.NONE
;
2771 if (value
== true) {
2772 // Turn off keyboard
2781 if (decPrivateModeFlag
== true) {
2783 if (value
== true) {
2790 if ((displayListener
!= null)
2791 && (type
== DeviceType
.XTERM
)
2793 // For xterms, reset to the actual width, not 80
2795 width
= displayListener
.getDisplayWidth();
2796 rightMargin
= width
- 1;
2799 width
= rightMargin
+ 1;
2802 // Entire screen is cleared, and scrolling region is
2804 eraseScreen(0, 0, height
- 1, width
- 1, false);
2805 scrollRegionTop
= 0;
2806 scrollRegionBottom
= height
- 1;
2807 // Also home the cursor
2808 cursorPosition(0, 0);
2812 if (decPrivateModeFlag
== true) {
2814 if (value
== true) {
2823 if (value
== true) {
2831 if (decPrivateModeFlag
== true) {
2833 if (value
== true) {
2835 * Set selects reverse screen, a white screen
2836 * background with black characters.
2838 if (reverseVideo
!= true) {
2840 * If in normal video, switch it back
2842 invertDisplayColors();
2844 reverseVideo
= true;
2847 * Reset selects normal screen, a black screen
2848 * background with white characters.
2850 if (reverseVideo
== true) {
2852 * If in reverse video already, switch it back
2854 invertDisplayColors();
2856 reverseVideo
= false;
2861 if (decPrivateModeFlag
== true) {
2863 if (value
== true) {
2864 // Origin is relative to scroll region cursor.
2865 // Cursor can NEVER leave scrolling region.
2866 currentState
.originMode
= true;
2867 cursorPosition(0, 0);
2869 // Origin is absolute to entire screen. Cursor can
2870 // leave the scrolling region via cup() and hvp().
2871 currentState
.originMode
= false;
2872 cursorPosition(0, 0);
2877 if (decPrivateModeFlag
== true) {
2879 if (value
== true) {
2881 currentState
.lineWrap
= true;
2883 // Turn linewrap off
2884 currentState
.lineWrap
= false;
2889 if (decPrivateModeFlag
== true) {
2891 if (value
== true) {
2892 // Keyboard auto-repeat on
2895 // Keyboard auto-repeat off
2901 if (decPrivateModeFlag
== false) {
2903 if (value
== true) {
2913 if (decPrivateModeFlag
== true) {
2919 if (decPrivateModeFlag
== true) {
2925 if (decPrivateModeFlag
== false) {
2927 if (value
== true) {
2929 * Set causes a received linefeed, form feed, or
2930 * vertical tab to move cursor to first column of
2931 * next line. RETURN transmits both a carriage return
2932 * and linefeed. This selection is also called new
2938 * Reset causes a received linefeed, form feed, or
2939 * vertical tab to move cursor to next line in
2940 * current column. RETURN transmits a carriage
2943 newLineMode
= false;
2949 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2950 if (decPrivateModeFlag
== true) {
2952 if (value
== true) {
2954 cursorVisible
= true;
2957 cursorVisible
= false;
2964 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2965 if (decPrivateModeFlag
== true) {
2967 if (value
== true) {
2968 // Select national mode NRC
2971 // Select multi-national mode
2980 if ((type
== DeviceType
.XTERM
)
2981 && (decPrivateModeFlag
== true)
2983 // Mouse: normal tracking mode
2984 if (value
== true) {
2985 mouseProtocol
= MouseProtocol
.NORMAL
;
2987 mouseProtocol
= MouseProtocol
.OFF
;
2993 if ((type
== DeviceType
.XTERM
)
2994 && (decPrivateModeFlag
== true)
2996 // Mouse: normal tracking mode
2997 if (value
== true) {
2998 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
3000 mouseProtocol
= MouseProtocol
.OFF
;
3006 if ((type
== DeviceType
.XTERM
)
3007 && (decPrivateModeFlag
== true)
3009 // Mouse: Any-event tracking mode
3010 if (value
== true) {
3011 mouseProtocol
= MouseProtocol
.ANYEVENT
;
3013 mouseProtocol
= MouseProtocol
.OFF
;
3019 if ((type
== DeviceType
.XTERM
)
3020 && (decPrivateModeFlag
== true)
3022 // Mouse: UTF-8 coordinates
3023 if (value
== true) {
3024 mouseEncoding
= MouseEncoding
.UTF8
;
3026 mouseEncoding
= MouseEncoding
.X10
;
3032 if ((type
== DeviceType
.XTERM
)
3033 && (decPrivateModeFlag
== true)
3035 // Mouse: SGR coordinates
3036 if (value
== true) {
3037 mouseEncoding
= MouseEncoding
.SGR
;
3039 mouseEncoding
= MouseEncoding
.X10
;
3052 * DECSC - Save cursor.
3054 private void decsc() {
3055 savedState
.setTo(currentState
);
3059 * DECRC - Restore cursor.
3061 private void decrc() {
3062 currentState
.setTo(savedState
);
3068 private void ind() {
3069 // Move the cursor and scroll if necessary. If at the bottom line
3070 // already, a scroll up is supposed to be performed.
3071 if (currentState
.cursorY
== scrollRegionBottom
) {
3072 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3074 cursorDown(1, true);
3078 * RI - Reverse index.
3081 // Move the cursor and scroll if necessary. If at the top line
3082 // already, a scroll down is supposed to be performed.
3083 if (currentState
.cursorY
== scrollRegionTop
) {
3084 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3092 private void nel() {
3093 // Move the cursor and scroll if necessary. If at the bottom line
3094 // already, a scroll up is supposed to be performed.
3095 if (currentState
.cursorY
== scrollRegionBottom
) {
3096 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3098 cursorDown(1, true);
3100 // Reset to the beginning of the next line
3101 currentState
.cursorX
= 0;
3105 * DECKPAM - Keypad application mode.
3107 private void deckpam() {
3108 keypadMode
= KeypadMode
.Application
;
3112 * DECKPNM - Keypad numeric mode.
3114 private void deckpnm() {
3115 keypadMode
= KeypadMode
.Numeric
;
3121 * @param n number of spaces to move
3122 * @param honorScrollRegion if true, then do nothing if the cursor is
3123 * outside the scrolling region
3125 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3129 * Special case: if a user moves the cursor from the right margin, we
3130 * have to reset the VT100 right margin flag.
3133 wrapLineFlag
= false;
3136 for (int i
= 0; i
< n
; i
++) {
3137 if (honorScrollRegion
== true) {
3138 // Honor the scrolling region
3139 if ((currentState
.cursorY
< scrollRegionTop
)
3140 || (currentState
.cursorY
> scrollRegionBottom
)
3142 // Outside region, do nothing
3145 // Inside region, go up
3146 top
= scrollRegionTop
;
3148 // Non-scrolling case
3152 if (currentState
.cursorY
> top
) {
3153 currentState
.cursorY
--;
3159 * Move down n spaces.
3161 * @param n number of spaces to move
3162 * @param honorScrollRegion if true, then do nothing if the cursor is
3163 * outside the scrolling region
3165 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3169 * Special case: if a user moves the cursor from the right margin, we
3170 * have to reset the VT100 right margin flag.
3173 wrapLineFlag
= false;
3176 for (int i
= 0; i
< n
; i
++) {
3178 if (honorScrollRegion
== true) {
3179 // Honor the scrolling region
3180 if (currentState
.cursorY
> scrollRegionBottom
) {
3181 // Outside region, do nothing
3184 // Inside region, go down
3185 bottom
= scrollRegionBottom
;
3187 // Non-scrolling case
3188 bottom
= height
- 1;
3191 if (currentState
.cursorY
< bottom
) {
3192 currentState
.cursorY
++;
3198 * Move left n spaces.
3200 * @param n number of spaces to move
3201 * @param honorScrollRegion if true, then do nothing if the cursor is
3202 * outside the scrolling region
3204 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3206 * Special case: if a user moves the cursor from the right margin, we
3207 * have to reset the VT100 right margin flag.
3210 wrapLineFlag
= false;
3213 for (int i
= 0; i
< n
; i
++) {
3214 if (honorScrollRegion
== true) {
3215 // Honor the scrolling region
3216 if ((currentState
.cursorY
< scrollRegionTop
)
3217 || (currentState
.cursorY
> scrollRegionBottom
)
3219 // Outside region, do nothing
3224 if (currentState
.cursorX
> 0) {
3225 currentState
.cursorX
--;
3231 * Move right n spaces.
3233 * @param n number of spaces to move
3234 * @param honorScrollRegion if true, then do nothing if the cursor is
3235 * outside the scrolling region
3237 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3238 int rightMargin
= this.rightMargin
;
3241 * Special case: if a user moves the cursor from the right margin, we
3242 * have to reset the VT100 right margin flag.
3245 wrapLineFlag
= false;
3248 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3249 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3252 for (int i
= 0; i
< n
; i
++) {
3253 if (honorScrollRegion
== true) {
3254 // Honor the scrolling region
3255 if ((currentState
.cursorY
< scrollRegionTop
)
3256 || (currentState
.cursorY
> scrollRegionBottom
)
3258 // Outside region, do nothing
3263 if (currentState
.cursorX
< rightMargin
) {
3264 currentState
.cursorX
++;
3270 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3272 * @param row row to move to
3273 * @param col column to move to
3275 private void cursorPosition(int row
, final int col
) {
3276 int rightMargin
= this.rightMargin
;
3281 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3282 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3285 // Set column number
3286 currentState
.cursorX
= col
;
3288 // Sanity check, bring column back to margin.
3289 if (currentState
.cursorX
> rightMargin
) {
3290 currentState
.cursorX
= rightMargin
;
3294 if (currentState
.originMode
== true) {
3295 row
+= scrollRegionTop
;
3297 if (currentState
.cursorY
< row
) {
3298 cursorDown(row
- currentState
.cursorY
, false);
3299 } else if (currentState
.cursorY
> row
) {
3300 cursorUp(currentState
.cursorY
- row
, false);
3303 wrapLineFlag
= false;
3307 * HTS - Horizontal tabulation set.
3309 private void hts() {
3310 for (Integer stop
: tabStops
) {
3311 if (stop
== currentState
.cursorX
) {
3312 // Already have a tab stop here
3317 // Append a tab stop to the end of the array and resort them
3318 tabStops
.add(currentState
.cursorX
);
3319 Collections
.sort(tabStops
);
3323 * DECSWL - Single-width line.
3325 private void decswl() {
3326 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3327 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3331 * DECDWL - Double-width line.
3333 private void decdwl() {
3334 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3335 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3339 * DECHDL - Double-height + double-width line.
3341 * @param topHalf if true, this sets the row to be the top half row of a
3344 private void dechdl(final boolean topHalf
) {
3345 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3346 if (topHalf
== true) {
3347 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3349 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3354 * DECALN - Screen alignment display.
3356 private void decaln() {
3357 Cell newCell
= new Cell();
3358 newCell
.setChar('E');
3359 for (DisplayLine line
: display
) {
3360 for (int i
= 0; i
< line
.length(); i
++) {
3361 line
.replace(i
, newCell
);
3367 * DECSCL - Compatibility level.
3369 private void decscl() {
3370 int i
= getCsiParam(0, 0);
3371 int j
= getCsiParam(1, 0);
3375 currentState
.g0Charset
= CharacterSet
.US
;
3376 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3378 } else if (i
== 62) {
3380 if ((j
== 0) || (j
== 2)) {
3382 } else if (j
== 1) {
3389 * CUD - Cursor down.
3391 private void cud() {
3392 cursorDown(getCsiParam(0, 1, 1, height
), true);
3396 * CUF - Cursor forward.
3398 private void cuf() {
3399 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3403 * CUB - Cursor backward.
3405 private void cub() {
3406 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3412 private void cuu() {
3413 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3417 * CUP - Cursor position.
3419 private void cup() {
3420 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3421 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3425 * CNL - Cursor down and to column 1.
3427 private void cnl() {
3428 cursorDown(getCsiParam(0, 1, 1, height
), true);
3430 cursorLeft(currentState
.cursorX
, true);
3434 * CPL - Cursor up and to column 1.
3436 private void cpl() {
3437 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3439 cursorLeft(currentState
.cursorX
, true);
3443 * CHA - Cursor to column # in current row.
3445 private void cha() {
3446 cursorPosition(currentState
.cursorY
,
3447 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3451 * VPA - Cursor to row #, same column.
3453 private void vpa() {
3454 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3455 currentState
.cursorX
);
3459 * ED - Erase in display.
3462 boolean honorProtected
= false;
3463 boolean decPrivateModeFlag
= false;
3465 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3466 if (collectBuffer
.charAt(i
) == '?') {
3467 decPrivateModeFlag
= true;
3472 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3473 && (decPrivateModeFlag
== true)
3475 honorProtected
= true;
3478 int i
= getCsiParam(0, 0);
3481 // Erase from here to end of screen
3482 if (currentState
.cursorY
< height
- 1) {
3483 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3486 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3487 } else if (i
== 1) {
3488 // Erase from beginning of screen to here
3489 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3491 eraseLine(0, currentState
.cursorX
, honorProtected
);
3492 } else if (i
== 2) {
3493 // Erase entire screen
3494 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3499 * EL - Erase in line.
3502 boolean honorProtected
= false;
3503 boolean decPrivateModeFlag
= false;
3505 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3506 if (collectBuffer
.charAt(i
) == '?') {
3507 decPrivateModeFlag
= true;
3512 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3513 && (decPrivateModeFlag
== true)
3515 honorProtected
= true;
3518 int i
= getCsiParam(0, 0);
3521 // Erase from here to end of line
3522 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3523 } else if (i
== 1) {
3524 // Erase from beginning of line to here
3525 eraseLine(0, currentState
.cursorX
, honorProtected
);
3526 } else if (i
== 2) {
3527 // Erase entire line
3528 eraseLine(0, width
- 1, honorProtected
);
3533 * ECH - Erase # of characters in current row.
3535 private void ech() {
3536 int i
= getCsiParam(0, 1, 1, width
);
3538 // Erase from here to i characters
3539 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3546 int i
= getCsiParam(0, 1);
3548 if ((currentState
.cursorY
>= scrollRegionTop
)
3549 && (currentState
.cursorY
<= scrollRegionBottom
)
3552 // I can get the same effect with a scroll-down
3553 scrollingRegionScrollDown(currentState
.cursorY
,
3554 scrollRegionBottom
, i
);
3559 * DCH - Delete char.
3561 private void dch() {
3562 int n
= getCsiParam(0, 1);
3563 DisplayLine line
= display
.get(currentState
.cursorY
);
3564 Cell blank
= new Cell();
3565 for (int i
= 0; i
< n
; i
++) {
3566 line
.delete(currentState
.cursorX
, blank
);
3571 * ICH - Insert blank char at cursor.
3573 private void ich() {
3574 int n
= getCsiParam(0, 1);
3575 DisplayLine line
= display
.get(currentState
.cursorY
);
3576 Cell blank
= new Cell();
3577 for (int i
= 0; i
< n
; i
++) {
3578 line
.insert(currentState
.cursorX
, blank
);
3586 int i
= getCsiParam(0, 1);
3588 if ((currentState
.cursorY
>= scrollRegionTop
)
3589 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3591 // I can get the same effect with a scroll-down
3592 scrollingRegionScrollUp(currentState
.cursorY
,
3593 scrollRegionBottom
, i
);
3598 * HVP - Horizontal and vertical position.
3600 private void hvp() {
3605 * REP - Repeat character.
3607 private void rep() {
3608 int n
= getCsiParam(0, 1);
3609 for (int i
= 0; i
< n
; i
++) {
3610 printCharacter(repCh
);
3618 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3619 getCsiParam(0, 1, 1, height
));
3626 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3627 getCsiParam(0, 1, 1, height
));
3631 * CBT - Go back X tab stops.
3633 private void cbt() {
3634 int tabsToMove
= getCsiParam(0, 1);
3637 for (int i
= 0; i
< tabsToMove
; i
++) {
3638 int j
= currentState
.cursorX
;
3639 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3640 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3648 j
= tabStops
.get(tabI
);
3650 cursorPosition(currentState
.cursorY
, j
);
3655 * CHT - Advance X tab stops.
3657 private void cht() {
3658 int n
= getCsiParam(0, 1);
3659 for (int i
= 0; i
< n
; i
++) {
3660 advanceToNextTabStop();
3665 * SGR - Select graphics rendition.
3667 private void sgr() {
3669 if (csiParams
.size() == 0) {
3670 currentState
.attr
.reset();
3674 int sgrColorMode
= -1;
3675 boolean idx88Color
= false;
3676 boolean rgbColor
= false;
3680 for (Integer i
: csiParams
) {
3682 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
3684 assert (type
== DeviceType
.XTERM
);
3688 * Indexed color mode, we now have the index number.
3690 if (sgrColorMode
== 38) {
3691 currentState
.attr
.setForeColorRGB(get88Color(i
));
3693 assert (sgrColorMode
== 48);
3694 currentState
.attr
.setBackColorRGB(get88Color(i
));
3703 * RGB color mode, we are collecting tokens.
3707 } else if (rgbGreen
== -1) {
3708 rgbGreen
= i
& 0xFF;
3710 int rgb
= rgbRed
<< 16;
3711 rgb
|= rgbGreen
<< 8;
3714 // System.err.printf("RGB: %08x\n", rgb);
3716 if (sgrColorMode
== 38) {
3717 currentState
.attr
.setForeColorRGB(rgb
);
3719 assert (sgrColorMode
== 48);
3720 currentState
.attr
.setBackColorRGB(rgb
);
3741 * Indexed color mode.
3748 * This is neither indexed nor RGB color. Bail out.
3753 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
3759 currentState
.attr
.reset();
3764 currentState
.attr
.setBold(true);
3769 currentState
.attr
.setUnderline(true);
3774 currentState
.attr
.setBlink(true);
3779 currentState
.attr
.setReverse(true);
3786 if (type
== DeviceType
.XTERM
) {
3796 // Set black foreground
3797 currentState
.attr
.setForeColorRGB(get88Color(8));
3800 // Set red foreground
3801 currentState
.attr
.setForeColorRGB(get88Color(9));
3804 // Set green foreground
3805 currentState
.attr
.setForeColorRGB(get88Color(10));
3808 // Set yellow foreground
3809 currentState
.attr
.setForeColorRGB(get88Color(11));
3812 // Set blue foreground
3813 currentState
.attr
.setForeColorRGB(get88Color(12));
3816 // Set magenta foreground
3817 currentState
.attr
.setForeColorRGB(get88Color(13));
3820 // Set cyan foreground
3821 currentState
.attr
.setForeColorRGB(get88Color(14));
3824 // Set white foreground
3825 currentState
.attr
.setForeColorRGB(get88Color(15));
3829 // Set black background
3830 currentState
.attr
.setBackColorRGB(get88Color(8));
3833 // Set red background
3834 currentState
.attr
.setBackColorRGB(get88Color(9));
3837 // Set green background
3838 currentState
.attr
.setBackColorRGB(get88Color(10));
3841 // Set yellow background
3842 currentState
.attr
.setBackColorRGB(get88Color(11));
3845 // Set blue background
3846 currentState
.attr
.setBackColorRGB(get88Color(12));
3849 // Set magenta background
3850 currentState
.attr
.setBackColorRGB(get88Color(13));
3853 // Set cyan background
3854 currentState
.attr
.setBackColorRGB(get88Color(14));
3857 // Set white background
3858 currentState
.attr
.setBackColorRGB(get88Color(15));
3866 if ((type
== DeviceType
.VT220
)
3867 || (type
== DeviceType
.XTERM
)) {
3873 currentState
.attr
.setBold(false);
3878 currentState
.attr
.setUnderline(false);
3883 currentState
.attr
.setBlink(false);
3888 currentState
.attr
.setReverse(false);
3896 // A true VT100/102/220 does not support color, however everyone
3897 // is used to their terminal emulator supporting color so we will
3898 // unconditionally support color for all DeviceType's.
3903 // Set black foreground
3904 currentState
.attr
.setForeColor(Color
.BLACK
);
3905 currentState
.attr
.setForeColorRGB(-1);
3908 // Set red foreground
3909 currentState
.attr
.setForeColor(Color
.RED
);
3910 currentState
.attr
.setForeColorRGB(-1);
3913 // Set green foreground
3914 currentState
.attr
.setForeColor(Color
.GREEN
);
3915 currentState
.attr
.setForeColorRGB(-1);
3918 // Set yellow foreground
3919 currentState
.attr
.setForeColor(Color
.YELLOW
);
3920 currentState
.attr
.setForeColorRGB(-1);
3923 // Set blue foreground
3924 currentState
.attr
.setForeColor(Color
.BLUE
);
3925 currentState
.attr
.setForeColorRGB(-1);
3928 // Set magenta foreground
3929 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3930 currentState
.attr
.setForeColorRGB(-1);
3933 // Set cyan foreground
3934 currentState
.attr
.setForeColor(Color
.CYAN
);
3935 currentState
.attr
.setForeColorRGB(-1);
3938 // Set white foreground
3939 currentState
.attr
.setForeColor(Color
.WHITE
);
3940 currentState
.attr
.setForeColorRGB(-1);
3943 if (type
== DeviceType
.XTERM
) {
3945 * Xterm supports T.416 / ISO-8613-3 codes to select
3946 * either an indexed color or an RGB value. (It also
3947 * permits these ISO-8613-3 SGR sequences to be separated
3948 * by colons rather than semicolons.)
3950 * We will support only the following:
3952 * 1. Indexed color mode (88- or 256-color modes).
3956 * These cover most of the use cases in the real world.
3958 * HOWEVER, note that this is an awful broken "standard",
3959 * with no way to do it "right". See
3960 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
3961 * for a detailed discussion of the current state of RGB
3962 * in various terminals, the point of which is that none
3963 * of them really do the same thing despite all appearing
3967 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
3968 * where it is assumed that supporting just the "indexed
3969 * mode" of these sequences (which could align easily
3970 * with existing SGR colors) is assumed to mean full
3971 * support of 24-bit RGB. So it is all or nothing.
3973 * Finally, these sequences break the assumptions of
3974 * standard ECMA-48 style parsers as pointed out at
3975 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
3976 * Therefore in order to keep a clean display, we cannot
3977 * parse anything else in this sequence.
3982 // Underscore on, default foreground color
3983 currentState
.attr
.setUnderline(true);
3984 currentState
.attr
.setForeColor(Color
.WHITE
);
3988 // Underscore off, default foreground color
3989 currentState
.attr
.setUnderline(false);
3990 currentState
.attr
.setForeColor(Color
.WHITE
);
3991 currentState
.attr
.setForeColorRGB(-1);
3994 // Set black background
3995 currentState
.attr
.setBackColor(Color
.BLACK
);
3996 currentState
.attr
.setBackColorRGB(-1);
3999 // Set red background
4000 currentState
.attr
.setBackColor(Color
.RED
);
4001 currentState
.attr
.setBackColorRGB(-1);
4004 // Set green background
4005 currentState
.attr
.setBackColor(Color
.GREEN
);
4006 currentState
.attr
.setBackColorRGB(-1);
4009 // Set yellow background
4010 currentState
.attr
.setBackColor(Color
.YELLOW
);
4011 currentState
.attr
.setBackColorRGB(-1);
4014 // Set blue background
4015 currentState
.attr
.setBackColor(Color
.BLUE
);
4016 currentState
.attr
.setBackColorRGB(-1);
4019 // Set magenta background
4020 currentState
.attr
.setBackColor(Color
.MAGENTA
);
4021 currentState
.attr
.setBackColorRGB(-1);
4024 // Set cyan background
4025 currentState
.attr
.setBackColor(Color
.CYAN
);
4026 currentState
.attr
.setBackColorRGB(-1);
4029 // Set white background
4030 currentState
.attr
.setBackColor(Color
.WHITE
);
4031 currentState
.attr
.setBackColorRGB(-1);
4034 if (type
== DeviceType
.XTERM
) {
4036 * Xterm supports T.416 / ISO-8613-3 codes to select
4037 * either an indexed color or an RGB value. (It also
4038 * permits these ISO-8613-3 SGR sequences to be separated
4039 * by colons rather than semicolons.)
4041 * We will support only the following:
4043 * 1. Indexed color mode (88- or 256-color modes).
4047 * These cover most of the use cases in the real world.
4049 * HOWEVER, note that this is an awful broken "standard",
4050 * with no way to do it "right". See
4051 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4052 * for a detailed discussion of the current state of RGB
4053 * in various terminals, the point of which is that none
4054 * of them really do the same thing despite all appearing
4058 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4059 * where it is assumed that supporting just the "indexed
4060 * mode" of these sequences (which could align easily
4061 * with existing SGR colors) is assumed to mean full
4062 * support of 24-bit RGB. So it is all or nothing.
4064 * Finally, these sequences break the assumptions of
4065 * standard ECMA-48 style parsers as pointed out at
4066 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4067 * Therefore in order to keep a clean display, we cannot
4068 * parse anything else in this sequence.
4075 // Default background
4076 currentState
.attr
.setBackColor(Color
.BLACK
);
4077 currentState
.attr
.setBackColorRGB(-1);
4087 * DA - Device attributes.
4090 int extendedFlag
= 0;
4092 if (collectBuffer
.length() > 0) {
4093 String args
= collectBuffer
.substring(1);
4094 if (collectBuffer
.charAt(0) == '>') {
4096 if (collectBuffer
.length() >= 2) {
4097 i
= Integer
.parseInt(args
.toString());
4099 } else if (collectBuffer
.charAt(0) == '=') {
4101 if (collectBuffer
.length() >= 2) {
4102 i
= Integer
.parseInt(args
.toString());
4105 // Unknown code, bail out
4110 if ((i
!= 0) && (i
!= 1)) {
4114 if ((extendedFlag
== 0) && (i
== 0)) {
4115 // Send string directly to remote side
4116 writeRemote(deviceTypeResponse());
4120 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4122 if ((extendedFlag
== 1) && (i
== 0)) {
4124 * Request "What type of terminal are you, what is your
4125 * firmware version, and what hardware options do you have
4128 * Respond: "I am a VT220 (identification code of 1), my
4129 * firmware version is _____ (Pv), and I have _____ Po
4130 * options installed."
4136 if (s8c1t
== true) {
4137 writeRemote("\u009b>1;10;0c");
4139 writeRemote("\033[>1;10;0c");
4145 if ((extendedFlag
== 2) && (i
== 0)) {
4148 * Request "What is your unit ID?"
4150 * Respond: "I was manufactured at site 00 and have a unique ID
4154 writeRemote("\033P!|00010203\033\\");
4159 * DECSTBM - Set top and bottom margins.
4161 private void decstbm() {
4162 boolean decPrivateModeFlag
= false;
4164 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4165 if (collectBuffer
.charAt(i
) == '?') {
4166 decPrivateModeFlag
= true;
4170 if (decPrivateModeFlag
) {
4171 // This could be restore DEC private mode values.
4175 int top
= getCsiParam(0, 1, 1, height
) - 1;
4176 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4181 scrollRegionTop
= top
;
4182 scrollRegionBottom
= bottom
;
4185 cursorPosition(0, 0);
4190 * DECREQTPARM - Request terminal parameters.
4192 private void decreqtparm() {
4193 int i
= getCsiParam(0, 0);
4195 if ((i
!= 0) && (i
!= 1)) {
4202 * Request terminal parameters.
4206 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4207 * (CLoCk MULtiplier = 1, STP option flags = 0)
4211 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4214 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4216 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4222 * DECSCA - Select Character Attributes.
4224 private void decsca() {
4225 int i
= getCsiParam(0, 0);
4227 if ((i
== 0) || (i
== 2)) {
4229 currentState
.attr
.setProtect(false);
4233 currentState
.attr
.setProtect(true);
4238 * DECSTR - Soft Terminal Reset.
4240 private void decstr() {
4241 // Do exactly like RIS - Reset to initial state
4243 // Do I clear screen too? I think so...
4244 eraseScreen(0, 0, height
- 1, width
- 1, false);
4245 cursorPosition(0, 0);
4249 * DSR - Device status report.
4251 private void dsr() {
4252 boolean decPrivateModeFlag
= false;
4253 int row
= currentState
.cursorY
;
4255 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4256 if (collectBuffer
.charAt(i
) == '?') {
4257 decPrivateModeFlag
= true;
4262 int i
= getCsiParam(0, 0);
4267 // Request status report. Respond with "OK, no malfunction."
4269 // Send string directly to remote side
4270 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4273 writeRemote("\u009b0n");
4275 writeRemote("\033[0n");
4280 // Request cursor position. Respond with current position.
4281 if (currentState
.originMode
== true) {
4282 row
-= scrollRegionTop
;
4285 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4288 str
= String
.format("\u009b%d;%dR", row
+ 1,
4289 currentState
.cursorX
+ 1);
4291 str
= String
.format("\033[%d;%dR", row
+ 1,
4292 currentState
.cursorX
+ 1);
4295 // Send string directly to remote side
4300 if (decPrivateModeFlag
== true) {
4302 // Request printer status report. Respond with "Printer not
4305 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4306 && (s8c1t
== true)) {
4307 writeRemote("\u009b?13n");
4309 writeRemote("\033[?13n");
4315 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4316 && (decPrivateModeFlag
== true)
4319 // Request user-defined keys are locked or unlocked. Respond
4320 // with "User-defined keys are locked."
4322 if (s8c1t
== true) {
4323 writeRemote("\u009b?21n");
4325 writeRemote("\033[?21n");
4331 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4332 && (decPrivateModeFlag
== true)
4335 // Request keyboard language. Respond with "Keyboard
4336 // language is North American."
4338 if (s8c1t
== true) {
4339 writeRemote("\u009b?27;1n");
4341 writeRemote("\033[?27;1n");
4348 // Some other option, ignore
4354 * TBC - Tabulation clear.
4356 private void tbc() {
4357 int i
= getCsiParam(0, 0);
4359 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4360 for (Integer stop
: tabStops
) {
4361 if (stop
== currentState
.cursorX
) {
4366 tabStops
= newStops
;
4374 * Erase the characters in the current line from the start column to the
4375 * end column, inclusive.
4377 * @param start starting column to erase (between 0 and width - 1)
4378 * @param end ending column to erase (between 0 and width - 1)
4379 * @param honorProtected if true, do not erase characters with the
4380 * protected attribute set
4382 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4387 if (end
> width
- 1) {
4394 for (int i
= start
; i
<= end
; i
++) {
4395 DisplayLine line
= display
.get(currentState
.cursorY
);
4396 if ((!honorProtected
)
4397 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4404 * From the VT102 manual:
4406 * Erasing a character also erases any character
4407 * attribute of the character.
4413 * Erase with the current color a.k.a. back-color erase
4416 line
.setChar(i
, ' ');
4417 line
.setAttr(i
, currentState
.attr
);
4425 * Erase a rectangular section of the screen, inclusive. end column,
4428 * @param startRow starting row to erase (between 0 and height - 1)
4429 * @param startCol starting column to erase (between 0 and width - 1)
4430 * @param endRow ending row to erase (between 0 and height - 1)
4431 * @param endCol ending column to erase (between 0 and width - 1)
4432 * @param honorProtected if true, do not erase characters with the
4433 * protected attribute set
4435 private void eraseScreen(final int startRow
, final int startCol
,
4436 final int endRow
, final int endCol
, final boolean honorProtected
) {
4444 || (endRow
< startRow
)
4445 || (endCol
< startCol
)
4450 oldCursorY
= currentState
.cursorY
;
4451 for (int i
= startRow
; i
<= endRow
; i
++) {
4452 currentState
.cursorY
= i
;
4453 eraseLine(startCol
, endCol
, honorProtected
);
4455 // Erase display clears the double attributes
4456 display
.get(i
).setDoubleWidth(false);
4457 display
.get(i
).setDoubleHeight(0);
4459 currentState
.cursorY
= oldCursorY
;
4463 * VT220 printer functions. All of these are parsed, but won't do
4466 private void printerFunctions() {
4467 boolean decPrivateModeFlag
= false;
4468 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4469 if (collectBuffer
.charAt(i
) == '?') {
4470 decPrivateModeFlag
= true;
4475 int i
= getCsiParam(0, 0);
4480 if (decPrivateModeFlag
== false) {
4486 if (decPrivateModeFlag
== true) {
4487 // Print cursor line
4492 if (decPrivateModeFlag
== true) {
4493 // Auto print mode OFF
4495 // Printer controller OFF
4497 // Characters re-appear on the screen
4498 printerControllerMode
= false;
4503 if (decPrivateModeFlag
== true) {
4507 // Printer controller
4509 // Characters get sucked into oblivion
4510 printerControllerMode
= true;
4521 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4522 * of remote systems will send an XTerm title sequence even if TERM isn't
4525 * @param xtermChar the character received from the remote side
4527 private void oscPut(final char xtermChar
) {
4528 // System.err.println("oscPut: " + xtermChar);
4531 collectBuffer
.append(xtermChar
);
4534 if ((xtermChar
== 0x07)
4535 || (collectBuffer
.toString().endsWith("\033\\"))
4538 if (xtermChar
== 0x07) {
4539 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
4541 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4544 String
[] p
= args
.toString().split(";");
4546 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4553 if (p
[0].equals("4")) {
4554 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
4555 // Set a color index value
4557 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
4558 } catch (NumberFormatException e
) {
4565 // Go to SCAN_GROUND state
4572 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
4573 * Jexer ECMA48Terminal to talk to ECMA48.
4575 * @param pmChar the character received from the remote side
4577 private void pmPut(final char pmChar
) {
4578 // System.err.println("pmPut: " + pmChar);
4581 collectBuffer
.append(pmChar
);
4584 if (collectBuffer
.toString().endsWith("\033\\")) {
4586 arg
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4588 // System.err.println("arg: '" + arg + "'");
4590 if (arg
.equals("hideMousePointer")) {
4591 hideMousePointer
= true;
4593 if (arg
.equals("showMousePointer")) {
4594 hideMousePointer
= false;
4597 // Go to SCAN_GROUND state
4604 * Run this input character through the ECMA48 state machine.
4606 * @param ch character from the remote side
4608 private void consume(char ch
) {
4611 // System.err.printf("%c", ch);
4613 // Special case for VT10x: 7-bit characters only
4614 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4615 ch
= (char)(ch
& 0x7F);
4618 // Special "anywhere" states
4620 // 18, 1A --> execute, then switch to SCAN_GROUND
4621 if ((ch
== 0x18) || (ch
== 0x1A)) {
4622 // CAN and SUB abort escape sequences
4627 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4631 if ((type
== DeviceType
.XTERM
)
4632 && ((scanState
== ScanState
.OSC_STRING
)
4633 || (scanState
== ScanState
.SOSPMAPC_STRING
))
4635 // Xterm can pass ESCAPE to its OSC sequence.
4636 // Jexer can pass ESCAPE to its PM sequence.
4637 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
4638 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4639 && (scanState
!= ScanState
.DCS_IGNORE
)
4640 && (scanState
!= ScanState
.DCS_PARAM
)
4641 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4644 scanState
= ScanState
.ESCAPE
;
4649 // 0x9B == CSI 8-bit sequence
4651 scanState
= ScanState
.CSI_ENTRY
;
4655 // 0x9D goes to ScanState.OSC_STRING
4657 scanState
= ScanState
.OSC_STRING
;
4661 // 0x90 goes to DCS_ENTRY
4663 scanState
= ScanState
.DCS_ENTRY
;
4667 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4668 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4669 scanState
= ScanState
.SOSPMAPC_STRING
;
4673 // 0x7F (DEL) is always discarded
4678 switch (scanState
) {
4681 // 00-17, 19, 1C-1F --> execute
4682 // 80-8F, 91-9A, 9C --> execute
4683 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4684 handleControlChar(ch
);
4688 if (((ch
>= 0x20) && (ch
<= 0x7F))
4692 // VT220 printer --> trash bin
4693 if (((type
== DeviceType
.VT220
)
4694 || (type
== DeviceType
.XTERM
))
4695 && (printerControllerMode
== true)
4700 // Hang onto this character
4701 repCh
= mapCharacter(ch
);
4703 // Print this character
4704 printCharacter(repCh
);
4709 // 00-17, 19, 1C-1F --> execute
4711 handleControlChar(ch
);
4715 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4716 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4718 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4722 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4723 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4734 // DECSC - Save cursor
4735 // Note this code overlaps both ANSI and VT52 mode
4740 // DECRC - Restore cursor
4741 // Note this code overlaps both ANSI and VT52 mode
4750 if (vt52Mode
== true) {
4751 // DECANM - Enter ANSI mode
4753 arrowKeyMode
= ArrowKeyMode
.VT100
;
4756 * From the VT102 docs: "You use ANSI mode to select
4757 * most terminal features; the terminal uses the same
4758 * features when it switches to VT52 mode. You
4759 * cannot, however, change most of these features in
4762 * In other words, do not reset any other attributes
4763 * when switching between VT52 submode and ANSI.
4767 currentState
.g0Charset
= CharacterSet
.US
;
4768 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4770 singleshift
= Singleshift
.NONE
;
4771 currentState
.glLockshift
= LockshiftMode
.NONE
;
4772 currentState
.grLockshift
= LockshiftMode
.NONE
;
4776 // DECKPAM - Keypad application mode
4777 // Note this code overlaps both ANSI and VT52 mode
4781 // DECKPNM - Keypad numeric mode
4782 // Note this code overlaps both ANSI and VT52 mode
4789 if (vt52Mode
== true) {
4790 // Cursor up, and stop at the top without scrolling
4795 if (vt52Mode
== true) {
4796 // Cursor down, and stop at the bottom without scrolling
4797 cursorDown(1, false);
4801 if (vt52Mode
== true) {
4802 // Cursor right, and stop at the right without scrolling
4803 cursorRight(1, false);
4807 if (vt52Mode
== true) {
4808 // Cursor left, and stop at the left without scrolling
4809 cursorLeft(1, false);
4816 if (vt52Mode
== true) {
4824 if (vt52Mode
== true) {
4825 // G0 --> Special graphics
4826 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
4830 if (vt52Mode
== true) {
4832 currentState
.g0Charset
= CharacterSet
.US
;
4836 if (vt52Mode
== true) {
4838 cursorPosition(0, 0);
4840 // HTS - Horizontal tabulation set
4845 if (vt52Mode
== true) {
4846 // Reverse line feed. Same as RI.
4851 if (vt52Mode
== true) {
4852 // Erase to end of screen
4853 eraseLine(currentState
.cursorX
, width
- 1, false);
4854 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4859 if (vt52Mode
== true) {
4860 // Erase to end of line
4861 eraseLine(currentState
.cursorX
, width
- 1, false);
4867 if (vt52Mode
== true) {
4870 // RI - Reverse index
4875 if (vt52Mode
== false) {
4877 singleshift
= Singleshift
.SS2
;
4881 if (vt52Mode
== false) {
4883 singleshift
= Singleshift
.SS3
;
4890 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4906 if (vt52Mode
== true) {
4907 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4915 if (vt52Mode
== true) {
4917 // Send string directly to remote side
4918 writeRemote("\033/Z");
4921 // Send string directly to remote side
4922 writeRemote(deviceTypeResponse());
4933 // VT52 cannot get to any of these other states
4934 if (vt52Mode
== true) {
4939 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4946 // RIS - Reset to initial state
4948 // Do I clear screen too? I think so...
4949 eraseScreen(0, 0, height
- 1, width
- 1, false);
4950 cursorPosition(0, 0);
4964 if ((type
== DeviceType
.VT220
)
4965 || (type
== DeviceType
.XTERM
)) {
4967 // VT220 lockshift G2 into GL
4968 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4973 if ((type
== DeviceType
.VT220
)
4974 || (type
== DeviceType
.XTERM
)) {
4976 // VT220 lockshift G3 into GL
4977 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4995 if ((type
== DeviceType
.VT220
)
4996 || (type
== DeviceType
.XTERM
)) {
4998 // VT220 lockshift G3 into GR
4999 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
5004 if ((type
== DeviceType
.VT220
)
5005 || (type
== DeviceType
.XTERM
)) {
5007 // VT220 lockshift G2 into GR
5008 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
5014 if ((type
== DeviceType
.VT220
)
5015 || (type
== DeviceType
.XTERM
)) {
5017 // VT220 lockshift G1 into GR
5018 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
5028 // 0x5B goes to CSI_ENTRY
5030 scanState
= ScanState
.CSI_ENTRY
;
5033 // 0x5D goes to OSC_STRING
5035 scanState
= ScanState
.OSC_STRING
;
5038 // 0x50 goes to DCS_ENTRY
5040 scanState
= ScanState
.DCS_ENTRY
;
5043 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5044 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
5045 scanState
= ScanState
.SOSPMAPC_STRING
;
5050 case ESCAPE_INTERMEDIATE
:
5051 // 00-17, 19, 1C-1F --> execute
5053 handleControlChar(ch
);
5056 // 20-2F --> collect
5057 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5061 // 30-7E --> dispatch, then switch to GROUND
5062 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
5065 if ((collectBuffer
.length() == 1)
5066 && (collectBuffer
.charAt(0) == '(')) {
5067 // G0 --> Special graphics
5068 currentState
.g0Charset
= CharacterSet
.DRAWING
;
5070 if ((collectBuffer
.length() == 1)
5071 && (collectBuffer
.charAt(0) == ')')) {
5072 // G1 --> Special graphics
5073 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5075 if ((type
== DeviceType
.VT220
)
5076 || (type
== DeviceType
.XTERM
)) {
5078 if ((collectBuffer
.length() == 1)
5079 && (collectBuffer
.charAt(0) == '*')) {
5080 // G2 --> Special graphics
5081 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5083 if ((collectBuffer
.length() == 1)
5084 && (collectBuffer
.charAt(0) == '+')) {
5085 // G3 --> Special graphics
5086 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5091 if ((collectBuffer
.length() == 1)
5092 && (collectBuffer
.charAt(0) == '(')) {
5093 // G0 --> Alternate character ROM standard character set
5094 currentState
.g0Charset
= CharacterSet
.ROM
;
5096 if ((collectBuffer
.length() == 1)
5097 && (collectBuffer
.charAt(0) == ')')) {
5098 // G1 --> Alternate character ROM standard character set
5099 currentState
.g1Charset
= CharacterSet
.ROM
;
5103 if ((collectBuffer
.length() == 1)
5104 && (collectBuffer
.charAt(0) == '(')) {
5105 // G0 --> Alternate character ROM special graphics
5106 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5108 if ((collectBuffer
.length() == 1)
5109 && (collectBuffer
.charAt(0) == ')')) {
5110 // G1 --> Alternate character ROM special graphics
5111 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5115 if ((collectBuffer
.length() == 1)
5116 && (collectBuffer
.charAt(0) == '#')) {
5117 // DECDHL - Double-height line (top half)
5122 if ((collectBuffer
.length() == 1)
5123 && (collectBuffer
.charAt(0) == '#')) {
5124 // DECDHL - Double-height line (bottom half)
5127 if ((type
== DeviceType
.VT220
)
5128 || (type
== DeviceType
.XTERM
)) {
5130 if ((collectBuffer
.length() == 1)
5131 && (collectBuffer
.charAt(0) == '(')) {
5133 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5135 if ((collectBuffer
.length() == 1)
5136 && (collectBuffer
.charAt(0) == ')')) {
5138 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5140 if ((collectBuffer
.length() == 1)
5141 && (collectBuffer
.charAt(0) == '*')) {
5143 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5145 if ((collectBuffer
.length() == 1)
5146 && (collectBuffer
.charAt(0) == '+')) {
5148 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5153 if ((collectBuffer
.length() == 1)
5154 && (collectBuffer
.charAt(0) == '#')) {
5155 // DECSWL - Single-width line
5158 if ((type
== DeviceType
.VT220
)
5159 || (type
== DeviceType
.XTERM
)) {
5161 if ((collectBuffer
.length() == 1)
5162 && (collectBuffer
.charAt(0) == '(')) {
5164 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5166 if ((collectBuffer
.length() == 1)
5167 && (collectBuffer
.charAt(0) == ')')) {
5169 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5171 if ((collectBuffer
.length() == 1)
5172 && (collectBuffer
.charAt(0) == '*')) {
5174 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5176 if ((collectBuffer
.length() == 1)
5177 && (collectBuffer
.charAt(0) == '+')) {
5179 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5184 if ((collectBuffer
.length() == 1)
5185 && (collectBuffer
.charAt(0) == '#')) {
5186 // DECDWL - Double-width line
5189 if ((type
== DeviceType
.VT220
)
5190 || (type
== DeviceType
.XTERM
)) {
5192 if ((collectBuffer
.length() == 1)
5193 && (collectBuffer
.charAt(0) == '(')) {
5195 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5197 if ((collectBuffer
.length() == 1)
5198 && (collectBuffer
.charAt(0) == ')')) {
5200 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5202 if ((collectBuffer
.length() == 1)
5203 && (collectBuffer
.charAt(0) == '*')) {
5205 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5207 if ((collectBuffer
.length() == 1)
5208 && (collectBuffer
.charAt(0) == '+')) {
5210 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5215 if ((type
== DeviceType
.VT220
)
5216 || (type
== DeviceType
.XTERM
)) {
5218 if ((collectBuffer
.length() == 1)
5219 && (collectBuffer
.charAt(0) == '(')) {
5221 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5223 if ((collectBuffer
.length() == 1)
5224 && (collectBuffer
.charAt(0) == ')')) {
5226 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5228 if ((collectBuffer
.length() == 1)
5229 && (collectBuffer
.charAt(0) == '*')) {
5231 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5233 if ((collectBuffer
.length() == 1)
5234 && (collectBuffer
.charAt(0) == '+')) {
5236 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5241 if ((collectBuffer
.length() == 1)
5242 && (collectBuffer
.charAt(0) == '#')) {
5243 // DECALN - Screen alignment display
5252 if ((type
== DeviceType
.VT220
)
5253 || (type
== DeviceType
.XTERM
)) {
5255 if ((collectBuffer
.length() == 1)
5256 && (collectBuffer
.charAt(0) == '(')) {
5257 // G0 --> DEC_SUPPLEMENTAL
5258 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5260 if ((collectBuffer
.length() == 1)
5261 && (collectBuffer
.charAt(0) == ')')) {
5262 // G1 --> DEC_SUPPLEMENTAL
5263 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5265 if ((collectBuffer
.length() == 1)
5266 && (collectBuffer
.charAt(0) == '*')) {
5267 // G2 --> DEC_SUPPLEMENTAL
5268 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5270 if ((collectBuffer
.length() == 1)
5271 && (collectBuffer
.charAt(0) == '+')) {
5272 // G3 --> DEC_SUPPLEMENTAL
5273 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5278 if ((type
== DeviceType
.VT220
)
5279 || (type
== DeviceType
.XTERM
)) {
5281 if ((collectBuffer
.length() == 1)
5282 && (collectBuffer
.charAt(0) == '(')) {
5284 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5286 if ((collectBuffer
.length() == 1)
5287 && (collectBuffer
.charAt(0) == ')')) {
5289 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5291 if ((collectBuffer
.length() == 1)
5292 && (collectBuffer
.charAt(0) == '*')) {
5294 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5296 if ((collectBuffer
.length() == 1)
5297 && (collectBuffer
.charAt(0) == '+')) {
5299 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5308 if ((collectBuffer
.length() == 1)
5309 && (collectBuffer
.charAt(0) == '(')) {
5310 // G0 --> United Kingdom set
5311 currentState
.g0Charset
= CharacterSet
.UK
;
5313 if ((collectBuffer
.length() == 1)
5314 && (collectBuffer
.charAt(0) == ')')) {
5315 // G1 --> United Kingdom set
5316 currentState
.g1Charset
= CharacterSet
.UK
;
5318 if ((type
== DeviceType
.VT220
)
5319 || (type
== DeviceType
.XTERM
)) {
5321 if ((collectBuffer
.length() == 1)
5322 && (collectBuffer
.charAt(0) == '*')) {
5323 // G2 --> United Kingdom set
5324 currentState
.g2Charset
= CharacterSet
.UK
;
5326 if ((collectBuffer
.length() == 1)
5327 && (collectBuffer
.charAt(0) == '+')) {
5328 // G3 --> United Kingdom set
5329 currentState
.g3Charset
= CharacterSet
.UK
;
5334 if ((collectBuffer
.length() == 1)
5335 && (collectBuffer
.charAt(0) == '(')) {
5337 currentState
.g0Charset
= CharacterSet
.US
;
5339 if ((collectBuffer
.length() == 1)
5340 && (collectBuffer
.charAt(0) == ')')) {
5342 currentState
.g1Charset
= CharacterSet
.US
;
5344 if ((type
== DeviceType
.VT220
)
5345 || (type
== DeviceType
.XTERM
)) {
5347 if ((collectBuffer
.length() == 1)
5348 && (collectBuffer
.charAt(0) == '*')) {
5350 currentState
.g2Charset
= CharacterSet
.US
;
5352 if ((collectBuffer
.length() == 1)
5353 && (collectBuffer
.charAt(0) == '+')) {
5355 currentState
.g3Charset
= CharacterSet
.US
;
5360 if ((type
== DeviceType
.VT220
)
5361 || (type
== DeviceType
.XTERM
)) {
5363 if ((collectBuffer
.length() == 1)
5364 && (collectBuffer
.charAt(0) == '(')) {
5366 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5368 if ((collectBuffer
.length() == 1)
5369 && (collectBuffer
.charAt(0) == ')')) {
5371 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5373 if ((collectBuffer
.length() == 1)
5374 && (collectBuffer
.charAt(0) == '*')) {
5376 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5378 if ((collectBuffer
.length() == 1)
5379 && (collectBuffer
.charAt(0) == '+')) {
5381 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5388 if ((type
== DeviceType
.VT220
)
5389 || (type
== DeviceType
.XTERM
)) {
5391 if ((collectBuffer
.length() == 1)
5392 && (collectBuffer
.charAt(0) == '(')) {
5394 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5396 if ((collectBuffer
.length() == 1)
5397 && (collectBuffer
.charAt(0) == ')')) {
5399 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5401 if ((collectBuffer
.length() == 1)
5402 && (collectBuffer
.charAt(0) == '*')) {
5404 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5406 if ((collectBuffer
.length() == 1)
5407 && (collectBuffer
.charAt(0) == '+')) {
5409 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5414 if ((type
== DeviceType
.VT220
)
5415 || (type
== DeviceType
.XTERM
)) {
5417 if ((collectBuffer
.length() == 1)
5418 && (collectBuffer
.charAt(0) == ' ')) {
5425 if ((type
== DeviceType
.VT220
)
5426 || (type
== DeviceType
.XTERM
)) {
5428 if ((collectBuffer
.length() == 1)
5429 && (collectBuffer
.charAt(0) == ' ')) {
5436 if ((type
== DeviceType
.VT220
)
5437 || (type
== DeviceType
.XTERM
)) {
5439 if ((collectBuffer
.length() == 1)
5440 && (collectBuffer
.charAt(0) == '(')) {
5442 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5444 if ((collectBuffer
.length() == 1)
5445 && (collectBuffer
.charAt(0) == ')')) {
5447 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5449 if ((collectBuffer
.length() == 1)
5450 && (collectBuffer
.charAt(0) == '*')) {
5452 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5454 if ((collectBuffer
.length() == 1)
5455 && (collectBuffer
.charAt(0) == '+')) {
5457 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5465 if ((type
== DeviceType
.VT220
)
5466 || (type
== DeviceType
.XTERM
)) {
5468 if ((collectBuffer
.length() == 1)
5469 && (collectBuffer
.charAt(0) == '(')) {
5471 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5473 if ((collectBuffer
.length() == 1)
5474 && (collectBuffer
.charAt(0) == ')')) {
5476 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5478 if ((collectBuffer
.length() == 1)
5479 && (collectBuffer
.charAt(0) == '*')) {
5481 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5483 if ((collectBuffer
.length() == 1)
5484 && (collectBuffer
.charAt(0) == '+')) {
5486 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5497 if ((type
== DeviceType
.VT220
)
5498 || (type
== DeviceType
.XTERM
)) {
5500 if ((collectBuffer
.length() == 1)
5501 && (collectBuffer
.charAt(0) == '(')) {
5503 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5505 if ((collectBuffer
.length() == 1)
5506 && (collectBuffer
.charAt(0) == ')')) {
5508 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5510 if ((collectBuffer
.length() == 1)
5511 && (collectBuffer
.charAt(0) == '*')) {
5513 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5515 if ((collectBuffer
.length() == 1)
5516 && (collectBuffer
.charAt(0) == '+')) {
5518 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5523 if ((type
== DeviceType
.VT220
)
5524 || (type
== DeviceType
.XTERM
)) {
5526 if ((collectBuffer
.length() == 1)
5527 && (collectBuffer
.charAt(0) == '(')) {
5529 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5531 if ((collectBuffer
.length() == 1)
5532 && (collectBuffer
.charAt(0) == ')')) {
5534 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5536 if ((collectBuffer
.length() == 1)
5537 && (collectBuffer
.charAt(0) == '*')) {
5539 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5541 if ((collectBuffer
.length() == 1)
5542 && (collectBuffer
.charAt(0) == '+')) {
5544 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5556 if ((type
== DeviceType
.VT220
)
5557 || (type
== DeviceType
.XTERM
)) {
5559 if ((collectBuffer
.length() == 1)
5560 && (collectBuffer
.charAt(0) == '(')) {
5562 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5564 if ((collectBuffer
.length() == 1)
5565 && (collectBuffer
.charAt(0) == ')')) {
5567 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5569 if ((collectBuffer
.length() == 1)
5570 && (collectBuffer
.charAt(0) == '*')) {
5572 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5574 if ((collectBuffer
.length() == 1)
5575 && (collectBuffer
.charAt(0) == '+')) {
5577 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5582 if ((type
== DeviceType
.VT220
)
5583 || (type
== DeviceType
.XTERM
)) {
5585 if ((collectBuffer
.length() == 1)
5586 && (collectBuffer
.charAt(0) == '(')) {
5588 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5590 if ((collectBuffer
.length() == 1)
5591 && (collectBuffer
.charAt(0) == ')')) {
5593 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5595 if ((collectBuffer
.length() == 1)
5596 && (collectBuffer
.charAt(0) == '*')) {
5598 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5600 if ((collectBuffer
.length() == 1)
5601 && (collectBuffer
.charAt(0) == '+')) {
5603 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5650 // 0x9C goes to GROUND
5658 // 00-17, 19, 1C-1F --> execute
5660 handleControlChar(ch
);
5663 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5664 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5666 scanState
= ScanState
.CSI_INTERMEDIATE
;
5669 // 30-39, 3B --> param, then switch to CSI_PARAM
5670 if ((ch
>= '0') && (ch
<= '9')) {
5672 scanState
= ScanState
.CSI_PARAM
;
5676 scanState
= ScanState
.CSI_PARAM
;
5679 // 3C-3F --> collect, then switch to CSI_PARAM
5680 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5682 scanState
= ScanState
.CSI_PARAM
;
5685 // 40-7E --> dispatch, then switch to GROUND
5686 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5689 // ICH - Insert character
5697 // CUD - Cursor down
5701 // CUF - Cursor forward
5705 // CUB - Cursor backward
5709 // CNL - Cursor down and to column 1
5710 if (type
== DeviceType
.XTERM
) {
5715 // CPL - Cursor up and to column 1
5716 if (type
== DeviceType
.XTERM
) {
5721 // CHA - Cursor to column # in current row
5722 if (type
== DeviceType
.XTERM
) {
5727 // CUP - Cursor position
5731 // CHT - Cursor forward X tab stops (default 1)
5732 if (type
== DeviceType
.XTERM
) {
5737 // ED - Erase in display
5741 // EL - Erase in line
5756 // DCH - Delete character
5763 // Scroll up X lines (default 1)
5764 if (type
== DeviceType
.XTERM
) {
5769 // Scroll down X lines (default 1)
5770 if (type
== DeviceType
.XTERM
) {
5779 if ((type
== DeviceType
.VT220
)
5780 || (type
== DeviceType
.XTERM
)) {
5782 // ECH - Erase character
5789 // CBT - Cursor backward X tab stops (default 1)
5790 if (type
== DeviceType
.XTERM
) {
5801 // HPA - Cursor to column # in current row. Same as CHA
5802 if (type
== DeviceType
.XTERM
) {
5807 // HPR - Cursor right. Same as CUF
5808 if (type
== DeviceType
.XTERM
) {
5813 // REP - Repeat last char X times
5814 if (type
== DeviceType
.XTERM
) {
5819 // DA - Device attributes
5823 // VPA - Cursor to row, current column.
5824 if (type
== DeviceType
.XTERM
) {
5829 // VPR - Cursor down. Same as CUD
5830 if (type
== DeviceType
.XTERM
) {
5835 // HVP - Horizontal and vertical position
5839 // TBC - Tabulation clear
5843 // Sets an ANSI or DEC private toggle
5847 if ((type
== DeviceType
.VT220
)
5848 || (type
== DeviceType
.XTERM
)) {
5850 // Printer functions
5858 // Sets an ANSI or DEC private toggle
5862 // SGR - Select graphics rendition
5866 // DSR - Device status report
5873 // DECLL - Load leds
5877 // DECSTBM - Set top and bottom margins
5881 // Save cursor (ANSI.SYS)
5882 if (type
== DeviceType
.XTERM
) {
5883 savedState
.cursorX
= currentState
.cursorX
;
5884 savedState
.cursorY
= currentState
.cursorY
;
5890 // Restore cursor (ANSI.SYS)
5891 if (type
== DeviceType
.XTERM
) {
5892 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5899 // DECREQTPARM - Request terminal parameters
5915 // 0x9C goes to GROUND
5920 // 0x3A goes to CSI_IGNORE
5922 scanState
= ScanState
.CSI_IGNORE
;
5927 // 00-17, 19, 1C-1F --> execute
5929 handleControlChar(ch
);
5932 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5933 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5935 scanState
= ScanState
.CSI_INTERMEDIATE
;
5938 // 30-39, 3B --> param
5939 if ((ch
>= '0') && (ch
<= '9')) {
5946 // 0x3A goes to CSI_IGNORE
5948 scanState
= ScanState
.CSI_IGNORE
;
5950 // 0x3C-3F goes to CSI_IGNORE
5951 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5952 scanState
= ScanState
.CSI_IGNORE
;
5955 // 40-7E --> dispatch, then switch to GROUND
5956 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5959 // ICH - Insert character
5967 // CUD - Cursor down
5971 // CUF - Cursor forward
5975 // CUB - Cursor backward
5979 // CNL - Cursor down and to column 1
5980 if (type
== DeviceType
.XTERM
) {
5985 // CPL - Cursor up and to column 1
5986 if (type
== DeviceType
.XTERM
) {
5991 // CHA - Cursor to column # in current row
5992 if (type
== DeviceType
.XTERM
) {
5997 // CUP - Cursor position
6001 // CHT - Cursor forward X tab stops (default 1)
6002 if (type
== DeviceType
.XTERM
) {
6007 // ED - Erase in display
6011 // EL - Erase in line
6026 // DCH - Delete character
6033 // Scroll up X lines (default 1)
6034 if (type
== DeviceType
.XTERM
) {
6039 // Scroll down X lines (default 1)
6040 if (type
== DeviceType
.XTERM
) {
6049 if ((type
== DeviceType
.VT220
)
6050 || (type
== DeviceType
.XTERM
)) {
6052 // ECH - Erase character
6059 // CBT - Cursor backward X tab stops (default 1)
6060 if (type
== DeviceType
.XTERM
) {
6071 // HPA - Cursor to column # in current row. Same as CHA
6072 if (type
== DeviceType
.XTERM
) {
6077 // HPR - Cursor right. Same as CUF
6078 if (type
== DeviceType
.XTERM
) {
6083 // REP - Repeat last char X times
6084 if (type
== DeviceType
.XTERM
) {
6089 // DA - Device attributes
6093 // VPA - Cursor to row, current column.
6094 if (type
== DeviceType
.XTERM
) {
6099 // VPR - Cursor down. Same as CUD
6100 if (type
== DeviceType
.XTERM
) {
6105 // HVP - Horizontal and vertical position
6109 // TBC - Tabulation clear
6113 // Sets an ANSI or DEC private toggle
6117 if ((type
== DeviceType
.VT220
)
6118 || (type
== DeviceType
.XTERM
)) {
6120 // Printer functions
6128 // Sets an ANSI or DEC private toggle
6132 // SGR - Select graphics rendition
6136 // DSR - Device status report
6143 // DECLL - Load leds
6147 // DECSTBM - Set top and bottom margins
6157 // DECREQTPARM - Request terminal parameters
6174 case CSI_INTERMEDIATE
:
6175 // 00-17, 19, 1C-1F --> execute
6177 handleControlChar(ch
);
6180 // 20-2F --> collect
6181 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6185 // 0x30-3F goes to CSI_IGNORE
6186 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6187 scanState
= ScanState
.CSI_IGNORE
;
6190 // 40-7E --> dispatch, then switch to GROUND
6191 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6243 if (((type
== DeviceType
.VT220
)
6244 || (type
== DeviceType
.XTERM
))
6245 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6247 // DECSCL - compatibility level
6250 if ((type
== DeviceType
.XTERM
)
6251 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6253 // DECSTR - Soft terminal reset
6258 if (((type
== DeviceType
.VT220
)
6259 || (type
== DeviceType
.XTERM
))
6260 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6288 // 00-17, 19, 1C-1F --> execute
6290 handleControlChar(ch
);
6293 // 20-2F --> collect
6294 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6298 // 40-7E --> ignore, then switch to GROUND
6299 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6303 // 20-3F, 7F --> ignore
6309 // 0x9C goes to GROUND
6314 // 0x1B 0x5C goes to GROUND
6319 if ((collectBuffer
.length() > 0)
6320 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6326 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6327 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6329 scanState
= ScanState
.DCS_INTERMEDIATE
;
6332 // 30-39, 3B --> param, then switch to DCS_PARAM
6333 if ((ch
>= '0') && (ch
<= '9')) {
6335 scanState
= ScanState
.DCS_PARAM
;
6339 scanState
= ScanState
.DCS_PARAM
;
6342 // 3C-3F --> collect, then switch to DCS_PARAM
6343 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6345 scanState
= ScanState
.DCS_PARAM
;
6348 // 00-17, 19, 1C-1F, 7F --> ignore
6350 // 0x3A goes to DCS_IGNORE
6352 scanState
= ScanState
.DCS_IGNORE
;
6355 // 0x40-7E goes to DCS_PASSTHROUGH
6356 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6357 scanState
= ScanState
.DCS_PASSTHROUGH
;
6361 case DCS_INTERMEDIATE
:
6363 // 0x9C goes to GROUND
6368 // 0x1B 0x5C goes to GROUND
6373 if ((collectBuffer
.length() > 0)
6374 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6380 // 0x30-3F goes to DCS_IGNORE
6381 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6382 scanState
= ScanState
.DCS_IGNORE
;
6385 // 0x40-7E goes to DCS_PASSTHROUGH
6386 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6387 scanState
= ScanState
.DCS_PASSTHROUGH
;
6390 // 00-17, 19, 1C-1F, 7F --> ignore
6395 // 0x9C goes to GROUND
6400 // 0x1B 0x5C goes to GROUND
6405 if ((collectBuffer
.length() > 0)
6406 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6412 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6413 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6415 scanState
= ScanState
.DCS_INTERMEDIATE
;
6418 // 30-39, 3B --> param
6419 if ((ch
>= '0') && (ch
<= '9')) {
6426 // 00-17, 19, 1C-1F, 7F --> ignore
6428 // 0x3A, 3C-3F goes to DCS_IGNORE
6430 scanState
= ScanState
.DCS_IGNORE
;
6432 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6433 scanState
= ScanState
.DCS_IGNORE
;
6436 // 0x40-7E goes to DCS_PASSTHROUGH
6437 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6438 scanState
= ScanState
.DCS_PASSTHROUGH
;
6442 case DCS_PASSTHROUGH
:
6443 // 0x9C goes to GROUND
6448 // 0x1B 0x5C goes to GROUND
6453 if ((collectBuffer
.length() > 0)
6454 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6460 // 00-17, 19, 1C-1F, 20-7E --> put
6468 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6471 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6480 // 00-17, 19, 1C-1F, 20-7F --> ignore
6482 // 0x9C goes to GROUND
6489 case SOSPMAPC_STRING
:
6490 // 00-17, 19, 1C-1F, 20-7F --> ignore
6492 // Special case for Jexer: PM can pass one control character
6497 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6501 // 0x9C goes to GROUND
6509 // Special case for Xterm: OSC can pass control characters
6510 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
6514 // 00-17, 19, 1C-1F --> ignore
6516 // 20-7F --> osc_put
6517 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6521 // 0x9C goes to GROUND
6528 case VT52_DIRECT_CURSOR_ADDRESS
:
6529 // This is a special case for the VT52 sequence "ESC Y l c"
6530 if (collectBuffer
.length() == 0) {
6532 } else if (collectBuffer
.length() == 1) {
6533 // We've got the two characters, one in the buffer and the
6535 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
6544 * Expose current cursor X to outside world.
6546 * @return current cursor X
6548 public final int getCursorX() {
6549 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6550 return currentState
.cursorX
* 2;
6552 return currentState
.cursorX
;
6556 * Expose current cursor Y to outside world.
6558 * @return current cursor Y
6560 public final int getCursorY() {
6561 return currentState
.cursorY
;
6565 * Returns true if this terminal has requested the mouse pointer be
6568 * @return true if this terminal has requested the mouse pointer be
6571 public final boolean hasHiddenMousePointer() {
6572 return hideMousePointer
;