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
.BufferedOutputStream
;
34 import java
.io
.CharArrayWriter
;
35 import java
.io
.InputStream
;
36 import java
.io
.InputStreamReader
;
37 import java
.io
.IOException
;
38 import java
.io
.OutputStream
;
39 import java
.io
.OutputStreamWriter
;
40 import java
.io
.PrintWriter
;
41 import java
.io
.Reader
;
42 import java
.io
.UnsupportedEncodingException
;
43 import java
.io
.Writer
;
44 import java
.util
.ArrayList
;
45 import java
.util
.Collections
;
46 import java
.util
.HashMap
;
47 import java
.util
.List
;
49 import jexer
.TKeypress
;
50 import jexer
.backend
.GlyphMaker
;
51 import jexer
.bits
.Color
;
52 import jexer
.bits
.Cell
;
53 import jexer
.bits
.CellAttributes
;
54 import jexer
.bits
.StringUtils
;
55 import jexer
.event
.TInputEvent
;
56 import jexer
.event
.TKeypressEvent
;
57 import jexer
.event
.TMouseEvent
;
58 import jexer
.io
.ReadTimeoutException
;
59 import jexer
.io
.TimeoutInputStream
;
60 import static jexer
.TKeypress
.*;
63 * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
64 * including a scrollback buffer.
67 * It currently implements VT100, VT102, VT220, and XTERM with the following
71 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
72 * failure with XTERM. This is due to vttest failing to decode the UTF-8
76 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
77 * from VT100 are not supported.
80 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
81 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
82 * because DECSCL is not supported, it will fail the last part of the
83 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
86 * - Numeric/application keys from the number pad are not supported because
87 * they are not exposed from the TKeypress API.
90 * - VT52 HOLD SCREEN mode is not supported.
93 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
94 * numerators) are not rendered correctly.
97 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
99 public class ECMA48
implements Runnable
{
101 // ------------------------------------------------------------------------
102 // Constants --------------------------------------------------------------
103 // ------------------------------------------------------------------------
106 * The emulator can emulate several kinds of terminals.
108 public enum DeviceType
{
110 * DEC VT100 but also including the three VT102 functions.
131 * Parser character scan states.
133 private enum ScanState
{
149 VT52_DIRECT_CURSOR_ADDRESS
153 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
154 * can't really use it in keypress() because we do not see number pad
155 * events from TKeypress.
157 private enum KeypadMode
{
163 * Arrow keys can emit three different sequences (DECCKM or VT52
166 private enum ArrowKeyMode
{
173 * Available character sets for GL, GR, G0, G1, G2, G3.
175 private enum CharacterSet
{
196 * Single-shift states used by the C1 control characters SS2 (0x8E) and
199 private enum Singleshift
{
206 * VT220+ lockshift states.
208 private enum LockshiftMode
{
218 * XTERM mouse reporting protocols.
220 public enum MouseProtocol
{
229 * XTERM mouse reporting encodings.
231 private enum MouseEncoding
{
237 // ------------------------------------------------------------------------
238 // Variables --------------------------------------------------------------
239 // ------------------------------------------------------------------------
242 * The enclosing listening object.
244 private DisplayListener displayListener
;
247 * When true, the reader thread is expected to exit.
249 private volatile boolean stopReaderThread
= false;
254 private Thread readerThread
= null;
257 * The type of emulator to be.
259 private DeviceType type
= DeviceType
.VT102
;
262 * The scrollback buffer characters + attributes.
264 private volatile ArrayList
<DisplayLine
> scrollback
;
267 * The raw display buffer characters + attributes.
269 private volatile ArrayList
<DisplayLine
> display
;
272 * The maximum number of lines in the scrollback buffer.
274 private int maxScrollback
= 10000;
277 * The terminal's input. For type == XTERM, this is an InputStreamReader
278 * with UTF-8 encoding.
280 private Reader input
;
283 * The terminal's raw InputStream. This is used for type != XTERM.
285 private volatile TimeoutInputStream inputStream
;
288 * The terminal's output. For type == XTERM, this wraps an
289 * OutputStreamWriter with UTF-8 encoding.
291 private Writer output
;
294 * The terminal's raw OutputStream. This is used for type != XTERM.
296 private OutputStream outputStream
;
299 * Current scanning state.
301 private ScanState scanState
;
304 * Which mouse protocol is active.
306 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
309 * Which mouse encoding is active.
311 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
314 * A terminal may request that the mouse pointer be hidden using a
315 * Privacy Message containing either "hideMousePointer" or
316 * "showMousePointer". This is currently only used within Jexer by
317 * TTerminalWindow so that only the bottom-most instance of nested
318 * Jexer's draws the mouse within its application window.
320 private boolean hideMousePointer
= false;
323 * Physical display width. We start at 80x24, but the user can resize us
329 * Physical display height. We start at 80x24, but the user can resize
335 * Top margin of the scrolling region.
337 private int scrollRegionTop
;
340 * Bottom margin of the scrolling region.
342 private int scrollRegionBottom
;
345 * Right margin column number. This can be selected by the remote side
346 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
348 private int rightMargin
;
351 * Last character printed.
356 * VT100-style line wrapping: a character is placed in column 80 (or
357 * 132), but the line does NOT wrap until another character is written to
358 * column 1 of the next line, after which the cursor moves to column 2.
360 private boolean wrapLineFlag
;
363 * VT220 single shift flag.
365 private Singleshift singleshift
= Singleshift
.NONE
;
368 * true = insert characters, false = overwrite.
370 private boolean insertMode
= false;
373 * VT52 mode as selected by DECANM. True means VT52, false means
374 * ANSI. Default is ANSI.
376 private boolean vt52Mode
= false;
379 * Visible cursor (DECTCEM).
381 private boolean cursorVisible
= true;
384 * Screen title as set by the xterm OSC sequence. Lots of applications
385 * send a screenTitle regardless of whether it is an xterm client or not.
387 private String screenTitle
= "";
390 * Parameter characters being collected.
392 private List
<Integer
> csiParams
;
395 * Non-csi collect buffer.
397 private StringBuilder collectBuffer
;
400 * When true, use the G1 character set.
402 private boolean shiftOut
= false;
405 * Horizontal tab stop locations.
407 private List
<Integer
> tabStops
;
410 * S8C1T. True means 8bit controls, false means 7bit controls.
412 private boolean s8c1t
= false;
415 * Printer mode. True means send all output to printer, which discards
418 private boolean printerControllerMode
= false;
421 * LMN line mode. If true, linefeed() puts the cursor on the first
422 * column of the next line. If false, linefeed() puts the cursor one
423 * line down on the current line. The default is false.
425 private boolean newLineMode
= false;
428 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
430 private ArrowKeyMode arrowKeyMode
;
433 * Whether number pad keys send VT100 or VT52, application or numeric
436 @SuppressWarnings("unused")
437 private KeypadMode keypadMode
;
440 * When true, the terminal is in 132-column mode (DECCOLM).
442 private boolean columns132
= false;
445 * true = reverse video. Set by DECSCNM.
447 private boolean reverseVideo
= false;
450 * false = echo characters locally.
452 private boolean fullDuplex
= true;
455 * The current terminal state.
457 private SaveableState currentState
;
460 * The last saved terminal state.
462 private SaveableState savedState
;
465 * The 88- or 256-color support RGB colors.
467 private List
<Integer
> colors88
;
470 * Sixel collection buffer.
472 private StringBuilder sixelParseBuffer
;
475 * Sixel shared palette.
477 private HashMap
<Integer
, java
.awt
.Color
> sixelPalette
;
480 * The width of a character cell in pixels.
482 private int textWidth
= 16;
485 * The height of a character cell in pixels.
487 private int textHeight
= 20;
490 * The last used height of a character cell in pixels, only used for
493 private int lastTextHeight
= -1;
496 * The glyph drawer for full-width chars.
498 private GlyphMaker glyphMaker
= null;
501 * Input queue for keystrokes and mouse events to send to the remote
504 private ArrayList
<TInputEvent
> userQueue
= new ArrayList
<TInputEvent
>();
507 * DECSC/DECRC save/restore a subset of the total state. This class
508 * encapsulates those specific flags/modes.
510 private class SaveableState
{
513 * When true, cursor positions are relative to the scrolling region.
515 public boolean originMode
= false;
518 * The current editing X position.
520 public int cursorX
= 0;
523 * The current editing Y position.
525 public int cursorY
= 0;
528 * Which character set is currently selected in G0.
530 public CharacterSet g0Charset
= CharacterSet
.US
;
533 * Which character set is currently selected in G1.
535 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
538 * Which character set is currently selected in G2.
540 public CharacterSet g2Charset
= CharacterSet
.US
;
543 * Which character set is currently selected in G3.
545 public CharacterSet g3Charset
= CharacterSet
.US
;
548 * Which character set is currently selected in GR.
550 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
553 * The current drawing attributes.
555 public CellAttributes attr
;
560 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
565 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
570 public boolean lineWrap
= true;
575 public void reset() {
579 g0Charset
= CharacterSet
.US
;
580 g1Charset
= CharacterSet
.DRAWING
;
581 g2Charset
= CharacterSet
.US
;
582 g3Charset
= CharacterSet
.US
;
583 grCharset
= CharacterSet
.DRAWING
;
584 attr
= new CellAttributes();
585 glLockshift
= LockshiftMode
.NONE
;
586 grLockshift
= LockshiftMode
.NONE
;
591 * Copy attributes from another instance.
593 * @param that the other instance to match
595 public void setTo(final SaveableState that
) {
596 this.originMode
= that
.originMode
;
597 this.cursorX
= that
.cursorX
;
598 this.cursorY
= that
.cursorY
;
599 this.g0Charset
= that
.g0Charset
;
600 this.g1Charset
= that
.g1Charset
;
601 this.g2Charset
= that
.g2Charset
;
602 this.g3Charset
= that
.g3Charset
;
603 this.grCharset
= that
.grCharset
;
604 this.attr
= new CellAttributes();
605 this.attr
.setTo(that
.attr
);
606 this.glLockshift
= that
.glLockshift
;
607 this.grLockshift
= that
.grLockshift
;
608 this.lineWrap
= that
.lineWrap
;
612 * Public constructor.
614 public SaveableState() {
619 // ------------------------------------------------------------------------
620 // Constructors -----------------------------------------------------------
621 // ------------------------------------------------------------------------
624 * Public constructor.
626 * @param type one of the DeviceType constants to select VT100, VT102,
628 * @param inputStream an InputStream connected to the remote side. For
629 * type == XTERM, inputStream is converted to a Reader with UTF-8
631 * @param outputStream an OutputStream connected to the remote user. For
632 * type == XTERM, outputStream is converted to a Writer with UTF-8
634 * @param displayListener a callback to the outer display, or null for
635 * default VT100 behavior
636 * @throws UnsupportedEncodingException if an exception is thrown when
637 * creating the InputStreamReader
639 public ECMA48(final DeviceType type
, final InputStream inputStream
,
640 final OutputStream outputStream
, final DisplayListener displayListener
)
641 throws UnsupportedEncodingException
{
643 assert (inputStream
!= null);
644 assert (outputStream
!= null);
646 csiParams
= new ArrayList
<Integer
>();
647 tabStops
= new ArrayList
<Integer
>();
648 scrollback
= new ArrayList
<DisplayLine
>();
649 display
= new ArrayList
<DisplayLine
>();
652 if (inputStream
instanceof TimeoutInputStream
) {
653 this.inputStream
= (TimeoutInputStream
)inputStream
;
655 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
657 if (type
== DeviceType
.XTERM
) {
658 this.input
= new InputStreamReader(this.inputStream
, "UTF-8");
659 this.output
= new OutputStreamWriter(new
660 BufferedOutputStream(outputStream
), "UTF-8");
661 this.outputStream
= null;
664 this.outputStream
= new BufferedOutputStream(outputStream
);
666 this.displayListener
= displayListener
;
669 for (int i
= 0; i
< height
; i
++) {
670 display
.add(new DisplayLine(currentState
.attr
));
673 // Spin up the input reader
674 readerThread
= new Thread(this);
675 readerThread
.start();
678 // ------------------------------------------------------------------------
679 // Runnable ---------------------------------------------------------------
680 // ------------------------------------------------------------------------
683 * Read function runs on a separate thread.
685 public final void run() {
686 boolean utf8
= false;
687 boolean done
= false;
689 if (type
== DeviceType
.XTERM
) {
693 // available() will often return > 1, so we need to read in chunks to
695 char [] readBufferUTF8
= null;
696 byte [] readBuffer
= null;
698 readBufferUTF8
= new char[2048];
700 readBuffer
= new byte[2048];
703 while (!done
&& !stopReaderThread
) {
704 synchronized (userQueue
) {
705 while (userQueue
.size() > 0) {
706 handleUserEvent(userQueue
.remove(0));
711 int n
= inputStream
.available();
713 // System.err.printf("available() %d\n", n); System.err.flush();
715 if (readBufferUTF8
.length
< n
) {
716 // The buffer wasn't big enough, make it huger
717 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
720 readBufferUTF8
= new char[newSizeHalf
* 2];
723 if (readBuffer
.length
< n
) {
724 // The buffer wasn't big enough, make it huger
725 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
726 readBuffer
= new byte[newSizeHalf
* 2];
732 } catch (InterruptedException e
) {
741 rc
= input
.read(readBufferUTF8
, 0,
742 readBufferUTF8
.length
);
744 rc
= inputStream
.read(readBuffer
, 0,
747 } catch (ReadTimeoutException e
) {
751 // System.err.printf("read() %d\n", rc); System.err.flush();
756 // Don't step on UI events
757 synchronized (this) {
759 for (int i
= 0; i
< rc
;) {
760 int ch
= Character
.codePointAt(readBufferUTF8
,
762 i
+= Character
.charCount(ch
);
766 for (int i
= 0; i
< rc
; i
++) {
767 consume(readBuffer
[i
]);
771 // Permit my enclosing UI to know that I updated.
772 if (displayListener
!= null) {
773 displayListener
.displayChanged();
776 // System.err.println("end while loop"); System.err.flush();
777 } catch (IOException e
) {
780 // This is an unusual case. We want to see the stack trace,
781 // but it is related to the spawned process rather than the
782 // actual UI. We will generate the stack trace, and consume
783 // it as though it was emitted by the shell.
784 CharArrayWriter writer
= new CharArrayWriter();
785 // Send a ST and RIS to clear the emulator state.
787 writer
.write("\033\\\033c");
788 writer
.write("\n-----------------------------------\n");
789 e
.printStackTrace(new PrintWriter(writer
));
790 writer
.write("\n-----------------------------------\n");
791 } catch (IOException e2
) {
794 char [] stackTrace
= writer
.toCharArray();
795 for (int i
= 0; i
< stackTrace
.length
; i
++) {
796 if (stackTrace
[i
] == '\n') {
799 consume(stackTrace
[i
]);
803 } // while ((done == false) && (stopReaderThread == false))
805 // Let the rest of the world know that I am done.
806 stopReaderThread
= true;
809 inputStream
.cancelRead();
812 } catch (IOException e
) {
818 } catch (IOException e
) {
822 // Permit my enclosing UI to know that I updated.
823 if (displayListener
!= null) {
824 displayListener
.displayChanged();
827 // System.err.println("*** run() exiting..."); System.err.flush();
830 // ------------------------------------------------------------------------
831 // ECMA48 -----------------------------------------------------------------
832 // ------------------------------------------------------------------------
835 * Process keyboard and mouse events from the user.
837 * @param event the input event to consume
839 private void handleUserEvent(final TInputEvent event
) {
840 if (event
instanceof TKeypressEvent
) {
841 keypress(((TKeypressEvent
) event
).getKey());
843 if (event
instanceof TMouseEvent
) {
844 mouse((TMouseEvent
) event
);
849 * Add a keyboard and mouse event from the user to the queue.
851 * @param event the input event to consume
853 public void addUserEvent(final TInputEvent event
) {
854 synchronized (userQueue
) {
855 userQueue
.add(event
);
860 * Return the proper primary Device Attributes string.
862 * @return string to send to remote side that is appropriate for the
865 private String
deviceTypeResponse() {
868 // "I am a VT100 with advanced video option" (often VT102)
877 // "I am a VT220" - 7 bit version
879 return "\033[?62;1;6;9;4;22c";
880 // return "\033[?62;1;6;9;4;22;444c";
882 // "I am a VT220" - 8 bit version
883 return "\u009b?62;1;6;9;4;22c";
884 // return "\u009b?62;1;6;9;4;22;444c";
886 throw new IllegalArgumentException("Invalid device type: " + type
);
891 * Return the proper TERM environment variable for this device type.
893 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
894 * @return "vt100", "xterm", etc.
896 public static String
deviceTypeTerm(final DeviceType deviceType
) {
897 switch (deviceType
) {
911 throw new IllegalArgumentException("Invalid device type: "
917 * Return the proper LANG for this device type. Only XTERM devices know
918 * about UTF-8, the others are defined by their standard to be either
919 * 7-bit or 8-bit characters only.
921 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
922 * @param baseLang a base language without UTF-8 flag such as "C" or
924 * @return "en_US", "en_US.UTF-8", etc.
926 public static String
deviceTypeLang(final DeviceType deviceType
,
927 final String baseLang
) {
929 switch (deviceType
) {
937 return baseLang
+ ".UTF-8";
940 throw new IllegalArgumentException("Invalid device type: "
946 * Write a string directly to the remote side.
948 * @param str string to send
950 public void writeRemote(final String str
) {
951 if (stopReaderThread
) {
952 // Reader hit EOF, bail out now.
957 // System.err.printf("writeRemote() '%s'\n", str);
963 if (outputStream
== null) {
967 outputStream
.flush();
968 for (int i
= 0; i
< str
.length(); i
++) {
969 outputStream
.write(str
.charAt(i
));
971 outputStream
.flush();
972 } catch (IOException e
) {
978 if (output
== null) {
985 } catch (IOException e
) {
991 throw new IllegalArgumentException("Invalid device type: " + type
);
996 * Close the input and output streams and stop the reader thread. Note
997 * that it is safe to call this multiple times.
999 public final void close() {
1001 // Tell the reader thread to stop looking at input. It will close
1002 // the input streams.
1003 if (stopReaderThread
== false) {
1004 stopReaderThread
= true;
1006 readerThread
.join(1000);
1007 } catch (InterruptedException e
) {
1012 // Now close the output stream.
1017 if (outputStream
!= null) {
1019 outputStream
.close();
1020 } catch (IOException e
) {
1023 outputStream
= null;
1027 if (outputStream
!= null) {
1029 outputStream
.close();
1030 } catch (IOException e
) {
1033 outputStream
= null;
1035 if (output
!= null) {
1038 } catch (IOException e
) {
1045 throw new IllegalArgumentException("Invalid device type: " +
1051 * See if the reader thread is still running.
1053 * @return if true, we are still connected to / reading from the remote
1056 public final boolean isReading() {
1057 return (!stopReaderThread
);
1061 * Obtain a new blank display line for an external user
1062 * (e.g. TTerminalWindow).
1064 * @return new blank line
1066 public final DisplayLine
getBlankDisplayLine() {
1067 return new DisplayLine(currentState
.attr
);
1071 * Get the scrollback buffer.
1073 * @return the scrollback buffer
1075 public final List
<DisplayLine
> getScrollbackBuffer() {
1080 * Get the display buffer.
1082 * @return the display buffer
1084 public final List
<DisplayLine
> getDisplayBuffer() {
1089 * Get the visible display + scrollback buffer, offset by a specified
1090 * number of rows from the bottom.
1092 * @param visibleHeight the total height of the display to show
1093 * @param scrollBottom the number of rows from the bottom to scroll back
1094 * @return a copy of the display + scrollback buffers
1096 public final List
<DisplayLine
> getVisibleDisplay(final int visibleHeight
,
1097 final int scrollBottom
) {
1099 assert (visibleHeight
>= 0);
1100 assert (scrollBottom
>= 0);
1102 int visibleBottom
= scrollback
.size() + display
.size() - scrollBottom
;
1104 List
<DisplayLine
> preceedingBlankLines
= new ArrayList
<DisplayLine
>();
1105 int visibleTop
= visibleBottom
- visibleHeight
;
1106 if (visibleTop
< 0) {
1107 for (int i
= visibleTop
; i
< 0; i
++) {
1108 preceedingBlankLines
.add(getBlankDisplayLine());
1112 assert (visibleTop
>= 0);
1114 List
<DisplayLine
> displayLines
= new ArrayList
<DisplayLine
>();
1115 displayLines
.addAll(scrollback
);
1116 displayLines
.addAll(display
);
1118 List
<DisplayLine
> visibleLines
= new ArrayList
<DisplayLine
>();
1119 visibleLines
.addAll(preceedingBlankLines
);
1120 visibleLines
.addAll(displayLines
.subList(visibleTop
, visibleBottom
));
1122 // Fill in the blank lines on bottom
1123 int bottomBlankLines
= visibleHeight
- visibleLines
.size();
1124 assert (bottomBlankLines
>= 0);
1125 for (int i
= 0; i
< bottomBlankLines
; i
++) {
1126 visibleLines
.add(getBlankDisplayLine());
1129 return copyBuffer(visibleLines
);
1133 * Copy a display buffer.
1135 * @param buffer the buffer to copy
1136 * @return a deep copy of the buffer's data
1138 private List
<DisplayLine
> copyBuffer(final List
<DisplayLine
> buffer
) {
1139 ArrayList
<DisplayLine
> result
= new ArrayList
<DisplayLine
>(buffer
.size());
1140 for (DisplayLine line
: buffer
) {
1141 result
.add(new DisplayLine(line
));
1147 * Get the display width.
1149 * @return the width (usually 80 or 132)
1151 public final int getWidth() {
1156 * Set the display width.
1158 * @param width the new width
1160 public final synchronized void setWidth(final int width
) {
1162 rightMargin
= width
- 1;
1163 if (currentState
.cursorX
>= width
) {
1164 currentState
.cursorX
= width
- 1;
1166 if (savedState
.cursorX
>= width
) {
1167 savedState
.cursorX
= width
- 1;
1172 * Get the display height.
1174 * @return the height (usually 24)
1176 public final int getHeight() {
1181 * Set the display height.
1183 * @param height the new height
1185 public final synchronized void setHeight(final int height
) {
1186 int delta
= height
- this.height
;
1187 this.height
= height
;
1188 scrollRegionBottom
+= delta
;
1189 if (scrollRegionBottom
< 0) {
1190 scrollRegionBottom
= height
;
1192 if (scrollRegionTop
>= scrollRegionBottom
) {
1193 scrollRegionTop
= 0;
1195 if (currentState
.cursorY
>= height
) {
1196 currentState
.cursorY
= height
- 1;
1198 if (savedState
.cursorY
>= height
) {
1199 savedState
.cursorY
= height
- 1;
1201 while (display
.size() < height
) {
1202 DisplayLine line
= new DisplayLine(currentState
.attr
);
1203 line
.setReverseColor(reverseVideo
);
1206 while (display
.size() > height
) {
1207 scrollback
.add(display
.remove(0));
1212 * Get visible cursor flag.
1214 * @return if true, the cursor is visible
1216 public final boolean isCursorVisible() {
1217 return cursorVisible
;
1221 * Get the screen title as set by the xterm OSC sequence. Lots of
1222 * applications send a screenTitle regardless of whether it is an xterm
1225 * @return screen title
1227 public final String
getScreenTitle() {
1232 * Get 132 columns value.
1234 * @return if true, the terminal is in 132 column mode
1236 public final boolean isColumns132() {
1241 * Clear the CSI parameters and flags.
1243 private void toGround() {
1245 collectBuffer
= new StringBuilder(8);
1246 scanState
= ScanState
.GROUND
;
1250 * Reset the tab stops list.
1252 private void resetTabStops() {
1254 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
1255 tabStops
.add(Integer
.valueOf(i
* 8));
1260 * Reset the 88- or 256-colors.
1262 private void resetColors() {
1263 colors88
= new ArrayList
<Integer
>(256);
1264 for (int i
= 0; i
< 256; i
++) {
1268 // Set default system colors.
1269 colors88
.set(0, 0x00000000);
1270 colors88
.set(1, 0x00a80000);
1271 colors88
.set(2, 0x0000a800);
1272 colors88
.set(3, 0x00a85400);
1273 colors88
.set(4, 0x000000a8);
1274 colors88
.set(5, 0x00a800a8);
1275 colors88
.set(6, 0x0000a8a8);
1276 colors88
.set(7, 0x00a8a8a8);
1278 colors88
.set(8, 0x00545454);
1279 colors88
.set(9, 0x00fc5454);
1280 colors88
.set(10, 0x0054fc54);
1281 colors88
.set(11, 0x00fcfc54);
1282 colors88
.set(12, 0x005454fc);
1283 colors88
.set(13, 0x00fc54fc);
1284 colors88
.set(14, 0x0054fcfc);
1285 colors88
.set(15, 0x00fcfcfc);
1289 * Get the RGB value of one of the indexed colors.
1291 * @param index the color index
1292 * @return the RGB value
1294 private int get88Color(final int index
) {
1295 // System.err.print("get88Color: " + index);
1296 if ((index
< 0) || (index
> colors88
.size())) {
1297 // System.err.println(" -- UNKNOWN");
1300 // System.err.printf(" %08x\n", colors88.get(index));
1301 return colors88
.get(index
);
1305 * Set one of the indexed colors to a color specification.
1307 * @param index the color index
1308 * @param spec the specification, typically something like "rgb:aa/bb/cc"
1310 private void set88Color(final int index
, final String spec
) {
1311 // System.err.println("set88Color: " + index + " '" + spec + "'");
1313 if ((index
< 0) || (index
> colors88
.size())) {
1316 if (spec
.startsWith("rgb:")) {
1317 String
[] rgbTokens
= spec
.substring(4).split("/");
1318 if (rgbTokens
.length
== 3) {
1320 int rgb
= (Integer
.parseInt(rgbTokens
[0], 16) << 16);
1321 rgb
|= Integer
.parseInt(rgbTokens
[1], 16) << 8;
1322 rgb
|= Integer
.parseInt(rgbTokens
[2], 16);
1323 // System.err.printf(" set to %08x\n", rgb);
1324 colors88
.set(index
, rgb
);
1325 } catch (NumberFormatException e
) {
1332 if (spec
.toLowerCase().equals("black")) {
1333 colors88
.set(index
, 0x00000000);
1334 } else if (spec
.toLowerCase().equals("red")) {
1335 colors88
.set(index
, 0x00a80000);
1336 } else if (spec
.toLowerCase().equals("green")) {
1337 colors88
.set(index
, 0x0000a800);
1338 } else if (spec
.toLowerCase().equals("yellow")) {
1339 colors88
.set(index
, 0x00a85400);
1340 } else if (spec
.toLowerCase().equals("blue")) {
1341 colors88
.set(index
, 0x000000a8);
1342 } else if (spec
.toLowerCase().equals("magenta")) {
1343 colors88
.set(index
, 0x00a800a8);
1344 } else if (spec
.toLowerCase().equals("cyan")) {
1345 colors88
.set(index
, 0x0000a8a8);
1346 } else if (spec
.toLowerCase().equals("white")) {
1347 colors88
.set(index
, 0x00a8a8a8);
1353 * Reset the emulation state.
1355 private void reset() {
1357 currentState
= new SaveableState();
1358 savedState
= new SaveableState();
1359 scanState
= ScanState
.GROUND
;
1362 scrollRegionTop
= 0;
1363 scrollRegionBottom
= height
- 1;
1364 rightMargin
= width
- 1;
1365 newLineMode
= false;
1366 arrowKeyMode
= ArrowKeyMode
.ANSI
;
1367 keypadMode
= KeypadMode
.Numeric
;
1368 wrapLineFlag
= false;
1369 if (displayListener
!= null) {
1370 width
= displayListener
.getDisplayWidth();
1371 height
= displayListener
.getDisplayHeight();
1372 rightMargin
= width
- 1;
1380 newLineMode
= false;
1381 reverseVideo
= false;
1383 cursorVisible
= true;
1386 singleshift
= Singleshift
.NONE
;
1388 printerControllerMode
= false;
1391 mouseProtocol
= MouseProtocol
.OFF
;
1392 mouseEncoding
= MouseEncoding
.X10
;
1397 // Reset extra colors
1405 * Append a new line to the bottom of the display, adding lines off the
1406 * top to the scrollback buffer.
1408 private void newDisplayLine() {
1409 // Scroll the top line off into the scrollback buffer
1410 scrollback
.add(display
.get(0));
1411 if (scrollback
.size() > maxScrollback
) {
1412 scrollback
.remove(0);
1413 scrollback
.trimToSize();
1416 display
.trimToSize();
1417 DisplayLine line
= new DisplayLine(currentState
.attr
);
1418 line
.setReverseColor(reverseVideo
);
1423 * Wraps the current line.
1425 private void wrapCurrentLine() {
1426 if (currentState
.cursorY
== height
- 1) {
1429 if (currentState
.cursorY
< height
- 1) {
1430 currentState
.cursorY
++;
1432 currentState
.cursorX
= 0;
1436 * Handle a carriage return.
1438 private void carriageReturn() {
1439 currentState
.cursorX
= 0;
1440 wrapLineFlag
= false;
1444 * Reverse the color of the visible display.
1446 private void invertDisplayColors() {
1447 for (DisplayLine line
: display
) {
1448 line
.setReverseColor(!line
.isReverseColor());
1453 * Handle a linefeed.
1455 private void linefeed() {
1457 if (currentState
.cursorY
< scrollRegionBottom
) {
1458 // Increment screen y
1459 currentState
.cursorY
++;
1462 // Screen y does not increment
1465 * Two cases: either we're inside a scrolling region or not. If
1466 * the scrolling region bottom is the bottom of the screen, then
1467 * push the top line into the buffer. Else scroll the scrolling
1470 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1472 // We're at the bottom of the scroll region, AND the scroll
1473 // region is the entire screen.
1479 // We're at the bottom of the scroll region, AND the scroll
1480 // region is NOT the entire screen.
1481 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1486 currentState
.cursorX
= 0;
1488 wrapLineFlag
= false;
1492 * Prints one character to the display buffer.
1494 * @param ch character to display
1496 private void printCharacter(final int ch
) {
1497 int rightMargin
= this.rightMargin
;
1499 if (StringUtils
.width(ch
) == 2) {
1500 // This is a full-width character. Save two spaces, and then
1501 // draw the character as two image halves.
1502 int x0
= currentState
.cursorX
;
1503 int y0
= currentState
.cursorY
;
1504 printCharacter(' ');
1505 printCharacter(' ');
1506 if ((currentState
.cursorX
== x0
+ 2)
1507 && (currentState
.cursorY
== y0
)
1509 // We can draw both halves of the character.
1510 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1511 } else if ((currentState
.cursorX
== x0
+ 1)
1512 && (currentState
.cursorY
== y0
)
1514 // VT100 line wrap behavior: we should be at the right
1515 // margin. We can draw both halves of the character.
1516 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1518 // The character splits across the line. Draw the entire
1519 // character on the new line, giving one more space for it.
1520 x0
= currentState
.cursorX
- 1;
1521 y0
= currentState
.cursorY
;
1522 printCharacter(' ');
1523 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1528 // Check if we have double-width, and if so chop at 40/66 instead of
1530 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1531 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1534 // Check the unusually-complicated line wrapping conditions...
1535 if (currentState
.cursorX
== rightMargin
) {
1537 if (currentState
.lineWrap
== true) {
1539 * This case happens when: the cursor was already on the
1540 * right margin (either through printing or by an explicit
1541 * placement command), and a character was printed.
1543 * The line wraps only when a new character arrives AND the
1544 * cursor is already on the right margin AND has placed a
1545 * character in its cell. Easier to see than to explain.
1547 if (wrapLineFlag
== false) {
1549 * This block marks the case that we are in the margin
1550 * and the first character has been received and printed.
1552 wrapLineFlag
= true;
1555 * This block marks the case that we are in the margin
1556 * and the second character has been received and
1559 wrapLineFlag
= false;
1563 } else if (currentState
.cursorX
<= rightMargin
) {
1565 * This is the normal case: a character came in and was printed
1566 * to the left of the right margin column.
1569 // Turn off VT100 special-case flag
1570 wrapLineFlag
= false;
1573 // "Print" the character
1574 Cell newCell
= new Cell(ch
);
1575 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1576 newCellAttributes
.setTo(currentState
.attr
);
1577 DisplayLine line
= display
.get(currentState
.cursorY
);
1579 if (StringUtils
.width(ch
) == 1) {
1580 // Insert mode special case
1581 if (insertMode
== true) {
1582 line
.insert(currentState
.cursorX
, newCell
);
1584 // Replace an existing character
1585 line
.replace(currentState
.cursorX
, newCell
);
1588 // Increment horizontal
1589 if (wrapLineFlag
== false) {
1590 currentState
.cursorX
++;
1591 if (currentState
.cursorX
> rightMargin
) {
1592 currentState
.cursorX
--;
1599 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1600 * send to the remote side.
1602 * @param mouse mouse event received from the local user
1604 private void mouse(final TMouseEvent mouse
) {
1607 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1608 mouseProtocol, mouseEncoding, mouse);
1611 if (mouseEncoding
== MouseEncoding
.X10
) {
1612 // We will support X10 but only for (160,94) and smaller.
1613 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1618 switch (mouseProtocol
) {
1625 // Only report button presses
1626 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1632 // Only report button presses and releases
1633 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1634 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1642 * Only report button presses, button releases, and motions that
1643 * have a button down (i.e. drag-and-drop).
1645 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1646 if (!mouse
.isMouse1()
1647 && !mouse
.isMouse2()
1648 && !mouse
.isMouse3()
1649 && !mouse
.isMouseWheelUp()
1650 && !mouse
.isMouseWheelDown()
1658 // Report everything
1662 // Now encode the event
1663 StringBuilder sb
= new StringBuilder(6);
1664 if (mouseEncoding
== MouseEncoding
.SGR
) {
1665 sb
.append((char) 0x1B);
1668 if (mouse
.isMouse1()) {
1669 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1674 } else if (mouse
.isMouse2()) {
1675 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1680 } else if (mouse
.isMouse3()) {
1681 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1686 } else if (mouse
.isMouseWheelUp()) {
1688 } else if (mouse
.isMouseWheelDown()) {
1691 // This is motion with no buttons down.
1695 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1698 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1705 // X10 and UTF8 encodings
1706 sb
.append((char) 0x1B);
1709 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1710 sb
.append((char) (0x03 + 32));
1711 } else if (mouse
.isMouse1()) {
1712 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1713 sb
.append((char) (0x00 + 32 + 32));
1715 sb
.append((char) (0x00 + 32));
1717 } else if (mouse
.isMouse2()) {
1718 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1719 sb
.append((char) (0x01 + 32 + 32));
1721 sb
.append((char) (0x01 + 32));
1723 } else if (mouse
.isMouse3()) {
1724 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1725 sb
.append((char) (0x02 + 32 + 32));
1727 sb
.append((char) (0x02 + 32));
1729 } else if (mouse
.isMouseWheelUp()) {
1730 sb
.append((char) (0x04 + 64));
1731 } else if (mouse
.isMouseWheelDown()) {
1732 sb
.append((char) (0x05 + 64));
1734 // This is motion with no buttons down.
1735 sb
.append((char) (0x03 + 32));
1738 sb
.append((char) (mouse
.getX() + 33));
1739 sb
.append((char) (mouse
.getY() + 33));
1742 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1743 writeRemote(sb
.toString());
1747 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1748 * send to the remote side.
1750 * @param keypress keypress received from the local user
1752 private void keypress(final TKeypress keypress
) {
1753 writeRemote(keypressToString(keypress
));
1757 * Build one of the complex xterm keystroke sequences, storing the result in
1758 * xterm_keystroke_buffer.
1760 * @param ss3 the prefix to use based on VT100 state.
1761 * @param first the first character, usually a number.
1762 * @param first the last character, one of the following: ~ A B C D F H
1763 * @param ctrl whether or not ctrl is down
1764 * @param alt whether or not alt is down
1765 * @param shift whether or not shift is down
1766 * @return the buffer with the full key sequence
1768 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1769 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1771 StringBuilder sb
= new StringBuilder(ss3
);
1772 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1776 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1778 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1780 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1782 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1784 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1786 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1788 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1793 return sb
.toString();
1797 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1799 * @param keypress keypress received from the local user
1800 * @return string to transmit to the remote side
1802 @SuppressWarnings("fallthrough")
1803 private String
keypressToString(final TKeypress keypress
) {
1805 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1807 * If this is a control character, process it like it came from
1810 if (keypress
.getChar() < 0x20) {
1811 handleControlChar((char) keypress
.getChar());
1813 // Local echo for everything else
1814 printCharacter(keypress
.getChar());
1816 if (displayListener
!= null) {
1817 displayListener
.displayChanged();
1821 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1826 // Handle control characters
1827 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1828 StringBuilder sb
= new StringBuilder();
1829 int ch
= keypress
.getChar();
1831 sb
.append(Character
.toChars(ch
));
1832 return sb
.toString();
1835 // Handle alt characters
1836 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1837 StringBuilder sb
= new StringBuilder("\033");
1838 int ch
= keypress
.getChar();
1839 sb
.append(Character
.toChars(ch
));
1840 return sb
.toString();
1843 if (keypress
.equals(kbBackspaceDel
)) {
1856 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1859 switch (arrowKeyMode
) {
1861 return xtermBuildKeySequence("\033[", '1', 'D',
1862 keypress
.isCtrl(), keypress
.isAlt(),
1863 keypress
.isShift());
1865 return xtermBuildKeySequence("\033", '1', 'D',
1866 keypress
.isCtrl(), keypress
.isAlt(),
1867 keypress
.isShift());
1869 return xtermBuildKeySequence("\033O", '1', 'D',
1870 keypress
.isCtrl(), keypress
.isAlt(),
1871 keypress
.isShift());
1874 switch (arrowKeyMode
) {
1885 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1888 switch (arrowKeyMode
) {
1890 return xtermBuildKeySequence("\033[", '1', 'C',
1891 keypress
.isCtrl(), keypress
.isAlt(),
1892 keypress
.isShift());
1894 return xtermBuildKeySequence("\033", '1', 'C',
1895 keypress
.isCtrl(), keypress
.isAlt(),
1896 keypress
.isShift());
1898 return xtermBuildKeySequence("\033O", '1', 'C',
1899 keypress
.isCtrl(), keypress
.isAlt(),
1900 keypress
.isShift());
1903 switch (arrowKeyMode
) {
1914 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1917 switch (arrowKeyMode
) {
1919 return xtermBuildKeySequence("\033[", '1', 'A',
1920 keypress
.isCtrl(), keypress
.isAlt(),
1921 keypress
.isShift());
1923 return xtermBuildKeySequence("\033", '1', 'A',
1924 keypress
.isCtrl(), keypress
.isAlt(),
1925 keypress
.isShift());
1927 return xtermBuildKeySequence("\033O", '1', 'A',
1928 keypress
.isCtrl(), keypress
.isAlt(),
1929 keypress
.isShift());
1932 switch (arrowKeyMode
) {
1943 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1946 switch (arrowKeyMode
) {
1948 return xtermBuildKeySequence("\033[", '1', 'B',
1949 keypress
.isCtrl(), keypress
.isAlt(),
1950 keypress
.isShift());
1952 return xtermBuildKeySequence("\033", '1', 'B',
1953 keypress
.isCtrl(), keypress
.isAlt(),
1954 keypress
.isShift());
1956 return xtermBuildKeySequence("\033O", '1', 'B',
1957 keypress
.isCtrl(), keypress
.isAlt(),
1958 keypress
.isShift());
1961 switch (arrowKeyMode
) {
1972 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1975 switch (arrowKeyMode
) {
1977 return xtermBuildKeySequence("\033[", '1', 'H',
1978 keypress
.isCtrl(), keypress
.isAlt(),
1979 keypress
.isShift());
1981 return xtermBuildKeySequence("\033", '1', 'H',
1982 keypress
.isCtrl(), keypress
.isAlt(),
1983 keypress
.isShift());
1985 return xtermBuildKeySequence("\033O", '1', 'H',
1986 keypress
.isCtrl(), keypress
.isAlt(),
1987 keypress
.isShift());
1990 switch (arrowKeyMode
) {
2001 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
2004 switch (arrowKeyMode
) {
2006 return xtermBuildKeySequence("\033[", '1', 'F',
2007 keypress
.isCtrl(), keypress
.isAlt(),
2008 keypress
.isShift());
2010 return xtermBuildKeySequence("\033", '1', 'F',
2011 keypress
.isCtrl(), keypress
.isAlt(),
2012 keypress
.isShift());
2014 return xtermBuildKeySequence("\033O", '1', 'F',
2015 keypress
.isCtrl(), keypress
.isAlt(),
2016 keypress
.isShift());
2019 switch (arrowKeyMode
) {
2030 if (keypress
.equals(kbF1
)) {
2038 if (keypress
.equals(kbF2
)) {
2046 if (keypress
.equals(kbF3
)) {
2054 if (keypress
.equals(kbF4
)) {
2062 if (keypress
.equals(kbF5
)) {
2075 if (keypress
.equals(kbF6
)) {
2088 if (keypress
.equals(kbF7
)) {
2101 if (keypress
.equals(kbF8
)) {
2114 if (keypress
.equals(kbF9
)) {
2127 if (keypress
.equals(kbF10
)) {
2140 if (keypress
.equals(kbF11
)) {
2144 if (keypress
.equals(kbF12
)) {
2148 if (keypress
.equals(kbShiftF1
)) {
2153 if (type
== DeviceType
.XTERM
) {
2159 if (keypress
.equals(kbShiftF2
)) {
2164 if (type
== DeviceType
.XTERM
) {
2170 if (keypress
.equals(kbShiftF3
)) {
2175 if (type
== DeviceType
.XTERM
) {
2181 if (keypress
.equals(kbShiftF4
)) {
2186 if (type
== DeviceType
.XTERM
) {
2192 if (keypress
.equals(kbShiftF5
)) {
2194 return "\033[15;2~";
2197 if (keypress
.equals(kbShiftF6
)) {
2199 return "\033[17;2~";
2202 if (keypress
.equals(kbShiftF7
)) {
2204 return "\033[18;2~";
2207 if (keypress
.equals(kbShiftF8
)) {
2209 return "\033[19;2~";
2212 if (keypress
.equals(kbShiftF9
)) {
2214 return "\033[20;2~";
2217 if (keypress
.equals(kbShiftF10
)) {
2219 return "\033[21;2~";
2222 if (keypress
.equals(kbShiftF11
)) {
2224 return "\033[23;2~";
2227 if (keypress
.equals(kbShiftF12
)) {
2229 return "\033[24;2~";
2232 if (keypress
.equals(kbCtrlF1
)) {
2237 if (type
== DeviceType
.XTERM
) {
2243 if (keypress
.equals(kbCtrlF2
)) {
2248 if (type
== DeviceType
.XTERM
) {
2254 if (keypress
.equals(kbCtrlF3
)) {
2259 if (type
== DeviceType
.XTERM
) {
2265 if (keypress
.equals(kbCtrlF4
)) {
2270 if (type
== DeviceType
.XTERM
) {
2276 if (keypress
.equals(kbCtrlF5
)) {
2278 return "\033[15;5~";
2281 if (keypress
.equals(kbCtrlF6
)) {
2283 return "\033[17;5~";
2286 if (keypress
.equals(kbCtrlF7
)) {
2288 return "\033[18;5~";
2291 if (keypress
.equals(kbCtrlF8
)) {
2293 return "\033[19;5~";
2296 if (keypress
.equals(kbCtrlF9
)) {
2298 return "\033[20;5~";
2301 if (keypress
.equals(kbCtrlF10
)) {
2303 return "\033[21;5~";
2306 if (keypress
.equals(kbCtrlF11
)) {
2308 return "\033[23;5~";
2311 if (keypress
.equals(kbCtrlF12
)) {
2313 return "\033[24;5~";
2316 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2319 return xtermBuildKeySequence("\033[", '5', '~',
2320 keypress
.isCtrl(), keypress
.isAlt(),
2321 keypress
.isShift());
2327 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2330 return xtermBuildKeySequence("\033[", '6', '~',
2331 keypress
.isCtrl(), keypress
.isAlt(),
2332 keypress
.isShift());
2338 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2341 return xtermBuildKeySequence("\033[", '2', '~',
2342 keypress
.isCtrl(), keypress
.isAlt(),
2343 keypress
.isShift());
2349 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2352 return xtermBuildKeySequence("\033[", '3', '~',
2353 keypress
.isCtrl(), keypress
.isAlt(),
2354 keypress
.isShift());
2356 // Delete sends real delete for VTxxx
2361 if (keypress
.equals(kbEnter
)) {
2365 if (keypress
.equals(kbEsc
)) {
2369 if (keypress
.equals(kbAltEsc
)) {
2373 if (keypress
.equals(kbTab
)) {
2377 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2378 (keypress
.equals(kbShiftTab
))
2388 // Non-alt, non-ctrl characters
2389 if (!keypress
.isFnKey()) {
2390 StringBuilder sb
= new StringBuilder();
2391 sb
.append(Character
.toChars(keypress
.getChar()));
2392 return sb
.toString();
2398 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2401 * @param ch 8-bit character from the remote side
2402 * @param charsetGl character set defined for GL
2403 * @param charsetGr character set defined for GR
2404 * @return character to display on the screen
2406 private char mapCharacterCharset(final int ch
,
2407 final CharacterSet charsetGl
,
2408 final CharacterSet charsetGr
) {
2410 int lookupChar
= ch
;
2411 CharacterSet lookupCharset
= charsetGl
;
2414 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2415 lookupCharset
= charsetGr
;
2419 switch (lookupCharset
) {
2422 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2425 return DECCharacterSets
.UK
[lookupChar
];
2428 return DECCharacterSets
.US_ASCII
[lookupChar
];
2431 return DECCharacterSets
.NL
[lookupChar
];
2434 return DECCharacterSets
.FI
[lookupChar
];
2437 return DECCharacterSets
.FR
[lookupChar
];
2440 return DECCharacterSets
.FR_CA
[lookupChar
];
2443 return DECCharacterSets
.DE
[lookupChar
];
2446 return DECCharacterSets
.IT
[lookupChar
];
2449 return DECCharacterSets
.NO
[lookupChar
];
2452 return DECCharacterSets
.ES
[lookupChar
];
2455 return DECCharacterSets
.SV
[lookupChar
];
2458 return DECCharacterSets
.SWISS
[lookupChar
];
2460 case DEC_SUPPLEMENTAL
:
2461 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2464 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2467 return DECCharacterSets
.US_ASCII
[lookupChar
];
2470 return DECCharacterSets
.US_ASCII
[lookupChar
];
2473 throw new IllegalArgumentException("Invalid character set value: "
2479 * Map an 8-bit byte into a printable character.
2481 * @param ch either 8-bit or Unicode character from the remote side
2482 * @return character to display on the screen
2484 private int mapCharacter(final int ch
) {
2486 // Unicode character, just return it
2490 CharacterSet charsetGl
= currentState
.g0Charset
;
2491 CharacterSet charsetGr
= currentState
.grCharset
;
2493 if (vt52Mode
== true) {
2494 if (shiftOut
== true) {
2495 // Shifted out character, pull from VT52 graphics
2496 charsetGl
= currentState
.g1Charset
;
2497 charsetGr
= CharacterSet
.US
;
2500 charsetGl
= currentState
.g0Charset
;
2501 charsetGr
= CharacterSet
.US
;
2504 // Pull the character
2505 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2509 if (shiftOut
== true) {
2510 // Shifted out character, pull from G1
2511 charsetGl
= currentState
.g1Charset
;
2512 charsetGr
= currentState
.grCharset
;
2514 // Pull the character
2515 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2519 if (singleshift
== Singleshift
.SS2
) {
2521 singleshift
= Singleshift
.NONE
;
2523 // Shifted out character, pull from G2
2524 charsetGl
= currentState
.g2Charset
;
2525 charsetGr
= currentState
.grCharset
;
2529 if (singleshift
== Singleshift
.SS3
) {
2531 singleshift
= Singleshift
.NONE
;
2533 // Shifted out character, pull from G3
2534 charsetGl
= currentState
.g3Charset
;
2535 charsetGr
= currentState
.grCharset
;
2538 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2539 // Check for locking shift
2541 switch (currentState
.glLockshift
) {
2544 throw new IllegalArgumentException("programming bug");
2547 throw new IllegalArgumentException("programming bug");
2550 throw new IllegalArgumentException("programming bug");
2554 charsetGl
= currentState
.g2Charset
;
2559 charsetGl
= currentState
.g3Charset
;
2564 charsetGl
= currentState
.g0Charset
;
2568 switch (currentState
.grLockshift
) {
2571 throw new IllegalArgumentException("programming bug");
2574 throw new IllegalArgumentException("programming bug");
2578 charsetGr
= currentState
.g1Charset
;
2583 charsetGr
= currentState
.g2Charset
;
2588 charsetGr
= currentState
.g3Charset
;
2593 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2600 // Pull the character
2601 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2605 * Scroll the text within a scrolling region up n lines.
2607 * @param regionTop top row of the scrolling region
2608 * @param regionBottom bottom row of the scrolling region
2609 * @param n number of lines to scroll
2611 private void scrollingRegionScrollUp(final int regionTop
,
2612 final int regionBottom
, final int n
) {
2614 if (regionTop
>= regionBottom
) {
2618 // Sanity check: see if there will be any characters left after the
2620 if (regionBottom
+ 1 - regionTop
<= n
) {
2621 // There won't be anything left in the region, so just call
2622 // eraseScreen() and return.
2623 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2627 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2628 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2629 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2631 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2632 - remaining
, regionBottom
+ 1);
2633 display
= new ArrayList
<DisplayLine
>(displayTop
);
2634 display
.addAll(displayMiddle
);
2635 for (int i
= 0; i
< n
; i
++) {
2636 DisplayLine line
= new DisplayLine(currentState
.attr
);
2637 line
.setReverseColor(reverseVideo
);
2640 display
.addAll(displayBottom
);
2642 assert (display
.size() == height
);
2646 * Scroll the text within a scrolling region down n lines.
2648 * @param regionTop top row of the scrolling region
2649 * @param regionBottom bottom row of the scrolling region
2650 * @param n number of lines to scroll
2652 private void scrollingRegionScrollDown(final int regionTop
,
2653 final int regionBottom
, final int n
) {
2655 if (regionTop
>= regionBottom
) {
2659 // Sanity check: see if there will be any characters left after the
2661 if (regionBottom
+ 1 - regionTop
<= n
) {
2662 // There won't be anything left in the region, so just call
2663 // eraseScreen() and return.
2664 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2668 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2669 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2670 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2672 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2673 regionTop
+ remaining
);
2674 display
= new ArrayList
<DisplayLine
>(displayTop
);
2675 for (int i
= 0; i
< n
; i
++) {
2676 DisplayLine line
= new DisplayLine(currentState
.attr
);
2677 line
.setReverseColor(reverseVideo
);
2680 display
.addAll(displayMiddle
);
2681 display
.addAll(displayBottom
);
2683 assert (display
.size() == height
);
2687 * Process a control character.
2689 * @param ch 8-bit character from the remote side
2691 private void handleControlChar(final char ch
) {
2692 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2703 // Transmit the answerback message.
2714 cursorLeft(1, false);
2719 advanceToNextTabStop();
2745 currentState
.glLockshift
= LockshiftMode
.NONE
;
2751 currentState
.glLockshift
= LockshiftMode
.NONE
;
2776 singleshift
= Singleshift
.SS2
;
2781 singleshift
= Singleshift
.SS3
;
2791 * Advance the cursor to the next tab stop.
2793 private void advanceToNextTabStop() {
2794 if (tabStops
.size() == 0) {
2795 // Go to the rightmost column
2796 cursorRight(rightMargin
- currentState
.cursorX
, false);
2799 for (Integer stop
: tabStops
) {
2800 if (stop
> currentState
.cursorX
) {
2801 cursorRight(stop
- currentState
.cursorX
, false);
2806 * We got here, meaning there isn't a tab stop beyond the current
2807 * cursor position. Place the cursor of the right-most edge of the
2810 cursorRight(rightMargin
- currentState
.cursorX
, false);
2814 * Save a character into the collect buffer.
2816 * @param ch character to save
2818 private void collect(final char ch
) {
2819 collectBuffer
.append(ch
);
2823 * Save a byte into the CSI parameters buffer.
2825 * @param ch byte to save
2827 private void param(final byte ch
) {
2828 if (csiParams
.size() == 0) {
2829 csiParams
.add(Integer
.valueOf(0));
2831 Integer x
= csiParams
.get(csiParams
.size() - 1);
2832 if ((ch
>= '0') && (ch
<= '9')) {
2835 csiParams
.set(csiParams
.size() - 1, x
);
2838 if ((ch
== ';') && (csiParams
.size() < 16)) {
2839 csiParams
.add(Integer
.valueOf(0));
2844 * Get a CSI parameter value, with a default.
2846 * @param position parameter index. 0 is the first parameter.
2847 * @param defaultValue value to use if csiParams[position] doesn't exist
2848 * @return parameter value
2850 private int getCsiParam(final int position
, final int defaultValue
) {
2851 if (csiParams
.size() < position
+ 1) {
2852 return defaultValue
;
2854 return csiParams
.get(position
).intValue();
2858 * Get a CSI parameter value, clamped to within min/max.
2860 * @param position parameter index. 0 is the first parameter.
2861 * @param defaultValue value to use if csiParams[position] doesn't exist
2862 * @param minValue minimum value inclusive
2863 * @param maxValue maximum value inclusive
2864 * @return parameter value
2866 private int getCsiParam(final int position
, final int defaultValue
,
2867 final int minValue
, final int maxValue
) {
2869 assert (minValue
<= maxValue
);
2870 int value
= getCsiParam(position
, defaultValue
);
2871 if (value
< minValue
) {
2874 if (value
> maxValue
) {
2881 * Set or unset a toggle.
2883 * @param value true for set ('h'), false for reset ('l')
2885 private void setToggle(final boolean value
) {
2886 boolean decPrivateModeFlag
= false;
2888 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2889 if (collectBuffer
.charAt(i
) == '?') {
2890 decPrivateModeFlag
= true;
2895 for (Integer i
: csiParams
) {
2900 if (decPrivateModeFlag
== true) {
2902 if (value
== true) {
2903 // Use application arrow keys
2904 arrowKeyMode
= ArrowKeyMode
.VT100
;
2906 // Use ANSI arrow keys
2907 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2912 if (decPrivateModeFlag
== true) {
2913 if (value
== false) {
2917 arrowKeyMode
= ArrowKeyMode
.VT52
;
2920 * From the VT102 docs: "You use ANSI mode to select
2921 * most terminal features; the terminal uses the same
2922 * features when it switches to VT52 mode. You
2923 * cannot, however, change most of these features in
2926 * In other words, do not reset any other attributes
2927 * when switching between VT52 submode and ANSI.
2929 * HOWEVER, the real vt100 does switch the character
2930 * set according to Usenet.
2932 currentState
.g0Charset
= CharacterSet
.US
;
2933 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2936 if ((type
== DeviceType
.VT220
)
2937 || (type
== DeviceType
.XTERM
)) {
2939 // VT52 mode is explicitly 7-bit
2941 singleshift
= Singleshift
.NONE
;
2946 if (value
== true) {
2947 // Turn off keyboard
2956 if (decPrivateModeFlag
== true) {
2958 if (value
== true) {
2965 if ((displayListener
!= null)
2966 && (type
== DeviceType
.XTERM
)
2968 // For xterms, reset to the actual width, not 80
2970 width
= displayListener
.getDisplayWidth();
2971 rightMargin
= width
- 1;
2974 width
= rightMargin
+ 1;
2977 // Entire screen is cleared, and scrolling region is
2979 eraseScreen(0, 0, height
- 1, width
- 1, false);
2980 scrollRegionTop
= 0;
2981 scrollRegionBottom
= height
- 1;
2982 // Also home the cursor
2983 cursorPosition(0, 0);
2987 if (decPrivateModeFlag
== true) {
2989 if (value
== true) {
2998 if (value
== true) {
3006 if (decPrivateModeFlag
== true) {
3008 if (value
== true) {
3010 * Set selects reverse screen, a white screen
3011 * background with black characters.
3013 if (reverseVideo
!= true) {
3015 * If in normal video, switch it back
3017 invertDisplayColors();
3019 reverseVideo
= true;
3022 * Reset selects normal screen, a black screen
3023 * background with white characters.
3025 if (reverseVideo
== true) {
3027 * If in reverse video already, switch it back
3029 invertDisplayColors();
3031 reverseVideo
= false;
3036 if (decPrivateModeFlag
== true) {
3038 if (value
== true) {
3039 // Origin is relative to scroll region cursor.
3040 // Cursor can NEVER leave scrolling region.
3041 currentState
.originMode
= true;
3042 cursorPosition(0, 0);
3044 // Origin is absolute to entire screen. Cursor can
3045 // leave the scrolling region via cup() and hvp().
3046 currentState
.originMode
= false;
3047 cursorPosition(0, 0);
3052 if (decPrivateModeFlag
== true) {
3054 if (value
== true) {
3056 currentState
.lineWrap
= true;
3058 // Turn linewrap off
3059 currentState
.lineWrap
= false;
3064 if (decPrivateModeFlag
== true) {
3066 if (value
== true) {
3067 // Keyboard auto-repeat on
3070 // Keyboard auto-repeat off
3076 if (decPrivateModeFlag
== false) {
3078 if (value
== true) {
3088 if (decPrivateModeFlag
== true) {
3094 if (decPrivateModeFlag
== true) {
3100 if (decPrivateModeFlag
== false) {
3102 if (value
== true) {
3104 * Set causes a received linefeed, form feed, or
3105 * vertical tab to move cursor to first column of
3106 * next line. RETURN transmits both a carriage return
3107 * and linefeed. This selection is also called new
3113 * Reset causes a received linefeed, form feed, or
3114 * vertical tab to move cursor to next line in
3115 * current column. RETURN transmits a carriage
3118 newLineMode
= false;
3124 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3125 if (decPrivateModeFlag
== true) {
3127 if (value
== true) {
3129 cursorVisible
= true;
3132 cursorVisible
= false;
3139 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3140 if (decPrivateModeFlag
== true) {
3142 if (value
== true) {
3143 // Select national mode NRC
3146 // Select multi-national mode
3155 if (type
== DeviceType
.XTERM
) {
3156 if (decPrivateModeFlag
== true) {
3157 if (value
== true) {
3158 // Enable sixel scrolling (default).
3161 // Disable sixel scrolling.
3170 if ((type
== DeviceType
.XTERM
)
3171 && (decPrivateModeFlag
== true)
3173 // Mouse: normal tracking mode
3174 if (value
== true) {
3175 mouseProtocol
= MouseProtocol
.NORMAL
;
3177 mouseProtocol
= MouseProtocol
.OFF
;
3183 if ((type
== DeviceType
.XTERM
)
3184 && (decPrivateModeFlag
== true)
3186 // Mouse: normal tracking mode
3187 if (value
== true) {
3188 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
3190 mouseProtocol
= MouseProtocol
.OFF
;
3196 if ((type
== DeviceType
.XTERM
)
3197 && (decPrivateModeFlag
== true)
3199 // Mouse: Any-event tracking mode
3200 if (value
== true) {
3201 mouseProtocol
= MouseProtocol
.ANYEVENT
;
3203 mouseProtocol
= MouseProtocol
.OFF
;
3209 if ((type
== DeviceType
.XTERM
)
3210 && (decPrivateModeFlag
== true)
3212 // Mouse: UTF-8 coordinates
3213 if (value
== true) {
3214 mouseEncoding
= MouseEncoding
.UTF8
;
3216 mouseEncoding
= MouseEncoding
.X10
;
3222 if ((type
== DeviceType
.XTERM
)
3223 && (decPrivateModeFlag
== true)
3225 // Mouse: SGR coordinates
3226 if (value
== true) {
3227 mouseEncoding
= MouseEncoding
.SGR
;
3229 mouseEncoding
= MouseEncoding
.X10
;
3235 if (type
== DeviceType
.XTERM
) {
3236 if (decPrivateModeFlag
== true) {
3237 if (value
== true) {
3238 // Use private color registers for each sixel
3239 // graphic (default).
3240 sixelPalette
= null;
3242 // Use shared color registers for each sixel
3244 sixelPalette
= new HashMap
<Integer
, java
.awt
.Color
>();
3258 * DECSC - Save cursor.
3260 private void decsc() {
3261 savedState
.setTo(currentState
);
3265 * DECRC - Restore cursor.
3267 private void decrc() {
3268 currentState
.setTo(savedState
);
3274 private void ind() {
3275 // Move the cursor and scroll if necessary. If at the bottom line
3276 // already, a scroll up is supposed to be performed.
3277 if (currentState
.cursorY
== scrollRegionBottom
) {
3278 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3280 cursorDown(1, true);
3284 * RI - Reverse index.
3287 // Move the cursor and scroll if necessary. If at the top line
3288 // already, a scroll down is supposed to be performed.
3289 if (currentState
.cursorY
== scrollRegionTop
) {
3290 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3298 private void nel() {
3299 // Move the cursor and scroll if necessary. If at the bottom line
3300 // already, a scroll up is supposed to be performed.
3301 if (currentState
.cursorY
== scrollRegionBottom
) {
3302 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3304 cursorDown(1, true);
3306 // Reset to the beginning of the next line
3307 currentState
.cursorX
= 0;
3311 * DECKPAM - Keypad application mode.
3313 private void deckpam() {
3314 keypadMode
= KeypadMode
.Application
;
3318 * DECKPNM - Keypad numeric mode.
3320 private void deckpnm() {
3321 keypadMode
= KeypadMode
.Numeric
;
3327 * @param n number of spaces to move
3328 * @param honorScrollRegion if true, then do nothing if the cursor is
3329 * outside the scrolling region
3331 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3335 * Special case: if a user moves the cursor from the right margin, we
3336 * have to reset the VT100 right margin flag.
3339 wrapLineFlag
= false;
3342 for (int i
= 0; i
< n
; i
++) {
3343 if (honorScrollRegion
== true) {
3344 // Honor the scrolling region
3345 if ((currentState
.cursorY
< scrollRegionTop
)
3346 || (currentState
.cursorY
> scrollRegionBottom
)
3348 // Outside region, do nothing
3351 // Inside region, go up
3352 top
= scrollRegionTop
;
3354 // Non-scrolling case
3358 if (currentState
.cursorY
> top
) {
3359 currentState
.cursorY
--;
3365 * Move down n spaces.
3367 * @param n number of spaces to move
3368 * @param honorScrollRegion if true, then do nothing if the cursor is
3369 * outside the scrolling region
3371 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3375 * Special case: if a user moves the cursor from the right margin, we
3376 * have to reset the VT100 right margin flag.
3379 wrapLineFlag
= false;
3382 for (int i
= 0; i
< n
; i
++) {
3384 if (honorScrollRegion
== true) {
3385 // Honor the scrolling region
3386 if (currentState
.cursorY
> scrollRegionBottom
) {
3387 // Outside region, do nothing
3390 // Inside region, go down
3391 bottom
= scrollRegionBottom
;
3393 // Non-scrolling case
3394 bottom
= height
- 1;
3397 if (currentState
.cursorY
< bottom
) {
3398 currentState
.cursorY
++;
3404 * Move left n spaces.
3406 * @param n number of spaces to move
3407 * @param honorScrollRegion if true, then do nothing if the cursor is
3408 * outside the scrolling region
3410 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3412 * Special case: if a user moves the cursor from the right margin, we
3413 * have to reset the VT100 right margin flag.
3416 wrapLineFlag
= false;
3419 for (int i
= 0; i
< n
; i
++) {
3420 if (honorScrollRegion
== true) {
3421 // Honor the scrolling region
3422 if ((currentState
.cursorY
< scrollRegionTop
)
3423 || (currentState
.cursorY
> scrollRegionBottom
)
3425 // Outside region, do nothing
3430 if (currentState
.cursorX
> 0) {
3431 currentState
.cursorX
--;
3437 * Move right n spaces.
3439 * @param n number of spaces to move
3440 * @param honorScrollRegion if true, then do nothing if the cursor is
3441 * outside the scrolling region
3443 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3444 int rightMargin
= this.rightMargin
;
3447 * Special case: if a user moves the cursor from the right margin, we
3448 * have to reset the VT100 right margin flag.
3451 wrapLineFlag
= false;
3454 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3455 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3458 for (int i
= 0; i
< n
; i
++) {
3459 if (honorScrollRegion
== true) {
3460 // Honor the scrolling region
3461 if ((currentState
.cursorY
< scrollRegionTop
)
3462 || (currentState
.cursorY
> scrollRegionBottom
)
3464 // Outside region, do nothing
3469 if (currentState
.cursorX
< rightMargin
) {
3470 currentState
.cursorX
++;
3476 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3478 * @param row row to move to
3479 * @param col column to move to
3481 private void cursorPosition(int row
, final int col
) {
3482 int rightMargin
= this.rightMargin
;
3487 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3488 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3491 // Set column number
3492 currentState
.cursorX
= col
;
3494 // Sanity check, bring column back to margin.
3495 if (currentState
.cursorX
> rightMargin
) {
3496 currentState
.cursorX
= rightMargin
;
3500 if (currentState
.originMode
== true) {
3501 row
+= scrollRegionTop
;
3503 if (currentState
.cursorY
< row
) {
3504 cursorDown(row
- currentState
.cursorY
, false);
3505 } else if (currentState
.cursorY
> row
) {
3506 cursorUp(currentState
.cursorY
- row
, false);
3509 wrapLineFlag
= false;
3513 * HTS - Horizontal tabulation set.
3515 private void hts() {
3516 for (Integer stop
: tabStops
) {
3517 if (stop
== currentState
.cursorX
) {
3518 // Already have a tab stop here
3523 // Append a tab stop to the end of the array and resort them
3524 tabStops
.add(currentState
.cursorX
);
3525 Collections
.sort(tabStops
);
3529 * DECSWL - Single-width line.
3531 private void decswl() {
3532 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3533 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3537 * DECDWL - Double-width line.
3539 private void decdwl() {
3540 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3541 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3545 * DECHDL - Double-height + double-width line.
3547 * @param topHalf if true, this sets the row to be the top half row of a
3550 private void dechdl(final boolean topHalf
) {
3551 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3552 if (topHalf
== true) {
3553 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3555 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3560 * DECALN - Screen alignment display.
3562 private void decaln() {
3563 Cell newCell
= new Cell('E');
3564 for (DisplayLine line
: display
) {
3565 for (int i
= 0; i
< line
.length(); i
++) {
3566 line
.replace(i
, newCell
);
3572 * DECSCL - Compatibility level.
3574 private void decscl() {
3575 int i
= getCsiParam(0, 0);
3576 int j
= getCsiParam(1, 0);
3580 currentState
.g0Charset
= CharacterSet
.US
;
3581 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3583 } else if (i
== 62) {
3585 if ((j
== 0) || (j
== 2)) {
3587 } else if (j
== 1) {
3594 * CUD - Cursor down.
3596 private void cud() {
3597 cursorDown(getCsiParam(0, 1, 1, height
), true);
3601 * CUF - Cursor forward.
3603 private void cuf() {
3604 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3608 * CUB - Cursor backward.
3610 private void cub() {
3611 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3617 private void cuu() {
3618 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3622 * CUP - Cursor position.
3624 private void cup() {
3625 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3626 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3630 * CNL - Cursor down and to column 1.
3632 private void cnl() {
3633 cursorDown(getCsiParam(0, 1, 1, height
), true);
3635 cursorLeft(currentState
.cursorX
, true);
3639 * CPL - Cursor up and to column 1.
3641 private void cpl() {
3642 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3644 cursorLeft(currentState
.cursorX
, true);
3648 * CHA - Cursor to column # in current row.
3650 private void cha() {
3651 cursorPosition(currentState
.cursorY
,
3652 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3656 * VPA - Cursor to row #, same column.
3658 private void vpa() {
3659 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3660 currentState
.cursorX
);
3664 * ED - Erase in display.
3667 boolean honorProtected
= false;
3668 boolean decPrivateModeFlag
= false;
3670 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3671 if (collectBuffer
.charAt(i
) == '?') {
3672 decPrivateModeFlag
= true;
3677 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3678 && (decPrivateModeFlag
== true)
3680 honorProtected
= true;
3683 int i
= getCsiParam(0, 0);
3686 // Erase from here to end of screen
3687 if (currentState
.cursorY
< height
- 1) {
3688 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3691 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3692 } else if (i
== 1) {
3693 // Erase from beginning of screen to here
3694 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3696 eraseLine(0, currentState
.cursorX
, honorProtected
);
3697 } else if (i
== 2) {
3698 // Erase entire screen
3699 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3704 * EL - Erase in line.
3707 boolean honorProtected
= false;
3708 boolean decPrivateModeFlag
= false;
3710 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3711 if (collectBuffer
.charAt(i
) == '?') {
3712 decPrivateModeFlag
= true;
3717 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3718 && (decPrivateModeFlag
== true)
3720 honorProtected
= true;
3723 int i
= getCsiParam(0, 0);
3726 // Erase from here to end of line
3727 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3728 } else if (i
== 1) {
3729 // Erase from beginning of line to here
3730 eraseLine(0, currentState
.cursorX
, honorProtected
);
3731 } else if (i
== 2) {
3732 // Erase entire line
3733 eraseLine(0, width
- 1, honorProtected
);
3738 * ECH - Erase # of characters in current row.
3740 private void ech() {
3741 int i
= getCsiParam(0, 1, 1, width
);
3743 // Erase from here to i characters
3744 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3751 int i
= getCsiParam(0, 1);
3753 if ((currentState
.cursorY
>= scrollRegionTop
)
3754 && (currentState
.cursorY
<= scrollRegionBottom
)
3757 // I can get the same effect with a scroll-down
3758 scrollingRegionScrollDown(currentState
.cursorY
,
3759 scrollRegionBottom
, i
);
3764 * DCH - Delete char.
3766 private void dch() {
3767 int n
= getCsiParam(0, 1);
3768 DisplayLine line
= display
.get(currentState
.cursorY
);
3769 Cell blank
= new Cell();
3770 for (int i
= 0; i
< n
; i
++) {
3771 line
.delete(currentState
.cursorX
, blank
);
3776 * ICH - Insert blank char at cursor.
3778 private void ich() {
3779 int n
= getCsiParam(0, 1);
3780 DisplayLine line
= display
.get(currentState
.cursorY
);
3781 Cell blank
= new Cell();
3782 for (int i
= 0; i
< n
; i
++) {
3783 line
.insert(currentState
.cursorX
, blank
);
3791 int i
= getCsiParam(0, 1);
3793 if ((currentState
.cursorY
>= scrollRegionTop
)
3794 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3796 // I can get the same effect with a scroll-down
3797 scrollingRegionScrollUp(currentState
.cursorY
,
3798 scrollRegionBottom
, i
);
3803 * HVP - Horizontal and vertical position.
3805 private void hvp() {
3810 * REP - Repeat character.
3812 private void rep() {
3813 int n
= getCsiParam(0, 1);
3814 for (int i
= 0; i
< n
; i
++) {
3815 printCharacter(repCh
);
3823 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3824 getCsiParam(0, 1, 1, height
));
3831 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3832 getCsiParam(0, 1, 1, height
));
3836 * CBT - Go back X tab stops.
3838 private void cbt() {
3839 int tabsToMove
= getCsiParam(0, 1);
3842 for (int i
= 0; i
< tabsToMove
; i
++) {
3843 int j
= currentState
.cursorX
;
3844 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3845 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3853 j
= tabStops
.get(tabI
);
3855 cursorPosition(currentState
.cursorY
, j
);
3860 * CHT - Advance X tab stops.
3862 private void cht() {
3863 int n
= getCsiParam(0, 1);
3864 for (int i
= 0; i
< n
; i
++) {
3865 advanceToNextTabStop();
3870 * SGR - Select graphics rendition.
3872 private void sgr() {
3874 if (csiParams
.size() == 0) {
3875 currentState
.attr
.reset();
3879 int sgrColorMode
= -1;
3880 boolean idx88Color
= false;
3881 boolean rgbColor
= false;
3885 for (Integer i
: csiParams
) {
3887 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
3889 assert (type
== DeviceType
.XTERM
);
3893 * Indexed color mode, we now have the index number.
3895 if (sgrColorMode
== 38) {
3896 currentState
.attr
.setForeColorRGB(get88Color(i
));
3898 assert (sgrColorMode
== 48);
3899 currentState
.attr
.setBackColorRGB(get88Color(i
));
3908 * RGB color mode, we are collecting tokens.
3912 } else if (rgbGreen
== -1) {
3913 rgbGreen
= i
& 0xFF;
3915 int rgb
= rgbRed
<< 16;
3916 rgb
|= rgbGreen
<< 8;
3919 // System.err.printf("RGB: %08x\n", rgb);
3921 if (sgrColorMode
== 38) {
3922 currentState
.attr
.setForeColorRGB(rgb
);
3924 assert (sgrColorMode
== 48);
3925 currentState
.attr
.setBackColorRGB(rgb
);
3946 * Indexed color mode.
3953 * This is neither indexed nor RGB color. Bail out.
3958 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
3964 currentState
.attr
.reset();
3969 currentState
.attr
.setBold(true);
3974 currentState
.attr
.setUnderline(true);
3979 currentState
.attr
.setBlink(true);
3984 currentState
.attr
.setReverse(true);
3991 if (type
== DeviceType
.XTERM
) {
4001 // Set black foreground
4002 currentState
.attr
.setForeColorRGB(get88Color(8));
4005 // Set red foreground
4006 currentState
.attr
.setForeColorRGB(get88Color(9));
4009 // Set green foreground
4010 currentState
.attr
.setForeColorRGB(get88Color(10));
4013 // Set yellow foreground
4014 currentState
.attr
.setForeColorRGB(get88Color(11));
4017 // Set blue foreground
4018 currentState
.attr
.setForeColorRGB(get88Color(12));
4021 // Set magenta foreground
4022 currentState
.attr
.setForeColorRGB(get88Color(13));
4025 // Set cyan foreground
4026 currentState
.attr
.setForeColorRGB(get88Color(14));
4029 // Set white foreground
4030 currentState
.attr
.setForeColorRGB(get88Color(15));
4034 // Set black background
4035 currentState
.attr
.setBackColorRGB(get88Color(8));
4038 // Set red background
4039 currentState
.attr
.setBackColorRGB(get88Color(9));
4042 // Set green background
4043 currentState
.attr
.setBackColorRGB(get88Color(10));
4046 // Set yellow background
4047 currentState
.attr
.setBackColorRGB(get88Color(11));
4050 // Set blue background
4051 currentState
.attr
.setBackColorRGB(get88Color(12));
4054 // Set magenta background
4055 currentState
.attr
.setBackColorRGB(get88Color(13));
4058 // Set cyan background
4059 currentState
.attr
.setBackColorRGB(get88Color(14));
4062 // Set white background
4063 currentState
.attr
.setBackColorRGB(get88Color(15));
4071 if ((type
== DeviceType
.VT220
)
4072 || (type
== DeviceType
.XTERM
)) {
4078 currentState
.attr
.setBold(false);
4083 currentState
.attr
.setUnderline(false);
4088 currentState
.attr
.setBlink(false);
4093 currentState
.attr
.setReverse(false);
4101 // A true VT100/102/220 does not support color, however everyone
4102 // is used to their terminal emulator supporting color so we will
4103 // unconditionally support color for all DeviceType's.
4108 // Set black foreground
4109 currentState
.attr
.setForeColor(Color
.BLACK
);
4110 currentState
.attr
.setForeColorRGB(-1);
4113 // Set red foreground
4114 currentState
.attr
.setForeColor(Color
.RED
);
4115 currentState
.attr
.setForeColorRGB(-1);
4118 // Set green foreground
4119 currentState
.attr
.setForeColor(Color
.GREEN
);
4120 currentState
.attr
.setForeColorRGB(-1);
4123 // Set yellow foreground
4124 currentState
.attr
.setForeColor(Color
.YELLOW
);
4125 currentState
.attr
.setForeColorRGB(-1);
4128 // Set blue foreground
4129 currentState
.attr
.setForeColor(Color
.BLUE
);
4130 currentState
.attr
.setForeColorRGB(-1);
4133 // Set magenta foreground
4134 currentState
.attr
.setForeColor(Color
.MAGENTA
);
4135 currentState
.attr
.setForeColorRGB(-1);
4138 // Set cyan foreground
4139 currentState
.attr
.setForeColor(Color
.CYAN
);
4140 currentState
.attr
.setForeColorRGB(-1);
4143 // Set white foreground
4144 currentState
.attr
.setForeColor(Color
.WHITE
);
4145 currentState
.attr
.setForeColorRGB(-1);
4148 if (type
== DeviceType
.XTERM
) {
4150 * Xterm supports T.416 / ISO-8613-3 codes to select
4151 * either an indexed color or an RGB value. (It also
4152 * permits these ISO-8613-3 SGR sequences to be separated
4153 * by colons rather than semicolons.)
4155 * We will support only the following:
4157 * 1. Indexed color mode (88- or 256-color modes).
4161 * These cover most of the use cases in the real world.
4163 * HOWEVER, note that this is an awful broken "standard",
4164 * with no way to do it "right". See
4165 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4166 * for a detailed discussion of the current state of RGB
4167 * in various terminals, the point of which is that none
4168 * of them really do the same thing despite all appearing
4172 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4173 * where it is assumed that supporting just the "indexed
4174 * mode" of these sequences (which could align easily
4175 * with existing SGR colors) is assumed to mean full
4176 * support of 24-bit RGB. So it is all or nothing.
4178 * Finally, these sequences break the assumptions of
4179 * standard ECMA-48 style parsers as pointed out at
4180 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4181 * Therefore in order to keep a clean display, we cannot
4182 * parse anything else in this sequence.
4187 // Underscore on, default foreground color
4188 currentState
.attr
.setUnderline(true);
4189 currentState
.attr
.setForeColor(Color
.WHITE
);
4193 // Underscore off, default foreground color
4194 currentState
.attr
.setUnderline(false);
4195 currentState
.attr
.setForeColor(Color
.WHITE
);
4196 currentState
.attr
.setForeColorRGB(-1);
4199 // Set black background
4200 currentState
.attr
.setBackColor(Color
.BLACK
);
4201 currentState
.attr
.setBackColorRGB(-1);
4204 // Set red background
4205 currentState
.attr
.setBackColor(Color
.RED
);
4206 currentState
.attr
.setBackColorRGB(-1);
4209 // Set green background
4210 currentState
.attr
.setBackColor(Color
.GREEN
);
4211 currentState
.attr
.setBackColorRGB(-1);
4214 // Set yellow background
4215 currentState
.attr
.setBackColor(Color
.YELLOW
);
4216 currentState
.attr
.setBackColorRGB(-1);
4219 // Set blue background
4220 currentState
.attr
.setBackColor(Color
.BLUE
);
4221 currentState
.attr
.setBackColorRGB(-1);
4224 // Set magenta background
4225 currentState
.attr
.setBackColor(Color
.MAGENTA
);
4226 currentState
.attr
.setBackColorRGB(-1);
4229 // Set cyan background
4230 currentState
.attr
.setBackColor(Color
.CYAN
);
4231 currentState
.attr
.setBackColorRGB(-1);
4234 // Set white background
4235 currentState
.attr
.setBackColor(Color
.WHITE
);
4236 currentState
.attr
.setBackColorRGB(-1);
4239 if (type
== DeviceType
.XTERM
) {
4241 * Xterm supports T.416 / ISO-8613-3 codes to select
4242 * either an indexed color or an RGB value. (It also
4243 * permits these ISO-8613-3 SGR sequences to be separated
4244 * by colons rather than semicolons.)
4246 * We will support only the following:
4248 * 1. Indexed color mode (88- or 256-color modes).
4252 * These cover most of the use cases in the real world.
4254 * HOWEVER, note that this is an awful broken "standard",
4255 * with no way to do it "right". See
4256 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4257 * for a detailed discussion of the current state of RGB
4258 * in various terminals, the point of which is that none
4259 * of them really do the same thing despite all appearing
4263 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4264 * where it is assumed that supporting just the "indexed
4265 * mode" of these sequences (which could align easily
4266 * with existing SGR colors) is assumed to mean full
4267 * support of 24-bit RGB. So it is all or nothing.
4269 * Finally, these sequences break the assumptions of
4270 * standard ECMA-48 style parsers as pointed out at
4271 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4272 * Therefore in order to keep a clean display, we cannot
4273 * parse anything else in this sequence.
4280 // Default background
4281 currentState
.attr
.setBackColor(Color
.BLACK
);
4282 currentState
.attr
.setBackColorRGB(-1);
4292 * DA - Device attributes.
4295 int extendedFlag
= 0;
4297 if (collectBuffer
.length() > 0) {
4298 String args
= collectBuffer
.substring(1);
4299 if (collectBuffer
.charAt(0) == '>') {
4301 if (collectBuffer
.length() >= 2) {
4302 i
= Integer
.parseInt(args
);
4304 } else if (collectBuffer
.charAt(0) == '=') {
4306 if (collectBuffer
.length() >= 2) {
4307 i
= Integer
.parseInt(args
);
4310 // Unknown code, bail out
4315 if ((i
!= 0) && (i
!= 1)) {
4319 if ((extendedFlag
== 0) && (i
== 0)) {
4320 // Send string directly to remote side
4321 writeRemote(deviceTypeResponse());
4325 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4327 if ((extendedFlag
== 1) && (i
== 0)) {
4329 * Request "What type of terminal are you, what is your
4330 * firmware version, and what hardware options do you have
4333 * Respond: "I am a VT220 (identification code of 1), my
4334 * firmware version is _____ (Pv), and I have _____ Po
4335 * options installed."
4341 if (s8c1t
== true) {
4342 writeRemote("\u009b>1;10;0c");
4344 writeRemote("\033[>1;10;0c");
4350 if ((extendedFlag
== 2) && (i
== 0)) {
4353 * Request "What is your unit ID?"
4355 * Respond: "I was manufactured at site 00 and have a unique ID
4359 writeRemote("\033P!|00010203\033\\");
4364 * DECSTBM - Set top and bottom margins.
4366 private void decstbm() {
4367 boolean decPrivateModeFlag
= false;
4369 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4370 if (collectBuffer
.charAt(i
) == '?') {
4371 decPrivateModeFlag
= true;
4375 if (decPrivateModeFlag
) {
4376 // This could be restore DEC private mode values.
4380 int top
= getCsiParam(0, 1, 1, height
) - 1;
4381 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4386 scrollRegionTop
= top
;
4387 scrollRegionBottom
= bottom
;
4390 cursorPosition(0, 0);
4395 * DECREQTPARM - Request terminal parameters.
4397 private void decreqtparm() {
4398 int i
= getCsiParam(0, 0);
4400 if ((i
!= 0) && (i
!= 1)) {
4407 * Request terminal parameters.
4411 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4412 * (CLoCk MULtiplier = 1, STP option flags = 0)
4416 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4419 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4421 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4427 * DECSCA - Select Character Attributes.
4429 private void decsca() {
4430 int i
= getCsiParam(0, 0);
4432 if ((i
== 0) || (i
== 2)) {
4434 currentState
.attr
.setProtect(false);
4438 currentState
.attr
.setProtect(true);
4443 * DECSTR - Soft Terminal Reset.
4445 private void decstr() {
4446 // Do exactly like RIS - Reset to initial state
4448 // Do I clear screen too? I think so...
4449 eraseScreen(0, 0, height
- 1, width
- 1, false);
4450 cursorPosition(0, 0);
4454 * DSR - Device status report.
4456 private void dsr() {
4457 boolean decPrivateModeFlag
= false;
4458 int row
= currentState
.cursorY
;
4460 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4461 if (collectBuffer
.charAt(i
) == '?') {
4462 decPrivateModeFlag
= true;
4467 int i
= getCsiParam(0, 0);
4472 // Request status report. Respond with "OK, no malfunction."
4474 // Send string directly to remote side
4475 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4478 writeRemote("\u009b0n");
4480 writeRemote("\033[0n");
4485 // Request cursor position. Respond with current position.
4486 if (currentState
.originMode
== true) {
4487 row
-= scrollRegionTop
;
4490 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4493 str
= String
.format("\u009b%d;%dR", row
+ 1,
4494 currentState
.cursorX
+ 1);
4496 str
= String
.format("\033[%d;%dR", row
+ 1,
4497 currentState
.cursorX
+ 1);
4500 // Send string directly to remote side
4505 if (decPrivateModeFlag
== true) {
4507 // Request printer status report. Respond with "Printer not
4510 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4511 && (s8c1t
== true)) {
4512 writeRemote("\u009b?13n");
4514 writeRemote("\033[?13n");
4520 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4521 && (decPrivateModeFlag
== true)
4524 // Request user-defined keys are locked or unlocked. Respond
4525 // with "User-defined keys are locked."
4527 if (s8c1t
== true) {
4528 writeRemote("\u009b?21n");
4530 writeRemote("\033[?21n");
4536 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4537 && (decPrivateModeFlag
== true)
4540 // Request keyboard language. Respond with "Keyboard
4541 // language is North American."
4543 if (s8c1t
== true) {
4544 writeRemote("\u009b?27;1n");
4546 writeRemote("\033[?27;1n");
4553 // Some other option, ignore
4559 * TBC - Tabulation clear.
4561 private void tbc() {
4562 int i
= getCsiParam(0, 0);
4564 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4565 for (Integer stop
: tabStops
) {
4566 if (stop
== currentState
.cursorX
) {
4571 tabStops
= newStops
;
4579 * Erase the characters in the current line from the start column to the
4580 * end column, inclusive.
4582 * @param start starting column to erase (between 0 and width - 1)
4583 * @param end ending column to erase (between 0 and width - 1)
4584 * @param honorProtected if true, do not erase characters with the
4585 * protected attribute set
4587 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4592 if (end
> width
- 1) {
4599 for (int i
= start
; i
<= end
; i
++) {
4600 DisplayLine line
= display
.get(currentState
.cursorY
);
4601 if ((!honorProtected
)
4602 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4609 * From the VT102 manual:
4611 * Erasing a character also erases any character
4612 * attribute of the character.
4618 * Erase with the current color a.k.a. back-color erase
4621 line
.setChar(i
, ' ');
4622 line
.setAttr(i
, currentState
.attr
);
4630 * Erase a rectangular section of the screen, inclusive. end column,
4633 * @param startRow starting row to erase (between 0 and height - 1)
4634 * @param startCol starting column to erase (between 0 and width - 1)
4635 * @param endRow ending row to erase (between 0 and height - 1)
4636 * @param endCol ending column to erase (between 0 and width - 1)
4637 * @param honorProtected if true, do not erase characters with the
4638 * protected attribute set
4640 private void eraseScreen(final int startRow
, final int startCol
,
4641 final int endRow
, final int endCol
, final boolean honorProtected
) {
4649 || (endRow
< startRow
)
4650 || (endCol
< startCol
)
4655 oldCursorY
= currentState
.cursorY
;
4656 for (int i
= startRow
; i
<= endRow
; i
++) {
4657 currentState
.cursorY
= i
;
4658 eraseLine(startCol
, endCol
, honorProtected
);
4660 // Erase display clears the double attributes
4661 display
.get(i
).setDoubleWidth(false);
4662 display
.get(i
).setDoubleHeight(0);
4664 currentState
.cursorY
= oldCursorY
;
4668 * VT220 printer functions. All of these are parsed, but won't do
4671 private void printerFunctions() {
4672 boolean decPrivateModeFlag
= false;
4673 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4674 if (collectBuffer
.charAt(i
) == '?') {
4675 decPrivateModeFlag
= true;
4680 int i
= getCsiParam(0, 0);
4685 if (decPrivateModeFlag
== false) {
4691 if (decPrivateModeFlag
== true) {
4692 // Print cursor line
4697 if (decPrivateModeFlag
== true) {
4698 // Auto print mode OFF
4700 // Printer controller OFF
4702 // Characters re-appear on the screen
4703 printerControllerMode
= false;
4708 if (decPrivateModeFlag
== true) {
4712 // Printer controller
4714 // Characters get sucked into oblivion
4715 printerControllerMode
= true;
4726 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4727 * of remote systems will send an XTerm title sequence even if TERM isn't
4730 * @param xtermChar the character received from the remote side
4732 private void oscPut(final char xtermChar
) {
4733 // System.err.println("oscPut: " + xtermChar);
4736 collectBuffer
.append(xtermChar
);
4739 if ((xtermChar
== 0x07)
4740 || (collectBuffer
.toString().endsWith("\033\\"))
4743 if (xtermChar
== 0x07) {
4744 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
4746 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4749 String
[] p
= args
.split(";");
4751 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4758 if (p
[0].equals("4")) {
4759 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
4760 // Set a color index value
4762 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
4763 } catch (NumberFormatException e
) {
4769 if (p
[0].equals("10")) {
4770 if (p
[1].equals("?")) {
4771 // Respond with foreground color.
4772 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToForegroundColor(currentState
.attr
);
4774 writeRemote(String
.format(
4775 "\033]10;rgb:%04x/%04x/%04x\033\\",
4776 color
.getRed() << 8,
4777 color
.getGreen() << 8,
4778 color
.getBlue() << 8));
4782 if (p
[0].equals("11")) {
4783 if (p
[1].equals("?")) {
4784 // Respond with background color.
4785 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToBackgroundColor(currentState
.attr
);
4787 writeRemote(String
.format(
4788 "\033]11;rgb:%04x/%04x/%04x\033\\",
4789 color
.getRed() << 8,
4790 color
.getGreen() << 8,
4791 color
.getBlue() << 8));
4795 if (p
[0].equals("444") && (p
.length
== 5)) {
4797 parseJexerImage(p
[1], p
[2], p
[3], p
[4]);
4802 // Go to SCAN_GROUND state
4809 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
4810 * Jexer ECMA48Terminal to talk to ECMA48.
4812 * @param pmChar the character received from the remote side
4814 private void pmPut(final char pmChar
) {
4815 // System.err.println("pmPut: " + pmChar);
4818 collectBuffer
.append(pmChar
);
4821 if (collectBuffer
.toString().endsWith("\033\\")) {
4823 arg
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4825 // System.err.println("arg: '" + arg + "'");
4827 if (arg
.equals("hideMousePointer")) {
4828 hideMousePointer
= true;
4830 if (arg
.equals("showMousePointer")) {
4831 hideMousePointer
= false;
4834 // Go to SCAN_GROUND state
4841 * Perform xterm window operations.
4843 private void xtermWindowOps() {
4844 boolean xtermPrivateModeFlag
= false;
4846 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4847 if (collectBuffer
.charAt(i
) == '?') {
4848 xtermPrivateModeFlag
= true;
4853 int i
= getCsiParam(0, 0);
4855 if (!xtermPrivateModeFlag
) {
4858 // Report xterm text area size in pixels as CSI 4 ; height ;
4860 writeRemote(String
.format("\033[4;%d;%dt", textHeight
* height
,
4861 textWidth
* width
));
4864 // Report character size in pixels as CSI 6 ; height ; width
4866 writeRemote(String
.format("\033[6;%d;%dt", textHeight
,
4870 // Report the text are size in characters as CSI 8 ; height ;
4872 writeRemote(String
.format("\033[8;%d;%dt", height
, width
));
4881 * Respond to xterm sixel query.
4883 private void xtermSixelQuery() {
4884 int item
= getCsiParam(0, 0);
4885 int action
= getCsiParam(1, 0);
4886 int value
= getCsiParam(2, 0);
4891 // Report number of color registers.
4892 writeRemote(String
.format("\033[?%d;%d;%dS", item
, 0, 1024));
4899 // We will not support this option.
4900 writeRemote(String
.format("\033[?%d;%dS", item
, action
));
4904 * Run this input character through the ECMA48 state machine.
4906 * @param ch character from the remote side
4908 private void consume(int ch
) {
4911 // System.err.printf("%c STATE = %s\n", ch, scanState);
4913 // Special case for VT10x: 7-bit characters only
4914 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4918 // Special "anywhere" states
4920 // 18, 1A --> execute, then switch to SCAN_GROUND
4921 if ((ch
== 0x18) || (ch
== 0x1A)) {
4922 // CAN and SUB abort escape sequences
4927 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4931 if ((type
== DeviceType
.XTERM
)
4932 && ((scanState
== ScanState
.OSC_STRING
)
4933 || (scanState
== ScanState
.DCS_SIXEL
)
4934 || (scanState
== ScanState
.SOSPMAPC_STRING
))
4936 // Xterm can pass ESCAPE to its OSC sequence.
4937 // Xterm can pass ESCAPE to its DCS sequence.
4938 // Jexer can pass ESCAPE to its PM sequence.
4939 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
4940 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4941 && (scanState
!= ScanState
.DCS_IGNORE
)
4942 && (scanState
!= ScanState
.DCS_PARAM
)
4943 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4945 scanState
= ScanState
.ESCAPE
;
4950 // 0x9B == CSI 8-bit sequence
4952 scanState
= ScanState
.CSI_ENTRY
;
4956 // 0x9D goes to ScanState.OSC_STRING
4958 scanState
= ScanState
.OSC_STRING
;
4962 // 0x90 goes to DCS_ENTRY
4964 scanState
= ScanState
.DCS_ENTRY
;
4968 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4969 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4970 scanState
= ScanState
.SOSPMAPC_STRING
;
4974 // 0x7F (DEL) is always discarded
4979 switch (scanState
) {
4982 // 00-17, 19, 1C-1F --> execute
4983 // 80-8F, 91-9A, 9C --> execute
4984 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4985 handleControlChar((char) ch
);
4989 if (((ch
>= 0x20) && (ch
<= 0x7F))
4993 // VT220 printer --> trash bin
4994 if (((type
== DeviceType
.VT220
)
4995 || (type
== DeviceType
.XTERM
))
4996 && (printerControllerMode
== true)
5001 // Hang onto this character
5002 repCh
= mapCharacter(ch
);
5004 // Print this character
5005 printCharacter(repCh
);
5010 // 00-17, 19, 1C-1F --> execute
5012 handleControlChar((char) ch
);
5016 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
5017 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5019 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
5023 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
5024 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
5035 // DECSC - Save cursor
5036 // Note this code overlaps both ANSI and VT52 mode
5041 // DECRC - Restore cursor
5042 // Note this code overlaps both ANSI and VT52 mode
5051 if (vt52Mode
== true) {
5052 // DECANM - Enter ANSI mode
5054 arrowKeyMode
= ArrowKeyMode
.VT100
;
5057 * From the VT102 docs: "You use ANSI mode to select
5058 * most terminal features; the terminal uses the same
5059 * features when it switches to VT52 mode. You
5060 * cannot, however, change most of these features in
5063 * In other words, do not reset any other attributes
5064 * when switching between VT52 submode and ANSI.
5068 currentState
.g0Charset
= CharacterSet
.US
;
5069 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5071 singleshift
= Singleshift
.NONE
;
5072 currentState
.glLockshift
= LockshiftMode
.NONE
;
5073 currentState
.grLockshift
= LockshiftMode
.NONE
;
5077 // DECKPAM - Keypad application mode
5078 // Note this code overlaps both ANSI and VT52 mode
5082 // DECKPNM - Keypad numeric mode
5083 // Note this code overlaps both ANSI and VT52 mode
5090 if (vt52Mode
== true) {
5091 // Cursor up, and stop at the top without scrolling
5096 if (vt52Mode
== true) {
5097 // Cursor down, and stop at the bottom without scrolling
5098 cursorDown(1, false);
5102 if (vt52Mode
== true) {
5103 // Cursor right, and stop at the right without scrolling
5104 cursorRight(1, false);
5108 if (vt52Mode
== true) {
5109 // Cursor left, and stop at the left without scrolling
5110 cursorLeft(1, false);
5117 if (vt52Mode
== true) {
5125 if (vt52Mode
== true) {
5126 // G0 --> Special graphics
5127 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
5131 if (vt52Mode
== true) {
5133 currentState
.g0Charset
= CharacterSet
.US
;
5137 if (vt52Mode
== true) {
5139 cursorPosition(0, 0);
5141 // HTS - Horizontal tabulation set
5146 if (vt52Mode
== true) {
5147 // Reverse line feed. Same as RI.
5152 if (vt52Mode
== true) {
5153 // Erase to end of screen
5154 eraseLine(currentState
.cursorX
, width
- 1, false);
5155 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
5160 if (vt52Mode
== true) {
5161 // Erase to end of line
5162 eraseLine(currentState
.cursorX
, width
- 1, false);
5168 if (vt52Mode
== true) {
5171 // RI - Reverse index
5176 if (vt52Mode
== false) {
5178 singleshift
= Singleshift
.SS2
;
5182 if (vt52Mode
== false) {
5184 singleshift
= Singleshift
.SS3
;
5191 if ((ch
>= 0x51) && (ch
<= 0x57)) {
5207 if (vt52Mode
== true) {
5208 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
5216 if (vt52Mode
== true) {
5218 // Send string directly to remote side
5219 writeRemote("\033/Z");
5222 // Send string directly to remote side
5223 writeRemote(deviceTypeResponse());
5234 // VT52 cannot get to any of these other states
5235 if (vt52Mode
== true) {
5240 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
5247 // RIS - Reset to initial state
5249 // Do I clear screen too? I think so...
5250 eraseScreen(0, 0, height
- 1, width
- 1, false);
5251 cursorPosition(0, 0);
5265 if ((type
== DeviceType
.VT220
)
5266 || (type
== DeviceType
.XTERM
)) {
5268 // VT220 lockshift G2 into GL
5269 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
5274 if ((type
== DeviceType
.VT220
)
5275 || (type
== DeviceType
.XTERM
)) {
5277 // VT220 lockshift G3 into GL
5278 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
5296 if ((type
== DeviceType
.VT220
)
5297 || (type
== DeviceType
.XTERM
)) {
5299 // VT220 lockshift G3 into GR
5300 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
5305 if ((type
== DeviceType
.VT220
)
5306 || (type
== DeviceType
.XTERM
)) {
5308 // VT220 lockshift G2 into GR
5309 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
5315 if ((type
== DeviceType
.VT220
)
5316 || (type
== DeviceType
.XTERM
)) {
5318 // VT220 lockshift G1 into GR
5319 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
5329 // 0x5B goes to CSI_ENTRY
5331 scanState
= ScanState
.CSI_ENTRY
;
5334 // 0x5D goes to OSC_STRING
5336 scanState
= ScanState
.OSC_STRING
;
5339 // 0x50 goes to DCS_ENTRY
5341 scanState
= ScanState
.DCS_ENTRY
;
5344 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5345 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
5346 scanState
= ScanState
.SOSPMAPC_STRING
;
5351 case ESCAPE_INTERMEDIATE
:
5352 // 00-17, 19, 1C-1F --> execute
5354 handleControlChar((char) ch
);
5357 // 20-2F --> collect
5358 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5362 // 30-7E --> dispatch, then switch to GROUND
5363 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
5366 if ((collectBuffer
.length() == 1)
5367 && (collectBuffer
.charAt(0) == '(')) {
5368 // G0 --> Special graphics
5369 currentState
.g0Charset
= CharacterSet
.DRAWING
;
5371 if ((collectBuffer
.length() == 1)
5372 && (collectBuffer
.charAt(0) == ')')) {
5373 // G1 --> Special graphics
5374 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5376 if ((type
== DeviceType
.VT220
)
5377 || (type
== DeviceType
.XTERM
)) {
5379 if ((collectBuffer
.length() == 1)
5380 && (collectBuffer
.charAt(0) == '*')) {
5381 // G2 --> Special graphics
5382 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5384 if ((collectBuffer
.length() == 1)
5385 && (collectBuffer
.charAt(0) == '+')) {
5386 // G3 --> Special graphics
5387 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5392 if ((collectBuffer
.length() == 1)
5393 && (collectBuffer
.charAt(0) == '(')) {
5394 // G0 --> Alternate character ROM standard character set
5395 currentState
.g0Charset
= CharacterSet
.ROM
;
5397 if ((collectBuffer
.length() == 1)
5398 && (collectBuffer
.charAt(0) == ')')) {
5399 // G1 --> Alternate character ROM standard character set
5400 currentState
.g1Charset
= CharacterSet
.ROM
;
5404 if ((collectBuffer
.length() == 1)
5405 && (collectBuffer
.charAt(0) == '(')) {
5406 // G0 --> Alternate character ROM special graphics
5407 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5409 if ((collectBuffer
.length() == 1)
5410 && (collectBuffer
.charAt(0) == ')')) {
5411 // G1 --> Alternate character ROM special graphics
5412 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5416 if ((collectBuffer
.length() == 1)
5417 && (collectBuffer
.charAt(0) == '#')) {
5418 // DECDHL - Double-height line (top half)
5423 if ((collectBuffer
.length() == 1)
5424 && (collectBuffer
.charAt(0) == '#')) {
5425 // DECDHL - Double-height line (bottom half)
5428 if ((type
== DeviceType
.VT220
)
5429 || (type
== DeviceType
.XTERM
)) {
5431 if ((collectBuffer
.length() == 1)
5432 && (collectBuffer
.charAt(0) == '(')) {
5434 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5436 if ((collectBuffer
.length() == 1)
5437 && (collectBuffer
.charAt(0) == ')')) {
5439 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5441 if ((collectBuffer
.length() == 1)
5442 && (collectBuffer
.charAt(0) == '*')) {
5444 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5446 if ((collectBuffer
.length() == 1)
5447 && (collectBuffer
.charAt(0) == '+')) {
5449 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5454 if ((collectBuffer
.length() == 1)
5455 && (collectBuffer
.charAt(0) == '#')) {
5456 // DECSWL - Single-width line
5459 if ((type
== DeviceType
.VT220
)
5460 || (type
== DeviceType
.XTERM
)) {
5462 if ((collectBuffer
.length() == 1)
5463 && (collectBuffer
.charAt(0) == '(')) {
5465 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5467 if ((collectBuffer
.length() == 1)
5468 && (collectBuffer
.charAt(0) == ')')) {
5470 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5472 if ((collectBuffer
.length() == 1)
5473 && (collectBuffer
.charAt(0) == '*')) {
5475 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5477 if ((collectBuffer
.length() == 1)
5478 && (collectBuffer
.charAt(0) == '+')) {
5480 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5485 if ((collectBuffer
.length() == 1)
5486 && (collectBuffer
.charAt(0) == '#')) {
5487 // DECDWL - Double-width line
5490 if ((type
== DeviceType
.VT220
)
5491 || (type
== DeviceType
.XTERM
)) {
5493 if ((collectBuffer
.length() == 1)
5494 && (collectBuffer
.charAt(0) == '(')) {
5496 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5498 if ((collectBuffer
.length() == 1)
5499 && (collectBuffer
.charAt(0) == ')')) {
5501 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5503 if ((collectBuffer
.length() == 1)
5504 && (collectBuffer
.charAt(0) == '*')) {
5506 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5508 if ((collectBuffer
.length() == 1)
5509 && (collectBuffer
.charAt(0) == '+')) {
5511 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5516 if ((type
== DeviceType
.VT220
)
5517 || (type
== DeviceType
.XTERM
)) {
5519 if ((collectBuffer
.length() == 1)
5520 && (collectBuffer
.charAt(0) == '(')) {
5522 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5524 if ((collectBuffer
.length() == 1)
5525 && (collectBuffer
.charAt(0) == ')')) {
5527 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5529 if ((collectBuffer
.length() == 1)
5530 && (collectBuffer
.charAt(0) == '*')) {
5532 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5534 if ((collectBuffer
.length() == 1)
5535 && (collectBuffer
.charAt(0) == '+')) {
5537 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5542 if ((collectBuffer
.length() == 1)
5543 && (collectBuffer
.charAt(0) == '#')) {
5544 // DECALN - Screen alignment display
5553 if ((type
== DeviceType
.VT220
)
5554 || (type
== DeviceType
.XTERM
)) {
5556 if ((collectBuffer
.length() == 1)
5557 && (collectBuffer
.charAt(0) == '(')) {
5558 // G0 --> DEC_SUPPLEMENTAL
5559 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5561 if ((collectBuffer
.length() == 1)
5562 && (collectBuffer
.charAt(0) == ')')) {
5563 // G1 --> DEC_SUPPLEMENTAL
5564 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5566 if ((collectBuffer
.length() == 1)
5567 && (collectBuffer
.charAt(0) == '*')) {
5568 // G2 --> DEC_SUPPLEMENTAL
5569 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5571 if ((collectBuffer
.length() == 1)
5572 && (collectBuffer
.charAt(0) == '+')) {
5573 // G3 --> DEC_SUPPLEMENTAL
5574 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5579 if ((type
== DeviceType
.VT220
)
5580 || (type
== DeviceType
.XTERM
)) {
5582 if ((collectBuffer
.length() == 1)
5583 && (collectBuffer
.charAt(0) == '(')) {
5585 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5587 if ((collectBuffer
.length() == 1)
5588 && (collectBuffer
.charAt(0) == ')')) {
5590 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5592 if ((collectBuffer
.length() == 1)
5593 && (collectBuffer
.charAt(0) == '*')) {
5595 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5597 if ((collectBuffer
.length() == 1)
5598 && (collectBuffer
.charAt(0) == '+')) {
5600 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5609 if ((collectBuffer
.length() == 1)
5610 && (collectBuffer
.charAt(0) == '(')) {
5611 // G0 --> United Kingdom set
5612 currentState
.g0Charset
= CharacterSet
.UK
;
5614 if ((collectBuffer
.length() == 1)
5615 && (collectBuffer
.charAt(0) == ')')) {
5616 // G1 --> United Kingdom set
5617 currentState
.g1Charset
= CharacterSet
.UK
;
5619 if ((type
== DeviceType
.VT220
)
5620 || (type
== DeviceType
.XTERM
)) {
5622 if ((collectBuffer
.length() == 1)
5623 && (collectBuffer
.charAt(0) == '*')) {
5624 // G2 --> United Kingdom set
5625 currentState
.g2Charset
= CharacterSet
.UK
;
5627 if ((collectBuffer
.length() == 1)
5628 && (collectBuffer
.charAt(0) == '+')) {
5629 // G3 --> United Kingdom set
5630 currentState
.g3Charset
= CharacterSet
.UK
;
5635 if ((collectBuffer
.length() == 1)
5636 && (collectBuffer
.charAt(0) == '(')) {
5638 currentState
.g0Charset
= CharacterSet
.US
;
5640 if ((collectBuffer
.length() == 1)
5641 && (collectBuffer
.charAt(0) == ')')) {
5643 currentState
.g1Charset
= CharacterSet
.US
;
5645 if ((type
== DeviceType
.VT220
)
5646 || (type
== DeviceType
.XTERM
)) {
5648 if ((collectBuffer
.length() == 1)
5649 && (collectBuffer
.charAt(0) == '*')) {
5651 currentState
.g2Charset
= CharacterSet
.US
;
5653 if ((collectBuffer
.length() == 1)
5654 && (collectBuffer
.charAt(0) == '+')) {
5656 currentState
.g3Charset
= CharacterSet
.US
;
5661 if ((type
== DeviceType
.VT220
)
5662 || (type
== DeviceType
.XTERM
)) {
5664 if ((collectBuffer
.length() == 1)
5665 && (collectBuffer
.charAt(0) == '(')) {
5667 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5669 if ((collectBuffer
.length() == 1)
5670 && (collectBuffer
.charAt(0) == ')')) {
5672 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5674 if ((collectBuffer
.length() == 1)
5675 && (collectBuffer
.charAt(0) == '*')) {
5677 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5679 if ((collectBuffer
.length() == 1)
5680 && (collectBuffer
.charAt(0) == '+')) {
5682 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5689 if ((type
== DeviceType
.VT220
)
5690 || (type
== DeviceType
.XTERM
)) {
5692 if ((collectBuffer
.length() == 1)
5693 && (collectBuffer
.charAt(0) == '(')) {
5695 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5697 if ((collectBuffer
.length() == 1)
5698 && (collectBuffer
.charAt(0) == ')')) {
5700 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5702 if ((collectBuffer
.length() == 1)
5703 && (collectBuffer
.charAt(0) == '*')) {
5705 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5707 if ((collectBuffer
.length() == 1)
5708 && (collectBuffer
.charAt(0) == '+')) {
5710 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5715 if ((type
== DeviceType
.VT220
)
5716 || (type
== DeviceType
.XTERM
)) {
5718 if ((collectBuffer
.length() == 1)
5719 && (collectBuffer
.charAt(0) == ' ')) {
5726 if ((type
== DeviceType
.VT220
)
5727 || (type
== DeviceType
.XTERM
)) {
5729 if ((collectBuffer
.length() == 1)
5730 && (collectBuffer
.charAt(0) == ' ')) {
5737 if ((type
== DeviceType
.VT220
)
5738 || (type
== DeviceType
.XTERM
)) {
5740 if ((collectBuffer
.length() == 1)
5741 && (collectBuffer
.charAt(0) == '(')) {
5743 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5745 if ((collectBuffer
.length() == 1)
5746 && (collectBuffer
.charAt(0) == ')')) {
5748 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5750 if ((collectBuffer
.length() == 1)
5751 && (collectBuffer
.charAt(0) == '*')) {
5753 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5755 if ((collectBuffer
.length() == 1)
5756 && (collectBuffer
.charAt(0) == '+')) {
5758 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5766 if ((type
== DeviceType
.VT220
)
5767 || (type
== DeviceType
.XTERM
)) {
5769 if ((collectBuffer
.length() == 1)
5770 && (collectBuffer
.charAt(0) == '(')) {
5772 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5774 if ((collectBuffer
.length() == 1)
5775 && (collectBuffer
.charAt(0) == ')')) {
5777 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5779 if ((collectBuffer
.length() == 1)
5780 && (collectBuffer
.charAt(0) == '*')) {
5782 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5784 if ((collectBuffer
.length() == 1)
5785 && (collectBuffer
.charAt(0) == '+')) {
5787 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5798 if ((type
== DeviceType
.VT220
)
5799 || (type
== DeviceType
.XTERM
)) {
5801 if ((collectBuffer
.length() == 1)
5802 && (collectBuffer
.charAt(0) == '(')) {
5804 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5806 if ((collectBuffer
.length() == 1)
5807 && (collectBuffer
.charAt(0) == ')')) {
5809 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5811 if ((collectBuffer
.length() == 1)
5812 && (collectBuffer
.charAt(0) == '*')) {
5814 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5816 if ((collectBuffer
.length() == 1)
5817 && (collectBuffer
.charAt(0) == '+')) {
5819 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5824 if ((type
== DeviceType
.VT220
)
5825 || (type
== DeviceType
.XTERM
)) {
5827 if ((collectBuffer
.length() == 1)
5828 && (collectBuffer
.charAt(0) == '(')) {
5830 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5832 if ((collectBuffer
.length() == 1)
5833 && (collectBuffer
.charAt(0) == ')')) {
5835 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5837 if ((collectBuffer
.length() == 1)
5838 && (collectBuffer
.charAt(0) == '*')) {
5840 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5842 if ((collectBuffer
.length() == 1)
5843 && (collectBuffer
.charAt(0) == '+')) {
5845 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5857 if ((type
== DeviceType
.VT220
)
5858 || (type
== DeviceType
.XTERM
)) {
5860 if ((collectBuffer
.length() == 1)
5861 && (collectBuffer
.charAt(0) == '(')) {
5863 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5865 if ((collectBuffer
.length() == 1)
5866 && (collectBuffer
.charAt(0) == ')')) {
5868 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5870 if ((collectBuffer
.length() == 1)
5871 && (collectBuffer
.charAt(0) == '*')) {
5873 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5875 if ((collectBuffer
.length() == 1)
5876 && (collectBuffer
.charAt(0) == '+')) {
5878 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5883 if ((type
== DeviceType
.VT220
)
5884 || (type
== DeviceType
.XTERM
)) {
5886 if ((collectBuffer
.length() == 1)
5887 && (collectBuffer
.charAt(0) == '(')) {
5889 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5891 if ((collectBuffer
.length() == 1)
5892 && (collectBuffer
.charAt(0) == ')')) {
5894 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5896 if ((collectBuffer
.length() == 1)
5897 && (collectBuffer
.charAt(0) == '*')) {
5899 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5901 if ((collectBuffer
.length() == 1)
5902 && (collectBuffer
.charAt(0) == '+')) {
5904 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5951 // 0x9C goes to GROUND
5959 // 00-17, 19, 1C-1F --> execute
5961 handleControlChar((char) ch
);
5964 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5965 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5967 scanState
= ScanState
.CSI_INTERMEDIATE
;
5970 // 30-39, 3B --> param, then switch to CSI_PARAM
5971 if ((ch
>= '0') && (ch
<= '9')) {
5973 scanState
= ScanState
.CSI_PARAM
;
5977 scanState
= ScanState
.CSI_PARAM
;
5980 // 3C-3F --> collect, then switch to CSI_PARAM
5981 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5983 scanState
= ScanState
.CSI_PARAM
;
5986 // 40-7E --> dispatch, then switch to GROUND
5987 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5990 // ICH - Insert character
5998 // CUD - Cursor down
6002 // CUF - Cursor forward
6006 // CUB - Cursor backward
6010 // CNL - Cursor down and to column 1
6011 if (type
== DeviceType
.XTERM
) {
6016 // CPL - Cursor up and to column 1
6017 if (type
== DeviceType
.XTERM
) {
6022 // CHA - Cursor to column # in current row
6023 if (type
== DeviceType
.XTERM
) {
6028 // CUP - Cursor position
6032 // CHT - Cursor forward X tab stops (default 1)
6033 if (type
== DeviceType
.XTERM
) {
6038 // ED - Erase in display
6042 // EL - Erase in line
6057 // DCH - Delete character
6064 // Scroll up X lines (default 1)
6065 if (type
== DeviceType
.XTERM
) {
6066 boolean xtermPrivateModeFlag
= false;
6067 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6068 if (collectBuffer
.charAt(i
) == '?') {
6069 xtermPrivateModeFlag
= true;
6073 if (xtermPrivateModeFlag
) {
6081 // Scroll down X lines (default 1)
6082 if (type
== DeviceType
.XTERM
) {
6091 if ((type
== DeviceType
.VT220
)
6092 || (type
== DeviceType
.XTERM
)) {
6094 // ECH - Erase character
6101 // CBT - Cursor backward X tab stops (default 1)
6102 if (type
== DeviceType
.XTERM
) {
6113 // HPA - Cursor to column # in current row. Same as CHA
6114 if (type
== DeviceType
.XTERM
) {
6119 // HPR - Cursor right. Same as CUF
6120 if (type
== DeviceType
.XTERM
) {
6125 // REP - Repeat last char X times
6126 if (type
== DeviceType
.XTERM
) {
6131 // DA - Device attributes
6135 // VPA - Cursor to row, current column.
6136 if (type
== DeviceType
.XTERM
) {
6141 // VPR - Cursor down. Same as CUD
6142 if (type
== DeviceType
.XTERM
) {
6147 // HVP - Horizontal and vertical position
6151 // TBC - Tabulation clear
6155 // Sets an ANSI or DEC private toggle
6159 if ((type
== DeviceType
.VT220
)
6160 || (type
== DeviceType
.XTERM
)) {
6162 // Printer functions
6170 // Sets an ANSI or DEC private toggle
6174 // SGR - Select graphics rendition
6178 // DSR - Device status report
6185 // DECLL - Load leds
6189 // DECSTBM - Set top and bottom margins
6193 // Save cursor (ANSI.SYS)
6194 if (type
== DeviceType
.XTERM
) {
6195 savedState
.cursorX
= currentState
.cursorX
;
6196 savedState
.cursorY
= currentState
.cursorY
;
6200 if (type
== DeviceType
.XTERM
) {
6201 // Window operations
6206 // Restore cursor (ANSI.SYS)
6207 if (type
== DeviceType
.XTERM
) {
6208 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
6215 // DECREQTPARM - Request terminal parameters
6231 // 0x9C goes to GROUND
6236 // 0x3A goes to CSI_IGNORE
6238 scanState
= ScanState
.CSI_IGNORE
;
6243 // 00-17, 19, 1C-1F --> execute
6245 handleControlChar((char) ch
);
6248 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6249 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6251 scanState
= ScanState
.CSI_INTERMEDIATE
;
6254 // 30-39, 3B --> param
6255 if ((ch
>= '0') && (ch
<= '9')) {
6262 // 0x3A goes to CSI_IGNORE
6264 scanState
= ScanState
.CSI_IGNORE
;
6266 // 0x3C-3F goes to CSI_IGNORE
6267 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6268 scanState
= ScanState
.CSI_IGNORE
;
6271 // 40-7E --> dispatch, then switch to GROUND
6272 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6275 // ICH - Insert character
6283 // CUD - Cursor down
6287 // CUF - Cursor forward
6291 // CUB - Cursor backward
6295 // CNL - Cursor down and to column 1
6296 if (type
== DeviceType
.XTERM
) {
6301 // CPL - Cursor up and to column 1
6302 if (type
== DeviceType
.XTERM
) {
6307 // CHA - Cursor to column # in current row
6308 if (type
== DeviceType
.XTERM
) {
6313 // CUP - Cursor position
6317 // CHT - Cursor forward X tab stops (default 1)
6318 if (type
== DeviceType
.XTERM
) {
6323 // ED - Erase in display
6327 // EL - Erase in line
6342 // DCH - Delete character
6349 // Scroll up X lines (default 1)
6350 if (type
== DeviceType
.XTERM
) {
6351 boolean xtermPrivateModeFlag
= false;
6352 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6353 if (collectBuffer
.charAt(i
) == '?') {
6354 xtermPrivateModeFlag
= true;
6358 if (xtermPrivateModeFlag
) {
6366 // Scroll down X lines (default 1)
6367 if (type
== DeviceType
.XTERM
) {
6376 if ((type
== DeviceType
.VT220
)
6377 || (type
== DeviceType
.XTERM
)) {
6379 // ECH - Erase character
6386 // CBT - Cursor backward X tab stops (default 1)
6387 if (type
== DeviceType
.XTERM
) {
6398 // HPA - Cursor to column # in current row. Same as CHA
6399 if (type
== DeviceType
.XTERM
) {
6404 // HPR - Cursor right. Same as CUF
6405 if (type
== DeviceType
.XTERM
) {
6410 // REP - Repeat last char X times
6411 if (type
== DeviceType
.XTERM
) {
6416 // DA - Device attributes
6420 // VPA - Cursor to row, current column.
6421 if (type
== DeviceType
.XTERM
) {
6426 // VPR - Cursor down. Same as CUD
6427 if (type
== DeviceType
.XTERM
) {
6432 // HVP - Horizontal and vertical position
6436 // TBC - Tabulation clear
6440 // Sets an ANSI or DEC private toggle
6444 if ((type
== DeviceType
.VT220
)
6445 || (type
== DeviceType
.XTERM
)) {
6447 // Printer functions
6455 // Sets an ANSI or DEC private toggle
6459 // SGR - Select graphics rendition
6463 // DSR - Device status report
6470 // DECLL - Load leds
6474 // DECSTBM - Set top and bottom margins
6480 if (type
== DeviceType
.XTERM
) {
6481 // Window operations
6490 // DECREQTPARM - Request terminal parameters
6507 case CSI_INTERMEDIATE
:
6508 // 00-17, 19, 1C-1F --> execute
6510 handleControlChar((char) ch
);
6513 // 20-2F --> collect
6514 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6518 // 0x30-3F goes to CSI_IGNORE
6519 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6520 scanState
= ScanState
.CSI_IGNORE
;
6523 // 40-7E --> dispatch, then switch to GROUND
6524 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6576 if (((type
== DeviceType
.VT220
)
6577 || (type
== DeviceType
.XTERM
))
6578 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6580 // DECSCL - compatibility level
6583 if ((type
== DeviceType
.XTERM
)
6584 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6586 // DECSTR - Soft terminal reset
6591 if (((type
== DeviceType
.VT220
)
6592 || (type
== DeviceType
.XTERM
))
6593 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6621 // 00-17, 19, 1C-1F --> execute
6623 handleControlChar((char) ch
);
6626 // 20-2F --> collect
6627 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6631 // 40-7E --> ignore, then switch to GROUND
6632 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6636 // 20-3F, 7F --> ignore
6642 // 0x9C goes to GROUND
6647 // 0x1B 0x5C goes to GROUND
6652 if ((collectBuffer
.length() > 0)
6653 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6659 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6660 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6662 scanState
= ScanState
.DCS_INTERMEDIATE
;
6665 // 30-39, 3B --> param, then switch to DCS_PARAM
6666 if ((ch
>= '0') && (ch
<= '9')) {
6668 scanState
= ScanState
.DCS_PARAM
;
6672 scanState
= ScanState
.DCS_PARAM
;
6675 // 3C-3F --> collect, then switch to DCS_PARAM
6676 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6678 scanState
= ScanState
.DCS_PARAM
;
6681 // 00-17, 19, 1C-1F, 7F --> ignore
6683 // 0x3A goes to DCS_IGNORE
6685 scanState
= ScanState
.DCS_IGNORE
;
6688 // 0x71 goes to DCS_SIXEL
6690 sixelParseBuffer
= new StringBuilder();
6691 scanState
= ScanState
.DCS_SIXEL
;
6692 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6693 // 0x40-7E goes to DCS_PASSTHROUGH
6694 scanState
= ScanState
.DCS_PASSTHROUGH
;
6698 case DCS_INTERMEDIATE
:
6700 // 0x9C goes to GROUND
6705 // 0x1B 0x5C goes to GROUND
6710 if ((collectBuffer
.length() > 0)
6711 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6717 // 0x30-3F goes to DCS_IGNORE
6718 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6719 scanState
= ScanState
.DCS_IGNORE
;
6722 // 0x40-7E goes to DCS_PASSTHROUGH
6723 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6724 scanState
= ScanState
.DCS_PASSTHROUGH
;
6727 // 00-17, 19, 1C-1F, 7F --> ignore
6732 // 0x9C goes to GROUND
6737 // 0x1B 0x5C goes to GROUND
6742 if ((collectBuffer
.length() > 0)
6743 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6749 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6750 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6752 scanState
= ScanState
.DCS_INTERMEDIATE
;
6755 // 30-39, 3B --> param
6756 if ((ch
>= '0') && (ch
<= '9')) {
6763 // 00-17, 19, 1C-1F, 7F --> ignore
6765 // 0x3A, 3C-3F goes to DCS_IGNORE
6767 scanState
= ScanState
.DCS_IGNORE
;
6769 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6770 scanState
= ScanState
.DCS_IGNORE
;
6773 // 0x71 goes to DCS_SIXEL
6775 sixelParseBuffer
= new StringBuilder();
6776 scanState
= ScanState
.DCS_SIXEL
;
6777 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6778 // 0x40-7E goes to DCS_PASSTHROUGH
6779 scanState
= ScanState
.DCS_PASSTHROUGH
;
6783 case DCS_PASSTHROUGH
:
6784 // 0x9C goes to GROUND
6789 // 0x1B 0x5C goes to GROUND
6794 if ((collectBuffer
.length() > 0)
6795 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6801 // 00-17, 19, 1C-1F, 20-7E --> put
6803 // We ignore all DCS except sixel.
6807 // We ignore all DCS except sixel.
6810 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6811 // We ignore all DCS except sixel.
6814 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6815 // We ignore all DCS except sixel.
6824 // 00-17, 19, 1C-1F, 20-7F --> ignore
6826 // 0x9C goes to GROUND
6834 // 0x9C goes to GROUND
6841 // 0x1B 0x5C goes to GROUND
6847 if ((collectBuffer
.length() > 0)
6848 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6856 // 00-17, 19, 1C-1F, 20-7E --> put
6859 || ((ch
>= 0x1C) && (ch
<= 0x1F))
6860 || ((ch
>= 0x20) && (ch
<= 0x7E))
6862 sixelParseBuffer
.append((char) ch
);
6868 case SOSPMAPC_STRING
:
6869 // 00-17, 19, 1C-1F, 20-7F --> ignore
6871 // Special case for Jexer: PM can pass one control character
6876 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6880 // 0x9C goes to GROUND
6888 // Special case for Xterm: OSC can pass control characters
6889 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
6893 // 00-17, 19, 1C-1F --> ignore
6895 // 20-7F --> osc_put
6896 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6900 // 0x9C goes to GROUND
6907 case VT52_DIRECT_CURSOR_ADDRESS
:
6908 // This is a special case for the VT52 sequence "ESC Y l c"
6909 if (collectBuffer
.length() == 0) {
6911 } else if (collectBuffer
.length() == 1) {
6912 // We've got the two characters, one in the buffer and the
6914 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
6923 * Expose current cursor X to outside world.
6925 * @return current cursor X
6927 public final int getCursorX() {
6928 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6929 return currentState
.cursorX
* 2;
6931 return currentState
.cursorX
;
6935 * Expose current cursor Y to outside world.
6937 * @return current cursor Y
6939 public final int getCursorY() {
6940 return currentState
.cursorY
;
6944 * Returns true if this terminal has requested the mouse pointer be
6947 * @return true if this terminal has requested the mouse pointer be
6950 public final boolean hasHiddenMousePointer() {
6951 return hideMousePointer
;
6955 * Get the mouse protocol.
6957 * @return MouseProtocol.OFF, MouseProtocol.X10, etc.
6959 public MouseProtocol
getMouseProtocol() {
6960 return mouseProtocol
;
6964 * Draw the left and right cells of a two-cell-wide (full-width) glyph.
6966 * @param leftX the x position to draw the left half to
6967 * @param leftY the y position to draw the left half to
6968 * @param rightX the x position to draw the right half to
6969 * @param rightY the y position to draw the right half to
6970 * @param ch the character to draw
6972 private void drawHalves(final int leftX
, final int leftY
,
6973 final int rightX
, final int rightY
, final int ch
) {
6975 // System.err.println("drawHalves(): " + Integer.toHexString(ch));
6977 if (lastTextHeight
!= textHeight
) {
6978 glyphMaker
= GlyphMaker
.getInstance(textHeight
);
6979 lastTextHeight
= textHeight
;
6982 Cell cell
= new Cell(ch
, currentState
.attr
);
6983 BufferedImage image
= glyphMaker
.getImage(cell
, textWidth
* 2,
6985 BufferedImage leftImage
= image
.getSubimage(0, 0, textWidth
,
6987 BufferedImage rightImage
= image
.getSubimage(textWidth
, 0, textWidth
,
6990 Cell left
= new Cell(cell
);
6991 left
.setImage(leftImage
);
6992 left
.setWidth(Cell
.Width
.LEFT
);
6993 display
.get(leftY
).replace(leftX
, left
);
6995 Cell right
= new Cell(cell
);
6996 right
.setImage(rightImage
);
6997 right
.setWidth(Cell
.Width
.RIGHT
);
6998 display
.get(rightY
).replace(rightX
, right
);
7002 * Set the width of a character cell in pixels.
7004 * @param textWidth the width in pixels of a character cell
7006 public void setTextWidth(final int textWidth
) {
7007 this.textWidth
= textWidth
;
7011 * Set the height of a character cell in pixels.
7013 * @param textHeight the height in pixels of a character cell
7015 public void setTextHeight(final int textHeight
) {
7016 this.textHeight
= textHeight
;
7020 * Parse a sixel string into a bitmap image, and overlay that image onto
7023 private void parseSixel() {
7026 System.err.println("parseSixel(): '" + sixelParseBuffer.toString()
7030 Sixel sixel
= new Sixel(sixelParseBuffer
.toString(), sixelPalette
);
7031 BufferedImage image
= sixel
.getImage();
7033 // System.err.println("parseSixel(): image " + image);
7035 if (image
== null) {
7036 // Sixel data was malformed in some way, bail out.
7043 * Break up the image into text cell sized pieces as a new array of
7046 * Note original column position x0.
7050 * 1. Advance (printCharacter(' ')) for horizontal increment, or
7051 * index (linefeed() + cursorPosition(y, x0)) for vertical
7054 * 2. Set (x, y) cell image data.
7056 * 3. For the right and bottom edges:
7058 * a. Render the text to pixels using Terminus font.
7060 * b. Blit the image on top of the text, using alpha channel.
7062 int cellColumns
= image
.getWidth() / textWidth
;
7063 if (cellColumns
* textWidth
< image
.getWidth()) {
7066 int cellRows
= image
.getHeight() / textHeight
;
7067 if (cellRows
* textHeight
< image
.getHeight()) {
7071 // Break the image up into an array of cells.
7072 Cell
[][] cells
= new Cell
[cellColumns
][cellRows
];
7074 for (int x
= 0; x
< cellColumns
; x
++) {
7075 for (int y
= 0; y
< cellRows
; y
++) {
7077 int width
= textWidth
;
7078 if ((x
+ 1) * textWidth
> image
.getWidth()) {
7079 width
= image
.getWidth() - (x
* textWidth
);
7081 int height
= textHeight
;
7082 if ((y
+ 1) * textHeight
> image
.getHeight()) {
7083 height
= image
.getHeight() - (y
* textHeight
);
7086 Cell cell
= new Cell();
7087 cell
.setImage(image
.getSubimage(x
* textWidth
,
7088 y
* textHeight
, width
, height
));
7094 int x0
= currentState
.cursorX
;
7095 for (int y
= 0; y
< cellRows
; y
++) {
7096 for (int x
= 0; x
< cellColumns
; x
++) {
7097 assert (currentState
.cursorX
<= rightMargin
);
7099 // TODO: Render text of current cell first, then image over
7100 // it (accounting for blank pixels). For now, just copy the
7102 DisplayLine line
= display
.get(currentState
.cursorY
);
7103 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
7105 // If at the end of the visible screen, stop.
7106 if (currentState
.cursorX
== rightMargin
) {
7109 // Room for more image on the visible screen.
7110 currentState
.cursorX
++;
7113 cursorPosition(currentState
.cursorY
, x0
);
7119 * Parse a "Jexer" image string into a bitmap image, and overlay that
7120 * image onto the text cells.
7122 * @param pw width token
7123 * @param ph height token
7124 * @param ps scroll token
7125 * @param data pixel data
7127 private void parseJexerImage(final String pw
, final String ph
,
7128 final String ps
, final String data
) {
7131 int imageHeight
= 0;
7132 boolean scroll
= false;
7134 imageWidth
= Integer
.parseInt(pw
);
7135 imageHeight
= Integer
.parseInt(ph
);
7136 } catch (NumberFormatException e
) {
7140 if ((imageWidth
< 1)
7141 || (imageWidth
> 10000)
7142 || (imageHeight
< 1)
7143 || (imageHeight
> 10000)
7147 if (ps
.equals("1")) {
7149 } else if (ps
.equals("0")) {
7155 java
.util
.Base64
.Decoder base64
= java
.util
.Base64
.getDecoder();
7156 byte [] bytes
= base64
.decode(data
);
7157 if (bytes
.length
!= (imageWidth
* imageHeight
* 3)) {
7161 BufferedImage image
= new BufferedImage(imageWidth
, imageHeight
,
7162 BufferedImage
.TYPE_INT_ARGB
);
7164 for (int x
= 0; x
< imageWidth
; x
++) {
7165 for (int y
= 0; y
< imageHeight
; y
++) {
7166 int red
= bytes
[(y
* imageWidth
* 3) + (x
* 3) ];
7170 int green
= bytes
[(y
* imageWidth
* 3) + (x
* 3) + 1];
7174 int blue
= bytes
[(y
* imageWidth
* 3) + (x
* 3) + 2];
7178 int rgb
= 0xFF000000 | (red
<< 16) | (green
<< 8) | blue
;
7179 image
.setRGB(x
, y
, rgb
);
7186 * Break up the image into text cell sized pieces as a new array of
7189 * Note original column position x0.
7193 * 1. Advance (printCharacter(' ')) for horizontal increment, or
7194 * index (linefeed() + cursorPosition(y, x0)) for vertical
7197 * 2. Set (x, y) cell image data.
7199 * 3. For the right and bottom edges:
7201 * a. Render the text to pixels using Terminus font.
7203 * b. Blit the image on top of the text, using alpha channel.
7205 int cellColumns
= image
.getWidth() / textWidth
;
7206 if (cellColumns
* textWidth
< image
.getWidth()) {
7209 int cellRows
= image
.getHeight() / textHeight
;
7210 if (cellRows
* textHeight
< image
.getHeight()) {
7214 // Break the image up into an array of cells.
7215 Cell
[][] cells
= new Cell
[cellColumns
][cellRows
];
7217 for (int x
= 0; x
< cellColumns
; x
++) {
7218 for (int y
= 0; y
< cellRows
; y
++) {
7220 int width
= textWidth
;
7221 if ((x
+ 1) * textWidth
> image
.getWidth()) {
7222 width
= image
.getWidth() - (x
* textWidth
);
7224 int height
= textHeight
;
7225 if ((y
+ 1) * textHeight
> image
.getHeight()) {
7226 height
= image
.getHeight() - (y
* textHeight
);
7229 Cell cell
= new Cell();
7230 cell
.setImage(image
.getSubimage(x
* textWidth
,
7231 y
* textHeight
, width
, height
));
7237 int x0
= currentState
.cursorX
;
7238 for (int y
= 0; y
< cellRows
; y
++) {
7239 for (int x
= 0; x
< cellColumns
; x
++) {
7240 assert (currentState
.cursorX
<= rightMargin
);
7241 DisplayLine line
= display
.get(currentState
.cursorY
);
7242 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
7243 // If at the end of the visible screen, stop.
7244 if (currentState
.cursorX
== rightMargin
) {
7247 // Room for more image on the visible screen.
7248 currentState
.cursorX
++;
7250 if ((scroll
== true)
7251 || ((scroll
== false)
7252 && (currentState
.cursorY
< scrollRegionBottom
))
7256 cursorPosition(currentState
.cursorY
, x0
);