2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.tterminal
;
31 import java
.awt
.Graphics
;
32 import java
.awt
.image
.BufferedImage
;
33 import java
.io
.BufferedInputStream
;
34 import java
.io
.BufferedOutputStream
;
35 import java
.io
.ByteArrayInputStream
;
36 import java
.io
.CharArrayWriter
;
37 import java
.io
.InputStream
;
38 import java
.io
.InputStreamReader
;
39 import java
.io
.IOException
;
40 import java
.io
.OutputStream
;
41 import java
.io
.OutputStreamWriter
;
42 import java
.io
.PrintWriter
;
43 import java
.io
.Reader
;
44 import java
.io
.UnsupportedEncodingException
;
45 import java
.io
.Writer
;
46 import java
.util
.ArrayList
;
47 import java
.util
.Collections
;
48 import java
.util
.HashMap
;
49 import java
.util
.List
;
50 import javax
.imageio
.ImageIO
;
52 import jexer
.TKeypress
;
53 import jexer
.backend
.GlyphMaker
;
54 import jexer
.bits
.Color
;
55 import jexer
.bits
.Cell
;
56 import jexer
.bits
.CellAttributes
;
57 import jexer
.bits
.StringUtils
;
58 import jexer
.event
.TInputEvent
;
59 import jexer
.event
.TKeypressEvent
;
60 import jexer
.event
.TMouseEvent
;
61 import jexer
.io
.ReadTimeoutException
;
62 import jexer
.io
.TimeoutInputStream
;
63 import static jexer
.TKeypress
.*;
66 * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
67 * including a scrollback buffer.
70 * It currently implements VT100, VT102, VT220, and XTERM with the following
74 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
75 * failure with XTERM. This is due to vttest failing to decode the UTF-8
79 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
80 * from VT100 are not supported.
83 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
84 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
85 * because DECSCL is not supported, it will fail the last part of the
86 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
89 * - Numeric/application keys from the number pad are not supported because
90 * they are not exposed from the TKeypress API.
93 * - VT52 HOLD SCREEN mode is not supported.
96 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
97 * numerators) are not rendered correctly.
100 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
102 public class ECMA48
implements Runnable
{
104 // ------------------------------------------------------------------------
105 // Constants --------------------------------------------------------------
106 // ------------------------------------------------------------------------
109 * The emulator can emulate several kinds of terminals.
111 public enum DeviceType
{
113 * DEC VT100 but also including the three VT102 functions.
134 * Parser character scan states.
136 private enum ScanState
{
152 VT52_DIRECT_CURSOR_ADDRESS
156 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
157 * can't really use it in keypress() because we do not see number pad
158 * events from TKeypress.
160 private enum KeypadMode
{
166 * Arrow keys can emit three different sequences (DECCKM or VT52
169 private enum ArrowKeyMode
{
176 * Available character sets for GL, GR, G0, G1, G2, G3.
178 private enum CharacterSet
{
199 * Single-shift states used by the C1 control characters SS2 (0x8E) and
202 private enum Singleshift
{
209 * VT220+ lockshift states.
211 private enum LockshiftMode
{
221 * XTERM mouse reporting protocols.
223 public enum MouseProtocol
{
232 * XTERM mouse reporting encodings.
234 private enum MouseEncoding
{
240 // ------------------------------------------------------------------------
241 // Variables --------------------------------------------------------------
242 // ------------------------------------------------------------------------
245 * The enclosing listening object.
247 private DisplayListener displayListener
;
250 * When true, the reader thread is expected to exit.
252 private volatile boolean stopReaderThread
= false;
257 private Thread readerThread
= null;
260 * The type of emulator to be.
262 private final DeviceType type
;
265 * The scrollback buffer characters + attributes.
267 private volatile ArrayList
<DisplayLine
> scrollback
;
270 * The raw display buffer characters + attributes.
272 private volatile ArrayList
<DisplayLine
> display
;
275 * The maximum number of lines in the scrollback buffer.
277 private int scrollbackMax
= 10000;
280 * The terminal's input. For type == XTERM, this is an InputStreamReader
281 * with UTF-8 encoding.
283 private Reader input
;
286 * The terminal's raw InputStream. This is used for type != XTERM.
288 private volatile TimeoutInputStream inputStream
;
291 * The terminal's output. For type == XTERM, this wraps an
292 * OutputStreamWriter with UTF-8 encoding.
294 private Writer output
;
297 * The terminal's raw OutputStream. This is used for type != XTERM.
299 private OutputStream outputStream
;
302 * Current scanning state.
304 private ScanState scanState
;
307 * Which mouse protocol is active.
309 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
312 * Which mouse encoding is active.
314 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
317 * A terminal may request that the mouse pointer be hidden using a
318 * Privacy Message containing either "hideMousePointer" or
319 * "showMousePointer". This is currently only used within Jexer by
320 * TTerminalWindow so that only the bottom-most instance of nested
321 * Jexer's draws the mouse within its application window.
323 private boolean hideMousePointer
= false;
326 * Physical display width. We start at 80x24, but the user can resize us
329 private int width
= 80;
332 * Physical display height. We start at 80x24, but the user can resize
335 private int height
= 24;
338 * Top margin of the scrolling region.
340 private int scrollRegionTop
= 0;
343 * Bottom margin of the scrolling region.
345 private int scrollRegionBottom
= height
- 1;
348 * Right margin column number. This can be selected by the remote side
349 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
351 private int rightMargin
= 79;
354 * Last character printed.
359 * VT100-style line wrapping: a character is placed in column 80 (or
360 * 132), but the line does NOT wrap until another character is written to
361 * column 1 of the next line, after which the cursor moves to column 2.
363 private boolean wrapLineFlag
= false;
366 * VT220 single shift flag.
368 private Singleshift singleshift
= Singleshift
.NONE
;
371 * true = insert characters, false = overwrite.
373 private boolean insertMode
= false;
376 * VT52 mode as selected by DECANM. True means VT52, false means
377 * ANSI. Default is ANSI.
379 private boolean vt52Mode
= false;
382 * Visible cursor (DECTCEM).
384 private boolean cursorVisible
= true;
387 * Screen title as set by the xterm OSC sequence. Lots of applications
388 * send a screenTitle regardless of whether it is an xterm client or not.
390 private String screenTitle
= "";
393 * Parameter characters being collected.
395 private List
<Integer
> csiParams
;
398 * Non-csi collect buffer.
400 private StringBuilder collectBuffer
= new StringBuilder(128);
403 * When true, use the G1 character set.
405 private boolean shiftOut
= false;
408 * Horizontal tab stop locations.
410 private List
<Integer
> tabStops
;
413 * S8C1T. True means 8bit controls, false means 7bit controls.
415 private boolean s8c1t
= false;
418 * Printer mode. True means send all output to printer, which discards
421 private boolean printerControllerMode
= false;
424 * LMN line mode. If true, linefeed() puts the cursor on the first
425 * column of the next line. If false, linefeed() puts the cursor one
426 * line down on the current line. The default is false.
428 private boolean newLineMode
= false;
431 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
433 private ArrowKeyMode arrowKeyMode
;
436 * Whether number pad keys send VT100 or VT52, application or numeric
439 @SuppressWarnings("unused")
440 private KeypadMode keypadMode
;
443 * When true, the terminal is in 132-column mode (DECCOLM).
445 private boolean columns132
= false;
448 * true = reverse video. Set by DECSCNM.
450 private boolean reverseVideo
= false;
453 * false = echo characters locally.
455 private boolean fullDuplex
= true;
458 * The current terminal state.
460 private SaveableState currentState
;
463 * The last saved terminal state.
465 private SaveableState savedState
;
468 * The 88- or 256-color support RGB colors.
470 private List
<Integer
> colors88
;
473 * Sixel collection buffer.
475 private StringBuilder sixelParseBuffer
= new StringBuilder(2048);
478 * Sixel shared palette.
480 private HashMap
<Integer
, java
.awt
.Color
> sixelPalette
;
483 * The width of a character cell in pixels.
485 private int textWidth
= 16;
488 * The height of a character cell in pixels.
490 private int textHeight
= 20;
493 * The last used height of a character cell in pixels, only used for
496 private int lastTextHeight
= -1;
499 * The glyph drawer for full-width chars.
501 private GlyphMaker glyphMaker
= null;
504 * Input queue for keystrokes and mouse events to send to the remote
507 private ArrayList
<TInputEvent
> userQueue
= new ArrayList
<TInputEvent
>();
510 * Number of bytes/characters passed to consume().
512 private long readCount
= 0;
515 * DECSC/DECRC save/restore a subset of the total state. This class
516 * encapsulates those specific flags/modes.
518 private class SaveableState
{
521 * When true, cursor positions are relative to the scrolling region.
523 public boolean originMode
= false;
526 * The current editing X position.
528 public int cursorX
= 0;
531 * The current editing Y position.
533 public int cursorY
= 0;
536 * Which character set is currently selected in G0.
538 public CharacterSet g0Charset
= CharacterSet
.US
;
541 * Which character set is currently selected in G1.
543 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
546 * Which character set is currently selected in G2.
548 public CharacterSet g2Charset
= CharacterSet
.US
;
551 * Which character set is currently selected in G3.
553 public CharacterSet g3Charset
= CharacterSet
.US
;
556 * Which character set is currently selected in GR.
558 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
561 * The current drawing attributes.
563 public CellAttributes attr
;
568 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
573 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
578 public boolean lineWrap
= true;
583 public void reset() {
587 g0Charset
= CharacterSet
.US
;
588 g1Charset
= CharacterSet
.DRAWING
;
589 g2Charset
= CharacterSet
.US
;
590 g3Charset
= CharacterSet
.US
;
591 grCharset
= CharacterSet
.DRAWING
;
592 attr
= new CellAttributes();
593 glLockshift
= LockshiftMode
.NONE
;
594 grLockshift
= LockshiftMode
.NONE
;
599 * Copy attributes from another instance.
601 * @param that the other instance to match
603 public void setTo(final SaveableState that
) {
604 this.originMode
= that
.originMode
;
605 this.cursorX
= that
.cursorX
;
606 this.cursorY
= that
.cursorY
;
607 this.g0Charset
= that
.g0Charset
;
608 this.g1Charset
= that
.g1Charset
;
609 this.g2Charset
= that
.g2Charset
;
610 this.g3Charset
= that
.g3Charset
;
611 this.grCharset
= that
.grCharset
;
612 this.attr
= new CellAttributes();
613 this.attr
.setTo(that
.attr
);
614 this.glLockshift
= that
.glLockshift
;
615 this.grLockshift
= that
.grLockshift
;
616 this.lineWrap
= that
.lineWrap
;
620 * Public constructor.
622 public SaveableState() {
627 // ------------------------------------------------------------------------
628 // Constructors -----------------------------------------------------------
629 // ------------------------------------------------------------------------
632 * Public constructor.
634 * @param type one of the DeviceType constants to select VT100, VT102,
636 * @param inputStream an InputStream connected to the remote side. For
637 * type == XTERM, inputStream is converted to a Reader with UTF-8
639 * @param outputStream an OutputStream connected to the remote user. For
640 * type == XTERM, outputStream is converted to a Writer with UTF-8
642 * @param displayListener a callback to the outer display, or null for
643 * default VT100 behavior
644 * @throws UnsupportedEncodingException if an exception is thrown when
645 * creating the InputStreamReader
647 public ECMA48(final DeviceType type
, final InputStream inputStream
,
648 final OutputStream outputStream
, final DisplayListener displayListener
)
649 throws UnsupportedEncodingException
{
651 assert (inputStream
!= null);
652 assert (outputStream
!= null);
654 csiParams
= new ArrayList
<Integer
>();
655 tabStops
= new ArrayList
<Integer
>();
656 scrollback
= new ArrayList
<DisplayLine
>();
657 display
= new ArrayList
<DisplayLine
>();
660 if (inputStream
instanceof TimeoutInputStream
) {
661 this.inputStream
= (TimeoutInputStream
)inputStream
;
663 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
665 if (type
== DeviceType
.XTERM
) {
666 this.input
= new InputStreamReader(new BufferedInputStream(
667 this.inputStream
, 1024 * 128), "UTF-8");
668 this.output
= new OutputStreamWriter(new
669 BufferedOutputStream(outputStream
), "UTF-8");
670 this.outputStream
= null;
673 this.outputStream
= new BufferedOutputStream(outputStream
);
675 this.displayListener
= displayListener
;
678 for (int i
= 0; i
< height
; i
++) {
679 display
.add(new DisplayLine(currentState
.attr
));
681 assert (currentState
.cursorY
< height
);
682 assert (currentState
.cursorX
< width
);
684 // Spin up the input reader
685 readerThread
= new Thread(this);
686 readerThread
.start();
689 // ------------------------------------------------------------------------
690 // Runnable ---------------------------------------------------------------
691 // ------------------------------------------------------------------------
694 * Read function runs on a separate thread.
696 public final void run() {
697 boolean utf8
= false;
698 boolean done
= false;
700 if (type
== DeviceType
.XTERM
) {
704 // available() will often return > 1, so we need to read in chunks to
706 char [] readBufferUTF8
= null;
707 byte [] readBuffer
= null;
709 readBufferUTF8
= new char[2048];
711 readBuffer
= new byte[2048];
714 while (!done
&& !stopReaderThread
) {
715 synchronized (userQueue
) {
716 while (userQueue
.size() > 0) {
717 handleUserEvent(userQueue
.remove(0));
722 int n
= inputStream
.available();
724 // System.err.printf("available() %d\n", n); System.err.flush();
726 if (readBufferUTF8
.length
< n
) {
727 // The buffer wasn't big enough, make it huger
728 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
731 readBufferUTF8
= new char[newSizeHalf
* 2];
734 if (readBuffer
.length
< n
) {
735 // The buffer wasn't big enough, make it huger
736 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
737 readBuffer
= new byte[newSizeHalf
* 2];
743 } catch (InterruptedException e
) {
752 rc
= input
.read(readBufferUTF8
, 0,
753 readBufferUTF8
.length
);
755 rc
= inputStream
.read(readBuffer
, 0,
758 } catch (ReadTimeoutException e
) {
762 // System.err.printf("read() %d\n", rc); System.err.flush();
767 // Don't step on UI events
768 synchronized (this) {
770 for (int i
= 0; i
< rc
;) {
771 int ch
= Character
.codePointAt(readBufferUTF8
,
773 i
+= Character
.charCount(ch
);
775 // Special case for VT10x: 7-bit characters
777 if ((type
== DeviceType
.VT100
)
778 || (type
== DeviceType
.VT102
)
786 for (int i
= 0; i
< rc
; i
++) {
787 // Special case for VT10x: 7-bit characters
789 if ((type
== DeviceType
.VT100
)
790 || (type
== DeviceType
.VT102
)
792 consume(readBuffer
[i
] & 0x7F);
794 consume(readBuffer
[i
]);
799 // Permit my enclosing UI to know that I updated.
800 if (displayListener
!= null) {
801 displayListener
.displayChanged();
804 // System.err.println("end while loop"); System.err.flush();
805 } catch (IOException e
) {
808 // This is an unusual case. We want to see the stack trace,
809 // but it is related to the spawned process rather than the
810 // actual UI. We will generate the stack trace, and consume
811 // it as though it was emitted by the shell.
812 CharArrayWriter writer
= new CharArrayWriter();
813 // Send a ST and RIS to clear the emulator state.
815 writer
.write("\033\\\033c");
816 writer
.write("\n-----------------------------------\n");
817 e
.printStackTrace(new PrintWriter(writer
));
818 writer
.write("\n-----------------------------------\n");
819 } catch (IOException e2
) {
822 char [] stackTrace
= writer
.toCharArray();
823 for (int i
= 0; i
< stackTrace
.length
; i
++) {
824 if (stackTrace
[i
] == '\n') {
827 consume(stackTrace
[i
]);
831 } // while ((done == false) && (stopReaderThread == false))
833 // Let the rest of the world know that I am done.
834 stopReaderThread
= true;
837 inputStream
.cancelRead();
840 } catch (IOException e
) {
846 } catch (IOException e
) {
850 // Permit my enclosing UI to know that I updated.
851 if (displayListener
!= null) {
852 displayListener
.displayChanged();
855 // System.err.println("*** run() exiting..."); System.err.flush();
858 // ------------------------------------------------------------------------
859 // ECMA48 -----------------------------------------------------------------
860 // ------------------------------------------------------------------------
863 * Wait for a period of time to get output from the launched process.
865 * @param millis millis to wait for, or 0 to wait forever
866 * @return true if the launched process has emitted something
868 public boolean waitForOutput(final int millis
) {
870 throw new IllegalArgumentException("timeout must be >= 0");
872 int waitedMillis
= millis
;
873 final int pollTimeout
= 5;
875 if (readCount
!= 0) {
878 if ((millis
> 0) && (waitedMillis
< 0)){
882 Thread
.sleep(pollTimeout
);
883 } catch (InterruptedException e
) {
886 waitedMillis
-= pollTimeout
;
891 * Process keyboard and mouse events from the user.
893 * @param event the input event to consume
895 private void handleUserEvent(final TInputEvent event
) {
896 if (event
instanceof TKeypressEvent
) {
897 keypress(((TKeypressEvent
) event
).getKey());
899 if (event
instanceof TMouseEvent
) {
900 mouse((TMouseEvent
) event
);
905 * Add a keyboard and mouse event from the user to the queue.
907 * @param event the input event to consume
909 public void addUserEvent(final TInputEvent event
) {
910 synchronized (userQueue
) {
911 userQueue
.add(event
);
916 * Return the proper primary Device Attributes string.
918 * @return string to send to remote side that is appropriate for the
921 private String
deviceTypeResponse() {
924 // "I am a VT100 with advanced video option" (often VT102)
933 // "I am a VT220" - 7 bit version, with sixel and Jexer image
936 return "\033[?62;1;6;9;4;22;444c";
938 // "I am a VT220" - 8 bit version, with sixel and Jexer image
940 return "\u009b?62;1;6;9;4;22;444c";
942 throw new IllegalArgumentException("Invalid device type: " + type
);
947 * Return the proper TERM environment variable for this device type.
949 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
950 * @return "vt100", "xterm", etc.
952 public static String
deviceTypeTerm(final DeviceType deviceType
) {
953 switch (deviceType
) {
967 throw new IllegalArgumentException("Invalid device type: "
973 * Return the proper LANG for this device type. Only XTERM devices know
974 * about UTF-8, the others are defined by their standard to be either
975 * 7-bit or 8-bit characters only.
977 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
978 * @param baseLang a base language without UTF-8 flag such as "C" or
980 * @return "en_US", "en_US.UTF-8", etc.
982 public static String
deviceTypeLang(final DeviceType deviceType
,
983 final String baseLang
) {
985 switch (deviceType
) {
993 return baseLang
+ ".UTF-8";
996 throw new IllegalArgumentException("Invalid device type: "
1002 * Write a string directly to the remote side.
1004 * @param str string to send
1006 public void writeRemote(final String str
) {
1007 if (stopReaderThread
) {
1008 // Reader hit EOF, bail out now.
1013 // System.err.printf("writeRemote() '%s'\n", str);
1019 if (outputStream
== null) {
1023 outputStream
.flush();
1024 for (int i
= 0; i
< str
.length(); i
++) {
1025 outputStream
.write(str
.charAt(i
));
1027 outputStream
.flush();
1028 } catch (IOException e
) {
1034 if (output
== null) {
1041 } catch (IOException e
) {
1047 throw new IllegalArgumentException("Invalid device type: " + type
);
1052 * Close the input and output streams and stop the reader thread. Note
1053 * that it is safe to call this multiple times.
1055 public final void close() {
1057 // Tell the reader thread to stop looking at input. It will close
1058 // the input streams.
1059 if (stopReaderThread
== false) {
1060 stopReaderThread
= true;
1063 // Now close the output stream.
1068 if (outputStream
!= null) {
1070 outputStream
.close();
1071 } catch (IOException e
) {
1074 outputStream
= null;
1078 if (outputStream
!= null) {
1080 outputStream
.close();
1081 } catch (IOException e
) {
1084 outputStream
= null;
1086 if (output
!= null) {
1089 } catch (IOException e
) {
1096 throw new IllegalArgumentException("Invalid device type: " +
1102 * See if the reader thread is still running.
1104 * @return if true, we are still connected to / reading from the remote
1107 public final boolean isReading() {
1108 return (!stopReaderThread
);
1112 * Obtain a new blank display line for an external user
1113 * (e.g. TTerminalWindow).
1115 * @return new blank line
1117 public final DisplayLine
getBlankDisplayLine() {
1118 return new DisplayLine(currentState
.attr
);
1122 * Get the scrollback buffer.
1124 * @return the scrollback buffer
1126 public final List
<DisplayLine
> getScrollbackBuffer() {
1131 * Get the display buffer.
1133 * @return the display buffer
1135 public final List
<DisplayLine
> getDisplayBuffer() {
1140 * Get the visible display + scrollback buffer, offset by a specified
1141 * number of rows from the bottom.
1143 * @param visibleHeight the total height of the display to show
1144 * @param scrollBottom the number of rows from the bottom to scroll back
1145 * @return a copy of the display + scrollback buffers
1147 public final List
<DisplayLine
> getVisibleDisplay(final int visibleHeight
,
1148 final int scrollBottom
) {
1150 assert (visibleHeight
>= 0);
1151 assert (scrollBottom
>= 0);
1153 int visibleBottom
= scrollback
.size() + display
.size() - scrollBottom
;
1155 List
<DisplayLine
> preceedingBlankLines
= new ArrayList
<DisplayLine
>();
1156 int visibleTop
= visibleBottom
- visibleHeight
;
1157 if (visibleTop
< 0) {
1158 for (int i
= visibleTop
; i
< 0; i
++) {
1159 preceedingBlankLines
.add(getBlankDisplayLine());
1163 assert (visibleTop
>= 0);
1165 List
<DisplayLine
> displayLines
= new ArrayList
<DisplayLine
>();
1166 displayLines
.addAll(scrollback
);
1167 displayLines
.addAll(display
);
1169 List
<DisplayLine
> visibleLines
= new ArrayList
<DisplayLine
>();
1170 visibleLines
.addAll(preceedingBlankLines
);
1171 visibleLines
.addAll(displayLines
.subList(visibleTop
, visibleBottom
));
1173 // Fill in the blank lines on bottom
1174 int bottomBlankLines
= visibleHeight
- visibleLines
.size();
1175 assert (bottomBlankLines
>= 0);
1176 for (int i
= 0; i
< bottomBlankLines
; i
++) {
1177 visibleLines
.add(getBlankDisplayLine());
1180 return copyBuffer(visibleLines
);
1184 * Copy a display buffer.
1186 * @param buffer the buffer to copy
1187 * @return a deep copy of the buffer's data
1189 private List
<DisplayLine
> copyBuffer(final List
<DisplayLine
> buffer
) {
1190 ArrayList
<DisplayLine
> result
= new ArrayList
<DisplayLine
>(buffer
.size());
1191 for (DisplayLine line
: buffer
) {
1192 result
.add(new DisplayLine(line
));
1198 * Get the display width.
1200 * @return the width (usually 80 or 132)
1202 public final int getWidth() {
1207 * Set the display width.
1209 * @param width the new width
1211 public final synchronized void setWidth(final int width
) {
1213 rightMargin
= width
- 1;
1214 if (currentState
.cursorX
>= width
) {
1215 currentState
.cursorX
= width
- 1;
1217 if (savedState
.cursorX
>= width
) {
1218 savedState
.cursorX
= width
- 1;
1223 * Get the display height.
1225 * @return the height (usually 24)
1227 public final int getHeight() {
1232 * Set the display height.
1234 * @param height the new height
1236 public final synchronized void setHeight(final int height
) {
1237 int delta
= height
- this.height
;
1238 this.height
= height
;
1239 scrollRegionBottom
+= delta
;
1240 if ((scrollRegionBottom
< 0) || (scrollRegionTop
> height
- 1)) {
1241 scrollRegionBottom
= height
- 1;
1243 if (scrollRegionTop
>= scrollRegionBottom
) {
1244 scrollRegionTop
= 0;
1246 if (currentState
.cursorY
>= height
) {
1247 currentState
.cursorY
= height
- 1;
1249 if (savedState
.cursorY
>= height
) {
1250 savedState
.cursorY
= height
- 1;
1252 while (display
.size() < height
) {
1253 DisplayLine line
= new DisplayLine(currentState
.attr
);
1254 line
.setReverseColor(reverseVideo
);
1257 while (display
.size() > height
) {
1258 appendScrollbackLine(display
.remove(0));
1263 * Get the maximum number of lines in the scrollback buffer.
1265 * @return the maximum number of lines in the scrollback buffer
1267 public int getScrollbackMax() {
1268 return scrollbackMax
;
1272 * Set the maximum number of lines for the scrollback buffer.
1274 * @param scrollbackMax the maximum number of lines for the scrollback
1277 public final void setScrollbackMax(final int scrollbackMax
) {
1278 this.scrollbackMax
= scrollbackMax
;
1282 * Get visible cursor flag.
1284 * @return if true, the cursor is visible
1286 public final boolean isCursorVisible() {
1287 return cursorVisible
;
1291 * Get the screen title as set by the xterm OSC sequence. Lots of
1292 * applications send a screenTitle regardless of whether it is an xterm
1295 * @return screen title
1297 public final String
getScreenTitle() {
1302 * Get 132 columns value.
1304 * @return if true, the terminal is in 132 column mode
1306 public final boolean isColumns132() {
1311 * Clear the CSI parameters and flags.
1313 private void toGround() {
1315 collectBuffer
.setLength(0);
1316 scanState
= ScanState
.GROUND
;
1320 * Reset the tab stops list.
1322 private void resetTabStops() {
1324 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
1325 tabStops
.add(Integer
.valueOf(i
* 8));
1330 * Reset the 88- or 256-colors.
1332 private void resetColors() {
1333 colors88
= new ArrayList
<Integer
>(256);
1334 for (int i
= 0; i
< 256; i
++) {
1338 // Set default system colors. These match DOS colors.
1339 colors88
.set(0, 0x00000000);
1340 colors88
.set(1, 0x00a80000);
1341 colors88
.set(2, 0x0000a800);
1342 colors88
.set(3, 0x00a85400);
1343 colors88
.set(4, 0x000000a8);
1344 colors88
.set(5, 0x00a800a8);
1345 colors88
.set(6, 0x0000a8a8);
1346 colors88
.set(7, 0x00a8a8a8);
1348 colors88
.set(8, 0x00545454);
1349 colors88
.set(9, 0x00fc5454);
1350 colors88
.set(10, 0x0054fc54);
1351 colors88
.set(11, 0x00fcfc54);
1352 colors88
.set(12, 0x005454fc);
1353 colors88
.set(13, 0x00fc54fc);
1354 colors88
.set(14, 0x0054fcfc);
1355 colors88
.set(15, 0x00fcfcfc);
1357 // These match xterm's default colors from 256colres.h.
1358 colors88
.set(16, 0x000000);
1359 colors88
.set(17, 0x00005f);
1360 colors88
.set(18, 0x000087);
1361 colors88
.set(19, 0x0000af);
1362 colors88
.set(20, 0x0000d7);
1363 colors88
.set(21, 0x0000ff);
1364 colors88
.set(22, 0x005f00);
1365 colors88
.set(23, 0x005f5f);
1366 colors88
.set(24, 0x005f87);
1367 colors88
.set(25, 0x005faf);
1368 colors88
.set(26, 0x005fd7);
1369 colors88
.set(27, 0x005fff);
1370 colors88
.set(28, 0x008700);
1371 colors88
.set(29, 0x00875f);
1372 colors88
.set(30, 0x008787);
1373 colors88
.set(31, 0x0087af);
1374 colors88
.set(32, 0x0087d7);
1375 colors88
.set(33, 0x0087ff);
1376 colors88
.set(34, 0x00af00);
1377 colors88
.set(35, 0x00af5f);
1378 colors88
.set(36, 0x00af87);
1379 colors88
.set(37, 0x00afaf);
1380 colors88
.set(38, 0x00afd7);
1381 colors88
.set(39, 0x00afff);
1382 colors88
.set(40, 0x00d700);
1383 colors88
.set(41, 0x00d75f);
1384 colors88
.set(42, 0x00d787);
1385 colors88
.set(43, 0x00d7af);
1386 colors88
.set(44, 0x00d7d7);
1387 colors88
.set(45, 0x00d7ff);
1388 colors88
.set(46, 0x00ff00);
1389 colors88
.set(47, 0x00ff5f);
1390 colors88
.set(48, 0x00ff87);
1391 colors88
.set(49, 0x00ffaf);
1392 colors88
.set(50, 0x00ffd7);
1393 colors88
.set(51, 0x00ffff);
1394 colors88
.set(52, 0x5f0000);
1395 colors88
.set(53, 0x5f005f);
1396 colors88
.set(54, 0x5f0087);
1397 colors88
.set(55, 0x5f00af);
1398 colors88
.set(56, 0x5f00d7);
1399 colors88
.set(57, 0x5f00ff);
1400 colors88
.set(58, 0x5f5f00);
1401 colors88
.set(59, 0x5f5f5f);
1402 colors88
.set(60, 0x5f5f87);
1403 colors88
.set(61, 0x5f5faf);
1404 colors88
.set(62, 0x5f5fd7);
1405 colors88
.set(63, 0x5f5fff);
1406 colors88
.set(64, 0x5f8700);
1407 colors88
.set(65, 0x5f875f);
1408 colors88
.set(66, 0x5f8787);
1409 colors88
.set(67, 0x5f87af);
1410 colors88
.set(68, 0x5f87d7);
1411 colors88
.set(69, 0x5f87ff);
1412 colors88
.set(70, 0x5faf00);
1413 colors88
.set(71, 0x5faf5f);
1414 colors88
.set(72, 0x5faf87);
1415 colors88
.set(73, 0x5fafaf);
1416 colors88
.set(74, 0x5fafd7);
1417 colors88
.set(75, 0x5fafff);
1418 colors88
.set(76, 0x5fd700);
1419 colors88
.set(77, 0x5fd75f);
1420 colors88
.set(78, 0x5fd787);
1421 colors88
.set(79, 0x5fd7af);
1422 colors88
.set(80, 0x5fd7d7);
1423 colors88
.set(81, 0x5fd7ff);
1424 colors88
.set(82, 0x5fff00);
1425 colors88
.set(83, 0x5fff5f);
1426 colors88
.set(84, 0x5fff87);
1427 colors88
.set(85, 0x5fffaf);
1428 colors88
.set(86, 0x5fffd7);
1429 colors88
.set(87, 0x5fffff);
1430 colors88
.set(88, 0x870000);
1431 colors88
.set(89, 0x87005f);
1432 colors88
.set(90, 0x870087);
1433 colors88
.set(91, 0x8700af);
1434 colors88
.set(92, 0x8700d7);
1435 colors88
.set(93, 0x8700ff);
1436 colors88
.set(94, 0x875f00);
1437 colors88
.set(95, 0x875f5f);
1438 colors88
.set(96, 0x875f87);
1439 colors88
.set(97, 0x875faf);
1440 colors88
.set(98, 0x875fd7);
1441 colors88
.set(99, 0x875fff);
1442 colors88
.set(100, 0x878700);
1443 colors88
.set(101, 0x87875f);
1444 colors88
.set(102, 0x878787);
1445 colors88
.set(103, 0x8787af);
1446 colors88
.set(104, 0x8787d7);
1447 colors88
.set(105, 0x8787ff);
1448 colors88
.set(106, 0x87af00);
1449 colors88
.set(107, 0x87af5f);
1450 colors88
.set(108, 0x87af87);
1451 colors88
.set(109, 0x87afaf);
1452 colors88
.set(110, 0x87afd7);
1453 colors88
.set(111, 0x87afff);
1454 colors88
.set(112, 0x87d700);
1455 colors88
.set(113, 0x87d75f);
1456 colors88
.set(114, 0x87d787);
1457 colors88
.set(115, 0x87d7af);
1458 colors88
.set(116, 0x87d7d7);
1459 colors88
.set(117, 0x87d7ff);
1460 colors88
.set(118, 0x87ff00);
1461 colors88
.set(119, 0x87ff5f);
1462 colors88
.set(120, 0x87ff87);
1463 colors88
.set(121, 0x87ffaf);
1464 colors88
.set(122, 0x87ffd7);
1465 colors88
.set(123, 0x87ffff);
1466 colors88
.set(124, 0xaf0000);
1467 colors88
.set(125, 0xaf005f);
1468 colors88
.set(126, 0xaf0087);
1469 colors88
.set(127, 0xaf00af);
1470 colors88
.set(128, 0xaf00d7);
1471 colors88
.set(129, 0xaf00ff);
1472 colors88
.set(130, 0xaf5f00);
1473 colors88
.set(131, 0xaf5f5f);
1474 colors88
.set(132, 0xaf5f87);
1475 colors88
.set(133, 0xaf5faf);
1476 colors88
.set(134, 0xaf5fd7);
1477 colors88
.set(135, 0xaf5fff);
1478 colors88
.set(136, 0xaf8700);
1479 colors88
.set(137, 0xaf875f);
1480 colors88
.set(138, 0xaf8787);
1481 colors88
.set(139, 0xaf87af);
1482 colors88
.set(140, 0xaf87d7);
1483 colors88
.set(141, 0xaf87ff);
1484 colors88
.set(142, 0xafaf00);
1485 colors88
.set(143, 0xafaf5f);
1486 colors88
.set(144, 0xafaf87);
1487 colors88
.set(145, 0xafafaf);
1488 colors88
.set(146, 0xafafd7);
1489 colors88
.set(147, 0xafafff);
1490 colors88
.set(148, 0xafd700);
1491 colors88
.set(149, 0xafd75f);
1492 colors88
.set(150, 0xafd787);
1493 colors88
.set(151, 0xafd7af);
1494 colors88
.set(152, 0xafd7d7);
1495 colors88
.set(153, 0xafd7ff);
1496 colors88
.set(154, 0xafff00);
1497 colors88
.set(155, 0xafff5f);
1498 colors88
.set(156, 0xafff87);
1499 colors88
.set(157, 0xafffaf);
1500 colors88
.set(158, 0xafffd7);
1501 colors88
.set(159, 0xafffff);
1502 colors88
.set(160, 0xd70000);
1503 colors88
.set(161, 0xd7005f);
1504 colors88
.set(162, 0xd70087);
1505 colors88
.set(163, 0xd700af);
1506 colors88
.set(164, 0xd700d7);
1507 colors88
.set(165, 0xd700ff);
1508 colors88
.set(166, 0xd75f00);
1509 colors88
.set(167, 0xd75f5f);
1510 colors88
.set(168, 0xd75f87);
1511 colors88
.set(169, 0xd75faf);
1512 colors88
.set(170, 0xd75fd7);
1513 colors88
.set(171, 0xd75fff);
1514 colors88
.set(172, 0xd78700);
1515 colors88
.set(173, 0xd7875f);
1516 colors88
.set(174, 0xd78787);
1517 colors88
.set(175, 0xd787af);
1518 colors88
.set(176, 0xd787d7);
1519 colors88
.set(177, 0xd787ff);
1520 colors88
.set(178, 0xd7af00);
1521 colors88
.set(179, 0xd7af5f);
1522 colors88
.set(180, 0xd7af87);
1523 colors88
.set(181, 0xd7afaf);
1524 colors88
.set(182, 0xd7afd7);
1525 colors88
.set(183, 0xd7afff);
1526 colors88
.set(184, 0xd7d700);
1527 colors88
.set(185, 0xd7d75f);
1528 colors88
.set(186, 0xd7d787);
1529 colors88
.set(187, 0xd7d7af);
1530 colors88
.set(188, 0xd7d7d7);
1531 colors88
.set(189, 0xd7d7ff);
1532 colors88
.set(190, 0xd7ff00);
1533 colors88
.set(191, 0xd7ff5f);
1534 colors88
.set(192, 0xd7ff87);
1535 colors88
.set(193, 0xd7ffaf);
1536 colors88
.set(194, 0xd7ffd7);
1537 colors88
.set(195, 0xd7ffff);
1538 colors88
.set(196, 0xff0000);
1539 colors88
.set(197, 0xff005f);
1540 colors88
.set(198, 0xff0087);
1541 colors88
.set(199, 0xff00af);
1542 colors88
.set(200, 0xff00d7);
1543 colors88
.set(201, 0xff00ff);
1544 colors88
.set(202, 0xff5f00);
1545 colors88
.set(203, 0xff5f5f);
1546 colors88
.set(204, 0xff5f87);
1547 colors88
.set(205, 0xff5faf);
1548 colors88
.set(206, 0xff5fd7);
1549 colors88
.set(207, 0xff5fff);
1550 colors88
.set(208, 0xff8700);
1551 colors88
.set(209, 0xff875f);
1552 colors88
.set(210, 0xff8787);
1553 colors88
.set(211, 0xff87af);
1554 colors88
.set(212, 0xff87d7);
1555 colors88
.set(213, 0xff87ff);
1556 colors88
.set(214, 0xffaf00);
1557 colors88
.set(215, 0xffaf5f);
1558 colors88
.set(216, 0xffaf87);
1559 colors88
.set(217, 0xffafaf);
1560 colors88
.set(218, 0xffafd7);
1561 colors88
.set(219, 0xffafff);
1562 colors88
.set(220, 0xffd700);
1563 colors88
.set(221, 0xffd75f);
1564 colors88
.set(222, 0xffd787);
1565 colors88
.set(223, 0xffd7af);
1566 colors88
.set(224, 0xffd7d7);
1567 colors88
.set(225, 0xffd7ff);
1568 colors88
.set(226, 0xffff00);
1569 colors88
.set(227, 0xffff5f);
1570 colors88
.set(228, 0xffff87);
1571 colors88
.set(229, 0xffffaf);
1572 colors88
.set(230, 0xffffd7);
1573 colors88
.set(231, 0xffffff);
1574 colors88
.set(232, 0x080808);
1575 colors88
.set(233, 0x121212);
1576 colors88
.set(234, 0x1c1c1c);
1577 colors88
.set(235, 0x262626);
1578 colors88
.set(236, 0x303030);
1579 colors88
.set(237, 0x3a3a3a);
1580 colors88
.set(238, 0x444444);
1581 colors88
.set(239, 0x4e4e4e);
1582 colors88
.set(240, 0x585858);
1583 colors88
.set(241, 0x626262);
1584 colors88
.set(242, 0x6c6c6c);
1585 colors88
.set(243, 0x767676);
1586 colors88
.set(244, 0x808080);
1587 colors88
.set(245, 0x8a8a8a);
1588 colors88
.set(246, 0x949494);
1589 colors88
.set(247, 0x9e9e9e);
1590 colors88
.set(248, 0xa8a8a8);
1591 colors88
.set(249, 0xb2b2b2);
1592 colors88
.set(250, 0xbcbcbc);
1593 colors88
.set(251, 0xc6c6c6);
1594 colors88
.set(252, 0xd0d0d0);
1595 colors88
.set(253, 0xdadada);
1596 colors88
.set(254, 0xe4e4e4);
1597 colors88
.set(255, 0xeeeeee);
1602 * Get the RGB value of one of the indexed colors.
1604 * @param index the color index
1605 * @return the RGB value
1607 private int get88Color(final int index
) {
1608 // System.err.print("get88Color: " + index);
1609 if ((index
< 0) || (index
> colors88
.size())) {
1610 // System.err.println(" -- UNKNOWN");
1613 // System.err.printf(" %08x\n", colors88.get(index));
1614 return colors88
.get(index
);
1618 * Set one of the indexed colors to a color specification.
1620 * @param index the color index
1621 * @param spec the specification, typically something like "rgb:aa/bb/cc"
1623 private void set88Color(final int index
, final String spec
) {
1624 // System.err.println("set88Color: " + index + " '" + spec + "'");
1626 if ((index
< 0) || (index
> colors88
.size())) {
1629 if (spec
.startsWith("rgb:")) {
1630 String
[] rgbTokens
= spec
.substring(4).split("/");
1631 if (rgbTokens
.length
== 3) {
1633 int rgb
= (Integer
.parseInt(rgbTokens
[0], 16) << 16);
1634 rgb
|= Integer
.parseInt(rgbTokens
[1], 16) << 8;
1635 rgb
|= Integer
.parseInt(rgbTokens
[2], 16);
1636 // System.err.printf(" set to %08x\n", rgb);
1637 colors88
.set(index
, rgb
);
1638 } catch (NumberFormatException e
) {
1645 if (spec
.toLowerCase().equals("black")) {
1646 colors88
.set(index
, 0x00000000);
1647 } else if (spec
.toLowerCase().equals("red")) {
1648 colors88
.set(index
, 0x00a80000);
1649 } else if (spec
.toLowerCase().equals("green")) {
1650 colors88
.set(index
, 0x0000a800);
1651 } else if (spec
.toLowerCase().equals("yellow")) {
1652 colors88
.set(index
, 0x00a85400);
1653 } else if (spec
.toLowerCase().equals("blue")) {
1654 colors88
.set(index
, 0x000000a8);
1655 } else if (spec
.toLowerCase().equals("magenta")) {
1656 colors88
.set(index
, 0x00a800a8);
1657 } else if (spec
.toLowerCase().equals("cyan")) {
1658 colors88
.set(index
, 0x0000a8a8);
1659 } else if (spec
.toLowerCase().equals("white")) {
1660 colors88
.set(index
, 0x00a8a8a8);
1666 * Reset the emulation state.
1668 private void reset() {
1670 currentState
= new SaveableState();
1671 savedState
= new SaveableState();
1672 scanState
= ScanState
.GROUND
;
1673 if (displayListener
!= null) {
1674 width
= displayListener
.getDisplayWidth();
1675 height
= displayListener
.getDisplayHeight();
1680 scrollRegionTop
= 0;
1681 scrollRegionBottom
= height
- 1;
1682 rightMargin
= width
- 1;
1683 newLineMode
= false;
1684 arrowKeyMode
= ArrowKeyMode
.ANSI
;
1685 keypadMode
= KeypadMode
.Numeric
;
1686 wrapLineFlag
= false;
1693 newLineMode
= false;
1694 reverseVideo
= false;
1696 cursorVisible
= true;
1699 singleshift
= Singleshift
.NONE
;
1701 printerControllerMode
= false;
1704 mouseProtocol
= MouseProtocol
.OFF
;
1705 mouseEncoding
= MouseEncoding
.X10
;
1710 // Reset extra colors
1718 * Append a to the scrollback buffer, clearing image data for lines more
1719 * than three screenfuls in.
1721 private void appendScrollbackLine(DisplayLine line
) {
1722 scrollback
.add(line
);
1723 if (scrollback
.size() > height
* 3) {
1724 scrollback
.get(scrollback
.size() - (height
* 3)).clearImages();
1729 * Append a new line to the bottom of the display, adding lines off the
1730 * top to the scrollback buffer.
1732 private void newDisplayLine() {
1733 // Scroll the top line off into the scrollback buffer
1734 appendScrollbackLine(display
.get(0));
1735 while (scrollback
.size() > scrollbackMax
) {
1736 scrollback
.remove(0);
1737 scrollback
.trimToSize();
1740 display
.trimToSize();
1741 DisplayLine line
= new DisplayLine(currentState
.attr
);
1742 line
.setReverseColor(reverseVideo
);
1747 * Wraps the current line.
1749 private void wrapCurrentLine() {
1750 if (currentState
.cursorY
== height
- 1) {
1753 if (currentState
.cursorY
< height
- 1) {
1754 currentState
.cursorY
++;
1756 currentState
.cursorX
= 0;
1760 * Handle a carriage return.
1762 private void carriageReturn() {
1763 currentState
.cursorX
= 0;
1764 wrapLineFlag
= false;
1768 * Reverse the color of the visible display.
1770 private void invertDisplayColors() {
1771 for (DisplayLine line
: display
) {
1772 line
.setReverseColor(!line
.isReverseColor());
1777 * Handle a linefeed.
1779 private void linefeed() {
1780 if (currentState
.cursorY
< scrollRegionBottom
) {
1781 // Increment screen y
1782 currentState
.cursorY
++;
1785 // Screen y does not increment
1788 * Two cases: either we're inside a scrolling region or not. If
1789 * the scrolling region bottom is the bottom of the screen, then
1790 * push the top line into the buffer. Else scroll the scrolling
1793 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1795 // We're at the bottom of the scroll region, AND the scroll
1796 // region is the entire screen.
1802 // We're at the bottom of the scroll region, AND the scroll
1803 // region is NOT the entire screen.
1804 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1809 currentState
.cursorX
= 0;
1811 wrapLineFlag
= false;
1815 * Prints one character to the display buffer.
1817 * @param ch character to display
1819 private void printCharacter(final int ch
) {
1820 int rightMargin
= this.rightMargin
;
1822 if (StringUtils
.width(ch
) == 2) {
1823 // This is a full-width character. Save two spaces, and then
1824 // draw the character as two image halves.
1825 int x0
= currentState
.cursorX
;
1826 int y0
= currentState
.cursorY
;
1827 printCharacter(' ');
1828 printCharacter(' ');
1829 if ((currentState
.cursorX
== x0
+ 2)
1830 && (currentState
.cursorY
== y0
)
1832 // We can draw both halves of the character.
1833 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1834 } else if ((currentState
.cursorX
== x0
+ 1)
1835 && (currentState
.cursorY
== y0
)
1837 // VT100 line wrap behavior: we should be at the right
1838 // margin. We can draw both halves of the character.
1839 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1841 // The character splits across the line. Draw the entire
1842 // character on the new line, giving one more space for it.
1843 x0
= currentState
.cursorX
- 1;
1844 y0
= currentState
.cursorY
;
1845 printCharacter(' ');
1846 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1851 // Check if we have double-width, and if so chop at 40/66 instead of
1853 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1854 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1857 // Check the unusually-complicated line wrapping conditions...
1858 if (currentState
.cursorX
== rightMargin
) {
1860 if (currentState
.lineWrap
== true) {
1862 * This case happens when: the cursor was already on the
1863 * right margin (either through printing or by an explicit
1864 * placement command), and a character was printed.
1866 * The line wraps only when a new character arrives AND the
1867 * cursor is already on the right margin AND has placed a
1868 * character in its cell. Easier to see than to explain.
1870 if (wrapLineFlag
== false) {
1872 * This block marks the case that we are in the margin
1873 * and the first character has been received and printed.
1875 wrapLineFlag
= true;
1878 * This block marks the case that we are in the margin
1879 * and the second character has been received and
1882 wrapLineFlag
= false;
1886 } else if (currentState
.cursorX
<= rightMargin
) {
1888 * This is the normal case: a character came in and was printed
1889 * to the left of the right margin column.
1892 // Turn off VT100 special-case flag
1893 wrapLineFlag
= false;
1896 // "Print" the character
1897 Cell newCell
= new Cell(ch
);
1898 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1899 newCellAttributes
.setTo(currentState
.attr
);
1900 DisplayLine line
= display
.get(currentState
.cursorY
);
1902 if (StringUtils
.width(ch
) == 1) {
1903 // Insert mode special case
1904 if (insertMode
== true) {
1905 line
.insert(currentState
.cursorX
, newCell
);
1907 // Replace an existing character
1908 line
.replace(currentState
.cursorX
, newCell
);
1911 // Increment horizontal
1912 if (wrapLineFlag
== false) {
1913 currentState
.cursorX
++;
1914 if (currentState
.cursorX
> rightMargin
) {
1915 currentState
.cursorX
--;
1922 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1923 * send to the remote side.
1925 * @param mouse mouse event received from the local user
1927 private void mouse(final TMouseEvent mouse
) {
1930 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1931 mouseProtocol, mouseEncoding, mouse);
1934 if (mouseEncoding
== MouseEncoding
.X10
) {
1935 // We will support X10 but only for (160,94) and smaller.
1936 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1941 switch (mouseProtocol
) {
1948 // Only report button presses
1949 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1955 // Only report button presses and releases
1956 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1957 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1965 * Only report button presses, button releases, and motions that
1966 * have a button down (i.e. drag-and-drop).
1968 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1969 if (!mouse
.isMouse1()
1970 && !mouse
.isMouse2()
1971 && !mouse
.isMouse3()
1972 && !mouse
.isMouseWheelUp()
1973 && !mouse
.isMouseWheelDown()
1981 // Report everything
1985 // Now encode the event
1986 StringBuilder sb
= new StringBuilder(6);
1987 if (mouseEncoding
== MouseEncoding
.SGR
) {
1988 sb
.append((char) 0x1B);
1992 if (mouse
.isMouse1()) {
1993 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1998 } else if (mouse
.isMouse2()) {
1999 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
2004 } else if (mouse
.isMouse3()) {
2005 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
2010 } else if (mouse
.isMouseWheelUp()) {
2012 } else if (mouse
.isMouseWheelDown()) {
2015 // This is motion with no buttons down.
2018 if (mouse
.isAlt()) {
2021 if (mouse
.isCtrl()) {
2024 if (mouse
.isShift()) {
2028 sb
.append(String
.format("%d;%d;%d", buttons
, mouse
.getX() + 1,
2031 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
2038 // X10 and UTF8 encodings
2039 sb
.append((char) 0x1B);
2043 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
2044 buttons
= 0x03 + 32;
2045 } else if (mouse
.isMouse1()) {
2046 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
2047 buttons
= 0x00 + 32 + 32;
2049 buttons
= 0x00 + 32;
2051 } else if (mouse
.isMouse2()) {
2052 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
2053 buttons
= 0x01 + 32 + 32;
2055 buttons
= 0x01 + 32;
2057 } else if (mouse
.isMouse3()) {
2058 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
2059 buttons
= 0x02 + 32 + 32;
2061 buttons
= 0x02 + 32;
2063 } else if (mouse
.isMouseWheelUp()) {
2064 buttons
= 0x04 + 64;
2065 } else if (mouse
.isMouseWheelDown()) {
2066 buttons
= 0x05 + 64;
2068 // This is motion with no buttons down.
2069 buttons
= 0x03 + 32;
2071 if (mouse
.isAlt()) {
2074 if (mouse
.isCtrl()) {
2077 if (mouse
.isShift()) {
2081 sb
.append((char) (buttons
& 0xFF));
2082 sb
.append((char) (mouse
.getX() + 33));
2083 sb
.append((char) (mouse
.getY() + 33));
2086 // System.err.printf("Would write: \'%s\'\n", sb.toString());
2087 writeRemote(sb
.toString());
2091 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
2092 * send to the remote side.
2094 * @param keypress keypress received from the local user
2096 private void keypress(final TKeypress keypress
) {
2097 writeRemote(keypressToString(keypress
));
2101 * Build one of the complex xterm keystroke sequences, storing the result in
2102 * xterm_keystroke_buffer.
2104 * @param ss3 the prefix to use based on VT100 state.
2105 * @param first the first character, usually a number.
2106 * @param first the last character, one of the following: ~ A B C D F H
2107 * @param ctrl whether or not ctrl is down
2108 * @param alt whether or not alt is down
2109 * @param shift whether or not shift is down
2110 * @return the buffer with the full key sequence
2112 private String
xtermBuildKeySequence(final String ss3
, final char first
,
2113 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
2115 StringBuilder sb
= new StringBuilder(ss3
);
2116 if ((last
== '~') || (ctrl
== true) || (alt
== true)
2120 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
2122 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
2124 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
2126 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
2128 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
2130 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
2132 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
2137 return sb
.toString();
2141 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
2143 * @param keypress keypress received from the local user
2144 * @return string to transmit to the remote side
2146 @SuppressWarnings("fallthrough")
2147 private String
keypressToString(final TKeypress keypress
) {
2149 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
2151 * If this is a control character, process it like it came from
2154 if (keypress
.getChar() < 0x20) {
2155 handleControlChar((char) keypress
.getChar());
2157 // Local echo for everything else
2158 printCharacter(keypress
.getChar());
2160 if (displayListener
!= null) {
2161 displayListener
.displayChanged();
2165 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
2170 // Handle control characters
2171 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
2172 StringBuilder sb
= new StringBuilder();
2173 int ch
= keypress
.getChar();
2175 sb
.append(Character
.toChars(ch
));
2176 return sb
.toString();
2179 // Handle alt characters
2180 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
2181 StringBuilder sb
= new StringBuilder("\033");
2182 int ch
= keypress
.getChar();
2183 sb
.append(Character
.toChars(ch
));
2184 return sb
.toString();
2187 if (keypress
.equals(kbBackspaceDel
)) {
2200 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
2203 switch (arrowKeyMode
) {
2205 return xtermBuildKeySequence("\033[", '1', 'D',
2206 keypress
.isCtrl(), keypress
.isAlt(),
2207 keypress
.isShift());
2209 return xtermBuildKeySequence("\033", '1', 'D',
2210 keypress
.isCtrl(), keypress
.isAlt(),
2211 keypress
.isShift());
2213 return xtermBuildKeySequence("\033O", '1', 'D',
2214 keypress
.isCtrl(), keypress
.isAlt(),
2215 keypress
.isShift());
2218 switch (arrowKeyMode
) {
2229 if (keypress
.equalsWithoutModifiers(kbRight
)) {
2232 switch (arrowKeyMode
) {
2234 return xtermBuildKeySequence("\033[", '1', 'C',
2235 keypress
.isCtrl(), keypress
.isAlt(),
2236 keypress
.isShift());
2238 return xtermBuildKeySequence("\033", '1', 'C',
2239 keypress
.isCtrl(), keypress
.isAlt(),
2240 keypress
.isShift());
2242 return xtermBuildKeySequence("\033O", '1', 'C',
2243 keypress
.isCtrl(), keypress
.isAlt(),
2244 keypress
.isShift());
2247 switch (arrowKeyMode
) {
2258 if (keypress
.equalsWithoutModifiers(kbUp
)) {
2261 switch (arrowKeyMode
) {
2263 return xtermBuildKeySequence("\033[", '1', 'A',
2264 keypress
.isCtrl(), keypress
.isAlt(),
2265 keypress
.isShift());
2267 return xtermBuildKeySequence("\033", '1', 'A',
2268 keypress
.isCtrl(), keypress
.isAlt(),
2269 keypress
.isShift());
2271 return xtermBuildKeySequence("\033O", '1', 'A',
2272 keypress
.isCtrl(), keypress
.isAlt(),
2273 keypress
.isShift());
2276 switch (arrowKeyMode
) {
2287 if (keypress
.equalsWithoutModifiers(kbDown
)) {
2290 switch (arrowKeyMode
) {
2292 return xtermBuildKeySequence("\033[", '1', 'B',
2293 keypress
.isCtrl(), keypress
.isAlt(),
2294 keypress
.isShift());
2296 return xtermBuildKeySequence("\033", '1', 'B',
2297 keypress
.isCtrl(), keypress
.isAlt(),
2298 keypress
.isShift());
2300 return xtermBuildKeySequence("\033O", '1', 'B',
2301 keypress
.isCtrl(), keypress
.isAlt(),
2302 keypress
.isShift());
2305 switch (arrowKeyMode
) {
2316 if (keypress
.equalsWithoutModifiers(kbHome
)) {
2319 switch (arrowKeyMode
) {
2321 return xtermBuildKeySequence("\033[", '1', 'H',
2322 keypress
.isCtrl(), keypress
.isAlt(),
2323 keypress
.isShift());
2325 return xtermBuildKeySequence("\033", '1', 'H',
2326 keypress
.isCtrl(), keypress
.isAlt(),
2327 keypress
.isShift());
2329 return xtermBuildKeySequence("\033O", '1', 'H',
2330 keypress
.isCtrl(), keypress
.isAlt(),
2331 keypress
.isShift());
2334 switch (arrowKeyMode
) {
2345 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
2348 switch (arrowKeyMode
) {
2350 return xtermBuildKeySequence("\033[", '1', 'F',
2351 keypress
.isCtrl(), keypress
.isAlt(),
2352 keypress
.isShift());
2354 return xtermBuildKeySequence("\033", '1', 'F',
2355 keypress
.isCtrl(), keypress
.isAlt(),
2356 keypress
.isShift());
2358 return xtermBuildKeySequence("\033O", '1', 'F',
2359 keypress
.isCtrl(), keypress
.isAlt(),
2360 keypress
.isShift());
2363 switch (arrowKeyMode
) {
2374 if (keypress
.equals(kbF1
)) {
2382 if (keypress
.equals(kbF2
)) {
2390 if (keypress
.equals(kbF3
)) {
2398 if (keypress
.equals(kbF4
)) {
2406 if (keypress
.equals(kbF5
)) {
2419 if (keypress
.equals(kbF6
)) {
2432 if (keypress
.equals(kbF7
)) {
2445 if (keypress
.equals(kbF8
)) {
2458 if (keypress
.equals(kbF9
)) {
2471 if (keypress
.equals(kbF10
)) {
2484 if (keypress
.equals(kbF11
)) {
2488 if (keypress
.equals(kbF12
)) {
2492 if (keypress
.equals(kbShiftF1
)) {
2497 if (type
== DeviceType
.XTERM
) {
2503 if (keypress
.equals(kbShiftF2
)) {
2508 if (type
== DeviceType
.XTERM
) {
2514 if (keypress
.equals(kbShiftF3
)) {
2519 if (type
== DeviceType
.XTERM
) {
2525 if (keypress
.equals(kbShiftF4
)) {
2530 if (type
== DeviceType
.XTERM
) {
2536 if (keypress
.equals(kbShiftF5
)) {
2538 return "\033[15;2~";
2541 if (keypress
.equals(kbShiftF6
)) {
2543 return "\033[17;2~";
2546 if (keypress
.equals(kbShiftF7
)) {
2548 return "\033[18;2~";
2551 if (keypress
.equals(kbShiftF8
)) {
2553 return "\033[19;2~";
2556 if (keypress
.equals(kbShiftF9
)) {
2558 return "\033[20;2~";
2561 if (keypress
.equals(kbShiftF10
)) {
2563 return "\033[21;2~";
2566 if (keypress
.equals(kbShiftF11
)) {
2568 return "\033[23;2~";
2571 if (keypress
.equals(kbShiftF12
)) {
2573 return "\033[24;2~";
2576 if (keypress
.equals(kbCtrlF1
)) {
2581 if (type
== DeviceType
.XTERM
) {
2587 if (keypress
.equals(kbCtrlF2
)) {
2592 if (type
== DeviceType
.XTERM
) {
2598 if (keypress
.equals(kbCtrlF3
)) {
2603 if (type
== DeviceType
.XTERM
) {
2609 if (keypress
.equals(kbCtrlF4
)) {
2614 if (type
== DeviceType
.XTERM
) {
2620 if (keypress
.equals(kbCtrlF5
)) {
2622 return "\033[15;5~";
2625 if (keypress
.equals(kbCtrlF6
)) {
2627 return "\033[17;5~";
2630 if (keypress
.equals(kbCtrlF7
)) {
2632 return "\033[18;5~";
2635 if (keypress
.equals(kbCtrlF8
)) {
2637 return "\033[19;5~";
2640 if (keypress
.equals(kbCtrlF9
)) {
2642 return "\033[20;5~";
2645 if (keypress
.equals(kbCtrlF10
)) {
2647 return "\033[21;5~";
2650 if (keypress
.equals(kbCtrlF11
)) {
2652 return "\033[23;5~";
2655 if (keypress
.equals(kbCtrlF12
)) {
2657 return "\033[24;5~";
2660 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2663 return xtermBuildKeySequence("\033[", '5', '~',
2664 keypress
.isCtrl(), keypress
.isAlt(),
2665 keypress
.isShift());
2671 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2674 return xtermBuildKeySequence("\033[", '6', '~',
2675 keypress
.isCtrl(), keypress
.isAlt(),
2676 keypress
.isShift());
2682 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2685 return xtermBuildKeySequence("\033[", '2', '~',
2686 keypress
.isCtrl(), keypress
.isAlt(),
2687 keypress
.isShift());
2693 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2696 return xtermBuildKeySequence("\033[", '3', '~',
2697 keypress
.isCtrl(), keypress
.isAlt(),
2698 keypress
.isShift());
2700 // Delete sends real delete for VTxxx
2705 if (keypress
.equals(kbEnter
)) {
2709 if (keypress
.equals(kbEsc
)) {
2713 if (keypress
.equals(kbAltEsc
)) {
2717 if (keypress
.equals(kbTab
)) {
2721 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2722 (keypress
.equals(kbShiftTab
))
2732 // Non-alt, non-ctrl characters
2733 if (!keypress
.isFnKey()) {
2734 StringBuilder sb
= new StringBuilder();
2735 sb
.append(Character
.toChars(keypress
.getChar()));
2736 return sb
.toString();
2742 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2745 * @param ch 8-bit character from the remote side
2746 * @param charsetGl character set defined for GL
2747 * @param charsetGr character set defined for GR
2748 * @return character to display on the screen
2750 private char mapCharacterCharset(final int ch
,
2751 final CharacterSet charsetGl
,
2752 final CharacterSet charsetGr
) {
2754 int lookupChar
= ch
;
2755 CharacterSet lookupCharset
= charsetGl
;
2758 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2759 lookupCharset
= charsetGr
;
2763 switch (lookupCharset
) {
2766 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2769 return DECCharacterSets
.UK
[lookupChar
];
2772 return DECCharacterSets
.US_ASCII
[lookupChar
];
2775 return DECCharacterSets
.NL
[lookupChar
];
2778 return DECCharacterSets
.FI
[lookupChar
];
2781 return DECCharacterSets
.FR
[lookupChar
];
2784 return DECCharacterSets
.FR_CA
[lookupChar
];
2787 return DECCharacterSets
.DE
[lookupChar
];
2790 return DECCharacterSets
.IT
[lookupChar
];
2793 return DECCharacterSets
.NO
[lookupChar
];
2796 return DECCharacterSets
.ES
[lookupChar
];
2799 return DECCharacterSets
.SV
[lookupChar
];
2802 return DECCharacterSets
.SWISS
[lookupChar
];
2804 case DEC_SUPPLEMENTAL
:
2805 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2808 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2811 return DECCharacterSets
.US_ASCII
[lookupChar
];
2814 return DECCharacterSets
.US_ASCII
[lookupChar
];
2817 throw new IllegalArgumentException("Invalid character set value: "
2823 * Map an 8-bit byte into a printable character.
2825 * @param ch either 8-bit or Unicode character from the remote side
2826 * @return character to display on the screen
2828 private int mapCharacter(final int ch
) {
2830 // Unicode character, just return it
2834 CharacterSet charsetGl
= currentState
.g0Charset
;
2835 CharacterSet charsetGr
= currentState
.grCharset
;
2837 if (vt52Mode
== true) {
2838 if (shiftOut
== true) {
2839 // Shifted out character, pull from VT52 graphics
2840 charsetGl
= currentState
.g1Charset
;
2841 charsetGr
= CharacterSet
.US
;
2844 charsetGl
= currentState
.g0Charset
;
2845 charsetGr
= CharacterSet
.US
;
2848 // Pull the character
2849 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2853 if (shiftOut
== true) {
2854 // Shifted out character, pull from G1
2855 charsetGl
= currentState
.g1Charset
;
2856 charsetGr
= currentState
.grCharset
;
2858 // Pull the character
2859 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2863 if (singleshift
== Singleshift
.SS2
) {
2865 singleshift
= Singleshift
.NONE
;
2867 // Shifted out character, pull from G2
2868 charsetGl
= currentState
.g2Charset
;
2869 charsetGr
= currentState
.grCharset
;
2873 if (singleshift
== Singleshift
.SS3
) {
2875 singleshift
= Singleshift
.NONE
;
2877 // Shifted out character, pull from G3
2878 charsetGl
= currentState
.g3Charset
;
2879 charsetGr
= currentState
.grCharset
;
2882 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2883 // Check for locking shift
2885 switch (currentState
.glLockshift
) {
2888 throw new IllegalArgumentException("programming bug");
2891 throw new IllegalArgumentException("programming bug");
2894 throw new IllegalArgumentException("programming bug");
2898 charsetGl
= currentState
.g2Charset
;
2903 charsetGl
= currentState
.g3Charset
;
2908 charsetGl
= currentState
.g0Charset
;
2912 switch (currentState
.grLockshift
) {
2915 throw new IllegalArgumentException("programming bug");
2918 throw new IllegalArgumentException("programming bug");
2922 charsetGr
= currentState
.g1Charset
;
2927 charsetGr
= currentState
.g2Charset
;
2932 charsetGr
= currentState
.g3Charset
;
2937 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2944 // Pull the character
2945 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2949 * Scroll the text within a scrolling region up n lines.
2951 * @param regionTop top row of the scrolling region
2952 * @param regionBottom bottom row of the scrolling region
2953 * @param n number of lines to scroll
2955 private void scrollingRegionScrollUp(final int regionTop
,
2956 final int regionBottom
, final int n
) {
2958 if (regionTop
>= regionBottom
) {
2962 // Sanity check: see if there will be any characters left after the
2964 if (regionBottom
+ 1 - regionTop
<= n
) {
2965 // There won't be anything left in the region, so just call
2966 // eraseScreen() and return.
2967 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2971 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2972 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2973 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2975 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2976 - remaining
, regionBottom
+ 1);
2977 display
= new ArrayList
<DisplayLine
>(displayTop
);
2978 display
.addAll(displayMiddle
);
2979 for (int i
= 0; i
< n
; i
++) {
2980 DisplayLine line
= new DisplayLine(currentState
.attr
);
2981 line
.setReverseColor(reverseVideo
);
2984 display
.addAll(displayBottom
);
2986 assert (display
.size() == height
);
2990 * Scroll the text within a scrolling region down n lines.
2992 * @param regionTop top row of the scrolling region
2993 * @param regionBottom bottom row of the scrolling region
2994 * @param n number of lines to scroll
2996 private void scrollingRegionScrollDown(final int regionTop
,
2997 final int regionBottom
, final int n
) {
2999 if (regionTop
>= regionBottom
) {
3003 // Sanity check: see if there will be any characters left after the
3005 if (regionBottom
+ 1 - regionTop
<= n
) {
3006 // There won't be anything left in the region, so just call
3007 // eraseScreen() and return.
3008 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
3012 int remaining
= regionBottom
+ 1 - regionTop
- n
;
3013 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
3014 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
3016 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
3017 regionTop
+ remaining
);
3018 display
= new ArrayList
<DisplayLine
>(displayTop
);
3019 for (int i
= 0; i
< n
; i
++) {
3020 DisplayLine line
= new DisplayLine(currentState
.attr
);
3021 line
.setReverseColor(reverseVideo
);
3024 display
.addAll(displayMiddle
);
3025 display
.addAll(displayBottom
);
3027 assert (display
.size() == height
);
3031 * Process a control character.
3033 * @param ch 8-bit character from the remote side
3035 private void handleControlChar(final char ch
) {
3036 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
3047 // Transmit the answerback message.
3058 cursorLeft(1, false);
3063 advanceToNextTabStop();
3089 currentState
.glLockshift
= LockshiftMode
.NONE
;
3095 currentState
.glLockshift
= LockshiftMode
.NONE
;
3120 singleshift
= Singleshift
.SS2
;
3125 singleshift
= Singleshift
.SS3
;
3135 * Advance the cursor to the next tab stop.
3137 private void advanceToNextTabStop() {
3138 if (tabStops
.size() == 0) {
3139 // Go to the rightmost column
3140 cursorRight(rightMargin
- currentState
.cursorX
, false);
3143 for (Integer stop
: tabStops
) {
3144 if (stop
> currentState
.cursorX
) {
3145 cursorRight(stop
- currentState
.cursorX
, false);
3150 * We got here, meaning there isn't a tab stop beyond the current
3151 * cursor position. Place the cursor of the right-most edge of the
3154 cursorRight(rightMargin
- currentState
.cursorX
, false);
3158 * Save a character into the collect buffer.
3160 * @param ch character to save
3162 private void collect(final char ch
) {
3163 collectBuffer
.append(ch
);
3167 * Save a byte into the CSI parameters buffer.
3169 * @param ch byte to save
3171 private void param(final byte ch
) {
3172 if (csiParams
.size() == 0) {
3173 csiParams
.add(Integer
.valueOf(0));
3175 Integer x
= csiParams
.get(csiParams
.size() - 1);
3176 if ((ch
>= '0') && (ch
<= '9')) {
3179 csiParams
.set(csiParams
.size() - 1, x
);
3182 if ((ch
== ';') && (csiParams
.size() < 16)) {
3183 csiParams
.add(Integer
.valueOf(0));
3188 * Get a CSI parameter value, with a default.
3190 * @param position parameter index. 0 is the first parameter.
3191 * @param defaultValue value to use if csiParams[position] doesn't exist
3192 * @return parameter value
3194 private int getCsiParam(final int position
, final int defaultValue
) {
3195 if (csiParams
.size() < position
+ 1) {
3196 return defaultValue
;
3198 return csiParams
.get(position
).intValue();
3202 * Get a CSI parameter value, clamped to within min/max.
3204 * @param position parameter index. 0 is the first parameter.
3205 * @param defaultValue value to use if csiParams[position] doesn't exist
3206 * @param minValue minimum value inclusive
3207 * @param maxValue maximum value inclusive
3208 * @return parameter value
3210 private int getCsiParam(final int position
, final int defaultValue
,
3211 final int minValue
, final int maxValue
) {
3213 assert (minValue
<= maxValue
);
3214 int value
= getCsiParam(position
, defaultValue
);
3215 if (value
< minValue
) {
3218 if (value
> maxValue
) {
3225 * Set or unset a toggle.
3227 * @param value true for set ('h'), false for reset ('l')
3229 private void setToggle(final boolean value
) {
3230 boolean decPrivateModeFlag
= false;
3232 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3233 if (collectBuffer
.charAt(i
) == '?') {
3234 decPrivateModeFlag
= true;
3239 for (Integer i
: csiParams
) {
3244 if (decPrivateModeFlag
== true) {
3246 if (value
== true) {
3247 // Use application arrow keys
3248 arrowKeyMode
= ArrowKeyMode
.VT100
;
3250 // Use ANSI arrow keys
3251 arrowKeyMode
= ArrowKeyMode
.ANSI
;
3256 if (decPrivateModeFlag
== true) {
3257 if (value
== false) {
3261 arrowKeyMode
= ArrowKeyMode
.VT52
;
3264 * From the VT102 docs: "You use ANSI mode to select
3265 * most terminal features; the terminal uses the same
3266 * features when it switches to VT52 mode. You
3267 * cannot, however, change most of these features in
3270 * In other words, do not reset any other attributes
3271 * when switching between VT52 submode and ANSI.
3273 * HOWEVER, the real vt100 does switch the character
3274 * set according to Usenet.
3276 currentState
.g0Charset
= CharacterSet
.US
;
3277 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3280 if ((type
== DeviceType
.VT220
)
3281 || (type
== DeviceType
.XTERM
)) {
3283 // VT52 mode is explicitly 7-bit
3285 singleshift
= Singleshift
.NONE
;
3290 if (value
== true) {
3291 // Turn off keyboard
3300 if (decPrivateModeFlag
== true) {
3302 if (value
== true) {
3309 if ((displayListener
!= null)
3310 && (type
== DeviceType
.XTERM
)
3312 // For xterms, reset to the actual width, not 80
3314 width
= displayListener
.getDisplayWidth();
3315 rightMargin
= width
- 1;
3318 width
= rightMargin
+ 1;
3321 // Entire screen is cleared, and scrolling region is
3323 eraseScreen(0, 0, height
- 1, width
- 1, false);
3324 scrollRegionTop
= 0;
3325 scrollRegionBottom
= height
- 1;
3326 // Also home the cursor
3327 cursorPosition(0, 0);
3331 if (decPrivateModeFlag
== true) {
3333 if (value
== true) {
3342 if (value
== true) {
3350 if (decPrivateModeFlag
== true) {
3352 if (value
== true) {
3354 * Set selects reverse screen, a white screen
3355 * background with black characters.
3357 if (reverseVideo
!= true) {
3359 * If in normal video, switch it back
3361 invertDisplayColors();
3363 reverseVideo
= true;
3366 * Reset selects normal screen, a black screen
3367 * background with white characters.
3369 if (reverseVideo
== true) {
3371 * If in reverse video already, switch it back
3373 invertDisplayColors();
3375 reverseVideo
= false;
3380 if (decPrivateModeFlag
== true) {
3382 if (value
== true) {
3383 // Origin is relative to scroll region cursor.
3384 // Cursor can NEVER leave scrolling region.
3385 currentState
.originMode
= true;
3386 cursorPosition(0, 0);
3388 // Origin is absolute to entire screen. Cursor can
3389 // leave the scrolling region via cup() and hvp().
3390 currentState
.originMode
= false;
3391 cursorPosition(0, 0);
3396 if (decPrivateModeFlag
== true) {
3398 if (value
== true) {
3400 currentState
.lineWrap
= true;
3402 // Turn linewrap off
3403 currentState
.lineWrap
= false;
3408 if (decPrivateModeFlag
== true) {
3410 if (value
== true) {
3411 // Keyboard auto-repeat on
3414 // Keyboard auto-repeat off
3420 if (decPrivateModeFlag
== false) {
3422 if (value
== true) {
3432 if (decPrivateModeFlag
== true) {
3438 if (decPrivateModeFlag
== true) {
3444 if (decPrivateModeFlag
== false) {
3446 if (value
== true) {
3448 * Set causes a received linefeed, form feed, or
3449 * vertical tab to move cursor to first column of
3450 * next line. RETURN transmits both a carriage return
3451 * and linefeed. This selection is also called new
3457 * Reset causes a received linefeed, form feed, or
3458 * vertical tab to move cursor to next line in
3459 * current column. RETURN transmits a carriage
3462 newLineMode
= false;
3468 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3469 if (decPrivateModeFlag
== true) {
3471 if (value
== true) {
3473 cursorVisible
= true;
3476 cursorVisible
= false;
3483 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3484 if (decPrivateModeFlag
== true) {
3486 if (value
== true) {
3487 // Select national mode NRC
3490 // Select multi-national mode
3499 if (type
== DeviceType
.XTERM
) {
3500 if (decPrivateModeFlag
== true) {
3501 if (value
== true) {
3502 // Enable sixel scrolling (default).
3505 // Disable sixel scrolling.
3514 if ((type
== DeviceType
.XTERM
)
3515 && (decPrivateModeFlag
== true)
3517 // Mouse: normal tracking mode
3518 if (value
== true) {
3519 mouseProtocol
= MouseProtocol
.NORMAL
;
3521 mouseProtocol
= MouseProtocol
.OFF
;
3527 if ((type
== DeviceType
.XTERM
)
3528 && (decPrivateModeFlag
== true)
3530 // Mouse: normal tracking mode
3531 if (value
== true) {
3532 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
3534 mouseProtocol
= MouseProtocol
.OFF
;
3540 if ((type
== DeviceType
.XTERM
)
3541 && (decPrivateModeFlag
== true)
3543 // Mouse: Any-event tracking mode
3544 if (value
== true) {
3545 mouseProtocol
= MouseProtocol
.ANYEVENT
;
3547 mouseProtocol
= MouseProtocol
.OFF
;
3553 if ((type
== DeviceType
.XTERM
)
3554 && (decPrivateModeFlag
== true)
3556 // Mouse: UTF-8 coordinates
3557 if (value
== true) {
3558 mouseEncoding
= MouseEncoding
.UTF8
;
3560 mouseEncoding
= MouseEncoding
.X10
;
3566 if ((type
== DeviceType
.XTERM
)
3567 && (decPrivateModeFlag
== true)
3569 // Mouse: SGR coordinates
3570 if (value
== true) {
3571 mouseEncoding
= MouseEncoding
.SGR
;
3573 mouseEncoding
= MouseEncoding
.X10
;
3579 if (type
== DeviceType
.XTERM
) {
3580 if (decPrivateModeFlag
== true) {
3581 if (value
== true) {
3582 // Use private color registers for each sixel
3583 // graphic (default).
3584 sixelPalette
= null;
3586 // Use shared color registers for each sixel
3588 sixelPalette
= new HashMap
<Integer
, java
.awt
.Color
>();
3602 * DECSC - Save cursor.
3604 private void decsc() {
3605 savedState
.setTo(currentState
);
3609 * DECRC - Restore cursor.
3611 private void decrc() {
3612 currentState
.setTo(savedState
);
3618 private void ind() {
3619 // Move the cursor and scroll if necessary. If at the bottom line
3620 // already, a scroll up is supposed to be performed.
3621 if (currentState
.cursorY
== scrollRegionBottom
) {
3622 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3624 cursorDown(1, true);
3628 * RI - Reverse index.
3631 // Move the cursor and scroll if necessary. If at the top line
3632 // already, a scroll down is supposed to be performed.
3633 if (currentState
.cursorY
== scrollRegionTop
) {
3634 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3642 private void nel() {
3643 // Move the cursor and scroll if necessary. If at the bottom line
3644 // already, a scroll up is supposed to be performed.
3645 if (currentState
.cursorY
== scrollRegionBottom
) {
3646 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3648 cursorDown(1, true);
3650 // Reset to the beginning of the next line
3651 currentState
.cursorX
= 0;
3655 * DECKPAM - Keypad application mode.
3657 private void deckpam() {
3658 keypadMode
= KeypadMode
.Application
;
3662 * DECKPNM - Keypad numeric mode.
3664 private void deckpnm() {
3665 keypadMode
= KeypadMode
.Numeric
;
3671 * @param n number of spaces to move
3672 * @param honorScrollRegion if true, then do nothing if the cursor is
3673 * outside the scrolling region
3675 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3679 * Special case: if a user moves the cursor from the right margin, we
3680 * have to reset the VT100 right margin flag.
3683 wrapLineFlag
= false;
3686 for (int i
= 0; i
< n
; i
++) {
3687 if (honorScrollRegion
== true) {
3688 // Honor the scrolling region
3689 if ((currentState
.cursorY
< scrollRegionTop
)
3690 || (currentState
.cursorY
> scrollRegionBottom
)
3692 // Outside region, do nothing
3695 // Inside region, go up
3696 top
= scrollRegionTop
;
3698 // Non-scrolling case
3702 if (currentState
.cursorY
> top
) {
3703 currentState
.cursorY
--;
3709 * Move down n spaces.
3711 * @param n number of spaces to move
3712 * @param honorScrollRegion if true, then do nothing if the cursor is
3713 * outside the scrolling region
3715 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3719 * Special case: if a user moves the cursor from the right margin, we
3720 * have to reset the VT100 right margin flag.
3723 wrapLineFlag
= false;
3726 for (int i
= 0; i
< n
; i
++) {
3728 if (honorScrollRegion
== true) {
3729 // Honor the scrolling region
3730 if (currentState
.cursorY
> scrollRegionBottom
) {
3731 // Outside region, do nothing
3734 // Inside region, go down
3735 bottom
= scrollRegionBottom
;
3737 // Non-scrolling case
3738 bottom
= height
- 1;
3741 if (currentState
.cursorY
< bottom
) {
3742 currentState
.cursorY
++;
3748 * Move left n spaces.
3750 * @param n number of spaces to move
3751 * @param honorScrollRegion if true, then do nothing if the cursor is
3752 * outside the scrolling region
3754 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3756 * Special case: if a user moves the cursor from the right margin, we
3757 * have to reset the VT100 right margin flag.
3760 wrapLineFlag
= false;
3763 for (int i
= 0; i
< n
; i
++) {
3764 if (honorScrollRegion
== true) {
3765 // Honor the scrolling region
3766 if ((currentState
.cursorY
< scrollRegionTop
)
3767 || (currentState
.cursorY
> scrollRegionBottom
)
3769 // Outside region, do nothing
3774 if (currentState
.cursorX
> 0) {
3775 currentState
.cursorX
--;
3781 * Move right n spaces.
3783 * @param n number of spaces to move
3784 * @param honorScrollRegion if true, then do nothing if the cursor is
3785 * outside the scrolling region
3787 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3788 int rightMargin
= this.rightMargin
;
3791 * Special case: if a user moves the cursor from the right margin, we
3792 * have to reset the VT100 right margin flag.
3795 wrapLineFlag
= false;
3798 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3799 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3802 for (int i
= 0; i
< n
; i
++) {
3803 if (honorScrollRegion
== true) {
3804 // Honor the scrolling region
3805 if ((currentState
.cursorY
< scrollRegionTop
)
3806 || (currentState
.cursorY
> scrollRegionBottom
)
3808 // Outside region, do nothing
3813 if (currentState
.cursorX
< rightMargin
) {
3814 currentState
.cursorX
++;
3820 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3822 * @param row row to move to
3823 * @param col column to move to
3825 private void cursorPosition(int row
, final int col
) {
3826 int rightMargin
= this.rightMargin
;
3831 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3832 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3835 // Set column number
3836 currentState
.cursorX
= col
;
3838 // Sanity check, bring column back to margin.
3839 if (currentState
.cursorX
> rightMargin
) {
3840 currentState
.cursorX
= rightMargin
;
3844 if (currentState
.originMode
== true) {
3845 row
+= scrollRegionTop
;
3847 if (currentState
.cursorY
< row
) {
3848 cursorDown(row
- currentState
.cursorY
, false);
3849 } else if (currentState
.cursorY
> row
) {
3850 cursorUp(currentState
.cursorY
- row
, false);
3853 wrapLineFlag
= false;
3857 * HTS - Horizontal tabulation set.
3859 private void hts() {
3860 for (Integer stop
: tabStops
) {
3861 if (stop
== currentState
.cursorX
) {
3862 // Already have a tab stop here
3867 // Append a tab stop to the end of the array and resort them
3868 tabStops
.add(currentState
.cursorX
);
3869 Collections
.sort(tabStops
);
3873 * DECSWL - Single-width line.
3875 private void decswl() {
3876 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3877 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3881 * DECDWL - Double-width line.
3883 private void decdwl() {
3884 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3885 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3889 * DECHDL - Double-height + double-width line.
3891 * @param topHalf if true, this sets the row to be the top half row of a
3894 private void dechdl(final boolean topHalf
) {
3895 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3896 if (topHalf
== true) {
3897 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3899 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3904 * DECALN - Screen alignment display.
3906 private void decaln() {
3907 Cell newCell
= new Cell('E');
3908 for (DisplayLine line
: display
) {
3909 for (int i
= 0; i
< line
.length(); i
++) {
3910 line
.replace(i
, newCell
);
3916 * DECSCL - Compatibility level.
3918 private void decscl() {
3919 int i
= getCsiParam(0, 0);
3920 int j
= getCsiParam(1, 0);
3924 currentState
.g0Charset
= CharacterSet
.US
;
3925 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3927 } else if (i
== 62) {
3929 if ((j
== 0) || (j
== 2)) {
3931 } else if (j
== 1) {
3938 * CUD - Cursor down.
3940 private void cud() {
3941 cursorDown(getCsiParam(0, 1, 1, height
), true);
3945 * CUF - Cursor forward.
3947 private void cuf() {
3948 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3952 * CUB - Cursor backward.
3954 private void cub() {
3955 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3961 private void cuu() {
3962 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3966 * CUP - Cursor position.
3968 private void cup() {
3969 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3970 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3974 * CNL - Cursor down and to column 1.
3976 private void cnl() {
3977 cursorDown(getCsiParam(0, 1, 1, height
), true);
3979 cursorLeft(currentState
.cursorX
, true);
3983 * CPL - Cursor up and to column 1.
3985 private void cpl() {
3986 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3988 cursorLeft(currentState
.cursorX
, true);
3992 * CHA - Cursor to column # in current row.
3994 private void cha() {
3995 cursorPosition(currentState
.cursorY
,
3996 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
4000 * VPA - Cursor to row #, same column.
4002 private void vpa() {
4003 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
4004 currentState
.cursorX
);
4008 * ED - Erase in display.
4011 boolean honorProtected
= false;
4012 boolean decPrivateModeFlag
= false;
4014 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4015 if (collectBuffer
.charAt(i
) == '?') {
4016 decPrivateModeFlag
= true;
4021 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4022 && (decPrivateModeFlag
== true)
4024 honorProtected
= true;
4027 int i
= getCsiParam(0, 0);
4030 // Erase from here to end of screen
4031 if (currentState
.cursorY
< height
- 1) {
4032 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
4035 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
4036 } else if (i
== 1) {
4037 // Erase from beginning of screen to here
4038 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
4040 eraseLine(0, currentState
.cursorX
, honorProtected
);
4041 } else if (i
== 2) {
4042 // Erase entire screen
4043 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
4048 * EL - Erase in line.
4051 boolean honorProtected
= false;
4052 boolean decPrivateModeFlag
= false;
4054 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4055 if (collectBuffer
.charAt(i
) == '?') {
4056 decPrivateModeFlag
= true;
4061 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4062 && (decPrivateModeFlag
== true)
4064 honorProtected
= true;
4067 int i
= getCsiParam(0, 0);
4070 // Erase from here to end of line
4071 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
4072 } else if (i
== 1) {
4073 // Erase from beginning of line to here
4074 eraseLine(0, currentState
.cursorX
, honorProtected
);
4075 } else if (i
== 2) {
4076 // Erase entire line
4077 eraseLine(0, width
- 1, honorProtected
);
4082 * ECH - Erase # of characters in current row.
4084 private void ech() {
4085 int i
= getCsiParam(0, 1, 1, width
);
4087 // Erase from here to i characters
4088 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
4095 int i
= getCsiParam(0, 1);
4097 if ((currentState
.cursorY
>= scrollRegionTop
)
4098 && (currentState
.cursorY
<= scrollRegionBottom
)
4101 // I can get the same effect with a scroll-down
4102 scrollingRegionScrollDown(currentState
.cursorY
,
4103 scrollRegionBottom
, i
);
4108 * DCH - Delete char.
4110 private void dch() {
4111 int n
= getCsiParam(0, 1);
4112 DisplayLine line
= display
.get(currentState
.cursorY
);
4113 Cell blank
= new Cell();
4114 for (int i
= 0; i
< n
; i
++) {
4115 line
.delete(currentState
.cursorX
, blank
);
4120 * ICH - Insert blank char at cursor.
4122 private void ich() {
4123 int n
= getCsiParam(0, 1);
4124 DisplayLine line
= display
.get(currentState
.cursorY
);
4125 Cell blank
= new Cell();
4126 for (int i
= 0; i
< n
; i
++) {
4127 line
.insert(currentState
.cursorX
, blank
);
4135 int i
= getCsiParam(0, 1);
4137 if ((currentState
.cursorY
>= scrollRegionTop
)
4138 && (currentState
.cursorY
<= scrollRegionBottom
)) {
4140 // I can get the same effect with a scroll-down
4141 scrollingRegionScrollUp(currentState
.cursorY
,
4142 scrollRegionBottom
, i
);
4147 * HVP - Horizontal and vertical position.
4149 private void hvp() {
4154 * REP - Repeat character.
4156 private void rep() {
4157 int n
= getCsiParam(0, 1);
4158 for (int i
= 0; i
< n
; i
++) {
4159 printCharacter(repCh
);
4167 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
4168 getCsiParam(0, 1, 1, height
));
4175 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
4176 getCsiParam(0, 1, 1, height
));
4180 * CBT - Go back X tab stops.
4182 private void cbt() {
4183 int tabsToMove
= getCsiParam(0, 1);
4186 for (int i
= 0; i
< tabsToMove
; i
++) {
4187 int j
= currentState
.cursorX
;
4188 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
4189 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
4197 j
= tabStops
.get(tabI
);
4199 cursorPosition(currentState
.cursorY
, j
);
4204 * CHT - Advance X tab stops.
4206 private void cht() {
4207 int n
= getCsiParam(0, 1);
4208 for (int i
= 0; i
< n
; i
++) {
4209 advanceToNextTabStop();
4214 * SGR - Select graphics rendition.
4216 private void sgr() {
4218 if (csiParams
.size() == 0) {
4219 currentState
.attr
.reset();
4223 int sgrColorMode
= -1;
4224 boolean idx88Color
= false;
4225 boolean rgbColor
= false;
4229 for (Integer i
: csiParams
) {
4231 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
4233 assert (type
== DeviceType
.XTERM
);
4237 * Indexed color mode, we now have the index number.
4239 if (sgrColorMode
== 38) {
4240 currentState
.attr
.setForeColorRGB(get88Color(i
));
4242 assert (sgrColorMode
== 48);
4243 currentState
.attr
.setBackColorRGB(get88Color(i
));
4252 * RGB color mode, we are collecting tokens.
4256 } else if (rgbGreen
== -1) {
4257 rgbGreen
= i
& 0xFF;
4259 int rgb
= rgbRed
<< 16;
4260 rgb
|= rgbGreen
<< 8;
4263 // System.err.printf("RGB: %08x\n", rgb);
4265 if (sgrColorMode
== 38) {
4266 currentState
.attr
.setForeColorRGB(rgb
);
4268 assert (sgrColorMode
== 48);
4269 currentState
.attr
.setBackColorRGB(rgb
);
4290 * Indexed color mode.
4297 * This is neither indexed nor RGB color. Bail out.
4302 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
4308 currentState
.attr
.reset();
4313 currentState
.attr
.setBold(true);
4318 currentState
.attr
.setUnderline(true);
4323 currentState
.attr
.setBlink(true);
4328 currentState
.attr
.setReverse(true);
4335 if (type
== DeviceType
.XTERM
) {
4345 // Set black foreground
4346 currentState
.attr
.setForeColorRGB(get88Color(8));
4349 // Set red foreground
4350 currentState
.attr
.setForeColorRGB(get88Color(9));
4353 // Set green foreground
4354 currentState
.attr
.setForeColorRGB(get88Color(10));
4357 // Set yellow foreground
4358 currentState
.attr
.setForeColorRGB(get88Color(11));
4361 // Set blue foreground
4362 currentState
.attr
.setForeColorRGB(get88Color(12));
4365 // Set magenta foreground
4366 currentState
.attr
.setForeColorRGB(get88Color(13));
4369 // Set cyan foreground
4370 currentState
.attr
.setForeColorRGB(get88Color(14));
4373 // Set white foreground
4374 currentState
.attr
.setForeColorRGB(get88Color(15));
4378 // Set black background
4379 currentState
.attr
.setBackColorRGB(get88Color(8));
4382 // Set red background
4383 currentState
.attr
.setBackColorRGB(get88Color(9));
4386 // Set green background
4387 currentState
.attr
.setBackColorRGB(get88Color(10));
4390 // Set yellow background
4391 currentState
.attr
.setBackColorRGB(get88Color(11));
4394 // Set blue background
4395 currentState
.attr
.setBackColorRGB(get88Color(12));
4398 // Set magenta background
4399 currentState
.attr
.setBackColorRGB(get88Color(13));
4402 // Set cyan background
4403 currentState
.attr
.setBackColorRGB(get88Color(14));
4406 // Set white background
4407 currentState
.attr
.setBackColorRGB(get88Color(15));
4415 if ((type
== DeviceType
.VT220
)
4416 || (type
== DeviceType
.XTERM
)) {
4422 currentState
.attr
.setBold(false);
4427 currentState
.attr
.setUnderline(false);
4432 currentState
.attr
.setBlink(false);
4437 currentState
.attr
.setReverse(false);
4445 // A true VT100/102/220 does not support color, however everyone
4446 // is used to their terminal emulator supporting color so we will
4447 // unconditionally support color for all DeviceType's.
4452 // Set black foreground
4453 currentState
.attr
.setForeColor(Color
.BLACK
);
4454 currentState
.attr
.setForeColorRGB(-1);
4457 // Set red foreground
4458 currentState
.attr
.setForeColor(Color
.RED
);
4459 currentState
.attr
.setForeColorRGB(-1);
4462 // Set green foreground
4463 currentState
.attr
.setForeColor(Color
.GREEN
);
4464 currentState
.attr
.setForeColorRGB(-1);
4467 // Set yellow foreground
4468 currentState
.attr
.setForeColor(Color
.YELLOW
);
4469 currentState
.attr
.setForeColorRGB(-1);
4472 // Set blue foreground
4473 currentState
.attr
.setForeColor(Color
.BLUE
);
4474 currentState
.attr
.setForeColorRGB(-1);
4477 // Set magenta foreground
4478 currentState
.attr
.setForeColor(Color
.MAGENTA
);
4479 currentState
.attr
.setForeColorRGB(-1);
4482 // Set cyan foreground
4483 currentState
.attr
.setForeColor(Color
.CYAN
);
4484 currentState
.attr
.setForeColorRGB(-1);
4487 // Set white foreground
4488 currentState
.attr
.setForeColor(Color
.WHITE
);
4489 currentState
.attr
.setForeColorRGB(-1);
4492 if (type
== DeviceType
.XTERM
) {
4494 * Xterm supports T.416 / ISO-8613-3 codes to select
4495 * either an indexed color or an RGB value. (It also
4496 * permits these ISO-8613-3 SGR sequences to be separated
4497 * by colons rather than semicolons.)
4499 * We will support only the following:
4501 * 1. Indexed color mode (88- or 256-color modes).
4505 * These cover most of the use cases in the real world.
4507 * HOWEVER, note that this is an awful broken "standard",
4508 * with no way to do it "right". See
4509 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4510 * for a detailed discussion of the current state of RGB
4511 * in various terminals, the point of which is that none
4512 * of them really do the same thing despite all appearing
4516 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4517 * where it is assumed that supporting just the "indexed
4518 * mode" of these sequences (which could align easily
4519 * with existing SGR colors) is assumed to mean full
4520 * support of 24-bit RGB. So it is all or nothing.
4522 * Finally, these sequences break the assumptions of
4523 * standard ECMA-48 style parsers as pointed out at
4524 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4525 * Therefore in order to keep a clean display, we cannot
4526 * parse anything else in this sequence.
4531 // Underscore on, default foreground color
4532 currentState
.attr
.setUnderline(true);
4533 currentState
.attr
.setForeColor(Color
.WHITE
);
4537 // Underscore off, default foreground color
4538 currentState
.attr
.setUnderline(false);
4539 currentState
.attr
.setForeColor(Color
.WHITE
);
4540 currentState
.attr
.setForeColorRGB(-1);
4543 // Set black background
4544 currentState
.attr
.setBackColor(Color
.BLACK
);
4545 currentState
.attr
.setBackColorRGB(-1);
4548 // Set red background
4549 currentState
.attr
.setBackColor(Color
.RED
);
4550 currentState
.attr
.setBackColorRGB(-1);
4553 // Set green background
4554 currentState
.attr
.setBackColor(Color
.GREEN
);
4555 currentState
.attr
.setBackColorRGB(-1);
4558 // Set yellow background
4559 currentState
.attr
.setBackColor(Color
.YELLOW
);
4560 currentState
.attr
.setBackColorRGB(-1);
4563 // Set blue background
4564 currentState
.attr
.setBackColor(Color
.BLUE
);
4565 currentState
.attr
.setBackColorRGB(-1);
4568 // Set magenta background
4569 currentState
.attr
.setBackColor(Color
.MAGENTA
);
4570 currentState
.attr
.setBackColorRGB(-1);
4573 // Set cyan background
4574 currentState
.attr
.setBackColor(Color
.CYAN
);
4575 currentState
.attr
.setBackColorRGB(-1);
4578 // Set white background
4579 currentState
.attr
.setBackColor(Color
.WHITE
);
4580 currentState
.attr
.setBackColorRGB(-1);
4583 if (type
== DeviceType
.XTERM
) {
4585 * Xterm supports T.416 / ISO-8613-3 codes to select
4586 * either an indexed color or an RGB value. (It also
4587 * permits these ISO-8613-3 SGR sequences to be separated
4588 * by colons rather than semicolons.)
4590 * We will support only the following:
4592 * 1. Indexed color mode (88- or 256-color modes).
4596 * These cover most of the use cases in the real world.
4598 * HOWEVER, note that this is an awful broken "standard",
4599 * with no way to do it "right". See
4600 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4601 * for a detailed discussion of the current state of RGB
4602 * in various terminals, the point of which is that none
4603 * of them really do the same thing despite all appearing
4607 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4608 * where it is assumed that supporting just the "indexed
4609 * mode" of these sequences (which could align easily
4610 * with existing SGR colors) is assumed to mean full
4611 * support of 24-bit RGB. So it is all or nothing.
4613 * Finally, these sequences break the assumptions of
4614 * standard ECMA-48 style parsers as pointed out at
4615 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4616 * Therefore in order to keep a clean display, we cannot
4617 * parse anything else in this sequence.
4624 // Default background
4625 currentState
.attr
.setBackColor(Color
.BLACK
);
4626 currentState
.attr
.setBackColorRGB(-1);
4636 * DA - Device attributes.
4639 int extendedFlag
= 0;
4641 if (collectBuffer
.length() > 0) {
4642 String args
= collectBuffer
.substring(1);
4643 if (collectBuffer
.charAt(0) == '>') {
4645 if (collectBuffer
.length() >= 2) {
4646 i
= Integer
.parseInt(args
);
4648 } else if (collectBuffer
.charAt(0) == '=') {
4650 if (collectBuffer
.length() >= 2) {
4651 i
= Integer
.parseInt(args
);
4654 // Unknown code, bail out
4659 if ((i
!= 0) && (i
!= 1)) {
4663 if ((extendedFlag
== 0) && (i
== 0)) {
4664 // Send string directly to remote side
4665 writeRemote(deviceTypeResponse());
4669 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4671 if ((extendedFlag
== 1) && (i
== 0)) {
4673 * Request "What type of terminal are you, what is your
4674 * firmware version, and what hardware options do you have
4677 * Respond: "I am a VT220 (identification code of 1), my
4678 * firmware version is _____ (Pv), and I have _____ Po
4679 * options installed."
4685 if (s8c1t
== true) {
4686 writeRemote("\u009b>1;10;0c");
4688 writeRemote("\033[>1;10;0c");
4694 if ((extendedFlag
== 2) && (i
== 0)) {
4697 * Request "What is your unit ID?"
4699 * Respond: "I was manufactured at site 00 and have a unique ID
4703 writeRemote("\033P!|00010203\033\\");
4708 * DECSTBM - Set top and bottom margins.
4710 private void decstbm() {
4711 boolean decPrivateModeFlag
= false;
4713 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4714 if (collectBuffer
.charAt(i
) == '?') {
4715 decPrivateModeFlag
= true;
4719 if (decPrivateModeFlag
) {
4720 // This could be restore DEC private mode values.
4724 int top
= getCsiParam(0, 1, 1, height
) - 1;
4725 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4726 if (bottom
> height
- 1) {
4727 bottom
= height
- 1;
4733 scrollRegionTop
= top
;
4734 scrollRegionBottom
= bottom
;
4737 cursorPosition(0, 0);
4742 * DECREQTPARM - Request terminal parameters.
4744 private void decreqtparm() {
4745 int i
= getCsiParam(0, 0);
4747 if ((i
!= 0) && (i
!= 1)) {
4754 * Request terminal parameters.
4758 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4759 * (CLoCk MULtiplier = 1, STP option flags = 0)
4763 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4766 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4768 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4774 * DECSCA - Select Character Attributes.
4776 private void decsca() {
4777 int i
= getCsiParam(0, 0);
4779 if ((i
== 0) || (i
== 2)) {
4781 currentState
.attr
.setProtect(false);
4785 currentState
.attr
.setProtect(true);
4790 * DECSTR - Soft Terminal Reset.
4792 private void decstr() {
4793 // Do exactly like RIS - Reset to initial state
4795 // Do I clear screen too? I think so...
4796 eraseScreen(0, 0, height
- 1, width
- 1, false);
4797 cursorPosition(0, 0);
4801 * DSR - Device status report.
4803 private void dsr() {
4804 boolean decPrivateModeFlag
= false;
4805 int row
= currentState
.cursorY
;
4807 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4808 if (collectBuffer
.charAt(i
) == '?') {
4809 decPrivateModeFlag
= true;
4814 int i
= getCsiParam(0, 0);
4819 // Request status report. Respond with "OK, no malfunction."
4821 // Send string directly to remote side
4822 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4825 writeRemote("\u009b0n");
4827 writeRemote("\033[0n");
4832 // Request cursor position. Respond with current position.
4833 if (currentState
.originMode
== true) {
4834 row
-= scrollRegionTop
;
4837 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4840 str
= String
.format("\u009b%d;%dR", row
+ 1,
4841 currentState
.cursorX
+ 1);
4843 str
= String
.format("\033[%d;%dR", row
+ 1,
4844 currentState
.cursorX
+ 1);
4847 // Send string directly to remote side
4852 if (decPrivateModeFlag
== true) {
4854 // Request printer status report. Respond with "Printer not
4857 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4858 && (s8c1t
== true)) {
4859 writeRemote("\u009b?13n");
4861 writeRemote("\033[?13n");
4867 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4868 && (decPrivateModeFlag
== true)
4871 // Request user-defined keys are locked or unlocked. Respond
4872 // with "User-defined keys are locked."
4874 if (s8c1t
== true) {
4875 writeRemote("\u009b?21n");
4877 writeRemote("\033[?21n");
4883 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4884 && (decPrivateModeFlag
== true)
4887 // Request keyboard language. Respond with "Keyboard
4888 // language is North American."
4890 if (s8c1t
== true) {
4891 writeRemote("\u009b?27;1n");
4893 writeRemote("\033[?27;1n");
4900 // Some other option, ignore
4906 * TBC - Tabulation clear.
4908 private void tbc() {
4909 int i
= getCsiParam(0, 0);
4911 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4912 for (Integer stop
: tabStops
) {
4913 if (stop
== currentState
.cursorX
) {
4918 tabStops
= newStops
;
4926 * Erase the characters in the current line from the start column to the
4927 * end column, inclusive.
4929 * @param start starting column to erase (between 0 and width - 1)
4930 * @param end ending column to erase (between 0 and width - 1)
4931 * @param honorProtected if true, do not erase characters with the
4932 * protected attribute set
4934 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4939 if (end
> width
- 1) {
4946 for (int i
= start
; i
<= end
; i
++) {
4947 DisplayLine line
= display
.get(currentState
.cursorY
);
4948 if ((!honorProtected
)
4949 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4956 * From the VT102 manual:
4958 * Erasing a character also erases any character
4959 * attribute of the character.
4965 * Erase with the current color a.k.a. back-color erase
4968 line
.setChar(i
, ' ');
4969 line
.setAttr(i
, currentState
.attr
);
4977 * Erase a rectangular section of the screen, inclusive. end column,
4980 * @param startRow starting row to erase (between 0 and height - 1)
4981 * @param startCol starting column to erase (between 0 and width - 1)
4982 * @param endRow ending row to erase (between 0 and height - 1)
4983 * @param endCol ending column to erase (between 0 and width - 1)
4984 * @param honorProtected if true, do not erase characters with the
4985 * protected attribute set
4987 private void eraseScreen(final int startRow
, final int startCol
,
4988 final int endRow
, final int endCol
, final boolean honorProtected
) {
4996 || (endRow
< startRow
)
4997 || (endCol
< startCol
)
5002 oldCursorY
= currentState
.cursorY
;
5003 for (int i
= startRow
; i
<= endRow
; i
++) {
5004 currentState
.cursorY
= i
;
5005 eraseLine(startCol
, endCol
, honorProtected
);
5007 // Erase display clears the double attributes
5008 display
.get(i
).setDoubleWidth(false);
5009 display
.get(i
).setDoubleHeight(0);
5011 currentState
.cursorY
= oldCursorY
;
5015 * VT220 printer functions. All of these are parsed, but won't do
5018 private void printerFunctions() {
5019 boolean decPrivateModeFlag
= false;
5020 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
5021 if (collectBuffer
.charAt(i
) == '?') {
5022 decPrivateModeFlag
= true;
5027 int i
= getCsiParam(0, 0);
5032 if (decPrivateModeFlag
== false) {
5038 if (decPrivateModeFlag
== true) {
5039 // Print cursor line
5044 if (decPrivateModeFlag
== true) {
5045 // Auto print mode OFF
5047 // Printer controller OFF
5049 // Characters re-appear on the screen
5050 printerControllerMode
= false;
5055 if (decPrivateModeFlag
== true) {
5059 // Printer controller
5061 // Characters get sucked into oblivion
5062 printerControllerMode
= true;
5073 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
5074 * of remote systems will send an XTerm title sequence even if TERM isn't
5077 * @param xtermChar the character received from the remote side
5079 private void oscPut(final char xtermChar
) {
5080 // System.err.println("oscPut: " + xtermChar);
5082 boolean oscEnd
= false;
5084 if (xtermChar
== 0x07) {
5087 if ((xtermChar
== '\\')
5088 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\033')
5094 collectBuffer
.append(xtermChar
);
5099 if (xtermChar
== 0x07) {
5100 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
5102 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
5105 String
[] p
= args
.split(";");
5107 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
5114 if (p
[0].equals("4")) {
5115 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
5116 // Set a color index value
5118 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
5119 } catch (NumberFormatException e
) {
5125 if (p
[0].equals("10")) {
5126 if (p
[1].equals("?")) {
5127 // Respond with foreground color.
5128 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToForegroundColor(currentState
.attr
);
5130 writeRemote(String
.format(
5131 "\033]10;rgb:%04x/%04x/%04x\033\\",
5132 color
.getRed() << 8,
5133 color
.getGreen() << 8,
5134 color
.getBlue() << 8));
5138 if (p
[0].equals("11")) {
5139 if (p
[1].equals("?")) {
5140 // Respond with background color.
5141 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToBackgroundColor(currentState
.attr
);
5143 writeRemote(String
.format(
5144 "\033]11;rgb:%04x/%04x/%04x\033\\",
5145 color
.getRed() << 8,
5146 color
.getGreen() << 8,
5147 color
.getBlue() << 8));
5151 if (p
[0].equals("444")) {
5152 if (p
[1].equals("0") && (p
.length
== 6)) {
5153 // Jexer image - RGB
5154 parseJexerImageRGB(p
[2], p
[3], p
[4], p
[5]);
5155 } else if (p
[1].equals("1") && (p
.length
== 4)) {
5156 // Jexer image - PNG
5157 parseJexerImageFile(1, p
[2], p
[3]);
5158 } else if (p
[1].equals("2") && (p
.length
== 4)) {
5159 // Jexer image - JPG
5160 parseJexerImageFile(2, p
[2], p
[3]);
5165 // Go to SCAN_GROUND state
5172 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
5173 * Jexer ECMA48Terminal to talk to ECMA48.
5175 * @param pmChar the character received from the remote side
5177 private void pmPut(final char pmChar
) {
5178 // System.err.println("pmPut: " + pmChar);
5180 boolean pmEnd
= false;
5182 if ((pmChar
== '\\')
5183 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\033')
5189 collectBuffer
.append(pmChar
);
5194 arg
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
5196 // System.err.println("arg: '" + arg + "'");
5198 if (arg
.equals("hideMousePointer")) {
5199 hideMousePointer
= true;
5201 if (arg
.equals("showMousePointer")) {
5202 hideMousePointer
= false;
5205 // Go to SCAN_GROUND state
5212 * Perform xterm window operations.
5214 private void xtermWindowOps() {
5215 boolean xtermPrivateModeFlag
= false;
5217 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
5218 if (collectBuffer
.charAt(i
) == '?') {
5219 xtermPrivateModeFlag
= true;
5224 int i
= getCsiParam(0, 0);
5226 if (!xtermPrivateModeFlag
) {
5229 // Report xterm text area size in pixels as CSI 4 ; height ;
5231 writeRemote(String
.format("\033[4;%d;%dt", textHeight
* height
,
5232 textWidth
* width
));
5235 // Report character size in pixels as CSI 6 ; height ; width
5237 writeRemote(String
.format("\033[6;%d;%dt", textHeight
,
5241 // Report the text are size in characters as CSI 8 ; height ;
5243 writeRemote(String
.format("\033[8;%d;%dt", height
, width
));
5252 * Respond to xterm sixel query.
5254 private void xtermSixelQuery() {
5255 int item
= getCsiParam(0, 0);
5256 int action
= getCsiParam(1, 0);
5257 int value
= getCsiParam(2, 0);
5262 // Report number of color registers.
5263 writeRemote(String
.format("\033[?%d;%d;%dS", item
, 0, 1024));
5270 // We will not support this option.
5271 writeRemote(String
.format("\033[?%d;%dS", item
, action
));
5275 * Run this input character through the ECMA48 state machine.
5277 * @param ch character from the remote side
5279 private void consume(final int ch
) {
5283 // System.err.printf("%c STATE = %s\n", ch, scanState);
5285 // Special "anywhere" states
5287 // 18, 1A --> execute, then switch to SCAN_GROUND
5288 if ((ch
== 0x18) || (ch
== 0x1A)) {
5289 // CAN and SUB abort escape sequences
5294 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
5298 if ((type
== DeviceType
.XTERM
)
5299 && ((scanState
== ScanState
.OSC_STRING
)
5300 || (scanState
== ScanState
.DCS_SIXEL
)
5301 || (scanState
== ScanState
.SOSPMAPC_STRING
))
5303 // Xterm can pass ESCAPE to its OSC sequence.
5304 // Xterm can pass ESCAPE to its DCS sequence.
5305 // Jexer can pass ESCAPE to its PM sequence.
5306 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
5307 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
5308 && (scanState
!= ScanState
.DCS_IGNORE
)
5309 && (scanState
!= ScanState
.DCS_PARAM
)
5310 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
5312 scanState
= ScanState
.ESCAPE
;
5317 // 0x9B == CSI 8-bit sequence
5319 scanState
= ScanState
.CSI_ENTRY
;
5323 // 0x9D goes to ScanState.OSC_STRING
5325 scanState
= ScanState
.OSC_STRING
;
5329 // 0x90 goes to DCS_ENTRY
5331 scanState
= ScanState
.DCS_ENTRY
;
5335 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
5336 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
5337 scanState
= ScanState
.SOSPMAPC_STRING
;
5341 // 0x7F (DEL) is always discarded
5346 switch (scanState
) {
5349 // 00-17, 19, 1C-1F --> execute
5350 // 80-8F, 91-9A, 9C --> execute
5351 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
5352 handleControlChar((char) ch
);
5356 if (((ch
>= 0x20) && (ch
<= 0x7F))
5360 // VT220 printer --> trash bin
5361 if (((type
== DeviceType
.VT220
)
5362 || (type
== DeviceType
.XTERM
))
5363 && (printerControllerMode
== true)
5368 // Hang onto this character
5369 repCh
= mapCharacter(ch
);
5371 // Print this character
5372 printCharacter(repCh
);
5377 // 00-17, 19, 1C-1F --> execute
5379 handleControlChar((char) ch
);
5383 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
5384 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5386 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
5390 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
5391 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
5402 // DECSC - Save cursor
5403 // Note this code overlaps both ANSI and VT52 mode
5408 // DECRC - Restore cursor
5409 // Note this code overlaps both ANSI and VT52 mode
5418 if (vt52Mode
== true) {
5419 // DECANM - Enter ANSI mode
5421 arrowKeyMode
= ArrowKeyMode
.VT100
;
5424 * From the VT102 docs: "You use ANSI mode to select
5425 * most terminal features; the terminal uses the same
5426 * features when it switches to VT52 mode. You
5427 * cannot, however, change most of these features in
5430 * In other words, do not reset any other attributes
5431 * when switching between VT52 submode and ANSI.
5435 currentState
.g0Charset
= CharacterSet
.US
;
5436 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5438 singleshift
= Singleshift
.NONE
;
5439 currentState
.glLockshift
= LockshiftMode
.NONE
;
5440 currentState
.grLockshift
= LockshiftMode
.NONE
;
5444 // DECKPAM - Keypad application mode
5445 // Note this code overlaps both ANSI and VT52 mode
5449 // DECKPNM - Keypad numeric mode
5450 // Note this code overlaps both ANSI and VT52 mode
5457 if (vt52Mode
== true) {
5458 // Cursor up, and stop at the top without scrolling
5463 if (vt52Mode
== true) {
5464 // Cursor down, and stop at the bottom without scrolling
5465 cursorDown(1, false);
5469 if (vt52Mode
== true) {
5470 // Cursor right, and stop at the right without scrolling
5471 cursorRight(1, false);
5475 if (vt52Mode
== true) {
5476 // Cursor left, and stop at the left without scrolling
5477 cursorLeft(1, false);
5484 if (vt52Mode
== true) {
5492 if (vt52Mode
== true) {
5493 // G0 --> Special graphics
5494 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
5498 if (vt52Mode
== true) {
5500 currentState
.g0Charset
= CharacterSet
.US
;
5504 if (vt52Mode
== true) {
5506 cursorPosition(0, 0);
5508 // HTS - Horizontal tabulation set
5513 if (vt52Mode
== true) {
5514 // Reverse line feed. Same as RI.
5519 if (vt52Mode
== true) {
5520 // Erase to end of screen
5521 eraseLine(currentState
.cursorX
, width
- 1, false);
5522 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
5527 if (vt52Mode
== true) {
5528 // Erase to end of line
5529 eraseLine(currentState
.cursorX
, width
- 1, false);
5535 if (vt52Mode
== true) {
5538 // RI - Reverse index
5543 if (vt52Mode
== false) {
5545 singleshift
= Singleshift
.SS2
;
5549 if (vt52Mode
== false) {
5551 singleshift
= Singleshift
.SS3
;
5558 if ((ch
>= 0x51) && (ch
<= 0x57)) {
5574 if (vt52Mode
== true) {
5575 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
5583 if (vt52Mode
== true) {
5585 // Send string directly to remote side
5586 writeRemote("\033/Z");
5589 // Send string directly to remote side
5590 writeRemote(deviceTypeResponse());
5601 // VT52 cannot get to any of these other states
5602 if (vt52Mode
== true) {
5607 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
5614 // RIS - Reset to initial state
5616 // Do I clear screen too? I think so...
5617 eraseScreen(0, 0, height
- 1, width
- 1, false);
5618 cursorPosition(0, 0);
5632 if ((type
== DeviceType
.VT220
)
5633 || (type
== DeviceType
.XTERM
)) {
5635 // VT220 lockshift G2 into GL
5636 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
5641 if ((type
== DeviceType
.VT220
)
5642 || (type
== DeviceType
.XTERM
)) {
5644 // VT220 lockshift G3 into GL
5645 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
5663 if ((type
== DeviceType
.VT220
)
5664 || (type
== DeviceType
.XTERM
)) {
5666 // VT220 lockshift G3 into GR
5667 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
5672 if ((type
== DeviceType
.VT220
)
5673 || (type
== DeviceType
.XTERM
)) {
5675 // VT220 lockshift G2 into GR
5676 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
5682 if ((type
== DeviceType
.VT220
)
5683 || (type
== DeviceType
.XTERM
)) {
5685 // VT220 lockshift G1 into GR
5686 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
5696 // 0x5B goes to CSI_ENTRY
5698 scanState
= ScanState
.CSI_ENTRY
;
5701 // 0x5D goes to OSC_STRING
5703 scanState
= ScanState
.OSC_STRING
;
5706 // 0x50 goes to DCS_ENTRY
5708 scanState
= ScanState
.DCS_ENTRY
;
5711 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5712 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
5713 scanState
= ScanState
.SOSPMAPC_STRING
;
5718 case ESCAPE_INTERMEDIATE
:
5719 // 00-17, 19, 1C-1F --> execute
5721 handleControlChar((char) ch
);
5724 // 20-2F --> collect
5725 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5729 // 30-7E --> dispatch, then switch to GROUND
5730 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
5733 if ((collectBuffer
.length() == 1)
5734 && (collectBuffer
.charAt(0) == '(')) {
5735 // G0 --> Special graphics
5736 currentState
.g0Charset
= CharacterSet
.DRAWING
;
5738 if ((collectBuffer
.length() == 1)
5739 && (collectBuffer
.charAt(0) == ')')) {
5740 // G1 --> Special graphics
5741 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5743 if ((type
== DeviceType
.VT220
)
5744 || (type
== DeviceType
.XTERM
)) {
5746 if ((collectBuffer
.length() == 1)
5747 && (collectBuffer
.charAt(0) == '*')) {
5748 // G2 --> Special graphics
5749 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5751 if ((collectBuffer
.length() == 1)
5752 && (collectBuffer
.charAt(0) == '+')) {
5753 // G3 --> Special graphics
5754 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5759 if ((collectBuffer
.length() == 1)
5760 && (collectBuffer
.charAt(0) == '(')) {
5761 // G0 --> Alternate character ROM standard character set
5762 currentState
.g0Charset
= CharacterSet
.ROM
;
5764 if ((collectBuffer
.length() == 1)
5765 && (collectBuffer
.charAt(0) == ')')) {
5766 // G1 --> Alternate character ROM standard character set
5767 currentState
.g1Charset
= CharacterSet
.ROM
;
5771 if ((collectBuffer
.length() == 1)
5772 && (collectBuffer
.charAt(0) == '(')) {
5773 // G0 --> Alternate character ROM special graphics
5774 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5776 if ((collectBuffer
.length() == 1)
5777 && (collectBuffer
.charAt(0) == ')')) {
5778 // G1 --> Alternate character ROM special graphics
5779 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5783 if ((collectBuffer
.length() == 1)
5784 && (collectBuffer
.charAt(0) == '#')) {
5785 // DECDHL - Double-height line (top half)
5790 if ((collectBuffer
.length() == 1)
5791 && (collectBuffer
.charAt(0) == '#')) {
5792 // DECDHL - Double-height line (bottom half)
5795 if ((type
== DeviceType
.VT220
)
5796 || (type
== DeviceType
.XTERM
)) {
5798 if ((collectBuffer
.length() == 1)
5799 && (collectBuffer
.charAt(0) == '(')) {
5801 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5803 if ((collectBuffer
.length() == 1)
5804 && (collectBuffer
.charAt(0) == ')')) {
5806 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5808 if ((collectBuffer
.length() == 1)
5809 && (collectBuffer
.charAt(0) == '*')) {
5811 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5813 if ((collectBuffer
.length() == 1)
5814 && (collectBuffer
.charAt(0) == '+')) {
5816 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5821 if ((collectBuffer
.length() == 1)
5822 && (collectBuffer
.charAt(0) == '#')) {
5823 // DECSWL - Single-width line
5826 if ((type
== DeviceType
.VT220
)
5827 || (type
== DeviceType
.XTERM
)) {
5829 if ((collectBuffer
.length() == 1)
5830 && (collectBuffer
.charAt(0) == '(')) {
5832 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5834 if ((collectBuffer
.length() == 1)
5835 && (collectBuffer
.charAt(0) == ')')) {
5837 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5839 if ((collectBuffer
.length() == 1)
5840 && (collectBuffer
.charAt(0) == '*')) {
5842 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5844 if ((collectBuffer
.length() == 1)
5845 && (collectBuffer
.charAt(0) == '+')) {
5847 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5852 if ((collectBuffer
.length() == 1)
5853 && (collectBuffer
.charAt(0) == '#')) {
5854 // DECDWL - Double-width line
5857 if ((type
== DeviceType
.VT220
)
5858 || (type
== DeviceType
.XTERM
)) {
5860 if ((collectBuffer
.length() == 1)
5861 && (collectBuffer
.charAt(0) == '(')) {
5863 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5865 if ((collectBuffer
.length() == 1)
5866 && (collectBuffer
.charAt(0) == ')')) {
5868 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5870 if ((collectBuffer
.length() == 1)
5871 && (collectBuffer
.charAt(0) == '*')) {
5873 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5875 if ((collectBuffer
.length() == 1)
5876 && (collectBuffer
.charAt(0) == '+')) {
5878 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5883 if ((type
== DeviceType
.VT220
)
5884 || (type
== DeviceType
.XTERM
)) {
5886 if ((collectBuffer
.length() == 1)
5887 && (collectBuffer
.charAt(0) == '(')) {
5889 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5891 if ((collectBuffer
.length() == 1)
5892 && (collectBuffer
.charAt(0) == ')')) {
5894 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5896 if ((collectBuffer
.length() == 1)
5897 && (collectBuffer
.charAt(0) == '*')) {
5899 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5901 if ((collectBuffer
.length() == 1)
5902 && (collectBuffer
.charAt(0) == '+')) {
5904 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5909 if ((collectBuffer
.length() == 1)
5910 && (collectBuffer
.charAt(0) == '#')) {
5911 // DECALN - Screen alignment display
5920 if ((type
== DeviceType
.VT220
)
5921 || (type
== DeviceType
.XTERM
)) {
5923 if ((collectBuffer
.length() == 1)
5924 && (collectBuffer
.charAt(0) == '(')) {
5925 // G0 --> DEC_SUPPLEMENTAL
5926 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5928 if ((collectBuffer
.length() == 1)
5929 && (collectBuffer
.charAt(0) == ')')) {
5930 // G1 --> DEC_SUPPLEMENTAL
5931 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5933 if ((collectBuffer
.length() == 1)
5934 && (collectBuffer
.charAt(0) == '*')) {
5935 // G2 --> DEC_SUPPLEMENTAL
5936 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5938 if ((collectBuffer
.length() == 1)
5939 && (collectBuffer
.charAt(0) == '+')) {
5940 // G3 --> DEC_SUPPLEMENTAL
5941 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5946 if ((type
== DeviceType
.VT220
)
5947 || (type
== DeviceType
.XTERM
)) {
5949 if ((collectBuffer
.length() == 1)
5950 && (collectBuffer
.charAt(0) == '(')) {
5952 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5954 if ((collectBuffer
.length() == 1)
5955 && (collectBuffer
.charAt(0) == ')')) {
5957 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5959 if ((collectBuffer
.length() == 1)
5960 && (collectBuffer
.charAt(0) == '*')) {
5962 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5964 if ((collectBuffer
.length() == 1)
5965 && (collectBuffer
.charAt(0) == '+')) {
5967 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5976 if ((collectBuffer
.length() == 1)
5977 && (collectBuffer
.charAt(0) == '(')) {
5978 // G0 --> United Kingdom set
5979 currentState
.g0Charset
= CharacterSet
.UK
;
5981 if ((collectBuffer
.length() == 1)
5982 && (collectBuffer
.charAt(0) == ')')) {
5983 // G1 --> United Kingdom set
5984 currentState
.g1Charset
= CharacterSet
.UK
;
5986 if ((type
== DeviceType
.VT220
)
5987 || (type
== DeviceType
.XTERM
)) {
5989 if ((collectBuffer
.length() == 1)
5990 && (collectBuffer
.charAt(0) == '*')) {
5991 // G2 --> United Kingdom set
5992 currentState
.g2Charset
= CharacterSet
.UK
;
5994 if ((collectBuffer
.length() == 1)
5995 && (collectBuffer
.charAt(0) == '+')) {
5996 // G3 --> United Kingdom set
5997 currentState
.g3Charset
= CharacterSet
.UK
;
6002 if ((collectBuffer
.length() == 1)
6003 && (collectBuffer
.charAt(0) == '(')) {
6005 currentState
.g0Charset
= CharacterSet
.US
;
6007 if ((collectBuffer
.length() == 1)
6008 && (collectBuffer
.charAt(0) == ')')) {
6010 currentState
.g1Charset
= CharacterSet
.US
;
6012 if ((type
== DeviceType
.VT220
)
6013 || (type
== DeviceType
.XTERM
)) {
6015 if ((collectBuffer
.length() == 1)
6016 && (collectBuffer
.charAt(0) == '*')) {
6018 currentState
.g2Charset
= CharacterSet
.US
;
6020 if ((collectBuffer
.length() == 1)
6021 && (collectBuffer
.charAt(0) == '+')) {
6023 currentState
.g3Charset
= CharacterSet
.US
;
6028 if ((type
== DeviceType
.VT220
)
6029 || (type
== DeviceType
.XTERM
)) {
6031 if ((collectBuffer
.length() == 1)
6032 && (collectBuffer
.charAt(0) == '(')) {
6034 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
6036 if ((collectBuffer
.length() == 1)
6037 && (collectBuffer
.charAt(0) == ')')) {
6039 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
6041 if ((collectBuffer
.length() == 1)
6042 && (collectBuffer
.charAt(0) == '*')) {
6044 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
6046 if ((collectBuffer
.length() == 1)
6047 && (collectBuffer
.charAt(0) == '+')) {
6049 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
6056 if ((type
== DeviceType
.VT220
)
6057 || (type
== DeviceType
.XTERM
)) {
6059 if ((collectBuffer
.length() == 1)
6060 && (collectBuffer
.charAt(0) == '(')) {
6062 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
6064 if ((collectBuffer
.length() == 1)
6065 && (collectBuffer
.charAt(0) == ')')) {
6067 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
6069 if ((collectBuffer
.length() == 1)
6070 && (collectBuffer
.charAt(0) == '*')) {
6072 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
6074 if ((collectBuffer
.length() == 1)
6075 && (collectBuffer
.charAt(0) == '+')) {
6077 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
6082 if ((type
== DeviceType
.VT220
)
6083 || (type
== DeviceType
.XTERM
)) {
6085 if ((collectBuffer
.length() == 1)
6086 && (collectBuffer
.charAt(0) == ' ')) {
6093 if ((type
== DeviceType
.VT220
)
6094 || (type
== DeviceType
.XTERM
)) {
6096 if ((collectBuffer
.length() == 1)
6097 && (collectBuffer
.charAt(0) == ' ')) {
6104 if ((type
== DeviceType
.VT220
)
6105 || (type
== DeviceType
.XTERM
)) {
6107 if ((collectBuffer
.length() == 1)
6108 && (collectBuffer
.charAt(0) == '(')) {
6110 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
6112 if ((collectBuffer
.length() == 1)
6113 && (collectBuffer
.charAt(0) == ')')) {
6115 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
6117 if ((collectBuffer
.length() == 1)
6118 && (collectBuffer
.charAt(0) == '*')) {
6120 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
6122 if ((collectBuffer
.length() == 1)
6123 && (collectBuffer
.charAt(0) == '+')) {
6125 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
6133 if ((type
== DeviceType
.VT220
)
6134 || (type
== DeviceType
.XTERM
)) {
6136 if ((collectBuffer
.length() == 1)
6137 && (collectBuffer
.charAt(0) == '(')) {
6139 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
6141 if ((collectBuffer
.length() == 1)
6142 && (collectBuffer
.charAt(0) == ')')) {
6144 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
6146 if ((collectBuffer
.length() == 1)
6147 && (collectBuffer
.charAt(0) == '*')) {
6149 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
6151 if ((collectBuffer
.length() == 1)
6152 && (collectBuffer
.charAt(0) == '+')) {
6154 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
6165 if ((type
== DeviceType
.VT220
)
6166 || (type
== DeviceType
.XTERM
)) {
6168 if ((collectBuffer
.length() == 1)
6169 && (collectBuffer
.charAt(0) == '(')) {
6171 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
6173 if ((collectBuffer
.length() == 1)
6174 && (collectBuffer
.charAt(0) == ')')) {
6176 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
6178 if ((collectBuffer
.length() == 1)
6179 && (collectBuffer
.charAt(0) == '*')) {
6181 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
6183 if ((collectBuffer
.length() == 1)
6184 && (collectBuffer
.charAt(0) == '+')) {
6186 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
6191 if ((type
== DeviceType
.VT220
)
6192 || (type
== DeviceType
.XTERM
)) {
6194 if ((collectBuffer
.length() == 1)
6195 && (collectBuffer
.charAt(0) == '(')) {
6197 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
6199 if ((collectBuffer
.length() == 1)
6200 && (collectBuffer
.charAt(0) == ')')) {
6202 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
6204 if ((collectBuffer
.length() == 1)
6205 && (collectBuffer
.charAt(0) == '*')) {
6207 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
6209 if ((collectBuffer
.length() == 1)
6210 && (collectBuffer
.charAt(0) == '+')) {
6212 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
6224 if ((type
== DeviceType
.VT220
)
6225 || (type
== DeviceType
.XTERM
)) {
6227 if ((collectBuffer
.length() == 1)
6228 && (collectBuffer
.charAt(0) == '(')) {
6230 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
6232 if ((collectBuffer
.length() == 1)
6233 && (collectBuffer
.charAt(0) == ')')) {
6235 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
6237 if ((collectBuffer
.length() == 1)
6238 && (collectBuffer
.charAt(0) == '*')) {
6240 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
6242 if ((collectBuffer
.length() == 1)
6243 && (collectBuffer
.charAt(0) == '+')) {
6245 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
6250 if ((type
== DeviceType
.VT220
)
6251 || (type
== DeviceType
.XTERM
)) {
6253 if ((collectBuffer
.length() == 1)
6254 && (collectBuffer
.charAt(0) == '(')) {
6256 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
6258 if ((collectBuffer
.length() == 1)
6259 && (collectBuffer
.charAt(0) == ')')) {
6261 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
6263 if ((collectBuffer
.length() == 1)
6264 && (collectBuffer
.charAt(0) == '*')) {
6266 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
6268 if ((collectBuffer
.length() == 1)
6269 && (collectBuffer
.charAt(0) == '+')) {
6271 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
6318 // 0x9C goes to GROUND
6326 // 00-17, 19, 1C-1F --> execute
6328 handleControlChar((char) ch
);
6331 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6332 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6334 scanState
= ScanState
.CSI_INTERMEDIATE
;
6337 // 30-39, 3B --> param, then switch to CSI_PARAM
6338 if ((ch
>= '0') && (ch
<= '9')) {
6340 scanState
= ScanState
.CSI_PARAM
;
6344 scanState
= ScanState
.CSI_PARAM
;
6347 // 3C-3F --> collect, then switch to CSI_PARAM
6348 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6350 scanState
= ScanState
.CSI_PARAM
;
6353 // 40-7E --> dispatch, then switch to GROUND
6354 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6357 // ICH - Insert character
6365 // CUD - Cursor down
6369 // CUF - Cursor forward
6373 // CUB - Cursor backward
6377 // CNL - Cursor down and to column 1
6378 if (type
== DeviceType
.XTERM
) {
6383 // CPL - Cursor up and to column 1
6384 if (type
== DeviceType
.XTERM
) {
6389 // CHA - Cursor to column # in current row
6390 if (type
== DeviceType
.XTERM
) {
6395 // CUP - Cursor position
6399 // CHT - Cursor forward X tab stops (default 1)
6400 if (type
== DeviceType
.XTERM
) {
6405 // ED - Erase in display
6409 // EL - Erase in line
6424 // DCH - Delete character
6431 // Scroll up X lines (default 1)
6432 if (type
== DeviceType
.XTERM
) {
6433 boolean xtermPrivateModeFlag
= false;
6434 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6435 if (collectBuffer
.charAt(i
) == '?') {
6436 xtermPrivateModeFlag
= true;
6440 if (xtermPrivateModeFlag
) {
6448 // Scroll down X lines (default 1)
6449 if (type
== DeviceType
.XTERM
) {
6458 if ((type
== DeviceType
.VT220
)
6459 || (type
== DeviceType
.XTERM
)) {
6461 // ECH - Erase character
6468 // CBT - Cursor backward X tab stops (default 1)
6469 if (type
== DeviceType
.XTERM
) {
6480 // HPA - Cursor to column # in current row. Same as CHA
6481 if (type
== DeviceType
.XTERM
) {
6486 // HPR - Cursor right. Same as CUF
6487 if (type
== DeviceType
.XTERM
) {
6492 // REP - Repeat last char X times
6493 if (type
== DeviceType
.XTERM
) {
6498 // DA - Device attributes
6502 // VPA - Cursor to row, current column.
6503 if (type
== DeviceType
.XTERM
) {
6508 // VPR - Cursor down. Same as CUD
6509 if (type
== DeviceType
.XTERM
) {
6514 // HVP - Horizontal and vertical position
6518 // TBC - Tabulation clear
6522 // Sets an ANSI or DEC private toggle
6526 if ((type
== DeviceType
.VT220
)
6527 || (type
== DeviceType
.XTERM
)) {
6529 // Printer functions
6537 // Sets an ANSI or DEC private toggle
6541 // SGR - Select graphics rendition
6545 // DSR - Device status report
6552 // DECLL - Load leds
6556 // DECSTBM - Set top and bottom margins
6560 // Save cursor (ANSI.SYS)
6561 if (type
== DeviceType
.XTERM
) {
6562 savedState
.cursorX
= currentState
.cursorX
;
6563 savedState
.cursorY
= currentState
.cursorY
;
6567 if (type
== DeviceType
.XTERM
) {
6568 // Window operations
6573 // Restore cursor (ANSI.SYS)
6574 if (type
== DeviceType
.XTERM
) {
6575 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
6582 // DECREQTPARM - Request terminal parameters
6598 // 0x9C goes to GROUND
6603 // 0x3A goes to CSI_IGNORE
6605 scanState
= ScanState
.CSI_IGNORE
;
6610 // 00-17, 19, 1C-1F --> execute
6612 handleControlChar((char) ch
);
6615 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6616 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6618 scanState
= ScanState
.CSI_INTERMEDIATE
;
6621 // 30-39, 3B --> param
6622 if ((ch
>= '0') && (ch
<= '9')) {
6629 // 0x3A goes to CSI_IGNORE
6631 scanState
= ScanState
.CSI_IGNORE
;
6633 // 0x3C-3F goes to CSI_IGNORE
6634 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6635 scanState
= ScanState
.CSI_IGNORE
;
6638 // 40-7E --> dispatch, then switch to GROUND
6639 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6642 // ICH - Insert character
6650 // CUD - Cursor down
6654 // CUF - Cursor forward
6658 // CUB - Cursor backward
6662 // CNL - Cursor down and to column 1
6663 if (type
== DeviceType
.XTERM
) {
6668 // CPL - Cursor up and to column 1
6669 if (type
== DeviceType
.XTERM
) {
6674 // CHA - Cursor to column # in current row
6675 if (type
== DeviceType
.XTERM
) {
6680 // CUP - Cursor position
6684 // CHT - Cursor forward X tab stops (default 1)
6685 if (type
== DeviceType
.XTERM
) {
6690 // ED - Erase in display
6694 // EL - Erase in line
6709 // DCH - Delete character
6716 // Scroll up X lines (default 1)
6717 if (type
== DeviceType
.XTERM
) {
6718 boolean xtermPrivateModeFlag
= false;
6719 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6720 if (collectBuffer
.charAt(i
) == '?') {
6721 xtermPrivateModeFlag
= true;
6725 if (xtermPrivateModeFlag
) {
6733 // Scroll down X lines (default 1)
6734 if (type
== DeviceType
.XTERM
) {
6743 if ((type
== DeviceType
.VT220
)
6744 || (type
== DeviceType
.XTERM
)) {
6746 // ECH - Erase character
6753 // CBT - Cursor backward X tab stops (default 1)
6754 if (type
== DeviceType
.XTERM
) {
6765 // HPA - Cursor to column # in current row. Same as CHA
6766 if (type
== DeviceType
.XTERM
) {
6771 // HPR - Cursor right. Same as CUF
6772 if (type
== DeviceType
.XTERM
) {
6777 // REP - Repeat last char X times
6778 if (type
== DeviceType
.XTERM
) {
6783 // DA - Device attributes
6787 // VPA - Cursor to row, current column.
6788 if (type
== DeviceType
.XTERM
) {
6793 // VPR - Cursor down. Same as CUD
6794 if (type
== DeviceType
.XTERM
) {
6799 // HVP - Horizontal and vertical position
6803 // TBC - Tabulation clear
6807 // Sets an ANSI or DEC private toggle
6811 if ((type
== DeviceType
.VT220
)
6812 || (type
== DeviceType
.XTERM
)) {
6814 // Printer functions
6822 // Sets an ANSI or DEC private toggle
6826 // SGR - Select graphics rendition
6830 // DSR - Device status report
6837 // DECLL - Load leds
6841 // DECSTBM - Set top and bottom margins
6847 if (type
== DeviceType
.XTERM
) {
6848 // Window operations
6857 // DECREQTPARM - Request terminal parameters
6874 case CSI_INTERMEDIATE
:
6875 // 00-17, 19, 1C-1F --> execute
6877 handleControlChar((char) ch
);
6880 // 20-2F --> collect
6881 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6885 // 0x30-3F goes to CSI_IGNORE
6886 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6887 scanState
= ScanState
.CSI_IGNORE
;
6890 // 40-7E --> dispatch, then switch to GROUND
6891 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6943 if (((type
== DeviceType
.VT220
)
6944 || (type
== DeviceType
.XTERM
))
6945 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6947 // DECSCL - compatibility level
6950 if ((type
== DeviceType
.XTERM
)
6951 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6953 // DECSTR - Soft terminal reset
6958 if (((type
== DeviceType
.VT220
)
6959 || (type
== DeviceType
.XTERM
))
6960 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6988 // 00-17, 19, 1C-1F --> execute
6990 handleControlChar((char) ch
);
6993 // 20-2F --> collect
6994 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6998 // 40-7E --> ignore, then switch to GROUND
6999 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
7003 // 20-3F, 7F --> ignore
7009 // 0x9C goes to GROUND
7014 // 0x1B 0x5C goes to GROUND
7019 if ((collectBuffer
.length() > 0)
7020 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
7026 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
7027 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
7029 scanState
= ScanState
.DCS_INTERMEDIATE
;
7032 // 30-39, 3B --> param, then switch to DCS_PARAM
7033 if ((ch
>= '0') && (ch
<= '9')) {
7035 scanState
= ScanState
.DCS_PARAM
;
7039 scanState
= ScanState
.DCS_PARAM
;
7042 // 3C-3F --> collect, then switch to DCS_PARAM
7043 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
7045 scanState
= ScanState
.DCS_PARAM
;
7048 // 00-17, 19, 1C-1F, 7F --> ignore
7050 // 0x3A goes to DCS_IGNORE
7052 scanState
= ScanState
.DCS_IGNORE
;
7055 // 0x71 goes to DCS_SIXEL
7057 sixelParseBuffer
.setLength(0);
7058 scanState
= ScanState
.DCS_SIXEL
;
7059 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
7060 // 0x40-7E goes to DCS_PASSTHROUGH
7061 scanState
= ScanState
.DCS_PASSTHROUGH
;
7065 case DCS_INTERMEDIATE
:
7067 // 0x9C goes to GROUND
7072 // 0x1B 0x5C goes to GROUND
7077 if ((collectBuffer
.length() > 0)
7078 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
7084 // 0x30-3F goes to DCS_IGNORE
7085 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
7086 scanState
= ScanState
.DCS_IGNORE
;
7089 // 0x40-7E goes to DCS_PASSTHROUGH
7090 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
7091 scanState
= ScanState
.DCS_PASSTHROUGH
;
7094 // 00-17, 19, 1C-1F, 7F --> ignore
7099 // 0x9C goes to GROUND
7104 // 0x1B 0x5C goes to GROUND
7109 if ((collectBuffer
.length() > 0)
7110 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
7116 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
7117 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
7119 scanState
= ScanState
.DCS_INTERMEDIATE
;
7122 // 30-39, 3B --> param
7123 if ((ch
>= '0') && (ch
<= '9')) {
7130 // 00-17, 19, 1C-1F, 7F --> ignore
7132 // 0x3A, 3C-3F goes to DCS_IGNORE
7134 scanState
= ScanState
.DCS_IGNORE
;
7136 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
7137 scanState
= ScanState
.DCS_IGNORE
;
7140 // 0x71 goes to DCS_SIXEL
7142 sixelParseBuffer
.setLength(0);
7143 scanState
= ScanState
.DCS_SIXEL
;
7144 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
7145 // 0x40-7E goes to DCS_PASSTHROUGH
7146 scanState
= ScanState
.DCS_PASSTHROUGH
;
7150 case DCS_PASSTHROUGH
:
7151 // 0x9C goes to GROUND
7156 // 0x1B 0x5C goes to GROUND
7161 if ((collectBuffer
.length() > 0)
7162 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
7168 // 00-17, 19, 1C-1F, 20-7E --> put
7170 // We ignore all DCS except sixel.
7174 // We ignore all DCS except sixel.
7177 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
7178 // We ignore all DCS except sixel.
7181 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
7182 // We ignore all DCS except sixel.
7191 // 00-17, 19, 1C-1F, 20-7F --> ignore
7193 // 0x9C goes to GROUND
7201 // 0x9C goes to GROUND
7208 // 0x1B 0x5C goes to GROUND
7214 if ((collectBuffer
.length() > 0)
7215 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
7223 // 00-17, 19, 1C-1F, 20-7E --> put
7226 || ((ch
>= 0x1C) && (ch
<= 0x1F))
7227 || ((ch
>= 0x20) && (ch
<= 0x7E))
7229 sixelParseBuffer
.append((char) ch
);
7235 case SOSPMAPC_STRING
:
7236 // 00-17, 19, 1C-1F, 20-7F --> ignore
7238 // Special case for Jexer: PM can pass one control character
7243 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
7247 // 0x9C goes to GROUND
7255 // Special case for Xterm: OSC can pass control characters
7256 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
7260 // 00-17, 19, 1C-1F --> ignore
7262 // 20-7F --> osc_put
7263 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
7267 // 0x9C goes to GROUND
7274 case VT52_DIRECT_CURSOR_ADDRESS
:
7275 // This is a special case for the VT52 sequence "ESC Y l c"
7276 if (collectBuffer
.length() == 0) {
7278 } else if (collectBuffer
.length() == 1) {
7279 // We've got the two characters, one in the buffer and the
7281 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
7290 * Expose current cursor X to outside world.
7292 * @return current cursor X
7294 public final int getCursorX() {
7295 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
7296 return currentState
.cursorX
* 2;
7298 return currentState
.cursorX
;
7302 * Expose current cursor Y to outside world.
7304 * @return current cursor Y
7306 public final int getCursorY() {
7307 return currentState
.cursorY
;
7311 * Returns true if this terminal has requested the mouse pointer be
7314 * @return true if this terminal has requested the mouse pointer be
7317 public final boolean hasHiddenMousePointer() {
7318 return hideMousePointer
;
7322 * Get the mouse protocol.
7324 * @return MouseProtocol.OFF, MouseProtocol.X10, etc.
7326 public MouseProtocol
getMouseProtocol() {
7327 return mouseProtocol
;
7331 * Draw the left and right cells of a two-cell-wide (full-width) glyph.
7333 * @param leftX the x position to draw the left half to
7334 * @param leftY the y position to draw the left half to
7335 * @param rightX the x position to draw the right half to
7336 * @param rightY the y position to draw the right half to
7337 * @param ch the character to draw
7339 private void drawHalves(final int leftX
, final int leftY
,
7340 final int rightX
, final int rightY
, final int ch
) {
7342 // System.err.println("drawHalves(): " + Integer.toHexString(ch));
7344 if (lastTextHeight
!= textHeight
) {
7345 glyphMaker
= GlyphMaker
.getInstance(textHeight
);
7346 lastTextHeight
= textHeight
;
7349 Cell cell
= new Cell(ch
, currentState
.attr
);
7350 BufferedImage image
= glyphMaker
.getImage(cell
, textWidth
* 2,
7352 BufferedImage leftImage
= image
.getSubimage(0, 0, textWidth
,
7354 BufferedImage rightImage
= image
.getSubimage(textWidth
, 0, textWidth
,
7357 Cell left
= new Cell(cell
);
7358 left
.setImage(leftImage
);
7359 left
.setWidth(Cell
.Width
.LEFT
);
7360 display
.get(leftY
).replace(leftX
, left
);
7362 Cell right
= new Cell(cell
);
7363 right
.setImage(rightImage
);
7364 right
.setWidth(Cell
.Width
.RIGHT
);
7365 display
.get(rightY
).replace(rightX
, right
);
7369 * Set the width of a character cell in pixels.
7371 * @param textWidth the width in pixels of a character cell
7373 public void setTextWidth(final int textWidth
) {
7374 this.textWidth
= textWidth
;
7378 * Set the height of a character cell in pixels.
7380 * @param textHeight the height in pixels of a character cell
7382 public void setTextHeight(final int textHeight
) {
7383 this.textHeight
= textHeight
;
7387 * Parse a sixel string into a bitmap image, and overlay that image onto
7390 private void parseSixel() {
7393 System.err.println("parseSixel(): '" + sixelParseBuffer.toString()
7397 Sixel sixel
= new Sixel(sixelParseBuffer
.toString(), sixelPalette
);
7398 BufferedImage image
= sixel
.getImage();
7400 // System.err.println("parseSixel(): image " + image);
7402 if (image
== null) {
7403 // Sixel data was malformed in some way, bail out.
7406 if ((image
.getWidth() < 1)
7407 || (image
.getWidth() > 10000)
7408 || (image
.getHeight() < 1)
7409 || (image
.getHeight() > 10000)
7414 imageToCells(image
, true);
7418 * Parse a "Jexer" RGB image string into a bitmap image, and overlay that
7419 * image onto the text cells.
7421 * @param pw width token
7422 * @param ph height token
7423 * @param ps scroll token
7424 * @param data pixel data
7426 private void parseJexerImageRGB(final String pw
, final String ph
,
7427 final String ps
, final String data
) {
7430 int imageHeight
= 0;
7431 boolean scroll
= false;
7433 imageWidth
= Integer
.parseInt(pw
);
7434 imageHeight
= Integer
.parseInt(ph
);
7435 } catch (NumberFormatException e
) {
7439 if ((imageWidth
< 1)
7440 || (imageWidth
> 10000)
7441 || (imageHeight
< 1)
7442 || (imageHeight
> 10000)
7446 if (ps
.equals("1")) {
7448 } else if (ps
.equals("0")) {
7454 byte [] bytes
= StringUtils
.fromBase64(data
.getBytes());
7455 if (bytes
.length
!= (imageWidth
* imageHeight
* 3)) {
7459 BufferedImage image
= new BufferedImage(imageWidth
, imageHeight
,
7460 BufferedImage
.TYPE_INT_ARGB
);
7462 for (int x
= 0; x
< imageWidth
; x
++) {
7463 for (int y
= 0; y
< imageHeight
; y
++) {
7464 int red
= bytes
[(y
* imageWidth
* 3) + (x
* 3) ];
7468 int green
= bytes
[(y
* imageWidth
* 3) + (x
* 3) + 1];
7472 int blue
= bytes
[(y
* imageWidth
* 3) + (x
* 3) + 2];
7476 int rgb
= 0xFF000000 | (red
<< 16) | (green
<< 8) | blue
;
7477 image
.setRGB(x
, y
, rgb
);
7481 imageToCells(image
, scroll
);
7485 * Parse a "Jexer" PNG or JPG image string into a bitmap image, and
7486 * overlay that image onto the text cells.
7488 * @param type 1 for PNG, 2 for JPG
7489 * @param ps scroll token
7490 * @param data pixel data
7492 private void parseJexerImageFile(final int type
, final String ps
,
7493 final String data
) {
7496 int imageHeight
= 0;
7497 boolean scroll
= false;
7498 BufferedImage image
= null;
7500 byte [] bytes
= StringUtils
.fromBase64(data
.getBytes());
7504 if ((bytes
[0] != (byte) 0x89)
7505 || (bytes
[1] != 'P')
7506 || (bytes
[2] != 'N')
7507 || (bytes
[3] != 'G')
7508 || (bytes
[4] != (byte) 0x0D)
7509 || (bytes
[5] != (byte) 0x0A)
7510 || (bytes
[6] != (byte) 0x1A)
7511 || (bytes
[7] != (byte) 0x0A)
7513 // File does not have PNG header, bail out.
7519 if ((bytes
[0] != (byte) 0XFF)
7520 || (bytes
[1] != (byte) 0xD8)
7521 || (bytes
[2] != (byte) 0xFF)
7523 // File does not have JPG header, bail out.
7529 // Unsupported type, bail out.
7533 image
= ImageIO
.read(new ByteArrayInputStream(bytes
));
7534 } catch (IOException e
) {
7538 assert (image
!= null);
7539 imageWidth
= image
.getWidth();
7540 imageHeight
= image
.getHeight();
7541 if ((imageWidth
< 1)
7542 || (imageWidth
> 10000)
7543 || (imageHeight
< 1)
7544 || (imageHeight
> 10000)
7548 if (ps
.equals("1")) {
7550 } else if (ps
.equals("0")) {
7556 imageToCells(image
, scroll
);
7560 * Break up an image into the cells at the current cursor.
7562 * @param image the image to display
7563 * @param scroll if true, scroll the image and move the cursor
7565 private void imageToCells(final BufferedImage image
, final boolean scroll
) {
7566 assert (image
!= null);
7571 * Break up the image into text cell sized pieces as a new array of
7574 * Note original column position x0.
7578 * 1. Advance (printCharacter(' ')) for horizontal increment, or
7579 * index (linefeed() + cursorPosition(y, x0)) for vertical
7582 * 2. Set (x, y) cell image data.
7584 * 3. For the right and bottom edges:
7586 * a. Render the text to pixels using Terminus font.
7588 * b. Blit the image on top of the text, using alpha channel.
7590 int cellColumns
= image
.getWidth() / textWidth
;
7591 if (cellColumns
* textWidth
< image
.getWidth()) {
7594 int cellRows
= image
.getHeight() / textHeight
;
7595 if (cellRows
* textHeight
< image
.getHeight()) {
7599 // Break the image up into an array of cells.
7600 Cell
[][] cells
= new Cell
[cellColumns
][cellRows
];
7602 for (int x
= 0; x
< cellColumns
; x
++) {
7603 for (int y
= 0; y
< cellRows
; y
++) {
7605 int width
= textWidth
;
7606 if ((x
+ 1) * textWidth
> image
.getWidth()) {
7607 width
= image
.getWidth() - (x
* textWidth
);
7609 int height
= textHeight
;
7610 if ((y
+ 1) * textHeight
> image
.getHeight()) {
7611 height
= image
.getHeight() - (y
* textHeight
);
7614 Cell cell
= new Cell();
7615 if ((width
!= textWidth
) || (height
!= textHeight
)) {
7616 BufferedImage newImage
;
7617 newImage
= new BufferedImage(textWidth
, textHeight
,
7618 BufferedImage
.TYPE_INT_ARGB
);
7620 Graphics gr
= newImage
.getGraphics();
7621 gr
.drawImage(image
.getSubimage(x
* textWidth
,
7622 y
* textHeight
, width
, height
),
7625 cell
.setImage(newImage
);
7627 cell
.setImage(image
.getSubimage(x
* textWidth
,
7628 y
* textHeight
, width
, height
));
7635 int x0
= currentState
.cursorX
;
7636 int y0
= currentState
.cursorY
;
7637 for (int y
= 0; y
< cellRows
; y
++) {
7638 for (int x
= 0; x
< cellColumns
; x
++) {
7639 assert (currentState
.cursorX
<= rightMargin
);
7641 // A real sixel terminal would render the text of the current
7642 // cell first, then image over it (accounting for blank
7643 // pixels). We do not support that. A cell is either text,
7644 // or image, but not a mix of image-over-text.
7645 DisplayLine line
= display
.get(currentState
.cursorY
);
7646 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
7648 // If at the end of the visible screen, stop.
7649 if (currentState
.cursorX
== rightMargin
) {
7652 // Room for more image on the visible screen.
7653 currentState
.cursorX
++;
7655 if (currentState
.cursorY
< scrollRegionBottom
- 1) {
7656 // Not at the bottom, down a line.
7658 } else if (scroll
== true) {
7659 // At the bottom, scroll as needed.
7662 // At the bottom, no more scrolling, done.
7666 cursorPosition(currentState
.cursorY
, x0
);
7669 if (scroll
== false) {
7670 cursorPosition(y0
, x0
);