2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.tterminal
;
31 import java
.awt
.Graphics2D
;
32 import java
.awt
.image
.BufferedImage
;
33 import java
.io
.BufferedOutputStream
;
34 import java
.io
.CharArrayWriter
;
35 import java
.io
.InputStream
;
36 import java
.io
.InputStreamReader
;
37 import java
.io
.IOException
;
38 import java
.io
.OutputStream
;
39 import java
.io
.OutputStreamWriter
;
40 import java
.io
.PrintWriter
;
41 import java
.io
.Reader
;
42 import java
.io
.UnsupportedEncodingException
;
43 import java
.io
.Writer
;
44 import java
.util
.ArrayList
;
45 import java
.util
.Collections
;
46 import java
.util
.HashMap
;
47 import java
.util
.List
;
49 import jexer
.TKeypress
;
50 import jexer
.event
.TMouseEvent
;
51 import jexer
.bits
.Color
;
52 import jexer
.bits
.Cell
;
53 import jexer
.bits
.CellAttributes
;
54 import jexer
.io
.ReadTimeoutException
;
55 import jexer
.io
.TimeoutInputStream
;
56 import static jexer
.TKeypress
.*;
59 * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
60 * including a scrollback buffer.
63 * It currently implements VT100, VT102, VT220, and XTERM with the following
67 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
68 * failure with XTERM. This is due to vttest failing to decode the UTF-8
72 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
73 * from VT100 are not supported.
76 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
77 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
78 * because DECSCL is not supported, it will fail the last part of the
79 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
82 * - Numeric/application keys from the number pad are not supported because
83 * they are not exposed from the TKeypress API.
86 * - VT52 HOLD SCREEN mode is not supported.
89 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
90 * numerators) are not rendered correctly.
93 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
95 public class ECMA48
implements Runnable
{
97 // ------------------------------------------------------------------------
98 // Constants --------------------------------------------------------------
99 // ------------------------------------------------------------------------
102 * The emulator can emulate several kinds of terminals.
104 public enum DeviceType
{
106 * DEC VT100 but also including the three VT102 functions.
127 * Parser character scan states.
129 private enum ScanState
{
145 VT52_DIRECT_CURSOR_ADDRESS
149 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
150 * can't really use it in keypress() because we do not see number pad
151 * events from TKeypress.
153 private enum KeypadMode
{
159 * Arrow keys can emit three different sequences (DECCKM or VT52
162 private enum ArrowKeyMode
{
169 * Available character sets for GL, GR, G0, G1, G2, G3.
171 private enum CharacterSet
{
192 * Single-shift states used by the C1 control characters SS2 (0x8E) and
195 private enum Singleshift
{
202 * VT220+ lockshift states.
204 private enum LockshiftMode
{
214 * XTERM mouse reporting protocols.
216 public enum MouseProtocol
{
225 * XTERM mouse reporting encodings.
227 private enum MouseEncoding
{
233 // ------------------------------------------------------------------------
234 // Variables --------------------------------------------------------------
235 // ------------------------------------------------------------------------
238 * The enclosing listening object.
240 private DisplayListener displayListener
;
243 * When true, the reader thread is expected to exit.
245 private volatile boolean stopReaderThread
= false;
250 private Thread readerThread
= null;
253 * The type of emulator to be.
255 private DeviceType type
= DeviceType
.VT102
;
258 * The scrollback buffer characters + attributes.
260 private volatile ArrayList
<DisplayLine
> scrollback
;
263 * The raw display buffer characters + attributes.
265 private volatile ArrayList
<DisplayLine
> display
;
268 * The maximum number of lines in the scrollback buffer.
270 private int maxScrollback
= 10000;
273 * The terminal's input. For type == XTERM, this is an InputStreamReader
274 * with UTF-8 encoding.
276 private Reader input
;
279 * The terminal's raw InputStream. This is used for type != XTERM.
281 private volatile TimeoutInputStream inputStream
;
284 * The terminal's output. For type == XTERM, this wraps an
285 * OutputStreamWriter with UTF-8 encoding.
287 private Writer output
;
290 * The terminal's raw OutputStream. This is used for type != XTERM.
292 private OutputStream outputStream
;
295 * Current scanning state.
297 private ScanState scanState
;
300 * Which mouse protocol is active.
302 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
305 * Which mouse encoding is active.
307 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
310 * A terminal may request that the mouse pointer be hidden using a
311 * Privacy Message containing either "hideMousePointer" or
312 * "showMousePointer". This is currently only used within Jexer by
313 * TTerminalWindow so that only the bottom-most instance of nested
314 * Jexer's draws the mouse within its application window.
316 private boolean hideMousePointer
= false;
319 * Physical display width. We start at 80x24, but the user can resize us
325 * Physical display height. We start at 80x24, but the user can resize
331 * Top margin of the scrolling region.
333 private int scrollRegionTop
;
336 * Bottom margin of the scrolling region.
338 private int scrollRegionBottom
;
341 * Right margin column number. This can be selected by the remote side
342 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
344 private int rightMargin
;
347 * Last character printed.
352 * VT100-style line wrapping: a character is placed in column 80 (or
353 * 132), but the line does NOT wrap until another character is written to
354 * column 1 of the next line, after which the cursor moves to column 2.
356 private boolean wrapLineFlag
;
359 * VT220 single shift flag.
361 private Singleshift singleshift
= Singleshift
.NONE
;
364 * true = insert characters, false = overwrite.
366 private boolean insertMode
= false;
369 * VT52 mode as selected by DECANM. True means VT52, false means
370 * ANSI. Default is ANSI.
372 private boolean vt52Mode
= false;
375 * Visible cursor (DECTCEM).
377 private boolean cursorVisible
= true;
380 * Screen title as set by the xterm OSC sequence. Lots of applications
381 * send a screenTitle regardless of whether it is an xterm client or not.
383 private String screenTitle
= "";
386 * Parameter characters being collected.
388 private List
<Integer
> csiParams
;
391 * Non-csi collect buffer.
393 private StringBuilder collectBuffer
;
396 * When true, use the G1 character set.
398 private boolean shiftOut
= false;
401 * Horizontal tab stop locations.
403 private List
<Integer
> tabStops
;
406 * S8C1T. True means 8bit controls, false means 7bit controls.
408 private boolean s8c1t
= false;
411 * Printer mode. True means send all output to printer, which discards
414 private boolean printerControllerMode
= false;
417 * LMN line mode. If true, linefeed() puts the cursor on the first
418 * column of the next line. If false, linefeed() puts the cursor one
419 * line down on the current line. The default is false.
421 private boolean newLineMode
= false;
424 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
426 private ArrowKeyMode arrowKeyMode
;
429 * Whether number pad keys send VT100 or VT52, application or numeric
432 @SuppressWarnings("unused")
433 private KeypadMode keypadMode
;
436 * When true, the terminal is in 132-column mode (DECCOLM).
438 private boolean columns132
= false;
441 * true = reverse video. Set by DECSCNM.
443 private boolean reverseVideo
= false;
446 * false = echo characters locally.
448 private boolean fullDuplex
= true;
451 * The current terminal state.
453 private SaveableState currentState
;
456 * The last saved terminal state.
458 private SaveableState savedState
;
461 * The 88- or 256-color support RGB colors.
463 private List
<Integer
> colors88
;
466 * Sixel collection buffer.
468 private StringBuilder sixelParseBuffer
;
471 * The width of a character cell in pixels.
473 private int textWidth
= 16;
476 * The height of a character cell in pixels.
478 private int textHeight
= 20;
481 * DECSC/DECRC save/restore a subset of the total state. This class
482 * encapsulates those specific flags/modes.
484 private class SaveableState
{
487 * When true, cursor positions are relative to the scrolling region.
489 public boolean originMode
= false;
492 * The current editing X position.
494 public int cursorX
= 0;
497 * The current editing Y position.
499 public int cursorY
= 0;
502 * Which character set is currently selected in G0.
504 public CharacterSet g0Charset
= CharacterSet
.US
;
507 * Which character set is currently selected in G1.
509 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
512 * Which character set is currently selected in G2.
514 public CharacterSet g2Charset
= CharacterSet
.US
;
517 * Which character set is currently selected in G3.
519 public CharacterSet g3Charset
= CharacterSet
.US
;
522 * Which character set is currently selected in GR.
524 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
527 * The current drawing attributes.
529 public CellAttributes attr
;
534 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
539 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
544 public boolean lineWrap
= true;
549 public void reset() {
553 g0Charset
= CharacterSet
.US
;
554 g1Charset
= CharacterSet
.DRAWING
;
555 g2Charset
= CharacterSet
.US
;
556 g3Charset
= CharacterSet
.US
;
557 grCharset
= CharacterSet
.DRAWING
;
558 attr
= new CellAttributes();
559 glLockshift
= LockshiftMode
.NONE
;
560 grLockshift
= LockshiftMode
.NONE
;
565 * Copy attributes from another instance.
567 * @param that the other instance to match
569 public void setTo(final SaveableState that
) {
570 this.originMode
= that
.originMode
;
571 this.cursorX
= that
.cursorX
;
572 this.cursorY
= that
.cursorY
;
573 this.g0Charset
= that
.g0Charset
;
574 this.g1Charset
= that
.g1Charset
;
575 this.g2Charset
= that
.g2Charset
;
576 this.g3Charset
= that
.g3Charset
;
577 this.grCharset
= that
.grCharset
;
578 this.attr
= new CellAttributes();
579 this.attr
.setTo(that
.attr
);
580 this.glLockshift
= that
.glLockshift
;
581 this.grLockshift
= that
.grLockshift
;
582 this.lineWrap
= that
.lineWrap
;
586 * Public constructor.
588 public SaveableState() {
593 // ------------------------------------------------------------------------
594 // Constructors -----------------------------------------------------------
595 // ------------------------------------------------------------------------
598 * Public constructor.
600 * @param type one of the DeviceType constants to select VT100, VT102,
602 * @param inputStream an InputStream connected to the remote side. For
603 * type == XTERM, inputStream is converted to a Reader with UTF-8
605 * @param outputStream an OutputStream connected to the remote user. For
606 * type == XTERM, outputStream is converted to a Writer with UTF-8
608 * @param displayListener a callback to the outer display, or null for
609 * default VT100 behavior
610 * @throws UnsupportedEncodingException if an exception is thrown when
611 * creating the InputStreamReader
613 public ECMA48(final DeviceType type
, final InputStream inputStream
,
614 final OutputStream outputStream
, final DisplayListener displayListener
)
615 throws UnsupportedEncodingException
{
617 assert (inputStream
!= null);
618 assert (outputStream
!= null);
620 csiParams
= new ArrayList
<Integer
>();
621 tabStops
= new ArrayList
<Integer
>();
622 scrollback
= new ArrayList
<DisplayLine
>();
623 display
= new ArrayList
<DisplayLine
>();
626 if (inputStream
instanceof TimeoutInputStream
) {
627 this.inputStream
= (TimeoutInputStream
)inputStream
;
629 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
631 if (type
== DeviceType
.XTERM
) {
632 this.input
= new InputStreamReader(this.inputStream
, "UTF-8");
633 this.output
= new OutputStreamWriter(new
634 BufferedOutputStream(outputStream
), "UTF-8");
635 this.outputStream
= null;
638 this.outputStream
= new BufferedOutputStream(outputStream
);
640 this.displayListener
= displayListener
;
643 for (int i
= 0; i
< height
; i
++) {
644 display
.add(new DisplayLine(currentState
.attr
));
647 // Spin up the input reader
648 readerThread
= new Thread(this);
649 readerThread
.start();
652 // ------------------------------------------------------------------------
653 // Runnable ---------------------------------------------------------------
654 // ------------------------------------------------------------------------
657 * Read function runs on a separate thread.
659 public final void run() {
660 boolean utf8
= false;
661 boolean done
= false;
663 if (type
== DeviceType
.XTERM
) {
667 // available() will often return > 1, so we need to read in chunks to
669 char [] readBufferUTF8
= null;
670 byte [] readBuffer
= null;
672 readBufferUTF8
= new char[128];
674 readBuffer
= new byte[128];
677 while (!done
&& !stopReaderThread
) {
679 int n
= inputStream
.available();
681 // System.err.printf("available() %d\n", n); System.err.flush();
683 if (readBufferUTF8
.length
< n
) {
684 // The buffer wasn't big enough, make it huger
685 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
688 readBufferUTF8
= new char[newSizeHalf
* 2];
691 if (readBuffer
.length
< n
) {
692 // The buffer wasn't big enough, make it huger
693 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
694 readBuffer
= new byte[newSizeHalf
* 2];
700 } catch (InterruptedException e
) {
709 rc
= input
.read(readBufferUTF8
, 0,
710 readBufferUTF8
.length
);
712 rc
= inputStream
.read(readBuffer
, 0,
715 } catch (ReadTimeoutException e
) {
719 // System.err.printf("read() %d\n", rc); System.err.flush();
724 // Don't step on UI events
725 synchronized (this) {
726 for (int i
= 0; i
< rc
; i
++) {
729 ch
= readBufferUTF8
[i
];
737 // Permit my enclosing UI to know that I updated.
738 if (displayListener
!= null) {
739 displayListener
.displayChanged();
742 // System.err.println("end while loop"); System.err.flush();
743 } catch (IOException e
) {
746 // This is an unusual case. We want to see the stack trace,
747 // but it is related to the spawned process rather than the
748 // actual UI. We will generate the stack trace, and consume
749 // it as though it was emitted by the shell.
750 CharArrayWriter writer
= new CharArrayWriter();
751 // Send a ST and RIS to clear the emulator state.
753 writer
.write("\033\\\033c");
754 writer
.write("\n-----------------------------------\n");
755 e
.printStackTrace(new PrintWriter(writer
));
756 writer
.write("\n-----------------------------------\n");
757 } catch (IOException e2
) {
760 char [] stackTrace
= writer
.toCharArray();
761 for (int i
= 0; i
< stackTrace
.length
; i
++) {
762 if (stackTrace
[i
] == '\n') {
765 consume(stackTrace
[i
]);
769 } // while ((done == false) && (stopReaderThread == false))
771 // Let the rest of the world know that I am done.
772 stopReaderThread
= true;
775 inputStream
.cancelRead();
778 } catch (IOException e
) {
784 } catch (IOException e
) {
788 // Permit my enclosing UI to know that I updated.
789 if (displayListener
!= null) {
790 displayListener
.displayChanged();
793 // System.err.println("*** run() exiting..."); System.err.flush();
796 // ------------------------------------------------------------------------
797 // ECMA48 -----------------------------------------------------------------
798 // ------------------------------------------------------------------------
801 * Return the proper primary Device Attributes string.
803 * @return string to send to remote side that is appropriate for the
806 private String
deviceTypeResponse() {
809 // "I am a VT100 with advanced video option" (often VT102)
818 // "I am a VT220" - 7 bit version
820 return "\033[?62;1;6c";
822 // "I am a VT220" - 8 bit version
823 return "\u009b?62;1;6c";
825 throw new IllegalArgumentException("Invalid device type: " + type
);
830 * Return the proper TERM environment variable for this device type.
832 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
833 * @return "vt100", "xterm", etc.
835 public static String
deviceTypeTerm(final DeviceType deviceType
) {
836 switch (deviceType
) {
850 throw new IllegalArgumentException("Invalid device type: "
856 * Return the proper LANG for this device type. Only XTERM devices know
857 * about UTF-8, the others are defined by their standard to be either
858 * 7-bit or 8-bit characters only.
860 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
861 * @param baseLang a base language without UTF-8 flag such as "C" or
863 * @return "en_US", "en_US.UTF-8", etc.
865 public static String
deviceTypeLang(final DeviceType deviceType
,
866 final String baseLang
) {
868 switch (deviceType
) {
876 return baseLang
+ ".UTF-8";
879 throw new IllegalArgumentException("Invalid device type: "
885 * Write a string directly to the remote side.
887 * @param str string to send
889 public void writeRemote(final String str
) {
890 if (stopReaderThread
) {
891 // Reader hit EOF, bail out now.
896 // System.err.printf("writeRemote() '%s'\n", str);
902 if (outputStream
== null) {
906 outputStream
.flush();
907 for (int i
= 0; i
< str
.length(); i
++) {
908 outputStream
.write(str
.charAt(i
));
910 outputStream
.flush();
911 } catch (IOException e
) {
917 if (output
== null) {
924 } catch (IOException e
) {
930 throw new IllegalArgumentException("Invalid device type: " + type
);
935 * Close the input and output streams and stop the reader thread. Note
936 * that it is safe to call this multiple times.
938 public final void close() {
940 // Tell the reader thread to stop looking at input. It will close
941 // the input streams.
942 if (stopReaderThread
== false) {
943 stopReaderThread
= true;
945 readerThread
.join(1000);
946 } catch (InterruptedException e
) {
951 // Now close the output stream.
956 if (outputStream
!= null) {
958 outputStream
.close();
959 } catch (IOException e
) {
966 if (outputStream
!= null) {
968 outputStream
.close();
969 } catch (IOException e
) {
974 if (output
!= null) {
977 } catch (IOException e
) {
984 throw new IllegalArgumentException("Invalid device type: " +
990 * See if the reader thread is still running.
992 * @return if true, we are still connected to / reading from the remote
995 public final boolean isReading() {
996 return (!stopReaderThread
);
1000 * Obtain a new blank display line for an external user
1001 * (e.g. TTerminalWindow).
1003 * @return new blank line
1005 public final DisplayLine
getBlankDisplayLine() {
1006 return new DisplayLine(currentState
.attr
);
1010 * Get the scrollback buffer.
1012 * @return the scrollback buffer
1014 public final List
<DisplayLine
> getScrollbackBuffer() {
1019 * Get the display buffer.
1021 * @return the display buffer
1023 public final List
<DisplayLine
> getDisplayBuffer() {
1028 * Get the display width.
1030 * @return the width (usually 80 or 132)
1032 public final int getWidth() {
1037 * Set the display width.
1039 * @param width the new width
1041 public final void setWidth(final int width
) {
1043 rightMargin
= width
- 1;
1044 if (currentState
.cursorX
>= width
) {
1045 currentState
.cursorX
= width
- 1;
1047 if (savedState
.cursorX
>= width
) {
1048 savedState
.cursorX
= width
- 1;
1053 * Get the display height.
1055 * @return the height (usually 24)
1057 public final int getHeight() {
1062 * Set the display height.
1064 * @param height the new height
1066 public final void setHeight(final int height
) {
1067 int delta
= height
- this.height
;
1068 this.height
= height
;
1069 scrollRegionBottom
+= delta
;
1070 if (scrollRegionBottom
< 0) {
1071 scrollRegionBottom
= height
;
1073 if (scrollRegionTop
>= scrollRegionBottom
) {
1074 scrollRegionTop
= 0;
1076 if (currentState
.cursorY
>= height
) {
1077 currentState
.cursorY
= height
- 1;
1079 if (savedState
.cursorY
>= height
) {
1080 savedState
.cursorY
= height
- 1;
1082 while (display
.size() < height
) {
1083 DisplayLine line
= new DisplayLine(currentState
.attr
);
1084 line
.setReverseColor(reverseVideo
);
1087 while (display
.size() > height
) {
1088 scrollback
.add(display
.remove(0));
1093 * Get visible cursor flag.
1095 * @return if true, the cursor is visible
1097 public final boolean isCursorVisible() {
1098 return cursorVisible
;
1102 * Get the screen title as set by the xterm OSC sequence. Lots of
1103 * applications send a screenTitle regardless of whether it is an xterm
1106 * @return screen title
1108 public final String
getScreenTitle() {
1113 * Get 132 columns value.
1115 * @return if true, the terminal is in 132 column mode
1117 public final boolean isColumns132() {
1122 * Clear the CSI parameters and flags.
1124 private void toGround() {
1126 collectBuffer
= new StringBuilder(8);
1127 scanState
= ScanState
.GROUND
;
1131 * Reset the tab stops list.
1133 private void resetTabStops() {
1135 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
1136 tabStops
.add(Integer
.valueOf(i
* 8));
1141 * Reset the 88- or 256-colors.
1143 private void resetColors() {
1144 colors88
= new ArrayList
<Integer
>(256);
1145 for (int i
= 0; i
< 256; i
++) {
1149 // Set default system colors.
1150 colors88
.set(0, 0x00000000);
1151 colors88
.set(1, 0x00a80000);
1152 colors88
.set(2, 0x0000a800);
1153 colors88
.set(3, 0x00a85400);
1154 colors88
.set(4, 0x000000a8);
1155 colors88
.set(5, 0x00a800a8);
1156 colors88
.set(6, 0x0000a8a8);
1157 colors88
.set(7, 0x00a8a8a8);
1159 colors88
.set(8, 0x00545454);
1160 colors88
.set(9, 0x00fc5454);
1161 colors88
.set(10, 0x0054fc54);
1162 colors88
.set(11, 0x00fcfc54);
1163 colors88
.set(12, 0x005454fc);
1164 colors88
.set(13, 0x00fc54fc);
1165 colors88
.set(14, 0x0054fcfc);
1166 colors88
.set(15, 0x00fcfcfc);
1170 * Get the RGB value of one of the indexed colors.
1172 * @param index the color index
1173 * @return the RGB value
1175 private int get88Color(final int index
) {
1176 // System.err.print("get88Color: " + index);
1177 if ((index
< 0) || (index
> colors88
.size())) {
1178 // System.err.println(" -- UNKNOWN");
1181 // System.err.printf(" %08x\n", colors88.get(index));
1182 return colors88
.get(index
);
1186 * Set one of the indexed colors to a color specification.
1188 * @param index the color index
1189 * @param spec the specification, typically something like "rgb:aa/bb/cc"
1191 private void set88Color(final int index
, final String spec
) {
1192 // System.err.println("set88Color: " + index + " '" + spec + "'");
1194 if ((index
< 0) || (index
> colors88
.size())) {
1197 if (spec
.startsWith("rgb:")) {
1198 String
[] rgbTokens
= spec
.substring(4).split("/");
1199 if (rgbTokens
.length
== 3) {
1201 int rgb
= (Integer
.parseInt(rgbTokens
[0], 16) << 16);
1202 rgb
|= Integer
.parseInt(rgbTokens
[1], 16) << 8;
1203 rgb
|= Integer
.parseInt(rgbTokens
[2], 16);
1204 // System.err.printf(" set to %08x\n", rgb);
1205 colors88
.set(index
, rgb
);
1206 } catch (NumberFormatException e
) {
1213 if (spec
.toLowerCase().equals("black")) {
1214 colors88
.set(index
, 0x00000000);
1215 } else if (spec
.toLowerCase().equals("red")) {
1216 colors88
.set(index
, 0x00a80000);
1217 } else if (spec
.toLowerCase().equals("green")) {
1218 colors88
.set(index
, 0x0000a800);
1219 } else if (spec
.toLowerCase().equals("yellow")) {
1220 colors88
.set(index
, 0x00a85400);
1221 } else if (spec
.toLowerCase().equals("blue")) {
1222 colors88
.set(index
, 0x000000a8);
1223 } else if (spec
.toLowerCase().equals("magenta")) {
1224 colors88
.set(index
, 0x00a800a8);
1225 } else if (spec
.toLowerCase().equals("cyan")) {
1226 colors88
.set(index
, 0x0000a8a8);
1227 } else if (spec
.toLowerCase().equals("white")) {
1228 colors88
.set(index
, 0x00a8a8a8);
1234 * Reset the emulation state.
1236 private void reset() {
1238 currentState
= new SaveableState();
1239 savedState
= new SaveableState();
1240 scanState
= ScanState
.GROUND
;
1243 scrollRegionTop
= 0;
1244 scrollRegionBottom
= height
- 1;
1245 rightMargin
= width
- 1;
1246 newLineMode
= false;
1247 arrowKeyMode
= ArrowKeyMode
.ANSI
;
1248 keypadMode
= KeypadMode
.Numeric
;
1249 wrapLineFlag
= false;
1250 if (displayListener
!= null) {
1251 width
= displayListener
.getDisplayWidth();
1252 height
= displayListener
.getDisplayHeight();
1253 rightMargin
= width
- 1;
1261 newLineMode
= false;
1262 reverseVideo
= false;
1264 cursorVisible
= true;
1267 singleshift
= Singleshift
.NONE
;
1269 printerControllerMode
= false;
1272 mouseProtocol
= MouseProtocol
.OFF
;
1273 mouseEncoding
= MouseEncoding
.X10
;
1278 // Reset extra colors
1286 * Append a new line to the bottom of the display, adding lines off the
1287 * top to the scrollback buffer.
1289 private void newDisplayLine() {
1290 // Scroll the top line off into the scrollback buffer
1291 scrollback
.add(display
.get(0));
1292 if (scrollback
.size() > maxScrollback
) {
1293 scrollback
.remove(0);
1294 scrollback
.trimToSize();
1297 display
.trimToSize();
1298 DisplayLine line
= new DisplayLine(currentState
.attr
);
1299 line
.setReverseColor(reverseVideo
);
1304 * Wraps the current line.
1306 private void wrapCurrentLine() {
1307 if (currentState
.cursorY
== height
- 1) {
1310 if (currentState
.cursorY
< height
- 1) {
1311 currentState
.cursorY
++;
1313 currentState
.cursorX
= 0;
1317 * Handle a carriage return.
1319 private void carriageReturn() {
1320 currentState
.cursorX
= 0;
1321 wrapLineFlag
= false;
1325 * Reverse the color of the visible display.
1327 private void invertDisplayColors() {
1328 for (DisplayLine line
: display
) {
1329 line
.setReverseColor(!line
.isReverseColor());
1334 * Handle a linefeed.
1336 private void linefeed() {
1338 if (currentState
.cursorY
< scrollRegionBottom
) {
1339 // Increment screen y
1340 currentState
.cursorY
++;
1343 // Screen y does not increment
1346 * Two cases: either we're inside a scrolling region or not. If
1347 * the scrolling region bottom is the bottom of the screen, then
1348 * push the top line into the buffer. Else scroll the scrolling
1351 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1353 // We're at the bottom of the scroll region, AND the scroll
1354 // region is the entire screen.
1360 // We're at the bottom of the scroll region, AND the scroll
1361 // region is NOT the entire screen.
1362 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1367 currentState
.cursorX
= 0;
1369 wrapLineFlag
= false;
1373 * Prints one character to the display buffer.
1375 * @param ch character to display
1377 private void printCharacter(final char ch
) {
1378 int rightMargin
= this.rightMargin
;
1380 // Check if we have double-width, and if so chop at 40/66 instead of
1382 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1383 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1386 // Check the unusually-complicated line wrapping conditions...
1387 if (currentState
.cursorX
== rightMargin
) {
1389 if (currentState
.lineWrap
== true) {
1391 * This case happens when: the cursor was already on the
1392 * right margin (either through printing or by an explicit
1393 * placement command), and a character was printed.
1395 * The line wraps only when a new character arrives AND the
1396 * cursor is already on the right margin AND has placed a
1397 * character in its cell. Easier to see than to explain.
1399 if (wrapLineFlag
== false) {
1401 * This block marks the case that we are in the margin
1402 * and the first character has been received and printed.
1404 wrapLineFlag
= true;
1407 * This block marks the case that we are in the margin
1408 * and the second character has been received and
1411 wrapLineFlag
= false;
1415 } else if (currentState
.cursorX
<= rightMargin
) {
1417 * This is the normal case: a character came in and was printed
1418 * to the left of the right margin column.
1421 // Turn off VT100 special-case flag
1422 wrapLineFlag
= false;
1425 // "Print" the character
1426 Cell newCell
= new Cell(ch
);
1427 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1428 newCellAttributes
.setTo(currentState
.attr
);
1429 DisplayLine line
= display
.get(currentState
.cursorY
);
1430 // Insert mode special case
1431 if (insertMode
== true) {
1432 line
.insert(currentState
.cursorX
, newCell
);
1434 // Replace an existing character
1435 line
.replace(currentState
.cursorX
, newCell
);
1438 // Increment horizontal
1439 if (wrapLineFlag
== false) {
1440 currentState
.cursorX
++;
1441 if (currentState
.cursorX
> rightMargin
) {
1442 currentState
.cursorX
--;
1448 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1449 * send to the remote side.
1451 * @param mouse mouse event received from the local user
1453 public void mouse(final TMouseEvent mouse
) {
1456 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1457 mouseProtocol, mouseEncoding, mouse);
1460 if (mouseEncoding
== MouseEncoding
.X10
) {
1461 // We will support X10 but only for (160,94) and smaller.
1462 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1467 switch (mouseProtocol
) {
1474 // Only report button presses
1475 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1481 // Only report button presses and releases
1482 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1483 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1491 * Only report button presses, button releases, and motions that
1492 * have a button down (i.e. drag-and-drop).
1494 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1495 if (!mouse
.isMouse1()
1496 && !mouse
.isMouse2()
1497 && !mouse
.isMouse3()
1498 && !mouse
.isMouseWheelUp()
1499 && !mouse
.isMouseWheelDown()
1507 // Report everything
1511 // Now encode the event
1512 StringBuilder sb
= new StringBuilder(6);
1513 if (mouseEncoding
== MouseEncoding
.SGR
) {
1514 sb
.append((char) 0x1B);
1517 if (mouse
.isMouse1()) {
1518 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1523 } else if (mouse
.isMouse2()) {
1524 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1529 } else if (mouse
.isMouse3()) {
1530 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1535 } else if (mouse
.isMouseWheelUp()) {
1537 } else if (mouse
.isMouseWheelDown()) {
1540 // This is motion with no buttons down.
1544 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1547 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1554 // X10 and UTF8 encodings
1555 sb
.append((char) 0x1B);
1558 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1559 sb
.append((char) (0x03 + 32));
1560 } else if (mouse
.isMouse1()) {
1561 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1562 sb
.append((char) (0x00 + 32 + 32));
1564 sb
.append((char) (0x00 + 32));
1566 } else if (mouse
.isMouse2()) {
1567 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1568 sb
.append((char) (0x01 + 32 + 32));
1570 sb
.append((char) (0x01 + 32));
1572 } else if (mouse
.isMouse3()) {
1573 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1574 sb
.append((char) (0x02 + 32 + 32));
1576 sb
.append((char) (0x02 + 32));
1578 } else if (mouse
.isMouseWheelUp()) {
1579 sb
.append((char) (0x04 + 64));
1580 } else if (mouse
.isMouseWheelDown()) {
1581 sb
.append((char) (0x05 + 64));
1583 // This is motion with no buttons down.
1584 sb
.append((char) (0x03 + 32));
1587 sb
.append((char) (mouse
.getX() + 33));
1588 sb
.append((char) (mouse
.getY() + 33));
1591 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1592 writeRemote(sb
.toString());
1596 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1597 * send to the remote side.
1599 * @param keypress keypress received from the local user
1601 public void keypress(final TKeypress keypress
) {
1602 writeRemote(keypressToString(keypress
));
1606 * Build one of the complex xterm keystroke sequences, storing the result in
1607 * xterm_keystroke_buffer.
1609 * @param ss3 the prefix to use based on VT100 state.
1610 * @param first the first character, usually a number.
1611 * @param first the last character, one of the following: ~ A B C D F H
1612 * @param ctrl whether or not ctrl is down
1613 * @param alt whether or not alt is down
1614 * @param shift whether or not shift is down
1615 * @return the buffer with the full key sequence
1617 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1618 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1620 StringBuilder sb
= new StringBuilder(ss3
);
1621 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1625 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1627 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1629 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1631 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1633 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1635 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1637 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1642 return sb
.toString();
1646 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1648 * @param keypress keypress received from the local user
1649 * @return string to transmit to the remote side
1651 @SuppressWarnings("fallthrough")
1652 private String
keypressToString(final TKeypress keypress
) {
1654 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1656 * If this is a control character, process it like it came from
1659 if (keypress
.getChar() < 0x20) {
1660 handleControlChar(keypress
.getChar());
1662 // Local echo for everything else
1663 printCharacter(keypress
.getChar());
1667 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1672 // Handle control characters
1673 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1674 StringBuilder sb
= new StringBuilder();
1675 char ch
= keypress
.getChar();
1678 return sb
.toString();
1681 // Handle alt characters
1682 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1683 StringBuilder sb
= new StringBuilder("\033");
1684 char ch
= keypress
.getChar();
1686 return sb
.toString();
1689 if (keypress
.equals(kbBackspaceDel
)) {
1702 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1705 switch (arrowKeyMode
) {
1707 return xtermBuildKeySequence("\033[", '1', 'D',
1708 keypress
.isCtrl(), keypress
.isAlt(),
1709 keypress
.isShift());
1711 return xtermBuildKeySequence("\033", '1', 'D',
1712 keypress
.isCtrl(), keypress
.isAlt(),
1713 keypress
.isShift());
1715 return xtermBuildKeySequence("\033O", '1', 'D',
1716 keypress
.isCtrl(), keypress
.isAlt(),
1717 keypress
.isShift());
1720 switch (arrowKeyMode
) {
1731 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1734 switch (arrowKeyMode
) {
1736 return xtermBuildKeySequence("\033[", '1', 'C',
1737 keypress
.isCtrl(), keypress
.isAlt(),
1738 keypress
.isShift());
1740 return xtermBuildKeySequence("\033", '1', 'C',
1741 keypress
.isCtrl(), keypress
.isAlt(),
1742 keypress
.isShift());
1744 return xtermBuildKeySequence("\033O", '1', 'C',
1745 keypress
.isCtrl(), keypress
.isAlt(),
1746 keypress
.isShift());
1749 switch (arrowKeyMode
) {
1760 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1763 switch (arrowKeyMode
) {
1765 return xtermBuildKeySequence("\033[", '1', 'A',
1766 keypress
.isCtrl(), keypress
.isAlt(),
1767 keypress
.isShift());
1769 return xtermBuildKeySequence("\033", '1', 'A',
1770 keypress
.isCtrl(), keypress
.isAlt(),
1771 keypress
.isShift());
1773 return xtermBuildKeySequence("\033O", '1', 'A',
1774 keypress
.isCtrl(), keypress
.isAlt(),
1775 keypress
.isShift());
1778 switch (arrowKeyMode
) {
1789 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1792 switch (arrowKeyMode
) {
1794 return xtermBuildKeySequence("\033[", '1', 'B',
1795 keypress
.isCtrl(), keypress
.isAlt(),
1796 keypress
.isShift());
1798 return xtermBuildKeySequence("\033", '1', 'B',
1799 keypress
.isCtrl(), keypress
.isAlt(),
1800 keypress
.isShift());
1802 return xtermBuildKeySequence("\033O", '1', 'B',
1803 keypress
.isCtrl(), keypress
.isAlt(),
1804 keypress
.isShift());
1807 switch (arrowKeyMode
) {
1818 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1821 switch (arrowKeyMode
) {
1823 return xtermBuildKeySequence("\033[", '1', 'H',
1824 keypress
.isCtrl(), keypress
.isAlt(),
1825 keypress
.isShift());
1827 return xtermBuildKeySequence("\033", '1', 'H',
1828 keypress
.isCtrl(), keypress
.isAlt(),
1829 keypress
.isShift());
1831 return xtermBuildKeySequence("\033O", '1', 'H',
1832 keypress
.isCtrl(), keypress
.isAlt(),
1833 keypress
.isShift());
1836 switch (arrowKeyMode
) {
1847 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1850 switch (arrowKeyMode
) {
1852 return xtermBuildKeySequence("\033[", '1', 'F',
1853 keypress
.isCtrl(), keypress
.isAlt(),
1854 keypress
.isShift());
1856 return xtermBuildKeySequence("\033", '1', 'F',
1857 keypress
.isCtrl(), keypress
.isAlt(),
1858 keypress
.isShift());
1860 return xtermBuildKeySequence("\033O", '1', 'F',
1861 keypress
.isCtrl(), keypress
.isAlt(),
1862 keypress
.isShift());
1865 switch (arrowKeyMode
) {
1876 if (keypress
.equals(kbF1
)) {
1884 if (keypress
.equals(kbF2
)) {
1892 if (keypress
.equals(kbF3
)) {
1900 if (keypress
.equals(kbF4
)) {
1908 if (keypress
.equals(kbF5
)) {
1921 if (keypress
.equals(kbF6
)) {
1934 if (keypress
.equals(kbF7
)) {
1947 if (keypress
.equals(kbF8
)) {
1960 if (keypress
.equals(kbF9
)) {
1973 if (keypress
.equals(kbF10
)) {
1986 if (keypress
.equals(kbF11
)) {
1990 if (keypress
.equals(kbF12
)) {
1994 if (keypress
.equals(kbShiftF1
)) {
1999 if (type
== DeviceType
.XTERM
) {
2005 if (keypress
.equals(kbShiftF2
)) {
2010 if (type
== DeviceType
.XTERM
) {
2016 if (keypress
.equals(kbShiftF3
)) {
2021 if (type
== DeviceType
.XTERM
) {
2027 if (keypress
.equals(kbShiftF4
)) {
2032 if (type
== DeviceType
.XTERM
) {
2038 if (keypress
.equals(kbShiftF5
)) {
2040 return "\033[15;2~";
2043 if (keypress
.equals(kbShiftF6
)) {
2045 return "\033[17;2~";
2048 if (keypress
.equals(kbShiftF7
)) {
2050 return "\033[18;2~";
2053 if (keypress
.equals(kbShiftF8
)) {
2055 return "\033[19;2~";
2058 if (keypress
.equals(kbShiftF9
)) {
2060 return "\033[20;2~";
2063 if (keypress
.equals(kbShiftF10
)) {
2065 return "\033[21;2~";
2068 if (keypress
.equals(kbShiftF11
)) {
2070 return "\033[23;2~";
2073 if (keypress
.equals(kbShiftF12
)) {
2075 return "\033[24;2~";
2078 if (keypress
.equals(kbCtrlF1
)) {
2083 if (type
== DeviceType
.XTERM
) {
2089 if (keypress
.equals(kbCtrlF2
)) {
2094 if (type
== DeviceType
.XTERM
) {
2100 if (keypress
.equals(kbCtrlF3
)) {
2105 if (type
== DeviceType
.XTERM
) {
2111 if (keypress
.equals(kbCtrlF4
)) {
2116 if (type
== DeviceType
.XTERM
) {
2122 if (keypress
.equals(kbCtrlF5
)) {
2124 return "\033[15;5~";
2127 if (keypress
.equals(kbCtrlF6
)) {
2129 return "\033[17;5~";
2132 if (keypress
.equals(kbCtrlF7
)) {
2134 return "\033[18;5~";
2137 if (keypress
.equals(kbCtrlF8
)) {
2139 return "\033[19;5~";
2142 if (keypress
.equals(kbCtrlF9
)) {
2144 return "\033[20;5~";
2147 if (keypress
.equals(kbCtrlF10
)) {
2149 return "\033[21;5~";
2152 if (keypress
.equals(kbCtrlF11
)) {
2154 return "\033[23;5~";
2157 if (keypress
.equals(kbCtrlF12
)) {
2159 return "\033[24;5~";
2162 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2165 return xtermBuildKeySequence("\033[", '5', '~',
2166 keypress
.isCtrl(), keypress
.isAlt(),
2167 keypress
.isShift());
2173 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2176 return xtermBuildKeySequence("\033[", '6', '~',
2177 keypress
.isCtrl(), keypress
.isAlt(),
2178 keypress
.isShift());
2184 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2187 return xtermBuildKeySequence("\033[", '2', '~',
2188 keypress
.isCtrl(), keypress
.isAlt(),
2189 keypress
.isShift());
2195 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2198 return xtermBuildKeySequence("\033[", '3', '~',
2199 keypress
.isCtrl(), keypress
.isAlt(),
2200 keypress
.isShift());
2202 // Delete sends real delete for VTxxx
2207 if (keypress
.equals(kbEnter
)) {
2211 if (keypress
.equals(kbEsc
)) {
2215 if (keypress
.equals(kbAltEsc
)) {
2219 if (keypress
.equals(kbTab
)) {
2223 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2224 (keypress
.equals(kbShiftTab
))
2234 // Non-alt, non-ctrl characters
2235 if (!keypress
.isFnKey()) {
2236 StringBuilder sb
= new StringBuilder();
2237 sb
.append(keypress
.getChar());
2238 return sb
.toString();
2244 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2247 * @param ch 8-bit character from the remote side
2248 * @param charsetGl character set defined for GL
2249 * @param charsetGr character set defined for GR
2250 * @return character to display on the screen
2252 private char mapCharacterCharset(final char ch
,
2253 final CharacterSet charsetGl
,
2254 final CharacterSet charsetGr
) {
2256 int lookupChar
= ch
;
2257 CharacterSet lookupCharset
= charsetGl
;
2260 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2261 lookupCharset
= charsetGr
;
2265 switch (lookupCharset
) {
2268 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2271 return DECCharacterSets
.UK
[lookupChar
];
2274 return DECCharacterSets
.US_ASCII
[lookupChar
];
2277 return DECCharacterSets
.NL
[lookupChar
];
2280 return DECCharacterSets
.FI
[lookupChar
];
2283 return DECCharacterSets
.FR
[lookupChar
];
2286 return DECCharacterSets
.FR_CA
[lookupChar
];
2289 return DECCharacterSets
.DE
[lookupChar
];
2292 return DECCharacterSets
.IT
[lookupChar
];
2295 return DECCharacterSets
.NO
[lookupChar
];
2298 return DECCharacterSets
.ES
[lookupChar
];
2301 return DECCharacterSets
.SV
[lookupChar
];
2304 return DECCharacterSets
.SWISS
[lookupChar
];
2306 case DEC_SUPPLEMENTAL
:
2307 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2310 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2313 return DECCharacterSets
.US_ASCII
[lookupChar
];
2316 return DECCharacterSets
.US_ASCII
[lookupChar
];
2319 throw new IllegalArgumentException("Invalid character set value: "
2325 * Map an 8-bit byte into a printable character.
2327 * @param ch either 8-bit or Unicode character from the remote side
2328 * @return character to display on the screen
2330 private char mapCharacter(final char ch
) {
2332 // Unicode character, just return it
2336 CharacterSet charsetGl
= currentState
.g0Charset
;
2337 CharacterSet charsetGr
= currentState
.grCharset
;
2339 if (vt52Mode
== true) {
2340 if (shiftOut
== true) {
2341 // Shifted out character, pull from VT52 graphics
2342 charsetGl
= currentState
.g1Charset
;
2343 charsetGr
= CharacterSet
.US
;
2346 charsetGl
= currentState
.g0Charset
;
2347 charsetGr
= CharacterSet
.US
;
2350 // Pull the character
2351 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2355 if (shiftOut
== true) {
2356 // Shifted out character, pull from G1
2357 charsetGl
= currentState
.g1Charset
;
2358 charsetGr
= currentState
.grCharset
;
2360 // Pull the character
2361 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2365 if (singleshift
== Singleshift
.SS2
) {
2367 singleshift
= Singleshift
.NONE
;
2369 // Shifted out character, pull from G2
2370 charsetGl
= currentState
.g2Charset
;
2371 charsetGr
= currentState
.grCharset
;
2375 if (singleshift
== Singleshift
.SS3
) {
2377 singleshift
= Singleshift
.NONE
;
2379 // Shifted out character, pull from G3
2380 charsetGl
= currentState
.g3Charset
;
2381 charsetGr
= currentState
.grCharset
;
2384 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2385 // Check for locking shift
2387 switch (currentState
.glLockshift
) {
2390 throw new IllegalArgumentException("programming bug");
2393 throw new IllegalArgumentException("programming bug");
2396 throw new IllegalArgumentException("programming bug");
2400 charsetGl
= currentState
.g2Charset
;
2405 charsetGl
= currentState
.g3Charset
;
2410 charsetGl
= currentState
.g0Charset
;
2414 switch (currentState
.grLockshift
) {
2417 throw new IllegalArgumentException("programming bug");
2420 throw new IllegalArgumentException("programming bug");
2424 charsetGr
= currentState
.g1Charset
;
2429 charsetGr
= currentState
.g2Charset
;
2434 charsetGr
= currentState
.g3Charset
;
2439 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2446 // Pull the character
2447 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2451 * Scroll the text within a scrolling region up n lines.
2453 * @param regionTop top row of the scrolling region
2454 * @param regionBottom bottom row of the scrolling region
2455 * @param n number of lines to scroll
2457 private void scrollingRegionScrollUp(final int regionTop
,
2458 final int regionBottom
, final int n
) {
2460 if (regionTop
>= regionBottom
) {
2464 // Sanity check: see if there will be any characters left after the
2466 if (regionBottom
+ 1 - regionTop
<= n
) {
2467 // There won't be anything left in the region, so just call
2468 // eraseScreen() and return.
2469 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2473 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2474 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2475 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2477 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2478 - remaining
, regionBottom
+ 1);
2479 display
= new ArrayList
<DisplayLine
>(displayTop
);
2480 display
.addAll(displayMiddle
);
2481 for (int i
= 0; i
< n
; i
++) {
2482 DisplayLine line
= new DisplayLine(currentState
.attr
);
2483 line
.setReverseColor(reverseVideo
);
2486 display
.addAll(displayBottom
);
2488 assert (display
.size() == height
);
2492 * Scroll the text within a scrolling region down n lines.
2494 * @param regionTop top row of the scrolling region
2495 * @param regionBottom bottom row of the scrolling region
2496 * @param n number of lines to scroll
2498 private void scrollingRegionScrollDown(final int regionTop
,
2499 final int regionBottom
, final int n
) {
2501 if (regionTop
>= regionBottom
) {
2505 // Sanity check: see if there will be any characters left after the
2507 if (regionBottom
+ 1 - regionTop
<= n
) {
2508 // There won't be anything left in the region, so just call
2509 // eraseScreen() and return.
2510 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2514 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2515 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2516 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2518 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2519 regionTop
+ remaining
);
2520 display
= new ArrayList
<DisplayLine
>(displayTop
);
2521 for (int i
= 0; i
< n
; i
++) {
2522 DisplayLine line
= new DisplayLine(currentState
.attr
);
2523 line
.setReverseColor(reverseVideo
);
2526 display
.addAll(displayMiddle
);
2527 display
.addAll(displayBottom
);
2529 assert (display
.size() == height
);
2533 * Process a control character.
2535 * @param ch 8-bit character from the remote side
2537 private void handleControlChar(final char ch
) {
2538 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2549 // Transmit the answerback message.
2560 cursorLeft(1, false);
2565 advanceToNextTabStop();
2591 currentState
.glLockshift
= LockshiftMode
.NONE
;
2597 currentState
.glLockshift
= LockshiftMode
.NONE
;
2622 singleshift
= Singleshift
.SS2
;
2627 singleshift
= Singleshift
.SS3
;
2637 * Advance the cursor to the next tab stop.
2639 private void advanceToNextTabStop() {
2640 if (tabStops
.size() == 0) {
2641 // Go to the rightmost column
2642 cursorRight(rightMargin
- currentState
.cursorX
, false);
2645 for (Integer stop
: tabStops
) {
2646 if (stop
> currentState
.cursorX
) {
2647 cursorRight(stop
- currentState
.cursorX
, false);
2652 * We got here, meaning there isn't a tab stop beyond the current
2653 * cursor position. Place the cursor of the right-most edge of the
2656 cursorRight(rightMargin
- currentState
.cursorX
, false);
2660 * Save a character into the collect buffer.
2662 * @param ch character to save
2664 private void collect(final char ch
) {
2665 collectBuffer
.append(ch
);
2669 * Save a byte into the CSI parameters buffer.
2671 * @param ch byte to save
2673 private void param(final byte ch
) {
2674 if (csiParams
.size() == 0) {
2675 csiParams
.add(Integer
.valueOf(0));
2677 Integer x
= csiParams
.get(csiParams
.size() - 1);
2678 if ((ch
>= '0') && (ch
<= '9')) {
2681 csiParams
.set(csiParams
.size() - 1, x
);
2684 if ((ch
== ';') && (csiParams
.size() < 16)) {
2685 csiParams
.add(Integer
.valueOf(0));
2690 * Get a CSI parameter value, with a default.
2692 * @param position parameter index. 0 is the first parameter.
2693 * @param defaultValue value to use if csiParams[position] doesn't exist
2694 * @return parameter value
2696 private int getCsiParam(final int position
, final int defaultValue
) {
2697 if (csiParams
.size() < position
+ 1) {
2698 return defaultValue
;
2700 return csiParams
.get(position
).intValue();
2704 * Get a CSI parameter value, clamped to within min/max.
2706 * @param position parameter index. 0 is the first parameter.
2707 * @param defaultValue value to use if csiParams[position] doesn't exist
2708 * @param minValue minimum value inclusive
2709 * @param maxValue maximum value inclusive
2710 * @return parameter value
2712 private int getCsiParam(final int position
, final int defaultValue
,
2713 final int minValue
, final int maxValue
) {
2715 assert (minValue
<= maxValue
);
2716 int value
= getCsiParam(position
, defaultValue
);
2717 if (value
< minValue
) {
2720 if (value
> maxValue
) {
2727 * Set or unset a toggle.
2729 * @param value true for set ('h'), false for reset ('l')
2731 private void setToggle(final boolean value
) {
2732 boolean decPrivateModeFlag
= false;
2733 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2734 if (collectBuffer
.charAt(i
) == '?') {
2735 decPrivateModeFlag
= true;
2740 for (Integer i
: csiParams
) {
2745 if (decPrivateModeFlag
== true) {
2747 if (value
== true) {
2748 // Use application arrow keys
2749 arrowKeyMode
= ArrowKeyMode
.VT100
;
2751 // Use ANSI arrow keys
2752 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2757 if (decPrivateModeFlag
== true) {
2758 if (value
== false) {
2762 arrowKeyMode
= ArrowKeyMode
.VT52
;
2765 * From the VT102 docs: "You use ANSI mode to select
2766 * most terminal features; the terminal uses the same
2767 * features when it switches to VT52 mode. You
2768 * cannot, however, change most of these features in
2771 * In other words, do not reset any other attributes
2772 * when switching between VT52 submode and ANSI.
2774 * HOWEVER, the real vt100 does switch the character
2775 * set according to Usenet.
2777 currentState
.g0Charset
= CharacterSet
.US
;
2778 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2781 if ((type
== DeviceType
.VT220
)
2782 || (type
== DeviceType
.XTERM
)) {
2784 // VT52 mode is explicitly 7-bit
2786 singleshift
= Singleshift
.NONE
;
2791 if (value
== true) {
2792 // Turn off keyboard
2801 if (decPrivateModeFlag
== true) {
2803 if (value
== true) {
2810 if ((displayListener
!= null)
2811 && (type
== DeviceType
.XTERM
)
2813 // For xterms, reset to the actual width, not 80
2815 width
= displayListener
.getDisplayWidth();
2816 rightMargin
= width
- 1;
2819 width
= rightMargin
+ 1;
2822 // Entire screen is cleared, and scrolling region is
2824 eraseScreen(0, 0, height
- 1, width
- 1, false);
2825 scrollRegionTop
= 0;
2826 scrollRegionBottom
= height
- 1;
2827 // Also home the cursor
2828 cursorPosition(0, 0);
2832 if (decPrivateModeFlag
== true) {
2834 if (value
== true) {
2843 if (value
== true) {
2851 if (decPrivateModeFlag
== true) {
2853 if (value
== true) {
2855 * Set selects reverse screen, a white screen
2856 * background with black characters.
2858 if (reverseVideo
!= true) {
2860 * If in normal video, switch it back
2862 invertDisplayColors();
2864 reverseVideo
= true;
2867 * Reset selects normal screen, a black screen
2868 * background with white characters.
2870 if (reverseVideo
== true) {
2872 * If in reverse video already, switch it back
2874 invertDisplayColors();
2876 reverseVideo
= false;
2881 if (decPrivateModeFlag
== true) {
2883 if (value
== true) {
2884 // Origin is relative to scroll region cursor.
2885 // Cursor can NEVER leave scrolling region.
2886 currentState
.originMode
= true;
2887 cursorPosition(0, 0);
2889 // Origin is absolute to entire screen. Cursor can
2890 // leave the scrolling region via cup() and hvp().
2891 currentState
.originMode
= false;
2892 cursorPosition(0, 0);
2897 if (decPrivateModeFlag
== true) {
2899 if (value
== true) {
2901 currentState
.lineWrap
= true;
2903 // Turn linewrap off
2904 currentState
.lineWrap
= false;
2909 if (decPrivateModeFlag
== true) {
2911 if (value
== true) {
2912 // Keyboard auto-repeat on
2915 // Keyboard auto-repeat off
2921 if (decPrivateModeFlag
== false) {
2923 if (value
== true) {
2933 if (decPrivateModeFlag
== true) {
2939 if (decPrivateModeFlag
== true) {
2945 if (decPrivateModeFlag
== false) {
2947 if (value
== true) {
2949 * Set causes a received linefeed, form feed, or
2950 * vertical tab to move cursor to first column of
2951 * next line. RETURN transmits both a carriage return
2952 * and linefeed. This selection is also called new
2958 * Reset causes a received linefeed, form feed, or
2959 * vertical tab to move cursor to next line in
2960 * current column. RETURN transmits a carriage
2963 newLineMode
= false;
2969 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2970 if (decPrivateModeFlag
== true) {
2972 if (value
== true) {
2974 cursorVisible
= true;
2977 cursorVisible
= false;
2984 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2985 if (decPrivateModeFlag
== true) {
2987 if (value
== true) {
2988 // Select national mode NRC
2991 // Select multi-national mode
3000 if ((type
== DeviceType
.XTERM
)
3001 && (decPrivateModeFlag
== true)
3003 // Mouse: normal tracking mode
3004 if (value
== true) {
3005 mouseProtocol
= MouseProtocol
.NORMAL
;
3007 mouseProtocol
= MouseProtocol
.OFF
;
3013 if ((type
== DeviceType
.XTERM
)
3014 && (decPrivateModeFlag
== true)
3016 // Mouse: normal tracking mode
3017 if (value
== true) {
3018 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
3020 mouseProtocol
= MouseProtocol
.OFF
;
3026 if ((type
== DeviceType
.XTERM
)
3027 && (decPrivateModeFlag
== true)
3029 // Mouse: Any-event tracking mode
3030 if (value
== true) {
3031 mouseProtocol
= MouseProtocol
.ANYEVENT
;
3033 mouseProtocol
= MouseProtocol
.OFF
;
3039 if ((type
== DeviceType
.XTERM
)
3040 && (decPrivateModeFlag
== true)
3042 // Mouse: UTF-8 coordinates
3043 if (value
== true) {
3044 mouseEncoding
= MouseEncoding
.UTF8
;
3046 mouseEncoding
= MouseEncoding
.X10
;
3052 if ((type
== DeviceType
.XTERM
)
3053 && (decPrivateModeFlag
== true)
3055 // Mouse: SGR coordinates
3056 if (value
== true) {
3057 mouseEncoding
= MouseEncoding
.SGR
;
3059 mouseEncoding
= MouseEncoding
.X10
;
3072 * DECSC - Save cursor.
3074 private void decsc() {
3075 savedState
.setTo(currentState
);
3079 * DECRC - Restore cursor.
3081 private void decrc() {
3082 currentState
.setTo(savedState
);
3088 private void ind() {
3089 // Move the cursor and scroll if necessary. If at the bottom line
3090 // already, a scroll up is supposed to be performed.
3091 if (currentState
.cursorY
== scrollRegionBottom
) {
3092 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3094 cursorDown(1, true);
3098 * RI - Reverse index.
3101 // Move the cursor and scroll if necessary. If at the top line
3102 // already, a scroll down is supposed to be performed.
3103 if (currentState
.cursorY
== scrollRegionTop
) {
3104 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3112 private void nel() {
3113 // Move the cursor and scroll if necessary. If at the bottom line
3114 // already, a scroll up is supposed to be performed.
3115 if (currentState
.cursorY
== scrollRegionBottom
) {
3116 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3118 cursorDown(1, true);
3120 // Reset to the beginning of the next line
3121 currentState
.cursorX
= 0;
3125 * DECKPAM - Keypad application mode.
3127 private void deckpam() {
3128 keypadMode
= KeypadMode
.Application
;
3132 * DECKPNM - Keypad numeric mode.
3134 private void deckpnm() {
3135 keypadMode
= KeypadMode
.Numeric
;
3141 * @param n number of spaces to move
3142 * @param honorScrollRegion if true, then do nothing if the cursor is
3143 * outside the scrolling region
3145 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3149 * Special case: if a user moves the cursor from the right margin, we
3150 * have to reset the VT100 right margin flag.
3153 wrapLineFlag
= false;
3156 for (int i
= 0; i
< n
; i
++) {
3157 if (honorScrollRegion
== true) {
3158 // Honor the scrolling region
3159 if ((currentState
.cursorY
< scrollRegionTop
)
3160 || (currentState
.cursorY
> scrollRegionBottom
)
3162 // Outside region, do nothing
3165 // Inside region, go up
3166 top
= scrollRegionTop
;
3168 // Non-scrolling case
3172 if (currentState
.cursorY
> top
) {
3173 currentState
.cursorY
--;
3179 * Move down n spaces.
3181 * @param n number of spaces to move
3182 * @param honorScrollRegion if true, then do nothing if the cursor is
3183 * outside the scrolling region
3185 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3189 * Special case: if a user moves the cursor from the right margin, we
3190 * have to reset the VT100 right margin flag.
3193 wrapLineFlag
= false;
3196 for (int i
= 0; i
< n
; i
++) {
3198 if (honorScrollRegion
== true) {
3199 // Honor the scrolling region
3200 if (currentState
.cursorY
> scrollRegionBottom
) {
3201 // Outside region, do nothing
3204 // Inside region, go down
3205 bottom
= scrollRegionBottom
;
3207 // Non-scrolling case
3208 bottom
= height
- 1;
3211 if (currentState
.cursorY
< bottom
) {
3212 currentState
.cursorY
++;
3218 * Move left n spaces.
3220 * @param n number of spaces to move
3221 * @param honorScrollRegion if true, then do nothing if the cursor is
3222 * outside the scrolling region
3224 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3226 * Special case: if a user moves the cursor from the right margin, we
3227 * have to reset the VT100 right margin flag.
3230 wrapLineFlag
= false;
3233 for (int i
= 0; i
< n
; i
++) {
3234 if (honorScrollRegion
== true) {
3235 // Honor the scrolling region
3236 if ((currentState
.cursorY
< scrollRegionTop
)
3237 || (currentState
.cursorY
> scrollRegionBottom
)
3239 // Outside region, do nothing
3244 if (currentState
.cursorX
> 0) {
3245 currentState
.cursorX
--;
3251 * Move right n spaces.
3253 * @param n number of spaces to move
3254 * @param honorScrollRegion if true, then do nothing if the cursor is
3255 * outside the scrolling region
3257 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3258 int rightMargin
= this.rightMargin
;
3261 * Special case: if a user moves the cursor from the right margin, we
3262 * have to reset the VT100 right margin flag.
3265 wrapLineFlag
= false;
3268 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3269 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3272 for (int i
= 0; i
< n
; i
++) {
3273 if (honorScrollRegion
== true) {
3274 // Honor the scrolling region
3275 if ((currentState
.cursorY
< scrollRegionTop
)
3276 || (currentState
.cursorY
> scrollRegionBottom
)
3278 // Outside region, do nothing
3283 if (currentState
.cursorX
< rightMargin
) {
3284 currentState
.cursorX
++;
3290 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3292 * @param row row to move to
3293 * @param col column to move to
3295 private void cursorPosition(int row
, final int col
) {
3296 int rightMargin
= this.rightMargin
;
3301 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3302 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3305 // Set column number
3306 currentState
.cursorX
= col
;
3308 // Sanity check, bring column back to margin.
3309 if (currentState
.cursorX
> rightMargin
) {
3310 currentState
.cursorX
= rightMargin
;
3314 if (currentState
.originMode
== true) {
3315 row
+= scrollRegionTop
;
3317 if (currentState
.cursorY
< row
) {
3318 cursorDown(row
- currentState
.cursorY
, false);
3319 } else if (currentState
.cursorY
> row
) {
3320 cursorUp(currentState
.cursorY
- row
, false);
3323 wrapLineFlag
= false;
3327 * HTS - Horizontal tabulation set.
3329 private void hts() {
3330 for (Integer stop
: tabStops
) {
3331 if (stop
== currentState
.cursorX
) {
3332 // Already have a tab stop here
3337 // Append a tab stop to the end of the array and resort them
3338 tabStops
.add(currentState
.cursorX
);
3339 Collections
.sort(tabStops
);
3343 * DECSWL - Single-width line.
3345 private void decswl() {
3346 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3347 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3351 * DECDWL - Double-width line.
3353 private void decdwl() {
3354 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3355 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3359 * DECHDL - Double-height + double-width line.
3361 * @param topHalf if true, this sets the row to be the top half row of a
3364 private void dechdl(final boolean topHalf
) {
3365 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3366 if (topHalf
== true) {
3367 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3369 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3374 * DECALN - Screen alignment display.
3376 private void decaln() {
3377 Cell newCell
= new Cell();
3378 newCell
.setChar('E');
3379 for (DisplayLine line
: display
) {
3380 for (int i
= 0; i
< line
.length(); i
++) {
3381 line
.replace(i
, newCell
);
3387 * DECSCL - Compatibility level.
3389 private void decscl() {
3390 int i
= getCsiParam(0, 0);
3391 int j
= getCsiParam(1, 0);
3395 currentState
.g0Charset
= CharacterSet
.US
;
3396 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3398 } else if (i
== 62) {
3400 if ((j
== 0) || (j
== 2)) {
3402 } else if (j
== 1) {
3409 * CUD - Cursor down.
3411 private void cud() {
3412 cursorDown(getCsiParam(0, 1, 1, height
), true);
3416 * CUF - Cursor forward.
3418 private void cuf() {
3419 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3423 * CUB - Cursor backward.
3425 private void cub() {
3426 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3432 private void cuu() {
3433 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3437 * CUP - Cursor position.
3439 private void cup() {
3440 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3441 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3445 * CNL - Cursor down and to column 1.
3447 private void cnl() {
3448 cursorDown(getCsiParam(0, 1, 1, height
), true);
3450 cursorLeft(currentState
.cursorX
, true);
3454 * CPL - Cursor up and to column 1.
3456 private void cpl() {
3457 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3459 cursorLeft(currentState
.cursorX
, true);
3463 * CHA - Cursor to column # in current row.
3465 private void cha() {
3466 cursorPosition(currentState
.cursorY
,
3467 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3471 * VPA - Cursor to row #, same column.
3473 private void vpa() {
3474 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3475 currentState
.cursorX
);
3479 * ED - Erase in display.
3482 boolean honorProtected
= false;
3483 boolean decPrivateModeFlag
= false;
3485 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3486 if (collectBuffer
.charAt(i
) == '?') {
3487 decPrivateModeFlag
= true;
3492 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3493 && (decPrivateModeFlag
== true)
3495 honorProtected
= true;
3498 int i
= getCsiParam(0, 0);
3501 // Erase from here to end of screen
3502 if (currentState
.cursorY
< height
- 1) {
3503 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3506 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3507 } else if (i
== 1) {
3508 // Erase from beginning of screen to here
3509 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3511 eraseLine(0, currentState
.cursorX
, honorProtected
);
3512 } else if (i
== 2) {
3513 // Erase entire screen
3514 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3519 * EL - Erase in line.
3522 boolean honorProtected
= false;
3523 boolean decPrivateModeFlag
= false;
3525 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3526 if (collectBuffer
.charAt(i
) == '?') {
3527 decPrivateModeFlag
= true;
3532 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3533 && (decPrivateModeFlag
== true)
3535 honorProtected
= true;
3538 int i
= getCsiParam(0, 0);
3541 // Erase from here to end of line
3542 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3543 } else if (i
== 1) {
3544 // Erase from beginning of line to here
3545 eraseLine(0, currentState
.cursorX
, honorProtected
);
3546 } else if (i
== 2) {
3547 // Erase entire line
3548 eraseLine(0, width
- 1, honorProtected
);
3553 * ECH - Erase # of characters in current row.
3555 private void ech() {
3556 int i
= getCsiParam(0, 1, 1, width
);
3558 // Erase from here to i characters
3559 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3566 int i
= getCsiParam(0, 1);
3568 if ((currentState
.cursorY
>= scrollRegionTop
)
3569 && (currentState
.cursorY
<= scrollRegionBottom
)
3572 // I can get the same effect with a scroll-down
3573 scrollingRegionScrollDown(currentState
.cursorY
,
3574 scrollRegionBottom
, i
);
3579 * DCH - Delete char.
3581 private void dch() {
3582 int n
= getCsiParam(0, 1);
3583 DisplayLine line
= display
.get(currentState
.cursorY
);
3584 Cell blank
= new Cell();
3585 for (int i
= 0; i
< n
; i
++) {
3586 line
.delete(currentState
.cursorX
, blank
);
3591 * ICH - Insert blank char at cursor.
3593 private void ich() {
3594 int n
= getCsiParam(0, 1);
3595 DisplayLine line
= display
.get(currentState
.cursorY
);
3596 Cell blank
= new Cell();
3597 for (int i
= 0; i
< n
; i
++) {
3598 line
.insert(currentState
.cursorX
, blank
);
3606 int i
= getCsiParam(0, 1);
3608 if ((currentState
.cursorY
>= scrollRegionTop
)
3609 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3611 // I can get the same effect with a scroll-down
3612 scrollingRegionScrollUp(currentState
.cursorY
,
3613 scrollRegionBottom
, i
);
3618 * HVP - Horizontal and vertical position.
3620 private void hvp() {
3625 * REP - Repeat character.
3627 private void rep() {
3628 int n
= getCsiParam(0, 1);
3629 for (int i
= 0; i
< n
; i
++) {
3630 printCharacter(repCh
);
3638 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3639 getCsiParam(0, 1, 1, height
));
3646 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3647 getCsiParam(0, 1, 1, height
));
3651 * CBT - Go back X tab stops.
3653 private void cbt() {
3654 int tabsToMove
= getCsiParam(0, 1);
3657 for (int i
= 0; i
< tabsToMove
; i
++) {
3658 int j
= currentState
.cursorX
;
3659 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3660 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3668 j
= tabStops
.get(tabI
);
3670 cursorPosition(currentState
.cursorY
, j
);
3675 * CHT - Advance X tab stops.
3677 private void cht() {
3678 int n
= getCsiParam(0, 1);
3679 for (int i
= 0; i
< n
; i
++) {
3680 advanceToNextTabStop();
3685 * SGR - Select graphics rendition.
3687 private void sgr() {
3689 if (csiParams
.size() == 0) {
3690 currentState
.attr
.reset();
3694 int sgrColorMode
= -1;
3695 boolean idx88Color
= false;
3696 boolean rgbColor
= false;
3700 for (Integer i
: csiParams
) {
3702 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
3704 assert (type
== DeviceType
.XTERM
);
3708 * Indexed color mode, we now have the index number.
3710 if (sgrColorMode
== 38) {
3711 currentState
.attr
.setForeColorRGB(get88Color(i
));
3713 assert (sgrColorMode
== 48);
3714 currentState
.attr
.setBackColorRGB(get88Color(i
));
3723 * RGB color mode, we are collecting tokens.
3727 } else if (rgbGreen
== -1) {
3728 rgbGreen
= i
& 0xFF;
3730 int rgb
= rgbRed
<< 16;
3731 rgb
|= rgbGreen
<< 8;
3734 // System.err.printf("RGB: %08x\n", rgb);
3736 if (sgrColorMode
== 38) {
3737 currentState
.attr
.setForeColorRGB(rgb
);
3739 assert (sgrColorMode
== 48);
3740 currentState
.attr
.setBackColorRGB(rgb
);
3761 * Indexed color mode.
3768 * This is neither indexed nor RGB color. Bail out.
3773 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
3779 currentState
.attr
.reset();
3784 currentState
.attr
.setBold(true);
3789 currentState
.attr
.setUnderline(true);
3794 currentState
.attr
.setBlink(true);
3799 currentState
.attr
.setReverse(true);
3806 if (type
== DeviceType
.XTERM
) {
3816 // Set black foreground
3817 currentState
.attr
.setForeColorRGB(get88Color(8));
3820 // Set red foreground
3821 currentState
.attr
.setForeColorRGB(get88Color(9));
3824 // Set green foreground
3825 currentState
.attr
.setForeColorRGB(get88Color(10));
3828 // Set yellow foreground
3829 currentState
.attr
.setForeColorRGB(get88Color(11));
3832 // Set blue foreground
3833 currentState
.attr
.setForeColorRGB(get88Color(12));
3836 // Set magenta foreground
3837 currentState
.attr
.setForeColorRGB(get88Color(13));
3840 // Set cyan foreground
3841 currentState
.attr
.setForeColorRGB(get88Color(14));
3844 // Set white foreground
3845 currentState
.attr
.setForeColorRGB(get88Color(15));
3849 // Set black background
3850 currentState
.attr
.setBackColorRGB(get88Color(8));
3853 // Set red background
3854 currentState
.attr
.setBackColorRGB(get88Color(9));
3857 // Set green background
3858 currentState
.attr
.setBackColorRGB(get88Color(10));
3861 // Set yellow background
3862 currentState
.attr
.setBackColorRGB(get88Color(11));
3865 // Set blue background
3866 currentState
.attr
.setBackColorRGB(get88Color(12));
3869 // Set magenta background
3870 currentState
.attr
.setBackColorRGB(get88Color(13));
3873 // Set cyan background
3874 currentState
.attr
.setBackColorRGB(get88Color(14));
3877 // Set white background
3878 currentState
.attr
.setBackColorRGB(get88Color(15));
3886 if ((type
== DeviceType
.VT220
)
3887 || (type
== DeviceType
.XTERM
)) {
3893 currentState
.attr
.setBold(false);
3898 currentState
.attr
.setUnderline(false);
3903 currentState
.attr
.setBlink(false);
3908 currentState
.attr
.setReverse(false);
3916 // A true VT100/102/220 does not support color, however everyone
3917 // is used to their terminal emulator supporting color so we will
3918 // unconditionally support color for all DeviceType's.
3923 // Set black foreground
3924 currentState
.attr
.setForeColor(Color
.BLACK
);
3925 currentState
.attr
.setForeColorRGB(-1);
3928 // Set red foreground
3929 currentState
.attr
.setForeColor(Color
.RED
);
3930 currentState
.attr
.setForeColorRGB(-1);
3933 // Set green foreground
3934 currentState
.attr
.setForeColor(Color
.GREEN
);
3935 currentState
.attr
.setForeColorRGB(-1);
3938 // Set yellow foreground
3939 currentState
.attr
.setForeColor(Color
.YELLOW
);
3940 currentState
.attr
.setForeColorRGB(-1);
3943 // Set blue foreground
3944 currentState
.attr
.setForeColor(Color
.BLUE
);
3945 currentState
.attr
.setForeColorRGB(-1);
3948 // Set magenta foreground
3949 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3950 currentState
.attr
.setForeColorRGB(-1);
3953 // Set cyan foreground
3954 currentState
.attr
.setForeColor(Color
.CYAN
);
3955 currentState
.attr
.setForeColorRGB(-1);
3958 // Set white foreground
3959 currentState
.attr
.setForeColor(Color
.WHITE
);
3960 currentState
.attr
.setForeColorRGB(-1);
3963 if (type
== DeviceType
.XTERM
) {
3965 * Xterm supports T.416 / ISO-8613-3 codes to select
3966 * either an indexed color or an RGB value. (It also
3967 * permits these ISO-8613-3 SGR sequences to be separated
3968 * by colons rather than semicolons.)
3970 * We will support only the following:
3972 * 1. Indexed color mode (88- or 256-color modes).
3976 * These cover most of the use cases in the real world.
3978 * HOWEVER, note that this is an awful broken "standard",
3979 * with no way to do it "right". See
3980 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
3981 * for a detailed discussion of the current state of RGB
3982 * in various terminals, the point of which is that none
3983 * of them really do the same thing despite all appearing
3987 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
3988 * where it is assumed that supporting just the "indexed
3989 * mode" of these sequences (which could align easily
3990 * with existing SGR colors) is assumed to mean full
3991 * support of 24-bit RGB. So it is all or nothing.
3993 * Finally, these sequences break the assumptions of
3994 * standard ECMA-48 style parsers as pointed out at
3995 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
3996 * Therefore in order to keep a clean display, we cannot
3997 * parse anything else in this sequence.
4002 // Underscore on, default foreground color
4003 currentState
.attr
.setUnderline(true);
4004 currentState
.attr
.setForeColor(Color
.WHITE
);
4008 // Underscore off, default foreground color
4009 currentState
.attr
.setUnderline(false);
4010 currentState
.attr
.setForeColor(Color
.WHITE
);
4011 currentState
.attr
.setForeColorRGB(-1);
4014 // Set black background
4015 currentState
.attr
.setBackColor(Color
.BLACK
);
4016 currentState
.attr
.setBackColorRGB(-1);
4019 // Set red background
4020 currentState
.attr
.setBackColor(Color
.RED
);
4021 currentState
.attr
.setBackColorRGB(-1);
4024 // Set green background
4025 currentState
.attr
.setBackColor(Color
.GREEN
);
4026 currentState
.attr
.setBackColorRGB(-1);
4029 // Set yellow background
4030 currentState
.attr
.setBackColor(Color
.YELLOW
);
4031 currentState
.attr
.setBackColorRGB(-1);
4034 // Set blue background
4035 currentState
.attr
.setBackColor(Color
.BLUE
);
4036 currentState
.attr
.setBackColorRGB(-1);
4039 // Set magenta background
4040 currentState
.attr
.setBackColor(Color
.MAGENTA
);
4041 currentState
.attr
.setBackColorRGB(-1);
4044 // Set cyan background
4045 currentState
.attr
.setBackColor(Color
.CYAN
);
4046 currentState
.attr
.setBackColorRGB(-1);
4049 // Set white background
4050 currentState
.attr
.setBackColor(Color
.WHITE
);
4051 currentState
.attr
.setBackColorRGB(-1);
4054 if (type
== DeviceType
.XTERM
) {
4056 * Xterm supports T.416 / ISO-8613-3 codes to select
4057 * either an indexed color or an RGB value. (It also
4058 * permits these ISO-8613-3 SGR sequences to be separated
4059 * by colons rather than semicolons.)
4061 * We will support only the following:
4063 * 1. Indexed color mode (88- or 256-color modes).
4067 * These cover most of the use cases in the real world.
4069 * HOWEVER, note that this is an awful broken "standard",
4070 * with no way to do it "right". See
4071 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4072 * for a detailed discussion of the current state of RGB
4073 * in various terminals, the point of which is that none
4074 * of them really do the same thing despite all appearing
4078 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4079 * where it is assumed that supporting just the "indexed
4080 * mode" of these sequences (which could align easily
4081 * with existing SGR colors) is assumed to mean full
4082 * support of 24-bit RGB. So it is all or nothing.
4084 * Finally, these sequences break the assumptions of
4085 * standard ECMA-48 style parsers as pointed out at
4086 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4087 * Therefore in order to keep a clean display, we cannot
4088 * parse anything else in this sequence.
4095 // Default background
4096 currentState
.attr
.setBackColor(Color
.BLACK
);
4097 currentState
.attr
.setBackColorRGB(-1);
4107 * DA - Device attributes.
4110 int extendedFlag
= 0;
4112 if (collectBuffer
.length() > 0) {
4113 String args
= collectBuffer
.substring(1);
4114 if (collectBuffer
.charAt(0) == '>') {
4116 if (collectBuffer
.length() >= 2) {
4117 i
= Integer
.parseInt(args
);
4119 } else if (collectBuffer
.charAt(0) == '=') {
4121 if (collectBuffer
.length() >= 2) {
4122 i
= Integer
.parseInt(args
);
4125 // Unknown code, bail out
4130 if ((i
!= 0) && (i
!= 1)) {
4134 if ((extendedFlag
== 0) && (i
== 0)) {
4135 // Send string directly to remote side
4136 writeRemote(deviceTypeResponse());
4140 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4142 if ((extendedFlag
== 1) && (i
== 0)) {
4144 * Request "What type of terminal are you, what is your
4145 * firmware version, and what hardware options do you have
4148 * Respond: "I am a VT220 (identification code of 1), my
4149 * firmware version is _____ (Pv), and I have _____ Po
4150 * options installed."
4156 if (s8c1t
== true) {
4157 writeRemote("\u009b>1;10;0c");
4159 writeRemote("\033[>1;10;0c");
4165 if ((extendedFlag
== 2) && (i
== 0)) {
4168 * Request "What is your unit ID?"
4170 * Respond: "I was manufactured at site 00 and have a unique ID
4174 writeRemote("\033P!|00010203\033\\");
4179 * DECSTBM - Set top and bottom margins.
4181 private void decstbm() {
4182 boolean decPrivateModeFlag
= false;
4184 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4185 if (collectBuffer
.charAt(i
) == '?') {
4186 decPrivateModeFlag
= true;
4190 if (decPrivateModeFlag
) {
4191 // This could be restore DEC private mode values.
4195 int top
= getCsiParam(0, 1, 1, height
) - 1;
4196 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4201 scrollRegionTop
= top
;
4202 scrollRegionBottom
= bottom
;
4205 cursorPosition(0, 0);
4210 * DECREQTPARM - Request terminal parameters.
4212 private void decreqtparm() {
4213 int i
= getCsiParam(0, 0);
4215 if ((i
!= 0) && (i
!= 1)) {
4222 * Request terminal parameters.
4226 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4227 * (CLoCk MULtiplier = 1, STP option flags = 0)
4231 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4234 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4236 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4242 * DECSCA - Select Character Attributes.
4244 private void decsca() {
4245 int i
= getCsiParam(0, 0);
4247 if ((i
== 0) || (i
== 2)) {
4249 currentState
.attr
.setProtect(false);
4253 currentState
.attr
.setProtect(true);
4258 * DECSTR - Soft Terminal Reset.
4260 private void decstr() {
4261 // Do exactly like RIS - Reset to initial state
4263 // Do I clear screen too? I think so...
4264 eraseScreen(0, 0, height
- 1, width
- 1, false);
4265 cursorPosition(0, 0);
4269 * DSR - Device status report.
4271 private void dsr() {
4272 boolean decPrivateModeFlag
= false;
4273 int row
= currentState
.cursorY
;
4275 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4276 if (collectBuffer
.charAt(i
) == '?') {
4277 decPrivateModeFlag
= true;
4282 int i
= getCsiParam(0, 0);
4287 // Request status report. Respond with "OK, no malfunction."
4289 // Send string directly to remote side
4290 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4293 writeRemote("\u009b0n");
4295 writeRemote("\033[0n");
4300 // Request cursor position. Respond with current position.
4301 if (currentState
.originMode
== true) {
4302 row
-= scrollRegionTop
;
4305 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4308 str
= String
.format("\u009b%d;%dR", row
+ 1,
4309 currentState
.cursorX
+ 1);
4311 str
= String
.format("\033[%d;%dR", row
+ 1,
4312 currentState
.cursorX
+ 1);
4315 // Send string directly to remote side
4320 if (decPrivateModeFlag
== true) {
4322 // Request printer status report. Respond with "Printer not
4325 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4326 && (s8c1t
== true)) {
4327 writeRemote("\u009b?13n");
4329 writeRemote("\033[?13n");
4335 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4336 && (decPrivateModeFlag
== true)
4339 // Request user-defined keys are locked or unlocked. Respond
4340 // with "User-defined keys are locked."
4342 if (s8c1t
== true) {
4343 writeRemote("\u009b?21n");
4345 writeRemote("\033[?21n");
4351 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4352 && (decPrivateModeFlag
== true)
4355 // Request keyboard language. Respond with "Keyboard
4356 // language is North American."
4358 if (s8c1t
== true) {
4359 writeRemote("\u009b?27;1n");
4361 writeRemote("\033[?27;1n");
4368 // Some other option, ignore
4374 * TBC - Tabulation clear.
4376 private void tbc() {
4377 int i
= getCsiParam(0, 0);
4379 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4380 for (Integer stop
: tabStops
) {
4381 if (stop
== currentState
.cursorX
) {
4386 tabStops
= newStops
;
4394 * Erase the characters in the current line from the start column to the
4395 * end column, inclusive.
4397 * @param start starting column to erase (between 0 and width - 1)
4398 * @param end ending column to erase (between 0 and width - 1)
4399 * @param honorProtected if true, do not erase characters with the
4400 * protected attribute set
4402 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4407 if (end
> width
- 1) {
4414 for (int i
= start
; i
<= end
; i
++) {
4415 DisplayLine line
= display
.get(currentState
.cursorY
);
4416 if ((!honorProtected
)
4417 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4424 * From the VT102 manual:
4426 * Erasing a character also erases any character
4427 * attribute of the character.
4433 * Erase with the current color a.k.a. back-color erase
4436 line
.setChar(i
, ' ');
4437 line
.setAttr(i
, currentState
.attr
);
4445 * Erase a rectangular section of the screen, inclusive. end column,
4448 * @param startRow starting row to erase (between 0 and height - 1)
4449 * @param startCol starting column to erase (between 0 and width - 1)
4450 * @param endRow ending row to erase (between 0 and height - 1)
4451 * @param endCol ending column to erase (between 0 and width - 1)
4452 * @param honorProtected if true, do not erase characters with the
4453 * protected attribute set
4455 private void eraseScreen(final int startRow
, final int startCol
,
4456 final int endRow
, final int endCol
, final boolean honorProtected
) {
4464 || (endRow
< startRow
)
4465 || (endCol
< startCol
)
4470 oldCursorY
= currentState
.cursorY
;
4471 for (int i
= startRow
; i
<= endRow
; i
++) {
4472 currentState
.cursorY
= i
;
4473 eraseLine(startCol
, endCol
, honorProtected
);
4475 // Erase display clears the double attributes
4476 display
.get(i
).setDoubleWidth(false);
4477 display
.get(i
).setDoubleHeight(0);
4479 currentState
.cursorY
= oldCursorY
;
4483 * VT220 printer functions. All of these are parsed, but won't do
4486 private void printerFunctions() {
4487 boolean decPrivateModeFlag
= false;
4488 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4489 if (collectBuffer
.charAt(i
) == '?') {
4490 decPrivateModeFlag
= true;
4495 int i
= getCsiParam(0, 0);
4500 if (decPrivateModeFlag
== false) {
4506 if (decPrivateModeFlag
== true) {
4507 // Print cursor line
4512 if (decPrivateModeFlag
== true) {
4513 // Auto print mode OFF
4515 // Printer controller OFF
4517 // Characters re-appear on the screen
4518 printerControllerMode
= false;
4523 if (decPrivateModeFlag
== true) {
4527 // Printer controller
4529 // Characters get sucked into oblivion
4530 printerControllerMode
= true;
4541 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4542 * of remote systems will send an XTerm title sequence even if TERM isn't
4545 * @param xtermChar the character received from the remote side
4547 private void oscPut(final char xtermChar
) {
4548 // System.err.println("oscPut: " + xtermChar);
4551 collectBuffer
.append(xtermChar
);
4554 if ((xtermChar
== 0x07)
4555 || (collectBuffer
.toString().endsWith("\033\\"))
4558 if (xtermChar
== 0x07) {
4559 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
4561 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4564 String
[] p
= args
.split(";");
4566 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4573 if (p
[0].equals("4")) {
4574 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
4575 // Set a color index value
4577 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
4578 } catch (NumberFormatException e
) {
4585 // Go to SCAN_GROUND state
4592 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
4593 * Jexer ECMA48Terminal to talk to ECMA48.
4595 * @param pmChar the character received from the remote side
4597 private void pmPut(final char pmChar
) {
4598 // System.err.println("pmPut: " + pmChar);
4601 collectBuffer
.append(pmChar
);
4604 if (collectBuffer
.toString().endsWith("\033\\")) {
4606 arg
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4608 // System.err.println("arg: '" + arg + "'");
4610 if (arg
.equals("hideMousePointer")) {
4611 hideMousePointer
= true;
4613 if (arg
.equals("showMousePointer")) {
4614 hideMousePointer
= false;
4617 // Go to SCAN_GROUND state
4624 * Perform xterm window operations.
4626 private void xtermWindowOps() {
4627 boolean xtermPrivateModeFlag
= false;
4629 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4630 if (collectBuffer
.charAt(i
) == '?') {
4631 xtermPrivateModeFlag
= true;
4636 int i
= getCsiParam(0, 0);
4638 if (!xtermPrivateModeFlag
) {
4640 // Report xterm window in pixels as CSI 4 ; height ; width t
4641 writeRemote(String
.format("\033[4;%d;%dt", textHeight
* height
,
4642 textWidth
* width
));
4648 * Run this input character through the ECMA48 state machine.
4650 * @param ch character from the remote side
4652 private void consume(char ch
) {
4655 // System.err.printf("%c STATE = %s\n", ch, scanState);
4657 // Special case for VT10x: 7-bit characters only
4658 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4659 ch
= (char)(ch
& 0x7F);
4662 // Special "anywhere" states
4664 // 18, 1A --> execute, then switch to SCAN_GROUND
4665 if ((ch
== 0x18) || (ch
== 0x1A)) {
4666 // CAN and SUB abort escape sequences
4671 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4675 if ((type
== DeviceType
.XTERM
)
4676 && ((scanState
== ScanState
.OSC_STRING
)
4677 || (scanState
== ScanState
.DCS_SIXEL
)
4678 || (scanState
== ScanState
.SOSPMAPC_STRING
))
4680 // Xterm can pass ESCAPE to its OSC sequence.
4681 // Xterm can pass ESCAPE to its DCS sequence.
4682 // Jexer can pass ESCAPE to its PM sequence.
4683 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
4684 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4685 && (scanState
!= ScanState
.DCS_IGNORE
)
4686 && (scanState
!= ScanState
.DCS_PARAM
)
4687 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4689 scanState
= ScanState
.ESCAPE
;
4694 // 0x9B == CSI 8-bit sequence
4696 scanState
= ScanState
.CSI_ENTRY
;
4700 // 0x9D goes to ScanState.OSC_STRING
4702 scanState
= ScanState
.OSC_STRING
;
4706 // 0x90 goes to DCS_ENTRY
4708 scanState
= ScanState
.DCS_ENTRY
;
4712 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4713 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4714 scanState
= ScanState
.SOSPMAPC_STRING
;
4718 // 0x7F (DEL) is always discarded
4723 switch (scanState
) {
4726 // 00-17, 19, 1C-1F --> execute
4727 // 80-8F, 91-9A, 9C --> execute
4728 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4729 handleControlChar(ch
);
4733 if (((ch
>= 0x20) && (ch
<= 0x7F))
4737 // VT220 printer --> trash bin
4738 if (((type
== DeviceType
.VT220
)
4739 || (type
== DeviceType
.XTERM
))
4740 && (printerControllerMode
== true)
4745 // Hang onto this character
4746 repCh
= mapCharacter(ch
);
4748 // Print this character
4749 printCharacter(repCh
);
4754 // 00-17, 19, 1C-1F --> execute
4756 handleControlChar(ch
);
4760 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4761 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4763 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4767 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4768 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4779 // DECSC - Save cursor
4780 // Note this code overlaps both ANSI and VT52 mode
4785 // DECRC - Restore cursor
4786 // Note this code overlaps both ANSI and VT52 mode
4795 if (vt52Mode
== true) {
4796 // DECANM - Enter ANSI mode
4798 arrowKeyMode
= ArrowKeyMode
.VT100
;
4801 * From the VT102 docs: "You use ANSI mode to select
4802 * most terminal features; the terminal uses the same
4803 * features when it switches to VT52 mode. You
4804 * cannot, however, change most of these features in
4807 * In other words, do not reset any other attributes
4808 * when switching between VT52 submode and ANSI.
4812 currentState
.g0Charset
= CharacterSet
.US
;
4813 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4815 singleshift
= Singleshift
.NONE
;
4816 currentState
.glLockshift
= LockshiftMode
.NONE
;
4817 currentState
.grLockshift
= LockshiftMode
.NONE
;
4821 // DECKPAM - Keypad application mode
4822 // Note this code overlaps both ANSI and VT52 mode
4826 // DECKPNM - Keypad numeric mode
4827 // Note this code overlaps both ANSI and VT52 mode
4834 if (vt52Mode
== true) {
4835 // Cursor up, and stop at the top without scrolling
4840 if (vt52Mode
== true) {
4841 // Cursor down, and stop at the bottom without scrolling
4842 cursorDown(1, false);
4846 if (vt52Mode
== true) {
4847 // Cursor right, and stop at the right without scrolling
4848 cursorRight(1, false);
4852 if (vt52Mode
== true) {
4853 // Cursor left, and stop at the left without scrolling
4854 cursorLeft(1, false);
4861 if (vt52Mode
== true) {
4869 if (vt52Mode
== true) {
4870 // G0 --> Special graphics
4871 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
4875 if (vt52Mode
== true) {
4877 currentState
.g0Charset
= CharacterSet
.US
;
4881 if (vt52Mode
== true) {
4883 cursorPosition(0, 0);
4885 // HTS - Horizontal tabulation set
4890 if (vt52Mode
== true) {
4891 // Reverse line feed. Same as RI.
4896 if (vt52Mode
== true) {
4897 // Erase to end of screen
4898 eraseLine(currentState
.cursorX
, width
- 1, false);
4899 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4904 if (vt52Mode
== true) {
4905 // Erase to end of line
4906 eraseLine(currentState
.cursorX
, width
- 1, false);
4912 if (vt52Mode
== true) {
4915 // RI - Reverse index
4920 if (vt52Mode
== false) {
4922 singleshift
= Singleshift
.SS2
;
4926 if (vt52Mode
== false) {
4928 singleshift
= Singleshift
.SS3
;
4935 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4951 if (vt52Mode
== true) {
4952 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
4960 if (vt52Mode
== true) {
4962 // Send string directly to remote side
4963 writeRemote("\033/Z");
4966 // Send string directly to remote side
4967 writeRemote(deviceTypeResponse());
4978 // VT52 cannot get to any of these other states
4979 if (vt52Mode
== true) {
4984 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
4991 // RIS - Reset to initial state
4993 // Do I clear screen too? I think so...
4994 eraseScreen(0, 0, height
- 1, width
- 1, false);
4995 cursorPosition(0, 0);
5009 if ((type
== DeviceType
.VT220
)
5010 || (type
== DeviceType
.XTERM
)) {
5012 // VT220 lockshift G2 into GL
5013 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
5018 if ((type
== DeviceType
.VT220
)
5019 || (type
== DeviceType
.XTERM
)) {
5021 // VT220 lockshift G3 into GL
5022 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
5040 if ((type
== DeviceType
.VT220
)
5041 || (type
== DeviceType
.XTERM
)) {
5043 // VT220 lockshift G3 into GR
5044 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
5049 if ((type
== DeviceType
.VT220
)
5050 || (type
== DeviceType
.XTERM
)) {
5052 // VT220 lockshift G2 into GR
5053 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
5059 if ((type
== DeviceType
.VT220
)
5060 || (type
== DeviceType
.XTERM
)) {
5062 // VT220 lockshift G1 into GR
5063 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
5073 // 0x5B goes to CSI_ENTRY
5075 scanState
= ScanState
.CSI_ENTRY
;
5078 // 0x5D goes to OSC_STRING
5080 scanState
= ScanState
.OSC_STRING
;
5083 // 0x50 goes to DCS_ENTRY
5085 scanState
= ScanState
.DCS_ENTRY
;
5088 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5089 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
5090 scanState
= ScanState
.SOSPMAPC_STRING
;
5095 case ESCAPE_INTERMEDIATE
:
5096 // 00-17, 19, 1C-1F --> execute
5098 handleControlChar(ch
);
5101 // 20-2F --> collect
5102 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5106 // 30-7E --> dispatch, then switch to GROUND
5107 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
5110 if ((collectBuffer
.length() == 1)
5111 && (collectBuffer
.charAt(0) == '(')) {
5112 // G0 --> Special graphics
5113 currentState
.g0Charset
= CharacterSet
.DRAWING
;
5115 if ((collectBuffer
.length() == 1)
5116 && (collectBuffer
.charAt(0) == ')')) {
5117 // G1 --> Special graphics
5118 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5120 if ((type
== DeviceType
.VT220
)
5121 || (type
== DeviceType
.XTERM
)) {
5123 if ((collectBuffer
.length() == 1)
5124 && (collectBuffer
.charAt(0) == '*')) {
5125 // G2 --> Special graphics
5126 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5128 if ((collectBuffer
.length() == 1)
5129 && (collectBuffer
.charAt(0) == '+')) {
5130 // G3 --> Special graphics
5131 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5136 if ((collectBuffer
.length() == 1)
5137 && (collectBuffer
.charAt(0) == '(')) {
5138 // G0 --> Alternate character ROM standard character set
5139 currentState
.g0Charset
= CharacterSet
.ROM
;
5141 if ((collectBuffer
.length() == 1)
5142 && (collectBuffer
.charAt(0) == ')')) {
5143 // G1 --> Alternate character ROM standard character set
5144 currentState
.g1Charset
= CharacterSet
.ROM
;
5148 if ((collectBuffer
.length() == 1)
5149 && (collectBuffer
.charAt(0) == '(')) {
5150 // G0 --> Alternate character ROM special graphics
5151 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5153 if ((collectBuffer
.length() == 1)
5154 && (collectBuffer
.charAt(0) == ')')) {
5155 // G1 --> Alternate character ROM special graphics
5156 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5160 if ((collectBuffer
.length() == 1)
5161 && (collectBuffer
.charAt(0) == '#')) {
5162 // DECDHL - Double-height line (top half)
5167 if ((collectBuffer
.length() == 1)
5168 && (collectBuffer
.charAt(0) == '#')) {
5169 // DECDHL - Double-height line (bottom half)
5172 if ((type
== DeviceType
.VT220
)
5173 || (type
== DeviceType
.XTERM
)) {
5175 if ((collectBuffer
.length() == 1)
5176 && (collectBuffer
.charAt(0) == '(')) {
5178 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5180 if ((collectBuffer
.length() == 1)
5181 && (collectBuffer
.charAt(0) == ')')) {
5183 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5185 if ((collectBuffer
.length() == 1)
5186 && (collectBuffer
.charAt(0) == '*')) {
5188 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5190 if ((collectBuffer
.length() == 1)
5191 && (collectBuffer
.charAt(0) == '+')) {
5193 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5198 if ((collectBuffer
.length() == 1)
5199 && (collectBuffer
.charAt(0) == '#')) {
5200 // DECSWL - Single-width line
5203 if ((type
== DeviceType
.VT220
)
5204 || (type
== DeviceType
.XTERM
)) {
5206 if ((collectBuffer
.length() == 1)
5207 && (collectBuffer
.charAt(0) == '(')) {
5209 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5211 if ((collectBuffer
.length() == 1)
5212 && (collectBuffer
.charAt(0) == ')')) {
5214 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5216 if ((collectBuffer
.length() == 1)
5217 && (collectBuffer
.charAt(0) == '*')) {
5219 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5221 if ((collectBuffer
.length() == 1)
5222 && (collectBuffer
.charAt(0) == '+')) {
5224 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5229 if ((collectBuffer
.length() == 1)
5230 && (collectBuffer
.charAt(0) == '#')) {
5231 // DECDWL - Double-width line
5234 if ((type
== DeviceType
.VT220
)
5235 || (type
== DeviceType
.XTERM
)) {
5237 if ((collectBuffer
.length() == 1)
5238 && (collectBuffer
.charAt(0) == '(')) {
5240 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5242 if ((collectBuffer
.length() == 1)
5243 && (collectBuffer
.charAt(0) == ')')) {
5245 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5247 if ((collectBuffer
.length() == 1)
5248 && (collectBuffer
.charAt(0) == '*')) {
5250 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5252 if ((collectBuffer
.length() == 1)
5253 && (collectBuffer
.charAt(0) == '+')) {
5255 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5260 if ((type
== DeviceType
.VT220
)
5261 || (type
== DeviceType
.XTERM
)) {
5263 if ((collectBuffer
.length() == 1)
5264 && (collectBuffer
.charAt(0) == '(')) {
5266 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5268 if ((collectBuffer
.length() == 1)
5269 && (collectBuffer
.charAt(0) == ')')) {
5271 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5273 if ((collectBuffer
.length() == 1)
5274 && (collectBuffer
.charAt(0) == '*')) {
5276 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5278 if ((collectBuffer
.length() == 1)
5279 && (collectBuffer
.charAt(0) == '+')) {
5281 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5286 if ((collectBuffer
.length() == 1)
5287 && (collectBuffer
.charAt(0) == '#')) {
5288 // DECALN - Screen alignment display
5297 if ((type
== DeviceType
.VT220
)
5298 || (type
== DeviceType
.XTERM
)) {
5300 if ((collectBuffer
.length() == 1)
5301 && (collectBuffer
.charAt(0) == '(')) {
5302 // G0 --> DEC_SUPPLEMENTAL
5303 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5305 if ((collectBuffer
.length() == 1)
5306 && (collectBuffer
.charAt(0) == ')')) {
5307 // G1 --> DEC_SUPPLEMENTAL
5308 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5310 if ((collectBuffer
.length() == 1)
5311 && (collectBuffer
.charAt(0) == '*')) {
5312 // G2 --> DEC_SUPPLEMENTAL
5313 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5315 if ((collectBuffer
.length() == 1)
5316 && (collectBuffer
.charAt(0) == '+')) {
5317 // G3 --> DEC_SUPPLEMENTAL
5318 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5323 if ((type
== DeviceType
.VT220
)
5324 || (type
== DeviceType
.XTERM
)) {
5326 if ((collectBuffer
.length() == 1)
5327 && (collectBuffer
.charAt(0) == '(')) {
5329 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5331 if ((collectBuffer
.length() == 1)
5332 && (collectBuffer
.charAt(0) == ')')) {
5334 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5336 if ((collectBuffer
.length() == 1)
5337 && (collectBuffer
.charAt(0) == '*')) {
5339 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5341 if ((collectBuffer
.length() == 1)
5342 && (collectBuffer
.charAt(0) == '+')) {
5344 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5353 if ((collectBuffer
.length() == 1)
5354 && (collectBuffer
.charAt(0) == '(')) {
5355 // G0 --> United Kingdom set
5356 currentState
.g0Charset
= CharacterSet
.UK
;
5358 if ((collectBuffer
.length() == 1)
5359 && (collectBuffer
.charAt(0) == ')')) {
5360 // G1 --> United Kingdom set
5361 currentState
.g1Charset
= CharacterSet
.UK
;
5363 if ((type
== DeviceType
.VT220
)
5364 || (type
== DeviceType
.XTERM
)) {
5366 if ((collectBuffer
.length() == 1)
5367 && (collectBuffer
.charAt(0) == '*')) {
5368 // G2 --> United Kingdom set
5369 currentState
.g2Charset
= CharacterSet
.UK
;
5371 if ((collectBuffer
.length() == 1)
5372 && (collectBuffer
.charAt(0) == '+')) {
5373 // G3 --> United Kingdom set
5374 currentState
.g3Charset
= CharacterSet
.UK
;
5379 if ((collectBuffer
.length() == 1)
5380 && (collectBuffer
.charAt(0) == '(')) {
5382 currentState
.g0Charset
= CharacterSet
.US
;
5384 if ((collectBuffer
.length() == 1)
5385 && (collectBuffer
.charAt(0) == ')')) {
5387 currentState
.g1Charset
= CharacterSet
.US
;
5389 if ((type
== DeviceType
.VT220
)
5390 || (type
== DeviceType
.XTERM
)) {
5392 if ((collectBuffer
.length() == 1)
5393 && (collectBuffer
.charAt(0) == '*')) {
5395 currentState
.g2Charset
= CharacterSet
.US
;
5397 if ((collectBuffer
.length() == 1)
5398 && (collectBuffer
.charAt(0) == '+')) {
5400 currentState
.g3Charset
= CharacterSet
.US
;
5405 if ((type
== DeviceType
.VT220
)
5406 || (type
== DeviceType
.XTERM
)) {
5408 if ((collectBuffer
.length() == 1)
5409 && (collectBuffer
.charAt(0) == '(')) {
5411 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5413 if ((collectBuffer
.length() == 1)
5414 && (collectBuffer
.charAt(0) == ')')) {
5416 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5418 if ((collectBuffer
.length() == 1)
5419 && (collectBuffer
.charAt(0) == '*')) {
5421 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5423 if ((collectBuffer
.length() == 1)
5424 && (collectBuffer
.charAt(0) == '+')) {
5426 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5433 if ((type
== DeviceType
.VT220
)
5434 || (type
== DeviceType
.XTERM
)) {
5436 if ((collectBuffer
.length() == 1)
5437 && (collectBuffer
.charAt(0) == '(')) {
5439 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5441 if ((collectBuffer
.length() == 1)
5442 && (collectBuffer
.charAt(0) == ')')) {
5444 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5446 if ((collectBuffer
.length() == 1)
5447 && (collectBuffer
.charAt(0) == '*')) {
5449 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5451 if ((collectBuffer
.length() == 1)
5452 && (collectBuffer
.charAt(0) == '+')) {
5454 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5459 if ((type
== DeviceType
.VT220
)
5460 || (type
== DeviceType
.XTERM
)) {
5462 if ((collectBuffer
.length() == 1)
5463 && (collectBuffer
.charAt(0) == ' ')) {
5470 if ((type
== DeviceType
.VT220
)
5471 || (type
== DeviceType
.XTERM
)) {
5473 if ((collectBuffer
.length() == 1)
5474 && (collectBuffer
.charAt(0) == ' ')) {
5481 if ((type
== DeviceType
.VT220
)
5482 || (type
== DeviceType
.XTERM
)) {
5484 if ((collectBuffer
.length() == 1)
5485 && (collectBuffer
.charAt(0) == '(')) {
5487 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5489 if ((collectBuffer
.length() == 1)
5490 && (collectBuffer
.charAt(0) == ')')) {
5492 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5494 if ((collectBuffer
.length() == 1)
5495 && (collectBuffer
.charAt(0) == '*')) {
5497 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5499 if ((collectBuffer
.length() == 1)
5500 && (collectBuffer
.charAt(0) == '+')) {
5502 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5510 if ((type
== DeviceType
.VT220
)
5511 || (type
== DeviceType
.XTERM
)) {
5513 if ((collectBuffer
.length() == 1)
5514 && (collectBuffer
.charAt(0) == '(')) {
5516 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5518 if ((collectBuffer
.length() == 1)
5519 && (collectBuffer
.charAt(0) == ')')) {
5521 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5523 if ((collectBuffer
.length() == 1)
5524 && (collectBuffer
.charAt(0) == '*')) {
5526 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5528 if ((collectBuffer
.length() == 1)
5529 && (collectBuffer
.charAt(0) == '+')) {
5531 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5542 if ((type
== DeviceType
.VT220
)
5543 || (type
== DeviceType
.XTERM
)) {
5545 if ((collectBuffer
.length() == 1)
5546 && (collectBuffer
.charAt(0) == '(')) {
5548 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5550 if ((collectBuffer
.length() == 1)
5551 && (collectBuffer
.charAt(0) == ')')) {
5553 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5555 if ((collectBuffer
.length() == 1)
5556 && (collectBuffer
.charAt(0) == '*')) {
5558 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5560 if ((collectBuffer
.length() == 1)
5561 && (collectBuffer
.charAt(0) == '+')) {
5563 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5568 if ((type
== DeviceType
.VT220
)
5569 || (type
== DeviceType
.XTERM
)) {
5571 if ((collectBuffer
.length() == 1)
5572 && (collectBuffer
.charAt(0) == '(')) {
5574 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5576 if ((collectBuffer
.length() == 1)
5577 && (collectBuffer
.charAt(0) == ')')) {
5579 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5581 if ((collectBuffer
.length() == 1)
5582 && (collectBuffer
.charAt(0) == '*')) {
5584 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5586 if ((collectBuffer
.length() == 1)
5587 && (collectBuffer
.charAt(0) == '+')) {
5589 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5601 if ((type
== DeviceType
.VT220
)
5602 || (type
== DeviceType
.XTERM
)) {
5604 if ((collectBuffer
.length() == 1)
5605 && (collectBuffer
.charAt(0) == '(')) {
5607 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5609 if ((collectBuffer
.length() == 1)
5610 && (collectBuffer
.charAt(0) == ')')) {
5612 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5614 if ((collectBuffer
.length() == 1)
5615 && (collectBuffer
.charAt(0) == '*')) {
5617 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5619 if ((collectBuffer
.length() == 1)
5620 && (collectBuffer
.charAt(0) == '+')) {
5622 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5627 if ((type
== DeviceType
.VT220
)
5628 || (type
== DeviceType
.XTERM
)) {
5630 if ((collectBuffer
.length() == 1)
5631 && (collectBuffer
.charAt(0) == '(')) {
5633 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5635 if ((collectBuffer
.length() == 1)
5636 && (collectBuffer
.charAt(0) == ')')) {
5638 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5640 if ((collectBuffer
.length() == 1)
5641 && (collectBuffer
.charAt(0) == '*')) {
5643 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5645 if ((collectBuffer
.length() == 1)
5646 && (collectBuffer
.charAt(0) == '+')) {
5648 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5695 // 0x9C goes to GROUND
5703 // 00-17, 19, 1C-1F --> execute
5705 handleControlChar(ch
);
5708 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5709 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5711 scanState
= ScanState
.CSI_INTERMEDIATE
;
5714 // 30-39, 3B --> param, then switch to CSI_PARAM
5715 if ((ch
>= '0') && (ch
<= '9')) {
5717 scanState
= ScanState
.CSI_PARAM
;
5721 scanState
= ScanState
.CSI_PARAM
;
5724 // 3C-3F --> collect, then switch to CSI_PARAM
5725 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5727 scanState
= ScanState
.CSI_PARAM
;
5730 // 40-7E --> dispatch, then switch to GROUND
5731 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5734 // ICH - Insert character
5742 // CUD - Cursor down
5746 // CUF - Cursor forward
5750 // CUB - Cursor backward
5754 // CNL - Cursor down and to column 1
5755 if (type
== DeviceType
.XTERM
) {
5760 // CPL - Cursor up and to column 1
5761 if (type
== DeviceType
.XTERM
) {
5766 // CHA - Cursor to column # in current row
5767 if (type
== DeviceType
.XTERM
) {
5772 // CUP - Cursor position
5776 // CHT - Cursor forward X tab stops (default 1)
5777 if (type
== DeviceType
.XTERM
) {
5782 // ED - Erase in display
5786 // EL - Erase in line
5801 // DCH - Delete character
5808 // Scroll up X lines (default 1)
5809 if (type
== DeviceType
.XTERM
) {
5814 // Scroll down X lines (default 1)
5815 if (type
== DeviceType
.XTERM
) {
5824 if ((type
== DeviceType
.VT220
)
5825 || (type
== DeviceType
.XTERM
)) {
5827 // ECH - Erase character
5834 // CBT - Cursor backward X tab stops (default 1)
5835 if (type
== DeviceType
.XTERM
) {
5846 // HPA - Cursor to column # in current row. Same as CHA
5847 if (type
== DeviceType
.XTERM
) {
5852 // HPR - Cursor right. Same as CUF
5853 if (type
== DeviceType
.XTERM
) {
5858 // REP - Repeat last char X times
5859 if (type
== DeviceType
.XTERM
) {
5864 // DA - Device attributes
5868 // VPA - Cursor to row, current column.
5869 if (type
== DeviceType
.XTERM
) {
5874 // VPR - Cursor down. Same as CUD
5875 if (type
== DeviceType
.XTERM
) {
5880 // HVP - Horizontal and vertical position
5884 // TBC - Tabulation clear
5888 // Sets an ANSI or DEC private toggle
5892 if ((type
== DeviceType
.VT220
)
5893 || (type
== DeviceType
.XTERM
)) {
5895 // Printer functions
5903 // Sets an ANSI or DEC private toggle
5907 // SGR - Select graphics rendition
5911 // DSR - Device status report
5918 // DECLL - Load leds
5922 // DECSTBM - Set top and bottom margins
5926 // Save cursor (ANSI.SYS)
5927 if (type
== DeviceType
.XTERM
) {
5928 savedState
.cursorX
= currentState
.cursorX
;
5929 savedState
.cursorY
= currentState
.cursorY
;
5933 if (type
== DeviceType
.XTERM
) {
5934 // Window operations
5939 // Restore cursor (ANSI.SYS)
5940 if (type
== DeviceType
.XTERM
) {
5941 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5948 // DECREQTPARM - Request terminal parameters
5964 // 0x9C goes to GROUND
5969 // 0x3A goes to CSI_IGNORE
5971 scanState
= ScanState
.CSI_IGNORE
;
5976 // 00-17, 19, 1C-1F --> execute
5978 handleControlChar(ch
);
5981 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5982 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5984 scanState
= ScanState
.CSI_INTERMEDIATE
;
5987 // 30-39, 3B --> param
5988 if ((ch
>= '0') && (ch
<= '9')) {
5995 // 0x3A goes to CSI_IGNORE
5997 scanState
= ScanState
.CSI_IGNORE
;
5999 // 0x3C-3F goes to CSI_IGNORE
6000 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6001 scanState
= ScanState
.CSI_IGNORE
;
6004 // 40-7E --> dispatch, then switch to GROUND
6005 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6008 // ICH - Insert character
6016 // CUD - Cursor down
6020 // CUF - Cursor forward
6024 // CUB - Cursor backward
6028 // CNL - Cursor down and to column 1
6029 if (type
== DeviceType
.XTERM
) {
6034 // CPL - Cursor up and to column 1
6035 if (type
== DeviceType
.XTERM
) {
6040 // CHA - Cursor to column # in current row
6041 if (type
== DeviceType
.XTERM
) {
6046 // CUP - Cursor position
6050 // CHT - Cursor forward X tab stops (default 1)
6051 if (type
== DeviceType
.XTERM
) {
6056 // ED - Erase in display
6060 // EL - Erase in line
6075 // DCH - Delete character
6082 // Scroll up X lines (default 1)
6083 if (type
== DeviceType
.XTERM
) {
6088 // Scroll down X lines (default 1)
6089 if (type
== DeviceType
.XTERM
) {
6098 if ((type
== DeviceType
.VT220
)
6099 || (type
== DeviceType
.XTERM
)) {
6101 // ECH - Erase character
6108 // CBT - Cursor backward X tab stops (default 1)
6109 if (type
== DeviceType
.XTERM
) {
6120 // HPA - Cursor to column # in current row. Same as CHA
6121 if (type
== DeviceType
.XTERM
) {
6126 // HPR - Cursor right. Same as CUF
6127 if (type
== DeviceType
.XTERM
) {
6132 // REP - Repeat last char X times
6133 if (type
== DeviceType
.XTERM
) {
6138 // DA - Device attributes
6142 // VPA - Cursor to row, current column.
6143 if (type
== DeviceType
.XTERM
) {
6148 // VPR - Cursor down. Same as CUD
6149 if (type
== DeviceType
.XTERM
) {
6154 // HVP - Horizontal and vertical position
6158 // TBC - Tabulation clear
6162 // Sets an ANSI or DEC private toggle
6166 if ((type
== DeviceType
.VT220
)
6167 || (type
== DeviceType
.XTERM
)) {
6169 // Printer functions
6177 // Sets an ANSI or DEC private toggle
6181 // SGR - Select graphics rendition
6185 // DSR - Device status report
6192 // DECLL - Load leds
6196 // DECSTBM - Set top and bottom margins
6202 if (type
== DeviceType
.XTERM
) {
6203 // Window operations
6212 // DECREQTPARM - Request terminal parameters
6229 case CSI_INTERMEDIATE
:
6230 // 00-17, 19, 1C-1F --> execute
6232 handleControlChar(ch
);
6235 // 20-2F --> collect
6236 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6240 // 0x30-3F goes to CSI_IGNORE
6241 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6242 scanState
= ScanState
.CSI_IGNORE
;
6245 // 40-7E --> dispatch, then switch to GROUND
6246 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6298 if (((type
== DeviceType
.VT220
)
6299 || (type
== DeviceType
.XTERM
))
6300 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6302 // DECSCL - compatibility level
6305 if ((type
== DeviceType
.XTERM
)
6306 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6308 // DECSTR - Soft terminal reset
6313 if (((type
== DeviceType
.VT220
)
6314 || (type
== DeviceType
.XTERM
))
6315 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6343 // 00-17, 19, 1C-1F --> execute
6345 handleControlChar(ch
);
6348 // 20-2F --> collect
6349 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6353 // 40-7E --> ignore, then switch to GROUND
6354 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6358 // 20-3F, 7F --> ignore
6364 // 0x9C goes to GROUND
6369 // 0x1B 0x5C goes to GROUND
6374 if ((collectBuffer
.length() > 0)
6375 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6381 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6382 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6384 scanState
= ScanState
.DCS_INTERMEDIATE
;
6387 // 30-39, 3B --> param, then switch to DCS_PARAM
6388 if ((ch
>= '0') && (ch
<= '9')) {
6390 scanState
= ScanState
.DCS_PARAM
;
6394 scanState
= ScanState
.DCS_PARAM
;
6397 // 3C-3F --> collect, then switch to DCS_PARAM
6398 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6400 scanState
= ScanState
.DCS_PARAM
;
6403 // 00-17, 19, 1C-1F, 7F --> ignore
6405 // 0x3A goes to DCS_IGNORE
6407 scanState
= ScanState
.DCS_IGNORE
;
6410 // 0x71 goes to DCS_SIXEL
6412 sixelParseBuffer
= new StringBuilder();
6413 scanState
= ScanState
.DCS_SIXEL
;
6414 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6415 // 0x40-7E goes to DCS_PASSTHROUGH
6416 scanState
= ScanState
.DCS_PASSTHROUGH
;
6420 case DCS_INTERMEDIATE
:
6422 // 0x9C goes to GROUND
6427 // 0x1B 0x5C goes to GROUND
6432 if ((collectBuffer
.length() > 0)
6433 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6439 // 0x30-3F goes to DCS_IGNORE
6440 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6441 scanState
= ScanState
.DCS_IGNORE
;
6444 // 0x40-7E goes to DCS_PASSTHROUGH
6445 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6446 scanState
= ScanState
.DCS_PASSTHROUGH
;
6449 // 00-17, 19, 1C-1F, 7F --> ignore
6454 // 0x9C goes to GROUND
6459 // 0x1B 0x5C goes to GROUND
6464 if ((collectBuffer
.length() > 0)
6465 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6471 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6472 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6474 scanState
= ScanState
.DCS_INTERMEDIATE
;
6477 // 30-39, 3B --> param
6478 if ((ch
>= '0') && (ch
<= '9')) {
6485 // 00-17, 19, 1C-1F, 7F --> ignore
6487 // 0x3A, 3C-3F goes to DCS_IGNORE
6489 scanState
= ScanState
.DCS_IGNORE
;
6491 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6492 scanState
= ScanState
.DCS_IGNORE
;
6495 // 0x71 goes to DCS_SIXEL
6497 sixelParseBuffer
= new StringBuilder();
6498 scanState
= ScanState
.DCS_SIXEL
;
6499 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6500 // 0x40-7E goes to DCS_PASSTHROUGH
6501 scanState
= ScanState
.DCS_PASSTHROUGH
;
6505 case DCS_PASSTHROUGH
:
6506 // 0x9C goes to GROUND
6511 // 0x1B 0x5C goes to GROUND
6516 if ((collectBuffer
.length() > 0)
6517 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6523 // 00-17, 19, 1C-1F, 20-7E --> put
6531 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6534 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6543 // 00-17, 19, 1C-1F, 20-7F --> ignore
6545 // 0x9C goes to GROUND
6553 // 0x9C goes to GROUND
6559 // 0x1B 0x5C goes to GROUND
6564 if ((collectBuffer
.length() > 0)
6565 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6572 // 00-17, 19, 1C-1F, 20-7E --> put
6574 sixelParseBuffer
.append(ch
);
6578 sixelParseBuffer
.append(ch
);
6581 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6582 sixelParseBuffer
.append(ch
);
6585 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6586 sixelParseBuffer
.append(ch
);
6594 case SOSPMAPC_STRING
:
6595 // 00-17, 19, 1C-1F, 20-7F --> ignore
6597 // Special case for Jexer: PM can pass one control character
6602 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6606 // 0x9C goes to GROUND
6614 // Special case for Xterm: OSC can pass control characters
6615 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
6619 // 00-17, 19, 1C-1F --> ignore
6621 // 20-7F --> osc_put
6622 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6626 // 0x9C goes to GROUND
6633 case VT52_DIRECT_CURSOR_ADDRESS
:
6634 // This is a special case for the VT52 sequence "ESC Y l c"
6635 if (collectBuffer
.length() == 0) {
6637 } else if (collectBuffer
.length() == 1) {
6638 // We've got the two characters, one in the buffer and the
6640 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
6649 * Expose current cursor X to outside world.
6651 * @return current cursor X
6653 public final int getCursorX() {
6654 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6655 return currentState
.cursorX
* 2;
6657 return currentState
.cursorX
;
6661 * Expose current cursor Y to outside world.
6663 * @return current cursor Y
6665 public final int getCursorY() {
6666 return currentState
.cursorY
;
6670 * Returns true if this terminal has requested the mouse pointer be
6673 * @return true if this terminal has requested the mouse pointer be
6676 public final boolean hasHiddenMousePointer() {
6677 return hideMousePointer
;
6681 * Get the mouse protocol.
6683 * @return MouseProtocol.OFF, MouseProtocol.X10, etc.
6685 public MouseProtocol
getMouseProtocol() {
6686 return mouseProtocol
;
6689 // ------------------------------------------------------------------------
6690 // Sixel support ----------------------------------------------------------
6691 // ------------------------------------------------------------------------
6694 * Set the width of a character cell in pixels.
6696 * @param textWidth the width in pixels of a character cell
6698 public void setTextWidth(final int textWidth
) {
6699 this.textWidth
= textWidth
;
6703 * Set the height of a character cell in pixels.
6705 * @param textHeight the height in pixels of a character cell
6707 public void setTextHeight(final int textHeight
) {
6708 this.textHeight
= textHeight
;
6712 * Parse a sixel string into a bitmap image, and overlay that image onto
6715 private void parseSixel() {
6718 System.err.println("parseSixel(): '" + sixelParseBuffer.toString()
6722 Sixel sixel
= new Sixel(sixelParseBuffer
.toString());
6723 BufferedImage image
= sixel
.getImage();
6725 // System.err.println("parseSixel(): image " + image);
6727 if (image
== null) {
6728 // Sixel data was malformed in some way, bail out.
6735 * Break up the image into text cell sized pieces as a new array of
6738 * Note original column position x0.
6742 * 1. Advance (printCharacter(' ')) for horizontal increment, or
6743 * index (linefeed() + cursorPosition(y, x0)) for vertical
6746 * 2. Set (x, y) cell image data.
6748 * 3. For the right and bottom edges:
6750 * a. Render the text to pixels using Terminus font.
6752 * b. Blit the image on top of the text, using alpha channel.
6754 int cellColumns
= image
.getWidth() / textWidth
;
6755 if (cellColumns
* textWidth
< image
.getWidth()) {
6758 int cellRows
= image
.getHeight() / textHeight
;
6759 if (cellRows
* textHeight
< image
.getHeight()) {
6763 // Break the image up into an array of cells.
6764 Cell
[][] cells
= new Cell
[cellColumns
][cellRows
];
6766 for (int x
= 0; x
< cellColumns
; x
++) {
6767 for (int y
= 0; y
< cellRows
; y
++) {
6769 int width
= textWidth
;
6770 if ((x
+ 1) * textWidth
> image
.getWidth()) {
6771 width
= image
.getWidth() - (x
* textWidth
);
6773 int height
= textHeight
;
6774 if ((y
+ 1) * textHeight
> image
.getHeight()) {
6775 height
= image
.getHeight() - (y
* textHeight
);
6778 Cell cell
= new Cell();
6779 cell
.setImage(image
.getSubimage(x
* textWidth
,
6780 y
* textHeight
, width
, height
));
6786 int x0
= currentState
.cursorX
;
6787 for (int y
= 0; y
< cellRows
; y
++) {
6788 for (int x
= 0; x
< cellColumns
; x
++) {
6789 printCharacter(' ');
6790 cursorLeft(1, false);
6791 if ((x
== cellColumns
- 1) || (y
== cellRows
- 1)) {
6792 // TODO: render text of current cell first, then image
6793 // over it. For now, just copy the cell.
6794 DisplayLine line
= display
.get(currentState
.cursorY
);
6795 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
6797 // Copy the image cell into the display.
6798 DisplayLine line
= display
.get(currentState
.cursorY
);
6799 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
6801 cursorRight(1, false);
6804 cursorPosition(currentState
.cursorY
, x0
);