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
329 private int width
= 80;
332 * Physical display height. We start at 80x24, but the user can resize
335 private int height
= 24;
338 * Top margin of the scrolling region.
340 private int scrollRegionTop
= 0;
343 * Bottom margin of the scrolling region.
345 private int scrollRegionBottom
= height
- 1;
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
= 79;
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
= false;
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
));
676 assert (currentState
.cursorY
< height
);
677 assert (currentState
.cursorX
< width
);
679 // Spin up the input reader
680 readerThread
= new Thread(this);
681 readerThread
.start();
684 // ------------------------------------------------------------------------
685 // Runnable ---------------------------------------------------------------
686 // ------------------------------------------------------------------------
689 * Read function runs on a separate thread.
691 public final void run() {
692 boolean utf8
= false;
693 boolean done
= false;
695 if (type
== DeviceType
.XTERM
) {
699 // available() will often return > 1, so we need to read in chunks to
701 char [] readBufferUTF8
= null;
702 byte [] readBuffer
= null;
704 readBufferUTF8
= new char[2048];
706 readBuffer
= new byte[2048];
709 while (!done
&& !stopReaderThread
) {
710 synchronized (userQueue
) {
711 while (userQueue
.size() > 0) {
712 handleUserEvent(userQueue
.remove(0));
717 int n
= inputStream
.available();
719 // System.err.printf("available() %d\n", n); System.err.flush();
721 if (readBufferUTF8
.length
< n
) {
722 // The buffer wasn't big enough, make it huger
723 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
726 readBufferUTF8
= new char[newSizeHalf
* 2];
729 if (readBuffer
.length
< n
) {
730 // The buffer wasn't big enough, make it huger
731 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
732 readBuffer
= new byte[newSizeHalf
* 2];
738 } catch (InterruptedException e
) {
747 rc
= input
.read(readBufferUTF8
, 0,
748 readBufferUTF8
.length
);
750 rc
= inputStream
.read(readBuffer
, 0,
753 } catch (ReadTimeoutException e
) {
757 // System.err.printf("read() %d\n", rc); System.err.flush();
762 // Don't step on UI events
763 synchronized (this) {
765 for (int i
= 0; i
< rc
;) {
766 int ch
= Character
.codePointAt(readBufferUTF8
,
768 i
+= Character
.charCount(ch
);
770 // Special case for VT10x: 7-bit characters
772 if ((type
== DeviceType
.VT100
)
773 || (type
== DeviceType
.VT102
)
781 for (int i
= 0; i
< rc
; i
++) {
782 // Special case for VT10x: 7-bit characters
784 if ((type
== DeviceType
.VT100
)
785 || (type
== DeviceType
.VT102
)
787 consume(readBuffer
[i
] & 0x7F);
789 consume(readBuffer
[i
]);
794 // Permit my enclosing UI to know that I updated.
795 if (displayListener
!= null) {
796 displayListener
.displayChanged();
799 // System.err.println("end while loop"); System.err.flush();
800 } catch (IOException e
) {
803 // This is an unusual case. We want to see the stack trace,
804 // but it is related to the spawned process rather than the
805 // actual UI. We will generate the stack trace, and consume
806 // it as though it was emitted by the shell.
807 CharArrayWriter writer
= new CharArrayWriter();
808 // Send a ST and RIS to clear the emulator state.
810 writer
.write("\033\\\033c");
811 writer
.write("\n-----------------------------------\n");
812 e
.printStackTrace(new PrintWriter(writer
));
813 writer
.write("\n-----------------------------------\n");
814 } catch (IOException e2
) {
817 char [] stackTrace
= writer
.toCharArray();
818 for (int i
= 0; i
< stackTrace
.length
; i
++) {
819 if (stackTrace
[i
] == '\n') {
822 consume(stackTrace
[i
]);
826 } // while ((done == false) && (stopReaderThread == false))
828 // Let the rest of the world know that I am done.
829 stopReaderThread
= true;
832 inputStream
.cancelRead();
835 } catch (IOException e
) {
841 } catch (IOException e
) {
845 // Permit my enclosing UI to know that I updated.
846 if (displayListener
!= null) {
847 displayListener
.displayChanged();
850 // System.err.println("*** run() exiting..."); System.err.flush();
853 // ------------------------------------------------------------------------
854 // ECMA48 -----------------------------------------------------------------
855 // ------------------------------------------------------------------------
858 * Process keyboard and mouse events from the user.
860 * @param event the input event to consume
862 private void handleUserEvent(final TInputEvent event
) {
863 if (event
instanceof TKeypressEvent
) {
864 keypress(((TKeypressEvent
) event
).getKey());
866 if (event
instanceof TMouseEvent
) {
867 mouse((TMouseEvent
) event
);
872 * Add a keyboard and mouse event from the user to the queue.
874 * @param event the input event to consume
876 public void addUserEvent(final TInputEvent event
) {
877 synchronized (userQueue
) {
878 userQueue
.add(event
);
883 * Return the proper primary Device Attributes string.
885 * @return string to send to remote side that is appropriate for the
888 private String
deviceTypeResponse() {
891 // "I am a VT100 with advanced video option" (often VT102)
900 // "I am a VT220" - 7 bit version, with sixel and Jexer image
903 return "\033[?62;1;6;9;4;22;444c";
905 // "I am a VT220" - 8 bit version, with sixel and Jexer image
907 return "\u009b?62;1;6;9;4;22;444c";
909 throw new IllegalArgumentException("Invalid device type: " + type
);
914 * Return the proper TERM environment variable for this device type.
916 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
917 * @return "vt100", "xterm", etc.
919 public static String
deviceTypeTerm(final DeviceType deviceType
) {
920 switch (deviceType
) {
934 throw new IllegalArgumentException("Invalid device type: "
940 * Return the proper LANG for this device type. Only XTERM devices know
941 * about UTF-8, the others are defined by their standard to be either
942 * 7-bit or 8-bit characters only.
944 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
945 * @param baseLang a base language without UTF-8 flag such as "C" or
947 * @return "en_US", "en_US.UTF-8", etc.
949 public static String
deviceTypeLang(final DeviceType deviceType
,
950 final String baseLang
) {
952 switch (deviceType
) {
960 return baseLang
+ ".UTF-8";
963 throw new IllegalArgumentException("Invalid device type: "
969 * Write a string directly to the remote side.
971 * @param str string to send
973 public void writeRemote(final String str
) {
974 if (stopReaderThread
) {
975 // Reader hit EOF, bail out now.
980 // System.err.printf("writeRemote() '%s'\n", str);
986 if (outputStream
== null) {
990 outputStream
.flush();
991 for (int i
= 0; i
< str
.length(); i
++) {
992 outputStream
.write(str
.charAt(i
));
994 outputStream
.flush();
995 } catch (IOException e
) {
1001 if (output
== null) {
1008 } catch (IOException e
) {
1014 throw new IllegalArgumentException("Invalid device type: " + type
);
1019 * Close the input and output streams and stop the reader thread. Note
1020 * that it is safe to call this multiple times.
1022 public final void close() {
1024 // Tell the reader thread to stop looking at input. It will close
1025 // the input streams.
1026 if (stopReaderThread
== false) {
1027 stopReaderThread
= true;
1030 // Now close the output stream.
1035 if (outputStream
!= null) {
1037 outputStream
.close();
1038 } catch (IOException e
) {
1041 outputStream
= null;
1045 if (outputStream
!= null) {
1047 outputStream
.close();
1048 } catch (IOException e
) {
1051 outputStream
= null;
1053 if (output
!= null) {
1056 } catch (IOException e
) {
1063 throw new IllegalArgumentException("Invalid device type: " +
1069 * See if the reader thread is still running.
1071 * @return if true, we are still connected to / reading from the remote
1074 public final boolean isReading() {
1075 return (!stopReaderThread
);
1079 * Obtain a new blank display line for an external user
1080 * (e.g. TTerminalWindow).
1082 * @return new blank line
1084 public final DisplayLine
getBlankDisplayLine() {
1085 return new DisplayLine(currentState
.attr
);
1089 * Get the scrollback buffer.
1091 * @return the scrollback buffer
1093 public final List
<DisplayLine
> getScrollbackBuffer() {
1098 * Get the display buffer.
1100 * @return the display buffer
1102 public final List
<DisplayLine
> getDisplayBuffer() {
1107 * Get the visible display + scrollback buffer, offset by a specified
1108 * number of rows from the bottom.
1110 * @param visibleHeight the total height of the display to show
1111 * @param scrollBottom the number of rows from the bottom to scroll back
1112 * @return a copy of the display + scrollback buffers
1114 public final List
<DisplayLine
> getVisibleDisplay(final int visibleHeight
,
1115 final int scrollBottom
) {
1117 assert (visibleHeight
>= 0);
1118 assert (scrollBottom
>= 0);
1120 int visibleBottom
= scrollback
.size() + display
.size() - scrollBottom
;
1122 List
<DisplayLine
> preceedingBlankLines
= new ArrayList
<DisplayLine
>();
1123 int visibleTop
= visibleBottom
- visibleHeight
;
1124 if (visibleTop
< 0) {
1125 for (int i
= visibleTop
; i
< 0; i
++) {
1126 preceedingBlankLines
.add(getBlankDisplayLine());
1130 assert (visibleTop
>= 0);
1132 List
<DisplayLine
> displayLines
= new ArrayList
<DisplayLine
>();
1133 displayLines
.addAll(scrollback
);
1134 displayLines
.addAll(display
);
1136 List
<DisplayLine
> visibleLines
= new ArrayList
<DisplayLine
>();
1137 visibleLines
.addAll(preceedingBlankLines
);
1138 visibleLines
.addAll(displayLines
.subList(visibleTop
, visibleBottom
));
1140 // Fill in the blank lines on bottom
1141 int bottomBlankLines
= visibleHeight
- visibleLines
.size();
1142 assert (bottomBlankLines
>= 0);
1143 for (int i
= 0; i
< bottomBlankLines
; i
++) {
1144 visibleLines
.add(getBlankDisplayLine());
1147 return copyBuffer(visibleLines
);
1151 * Copy a display buffer.
1153 * @param buffer the buffer to copy
1154 * @return a deep copy of the buffer's data
1156 private List
<DisplayLine
> copyBuffer(final List
<DisplayLine
> buffer
) {
1157 ArrayList
<DisplayLine
> result
= new ArrayList
<DisplayLine
>(buffer
.size());
1158 for (DisplayLine line
: buffer
) {
1159 result
.add(new DisplayLine(line
));
1165 * Get the display width.
1167 * @return the width (usually 80 or 132)
1169 public final int getWidth() {
1174 * Set the display width.
1176 * @param width the new width
1178 public final synchronized void setWidth(final int width
) {
1180 rightMargin
= width
- 1;
1181 if (currentState
.cursorX
>= width
) {
1182 currentState
.cursorX
= width
- 1;
1184 if (savedState
.cursorX
>= width
) {
1185 savedState
.cursorX
= width
- 1;
1190 * Get the display height.
1192 * @return the height (usually 24)
1194 public final int getHeight() {
1199 * Set the display height.
1201 * @param height the new height
1203 public final synchronized void setHeight(final int height
) {
1204 int delta
= height
- this.height
;
1205 this.height
= height
;
1206 scrollRegionBottom
+= delta
;
1207 if ((scrollRegionBottom
< 0) || (scrollRegionTop
> height
- 1)) {
1208 scrollRegionBottom
= height
- 1;
1210 if (scrollRegionTop
>= scrollRegionBottom
) {
1211 scrollRegionTop
= 0;
1213 if (currentState
.cursorY
>= height
) {
1214 currentState
.cursorY
= height
- 1;
1216 if (savedState
.cursorY
>= height
) {
1217 savedState
.cursorY
= height
- 1;
1219 while (display
.size() < height
) {
1220 DisplayLine line
= new DisplayLine(currentState
.attr
);
1221 line
.setReverseColor(reverseVideo
);
1224 while (display
.size() > height
) {
1225 scrollback
.add(display
.remove(0));
1230 * Get visible cursor flag.
1232 * @return if true, the cursor is visible
1234 public final boolean isCursorVisible() {
1235 return cursorVisible
;
1239 * Get the screen title as set by the xterm OSC sequence. Lots of
1240 * applications send a screenTitle regardless of whether it is an xterm
1243 * @return screen title
1245 public final String
getScreenTitle() {
1250 * Get 132 columns value.
1252 * @return if true, the terminal is in 132 column mode
1254 public final boolean isColumns132() {
1259 * Clear the CSI parameters and flags.
1261 private void toGround() {
1263 collectBuffer
.setLength(0);
1264 scanState
= ScanState
.GROUND
;
1268 * Reset the tab stops list.
1270 private void resetTabStops() {
1272 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
1273 tabStops
.add(Integer
.valueOf(i
* 8));
1278 * Reset the 88- or 256-colors.
1280 private void resetColors() {
1281 colors88
= new ArrayList
<Integer
>(256);
1282 for (int i
= 0; i
< 256; i
++) {
1286 // Set default system colors.
1287 colors88
.set(0, 0x00000000);
1288 colors88
.set(1, 0x00a80000);
1289 colors88
.set(2, 0x0000a800);
1290 colors88
.set(3, 0x00a85400);
1291 colors88
.set(4, 0x000000a8);
1292 colors88
.set(5, 0x00a800a8);
1293 colors88
.set(6, 0x0000a8a8);
1294 colors88
.set(7, 0x00a8a8a8);
1296 colors88
.set(8, 0x00545454);
1297 colors88
.set(9, 0x00fc5454);
1298 colors88
.set(10, 0x0054fc54);
1299 colors88
.set(11, 0x00fcfc54);
1300 colors88
.set(12, 0x005454fc);
1301 colors88
.set(13, 0x00fc54fc);
1302 colors88
.set(14, 0x0054fcfc);
1303 colors88
.set(15, 0x00fcfcfc);
1307 * Get the RGB value of one of the indexed colors.
1309 * @param index the color index
1310 * @return the RGB value
1312 private int get88Color(final int index
) {
1313 // System.err.print("get88Color: " + index);
1314 if ((index
< 0) || (index
> colors88
.size())) {
1315 // System.err.println(" -- UNKNOWN");
1318 // System.err.printf(" %08x\n", colors88.get(index));
1319 return colors88
.get(index
);
1323 * Set one of the indexed colors to a color specification.
1325 * @param index the color index
1326 * @param spec the specification, typically something like "rgb:aa/bb/cc"
1328 private void set88Color(final int index
, final String spec
) {
1329 // System.err.println("set88Color: " + index + " '" + spec + "'");
1331 if ((index
< 0) || (index
> colors88
.size())) {
1334 if (spec
.startsWith("rgb:")) {
1335 String
[] rgbTokens
= spec
.substring(4).split("/");
1336 if (rgbTokens
.length
== 3) {
1338 int rgb
= (Integer
.parseInt(rgbTokens
[0], 16) << 16);
1339 rgb
|= Integer
.parseInt(rgbTokens
[1], 16) << 8;
1340 rgb
|= Integer
.parseInt(rgbTokens
[2], 16);
1341 // System.err.printf(" set to %08x\n", rgb);
1342 colors88
.set(index
, rgb
);
1343 } catch (NumberFormatException e
) {
1350 if (spec
.toLowerCase().equals("black")) {
1351 colors88
.set(index
, 0x00000000);
1352 } else if (spec
.toLowerCase().equals("red")) {
1353 colors88
.set(index
, 0x00a80000);
1354 } else if (spec
.toLowerCase().equals("green")) {
1355 colors88
.set(index
, 0x0000a800);
1356 } else if (spec
.toLowerCase().equals("yellow")) {
1357 colors88
.set(index
, 0x00a85400);
1358 } else if (spec
.toLowerCase().equals("blue")) {
1359 colors88
.set(index
, 0x000000a8);
1360 } else if (spec
.toLowerCase().equals("magenta")) {
1361 colors88
.set(index
, 0x00a800a8);
1362 } else if (spec
.toLowerCase().equals("cyan")) {
1363 colors88
.set(index
, 0x0000a8a8);
1364 } else if (spec
.toLowerCase().equals("white")) {
1365 colors88
.set(index
, 0x00a8a8a8);
1371 * Reset the emulation state.
1373 private void reset() {
1375 currentState
= new SaveableState();
1376 savedState
= new SaveableState();
1377 scanState
= ScanState
.GROUND
;
1378 if (displayListener
!= null) {
1379 width
= displayListener
.getDisplayWidth();
1380 height
= displayListener
.getDisplayHeight();
1385 scrollRegionTop
= 0;
1386 scrollRegionBottom
= height
- 1;
1387 rightMargin
= width
- 1;
1388 newLineMode
= false;
1389 arrowKeyMode
= ArrowKeyMode
.ANSI
;
1390 keypadMode
= KeypadMode
.Numeric
;
1391 wrapLineFlag
= false;
1398 newLineMode
= false;
1399 reverseVideo
= false;
1401 cursorVisible
= true;
1404 singleshift
= Singleshift
.NONE
;
1406 printerControllerMode
= false;
1409 mouseProtocol
= MouseProtocol
.OFF
;
1410 mouseEncoding
= MouseEncoding
.X10
;
1415 // Reset extra colors
1423 * Append a new line to the bottom of the display, adding lines off the
1424 * top to the scrollback buffer.
1426 private void newDisplayLine() {
1427 // Scroll the top line off into the scrollback buffer
1428 scrollback
.add(display
.get(0));
1429 if (scrollback
.size() > maxScrollback
) {
1430 scrollback
.remove(0);
1431 scrollback
.trimToSize();
1434 display
.trimToSize();
1435 DisplayLine line
= new DisplayLine(currentState
.attr
);
1436 line
.setReverseColor(reverseVideo
);
1441 * Wraps the current line.
1443 private void wrapCurrentLine() {
1444 if (currentState
.cursorY
== height
- 1) {
1447 if (currentState
.cursorY
< height
- 1) {
1448 currentState
.cursorY
++;
1450 currentState
.cursorX
= 0;
1454 * Handle a carriage return.
1456 private void carriageReturn() {
1457 currentState
.cursorX
= 0;
1458 wrapLineFlag
= false;
1462 * Reverse the color of the visible display.
1464 private void invertDisplayColors() {
1465 for (DisplayLine line
: display
) {
1466 line
.setReverseColor(!line
.isReverseColor());
1471 * Handle a linefeed.
1473 private void linefeed() {
1474 if (currentState
.cursorY
< scrollRegionBottom
) {
1475 // Increment screen y
1476 currentState
.cursorY
++;
1479 // Screen y does not increment
1482 * Two cases: either we're inside a scrolling region or not. If
1483 * the scrolling region bottom is the bottom of the screen, then
1484 * push the top line into the buffer. Else scroll the scrolling
1487 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1489 // We're at the bottom of the scroll region, AND the scroll
1490 // region is the entire screen.
1496 // We're at the bottom of the scroll region, AND the scroll
1497 // region is NOT the entire screen.
1498 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1503 currentState
.cursorX
= 0;
1505 wrapLineFlag
= false;
1509 * Prints one character to the display buffer.
1511 * @param ch character to display
1513 private void printCharacter(final int ch
) {
1514 int rightMargin
= this.rightMargin
;
1516 if (StringUtils
.width(ch
) == 2) {
1517 // This is a full-width character. Save two spaces, and then
1518 // draw the character as two image halves.
1519 int x0
= currentState
.cursorX
;
1520 int y0
= currentState
.cursorY
;
1521 printCharacter(' ');
1522 printCharacter(' ');
1523 if ((currentState
.cursorX
== x0
+ 2)
1524 && (currentState
.cursorY
== y0
)
1526 // We can draw both halves of the character.
1527 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1528 } else if ((currentState
.cursorX
== x0
+ 1)
1529 && (currentState
.cursorY
== y0
)
1531 // VT100 line wrap behavior: we should be at the right
1532 // margin. We can draw both halves of the character.
1533 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1535 // The character splits across the line. Draw the entire
1536 // character on the new line, giving one more space for it.
1537 x0
= currentState
.cursorX
- 1;
1538 y0
= currentState
.cursorY
;
1539 printCharacter(' ');
1540 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1545 // Check if we have double-width, and if so chop at 40/66 instead of
1547 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1548 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1551 // Check the unusually-complicated line wrapping conditions...
1552 if (currentState
.cursorX
== rightMargin
) {
1554 if (currentState
.lineWrap
== true) {
1556 * This case happens when: the cursor was already on the
1557 * right margin (either through printing or by an explicit
1558 * placement command), and a character was printed.
1560 * The line wraps only when a new character arrives AND the
1561 * cursor is already on the right margin AND has placed a
1562 * character in its cell. Easier to see than to explain.
1564 if (wrapLineFlag
== false) {
1566 * This block marks the case that we are in the margin
1567 * and the first character has been received and printed.
1569 wrapLineFlag
= true;
1572 * This block marks the case that we are in the margin
1573 * and the second character has been received and
1576 wrapLineFlag
= false;
1580 } else if (currentState
.cursorX
<= rightMargin
) {
1582 * This is the normal case: a character came in and was printed
1583 * to the left of the right margin column.
1586 // Turn off VT100 special-case flag
1587 wrapLineFlag
= false;
1590 // "Print" the character
1591 Cell newCell
= new Cell(ch
);
1592 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1593 newCellAttributes
.setTo(currentState
.attr
);
1594 DisplayLine line
= display
.get(currentState
.cursorY
);
1596 if (StringUtils
.width(ch
) == 1) {
1597 // Insert mode special case
1598 if (insertMode
== true) {
1599 line
.insert(currentState
.cursorX
, newCell
);
1601 // Replace an existing character
1602 line
.replace(currentState
.cursorX
, newCell
);
1605 // Increment horizontal
1606 if (wrapLineFlag
== false) {
1607 currentState
.cursorX
++;
1608 if (currentState
.cursorX
> rightMargin
) {
1609 currentState
.cursorX
--;
1616 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1617 * send to the remote side.
1619 * @param mouse mouse event received from the local user
1621 private void mouse(final TMouseEvent mouse
) {
1624 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1625 mouseProtocol, mouseEncoding, mouse);
1628 if (mouseEncoding
== MouseEncoding
.X10
) {
1629 // We will support X10 but only for (160,94) and smaller.
1630 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1635 switch (mouseProtocol
) {
1642 // Only report button presses
1643 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1649 // Only report button presses and releases
1650 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1651 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1659 * Only report button presses, button releases, and motions that
1660 * have a button down (i.e. drag-and-drop).
1662 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1663 if (!mouse
.isMouse1()
1664 && !mouse
.isMouse2()
1665 && !mouse
.isMouse3()
1666 && !mouse
.isMouseWheelUp()
1667 && !mouse
.isMouseWheelDown()
1675 // Report everything
1679 // Now encode the event
1680 StringBuilder sb
= new StringBuilder(6);
1681 if (mouseEncoding
== MouseEncoding
.SGR
) {
1682 sb
.append((char) 0x1B);
1686 if (mouse
.isMouse1()) {
1687 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1692 } else if (mouse
.isMouse2()) {
1693 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1698 } else if (mouse
.isMouse3()) {
1699 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1704 } else if (mouse
.isMouseWheelUp()) {
1706 } else if (mouse
.isMouseWheelDown()) {
1709 // This is motion with no buttons down.
1712 if (mouse
.isAlt()) {
1715 if (mouse
.isCtrl()) {
1718 if (mouse
.isShift()) {
1722 sb
.append(String
.format("%d;%d;%d", buttons
, mouse
.getX() + 1,
1725 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1732 // X10 and UTF8 encodings
1733 sb
.append((char) 0x1B);
1737 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1738 buttons
= 0x03 + 32;
1739 } else if (mouse
.isMouse1()) {
1740 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1741 buttons
= 0x00 + 32 + 32;
1743 buttons
= 0x00 + 32;
1745 } else if (mouse
.isMouse2()) {
1746 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1747 buttons
= 0x01 + 32 + 32;
1749 buttons
= 0x01 + 32;
1751 } else if (mouse
.isMouse3()) {
1752 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1753 buttons
= 0x02 + 32 + 32;
1755 buttons
= 0x02 + 32;
1757 } else if (mouse
.isMouseWheelUp()) {
1758 buttons
= 0x04 + 64;
1759 } else if (mouse
.isMouseWheelDown()) {
1760 buttons
= 0x05 + 64;
1762 // This is motion with no buttons down.
1763 buttons
= 0x03 + 32;
1765 if (mouse
.isAlt()) {
1768 if (mouse
.isCtrl()) {
1771 if (mouse
.isShift()) {
1775 sb
.append((char) (buttons
& 0xFF));
1776 sb
.append((char) (mouse
.getX() + 33));
1777 sb
.append((char) (mouse
.getY() + 33));
1780 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1781 writeRemote(sb
.toString());
1785 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1786 * send to the remote side.
1788 * @param keypress keypress received from the local user
1790 private void keypress(final TKeypress keypress
) {
1791 writeRemote(keypressToString(keypress
));
1795 * Build one of the complex xterm keystroke sequences, storing the result in
1796 * xterm_keystroke_buffer.
1798 * @param ss3 the prefix to use based on VT100 state.
1799 * @param first the first character, usually a number.
1800 * @param first the last character, one of the following: ~ A B C D F H
1801 * @param ctrl whether or not ctrl is down
1802 * @param alt whether or not alt is down
1803 * @param shift whether or not shift is down
1804 * @return the buffer with the full key sequence
1806 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1807 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1809 StringBuilder sb
= new StringBuilder(ss3
);
1810 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1814 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1816 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1818 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1820 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1822 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1824 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1826 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1831 return sb
.toString();
1835 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1837 * @param keypress keypress received from the local user
1838 * @return string to transmit to the remote side
1840 @SuppressWarnings("fallthrough")
1841 private String
keypressToString(final TKeypress keypress
) {
1843 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1845 * If this is a control character, process it like it came from
1848 if (keypress
.getChar() < 0x20) {
1849 handleControlChar((char) keypress
.getChar());
1851 // Local echo for everything else
1852 printCharacter(keypress
.getChar());
1854 if (displayListener
!= null) {
1855 displayListener
.displayChanged();
1859 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1864 // Handle control characters
1865 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1866 StringBuilder sb
= new StringBuilder();
1867 int ch
= keypress
.getChar();
1869 sb
.append(Character
.toChars(ch
));
1870 return sb
.toString();
1873 // Handle alt characters
1874 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1875 StringBuilder sb
= new StringBuilder("\033");
1876 int ch
= keypress
.getChar();
1877 sb
.append(Character
.toChars(ch
));
1878 return sb
.toString();
1881 if (keypress
.equals(kbBackspaceDel
)) {
1894 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1897 switch (arrowKeyMode
) {
1899 return xtermBuildKeySequence("\033[", '1', 'D',
1900 keypress
.isCtrl(), keypress
.isAlt(),
1901 keypress
.isShift());
1903 return xtermBuildKeySequence("\033", '1', 'D',
1904 keypress
.isCtrl(), keypress
.isAlt(),
1905 keypress
.isShift());
1907 return xtermBuildKeySequence("\033O", '1', 'D',
1908 keypress
.isCtrl(), keypress
.isAlt(),
1909 keypress
.isShift());
1912 switch (arrowKeyMode
) {
1923 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1926 switch (arrowKeyMode
) {
1928 return xtermBuildKeySequence("\033[", '1', 'C',
1929 keypress
.isCtrl(), keypress
.isAlt(),
1930 keypress
.isShift());
1932 return xtermBuildKeySequence("\033", '1', 'C',
1933 keypress
.isCtrl(), keypress
.isAlt(),
1934 keypress
.isShift());
1936 return xtermBuildKeySequence("\033O", '1', 'C',
1937 keypress
.isCtrl(), keypress
.isAlt(),
1938 keypress
.isShift());
1941 switch (arrowKeyMode
) {
1952 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1955 switch (arrowKeyMode
) {
1957 return xtermBuildKeySequence("\033[", '1', 'A',
1958 keypress
.isCtrl(), keypress
.isAlt(),
1959 keypress
.isShift());
1961 return xtermBuildKeySequence("\033", '1', 'A',
1962 keypress
.isCtrl(), keypress
.isAlt(),
1963 keypress
.isShift());
1965 return xtermBuildKeySequence("\033O", '1', 'A',
1966 keypress
.isCtrl(), keypress
.isAlt(),
1967 keypress
.isShift());
1970 switch (arrowKeyMode
) {
1981 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1984 switch (arrowKeyMode
) {
1986 return xtermBuildKeySequence("\033[", '1', 'B',
1987 keypress
.isCtrl(), keypress
.isAlt(),
1988 keypress
.isShift());
1990 return xtermBuildKeySequence("\033", '1', 'B',
1991 keypress
.isCtrl(), keypress
.isAlt(),
1992 keypress
.isShift());
1994 return xtermBuildKeySequence("\033O", '1', 'B',
1995 keypress
.isCtrl(), keypress
.isAlt(),
1996 keypress
.isShift());
1999 switch (arrowKeyMode
) {
2010 if (keypress
.equalsWithoutModifiers(kbHome
)) {
2013 switch (arrowKeyMode
) {
2015 return xtermBuildKeySequence("\033[", '1', 'H',
2016 keypress
.isCtrl(), keypress
.isAlt(),
2017 keypress
.isShift());
2019 return xtermBuildKeySequence("\033", '1', 'H',
2020 keypress
.isCtrl(), keypress
.isAlt(),
2021 keypress
.isShift());
2023 return xtermBuildKeySequence("\033O", '1', 'H',
2024 keypress
.isCtrl(), keypress
.isAlt(),
2025 keypress
.isShift());
2028 switch (arrowKeyMode
) {
2039 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
2042 switch (arrowKeyMode
) {
2044 return xtermBuildKeySequence("\033[", '1', 'F',
2045 keypress
.isCtrl(), keypress
.isAlt(),
2046 keypress
.isShift());
2048 return xtermBuildKeySequence("\033", '1', 'F',
2049 keypress
.isCtrl(), keypress
.isAlt(),
2050 keypress
.isShift());
2052 return xtermBuildKeySequence("\033O", '1', 'F',
2053 keypress
.isCtrl(), keypress
.isAlt(),
2054 keypress
.isShift());
2057 switch (arrowKeyMode
) {
2068 if (keypress
.equals(kbF1
)) {
2076 if (keypress
.equals(kbF2
)) {
2084 if (keypress
.equals(kbF3
)) {
2092 if (keypress
.equals(kbF4
)) {
2100 if (keypress
.equals(kbF5
)) {
2113 if (keypress
.equals(kbF6
)) {
2126 if (keypress
.equals(kbF7
)) {
2139 if (keypress
.equals(kbF8
)) {
2152 if (keypress
.equals(kbF9
)) {
2165 if (keypress
.equals(kbF10
)) {
2178 if (keypress
.equals(kbF11
)) {
2182 if (keypress
.equals(kbF12
)) {
2186 if (keypress
.equals(kbShiftF1
)) {
2191 if (type
== DeviceType
.XTERM
) {
2197 if (keypress
.equals(kbShiftF2
)) {
2202 if (type
== DeviceType
.XTERM
) {
2208 if (keypress
.equals(kbShiftF3
)) {
2213 if (type
== DeviceType
.XTERM
) {
2219 if (keypress
.equals(kbShiftF4
)) {
2224 if (type
== DeviceType
.XTERM
) {
2230 if (keypress
.equals(kbShiftF5
)) {
2232 return "\033[15;2~";
2235 if (keypress
.equals(kbShiftF6
)) {
2237 return "\033[17;2~";
2240 if (keypress
.equals(kbShiftF7
)) {
2242 return "\033[18;2~";
2245 if (keypress
.equals(kbShiftF8
)) {
2247 return "\033[19;2~";
2250 if (keypress
.equals(kbShiftF9
)) {
2252 return "\033[20;2~";
2255 if (keypress
.equals(kbShiftF10
)) {
2257 return "\033[21;2~";
2260 if (keypress
.equals(kbShiftF11
)) {
2262 return "\033[23;2~";
2265 if (keypress
.equals(kbShiftF12
)) {
2267 return "\033[24;2~";
2270 if (keypress
.equals(kbCtrlF1
)) {
2275 if (type
== DeviceType
.XTERM
) {
2281 if (keypress
.equals(kbCtrlF2
)) {
2286 if (type
== DeviceType
.XTERM
) {
2292 if (keypress
.equals(kbCtrlF3
)) {
2297 if (type
== DeviceType
.XTERM
) {
2303 if (keypress
.equals(kbCtrlF4
)) {
2308 if (type
== DeviceType
.XTERM
) {
2314 if (keypress
.equals(kbCtrlF5
)) {
2316 return "\033[15;5~";
2319 if (keypress
.equals(kbCtrlF6
)) {
2321 return "\033[17;5~";
2324 if (keypress
.equals(kbCtrlF7
)) {
2326 return "\033[18;5~";
2329 if (keypress
.equals(kbCtrlF8
)) {
2331 return "\033[19;5~";
2334 if (keypress
.equals(kbCtrlF9
)) {
2336 return "\033[20;5~";
2339 if (keypress
.equals(kbCtrlF10
)) {
2341 return "\033[21;5~";
2344 if (keypress
.equals(kbCtrlF11
)) {
2346 return "\033[23;5~";
2349 if (keypress
.equals(kbCtrlF12
)) {
2351 return "\033[24;5~";
2354 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2357 return xtermBuildKeySequence("\033[", '5', '~',
2358 keypress
.isCtrl(), keypress
.isAlt(),
2359 keypress
.isShift());
2365 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2368 return xtermBuildKeySequence("\033[", '6', '~',
2369 keypress
.isCtrl(), keypress
.isAlt(),
2370 keypress
.isShift());
2376 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2379 return xtermBuildKeySequence("\033[", '2', '~',
2380 keypress
.isCtrl(), keypress
.isAlt(),
2381 keypress
.isShift());
2387 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2390 return xtermBuildKeySequence("\033[", '3', '~',
2391 keypress
.isCtrl(), keypress
.isAlt(),
2392 keypress
.isShift());
2394 // Delete sends real delete for VTxxx
2399 if (keypress
.equals(kbEnter
)) {
2403 if (keypress
.equals(kbEsc
)) {
2407 if (keypress
.equals(kbAltEsc
)) {
2411 if (keypress
.equals(kbTab
)) {
2415 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2416 (keypress
.equals(kbShiftTab
))
2426 // Non-alt, non-ctrl characters
2427 if (!keypress
.isFnKey()) {
2428 StringBuilder sb
= new StringBuilder();
2429 sb
.append(Character
.toChars(keypress
.getChar()));
2430 return sb
.toString();
2436 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2439 * @param ch 8-bit character from the remote side
2440 * @param charsetGl character set defined for GL
2441 * @param charsetGr character set defined for GR
2442 * @return character to display on the screen
2444 private char mapCharacterCharset(final int ch
,
2445 final CharacterSet charsetGl
,
2446 final CharacterSet charsetGr
) {
2448 int lookupChar
= ch
;
2449 CharacterSet lookupCharset
= charsetGl
;
2452 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2453 lookupCharset
= charsetGr
;
2457 switch (lookupCharset
) {
2460 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2463 return DECCharacterSets
.UK
[lookupChar
];
2466 return DECCharacterSets
.US_ASCII
[lookupChar
];
2469 return DECCharacterSets
.NL
[lookupChar
];
2472 return DECCharacterSets
.FI
[lookupChar
];
2475 return DECCharacterSets
.FR
[lookupChar
];
2478 return DECCharacterSets
.FR_CA
[lookupChar
];
2481 return DECCharacterSets
.DE
[lookupChar
];
2484 return DECCharacterSets
.IT
[lookupChar
];
2487 return DECCharacterSets
.NO
[lookupChar
];
2490 return DECCharacterSets
.ES
[lookupChar
];
2493 return DECCharacterSets
.SV
[lookupChar
];
2496 return DECCharacterSets
.SWISS
[lookupChar
];
2498 case DEC_SUPPLEMENTAL
:
2499 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2502 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2505 return DECCharacterSets
.US_ASCII
[lookupChar
];
2508 return DECCharacterSets
.US_ASCII
[lookupChar
];
2511 throw new IllegalArgumentException("Invalid character set value: "
2517 * Map an 8-bit byte into a printable character.
2519 * @param ch either 8-bit or Unicode character from the remote side
2520 * @return character to display on the screen
2522 private int mapCharacter(final int ch
) {
2524 // Unicode character, just return it
2528 CharacterSet charsetGl
= currentState
.g0Charset
;
2529 CharacterSet charsetGr
= currentState
.grCharset
;
2531 if (vt52Mode
== true) {
2532 if (shiftOut
== true) {
2533 // Shifted out character, pull from VT52 graphics
2534 charsetGl
= currentState
.g1Charset
;
2535 charsetGr
= CharacterSet
.US
;
2538 charsetGl
= currentState
.g0Charset
;
2539 charsetGr
= CharacterSet
.US
;
2542 // Pull the character
2543 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2547 if (shiftOut
== true) {
2548 // Shifted out character, pull from G1
2549 charsetGl
= currentState
.g1Charset
;
2550 charsetGr
= currentState
.grCharset
;
2552 // Pull the character
2553 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2557 if (singleshift
== Singleshift
.SS2
) {
2559 singleshift
= Singleshift
.NONE
;
2561 // Shifted out character, pull from G2
2562 charsetGl
= currentState
.g2Charset
;
2563 charsetGr
= currentState
.grCharset
;
2567 if (singleshift
== Singleshift
.SS3
) {
2569 singleshift
= Singleshift
.NONE
;
2571 // Shifted out character, pull from G3
2572 charsetGl
= currentState
.g3Charset
;
2573 charsetGr
= currentState
.grCharset
;
2576 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2577 // Check for locking shift
2579 switch (currentState
.glLockshift
) {
2582 throw new IllegalArgumentException("programming bug");
2585 throw new IllegalArgumentException("programming bug");
2588 throw new IllegalArgumentException("programming bug");
2592 charsetGl
= currentState
.g2Charset
;
2597 charsetGl
= currentState
.g3Charset
;
2602 charsetGl
= currentState
.g0Charset
;
2606 switch (currentState
.grLockshift
) {
2609 throw new IllegalArgumentException("programming bug");
2612 throw new IllegalArgumentException("programming bug");
2616 charsetGr
= currentState
.g1Charset
;
2621 charsetGr
= currentState
.g2Charset
;
2626 charsetGr
= currentState
.g3Charset
;
2631 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2638 // Pull the character
2639 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2643 * Scroll the text within a scrolling region up n lines.
2645 * @param regionTop top row of the scrolling region
2646 * @param regionBottom bottom row of the scrolling region
2647 * @param n number of lines to scroll
2649 private void scrollingRegionScrollUp(final int regionTop
,
2650 final int regionBottom
, final int n
) {
2652 if (regionTop
>= regionBottom
) {
2656 // Sanity check: see if there will be any characters left after the
2658 if (regionBottom
+ 1 - regionTop
<= n
) {
2659 // There won't be anything left in the region, so just call
2660 // eraseScreen() and return.
2661 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2665 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2666 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2667 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2669 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2670 - remaining
, regionBottom
+ 1);
2671 display
= new ArrayList
<DisplayLine
>(displayTop
);
2672 display
.addAll(displayMiddle
);
2673 for (int i
= 0; i
< n
; i
++) {
2674 DisplayLine line
= new DisplayLine(currentState
.attr
);
2675 line
.setReverseColor(reverseVideo
);
2678 display
.addAll(displayBottom
);
2680 assert (display
.size() == height
);
2684 * Scroll the text within a scrolling region down n lines.
2686 * @param regionTop top row of the scrolling region
2687 * @param regionBottom bottom row of the scrolling region
2688 * @param n number of lines to scroll
2690 private void scrollingRegionScrollDown(final int regionTop
,
2691 final int regionBottom
, final int n
) {
2693 if (regionTop
>= regionBottom
) {
2697 // Sanity check: see if there will be any characters left after the
2699 if (regionBottom
+ 1 - regionTop
<= n
) {
2700 // There won't be anything left in the region, so just call
2701 // eraseScreen() and return.
2702 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2706 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2707 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2708 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2710 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2711 regionTop
+ remaining
);
2712 display
= new ArrayList
<DisplayLine
>(displayTop
);
2713 for (int i
= 0; i
< n
; i
++) {
2714 DisplayLine line
= new DisplayLine(currentState
.attr
);
2715 line
.setReverseColor(reverseVideo
);
2718 display
.addAll(displayMiddle
);
2719 display
.addAll(displayBottom
);
2721 assert (display
.size() == height
);
2725 * Process a control character.
2727 * @param ch 8-bit character from the remote side
2729 private void handleControlChar(final char ch
) {
2730 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2741 // Transmit the answerback message.
2752 cursorLeft(1, false);
2757 advanceToNextTabStop();
2783 currentState
.glLockshift
= LockshiftMode
.NONE
;
2789 currentState
.glLockshift
= LockshiftMode
.NONE
;
2814 singleshift
= Singleshift
.SS2
;
2819 singleshift
= Singleshift
.SS3
;
2829 * Advance the cursor to the next tab stop.
2831 private void advanceToNextTabStop() {
2832 if (tabStops
.size() == 0) {
2833 // Go to the rightmost column
2834 cursorRight(rightMargin
- currentState
.cursorX
, false);
2837 for (Integer stop
: tabStops
) {
2838 if (stop
> currentState
.cursorX
) {
2839 cursorRight(stop
- currentState
.cursorX
, false);
2844 * We got here, meaning there isn't a tab stop beyond the current
2845 * cursor position. Place the cursor of the right-most edge of the
2848 cursorRight(rightMargin
- currentState
.cursorX
, false);
2852 * Save a character into the collect buffer.
2854 * @param ch character to save
2856 private void collect(final char ch
) {
2857 collectBuffer
.append(ch
);
2861 * Save a byte into the CSI parameters buffer.
2863 * @param ch byte to save
2865 private void param(final byte ch
) {
2866 if (csiParams
.size() == 0) {
2867 csiParams
.add(Integer
.valueOf(0));
2869 Integer x
= csiParams
.get(csiParams
.size() - 1);
2870 if ((ch
>= '0') && (ch
<= '9')) {
2873 csiParams
.set(csiParams
.size() - 1, x
);
2876 if ((ch
== ';') && (csiParams
.size() < 16)) {
2877 csiParams
.add(Integer
.valueOf(0));
2882 * Get a CSI parameter value, with a default.
2884 * @param position parameter index. 0 is the first parameter.
2885 * @param defaultValue value to use if csiParams[position] doesn't exist
2886 * @return parameter value
2888 private int getCsiParam(final int position
, final int defaultValue
) {
2889 if (csiParams
.size() < position
+ 1) {
2890 return defaultValue
;
2892 return csiParams
.get(position
).intValue();
2896 * Get a CSI parameter value, clamped to within min/max.
2898 * @param position parameter index. 0 is the first parameter.
2899 * @param defaultValue value to use if csiParams[position] doesn't exist
2900 * @param minValue minimum value inclusive
2901 * @param maxValue maximum value inclusive
2902 * @return parameter value
2904 private int getCsiParam(final int position
, final int defaultValue
,
2905 final int minValue
, final int maxValue
) {
2907 assert (minValue
<= maxValue
);
2908 int value
= getCsiParam(position
, defaultValue
);
2909 if (value
< minValue
) {
2912 if (value
> maxValue
) {
2919 * Set or unset a toggle.
2921 * @param value true for set ('h'), false for reset ('l')
2923 private void setToggle(final boolean value
) {
2924 boolean decPrivateModeFlag
= false;
2926 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2927 if (collectBuffer
.charAt(i
) == '?') {
2928 decPrivateModeFlag
= true;
2933 for (Integer i
: csiParams
) {
2938 if (decPrivateModeFlag
== true) {
2940 if (value
== true) {
2941 // Use application arrow keys
2942 arrowKeyMode
= ArrowKeyMode
.VT100
;
2944 // Use ANSI arrow keys
2945 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2950 if (decPrivateModeFlag
== true) {
2951 if (value
== false) {
2955 arrowKeyMode
= ArrowKeyMode
.VT52
;
2958 * From the VT102 docs: "You use ANSI mode to select
2959 * most terminal features; the terminal uses the same
2960 * features when it switches to VT52 mode. You
2961 * cannot, however, change most of these features in
2964 * In other words, do not reset any other attributes
2965 * when switching between VT52 submode and ANSI.
2967 * HOWEVER, the real vt100 does switch the character
2968 * set according to Usenet.
2970 currentState
.g0Charset
= CharacterSet
.US
;
2971 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2974 if ((type
== DeviceType
.VT220
)
2975 || (type
== DeviceType
.XTERM
)) {
2977 // VT52 mode is explicitly 7-bit
2979 singleshift
= Singleshift
.NONE
;
2984 if (value
== true) {
2985 // Turn off keyboard
2994 if (decPrivateModeFlag
== true) {
2996 if (value
== true) {
3003 if ((displayListener
!= null)
3004 && (type
== DeviceType
.XTERM
)
3006 // For xterms, reset to the actual width, not 80
3008 width
= displayListener
.getDisplayWidth();
3009 rightMargin
= width
- 1;
3012 width
= rightMargin
+ 1;
3015 // Entire screen is cleared, and scrolling region is
3017 eraseScreen(0, 0, height
- 1, width
- 1, false);
3018 scrollRegionTop
= 0;
3019 scrollRegionBottom
= height
- 1;
3020 // Also home the cursor
3021 cursorPosition(0, 0);
3025 if (decPrivateModeFlag
== true) {
3027 if (value
== true) {
3036 if (value
== true) {
3044 if (decPrivateModeFlag
== true) {
3046 if (value
== true) {
3048 * Set selects reverse screen, a white screen
3049 * background with black characters.
3051 if (reverseVideo
!= true) {
3053 * If in normal video, switch it back
3055 invertDisplayColors();
3057 reverseVideo
= true;
3060 * Reset selects normal screen, a black screen
3061 * background with white characters.
3063 if (reverseVideo
== true) {
3065 * If in reverse video already, switch it back
3067 invertDisplayColors();
3069 reverseVideo
= false;
3074 if (decPrivateModeFlag
== true) {
3076 if (value
== true) {
3077 // Origin is relative to scroll region cursor.
3078 // Cursor can NEVER leave scrolling region.
3079 currentState
.originMode
= true;
3080 cursorPosition(0, 0);
3082 // Origin is absolute to entire screen. Cursor can
3083 // leave the scrolling region via cup() and hvp().
3084 currentState
.originMode
= false;
3085 cursorPosition(0, 0);
3090 if (decPrivateModeFlag
== true) {
3092 if (value
== true) {
3094 currentState
.lineWrap
= true;
3096 // Turn linewrap off
3097 currentState
.lineWrap
= false;
3102 if (decPrivateModeFlag
== true) {
3104 if (value
== true) {
3105 // Keyboard auto-repeat on
3108 // Keyboard auto-repeat off
3114 if (decPrivateModeFlag
== false) {
3116 if (value
== true) {
3126 if (decPrivateModeFlag
== true) {
3132 if (decPrivateModeFlag
== true) {
3138 if (decPrivateModeFlag
== false) {
3140 if (value
== true) {
3142 * Set causes a received linefeed, form feed, or
3143 * vertical tab to move cursor to first column of
3144 * next line. RETURN transmits both a carriage return
3145 * and linefeed. This selection is also called new
3151 * Reset causes a received linefeed, form feed, or
3152 * vertical tab to move cursor to next line in
3153 * current column. RETURN transmits a carriage
3156 newLineMode
= false;
3162 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3163 if (decPrivateModeFlag
== true) {
3165 if (value
== true) {
3167 cursorVisible
= true;
3170 cursorVisible
= false;
3177 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3178 if (decPrivateModeFlag
== true) {
3180 if (value
== true) {
3181 // Select national mode NRC
3184 // Select multi-national mode
3193 if (type
== DeviceType
.XTERM
) {
3194 if (decPrivateModeFlag
== true) {
3195 if (value
== true) {
3196 // Enable sixel scrolling (default).
3199 // Disable sixel scrolling.
3208 if ((type
== DeviceType
.XTERM
)
3209 && (decPrivateModeFlag
== true)
3211 // Mouse: normal tracking mode
3212 if (value
== true) {
3213 mouseProtocol
= MouseProtocol
.NORMAL
;
3215 mouseProtocol
= MouseProtocol
.OFF
;
3221 if ((type
== DeviceType
.XTERM
)
3222 && (decPrivateModeFlag
== true)
3224 // Mouse: normal tracking mode
3225 if (value
== true) {
3226 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
3228 mouseProtocol
= MouseProtocol
.OFF
;
3234 if ((type
== DeviceType
.XTERM
)
3235 && (decPrivateModeFlag
== true)
3237 // Mouse: Any-event tracking mode
3238 if (value
== true) {
3239 mouseProtocol
= MouseProtocol
.ANYEVENT
;
3241 mouseProtocol
= MouseProtocol
.OFF
;
3247 if ((type
== DeviceType
.XTERM
)
3248 && (decPrivateModeFlag
== true)
3250 // Mouse: UTF-8 coordinates
3251 if (value
== true) {
3252 mouseEncoding
= MouseEncoding
.UTF8
;
3254 mouseEncoding
= MouseEncoding
.X10
;
3260 if ((type
== DeviceType
.XTERM
)
3261 && (decPrivateModeFlag
== true)
3263 // Mouse: SGR coordinates
3264 if (value
== true) {
3265 mouseEncoding
= MouseEncoding
.SGR
;
3267 mouseEncoding
= MouseEncoding
.X10
;
3273 if (type
== DeviceType
.XTERM
) {
3274 if (decPrivateModeFlag
== true) {
3275 if (value
== true) {
3276 // Use private color registers for each sixel
3277 // graphic (default).
3278 sixelPalette
= null;
3280 // Use shared color registers for each sixel
3282 sixelPalette
= new HashMap
<Integer
, java
.awt
.Color
>();
3296 * DECSC - Save cursor.
3298 private void decsc() {
3299 savedState
.setTo(currentState
);
3303 * DECRC - Restore cursor.
3305 private void decrc() {
3306 currentState
.setTo(savedState
);
3312 private void ind() {
3313 // Move the cursor and scroll if necessary. If at the bottom line
3314 // already, a scroll up is supposed to be performed.
3315 if (currentState
.cursorY
== scrollRegionBottom
) {
3316 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3318 cursorDown(1, true);
3322 * RI - Reverse index.
3325 // Move the cursor and scroll if necessary. If at the top line
3326 // already, a scroll down is supposed to be performed.
3327 if (currentState
.cursorY
== scrollRegionTop
) {
3328 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3336 private void nel() {
3337 // Move the cursor and scroll if necessary. If at the bottom line
3338 // already, a scroll up is supposed to be performed.
3339 if (currentState
.cursorY
== scrollRegionBottom
) {
3340 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3342 cursorDown(1, true);
3344 // Reset to the beginning of the next line
3345 currentState
.cursorX
= 0;
3349 * DECKPAM - Keypad application mode.
3351 private void deckpam() {
3352 keypadMode
= KeypadMode
.Application
;
3356 * DECKPNM - Keypad numeric mode.
3358 private void deckpnm() {
3359 keypadMode
= KeypadMode
.Numeric
;
3365 * @param n number of spaces to move
3366 * @param honorScrollRegion if true, then do nothing if the cursor is
3367 * outside the scrolling region
3369 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3373 * Special case: if a user moves the cursor from the right margin, we
3374 * have to reset the VT100 right margin flag.
3377 wrapLineFlag
= false;
3380 for (int i
= 0; i
< n
; i
++) {
3381 if (honorScrollRegion
== true) {
3382 // Honor the scrolling region
3383 if ((currentState
.cursorY
< scrollRegionTop
)
3384 || (currentState
.cursorY
> scrollRegionBottom
)
3386 // Outside region, do nothing
3389 // Inside region, go up
3390 top
= scrollRegionTop
;
3392 // Non-scrolling case
3396 if (currentState
.cursorY
> top
) {
3397 currentState
.cursorY
--;
3403 * Move down n spaces.
3405 * @param n number of spaces to move
3406 * @param honorScrollRegion if true, then do nothing if the cursor is
3407 * outside the scrolling region
3409 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3413 * Special case: if a user moves the cursor from the right margin, we
3414 * have to reset the VT100 right margin flag.
3417 wrapLineFlag
= false;
3420 for (int i
= 0; i
< n
; i
++) {
3422 if (honorScrollRegion
== true) {
3423 // Honor the scrolling region
3424 if (currentState
.cursorY
> scrollRegionBottom
) {
3425 // Outside region, do nothing
3428 // Inside region, go down
3429 bottom
= scrollRegionBottom
;
3431 // Non-scrolling case
3432 bottom
= height
- 1;
3435 if (currentState
.cursorY
< bottom
) {
3436 currentState
.cursorY
++;
3442 * Move left n spaces.
3444 * @param n number of spaces to move
3445 * @param honorScrollRegion if true, then do nothing if the cursor is
3446 * outside the scrolling region
3448 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3450 * Special case: if a user moves the cursor from the right margin, we
3451 * have to reset the VT100 right margin flag.
3454 wrapLineFlag
= false;
3457 for (int i
= 0; i
< n
; i
++) {
3458 if (honorScrollRegion
== true) {
3459 // Honor the scrolling region
3460 if ((currentState
.cursorY
< scrollRegionTop
)
3461 || (currentState
.cursorY
> scrollRegionBottom
)
3463 // Outside region, do nothing
3468 if (currentState
.cursorX
> 0) {
3469 currentState
.cursorX
--;
3475 * Move right n spaces.
3477 * @param n number of spaces to move
3478 * @param honorScrollRegion if true, then do nothing if the cursor is
3479 * outside the scrolling region
3481 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3482 int rightMargin
= this.rightMargin
;
3485 * Special case: if a user moves the cursor from the right margin, we
3486 * have to reset the VT100 right margin flag.
3489 wrapLineFlag
= false;
3492 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3493 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3496 for (int i
= 0; i
< n
; i
++) {
3497 if (honorScrollRegion
== true) {
3498 // Honor the scrolling region
3499 if ((currentState
.cursorY
< scrollRegionTop
)
3500 || (currentState
.cursorY
> scrollRegionBottom
)
3502 // Outside region, do nothing
3507 if (currentState
.cursorX
< rightMargin
) {
3508 currentState
.cursorX
++;
3514 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3516 * @param row row to move to
3517 * @param col column to move to
3519 private void cursorPosition(int row
, final int col
) {
3520 int rightMargin
= this.rightMargin
;
3525 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3526 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3529 // Set column number
3530 currentState
.cursorX
= col
;
3532 // Sanity check, bring column back to margin.
3533 if (currentState
.cursorX
> rightMargin
) {
3534 currentState
.cursorX
= rightMargin
;
3538 if (currentState
.originMode
== true) {
3539 row
+= scrollRegionTop
;
3541 if (currentState
.cursorY
< row
) {
3542 cursorDown(row
- currentState
.cursorY
, false);
3543 } else if (currentState
.cursorY
> row
) {
3544 cursorUp(currentState
.cursorY
- row
, false);
3547 wrapLineFlag
= false;
3551 * HTS - Horizontal tabulation set.
3553 private void hts() {
3554 for (Integer stop
: tabStops
) {
3555 if (stop
== currentState
.cursorX
) {
3556 // Already have a tab stop here
3561 // Append a tab stop to the end of the array and resort them
3562 tabStops
.add(currentState
.cursorX
);
3563 Collections
.sort(tabStops
);
3567 * DECSWL - Single-width line.
3569 private void decswl() {
3570 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3571 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3575 * DECDWL - Double-width line.
3577 private void decdwl() {
3578 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3579 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3583 * DECHDL - Double-height + double-width line.
3585 * @param topHalf if true, this sets the row to be the top half row of a
3588 private void dechdl(final boolean topHalf
) {
3589 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3590 if (topHalf
== true) {
3591 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3593 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3598 * DECALN - Screen alignment display.
3600 private void decaln() {
3601 Cell newCell
= new Cell('E');
3602 for (DisplayLine line
: display
) {
3603 for (int i
= 0; i
< line
.length(); i
++) {
3604 line
.replace(i
, newCell
);
3610 * DECSCL - Compatibility level.
3612 private void decscl() {
3613 int i
= getCsiParam(0, 0);
3614 int j
= getCsiParam(1, 0);
3618 currentState
.g0Charset
= CharacterSet
.US
;
3619 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3621 } else if (i
== 62) {
3623 if ((j
== 0) || (j
== 2)) {
3625 } else if (j
== 1) {
3632 * CUD - Cursor down.
3634 private void cud() {
3635 cursorDown(getCsiParam(0, 1, 1, height
), true);
3639 * CUF - Cursor forward.
3641 private void cuf() {
3642 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3646 * CUB - Cursor backward.
3648 private void cub() {
3649 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3655 private void cuu() {
3656 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3660 * CUP - Cursor position.
3662 private void cup() {
3663 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3664 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3668 * CNL - Cursor down and to column 1.
3670 private void cnl() {
3671 cursorDown(getCsiParam(0, 1, 1, height
), true);
3673 cursorLeft(currentState
.cursorX
, true);
3677 * CPL - Cursor up and to column 1.
3679 private void cpl() {
3680 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3682 cursorLeft(currentState
.cursorX
, true);
3686 * CHA - Cursor to column # in current row.
3688 private void cha() {
3689 cursorPosition(currentState
.cursorY
,
3690 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3694 * VPA - Cursor to row #, same column.
3696 private void vpa() {
3697 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3698 currentState
.cursorX
);
3702 * ED - Erase in display.
3705 boolean honorProtected
= false;
3706 boolean decPrivateModeFlag
= false;
3708 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3709 if (collectBuffer
.charAt(i
) == '?') {
3710 decPrivateModeFlag
= true;
3715 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3716 && (decPrivateModeFlag
== true)
3718 honorProtected
= true;
3721 int i
= getCsiParam(0, 0);
3724 // Erase from here to end of screen
3725 if (currentState
.cursorY
< height
- 1) {
3726 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3729 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3730 } else if (i
== 1) {
3731 // Erase from beginning of screen to here
3732 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3734 eraseLine(0, currentState
.cursorX
, honorProtected
);
3735 } else if (i
== 2) {
3736 // Erase entire screen
3737 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3742 * EL - Erase in line.
3745 boolean honorProtected
= false;
3746 boolean decPrivateModeFlag
= false;
3748 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3749 if (collectBuffer
.charAt(i
) == '?') {
3750 decPrivateModeFlag
= true;
3755 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3756 && (decPrivateModeFlag
== true)
3758 honorProtected
= true;
3761 int i
= getCsiParam(0, 0);
3764 // Erase from here to end of line
3765 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3766 } else if (i
== 1) {
3767 // Erase from beginning of line to here
3768 eraseLine(0, currentState
.cursorX
, honorProtected
);
3769 } else if (i
== 2) {
3770 // Erase entire line
3771 eraseLine(0, width
- 1, honorProtected
);
3776 * ECH - Erase # of characters in current row.
3778 private void ech() {
3779 int i
= getCsiParam(0, 1, 1, width
);
3781 // Erase from here to i characters
3782 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3789 int i
= getCsiParam(0, 1);
3791 if ((currentState
.cursorY
>= scrollRegionTop
)
3792 && (currentState
.cursorY
<= scrollRegionBottom
)
3795 // I can get the same effect with a scroll-down
3796 scrollingRegionScrollDown(currentState
.cursorY
,
3797 scrollRegionBottom
, i
);
3802 * DCH - Delete char.
3804 private void dch() {
3805 int n
= getCsiParam(0, 1);
3806 DisplayLine line
= display
.get(currentState
.cursorY
);
3807 Cell blank
= new Cell();
3808 for (int i
= 0; i
< n
; i
++) {
3809 line
.delete(currentState
.cursorX
, blank
);
3814 * ICH - Insert blank char at cursor.
3816 private void ich() {
3817 int n
= getCsiParam(0, 1);
3818 DisplayLine line
= display
.get(currentState
.cursorY
);
3819 Cell blank
= new Cell();
3820 for (int i
= 0; i
< n
; i
++) {
3821 line
.insert(currentState
.cursorX
, blank
);
3829 int i
= getCsiParam(0, 1);
3831 if ((currentState
.cursorY
>= scrollRegionTop
)
3832 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3834 // I can get the same effect with a scroll-down
3835 scrollingRegionScrollUp(currentState
.cursorY
,
3836 scrollRegionBottom
, i
);
3841 * HVP - Horizontal and vertical position.
3843 private void hvp() {
3848 * REP - Repeat character.
3850 private void rep() {
3851 int n
= getCsiParam(0, 1);
3852 for (int i
= 0; i
< n
; i
++) {
3853 printCharacter(repCh
);
3861 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3862 getCsiParam(0, 1, 1, height
));
3869 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3870 getCsiParam(0, 1, 1, height
));
3874 * CBT - Go back X tab stops.
3876 private void cbt() {
3877 int tabsToMove
= getCsiParam(0, 1);
3880 for (int i
= 0; i
< tabsToMove
; i
++) {
3881 int j
= currentState
.cursorX
;
3882 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3883 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3891 j
= tabStops
.get(tabI
);
3893 cursorPosition(currentState
.cursorY
, j
);
3898 * CHT - Advance X tab stops.
3900 private void cht() {
3901 int n
= getCsiParam(0, 1);
3902 for (int i
= 0; i
< n
; i
++) {
3903 advanceToNextTabStop();
3908 * SGR - Select graphics rendition.
3910 private void sgr() {
3912 if (csiParams
.size() == 0) {
3913 currentState
.attr
.reset();
3917 int sgrColorMode
= -1;
3918 boolean idx88Color
= false;
3919 boolean rgbColor
= false;
3923 for (Integer i
: csiParams
) {
3925 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
3927 assert (type
== DeviceType
.XTERM
);
3931 * Indexed color mode, we now have the index number.
3933 if (sgrColorMode
== 38) {
3934 currentState
.attr
.setForeColorRGB(get88Color(i
));
3936 assert (sgrColorMode
== 48);
3937 currentState
.attr
.setBackColorRGB(get88Color(i
));
3946 * RGB color mode, we are collecting tokens.
3950 } else if (rgbGreen
== -1) {
3951 rgbGreen
= i
& 0xFF;
3953 int rgb
= rgbRed
<< 16;
3954 rgb
|= rgbGreen
<< 8;
3957 // System.err.printf("RGB: %08x\n", rgb);
3959 if (sgrColorMode
== 38) {
3960 currentState
.attr
.setForeColorRGB(rgb
);
3962 assert (sgrColorMode
== 48);
3963 currentState
.attr
.setBackColorRGB(rgb
);
3984 * Indexed color mode.
3991 * This is neither indexed nor RGB color. Bail out.
3996 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
4002 currentState
.attr
.reset();
4007 currentState
.attr
.setBold(true);
4012 currentState
.attr
.setUnderline(true);
4017 currentState
.attr
.setBlink(true);
4022 currentState
.attr
.setReverse(true);
4029 if (type
== DeviceType
.XTERM
) {
4039 // Set black foreground
4040 currentState
.attr
.setForeColorRGB(get88Color(8));
4043 // Set red foreground
4044 currentState
.attr
.setForeColorRGB(get88Color(9));
4047 // Set green foreground
4048 currentState
.attr
.setForeColorRGB(get88Color(10));
4051 // Set yellow foreground
4052 currentState
.attr
.setForeColorRGB(get88Color(11));
4055 // Set blue foreground
4056 currentState
.attr
.setForeColorRGB(get88Color(12));
4059 // Set magenta foreground
4060 currentState
.attr
.setForeColorRGB(get88Color(13));
4063 // Set cyan foreground
4064 currentState
.attr
.setForeColorRGB(get88Color(14));
4067 // Set white foreground
4068 currentState
.attr
.setForeColorRGB(get88Color(15));
4072 // Set black background
4073 currentState
.attr
.setBackColorRGB(get88Color(8));
4076 // Set red background
4077 currentState
.attr
.setBackColorRGB(get88Color(9));
4080 // Set green background
4081 currentState
.attr
.setBackColorRGB(get88Color(10));
4084 // Set yellow background
4085 currentState
.attr
.setBackColorRGB(get88Color(11));
4088 // Set blue background
4089 currentState
.attr
.setBackColorRGB(get88Color(12));
4092 // Set magenta background
4093 currentState
.attr
.setBackColorRGB(get88Color(13));
4096 // Set cyan background
4097 currentState
.attr
.setBackColorRGB(get88Color(14));
4100 // Set white background
4101 currentState
.attr
.setBackColorRGB(get88Color(15));
4109 if ((type
== DeviceType
.VT220
)
4110 || (type
== DeviceType
.XTERM
)) {
4116 currentState
.attr
.setBold(false);
4121 currentState
.attr
.setUnderline(false);
4126 currentState
.attr
.setBlink(false);
4131 currentState
.attr
.setReverse(false);
4139 // A true VT100/102/220 does not support color, however everyone
4140 // is used to their terminal emulator supporting color so we will
4141 // unconditionally support color for all DeviceType's.
4146 // Set black foreground
4147 currentState
.attr
.setForeColor(Color
.BLACK
);
4148 currentState
.attr
.setForeColorRGB(-1);
4151 // Set red foreground
4152 currentState
.attr
.setForeColor(Color
.RED
);
4153 currentState
.attr
.setForeColorRGB(-1);
4156 // Set green foreground
4157 currentState
.attr
.setForeColor(Color
.GREEN
);
4158 currentState
.attr
.setForeColorRGB(-1);
4161 // Set yellow foreground
4162 currentState
.attr
.setForeColor(Color
.YELLOW
);
4163 currentState
.attr
.setForeColorRGB(-1);
4166 // Set blue foreground
4167 currentState
.attr
.setForeColor(Color
.BLUE
);
4168 currentState
.attr
.setForeColorRGB(-1);
4171 // Set magenta foreground
4172 currentState
.attr
.setForeColor(Color
.MAGENTA
);
4173 currentState
.attr
.setForeColorRGB(-1);
4176 // Set cyan foreground
4177 currentState
.attr
.setForeColor(Color
.CYAN
);
4178 currentState
.attr
.setForeColorRGB(-1);
4181 // Set white foreground
4182 currentState
.attr
.setForeColor(Color
.WHITE
);
4183 currentState
.attr
.setForeColorRGB(-1);
4186 if (type
== DeviceType
.XTERM
) {
4188 * Xterm supports T.416 / ISO-8613-3 codes to select
4189 * either an indexed color or an RGB value. (It also
4190 * permits these ISO-8613-3 SGR sequences to be separated
4191 * by colons rather than semicolons.)
4193 * We will support only the following:
4195 * 1. Indexed color mode (88- or 256-color modes).
4199 * These cover most of the use cases in the real world.
4201 * HOWEVER, note that this is an awful broken "standard",
4202 * with no way to do it "right". See
4203 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4204 * for a detailed discussion of the current state of RGB
4205 * in various terminals, the point of which is that none
4206 * of them really do the same thing despite all appearing
4210 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4211 * where it is assumed that supporting just the "indexed
4212 * mode" of these sequences (which could align easily
4213 * with existing SGR colors) is assumed to mean full
4214 * support of 24-bit RGB. So it is all or nothing.
4216 * Finally, these sequences break the assumptions of
4217 * standard ECMA-48 style parsers as pointed out at
4218 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4219 * Therefore in order to keep a clean display, we cannot
4220 * parse anything else in this sequence.
4225 // Underscore on, default foreground color
4226 currentState
.attr
.setUnderline(true);
4227 currentState
.attr
.setForeColor(Color
.WHITE
);
4231 // Underscore off, default foreground color
4232 currentState
.attr
.setUnderline(false);
4233 currentState
.attr
.setForeColor(Color
.WHITE
);
4234 currentState
.attr
.setForeColorRGB(-1);
4237 // Set black background
4238 currentState
.attr
.setBackColor(Color
.BLACK
);
4239 currentState
.attr
.setBackColorRGB(-1);
4242 // Set red background
4243 currentState
.attr
.setBackColor(Color
.RED
);
4244 currentState
.attr
.setBackColorRGB(-1);
4247 // Set green background
4248 currentState
.attr
.setBackColor(Color
.GREEN
);
4249 currentState
.attr
.setBackColorRGB(-1);
4252 // Set yellow background
4253 currentState
.attr
.setBackColor(Color
.YELLOW
);
4254 currentState
.attr
.setBackColorRGB(-1);
4257 // Set blue background
4258 currentState
.attr
.setBackColor(Color
.BLUE
);
4259 currentState
.attr
.setBackColorRGB(-1);
4262 // Set magenta background
4263 currentState
.attr
.setBackColor(Color
.MAGENTA
);
4264 currentState
.attr
.setBackColorRGB(-1);
4267 // Set cyan background
4268 currentState
.attr
.setBackColor(Color
.CYAN
);
4269 currentState
.attr
.setBackColorRGB(-1);
4272 // Set white background
4273 currentState
.attr
.setBackColor(Color
.WHITE
);
4274 currentState
.attr
.setBackColorRGB(-1);
4277 if (type
== DeviceType
.XTERM
) {
4279 * Xterm supports T.416 / ISO-8613-3 codes to select
4280 * either an indexed color or an RGB value. (It also
4281 * permits these ISO-8613-3 SGR sequences to be separated
4282 * by colons rather than semicolons.)
4284 * We will support only the following:
4286 * 1. Indexed color mode (88- or 256-color modes).
4290 * These cover most of the use cases in the real world.
4292 * HOWEVER, note that this is an awful broken "standard",
4293 * with no way to do it "right". See
4294 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4295 * for a detailed discussion of the current state of RGB
4296 * in various terminals, the point of which is that none
4297 * of them really do the same thing despite all appearing
4301 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4302 * where it is assumed that supporting just the "indexed
4303 * mode" of these sequences (which could align easily
4304 * with existing SGR colors) is assumed to mean full
4305 * support of 24-bit RGB. So it is all or nothing.
4307 * Finally, these sequences break the assumptions of
4308 * standard ECMA-48 style parsers as pointed out at
4309 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4310 * Therefore in order to keep a clean display, we cannot
4311 * parse anything else in this sequence.
4318 // Default background
4319 currentState
.attr
.setBackColor(Color
.BLACK
);
4320 currentState
.attr
.setBackColorRGB(-1);
4330 * DA - Device attributes.
4333 int extendedFlag
= 0;
4335 if (collectBuffer
.length() > 0) {
4336 String args
= collectBuffer
.substring(1);
4337 if (collectBuffer
.charAt(0) == '>') {
4339 if (collectBuffer
.length() >= 2) {
4340 i
= Integer
.parseInt(args
);
4342 } else if (collectBuffer
.charAt(0) == '=') {
4344 if (collectBuffer
.length() >= 2) {
4345 i
= Integer
.parseInt(args
);
4348 // Unknown code, bail out
4353 if ((i
!= 0) && (i
!= 1)) {
4357 if ((extendedFlag
== 0) && (i
== 0)) {
4358 // Send string directly to remote side
4359 writeRemote(deviceTypeResponse());
4363 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4365 if ((extendedFlag
== 1) && (i
== 0)) {
4367 * Request "What type of terminal are you, what is your
4368 * firmware version, and what hardware options do you have
4371 * Respond: "I am a VT220 (identification code of 1), my
4372 * firmware version is _____ (Pv), and I have _____ Po
4373 * options installed."
4379 if (s8c1t
== true) {
4380 writeRemote("\u009b>1;10;0c");
4382 writeRemote("\033[>1;10;0c");
4388 if ((extendedFlag
== 2) && (i
== 0)) {
4391 * Request "What is your unit ID?"
4393 * Respond: "I was manufactured at site 00 and have a unique ID
4397 writeRemote("\033P!|00010203\033\\");
4402 * DECSTBM - Set top and bottom margins.
4404 private void decstbm() {
4405 boolean decPrivateModeFlag
= false;
4407 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4408 if (collectBuffer
.charAt(i
) == '?') {
4409 decPrivateModeFlag
= true;
4413 if (decPrivateModeFlag
) {
4414 // This could be restore DEC private mode values.
4418 int top
= getCsiParam(0, 1, 1, height
) - 1;
4419 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4420 if (bottom
> height
- 1) {
4421 bottom
= height
- 1;
4427 scrollRegionTop
= top
;
4428 scrollRegionBottom
= bottom
;
4431 cursorPosition(0, 0);
4436 * DECREQTPARM - Request terminal parameters.
4438 private void decreqtparm() {
4439 int i
= getCsiParam(0, 0);
4441 if ((i
!= 0) && (i
!= 1)) {
4448 * Request terminal parameters.
4452 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4453 * (CLoCk MULtiplier = 1, STP option flags = 0)
4457 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4460 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4462 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4468 * DECSCA - Select Character Attributes.
4470 private void decsca() {
4471 int i
= getCsiParam(0, 0);
4473 if ((i
== 0) || (i
== 2)) {
4475 currentState
.attr
.setProtect(false);
4479 currentState
.attr
.setProtect(true);
4484 * DECSTR - Soft Terminal Reset.
4486 private void decstr() {
4487 // Do exactly like RIS - Reset to initial state
4489 // Do I clear screen too? I think so...
4490 eraseScreen(0, 0, height
- 1, width
- 1, false);
4491 cursorPosition(0, 0);
4495 * DSR - Device status report.
4497 private void dsr() {
4498 boolean decPrivateModeFlag
= false;
4499 int row
= currentState
.cursorY
;
4501 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4502 if (collectBuffer
.charAt(i
) == '?') {
4503 decPrivateModeFlag
= true;
4508 int i
= getCsiParam(0, 0);
4513 // Request status report. Respond with "OK, no malfunction."
4515 // Send string directly to remote side
4516 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4519 writeRemote("\u009b0n");
4521 writeRemote("\033[0n");
4526 // Request cursor position. Respond with current position.
4527 if (currentState
.originMode
== true) {
4528 row
-= scrollRegionTop
;
4531 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4534 str
= String
.format("\u009b%d;%dR", row
+ 1,
4535 currentState
.cursorX
+ 1);
4537 str
= String
.format("\033[%d;%dR", row
+ 1,
4538 currentState
.cursorX
+ 1);
4541 // Send string directly to remote side
4546 if (decPrivateModeFlag
== true) {
4548 // Request printer status report. Respond with "Printer not
4551 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4552 && (s8c1t
== true)) {
4553 writeRemote("\u009b?13n");
4555 writeRemote("\033[?13n");
4561 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4562 && (decPrivateModeFlag
== true)
4565 // Request user-defined keys are locked or unlocked. Respond
4566 // with "User-defined keys are locked."
4568 if (s8c1t
== true) {
4569 writeRemote("\u009b?21n");
4571 writeRemote("\033[?21n");
4577 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4578 && (decPrivateModeFlag
== true)
4581 // Request keyboard language. Respond with "Keyboard
4582 // language is North American."
4584 if (s8c1t
== true) {
4585 writeRemote("\u009b?27;1n");
4587 writeRemote("\033[?27;1n");
4594 // Some other option, ignore
4600 * TBC - Tabulation clear.
4602 private void tbc() {
4603 int i
= getCsiParam(0, 0);
4605 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4606 for (Integer stop
: tabStops
) {
4607 if (stop
== currentState
.cursorX
) {
4612 tabStops
= newStops
;
4620 * Erase the characters in the current line from the start column to the
4621 * end column, inclusive.
4623 * @param start starting column to erase (between 0 and width - 1)
4624 * @param end ending column to erase (between 0 and width - 1)
4625 * @param honorProtected if true, do not erase characters with the
4626 * protected attribute set
4628 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4633 if (end
> width
- 1) {
4640 for (int i
= start
; i
<= end
; i
++) {
4641 DisplayLine line
= display
.get(currentState
.cursorY
);
4642 if ((!honorProtected
)
4643 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4650 * From the VT102 manual:
4652 * Erasing a character also erases any character
4653 * attribute of the character.
4659 * Erase with the current color a.k.a. back-color erase
4662 line
.setChar(i
, ' ');
4663 line
.setAttr(i
, currentState
.attr
);
4671 * Erase a rectangular section of the screen, inclusive. end column,
4674 * @param startRow starting row to erase (between 0 and height - 1)
4675 * @param startCol starting column to erase (between 0 and width - 1)
4676 * @param endRow ending row to erase (between 0 and height - 1)
4677 * @param endCol ending column to erase (between 0 and width - 1)
4678 * @param honorProtected if true, do not erase characters with the
4679 * protected attribute set
4681 private void eraseScreen(final int startRow
, final int startCol
,
4682 final int endRow
, final int endCol
, final boolean honorProtected
) {
4690 || (endRow
< startRow
)
4691 || (endCol
< startCol
)
4696 oldCursorY
= currentState
.cursorY
;
4697 for (int i
= startRow
; i
<= endRow
; i
++) {
4698 currentState
.cursorY
= i
;
4699 eraseLine(startCol
, endCol
, honorProtected
);
4701 // Erase display clears the double attributes
4702 display
.get(i
).setDoubleWidth(false);
4703 display
.get(i
).setDoubleHeight(0);
4705 currentState
.cursorY
= oldCursorY
;
4709 * VT220 printer functions. All of these are parsed, but won't do
4712 private void printerFunctions() {
4713 boolean decPrivateModeFlag
= false;
4714 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4715 if (collectBuffer
.charAt(i
) == '?') {
4716 decPrivateModeFlag
= true;
4721 int i
= getCsiParam(0, 0);
4726 if (decPrivateModeFlag
== false) {
4732 if (decPrivateModeFlag
== true) {
4733 // Print cursor line
4738 if (decPrivateModeFlag
== true) {
4739 // Auto print mode OFF
4741 // Printer controller OFF
4743 // Characters re-appear on the screen
4744 printerControllerMode
= false;
4749 if (decPrivateModeFlag
== true) {
4753 // Printer controller
4755 // Characters get sucked into oblivion
4756 printerControllerMode
= true;
4767 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4768 * of remote systems will send an XTerm title sequence even if TERM isn't
4771 * @param xtermChar the character received from the remote side
4773 private void oscPut(final char xtermChar
) {
4774 // System.err.println("oscPut: " + xtermChar);
4776 boolean oscEnd
= false;
4778 if (xtermChar
== 0x07) {
4781 if ((xtermChar
== '\\')
4782 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\033')
4788 collectBuffer
.append(xtermChar
);
4793 if (xtermChar
== 0x07) {
4794 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
4796 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4799 String
[] p
= args
.split(";");
4801 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4808 if (p
[0].equals("4")) {
4809 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
4810 // Set a color index value
4812 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
4813 } catch (NumberFormatException e
) {
4819 if (p
[0].equals("10")) {
4820 if (p
[1].equals("?")) {
4821 // Respond with foreground color.
4822 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToForegroundColor(currentState
.attr
);
4824 writeRemote(String
.format(
4825 "\033]10;rgb:%04x/%04x/%04x\033\\",
4826 color
.getRed() << 8,
4827 color
.getGreen() << 8,
4828 color
.getBlue() << 8));
4832 if (p
[0].equals("11")) {
4833 if (p
[1].equals("?")) {
4834 // Respond with background color.
4835 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToBackgroundColor(currentState
.attr
);
4837 writeRemote(String
.format(
4838 "\033]11;rgb:%04x/%04x/%04x\033\\",
4839 color
.getRed() << 8,
4840 color
.getGreen() << 8,
4841 color
.getBlue() << 8));
4845 if (p
[0].equals("444")) {
4846 if (p
[1].equals("0") && (p
.length
== 6)) {
4847 // Jexer image - RGB
4848 parseJexerImageRGB(p
[2], p
[3], p
[4], p
[5]);
4849 } else if (p
[1].equals("1") && (p
.length
== 4)) {
4850 // Jexer image - PNG
4851 parseJexerImageFile(1, p
[2], p
[3]);
4852 } else if (p
[1].equals("2") && (p
.length
== 4)) {
4853 // Jexer image - JPG
4854 parseJexerImageFile(2, p
[2], p
[3]);
4859 // Go to SCAN_GROUND state
4866 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
4867 * Jexer ECMA48Terminal to talk to ECMA48.
4869 * @param pmChar the character received from the remote side
4871 private void pmPut(final char pmChar
) {
4872 // System.err.println("pmPut: " + pmChar);
4874 boolean pmEnd
= false;
4876 if ((pmChar
== '\\')
4877 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\033')
4883 collectBuffer
.append(pmChar
);
4888 arg
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4890 // System.err.println("arg: '" + arg + "'");
4892 if (arg
.equals("hideMousePointer")) {
4893 hideMousePointer
= true;
4895 if (arg
.equals("showMousePointer")) {
4896 hideMousePointer
= false;
4899 // Go to SCAN_GROUND state
4906 * Perform xterm window operations.
4908 private void xtermWindowOps() {
4909 boolean xtermPrivateModeFlag
= false;
4911 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4912 if (collectBuffer
.charAt(i
) == '?') {
4913 xtermPrivateModeFlag
= true;
4918 int i
= getCsiParam(0, 0);
4920 if (!xtermPrivateModeFlag
) {
4923 // Report xterm text area size in pixels as CSI 4 ; height ;
4925 writeRemote(String
.format("\033[4;%d;%dt", textHeight
* height
,
4926 textWidth
* width
));
4929 // Report character size in pixels as CSI 6 ; height ; width
4931 writeRemote(String
.format("\033[6;%d;%dt", textHeight
,
4935 // Report the text are size in characters as CSI 8 ; height ;
4937 writeRemote(String
.format("\033[8;%d;%dt", height
, width
));
4946 * Respond to xterm sixel query.
4948 private void xtermSixelQuery() {
4949 int item
= getCsiParam(0, 0);
4950 int action
= getCsiParam(1, 0);
4951 int value
= getCsiParam(2, 0);
4956 // Report number of color registers.
4957 writeRemote(String
.format("\033[?%d;%d;%dS", item
, 0, 1024));
4964 // We will not support this option.
4965 writeRemote(String
.format("\033[?%d;%dS", item
, action
));
4969 * Run this input character through the ECMA48 state machine.
4971 * @param ch character from the remote side
4973 private void consume(final int ch
) {
4976 // System.err.printf("%c STATE = %s\n", ch, scanState);
4978 // Special "anywhere" states
4980 // 18, 1A --> execute, then switch to SCAN_GROUND
4981 if ((ch
== 0x18) || (ch
== 0x1A)) {
4982 // CAN and SUB abort escape sequences
4987 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4991 if ((type
== DeviceType
.XTERM
)
4992 && ((scanState
== ScanState
.OSC_STRING
)
4993 || (scanState
== ScanState
.DCS_SIXEL
)
4994 || (scanState
== ScanState
.SOSPMAPC_STRING
))
4996 // Xterm can pass ESCAPE to its OSC sequence.
4997 // Xterm can pass ESCAPE to its DCS sequence.
4998 // Jexer can pass ESCAPE to its PM sequence.
4999 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
5000 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
5001 && (scanState
!= ScanState
.DCS_IGNORE
)
5002 && (scanState
!= ScanState
.DCS_PARAM
)
5003 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
5005 scanState
= ScanState
.ESCAPE
;
5010 // 0x9B == CSI 8-bit sequence
5012 scanState
= ScanState
.CSI_ENTRY
;
5016 // 0x9D goes to ScanState.OSC_STRING
5018 scanState
= ScanState
.OSC_STRING
;
5022 // 0x90 goes to DCS_ENTRY
5024 scanState
= ScanState
.DCS_ENTRY
;
5028 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
5029 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
5030 scanState
= ScanState
.SOSPMAPC_STRING
;
5034 // 0x7F (DEL) is always discarded
5039 switch (scanState
) {
5042 // 00-17, 19, 1C-1F --> execute
5043 // 80-8F, 91-9A, 9C --> execute
5044 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
5045 handleControlChar((char) ch
);
5049 if (((ch
>= 0x20) && (ch
<= 0x7F))
5053 // VT220 printer --> trash bin
5054 if (((type
== DeviceType
.VT220
)
5055 || (type
== DeviceType
.XTERM
))
5056 && (printerControllerMode
== true)
5061 // Hang onto this character
5062 repCh
= mapCharacter(ch
);
5064 // Print this character
5065 printCharacter(repCh
);
5070 // 00-17, 19, 1C-1F --> execute
5072 handleControlChar((char) ch
);
5076 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
5077 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5079 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
5083 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
5084 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
5095 // DECSC - Save cursor
5096 // Note this code overlaps both ANSI and VT52 mode
5101 // DECRC - Restore cursor
5102 // Note this code overlaps both ANSI and VT52 mode
5111 if (vt52Mode
== true) {
5112 // DECANM - Enter ANSI mode
5114 arrowKeyMode
= ArrowKeyMode
.VT100
;
5117 * From the VT102 docs: "You use ANSI mode to select
5118 * most terminal features; the terminal uses the same
5119 * features when it switches to VT52 mode. You
5120 * cannot, however, change most of these features in
5123 * In other words, do not reset any other attributes
5124 * when switching between VT52 submode and ANSI.
5128 currentState
.g0Charset
= CharacterSet
.US
;
5129 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5131 singleshift
= Singleshift
.NONE
;
5132 currentState
.glLockshift
= LockshiftMode
.NONE
;
5133 currentState
.grLockshift
= LockshiftMode
.NONE
;
5137 // DECKPAM - Keypad application mode
5138 // Note this code overlaps both ANSI and VT52 mode
5142 // DECKPNM - Keypad numeric mode
5143 // Note this code overlaps both ANSI and VT52 mode
5150 if (vt52Mode
== true) {
5151 // Cursor up, and stop at the top without scrolling
5156 if (vt52Mode
== true) {
5157 // Cursor down, and stop at the bottom without scrolling
5158 cursorDown(1, false);
5162 if (vt52Mode
== true) {
5163 // Cursor right, and stop at the right without scrolling
5164 cursorRight(1, false);
5168 if (vt52Mode
== true) {
5169 // Cursor left, and stop at the left without scrolling
5170 cursorLeft(1, false);
5177 if (vt52Mode
== true) {
5185 if (vt52Mode
== true) {
5186 // G0 --> Special graphics
5187 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
5191 if (vt52Mode
== true) {
5193 currentState
.g0Charset
= CharacterSet
.US
;
5197 if (vt52Mode
== true) {
5199 cursorPosition(0, 0);
5201 // HTS - Horizontal tabulation set
5206 if (vt52Mode
== true) {
5207 // Reverse line feed. Same as RI.
5212 if (vt52Mode
== true) {
5213 // Erase to end of screen
5214 eraseLine(currentState
.cursorX
, width
- 1, false);
5215 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
5220 if (vt52Mode
== true) {
5221 // Erase to end of line
5222 eraseLine(currentState
.cursorX
, width
- 1, false);
5228 if (vt52Mode
== true) {
5231 // RI - Reverse index
5236 if (vt52Mode
== false) {
5238 singleshift
= Singleshift
.SS2
;
5242 if (vt52Mode
== false) {
5244 singleshift
= Singleshift
.SS3
;
5251 if ((ch
>= 0x51) && (ch
<= 0x57)) {
5267 if (vt52Mode
== true) {
5268 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
5276 if (vt52Mode
== true) {
5278 // Send string directly to remote side
5279 writeRemote("\033/Z");
5282 // Send string directly to remote side
5283 writeRemote(deviceTypeResponse());
5294 // VT52 cannot get to any of these other states
5295 if (vt52Mode
== true) {
5300 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
5307 // RIS - Reset to initial state
5309 // Do I clear screen too? I think so...
5310 eraseScreen(0, 0, height
- 1, width
- 1, false);
5311 cursorPosition(0, 0);
5325 if ((type
== DeviceType
.VT220
)
5326 || (type
== DeviceType
.XTERM
)) {
5328 // VT220 lockshift G2 into GL
5329 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
5334 if ((type
== DeviceType
.VT220
)
5335 || (type
== DeviceType
.XTERM
)) {
5337 // VT220 lockshift G3 into GL
5338 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
5356 if ((type
== DeviceType
.VT220
)
5357 || (type
== DeviceType
.XTERM
)) {
5359 // VT220 lockshift G3 into GR
5360 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
5365 if ((type
== DeviceType
.VT220
)
5366 || (type
== DeviceType
.XTERM
)) {
5368 // VT220 lockshift G2 into GR
5369 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
5375 if ((type
== DeviceType
.VT220
)
5376 || (type
== DeviceType
.XTERM
)) {
5378 // VT220 lockshift G1 into GR
5379 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
5389 // 0x5B goes to CSI_ENTRY
5391 scanState
= ScanState
.CSI_ENTRY
;
5394 // 0x5D goes to OSC_STRING
5396 scanState
= ScanState
.OSC_STRING
;
5399 // 0x50 goes to DCS_ENTRY
5401 scanState
= ScanState
.DCS_ENTRY
;
5404 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5405 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
5406 scanState
= ScanState
.SOSPMAPC_STRING
;
5411 case ESCAPE_INTERMEDIATE
:
5412 // 00-17, 19, 1C-1F --> execute
5414 handleControlChar((char) ch
);
5417 // 20-2F --> collect
5418 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5422 // 30-7E --> dispatch, then switch to GROUND
5423 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
5426 if ((collectBuffer
.length() == 1)
5427 && (collectBuffer
.charAt(0) == '(')) {
5428 // G0 --> Special graphics
5429 currentState
.g0Charset
= CharacterSet
.DRAWING
;
5431 if ((collectBuffer
.length() == 1)
5432 && (collectBuffer
.charAt(0) == ')')) {
5433 // G1 --> Special graphics
5434 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5436 if ((type
== DeviceType
.VT220
)
5437 || (type
== DeviceType
.XTERM
)) {
5439 if ((collectBuffer
.length() == 1)
5440 && (collectBuffer
.charAt(0) == '*')) {
5441 // G2 --> Special graphics
5442 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5444 if ((collectBuffer
.length() == 1)
5445 && (collectBuffer
.charAt(0) == '+')) {
5446 // G3 --> Special graphics
5447 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5452 if ((collectBuffer
.length() == 1)
5453 && (collectBuffer
.charAt(0) == '(')) {
5454 // G0 --> Alternate character ROM standard character set
5455 currentState
.g0Charset
= CharacterSet
.ROM
;
5457 if ((collectBuffer
.length() == 1)
5458 && (collectBuffer
.charAt(0) == ')')) {
5459 // G1 --> Alternate character ROM standard character set
5460 currentState
.g1Charset
= CharacterSet
.ROM
;
5464 if ((collectBuffer
.length() == 1)
5465 && (collectBuffer
.charAt(0) == '(')) {
5466 // G0 --> Alternate character ROM special graphics
5467 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5469 if ((collectBuffer
.length() == 1)
5470 && (collectBuffer
.charAt(0) == ')')) {
5471 // G1 --> Alternate character ROM special graphics
5472 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5476 if ((collectBuffer
.length() == 1)
5477 && (collectBuffer
.charAt(0) == '#')) {
5478 // DECDHL - Double-height line (top half)
5483 if ((collectBuffer
.length() == 1)
5484 && (collectBuffer
.charAt(0) == '#')) {
5485 // DECDHL - Double-height line (bottom half)
5488 if ((type
== DeviceType
.VT220
)
5489 || (type
== DeviceType
.XTERM
)) {
5491 if ((collectBuffer
.length() == 1)
5492 && (collectBuffer
.charAt(0) == '(')) {
5494 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5496 if ((collectBuffer
.length() == 1)
5497 && (collectBuffer
.charAt(0) == ')')) {
5499 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5501 if ((collectBuffer
.length() == 1)
5502 && (collectBuffer
.charAt(0) == '*')) {
5504 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5506 if ((collectBuffer
.length() == 1)
5507 && (collectBuffer
.charAt(0) == '+')) {
5509 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5514 if ((collectBuffer
.length() == 1)
5515 && (collectBuffer
.charAt(0) == '#')) {
5516 // DECSWL - Single-width line
5519 if ((type
== DeviceType
.VT220
)
5520 || (type
== DeviceType
.XTERM
)) {
5522 if ((collectBuffer
.length() == 1)
5523 && (collectBuffer
.charAt(0) == '(')) {
5525 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5527 if ((collectBuffer
.length() == 1)
5528 && (collectBuffer
.charAt(0) == ')')) {
5530 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5532 if ((collectBuffer
.length() == 1)
5533 && (collectBuffer
.charAt(0) == '*')) {
5535 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5537 if ((collectBuffer
.length() == 1)
5538 && (collectBuffer
.charAt(0) == '+')) {
5540 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5545 if ((collectBuffer
.length() == 1)
5546 && (collectBuffer
.charAt(0) == '#')) {
5547 // DECDWL - Double-width line
5550 if ((type
== DeviceType
.VT220
)
5551 || (type
== DeviceType
.XTERM
)) {
5553 if ((collectBuffer
.length() == 1)
5554 && (collectBuffer
.charAt(0) == '(')) {
5556 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5558 if ((collectBuffer
.length() == 1)
5559 && (collectBuffer
.charAt(0) == ')')) {
5561 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5563 if ((collectBuffer
.length() == 1)
5564 && (collectBuffer
.charAt(0) == '*')) {
5566 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5568 if ((collectBuffer
.length() == 1)
5569 && (collectBuffer
.charAt(0) == '+')) {
5571 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5576 if ((type
== DeviceType
.VT220
)
5577 || (type
== DeviceType
.XTERM
)) {
5579 if ((collectBuffer
.length() == 1)
5580 && (collectBuffer
.charAt(0) == '(')) {
5582 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5584 if ((collectBuffer
.length() == 1)
5585 && (collectBuffer
.charAt(0) == ')')) {
5587 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5589 if ((collectBuffer
.length() == 1)
5590 && (collectBuffer
.charAt(0) == '*')) {
5592 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5594 if ((collectBuffer
.length() == 1)
5595 && (collectBuffer
.charAt(0) == '+')) {
5597 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5602 if ((collectBuffer
.length() == 1)
5603 && (collectBuffer
.charAt(0) == '#')) {
5604 // DECALN - Screen alignment display
5613 if ((type
== DeviceType
.VT220
)
5614 || (type
== DeviceType
.XTERM
)) {
5616 if ((collectBuffer
.length() == 1)
5617 && (collectBuffer
.charAt(0) == '(')) {
5618 // G0 --> DEC_SUPPLEMENTAL
5619 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5621 if ((collectBuffer
.length() == 1)
5622 && (collectBuffer
.charAt(0) == ')')) {
5623 // G1 --> DEC_SUPPLEMENTAL
5624 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5626 if ((collectBuffer
.length() == 1)
5627 && (collectBuffer
.charAt(0) == '*')) {
5628 // G2 --> DEC_SUPPLEMENTAL
5629 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5631 if ((collectBuffer
.length() == 1)
5632 && (collectBuffer
.charAt(0) == '+')) {
5633 // G3 --> DEC_SUPPLEMENTAL
5634 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5639 if ((type
== DeviceType
.VT220
)
5640 || (type
== DeviceType
.XTERM
)) {
5642 if ((collectBuffer
.length() == 1)
5643 && (collectBuffer
.charAt(0) == '(')) {
5645 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5647 if ((collectBuffer
.length() == 1)
5648 && (collectBuffer
.charAt(0) == ')')) {
5650 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5652 if ((collectBuffer
.length() == 1)
5653 && (collectBuffer
.charAt(0) == '*')) {
5655 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5657 if ((collectBuffer
.length() == 1)
5658 && (collectBuffer
.charAt(0) == '+')) {
5660 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5669 if ((collectBuffer
.length() == 1)
5670 && (collectBuffer
.charAt(0) == '(')) {
5671 // G0 --> United Kingdom set
5672 currentState
.g0Charset
= CharacterSet
.UK
;
5674 if ((collectBuffer
.length() == 1)
5675 && (collectBuffer
.charAt(0) == ')')) {
5676 // G1 --> United Kingdom set
5677 currentState
.g1Charset
= CharacterSet
.UK
;
5679 if ((type
== DeviceType
.VT220
)
5680 || (type
== DeviceType
.XTERM
)) {
5682 if ((collectBuffer
.length() == 1)
5683 && (collectBuffer
.charAt(0) == '*')) {
5684 // G2 --> United Kingdom set
5685 currentState
.g2Charset
= CharacterSet
.UK
;
5687 if ((collectBuffer
.length() == 1)
5688 && (collectBuffer
.charAt(0) == '+')) {
5689 // G3 --> United Kingdom set
5690 currentState
.g3Charset
= CharacterSet
.UK
;
5695 if ((collectBuffer
.length() == 1)
5696 && (collectBuffer
.charAt(0) == '(')) {
5698 currentState
.g0Charset
= CharacterSet
.US
;
5700 if ((collectBuffer
.length() == 1)
5701 && (collectBuffer
.charAt(0) == ')')) {
5703 currentState
.g1Charset
= CharacterSet
.US
;
5705 if ((type
== DeviceType
.VT220
)
5706 || (type
== DeviceType
.XTERM
)) {
5708 if ((collectBuffer
.length() == 1)
5709 && (collectBuffer
.charAt(0) == '*')) {
5711 currentState
.g2Charset
= CharacterSet
.US
;
5713 if ((collectBuffer
.length() == 1)
5714 && (collectBuffer
.charAt(0) == '+')) {
5716 currentState
.g3Charset
= CharacterSet
.US
;
5721 if ((type
== DeviceType
.VT220
)
5722 || (type
== DeviceType
.XTERM
)) {
5724 if ((collectBuffer
.length() == 1)
5725 && (collectBuffer
.charAt(0) == '(')) {
5727 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5729 if ((collectBuffer
.length() == 1)
5730 && (collectBuffer
.charAt(0) == ')')) {
5732 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5734 if ((collectBuffer
.length() == 1)
5735 && (collectBuffer
.charAt(0) == '*')) {
5737 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5739 if ((collectBuffer
.length() == 1)
5740 && (collectBuffer
.charAt(0) == '+')) {
5742 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5749 if ((type
== DeviceType
.VT220
)
5750 || (type
== DeviceType
.XTERM
)) {
5752 if ((collectBuffer
.length() == 1)
5753 && (collectBuffer
.charAt(0) == '(')) {
5755 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5757 if ((collectBuffer
.length() == 1)
5758 && (collectBuffer
.charAt(0) == ')')) {
5760 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5762 if ((collectBuffer
.length() == 1)
5763 && (collectBuffer
.charAt(0) == '*')) {
5765 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5767 if ((collectBuffer
.length() == 1)
5768 && (collectBuffer
.charAt(0) == '+')) {
5770 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5775 if ((type
== DeviceType
.VT220
)
5776 || (type
== DeviceType
.XTERM
)) {
5778 if ((collectBuffer
.length() == 1)
5779 && (collectBuffer
.charAt(0) == ' ')) {
5786 if ((type
== DeviceType
.VT220
)
5787 || (type
== DeviceType
.XTERM
)) {
5789 if ((collectBuffer
.length() == 1)
5790 && (collectBuffer
.charAt(0) == ' ')) {
5797 if ((type
== DeviceType
.VT220
)
5798 || (type
== DeviceType
.XTERM
)) {
5800 if ((collectBuffer
.length() == 1)
5801 && (collectBuffer
.charAt(0) == '(')) {
5803 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5805 if ((collectBuffer
.length() == 1)
5806 && (collectBuffer
.charAt(0) == ')')) {
5808 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5810 if ((collectBuffer
.length() == 1)
5811 && (collectBuffer
.charAt(0) == '*')) {
5813 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5815 if ((collectBuffer
.length() == 1)
5816 && (collectBuffer
.charAt(0) == '+')) {
5818 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5826 if ((type
== DeviceType
.VT220
)
5827 || (type
== DeviceType
.XTERM
)) {
5829 if ((collectBuffer
.length() == 1)
5830 && (collectBuffer
.charAt(0) == '(')) {
5832 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5834 if ((collectBuffer
.length() == 1)
5835 && (collectBuffer
.charAt(0) == ')')) {
5837 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5839 if ((collectBuffer
.length() == 1)
5840 && (collectBuffer
.charAt(0) == '*')) {
5842 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5844 if ((collectBuffer
.length() == 1)
5845 && (collectBuffer
.charAt(0) == '+')) {
5847 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5858 if ((type
== DeviceType
.VT220
)
5859 || (type
== DeviceType
.XTERM
)) {
5861 if ((collectBuffer
.length() == 1)
5862 && (collectBuffer
.charAt(0) == '(')) {
5864 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5866 if ((collectBuffer
.length() == 1)
5867 && (collectBuffer
.charAt(0) == ')')) {
5869 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5871 if ((collectBuffer
.length() == 1)
5872 && (collectBuffer
.charAt(0) == '*')) {
5874 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5876 if ((collectBuffer
.length() == 1)
5877 && (collectBuffer
.charAt(0) == '+')) {
5879 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5884 if ((type
== DeviceType
.VT220
)
5885 || (type
== DeviceType
.XTERM
)) {
5887 if ((collectBuffer
.length() == 1)
5888 && (collectBuffer
.charAt(0) == '(')) {
5890 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5892 if ((collectBuffer
.length() == 1)
5893 && (collectBuffer
.charAt(0) == ')')) {
5895 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5897 if ((collectBuffer
.length() == 1)
5898 && (collectBuffer
.charAt(0) == '*')) {
5900 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5902 if ((collectBuffer
.length() == 1)
5903 && (collectBuffer
.charAt(0) == '+')) {
5905 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5917 if ((type
== DeviceType
.VT220
)
5918 || (type
== DeviceType
.XTERM
)) {
5920 if ((collectBuffer
.length() == 1)
5921 && (collectBuffer
.charAt(0) == '(')) {
5923 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5925 if ((collectBuffer
.length() == 1)
5926 && (collectBuffer
.charAt(0) == ')')) {
5928 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5930 if ((collectBuffer
.length() == 1)
5931 && (collectBuffer
.charAt(0) == '*')) {
5933 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5935 if ((collectBuffer
.length() == 1)
5936 && (collectBuffer
.charAt(0) == '+')) {
5938 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5943 if ((type
== DeviceType
.VT220
)
5944 || (type
== DeviceType
.XTERM
)) {
5946 if ((collectBuffer
.length() == 1)
5947 && (collectBuffer
.charAt(0) == '(')) {
5949 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5951 if ((collectBuffer
.length() == 1)
5952 && (collectBuffer
.charAt(0) == ')')) {
5954 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5956 if ((collectBuffer
.length() == 1)
5957 && (collectBuffer
.charAt(0) == '*')) {
5959 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5961 if ((collectBuffer
.length() == 1)
5962 && (collectBuffer
.charAt(0) == '+')) {
5964 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
6011 // 0x9C goes to GROUND
6019 // 00-17, 19, 1C-1F --> execute
6021 handleControlChar((char) ch
);
6024 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6025 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6027 scanState
= ScanState
.CSI_INTERMEDIATE
;
6030 // 30-39, 3B --> param, then switch to CSI_PARAM
6031 if ((ch
>= '0') && (ch
<= '9')) {
6033 scanState
= ScanState
.CSI_PARAM
;
6037 scanState
= ScanState
.CSI_PARAM
;
6040 // 3C-3F --> collect, then switch to CSI_PARAM
6041 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6043 scanState
= ScanState
.CSI_PARAM
;
6046 // 40-7E --> dispatch, then switch to GROUND
6047 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6050 // ICH - Insert character
6058 // CUD - Cursor down
6062 // CUF - Cursor forward
6066 // CUB - Cursor backward
6070 // CNL - Cursor down and to column 1
6071 if (type
== DeviceType
.XTERM
) {
6076 // CPL - Cursor up and to column 1
6077 if (type
== DeviceType
.XTERM
) {
6082 // CHA - Cursor to column # in current row
6083 if (type
== DeviceType
.XTERM
) {
6088 // CUP - Cursor position
6092 // CHT - Cursor forward X tab stops (default 1)
6093 if (type
== DeviceType
.XTERM
) {
6098 // ED - Erase in display
6102 // EL - Erase in line
6117 // DCH - Delete character
6124 // Scroll up X lines (default 1)
6125 if (type
== DeviceType
.XTERM
) {
6126 boolean xtermPrivateModeFlag
= false;
6127 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6128 if (collectBuffer
.charAt(i
) == '?') {
6129 xtermPrivateModeFlag
= true;
6133 if (xtermPrivateModeFlag
) {
6141 // Scroll down X lines (default 1)
6142 if (type
== DeviceType
.XTERM
) {
6151 if ((type
== DeviceType
.VT220
)
6152 || (type
== DeviceType
.XTERM
)) {
6154 // ECH - Erase character
6161 // CBT - Cursor backward X tab stops (default 1)
6162 if (type
== DeviceType
.XTERM
) {
6173 // HPA - Cursor to column # in current row. Same as CHA
6174 if (type
== DeviceType
.XTERM
) {
6179 // HPR - Cursor right. Same as CUF
6180 if (type
== DeviceType
.XTERM
) {
6185 // REP - Repeat last char X times
6186 if (type
== DeviceType
.XTERM
) {
6191 // DA - Device attributes
6195 // VPA - Cursor to row, current column.
6196 if (type
== DeviceType
.XTERM
) {
6201 // VPR - Cursor down. Same as CUD
6202 if (type
== DeviceType
.XTERM
) {
6207 // HVP - Horizontal and vertical position
6211 // TBC - Tabulation clear
6215 // Sets an ANSI or DEC private toggle
6219 if ((type
== DeviceType
.VT220
)
6220 || (type
== DeviceType
.XTERM
)) {
6222 // Printer functions
6230 // Sets an ANSI or DEC private toggle
6234 // SGR - Select graphics rendition
6238 // DSR - Device status report
6245 // DECLL - Load leds
6249 // DECSTBM - Set top and bottom margins
6253 // Save cursor (ANSI.SYS)
6254 if (type
== DeviceType
.XTERM
) {
6255 savedState
.cursorX
= currentState
.cursorX
;
6256 savedState
.cursorY
= currentState
.cursorY
;
6260 if (type
== DeviceType
.XTERM
) {
6261 // Window operations
6266 // Restore cursor (ANSI.SYS)
6267 if (type
== DeviceType
.XTERM
) {
6268 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
6275 // DECREQTPARM - Request terminal parameters
6291 // 0x9C goes to GROUND
6296 // 0x3A goes to CSI_IGNORE
6298 scanState
= ScanState
.CSI_IGNORE
;
6303 // 00-17, 19, 1C-1F --> execute
6305 handleControlChar((char) ch
);
6308 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6309 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6311 scanState
= ScanState
.CSI_INTERMEDIATE
;
6314 // 30-39, 3B --> param
6315 if ((ch
>= '0') && (ch
<= '9')) {
6322 // 0x3A goes to CSI_IGNORE
6324 scanState
= ScanState
.CSI_IGNORE
;
6326 // 0x3C-3F goes to CSI_IGNORE
6327 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6328 scanState
= ScanState
.CSI_IGNORE
;
6331 // 40-7E --> dispatch, then switch to GROUND
6332 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6335 // ICH - Insert character
6343 // CUD - Cursor down
6347 // CUF - Cursor forward
6351 // CUB - Cursor backward
6355 // CNL - Cursor down and to column 1
6356 if (type
== DeviceType
.XTERM
) {
6361 // CPL - Cursor up and to column 1
6362 if (type
== DeviceType
.XTERM
) {
6367 // CHA - Cursor to column # in current row
6368 if (type
== DeviceType
.XTERM
) {
6373 // CUP - Cursor position
6377 // CHT - Cursor forward X tab stops (default 1)
6378 if (type
== DeviceType
.XTERM
) {
6383 // ED - Erase in display
6387 // EL - Erase in line
6402 // DCH - Delete character
6409 // Scroll up X lines (default 1)
6410 if (type
== DeviceType
.XTERM
) {
6411 boolean xtermPrivateModeFlag
= false;
6412 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6413 if (collectBuffer
.charAt(i
) == '?') {
6414 xtermPrivateModeFlag
= true;
6418 if (xtermPrivateModeFlag
) {
6426 // Scroll down X lines (default 1)
6427 if (type
== DeviceType
.XTERM
) {
6436 if ((type
== DeviceType
.VT220
)
6437 || (type
== DeviceType
.XTERM
)) {
6439 // ECH - Erase character
6446 // CBT - Cursor backward X tab stops (default 1)
6447 if (type
== DeviceType
.XTERM
) {
6458 // HPA - Cursor to column # in current row. Same as CHA
6459 if (type
== DeviceType
.XTERM
) {
6464 // HPR - Cursor right. Same as CUF
6465 if (type
== DeviceType
.XTERM
) {
6470 // REP - Repeat last char X times
6471 if (type
== DeviceType
.XTERM
) {
6476 // DA - Device attributes
6480 // VPA - Cursor to row, current column.
6481 if (type
== DeviceType
.XTERM
) {
6486 // VPR - Cursor down. Same as CUD
6487 if (type
== DeviceType
.XTERM
) {
6492 // HVP - Horizontal and vertical position
6496 // TBC - Tabulation clear
6500 // Sets an ANSI or DEC private toggle
6504 if ((type
== DeviceType
.VT220
)
6505 || (type
== DeviceType
.XTERM
)) {
6507 // Printer functions
6515 // Sets an ANSI or DEC private toggle
6519 // SGR - Select graphics rendition
6523 // DSR - Device status report
6530 // DECLL - Load leds
6534 // DECSTBM - Set top and bottom margins
6540 if (type
== DeviceType
.XTERM
) {
6541 // Window operations
6550 // DECREQTPARM - Request terminal parameters
6567 case CSI_INTERMEDIATE
:
6568 // 00-17, 19, 1C-1F --> execute
6570 handleControlChar((char) ch
);
6573 // 20-2F --> collect
6574 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6578 // 0x30-3F goes to CSI_IGNORE
6579 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6580 scanState
= ScanState
.CSI_IGNORE
;
6583 // 40-7E --> dispatch, then switch to GROUND
6584 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6636 if (((type
== DeviceType
.VT220
)
6637 || (type
== DeviceType
.XTERM
))
6638 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6640 // DECSCL - compatibility level
6643 if ((type
== DeviceType
.XTERM
)
6644 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6646 // DECSTR - Soft terminal reset
6651 if (((type
== DeviceType
.VT220
)
6652 || (type
== DeviceType
.XTERM
))
6653 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6681 // 00-17, 19, 1C-1F --> execute
6683 handleControlChar((char) ch
);
6686 // 20-2F --> collect
6687 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6691 // 40-7E --> ignore, then switch to GROUND
6692 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6696 // 20-3F, 7F --> ignore
6702 // 0x9C goes to GROUND
6707 // 0x1B 0x5C goes to GROUND
6712 if ((collectBuffer
.length() > 0)
6713 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6719 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6720 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6722 scanState
= ScanState
.DCS_INTERMEDIATE
;
6725 // 30-39, 3B --> param, then switch to DCS_PARAM
6726 if ((ch
>= '0') && (ch
<= '9')) {
6728 scanState
= ScanState
.DCS_PARAM
;
6732 scanState
= ScanState
.DCS_PARAM
;
6735 // 3C-3F --> collect, then switch to DCS_PARAM
6736 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6738 scanState
= ScanState
.DCS_PARAM
;
6741 // 00-17, 19, 1C-1F, 7F --> ignore
6743 // 0x3A goes to DCS_IGNORE
6745 scanState
= ScanState
.DCS_IGNORE
;
6748 // 0x71 goes to DCS_SIXEL
6750 sixelParseBuffer
.setLength(0);
6751 scanState
= ScanState
.DCS_SIXEL
;
6752 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6753 // 0x40-7E goes to DCS_PASSTHROUGH
6754 scanState
= ScanState
.DCS_PASSTHROUGH
;
6758 case DCS_INTERMEDIATE
:
6760 // 0x9C goes to GROUND
6765 // 0x1B 0x5C goes to GROUND
6770 if ((collectBuffer
.length() > 0)
6771 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6777 // 0x30-3F goes to DCS_IGNORE
6778 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6779 scanState
= ScanState
.DCS_IGNORE
;
6782 // 0x40-7E goes to DCS_PASSTHROUGH
6783 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6784 scanState
= ScanState
.DCS_PASSTHROUGH
;
6787 // 00-17, 19, 1C-1F, 7F --> ignore
6792 // 0x9C goes to GROUND
6797 // 0x1B 0x5C goes to GROUND
6802 if ((collectBuffer
.length() > 0)
6803 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6809 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6810 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6812 scanState
= ScanState
.DCS_INTERMEDIATE
;
6815 // 30-39, 3B --> param
6816 if ((ch
>= '0') && (ch
<= '9')) {
6823 // 00-17, 19, 1C-1F, 7F --> ignore
6825 // 0x3A, 3C-3F goes to DCS_IGNORE
6827 scanState
= ScanState
.DCS_IGNORE
;
6829 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6830 scanState
= ScanState
.DCS_IGNORE
;
6833 // 0x71 goes to DCS_SIXEL
6835 sixelParseBuffer
.setLength(0);
6836 scanState
= ScanState
.DCS_SIXEL
;
6837 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6838 // 0x40-7E goes to DCS_PASSTHROUGH
6839 scanState
= ScanState
.DCS_PASSTHROUGH
;
6843 case DCS_PASSTHROUGH
:
6844 // 0x9C goes to GROUND
6849 // 0x1B 0x5C goes to GROUND
6854 if ((collectBuffer
.length() > 0)
6855 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6861 // 00-17, 19, 1C-1F, 20-7E --> put
6863 // We ignore all DCS except sixel.
6867 // We ignore all DCS except sixel.
6870 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6871 // We ignore all DCS except sixel.
6874 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6875 // We ignore all DCS except sixel.
6884 // 00-17, 19, 1C-1F, 20-7F --> ignore
6886 // 0x9C goes to GROUND
6894 // 0x9C goes to GROUND
6901 // 0x1B 0x5C goes to GROUND
6907 if ((collectBuffer
.length() > 0)
6908 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6916 // 00-17, 19, 1C-1F, 20-7E --> put
6919 || ((ch
>= 0x1C) && (ch
<= 0x1F))
6920 || ((ch
>= 0x20) && (ch
<= 0x7E))
6922 sixelParseBuffer
.append((char) ch
);
6928 case SOSPMAPC_STRING
:
6929 // 00-17, 19, 1C-1F, 20-7F --> ignore
6931 // Special case for Jexer: PM can pass one control character
6936 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6940 // 0x9C goes to GROUND
6948 // Special case for Xterm: OSC can pass control characters
6949 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
6953 // 00-17, 19, 1C-1F --> ignore
6955 // 20-7F --> osc_put
6956 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6960 // 0x9C goes to GROUND
6967 case VT52_DIRECT_CURSOR_ADDRESS
:
6968 // This is a special case for the VT52 sequence "ESC Y l c"
6969 if (collectBuffer
.length() == 0) {
6971 } else if (collectBuffer
.length() == 1) {
6972 // We've got the two characters, one in the buffer and the
6974 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
6983 * Expose current cursor X to outside world.
6985 * @return current cursor X
6987 public final int getCursorX() {
6988 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6989 return currentState
.cursorX
* 2;
6991 return currentState
.cursorX
;
6995 * Expose current cursor Y to outside world.
6997 * @return current cursor Y
6999 public final int getCursorY() {
7000 return currentState
.cursorY
;
7004 * Returns true if this terminal has requested the mouse pointer be
7007 * @return true if this terminal has requested the mouse pointer be
7010 public final boolean hasHiddenMousePointer() {
7011 return hideMousePointer
;
7015 * Get the mouse protocol.
7017 * @return MouseProtocol.OFF, MouseProtocol.X10, etc.
7019 public MouseProtocol
getMouseProtocol() {
7020 return mouseProtocol
;
7024 * Draw the left and right cells of a two-cell-wide (full-width) glyph.
7026 * @param leftX the x position to draw the left half to
7027 * @param leftY the y position to draw the left half to
7028 * @param rightX the x position to draw the right half to
7029 * @param rightY the y position to draw the right half to
7030 * @param ch the character to draw
7032 private void drawHalves(final int leftX
, final int leftY
,
7033 final int rightX
, final int rightY
, final int ch
) {
7035 // System.err.println("drawHalves(): " + Integer.toHexString(ch));
7037 if (lastTextHeight
!= textHeight
) {
7038 glyphMaker
= GlyphMaker
.getInstance(textHeight
);
7039 lastTextHeight
= textHeight
;
7042 Cell cell
= new Cell(ch
, currentState
.attr
);
7043 BufferedImage image
= glyphMaker
.getImage(cell
, textWidth
* 2,
7045 BufferedImage leftImage
= image
.getSubimage(0, 0, textWidth
,
7047 BufferedImage rightImage
= image
.getSubimage(textWidth
, 0, textWidth
,
7050 Cell left
= new Cell(cell
);
7051 left
.setImage(leftImage
);
7052 left
.setWidth(Cell
.Width
.LEFT
);
7053 display
.get(leftY
).replace(leftX
, left
);
7055 Cell right
= new Cell(cell
);
7056 right
.setImage(rightImage
);
7057 right
.setWidth(Cell
.Width
.RIGHT
);
7058 display
.get(rightY
).replace(rightX
, right
);
7062 * Set the width of a character cell in pixels.
7064 * @param textWidth the width in pixels of a character cell
7066 public void setTextWidth(final int textWidth
) {
7067 this.textWidth
= textWidth
;
7071 * Set the height of a character cell in pixels.
7073 * @param textHeight the height in pixels of a character cell
7075 public void setTextHeight(final int textHeight
) {
7076 this.textHeight
= textHeight
;
7080 * Parse a sixel string into a bitmap image, and overlay that image onto
7083 private void parseSixel() {
7086 System.err.println("parseSixel(): '" + sixelParseBuffer.toString()
7090 Sixel sixel
= new Sixel(sixelParseBuffer
.toString(), sixelPalette
);
7091 BufferedImage image
= sixel
.getImage();
7093 // System.err.println("parseSixel(): image " + image);
7095 if (image
== null) {
7096 // Sixel data was malformed in some way, bail out.
7099 if ((image
.getWidth() < 1)
7100 || (image
.getWidth() > 10000)
7101 || (image
.getHeight() < 1)
7102 || (image
.getHeight() > 10000)
7107 imageToCells(image
, true);
7111 * Parse a "Jexer" RGB image string into a bitmap image, and overlay that
7112 * image onto the text cells.
7114 * @param pw width token
7115 * @param ph height token
7116 * @param ps scroll token
7117 * @param data pixel data
7119 private void parseJexerImageRGB(final String pw
, final String ph
,
7120 final String ps
, final String data
) {
7123 int imageHeight
= 0;
7124 boolean scroll
= false;
7126 imageWidth
= Integer
.parseInt(pw
);
7127 imageHeight
= Integer
.parseInt(ph
);
7128 } catch (NumberFormatException e
) {
7132 if ((imageWidth
< 1)
7133 || (imageWidth
> 10000)
7134 || (imageHeight
< 1)
7135 || (imageHeight
> 10000)
7139 if (ps
.equals("1")) {
7141 } else if (ps
.equals("0")) {
7147 byte [] bytes
= StringUtils
.fromBase64(data
.getBytes());
7148 if (bytes
.length
!= (imageWidth
* imageHeight
* 3)) {
7152 BufferedImage image
= new BufferedImage(imageWidth
, imageHeight
,
7153 BufferedImage
.TYPE_INT_ARGB
);
7155 for (int x
= 0; x
< imageWidth
; x
++) {
7156 for (int y
= 0; y
< imageHeight
; y
++) {
7157 int red
= bytes
[(y
* imageWidth
* 3) + (x
* 3) ];
7161 int green
= bytes
[(y
* imageWidth
* 3) + (x
* 3) + 1];
7165 int blue
= bytes
[(y
* imageWidth
* 3) + (x
* 3) + 2];
7169 int rgb
= 0xFF000000 | (red
<< 16) | (green
<< 8) | blue
;
7170 image
.setRGB(x
, y
, rgb
);
7174 imageToCells(image
, scroll
);
7178 * Parse a "Jexer" PNG or JPG image string into a bitmap image, and
7179 * overlay that image onto the text cells.
7181 * @param type 1 for PNG, 2 for JPG
7182 * @param ps scroll token
7183 * @param data pixel data
7185 private void parseJexerImageFile(final int type
, final String ps
,
7186 final String data
) {
7189 int imageHeight
= 0;
7190 boolean scroll
= false;
7191 BufferedImage image
= null;
7193 byte [] bytes
= StringUtils
.fromBase64(data
.getBytes());
7197 if ((bytes
[0] != (byte) 0x89)
7198 || (bytes
[1] != 'P')
7199 || (bytes
[2] != 'N')
7200 || (bytes
[3] != 'G')
7201 || (bytes
[4] != (byte) 0x0D)
7202 || (bytes
[5] != (byte) 0x0A)
7203 || (bytes
[6] != (byte) 0x1A)
7204 || (bytes
[7] != (byte) 0x0A)
7206 // File does not have PNG header, bail out.
7212 if ((bytes
[0] != (byte) 0XFF)
7213 || (bytes
[1] != (byte) 0xD8)
7214 || (bytes
[2] != (byte) 0xFF)
7216 // File does not have JPG header, bail out.
7222 // Unsupported type, bail out.
7226 image
= ImageIO
.read(new ByteArrayInputStream(bytes
));
7227 } catch (IOException e
) {
7231 assert (image
!= null);
7232 imageWidth
= image
.getWidth();
7233 imageHeight
= image
.getHeight();
7234 if ((imageWidth
< 1)
7235 || (imageWidth
> 10000)
7236 || (imageHeight
< 1)
7237 || (imageHeight
> 10000)
7241 if (ps
.equals("1")) {
7243 } else if (ps
.equals("0")) {
7249 imageToCells(image
, scroll
);
7253 * Break up an image into the cells at the current cursor.
7255 * @param image the image to display
7256 * @param scroll if true, scroll the image and move the cursor
7258 private void imageToCells(final BufferedImage image
, final boolean scroll
) {
7259 assert (image
!= null);
7264 * Break up the image into text cell sized pieces as a new array of
7267 * Note original column position x0.
7271 * 1. Advance (printCharacter(' ')) for horizontal increment, or
7272 * index (linefeed() + cursorPosition(y, x0)) for vertical
7275 * 2. Set (x, y) cell image data.
7277 * 3. For the right and bottom edges:
7279 * a. Render the text to pixels using Terminus font.
7281 * b. Blit the image on top of the text, using alpha channel.
7283 int cellColumns
= image
.getWidth() / textWidth
;
7284 if (cellColumns
* textWidth
< image
.getWidth()) {
7287 int cellRows
= image
.getHeight() / textHeight
;
7288 if (cellRows
* textHeight
< image
.getHeight()) {
7292 // Break the image up into an array of cells.
7293 Cell
[][] cells
= new Cell
[cellColumns
][cellRows
];
7295 for (int x
= 0; x
< cellColumns
; x
++) {
7296 for (int y
= 0; y
< cellRows
; y
++) {
7298 int width
= textWidth
;
7299 if ((x
+ 1) * textWidth
> image
.getWidth()) {
7300 width
= image
.getWidth() - (x
* textWidth
);
7302 int height
= textHeight
;
7303 if ((y
+ 1) * textHeight
> image
.getHeight()) {
7304 height
= image
.getHeight() - (y
* textHeight
);
7307 Cell cell
= new Cell();
7308 if ((width
!= textWidth
) || (height
!= textHeight
)) {
7309 BufferedImage newImage
;
7310 newImage
= new BufferedImage(textWidth
, textHeight
,
7311 BufferedImage
.TYPE_INT_ARGB
);
7313 java
.awt
.Graphics gr
= newImage
.getGraphics();
7314 gr
.drawImage(image
.getSubimage(x
* textWidth
,
7315 y
* textHeight
, width
, height
),
7318 cell
.setImage(newImage
);
7320 cell
.setImage(image
.getSubimage(x
* textWidth
,
7321 y
* textHeight
, width
, height
));
7328 int x0
= currentState
.cursorX
;
7329 int y0
= currentState
.cursorY
;
7330 for (int y
= 0; y
< cellRows
; y
++) {
7331 for (int x
= 0; x
< cellColumns
; x
++) {
7332 assert (currentState
.cursorX
<= rightMargin
);
7334 // A real sixel terminal would render the text of the current
7335 // cell first, then image over it (accounting for blank
7336 // pixels). We do not support that. A cell is either text,
7337 // or image, but not a mix of image-over-text.
7338 DisplayLine line
= display
.get(currentState
.cursorY
);
7339 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
7341 // If at the end of the visible screen, stop.
7342 if (currentState
.cursorX
== rightMargin
) {
7345 // Room for more image on the visible screen.
7346 currentState
.cursorX
++;
7348 if (currentState
.cursorY
< scrollRegionBottom
- 1) {
7349 // Not at the bottom, down a line.
7351 } else if (scroll
== true) {
7352 // At the bottom, scroll as needed.
7355 // At the bottom, no more scrolling, done.
7359 cursorPosition(currentState
.cursorY
, x0
);
7362 if (scroll
== false) {
7363 cursorPosition(y0
, x0
);