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
.BufferedInputStream
;
34 import java
.io
.BufferedOutputStream
;
35 import java
.io
.ByteArrayInputStream
;
36 import java
.io
.CharArrayWriter
;
37 import java
.io
.InputStream
;
38 import java
.io
.InputStreamReader
;
39 import java
.io
.IOException
;
40 import java
.io
.OutputStream
;
41 import java
.io
.OutputStreamWriter
;
42 import java
.io
.PrintWriter
;
43 import java
.io
.Reader
;
44 import java
.io
.UnsupportedEncodingException
;
45 import java
.io
.Writer
;
46 import java
.util
.ArrayList
;
47 import java
.util
.Collections
;
48 import java
.util
.HashMap
;
49 import java
.util
.List
;
50 import javax
.imageio
.ImageIO
;
52 import jexer
.TKeypress
;
53 import jexer
.backend
.GlyphMaker
;
54 import jexer
.bits
.Color
;
55 import jexer
.bits
.Cell
;
56 import jexer
.bits
.CellAttributes
;
57 import jexer
.bits
.StringUtils
;
58 import jexer
.event
.TInputEvent
;
59 import jexer
.event
.TKeypressEvent
;
60 import jexer
.event
.TMouseEvent
;
61 import jexer
.io
.ReadTimeoutException
;
62 import jexer
.io
.TimeoutInputStream
;
63 import static jexer
.TKeypress
.*;
66 * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
67 * including a scrollback buffer.
70 * It currently implements VT100, VT102, VT220, and XTERM with the following
74 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
75 * failure with XTERM. This is due to vttest failing to decode the UTF-8
79 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
80 * from VT100 are not supported.
83 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
84 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
85 * because DECSCL is not supported, it will fail the last part of the
86 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
89 * - Numeric/application keys from the number pad are not supported because
90 * they are not exposed from the TKeypress API.
93 * - VT52 HOLD SCREEN mode is not supported.
96 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
97 * numerators) are not rendered correctly.
100 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
102 public class ECMA48
implements Runnable
{
104 // ------------------------------------------------------------------------
105 // Constants --------------------------------------------------------------
106 // ------------------------------------------------------------------------
109 * The emulator can emulate several kinds of terminals.
111 public enum DeviceType
{
113 * DEC VT100 but also including the three VT102 functions.
134 * Parser character scan states.
136 private enum ScanState
{
152 VT52_DIRECT_CURSOR_ADDRESS
156 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
157 * can't really use it in keypress() because we do not see number pad
158 * events from TKeypress.
160 private enum KeypadMode
{
166 * Arrow keys can emit three different sequences (DECCKM or VT52
169 private enum ArrowKeyMode
{
176 * Available character sets for GL, GR, G0, G1, G2, G3.
178 private enum CharacterSet
{
199 * Single-shift states used by the C1 control characters SS2 (0x8E) and
202 private enum Singleshift
{
209 * VT220+ lockshift states.
211 private enum LockshiftMode
{
221 * XTERM mouse reporting protocols.
223 public enum MouseProtocol
{
232 * XTERM mouse reporting encodings.
234 private enum MouseEncoding
{
240 // ------------------------------------------------------------------------
241 // Variables --------------------------------------------------------------
242 // ------------------------------------------------------------------------
245 * The enclosing listening object.
247 private DisplayListener displayListener
;
250 * When true, the reader thread is expected to exit.
252 private volatile boolean stopReaderThread
= false;
257 private Thread readerThread
= null;
260 * The type of emulator to be.
262 private final DeviceType type
;
265 * The scrollback buffer characters + attributes.
267 private volatile ArrayList
<DisplayLine
> scrollback
;
270 * The raw display buffer characters + attributes.
272 private volatile ArrayList
<DisplayLine
> display
;
275 * The maximum number of lines in the scrollback buffer.
277 private int maxScrollback
= 10000;
280 * The terminal's input. For type == XTERM, this is an InputStreamReader
281 * with UTF-8 encoding.
283 private Reader input
;
286 * The terminal's raw InputStream. This is used for type != XTERM.
288 private volatile TimeoutInputStream inputStream
;
291 * The terminal's output. For type == XTERM, this wraps an
292 * OutputStreamWriter with UTF-8 encoding.
294 private Writer output
;
297 * The terminal's raw OutputStream. This is used for type != XTERM.
299 private OutputStream outputStream
;
302 * Current scanning state.
304 private ScanState scanState
;
307 * Which mouse protocol is active.
309 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
312 * Which mouse encoding is active.
314 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
317 * A terminal may request that the mouse pointer be hidden using a
318 * Privacy Message containing either "hideMousePointer" or
319 * "showMousePointer". This is currently only used within Jexer by
320 * TTerminalWindow so that only the bottom-most instance of nested
321 * Jexer's draws the mouse within its application window.
323 private boolean hideMousePointer
= false;
326 * Physical display width. We start at 80x24, but the user can resize us
332 * Physical display height. We start at 80x24, but the user can resize
338 * Top margin of the scrolling region.
340 private int scrollRegionTop
;
343 * Bottom margin of the scrolling region.
345 private int scrollRegionBottom
;
348 * Right margin column number. This can be selected by the remote side
349 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
351 private int rightMargin
;
354 * Last character printed.
359 * VT100-style line wrapping: a character is placed in column 80 (or
360 * 132), but the line does NOT wrap until another character is written to
361 * column 1 of the next line, after which the cursor moves to column 2.
363 private boolean wrapLineFlag
;
366 * VT220 single shift flag.
368 private Singleshift singleshift
= Singleshift
.NONE
;
371 * true = insert characters, false = overwrite.
373 private boolean insertMode
= false;
376 * VT52 mode as selected by DECANM. True means VT52, false means
377 * ANSI. Default is ANSI.
379 private boolean vt52Mode
= false;
382 * Visible cursor (DECTCEM).
384 private boolean cursorVisible
= true;
387 * Screen title as set by the xterm OSC sequence. Lots of applications
388 * send a screenTitle regardless of whether it is an xterm client or not.
390 private String screenTitle
= "";
393 * Parameter characters being collected.
395 private List
<Integer
> csiParams
;
398 * Non-csi collect buffer.
400 private StringBuilder collectBuffer
= new StringBuilder(128);
403 * When true, use the G1 character set.
405 private boolean shiftOut
= false;
408 * Horizontal tab stop locations.
410 private List
<Integer
> tabStops
;
413 * S8C1T. True means 8bit controls, false means 7bit controls.
415 private boolean s8c1t
= false;
418 * Printer mode. True means send all output to printer, which discards
421 private boolean printerControllerMode
= false;
424 * LMN line mode. If true, linefeed() puts the cursor on the first
425 * column of the next line. If false, linefeed() puts the cursor one
426 * line down on the current line. The default is false.
428 private boolean newLineMode
= false;
431 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
433 private ArrowKeyMode arrowKeyMode
;
436 * Whether number pad keys send VT100 or VT52, application or numeric
439 @SuppressWarnings("unused")
440 private KeypadMode keypadMode
;
443 * When true, the terminal is in 132-column mode (DECCOLM).
445 private boolean columns132
= false;
448 * true = reverse video. Set by DECSCNM.
450 private boolean reverseVideo
= false;
453 * false = echo characters locally.
455 private boolean fullDuplex
= true;
458 * The current terminal state.
460 private SaveableState currentState
;
463 * The last saved terminal state.
465 private SaveableState savedState
;
468 * The 88- or 256-color support RGB colors.
470 private List
<Integer
> colors88
;
473 * Sixel collection buffer.
475 private StringBuilder sixelParseBuffer
= new StringBuilder(2048);
478 * Sixel shared palette.
480 private HashMap
<Integer
, java
.awt
.Color
> sixelPalette
;
483 * The width of a character cell in pixels.
485 private int textWidth
= 16;
488 * The height of a character cell in pixels.
490 private int textHeight
= 20;
493 * The last used height of a character cell in pixels, only used for
496 private int lastTextHeight
= -1;
499 * The glyph drawer for full-width chars.
501 private GlyphMaker glyphMaker
= null;
504 * Input queue for keystrokes and mouse events to send to the remote
507 private ArrayList
<TInputEvent
> userQueue
= new ArrayList
<TInputEvent
>();
510 * DECSC/DECRC save/restore a subset of the total state. This class
511 * encapsulates those specific flags/modes.
513 private class SaveableState
{
516 * When true, cursor positions are relative to the scrolling region.
518 public boolean originMode
= false;
521 * The current editing X position.
523 public int cursorX
= 0;
526 * The current editing Y position.
528 public int cursorY
= 0;
531 * Which character set is currently selected in G0.
533 public CharacterSet g0Charset
= CharacterSet
.US
;
536 * Which character set is currently selected in G1.
538 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
541 * Which character set is currently selected in G2.
543 public CharacterSet g2Charset
= CharacterSet
.US
;
546 * Which character set is currently selected in G3.
548 public CharacterSet g3Charset
= CharacterSet
.US
;
551 * Which character set is currently selected in GR.
553 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
556 * The current drawing attributes.
558 public CellAttributes attr
;
563 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
568 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
573 public boolean lineWrap
= true;
578 public void reset() {
582 g0Charset
= CharacterSet
.US
;
583 g1Charset
= CharacterSet
.DRAWING
;
584 g2Charset
= CharacterSet
.US
;
585 g3Charset
= CharacterSet
.US
;
586 grCharset
= CharacterSet
.DRAWING
;
587 attr
= new CellAttributes();
588 glLockshift
= LockshiftMode
.NONE
;
589 grLockshift
= LockshiftMode
.NONE
;
594 * Copy attributes from another instance.
596 * @param that the other instance to match
598 public void setTo(final SaveableState that
) {
599 this.originMode
= that
.originMode
;
600 this.cursorX
= that
.cursorX
;
601 this.cursorY
= that
.cursorY
;
602 this.g0Charset
= that
.g0Charset
;
603 this.g1Charset
= that
.g1Charset
;
604 this.g2Charset
= that
.g2Charset
;
605 this.g3Charset
= that
.g3Charset
;
606 this.grCharset
= that
.grCharset
;
607 this.attr
= new CellAttributes();
608 this.attr
.setTo(that
.attr
);
609 this.glLockshift
= that
.glLockshift
;
610 this.grLockshift
= that
.grLockshift
;
611 this.lineWrap
= that
.lineWrap
;
615 * Public constructor.
617 public SaveableState() {
622 // ------------------------------------------------------------------------
623 // Constructors -----------------------------------------------------------
624 // ------------------------------------------------------------------------
627 * Public constructor.
629 * @param type one of the DeviceType constants to select VT100, VT102,
631 * @param inputStream an InputStream connected to the remote side. For
632 * type == XTERM, inputStream is converted to a Reader with UTF-8
634 * @param outputStream an OutputStream connected to the remote user. For
635 * type == XTERM, outputStream is converted to a Writer with UTF-8
637 * @param displayListener a callback to the outer display, or null for
638 * default VT100 behavior
639 * @throws UnsupportedEncodingException if an exception is thrown when
640 * creating the InputStreamReader
642 public ECMA48(final DeviceType type
, final InputStream inputStream
,
643 final OutputStream outputStream
, final DisplayListener displayListener
)
644 throws UnsupportedEncodingException
{
646 assert (inputStream
!= null);
647 assert (outputStream
!= null);
649 csiParams
= new ArrayList
<Integer
>();
650 tabStops
= new ArrayList
<Integer
>();
651 scrollback
= new ArrayList
<DisplayLine
>();
652 display
= new ArrayList
<DisplayLine
>();
655 if (inputStream
instanceof TimeoutInputStream
) {
656 this.inputStream
= (TimeoutInputStream
)inputStream
;
658 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
660 if (type
== DeviceType
.XTERM
) {
661 this.input
= new InputStreamReader(new BufferedInputStream(
662 this.inputStream
, 1024 * 128), "UTF-8");
663 this.output
= new OutputStreamWriter(new
664 BufferedOutputStream(outputStream
), "UTF-8");
665 this.outputStream
= null;
668 this.outputStream
= new BufferedOutputStream(outputStream
);
670 this.displayListener
= displayListener
;
673 for (int i
= 0; i
< height
; i
++) {
674 display
.add(new DisplayLine(currentState
.attr
));
677 // Spin up the input reader
678 readerThread
= new Thread(this);
679 readerThread
.start();
682 // ------------------------------------------------------------------------
683 // Runnable ---------------------------------------------------------------
684 // ------------------------------------------------------------------------
687 * Read function runs on a separate thread.
689 public final void run() {
690 boolean utf8
= false;
691 boolean done
= false;
693 if (type
== DeviceType
.XTERM
) {
697 // available() will often return > 1, so we need to read in chunks to
699 char [] readBufferUTF8
= null;
700 byte [] readBuffer
= null;
702 readBufferUTF8
= new char[2048];
704 readBuffer
= new byte[2048];
707 while (!done
&& !stopReaderThread
) {
708 synchronized (userQueue
) {
709 while (userQueue
.size() > 0) {
710 handleUserEvent(userQueue
.remove(0));
715 int n
= inputStream
.available();
717 // System.err.printf("available() %d\n", n); System.err.flush();
719 if (readBufferUTF8
.length
< n
) {
720 // The buffer wasn't big enough, make it huger
721 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
724 readBufferUTF8
= new char[newSizeHalf
* 2];
727 if (readBuffer
.length
< n
) {
728 // The buffer wasn't big enough, make it huger
729 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
730 readBuffer
= new byte[newSizeHalf
* 2];
736 } catch (InterruptedException e
) {
745 rc
= input
.read(readBufferUTF8
, 0,
746 readBufferUTF8
.length
);
748 rc
= inputStream
.read(readBuffer
, 0,
751 } catch (ReadTimeoutException e
) {
755 // System.err.printf("read() %d\n", rc); System.err.flush();
760 // Don't step on UI events
761 synchronized (this) {
763 for (int i
= 0; i
< rc
;) {
764 int ch
= Character
.codePointAt(readBufferUTF8
,
766 i
+= Character
.charCount(ch
);
768 // Special case for VT10x: 7-bit characters
770 if ((type
== DeviceType
.VT100
)
771 || (type
== DeviceType
.VT102
)
779 for (int i
= 0; i
< rc
; i
++) {
780 // Special case for VT10x: 7-bit characters
782 if ((type
== DeviceType
.VT100
)
783 || (type
== DeviceType
.VT102
)
785 consume(readBuffer
[i
] & 0x7F);
787 consume(readBuffer
[i
]);
792 // Permit my enclosing UI to know that I updated.
793 if (displayListener
!= null) {
794 displayListener
.displayChanged();
797 // System.err.println("end while loop"); System.err.flush();
798 } catch (IOException e
) {
801 // This is an unusual case. We want to see the stack trace,
802 // but it is related to the spawned process rather than the
803 // actual UI. We will generate the stack trace, and consume
804 // it as though it was emitted by the shell.
805 CharArrayWriter writer
= new CharArrayWriter();
806 // Send a ST and RIS to clear the emulator state.
808 writer
.write("\033\\\033c");
809 writer
.write("\n-----------------------------------\n");
810 e
.printStackTrace(new PrintWriter(writer
));
811 writer
.write("\n-----------------------------------\n");
812 } catch (IOException e2
) {
815 char [] stackTrace
= writer
.toCharArray();
816 for (int i
= 0; i
< stackTrace
.length
; i
++) {
817 if (stackTrace
[i
] == '\n') {
820 consume(stackTrace
[i
]);
824 } // while ((done == false) && (stopReaderThread == false))
826 // Let the rest of the world know that I am done.
827 stopReaderThread
= true;
830 inputStream
.cancelRead();
833 } catch (IOException e
) {
839 } catch (IOException e
) {
843 // Permit my enclosing UI to know that I updated.
844 if (displayListener
!= null) {
845 displayListener
.displayChanged();
848 // System.err.println("*** run() exiting..."); System.err.flush();
851 // ------------------------------------------------------------------------
852 // ECMA48 -----------------------------------------------------------------
853 // ------------------------------------------------------------------------
856 * Process keyboard and mouse events from the user.
858 * @param event the input event to consume
860 private void handleUserEvent(final TInputEvent event
) {
861 if (event
instanceof TKeypressEvent
) {
862 keypress(((TKeypressEvent
) event
).getKey());
864 if (event
instanceof TMouseEvent
) {
865 mouse((TMouseEvent
) event
);
870 * Add a keyboard and mouse event from the user to the queue.
872 * @param event the input event to consume
874 public void addUserEvent(final TInputEvent event
) {
875 synchronized (userQueue
) {
876 userQueue
.add(event
);
881 * Return the proper primary Device Attributes string.
883 * @return string to send to remote side that is appropriate for the
886 private String
deviceTypeResponse() {
889 // "I am a VT100 with advanced video option" (often VT102)
898 // "I am a VT220" - 7 bit version, with sixel and Jexer image
901 return "\033[?62;1;6;9;4;22;444c";
903 // "I am a VT220" - 8 bit version, with sixel and Jexer image
905 return "\u009b?62;1;6;9;4;22;444c";
907 throw new IllegalArgumentException("Invalid device type: " + type
);
912 * Return the proper TERM environment variable for this device type.
914 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
915 * @return "vt100", "xterm", etc.
917 public static String
deviceTypeTerm(final DeviceType deviceType
) {
918 switch (deviceType
) {
932 throw new IllegalArgumentException("Invalid device type: "
938 * Return the proper LANG for this device type. Only XTERM devices know
939 * about UTF-8, the others are defined by their standard to be either
940 * 7-bit or 8-bit characters only.
942 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
943 * @param baseLang a base language without UTF-8 flag such as "C" or
945 * @return "en_US", "en_US.UTF-8", etc.
947 public static String
deviceTypeLang(final DeviceType deviceType
,
948 final String baseLang
) {
950 switch (deviceType
) {
958 return baseLang
+ ".UTF-8";
961 throw new IllegalArgumentException("Invalid device type: "
967 * Write a string directly to the remote side.
969 * @param str string to send
971 public void writeRemote(final String str
) {
972 if (stopReaderThread
) {
973 // Reader hit EOF, bail out now.
978 // System.err.printf("writeRemote() '%s'\n", str);
984 if (outputStream
== null) {
988 outputStream
.flush();
989 for (int i
= 0; i
< str
.length(); i
++) {
990 outputStream
.write(str
.charAt(i
));
992 outputStream
.flush();
993 } catch (IOException e
) {
999 if (output
== null) {
1006 } catch (IOException e
) {
1012 throw new IllegalArgumentException("Invalid device type: " + type
);
1017 * Close the input and output streams and stop the reader thread. Note
1018 * that it is safe to call this multiple times.
1020 public final void close() {
1022 // Tell the reader thread to stop looking at input. It will close
1023 // the input streams.
1024 if (stopReaderThread
== false) {
1025 stopReaderThread
= true;
1027 readerThread
.join(1000);
1028 } catch (InterruptedException e
) {
1033 // Now close the output stream.
1038 if (outputStream
!= null) {
1040 outputStream
.close();
1041 } catch (IOException e
) {
1044 outputStream
= null;
1048 if (outputStream
!= null) {
1050 outputStream
.close();
1051 } catch (IOException e
) {
1054 outputStream
= null;
1056 if (output
!= null) {
1059 } catch (IOException e
) {
1066 throw new IllegalArgumentException("Invalid device type: " +
1072 * See if the reader thread is still running.
1074 * @return if true, we are still connected to / reading from the remote
1077 public final boolean isReading() {
1078 return (!stopReaderThread
);
1082 * Obtain a new blank display line for an external user
1083 * (e.g. TTerminalWindow).
1085 * @return new blank line
1087 public final DisplayLine
getBlankDisplayLine() {
1088 return new DisplayLine(currentState
.attr
);
1092 * Get the scrollback buffer.
1094 * @return the scrollback buffer
1096 public final List
<DisplayLine
> getScrollbackBuffer() {
1101 * Get the display buffer.
1103 * @return the display buffer
1105 public final List
<DisplayLine
> getDisplayBuffer() {
1110 * Get the visible display + scrollback buffer, offset by a specified
1111 * number of rows from the bottom.
1113 * @param visibleHeight the total height of the display to show
1114 * @param scrollBottom the number of rows from the bottom to scroll back
1115 * @return a copy of the display + scrollback buffers
1117 public final List
<DisplayLine
> getVisibleDisplay(final int visibleHeight
,
1118 final int scrollBottom
) {
1120 assert (visibleHeight
>= 0);
1121 assert (scrollBottom
>= 0);
1123 int visibleBottom
= scrollback
.size() + display
.size() - scrollBottom
;
1125 List
<DisplayLine
> preceedingBlankLines
= new ArrayList
<DisplayLine
>();
1126 int visibleTop
= visibleBottom
- visibleHeight
;
1127 if (visibleTop
< 0) {
1128 for (int i
= visibleTop
; i
< 0; i
++) {
1129 preceedingBlankLines
.add(getBlankDisplayLine());
1133 assert (visibleTop
>= 0);
1135 List
<DisplayLine
> displayLines
= new ArrayList
<DisplayLine
>();
1136 displayLines
.addAll(scrollback
);
1137 displayLines
.addAll(display
);
1139 List
<DisplayLine
> visibleLines
= new ArrayList
<DisplayLine
>();
1140 visibleLines
.addAll(preceedingBlankLines
);
1141 visibleLines
.addAll(displayLines
.subList(visibleTop
, visibleBottom
));
1143 // Fill in the blank lines on bottom
1144 int bottomBlankLines
= visibleHeight
- visibleLines
.size();
1145 assert (bottomBlankLines
>= 0);
1146 for (int i
= 0; i
< bottomBlankLines
; i
++) {
1147 visibleLines
.add(getBlankDisplayLine());
1150 return copyBuffer(visibleLines
);
1154 * Copy a display buffer.
1156 * @param buffer the buffer to copy
1157 * @return a deep copy of the buffer's data
1159 private List
<DisplayLine
> copyBuffer(final List
<DisplayLine
> buffer
) {
1160 ArrayList
<DisplayLine
> result
= new ArrayList
<DisplayLine
>(buffer
.size());
1161 for (DisplayLine line
: buffer
) {
1162 result
.add(new DisplayLine(line
));
1168 * Get the display width.
1170 * @return the width (usually 80 or 132)
1172 public final int getWidth() {
1177 * Set the display width.
1179 * @param width the new width
1181 public final synchronized void setWidth(final int width
) {
1183 rightMargin
= width
- 1;
1184 if (currentState
.cursorX
>= width
) {
1185 currentState
.cursorX
= width
- 1;
1187 if (savedState
.cursorX
>= width
) {
1188 savedState
.cursorX
= width
- 1;
1193 * Get the display height.
1195 * @return the height (usually 24)
1197 public final int getHeight() {
1202 * Set the display height.
1204 * @param height the new height
1206 public final synchronized void setHeight(final int height
) {
1207 int delta
= height
- this.height
;
1208 this.height
= height
;
1209 scrollRegionBottom
+= delta
;
1210 if (scrollRegionBottom
< 0) {
1211 scrollRegionBottom
= height
;
1213 if (scrollRegionTop
>= scrollRegionBottom
) {
1214 scrollRegionTop
= 0;
1216 if (currentState
.cursorY
>= height
) {
1217 currentState
.cursorY
= height
- 1;
1219 if (savedState
.cursorY
>= height
) {
1220 savedState
.cursorY
= height
- 1;
1222 while (display
.size() < height
) {
1223 DisplayLine line
= new DisplayLine(currentState
.attr
);
1224 line
.setReverseColor(reverseVideo
);
1227 while (display
.size() > height
) {
1228 scrollback
.add(display
.remove(0));
1233 * Get visible cursor flag.
1235 * @return if true, the cursor is visible
1237 public final boolean isCursorVisible() {
1238 return cursorVisible
;
1242 * Get the screen title as set by the xterm OSC sequence. Lots of
1243 * applications send a screenTitle regardless of whether it is an xterm
1246 * @return screen title
1248 public final String
getScreenTitle() {
1253 * Get 132 columns value.
1255 * @return if true, the terminal is in 132 column mode
1257 public final boolean isColumns132() {
1262 * Clear the CSI parameters and flags.
1264 private void toGround() {
1266 collectBuffer
.setLength(0);
1267 scanState
= ScanState
.GROUND
;
1271 * Reset the tab stops list.
1273 private void resetTabStops() {
1275 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
1276 tabStops
.add(Integer
.valueOf(i
* 8));
1281 * Reset the 88- or 256-colors.
1283 private void resetColors() {
1284 colors88
= new ArrayList
<Integer
>(256);
1285 for (int i
= 0; i
< 256; i
++) {
1289 // Set default system colors.
1290 colors88
.set(0, 0x00000000);
1291 colors88
.set(1, 0x00a80000);
1292 colors88
.set(2, 0x0000a800);
1293 colors88
.set(3, 0x00a85400);
1294 colors88
.set(4, 0x000000a8);
1295 colors88
.set(5, 0x00a800a8);
1296 colors88
.set(6, 0x0000a8a8);
1297 colors88
.set(7, 0x00a8a8a8);
1299 colors88
.set(8, 0x00545454);
1300 colors88
.set(9, 0x00fc5454);
1301 colors88
.set(10, 0x0054fc54);
1302 colors88
.set(11, 0x00fcfc54);
1303 colors88
.set(12, 0x005454fc);
1304 colors88
.set(13, 0x00fc54fc);
1305 colors88
.set(14, 0x0054fcfc);
1306 colors88
.set(15, 0x00fcfcfc);
1310 * Get the RGB value of one of the indexed colors.
1312 * @param index the color index
1313 * @return the RGB value
1315 private int get88Color(final int index
) {
1316 // System.err.print("get88Color: " + index);
1317 if ((index
< 0) || (index
> colors88
.size())) {
1318 // System.err.println(" -- UNKNOWN");
1321 // System.err.printf(" %08x\n", colors88.get(index));
1322 return colors88
.get(index
);
1326 * Set one of the indexed colors to a color specification.
1328 * @param index the color index
1329 * @param spec the specification, typically something like "rgb:aa/bb/cc"
1331 private void set88Color(final int index
, final String spec
) {
1332 // System.err.println("set88Color: " + index + " '" + spec + "'");
1334 if ((index
< 0) || (index
> colors88
.size())) {
1337 if (spec
.startsWith("rgb:")) {
1338 String
[] rgbTokens
= spec
.substring(4).split("/");
1339 if (rgbTokens
.length
== 3) {
1341 int rgb
= (Integer
.parseInt(rgbTokens
[0], 16) << 16);
1342 rgb
|= Integer
.parseInt(rgbTokens
[1], 16) << 8;
1343 rgb
|= Integer
.parseInt(rgbTokens
[2], 16);
1344 // System.err.printf(" set to %08x\n", rgb);
1345 colors88
.set(index
, rgb
);
1346 } catch (NumberFormatException e
) {
1353 if (spec
.toLowerCase().equals("black")) {
1354 colors88
.set(index
, 0x00000000);
1355 } else if (spec
.toLowerCase().equals("red")) {
1356 colors88
.set(index
, 0x00a80000);
1357 } else if (spec
.toLowerCase().equals("green")) {
1358 colors88
.set(index
, 0x0000a800);
1359 } else if (spec
.toLowerCase().equals("yellow")) {
1360 colors88
.set(index
, 0x00a85400);
1361 } else if (spec
.toLowerCase().equals("blue")) {
1362 colors88
.set(index
, 0x000000a8);
1363 } else if (spec
.toLowerCase().equals("magenta")) {
1364 colors88
.set(index
, 0x00a800a8);
1365 } else if (spec
.toLowerCase().equals("cyan")) {
1366 colors88
.set(index
, 0x0000a8a8);
1367 } else if (spec
.toLowerCase().equals("white")) {
1368 colors88
.set(index
, 0x00a8a8a8);
1374 * Reset the emulation state.
1376 private void reset() {
1378 currentState
= new SaveableState();
1379 savedState
= new SaveableState();
1380 scanState
= ScanState
.GROUND
;
1383 scrollRegionTop
= 0;
1384 scrollRegionBottom
= height
- 1;
1385 rightMargin
= width
- 1;
1386 newLineMode
= false;
1387 arrowKeyMode
= ArrowKeyMode
.ANSI
;
1388 keypadMode
= KeypadMode
.Numeric
;
1389 wrapLineFlag
= false;
1390 if (displayListener
!= null) {
1391 width
= displayListener
.getDisplayWidth();
1392 height
= displayListener
.getDisplayHeight();
1393 rightMargin
= width
- 1;
1401 newLineMode
= false;
1402 reverseVideo
= false;
1404 cursorVisible
= true;
1407 singleshift
= Singleshift
.NONE
;
1409 printerControllerMode
= false;
1412 mouseProtocol
= MouseProtocol
.OFF
;
1413 mouseEncoding
= MouseEncoding
.X10
;
1418 // Reset extra colors
1426 * Append a new line to the bottom of the display, adding lines off the
1427 * top to the scrollback buffer.
1429 private void newDisplayLine() {
1430 // Scroll the top line off into the scrollback buffer
1431 scrollback
.add(display
.get(0));
1432 if (scrollback
.size() > maxScrollback
) {
1433 scrollback
.remove(0);
1434 scrollback
.trimToSize();
1437 display
.trimToSize();
1438 DisplayLine line
= new DisplayLine(currentState
.attr
);
1439 line
.setReverseColor(reverseVideo
);
1444 * Wraps the current line.
1446 private void wrapCurrentLine() {
1447 if (currentState
.cursorY
== height
- 1) {
1450 if (currentState
.cursorY
< height
- 1) {
1451 currentState
.cursorY
++;
1453 currentState
.cursorX
= 0;
1457 * Handle a carriage return.
1459 private void carriageReturn() {
1460 currentState
.cursorX
= 0;
1461 wrapLineFlag
= false;
1465 * Reverse the color of the visible display.
1467 private void invertDisplayColors() {
1468 for (DisplayLine line
: display
) {
1469 line
.setReverseColor(!line
.isReverseColor());
1474 * Handle a linefeed.
1476 private void linefeed() {
1478 if (currentState
.cursorY
< scrollRegionBottom
) {
1479 // Increment screen y
1480 currentState
.cursorY
++;
1483 // Screen y does not increment
1486 * Two cases: either we're inside a scrolling region or not. If
1487 * the scrolling region bottom is the bottom of the screen, then
1488 * push the top line into the buffer. Else scroll the scrolling
1491 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1493 // We're at the bottom of the scroll region, AND the scroll
1494 // region is the entire screen.
1500 // We're at the bottom of the scroll region, AND the scroll
1501 // region is NOT the entire screen.
1502 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1507 currentState
.cursorX
= 0;
1509 wrapLineFlag
= false;
1513 * Prints one character to the display buffer.
1515 * @param ch character to display
1517 private void printCharacter(final int ch
) {
1518 int rightMargin
= this.rightMargin
;
1520 if (StringUtils
.width(ch
) == 2) {
1521 // This is a full-width character. Save two spaces, and then
1522 // draw the character as two image halves.
1523 int x0
= currentState
.cursorX
;
1524 int y0
= currentState
.cursorY
;
1525 printCharacter(' ');
1526 printCharacter(' ');
1527 if ((currentState
.cursorX
== x0
+ 2)
1528 && (currentState
.cursorY
== y0
)
1530 // We can draw both halves of the character.
1531 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1532 } else if ((currentState
.cursorX
== x0
+ 1)
1533 && (currentState
.cursorY
== y0
)
1535 // VT100 line wrap behavior: we should be at the right
1536 // margin. We can draw both halves of the character.
1537 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1539 // The character splits across the line. Draw the entire
1540 // character on the new line, giving one more space for it.
1541 x0
= currentState
.cursorX
- 1;
1542 y0
= currentState
.cursorY
;
1543 printCharacter(' ');
1544 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1549 // Check if we have double-width, and if so chop at 40/66 instead of
1551 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1552 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1555 // Check the unusually-complicated line wrapping conditions...
1556 if (currentState
.cursorX
== rightMargin
) {
1558 if (currentState
.lineWrap
== true) {
1560 * This case happens when: the cursor was already on the
1561 * right margin (either through printing or by an explicit
1562 * placement command), and a character was printed.
1564 * The line wraps only when a new character arrives AND the
1565 * cursor is already on the right margin AND has placed a
1566 * character in its cell. Easier to see than to explain.
1568 if (wrapLineFlag
== false) {
1570 * This block marks the case that we are in the margin
1571 * and the first character has been received and printed.
1573 wrapLineFlag
= true;
1576 * This block marks the case that we are in the margin
1577 * and the second character has been received and
1580 wrapLineFlag
= false;
1584 } else if (currentState
.cursorX
<= rightMargin
) {
1586 * This is the normal case: a character came in and was printed
1587 * to the left of the right margin column.
1590 // Turn off VT100 special-case flag
1591 wrapLineFlag
= false;
1594 // "Print" the character
1595 Cell newCell
= new Cell(ch
);
1596 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1597 newCellAttributes
.setTo(currentState
.attr
);
1598 DisplayLine line
= display
.get(currentState
.cursorY
);
1600 if (StringUtils
.width(ch
) == 1) {
1601 // Insert mode special case
1602 if (insertMode
== true) {
1603 line
.insert(currentState
.cursorX
, newCell
);
1605 // Replace an existing character
1606 line
.replace(currentState
.cursorX
, newCell
);
1609 // Increment horizontal
1610 if (wrapLineFlag
== false) {
1611 currentState
.cursorX
++;
1612 if (currentState
.cursorX
> rightMargin
) {
1613 currentState
.cursorX
--;
1620 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1621 * send to the remote side.
1623 * @param mouse mouse event received from the local user
1625 private void mouse(final TMouseEvent mouse
) {
1628 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1629 mouseProtocol, mouseEncoding, mouse);
1632 if (mouseEncoding
== MouseEncoding
.X10
) {
1633 // We will support X10 but only for (160,94) and smaller.
1634 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1639 switch (mouseProtocol
) {
1646 // Only report button presses
1647 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1653 // Only report button presses and releases
1654 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1655 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1663 * Only report button presses, button releases, and motions that
1664 * have a button down (i.e. drag-and-drop).
1666 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1667 if (!mouse
.isMouse1()
1668 && !mouse
.isMouse2()
1669 && !mouse
.isMouse3()
1670 && !mouse
.isMouseWheelUp()
1671 && !mouse
.isMouseWheelDown()
1679 // Report everything
1683 // Now encode the event
1684 StringBuilder sb
= new StringBuilder(6);
1685 if (mouseEncoding
== MouseEncoding
.SGR
) {
1686 sb
.append((char) 0x1B);
1689 if (mouse
.isMouse1()) {
1690 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1695 } else if (mouse
.isMouse2()) {
1696 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1701 } else if (mouse
.isMouse3()) {
1702 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1707 } else if (mouse
.isMouseWheelUp()) {
1709 } else if (mouse
.isMouseWheelDown()) {
1712 // This is motion with no buttons down.
1716 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1719 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1726 // X10 and UTF8 encodings
1727 sb
.append((char) 0x1B);
1730 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1731 sb
.append((char) (0x03 + 32));
1732 } else if (mouse
.isMouse1()) {
1733 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1734 sb
.append((char) (0x00 + 32 + 32));
1736 sb
.append((char) (0x00 + 32));
1738 } else if (mouse
.isMouse2()) {
1739 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1740 sb
.append((char) (0x01 + 32 + 32));
1742 sb
.append((char) (0x01 + 32));
1744 } else if (mouse
.isMouse3()) {
1745 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1746 sb
.append((char) (0x02 + 32 + 32));
1748 sb
.append((char) (0x02 + 32));
1750 } else if (mouse
.isMouseWheelUp()) {
1751 sb
.append((char) (0x04 + 64));
1752 } else if (mouse
.isMouseWheelDown()) {
1753 sb
.append((char) (0x05 + 64));
1755 // This is motion with no buttons down.
1756 sb
.append((char) (0x03 + 32));
1759 sb
.append((char) (mouse
.getX() + 33));
1760 sb
.append((char) (mouse
.getY() + 33));
1763 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1764 writeRemote(sb
.toString());
1768 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1769 * send to the remote side.
1771 * @param keypress keypress received from the local user
1773 private void keypress(final TKeypress keypress
) {
1774 writeRemote(keypressToString(keypress
));
1778 * Build one of the complex xterm keystroke sequences, storing the result in
1779 * xterm_keystroke_buffer.
1781 * @param ss3 the prefix to use based on VT100 state.
1782 * @param first the first character, usually a number.
1783 * @param first the last character, one of the following: ~ A B C D F H
1784 * @param ctrl whether or not ctrl is down
1785 * @param alt whether or not alt is down
1786 * @param shift whether or not shift is down
1787 * @return the buffer with the full key sequence
1789 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1790 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1792 StringBuilder sb
= new StringBuilder(ss3
);
1793 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1797 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1799 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1801 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1803 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1805 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1807 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1809 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1814 return sb
.toString();
1818 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1820 * @param keypress keypress received from the local user
1821 * @return string to transmit to the remote side
1823 @SuppressWarnings("fallthrough")
1824 private String
keypressToString(final TKeypress keypress
) {
1826 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1828 * If this is a control character, process it like it came from
1831 if (keypress
.getChar() < 0x20) {
1832 handleControlChar((char) keypress
.getChar());
1834 // Local echo for everything else
1835 printCharacter(keypress
.getChar());
1837 if (displayListener
!= null) {
1838 displayListener
.displayChanged();
1842 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1847 // Handle control characters
1848 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1849 StringBuilder sb
= new StringBuilder();
1850 int ch
= keypress
.getChar();
1852 sb
.append(Character
.toChars(ch
));
1853 return sb
.toString();
1856 // Handle alt characters
1857 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1858 StringBuilder sb
= new StringBuilder("\033");
1859 int ch
= keypress
.getChar();
1860 sb
.append(Character
.toChars(ch
));
1861 return sb
.toString();
1864 if (keypress
.equals(kbBackspaceDel
)) {
1877 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1880 switch (arrowKeyMode
) {
1882 return xtermBuildKeySequence("\033[", '1', 'D',
1883 keypress
.isCtrl(), keypress
.isAlt(),
1884 keypress
.isShift());
1886 return xtermBuildKeySequence("\033", '1', 'D',
1887 keypress
.isCtrl(), keypress
.isAlt(),
1888 keypress
.isShift());
1890 return xtermBuildKeySequence("\033O", '1', 'D',
1891 keypress
.isCtrl(), keypress
.isAlt(),
1892 keypress
.isShift());
1895 switch (arrowKeyMode
) {
1906 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1909 switch (arrowKeyMode
) {
1911 return xtermBuildKeySequence("\033[", '1', 'C',
1912 keypress
.isCtrl(), keypress
.isAlt(),
1913 keypress
.isShift());
1915 return xtermBuildKeySequence("\033", '1', 'C',
1916 keypress
.isCtrl(), keypress
.isAlt(),
1917 keypress
.isShift());
1919 return xtermBuildKeySequence("\033O", '1', 'C',
1920 keypress
.isCtrl(), keypress
.isAlt(),
1921 keypress
.isShift());
1924 switch (arrowKeyMode
) {
1935 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1938 switch (arrowKeyMode
) {
1940 return xtermBuildKeySequence("\033[", '1', 'A',
1941 keypress
.isCtrl(), keypress
.isAlt(),
1942 keypress
.isShift());
1944 return xtermBuildKeySequence("\033", '1', 'A',
1945 keypress
.isCtrl(), keypress
.isAlt(),
1946 keypress
.isShift());
1948 return xtermBuildKeySequence("\033O", '1', 'A',
1949 keypress
.isCtrl(), keypress
.isAlt(),
1950 keypress
.isShift());
1953 switch (arrowKeyMode
) {
1964 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1967 switch (arrowKeyMode
) {
1969 return xtermBuildKeySequence("\033[", '1', 'B',
1970 keypress
.isCtrl(), keypress
.isAlt(),
1971 keypress
.isShift());
1973 return xtermBuildKeySequence("\033", '1', 'B',
1974 keypress
.isCtrl(), keypress
.isAlt(),
1975 keypress
.isShift());
1977 return xtermBuildKeySequence("\033O", '1', 'B',
1978 keypress
.isCtrl(), keypress
.isAlt(),
1979 keypress
.isShift());
1982 switch (arrowKeyMode
) {
1993 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1996 switch (arrowKeyMode
) {
1998 return xtermBuildKeySequence("\033[", '1', 'H',
1999 keypress
.isCtrl(), keypress
.isAlt(),
2000 keypress
.isShift());
2002 return xtermBuildKeySequence("\033", '1', 'H',
2003 keypress
.isCtrl(), keypress
.isAlt(),
2004 keypress
.isShift());
2006 return xtermBuildKeySequence("\033O", '1', 'H',
2007 keypress
.isCtrl(), keypress
.isAlt(),
2008 keypress
.isShift());
2011 switch (arrowKeyMode
) {
2022 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
2025 switch (arrowKeyMode
) {
2027 return xtermBuildKeySequence("\033[", '1', 'F',
2028 keypress
.isCtrl(), keypress
.isAlt(),
2029 keypress
.isShift());
2031 return xtermBuildKeySequence("\033", '1', 'F',
2032 keypress
.isCtrl(), keypress
.isAlt(),
2033 keypress
.isShift());
2035 return xtermBuildKeySequence("\033O", '1', 'F',
2036 keypress
.isCtrl(), keypress
.isAlt(),
2037 keypress
.isShift());
2040 switch (arrowKeyMode
) {
2051 if (keypress
.equals(kbF1
)) {
2059 if (keypress
.equals(kbF2
)) {
2067 if (keypress
.equals(kbF3
)) {
2075 if (keypress
.equals(kbF4
)) {
2083 if (keypress
.equals(kbF5
)) {
2096 if (keypress
.equals(kbF6
)) {
2109 if (keypress
.equals(kbF7
)) {
2122 if (keypress
.equals(kbF8
)) {
2135 if (keypress
.equals(kbF9
)) {
2148 if (keypress
.equals(kbF10
)) {
2161 if (keypress
.equals(kbF11
)) {
2165 if (keypress
.equals(kbF12
)) {
2169 if (keypress
.equals(kbShiftF1
)) {
2174 if (type
== DeviceType
.XTERM
) {
2180 if (keypress
.equals(kbShiftF2
)) {
2185 if (type
== DeviceType
.XTERM
) {
2191 if (keypress
.equals(kbShiftF3
)) {
2196 if (type
== DeviceType
.XTERM
) {
2202 if (keypress
.equals(kbShiftF4
)) {
2207 if (type
== DeviceType
.XTERM
) {
2213 if (keypress
.equals(kbShiftF5
)) {
2215 return "\033[15;2~";
2218 if (keypress
.equals(kbShiftF6
)) {
2220 return "\033[17;2~";
2223 if (keypress
.equals(kbShiftF7
)) {
2225 return "\033[18;2~";
2228 if (keypress
.equals(kbShiftF8
)) {
2230 return "\033[19;2~";
2233 if (keypress
.equals(kbShiftF9
)) {
2235 return "\033[20;2~";
2238 if (keypress
.equals(kbShiftF10
)) {
2240 return "\033[21;2~";
2243 if (keypress
.equals(kbShiftF11
)) {
2245 return "\033[23;2~";
2248 if (keypress
.equals(kbShiftF12
)) {
2250 return "\033[24;2~";
2253 if (keypress
.equals(kbCtrlF1
)) {
2258 if (type
== DeviceType
.XTERM
) {
2264 if (keypress
.equals(kbCtrlF2
)) {
2269 if (type
== DeviceType
.XTERM
) {
2275 if (keypress
.equals(kbCtrlF3
)) {
2280 if (type
== DeviceType
.XTERM
) {
2286 if (keypress
.equals(kbCtrlF4
)) {
2291 if (type
== DeviceType
.XTERM
) {
2297 if (keypress
.equals(kbCtrlF5
)) {
2299 return "\033[15;5~";
2302 if (keypress
.equals(kbCtrlF6
)) {
2304 return "\033[17;5~";
2307 if (keypress
.equals(kbCtrlF7
)) {
2309 return "\033[18;5~";
2312 if (keypress
.equals(kbCtrlF8
)) {
2314 return "\033[19;5~";
2317 if (keypress
.equals(kbCtrlF9
)) {
2319 return "\033[20;5~";
2322 if (keypress
.equals(kbCtrlF10
)) {
2324 return "\033[21;5~";
2327 if (keypress
.equals(kbCtrlF11
)) {
2329 return "\033[23;5~";
2332 if (keypress
.equals(kbCtrlF12
)) {
2334 return "\033[24;5~";
2337 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2340 return xtermBuildKeySequence("\033[", '5', '~',
2341 keypress
.isCtrl(), keypress
.isAlt(),
2342 keypress
.isShift());
2348 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2351 return xtermBuildKeySequence("\033[", '6', '~',
2352 keypress
.isCtrl(), keypress
.isAlt(),
2353 keypress
.isShift());
2359 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2362 return xtermBuildKeySequence("\033[", '2', '~',
2363 keypress
.isCtrl(), keypress
.isAlt(),
2364 keypress
.isShift());
2370 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2373 return xtermBuildKeySequence("\033[", '3', '~',
2374 keypress
.isCtrl(), keypress
.isAlt(),
2375 keypress
.isShift());
2377 // Delete sends real delete for VTxxx
2382 if (keypress
.equals(kbEnter
)) {
2386 if (keypress
.equals(kbEsc
)) {
2390 if (keypress
.equals(kbAltEsc
)) {
2394 if (keypress
.equals(kbTab
)) {
2398 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2399 (keypress
.equals(kbShiftTab
))
2409 // Non-alt, non-ctrl characters
2410 if (!keypress
.isFnKey()) {
2411 StringBuilder sb
= new StringBuilder();
2412 sb
.append(Character
.toChars(keypress
.getChar()));
2413 return sb
.toString();
2419 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2422 * @param ch 8-bit character from the remote side
2423 * @param charsetGl character set defined for GL
2424 * @param charsetGr character set defined for GR
2425 * @return character to display on the screen
2427 private char mapCharacterCharset(final int ch
,
2428 final CharacterSet charsetGl
,
2429 final CharacterSet charsetGr
) {
2431 int lookupChar
= ch
;
2432 CharacterSet lookupCharset
= charsetGl
;
2435 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2436 lookupCharset
= charsetGr
;
2440 switch (lookupCharset
) {
2443 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2446 return DECCharacterSets
.UK
[lookupChar
];
2449 return DECCharacterSets
.US_ASCII
[lookupChar
];
2452 return DECCharacterSets
.NL
[lookupChar
];
2455 return DECCharacterSets
.FI
[lookupChar
];
2458 return DECCharacterSets
.FR
[lookupChar
];
2461 return DECCharacterSets
.FR_CA
[lookupChar
];
2464 return DECCharacterSets
.DE
[lookupChar
];
2467 return DECCharacterSets
.IT
[lookupChar
];
2470 return DECCharacterSets
.NO
[lookupChar
];
2473 return DECCharacterSets
.ES
[lookupChar
];
2476 return DECCharacterSets
.SV
[lookupChar
];
2479 return DECCharacterSets
.SWISS
[lookupChar
];
2481 case DEC_SUPPLEMENTAL
:
2482 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2485 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2488 return DECCharacterSets
.US_ASCII
[lookupChar
];
2491 return DECCharacterSets
.US_ASCII
[lookupChar
];
2494 throw new IllegalArgumentException("Invalid character set value: "
2500 * Map an 8-bit byte into a printable character.
2502 * @param ch either 8-bit or Unicode character from the remote side
2503 * @return character to display on the screen
2505 private int mapCharacter(final int ch
) {
2507 // Unicode character, just return it
2511 CharacterSet charsetGl
= currentState
.g0Charset
;
2512 CharacterSet charsetGr
= currentState
.grCharset
;
2514 if (vt52Mode
== true) {
2515 if (shiftOut
== true) {
2516 // Shifted out character, pull from VT52 graphics
2517 charsetGl
= currentState
.g1Charset
;
2518 charsetGr
= CharacterSet
.US
;
2521 charsetGl
= currentState
.g0Charset
;
2522 charsetGr
= CharacterSet
.US
;
2525 // Pull the character
2526 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2530 if (shiftOut
== true) {
2531 // Shifted out character, pull from G1
2532 charsetGl
= currentState
.g1Charset
;
2533 charsetGr
= currentState
.grCharset
;
2535 // Pull the character
2536 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2540 if (singleshift
== Singleshift
.SS2
) {
2542 singleshift
= Singleshift
.NONE
;
2544 // Shifted out character, pull from G2
2545 charsetGl
= currentState
.g2Charset
;
2546 charsetGr
= currentState
.grCharset
;
2550 if (singleshift
== Singleshift
.SS3
) {
2552 singleshift
= Singleshift
.NONE
;
2554 // Shifted out character, pull from G3
2555 charsetGl
= currentState
.g3Charset
;
2556 charsetGr
= currentState
.grCharset
;
2559 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2560 // Check for locking shift
2562 switch (currentState
.glLockshift
) {
2565 throw new IllegalArgumentException("programming bug");
2568 throw new IllegalArgumentException("programming bug");
2571 throw new IllegalArgumentException("programming bug");
2575 charsetGl
= currentState
.g2Charset
;
2580 charsetGl
= currentState
.g3Charset
;
2585 charsetGl
= currentState
.g0Charset
;
2589 switch (currentState
.grLockshift
) {
2592 throw new IllegalArgumentException("programming bug");
2595 throw new IllegalArgumentException("programming bug");
2599 charsetGr
= currentState
.g1Charset
;
2604 charsetGr
= currentState
.g2Charset
;
2609 charsetGr
= currentState
.g3Charset
;
2614 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2621 // Pull the character
2622 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2626 * Scroll the text within a scrolling region up n lines.
2628 * @param regionTop top row of the scrolling region
2629 * @param regionBottom bottom row of the scrolling region
2630 * @param n number of lines to scroll
2632 private void scrollingRegionScrollUp(final int regionTop
,
2633 final int regionBottom
, final int n
) {
2635 if (regionTop
>= regionBottom
) {
2639 // Sanity check: see if there will be any characters left after the
2641 if (regionBottom
+ 1 - regionTop
<= n
) {
2642 // There won't be anything left in the region, so just call
2643 // eraseScreen() and return.
2644 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2648 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2649 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2650 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2652 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2653 - remaining
, regionBottom
+ 1);
2654 display
= new ArrayList
<DisplayLine
>(displayTop
);
2655 display
.addAll(displayMiddle
);
2656 for (int i
= 0; i
< n
; i
++) {
2657 DisplayLine line
= new DisplayLine(currentState
.attr
);
2658 line
.setReverseColor(reverseVideo
);
2661 display
.addAll(displayBottom
);
2663 assert (display
.size() == height
);
2667 * Scroll the text within a scrolling region down n lines.
2669 * @param regionTop top row of the scrolling region
2670 * @param regionBottom bottom row of the scrolling region
2671 * @param n number of lines to scroll
2673 private void scrollingRegionScrollDown(final int regionTop
,
2674 final int regionBottom
, final int n
) {
2676 if (regionTop
>= regionBottom
) {
2680 // Sanity check: see if there will be any characters left after the
2682 if (regionBottom
+ 1 - regionTop
<= n
) {
2683 // There won't be anything left in the region, so just call
2684 // eraseScreen() and return.
2685 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2689 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2690 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2691 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2693 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2694 regionTop
+ remaining
);
2695 display
= new ArrayList
<DisplayLine
>(displayTop
);
2696 for (int i
= 0; i
< n
; i
++) {
2697 DisplayLine line
= new DisplayLine(currentState
.attr
);
2698 line
.setReverseColor(reverseVideo
);
2701 display
.addAll(displayMiddle
);
2702 display
.addAll(displayBottom
);
2704 assert (display
.size() == height
);
2708 * Process a control character.
2710 * @param ch 8-bit character from the remote side
2712 private void handleControlChar(final char ch
) {
2713 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2724 // Transmit the answerback message.
2735 cursorLeft(1, false);
2740 advanceToNextTabStop();
2766 currentState
.glLockshift
= LockshiftMode
.NONE
;
2772 currentState
.glLockshift
= LockshiftMode
.NONE
;
2797 singleshift
= Singleshift
.SS2
;
2802 singleshift
= Singleshift
.SS3
;
2812 * Advance the cursor to the next tab stop.
2814 private void advanceToNextTabStop() {
2815 if (tabStops
.size() == 0) {
2816 // Go to the rightmost column
2817 cursorRight(rightMargin
- currentState
.cursorX
, false);
2820 for (Integer stop
: tabStops
) {
2821 if (stop
> currentState
.cursorX
) {
2822 cursorRight(stop
- currentState
.cursorX
, false);
2827 * We got here, meaning there isn't a tab stop beyond the current
2828 * cursor position. Place the cursor of the right-most edge of the
2831 cursorRight(rightMargin
- currentState
.cursorX
, false);
2835 * Save a character into the collect buffer.
2837 * @param ch character to save
2839 private void collect(final char ch
) {
2840 collectBuffer
.append(ch
);
2844 * Save a byte into the CSI parameters buffer.
2846 * @param ch byte to save
2848 private void param(final byte ch
) {
2849 if (csiParams
.size() == 0) {
2850 csiParams
.add(Integer
.valueOf(0));
2852 Integer x
= csiParams
.get(csiParams
.size() - 1);
2853 if ((ch
>= '0') && (ch
<= '9')) {
2856 csiParams
.set(csiParams
.size() - 1, x
);
2859 if ((ch
== ';') && (csiParams
.size() < 16)) {
2860 csiParams
.add(Integer
.valueOf(0));
2865 * Get a CSI parameter value, with a default.
2867 * @param position parameter index. 0 is the first parameter.
2868 * @param defaultValue value to use if csiParams[position] doesn't exist
2869 * @return parameter value
2871 private int getCsiParam(final int position
, final int defaultValue
) {
2872 if (csiParams
.size() < position
+ 1) {
2873 return defaultValue
;
2875 return csiParams
.get(position
).intValue();
2879 * Get a CSI parameter value, clamped to within min/max.
2881 * @param position parameter index. 0 is the first parameter.
2882 * @param defaultValue value to use if csiParams[position] doesn't exist
2883 * @param minValue minimum value inclusive
2884 * @param maxValue maximum value inclusive
2885 * @return parameter value
2887 private int getCsiParam(final int position
, final int defaultValue
,
2888 final int minValue
, final int maxValue
) {
2890 assert (minValue
<= maxValue
);
2891 int value
= getCsiParam(position
, defaultValue
);
2892 if (value
< minValue
) {
2895 if (value
> maxValue
) {
2902 * Set or unset a toggle.
2904 * @param value true for set ('h'), false for reset ('l')
2906 private void setToggle(final boolean value
) {
2907 boolean decPrivateModeFlag
= false;
2909 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2910 if (collectBuffer
.charAt(i
) == '?') {
2911 decPrivateModeFlag
= true;
2916 for (Integer i
: csiParams
) {
2921 if (decPrivateModeFlag
== true) {
2923 if (value
== true) {
2924 // Use application arrow keys
2925 arrowKeyMode
= ArrowKeyMode
.VT100
;
2927 // Use ANSI arrow keys
2928 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2933 if (decPrivateModeFlag
== true) {
2934 if (value
== false) {
2938 arrowKeyMode
= ArrowKeyMode
.VT52
;
2941 * From the VT102 docs: "You use ANSI mode to select
2942 * most terminal features; the terminal uses the same
2943 * features when it switches to VT52 mode. You
2944 * cannot, however, change most of these features in
2947 * In other words, do not reset any other attributes
2948 * when switching between VT52 submode and ANSI.
2950 * HOWEVER, the real vt100 does switch the character
2951 * set according to Usenet.
2953 currentState
.g0Charset
= CharacterSet
.US
;
2954 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2957 if ((type
== DeviceType
.VT220
)
2958 || (type
== DeviceType
.XTERM
)) {
2960 // VT52 mode is explicitly 7-bit
2962 singleshift
= Singleshift
.NONE
;
2967 if (value
== true) {
2968 // Turn off keyboard
2977 if (decPrivateModeFlag
== true) {
2979 if (value
== true) {
2986 if ((displayListener
!= null)
2987 && (type
== DeviceType
.XTERM
)
2989 // For xterms, reset to the actual width, not 80
2991 width
= displayListener
.getDisplayWidth();
2992 rightMargin
= width
- 1;
2995 width
= rightMargin
+ 1;
2998 // Entire screen is cleared, and scrolling region is
3000 eraseScreen(0, 0, height
- 1, width
- 1, false);
3001 scrollRegionTop
= 0;
3002 scrollRegionBottom
= height
- 1;
3003 // Also home the cursor
3004 cursorPosition(0, 0);
3008 if (decPrivateModeFlag
== true) {
3010 if (value
== true) {
3019 if (value
== true) {
3027 if (decPrivateModeFlag
== true) {
3029 if (value
== true) {
3031 * Set selects reverse screen, a white screen
3032 * background with black characters.
3034 if (reverseVideo
!= true) {
3036 * If in normal video, switch it back
3038 invertDisplayColors();
3040 reverseVideo
= true;
3043 * Reset selects normal screen, a black screen
3044 * background with white characters.
3046 if (reverseVideo
== true) {
3048 * If in reverse video already, switch it back
3050 invertDisplayColors();
3052 reverseVideo
= false;
3057 if (decPrivateModeFlag
== true) {
3059 if (value
== true) {
3060 // Origin is relative to scroll region cursor.
3061 // Cursor can NEVER leave scrolling region.
3062 currentState
.originMode
= true;
3063 cursorPosition(0, 0);
3065 // Origin is absolute to entire screen. Cursor can
3066 // leave the scrolling region via cup() and hvp().
3067 currentState
.originMode
= false;
3068 cursorPosition(0, 0);
3073 if (decPrivateModeFlag
== true) {
3075 if (value
== true) {
3077 currentState
.lineWrap
= true;
3079 // Turn linewrap off
3080 currentState
.lineWrap
= false;
3085 if (decPrivateModeFlag
== true) {
3087 if (value
== true) {
3088 // Keyboard auto-repeat on
3091 // Keyboard auto-repeat off
3097 if (decPrivateModeFlag
== false) {
3099 if (value
== true) {
3109 if (decPrivateModeFlag
== true) {
3115 if (decPrivateModeFlag
== true) {
3121 if (decPrivateModeFlag
== false) {
3123 if (value
== true) {
3125 * Set causes a received linefeed, form feed, or
3126 * vertical tab to move cursor to first column of
3127 * next line. RETURN transmits both a carriage return
3128 * and linefeed. This selection is also called new
3134 * Reset causes a received linefeed, form feed, or
3135 * vertical tab to move cursor to next line in
3136 * current column. RETURN transmits a carriage
3139 newLineMode
= false;
3145 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3146 if (decPrivateModeFlag
== true) {
3148 if (value
== true) {
3150 cursorVisible
= true;
3153 cursorVisible
= false;
3160 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3161 if (decPrivateModeFlag
== true) {
3163 if (value
== true) {
3164 // Select national mode NRC
3167 // Select multi-national mode
3176 if (type
== DeviceType
.XTERM
) {
3177 if (decPrivateModeFlag
== true) {
3178 if (value
== true) {
3179 // Enable sixel scrolling (default).
3182 // Disable sixel scrolling.
3191 if ((type
== DeviceType
.XTERM
)
3192 && (decPrivateModeFlag
== true)
3194 // Mouse: normal tracking mode
3195 if (value
== true) {
3196 mouseProtocol
= MouseProtocol
.NORMAL
;
3198 mouseProtocol
= MouseProtocol
.OFF
;
3204 if ((type
== DeviceType
.XTERM
)
3205 && (decPrivateModeFlag
== true)
3207 // Mouse: normal tracking mode
3208 if (value
== true) {
3209 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
3211 mouseProtocol
= MouseProtocol
.OFF
;
3217 if ((type
== DeviceType
.XTERM
)
3218 && (decPrivateModeFlag
== true)
3220 // Mouse: Any-event tracking mode
3221 if (value
== true) {
3222 mouseProtocol
= MouseProtocol
.ANYEVENT
;
3224 mouseProtocol
= MouseProtocol
.OFF
;
3230 if ((type
== DeviceType
.XTERM
)
3231 && (decPrivateModeFlag
== true)
3233 // Mouse: UTF-8 coordinates
3234 if (value
== true) {
3235 mouseEncoding
= MouseEncoding
.UTF8
;
3237 mouseEncoding
= MouseEncoding
.X10
;
3243 if ((type
== DeviceType
.XTERM
)
3244 && (decPrivateModeFlag
== true)
3246 // Mouse: SGR coordinates
3247 if (value
== true) {
3248 mouseEncoding
= MouseEncoding
.SGR
;
3250 mouseEncoding
= MouseEncoding
.X10
;
3256 if (type
== DeviceType
.XTERM
) {
3257 if (decPrivateModeFlag
== true) {
3258 if (value
== true) {
3259 // Use private color registers for each sixel
3260 // graphic (default).
3261 sixelPalette
= null;
3263 // Use shared color registers for each sixel
3265 sixelPalette
= new HashMap
<Integer
, java
.awt
.Color
>();
3279 * DECSC - Save cursor.
3281 private void decsc() {
3282 savedState
.setTo(currentState
);
3286 * DECRC - Restore cursor.
3288 private void decrc() {
3289 currentState
.setTo(savedState
);
3295 private void ind() {
3296 // Move the cursor and scroll if necessary. If at the bottom line
3297 // already, a scroll up is supposed to be performed.
3298 if (currentState
.cursorY
== scrollRegionBottom
) {
3299 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3301 cursorDown(1, true);
3305 * RI - Reverse index.
3308 // Move the cursor and scroll if necessary. If at the top line
3309 // already, a scroll down is supposed to be performed.
3310 if (currentState
.cursorY
== scrollRegionTop
) {
3311 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3319 private void nel() {
3320 // Move the cursor and scroll if necessary. If at the bottom line
3321 // already, a scroll up is supposed to be performed.
3322 if (currentState
.cursorY
== scrollRegionBottom
) {
3323 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3325 cursorDown(1, true);
3327 // Reset to the beginning of the next line
3328 currentState
.cursorX
= 0;
3332 * DECKPAM - Keypad application mode.
3334 private void deckpam() {
3335 keypadMode
= KeypadMode
.Application
;
3339 * DECKPNM - Keypad numeric mode.
3341 private void deckpnm() {
3342 keypadMode
= KeypadMode
.Numeric
;
3348 * @param n number of spaces to move
3349 * @param honorScrollRegion if true, then do nothing if the cursor is
3350 * outside the scrolling region
3352 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3356 * Special case: if a user moves the cursor from the right margin, we
3357 * have to reset the VT100 right margin flag.
3360 wrapLineFlag
= false;
3363 for (int i
= 0; i
< n
; i
++) {
3364 if (honorScrollRegion
== true) {
3365 // Honor the scrolling region
3366 if ((currentState
.cursorY
< scrollRegionTop
)
3367 || (currentState
.cursorY
> scrollRegionBottom
)
3369 // Outside region, do nothing
3372 // Inside region, go up
3373 top
= scrollRegionTop
;
3375 // Non-scrolling case
3379 if (currentState
.cursorY
> top
) {
3380 currentState
.cursorY
--;
3386 * Move down n spaces.
3388 * @param n number of spaces to move
3389 * @param honorScrollRegion if true, then do nothing if the cursor is
3390 * outside the scrolling region
3392 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3396 * Special case: if a user moves the cursor from the right margin, we
3397 * have to reset the VT100 right margin flag.
3400 wrapLineFlag
= false;
3403 for (int i
= 0; i
< n
; i
++) {
3405 if (honorScrollRegion
== true) {
3406 // Honor the scrolling region
3407 if (currentState
.cursorY
> scrollRegionBottom
) {
3408 // Outside region, do nothing
3411 // Inside region, go down
3412 bottom
= scrollRegionBottom
;
3414 // Non-scrolling case
3415 bottom
= height
- 1;
3418 if (currentState
.cursorY
< bottom
) {
3419 currentState
.cursorY
++;
3425 * Move left n spaces.
3427 * @param n number of spaces to move
3428 * @param honorScrollRegion if true, then do nothing if the cursor is
3429 * outside the scrolling region
3431 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3433 * Special case: if a user moves the cursor from the right margin, we
3434 * have to reset the VT100 right margin flag.
3437 wrapLineFlag
= false;
3440 for (int i
= 0; i
< n
; i
++) {
3441 if (honorScrollRegion
== true) {
3442 // Honor the scrolling region
3443 if ((currentState
.cursorY
< scrollRegionTop
)
3444 || (currentState
.cursorY
> scrollRegionBottom
)
3446 // Outside region, do nothing
3451 if (currentState
.cursorX
> 0) {
3452 currentState
.cursorX
--;
3458 * Move right n spaces.
3460 * @param n number of spaces to move
3461 * @param honorScrollRegion if true, then do nothing if the cursor is
3462 * outside the scrolling region
3464 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3465 int rightMargin
= this.rightMargin
;
3468 * Special case: if a user moves the cursor from the right margin, we
3469 * have to reset the VT100 right margin flag.
3472 wrapLineFlag
= false;
3475 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3476 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3479 for (int i
= 0; i
< n
; i
++) {
3480 if (honorScrollRegion
== true) {
3481 // Honor the scrolling region
3482 if ((currentState
.cursorY
< scrollRegionTop
)
3483 || (currentState
.cursorY
> scrollRegionBottom
)
3485 // Outside region, do nothing
3490 if (currentState
.cursorX
< rightMargin
) {
3491 currentState
.cursorX
++;
3497 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3499 * @param row row to move to
3500 * @param col column to move to
3502 private void cursorPosition(int row
, final int col
) {
3503 int rightMargin
= this.rightMargin
;
3508 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3509 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3512 // Set column number
3513 currentState
.cursorX
= col
;
3515 // Sanity check, bring column back to margin.
3516 if (currentState
.cursorX
> rightMargin
) {
3517 currentState
.cursorX
= rightMargin
;
3521 if (currentState
.originMode
== true) {
3522 row
+= scrollRegionTop
;
3524 if (currentState
.cursorY
< row
) {
3525 cursorDown(row
- currentState
.cursorY
, false);
3526 } else if (currentState
.cursorY
> row
) {
3527 cursorUp(currentState
.cursorY
- row
, false);
3530 wrapLineFlag
= false;
3534 * HTS - Horizontal tabulation set.
3536 private void hts() {
3537 for (Integer stop
: tabStops
) {
3538 if (stop
== currentState
.cursorX
) {
3539 // Already have a tab stop here
3544 // Append a tab stop to the end of the array and resort them
3545 tabStops
.add(currentState
.cursorX
);
3546 Collections
.sort(tabStops
);
3550 * DECSWL - Single-width line.
3552 private void decswl() {
3553 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3554 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3558 * DECDWL - Double-width line.
3560 private void decdwl() {
3561 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3562 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3566 * DECHDL - Double-height + double-width line.
3568 * @param topHalf if true, this sets the row to be the top half row of a
3571 private void dechdl(final boolean topHalf
) {
3572 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3573 if (topHalf
== true) {
3574 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3576 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3581 * DECALN - Screen alignment display.
3583 private void decaln() {
3584 Cell newCell
= new Cell('E');
3585 for (DisplayLine line
: display
) {
3586 for (int i
= 0; i
< line
.length(); i
++) {
3587 line
.replace(i
, newCell
);
3593 * DECSCL - Compatibility level.
3595 private void decscl() {
3596 int i
= getCsiParam(0, 0);
3597 int j
= getCsiParam(1, 0);
3601 currentState
.g0Charset
= CharacterSet
.US
;
3602 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3604 } else if (i
== 62) {
3606 if ((j
== 0) || (j
== 2)) {
3608 } else if (j
== 1) {
3615 * CUD - Cursor down.
3617 private void cud() {
3618 cursorDown(getCsiParam(0, 1, 1, height
), true);
3622 * CUF - Cursor forward.
3624 private void cuf() {
3625 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3629 * CUB - Cursor backward.
3631 private void cub() {
3632 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3638 private void cuu() {
3639 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3643 * CUP - Cursor position.
3645 private void cup() {
3646 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3647 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3651 * CNL - Cursor down and to column 1.
3653 private void cnl() {
3654 cursorDown(getCsiParam(0, 1, 1, height
), true);
3656 cursorLeft(currentState
.cursorX
, true);
3660 * CPL - Cursor up and to column 1.
3662 private void cpl() {
3663 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3665 cursorLeft(currentState
.cursorX
, true);
3669 * CHA - Cursor to column # in current row.
3671 private void cha() {
3672 cursorPosition(currentState
.cursorY
,
3673 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3677 * VPA - Cursor to row #, same column.
3679 private void vpa() {
3680 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3681 currentState
.cursorX
);
3685 * ED - Erase in display.
3688 boolean honorProtected
= false;
3689 boolean decPrivateModeFlag
= false;
3691 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3692 if (collectBuffer
.charAt(i
) == '?') {
3693 decPrivateModeFlag
= true;
3698 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3699 && (decPrivateModeFlag
== true)
3701 honorProtected
= true;
3704 int i
= getCsiParam(0, 0);
3707 // Erase from here to end of screen
3708 if (currentState
.cursorY
< height
- 1) {
3709 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3712 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3713 } else if (i
== 1) {
3714 // Erase from beginning of screen to here
3715 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3717 eraseLine(0, currentState
.cursorX
, honorProtected
);
3718 } else if (i
== 2) {
3719 // Erase entire screen
3720 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3725 * EL - Erase in line.
3728 boolean honorProtected
= false;
3729 boolean decPrivateModeFlag
= false;
3731 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3732 if (collectBuffer
.charAt(i
) == '?') {
3733 decPrivateModeFlag
= true;
3738 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3739 && (decPrivateModeFlag
== true)
3741 honorProtected
= true;
3744 int i
= getCsiParam(0, 0);
3747 // Erase from here to end of line
3748 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3749 } else if (i
== 1) {
3750 // Erase from beginning of line to here
3751 eraseLine(0, currentState
.cursorX
, honorProtected
);
3752 } else if (i
== 2) {
3753 // Erase entire line
3754 eraseLine(0, width
- 1, honorProtected
);
3759 * ECH - Erase # of characters in current row.
3761 private void ech() {
3762 int i
= getCsiParam(0, 1, 1, width
);
3764 // Erase from here to i characters
3765 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3772 int i
= getCsiParam(0, 1);
3774 if ((currentState
.cursorY
>= scrollRegionTop
)
3775 && (currentState
.cursorY
<= scrollRegionBottom
)
3778 // I can get the same effect with a scroll-down
3779 scrollingRegionScrollDown(currentState
.cursorY
,
3780 scrollRegionBottom
, i
);
3785 * DCH - Delete char.
3787 private void dch() {
3788 int n
= getCsiParam(0, 1);
3789 DisplayLine line
= display
.get(currentState
.cursorY
);
3790 Cell blank
= new Cell();
3791 for (int i
= 0; i
< n
; i
++) {
3792 line
.delete(currentState
.cursorX
, blank
);
3797 * ICH - Insert blank char at cursor.
3799 private void ich() {
3800 int n
= getCsiParam(0, 1);
3801 DisplayLine line
= display
.get(currentState
.cursorY
);
3802 Cell blank
= new Cell();
3803 for (int i
= 0; i
< n
; i
++) {
3804 line
.insert(currentState
.cursorX
, blank
);
3812 int i
= getCsiParam(0, 1);
3814 if ((currentState
.cursorY
>= scrollRegionTop
)
3815 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3817 // I can get the same effect with a scroll-down
3818 scrollingRegionScrollUp(currentState
.cursorY
,
3819 scrollRegionBottom
, i
);
3824 * HVP - Horizontal and vertical position.
3826 private void hvp() {
3831 * REP - Repeat character.
3833 private void rep() {
3834 int n
= getCsiParam(0, 1);
3835 for (int i
= 0; i
< n
; i
++) {
3836 printCharacter(repCh
);
3844 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3845 getCsiParam(0, 1, 1, height
));
3852 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3853 getCsiParam(0, 1, 1, height
));
3857 * CBT - Go back X tab stops.
3859 private void cbt() {
3860 int tabsToMove
= getCsiParam(0, 1);
3863 for (int i
= 0; i
< tabsToMove
; i
++) {
3864 int j
= currentState
.cursorX
;
3865 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3866 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3874 j
= tabStops
.get(tabI
);
3876 cursorPosition(currentState
.cursorY
, j
);
3881 * CHT - Advance X tab stops.
3883 private void cht() {
3884 int n
= getCsiParam(0, 1);
3885 for (int i
= 0; i
< n
; i
++) {
3886 advanceToNextTabStop();
3891 * SGR - Select graphics rendition.
3893 private void sgr() {
3895 if (csiParams
.size() == 0) {
3896 currentState
.attr
.reset();
3900 int sgrColorMode
= -1;
3901 boolean idx88Color
= false;
3902 boolean rgbColor
= false;
3906 for (Integer i
: csiParams
) {
3908 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
3910 assert (type
== DeviceType
.XTERM
);
3914 * Indexed color mode, we now have the index number.
3916 if (sgrColorMode
== 38) {
3917 currentState
.attr
.setForeColorRGB(get88Color(i
));
3919 assert (sgrColorMode
== 48);
3920 currentState
.attr
.setBackColorRGB(get88Color(i
));
3929 * RGB color mode, we are collecting tokens.
3933 } else if (rgbGreen
== -1) {
3934 rgbGreen
= i
& 0xFF;
3936 int rgb
= rgbRed
<< 16;
3937 rgb
|= rgbGreen
<< 8;
3940 // System.err.printf("RGB: %08x\n", rgb);
3942 if (sgrColorMode
== 38) {
3943 currentState
.attr
.setForeColorRGB(rgb
);
3945 assert (sgrColorMode
== 48);
3946 currentState
.attr
.setBackColorRGB(rgb
);
3967 * Indexed color mode.
3974 * This is neither indexed nor RGB color. Bail out.
3979 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
3985 currentState
.attr
.reset();
3990 currentState
.attr
.setBold(true);
3995 currentState
.attr
.setUnderline(true);
4000 currentState
.attr
.setBlink(true);
4005 currentState
.attr
.setReverse(true);
4012 if (type
== DeviceType
.XTERM
) {
4022 // Set black foreground
4023 currentState
.attr
.setForeColorRGB(get88Color(8));
4026 // Set red foreground
4027 currentState
.attr
.setForeColorRGB(get88Color(9));
4030 // Set green foreground
4031 currentState
.attr
.setForeColorRGB(get88Color(10));
4034 // Set yellow foreground
4035 currentState
.attr
.setForeColorRGB(get88Color(11));
4038 // Set blue foreground
4039 currentState
.attr
.setForeColorRGB(get88Color(12));
4042 // Set magenta foreground
4043 currentState
.attr
.setForeColorRGB(get88Color(13));
4046 // Set cyan foreground
4047 currentState
.attr
.setForeColorRGB(get88Color(14));
4050 // Set white foreground
4051 currentState
.attr
.setForeColorRGB(get88Color(15));
4055 // Set black background
4056 currentState
.attr
.setBackColorRGB(get88Color(8));
4059 // Set red background
4060 currentState
.attr
.setBackColorRGB(get88Color(9));
4063 // Set green background
4064 currentState
.attr
.setBackColorRGB(get88Color(10));
4067 // Set yellow background
4068 currentState
.attr
.setBackColorRGB(get88Color(11));
4071 // Set blue background
4072 currentState
.attr
.setBackColorRGB(get88Color(12));
4075 // Set magenta background
4076 currentState
.attr
.setBackColorRGB(get88Color(13));
4079 // Set cyan background
4080 currentState
.attr
.setBackColorRGB(get88Color(14));
4083 // Set white background
4084 currentState
.attr
.setBackColorRGB(get88Color(15));
4092 if ((type
== DeviceType
.VT220
)
4093 || (type
== DeviceType
.XTERM
)) {
4099 currentState
.attr
.setBold(false);
4104 currentState
.attr
.setUnderline(false);
4109 currentState
.attr
.setBlink(false);
4114 currentState
.attr
.setReverse(false);
4122 // A true VT100/102/220 does not support color, however everyone
4123 // is used to their terminal emulator supporting color so we will
4124 // unconditionally support color for all DeviceType's.
4129 // Set black foreground
4130 currentState
.attr
.setForeColor(Color
.BLACK
);
4131 currentState
.attr
.setForeColorRGB(-1);
4134 // Set red foreground
4135 currentState
.attr
.setForeColor(Color
.RED
);
4136 currentState
.attr
.setForeColorRGB(-1);
4139 // Set green foreground
4140 currentState
.attr
.setForeColor(Color
.GREEN
);
4141 currentState
.attr
.setForeColorRGB(-1);
4144 // Set yellow foreground
4145 currentState
.attr
.setForeColor(Color
.YELLOW
);
4146 currentState
.attr
.setForeColorRGB(-1);
4149 // Set blue foreground
4150 currentState
.attr
.setForeColor(Color
.BLUE
);
4151 currentState
.attr
.setForeColorRGB(-1);
4154 // Set magenta foreground
4155 currentState
.attr
.setForeColor(Color
.MAGENTA
);
4156 currentState
.attr
.setForeColorRGB(-1);
4159 // Set cyan foreground
4160 currentState
.attr
.setForeColor(Color
.CYAN
);
4161 currentState
.attr
.setForeColorRGB(-1);
4164 // Set white foreground
4165 currentState
.attr
.setForeColor(Color
.WHITE
);
4166 currentState
.attr
.setForeColorRGB(-1);
4169 if (type
== DeviceType
.XTERM
) {
4171 * Xterm supports T.416 / ISO-8613-3 codes to select
4172 * either an indexed color or an RGB value. (It also
4173 * permits these ISO-8613-3 SGR sequences to be separated
4174 * by colons rather than semicolons.)
4176 * We will support only the following:
4178 * 1. Indexed color mode (88- or 256-color modes).
4182 * These cover most of the use cases in the real world.
4184 * HOWEVER, note that this is an awful broken "standard",
4185 * with no way to do it "right". See
4186 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4187 * for a detailed discussion of the current state of RGB
4188 * in various terminals, the point of which is that none
4189 * of them really do the same thing despite all appearing
4193 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4194 * where it is assumed that supporting just the "indexed
4195 * mode" of these sequences (which could align easily
4196 * with existing SGR colors) is assumed to mean full
4197 * support of 24-bit RGB. So it is all or nothing.
4199 * Finally, these sequences break the assumptions of
4200 * standard ECMA-48 style parsers as pointed out at
4201 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4202 * Therefore in order to keep a clean display, we cannot
4203 * parse anything else in this sequence.
4208 // Underscore on, default foreground color
4209 currentState
.attr
.setUnderline(true);
4210 currentState
.attr
.setForeColor(Color
.WHITE
);
4214 // Underscore off, default foreground color
4215 currentState
.attr
.setUnderline(false);
4216 currentState
.attr
.setForeColor(Color
.WHITE
);
4217 currentState
.attr
.setForeColorRGB(-1);
4220 // Set black background
4221 currentState
.attr
.setBackColor(Color
.BLACK
);
4222 currentState
.attr
.setBackColorRGB(-1);
4225 // Set red background
4226 currentState
.attr
.setBackColor(Color
.RED
);
4227 currentState
.attr
.setBackColorRGB(-1);
4230 // Set green background
4231 currentState
.attr
.setBackColor(Color
.GREEN
);
4232 currentState
.attr
.setBackColorRGB(-1);
4235 // Set yellow background
4236 currentState
.attr
.setBackColor(Color
.YELLOW
);
4237 currentState
.attr
.setBackColorRGB(-1);
4240 // Set blue background
4241 currentState
.attr
.setBackColor(Color
.BLUE
);
4242 currentState
.attr
.setBackColorRGB(-1);
4245 // Set magenta background
4246 currentState
.attr
.setBackColor(Color
.MAGENTA
);
4247 currentState
.attr
.setBackColorRGB(-1);
4250 // Set cyan background
4251 currentState
.attr
.setBackColor(Color
.CYAN
);
4252 currentState
.attr
.setBackColorRGB(-1);
4255 // Set white background
4256 currentState
.attr
.setBackColor(Color
.WHITE
);
4257 currentState
.attr
.setBackColorRGB(-1);
4260 if (type
== DeviceType
.XTERM
) {
4262 * Xterm supports T.416 / ISO-8613-3 codes to select
4263 * either an indexed color or an RGB value. (It also
4264 * permits these ISO-8613-3 SGR sequences to be separated
4265 * by colons rather than semicolons.)
4267 * We will support only the following:
4269 * 1. Indexed color mode (88- or 256-color modes).
4273 * These cover most of the use cases in the real world.
4275 * HOWEVER, note that this is an awful broken "standard",
4276 * with no way to do it "right". See
4277 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4278 * for a detailed discussion of the current state of RGB
4279 * in various terminals, the point of which is that none
4280 * of them really do the same thing despite all appearing
4284 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4285 * where it is assumed that supporting just the "indexed
4286 * mode" of these sequences (which could align easily
4287 * with existing SGR colors) is assumed to mean full
4288 * support of 24-bit RGB. So it is all or nothing.
4290 * Finally, these sequences break the assumptions of
4291 * standard ECMA-48 style parsers as pointed out at
4292 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4293 * Therefore in order to keep a clean display, we cannot
4294 * parse anything else in this sequence.
4301 // Default background
4302 currentState
.attr
.setBackColor(Color
.BLACK
);
4303 currentState
.attr
.setBackColorRGB(-1);
4313 * DA - Device attributes.
4316 int extendedFlag
= 0;
4318 if (collectBuffer
.length() > 0) {
4319 String args
= collectBuffer
.substring(1);
4320 if (collectBuffer
.charAt(0) == '>') {
4322 if (collectBuffer
.length() >= 2) {
4323 i
= Integer
.parseInt(args
);
4325 } else if (collectBuffer
.charAt(0) == '=') {
4327 if (collectBuffer
.length() >= 2) {
4328 i
= Integer
.parseInt(args
);
4331 // Unknown code, bail out
4336 if ((i
!= 0) && (i
!= 1)) {
4340 if ((extendedFlag
== 0) && (i
== 0)) {
4341 // Send string directly to remote side
4342 writeRemote(deviceTypeResponse());
4346 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4348 if ((extendedFlag
== 1) && (i
== 0)) {
4350 * Request "What type of terminal are you, what is your
4351 * firmware version, and what hardware options do you have
4354 * Respond: "I am a VT220 (identification code of 1), my
4355 * firmware version is _____ (Pv), and I have _____ Po
4356 * options installed."
4362 if (s8c1t
== true) {
4363 writeRemote("\u009b>1;10;0c");
4365 writeRemote("\033[>1;10;0c");
4371 if ((extendedFlag
== 2) && (i
== 0)) {
4374 * Request "What is your unit ID?"
4376 * Respond: "I was manufactured at site 00 and have a unique ID
4380 writeRemote("\033P!|00010203\033\\");
4385 * DECSTBM - Set top and bottom margins.
4387 private void decstbm() {
4388 boolean decPrivateModeFlag
= false;
4390 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4391 if (collectBuffer
.charAt(i
) == '?') {
4392 decPrivateModeFlag
= true;
4396 if (decPrivateModeFlag
) {
4397 // This could be restore DEC private mode values.
4401 int top
= getCsiParam(0, 1, 1, height
) - 1;
4402 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4407 scrollRegionTop
= top
;
4408 scrollRegionBottom
= bottom
;
4411 cursorPosition(0, 0);
4416 * DECREQTPARM - Request terminal parameters.
4418 private void decreqtparm() {
4419 int i
= getCsiParam(0, 0);
4421 if ((i
!= 0) && (i
!= 1)) {
4428 * Request terminal parameters.
4432 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4433 * (CLoCk MULtiplier = 1, STP option flags = 0)
4437 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4440 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4442 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4448 * DECSCA - Select Character Attributes.
4450 private void decsca() {
4451 int i
= getCsiParam(0, 0);
4453 if ((i
== 0) || (i
== 2)) {
4455 currentState
.attr
.setProtect(false);
4459 currentState
.attr
.setProtect(true);
4464 * DECSTR - Soft Terminal Reset.
4466 private void decstr() {
4467 // Do exactly like RIS - Reset to initial state
4469 // Do I clear screen too? I think so...
4470 eraseScreen(0, 0, height
- 1, width
- 1, false);
4471 cursorPosition(0, 0);
4475 * DSR - Device status report.
4477 private void dsr() {
4478 boolean decPrivateModeFlag
= false;
4479 int row
= currentState
.cursorY
;
4481 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4482 if (collectBuffer
.charAt(i
) == '?') {
4483 decPrivateModeFlag
= true;
4488 int i
= getCsiParam(0, 0);
4493 // Request status report. Respond with "OK, no malfunction."
4495 // Send string directly to remote side
4496 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4499 writeRemote("\u009b0n");
4501 writeRemote("\033[0n");
4506 // Request cursor position. Respond with current position.
4507 if (currentState
.originMode
== true) {
4508 row
-= scrollRegionTop
;
4511 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4514 str
= String
.format("\u009b%d;%dR", row
+ 1,
4515 currentState
.cursorX
+ 1);
4517 str
= String
.format("\033[%d;%dR", row
+ 1,
4518 currentState
.cursorX
+ 1);
4521 // Send string directly to remote side
4526 if (decPrivateModeFlag
== true) {
4528 // Request printer status report. Respond with "Printer not
4531 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4532 && (s8c1t
== true)) {
4533 writeRemote("\u009b?13n");
4535 writeRemote("\033[?13n");
4541 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4542 && (decPrivateModeFlag
== true)
4545 // Request user-defined keys are locked or unlocked. Respond
4546 // with "User-defined keys are locked."
4548 if (s8c1t
== true) {
4549 writeRemote("\u009b?21n");
4551 writeRemote("\033[?21n");
4557 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4558 && (decPrivateModeFlag
== true)
4561 // Request keyboard language. Respond with "Keyboard
4562 // language is North American."
4564 if (s8c1t
== true) {
4565 writeRemote("\u009b?27;1n");
4567 writeRemote("\033[?27;1n");
4574 // Some other option, ignore
4580 * TBC - Tabulation clear.
4582 private void tbc() {
4583 int i
= getCsiParam(0, 0);
4585 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4586 for (Integer stop
: tabStops
) {
4587 if (stop
== currentState
.cursorX
) {
4592 tabStops
= newStops
;
4600 * Erase the characters in the current line from the start column to the
4601 * end column, inclusive.
4603 * @param start starting column to erase (between 0 and width - 1)
4604 * @param end ending column to erase (between 0 and width - 1)
4605 * @param honorProtected if true, do not erase characters with the
4606 * protected attribute set
4608 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4613 if (end
> width
- 1) {
4620 for (int i
= start
; i
<= end
; i
++) {
4621 DisplayLine line
= display
.get(currentState
.cursorY
);
4622 if ((!honorProtected
)
4623 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4630 * From the VT102 manual:
4632 * Erasing a character also erases any character
4633 * attribute of the character.
4639 * Erase with the current color a.k.a. back-color erase
4642 line
.setChar(i
, ' ');
4643 line
.setAttr(i
, currentState
.attr
);
4651 * Erase a rectangular section of the screen, inclusive. end column,
4654 * @param startRow starting row to erase (between 0 and height - 1)
4655 * @param startCol starting column to erase (between 0 and width - 1)
4656 * @param endRow ending row to erase (between 0 and height - 1)
4657 * @param endCol ending column to erase (between 0 and width - 1)
4658 * @param honorProtected if true, do not erase characters with the
4659 * protected attribute set
4661 private void eraseScreen(final int startRow
, final int startCol
,
4662 final int endRow
, final int endCol
, final boolean honorProtected
) {
4670 || (endRow
< startRow
)
4671 || (endCol
< startCol
)
4676 oldCursorY
= currentState
.cursorY
;
4677 for (int i
= startRow
; i
<= endRow
; i
++) {
4678 currentState
.cursorY
= i
;
4679 eraseLine(startCol
, endCol
, honorProtected
);
4681 // Erase display clears the double attributes
4682 display
.get(i
).setDoubleWidth(false);
4683 display
.get(i
).setDoubleHeight(0);
4685 currentState
.cursorY
= oldCursorY
;
4689 * VT220 printer functions. All of these are parsed, but won't do
4692 private void printerFunctions() {
4693 boolean decPrivateModeFlag
= false;
4694 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4695 if (collectBuffer
.charAt(i
) == '?') {
4696 decPrivateModeFlag
= true;
4701 int i
= getCsiParam(0, 0);
4706 if (decPrivateModeFlag
== false) {
4712 if (decPrivateModeFlag
== true) {
4713 // Print cursor line
4718 if (decPrivateModeFlag
== true) {
4719 // Auto print mode OFF
4721 // Printer controller OFF
4723 // Characters re-appear on the screen
4724 printerControllerMode
= false;
4729 if (decPrivateModeFlag
== true) {
4733 // Printer controller
4735 // Characters get sucked into oblivion
4736 printerControllerMode
= true;
4747 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4748 * of remote systems will send an XTerm title sequence even if TERM isn't
4751 * @param xtermChar the character received from the remote side
4753 private void oscPut(final char xtermChar
) {
4754 // System.err.println("oscPut: " + xtermChar);
4757 collectBuffer
.append(xtermChar
);
4760 if ((xtermChar
== 0x07)
4761 || (collectBuffer
.toString().endsWith("\033\\"))
4764 if (xtermChar
== 0x07) {
4765 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
4767 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4770 String
[] p
= args
.split(";");
4772 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4779 if (p
[0].equals("4")) {
4780 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
4781 // Set a color index value
4783 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
4784 } catch (NumberFormatException e
) {
4790 if (p
[0].equals("10")) {
4791 if (p
[1].equals("?")) {
4792 // Respond with foreground color.
4793 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToForegroundColor(currentState
.attr
);
4795 writeRemote(String
.format(
4796 "\033]10;rgb:%04x/%04x/%04x\033\\",
4797 color
.getRed() << 8,
4798 color
.getGreen() << 8,
4799 color
.getBlue() << 8));
4803 if (p
[0].equals("11")) {
4804 if (p
[1].equals("?")) {
4805 // Respond with background color.
4806 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToBackgroundColor(currentState
.attr
);
4808 writeRemote(String
.format(
4809 "\033]11;rgb:%04x/%04x/%04x\033\\",
4810 color
.getRed() << 8,
4811 color
.getGreen() << 8,
4812 color
.getBlue() << 8));
4816 if (p
[0].equals("444")) {
4817 if (p
[1].equals("0") && (p
.length
== 6)) {
4818 // Jexer image - RGB
4819 parseJexerImageRGB(p
[2], p
[3], p
[4], p
[5]);
4820 } else if (p
[1].equals("1") && (p
.length
== 4)) {
4821 // Jexer image - PNG
4822 parseJexerImageFile(1, p
[2], p
[3]);
4823 } else if (p
[1].equals("2") && (p
.length
== 4)) {
4824 // Jexer image - JPG
4825 parseJexerImageFile(2, p
[2], p
[3]);
4830 // Go to SCAN_GROUND state
4837 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
4838 * Jexer ECMA48Terminal to talk to ECMA48.
4840 * @param pmChar the character received from the remote side
4842 private void pmPut(final char pmChar
) {
4843 // System.err.println("pmPut: " + pmChar);
4846 collectBuffer
.append(pmChar
);
4849 if (collectBuffer
.toString().endsWith("\033\\")) {
4851 arg
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4853 // System.err.println("arg: '" + arg + "'");
4855 if (arg
.equals("hideMousePointer")) {
4856 hideMousePointer
= true;
4858 if (arg
.equals("showMousePointer")) {
4859 hideMousePointer
= false;
4862 // Go to SCAN_GROUND state
4869 * Perform xterm window operations.
4871 private void xtermWindowOps() {
4872 boolean xtermPrivateModeFlag
= false;
4874 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4875 if (collectBuffer
.charAt(i
) == '?') {
4876 xtermPrivateModeFlag
= true;
4881 int i
= getCsiParam(0, 0);
4883 if (!xtermPrivateModeFlag
) {
4886 // Report xterm text area size in pixels as CSI 4 ; height ;
4888 writeRemote(String
.format("\033[4;%d;%dt", textHeight
* height
,
4889 textWidth
* width
));
4892 // Report character size in pixels as CSI 6 ; height ; width
4894 writeRemote(String
.format("\033[6;%d;%dt", textHeight
,
4898 // Report the text are size in characters as CSI 8 ; height ;
4900 writeRemote(String
.format("\033[8;%d;%dt", height
, width
));
4909 * Respond to xterm sixel query.
4911 private void xtermSixelQuery() {
4912 int item
= getCsiParam(0, 0);
4913 int action
= getCsiParam(1, 0);
4914 int value
= getCsiParam(2, 0);
4919 // Report number of color registers.
4920 writeRemote(String
.format("\033[?%d;%d;%dS", item
, 0, 1024));
4927 // We will not support this option.
4928 writeRemote(String
.format("\033[?%d;%dS", item
, action
));
4932 * Run this input character through the ECMA48 state machine.
4934 * @param ch character from the remote side
4936 private void consume(final int ch
) {
4939 // System.err.printf("%c STATE = %s\n", ch, scanState);
4941 // Special "anywhere" states
4943 // 18, 1A --> execute, then switch to SCAN_GROUND
4944 if ((ch
== 0x18) || (ch
== 0x1A)) {
4945 // CAN and SUB abort escape sequences
4950 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4954 if ((type
== DeviceType
.XTERM
)
4955 && ((scanState
== ScanState
.OSC_STRING
)
4956 || (scanState
== ScanState
.DCS_SIXEL
)
4957 || (scanState
== ScanState
.SOSPMAPC_STRING
))
4959 // Xterm can pass ESCAPE to its OSC sequence.
4960 // Xterm can pass ESCAPE to its DCS sequence.
4961 // Jexer can pass ESCAPE to its PM sequence.
4962 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
4963 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4964 && (scanState
!= ScanState
.DCS_IGNORE
)
4965 && (scanState
!= ScanState
.DCS_PARAM
)
4966 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4968 scanState
= ScanState
.ESCAPE
;
4973 // 0x9B == CSI 8-bit sequence
4975 scanState
= ScanState
.CSI_ENTRY
;
4979 // 0x9D goes to ScanState.OSC_STRING
4981 scanState
= ScanState
.OSC_STRING
;
4985 // 0x90 goes to DCS_ENTRY
4987 scanState
= ScanState
.DCS_ENTRY
;
4991 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4992 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4993 scanState
= ScanState
.SOSPMAPC_STRING
;
4997 // 0x7F (DEL) is always discarded
5002 switch (scanState
) {
5005 // 00-17, 19, 1C-1F --> execute
5006 // 80-8F, 91-9A, 9C --> execute
5007 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
5008 handleControlChar((char) ch
);
5012 if (((ch
>= 0x20) && (ch
<= 0x7F))
5016 // VT220 printer --> trash bin
5017 if (((type
== DeviceType
.VT220
)
5018 || (type
== DeviceType
.XTERM
))
5019 && (printerControllerMode
== true)
5024 // Hang onto this character
5025 repCh
= mapCharacter(ch
);
5027 // Print this character
5028 printCharacter(repCh
);
5033 // 00-17, 19, 1C-1F --> execute
5035 handleControlChar((char) ch
);
5039 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
5040 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5042 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
5046 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
5047 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
5058 // DECSC - Save cursor
5059 // Note this code overlaps both ANSI and VT52 mode
5064 // DECRC - Restore cursor
5065 // Note this code overlaps both ANSI and VT52 mode
5074 if (vt52Mode
== true) {
5075 // DECANM - Enter ANSI mode
5077 arrowKeyMode
= ArrowKeyMode
.VT100
;
5080 * From the VT102 docs: "You use ANSI mode to select
5081 * most terminal features; the terminal uses the same
5082 * features when it switches to VT52 mode. You
5083 * cannot, however, change most of these features in
5086 * In other words, do not reset any other attributes
5087 * when switching between VT52 submode and ANSI.
5091 currentState
.g0Charset
= CharacterSet
.US
;
5092 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5094 singleshift
= Singleshift
.NONE
;
5095 currentState
.glLockshift
= LockshiftMode
.NONE
;
5096 currentState
.grLockshift
= LockshiftMode
.NONE
;
5100 // DECKPAM - Keypad application mode
5101 // Note this code overlaps both ANSI and VT52 mode
5105 // DECKPNM - Keypad numeric mode
5106 // Note this code overlaps both ANSI and VT52 mode
5113 if (vt52Mode
== true) {
5114 // Cursor up, and stop at the top without scrolling
5119 if (vt52Mode
== true) {
5120 // Cursor down, and stop at the bottom without scrolling
5121 cursorDown(1, false);
5125 if (vt52Mode
== true) {
5126 // Cursor right, and stop at the right without scrolling
5127 cursorRight(1, false);
5131 if (vt52Mode
== true) {
5132 // Cursor left, and stop at the left without scrolling
5133 cursorLeft(1, false);
5140 if (vt52Mode
== true) {
5148 if (vt52Mode
== true) {
5149 // G0 --> Special graphics
5150 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
5154 if (vt52Mode
== true) {
5156 currentState
.g0Charset
= CharacterSet
.US
;
5160 if (vt52Mode
== true) {
5162 cursorPosition(0, 0);
5164 // HTS - Horizontal tabulation set
5169 if (vt52Mode
== true) {
5170 // Reverse line feed. Same as RI.
5175 if (vt52Mode
== true) {
5176 // Erase to end of screen
5177 eraseLine(currentState
.cursorX
, width
- 1, false);
5178 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
5183 if (vt52Mode
== true) {
5184 // Erase to end of line
5185 eraseLine(currentState
.cursorX
, width
- 1, false);
5191 if (vt52Mode
== true) {
5194 // RI - Reverse index
5199 if (vt52Mode
== false) {
5201 singleshift
= Singleshift
.SS2
;
5205 if (vt52Mode
== false) {
5207 singleshift
= Singleshift
.SS3
;
5214 if ((ch
>= 0x51) && (ch
<= 0x57)) {
5230 if (vt52Mode
== true) {
5231 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
5239 if (vt52Mode
== true) {
5241 // Send string directly to remote side
5242 writeRemote("\033/Z");
5245 // Send string directly to remote side
5246 writeRemote(deviceTypeResponse());
5257 // VT52 cannot get to any of these other states
5258 if (vt52Mode
== true) {
5263 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
5270 // RIS - Reset to initial state
5272 // Do I clear screen too? I think so...
5273 eraseScreen(0, 0, height
- 1, width
- 1, false);
5274 cursorPosition(0, 0);
5288 if ((type
== DeviceType
.VT220
)
5289 || (type
== DeviceType
.XTERM
)) {
5291 // VT220 lockshift G2 into GL
5292 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
5297 if ((type
== DeviceType
.VT220
)
5298 || (type
== DeviceType
.XTERM
)) {
5300 // VT220 lockshift G3 into GL
5301 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
5319 if ((type
== DeviceType
.VT220
)
5320 || (type
== DeviceType
.XTERM
)) {
5322 // VT220 lockshift G3 into GR
5323 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
5328 if ((type
== DeviceType
.VT220
)
5329 || (type
== DeviceType
.XTERM
)) {
5331 // VT220 lockshift G2 into GR
5332 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
5338 if ((type
== DeviceType
.VT220
)
5339 || (type
== DeviceType
.XTERM
)) {
5341 // VT220 lockshift G1 into GR
5342 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
5352 // 0x5B goes to CSI_ENTRY
5354 scanState
= ScanState
.CSI_ENTRY
;
5357 // 0x5D goes to OSC_STRING
5359 scanState
= ScanState
.OSC_STRING
;
5362 // 0x50 goes to DCS_ENTRY
5364 scanState
= ScanState
.DCS_ENTRY
;
5367 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5368 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
5369 scanState
= ScanState
.SOSPMAPC_STRING
;
5374 case ESCAPE_INTERMEDIATE
:
5375 // 00-17, 19, 1C-1F --> execute
5377 handleControlChar((char) ch
);
5380 // 20-2F --> collect
5381 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5385 // 30-7E --> dispatch, then switch to GROUND
5386 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
5389 if ((collectBuffer
.length() == 1)
5390 && (collectBuffer
.charAt(0) == '(')) {
5391 // G0 --> Special graphics
5392 currentState
.g0Charset
= CharacterSet
.DRAWING
;
5394 if ((collectBuffer
.length() == 1)
5395 && (collectBuffer
.charAt(0) == ')')) {
5396 // G1 --> Special graphics
5397 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5399 if ((type
== DeviceType
.VT220
)
5400 || (type
== DeviceType
.XTERM
)) {
5402 if ((collectBuffer
.length() == 1)
5403 && (collectBuffer
.charAt(0) == '*')) {
5404 // G2 --> Special graphics
5405 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5407 if ((collectBuffer
.length() == 1)
5408 && (collectBuffer
.charAt(0) == '+')) {
5409 // G3 --> Special graphics
5410 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5415 if ((collectBuffer
.length() == 1)
5416 && (collectBuffer
.charAt(0) == '(')) {
5417 // G0 --> Alternate character ROM standard character set
5418 currentState
.g0Charset
= CharacterSet
.ROM
;
5420 if ((collectBuffer
.length() == 1)
5421 && (collectBuffer
.charAt(0) == ')')) {
5422 // G1 --> Alternate character ROM standard character set
5423 currentState
.g1Charset
= CharacterSet
.ROM
;
5427 if ((collectBuffer
.length() == 1)
5428 && (collectBuffer
.charAt(0) == '(')) {
5429 // G0 --> Alternate character ROM special graphics
5430 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5432 if ((collectBuffer
.length() == 1)
5433 && (collectBuffer
.charAt(0) == ')')) {
5434 // G1 --> Alternate character ROM special graphics
5435 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5439 if ((collectBuffer
.length() == 1)
5440 && (collectBuffer
.charAt(0) == '#')) {
5441 // DECDHL - Double-height line (top half)
5446 if ((collectBuffer
.length() == 1)
5447 && (collectBuffer
.charAt(0) == '#')) {
5448 // DECDHL - Double-height line (bottom half)
5451 if ((type
== DeviceType
.VT220
)
5452 || (type
== DeviceType
.XTERM
)) {
5454 if ((collectBuffer
.length() == 1)
5455 && (collectBuffer
.charAt(0) == '(')) {
5457 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5459 if ((collectBuffer
.length() == 1)
5460 && (collectBuffer
.charAt(0) == ')')) {
5462 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5464 if ((collectBuffer
.length() == 1)
5465 && (collectBuffer
.charAt(0) == '*')) {
5467 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5469 if ((collectBuffer
.length() == 1)
5470 && (collectBuffer
.charAt(0) == '+')) {
5472 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5477 if ((collectBuffer
.length() == 1)
5478 && (collectBuffer
.charAt(0) == '#')) {
5479 // DECSWL - Single-width line
5482 if ((type
== DeviceType
.VT220
)
5483 || (type
== DeviceType
.XTERM
)) {
5485 if ((collectBuffer
.length() == 1)
5486 && (collectBuffer
.charAt(0) == '(')) {
5488 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5490 if ((collectBuffer
.length() == 1)
5491 && (collectBuffer
.charAt(0) == ')')) {
5493 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5495 if ((collectBuffer
.length() == 1)
5496 && (collectBuffer
.charAt(0) == '*')) {
5498 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5500 if ((collectBuffer
.length() == 1)
5501 && (collectBuffer
.charAt(0) == '+')) {
5503 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5508 if ((collectBuffer
.length() == 1)
5509 && (collectBuffer
.charAt(0) == '#')) {
5510 // DECDWL - Double-width line
5513 if ((type
== DeviceType
.VT220
)
5514 || (type
== DeviceType
.XTERM
)) {
5516 if ((collectBuffer
.length() == 1)
5517 && (collectBuffer
.charAt(0) == '(')) {
5519 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5521 if ((collectBuffer
.length() == 1)
5522 && (collectBuffer
.charAt(0) == ')')) {
5524 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5526 if ((collectBuffer
.length() == 1)
5527 && (collectBuffer
.charAt(0) == '*')) {
5529 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5531 if ((collectBuffer
.length() == 1)
5532 && (collectBuffer
.charAt(0) == '+')) {
5534 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5539 if ((type
== DeviceType
.VT220
)
5540 || (type
== DeviceType
.XTERM
)) {
5542 if ((collectBuffer
.length() == 1)
5543 && (collectBuffer
.charAt(0) == '(')) {
5545 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5547 if ((collectBuffer
.length() == 1)
5548 && (collectBuffer
.charAt(0) == ')')) {
5550 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5552 if ((collectBuffer
.length() == 1)
5553 && (collectBuffer
.charAt(0) == '*')) {
5555 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5557 if ((collectBuffer
.length() == 1)
5558 && (collectBuffer
.charAt(0) == '+')) {
5560 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5565 if ((collectBuffer
.length() == 1)
5566 && (collectBuffer
.charAt(0) == '#')) {
5567 // DECALN - Screen alignment display
5576 if ((type
== DeviceType
.VT220
)
5577 || (type
== DeviceType
.XTERM
)) {
5579 if ((collectBuffer
.length() == 1)
5580 && (collectBuffer
.charAt(0) == '(')) {
5581 // G0 --> DEC_SUPPLEMENTAL
5582 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5584 if ((collectBuffer
.length() == 1)
5585 && (collectBuffer
.charAt(0) == ')')) {
5586 // G1 --> DEC_SUPPLEMENTAL
5587 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5589 if ((collectBuffer
.length() == 1)
5590 && (collectBuffer
.charAt(0) == '*')) {
5591 // G2 --> DEC_SUPPLEMENTAL
5592 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5594 if ((collectBuffer
.length() == 1)
5595 && (collectBuffer
.charAt(0) == '+')) {
5596 // G3 --> DEC_SUPPLEMENTAL
5597 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5602 if ((type
== DeviceType
.VT220
)
5603 || (type
== DeviceType
.XTERM
)) {
5605 if ((collectBuffer
.length() == 1)
5606 && (collectBuffer
.charAt(0) == '(')) {
5608 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5610 if ((collectBuffer
.length() == 1)
5611 && (collectBuffer
.charAt(0) == ')')) {
5613 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5615 if ((collectBuffer
.length() == 1)
5616 && (collectBuffer
.charAt(0) == '*')) {
5618 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5620 if ((collectBuffer
.length() == 1)
5621 && (collectBuffer
.charAt(0) == '+')) {
5623 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5632 if ((collectBuffer
.length() == 1)
5633 && (collectBuffer
.charAt(0) == '(')) {
5634 // G0 --> United Kingdom set
5635 currentState
.g0Charset
= CharacterSet
.UK
;
5637 if ((collectBuffer
.length() == 1)
5638 && (collectBuffer
.charAt(0) == ')')) {
5639 // G1 --> United Kingdom set
5640 currentState
.g1Charset
= CharacterSet
.UK
;
5642 if ((type
== DeviceType
.VT220
)
5643 || (type
== DeviceType
.XTERM
)) {
5645 if ((collectBuffer
.length() == 1)
5646 && (collectBuffer
.charAt(0) == '*')) {
5647 // G2 --> United Kingdom set
5648 currentState
.g2Charset
= CharacterSet
.UK
;
5650 if ((collectBuffer
.length() == 1)
5651 && (collectBuffer
.charAt(0) == '+')) {
5652 // G3 --> United Kingdom set
5653 currentState
.g3Charset
= CharacterSet
.UK
;
5658 if ((collectBuffer
.length() == 1)
5659 && (collectBuffer
.charAt(0) == '(')) {
5661 currentState
.g0Charset
= CharacterSet
.US
;
5663 if ((collectBuffer
.length() == 1)
5664 && (collectBuffer
.charAt(0) == ')')) {
5666 currentState
.g1Charset
= CharacterSet
.US
;
5668 if ((type
== DeviceType
.VT220
)
5669 || (type
== DeviceType
.XTERM
)) {
5671 if ((collectBuffer
.length() == 1)
5672 && (collectBuffer
.charAt(0) == '*')) {
5674 currentState
.g2Charset
= CharacterSet
.US
;
5676 if ((collectBuffer
.length() == 1)
5677 && (collectBuffer
.charAt(0) == '+')) {
5679 currentState
.g3Charset
= CharacterSet
.US
;
5684 if ((type
== DeviceType
.VT220
)
5685 || (type
== DeviceType
.XTERM
)) {
5687 if ((collectBuffer
.length() == 1)
5688 && (collectBuffer
.charAt(0) == '(')) {
5690 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5692 if ((collectBuffer
.length() == 1)
5693 && (collectBuffer
.charAt(0) == ')')) {
5695 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5697 if ((collectBuffer
.length() == 1)
5698 && (collectBuffer
.charAt(0) == '*')) {
5700 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5702 if ((collectBuffer
.length() == 1)
5703 && (collectBuffer
.charAt(0) == '+')) {
5705 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5712 if ((type
== DeviceType
.VT220
)
5713 || (type
== DeviceType
.XTERM
)) {
5715 if ((collectBuffer
.length() == 1)
5716 && (collectBuffer
.charAt(0) == '(')) {
5718 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5720 if ((collectBuffer
.length() == 1)
5721 && (collectBuffer
.charAt(0) == ')')) {
5723 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5725 if ((collectBuffer
.length() == 1)
5726 && (collectBuffer
.charAt(0) == '*')) {
5728 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5730 if ((collectBuffer
.length() == 1)
5731 && (collectBuffer
.charAt(0) == '+')) {
5733 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5738 if ((type
== DeviceType
.VT220
)
5739 || (type
== DeviceType
.XTERM
)) {
5741 if ((collectBuffer
.length() == 1)
5742 && (collectBuffer
.charAt(0) == ' ')) {
5749 if ((type
== DeviceType
.VT220
)
5750 || (type
== DeviceType
.XTERM
)) {
5752 if ((collectBuffer
.length() == 1)
5753 && (collectBuffer
.charAt(0) == ' ')) {
5760 if ((type
== DeviceType
.VT220
)
5761 || (type
== DeviceType
.XTERM
)) {
5763 if ((collectBuffer
.length() == 1)
5764 && (collectBuffer
.charAt(0) == '(')) {
5766 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5768 if ((collectBuffer
.length() == 1)
5769 && (collectBuffer
.charAt(0) == ')')) {
5771 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5773 if ((collectBuffer
.length() == 1)
5774 && (collectBuffer
.charAt(0) == '*')) {
5776 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5778 if ((collectBuffer
.length() == 1)
5779 && (collectBuffer
.charAt(0) == '+')) {
5781 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5789 if ((type
== DeviceType
.VT220
)
5790 || (type
== DeviceType
.XTERM
)) {
5792 if ((collectBuffer
.length() == 1)
5793 && (collectBuffer
.charAt(0) == '(')) {
5795 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5797 if ((collectBuffer
.length() == 1)
5798 && (collectBuffer
.charAt(0) == ')')) {
5800 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5802 if ((collectBuffer
.length() == 1)
5803 && (collectBuffer
.charAt(0) == '*')) {
5805 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5807 if ((collectBuffer
.length() == 1)
5808 && (collectBuffer
.charAt(0) == '+')) {
5810 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5821 if ((type
== DeviceType
.VT220
)
5822 || (type
== DeviceType
.XTERM
)) {
5824 if ((collectBuffer
.length() == 1)
5825 && (collectBuffer
.charAt(0) == '(')) {
5827 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5829 if ((collectBuffer
.length() == 1)
5830 && (collectBuffer
.charAt(0) == ')')) {
5832 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5834 if ((collectBuffer
.length() == 1)
5835 && (collectBuffer
.charAt(0) == '*')) {
5837 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5839 if ((collectBuffer
.length() == 1)
5840 && (collectBuffer
.charAt(0) == '+')) {
5842 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5847 if ((type
== DeviceType
.VT220
)
5848 || (type
== DeviceType
.XTERM
)) {
5850 if ((collectBuffer
.length() == 1)
5851 && (collectBuffer
.charAt(0) == '(')) {
5853 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5855 if ((collectBuffer
.length() == 1)
5856 && (collectBuffer
.charAt(0) == ')')) {
5858 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5860 if ((collectBuffer
.length() == 1)
5861 && (collectBuffer
.charAt(0) == '*')) {
5863 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5865 if ((collectBuffer
.length() == 1)
5866 && (collectBuffer
.charAt(0) == '+')) {
5868 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5880 if ((type
== DeviceType
.VT220
)
5881 || (type
== DeviceType
.XTERM
)) {
5883 if ((collectBuffer
.length() == 1)
5884 && (collectBuffer
.charAt(0) == '(')) {
5886 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5888 if ((collectBuffer
.length() == 1)
5889 && (collectBuffer
.charAt(0) == ')')) {
5891 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5893 if ((collectBuffer
.length() == 1)
5894 && (collectBuffer
.charAt(0) == '*')) {
5896 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5898 if ((collectBuffer
.length() == 1)
5899 && (collectBuffer
.charAt(0) == '+')) {
5901 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5906 if ((type
== DeviceType
.VT220
)
5907 || (type
== DeviceType
.XTERM
)) {
5909 if ((collectBuffer
.length() == 1)
5910 && (collectBuffer
.charAt(0) == '(')) {
5912 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5914 if ((collectBuffer
.length() == 1)
5915 && (collectBuffer
.charAt(0) == ')')) {
5917 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5919 if ((collectBuffer
.length() == 1)
5920 && (collectBuffer
.charAt(0) == '*')) {
5922 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5924 if ((collectBuffer
.length() == 1)
5925 && (collectBuffer
.charAt(0) == '+')) {
5927 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5974 // 0x9C goes to GROUND
5982 // 00-17, 19, 1C-1F --> execute
5984 handleControlChar((char) ch
);
5987 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5988 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5990 scanState
= ScanState
.CSI_INTERMEDIATE
;
5993 // 30-39, 3B --> param, then switch to CSI_PARAM
5994 if ((ch
>= '0') && (ch
<= '9')) {
5996 scanState
= ScanState
.CSI_PARAM
;
6000 scanState
= ScanState
.CSI_PARAM
;
6003 // 3C-3F --> collect, then switch to CSI_PARAM
6004 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6006 scanState
= ScanState
.CSI_PARAM
;
6009 // 40-7E --> dispatch, then switch to GROUND
6010 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6013 // ICH - Insert character
6021 // CUD - Cursor down
6025 // CUF - Cursor forward
6029 // CUB - Cursor backward
6033 // CNL - Cursor down and to column 1
6034 if (type
== DeviceType
.XTERM
) {
6039 // CPL - Cursor up and to column 1
6040 if (type
== DeviceType
.XTERM
) {
6045 // CHA - Cursor to column # in current row
6046 if (type
== DeviceType
.XTERM
) {
6051 // CUP - Cursor position
6055 // CHT - Cursor forward X tab stops (default 1)
6056 if (type
== DeviceType
.XTERM
) {
6061 // ED - Erase in display
6065 // EL - Erase in line
6080 // DCH - Delete character
6087 // Scroll up X lines (default 1)
6088 if (type
== DeviceType
.XTERM
) {
6089 boolean xtermPrivateModeFlag
= false;
6090 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6091 if (collectBuffer
.charAt(i
) == '?') {
6092 xtermPrivateModeFlag
= true;
6096 if (xtermPrivateModeFlag
) {
6104 // Scroll down X lines (default 1)
6105 if (type
== DeviceType
.XTERM
) {
6114 if ((type
== DeviceType
.VT220
)
6115 || (type
== DeviceType
.XTERM
)) {
6117 // ECH - Erase character
6124 // CBT - Cursor backward X tab stops (default 1)
6125 if (type
== DeviceType
.XTERM
) {
6136 // HPA - Cursor to column # in current row. Same as CHA
6137 if (type
== DeviceType
.XTERM
) {
6142 // HPR - Cursor right. Same as CUF
6143 if (type
== DeviceType
.XTERM
) {
6148 // REP - Repeat last char X times
6149 if (type
== DeviceType
.XTERM
) {
6154 // DA - Device attributes
6158 // VPA - Cursor to row, current column.
6159 if (type
== DeviceType
.XTERM
) {
6164 // VPR - Cursor down. Same as CUD
6165 if (type
== DeviceType
.XTERM
) {
6170 // HVP - Horizontal and vertical position
6174 // TBC - Tabulation clear
6178 // Sets an ANSI or DEC private toggle
6182 if ((type
== DeviceType
.VT220
)
6183 || (type
== DeviceType
.XTERM
)) {
6185 // Printer functions
6193 // Sets an ANSI or DEC private toggle
6197 // SGR - Select graphics rendition
6201 // DSR - Device status report
6208 // DECLL - Load leds
6212 // DECSTBM - Set top and bottom margins
6216 // Save cursor (ANSI.SYS)
6217 if (type
== DeviceType
.XTERM
) {
6218 savedState
.cursorX
= currentState
.cursorX
;
6219 savedState
.cursorY
= currentState
.cursorY
;
6223 if (type
== DeviceType
.XTERM
) {
6224 // Window operations
6229 // Restore cursor (ANSI.SYS)
6230 if (type
== DeviceType
.XTERM
) {
6231 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
6238 // DECREQTPARM - Request terminal parameters
6254 // 0x9C goes to GROUND
6259 // 0x3A goes to CSI_IGNORE
6261 scanState
= ScanState
.CSI_IGNORE
;
6266 // 00-17, 19, 1C-1F --> execute
6268 handleControlChar((char) ch
);
6271 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6272 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6274 scanState
= ScanState
.CSI_INTERMEDIATE
;
6277 // 30-39, 3B --> param
6278 if ((ch
>= '0') && (ch
<= '9')) {
6285 // 0x3A goes to CSI_IGNORE
6287 scanState
= ScanState
.CSI_IGNORE
;
6289 // 0x3C-3F goes to CSI_IGNORE
6290 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6291 scanState
= ScanState
.CSI_IGNORE
;
6294 // 40-7E --> dispatch, then switch to GROUND
6295 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6298 // ICH - Insert character
6306 // CUD - Cursor down
6310 // CUF - Cursor forward
6314 // CUB - Cursor backward
6318 // CNL - Cursor down and to column 1
6319 if (type
== DeviceType
.XTERM
) {
6324 // CPL - Cursor up and to column 1
6325 if (type
== DeviceType
.XTERM
) {
6330 // CHA - Cursor to column # in current row
6331 if (type
== DeviceType
.XTERM
) {
6336 // CUP - Cursor position
6340 // CHT - Cursor forward X tab stops (default 1)
6341 if (type
== DeviceType
.XTERM
) {
6346 // ED - Erase in display
6350 // EL - Erase in line
6365 // DCH - Delete character
6372 // Scroll up X lines (default 1)
6373 if (type
== DeviceType
.XTERM
) {
6374 boolean xtermPrivateModeFlag
= false;
6375 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6376 if (collectBuffer
.charAt(i
) == '?') {
6377 xtermPrivateModeFlag
= true;
6381 if (xtermPrivateModeFlag
) {
6389 // Scroll down X lines (default 1)
6390 if (type
== DeviceType
.XTERM
) {
6399 if ((type
== DeviceType
.VT220
)
6400 || (type
== DeviceType
.XTERM
)) {
6402 // ECH - Erase character
6409 // CBT - Cursor backward X tab stops (default 1)
6410 if (type
== DeviceType
.XTERM
) {
6421 // HPA - Cursor to column # in current row. Same as CHA
6422 if (type
== DeviceType
.XTERM
) {
6427 // HPR - Cursor right. Same as CUF
6428 if (type
== DeviceType
.XTERM
) {
6433 // REP - Repeat last char X times
6434 if (type
== DeviceType
.XTERM
) {
6439 // DA - Device attributes
6443 // VPA - Cursor to row, current column.
6444 if (type
== DeviceType
.XTERM
) {
6449 // VPR - Cursor down. Same as CUD
6450 if (type
== DeviceType
.XTERM
) {
6455 // HVP - Horizontal and vertical position
6459 // TBC - Tabulation clear
6463 // Sets an ANSI or DEC private toggle
6467 if ((type
== DeviceType
.VT220
)
6468 || (type
== DeviceType
.XTERM
)) {
6470 // Printer functions
6478 // Sets an ANSI or DEC private toggle
6482 // SGR - Select graphics rendition
6486 // DSR - Device status report
6493 // DECLL - Load leds
6497 // DECSTBM - Set top and bottom margins
6503 if (type
== DeviceType
.XTERM
) {
6504 // Window operations
6513 // DECREQTPARM - Request terminal parameters
6530 case CSI_INTERMEDIATE
:
6531 // 00-17, 19, 1C-1F --> execute
6533 handleControlChar((char) ch
);
6536 // 20-2F --> collect
6537 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6541 // 0x30-3F goes to CSI_IGNORE
6542 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6543 scanState
= ScanState
.CSI_IGNORE
;
6546 // 40-7E --> dispatch, then switch to GROUND
6547 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6599 if (((type
== DeviceType
.VT220
)
6600 || (type
== DeviceType
.XTERM
))
6601 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6603 // DECSCL - compatibility level
6606 if ((type
== DeviceType
.XTERM
)
6607 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6609 // DECSTR - Soft terminal reset
6614 if (((type
== DeviceType
.VT220
)
6615 || (type
== DeviceType
.XTERM
))
6616 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6644 // 00-17, 19, 1C-1F --> execute
6646 handleControlChar((char) ch
);
6649 // 20-2F --> collect
6650 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6654 // 40-7E --> ignore, then switch to GROUND
6655 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6659 // 20-3F, 7F --> ignore
6665 // 0x9C goes to GROUND
6670 // 0x1B 0x5C goes to GROUND
6675 if ((collectBuffer
.length() > 0)
6676 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6682 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6683 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6685 scanState
= ScanState
.DCS_INTERMEDIATE
;
6688 // 30-39, 3B --> param, then switch to DCS_PARAM
6689 if ((ch
>= '0') && (ch
<= '9')) {
6691 scanState
= ScanState
.DCS_PARAM
;
6695 scanState
= ScanState
.DCS_PARAM
;
6698 // 3C-3F --> collect, then switch to DCS_PARAM
6699 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6701 scanState
= ScanState
.DCS_PARAM
;
6704 // 00-17, 19, 1C-1F, 7F --> ignore
6706 // 0x3A goes to DCS_IGNORE
6708 scanState
= ScanState
.DCS_IGNORE
;
6711 // 0x71 goes to DCS_SIXEL
6713 sixelParseBuffer
.setLength(0);
6714 scanState
= ScanState
.DCS_SIXEL
;
6715 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6716 // 0x40-7E goes to DCS_PASSTHROUGH
6717 scanState
= ScanState
.DCS_PASSTHROUGH
;
6721 case DCS_INTERMEDIATE
:
6723 // 0x9C goes to GROUND
6728 // 0x1B 0x5C goes to GROUND
6733 if ((collectBuffer
.length() > 0)
6734 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6740 // 0x30-3F goes to DCS_IGNORE
6741 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6742 scanState
= ScanState
.DCS_IGNORE
;
6745 // 0x40-7E goes to DCS_PASSTHROUGH
6746 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6747 scanState
= ScanState
.DCS_PASSTHROUGH
;
6750 // 00-17, 19, 1C-1F, 7F --> ignore
6755 // 0x9C goes to GROUND
6760 // 0x1B 0x5C goes to GROUND
6765 if ((collectBuffer
.length() > 0)
6766 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6772 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6773 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6775 scanState
= ScanState
.DCS_INTERMEDIATE
;
6778 // 30-39, 3B --> param
6779 if ((ch
>= '0') && (ch
<= '9')) {
6786 // 00-17, 19, 1C-1F, 7F --> ignore
6788 // 0x3A, 3C-3F goes to DCS_IGNORE
6790 scanState
= ScanState
.DCS_IGNORE
;
6792 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6793 scanState
= ScanState
.DCS_IGNORE
;
6796 // 0x71 goes to DCS_SIXEL
6798 sixelParseBuffer
.setLength(0);
6799 scanState
= ScanState
.DCS_SIXEL
;
6800 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6801 // 0x40-7E goes to DCS_PASSTHROUGH
6802 scanState
= ScanState
.DCS_PASSTHROUGH
;
6806 case DCS_PASSTHROUGH
:
6807 // 0x9C goes to GROUND
6812 // 0x1B 0x5C goes to GROUND
6817 if ((collectBuffer
.length() > 0)
6818 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6824 // 00-17, 19, 1C-1F, 20-7E --> put
6826 // We ignore all DCS except sixel.
6830 // We ignore all DCS except sixel.
6833 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6834 // We ignore all DCS except sixel.
6837 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6838 // We ignore all DCS except sixel.
6847 // 00-17, 19, 1C-1F, 20-7F --> ignore
6849 // 0x9C goes to GROUND
6857 // 0x9C goes to GROUND
6864 // 0x1B 0x5C goes to GROUND
6870 if ((collectBuffer
.length() > 0)
6871 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6879 // 00-17, 19, 1C-1F, 20-7E --> put
6882 || ((ch
>= 0x1C) && (ch
<= 0x1F))
6883 || ((ch
>= 0x20) && (ch
<= 0x7E))
6885 sixelParseBuffer
.append((char) ch
);
6891 case SOSPMAPC_STRING
:
6892 // 00-17, 19, 1C-1F, 20-7F --> ignore
6894 // Special case for Jexer: PM can pass one control character
6899 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6903 // 0x9C goes to GROUND
6911 // Special case for Xterm: OSC can pass control characters
6912 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
6916 // 00-17, 19, 1C-1F --> ignore
6918 // 20-7F --> osc_put
6919 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6923 // 0x9C goes to GROUND
6930 case VT52_DIRECT_CURSOR_ADDRESS
:
6931 // This is a special case for the VT52 sequence "ESC Y l c"
6932 if (collectBuffer
.length() == 0) {
6934 } else if (collectBuffer
.length() == 1) {
6935 // We've got the two characters, one in the buffer and the
6937 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
6946 * Expose current cursor X to outside world.
6948 * @return current cursor X
6950 public final int getCursorX() {
6951 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6952 return currentState
.cursorX
* 2;
6954 return currentState
.cursorX
;
6958 * Expose current cursor Y to outside world.
6960 * @return current cursor Y
6962 public final int getCursorY() {
6963 return currentState
.cursorY
;
6967 * Returns true if this terminal has requested the mouse pointer be
6970 * @return true if this terminal has requested the mouse pointer be
6973 public final boolean hasHiddenMousePointer() {
6974 return hideMousePointer
;
6978 * Get the mouse protocol.
6980 * @return MouseProtocol.OFF, MouseProtocol.X10, etc.
6982 public MouseProtocol
getMouseProtocol() {
6983 return mouseProtocol
;
6987 * Draw the left and right cells of a two-cell-wide (full-width) glyph.
6989 * @param leftX the x position to draw the left half to
6990 * @param leftY the y position to draw the left half to
6991 * @param rightX the x position to draw the right half to
6992 * @param rightY the y position to draw the right half to
6993 * @param ch the character to draw
6995 private void drawHalves(final int leftX
, final int leftY
,
6996 final int rightX
, final int rightY
, final int ch
) {
6998 // System.err.println("drawHalves(): " + Integer.toHexString(ch));
7000 if (lastTextHeight
!= textHeight
) {
7001 glyphMaker
= GlyphMaker
.getInstance(textHeight
);
7002 lastTextHeight
= textHeight
;
7005 Cell cell
= new Cell(ch
, currentState
.attr
);
7006 BufferedImage image
= glyphMaker
.getImage(cell
, textWidth
* 2,
7008 BufferedImage leftImage
= image
.getSubimage(0, 0, textWidth
,
7010 BufferedImage rightImage
= image
.getSubimage(textWidth
, 0, textWidth
,
7013 Cell left
= new Cell(cell
);
7014 left
.setImage(leftImage
);
7015 left
.setWidth(Cell
.Width
.LEFT
);
7016 display
.get(leftY
).replace(leftX
, left
);
7018 Cell right
= new Cell(cell
);
7019 right
.setImage(rightImage
);
7020 right
.setWidth(Cell
.Width
.RIGHT
);
7021 display
.get(rightY
).replace(rightX
, right
);
7025 * Set the width of a character cell in pixels.
7027 * @param textWidth the width in pixels of a character cell
7029 public void setTextWidth(final int textWidth
) {
7030 this.textWidth
= textWidth
;
7034 * Set the height of a character cell in pixels.
7036 * @param textHeight the height in pixels of a character cell
7038 public void setTextHeight(final int textHeight
) {
7039 this.textHeight
= textHeight
;
7043 * Parse a sixel string into a bitmap image, and overlay that image onto
7046 private void parseSixel() {
7049 System.err.println("parseSixel(): '" + sixelParseBuffer.toString()
7053 Sixel sixel
= new Sixel(sixelParseBuffer
.toString(), sixelPalette
);
7054 BufferedImage image
= sixel
.getImage();
7056 // System.err.println("parseSixel(): image " + image);
7058 if (image
== null) {
7059 // Sixel data was malformed in some way, bail out.
7062 if ((image
.getWidth() < 1)
7063 || (image
.getWidth() > 10000)
7064 || (image
.getHeight() < 1)
7065 || (image
.getHeight() > 10000)
7070 imageToCells(image
, true);
7074 * Parse a "Jexer" RGB image string into a bitmap image, and overlay that
7075 * image onto the text cells.
7077 * @param pw width token
7078 * @param ph height token
7079 * @param ps scroll token
7080 * @param data pixel data
7082 private void parseJexerImageRGB(final String pw
, final String ph
,
7083 final String ps
, final String data
) {
7086 int imageHeight
= 0;
7087 boolean scroll
= false;
7089 imageWidth
= Integer
.parseInt(pw
);
7090 imageHeight
= Integer
.parseInt(ph
);
7091 } catch (NumberFormatException e
) {
7095 if ((imageWidth
< 1)
7096 || (imageWidth
> 10000)
7097 || (imageHeight
< 1)
7098 || (imageHeight
> 10000)
7102 if (ps
.equals("1")) {
7104 } else if (ps
.equals("0")) {
7110 java
.util
.Base64
.Decoder base64
= java
.util
.Base64
.getDecoder();
7111 byte [] bytes
= base64
.decode(data
);
7112 if (bytes
.length
!= (imageWidth
* imageHeight
* 3)) {
7116 BufferedImage image
= new BufferedImage(imageWidth
, imageHeight
,
7117 BufferedImage
.TYPE_INT_ARGB
);
7119 for (int x
= 0; x
< imageWidth
; x
++) {
7120 for (int y
= 0; y
< imageHeight
; y
++) {
7121 int red
= bytes
[(y
* imageWidth
* 3) + (x
* 3) ];
7125 int green
= bytes
[(y
* imageWidth
* 3) + (x
* 3) + 1];
7129 int blue
= bytes
[(y
* imageWidth
* 3) + (x
* 3) + 2];
7133 int rgb
= 0xFF000000 | (red
<< 16) | (green
<< 8) | blue
;
7134 image
.setRGB(x
, y
, rgb
);
7138 imageToCells(image
, scroll
);
7142 * Parse a "Jexer" PNG or JPG image string into a bitmap image, and
7143 * overlay that image onto the text cells.
7145 * @param type 1 for PNG, 2 for JPG
7146 * @param ps scroll token
7147 * @param data pixel data
7149 private void parseJexerImageFile(final int type
, final String ps
,
7150 final String data
) {
7153 int imageHeight
= 0;
7154 boolean scroll
= false;
7155 BufferedImage image
= null;
7157 java
.util
.Base64
.Decoder base64
= java
.util
.Base64
.getDecoder();
7158 byte [] bytes
= base64
.decode(data
);
7162 if ((bytes
[0] != (byte) 0x89)
7163 || (bytes
[1] != 'P')
7164 || (bytes
[2] != 'N')
7165 || (bytes
[3] != 'G')
7166 || (bytes
[4] != (byte) 0x0D)
7167 || (bytes
[5] != (byte) 0x0A)
7168 || (bytes
[6] != (byte) 0x1A)
7169 || (bytes
[7] != (byte) 0x0A)
7171 // File does not have PNG header, bail out.
7177 if ((bytes
[0] != (byte) 0XFF)
7178 || (bytes
[1] != (byte) 0xD8)
7179 || (bytes
[2] != (byte) 0xFF)
7181 // File does not have JPG header, bail out.
7187 // Unsupported type, bail out.
7191 image
= ImageIO
.read(new ByteArrayInputStream(bytes
));
7192 } catch (IOException e
) {
7196 assert (image
!= null);
7197 imageWidth
= image
.getWidth();
7198 imageHeight
= image
.getHeight();
7199 if ((imageWidth
< 1)
7200 || (imageWidth
> 10000)
7201 || (imageHeight
< 1)
7202 || (imageHeight
> 10000)
7206 if (ps
.equals("1")) {
7208 } else if (ps
.equals("0")) {
7214 imageToCells(image
, scroll
);
7218 * Break up an image into the cells at the current cursor.
7220 * @param image the image to display
7221 * @param scroll if true, scroll the image and move the cursor
7223 private void imageToCells(final BufferedImage image
, final boolean scroll
) {
7224 assert (image
!= null);
7229 * Break up the image into text cell sized pieces as a new array of
7232 * Note original column position x0.
7236 * 1. Advance (printCharacter(' ')) for horizontal increment, or
7237 * index (linefeed() + cursorPosition(y, x0)) for vertical
7240 * 2. Set (x, y) cell image data.
7242 * 3. For the right and bottom edges:
7244 * a. Render the text to pixels using Terminus font.
7246 * b. Blit the image on top of the text, using alpha channel.
7248 int cellColumns
= image
.getWidth() / textWidth
;
7249 if (cellColumns
* textWidth
< image
.getWidth()) {
7252 int cellRows
= image
.getHeight() / textHeight
;
7253 if (cellRows
* textHeight
< image
.getHeight()) {
7257 // Break the image up into an array of cells.
7258 Cell
[][] cells
= new Cell
[cellColumns
][cellRows
];
7260 for (int x
= 0; x
< cellColumns
; x
++) {
7261 for (int y
= 0; y
< cellRows
; y
++) {
7263 int width
= textWidth
;
7264 if ((x
+ 1) * textWidth
> image
.getWidth()) {
7265 width
= image
.getWidth() - (x
* textWidth
);
7267 int height
= textHeight
;
7268 if ((y
+ 1) * textHeight
> image
.getHeight()) {
7269 height
= image
.getHeight() - (y
* textHeight
);
7272 Cell cell
= new Cell();
7273 cell
.setImage(image
.getSubimage(x
* textWidth
,
7274 y
* textHeight
, width
, height
));
7280 int x0
= currentState
.cursorX
;
7281 int y0
= currentState
.cursorY
;
7282 for (int y
= 0; y
< cellRows
; y
++) {
7283 for (int x
= 0; x
< cellColumns
; x
++) {
7284 assert (currentState
.cursorX
<= rightMargin
);
7286 // TODO: Render text of current cell first, then image over
7287 // it (accounting for blank pixels). For now, just copy the
7289 DisplayLine line
= display
.get(currentState
.cursorY
);
7290 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
7292 // If at the end of the visible screen, stop.
7293 if (currentState
.cursorX
== rightMargin
) {
7296 // Room for more image on the visible screen.
7297 currentState
.cursorX
++;
7299 if (currentState
.cursorY
< scrollRegionBottom
- 1) {
7300 // Not at the bottom, down a line.
7302 } else if (scroll
== true) {
7303 // At the bottom, scroll as needed.
7306 // At the bottom, no more scrolling, done.
7309 cursorPosition(currentState
.cursorY
, x0
);
7312 if (scroll
== false) {
7313 cursorPosition(y0
, x0
);