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(Integer
.valueOf(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 @SuppressWarnings("fallthrough")
1633 private String
keypressToString(final TKeypress keypress
) {
1635 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1637 * If this is a control character, process it like it came from
1640 if (keypress
.getChar() < 0x20) {
1641 handleControlChar(keypress
.getChar());
1643 // Local echo for everything else
1644 printCharacter(keypress
.getChar());
1648 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1653 // Handle control characters
1654 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1655 StringBuilder sb
= new StringBuilder();
1656 char ch
= keypress
.getChar();
1659 return sb
.toString();
1662 // Handle alt characters
1663 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1664 StringBuilder sb
= new StringBuilder("\033");
1665 char ch
= keypress
.getChar();
1667 return sb
.toString();
1670 if (keypress
.equals(kbBackspaceDel
)) {
1683 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1686 switch (arrowKeyMode
) {
1688 return xtermBuildKeySequence("\033[", '1', 'D',
1689 keypress
.isCtrl(), keypress
.isAlt(),
1690 keypress
.isShift());
1692 return xtermBuildKeySequence("\033", '1', 'D',
1693 keypress
.isCtrl(), keypress
.isAlt(),
1694 keypress
.isShift());
1696 return xtermBuildKeySequence("\033O", '1', 'D',
1697 keypress
.isCtrl(), keypress
.isAlt(),
1698 keypress
.isShift());
1701 switch (arrowKeyMode
) {
1712 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1715 switch (arrowKeyMode
) {
1717 return xtermBuildKeySequence("\033[", '1', 'C',
1718 keypress
.isCtrl(), keypress
.isAlt(),
1719 keypress
.isShift());
1721 return xtermBuildKeySequence("\033", '1', 'C',
1722 keypress
.isCtrl(), keypress
.isAlt(),
1723 keypress
.isShift());
1725 return xtermBuildKeySequence("\033O", '1', 'C',
1726 keypress
.isCtrl(), keypress
.isAlt(),
1727 keypress
.isShift());
1730 switch (arrowKeyMode
) {
1741 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1744 switch (arrowKeyMode
) {
1746 return xtermBuildKeySequence("\033[", '1', 'A',
1747 keypress
.isCtrl(), keypress
.isAlt(),
1748 keypress
.isShift());
1750 return xtermBuildKeySequence("\033", '1', 'A',
1751 keypress
.isCtrl(), keypress
.isAlt(),
1752 keypress
.isShift());
1754 return xtermBuildKeySequence("\033O", '1', 'A',
1755 keypress
.isCtrl(), keypress
.isAlt(),
1756 keypress
.isShift());
1759 switch (arrowKeyMode
) {
1770 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1773 switch (arrowKeyMode
) {
1775 return xtermBuildKeySequence("\033[", '1', 'B',
1776 keypress
.isCtrl(), keypress
.isAlt(),
1777 keypress
.isShift());
1779 return xtermBuildKeySequence("\033", '1', 'B',
1780 keypress
.isCtrl(), keypress
.isAlt(),
1781 keypress
.isShift());
1783 return xtermBuildKeySequence("\033O", '1', 'B',
1784 keypress
.isCtrl(), keypress
.isAlt(),
1785 keypress
.isShift());
1788 switch (arrowKeyMode
) {
1799 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1802 switch (arrowKeyMode
) {
1804 return xtermBuildKeySequence("\033[", '1', 'H',
1805 keypress
.isCtrl(), keypress
.isAlt(),
1806 keypress
.isShift());
1808 return xtermBuildKeySequence("\033", '1', 'H',
1809 keypress
.isCtrl(), keypress
.isAlt(),
1810 keypress
.isShift());
1812 return xtermBuildKeySequence("\033O", '1', 'H',
1813 keypress
.isCtrl(), keypress
.isAlt(),
1814 keypress
.isShift());
1817 switch (arrowKeyMode
) {
1828 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1831 switch (arrowKeyMode
) {
1833 return xtermBuildKeySequence("\033[", '1', 'F',
1834 keypress
.isCtrl(), keypress
.isAlt(),
1835 keypress
.isShift());
1837 return xtermBuildKeySequence("\033", '1', 'F',
1838 keypress
.isCtrl(), keypress
.isAlt(),
1839 keypress
.isShift());
1841 return xtermBuildKeySequence("\033O", '1', 'F',
1842 keypress
.isCtrl(), keypress
.isAlt(),
1843 keypress
.isShift());
1846 switch (arrowKeyMode
) {
1857 if (keypress
.equals(kbF1
)) {
1865 if (keypress
.equals(kbF2
)) {
1873 if (keypress
.equals(kbF3
)) {
1881 if (keypress
.equals(kbF4
)) {
1889 if (keypress
.equals(kbF5
)) {
1902 if (keypress
.equals(kbF6
)) {
1915 if (keypress
.equals(kbF7
)) {
1928 if (keypress
.equals(kbF8
)) {
1941 if (keypress
.equals(kbF9
)) {
1954 if (keypress
.equals(kbF10
)) {
1967 if (keypress
.equals(kbF11
)) {
1971 if (keypress
.equals(kbF12
)) {
1975 if (keypress
.equals(kbShiftF1
)) {
1980 if (type
== DeviceType
.XTERM
) {
1986 if (keypress
.equals(kbShiftF2
)) {
1991 if (type
== DeviceType
.XTERM
) {
1997 if (keypress
.equals(kbShiftF3
)) {
2002 if (type
== DeviceType
.XTERM
) {
2008 if (keypress
.equals(kbShiftF4
)) {
2013 if (type
== DeviceType
.XTERM
) {
2019 if (keypress
.equals(kbShiftF5
)) {
2021 return "\033[15;2~";
2024 if (keypress
.equals(kbShiftF6
)) {
2026 return "\033[17;2~";
2029 if (keypress
.equals(kbShiftF7
)) {
2031 return "\033[18;2~";
2034 if (keypress
.equals(kbShiftF8
)) {
2036 return "\033[19;2~";
2039 if (keypress
.equals(kbShiftF9
)) {
2041 return "\033[20;2~";
2044 if (keypress
.equals(kbShiftF10
)) {
2046 return "\033[21;2~";
2049 if (keypress
.equals(kbShiftF11
)) {
2051 return "\033[23;2~";
2054 if (keypress
.equals(kbShiftF12
)) {
2056 return "\033[24;2~";
2059 if (keypress
.equals(kbCtrlF1
)) {
2064 if (type
== DeviceType
.XTERM
) {
2070 if (keypress
.equals(kbCtrlF2
)) {
2075 if (type
== DeviceType
.XTERM
) {
2081 if (keypress
.equals(kbCtrlF3
)) {
2086 if (type
== DeviceType
.XTERM
) {
2092 if (keypress
.equals(kbCtrlF4
)) {
2097 if (type
== DeviceType
.XTERM
) {
2103 if (keypress
.equals(kbCtrlF5
)) {
2105 return "\033[15;5~";
2108 if (keypress
.equals(kbCtrlF6
)) {
2110 return "\033[17;5~";
2113 if (keypress
.equals(kbCtrlF7
)) {
2115 return "\033[18;5~";
2118 if (keypress
.equals(kbCtrlF8
)) {
2120 return "\033[19;5~";
2123 if (keypress
.equals(kbCtrlF9
)) {
2125 return "\033[20;5~";
2128 if (keypress
.equals(kbCtrlF10
)) {
2130 return "\033[21;5~";
2133 if (keypress
.equals(kbCtrlF11
)) {
2135 return "\033[23;5~";
2138 if (keypress
.equals(kbCtrlF12
)) {
2140 return "\033[24;5~";
2143 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2146 return xtermBuildKeySequence("\033[", '5', '~',
2147 keypress
.isCtrl(), keypress
.isAlt(),
2148 keypress
.isShift());
2154 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2157 return xtermBuildKeySequence("\033[", '6', '~',
2158 keypress
.isCtrl(), keypress
.isAlt(),
2159 keypress
.isShift());
2165 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2168 return xtermBuildKeySequence("\033[", '2', '~',
2169 keypress
.isCtrl(), keypress
.isAlt(),
2170 keypress
.isShift());
2176 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2179 return xtermBuildKeySequence("\033[", '3', '~',
2180 keypress
.isCtrl(), keypress
.isAlt(),
2181 keypress
.isShift());
2183 // Delete sends real delete for VTxxx
2188 if (keypress
.equals(kbEnter
)) {
2192 if (keypress
.equals(kbEsc
)) {
2196 if (keypress
.equals(kbAltEsc
)) {
2200 if (keypress
.equals(kbTab
)) {
2204 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2205 (keypress
.equals(kbShiftTab
))
2215 // Non-alt, non-ctrl characters
2216 if (!keypress
.isFnKey()) {
2217 StringBuilder sb
= new StringBuilder();
2218 sb
.append(keypress
.getChar());
2219 return sb
.toString();
2225 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2228 * @param ch 8-bit character from the remote side
2229 * @param charsetGl character set defined for GL
2230 * @param charsetGr character set defined for GR
2231 * @return character to display on the screen
2233 private char mapCharacterCharset(final char ch
,
2234 final CharacterSet charsetGl
,
2235 final CharacterSet charsetGr
) {
2237 int lookupChar
= ch
;
2238 CharacterSet lookupCharset
= charsetGl
;
2241 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2242 lookupCharset
= charsetGr
;
2246 switch (lookupCharset
) {
2249 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2252 return DECCharacterSets
.UK
[lookupChar
];
2255 return DECCharacterSets
.US_ASCII
[lookupChar
];
2258 return DECCharacterSets
.NL
[lookupChar
];
2261 return DECCharacterSets
.FI
[lookupChar
];
2264 return DECCharacterSets
.FR
[lookupChar
];
2267 return DECCharacterSets
.FR_CA
[lookupChar
];
2270 return DECCharacterSets
.DE
[lookupChar
];
2273 return DECCharacterSets
.IT
[lookupChar
];
2276 return DECCharacterSets
.NO
[lookupChar
];
2279 return DECCharacterSets
.ES
[lookupChar
];
2282 return DECCharacterSets
.SV
[lookupChar
];
2285 return DECCharacterSets
.SWISS
[lookupChar
];
2287 case DEC_SUPPLEMENTAL
:
2288 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2291 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2294 return DECCharacterSets
.US_ASCII
[lookupChar
];
2297 return DECCharacterSets
.US_ASCII
[lookupChar
];
2300 throw new IllegalArgumentException("Invalid character set value: "
2306 * Map an 8-bit byte into a printable character.
2308 * @param ch either 8-bit or Unicode character from the remote side
2309 * @return character to display on the screen
2311 private char mapCharacter(final char ch
) {
2313 // Unicode character, just return it
2317 CharacterSet charsetGl
= currentState
.g0Charset
;
2318 CharacterSet charsetGr
= currentState
.grCharset
;
2320 if (vt52Mode
== true) {
2321 if (shiftOut
== true) {
2322 // Shifted out character, pull from VT52 graphics
2323 charsetGl
= currentState
.g1Charset
;
2324 charsetGr
= CharacterSet
.US
;
2327 charsetGl
= currentState
.g0Charset
;
2328 charsetGr
= CharacterSet
.US
;
2331 // Pull the character
2332 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2336 if (shiftOut
== true) {
2337 // Shifted out character, pull from G1
2338 charsetGl
= currentState
.g1Charset
;
2339 charsetGr
= currentState
.grCharset
;
2341 // Pull the character
2342 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2346 if (singleshift
== Singleshift
.SS2
) {
2348 singleshift
= Singleshift
.NONE
;
2350 // Shifted out character, pull from G2
2351 charsetGl
= currentState
.g2Charset
;
2352 charsetGr
= currentState
.grCharset
;
2356 if (singleshift
== Singleshift
.SS3
) {
2358 singleshift
= Singleshift
.NONE
;
2360 // Shifted out character, pull from G3
2361 charsetGl
= currentState
.g3Charset
;
2362 charsetGr
= currentState
.grCharset
;
2365 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2366 // Check for locking shift
2368 switch (currentState
.glLockshift
) {
2371 throw new IllegalArgumentException("programming bug");
2374 throw new IllegalArgumentException("programming bug");
2377 throw new IllegalArgumentException("programming bug");
2381 charsetGl
= currentState
.g2Charset
;
2386 charsetGl
= currentState
.g3Charset
;
2391 charsetGl
= currentState
.g0Charset
;
2395 switch (currentState
.grLockshift
) {
2398 throw new IllegalArgumentException("programming bug");
2401 throw new IllegalArgumentException("programming bug");
2405 charsetGr
= currentState
.g1Charset
;
2410 charsetGr
= currentState
.g2Charset
;
2415 charsetGr
= currentState
.g3Charset
;
2420 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2427 // Pull the character
2428 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2432 * Scroll the text within a scrolling region up n lines.
2434 * @param regionTop top row of the scrolling region
2435 * @param regionBottom bottom row of the scrolling region
2436 * @param n number of lines to scroll
2438 private void scrollingRegionScrollUp(final int regionTop
,
2439 final int regionBottom
, final int n
) {
2441 if (regionTop
>= regionBottom
) {
2445 // Sanity check: see if there will be any characters left after the
2447 if (regionBottom
+ 1 - regionTop
<= n
) {
2448 // There won't be anything left in the region, so just call
2449 // eraseScreen() and return.
2450 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2454 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2455 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2456 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2458 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2459 - remaining
, regionBottom
+ 1);
2460 display
= new ArrayList
<DisplayLine
>(displayTop
);
2461 display
.addAll(displayMiddle
);
2462 for (int i
= 0; i
< n
; i
++) {
2463 DisplayLine line
= new DisplayLine(currentState
.attr
);
2464 line
.setReverseColor(reverseVideo
);
2467 display
.addAll(displayBottom
);
2469 assert (display
.size() == height
);
2473 * Scroll the text within a scrolling region down n lines.
2475 * @param regionTop top row of the scrolling region
2476 * @param regionBottom bottom row of the scrolling region
2477 * @param n number of lines to scroll
2479 private void scrollingRegionScrollDown(final int regionTop
,
2480 final int regionBottom
, final int n
) {
2482 if (regionTop
>= regionBottom
) {
2486 // Sanity check: see if there will be any characters left after the
2488 if (regionBottom
+ 1 - regionTop
<= n
) {
2489 // There won't be anything left in the region, so just call
2490 // eraseScreen() and return.
2491 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2495 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2496 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2497 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2499 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2500 regionTop
+ remaining
);
2501 display
= new ArrayList
<DisplayLine
>(displayTop
);
2502 for (int i
= 0; i
< n
; i
++) {
2503 DisplayLine line
= new DisplayLine(currentState
.attr
);
2504 line
.setReverseColor(reverseVideo
);
2507 display
.addAll(displayMiddle
);
2508 display
.addAll(displayBottom
);
2510 assert (display
.size() == height
);
2514 * Process a control character.
2516 * @param ch 8-bit character from the remote side
2518 private void handleControlChar(final char ch
) {
2519 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2530 // Transmit the answerback message.
2541 cursorLeft(1, false);
2546 advanceToNextTabStop();
2572 currentState
.glLockshift
= LockshiftMode
.NONE
;
2578 currentState
.glLockshift
= LockshiftMode
.NONE
;
2603 singleshift
= Singleshift
.SS2
;
2608 singleshift
= Singleshift
.SS3
;
2618 * Advance the cursor to the next tab stop.
2620 private void advanceToNextTabStop() {
2621 if (tabStops
.size() == 0) {
2622 // Go to the rightmost column
2623 cursorRight(rightMargin
- currentState
.cursorX
, false);
2626 for (Integer stop
: tabStops
) {
2627 if (stop
> currentState
.cursorX
) {
2628 cursorRight(stop
- currentState
.cursorX
, false);
2633 * We got here, meaning there isn't a tab stop beyond the current
2634 * cursor position. Place the cursor of the right-most edge of the
2637 cursorRight(rightMargin
- currentState
.cursorX
, false);
2641 * Save a character into the collect buffer.
2643 * @param ch character to save
2645 private void collect(final char ch
) {
2646 collectBuffer
.append(ch
);
2650 * Save a byte into the CSI parameters buffer.
2652 * @param ch byte to save
2654 private void param(final byte ch
) {
2655 if (csiParams
.size() == 0) {
2656 csiParams
.add(Integer
.valueOf(0));
2658 Integer x
= csiParams
.get(csiParams
.size() - 1);
2659 if ((ch
>= '0') && (ch
<= '9')) {
2662 csiParams
.set(csiParams
.size() - 1, x
);
2666 csiParams
.add(Integer
.valueOf(0));
2671 * Get a CSI parameter value, with a default.
2673 * @param position parameter index. 0 is the first parameter.
2674 * @param defaultValue value to use if csiParams[position] doesn't exist
2675 * @return parameter value
2677 private int getCsiParam(final int position
, final int defaultValue
) {
2678 if (csiParams
.size() < position
+ 1) {
2679 return defaultValue
;
2681 return csiParams
.get(position
).intValue();
2685 * Get a CSI parameter value, clamped to within min/max.
2687 * @param position parameter index. 0 is the first parameter.
2688 * @param defaultValue value to use if csiParams[position] doesn't exist
2689 * @param minValue minimum value inclusive
2690 * @param maxValue maximum value inclusive
2691 * @return parameter value
2693 private int getCsiParam(final int position
, final int defaultValue
,
2694 final int minValue
, final int maxValue
) {
2696 assert (minValue
<= maxValue
);
2697 int value
= getCsiParam(position
, defaultValue
);
2698 if (value
< minValue
) {
2701 if (value
> maxValue
) {
2708 * Set or unset a toggle.
2710 * @param value true for set ('h'), false for reset ('l')
2712 private void setToggle(final boolean value
) {
2713 boolean decPrivateModeFlag
= false;
2714 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2715 if (collectBuffer
.charAt(i
) == '?') {
2716 decPrivateModeFlag
= true;
2721 for (Integer i
: csiParams
) {
2726 if (decPrivateModeFlag
== true) {
2728 if (value
== true) {
2729 // Use application arrow keys
2730 arrowKeyMode
= ArrowKeyMode
.VT100
;
2732 // Use ANSI arrow keys
2733 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2738 if (decPrivateModeFlag
== true) {
2739 if (value
== false) {
2743 arrowKeyMode
= ArrowKeyMode
.VT52
;
2746 * From the VT102 docs: "You use ANSI mode to select
2747 * most terminal features; the terminal uses the same
2748 * features when it switches to VT52 mode. You
2749 * cannot, however, change most of these features in
2752 * In other words, do not reset any other attributes
2753 * when switching between VT52 submode and ANSI.
2755 * HOWEVER, the real vt100 does switch the character
2756 * set according to Usenet.
2758 currentState
.g0Charset
= CharacterSet
.US
;
2759 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2762 if ((type
== DeviceType
.VT220
)
2763 || (type
== DeviceType
.XTERM
)) {
2765 // VT52 mode is explicitly 7-bit
2767 singleshift
= Singleshift
.NONE
;
2772 if (value
== true) {
2773 // Turn off keyboard
2782 if (decPrivateModeFlag
== true) {
2784 if (value
== true) {
2791 if ((displayListener
!= null)
2792 && (type
== DeviceType
.XTERM
)
2794 // For xterms, reset to the actual width, not 80
2796 width
= displayListener
.getDisplayWidth();
2797 rightMargin
= width
- 1;
2800 width
= rightMargin
+ 1;
2803 // Entire screen is cleared, and scrolling region is
2805 eraseScreen(0, 0, height
- 1, width
- 1, false);
2806 scrollRegionTop
= 0;
2807 scrollRegionBottom
= height
- 1;
2808 // Also home the cursor
2809 cursorPosition(0, 0);
2813 if (decPrivateModeFlag
== true) {
2815 if (value
== true) {
2824 if (value
== true) {
2832 if (decPrivateModeFlag
== true) {
2834 if (value
== true) {
2836 * Set selects reverse screen, a white screen
2837 * background with black characters.
2839 if (reverseVideo
!= true) {
2841 * If in normal video, switch it back
2843 invertDisplayColors();
2845 reverseVideo
= true;
2848 * Reset selects normal screen, a black screen
2849 * background with white characters.
2851 if (reverseVideo
== true) {
2853 * If in reverse video already, switch it back
2855 invertDisplayColors();
2857 reverseVideo
= false;
2862 if (decPrivateModeFlag
== true) {
2864 if (value
== true) {
2865 // Origin is relative to scroll region cursor.
2866 // Cursor can NEVER leave scrolling region.
2867 currentState
.originMode
= true;
2868 cursorPosition(0, 0);
2870 // Origin is absolute to entire screen. Cursor can
2871 // leave the scrolling region via cup() and hvp().
2872 currentState
.originMode
= false;
2873 cursorPosition(0, 0);
2878 if (decPrivateModeFlag
== true) {
2880 if (value
== true) {
2882 currentState
.lineWrap
= true;
2884 // Turn linewrap off
2885 currentState
.lineWrap
= false;
2890 if (decPrivateModeFlag
== true) {
2892 if (value
== true) {
2893 // Keyboard auto-repeat on
2896 // Keyboard auto-repeat off
2902 if (decPrivateModeFlag
== false) {
2904 if (value
== true) {
2914 if (decPrivateModeFlag
== true) {
2920 if (decPrivateModeFlag
== true) {
2926 if (decPrivateModeFlag
== false) {
2928 if (value
== true) {
2930 * Set causes a received linefeed, form feed, or
2931 * vertical tab to move cursor to first column of
2932 * next line. RETURN transmits both a carriage return
2933 * and linefeed. This selection is also called new
2939 * Reset causes a received linefeed, form feed, or
2940 * vertical tab to move cursor to next line in
2941 * current column. RETURN transmits a carriage
2944 newLineMode
= false;
2950 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2951 if (decPrivateModeFlag
== true) {
2953 if (value
== true) {
2955 cursorVisible
= true;
2958 cursorVisible
= false;
2965 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2966 if (decPrivateModeFlag
== true) {
2968 if (value
== true) {
2969 // Select national mode NRC
2972 // Select multi-national mode
2981 if ((type
== DeviceType
.XTERM
)
2982 && (decPrivateModeFlag
== true)
2984 // Mouse: normal tracking mode
2985 if (value
== true) {
2986 mouseProtocol
= MouseProtocol
.NORMAL
;
2988 mouseProtocol
= MouseProtocol
.OFF
;
2994 if ((type
== DeviceType
.XTERM
)
2995 && (decPrivateModeFlag
== true)
2997 // Mouse: normal tracking mode
2998 if (value
== true) {
2999 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
3001 mouseProtocol
= MouseProtocol
.OFF
;
3007 if ((type
== DeviceType
.XTERM
)
3008 && (decPrivateModeFlag
== true)
3010 // Mouse: Any-event tracking mode
3011 if (value
== true) {
3012 mouseProtocol
= MouseProtocol
.ANYEVENT
;
3014 mouseProtocol
= MouseProtocol
.OFF
;
3020 if ((type
== DeviceType
.XTERM
)
3021 && (decPrivateModeFlag
== true)
3023 // Mouse: UTF-8 coordinates
3024 if (value
== true) {
3025 mouseEncoding
= MouseEncoding
.UTF8
;
3027 mouseEncoding
= MouseEncoding
.X10
;
3033 if ((type
== DeviceType
.XTERM
)
3034 && (decPrivateModeFlag
== true)
3036 // Mouse: SGR coordinates
3037 if (value
== true) {
3038 mouseEncoding
= MouseEncoding
.SGR
;
3040 mouseEncoding
= MouseEncoding
.X10
;
3053 * DECSC - Save cursor.
3055 private void decsc() {
3056 savedState
.setTo(currentState
);
3060 * DECRC - Restore cursor.
3062 private void decrc() {
3063 currentState
.setTo(savedState
);
3069 private void ind() {
3070 // Move the cursor and scroll if necessary. If at the bottom line
3071 // already, a scroll up is supposed to be performed.
3072 if (currentState
.cursorY
== scrollRegionBottom
) {
3073 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3075 cursorDown(1, true);
3079 * RI - Reverse index.
3082 // Move the cursor and scroll if necessary. If at the top line
3083 // already, a scroll down is supposed to be performed.
3084 if (currentState
.cursorY
== scrollRegionTop
) {
3085 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3093 private void nel() {
3094 // Move the cursor and scroll if necessary. If at the bottom line
3095 // already, a scroll up is supposed to be performed.
3096 if (currentState
.cursorY
== scrollRegionBottom
) {
3097 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3099 cursorDown(1, true);
3101 // Reset to the beginning of the next line
3102 currentState
.cursorX
= 0;
3106 * DECKPAM - Keypad application mode.
3108 private void deckpam() {
3109 keypadMode
= KeypadMode
.Application
;
3113 * DECKPNM - Keypad numeric mode.
3115 private void deckpnm() {
3116 keypadMode
= KeypadMode
.Numeric
;
3122 * @param n number of spaces to move
3123 * @param honorScrollRegion if true, then do nothing if the cursor is
3124 * outside the scrolling region
3126 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3130 * Special case: if a user moves the cursor from the right margin, we
3131 * have to reset the VT100 right margin flag.
3134 wrapLineFlag
= false;
3137 for (int i
= 0; i
< n
; i
++) {
3138 if (honorScrollRegion
== true) {
3139 // Honor the scrolling region
3140 if ((currentState
.cursorY
< scrollRegionTop
)
3141 || (currentState
.cursorY
> scrollRegionBottom
)
3143 // Outside region, do nothing
3146 // Inside region, go up
3147 top
= scrollRegionTop
;
3149 // Non-scrolling case
3153 if (currentState
.cursorY
> top
) {
3154 currentState
.cursorY
--;
3160 * Move down n spaces.
3162 * @param n number of spaces to move
3163 * @param honorScrollRegion if true, then do nothing if the cursor is
3164 * outside the scrolling region
3166 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3170 * Special case: if a user moves the cursor from the right margin, we
3171 * have to reset the VT100 right margin flag.
3174 wrapLineFlag
= false;
3177 for (int i
= 0; i
< n
; i
++) {
3179 if (honorScrollRegion
== true) {
3180 // Honor the scrolling region
3181 if (currentState
.cursorY
> scrollRegionBottom
) {
3182 // Outside region, do nothing
3185 // Inside region, go down
3186 bottom
= scrollRegionBottom
;
3188 // Non-scrolling case
3189 bottom
= height
- 1;
3192 if (currentState
.cursorY
< bottom
) {
3193 currentState
.cursorY
++;
3199 * Move left n spaces.
3201 * @param n number of spaces to move
3202 * @param honorScrollRegion if true, then do nothing if the cursor is
3203 * outside the scrolling region
3205 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3207 * Special case: if a user moves the cursor from the right margin, we
3208 * have to reset the VT100 right margin flag.
3211 wrapLineFlag
= false;
3214 for (int i
= 0; i
< n
; i
++) {
3215 if (honorScrollRegion
== true) {
3216 // Honor the scrolling region
3217 if ((currentState
.cursorY
< scrollRegionTop
)
3218 || (currentState
.cursorY
> scrollRegionBottom
)
3220 // Outside region, do nothing
3225 if (currentState
.cursorX
> 0) {
3226 currentState
.cursorX
--;
3232 * Move right n spaces.
3234 * @param n number of spaces to move
3235 * @param honorScrollRegion if true, then do nothing if the cursor is
3236 * outside the scrolling region
3238 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3239 int rightMargin
= this.rightMargin
;
3242 * Special case: if a user moves the cursor from the right margin, we
3243 * have to reset the VT100 right margin flag.
3246 wrapLineFlag
= false;
3249 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3250 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3253 for (int i
= 0; i
< n
; i
++) {
3254 if (honorScrollRegion
== true) {
3255 // Honor the scrolling region
3256 if ((currentState
.cursorY
< scrollRegionTop
)
3257 || (currentState
.cursorY
> scrollRegionBottom
)
3259 // Outside region, do nothing
3264 if (currentState
.cursorX
< rightMargin
) {
3265 currentState
.cursorX
++;
3271 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3273 * @param row row to move to
3274 * @param col column to move to
3276 private void cursorPosition(int row
, final int col
) {
3277 int rightMargin
= this.rightMargin
;
3282 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3283 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3286 // Set column number
3287 currentState
.cursorX
= col
;
3289 // Sanity check, bring column back to margin.
3290 if (currentState
.cursorX
> rightMargin
) {
3291 currentState
.cursorX
= rightMargin
;
3295 if (currentState
.originMode
== true) {
3296 row
+= scrollRegionTop
;
3298 if (currentState
.cursorY
< row
) {
3299 cursorDown(row
- currentState
.cursorY
, false);
3300 } else if (currentState
.cursorY
> row
) {
3301 cursorUp(currentState
.cursorY
- row
, false);
3304 wrapLineFlag
= false;
3308 * HTS - Horizontal tabulation set.
3310 private void hts() {
3311 for (Integer stop
: tabStops
) {
3312 if (stop
== currentState
.cursorX
) {
3313 // Already have a tab stop here
3318 // Append a tab stop to the end of the array and resort them
3319 tabStops
.add(currentState
.cursorX
);
3320 Collections
.sort(tabStops
);
3324 * DECSWL - Single-width line.
3326 private void decswl() {
3327 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3328 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3332 * DECDWL - Double-width line.
3334 private void decdwl() {
3335 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3336 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3340 * DECHDL - Double-height + double-width line.
3342 * @param topHalf if true, this sets the row to be the top half row of a
3345 private void dechdl(final boolean topHalf
) {
3346 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3347 if (topHalf
== true) {
3348 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3350 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3355 * DECALN - Screen alignment display.
3357 private void decaln() {
3358 Cell newCell
= new Cell();
3359 newCell
.setChar('E');
3360 for (DisplayLine line
: display
) {
3361 for (int i
= 0; i
< line
.length(); i
++) {
3362 line
.replace(i
, newCell
);
3368 * DECSCL - Compatibility level.
3370 private void decscl() {
3371 int i
= getCsiParam(0, 0);
3372 int j
= getCsiParam(1, 0);
3376 currentState
.g0Charset
= CharacterSet
.US
;
3377 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3379 } else if (i
== 62) {
3381 if ((j
== 0) || (j
== 2)) {
3383 } else if (j
== 1) {
3390 * CUD - Cursor down.
3392 private void cud() {
3393 cursorDown(getCsiParam(0, 1, 1, height
), true);
3397 * CUF - Cursor forward.
3399 private void cuf() {
3400 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3404 * CUB - Cursor backward.
3406 private void cub() {
3407 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3413 private void cuu() {
3414 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3418 * CUP - Cursor position.
3420 private void cup() {
3421 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3422 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3426 * CNL - Cursor down and to column 1.
3428 private void cnl() {
3429 cursorDown(getCsiParam(0, 1, 1, height
), true);
3431 cursorLeft(currentState
.cursorX
, true);
3435 * CPL - Cursor up and to column 1.
3437 private void cpl() {
3438 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3440 cursorLeft(currentState
.cursorX
, true);
3444 * CHA - Cursor to column # in current row.
3446 private void cha() {
3447 cursorPosition(currentState
.cursorY
,
3448 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3452 * VPA - Cursor to row #, same column.
3454 private void vpa() {
3455 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3456 currentState
.cursorX
);
3460 * ED - Erase in display.
3463 boolean honorProtected
= false;
3464 boolean decPrivateModeFlag
= false;
3466 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3467 if (collectBuffer
.charAt(i
) == '?') {
3468 decPrivateModeFlag
= true;
3473 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3474 && (decPrivateModeFlag
== true)
3476 honorProtected
= true;
3479 int i
= getCsiParam(0, 0);
3482 // Erase from here to end of screen
3483 if (currentState
.cursorY
< height
- 1) {
3484 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3487 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3488 } else if (i
== 1) {
3489 // Erase from beginning of screen to here
3490 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3492 eraseLine(0, currentState
.cursorX
, honorProtected
);
3493 } else if (i
== 2) {
3494 // Erase entire screen
3495 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3500 * EL - Erase in line.
3503 boolean honorProtected
= false;
3504 boolean decPrivateModeFlag
= false;
3506 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3507 if (collectBuffer
.charAt(i
) == '?') {
3508 decPrivateModeFlag
= true;
3513 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3514 && (decPrivateModeFlag
== true)
3516 honorProtected
= true;
3519 int i
= getCsiParam(0, 0);
3522 // Erase from here to end of line
3523 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3524 } else if (i
== 1) {
3525 // Erase from beginning of line to here
3526 eraseLine(0, currentState
.cursorX
, honorProtected
);
3527 } else if (i
== 2) {
3528 // Erase entire line
3529 eraseLine(0, width
- 1, honorProtected
);
3534 * ECH - Erase # of characters in current row.
3536 private void ech() {
3537 int i
= getCsiParam(0, 1, 1, width
);
3539 // Erase from here to i characters
3540 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3547 int i
= getCsiParam(0, 1);
3549 if ((currentState
.cursorY
>= scrollRegionTop
)
3550 && (currentState
.cursorY
<= scrollRegionBottom
)
3553 // I can get the same effect with a scroll-down
3554 scrollingRegionScrollDown(currentState
.cursorY
,
3555 scrollRegionBottom
, i
);
3560 * DCH - Delete char.
3562 private void dch() {
3563 int n
= getCsiParam(0, 1);
3564 DisplayLine line
= display
.get(currentState
.cursorY
);
3565 Cell blank
= new Cell();
3566 for (int i
= 0; i
< n
; i
++) {
3567 line
.delete(currentState
.cursorX
, blank
);
3572 * ICH - Insert blank char at cursor.
3574 private void ich() {
3575 int n
= getCsiParam(0, 1);
3576 DisplayLine line
= display
.get(currentState
.cursorY
);
3577 Cell blank
= new Cell();
3578 for (int i
= 0; i
< n
; i
++) {
3579 line
.insert(currentState
.cursorX
, blank
);
3587 int i
= getCsiParam(0, 1);
3589 if ((currentState
.cursorY
>= scrollRegionTop
)
3590 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3592 // I can get the same effect with a scroll-down
3593 scrollingRegionScrollUp(currentState
.cursorY
,
3594 scrollRegionBottom
, i
);
3599 * HVP - Horizontal and vertical position.
3601 private void hvp() {
3606 * REP - Repeat character.
3608 private void rep() {
3609 int n
= getCsiParam(0, 1);
3610 for (int i
= 0; i
< n
; i
++) {
3611 printCharacter(repCh
);
3619 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3620 getCsiParam(0, 1, 1, height
));
3627 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3628 getCsiParam(0, 1, 1, height
));
3632 * CBT - Go back X tab stops.
3634 private void cbt() {
3635 int tabsToMove
= getCsiParam(0, 1);
3638 for (int i
= 0; i
< tabsToMove
; i
++) {
3639 int j
= currentState
.cursorX
;
3640 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3641 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3649 j
= tabStops
.get(tabI
);
3651 cursorPosition(currentState
.cursorY
, j
);
3656 * CHT - Advance X tab stops.
3658 private void cht() {
3659 int n
= getCsiParam(0, 1);
3660 for (int i
= 0; i
< n
; i
++) {
3661 advanceToNextTabStop();
3666 * SGR - Select graphics rendition.
3668 private void sgr() {
3670 if (csiParams
.size() == 0) {
3671 currentState
.attr
.reset();
3675 int sgrColorMode
= -1;
3676 boolean idx88Color
= false;
3677 boolean rgbColor
= false;
3681 for (Integer i
: csiParams
) {
3683 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
3685 assert (type
== DeviceType
.XTERM
);
3689 * Indexed color mode, we now have the index number.
3691 if (sgrColorMode
== 38) {
3692 currentState
.attr
.setForeColorRGB(get88Color(i
));
3694 assert (sgrColorMode
== 48);
3695 currentState
.attr
.setBackColorRGB(get88Color(i
));
3704 * RGB color mode, we are collecting tokens.
3708 } else if (rgbGreen
== -1) {
3709 rgbGreen
= i
& 0xFF;
3711 int rgb
= rgbRed
<< 16;
3712 rgb
|= rgbGreen
<< 8;
3715 // System.err.printf("RGB: %08x\n", rgb);
3717 if (sgrColorMode
== 38) {
3718 currentState
.attr
.setForeColorRGB(rgb
);
3720 assert (sgrColorMode
== 48);
3721 currentState
.attr
.setBackColorRGB(rgb
);
3742 * Indexed color mode.
3749 * This is neither indexed nor RGB color. Bail out.
3754 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
3760 currentState
.attr
.reset();
3765 currentState
.attr
.setBold(true);
3770 currentState
.attr
.setUnderline(true);
3775 currentState
.attr
.setBlink(true);
3780 currentState
.attr
.setReverse(true);
3787 if (type
== DeviceType
.XTERM
) {
3797 // Set black foreground
3798 currentState
.attr
.setForeColorRGB(get88Color(8));
3801 // Set red foreground
3802 currentState
.attr
.setForeColorRGB(get88Color(9));
3805 // Set green foreground
3806 currentState
.attr
.setForeColorRGB(get88Color(10));
3809 // Set yellow foreground
3810 currentState
.attr
.setForeColorRGB(get88Color(11));
3813 // Set blue foreground
3814 currentState
.attr
.setForeColorRGB(get88Color(12));
3817 // Set magenta foreground
3818 currentState
.attr
.setForeColorRGB(get88Color(13));
3821 // Set cyan foreground
3822 currentState
.attr
.setForeColorRGB(get88Color(14));
3825 // Set white foreground
3826 currentState
.attr
.setForeColorRGB(get88Color(15));
3830 // Set black background
3831 currentState
.attr
.setBackColorRGB(get88Color(8));
3834 // Set red background
3835 currentState
.attr
.setBackColorRGB(get88Color(9));
3838 // Set green background
3839 currentState
.attr
.setBackColorRGB(get88Color(10));
3842 // Set yellow background
3843 currentState
.attr
.setBackColorRGB(get88Color(11));
3846 // Set blue background
3847 currentState
.attr
.setBackColorRGB(get88Color(12));
3850 // Set magenta background
3851 currentState
.attr
.setBackColorRGB(get88Color(13));
3854 // Set cyan background
3855 currentState
.attr
.setBackColorRGB(get88Color(14));
3858 // Set white background
3859 currentState
.attr
.setBackColorRGB(get88Color(15));
3867 if ((type
== DeviceType
.VT220
)
3868 || (type
== DeviceType
.XTERM
)) {
3874 currentState
.attr
.setBold(false);
3879 currentState
.attr
.setUnderline(false);
3884 currentState
.attr
.setBlink(false);
3889 currentState
.attr
.setReverse(false);
3897 // A true VT100/102/220 does not support color, however everyone
3898 // is used to their terminal emulator supporting color so we will
3899 // unconditionally support color for all DeviceType's.
3904 // Set black foreground
3905 currentState
.attr
.setForeColor(Color
.BLACK
);
3906 currentState
.attr
.setForeColorRGB(-1);
3909 // Set red foreground
3910 currentState
.attr
.setForeColor(Color
.RED
);
3911 currentState
.attr
.setForeColorRGB(-1);
3914 // Set green foreground
3915 currentState
.attr
.setForeColor(Color
.GREEN
);
3916 currentState
.attr
.setForeColorRGB(-1);
3919 // Set yellow foreground
3920 currentState
.attr
.setForeColor(Color
.YELLOW
);
3921 currentState
.attr
.setForeColorRGB(-1);
3924 // Set blue foreground
3925 currentState
.attr
.setForeColor(Color
.BLUE
);
3926 currentState
.attr
.setForeColorRGB(-1);
3929 // Set magenta foreground
3930 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3931 currentState
.attr
.setForeColorRGB(-1);
3934 // Set cyan foreground
3935 currentState
.attr
.setForeColor(Color
.CYAN
);
3936 currentState
.attr
.setForeColorRGB(-1);
3939 // Set white foreground
3940 currentState
.attr
.setForeColor(Color
.WHITE
);
3941 currentState
.attr
.setForeColorRGB(-1);
3944 if (type
== DeviceType
.XTERM
) {
3946 * Xterm supports T.416 / ISO-8613-3 codes to select
3947 * either an indexed color or an RGB value. (It also
3948 * permits these ISO-8613-3 SGR sequences to be separated
3949 * by colons rather than semicolons.)
3951 * We will support only the following:
3953 * 1. Indexed color mode (88- or 256-color modes).
3957 * These cover most of the use cases in the real world.
3959 * HOWEVER, note that this is an awful broken "standard",
3960 * with no way to do it "right". See
3961 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
3962 * for a detailed discussion of the current state of RGB
3963 * in various terminals, the point of which is that none
3964 * of them really do the same thing despite all appearing
3968 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
3969 * where it is assumed that supporting just the "indexed
3970 * mode" of these sequences (which could align easily
3971 * with existing SGR colors) is assumed to mean full
3972 * support of 24-bit RGB. So it is all or nothing.
3974 * Finally, these sequences break the assumptions of
3975 * standard ECMA-48 style parsers as pointed out at
3976 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
3977 * Therefore in order to keep a clean display, we cannot
3978 * parse anything else in this sequence.
3983 // Underscore on, default foreground color
3984 currentState
.attr
.setUnderline(true);
3985 currentState
.attr
.setForeColor(Color
.WHITE
);
3989 // Underscore off, default foreground color
3990 currentState
.attr
.setUnderline(false);
3991 currentState
.attr
.setForeColor(Color
.WHITE
);
3992 currentState
.attr
.setForeColorRGB(-1);
3995 // Set black background
3996 currentState
.attr
.setBackColor(Color
.BLACK
);
3997 currentState
.attr
.setBackColorRGB(-1);
4000 // Set red background
4001 currentState
.attr
.setBackColor(Color
.RED
);
4002 currentState
.attr
.setBackColorRGB(-1);
4005 // Set green background
4006 currentState
.attr
.setBackColor(Color
.GREEN
);
4007 currentState
.attr
.setBackColorRGB(-1);
4010 // Set yellow background
4011 currentState
.attr
.setBackColor(Color
.YELLOW
);
4012 currentState
.attr
.setBackColorRGB(-1);
4015 // Set blue background
4016 currentState
.attr
.setBackColor(Color
.BLUE
);
4017 currentState
.attr
.setBackColorRGB(-1);
4020 // Set magenta background
4021 currentState
.attr
.setBackColor(Color
.MAGENTA
);
4022 currentState
.attr
.setBackColorRGB(-1);
4025 // Set cyan background
4026 currentState
.attr
.setBackColor(Color
.CYAN
);
4027 currentState
.attr
.setBackColorRGB(-1);
4030 // Set white background
4031 currentState
.attr
.setBackColor(Color
.WHITE
);
4032 currentState
.attr
.setBackColorRGB(-1);
4035 if (type
== DeviceType
.XTERM
) {
4037 * Xterm supports T.416 / ISO-8613-3 codes to select
4038 * either an indexed color or an RGB value. (It also
4039 * permits these ISO-8613-3 SGR sequences to be separated
4040 * by colons rather than semicolons.)
4042 * We will support only the following:
4044 * 1. Indexed color mode (88- or 256-color modes).
4048 * These cover most of the use cases in the real world.
4050 * HOWEVER, note that this is an awful broken "standard",
4051 * with no way to do it "right". See
4052 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4053 * for a detailed discussion of the current state of RGB
4054 * in various terminals, the point of which is that none
4055 * of them really do the same thing despite all appearing
4059 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4060 * where it is assumed that supporting just the "indexed
4061 * mode" of these sequences (which could align easily
4062 * with existing SGR colors) is assumed to mean full
4063 * support of 24-bit RGB. So it is all or nothing.
4065 * Finally, these sequences break the assumptions of
4066 * standard ECMA-48 style parsers as pointed out at
4067 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4068 * Therefore in order to keep a clean display, we cannot
4069 * parse anything else in this sequence.
4076 // Default background
4077 currentState
.attr
.setBackColor(Color
.BLACK
);
4078 currentState
.attr
.setBackColorRGB(-1);
4088 * DA - Device attributes.
4091 int extendedFlag
= 0;
4093 if (collectBuffer
.length() > 0) {
4094 String args
= collectBuffer
.substring(1);
4095 if (collectBuffer
.charAt(0) == '>') {
4097 if (collectBuffer
.length() >= 2) {
4098 i
= Integer
.parseInt(args
.toString());
4100 } else if (collectBuffer
.charAt(0) == '=') {
4102 if (collectBuffer
.length() >= 2) {
4103 i
= Integer
.parseInt(args
.toString());
4106 // Unknown code, bail out
4111 if ((i
!= 0) && (i
!= 1)) {
4115 if ((extendedFlag
== 0) && (i
== 0)) {
4116 // Send string directly to remote side
4117 writeRemote(deviceTypeResponse());
4121 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4123 if ((extendedFlag
== 1) && (i
== 0)) {
4125 * Request "What type of terminal are you, what is your
4126 * firmware version, and what hardware options do you have
4129 * Respond: "I am a VT220 (identification code of 1), my
4130 * firmware version is _____ (Pv), and I have _____ Po
4131 * options installed."
4137 if (s8c1t
== true) {
4138 writeRemote("\u009b>1;10;0c");
4140 writeRemote("\033[>1;10;0c");
4146 if ((extendedFlag
== 2) && (i
== 0)) {
4149 * Request "What is your unit ID?"
4151 * Respond: "I was manufactured at site 00 and have a unique ID
4155 writeRemote("\033P!|00010203\033\\");
4160 * DECSTBM - Set top and bottom margins.
4162 private void decstbm() {
4163 boolean decPrivateModeFlag
= false;
4165 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4166 if (collectBuffer
.charAt(i
) == '?') {
4167 decPrivateModeFlag
= true;
4171 if (decPrivateModeFlag
) {
4172 // This could be restore DEC private mode values.
4176 int top
= getCsiParam(0, 1, 1, height
) - 1;
4177 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4182 scrollRegionTop
= top
;
4183 scrollRegionBottom
= bottom
;
4186 cursorPosition(0, 0);
4191 * DECREQTPARM - Request terminal parameters.
4193 private void decreqtparm() {
4194 int i
= getCsiParam(0, 0);
4196 if ((i
!= 0) && (i
!= 1)) {
4203 * Request terminal parameters.
4207 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4208 * (CLoCk MULtiplier = 1, STP option flags = 0)
4212 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4215 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4217 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4223 * DECSCA - Select Character Attributes.
4225 private void decsca() {
4226 int i
= getCsiParam(0, 0);
4228 if ((i
== 0) || (i
== 2)) {
4230 currentState
.attr
.setProtect(false);
4234 currentState
.attr
.setProtect(true);
4239 * DECSTR - Soft Terminal Reset.
4241 private void decstr() {
4242 // Do exactly like RIS - Reset to initial state
4244 // Do I clear screen too? I think so...
4245 eraseScreen(0, 0, height
- 1, width
- 1, false);
4246 cursorPosition(0, 0);
4250 * DSR - Device status report.
4252 private void dsr() {
4253 boolean decPrivateModeFlag
= false;
4254 int row
= currentState
.cursorY
;
4256 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4257 if (collectBuffer
.charAt(i
) == '?') {
4258 decPrivateModeFlag
= true;
4263 int i
= getCsiParam(0, 0);
4268 // Request status report. Respond with "OK, no malfunction."
4270 // Send string directly to remote side
4271 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4274 writeRemote("\u009b0n");
4276 writeRemote("\033[0n");
4281 // Request cursor position. Respond with current position.
4282 if (currentState
.originMode
== true) {
4283 row
-= scrollRegionTop
;
4286 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4289 str
= String
.format("\u009b%d;%dR", row
+ 1,
4290 currentState
.cursorX
+ 1);
4292 str
= String
.format("\033[%d;%dR", row
+ 1,
4293 currentState
.cursorX
+ 1);
4296 // Send string directly to remote side
4301 if (decPrivateModeFlag
== true) {
4303 // Request printer status report. Respond with "Printer not
4306 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4307 && (s8c1t
== true)) {
4308 writeRemote("\u009b?13n");
4310 writeRemote("\033[?13n");
4316 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4317 && (decPrivateModeFlag
== true)
4320 // Request user-defined keys are locked or unlocked. Respond
4321 // with "User-defined keys are locked."
4323 if (s8c1t
== true) {
4324 writeRemote("\u009b?21n");
4326 writeRemote("\033[?21n");
4332 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4333 && (decPrivateModeFlag
== true)
4336 // Request keyboard language. Respond with "Keyboard
4337 // language is North American."
4339 if (s8c1t
== true) {
4340 writeRemote("\u009b?27;1n");
4342 writeRemote("\033[?27;1n");
4349 // Some other option, ignore
4355 * TBC - Tabulation clear.
4357 private void tbc() {
4358 int i
= getCsiParam(0, 0);
4360 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4361 for (Integer stop
: tabStops
) {
4362 if (stop
== currentState
.cursorX
) {
4367 tabStops
= newStops
;
4375 * Erase the characters in the current line from the start column to the
4376 * end column, inclusive.
4378 * @param start starting column to erase (between 0 and width - 1)
4379 * @param end ending column to erase (between 0 and width - 1)
4380 * @param honorProtected if true, do not erase characters with the
4381 * protected attribute set
4383 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4388 if (end
> width
- 1) {
4395 for (int i
= start
; i
<= end
; i
++) {
4396 DisplayLine line
= display
.get(currentState
.cursorY
);
4397 if ((!honorProtected
)
4398 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4405 * From the VT102 manual:
4407 * Erasing a character also erases any character
4408 * attribute of the character.
4414 * Erase with the current color a.k.a. back-color erase
4417 line
.setChar(i
, ' ');
4418 line
.setAttr(i
, currentState
.attr
);
4426 * Erase a rectangular section of the screen, inclusive. end column,
4429 * @param startRow starting row to erase (between 0 and height - 1)
4430 * @param startCol starting column to erase (between 0 and width - 1)
4431 * @param endRow ending row to erase (between 0 and height - 1)
4432 * @param endCol ending column to erase (between 0 and width - 1)
4433 * @param honorProtected if true, do not erase characters with the
4434 * protected attribute set
4436 private void eraseScreen(final int startRow
, final int startCol
,
4437 final int endRow
, final int endCol
, final boolean honorProtected
) {
4445 || (endRow
< startRow
)
4446 || (endCol
< startCol
)
4451 oldCursorY
= currentState
.cursorY
;
4452 for (int i
= startRow
; i
<= endRow
; i
++) {
4453 currentState
.cursorY
= i
;
4454 eraseLine(startCol
, endCol
, honorProtected
);
4456 // Erase display clears the double attributes
4457 display
.get(i
).setDoubleWidth(false);
4458 display
.get(i
).setDoubleHeight(0);
4460 currentState
.cursorY
= oldCursorY
;
4464 * VT220 printer functions. All of these are parsed, but won't do
4467 private void printerFunctions() {
4468 boolean decPrivateModeFlag
= false;
4469 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4470 if (collectBuffer
.charAt(i
) == '?') {
4471 decPrivateModeFlag
= true;
4476 int i
= getCsiParam(0, 0);
4481 if (decPrivateModeFlag
== false) {
4487 if (decPrivateModeFlag
== true) {
4488 // Print cursor line
4493 if (decPrivateModeFlag
== true) {
4494 // Auto print mode OFF
4496 // Printer controller OFF
4498 // Characters re-appear on the screen
4499 printerControllerMode
= false;
4504 if (decPrivateModeFlag
== true) {
4508 // Printer controller
4510 // Characters get sucked into oblivion
4511 printerControllerMode
= true;
4522 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4523 * of remote systems will send an XTerm title sequence even if TERM isn't
4526 * @param xtermChar the character received from the remote side
4528 private void oscPut(final char xtermChar
) {
4529 // System.err.println("oscPut: " + xtermChar);
4532 collectBuffer
.append(xtermChar
);
4535 if ((xtermChar
== 0x07)
4536 || (collectBuffer
.toString().endsWith("\033\\"))
4539 if (xtermChar
== 0x07) {
4540 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
4542 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4545 String
[] p
= args
.toString().split(";");
4547 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4554 if (p
[0].equals("4")) {
4555 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
4556 // Set a color index value
4558 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
4559 } catch (NumberFormatException e
) {
4566 // Go to SCAN_GROUND state
4573 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
4574 * Jexer ECMA48Terminal to talk to ECMA48.
4576 * @param pmChar the character received from the remote side
4578 private void pmPut(final char pmChar
) {
4579 // System.err.println("pmPut: " + pmChar);
4582 collectBuffer
.append(pmChar
);
4585 if (collectBuffer
.toString().endsWith("\033\\")) {
4587 arg
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4589 // System.err.println("arg: '" + arg + "'");
4591 if (arg
.equals("hideMousePointer")) {
4592 hideMousePointer
= true;
4594 if (arg
.equals("showMousePointer")) {
4595 hideMousePointer
= false;
4598 // Go to SCAN_GROUND state
4605 * Run this input character through the ECMA48 state machine.
4607 * @param ch character from the remote side
4609 private void consume(char ch
) {
4612 // System.err.printf("%c", ch);
4614 // Special case for VT10x: 7-bit characters only
4615 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4616 ch
= (char)(ch
& 0x7F);
4619 // Special "anywhere" states
4621 // 18, 1A --> execute, then switch to SCAN_GROUND
4622 if ((ch
== 0x18) || (ch
== 0x1A)) {
4623 // CAN and SUB abort escape sequences
4628 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4632 if ((type
== DeviceType
.XTERM
)
4633 && ((scanState
== ScanState
.OSC_STRING
)
4634 || (scanState
== ScanState
.SOSPMAPC_STRING
))
4636 // Xterm can pass ESCAPE to its OSC sequence.
4637 // Jexer can pass ESCAPE to its PM sequence.
4638 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
4639 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4640 && (scanState
!= ScanState
.DCS_IGNORE
)
4641 && (scanState
!= ScanState
.DCS_PARAM
)
4642 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4645 scanState
= ScanState
.ESCAPE
;
4650 // 0x9B == CSI 8-bit sequence
4652 scanState
= ScanState
.CSI_ENTRY
;
4656 // 0x9D goes to ScanState.OSC_STRING
4658 scanState
= ScanState
.OSC_STRING
;
4662 // 0x90 goes to DCS_ENTRY
4664 scanState
= ScanState
.DCS_ENTRY
;
4668 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4669 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4670 scanState
= ScanState
.SOSPMAPC_STRING
;
4674 // 0x7F (DEL) is always discarded
4679 switch (scanState
) {
4682 // 00-17, 19, 1C-1F --> execute
4683 // 80-8F, 91-9A, 9C --> execute
4684 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4685 handleControlChar(ch
);
4689 if (((ch
>= 0x20) && (ch
<= 0x7F))
4693 // VT220 printer --> trash bin
4694 if (((type
== DeviceType
.VT220
)
4695 || (type
== DeviceType
.XTERM
))
4696 && (printerControllerMode
== true)
4701 // Hang onto this character
4702 repCh
= mapCharacter(ch
);
4704 // Print this character
4705 printCharacter(repCh
);
4710 // 00-17, 19, 1C-1F --> execute
4712 handleControlChar(ch
);
4716 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4717 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4719 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4723 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4724 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4735 // DECSC - Save cursor
4736 // Note this code overlaps both ANSI and VT52 mode
4741 // DECRC - Restore cursor
4742 // Note this code overlaps both ANSI and VT52 mode
4751 if (vt52Mode
== true) {
4752 // DECANM - Enter ANSI mode
4754 arrowKeyMode
= ArrowKeyMode
.VT100
;
4757 * From the VT102 docs: "You use ANSI mode to select
4758 * most terminal features; the terminal uses the same
4759 * features when it switches to VT52 mode. You
4760 * cannot, however, change most of these features in
4763 * In other words, do not reset any other attributes
4764 * when switching between VT52 submode and ANSI.
4768 currentState
.g0Charset
= CharacterSet
.US
;
4769 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4771 singleshift
= Singleshift
.NONE
;
4772 currentState
.glLockshift
= LockshiftMode
.NONE
;
4773 currentState
.grLockshift
= LockshiftMode
.NONE
;
4777 // DECKPAM - Keypad application mode
4778 // Note this code overlaps both ANSI and VT52 mode
4782 // DECKPNM - Keypad numeric mode
4783 // Note this code overlaps both ANSI and VT52 mode
4790 if (vt52Mode
== true) {
4791 // Cursor up, and stop at the top without scrolling
4796 if (vt52Mode
== true) {
4797 // Cursor down, and stop at the bottom without scrolling
4798 cursorDown(1, false);
4802 if (vt52Mode
== true) {
4803 // Cursor right, and stop at the right without scrolling
4804 cursorRight(1, false);
4808 if (vt52Mode
== true) {
4809 // Cursor left, and stop at the left without scrolling
4810 cursorLeft(1, false);
4817 if (vt52Mode
== true) {
4825 if (vt52Mode
== true) {
4826 // G0 --> Special graphics
4827 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
4831 if (vt52Mode
== true) {
4833 currentState
.g0Charset
= CharacterSet
.US
;
4837 if (vt52Mode
== true) {
4839 cursorPosition(0, 0);
4841 // HTS - Horizontal tabulation set
4846 if (vt52Mode
== true) {
4847 // Reverse line feed. Same as RI.
4852 if (vt52Mode
== true) {
4853 // Erase to end of screen
4854 eraseLine(currentState
.cursorX
, width
- 1, false);
4855 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4860 if (vt52Mode
== true) {
4861 // Erase to end of line
4862 eraseLine(currentState
.cursorX
, width
- 1, false);
4868 if (vt52Mode
== true) {
4871 // RI - Reverse index
4876 if (vt52Mode
== false) {
4878 singleshift
= Singleshift
.SS2
;
4882 if (vt52Mode
== false) {
4884 singleshift
= Singleshift
.SS3
;
4891 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4907 if (vt52Mode
== true) {
4908 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4916 if (vt52Mode
== true) {
4918 // Send string directly to remote side
4919 writeRemote("\033/Z");
4922 // Send string directly to remote side
4923 writeRemote(deviceTypeResponse());
4934 // VT52 cannot get to any of these other states
4935 if (vt52Mode
== true) {
4940 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4947 // RIS - Reset to initial state
4949 // Do I clear screen too? I think so...
4950 eraseScreen(0, 0, height
- 1, width
- 1, false);
4951 cursorPosition(0, 0);
4965 if ((type
== DeviceType
.VT220
)
4966 || (type
== DeviceType
.XTERM
)) {
4968 // VT220 lockshift G2 into GL
4969 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4974 if ((type
== DeviceType
.VT220
)
4975 || (type
== DeviceType
.XTERM
)) {
4977 // VT220 lockshift G3 into GL
4978 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4996 if ((type
== DeviceType
.VT220
)
4997 || (type
== DeviceType
.XTERM
)) {
4999 // VT220 lockshift G3 into GR
5000 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
5005 if ((type
== DeviceType
.VT220
)
5006 || (type
== DeviceType
.XTERM
)) {
5008 // VT220 lockshift G2 into GR
5009 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
5015 if ((type
== DeviceType
.VT220
)
5016 || (type
== DeviceType
.XTERM
)) {
5018 // VT220 lockshift G1 into GR
5019 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
5029 // 0x5B goes to CSI_ENTRY
5031 scanState
= ScanState
.CSI_ENTRY
;
5034 // 0x5D goes to OSC_STRING
5036 scanState
= ScanState
.OSC_STRING
;
5039 // 0x50 goes to DCS_ENTRY
5041 scanState
= ScanState
.DCS_ENTRY
;
5044 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5045 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
5046 scanState
= ScanState
.SOSPMAPC_STRING
;
5051 case ESCAPE_INTERMEDIATE
:
5052 // 00-17, 19, 1C-1F --> execute
5054 handleControlChar(ch
);
5057 // 20-2F --> collect
5058 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5062 // 30-7E --> dispatch, then switch to GROUND
5063 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
5066 if ((collectBuffer
.length() == 1)
5067 && (collectBuffer
.charAt(0) == '(')) {
5068 // G0 --> Special graphics
5069 currentState
.g0Charset
= CharacterSet
.DRAWING
;
5071 if ((collectBuffer
.length() == 1)
5072 && (collectBuffer
.charAt(0) == ')')) {
5073 // G1 --> Special graphics
5074 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5076 if ((type
== DeviceType
.VT220
)
5077 || (type
== DeviceType
.XTERM
)) {
5079 if ((collectBuffer
.length() == 1)
5080 && (collectBuffer
.charAt(0) == '*')) {
5081 // G2 --> Special graphics
5082 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5084 if ((collectBuffer
.length() == 1)
5085 && (collectBuffer
.charAt(0) == '+')) {
5086 // G3 --> Special graphics
5087 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5092 if ((collectBuffer
.length() == 1)
5093 && (collectBuffer
.charAt(0) == '(')) {
5094 // G0 --> Alternate character ROM standard character set
5095 currentState
.g0Charset
= CharacterSet
.ROM
;
5097 if ((collectBuffer
.length() == 1)
5098 && (collectBuffer
.charAt(0) == ')')) {
5099 // G1 --> Alternate character ROM standard character set
5100 currentState
.g1Charset
= CharacterSet
.ROM
;
5104 if ((collectBuffer
.length() == 1)
5105 && (collectBuffer
.charAt(0) == '(')) {
5106 // G0 --> Alternate character ROM special graphics
5107 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5109 if ((collectBuffer
.length() == 1)
5110 && (collectBuffer
.charAt(0) == ')')) {
5111 // G1 --> Alternate character ROM special graphics
5112 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5116 if ((collectBuffer
.length() == 1)
5117 && (collectBuffer
.charAt(0) == '#')) {
5118 // DECDHL - Double-height line (top half)
5123 if ((collectBuffer
.length() == 1)
5124 && (collectBuffer
.charAt(0) == '#')) {
5125 // DECDHL - Double-height line (bottom half)
5128 if ((type
== DeviceType
.VT220
)
5129 || (type
== DeviceType
.XTERM
)) {
5131 if ((collectBuffer
.length() == 1)
5132 && (collectBuffer
.charAt(0) == '(')) {
5134 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5136 if ((collectBuffer
.length() == 1)
5137 && (collectBuffer
.charAt(0) == ')')) {
5139 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5141 if ((collectBuffer
.length() == 1)
5142 && (collectBuffer
.charAt(0) == '*')) {
5144 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5146 if ((collectBuffer
.length() == 1)
5147 && (collectBuffer
.charAt(0) == '+')) {
5149 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5154 if ((collectBuffer
.length() == 1)
5155 && (collectBuffer
.charAt(0) == '#')) {
5156 // DECSWL - Single-width line
5159 if ((type
== DeviceType
.VT220
)
5160 || (type
== DeviceType
.XTERM
)) {
5162 if ((collectBuffer
.length() == 1)
5163 && (collectBuffer
.charAt(0) == '(')) {
5165 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5167 if ((collectBuffer
.length() == 1)
5168 && (collectBuffer
.charAt(0) == ')')) {
5170 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5172 if ((collectBuffer
.length() == 1)
5173 && (collectBuffer
.charAt(0) == '*')) {
5175 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5177 if ((collectBuffer
.length() == 1)
5178 && (collectBuffer
.charAt(0) == '+')) {
5180 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5185 if ((collectBuffer
.length() == 1)
5186 && (collectBuffer
.charAt(0) == '#')) {
5187 // DECDWL - Double-width line
5190 if ((type
== DeviceType
.VT220
)
5191 || (type
== DeviceType
.XTERM
)) {
5193 if ((collectBuffer
.length() == 1)
5194 && (collectBuffer
.charAt(0) == '(')) {
5196 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5198 if ((collectBuffer
.length() == 1)
5199 && (collectBuffer
.charAt(0) == ')')) {
5201 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5203 if ((collectBuffer
.length() == 1)
5204 && (collectBuffer
.charAt(0) == '*')) {
5206 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5208 if ((collectBuffer
.length() == 1)
5209 && (collectBuffer
.charAt(0) == '+')) {
5211 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5216 if ((type
== DeviceType
.VT220
)
5217 || (type
== DeviceType
.XTERM
)) {
5219 if ((collectBuffer
.length() == 1)
5220 && (collectBuffer
.charAt(0) == '(')) {
5222 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5224 if ((collectBuffer
.length() == 1)
5225 && (collectBuffer
.charAt(0) == ')')) {
5227 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5229 if ((collectBuffer
.length() == 1)
5230 && (collectBuffer
.charAt(0) == '*')) {
5232 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5234 if ((collectBuffer
.length() == 1)
5235 && (collectBuffer
.charAt(0) == '+')) {
5237 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5242 if ((collectBuffer
.length() == 1)
5243 && (collectBuffer
.charAt(0) == '#')) {
5244 // DECALN - Screen alignment display
5253 if ((type
== DeviceType
.VT220
)
5254 || (type
== DeviceType
.XTERM
)) {
5256 if ((collectBuffer
.length() == 1)
5257 && (collectBuffer
.charAt(0) == '(')) {
5258 // G0 --> DEC_SUPPLEMENTAL
5259 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5261 if ((collectBuffer
.length() == 1)
5262 && (collectBuffer
.charAt(0) == ')')) {
5263 // G1 --> DEC_SUPPLEMENTAL
5264 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5266 if ((collectBuffer
.length() == 1)
5267 && (collectBuffer
.charAt(0) == '*')) {
5268 // G2 --> DEC_SUPPLEMENTAL
5269 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5271 if ((collectBuffer
.length() == 1)
5272 && (collectBuffer
.charAt(0) == '+')) {
5273 // G3 --> DEC_SUPPLEMENTAL
5274 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5279 if ((type
== DeviceType
.VT220
)
5280 || (type
== DeviceType
.XTERM
)) {
5282 if ((collectBuffer
.length() == 1)
5283 && (collectBuffer
.charAt(0) == '(')) {
5285 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5287 if ((collectBuffer
.length() == 1)
5288 && (collectBuffer
.charAt(0) == ')')) {
5290 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5292 if ((collectBuffer
.length() == 1)
5293 && (collectBuffer
.charAt(0) == '*')) {
5295 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5297 if ((collectBuffer
.length() == 1)
5298 && (collectBuffer
.charAt(0) == '+')) {
5300 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5309 if ((collectBuffer
.length() == 1)
5310 && (collectBuffer
.charAt(0) == '(')) {
5311 // G0 --> United Kingdom set
5312 currentState
.g0Charset
= CharacterSet
.UK
;
5314 if ((collectBuffer
.length() == 1)
5315 && (collectBuffer
.charAt(0) == ')')) {
5316 // G1 --> United Kingdom set
5317 currentState
.g1Charset
= CharacterSet
.UK
;
5319 if ((type
== DeviceType
.VT220
)
5320 || (type
== DeviceType
.XTERM
)) {
5322 if ((collectBuffer
.length() == 1)
5323 && (collectBuffer
.charAt(0) == '*')) {
5324 // G2 --> United Kingdom set
5325 currentState
.g2Charset
= CharacterSet
.UK
;
5327 if ((collectBuffer
.length() == 1)
5328 && (collectBuffer
.charAt(0) == '+')) {
5329 // G3 --> United Kingdom set
5330 currentState
.g3Charset
= CharacterSet
.UK
;
5335 if ((collectBuffer
.length() == 1)
5336 && (collectBuffer
.charAt(0) == '(')) {
5338 currentState
.g0Charset
= CharacterSet
.US
;
5340 if ((collectBuffer
.length() == 1)
5341 && (collectBuffer
.charAt(0) == ')')) {
5343 currentState
.g1Charset
= CharacterSet
.US
;
5345 if ((type
== DeviceType
.VT220
)
5346 || (type
== DeviceType
.XTERM
)) {
5348 if ((collectBuffer
.length() == 1)
5349 && (collectBuffer
.charAt(0) == '*')) {
5351 currentState
.g2Charset
= CharacterSet
.US
;
5353 if ((collectBuffer
.length() == 1)
5354 && (collectBuffer
.charAt(0) == '+')) {
5356 currentState
.g3Charset
= CharacterSet
.US
;
5361 if ((type
== DeviceType
.VT220
)
5362 || (type
== DeviceType
.XTERM
)) {
5364 if ((collectBuffer
.length() == 1)
5365 && (collectBuffer
.charAt(0) == '(')) {
5367 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5369 if ((collectBuffer
.length() == 1)
5370 && (collectBuffer
.charAt(0) == ')')) {
5372 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5374 if ((collectBuffer
.length() == 1)
5375 && (collectBuffer
.charAt(0) == '*')) {
5377 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5379 if ((collectBuffer
.length() == 1)
5380 && (collectBuffer
.charAt(0) == '+')) {
5382 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5389 if ((type
== DeviceType
.VT220
)
5390 || (type
== DeviceType
.XTERM
)) {
5392 if ((collectBuffer
.length() == 1)
5393 && (collectBuffer
.charAt(0) == '(')) {
5395 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5397 if ((collectBuffer
.length() == 1)
5398 && (collectBuffer
.charAt(0) == ')')) {
5400 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5402 if ((collectBuffer
.length() == 1)
5403 && (collectBuffer
.charAt(0) == '*')) {
5405 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5407 if ((collectBuffer
.length() == 1)
5408 && (collectBuffer
.charAt(0) == '+')) {
5410 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5415 if ((type
== DeviceType
.VT220
)
5416 || (type
== DeviceType
.XTERM
)) {
5418 if ((collectBuffer
.length() == 1)
5419 && (collectBuffer
.charAt(0) == ' ')) {
5426 if ((type
== DeviceType
.VT220
)
5427 || (type
== DeviceType
.XTERM
)) {
5429 if ((collectBuffer
.length() == 1)
5430 && (collectBuffer
.charAt(0) == ' ')) {
5437 if ((type
== DeviceType
.VT220
)
5438 || (type
== DeviceType
.XTERM
)) {
5440 if ((collectBuffer
.length() == 1)
5441 && (collectBuffer
.charAt(0) == '(')) {
5443 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5445 if ((collectBuffer
.length() == 1)
5446 && (collectBuffer
.charAt(0) == ')')) {
5448 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5450 if ((collectBuffer
.length() == 1)
5451 && (collectBuffer
.charAt(0) == '*')) {
5453 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5455 if ((collectBuffer
.length() == 1)
5456 && (collectBuffer
.charAt(0) == '+')) {
5458 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5466 if ((type
== DeviceType
.VT220
)
5467 || (type
== DeviceType
.XTERM
)) {
5469 if ((collectBuffer
.length() == 1)
5470 && (collectBuffer
.charAt(0) == '(')) {
5472 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5474 if ((collectBuffer
.length() == 1)
5475 && (collectBuffer
.charAt(0) == ')')) {
5477 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5479 if ((collectBuffer
.length() == 1)
5480 && (collectBuffer
.charAt(0) == '*')) {
5482 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5484 if ((collectBuffer
.length() == 1)
5485 && (collectBuffer
.charAt(0) == '+')) {
5487 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5498 if ((type
== DeviceType
.VT220
)
5499 || (type
== DeviceType
.XTERM
)) {
5501 if ((collectBuffer
.length() == 1)
5502 && (collectBuffer
.charAt(0) == '(')) {
5504 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5506 if ((collectBuffer
.length() == 1)
5507 && (collectBuffer
.charAt(0) == ')')) {
5509 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5511 if ((collectBuffer
.length() == 1)
5512 && (collectBuffer
.charAt(0) == '*')) {
5514 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5516 if ((collectBuffer
.length() == 1)
5517 && (collectBuffer
.charAt(0) == '+')) {
5519 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5524 if ((type
== DeviceType
.VT220
)
5525 || (type
== DeviceType
.XTERM
)) {
5527 if ((collectBuffer
.length() == 1)
5528 && (collectBuffer
.charAt(0) == '(')) {
5530 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5532 if ((collectBuffer
.length() == 1)
5533 && (collectBuffer
.charAt(0) == ')')) {
5535 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5537 if ((collectBuffer
.length() == 1)
5538 && (collectBuffer
.charAt(0) == '*')) {
5540 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5542 if ((collectBuffer
.length() == 1)
5543 && (collectBuffer
.charAt(0) == '+')) {
5545 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5557 if ((type
== DeviceType
.VT220
)
5558 || (type
== DeviceType
.XTERM
)) {
5560 if ((collectBuffer
.length() == 1)
5561 && (collectBuffer
.charAt(0) == '(')) {
5563 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5565 if ((collectBuffer
.length() == 1)
5566 && (collectBuffer
.charAt(0) == ')')) {
5568 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5570 if ((collectBuffer
.length() == 1)
5571 && (collectBuffer
.charAt(0) == '*')) {
5573 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5575 if ((collectBuffer
.length() == 1)
5576 && (collectBuffer
.charAt(0) == '+')) {
5578 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5583 if ((type
== DeviceType
.VT220
)
5584 || (type
== DeviceType
.XTERM
)) {
5586 if ((collectBuffer
.length() == 1)
5587 && (collectBuffer
.charAt(0) == '(')) {
5589 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5591 if ((collectBuffer
.length() == 1)
5592 && (collectBuffer
.charAt(0) == ')')) {
5594 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5596 if ((collectBuffer
.length() == 1)
5597 && (collectBuffer
.charAt(0) == '*')) {
5599 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5601 if ((collectBuffer
.length() == 1)
5602 && (collectBuffer
.charAt(0) == '+')) {
5604 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5651 // 0x9C goes to GROUND
5659 // 00-17, 19, 1C-1F --> execute
5661 handleControlChar(ch
);
5664 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5665 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5667 scanState
= ScanState
.CSI_INTERMEDIATE
;
5670 // 30-39, 3B --> param, then switch to CSI_PARAM
5671 if ((ch
>= '0') && (ch
<= '9')) {
5673 scanState
= ScanState
.CSI_PARAM
;
5677 scanState
= ScanState
.CSI_PARAM
;
5680 // 3C-3F --> collect, then switch to CSI_PARAM
5681 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5683 scanState
= ScanState
.CSI_PARAM
;
5686 // 40-7E --> dispatch, then switch to GROUND
5687 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5690 // ICH - Insert character
5698 // CUD - Cursor down
5702 // CUF - Cursor forward
5706 // CUB - Cursor backward
5710 // CNL - Cursor down and to column 1
5711 if (type
== DeviceType
.XTERM
) {
5716 // CPL - Cursor up and to column 1
5717 if (type
== DeviceType
.XTERM
) {
5722 // CHA - Cursor to column # in current row
5723 if (type
== DeviceType
.XTERM
) {
5728 // CUP - Cursor position
5732 // CHT - Cursor forward X tab stops (default 1)
5733 if (type
== DeviceType
.XTERM
) {
5738 // ED - Erase in display
5742 // EL - Erase in line
5757 // DCH - Delete character
5764 // Scroll up X lines (default 1)
5765 if (type
== DeviceType
.XTERM
) {
5770 // Scroll down X lines (default 1)
5771 if (type
== DeviceType
.XTERM
) {
5780 if ((type
== DeviceType
.VT220
)
5781 || (type
== DeviceType
.XTERM
)) {
5783 // ECH - Erase character
5790 // CBT - Cursor backward X tab stops (default 1)
5791 if (type
== DeviceType
.XTERM
) {
5802 // HPA - Cursor to column # in current row. Same as CHA
5803 if (type
== DeviceType
.XTERM
) {
5808 // HPR - Cursor right. Same as CUF
5809 if (type
== DeviceType
.XTERM
) {
5814 // REP - Repeat last char X times
5815 if (type
== DeviceType
.XTERM
) {
5820 // DA - Device attributes
5824 // VPA - Cursor to row, current column.
5825 if (type
== DeviceType
.XTERM
) {
5830 // VPR - Cursor down. Same as CUD
5831 if (type
== DeviceType
.XTERM
) {
5836 // HVP - Horizontal and vertical position
5840 // TBC - Tabulation clear
5844 // Sets an ANSI or DEC private toggle
5848 if ((type
== DeviceType
.VT220
)
5849 || (type
== DeviceType
.XTERM
)) {
5851 // Printer functions
5859 // Sets an ANSI or DEC private toggle
5863 // SGR - Select graphics rendition
5867 // DSR - Device status report
5874 // DECLL - Load leds
5878 // DECSTBM - Set top and bottom margins
5882 // Save cursor (ANSI.SYS)
5883 if (type
== DeviceType
.XTERM
) {
5884 savedState
.cursorX
= currentState
.cursorX
;
5885 savedState
.cursorY
= currentState
.cursorY
;
5891 // Restore cursor (ANSI.SYS)
5892 if (type
== DeviceType
.XTERM
) {
5893 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5900 // DECREQTPARM - Request terminal parameters
5916 // 0x9C goes to GROUND
5921 // 0x3A goes to CSI_IGNORE
5923 scanState
= ScanState
.CSI_IGNORE
;
5928 // 00-17, 19, 1C-1F --> execute
5930 handleControlChar(ch
);
5933 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5934 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5936 scanState
= ScanState
.CSI_INTERMEDIATE
;
5939 // 30-39, 3B --> param
5940 if ((ch
>= '0') && (ch
<= '9')) {
5947 // 0x3A goes to CSI_IGNORE
5949 scanState
= ScanState
.CSI_IGNORE
;
5951 // 0x3C-3F goes to CSI_IGNORE
5952 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5953 scanState
= ScanState
.CSI_IGNORE
;
5956 // 40-7E --> dispatch, then switch to GROUND
5957 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5960 // ICH - Insert character
5968 // CUD - Cursor down
5972 // CUF - Cursor forward
5976 // CUB - Cursor backward
5980 // CNL - Cursor down and to column 1
5981 if (type
== DeviceType
.XTERM
) {
5986 // CPL - Cursor up and to column 1
5987 if (type
== DeviceType
.XTERM
) {
5992 // CHA - Cursor to column # in current row
5993 if (type
== DeviceType
.XTERM
) {
5998 // CUP - Cursor position
6002 // CHT - Cursor forward X tab stops (default 1)
6003 if (type
== DeviceType
.XTERM
) {
6008 // ED - Erase in display
6012 // EL - Erase in line
6027 // DCH - Delete character
6034 // Scroll up X lines (default 1)
6035 if (type
== DeviceType
.XTERM
) {
6040 // Scroll down X lines (default 1)
6041 if (type
== DeviceType
.XTERM
) {
6050 if ((type
== DeviceType
.VT220
)
6051 || (type
== DeviceType
.XTERM
)) {
6053 // ECH - Erase character
6060 // CBT - Cursor backward X tab stops (default 1)
6061 if (type
== DeviceType
.XTERM
) {
6072 // HPA - Cursor to column # in current row. Same as CHA
6073 if (type
== DeviceType
.XTERM
) {
6078 // HPR - Cursor right. Same as CUF
6079 if (type
== DeviceType
.XTERM
) {
6084 // REP - Repeat last char X times
6085 if (type
== DeviceType
.XTERM
) {
6090 // DA - Device attributes
6094 // VPA - Cursor to row, current column.
6095 if (type
== DeviceType
.XTERM
) {
6100 // VPR - Cursor down. Same as CUD
6101 if (type
== DeviceType
.XTERM
) {
6106 // HVP - Horizontal and vertical position
6110 // TBC - Tabulation clear
6114 // Sets an ANSI or DEC private toggle
6118 if ((type
== DeviceType
.VT220
)
6119 || (type
== DeviceType
.XTERM
)) {
6121 // Printer functions
6129 // Sets an ANSI or DEC private toggle
6133 // SGR - Select graphics rendition
6137 // DSR - Device status report
6144 // DECLL - Load leds
6148 // DECSTBM - Set top and bottom margins
6158 // DECREQTPARM - Request terminal parameters
6175 case CSI_INTERMEDIATE
:
6176 // 00-17, 19, 1C-1F --> execute
6178 handleControlChar(ch
);
6181 // 20-2F --> collect
6182 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6186 // 0x30-3F goes to CSI_IGNORE
6187 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6188 scanState
= ScanState
.CSI_IGNORE
;
6191 // 40-7E --> dispatch, then switch to GROUND
6192 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6244 if (((type
== DeviceType
.VT220
)
6245 || (type
== DeviceType
.XTERM
))
6246 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6248 // DECSCL - compatibility level
6251 if ((type
== DeviceType
.XTERM
)
6252 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6254 // DECSTR - Soft terminal reset
6259 if (((type
== DeviceType
.VT220
)
6260 || (type
== DeviceType
.XTERM
))
6261 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6289 // 00-17, 19, 1C-1F --> execute
6291 handleControlChar(ch
);
6294 // 20-2F --> collect
6295 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6299 // 40-7E --> ignore, then switch to GROUND
6300 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6304 // 20-3F, 7F --> ignore
6310 // 0x9C goes to GROUND
6315 // 0x1B 0x5C goes to GROUND
6320 if ((collectBuffer
.length() > 0)
6321 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6327 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6328 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6330 scanState
= ScanState
.DCS_INTERMEDIATE
;
6333 // 30-39, 3B --> param, then switch to DCS_PARAM
6334 if ((ch
>= '0') && (ch
<= '9')) {
6336 scanState
= ScanState
.DCS_PARAM
;
6340 scanState
= ScanState
.DCS_PARAM
;
6343 // 3C-3F --> collect, then switch to DCS_PARAM
6344 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6346 scanState
= ScanState
.DCS_PARAM
;
6349 // 00-17, 19, 1C-1F, 7F --> ignore
6351 // 0x3A goes to DCS_IGNORE
6353 scanState
= ScanState
.DCS_IGNORE
;
6356 // 0x40-7E goes to DCS_PASSTHROUGH
6357 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6358 scanState
= ScanState
.DCS_PASSTHROUGH
;
6362 case DCS_INTERMEDIATE
:
6364 // 0x9C goes to GROUND
6369 // 0x1B 0x5C goes to GROUND
6374 if ((collectBuffer
.length() > 0)
6375 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6381 // 0x30-3F goes to DCS_IGNORE
6382 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6383 scanState
= ScanState
.DCS_IGNORE
;
6386 // 0x40-7E goes to DCS_PASSTHROUGH
6387 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6388 scanState
= ScanState
.DCS_PASSTHROUGH
;
6391 // 00-17, 19, 1C-1F, 7F --> ignore
6396 // 0x9C goes to GROUND
6401 // 0x1B 0x5C goes to GROUND
6406 if ((collectBuffer
.length() > 0)
6407 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6413 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6414 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6416 scanState
= ScanState
.DCS_INTERMEDIATE
;
6419 // 30-39, 3B --> param
6420 if ((ch
>= '0') && (ch
<= '9')) {
6427 // 00-17, 19, 1C-1F, 7F --> ignore
6429 // 0x3A, 3C-3F goes to DCS_IGNORE
6431 scanState
= ScanState
.DCS_IGNORE
;
6433 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6434 scanState
= ScanState
.DCS_IGNORE
;
6437 // 0x40-7E goes to DCS_PASSTHROUGH
6438 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6439 scanState
= ScanState
.DCS_PASSTHROUGH
;
6443 case DCS_PASSTHROUGH
:
6444 // 0x9C goes to GROUND
6449 // 0x1B 0x5C goes to GROUND
6454 if ((collectBuffer
.length() > 0)
6455 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6461 // 00-17, 19, 1C-1F, 20-7E --> put
6469 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6472 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6481 // 00-17, 19, 1C-1F, 20-7F --> ignore
6483 // 0x9C goes to GROUND
6490 case SOSPMAPC_STRING
:
6491 // 00-17, 19, 1C-1F, 20-7F --> ignore
6493 // Special case for Jexer: PM can pass one control character
6498 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6502 // 0x9C goes to GROUND
6510 // Special case for Xterm: OSC can pass control characters
6511 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
6515 // 00-17, 19, 1C-1F --> ignore
6517 // 20-7F --> osc_put
6518 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6522 // 0x9C goes to GROUND
6529 case VT52_DIRECT_CURSOR_ADDRESS
:
6530 // This is a special case for the VT52 sequence "ESC Y l c"
6531 if (collectBuffer
.length() == 0) {
6533 } else if (collectBuffer
.length() == 1) {
6534 // We've got the two characters, one in the buffer and the
6536 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
6545 * Expose current cursor X to outside world.
6547 * @return current cursor X
6549 public final int getCursorX() {
6550 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6551 return currentState
.cursorX
* 2;
6553 return currentState
.cursorX
;
6557 * Expose current cursor Y to outside world.
6559 * @return current cursor Y
6561 public final int getCursorY() {
6562 return currentState
.cursorY
;
6566 * Returns true if this terminal has requested the mouse pointer be
6569 * @return true if this terminal has requested the mouse pointer be
6572 public final boolean hasHiddenMousePointer() {
6573 return hideMousePointer
;