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 * The width of a character cell in pixels.
477 private int textWidth
= 16;
480 * The height of a character cell in pixels.
482 private int textHeight
= 20;
485 * The last used height of a character cell in pixels, only used for
488 private int lastTextHeight
= -1;
491 * The glyph drawer for full-width chars.
493 private GlyphMaker glyphMaker
= null;
496 * Input queue for keystrokes and mouse events to send to the remote
499 private ArrayList
<TInputEvent
> userQueue
= new ArrayList
<TInputEvent
>();
502 * DECSC/DECRC save/restore a subset of the total state. This class
503 * encapsulates those specific flags/modes.
505 private class SaveableState
{
508 * When true, cursor positions are relative to the scrolling region.
510 public boolean originMode
= false;
513 * The current editing X position.
515 public int cursorX
= 0;
518 * The current editing Y position.
520 public int cursorY
= 0;
523 * Which character set is currently selected in G0.
525 public CharacterSet g0Charset
= CharacterSet
.US
;
528 * Which character set is currently selected in G1.
530 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
533 * Which character set is currently selected in G2.
535 public CharacterSet g2Charset
= CharacterSet
.US
;
538 * Which character set is currently selected in G3.
540 public CharacterSet g3Charset
= CharacterSet
.US
;
543 * Which character set is currently selected in GR.
545 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
548 * The current drawing attributes.
550 public CellAttributes attr
;
555 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
560 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
565 public boolean lineWrap
= true;
570 public void reset() {
574 g0Charset
= CharacterSet
.US
;
575 g1Charset
= CharacterSet
.DRAWING
;
576 g2Charset
= CharacterSet
.US
;
577 g3Charset
= CharacterSet
.US
;
578 grCharset
= CharacterSet
.DRAWING
;
579 attr
= new CellAttributes();
580 glLockshift
= LockshiftMode
.NONE
;
581 grLockshift
= LockshiftMode
.NONE
;
586 * Copy attributes from another instance.
588 * @param that the other instance to match
590 public void setTo(final SaveableState that
) {
591 this.originMode
= that
.originMode
;
592 this.cursorX
= that
.cursorX
;
593 this.cursorY
= that
.cursorY
;
594 this.g0Charset
= that
.g0Charset
;
595 this.g1Charset
= that
.g1Charset
;
596 this.g2Charset
= that
.g2Charset
;
597 this.g3Charset
= that
.g3Charset
;
598 this.grCharset
= that
.grCharset
;
599 this.attr
= new CellAttributes();
600 this.attr
.setTo(that
.attr
);
601 this.glLockshift
= that
.glLockshift
;
602 this.grLockshift
= that
.grLockshift
;
603 this.lineWrap
= that
.lineWrap
;
607 * Public constructor.
609 public SaveableState() {
614 // ------------------------------------------------------------------------
615 // Constructors -----------------------------------------------------------
616 // ------------------------------------------------------------------------
619 * Public constructor.
621 * @param type one of the DeviceType constants to select VT100, VT102,
623 * @param inputStream an InputStream connected to the remote side. For
624 * type == XTERM, inputStream is converted to a Reader with UTF-8
626 * @param outputStream an OutputStream connected to the remote user. For
627 * type == XTERM, outputStream is converted to a Writer with UTF-8
629 * @param displayListener a callback to the outer display, or null for
630 * default VT100 behavior
631 * @throws UnsupportedEncodingException if an exception is thrown when
632 * creating the InputStreamReader
634 public ECMA48(final DeviceType type
, final InputStream inputStream
,
635 final OutputStream outputStream
, final DisplayListener displayListener
)
636 throws UnsupportedEncodingException
{
638 assert (inputStream
!= null);
639 assert (outputStream
!= null);
641 csiParams
= new ArrayList
<Integer
>();
642 tabStops
= new ArrayList
<Integer
>();
643 scrollback
= new ArrayList
<DisplayLine
>();
644 display
= new ArrayList
<DisplayLine
>();
647 if (inputStream
instanceof TimeoutInputStream
) {
648 this.inputStream
= (TimeoutInputStream
)inputStream
;
650 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
652 if (type
== DeviceType
.XTERM
) {
653 this.input
= new InputStreamReader(this.inputStream
, "UTF-8");
654 this.output
= new OutputStreamWriter(new
655 BufferedOutputStream(outputStream
), "UTF-8");
656 this.outputStream
= null;
659 this.outputStream
= new BufferedOutputStream(outputStream
);
661 this.displayListener
= displayListener
;
664 for (int i
= 0; i
< height
; i
++) {
665 display
.add(new DisplayLine(currentState
.attr
));
668 // Spin up the input reader
669 readerThread
= new Thread(this);
670 readerThread
.start();
673 // ------------------------------------------------------------------------
674 // Runnable ---------------------------------------------------------------
675 // ------------------------------------------------------------------------
678 * Read function runs on a separate thread.
680 public final void run() {
681 boolean utf8
= false;
682 boolean done
= false;
684 if (type
== DeviceType
.XTERM
) {
688 // available() will often return > 1, so we need to read in chunks to
690 char [] readBufferUTF8
= null;
691 byte [] readBuffer
= null;
693 readBufferUTF8
= new char[2048];
695 readBuffer
= new byte[2048];
698 while (!done
&& !stopReaderThread
) {
699 synchronized (userQueue
) {
700 while (userQueue
.size() > 0) {
701 handleUserEvent(userQueue
.remove(0));
706 int n
= inputStream
.available();
708 // System.err.printf("available() %d\n", n); System.err.flush();
710 if (readBufferUTF8
.length
< n
) {
711 // The buffer wasn't big enough, make it huger
712 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
715 readBufferUTF8
= new char[newSizeHalf
* 2];
718 if (readBuffer
.length
< n
) {
719 // The buffer wasn't big enough, make it huger
720 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
721 readBuffer
= new byte[newSizeHalf
* 2];
727 } catch (InterruptedException e
) {
736 rc
= input
.read(readBufferUTF8
, 0,
737 readBufferUTF8
.length
);
739 rc
= inputStream
.read(readBuffer
, 0,
742 } catch (ReadTimeoutException e
) {
746 // System.err.printf("read() %d\n", rc); System.err.flush();
751 // Don't step on UI events
752 synchronized (this) {
754 for (int i
= 0; i
< rc
;) {
755 int ch
= Character
.codePointAt(readBufferUTF8
,
757 i
+= Character
.charCount(ch
);
761 for (int i
= 0; i
< rc
; i
++) {
762 consume(readBuffer
[i
]);
766 // Permit my enclosing UI to know that I updated.
767 if (displayListener
!= null) {
768 displayListener
.displayChanged();
771 // System.err.println("end while loop"); System.err.flush();
772 } catch (IOException e
) {
775 // This is an unusual case. We want to see the stack trace,
776 // but it is related to the spawned process rather than the
777 // actual UI. We will generate the stack trace, and consume
778 // it as though it was emitted by the shell.
779 CharArrayWriter writer
= new CharArrayWriter();
780 // Send a ST and RIS to clear the emulator state.
782 writer
.write("\033\\\033c");
783 writer
.write("\n-----------------------------------\n");
784 e
.printStackTrace(new PrintWriter(writer
));
785 writer
.write("\n-----------------------------------\n");
786 } catch (IOException e2
) {
789 char [] stackTrace
= writer
.toCharArray();
790 for (int i
= 0; i
< stackTrace
.length
; i
++) {
791 if (stackTrace
[i
] == '\n') {
794 consume(stackTrace
[i
]);
798 } // while ((done == false) && (stopReaderThread == false))
800 // Let the rest of the world know that I am done.
801 stopReaderThread
= true;
804 inputStream
.cancelRead();
807 } catch (IOException e
) {
813 } catch (IOException e
) {
817 // Permit my enclosing UI to know that I updated.
818 if (displayListener
!= null) {
819 displayListener
.displayChanged();
822 // System.err.println("*** run() exiting..."); System.err.flush();
825 // ------------------------------------------------------------------------
826 // ECMA48 -----------------------------------------------------------------
827 // ------------------------------------------------------------------------
830 * Process keyboard and mouse events from the user.
832 * @param event the input event to consume
834 private void handleUserEvent(final TInputEvent event
) {
835 if (event
instanceof TKeypressEvent
) {
836 keypress(((TKeypressEvent
) event
).getKey());
838 if (event
instanceof TMouseEvent
) {
839 mouse((TMouseEvent
) event
);
844 * Add a keyboard and mouse event from the user to the queue.
846 * @param event the input event to consume
848 public void addUserEvent(final TInputEvent event
) {
849 synchronized (userQueue
) {
850 userQueue
.add(event
);
855 * Return the proper primary Device Attributes string.
857 * @return string to send to remote side that is appropriate for the
860 private String
deviceTypeResponse() {
863 // "I am a VT100 with advanced video option" (often VT102)
872 // "I am a VT220" - 7 bit version
874 return "\033[?62;1;6;9;4c";
876 // "I am a VT220" - 8 bit version
877 return "\u009b?62;1;6;9;4c";
879 throw new IllegalArgumentException("Invalid device type: " + type
);
884 * Return the proper TERM environment variable for this device type.
886 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
887 * @return "vt100", "xterm", etc.
889 public static String
deviceTypeTerm(final DeviceType deviceType
) {
890 switch (deviceType
) {
904 throw new IllegalArgumentException("Invalid device type: "
910 * Return the proper LANG for this device type. Only XTERM devices know
911 * about UTF-8, the others are defined by their standard to be either
912 * 7-bit or 8-bit characters only.
914 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
915 * @param baseLang a base language without UTF-8 flag such as "C" or
917 * @return "en_US", "en_US.UTF-8", etc.
919 public static String
deviceTypeLang(final DeviceType deviceType
,
920 final String baseLang
) {
922 switch (deviceType
) {
930 return baseLang
+ ".UTF-8";
933 throw new IllegalArgumentException("Invalid device type: "
939 * Write a string directly to the remote side.
941 * @param str string to send
943 public void writeRemote(final String str
) {
944 if (stopReaderThread
) {
945 // Reader hit EOF, bail out now.
950 // System.err.printf("writeRemote() '%s'\n", str);
956 if (outputStream
== null) {
960 outputStream
.flush();
961 for (int i
= 0; i
< str
.length(); i
++) {
962 outputStream
.write(str
.charAt(i
));
964 outputStream
.flush();
965 } catch (IOException e
) {
971 if (output
== null) {
978 } catch (IOException e
) {
984 throw new IllegalArgumentException("Invalid device type: " + type
);
989 * Close the input and output streams and stop the reader thread. Note
990 * that it is safe to call this multiple times.
992 public final void close() {
994 // Tell the reader thread to stop looking at input. It will close
995 // the input streams.
996 if (stopReaderThread
== false) {
997 stopReaderThread
= true;
999 readerThread
.join(1000);
1000 } catch (InterruptedException e
) {
1005 // Now close the output stream.
1010 if (outputStream
!= null) {
1012 outputStream
.close();
1013 } catch (IOException e
) {
1016 outputStream
= null;
1020 if (outputStream
!= null) {
1022 outputStream
.close();
1023 } catch (IOException e
) {
1026 outputStream
= null;
1028 if (output
!= null) {
1031 } catch (IOException e
) {
1038 throw new IllegalArgumentException("Invalid device type: " +
1044 * See if the reader thread is still running.
1046 * @return if true, we are still connected to / reading from the remote
1049 public final boolean isReading() {
1050 return (!stopReaderThread
);
1054 * Obtain a new blank display line for an external user
1055 * (e.g. TTerminalWindow).
1057 * @return new blank line
1059 public final DisplayLine
getBlankDisplayLine() {
1060 return new DisplayLine(currentState
.attr
);
1064 * Get the scrollback buffer.
1066 * @return the scrollback buffer
1068 public final List
<DisplayLine
> getScrollbackBuffer() {
1073 * Get the display buffer.
1075 * @return the display buffer
1077 public final List
<DisplayLine
> getDisplayBuffer() {
1082 * Get the visible display + scrollback buffer, offset by a specified
1083 * number of rows from the bottom.
1085 * @param visibleHeight the total height of the display to show
1086 * @param scrollBottom the number of rows from the bottom to scroll back
1087 * @return a copy of the display + scrollback buffers
1089 public final List
<DisplayLine
> getVisibleDisplay(final int visibleHeight
,
1090 final int scrollBottom
) {
1092 assert (visibleHeight
>= 0);
1093 assert (scrollBottom
>= 0);
1095 int visibleBottom
= scrollback
.size() + display
.size() - scrollBottom
;
1097 List
<DisplayLine
> preceedingBlankLines
= new ArrayList
<DisplayLine
>();
1098 int visibleTop
= visibleBottom
- visibleHeight
;
1099 if (visibleTop
< 0) {
1100 for (int i
= visibleTop
; i
< 0; i
++) {
1101 preceedingBlankLines
.add(getBlankDisplayLine());
1105 assert (visibleTop
>= 0);
1107 List
<DisplayLine
> displayLines
= new ArrayList
<DisplayLine
>();
1108 displayLines
.addAll(scrollback
);
1109 displayLines
.addAll(display
);
1111 List
<DisplayLine
> visibleLines
= new ArrayList
<DisplayLine
>();
1112 visibleLines
.addAll(preceedingBlankLines
);
1113 visibleLines
.addAll(displayLines
.subList(visibleTop
, visibleBottom
));
1115 // Fill in the blank lines on bottom
1116 int bottomBlankLines
= visibleHeight
- visibleLines
.size();
1117 assert (bottomBlankLines
>= 0);
1118 for (int i
= 0; i
< bottomBlankLines
; i
++) {
1119 visibleLines
.add(getBlankDisplayLine());
1122 return copyBuffer(visibleLines
);
1126 * Copy a display buffer.
1128 * @param buffer the buffer to copy
1129 * @return a deep copy of the buffer's data
1131 private List
<DisplayLine
> copyBuffer(final List
<DisplayLine
> buffer
) {
1132 ArrayList
<DisplayLine
> result
= new ArrayList
<DisplayLine
>(buffer
.size());
1133 for (DisplayLine line
: buffer
) {
1134 result
.add(new DisplayLine(line
));
1140 * Get the display width.
1142 * @return the width (usually 80 or 132)
1144 public final int getWidth() {
1149 * Set the display width.
1151 * @param width the new width
1153 public final synchronized void setWidth(final int width
) {
1155 rightMargin
= width
- 1;
1156 if (currentState
.cursorX
>= width
) {
1157 currentState
.cursorX
= width
- 1;
1159 if (savedState
.cursorX
>= width
) {
1160 savedState
.cursorX
= width
- 1;
1165 * Get the display height.
1167 * @return the height (usually 24)
1169 public final int getHeight() {
1174 * Set the display height.
1176 * @param height the new height
1178 public final synchronized void setHeight(final int height
) {
1179 int delta
= height
- this.height
;
1180 this.height
= height
;
1181 scrollRegionBottom
+= delta
;
1182 if (scrollRegionBottom
< 0) {
1183 scrollRegionBottom
= height
;
1185 if (scrollRegionTop
>= scrollRegionBottom
) {
1186 scrollRegionTop
= 0;
1188 if (currentState
.cursorY
>= height
) {
1189 currentState
.cursorY
= height
- 1;
1191 if (savedState
.cursorY
>= height
) {
1192 savedState
.cursorY
= height
- 1;
1194 while (display
.size() < height
) {
1195 DisplayLine line
= new DisplayLine(currentState
.attr
);
1196 line
.setReverseColor(reverseVideo
);
1199 while (display
.size() > height
) {
1200 scrollback
.add(display
.remove(0));
1205 * Get visible cursor flag.
1207 * @return if true, the cursor is visible
1209 public final boolean isCursorVisible() {
1210 return cursorVisible
;
1214 * Get the screen title as set by the xterm OSC sequence. Lots of
1215 * applications send a screenTitle regardless of whether it is an xterm
1218 * @return screen title
1220 public final String
getScreenTitle() {
1225 * Get 132 columns value.
1227 * @return if true, the terminal is in 132 column mode
1229 public final boolean isColumns132() {
1234 * Clear the CSI parameters and flags.
1236 private void toGround() {
1238 collectBuffer
= new StringBuilder(8);
1239 scanState
= ScanState
.GROUND
;
1243 * Reset the tab stops list.
1245 private void resetTabStops() {
1247 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
1248 tabStops
.add(Integer
.valueOf(i
* 8));
1253 * Reset the 88- or 256-colors.
1255 private void resetColors() {
1256 colors88
= new ArrayList
<Integer
>(256);
1257 for (int i
= 0; i
< 256; i
++) {
1261 // Set default system colors.
1262 colors88
.set(0, 0x00000000);
1263 colors88
.set(1, 0x00a80000);
1264 colors88
.set(2, 0x0000a800);
1265 colors88
.set(3, 0x00a85400);
1266 colors88
.set(4, 0x000000a8);
1267 colors88
.set(5, 0x00a800a8);
1268 colors88
.set(6, 0x0000a8a8);
1269 colors88
.set(7, 0x00a8a8a8);
1271 colors88
.set(8, 0x00545454);
1272 colors88
.set(9, 0x00fc5454);
1273 colors88
.set(10, 0x0054fc54);
1274 colors88
.set(11, 0x00fcfc54);
1275 colors88
.set(12, 0x005454fc);
1276 colors88
.set(13, 0x00fc54fc);
1277 colors88
.set(14, 0x0054fcfc);
1278 colors88
.set(15, 0x00fcfcfc);
1282 * Get the RGB value of one of the indexed colors.
1284 * @param index the color index
1285 * @return the RGB value
1287 private int get88Color(final int index
) {
1288 // System.err.print("get88Color: " + index);
1289 if ((index
< 0) || (index
> colors88
.size())) {
1290 // System.err.println(" -- UNKNOWN");
1293 // System.err.printf(" %08x\n", colors88.get(index));
1294 return colors88
.get(index
);
1298 * Set one of the indexed colors to a color specification.
1300 * @param index the color index
1301 * @param spec the specification, typically something like "rgb:aa/bb/cc"
1303 private void set88Color(final int index
, final String spec
) {
1304 // System.err.println("set88Color: " + index + " '" + spec + "'");
1306 if ((index
< 0) || (index
> colors88
.size())) {
1309 if (spec
.startsWith("rgb:")) {
1310 String
[] rgbTokens
= spec
.substring(4).split("/");
1311 if (rgbTokens
.length
== 3) {
1313 int rgb
= (Integer
.parseInt(rgbTokens
[0], 16) << 16);
1314 rgb
|= Integer
.parseInt(rgbTokens
[1], 16) << 8;
1315 rgb
|= Integer
.parseInt(rgbTokens
[2], 16);
1316 // System.err.printf(" set to %08x\n", rgb);
1317 colors88
.set(index
, rgb
);
1318 } catch (NumberFormatException e
) {
1325 if (spec
.toLowerCase().equals("black")) {
1326 colors88
.set(index
, 0x00000000);
1327 } else if (spec
.toLowerCase().equals("red")) {
1328 colors88
.set(index
, 0x00a80000);
1329 } else if (spec
.toLowerCase().equals("green")) {
1330 colors88
.set(index
, 0x0000a800);
1331 } else if (spec
.toLowerCase().equals("yellow")) {
1332 colors88
.set(index
, 0x00a85400);
1333 } else if (spec
.toLowerCase().equals("blue")) {
1334 colors88
.set(index
, 0x000000a8);
1335 } else if (spec
.toLowerCase().equals("magenta")) {
1336 colors88
.set(index
, 0x00a800a8);
1337 } else if (spec
.toLowerCase().equals("cyan")) {
1338 colors88
.set(index
, 0x0000a8a8);
1339 } else if (spec
.toLowerCase().equals("white")) {
1340 colors88
.set(index
, 0x00a8a8a8);
1346 * Reset the emulation state.
1348 private void reset() {
1350 currentState
= new SaveableState();
1351 savedState
= new SaveableState();
1352 scanState
= ScanState
.GROUND
;
1355 scrollRegionTop
= 0;
1356 scrollRegionBottom
= height
- 1;
1357 rightMargin
= width
- 1;
1358 newLineMode
= false;
1359 arrowKeyMode
= ArrowKeyMode
.ANSI
;
1360 keypadMode
= KeypadMode
.Numeric
;
1361 wrapLineFlag
= false;
1362 if (displayListener
!= null) {
1363 width
= displayListener
.getDisplayWidth();
1364 height
= displayListener
.getDisplayHeight();
1365 rightMargin
= width
- 1;
1373 newLineMode
= false;
1374 reverseVideo
= false;
1376 cursorVisible
= true;
1379 singleshift
= Singleshift
.NONE
;
1381 printerControllerMode
= false;
1384 mouseProtocol
= MouseProtocol
.OFF
;
1385 mouseEncoding
= MouseEncoding
.X10
;
1390 // Reset extra colors
1398 * Append a new line to the bottom of the display, adding lines off the
1399 * top to the scrollback buffer.
1401 private void newDisplayLine() {
1402 // Scroll the top line off into the scrollback buffer
1403 scrollback
.add(display
.get(0));
1404 if (scrollback
.size() > maxScrollback
) {
1405 scrollback
.remove(0);
1406 scrollback
.trimToSize();
1409 display
.trimToSize();
1410 DisplayLine line
= new DisplayLine(currentState
.attr
);
1411 line
.setReverseColor(reverseVideo
);
1416 * Wraps the current line.
1418 private void wrapCurrentLine() {
1419 if (currentState
.cursorY
== height
- 1) {
1422 if (currentState
.cursorY
< height
- 1) {
1423 currentState
.cursorY
++;
1425 currentState
.cursorX
= 0;
1429 * Handle a carriage return.
1431 private void carriageReturn() {
1432 currentState
.cursorX
= 0;
1433 wrapLineFlag
= false;
1437 * Reverse the color of the visible display.
1439 private void invertDisplayColors() {
1440 for (DisplayLine line
: display
) {
1441 line
.setReverseColor(!line
.isReverseColor());
1446 * Handle a linefeed.
1448 private void linefeed() {
1450 if (currentState
.cursorY
< scrollRegionBottom
) {
1451 // Increment screen y
1452 currentState
.cursorY
++;
1455 // Screen y does not increment
1458 * Two cases: either we're inside a scrolling region or not. If
1459 * the scrolling region bottom is the bottom of the screen, then
1460 * push the top line into the buffer. Else scroll the scrolling
1463 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1465 // We're at the bottom of the scroll region, AND the scroll
1466 // region is the entire screen.
1472 // We're at the bottom of the scroll region, AND the scroll
1473 // region is NOT the entire screen.
1474 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1479 currentState
.cursorX
= 0;
1481 wrapLineFlag
= false;
1485 * Prints one character to the display buffer.
1487 * @param ch character to display
1489 private void printCharacter(final int ch
) {
1490 int rightMargin
= this.rightMargin
;
1492 if (StringUtils
.width(ch
) == 2) {
1493 // This is a full-width character. Save two spaces, and then
1494 // draw the character as two image halves.
1495 int x0
= currentState
.cursorX
;
1496 int y0
= currentState
.cursorY
;
1497 printCharacter(' ');
1498 printCharacter(' ');
1499 if ((currentState
.cursorX
== x0
+ 2)
1500 && (currentState
.cursorY
== y0
)
1502 // We can draw both halves of the character.
1503 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1504 } else if ((currentState
.cursorX
== x0
+ 1)
1505 && (currentState
.cursorY
== y0
)
1507 // VT100 line wrap behavior: we should be at the right
1508 // margin. We can draw both halves of the character.
1509 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1511 // The character splits across the line. Draw the entire
1512 // character on the new line, giving one more space for it.
1513 x0
= currentState
.cursorX
- 1;
1514 y0
= currentState
.cursorY
;
1515 printCharacter(' ');
1516 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1521 // Check if we have double-width, and if so chop at 40/66 instead of
1523 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1524 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1527 // Check the unusually-complicated line wrapping conditions...
1528 if (currentState
.cursorX
== rightMargin
) {
1530 if (currentState
.lineWrap
== true) {
1532 * This case happens when: the cursor was already on the
1533 * right margin (either through printing or by an explicit
1534 * placement command), and a character was printed.
1536 * The line wraps only when a new character arrives AND the
1537 * cursor is already on the right margin AND has placed a
1538 * character in its cell. Easier to see than to explain.
1540 if (wrapLineFlag
== false) {
1542 * This block marks the case that we are in the margin
1543 * and the first character has been received and printed.
1545 wrapLineFlag
= true;
1548 * This block marks the case that we are in the margin
1549 * and the second character has been received and
1552 wrapLineFlag
= false;
1556 } else if (currentState
.cursorX
<= rightMargin
) {
1558 * This is the normal case: a character came in and was printed
1559 * to the left of the right margin column.
1562 // Turn off VT100 special-case flag
1563 wrapLineFlag
= false;
1566 // "Print" the character
1567 Cell newCell
= new Cell(ch
);
1568 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1569 newCellAttributes
.setTo(currentState
.attr
);
1570 DisplayLine line
= display
.get(currentState
.cursorY
);
1572 if (StringUtils
.width(ch
) == 1) {
1573 // Insert mode special case
1574 if (insertMode
== true) {
1575 line
.insert(currentState
.cursorX
, newCell
);
1577 // Replace an existing character
1578 line
.replace(currentState
.cursorX
, newCell
);
1581 // Increment horizontal
1582 if (wrapLineFlag
== false) {
1583 currentState
.cursorX
++;
1584 if (currentState
.cursorX
> rightMargin
) {
1585 currentState
.cursorX
--;
1592 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1593 * send to the remote side.
1595 * @param mouse mouse event received from the local user
1597 private void mouse(final TMouseEvent mouse
) {
1600 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1601 mouseProtocol, mouseEncoding, mouse);
1604 if (mouseEncoding
== MouseEncoding
.X10
) {
1605 // We will support X10 but only for (160,94) and smaller.
1606 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1611 switch (mouseProtocol
) {
1618 // Only report button presses
1619 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1625 // Only report button presses and releases
1626 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1627 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1635 * Only report button presses, button releases, and motions that
1636 * have a button down (i.e. drag-and-drop).
1638 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1639 if (!mouse
.isMouse1()
1640 && !mouse
.isMouse2()
1641 && !mouse
.isMouse3()
1642 && !mouse
.isMouseWheelUp()
1643 && !mouse
.isMouseWheelDown()
1651 // Report everything
1655 // Now encode the event
1656 StringBuilder sb
= new StringBuilder(6);
1657 if (mouseEncoding
== MouseEncoding
.SGR
) {
1658 sb
.append((char) 0x1B);
1661 if (mouse
.isMouse1()) {
1662 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1667 } else if (mouse
.isMouse2()) {
1668 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1673 } else if (mouse
.isMouse3()) {
1674 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1679 } else if (mouse
.isMouseWheelUp()) {
1681 } else if (mouse
.isMouseWheelDown()) {
1684 // This is motion with no buttons down.
1688 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1691 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1698 // X10 and UTF8 encodings
1699 sb
.append((char) 0x1B);
1702 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1703 sb
.append((char) (0x03 + 32));
1704 } else if (mouse
.isMouse1()) {
1705 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1706 sb
.append((char) (0x00 + 32 + 32));
1708 sb
.append((char) (0x00 + 32));
1710 } else if (mouse
.isMouse2()) {
1711 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1712 sb
.append((char) (0x01 + 32 + 32));
1714 sb
.append((char) (0x01 + 32));
1716 } else if (mouse
.isMouse3()) {
1717 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1718 sb
.append((char) (0x02 + 32 + 32));
1720 sb
.append((char) (0x02 + 32));
1722 } else if (mouse
.isMouseWheelUp()) {
1723 sb
.append((char) (0x04 + 64));
1724 } else if (mouse
.isMouseWheelDown()) {
1725 sb
.append((char) (0x05 + 64));
1727 // This is motion with no buttons down.
1728 sb
.append((char) (0x03 + 32));
1731 sb
.append((char) (mouse
.getX() + 33));
1732 sb
.append((char) (mouse
.getY() + 33));
1735 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1736 writeRemote(sb
.toString());
1740 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1741 * send to the remote side.
1743 * @param keypress keypress received from the local user
1745 private void keypress(final TKeypress keypress
) {
1746 writeRemote(keypressToString(keypress
));
1750 * Build one of the complex xterm keystroke sequences, storing the result in
1751 * xterm_keystroke_buffer.
1753 * @param ss3 the prefix to use based on VT100 state.
1754 * @param first the first character, usually a number.
1755 * @param first the last character, one of the following: ~ A B C D F H
1756 * @param ctrl whether or not ctrl is down
1757 * @param alt whether or not alt is down
1758 * @param shift whether or not shift is down
1759 * @return the buffer with the full key sequence
1761 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1762 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1764 StringBuilder sb
= new StringBuilder(ss3
);
1765 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1769 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1771 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1773 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1775 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1777 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1779 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1781 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1786 return sb
.toString();
1790 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1792 * @param keypress keypress received from the local user
1793 * @return string to transmit to the remote side
1795 @SuppressWarnings("fallthrough")
1796 private String
keypressToString(final TKeypress keypress
) {
1798 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1800 * If this is a control character, process it like it came from
1803 if (keypress
.getChar() < 0x20) {
1804 handleControlChar((char) keypress
.getChar());
1806 // Local echo for everything else
1807 printCharacter(keypress
.getChar());
1809 if (displayListener
!= null) {
1810 displayListener
.displayChanged();
1814 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1819 // Handle control characters
1820 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1821 StringBuilder sb
= new StringBuilder();
1822 int ch
= keypress
.getChar();
1824 sb
.append(Character
.toChars(ch
));
1825 return sb
.toString();
1828 // Handle alt characters
1829 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1830 StringBuilder sb
= new StringBuilder("\033");
1831 int ch
= keypress
.getChar();
1832 sb
.append(Character
.toChars(ch
));
1833 return sb
.toString();
1836 if (keypress
.equals(kbBackspaceDel
)) {
1849 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1852 switch (arrowKeyMode
) {
1854 return xtermBuildKeySequence("\033[", '1', 'D',
1855 keypress
.isCtrl(), keypress
.isAlt(),
1856 keypress
.isShift());
1858 return xtermBuildKeySequence("\033", '1', 'D',
1859 keypress
.isCtrl(), keypress
.isAlt(),
1860 keypress
.isShift());
1862 return xtermBuildKeySequence("\033O", '1', 'D',
1863 keypress
.isCtrl(), keypress
.isAlt(),
1864 keypress
.isShift());
1867 switch (arrowKeyMode
) {
1878 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1881 switch (arrowKeyMode
) {
1883 return xtermBuildKeySequence("\033[", '1', 'C',
1884 keypress
.isCtrl(), keypress
.isAlt(),
1885 keypress
.isShift());
1887 return xtermBuildKeySequence("\033", '1', 'C',
1888 keypress
.isCtrl(), keypress
.isAlt(),
1889 keypress
.isShift());
1891 return xtermBuildKeySequence("\033O", '1', 'C',
1892 keypress
.isCtrl(), keypress
.isAlt(),
1893 keypress
.isShift());
1896 switch (arrowKeyMode
) {
1907 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1910 switch (arrowKeyMode
) {
1912 return xtermBuildKeySequence("\033[", '1', 'A',
1913 keypress
.isCtrl(), keypress
.isAlt(),
1914 keypress
.isShift());
1916 return xtermBuildKeySequence("\033", '1', 'A',
1917 keypress
.isCtrl(), keypress
.isAlt(),
1918 keypress
.isShift());
1920 return xtermBuildKeySequence("\033O", '1', 'A',
1921 keypress
.isCtrl(), keypress
.isAlt(),
1922 keypress
.isShift());
1925 switch (arrowKeyMode
) {
1936 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1939 switch (arrowKeyMode
) {
1941 return xtermBuildKeySequence("\033[", '1', 'B',
1942 keypress
.isCtrl(), keypress
.isAlt(),
1943 keypress
.isShift());
1945 return xtermBuildKeySequence("\033", '1', 'B',
1946 keypress
.isCtrl(), keypress
.isAlt(),
1947 keypress
.isShift());
1949 return xtermBuildKeySequence("\033O", '1', 'B',
1950 keypress
.isCtrl(), keypress
.isAlt(),
1951 keypress
.isShift());
1954 switch (arrowKeyMode
) {
1965 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1968 switch (arrowKeyMode
) {
1970 return xtermBuildKeySequence("\033[", '1', 'H',
1971 keypress
.isCtrl(), keypress
.isAlt(),
1972 keypress
.isShift());
1974 return xtermBuildKeySequence("\033", '1', 'H',
1975 keypress
.isCtrl(), keypress
.isAlt(),
1976 keypress
.isShift());
1978 return xtermBuildKeySequence("\033O", '1', 'H',
1979 keypress
.isCtrl(), keypress
.isAlt(),
1980 keypress
.isShift());
1983 switch (arrowKeyMode
) {
1994 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1997 switch (arrowKeyMode
) {
1999 return xtermBuildKeySequence("\033[", '1', 'F',
2000 keypress
.isCtrl(), keypress
.isAlt(),
2001 keypress
.isShift());
2003 return xtermBuildKeySequence("\033", '1', 'F',
2004 keypress
.isCtrl(), keypress
.isAlt(),
2005 keypress
.isShift());
2007 return xtermBuildKeySequence("\033O", '1', 'F',
2008 keypress
.isCtrl(), keypress
.isAlt(),
2009 keypress
.isShift());
2012 switch (arrowKeyMode
) {
2023 if (keypress
.equals(kbF1
)) {
2031 if (keypress
.equals(kbF2
)) {
2039 if (keypress
.equals(kbF3
)) {
2047 if (keypress
.equals(kbF4
)) {
2055 if (keypress
.equals(kbF5
)) {
2068 if (keypress
.equals(kbF6
)) {
2081 if (keypress
.equals(kbF7
)) {
2094 if (keypress
.equals(kbF8
)) {
2107 if (keypress
.equals(kbF9
)) {
2120 if (keypress
.equals(kbF10
)) {
2133 if (keypress
.equals(kbF11
)) {
2137 if (keypress
.equals(kbF12
)) {
2141 if (keypress
.equals(kbShiftF1
)) {
2146 if (type
== DeviceType
.XTERM
) {
2152 if (keypress
.equals(kbShiftF2
)) {
2157 if (type
== DeviceType
.XTERM
) {
2163 if (keypress
.equals(kbShiftF3
)) {
2168 if (type
== DeviceType
.XTERM
) {
2174 if (keypress
.equals(kbShiftF4
)) {
2179 if (type
== DeviceType
.XTERM
) {
2185 if (keypress
.equals(kbShiftF5
)) {
2187 return "\033[15;2~";
2190 if (keypress
.equals(kbShiftF6
)) {
2192 return "\033[17;2~";
2195 if (keypress
.equals(kbShiftF7
)) {
2197 return "\033[18;2~";
2200 if (keypress
.equals(kbShiftF8
)) {
2202 return "\033[19;2~";
2205 if (keypress
.equals(kbShiftF9
)) {
2207 return "\033[20;2~";
2210 if (keypress
.equals(kbShiftF10
)) {
2212 return "\033[21;2~";
2215 if (keypress
.equals(kbShiftF11
)) {
2217 return "\033[23;2~";
2220 if (keypress
.equals(kbShiftF12
)) {
2222 return "\033[24;2~";
2225 if (keypress
.equals(kbCtrlF1
)) {
2230 if (type
== DeviceType
.XTERM
) {
2236 if (keypress
.equals(kbCtrlF2
)) {
2241 if (type
== DeviceType
.XTERM
) {
2247 if (keypress
.equals(kbCtrlF3
)) {
2252 if (type
== DeviceType
.XTERM
) {
2258 if (keypress
.equals(kbCtrlF4
)) {
2263 if (type
== DeviceType
.XTERM
) {
2269 if (keypress
.equals(kbCtrlF5
)) {
2271 return "\033[15;5~";
2274 if (keypress
.equals(kbCtrlF6
)) {
2276 return "\033[17;5~";
2279 if (keypress
.equals(kbCtrlF7
)) {
2281 return "\033[18;5~";
2284 if (keypress
.equals(kbCtrlF8
)) {
2286 return "\033[19;5~";
2289 if (keypress
.equals(kbCtrlF9
)) {
2291 return "\033[20;5~";
2294 if (keypress
.equals(kbCtrlF10
)) {
2296 return "\033[21;5~";
2299 if (keypress
.equals(kbCtrlF11
)) {
2301 return "\033[23;5~";
2304 if (keypress
.equals(kbCtrlF12
)) {
2306 return "\033[24;5~";
2309 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2312 return xtermBuildKeySequence("\033[", '5', '~',
2313 keypress
.isCtrl(), keypress
.isAlt(),
2314 keypress
.isShift());
2320 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2323 return xtermBuildKeySequence("\033[", '6', '~',
2324 keypress
.isCtrl(), keypress
.isAlt(),
2325 keypress
.isShift());
2331 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2334 return xtermBuildKeySequence("\033[", '2', '~',
2335 keypress
.isCtrl(), keypress
.isAlt(),
2336 keypress
.isShift());
2342 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2345 return xtermBuildKeySequence("\033[", '3', '~',
2346 keypress
.isCtrl(), keypress
.isAlt(),
2347 keypress
.isShift());
2349 // Delete sends real delete for VTxxx
2354 if (keypress
.equals(kbEnter
)) {
2358 if (keypress
.equals(kbEsc
)) {
2362 if (keypress
.equals(kbAltEsc
)) {
2366 if (keypress
.equals(kbTab
)) {
2370 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2371 (keypress
.equals(kbShiftTab
))
2381 // Non-alt, non-ctrl characters
2382 if (!keypress
.isFnKey()) {
2383 StringBuilder sb
= new StringBuilder();
2384 sb
.append(Character
.toChars(keypress
.getChar()));
2385 return sb
.toString();
2391 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2394 * @param ch 8-bit character from the remote side
2395 * @param charsetGl character set defined for GL
2396 * @param charsetGr character set defined for GR
2397 * @return character to display on the screen
2399 private char mapCharacterCharset(final int ch
,
2400 final CharacterSet charsetGl
,
2401 final CharacterSet charsetGr
) {
2403 int lookupChar
= ch
;
2404 CharacterSet lookupCharset
= charsetGl
;
2407 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2408 lookupCharset
= charsetGr
;
2412 switch (lookupCharset
) {
2415 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2418 return DECCharacterSets
.UK
[lookupChar
];
2421 return DECCharacterSets
.US_ASCII
[lookupChar
];
2424 return DECCharacterSets
.NL
[lookupChar
];
2427 return DECCharacterSets
.FI
[lookupChar
];
2430 return DECCharacterSets
.FR
[lookupChar
];
2433 return DECCharacterSets
.FR_CA
[lookupChar
];
2436 return DECCharacterSets
.DE
[lookupChar
];
2439 return DECCharacterSets
.IT
[lookupChar
];
2442 return DECCharacterSets
.NO
[lookupChar
];
2445 return DECCharacterSets
.ES
[lookupChar
];
2448 return DECCharacterSets
.SV
[lookupChar
];
2451 return DECCharacterSets
.SWISS
[lookupChar
];
2453 case DEC_SUPPLEMENTAL
:
2454 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2457 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2460 return DECCharacterSets
.US_ASCII
[lookupChar
];
2463 return DECCharacterSets
.US_ASCII
[lookupChar
];
2466 throw new IllegalArgumentException("Invalid character set value: "
2472 * Map an 8-bit byte into a printable character.
2474 * @param ch either 8-bit or Unicode character from the remote side
2475 * @return character to display on the screen
2477 private int mapCharacter(final int ch
) {
2479 // Unicode character, just return it
2483 CharacterSet charsetGl
= currentState
.g0Charset
;
2484 CharacterSet charsetGr
= currentState
.grCharset
;
2486 if (vt52Mode
== true) {
2487 if (shiftOut
== true) {
2488 // Shifted out character, pull from VT52 graphics
2489 charsetGl
= currentState
.g1Charset
;
2490 charsetGr
= CharacterSet
.US
;
2493 charsetGl
= currentState
.g0Charset
;
2494 charsetGr
= CharacterSet
.US
;
2497 // Pull the character
2498 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2502 if (shiftOut
== true) {
2503 // Shifted out character, pull from G1
2504 charsetGl
= currentState
.g1Charset
;
2505 charsetGr
= currentState
.grCharset
;
2507 // Pull the character
2508 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2512 if (singleshift
== Singleshift
.SS2
) {
2514 singleshift
= Singleshift
.NONE
;
2516 // Shifted out character, pull from G2
2517 charsetGl
= currentState
.g2Charset
;
2518 charsetGr
= currentState
.grCharset
;
2522 if (singleshift
== Singleshift
.SS3
) {
2524 singleshift
= Singleshift
.NONE
;
2526 // Shifted out character, pull from G3
2527 charsetGl
= currentState
.g3Charset
;
2528 charsetGr
= currentState
.grCharset
;
2531 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2532 // Check for locking shift
2534 switch (currentState
.glLockshift
) {
2537 throw new IllegalArgumentException("programming bug");
2540 throw new IllegalArgumentException("programming bug");
2543 throw new IllegalArgumentException("programming bug");
2547 charsetGl
= currentState
.g2Charset
;
2552 charsetGl
= currentState
.g3Charset
;
2557 charsetGl
= currentState
.g0Charset
;
2561 switch (currentState
.grLockshift
) {
2564 throw new IllegalArgumentException("programming bug");
2567 throw new IllegalArgumentException("programming bug");
2571 charsetGr
= currentState
.g1Charset
;
2576 charsetGr
= currentState
.g2Charset
;
2581 charsetGr
= currentState
.g3Charset
;
2586 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2593 // Pull the character
2594 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2598 * Scroll the text within a scrolling region up n lines.
2600 * @param regionTop top row of the scrolling region
2601 * @param regionBottom bottom row of the scrolling region
2602 * @param n number of lines to scroll
2604 private void scrollingRegionScrollUp(final int regionTop
,
2605 final int regionBottom
, final int n
) {
2607 if (regionTop
>= regionBottom
) {
2611 // Sanity check: see if there will be any characters left after the
2613 if (regionBottom
+ 1 - regionTop
<= n
) {
2614 // There won't be anything left in the region, so just call
2615 // eraseScreen() and return.
2616 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2620 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2621 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2622 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2624 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2625 - remaining
, regionBottom
+ 1);
2626 display
= new ArrayList
<DisplayLine
>(displayTop
);
2627 display
.addAll(displayMiddle
);
2628 for (int i
= 0; i
< n
; i
++) {
2629 DisplayLine line
= new DisplayLine(currentState
.attr
);
2630 line
.setReverseColor(reverseVideo
);
2633 display
.addAll(displayBottom
);
2635 assert (display
.size() == height
);
2639 * Scroll the text within a scrolling region down n lines.
2641 * @param regionTop top row of the scrolling region
2642 * @param regionBottom bottom row of the scrolling region
2643 * @param n number of lines to scroll
2645 private void scrollingRegionScrollDown(final int regionTop
,
2646 final int regionBottom
, final int n
) {
2648 if (regionTop
>= regionBottom
) {
2652 // Sanity check: see if there will be any characters left after the
2654 if (regionBottom
+ 1 - regionTop
<= n
) {
2655 // There won't be anything left in the region, so just call
2656 // eraseScreen() and return.
2657 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2661 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2662 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2663 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2665 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2666 regionTop
+ remaining
);
2667 display
= new ArrayList
<DisplayLine
>(displayTop
);
2668 for (int i
= 0; i
< n
; i
++) {
2669 DisplayLine line
= new DisplayLine(currentState
.attr
);
2670 line
.setReverseColor(reverseVideo
);
2673 display
.addAll(displayMiddle
);
2674 display
.addAll(displayBottom
);
2676 assert (display
.size() == height
);
2680 * Process a control character.
2682 * @param ch 8-bit character from the remote side
2684 private void handleControlChar(final char ch
) {
2685 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2696 // Transmit the answerback message.
2707 cursorLeft(1, false);
2712 advanceToNextTabStop();
2738 currentState
.glLockshift
= LockshiftMode
.NONE
;
2744 currentState
.glLockshift
= LockshiftMode
.NONE
;
2769 singleshift
= Singleshift
.SS2
;
2774 singleshift
= Singleshift
.SS3
;
2784 * Advance the cursor to the next tab stop.
2786 private void advanceToNextTabStop() {
2787 if (tabStops
.size() == 0) {
2788 // Go to the rightmost column
2789 cursorRight(rightMargin
- currentState
.cursorX
, false);
2792 for (Integer stop
: tabStops
) {
2793 if (stop
> currentState
.cursorX
) {
2794 cursorRight(stop
- currentState
.cursorX
, false);
2799 * We got here, meaning there isn't a tab stop beyond the current
2800 * cursor position. Place the cursor of the right-most edge of the
2803 cursorRight(rightMargin
- currentState
.cursorX
, false);
2807 * Save a character into the collect buffer.
2809 * @param ch character to save
2811 private void collect(final char ch
) {
2812 collectBuffer
.append(ch
);
2816 * Save a byte into the CSI parameters buffer.
2818 * @param ch byte to save
2820 private void param(final byte ch
) {
2821 if (csiParams
.size() == 0) {
2822 csiParams
.add(Integer
.valueOf(0));
2824 Integer x
= csiParams
.get(csiParams
.size() - 1);
2825 if ((ch
>= '0') && (ch
<= '9')) {
2828 csiParams
.set(csiParams
.size() - 1, x
);
2831 if ((ch
== ';') && (csiParams
.size() < 16)) {
2832 csiParams
.add(Integer
.valueOf(0));
2837 * Get a CSI parameter value, with a default.
2839 * @param position parameter index. 0 is the first parameter.
2840 * @param defaultValue value to use if csiParams[position] doesn't exist
2841 * @return parameter value
2843 private int getCsiParam(final int position
, final int defaultValue
) {
2844 if (csiParams
.size() < position
+ 1) {
2845 return defaultValue
;
2847 return csiParams
.get(position
).intValue();
2851 * Get a CSI parameter value, clamped to within min/max.
2853 * @param position parameter index. 0 is the first parameter.
2854 * @param defaultValue value to use if csiParams[position] doesn't exist
2855 * @param minValue minimum value inclusive
2856 * @param maxValue maximum value inclusive
2857 * @return parameter value
2859 private int getCsiParam(final int position
, final int defaultValue
,
2860 final int minValue
, final int maxValue
) {
2862 assert (minValue
<= maxValue
);
2863 int value
= getCsiParam(position
, defaultValue
);
2864 if (value
< minValue
) {
2867 if (value
> maxValue
) {
2874 * Set or unset a toggle.
2876 * @param value true for set ('h'), false for reset ('l')
2878 private void setToggle(final boolean value
) {
2879 boolean decPrivateModeFlag
= false;
2881 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2882 if (collectBuffer
.charAt(i
) == '?') {
2883 decPrivateModeFlag
= true;
2888 for (Integer i
: csiParams
) {
2893 if (decPrivateModeFlag
== true) {
2895 if (value
== true) {
2896 // Use application arrow keys
2897 arrowKeyMode
= ArrowKeyMode
.VT100
;
2899 // Use ANSI arrow keys
2900 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2905 if (decPrivateModeFlag
== true) {
2906 if (value
== false) {
2910 arrowKeyMode
= ArrowKeyMode
.VT52
;
2913 * From the VT102 docs: "You use ANSI mode to select
2914 * most terminal features; the terminal uses the same
2915 * features when it switches to VT52 mode. You
2916 * cannot, however, change most of these features in
2919 * In other words, do not reset any other attributes
2920 * when switching between VT52 submode and ANSI.
2922 * HOWEVER, the real vt100 does switch the character
2923 * set according to Usenet.
2925 currentState
.g0Charset
= CharacterSet
.US
;
2926 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2929 if ((type
== DeviceType
.VT220
)
2930 || (type
== DeviceType
.XTERM
)) {
2932 // VT52 mode is explicitly 7-bit
2934 singleshift
= Singleshift
.NONE
;
2939 if (value
== true) {
2940 // Turn off keyboard
2949 if (decPrivateModeFlag
== true) {
2951 if (value
== true) {
2958 if ((displayListener
!= null)
2959 && (type
== DeviceType
.XTERM
)
2961 // For xterms, reset to the actual width, not 80
2963 width
= displayListener
.getDisplayWidth();
2964 rightMargin
= width
- 1;
2967 width
= rightMargin
+ 1;
2970 // Entire screen is cleared, and scrolling region is
2972 eraseScreen(0, 0, height
- 1, width
- 1, false);
2973 scrollRegionTop
= 0;
2974 scrollRegionBottom
= height
- 1;
2975 // Also home the cursor
2976 cursorPosition(0, 0);
2980 if (decPrivateModeFlag
== true) {
2982 if (value
== true) {
2991 if (value
== true) {
2999 if (decPrivateModeFlag
== true) {
3001 if (value
== true) {
3003 * Set selects reverse screen, a white screen
3004 * background with black characters.
3006 if (reverseVideo
!= true) {
3008 * If in normal video, switch it back
3010 invertDisplayColors();
3012 reverseVideo
= true;
3015 * Reset selects normal screen, a black screen
3016 * background with white characters.
3018 if (reverseVideo
== true) {
3020 * If in reverse video already, switch it back
3022 invertDisplayColors();
3024 reverseVideo
= false;
3029 if (decPrivateModeFlag
== true) {
3031 if (value
== true) {
3032 // Origin is relative to scroll region cursor.
3033 // Cursor can NEVER leave scrolling region.
3034 currentState
.originMode
= true;
3035 cursorPosition(0, 0);
3037 // Origin is absolute to entire screen. Cursor can
3038 // leave the scrolling region via cup() and hvp().
3039 currentState
.originMode
= false;
3040 cursorPosition(0, 0);
3045 if (decPrivateModeFlag
== true) {
3047 if (value
== true) {
3049 currentState
.lineWrap
= true;
3051 // Turn linewrap off
3052 currentState
.lineWrap
= false;
3057 if (decPrivateModeFlag
== true) {
3059 if (value
== true) {
3060 // Keyboard auto-repeat on
3063 // Keyboard auto-repeat off
3069 if (decPrivateModeFlag
== false) {
3071 if (value
== true) {
3081 if (decPrivateModeFlag
== true) {
3087 if (decPrivateModeFlag
== true) {
3093 if (decPrivateModeFlag
== false) {
3095 if (value
== true) {
3097 * Set causes a received linefeed, form feed, or
3098 * vertical tab to move cursor to first column of
3099 * next line. RETURN transmits both a carriage return
3100 * and linefeed. This selection is also called new
3106 * Reset causes a received linefeed, form feed, or
3107 * vertical tab to move cursor to next line in
3108 * current column. RETURN transmits a carriage
3111 newLineMode
= false;
3117 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3118 if (decPrivateModeFlag
== true) {
3120 if (value
== true) {
3122 cursorVisible
= true;
3125 cursorVisible
= false;
3132 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3133 if (decPrivateModeFlag
== true) {
3135 if (value
== true) {
3136 // Select national mode NRC
3139 // Select multi-national mode
3148 if ((type
== DeviceType
.XTERM
)
3149 && (decPrivateModeFlag
== true)
3151 // Mouse: normal tracking mode
3152 if (value
== true) {
3153 mouseProtocol
= MouseProtocol
.NORMAL
;
3155 mouseProtocol
= MouseProtocol
.OFF
;
3161 if ((type
== DeviceType
.XTERM
)
3162 && (decPrivateModeFlag
== true)
3164 // Mouse: normal tracking mode
3165 if (value
== true) {
3166 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
3168 mouseProtocol
= MouseProtocol
.OFF
;
3174 if ((type
== DeviceType
.XTERM
)
3175 && (decPrivateModeFlag
== true)
3177 // Mouse: Any-event tracking mode
3178 if (value
== true) {
3179 mouseProtocol
= MouseProtocol
.ANYEVENT
;
3181 mouseProtocol
= MouseProtocol
.OFF
;
3187 if ((type
== DeviceType
.XTERM
)
3188 && (decPrivateModeFlag
== true)
3190 // Mouse: UTF-8 coordinates
3191 if (value
== true) {
3192 mouseEncoding
= MouseEncoding
.UTF8
;
3194 mouseEncoding
= MouseEncoding
.X10
;
3200 if ((type
== DeviceType
.XTERM
)
3201 && (decPrivateModeFlag
== true)
3203 // Mouse: SGR coordinates
3204 if (value
== true) {
3205 mouseEncoding
= MouseEncoding
.SGR
;
3207 mouseEncoding
= MouseEncoding
.X10
;
3220 * DECSC - Save cursor.
3222 private void decsc() {
3223 savedState
.setTo(currentState
);
3227 * DECRC - Restore cursor.
3229 private void decrc() {
3230 currentState
.setTo(savedState
);
3236 private void ind() {
3237 // Move the cursor and scroll if necessary. If at the bottom line
3238 // already, a scroll up is supposed to be performed.
3239 if (currentState
.cursorY
== scrollRegionBottom
) {
3240 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3242 cursorDown(1, true);
3246 * RI - Reverse index.
3249 // Move the cursor and scroll if necessary. If at the top line
3250 // already, a scroll down is supposed to be performed.
3251 if (currentState
.cursorY
== scrollRegionTop
) {
3252 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3260 private void nel() {
3261 // Move the cursor and scroll if necessary. If at the bottom line
3262 // already, a scroll up is supposed to be performed.
3263 if (currentState
.cursorY
== scrollRegionBottom
) {
3264 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3266 cursorDown(1, true);
3268 // Reset to the beginning of the next line
3269 currentState
.cursorX
= 0;
3273 * DECKPAM - Keypad application mode.
3275 private void deckpam() {
3276 keypadMode
= KeypadMode
.Application
;
3280 * DECKPNM - Keypad numeric mode.
3282 private void deckpnm() {
3283 keypadMode
= KeypadMode
.Numeric
;
3289 * @param n number of spaces to move
3290 * @param honorScrollRegion if true, then do nothing if the cursor is
3291 * outside the scrolling region
3293 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3297 * Special case: if a user moves the cursor from the right margin, we
3298 * have to reset the VT100 right margin flag.
3301 wrapLineFlag
= false;
3304 for (int i
= 0; i
< n
; i
++) {
3305 if (honorScrollRegion
== true) {
3306 // Honor the scrolling region
3307 if ((currentState
.cursorY
< scrollRegionTop
)
3308 || (currentState
.cursorY
> scrollRegionBottom
)
3310 // Outside region, do nothing
3313 // Inside region, go up
3314 top
= scrollRegionTop
;
3316 // Non-scrolling case
3320 if (currentState
.cursorY
> top
) {
3321 currentState
.cursorY
--;
3327 * Move down n spaces.
3329 * @param n number of spaces to move
3330 * @param honorScrollRegion if true, then do nothing if the cursor is
3331 * outside the scrolling region
3333 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3337 * Special case: if a user moves the cursor from the right margin, we
3338 * have to reset the VT100 right margin flag.
3341 wrapLineFlag
= false;
3344 for (int i
= 0; i
< n
; i
++) {
3346 if (honorScrollRegion
== true) {
3347 // Honor the scrolling region
3348 if (currentState
.cursorY
> scrollRegionBottom
) {
3349 // Outside region, do nothing
3352 // Inside region, go down
3353 bottom
= scrollRegionBottom
;
3355 // Non-scrolling case
3356 bottom
= height
- 1;
3359 if (currentState
.cursorY
< bottom
) {
3360 currentState
.cursorY
++;
3366 * Move left n spaces.
3368 * @param n number of spaces to move
3369 * @param honorScrollRegion if true, then do nothing if the cursor is
3370 * outside the scrolling region
3372 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3374 * Special case: if a user moves the cursor from the right margin, we
3375 * have to reset the VT100 right margin flag.
3378 wrapLineFlag
= false;
3381 for (int i
= 0; i
< n
; i
++) {
3382 if (honorScrollRegion
== true) {
3383 // Honor the scrolling region
3384 if ((currentState
.cursorY
< scrollRegionTop
)
3385 || (currentState
.cursorY
> scrollRegionBottom
)
3387 // Outside region, do nothing
3392 if (currentState
.cursorX
> 0) {
3393 currentState
.cursorX
--;
3399 * Move right n spaces.
3401 * @param n number of spaces to move
3402 * @param honorScrollRegion if true, then do nothing if the cursor is
3403 * outside the scrolling region
3405 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3406 int rightMargin
= this.rightMargin
;
3409 * Special case: if a user moves the cursor from the right margin, we
3410 * have to reset the VT100 right margin flag.
3413 wrapLineFlag
= false;
3416 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3417 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3420 for (int i
= 0; i
< n
; i
++) {
3421 if (honorScrollRegion
== true) {
3422 // Honor the scrolling region
3423 if ((currentState
.cursorY
< scrollRegionTop
)
3424 || (currentState
.cursorY
> scrollRegionBottom
)
3426 // Outside region, do nothing
3431 if (currentState
.cursorX
< rightMargin
) {
3432 currentState
.cursorX
++;
3438 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3440 * @param row row to move to
3441 * @param col column to move to
3443 private void cursorPosition(int row
, final int col
) {
3444 int rightMargin
= this.rightMargin
;
3449 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3450 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3453 // Set column number
3454 currentState
.cursorX
= col
;
3456 // Sanity check, bring column back to margin.
3457 if (currentState
.cursorX
> rightMargin
) {
3458 currentState
.cursorX
= rightMargin
;
3462 if (currentState
.originMode
== true) {
3463 row
+= scrollRegionTop
;
3465 if (currentState
.cursorY
< row
) {
3466 cursorDown(row
- currentState
.cursorY
, false);
3467 } else if (currentState
.cursorY
> row
) {
3468 cursorUp(currentState
.cursorY
- row
, false);
3471 wrapLineFlag
= false;
3475 * HTS - Horizontal tabulation set.
3477 private void hts() {
3478 for (Integer stop
: tabStops
) {
3479 if (stop
== currentState
.cursorX
) {
3480 // Already have a tab stop here
3485 // Append a tab stop to the end of the array and resort them
3486 tabStops
.add(currentState
.cursorX
);
3487 Collections
.sort(tabStops
);
3491 * DECSWL - Single-width line.
3493 private void decswl() {
3494 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3495 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3499 * DECDWL - Double-width line.
3501 private void decdwl() {
3502 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3503 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3507 * DECHDL - Double-height + double-width line.
3509 * @param topHalf if true, this sets the row to be the top half row of a
3512 private void dechdl(final boolean topHalf
) {
3513 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3514 if (topHalf
== true) {
3515 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3517 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3522 * DECALN - Screen alignment display.
3524 private void decaln() {
3525 Cell newCell
= new Cell('E');
3526 for (DisplayLine line
: display
) {
3527 for (int i
= 0; i
< line
.length(); i
++) {
3528 line
.replace(i
, newCell
);
3534 * DECSCL - Compatibility level.
3536 private void decscl() {
3537 int i
= getCsiParam(0, 0);
3538 int j
= getCsiParam(1, 0);
3542 currentState
.g0Charset
= CharacterSet
.US
;
3543 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3545 } else if (i
== 62) {
3547 if ((j
== 0) || (j
== 2)) {
3549 } else if (j
== 1) {
3556 * CUD - Cursor down.
3558 private void cud() {
3559 cursorDown(getCsiParam(0, 1, 1, height
), true);
3563 * CUF - Cursor forward.
3565 private void cuf() {
3566 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3570 * CUB - Cursor backward.
3572 private void cub() {
3573 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3579 private void cuu() {
3580 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3584 * CUP - Cursor position.
3586 private void cup() {
3587 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3588 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3592 * CNL - Cursor down and to column 1.
3594 private void cnl() {
3595 cursorDown(getCsiParam(0, 1, 1, height
), true);
3597 cursorLeft(currentState
.cursorX
, true);
3601 * CPL - Cursor up and to column 1.
3603 private void cpl() {
3604 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3606 cursorLeft(currentState
.cursorX
, true);
3610 * CHA - Cursor to column # in current row.
3612 private void cha() {
3613 cursorPosition(currentState
.cursorY
,
3614 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3618 * VPA - Cursor to row #, same column.
3620 private void vpa() {
3621 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3622 currentState
.cursorX
);
3626 * ED - Erase in display.
3629 boolean honorProtected
= false;
3630 boolean decPrivateModeFlag
= false;
3632 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3633 if (collectBuffer
.charAt(i
) == '?') {
3634 decPrivateModeFlag
= true;
3639 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3640 && (decPrivateModeFlag
== true)
3642 honorProtected
= true;
3645 int i
= getCsiParam(0, 0);
3648 // Erase from here to end of screen
3649 if (currentState
.cursorY
< height
- 1) {
3650 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3653 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3654 } else if (i
== 1) {
3655 // Erase from beginning of screen to here
3656 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3658 eraseLine(0, currentState
.cursorX
, honorProtected
);
3659 } else if (i
== 2) {
3660 // Erase entire screen
3661 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3666 * EL - Erase in line.
3669 boolean honorProtected
= false;
3670 boolean decPrivateModeFlag
= false;
3672 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3673 if (collectBuffer
.charAt(i
) == '?') {
3674 decPrivateModeFlag
= true;
3679 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3680 && (decPrivateModeFlag
== true)
3682 honorProtected
= true;
3685 int i
= getCsiParam(0, 0);
3688 // Erase from here to end of line
3689 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3690 } else if (i
== 1) {
3691 // Erase from beginning of line to here
3692 eraseLine(0, currentState
.cursorX
, honorProtected
);
3693 } else if (i
== 2) {
3694 // Erase entire line
3695 eraseLine(0, width
- 1, honorProtected
);
3700 * ECH - Erase # of characters in current row.
3702 private void ech() {
3703 int i
= getCsiParam(0, 1, 1, width
);
3705 // Erase from here to i characters
3706 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3713 int i
= getCsiParam(0, 1);
3715 if ((currentState
.cursorY
>= scrollRegionTop
)
3716 && (currentState
.cursorY
<= scrollRegionBottom
)
3719 // I can get the same effect with a scroll-down
3720 scrollingRegionScrollDown(currentState
.cursorY
,
3721 scrollRegionBottom
, i
);
3726 * DCH - Delete char.
3728 private void dch() {
3729 int n
= getCsiParam(0, 1);
3730 DisplayLine line
= display
.get(currentState
.cursorY
);
3731 Cell blank
= new Cell();
3732 for (int i
= 0; i
< n
; i
++) {
3733 line
.delete(currentState
.cursorX
, blank
);
3738 * ICH - Insert blank char at cursor.
3740 private void ich() {
3741 int n
= getCsiParam(0, 1);
3742 DisplayLine line
= display
.get(currentState
.cursorY
);
3743 Cell blank
= new Cell();
3744 for (int i
= 0; i
< n
; i
++) {
3745 line
.insert(currentState
.cursorX
, blank
);
3753 int i
= getCsiParam(0, 1);
3755 if ((currentState
.cursorY
>= scrollRegionTop
)
3756 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3758 // I can get the same effect with a scroll-down
3759 scrollingRegionScrollUp(currentState
.cursorY
,
3760 scrollRegionBottom
, i
);
3765 * HVP - Horizontal and vertical position.
3767 private void hvp() {
3772 * REP - Repeat character.
3774 private void rep() {
3775 int n
= getCsiParam(0, 1);
3776 for (int i
= 0; i
< n
; i
++) {
3777 printCharacter(repCh
);
3785 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3786 getCsiParam(0, 1, 1, height
));
3793 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3794 getCsiParam(0, 1, 1, height
));
3798 * CBT - Go back X tab stops.
3800 private void cbt() {
3801 int tabsToMove
= getCsiParam(0, 1);
3804 for (int i
= 0; i
< tabsToMove
; i
++) {
3805 int j
= currentState
.cursorX
;
3806 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3807 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3815 j
= tabStops
.get(tabI
);
3817 cursorPosition(currentState
.cursorY
, j
);
3822 * CHT - Advance X tab stops.
3824 private void cht() {
3825 int n
= getCsiParam(0, 1);
3826 for (int i
= 0; i
< n
; i
++) {
3827 advanceToNextTabStop();
3832 * SGR - Select graphics rendition.
3834 private void sgr() {
3836 if (csiParams
.size() == 0) {
3837 currentState
.attr
.reset();
3841 int sgrColorMode
= -1;
3842 boolean idx88Color
= false;
3843 boolean rgbColor
= false;
3847 for (Integer i
: csiParams
) {
3849 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
3851 assert (type
== DeviceType
.XTERM
);
3855 * Indexed color mode, we now have the index number.
3857 if (sgrColorMode
== 38) {
3858 currentState
.attr
.setForeColorRGB(get88Color(i
));
3860 assert (sgrColorMode
== 48);
3861 currentState
.attr
.setBackColorRGB(get88Color(i
));
3870 * RGB color mode, we are collecting tokens.
3874 } else if (rgbGreen
== -1) {
3875 rgbGreen
= i
& 0xFF;
3877 int rgb
= rgbRed
<< 16;
3878 rgb
|= rgbGreen
<< 8;
3881 // System.err.printf("RGB: %08x\n", rgb);
3883 if (sgrColorMode
== 38) {
3884 currentState
.attr
.setForeColorRGB(rgb
);
3886 assert (sgrColorMode
== 48);
3887 currentState
.attr
.setBackColorRGB(rgb
);
3908 * Indexed color mode.
3915 * This is neither indexed nor RGB color. Bail out.
3920 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
3926 currentState
.attr
.reset();
3931 currentState
.attr
.setBold(true);
3936 currentState
.attr
.setUnderline(true);
3941 currentState
.attr
.setBlink(true);
3946 currentState
.attr
.setReverse(true);
3953 if (type
== DeviceType
.XTERM
) {
3963 // Set black foreground
3964 currentState
.attr
.setForeColorRGB(get88Color(8));
3967 // Set red foreground
3968 currentState
.attr
.setForeColorRGB(get88Color(9));
3971 // Set green foreground
3972 currentState
.attr
.setForeColorRGB(get88Color(10));
3975 // Set yellow foreground
3976 currentState
.attr
.setForeColorRGB(get88Color(11));
3979 // Set blue foreground
3980 currentState
.attr
.setForeColorRGB(get88Color(12));
3983 // Set magenta foreground
3984 currentState
.attr
.setForeColorRGB(get88Color(13));
3987 // Set cyan foreground
3988 currentState
.attr
.setForeColorRGB(get88Color(14));
3991 // Set white foreground
3992 currentState
.attr
.setForeColorRGB(get88Color(15));
3996 // Set black background
3997 currentState
.attr
.setBackColorRGB(get88Color(8));
4000 // Set red background
4001 currentState
.attr
.setBackColorRGB(get88Color(9));
4004 // Set green background
4005 currentState
.attr
.setBackColorRGB(get88Color(10));
4008 // Set yellow background
4009 currentState
.attr
.setBackColorRGB(get88Color(11));
4012 // Set blue background
4013 currentState
.attr
.setBackColorRGB(get88Color(12));
4016 // Set magenta background
4017 currentState
.attr
.setBackColorRGB(get88Color(13));
4020 // Set cyan background
4021 currentState
.attr
.setBackColorRGB(get88Color(14));
4024 // Set white background
4025 currentState
.attr
.setBackColorRGB(get88Color(15));
4033 if ((type
== DeviceType
.VT220
)
4034 || (type
== DeviceType
.XTERM
)) {
4040 currentState
.attr
.setBold(false);
4045 currentState
.attr
.setUnderline(false);
4050 currentState
.attr
.setBlink(false);
4055 currentState
.attr
.setReverse(false);
4063 // A true VT100/102/220 does not support color, however everyone
4064 // is used to their terminal emulator supporting color so we will
4065 // unconditionally support color for all DeviceType's.
4070 // Set black foreground
4071 currentState
.attr
.setForeColor(Color
.BLACK
);
4072 currentState
.attr
.setForeColorRGB(-1);
4075 // Set red foreground
4076 currentState
.attr
.setForeColor(Color
.RED
);
4077 currentState
.attr
.setForeColorRGB(-1);
4080 // Set green foreground
4081 currentState
.attr
.setForeColor(Color
.GREEN
);
4082 currentState
.attr
.setForeColorRGB(-1);
4085 // Set yellow foreground
4086 currentState
.attr
.setForeColor(Color
.YELLOW
);
4087 currentState
.attr
.setForeColorRGB(-1);
4090 // Set blue foreground
4091 currentState
.attr
.setForeColor(Color
.BLUE
);
4092 currentState
.attr
.setForeColorRGB(-1);
4095 // Set magenta foreground
4096 currentState
.attr
.setForeColor(Color
.MAGENTA
);
4097 currentState
.attr
.setForeColorRGB(-1);
4100 // Set cyan foreground
4101 currentState
.attr
.setForeColor(Color
.CYAN
);
4102 currentState
.attr
.setForeColorRGB(-1);
4105 // Set white foreground
4106 currentState
.attr
.setForeColor(Color
.WHITE
);
4107 currentState
.attr
.setForeColorRGB(-1);
4110 if (type
== DeviceType
.XTERM
) {
4112 * Xterm supports T.416 / ISO-8613-3 codes to select
4113 * either an indexed color or an RGB value. (It also
4114 * permits these ISO-8613-3 SGR sequences to be separated
4115 * by colons rather than semicolons.)
4117 * We will support only the following:
4119 * 1. Indexed color mode (88- or 256-color modes).
4123 * These cover most of the use cases in the real world.
4125 * HOWEVER, note that this is an awful broken "standard",
4126 * with no way to do it "right". See
4127 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4128 * for a detailed discussion of the current state of RGB
4129 * in various terminals, the point of which is that none
4130 * of them really do the same thing despite all appearing
4134 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4135 * where it is assumed that supporting just the "indexed
4136 * mode" of these sequences (which could align easily
4137 * with existing SGR colors) is assumed to mean full
4138 * support of 24-bit RGB. So it is all or nothing.
4140 * Finally, these sequences break the assumptions of
4141 * standard ECMA-48 style parsers as pointed out at
4142 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4143 * Therefore in order to keep a clean display, we cannot
4144 * parse anything else in this sequence.
4149 // Underscore on, default foreground color
4150 currentState
.attr
.setUnderline(true);
4151 currentState
.attr
.setForeColor(Color
.WHITE
);
4155 // Underscore off, default foreground color
4156 currentState
.attr
.setUnderline(false);
4157 currentState
.attr
.setForeColor(Color
.WHITE
);
4158 currentState
.attr
.setForeColorRGB(-1);
4161 // Set black background
4162 currentState
.attr
.setBackColor(Color
.BLACK
);
4163 currentState
.attr
.setBackColorRGB(-1);
4166 // Set red background
4167 currentState
.attr
.setBackColor(Color
.RED
);
4168 currentState
.attr
.setBackColorRGB(-1);
4171 // Set green background
4172 currentState
.attr
.setBackColor(Color
.GREEN
);
4173 currentState
.attr
.setBackColorRGB(-1);
4176 // Set yellow background
4177 currentState
.attr
.setBackColor(Color
.YELLOW
);
4178 currentState
.attr
.setBackColorRGB(-1);
4181 // Set blue background
4182 currentState
.attr
.setBackColor(Color
.BLUE
);
4183 currentState
.attr
.setBackColorRGB(-1);
4186 // Set magenta background
4187 currentState
.attr
.setBackColor(Color
.MAGENTA
);
4188 currentState
.attr
.setBackColorRGB(-1);
4191 // Set cyan background
4192 currentState
.attr
.setBackColor(Color
.CYAN
);
4193 currentState
.attr
.setBackColorRGB(-1);
4196 // Set white background
4197 currentState
.attr
.setBackColor(Color
.WHITE
);
4198 currentState
.attr
.setBackColorRGB(-1);
4201 if (type
== DeviceType
.XTERM
) {
4203 * Xterm supports T.416 / ISO-8613-3 codes to select
4204 * either an indexed color or an RGB value. (It also
4205 * permits these ISO-8613-3 SGR sequences to be separated
4206 * by colons rather than semicolons.)
4208 * We will support only the following:
4210 * 1. Indexed color mode (88- or 256-color modes).
4214 * These cover most of the use cases in the real world.
4216 * HOWEVER, note that this is an awful broken "standard",
4217 * with no way to do it "right". See
4218 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4219 * for a detailed discussion of the current state of RGB
4220 * in various terminals, the point of which is that none
4221 * of them really do the same thing despite all appearing
4225 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4226 * where it is assumed that supporting just the "indexed
4227 * mode" of these sequences (which could align easily
4228 * with existing SGR colors) is assumed to mean full
4229 * support of 24-bit RGB. So it is all or nothing.
4231 * Finally, these sequences break the assumptions of
4232 * standard ECMA-48 style parsers as pointed out at
4233 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4234 * Therefore in order to keep a clean display, we cannot
4235 * parse anything else in this sequence.
4242 // Default background
4243 currentState
.attr
.setBackColor(Color
.BLACK
);
4244 currentState
.attr
.setBackColorRGB(-1);
4254 * DA - Device attributes.
4257 int extendedFlag
= 0;
4259 if (collectBuffer
.length() > 0) {
4260 String args
= collectBuffer
.substring(1);
4261 if (collectBuffer
.charAt(0) == '>') {
4263 if (collectBuffer
.length() >= 2) {
4264 i
= Integer
.parseInt(args
);
4266 } else if (collectBuffer
.charAt(0) == '=') {
4268 if (collectBuffer
.length() >= 2) {
4269 i
= Integer
.parseInt(args
);
4272 // Unknown code, bail out
4277 if ((i
!= 0) && (i
!= 1)) {
4281 if ((extendedFlag
== 0) && (i
== 0)) {
4282 // Send string directly to remote side
4283 writeRemote(deviceTypeResponse());
4287 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4289 if ((extendedFlag
== 1) && (i
== 0)) {
4291 * Request "What type of terminal are you, what is your
4292 * firmware version, and what hardware options do you have
4295 * Respond: "I am a VT220 (identification code of 1), my
4296 * firmware version is _____ (Pv), and I have _____ Po
4297 * options installed."
4303 if (s8c1t
== true) {
4304 writeRemote("\u009b>1;10;0c");
4306 writeRemote("\033[>1;10;0c");
4312 if ((extendedFlag
== 2) && (i
== 0)) {
4315 * Request "What is your unit ID?"
4317 * Respond: "I was manufactured at site 00 and have a unique ID
4321 writeRemote("\033P!|00010203\033\\");
4326 * DECSTBM - Set top and bottom margins.
4328 private void decstbm() {
4329 boolean decPrivateModeFlag
= false;
4331 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4332 if (collectBuffer
.charAt(i
) == '?') {
4333 decPrivateModeFlag
= true;
4337 if (decPrivateModeFlag
) {
4338 // This could be restore DEC private mode values.
4342 int top
= getCsiParam(0, 1, 1, height
) - 1;
4343 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4348 scrollRegionTop
= top
;
4349 scrollRegionBottom
= bottom
;
4352 cursorPosition(0, 0);
4357 * DECREQTPARM - Request terminal parameters.
4359 private void decreqtparm() {
4360 int i
= getCsiParam(0, 0);
4362 if ((i
!= 0) && (i
!= 1)) {
4369 * Request terminal parameters.
4373 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4374 * (CLoCk MULtiplier = 1, STP option flags = 0)
4378 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4381 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4383 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4389 * DECSCA - Select Character Attributes.
4391 private void decsca() {
4392 int i
= getCsiParam(0, 0);
4394 if ((i
== 0) || (i
== 2)) {
4396 currentState
.attr
.setProtect(false);
4400 currentState
.attr
.setProtect(true);
4405 * DECSTR - Soft Terminal Reset.
4407 private void decstr() {
4408 // Do exactly like RIS - Reset to initial state
4410 // Do I clear screen too? I think so...
4411 eraseScreen(0, 0, height
- 1, width
- 1, false);
4412 cursorPosition(0, 0);
4416 * DSR - Device status report.
4418 private void dsr() {
4419 boolean decPrivateModeFlag
= false;
4420 int row
= currentState
.cursorY
;
4422 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4423 if (collectBuffer
.charAt(i
) == '?') {
4424 decPrivateModeFlag
= true;
4429 int i
= getCsiParam(0, 0);
4434 // Request status report. Respond with "OK, no malfunction."
4436 // Send string directly to remote side
4437 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4440 writeRemote("\u009b0n");
4442 writeRemote("\033[0n");
4447 // Request cursor position. Respond with current position.
4448 if (currentState
.originMode
== true) {
4449 row
-= scrollRegionTop
;
4452 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4455 str
= String
.format("\u009b%d;%dR", row
+ 1,
4456 currentState
.cursorX
+ 1);
4458 str
= String
.format("\033[%d;%dR", row
+ 1,
4459 currentState
.cursorX
+ 1);
4462 // Send string directly to remote side
4467 if (decPrivateModeFlag
== true) {
4469 // Request printer status report. Respond with "Printer not
4472 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4473 && (s8c1t
== true)) {
4474 writeRemote("\u009b?13n");
4476 writeRemote("\033[?13n");
4482 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4483 && (decPrivateModeFlag
== true)
4486 // Request user-defined keys are locked or unlocked. Respond
4487 // with "User-defined keys are locked."
4489 if (s8c1t
== true) {
4490 writeRemote("\u009b?21n");
4492 writeRemote("\033[?21n");
4498 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4499 && (decPrivateModeFlag
== true)
4502 // Request keyboard language. Respond with "Keyboard
4503 // language is North American."
4505 if (s8c1t
== true) {
4506 writeRemote("\u009b?27;1n");
4508 writeRemote("\033[?27;1n");
4515 // Some other option, ignore
4521 * TBC - Tabulation clear.
4523 private void tbc() {
4524 int i
= getCsiParam(0, 0);
4526 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4527 for (Integer stop
: tabStops
) {
4528 if (stop
== currentState
.cursorX
) {
4533 tabStops
= newStops
;
4541 * Erase the characters in the current line from the start column to the
4542 * end column, inclusive.
4544 * @param start starting column to erase (between 0 and width - 1)
4545 * @param end ending column to erase (between 0 and width - 1)
4546 * @param honorProtected if true, do not erase characters with the
4547 * protected attribute set
4549 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4554 if (end
> width
- 1) {
4561 for (int i
= start
; i
<= end
; i
++) {
4562 DisplayLine line
= display
.get(currentState
.cursorY
);
4563 if ((!honorProtected
)
4564 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4571 * From the VT102 manual:
4573 * Erasing a character also erases any character
4574 * attribute of the character.
4580 * Erase with the current color a.k.a. back-color erase
4583 line
.setChar(i
, ' ');
4584 line
.setAttr(i
, currentState
.attr
);
4592 * Erase a rectangular section of the screen, inclusive. end column,
4595 * @param startRow starting row to erase (between 0 and height - 1)
4596 * @param startCol starting column to erase (between 0 and width - 1)
4597 * @param endRow ending row to erase (between 0 and height - 1)
4598 * @param endCol ending column to erase (between 0 and width - 1)
4599 * @param honorProtected if true, do not erase characters with the
4600 * protected attribute set
4602 private void eraseScreen(final int startRow
, final int startCol
,
4603 final int endRow
, final int endCol
, final boolean honorProtected
) {
4611 || (endRow
< startRow
)
4612 || (endCol
< startCol
)
4617 oldCursorY
= currentState
.cursorY
;
4618 for (int i
= startRow
; i
<= endRow
; i
++) {
4619 currentState
.cursorY
= i
;
4620 eraseLine(startCol
, endCol
, honorProtected
);
4622 // Erase display clears the double attributes
4623 display
.get(i
).setDoubleWidth(false);
4624 display
.get(i
).setDoubleHeight(0);
4626 currentState
.cursorY
= oldCursorY
;
4630 * VT220 printer functions. All of these are parsed, but won't do
4633 private void printerFunctions() {
4634 boolean decPrivateModeFlag
= false;
4635 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4636 if (collectBuffer
.charAt(i
) == '?') {
4637 decPrivateModeFlag
= true;
4642 int i
= getCsiParam(0, 0);
4647 if (decPrivateModeFlag
== false) {
4653 if (decPrivateModeFlag
== true) {
4654 // Print cursor line
4659 if (decPrivateModeFlag
== true) {
4660 // Auto print mode OFF
4662 // Printer controller OFF
4664 // Characters re-appear on the screen
4665 printerControllerMode
= false;
4670 if (decPrivateModeFlag
== true) {
4674 // Printer controller
4676 // Characters get sucked into oblivion
4677 printerControllerMode
= true;
4688 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4689 * of remote systems will send an XTerm title sequence even if TERM isn't
4692 * @param xtermChar the character received from the remote side
4694 private void oscPut(final char xtermChar
) {
4695 // System.err.println("oscPut: " + xtermChar);
4698 collectBuffer
.append(xtermChar
);
4701 if ((xtermChar
== 0x07)
4702 || (collectBuffer
.toString().endsWith("\033\\"))
4705 if (xtermChar
== 0x07) {
4706 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
4708 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4711 String
[] p
= args
.split(";");
4713 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4720 if (p
[0].equals("4")) {
4721 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
4722 // Set a color index value
4724 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
4725 } catch (NumberFormatException e
) {
4731 if (p
[0].equals("10")) {
4732 if (p
[1].equals("?")) {
4733 // Respond with foreground color.
4734 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToForegroundColor(currentState
.attr
);
4736 writeRemote(String
.format(
4737 "\033]10;rgb:%04x/%04x/%04x\033\\",
4738 color
.getRed() << 8,
4739 color
.getGreen() << 8,
4740 color
.getBlue() << 8));
4744 if (p
[0].equals("11")) {
4745 if (p
[1].equals("?")) {
4746 // Respond with background color.
4747 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToBackgroundColor(currentState
.attr
);
4749 writeRemote(String
.format(
4750 "\033]11;rgb:%04x/%04x/%04x\033\\",
4751 color
.getRed() << 8,
4752 color
.getGreen() << 8,
4753 color
.getBlue() << 8));
4758 // Go to SCAN_GROUND state
4765 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
4766 * Jexer ECMA48Terminal to talk to ECMA48.
4768 * @param pmChar the character received from the remote side
4770 private void pmPut(final char pmChar
) {
4771 // System.err.println("pmPut: " + pmChar);
4774 collectBuffer
.append(pmChar
);
4777 if (collectBuffer
.toString().endsWith("\033\\")) {
4779 arg
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4781 // System.err.println("arg: '" + arg + "'");
4783 if (arg
.equals("hideMousePointer")) {
4784 hideMousePointer
= true;
4786 if (arg
.equals("showMousePointer")) {
4787 hideMousePointer
= false;
4790 // Go to SCAN_GROUND state
4797 * Perform xterm window operations.
4799 private void xtermWindowOps() {
4800 boolean xtermPrivateModeFlag
= false;
4802 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4803 if (collectBuffer
.charAt(i
) == '?') {
4804 xtermPrivateModeFlag
= true;
4809 int i
= getCsiParam(0, 0);
4811 if (!xtermPrivateModeFlag
) {
4814 // Report xterm text area size in pixels as CSI 4 ; height ;
4816 writeRemote(String
.format("\033[4;%d;%dt", textHeight
* height
,
4817 textWidth
* width
));
4820 // Report character size in pixels as CSI 6 ; height ; width
4822 writeRemote(String
.format("\033[6;%d;%dt", textHeight
,
4826 // Report the text are size in characters as CSI 8 ; height ;
4828 writeRemote(String
.format("\033[8;%d;%dt", height
, width
));
4837 * Respond to xterm sixel query.
4839 private void xtermSixelQuery() {
4840 int item
= getCsiParam(0, 0);
4841 int action
= getCsiParam(1, 0);
4842 int value
= getCsiParam(2, 0);
4847 // Report number of color registers.
4848 writeRemote(String
.format("\033[?%d;%d;%dS", item
, 0, 1024));
4855 // We will not support this option.
4856 writeRemote(String
.format("\033[?%d;%dS", item
, action
));
4860 * Run this input character through the ECMA48 state machine.
4862 * @param ch character from the remote side
4864 private void consume(int ch
) {
4867 // System.err.printf("%c STATE = %s\n", ch, scanState);
4869 // Special case for VT10x: 7-bit characters only
4870 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4874 // Special "anywhere" states
4876 // 18, 1A --> execute, then switch to SCAN_GROUND
4877 if ((ch
== 0x18) || (ch
== 0x1A)) {
4878 // CAN and SUB abort escape sequences
4883 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4887 if ((type
== DeviceType
.XTERM
)
4888 && ((scanState
== ScanState
.OSC_STRING
)
4889 || (scanState
== ScanState
.DCS_SIXEL
)
4890 || (scanState
== ScanState
.SOSPMAPC_STRING
))
4892 // Xterm can pass ESCAPE to its OSC sequence.
4893 // Xterm can pass ESCAPE to its DCS sequence.
4894 // Jexer can pass ESCAPE to its PM sequence.
4895 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
4896 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4897 && (scanState
!= ScanState
.DCS_IGNORE
)
4898 && (scanState
!= ScanState
.DCS_PARAM
)
4899 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4901 scanState
= ScanState
.ESCAPE
;
4906 // 0x9B == CSI 8-bit sequence
4908 scanState
= ScanState
.CSI_ENTRY
;
4912 // 0x9D goes to ScanState.OSC_STRING
4914 scanState
= ScanState
.OSC_STRING
;
4918 // 0x90 goes to DCS_ENTRY
4920 scanState
= ScanState
.DCS_ENTRY
;
4924 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4925 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4926 scanState
= ScanState
.SOSPMAPC_STRING
;
4930 // 0x7F (DEL) is always discarded
4935 switch (scanState
) {
4938 // 00-17, 19, 1C-1F --> execute
4939 // 80-8F, 91-9A, 9C --> execute
4940 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4941 handleControlChar((char) ch
);
4945 if (((ch
>= 0x20) && (ch
<= 0x7F))
4949 // VT220 printer --> trash bin
4950 if (((type
== DeviceType
.VT220
)
4951 || (type
== DeviceType
.XTERM
))
4952 && (printerControllerMode
== true)
4957 // Hang onto this character
4958 repCh
= mapCharacter(ch
);
4960 // Print this character
4961 printCharacter(repCh
);
4966 // 00-17, 19, 1C-1F --> execute
4968 handleControlChar((char) ch
);
4972 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4973 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4975 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4979 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4980 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4991 // DECSC - Save cursor
4992 // Note this code overlaps both ANSI and VT52 mode
4997 // DECRC - Restore cursor
4998 // Note this code overlaps both ANSI and VT52 mode
5007 if (vt52Mode
== true) {
5008 // DECANM - Enter ANSI mode
5010 arrowKeyMode
= ArrowKeyMode
.VT100
;
5013 * From the VT102 docs: "You use ANSI mode to select
5014 * most terminal features; the terminal uses the same
5015 * features when it switches to VT52 mode. You
5016 * cannot, however, change most of these features in
5019 * In other words, do not reset any other attributes
5020 * when switching between VT52 submode and ANSI.
5024 currentState
.g0Charset
= CharacterSet
.US
;
5025 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5027 singleshift
= Singleshift
.NONE
;
5028 currentState
.glLockshift
= LockshiftMode
.NONE
;
5029 currentState
.grLockshift
= LockshiftMode
.NONE
;
5033 // DECKPAM - Keypad application mode
5034 // Note this code overlaps both ANSI and VT52 mode
5038 // DECKPNM - Keypad numeric mode
5039 // Note this code overlaps both ANSI and VT52 mode
5046 if (vt52Mode
== true) {
5047 // Cursor up, and stop at the top without scrolling
5052 if (vt52Mode
== true) {
5053 // Cursor down, and stop at the bottom without scrolling
5054 cursorDown(1, false);
5058 if (vt52Mode
== true) {
5059 // Cursor right, and stop at the right without scrolling
5060 cursorRight(1, false);
5064 if (vt52Mode
== true) {
5065 // Cursor left, and stop at the left without scrolling
5066 cursorLeft(1, false);
5073 if (vt52Mode
== true) {
5081 if (vt52Mode
== true) {
5082 // G0 --> Special graphics
5083 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
5087 if (vt52Mode
== true) {
5089 currentState
.g0Charset
= CharacterSet
.US
;
5093 if (vt52Mode
== true) {
5095 cursorPosition(0, 0);
5097 // HTS - Horizontal tabulation set
5102 if (vt52Mode
== true) {
5103 // Reverse line feed. Same as RI.
5108 if (vt52Mode
== true) {
5109 // Erase to end of screen
5110 eraseLine(currentState
.cursorX
, width
- 1, false);
5111 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
5116 if (vt52Mode
== true) {
5117 // Erase to end of line
5118 eraseLine(currentState
.cursorX
, width
- 1, false);
5124 if (vt52Mode
== true) {
5127 // RI - Reverse index
5132 if (vt52Mode
== false) {
5134 singleshift
= Singleshift
.SS2
;
5138 if (vt52Mode
== false) {
5140 singleshift
= Singleshift
.SS3
;
5147 if ((ch
>= 0x51) && (ch
<= 0x57)) {
5163 if (vt52Mode
== true) {
5164 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
5172 if (vt52Mode
== true) {
5174 // Send string directly to remote side
5175 writeRemote("\033/Z");
5178 // Send string directly to remote side
5179 writeRemote(deviceTypeResponse());
5190 // VT52 cannot get to any of these other states
5191 if (vt52Mode
== true) {
5196 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
5203 // RIS - Reset to initial state
5205 // Do I clear screen too? I think so...
5206 eraseScreen(0, 0, height
- 1, width
- 1, false);
5207 cursorPosition(0, 0);
5221 if ((type
== DeviceType
.VT220
)
5222 || (type
== DeviceType
.XTERM
)) {
5224 // VT220 lockshift G2 into GL
5225 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
5230 if ((type
== DeviceType
.VT220
)
5231 || (type
== DeviceType
.XTERM
)) {
5233 // VT220 lockshift G3 into GL
5234 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
5252 if ((type
== DeviceType
.VT220
)
5253 || (type
== DeviceType
.XTERM
)) {
5255 // VT220 lockshift G3 into GR
5256 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
5261 if ((type
== DeviceType
.VT220
)
5262 || (type
== DeviceType
.XTERM
)) {
5264 // VT220 lockshift G2 into GR
5265 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
5271 if ((type
== DeviceType
.VT220
)
5272 || (type
== DeviceType
.XTERM
)) {
5274 // VT220 lockshift G1 into GR
5275 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
5285 // 0x5B goes to CSI_ENTRY
5287 scanState
= ScanState
.CSI_ENTRY
;
5290 // 0x5D goes to OSC_STRING
5292 scanState
= ScanState
.OSC_STRING
;
5295 // 0x50 goes to DCS_ENTRY
5297 scanState
= ScanState
.DCS_ENTRY
;
5300 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5301 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
5302 scanState
= ScanState
.SOSPMAPC_STRING
;
5307 case ESCAPE_INTERMEDIATE
:
5308 // 00-17, 19, 1C-1F --> execute
5310 handleControlChar((char) ch
);
5313 // 20-2F --> collect
5314 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5318 // 30-7E --> dispatch, then switch to GROUND
5319 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
5322 if ((collectBuffer
.length() == 1)
5323 && (collectBuffer
.charAt(0) == '(')) {
5324 // G0 --> Special graphics
5325 currentState
.g0Charset
= CharacterSet
.DRAWING
;
5327 if ((collectBuffer
.length() == 1)
5328 && (collectBuffer
.charAt(0) == ')')) {
5329 // G1 --> Special graphics
5330 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5332 if ((type
== DeviceType
.VT220
)
5333 || (type
== DeviceType
.XTERM
)) {
5335 if ((collectBuffer
.length() == 1)
5336 && (collectBuffer
.charAt(0) == '*')) {
5337 // G2 --> Special graphics
5338 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5340 if ((collectBuffer
.length() == 1)
5341 && (collectBuffer
.charAt(0) == '+')) {
5342 // G3 --> Special graphics
5343 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5348 if ((collectBuffer
.length() == 1)
5349 && (collectBuffer
.charAt(0) == '(')) {
5350 // G0 --> Alternate character ROM standard character set
5351 currentState
.g0Charset
= CharacterSet
.ROM
;
5353 if ((collectBuffer
.length() == 1)
5354 && (collectBuffer
.charAt(0) == ')')) {
5355 // G1 --> Alternate character ROM standard character set
5356 currentState
.g1Charset
= CharacterSet
.ROM
;
5360 if ((collectBuffer
.length() == 1)
5361 && (collectBuffer
.charAt(0) == '(')) {
5362 // G0 --> Alternate character ROM special graphics
5363 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5365 if ((collectBuffer
.length() == 1)
5366 && (collectBuffer
.charAt(0) == ')')) {
5367 // G1 --> Alternate character ROM special graphics
5368 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5372 if ((collectBuffer
.length() == 1)
5373 && (collectBuffer
.charAt(0) == '#')) {
5374 // DECDHL - Double-height line (top half)
5379 if ((collectBuffer
.length() == 1)
5380 && (collectBuffer
.charAt(0) == '#')) {
5381 // DECDHL - Double-height line (bottom half)
5384 if ((type
== DeviceType
.VT220
)
5385 || (type
== DeviceType
.XTERM
)) {
5387 if ((collectBuffer
.length() == 1)
5388 && (collectBuffer
.charAt(0) == '(')) {
5390 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5392 if ((collectBuffer
.length() == 1)
5393 && (collectBuffer
.charAt(0) == ')')) {
5395 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5397 if ((collectBuffer
.length() == 1)
5398 && (collectBuffer
.charAt(0) == '*')) {
5400 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5402 if ((collectBuffer
.length() == 1)
5403 && (collectBuffer
.charAt(0) == '+')) {
5405 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5410 if ((collectBuffer
.length() == 1)
5411 && (collectBuffer
.charAt(0) == '#')) {
5412 // DECSWL - Single-width line
5415 if ((type
== DeviceType
.VT220
)
5416 || (type
== DeviceType
.XTERM
)) {
5418 if ((collectBuffer
.length() == 1)
5419 && (collectBuffer
.charAt(0) == '(')) {
5421 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5423 if ((collectBuffer
.length() == 1)
5424 && (collectBuffer
.charAt(0) == ')')) {
5426 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5428 if ((collectBuffer
.length() == 1)
5429 && (collectBuffer
.charAt(0) == '*')) {
5431 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5433 if ((collectBuffer
.length() == 1)
5434 && (collectBuffer
.charAt(0) == '+')) {
5436 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5441 if ((collectBuffer
.length() == 1)
5442 && (collectBuffer
.charAt(0) == '#')) {
5443 // DECDWL - Double-width line
5446 if ((type
== DeviceType
.VT220
)
5447 || (type
== DeviceType
.XTERM
)) {
5449 if ((collectBuffer
.length() == 1)
5450 && (collectBuffer
.charAt(0) == '(')) {
5452 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5454 if ((collectBuffer
.length() == 1)
5455 && (collectBuffer
.charAt(0) == ')')) {
5457 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5459 if ((collectBuffer
.length() == 1)
5460 && (collectBuffer
.charAt(0) == '*')) {
5462 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5464 if ((collectBuffer
.length() == 1)
5465 && (collectBuffer
.charAt(0) == '+')) {
5467 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5472 if ((type
== DeviceType
.VT220
)
5473 || (type
== DeviceType
.XTERM
)) {
5475 if ((collectBuffer
.length() == 1)
5476 && (collectBuffer
.charAt(0) == '(')) {
5478 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5480 if ((collectBuffer
.length() == 1)
5481 && (collectBuffer
.charAt(0) == ')')) {
5483 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5485 if ((collectBuffer
.length() == 1)
5486 && (collectBuffer
.charAt(0) == '*')) {
5488 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5490 if ((collectBuffer
.length() == 1)
5491 && (collectBuffer
.charAt(0) == '+')) {
5493 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5498 if ((collectBuffer
.length() == 1)
5499 && (collectBuffer
.charAt(0) == '#')) {
5500 // DECALN - Screen alignment display
5509 if ((type
== DeviceType
.VT220
)
5510 || (type
== DeviceType
.XTERM
)) {
5512 if ((collectBuffer
.length() == 1)
5513 && (collectBuffer
.charAt(0) == '(')) {
5514 // G0 --> DEC_SUPPLEMENTAL
5515 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5517 if ((collectBuffer
.length() == 1)
5518 && (collectBuffer
.charAt(0) == ')')) {
5519 // G1 --> DEC_SUPPLEMENTAL
5520 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5522 if ((collectBuffer
.length() == 1)
5523 && (collectBuffer
.charAt(0) == '*')) {
5524 // G2 --> DEC_SUPPLEMENTAL
5525 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5527 if ((collectBuffer
.length() == 1)
5528 && (collectBuffer
.charAt(0) == '+')) {
5529 // G3 --> DEC_SUPPLEMENTAL
5530 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5535 if ((type
== DeviceType
.VT220
)
5536 || (type
== DeviceType
.XTERM
)) {
5538 if ((collectBuffer
.length() == 1)
5539 && (collectBuffer
.charAt(0) == '(')) {
5541 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5543 if ((collectBuffer
.length() == 1)
5544 && (collectBuffer
.charAt(0) == ')')) {
5546 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5548 if ((collectBuffer
.length() == 1)
5549 && (collectBuffer
.charAt(0) == '*')) {
5551 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5553 if ((collectBuffer
.length() == 1)
5554 && (collectBuffer
.charAt(0) == '+')) {
5556 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5565 if ((collectBuffer
.length() == 1)
5566 && (collectBuffer
.charAt(0) == '(')) {
5567 // G0 --> United Kingdom set
5568 currentState
.g0Charset
= CharacterSet
.UK
;
5570 if ((collectBuffer
.length() == 1)
5571 && (collectBuffer
.charAt(0) == ')')) {
5572 // G1 --> United Kingdom set
5573 currentState
.g1Charset
= CharacterSet
.UK
;
5575 if ((type
== DeviceType
.VT220
)
5576 || (type
== DeviceType
.XTERM
)) {
5578 if ((collectBuffer
.length() == 1)
5579 && (collectBuffer
.charAt(0) == '*')) {
5580 // G2 --> United Kingdom set
5581 currentState
.g2Charset
= CharacterSet
.UK
;
5583 if ((collectBuffer
.length() == 1)
5584 && (collectBuffer
.charAt(0) == '+')) {
5585 // G3 --> United Kingdom set
5586 currentState
.g3Charset
= CharacterSet
.UK
;
5591 if ((collectBuffer
.length() == 1)
5592 && (collectBuffer
.charAt(0) == '(')) {
5594 currentState
.g0Charset
= CharacterSet
.US
;
5596 if ((collectBuffer
.length() == 1)
5597 && (collectBuffer
.charAt(0) == ')')) {
5599 currentState
.g1Charset
= CharacterSet
.US
;
5601 if ((type
== DeviceType
.VT220
)
5602 || (type
== DeviceType
.XTERM
)) {
5604 if ((collectBuffer
.length() == 1)
5605 && (collectBuffer
.charAt(0) == '*')) {
5607 currentState
.g2Charset
= CharacterSet
.US
;
5609 if ((collectBuffer
.length() == 1)
5610 && (collectBuffer
.charAt(0) == '+')) {
5612 currentState
.g3Charset
= CharacterSet
.US
;
5617 if ((type
== DeviceType
.VT220
)
5618 || (type
== DeviceType
.XTERM
)) {
5620 if ((collectBuffer
.length() == 1)
5621 && (collectBuffer
.charAt(0) == '(')) {
5623 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5625 if ((collectBuffer
.length() == 1)
5626 && (collectBuffer
.charAt(0) == ')')) {
5628 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5630 if ((collectBuffer
.length() == 1)
5631 && (collectBuffer
.charAt(0) == '*')) {
5633 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5635 if ((collectBuffer
.length() == 1)
5636 && (collectBuffer
.charAt(0) == '+')) {
5638 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5645 if ((type
== DeviceType
.VT220
)
5646 || (type
== DeviceType
.XTERM
)) {
5648 if ((collectBuffer
.length() == 1)
5649 && (collectBuffer
.charAt(0) == '(')) {
5651 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5653 if ((collectBuffer
.length() == 1)
5654 && (collectBuffer
.charAt(0) == ')')) {
5656 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5658 if ((collectBuffer
.length() == 1)
5659 && (collectBuffer
.charAt(0) == '*')) {
5661 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5663 if ((collectBuffer
.length() == 1)
5664 && (collectBuffer
.charAt(0) == '+')) {
5666 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5671 if ((type
== DeviceType
.VT220
)
5672 || (type
== DeviceType
.XTERM
)) {
5674 if ((collectBuffer
.length() == 1)
5675 && (collectBuffer
.charAt(0) == ' ')) {
5682 if ((type
== DeviceType
.VT220
)
5683 || (type
== DeviceType
.XTERM
)) {
5685 if ((collectBuffer
.length() == 1)
5686 && (collectBuffer
.charAt(0) == ' ')) {
5693 if ((type
== DeviceType
.VT220
)
5694 || (type
== DeviceType
.XTERM
)) {
5696 if ((collectBuffer
.length() == 1)
5697 && (collectBuffer
.charAt(0) == '(')) {
5699 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5701 if ((collectBuffer
.length() == 1)
5702 && (collectBuffer
.charAt(0) == ')')) {
5704 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5706 if ((collectBuffer
.length() == 1)
5707 && (collectBuffer
.charAt(0) == '*')) {
5709 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5711 if ((collectBuffer
.length() == 1)
5712 && (collectBuffer
.charAt(0) == '+')) {
5714 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5722 if ((type
== DeviceType
.VT220
)
5723 || (type
== DeviceType
.XTERM
)) {
5725 if ((collectBuffer
.length() == 1)
5726 && (collectBuffer
.charAt(0) == '(')) {
5728 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5730 if ((collectBuffer
.length() == 1)
5731 && (collectBuffer
.charAt(0) == ')')) {
5733 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5735 if ((collectBuffer
.length() == 1)
5736 && (collectBuffer
.charAt(0) == '*')) {
5738 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5740 if ((collectBuffer
.length() == 1)
5741 && (collectBuffer
.charAt(0) == '+')) {
5743 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5754 if ((type
== DeviceType
.VT220
)
5755 || (type
== DeviceType
.XTERM
)) {
5757 if ((collectBuffer
.length() == 1)
5758 && (collectBuffer
.charAt(0) == '(')) {
5760 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5762 if ((collectBuffer
.length() == 1)
5763 && (collectBuffer
.charAt(0) == ')')) {
5765 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5767 if ((collectBuffer
.length() == 1)
5768 && (collectBuffer
.charAt(0) == '*')) {
5770 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5772 if ((collectBuffer
.length() == 1)
5773 && (collectBuffer
.charAt(0) == '+')) {
5775 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5780 if ((type
== DeviceType
.VT220
)
5781 || (type
== DeviceType
.XTERM
)) {
5783 if ((collectBuffer
.length() == 1)
5784 && (collectBuffer
.charAt(0) == '(')) {
5786 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5788 if ((collectBuffer
.length() == 1)
5789 && (collectBuffer
.charAt(0) == ')')) {
5791 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5793 if ((collectBuffer
.length() == 1)
5794 && (collectBuffer
.charAt(0) == '*')) {
5796 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5798 if ((collectBuffer
.length() == 1)
5799 && (collectBuffer
.charAt(0) == '+')) {
5801 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5813 if ((type
== DeviceType
.VT220
)
5814 || (type
== DeviceType
.XTERM
)) {
5816 if ((collectBuffer
.length() == 1)
5817 && (collectBuffer
.charAt(0) == '(')) {
5819 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5821 if ((collectBuffer
.length() == 1)
5822 && (collectBuffer
.charAt(0) == ')')) {
5824 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5826 if ((collectBuffer
.length() == 1)
5827 && (collectBuffer
.charAt(0) == '*')) {
5829 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5831 if ((collectBuffer
.length() == 1)
5832 && (collectBuffer
.charAt(0) == '+')) {
5834 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5839 if ((type
== DeviceType
.VT220
)
5840 || (type
== DeviceType
.XTERM
)) {
5842 if ((collectBuffer
.length() == 1)
5843 && (collectBuffer
.charAt(0) == '(')) {
5845 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5847 if ((collectBuffer
.length() == 1)
5848 && (collectBuffer
.charAt(0) == ')')) {
5850 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5852 if ((collectBuffer
.length() == 1)
5853 && (collectBuffer
.charAt(0) == '*')) {
5855 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5857 if ((collectBuffer
.length() == 1)
5858 && (collectBuffer
.charAt(0) == '+')) {
5860 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5907 // 0x9C goes to GROUND
5915 // 00-17, 19, 1C-1F --> execute
5917 handleControlChar((char) ch
);
5920 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5921 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5923 scanState
= ScanState
.CSI_INTERMEDIATE
;
5926 // 30-39, 3B --> param, then switch to CSI_PARAM
5927 if ((ch
>= '0') && (ch
<= '9')) {
5929 scanState
= ScanState
.CSI_PARAM
;
5933 scanState
= ScanState
.CSI_PARAM
;
5936 // 3C-3F --> collect, then switch to CSI_PARAM
5937 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5939 scanState
= ScanState
.CSI_PARAM
;
5942 // 40-7E --> dispatch, then switch to GROUND
5943 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5946 // ICH - Insert character
5954 // CUD - Cursor down
5958 // CUF - Cursor forward
5962 // CUB - Cursor backward
5966 // CNL - Cursor down and to column 1
5967 if (type
== DeviceType
.XTERM
) {
5972 // CPL - Cursor up and to column 1
5973 if (type
== DeviceType
.XTERM
) {
5978 // CHA - Cursor to column # in current row
5979 if (type
== DeviceType
.XTERM
) {
5984 // CUP - Cursor position
5988 // CHT - Cursor forward X tab stops (default 1)
5989 if (type
== DeviceType
.XTERM
) {
5994 // ED - Erase in display
5998 // EL - Erase in line
6013 // DCH - Delete character
6020 // Scroll up X lines (default 1)
6021 if (type
== DeviceType
.XTERM
) {
6022 boolean xtermPrivateModeFlag
= false;
6023 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6024 if (collectBuffer
.charAt(i
) == '?') {
6025 xtermPrivateModeFlag
= true;
6029 if (xtermPrivateModeFlag
) {
6037 // Scroll down X lines (default 1)
6038 if (type
== DeviceType
.XTERM
) {
6047 if ((type
== DeviceType
.VT220
)
6048 || (type
== DeviceType
.XTERM
)) {
6050 // ECH - Erase character
6057 // CBT - Cursor backward X tab stops (default 1)
6058 if (type
== DeviceType
.XTERM
) {
6069 // HPA - Cursor to column # in current row. Same as CHA
6070 if (type
== DeviceType
.XTERM
) {
6075 // HPR - Cursor right. Same as CUF
6076 if (type
== DeviceType
.XTERM
) {
6081 // REP - Repeat last char X times
6082 if (type
== DeviceType
.XTERM
) {
6087 // DA - Device attributes
6091 // VPA - Cursor to row, current column.
6092 if (type
== DeviceType
.XTERM
) {
6097 // VPR - Cursor down. Same as CUD
6098 if (type
== DeviceType
.XTERM
) {
6103 // HVP - Horizontal and vertical position
6107 // TBC - Tabulation clear
6111 // Sets an ANSI or DEC private toggle
6115 if ((type
== DeviceType
.VT220
)
6116 || (type
== DeviceType
.XTERM
)) {
6118 // Printer functions
6126 // Sets an ANSI or DEC private toggle
6130 // SGR - Select graphics rendition
6134 // DSR - Device status report
6141 // DECLL - Load leds
6145 // DECSTBM - Set top and bottom margins
6149 // Save cursor (ANSI.SYS)
6150 if (type
== DeviceType
.XTERM
) {
6151 savedState
.cursorX
= currentState
.cursorX
;
6152 savedState
.cursorY
= currentState
.cursorY
;
6156 if (type
== DeviceType
.XTERM
) {
6157 // Window operations
6162 // Restore cursor (ANSI.SYS)
6163 if (type
== DeviceType
.XTERM
) {
6164 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
6171 // DECREQTPARM - Request terminal parameters
6187 // 0x9C goes to GROUND
6192 // 0x3A goes to CSI_IGNORE
6194 scanState
= ScanState
.CSI_IGNORE
;
6199 // 00-17, 19, 1C-1F --> execute
6201 handleControlChar((char) ch
);
6204 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6205 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6207 scanState
= ScanState
.CSI_INTERMEDIATE
;
6210 // 30-39, 3B --> param
6211 if ((ch
>= '0') && (ch
<= '9')) {
6218 // 0x3A goes to CSI_IGNORE
6220 scanState
= ScanState
.CSI_IGNORE
;
6222 // 0x3C-3F goes to CSI_IGNORE
6223 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6224 scanState
= ScanState
.CSI_IGNORE
;
6227 // 40-7E --> dispatch, then switch to GROUND
6228 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6231 // ICH - Insert character
6239 // CUD - Cursor down
6243 // CUF - Cursor forward
6247 // CUB - Cursor backward
6251 // CNL - Cursor down and to column 1
6252 if (type
== DeviceType
.XTERM
) {
6257 // CPL - Cursor up and to column 1
6258 if (type
== DeviceType
.XTERM
) {
6263 // CHA - Cursor to column # in current row
6264 if (type
== DeviceType
.XTERM
) {
6269 // CUP - Cursor position
6273 // CHT - Cursor forward X tab stops (default 1)
6274 if (type
== DeviceType
.XTERM
) {
6279 // ED - Erase in display
6283 // EL - Erase in line
6298 // DCH - Delete character
6305 // Scroll up X lines (default 1)
6306 if (type
== DeviceType
.XTERM
) {
6307 boolean xtermPrivateModeFlag
= false;
6308 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6309 if (collectBuffer
.charAt(i
) == '?') {
6310 xtermPrivateModeFlag
= true;
6314 if (xtermPrivateModeFlag
) {
6322 // Scroll down X lines (default 1)
6323 if (type
== DeviceType
.XTERM
) {
6332 if ((type
== DeviceType
.VT220
)
6333 || (type
== DeviceType
.XTERM
)) {
6335 // ECH - Erase character
6342 // CBT - Cursor backward X tab stops (default 1)
6343 if (type
== DeviceType
.XTERM
) {
6354 // HPA - Cursor to column # in current row. Same as CHA
6355 if (type
== DeviceType
.XTERM
) {
6360 // HPR - Cursor right. Same as CUF
6361 if (type
== DeviceType
.XTERM
) {
6366 // REP - Repeat last char X times
6367 if (type
== DeviceType
.XTERM
) {
6372 // DA - Device attributes
6376 // VPA - Cursor to row, current column.
6377 if (type
== DeviceType
.XTERM
) {
6382 // VPR - Cursor down. Same as CUD
6383 if (type
== DeviceType
.XTERM
) {
6388 // HVP - Horizontal and vertical position
6392 // TBC - Tabulation clear
6396 // Sets an ANSI or DEC private toggle
6400 if ((type
== DeviceType
.VT220
)
6401 || (type
== DeviceType
.XTERM
)) {
6403 // Printer functions
6411 // Sets an ANSI or DEC private toggle
6415 // SGR - Select graphics rendition
6419 // DSR - Device status report
6426 // DECLL - Load leds
6430 // DECSTBM - Set top and bottom margins
6436 if (type
== DeviceType
.XTERM
) {
6437 // Window operations
6446 // DECREQTPARM - Request terminal parameters
6463 case CSI_INTERMEDIATE
:
6464 // 00-17, 19, 1C-1F --> execute
6466 handleControlChar((char) ch
);
6469 // 20-2F --> collect
6470 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6474 // 0x30-3F goes to CSI_IGNORE
6475 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6476 scanState
= ScanState
.CSI_IGNORE
;
6479 // 40-7E --> dispatch, then switch to GROUND
6480 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6532 if (((type
== DeviceType
.VT220
)
6533 || (type
== DeviceType
.XTERM
))
6534 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6536 // DECSCL - compatibility level
6539 if ((type
== DeviceType
.XTERM
)
6540 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6542 // DECSTR - Soft terminal reset
6547 if (((type
== DeviceType
.VT220
)
6548 || (type
== DeviceType
.XTERM
))
6549 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6577 // 00-17, 19, 1C-1F --> execute
6579 handleControlChar((char) ch
);
6582 // 20-2F --> collect
6583 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6587 // 40-7E --> ignore, then switch to GROUND
6588 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6592 // 20-3F, 7F --> ignore
6598 // 0x9C goes to GROUND
6603 // 0x1B 0x5C goes to GROUND
6608 if ((collectBuffer
.length() > 0)
6609 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6615 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6616 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6618 scanState
= ScanState
.DCS_INTERMEDIATE
;
6621 // 30-39, 3B --> param, then switch to DCS_PARAM
6622 if ((ch
>= '0') && (ch
<= '9')) {
6624 scanState
= ScanState
.DCS_PARAM
;
6628 scanState
= ScanState
.DCS_PARAM
;
6631 // 3C-3F --> collect, then switch to DCS_PARAM
6632 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6634 scanState
= ScanState
.DCS_PARAM
;
6637 // 00-17, 19, 1C-1F, 7F --> ignore
6639 // 0x3A goes to DCS_IGNORE
6641 scanState
= ScanState
.DCS_IGNORE
;
6644 // 0x71 goes to DCS_SIXEL
6646 sixelParseBuffer
= new StringBuilder();
6647 scanState
= ScanState
.DCS_SIXEL
;
6648 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6649 // 0x40-7E goes to DCS_PASSTHROUGH
6650 scanState
= ScanState
.DCS_PASSTHROUGH
;
6654 case DCS_INTERMEDIATE
:
6656 // 0x9C goes to GROUND
6661 // 0x1B 0x5C goes to GROUND
6666 if ((collectBuffer
.length() > 0)
6667 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6673 // 0x30-3F goes to DCS_IGNORE
6674 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6675 scanState
= ScanState
.DCS_IGNORE
;
6678 // 0x40-7E goes to DCS_PASSTHROUGH
6679 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6680 scanState
= ScanState
.DCS_PASSTHROUGH
;
6683 // 00-17, 19, 1C-1F, 7F --> ignore
6688 // 0x9C goes to GROUND
6693 // 0x1B 0x5C goes to GROUND
6698 if ((collectBuffer
.length() > 0)
6699 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6705 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6706 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6708 scanState
= ScanState
.DCS_INTERMEDIATE
;
6711 // 30-39, 3B --> param
6712 if ((ch
>= '0') && (ch
<= '9')) {
6719 // 00-17, 19, 1C-1F, 7F --> ignore
6721 // 0x3A, 3C-3F goes to DCS_IGNORE
6723 scanState
= ScanState
.DCS_IGNORE
;
6725 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6726 scanState
= ScanState
.DCS_IGNORE
;
6729 // 0x71 goes to DCS_SIXEL
6731 sixelParseBuffer
= new StringBuilder();
6732 scanState
= ScanState
.DCS_SIXEL
;
6733 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6734 // 0x40-7E goes to DCS_PASSTHROUGH
6735 scanState
= ScanState
.DCS_PASSTHROUGH
;
6739 case DCS_PASSTHROUGH
:
6740 // 0x9C goes to GROUND
6745 // 0x1B 0x5C goes to GROUND
6750 if ((collectBuffer
.length() > 0)
6751 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6757 // 00-17, 19, 1C-1F, 20-7E --> put
6759 // We ignore all DCS except sixel.
6763 // We ignore all DCS except sixel.
6766 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6767 // We ignore all DCS except sixel.
6770 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6771 // We ignore all DCS except sixel.
6780 // 00-17, 19, 1C-1F, 20-7F --> ignore
6782 // 0x9C goes to GROUND
6790 // 0x9C goes to GROUND
6796 // 0x1B 0x5C goes to GROUND
6801 if ((collectBuffer
.length() > 0)
6802 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6809 // 00-17, 19, 1C-1F, 20-7E --> put
6811 sixelParseBuffer
.append((char) ch
);
6815 sixelParseBuffer
.append((char) ch
);
6818 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6819 sixelParseBuffer
.append((char) ch
);
6822 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6823 sixelParseBuffer
.append((char) ch
);
6831 case SOSPMAPC_STRING
:
6832 // 00-17, 19, 1C-1F, 20-7F --> ignore
6834 // Special case for Jexer: PM can pass one control character
6839 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6843 // 0x9C goes to GROUND
6851 // Special case for Xterm: OSC can pass control characters
6852 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
6856 // 00-17, 19, 1C-1F --> ignore
6858 // 20-7F --> osc_put
6859 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6863 // 0x9C goes to GROUND
6870 case VT52_DIRECT_CURSOR_ADDRESS
:
6871 // This is a special case for the VT52 sequence "ESC Y l c"
6872 if (collectBuffer
.length() == 0) {
6874 } else if (collectBuffer
.length() == 1) {
6875 // We've got the two characters, one in the buffer and the
6877 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
6886 * Expose current cursor X to outside world.
6888 * @return current cursor X
6890 public final int getCursorX() {
6891 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6892 return currentState
.cursorX
* 2;
6894 return currentState
.cursorX
;
6898 * Expose current cursor Y to outside world.
6900 * @return current cursor Y
6902 public final int getCursorY() {
6903 return currentState
.cursorY
;
6907 * Returns true if this terminal has requested the mouse pointer be
6910 * @return true if this terminal has requested the mouse pointer be
6913 public final boolean hasHiddenMousePointer() {
6914 return hideMousePointer
;
6918 * Get the mouse protocol.
6920 * @return MouseProtocol.OFF, MouseProtocol.X10, etc.
6922 public MouseProtocol
getMouseProtocol() {
6923 return mouseProtocol
;
6926 // ------------------------------------------------------------------------
6927 // Sixel support ----------------------------------------------------------
6928 // ------------------------------------------------------------------------
6931 * Set the width of a character cell in pixels.
6933 * @param textWidth the width in pixels of a character cell
6935 public void setTextWidth(final int textWidth
) {
6936 this.textWidth
= textWidth
;
6940 * Set the height of a character cell in pixels.
6942 * @param textHeight the height in pixels of a character cell
6944 public void setTextHeight(final int textHeight
) {
6945 this.textHeight
= textHeight
;
6949 * Parse a sixel string into a bitmap image, and overlay that image onto
6952 private void parseSixel() {
6955 System.err.println("parseSixel(): '" + sixelParseBuffer.toString()
6959 Sixel sixel
= new Sixel(sixelParseBuffer
.toString());
6960 BufferedImage image
= sixel
.getImage();
6962 // System.err.println("parseSixel(): image " + image);
6964 if (image
== null) {
6965 // Sixel data was malformed in some way, bail out.
6972 * Break up the image into text cell sized pieces as a new array of
6975 * Note original column position x0.
6979 * 1. Advance (printCharacter(' ')) for horizontal increment, or
6980 * index (linefeed() + cursorPosition(y, x0)) for vertical
6983 * 2. Set (x, y) cell image data.
6985 * 3. For the right and bottom edges:
6987 * a. Render the text to pixels using Terminus font.
6989 * b. Blit the image on top of the text, using alpha channel.
6991 int cellColumns
= image
.getWidth() / textWidth
;
6992 if (cellColumns
* textWidth
< image
.getWidth()) {
6995 int cellRows
= image
.getHeight() / textHeight
;
6996 if (cellRows
* textHeight
< image
.getHeight()) {
7000 // Break the image up into an array of cells.
7001 Cell
[][] cells
= new Cell
[cellColumns
][cellRows
];
7003 for (int x
= 0; x
< cellColumns
; x
++) {
7004 for (int y
= 0; y
< cellRows
; y
++) {
7006 int width
= textWidth
;
7007 if ((x
+ 1) * textWidth
> image
.getWidth()) {
7008 width
= image
.getWidth() - (x
* textWidth
);
7010 int height
= textHeight
;
7011 if ((y
+ 1) * textHeight
> image
.getHeight()) {
7012 height
= image
.getHeight() - (y
* textHeight
);
7015 Cell cell
= new Cell();
7016 cell
.setImage(image
.getSubimage(x
* textWidth
,
7017 y
* textHeight
, width
, height
));
7023 int x0
= currentState
.cursorX
;
7024 for (int y
= 0; y
< cellRows
; y
++) {
7025 for (int x
= 0; x
< cellColumns
; x
++) {
7026 printCharacter(' ');
7027 cursorLeft(1, false);
7028 if ((x
== cellColumns
- 1) || (y
== cellRows
- 1)) {
7029 // TODO: render text of current cell first, then image
7030 // over it. For now, just copy the cell.
7031 DisplayLine line
= display
.get(currentState
.cursorY
);
7032 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
7034 // Copy the image cell into the display.
7035 DisplayLine line
= display
.get(currentState
.cursorY
);
7036 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
7038 cursorRight(1, false);
7041 cursorPosition(currentState
.cursorY
, x0
);
7047 * Draw the left and right cells of a two-cell-wide (full-width) glyph.
7049 * @param leftX the x position to draw the left half to
7050 * @param leftY the y position to draw the left half to
7051 * @param rightX the x position to draw the right half to
7052 * @param rightY the y position to draw the right half to
7053 * @param ch the character to draw
7055 private void drawHalves(final int leftX
, final int leftY
,
7056 final int rightX
, final int rightY
, final int ch
) {
7058 // System.err.println("drawHalves(): " + Integer.toHexString(ch));
7060 if (lastTextHeight
!= textHeight
) {
7061 glyphMaker
= GlyphMaker
.getInstance(textHeight
);
7062 lastTextHeight
= textHeight
;
7065 Cell cell
= new Cell(ch
, currentState
.attr
);
7066 BufferedImage image
= glyphMaker
.getImage(cell
, textWidth
* 2,
7068 BufferedImage leftImage
= image
.getSubimage(0, 0, textWidth
,
7070 BufferedImage rightImage
= image
.getSubimage(textWidth
, 0, textWidth
,
7073 Cell left
= new Cell(cell
);
7074 left
.setImage(leftImage
);
7075 left
.setWidth(Cell
.Width
.LEFT
);
7076 display
.get(leftY
).replace(leftX
, left
);
7078 Cell right
= new Cell(cell
);
7079 right
.setImage(rightImage
);
7080 right
.setWidth(Cell
.Width
.RIGHT
);
7081 display
.get(rightY
).replace(rightX
, right
);