2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.tterminal
;
31 import java
.io
.BufferedOutputStream
;
32 import java
.io
.InputStream
;
33 import java
.io
.InputStreamReader
;
34 import java
.io
.IOException
;
35 import java
.io
.OutputStream
;
36 import java
.io
.OutputStreamWriter
;
37 import java
.io
.Reader
;
38 import java
.io
.UnsupportedEncodingException
;
39 import java
.io
.Writer
;
40 import java
.util
.ArrayList
;
41 import java
.util
.Collections
;
42 import java
.util
.LinkedList
;
43 import java
.util
.List
;
45 import jexer
.TKeypress
;
46 import jexer
.event
.TMouseEvent
;
47 import jexer
.bits
.Color
;
48 import jexer
.bits
.Cell
;
49 import jexer
.bits
.CellAttributes
;
50 import jexer
.io
.ReadTimeoutException
;
51 import jexer
.io
.TimeoutInputStream
;
52 import static jexer
.TKeypress
.*;
55 * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
56 * including a scrollback buffer.
59 * It currently implements VT100, VT102, VT220, and XTERM with the following
63 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
64 * failure with XTERM. This is due to vttest failing to decode the UTF-8
68 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
69 * from VT100 are not supported.
72 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
73 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
74 * because DECSCL is not supported, it will fail the last part of the
75 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
78 * - Numeric/application keys from the number pad are not supported because
79 * they are not exposed from the TKeypress API.
82 * - VT52 HOLD SCREEN mode is not supported.
85 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
86 * numerators) are not rendered correctly.
89 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
91 public class ECMA48
implements Runnable
{
93 // ------------------------------------------------------------------------
94 // Constants --------------------------------------------------------------
95 // ------------------------------------------------------------------------
98 * The emulator can emulate several kinds of terminals.
100 public enum DeviceType
{
102 * DEC VT100 but also including the three VT102 functions.
123 * Parser character scan states.
125 private enum ScanState
{
140 VT52_DIRECT_CURSOR_ADDRESS
144 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
145 * can't really use it in keypress() because we do not see number pad
146 * events from TKeypress.
148 private enum KeypadMode
{
154 * Arrow keys can emit three different sequences (DECCKM or VT52
157 private enum ArrowKeyMode
{
164 * Available character sets for GL, GR, G0, G1, G2, G3.
166 private enum CharacterSet
{
187 * Single-shift states used by the C1 control characters SS2 (0x8E) and
190 private enum Singleshift
{
197 * VT220+ lockshift states.
199 private enum LockshiftMode
{
209 * XTERM mouse reporting protocols.
211 private enum MouseProtocol
{
220 * XTERM mouse reporting encodings.
222 private enum MouseEncoding
{
228 // ------------------------------------------------------------------------
229 // Variables --------------------------------------------------------------
230 // ------------------------------------------------------------------------
233 * The enclosing listening object.
235 private DisplayListener displayListener
;
238 * When true, the reader thread is expected to exit.
240 private volatile boolean stopReaderThread
= false;
245 private Thread readerThread
= null;
248 * The type of emulator to be.
250 private DeviceType type
= DeviceType
.VT102
;
253 * The scrollback buffer characters + attributes.
255 private volatile List
<DisplayLine
> scrollback
;
258 * The raw display buffer characters + attributes.
260 private volatile List
<DisplayLine
> display
;
263 * The terminal's input. For type == XTERM, this is an InputStreamReader
264 * with UTF-8 encoding.
266 private Reader input
;
269 * The terminal's raw InputStream. This is used for type != XTERM.
271 private volatile TimeoutInputStream inputStream
;
274 * The terminal's output. For type == XTERM, this wraps an
275 * OutputStreamWriter with UTF-8 encoding.
277 private Writer output
;
280 * The terminal's raw OutputStream. This is used for type != XTERM.
282 private OutputStream outputStream
;
285 * Current scanning state.
287 private ScanState scanState
;
290 * Which mouse protocol is active.
292 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
295 * Which mouse encoding is active.
297 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
300 * Physical display width. We start at 80x24, but the user can resize us
306 * Physical display height. We start at 80x24, but the user can resize
312 * Top margin of the scrolling region.
314 private int scrollRegionTop
;
317 * Bottom margin of the scrolling region.
319 private int scrollRegionBottom
;
322 * Right margin column number. This can be selected by the remote side
323 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
325 private int rightMargin
;
328 * Last character printed.
333 * VT100-style line wrapping: a character is placed in column 80 (or
334 * 132), but the line does NOT wrap until another character is written to
335 * column 1 of the next line, after which the cursor moves to column 2.
337 private boolean wrapLineFlag
;
340 * VT220 single shift flag.
342 private Singleshift singleshift
= Singleshift
.NONE
;
345 * true = insert characters, false = overwrite.
347 private boolean insertMode
= false;
350 * VT52 mode as selected by DECANM. True means VT52, false means
351 * ANSI. Default is ANSI.
353 private boolean vt52Mode
= false;
356 * Visible cursor (DECTCEM).
358 private boolean cursorVisible
= true;
361 * Screen title as set by the xterm OSC sequence. Lots of applications
362 * send a screenTitle regardless of whether it is an xterm client or not.
364 private String screenTitle
= "";
367 * Parameter characters being collected.
369 private List
<Integer
> csiParams
;
372 * Non-csi collect buffer.
374 private StringBuilder collectBuffer
;
377 * When true, use the G1 character set.
379 private boolean shiftOut
= false;
382 * Horizontal tab stop locations.
384 private List
<Integer
> tabStops
;
387 * S8C1T. True means 8bit controls, false means 7bit controls.
389 private boolean s8c1t
= false;
392 * Printer mode. True means send all output to printer, which discards
395 private boolean printerControllerMode
= false;
398 * LMN line mode. If true, linefeed() puts the cursor on the first
399 * column of the next line. If false, linefeed() puts the cursor one
400 * line down on the current line. The default is false.
402 private boolean newLineMode
= false;
405 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
407 private ArrowKeyMode arrowKeyMode
;
410 * Whether number pad keys send VT100 or VT52, application or numeric
413 @SuppressWarnings("unused")
414 private KeypadMode keypadMode
;
417 * When true, the terminal is in 132-column mode (DECCOLM).
419 private boolean columns132
= false;
422 * true = reverse video. Set by DECSCNM.
424 private boolean reverseVideo
= false;
427 * false = echo characters locally.
429 private boolean fullDuplex
= true;
432 * The current terminal state.
434 private SaveableState currentState
;
437 * The last saved terminal state.
439 private SaveableState savedState
;
442 * DECSC/DECRC save/restore a subset of the total state. This class
443 * encapsulates those specific flags/modes.
445 private class SaveableState
{
448 * When true, cursor positions are relative to the scrolling region.
450 public boolean originMode
= false;
453 * The current editing X position.
455 public int cursorX
= 0;
458 * The current editing Y position.
460 public int cursorY
= 0;
463 * Which character set is currently selected in G0.
465 public CharacterSet g0Charset
= CharacterSet
.US
;
468 * Which character set is currently selected in G1.
470 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
473 * Which character set is currently selected in G2.
475 public CharacterSet g2Charset
= CharacterSet
.US
;
478 * Which character set is currently selected in G3.
480 public CharacterSet g3Charset
= CharacterSet
.US
;
483 * Which character set is currently selected in GR.
485 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
488 * The current drawing attributes.
490 public CellAttributes attr
;
495 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
500 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
505 public boolean lineWrap
= true;
510 public void reset() {
514 g0Charset
= CharacterSet
.US
;
515 g1Charset
= CharacterSet
.DRAWING
;
516 g2Charset
= CharacterSet
.US
;
517 g3Charset
= CharacterSet
.US
;
518 grCharset
= CharacterSet
.DRAWING
;
519 attr
= new CellAttributes();
520 glLockshift
= LockshiftMode
.NONE
;
521 grLockshift
= LockshiftMode
.NONE
;
526 * Copy attributes from another instance.
528 * @param that the other instance to match
530 public void setTo(final SaveableState that
) {
531 this.originMode
= that
.originMode
;
532 this.cursorX
= that
.cursorX
;
533 this.cursorY
= that
.cursorY
;
534 this.g0Charset
= that
.g0Charset
;
535 this.g1Charset
= that
.g1Charset
;
536 this.g2Charset
= that
.g2Charset
;
537 this.g3Charset
= that
.g3Charset
;
538 this.grCharset
= that
.grCharset
;
539 this.attr
= new CellAttributes();
540 this.attr
.setTo(that
.attr
);
541 this.glLockshift
= that
.glLockshift
;
542 this.grLockshift
= that
.grLockshift
;
543 this.lineWrap
= that
.lineWrap
;
547 * Public constructor.
549 public SaveableState() {
554 // ------------------------------------------------------------------------
555 // Constructors -----------------------------------------------------------
556 // ------------------------------------------------------------------------
559 * Public constructor.
561 * @param type one of the DeviceType constants to select VT100, VT102,
563 * @param inputStream an InputStream connected to the remote side. For
564 * type == XTERM, inputStream is converted to a Reader with UTF-8
566 * @param outputStream an OutputStream connected to the remote user. For
567 * type == XTERM, outputStream is converted to a Writer with UTF-8
569 * @param displayListener a callback to the outer display, or null for
570 * default VT100 behavior
571 * @throws UnsupportedEncodingException if an exception is thrown when
572 * creating the InputStreamReader
574 public ECMA48(final DeviceType type
, final InputStream inputStream
,
575 final OutputStream outputStream
, final DisplayListener displayListener
)
576 throws UnsupportedEncodingException
{
578 assert (inputStream
!= null);
579 assert (outputStream
!= null);
581 csiParams
= new ArrayList
<Integer
>();
582 tabStops
= new ArrayList
<Integer
>();
583 scrollback
= new LinkedList
<DisplayLine
>();
584 display
= new LinkedList
<DisplayLine
>();
587 if (inputStream
instanceof TimeoutInputStream
) {
588 this.inputStream
= (TimeoutInputStream
)inputStream
;
590 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
592 if (type
== DeviceType
.XTERM
) {
593 this.input
= new InputStreamReader(this.inputStream
, "UTF-8");
594 this.output
= new OutputStreamWriter(new
595 BufferedOutputStream(outputStream
), "UTF-8");
596 this.outputStream
= null;
599 this.outputStream
= new BufferedOutputStream(outputStream
);
601 this.displayListener
= displayListener
;
604 for (int i
= 0; i
< height
; i
++) {
605 display
.add(new DisplayLine(currentState
.attr
));
608 // Spin up the input reader
609 readerThread
= new Thread(this);
610 readerThread
.start();
613 // ------------------------------------------------------------------------
614 // Runnable ---------------------------------------------------------------
615 // ------------------------------------------------------------------------
618 * Read function runs on a separate thread.
620 public final void run() {
621 boolean utf8
= false;
622 boolean done
= false;
624 if (type
== DeviceType
.XTERM
) {
628 // available() will often return > 1, so we need to read in chunks to
630 char [] readBufferUTF8
= null;
631 byte [] readBuffer
= null;
633 readBufferUTF8
= new char[128];
635 readBuffer
= new byte[128];
638 while (!done
&& !stopReaderThread
) {
640 int n
= inputStream
.available();
642 // System.err.printf("available() %d\n", n); System.err.flush();
644 if (readBufferUTF8
.length
< n
) {
645 // The buffer wasn't big enough, make it huger
646 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
649 readBufferUTF8
= new char[newSizeHalf
* 2];
652 if (readBuffer
.length
< n
) {
653 // The buffer wasn't big enough, make it huger
654 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
655 readBuffer
= new byte[newSizeHalf
* 2];
661 } catch (InterruptedException e
) {
670 rc
= input
.read(readBufferUTF8
, 0,
671 readBufferUTF8
.length
);
673 rc
= inputStream
.read(readBuffer
, 0,
676 } catch (ReadTimeoutException e
) {
680 // System.err.printf("read() %d\n", rc); System.err.flush();
685 // Don't step on UI events
686 synchronized (this) {
687 for (int i
= 0; i
< rc
; i
++) {
690 ch
= readBufferUTF8
[i
];
698 // Permit my enclosing UI to know that I updated.
699 if (displayListener
!= null) {
700 displayListener
.displayChanged();
703 // System.err.println("end while loop"); System.err.flush();
704 } catch (IOException e
) {
709 } // while ((done == false) && (stopReaderThread == false))
711 // Let the rest of the world know that I am done.
712 stopReaderThread
= true;
715 inputStream
.cancelRead();
718 } catch (IOException e
) {
724 } catch (IOException e
) {
728 // Permit my enclosing UI to know that I updated.
729 if (displayListener
!= null) {
730 displayListener
.displayChanged();
733 // System.err.println("*** run() exiting..."); System.err.flush();
736 // ------------------------------------------------------------------------
737 // ECMA48 -----------------------------------------------------------------
738 // ------------------------------------------------------------------------
741 * Return the proper primary Device Attributes string.
743 * @return string to send to remote side that is appropriate for the
746 private String
deviceTypeResponse() {
749 // "I am a VT100 with advanced video option" (often VT102)
758 // "I am a VT220" - 7 bit version
760 return "\033[?62;1;6c";
762 // "I am a VT220" - 8 bit version
763 return "\u009b?62;1;6c";
765 throw new IllegalArgumentException("Invalid device type: " + type
);
770 * Return the proper TERM environment variable for this device type.
772 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
773 * @return "vt100", "xterm", etc.
775 public static String
deviceTypeTerm(final DeviceType deviceType
) {
776 switch (deviceType
) {
790 throw new IllegalArgumentException("Invalid device type: "
796 * Return the proper LANG for this device type. Only XTERM devices know
797 * about UTF-8, the others are defined by their standard to be either
798 * 7-bit or 8-bit characters only.
800 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
801 * @param baseLang a base language without UTF-8 flag such as "C" or
803 * @return "en_US", "en_US.UTF-8", etc.
805 public static String
deviceTypeLang(final DeviceType deviceType
,
806 final String baseLang
) {
808 switch (deviceType
) {
816 return baseLang
+ ".UTF-8";
819 throw new IllegalArgumentException("Invalid device type: "
825 * Write a string directly to the remote side.
827 * @param str string to send
829 public void writeRemote(final String str
) {
830 if (stopReaderThread
) {
831 // Reader hit EOF, bail out now.
836 // System.err.printf("writeRemote() '%s'\n", str);
842 if (outputStream
== null) {
846 outputStream
.flush();
847 for (int i
= 0; i
< str
.length(); i
++) {
848 outputStream
.write(str
.charAt(i
));
850 outputStream
.flush();
851 } catch (IOException e
) {
857 if (output
== null) {
864 } catch (IOException e
) {
870 throw new IllegalArgumentException("Invalid device type: " + type
);
875 * Close the input and output streams and stop the reader thread. Note
876 * that it is safe to call this multiple times.
878 public final void close() {
880 // Tell the reader thread to stop looking at input. It will close
881 // the input streams.
882 if (stopReaderThread
== false) {
883 stopReaderThread
= true;
885 readerThread
.join(1000);
886 } catch (InterruptedException e
) {
891 // Now close the output stream.
896 if (outputStream
!= null) {
898 outputStream
.close();
899 } catch (IOException e
) {
906 if (outputStream
!= null) {
908 outputStream
.close();
909 } catch (IOException e
) {
914 if (output
!= null) {
917 } catch (IOException e
) {
924 throw new IllegalArgumentException("Invalid device type: " +
930 * See if the reader thread is still running.
932 * @return if true, we are still connected to / reading from the remote
935 public final boolean isReading() {
936 return (!stopReaderThread
);
940 * Obtain a new blank display line for an external user
941 * (e.g. TTerminalWindow).
943 * @return new blank line
945 public final DisplayLine
getBlankDisplayLine() {
946 return new DisplayLine(currentState
.attr
);
950 * Get the scrollback buffer.
952 * @return the scrollback buffer
954 public final List
<DisplayLine
> getScrollbackBuffer() {
959 * Get the display buffer.
961 * @return the display buffer
963 public final List
<DisplayLine
> getDisplayBuffer() {
968 * Get the display width.
970 * @return the width (usually 80 or 132)
972 public final int getWidth() {
977 * Set the display width.
979 * @param width the new width
981 public final void setWidth(final int width
) {
983 rightMargin
= width
- 1;
984 if (currentState
.cursorX
>= width
) {
985 currentState
.cursorX
= width
- 1;
987 if (savedState
.cursorX
>= width
) {
988 savedState
.cursorX
= width
- 1;
993 * Get the display height.
995 * @return the height (usually 24)
997 public final int getHeight() {
1002 * Set the display height.
1004 * @param height the new height
1006 public final void setHeight(final int height
) {
1007 int delta
= height
- this.height
;
1008 this.height
= height
;
1009 scrollRegionBottom
+= delta
;
1010 if (scrollRegionBottom
< 0) {
1011 scrollRegionBottom
= height
;
1013 if (scrollRegionTop
>= scrollRegionBottom
) {
1014 scrollRegionTop
= 0;
1016 if (currentState
.cursorY
>= height
) {
1017 currentState
.cursorY
= height
- 1;
1019 if (savedState
.cursorY
>= height
) {
1020 savedState
.cursorY
= height
- 1;
1022 while (display
.size() < height
) {
1023 DisplayLine line
= new DisplayLine(currentState
.attr
);
1024 line
.setReverseColor(reverseVideo
);
1027 while (display
.size() > height
) {
1028 scrollback
.add(display
.remove(0));
1033 * Get visible cursor flag.
1035 * @return if true, the cursor is visible
1037 public final boolean isCursorVisible() {
1038 return cursorVisible
;
1042 * Get the screen title as set by the xterm OSC sequence. Lots of
1043 * applications send a screenTitle regardless of whether it is an xterm
1046 * @return screen title
1048 public final String
getScreenTitle() {
1053 * Get 132 columns value.
1055 * @return if true, the terminal is in 132 column mode
1057 public final boolean isColumns132() {
1062 * Clear the CSI parameters and flags.
1064 private void toGround() {
1066 collectBuffer
= new StringBuilder(8);
1067 scanState
= ScanState
.GROUND
;
1071 * Reset the tab stops list.
1073 private void resetTabStops() {
1075 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
1076 tabStops
.add(new Integer(i
* 8));
1081 * Reset the emulation state.
1083 private void reset() {
1085 currentState
= new SaveableState();
1086 savedState
= new SaveableState();
1087 scanState
= ScanState
.GROUND
;
1090 scrollRegionTop
= 0;
1091 scrollRegionBottom
= height
- 1;
1092 rightMargin
= width
- 1;
1093 newLineMode
= false;
1094 arrowKeyMode
= ArrowKeyMode
.ANSI
;
1095 keypadMode
= KeypadMode
.Numeric
;
1096 wrapLineFlag
= false;
1097 if (displayListener
!= null) {
1098 width
= displayListener
.getDisplayWidth();
1099 height
= displayListener
.getDisplayHeight();
1100 rightMargin
= width
- 1;
1108 newLineMode
= false;
1109 reverseVideo
= false;
1111 cursorVisible
= true;
1114 singleshift
= Singleshift
.NONE
;
1116 printerControllerMode
= false;
1119 mouseProtocol
= MouseProtocol
.OFF
;
1120 mouseEncoding
= MouseEncoding
.X10
;
1130 * Append a new line to the bottom of the display, adding lines off the
1131 * top to the scrollback buffer.
1133 private void newDisplayLine() {
1134 // Scroll the top line off into the scrollback buffer
1135 scrollback
.add(display
.get(0));
1137 DisplayLine line
= new DisplayLine(currentState
.attr
);
1138 line
.setReverseColor(reverseVideo
);
1143 * Wraps the current line.
1145 private void wrapCurrentLine() {
1146 if (currentState
.cursorY
== height
- 1) {
1149 if (currentState
.cursorY
< height
- 1) {
1150 currentState
.cursorY
++;
1152 currentState
.cursorX
= 0;
1156 * Handle a carriage return.
1158 private void carriageReturn() {
1159 currentState
.cursorX
= 0;
1160 wrapLineFlag
= false;
1164 * Reverse the color of the visible display.
1166 private void invertDisplayColors() {
1167 for (DisplayLine line
: display
) {
1168 line
.setReverseColor(!line
.isReverseColor());
1173 * Handle a linefeed.
1175 private void linefeed() {
1177 if (currentState
.cursorY
< scrollRegionBottom
) {
1178 // Increment screen y
1179 currentState
.cursorY
++;
1182 // Screen y does not increment
1185 * Two cases: either we're inside a scrolling region or not. If
1186 * the scrolling region bottom is the bottom of the screen, then
1187 * push the top line into the buffer. Else scroll the scrolling
1190 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1192 // We're at the bottom of the scroll region, AND the scroll
1193 // region is the entire screen.
1199 // We're at the bottom of the scroll region, AND the scroll
1200 // region is NOT the entire screen.
1201 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1206 currentState
.cursorX
= 0;
1208 wrapLineFlag
= false;
1212 * Prints one character to the display buffer.
1214 * @param ch character to display
1216 private void printCharacter(final char ch
) {
1217 int rightMargin
= this.rightMargin
;
1219 // Check if we have double-width, and if so chop at 40/66 instead of
1221 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1222 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1225 // Check the unusually-complicated line wrapping conditions...
1226 if (currentState
.cursorX
== rightMargin
) {
1228 if (currentState
.lineWrap
== true) {
1230 * This case happens when: the cursor was already on the
1231 * right margin (either through printing or by an explicit
1232 * placement command), and a character was printed.
1234 * The line wraps only when a new character arrives AND the
1235 * cursor is already on the right margin AND has placed a
1236 * character in its cell. Easier to see than to explain.
1238 if (wrapLineFlag
== false) {
1240 * This block marks the case that we are in the margin
1241 * and the first character has been received and printed.
1243 wrapLineFlag
= true;
1246 * This block marks the case that we are in the margin
1247 * and the second character has been received and
1250 wrapLineFlag
= false;
1254 } else if (currentState
.cursorX
<= rightMargin
) {
1256 * This is the normal case: a character came in and was printed
1257 * to the left of the right margin column.
1260 // Turn off VT100 special-case flag
1261 wrapLineFlag
= false;
1264 // "Print" the character
1265 Cell newCell
= new Cell(ch
);
1266 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1267 newCellAttributes
.setTo(currentState
.attr
);
1268 DisplayLine line
= display
.get(currentState
.cursorY
);
1269 // Insert mode special case
1270 if (insertMode
== true) {
1271 line
.insert(currentState
.cursorX
, newCell
);
1273 // Replace an existing character
1274 line
.replace(currentState
.cursorX
, newCell
);
1277 // Increment horizontal
1278 if (wrapLineFlag
== false) {
1279 currentState
.cursorX
++;
1280 if (currentState
.cursorX
> rightMargin
) {
1281 currentState
.cursorX
--;
1287 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1288 * send to the remote side.
1290 * @param mouse mouse event received from the local user
1292 public void mouse(final TMouseEvent mouse
) {
1295 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1296 mouseProtocol, mouseEncoding, mouse);
1299 if (mouseEncoding
== MouseEncoding
.X10
) {
1300 // We will support X10 but only for (160,94) and smaller.
1301 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1306 switch (mouseProtocol
) {
1313 // Only report button presses
1314 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1320 // Only report button presses and releases
1321 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1322 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1330 * Only report button presses, button releases, and motions that
1331 * have a button down (i.e. drag-and-drop).
1333 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1334 if (!mouse
.isMouse1()
1335 && !mouse
.isMouse2()
1336 && !mouse
.isMouse3()
1337 && !mouse
.isMouseWheelUp()
1338 && !mouse
.isMouseWheelDown()
1346 // Report everything
1350 // Now encode the event
1351 StringBuilder sb
= new StringBuilder(6);
1352 if (mouseEncoding
== MouseEncoding
.SGR
) {
1353 sb
.append((char) 0x1B);
1356 if (mouse
.isMouse1()) {
1357 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1362 } else if (mouse
.isMouse2()) {
1363 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1368 } else if (mouse
.isMouse3()) {
1369 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1374 } else if (mouse
.isMouseWheelUp()) {
1376 } else if (mouse
.isMouseWheelDown()) {
1379 // This is motion with no buttons down.
1383 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1386 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1393 // X10 and UTF8 encodings
1394 sb
.append((char) 0x1B);
1397 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1398 sb
.append((char) (0x03 + 32));
1399 } else if (mouse
.isMouse1()) {
1400 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1401 sb
.append((char) (0x00 + 32 + 32));
1403 sb
.append((char) (0x00 + 32));
1405 } else if (mouse
.isMouse2()) {
1406 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1407 sb
.append((char) (0x01 + 32 + 32));
1409 sb
.append((char) (0x01 + 32));
1411 } else if (mouse
.isMouse3()) {
1412 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1413 sb
.append((char) (0x02 + 32 + 32));
1415 sb
.append((char) (0x02 + 32));
1417 } else if (mouse
.isMouseWheelUp()) {
1418 sb
.append((char) (0x04 + 64));
1419 } else if (mouse
.isMouseWheelDown()) {
1420 sb
.append((char) (0x05 + 64));
1422 // This is motion with no buttons down.
1423 sb
.append((char) (0x03 + 32));
1426 sb
.append((char) (mouse
.getX() + 33));
1427 sb
.append((char) (mouse
.getY() + 33));
1430 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1431 writeRemote(sb
.toString());
1435 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1436 * send to the remote side.
1438 * @param keypress keypress received from the local user
1440 public void keypress(final TKeypress keypress
) {
1441 writeRemote(keypressToString(keypress
));
1445 * Build one of the complex xterm keystroke sequences, storing the result in
1446 * xterm_keystroke_buffer.
1448 * @param ss3 the prefix to use based on VT100 state.
1449 * @param first the first character, usually a number.
1450 * @param first the last character, one of the following: ~ A B C D F H
1451 * @param ctrl whether or not ctrl is down
1452 * @param alt whether or not alt is down
1453 * @param shift whether or not shift is down
1454 * @return the buffer with the full key sequence
1456 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1457 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1459 StringBuilder sb
= new StringBuilder(ss3
);
1460 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1464 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1466 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1468 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1470 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1472 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1474 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1476 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1481 return sb
.toString();
1485 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1487 * @param keypress keypress received from the local user
1488 * @return string to transmit to the remote side
1490 private String
keypressToString(final TKeypress keypress
) {
1492 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1494 * If this is a control character, process it like it came from
1497 if (keypress
.getChar() < 0x20) {
1498 handleControlChar(keypress
.getChar());
1500 // Local echo for everything else
1501 printCharacter(keypress
.getChar());
1505 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1510 // Handle control characters
1511 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1512 StringBuilder sb
= new StringBuilder();
1513 char ch
= keypress
.getChar();
1516 return sb
.toString();
1519 // Handle alt characters
1520 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1521 StringBuilder sb
= new StringBuilder("\033");
1522 char ch
= keypress
.getChar();
1524 return sb
.toString();
1527 if (keypress
.equals(kbBackspaceDel
)) {
1540 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1543 switch (arrowKeyMode
) {
1545 return xtermBuildKeySequence("\033[", '1', 'D',
1546 keypress
.isCtrl(), keypress
.isAlt(),
1547 keypress
.isShift());
1549 return xtermBuildKeySequence("\033", '1', 'D',
1550 keypress
.isCtrl(), keypress
.isAlt(),
1551 keypress
.isShift());
1553 return xtermBuildKeySequence("\033O", '1', 'D',
1554 keypress
.isCtrl(), keypress
.isAlt(),
1555 keypress
.isShift());
1558 switch (arrowKeyMode
) {
1569 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1572 switch (arrowKeyMode
) {
1574 return xtermBuildKeySequence("\033[", '1', 'C',
1575 keypress
.isCtrl(), keypress
.isAlt(),
1576 keypress
.isShift());
1578 return xtermBuildKeySequence("\033", '1', 'C',
1579 keypress
.isCtrl(), keypress
.isAlt(),
1580 keypress
.isShift());
1582 return xtermBuildKeySequence("\033O", '1', 'C',
1583 keypress
.isCtrl(), keypress
.isAlt(),
1584 keypress
.isShift());
1587 switch (arrowKeyMode
) {
1598 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1601 switch (arrowKeyMode
) {
1603 return xtermBuildKeySequence("\033[", '1', 'A',
1604 keypress
.isCtrl(), keypress
.isAlt(),
1605 keypress
.isShift());
1607 return xtermBuildKeySequence("\033", '1', 'A',
1608 keypress
.isCtrl(), keypress
.isAlt(),
1609 keypress
.isShift());
1611 return xtermBuildKeySequence("\033O", '1', 'A',
1612 keypress
.isCtrl(), keypress
.isAlt(),
1613 keypress
.isShift());
1616 switch (arrowKeyMode
) {
1627 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1630 switch (arrowKeyMode
) {
1632 return xtermBuildKeySequence("\033[", '1', 'B',
1633 keypress
.isCtrl(), keypress
.isAlt(),
1634 keypress
.isShift());
1636 return xtermBuildKeySequence("\033", '1', 'B',
1637 keypress
.isCtrl(), keypress
.isAlt(),
1638 keypress
.isShift());
1640 return xtermBuildKeySequence("\033O", '1', 'B',
1641 keypress
.isCtrl(), keypress
.isAlt(),
1642 keypress
.isShift());
1645 switch (arrowKeyMode
) {
1656 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1659 switch (arrowKeyMode
) {
1661 return xtermBuildKeySequence("\033[", '1', 'H',
1662 keypress
.isCtrl(), keypress
.isAlt(),
1663 keypress
.isShift());
1665 return xtermBuildKeySequence("\033", '1', 'H',
1666 keypress
.isCtrl(), keypress
.isAlt(),
1667 keypress
.isShift());
1669 return xtermBuildKeySequence("\033O", '1', 'H',
1670 keypress
.isCtrl(), keypress
.isAlt(),
1671 keypress
.isShift());
1674 switch (arrowKeyMode
) {
1685 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1688 switch (arrowKeyMode
) {
1690 return xtermBuildKeySequence("\033[", '1', 'F',
1691 keypress
.isCtrl(), keypress
.isAlt(),
1692 keypress
.isShift());
1694 return xtermBuildKeySequence("\033", '1', 'F',
1695 keypress
.isCtrl(), keypress
.isAlt(),
1696 keypress
.isShift());
1698 return xtermBuildKeySequence("\033O", '1', 'F',
1699 keypress
.isCtrl(), keypress
.isAlt(),
1700 keypress
.isShift());
1703 switch (arrowKeyMode
) {
1714 if (keypress
.equals(kbF1
)) {
1722 if (keypress
.equals(kbF2
)) {
1730 if (keypress
.equals(kbF3
)) {
1738 if (keypress
.equals(kbF4
)) {
1746 if (keypress
.equals(kbF5
)) {
1759 if (keypress
.equals(kbF6
)) {
1772 if (keypress
.equals(kbF7
)) {
1785 if (keypress
.equals(kbF8
)) {
1798 if (keypress
.equals(kbF9
)) {
1811 if (keypress
.equals(kbF10
)) {
1824 if (keypress
.equals(kbF11
)) {
1828 if (keypress
.equals(kbF12
)) {
1832 if (keypress
.equals(kbShiftF1
)) {
1837 if (type
== DeviceType
.XTERM
) {
1843 if (keypress
.equals(kbShiftF2
)) {
1848 if (type
== DeviceType
.XTERM
) {
1854 if (keypress
.equals(kbShiftF3
)) {
1859 if (type
== DeviceType
.XTERM
) {
1865 if (keypress
.equals(kbShiftF4
)) {
1870 if (type
== DeviceType
.XTERM
) {
1876 if (keypress
.equals(kbShiftF5
)) {
1878 return "\033[15;2~";
1881 if (keypress
.equals(kbShiftF6
)) {
1883 return "\033[17;2~";
1886 if (keypress
.equals(kbShiftF7
)) {
1888 return "\033[18;2~";
1891 if (keypress
.equals(kbShiftF8
)) {
1893 return "\033[19;2~";
1896 if (keypress
.equals(kbShiftF9
)) {
1898 return "\033[20;2~";
1901 if (keypress
.equals(kbShiftF10
)) {
1903 return "\033[21;2~";
1906 if (keypress
.equals(kbShiftF11
)) {
1908 return "\033[23;2~";
1911 if (keypress
.equals(kbShiftF12
)) {
1913 return "\033[24;2~";
1916 if (keypress
.equals(kbCtrlF1
)) {
1921 if (type
== DeviceType
.XTERM
) {
1927 if (keypress
.equals(kbCtrlF2
)) {
1932 if (type
== DeviceType
.XTERM
) {
1938 if (keypress
.equals(kbCtrlF3
)) {
1943 if (type
== DeviceType
.XTERM
) {
1949 if (keypress
.equals(kbCtrlF4
)) {
1954 if (type
== DeviceType
.XTERM
) {
1960 if (keypress
.equals(kbCtrlF5
)) {
1962 return "\033[15;5~";
1965 if (keypress
.equals(kbCtrlF6
)) {
1967 return "\033[17;5~";
1970 if (keypress
.equals(kbCtrlF7
)) {
1972 return "\033[18;5~";
1975 if (keypress
.equals(kbCtrlF8
)) {
1977 return "\033[19;5~";
1980 if (keypress
.equals(kbCtrlF9
)) {
1982 return "\033[20;5~";
1985 if (keypress
.equals(kbCtrlF10
)) {
1987 return "\033[21;5~";
1990 if (keypress
.equals(kbCtrlF11
)) {
1992 return "\033[23;5~";
1995 if (keypress
.equals(kbCtrlF12
)) {
1997 return "\033[24;5~";
2000 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2003 return xtermBuildKeySequence("\033[", '5', '~',
2004 keypress
.isCtrl(), keypress
.isAlt(),
2005 keypress
.isShift());
2011 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2014 return xtermBuildKeySequence("\033[", '6', '~',
2015 keypress
.isCtrl(), keypress
.isAlt(),
2016 keypress
.isShift());
2022 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2025 return xtermBuildKeySequence("\033[", '2', '~',
2026 keypress
.isCtrl(), keypress
.isAlt(),
2027 keypress
.isShift());
2033 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2036 return xtermBuildKeySequence("\033[", '3', '~',
2037 keypress
.isCtrl(), keypress
.isAlt(),
2038 keypress
.isShift());
2040 // Delete sends real delete for VTxxx
2045 if (keypress
.equals(kbEnter
)) {
2049 if (keypress
.equals(kbEsc
)) {
2053 if (keypress
.equals(kbAltEsc
)) {
2057 if (keypress
.equals(kbTab
)) {
2061 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2062 (keypress
.equals(kbShiftTab
))
2072 // Non-alt, non-ctrl characters
2073 if (!keypress
.isFnKey()) {
2074 StringBuilder sb
= new StringBuilder();
2075 sb
.append(keypress
.getChar());
2076 return sb
.toString();
2082 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2085 * @param ch 8-bit character from the remote side
2086 * @param charsetGl character set defined for GL
2087 * @param charsetGr character set defined for GR
2088 * @return character to display on the screen
2090 private char mapCharacterCharset(final char ch
,
2091 final CharacterSet charsetGl
,
2092 final CharacterSet charsetGr
) {
2094 int lookupChar
= ch
;
2095 CharacterSet lookupCharset
= charsetGl
;
2098 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2099 lookupCharset
= charsetGr
;
2103 switch (lookupCharset
) {
2106 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2109 return DECCharacterSets
.UK
[lookupChar
];
2112 return DECCharacterSets
.US_ASCII
[lookupChar
];
2115 return DECCharacterSets
.NL
[lookupChar
];
2118 return DECCharacterSets
.FI
[lookupChar
];
2121 return DECCharacterSets
.FR
[lookupChar
];
2124 return DECCharacterSets
.FR_CA
[lookupChar
];
2127 return DECCharacterSets
.DE
[lookupChar
];
2130 return DECCharacterSets
.IT
[lookupChar
];
2133 return DECCharacterSets
.NO
[lookupChar
];
2136 return DECCharacterSets
.ES
[lookupChar
];
2139 return DECCharacterSets
.SV
[lookupChar
];
2142 return DECCharacterSets
.SWISS
[lookupChar
];
2144 case DEC_SUPPLEMENTAL
:
2145 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2148 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2151 return DECCharacterSets
.US_ASCII
[lookupChar
];
2154 return DECCharacterSets
.US_ASCII
[lookupChar
];
2157 throw new IllegalArgumentException("Invalid character set value: "
2163 * Map an 8-bit byte into a printable character.
2165 * @param ch either 8-bit or Unicode character from the remote side
2166 * @return character to display on the screen
2168 private char mapCharacter(final char ch
) {
2170 // Unicode character, just return it
2174 CharacterSet charsetGl
= currentState
.g0Charset
;
2175 CharacterSet charsetGr
= currentState
.grCharset
;
2177 if (vt52Mode
== true) {
2178 if (shiftOut
== true) {
2179 // Shifted out character, pull from VT52 graphics
2180 charsetGl
= currentState
.g1Charset
;
2181 charsetGr
= CharacterSet
.US
;
2184 charsetGl
= currentState
.g0Charset
;
2185 charsetGr
= CharacterSet
.US
;
2188 // Pull the character
2189 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2193 if (shiftOut
== true) {
2194 // Shifted out character, pull from G1
2195 charsetGl
= currentState
.g1Charset
;
2196 charsetGr
= currentState
.grCharset
;
2198 // Pull the character
2199 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2203 if (singleshift
== Singleshift
.SS2
) {
2205 singleshift
= Singleshift
.NONE
;
2207 // Shifted out character, pull from G2
2208 charsetGl
= currentState
.g2Charset
;
2209 charsetGr
= currentState
.grCharset
;
2213 if (singleshift
== Singleshift
.SS3
) {
2215 singleshift
= Singleshift
.NONE
;
2217 // Shifted out character, pull from G3
2218 charsetGl
= currentState
.g3Charset
;
2219 charsetGr
= currentState
.grCharset
;
2222 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2223 // Check for locking shift
2225 switch (currentState
.glLockshift
) {
2238 charsetGl
= currentState
.g2Charset
;
2243 charsetGl
= currentState
.g3Charset
;
2248 charsetGl
= currentState
.g0Charset
;
2252 switch (currentState
.grLockshift
) {
2262 charsetGr
= currentState
.g1Charset
;
2267 charsetGr
= currentState
.g2Charset
;
2272 charsetGr
= currentState
.g3Charset
;
2277 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2284 // Pull the character
2285 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2289 * Scroll the text within a scrolling region up n lines.
2291 * @param regionTop top row of the scrolling region
2292 * @param regionBottom bottom row of the scrolling region
2293 * @param n number of lines to scroll
2295 private void scrollingRegionScrollUp(final int regionTop
,
2296 final int regionBottom
, final int n
) {
2298 if (regionTop
>= regionBottom
) {
2302 // Sanity check: see if there will be any characters left after the
2304 if (regionBottom
+ 1 - regionTop
<= n
) {
2305 // There won't be anything left in the region, so just call
2306 // eraseScreen() and return.
2307 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2311 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2312 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2313 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2315 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2316 - remaining
, regionBottom
+ 1);
2317 display
= new LinkedList
<DisplayLine
>(displayTop
);
2318 display
.addAll(displayMiddle
);
2319 for (int i
= 0; i
< n
; i
++) {
2320 DisplayLine line
= new DisplayLine(currentState
.attr
);
2321 line
.setReverseColor(reverseVideo
);
2324 display
.addAll(displayBottom
);
2326 assert (display
.size() == height
);
2330 * Scroll the text within a scrolling region down n lines.
2332 * @param regionTop top row of the scrolling region
2333 * @param regionBottom bottom row of the scrolling region
2334 * @param n number of lines to scroll
2336 private void scrollingRegionScrollDown(final int regionTop
,
2337 final int regionBottom
, final int n
) {
2339 if (regionTop
>= regionBottom
) {
2343 // Sanity check: see if there will be any characters left after the
2345 if (regionBottom
+ 1 - regionTop
<= n
) {
2346 // There won't be anything left in the region, so just call
2347 // eraseScreen() and return.
2348 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2352 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2353 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2354 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2356 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2357 regionTop
+ remaining
);
2358 display
= new LinkedList
<DisplayLine
>(displayTop
);
2359 for (int i
= 0; i
< n
; i
++) {
2360 DisplayLine line
= new DisplayLine(currentState
.attr
);
2361 line
.setReverseColor(reverseVideo
);
2364 display
.addAll(displayMiddle
);
2365 display
.addAll(displayBottom
);
2367 assert (display
.size() == height
);
2371 * Process a control character.
2373 * @param ch 8-bit character from the remote side
2375 private void handleControlChar(final char ch
) {
2376 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2387 // Transmit the answerback message.
2398 cursorLeft(1, false);
2403 advanceToNextTabStop();
2429 currentState
.glLockshift
= LockshiftMode
.NONE
;
2435 currentState
.glLockshift
= LockshiftMode
.NONE
;
2460 singleshift
= Singleshift
.SS2
;
2465 singleshift
= Singleshift
.SS3
;
2475 * Advance the cursor to the next tab stop.
2477 private void advanceToNextTabStop() {
2478 if (tabStops
.size() == 0) {
2479 // Go to the rightmost column
2480 cursorRight(rightMargin
- currentState
.cursorX
, false);
2483 for (Integer stop
: tabStops
) {
2484 if (stop
> currentState
.cursorX
) {
2485 cursorRight(stop
- currentState
.cursorX
, false);
2490 * We got here, meaning there isn't a tab stop beyond the current
2491 * cursor position. Place the cursor of the right-most edge of the
2494 cursorRight(rightMargin
- currentState
.cursorX
, false);
2498 * Save a character into the collect buffer.
2500 * @param ch character to save
2502 private void collect(final char ch
) {
2503 collectBuffer
.append(ch
);
2507 * Save a byte into the CSI parameters buffer.
2509 * @param ch byte to save
2511 private void param(final byte ch
) {
2512 if (csiParams
.size() == 0) {
2513 csiParams
.add(new Integer(0));
2515 Integer x
= csiParams
.get(csiParams
.size() - 1);
2516 if ((ch
>= '0') && (ch
<= '9')) {
2519 csiParams
.set(csiParams
.size() - 1, x
);
2523 csiParams
.add(new Integer(0));
2528 * Get a CSI parameter value, with a default.
2530 * @param position parameter index. 0 is the first parameter.
2531 * @param defaultValue value to use if csiParams[position] doesn't exist
2532 * @return parameter value
2534 private int getCsiParam(final int position
, final int defaultValue
) {
2535 if (csiParams
.size() < position
+ 1) {
2536 return defaultValue
;
2538 return csiParams
.get(position
).intValue();
2542 * Get a CSI parameter value, clamped to within min/max.
2544 * @param position parameter index. 0 is the first parameter.
2545 * @param defaultValue value to use if csiParams[position] doesn't exist
2546 * @param minValue minimum value inclusive
2547 * @param maxValue maximum value inclusive
2548 * @return parameter value
2550 private int getCsiParam(final int position
, final int defaultValue
,
2551 final int minValue
, final int maxValue
) {
2553 assert (minValue
<= maxValue
);
2554 int value
= getCsiParam(position
, defaultValue
);
2555 if (value
< minValue
) {
2558 if (value
> maxValue
) {
2565 * Set or unset a toggle.
2567 * @param value true for set ('h'), false for reset ('l')
2569 private void setToggle(final boolean value
) {
2570 boolean decPrivateModeFlag
= false;
2571 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2572 if (collectBuffer
.charAt(i
) == '?') {
2573 decPrivateModeFlag
= true;
2578 for (Integer i
: csiParams
) {
2583 if (decPrivateModeFlag
== true) {
2585 if (value
== true) {
2586 // Use application arrow keys
2587 arrowKeyMode
= ArrowKeyMode
.VT100
;
2589 // Use ANSI arrow keys
2590 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2595 if (decPrivateModeFlag
== true) {
2596 if (value
== false) {
2600 arrowKeyMode
= ArrowKeyMode
.VT52
;
2603 * From the VT102 docs: "You use ANSI mode to select
2604 * most terminal features; the terminal uses the same
2605 * features when it switches to VT52 mode. You
2606 * cannot, however, change most of these features in
2609 * In other words, do not reset any other attributes
2610 * when switching between VT52 submode and ANSI.
2612 * HOWEVER, the real vt100 does switch the character
2613 * set according to Usenet.
2615 currentState
.g0Charset
= CharacterSet
.US
;
2616 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2619 if ((type
== DeviceType
.VT220
)
2620 || (type
== DeviceType
.XTERM
)) {
2622 // VT52 mode is explicitly 7-bit
2624 singleshift
= Singleshift
.NONE
;
2629 if (value
== true) {
2630 // Turn off keyboard
2639 if (decPrivateModeFlag
== true) {
2641 if (value
== true) {
2648 if ((displayListener
!= null)
2649 && (type
== DeviceType
.XTERM
)
2651 // For xterms, reset to the actual width, not 80
2653 width
= displayListener
.getDisplayWidth();
2654 rightMargin
= width
- 1;
2657 width
= rightMargin
+ 1;
2660 // Entire screen is cleared, and scrolling region is
2662 eraseScreen(0, 0, height
- 1, width
- 1, false);
2663 scrollRegionTop
= 0;
2664 scrollRegionBottom
= height
- 1;
2665 // Also home the cursor
2666 cursorPosition(0, 0);
2670 if (decPrivateModeFlag
== true) {
2672 if (value
== true) {
2681 if (value
== true) {
2689 if (decPrivateModeFlag
== true) {
2691 if (value
== true) {
2693 * Set selects reverse screen, a white screen
2694 * background with black characters.
2696 if (reverseVideo
!= true) {
2698 * If in normal video, switch it back
2700 invertDisplayColors();
2702 reverseVideo
= true;
2705 * Reset selects normal screen, a black screen
2706 * background with white characters.
2708 if (reverseVideo
== true) {
2710 * If in reverse video already, switch it back
2712 invertDisplayColors();
2714 reverseVideo
= false;
2719 if (decPrivateModeFlag
== true) {
2721 if (value
== true) {
2722 // Origin is relative to scroll region cursor.
2723 // Cursor can NEVER leave scrolling region.
2724 currentState
.originMode
= true;
2725 cursorPosition(0, 0);
2727 // Origin is absolute to entire screen. Cursor can
2728 // leave the scrolling region via cup() and hvp().
2729 currentState
.originMode
= false;
2730 cursorPosition(0, 0);
2735 if (decPrivateModeFlag
== true) {
2737 if (value
== true) {
2739 currentState
.lineWrap
= true;
2741 // Turn linewrap off
2742 currentState
.lineWrap
= false;
2747 if (decPrivateModeFlag
== true) {
2749 if (value
== true) {
2750 // Keyboard auto-repeat on
2753 // Keyboard auto-repeat off
2759 if (decPrivateModeFlag
== false) {
2761 if (value
== true) {
2771 if (decPrivateModeFlag
== true) {
2777 if (decPrivateModeFlag
== true) {
2783 if (decPrivateModeFlag
== false) {
2785 if (value
== true) {
2787 * Set causes a received linefeed, form feed, or
2788 * vertical tab to move cursor to first column of
2789 * next line. RETURN transmits both a carriage return
2790 * and linefeed. This selection is also called new
2796 * Reset causes a received linefeed, form feed, or
2797 * vertical tab to move cursor to next line in
2798 * current column. RETURN transmits a carriage
2801 newLineMode
= false;
2807 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2808 if (decPrivateModeFlag
== true) {
2810 if (value
== true) {
2812 cursorVisible
= true;
2815 cursorVisible
= false;
2822 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2823 if (decPrivateModeFlag
== true) {
2825 if (value
== true) {
2826 // Select national mode NRC
2829 // Select multi-national mode
2838 if ((type
== DeviceType
.XTERM
)
2839 && (decPrivateModeFlag
== true)
2841 // Mouse: normal tracking mode
2842 if (value
== true) {
2843 mouseProtocol
= MouseProtocol
.NORMAL
;
2845 mouseProtocol
= MouseProtocol
.OFF
;
2851 if ((type
== DeviceType
.XTERM
)
2852 && (decPrivateModeFlag
== true)
2854 // Mouse: normal tracking mode
2855 if (value
== true) {
2856 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
2858 mouseProtocol
= MouseProtocol
.OFF
;
2864 if ((type
== DeviceType
.XTERM
)
2865 && (decPrivateModeFlag
== true)
2867 // Mouse: Any-event tracking mode
2868 if (value
== true) {
2869 mouseProtocol
= MouseProtocol
.ANYEVENT
;
2871 mouseProtocol
= MouseProtocol
.OFF
;
2877 if ((type
== DeviceType
.XTERM
)
2878 && (decPrivateModeFlag
== true)
2880 // Mouse: UTF-8 coordinates
2881 if (value
== true) {
2882 mouseEncoding
= MouseEncoding
.UTF8
;
2884 mouseEncoding
= MouseEncoding
.X10
;
2890 if ((type
== DeviceType
.XTERM
)
2891 && (decPrivateModeFlag
== true)
2893 // Mouse: SGR coordinates
2894 if (value
== true) {
2895 mouseEncoding
= MouseEncoding
.SGR
;
2897 mouseEncoding
= MouseEncoding
.X10
;
2910 * DECSC - Save cursor.
2912 private void decsc() {
2913 savedState
.setTo(currentState
);
2917 * DECRC - Restore cursor.
2919 private void decrc() {
2920 currentState
.setTo(savedState
);
2926 private void ind() {
2927 // Move the cursor and scroll if necessary. If at the bottom line
2928 // already, a scroll up is supposed to be performed.
2929 if (currentState
.cursorY
== scrollRegionBottom
) {
2930 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2932 cursorDown(1, true);
2936 * RI - Reverse index.
2939 // Move the cursor and scroll if necessary. If at the top line
2940 // already, a scroll down is supposed to be performed.
2941 if (currentState
.cursorY
== scrollRegionTop
) {
2942 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
2950 private void nel() {
2951 // Move the cursor and scroll if necessary. If at the bottom line
2952 // already, a scroll up is supposed to be performed.
2953 if (currentState
.cursorY
== scrollRegionBottom
) {
2954 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
2956 cursorDown(1, true);
2958 // Reset to the beginning of the next line
2959 currentState
.cursorX
= 0;
2963 * DECKPAM - Keypad application mode.
2965 private void deckpam() {
2966 keypadMode
= KeypadMode
.Application
;
2970 * DECKPNM - Keypad numeric mode.
2972 private void deckpnm() {
2973 keypadMode
= KeypadMode
.Numeric
;
2979 * @param n number of spaces to move
2980 * @param honorScrollRegion if true, then do nothing if the cursor is
2981 * outside the scrolling region
2983 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
2987 * Special case: if a user moves the cursor from the right margin, we
2988 * have to reset the VT100 right margin flag.
2991 wrapLineFlag
= false;
2994 for (int i
= 0; i
< n
; i
++) {
2995 if (honorScrollRegion
== true) {
2996 // Honor the scrolling region
2997 if ((currentState
.cursorY
< scrollRegionTop
)
2998 || (currentState
.cursorY
> scrollRegionBottom
)
3000 // Outside region, do nothing
3003 // Inside region, go up
3004 top
= scrollRegionTop
;
3006 // Non-scrolling case
3010 if (currentState
.cursorY
> top
) {
3011 currentState
.cursorY
--;
3017 * Move down n spaces.
3019 * @param n number of spaces to move
3020 * @param honorScrollRegion if true, then do nothing if the cursor is
3021 * outside the scrolling region
3023 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3027 * Special case: if a user moves the cursor from the right margin, we
3028 * have to reset the VT100 right margin flag.
3031 wrapLineFlag
= false;
3034 for (int i
= 0; i
< n
; i
++) {
3036 if (honorScrollRegion
== true) {
3037 // Honor the scrolling region
3038 if (currentState
.cursorY
> scrollRegionBottom
) {
3039 // Outside region, do nothing
3042 // Inside region, go down
3043 bottom
= scrollRegionBottom
;
3045 // Non-scrolling case
3046 bottom
= height
- 1;
3049 if (currentState
.cursorY
< bottom
) {
3050 currentState
.cursorY
++;
3056 * Move left n spaces.
3058 * @param n number of spaces to move
3059 * @param honorScrollRegion if true, then do nothing if the cursor is
3060 * outside the scrolling region
3062 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3064 * Special case: if a user moves the cursor from the right margin, we
3065 * have to reset the VT100 right margin flag.
3068 wrapLineFlag
= false;
3071 for (int i
= 0; i
< n
; i
++) {
3072 if (honorScrollRegion
== true) {
3073 // Honor the scrolling region
3074 if ((currentState
.cursorY
< scrollRegionTop
)
3075 || (currentState
.cursorY
> scrollRegionBottom
)
3077 // Outside region, do nothing
3082 if (currentState
.cursorX
> 0) {
3083 currentState
.cursorX
--;
3089 * Move right n spaces.
3091 * @param n number of spaces to move
3092 * @param honorScrollRegion if true, then do nothing if the cursor is
3093 * outside the scrolling region
3095 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3096 int rightMargin
= this.rightMargin
;
3099 * Special case: if a user moves the cursor from the right margin, we
3100 * have to reset the VT100 right margin flag.
3103 wrapLineFlag
= false;
3106 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3107 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3110 for (int i
= 0; i
< n
; i
++) {
3111 if (honorScrollRegion
== true) {
3112 // Honor the scrolling region
3113 if ((currentState
.cursorY
< scrollRegionTop
)
3114 || (currentState
.cursorY
> scrollRegionBottom
)
3116 // Outside region, do nothing
3121 if (currentState
.cursorX
< rightMargin
) {
3122 currentState
.cursorX
++;
3128 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3130 * @param row row to move to
3131 * @param col column to move to
3133 private void cursorPosition(int row
, final int col
) {
3134 int rightMargin
= this.rightMargin
;
3139 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3140 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3143 // Set column number
3144 currentState
.cursorX
= col
;
3146 // Sanity check, bring column back to margin.
3147 if (currentState
.cursorX
> rightMargin
) {
3148 currentState
.cursorX
= rightMargin
;
3152 if (currentState
.originMode
== true) {
3153 row
+= scrollRegionTop
;
3155 if (currentState
.cursorY
< row
) {
3156 cursorDown(row
- currentState
.cursorY
, false);
3157 } else if (currentState
.cursorY
> row
) {
3158 cursorUp(currentState
.cursorY
- row
, false);
3161 wrapLineFlag
= false;
3165 * HTS - Horizontal tabulation set.
3167 private void hts() {
3168 for (Integer stop
: tabStops
) {
3169 if (stop
== currentState
.cursorX
) {
3170 // Already have a tab stop here
3175 // Append a tab stop to the end of the array and resort them
3176 tabStops
.add(currentState
.cursorX
);
3177 Collections
.sort(tabStops
);
3181 * DECSWL - Single-width line.
3183 private void decswl() {
3184 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3185 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3189 * DECDWL - Double-width line.
3191 private void decdwl() {
3192 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3193 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3197 * DECHDL - Double-height + double-width line.
3199 * @param topHalf if true, this sets the row to be the top half row of a
3202 private void dechdl(final boolean topHalf
) {
3203 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3204 if (topHalf
== true) {
3205 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3207 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3212 * DECALN - Screen alignment display.
3214 private void decaln() {
3215 Cell newCell
= new Cell();
3216 newCell
.setChar('E');
3217 for (DisplayLine line
: display
) {
3218 for (int i
= 0; i
< line
.length(); i
++) {
3219 line
.replace(i
, newCell
);
3225 * DECSCL - Compatibility level.
3227 private void decscl() {
3228 int i
= getCsiParam(0, 0);
3229 int j
= getCsiParam(1, 0);
3233 currentState
.g0Charset
= CharacterSet
.US
;
3234 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3236 } else if (i
== 62) {
3238 if ((j
== 0) || (j
== 2)) {
3240 } else if (j
== 1) {
3247 * CUD - Cursor down.
3249 private void cud() {
3250 cursorDown(getCsiParam(0, 1, 1, height
), true);
3254 * CUF - Cursor forward.
3256 private void cuf() {
3257 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3261 * CUB - Cursor backward.
3263 private void cub() {
3264 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3270 private void cuu() {
3271 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3275 * CUP - Cursor position.
3277 private void cup() {
3278 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3279 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3283 * CNL - Cursor down and to column 1.
3285 private void cnl() {
3286 cursorDown(getCsiParam(0, 1, 1, height
), true);
3288 cursorLeft(currentState
.cursorX
, true);
3292 * CPL - Cursor up and to column 1.
3294 private void cpl() {
3295 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3297 cursorLeft(currentState
.cursorX
, true);
3301 * CHA - Cursor to column # in current row.
3303 private void cha() {
3304 cursorPosition(currentState
.cursorY
,
3305 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3309 * VPA - Cursor to row #, same column.
3311 private void vpa() {
3312 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3313 currentState
.cursorX
);
3317 * ED - Erase in display.
3320 boolean honorProtected
= false;
3321 boolean decPrivateModeFlag
= false;
3323 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3324 if (collectBuffer
.charAt(i
) == '?') {
3325 decPrivateModeFlag
= true;
3330 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3331 && (decPrivateModeFlag
== true)
3333 honorProtected
= true;
3336 int i
= getCsiParam(0, 0);
3339 // Erase from here to end of screen
3340 if (currentState
.cursorY
< height
- 1) {
3341 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3344 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3345 } else if (i
== 1) {
3346 // Erase from beginning of screen to here
3347 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3349 eraseLine(0, currentState
.cursorX
, honorProtected
);
3350 } else if (i
== 2) {
3351 // Erase entire screen
3352 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3357 * EL - Erase in line.
3360 boolean honorProtected
= false;
3361 boolean decPrivateModeFlag
= false;
3363 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3364 if (collectBuffer
.charAt(i
) == '?') {
3365 decPrivateModeFlag
= true;
3370 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3371 && (decPrivateModeFlag
== true)
3373 honorProtected
= true;
3376 int i
= getCsiParam(0, 0);
3379 // Erase from here to end of line
3380 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3381 } else if (i
== 1) {
3382 // Erase from beginning of line to here
3383 eraseLine(0, currentState
.cursorX
, honorProtected
);
3384 } else if (i
== 2) {
3385 // Erase entire line
3386 eraseLine(0, width
- 1, honorProtected
);
3391 * ECH - Erase # of characters in current row.
3393 private void ech() {
3394 int i
= getCsiParam(0, 1, 1, width
);
3396 // Erase from here to i characters
3397 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3404 int i
= getCsiParam(0, 1);
3406 if ((currentState
.cursorY
>= scrollRegionTop
)
3407 && (currentState
.cursorY
<= scrollRegionBottom
)
3410 // I can get the same effect with a scroll-down
3411 scrollingRegionScrollDown(currentState
.cursorY
,
3412 scrollRegionBottom
, i
);
3417 * DCH - Delete char.
3419 private void dch() {
3420 int n
= getCsiParam(0, 1);
3421 DisplayLine line
= display
.get(currentState
.cursorY
);
3422 Cell blank
= new Cell();
3423 for (int i
= 0; i
< n
; i
++) {
3424 line
.delete(currentState
.cursorX
, blank
);
3429 * ICH - Insert blank char at cursor.
3431 private void ich() {
3432 int n
= getCsiParam(0, 1);
3433 DisplayLine line
= display
.get(currentState
.cursorY
);
3434 Cell blank
= new Cell();
3435 for (int i
= 0; i
< n
; i
++) {
3436 line
.insert(currentState
.cursorX
, blank
);
3444 int i
= getCsiParam(0, 1);
3446 if ((currentState
.cursorY
>= scrollRegionTop
)
3447 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3449 // I can get the same effect with a scroll-down
3450 scrollingRegionScrollUp(currentState
.cursorY
,
3451 scrollRegionBottom
, i
);
3456 * HVP - Horizontal and vertical position.
3458 private void hvp() {
3463 * REP - Repeat character.
3465 private void rep() {
3466 int n
= getCsiParam(0, 1);
3467 for (int i
= 0; i
< n
; i
++) {
3468 printCharacter(repCh
);
3476 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3477 getCsiParam(0, 1, 1, height
));
3484 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3485 getCsiParam(0, 1, 1, height
));
3489 * CBT - Go back X tab stops.
3491 private void cbt() {
3492 int tabsToMove
= getCsiParam(0, 1);
3495 for (int i
= 0; i
< tabsToMove
; i
++) {
3496 int j
= currentState
.cursorX
;
3497 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3498 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3506 j
= tabStops
.get(tabI
);
3508 cursorPosition(currentState
.cursorY
, j
);
3513 * CHT - Advance X tab stops.
3515 private void cht() {
3516 int n
= getCsiParam(0, 1);
3517 for (int i
= 0; i
< n
; i
++) {
3518 advanceToNextTabStop();
3523 * SGR - Select graphics rendition.
3525 private void sgr() {
3527 if (csiParams
.size() == 0) {
3528 currentState
.attr
.reset();
3532 for (Integer i
: csiParams
) {
3538 currentState
.attr
.reset();
3543 currentState
.attr
.setBold(true);
3548 currentState
.attr
.setUnderline(true);
3553 currentState
.attr
.setBlink(true);
3558 currentState
.attr
.setReverse(true);
3565 if (type
== DeviceType
.XTERM
) {
3579 if ((type
== DeviceType
.VT220
)
3580 || (type
== DeviceType
.XTERM
)) {
3586 currentState
.attr
.setBold(false);
3591 currentState
.attr
.setUnderline(false);
3596 currentState
.attr
.setBlink(false);
3601 currentState
.attr
.setReverse(false);
3609 // A true VT100/102/220 does not support color, however everyone
3610 // is used to their terminal emulator supporting color so we will
3611 // unconditionally support color for all DeviceType's.
3616 // Set black foreground
3617 currentState
.attr
.setForeColor(Color
.BLACK
);
3620 // Set red foreground
3621 currentState
.attr
.setForeColor(Color
.RED
);
3624 // Set green foreground
3625 currentState
.attr
.setForeColor(Color
.GREEN
);
3628 // Set yellow foreground
3629 currentState
.attr
.setForeColor(Color
.YELLOW
);
3632 // Set blue foreground
3633 currentState
.attr
.setForeColor(Color
.BLUE
);
3636 // Set magenta foreground
3637 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3640 // Set cyan foreground
3641 currentState
.attr
.setForeColor(Color
.CYAN
);
3644 // Set white foreground
3645 currentState
.attr
.setForeColor(Color
.WHITE
);
3648 if (type
== DeviceType
.XTERM
) {
3650 * Xterm supports T.416 / ISO-8613-3 codes to select
3651 * either an indexed color or an RGB value. (It also
3652 * permits these ISO-8613-3 SGR sequences to be separated
3653 * by colons rather than semicolons.)
3655 * We will not support any of these additional color
3656 * codes at this time:
3658 * 1. http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
3659 * has a detailed discussion of the current state of
3660 * RGB in various terminals, the point of which is
3661 * that none of them really do the same thing despite
3662 * all appearing to be "xterm".
3665 * https://bugs.kde.org/show_bug.cgi?id=107487#c3,
3666 * even supporting just the "indexed mode" of these
3667 * sequences (which could align easily with existing
3668 * SGR colors) is assumed to mean full support of
3669 * 24-bit RGB. So it is all or nothing.
3671 * Finally, these sequences break the assumptions of
3672 * standard ECMA-48 style parsers as pointed out at
3673 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
3674 * Therefore in order to keep a clean display, we cannot
3675 * parse anything else in this sequence.
3679 // Underscore on, default foreground color
3680 currentState
.attr
.setUnderline(true);
3681 currentState
.attr
.setForeColor(Color
.WHITE
);
3685 // Underscore off, default foreground color
3686 currentState
.attr
.setUnderline(false);
3687 currentState
.attr
.setForeColor(Color
.WHITE
);
3690 // Set black background
3691 currentState
.attr
.setBackColor(Color
.BLACK
);
3694 // Set red background
3695 currentState
.attr
.setBackColor(Color
.RED
);
3698 // Set green background
3699 currentState
.attr
.setBackColor(Color
.GREEN
);
3702 // Set yellow background
3703 currentState
.attr
.setBackColor(Color
.YELLOW
);
3706 // Set blue background
3707 currentState
.attr
.setBackColor(Color
.BLUE
);
3710 // Set magenta background
3711 currentState
.attr
.setBackColor(Color
.MAGENTA
);
3714 // Set cyan background
3715 currentState
.attr
.setBackColor(Color
.CYAN
);
3718 // Set white background
3719 currentState
.attr
.setBackColor(Color
.WHITE
);
3722 if (type
== DeviceType
.XTERM
) {
3724 * Xterm supports T.416 / ISO-8613-3 codes to select
3725 * either an indexed color or an RGB value. (It also
3726 * permits these ISO-8613-3 SGR sequences to be separated
3727 * by colons rather than semicolons.)
3729 * We will not support this at this time. Also, in order
3730 * to keep a clean display, we cannot parse anything else
3737 // Default background
3738 currentState
.attr
.setBackColor(Color
.BLACK
);
3748 * DA - Device attributes.
3751 int extendedFlag
= 0;
3753 if (collectBuffer
.length() > 0) {
3754 String args
= collectBuffer
.substring(1);
3755 if (collectBuffer
.charAt(0) == '>') {
3757 if (collectBuffer
.length() >= 2) {
3758 i
= Integer
.parseInt(args
.toString());
3760 } else if (collectBuffer
.charAt(0) == '=') {
3762 if (collectBuffer
.length() >= 2) {
3763 i
= Integer
.parseInt(args
.toString());
3766 // Unknown code, bail out
3771 if ((i
!= 0) && (i
!= 1)) {
3775 if ((extendedFlag
== 0) && (i
== 0)) {
3776 // Send string directly to remote side
3777 writeRemote(deviceTypeResponse());
3781 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3783 if ((extendedFlag
== 1) && (i
== 0)) {
3785 * Request "What type of terminal are you, what is your
3786 * firmware version, and what hardware options do you have
3789 * Respond: "I am a VT220 (identification code of 1), my
3790 * firmware version is _____ (Pv), and I have _____ Po
3791 * options installed."
3797 if (s8c1t
== true) {
3798 writeRemote("\u009b>1;10;0c");
3800 writeRemote("\033[>1;10;0c");
3806 if ((extendedFlag
== 2) && (i
== 0)) {
3809 * Request "What is your unit ID?"
3811 * Respond: "I was manufactured at site 00 and have a unique ID
3815 writeRemote("\033P!|00010203\033\\");
3820 * DECSTBM - Set top and bottom margins.
3822 private void decstbm() {
3823 boolean decPrivateModeFlag
= false;
3825 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3826 if (collectBuffer
.charAt(i
) == '?') {
3827 decPrivateModeFlag
= true;
3831 if (decPrivateModeFlag
) {
3832 // This could be restore DEC private mode values.
3836 int top
= getCsiParam(0, 1, 1, height
) - 1;
3837 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
3842 scrollRegionTop
= top
;
3843 scrollRegionBottom
= bottom
;
3846 cursorPosition(0, 0);
3851 * DECREQTPARM - Request terminal parameters.
3853 private void decreqtparm() {
3854 int i
= getCsiParam(0, 0);
3856 if ((i
!= 0) && (i
!= 1)) {
3863 * Request terminal parameters.
3867 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3868 * (CLoCk MULtiplier = 1, STP option flags = 0)
3872 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3875 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
3877 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
3883 * DECSCA - Select Character Attributes.
3885 private void decsca() {
3886 int i
= getCsiParam(0, 0);
3888 if ((i
== 0) || (i
== 2)) {
3890 currentState
.attr
.setProtect(false);
3894 currentState
.attr
.setProtect(true);
3899 * DECSTR - Soft Terminal Reset.
3901 private void decstr() {
3902 // Do exactly like RIS - Reset to initial state
3904 // Do I clear screen too? I think so...
3905 eraseScreen(0, 0, height
- 1, width
- 1, false);
3906 cursorPosition(0, 0);
3910 * DSR - Device status report.
3912 private void dsr() {
3913 boolean decPrivateModeFlag
= false;
3914 int row
= currentState
.cursorY
;
3916 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3917 if (collectBuffer
.charAt(i
) == '?') {
3918 decPrivateModeFlag
= true;
3923 int i
= getCsiParam(0, 0);
3928 // Request status report. Respond with "OK, no malfunction."
3930 // Send string directly to remote side
3931 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3934 writeRemote("\u009b0n");
3936 writeRemote("\033[0n");
3941 // Request cursor position. Respond with current position.
3942 if (currentState
.originMode
== true) {
3943 row
-= scrollRegionTop
;
3946 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3949 str
= String
.format("\u009b%d;%dR", row
+ 1,
3950 currentState
.cursorX
+ 1);
3952 str
= String
.format("\033[%d;%dR", row
+ 1,
3953 currentState
.cursorX
+ 1);
3956 // Send string directly to remote side
3961 if (decPrivateModeFlag
== true) {
3963 // Request printer status report. Respond with "Printer not
3966 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3967 && (s8c1t
== true)) {
3968 writeRemote("\u009b?13n");
3970 writeRemote("\033[?13n");
3976 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3977 && (decPrivateModeFlag
== true)
3980 // Request user-defined keys are locked or unlocked. Respond
3981 // with "User-defined keys are locked."
3983 if (s8c1t
== true) {
3984 writeRemote("\u009b?21n");
3986 writeRemote("\033[?21n");
3992 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3993 && (decPrivateModeFlag
== true)
3996 // Request keyboard language. Respond with "Keyboard
3997 // language is North American."
3999 if (s8c1t
== true) {
4000 writeRemote("\u009b?27;1n");
4002 writeRemote("\033[?27;1n");
4009 // Some other option, ignore
4015 * TBC - Tabulation clear.
4017 private void tbc() {
4018 int i
= getCsiParam(0, 0);
4020 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4021 for (Integer stop
: tabStops
) {
4022 if (stop
== currentState
.cursorX
) {
4027 tabStops
= newStops
;
4035 * Erase the characters in the current line from the start column to the
4036 * end column, inclusive.
4038 * @param start starting column to erase (between 0 and width - 1)
4039 * @param end ending column to erase (between 0 and width - 1)
4040 * @param honorProtected if true, do not erase characters with the
4041 * protected attribute set
4043 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4048 if (end
> width
- 1) {
4055 for (int i
= start
; i
<= end
; i
++) {
4056 DisplayLine line
= display
.get(currentState
.cursorY
);
4057 if ((!honorProtected
)
4058 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4065 * From the VT102 manual:
4067 * Erasing a character also erases any character
4068 * attribute of the character.
4074 * Erase with the current color a.k.a. back-color erase
4077 line
.setChar(i
, ' ');
4078 line
.setAttr(i
, currentState
.attr
);
4086 * Erase a rectangular section of the screen, inclusive. end column,
4089 * @param startRow starting row to erase (between 0 and height - 1)
4090 * @param startCol starting column to erase (between 0 and width - 1)
4091 * @param endRow ending row to erase (between 0 and height - 1)
4092 * @param endCol ending column to erase (between 0 and width - 1)
4093 * @param honorProtected if true, do not erase characters with the
4094 * protected attribute set
4096 private void eraseScreen(final int startRow
, final int startCol
,
4097 final int endRow
, final int endCol
, final boolean honorProtected
) {
4105 || (endRow
< startRow
)
4106 || (endCol
< startCol
)
4111 oldCursorY
= currentState
.cursorY
;
4112 for (int i
= startRow
; i
<= endRow
; i
++) {
4113 currentState
.cursorY
= i
;
4114 eraseLine(startCol
, endCol
, honorProtected
);
4116 // Erase display clears the double attributes
4117 display
.get(i
).setDoubleWidth(false);
4118 display
.get(i
).setDoubleHeight(0);
4120 currentState
.cursorY
= oldCursorY
;
4124 * VT220 printer functions. All of these are parsed, but won't do
4127 private void printerFunctions() {
4128 boolean decPrivateModeFlag
= false;
4129 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4130 if (collectBuffer
.charAt(i
) == '?') {
4131 decPrivateModeFlag
= true;
4136 int i
= getCsiParam(0, 0);
4141 if (decPrivateModeFlag
== false) {
4147 if (decPrivateModeFlag
== true) {
4148 // Print cursor line
4153 if (decPrivateModeFlag
== true) {
4154 // Auto print mode OFF
4156 // Printer controller OFF
4158 // Characters re-appear on the screen
4159 printerControllerMode
= false;
4164 if (decPrivateModeFlag
== true) {
4168 // Printer controller
4170 // Characters get sucked into oblivion
4171 printerControllerMode
= true;
4182 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4183 * of remote systems will send an XTerm title sequence even if TERM isn't
4186 * @param xtermChar the character received from the remote side
4188 private void oscPut(final char xtermChar
) {
4190 collectBuffer
.append(xtermChar
);
4193 if (xtermChar
== 0x07) {
4194 String args
= collectBuffer
.substring(0,
4195 collectBuffer
.length() - 1);
4196 String
[] p
= args
.toString().split(";");
4198 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4206 // Go to SCAN_GROUND state
4213 * Run this input character through the ECMA48 state machine.
4215 * @param ch character from the remote side
4217 private void consume(char ch
) {
4220 // System.err.printf("%c", ch);
4222 // Special case for VT10x: 7-bit characters only
4223 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4224 ch
= (char)(ch
& 0x7F);
4227 // Special "anywhere" states
4229 // 18, 1A --> execute, then switch to SCAN_GROUND
4230 if ((ch
== 0x18) || (ch
== 0x1A)) {
4231 // CAN and SUB abort escape sequences
4236 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4240 && (scanState
!= ScanState
.DCS_ENTRY
)
4241 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4242 && (scanState
!= ScanState
.DCS_IGNORE
)
4243 && (scanState
!= ScanState
.DCS_PARAM
)
4244 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4247 scanState
= ScanState
.ESCAPE
;
4251 // 0x9B == CSI 8-bit sequence
4253 scanState
= ScanState
.CSI_ENTRY
;
4257 // 0x9D goes to ScanState.OSC_STRING
4259 scanState
= ScanState
.OSC_STRING
;
4263 // 0x90 goes to DCS_ENTRY
4265 scanState
= ScanState
.DCS_ENTRY
;
4269 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4270 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4271 scanState
= ScanState
.SOSPMAPC_STRING
;
4275 // 0x7F (DEL) is always discarded
4280 switch (scanState
) {
4283 // 00-17, 19, 1C-1F --> execute
4284 // 80-8F, 91-9A, 9C --> execute
4285 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4286 handleControlChar(ch
);
4290 if (((ch
>= 0x20) && (ch
<= 0x7F))
4294 // VT220 printer --> trash bin
4295 if (((type
== DeviceType
.VT220
)
4296 || (type
== DeviceType
.XTERM
))
4297 && (printerControllerMode
== true)
4302 // Hang onto this character
4303 repCh
= mapCharacter(ch
);
4305 // Print this character
4306 printCharacter(repCh
);
4311 // 00-17, 19, 1C-1F --> execute
4313 handleControlChar(ch
);
4317 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4318 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4320 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4324 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4325 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4336 // DECSC - Save cursor
4337 // Note this code overlaps both ANSI and VT52 mode
4342 // DECRC - Restore cursor
4343 // Note this code overlaps both ANSI and VT52 mode
4352 if (vt52Mode
== true) {
4353 // DECANM - Enter ANSI mode
4355 arrowKeyMode
= ArrowKeyMode
.VT100
;
4358 * From the VT102 docs: "You use ANSI mode to select
4359 * most terminal features; the terminal uses the same
4360 * features when it switches to VT52 mode. You
4361 * cannot, however, change most of these features in
4364 * In other words, do not reset any other attributes
4365 * when switching between VT52 submode and ANSI.
4369 currentState
.g0Charset
= CharacterSet
.US
;
4370 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4372 singleshift
= Singleshift
.NONE
;
4373 currentState
.glLockshift
= LockshiftMode
.NONE
;
4374 currentState
.grLockshift
= LockshiftMode
.NONE
;
4378 // DECKPAM - Keypad application mode
4379 // Note this code overlaps both ANSI and VT52 mode
4383 // DECKPNM - Keypad numeric mode
4384 // Note this code overlaps both ANSI and VT52 mode
4391 if (vt52Mode
== true) {
4392 // Cursor up, and stop at the top without scrolling
4397 if (vt52Mode
== true) {
4398 // Cursor down, and stop at the bottom without scrolling
4399 cursorDown(1, false);
4403 if (vt52Mode
== true) {
4404 // Cursor right, and stop at the right without scrolling
4405 cursorRight(1, false);
4409 if (vt52Mode
== true) {
4410 // Cursor left, and stop at the left without scrolling
4411 cursorLeft(1, false);
4418 if (vt52Mode
== true) {
4426 if (vt52Mode
== true) {
4427 // G0 --> Special graphics
4428 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
4432 if (vt52Mode
== true) {
4434 currentState
.g0Charset
= CharacterSet
.US
;
4438 if (vt52Mode
== true) {
4440 cursorPosition(0, 0);
4442 // HTS - Horizontal tabulation set
4447 if (vt52Mode
== true) {
4448 // Reverse line feed. Same as RI.
4453 if (vt52Mode
== true) {
4454 // Erase to end of screen
4455 eraseLine(currentState
.cursorX
, width
- 1, false);
4456 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4461 if (vt52Mode
== true) {
4462 // Erase to end of line
4463 eraseLine(currentState
.cursorX
, width
- 1, false);
4469 if (vt52Mode
== true) {
4472 // RI - Reverse index
4477 if (vt52Mode
== false) {
4479 singleshift
= Singleshift
.SS2
;
4483 if (vt52Mode
== false) {
4485 singleshift
= Singleshift
.SS3
;
4492 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4508 if (vt52Mode
== true) {
4509 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4517 if (vt52Mode
== true) {
4519 // Send string directly to remote side
4520 writeRemote("\033/Z");
4523 // Send string directly to remote side
4524 writeRemote(deviceTypeResponse());
4535 // VT52 cannot get to any of these other states
4536 if (vt52Mode
== true) {
4541 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4548 // RIS - Reset to initial state
4550 // Do I clear screen too? I think so...
4551 eraseScreen(0, 0, height
- 1, width
- 1, false);
4552 cursorPosition(0, 0);
4566 if ((type
== DeviceType
.VT220
)
4567 || (type
== DeviceType
.XTERM
)) {
4569 // VT220 lockshift G2 into GL
4570 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
4575 if ((type
== DeviceType
.VT220
)
4576 || (type
== DeviceType
.XTERM
)) {
4578 // VT220 lockshift G3 into GL
4579 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
4597 if ((type
== DeviceType
.VT220
)
4598 || (type
== DeviceType
.XTERM
)) {
4600 // VT220 lockshift G3 into GR
4601 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
4606 if ((type
== DeviceType
.VT220
)
4607 || (type
== DeviceType
.XTERM
)) {
4609 // VT220 lockshift G2 into GR
4610 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
4616 if ((type
== DeviceType
.VT220
)
4617 || (type
== DeviceType
.XTERM
)) {
4619 // VT220 lockshift G1 into GR
4620 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
4630 // 0x5B goes to CSI_ENTRY
4632 scanState
= ScanState
.CSI_ENTRY
;
4635 // 0x5D goes to OSC_STRING
4637 scanState
= ScanState
.OSC_STRING
;
4640 // 0x50 goes to DCS_ENTRY
4642 scanState
= ScanState
.DCS_ENTRY
;
4645 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4646 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
4647 scanState
= ScanState
.SOSPMAPC_STRING
;
4652 case ESCAPE_INTERMEDIATE
:
4653 // 00-17, 19, 1C-1F --> execute
4655 handleControlChar(ch
);
4658 // 20-2F --> collect
4659 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4663 // 30-7E --> dispatch, then switch to GROUND
4664 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
4667 if ((collectBuffer
.length() == 1)
4668 && (collectBuffer
.charAt(0) == '(')) {
4669 // G0 --> Special graphics
4670 currentState
.g0Charset
= CharacterSet
.DRAWING
;
4672 if ((collectBuffer
.length() == 1)
4673 && (collectBuffer
.charAt(0) == ')')) {
4674 // G1 --> Special graphics
4675 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4677 if ((type
== DeviceType
.VT220
)
4678 || (type
== DeviceType
.XTERM
)) {
4680 if ((collectBuffer
.length() == 1)
4681 && (collectBuffer
.charAt(0) == '*')) {
4682 // G2 --> Special graphics
4683 currentState
.g2Charset
= CharacterSet
.DRAWING
;
4685 if ((collectBuffer
.length() == 1)
4686 && (collectBuffer
.charAt(0) == '+')) {
4687 // G3 --> Special graphics
4688 currentState
.g3Charset
= CharacterSet
.DRAWING
;
4693 if ((collectBuffer
.length() == 1)
4694 && (collectBuffer
.charAt(0) == '(')) {
4695 // G0 --> Alternate character ROM standard character set
4696 currentState
.g0Charset
= CharacterSet
.ROM
;
4698 if ((collectBuffer
.length() == 1)
4699 && (collectBuffer
.charAt(0) == ')')) {
4700 // G1 --> Alternate character ROM standard character set
4701 currentState
.g1Charset
= CharacterSet
.ROM
;
4705 if ((collectBuffer
.length() == 1)
4706 && (collectBuffer
.charAt(0) == '(')) {
4707 // G0 --> Alternate character ROM special graphics
4708 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
4710 if ((collectBuffer
.length() == 1)
4711 && (collectBuffer
.charAt(0) == ')')) {
4712 // G1 --> Alternate character ROM special graphics
4713 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
4717 if ((collectBuffer
.length() == 1)
4718 && (collectBuffer
.charAt(0) == '#')) {
4719 // DECDHL - Double-height line (top half)
4724 if ((collectBuffer
.length() == 1)
4725 && (collectBuffer
.charAt(0) == '#')) {
4726 // DECDHL - Double-height line (bottom half)
4729 if ((type
== DeviceType
.VT220
)
4730 || (type
== DeviceType
.XTERM
)) {
4732 if ((collectBuffer
.length() == 1)
4733 && (collectBuffer
.charAt(0) == '(')) {
4735 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
4737 if ((collectBuffer
.length() == 1)
4738 && (collectBuffer
.charAt(0) == ')')) {
4740 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
4742 if ((collectBuffer
.length() == 1)
4743 && (collectBuffer
.charAt(0) == '*')) {
4745 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
4747 if ((collectBuffer
.length() == 1)
4748 && (collectBuffer
.charAt(0) == '+')) {
4750 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
4755 if ((collectBuffer
.length() == 1)
4756 && (collectBuffer
.charAt(0) == '#')) {
4757 // DECSWL - Single-width line
4760 if ((type
== DeviceType
.VT220
)
4761 || (type
== DeviceType
.XTERM
)) {
4763 if ((collectBuffer
.length() == 1)
4764 && (collectBuffer
.charAt(0) == '(')) {
4766 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4768 if ((collectBuffer
.length() == 1)
4769 && (collectBuffer
.charAt(0) == ')')) {
4771 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4773 if ((collectBuffer
.length() == 1)
4774 && (collectBuffer
.charAt(0) == '*')) {
4776 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4778 if ((collectBuffer
.length() == 1)
4779 && (collectBuffer
.charAt(0) == '+')) {
4781 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4786 if ((collectBuffer
.length() == 1)
4787 && (collectBuffer
.charAt(0) == '#')) {
4788 // DECDWL - Double-width line
4791 if ((type
== DeviceType
.VT220
)
4792 || (type
== DeviceType
.XTERM
)) {
4794 if ((collectBuffer
.length() == 1)
4795 && (collectBuffer
.charAt(0) == '(')) {
4797 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4799 if ((collectBuffer
.length() == 1)
4800 && (collectBuffer
.charAt(0) == ')')) {
4802 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
4804 if ((collectBuffer
.length() == 1)
4805 && (collectBuffer
.charAt(0) == '*')) {
4807 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
4809 if ((collectBuffer
.length() == 1)
4810 && (collectBuffer
.charAt(0) == '+')) {
4812 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
4817 if ((type
== DeviceType
.VT220
)
4818 || (type
== DeviceType
.XTERM
)) {
4820 if ((collectBuffer
.length() == 1)
4821 && (collectBuffer
.charAt(0) == '(')) {
4823 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
4825 if ((collectBuffer
.length() == 1)
4826 && (collectBuffer
.charAt(0) == ')')) {
4828 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
4830 if ((collectBuffer
.length() == 1)
4831 && (collectBuffer
.charAt(0) == '*')) {
4833 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
4835 if ((collectBuffer
.length() == 1)
4836 && (collectBuffer
.charAt(0) == '+')) {
4838 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
4843 if ((collectBuffer
.length() == 1)
4844 && (collectBuffer
.charAt(0) == '#')) {
4845 // DECALN - Screen alignment display
4854 if ((type
== DeviceType
.VT220
)
4855 || (type
== DeviceType
.XTERM
)) {
4857 if ((collectBuffer
.length() == 1)
4858 && (collectBuffer
.charAt(0) == '(')) {
4859 // G0 --> DEC_SUPPLEMENTAL
4860 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4862 if ((collectBuffer
.length() == 1)
4863 && (collectBuffer
.charAt(0) == ')')) {
4864 // G1 --> DEC_SUPPLEMENTAL
4865 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4867 if ((collectBuffer
.length() == 1)
4868 && (collectBuffer
.charAt(0) == '*')) {
4869 // G2 --> DEC_SUPPLEMENTAL
4870 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4872 if ((collectBuffer
.length() == 1)
4873 && (collectBuffer
.charAt(0) == '+')) {
4874 // G3 --> DEC_SUPPLEMENTAL
4875 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
4880 if ((type
== DeviceType
.VT220
)
4881 || (type
== DeviceType
.XTERM
)) {
4883 if ((collectBuffer
.length() == 1)
4884 && (collectBuffer
.charAt(0) == '(')) {
4886 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
4888 if ((collectBuffer
.length() == 1)
4889 && (collectBuffer
.charAt(0) == ')')) {
4891 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
4893 if ((collectBuffer
.length() == 1)
4894 && (collectBuffer
.charAt(0) == '*')) {
4896 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
4898 if ((collectBuffer
.length() == 1)
4899 && (collectBuffer
.charAt(0) == '+')) {
4901 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
4910 if ((collectBuffer
.length() == 1)
4911 && (collectBuffer
.charAt(0) == '(')) {
4912 // G0 --> United Kingdom set
4913 currentState
.g0Charset
= CharacterSet
.UK
;
4915 if ((collectBuffer
.length() == 1)
4916 && (collectBuffer
.charAt(0) == ')')) {
4917 // G1 --> United Kingdom set
4918 currentState
.g1Charset
= CharacterSet
.UK
;
4920 if ((type
== DeviceType
.VT220
)
4921 || (type
== DeviceType
.XTERM
)) {
4923 if ((collectBuffer
.length() == 1)
4924 && (collectBuffer
.charAt(0) == '*')) {
4925 // G2 --> United Kingdom set
4926 currentState
.g2Charset
= CharacterSet
.UK
;
4928 if ((collectBuffer
.length() == 1)
4929 && (collectBuffer
.charAt(0) == '+')) {
4930 // G3 --> United Kingdom set
4931 currentState
.g3Charset
= CharacterSet
.UK
;
4936 if ((collectBuffer
.length() == 1)
4937 && (collectBuffer
.charAt(0) == '(')) {
4939 currentState
.g0Charset
= CharacterSet
.US
;
4941 if ((collectBuffer
.length() == 1)
4942 && (collectBuffer
.charAt(0) == ')')) {
4944 currentState
.g1Charset
= CharacterSet
.US
;
4946 if ((type
== DeviceType
.VT220
)
4947 || (type
== DeviceType
.XTERM
)) {
4949 if ((collectBuffer
.length() == 1)
4950 && (collectBuffer
.charAt(0) == '*')) {
4952 currentState
.g2Charset
= CharacterSet
.US
;
4954 if ((collectBuffer
.length() == 1)
4955 && (collectBuffer
.charAt(0) == '+')) {
4957 currentState
.g3Charset
= CharacterSet
.US
;
4962 if ((type
== DeviceType
.VT220
)
4963 || (type
== DeviceType
.XTERM
)) {
4965 if ((collectBuffer
.length() == 1)
4966 && (collectBuffer
.charAt(0) == '(')) {
4968 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
4970 if ((collectBuffer
.length() == 1)
4971 && (collectBuffer
.charAt(0) == ')')) {
4973 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
4975 if ((collectBuffer
.length() == 1)
4976 && (collectBuffer
.charAt(0) == '*')) {
4978 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
4980 if ((collectBuffer
.length() == 1)
4981 && (collectBuffer
.charAt(0) == '+')) {
4983 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
4990 if ((type
== DeviceType
.VT220
)
4991 || (type
== DeviceType
.XTERM
)) {
4993 if ((collectBuffer
.length() == 1)
4994 && (collectBuffer
.charAt(0) == '(')) {
4996 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
4998 if ((collectBuffer
.length() == 1)
4999 && (collectBuffer
.charAt(0) == ')')) {
5001 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5003 if ((collectBuffer
.length() == 1)
5004 && (collectBuffer
.charAt(0) == '*')) {
5006 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5008 if ((collectBuffer
.length() == 1)
5009 && (collectBuffer
.charAt(0) == '+')) {
5011 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5016 if ((type
== DeviceType
.VT220
)
5017 || (type
== DeviceType
.XTERM
)) {
5019 if ((collectBuffer
.length() == 1)
5020 && (collectBuffer
.charAt(0) == ' ')) {
5027 if ((type
== DeviceType
.VT220
)
5028 || (type
== DeviceType
.XTERM
)) {
5030 if ((collectBuffer
.length() == 1)
5031 && (collectBuffer
.charAt(0) == ' ')) {
5038 if ((type
== DeviceType
.VT220
)
5039 || (type
== DeviceType
.XTERM
)) {
5041 if ((collectBuffer
.length() == 1)
5042 && (collectBuffer
.charAt(0) == '(')) {
5044 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5046 if ((collectBuffer
.length() == 1)
5047 && (collectBuffer
.charAt(0) == ')')) {
5049 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5051 if ((collectBuffer
.length() == 1)
5052 && (collectBuffer
.charAt(0) == '*')) {
5054 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5056 if ((collectBuffer
.length() == 1)
5057 && (collectBuffer
.charAt(0) == '+')) {
5059 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5067 if ((type
== DeviceType
.VT220
)
5068 || (type
== DeviceType
.XTERM
)) {
5070 if ((collectBuffer
.length() == 1)
5071 && (collectBuffer
.charAt(0) == '(')) {
5073 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5075 if ((collectBuffer
.length() == 1)
5076 && (collectBuffer
.charAt(0) == ')')) {
5078 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5080 if ((collectBuffer
.length() == 1)
5081 && (collectBuffer
.charAt(0) == '*')) {
5083 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5085 if ((collectBuffer
.length() == 1)
5086 && (collectBuffer
.charAt(0) == '+')) {
5088 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5099 if ((type
== DeviceType
.VT220
)
5100 || (type
== DeviceType
.XTERM
)) {
5102 if ((collectBuffer
.length() == 1)
5103 && (collectBuffer
.charAt(0) == '(')) {
5105 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5107 if ((collectBuffer
.length() == 1)
5108 && (collectBuffer
.charAt(0) == ')')) {
5110 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5112 if ((collectBuffer
.length() == 1)
5113 && (collectBuffer
.charAt(0) == '*')) {
5115 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5117 if ((collectBuffer
.length() == 1)
5118 && (collectBuffer
.charAt(0) == '+')) {
5120 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5125 if ((type
== DeviceType
.VT220
)
5126 || (type
== DeviceType
.XTERM
)) {
5128 if ((collectBuffer
.length() == 1)
5129 && (collectBuffer
.charAt(0) == '(')) {
5131 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5133 if ((collectBuffer
.length() == 1)
5134 && (collectBuffer
.charAt(0) == ')')) {
5136 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5138 if ((collectBuffer
.length() == 1)
5139 && (collectBuffer
.charAt(0) == '*')) {
5141 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5143 if ((collectBuffer
.length() == 1)
5144 && (collectBuffer
.charAt(0) == '+')) {
5146 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5158 if ((type
== DeviceType
.VT220
)
5159 || (type
== DeviceType
.XTERM
)) {
5161 if ((collectBuffer
.length() == 1)
5162 && (collectBuffer
.charAt(0) == '(')) {
5164 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5166 if ((collectBuffer
.length() == 1)
5167 && (collectBuffer
.charAt(0) == ')')) {
5169 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5171 if ((collectBuffer
.length() == 1)
5172 && (collectBuffer
.charAt(0) == '*')) {
5174 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5176 if ((collectBuffer
.length() == 1)
5177 && (collectBuffer
.charAt(0) == '+')) {
5179 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5184 if ((type
== DeviceType
.VT220
)
5185 || (type
== DeviceType
.XTERM
)) {
5187 if ((collectBuffer
.length() == 1)
5188 && (collectBuffer
.charAt(0) == '(')) {
5190 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5192 if ((collectBuffer
.length() == 1)
5193 && (collectBuffer
.charAt(0) == ')')) {
5195 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5197 if ((collectBuffer
.length() == 1)
5198 && (collectBuffer
.charAt(0) == '*')) {
5200 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5202 if ((collectBuffer
.length() == 1)
5203 && (collectBuffer
.charAt(0) == '+')) {
5205 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5252 // 0x9C goes to GROUND
5260 // 00-17, 19, 1C-1F --> execute
5262 handleControlChar(ch
);
5265 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5266 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5268 scanState
= ScanState
.CSI_INTERMEDIATE
;
5271 // 30-39, 3B --> param, then switch to CSI_PARAM
5272 if ((ch
>= '0') && (ch
<= '9')) {
5274 scanState
= ScanState
.CSI_PARAM
;
5278 scanState
= ScanState
.CSI_PARAM
;
5281 // 3C-3F --> collect, then switch to CSI_PARAM
5282 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5284 scanState
= ScanState
.CSI_PARAM
;
5287 // 40-7E --> dispatch, then switch to GROUND
5288 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5291 // ICH - Insert character
5299 // CUD - Cursor down
5303 // CUF - Cursor forward
5307 // CUB - Cursor backward
5311 // CNL - Cursor down and to column 1
5312 if (type
== DeviceType
.XTERM
) {
5317 // CPL - Cursor up and to column 1
5318 if (type
== DeviceType
.XTERM
) {
5323 // CHA - Cursor to column # in current row
5324 if (type
== DeviceType
.XTERM
) {
5329 // CUP - Cursor position
5333 // CHT - Cursor forward X tab stops (default 1)
5334 if (type
== DeviceType
.XTERM
) {
5339 // ED - Erase in display
5343 // EL - Erase in line
5358 // DCH - Delete character
5365 // Scroll up X lines (default 1)
5366 if (type
== DeviceType
.XTERM
) {
5371 // Scroll down X lines (default 1)
5372 if (type
== DeviceType
.XTERM
) {
5381 if ((type
== DeviceType
.VT220
)
5382 || (type
== DeviceType
.XTERM
)) {
5384 // ECH - Erase character
5391 // CBT - Cursor backward X tab stops (default 1)
5392 if (type
== DeviceType
.XTERM
) {
5403 // HPA - Cursor to column # in current row. Same as CHA
5404 if (type
== DeviceType
.XTERM
) {
5409 // HPR - Cursor right. Same as CUF
5410 if (type
== DeviceType
.XTERM
) {
5415 // REP - Repeat last char X times
5416 if (type
== DeviceType
.XTERM
) {
5421 // DA - Device attributes
5425 // VPA - Cursor to row, current column.
5426 if (type
== DeviceType
.XTERM
) {
5431 // VPR - Cursor down. Same as CUD
5432 if (type
== DeviceType
.XTERM
) {
5437 // HVP - Horizontal and vertical position
5441 // TBC - Tabulation clear
5445 // Sets an ANSI or DEC private toggle
5449 if ((type
== DeviceType
.VT220
)
5450 || (type
== DeviceType
.XTERM
)) {
5452 // Printer functions
5460 // Sets an ANSI or DEC private toggle
5464 // SGR - Select graphics rendition
5468 // DSR - Device status report
5475 // DECLL - Load leds
5479 // DECSTBM - Set top and bottom margins
5483 // Save cursor (ANSI.SYS)
5484 if (type
== DeviceType
.XTERM
) {
5485 savedState
.cursorX
= currentState
.cursorX
;
5486 savedState
.cursorY
= currentState
.cursorY
;
5492 // Restore cursor (ANSI.SYS)
5493 if (type
== DeviceType
.XTERM
) {
5494 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5501 // DECREQTPARM - Request terminal parameters
5517 // 0x9C goes to GROUND
5522 // 0x3A goes to CSI_IGNORE
5524 scanState
= ScanState
.CSI_IGNORE
;
5529 // 00-17, 19, 1C-1F --> execute
5531 handleControlChar(ch
);
5534 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5535 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5537 scanState
= ScanState
.CSI_INTERMEDIATE
;
5540 // 30-39, 3B --> param
5541 if ((ch
>= '0') && (ch
<= '9')) {
5548 // 0x3A goes to CSI_IGNORE
5550 scanState
= ScanState
.CSI_IGNORE
;
5552 // 0x3C-3F goes to CSI_IGNORE
5553 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5554 scanState
= ScanState
.CSI_IGNORE
;
5557 // 40-7E --> dispatch, then switch to GROUND
5558 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5561 // ICH - Insert character
5569 // CUD - Cursor down
5573 // CUF - Cursor forward
5577 // CUB - Cursor backward
5581 // CNL - Cursor down and to column 1
5582 if (type
== DeviceType
.XTERM
) {
5587 // CPL - Cursor up and to column 1
5588 if (type
== DeviceType
.XTERM
) {
5593 // CHA - Cursor to column # in current row
5594 if (type
== DeviceType
.XTERM
) {
5599 // CUP - Cursor position
5603 // CHT - Cursor forward X tab stops (default 1)
5604 if (type
== DeviceType
.XTERM
) {
5609 // ED - Erase in display
5613 // EL - Erase in line
5628 // DCH - Delete character
5635 // Scroll up X lines (default 1)
5636 if (type
== DeviceType
.XTERM
) {
5641 // Scroll down X lines (default 1)
5642 if (type
== DeviceType
.XTERM
) {
5651 if ((type
== DeviceType
.VT220
)
5652 || (type
== DeviceType
.XTERM
)) {
5654 // ECH - Erase character
5661 // CBT - Cursor backward X tab stops (default 1)
5662 if (type
== DeviceType
.XTERM
) {
5673 // HPA - Cursor to column # in current row. Same as CHA
5674 if (type
== DeviceType
.XTERM
) {
5679 // HPR - Cursor right. Same as CUF
5680 if (type
== DeviceType
.XTERM
) {
5685 // REP - Repeat last char X times
5686 if (type
== DeviceType
.XTERM
) {
5691 // DA - Device attributes
5695 // VPA - Cursor to row, current column.
5696 if (type
== DeviceType
.XTERM
) {
5701 // VPR - Cursor down. Same as CUD
5702 if (type
== DeviceType
.XTERM
) {
5707 // HVP - Horizontal and vertical position
5711 // TBC - Tabulation clear
5715 // Sets an ANSI or DEC private toggle
5719 if ((type
== DeviceType
.VT220
)
5720 || (type
== DeviceType
.XTERM
)) {
5722 // Printer functions
5730 // Sets an ANSI or DEC private toggle
5734 // SGR - Select graphics rendition
5738 // DSR - Device status report
5745 // DECLL - Load leds
5749 // DECSTBM - Set top and bottom margins
5759 // DECREQTPARM - Request terminal parameters
5776 case CSI_INTERMEDIATE
:
5777 // 00-17, 19, 1C-1F --> execute
5779 handleControlChar(ch
);
5782 // 20-2F --> collect
5783 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5787 // 0x30-3F goes to CSI_IGNORE
5788 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5789 scanState
= ScanState
.CSI_IGNORE
;
5792 // 40-7E --> dispatch, then switch to GROUND
5793 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5845 if (((type
== DeviceType
.VT220
)
5846 || (type
== DeviceType
.XTERM
))
5847 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5849 // DECSCL - compatibility level
5852 if ((type
== DeviceType
.XTERM
)
5853 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
5855 // DECSTR - Soft terminal reset
5860 if (((type
== DeviceType
.VT220
)
5861 || (type
== DeviceType
.XTERM
))
5862 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
5890 // 00-17, 19, 1C-1F --> execute
5892 handleControlChar(ch
);
5895 // 20-2F --> collect
5896 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5900 // 40-7E --> ignore, then switch to GROUND
5901 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5905 // 20-3F, 7F --> ignore
5911 // 0x9C goes to GROUND
5916 // 0x1B 0x5C goes to GROUND
5921 if ((collectBuffer
.length() > 0)
5922 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5928 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5929 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5931 scanState
= ScanState
.DCS_INTERMEDIATE
;
5934 // 30-39, 3B --> param, then switch to DCS_PARAM
5935 if ((ch
>= '0') && (ch
<= '9')) {
5937 scanState
= ScanState
.DCS_PARAM
;
5941 scanState
= ScanState
.DCS_PARAM
;
5944 // 3C-3F --> collect, then switch to DCS_PARAM
5945 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5947 scanState
= ScanState
.DCS_PARAM
;
5950 // 00-17, 19, 1C-1F, 7F --> ignore
5952 // 0x3A goes to DCS_IGNORE
5954 scanState
= ScanState
.DCS_IGNORE
;
5957 // 0x40-7E goes to DCS_PASSTHROUGH
5958 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5959 scanState
= ScanState
.DCS_PASSTHROUGH
;
5963 case DCS_INTERMEDIATE
:
5965 // 0x9C goes to GROUND
5970 // 0x1B 0x5C goes to GROUND
5975 if ((collectBuffer
.length() > 0)
5976 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
5982 // 0x30-3F goes to DCS_IGNORE
5983 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
5984 scanState
= ScanState
.DCS_IGNORE
;
5987 // 0x40-7E goes to DCS_PASSTHROUGH
5988 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5989 scanState
= ScanState
.DCS_PASSTHROUGH
;
5992 // 00-17, 19, 1C-1F, 7F --> ignore
5997 // 0x9C goes to GROUND
6002 // 0x1B 0x5C goes to GROUND
6007 if ((collectBuffer
.length() > 0)
6008 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6014 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6015 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6017 scanState
= ScanState
.DCS_INTERMEDIATE
;
6020 // 30-39, 3B --> param
6021 if ((ch
>= '0') && (ch
<= '9')) {
6028 // 00-17, 19, 1C-1F, 7F --> ignore
6030 // 0x3A, 3C-3F goes to DCS_IGNORE
6032 scanState
= ScanState
.DCS_IGNORE
;
6034 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6035 scanState
= ScanState
.DCS_IGNORE
;
6038 // 0x40-7E goes to DCS_PASSTHROUGH
6039 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6040 scanState
= ScanState
.DCS_PASSTHROUGH
;
6044 case DCS_PASSTHROUGH
:
6045 // 0x9C goes to GROUND
6050 // 0x1B 0x5C goes to GROUND
6055 if ((collectBuffer
.length() > 0)
6056 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6062 // 00-17, 19, 1C-1F, 20-7E --> put
6070 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6073 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6082 // 00-17, 19, 1C-1F, 20-7F --> ignore
6084 // 0x9C goes to GROUND
6091 case SOSPMAPC_STRING
:
6092 // 00-17, 19, 1C-1F, 20-7F --> ignore
6094 // 0x9C goes to GROUND
6102 // Special case for Xterm: OSC can pass control characters
6103 if ((ch
== 0x9C) || (ch
<= 0x07)) {
6107 // 00-17, 19, 1C-1F --> ignore
6109 // 20-7F --> osc_put
6110 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6114 // 0x9C goes to GROUND
6121 case VT52_DIRECT_CURSOR_ADDRESS
:
6122 // This is a special case for the VT52 sequence "ESC Y l c"
6123 if (collectBuffer
.length() == 0) {
6125 } else if (collectBuffer
.length() == 1) {
6126 // We've got the two characters, one in the buffer and the
6128 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
6137 * Expose current cursor X to outside world.
6139 * @return current cursor X
6141 public final int getCursorX() {
6142 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6143 return currentState
.cursorX
* 2;
6145 return currentState
.cursorX
;
6149 * Expose current cursor Y to outside world.
6151 * @return current cursor Y
6153 public final int getCursorY() {
6154 return currentState
.cursorY
;