2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.tterminal
;
31 import java
.awt
.Graphics2D
;
32 import java
.awt
.image
.BufferedImage
;
33 import java
.io
.BufferedInputStream
;
34 import java
.io
.BufferedOutputStream
;
35 import java
.io
.CharArrayWriter
;
36 import java
.io
.InputStream
;
37 import java
.io
.InputStreamReader
;
38 import java
.io
.IOException
;
39 import java
.io
.OutputStream
;
40 import java
.io
.OutputStreamWriter
;
41 import java
.io
.PrintWriter
;
42 import java
.io
.Reader
;
43 import java
.io
.UnsupportedEncodingException
;
44 import java
.io
.Writer
;
45 import java
.util
.ArrayList
;
46 import java
.util
.Collections
;
47 import java
.util
.HashMap
;
48 import java
.util
.List
;
50 import jexer
.TKeypress
;
51 import jexer
.backend
.GlyphMaker
;
52 import jexer
.bits
.Color
;
53 import jexer
.bits
.Cell
;
54 import jexer
.bits
.CellAttributes
;
55 import jexer
.bits
.StringUtils
;
56 import jexer
.event
.TInputEvent
;
57 import jexer
.event
.TKeypressEvent
;
58 import jexer
.event
.TMouseEvent
;
59 import jexer
.io
.ReadTimeoutException
;
60 import jexer
.io
.TimeoutInputStream
;
61 import static jexer
.TKeypress
.*;
64 * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
65 * including a scrollback buffer.
68 * It currently implements VT100, VT102, VT220, and XTERM with the following
72 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
73 * failure with XTERM. This is due to vttest failing to decode the UTF-8
77 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
78 * from VT100 are not supported.
81 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
82 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
83 * because DECSCL is not supported, it will fail the last part of the
84 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
87 * - Numeric/application keys from the number pad are not supported because
88 * they are not exposed from the TKeypress API.
91 * - VT52 HOLD SCREEN mode is not supported.
94 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
95 * numerators) are not rendered correctly.
98 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
100 public class ECMA48
implements Runnable
{
102 // ------------------------------------------------------------------------
103 // Constants --------------------------------------------------------------
104 // ------------------------------------------------------------------------
107 * The emulator can emulate several kinds of terminals.
109 public enum DeviceType
{
111 * DEC VT100 but also including the three VT102 functions.
132 * Parser character scan states.
134 private enum ScanState
{
150 VT52_DIRECT_CURSOR_ADDRESS
154 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
155 * can't really use it in keypress() because we do not see number pad
156 * events from TKeypress.
158 private enum KeypadMode
{
164 * Arrow keys can emit three different sequences (DECCKM or VT52
167 private enum ArrowKeyMode
{
174 * Available character sets for GL, GR, G0, G1, G2, G3.
176 private enum CharacterSet
{
197 * Single-shift states used by the C1 control characters SS2 (0x8E) and
200 private enum Singleshift
{
207 * VT220+ lockshift states.
209 private enum LockshiftMode
{
219 * XTERM mouse reporting protocols.
221 public enum MouseProtocol
{
230 * XTERM mouse reporting encodings.
232 private enum MouseEncoding
{
238 // ------------------------------------------------------------------------
239 // Variables --------------------------------------------------------------
240 // ------------------------------------------------------------------------
243 * The enclosing listening object.
245 private DisplayListener displayListener
;
248 * When true, the reader thread is expected to exit.
250 private volatile boolean stopReaderThread
= false;
255 private Thread readerThread
= null;
258 * The type of emulator to be.
260 private final DeviceType type
;
263 * The scrollback buffer characters + attributes.
265 private volatile ArrayList
<DisplayLine
> scrollback
;
268 * The raw display buffer characters + attributes.
270 private volatile ArrayList
<DisplayLine
> display
;
273 * The maximum number of lines in the scrollback buffer.
275 private int maxScrollback
= 10000;
278 * The terminal's input. For type == XTERM, this is an InputStreamReader
279 * with UTF-8 encoding.
281 private Reader input
;
284 * The terminal's raw InputStream. This is used for type != XTERM.
286 private volatile TimeoutInputStream inputStream
;
289 * The terminal's output. For type == XTERM, this wraps an
290 * OutputStreamWriter with UTF-8 encoding.
292 private Writer output
;
295 * The terminal's raw OutputStream. This is used for type != XTERM.
297 private OutputStream outputStream
;
300 * Current scanning state.
302 private ScanState scanState
;
305 * Which mouse protocol is active.
307 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
310 * Which mouse encoding is active.
312 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
315 * A terminal may request that the mouse pointer be hidden using a
316 * Privacy Message containing either "hideMousePointer" or
317 * "showMousePointer". This is currently only used within Jexer by
318 * TTerminalWindow so that only the bottom-most instance of nested
319 * Jexer's draws the mouse within its application window.
321 private boolean hideMousePointer
= false;
324 * Physical display width. We start at 80x24, but the user can resize us
330 * Physical display height. We start at 80x24, but the user can resize
336 * Top margin of the scrolling region.
338 private int scrollRegionTop
;
341 * Bottom margin of the scrolling region.
343 private int scrollRegionBottom
;
346 * Right margin column number. This can be selected by the remote side
347 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
349 private int rightMargin
;
352 * Last character printed.
357 * VT100-style line wrapping: a character is placed in column 80 (or
358 * 132), but the line does NOT wrap until another character is written to
359 * column 1 of the next line, after which the cursor moves to column 2.
361 private boolean wrapLineFlag
;
364 * VT220 single shift flag.
366 private Singleshift singleshift
= Singleshift
.NONE
;
369 * true = insert characters, false = overwrite.
371 private boolean insertMode
= false;
374 * VT52 mode as selected by DECANM. True means VT52, false means
375 * ANSI. Default is ANSI.
377 private boolean vt52Mode
= false;
380 * Visible cursor (DECTCEM).
382 private boolean cursorVisible
= true;
385 * Screen title as set by the xterm OSC sequence. Lots of applications
386 * send a screenTitle regardless of whether it is an xterm client or not.
388 private String screenTitle
= "";
391 * Parameter characters being collected.
393 private List
<Integer
> csiParams
;
396 * Non-csi collect buffer.
398 private StringBuilder collectBuffer
;
401 * When true, use the G1 character set.
403 private boolean shiftOut
= false;
406 * Horizontal tab stop locations.
408 private List
<Integer
> tabStops
;
411 * S8C1T. True means 8bit controls, false means 7bit controls.
413 private boolean s8c1t
= false;
416 * Printer mode. True means send all output to printer, which discards
419 private boolean printerControllerMode
= false;
422 * LMN line mode. If true, linefeed() puts the cursor on the first
423 * column of the next line. If false, linefeed() puts the cursor one
424 * line down on the current line. The default is false.
426 private boolean newLineMode
= false;
429 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
431 private ArrowKeyMode arrowKeyMode
;
434 * Whether number pad keys send VT100 or VT52, application or numeric
437 @SuppressWarnings("unused")
438 private KeypadMode keypadMode
;
441 * When true, the terminal is in 132-column mode (DECCOLM).
443 private boolean columns132
= false;
446 * true = reverse video. Set by DECSCNM.
448 private boolean reverseVideo
= false;
451 * false = echo characters locally.
453 private boolean fullDuplex
= true;
456 * The current terminal state.
458 private SaveableState currentState
;
461 * The last saved terminal state.
463 private SaveableState savedState
;
466 * The 88- or 256-color support RGB colors.
468 private List
<Integer
> colors88
;
471 * Sixel collection buffer.
473 private StringBuilder sixelParseBuffer
;
476 * Sixel shared palette.
478 private HashMap
<Integer
, java
.awt
.Color
> sixelPalette
;
481 * The width of a character cell in pixels.
483 private int textWidth
= 16;
486 * The height of a character cell in pixels.
488 private int textHeight
= 20;
491 * The last used height of a character cell in pixels, only used for
494 private int lastTextHeight
= -1;
497 * The glyph drawer for full-width chars.
499 private GlyphMaker glyphMaker
= null;
502 * Input queue for keystrokes and mouse events to send to the remote
505 private ArrayList
<TInputEvent
> userQueue
= new ArrayList
<TInputEvent
>();
508 * DECSC/DECRC save/restore a subset of the total state. This class
509 * encapsulates those specific flags/modes.
511 private class SaveableState
{
514 * When true, cursor positions are relative to the scrolling region.
516 public boolean originMode
= false;
519 * The current editing X position.
521 public int cursorX
= 0;
524 * The current editing Y position.
526 public int cursorY
= 0;
529 * Which character set is currently selected in G0.
531 public CharacterSet g0Charset
= CharacterSet
.US
;
534 * Which character set is currently selected in G1.
536 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
539 * Which character set is currently selected in G2.
541 public CharacterSet g2Charset
= CharacterSet
.US
;
544 * Which character set is currently selected in G3.
546 public CharacterSet g3Charset
= CharacterSet
.US
;
549 * Which character set is currently selected in GR.
551 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
554 * The current drawing attributes.
556 public CellAttributes attr
;
561 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
566 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
571 public boolean lineWrap
= true;
576 public void reset() {
580 g0Charset
= CharacterSet
.US
;
581 g1Charset
= CharacterSet
.DRAWING
;
582 g2Charset
= CharacterSet
.US
;
583 g3Charset
= CharacterSet
.US
;
584 grCharset
= CharacterSet
.DRAWING
;
585 attr
= new CellAttributes();
586 glLockshift
= LockshiftMode
.NONE
;
587 grLockshift
= LockshiftMode
.NONE
;
592 * Copy attributes from another instance.
594 * @param that the other instance to match
596 public void setTo(final SaveableState that
) {
597 this.originMode
= that
.originMode
;
598 this.cursorX
= that
.cursorX
;
599 this.cursorY
= that
.cursorY
;
600 this.g0Charset
= that
.g0Charset
;
601 this.g1Charset
= that
.g1Charset
;
602 this.g2Charset
= that
.g2Charset
;
603 this.g3Charset
= that
.g3Charset
;
604 this.grCharset
= that
.grCharset
;
605 this.attr
= new CellAttributes();
606 this.attr
.setTo(that
.attr
);
607 this.glLockshift
= that
.glLockshift
;
608 this.grLockshift
= that
.grLockshift
;
609 this.lineWrap
= that
.lineWrap
;
613 * Public constructor.
615 public SaveableState() {
620 // ------------------------------------------------------------------------
621 // Constructors -----------------------------------------------------------
622 // ------------------------------------------------------------------------
625 * Public constructor.
627 * @param type one of the DeviceType constants to select VT100, VT102,
629 * @param inputStream an InputStream connected to the remote side. For
630 * type == XTERM, inputStream is converted to a Reader with UTF-8
632 * @param outputStream an OutputStream connected to the remote user. For
633 * type == XTERM, outputStream is converted to a Writer with UTF-8
635 * @param displayListener a callback to the outer display, or null for
636 * default VT100 behavior
637 * @throws UnsupportedEncodingException if an exception is thrown when
638 * creating the InputStreamReader
640 public ECMA48(final DeviceType type
, final InputStream inputStream
,
641 final OutputStream outputStream
, final DisplayListener displayListener
)
642 throws UnsupportedEncodingException
{
644 assert (inputStream
!= null);
645 assert (outputStream
!= null);
647 csiParams
= new ArrayList
<Integer
>();
648 tabStops
= new ArrayList
<Integer
>();
649 scrollback
= new ArrayList
<DisplayLine
>();
650 display
= new ArrayList
<DisplayLine
>();
653 if (inputStream
instanceof TimeoutInputStream
) {
654 this.inputStream
= (TimeoutInputStream
)inputStream
;
656 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
658 if (type
== DeviceType
.XTERM
) {
659 this.input
= new InputStreamReader(new BufferedInputStream(
660 this.inputStream
, 1024 * 128), "UTF-8");
661 this.output
= new OutputStreamWriter(new
662 BufferedOutputStream(outputStream
), "UTF-8");
663 this.outputStream
= null;
666 this.outputStream
= new BufferedOutputStream(outputStream
);
668 this.displayListener
= displayListener
;
671 for (int i
= 0; i
< height
; i
++) {
672 display
.add(new DisplayLine(currentState
.attr
));
675 // Spin up the input reader
676 readerThread
= new Thread(this);
677 readerThread
.start();
680 // ------------------------------------------------------------------------
681 // Runnable ---------------------------------------------------------------
682 // ------------------------------------------------------------------------
685 * Read function runs on a separate thread.
687 public final void run() {
688 boolean utf8
= false;
689 boolean done
= false;
691 if (type
== DeviceType
.XTERM
) {
695 // available() will often return > 1, so we need to read in chunks to
697 char [] readBufferUTF8
= null;
698 byte [] readBuffer
= null;
700 readBufferUTF8
= new char[2048];
702 readBuffer
= new byte[2048];
705 while (!done
&& !stopReaderThread
) {
706 synchronized (userQueue
) {
707 while (userQueue
.size() > 0) {
708 handleUserEvent(userQueue
.remove(0));
713 int n
= inputStream
.available();
715 // System.err.printf("available() %d\n", n); System.err.flush();
717 if (readBufferUTF8
.length
< n
) {
718 // The buffer wasn't big enough, make it huger
719 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
722 readBufferUTF8
= new char[newSizeHalf
* 2];
725 if (readBuffer
.length
< n
) {
726 // The buffer wasn't big enough, make it huger
727 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
728 readBuffer
= new byte[newSizeHalf
* 2];
734 } catch (InterruptedException e
) {
743 rc
= input
.read(readBufferUTF8
, 0,
744 readBufferUTF8
.length
);
746 rc
= inputStream
.read(readBuffer
, 0,
749 } catch (ReadTimeoutException e
) {
753 // System.err.printf("read() %d\n", rc); System.err.flush();
758 // Don't step on UI events
759 synchronized (this) {
761 for (int i
= 0; i
< rc
;) {
762 int ch
= Character
.codePointAt(readBufferUTF8
,
764 i
+= Character
.charCount(ch
);
766 // Special case for VT10x: 7-bit characters
768 if ((type
== DeviceType
.VT100
)
769 || (type
== DeviceType
.VT102
)
777 for (int i
= 0; i
< rc
; i
++) {
778 // Special case for VT10x: 7-bit characters
780 if ((type
== DeviceType
.VT100
)
781 || (type
== DeviceType
.VT102
)
783 consume(readBuffer
[i
] & 0x7F);
785 consume(readBuffer
[i
]);
790 // Permit my enclosing UI to know that I updated.
791 if (displayListener
!= null) {
792 displayListener
.displayChanged();
795 // System.err.println("end while loop"); System.err.flush();
796 } catch (IOException e
) {
799 // This is an unusual case. We want to see the stack trace,
800 // but it is related to the spawned process rather than the
801 // actual UI. We will generate the stack trace, and consume
802 // it as though it was emitted by the shell.
803 CharArrayWriter writer
= new CharArrayWriter();
804 // Send a ST and RIS to clear the emulator state.
806 writer
.write("\033\\\033c");
807 writer
.write("\n-----------------------------------\n");
808 e
.printStackTrace(new PrintWriter(writer
));
809 writer
.write("\n-----------------------------------\n");
810 } catch (IOException e2
) {
813 char [] stackTrace
= writer
.toCharArray();
814 for (int i
= 0; i
< stackTrace
.length
; i
++) {
815 if (stackTrace
[i
] == '\n') {
818 consume(stackTrace
[i
]);
822 } // while ((done == false) && (stopReaderThread == false))
824 // Let the rest of the world know that I am done.
825 stopReaderThread
= true;
828 inputStream
.cancelRead();
831 } catch (IOException e
) {
837 } catch (IOException e
) {
841 // Permit my enclosing UI to know that I updated.
842 if (displayListener
!= null) {
843 displayListener
.displayChanged();
846 // System.err.println("*** run() exiting..."); System.err.flush();
849 // ------------------------------------------------------------------------
850 // ECMA48 -----------------------------------------------------------------
851 // ------------------------------------------------------------------------
854 * Process keyboard and mouse events from the user.
856 * @param event the input event to consume
858 private void handleUserEvent(final TInputEvent event
) {
859 if (event
instanceof TKeypressEvent
) {
860 keypress(((TKeypressEvent
) event
).getKey());
862 if (event
instanceof TMouseEvent
) {
863 mouse((TMouseEvent
) event
);
868 * Add a keyboard and mouse event from the user to the queue.
870 * @param event the input event to consume
872 public void addUserEvent(final TInputEvent event
) {
873 synchronized (userQueue
) {
874 userQueue
.add(event
);
879 * Return the proper primary Device Attributes string.
881 * @return string to send to remote side that is appropriate for the
884 private String
deviceTypeResponse() {
887 // "I am a VT100 with advanced video option" (often VT102)
896 // "I am a VT220" - 7 bit version
898 return "\033[?62;1;6;9;4;22c";
899 // return "\033[?62;1;6;9;4;22;444c";
901 // "I am a VT220" - 8 bit version
902 return "\u009b?62;1;6;9;4;22c";
903 // return "\u009b?62;1;6;9;4;22;444c";
905 throw new IllegalArgumentException("Invalid device type: " + type
);
910 * Return the proper TERM environment variable for this device type.
912 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
913 * @return "vt100", "xterm", etc.
915 public static String
deviceTypeTerm(final DeviceType deviceType
) {
916 switch (deviceType
) {
930 throw new IllegalArgumentException("Invalid device type: "
936 * Return the proper LANG for this device type. Only XTERM devices know
937 * about UTF-8, the others are defined by their standard to be either
938 * 7-bit or 8-bit characters only.
940 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
941 * @param baseLang a base language without UTF-8 flag such as "C" or
943 * @return "en_US", "en_US.UTF-8", etc.
945 public static String
deviceTypeLang(final DeviceType deviceType
,
946 final String baseLang
) {
948 switch (deviceType
) {
956 return baseLang
+ ".UTF-8";
959 throw new IllegalArgumentException("Invalid device type: "
965 * Write a string directly to the remote side.
967 * @param str string to send
969 public void writeRemote(final String str
) {
970 if (stopReaderThread
) {
971 // Reader hit EOF, bail out now.
976 // System.err.printf("writeRemote() '%s'\n", str);
982 if (outputStream
== null) {
986 outputStream
.flush();
987 for (int i
= 0; i
< str
.length(); i
++) {
988 outputStream
.write(str
.charAt(i
));
990 outputStream
.flush();
991 } catch (IOException e
) {
997 if (output
== null) {
1004 } catch (IOException e
) {
1010 throw new IllegalArgumentException("Invalid device type: " + type
);
1015 * Close the input and output streams and stop the reader thread. Note
1016 * that it is safe to call this multiple times.
1018 public final void close() {
1020 // Tell the reader thread to stop looking at input. It will close
1021 // the input streams.
1022 if (stopReaderThread
== false) {
1023 stopReaderThread
= true;
1025 readerThread
.join(1000);
1026 } catch (InterruptedException e
) {
1031 // Now close the output stream.
1036 if (outputStream
!= null) {
1038 outputStream
.close();
1039 } catch (IOException e
) {
1042 outputStream
= null;
1046 if (outputStream
!= null) {
1048 outputStream
.close();
1049 } catch (IOException e
) {
1052 outputStream
= null;
1054 if (output
!= null) {
1057 } catch (IOException e
) {
1064 throw new IllegalArgumentException("Invalid device type: " +
1070 * See if the reader thread is still running.
1072 * @return if true, we are still connected to / reading from the remote
1075 public final boolean isReading() {
1076 return (!stopReaderThread
);
1080 * Obtain a new blank display line for an external user
1081 * (e.g. TTerminalWindow).
1083 * @return new blank line
1085 public final DisplayLine
getBlankDisplayLine() {
1086 return new DisplayLine(currentState
.attr
);
1090 * Get the scrollback buffer.
1092 * @return the scrollback buffer
1094 public final List
<DisplayLine
> getScrollbackBuffer() {
1099 * Get the display buffer.
1101 * @return the display buffer
1103 public final List
<DisplayLine
> getDisplayBuffer() {
1108 * Get the visible display + scrollback buffer, offset by a specified
1109 * number of rows from the bottom.
1111 * @param visibleHeight the total height of the display to show
1112 * @param scrollBottom the number of rows from the bottom to scroll back
1113 * @return a copy of the display + scrollback buffers
1115 public final List
<DisplayLine
> getVisibleDisplay(final int visibleHeight
,
1116 final int scrollBottom
) {
1118 assert (visibleHeight
>= 0);
1119 assert (scrollBottom
>= 0);
1121 int visibleBottom
= scrollback
.size() + display
.size() - scrollBottom
;
1123 List
<DisplayLine
> preceedingBlankLines
= new ArrayList
<DisplayLine
>();
1124 int visibleTop
= visibleBottom
- visibleHeight
;
1125 if (visibleTop
< 0) {
1126 for (int i
= visibleTop
; i
< 0; i
++) {
1127 preceedingBlankLines
.add(getBlankDisplayLine());
1131 assert (visibleTop
>= 0);
1133 List
<DisplayLine
> displayLines
= new ArrayList
<DisplayLine
>();
1134 displayLines
.addAll(scrollback
);
1135 displayLines
.addAll(display
);
1137 List
<DisplayLine
> visibleLines
= new ArrayList
<DisplayLine
>();
1138 visibleLines
.addAll(preceedingBlankLines
);
1139 visibleLines
.addAll(displayLines
.subList(visibleTop
, visibleBottom
));
1141 // Fill in the blank lines on bottom
1142 int bottomBlankLines
= visibleHeight
- visibleLines
.size();
1143 assert (bottomBlankLines
>= 0);
1144 for (int i
= 0; i
< bottomBlankLines
; i
++) {
1145 visibleLines
.add(getBlankDisplayLine());
1148 return copyBuffer(visibleLines
);
1152 * Copy a display buffer.
1154 * @param buffer the buffer to copy
1155 * @return a deep copy of the buffer's data
1157 private List
<DisplayLine
> copyBuffer(final List
<DisplayLine
> buffer
) {
1158 ArrayList
<DisplayLine
> result
= new ArrayList
<DisplayLine
>(buffer
.size());
1159 for (DisplayLine line
: buffer
) {
1160 result
.add(new DisplayLine(line
));
1166 * Get the display width.
1168 * @return the width (usually 80 or 132)
1170 public final int getWidth() {
1175 * Set the display width.
1177 * @param width the new width
1179 public final synchronized void setWidth(final int width
) {
1181 rightMargin
= width
- 1;
1182 if (currentState
.cursorX
>= width
) {
1183 currentState
.cursorX
= width
- 1;
1185 if (savedState
.cursorX
>= width
) {
1186 savedState
.cursorX
= width
- 1;
1191 * Get the display height.
1193 * @return the height (usually 24)
1195 public final int getHeight() {
1200 * Set the display height.
1202 * @param height the new height
1204 public final synchronized void setHeight(final int height
) {
1205 int delta
= height
- this.height
;
1206 this.height
= height
;
1207 scrollRegionBottom
+= delta
;
1208 if (scrollRegionBottom
< 0) {
1209 scrollRegionBottom
= height
;
1211 if (scrollRegionTop
>= scrollRegionBottom
) {
1212 scrollRegionTop
= 0;
1214 if (currentState
.cursorY
>= height
) {
1215 currentState
.cursorY
= height
- 1;
1217 if (savedState
.cursorY
>= height
) {
1218 savedState
.cursorY
= height
- 1;
1220 while (display
.size() < height
) {
1221 DisplayLine line
= new DisplayLine(currentState
.attr
);
1222 line
.setReverseColor(reverseVideo
);
1225 while (display
.size() > height
) {
1226 scrollback
.add(display
.remove(0));
1231 * Get visible cursor flag.
1233 * @return if true, the cursor is visible
1235 public final boolean isCursorVisible() {
1236 return cursorVisible
;
1240 * Get the screen title as set by the xterm OSC sequence. Lots of
1241 * applications send a screenTitle regardless of whether it is an xterm
1244 * @return screen title
1246 public final String
getScreenTitle() {
1251 * Get 132 columns value.
1253 * @return if true, the terminal is in 132 column mode
1255 public final boolean isColumns132() {
1260 * Clear the CSI parameters and flags.
1262 private void toGround() {
1264 collectBuffer
= new StringBuilder(8);
1265 scanState
= ScanState
.GROUND
;
1269 * Reset the tab stops list.
1271 private void resetTabStops() {
1273 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
1274 tabStops
.add(Integer
.valueOf(i
* 8));
1279 * Reset the 88- or 256-colors.
1281 private void resetColors() {
1282 colors88
= new ArrayList
<Integer
>(256);
1283 for (int i
= 0; i
< 256; i
++) {
1287 // Set default system colors.
1288 colors88
.set(0, 0x00000000);
1289 colors88
.set(1, 0x00a80000);
1290 colors88
.set(2, 0x0000a800);
1291 colors88
.set(3, 0x00a85400);
1292 colors88
.set(4, 0x000000a8);
1293 colors88
.set(5, 0x00a800a8);
1294 colors88
.set(6, 0x0000a8a8);
1295 colors88
.set(7, 0x00a8a8a8);
1297 colors88
.set(8, 0x00545454);
1298 colors88
.set(9, 0x00fc5454);
1299 colors88
.set(10, 0x0054fc54);
1300 colors88
.set(11, 0x00fcfc54);
1301 colors88
.set(12, 0x005454fc);
1302 colors88
.set(13, 0x00fc54fc);
1303 colors88
.set(14, 0x0054fcfc);
1304 colors88
.set(15, 0x00fcfcfc);
1308 * Get the RGB value of one of the indexed colors.
1310 * @param index the color index
1311 * @return the RGB value
1313 private int get88Color(final int index
) {
1314 // System.err.print("get88Color: " + index);
1315 if ((index
< 0) || (index
> colors88
.size())) {
1316 // System.err.println(" -- UNKNOWN");
1319 // System.err.printf(" %08x\n", colors88.get(index));
1320 return colors88
.get(index
);
1324 * Set one of the indexed colors to a color specification.
1326 * @param index the color index
1327 * @param spec the specification, typically something like "rgb:aa/bb/cc"
1329 private void set88Color(final int index
, final String spec
) {
1330 // System.err.println("set88Color: " + index + " '" + spec + "'");
1332 if ((index
< 0) || (index
> colors88
.size())) {
1335 if (spec
.startsWith("rgb:")) {
1336 String
[] rgbTokens
= spec
.substring(4).split("/");
1337 if (rgbTokens
.length
== 3) {
1339 int rgb
= (Integer
.parseInt(rgbTokens
[0], 16) << 16);
1340 rgb
|= Integer
.parseInt(rgbTokens
[1], 16) << 8;
1341 rgb
|= Integer
.parseInt(rgbTokens
[2], 16);
1342 // System.err.printf(" set to %08x\n", rgb);
1343 colors88
.set(index
, rgb
);
1344 } catch (NumberFormatException e
) {
1351 if (spec
.toLowerCase().equals("black")) {
1352 colors88
.set(index
, 0x00000000);
1353 } else if (spec
.toLowerCase().equals("red")) {
1354 colors88
.set(index
, 0x00a80000);
1355 } else if (spec
.toLowerCase().equals("green")) {
1356 colors88
.set(index
, 0x0000a800);
1357 } else if (spec
.toLowerCase().equals("yellow")) {
1358 colors88
.set(index
, 0x00a85400);
1359 } else if (spec
.toLowerCase().equals("blue")) {
1360 colors88
.set(index
, 0x000000a8);
1361 } else if (spec
.toLowerCase().equals("magenta")) {
1362 colors88
.set(index
, 0x00a800a8);
1363 } else if (spec
.toLowerCase().equals("cyan")) {
1364 colors88
.set(index
, 0x0000a8a8);
1365 } else if (spec
.toLowerCase().equals("white")) {
1366 colors88
.set(index
, 0x00a8a8a8);
1372 * Reset the emulation state.
1374 private void reset() {
1376 currentState
= new SaveableState();
1377 savedState
= new SaveableState();
1378 scanState
= ScanState
.GROUND
;
1381 scrollRegionTop
= 0;
1382 scrollRegionBottom
= height
- 1;
1383 rightMargin
= width
- 1;
1384 newLineMode
= false;
1385 arrowKeyMode
= ArrowKeyMode
.ANSI
;
1386 keypadMode
= KeypadMode
.Numeric
;
1387 wrapLineFlag
= false;
1388 if (displayListener
!= null) {
1389 width
= displayListener
.getDisplayWidth();
1390 height
= displayListener
.getDisplayHeight();
1391 rightMargin
= width
- 1;
1399 newLineMode
= false;
1400 reverseVideo
= false;
1402 cursorVisible
= true;
1405 singleshift
= Singleshift
.NONE
;
1407 printerControllerMode
= false;
1410 mouseProtocol
= MouseProtocol
.OFF
;
1411 mouseEncoding
= MouseEncoding
.X10
;
1416 // Reset extra colors
1424 * Append a new line to the bottom of the display, adding lines off the
1425 * top to the scrollback buffer.
1427 private void newDisplayLine() {
1428 // Scroll the top line off into the scrollback buffer
1429 scrollback
.add(display
.get(0));
1430 if (scrollback
.size() > maxScrollback
) {
1431 scrollback
.remove(0);
1432 scrollback
.trimToSize();
1435 display
.trimToSize();
1436 DisplayLine line
= new DisplayLine(currentState
.attr
);
1437 line
.setReverseColor(reverseVideo
);
1442 * Wraps the current line.
1444 private void wrapCurrentLine() {
1445 if (currentState
.cursorY
== height
- 1) {
1448 if (currentState
.cursorY
< height
- 1) {
1449 currentState
.cursorY
++;
1451 currentState
.cursorX
= 0;
1455 * Handle a carriage return.
1457 private void carriageReturn() {
1458 currentState
.cursorX
= 0;
1459 wrapLineFlag
= false;
1463 * Reverse the color of the visible display.
1465 private void invertDisplayColors() {
1466 for (DisplayLine line
: display
) {
1467 line
.setReverseColor(!line
.isReverseColor());
1472 * Handle a linefeed.
1474 private void linefeed() {
1476 if (currentState
.cursorY
< scrollRegionBottom
) {
1477 // Increment screen y
1478 currentState
.cursorY
++;
1481 // Screen y does not increment
1484 * Two cases: either we're inside a scrolling region or not. If
1485 * the scrolling region bottom is the bottom of the screen, then
1486 * push the top line into the buffer. Else scroll the scrolling
1489 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1491 // We're at the bottom of the scroll region, AND the scroll
1492 // region is the entire screen.
1498 // We're at the bottom of the scroll region, AND the scroll
1499 // region is NOT the entire screen.
1500 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1505 currentState
.cursorX
= 0;
1507 wrapLineFlag
= false;
1511 * Prints one character to the display buffer.
1513 * @param ch character to display
1515 private void printCharacter(final int ch
) {
1516 int rightMargin
= this.rightMargin
;
1518 if (StringUtils
.width(ch
) == 2) {
1519 // This is a full-width character. Save two spaces, and then
1520 // draw the character as two image halves.
1521 int x0
= currentState
.cursorX
;
1522 int y0
= currentState
.cursorY
;
1523 printCharacter(' ');
1524 printCharacter(' ');
1525 if ((currentState
.cursorX
== x0
+ 2)
1526 && (currentState
.cursorY
== y0
)
1528 // We can draw both halves of the character.
1529 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1530 } else if ((currentState
.cursorX
== x0
+ 1)
1531 && (currentState
.cursorY
== y0
)
1533 // VT100 line wrap behavior: we should be at the right
1534 // margin. We can draw both halves of the character.
1535 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1537 // The character splits across the line. Draw the entire
1538 // character on the new line, giving one more space for it.
1539 x0
= currentState
.cursorX
- 1;
1540 y0
= currentState
.cursorY
;
1541 printCharacter(' ');
1542 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1547 // Check if we have double-width, and if so chop at 40/66 instead of
1549 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1550 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1553 // Check the unusually-complicated line wrapping conditions...
1554 if (currentState
.cursorX
== rightMargin
) {
1556 if (currentState
.lineWrap
== true) {
1558 * This case happens when: the cursor was already on the
1559 * right margin (either through printing or by an explicit
1560 * placement command), and a character was printed.
1562 * The line wraps only when a new character arrives AND the
1563 * cursor is already on the right margin AND has placed a
1564 * character in its cell. Easier to see than to explain.
1566 if (wrapLineFlag
== false) {
1568 * This block marks the case that we are in the margin
1569 * and the first character has been received and printed.
1571 wrapLineFlag
= true;
1574 * This block marks the case that we are in the margin
1575 * and the second character has been received and
1578 wrapLineFlag
= false;
1582 } else if (currentState
.cursorX
<= rightMargin
) {
1584 * This is the normal case: a character came in and was printed
1585 * to the left of the right margin column.
1588 // Turn off VT100 special-case flag
1589 wrapLineFlag
= false;
1592 // "Print" the character
1593 Cell newCell
= new Cell(ch
);
1594 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1595 newCellAttributes
.setTo(currentState
.attr
);
1596 DisplayLine line
= display
.get(currentState
.cursorY
);
1598 if (StringUtils
.width(ch
) == 1) {
1599 // Insert mode special case
1600 if (insertMode
== true) {
1601 line
.insert(currentState
.cursorX
, newCell
);
1603 // Replace an existing character
1604 line
.replace(currentState
.cursorX
, newCell
);
1607 // Increment horizontal
1608 if (wrapLineFlag
== false) {
1609 currentState
.cursorX
++;
1610 if (currentState
.cursorX
> rightMargin
) {
1611 currentState
.cursorX
--;
1618 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1619 * send to the remote side.
1621 * @param mouse mouse event received from the local user
1623 private void mouse(final TMouseEvent mouse
) {
1626 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1627 mouseProtocol, mouseEncoding, mouse);
1630 if (mouseEncoding
== MouseEncoding
.X10
) {
1631 // We will support X10 but only for (160,94) and smaller.
1632 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1637 switch (mouseProtocol
) {
1644 // Only report button presses
1645 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1651 // Only report button presses and releases
1652 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1653 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1661 * Only report button presses, button releases, and motions that
1662 * have a button down (i.e. drag-and-drop).
1664 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1665 if (!mouse
.isMouse1()
1666 && !mouse
.isMouse2()
1667 && !mouse
.isMouse3()
1668 && !mouse
.isMouseWheelUp()
1669 && !mouse
.isMouseWheelDown()
1677 // Report everything
1681 // Now encode the event
1682 StringBuilder sb
= new StringBuilder(6);
1683 if (mouseEncoding
== MouseEncoding
.SGR
) {
1684 sb
.append((char) 0x1B);
1687 if (mouse
.isMouse1()) {
1688 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1693 } else if (mouse
.isMouse2()) {
1694 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1699 } else if (mouse
.isMouse3()) {
1700 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1705 } else if (mouse
.isMouseWheelUp()) {
1707 } else if (mouse
.isMouseWheelDown()) {
1710 // This is motion with no buttons down.
1714 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1717 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1724 // X10 and UTF8 encodings
1725 sb
.append((char) 0x1B);
1728 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1729 sb
.append((char) (0x03 + 32));
1730 } else if (mouse
.isMouse1()) {
1731 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1732 sb
.append((char) (0x00 + 32 + 32));
1734 sb
.append((char) (0x00 + 32));
1736 } else if (mouse
.isMouse2()) {
1737 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1738 sb
.append((char) (0x01 + 32 + 32));
1740 sb
.append((char) (0x01 + 32));
1742 } else if (mouse
.isMouse3()) {
1743 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1744 sb
.append((char) (0x02 + 32 + 32));
1746 sb
.append((char) (0x02 + 32));
1748 } else if (mouse
.isMouseWheelUp()) {
1749 sb
.append((char) (0x04 + 64));
1750 } else if (mouse
.isMouseWheelDown()) {
1751 sb
.append((char) (0x05 + 64));
1753 // This is motion with no buttons down.
1754 sb
.append((char) (0x03 + 32));
1757 sb
.append((char) (mouse
.getX() + 33));
1758 sb
.append((char) (mouse
.getY() + 33));
1761 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1762 writeRemote(sb
.toString());
1766 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1767 * send to the remote side.
1769 * @param keypress keypress received from the local user
1771 private void keypress(final TKeypress keypress
) {
1772 writeRemote(keypressToString(keypress
));
1776 * Build one of the complex xterm keystroke sequences, storing the result in
1777 * xterm_keystroke_buffer.
1779 * @param ss3 the prefix to use based on VT100 state.
1780 * @param first the first character, usually a number.
1781 * @param first the last character, one of the following: ~ A B C D F H
1782 * @param ctrl whether or not ctrl is down
1783 * @param alt whether or not alt is down
1784 * @param shift whether or not shift is down
1785 * @return the buffer with the full key sequence
1787 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1788 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1790 StringBuilder sb
= new StringBuilder(ss3
);
1791 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1795 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1797 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1799 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1801 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1803 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1805 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1807 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1812 return sb
.toString();
1816 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1818 * @param keypress keypress received from the local user
1819 * @return string to transmit to the remote side
1821 @SuppressWarnings("fallthrough")
1822 private String
keypressToString(final TKeypress keypress
) {
1824 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1826 * If this is a control character, process it like it came from
1829 if (keypress
.getChar() < 0x20) {
1830 handleControlChar((char) keypress
.getChar());
1832 // Local echo for everything else
1833 printCharacter(keypress
.getChar());
1835 if (displayListener
!= null) {
1836 displayListener
.displayChanged();
1840 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1845 // Handle control characters
1846 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1847 StringBuilder sb
= new StringBuilder();
1848 int ch
= keypress
.getChar();
1850 sb
.append(Character
.toChars(ch
));
1851 return sb
.toString();
1854 // Handle alt characters
1855 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1856 StringBuilder sb
= new StringBuilder("\033");
1857 int ch
= keypress
.getChar();
1858 sb
.append(Character
.toChars(ch
));
1859 return sb
.toString();
1862 if (keypress
.equals(kbBackspaceDel
)) {
1875 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1878 switch (arrowKeyMode
) {
1880 return xtermBuildKeySequence("\033[", '1', 'D',
1881 keypress
.isCtrl(), keypress
.isAlt(),
1882 keypress
.isShift());
1884 return xtermBuildKeySequence("\033", '1', 'D',
1885 keypress
.isCtrl(), keypress
.isAlt(),
1886 keypress
.isShift());
1888 return xtermBuildKeySequence("\033O", '1', 'D',
1889 keypress
.isCtrl(), keypress
.isAlt(),
1890 keypress
.isShift());
1893 switch (arrowKeyMode
) {
1904 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1907 switch (arrowKeyMode
) {
1909 return xtermBuildKeySequence("\033[", '1', 'C',
1910 keypress
.isCtrl(), keypress
.isAlt(),
1911 keypress
.isShift());
1913 return xtermBuildKeySequence("\033", '1', 'C',
1914 keypress
.isCtrl(), keypress
.isAlt(),
1915 keypress
.isShift());
1917 return xtermBuildKeySequence("\033O", '1', 'C',
1918 keypress
.isCtrl(), keypress
.isAlt(),
1919 keypress
.isShift());
1922 switch (arrowKeyMode
) {
1933 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1936 switch (arrowKeyMode
) {
1938 return xtermBuildKeySequence("\033[", '1', 'A',
1939 keypress
.isCtrl(), keypress
.isAlt(),
1940 keypress
.isShift());
1942 return xtermBuildKeySequence("\033", '1', 'A',
1943 keypress
.isCtrl(), keypress
.isAlt(),
1944 keypress
.isShift());
1946 return xtermBuildKeySequence("\033O", '1', 'A',
1947 keypress
.isCtrl(), keypress
.isAlt(),
1948 keypress
.isShift());
1951 switch (arrowKeyMode
) {
1962 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1965 switch (arrowKeyMode
) {
1967 return xtermBuildKeySequence("\033[", '1', 'B',
1968 keypress
.isCtrl(), keypress
.isAlt(),
1969 keypress
.isShift());
1971 return xtermBuildKeySequence("\033", '1', 'B',
1972 keypress
.isCtrl(), keypress
.isAlt(),
1973 keypress
.isShift());
1975 return xtermBuildKeySequence("\033O", '1', 'B',
1976 keypress
.isCtrl(), keypress
.isAlt(),
1977 keypress
.isShift());
1980 switch (arrowKeyMode
) {
1991 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1994 switch (arrowKeyMode
) {
1996 return xtermBuildKeySequence("\033[", '1', 'H',
1997 keypress
.isCtrl(), keypress
.isAlt(),
1998 keypress
.isShift());
2000 return xtermBuildKeySequence("\033", '1', 'H',
2001 keypress
.isCtrl(), keypress
.isAlt(),
2002 keypress
.isShift());
2004 return xtermBuildKeySequence("\033O", '1', 'H',
2005 keypress
.isCtrl(), keypress
.isAlt(),
2006 keypress
.isShift());
2009 switch (arrowKeyMode
) {
2020 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
2023 switch (arrowKeyMode
) {
2025 return xtermBuildKeySequence("\033[", '1', 'F',
2026 keypress
.isCtrl(), keypress
.isAlt(),
2027 keypress
.isShift());
2029 return xtermBuildKeySequence("\033", '1', 'F',
2030 keypress
.isCtrl(), keypress
.isAlt(),
2031 keypress
.isShift());
2033 return xtermBuildKeySequence("\033O", '1', 'F',
2034 keypress
.isCtrl(), keypress
.isAlt(),
2035 keypress
.isShift());
2038 switch (arrowKeyMode
) {
2049 if (keypress
.equals(kbF1
)) {
2057 if (keypress
.equals(kbF2
)) {
2065 if (keypress
.equals(kbF3
)) {
2073 if (keypress
.equals(kbF4
)) {
2081 if (keypress
.equals(kbF5
)) {
2094 if (keypress
.equals(kbF6
)) {
2107 if (keypress
.equals(kbF7
)) {
2120 if (keypress
.equals(kbF8
)) {
2133 if (keypress
.equals(kbF9
)) {
2146 if (keypress
.equals(kbF10
)) {
2159 if (keypress
.equals(kbF11
)) {
2163 if (keypress
.equals(kbF12
)) {
2167 if (keypress
.equals(kbShiftF1
)) {
2172 if (type
== DeviceType
.XTERM
) {
2178 if (keypress
.equals(kbShiftF2
)) {
2183 if (type
== DeviceType
.XTERM
) {
2189 if (keypress
.equals(kbShiftF3
)) {
2194 if (type
== DeviceType
.XTERM
) {
2200 if (keypress
.equals(kbShiftF4
)) {
2205 if (type
== DeviceType
.XTERM
) {
2211 if (keypress
.equals(kbShiftF5
)) {
2213 return "\033[15;2~";
2216 if (keypress
.equals(kbShiftF6
)) {
2218 return "\033[17;2~";
2221 if (keypress
.equals(kbShiftF7
)) {
2223 return "\033[18;2~";
2226 if (keypress
.equals(kbShiftF8
)) {
2228 return "\033[19;2~";
2231 if (keypress
.equals(kbShiftF9
)) {
2233 return "\033[20;2~";
2236 if (keypress
.equals(kbShiftF10
)) {
2238 return "\033[21;2~";
2241 if (keypress
.equals(kbShiftF11
)) {
2243 return "\033[23;2~";
2246 if (keypress
.equals(kbShiftF12
)) {
2248 return "\033[24;2~";
2251 if (keypress
.equals(kbCtrlF1
)) {
2256 if (type
== DeviceType
.XTERM
) {
2262 if (keypress
.equals(kbCtrlF2
)) {
2267 if (type
== DeviceType
.XTERM
) {
2273 if (keypress
.equals(kbCtrlF3
)) {
2278 if (type
== DeviceType
.XTERM
) {
2284 if (keypress
.equals(kbCtrlF4
)) {
2289 if (type
== DeviceType
.XTERM
) {
2295 if (keypress
.equals(kbCtrlF5
)) {
2297 return "\033[15;5~";
2300 if (keypress
.equals(kbCtrlF6
)) {
2302 return "\033[17;5~";
2305 if (keypress
.equals(kbCtrlF7
)) {
2307 return "\033[18;5~";
2310 if (keypress
.equals(kbCtrlF8
)) {
2312 return "\033[19;5~";
2315 if (keypress
.equals(kbCtrlF9
)) {
2317 return "\033[20;5~";
2320 if (keypress
.equals(kbCtrlF10
)) {
2322 return "\033[21;5~";
2325 if (keypress
.equals(kbCtrlF11
)) {
2327 return "\033[23;5~";
2330 if (keypress
.equals(kbCtrlF12
)) {
2332 return "\033[24;5~";
2335 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2338 return xtermBuildKeySequence("\033[", '5', '~',
2339 keypress
.isCtrl(), keypress
.isAlt(),
2340 keypress
.isShift());
2346 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2349 return xtermBuildKeySequence("\033[", '6', '~',
2350 keypress
.isCtrl(), keypress
.isAlt(),
2351 keypress
.isShift());
2357 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2360 return xtermBuildKeySequence("\033[", '2', '~',
2361 keypress
.isCtrl(), keypress
.isAlt(),
2362 keypress
.isShift());
2368 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2371 return xtermBuildKeySequence("\033[", '3', '~',
2372 keypress
.isCtrl(), keypress
.isAlt(),
2373 keypress
.isShift());
2375 // Delete sends real delete for VTxxx
2380 if (keypress
.equals(kbEnter
)) {
2384 if (keypress
.equals(kbEsc
)) {
2388 if (keypress
.equals(kbAltEsc
)) {
2392 if (keypress
.equals(kbTab
)) {
2396 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2397 (keypress
.equals(kbShiftTab
))
2407 // Non-alt, non-ctrl characters
2408 if (!keypress
.isFnKey()) {
2409 StringBuilder sb
= new StringBuilder();
2410 sb
.append(Character
.toChars(keypress
.getChar()));
2411 return sb
.toString();
2417 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2420 * @param ch 8-bit character from the remote side
2421 * @param charsetGl character set defined for GL
2422 * @param charsetGr character set defined for GR
2423 * @return character to display on the screen
2425 private char mapCharacterCharset(final int ch
,
2426 final CharacterSet charsetGl
,
2427 final CharacterSet charsetGr
) {
2429 int lookupChar
= ch
;
2430 CharacterSet lookupCharset
= charsetGl
;
2433 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2434 lookupCharset
= charsetGr
;
2438 switch (lookupCharset
) {
2441 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2444 return DECCharacterSets
.UK
[lookupChar
];
2447 return DECCharacterSets
.US_ASCII
[lookupChar
];
2450 return DECCharacterSets
.NL
[lookupChar
];
2453 return DECCharacterSets
.FI
[lookupChar
];
2456 return DECCharacterSets
.FR
[lookupChar
];
2459 return DECCharacterSets
.FR_CA
[lookupChar
];
2462 return DECCharacterSets
.DE
[lookupChar
];
2465 return DECCharacterSets
.IT
[lookupChar
];
2468 return DECCharacterSets
.NO
[lookupChar
];
2471 return DECCharacterSets
.ES
[lookupChar
];
2474 return DECCharacterSets
.SV
[lookupChar
];
2477 return DECCharacterSets
.SWISS
[lookupChar
];
2479 case DEC_SUPPLEMENTAL
:
2480 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2483 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2486 return DECCharacterSets
.US_ASCII
[lookupChar
];
2489 return DECCharacterSets
.US_ASCII
[lookupChar
];
2492 throw new IllegalArgumentException("Invalid character set value: "
2498 * Map an 8-bit byte into a printable character.
2500 * @param ch either 8-bit or Unicode character from the remote side
2501 * @return character to display on the screen
2503 private int mapCharacter(final int ch
) {
2505 // Unicode character, just return it
2509 CharacterSet charsetGl
= currentState
.g0Charset
;
2510 CharacterSet charsetGr
= currentState
.grCharset
;
2512 if (vt52Mode
== true) {
2513 if (shiftOut
== true) {
2514 // Shifted out character, pull from VT52 graphics
2515 charsetGl
= currentState
.g1Charset
;
2516 charsetGr
= CharacterSet
.US
;
2519 charsetGl
= currentState
.g0Charset
;
2520 charsetGr
= CharacterSet
.US
;
2523 // Pull the character
2524 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2528 if (shiftOut
== true) {
2529 // Shifted out character, pull from G1
2530 charsetGl
= currentState
.g1Charset
;
2531 charsetGr
= currentState
.grCharset
;
2533 // Pull the character
2534 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2538 if (singleshift
== Singleshift
.SS2
) {
2540 singleshift
= Singleshift
.NONE
;
2542 // Shifted out character, pull from G2
2543 charsetGl
= currentState
.g2Charset
;
2544 charsetGr
= currentState
.grCharset
;
2548 if (singleshift
== Singleshift
.SS3
) {
2550 singleshift
= Singleshift
.NONE
;
2552 // Shifted out character, pull from G3
2553 charsetGl
= currentState
.g3Charset
;
2554 charsetGr
= currentState
.grCharset
;
2557 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2558 // Check for locking shift
2560 switch (currentState
.glLockshift
) {
2563 throw new IllegalArgumentException("programming bug");
2566 throw new IllegalArgumentException("programming bug");
2569 throw new IllegalArgumentException("programming bug");
2573 charsetGl
= currentState
.g2Charset
;
2578 charsetGl
= currentState
.g3Charset
;
2583 charsetGl
= currentState
.g0Charset
;
2587 switch (currentState
.grLockshift
) {
2590 throw new IllegalArgumentException("programming bug");
2593 throw new IllegalArgumentException("programming bug");
2597 charsetGr
= currentState
.g1Charset
;
2602 charsetGr
= currentState
.g2Charset
;
2607 charsetGr
= currentState
.g3Charset
;
2612 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2619 // Pull the character
2620 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2624 * Scroll the text within a scrolling region up n lines.
2626 * @param regionTop top row of the scrolling region
2627 * @param regionBottom bottom row of the scrolling region
2628 * @param n number of lines to scroll
2630 private void scrollingRegionScrollUp(final int regionTop
,
2631 final int regionBottom
, final int n
) {
2633 if (regionTop
>= regionBottom
) {
2637 // Sanity check: see if there will be any characters left after the
2639 if (regionBottom
+ 1 - regionTop
<= n
) {
2640 // There won't be anything left in the region, so just call
2641 // eraseScreen() and return.
2642 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2646 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2647 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2648 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2650 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2651 - remaining
, regionBottom
+ 1);
2652 display
= new ArrayList
<DisplayLine
>(displayTop
);
2653 display
.addAll(displayMiddle
);
2654 for (int i
= 0; i
< n
; i
++) {
2655 DisplayLine line
= new DisplayLine(currentState
.attr
);
2656 line
.setReverseColor(reverseVideo
);
2659 display
.addAll(displayBottom
);
2661 assert (display
.size() == height
);
2665 * Scroll the text within a scrolling region down n lines.
2667 * @param regionTop top row of the scrolling region
2668 * @param regionBottom bottom row of the scrolling region
2669 * @param n number of lines to scroll
2671 private void scrollingRegionScrollDown(final int regionTop
,
2672 final int regionBottom
, final int n
) {
2674 if (regionTop
>= regionBottom
) {
2678 // Sanity check: see if there will be any characters left after the
2680 if (regionBottom
+ 1 - regionTop
<= n
) {
2681 // There won't be anything left in the region, so just call
2682 // eraseScreen() and return.
2683 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2687 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2688 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2689 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2691 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2692 regionTop
+ remaining
);
2693 display
= new ArrayList
<DisplayLine
>(displayTop
);
2694 for (int i
= 0; i
< n
; i
++) {
2695 DisplayLine line
= new DisplayLine(currentState
.attr
);
2696 line
.setReverseColor(reverseVideo
);
2699 display
.addAll(displayMiddle
);
2700 display
.addAll(displayBottom
);
2702 assert (display
.size() == height
);
2706 * Process a control character.
2708 * @param ch 8-bit character from the remote side
2710 private void handleControlChar(final char ch
) {
2711 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2722 // Transmit the answerback message.
2733 cursorLeft(1, false);
2738 advanceToNextTabStop();
2764 currentState
.glLockshift
= LockshiftMode
.NONE
;
2770 currentState
.glLockshift
= LockshiftMode
.NONE
;
2795 singleshift
= Singleshift
.SS2
;
2800 singleshift
= Singleshift
.SS3
;
2810 * Advance the cursor to the next tab stop.
2812 private void advanceToNextTabStop() {
2813 if (tabStops
.size() == 0) {
2814 // Go to the rightmost column
2815 cursorRight(rightMargin
- currentState
.cursorX
, false);
2818 for (Integer stop
: tabStops
) {
2819 if (stop
> currentState
.cursorX
) {
2820 cursorRight(stop
- currentState
.cursorX
, false);
2825 * We got here, meaning there isn't a tab stop beyond the current
2826 * cursor position. Place the cursor of the right-most edge of the
2829 cursorRight(rightMargin
- currentState
.cursorX
, false);
2833 * Save a character into the collect buffer.
2835 * @param ch character to save
2837 private void collect(final char ch
) {
2838 collectBuffer
.append(ch
);
2842 * Save a byte into the CSI parameters buffer.
2844 * @param ch byte to save
2846 private void param(final byte ch
) {
2847 if (csiParams
.size() == 0) {
2848 csiParams
.add(Integer
.valueOf(0));
2850 Integer x
= csiParams
.get(csiParams
.size() - 1);
2851 if ((ch
>= '0') && (ch
<= '9')) {
2854 csiParams
.set(csiParams
.size() - 1, x
);
2857 if ((ch
== ';') && (csiParams
.size() < 16)) {
2858 csiParams
.add(Integer
.valueOf(0));
2863 * Get a CSI parameter value, with a default.
2865 * @param position parameter index. 0 is the first parameter.
2866 * @param defaultValue value to use if csiParams[position] doesn't exist
2867 * @return parameter value
2869 private int getCsiParam(final int position
, final int defaultValue
) {
2870 if (csiParams
.size() < position
+ 1) {
2871 return defaultValue
;
2873 return csiParams
.get(position
).intValue();
2877 * Get a CSI parameter value, clamped to within min/max.
2879 * @param position parameter index. 0 is the first parameter.
2880 * @param defaultValue value to use if csiParams[position] doesn't exist
2881 * @param minValue minimum value inclusive
2882 * @param maxValue maximum value inclusive
2883 * @return parameter value
2885 private int getCsiParam(final int position
, final int defaultValue
,
2886 final int minValue
, final int maxValue
) {
2888 assert (minValue
<= maxValue
);
2889 int value
= getCsiParam(position
, defaultValue
);
2890 if (value
< minValue
) {
2893 if (value
> maxValue
) {
2900 * Set or unset a toggle.
2902 * @param value true for set ('h'), false for reset ('l')
2904 private void setToggle(final boolean value
) {
2905 boolean decPrivateModeFlag
= false;
2907 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2908 if (collectBuffer
.charAt(i
) == '?') {
2909 decPrivateModeFlag
= true;
2914 for (Integer i
: csiParams
) {
2919 if (decPrivateModeFlag
== true) {
2921 if (value
== true) {
2922 // Use application arrow keys
2923 arrowKeyMode
= ArrowKeyMode
.VT100
;
2925 // Use ANSI arrow keys
2926 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2931 if (decPrivateModeFlag
== true) {
2932 if (value
== false) {
2936 arrowKeyMode
= ArrowKeyMode
.VT52
;
2939 * From the VT102 docs: "You use ANSI mode to select
2940 * most terminal features; the terminal uses the same
2941 * features when it switches to VT52 mode. You
2942 * cannot, however, change most of these features in
2945 * In other words, do not reset any other attributes
2946 * when switching between VT52 submode and ANSI.
2948 * HOWEVER, the real vt100 does switch the character
2949 * set according to Usenet.
2951 currentState
.g0Charset
= CharacterSet
.US
;
2952 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2955 if ((type
== DeviceType
.VT220
)
2956 || (type
== DeviceType
.XTERM
)) {
2958 // VT52 mode is explicitly 7-bit
2960 singleshift
= Singleshift
.NONE
;
2965 if (value
== true) {
2966 // Turn off keyboard
2975 if (decPrivateModeFlag
== true) {
2977 if (value
== true) {
2984 if ((displayListener
!= null)
2985 && (type
== DeviceType
.XTERM
)
2987 // For xterms, reset to the actual width, not 80
2989 width
= displayListener
.getDisplayWidth();
2990 rightMargin
= width
- 1;
2993 width
= rightMargin
+ 1;
2996 // Entire screen is cleared, and scrolling region is
2998 eraseScreen(0, 0, height
- 1, width
- 1, false);
2999 scrollRegionTop
= 0;
3000 scrollRegionBottom
= height
- 1;
3001 // Also home the cursor
3002 cursorPosition(0, 0);
3006 if (decPrivateModeFlag
== true) {
3008 if (value
== true) {
3017 if (value
== true) {
3025 if (decPrivateModeFlag
== true) {
3027 if (value
== true) {
3029 * Set selects reverse screen, a white screen
3030 * background with black characters.
3032 if (reverseVideo
!= true) {
3034 * If in normal video, switch it back
3036 invertDisplayColors();
3038 reverseVideo
= true;
3041 * Reset selects normal screen, a black screen
3042 * background with white characters.
3044 if (reverseVideo
== true) {
3046 * If in reverse video already, switch it back
3048 invertDisplayColors();
3050 reverseVideo
= false;
3055 if (decPrivateModeFlag
== true) {
3057 if (value
== true) {
3058 // Origin is relative to scroll region cursor.
3059 // Cursor can NEVER leave scrolling region.
3060 currentState
.originMode
= true;
3061 cursorPosition(0, 0);
3063 // Origin is absolute to entire screen. Cursor can
3064 // leave the scrolling region via cup() and hvp().
3065 currentState
.originMode
= false;
3066 cursorPosition(0, 0);
3071 if (decPrivateModeFlag
== true) {
3073 if (value
== true) {
3075 currentState
.lineWrap
= true;
3077 // Turn linewrap off
3078 currentState
.lineWrap
= false;
3083 if (decPrivateModeFlag
== true) {
3085 if (value
== true) {
3086 // Keyboard auto-repeat on
3089 // Keyboard auto-repeat off
3095 if (decPrivateModeFlag
== false) {
3097 if (value
== true) {
3107 if (decPrivateModeFlag
== true) {
3113 if (decPrivateModeFlag
== true) {
3119 if (decPrivateModeFlag
== false) {
3121 if (value
== true) {
3123 * Set causes a received linefeed, form feed, or
3124 * vertical tab to move cursor to first column of
3125 * next line. RETURN transmits both a carriage return
3126 * and linefeed. This selection is also called new
3132 * Reset causes a received linefeed, form feed, or
3133 * vertical tab to move cursor to next line in
3134 * current column. RETURN transmits a carriage
3137 newLineMode
= false;
3143 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3144 if (decPrivateModeFlag
== true) {
3146 if (value
== true) {
3148 cursorVisible
= true;
3151 cursorVisible
= false;
3158 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3159 if (decPrivateModeFlag
== true) {
3161 if (value
== true) {
3162 // Select national mode NRC
3165 // Select multi-national mode
3174 if (type
== DeviceType
.XTERM
) {
3175 if (decPrivateModeFlag
== true) {
3176 if (value
== true) {
3177 // Enable sixel scrolling (default).
3180 // Disable sixel scrolling.
3189 if ((type
== DeviceType
.XTERM
)
3190 && (decPrivateModeFlag
== true)
3192 // Mouse: normal tracking mode
3193 if (value
== true) {
3194 mouseProtocol
= MouseProtocol
.NORMAL
;
3196 mouseProtocol
= MouseProtocol
.OFF
;
3202 if ((type
== DeviceType
.XTERM
)
3203 && (decPrivateModeFlag
== true)
3205 // Mouse: normal tracking mode
3206 if (value
== true) {
3207 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
3209 mouseProtocol
= MouseProtocol
.OFF
;
3215 if ((type
== DeviceType
.XTERM
)
3216 && (decPrivateModeFlag
== true)
3218 // Mouse: Any-event tracking mode
3219 if (value
== true) {
3220 mouseProtocol
= MouseProtocol
.ANYEVENT
;
3222 mouseProtocol
= MouseProtocol
.OFF
;
3228 if ((type
== DeviceType
.XTERM
)
3229 && (decPrivateModeFlag
== true)
3231 // Mouse: UTF-8 coordinates
3232 if (value
== true) {
3233 mouseEncoding
= MouseEncoding
.UTF8
;
3235 mouseEncoding
= MouseEncoding
.X10
;
3241 if ((type
== DeviceType
.XTERM
)
3242 && (decPrivateModeFlag
== true)
3244 // Mouse: SGR coordinates
3245 if (value
== true) {
3246 mouseEncoding
= MouseEncoding
.SGR
;
3248 mouseEncoding
= MouseEncoding
.X10
;
3254 if (type
== DeviceType
.XTERM
) {
3255 if (decPrivateModeFlag
== true) {
3256 if (value
== true) {
3257 // Use private color registers for each sixel
3258 // graphic (default).
3259 sixelPalette
= null;
3261 // Use shared color registers for each sixel
3263 sixelPalette
= new HashMap
<Integer
, java
.awt
.Color
>();
3277 * DECSC - Save cursor.
3279 private void decsc() {
3280 savedState
.setTo(currentState
);
3284 * DECRC - Restore cursor.
3286 private void decrc() {
3287 currentState
.setTo(savedState
);
3293 private void ind() {
3294 // Move the cursor and scroll if necessary. If at the bottom line
3295 // already, a scroll up is supposed to be performed.
3296 if (currentState
.cursorY
== scrollRegionBottom
) {
3297 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3299 cursorDown(1, true);
3303 * RI - Reverse index.
3306 // Move the cursor and scroll if necessary. If at the top line
3307 // already, a scroll down is supposed to be performed.
3308 if (currentState
.cursorY
== scrollRegionTop
) {
3309 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3317 private void nel() {
3318 // Move the cursor and scroll if necessary. If at the bottom line
3319 // already, a scroll up is supposed to be performed.
3320 if (currentState
.cursorY
== scrollRegionBottom
) {
3321 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3323 cursorDown(1, true);
3325 // Reset to the beginning of the next line
3326 currentState
.cursorX
= 0;
3330 * DECKPAM - Keypad application mode.
3332 private void deckpam() {
3333 keypadMode
= KeypadMode
.Application
;
3337 * DECKPNM - Keypad numeric mode.
3339 private void deckpnm() {
3340 keypadMode
= KeypadMode
.Numeric
;
3346 * @param n number of spaces to move
3347 * @param honorScrollRegion if true, then do nothing if the cursor is
3348 * outside the scrolling region
3350 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3354 * Special case: if a user moves the cursor from the right margin, we
3355 * have to reset the VT100 right margin flag.
3358 wrapLineFlag
= false;
3361 for (int i
= 0; i
< n
; i
++) {
3362 if (honorScrollRegion
== true) {
3363 // Honor the scrolling region
3364 if ((currentState
.cursorY
< scrollRegionTop
)
3365 || (currentState
.cursorY
> scrollRegionBottom
)
3367 // Outside region, do nothing
3370 // Inside region, go up
3371 top
= scrollRegionTop
;
3373 // Non-scrolling case
3377 if (currentState
.cursorY
> top
) {
3378 currentState
.cursorY
--;
3384 * Move down n spaces.
3386 * @param n number of spaces to move
3387 * @param honorScrollRegion if true, then do nothing if the cursor is
3388 * outside the scrolling region
3390 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3394 * Special case: if a user moves the cursor from the right margin, we
3395 * have to reset the VT100 right margin flag.
3398 wrapLineFlag
= false;
3401 for (int i
= 0; i
< n
; i
++) {
3403 if (honorScrollRegion
== true) {
3404 // Honor the scrolling region
3405 if (currentState
.cursorY
> scrollRegionBottom
) {
3406 // Outside region, do nothing
3409 // Inside region, go down
3410 bottom
= scrollRegionBottom
;
3412 // Non-scrolling case
3413 bottom
= height
- 1;
3416 if (currentState
.cursorY
< bottom
) {
3417 currentState
.cursorY
++;
3423 * Move left n spaces.
3425 * @param n number of spaces to move
3426 * @param honorScrollRegion if true, then do nothing if the cursor is
3427 * outside the scrolling region
3429 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3431 * Special case: if a user moves the cursor from the right margin, we
3432 * have to reset the VT100 right margin flag.
3435 wrapLineFlag
= false;
3438 for (int i
= 0; i
< n
; i
++) {
3439 if (honorScrollRegion
== true) {
3440 // Honor the scrolling region
3441 if ((currentState
.cursorY
< scrollRegionTop
)
3442 || (currentState
.cursorY
> scrollRegionBottom
)
3444 // Outside region, do nothing
3449 if (currentState
.cursorX
> 0) {
3450 currentState
.cursorX
--;
3456 * Move right n spaces.
3458 * @param n number of spaces to move
3459 * @param honorScrollRegion if true, then do nothing if the cursor is
3460 * outside the scrolling region
3462 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3463 int rightMargin
= this.rightMargin
;
3466 * Special case: if a user moves the cursor from the right margin, we
3467 * have to reset the VT100 right margin flag.
3470 wrapLineFlag
= false;
3473 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3474 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3477 for (int i
= 0; i
< n
; i
++) {
3478 if (honorScrollRegion
== true) {
3479 // Honor the scrolling region
3480 if ((currentState
.cursorY
< scrollRegionTop
)
3481 || (currentState
.cursorY
> scrollRegionBottom
)
3483 // Outside region, do nothing
3488 if (currentState
.cursorX
< rightMargin
) {
3489 currentState
.cursorX
++;
3495 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3497 * @param row row to move to
3498 * @param col column to move to
3500 private void cursorPosition(int row
, final int col
) {
3501 int rightMargin
= this.rightMargin
;
3506 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3507 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3510 // Set column number
3511 currentState
.cursorX
= col
;
3513 // Sanity check, bring column back to margin.
3514 if (currentState
.cursorX
> rightMargin
) {
3515 currentState
.cursorX
= rightMargin
;
3519 if (currentState
.originMode
== true) {
3520 row
+= scrollRegionTop
;
3522 if (currentState
.cursorY
< row
) {
3523 cursorDown(row
- currentState
.cursorY
, false);
3524 } else if (currentState
.cursorY
> row
) {
3525 cursorUp(currentState
.cursorY
- row
, false);
3528 wrapLineFlag
= false;
3532 * HTS - Horizontal tabulation set.
3534 private void hts() {
3535 for (Integer stop
: tabStops
) {
3536 if (stop
== currentState
.cursorX
) {
3537 // Already have a tab stop here
3542 // Append a tab stop to the end of the array and resort them
3543 tabStops
.add(currentState
.cursorX
);
3544 Collections
.sort(tabStops
);
3548 * DECSWL - Single-width line.
3550 private void decswl() {
3551 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3552 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3556 * DECDWL - Double-width line.
3558 private void decdwl() {
3559 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3560 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3564 * DECHDL - Double-height + double-width line.
3566 * @param topHalf if true, this sets the row to be the top half row of a
3569 private void dechdl(final boolean topHalf
) {
3570 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3571 if (topHalf
== true) {
3572 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3574 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3579 * DECALN - Screen alignment display.
3581 private void decaln() {
3582 Cell newCell
= new Cell('E');
3583 for (DisplayLine line
: display
) {
3584 for (int i
= 0; i
< line
.length(); i
++) {
3585 line
.replace(i
, newCell
);
3591 * DECSCL - Compatibility level.
3593 private void decscl() {
3594 int i
= getCsiParam(0, 0);
3595 int j
= getCsiParam(1, 0);
3599 currentState
.g0Charset
= CharacterSet
.US
;
3600 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3602 } else if (i
== 62) {
3604 if ((j
== 0) || (j
== 2)) {
3606 } else if (j
== 1) {
3613 * CUD - Cursor down.
3615 private void cud() {
3616 cursorDown(getCsiParam(0, 1, 1, height
), true);
3620 * CUF - Cursor forward.
3622 private void cuf() {
3623 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3627 * CUB - Cursor backward.
3629 private void cub() {
3630 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3636 private void cuu() {
3637 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3641 * CUP - Cursor position.
3643 private void cup() {
3644 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3645 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3649 * CNL - Cursor down and to column 1.
3651 private void cnl() {
3652 cursorDown(getCsiParam(0, 1, 1, height
), true);
3654 cursorLeft(currentState
.cursorX
, true);
3658 * CPL - Cursor up and to column 1.
3660 private void cpl() {
3661 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3663 cursorLeft(currentState
.cursorX
, true);
3667 * CHA - Cursor to column # in current row.
3669 private void cha() {
3670 cursorPosition(currentState
.cursorY
,
3671 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3675 * VPA - Cursor to row #, same column.
3677 private void vpa() {
3678 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3679 currentState
.cursorX
);
3683 * ED - Erase in display.
3686 boolean honorProtected
= false;
3687 boolean decPrivateModeFlag
= false;
3689 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3690 if (collectBuffer
.charAt(i
) == '?') {
3691 decPrivateModeFlag
= true;
3696 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3697 && (decPrivateModeFlag
== true)
3699 honorProtected
= true;
3702 int i
= getCsiParam(0, 0);
3705 // Erase from here to end of screen
3706 if (currentState
.cursorY
< height
- 1) {
3707 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3710 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3711 } else if (i
== 1) {
3712 // Erase from beginning of screen to here
3713 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3715 eraseLine(0, currentState
.cursorX
, honorProtected
);
3716 } else if (i
== 2) {
3717 // Erase entire screen
3718 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3723 * EL - Erase in line.
3726 boolean honorProtected
= false;
3727 boolean decPrivateModeFlag
= false;
3729 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3730 if (collectBuffer
.charAt(i
) == '?') {
3731 decPrivateModeFlag
= true;
3736 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3737 && (decPrivateModeFlag
== true)
3739 honorProtected
= true;
3742 int i
= getCsiParam(0, 0);
3745 // Erase from here to end of line
3746 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3747 } else if (i
== 1) {
3748 // Erase from beginning of line to here
3749 eraseLine(0, currentState
.cursorX
, honorProtected
);
3750 } else if (i
== 2) {
3751 // Erase entire line
3752 eraseLine(0, width
- 1, honorProtected
);
3757 * ECH - Erase # of characters in current row.
3759 private void ech() {
3760 int i
= getCsiParam(0, 1, 1, width
);
3762 // Erase from here to i characters
3763 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3770 int i
= getCsiParam(0, 1);
3772 if ((currentState
.cursorY
>= scrollRegionTop
)
3773 && (currentState
.cursorY
<= scrollRegionBottom
)
3776 // I can get the same effect with a scroll-down
3777 scrollingRegionScrollDown(currentState
.cursorY
,
3778 scrollRegionBottom
, i
);
3783 * DCH - Delete char.
3785 private void dch() {
3786 int n
= getCsiParam(0, 1);
3787 DisplayLine line
= display
.get(currentState
.cursorY
);
3788 Cell blank
= new Cell();
3789 for (int i
= 0; i
< n
; i
++) {
3790 line
.delete(currentState
.cursorX
, blank
);
3795 * ICH - Insert blank char at cursor.
3797 private void ich() {
3798 int n
= getCsiParam(0, 1);
3799 DisplayLine line
= display
.get(currentState
.cursorY
);
3800 Cell blank
= new Cell();
3801 for (int i
= 0; i
< n
; i
++) {
3802 line
.insert(currentState
.cursorX
, blank
);
3810 int i
= getCsiParam(0, 1);
3812 if ((currentState
.cursorY
>= scrollRegionTop
)
3813 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3815 // I can get the same effect with a scroll-down
3816 scrollingRegionScrollUp(currentState
.cursorY
,
3817 scrollRegionBottom
, i
);
3822 * HVP - Horizontal and vertical position.
3824 private void hvp() {
3829 * REP - Repeat character.
3831 private void rep() {
3832 int n
= getCsiParam(0, 1);
3833 for (int i
= 0; i
< n
; i
++) {
3834 printCharacter(repCh
);
3842 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3843 getCsiParam(0, 1, 1, height
));
3850 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3851 getCsiParam(0, 1, 1, height
));
3855 * CBT - Go back X tab stops.
3857 private void cbt() {
3858 int tabsToMove
= getCsiParam(0, 1);
3861 for (int i
= 0; i
< tabsToMove
; i
++) {
3862 int j
= currentState
.cursorX
;
3863 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3864 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3872 j
= tabStops
.get(tabI
);
3874 cursorPosition(currentState
.cursorY
, j
);
3879 * CHT - Advance X tab stops.
3881 private void cht() {
3882 int n
= getCsiParam(0, 1);
3883 for (int i
= 0; i
< n
; i
++) {
3884 advanceToNextTabStop();
3889 * SGR - Select graphics rendition.
3891 private void sgr() {
3893 if (csiParams
.size() == 0) {
3894 currentState
.attr
.reset();
3898 int sgrColorMode
= -1;
3899 boolean idx88Color
= false;
3900 boolean rgbColor
= false;
3904 for (Integer i
: csiParams
) {
3906 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
3908 assert (type
== DeviceType
.XTERM
);
3912 * Indexed color mode, we now have the index number.
3914 if (sgrColorMode
== 38) {
3915 currentState
.attr
.setForeColorRGB(get88Color(i
));
3917 assert (sgrColorMode
== 48);
3918 currentState
.attr
.setBackColorRGB(get88Color(i
));
3927 * RGB color mode, we are collecting tokens.
3931 } else if (rgbGreen
== -1) {
3932 rgbGreen
= i
& 0xFF;
3934 int rgb
= rgbRed
<< 16;
3935 rgb
|= rgbGreen
<< 8;
3938 // System.err.printf("RGB: %08x\n", rgb);
3940 if (sgrColorMode
== 38) {
3941 currentState
.attr
.setForeColorRGB(rgb
);
3943 assert (sgrColorMode
== 48);
3944 currentState
.attr
.setBackColorRGB(rgb
);
3965 * Indexed color mode.
3972 * This is neither indexed nor RGB color. Bail out.
3977 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
3983 currentState
.attr
.reset();
3988 currentState
.attr
.setBold(true);
3993 currentState
.attr
.setUnderline(true);
3998 currentState
.attr
.setBlink(true);
4003 currentState
.attr
.setReverse(true);
4010 if (type
== DeviceType
.XTERM
) {
4020 // Set black foreground
4021 currentState
.attr
.setForeColorRGB(get88Color(8));
4024 // Set red foreground
4025 currentState
.attr
.setForeColorRGB(get88Color(9));
4028 // Set green foreground
4029 currentState
.attr
.setForeColorRGB(get88Color(10));
4032 // Set yellow foreground
4033 currentState
.attr
.setForeColorRGB(get88Color(11));
4036 // Set blue foreground
4037 currentState
.attr
.setForeColorRGB(get88Color(12));
4040 // Set magenta foreground
4041 currentState
.attr
.setForeColorRGB(get88Color(13));
4044 // Set cyan foreground
4045 currentState
.attr
.setForeColorRGB(get88Color(14));
4048 // Set white foreground
4049 currentState
.attr
.setForeColorRGB(get88Color(15));
4053 // Set black background
4054 currentState
.attr
.setBackColorRGB(get88Color(8));
4057 // Set red background
4058 currentState
.attr
.setBackColorRGB(get88Color(9));
4061 // Set green background
4062 currentState
.attr
.setBackColorRGB(get88Color(10));
4065 // Set yellow background
4066 currentState
.attr
.setBackColorRGB(get88Color(11));
4069 // Set blue background
4070 currentState
.attr
.setBackColorRGB(get88Color(12));
4073 // Set magenta background
4074 currentState
.attr
.setBackColorRGB(get88Color(13));
4077 // Set cyan background
4078 currentState
.attr
.setBackColorRGB(get88Color(14));
4081 // Set white background
4082 currentState
.attr
.setBackColorRGB(get88Color(15));
4090 if ((type
== DeviceType
.VT220
)
4091 || (type
== DeviceType
.XTERM
)) {
4097 currentState
.attr
.setBold(false);
4102 currentState
.attr
.setUnderline(false);
4107 currentState
.attr
.setBlink(false);
4112 currentState
.attr
.setReverse(false);
4120 // A true VT100/102/220 does not support color, however everyone
4121 // is used to their terminal emulator supporting color so we will
4122 // unconditionally support color for all DeviceType's.
4127 // Set black foreground
4128 currentState
.attr
.setForeColor(Color
.BLACK
);
4129 currentState
.attr
.setForeColorRGB(-1);
4132 // Set red foreground
4133 currentState
.attr
.setForeColor(Color
.RED
);
4134 currentState
.attr
.setForeColorRGB(-1);
4137 // Set green foreground
4138 currentState
.attr
.setForeColor(Color
.GREEN
);
4139 currentState
.attr
.setForeColorRGB(-1);
4142 // Set yellow foreground
4143 currentState
.attr
.setForeColor(Color
.YELLOW
);
4144 currentState
.attr
.setForeColorRGB(-1);
4147 // Set blue foreground
4148 currentState
.attr
.setForeColor(Color
.BLUE
);
4149 currentState
.attr
.setForeColorRGB(-1);
4152 // Set magenta foreground
4153 currentState
.attr
.setForeColor(Color
.MAGENTA
);
4154 currentState
.attr
.setForeColorRGB(-1);
4157 // Set cyan foreground
4158 currentState
.attr
.setForeColor(Color
.CYAN
);
4159 currentState
.attr
.setForeColorRGB(-1);
4162 // Set white foreground
4163 currentState
.attr
.setForeColor(Color
.WHITE
);
4164 currentState
.attr
.setForeColorRGB(-1);
4167 if (type
== DeviceType
.XTERM
) {
4169 * Xterm supports T.416 / ISO-8613-3 codes to select
4170 * either an indexed color or an RGB value. (It also
4171 * permits these ISO-8613-3 SGR sequences to be separated
4172 * by colons rather than semicolons.)
4174 * We will support only the following:
4176 * 1. Indexed color mode (88- or 256-color modes).
4180 * These cover most of the use cases in the real world.
4182 * HOWEVER, note that this is an awful broken "standard",
4183 * with no way to do it "right". See
4184 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4185 * for a detailed discussion of the current state of RGB
4186 * in various terminals, the point of which is that none
4187 * of them really do the same thing despite all appearing
4191 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4192 * where it is assumed that supporting just the "indexed
4193 * mode" of these sequences (which could align easily
4194 * with existing SGR colors) is assumed to mean full
4195 * support of 24-bit RGB. So it is all or nothing.
4197 * Finally, these sequences break the assumptions of
4198 * standard ECMA-48 style parsers as pointed out at
4199 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4200 * Therefore in order to keep a clean display, we cannot
4201 * parse anything else in this sequence.
4206 // Underscore on, default foreground color
4207 currentState
.attr
.setUnderline(true);
4208 currentState
.attr
.setForeColor(Color
.WHITE
);
4212 // Underscore off, default foreground color
4213 currentState
.attr
.setUnderline(false);
4214 currentState
.attr
.setForeColor(Color
.WHITE
);
4215 currentState
.attr
.setForeColorRGB(-1);
4218 // Set black background
4219 currentState
.attr
.setBackColor(Color
.BLACK
);
4220 currentState
.attr
.setBackColorRGB(-1);
4223 // Set red background
4224 currentState
.attr
.setBackColor(Color
.RED
);
4225 currentState
.attr
.setBackColorRGB(-1);
4228 // Set green background
4229 currentState
.attr
.setBackColor(Color
.GREEN
);
4230 currentState
.attr
.setBackColorRGB(-1);
4233 // Set yellow background
4234 currentState
.attr
.setBackColor(Color
.YELLOW
);
4235 currentState
.attr
.setBackColorRGB(-1);
4238 // Set blue background
4239 currentState
.attr
.setBackColor(Color
.BLUE
);
4240 currentState
.attr
.setBackColorRGB(-1);
4243 // Set magenta background
4244 currentState
.attr
.setBackColor(Color
.MAGENTA
);
4245 currentState
.attr
.setBackColorRGB(-1);
4248 // Set cyan background
4249 currentState
.attr
.setBackColor(Color
.CYAN
);
4250 currentState
.attr
.setBackColorRGB(-1);
4253 // Set white background
4254 currentState
.attr
.setBackColor(Color
.WHITE
);
4255 currentState
.attr
.setBackColorRGB(-1);
4258 if (type
== DeviceType
.XTERM
) {
4260 * Xterm supports T.416 / ISO-8613-3 codes to select
4261 * either an indexed color or an RGB value. (It also
4262 * permits these ISO-8613-3 SGR sequences to be separated
4263 * by colons rather than semicolons.)
4265 * We will support only the following:
4267 * 1. Indexed color mode (88- or 256-color modes).
4271 * These cover most of the use cases in the real world.
4273 * HOWEVER, note that this is an awful broken "standard",
4274 * with no way to do it "right". See
4275 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4276 * for a detailed discussion of the current state of RGB
4277 * in various terminals, the point of which is that none
4278 * of them really do the same thing despite all appearing
4282 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4283 * where it is assumed that supporting just the "indexed
4284 * mode" of these sequences (which could align easily
4285 * with existing SGR colors) is assumed to mean full
4286 * support of 24-bit RGB. So it is all or nothing.
4288 * Finally, these sequences break the assumptions of
4289 * standard ECMA-48 style parsers as pointed out at
4290 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4291 * Therefore in order to keep a clean display, we cannot
4292 * parse anything else in this sequence.
4299 // Default background
4300 currentState
.attr
.setBackColor(Color
.BLACK
);
4301 currentState
.attr
.setBackColorRGB(-1);
4311 * DA - Device attributes.
4314 int extendedFlag
= 0;
4316 if (collectBuffer
.length() > 0) {
4317 String args
= collectBuffer
.substring(1);
4318 if (collectBuffer
.charAt(0) == '>') {
4320 if (collectBuffer
.length() >= 2) {
4321 i
= Integer
.parseInt(args
);
4323 } else if (collectBuffer
.charAt(0) == '=') {
4325 if (collectBuffer
.length() >= 2) {
4326 i
= Integer
.parseInt(args
);
4329 // Unknown code, bail out
4334 if ((i
!= 0) && (i
!= 1)) {
4338 if ((extendedFlag
== 0) && (i
== 0)) {
4339 // Send string directly to remote side
4340 writeRemote(deviceTypeResponse());
4344 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4346 if ((extendedFlag
== 1) && (i
== 0)) {
4348 * Request "What type of terminal are you, what is your
4349 * firmware version, and what hardware options do you have
4352 * Respond: "I am a VT220 (identification code of 1), my
4353 * firmware version is _____ (Pv), and I have _____ Po
4354 * options installed."
4360 if (s8c1t
== true) {
4361 writeRemote("\u009b>1;10;0c");
4363 writeRemote("\033[>1;10;0c");
4369 if ((extendedFlag
== 2) && (i
== 0)) {
4372 * Request "What is your unit ID?"
4374 * Respond: "I was manufactured at site 00 and have a unique ID
4378 writeRemote("\033P!|00010203\033\\");
4383 * DECSTBM - Set top and bottom margins.
4385 private void decstbm() {
4386 boolean decPrivateModeFlag
= false;
4388 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4389 if (collectBuffer
.charAt(i
) == '?') {
4390 decPrivateModeFlag
= true;
4394 if (decPrivateModeFlag
) {
4395 // This could be restore DEC private mode values.
4399 int top
= getCsiParam(0, 1, 1, height
) - 1;
4400 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4405 scrollRegionTop
= top
;
4406 scrollRegionBottom
= bottom
;
4409 cursorPosition(0, 0);
4414 * DECREQTPARM - Request terminal parameters.
4416 private void decreqtparm() {
4417 int i
= getCsiParam(0, 0);
4419 if ((i
!= 0) && (i
!= 1)) {
4426 * Request terminal parameters.
4430 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4431 * (CLoCk MULtiplier = 1, STP option flags = 0)
4435 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4438 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4440 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4446 * DECSCA - Select Character Attributes.
4448 private void decsca() {
4449 int i
= getCsiParam(0, 0);
4451 if ((i
== 0) || (i
== 2)) {
4453 currentState
.attr
.setProtect(false);
4457 currentState
.attr
.setProtect(true);
4462 * DECSTR - Soft Terminal Reset.
4464 private void decstr() {
4465 // Do exactly like RIS - Reset to initial state
4467 // Do I clear screen too? I think so...
4468 eraseScreen(0, 0, height
- 1, width
- 1, false);
4469 cursorPosition(0, 0);
4473 * DSR - Device status report.
4475 private void dsr() {
4476 boolean decPrivateModeFlag
= false;
4477 int row
= currentState
.cursorY
;
4479 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4480 if (collectBuffer
.charAt(i
) == '?') {
4481 decPrivateModeFlag
= true;
4486 int i
= getCsiParam(0, 0);
4491 // Request status report. Respond with "OK, no malfunction."
4493 // Send string directly to remote side
4494 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4497 writeRemote("\u009b0n");
4499 writeRemote("\033[0n");
4504 // Request cursor position. Respond with current position.
4505 if (currentState
.originMode
== true) {
4506 row
-= scrollRegionTop
;
4509 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4512 str
= String
.format("\u009b%d;%dR", row
+ 1,
4513 currentState
.cursorX
+ 1);
4515 str
= String
.format("\033[%d;%dR", row
+ 1,
4516 currentState
.cursorX
+ 1);
4519 // Send string directly to remote side
4524 if (decPrivateModeFlag
== true) {
4526 // Request printer status report. Respond with "Printer not
4529 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4530 && (s8c1t
== true)) {
4531 writeRemote("\u009b?13n");
4533 writeRemote("\033[?13n");
4539 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4540 && (decPrivateModeFlag
== true)
4543 // Request user-defined keys are locked or unlocked. Respond
4544 // with "User-defined keys are locked."
4546 if (s8c1t
== true) {
4547 writeRemote("\u009b?21n");
4549 writeRemote("\033[?21n");
4555 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4556 && (decPrivateModeFlag
== true)
4559 // Request keyboard language. Respond with "Keyboard
4560 // language is North American."
4562 if (s8c1t
== true) {
4563 writeRemote("\u009b?27;1n");
4565 writeRemote("\033[?27;1n");
4572 // Some other option, ignore
4578 * TBC - Tabulation clear.
4580 private void tbc() {
4581 int i
= getCsiParam(0, 0);
4583 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4584 for (Integer stop
: tabStops
) {
4585 if (stop
== currentState
.cursorX
) {
4590 tabStops
= newStops
;
4598 * Erase the characters in the current line from the start column to the
4599 * end column, inclusive.
4601 * @param start starting column to erase (between 0 and width - 1)
4602 * @param end ending column to erase (between 0 and width - 1)
4603 * @param honorProtected if true, do not erase characters with the
4604 * protected attribute set
4606 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4611 if (end
> width
- 1) {
4618 for (int i
= start
; i
<= end
; i
++) {
4619 DisplayLine line
= display
.get(currentState
.cursorY
);
4620 if ((!honorProtected
)
4621 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4628 * From the VT102 manual:
4630 * Erasing a character also erases any character
4631 * attribute of the character.
4637 * Erase with the current color a.k.a. back-color erase
4640 line
.setChar(i
, ' ');
4641 line
.setAttr(i
, currentState
.attr
);
4649 * Erase a rectangular section of the screen, inclusive. end column,
4652 * @param startRow starting row to erase (between 0 and height - 1)
4653 * @param startCol starting column to erase (between 0 and width - 1)
4654 * @param endRow ending row to erase (between 0 and height - 1)
4655 * @param endCol ending column to erase (between 0 and width - 1)
4656 * @param honorProtected if true, do not erase characters with the
4657 * protected attribute set
4659 private void eraseScreen(final int startRow
, final int startCol
,
4660 final int endRow
, final int endCol
, final boolean honorProtected
) {
4668 || (endRow
< startRow
)
4669 || (endCol
< startCol
)
4674 oldCursorY
= currentState
.cursorY
;
4675 for (int i
= startRow
; i
<= endRow
; i
++) {
4676 currentState
.cursorY
= i
;
4677 eraseLine(startCol
, endCol
, honorProtected
);
4679 // Erase display clears the double attributes
4680 display
.get(i
).setDoubleWidth(false);
4681 display
.get(i
).setDoubleHeight(0);
4683 currentState
.cursorY
= oldCursorY
;
4687 * VT220 printer functions. All of these are parsed, but won't do
4690 private void printerFunctions() {
4691 boolean decPrivateModeFlag
= false;
4692 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4693 if (collectBuffer
.charAt(i
) == '?') {
4694 decPrivateModeFlag
= true;
4699 int i
= getCsiParam(0, 0);
4704 if (decPrivateModeFlag
== false) {
4710 if (decPrivateModeFlag
== true) {
4711 // Print cursor line
4716 if (decPrivateModeFlag
== true) {
4717 // Auto print mode OFF
4719 // Printer controller OFF
4721 // Characters re-appear on the screen
4722 printerControllerMode
= false;
4727 if (decPrivateModeFlag
== true) {
4731 // Printer controller
4733 // Characters get sucked into oblivion
4734 printerControllerMode
= true;
4745 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4746 * of remote systems will send an XTerm title sequence even if TERM isn't
4749 * @param xtermChar the character received from the remote side
4751 private void oscPut(final char xtermChar
) {
4752 // System.err.println("oscPut: " + xtermChar);
4755 collectBuffer
.append(xtermChar
);
4758 if ((xtermChar
== 0x07)
4759 || (collectBuffer
.toString().endsWith("\033\\"))
4762 if (xtermChar
== 0x07) {
4763 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
4765 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4768 String
[] p
= args
.split(";");
4770 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4777 if (p
[0].equals("4")) {
4778 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
4779 // Set a color index value
4781 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
4782 } catch (NumberFormatException e
) {
4788 if (p
[0].equals("10")) {
4789 if (p
[1].equals("?")) {
4790 // Respond with foreground color.
4791 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToForegroundColor(currentState
.attr
);
4793 writeRemote(String
.format(
4794 "\033]10;rgb:%04x/%04x/%04x\033\\",
4795 color
.getRed() << 8,
4796 color
.getGreen() << 8,
4797 color
.getBlue() << 8));
4801 if (p
[0].equals("11")) {
4802 if (p
[1].equals("?")) {
4803 // Respond with background color.
4804 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToBackgroundColor(currentState
.attr
);
4806 writeRemote(String
.format(
4807 "\033]11;rgb:%04x/%04x/%04x\033\\",
4808 color
.getRed() << 8,
4809 color
.getGreen() << 8,
4810 color
.getBlue() << 8));
4814 if (p
[0].equals("444") && (p
.length
== 5)) {
4816 parseJexerImage(p
[1], p
[2], p
[3], p
[4]);
4821 // Go to SCAN_GROUND state
4828 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
4829 * Jexer ECMA48Terminal to talk to ECMA48.
4831 * @param pmChar the character received from the remote side
4833 private void pmPut(final char pmChar
) {
4834 // System.err.println("pmPut: " + pmChar);
4837 collectBuffer
.append(pmChar
);
4840 if (collectBuffer
.toString().endsWith("\033\\")) {
4842 arg
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4844 // System.err.println("arg: '" + arg + "'");
4846 if (arg
.equals("hideMousePointer")) {
4847 hideMousePointer
= true;
4849 if (arg
.equals("showMousePointer")) {
4850 hideMousePointer
= false;
4853 // Go to SCAN_GROUND state
4860 * Perform xterm window operations.
4862 private void xtermWindowOps() {
4863 boolean xtermPrivateModeFlag
= false;
4865 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4866 if (collectBuffer
.charAt(i
) == '?') {
4867 xtermPrivateModeFlag
= true;
4872 int i
= getCsiParam(0, 0);
4874 if (!xtermPrivateModeFlag
) {
4877 // Report xterm text area size in pixels as CSI 4 ; height ;
4879 writeRemote(String
.format("\033[4;%d;%dt", textHeight
* height
,
4880 textWidth
* width
));
4883 // Report character size in pixels as CSI 6 ; height ; width
4885 writeRemote(String
.format("\033[6;%d;%dt", textHeight
,
4889 // Report the text are size in characters as CSI 8 ; height ;
4891 writeRemote(String
.format("\033[8;%d;%dt", height
, width
));
4900 * Respond to xterm sixel query.
4902 private void xtermSixelQuery() {
4903 int item
= getCsiParam(0, 0);
4904 int action
= getCsiParam(1, 0);
4905 int value
= getCsiParam(2, 0);
4910 // Report number of color registers.
4911 writeRemote(String
.format("\033[?%d;%d;%dS", item
, 0, 1024));
4918 // We will not support this option.
4919 writeRemote(String
.format("\033[?%d;%dS", item
, action
));
4923 * Run this input character through the ECMA48 state machine.
4925 * @param ch character from the remote side
4927 private void consume(final int ch
) {
4930 // System.err.printf("%c STATE = %s\n", ch, scanState);
4932 // Special "anywhere" states
4934 // 18, 1A --> execute, then switch to SCAN_GROUND
4935 if ((ch
== 0x18) || (ch
== 0x1A)) {
4936 // CAN and SUB abort escape sequences
4941 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4945 if ((type
== DeviceType
.XTERM
)
4946 && ((scanState
== ScanState
.OSC_STRING
)
4947 || (scanState
== ScanState
.DCS_SIXEL
)
4948 || (scanState
== ScanState
.SOSPMAPC_STRING
))
4950 // Xterm can pass ESCAPE to its OSC sequence.
4951 // Xterm can pass ESCAPE to its DCS sequence.
4952 // Jexer can pass ESCAPE to its PM sequence.
4953 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
4954 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4955 && (scanState
!= ScanState
.DCS_IGNORE
)
4956 && (scanState
!= ScanState
.DCS_PARAM
)
4957 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4959 scanState
= ScanState
.ESCAPE
;
4964 // 0x9B == CSI 8-bit sequence
4966 scanState
= ScanState
.CSI_ENTRY
;
4970 // 0x9D goes to ScanState.OSC_STRING
4972 scanState
= ScanState
.OSC_STRING
;
4976 // 0x90 goes to DCS_ENTRY
4978 scanState
= ScanState
.DCS_ENTRY
;
4982 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4983 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4984 scanState
= ScanState
.SOSPMAPC_STRING
;
4988 // 0x7F (DEL) is always discarded
4993 switch (scanState
) {
4996 // 00-17, 19, 1C-1F --> execute
4997 // 80-8F, 91-9A, 9C --> execute
4998 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4999 handleControlChar((char) ch
);
5003 if (((ch
>= 0x20) && (ch
<= 0x7F))
5007 // VT220 printer --> trash bin
5008 if (((type
== DeviceType
.VT220
)
5009 || (type
== DeviceType
.XTERM
))
5010 && (printerControllerMode
== true)
5015 // Hang onto this character
5016 repCh
= mapCharacter(ch
);
5018 // Print this character
5019 printCharacter(repCh
);
5024 // 00-17, 19, 1C-1F --> execute
5026 handleControlChar((char) ch
);
5030 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
5031 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5033 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
5037 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
5038 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
5049 // DECSC - Save cursor
5050 // Note this code overlaps both ANSI and VT52 mode
5055 // DECRC - Restore cursor
5056 // Note this code overlaps both ANSI and VT52 mode
5065 if (vt52Mode
== true) {
5066 // DECANM - Enter ANSI mode
5068 arrowKeyMode
= ArrowKeyMode
.VT100
;
5071 * From the VT102 docs: "You use ANSI mode to select
5072 * most terminal features; the terminal uses the same
5073 * features when it switches to VT52 mode. You
5074 * cannot, however, change most of these features in
5077 * In other words, do not reset any other attributes
5078 * when switching between VT52 submode and ANSI.
5082 currentState
.g0Charset
= CharacterSet
.US
;
5083 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5085 singleshift
= Singleshift
.NONE
;
5086 currentState
.glLockshift
= LockshiftMode
.NONE
;
5087 currentState
.grLockshift
= LockshiftMode
.NONE
;
5091 // DECKPAM - Keypad application mode
5092 // Note this code overlaps both ANSI and VT52 mode
5096 // DECKPNM - Keypad numeric mode
5097 // Note this code overlaps both ANSI and VT52 mode
5104 if (vt52Mode
== true) {
5105 // Cursor up, and stop at the top without scrolling
5110 if (vt52Mode
== true) {
5111 // Cursor down, and stop at the bottom without scrolling
5112 cursorDown(1, false);
5116 if (vt52Mode
== true) {
5117 // Cursor right, and stop at the right without scrolling
5118 cursorRight(1, false);
5122 if (vt52Mode
== true) {
5123 // Cursor left, and stop at the left without scrolling
5124 cursorLeft(1, false);
5131 if (vt52Mode
== true) {
5139 if (vt52Mode
== true) {
5140 // G0 --> Special graphics
5141 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
5145 if (vt52Mode
== true) {
5147 currentState
.g0Charset
= CharacterSet
.US
;
5151 if (vt52Mode
== true) {
5153 cursorPosition(0, 0);
5155 // HTS - Horizontal tabulation set
5160 if (vt52Mode
== true) {
5161 // Reverse line feed. Same as RI.
5166 if (vt52Mode
== true) {
5167 // Erase to end of screen
5168 eraseLine(currentState
.cursorX
, width
- 1, false);
5169 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
5174 if (vt52Mode
== true) {
5175 // Erase to end of line
5176 eraseLine(currentState
.cursorX
, width
- 1, false);
5182 if (vt52Mode
== true) {
5185 // RI - Reverse index
5190 if (vt52Mode
== false) {
5192 singleshift
= Singleshift
.SS2
;
5196 if (vt52Mode
== false) {
5198 singleshift
= Singleshift
.SS3
;
5205 if ((ch
>= 0x51) && (ch
<= 0x57)) {
5221 if (vt52Mode
== true) {
5222 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
5230 if (vt52Mode
== true) {
5232 // Send string directly to remote side
5233 writeRemote("\033/Z");
5236 // Send string directly to remote side
5237 writeRemote(deviceTypeResponse());
5248 // VT52 cannot get to any of these other states
5249 if (vt52Mode
== true) {
5254 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
5261 // RIS - Reset to initial state
5263 // Do I clear screen too? I think so...
5264 eraseScreen(0, 0, height
- 1, width
- 1, false);
5265 cursorPosition(0, 0);
5279 if ((type
== DeviceType
.VT220
)
5280 || (type
== DeviceType
.XTERM
)) {
5282 // VT220 lockshift G2 into GL
5283 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
5288 if ((type
== DeviceType
.VT220
)
5289 || (type
== DeviceType
.XTERM
)) {
5291 // VT220 lockshift G3 into GL
5292 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
5310 if ((type
== DeviceType
.VT220
)
5311 || (type
== DeviceType
.XTERM
)) {
5313 // VT220 lockshift G3 into GR
5314 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
5319 if ((type
== DeviceType
.VT220
)
5320 || (type
== DeviceType
.XTERM
)) {
5322 // VT220 lockshift G2 into GR
5323 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
5329 if ((type
== DeviceType
.VT220
)
5330 || (type
== DeviceType
.XTERM
)) {
5332 // VT220 lockshift G1 into GR
5333 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
5343 // 0x5B goes to CSI_ENTRY
5345 scanState
= ScanState
.CSI_ENTRY
;
5348 // 0x5D goes to OSC_STRING
5350 scanState
= ScanState
.OSC_STRING
;
5353 // 0x50 goes to DCS_ENTRY
5355 scanState
= ScanState
.DCS_ENTRY
;
5358 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5359 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
5360 scanState
= ScanState
.SOSPMAPC_STRING
;
5365 case ESCAPE_INTERMEDIATE
:
5366 // 00-17, 19, 1C-1F --> execute
5368 handleControlChar((char) ch
);
5371 // 20-2F --> collect
5372 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5376 // 30-7E --> dispatch, then switch to GROUND
5377 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
5380 if ((collectBuffer
.length() == 1)
5381 && (collectBuffer
.charAt(0) == '(')) {
5382 // G0 --> Special graphics
5383 currentState
.g0Charset
= CharacterSet
.DRAWING
;
5385 if ((collectBuffer
.length() == 1)
5386 && (collectBuffer
.charAt(0) == ')')) {
5387 // G1 --> Special graphics
5388 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5390 if ((type
== DeviceType
.VT220
)
5391 || (type
== DeviceType
.XTERM
)) {
5393 if ((collectBuffer
.length() == 1)
5394 && (collectBuffer
.charAt(0) == '*')) {
5395 // G2 --> Special graphics
5396 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5398 if ((collectBuffer
.length() == 1)
5399 && (collectBuffer
.charAt(0) == '+')) {
5400 // G3 --> Special graphics
5401 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5406 if ((collectBuffer
.length() == 1)
5407 && (collectBuffer
.charAt(0) == '(')) {
5408 // G0 --> Alternate character ROM standard character set
5409 currentState
.g0Charset
= CharacterSet
.ROM
;
5411 if ((collectBuffer
.length() == 1)
5412 && (collectBuffer
.charAt(0) == ')')) {
5413 // G1 --> Alternate character ROM standard character set
5414 currentState
.g1Charset
= CharacterSet
.ROM
;
5418 if ((collectBuffer
.length() == 1)
5419 && (collectBuffer
.charAt(0) == '(')) {
5420 // G0 --> Alternate character ROM special graphics
5421 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5423 if ((collectBuffer
.length() == 1)
5424 && (collectBuffer
.charAt(0) == ')')) {
5425 // G1 --> Alternate character ROM special graphics
5426 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5430 if ((collectBuffer
.length() == 1)
5431 && (collectBuffer
.charAt(0) == '#')) {
5432 // DECDHL - Double-height line (top half)
5437 if ((collectBuffer
.length() == 1)
5438 && (collectBuffer
.charAt(0) == '#')) {
5439 // DECDHL - Double-height line (bottom half)
5442 if ((type
== DeviceType
.VT220
)
5443 || (type
== DeviceType
.XTERM
)) {
5445 if ((collectBuffer
.length() == 1)
5446 && (collectBuffer
.charAt(0) == '(')) {
5448 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5450 if ((collectBuffer
.length() == 1)
5451 && (collectBuffer
.charAt(0) == ')')) {
5453 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5455 if ((collectBuffer
.length() == 1)
5456 && (collectBuffer
.charAt(0) == '*')) {
5458 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5460 if ((collectBuffer
.length() == 1)
5461 && (collectBuffer
.charAt(0) == '+')) {
5463 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5468 if ((collectBuffer
.length() == 1)
5469 && (collectBuffer
.charAt(0) == '#')) {
5470 // DECSWL - Single-width line
5473 if ((type
== DeviceType
.VT220
)
5474 || (type
== DeviceType
.XTERM
)) {
5476 if ((collectBuffer
.length() == 1)
5477 && (collectBuffer
.charAt(0) == '(')) {
5479 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5481 if ((collectBuffer
.length() == 1)
5482 && (collectBuffer
.charAt(0) == ')')) {
5484 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5486 if ((collectBuffer
.length() == 1)
5487 && (collectBuffer
.charAt(0) == '*')) {
5489 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5491 if ((collectBuffer
.length() == 1)
5492 && (collectBuffer
.charAt(0) == '+')) {
5494 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5499 if ((collectBuffer
.length() == 1)
5500 && (collectBuffer
.charAt(0) == '#')) {
5501 // DECDWL - Double-width line
5504 if ((type
== DeviceType
.VT220
)
5505 || (type
== DeviceType
.XTERM
)) {
5507 if ((collectBuffer
.length() == 1)
5508 && (collectBuffer
.charAt(0) == '(')) {
5510 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5512 if ((collectBuffer
.length() == 1)
5513 && (collectBuffer
.charAt(0) == ')')) {
5515 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5517 if ((collectBuffer
.length() == 1)
5518 && (collectBuffer
.charAt(0) == '*')) {
5520 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5522 if ((collectBuffer
.length() == 1)
5523 && (collectBuffer
.charAt(0) == '+')) {
5525 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5530 if ((type
== DeviceType
.VT220
)
5531 || (type
== DeviceType
.XTERM
)) {
5533 if ((collectBuffer
.length() == 1)
5534 && (collectBuffer
.charAt(0) == '(')) {
5536 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5538 if ((collectBuffer
.length() == 1)
5539 && (collectBuffer
.charAt(0) == ')')) {
5541 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5543 if ((collectBuffer
.length() == 1)
5544 && (collectBuffer
.charAt(0) == '*')) {
5546 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5548 if ((collectBuffer
.length() == 1)
5549 && (collectBuffer
.charAt(0) == '+')) {
5551 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5556 if ((collectBuffer
.length() == 1)
5557 && (collectBuffer
.charAt(0) == '#')) {
5558 // DECALN - Screen alignment display
5567 if ((type
== DeviceType
.VT220
)
5568 || (type
== DeviceType
.XTERM
)) {
5570 if ((collectBuffer
.length() == 1)
5571 && (collectBuffer
.charAt(0) == '(')) {
5572 // G0 --> DEC_SUPPLEMENTAL
5573 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5575 if ((collectBuffer
.length() == 1)
5576 && (collectBuffer
.charAt(0) == ')')) {
5577 // G1 --> DEC_SUPPLEMENTAL
5578 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5580 if ((collectBuffer
.length() == 1)
5581 && (collectBuffer
.charAt(0) == '*')) {
5582 // G2 --> DEC_SUPPLEMENTAL
5583 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5585 if ((collectBuffer
.length() == 1)
5586 && (collectBuffer
.charAt(0) == '+')) {
5587 // G3 --> DEC_SUPPLEMENTAL
5588 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5593 if ((type
== DeviceType
.VT220
)
5594 || (type
== DeviceType
.XTERM
)) {
5596 if ((collectBuffer
.length() == 1)
5597 && (collectBuffer
.charAt(0) == '(')) {
5599 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5601 if ((collectBuffer
.length() == 1)
5602 && (collectBuffer
.charAt(0) == ')')) {
5604 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5606 if ((collectBuffer
.length() == 1)
5607 && (collectBuffer
.charAt(0) == '*')) {
5609 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5611 if ((collectBuffer
.length() == 1)
5612 && (collectBuffer
.charAt(0) == '+')) {
5614 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5623 if ((collectBuffer
.length() == 1)
5624 && (collectBuffer
.charAt(0) == '(')) {
5625 // G0 --> United Kingdom set
5626 currentState
.g0Charset
= CharacterSet
.UK
;
5628 if ((collectBuffer
.length() == 1)
5629 && (collectBuffer
.charAt(0) == ')')) {
5630 // G1 --> United Kingdom set
5631 currentState
.g1Charset
= CharacterSet
.UK
;
5633 if ((type
== DeviceType
.VT220
)
5634 || (type
== DeviceType
.XTERM
)) {
5636 if ((collectBuffer
.length() == 1)
5637 && (collectBuffer
.charAt(0) == '*')) {
5638 // G2 --> United Kingdom set
5639 currentState
.g2Charset
= CharacterSet
.UK
;
5641 if ((collectBuffer
.length() == 1)
5642 && (collectBuffer
.charAt(0) == '+')) {
5643 // G3 --> United Kingdom set
5644 currentState
.g3Charset
= CharacterSet
.UK
;
5649 if ((collectBuffer
.length() == 1)
5650 && (collectBuffer
.charAt(0) == '(')) {
5652 currentState
.g0Charset
= CharacterSet
.US
;
5654 if ((collectBuffer
.length() == 1)
5655 && (collectBuffer
.charAt(0) == ')')) {
5657 currentState
.g1Charset
= CharacterSet
.US
;
5659 if ((type
== DeviceType
.VT220
)
5660 || (type
== DeviceType
.XTERM
)) {
5662 if ((collectBuffer
.length() == 1)
5663 && (collectBuffer
.charAt(0) == '*')) {
5665 currentState
.g2Charset
= CharacterSet
.US
;
5667 if ((collectBuffer
.length() == 1)
5668 && (collectBuffer
.charAt(0) == '+')) {
5670 currentState
.g3Charset
= CharacterSet
.US
;
5675 if ((type
== DeviceType
.VT220
)
5676 || (type
== DeviceType
.XTERM
)) {
5678 if ((collectBuffer
.length() == 1)
5679 && (collectBuffer
.charAt(0) == '(')) {
5681 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5683 if ((collectBuffer
.length() == 1)
5684 && (collectBuffer
.charAt(0) == ')')) {
5686 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5688 if ((collectBuffer
.length() == 1)
5689 && (collectBuffer
.charAt(0) == '*')) {
5691 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5693 if ((collectBuffer
.length() == 1)
5694 && (collectBuffer
.charAt(0) == '+')) {
5696 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5703 if ((type
== DeviceType
.VT220
)
5704 || (type
== DeviceType
.XTERM
)) {
5706 if ((collectBuffer
.length() == 1)
5707 && (collectBuffer
.charAt(0) == '(')) {
5709 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5711 if ((collectBuffer
.length() == 1)
5712 && (collectBuffer
.charAt(0) == ')')) {
5714 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5716 if ((collectBuffer
.length() == 1)
5717 && (collectBuffer
.charAt(0) == '*')) {
5719 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5721 if ((collectBuffer
.length() == 1)
5722 && (collectBuffer
.charAt(0) == '+')) {
5724 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5729 if ((type
== DeviceType
.VT220
)
5730 || (type
== DeviceType
.XTERM
)) {
5732 if ((collectBuffer
.length() == 1)
5733 && (collectBuffer
.charAt(0) == ' ')) {
5740 if ((type
== DeviceType
.VT220
)
5741 || (type
== DeviceType
.XTERM
)) {
5743 if ((collectBuffer
.length() == 1)
5744 && (collectBuffer
.charAt(0) == ' ')) {
5751 if ((type
== DeviceType
.VT220
)
5752 || (type
== DeviceType
.XTERM
)) {
5754 if ((collectBuffer
.length() == 1)
5755 && (collectBuffer
.charAt(0) == '(')) {
5757 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5759 if ((collectBuffer
.length() == 1)
5760 && (collectBuffer
.charAt(0) == ')')) {
5762 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5764 if ((collectBuffer
.length() == 1)
5765 && (collectBuffer
.charAt(0) == '*')) {
5767 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5769 if ((collectBuffer
.length() == 1)
5770 && (collectBuffer
.charAt(0) == '+')) {
5772 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5780 if ((type
== DeviceType
.VT220
)
5781 || (type
== DeviceType
.XTERM
)) {
5783 if ((collectBuffer
.length() == 1)
5784 && (collectBuffer
.charAt(0) == '(')) {
5786 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5788 if ((collectBuffer
.length() == 1)
5789 && (collectBuffer
.charAt(0) == ')')) {
5791 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5793 if ((collectBuffer
.length() == 1)
5794 && (collectBuffer
.charAt(0) == '*')) {
5796 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5798 if ((collectBuffer
.length() == 1)
5799 && (collectBuffer
.charAt(0) == '+')) {
5801 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5812 if ((type
== DeviceType
.VT220
)
5813 || (type
== DeviceType
.XTERM
)) {
5815 if ((collectBuffer
.length() == 1)
5816 && (collectBuffer
.charAt(0) == '(')) {
5818 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5820 if ((collectBuffer
.length() == 1)
5821 && (collectBuffer
.charAt(0) == ')')) {
5823 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5825 if ((collectBuffer
.length() == 1)
5826 && (collectBuffer
.charAt(0) == '*')) {
5828 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5830 if ((collectBuffer
.length() == 1)
5831 && (collectBuffer
.charAt(0) == '+')) {
5833 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5838 if ((type
== DeviceType
.VT220
)
5839 || (type
== DeviceType
.XTERM
)) {
5841 if ((collectBuffer
.length() == 1)
5842 && (collectBuffer
.charAt(0) == '(')) {
5844 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5846 if ((collectBuffer
.length() == 1)
5847 && (collectBuffer
.charAt(0) == ')')) {
5849 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5851 if ((collectBuffer
.length() == 1)
5852 && (collectBuffer
.charAt(0) == '*')) {
5854 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5856 if ((collectBuffer
.length() == 1)
5857 && (collectBuffer
.charAt(0) == '+')) {
5859 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5871 if ((type
== DeviceType
.VT220
)
5872 || (type
== DeviceType
.XTERM
)) {
5874 if ((collectBuffer
.length() == 1)
5875 && (collectBuffer
.charAt(0) == '(')) {
5877 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5879 if ((collectBuffer
.length() == 1)
5880 && (collectBuffer
.charAt(0) == ')')) {
5882 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5884 if ((collectBuffer
.length() == 1)
5885 && (collectBuffer
.charAt(0) == '*')) {
5887 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5889 if ((collectBuffer
.length() == 1)
5890 && (collectBuffer
.charAt(0) == '+')) {
5892 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5897 if ((type
== DeviceType
.VT220
)
5898 || (type
== DeviceType
.XTERM
)) {
5900 if ((collectBuffer
.length() == 1)
5901 && (collectBuffer
.charAt(0) == '(')) {
5903 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5905 if ((collectBuffer
.length() == 1)
5906 && (collectBuffer
.charAt(0) == ')')) {
5908 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5910 if ((collectBuffer
.length() == 1)
5911 && (collectBuffer
.charAt(0) == '*')) {
5913 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5915 if ((collectBuffer
.length() == 1)
5916 && (collectBuffer
.charAt(0) == '+')) {
5918 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5965 // 0x9C goes to GROUND
5973 // 00-17, 19, 1C-1F --> execute
5975 handleControlChar((char) ch
);
5978 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5979 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5981 scanState
= ScanState
.CSI_INTERMEDIATE
;
5984 // 30-39, 3B --> param, then switch to CSI_PARAM
5985 if ((ch
>= '0') && (ch
<= '9')) {
5987 scanState
= ScanState
.CSI_PARAM
;
5991 scanState
= ScanState
.CSI_PARAM
;
5994 // 3C-3F --> collect, then switch to CSI_PARAM
5995 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5997 scanState
= ScanState
.CSI_PARAM
;
6000 // 40-7E --> dispatch, then switch to GROUND
6001 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6004 // ICH - Insert character
6012 // CUD - Cursor down
6016 // CUF - Cursor forward
6020 // CUB - Cursor backward
6024 // CNL - Cursor down and to column 1
6025 if (type
== DeviceType
.XTERM
) {
6030 // CPL - Cursor up and to column 1
6031 if (type
== DeviceType
.XTERM
) {
6036 // CHA - Cursor to column # in current row
6037 if (type
== DeviceType
.XTERM
) {
6042 // CUP - Cursor position
6046 // CHT - Cursor forward X tab stops (default 1)
6047 if (type
== DeviceType
.XTERM
) {
6052 // ED - Erase in display
6056 // EL - Erase in line
6071 // DCH - Delete character
6078 // Scroll up X lines (default 1)
6079 if (type
== DeviceType
.XTERM
) {
6080 boolean xtermPrivateModeFlag
= false;
6081 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6082 if (collectBuffer
.charAt(i
) == '?') {
6083 xtermPrivateModeFlag
= true;
6087 if (xtermPrivateModeFlag
) {
6095 // Scroll down X lines (default 1)
6096 if (type
== DeviceType
.XTERM
) {
6105 if ((type
== DeviceType
.VT220
)
6106 || (type
== DeviceType
.XTERM
)) {
6108 // ECH - Erase character
6115 // CBT - Cursor backward X tab stops (default 1)
6116 if (type
== DeviceType
.XTERM
) {
6127 // HPA - Cursor to column # in current row. Same as CHA
6128 if (type
== DeviceType
.XTERM
) {
6133 // HPR - Cursor right. Same as CUF
6134 if (type
== DeviceType
.XTERM
) {
6139 // REP - Repeat last char X times
6140 if (type
== DeviceType
.XTERM
) {
6145 // DA - Device attributes
6149 // VPA - Cursor to row, current column.
6150 if (type
== DeviceType
.XTERM
) {
6155 // VPR - Cursor down. Same as CUD
6156 if (type
== DeviceType
.XTERM
) {
6161 // HVP - Horizontal and vertical position
6165 // TBC - Tabulation clear
6169 // Sets an ANSI or DEC private toggle
6173 if ((type
== DeviceType
.VT220
)
6174 || (type
== DeviceType
.XTERM
)) {
6176 // Printer functions
6184 // Sets an ANSI or DEC private toggle
6188 // SGR - Select graphics rendition
6192 // DSR - Device status report
6199 // DECLL - Load leds
6203 // DECSTBM - Set top and bottom margins
6207 // Save cursor (ANSI.SYS)
6208 if (type
== DeviceType
.XTERM
) {
6209 savedState
.cursorX
= currentState
.cursorX
;
6210 savedState
.cursorY
= currentState
.cursorY
;
6214 if (type
== DeviceType
.XTERM
) {
6215 // Window operations
6220 // Restore cursor (ANSI.SYS)
6221 if (type
== DeviceType
.XTERM
) {
6222 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
6229 // DECREQTPARM - Request terminal parameters
6245 // 0x9C goes to GROUND
6250 // 0x3A goes to CSI_IGNORE
6252 scanState
= ScanState
.CSI_IGNORE
;
6257 // 00-17, 19, 1C-1F --> execute
6259 handleControlChar((char) ch
);
6262 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6263 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6265 scanState
= ScanState
.CSI_INTERMEDIATE
;
6268 // 30-39, 3B --> param
6269 if ((ch
>= '0') && (ch
<= '9')) {
6276 // 0x3A goes to CSI_IGNORE
6278 scanState
= ScanState
.CSI_IGNORE
;
6280 // 0x3C-3F goes to CSI_IGNORE
6281 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6282 scanState
= ScanState
.CSI_IGNORE
;
6285 // 40-7E --> dispatch, then switch to GROUND
6286 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6289 // ICH - Insert character
6297 // CUD - Cursor down
6301 // CUF - Cursor forward
6305 // CUB - Cursor backward
6309 // CNL - Cursor down and to column 1
6310 if (type
== DeviceType
.XTERM
) {
6315 // CPL - Cursor up and to column 1
6316 if (type
== DeviceType
.XTERM
) {
6321 // CHA - Cursor to column # in current row
6322 if (type
== DeviceType
.XTERM
) {
6327 // CUP - Cursor position
6331 // CHT - Cursor forward X tab stops (default 1)
6332 if (type
== DeviceType
.XTERM
) {
6337 // ED - Erase in display
6341 // EL - Erase in line
6356 // DCH - Delete character
6363 // Scroll up X lines (default 1)
6364 if (type
== DeviceType
.XTERM
) {
6365 boolean xtermPrivateModeFlag
= false;
6366 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6367 if (collectBuffer
.charAt(i
) == '?') {
6368 xtermPrivateModeFlag
= true;
6372 if (xtermPrivateModeFlag
) {
6380 // Scroll down X lines (default 1)
6381 if (type
== DeviceType
.XTERM
) {
6390 if ((type
== DeviceType
.VT220
)
6391 || (type
== DeviceType
.XTERM
)) {
6393 // ECH - Erase character
6400 // CBT - Cursor backward X tab stops (default 1)
6401 if (type
== DeviceType
.XTERM
) {
6412 // HPA - Cursor to column # in current row. Same as CHA
6413 if (type
== DeviceType
.XTERM
) {
6418 // HPR - Cursor right. Same as CUF
6419 if (type
== DeviceType
.XTERM
) {
6424 // REP - Repeat last char X times
6425 if (type
== DeviceType
.XTERM
) {
6430 // DA - Device attributes
6434 // VPA - Cursor to row, current column.
6435 if (type
== DeviceType
.XTERM
) {
6440 // VPR - Cursor down. Same as CUD
6441 if (type
== DeviceType
.XTERM
) {
6446 // HVP - Horizontal and vertical position
6450 // TBC - Tabulation clear
6454 // Sets an ANSI or DEC private toggle
6458 if ((type
== DeviceType
.VT220
)
6459 || (type
== DeviceType
.XTERM
)) {
6461 // Printer functions
6469 // Sets an ANSI or DEC private toggle
6473 // SGR - Select graphics rendition
6477 // DSR - Device status report
6484 // DECLL - Load leds
6488 // DECSTBM - Set top and bottom margins
6494 if (type
== DeviceType
.XTERM
) {
6495 // Window operations
6504 // DECREQTPARM - Request terminal parameters
6521 case CSI_INTERMEDIATE
:
6522 // 00-17, 19, 1C-1F --> execute
6524 handleControlChar((char) ch
);
6527 // 20-2F --> collect
6528 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6532 // 0x30-3F goes to CSI_IGNORE
6533 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6534 scanState
= ScanState
.CSI_IGNORE
;
6537 // 40-7E --> dispatch, then switch to GROUND
6538 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6590 if (((type
== DeviceType
.VT220
)
6591 || (type
== DeviceType
.XTERM
))
6592 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6594 // DECSCL - compatibility level
6597 if ((type
== DeviceType
.XTERM
)
6598 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6600 // DECSTR - Soft terminal reset
6605 if (((type
== DeviceType
.VT220
)
6606 || (type
== DeviceType
.XTERM
))
6607 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6635 // 00-17, 19, 1C-1F --> execute
6637 handleControlChar((char) ch
);
6640 // 20-2F --> collect
6641 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6645 // 40-7E --> ignore, then switch to GROUND
6646 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6650 // 20-3F, 7F --> ignore
6656 // 0x9C goes to GROUND
6661 // 0x1B 0x5C goes to GROUND
6666 if ((collectBuffer
.length() > 0)
6667 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6673 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6674 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6676 scanState
= ScanState
.DCS_INTERMEDIATE
;
6679 // 30-39, 3B --> param, then switch to DCS_PARAM
6680 if ((ch
>= '0') && (ch
<= '9')) {
6682 scanState
= ScanState
.DCS_PARAM
;
6686 scanState
= ScanState
.DCS_PARAM
;
6689 // 3C-3F --> collect, then switch to DCS_PARAM
6690 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6692 scanState
= ScanState
.DCS_PARAM
;
6695 // 00-17, 19, 1C-1F, 7F --> ignore
6697 // 0x3A goes to DCS_IGNORE
6699 scanState
= ScanState
.DCS_IGNORE
;
6702 // 0x71 goes to DCS_SIXEL
6704 sixelParseBuffer
= new StringBuilder();
6705 scanState
= ScanState
.DCS_SIXEL
;
6706 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6707 // 0x40-7E goes to DCS_PASSTHROUGH
6708 scanState
= ScanState
.DCS_PASSTHROUGH
;
6712 case DCS_INTERMEDIATE
:
6714 // 0x9C goes to GROUND
6719 // 0x1B 0x5C goes to GROUND
6724 if ((collectBuffer
.length() > 0)
6725 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6731 // 0x30-3F goes to DCS_IGNORE
6732 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6733 scanState
= ScanState
.DCS_IGNORE
;
6736 // 0x40-7E goes to DCS_PASSTHROUGH
6737 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6738 scanState
= ScanState
.DCS_PASSTHROUGH
;
6741 // 00-17, 19, 1C-1F, 7F --> ignore
6746 // 0x9C goes to GROUND
6751 // 0x1B 0x5C goes to GROUND
6756 if ((collectBuffer
.length() > 0)
6757 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6763 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6764 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6766 scanState
= ScanState
.DCS_INTERMEDIATE
;
6769 // 30-39, 3B --> param
6770 if ((ch
>= '0') && (ch
<= '9')) {
6777 // 00-17, 19, 1C-1F, 7F --> ignore
6779 // 0x3A, 3C-3F goes to DCS_IGNORE
6781 scanState
= ScanState
.DCS_IGNORE
;
6783 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6784 scanState
= ScanState
.DCS_IGNORE
;
6787 // 0x71 goes to DCS_SIXEL
6789 sixelParseBuffer
= new StringBuilder();
6790 scanState
= ScanState
.DCS_SIXEL
;
6791 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6792 // 0x40-7E goes to DCS_PASSTHROUGH
6793 scanState
= ScanState
.DCS_PASSTHROUGH
;
6797 case DCS_PASSTHROUGH
:
6798 // 0x9C goes to GROUND
6803 // 0x1B 0x5C goes to GROUND
6808 if ((collectBuffer
.length() > 0)
6809 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6815 // 00-17, 19, 1C-1F, 20-7E --> put
6817 // We ignore all DCS except sixel.
6821 // We ignore all DCS except sixel.
6824 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6825 // We ignore all DCS except sixel.
6828 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6829 // We ignore all DCS except sixel.
6838 // 00-17, 19, 1C-1F, 20-7F --> ignore
6840 // 0x9C goes to GROUND
6848 // 0x9C goes to GROUND
6855 // 0x1B 0x5C goes to GROUND
6861 if ((collectBuffer
.length() > 0)
6862 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6870 // 00-17, 19, 1C-1F, 20-7E --> put
6873 || ((ch
>= 0x1C) && (ch
<= 0x1F))
6874 || ((ch
>= 0x20) && (ch
<= 0x7E))
6876 sixelParseBuffer
.append((char) ch
);
6882 case SOSPMAPC_STRING
:
6883 // 00-17, 19, 1C-1F, 20-7F --> ignore
6885 // Special case for Jexer: PM can pass one control character
6890 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6894 // 0x9C goes to GROUND
6902 // Special case for Xterm: OSC can pass control characters
6903 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
6907 // 00-17, 19, 1C-1F --> ignore
6909 // 20-7F --> osc_put
6910 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6914 // 0x9C goes to GROUND
6921 case VT52_DIRECT_CURSOR_ADDRESS
:
6922 // This is a special case for the VT52 sequence "ESC Y l c"
6923 if (collectBuffer
.length() == 0) {
6925 } else if (collectBuffer
.length() == 1) {
6926 // We've got the two characters, one in the buffer and the
6928 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
6937 * Expose current cursor X to outside world.
6939 * @return current cursor X
6941 public final int getCursorX() {
6942 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6943 return currentState
.cursorX
* 2;
6945 return currentState
.cursorX
;
6949 * Expose current cursor Y to outside world.
6951 * @return current cursor Y
6953 public final int getCursorY() {
6954 return currentState
.cursorY
;
6958 * Returns true if this terminal has requested the mouse pointer be
6961 * @return true if this terminal has requested the mouse pointer be
6964 public final boolean hasHiddenMousePointer() {
6965 return hideMousePointer
;
6969 * Get the mouse protocol.
6971 * @return MouseProtocol.OFF, MouseProtocol.X10, etc.
6973 public MouseProtocol
getMouseProtocol() {
6974 return mouseProtocol
;
6978 * Draw the left and right cells of a two-cell-wide (full-width) glyph.
6980 * @param leftX the x position to draw the left half to
6981 * @param leftY the y position to draw the left half to
6982 * @param rightX the x position to draw the right half to
6983 * @param rightY the y position to draw the right half to
6984 * @param ch the character to draw
6986 private void drawHalves(final int leftX
, final int leftY
,
6987 final int rightX
, final int rightY
, final int ch
) {
6989 // System.err.println("drawHalves(): " + Integer.toHexString(ch));
6991 if (lastTextHeight
!= textHeight
) {
6992 glyphMaker
= GlyphMaker
.getInstance(textHeight
);
6993 lastTextHeight
= textHeight
;
6996 Cell cell
= new Cell(ch
, currentState
.attr
);
6997 BufferedImage image
= glyphMaker
.getImage(cell
, textWidth
* 2,
6999 BufferedImage leftImage
= image
.getSubimage(0, 0, textWidth
,
7001 BufferedImage rightImage
= image
.getSubimage(textWidth
, 0, textWidth
,
7004 Cell left
= new Cell(cell
);
7005 left
.setImage(leftImage
);
7006 left
.setWidth(Cell
.Width
.LEFT
);
7007 display
.get(leftY
).replace(leftX
, left
);
7009 Cell right
= new Cell(cell
);
7010 right
.setImage(rightImage
);
7011 right
.setWidth(Cell
.Width
.RIGHT
);
7012 display
.get(rightY
).replace(rightX
, right
);
7016 * Set the width of a character cell in pixels.
7018 * @param textWidth the width in pixels of a character cell
7020 public void setTextWidth(final int textWidth
) {
7021 this.textWidth
= textWidth
;
7025 * Set the height of a character cell in pixels.
7027 * @param textHeight the height in pixels of a character cell
7029 public void setTextHeight(final int textHeight
) {
7030 this.textHeight
= textHeight
;
7034 * Parse a sixel string into a bitmap image, and overlay that image onto
7037 private void parseSixel() {
7040 System.err.println("parseSixel(): '" + sixelParseBuffer.toString()
7044 Sixel sixel
= new Sixel(sixelParseBuffer
.toString(), sixelPalette
);
7045 BufferedImage image
= sixel
.getImage();
7047 // System.err.println("parseSixel(): image " + image);
7049 if (image
== null) {
7050 // Sixel data was malformed in some way, bail out.
7057 * Break up the image into text cell sized pieces as a new array of
7060 * Note original column position x0.
7064 * 1. Advance (printCharacter(' ')) for horizontal increment, or
7065 * index (linefeed() + cursorPosition(y, x0)) for vertical
7068 * 2. Set (x, y) cell image data.
7070 * 3. For the right and bottom edges:
7072 * a. Render the text to pixels using Terminus font.
7074 * b. Blit the image on top of the text, using alpha channel.
7076 int cellColumns
= image
.getWidth() / textWidth
;
7077 if (cellColumns
* textWidth
< image
.getWidth()) {
7080 int cellRows
= image
.getHeight() / textHeight
;
7081 if (cellRows
* textHeight
< image
.getHeight()) {
7085 // Break the image up into an array of cells.
7086 Cell
[][] cells
= new Cell
[cellColumns
][cellRows
];
7088 for (int x
= 0; x
< cellColumns
; x
++) {
7089 for (int y
= 0; y
< cellRows
; y
++) {
7091 int width
= textWidth
;
7092 if ((x
+ 1) * textWidth
> image
.getWidth()) {
7093 width
= image
.getWidth() - (x
* textWidth
);
7095 int height
= textHeight
;
7096 if ((y
+ 1) * textHeight
> image
.getHeight()) {
7097 height
= image
.getHeight() - (y
* textHeight
);
7100 Cell cell
= new Cell();
7101 cell
.setImage(image
.getSubimage(x
* textWidth
,
7102 y
* textHeight
, width
, height
));
7108 int x0
= currentState
.cursorX
;
7109 for (int y
= 0; y
< cellRows
; y
++) {
7110 for (int x
= 0; x
< cellColumns
; x
++) {
7111 assert (currentState
.cursorX
<= rightMargin
);
7113 // TODO: Render text of current cell first, then image over
7114 // it (accounting for blank pixels). For now, just copy the
7116 DisplayLine line
= display
.get(currentState
.cursorY
);
7117 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
7119 // If at the end of the visible screen, stop.
7120 if (currentState
.cursorX
== rightMargin
) {
7123 // Room for more image on the visible screen.
7124 currentState
.cursorX
++;
7127 cursorPosition(currentState
.cursorY
, x0
);
7133 * Parse a "Jexer" image string into a bitmap image, and overlay that
7134 * image onto the text cells.
7136 * @param pw width token
7137 * @param ph height token
7138 * @param ps scroll token
7139 * @param data pixel data
7141 private void parseJexerImage(final String pw
, final String ph
,
7142 final String ps
, final String data
) {
7145 int imageHeight
= 0;
7146 boolean scroll
= false;
7148 imageWidth
= Integer
.parseInt(pw
);
7149 imageHeight
= Integer
.parseInt(ph
);
7150 } catch (NumberFormatException e
) {
7154 if ((imageWidth
< 1)
7155 || (imageWidth
> 10000)
7156 || (imageHeight
< 1)
7157 || (imageHeight
> 10000)
7161 if (ps
.equals("1")) {
7163 } else if (ps
.equals("0")) {
7169 java
.util
.Base64
.Decoder base64
= java
.util
.Base64
.getDecoder();
7170 byte [] bytes
= base64
.decode(data
);
7171 if (bytes
.length
!= (imageWidth
* imageHeight
* 3)) {
7175 BufferedImage image
= new BufferedImage(imageWidth
, imageHeight
,
7176 BufferedImage
.TYPE_INT_ARGB
);
7178 for (int x
= 0; x
< imageWidth
; x
++) {
7179 for (int y
= 0; y
< imageHeight
; y
++) {
7180 int red
= bytes
[(y
* imageWidth
* 3) + (x
* 3) ];
7184 int green
= bytes
[(y
* imageWidth
* 3) + (x
* 3) + 1];
7188 int blue
= bytes
[(y
* imageWidth
* 3) + (x
* 3) + 2];
7192 int rgb
= 0xFF000000 | (red
<< 16) | (green
<< 8) | blue
;
7193 image
.setRGB(x
, y
, rgb
);
7200 * Break up the image into text cell sized pieces as a new array of
7203 * Note original column position x0.
7207 * 1. Advance (printCharacter(' ')) for horizontal increment, or
7208 * index (linefeed() + cursorPosition(y, x0)) for vertical
7211 * 2. Set (x, y) cell image data.
7213 * 3. For the right and bottom edges:
7215 * a. Render the text to pixels using Terminus font.
7217 * b. Blit the image on top of the text, using alpha channel.
7219 int cellColumns
= image
.getWidth() / textWidth
;
7220 if (cellColumns
* textWidth
< image
.getWidth()) {
7223 int cellRows
= image
.getHeight() / textHeight
;
7224 if (cellRows
* textHeight
< image
.getHeight()) {
7228 // Break the image up into an array of cells.
7229 Cell
[][] cells
= new Cell
[cellColumns
][cellRows
];
7231 for (int x
= 0; x
< cellColumns
; x
++) {
7232 for (int y
= 0; y
< cellRows
; y
++) {
7234 int width
= textWidth
;
7235 if ((x
+ 1) * textWidth
> image
.getWidth()) {
7236 width
= image
.getWidth() - (x
* textWidth
);
7238 int height
= textHeight
;
7239 if ((y
+ 1) * textHeight
> image
.getHeight()) {
7240 height
= image
.getHeight() - (y
* textHeight
);
7243 Cell cell
= new Cell();
7244 cell
.setImage(image
.getSubimage(x
* textWidth
,
7245 y
* textHeight
, width
, height
));
7251 int x0
= currentState
.cursorX
;
7252 for (int y
= 0; y
< cellRows
; y
++) {
7253 for (int x
= 0; x
< cellColumns
; x
++) {
7254 assert (currentState
.cursorX
<= rightMargin
);
7255 DisplayLine line
= display
.get(currentState
.cursorY
);
7256 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
7257 // If at the end of the visible screen, stop.
7258 if (currentState
.cursorX
== rightMargin
) {
7261 // Room for more image on the visible screen.
7262 currentState
.cursorX
++;
7264 if ((scroll
== true)
7265 || ((scroll
== false)
7266 && (currentState
.cursorY
< scrollRegionBottom
))
7270 cursorPosition(currentState
.cursorY
, x0
);