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
.backend
.GlyphMaker
;
51 import jexer
.bits
.Color
;
52 import jexer
.bits
.Cell
;
53 import jexer
.bits
.CellAttributes
;
54 import jexer
.bits
.StringUtils
;
55 import jexer
.event
.TMouseEvent
;
56 import jexer
.io
.ReadTimeoutException
;
57 import jexer
.io
.TimeoutInputStream
;
58 import static jexer
.TKeypress
.*;
61 * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
62 * including a scrollback buffer.
65 * It currently implements VT100, VT102, VT220, and XTERM with the following
69 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
70 * failure with XTERM. This is due to vttest failing to decode the UTF-8
74 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
75 * from VT100 are not supported.
78 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
79 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
80 * because DECSCL is not supported, it will fail the last part of the
81 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
84 * - Numeric/application keys from the number pad are not supported because
85 * they are not exposed from the TKeypress API.
88 * - VT52 HOLD SCREEN mode is not supported.
91 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
92 * numerators) are not rendered correctly.
95 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
97 public class ECMA48
implements Runnable
{
99 // ------------------------------------------------------------------------
100 // Constants --------------------------------------------------------------
101 // ------------------------------------------------------------------------
104 * The emulator can emulate several kinds of terminals.
106 public enum DeviceType
{
108 * DEC VT100 but also including the three VT102 functions.
129 * Parser character scan states.
131 private enum ScanState
{
147 VT52_DIRECT_CURSOR_ADDRESS
151 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
152 * can't really use it in keypress() because we do not see number pad
153 * events from TKeypress.
155 private enum KeypadMode
{
161 * Arrow keys can emit three different sequences (DECCKM or VT52
164 private enum ArrowKeyMode
{
171 * Available character sets for GL, GR, G0, G1, G2, G3.
173 private enum CharacterSet
{
194 * Single-shift states used by the C1 control characters SS2 (0x8E) and
197 private enum Singleshift
{
204 * VT220+ lockshift states.
206 private enum LockshiftMode
{
216 * XTERM mouse reporting protocols.
218 public enum MouseProtocol
{
227 * XTERM mouse reporting encodings.
229 private enum MouseEncoding
{
235 // ------------------------------------------------------------------------
236 // Variables --------------------------------------------------------------
237 // ------------------------------------------------------------------------
240 * The enclosing listening object.
242 private DisplayListener displayListener
;
245 * When true, the reader thread is expected to exit.
247 private volatile boolean stopReaderThread
= false;
252 private Thread readerThread
= null;
255 * The type of emulator to be.
257 private DeviceType type
= DeviceType
.VT102
;
260 * The scrollback buffer characters + attributes.
262 private volatile ArrayList
<DisplayLine
> scrollback
;
265 * The raw display buffer characters + attributes.
267 private volatile ArrayList
<DisplayLine
> display
;
270 * The maximum number of lines in the scrollback buffer.
272 private int maxScrollback
= 10000;
275 * The terminal's input. For type == XTERM, this is an InputStreamReader
276 * with UTF-8 encoding.
278 private Reader input
;
281 * The terminal's raw InputStream. This is used for type != XTERM.
283 private volatile TimeoutInputStream inputStream
;
286 * The terminal's output. For type == XTERM, this wraps an
287 * OutputStreamWriter with UTF-8 encoding.
289 private Writer output
;
292 * The terminal's raw OutputStream. This is used for type != XTERM.
294 private OutputStream outputStream
;
297 * Current scanning state.
299 private ScanState scanState
;
302 * Which mouse protocol is active.
304 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
307 * Which mouse encoding is active.
309 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
312 * A terminal may request that the mouse pointer be hidden using a
313 * Privacy Message containing either "hideMousePointer" or
314 * "showMousePointer". This is currently only used within Jexer by
315 * TTerminalWindow so that only the bottom-most instance of nested
316 * Jexer's draws the mouse within its application window.
318 private boolean hideMousePointer
= false;
321 * Physical display width. We start at 80x24, but the user can resize us
327 * Physical display height. We start at 80x24, but the user can resize
333 * Top margin of the scrolling region.
335 private int scrollRegionTop
;
338 * Bottom margin of the scrolling region.
340 private int scrollRegionBottom
;
343 * Right margin column number. This can be selected by the remote side
344 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
346 private int rightMargin
;
349 * Last character printed.
354 * VT100-style line wrapping: a character is placed in column 80 (or
355 * 132), but the line does NOT wrap until another character is written to
356 * column 1 of the next line, after which the cursor moves to column 2.
358 private boolean wrapLineFlag
;
361 * VT220 single shift flag.
363 private Singleshift singleshift
= Singleshift
.NONE
;
366 * true = insert characters, false = overwrite.
368 private boolean insertMode
= false;
371 * VT52 mode as selected by DECANM. True means VT52, false means
372 * ANSI. Default is ANSI.
374 private boolean vt52Mode
= false;
377 * Visible cursor (DECTCEM).
379 private boolean cursorVisible
= true;
382 * Screen title as set by the xterm OSC sequence. Lots of applications
383 * send a screenTitle regardless of whether it is an xterm client or not.
385 private String screenTitle
= "";
388 * Parameter characters being collected.
390 private List
<Integer
> csiParams
;
393 * Non-csi collect buffer.
395 private StringBuilder collectBuffer
;
398 * When true, use the G1 character set.
400 private boolean shiftOut
= false;
403 * Horizontal tab stop locations.
405 private List
<Integer
> tabStops
;
408 * S8C1T. True means 8bit controls, false means 7bit controls.
410 private boolean s8c1t
= false;
413 * Printer mode. True means send all output to printer, which discards
416 private boolean printerControllerMode
= false;
419 * LMN line mode. If true, linefeed() puts the cursor on the first
420 * column of the next line. If false, linefeed() puts the cursor one
421 * line down on the current line. The default is false.
423 private boolean newLineMode
= false;
426 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
428 private ArrowKeyMode arrowKeyMode
;
431 * Whether number pad keys send VT100 or VT52, application or numeric
434 @SuppressWarnings("unused")
435 private KeypadMode keypadMode
;
438 * When true, the terminal is in 132-column mode (DECCOLM).
440 private boolean columns132
= false;
443 * true = reverse video. Set by DECSCNM.
445 private boolean reverseVideo
= false;
448 * false = echo characters locally.
450 private boolean fullDuplex
= true;
453 * The current terminal state.
455 private SaveableState currentState
;
458 * The last saved terminal state.
460 private SaveableState savedState
;
463 * The 88- or 256-color support RGB colors.
465 private List
<Integer
> colors88
;
468 * Sixel collection buffer.
470 private StringBuilder sixelParseBuffer
;
473 * The width of a character cell in pixels.
475 private int textWidth
= 16;
478 * The height of a character cell in pixels.
480 private int textHeight
= 20;
483 * The last used height of a character cell in pixels, only used for
486 private int lastTextHeight
= -1;
489 * The glyph drawer for full-width chars.
491 private GlyphMaker glyphMaker
= null;
494 * DECSC/DECRC save/restore a subset of the total state. This class
495 * encapsulates those specific flags/modes.
497 private class SaveableState
{
500 * When true, cursor positions are relative to the scrolling region.
502 public boolean originMode
= false;
505 * The current editing X position.
507 public int cursorX
= 0;
510 * The current editing Y position.
512 public int cursorY
= 0;
515 * Which character set is currently selected in G0.
517 public CharacterSet g0Charset
= CharacterSet
.US
;
520 * Which character set is currently selected in G1.
522 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
525 * Which character set is currently selected in G2.
527 public CharacterSet g2Charset
= CharacterSet
.US
;
530 * Which character set is currently selected in G3.
532 public CharacterSet g3Charset
= CharacterSet
.US
;
535 * Which character set is currently selected in GR.
537 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
540 * The current drawing attributes.
542 public CellAttributes attr
;
547 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
552 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
557 public boolean lineWrap
= true;
562 public void reset() {
566 g0Charset
= CharacterSet
.US
;
567 g1Charset
= CharacterSet
.DRAWING
;
568 g2Charset
= CharacterSet
.US
;
569 g3Charset
= CharacterSet
.US
;
570 grCharset
= CharacterSet
.DRAWING
;
571 attr
= new CellAttributes();
572 glLockshift
= LockshiftMode
.NONE
;
573 grLockshift
= LockshiftMode
.NONE
;
578 * Copy attributes from another instance.
580 * @param that the other instance to match
582 public void setTo(final SaveableState that
) {
583 this.originMode
= that
.originMode
;
584 this.cursorX
= that
.cursorX
;
585 this.cursorY
= that
.cursorY
;
586 this.g0Charset
= that
.g0Charset
;
587 this.g1Charset
= that
.g1Charset
;
588 this.g2Charset
= that
.g2Charset
;
589 this.g3Charset
= that
.g3Charset
;
590 this.grCharset
= that
.grCharset
;
591 this.attr
= new CellAttributes();
592 this.attr
.setTo(that
.attr
);
593 this.glLockshift
= that
.glLockshift
;
594 this.grLockshift
= that
.grLockshift
;
595 this.lineWrap
= that
.lineWrap
;
599 * Public constructor.
601 public SaveableState() {
606 // ------------------------------------------------------------------------
607 // Constructors -----------------------------------------------------------
608 // ------------------------------------------------------------------------
611 * Public constructor.
613 * @param type one of the DeviceType constants to select VT100, VT102,
615 * @param inputStream an InputStream connected to the remote side. For
616 * type == XTERM, inputStream is converted to a Reader with UTF-8
618 * @param outputStream an OutputStream connected to the remote user. For
619 * type == XTERM, outputStream is converted to a Writer with UTF-8
621 * @param displayListener a callback to the outer display, or null for
622 * default VT100 behavior
623 * @throws UnsupportedEncodingException if an exception is thrown when
624 * creating the InputStreamReader
626 public ECMA48(final DeviceType type
, final InputStream inputStream
,
627 final OutputStream outputStream
, final DisplayListener displayListener
)
628 throws UnsupportedEncodingException
{
630 assert (inputStream
!= null);
631 assert (outputStream
!= null);
633 csiParams
= new ArrayList
<Integer
>();
634 tabStops
= new ArrayList
<Integer
>();
635 scrollback
= new ArrayList
<DisplayLine
>();
636 display
= new ArrayList
<DisplayLine
>();
639 if (inputStream
instanceof TimeoutInputStream
) {
640 this.inputStream
= (TimeoutInputStream
)inputStream
;
642 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
644 if (type
== DeviceType
.XTERM
) {
645 this.input
= new InputStreamReader(this.inputStream
, "UTF-8");
646 this.output
= new OutputStreamWriter(new
647 BufferedOutputStream(outputStream
), "UTF-8");
648 this.outputStream
= null;
651 this.outputStream
= new BufferedOutputStream(outputStream
);
653 this.displayListener
= displayListener
;
656 for (int i
= 0; i
< height
; i
++) {
657 display
.add(new DisplayLine(currentState
.attr
));
660 // Spin up the input reader
661 readerThread
= new Thread(this);
662 readerThread
.start();
665 // ------------------------------------------------------------------------
666 // Runnable ---------------------------------------------------------------
667 // ------------------------------------------------------------------------
670 * Read function runs on a separate thread.
672 public final void run() {
673 boolean utf8
= false;
674 boolean done
= false;
676 if (type
== DeviceType
.XTERM
) {
680 // available() will often return > 1, so we need to read in chunks to
682 char [] readBufferUTF8
= null;
683 byte [] readBuffer
= null;
685 readBufferUTF8
= new char[128];
687 readBuffer
= new byte[128];
690 while (!done
&& !stopReaderThread
) {
692 int n
= inputStream
.available();
694 // System.err.printf("available() %d\n", n); System.err.flush();
696 if (readBufferUTF8
.length
< n
) {
697 // The buffer wasn't big enough, make it huger
698 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
701 readBufferUTF8
= new char[newSizeHalf
* 2];
704 if (readBuffer
.length
< n
) {
705 // The buffer wasn't big enough, make it huger
706 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
707 readBuffer
= new byte[newSizeHalf
* 2];
713 } catch (InterruptedException e
) {
722 rc
= input
.read(readBufferUTF8
, 0,
723 readBufferUTF8
.length
);
725 rc
= inputStream
.read(readBuffer
, 0,
728 } catch (ReadTimeoutException e
) {
732 // System.err.printf("read() %d\n", rc); System.err.flush();
737 // Don't step on UI events
738 synchronized (this) {
739 for (int i
= 0; i
< rc
; i
++) {
742 ch
= readBufferUTF8
[i
];
750 // Permit my enclosing UI to know that I updated.
751 if (displayListener
!= null) {
752 displayListener
.displayChanged();
755 // System.err.println("end while loop"); System.err.flush();
756 } catch (IOException e
) {
759 // This is an unusual case. We want to see the stack trace,
760 // but it is related to the spawned process rather than the
761 // actual UI. We will generate the stack trace, and consume
762 // it as though it was emitted by the shell.
763 CharArrayWriter writer
= new CharArrayWriter();
764 // Send a ST and RIS to clear the emulator state.
766 writer
.write("\033\\\033c");
767 writer
.write("\n-----------------------------------\n");
768 e
.printStackTrace(new PrintWriter(writer
));
769 writer
.write("\n-----------------------------------\n");
770 } catch (IOException e2
) {
773 char [] stackTrace
= writer
.toCharArray();
774 for (int i
= 0; i
< stackTrace
.length
; i
++) {
775 if (stackTrace
[i
] == '\n') {
778 consume(stackTrace
[i
]);
782 } // while ((done == false) && (stopReaderThread == false))
784 // Let the rest of the world know that I am done.
785 stopReaderThread
= true;
788 inputStream
.cancelRead();
791 } catch (IOException e
) {
797 } catch (IOException e
) {
801 // Permit my enclosing UI to know that I updated.
802 if (displayListener
!= null) {
803 displayListener
.displayChanged();
806 // System.err.println("*** run() exiting..."); System.err.flush();
809 // ------------------------------------------------------------------------
810 // ECMA48 -----------------------------------------------------------------
811 // ------------------------------------------------------------------------
814 * Return the proper primary Device Attributes string.
816 * @return string to send to remote side that is appropriate for the
819 private String
deviceTypeResponse() {
822 // "I am a VT100 with advanced video option" (often VT102)
831 // "I am a VT220" - 7 bit version
833 return "\033[?62;1;6c";
835 // "I am a VT220" - 8 bit version
836 return "\u009b?62;1;6c";
838 throw new IllegalArgumentException("Invalid device type: " + type
);
843 * Return the proper TERM environment variable for this device type.
845 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
846 * @return "vt100", "xterm", etc.
848 public static String
deviceTypeTerm(final DeviceType deviceType
) {
849 switch (deviceType
) {
863 throw new IllegalArgumentException("Invalid device type: "
869 * Return the proper LANG for this device type. Only XTERM devices know
870 * about UTF-8, the others are defined by their standard to be either
871 * 7-bit or 8-bit characters only.
873 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
874 * @param baseLang a base language without UTF-8 flag such as "C" or
876 * @return "en_US", "en_US.UTF-8", etc.
878 public static String
deviceTypeLang(final DeviceType deviceType
,
879 final String baseLang
) {
881 switch (deviceType
) {
889 return baseLang
+ ".UTF-8";
892 throw new IllegalArgumentException("Invalid device type: "
898 * Write a string directly to the remote side.
900 * @param str string to send
902 public void writeRemote(final String str
) {
903 if (stopReaderThread
) {
904 // Reader hit EOF, bail out now.
909 // System.err.printf("writeRemote() '%s'\n", str);
915 if (outputStream
== null) {
919 outputStream
.flush();
920 for (int i
= 0; i
< str
.length(); i
++) {
921 outputStream
.write(str
.charAt(i
));
923 outputStream
.flush();
924 } catch (IOException e
) {
930 if (output
== null) {
937 } catch (IOException e
) {
943 throw new IllegalArgumentException("Invalid device type: " + type
);
948 * Close the input and output streams and stop the reader thread. Note
949 * that it is safe to call this multiple times.
951 public final void close() {
953 // Tell the reader thread to stop looking at input. It will close
954 // the input streams.
955 if (stopReaderThread
== false) {
956 stopReaderThread
= true;
958 readerThread
.join(1000);
959 } catch (InterruptedException e
) {
964 // Now close the output stream.
969 if (outputStream
!= null) {
971 outputStream
.close();
972 } catch (IOException e
) {
979 if (outputStream
!= null) {
981 outputStream
.close();
982 } catch (IOException e
) {
987 if (output
!= null) {
990 } catch (IOException e
) {
997 throw new IllegalArgumentException("Invalid device type: " +
1003 * See if the reader thread is still running.
1005 * @return if true, we are still connected to / reading from the remote
1008 public final boolean isReading() {
1009 return (!stopReaderThread
);
1013 * Obtain a new blank display line for an external user
1014 * (e.g. TTerminalWindow).
1016 * @return new blank line
1018 public final DisplayLine
getBlankDisplayLine() {
1019 return new DisplayLine(currentState
.attr
);
1023 * Get the scrollback buffer.
1025 * @return the scrollback buffer
1027 public final List
<DisplayLine
> getScrollbackBuffer() {
1032 * Get the display buffer.
1034 * @return the display buffer
1036 public final List
<DisplayLine
> getDisplayBuffer() {
1041 * Get the display width.
1043 * @return the width (usually 80 or 132)
1045 public final int getWidth() {
1050 * Set the display width.
1052 * @param width the new width
1054 public final void setWidth(final int width
) {
1056 rightMargin
= width
- 1;
1057 if (currentState
.cursorX
>= width
) {
1058 currentState
.cursorX
= width
- 1;
1060 if (savedState
.cursorX
>= width
) {
1061 savedState
.cursorX
= width
- 1;
1066 * Get the display height.
1068 * @return the height (usually 24)
1070 public final int getHeight() {
1075 * Set the display height.
1077 * @param height the new height
1079 public final void setHeight(final int height
) {
1080 int delta
= height
- this.height
;
1081 this.height
= height
;
1082 scrollRegionBottom
+= delta
;
1083 if (scrollRegionBottom
< 0) {
1084 scrollRegionBottom
= height
;
1086 if (scrollRegionTop
>= scrollRegionBottom
) {
1087 scrollRegionTop
= 0;
1089 if (currentState
.cursorY
>= height
) {
1090 currentState
.cursorY
= height
- 1;
1092 if (savedState
.cursorY
>= height
) {
1093 savedState
.cursorY
= height
- 1;
1095 while (display
.size() < height
) {
1096 DisplayLine line
= new DisplayLine(currentState
.attr
);
1097 line
.setReverseColor(reverseVideo
);
1100 while (display
.size() > height
) {
1101 scrollback
.add(display
.remove(0));
1106 * Get visible cursor flag.
1108 * @return if true, the cursor is visible
1110 public final boolean isCursorVisible() {
1111 return cursorVisible
;
1115 * Get the screen title as set by the xterm OSC sequence. Lots of
1116 * applications send a screenTitle regardless of whether it is an xterm
1119 * @return screen title
1121 public final String
getScreenTitle() {
1126 * Get 132 columns value.
1128 * @return if true, the terminal is in 132 column mode
1130 public final boolean isColumns132() {
1135 * Clear the CSI parameters and flags.
1137 private void toGround() {
1139 collectBuffer
= new StringBuilder(8);
1140 scanState
= ScanState
.GROUND
;
1144 * Reset the tab stops list.
1146 private void resetTabStops() {
1148 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
1149 tabStops
.add(Integer
.valueOf(i
* 8));
1154 * Reset the 88- or 256-colors.
1156 private void resetColors() {
1157 colors88
= new ArrayList
<Integer
>(256);
1158 for (int i
= 0; i
< 256; i
++) {
1162 // Set default system colors.
1163 colors88
.set(0, 0x00000000);
1164 colors88
.set(1, 0x00a80000);
1165 colors88
.set(2, 0x0000a800);
1166 colors88
.set(3, 0x00a85400);
1167 colors88
.set(4, 0x000000a8);
1168 colors88
.set(5, 0x00a800a8);
1169 colors88
.set(6, 0x0000a8a8);
1170 colors88
.set(7, 0x00a8a8a8);
1172 colors88
.set(8, 0x00545454);
1173 colors88
.set(9, 0x00fc5454);
1174 colors88
.set(10, 0x0054fc54);
1175 colors88
.set(11, 0x00fcfc54);
1176 colors88
.set(12, 0x005454fc);
1177 colors88
.set(13, 0x00fc54fc);
1178 colors88
.set(14, 0x0054fcfc);
1179 colors88
.set(15, 0x00fcfcfc);
1183 * Get the RGB value of one of the indexed colors.
1185 * @param index the color index
1186 * @return the RGB value
1188 private int get88Color(final int index
) {
1189 // System.err.print("get88Color: " + index);
1190 if ((index
< 0) || (index
> colors88
.size())) {
1191 // System.err.println(" -- UNKNOWN");
1194 // System.err.printf(" %08x\n", colors88.get(index));
1195 return colors88
.get(index
);
1199 * Set one of the indexed colors to a color specification.
1201 * @param index the color index
1202 * @param spec the specification, typically something like "rgb:aa/bb/cc"
1204 private void set88Color(final int index
, final String spec
) {
1205 // System.err.println("set88Color: " + index + " '" + spec + "'");
1207 if ((index
< 0) || (index
> colors88
.size())) {
1210 if (spec
.startsWith("rgb:")) {
1211 String
[] rgbTokens
= spec
.substring(4).split("/");
1212 if (rgbTokens
.length
== 3) {
1214 int rgb
= (Integer
.parseInt(rgbTokens
[0], 16) << 16);
1215 rgb
|= Integer
.parseInt(rgbTokens
[1], 16) << 8;
1216 rgb
|= Integer
.parseInt(rgbTokens
[2], 16);
1217 // System.err.printf(" set to %08x\n", rgb);
1218 colors88
.set(index
, rgb
);
1219 } catch (NumberFormatException e
) {
1226 if (spec
.toLowerCase().equals("black")) {
1227 colors88
.set(index
, 0x00000000);
1228 } else if (spec
.toLowerCase().equals("red")) {
1229 colors88
.set(index
, 0x00a80000);
1230 } else if (spec
.toLowerCase().equals("green")) {
1231 colors88
.set(index
, 0x0000a800);
1232 } else if (spec
.toLowerCase().equals("yellow")) {
1233 colors88
.set(index
, 0x00a85400);
1234 } else if (spec
.toLowerCase().equals("blue")) {
1235 colors88
.set(index
, 0x000000a8);
1236 } else if (spec
.toLowerCase().equals("magenta")) {
1237 colors88
.set(index
, 0x00a800a8);
1238 } else if (spec
.toLowerCase().equals("cyan")) {
1239 colors88
.set(index
, 0x0000a8a8);
1240 } else if (spec
.toLowerCase().equals("white")) {
1241 colors88
.set(index
, 0x00a8a8a8);
1247 * Reset the emulation state.
1249 private void reset() {
1251 currentState
= new SaveableState();
1252 savedState
= new SaveableState();
1253 scanState
= ScanState
.GROUND
;
1256 scrollRegionTop
= 0;
1257 scrollRegionBottom
= height
- 1;
1258 rightMargin
= width
- 1;
1259 newLineMode
= false;
1260 arrowKeyMode
= ArrowKeyMode
.ANSI
;
1261 keypadMode
= KeypadMode
.Numeric
;
1262 wrapLineFlag
= false;
1263 if (displayListener
!= null) {
1264 width
= displayListener
.getDisplayWidth();
1265 height
= displayListener
.getDisplayHeight();
1266 rightMargin
= width
- 1;
1274 newLineMode
= false;
1275 reverseVideo
= false;
1277 cursorVisible
= true;
1280 singleshift
= Singleshift
.NONE
;
1282 printerControllerMode
= false;
1285 mouseProtocol
= MouseProtocol
.OFF
;
1286 mouseEncoding
= MouseEncoding
.X10
;
1291 // Reset extra colors
1299 * Append a new line to the bottom of the display, adding lines off the
1300 * top to the scrollback buffer.
1302 private void newDisplayLine() {
1303 // Scroll the top line off into the scrollback buffer
1304 scrollback
.add(display
.get(0));
1305 if (scrollback
.size() > maxScrollback
) {
1306 scrollback
.remove(0);
1307 scrollback
.trimToSize();
1310 display
.trimToSize();
1311 DisplayLine line
= new DisplayLine(currentState
.attr
);
1312 line
.setReverseColor(reverseVideo
);
1317 * Wraps the current line.
1319 private void wrapCurrentLine() {
1320 if (currentState
.cursorY
== height
- 1) {
1323 if (currentState
.cursorY
< height
- 1) {
1324 currentState
.cursorY
++;
1326 currentState
.cursorX
= 0;
1330 * Handle a carriage return.
1332 private void carriageReturn() {
1333 currentState
.cursorX
= 0;
1334 wrapLineFlag
= false;
1338 * Reverse the color of the visible display.
1340 private void invertDisplayColors() {
1341 for (DisplayLine line
: display
) {
1342 line
.setReverseColor(!line
.isReverseColor());
1347 * Handle a linefeed.
1349 private void linefeed() {
1351 if (currentState
.cursorY
< scrollRegionBottom
) {
1352 // Increment screen y
1353 currentState
.cursorY
++;
1356 // Screen y does not increment
1359 * Two cases: either we're inside a scrolling region or not. If
1360 * the scrolling region bottom is the bottom of the screen, then
1361 * push the top line into the buffer. Else scroll the scrolling
1364 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1366 // We're at the bottom of the scroll region, AND the scroll
1367 // region is the entire screen.
1373 // We're at the bottom of the scroll region, AND the scroll
1374 // region is NOT the entire screen.
1375 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1380 currentState
.cursorX
= 0;
1382 wrapLineFlag
= false;
1386 * Prints one character to the display buffer.
1388 * @param ch character to display
1390 private void printCharacter(final char ch
) {
1391 int rightMargin
= this.rightMargin
;
1393 if (StringUtils
.width(ch
) == 2) {
1394 // This is a full-width character. Save two spaces, and then
1395 // draw the character as two image halves.
1396 int x0
= currentState
.cursorX
;
1397 int y0
= currentState
.cursorY
;
1398 printCharacter(' ');
1399 printCharacter(' ');
1400 if ((currentState
.cursorX
== x0
+ 2)
1401 && (currentState
.cursorY
== y0
)
1403 // We can draw both halves of the character.
1404 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1405 } else if ((currentState
.cursorX
== x0
+ 1)
1406 && (currentState
.cursorY
== y0
)
1408 // VT100 line wrap behavior: we should be at the right
1409 // margin. We can draw both halves of the character.
1410 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1412 // The character splits across the line. Draw the entire
1413 // character on the new line, giving one more space for it.
1414 x0
= currentState
.cursorX
- 1;
1415 y0
= currentState
.cursorY
;
1416 printCharacter(' ');
1417 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1422 // Check if we have double-width, and if so chop at 40/66 instead of
1424 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1425 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1428 // Check the unusually-complicated line wrapping conditions...
1429 if (currentState
.cursorX
== rightMargin
) {
1431 if (currentState
.lineWrap
== true) {
1433 * This case happens when: the cursor was already on the
1434 * right margin (either through printing or by an explicit
1435 * placement command), and a character was printed.
1437 * The line wraps only when a new character arrives AND the
1438 * cursor is already on the right margin AND has placed a
1439 * character in its cell. Easier to see than to explain.
1441 if (wrapLineFlag
== false) {
1443 * This block marks the case that we are in the margin
1444 * and the first character has been received and printed.
1446 wrapLineFlag
= true;
1449 * This block marks the case that we are in the margin
1450 * and the second character has been received and
1453 wrapLineFlag
= false;
1457 } else if (currentState
.cursorX
<= rightMargin
) {
1459 * This is the normal case: a character came in and was printed
1460 * to the left of the right margin column.
1463 // Turn off VT100 special-case flag
1464 wrapLineFlag
= false;
1467 // "Print" the character
1468 Cell newCell
= new Cell(ch
);
1469 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1470 newCellAttributes
.setTo(currentState
.attr
);
1471 DisplayLine line
= display
.get(currentState
.cursorY
);
1473 if (StringUtils
.width(ch
) == 1) {
1474 // Insert mode special case
1475 if (insertMode
== true) {
1476 line
.insert(currentState
.cursorX
, newCell
);
1478 // Replace an existing character
1479 line
.replace(currentState
.cursorX
, newCell
);
1482 // Increment horizontal
1483 if (wrapLineFlag
== false) {
1484 currentState
.cursorX
++;
1485 if (currentState
.cursorX
> rightMargin
) {
1486 currentState
.cursorX
--;
1493 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1494 * send to the remote side.
1496 * @param mouse mouse event received from the local user
1498 public void mouse(final TMouseEvent mouse
) {
1501 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1502 mouseProtocol, mouseEncoding, mouse);
1505 if (mouseEncoding
== MouseEncoding
.X10
) {
1506 // We will support X10 but only for (160,94) and smaller.
1507 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1512 switch (mouseProtocol
) {
1519 // Only report button presses
1520 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1526 // Only report button presses and releases
1527 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1528 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1536 * Only report button presses, button releases, and motions that
1537 * have a button down (i.e. drag-and-drop).
1539 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1540 if (!mouse
.isMouse1()
1541 && !mouse
.isMouse2()
1542 && !mouse
.isMouse3()
1543 && !mouse
.isMouseWheelUp()
1544 && !mouse
.isMouseWheelDown()
1552 // Report everything
1556 // Now encode the event
1557 StringBuilder sb
= new StringBuilder(6);
1558 if (mouseEncoding
== MouseEncoding
.SGR
) {
1559 sb
.append((char) 0x1B);
1562 if (mouse
.isMouse1()) {
1563 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1568 } else if (mouse
.isMouse2()) {
1569 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1574 } else if (mouse
.isMouse3()) {
1575 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1580 } else if (mouse
.isMouseWheelUp()) {
1582 } else if (mouse
.isMouseWheelDown()) {
1585 // This is motion with no buttons down.
1589 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1592 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1599 // X10 and UTF8 encodings
1600 sb
.append((char) 0x1B);
1603 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1604 sb
.append((char) (0x03 + 32));
1605 } else if (mouse
.isMouse1()) {
1606 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1607 sb
.append((char) (0x00 + 32 + 32));
1609 sb
.append((char) (0x00 + 32));
1611 } else if (mouse
.isMouse2()) {
1612 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1613 sb
.append((char) (0x01 + 32 + 32));
1615 sb
.append((char) (0x01 + 32));
1617 } else if (mouse
.isMouse3()) {
1618 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1619 sb
.append((char) (0x02 + 32 + 32));
1621 sb
.append((char) (0x02 + 32));
1623 } else if (mouse
.isMouseWheelUp()) {
1624 sb
.append((char) (0x04 + 64));
1625 } else if (mouse
.isMouseWheelDown()) {
1626 sb
.append((char) (0x05 + 64));
1628 // This is motion with no buttons down.
1629 sb
.append((char) (0x03 + 32));
1632 sb
.append((char) (mouse
.getX() + 33));
1633 sb
.append((char) (mouse
.getY() + 33));
1636 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1637 writeRemote(sb
.toString());
1641 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1642 * send to the remote side.
1644 * @param keypress keypress received from the local user
1646 public void keypress(final TKeypress keypress
) {
1647 writeRemote(keypressToString(keypress
));
1651 * Build one of the complex xterm keystroke sequences, storing the result in
1652 * xterm_keystroke_buffer.
1654 * @param ss3 the prefix to use based on VT100 state.
1655 * @param first the first character, usually a number.
1656 * @param first the last character, one of the following: ~ A B C D F H
1657 * @param ctrl whether or not ctrl is down
1658 * @param alt whether or not alt is down
1659 * @param shift whether or not shift is down
1660 * @return the buffer with the full key sequence
1662 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1663 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1665 StringBuilder sb
= new StringBuilder(ss3
);
1666 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1670 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1672 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1674 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1676 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1678 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1680 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1682 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1687 return sb
.toString();
1691 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1693 * @param keypress keypress received from the local user
1694 * @return string to transmit to the remote side
1696 @SuppressWarnings("fallthrough")
1697 private String
keypressToString(final TKeypress keypress
) {
1699 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1701 * If this is a control character, process it like it came from
1704 if (keypress
.getChar() < 0x20) {
1705 handleControlChar(keypress
.getChar());
1707 // Local echo for everything else
1708 printCharacter(keypress
.getChar());
1712 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1717 // Handle control characters
1718 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1719 StringBuilder sb
= new StringBuilder();
1720 char ch
= keypress
.getChar();
1723 return sb
.toString();
1726 // Handle alt characters
1727 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1728 StringBuilder sb
= new StringBuilder("\033");
1729 char ch
= keypress
.getChar();
1731 return sb
.toString();
1734 if (keypress
.equals(kbBackspaceDel
)) {
1747 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1750 switch (arrowKeyMode
) {
1752 return xtermBuildKeySequence("\033[", '1', 'D',
1753 keypress
.isCtrl(), keypress
.isAlt(),
1754 keypress
.isShift());
1756 return xtermBuildKeySequence("\033", '1', 'D',
1757 keypress
.isCtrl(), keypress
.isAlt(),
1758 keypress
.isShift());
1760 return xtermBuildKeySequence("\033O", '1', 'D',
1761 keypress
.isCtrl(), keypress
.isAlt(),
1762 keypress
.isShift());
1765 switch (arrowKeyMode
) {
1776 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1779 switch (arrowKeyMode
) {
1781 return xtermBuildKeySequence("\033[", '1', 'C',
1782 keypress
.isCtrl(), keypress
.isAlt(),
1783 keypress
.isShift());
1785 return xtermBuildKeySequence("\033", '1', 'C',
1786 keypress
.isCtrl(), keypress
.isAlt(),
1787 keypress
.isShift());
1789 return xtermBuildKeySequence("\033O", '1', 'C',
1790 keypress
.isCtrl(), keypress
.isAlt(),
1791 keypress
.isShift());
1794 switch (arrowKeyMode
) {
1805 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1808 switch (arrowKeyMode
) {
1810 return xtermBuildKeySequence("\033[", '1', 'A',
1811 keypress
.isCtrl(), keypress
.isAlt(),
1812 keypress
.isShift());
1814 return xtermBuildKeySequence("\033", '1', 'A',
1815 keypress
.isCtrl(), keypress
.isAlt(),
1816 keypress
.isShift());
1818 return xtermBuildKeySequence("\033O", '1', 'A',
1819 keypress
.isCtrl(), keypress
.isAlt(),
1820 keypress
.isShift());
1823 switch (arrowKeyMode
) {
1834 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1837 switch (arrowKeyMode
) {
1839 return xtermBuildKeySequence("\033[", '1', 'B',
1840 keypress
.isCtrl(), keypress
.isAlt(),
1841 keypress
.isShift());
1843 return xtermBuildKeySequence("\033", '1', 'B',
1844 keypress
.isCtrl(), keypress
.isAlt(),
1845 keypress
.isShift());
1847 return xtermBuildKeySequence("\033O", '1', 'B',
1848 keypress
.isCtrl(), keypress
.isAlt(),
1849 keypress
.isShift());
1852 switch (arrowKeyMode
) {
1863 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1866 switch (arrowKeyMode
) {
1868 return xtermBuildKeySequence("\033[", '1', 'H',
1869 keypress
.isCtrl(), keypress
.isAlt(),
1870 keypress
.isShift());
1872 return xtermBuildKeySequence("\033", '1', 'H',
1873 keypress
.isCtrl(), keypress
.isAlt(),
1874 keypress
.isShift());
1876 return xtermBuildKeySequence("\033O", '1', 'H',
1877 keypress
.isCtrl(), keypress
.isAlt(),
1878 keypress
.isShift());
1881 switch (arrowKeyMode
) {
1892 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1895 switch (arrowKeyMode
) {
1897 return xtermBuildKeySequence("\033[", '1', 'F',
1898 keypress
.isCtrl(), keypress
.isAlt(),
1899 keypress
.isShift());
1901 return xtermBuildKeySequence("\033", '1', 'F',
1902 keypress
.isCtrl(), keypress
.isAlt(),
1903 keypress
.isShift());
1905 return xtermBuildKeySequence("\033O", '1', 'F',
1906 keypress
.isCtrl(), keypress
.isAlt(),
1907 keypress
.isShift());
1910 switch (arrowKeyMode
) {
1921 if (keypress
.equals(kbF1
)) {
1929 if (keypress
.equals(kbF2
)) {
1937 if (keypress
.equals(kbF3
)) {
1945 if (keypress
.equals(kbF4
)) {
1953 if (keypress
.equals(kbF5
)) {
1966 if (keypress
.equals(kbF6
)) {
1979 if (keypress
.equals(kbF7
)) {
1992 if (keypress
.equals(kbF8
)) {
2005 if (keypress
.equals(kbF9
)) {
2018 if (keypress
.equals(kbF10
)) {
2031 if (keypress
.equals(kbF11
)) {
2035 if (keypress
.equals(kbF12
)) {
2039 if (keypress
.equals(kbShiftF1
)) {
2044 if (type
== DeviceType
.XTERM
) {
2050 if (keypress
.equals(kbShiftF2
)) {
2055 if (type
== DeviceType
.XTERM
) {
2061 if (keypress
.equals(kbShiftF3
)) {
2066 if (type
== DeviceType
.XTERM
) {
2072 if (keypress
.equals(kbShiftF4
)) {
2077 if (type
== DeviceType
.XTERM
) {
2083 if (keypress
.equals(kbShiftF5
)) {
2085 return "\033[15;2~";
2088 if (keypress
.equals(kbShiftF6
)) {
2090 return "\033[17;2~";
2093 if (keypress
.equals(kbShiftF7
)) {
2095 return "\033[18;2~";
2098 if (keypress
.equals(kbShiftF8
)) {
2100 return "\033[19;2~";
2103 if (keypress
.equals(kbShiftF9
)) {
2105 return "\033[20;2~";
2108 if (keypress
.equals(kbShiftF10
)) {
2110 return "\033[21;2~";
2113 if (keypress
.equals(kbShiftF11
)) {
2115 return "\033[23;2~";
2118 if (keypress
.equals(kbShiftF12
)) {
2120 return "\033[24;2~";
2123 if (keypress
.equals(kbCtrlF1
)) {
2128 if (type
== DeviceType
.XTERM
) {
2134 if (keypress
.equals(kbCtrlF2
)) {
2139 if (type
== DeviceType
.XTERM
) {
2145 if (keypress
.equals(kbCtrlF3
)) {
2150 if (type
== DeviceType
.XTERM
) {
2156 if (keypress
.equals(kbCtrlF4
)) {
2161 if (type
== DeviceType
.XTERM
) {
2167 if (keypress
.equals(kbCtrlF5
)) {
2169 return "\033[15;5~";
2172 if (keypress
.equals(kbCtrlF6
)) {
2174 return "\033[17;5~";
2177 if (keypress
.equals(kbCtrlF7
)) {
2179 return "\033[18;5~";
2182 if (keypress
.equals(kbCtrlF8
)) {
2184 return "\033[19;5~";
2187 if (keypress
.equals(kbCtrlF9
)) {
2189 return "\033[20;5~";
2192 if (keypress
.equals(kbCtrlF10
)) {
2194 return "\033[21;5~";
2197 if (keypress
.equals(kbCtrlF11
)) {
2199 return "\033[23;5~";
2202 if (keypress
.equals(kbCtrlF12
)) {
2204 return "\033[24;5~";
2207 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2210 return xtermBuildKeySequence("\033[", '5', '~',
2211 keypress
.isCtrl(), keypress
.isAlt(),
2212 keypress
.isShift());
2218 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2221 return xtermBuildKeySequence("\033[", '6', '~',
2222 keypress
.isCtrl(), keypress
.isAlt(),
2223 keypress
.isShift());
2229 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2232 return xtermBuildKeySequence("\033[", '2', '~',
2233 keypress
.isCtrl(), keypress
.isAlt(),
2234 keypress
.isShift());
2240 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2243 return xtermBuildKeySequence("\033[", '3', '~',
2244 keypress
.isCtrl(), keypress
.isAlt(),
2245 keypress
.isShift());
2247 // Delete sends real delete for VTxxx
2252 if (keypress
.equals(kbEnter
)) {
2256 if (keypress
.equals(kbEsc
)) {
2260 if (keypress
.equals(kbAltEsc
)) {
2264 if (keypress
.equals(kbTab
)) {
2268 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2269 (keypress
.equals(kbShiftTab
))
2279 // Non-alt, non-ctrl characters
2280 if (!keypress
.isFnKey()) {
2281 StringBuilder sb
= new StringBuilder();
2282 sb
.append(keypress
.getChar());
2283 return sb
.toString();
2289 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2292 * @param ch 8-bit character from the remote side
2293 * @param charsetGl character set defined for GL
2294 * @param charsetGr character set defined for GR
2295 * @return character to display on the screen
2297 private char mapCharacterCharset(final char ch
,
2298 final CharacterSet charsetGl
,
2299 final CharacterSet charsetGr
) {
2301 int lookupChar
= ch
;
2302 CharacterSet lookupCharset
= charsetGl
;
2305 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2306 lookupCharset
= charsetGr
;
2310 switch (lookupCharset
) {
2313 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2316 return DECCharacterSets
.UK
[lookupChar
];
2319 return DECCharacterSets
.US_ASCII
[lookupChar
];
2322 return DECCharacterSets
.NL
[lookupChar
];
2325 return DECCharacterSets
.FI
[lookupChar
];
2328 return DECCharacterSets
.FR
[lookupChar
];
2331 return DECCharacterSets
.FR_CA
[lookupChar
];
2334 return DECCharacterSets
.DE
[lookupChar
];
2337 return DECCharacterSets
.IT
[lookupChar
];
2340 return DECCharacterSets
.NO
[lookupChar
];
2343 return DECCharacterSets
.ES
[lookupChar
];
2346 return DECCharacterSets
.SV
[lookupChar
];
2349 return DECCharacterSets
.SWISS
[lookupChar
];
2351 case DEC_SUPPLEMENTAL
:
2352 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2355 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2358 return DECCharacterSets
.US_ASCII
[lookupChar
];
2361 return DECCharacterSets
.US_ASCII
[lookupChar
];
2364 throw new IllegalArgumentException("Invalid character set value: "
2370 * Map an 8-bit byte into a printable character.
2372 * @param ch either 8-bit or Unicode character from the remote side
2373 * @return character to display on the screen
2375 private char mapCharacter(final char ch
) {
2377 // Unicode character, just return it
2381 CharacterSet charsetGl
= currentState
.g0Charset
;
2382 CharacterSet charsetGr
= currentState
.grCharset
;
2384 if (vt52Mode
== true) {
2385 if (shiftOut
== true) {
2386 // Shifted out character, pull from VT52 graphics
2387 charsetGl
= currentState
.g1Charset
;
2388 charsetGr
= CharacterSet
.US
;
2391 charsetGl
= currentState
.g0Charset
;
2392 charsetGr
= CharacterSet
.US
;
2395 // Pull the character
2396 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2400 if (shiftOut
== true) {
2401 // Shifted out character, pull from G1
2402 charsetGl
= currentState
.g1Charset
;
2403 charsetGr
= currentState
.grCharset
;
2405 // Pull the character
2406 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2410 if (singleshift
== Singleshift
.SS2
) {
2412 singleshift
= Singleshift
.NONE
;
2414 // Shifted out character, pull from G2
2415 charsetGl
= currentState
.g2Charset
;
2416 charsetGr
= currentState
.grCharset
;
2420 if (singleshift
== Singleshift
.SS3
) {
2422 singleshift
= Singleshift
.NONE
;
2424 // Shifted out character, pull from G3
2425 charsetGl
= currentState
.g3Charset
;
2426 charsetGr
= currentState
.grCharset
;
2429 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2430 // Check for locking shift
2432 switch (currentState
.glLockshift
) {
2435 throw new IllegalArgumentException("programming bug");
2438 throw new IllegalArgumentException("programming bug");
2441 throw new IllegalArgumentException("programming bug");
2445 charsetGl
= currentState
.g2Charset
;
2450 charsetGl
= currentState
.g3Charset
;
2455 charsetGl
= currentState
.g0Charset
;
2459 switch (currentState
.grLockshift
) {
2462 throw new IllegalArgumentException("programming bug");
2465 throw new IllegalArgumentException("programming bug");
2469 charsetGr
= currentState
.g1Charset
;
2474 charsetGr
= currentState
.g2Charset
;
2479 charsetGr
= currentState
.g3Charset
;
2484 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2491 // Pull the character
2492 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2496 * Scroll the text within a scrolling region up n lines.
2498 * @param regionTop top row of the scrolling region
2499 * @param regionBottom bottom row of the scrolling region
2500 * @param n number of lines to scroll
2502 private void scrollingRegionScrollUp(final int regionTop
,
2503 final int regionBottom
, final int n
) {
2505 if (regionTop
>= regionBottom
) {
2509 // Sanity check: see if there will be any characters left after the
2511 if (regionBottom
+ 1 - regionTop
<= n
) {
2512 // There won't be anything left in the region, so just call
2513 // eraseScreen() and return.
2514 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2518 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2519 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2520 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2522 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2523 - remaining
, regionBottom
+ 1);
2524 display
= new ArrayList
<DisplayLine
>(displayTop
);
2525 display
.addAll(displayMiddle
);
2526 for (int i
= 0; i
< n
; i
++) {
2527 DisplayLine line
= new DisplayLine(currentState
.attr
);
2528 line
.setReverseColor(reverseVideo
);
2531 display
.addAll(displayBottom
);
2533 assert (display
.size() == height
);
2537 * Scroll the text within a scrolling region down n lines.
2539 * @param regionTop top row of the scrolling region
2540 * @param regionBottom bottom row of the scrolling region
2541 * @param n number of lines to scroll
2543 private void scrollingRegionScrollDown(final int regionTop
,
2544 final int regionBottom
, final int n
) {
2546 if (regionTop
>= regionBottom
) {
2550 // Sanity check: see if there will be any characters left after the
2552 if (regionBottom
+ 1 - regionTop
<= n
) {
2553 // There won't be anything left in the region, so just call
2554 // eraseScreen() and return.
2555 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2559 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2560 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2561 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2563 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2564 regionTop
+ remaining
);
2565 display
= new ArrayList
<DisplayLine
>(displayTop
);
2566 for (int i
= 0; i
< n
; i
++) {
2567 DisplayLine line
= new DisplayLine(currentState
.attr
);
2568 line
.setReverseColor(reverseVideo
);
2571 display
.addAll(displayMiddle
);
2572 display
.addAll(displayBottom
);
2574 assert (display
.size() == height
);
2578 * Process a control character.
2580 * @param ch 8-bit character from the remote side
2582 private void handleControlChar(final char ch
) {
2583 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2594 // Transmit the answerback message.
2605 cursorLeft(1, false);
2610 advanceToNextTabStop();
2636 currentState
.glLockshift
= LockshiftMode
.NONE
;
2642 currentState
.glLockshift
= LockshiftMode
.NONE
;
2667 singleshift
= Singleshift
.SS2
;
2672 singleshift
= Singleshift
.SS3
;
2682 * Advance the cursor to the next tab stop.
2684 private void advanceToNextTabStop() {
2685 if (tabStops
.size() == 0) {
2686 // Go to the rightmost column
2687 cursorRight(rightMargin
- currentState
.cursorX
, false);
2690 for (Integer stop
: tabStops
) {
2691 if (stop
> currentState
.cursorX
) {
2692 cursorRight(stop
- currentState
.cursorX
, false);
2697 * We got here, meaning there isn't a tab stop beyond the current
2698 * cursor position. Place the cursor of the right-most edge of the
2701 cursorRight(rightMargin
- currentState
.cursorX
, false);
2705 * Save a character into the collect buffer.
2707 * @param ch character to save
2709 private void collect(final char ch
) {
2710 collectBuffer
.append(ch
);
2714 * Save a byte into the CSI parameters buffer.
2716 * @param ch byte to save
2718 private void param(final byte ch
) {
2719 if (csiParams
.size() == 0) {
2720 csiParams
.add(Integer
.valueOf(0));
2722 Integer x
= csiParams
.get(csiParams
.size() - 1);
2723 if ((ch
>= '0') && (ch
<= '9')) {
2726 csiParams
.set(csiParams
.size() - 1, x
);
2729 if ((ch
== ';') && (csiParams
.size() < 16)) {
2730 csiParams
.add(Integer
.valueOf(0));
2735 * Get a CSI parameter value, with a default.
2737 * @param position parameter index. 0 is the first parameter.
2738 * @param defaultValue value to use if csiParams[position] doesn't exist
2739 * @return parameter value
2741 private int getCsiParam(final int position
, final int defaultValue
) {
2742 if (csiParams
.size() < position
+ 1) {
2743 return defaultValue
;
2745 return csiParams
.get(position
).intValue();
2749 * Get a CSI parameter value, clamped to within min/max.
2751 * @param position parameter index. 0 is the first parameter.
2752 * @param defaultValue value to use if csiParams[position] doesn't exist
2753 * @param minValue minimum value inclusive
2754 * @param maxValue maximum value inclusive
2755 * @return parameter value
2757 private int getCsiParam(final int position
, final int defaultValue
,
2758 final int minValue
, final int maxValue
) {
2760 assert (minValue
<= maxValue
);
2761 int value
= getCsiParam(position
, defaultValue
);
2762 if (value
< minValue
) {
2765 if (value
> maxValue
) {
2772 * Set or unset a toggle.
2774 * @param value true for set ('h'), false for reset ('l')
2776 private void setToggle(final boolean value
) {
2777 boolean decPrivateModeFlag
= false;
2778 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2779 if (collectBuffer
.charAt(i
) == '?') {
2780 decPrivateModeFlag
= true;
2785 for (Integer i
: csiParams
) {
2790 if (decPrivateModeFlag
== true) {
2792 if (value
== true) {
2793 // Use application arrow keys
2794 arrowKeyMode
= ArrowKeyMode
.VT100
;
2796 // Use ANSI arrow keys
2797 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2802 if (decPrivateModeFlag
== true) {
2803 if (value
== false) {
2807 arrowKeyMode
= ArrowKeyMode
.VT52
;
2810 * From the VT102 docs: "You use ANSI mode to select
2811 * most terminal features; the terminal uses the same
2812 * features when it switches to VT52 mode. You
2813 * cannot, however, change most of these features in
2816 * In other words, do not reset any other attributes
2817 * when switching between VT52 submode and ANSI.
2819 * HOWEVER, the real vt100 does switch the character
2820 * set according to Usenet.
2822 currentState
.g0Charset
= CharacterSet
.US
;
2823 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2826 if ((type
== DeviceType
.VT220
)
2827 || (type
== DeviceType
.XTERM
)) {
2829 // VT52 mode is explicitly 7-bit
2831 singleshift
= Singleshift
.NONE
;
2836 if (value
== true) {
2837 // Turn off keyboard
2846 if (decPrivateModeFlag
== true) {
2848 if (value
== true) {
2855 if ((displayListener
!= null)
2856 && (type
== DeviceType
.XTERM
)
2858 // For xterms, reset to the actual width, not 80
2860 width
= displayListener
.getDisplayWidth();
2861 rightMargin
= width
- 1;
2864 width
= rightMargin
+ 1;
2867 // Entire screen is cleared, and scrolling region is
2869 eraseScreen(0, 0, height
- 1, width
- 1, false);
2870 scrollRegionTop
= 0;
2871 scrollRegionBottom
= height
- 1;
2872 // Also home the cursor
2873 cursorPosition(0, 0);
2877 if (decPrivateModeFlag
== true) {
2879 if (value
== true) {
2888 if (value
== true) {
2896 if (decPrivateModeFlag
== true) {
2898 if (value
== true) {
2900 * Set selects reverse screen, a white screen
2901 * background with black characters.
2903 if (reverseVideo
!= true) {
2905 * If in normal video, switch it back
2907 invertDisplayColors();
2909 reverseVideo
= true;
2912 * Reset selects normal screen, a black screen
2913 * background with white characters.
2915 if (reverseVideo
== true) {
2917 * If in reverse video already, switch it back
2919 invertDisplayColors();
2921 reverseVideo
= false;
2926 if (decPrivateModeFlag
== true) {
2928 if (value
== true) {
2929 // Origin is relative to scroll region cursor.
2930 // Cursor can NEVER leave scrolling region.
2931 currentState
.originMode
= true;
2932 cursorPosition(0, 0);
2934 // Origin is absolute to entire screen. Cursor can
2935 // leave the scrolling region via cup() and hvp().
2936 currentState
.originMode
= false;
2937 cursorPosition(0, 0);
2942 if (decPrivateModeFlag
== true) {
2944 if (value
== true) {
2946 currentState
.lineWrap
= true;
2948 // Turn linewrap off
2949 currentState
.lineWrap
= false;
2954 if (decPrivateModeFlag
== true) {
2956 if (value
== true) {
2957 // Keyboard auto-repeat on
2960 // Keyboard auto-repeat off
2966 if (decPrivateModeFlag
== false) {
2968 if (value
== true) {
2978 if (decPrivateModeFlag
== true) {
2984 if (decPrivateModeFlag
== true) {
2990 if (decPrivateModeFlag
== false) {
2992 if (value
== true) {
2994 * Set causes a received linefeed, form feed, or
2995 * vertical tab to move cursor to first column of
2996 * next line. RETURN transmits both a carriage return
2997 * and linefeed. This selection is also called new
3003 * Reset causes a received linefeed, form feed, or
3004 * vertical tab to move cursor to next line in
3005 * current column. RETURN transmits a carriage
3008 newLineMode
= false;
3014 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3015 if (decPrivateModeFlag
== true) {
3017 if (value
== true) {
3019 cursorVisible
= true;
3022 cursorVisible
= false;
3029 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3030 if (decPrivateModeFlag
== true) {
3032 if (value
== true) {
3033 // Select national mode NRC
3036 // Select multi-national mode
3045 if ((type
== DeviceType
.XTERM
)
3046 && (decPrivateModeFlag
== true)
3048 // Mouse: normal tracking mode
3049 if (value
== true) {
3050 mouseProtocol
= MouseProtocol
.NORMAL
;
3052 mouseProtocol
= MouseProtocol
.OFF
;
3058 if ((type
== DeviceType
.XTERM
)
3059 && (decPrivateModeFlag
== true)
3061 // Mouse: normal tracking mode
3062 if (value
== true) {
3063 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
3065 mouseProtocol
= MouseProtocol
.OFF
;
3071 if ((type
== DeviceType
.XTERM
)
3072 && (decPrivateModeFlag
== true)
3074 // Mouse: Any-event tracking mode
3075 if (value
== true) {
3076 mouseProtocol
= MouseProtocol
.ANYEVENT
;
3078 mouseProtocol
= MouseProtocol
.OFF
;
3084 if ((type
== DeviceType
.XTERM
)
3085 && (decPrivateModeFlag
== true)
3087 // Mouse: UTF-8 coordinates
3088 if (value
== true) {
3089 mouseEncoding
= MouseEncoding
.UTF8
;
3091 mouseEncoding
= MouseEncoding
.X10
;
3097 if ((type
== DeviceType
.XTERM
)
3098 && (decPrivateModeFlag
== true)
3100 // Mouse: SGR coordinates
3101 if (value
== true) {
3102 mouseEncoding
= MouseEncoding
.SGR
;
3104 mouseEncoding
= MouseEncoding
.X10
;
3117 * DECSC - Save cursor.
3119 private void decsc() {
3120 savedState
.setTo(currentState
);
3124 * DECRC - Restore cursor.
3126 private void decrc() {
3127 currentState
.setTo(savedState
);
3133 private void ind() {
3134 // Move the cursor and scroll if necessary. If at the bottom line
3135 // already, a scroll up is supposed to be performed.
3136 if (currentState
.cursorY
== scrollRegionBottom
) {
3137 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3139 cursorDown(1, true);
3143 * RI - Reverse index.
3146 // Move the cursor and scroll if necessary. If at the top line
3147 // already, a scroll down is supposed to be performed.
3148 if (currentState
.cursorY
== scrollRegionTop
) {
3149 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3157 private void nel() {
3158 // Move the cursor and scroll if necessary. If at the bottom line
3159 // already, a scroll up is supposed to be performed.
3160 if (currentState
.cursorY
== scrollRegionBottom
) {
3161 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3163 cursorDown(1, true);
3165 // Reset to the beginning of the next line
3166 currentState
.cursorX
= 0;
3170 * DECKPAM - Keypad application mode.
3172 private void deckpam() {
3173 keypadMode
= KeypadMode
.Application
;
3177 * DECKPNM - Keypad numeric mode.
3179 private void deckpnm() {
3180 keypadMode
= KeypadMode
.Numeric
;
3186 * @param n number of spaces to move
3187 * @param honorScrollRegion if true, then do nothing if the cursor is
3188 * outside the scrolling region
3190 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3194 * Special case: if a user moves the cursor from the right margin, we
3195 * have to reset the VT100 right margin flag.
3198 wrapLineFlag
= false;
3201 for (int i
= 0; i
< n
; i
++) {
3202 if (honorScrollRegion
== true) {
3203 // Honor the scrolling region
3204 if ((currentState
.cursorY
< scrollRegionTop
)
3205 || (currentState
.cursorY
> scrollRegionBottom
)
3207 // Outside region, do nothing
3210 // Inside region, go up
3211 top
= scrollRegionTop
;
3213 // Non-scrolling case
3217 if (currentState
.cursorY
> top
) {
3218 currentState
.cursorY
--;
3224 * Move down n spaces.
3226 * @param n number of spaces to move
3227 * @param honorScrollRegion if true, then do nothing if the cursor is
3228 * outside the scrolling region
3230 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3234 * Special case: if a user moves the cursor from the right margin, we
3235 * have to reset the VT100 right margin flag.
3238 wrapLineFlag
= false;
3241 for (int i
= 0; i
< n
; i
++) {
3243 if (honorScrollRegion
== true) {
3244 // Honor the scrolling region
3245 if (currentState
.cursorY
> scrollRegionBottom
) {
3246 // Outside region, do nothing
3249 // Inside region, go down
3250 bottom
= scrollRegionBottom
;
3252 // Non-scrolling case
3253 bottom
= height
- 1;
3256 if (currentState
.cursorY
< bottom
) {
3257 currentState
.cursorY
++;
3263 * Move left n spaces.
3265 * @param n number of spaces to move
3266 * @param honorScrollRegion if true, then do nothing if the cursor is
3267 * outside the scrolling region
3269 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3271 * Special case: if a user moves the cursor from the right margin, we
3272 * have to reset the VT100 right margin flag.
3275 wrapLineFlag
= false;
3278 for (int i
= 0; i
< n
; i
++) {
3279 if (honorScrollRegion
== true) {
3280 // Honor the scrolling region
3281 if ((currentState
.cursorY
< scrollRegionTop
)
3282 || (currentState
.cursorY
> scrollRegionBottom
)
3284 // Outside region, do nothing
3289 if (currentState
.cursorX
> 0) {
3290 currentState
.cursorX
--;
3296 * Move right n spaces.
3298 * @param n number of spaces to move
3299 * @param honorScrollRegion if true, then do nothing if the cursor is
3300 * outside the scrolling region
3302 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3303 int rightMargin
= this.rightMargin
;
3306 * Special case: if a user moves the cursor from the right margin, we
3307 * have to reset the VT100 right margin flag.
3310 wrapLineFlag
= false;
3313 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3314 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3317 for (int i
= 0; i
< n
; i
++) {
3318 if (honorScrollRegion
== true) {
3319 // Honor the scrolling region
3320 if ((currentState
.cursorY
< scrollRegionTop
)
3321 || (currentState
.cursorY
> scrollRegionBottom
)
3323 // Outside region, do nothing
3328 if (currentState
.cursorX
< rightMargin
) {
3329 currentState
.cursorX
++;
3335 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3337 * @param row row to move to
3338 * @param col column to move to
3340 private void cursorPosition(int row
, final int col
) {
3341 int rightMargin
= this.rightMargin
;
3346 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3347 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3350 // Set column number
3351 currentState
.cursorX
= col
;
3353 // Sanity check, bring column back to margin.
3354 if (currentState
.cursorX
> rightMargin
) {
3355 currentState
.cursorX
= rightMargin
;
3359 if (currentState
.originMode
== true) {
3360 row
+= scrollRegionTop
;
3362 if (currentState
.cursorY
< row
) {
3363 cursorDown(row
- currentState
.cursorY
, false);
3364 } else if (currentState
.cursorY
> row
) {
3365 cursorUp(currentState
.cursorY
- row
, false);
3368 wrapLineFlag
= false;
3372 * HTS - Horizontal tabulation set.
3374 private void hts() {
3375 for (Integer stop
: tabStops
) {
3376 if (stop
== currentState
.cursorX
) {
3377 // Already have a tab stop here
3382 // Append a tab stop to the end of the array and resort them
3383 tabStops
.add(currentState
.cursorX
);
3384 Collections
.sort(tabStops
);
3388 * DECSWL - Single-width line.
3390 private void decswl() {
3391 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3392 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3396 * DECDWL - Double-width line.
3398 private void decdwl() {
3399 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3400 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3404 * DECHDL - Double-height + double-width line.
3406 * @param topHalf if true, this sets the row to be the top half row of a
3409 private void dechdl(final boolean topHalf
) {
3410 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3411 if (topHalf
== true) {
3412 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3414 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3419 * DECALN - Screen alignment display.
3421 private void decaln() {
3422 Cell newCell
= new Cell();
3423 newCell
.setChar('E');
3424 for (DisplayLine line
: display
) {
3425 for (int i
= 0; i
< line
.length(); i
++) {
3426 line
.replace(i
, newCell
);
3432 * DECSCL - Compatibility level.
3434 private void decscl() {
3435 int i
= getCsiParam(0, 0);
3436 int j
= getCsiParam(1, 0);
3440 currentState
.g0Charset
= CharacterSet
.US
;
3441 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3443 } else if (i
== 62) {
3445 if ((j
== 0) || (j
== 2)) {
3447 } else if (j
== 1) {
3454 * CUD - Cursor down.
3456 private void cud() {
3457 cursorDown(getCsiParam(0, 1, 1, height
), true);
3461 * CUF - Cursor forward.
3463 private void cuf() {
3464 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3468 * CUB - Cursor backward.
3470 private void cub() {
3471 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3477 private void cuu() {
3478 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3482 * CUP - Cursor position.
3484 private void cup() {
3485 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3486 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3490 * CNL - Cursor down and to column 1.
3492 private void cnl() {
3493 cursorDown(getCsiParam(0, 1, 1, height
), true);
3495 cursorLeft(currentState
.cursorX
, true);
3499 * CPL - Cursor up and to column 1.
3501 private void cpl() {
3502 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3504 cursorLeft(currentState
.cursorX
, true);
3508 * CHA - Cursor to column # in current row.
3510 private void cha() {
3511 cursorPosition(currentState
.cursorY
,
3512 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3516 * VPA - Cursor to row #, same column.
3518 private void vpa() {
3519 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3520 currentState
.cursorX
);
3524 * ED - Erase in display.
3527 boolean honorProtected
= false;
3528 boolean decPrivateModeFlag
= false;
3530 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3531 if (collectBuffer
.charAt(i
) == '?') {
3532 decPrivateModeFlag
= true;
3537 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3538 && (decPrivateModeFlag
== true)
3540 honorProtected
= true;
3543 int i
= getCsiParam(0, 0);
3546 // Erase from here to end of screen
3547 if (currentState
.cursorY
< height
- 1) {
3548 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3551 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3552 } else if (i
== 1) {
3553 // Erase from beginning of screen to here
3554 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3556 eraseLine(0, currentState
.cursorX
, honorProtected
);
3557 } else if (i
== 2) {
3558 // Erase entire screen
3559 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3564 * EL - Erase in line.
3567 boolean honorProtected
= false;
3568 boolean decPrivateModeFlag
= false;
3570 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3571 if (collectBuffer
.charAt(i
) == '?') {
3572 decPrivateModeFlag
= true;
3577 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3578 && (decPrivateModeFlag
== true)
3580 honorProtected
= true;
3583 int i
= getCsiParam(0, 0);
3586 // Erase from here to end of line
3587 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3588 } else if (i
== 1) {
3589 // Erase from beginning of line to here
3590 eraseLine(0, currentState
.cursorX
, honorProtected
);
3591 } else if (i
== 2) {
3592 // Erase entire line
3593 eraseLine(0, width
- 1, honorProtected
);
3598 * ECH - Erase # of characters in current row.
3600 private void ech() {
3601 int i
= getCsiParam(0, 1, 1, width
);
3603 // Erase from here to i characters
3604 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3611 int i
= getCsiParam(0, 1);
3613 if ((currentState
.cursorY
>= scrollRegionTop
)
3614 && (currentState
.cursorY
<= scrollRegionBottom
)
3617 // I can get the same effect with a scroll-down
3618 scrollingRegionScrollDown(currentState
.cursorY
,
3619 scrollRegionBottom
, i
);
3624 * DCH - Delete char.
3626 private void dch() {
3627 int n
= getCsiParam(0, 1);
3628 DisplayLine line
= display
.get(currentState
.cursorY
);
3629 Cell blank
= new Cell();
3630 for (int i
= 0; i
< n
; i
++) {
3631 line
.delete(currentState
.cursorX
, blank
);
3636 * ICH - Insert blank char at cursor.
3638 private void ich() {
3639 int n
= getCsiParam(0, 1);
3640 DisplayLine line
= display
.get(currentState
.cursorY
);
3641 Cell blank
= new Cell();
3642 for (int i
= 0; i
< n
; i
++) {
3643 line
.insert(currentState
.cursorX
, blank
);
3651 int i
= getCsiParam(0, 1);
3653 if ((currentState
.cursorY
>= scrollRegionTop
)
3654 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3656 // I can get the same effect with a scroll-down
3657 scrollingRegionScrollUp(currentState
.cursorY
,
3658 scrollRegionBottom
, i
);
3663 * HVP - Horizontal and vertical position.
3665 private void hvp() {
3670 * REP - Repeat character.
3672 private void rep() {
3673 int n
= getCsiParam(0, 1);
3674 for (int i
= 0; i
< n
; i
++) {
3675 printCharacter(repCh
);
3683 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3684 getCsiParam(0, 1, 1, height
));
3691 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3692 getCsiParam(0, 1, 1, height
));
3696 * CBT - Go back X tab stops.
3698 private void cbt() {
3699 int tabsToMove
= getCsiParam(0, 1);
3702 for (int i
= 0; i
< tabsToMove
; i
++) {
3703 int j
= currentState
.cursorX
;
3704 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3705 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3713 j
= tabStops
.get(tabI
);
3715 cursorPosition(currentState
.cursorY
, j
);
3720 * CHT - Advance X tab stops.
3722 private void cht() {
3723 int n
= getCsiParam(0, 1);
3724 for (int i
= 0; i
< n
; i
++) {
3725 advanceToNextTabStop();
3730 * SGR - Select graphics rendition.
3732 private void sgr() {
3734 if (csiParams
.size() == 0) {
3735 currentState
.attr
.reset();
3739 int sgrColorMode
= -1;
3740 boolean idx88Color
= false;
3741 boolean rgbColor
= false;
3745 for (Integer i
: csiParams
) {
3747 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
3749 assert (type
== DeviceType
.XTERM
);
3753 * Indexed color mode, we now have the index number.
3755 if (sgrColorMode
== 38) {
3756 currentState
.attr
.setForeColorRGB(get88Color(i
));
3758 assert (sgrColorMode
== 48);
3759 currentState
.attr
.setBackColorRGB(get88Color(i
));
3768 * RGB color mode, we are collecting tokens.
3772 } else if (rgbGreen
== -1) {
3773 rgbGreen
= i
& 0xFF;
3775 int rgb
= rgbRed
<< 16;
3776 rgb
|= rgbGreen
<< 8;
3779 // System.err.printf("RGB: %08x\n", rgb);
3781 if (sgrColorMode
== 38) {
3782 currentState
.attr
.setForeColorRGB(rgb
);
3784 assert (sgrColorMode
== 48);
3785 currentState
.attr
.setBackColorRGB(rgb
);
3806 * Indexed color mode.
3813 * This is neither indexed nor RGB color. Bail out.
3818 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
3824 currentState
.attr
.reset();
3829 currentState
.attr
.setBold(true);
3834 currentState
.attr
.setUnderline(true);
3839 currentState
.attr
.setBlink(true);
3844 currentState
.attr
.setReverse(true);
3851 if (type
== DeviceType
.XTERM
) {
3861 // Set black foreground
3862 currentState
.attr
.setForeColorRGB(get88Color(8));
3865 // Set red foreground
3866 currentState
.attr
.setForeColorRGB(get88Color(9));
3869 // Set green foreground
3870 currentState
.attr
.setForeColorRGB(get88Color(10));
3873 // Set yellow foreground
3874 currentState
.attr
.setForeColorRGB(get88Color(11));
3877 // Set blue foreground
3878 currentState
.attr
.setForeColorRGB(get88Color(12));
3881 // Set magenta foreground
3882 currentState
.attr
.setForeColorRGB(get88Color(13));
3885 // Set cyan foreground
3886 currentState
.attr
.setForeColorRGB(get88Color(14));
3889 // Set white foreground
3890 currentState
.attr
.setForeColorRGB(get88Color(15));
3894 // Set black background
3895 currentState
.attr
.setBackColorRGB(get88Color(8));
3898 // Set red background
3899 currentState
.attr
.setBackColorRGB(get88Color(9));
3902 // Set green background
3903 currentState
.attr
.setBackColorRGB(get88Color(10));
3906 // Set yellow background
3907 currentState
.attr
.setBackColorRGB(get88Color(11));
3910 // Set blue background
3911 currentState
.attr
.setBackColorRGB(get88Color(12));
3914 // Set magenta background
3915 currentState
.attr
.setBackColorRGB(get88Color(13));
3918 // Set cyan background
3919 currentState
.attr
.setBackColorRGB(get88Color(14));
3922 // Set white background
3923 currentState
.attr
.setBackColorRGB(get88Color(15));
3931 if ((type
== DeviceType
.VT220
)
3932 || (type
== DeviceType
.XTERM
)) {
3938 currentState
.attr
.setBold(false);
3943 currentState
.attr
.setUnderline(false);
3948 currentState
.attr
.setBlink(false);
3953 currentState
.attr
.setReverse(false);
3961 // A true VT100/102/220 does not support color, however everyone
3962 // is used to their terminal emulator supporting color so we will
3963 // unconditionally support color for all DeviceType's.
3968 // Set black foreground
3969 currentState
.attr
.setForeColor(Color
.BLACK
);
3970 currentState
.attr
.setForeColorRGB(-1);
3973 // Set red foreground
3974 currentState
.attr
.setForeColor(Color
.RED
);
3975 currentState
.attr
.setForeColorRGB(-1);
3978 // Set green foreground
3979 currentState
.attr
.setForeColor(Color
.GREEN
);
3980 currentState
.attr
.setForeColorRGB(-1);
3983 // Set yellow foreground
3984 currentState
.attr
.setForeColor(Color
.YELLOW
);
3985 currentState
.attr
.setForeColorRGB(-1);
3988 // Set blue foreground
3989 currentState
.attr
.setForeColor(Color
.BLUE
);
3990 currentState
.attr
.setForeColorRGB(-1);
3993 // Set magenta foreground
3994 currentState
.attr
.setForeColor(Color
.MAGENTA
);
3995 currentState
.attr
.setForeColorRGB(-1);
3998 // Set cyan foreground
3999 currentState
.attr
.setForeColor(Color
.CYAN
);
4000 currentState
.attr
.setForeColorRGB(-1);
4003 // Set white foreground
4004 currentState
.attr
.setForeColor(Color
.WHITE
);
4005 currentState
.attr
.setForeColorRGB(-1);
4008 if (type
== DeviceType
.XTERM
) {
4010 * Xterm supports T.416 / ISO-8613-3 codes to select
4011 * either an indexed color or an RGB value. (It also
4012 * permits these ISO-8613-3 SGR sequences to be separated
4013 * by colons rather than semicolons.)
4015 * We will support only the following:
4017 * 1. Indexed color mode (88- or 256-color modes).
4021 * These cover most of the use cases in the real world.
4023 * HOWEVER, note that this is an awful broken "standard",
4024 * with no way to do it "right". See
4025 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4026 * for a detailed discussion of the current state of RGB
4027 * in various terminals, the point of which is that none
4028 * of them really do the same thing despite all appearing
4032 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4033 * where it is assumed that supporting just the "indexed
4034 * mode" of these sequences (which could align easily
4035 * with existing SGR colors) is assumed to mean full
4036 * support of 24-bit RGB. So it is all or nothing.
4038 * Finally, these sequences break the assumptions of
4039 * standard ECMA-48 style parsers as pointed out at
4040 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4041 * Therefore in order to keep a clean display, we cannot
4042 * parse anything else in this sequence.
4047 // Underscore on, default foreground color
4048 currentState
.attr
.setUnderline(true);
4049 currentState
.attr
.setForeColor(Color
.WHITE
);
4053 // Underscore off, default foreground color
4054 currentState
.attr
.setUnderline(false);
4055 currentState
.attr
.setForeColor(Color
.WHITE
);
4056 currentState
.attr
.setForeColorRGB(-1);
4059 // Set black background
4060 currentState
.attr
.setBackColor(Color
.BLACK
);
4061 currentState
.attr
.setBackColorRGB(-1);
4064 // Set red background
4065 currentState
.attr
.setBackColor(Color
.RED
);
4066 currentState
.attr
.setBackColorRGB(-1);
4069 // Set green background
4070 currentState
.attr
.setBackColor(Color
.GREEN
);
4071 currentState
.attr
.setBackColorRGB(-1);
4074 // Set yellow background
4075 currentState
.attr
.setBackColor(Color
.YELLOW
);
4076 currentState
.attr
.setBackColorRGB(-1);
4079 // Set blue background
4080 currentState
.attr
.setBackColor(Color
.BLUE
);
4081 currentState
.attr
.setBackColorRGB(-1);
4084 // Set magenta background
4085 currentState
.attr
.setBackColor(Color
.MAGENTA
);
4086 currentState
.attr
.setBackColorRGB(-1);
4089 // Set cyan background
4090 currentState
.attr
.setBackColor(Color
.CYAN
);
4091 currentState
.attr
.setBackColorRGB(-1);
4094 // Set white background
4095 currentState
.attr
.setBackColor(Color
.WHITE
);
4096 currentState
.attr
.setBackColorRGB(-1);
4099 if (type
== DeviceType
.XTERM
) {
4101 * Xterm supports T.416 / ISO-8613-3 codes to select
4102 * either an indexed color or an RGB value. (It also
4103 * permits these ISO-8613-3 SGR sequences to be separated
4104 * by colons rather than semicolons.)
4106 * We will support only the following:
4108 * 1. Indexed color mode (88- or 256-color modes).
4112 * These cover most of the use cases in the real world.
4114 * HOWEVER, note that this is an awful broken "standard",
4115 * with no way to do it "right". See
4116 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4117 * for a detailed discussion of the current state of RGB
4118 * in various terminals, the point of which is that none
4119 * of them really do the same thing despite all appearing
4123 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4124 * where it is assumed that supporting just the "indexed
4125 * mode" of these sequences (which could align easily
4126 * with existing SGR colors) is assumed to mean full
4127 * support of 24-bit RGB. So it is all or nothing.
4129 * Finally, these sequences break the assumptions of
4130 * standard ECMA-48 style parsers as pointed out at
4131 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4132 * Therefore in order to keep a clean display, we cannot
4133 * parse anything else in this sequence.
4140 // Default background
4141 currentState
.attr
.setBackColor(Color
.BLACK
);
4142 currentState
.attr
.setBackColorRGB(-1);
4152 * DA - Device attributes.
4155 int extendedFlag
= 0;
4157 if (collectBuffer
.length() > 0) {
4158 String args
= collectBuffer
.substring(1);
4159 if (collectBuffer
.charAt(0) == '>') {
4161 if (collectBuffer
.length() >= 2) {
4162 i
= Integer
.parseInt(args
);
4164 } else if (collectBuffer
.charAt(0) == '=') {
4166 if (collectBuffer
.length() >= 2) {
4167 i
= Integer
.parseInt(args
);
4170 // Unknown code, bail out
4175 if ((i
!= 0) && (i
!= 1)) {
4179 if ((extendedFlag
== 0) && (i
== 0)) {
4180 // Send string directly to remote side
4181 writeRemote(deviceTypeResponse());
4185 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4187 if ((extendedFlag
== 1) && (i
== 0)) {
4189 * Request "What type of terminal are you, what is your
4190 * firmware version, and what hardware options do you have
4193 * Respond: "I am a VT220 (identification code of 1), my
4194 * firmware version is _____ (Pv), and I have _____ Po
4195 * options installed."
4201 if (s8c1t
== true) {
4202 writeRemote("\u009b>1;10;0c");
4204 writeRemote("\033[>1;10;0c");
4210 if ((extendedFlag
== 2) && (i
== 0)) {
4213 * Request "What is your unit ID?"
4215 * Respond: "I was manufactured at site 00 and have a unique ID
4219 writeRemote("\033P!|00010203\033\\");
4224 * DECSTBM - Set top and bottom margins.
4226 private void decstbm() {
4227 boolean decPrivateModeFlag
= false;
4229 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4230 if (collectBuffer
.charAt(i
) == '?') {
4231 decPrivateModeFlag
= true;
4235 if (decPrivateModeFlag
) {
4236 // This could be restore DEC private mode values.
4240 int top
= getCsiParam(0, 1, 1, height
) - 1;
4241 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4246 scrollRegionTop
= top
;
4247 scrollRegionBottom
= bottom
;
4250 cursorPosition(0, 0);
4255 * DECREQTPARM - Request terminal parameters.
4257 private void decreqtparm() {
4258 int i
= getCsiParam(0, 0);
4260 if ((i
!= 0) && (i
!= 1)) {
4267 * Request terminal parameters.
4271 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4272 * (CLoCk MULtiplier = 1, STP option flags = 0)
4276 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4279 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4281 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4287 * DECSCA - Select Character Attributes.
4289 private void decsca() {
4290 int i
= getCsiParam(0, 0);
4292 if ((i
== 0) || (i
== 2)) {
4294 currentState
.attr
.setProtect(false);
4298 currentState
.attr
.setProtect(true);
4303 * DECSTR - Soft Terminal Reset.
4305 private void decstr() {
4306 // Do exactly like RIS - Reset to initial state
4308 // Do I clear screen too? I think so...
4309 eraseScreen(0, 0, height
- 1, width
- 1, false);
4310 cursorPosition(0, 0);
4314 * DSR - Device status report.
4316 private void dsr() {
4317 boolean decPrivateModeFlag
= false;
4318 int row
= currentState
.cursorY
;
4320 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4321 if (collectBuffer
.charAt(i
) == '?') {
4322 decPrivateModeFlag
= true;
4327 int i
= getCsiParam(0, 0);
4332 // Request status report. Respond with "OK, no malfunction."
4334 // Send string directly to remote side
4335 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4338 writeRemote("\u009b0n");
4340 writeRemote("\033[0n");
4345 // Request cursor position. Respond with current position.
4346 if (currentState
.originMode
== true) {
4347 row
-= scrollRegionTop
;
4350 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4353 str
= String
.format("\u009b%d;%dR", row
+ 1,
4354 currentState
.cursorX
+ 1);
4356 str
= String
.format("\033[%d;%dR", row
+ 1,
4357 currentState
.cursorX
+ 1);
4360 // Send string directly to remote side
4365 if (decPrivateModeFlag
== true) {
4367 // Request printer status report. Respond with "Printer not
4370 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4371 && (s8c1t
== true)) {
4372 writeRemote("\u009b?13n");
4374 writeRemote("\033[?13n");
4380 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4381 && (decPrivateModeFlag
== true)
4384 // Request user-defined keys are locked or unlocked. Respond
4385 // with "User-defined keys are locked."
4387 if (s8c1t
== true) {
4388 writeRemote("\u009b?21n");
4390 writeRemote("\033[?21n");
4396 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4397 && (decPrivateModeFlag
== true)
4400 // Request keyboard language. Respond with "Keyboard
4401 // language is North American."
4403 if (s8c1t
== true) {
4404 writeRemote("\u009b?27;1n");
4406 writeRemote("\033[?27;1n");
4413 // Some other option, ignore
4419 * TBC - Tabulation clear.
4421 private void tbc() {
4422 int i
= getCsiParam(0, 0);
4424 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4425 for (Integer stop
: tabStops
) {
4426 if (stop
== currentState
.cursorX
) {
4431 tabStops
= newStops
;
4439 * Erase the characters in the current line from the start column to the
4440 * end column, inclusive.
4442 * @param start starting column to erase (between 0 and width - 1)
4443 * @param end ending column to erase (between 0 and width - 1)
4444 * @param honorProtected if true, do not erase characters with the
4445 * protected attribute set
4447 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4452 if (end
> width
- 1) {
4459 for (int i
= start
; i
<= end
; i
++) {
4460 DisplayLine line
= display
.get(currentState
.cursorY
);
4461 if ((!honorProtected
)
4462 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4469 * From the VT102 manual:
4471 * Erasing a character also erases any character
4472 * attribute of the character.
4478 * Erase with the current color a.k.a. back-color erase
4481 line
.setChar(i
, ' ');
4482 line
.setAttr(i
, currentState
.attr
);
4490 * Erase a rectangular section of the screen, inclusive. end column,
4493 * @param startRow starting row to erase (between 0 and height - 1)
4494 * @param startCol starting column to erase (between 0 and width - 1)
4495 * @param endRow ending row to erase (between 0 and height - 1)
4496 * @param endCol ending column to erase (between 0 and width - 1)
4497 * @param honorProtected if true, do not erase characters with the
4498 * protected attribute set
4500 private void eraseScreen(final int startRow
, final int startCol
,
4501 final int endRow
, final int endCol
, final boolean honorProtected
) {
4509 || (endRow
< startRow
)
4510 || (endCol
< startCol
)
4515 oldCursorY
= currentState
.cursorY
;
4516 for (int i
= startRow
; i
<= endRow
; i
++) {
4517 currentState
.cursorY
= i
;
4518 eraseLine(startCol
, endCol
, honorProtected
);
4520 // Erase display clears the double attributes
4521 display
.get(i
).setDoubleWidth(false);
4522 display
.get(i
).setDoubleHeight(0);
4524 currentState
.cursorY
= oldCursorY
;
4528 * VT220 printer functions. All of these are parsed, but won't do
4531 private void printerFunctions() {
4532 boolean decPrivateModeFlag
= false;
4533 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4534 if (collectBuffer
.charAt(i
) == '?') {
4535 decPrivateModeFlag
= true;
4540 int i
= getCsiParam(0, 0);
4545 if (decPrivateModeFlag
== false) {
4551 if (decPrivateModeFlag
== true) {
4552 // Print cursor line
4557 if (decPrivateModeFlag
== true) {
4558 // Auto print mode OFF
4560 // Printer controller OFF
4562 // Characters re-appear on the screen
4563 printerControllerMode
= false;
4568 if (decPrivateModeFlag
== true) {
4572 // Printer controller
4574 // Characters get sucked into oblivion
4575 printerControllerMode
= true;
4586 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4587 * of remote systems will send an XTerm title sequence even if TERM isn't
4590 * @param xtermChar the character received from the remote side
4592 private void oscPut(final char xtermChar
) {
4593 // System.err.println("oscPut: " + xtermChar);
4596 collectBuffer
.append(xtermChar
);
4599 if ((xtermChar
== 0x07)
4600 || (collectBuffer
.toString().endsWith("\033\\"))
4603 if (xtermChar
== 0x07) {
4604 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
4606 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4609 String
[] p
= args
.split(";");
4611 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4618 if (p
[0].equals("4")) {
4619 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
4620 // Set a color index value
4622 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
4623 } catch (NumberFormatException e
) {
4630 // Go to SCAN_GROUND state
4637 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
4638 * Jexer ECMA48Terminal to talk to ECMA48.
4640 * @param pmChar the character received from the remote side
4642 private void pmPut(final char pmChar
) {
4643 // System.err.println("pmPut: " + pmChar);
4646 collectBuffer
.append(pmChar
);
4649 if (collectBuffer
.toString().endsWith("\033\\")) {
4651 arg
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4653 // System.err.println("arg: '" + arg + "'");
4655 if (arg
.equals("hideMousePointer")) {
4656 hideMousePointer
= true;
4658 if (arg
.equals("showMousePointer")) {
4659 hideMousePointer
= false;
4662 // Go to SCAN_GROUND state
4669 * Perform xterm window operations.
4671 private void xtermWindowOps() {
4672 boolean xtermPrivateModeFlag
= false;
4674 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4675 if (collectBuffer
.charAt(i
) == '?') {
4676 xtermPrivateModeFlag
= true;
4681 int i
= getCsiParam(0, 0);
4683 if (!xtermPrivateModeFlag
) {
4685 // Report xterm window in pixels as CSI 4 ; height ; width t
4686 writeRemote(String
.format("\033[4;%d;%dt", textHeight
* height
,
4687 textWidth
* width
));
4693 * Run this input character through the ECMA48 state machine.
4695 * @param ch character from the remote side
4697 private void consume(char ch
) {
4700 // System.err.printf("%c STATE = %s\n", ch, scanState);
4702 // Special case for VT10x: 7-bit characters only
4703 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4704 ch
= (char)(ch
& 0x7F);
4707 // Special "anywhere" states
4709 // 18, 1A --> execute, then switch to SCAN_GROUND
4710 if ((ch
== 0x18) || (ch
== 0x1A)) {
4711 // CAN and SUB abort escape sequences
4716 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4720 if ((type
== DeviceType
.XTERM
)
4721 && ((scanState
== ScanState
.OSC_STRING
)
4722 || (scanState
== ScanState
.DCS_SIXEL
)
4723 || (scanState
== ScanState
.SOSPMAPC_STRING
))
4725 // Xterm can pass ESCAPE to its OSC sequence.
4726 // Xterm can pass ESCAPE to its DCS sequence.
4727 // Jexer can pass ESCAPE to its PM sequence.
4728 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
4729 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4730 && (scanState
!= ScanState
.DCS_IGNORE
)
4731 && (scanState
!= ScanState
.DCS_PARAM
)
4732 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4734 scanState
= ScanState
.ESCAPE
;
4739 // 0x9B == CSI 8-bit sequence
4741 scanState
= ScanState
.CSI_ENTRY
;
4745 // 0x9D goes to ScanState.OSC_STRING
4747 scanState
= ScanState
.OSC_STRING
;
4751 // 0x90 goes to DCS_ENTRY
4753 scanState
= ScanState
.DCS_ENTRY
;
4757 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4758 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4759 scanState
= ScanState
.SOSPMAPC_STRING
;
4763 // 0x7F (DEL) is always discarded
4768 switch (scanState
) {
4771 // 00-17, 19, 1C-1F --> execute
4772 // 80-8F, 91-9A, 9C --> execute
4773 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4774 handleControlChar(ch
);
4778 if (((ch
>= 0x20) && (ch
<= 0x7F))
4782 // VT220 printer --> trash bin
4783 if (((type
== DeviceType
.VT220
)
4784 || (type
== DeviceType
.XTERM
))
4785 && (printerControllerMode
== true)
4790 // Hang onto this character
4791 repCh
= mapCharacter(ch
);
4793 // Print this character
4794 printCharacter(repCh
);
4799 // 00-17, 19, 1C-1F --> execute
4801 handleControlChar(ch
);
4805 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4806 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4808 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4812 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4813 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4824 // DECSC - Save cursor
4825 // Note this code overlaps both ANSI and VT52 mode
4830 // DECRC - Restore cursor
4831 // Note this code overlaps both ANSI and VT52 mode
4840 if (vt52Mode
== true) {
4841 // DECANM - Enter ANSI mode
4843 arrowKeyMode
= ArrowKeyMode
.VT100
;
4846 * From the VT102 docs: "You use ANSI mode to select
4847 * most terminal features; the terminal uses the same
4848 * features when it switches to VT52 mode. You
4849 * cannot, however, change most of these features in
4852 * In other words, do not reset any other attributes
4853 * when switching between VT52 submode and ANSI.
4857 currentState
.g0Charset
= CharacterSet
.US
;
4858 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4860 singleshift
= Singleshift
.NONE
;
4861 currentState
.glLockshift
= LockshiftMode
.NONE
;
4862 currentState
.grLockshift
= LockshiftMode
.NONE
;
4866 // DECKPAM - Keypad application mode
4867 // Note this code overlaps both ANSI and VT52 mode
4871 // DECKPNM - Keypad numeric mode
4872 // Note this code overlaps both ANSI and VT52 mode
4879 if (vt52Mode
== true) {
4880 // Cursor up, and stop at the top without scrolling
4885 if (vt52Mode
== true) {
4886 // Cursor down, and stop at the bottom without scrolling
4887 cursorDown(1, false);
4891 if (vt52Mode
== true) {
4892 // Cursor right, and stop at the right without scrolling
4893 cursorRight(1, false);
4897 if (vt52Mode
== true) {
4898 // Cursor left, and stop at the left without scrolling
4899 cursorLeft(1, false);
4906 if (vt52Mode
== true) {
4914 if (vt52Mode
== true) {
4915 // G0 --> Special graphics
4916 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
4920 if (vt52Mode
== true) {
4922 currentState
.g0Charset
= CharacterSet
.US
;
4926 if (vt52Mode
== true) {
4928 cursorPosition(0, 0);
4930 // HTS - Horizontal tabulation set
4935 if (vt52Mode
== true) {
4936 // Reverse line feed. Same as RI.
4941 if (vt52Mode
== true) {
4942 // Erase to end of screen
4943 eraseLine(currentState
.cursorX
, width
- 1, false);
4944 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
4949 if (vt52Mode
== true) {
4950 // Erase to end of line
4951 eraseLine(currentState
.cursorX
, width
- 1, false);
4957 if (vt52Mode
== true) {
4960 // RI - Reverse index
4965 if (vt52Mode
== false) {
4967 singleshift
= Singleshift
.SS2
;
4971 if (vt52Mode
== false) {
4973 singleshift
= Singleshift
.SS3
;
4980 if ((ch
>= 0x51) && (ch
<= 0x57)) {
4996 if (vt52Mode
== true) {
4997 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
5005 if (vt52Mode
== true) {
5007 // Send string directly to remote side
5008 writeRemote("\033/Z");
5011 // Send string directly to remote side
5012 writeRemote(deviceTypeResponse());
5023 // VT52 cannot get to any of these other states
5024 if (vt52Mode
== true) {
5029 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
5036 // RIS - Reset to initial state
5038 // Do I clear screen too? I think so...
5039 eraseScreen(0, 0, height
- 1, width
- 1, false);
5040 cursorPosition(0, 0);
5054 if ((type
== DeviceType
.VT220
)
5055 || (type
== DeviceType
.XTERM
)) {
5057 // VT220 lockshift G2 into GL
5058 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
5063 if ((type
== DeviceType
.VT220
)
5064 || (type
== DeviceType
.XTERM
)) {
5066 // VT220 lockshift G3 into GL
5067 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
5085 if ((type
== DeviceType
.VT220
)
5086 || (type
== DeviceType
.XTERM
)) {
5088 // VT220 lockshift G3 into GR
5089 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
5094 if ((type
== DeviceType
.VT220
)
5095 || (type
== DeviceType
.XTERM
)) {
5097 // VT220 lockshift G2 into GR
5098 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
5104 if ((type
== DeviceType
.VT220
)
5105 || (type
== DeviceType
.XTERM
)) {
5107 // VT220 lockshift G1 into GR
5108 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
5118 // 0x5B goes to CSI_ENTRY
5120 scanState
= ScanState
.CSI_ENTRY
;
5123 // 0x5D goes to OSC_STRING
5125 scanState
= ScanState
.OSC_STRING
;
5128 // 0x50 goes to DCS_ENTRY
5130 scanState
= ScanState
.DCS_ENTRY
;
5133 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5134 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
5135 scanState
= ScanState
.SOSPMAPC_STRING
;
5140 case ESCAPE_INTERMEDIATE
:
5141 // 00-17, 19, 1C-1F --> execute
5143 handleControlChar(ch
);
5146 // 20-2F --> collect
5147 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5151 // 30-7E --> dispatch, then switch to GROUND
5152 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
5155 if ((collectBuffer
.length() == 1)
5156 && (collectBuffer
.charAt(0) == '(')) {
5157 // G0 --> Special graphics
5158 currentState
.g0Charset
= CharacterSet
.DRAWING
;
5160 if ((collectBuffer
.length() == 1)
5161 && (collectBuffer
.charAt(0) == ')')) {
5162 // G1 --> Special graphics
5163 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5165 if ((type
== DeviceType
.VT220
)
5166 || (type
== DeviceType
.XTERM
)) {
5168 if ((collectBuffer
.length() == 1)
5169 && (collectBuffer
.charAt(0) == '*')) {
5170 // G2 --> Special graphics
5171 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5173 if ((collectBuffer
.length() == 1)
5174 && (collectBuffer
.charAt(0) == '+')) {
5175 // G3 --> Special graphics
5176 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5181 if ((collectBuffer
.length() == 1)
5182 && (collectBuffer
.charAt(0) == '(')) {
5183 // G0 --> Alternate character ROM standard character set
5184 currentState
.g0Charset
= CharacterSet
.ROM
;
5186 if ((collectBuffer
.length() == 1)
5187 && (collectBuffer
.charAt(0) == ')')) {
5188 // G1 --> Alternate character ROM standard character set
5189 currentState
.g1Charset
= CharacterSet
.ROM
;
5193 if ((collectBuffer
.length() == 1)
5194 && (collectBuffer
.charAt(0) == '(')) {
5195 // G0 --> Alternate character ROM special graphics
5196 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5198 if ((collectBuffer
.length() == 1)
5199 && (collectBuffer
.charAt(0) == ')')) {
5200 // G1 --> Alternate character ROM special graphics
5201 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5205 if ((collectBuffer
.length() == 1)
5206 && (collectBuffer
.charAt(0) == '#')) {
5207 // DECDHL - Double-height line (top half)
5212 if ((collectBuffer
.length() == 1)
5213 && (collectBuffer
.charAt(0) == '#')) {
5214 // DECDHL - Double-height line (bottom half)
5217 if ((type
== DeviceType
.VT220
)
5218 || (type
== DeviceType
.XTERM
)) {
5220 if ((collectBuffer
.length() == 1)
5221 && (collectBuffer
.charAt(0) == '(')) {
5223 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5225 if ((collectBuffer
.length() == 1)
5226 && (collectBuffer
.charAt(0) == ')')) {
5228 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5230 if ((collectBuffer
.length() == 1)
5231 && (collectBuffer
.charAt(0) == '*')) {
5233 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5235 if ((collectBuffer
.length() == 1)
5236 && (collectBuffer
.charAt(0) == '+')) {
5238 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5243 if ((collectBuffer
.length() == 1)
5244 && (collectBuffer
.charAt(0) == '#')) {
5245 // DECSWL - Single-width line
5248 if ((type
== DeviceType
.VT220
)
5249 || (type
== DeviceType
.XTERM
)) {
5251 if ((collectBuffer
.length() == 1)
5252 && (collectBuffer
.charAt(0) == '(')) {
5254 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5256 if ((collectBuffer
.length() == 1)
5257 && (collectBuffer
.charAt(0) == ')')) {
5259 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5261 if ((collectBuffer
.length() == 1)
5262 && (collectBuffer
.charAt(0) == '*')) {
5264 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5266 if ((collectBuffer
.length() == 1)
5267 && (collectBuffer
.charAt(0) == '+')) {
5269 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5274 if ((collectBuffer
.length() == 1)
5275 && (collectBuffer
.charAt(0) == '#')) {
5276 // DECDWL - Double-width line
5279 if ((type
== DeviceType
.VT220
)
5280 || (type
== DeviceType
.XTERM
)) {
5282 if ((collectBuffer
.length() == 1)
5283 && (collectBuffer
.charAt(0) == '(')) {
5285 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5287 if ((collectBuffer
.length() == 1)
5288 && (collectBuffer
.charAt(0) == ')')) {
5290 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5292 if ((collectBuffer
.length() == 1)
5293 && (collectBuffer
.charAt(0) == '*')) {
5295 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5297 if ((collectBuffer
.length() == 1)
5298 && (collectBuffer
.charAt(0) == '+')) {
5300 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5305 if ((type
== DeviceType
.VT220
)
5306 || (type
== DeviceType
.XTERM
)) {
5308 if ((collectBuffer
.length() == 1)
5309 && (collectBuffer
.charAt(0) == '(')) {
5311 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5313 if ((collectBuffer
.length() == 1)
5314 && (collectBuffer
.charAt(0) == ')')) {
5316 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5318 if ((collectBuffer
.length() == 1)
5319 && (collectBuffer
.charAt(0) == '*')) {
5321 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5323 if ((collectBuffer
.length() == 1)
5324 && (collectBuffer
.charAt(0) == '+')) {
5326 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5331 if ((collectBuffer
.length() == 1)
5332 && (collectBuffer
.charAt(0) == '#')) {
5333 // DECALN - Screen alignment display
5342 if ((type
== DeviceType
.VT220
)
5343 || (type
== DeviceType
.XTERM
)) {
5345 if ((collectBuffer
.length() == 1)
5346 && (collectBuffer
.charAt(0) == '(')) {
5347 // G0 --> DEC_SUPPLEMENTAL
5348 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5350 if ((collectBuffer
.length() == 1)
5351 && (collectBuffer
.charAt(0) == ')')) {
5352 // G1 --> DEC_SUPPLEMENTAL
5353 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5355 if ((collectBuffer
.length() == 1)
5356 && (collectBuffer
.charAt(0) == '*')) {
5357 // G2 --> DEC_SUPPLEMENTAL
5358 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5360 if ((collectBuffer
.length() == 1)
5361 && (collectBuffer
.charAt(0) == '+')) {
5362 // G3 --> DEC_SUPPLEMENTAL
5363 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5368 if ((type
== DeviceType
.VT220
)
5369 || (type
== DeviceType
.XTERM
)) {
5371 if ((collectBuffer
.length() == 1)
5372 && (collectBuffer
.charAt(0) == '(')) {
5374 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5376 if ((collectBuffer
.length() == 1)
5377 && (collectBuffer
.charAt(0) == ')')) {
5379 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5381 if ((collectBuffer
.length() == 1)
5382 && (collectBuffer
.charAt(0) == '*')) {
5384 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5386 if ((collectBuffer
.length() == 1)
5387 && (collectBuffer
.charAt(0) == '+')) {
5389 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5398 if ((collectBuffer
.length() == 1)
5399 && (collectBuffer
.charAt(0) == '(')) {
5400 // G0 --> United Kingdom set
5401 currentState
.g0Charset
= CharacterSet
.UK
;
5403 if ((collectBuffer
.length() == 1)
5404 && (collectBuffer
.charAt(0) == ')')) {
5405 // G1 --> United Kingdom set
5406 currentState
.g1Charset
= CharacterSet
.UK
;
5408 if ((type
== DeviceType
.VT220
)
5409 || (type
== DeviceType
.XTERM
)) {
5411 if ((collectBuffer
.length() == 1)
5412 && (collectBuffer
.charAt(0) == '*')) {
5413 // G2 --> United Kingdom set
5414 currentState
.g2Charset
= CharacterSet
.UK
;
5416 if ((collectBuffer
.length() == 1)
5417 && (collectBuffer
.charAt(0) == '+')) {
5418 // G3 --> United Kingdom set
5419 currentState
.g3Charset
= CharacterSet
.UK
;
5424 if ((collectBuffer
.length() == 1)
5425 && (collectBuffer
.charAt(0) == '(')) {
5427 currentState
.g0Charset
= CharacterSet
.US
;
5429 if ((collectBuffer
.length() == 1)
5430 && (collectBuffer
.charAt(0) == ')')) {
5432 currentState
.g1Charset
= CharacterSet
.US
;
5434 if ((type
== DeviceType
.VT220
)
5435 || (type
== DeviceType
.XTERM
)) {
5437 if ((collectBuffer
.length() == 1)
5438 && (collectBuffer
.charAt(0) == '*')) {
5440 currentState
.g2Charset
= CharacterSet
.US
;
5442 if ((collectBuffer
.length() == 1)
5443 && (collectBuffer
.charAt(0) == '+')) {
5445 currentState
.g3Charset
= CharacterSet
.US
;
5450 if ((type
== DeviceType
.VT220
)
5451 || (type
== DeviceType
.XTERM
)) {
5453 if ((collectBuffer
.length() == 1)
5454 && (collectBuffer
.charAt(0) == '(')) {
5456 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5458 if ((collectBuffer
.length() == 1)
5459 && (collectBuffer
.charAt(0) == ')')) {
5461 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5463 if ((collectBuffer
.length() == 1)
5464 && (collectBuffer
.charAt(0) == '*')) {
5466 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5468 if ((collectBuffer
.length() == 1)
5469 && (collectBuffer
.charAt(0) == '+')) {
5471 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5478 if ((type
== DeviceType
.VT220
)
5479 || (type
== DeviceType
.XTERM
)) {
5481 if ((collectBuffer
.length() == 1)
5482 && (collectBuffer
.charAt(0) == '(')) {
5484 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5486 if ((collectBuffer
.length() == 1)
5487 && (collectBuffer
.charAt(0) == ')')) {
5489 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5491 if ((collectBuffer
.length() == 1)
5492 && (collectBuffer
.charAt(0) == '*')) {
5494 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5496 if ((collectBuffer
.length() == 1)
5497 && (collectBuffer
.charAt(0) == '+')) {
5499 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5504 if ((type
== DeviceType
.VT220
)
5505 || (type
== DeviceType
.XTERM
)) {
5507 if ((collectBuffer
.length() == 1)
5508 && (collectBuffer
.charAt(0) == ' ')) {
5515 if ((type
== DeviceType
.VT220
)
5516 || (type
== DeviceType
.XTERM
)) {
5518 if ((collectBuffer
.length() == 1)
5519 && (collectBuffer
.charAt(0) == ' ')) {
5526 if ((type
== DeviceType
.VT220
)
5527 || (type
== DeviceType
.XTERM
)) {
5529 if ((collectBuffer
.length() == 1)
5530 && (collectBuffer
.charAt(0) == '(')) {
5532 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5534 if ((collectBuffer
.length() == 1)
5535 && (collectBuffer
.charAt(0) == ')')) {
5537 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5539 if ((collectBuffer
.length() == 1)
5540 && (collectBuffer
.charAt(0) == '*')) {
5542 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5544 if ((collectBuffer
.length() == 1)
5545 && (collectBuffer
.charAt(0) == '+')) {
5547 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5555 if ((type
== DeviceType
.VT220
)
5556 || (type
== DeviceType
.XTERM
)) {
5558 if ((collectBuffer
.length() == 1)
5559 && (collectBuffer
.charAt(0) == '(')) {
5561 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5563 if ((collectBuffer
.length() == 1)
5564 && (collectBuffer
.charAt(0) == ')')) {
5566 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5568 if ((collectBuffer
.length() == 1)
5569 && (collectBuffer
.charAt(0) == '*')) {
5571 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5573 if ((collectBuffer
.length() == 1)
5574 && (collectBuffer
.charAt(0) == '+')) {
5576 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5587 if ((type
== DeviceType
.VT220
)
5588 || (type
== DeviceType
.XTERM
)) {
5590 if ((collectBuffer
.length() == 1)
5591 && (collectBuffer
.charAt(0) == '(')) {
5593 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5595 if ((collectBuffer
.length() == 1)
5596 && (collectBuffer
.charAt(0) == ')')) {
5598 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5600 if ((collectBuffer
.length() == 1)
5601 && (collectBuffer
.charAt(0) == '*')) {
5603 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5605 if ((collectBuffer
.length() == 1)
5606 && (collectBuffer
.charAt(0) == '+')) {
5608 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5613 if ((type
== DeviceType
.VT220
)
5614 || (type
== DeviceType
.XTERM
)) {
5616 if ((collectBuffer
.length() == 1)
5617 && (collectBuffer
.charAt(0) == '(')) {
5619 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5621 if ((collectBuffer
.length() == 1)
5622 && (collectBuffer
.charAt(0) == ')')) {
5624 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5626 if ((collectBuffer
.length() == 1)
5627 && (collectBuffer
.charAt(0) == '*')) {
5629 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5631 if ((collectBuffer
.length() == 1)
5632 && (collectBuffer
.charAt(0) == '+')) {
5634 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5646 if ((type
== DeviceType
.VT220
)
5647 || (type
== DeviceType
.XTERM
)) {
5649 if ((collectBuffer
.length() == 1)
5650 && (collectBuffer
.charAt(0) == '(')) {
5652 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5654 if ((collectBuffer
.length() == 1)
5655 && (collectBuffer
.charAt(0) == ')')) {
5657 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5659 if ((collectBuffer
.length() == 1)
5660 && (collectBuffer
.charAt(0) == '*')) {
5662 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5664 if ((collectBuffer
.length() == 1)
5665 && (collectBuffer
.charAt(0) == '+')) {
5667 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5672 if ((type
== DeviceType
.VT220
)
5673 || (type
== DeviceType
.XTERM
)) {
5675 if ((collectBuffer
.length() == 1)
5676 && (collectBuffer
.charAt(0) == '(')) {
5678 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5680 if ((collectBuffer
.length() == 1)
5681 && (collectBuffer
.charAt(0) == ')')) {
5683 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5685 if ((collectBuffer
.length() == 1)
5686 && (collectBuffer
.charAt(0) == '*')) {
5688 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5690 if ((collectBuffer
.length() == 1)
5691 && (collectBuffer
.charAt(0) == '+')) {
5693 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5740 // 0x9C goes to GROUND
5748 // 00-17, 19, 1C-1F --> execute
5750 handleControlChar(ch
);
5753 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5754 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5756 scanState
= ScanState
.CSI_INTERMEDIATE
;
5759 // 30-39, 3B --> param, then switch to CSI_PARAM
5760 if ((ch
>= '0') && (ch
<= '9')) {
5762 scanState
= ScanState
.CSI_PARAM
;
5766 scanState
= ScanState
.CSI_PARAM
;
5769 // 3C-3F --> collect, then switch to CSI_PARAM
5770 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5772 scanState
= ScanState
.CSI_PARAM
;
5775 // 40-7E --> dispatch, then switch to GROUND
5776 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5779 // ICH - Insert character
5787 // CUD - Cursor down
5791 // CUF - Cursor forward
5795 // CUB - Cursor backward
5799 // CNL - Cursor down and to column 1
5800 if (type
== DeviceType
.XTERM
) {
5805 // CPL - Cursor up and to column 1
5806 if (type
== DeviceType
.XTERM
) {
5811 // CHA - Cursor to column # in current row
5812 if (type
== DeviceType
.XTERM
) {
5817 // CUP - Cursor position
5821 // CHT - Cursor forward X tab stops (default 1)
5822 if (type
== DeviceType
.XTERM
) {
5827 // ED - Erase in display
5831 // EL - Erase in line
5846 // DCH - Delete character
5853 // Scroll up X lines (default 1)
5854 if (type
== DeviceType
.XTERM
) {
5859 // Scroll down X lines (default 1)
5860 if (type
== DeviceType
.XTERM
) {
5869 if ((type
== DeviceType
.VT220
)
5870 || (type
== DeviceType
.XTERM
)) {
5872 // ECH - Erase character
5879 // CBT - Cursor backward X tab stops (default 1)
5880 if (type
== DeviceType
.XTERM
) {
5891 // HPA - Cursor to column # in current row. Same as CHA
5892 if (type
== DeviceType
.XTERM
) {
5897 // HPR - Cursor right. Same as CUF
5898 if (type
== DeviceType
.XTERM
) {
5903 // REP - Repeat last char X times
5904 if (type
== DeviceType
.XTERM
) {
5909 // DA - Device attributes
5913 // VPA - Cursor to row, current column.
5914 if (type
== DeviceType
.XTERM
) {
5919 // VPR - Cursor down. Same as CUD
5920 if (type
== DeviceType
.XTERM
) {
5925 // HVP - Horizontal and vertical position
5929 // TBC - Tabulation clear
5933 // Sets an ANSI or DEC private toggle
5937 if ((type
== DeviceType
.VT220
)
5938 || (type
== DeviceType
.XTERM
)) {
5940 // Printer functions
5948 // Sets an ANSI or DEC private toggle
5952 // SGR - Select graphics rendition
5956 // DSR - Device status report
5963 // DECLL - Load leds
5967 // DECSTBM - Set top and bottom margins
5971 // Save cursor (ANSI.SYS)
5972 if (type
== DeviceType
.XTERM
) {
5973 savedState
.cursorX
= currentState
.cursorX
;
5974 savedState
.cursorY
= currentState
.cursorY
;
5978 if (type
== DeviceType
.XTERM
) {
5979 // Window operations
5984 // Restore cursor (ANSI.SYS)
5985 if (type
== DeviceType
.XTERM
) {
5986 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
5993 // DECREQTPARM - Request terminal parameters
6009 // 0x9C goes to GROUND
6014 // 0x3A goes to CSI_IGNORE
6016 scanState
= ScanState
.CSI_IGNORE
;
6021 // 00-17, 19, 1C-1F --> execute
6023 handleControlChar(ch
);
6026 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6027 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6029 scanState
= ScanState
.CSI_INTERMEDIATE
;
6032 // 30-39, 3B --> param
6033 if ((ch
>= '0') && (ch
<= '9')) {
6040 // 0x3A goes to CSI_IGNORE
6042 scanState
= ScanState
.CSI_IGNORE
;
6044 // 0x3C-3F goes to CSI_IGNORE
6045 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6046 scanState
= ScanState
.CSI_IGNORE
;
6049 // 40-7E --> dispatch, then switch to GROUND
6050 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6053 // ICH - Insert character
6061 // CUD - Cursor down
6065 // CUF - Cursor forward
6069 // CUB - Cursor backward
6073 // CNL - Cursor down and to column 1
6074 if (type
== DeviceType
.XTERM
) {
6079 // CPL - Cursor up and to column 1
6080 if (type
== DeviceType
.XTERM
) {
6085 // CHA - Cursor to column # in current row
6086 if (type
== DeviceType
.XTERM
) {
6091 // CUP - Cursor position
6095 // CHT - Cursor forward X tab stops (default 1)
6096 if (type
== DeviceType
.XTERM
) {
6101 // ED - Erase in display
6105 // EL - Erase in line
6120 // DCH - Delete character
6127 // Scroll up X lines (default 1)
6128 if (type
== DeviceType
.XTERM
) {
6133 // Scroll down X lines (default 1)
6134 if (type
== DeviceType
.XTERM
) {
6143 if ((type
== DeviceType
.VT220
)
6144 || (type
== DeviceType
.XTERM
)) {
6146 // ECH - Erase character
6153 // CBT - Cursor backward X tab stops (default 1)
6154 if (type
== DeviceType
.XTERM
) {
6165 // HPA - Cursor to column # in current row. Same as CHA
6166 if (type
== DeviceType
.XTERM
) {
6171 // HPR - Cursor right. Same as CUF
6172 if (type
== DeviceType
.XTERM
) {
6177 // REP - Repeat last char X times
6178 if (type
== DeviceType
.XTERM
) {
6183 // DA - Device attributes
6187 // VPA - Cursor to row, current column.
6188 if (type
== DeviceType
.XTERM
) {
6193 // VPR - Cursor down. Same as CUD
6194 if (type
== DeviceType
.XTERM
) {
6199 // HVP - Horizontal and vertical position
6203 // TBC - Tabulation clear
6207 // Sets an ANSI or DEC private toggle
6211 if ((type
== DeviceType
.VT220
)
6212 || (type
== DeviceType
.XTERM
)) {
6214 // Printer functions
6222 // Sets an ANSI or DEC private toggle
6226 // SGR - Select graphics rendition
6230 // DSR - Device status report
6237 // DECLL - Load leds
6241 // DECSTBM - Set top and bottom margins
6247 if (type
== DeviceType
.XTERM
) {
6248 // Window operations
6257 // DECREQTPARM - Request terminal parameters
6274 case CSI_INTERMEDIATE
:
6275 // 00-17, 19, 1C-1F --> execute
6277 handleControlChar(ch
);
6280 // 20-2F --> collect
6281 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6285 // 0x30-3F goes to CSI_IGNORE
6286 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6287 scanState
= ScanState
.CSI_IGNORE
;
6290 // 40-7E --> dispatch, then switch to GROUND
6291 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6343 if (((type
== DeviceType
.VT220
)
6344 || (type
== DeviceType
.XTERM
))
6345 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6347 // DECSCL - compatibility level
6350 if ((type
== DeviceType
.XTERM
)
6351 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6353 // DECSTR - Soft terminal reset
6358 if (((type
== DeviceType
.VT220
)
6359 || (type
== DeviceType
.XTERM
))
6360 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6388 // 00-17, 19, 1C-1F --> execute
6390 handleControlChar(ch
);
6393 // 20-2F --> collect
6394 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6398 // 40-7E --> ignore, then switch to GROUND
6399 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6403 // 20-3F, 7F --> ignore
6409 // 0x9C goes to GROUND
6414 // 0x1B 0x5C goes to GROUND
6419 if ((collectBuffer
.length() > 0)
6420 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6426 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6427 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6429 scanState
= ScanState
.DCS_INTERMEDIATE
;
6432 // 30-39, 3B --> param, then switch to DCS_PARAM
6433 if ((ch
>= '0') && (ch
<= '9')) {
6435 scanState
= ScanState
.DCS_PARAM
;
6439 scanState
= ScanState
.DCS_PARAM
;
6442 // 3C-3F --> collect, then switch to DCS_PARAM
6443 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6445 scanState
= ScanState
.DCS_PARAM
;
6448 // 00-17, 19, 1C-1F, 7F --> ignore
6450 // 0x3A goes to DCS_IGNORE
6452 scanState
= ScanState
.DCS_IGNORE
;
6455 // 0x71 goes to DCS_SIXEL
6457 sixelParseBuffer
= new StringBuilder();
6458 scanState
= ScanState
.DCS_SIXEL
;
6459 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6460 // 0x40-7E goes to DCS_PASSTHROUGH
6461 scanState
= ScanState
.DCS_PASSTHROUGH
;
6465 case DCS_INTERMEDIATE
:
6467 // 0x9C goes to GROUND
6472 // 0x1B 0x5C goes to GROUND
6477 if ((collectBuffer
.length() > 0)
6478 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6484 // 0x30-3F goes to DCS_IGNORE
6485 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6486 scanState
= ScanState
.DCS_IGNORE
;
6489 // 0x40-7E goes to DCS_PASSTHROUGH
6490 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6491 scanState
= ScanState
.DCS_PASSTHROUGH
;
6494 // 00-17, 19, 1C-1F, 7F --> ignore
6499 // 0x9C goes to GROUND
6504 // 0x1B 0x5C goes to GROUND
6509 if ((collectBuffer
.length() > 0)
6510 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6516 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6517 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6519 scanState
= ScanState
.DCS_INTERMEDIATE
;
6522 // 30-39, 3B --> param
6523 if ((ch
>= '0') && (ch
<= '9')) {
6530 // 00-17, 19, 1C-1F, 7F --> ignore
6532 // 0x3A, 3C-3F goes to DCS_IGNORE
6534 scanState
= ScanState
.DCS_IGNORE
;
6536 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6537 scanState
= ScanState
.DCS_IGNORE
;
6540 // 0x71 goes to DCS_SIXEL
6542 sixelParseBuffer
= new StringBuilder();
6543 scanState
= ScanState
.DCS_SIXEL
;
6544 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6545 // 0x40-7E goes to DCS_PASSTHROUGH
6546 scanState
= ScanState
.DCS_PASSTHROUGH
;
6550 case DCS_PASSTHROUGH
:
6551 // 0x9C goes to GROUND
6556 // 0x1B 0x5C goes to GROUND
6561 if ((collectBuffer
.length() > 0)
6562 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6568 // 00-17, 19, 1C-1F, 20-7E --> put
6576 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6579 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6588 // 00-17, 19, 1C-1F, 20-7F --> ignore
6590 // 0x9C goes to GROUND
6598 // 0x9C goes to GROUND
6604 // 0x1B 0x5C goes to GROUND
6609 if ((collectBuffer
.length() > 0)
6610 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6617 // 00-17, 19, 1C-1F, 20-7E --> put
6619 sixelParseBuffer
.append(ch
);
6623 sixelParseBuffer
.append(ch
);
6626 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6627 sixelParseBuffer
.append(ch
);
6630 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6631 sixelParseBuffer
.append(ch
);
6639 case SOSPMAPC_STRING
:
6640 // 00-17, 19, 1C-1F, 20-7F --> ignore
6642 // Special case for Jexer: PM can pass one control character
6647 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6651 // 0x9C goes to GROUND
6659 // Special case for Xterm: OSC can pass control characters
6660 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
6664 // 00-17, 19, 1C-1F --> ignore
6666 // 20-7F --> osc_put
6667 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6671 // 0x9C goes to GROUND
6678 case VT52_DIRECT_CURSOR_ADDRESS
:
6679 // This is a special case for the VT52 sequence "ESC Y l c"
6680 if (collectBuffer
.length() == 0) {
6682 } else if (collectBuffer
.length() == 1) {
6683 // We've got the two characters, one in the buffer and the
6685 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
6694 * Expose current cursor X to outside world.
6696 * @return current cursor X
6698 public final int getCursorX() {
6699 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6700 return currentState
.cursorX
* 2;
6702 return currentState
.cursorX
;
6706 * Expose current cursor Y to outside world.
6708 * @return current cursor Y
6710 public final int getCursorY() {
6711 return currentState
.cursorY
;
6715 * Returns true if this terminal has requested the mouse pointer be
6718 * @return true if this terminal has requested the mouse pointer be
6721 public final boolean hasHiddenMousePointer() {
6722 return hideMousePointer
;
6726 * Get the mouse protocol.
6728 * @return MouseProtocol.OFF, MouseProtocol.X10, etc.
6730 public MouseProtocol
getMouseProtocol() {
6731 return mouseProtocol
;
6734 // ------------------------------------------------------------------------
6735 // Sixel support ----------------------------------------------------------
6736 // ------------------------------------------------------------------------
6739 * Set the width of a character cell in pixels.
6741 * @param textWidth the width in pixels of a character cell
6743 public void setTextWidth(final int textWidth
) {
6744 this.textWidth
= textWidth
;
6748 * Set the height of a character cell in pixels.
6750 * @param textHeight the height in pixels of a character cell
6752 public void setTextHeight(final int textHeight
) {
6753 this.textHeight
= textHeight
;
6757 * Parse a sixel string into a bitmap image, and overlay that image onto
6760 private void parseSixel() {
6763 System.err.println("parseSixel(): '" + sixelParseBuffer.toString()
6767 Sixel sixel
= new Sixel(sixelParseBuffer
.toString());
6768 BufferedImage image
= sixel
.getImage();
6770 // System.err.println("parseSixel(): image " + image);
6772 if (image
== null) {
6773 // Sixel data was malformed in some way, bail out.
6780 * Break up the image into text cell sized pieces as a new array of
6783 * Note original column position x0.
6787 * 1. Advance (printCharacter(' ')) for horizontal increment, or
6788 * index (linefeed() + cursorPosition(y, x0)) for vertical
6791 * 2. Set (x, y) cell image data.
6793 * 3. For the right and bottom edges:
6795 * a. Render the text to pixels using Terminus font.
6797 * b. Blit the image on top of the text, using alpha channel.
6799 int cellColumns
= image
.getWidth() / textWidth
;
6800 if (cellColumns
* textWidth
< image
.getWidth()) {
6803 int cellRows
= image
.getHeight() / textHeight
;
6804 if (cellRows
* textHeight
< image
.getHeight()) {
6808 // Break the image up into an array of cells.
6809 Cell
[][] cells
= new Cell
[cellColumns
][cellRows
];
6811 for (int x
= 0; x
< cellColumns
; x
++) {
6812 for (int y
= 0; y
< cellRows
; y
++) {
6814 int width
= textWidth
;
6815 if ((x
+ 1) * textWidth
> image
.getWidth()) {
6816 width
= image
.getWidth() - (x
* textWidth
);
6818 int height
= textHeight
;
6819 if ((y
+ 1) * textHeight
> image
.getHeight()) {
6820 height
= image
.getHeight() - (y
* textHeight
);
6823 Cell cell
= new Cell();
6824 cell
.setImage(image
.getSubimage(x
* textWidth
,
6825 y
* textHeight
, width
, height
));
6831 int x0
= currentState
.cursorX
;
6832 for (int y
= 0; y
< cellRows
; y
++) {
6833 for (int x
= 0; x
< cellColumns
; x
++) {
6834 printCharacter(' ');
6835 cursorLeft(1, false);
6836 if ((x
== cellColumns
- 1) || (y
== cellRows
- 1)) {
6837 // TODO: render text of current cell first, then image
6838 // over it. For now, just copy the cell.
6839 DisplayLine line
= display
.get(currentState
.cursorY
);
6840 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
6842 // Copy the image cell into the display.
6843 DisplayLine line
= display
.get(currentState
.cursorY
);
6844 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
6846 cursorRight(1, false);
6849 cursorPosition(currentState
.cursorY
, x0
);
6855 * Draw the left and right cells of a two-cell-wide (full-width) glyph.
6857 * @param leftX the x position to draw the left half to
6858 * @param leftY the y position to draw the left half to
6859 * @param rightX the x position to draw the right half to
6860 * @param rightY the y position to draw the right half to
6861 * @param ch the character to draw
6863 private void drawHalves(final int leftX
, final int leftY
,
6864 final int rightX
, final int rightY
, final char ch
) {
6866 // System.err.println("drawHalves(): " + Integer.toHexString(ch));
6868 if (lastTextHeight
!= textHeight
) {
6869 glyphMaker
= GlyphMaker
.getInstance(textHeight
);
6870 lastTextHeight
= textHeight
;
6873 Cell cell
= new Cell(ch
);
6874 cell
.setAttr(currentState
.attr
);
6875 BufferedImage image
= glyphMaker
.getImage(cell
, textWidth
* 2,
6877 BufferedImage leftImage
= image
.getSubimage(0, 0, textWidth
,
6879 BufferedImage rightImage
= image
.getSubimage(textWidth
, 0, textWidth
,
6882 Cell left
= new Cell();
6884 left
.setImage(leftImage
);
6885 left
.setWidth(Cell
.Width
.LEFT
);
6886 display
.get(leftY
).replace(leftX
, left
);
6888 Cell right
= new Cell();
6890 right
.setImage(rightImage
);
6891 right
.setWidth(Cell
.Width
.RIGHT
);
6892 display
.get(rightY
).replace(rightX
, right
);