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
.Graphics
;
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 scrollbackMax
= 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 appendScrollbackLine(display
.remove(0));
1263 * Get the maximum number of lines in the scrollback buffer.
1265 * @return the maximum number of lines in the scrollback buffer
1267 public int getScrollbackMax() {
1268 return scrollbackMax
;
1272 * Set the maximum number of lines for the scrollback buffer.
1274 * @param scrollbackMax the maximum number of lines for the scrollback
1277 public final void setScrollbackMax(final int scrollbackMax
) {
1278 this.scrollbackMax
= scrollbackMax
;
1282 * Get visible cursor flag.
1284 * @return if true, the cursor is visible
1286 public final boolean isCursorVisible() {
1287 return cursorVisible
;
1291 * Get the screen title as set by the xterm OSC sequence. Lots of
1292 * applications send a screenTitle regardless of whether it is an xterm
1295 * @return screen title
1297 public final String
getScreenTitle() {
1302 * Get 132 columns value.
1304 * @return if true, the terminal is in 132 column mode
1306 public final boolean isColumns132() {
1311 * Clear the CSI parameters and flags.
1313 private void toGround() {
1315 collectBuffer
.setLength(0);
1316 scanState
= ScanState
.GROUND
;
1320 * Reset the tab stops list.
1322 private void resetTabStops() {
1324 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
1325 tabStops
.add(Integer
.valueOf(i
* 8));
1330 * Reset the 88- or 256-colors.
1332 private void resetColors() {
1333 colors88
= new ArrayList
<Integer
>(256);
1334 for (int i
= 0; i
< 256; i
++) {
1338 // Set default system colors.
1339 colors88
.set(0, 0x00000000);
1340 colors88
.set(1, 0x00a80000);
1341 colors88
.set(2, 0x0000a800);
1342 colors88
.set(3, 0x00a85400);
1343 colors88
.set(4, 0x000000a8);
1344 colors88
.set(5, 0x00a800a8);
1345 colors88
.set(6, 0x0000a8a8);
1346 colors88
.set(7, 0x00a8a8a8);
1348 colors88
.set(8, 0x00545454);
1349 colors88
.set(9, 0x00fc5454);
1350 colors88
.set(10, 0x0054fc54);
1351 colors88
.set(11, 0x00fcfc54);
1352 colors88
.set(12, 0x005454fc);
1353 colors88
.set(13, 0x00fc54fc);
1354 colors88
.set(14, 0x0054fcfc);
1355 colors88
.set(15, 0x00fcfcfc);
1359 * Get the RGB value of one of the indexed colors.
1361 * @param index the color index
1362 * @return the RGB value
1364 private int get88Color(final int index
) {
1365 // System.err.print("get88Color: " + index);
1366 if ((index
< 0) || (index
> colors88
.size())) {
1367 // System.err.println(" -- UNKNOWN");
1370 // System.err.printf(" %08x\n", colors88.get(index));
1371 return colors88
.get(index
);
1375 * Set one of the indexed colors to a color specification.
1377 * @param index the color index
1378 * @param spec the specification, typically something like "rgb:aa/bb/cc"
1380 private void set88Color(final int index
, final String spec
) {
1381 // System.err.println("set88Color: " + index + " '" + spec + "'");
1383 if ((index
< 0) || (index
> colors88
.size())) {
1386 if (spec
.startsWith("rgb:")) {
1387 String
[] rgbTokens
= spec
.substring(4).split("/");
1388 if (rgbTokens
.length
== 3) {
1390 int rgb
= (Integer
.parseInt(rgbTokens
[0], 16) << 16);
1391 rgb
|= Integer
.parseInt(rgbTokens
[1], 16) << 8;
1392 rgb
|= Integer
.parseInt(rgbTokens
[2], 16);
1393 // System.err.printf(" set to %08x\n", rgb);
1394 colors88
.set(index
, rgb
);
1395 } catch (NumberFormatException e
) {
1402 if (spec
.toLowerCase().equals("black")) {
1403 colors88
.set(index
, 0x00000000);
1404 } else if (spec
.toLowerCase().equals("red")) {
1405 colors88
.set(index
, 0x00a80000);
1406 } else if (spec
.toLowerCase().equals("green")) {
1407 colors88
.set(index
, 0x0000a800);
1408 } else if (spec
.toLowerCase().equals("yellow")) {
1409 colors88
.set(index
, 0x00a85400);
1410 } else if (spec
.toLowerCase().equals("blue")) {
1411 colors88
.set(index
, 0x000000a8);
1412 } else if (spec
.toLowerCase().equals("magenta")) {
1413 colors88
.set(index
, 0x00a800a8);
1414 } else if (spec
.toLowerCase().equals("cyan")) {
1415 colors88
.set(index
, 0x0000a8a8);
1416 } else if (spec
.toLowerCase().equals("white")) {
1417 colors88
.set(index
, 0x00a8a8a8);
1423 * Reset the emulation state.
1425 private void reset() {
1427 currentState
= new SaveableState();
1428 savedState
= new SaveableState();
1429 scanState
= ScanState
.GROUND
;
1430 if (displayListener
!= null) {
1431 width
= displayListener
.getDisplayWidth();
1432 height
= displayListener
.getDisplayHeight();
1437 scrollRegionTop
= 0;
1438 scrollRegionBottom
= height
- 1;
1439 rightMargin
= width
- 1;
1440 newLineMode
= false;
1441 arrowKeyMode
= ArrowKeyMode
.ANSI
;
1442 keypadMode
= KeypadMode
.Numeric
;
1443 wrapLineFlag
= false;
1450 newLineMode
= false;
1451 reverseVideo
= false;
1453 cursorVisible
= true;
1456 singleshift
= Singleshift
.NONE
;
1458 printerControllerMode
= false;
1461 mouseProtocol
= MouseProtocol
.OFF
;
1462 mouseEncoding
= MouseEncoding
.X10
;
1467 // Reset extra colors
1475 * Append a to the scrollback buffer, clearing image data for lines more
1476 * than three screenfuls in.
1478 private void appendScrollbackLine(DisplayLine line
) {
1479 scrollback
.add(line
);
1480 if (scrollback
.size() > height
* 3) {
1481 scrollback
.get(scrollback
.size() - (height
* 3)).clearImages();
1486 * Append a new line to the bottom of the display, adding lines off the
1487 * top to the scrollback buffer.
1489 private void newDisplayLine() {
1490 // Scroll the top line off into the scrollback buffer
1491 appendScrollbackLine(display
.get(0));
1492 while (scrollback
.size() > scrollbackMax
) {
1493 scrollback
.remove(0);
1494 scrollback
.trimToSize();
1497 display
.trimToSize();
1498 DisplayLine line
= new DisplayLine(currentState
.attr
);
1499 line
.setReverseColor(reverseVideo
);
1504 * Wraps the current line.
1506 private void wrapCurrentLine() {
1507 if (currentState
.cursorY
== height
- 1) {
1510 if (currentState
.cursorY
< height
- 1) {
1511 currentState
.cursorY
++;
1513 currentState
.cursorX
= 0;
1517 * Handle a carriage return.
1519 private void carriageReturn() {
1520 currentState
.cursorX
= 0;
1521 wrapLineFlag
= false;
1525 * Reverse the color of the visible display.
1527 private void invertDisplayColors() {
1528 for (DisplayLine line
: display
) {
1529 line
.setReverseColor(!line
.isReverseColor());
1534 * Handle a linefeed.
1536 private void linefeed() {
1537 if (currentState
.cursorY
< scrollRegionBottom
) {
1538 // Increment screen y
1539 currentState
.cursorY
++;
1542 // Screen y does not increment
1545 * Two cases: either we're inside a scrolling region or not. If
1546 * the scrolling region bottom is the bottom of the screen, then
1547 * push the top line into the buffer. Else scroll the scrolling
1550 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1552 // We're at the bottom of the scroll region, AND the scroll
1553 // region is the entire screen.
1559 // We're at the bottom of the scroll region, AND the scroll
1560 // region is NOT the entire screen.
1561 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1566 currentState
.cursorX
= 0;
1568 wrapLineFlag
= false;
1572 * Prints one character to the display buffer.
1574 * @param ch character to display
1576 private void printCharacter(final int ch
) {
1577 int rightMargin
= this.rightMargin
;
1579 if (StringUtils
.width(ch
) == 2) {
1580 // This is a full-width character. Save two spaces, and then
1581 // draw the character as two image halves.
1582 int x0
= currentState
.cursorX
;
1583 int y0
= currentState
.cursorY
;
1584 printCharacter(' ');
1585 printCharacter(' ');
1586 if ((currentState
.cursorX
== x0
+ 2)
1587 && (currentState
.cursorY
== y0
)
1589 // We can draw both halves of the character.
1590 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1591 } else if ((currentState
.cursorX
== x0
+ 1)
1592 && (currentState
.cursorY
== y0
)
1594 // VT100 line wrap behavior: we should be at the right
1595 // margin. We can draw both halves of the character.
1596 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1598 // The character splits across the line. Draw the entire
1599 // character on the new line, giving one more space for it.
1600 x0
= currentState
.cursorX
- 1;
1601 y0
= currentState
.cursorY
;
1602 printCharacter(' ');
1603 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1608 // Check if we have double-width, and if so chop at 40/66 instead of
1610 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1611 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1614 // Check the unusually-complicated line wrapping conditions...
1615 if (currentState
.cursorX
== rightMargin
) {
1617 if (currentState
.lineWrap
== true) {
1619 * This case happens when: the cursor was already on the
1620 * right margin (either through printing or by an explicit
1621 * placement command), and a character was printed.
1623 * The line wraps only when a new character arrives AND the
1624 * cursor is already on the right margin AND has placed a
1625 * character in its cell. Easier to see than to explain.
1627 if (wrapLineFlag
== false) {
1629 * This block marks the case that we are in the margin
1630 * and the first character has been received and printed.
1632 wrapLineFlag
= true;
1635 * This block marks the case that we are in the margin
1636 * and the second character has been received and
1639 wrapLineFlag
= false;
1643 } else if (currentState
.cursorX
<= rightMargin
) {
1645 * This is the normal case: a character came in and was printed
1646 * to the left of the right margin column.
1649 // Turn off VT100 special-case flag
1650 wrapLineFlag
= false;
1653 // "Print" the character
1654 Cell newCell
= new Cell(ch
);
1655 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1656 newCellAttributes
.setTo(currentState
.attr
);
1657 DisplayLine line
= display
.get(currentState
.cursorY
);
1659 if (StringUtils
.width(ch
) == 1) {
1660 // Insert mode special case
1661 if (insertMode
== true) {
1662 line
.insert(currentState
.cursorX
, newCell
);
1664 // Replace an existing character
1665 line
.replace(currentState
.cursorX
, newCell
);
1668 // Increment horizontal
1669 if (wrapLineFlag
== false) {
1670 currentState
.cursorX
++;
1671 if (currentState
.cursorX
> rightMargin
) {
1672 currentState
.cursorX
--;
1679 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1680 * send to the remote side.
1682 * @param mouse mouse event received from the local user
1684 private void mouse(final TMouseEvent mouse
) {
1687 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1688 mouseProtocol, mouseEncoding, mouse);
1691 if (mouseEncoding
== MouseEncoding
.X10
) {
1692 // We will support X10 but only for (160,94) and smaller.
1693 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1698 switch (mouseProtocol
) {
1705 // Only report button presses
1706 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1712 // Only report button presses and releases
1713 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1714 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1722 * Only report button presses, button releases, and motions that
1723 * have a button down (i.e. drag-and-drop).
1725 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1726 if (!mouse
.isMouse1()
1727 && !mouse
.isMouse2()
1728 && !mouse
.isMouse3()
1729 && !mouse
.isMouseWheelUp()
1730 && !mouse
.isMouseWheelDown()
1738 // Report everything
1742 // Now encode the event
1743 StringBuilder sb
= new StringBuilder(6);
1744 if (mouseEncoding
== MouseEncoding
.SGR
) {
1745 sb
.append((char) 0x1B);
1749 if (mouse
.isMouse1()) {
1750 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1755 } else if (mouse
.isMouse2()) {
1756 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1761 } else if (mouse
.isMouse3()) {
1762 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1767 } else if (mouse
.isMouseWheelUp()) {
1769 } else if (mouse
.isMouseWheelDown()) {
1772 // This is motion with no buttons down.
1775 if (mouse
.isAlt()) {
1778 if (mouse
.isCtrl()) {
1781 if (mouse
.isShift()) {
1785 sb
.append(String
.format("%d;%d;%d", buttons
, mouse
.getX() + 1,
1788 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1795 // X10 and UTF8 encodings
1796 sb
.append((char) 0x1B);
1800 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1801 buttons
= 0x03 + 32;
1802 } else if (mouse
.isMouse1()) {
1803 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1804 buttons
= 0x00 + 32 + 32;
1806 buttons
= 0x00 + 32;
1808 } else if (mouse
.isMouse2()) {
1809 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1810 buttons
= 0x01 + 32 + 32;
1812 buttons
= 0x01 + 32;
1814 } else if (mouse
.isMouse3()) {
1815 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1816 buttons
= 0x02 + 32 + 32;
1818 buttons
= 0x02 + 32;
1820 } else if (mouse
.isMouseWheelUp()) {
1821 buttons
= 0x04 + 64;
1822 } else if (mouse
.isMouseWheelDown()) {
1823 buttons
= 0x05 + 64;
1825 // This is motion with no buttons down.
1826 buttons
= 0x03 + 32;
1828 if (mouse
.isAlt()) {
1831 if (mouse
.isCtrl()) {
1834 if (mouse
.isShift()) {
1838 sb
.append((char) (buttons
& 0xFF));
1839 sb
.append((char) (mouse
.getX() + 33));
1840 sb
.append((char) (mouse
.getY() + 33));
1843 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1844 writeRemote(sb
.toString());
1848 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1849 * send to the remote side.
1851 * @param keypress keypress received from the local user
1853 private void keypress(final TKeypress keypress
) {
1854 writeRemote(keypressToString(keypress
));
1858 * Build one of the complex xterm keystroke sequences, storing the result in
1859 * xterm_keystroke_buffer.
1861 * @param ss3 the prefix to use based on VT100 state.
1862 * @param first the first character, usually a number.
1863 * @param first the last character, one of the following: ~ A B C D F H
1864 * @param ctrl whether or not ctrl is down
1865 * @param alt whether or not alt is down
1866 * @param shift whether or not shift is down
1867 * @return the buffer with the full key sequence
1869 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1870 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1872 StringBuilder sb
= new StringBuilder(ss3
);
1873 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1877 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1879 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1881 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1883 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1885 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1887 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1889 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1894 return sb
.toString();
1898 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1900 * @param keypress keypress received from the local user
1901 * @return string to transmit to the remote side
1903 @SuppressWarnings("fallthrough")
1904 private String
keypressToString(final TKeypress keypress
) {
1906 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1908 * If this is a control character, process it like it came from
1911 if (keypress
.getChar() < 0x20) {
1912 handleControlChar((char) keypress
.getChar());
1914 // Local echo for everything else
1915 printCharacter(keypress
.getChar());
1917 if (displayListener
!= null) {
1918 displayListener
.displayChanged();
1922 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1927 // Handle control characters
1928 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1929 StringBuilder sb
= new StringBuilder();
1930 int ch
= keypress
.getChar();
1932 sb
.append(Character
.toChars(ch
));
1933 return sb
.toString();
1936 // Handle alt characters
1937 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1938 StringBuilder sb
= new StringBuilder("\033");
1939 int ch
= keypress
.getChar();
1940 sb
.append(Character
.toChars(ch
));
1941 return sb
.toString();
1944 if (keypress
.equals(kbBackspaceDel
)) {
1957 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1960 switch (arrowKeyMode
) {
1962 return xtermBuildKeySequence("\033[", '1', 'D',
1963 keypress
.isCtrl(), keypress
.isAlt(),
1964 keypress
.isShift());
1966 return xtermBuildKeySequence("\033", '1', 'D',
1967 keypress
.isCtrl(), keypress
.isAlt(),
1968 keypress
.isShift());
1970 return xtermBuildKeySequence("\033O", '1', 'D',
1971 keypress
.isCtrl(), keypress
.isAlt(),
1972 keypress
.isShift());
1975 switch (arrowKeyMode
) {
1986 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1989 switch (arrowKeyMode
) {
1991 return xtermBuildKeySequence("\033[", '1', 'C',
1992 keypress
.isCtrl(), keypress
.isAlt(),
1993 keypress
.isShift());
1995 return xtermBuildKeySequence("\033", '1', 'C',
1996 keypress
.isCtrl(), keypress
.isAlt(),
1997 keypress
.isShift());
1999 return xtermBuildKeySequence("\033O", '1', 'C',
2000 keypress
.isCtrl(), keypress
.isAlt(),
2001 keypress
.isShift());
2004 switch (arrowKeyMode
) {
2015 if (keypress
.equalsWithoutModifiers(kbUp
)) {
2018 switch (arrowKeyMode
) {
2020 return xtermBuildKeySequence("\033[", '1', 'A',
2021 keypress
.isCtrl(), keypress
.isAlt(),
2022 keypress
.isShift());
2024 return xtermBuildKeySequence("\033", '1', 'A',
2025 keypress
.isCtrl(), keypress
.isAlt(),
2026 keypress
.isShift());
2028 return xtermBuildKeySequence("\033O", '1', 'A',
2029 keypress
.isCtrl(), keypress
.isAlt(),
2030 keypress
.isShift());
2033 switch (arrowKeyMode
) {
2044 if (keypress
.equalsWithoutModifiers(kbDown
)) {
2047 switch (arrowKeyMode
) {
2049 return xtermBuildKeySequence("\033[", '1', 'B',
2050 keypress
.isCtrl(), keypress
.isAlt(),
2051 keypress
.isShift());
2053 return xtermBuildKeySequence("\033", '1', 'B',
2054 keypress
.isCtrl(), keypress
.isAlt(),
2055 keypress
.isShift());
2057 return xtermBuildKeySequence("\033O", '1', 'B',
2058 keypress
.isCtrl(), keypress
.isAlt(),
2059 keypress
.isShift());
2062 switch (arrowKeyMode
) {
2073 if (keypress
.equalsWithoutModifiers(kbHome
)) {
2076 switch (arrowKeyMode
) {
2078 return xtermBuildKeySequence("\033[", '1', 'H',
2079 keypress
.isCtrl(), keypress
.isAlt(),
2080 keypress
.isShift());
2082 return xtermBuildKeySequence("\033", '1', 'H',
2083 keypress
.isCtrl(), keypress
.isAlt(),
2084 keypress
.isShift());
2086 return xtermBuildKeySequence("\033O", '1', 'H',
2087 keypress
.isCtrl(), keypress
.isAlt(),
2088 keypress
.isShift());
2091 switch (arrowKeyMode
) {
2102 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
2105 switch (arrowKeyMode
) {
2107 return xtermBuildKeySequence("\033[", '1', 'F',
2108 keypress
.isCtrl(), keypress
.isAlt(),
2109 keypress
.isShift());
2111 return xtermBuildKeySequence("\033", '1', 'F',
2112 keypress
.isCtrl(), keypress
.isAlt(),
2113 keypress
.isShift());
2115 return xtermBuildKeySequence("\033O", '1', 'F',
2116 keypress
.isCtrl(), keypress
.isAlt(),
2117 keypress
.isShift());
2120 switch (arrowKeyMode
) {
2131 if (keypress
.equals(kbF1
)) {
2139 if (keypress
.equals(kbF2
)) {
2147 if (keypress
.equals(kbF3
)) {
2155 if (keypress
.equals(kbF4
)) {
2163 if (keypress
.equals(kbF5
)) {
2176 if (keypress
.equals(kbF6
)) {
2189 if (keypress
.equals(kbF7
)) {
2202 if (keypress
.equals(kbF8
)) {
2215 if (keypress
.equals(kbF9
)) {
2228 if (keypress
.equals(kbF10
)) {
2241 if (keypress
.equals(kbF11
)) {
2245 if (keypress
.equals(kbF12
)) {
2249 if (keypress
.equals(kbShiftF1
)) {
2254 if (type
== DeviceType
.XTERM
) {
2260 if (keypress
.equals(kbShiftF2
)) {
2265 if (type
== DeviceType
.XTERM
) {
2271 if (keypress
.equals(kbShiftF3
)) {
2276 if (type
== DeviceType
.XTERM
) {
2282 if (keypress
.equals(kbShiftF4
)) {
2287 if (type
== DeviceType
.XTERM
) {
2293 if (keypress
.equals(kbShiftF5
)) {
2295 return "\033[15;2~";
2298 if (keypress
.equals(kbShiftF6
)) {
2300 return "\033[17;2~";
2303 if (keypress
.equals(kbShiftF7
)) {
2305 return "\033[18;2~";
2308 if (keypress
.equals(kbShiftF8
)) {
2310 return "\033[19;2~";
2313 if (keypress
.equals(kbShiftF9
)) {
2315 return "\033[20;2~";
2318 if (keypress
.equals(kbShiftF10
)) {
2320 return "\033[21;2~";
2323 if (keypress
.equals(kbShiftF11
)) {
2325 return "\033[23;2~";
2328 if (keypress
.equals(kbShiftF12
)) {
2330 return "\033[24;2~";
2333 if (keypress
.equals(kbCtrlF1
)) {
2338 if (type
== DeviceType
.XTERM
) {
2344 if (keypress
.equals(kbCtrlF2
)) {
2349 if (type
== DeviceType
.XTERM
) {
2355 if (keypress
.equals(kbCtrlF3
)) {
2360 if (type
== DeviceType
.XTERM
) {
2366 if (keypress
.equals(kbCtrlF4
)) {
2371 if (type
== DeviceType
.XTERM
) {
2377 if (keypress
.equals(kbCtrlF5
)) {
2379 return "\033[15;5~";
2382 if (keypress
.equals(kbCtrlF6
)) {
2384 return "\033[17;5~";
2387 if (keypress
.equals(kbCtrlF7
)) {
2389 return "\033[18;5~";
2392 if (keypress
.equals(kbCtrlF8
)) {
2394 return "\033[19;5~";
2397 if (keypress
.equals(kbCtrlF9
)) {
2399 return "\033[20;5~";
2402 if (keypress
.equals(kbCtrlF10
)) {
2404 return "\033[21;5~";
2407 if (keypress
.equals(kbCtrlF11
)) {
2409 return "\033[23;5~";
2412 if (keypress
.equals(kbCtrlF12
)) {
2414 return "\033[24;5~";
2417 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2420 return xtermBuildKeySequence("\033[", '5', '~',
2421 keypress
.isCtrl(), keypress
.isAlt(),
2422 keypress
.isShift());
2428 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2431 return xtermBuildKeySequence("\033[", '6', '~',
2432 keypress
.isCtrl(), keypress
.isAlt(),
2433 keypress
.isShift());
2439 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2442 return xtermBuildKeySequence("\033[", '2', '~',
2443 keypress
.isCtrl(), keypress
.isAlt(),
2444 keypress
.isShift());
2450 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2453 return xtermBuildKeySequence("\033[", '3', '~',
2454 keypress
.isCtrl(), keypress
.isAlt(),
2455 keypress
.isShift());
2457 // Delete sends real delete for VTxxx
2462 if (keypress
.equals(kbEnter
)) {
2466 if (keypress
.equals(kbEsc
)) {
2470 if (keypress
.equals(kbAltEsc
)) {
2474 if (keypress
.equals(kbTab
)) {
2478 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2479 (keypress
.equals(kbShiftTab
))
2489 // Non-alt, non-ctrl characters
2490 if (!keypress
.isFnKey()) {
2491 StringBuilder sb
= new StringBuilder();
2492 sb
.append(Character
.toChars(keypress
.getChar()));
2493 return sb
.toString();
2499 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2502 * @param ch 8-bit character from the remote side
2503 * @param charsetGl character set defined for GL
2504 * @param charsetGr character set defined for GR
2505 * @return character to display on the screen
2507 private char mapCharacterCharset(final int ch
,
2508 final CharacterSet charsetGl
,
2509 final CharacterSet charsetGr
) {
2511 int lookupChar
= ch
;
2512 CharacterSet lookupCharset
= charsetGl
;
2515 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2516 lookupCharset
= charsetGr
;
2520 switch (lookupCharset
) {
2523 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2526 return DECCharacterSets
.UK
[lookupChar
];
2529 return DECCharacterSets
.US_ASCII
[lookupChar
];
2532 return DECCharacterSets
.NL
[lookupChar
];
2535 return DECCharacterSets
.FI
[lookupChar
];
2538 return DECCharacterSets
.FR
[lookupChar
];
2541 return DECCharacterSets
.FR_CA
[lookupChar
];
2544 return DECCharacterSets
.DE
[lookupChar
];
2547 return DECCharacterSets
.IT
[lookupChar
];
2550 return DECCharacterSets
.NO
[lookupChar
];
2553 return DECCharacterSets
.ES
[lookupChar
];
2556 return DECCharacterSets
.SV
[lookupChar
];
2559 return DECCharacterSets
.SWISS
[lookupChar
];
2561 case DEC_SUPPLEMENTAL
:
2562 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2565 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2568 return DECCharacterSets
.US_ASCII
[lookupChar
];
2571 return DECCharacterSets
.US_ASCII
[lookupChar
];
2574 throw new IllegalArgumentException("Invalid character set value: "
2580 * Map an 8-bit byte into a printable character.
2582 * @param ch either 8-bit or Unicode character from the remote side
2583 * @return character to display on the screen
2585 private int mapCharacter(final int ch
) {
2587 // Unicode character, just return it
2591 CharacterSet charsetGl
= currentState
.g0Charset
;
2592 CharacterSet charsetGr
= currentState
.grCharset
;
2594 if (vt52Mode
== true) {
2595 if (shiftOut
== true) {
2596 // Shifted out character, pull from VT52 graphics
2597 charsetGl
= currentState
.g1Charset
;
2598 charsetGr
= CharacterSet
.US
;
2601 charsetGl
= currentState
.g0Charset
;
2602 charsetGr
= CharacterSet
.US
;
2605 // Pull the character
2606 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2610 if (shiftOut
== true) {
2611 // Shifted out character, pull from G1
2612 charsetGl
= currentState
.g1Charset
;
2613 charsetGr
= currentState
.grCharset
;
2615 // Pull the character
2616 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2620 if (singleshift
== Singleshift
.SS2
) {
2622 singleshift
= Singleshift
.NONE
;
2624 // Shifted out character, pull from G2
2625 charsetGl
= currentState
.g2Charset
;
2626 charsetGr
= currentState
.grCharset
;
2630 if (singleshift
== Singleshift
.SS3
) {
2632 singleshift
= Singleshift
.NONE
;
2634 // Shifted out character, pull from G3
2635 charsetGl
= currentState
.g3Charset
;
2636 charsetGr
= currentState
.grCharset
;
2639 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2640 // Check for locking shift
2642 switch (currentState
.glLockshift
) {
2645 throw new IllegalArgumentException("programming bug");
2648 throw new IllegalArgumentException("programming bug");
2651 throw new IllegalArgumentException("programming bug");
2655 charsetGl
= currentState
.g2Charset
;
2660 charsetGl
= currentState
.g3Charset
;
2665 charsetGl
= currentState
.g0Charset
;
2669 switch (currentState
.grLockshift
) {
2672 throw new IllegalArgumentException("programming bug");
2675 throw new IllegalArgumentException("programming bug");
2679 charsetGr
= currentState
.g1Charset
;
2684 charsetGr
= currentState
.g2Charset
;
2689 charsetGr
= currentState
.g3Charset
;
2694 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2701 // Pull the character
2702 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2706 * Scroll the text within a scrolling region up n lines.
2708 * @param regionTop top row of the scrolling region
2709 * @param regionBottom bottom row of the scrolling region
2710 * @param n number of lines to scroll
2712 private void scrollingRegionScrollUp(final int regionTop
,
2713 final int regionBottom
, final int n
) {
2715 if (regionTop
>= regionBottom
) {
2719 // Sanity check: see if there will be any characters left after the
2721 if (regionBottom
+ 1 - regionTop
<= n
) {
2722 // There won't be anything left in the region, so just call
2723 // eraseScreen() and return.
2724 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2728 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2729 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2730 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2732 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2733 - remaining
, regionBottom
+ 1);
2734 display
= new ArrayList
<DisplayLine
>(displayTop
);
2735 display
.addAll(displayMiddle
);
2736 for (int i
= 0; i
< n
; i
++) {
2737 DisplayLine line
= new DisplayLine(currentState
.attr
);
2738 line
.setReverseColor(reverseVideo
);
2741 display
.addAll(displayBottom
);
2743 assert (display
.size() == height
);
2747 * Scroll the text within a scrolling region down n lines.
2749 * @param regionTop top row of the scrolling region
2750 * @param regionBottom bottom row of the scrolling region
2751 * @param n number of lines to scroll
2753 private void scrollingRegionScrollDown(final int regionTop
,
2754 final int regionBottom
, final int n
) {
2756 if (regionTop
>= regionBottom
) {
2760 // Sanity check: see if there will be any characters left after the
2762 if (regionBottom
+ 1 - regionTop
<= n
) {
2763 // There won't be anything left in the region, so just call
2764 // eraseScreen() and return.
2765 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2769 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2770 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2771 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2773 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2774 regionTop
+ remaining
);
2775 display
= new ArrayList
<DisplayLine
>(displayTop
);
2776 for (int i
= 0; i
< n
; i
++) {
2777 DisplayLine line
= new DisplayLine(currentState
.attr
);
2778 line
.setReverseColor(reverseVideo
);
2781 display
.addAll(displayMiddle
);
2782 display
.addAll(displayBottom
);
2784 assert (display
.size() == height
);
2788 * Process a control character.
2790 * @param ch 8-bit character from the remote side
2792 private void handleControlChar(final char ch
) {
2793 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2804 // Transmit the answerback message.
2815 cursorLeft(1, false);
2820 advanceToNextTabStop();
2846 currentState
.glLockshift
= LockshiftMode
.NONE
;
2852 currentState
.glLockshift
= LockshiftMode
.NONE
;
2877 singleshift
= Singleshift
.SS2
;
2882 singleshift
= Singleshift
.SS3
;
2892 * Advance the cursor to the next tab stop.
2894 private void advanceToNextTabStop() {
2895 if (tabStops
.size() == 0) {
2896 // Go to the rightmost column
2897 cursorRight(rightMargin
- currentState
.cursorX
, false);
2900 for (Integer stop
: tabStops
) {
2901 if (stop
> currentState
.cursorX
) {
2902 cursorRight(stop
- currentState
.cursorX
, false);
2907 * We got here, meaning there isn't a tab stop beyond the current
2908 * cursor position. Place the cursor of the right-most edge of the
2911 cursorRight(rightMargin
- currentState
.cursorX
, false);
2915 * Save a character into the collect buffer.
2917 * @param ch character to save
2919 private void collect(final char ch
) {
2920 collectBuffer
.append(ch
);
2924 * Save a byte into the CSI parameters buffer.
2926 * @param ch byte to save
2928 private void param(final byte ch
) {
2929 if (csiParams
.size() == 0) {
2930 csiParams
.add(Integer
.valueOf(0));
2932 Integer x
= csiParams
.get(csiParams
.size() - 1);
2933 if ((ch
>= '0') && (ch
<= '9')) {
2936 csiParams
.set(csiParams
.size() - 1, x
);
2939 if ((ch
== ';') && (csiParams
.size() < 16)) {
2940 csiParams
.add(Integer
.valueOf(0));
2945 * Get a CSI parameter value, with a default.
2947 * @param position parameter index. 0 is the first parameter.
2948 * @param defaultValue value to use if csiParams[position] doesn't exist
2949 * @return parameter value
2951 private int getCsiParam(final int position
, final int defaultValue
) {
2952 if (csiParams
.size() < position
+ 1) {
2953 return defaultValue
;
2955 return csiParams
.get(position
).intValue();
2959 * Get a CSI parameter value, clamped to within min/max.
2961 * @param position parameter index. 0 is the first parameter.
2962 * @param defaultValue value to use if csiParams[position] doesn't exist
2963 * @param minValue minimum value inclusive
2964 * @param maxValue maximum value inclusive
2965 * @return parameter value
2967 private int getCsiParam(final int position
, final int defaultValue
,
2968 final int minValue
, final int maxValue
) {
2970 assert (minValue
<= maxValue
);
2971 int value
= getCsiParam(position
, defaultValue
);
2972 if (value
< minValue
) {
2975 if (value
> maxValue
) {
2982 * Set or unset a toggle.
2984 * @param value true for set ('h'), false for reset ('l')
2986 private void setToggle(final boolean value
) {
2987 boolean decPrivateModeFlag
= false;
2989 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2990 if (collectBuffer
.charAt(i
) == '?') {
2991 decPrivateModeFlag
= true;
2996 for (Integer i
: csiParams
) {
3001 if (decPrivateModeFlag
== true) {
3003 if (value
== true) {
3004 // Use application arrow keys
3005 arrowKeyMode
= ArrowKeyMode
.VT100
;
3007 // Use ANSI arrow keys
3008 arrowKeyMode
= ArrowKeyMode
.ANSI
;
3013 if (decPrivateModeFlag
== true) {
3014 if (value
== false) {
3018 arrowKeyMode
= ArrowKeyMode
.VT52
;
3021 * From the VT102 docs: "You use ANSI mode to select
3022 * most terminal features; the terminal uses the same
3023 * features when it switches to VT52 mode. You
3024 * cannot, however, change most of these features in
3027 * In other words, do not reset any other attributes
3028 * when switching between VT52 submode and ANSI.
3030 * HOWEVER, the real vt100 does switch the character
3031 * set according to Usenet.
3033 currentState
.g0Charset
= CharacterSet
.US
;
3034 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3037 if ((type
== DeviceType
.VT220
)
3038 || (type
== DeviceType
.XTERM
)) {
3040 // VT52 mode is explicitly 7-bit
3042 singleshift
= Singleshift
.NONE
;
3047 if (value
== true) {
3048 // Turn off keyboard
3057 if (decPrivateModeFlag
== true) {
3059 if (value
== true) {
3066 if ((displayListener
!= null)
3067 && (type
== DeviceType
.XTERM
)
3069 // For xterms, reset to the actual width, not 80
3071 width
= displayListener
.getDisplayWidth();
3072 rightMargin
= width
- 1;
3075 width
= rightMargin
+ 1;
3078 // Entire screen is cleared, and scrolling region is
3080 eraseScreen(0, 0, height
- 1, width
- 1, false);
3081 scrollRegionTop
= 0;
3082 scrollRegionBottom
= height
- 1;
3083 // Also home the cursor
3084 cursorPosition(0, 0);
3088 if (decPrivateModeFlag
== true) {
3090 if (value
== true) {
3099 if (value
== true) {
3107 if (decPrivateModeFlag
== true) {
3109 if (value
== true) {
3111 * Set selects reverse screen, a white screen
3112 * background with black characters.
3114 if (reverseVideo
!= true) {
3116 * If in normal video, switch it back
3118 invertDisplayColors();
3120 reverseVideo
= true;
3123 * Reset selects normal screen, a black screen
3124 * background with white characters.
3126 if (reverseVideo
== true) {
3128 * If in reverse video already, switch it back
3130 invertDisplayColors();
3132 reverseVideo
= false;
3137 if (decPrivateModeFlag
== true) {
3139 if (value
== true) {
3140 // Origin is relative to scroll region cursor.
3141 // Cursor can NEVER leave scrolling region.
3142 currentState
.originMode
= true;
3143 cursorPosition(0, 0);
3145 // Origin is absolute to entire screen. Cursor can
3146 // leave the scrolling region via cup() and hvp().
3147 currentState
.originMode
= false;
3148 cursorPosition(0, 0);
3153 if (decPrivateModeFlag
== true) {
3155 if (value
== true) {
3157 currentState
.lineWrap
= true;
3159 // Turn linewrap off
3160 currentState
.lineWrap
= false;
3165 if (decPrivateModeFlag
== true) {
3167 if (value
== true) {
3168 // Keyboard auto-repeat on
3171 // Keyboard auto-repeat off
3177 if (decPrivateModeFlag
== false) {
3179 if (value
== true) {
3189 if (decPrivateModeFlag
== true) {
3195 if (decPrivateModeFlag
== true) {
3201 if (decPrivateModeFlag
== false) {
3203 if (value
== true) {
3205 * Set causes a received linefeed, form feed, or
3206 * vertical tab to move cursor to first column of
3207 * next line. RETURN transmits both a carriage return
3208 * and linefeed. This selection is also called new
3214 * Reset causes a received linefeed, form feed, or
3215 * vertical tab to move cursor to next line in
3216 * current column. RETURN transmits a carriage
3219 newLineMode
= false;
3225 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3226 if (decPrivateModeFlag
== true) {
3228 if (value
== true) {
3230 cursorVisible
= true;
3233 cursorVisible
= false;
3240 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3241 if (decPrivateModeFlag
== true) {
3243 if (value
== true) {
3244 // Select national mode NRC
3247 // Select multi-national mode
3256 if (type
== DeviceType
.XTERM
) {
3257 if (decPrivateModeFlag
== true) {
3258 if (value
== true) {
3259 // Enable sixel scrolling (default).
3262 // Disable sixel scrolling.
3271 if ((type
== DeviceType
.XTERM
)
3272 && (decPrivateModeFlag
== true)
3274 // Mouse: normal tracking mode
3275 if (value
== true) {
3276 mouseProtocol
= MouseProtocol
.NORMAL
;
3278 mouseProtocol
= MouseProtocol
.OFF
;
3284 if ((type
== DeviceType
.XTERM
)
3285 && (decPrivateModeFlag
== true)
3287 // Mouse: normal tracking mode
3288 if (value
== true) {
3289 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
3291 mouseProtocol
= MouseProtocol
.OFF
;
3297 if ((type
== DeviceType
.XTERM
)
3298 && (decPrivateModeFlag
== true)
3300 // Mouse: Any-event tracking mode
3301 if (value
== true) {
3302 mouseProtocol
= MouseProtocol
.ANYEVENT
;
3304 mouseProtocol
= MouseProtocol
.OFF
;
3310 if ((type
== DeviceType
.XTERM
)
3311 && (decPrivateModeFlag
== true)
3313 // Mouse: UTF-8 coordinates
3314 if (value
== true) {
3315 mouseEncoding
= MouseEncoding
.UTF8
;
3317 mouseEncoding
= MouseEncoding
.X10
;
3323 if ((type
== DeviceType
.XTERM
)
3324 && (decPrivateModeFlag
== true)
3326 // Mouse: SGR coordinates
3327 if (value
== true) {
3328 mouseEncoding
= MouseEncoding
.SGR
;
3330 mouseEncoding
= MouseEncoding
.X10
;
3336 if (type
== DeviceType
.XTERM
) {
3337 if (decPrivateModeFlag
== true) {
3338 if (value
== true) {
3339 // Use private color registers for each sixel
3340 // graphic (default).
3341 sixelPalette
= null;
3343 // Use shared color registers for each sixel
3345 sixelPalette
= new HashMap
<Integer
, java
.awt
.Color
>();
3359 * DECSC - Save cursor.
3361 private void decsc() {
3362 savedState
.setTo(currentState
);
3366 * DECRC - Restore cursor.
3368 private void decrc() {
3369 currentState
.setTo(savedState
);
3375 private void ind() {
3376 // Move the cursor and scroll if necessary. If at the bottom line
3377 // already, a scroll up is supposed to be performed.
3378 if (currentState
.cursorY
== scrollRegionBottom
) {
3379 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3381 cursorDown(1, true);
3385 * RI - Reverse index.
3388 // Move the cursor and scroll if necessary. If at the top line
3389 // already, a scroll down is supposed to be performed.
3390 if (currentState
.cursorY
== scrollRegionTop
) {
3391 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3399 private void nel() {
3400 // Move the cursor and scroll if necessary. If at the bottom line
3401 // already, a scroll up is supposed to be performed.
3402 if (currentState
.cursorY
== scrollRegionBottom
) {
3403 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3405 cursorDown(1, true);
3407 // Reset to the beginning of the next line
3408 currentState
.cursorX
= 0;
3412 * DECKPAM - Keypad application mode.
3414 private void deckpam() {
3415 keypadMode
= KeypadMode
.Application
;
3419 * DECKPNM - Keypad numeric mode.
3421 private void deckpnm() {
3422 keypadMode
= KeypadMode
.Numeric
;
3428 * @param n number of spaces to move
3429 * @param honorScrollRegion if true, then do nothing if the cursor is
3430 * outside the scrolling region
3432 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3436 * Special case: if a user moves the cursor from the right margin, we
3437 * have to reset the VT100 right margin flag.
3440 wrapLineFlag
= false;
3443 for (int i
= 0; i
< n
; i
++) {
3444 if (honorScrollRegion
== true) {
3445 // Honor the scrolling region
3446 if ((currentState
.cursorY
< scrollRegionTop
)
3447 || (currentState
.cursorY
> scrollRegionBottom
)
3449 // Outside region, do nothing
3452 // Inside region, go up
3453 top
= scrollRegionTop
;
3455 // Non-scrolling case
3459 if (currentState
.cursorY
> top
) {
3460 currentState
.cursorY
--;
3466 * Move down n spaces.
3468 * @param n number of spaces to move
3469 * @param honorScrollRegion if true, then do nothing if the cursor is
3470 * outside the scrolling region
3472 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3476 * Special case: if a user moves the cursor from the right margin, we
3477 * have to reset the VT100 right margin flag.
3480 wrapLineFlag
= false;
3483 for (int i
= 0; i
< n
; i
++) {
3485 if (honorScrollRegion
== true) {
3486 // Honor the scrolling region
3487 if (currentState
.cursorY
> scrollRegionBottom
) {
3488 // Outside region, do nothing
3491 // Inside region, go down
3492 bottom
= scrollRegionBottom
;
3494 // Non-scrolling case
3495 bottom
= height
- 1;
3498 if (currentState
.cursorY
< bottom
) {
3499 currentState
.cursorY
++;
3505 * Move left n spaces.
3507 * @param n number of spaces to move
3508 * @param honorScrollRegion if true, then do nothing if the cursor is
3509 * outside the scrolling region
3511 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3513 * Special case: if a user moves the cursor from the right margin, we
3514 * have to reset the VT100 right margin flag.
3517 wrapLineFlag
= false;
3520 for (int i
= 0; i
< n
; i
++) {
3521 if (honorScrollRegion
== true) {
3522 // Honor the scrolling region
3523 if ((currentState
.cursorY
< scrollRegionTop
)
3524 || (currentState
.cursorY
> scrollRegionBottom
)
3526 // Outside region, do nothing
3531 if (currentState
.cursorX
> 0) {
3532 currentState
.cursorX
--;
3538 * Move right n spaces.
3540 * @param n number of spaces to move
3541 * @param honorScrollRegion if true, then do nothing if the cursor is
3542 * outside the scrolling region
3544 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3545 int rightMargin
= this.rightMargin
;
3548 * Special case: if a user moves the cursor from the right margin, we
3549 * have to reset the VT100 right margin flag.
3552 wrapLineFlag
= false;
3555 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3556 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3559 for (int i
= 0; i
< n
; i
++) {
3560 if (honorScrollRegion
== true) {
3561 // Honor the scrolling region
3562 if ((currentState
.cursorY
< scrollRegionTop
)
3563 || (currentState
.cursorY
> scrollRegionBottom
)
3565 // Outside region, do nothing
3570 if (currentState
.cursorX
< rightMargin
) {
3571 currentState
.cursorX
++;
3577 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3579 * @param row row to move to
3580 * @param col column to move to
3582 private void cursorPosition(int row
, final int col
) {
3583 int rightMargin
= this.rightMargin
;
3588 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3589 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3592 // Set column number
3593 currentState
.cursorX
= col
;
3595 // Sanity check, bring column back to margin.
3596 if (currentState
.cursorX
> rightMargin
) {
3597 currentState
.cursorX
= rightMargin
;
3601 if (currentState
.originMode
== true) {
3602 row
+= scrollRegionTop
;
3604 if (currentState
.cursorY
< row
) {
3605 cursorDown(row
- currentState
.cursorY
, false);
3606 } else if (currentState
.cursorY
> row
) {
3607 cursorUp(currentState
.cursorY
- row
, false);
3610 wrapLineFlag
= false;
3614 * HTS - Horizontal tabulation set.
3616 private void hts() {
3617 for (Integer stop
: tabStops
) {
3618 if (stop
== currentState
.cursorX
) {
3619 // Already have a tab stop here
3624 // Append a tab stop to the end of the array and resort them
3625 tabStops
.add(currentState
.cursorX
);
3626 Collections
.sort(tabStops
);
3630 * DECSWL - Single-width line.
3632 private void decswl() {
3633 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3634 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3638 * DECDWL - Double-width line.
3640 private void decdwl() {
3641 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3642 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3646 * DECHDL - Double-height + double-width line.
3648 * @param topHalf if true, this sets the row to be the top half row of a
3651 private void dechdl(final boolean topHalf
) {
3652 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3653 if (topHalf
== true) {
3654 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3656 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3661 * DECALN - Screen alignment display.
3663 private void decaln() {
3664 Cell newCell
= new Cell('E');
3665 for (DisplayLine line
: display
) {
3666 for (int i
= 0; i
< line
.length(); i
++) {
3667 line
.replace(i
, newCell
);
3673 * DECSCL - Compatibility level.
3675 private void decscl() {
3676 int i
= getCsiParam(0, 0);
3677 int j
= getCsiParam(1, 0);
3681 currentState
.g0Charset
= CharacterSet
.US
;
3682 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3684 } else if (i
== 62) {
3686 if ((j
== 0) || (j
== 2)) {
3688 } else if (j
== 1) {
3695 * CUD - Cursor down.
3697 private void cud() {
3698 cursorDown(getCsiParam(0, 1, 1, height
), true);
3702 * CUF - Cursor forward.
3704 private void cuf() {
3705 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3709 * CUB - Cursor backward.
3711 private void cub() {
3712 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3718 private void cuu() {
3719 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3723 * CUP - Cursor position.
3725 private void cup() {
3726 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3727 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3731 * CNL - Cursor down and to column 1.
3733 private void cnl() {
3734 cursorDown(getCsiParam(0, 1, 1, height
), true);
3736 cursorLeft(currentState
.cursorX
, true);
3740 * CPL - Cursor up and to column 1.
3742 private void cpl() {
3743 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3745 cursorLeft(currentState
.cursorX
, true);
3749 * CHA - Cursor to column # in current row.
3751 private void cha() {
3752 cursorPosition(currentState
.cursorY
,
3753 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3757 * VPA - Cursor to row #, same column.
3759 private void vpa() {
3760 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3761 currentState
.cursorX
);
3765 * ED - Erase in display.
3768 boolean honorProtected
= false;
3769 boolean decPrivateModeFlag
= false;
3771 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3772 if (collectBuffer
.charAt(i
) == '?') {
3773 decPrivateModeFlag
= true;
3778 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3779 && (decPrivateModeFlag
== true)
3781 honorProtected
= true;
3784 int i
= getCsiParam(0, 0);
3787 // Erase from here to end of screen
3788 if (currentState
.cursorY
< height
- 1) {
3789 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3792 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3793 } else if (i
== 1) {
3794 // Erase from beginning of screen to here
3795 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3797 eraseLine(0, currentState
.cursorX
, honorProtected
);
3798 } else if (i
== 2) {
3799 // Erase entire screen
3800 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3805 * EL - Erase in line.
3808 boolean honorProtected
= false;
3809 boolean decPrivateModeFlag
= false;
3811 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3812 if (collectBuffer
.charAt(i
) == '?') {
3813 decPrivateModeFlag
= true;
3818 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3819 && (decPrivateModeFlag
== true)
3821 honorProtected
= true;
3824 int i
= getCsiParam(0, 0);
3827 // Erase from here to end of line
3828 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3829 } else if (i
== 1) {
3830 // Erase from beginning of line to here
3831 eraseLine(0, currentState
.cursorX
, honorProtected
);
3832 } else if (i
== 2) {
3833 // Erase entire line
3834 eraseLine(0, width
- 1, honorProtected
);
3839 * ECH - Erase # of characters in current row.
3841 private void ech() {
3842 int i
= getCsiParam(0, 1, 1, width
);
3844 // Erase from here to i characters
3845 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3852 int i
= getCsiParam(0, 1);
3854 if ((currentState
.cursorY
>= scrollRegionTop
)
3855 && (currentState
.cursorY
<= scrollRegionBottom
)
3858 // I can get the same effect with a scroll-down
3859 scrollingRegionScrollDown(currentState
.cursorY
,
3860 scrollRegionBottom
, i
);
3865 * DCH - Delete char.
3867 private void dch() {
3868 int n
= getCsiParam(0, 1);
3869 DisplayLine line
= display
.get(currentState
.cursorY
);
3870 Cell blank
= new Cell();
3871 for (int i
= 0; i
< n
; i
++) {
3872 line
.delete(currentState
.cursorX
, blank
);
3877 * ICH - Insert blank char at cursor.
3879 private void ich() {
3880 int n
= getCsiParam(0, 1);
3881 DisplayLine line
= display
.get(currentState
.cursorY
);
3882 Cell blank
= new Cell();
3883 for (int i
= 0; i
< n
; i
++) {
3884 line
.insert(currentState
.cursorX
, blank
);
3892 int i
= getCsiParam(0, 1);
3894 if ((currentState
.cursorY
>= scrollRegionTop
)
3895 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3897 // I can get the same effect with a scroll-down
3898 scrollingRegionScrollUp(currentState
.cursorY
,
3899 scrollRegionBottom
, i
);
3904 * HVP - Horizontal and vertical position.
3906 private void hvp() {
3911 * REP - Repeat character.
3913 private void rep() {
3914 int n
= getCsiParam(0, 1);
3915 for (int i
= 0; i
< n
; i
++) {
3916 printCharacter(repCh
);
3924 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3925 getCsiParam(0, 1, 1, height
));
3932 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3933 getCsiParam(0, 1, 1, height
));
3937 * CBT - Go back X tab stops.
3939 private void cbt() {
3940 int tabsToMove
= getCsiParam(0, 1);
3943 for (int i
= 0; i
< tabsToMove
; i
++) {
3944 int j
= currentState
.cursorX
;
3945 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3946 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3954 j
= tabStops
.get(tabI
);
3956 cursorPosition(currentState
.cursorY
, j
);
3961 * CHT - Advance X tab stops.
3963 private void cht() {
3964 int n
= getCsiParam(0, 1);
3965 for (int i
= 0; i
< n
; i
++) {
3966 advanceToNextTabStop();
3971 * SGR - Select graphics rendition.
3973 private void sgr() {
3975 if (csiParams
.size() == 0) {
3976 currentState
.attr
.reset();
3980 int sgrColorMode
= -1;
3981 boolean idx88Color
= false;
3982 boolean rgbColor
= false;
3986 for (Integer i
: csiParams
) {
3988 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
3990 assert (type
== DeviceType
.XTERM
);
3994 * Indexed color mode, we now have the index number.
3996 if (sgrColorMode
== 38) {
3997 currentState
.attr
.setForeColorRGB(get88Color(i
));
3999 assert (sgrColorMode
== 48);
4000 currentState
.attr
.setBackColorRGB(get88Color(i
));
4009 * RGB color mode, we are collecting tokens.
4013 } else if (rgbGreen
== -1) {
4014 rgbGreen
= i
& 0xFF;
4016 int rgb
= rgbRed
<< 16;
4017 rgb
|= rgbGreen
<< 8;
4020 // System.err.printf("RGB: %08x\n", rgb);
4022 if (sgrColorMode
== 38) {
4023 currentState
.attr
.setForeColorRGB(rgb
);
4025 assert (sgrColorMode
== 48);
4026 currentState
.attr
.setBackColorRGB(rgb
);
4047 * Indexed color mode.
4054 * This is neither indexed nor RGB color. Bail out.
4059 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
4065 currentState
.attr
.reset();
4070 currentState
.attr
.setBold(true);
4075 currentState
.attr
.setUnderline(true);
4080 currentState
.attr
.setBlink(true);
4085 currentState
.attr
.setReverse(true);
4092 if (type
== DeviceType
.XTERM
) {
4102 // Set black foreground
4103 currentState
.attr
.setForeColorRGB(get88Color(8));
4106 // Set red foreground
4107 currentState
.attr
.setForeColorRGB(get88Color(9));
4110 // Set green foreground
4111 currentState
.attr
.setForeColorRGB(get88Color(10));
4114 // Set yellow foreground
4115 currentState
.attr
.setForeColorRGB(get88Color(11));
4118 // Set blue foreground
4119 currentState
.attr
.setForeColorRGB(get88Color(12));
4122 // Set magenta foreground
4123 currentState
.attr
.setForeColorRGB(get88Color(13));
4126 // Set cyan foreground
4127 currentState
.attr
.setForeColorRGB(get88Color(14));
4130 // Set white foreground
4131 currentState
.attr
.setForeColorRGB(get88Color(15));
4135 // Set black background
4136 currentState
.attr
.setBackColorRGB(get88Color(8));
4139 // Set red background
4140 currentState
.attr
.setBackColorRGB(get88Color(9));
4143 // Set green background
4144 currentState
.attr
.setBackColorRGB(get88Color(10));
4147 // Set yellow background
4148 currentState
.attr
.setBackColorRGB(get88Color(11));
4151 // Set blue background
4152 currentState
.attr
.setBackColorRGB(get88Color(12));
4155 // Set magenta background
4156 currentState
.attr
.setBackColorRGB(get88Color(13));
4159 // Set cyan background
4160 currentState
.attr
.setBackColorRGB(get88Color(14));
4163 // Set white background
4164 currentState
.attr
.setBackColorRGB(get88Color(15));
4172 if ((type
== DeviceType
.VT220
)
4173 || (type
== DeviceType
.XTERM
)) {
4179 currentState
.attr
.setBold(false);
4184 currentState
.attr
.setUnderline(false);
4189 currentState
.attr
.setBlink(false);
4194 currentState
.attr
.setReverse(false);
4202 // A true VT100/102/220 does not support color, however everyone
4203 // is used to their terminal emulator supporting color so we will
4204 // unconditionally support color for all DeviceType's.
4209 // Set black foreground
4210 currentState
.attr
.setForeColor(Color
.BLACK
);
4211 currentState
.attr
.setForeColorRGB(-1);
4214 // Set red foreground
4215 currentState
.attr
.setForeColor(Color
.RED
);
4216 currentState
.attr
.setForeColorRGB(-1);
4219 // Set green foreground
4220 currentState
.attr
.setForeColor(Color
.GREEN
);
4221 currentState
.attr
.setForeColorRGB(-1);
4224 // Set yellow foreground
4225 currentState
.attr
.setForeColor(Color
.YELLOW
);
4226 currentState
.attr
.setForeColorRGB(-1);
4229 // Set blue foreground
4230 currentState
.attr
.setForeColor(Color
.BLUE
);
4231 currentState
.attr
.setForeColorRGB(-1);
4234 // Set magenta foreground
4235 currentState
.attr
.setForeColor(Color
.MAGENTA
);
4236 currentState
.attr
.setForeColorRGB(-1);
4239 // Set cyan foreground
4240 currentState
.attr
.setForeColor(Color
.CYAN
);
4241 currentState
.attr
.setForeColorRGB(-1);
4244 // Set white foreground
4245 currentState
.attr
.setForeColor(Color
.WHITE
);
4246 currentState
.attr
.setForeColorRGB(-1);
4249 if (type
== DeviceType
.XTERM
) {
4251 * Xterm supports T.416 / ISO-8613-3 codes to select
4252 * either an indexed color or an RGB value. (It also
4253 * permits these ISO-8613-3 SGR sequences to be separated
4254 * by colons rather than semicolons.)
4256 * We will support only the following:
4258 * 1. Indexed color mode (88- or 256-color modes).
4262 * These cover most of the use cases in the real world.
4264 * HOWEVER, note that this is an awful broken "standard",
4265 * with no way to do it "right". See
4266 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4267 * for a detailed discussion of the current state of RGB
4268 * in various terminals, the point of which is that none
4269 * of them really do the same thing despite all appearing
4273 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4274 * where it is assumed that supporting just the "indexed
4275 * mode" of these sequences (which could align easily
4276 * with existing SGR colors) is assumed to mean full
4277 * support of 24-bit RGB. So it is all or nothing.
4279 * Finally, these sequences break the assumptions of
4280 * standard ECMA-48 style parsers as pointed out at
4281 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4282 * Therefore in order to keep a clean display, we cannot
4283 * parse anything else in this sequence.
4288 // Underscore on, default foreground color
4289 currentState
.attr
.setUnderline(true);
4290 currentState
.attr
.setForeColor(Color
.WHITE
);
4294 // Underscore off, default foreground color
4295 currentState
.attr
.setUnderline(false);
4296 currentState
.attr
.setForeColor(Color
.WHITE
);
4297 currentState
.attr
.setForeColorRGB(-1);
4300 // Set black background
4301 currentState
.attr
.setBackColor(Color
.BLACK
);
4302 currentState
.attr
.setBackColorRGB(-1);
4305 // Set red background
4306 currentState
.attr
.setBackColor(Color
.RED
);
4307 currentState
.attr
.setBackColorRGB(-1);
4310 // Set green background
4311 currentState
.attr
.setBackColor(Color
.GREEN
);
4312 currentState
.attr
.setBackColorRGB(-1);
4315 // Set yellow background
4316 currentState
.attr
.setBackColor(Color
.YELLOW
);
4317 currentState
.attr
.setBackColorRGB(-1);
4320 // Set blue background
4321 currentState
.attr
.setBackColor(Color
.BLUE
);
4322 currentState
.attr
.setBackColorRGB(-1);
4325 // Set magenta background
4326 currentState
.attr
.setBackColor(Color
.MAGENTA
);
4327 currentState
.attr
.setBackColorRGB(-1);
4330 // Set cyan background
4331 currentState
.attr
.setBackColor(Color
.CYAN
);
4332 currentState
.attr
.setBackColorRGB(-1);
4335 // Set white background
4336 currentState
.attr
.setBackColor(Color
.WHITE
);
4337 currentState
.attr
.setBackColorRGB(-1);
4340 if (type
== DeviceType
.XTERM
) {
4342 * Xterm supports T.416 / ISO-8613-3 codes to select
4343 * either an indexed color or an RGB value. (It also
4344 * permits these ISO-8613-3 SGR sequences to be separated
4345 * by colons rather than semicolons.)
4347 * We will support only the following:
4349 * 1. Indexed color mode (88- or 256-color modes).
4353 * These cover most of the use cases in the real world.
4355 * HOWEVER, note that this is an awful broken "standard",
4356 * with no way to do it "right". See
4357 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4358 * for a detailed discussion of the current state of RGB
4359 * in various terminals, the point of which is that none
4360 * of them really do the same thing despite all appearing
4364 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4365 * where it is assumed that supporting just the "indexed
4366 * mode" of these sequences (which could align easily
4367 * with existing SGR colors) is assumed to mean full
4368 * support of 24-bit RGB. So it is all or nothing.
4370 * Finally, these sequences break the assumptions of
4371 * standard ECMA-48 style parsers as pointed out at
4372 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4373 * Therefore in order to keep a clean display, we cannot
4374 * parse anything else in this sequence.
4381 // Default background
4382 currentState
.attr
.setBackColor(Color
.BLACK
);
4383 currentState
.attr
.setBackColorRGB(-1);
4393 * DA - Device attributes.
4396 int extendedFlag
= 0;
4398 if (collectBuffer
.length() > 0) {
4399 String args
= collectBuffer
.substring(1);
4400 if (collectBuffer
.charAt(0) == '>') {
4402 if (collectBuffer
.length() >= 2) {
4403 i
= Integer
.parseInt(args
);
4405 } else if (collectBuffer
.charAt(0) == '=') {
4407 if (collectBuffer
.length() >= 2) {
4408 i
= Integer
.parseInt(args
);
4411 // Unknown code, bail out
4416 if ((i
!= 0) && (i
!= 1)) {
4420 if ((extendedFlag
== 0) && (i
== 0)) {
4421 // Send string directly to remote side
4422 writeRemote(deviceTypeResponse());
4426 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4428 if ((extendedFlag
== 1) && (i
== 0)) {
4430 * Request "What type of terminal are you, what is your
4431 * firmware version, and what hardware options do you have
4434 * Respond: "I am a VT220 (identification code of 1), my
4435 * firmware version is _____ (Pv), and I have _____ Po
4436 * options installed."
4442 if (s8c1t
== true) {
4443 writeRemote("\u009b>1;10;0c");
4445 writeRemote("\033[>1;10;0c");
4451 if ((extendedFlag
== 2) && (i
== 0)) {
4454 * Request "What is your unit ID?"
4456 * Respond: "I was manufactured at site 00 and have a unique ID
4460 writeRemote("\033P!|00010203\033\\");
4465 * DECSTBM - Set top and bottom margins.
4467 private void decstbm() {
4468 boolean decPrivateModeFlag
= false;
4470 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4471 if (collectBuffer
.charAt(i
) == '?') {
4472 decPrivateModeFlag
= true;
4476 if (decPrivateModeFlag
) {
4477 // This could be restore DEC private mode values.
4481 int top
= getCsiParam(0, 1, 1, height
) - 1;
4482 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4483 if (bottom
> height
- 1) {
4484 bottom
= height
- 1;
4490 scrollRegionTop
= top
;
4491 scrollRegionBottom
= bottom
;
4494 cursorPosition(0, 0);
4499 * DECREQTPARM - Request terminal parameters.
4501 private void decreqtparm() {
4502 int i
= getCsiParam(0, 0);
4504 if ((i
!= 0) && (i
!= 1)) {
4511 * Request terminal parameters.
4515 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4516 * (CLoCk MULtiplier = 1, STP option flags = 0)
4520 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4523 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4525 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4531 * DECSCA - Select Character Attributes.
4533 private void decsca() {
4534 int i
= getCsiParam(0, 0);
4536 if ((i
== 0) || (i
== 2)) {
4538 currentState
.attr
.setProtect(false);
4542 currentState
.attr
.setProtect(true);
4547 * DECSTR - Soft Terminal Reset.
4549 private void decstr() {
4550 // Do exactly like RIS - Reset to initial state
4552 // Do I clear screen too? I think so...
4553 eraseScreen(0, 0, height
- 1, width
- 1, false);
4554 cursorPosition(0, 0);
4558 * DSR - Device status report.
4560 private void dsr() {
4561 boolean decPrivateModeFlag
= false;
4562 int row
= currentState
.cursorY
;
4564 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4565 if (collectBuffer
.charAt(i
) == '?') {
4566 decPrivateModeFlag
= true;
4571 int i
= getCsiParam(0, 0);
4576 // Request status report. Respond with "OK, no malfunction."
4578 // Send string directly to remote side
4579 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4582 writeRemote("\u009b0n");
4584 writeRemote("\033[0n");
4589 // Request cursor position. Respond with current position.
4590 if (currentState
.originMode
== true) {
4591 row
-= scrollRegionTop
;
4594 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4597 str
= String
.format("\u009b%d;%dR", row
+ 1,
4598 currentState
.cursorX
+ 1);
4600 str
= String
.format("\033[%d;%dR", row
+ 1,
4601 currentState
.cursorX
+ 1);
4604 // Send string directly to remote side
4609 if (decPrivateModeFlag
== true) {
4611 // Request printer status report. Respond with "Printer not
4614 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4615 && (s8c1t
== true)) {
4616 writeRemote("\u009b?13n");
4618 writeRemote("\033[?13n");
4624 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4625 && (decPrivateModeFlag
== true)
4628 // Request user-defined keys are locked or unlocked. Respond
4629 // with "User-defined keys are locked."
4631 if (s8c1t
== true) {
4632 writeRemote("\u009b?21n");
4634 writeRemote("\033[?21n");
4640 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4641 && (decPrivateModeFlag
== true)
4644 // Request keyboard language. Respond with "Keyboard
4645 // language is North American."
4647 if (s8c1t
== true) {
4648 writeRemote("\u009b?27;1n");
4650 writeRemote("\033[?27;1n");
4657 // Some other option, ignore
4663 * TBC - Tabulation clear.
4665 private void tbc() {
4666 int i
= getCsiParam(0, 0);
4668 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4669 for (Integer stop
: tabStops
) {
4670 if (stop
== currentState
.cursorX
) {
4675 tabStops
= newStops
;
4683 * Erase the characters in the current line from the start column to the
4684 * end column, inclusive.
4686 * @param start starting column to erase (between 0 and width - 1)
4687 * @param end ending column to erase (between 0 and width - 1)
4688 * @param honorProtected if true, do not erase characters with the
4689 * protected attribute set
4691 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4696 if (end
> width
- 1) {
4703 for (int i
= start
; i
<= end
; i
++) {
4704 DisplayLine line
= display
.get(currentState
.cursorY
);
4705 if ((!honorProtected
)
4706 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4713 * From the VT102 manual:
4715 * Erasing a character also erases any character
4716 * attribute of the character.
4722 * Erase with the current color a.k.a. back-color erase
4725 line
.setChar(i
, ' ');
4726 line
.setAttr(i
, currentState
.attr
);
4734 * Erase a rectangular section of the screen, inclusive. end column,
4737 * @param startRow starting row to erase (between 0 and height - 1)
4738 * @param startCol starting column to erase (between 0 and width - 1)
4739 * @param endRow ending row to erase (between 0 and height - 1)
4740 * @param endCol ending column to erase (between 0 and width - 1)
4741 * @param honorProtected if true, do not erase characters with the
4742 * protected attribute set
4744 private void eraseScreen(final int startRow
, final int startCol
,
4745 final int endRow
, final int endCol
, final boolean honorProtected
) {
4753 || (endRow
< startRow
)
4754 || (endCol
< startCol
)
4759 oldCursorY
= currentState
.cursorY
;
4760 for (int i
= startRow
; i
<= endRow
; i
++) {
4761 currentState
.cursorY
= i
;
4762 eraseLine(startCol
, endCol
, honorProtected
);
4764 // Erase display clears the double attributes
4765 display
.get(i
).setDoubleWidth(false);
4766 display
.get(i
).setDoubleHeight(0);
4768 currentState
.cursorY
= oldCursorY
;
4772 * VT220 printer functions. All of these are parsed, but won't do
4775 private void printerFunctions() {
4776 boolean decPrivateModeFlag
= false;
4777 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4778 if (collectBuffer
.charAt(i
) == '?') {
4779 decPrivateModeFlag
= true;
4784 int i
= getCsiParam(0, 0);
4789 if (decPrivateModeFlag
== false) {
4795 if (decPrivateModeFlag
== true) {
4796 // Print cursor line
4801 if (decPrivateModeFlag
== true) {
4802 // Auto print mode OFF
4804 // Printer controller OFF
4806 // Characters re-appear on the screen
4807 printerControllerMode
= false;
4812 if (decPrivateModeFlag
== true) {
4816 // Printer controller
4818 // Characters get sucked into oblivion
4819 printerControllerMode
= true;
4830 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4831 * of remote systems will send an XTerm title sequence even if TERM isn't
4834 * @param xtermChar the character received from the remote side
4836 private void oscPut(final char xtermChar
) {
4837 // System.err.println("oscPut: " + xtermChar);
4839 boolean oscEnd
= false;
4841 if (xtermChar
== 0x07) {
4844 if ((xtermChar
== '\\')
4845 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\033')
4851 collectBuffer
.append(xtermChar
);
4856 if (xtermChar
== 0x07) {
4857 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
4859 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4862 String
[] p
= args
.split(";");
4864 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4871 if (p
[0].equals("4")) {
4872 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
4873 // Set a color index value
4875 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
4876 } catch (NumberFormatException e
) {
4882 if (p
[0].equals("10")) {
4883 if (p
[1].equals("?")) {
4884 // Respond with foreground color.
4885 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToForegroundColor(currentState
.attr
);
4887 writeRemote(String
.format(
4888 "\033]10;rgb:%04x/%04x/%04x\033\\",
4889 color
.getRed() << 8,
4890 color
.getGreen() << 8,
4891 color
.getBlue() << 8));
4895 if (p
[0].equals("11")) {
4896 if (p
[1].equals("?")) {
4897 // Respond with background color.
4898 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToBackgroundColor(currentState
.attr
);
4900 writeRemote(String
.format(
4901 "\033]11;rgb:%04x/%04x/%04x\033\\",
4902 color
.getRed() << 8,
4903 color
.getGreen() << 8,
4904 color
.getBlue() << 8));
4908 if (p
[0].equals("444")) {
4909 if (p
[1].equals("0") && (p
.length
== 6)) {
4910 // Jexer image - RGB
4911 parseJexerImageRGB(p
[2], p
[3], p
[4], p
[5]);
4912 } else if (p
[1].equals("1") && (p
.length
== 4)) {
4913 // Jexer image - PNG
4914 parseJexerImageFile(1, p
[2], p
[3]);
4915 } else if (p
[1].equals("2") && (p
.length
== 4)) {
4916 // Jexer image - JPG
4917 parseJexerImageFile(2, p
[2], p
[3]);
4922 // Go to SCAN_GROUND state
4929 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
4930 * Jexer ECMA48Terminal to talk to ECMA48.
4932 * @param pmChar the character received from the remote side
4934 private void pmPut(final char pmChar
) {
4935 // System.err.println("pmPut: " + pmChar);
4937 boolean pmEnd
= false;
4939 if ((pmChar
== '\\')
4940 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\033')
4946 collectBuffer
.append(pmChar
);
4951 arg
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4953 // System.err.println("arg: '" + arg + "'");
4955 if (arg
.equals("hideMousePointer")) {
4956 hideMousePointer
= true;
4958 if (arg
.equals("showMousePointer")) {
4959 hideMousePointer
= false;
4962 // Go to SCAN_GROUND state
4969 * Perform xterm window operations.
4971 private void xtermWindowOps() {
4972 boolean xtermPrivateModeFlag
= false;
4974 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4975 if (collectBuffer
.charAt(i
) == '?') {
4976 xtermPrivateModeFlag
= true;
4981 int i
= getCsiParam(0, 0);
4983 if (!xtermPrivateModeFlag
) {
4986 // Report xterm text area size in pixels as CSI 4 ; height ;
4988 writeRemote(String
.format("\033[4;%d;%dt", textHeight
* height
,
4989 textWidth
* width
));
4992 // Report character size in pixels as CSI 6 ; height ; width
4994 writeRemote(String
.format("\033[6;%d;%dt", textHeight
,
4998 // Report the text are size in characters as CSI 8 ; height ;
5000 writeRemote(String
.format("\033[8;%d;%dt", height
, width
));
5009 * Respond to xterm sixel query.
5011 private void xtermSixelQuery() {
5012 int item
= getCsiParam(0, 0);
5013 int action
= getCsiParam(1, 0);
5014 int value
= getCsiParam(2, 0);
5019 // Report number of color registers.
5020 writeRemote(String
.format("\033[?%d;%d;%dS", item
, 0, 1024));
5027 // We will not support this option.
5028 writeRemote(String
.format("\033[?%d;%dS", item
, action
));
5032 * Run this input character through the ECMA48 state machine.
5034 * @param ch character from the remote side
5036 private void consume(final int ch
) {
5040 // System.err.printf("%c STATE = %s\n", ch, scanState);
5042 // Special "anywhere" states
5044 // 18, 1A --> execute, then switch to SCAN_GROUND
5045 if ((ch
== 0x18) || (ch
== 0x1A)) {
5046 // CAN and SUB abort escape sequences
5051 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
5055 if ((type
== DeviceType
.XTERM
)
5056 && ((scanState
== ScanState
.OSC_STRING
)
5057 || (scanState
== ScanState
.DCS_SIXEL
)
5058 || (scanState
== ScanState
.SOSPMAPC_STRING
))
5060 // Xterm can pass ESCAPE to its OSC sequence.
5061 // Xterm can pass ESCAPE to its DCS sequence.
5062 // Jexer can pass ESCAPE to its PM sequence.
5063 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
5064 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
5065 && (scanState
!= ScanState
.DCS_IGNORE
)
5066 && (scanState
!= ScanState
.DCS_PARAM
)
5067 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
5069 scanState
= ScanState
.ESCAPE
;
5074 // 0x9B == CSI 8-bit sequence
5076 scanState
= ScanState
.CSI_ENTRY
;
5080 // 0x9D goes to ScanState.OSC_STRING
5082 scanState
= ScanState
.OSC_STRING
;
5086 // 0x90 goes to DCS_ENTRY
5088 scanState
= ScanState
.DCS_ENTRY
;
5092 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
5093 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
5094 scanState
= ScanState
.SOSPMAPC_STRING
;
5098 // 0x7F (DEL) is always discarded
5103 switch (scanState
) {
5106 // 00-17, 19, 1C-1F --> execute
5107 // 80-8F, 91-9A, 9C --> execute
5108 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
5109 handleControlChar((char) ch
);
5113 if (((ch
>= 0x20) && (ch
<= 0x7F))
5117 // VT220 printer --> trash bin
5118 if (((type
== DeviceType
.VT220
)
5119 || (type
== DeviceType
.XTERM
))
5120 && (printerControllerMode
== true)
5125 // Hang onto this character
5126 repCh
= mapCharacter(ch
);
5128 // Print this character
5129 printCharacter(repCh
);
5134 // 00-17, 19, 1C-1F --> execute
5136 handleControlChar((char) ch
);
5140 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
5141 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5143 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
5147 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
5148 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
5159 // DECSC - Save cursor
5160 // Note this code overlaps both ANSI and VT52 mode
5165 // DECRC - Restore cursor
5166 // Note this code overlaps both ANSI and VT52 mode
5175 if (vt52Mode
== true) {
5176 // DECANM - Enter ANSI mode
5178 arrowKeyMode
= ArrowKeyMode
.VT100
;
5181 * From the VT102 docs: "You use ANSI mode to select
5182 * most terminal features; the terminal uses the same
5183 * features when it switches to VT52 mode. You
5184 * cannot, however, change most of these features in
5187 * In other words, do not reset any other attributes
5188 * when switching between VT52 submode and ANSI.
5192 currentState
.g0Charset
= CharacterSet
.US
;
5193 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5195 singleshift
= Singleshift
.NONE
;
5196 currentState
.glLockshift
= LockshiftMode
.NONE
;
5197 currentState
.grLockshift
= LockshiftMode
.NONE
;
5201 // DECKPAM - Keypad application mode
5202 // Note this code overlaps both ANSI and VT52 mode
5206 // DECKPNM - Keypad numeric mode
5207 // Note this code overlaps both ANSI and VT52 mode
5214 if (vt52Mode
== true) {
5215 // Cursor up, and stop at the top without scrolling
5220 if (vt52Mode
== true) {
5221 // Cursor down, and stop at the bottom without scrolling
5222 cursorDown(1, false);
5226 if (vt52Mode
== true) {
5227 // Cursor right, and stop at the right without scrolling
5228 cursorRight(1, false);
5232 if (vt52Mode
== true) {
5233 // Cursor left, and stop at the left without scrolling
5234 cursorLeft(1, false);
5241 if (vt52Mode
== true) {
5249 if (vt52Mode
== true) {
5250 // G0 --> Special graphics
5251 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
5255 if (vt52Mode
== true) {
5257 currentState
.g0Charset
= CharacterSet
.US
;
5261 if (vt52Mode
== true) {
5263 cursorPosition(0, 0);
5265 // HTS - Horizontal tabulation set
5270 if (vt52Mode
== true) {
5271 // Reverse line feed. Same as RI.
5276 if (vt52Mode
== true) {
5277 // Erase to end of screen
5278 eraseLine(currentState
.cursorX
, width
- 1, false);
5279 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
5284 if (vt52Mode
== true) {
5285 // Erase to end of line
5286 eraseLine(currentState
.cursorX
, width
- 1, false);
5292 if (vt52Mode
== true) {
5295 // RI - Reverse index
5300 if (vt52Mode
== false) {
5302 singleshift
= Singleshift
.SS2
;
5306 if (vt52Mode
== false) {
5308 singleshift
= Singleshift
.SS3
;
5315 if ((ch
>= 0x51) && (ch
<= 0x57)) {
5331 if (vt52Mode
== true) {
5332 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
5340 if (vt52Mode
== true) {
5342 // Send string directly to remote side
5343 writeRemote("\033/Z");
5346 // Send string directly to remote side
5347 writeRemote(deviceTypeResponse());
5358 // VT52 cannot get to any of these other states
5359 if (vt52Mode
== true) {
5364 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
5371 // RIS - Reset to initial state
5373 // Do I clear screen too? I think so...
5374 eraseScreen(0, 0, height
- 1, width
- 1, false);
5375 cursorPosition(0, 0);
5389 if ((type
== DeviceType
.VT220
)
5390 || (type
== DeviceType
.XTERM
)) {
5392 // VT220 lockshift G2 into GL
5393 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
5398 if ((type
== DeviceType
.VT220
)
5399 || (type
== DeviceType
.XTERM
)) {
5401 // VT220 lockshift G3 into GL
5402 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
5420 if ((type
== DeviceType
.VT220
)
5421 || (type
== DeviceType
.XTERM
)) {
5423 // VT220 lockshift G3 into GR
5424 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
5429 if ((type
== DeviceType
.VT220
)
5430 || (type
== DeviceType
.XTERM
)) {
5432 // VT220 lockshift G2 into GR
5433 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
5439 if ((type
== DeviceType
.VT220
)
5440 || (type
== DeviceType
.XTERM
)) {
5442 // VT220 lockshift G1 into GR
5443 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
5453 // 0x5B goes to CSI_ENTRY
5455 scanState
= ScanState
.CSI_ENTRY
;
5458 // 0x5D goes to OSC_STRING
5460 scanState
= ScanState
.OSC_STRING
;
5463 // 0x50 goes to DCS_ENTRY
5465 scanState
= ScanState
.DCS_ENTRY
;
5468 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5469 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
5470 scanState
= ScanState
.SOSPMAPC_STRING
;
5475 case ESCAPE_INTERMEDIATE
:
5476 // 00-17, 19, 1C-1F --> execute
5478 handleControlChar((char) ch
);
5481 // 20-2F --> collect
5482 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5486 // 30-7E --> dispatch, then switch to GROUND
5487 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
5490 if ((collectBuffer
.length() == 1)
5491 && (collectBuffer
.charAt(0) == '(')) {
5492 // G0 --> Special graphics
5493 currentState
.g0Charset
= CharacterSet
.DRAWING
;
5495 if ((collectBuffer
.length() == 1)
5496 && (collectBuffer
.charAt(0) == ')')) {
5497 // G1 --> Special graphics
5498 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5500 if ((type
== DeviceType
.VT220
)
5501 || (type
== DeviceType
.XTERM
)) {
5503 if ((collectBuffer
.length() == 1)
5504 && (collectBuffer
.charAt(0) == '*')) {
5505 // G2 --> Special graphics
5506 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5508 if ((collectBuffer
.length() == 1)
5509 && (collectBuffer
.charAt(0) == '+')) {
5510 // G3 --> Special graphics
5511 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5516 if ((collectBuffer
.length() == 1)
5517 && (collectBuffer
.charAt(0) == '(')) {
5518 // G0 --> Alternate character ROM standard character set
5519 currentState
.g0Charset
= CharacterSet
.ROM
;
5521 if ((collectBuffer
.length() == 1)
5522 && (collectBuffer
.charAt(0) == ')')) {
5523 // G1 --> Alternate character ROM standard character set
5524 currentState
.g1Charset
= CharacterSet
.ROM
;
5528 if ((collectBuffer
.length() == 1)
5529 && (collectBuffer
.charAt(0) == '(')) {
5530 // G0 --> Alternate character ROM special graphics
5531 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5533 if ((collectBuffer
.length() == 1)
5534 && (collectBuffer
.charAt(0) == ')')) {
5535 // G1 --> Alternate character ROM special graphics
5536 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5540 if ((collectBuffer
.length() == 1)
5541 && (collectBuffer
.charAt(0) == '#')) {
5542 // DECDHL - Double-height line (top half)
5547 if ((collectBuffer
.length() == 1)
5548 && (collectBuffer
.charAt(0) == '#')) {
5549 // DECDHL - Double-height line (bottom half)
5552 if ((type
== DeviceType
.VT220
)
5553 || (type
== DeviceType
.XTERM
)) {
5555 if ((collectBuffer
.length() == 1)
5556 && (collectBuffer
.charAt(0) == '(')) {
5558 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5560 if ((collectBuffer
.length() == 1)
5561 && (collectBuffer
.charAt(0) == ')')) {
5563 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5565 if ((collectBuffer
.length() == 1)
5566 && (collectBuffer
.charAt(0) == '*')) {
5568 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5570 if ((collectBuffer
.length() == 1)
5571 && (collectBuffer
.charAt(0) == '+')) {
5573 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5578 if ((collectBuffer
.length() == 1)
5579 && (collectBuffer
.charAt(0) == '#')) {
5580 // DECSWL - Single-width line
5583 if ((type
== DeviceType
.VT220
)
5584 || (type
== DeviceType
.XTERM
)) {
5586 if ((collectBuffer
.length() == 1)
5587 && (collectBuffer
.charAt(0) == '(')) {
5589 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5591 if ((collectBuffer
.length() == 1)
5592 && (collectBuffer
.charAt(0) == ')')) {
5594 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5596 if ((collectBuffer
.length() == 1)
5597 && (collectBuffer
.charAt(0) == '*')) {
5599 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5601 if ((collectBuffer
.length() == 1)
5602 && (collectBuffer
.charAt(0) == '+')) {
5604 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5609 if ((collectBuffer
.length() == 1)
5610 && (collectBuffer
.charAt(0) == '#')) {
5611 // DECDWL - Double-width line
5614 if ((type
== DeviceType
.VT220
)
5615 || (type
== DeviceType
.XTERM
)) {
5617 if ((collectBuffer
.length() == 1)
5618 && (collectBuffer
.charAt(0) == '(')) {
5620 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5622 if ((collectBuffer
.length() == 1)
5623 && (collectBuffer
.charAt(0) == ')')) {
5625 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5627 if ((collectBuffer
.length() == 1)
5628 && (collectBuffer
.charAt(0) == '*')) {
5630 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5632 if ((collectBuffer
.length() == 1)
5633 && (collectBuffer
.charAt(0) == '+')) {
5635 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5640 if ((type
== DeviceType
.VT220
)
5641 || (type
== DeviceType
.XTERM
)) {
5643 if ((collectBuffer
.length() == 1)
5644 && (collectBuffer
.charAt(0) == '(')) {
5646 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5648 if ((collectBuffer
.length() == 1)
5649 && (collectBuffer
.charAt(0) == ')')) {
5651 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5653 if ((collectBuffer
.length() == 1)
5654 && (collectBuffer
.charAt(0) == '*')) {
5656 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5658 if ((collectBuffer
.length() == 1)
5659 && (collectBuffer
.charAt(0) == '+')) {
5661 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5666 if ((collectBuffer
.length() == 1)
5667 && (collectBuffer
.charAt(0) == '#')) {
5668 // DECALN - Screen alignment display
5677 if ((type
== DeviceType
.VT220
)
5678 || (type
== DeviceType
.XTERM
)) {
5680 if ((collectBuffer
.length() == 1)
5681 && (collectBuffer
.charAt(0) == '(')) {
5682 // G0 --> DEC_SUPPLEMENTAL
5683 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5685 if ((collectBuffer
.length() == 1)
5686 && (collectBuffer
.charAt(0) == ')')) {
5687 // G1 --> DEC_SUPPLEMENTAL
5688 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5690 if ((collectBuffer
.length() == 1)
5691 && (collectBuffer
.charAt(0) == '*')) {
5692 // G2 --> DEC_SUPPLEMENTAL
5693 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5695 if ((collectBuffer
.length() == 1)
5696 && (collectBuffer
.charAt(0) == '+')) {
5697 // G3 --> DEC_SUPPLEMENTAL
5698 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5703 if ((type
== DeviceType
.VT220
)
5704 || (type
== DeviceType
.XTERM
)) {
5706 if ((collectBuffer
.length() == 1)
5707 && (collectBuffer
.charAt(0) == '(')) {
5709 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5711 if ((collectBuffer
.length() == 1)
5712 && (collectBuffer
.charAt(0) == ')')) {
5714 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5716 if ((collectBuffer
.length() == 1)
5717 && (collectBuffer
.charAt(0) == '*')) {
5719 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5721 if ((collectBuffer
.length() == 1)
5722 && (collectBuffer
.charAt(0) == '+')) {
5724 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5733 if ((collectBuffer
.length() == 1)
5734 && (collectBuffer
.charAt(0) == '(')) {
5735 // G0 --> United Kingdom set
5736 currentState
.g0Charset
= CharacterSet
.UK
;
5738 if ((collectBuffer
.length() == 1)
5739 && (collectBuffer
.charAt(0) == ')')) {
5740 // G1 --> United Kingdom set
5741 currentState
.g1Charset
= CharacterSet
.UK
;
5743 if ((type
== DeviceType
.VT220
)
5744 || (type
== DeviceType
.XTERM
)) {
5746 if ((collectBuffer
.length() == 1)
5747 && (collectBuffer
.charAt(0) == '*')) {
5748 // G2 --> United Kingdom set
5749 currentState
.g2Charset
= CharacterSet
.UK
;
5751 if ((collectBuffer
.length() == 1)
5752 && (collectBuffer
.charAt(0) == '+')) {
5753 // G3 --> United Kingdom set
5754 currentState
.g3Charset
= CharacterSet
.UK
;
5759 if ((collectBuffer
.length() == 1)
5760 && (collectBuffer
.charAt(0) == '(')) {
5762 currentState
.g0Charset
= CharacterSet
.US
;
5764 if ((collectBuffer
.length() == 1)
5765 && (collectBuffer
.charAt(0) == ')')) {
5767 currentState
.g1Charset
= CharacterSet
.US
;
5769 if ((type
== DeviceType
.VT220
)
5770 || (type
== DeviceType
.XTERM
)) {
5772 if ((collectBuffer
.length() == 1)
5773 && (collectBuffer
.charAt(0) == '*')) {
5775 currentState
.g2Charset
= CharacterSet
.US
;
5777 if ((collectBuffer
.length() == 1)
5778 && (collectBuffer
.charAt(0) == '+')) {
5780 currentState
.g3Charset
= CharacterSet
.US
;
5785 if ((type
== DeviceType
.VT220
)
5786 || (type
== DeviceType
.XTERM
)) {
5788 if ((collectBuffer
.length() == 1)
5789 && (collectBuffer
.charAt(0) == '(')) {
5791 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5793 if ((collectBuffer
.length() == 1)
5794 && (collectBuffer
.charAt(0) == ')')) {
5796 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5798 if ((collectBuffer
.length() == 1)
5799 && (collectBuffer
.charAt(0) == '*')) {
5801 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5803 if ((collectBuffer
.length() == 1)
5804 && (collectBuffer
.charAt(0) == '+')) {
5806 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5813 if ((type
== DeviceType
.VT220
)
5814 || (type
== DeviceType
.XTERM
)) {
5816 if ((collectBuffer
.length() == 1)
5817 && (collectBuffer
.charAt(0) == '(')) {
5819 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5821 if ((collectBuffer
.length() == 1)
5822 && (collectBuffer
.charAt(0) == ')')) {
5824 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5826 if ((collectBuffer
.length() == 1)
5827 && (collectBuffer
.charAt(0) == '*')) {
5829 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5831 if ((collectBuffer
.length() == 1)
5832 && (collectBuffer
.charAt(0) == '+')) {
5834 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5839 if ((type
== DeviceType
.VT220
)
5840 || (type
== DeviceType
.XTERM
)) {
5842 if ((collectBuffer
.length() == 1)
5843 && (collectBuffer
.charAt(0) == ' ')) {
5850 if ((type
== DeviceType
.VT220
)
5851 || (type
== DeviceType
.XTERM
)) {
5853 if ((collectBuffer
.length() == 1)
5854 && (collectBuffer
.charAt(0) == ' ')) {
5861 if ((type
== DeviceType
.VT220
)
5862 || (type
== DeviceType
.XTERM
)) {
5864 if ((collectBuffer
.length() == 1)
5865 && (collectBuffer
.charAt(0) == '(')) {
5867 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5869 if ((collectBuffer
.length() == 1)
5870 && (collectBuffer
.charAt(0) == ')')) {
5872 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5874 if ((collectBuffer
.length() == 1)
5875 && (collectBuffer
.charAt(0) == '*')) {
5877 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5879 if ((collectBuffer
.length() == 1)
5880 && (collectBuffer
.charAt(0) == '+')) {
5882 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5890 if ((type
== DeviceType
.VT220
)
5891 || (type
== DeviceType
.XTERM
)) {
5893 if ((collectBuffer
.length() == 1)
5894 && (collectBuffer
.charAt(0) == '(')) {
5896 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5898 if ((collectBuffer
.length() == 1)
5899 && (collectBuffer
.charAt(0) == ')')) {
5901 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5903 if ((collectBuffer
.length() == 1)
5904 && (collectBuffer
.charAt(0) == '*')) {
5906 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5908 if ((collectBuffer
.length() == 1)
5909 && (collectBuffer
.charAt(0) == '+')) {
5911 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5922 if ((type
== DeviceType
.VT220
)
5923 || (type
== DeviceType
.XTERM
)) {
5925 if ((collectBuffer
.length() == 1)
5926 && (collectBuffer
.charAt(0) == '(')) {
5928 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5930 if ((collectBuffer
.length() == 1)
5931 && (collectBuffer
.charAt(0) == ')')) {
5933 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5935 if ((collectBuffer
.length() == 1)
5936 && (collectBuffer
.charAt(0) == '*')) {
5938 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5940 if ((collectBuffer
.length() == 1)
5941 && (collectBuffer
.charAt(0) == '+')) {
5943 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5948 if ((type
== DeviceType
.VT220
)
5949 || (type
== DeviceType
.XTERM
)) {
5951 if ((collectBuffer
.length() == 1)
5952 && (collectBuffer
.charAt(0) == '(')) {
5954 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5956 if ((collectBuffer
.length() == 1)
5957 && (collectBuffer
.charAt(0) == ')')) {
5959 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5961 if ((collectBuffer
.length() == 1)
5962 && (collectBuffer
.charAt(0) == '*')) {
5964 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5966 if ((collectBuffer
.length() == 1)
5967 && (collectBuffer
.charAt(0) == '+')) {
5969 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5981 if ((type
== DeviceType
.VT220
)
5982 || (type
== DeviceType
.XTERM
)) {
5984 if ((collectBuffer
.length() == 1)
5985 && (collectBuffer
.charAt(0) == '(')) {
5987 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5989 if ((collectBuffer
.length() == 1)
5990 && (collectBuffer
.charAt(0) == ')')) {
5992 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5994 if ((collectBuffer
.length() == 1)
5995 && (collectBuffer
.charAt(0) == '*')) {
5997 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5999 if ((collectBuffer
.length() == 1)
6000 && (collectBuffer
.charAt(0) == '+')) {
6002 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
6007 if ((type
== DeviceType
.VT220
)
6008 || (type
== DeviceType
.XTERM
)) {
6010 if ((collectBuffer
.length() == 1)
6011 && (collectBuffer
.charAt(0) == '(')) {
6013 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
6015 if ((collectBuffer
.length() == 1)
6016 && (collectBuffer
.charAt(0) == ')')) {
6018 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
6020 if ((collectBuffer
.length() == 1)
6021 && (collectBuffer
.charAt(0) == '*')) {
6023 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
6025 if ((collectBuffer
.length() == 1)
6026 && (collectBuffer
.charAt(0) == '+')) {
6028 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
6075 // 0x9C goes to GROUND
6083 // 00-17, 19, 1C-1F --> execute
6085 handleControlChar((char) ch
);
6088 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6089 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6091 scanState
= ScanState
.CSI_INTERMEDIATE
;
6094 // 30-39, 3B --> param, then switch to CSI_PARAM
6095 if ((ch
>= '0') && (ch
<= '9')) {
6097 scanState
= ScanState
.CSI_PARAM
;
6101 scanState
= ScanState
.CSI_PARAM
;
6104 // 3C-3F --> collect, then switch to CSI_PARAM
6105 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6107 scanState
= ScanState
.CSI_PARAM
;
6110 // 40-7E --> dispatch, then switch to GROUND
6111 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6114 // ICH - Insert character
6122 // CUD - Cursor down
6126 // CUF - Cursor forward
6130 // CUB - Cursor backward
6134 // CNL - Cursor down and to column 1
6135 if (type
== DeviceType
.XTERM
) {
6140 // CPL - Cursor up and to column 1
6141 if (type
== DeviceType
.XTERM
) {
6146 // CHA - Cursor to column # in current row
6147 if (type
== DeviceType
.XTERM
) {
6152 // CUP - Cursor position
6156 // CHT - Cursor forward X tab stops (default 1)
6157 if (type
== DeviceType
.XTERM
) {
6162 // ED - Erase in display
6166 // EL - Erase in line
6181 // DCH - Delete character
6188 // Scroll up X lines (default 1)
6189 if (type
== DeviceType
.XTERM
) {
6190 boolean xtermPrivateModeFlag
= false;
6191 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6192 if (collectBuffer
.charAt(i
) == '?') {
6193 xtermPrivateModeFlag
= true;
6197 if (xtermPrivateModeFlag
) {
6205 // Scroll down X lines (default 1)
6206 if (type
== DeviceType
.XTERM
) {
6215 if ((type
== DeviceType
.VT220
)
6216 || (type
== DeviceType
.XTERM
)) {
6218 // ECH - Erase character
6225 // CBT - Cursor backward X tab stops (default 1)
6226 if (type
== DeviceType
.XTERM
) {
6237 // HPA - Cursor to column # in current row. Same as CHA
6238 if (type
== DeviceType
.XTERM
) {
6243 // HPR - Cursor right. Same as CUF
6244 if (type
== DeviceType
.XTERM
) {
6249 // REP - Repeat last char X times
6250 if (type
== DeviceType
.XTERM
) {
6255 // DA - Device attributes
6259 // VPA - Cursor to row, current column.
6260 if (type
== DeviceType
.XTERM
) {
6265 // VPR - Cursor down. Same as CUD
6266 if (type
== DeviceType
.XTERM
) {
6271 // HVP - Horizontal and vertical position
6275 // TBC - Tabulation clear
6279 // Sets an ANSI or DEC private toggle
6283 if ((type
== DeviceType
.VT220
)
6284 || (type
== DeviceType
.XTERM
)) {
6286 // Printer functions
6294 // Sets an ANSI or DEC private toggle
6298 // SGR - Select graphics rendition
6302 // DSR - Device status report
6309 // DECLL - Load leds
6313 // DECSTBM - Set top and bottom margins
6317 // Save cursor (ANSI.SYS)
6318 if (type
== DeviceType
.XTERM
) {
6319 savedState
.cursorX
= currentState
.cursorX
;
6320 savedState
.cursorY
= currentState
.cursorY
;
6324 if (type
== DeviceType
.XTERM
) {
6325 // Window operations
6330 // Restore cursor (ANSI.SYS)
6331 if (type
== DeviceType
.XTERM
) {
6332 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
6339 // DECREQTPARM - Request terminal parameters
6355 // 0x9C goes to GROUND
6360 // 0x3A goes to CSI_IGNORE
6362 scanState
= ScanState
.CSI_IGNORE
;
6367 // 00-17, 19, 1C-1F --> execute
6369 handleControlChar((char) ch
);
6372 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6373 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6375 scanState
= ScanState
.CSI_INTERMEDIATE
;
6378 // 30-39, 3B --> param
6379 if ((ch
>= '0') && (ch
<= '9')) {
6386 // 0x3A goes to CSI_IGNORE
6388 scanState
= ScanState
.CSI_IGNORE
;
6390 // 0x3C-3F goes to CSI_IGNORE
6391 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6392 scanState
= ScanState
.CSI_IGNORE
;
6395 // 40-7E --> dispatch, then switch to GROUND
6396 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6399 // ICH - Insert character
6407 // CUD - Cursor down
6411 // CUF - Cursor forward
6415 // CUB - Cursor backward
6419 // CNL - Cursor down and to column 1
6420 if (type
== DeviceType
.XTERM
) {
6425 // CPL - Cursor up and to column 1
6426 if (type
== DeviceType
.XTERM
) {
6431 // CHA - Cursor to column # in current row
6432 if (type
== DeviceType
.XTERM
) {
6437 // CUP - Cursor position
6441 // CHT - Cursor forward X tab stops (default 1)
6442 if (type
== DeviceType
.XTERM
) {
6447 // ED - Erase in display
6451 // EL - Erase in line
6466 // DCH - Delete character
6473 // Scroll up X lines (default 1)
6474 if (type
== DeviceType
.XTERM
) {
6475 boolean xtermPrivateModeFlag
= false;
6476 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6477 if (collectBuffer
.charAt(i
) == '?') {
6478 xtermPrivateModeFlag
= true;
6482 if (xtermPrivateModeFlag
) {
6490 // Scroll down X lines (default 1)
6491 if (type
== DeviceType
.XTERM
) {
6500 if ((type
== DeviceType
.VT220
)
6501 || (type
== DeviceType
.XTERM
)) {
6503 // ECH - Erase character
6510 // CBT - Cursor backward X tab stops (default 1)
6511 if (type
== DeviceType
.XTERM
) {
6522 // HPA - Cursor to column # in current row. Same as CHA
6523 if (type
== DeviceType
.XTERM
) {
6528 // HPR - Cursor right. Same as CUF
6529 if (type
== DeviceType
.XTERM
) {
6534 // REP - Repeat last char X times
6535 if (type
== DeviceType
.XTERM
) {
6540 // DA - Device attributes
6544 // VPA - Cursor to row, current column.
6545 if (type
== DeviceType
.XTERM
) {
6550 // VPR - Cursor down. Same as CUD
6551 if (type
== DeviceType
.XTERM
) {
6556 // HVP - Horizontal and vertical position
6560 // TBC - Tabulation clear
6564 // Sets an ANSI or DEC private toggle
6568 if ((type
== DeviceType
.VT220
)
6569 || (type
== DeviceType
.XTERM
)) {
6571 // Printer functions
6579 // Sets an ANSI or DEC private toggle
6583 // SGR - Select graphics rendition
6587 // DSR - Device status report
6594 // DECLL - Load leds
6598 // DECSTBM - Set top and bottom margins
6604 if (type
== DeviceType
.XTERM
) {
6605 // Window operations
6614 // DECREQTPARM - Request terminal parameters
6631 case CSI_INTERMEDIATE
:
6632 // 00-17, 19, 1C-1F --> execute
6634 handleControlChar((char) ch
);
6637 // 20-2F --> collect
6638 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6642 // 0x30-3F goes to CSI_IGNORE
6643 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6644 scanState
= ScanState
.CSI_IGNORE
;
6647 // 40-7E --> dispatch, then switch to GROUND
6648 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6700 if (((type
== DeviceType
.VT220
)
6701 || (type
== DeviceType
.XTERM
))
6702 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6704 // DECSCL - compatibility level
6707 if ((type
== DeviceType
.XTERM
)
6708 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6710 // DECSTR - Soft terminal reset
6715 if (((type
== DeviceType
.VT220
)
6716 || (type
== DeviceType
.XTERM
))
6717 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6745 // 00-17, 19, 1C-1F --> execute
6747 handleControlChar((char) ch
);
6750 // 20-2F --> collect
6751 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6755 // 40-7E --> ignore, then switch to GROUND
6756 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6760 // 20-3F, 7F --> ignore
6766 // 0x9C goes to GROUND
6771 // 0x1B 0x5C goes to GROUND
6776 if ((collectBuffer
.length() > 0)
6777 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6783 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6784 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6786 scanState
= ScanState
.DCS_INTERMEDIATE
;
6789 // 30-39, 3B --> param, then switch to DCS_PARAM
6790 if ((ch
>= '0') && (ch
<= '9')) {
6792 scanState
= ScanState
.DCS_PARAM
;
6796 scanState
= ScanState
.DCS_PARAM
;
6799 // 3C-3F --> collect, then switch to DCS_PARAM
6800 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6802 scanState
= ScanState
.DCS_PARAM
;
6805 // 00-17, 19, 1C-1F, 7F --> ignore
6807 // 0x3A goes to DCS_IGNORE
6809 scanState
= ScanState
.DCS_IGNORE
;
6812 // 0x71 goes to DCS_SIXEL
6814 sixelParseBuffer
.setLength(0);
6815 scanState
= ScanState
.DCS_SIXEL
;
6816 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6817 // 0x40-7E goes to DCS_PASSTHROUGH
6818 scanState
= ScanState
.DCS_PASSTHROUGH
;
6822 case DCS_INTERMEDIATE
:
6824 // 0x9C goes to GROUND
6829 // 0x1B 0x5C goes to GROUND
6834 if ((collectBuffer
.length() > 0)
6835 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6841 // 0x30-3F goes to DCS_IGNORE
6842 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6843 scanState
= ScanState
.DCS_IGNORE
;
6846 // 0x40-7E goes to DCS_PASSTHROUGH
6847 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6848 scanState
= ScanState
.DCS_PASSTHROUGH
;
6851 // 00-17, 19, 1C-1F, 7F --> ignore
6856 // 0x9C goes to GROUND
6861 // 0x1B 0x5C goes to GROUND
6866 if ((collectBuffer
.length() > 0)
6867 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6873 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6874 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6876 scanState
= ScanState
.DCS_INTERMEDIATE
;
6879 // 30-39, 3B --> param
6880 if ((ch
>= '0') && (ch
<= '9')) {
6887 // 00-17, 19, 1C-1F, 7F --> ignore
6889 // 0x3A, 3C-3F goes to DCS_IGNORE
6891 scanState
= ScanState
.DCS_IGNORE
;
6893 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6894 scanState
= ScanState
.DCS_IGNORE
;
6897 // 0x71 goes to DCS_SIXEL
6899 sixelParseBuffer
.setLength(0);
6900 scanState
= ScanState
.DCS_SIXEL
;
6901 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6902 // 0x40-7E goes to DCS_PASSTHROUGH
6903 scanState
= ScanState
.DCS_PASSTHROUGH
;
6907 case DCS_PASSTHROUGH
:
6908 // 0x9C goes to GROUND
6913 // 0x1B 0x5C goes to GROUND
6918 if ((collectBuffer
.length() > 0)
6919 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6925 // 00-17, 19, 1C-1F, 20-7E --> put
6927 // We ignore all DCS except sixel.
6931 // We ignore all DCS except sixel.
6934 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6935 // We ignore all DCS except sixel.
6938 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6939 // We ignore all DCS except sixel.
6948 // 00-17, 19, 1C-1F, 20-7F --> ignore
6950 // 0x9C goes to GROUND
6958 // 0x9C goes to GROUND
6965 // 0x1B 0x5C goes to GROUND
6971 if ((collectBuffer
.length() > 0)
6972 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6980 // 00-17, 19, 1C-1F, 20-7E --> put
6983 || ((ch
>= 0x1C) && (ch
<= 0x1F))
6984 || ((ch
>= 0x20) && (ch
<= 0x7E))
6986 sixelParseBuffer
.append((char) ch
);
6992 case SOSPMAPC_STRING
:
6993 // 00-17, 19, 1C-1F, 20-7F --> ignore
6995 // Special case for Jexer: PM can pass one control character
7000 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
7004 // 0x9C goes to GROUND
7012 // Special case for Xterm: OSC can pass control characters
7013 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
7017 // 00-17, 19, 1C-1F --> ignore
7019 // 20-7F --> osc_put
7020 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
7024 // 0x9C goes to GROUND
7031 case VT52_DIRECT_CURSOR_ADDRESS
:
7032 // This is a special case for the VT52 sequence "ESC Y l c"
7033 if (collectBuffer
.length() == 0) {
7035 } else if (collectBuffer
.length() == 1) {
7036 // We've got the two characters, one in the buffer and the
7038 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
7047 * Expose current cursor X to outside world.
7049 * @return current cursor X
7051 public final int getCursorX() {
7052 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
7053 return currentState
.cursorX
* 2;
7055 return currentState
.cursorX
;
7059 * Expose current cursor Y to outside world.
7061 * @return current cursor Y
7063 public final int getCursorY() {
7064 return currentState
.cursorY
;
7068 * Returns true if this terminal has requested the mouse pointer be
7071 * @return true if this terminal has requested the mouse pointer be
7074 public final boolean hasHiddenMousePointer() {
7075 return hideMousePointer
;
7079 * Get the mouse protocol.
7081 * @return MouseProtocol.OFF, MouseProtocol.X10, etc.
7083 public MouseProtocol
getMouseProtocol() {
7084 return mouseProtocol
;
7088 * Draw the left and right cells of a two-cell-wide (full-width) glyph.
7090 * @param leftX the x position to draw the left half to
7091 * @param leftY the y position to draw the left half to
7092 * @param rightX the x position to draw the right half to
7093 * @param rightY the y position to draw the right half to
7094 * @param ch the character to draw
7096 private void drawHalves(final int leftX
, final int leftY
,
7097 final int rightX
, final int rightY
, final int ch
) {
7099 // System.err.println("drawHalves(): " + Integer.toHexString(ch));
7101 if (lastTextHeight
!= textHeight
) {
7102 glyphMaker
= GlyphMaker
.getInstance(textHeight
);
7103 lastTextHeight
= textHeight
;
7106 Cell cell
= new Cell(ch
, currentState
.attr
);
7107 BufferedImage image
= glyphMaker
.getImage(cell
, textWidth
* 2,
7109 BufferedImage leftImage
= image
.getSubimage(0, 0, textWidth
,
7111 BufferedImage rightImage
= image
.getSubimage(textWidth
, 0, textWidth
,
7114 Cell left
= new Cell(cell
);
7115 left
.setImage(leftImage
);
7116 left
.setWidth(Cell
.Width
.LEFT
);
7117 display
.get(leftY
).replace(leftX
, left
);
7119 Cell right
= new Cell(cell
);
7120 right
.setImage(rightImage
);
7121 right
.setWidth(Cell
.Width
.RIGHT
);
7122 display
.get(rightY
).replace(rightX
, right
);
7126 * Set the width of a character cell in pixels.
7128 * @param textWidth the width in pixels of a character cell
7130 public void setTextWidth(final int textWidth
) {
7131 this.textWidth
= textWidth
;
7135 * Set the height of a character cell in pixels.
7137 * @param textHeight the height in pixels of a character cell
7139 public void setTextHeight(final int textHeight
) {
7140 this.textHeight
= textHeight
;
7144 * Parse a sixel string into a bitmap image, and overlay that image onto
7147 private void parseSixel() {
7150 System.err.println("parseSixel(): '" + sixelParseBuffer.toString()
7154 Sixel sixel
= new Sixel(sixelParseBuffer
.toString(), sixelPalette
);
7155 BufferedImage image
= sixel
.getImage();
7157 // System.err.println("parseSixel(): image " + image);
7159 if (image
== null) {
7160 // Sixel data was malformed in some way, bail out.
7163 if ((image
.getWidth() < 1)
7164 || (image
.getWidth() > 10000)
7165 || (image
.getHeight() < 1)
7166 || (image
.getHeight() > 10000)
7171 imageToCells(image
, true);
7175 * Parse a "Jexer" RGB image string into a bitmap image, and overlay that
7176 * image onto the text cells.
7178 * @param pw width token
7179 * @param ph height token
7180 * @param ps scroll token
7181 * @param data pixel data
7183 private void parseJexerImageRGB(final String pw
, final String ph
,
7184 final String ps
, final String data
) {
7187 int imageHeight
= 0;
7188 boolean scroll
= false;
7190 imageWidth
= Integer
.parseInt(pw
);
7191 imageHeight
= Integer
.parseInt(ph
);
7192 } catch (NumberFormatException e
) {
7196 if ((imageWidth
< 1)
7197 || (imageWidth
> 10000)
7198 || (imageHeight
< 1)
7199 || (imageHeight
> 10000)
7203 if (ps
.equals("1")) {
7205 } else if (ps
.equals("0")) {
7211 byte [] bytes
= StringUtils
.fromBase64(data
.getBytes());
7212 if (bytes
.length
!= (imageWidth
* imageHeight
* 3)) {
7216 BufferedImage image
= new BufferedImage(imageWidth
, imageHeight
,
7217 BufferedImage
.TYPE_INT_ARGB
);
7219 for (int x
= 0; x
< imageWidth
; x
++) {
7220 for (int y
= 0; y
< imageHeight
; y
++) {
7221 int red
= bytes
[(y
* imageWidth
* 3) + (x
* 3) ];
7225 int green
= bytes
[(y
* imageWidth
* 3) + (x
* 3) + 1];
7229 int blue
= bytes
[(y
* imageWidth
* 3) + (x
* 3) + 2];
7233 int rgb
= 0xFF000000 | (red
<< 16) | (green
<< 8) | blue
;
7234 image
.setRGB(x
, y
, rgb
);
7238 imageToCells(image
, scroll
);
7242 * Parse a "Jexer" PNG or JPG image string into a bitmap image, and
7243 * overlay that image onto the text cells.
7245 * @param type 1 for PNG, 2 for JPG
7246 * @param ps scroll token
7247 * @param data pixel data
7249 private void parseJexerImageFile(final int type
, final String ps
,
7250 final String data
) {
7253 int imageHeight
= 0;
7254 boolean scroll
= false;
7255 BufferedImage image
= null;
7257 byte [] bytes
= StringUtils
.fromBase64(data
.getBytes());
7261 if ((bytes
[0] != (byte) 0x89)
7262 || (bytes
[1] != 'P')
7263 || (bytes
[2] != 'N')
7264 || (bytes
[3] != 'G')
7265 || (bytes
[4] != (byte) 0x0D)
7266 || (bytes
[5] != (byte) 0x0A)
7267 || (bytes
[6] != (byte) 0x1A)
7268 || (bytes
[7] != (byte) 0x0A)
7270 // File does not have PNG header, bail out.
7276 if ((bytes
[0] != (byte) 0XFF)
7277 || (bytes
[1] != (byte) 0xD8)
7278 || (bytes
[2] != (byte) 0xFF)
7280 // File does not have JPG header, bail out.
7286 // Unsupported type, bail out.
7290 image
= ImageIO
.read(new ByteArrayInputStream(bytes
));
7291 } catch (IOException e
) {
7295 assert (image
!= null);
7296 imageWidth
= image
.getWidth();
7297 imageHeight
= image
.getHeight();
7298 if ((imageWidth
< 1)
7299 || (imageWidth
> 10000)
7300 || (imageHeight
< 1)
7301 || (imageHeight
> 10000)
7305 if (ps
.equals("1")) {
7307 } else if (ps
.equals("0")) {
7313 imageToCells(image
, scroll
);
7317 * Break up an image into the cells at the current cursor.
7319 * @param image the image to display
7320 * @param scroll if true, scroll the image and move the cursor
7322 private void imageToCells(final BufferedImage image
, final boolean scroll
) {
7323 assert (image
!= null);
7328 * Break up the image into text cell sized pieces as a new array of
7331 * Note original column position x0.
7335 * 1. Advance (printCharacter(' ')) for horizontal increment, or
7336 * index (linefeed() + cursorPosition(y, x0)) for vertical
7339 * 2. Set (x, y) cell image data.
7341 * 3. For the right and bottom edges:
7343 * a. Render the text to pixels using Terminus font.
7345 * b. Blit the image on top of the text, using alpha channel.
7347 int cellColumns
= image
.getWidth() / textWidth
;
7348 if (cellColumns
* textWidth
< image
.getWidth()) {
7351 int cellRows
= image
.getHeight() / textHeight
;
7352 if (cellRows
* textHeight
< image
.getHeight()) {
7356 // Break the image up into an array of cells.
7357 Cell
[][] cells
= new Cell
[cellColumns
][cellRows
];
7359 for (int x
= 0; x
< cellColumns
; x
++) {
7360 for (int y
= 0; y
< cellRows
; y
++) {
7362 int width
= textWidth
;
7363 if ((x
+ 1) * textWidth
> image
.getWidth()) {
7364 width
= image
.getWidth() - (x
* textWidth
);
7366 int height
= textHeight
;
7367 if ((y
+ 1) * textHeight
> image
.getHeight()) {
7368 height
= image
.getHeight() - (y
* textHeight
);
7371 Cell cell
= new Cell();
7372 if ((width
!= textWidth
) || (height
!= textHeight
)) {
7373 BufferedImage newImage
;
7374 newImage
= new BufferedImage(textWidth
, textHeight
,
7375 BufferedImage
.TYPE_INT_ARGB
);
7377 Graphics gr
= newImage
.getGraphics();
7378 gr
.drawImage(image
.getSubimage(x
* textWidth
,
7379 y
* textHeight
, width
, height
),
7382 cell
.setImage(newImage
);
7384 cell
.setImage(image
.getSubimage(x
* textWidth
,
7385 y
* textHeight
, width
, height
));
7392 int x0
= currentState
.cursorX
;
7393 int y0
= currentState
.cursorY
;
7394 for (int y
= 0; y
< cellRows
; y
++) {
7395 for (int x
= 0; x
< cellColumns
; x
++) {
7396 assert (currentState
.cursorX
<= rightMargin
);
7398 // A real sixel terminal would render the text of the current
7399 // cell first, then image over it (accounting for blank
7400 // pixels). We do not support that. A cell is either text,
7401 // or image, but not a mix of image-over-text.
7402 DisplayLine line
= display
.get(currentState
.cursorY
);
7403 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
7405 // If at the end of the visible screen, stop.
7406 if (currentState
.cursorX
== rightMargin
) {
7409 // Room for more image on the visible screen.
7410 currentState
.cursorX
++;
7412 if (currentState
.cursorY
< scrollRegionBottom
- 1) {
7413 // Not at the bottom, down a line.
7415 } else if (scroll
== true) {
7416 // At the bottom, scroll as needed.
7419 // At the bottom, no more scrolling, done.
7423 cursorPosition(currentState
.cursorY
, x0
);
7426 if (scroll
== false) {
7427 cursorPosition(y0
, x0
);