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 * Number of bytes/characters passed to consume().
512 private long readCount
= 0;
515 * DECSC/DECRC save/restore a subset of the total state. This class
516 * encapsulates those specific flags/modes.
518 private class SaveableState
{
521 * When true, cursor positions are relative to the scrolling region.
523 public boolean originMode
= false;
526 * The current editing X position.
528 public int cursorX
= 0;
531 * The current editing Y position.
533 public int cursorY
= 0;
536 * Which character set is currently selected in G0.
538 public CharacterSet g0Charset
= CharacterSet
.US
;
541 * Which character set is currently selected in G1.
543 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
546 * Which character set is currently selected in G2.
548 public CharacterSet g2Charset
= CharacterSet
.US
;
551 * Which character set is currently selected in G3.
553 public CharacterSet g3Charset
= CharacterSet
.US
;
556 * Which character set is currently selected in GR.
558 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
561 * The current drawing attributes.
563 public CellAttributes attr
;
568 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
573 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
578 public boolean lineWrap
= true;
583 public void reset() {
587 g0Charset
= CharacterSet
.US
;
588 g1Charset
= CharacterSet
.DRAWING
;
589 g2Charset
= CharacterSet
.US
;
590 g3Charset
= CharacterSet
.US
;
591 grCharset
= CharacterSet
.DRAWING
;
592 attr
= new CellAttributes();
593 glLockshift
= LockshiftMode
.NONE
;
594 grLockshift
= LockshiftMode
.NONE
;
599 * Copy attributes from another instance.
601 * @param that the other instance to match
603 public void setTo(final SaveableState that
) {
604 this.originMode
= that
.originMode
;
605 this.cursorX
= that
.cursorX
;
606 this.cursorY
= that
.cursorY
;
607 this.g0Charset
= that
.g0Charset
;
608 this.g1Charset
= that
.g1Charset
;
609 this.g2Charset
= that
.g2Charset
;
610 this.g3Charset
= that
.g3Charset
;
611 this.grCharset
= that
.grCharset
;
612 this.attr
= new CellAttributes();
613 this.attr
.setTo(that
.attr
);
614 this.glLockshift
= that
.glLockshift
;
615 this.grLockshift
= that
.grLockshift
;
616 this.lineWrap
= that
.lineWrap
;
620 * Public constructor.
622 public SaveableState() {
627 // ------------------------------------------------------------------------
628 // Constructors -----------------------------------------------------------
629 // ------------------------------------------------------------------------
632 * Public constructor.
634 * @param type one of the DeviceType constants to select VT100, VT102,
636 * @param inputStream an InputStream connected to the remote side. For
637 * type == XTERM, inputStream is converted to a Reader with UTF-8
639 * @param outputStream an OutputStream connected to the remote user. For
640 * type == XTERM, outputStream is converted to a Writer with UTF-8
642 * @param displayListener a callback to the outer display, or null for
643 * default VT100 behavior
644 * @throws UnsupportedEncodingException if an exception is thrown when
645 * creating the InputStreamReader
647 public ECMA48(final DeviceType type
, final InputStream inputStream
,
648 final OutputStream outputStream
, final DisplayListener displayListener
)
649 throws UnsupportedEncodingException
{
651 assert (inputStream
!= null);
652 assert (outputStream
!= null);
654 csiParams
= new ArrayList
<Integer
>();
655 tabStops
= new ArrayList
<Integer
>();
656 scrollback
= new ArrayList
<DisplayLine
>();
657 display
= new ArrayList
<DisplayLine
>();
660 if (inputStream
instanceof TimeoutInputStream
) {
661 this.inputStream
= (TimeoutInputStream
)inputStream
;
663 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
665 if (type
== DeviceType
.XTERM
) {
666 this.input
= new InputStreamReader(new BufferedInputStream(
667 this.inputStream
, 1024 * 128), "UTF-8");
668 this.output
= new OutputStreamWriter(new
669 BufferedOutputStream(outputStream
), "UTF-8");
670 this.outputStream
= null;
673 this.outputStream
= new BufferedOutputStream(outputStream
);
675 this.displayListener
= displayListener
;
678 for (int i
= 0; i
< height
; i
++) {
679 display
.add(new DisplayLine(currentState
.attr
));
681 assert (currentState
.cursorY
< height
);
682 assert (currentState
.cursorX
< width
);
684 // Spin up the input reader
685 readerThread
= new Thread(this);
686 readerThread
.start();
689 // ------------------------------------------------------------------------
690 // Runnable ---------------------------------------------------------------
691 // ------------------------------------------------------------------------
694 * Read function runs on a separate thread.
696 public final void run() {
697 boolean utf8
= false;
698 boolean done
= false;
700 if (type
== DeviceType
.XTERM
) {
704 // available() will often return > 1, so we need to read in chunks to
706 char [] readBufferUTF8
= null;
707 byte [] readBuffer
= null;
709 readBufferUTF8
= new char[2048];
711 readBuffer
= new byte[2048];
714 while (!done
&& !stopReaderThread
) {
715 synchronized (userQueue
) {
716 while (userQueue
.size() > 0) {
717 handleUserEvent(userQueue
.remove(0));
722 int n
= inputStream
.available();
724 // System.err.printf("available() %d\n", n); System.err.flush();
726 if (readBufferUTF8
.length
< n
) {
727 // The buffer wasn't big enough, make it huger
728 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
731 readBufferUTF8
= new char[newSizeHalf
* 2];
734 if (readBuffer
.length
< n
) {
735 // The buffer wasn't big enough, make it huger
736 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
737 readBuffer
= new byte[newSizeHalf
* 2];
743 } catch (InterruptedException e
) {
752 rc
= input
.read(readBufferUTF8
, 0,
753 readBufferUTF8
.length
);
755 rc
= inputStream
.read(readBuffer
, 0,
758 } catch (ReadTimeoutException e
) {
762 // System.err.printf("read() %d\n", rc); System.err.flush();
767 // Don't step on UI events
768 synchronized (this) {
770 for (int i
= 0; i
< rc
;) {
771 int ch
= Character
.codePointAt(readBufferUTF8
,
773 i
+= Character
.charCount(ch
);
775 // Special case for VT10x: 7-bit characters
777 if ((type
== DeviceType
.VT100
)
778 || (type
== DeviceType
.VT102
)
786 for (int i
= 0; i
< rc
; i
++) {
787 // Special case for VT10x: 7-bit characters
789 if ((type
== DeviceType
.VT100
)
790 || (type
== DeviceType
.VT102
)
792 consume(readBuffer
[i
] & 0x7F);
794 consume(readBuffer
[i
]);
799 // Permit my enclosing UI to know that I updated.
800 if (displayListener
!= null) {
801 displayListener
.displayChanged();
804 // System.err.println("end while loop"); System.err.flush();
805 } catch (IOException e
) {
808 // This is an unusual case. We want to see the stack trace,
809 // but it is related to the spawned process rather than the
810 // actual UI. We will generate the stack trace, and consume
811 // it as though it was emitted by the shell.
812 CharArrayWriter writer
= new CharArrayWriter();
813 // Send a ST and RIS to clear the emulator state.
815 writer
.write("\033\\\033c");
816 writer
.write("\n-----------------------------------\n");
817 e
.printStackTrace(new PrintWriter(writer
));
818 writer
.write("\n-----------------------------------\n");
819 } catch (IOException e2
) {
822 char [] stackTrace
= writer
.toCharArray();
823 for (int i
= 0; i
< stackTrace
.length
; i
++) {
824 if (stackTrace
[i
] == '\n') {
827 consume(stackTrace
[i
]);
831 } // while ((done == false) && (stopReaderThread == false))
833 // Let the rest of the world know that I am done.
834 stopReaderThread
= true;
837 inputStream
.cancelRead();
840 } catch (IOException e
) {
846 } catch (IOException e
) {
850 // Permit my enclosing UI to know that I updated.
851 if (displayListener
!= null) {
852 displayListener
.displayChanged();
855 // System.err.println("*** run() exiting..."); System.err.flush();
858 // ------------------------------------------------------------------------
859 // ECMA48 -----------------------------------------------------------------
860 // ------------------------------------------------------------------------
863 * Wait for a period of time to get output from the launched process.
865 * @param millis millis to wait for, or 0 to wait forever
866 * @return true if the launched process has emitted something
868 public boolean waitForOutput(final int millis
) {
870 throw new IllegalArgumentException("timeout must be >= 0");
872 int waitedMillis
= millis
;
873 final int pollTimeout
= 5;
875 if (readCount
!= 0) {
878 if ((millis
> 0) && (waitedMillis
< 0)){
882 Thread
.sleep(pollTimeout
);
883 } catch (InterruptedException e
) {
886 waitedMillis
-= pollTimeout
;
891 * Process keyboard and mouse events from the user.
893 * @param event the input event to consume
895 private void handleUserEvent(final TInputEvent event
) {
896 if (event
instanceof TKeypressEvent
) {
897 keypress(((TKeypressEvent
) event
).getKey());
899 if (event
instanceof TMouseEvent
) {
900 mouse((TMouseEvent
) event
);
905 * Add a keyboard and mouse event from the user to the queue.
907 * @param event the input event to consume
909 public void addUserEvent(final TInputEvent event
) {
910 synchronized (userQueue
) {
911 userQueue
.add(event
);
916 * Return the proper primary Device Attributes string.
918 * @return string to send to remote side that is appropriate for the
921 private String
deviceTypeResponse() {
924 // "I am a VT100 with advanced video option" (often VT102)
933 // "I am a VT220" - 7 bit version, with sixel and Jexer image
936 return "\033[?62;1;6;9;4;22;444c";
938 // "I am a VT220" - 8 bit version, with sixel and Jexer image
940 return "\u009b?62;1;6;9;4;22;444c";
942 throw new IllegalArgumentException("Invalid device type: " + type
);
947 * Return the proper TERM environment variable for this device type.
949 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
950 * @return "vt100", "xterm", etc.
952 public static String
deviceTypeTerm(final DeviceType deviceType
) {
953 switch (deviceType
) {
967 throw new IllegalArgumentException("Invalid device type: "
973 * Return the proper LANG for this device type. Only XTERM devices know
974 * about UTF-8, the others are defined by their standard to be either
975 * 7-bit or 8-bit characters only.
977 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
978 * @param baseLang a base language without UTF-8 flag such as "C" or
980 * @return "en_US", "en_US.UTF-8", etc.
982 public static String
deviceTypeLang(final DeviceType deviceType
,
983 final String baseLang
) {
985 switch (deviceType
) {
993 return baseLang
+ ".UTF-8";
996 throw new IllegalArgumentException("Invalid device type: "
1002 * Write a string directly to the remote side.
1004 * @param str string to send
1006 public void writeRemote(final String str
) {
1007 if (stopReaderThread
) {
1008 // Reader hit EOF, bail out now.
1013 // System.err.printf("writeRemote() '%s'\n", str);
1019 if (outputStream
== null) {
1023 outputStream
.flush();
1024 for (int i
= 0; i
< str
.length(); i
++) {
1025 outputStream
.write(str
.charAt(i
));
1027 outputStream
.flush();
1028 } catch (IOException e
) {
1034 if (output
== null) {
1041 } catch (IOException e
) {
1047 throw new IllegalArgumentException("Invalid device type: " + type
);
1052 * Close the input and output streams and stop the reader thread. Note
1053 * that it is safe to call this multiple times.
1055 public final void close() {
1057 // Tell the reader thread to stop looking at input. It will close
1058 // the input streams.
1059 if (stopReaderThread
== false) {
1060 stopReaderThread
= true;
1063 // Now close the output stream.
1068 if (outputStream
!= null) {
1070 outputStream
.close();
1071 } catch (IOException e
) {
1074 outputStream
= null;
1078 if (outputStream
!= null) {
1080 outputStream
.close();
1081 } catch (IOException e
) {
1084 outputStream
= null;
1086 if (output
!= null) {
1089 } catch (IOException e
) {
1096 throw new IllegalArgumentException("Invalid device type: " +
1102 * See if the reader thread is still running.
1104 * @return if true, we are still connected to / reading from the remote
1107 public final boolean isReading() {
1108 return (!stopReaderThread
);
1112 * Obtain a new blank display line for an external user
1113 * (e.g. TTerminalWindow).
1115 * @return new blank line
1117 public final DisplayLine
getBlankDisplayLine() {
1118 return new DisplayLine(currentState
.attr
);
1122 * Get the scrollback buffer.
1124 * @return the scrollback buffer
1126 public final List
<DisplayLine
> getScrollbackBuffer() {
1131 * Get the display buffer.
1133 * @return the display buffer
1135 public final List
<DisplayLine
> getDisplayBuffer() {
1140 * Get the visible display + scrollback buffer, offset by a specified
1141 * number of rows from the bottom.
1143 * @param visibleHeight the total height of the display to show
1144 * @param scrollBottom the number of rows from the bottom to scroll back
1145 * @return a copy of the display + scrollback buffers
1147 public final List
<DisplayLine
> getVisibleDisplay(final int visibleHeight
,
1148 final int scrollBottom
) {
1150 assert (visibleHeight
>= 0);
1151 assert (scrollBottom
>= 0);
1153 int visibleBottom
= scrollback
.size() + display
.size() - scrollBottom
;
1155 List
<DisplayLine
> preceedingBlankLines
= new ArrayList
<DisplayLine
>();
1156 int visibleTop
= visibleBottom
- visibleHeight
;
1157 if (visibleTop
< 0) {
1158 for (int i
= visibleTop
; i
< 0; i
++) {
1159 preceedingBlankLines
.add(getBlankDisplayLine());
1163 assert (visibleTop
>= 0);
1165 List
<DisplayLine
> displayLines
= new ArrayList
<DisplayLine
>();
1166 displayLines
.addAll(scrollback
);
1167 displayLines
.addAll(display
);
1169 List
<DisplayLine
> visibleLines
= new ArrayList
<DisplayLine
>();
1170 visibleLines
.addAll(preceedingBlankLines
);
1171 visibleLines
.addAll(displayLines
.subList(visibleTop
, visibleBottom
));
1173 // Fill in the blank lines on bottom
1174 int bottomBlankLines
= visibleHeight
- visibleLines
.size();
1175 assert (bottomBlankLines
>= 0);
1176 for (int i
= 0; i
< bottomBlankLines
; i
++) {
1177 visibleLines
.add(getBlankDisplayLine());
1180 return copyBuffer(visibleLines
);
1184 * Copy a display buffer.
1186 * @param buffer the buffer to copy
1187 * @return a deep copy of the buffer's data
1189 private List
<DisplayLine
> copyBuffer(final List
<DisplayLine
> buffer
) {
1190 ArrayList
<DisplayLine
> result
= new ArrayList
<DisplayLine
>(buffer
.size());
1191 for (DisplayLine line
: buffer
) {
1192 result
.add(new DisplayLine(line
));
1198 * Get the display width.
1200 * @return the width (usually 80 or 132)
1202 public final int getWidth() {
1207 * Set the display width.
1209 * @param width the new width
1211 public final synchronized void setWidth(final int width
) {
1213 rightMargin
= width
- 1;
1214 if (currentState
.cursorX
>= width
) {
1215 currentState
.cursorX
= width
- 1;
1217 if (savedState
.cursorX
>= width
) {
1218 savedState
.cursorX
= width
- 1;
1223 * Get the display height.
1225 * @return the height (usually 24)
1227 public final int getHeight() {
1232 * Set the display height.
1234 * @param height the new height
1236 public final synchronized void setHeight(final int height
) {
1237 int delta
= height
- this.height
;
1238 this.height
= height
;
1239 scrollRegionBottom
+= delta
;
1240 if ((scrollRegionBottom
< 0) || (scrollRegionTop
> height
- 1)) {
1241 scrollRegionBottom
= height
- 1;
1243 if (scrollRegionTop
>= scrollRegionBottom
) {
1244 scrollRegionTop
= 0;
1246 if (currentState
.cursorY
>= height
) {
1247 currentState
.cursorY
= height
- 1;
1249 if (savedState
.cursorY
>= height
) {
1250 savedState
.cursorY
= height
- 1;
1252 while (display
.size() < height
) {
1253 DisplayLine line
= new DisplayLine(currentState
.attr
);
1254 line
.setReverseColor(reverseVideo
);
1257 while (display
.size() > height
) {
1258 scrollback
.add(display
.remove(0));
1263 * Get visible cursor flag.
1265 * @return if true, the cursor is visible
1267 public final boolean isCursorVisible() {
1268 return cursorVisible
;
1272 * Get the screen title as set by the xterm OSC sequence. Lots of
1273 * applications send a screenTitle regardless of whether it is an xterm
1276 * @return screen title
1278 public final String
getScreenTitle() {
1283 * Get 132 columns value.
1285 * @return if true, the terminal is in 132 column mode
1287 public final boolean isColumns132() {
1292 * Clear the CSI parameters and flags.
1294 private void toGround() {
1296 collectBuffer
.setLength(0);
1297 scanState
= ScanState
.GROUND
;
1301 * Reset the tab stops list.
1303 private void resetTabStops() {
1305 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
1306 tabStops
.add(Integer
.valueOf(i
* 8));
1311 * Reset the 88- or 256-colors.
1313 private void resetColors() {
1314 colors88
= new ArrayList
<Integer
>(256);
1315 for (int i
= 0; i
< 256; i
++) {
1319 // Set default system colors.
1320 colors88
.set(0, 0x00000000);
1321 colors88
.set(1, 0x00a80000);
1322 colors88
.set(2, 0x0000a800);
1323 colors88
.set(3, 0x00a85400);
1324 colors88
.set(4, 0x000000a8);
1325 colors88
.set(5, 0x00a800a8);
1326 colors88
.set(6, 0x0000a8a8);
1327 colors88
.set(7, 0x00a8a8a8);
1329 colors88
.set(8, 0x00545454);
1330 colors88
.set(9, 0x00fc5454);
1331 colors88
.set(10, 0x0054fc54);
1332 colors88
.set(11, 0x00fcfc54);
1333 colors88
.set(12, 0x005454fc);
1334 colors88
.set(13, 0x00fc54fc);
1335 colors88
.set(14, 0x0054fcfc);
1336 colors88
.set(15, 0x00fcfcfc);
1340 * Get the RGB value of one of the indexed colors.
1342 * @param index the color index
1343 * @return the RGB value
1345 private int get88Color(final int index
) {
1346 // System.err.print("get88Color: " + index);
1347 if ((index
< 0) || (index
> colors88
.size())) {
1348 // System.err.println(" -- UNKNOWN");
1351 // System.err.printf(" %08x\n", colors88.get(index));
1352 return colors88
.get(index
);
1356 * Set one of the indexed colors to a color specification.
1358 * @param index the color index
1359 * @param spec the specification, typically something like "rgb:aa/bb/cc"
1361 private void set88Color(final int index
, final String spec
) {
1362 // System.err.println("set88Color: " + index + " '" + spec + "'");
1364 if ((index
< 0) || (index
> colors88
.size())) {
1367 if (spec
.startsWith("rgb:")) {
1368 String
[] rgbTokens
= spec
.substring(4).split("/");
1369 if (rgbTokens
.length
== 3) {
1371 int rgb
= (Integer
.parseInt(rgbTokens
[0], 16) << 16);
1372 rgb
|= Integer
.parseInt(rgbTokens
[1], 16) << 8;
1373 rgb
|= Integer
.parseInt(rgbTokens
[2], 16);
1374 // System.err.printf(" set to %08x\n", rgb);
1375 colors88
.set(index
, rgb
);
1376 } catch (NumberFormatException e
) {
1383 if (spec
.toLowerCase().equals("black")) {
1384 colors88
.set(index
, 0x00000000);
1385 } else if (spec
.toLowerCase().equals("red")) {
1386 colors88
.set(index
, 0x00a80000);
1387 } else if (spec
.toLowerCase().equals("green")) {
1388 colors88
.set(index
, 0x0000a800);
1389 } else if (spec
.toLowerCase().equals("yellow")) {
1390 colors88
.set(index
, 0x00a85400);
1391 } else if (spec
.toLowerCase().equals("blue")) {
1392 colors88
.set(index
, 0x000000a8);
1393 } else if (spec
.toLowerCase().equals("magenta")) {
1394 colors88
.set(index
, 0x00a800a8);
1395 } else if (spec
.toLowerCase().equals("cyan")) {
1396 colors88
.set(index
, 0x0000a8a8);
1397 } else if (spec
.toLowerCase().equals("white")) {
1398 colors88
.set(index
, 0x00a8a8a8);
1404 * Reset the emulation state.
1406 private void reset() {
1408 currentState
= new SaveableState();
1409 savedState
= new SaveableState();
1410 scanState
= ScanState
.GROUND
;
1411 if (displayListener
!= null) {
1412 width
= displayListener
.getDisplayWidth();
1413 height
= displayListener
.getDisplayHeight();
1418 scrollRegionTop
= 0;
1419 scrollRegionBottom
= height
- 1;
1420 rightMargin
= width
- 1;
1421 newLineMode
= false;
1422 arrowKeyMode
= ArrowKeyMode
.ANSI
;
1423 keypadMode
= KeypadMode
.Numeric
;
1424 wrapLineFlag
= false;
1431 newLineMode
= false;
1432 reverseVideo
= false;
1434 cursorVisible
= true;
1437 singleshift
= Singleshift
.NONE
;
1439 printerControllerMode
= false;
1442 mouseProtocol
= MouseProtocol
.OFF
;
1443 mouseEncoding
= MouseEncoding
.X10
;
1448 // Reset extra colors
1456 * Append a new line to the bottom of the display, adding lines off the
1457 * top to the scrollback buffer.
1459 private void newDisplayLine() {
1460 // Scroll the top line off into the scrollback buffer
1461 scrollback
.add(display
.get(0));
1462 if (scrollback
.size() > maxScrollback
) {
1463 scrollback
.remove(0);
1464 scrollback
.trimToSize();
1467 display
.trimToSize();
1468 DisplayLine line
= new DisplayLine(currentState
.attr
);
1469 line
.setReverseColor(reverseVideo
);
1474 * Wraps the current line.
1476 private void wrapCurrentLine() {
1477 if (currentState
.cursorY
== height
- 1) {
1480 if (currentState
.cursorY
< height
- 1) {
1481 currentState
.cursorY
++;
1483 currentState
.cursorX
= 0;
1487 * Handle a carriage return.
1489 private void carriageReturn() {
1490 currentState
.cursorX
= 0;
1491 wrapLineFlag
= false;
1495 * Reverse the color of the visible display.
1497 private void invertDisplayColors() {
1498 for (DisplayLine line
: display
) {
1499 line
.setReverseColor(!line
.isReverseColor());
1504 * Handle a linefeed.
1506 private void linefeed() {
1507 if (currentState
.cursorY
< scrollRegionBottom
) {
1508 // Increment screen y
1509 currentState
.cursorY
++;
1512 // Screen y does not increment
1515 * Two cases: either we're inside a scrolling region or not. If
1516 * the scrolling region bottom is the bottom of the screen, then
1517 * push the top line into the buffer. Else scroll the scrolling
1520 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1522 // We're at the bottom of the scroll region, AND the scroll
1523 // region is the entire screen.
1529 // We're at the bottom of the scroll region, AND the scroll
1530 // region is NOT the entire screen.
1531 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1536 currentState
.cursorX
= 0;
1538 wrapLineFlag
= false;
1542 * Prints one character to the display buffer.
1544 * @param ch character to display
1546 private void printCharacter(final int ch
) {
1547 int rightMargin
= this.rightMargin
;
1549 if (StringUtils
.width(ch
) == 2) {
1550 // This is a full-width character. Save two spaces, and then
1551 // draw the character as two image halves.
1552 int x0
= currentState
.cursorX
;
1553 int y0
= currentState
.cursorY
;
1554 printCharacter(' ');
1555 printCharacter(' ');
1556 if ((currentState
.cursorX
== x0
+ 2)
1557 && (currentState
.cursorY
== y0
)
1559 // We can draw both halves of the character.
1560 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1561 } else if ((currentState
.cursorX
== x0
+ 1)
1562 && (currentState
.cursorY
== y0
)
1564 // VT100 line wrap behavior: we should be at the right
1565 // margin. We can draw both halves of the character.
1566 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1568 // The character splits across the line. Draw the entire
1569 // character on the new line, giving one more space for it.
1570 x0
= currentState
.cursorX
- 1;
1571 y0
= currentState
.cursorY
;
1572 printCharacter(' ');
1573 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1578 // Check if we have double-width, and if so chop at 40/66 instead of
1580 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1581 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1584 // Check the unusually-complicated line wrapping conditions...
1585 if (currentState
.cursorX
== rightMargin
) {
1587 if (currentState
.lineWrap
== true) {
1589 * This case happens when: the cursor was already on the
1590 * right margin (either through printing or by an explicit
1591 * placement command), and a character was printed.
1593 * The line wraps only when a new character arrives AND the
1594 * cursor is already on the right margin AND has placed a
1595 * character in its cell. Easier to see than to explain.
1597 if (wrapLineFlag
== false) {
1599 * This block marks the case that we are in the margin
1600 * and the first character has been received and printed.
1602 wrapLineFlag
= true;
1605 * This block marks the case that we are in the margin
1606 * and the second character has been received and
1609 wrapLineFlag
= false;
1613 } else if (currentState
.cursorX
<= rightMargin
) {
1615 * This is the normal case: a character came in and was printed
1616 * to the left of the right margin column.
1619 // Turn off VT100 special-case flag
1620 wrapLineFlag
= false;
1623 // "Print" the character
1624 Cell newCell
= new Cell(ch
);
1625 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1626 newCellAttributes
.setTo(currentState
.attr
);
1627 DisplayLine line
= display
.get(currentState
.cursorY
);
1629 if (StringUtils
.width(ch
) == 1) {
1630 // Insert mode special case
1631 if (insertMode
== true) {
1632 line
.insert(currentState
.cursorX
, newCell
);
1634 // Replace an existing character
1635 line
.replace(currentState
.cursorX
, newCell
);
1638 // Increment horizontal
1639 if (wrapLineFlag
== false) {
1640 currentState
.cursorX
++;
1641 if (currentState
.cursorX
> rightMargin
) {
1642 currentState
.cursorX
--;
1649 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1650 * send to the remote side.
1652 * @param mouse mouse event received from the local user
1654 private void mouse(final TMouseEvent mouse
) {
1657 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1658 mouseProtocol, mouseEncoding, mouse);
1661 if (mouseEncoding
== MouseEncoding
.X10
) {
1662 // We will support X10 but only for (160,94) and smaller.
1663 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1668 switch (mouseProtocol
) {
1675 // Only report button presses
1676 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1682 // Only report button presses and releases
1683 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1684 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1692 * Only report button presses, button releases, and motions that
1693 * have a button down (i.e. drag-and-drop).
1695 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1696 if (!mouse
.isMouse1()
1697 && !mouse
.isMouse2()
1698 && !mouse
.isMouse3()
1699 && !mouse
.isMouseWheelUp()
1700 && !mouse
.isMouseWheelDown()
1708 // Report everything
1712 // Now encode the event
1713 StringBuilder sb
= new StringBuilder(6);
1714 if (mouseEncoding
== MouseEncoding
.SGR
) {
1715 sb
.append((char) 0x1B);
1719 if (mouse
.isMouse1()) {
1720 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1725 } else if (mouse
.isMouse2()) {
1726 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1731 } else if (mouse
.isMouse3()) {
1732 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1737 } else if (mouse
.isMouseWheelUp()) {
1739 } else if (mouse
.isMouseWheelDown()) {
1742 // This is motion with no buttons down.
1745 if (mouse
.isAlt()) {
1748 if (mouse
.isCtrl()) {
1751 if (mouse
.isShift()) {
1755 sb
.append(String
.format("%d;%d;%d", buttons
, mouse
.getX() + 1,
1758 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1765 // X10 and UTF8 encodings
1766 sb
.append((char) 0x1B);
1770 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1771 buttons
= 0x03 + 32;
1772 } else if (mouse
.isMouse1()) {
1773 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1774 buttons
= 0x00 + 32 + 32;
1776 buttons
= 0x00 + 32;
1778 } else if (mouse
.isMouse2()) {
1779 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1780 buttons
= 0x01 + 32 + 32;
1782 buttons
= 0x01 + 32;
1784 } else if (mouse
.isMouse3()) {
1785 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1786 buttons
= 0x02 + 32 + 32;
1788 buttons
= 0x02 + 32;
1790 } else if (mouse
.isMouseWheelUp()) {
1791 buttons
= 0x04 + 64;
1792 } else if (mouse
.isMouseWheelDown()) {
1793 buttons
= 0x05 + 64;
1795 // This is motion with no buttons down.
1796 buttons
= 0x03 + 32;
1798 if (mouse
.isAlt()) {
1801 if (mouse
.isCtrl()) {
1804 if (mouse
.isShift()) {
1808 sb
.append((char) (buttons
& 0xFF));
1809 sb
.append((char) (mouse
.getX() + 33));
1810 sb
.append((char) (mouse
.getY() + 33));
1813 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1814 writeRemote(sb
.toString());
1818 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1819 * send to the remote side.
1821 * @param keypress keypress received from the local user
1823 private void keypress(final TKeypress keypress
) {
1824 writeRemote(keypressToString(keypress
));
1828 * Build one of the complex xterm keystroke sequences, storing the result in
1829 * xterm_keystroke_buffer.
1831 * @param ss3 the prefix to use based on VT100 state.
1832 * @param first the first character, usually a number.
1833 * @param first the last character, one of the following: ~ A B C D F H
1834 * @param ctrl whether or not ctrl is down
1835 * @param alt whether or not alt is down
1836 * @param shift whether or not shift is down
1837 * @return the buffer with the full key sequence
1839 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1840 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1842 StringBuilder sb
= new StringBuilder(ss3
);
1843 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1847 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1849 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1851 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1853 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1855 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1857 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1859 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1864 return sb
.toString();
1868 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1870 * @param keypress keypress received from the local user
1871 * @return string to transmit to the remote side
1873 @SuppressWarnings("fallthrough")
1874 private String
keypressToString(final TKeypress keypress
) {
1876 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1878 * If this is a control character, process it like it came from
1881 if (keypress
.getChar() < 0x20) {
1882 handleControlChar((char) keypress
.getChar());
1884 // Local echo for everything else
1885 printCharacter(keypress
.getChar());
1887 if (displayListener
!= null) {
1888 displayListener
.displayChanged();
1892 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1897 // Handle control characters
1898 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1899 StringBuilder sb
= new StringBuilder();
1900 int ch
= keypress
.getChar();
1902 sb
.append(Character
.toChars(ch
));
1903 return sb
.toString();
1906 // Handle alt characters
1907 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1908 StringBuilder sb
= new StringBuilder("\033");
1909 int ch
= keypress
.getChar();
1910 sb
.append(Character
.toChars(ch
));
1911 return sb
.toString();
1914 if (keypress
.equals(kbBackspaceDel
)) {
1927 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1930 switch (arrowKeyMode
) {
1932 return xtermBuildKeySequence("\033[", '1', 'D',
1933 keypress
.isCtrl(), keypress
.isAlt(),
1934 keypress
.isShift());
1936 return xtermBuildKeySequence("\033", '1', 'D',
1937 keypress
.isCtrl(), keypress
.isAlt(),
1938 keypress
.isShift());
1940 return xtermBuildKeySequence("\033O", '1', 'D',
1941 keypress
.isCtrl(), keypress
.isAlt(),
1942 keypress
.isShift());
1945 switch (arrowKeyMode
) {
1956 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1959 switch (arrowKeyMode
) {
1961 return xtermBuildKeySequence("\033[", '1', 'C',
1962 keypress
.isCtrl(), keypress
.isAlt(),
1963 keypress
.isShift());
1965 return xtermBuildKeySequence("\033", '1', 'C',
1966 keypress
.isCtrl(), keypress
.isAlt(),
1967 keypress
.isShift());
1969 return xtermBuildKeySequence("\033O", '1', 'C',
1970 keypress
.isCtrl(), keypress
.isAlt(),
1971 keypress
.isShift());
1974 switch (arrowKeyMode
) {
1985 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1988 switch (arrowKeyMode
) {
1990 return xtermBuildKeySequence("\033[", '1', 'A',
1991 keypress
.isCtrl(), keypress
.isAlt(),
1992 keypress
.isShift());
1994 return xtermBuildKeySequence("\033", '1', 'A',
1995 keypress
.isCtrl(), keypress
.isAlt(),
1996 keypress
.isShift());
1998 return xtermBuildKeySequence("\033O", '1', 'A',
1999 keypress
.isCtrl(), keypress
.isAlt(),
2000 keypress
.isShift());
2003 switch (arrowKeyMode
) {
2014 if (keypress
.equalsWithoutModifiers(kbDown
)) {
2017 switch (arrowKeyMode
) {
2019 return xtermBuildKeySequence("\033[", '1', 'B',
2020 keypress
.isCtrl(), keypress
.isAlt(),
2021 keypress
.isShift());
2023 return xtermBuildKeySequence("\033", '1', 'B',
2024 keypress
.isCtrl(), keypress
.isAlt(),
2025 keypress
.isShift());
2027 return xtermBuildKeySequence("\033O", '1', 'B',
2028 keypress
.isCtrl(), keypress
.isAlt(),
2029 keypress
.isShift());
2032 switch (arrowKeyMode
) {
2043 if (keypress
.equalsWithoutModifiers(kbHome
)) {
2046 switch (arrowKeyMode
) {
2048 return xtermBuildKeySequence("\033[", '1', 'H',
2049 keypress
.isCtrl(), keypress
.isAlt(),
2050 keypress
.isShift());
2052 return xtermBuildKeySequence("\033", '1', 'H',
2053 keypress
.isCtrl(), keypress
.isAlt(),
2054 keypress
.isShift());
2056 return xtermBuildKeySequence("\033O", '1', 'H',
2057 keypress
.isCtrl(), keypress
.isAlt(),
2058 keypress
.isShift());
2061 switch (arrowKeyMode
) {
2072 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
2075 switch (arrowKeyMode
) {
2077 return xtermBuildKeySequence("\033[", '1', 'F',
2078 keypress
.isCtrl(), keypress
.isAlt(),
2079 keypress
.isShift());
2081 return xtermBuildKeySequence("\033", '1', 'F',
2082 keypress
.isCtrl(), keypress
.isAlt(),
2083 keypress
.isShift());
2085 return xtermBuildKeySequence("\033O", '1', 'F',
2086 keypress
.isCtrl(), keypress
.isAlt(),
2087 keypress
.isShift());
2090 switch (arrowKeyMode
) {
2101 if (keypress
.equals(kbF1
)) {
2109 if (keypress
.equals(kbF2
)) {
2117 if (keypress
.equals(kbF3
)) {
2125 if (keypress
.equals(kbF4
)) {
2133 if (keypress
.equals(kbF5
)) {
2146 if (keypress
.equals(kbF6
)) {
2159 if (keypress
.equals(kbF7
)) {
2172 if (keypress
.equals(kbF8
)) {
2185 if (keypress
.equals(kbF9
)) {
2198 if (keypress
.equals(kbF10
)) {
2211 if (keypress
.equals(kbF11
)) {
2215 if (keypress
.equals(kbF12
)) {
2219 if (keypress
.equals(kbShiftF1
)) {
2224 if (type
== DeviceType
.XTERM
) {
2230 if (keypress
.equals(kbShiftF2
)) {
2235 if (type
== DeviceType
.XTERM
) {
2241 if (keypress
.equals(kbShiftF3
)) {
2246 if (type
== DeviceType
.XTERM
) {
2252 if (keypress
.equals(kbShiftF4
)) {
2257 if (type
== DeviceType
.XTERM
) {
2263 if (keypress
.equals(kbShiftF5
)) {
2265 return "\033[15;2~";
2268 if (keypress
.equals(kbShiftF6
)) {
2270 return "\033[17;2~";
2273 if (keypress
.equals(kbShiftF7
)) {
2275 return "\033[18;2~";
2278 if (keypress
.equals(kbShiftF8
)) {
2280 return "\033[19;2~";
2283 if (keypress
.equals(kbShiftF9
)) {
2285 return "\033[20;2~";
2288 if (keypress
.equals(kbShiftF10
)) {
2290 return "\033[21;2~";
2293 if (keypress
.equals(kbShiftF11
)) {
2295 return "\033[23;2~";
2298 if (keypress
.equals(kbShiftF12
)) {
2300 return "\033[24;2~";
2303 if (keypress
.equals(kbCtrlF1
)) {
2308 if (type
== DeviceType
.XTERM
) {
2314 if (keypress
.equals(kbCtrlF2
)) {
2319 if (type
== DeviceType
.XTERM
) {
2325 if (keypress
.equals(kbCtrlF3
)) {
2330 if (type
== DeviceType
.XTERM
) {
2336 if (keypress
.equals(kbCtrlF4
)) {
2341 if (type
== DeviceType
.XTERM
) {
2347 if (keypress
.equals(kbCtrlF5
)) {
2349 return "\033[15;5~";
2352 if (keypress
.equals(kbCtrlF6
)) {
2354 return "\033[17;5~";
2357 if (keypress
.equals(kbCtrlF7
)) {
2359 return "\033[18;5~";
2362 if (keypress
.equals(kbCtrlF8
)) {
2364 return "\033[19;5~";
2367 if (keypress
.equals(kbCtrlF9
)) {
2369 return "\033[20;5~";
2372 if (keypress
.equals(kbCtrlF10
)) {
2374 return "\033[21;5~";
2377 if (keypress
.equals(kbCtrlF11
)) {
2379 return "\033[23;5~";
2382 if (keypress
.equals(kbCtrlF12
)) {
2384 return "\033[24;5~";
2387 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2390 return xtermBuildKeySequence("\033[", '5', '~',
2391 keypress
.isCtrl(), keypress
.isAlt(),
2392 keypress
.isShift());
2398 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2401 return xtermBuildKeySequence("\033[", '6', '~',
2402 keypress
.isCtrl(), keypress
.isAlt(),
2403 keypress
.isShift());
2409 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2412 return xtermBuildKeySequence("\033[", '2', '~',
2413 keypress
.isCtrl(), keypress
.isAlt(),
2414 keypress
.isShift());
2420 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2423 return xtermBuildKeySequence("\033[", '3', '~',
2424 keypress
.isCtrl(), keypress
.isAlt(),
2425 keypress
.isShift());
2427 // Delete sends real delete for VTxxx
2432 if (keypress
.equals(kbEnter
)) {
2436 if (keypress
.equals(kbEsc
)) {
2440 if (keypress
.equals(kbAltEsc
)) {
2444 if (keypress
.equals(kbTab
)) {
2448 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2449 (keypress
.equals(kbShiftTab
))
2459 // Non-alt, non-ctrl characters
2460 if (!keypress
.isFnKey()) {
2461 StringBuilder sb
= new StringBuilder();
2462 sb
.append(Character
.toChars(keypress
.getChar()));
2463 return sb
.toString();
2469 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2472 * @param ch 8-bit character from the remote side
2473 * @param charsetGl character set defined for GL
2474 * @param charsetGr character set defined for GR
2475 * @return character to display on the screen
2477 private char mapCharacterCharset(final int ch
,
2478 final CharacterSet charsetGl
,
2479 final CharacterSet charsetGr
) {
2481 int lookupChar
= ch
;
2482 CharacterSet lookupCharset
= charsetGl
;
2485 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2486 lookupCharset
= charsetGr
;
2490 switch (lookupCharset
) {
2493 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2496 return DECCharacterSets
.UK
[lookupChar
];
2499 return DECCharacterSets
.US_ASCII
[lookupChar
];
2502 return DECCharacterSets
.NL
[lookupChar
];
2505 return DECCharacterSets
.FI
[lookupChar
];
2508 return DECCharacterSets
.FR
[lookupChar
];
2511 return DECCharacterSets
.FR_CA
[lookupChar
];
2514 return DECCharacterSets
.DE
[lookupChar
];
2517 return DECCharacterSets
.IT
[lookupChar
];
2520 return DECCharacterSets
.NO
[lookupChar
];
2523 return DECCharacterSets
.ES
[lookupChar
];
2526 return DECCharacterSets
.SV
[lookupChar
];
2529 return DECCharacterSets
.SWISS
[lookupChar
];
2531 case DEC_SUPPLEMENTAL
:
2532 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2535 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2538 return DECCharacterSets
.US_ASCII
[lookupChar
];
2541 return DECCharacterSets
.US_ASCII
[lookupChar
];
2544 throw new IllegalArgumentException("Invalid character set value: "
2550 * Map an 8-bit byte into a printable character.
2552 * @param ch either 8-bit or Unicode character from the remote side
2553 * @return character to display on the screen
2555 private int mapCharacter(final int ch
) {
2557 // Unicode character, just return it
2561 CharacterSet charsetGl
= currentState
.g0Charset
;
2562 CharacterSet charsetGr
= currentState
.grCharset
;
2564 if (vt52Mode
== true) {
2565 if (shiftOut
== true) {
2566 // Shifted out character, pull from VT52 graphics
2567 charsetGl
= currentState
.g1Charset
;
2568 charsetGr
= CharacterSet
.US
;
2571 charsetGl
= currentState
.g0Charset
;
2572 charsetGr
= CharacterSet
.US
;
2575 // Pull the character
2576 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2580 if (shiftOut
== true) {
2581 // Shifted out character, pull from G1
2582 charsetGl
= currentState
.g1Charset
;
2583 charsetGr
= currentState
.grCharset
;
2585 // Pull the character
2586 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2590 if (singleshift
== Singleshift
.SS2
) {
2592 singleshift
= Singleshift
.NONE
;
2594 // Shifted out character, pull from G2
2595 charsetGl
= currentState
.g2Charset
;
2596 charsetGr
= currentState
.grCharset
;
2600 if (singleshift
== Singleshift
.SS3
) {
2602 singleshift
= Singleshift
.NONE
;
2604 // Shifted out character, pull from G3
2605 charsetGl
= currentState
.g3Charset
;
2606 charsetGr
= currentState
.grCharset
;
2609 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2610 // Check for locking shift
2612 switch (currentState
.glLockshift
) {
2615 throw new IllegalArgumentException("programming bug");
2618 throw new IllegalArgumentException("programming bug");
2621 throw new IllegalArgumentException("programming bug");
2625 charsetGl
= currentState
.g2Charset
;
2630 charsetGl
= currentState
.g3Charset
;
2635 charsetGl
= currentState
.g0Charset
;
2639 switch (currentState
.grLockshift
) {
2642 throw new IllegalArgumentException("programming bug");
2645 throw new IllegalArgumentException("programming bug");
2649 charsetGr
= currentState
.g1Charset
;
2654 charsetGr
= currentState
.g2Charset
;
2659 charsetGr
= currentState
.g3Charset
;
2664 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2671 // Pull the character
2672 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2676 * Scroll the text within a scrolling region up n lines.
2678 * @param regionTop top row of the scrolling region
2679 * @param regionBottom bottom row of the scrolling region
2680 * @param n number of lines to scroll
2682 private void scrollingRegionScrollUp(final int regionTop
,
2683 final int regionBottom
, final int n
) {
2685 if (regionTop
>= regionBottom
) {
2689 // Sanity check: see if there will be any characters left after the
2691 if (regionBottom
+ 1 - regionTop
<= n
) {
2692 // There won't be anything left in the region, so just call
2693 // eraseScreen() and return.
2694 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2698 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2699 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2700 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2702 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2703 - remaining
, regionBottom
+ 1);
2704 display
= new ArrayList
<DisplayLine
>(displayTop
);
2705 display
.addAll(displayMiddle
);
2706 for (int i
= 0; i
< n
; i
++) {
2707 DisplayLine line
= new DisplayLine(currentState
.attr
);
2708 line
.setReverseColor(reverseVideo
);
2711 display
.addAll(displayBottom
);
2713 assert (display
.size() == height
);
2717 * Scroll the text within a scrolling region down n lines.
2719 * @param regionTop top row of the scrolling region
2720 * @param regionBottom bottom row of the scrolling region
2721 * @param n number of lines to scroll
2723 private void scrollingRegionScrollDown(final int regionTop
,
2724 final int regionBottom
, final int n
) {
2726 if (regionTop
>= regionBottom
) {
2730 // Sanity check: see if there will be any characters left after the
2732 if (regionBottom
+ 1 - regionTop
<= n
) {
2733 // There won't be anything left in the region, so just call
2734 // eraseScreen() and return.
2735 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2739 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2740 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2741 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2743 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2744 regionTop
+ remaining
);
2745 display
= new ArrayList
<DisplayLine
>(displayTop
);
2746 for (int i
= 0; i
< n
; i
++) {
2747 DisplayLine line
= new DisplayLine(currentState
.attr
);
2748 line
.setReverseColor(reverseVideo
);
2751 display
.addAll(displayMiddle
);
2752 display
.addAll(displayBottom
);
2754 assert (display
.size() == height
);
2758 * Process a control character.
2760 * @param ch 8-bit character from the remote side
2762 private void handleControlChar(final char ch
) {
2763 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2774 // Transmit the answerback message.
2785 cursorLeft(1, false);
2790 advanceToNextTabStop();
2816 currentState
.glLockshift
= LockshiftMode
.NONE
;
2822 currentState
.glLockshift
= LockshiftMode
.NONE
;
2847 singleshift
= Singleshift
.SS2
;
2852 singleshift
= Singleshift
.SS3
;
2862 * Advance the cursor to the next tab stop.
2864 private void advanceToNextTabStop() {
2865 if (tabStops
.size() == 0) {
2866 // Go to the rightmost column
2867 cursorRight(rightMargin
- currentState
.cursorX
, false);
2870 for (Integer stop
: tabStops
) {
2871 if (stop
> currentState
.cursorX
) {
2872 cursorRight(stop
- currentState
.cursorX
, false);
2877 * We got here, meaning there isn't a tab stop beyond the current
2878 * cursor position. Place the cursor of the right-most edge of the
2881 cursorRight(rightMargin
- currentState
.cursorX
, false);
2885 * Save a character into the collect buffer.
2887 * @param ch character to save
2889 private void collect(final char ch
) {
2890 collectBuffer
.append(ch
);
2894 * Save a byte into the CSI parameters buffer.
2896 * @param ch byte to save
2898 private void param(final byte ch
) {
2899 if (csiParams
.size() == 0) {
2900 csiParams
.add(Integer
.valueOf(0));
2902 Integer x
= csiParams
.get(csiParams
.size() - 1);
2903 if ((ch
>= '0') && (ch
<= '9')) {
2906 csiParams
.set(csiParams
.size() - 1, x
);
2909 if ((ch
== ';') && (csiParams
.size() < 16)) {
2910 csiParams
.add(Integer
.valueOf(0));
2915 * Get a CSI parameter value, with a default.
2917 * @param position parameter index. 0 is the first parameter.
2918 * @param defaultValue value to use if csiParams[position] doesn't exist
2919 * @return parameter value
2921 private int getCsiParam(final int position
, final int defaultValue
) {
2922 if (csiParams
.size() < position
+ 1) {
2923 return defaultValue
;
2925 return csiParams
.get(position
).intValue();
2929 * Get a CSI parameter value, clamped to within min/max.
2931 * @param position parameter index. 0 is the first parameter.
2932 * @param defaultValue value to use if csiParams[position] doesn't exist
2933 * @param minValue minimum value inclusive
2934 * @param maxValue maximum value inclusive
2935 * @return parameter value
2937 private int getCsiParam(final int position
, final int defaultValue
,
2938 final int minValue
, final int maxValue
) {
2940 assert (minValue
<= maxValue
);
2941 int value
= getCsiParam(position
, defaultValue
);
2942 if (value
< minValue
) {
2945 if (value
> maxValue
) {
2952 * Set or unset a toggle.
2954 * @param value true for set ('h'), false for reset ('l')
2956 private void setToggle(final boolean value
) {
2957 boolean decPrivateModeFlag
= false;
2959 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2960 if (collectBuffer
.charAt(i
) == '?') {
2961 decPrivateModeFlag
= true;
2966 for (Integer i
: csiParams
) {
2971 if (decPrivateModeFlag
== true) {
2973 if (value
== true) {
2974 // Use application arrow keys
2975 arrowKeyMode
= ArrowKeyMode
.VT100
;
2977 // Use ANSI arrow keys
2978 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2983 if (decPrivateModeFlag
== true) {
2984 if (value
== false) {
2988 arrowKeyMode
= ArrowKeyMode
.VT52
;
2991 * From the VT102 docs: "You use ANSI mode to select
2992 * most terminal features; the terminal uses the same
2993 * features when it switches to VT52 mode. You
2994 * cannot, however, change most of these features in
2997 * In other words, do not reset any other attributes
2998 * when switching between VT52 submode and ANSI.
3000 * HOWEVER, the real vt100 does switch the character
3001 * set according to Usenet.
3003 currentState
.g0Charset
= CharacterSet
.US
;
3004 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3007 if ((type
== DeviceType
.VT220
)
3008 || (type
== DeviceType
.XTERM
)) {
3010 // VT52 mode is explicitly 7-bit
3012 singleshift
= Singleshift
.NONE
;
3017 if (value
== true) {
3018 // Turn off keyboard
3027 if (decPrivateModeFlag
== true) {
3029 if (value
== true) {
3036 if ((displayListener
!= null)
3037 && (type
== DeviceType
.XTERM
)
3039 // For xterms, reset to the actual width, not 80
3041 width
= displayListener
.getDisplayWidth();
3042 rightMargin
= width
- 1;
3045 width
= rightMargin
+ 1;
3048 // Entire screen is cleared, and scrolling region is
3050 eraseScreen(0, 0, height
- 1, width
- 1, false);
3051 scrollRegionTop
= 0;
3052 scrollRegionBottom
= height
- 1;
3053 // Also home the cursor
3054 cursorPosition(0, 0);
3058 if (decPrivateModeFlag
== true) {
3060 if (value
== true) {
3069 if (value
== true) {
3077 if (decPrivateModeFlag
== true) {
3079 if (value
== true) {
3081 * Set selects reverse screen, a white screen
3082 * background with black characters.
3084 if (reverseVideo
!= true) {
3086 * If in normal video, switch it back
3088 invertDisplayColors();
3090 reverseVideo
= true;
3093 * Reset selects normal screen, a black screen
3094 * background with white characters.
3096 if (reverseVideo
== true) {
3098 * If in reverse video already, switch it back
3100 invertDisplayColors();
3102 reverseVideo
= false;
3107 if (decPrivateModeFlag
== true) {
3109 if (value
== true) {
3110 // Origin is relative to scroll region cursor.
3111 // Cursor can NEVER leave scrolling region.
3112 currentState
.originMode
= true;
3113 cursorPosition(0, 0);
3115 // Origin is absolute to entire screen. Cursor can
3116 // leave the scrolling region via cup() and hvp().
3117 currentState
.originMode
= false;
3118 cursorPosition(0, 0);
3123 if (decPrivateModeFlag
== true) {
3125 if (value
== true) {
3127 currentState
.lineWrap
= true;
3129 // Turn linewrap off
3130 currentState
.lineWrap
= false;
3135 if (decPrivateModeFlag
== true) {
3137 if (value
== true) {
3138 // Keyboard auto-repeat on
3141 // Keyboard auto-repeat off
3147 if (decPrivateModeFlag
== false) {
3149 if (value
== true) {
3159 if (decPrivateModeFlag
== true) {
3165 if (decPrivateModeFlag
== true) {
3171 if (decPrivateModeFlag
== false) {
3173 if (value
== true) {
3175 * Set causes a received linefeed, form feed, or
3176 * vertical tab to move cursor to first column of
3177 * next line. RETURN transmits both a carriage return
3178 * and linefeed. This selection is also called new
3184 * Reset causes a received linefeed, form feed, or
3185 * vertical tab to move cursor to next line in
3186 * current column. RETURN transmits a carriage
3189 newLineMode
= false;
3195 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3196 if (decPrivateModeFlag
== true) {
3198 if (value
== true) {
3200 cursorVisible
= true;
3203 cursorVisible
= false;
3210 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3211 if (decPrivateModeFlag
== true) {
3213 if (value
== true) {
3214 // Select national mode NRC
3217 // Select multi-national mode
3226 if (type
== DeviceType
.XTERM
) {
3227 if (decPrivateModeFlag
== true) {
3228 if (value
== true) {
3229 // Enable sixel scrolling (default).
3232 // Disable sixel scrolling.
3241 if ((type
== DeviceType
.XTERM
)
3242 && (decPrivateModeFlag
== true)
3244 // Mouse: normal tracking mode
3245 if (value
== true) {
3246 mouseProtocol
= MouseProtocol
.NORMAL
;
3248 mouseProtocol
= MouseProtocol
.OFF
;
3254 if ((type
== DeviceType
.XTERM
)
3255 && (decPrivateModeFlag
== true)
3257 // Mouse: normal tracking mode
3258 if (value
== true) {
3259 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
3261 mouseProtocol
= MouseProtocol
.OFF
;
3267 if ((type
== DeviceType
.XTERM
)
3268 && (decPrivateModeFlag
== true)
3270 // Mouse: Any-event tracking mode
3271 if (value
== true) {
3272 mouseProtocol
= MouseProtocol
.ANYEVENT
;
3274 mouseProtocol
= MouseProtocol
.OFF
;
3280 if ((type
== DeviceType
.XTERM
)
3281 && (decPrivateModeFlag
== true)
3283 // Mouse: UTF-8 coordinates
3284 if (value
== true) {
3285 mouseEncoding
= MouseEncoding
.UTF8
;
3287 mouseEncoding
= MouseEncoding
.X10
;
3293 if ((type
== DeviceType
.XTERM
)
3294 && (decPrivateModeFlag
== true)
3296 // Mouse: SGR coordinates
3297 if (value
== true) {
3298 mouseEncoding
= MouseEncoding
.SGR
;
3300 mouseEncoding
= MouseEncoding
.X10
;
3306 if (type
== DeviceType
.XTERM
) {
3307 if (decPrivateModeFlag
== true) {
3308 if (value
== true) {
3309 // Use private color registers for each sixel
3310 // graphic (default).
3311 sixelPalette
= null;
3313 // Use shared color registers for each sixel
3315 sixelPalette
= new HashMap
<Integer
, java
.awt
.Color
>();
3329 * DECSC - Save cursor.
3331 private void decsc() {
3332 savedState
.setTo(currentState
);
3336 * DECRC - Restore cursor.
3338 private void decrc() {
3339 currentState
.setTo(savedState
);
3345 private void ind() {
3346 // Move the cursor and scroll if necessary. If at the bottom line
3347 // already, a scroll up is supposed to be performed.
3348 if (currentState
.cursorY
== scrollRegionBottom
) {
3349 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3351 cursorDown(1, true);
3355 * RI - Reverse index.
3358 // Move the cursor and scroll if necessary. If at the top line
3359 // already, a scroll down is supposed to be performed.
3360 if (currentState
.cursorY
== scrollRegionTop
) {
3361 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3369 private void nel() {
3370 // Move the cursor and scroll if necessary. If at the bottom line
3371 // already, a scroll up is supposed to be performed.
3372 if (currentState
.cursorY
== scrollRegionBottom
) {
3373 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3375 cursorDown(1, true);
3377 // Reset to the beginning of the next line
3378 currentState
.cursorX
= 0;
3382 * DECKPAM - Keypad application mode.
3384 private void deckpam() {
3385 keypadMode
= KeypadMode
.Application
;
3389 * DECKPNM - Keypad numeric mode.
3391 private void deckpnm() {
3392 keypadMode
= KeypadMode
.Numeric
;
3398 * @param n number of spaces to move
3399 * @param honorScrollRegion if true, then do nothing if the cursor is
3400 * outside the scrolling region
3402 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3406 * Special case: if a user moves the cursor from the right margin, we
3407 * have to reset the VT100 right margin flag.
3410 wrapLineFlag
= false;
3413 for (int i
= 0; i
< n
; i
++) {
3414 if (honorScrollRegion
== true) {
3415 // Honor the scrolling region
3416 if ((currentState
.cursorY
< scrollRegionTop
)
3417 || (currentState
.cursorY
> scrollRegionBottom
)
3419 // Outside region, do nothing
3422 // Inside region, go up
3423 top
= scrollRegionTop
;
3425 // Non-scrolling case
3429 if (currentState
.cursorY
> top
) {
3430 currentState
.cursorY
--;
3436 * Move down n spaces.
3438 * @param n number of spaces to move
3439 * @param honorScrollRegion if true, then do nothing if the cursor is
3440 * outside the scrolling region
3442 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3446 * Special case: if a user moves the cursor from the right margin, we
3447 * have to reset the VT100 right margin flag.
3450 wrapLineFlag
= false;
3453 for (int i
= 0; i
< n
; i
++) {
3455 if (honorScrollRegion
== true) {
3456 // Honor the scrolling region
3457 if (currentState
.cursorY
> scrollRegionBottom
) {
3458 // Outside region, do nothing
3461 // Inside region, go down
3462 bottom
= scrollRegionBottom
;
3464 // Non-scrolling case
3465 bottom
= height
- 1;
3468 if (currentState
.cursorY
< bottom
) {
3469 currentState
.cursorY
++;
3475 * Move left 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 cursorLeft(final int n
, final boolean honorScrollRegion
) {
3483 * Special case: if a user moves the cursor from the right margin, we
3484 * have to reset the VT100 right margin flag.
3487 wrapLineFlag
= false;
3490 for (int i
= 0; i
< n
; i
++) {
3491 if (honorScrollRegion
== true) {
3492 // Honor the scrolling region
3493 if ((currentState
.cursorY
< scrollRegionTop
)
3494 || (currentState
.cursorY
> scrollRegionBottom
)
3496 // Outside region, do nothing
3501 if (currentState
.cursorX
> 0) {
3502 currentState
.cursorX
--;
3508 * Move right n spaces.
3510 * @param n number of spaces to move
3511 * @param honorScrollRegion if true, then do nothing if the cursor is
3512 * outside the scrolling region
3514 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3515 int rightMargin
= this.rightMargin
;
3518 * Special case: if a user moves the cursor from the right margin, we
3519 * have to reset the VT100 right margin flag.
3522 wrapLineFlag
= false;
3525 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3526 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3529 for (int i
= 0; i
< n
; i
++) {
3530 if (honorScrollRegion
== true) {
3531 // Honor the scrolling region
3532 if ((currentState
.cursorY
< scrollRegionTop
)
3533 || (currentState
.cursorY
> scrollRegionBottom
)
3535 // Outside region, do nothing
3540 if (currentState
.cursorX
< rightMargin
) {
3541 currentState
.cursorX
++;
3547 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3549 * @param row row to move to
3550 * @param col column to move to
3552 private void cursorPosition(int row
, final int col
) {
3553 int rightMargin
= this.rightMargin
;
3558 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3559 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3562 // Set column number
3563 currentState
.cursorX
= col
;
3565 // Sanity check, bring column back to margin.
3566 if (currentState
.cursorX
> rightMargin
) {
3567 currentState
.cursorX
= rightMargin
;
3571 if (currentState
.originMode
== true) {
3572 row
+= scrollRegionTop
;
3574 if (currentState
.cursorY
< row
) {
3575 cursorDown(row
- currentState
.cursorY
, false);
3576 } else if (currentState
.cursorY
> row
) {
3577 cursorUp(currentState
.cursorY
- row
, false);
3580 wrapLineFlag
= false;
3584 * HTS - Horizontal tabulation set.
3586 private void hts() {
3587 for (Integer stop
: tabStops
) {
3588 if (stop
== currentState
.cursorX
) {
3589 // Already have a tab stop here
3594 // Append a tab stop to the end of the array and resort them
3595 tabStops
.add(currentState
.cursorX
);
3596 Collections
.sort(tabStops
);
3600 * DECSWL - Single-width line.
3602 private void decswl() {
3603 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3604 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3608 * DECDWL - Double-width line.
3610 private void decdwl() {
3611 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3612 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3616 * DECHDL - Double-height + double-width line.
3618 * @param topHalf if true, this sets the row to be the top half row of a
3621 private void dechdl(final boolean topHalf
) {
3622 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3623 if (topHalf
== true) {
3624 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3626 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3631 * DECALN - Screen alignment display.
3633 private void decaln() {
3634 Cell newCell
= new Cell('E');
3635 for (DisplayLine line
: display
) {
3636 for (int i
= 0; i
< line
.length(); i
++) {
3637 line
.replace(i
, newCell
);
3643 * DECSCL - Compatibility level.
3645 private void decscl() {
3646 int i
= getCsiParam(0, 0);
3647 int j
= getCsiParam(1, 0);
3651 currentState
.g0Charset
= CharacterSet
.US
;
3652 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3654 } else if (i
== 62) {
3656 if ((j
== 0) || (j
== 2)) {
3658 } else if (j
== 1) {
3665 * CUD - Cursor down.
3667 private void cud() {
3668 cursorDown(getCsiParam(0, 1, 1, height
), true);
3672 * CUF - Cursor forward.
3674 private void cuf() {
3675 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3679 * CUB - Cursor backward.
3681 private void cub() {
3682 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3688 private void cuu() {
3689 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3693 * CUP - Cursor position.
3695 private void cup() {
3696 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3697 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3701 * CNL - Cursor down and to column 1.
3703 private void cnl() {
3704 cursorDown(getCsiParam(0, 1, 1, height
), true);
3706 cursorLeft(currentState
.cursorX
, true);
3710 * CPL - Cursor up and to column 1.
3712 private void cpl() {
3713 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3715 cursorLeft(currentState
.cursorX
, true);
3719 * CHA - Cursor to column # in current row.
3721 private void cha() {
3722 cursorPosition(currentState
.cursorY
,
3723 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3727 * VPA - Cursor to row #, same column.
3729 private void vpa() {
3730 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3731 currentState
.cursorX
);
3735 * ED - Erase in display.
3738 boolean honorProtected
= false;
3739 boolean decPrivateModeFlag
= false;
3741 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3742 if (collectBuffer
.charAt(i
) == '?') {
3743 decPrivateModeFlag
= true;
3748 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3749 && (decPrivateModeFlag
== true)
3751 honorProtected
= true;
3754 int i
= getCsiParam(0, 0);
3757 // Erase from here to end of screen
3758 if (currentState
.cursorY
< height
- 1) {
3759 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3762 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3763 } else if (i
== 1) {
3764 // Erase from beginning of screen to here
3765 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3767 eraseLine(0, currentState
.cursorX
, honorProtected
);
3768 } else if (i
== 2) {
3769 // Erase entire screen
3770 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3775 * EL - Erase in line.
3778 boolean honorProtected
= false;
3779 boolean decPrivateModeFlag
= false;
3781 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3782 if (collectBuffer
.charAt(i
) == '?') {
3783 decPrivateModeFlag
= true;
3788 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3789 && (decPrivateModeFlag
== true)
3791 honorProtected
= true;
3794 int i
= getCsiParam(0, 0);
3797 // Erase from here to end of line
3798 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3799 } else if (i
== 1) {
3800 // Erase from beginning of line to here
3801 eraseLine(0, currentState
.cursorX
, honorProtected
);
3802 } else if (i
== 2) {
3803 // Erase entire line
3804 eraseLine(0, width
- 1, honorProtected
);
3809 * ECH - Erase # of characters in current row.
3811 private void ech() {
3812 int i
= getCsiParam(0, 1, 1, width
);
3814 // Erase from here to i characters
3815 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3822 int i
= getCsiParam(0, 1);
3824 if ((currentState
.cursorY
>= scrollRegionTop
)
3825 && (currentState
.cursorY
<= scrollRegionBottom
)
3828 // I can get the same effect with a scroll-down
3829 scrollingRegionScrollDown(currentState
.cursorY
,
3830 scrollRegionBottom
, i
);
3835 * DCH - Delete char.
3837 private void dch() {
3838 int n
= getCsiParam(0, 1);
3839 DisplayLine line
= display
.get(currentState
.cursorY
);
3840 Cell blank
= new Cell();
3841 for (int i
= 0; i
< n
; i
++) {
3842 line
.delete(currentState
.cursorX
, blank
);
3847 * ICH - Insert blank char at cursor.
3849 private void ich() {
3850 int n
= getCsiParam(0, 1);
3851 DisplayLine line
= display
.get(currentState
.cursorY
);
3852 Cell blank
= new Cell();
3853 for (int i
= 0; i
< n
; i
++) {
3854 line
.insert(currentState
.cursorX
, blank
);
3862 int i
= getCsiParam(0, 1);
3864 if ((currentState
.cursorY
>= scrollRegionTop
)
3865 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3867 // I can get the same effect with a scroll-down
3868 scrollingRegionScrollUp(currentState
.cursorY
,
3869 scrollRegionBottom
, i
);
3874 * HVP - Horizontal and vertical position.
3876 private void hvp() {
3881 * REP - Repeat character.
3883 private void rep() {
3884 int n
= getCsiParam(0, 1);
3885 for (int i
= 0; i
< n
; i
++) {
3886 printCharacter(repCh
);
3894 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3895 getCsiParam(0, 1, 1, height
));
3902 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3903 getCsiParam(0, 1, 1, height
));
3907 * CBT - Go back X tab stops.
3909 private void cbt() {
3910 int tabsToMove
= getCsiParam(0, 1);
3913 for (int i
= 0; i
< tabsToMove
; i
++) {
3914 int j
= currentState
.cursorX
;
3915 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3916 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3924 j
= tabStops
.get(tabI
);
3926 cursorPosition(currentState
.cursorY
, j
);
3931 * CHT - Advance X tab stops.
3933 private void cht() {
3934 int n
= getCsiParam(0, 1);
3935 for (int i
= 0; i
< n
; i
++) {
3936 advanceToNextTabStop();
3941 * SGR - Select graphics rendition.
3943 private void sgr() {
3945 if (csiParams
.size() == 0) {
3946 currentState
.attr
.reset();
3950 int sgrColorMode
= -1;
3951 boolean idx88Color
= false;
3952 boolean rgbColor
= false;
3956 for (Integer i
: csiParams
) {
3958 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
3960 assert (type
== DeviceType
.XTERM
);
3964 * Indexed color mode, we now have the index number.
3966 if (sgrColorMode
== 38) {
3967 currentState
.attr
.setForeColorRGB(get88Color(i
));
3969 assert (sgrColorMode
== 48);
3970 currentState
.attr
.setBackColorRGB(get88Color(i
));
3979 * RGB color mode, we are collecting tokens.
3983 } else if (rgbGreen
== -1) {
3984 rgbGreen
= i
& 0xFF;
3986 int rgb
= rgbRed
<< 16;
3987 rgb
|= rgbGreen
<< 8;
3990 // System.err.printf("RGB: %08x\n", rgb);
3992 if (sgrColorMode
== 38) {
3993 currentState
.attr
.setForeColorRGB(rgb
);
3995 assert (sgrColorMode
== 48);
3996 currentState
.attr
.setBackColorRGB(rgb
);
4017 * Indexed color mode.
4024 * This is neither indexed nor RGB color. Bail out.
4029 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
4035 currentState
.attr
.reset();
4040 currentState
.attr
.setBold(true);
4045 currentState
.attr
.setUnderline(true);
4050 currentState
.attr
.setBlink(true);
4055 currentState
.attr
.setReverse(true);
4062 if (type
== DeviceType
.XTERM
) {
4072 // Set black foreground
4073 currentState
.attr
.setForeColorRGB(get88Color(8));
4076 // Set red foreground
4077 currentState
.attr
.setForeColorRGB(get88Color(9));
4080 // Set green foreground
4081 currentState
.attr
.setForeColorRGB(get88Color(10));
4084 // Set yellow foreground
4085 currentState
.attr
.setForeColorRGB(get88Color(11));
4088 // Set blue foreground
4089 currentState
.attr
.setForeColorRGB(get88Color(12));
4092 // Set magenta foreground
4093 currentState
.attr
.setForeColorRGB(get88Color(13));
4096 // Set cyan foreground
4097 currentState
.attr
.setForeColorRGB(get88Color(14));
4100 // Set white foreground
4101 currentState
.attr
.setForeColorRGB(get88Color(15));
4105 // Set black background
4106 currentState
.attr
.setBackColorRGB(get88Color(8));
4109 // Set red background
4110 currentState
.attr
.setBackColorRGB(get88Color(9));
4113 // Set green background
4114 currentState
.attr
.setBackColorRGB(get88Color(10));
4117 // Set yellow background
4118 currentState
.attr
.setBackColorRGB(get88Color(11));
4121 // Set blue background
4122 currentState
.attr
.setBackColorRGB(get88Color(12));
4125 // Set magenta background
4126 currentState
.attr
.setBackColorRGB(get88Color(13));
4129 // Set cyan background
4130 currentState
.attr
.setBackColorRGB(get88Color(14));
4133 // Set white background
4134 currentState
.attr
.setBackColorRGB(get88Color(15));
4142 if ((type
== DeviceType
.VT220
)
4143 || (type
== DeviceType
.XTERM
)) {
4149 currentState
.attr
.setBold(false);
4154 currentState
.attr
.setUnderline(false);
4159 currentState
.attr
.setBlink(false);
4164 currentState
.attr
.setReverse(false);
4172 // A true VT100/102/220 does not support color, however everyone
4173 // is used to their terminal emulator supporting color so we will
4174 // unconditionally support color for all DeviceType's.
4179 // Set black foreground
4180 currentState
.attr
.setForeColor(Color
.BLACK
);
4181 currentState
.attr
.setForeColorRGB(-1);
4184 // Set red foreground
4185 currentState
.attr
.setForeColor(Color
.RED
);
4186 currentState
.attr
.setForeColorRGB(-1);
4189 // Set green foreground
4190 currentState
.attr
.setForeColor(Color
.GREEN
);
4191 currentState
.attr
.setForeColorRGB(-1);
4194 // Set yellow foreground
4195 currentState
.attr
.setForeColor(Color
.YELLOW
);
4196 currentState
.attr
.setForeColorRGB(-1);
4199 // Set blue foreground
4200 currentState
.attr
.setForeColor(Color
.BLUE
);
4201 currentState
.attr
.setForeColorRGB(-1);
4204 // Set magenta foreground
4205 currentState
.attr
.setForeColor(Color
.MAGENTA
);
4206 currentState
.attr
.setForeColorRGB(-1);
4209 // Set cyan foreground
4210 currentState
.attr
.setForeColor(Color
.CYAN
);
4211 currentState
.attr
.setForeColorRGB(-1);
4214 // Set white foreground
4215 currentState
.attr
.setForeColor(Color
.WHITE
);
4216 currentState
.attr
.setForeColorRGB(-1);
4219 if (type
== DeviceType
.XTERM
) {
4221 * Xterm supports T.416 / ISO-8613-3 codes to select
4222 * either an indexed color or an RGB value. (It also
4223 * permits these ISO-8613-3 SGR sequences to be separated
4224 * by colons rather than semicolons.)
4226 * We will support only the following:
4228 * 1. Indexed color mode (88- or 256-color modes).
4232 * These cover most of the use cases in the real world.
4234 * HOWEVER, note that this is an awful broken "standard",
4235 * with no way to do it "right". See
4236 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4237 * for a detailed discussion of the current state of RGB
4238 * in various terminals, the point of which is that none
4239 * of them really do the same thing despite all appearing
4243 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4244 * where it is assumed that supporting just the "indexed
4245 * mode" of these sequences (which could align easily
4246 * with existing SGR colors) is assumed to mean full
4247 * support of 24-bit RGB. So it is all or nothing.
4249 * Finally, these sequences break the assumptions of
4250 * standard ECMA-48 style parsers as pointed out at
4251 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4252 * Therefore in order to keep a clean display, we cannot
4253 * parse anything else in this sequence.
4258 // Underscore on, default foreground color
4259 currentState
.attr
.setUnderline(true);
4260 currentState
.attr
.setForeColor(Color
.WHITE
);
4264 // Underscore off, default foreground color
4265 currentState
.attr
.setUnderline(false);
4266 currentState
.attr
.setForeColor(Color
.WHITE
);
4267 currentState
.attr
.setForeColorRGB(-1);
4270 // Set black background
4271 currentState
.attr
.setBackColor(Color
.BLACK
);
4272 currentState
.attr
.setBackColorRGB(-1);
4275 // Set red background
4276 currentState
.attr
.setBackColor(Color
.RED
);
4277 currentState
.attr
.setBackColorRGB(-1);
4280 // Set green background
4281 currentState
.attr
.setBackColor(Color
.GREEN
);
4282 currentState
.attr
.setBackColorRGB(-1);
4285 // Set yellow background
4286 currentState
.attr
.setBackColor(Color
.YELLOW
);
4287 currentState
.attr
.setBackColorRGB(-1);
4290 // Set blue background
4291 currentState
.attr
.setBackColor(Color
.BLUE
);
4292 currentState
.attr
.setBackColorRGB(-1);
4295 // Set magenta background
4296 currentState
.attr
.setBackColor(Color
.MAGENTA
);
4297 currentState
.attr
.setBackColorRGB(-1);
4300 // Set cyan background
4301 currentState
.attr
.setBackColor(Color
.CYAN
);
4302 currentState
.attr
.setBackColorRGB(-1);
4305 // Set white background
4306 currentState
.attr
.setBackColor(Color
.WHITE
);
4307 currentState
.attr
.setBackColorRGB(-1);
4310 if (type
== DeviceType
.XTERM
) {
4312 * Xterm supports T.416 / ISO-8613-3 codes to select
4313 * either an indexed color or an RGB value. (It also
4314 * permits these ISO-8613-3 SGR sequences to be separated
4315 * by colons rather than semicolons.)
4317 * We will support only the following:
4319 * 1. Indexed color mode (88- or 256-color modes).
4323 * These cover most of the use cases in the real world.
4325 * HOWEVER, note that this is an awful broken "standard",
4326 * with no way to do it "right". See
4327 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4328 * for a detailed discussion of the current state of RGB
4329 * in various terminals, the point of which is that none
4330 * of them really do the same thing despite all appearing
4334 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4335 * where it is assumed that supporting just the "indexed
4336 * mode" of these sequences (which could align easily
4337 * with existing SGR colors) is assumed to mean full
4338 * support of 24-bit RGB. So it is all or nothing.
4340 * Finally, these sequences break the assumptions of
4341 * standard ECMA-48 style parsers as pointed out at
4342 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4343 * Therefore in order to keep a clean display, we cannot
4344 * parse anything else in this sequence.
4351 // Default background
4352 currentState
.attr
.setBackColor(Color
.BLACK
);
4353 currentState
.attr
.setBackColorRGB(-1);
4363 * DA - Device attributes.
4366 int extendedFlag
= 0;
4368 if (collectBuffer
.length() > 0) {
4369 String args
= collectBuffer
.substring(1);
4370 if (collectBuffer
.charAt(0) == '>') {
4372 if (collectBuffer
.length() >= 2) {
4373 i
= Integer
.parseInt(args
);
4375 } else if (collectBuffer
.charAt(0) == '=') {
4377 if (collectBuffer
.length() >= 2) {
4378 i
= Integer
.parseInt(args
);
4381 // Unknown code, bail out
4386 if ((i
!= 0) && (i
!= 1)) {
4390 if ((extendedFlag
== 0) && (i
== 0)) {
4391 // Send string directly to remote side
4392 writeRemote(deviceTypeResponse());
4396 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4398 if ((extendedFlag
== 1) && (i
== 0)) {
4400 * Request "What type of terminal are you, what is your
4401 * firmware version, and what hardware options do you have
4404 * Respond: "I am a VT220 (identification code of 1), my
4405 * firmware version is _____ (Pv), and I have _____ Po
4406 * options installed."
4412 if (s8c1t
== true) {
4413 writeRemote("\u009b>1;10;0c");
4415 writeRemote("\033[>1;10;0c");
4421 if ((extendedFlag
== 2) && (i
== 0)) {
4424 * Request "What is your unit ID?"
4426 * Respond: "I was manufactured at site 00 and have a unique ID
4430 writeRemote("\033P!|00010203\033\\");
4435 * DECSTBM - Set top and bottom margins.
4437 private void decstbm() {
4438 boolean decPrivateModeFlag
= false;
4440 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4441 if (collectBuffer
.charAt(i
) == '?') {
4442 decPrivateModeFlag
= true;
4446 if (decPrivateModeFlag
) {
4447 // This could be restore DEC private mode values.
4451 int top
= getCsiParam(0, 1, 1, height
) - 1;
4452 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4453 if (bottom
> height
- 1) {
4454 bottom
= height
- 1;
4460 scrollRegionTop
= top
;
4461 scrollRegionBottom
= bottom
;
4464 cursorPosition(0, 0);
4469 * DECREQTPARM - Request terminal parameters.
4471 private void decreqtparm() {
4472 int i
= getCsiParam(0, 0);
4474 if ((i
!= 0) && (i
!= 1)) {
4481 * Request terminal parameters.
4485 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4486 * (CLoCk MULtiplier = 1, STP option flags = 0)
4490 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4493 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4495 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4501 * DECSCA - Select Character Attributes.
4503 private void decsca() {
4504 int i
= getCsiParam(0, 0);
4506 if ((i
== 0) || (i
== 2)) {
4508 currentState
.attr
.setProtect(false);
4512 currentState
.attr
.setProtect(true);
4517 * DECSTR - Soft Terminal Reset.
4519 private void decstr() {
4520 // Do exactly like RIS - Reset to initial state
4522 // Do I clear screen too? I think so...
4523 eraseScreen(0, 0, height
- 1, width
- 1, false);
4524 cursorPosition(0, 0);
4528 * DSR - Device status report.
4530 private void dsr() {
4531 boolean decPrivateModeFlag
= false;
4532 int row
= currentState
.cursorY
;
4534 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4535 if (collectBuffer
.charAt(i
) == '?') {
4536 decPrivateModeFlag
= true;
4541 int i
= getCsiParam(0, 0);
4546 // Request status report. Respond with "OK, no malfunction."
4548 // Send string directly to remote side
4549 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4552 writeRemote("\u009b0n");
4554 writeRemote("\033[0n");
4559 // Request cursor position. Respond with current position.
4560 if (currentState
.originMode
== true) {
4561 row
-= scrollRegionTop
;
4564 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4567 str
= String
.format("\u009b%d;%dR", row
+ 1,
4568 currentState
.cursorX
+ 1);
4570 str
= String
.format("\033[%d;%dR", row
+ 1,
4571 currentState
.cursorX
+ 1);
4574 // Send string directly to remote side
4579 if (decPrivateModeFlag
== true) {
4581 // Request printer status report. Respond with "Printer not
4584 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4585 && (s8c1t
== true)) {
4586 writeRemote("\u009b?13n");
4588 writeRemote("\033[?13n");
4594 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4595 && (decPrivateModeFlag
== true)
4598 // Request user-defined keys are locked or unlocked. Respond
4599 // with "User-defined keys are locked."
4601 if (s8c1t
== true) {
4602 writeRemote("\u009b?21n");
4604 writeRemote("\033[?21n");
4610 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4611 && (decPrivateModeFlag
== true)
4614 // Request keyboard language. Respond with "Keyboard
4615 // language is North American."
4617 if (s8c1t
== true) {
4618 writeRemote("\u009b?27;1n");
4620 writeRemote("\033[?27;1n");
4627 // Some other option, ignore
4633 * TBC - Tabulation clear.
4635 private void tbc() {
4636 int i
= getCsiParam(0, 0);
4638 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4639 for (Integer stop
: tabStops
) {
4640 if (stop
== currentState
.cursorX
) {
4645 tabStops
= newStops
;
4653 * Erase the characters in the current line from the start column to the
4654 * end column, inclusive.
4656 * @param start starting column to erase (between 0 and width - 1)
4657 * @param end ending column to erase (between 0 and width - 1)
4658 * @param honorProtected if true, do not erase characters with the
4659 * protected attribute set
4661 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4666 if (end
> width
- 1) {
4673 for (int i
= start
; i
<= end
; i
++) {
4674 DisplayLine line
= display
.get(currentState
.cursorY
);
4675 if ((!honorProtected
)
4676 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4683 * From the VT102 manual:
4685 * Erasing a character also erases any character
4686 * attribute of the character.
4692 * Erase with the current color a.k.a. back-color erase
4695 line
.setChar(i
, ' ');
4696 line
.setAttr(i
, currentState
.attr
);
4704 * Erase a rectangular section of the screen, inclusive. end column,
4707 * @param startRow starting row to erase (between 0 and height - 1)
4708 * @param startCol starting column to erase (between 0 and width - 1)
4709 * @param endRow ending row to erase (between 0 and height - 1)
4710 * @param endCol ending column to erase (between 0 and width - 1)
4711 * @param honorProtected if true, do not erase characters with the
4712 * protected attribute set
4714 private void eraseScreen(final int startRow
, final int startCol
,
4715 final int endRow
, final int endCol
, final boolean honorProtected
) {
4723 || (endRow
< startRow
)
4724 || (endCol
< startCol
)
4729 oldCursorY
= currentState
.cursorY
;
4730 for (int i
= startRow
; i
<= endRow
; i
++) {
4731 currentState
.cursorY
= i
;
4732 eraseLine(startCol
, endCol
, honorProtected
);
4734 // Erase display clears the double attributes
4735 display
.get(i
).setDoubleWidth(false);
4736 display
.get(i
).setDoubleHeight(0);
4738 currentState
.cursorY
= oldCursorY
;
4742 * VT220 printer functions. All of these are parsed, but won't do
4745 private void printerFunctions() {
4746 boolean decPrivateModeFlag
= false;
4747 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4748 if (collectBuffer
.charAt(i
) == '?') {
4749 decPrivateModeFlag
= true;
4754 int i
= getCsiParam(0, 0);
4759 if (decPrivateModeFlag
== false) {
4765 if (decPrivateModeFlag
== true) {
4766 // Print cursor line
4771 if (decPrivateModeFlag
== true) {
4772 // Auto print mode OFF
4774 // Printer controller OFF
4776 // Characters re-appear on the screen
4777 printerControllerMode
= false;
4782 if (decPrivateModeFlag
== true) {
4786 // Printer controller
4788 // Characters get sucked into oblivion
4789 printerControllerMode
= true;
4800 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4801 * of remote systems will send an XTerm title sequence even if TERM isn't
4804 * @param xtermChar the character received from the remote side
4806 private void oscPut(final char xtermChar
) {
4807 // System.err.println("oscPut: " + xtermChar);
4809 boolean oscEnd
= false;
4811 if (xtermChar
== 0x07) {
4814 if ((xtermChar
== '\\')
4815 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\033')
4821 collectBuffer
.append(xtermChar
);
4826 if (xtermChar
== 0x07) {
4827 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
4829 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4832 String
[] p
= args
.split(";");
4834 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4841 if (p
[0].equals("4")) {
4842 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
4843 // Set a color index value
4845 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
4846 } catch (NumberFormatException e
) {
4852 if (p
[0].equals("10")) {
4853 if (p
[1].equals("?")) {
4854 // Respond with foreground color.
4855 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToForegroundColor(currentState
.attr
);
4857 writeRemote(String
.format(
4858 "\033]10;rgb:%04x/%04x/%04x\033\\",
4859 color
.getRed() << 8,
4860 color
.getGreen() << 8,
4861 color
.getBlue() << 8));
4865 if (p
[0].equals("11")) {
4866 if (p
[1].equals("?")) {
4867 // Respond with background color.
4868 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToBackgroundColor(currentState
.attr
);
4870 writeRemote(String
.format(
4871 "\033]11;rgb:%04x/%04x/%04x\033\\",
4872 color
.getRed() << 8,
4873 color
.getGreen() << 8,
4874 color
.getBlue() << 8));
4878 if (p
[0].equals("444")) {
4879 if (p
[1].equals("0") && (p
.length
== 6)) {
4880 // Jexer image - RGB
4881 parseJexerImageRGB(p
[2], p
[3], p
[4], p
[5]);
4882 } else if (p
[1].equals("1") && (p
.length
== 4)) {
4883 // Jexer image - PNG
4884 parseJexerImageFile(1, p
[2], p
[3]);
4885 } else if (p
[1].equals("2") && (p
.length
== 4)) {
4886 // Jexer image - JPG
4887 parseJexerImageFile(2, p
[2], p
[3]);
4892 // Go to SCAN_GROUND state
4899 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
4900 * Jexer ECMA48Terminal to talk to ECMA48.
4902 * @param pmChar the character received from the remote side
4904 private void pmPut(final char pmChar
) {
4905 // System.err.println("pmPut: " + pmChar);
4907 boolean pmEnd
= false;
4909 if ((pmChar
== '\\')
4910 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\033')
4916 collectBuffer
.append(pmChar
);
4921 arg
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4923 // System.err.println("arg: '" + arg + "'");
4925 if (arg
.equals("hideMousePointer")) {
4926 hideMousePointer
= true;
4928 if (arg
.equals("showMousePointer")) {
4929 hideMousePointer
= false;
4932 // Go to SCAN_GROUND state
4939 * Perform xterm window operations.
4941 private void xtermWindowOps() {
4942 boolean xtermPrivateModeFlag
= false;
4944 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4945 if (collectBuffer
.charAt(i
) == '?') {
4946 xtermPrivateModeFlag
= true;
4951 int i
= getCsiParam(0, 0);
4953 if (!xtermPrivateModeFlag
) {
4956 // Report xterm text area size in pixels as CSI 4 ; height ;
4958 writeRemote(String
.format("\033[4;%d;%dt", textHeight
* height
,
4959 textWidth
* width
));
4962 // Report character size in pixels as CSI 6 ; height ; width
4964 writeRemote(String
.format("\033[6;%d;%dt", textHeight
,
4968 // Report the text are size in characters as CSI 8 ; height ;
4970 writeRemote(String
.format("\033[8;%d;%dt", height
, width
));
4979 * Respond to xterm sixel query.
4981 private void xtermSixelQuery() {
4982 int item
= getCsiParam(0, 0);
4983 int action
= getCsiParam(1, 0);
4984 int value
= getCsiParam(2, 0);
4989 // Report number of color registers.
4990 writeRemote(String
.format("\033[?%d;%d;%dS", item
, 0, 1024));
4997 // We will not support this option.
4998 writeRemote(String
.format("\033[?%d;%dS", item
, action
));
5002 * Run this input character through the ECMA48 state machine.
5004 * @param ch character from the remote side
5006 private void consume(final int ch
) {
5010 // System.err.printf("%c STATE = %s\n", ch, scanState);
5012 // Special "anywhere" states
5014 // 18, 1A --> execute, then switch to SCAN_GROUND
5015 if ((ch
== 0x18) || (ch
== 0x1A)) {
5016 // CAN and SUB abort escape sequences
5021 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
5025 if ((type
== DeviceType
.XTERM
)
5026 && ((scanState
== ScanState
.OSC_STRING
)
5027 || (scanState
== ScanState
.DCS_SIXEL
)
5028 || (scanState
== ScanState
.SOSPMAPC_STRING
))
5030 // Xterm can pass ESCAPE to its OSC sequence.
5031 // Xterm can pass ESCAPE to its DCS sequence.
5032 // Jexer can pass ESCAPE to its PM sequence.
5033 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
5034 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
5035 && (scanState
!= ScanState
.DCS_IGNORE
)
5036 && (scanState
!= ScanState
.DCS_PARAM
)
5037 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
5039 scanState
= ScanState
.ESCAPE
;
5044 // 0x9B == CSI 8-bit sequence
5046 scanState
= ScanState
.CSI_ENTRY
;
5050 // 0x9D goes to ScanState.OSC_STRING
5052 scanState
= ScanState
.OSC_STRING
;
5056 // 0x90 goes to DCS_ENTRY
5058 scanState
= ScanState
.DCS_ENTRY
;
5062 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
5063 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
5064 scanState
= ScanState
.SOSPMAPC_STRING
;
5068 // 0x7F (DEL) is always discarded
5073 switch (scanState
) {
5076 // 00-17, 19, 1C-1F --> execute
5077 // 80-8F, 91-9A, 9C --> execute
5078 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
5079 handleControlChar((char) ch
);
5083 if (((ch
>= 0x20) && (ch
<= 0x7F))
5087 // VT220 printer --> trash bin
5088 if (((type
== DeviceType
.VT220
)
5089 || (type
== DeviceType
.XTERM
))
5090 && (printerControllerMode
== true)
5095 // Hang onto this character
5096 repCh
= mapCharacter(ch
);
5098 // Print this character
5099 printCharacter(repCh
);
5104 // 00-17, 19, 1C-1F --> execute
5106 handleControlChar((char) ch
);
5110 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
5111 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5113 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
5117 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
5118 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
5129 // DECSC - Save cursor
5130 // Note this code overlaps both ANSI and VT52 mode
5135 // DECRC - Restore cursor
5136 // Note this code overlaps both ANSI and VT52 mode
5145 if (vt52Mode
== true) {
5146 // DECANM - Enter ANSI mode
5148 arrowKeyMode
= ArrowKeyMode
.VT100
;
5151 * From the VT102 docs: "You use ANSI mode to select
5152 * most terminal features; the terminal uses the same
5153 * features when it switches to VT52 mode. You
5154 * cannot, however, change most of these features in
5157 * In other words, do not reset any other attributes
5158 * when switching between VT52 submode and ANSI.
5162 currentState
.g0Charset
= CharacterSet
.US
;
5163 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5165 singleshift
= Singleshift
.NONE
;
5166 currentState
.glLockshift
= LockshiftMode
.NONE
;
5167 currentState
.grLockshift
= LockshiftMode
.NONE
;
5171 // DECKPAM - Keypad application mode
5172 // Note this code overlaps both ANSI and VT52 mode
5176 // DECKPNM - Keypad numeric mode
5177 // Note this code overlaps both ANSI and VT52 mode
5184 if (vt52Mode
== true) {
5185 // Cursor up, and stop at the top without scrolling
5190 if (vt52Mode
== true) {
5191 // Cursor down, and stop at the bottom without scrolling
5192 cursorDown(1, false);
5196 if (vt52Mode
== true) {
5197 // Cursor right, and stop at the right without scrolling
5198 cursorRight(1, false);
5202 if (vt52Mode
== true) {
5203 // Cursor left, and stop at the left without scrolling
5204 cursorLeft(1, false);
5211 if (vt52Mode
== true) {
5219 if (vt52Mode
== true) {
5220 // G0 --> Special graphics
5221 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
5225 if (vt52Mode
== true) {
5227 currentState
.g0Charset
= CharacterSet
.US
;
5231 if (vt52Mode
== true) {
5233 cursorPosition(0, 0);
5235 // HTS - Horizontal tabulation set
5240 if (vt52Mode
== true) {
5241 // Reverse line feed. Same as RI.
5246 if (vt52Mode
== true) {
5247 // Erase to end of screen
5248 eraseLine(currentState
.cursorX
, width
- 1, false);
5249 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
5254 if (vt52Mode
== true) {
5255 // Erase to end of line
5256 eraseLine(currentState
.cursorX
, width
- 1, false);
5262 if (vt52Mode
== true) {
5265 // RI - Reverse index
5270 if (vt52Mode
== false) {
5272 singleshift
= Singleshift
.SS2
;
5276 if (vt52Mode
== false) {
5278 singleshift
= Singleshift
.SS3
;
5285 if ((ch
>= 0x51) && (ch
<= 0x57)) {
5301 if (vt52Mode
== true) {
5302 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
5310 if (vt52Mode
== true) {
5312 // Send string directly to remote side
5313 writeRemote("\033/Z");
5316 // Send string directly to remote side
5317 writeRemote(deviceTypeResponse());
5328 // VT52 cannot get to any of these other states
5329 if (vt52Mode
== true) {
5334 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
5341 // RIS - Reset to initial state
5343 // Do I clear screen too? I think so...
5344 eraseScreen(0, 0, height
- 1, width
- 1, false);
5345 cursorPosition(0, 0);
5359 if ((type
== DeviceType
.VT220
)
5360 || (type
== DeviceType
.XTERM
)) {
5362 // VT220 lockshift G2 into GL
5363 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
5368 if ((type
== DeviceType
.VT220
)
5369 || (type
== DeviceType
.XTERM
)) {
5371 // VT220 lockshift G3 into GL
5372 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
5390 if ((type
== DeviceType
.VT220
)
5391 || (type
== DeviceType
.XTERM
)) {
5393 // VT220 lockshift G3 into GR
5394 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
5399 if ((type
== DeviceType
.VT220
)
5400 || (type
== DeviceType
.XTERM
)) {
5402 // VT220 lockshift G2 into GR
5403 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
5409 if ((type
== DeviceType
.VT220
)
5410 || (type
== DeviceType
.XTERM
)) {
5412 // VT220 lockshift G1 into GR
5413 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
5423 // 0x5B goes to CSI_ENTRY
5425 scanState
= ScanState
.CSI_ENTRY
;
5428 // 0x5D goes to OSC_STRING
5430 scanState
= ScanState
.OSC_STRING
;
5433 // 0x50 goes to DCS_ENTRY
5435 scanState
= ScanState
.DCS_ENTRY
;
5438 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5439 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
5440 scanState
= ScanState
.SOSPMAPC_STRING
;
5445 case ESCAPE_INTERMEDIATE
:
5446 // 00-17, 19, 1C-1F --> execute
5448 handleControlChar((char) ch
);
5451 // 20-2F --> collect
5452 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5456 // 30-7E --> dispatch, then switch to GROUND
5457 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
5460 if ((collectBuffer
.length() == 1)
5461 && (collectBuffer
.charAt(0) == '(')) {
5462 // G0 --> Special graphics
5463 currentState
.g0Charset
= CharacterSet
.DRAWING
;
5465 if ((collectBuffer
.length() == 1)
5466 && (collectBuffer
.charAt(0) == ')')) {
5467 // G1 --> Special graphics
5468 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5470 if ((type
== DeviceType
.VT220
)
5471 || (type
== DeviceType
.XTERM
)) {
5473 if ((collectBuffer
.length() == 1)
5474 && (collectBuffer
.charAt(0) == '*')) {
5475 // G2 --> Special graphics
5476 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5478 if ((collectBuffer
.length() == 1)
5479 && (collectBuffer
.charAt(0) == '+')) {
5480 // G3 --> Special graphics
5481 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5486 if ((collectBuffer
.length() == 1)
5487 && (collectBuffer
.charAt(0) == '(')) {
5488 // G0 --> Alternate character ROM standard character set
5489 currentState
.g0Charset
= CharacterSet
.ROM
;
5491 if ((collectBuffer
.length() == 1)
5492 && (collectBuffer
.charAt(0) == ')')) {
5493 // G1 --> Alternate character ROM standard character set
5494 currentState
.g1Charset
= CharacterSet
.ROM
;
5498 if ((collectBuffer
.length() == 1)
5499 && (collectBuffer
.charAt(0) == '(')) {
5500 // G0 --> Alternate character ROM special graphics
5501 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5503 if ((collectBuffer
.length() == 1)
5504 && (collectBuffer
.charAt(0) == ')')) {
5505 // G1 --> Alternate character ROM special graphics
5506 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5510 if ((collectBuffer
.length() == 1)
5511 && (collectBuffer
.charAt(0) == '#')) {
5512 // DECDHL - Double-height line (top half)
5517 if ((collectBuffer
.length() == 1)
5518 && (collectBuffer
.charAt(0) == '#')) {
5519 // DECDHL - Double-height line (bottom half)
5522 if ((type
== DeviceType
.VT220
)
5523 || (type
== DeviceType
.XTERM
)) {
5525 if ((collectBuffer
.length() == 1)
5526 && (collectBuffer
.charAt(0) == '(')) {
5528 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5530 if ((collectBuffer
.length() == 1)
5531 && (collectBuffer
.charAt(0) == ')')) {
5533 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5535 if ((collectBuffer
.length() == 1)
5536 && (collectBuffer
.charAt(0) == '*')) {
5538 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5540 if ((collectBuffer
.length() == 1)
5541 && (collectBuffer
.charAt(0) == '+')) {
5543 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5548 if ((collectBuffer
.length() == 1)
5549 && (collectBuffer
.charAt(0) == '#')) {
5550 // DECSWL - Single-width line
5553 if ((type
== DeviceType
.VT220
)
5554 || (type
== DeviceType
.XTERM
)) {
5556 if ((collectBuffer
.length() == 1)
5557 && (collectBuffer
.charAt(0) == '(')) {
5559 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5561 if ((collectBuffer
.length() == 1)
5562 && (collectBuffer
.charAt(0) == ')')) {
5564 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5566 if ((collectBuffer
.length() == 1)
5567 && (collectBuffer
.charAt(0) == '*')) {
5569 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5571 if ((collectBuffer
.length() == 1)
5572 && (collectBuffer
.charAt(0) == '+')) {
5574 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5579 if ((collectBuffer
.length() == 1)
5580 && (collectBuffer
.charAt(0) == '#')) {
5581 // DECDWL - Double-width line
5584 if ((type
== DeviceType
.VT220
)
5585 || (type
== DeviceType
.XTERM
)) {
5587 if ((collectBuffer
.length() == 1)
5588 && (collectBuffer
.charAt(0) == '(')) {
5590 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5592 if ((collectBuffer
.length() == 1)
5593 && (collectBuffer
.charAt(0) == ')')) {
5595 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5597 if ((collectBuffer
.length() == 1)
5598 && (collectBuffer
.charAt(0) == '*')) {
5600 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5602 if ((collectBuffer
.length() == 1)
5603 && (collectBuffer
.charAt(0) == '+')) {
5605 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5610 if ((type
== DeviceType
.VT220
)
5611 || (type
== DeviceType
.XTERM
)) {
5613 if ((collectBuffer
.length() == 1)
5614 && (collectBuffer
.charAt(0) == '(')) {
5616 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5618 if ((collectBuffer
.length() == 1)
5619 && (collectBuffer
.charAt(0) == ')')) {
5621 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5623 if ((collectBuffer
.length() == 1)
5624 && (collectBuffer
.charAt(0) == '*')) {
5626 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5628 if ((collectBuffer
.length() == 1)
5629 && (collectBuffer
.charAt(0) == '+')) {
5631 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5636 if ((collectBuffer
.length() == 1)
5637 && (collectBuffer
.charAt(0) == '#')) {
5638 // DECALN - Screen alignment display
5647 if ((type
== DeviceType
.VT220
)
5648 || (type
== DeviceType
.XTERM
)) {
5650 if ((collectBuffer
.length() == 1)
5651 && (collectBuffer
.charAt(0) == '(')) {
5652 // G0 --> DEC_SUPPLEMENTAL
5653 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5655 if ((collectBuffer
.length() == 1)
5656 && (collectBuffer
.charAt(0) == ')')) {
5657 // G1 --> DEC_SUPPLEMENTAL
5658 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5660 if ((collectBuffer
.length() == 1)
5661 && (collectBuffer
.charAt(0) == '*')) {
5662 // G2 --> DEC_SUPPLEMENTAL
5663 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5665 if ((collectBuffer
.length() == 1)
5666 && (collectBuffer
.charAt(0) == '+')) {
5667 // G3 --> DEC_SUPPLEMENTAL
5668 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5673 if ((type
== DeviceType
.VT220
)
5674 || (type
== DeviceType
.XTERM
)) {
5676 if ((collectBuffer
.length() == 1)
5677 && (collectBuffer
.charAt(0) == '(')) {
5679 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5681 if ((collectBuffer
.length() == 1)
5682 && (collectBuffer
.charAt(0) == ')')) {
5684 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5686 if ((collectBuffer
.length() == 1)
5687 && (collectBuffer
.charAt(0) == '*')) {
5689 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5691 if ((collectBuffer
.length() == 1)
5692 && (collectBuffer
.charAt(0) == '+')) {
5694 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5703 if ((collectBuffer
.length() == 1)
5704 && (collectBuffer
.charAt(0) == '(')) {
5705 // G0 --> United Kingdom set
5706 currentState
.g0Charset
= CharacterSet
.UK
;
5708 if ((collectBuffer
.length() == 1)
5709 && (collectBuffer
.charAt(0) == ')')) {
5710 // G1 --> United Kingdom set
5711 currentState
.g1Charset
= CharacterSet
.UK
;
5713 if ((type
== DeviceType
.VT220
)
5714 || (type
== DeviceType
.XTERM
)) {
5716 if ((collectBuffer
.length() == 1)
5717 && (collectBuffer
.charAt(0) == '*')) {
5718 // G2 --> United Kingdom set
5719 currentState
.g2Charset
= CharacterSet
.UK
;
5721 if ((collectBuffer
.length() == 1)
5722 && (collectBuffer
.charAt(0) == '+')) {
5723 // G3 --> United Kingdom set
5724 currentState
.g3Charset
= CharacterSet
.UK
;
5729 if ((collectBuffer
.length() == 1)
5730 && (collectBuffer
.charAt(0) == '(')) {
5732 currentState
.g0Charset
= CharacterSet
.US
;
5734 if ((collectBuffer
.length() == 1)
5735 && (collectBuffer
.charAt(0) == ')')) {
5737 currentState
.g1Charset
= CharacterSet
.US
;
5739 if ((type
== DeviceType
.VT220
)
5740 || (type
== DeviceType
.XTERM
)) {
5742 if ((collectBuffer
.length() == 1)
5743 && (collectBuffer
.charAt(0) == '*')) {
5745 currentState
.g2Charset
= CharacterSet
.US
;
5747 if ((collectBuffer
.length() == 1)
5748 && (collectBuffer
.charAt(0) == '+')) {
5750 currentState
.g3Charset
= CharacterSet
.US
;
5755 if ((type
== DeviceType
.VT220
)
5756 || (type
== DeviceType
.XTERM
)) {
5758 if ((collectBuffer
.length() == 1)
5759 && (collectBuffer
.charAt(0) == '(')) {
5761 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5763 if ((collectBuffer
.length() == 1)
5764 && (collectBuffer
.charAt(0) == ')')) {
5766 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5768 if ((collectBuffer
.length() == 1)
5769 && (collectBuffer
.charAt(0) == '*')) {
5771 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5773 if ((collectBuffer
.length() == 1)
5774 && (collectBuffer
.charAt(0) == '+')) {
5776 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5783 if ((type
== DeviceType
.VT220
)
5784 || (type
== DeviceType
.XTERM
)) {
5786 if ((collectBuffer
.length() == 1)
5787 && (collectBuffer
.charAt(0) == '(')) {
5789 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5791 if ((collectBuffer
.length() == 1)
5792 && (collectBuffer
.charAt(0) == ')')) {
5794 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5796 if ((collectBuffer
.length() == 1)
5797 && (collectBuffer
.charAt(0) == '*')) {
5799 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5801 if ((collectBuffer
.length() == 1)
5802 && (collectBuffer
.charAt(0) == '+')) {
5804 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5809 if ((type
== DeviceType
.VT220
)
5810 || (type
== DeviceType
.XTERM
)) {
5812 if ((collectBuffer
.length() == 1)
5813 && (collectBuffer
.charAt(0) == ' ')) {
5820 if ((type
== DeviceType
.VT220
)
5821 || (type
== DeviceType
.XTERM
)) {
5823 if ((collectBuffer
.length() == 1)
5824 && (collectBuffer
.charAt(0) == ' ')) {
5831 if ((type
== DeviceType
.VT220
)
5832 || (type
== DeviceType
.XTERM
)) {
5834 if ((collectBuffer
.length() == 1)
5835 && (collectBuffer
.charAt(0) == '(')) {
5837 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5839 if ((collectBuffer
.length() == 1)
5840 && (collectBuffer
.charAt(0) == ')')) {
5842 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5844 if ((collectBuffer
.length() == 1)
5845 && (collectBuffer
.charAt(0) == '*')) {
5847 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5849 if ((collectBuffer
.length() == 1)
5850 && (collectBuffer
.charAt(0) == '+')) {
5852 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5860 if ((type
== DeviceType
.VT220
)
5861 || (type
== DeviceType
.XTERM
)) {
5863 if ((collectBuffer
.length() == 1)
5864 && (collectBuffer
.charAt(0) == '(')) {
5866 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5868 if ((collectBuffer
.length() == 1)
5869 && (collectBuffer
.charAt(0) == ')')) {
5871 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5873 if ((collectBuffer
.length() == 1)
5874 && (collectBuffer
.charAt(0) == '*')) {
5876 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5878 if ((collectBuffer
.length() == 1)
5879 && (collectBuffer
.charAt(0) == '+')) {
5881 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5892 if ((type
== DeviceType
.VT220
)
5893 || (type
== DeviceType
.XTERM
)) {
5895 if ((collectBuffer
.length() == 1)
5896 && (collectBuffer
.charAt(0) == '(')) {
5898 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5900 if ((collectBuffer
.length() == 1)
5901 && (collectBuffer
.charAt(0) == ')')) {
5903 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5905 if ((collectBuffer
.length() == 1)
5906 && (collectBuffer
.charAt(0) == '*')) {
5908 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5910 if ((collectBuffer
.length() == 1)
5911 && (collectBuffer
.charAt(0) == '+')) {
5913 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5918 if ((type
== DeviceType
.VT220
)
5919 || (type
== DeviceType
.XTERM
)) {
5921 if ((collectBuffer
.length() == 1)
5922 && (collectBuffer
.charAt(0) == '(')) {
5924 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5926 if ((collectBuffer
.length() == 1)
5927 && (collectBuffer
.charAt(0) == ')')) {
5929 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5931 if ((collectBuffer
.length() == 1)
5932 && (collectBuffer
.charAt(0) == '*')) {
5934 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5936 if ((collectBuffer
.length() == 1)
5937 && (collectBuffer
.charAt(0) == '+')) {
5939 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5951 if ((type
== DeviceType
.VT220
)
5952 || (type
== DeviceType
.XTERM
)) {
5954 if ((collectBuffer
.length() == 1)
5955 && (collectBuffer
.charAt(0) == '(')) {
5957 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5959 if ((collectBuffer
.length() == 1)
5960 && (collectBuffer
.charAt(0) == ')')) {
5962 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5964 if ((collectBuffer
.length() == 1)
5965 && (collectBuffer
.charAt(0) == '*')) {
5967 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5969 if ((collectBuffer
.length() == 1)
5970 && (collectBuffer
.charAt(0) == '+')) {
5972 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5977 if ((type
== DeviceType
.VT220
)
5978 || (type
== DeviceType
.XTERM
)) {
5980 if ((collectBuffer
.length() == 1)
5981 && (collectBuffer
.charAt(0) == '(')) {
5983 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5985 if ((collectBuffer
.length() == 1)
5986 && (collectBuffer
.charAt(0) == ')')) {
5988 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5990 if ((collectBuffer
.length() == 1)
5991 && (collectBuffer
.charAt(0) == '*')) {
5993 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5995 if ((collectBuffer
.length() == 1)
5996 && (collectBuffer
.charAt(0) == '+')) {
5998 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
6045 // 0x9C goes to GROUND
6053 // 00-17, 19, 1C-1F --> execute
6055 handleControlChar((char) ch
);
6058 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6059 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6061 scanState
= ScanState
.CSI_INTERMEDIATE
;
6064 // 30-39, 3B --> param, then switch to CSI_PARAM
6065 if ((ch
>= '0') && (ch
<= '9')) {
6067 scanState
= ScanState
.CSI_PARAM
;
6071 scanState
= ScanState
.CSI_PARAM
;
6074 // 3C-3F --> collect, then switch to CSI_PARAM
6075 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6077 scanState
= ScanState
.CSI_PARAM
;
6080 // 40-7E --> dispatch, then switch to GROUND
6081 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6084 // ICH - Insert character
6092 // CUD - Cursor down
6096 // CUF - Cursor forward
6100 // CUB - Cursor backward
6104 // CNL - Cursor down and to column 1
6105 if (type
== DeviceType
.XTERM
) {
6110 // CPL - Cursor up and to column 1
6111 if (type
== DeviceType
.XTERM
) {
6116 // CHA - Cursor to column # in current row
6117 if (type
== DeviceType
.XTERM
) {
6122 // CUP - Cursor position
6126 // CHT - Cursor forward X tab stops (default 1)
6127 if (type
== DeviceType
.XTERM
) {
6132 // ED - Erase in display
6136 // EL - Erase in line
6151 // DCH - Delete character
6158 // Scroll up X lines (default 1)
6159 if (type
== DeviceType
.XTERM
) {
6160 boolean xtermPrivateModeFlag
= false;
6161 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6162 if (collectBuffer
.charAt(i
) == '?') {
6163 xtermPrivateModeFlag
= true;
6167 if (xtermPrivateModeFlag
) {
6175 // Scroll down X lines (default 1)
6176 if (type
== DeviceType
.XTERM
) {
6185 if ((type
== DeviceType
.VT220
)
6186 || (type
== DeviceType
.XTERM
)) {
6188 // ECH - Erase character
6195 // CBT - Cursor backward X tab stops (default 1)
6196 if (type
== DeviceType
.XTERM
) {
6207 // HPA - Cursor to column # in current row. Same as CHA
6208 if (type
== DeviceType
.XTERM
) {
6213 // HPR - Cursor right. Same as CUF
6214 if (type
== DeviceType
.XTERM
) {
6219 // REP - Repeat last char X times
6220 if (type
== DeviceType
.XTERM
) {
6225 // DA - Device attributes
6229 // VPA - Cursor to row, current column.
6230 if (type
== DeviceType
.XTERM
) {
6235 // VPR - Cursor down. Same as CUD
6236 if (type
== DeviceType
.XTERM
) {
6241 // HVP - Horizontal and vertical position
6245 // TBC - Tabulation clear
6249 // Sets an ANSI or DEC private toggle
6253 if ((type
== DeviceType
.VT220
)
6254 || (type
== DeviceType
.XTERM
)) {
6256 // Printer functions
6264 // Sets an ANSI or DEC private toggle
6268 // SGR - Select graphics rendition
6272 // DSR - Device status report
6279 // DECLL - Load leds
6283 // DECSTBM - Set top and bottom margins
6287 // Save cursor (ANSI.SYS)
6288 if (type
== DeviceType
.XTERM
) {
6289 savedState
.cursorX
= currentState
.cursorX
;
6290 savedState
.cursorY
= currentState
.cursorY
;
6294 if (type
== DeviceType
.XTERM
) {
6295 // Window operations
6300 // Restore cursor (ANSI.SYS)
6301 if (type
== DeviceType
.XTERM
) {
6302 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
6309 // DECREQTPARM - Request terminal parameters
6325 // 0x9C goes to GROUND
6330 // 0x3A goes to CSI_IGNORE
6332 scanState
= ScanState
.CSI_IGNORE
;
6337 // 00-17, 19, 1C-1F --> execute
6339 handleControlChar((char) ch
);
6342 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6343 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6345 scanState
= ScanState
.CSI_INTERMEDIATE
;
6348 // 30-39, 3B --> param
6349 if ((ch
>= '0') && (ch
<= '9')) {
6356 // 0x3A goes to CSI_IGNORE
6358 scanState
= ScanState
.CSI_IGNORE
;
6360 // 0x3C-3F goes to CSI_IGNORE
6361 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6362 scanState
= ScanState
.CSI_IGNORE
;
6365 // 40-7E --> dispatch, then switch to GROUND
6366 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6369 // ICH - Insert character
6377 // CUD - Cursor down
6381 // CUF - Cursor forward
6385 // CUB - Cursor backward
6389 // CNL - Cursor down and to column 1
6390 if (type
== DeviceType
.XTERM
) {
6395 // CPL - Cursor up and to column 1
6396 if (type
== DeviceType
.XTERM
) {
6401 // CHA - Cursor to column # in current row
6402 if (type
== DeviceType
.XTERM
) {
6407 // CUP - Cursor position
6411 // CHT - Cursor forward X tab stops (default 1)
6412 if (type
== DeviceType
.XTERM
) {
6417 // ED - Erase in display
6421 // EL - Erase in line
6436 // DCH - Delete character
6443 // Scroll up X lines (default 1)
6444 if (type
== DeviceType
.XTERM
) {
6445 boolean xtermPrivateModeFlag
= false;
6446 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6447 if (collectBuffer
.charAt(i
) == '?') {
6448 xtermPrivateModeFlag
= true;
6452 if (xtermPrivateModeFlag
) {
6460 // Scroll down X lines (default 1)
6461 if (type
== DeviceType
.XTERM
) {
6470 if ((type
== DeviceType
.VT220
)
6471 || (type
== DeviceType
.XTERM
)) {
6473 // ECH - Erase character
6480 // CBT - Cursor backward X tab stops (default 1)
6481 if (type
== DeviceType
.XTERM
) {
6492 // HPA - Cursor to column # in current row. Same as CHA
6493 if (type
== DeviceType
.XTERM
) {
6498 // HPR - Cursor right. Same as CUF
6499 if (type
== DeviceType
.XTERM
) {
6504 // REP - Repeat last char X times
6505 if (type
== DeviceType
.XTERM
) {
6510 // DA - Device attributes
6514 // VPA - Cursor to row, current column.
6515 if (type
== DeviceType
.XTERM
) {
6520 // VPR - Cursor down. Same as CUD
6521 if (type
== DeviceType
.XTERM
) {
6526 // HVP - Horizontal and vertical position
6530 // TBC - Tabulation clear
6534 // Sets an ANSI or DEC private toggle
6538 if ((type
== DeviceType
.VT220
)
6539 || (type
== DeviceType
.XTERM
)) {
6541 // Printer functions
6549 // Sets an ANSI or DEC private toggle
6553 // SGR - Select graphics rendition
6557 // DSR - Device status report
6564 // DECLL - Load leds
6568 // DECSTBM - Set top and bottom margins
6574 if (type
== DeviceType
.XTERM
) {
6575 // Window operations
6584 // DECREQTPARM - Request terminal parameters
6601 case CSI_INTERMEDIATE
:
6602 // 00-17, 19, 1C-1F --> execute
6604 handleControlChar((char) ch
);
6607 // 20-2F --> collect
6608 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6612 // 0x30-3F goes to CSI_IGNORE
6613 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6614 scanState
= ScanState
.CSI_IGNORE
;
6617 // 40-7E --> dispatch, then switch to GROUND
6618 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6670 if (((type
== DeviceType
.VT220
)
6671 || (type
== DeviceType
.XTERM
))
6672 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6674 // DECSCL - compatibility level
6677 if ((type
== DeviceType
.XTERM
)
6678 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6680 // DECSTR - Soft terminal reset
6685 if (((type
== DeviceType
.VT220
)
6686 || (type
== DeviceType
.XTERM
))
6687 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6715 // 00-17, 19, 1C-1F --> execute
6717 handleControlChar((char) ch
);
6720 // 20-2F --> collect
6721 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6725 // 40-7E --> ignore, then switch to GROUND
6726 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6730 // 20-3F, 7F --> ignore
6736 // 0x9C goes to GROUND
6741 // 0x1B 0x5C goes to GROUND
6746 if ((collectBuffer
.length() > 0)
6747 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6753 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6754 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6756 scanState
= ScanState
.DCS_INTERMEDIATE
;
6759 // 30-39, 3B --> param, then switch to DCS_PARAM
6760 if ((ch
>= '0') && (ch
<= '9')) {
6762 scanState
= ScanState
.DCS_PARAM
;
6766 scanState
= ScanState
.DCS_PARAM
;
6769 // 3C-3F --> collect, then switch to DCS_PARAM
6770 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6772 scanState
= ScanState
.DCS_PARAM
;
6775 // 00-17, 19, 1C-1F, 7F --> ignore
6777 // 0x3A goes to DCS_IGNORE
6779 scanState
= ScanState
.DCS_IGNORE
;
6782 // 0x71 goes to DCS_SIXEL
6784 sixelParseBuffer
.setLength(0);
6785 scanState
= ScanState
.DCS_SIXEL
;
6786 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6787 // 0x40-7E goes to DCS_PASSTHROUGH
6788 scanState
= ScanState
.DCS_PASSTHROUGH
;
6792 case DCS_INTERMEDIATE
:
6794 // 0x9C goes to GROUND
6799 // 0x1B 0x5C goes to GROUND
6804 if ((collectBuffer
.length() > 0)
6805 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6811 // 0x30-3F goes to DCS_IGNORE
6812 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6813 scanState
= ScanState
.DCS_IGNORE
;
6816 // 0x40-7E goes to DCS_PASSTHROUGH
6817 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6818 scanState
= ScanState
.DCS_PASSTHROUGH
;
6821 // 00-17, 19, 1C-1F, 7F --> ignore
6826 // 0x9C goes to GROUND
6831 // 0x1B 0x5C goes to GROUND
6836 if ((collectBuffer
.length() > 0)
6837 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6843 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6844 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6846 scanState
= ScanState
.DCS_INTERMEDIATE
;
6849 // 30-39, 3B --> param
6850 if ((ch
>= '0') && (ch
<= '9')) {
6857 // 00-17, 19, 1C-1F, 7F --> ignore
6859 // 0x3A, 3C-3F goes to DCS_IGNORE
6861 scanState
= ScanState
.DCS_IGNORE
;
6863 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6864 scanState
= ScanState
.DCS_IGNORE
;
6867 // 0x71 goes to DCS_SIXEL
6869 sixelParseBuffer
.setLength(0);
6870 scanState
= ScanState
.DCS_SIXEL
;
6871 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6872 // 0x40-7E goes to DCS_PASSTHROUGH
6873 scanState
= ScanState
.DCS_PASSTHROUGH
;
6877 case DCS_PASSTHROUGH
:
6878 // 0x9C goes to GROUND
6883 // 0x1B 0x5C goes to GROUND
6888 if ((collectBuffer
.length() > 0)
6889 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6895 // 00-17, 19, 1C-1F, 20-7E --> put
6897 // We ignore all DCS except sixel.
6901 // We ignore all DCS except sixel.
6904 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6905 // We ignore all DCS except sixel.
6908 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6909 // We ignore all DCS except sixel.
6918 // 00-17, 19, 1C-1F, 20-7F --> ignore
6920 // 0x9C goes to GROUND
6928 // 0x9C goes to GROUND
6935 // 0x1B 0x5C goes to GROUND
6941 if ((collectBuffer
.length() > 0)
6942 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6950 // 00-17, 19, 1C-1F, 20-7E --> put
6953 || ((ch
>= 0x1C) && (ch
<= 0x1F))
6954 || ((ch
>= 0x20) && (ch
<= 0x7E))
6956 sixelParseBuffer
.append((char) ch
);
6962 case SOSPMAPC_STRING
:
6963 // 00-17, 19, 1C-1F, 20-7F --> ignore
6965 // Special case for Jexer: PM can pass one control character
6970 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6974 // 0x9C goes to GROUND
6982 // Special case for Xterm: OSC can pass control characters
6983 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
6987 // 00-17, 19, 1C-1F --> ignore
6989 // 20-7F --> osc_put
6990 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6994 // 0x9C goes to GROUND
7001 case VT52_DIRECT_CURSOR_ADDRESS
:
7002 // This is a special case for the VT52 sequence "ESC Y l c"
7003 if (collectBuffer
.length() == 0) {
7005 } else if (collectBuffer
.length() == 1) {
7006 // We've got the two characters, one in the buffer and the
7008 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
7017 * Expose current cursor X to outside world.
7019 * @return current cursor X
7021 public final int getCursorX() {
7022 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
7023 return currentState
.cursorX
* 2;
7025 return currentState
.cursorX
;
7029 * Expose current cursor Y to outside world.
7031 * @return current cursor Y
7033 public final int getCursorY() {
7034 return currentState
.cursorY
;
7038 * Returns true if this terminal has requested the mouse pointer be
7041 * @return true if this terminal has requested the mouse pointer be
7044 public final boolean hasHiddenMousePointer() {
7045 return hideMousePointer
;
7049 * Get the mouse protocol.
7051 * @return MouseProtocol.OFF, MouseProtocol.X10, etc.
7053 public MouseProtocol
getMouseProtocol() {
7054 return mouseProtocol
;
7058 * Draw the left and right cells of a two-cell-wide (full-width) glyph.
7060 * @param leftX the x position to draw the left half to
7061 * @param leftY the y position to draw the left half to
7062 * @param rightX the x position to draw the right half to
7063 * @param rightY the y position to draw the right half to
7064 * @param ch the character to draw
7066 private void drawHalves(final int leftX
, final int leftY
,
7067 final int rightX
, final int rightY
, final int ch
) {
7069 // System.err.println("drawHalves(): " + Integer.toHexString(ch));
7071 if (lastTextHeight
!= textHeight
) {
7072 glyphMaker
= GlyphMaker
.getInstance(textHeight
);
7073 lastTextHeight
= textHeight
;
7076 Cell cell
= new Cell(ch
, currentState
.attr
);
7077 BufferedImage image
= glyphMaker
.getImage(cell
, textWidth
* 2,
7079 BufferedImage leftImage
= image
.getSubimage(0, 0, textWidth
,
7081 BufferedImage rightImage
= image
.getSubimage(textWidth
, 0, textWidth
,
7084 Cell left
= new Cell(cell
);
7085 left
.setImage(leftImage
);
7086 left
.setWidth(Cell
.Width
.LEFT
);
7087 display
.get(leftY
).replace(leftX
, left
);
7089 Cell right
= new Cell(cell
);
7090 right
.setImage(rightImage
);
7091 right
.setWidth(Cell
.Width
.RIGHT
);
7092 display
.get(rightY
).replace(rightX
, right
);
7096 * Set the width of a character cell in pixels.
7098 * @param textWidth the width in pixels of a character cell
7100 public void setTextWidth(final int textWidth
) {
7101 this.textWidth
= textWidth
;
7105 * Set the height of a character cell in pixels.
7107 * @param textHeight the height in pixels of a character cell
7109 public void setTextHeight(final int textHeight
) {
7110 this.textHeight
= textHeight
;
7114 * Parse a sixel string into a bitmap image, and overlay that image onto
7117 private void parseSixel() {
7120 System.err.println("parseSixel(): '" + sixelParseBuffer.toString()
7124 Sixel sixel
= new Sixel(sixelParseBuffer
.toString(), sixelPalette
);
7125 BufferedImage image
= sixel
.getImage();
7127 // System.err.println("parseSixel(): image " + image);
7129 if (image
== null) {
7130 // Sixel data was malformed in some way, bail out.
7133 if ((image
.getWidth() < 1)
7134 || (image
.getWidth() > 10000)
7135 || (image
.getHeight() < 1)
7136 || (image
.getHeight() > 10000)
7141 imageToCells(image
, true);
7145 * Parse a "Jexer" RGB image string into a bitmap image, and overlay that
7146 * image onto the text cells.
7148 * @param pw width token
7149 * @param ph height token
7150 * @param ps scroll token
7151 * @param data pixel data
7153 private void parseJexerImageRGB(final String pw
, final String ph
,
7154 final String ps
, final String data
) {
7157 int imageHeight
= 0;
7158 boolean scroll
= false;
7160 imageWidth
= Integer
.parseInt(pw
);
7161 imageHeight
= Integer
.parseInt(ph
);
7162 } catch (NumberFormatException e
) {
7166 if ((imageWidth
< 1)
7167 || (imageWidth
> 10000)
7168 || (imageHeight
< 1)
7169 || (imageHeight
> 10000)
7173 if (ps
.equals("1")) {
7175 } else if (ps
.equals("0")) {
7181 byte [] bytes
= StringUtils
.fromBase64(data
.getBytes());
7182 if (bytes
.length
!= (imageWidth
* imageHeight
* 3)) {
7186 BufferedImage image
= new BufferedImage(imageWidth
, imageHeight
,
7187 BufferedImage
.TYPE_INT_ARGB
);
7189 for (int x
= 0; x
< imageWidth
; x
++) {
7190 for (int y
= 0; y
< imageHeight
; y
++) {
7191 int red
= bytes
[(y
* imageWidth
* 3) + (x
* 3) ];
7195 int green
= bytes
[(y
* imageWidth
* 3) + (x
* 3) + 1];
7199 int blue
= bytes
[(y
* imageWidth
* 3) + (x
* 3) + 2];
7203 int rgb
= 0xFF000000 | (red
<< 16) | (green
<< 8) | blue
;
7204 image
.setRGB(x
, y
, rgb
);
7208 imageToCells(image
, scroll
);
7212 * Parse a "Jexer" PNG or JPG image string into a bitmap image, and
7213 * overlay that image onto the text cells.
7215 * @param type 1 for PNG, 2 for JPG
7216 * @param ps scroll token
7217 * @param data pixel data
7219 private void parseJexerImageFile(final int type
, final String ps
,
7220 final String data
) {
7223 int imageHeight
= 0;
7224 boolean scroll
= false;
7225 BufferedImage image
= null;
7227 byte [] bytes
= StringUtils
.fromBase64(data
.getBytes());
7231 if ((bytes
[0] != (byte) 0x89)
7232 || (bytes
[1] != 'P')
7233 || (bytes
[2] != 'N')
7234 || (bytes
[3] != 'G')
7235 || (bytes
[4] != (byte) 0x0D)
7236 || (bytes
[5] != (byte) 0x0A)
7237 || (bytes
[6] != (byte) 0x1A)
7238 || (bytes
[7] != (byte) 0x0A)
7240 // File does not have PNG header, bail out.
7246 if ((bytes
[0] != (byte) 0XFF)
7247 || (bytes
[1] != (byte) 0xD8)
7248 || (bytes
[2] != (byte) 0xFF)
7250 // File does not have JPG header, bail out.
7256 // Unsupported type, bail out.
7260 image
= ImageIO
.read(new ByteArrayInputStream(bytes
));
7261 } catch (IOException e
) {
7265 assert (image
!= null);
7266 imageWidth
= image
.getWidth();
7267 imageHeight
= image
.getHeight();
7268 if ((imageWidth
< 1)
7269 || (imageWidth
> 10000)
7270 || (imageHeight
< 1)
7271 || (imageHeight
> 10000)
7275 if (ps
.equals("1")) {
7277 } else if (ps
.equals("0")) {
7283 imageToCells(image
, scroll
);
7287 * Break up an image into the cells at the current cursor.
7289 * @param image the image to display
7290 * @param scroll if true, scroll the image and move the cursor
7292 private void imageToCells(final BufferedImage image
, final boolean scroll
) {
7293 assert (image
!= null);
7298 * Break up the image into text cell sized pieces as a new array of
7301 * Note original column position x0.
7305 * 1. Advance (printCharacter(' ')) for horizontal increment, or
7306 * index (linefeed() + cursorPosition(y, x0)) for vertical
7309 * 2. Set (x, y) cell image data.
7311 * 3. For the right and bottom edges:
7313 * a. Render the text to pixels using Terminus font.
7315 * b. Blit the image on top of the text, using alpha channel.
7317 int cellColumns
= image
.getWidth() / textWidth
;
7318 if (cellColumns
* textWidth
< image
.getWidth()) {
7321 int cellRows
= image
.getHeight() / textHeight
;
7322 if (cellRows
* textHeight
< image
.getHeight()) {
7326 // Break the image up into an array of cells.
7327 Cell
[][] cells
= new Cell
[cellColumns
][cellRows
];
7329 for (int x
= 0; x
< cellColumns
; x
++) {
7330 for (int y
= 0; y
< cellRows
; y
++) {
7332 int width
= textWidth
;
7333 if ((x
+ 1) * textWidth
> image
.getWidth()) {
7334 width
= image
.getWidth() - (x
* textWidth
);
7336 int height
= textHeight
;
7337 if ((y
+ 1) * textHeight
> image
.getHeight()) {
7338 height
= image
.getHeight() - (y
* textHeight
);
7341 Cell cell
= new Cell();
7342 if ((width
!= textWidth
) || (height
!= textHeight
)) {
7343 BufferedImage newImage
;
7344 newImage
= new BufferedImage(textWidth
, textHeight
,
7345 BufferedImage
.TYPE_INT_ARGB
);
7347 java
.awt
.Graphics gr
= newImage
.getGraphics();
7348 gr
.drawImage(image
.getSubimage(x
* textWidth
,
7349 y
* textHeight
, width
, height
),
7352 cell
.setImage(newImage
);
7354 cell
.setImage(image
.getSubimage(x
* textWidth
,
7355 y
* textHeight
, width
, height
));
7362 int x0
= currentState
.cursorX
;
7363 int y0
= currentState
.cursorY
;
7364 for (int y
= 0; y
< cellRows
; y
++) {
7365 for (int x
= 0; x
< cellColumns
; x
++) {
7366 assert (currentState
.cursorX
<= rightMargin
);
7368 // A real sixel terminal would render the text of the current
7369 // cell first, then image over it (accounting for blank
7370 // pixels). We do not support that. A cell is either text,
7371 // or image, but not a mix of image-over-text.
7372 DisplayLine line
= display
.get(currentState
.cursorY
);
7373 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
7375 // If at the end of the visible screen, stop.
7376 if (currentState
.cursorX
== rightMargin
) {
7379 // Room for more image on the visible screen.
7380 currentState
.cursorX
++;
7382 if (currentState
.cursorY
< scrollRegionBottom
- 1) {
7383 // Not at the bottom, down a line.
7385 } else if (scroll
== true) {
7386 // At the bottom, scroll as needed.
7389 // At the bottom, no more scrolling, done.
7393 cursorPosition(currentState
.cursorY
, x0
);
7396 if (scroll
== false) {
7397 cursorPosition(y0
, x0
);