2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.tterminal
;
31 import java
.awt
.Graphics2D
;
32 import java
.awt
.image
.BufferedImage
;
33 import java
.io
.BufferedOutputStream
;
34 import java
.io
.CharArrayWriter
;
35 import java
.io
.InputStream
;
36 import java
.io
.InputStreamReader
;
37 import java
.io
.IOException
;
38 import java
.io
.OutputStream
;
39 import java
.io
.OutputStreamWriter
;
40 import java
.io
.PrintWriter
;
41 import java
.io
.Reader
;
42 import java
.io
.UnsupportedEncodingException
;
43 import java
.io
.Writer
;
44 import java
.util
.ArrayList
;
45 import java
.util
.Collections
;
46 import java
.util
.HashMap
;
47 import java
.util
.List
;
49 import jexer
.TKeypress
;
50 import jexer
.backend
.GlyphMaker
;
51 import jexer
.bits
.Color
;
52 import jexer
.bits
.Cell
;
53 import jexer
.bits
.CellAttributes
;
54 import jexer
.bits
.StringUtils
;
55 import jexer
.event
.TInputEvent
;
56 import jexer
.event
.TKeypressEvent
;
57 import jexer
.event
.TMouseEvent
;
58 import jexer
.io
.ReadTimeoutException
;
59 import jexer
.io
.TimeoutInputStream
;
60 import static jexer
.TKeypress
.*;
63 * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
64 * including a scrollback buffer.
67 * It currently implements VT100, VT102, VT220, and XTERM with the following
71 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
72 * failure with XTERM. This is due to vttest failing to decode the UTF-8
76 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
77 * from VT100 are not supported.
80 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
81 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
82 * because DECSCL is not supported, it will fail the last part of the
83 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
86 * - Numeric/application keys from the number pad are not supported because
87 * they are not exposed from the TKeypress API.
90 * - VT52 HOLD SCREEN mode is not supported.
93 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
94 * numerators) are not rendered correctly.
97 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
99 public class ECMA48
implements Runnable
{
101 // ------------------------------------------------------------------------
102 // Constants --------------------------------------------------------------
103 // ------------------------------------------------------------------------
106 * The emulator can emulate several kinds of terminals.
108 public enum DeviceType
{
110 * DEC VT100 but also including the three VT102 functions.
131 * Parser character scan states.
133 private enum ScanState
{
149 VT52_DIRECT_CURSOR_ADDRESS
153 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
154 * can't really use it in keypress() because we do not see number pad
155 * events from TKeypress.
157 private enum KeypadMode
{
163 * Arrow keys can emit three different sequences (DECCKM or VT52
166 private enum ArrowKeyMode
{
173 * Available character sets for GL, GR, G0, G1, G2, G3.
175 private enum CharacterSet
{
196 * Single-shift states used by the C1 control characters SS2 (0x8E) and
199 private enum Singleshift
{
206 * VT220+ lockshift states.
208 private enum LockshiftMode
{
218 * XTERM mouse reporting protocols.
220 public enum MouseProtocol
{
229 * XTERM mouse reporting encodings.
231 private enum MouseEncoding
{
237 // ------------------------------------------------------------------------
238 // Variables --------------------------------------------------------------
239 // ------------------------------------------------------------------------
242 * The enclosing listening object.
244 private DisplayListener displayListener
;
247 * When true, the reader thread is expected to exit.
249 private volatile boolean stopReaderThread
= false;
254 private Thread readerThread
= null;
257 * The type of emulator to be.
259 private DeviceType type
= DeviceType
.VT102
;
262 * The scrollback buffer characters + attributes.
264 private volatile ArrayList
<DisplayLine
> scrollback
;
267 * The raw display buffer characters + attributes.
269 private volatile ArrayList
<DisplayLine
> display
;
272 * The maximum number of lines in the scrollback buffer.
274 private int maxScrollback
= 10000;
277 * The terminal's input. For type == XTERM, this is an InputStreamReader
278 * with UTF-8 encoding.
280 private Reader input
;
283 * The terminal's raw InputStream. This is used for type != XTERM.
285 private volatile TimeoutInputStream inputStream
;
288 * The terminal's output. For type == XTERM, this wraps an
289 * OutputStreamWriter with UTF-8 encoding.
291 private Writer output
;
294 * The terminal's raw OutputStream. This is used for type != XTERM.
296 private OutputStream outputStream
;
299 * Current scanning state.
301 private ScanState scanState
;
304 * Which mouse protocol is active.
306 private MouseProtocol mouseProtocol
= MouseProtocol
.OFF
;
309 * Which mouse encoding is active.
311 private MouseEncoding mouseEncoding
= MouseEncoding
.X10
;
314 * A terminal may request that the mouse pointer be hidden using a
315 * Privacy Message containing either "hideMousePointer" or
316 * "showMousePointer". This is currently only used within Jexer by
317 * TTerminalWindow so that only the bottom-most instance of nested
318 * Jexer's draws the mouse within its application window.
320 private boolean hideMousePointer
= false;
323 * Physical display width. We start at 80x24, but the user can resize us
329 * Physical display height. We start at 80x24, but the user can resize
335 * Top margin of the scrolling region.
337 private int scrollRegionTop
;
340 * Bottom margin of the scrolling region.
342 private int scrollRegionBottom
;
345 * Right margin column number. This can be selected by the remote side
346 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
348 private int rightMargin
;
351 * Last character printed.
356 * VT100-style line wrapping: a character is placed in column 80 (or
357 * 132), but the line does NOT wrap until another character is written to
358 * column 1 of the next line, after which the cursor moves to column 2.
360 private boolean wrapLineFlag
;
363 * VT220 single shift flag.
365 private Singleshift singleshift
= Singleshift
.NONE
;
368 * true = insert characters, false = overwrite.
370 private boolean insertMode
= false;
373 * VT52 mode as selected by DECANM. True means VT52, false means
374 * ANSI. Default is ANSI.
376 private boolean vt52Mode
= false;
379 * Visible cursor (DECTCEM).
381 private boolean cursorVisible
= true;
384 * Screen title as set by the xterm OSC sequence. Lots of applications
385 * send a screenTitle regardless of whether it is an xterm client or not.
387 private String screenTitle
= "";
390 * Parameter characters being collected.
392 private List
<Integer
> csiParams
;
395 * Non-csi collect buffer.
397 private StringBuilder collectBuffer
;
400 * When true, use the G1 character set.
402 private boolean shiftOut
= false;
405 * Horizontal tab stop locations.
407 private List
<Integer
> tabStops
;
410 * S8C1T. True means 8bit controls, false means 7bit controls.
412 private boolean s8c1t
= false;
415 * Printer mode. True means send all output to printer, which discards
418 private boolean printerControllerMode
= false;
421 * LMN line mode. If true, linefeed() puts the cursor on the first
422 * column of the next line. If false, linefeed() puts the cursor one
423 * line down on the current line. The default is false.
425 private boolean newLineMode
= false;
428 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
430 private ArrowKeyMode arrowKeyMode
;
433 * Whether number pad keys send VT100 or VT52, application or numeric
436 @SuppressWarnings("unused")
437 private KeypadMode keypadMode
;
440 * When true, the terminal is in 132-column mode (DECCOLM).
442 private boolean columns132
= false;
445 * true = reverse video. Set by DECSCNM.
447 private boolean reverseVideo
= false;
450 * false = echo characters locally.
452 private boolean fullDuplex
= true;
455 * The current terminal state.
457 private SaveableState currentState
;
460 * The last saved terminal state.
462 private SaveableState savedState
;
465 * The 88- or 256-color support RGB colors.
467 private List
<Integer
> colors88
;
470 * Sixel collection buffer.
472 private StringBuilder sixelParseBuffer
;
475 * The width of a character cell in pixels.
477 private int textWidth
= 16;
480 * The height of a character cell in pixels.
482 private int textHeight
= 20;
485 * The last used height of a character cell in pixels, only used for
488 private int lastTextHeight
= -1;
491 * The glyph drawer for full-width chars.
493 private GlyphMaker glyphMaker
= null;
496 * Input queue for keystrokes and mouse events to send to the remote
499 private ArrayList
<TInputEvent
> userQueue
= new ArrayList
<TInputEvent
>();
502 * DECSC/DECRC save/restore a subset of the total state. This class
503 * encapsulates those specific flags/modes.
505 private class SaveableState
{
508 * When true, cursor positions are relative to the scrolling region.
510 public boolean originMode
= false;
513 * The current editing X position.
515 public int cursorX
= 0;
518 * The current editing Y position.
520 public int cursorY
= 0;
523 * Which character set is currently selected in G0.
525 public CharacterSet g0Charset
= CharacterSet
.US
;
528 * Which character set is currently selected in G1.
530 public CharacterSet g1Charset
= CharacterSet
.DRAWING
;
533 * Which character set is currently selected in G2.
535 public CharacterSet g2Charset
= CharacterSet
.US
;
538 * Which character set is currently selected in G3.
540 public CharacterSet g3Charset
= CharacterSet
.US
;
543 * Which character set is currently selected in GR.
545 public CharacterSet grCharset
= CharacterSet
.DRAWING
;
548 * The current drawing attributes.
550 public CellAttributes attr
;
555 public LockshiftMode glLockshift
= LockshiftMode
.NONE
;
560 public LockshiftMode grLockshift
= LockshiftMode
.NONE
;
565 public boolean lineWrap
= true;
570 public void reset() {
574 g0Charset
= CharacterSet
.US
;
575 g1Charset
= CharacterSet
.DRAWING
;
576 g2Charset
= CharacterSet
.US
;
577 g3Charset
= CharacterSet
.US
;
578 grCharset
= CharacterSet
.DRAWING
;
579 attr
= new CellAttributes();
580 glLockshift
= LockshiftMode
.NONE
;
581 grLockshift
= LockshiftMode
.NONE
;
586 * Copy attributes from another instance.
588 * @param that the other instance to match
590 public void setTo(final SaveableState that
) {
591 this.originMode
= that
.originMode
;
592 this.cursorX
= that
.cursorX
;
593 this.cursorY
= that
.cursorY
;
594 this.g0Charset
= that
.g0Charset
;
595 this.g1Charset
= that
.g1Charset
;
596 this.g2Charset
= that
.g2Charset
;
597 this.g3Charset
= that
.g3Charset
;
598 this.grCharset
= that
.grCharset
;
599 this.attr
= new CellAttributes();
600 this.attr
.setTo(that
.attr
);
601 this.glLockshift
= that
.glLockshift
;
602 this.grLockshift
= that
.grLockshift
;
603 this.lineWrap
= that
.lineWrap
;
607 * Public constructor.
609 public SaveableState() {
614 // ------------------------------------------------------------------------
615 // Constructors -----------------------------------------------------------
616 // ------------------------------------------------------------------------
619 * Public constructor.
621 * @param type one of the DeviceType constants to select VT100, VT102,
623 * @param inputStream an InputStream connected to the remote side. For
624 * type == XTERM, inputStream is converted to a Reader with UTF-8
626 * @param outputStream an OutputStream connected to the remote user. For
627 * type == XTERM, outputStream is converted to a Writer with UTF-8
629 * @param displayListener a callback to the outer display, or null for
630 * default VT100 behavior
631 * @throws UnsupportedEncodingException if an exception is thrown when
632 * creating the InputStreamReader
634 public ECMA48(final DeviceType type
, final InputStream inputStream
,
635 final OutputStream outputStream
, final DisplayListener displayListener
)
636 throws UnsupportedEncodingException
{
638 assert (inputStream
!= null);
639 assert (outputStream
!= null);
641 csiParams
= new ArrayList
<Integer
>();
642 tabStops
= new ArrayList
<Integer
>();
643 scrollback
= new ArrayList
<DisplayLine
>();
644 display
= new ArrayList
<DisplayLine
>();
647 if (inputStream
instanceof TimeoutInputStream
) {
648 this.inputStream
= (TimeoutInputStream
)inputStream
;
650 this.inputStream
= new TimeoutInputStream(inputStream
, 2000);
652 if (type
== DeviceType
.XTERM
) {
653 this.input
= new InputStreamReader(this.inputStream
, "UTF-8");
654 this.output
= new OutputStreamWriter(new
655 BufferedOutputStream(outputStream
), "UTF-8");
656 this.outputStream
= null;
659 this.outputStream
= new BufferedOutputStream(outputStream
);
661 this.displayListener
= displayListener
;
664 for (int i
= 0; i
< height
; i
++) {
665 display
.add(new DisplayLine(currentState
.attr
));
668 // Spin up the input reader
669 readerThread
= new Thread(this);
670 readerThread
.start();
673 // ------------------------------------------------------------------------
674 // Runnable ---------------------------------------------------------------
675 // ------------------------------------------------------------------------
678 * Read function runs on a separate thread.
680 public final void run() {
681 boolean utf8
= false;
682 boolean done
= false;
684 if (type
== DeviceType
.XTERM
) {
688 // available() will often return > 1, so we need to read in chunks to
690 char [] readBufferUTF8
= null;
691 byte [] readBuffer
= null;
693 readBufferUTF8
= new char[2048];
695 readBuffer
= new byte[2048];
698 while (!done
&& !stopReaderThread
) {
699 synchronized (userQueue
) {
700 while (userQueue
.size() > 0) {
701 handleUserEvent(userQueue
.remove(0));
706 int n
= inputStream
.available();
708 // System.err.printf("available() %d\n", n); System.err.flush();
710 if (readBufferUTF8
.length
< n
) {
711 // The buffer wasn't big enough, make it huger
712 int newSizeHalf
= Math
.max(readBufferUTF8
.length
,
715 readBufferUTF8
= new char[newSizeHalf
* 2];
718 if (readBuffer
.length
< n
) {
719 // The buffer wasn't big enough, make it huger
720 int newSizeHalf
= Math
.max(readBuffer
.length
, n
);
721 readBuffer
= new byte[newSizeHalf
* 2];
727 } catch (InterruptedException e
) {
736 rc
= input
.read(readBufferUTF8
, 0,
737 readBufferUTF8
.length
);
739 rc
= inputStream
.read(readBuffer
, 0,
742 } catch (ReadTimeoutException e
) {
746 // System.err.printf("read() %d\n", rc); System.err.flush();
751 // Don't step on UI events
752 synchronized (this) {
754 for (int i
= 0; i
< rc
;) {
755 int ch
= Character
.codePointAt(readBufferUTF8
,
757 i
+= Character
.charCount(ch
);
761 for (int i
= 0; i
< rc
; i
++) {
762 consume(readBuffer
[i
]);
766 // Permit my enclosing UI to know that I updated.
767 if (displayListener
!= null) {
768 displayListener
.displayChanged();
771 // System.err.println("end while loop"); System.err.flush();
772 } catch (IOException e
) {
775 // This is an unusual case. We want to see the stack trace,
776 // but it is related to the spawned process rather than the
777 // actual UI. We will generate the stack trace, and consume
778 // it as though it was emitted by the shell.
779 CharArrayWriter writer
= new CharArrayWriter();
780 // Send a ST and RIS to clear the emulator state.
782 writer
.write("\033\\\033c");
783 writer
.write("\n-----------------------------------\n");
784 e
.printStackTrace(new PrintWriter(writer
));
785 writer
.write("\n-----------------------------------\n");
786 } catch (IOException e2
) {
789 char [] stackTrace
= writer
.toCharArray();
790 for (int i
= 0; i
< stackTrace
.length
; i
++) {
791 if (stackTrace
[i
] == '\n') {
794 consume(stackTrace
[i
]);
798 } // while ((done == false) && (stopReaderThread == false))
800 // Let the rest of the world know that I am done.
801 stopReaderThread
= true;
804 inputStream
.cancelRead();
807 } catch (IOException e
) {
813 } catch (IOException e
) {
817 // Permit my enclosing UI to know that I updated.
818 if (displayListener
!= null) {
819 displayListener
.displayChanged();
822 // System.err.println("*** run() exiting..."); System.err.flush();
825 // ------------------------------------------------------------------------
826 // ECMA48 -----------------------------------------------------------------
827 // ------------------------------------------------------------------------
830 * Process keyboard and mouse events from the user.
832 * @param event the input event to consume
834 private void handleUserEvent(final TInputEvent event
) {
835 if (event
instanceof TKeypressEvent
) {
836 keypress(((TKeypressEvent
) event
).getKey());
838 if (event
instanceof TMouseEvent
) {
839 mouse((TMouseEvent
) event
);
844 * Add a keyboard and mouse event from the user to the queue.
846 * @param event the input event to consume
848 public void addUserEvent(final TInputEvent event
) {
849 synchronized (userQueue
) {
850 userQueue
.add(event
);
855 * Return the proper primary Device Attributes string.
857 * @return string to send to remote side that is appropriate for the
860 private String
deviceTypeResponse() {
863 // "I am a VT100 with advanced video option" (often VT102)
872 // "I am a VT220" - 7 bit version
874 return "\033[?62;1;6;9;4c";
876 // "I am a VT220" - 8 bit version
877 return "\u009b?62;1;6;9;4c";
879 throw new IllegalArgumentException("Invalid device type: " + type
);
884 * Return the proper TERM environment variable for this device type.
886 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
887 * @return "vt100", "xterm", etc.
889 public static String
deviceTypeTerm(final DeviceType deviceType
) {
890 switch (deviceType
) {
904 throw new IllegalArgumentException("Invalid device type: "
910 * Return the proper LANG for this device type. Only XTERM devices know
911 * about UTF-8, the others are defined by their standard to be either
912 * 7-bit or 8-bit characters only.
914 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
915 * @param baseLang a base language without UTF-8 flag such as "C" or
917 * @return "en_US", "en_US.UTF-8", etc.
919 public static String
deviceTypeLang(final DeviceType deviceType
,
920 final String baseLang
) {
922 switch (deviceType
) {
930 return baseLang
+ ".UTF-8";
933 throw new IllegalArgumentException("Invalid device type: "
939 * Write a string directly to the remote side.
941 * @param str string to send
943 public void writeRemote(final String str
) {
944 if (stopReaderThread
) {
945 // Reader hit EOF, bail out now.
950 // System.err.printf("writeRemote() '%s'\n", str);
956 if (outputStream
== null) {
960 outputStream
.flush();
961 for (int i
= 0; i
< str
.length(); i
++) {
962 outputStream
.write(str
.charAt(i
));
964 outputStream
.flush();
965 } catch (IOException e
) {
971 if (output
== null) {
978 } catch (IOException e
) {
984 throw new IllegalArgumentException("Invalid device type: " + type
);
989 * Close the input and output streams and stop the reader thread. Note
990 * that it is safe to call this multiple times.
992 public final void close() {
994 // Tell the reader thread to stop looking at input. It will close
995 // the input streams.
996 if (stopReaderThread
== false) {
997 stopReaderThread
= true;
999 readerThread
.join(1000);
1000 } catch (InterruptedException e
) {
1005 // Now close the output stream.
1010 if (outputStream
!= null) {
1012 outputStream
.close();
1013 } catch (IOException e
) {
1016 outputStream
= null;
1020 if (outputStream
!= null) {
1022 outputStream
.close();
1023 } catch (IOException e
) {
1026 outputStream
= null;
1028 if (output
!= null) {
1031 } catch (IOException e
) {
1038 throw new IllegalArgumentException("Invalid device type: " +
1044 * See if the reader thread is still running.
1046 * @return if true, we are still connected to / reading from the remote
1049 public final boolean isReading() {
1050 return (!stopReaderThread
);
1054 * Obtain a new blank display line for an external user
1055 * (e.g. TTerminalWindow).
1057 * @return new blank line
1059 public final DisplayLine
getBlankDisplayLine() {
1060 return new DisplayLine(currentState
.attr
);
1064 * Get the scrollback buffer.
1066 * @return the scrollback buffer
1068 public final List
<DisplayLine
> getScrollbackBuffer() {
1073 * Get the display buffer.
1075 * @return the display buffer
1077 public final List
<DisplayLine
> getDisplayBuffer() {
1082 * Get the visible display + scrollback buffer, offset by a specified
1083 * number of rows from the bottom.
1085 * @param visibleHeight the total height of the display to show
1086 * @param scrollBottom the number of rows from the bottom to scroll back
1087 * @return a copy of the display + scrollback buffers
1089 public final List
<DisplayLine
> getVisibleDisplay(final int visibleHeight
,
1090 final int scrollBottom
) {
1092 assert (visibleHeight
>= 0);
1093 assert (scrollBottom
>= 0);
1095 int visibleBottom
= scrollback
.size() + display
.size() - scrollBottom
;
1097 List
<DisplayLine
> preceedingBlankLines
= new ArrayList
<DisplayLine
>();
1098 int visibleTop
= visibleBottom
- visibleHeight
;
1099 if (visibleTop
< 0) {
1100 for (int i
= visibleTop
; i
< 0; i
++) {
1101 preceedingBlankLines
.add(getBlankDisplayLine());
1105 assert (visibleTop
>= 0);
1107 List
<DisplayLine
> displayLines
= new ArrayList
<DisplayLine
>();
1108 displayLines
.addAll(scrollback
);
1109 displayLines
.addAll(display
);
1111 List
<DisplayLine
> visibleLines
= new ArrayList
<DisplayLine
>();
1112 visibleLines
.addAll(preceedingBlankLines
);
1113 visibleLines
.addAll(displayLines
.subList(visibleTop
, visibleBottom
));
1115 // Fill in the blank lines on bottom
1116 int bottomBlankLines
= visibleHeight
- visibleLines
.size();
1117 assert (bottomBlankLines
>= 0);
1118 for (int i
= 0; i
< bottomBlankLines
; i
++) {
1119 visibleLines
.add(getBlankDisplayLine());
1122 return copyBuffer(visibleLines
);
1126 * Copy a display buffer.
1128 * @param buffer the buffer to copy
1129 * @return a deep copy of the buffer's data
1131 private List
<DisplayLine
> copyBuffer(final List
<DisplayLine
> buffer
) {
1132 ArrayList
<DisplayLine
> result
= new ArrayList
<DisplayLine
>(buffer
.size());
1133 for (DisplayLine line
: buffer
) {
1134 result
.add(new DisplayLine(line
));
1140 * Get the display width.
1142 * @return the width (usually 80 or 132)
1144 public final int getWidth() {
1149 * Set the display width.
1151 * @param width the new width
1153 public final synchronized void setWidth(final int width
) {
1155 rightMargin
= width
- 1;
1156 if (currentState
.cursorX
>= width
) {
1157 currentState
.cursorX
= width
- 1;
1159 if (savedState
.cursorX
>= width
) {
1160 savedState
.cursorX
= width
- 1;
1165 * Get the display height.
1167 * @return the height (usually 24)
1169 public final int getHeight() {
1174 * Set the display height.
1176 * @param height the new height
1178 public final synchronized void setHeight(final int height
) {
1179 int delta
= height
- this.height
;
1180 this.height
= height
;
1181 scrollRegionBottom
+= delta
;
1182 if (scrollRegionBottom
< 0) {
1183 scrollRegionBottom
= height
;
1185 if (scrollRegionTop
>= scrollRegionBottom
) {
1186 scrollRegionTop
= 0;
1188 if (currentState
.cursorY
>= height
) {
1189 currentState
.cursorY
= height
- 1;
1191 if (savedState
.cursorY
>= height
) {
1192 savedState
.cursorY
= height
- 1;
1194 while (display
.size() < height
) {
1195 DisplayLine line
= new DisplayLine(currentState
.attr
);
1196 line
.setReverseColor(reverseVideo
);
1199 while (display
.size() > height
) {
1200 scrollback
.add(display
.remove(0));
1205 * Get visible cursor flag.
1207 * @return if true, the cursor is visible
1209 public final boolean isCursorVisible() {
1210 return cursorVisible
;
1214 * Get the screen title as set by the xterm OSC sequence. Lots of
1215 * applications send a screenTitle regardless of whether it is an xterm
1218 * @return screen title
1220 public final String
getScreenTitle() {
1225 * Get 132 columns value.
1227 * @return if true, the terminal is in 132 column mode
1229 public final boolean isColumns132() {
1234 * Clear the CSI parameters and flags.
1236 private void toGround() {
1238 collectBuffer
= new StringBuilder(8);
1239 scanState
= ScanState
.GROUND
;
1243 * Reset the tab stops list.
1245 private void resetTabStops() {
1247 for (int i
= 0; (i
* 8) <= rightMargin
; i
++) {
1248 tabStops
.add(Integer
.valueOf(i
* 8));
1253 * Reset the 88- or 256-colors.
1255 private void resetColors() {
1256 colors88
= new ArrayList
<Integer
>(256);
1257 for (int i
= 0; i
< 256; i
++) {
1261 // Set default system colors.
1262 colors88
.set(0, 0x00000000);
1263 colors88
.set(1, 0x00a80000);
1264 colors88
.set(2, 0x0000a800);
1265 colors88
.set(3, 0x00a85400);
1266 colors88
.set(4, 0x000000a8);
1267 colors88
.set(5, 0x00a800a8);
1268 colors88
.set(6, 0x0000a8a8);
1269 colors88
.set(7, 0x00a8a8a8);
1271 colors88
.set(8, 0x00545454);
1272 colors88
.set(9, 0x00fc5454);
1273 colors88
.set(10, 0x0054fc54);
1274 colors88
.set(11, 0x00fcfc54);
1275 colors88
.set(12, 0x005454fc);
1276 colors88
.set(13, 0x00fc54fc);
1277 colors88
.set(14, 0x0054fcfc);
1278 colors88
.set(15, 0x00fcfcfc);
1282 * Get the RGB value of one of the indexed colors.
1284 * @param index the color index
1285 * @return the RGB value
1287 private int get88Color(final int index
) {
1288 // System.err.print("get88Color: " + index);
1289 if ((index
< 0) || (index
> colors88
.size())) {
1290 // System.err.println(" -- UNKNOWN");
1293 // System.err.printf(" %08x\n", colors88.get(index));
1294 return colors88
.get(index
);
1298 * Set one of the indexed colors to a color specification.
1300 * @param index the color index
1301 * @param spec the specification, typically something like "rgb:aa/bb/cc"
1303 private void set88Color(final int index
, final String spec
) {
1304 // System.err.println("set88Color: " + index + " '" + spec + "'");
1306 if ((index
< 0) || (index
> colors88
.size())) {
1309 if (spec
.startsWith("rgb:")) {
1310 String
[] rgbTokens
= spec
.substring(4).split("/");
1311 if (rgbTokens
.length
== 3) {
1313 int rgb
= (Integer
.parseInt(rgbTokens
[0], 16) << 16);
1314 rgb
|= Integer
.parseInt(rgbTokens
[1], 16) << 8;
1315 rgb
|= Integer
.parseInt(rgbTokens
[2], 16);
1316 // System.err.printf(" set to %08x\n", rgb);
1317 colors88
.set(index
, rgb
);
1318 } catch (NumberFormatException e
) {
1325 if (spec
.toLowerCase().equals("black")) {
1326 colors88
.set(index
, 0x00000000);
1327 } else if (spec
.toLowerCase().equals("red")) {
1328 colors88
.set(index
, 0x00a80000);
1329 } else if (spec
.toLowerCase().equals("green")) {
1330 colors88
.set(index
, 0x0000a800);
1331 } else if (spec
.toLowerCase().equals("yellow")) {
1332 colors88
.set(index
, 0x00a85400);
1333 } else if (spec
.toLowerCase().equals("blue")) {
1334 colors88
.set(index
, 0x000000a8);
1335 } else if (spec
.toLowerCase().equals("magenta")) {
1336 colors88
.set(index
, 0x00a800a8);
1337 } else if (spec
.toLowerCase().equals("cyan")) {
1338 colors88
.set(index
, 0x0000a8a8);
1339 } else if (spec
.toLowerCase().equals("white")) {
1340 colors88
.set(index
, 0x00a8a8a8);
1346 * Reset the emulation state.
1348 private void reset() {
1350 currentState
= new SaveableState();
1351 savedState
= new SaveableState();
1352 scanState
= ScanState
.GROUND
;
1355 scrollRegionTop
= 0;
1356 scrollRegionBottom
= height
- 1;
1357 rightMargin
= width
- 1;
1358 newLineMode
= false;
1359 arrowKeyMode
= ArrowKeyMode
.ANSI
;
1360 keypadMode
= KeypadMode
.Numeric
;
1361 wrapLineFlag
= false;
1362 if (displayListener
!= null) {
1363 width
= displayListener
.getDisplayWidth();
1364 height
= displayListener
.getDisplayHeight();
1365 rightMargin
= width
- 1;
1373 newLineMode
= false;
1374 reverseVideo
= false;
1376 cursorVisible
= true;
1379 singleshift
= Singleshift
.NONE
;
1381 printerControllerMode
= false;
1384 mouseProtocol
= MouseProtocol
.OFF
;
1385 mouseEncoding
= MouseEncoding
.X10
;
1390 // Reset extra colors
1398 * Append a new line to the bottom of the display, adding lines off the
1399 * top to the scrollback buffer.
1401 private void newDisplayLine() {
1402 // Scroll the top line off into the scrollback buffer
1403 scrollback
.add(display
.get(0));
1404 if (scrollback
.size() > maxScrollback
) {
1405 scrollback
.remove(0);
1406 scrollback
.trimToSize();
1409 display
.trimToSize();
1410 DisplayLine line
= new DisplayLine(currentState
.attr
);
1411 line
.setReverseColor(reverseVideo
);
1416 * Wraps the current line.
1418 private void wrapCurrentLine() {
1419 if (currentState
.cursorY
== height
- 1) {
1422 if (currentState
.cursorY
< height
- 1) {
1423 currentState
.cursorY
++;
1425 currentState
.cursorX
= 0;
1429 * Handle a carriage return.
1431 private void carriageReturn() {
1432 currentState
.cursorX
= 0;
1433 wrapLineFlag
= false;
1437 * Reverse the color of the visible display.
1439 private void invertDisplayColors() {
1440 for (DisplayLine line
: display
) {
1441 line
.setReverseColor(!line
.isReverseColor());
1446 * Handle a linefeed.
1448 private void linefeed() {
1450 if (currentState
.cursorY
< scrollRegionBottom
) {
1451 // Increment screen y
1452 currentState
.cursorY
++;
1455 // Screen y does not increment
1458 * Two cases: either we're inside a scrolling region or not. If
1459 * the scrolling region bottom is the bottom of the screen, then
1460 * push the top line into the buffer. Else scroll the scrolling
1463 if ((scrollRegionBottom
== height
- 1) && (scrollRegionTop
== 0)) {
1465 // We're at the bottom of the scroll region, AND the scroll
1466 // region is the entire screen.
1472 // We're at the bottom of the scroll region, AND the scroll
1473 // region is NOT the entire screen.
1474 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
1479 currentState
.cursorX
= 0;
1481 wrapLineFlag
= false;
1485 * Prints one character to the display buffer.
1487 * @param ch character to display
1489 private void printCharacter(final int ch
) {
1490 int rightMargin
= this.rightMargin
;
1492 if (StringUtils
.width(ch
) == 2) {
1493 // This is a full-width character. Save two spaces, and then
1494 // draw the character as two image halves.
1495 int x0
= currentState
.cursorX
;
1496 int y0
= currentState
.cursorY
;
1497 printCharacter(' ');
1498 printCharacter(' ');
1499 if ((currentState
.cursorX
== x0
+ 2)
1500 && (currentState
.cursorY
== y0
)
1502 // We can draw both halves of the character.
1503 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1504 } else if ((currentState
.cursorX
== x0
+ 1)
1505 && (currentState
.cursorY
== y0
)
1507 // VT100 line wrap behavior: we should be at the right
1508 // margin. We can draw both halves of the character.
1509 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1511 // The character splits across the line. Draw the entire
1512 // character on the new line, giving one more space for it.
1513 x0
= currentState
.cursorX
- 1;
1514 y0
= currentState
.cursorY
;
1515 printCharacter(' ');
1516 drawHalves(x0
, y0
, x0
+ 1, y0
, ch
);
1521 // Check if we have double-width, and if so chop at 40/66 instead of
1523 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
1524 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
1527 // Check the unusually-complicated line wrapping conditions...
1528 if (currentState
.cursorX
== rightMargin
) {
1530 if (currentState
.lineWrap
== true) {
1532 * This case happens when: the cursor was already on the
1533 * right margin (either through printing or by an explicit
1534 * placement command), and a character was printed.
1536 * The line wraps only when a new character arrives AND the
1537 * cursor is already on the right margin AND has placed a
1538 * character in its cell. Easier to see than to explain.
1540 if (wrapLineFlag
== false) {
1542 * This block marks the case that we are in the margin
1543 * and the first character has been received and printed.
1545 wrapLineFlag
= true;
1548 * This block marks the case that we are in the margin
1549 * and the second character has been received and
1552 wrapLineFlag
= false;
1556 } else if (currentState
.cursorX
<= rightMargin
) {
1558 * This is the normal case: a character came in and was printed
1559 * to the left of the right margin column.
1562 // Turn off VT100 special-case flag
1563 wrapLineFlag
= false;
1566 // "Print" the character
1567 Cell newCell
= new Cell(ch
);
1568 CellAttributes newCellAttributes
= (CellAttributes
) newCell
;
1569 newCellAttributes
.setTo(currentState
.attr
);
1570 DisplayLine line
= display
.get(currentState
.cursorY
);
1572 if (StringUtils
.width(ch
) == 1) {
1573 // Insert mode special case
1574 if (insertMode
== true) {
1575 line
.insert(currentState
.cursorX
, newCell
);
1577 // Replace an existing character
1578 line
.replace(currentState
.cursorX
, newCell
);
1581 // Increment horizontal
1582 if (wrapLineFlag
== false) {
1583 currentState
.cursorX
++;
1584 if (currentState
.cursorX
> rightMargin
) {
1585 currentState
.cursorX
--;
1592 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1593 * send to the remote side.
1595 * @param mouse mouse event received from the local user
1597 private void mouse(final TMouseEvent mouse
) {
1600 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1601 mouseProtocol, mouseEncoding, mouse);
1604 if (mouseEncoding
== MouseEncoding
.X10
) {
1605 // We will support X10 but only for (160,94) and smaller.
1606 if ((mouse
.getX() >= 160) || (mouse
.getY() >= 94)) {
1611 switch (mouseProtocol
) {
1618 // Only report button presses
1619 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
) {
1625 // Only report button presses and releases
1626 if ((mouse
.getType() != TMouseEvent
.Type
.MOUSE_DOWN
)
1627 && (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
)
1635 * Only report button presses, button releases, and motions that
1636 * have a button down (i.e. drag-and-drop).
1638 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1639 if (!mouse
.isMouse1()
1640 && !mouse
.isMouse2()
1641 && !mouse
.isMouse3()
1642 && !mouse
.isMouseWheelUp()
1643 && !mouse
.isMouseWheelDown()
1651 // Report everything
1655 // Now encode the event
1656 StringBuilder sb
= new StringBuilder(6);
1657 if (mouseEncoding
== MouseEncoding
.SGR
) {
1658 sb
.append((char) 0x1B);
1661 if (mouse
.isMouse1()) {
1662 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1667 } else if (mouse
.isMouse2()) {
1668 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1673 } else if (mouse
.isMouse3()) {
1674 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1679 } else if (mouse
.isMouseWheelUp()) {
1681 } else if (mouse
.isMouseWheelDown()) {
1684 // This is motion with no buttons down.
1688 sb
.append(String
.format("%d;%d", mouse
.getX() + 1,
1691 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1698 // X10 and UTF8 encodings
1699 sb
.append((char) 0x1B);
1702 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1703 sb
.append((char) (0x03 + 32));
1704 } else if (mouse
.isMouse1()) {
1705 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1706 sb
.append((char) (0x00 + 32 + 32));
1708 sb
.append((char) (0x00 + 32));
1710 } else if (mouse
.isMouse2()) {
1711 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1712 sb
.append((char) (0x01 + 32 + 32));
1714 sb
.append((char) (0x01 + 32));
1716 } else if (mouse
.isMouse3()) {
1717 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
) {
1718 sb
.append((char) (0x02 + 32 + 32));
1720 sb
.append((char) (0x02 + 32));
1722 } else if (mouse
.isMouseWheelUp()) {
1723 sb
.append((char) (0x04 + 64));
1724 } else if (mouse
.isMouseWheelDown()) {
1725 sb
.append((char) (0x05 + 64));
1727 // This is motion with no buttons down.
1728 sb
.append((char) (0x03 + 32));
1731 sb
.append((char) (mouse
.getX() + 33));
1732 sb
.append((char) (mouse
.getY() + 33));
1735 // System.err.printf("Would write: \'%s\'\n", sb.toString());
1736 writeRemote(sb
.toString());
1740 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1741 * send to the remote side.
1743 * @param keypress keypress received from the local user
1745 private void keypress(final TKeypress keypress
) {
1746 writeRemote(keypressToString(keypress
));
1750 * Build one of the complex xterm keystroke sequences, storing the result in
1751 * xterm_keystroke_buffer.
1753 * @param ss3 the prefix to use based on VT100 state.
1754 * @param first the first character, usually a number.
1755 * @param first the last character, one of the following: ~ A B C D F H
1756 * @param ctrl whether or not ctrl is down
1757 * @param alt whether or not alt is down
1758 * @param shift whether or not shift is down
1759 * @return the buffer with the full key sequence
1761 private String
xtermBuildKeySequence(final String ss3
, final char first
,
1762 final char last
, boolean ctrl
, boolean alt
, boolean shift
) {
1764 StringBuilder sb
= new StringBuilder(ss3
);
1765 if ((last
== '~') || (ctrl
== true) || (alt
== true)
1769 if ( (ctrl
== false) && (alt
== false) && (shift
== true)) {
1771 } else if ((ctrl
== false) && (alt
== true) && (shift
== false)) {
1773 } else if ((ctrl
== false) && (alt
== true) && (shift
== true)) {
1775 } else if ((ctrl
== true) && (alt
== false) && (shift
== false)) {
1777 } else if ((ctrl
== true) && (alt
== false) && (shift
== true)) {
1779 } else if ((ctrl
== true) && (alt
== true) && (shift
== false)) {
1781 } else if ((ctrl
== true) && (alt
== true) && (shift
== true)) {
1786 return sb
.toString();
1790 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1792 * @param keypress keypress received from the local user
1793 * @return string to transmit to the remote side
1795 @SuppressWarnings("fallthrough")
1796 private String
keypressToString(final TKeypress keypress
) {
1798 if ((fullDuplex
== false) && (!keypress
.isFnKey())) {
1800 * If this is a control character, process it like it came from
1803 if (keypress
.getChar() < 0x20) {
1804 handleControlChar((char) keypress
.getChar());
1806 // Local echo for everything else
1807 printCharacter(keypress
.getChar());
1811 if ((newLineMode
== true) && (keypress
.equals(kbEnter
))) {
1816 // Handle control characters
1817 if ((keypress
.isCtrl()) && (!keypress
.isFnKey())) {
1818 StringBuilder sb
= new StringBuilder();
1819 int ch
= keypress
.getChar();
1821 sb
.append(Character
.toChars(ch
));
1822 return sb
.toString();
1825 // Handle alt characters
1826 if ((keypress
.isAlt()) && (!keypress
.isFnKey())) {
1827 StringBuilder sb
= new StringBuilder("\033");
1828 int ch
= keypress
.getChar();
1829 sb
.append(Character
.toChars(ch
));
1830 return sb
.toString();
1833 if (keypress
.equals(kbBackspaceDel
)) {
1846 if (keypress
.equalsWithoutModifiers(kbLeft
)) {
1849 switch (arrowKeyMode
) {
1851 return xtermBuildKeySequence("\033[", '1', 'D',
1852 keypress
.isCtrl(), keypress
.isAlt(),
1853 keypress
.isShift());
1855 return xtermBuildKeySequence("\033", '1', 'D',
1856 keypress
.isCtrl(), keypress
.isAlt(),
1857 keypress
.isShift());
1859 return xtermBuildKeySequence("\033O", '1', 'D',
1860 keypress
.isCtrl(), keypress
.isAlt(),
1861 keypress
.isShift());
1864 switch (arrowKeyMode
) {
1875 if (keypress
.equalsWithoutModifiers(kbRight
)) {
1878 switch (arrowKeyMode
) {
1880 return xtermBuildKeySequence("\033[", '1', 'C',
1881 keypress
.isCtrl(), keypress
.isAlt(),
1882 keypress
.isShift());
1884 return xtermBuildKeySequence("\033", '1', 'C',
1885 keypress
.isCtrl(), keypress
.isAlt(),
1886 keypress
.isShift());
1888 return xtermBuildKeySequence("\033O", '1', 'C',
1889 keypress
.isCtrl(), keypress
.isAlt(),
1890 keypress
.isShift());
1893 switch (arrowKeyMode
) {
1904 if (keypress
.equalsWithoutModifiers(kbUp
)) {
1907 switch (arrowKeyMode
) {
1909 return xtermBuildKeySequence("\033[", '1', 'A',
1910 keypress
.isCtrl(), keypress
.isAlt(),
1911 keypress
.isShift());
1913 return xtermBuildKeySequence("\033", '1', 'A',
1914 keypress
.isCtrl(), keypress
.isAlt(),
1915 keypress
.isShift());
1917 return xtermBuildKeySequence("\033O", '1', 'A',
1918 keypress
.isCtrl(), keypress
.isAlt(),
1919 keypress
.isShift());
1922 switch (arrowKeyMode
) {
1933 if (keypress
.equalsWithoutModifiers(kbDown
)) {
1936 switch (arrowKeyMode
) {
1938 return xtermBuildKeySequence("\033[", '1', 'B',
1939 keypress
.isCtrl(), keypress
.isAlt(),
1940 keypress
.isShift());
1942 return xtermBuildKeySequence("\033", '1', 'B',
1943 keypress
.isCtrl(), keypress
.isAlt(),
1944 keypress
.isShift());
1946 return xtermBuildKeySequence("\033O", '1', 'B',
1947 keypress
.isCtrl(), keypress
.isAlt(),
1948 keypress
.isShift());
1951 switch (arrowKeyMode
) {
1962 if (keypress
.equalsWithoutModifiers(kbHome
)) {
1965 switch (arrowKeyMode
) {
1967 return xtermBuildKeySequence("\033[", '1', 'H',
1968 keypress
.isCtrl(), keypress
.isAlt(),
1969 keypress
.isShift());
1971 return xtermBuildKeySequence("\033", '1', 'H',
1972 keypress
.isCtrl(), keypress
.isAlt(),
1973 keypress
.isShift());
1975 return xtermBuildKeySequence("\033O", '1', 'H',
1976 keypress
.isCtrl(), keypress
.isAlt(),
1977 keypress
.isShift());
1980 switch (arrowKeyMode
) {
1991 if (keypress
.equalsWithoutModifiers(kbEnd
)) {
1994 switch (arrowKeyMode
) {
1996 return xtermBuildKeySequence("\033[", '1', 'F',
1997 keypress
.isCtrl(), keypress
.isAlt(),
1998 keypress
.isShift());
2000 return xtermBuildKeySequence("\033", '1', 'F',
2001 keypress
.isCtrl(), keypress
.isAlt(),
2002 keypress
.isShift());
2004 return xtermBuildKeySequence("\033O", '1', 'F',
2005 keypress
.isCtrl(), keypress
.isAlt(),
2006 keypress
.isShift());
2009 switch (arrowKeyMode
) {
2020 if (keypress
.equals(kbF1
)) {
2028 if (keypress
.equals(kbF2
)) {
2036 if (keypress
.equals(kbF3
)) {
2044 if (keypress
.equals(kbF4
)) {
2052 if (keypress
.equals(kbF5
)) {
2065 if (keypress
.equals(kbF6
)) {
2078 if (keypress
.equals(kbF7
)) {
2091 if (keypress
.equals(kbF8
)) {
2104 if (keypress
.equals(kbF9
)) {
2117 if (keypress
.equals(kbF10
)) {
2130 if (keypress
.equals(kbF11
)) {
2134 if (keypress
.equals(kbF12
)) {
2138 if (keypress
.equals(kbShiftF1
)) {
2143 if (type
== DeviceType
.XTERM
) {
2149 if (keypress
.equals(kbShiftF2
)) {
2154 if (type
== DeviceType
.XTERM
) {
2160 if (keypress
.equals(kbShiftF3
)) {
2165 if (type
== DeviceType
.XTERM
) {
2171 if (keypress
.equals(kbShiftF4
)) {
2176 if (type
== DeviceType
.XTERM
) {
2182 if (keypress
.equals(kbShiftF5
)) {
2184 return "\033[15;2~";
2187 if (keypress
.equals(kbShiftF6
)) {
2189 return "\033[17;2~";
2192 if (keypress
.equals(kbShiftF7
)) {
2194 return "\033[18;2~";
2197 if (keypress
.equals(kbShiftF8
)) {
2199 return "\033[19;2~";
2202 if (keypress
.equals(kbShiftF9
)) {
2204 return "\033[20;2~";
2207 if (keypress
.equals(kbShiftF10
)) {
2209 return "\033[21;2~";
2212 if (keypress
.equals(kbShiftF11
)) {
2214 return "\033[23;2~";
2217 if (keypress
.equals(kbShiftF12
)) {
2219 return "\033[24;2~";
2222 if (keypress
.equals(kbCtrlF1
)) {
2227 if (type
== DeviceType
.XTERM
) {
2233 if (keypress
.equals(kbCtrlF2
)) {
2238 if (type
== DeviceType
.XTERM
) {
2244 if (keypress
.equals(kbCtrlF3
)) {
2249 if (type
== DeviceType
.XTERM
) {
2255 if (keypress
.equals(kbCtrlF4
)) {
2260 if (type
== DeviceType
.XTERM
) {
2266 if (keypress
.equals(kbCtrlF5
)) {
2268 return "\033[15;5~";
2271 if (keypress
.equals(kbCtrlF6
)) {
2273 return "\033[17;5~";
2276 if (keypress
.equals(kbCtrlF7
)) {
2278 return "\033[18;5~";
2281 if (keypress
.equals(kbCtrlF8
)) {
2283 return "\033[19;5~";
2286 if (keypress
.equals(kbCtrlF9
)) {
2288 return "\033[20;5~";
2291 if (keypress
.equals(kbCtrlF10
)) {
2293 return "\033[21;5~";
2296 if (keypress
.equals(kbCtrlF11
)) {
2298 return "\033[23;5~";
2301 if (keypress
.equals(kbCtrlF12
)) {
2303 return "\033[24;5~";
2306 if (keypress
.equalsWithoutModifiers(kbPgUp
)) {
2309 return xtermBuildKeySequence("\033[", '5', '~',
2310 keypress
.isCtrl(), keypress
.isAlt(),
2311 keypress
.isShift());
2317 if (keypress
.equalsWithoutModifiers(kbPgDn
)) {
2320 return xtermBuildKeySequence("\033[", '6', '~',
2321 keypress
.isCtrl(), keypress
.isAlt(),
2322 keypress
.isShift());
2328 if (keypress
.equalsWithoutModifiers(kbIns
)) {
2331 return xtermBuildKeySequence("\033[", '2', '~',
2332 keypress
.isCtrl(), keypress
.isAlt(),
2333 keypress
.isShift());
2339 if (keypress
.equalsWithoutModifiers(kbDel
)) {
2342 return xtermBuildKeySequence("\033[", '3', '~',
2343 keypress
.isCtrl(), keypress
.isAlt(),
2344 keypress
.isShift());
2346 // Delete sends real delete for VTxxx
2351 if (keypress
.equals(kbEnter
)) {
2355 if (keypress
.equals(kbEsc
)) {
2359 if (keypress
.equals(kbAltEsc
)) {
2363 if (keypress
.equals(kbTab
)) {
2367 if ((keypress
.equalsWithoutModifiers(kbBackTab
)) ||
2368 (keypress
.equals(kbShiftTab
))
2378 // Non-alt, non-ctrl characters
2379 if (!keypress
.isFnKey()) {
2380 StringBuilder sb
= new StringBuilder();
2381 sb
.append(Character
.toChars(keypress
.getChar()));
2382 return sb
.toString();
2388 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2391 * @param ch 8-bit character from the remote side
2392 * @param charsetGl character set defined for GL
2393 * @param charsetGr character set defined for GR
2394 * @return character to display on the screen
2396 private char mapCharacterCharset(final int ch
,
2397 final CharacterSet charsetGl
,
2398 final CharacterSet charsetGr
) {
2400 int lookupChar
= ch
;
2401 CharacterSet lookupCharset
= charsetGl
;
2404 assert ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
));
2405 lookupCharset
= charsetGr
;
2409 switch (lookupCharset
) {
2412 return DECCharacterSets
.SPECIAL_GRAPHICS
[lookupChar
];
2415 return DECCharacterSets
.UK
[lookupChar
];
2418 return DECCharacterSets
.US_ASCII
[lookupChar
];
2421 return DECCharacterSets
.NL
[lookupChar
];
2424 return DECCharacterSets
.FI
[lookupChar
];
2427 return DECCharacterSets
.FR
[lookupChar
];
2430 return DECCharacterSets
.FR_CA
[lookupChar
];
2433 return DECCharacterSets
.DE
[lookupChar
];
2436 return DECCharacterSets
.IT
[lookupChar
];
2439 return DECCharacterSets
.NO
[lookupChar
];
2442 return DECCharacterSets
.ES
[lookupChar
];
2445 return DECCharacterSets
.SV
[lookupChar
];
2448 return DECCharacterSets
.SWISS
[lookupChar
];
2450 case DEC_SUPPLEMENTAL
:
2451 return DECCharacterSets
.DEC_SUPPLEMENTAL
[lookupChar
];
2454 return DECCharacterSets
.VT52_SPECIAL_GRAPHICS
[lookupChar
];
2457 return DECCharacterSets
.US_ASCII
[lookupChar
];
2460 return DECCharacterSets
.US_ASCII
[lookupChar
];
2463 throw new IllegalArgumentException("Invalid character set value: "
2469 * Map an 8-bit byte into a printable character.
2471 * @param ch either 8-bit or Unicode character from the remote side
2472 * @return character to display on the screen
2474 private int mapCharacter(final int ch
) {
2476 // Unicode character, just return it
2480 CharacterSet charsetGl
= currentState
.g0Charset
;
2481 CharacterSet charsetGr
= currentState
.grCharset
;
2483 if (vt52Mode
== true) {
2484 if (shiftOut
== true) {
2485 // Shifted out character, pull from VT52 graphics
2486 charsetGl
= currentState
.g1Charset
;
2487 charsetGr
= CharacterSet
.US
;
2490 charsetGl
= currentState
.g0Charset
;
2491 charsetGr
= CharacterSet
.US
;
2494 // Pull the character
2495 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2499 if (shiftOut
== true) {
2500 // Shifted out character, pull from G1
2501 charsetGl
= currentState
.g1Charset
;
2502 charsetGr
= currentState
.grCharset
;
2504 // Pull the character
2505 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2509 if (singleshift
== Singleshift
.SS2
) {
2511 singleshift
= Singleshift
.NONE
;
2513 // Shifted out character, pull from G2
2514 charsetGl
= currentState
.g2Charset
;
2515 charsetGr
= currentState
.grCharset
;
2519 if (singleshift
== Singleshift
.SS3
) {
2521 singleshift
= Singleshift
.NONE
;
2523 // Shifted out character, pull from G3
2524 charsetGl
= currentState
.g3Charset
;
2525 charsetGr
= currentState
.grCharset
;
2528 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
2529 // Check for locking shift
2531 switch (currentState
.glLockshift
) {
2534 throw new IllegalArgumentException("programming bug");
2537 throw new IllegalArgumentException("programming bug");
2540 throw new IllegalArgumentException("programming bug");
2544 charsetGl
= currentState
.g2Charset
;
2549 charsetGl
= currentState
.g3Charset
;
2554 charsetGl
= currentState
.g0Charset
;
2558 switch (currentState
.grLockshift
) {
2561 throw new IllegalArgumentException("programming bug");
2564 throw new IllegalArgumentException("programming bug");
2568 charsetGr
= currentState
.g1Charset
;
2573 charsetGr
= currentState
.g2Charset
;
2578 charsetGr
= currentState
.g3Charset
;
2583 charsetGr
= CharacterSet
.DEC_SUPPLEMENTAL
;
2590 // Pull the character
2591 return mapCharacterCharset(ch
, charsetGl
, charsetGr
);
2595 * Scroll the text within a scrolling region up n lines.
2597 * @param regionTop top row of the scrolling region
2598 * @param regionBottom bottom row of the scrolling region
2599 * @param n number of lines to scroll
2601 private void scrollingRegionScrollUp(final int regionTop
,
2602 final int regionBottom
, final int n
) {
2604 if (regionTop
>= regionBottom
) {
2608 // Sanity check: see if there will be any characters left after the
2610 if (regionBottom
+ 1 - regionTop
<= n
) {
2611 // There won't be anything left in the region, so just call
2612 // eraseScreen() and return.
2613 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2617 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2618 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2619 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2621 List
<DisplayLine
> displayMiddle
= display
.subList(regionBottom
+ 1
2622 - remaining
, regionBottom
+ 1);
2623 display
= new ArrayList
<DisplayLine
>(displayTop
);
2624 display
.addAll(displayMiddle
);
2625 for (int i
= 0; i
< n
; i
++) {
2626 DisplayLine line
= new DisplayLine(currentState
.attr
);
2627 line
.setReverseColor(reverseVideo
);
2630 display
.addAll(displayBottom
);
2632 assert (display
.size() == height
);
2636 * Scroll the text within a scrolling region down n lines.
2638 * @param regionTop top row of the scrolling region
2639 * @param regionBottom bottom row of the scrolling region
2640 * @param n number of lines to scroll
2642 private void scrollingRegionScrollDown(final int regionTop
,
2643 final int regionBottom
, final int n
) {
2645 if (regionTop
>= regionBottom
) {
2649 // Sanity check: see if there will be any characters left after the
2651 if (regionBottom
+ 1 - regionTop
<= n
) {
2652 // There won't be anything left in the region, so just call
2653 // eraseScreen() and return.
2654 eraseScreen(regionTop
, 0, regionBottom
, width
- 1, false);
2658 int remaining
= regionBottom
+ 1 - regionTop
- n
;
2659 List
<DisplayLine
> displayTop
= display
.subList(0, regionTop
);
2660 List
<DisplayLine
> displayBottom
= display
.subList(regionBottom
+ 1,
2662 List
<DisplayLine
> displayMiddle
= display
.subList(regionTop
,
2663 regionTop
+ remaining
);
2664 display
= new ArrayList
<DisplayLine
>(displayTop
);
2665 for (int i
= 0; i
< n
; i
++) {
2666 DisplayLine line
= new DisplayLine(currentState
.attr
);
2667 line
.setReverseColor(reverseVideo
);
2670 display
.addAll(displayMiddle
);
2671 display
.addAll(displayBottom
);
2673 assert (display
.size() == height
);
2677 * Process a control character.
2679 * @param ch 8-bit character from the remote side
2681 private void handleControlChar(final char ch
) {
2682 assert ((ch
<= 0x1F) || ((ch
>= 0x7F) && (ch
<= 0x9F)));
2693 // Transmit the answerback message.
2704 cursorLeft(1, false);
2709 advanceToNextTabStop();
2735 currentState
.glLockshift
= LockshiftMode
.NONE
;
2741 currentState
.glLockshift
= LockshiftMode
.NONE
;
2766 singleshift
= Singleshift
.SS2
;
2771 singleshift
= Singleshift
.SS3
;
2781 * Advance the cursor to the next tab stop.
2783 private void advanceToNextTabStop() {
2784 if (tabStops
.size() == 0) {
2785 // Go to the rightmost column
2786 cursorRight(rightMargin
- currentState
.cursorX
, false);
2789 for (Integer stop
: tabStops
) {
2790 if (stop
> currentState
.cursorX
) {
2791 cursorRight(stop
- currentState
.cursorX
, false);
2796 * We got here, meaning there isn't a tab stop beyond the current
2797 * cursor position. Place the cursor of the right-most edge of the
2800 cursorRight(rightMargin
- currentState
.cursorX
, false);
2804 * Save a character into the collect buffer.
2806 * @param ch character to save
2808 private void collect(final char ch
) {
2809 collectBuffer
.append(ch
);
2813 * Save a byte into the CSI parameters buffer.
2815 * @param ch byte to save
2817 private void param(final byte ch
) {
2818 if (csiParams
.size() == 0) {
2819 csiParams
.add(Integer
.valueOf(0));
2821 Integer x
= csiParams
.get(csiParams
.size() - 1);
2822 if ((ch
>= '0') && (ch
<= '9')) {
2825 csiParams
.set(csiParams
.size() - 1, x
);
2828 if ((ch
== ';') && (csiParams
.size() < 16)) {
2829 csiParams
.add(Integer
.valueOf(0));
2834 * Get a CSI parameter value, with a default.
2836 * @param position parameter index. 0 is the first parameter.
2837 * @param defaultValue value to use if csiParams[position] doesn't exist
2838 * @return parameter value
2840 private int getCsiParam(final int position
, final int defaultValue
) {
2841 if (csiParams
.size() < position
+ 1) {
2842 return defaultValue
;
2844 return csiParams
.get(position
).intValue();
2848 * Get a CSI parameter value, clamped to within min/max.
2850 * @param position parameter index. 0 is the first parameter.
2851 * @param defaultValue value to use if csiParams[position] doesn't exist
2852 * @param minValue minimum value inclusive
2853 * @param maxValue maximum value inclusive
2854 * @return parameter value
2856 private int getCsiParam(final int position
, final int defaultValue
,
2857 final int minValue
, final int maxValue
) {
2859 assert (minValue
<= maxValue
);
2860 int value
= getCsiParam(position
, defaultValue
);
2861 if (value
< minValue
) {
2864 if (value
> maxValue
) {
2871 * Set or unset a toggle.
2873 * @param value true for set ('h'), false for reset ('l')
2875 private void setToggle(final boolean value
) {
2876 boolean decPrivateModeFlag
= false;
2878 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2879 if (collectBuffer
.charAt(i
) == '?') {
2880 decPrivateModeFlag
= true;
2885 for (Integer i
: csiParams
) {
2890 if (decPrivateModeFlag
== true) {
2892 if (value
== true) {
2893 // Use application arrow keys
2894 arrowKeyMode
= ArrowKeyMode
.VT100
;
2896 // Use ANSI arrow keys
2897 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2902 if (decPrivateModeFlag
== true) {
2903 if (value
== false) {
2907 arrowKeyMode
= ArrowKeyMode
.VT52
;
2910 * From the VT102 docs: "You use ANSI mode to select
2911 * most terminal features; the terminal uses the same
2912 * features when it switches to VT52 mode. You
2913 * cannot, however, change most of these features in
2916 * In other words, do not reset any other attributes
2917 * when switching between VT52 submode and ANSI.
2919 * HOWEVER, the real vt100 does switch the character
2920 * set according to Usenet.
2922 currentState
.g0Charset
= CharacterSet
.US
;
2923 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2926 if ((type
== DeviceType
.VT220
)
2927 || (type
== DeviceType
.XTERM
)) {
2929 // VT52 mode is explicitly 7-bit
2931 singleshift
= Singleshift
.NONE
;
2936 if (value
== true) {
2937 // Turn off keyboard
2946 if (decPrivateModeFlag
== true) {
2948 if (value
== true) {
2955 if ((displayListener
!= null)
2956 && (type
== DeviceType
.XTERM
)
2958 // For xterms, reset to the actual width, not 80
2960 width
= displayListener
.getDisplayWidth();
2961 rightMargin
= width
- 1;
2964 width
= rightMargin
+ 1;
2967 // Entire screen is cleared, and scrolling region is
2969 eraseScreen(0, 0, height
- 1, width
- 1, false);
2970 scrollRegionTop
= 0;
2971 scrollRegionBottom
= height
- 1;
2972 // Also home the cursor
2973 cursorPosition(0, 0);
2977 if (decPrivateModeFlag
== true) {
2979 if (value
== true) {
2988 if (value
== true) {
2996 if (decPrivateModeFlag
== true) {
2998 if (value
== true) {
3000 * Set selects reverse screen, a white screen
3001 * background with black characters.
3003 if (reverseVideo
!= true) {
3005 * If in normal video, switch it back
3007 invertDisplayColors();
3009 reverseVideo
= true;
3012 * Reset selects normal screen, a black screen
3013 * background with white characters.
3015 if (reverseVideo
== true) {
3017 * If in reverse video already, switch it back
3019 invertDisplayColors();
3021 reverseVideo
= false;
3026 if (decPrivateModeFlag
== true) {
3028 if (value
== true) {
3029 // Origin is relative to scroll region cursor.
3030 // Cursor can NEVER leave scrolling region.
3031 currentState
.originMode
= true;
3032 cursorPosition(0, 0);
3034 // Origin is absolute to entire screen. Cursor can
3035 // leave the scrolling region via cup() and hvp().
3036 currentState
.originMode
= false;
3037 cursorPosition(0, 0);
3042 if (decPrivateModeFlag
== true) {
3044 if (value
== true) {
3046 currentState
.lineWrap
= true;
3048 // Turn linewrap off
3049 currentState
.lineWrap
= false;
3054 if (decPrivateModeFlag
== true) {
3056 if (value
== true) {
3057 // Keyboard auto-repeat on
3060 // Keyboard auto-repeat off
3066 if (decPrivateModeFlag
== false) {
3068 if (value
== true) {
3078 if (decPrivateModeFlag
== true) {
3084 if (decPrivateModeFlag
== true) {
3090 if (decPrivateModeFlag
== false) {
3092 if (value
== true) {
3094 * Set causes a received linefeed, form feed, or
3095 * vertical tab to move cursor to first column of
3096 * next line. RETURN transmits both a carriage return
3097 * and linefeed. This selection is also called new
3103 * Reset causes a received linefeed, form feed, or
3104 * vertical tab to move cursor to next line in
3105 * current column. RETURN transmits a carriage
3108 newLineMode
= false;
3114 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3115 if (decPrivateModeFlag
== true) {
3117 if (value
== true) {
3119 cursorVisible
= true;
3122 cursorVisible
= false;
3129 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3130 if (decPrivateModeFlag
== true) {
3132 if (value
== true) {
3133 // Select national mode NRC
3136 // Select multi-national mode
3145 if ((type
== DeviceType
.XTERM
)
3146 && (decPrivateModeFlag
== true)
3148 // Mouse: normal tracking mode
3149 if (value
== true) {
3150 mouseProtocol
= MouseProtocol
.NORMAL
;
3152 mouseProtocol
= MouseProtocol
.OFF
;
3158 if ((type
== DeviceType
.XTERM
)
3159 && (decPrivateModeFlag
== true)
3161 // Mouse: normal tracking mode
3162 if (value
== true) {
3163 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
3165 mouseProtocol
= MouseProtocol
.OFF
;
3171 if ((type
== DeviceType
.XTERM
)
3172 && (decPrivateModeFlag
== true)
3174 // Mouse: Any-event tracking mode
3175 if (value
== true) {
3176 mouseProtocol
= MouseProtocol
.ANYEVENT
;
3178 mouseProtocol
= MouseProtocol
.OFF
;
3184 if ((type
== DeviceType
.XTERM
)
3185 && (decPrivateModeFlag
== true)
3187 // Mouse: UTF-8 coordinates
3188 if (value
== true) {
3189 mouseEncoding
= MouseEncoding
.UTF8
;
3191 mouseEncoding
= MouseEncoding
.X10
;
3197 if ((type
== DeviceType
.XTERM
)
3198 && (decPrivateModeFlag
== true)
3200 // Mouse: SGR coordinates
3201 if (value
== true) {
3202 mouseEncoding
= MouseEncoding
.SGR
;
3204 mouseEncoding
= MouseEncoding
.X10
;
3217 * DECSC - Save cursor.
3219 private void decsc() {
3220 savedState
.setTo(currentState
);
3224 * DECRC - Restore cursor.
3226 private void decrc() {
3227 currentState
.setTo(savedState
);
3233 private void ind() {
3234 // Move the cursor and scroll if necessary. If at the bottom line
3235 // already, a scroll up is supposed to be performed.
3236 if (currentState
.cursorY
== scrollRegionBottom
) {
3237 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3239 cursorDown(1, true);
3243 * RI - Reverse index.
3246 // Move the cursor and scroll if necessary. If at the top line
3247 // already, a scroll down is supposed to be performed.
3248 if (currentState
.cursorY
== scrollRegionTop
) {
3249 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3257 private void nel() {
3258 // Move the cursor and scroll if necessary. If at the bottom line
3259 // already, a scroll up is supposed to be performed.
3260 if (currentState
.cursorY
== scrollRegionBottom
) {
3261 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3263 cursorDown(1, true);
3265 // Reset to the beginning of the next line
3266 currentState
.cursorX
= 0;
3270 * DECKPAM - Keypad application mode.
3272 private void deckpam() {
3273 keypadMode
= KeypadMode
.Application
;
3277 * DECKPNM - Keypad numeric mode.
3279 private void deckpnm() {
3280 keypadMode
= KeypadMode
.Numeric
;
3286 * @param n number of spaces to move
3287 * @param honorScrollRegion if true, then do nothing if the cursor is
3288 * outside the scrolling region
3290 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3294 * Special case: if a user moves the cursor from the right margin, we
3295 * have to reset the VT100 right margin flag.
3298 wrapLineFlag
= false;
3301 for (int i
= 0; i
< n
; i
++) {
3302 if (honorScrollRegion
== true) {
3303 // Honor the scrolling region
3304 if ((currentState
.cursorY
< scrollRegionTop
)
3305 || (currentState
.cursorY
> scrollRegionBottom
)
3307 // Outside region, do nothing
3310 // Inside region, go up
3311 top
= scrollRegionTop
;
3313 // Non-scrolling case
3317 if (currentState
.cursorY
> top
) {
3318 currentState
.cursorY
--;
3324 * Move down n spaces.
3326 * @param n number of spaces to move
3327 * @param honorScrollRegion if true, then do nothing if the cursor is
3328 * outside the scrolling region
3330 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3334 * Special case: if a user moves the cursor from the right margin, we
3335 * have to reset the VT100 right margin flag.
3338 wrapLineFlag
= false;
3341 for (int i
= 0; i
< n
; i
++) {
3343 if (honorScrollRegion
== true) {
3344 // Honor the scrolling region
3345 if (currentState
.cursorY
> scrollRegionBottom
) {
3346 // Outside region, do nothing
3349 // Inside region, go down
3350 bottom
= scrollRegionBottom
;
3352 // Non-scrolling case
3353 bottom
= height
- 1;
3356 if (currentState
.cursorY
< bottom
) {
3357 currentState
.cursorY
++;
3363 * Move left n spaces.
3365 * @param n number of spaces to move
3366 * @param honorScrollRegion if true, then do nothing if the cursor is
3367 * outside the scrolling region
3369 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3371 * Special case: if a user moves the cursor from the right margin, we
3372 * have to reset the VT100 right margin flag.
3375 wrapLineFlag
= false;
3378 for (int i
= 0; i
< n
; i
++) {
3379 if (honorScrollRegion
== true) {
3380 // Honor the scrolling region
3381 if ((currentState
.cursorY
< scrollRegionTop
)
3382 || (currentState
.cursorY
> scrollRegionBottom
)
3384 // Outside region, do nothing
3389 if (currentState
.cursorX
> 0) {
3390 currentState
.cursorX
--;
3396 * Move right n spaces.
3398 * @param n number of spaces to move
3399 * @param honorScrollRegion if true, then do nothing if the cursor is
3400 * outside the scrolling region
3402 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3403 int rightMargin
= this.rightMargin
;
3406 * Special case: if a user moves the cursor from the right margin, we
3407 * have to reset the VT100 right margin flag.
3410 wrapLineFlag
= false;
3413 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3414 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3417 for (int i
= 0; i
< n
; i
++) {
3418 if (honorScrollRegion
== true) {
3419 // Honor the scrolling region
3420 if ((currentState
.cursorY
< scrollRegionTop
)
3421 || (currentState
.cursorY
> scrollRegionBottom
)
3423 // Outside region, do nothing
3428 if (currentState
.cursorX
< rightMargin
) {
3429 currentState
.cursorX
++;
3435 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3437 * @param row row to move to
3438 * @param col column to move to
3440 private void cursorPosition(int row
, final int col
) {
3441 int rightMargin
= this.rightMargin
;
3446 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3447 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3450 // Set column number
3451 currentState
.cursorX
= col
;
3453 // Sanity check, bring column back to margin.
3454 if (currentState
.cursorX
> rightMargin
) {
3455 currentState
.cursorX
= rightMargin
;
3459 if (currentState
.originMode
== true) {
3460 row
+= scrollRegionTop
;
3462 if (currentState
.cursorY
< row
) {
3463 cursorDown(row
- currentState
.cursorY
, false);
3464 } else if (currentState
.cursorY
> row
) {
3465 cursorUp(currentState
.cursorY
- row
, false);
3468 wrapLineFlag
= false;
3472 * HTS - Horizontal tabulation set.
3474 private void hts() {
3475 for (Integer stop
: tabStops
) {
3476 if (stop
== currentState
.cursorX
) {
3477 // Already have a tab stop here
3482 // Append a tab stop to the end of the array and resort them
3483 tabStops
.add(currentState
.cursorX
);
3484 Collections
.sort(tabStops
);
3488 * DECSWL - Single-width line.
3490 private void decswl() {
3491 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3492 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3496 * DECDWL - Double-width line.
3498 private void decdwl() {
3499 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3500 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3504 * DECHDL - Double-height + double-width line.
3506 * @param topHalf if true, this sets the row to be the top half row of a
3509 private void dechdl(final boolean topHalf
) {
3510 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3511 if (topHalf
== true) {
3512 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3514 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3519 * DECALN - Screen alignment display.
3521 private void decaln() {
3522 Cell newCell
= new Cell('E');
3523 for (DisplayLine line
: display
) {
3524 for (int i
= 0; i
< line
.length(); i
++) {
3525 line
.replace(i
, newCell
);
3531 * DECSCL - Compatibility level.
3533 private void decscl() {
3534 int i
= getCsiParam(0, 0);
3535 int j
= getCsiParam(1, 0);
3539 currentState
.g0Charset
= CharacterSet
.US
;
3540 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3542 } else if (i
== 62) {
3544 if ((j
== 0) || (j
== 2)) {
3546 } else if (j
== 1) {
3553 * CUD - Cursor down.
3555 private void cud() {
3556 cursorDown(getCsiParam(0, 1, 1, height
), true);
3560 * CUF - Cursor forward.
3562 private void cuf() {
3563 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3567 * CUB - Cursor backward.
3569 private void cub() {
3570 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3576 private void cuu() {
3577 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3581 * CUP - Cursor position.
3583 private void cup() {
3584 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3585 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3589 * CNL - Cursor down and to column 1.
3591 private void cnl() {
3592 cursorDown(getCsiParam(0, 1, 1, height
), true);
3594 cursorLeft(currentState
.cursorX
, true);
3598 * CPL - Cursor up and to column 1.
3600 private void cpl() {
3601 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3603 cursorLeft(currentState
.cursorX
, true);
3607 * CHA - Cursor to column # in current row.
3609 private void cha() {
3610 cursorPosition(currentState
.cursorY
,
3611 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3615 * VPA - Cursor to row #, same column.
3617 private void vpa() {
3618 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3619 currentState
.cursorX
);
3623 * ED - Erase in display.
3626 boolean honorProtected
= false;
3627 boolean decPrivateModeFlag
= false;
3629 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3630 if (collectBuffer
.charAt(i
) == '?') {
3631 decPrivateModeFlag
= true;
3636 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3637 && (decPrivateModeFlag
== true)
3639 honorProtected
= true;
3642 int i
= getCsiParam(0, 0);
3645 // Erase from here to end of screen
3646 if (currentState
.cursorY
< height
- 1) {
3647 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3650 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3651 } else if (i
== 1) {
3652 // Erase from beginning of screen to here
3653 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3655 eraseLine(0, currentState
.cursorX
, honorProtected
);
3656 } else if (i
== 2) {
3657 // Erase entire screen
3658 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3663 * EL - Erase in line.
3666 boolean honorProtected
= false;
3667 boolean decPrivateModeFlag
= false;
3669 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3670 if (collectBuffer
.charAt(i
) == '?') {
3671 decPrivateModeFlag
= true;
3676 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3677 && (decPrivateModeFlag
== true)
3679 honorProtected
= true;
3682 int i
= getCsiParam(0, 0);
3685 // Erase from here to end of line
3686 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3687 } else if (i
== 1) {
3688 // Erase from beginning of line to here
3689 eraseLine(0, currentState
.cursorX
, honorProtected
);
3690 } else if (i
== 2) {
3691 // Erase entire line
3692 eraseLine(0, width
- 1, honorProtected
);
3697 * ECH - Erase # of characters in current row.
3699 private void ech() {
3700 int i
= getCsiParam(0, 1, 1, width
);
3702 // Erase from here to i characters
3703 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3710 int i
= getCsiParam(0, 1);
3712 if ((currentState
.cursorY
>= scrollRegionTop
)
3713 && (currentState
.cursorY
<= scrollRegionBottom
)
3716 // I can get the same effect with a scroll-down
3717 scrollingRegionScrollDown(currentState
.cursorY
,
3718 scrollRegionBottom
, i
);
3723 * DCH - Delete char.
3725 private void dch() {
3726 int n
= getCsiParam(0, 1);
3727 DisplayLine line
= display
.get(currentState
.cursorY
);
3728 Cell blank
= new Cell();
3729 for (int i
= 0; i
< n
; i
++) {
3730 line
.delete(currentState
.cursorX
, blank
);
3735 * ICH - Insert blank char at cursor.
3737 private void ich() {
3738 int n
= getCsiParam(0, 1);
3739 DisplayLine line
= display
.get(currentState
.cursorY
);
3740 Cell blank
= new Cell();
3741 for (int i
= 0; i
< n
; i
++) {
3742 line
.insert(currentState
.cursorX
, blank
);
3750 int i
= getCsiParam(0, 1);
3752 if ((currentState
.cursorY
>= scrollRegionTop
)
3753 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3755 // I can get the same effect with a scroll-down
3756 scrollingRegionScrollUp(currentState
.cursorY
,
3757 scrollRegionBottom
, i
);
3762 * HVP - Horizontal and vertical position.
3764 private void hvp() {
3769 * REP - Repeat character.
3771 private void rep() {
3772 int n
= getCsiParam(0, 1);
3773 for (int i
= 0; i
< n
; i
++) {
3774 printCharacter(repCh
);
3782 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3783 getCsiParam(0, 1, 1, height
));
3790 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3791 getCsiParam(0, 1, 1, height
));
3795 * CBT - Go back X tab stops.
3797 private void cbt() {
3798 int tabsToMove
= getCsiParam(0, 1);
3801 for (int i
= 0; i
< tabsToMove
; i
++) {
3802 int j
= currentState
.cursorX
;
3803 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3804 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3812 j
= tabStops
.get(tabI
);
3814 cursorPosition(currentState
.cursorY
, j
);
3819 * CHT - Advance X tab stops.
3821 private void cht() {
3822 int n
= getCsiParam(0, 1);
3823 for (int i
= 0; i
< n
; i
++) {
3824 advanceToNextTabStop();
3829 * SGR - Select graphics rendition.
3831 private void sgr() {
3833 if (csiParams
.size() == 0) {
3834 currentState
.attr
.reset();
3838 int sgrColorMode
= -1;
3839 boolean idx88Color
= false;
3840 boolean rgbColor
= false;
3844 for (Integer i
: csiParams
) {
3846 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
3848 assert (type
== DeviceType
.XTERM
);
3852 * Indexed color mode, we now have the index number.
3854 if (sgrColorMode
== 38) {
3855 currentState
.attr
.setForeColorRGB(get88Color(i
));
3857 assert (sgrColorMode
== 48);
3858 currentState
.attr
.setBackColorRGB(get88Color(i
));
3867 * RGB color mode, we are collecting tokens.
3871 } else if (rgbGreen
== -1) {
3872 rgbGreen
= i
& 0xFF;
3874 int rgb
= rgbRed
<< 16;
3875 rgb
|= rgbGreen
<< 8;
3878 // System.err.printf("RGB: %08x\n", rgb);
3880 if (sgrColorMode
== 38) {
3881 currentState
.attr
.setForeColorRGB(rgb
);
3883 assert (sgrColorMode
== 48);
3884 currentState
.attr
.setBackColorRGB(rgb
);
3905 * Indexed color mode.
3912 * This is neither indexed nor RGB color. Bail out.
3917 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
3923 currentState
.attr
.reset();
3928 currentState
.attr
.setBold(true);
3933 currentState
.attr
.setUnderline(true);
3938 currentState
.attr
.setBlink(true);
3943 currentState
.attr
.setReverse(true);
3950 if (type
== DeviceType
.XTERM
) {
3960 // Set black foreground
3961 currentState
.attr
.setForeColorRGB(get88Color(8));
3964 // Set red foreground
3965 currentState
.attr
.setForeColorRGB(get88Color(9));
3968 // Set green foreground
3969 currentState
.attr
.setForeColorRGB(get88Color(10));
3972 // Set yellow foreground
3973 currentState
.attr
.setForeColorRGB(get88Color(11));
3976 // Set blue foreground
3977 currentState
.attr
.setForeColorRGB(get88Color(12));
3980 // Set magenta foreground
3981 currentState
.attr
.setForeColorRGB(get88Color(13));
3984 // Set cyan foreground
3985 currentState
.attr
.setForeColorRGB(get88Color(14));
3988 // Set white foreground
3989 currentState
.attr
.setForeColorRGB(get88Color(15));
3993 // Set black background
3994 currentState
.attr
.setBackColorRGB(get88Color(8));
3997 // Set red background
3998 currentState
.attr
.setBackColorRGB(get88Color(9));
4001 // Set green background
4002 currentState
.attr
.setBackColorRGB(get88Color(10));
4005 // Set yellow background
4006 currentState
.attr
.setBackColorRGB(get88Color(11));
4009 // Set blue background
4010 currentState
.attr
.setBackColorRGB(get88Color(12));
4013 // Set magenta background
4014 currentState
.attr
.setBackColorRGB(get88Color(13));
4017 // Set cyan background
4018 currentState
.attr
.setBackColorRGB(get88Color(14));
4021 // Set white background
4022 currentState
.attr
.setBackColorRGB(get88Color(15));
4030 if ((type
== DeviceType
.VT220
)
4031 || (type
== DeviceType
.XTERM
)) {
4037 currentState
.attr
.setBold(false);
4042 currentState
.attr
.setUnderline(false);
4047 currentState
.attr
.setBlink(false);
4052 currentState
.attr
.setReverse(false);
4060 // A true VT100/102/220 does not support color, however everyone
4061 // is used to their terminal emulator supporting color so we will
4062 // unconditionally support color for all DeviceType's.
4067 // Set black foreground
4068 currentState
.attr
.setForeColor(Color
.BLACK
);
4069 currentState
.attr
.setForeColorRGB(-1);
4072 // Set red foreground
4073 currentState
.attr
.setForeColor(Color
.RED
);
4074 currentState
.attr
.setForeColorRGB(-1);
4077 // Set green foreground
4078 currentState
.attr
.setForeColor(Color
.GREEN
);
4079 currentState
.attr
.setForeColorRGB(-1);
4082 // Set yellow foreground
4083 currentState
.attr
.setForeColor(Color
.YELLOW
);
4084 currentState
.attr
.setForeColorRGB(-1);
4087 // Set blue foreground
4088 currentState
.attr
.setForeColor(Color
.BLUE
);
4089 currentState
.attr
.setForeColorRGB(-1);
4092 // Set magenta foreground
4093 currentState
.attr
.setForeColor(Color
.MAGENTA
);
4094 currentState
.attr
.setForeColorRGB(-1);
4097 // Set cyan foreground
4098 currentState
.attr
.setForeColor(Color
.CYAN
);
4099 currentState
.attr
.setForeColorRGB(-1);
4102 // Set white foreground
4103 currentState
.attr
.setForeColor(Color
.WHITE
);
4104 currentState
.attr
.setForeColorRGB(-1);
4107 if (type
== DeviceType
.XTERM
) {
4109 * Xterm supports T.416 / ISO-8613-3 codes to select
4110 * either an indexed color or an RGB value. (It also
4111 * permits these ISO-8613-3 SGR sequences to be separated
4112 * by colons rather than semicolons.)
4114 * We will support only the following:
4116 * 1. Indexed color mode (88- or 256-color modes).
4120 * These cover most of the use cases in the real world.
4122 * HOWEVER, note that this is an awful broken "standard",
4123 * with no way to do it "right". See
4124 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4125 * for a detailed discussion of the current state of RGB
4126 * in various terminals, the point of which is that none
4127 * of them really do the same thing despite all appearing
4131 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4132 * where it is assumed that supporting just the "indexed
4133 * mode" of these sequences (which could align easily
4134 * with existing SGR colors) is assumed to mean full
4135 * support of 24-bit RGB. So it is all or nothing.
4137 * Finally, these sequences break the assumptions of
4138 * standard ECMA-48 style parsers as pointed out at
4139 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4140 * Therefore in order to keep a clean display, we cannot
4141 * parse anything else in this sequence.
4146 // Underscore on, default foreground color
4147 currentState
.attr
.setUnderline(true);
4148 currentState
.attr
.setForeColor(Color
.WHITE
);
4152 // Underscore off, default foreground color
4153 currentState
.attr
.setUnderline(false);
4154 currentState
.attr
.setForeColor(Color
.WHITE
);
4155 currentState
.attr
.setForeColorRGB(-1);
4158 // Set black background
4159 currentState
.attr
.setBackColor(Color
.BLACK
);
4160 currentState
.attr
.setBackColorRGB(-1);
4163 // Set red background
4164 currentState
.attr
.setBackColor(Color
.RED
);
4165 currentState
.attr
.setBackColorRGB(-1);
4168 // Set green background
4169 currentState
.attr
.setBackColor(Color
.GREEN
);
4170 currentState
.attr
.setBackColorRGB(-1);
4173 // Set yellow background
4174 currentState
.attr
.setBackColor(Color
.YELLOW
);
4175 currentState
.attr
.setBackColorRGB(-1);
4178 // Set blue background
4179 currentState
.attr
.setBackColor(Color
.BLUE
);
4180 currentState
.attr
.setBackColorRGB(-1);
4183 // Set magenta background
4184 currentState
.attr
.setBackColor(Color
.MAGENTA
);
4185 currentState
.attr
.setBackColorRGB(-1);
4188 // Set cyan background
4189 currentState
.attr
.setBackColor(Color
.CYAN
);
4190 currentState
.attr
.setBackColorRGB(-1);
4193 // Set white background
4194 currentState
.attr
.setBackColor(Color
.WHITE
);
4195 currentState
.attr
.setBackColorRGB(-1);
4198 if (type
== DeviceType
.XTERM
) {
4200 * Xterm supports T.416 / ISO-8613-3 codes to select
4201 * either an indexed color or an RGB value. (It also
4202 * permits these ISO-8613-3 SGR sequences to be separated
4203 * by colons rather than semicolons.)
4205 * We will support only the following:
4207 * 1. Indexed color mode (88- or 256-color modes).
4211 * These cover most of the use cases in the real world.
4213 * HOWEVER, note that this is an awful broken "standard",
4214 * with no way to do it "right". See
4215 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4216 * for a detailed discussion of the current state of RGB
4217 * in various terminals, the point of which is that none
4218 * of them really do the same thing despite all appearing
4222 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4223 * where it is assumed that supporting just the "indexed
4224 * mode" of these sequences (which could align easily
4225 * with existing SGR colors) is assumed to mean full
4226 * support of 24-bit RGB. So it is all or nothing.
4228 * Finally, these sequences break the assumptions of
4229 * standard ECMA-48 style parsers as pointed out at
4230 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4231 * Therefore in order to keep a clean display, we cannot
4232 * parse anything else in this sequence.
4239 // Default background
4240 currentState
.attr
.setBackColor(Color
.BLACK
);
4241 currentState
.attr
.setBackColorRGB(-1);
4251 * DA - Device attributes.
4254 int extendedFlag
= 0;
4256 if (collectBuffer
.length() > 0) {
4257 String args
= collectBuffer
.substring(1);
4258 if (collectBuffer
.charAt(0) == '>') {
4260 if (collectBuffer
.length() >= 2) {
4261 i
= Integer
.parseInt(args
);
4263 } else if (collectBuffer
.charAt(0) == '=') {
4265 if (collectBuffer
.length() >= 2) {
4266 i
= Integer
.parseInt(args
);
4269 // Unknown code, bail out
4274 if ((i
!= 0) && (i
!= 1)) {
4278 if ((extendedFlag
== 0) && (i
== 0)) {
4279 // Send string directly to remote side
4280 writeRemote(deviceTypeResponse());
4284 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4286 if ((extendedFlag
== 1) && (i
== 0)) {
4288 * Request "What type of terminal are you, what is your
4289 * firmware version, and what hardware options do you have
4292 * Respond: "I am a VT220 (identification code of 1), my
4293 * firmware version is _____ (Pv), and I have _____ Po
4294 * options installed."
4300 if (s8c1t
== true) {
4301 writeRemote("\u009b>1;10;0c");
4303 writeRemote("\033[>1;10;0c");
4309 if ((extendedFlag
== 2) && (i
== 0)) {
4312 * Request "What is your unit ID?"
4314 * Respond: "I was manufactured at site 00 and have a unique ID
4318 writeRemote("\033P!|00010203\033\\");
4323 * DECSTBM - Set top and bottom margins.
4325 private void decstbm() {
4326 boolean decPrivateModeFlag
= false;
4328 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4329 if (collectBuffer
.charAt(i
) == '?') {
4330 decPrivateModeFlag
= true;
4334 if (decPrivateModeFlag
) {
4335 // This could be restore DEC private mode values.
4339 int top
= getCsiParam(0, 1, 1, height
) - 1;
4340 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4345 scrollRegionTop
= top
;
4346 scrollRegionBottom
= bottom
;
4349 cursorPosition(0, 0);
4354 * DECREQTPARM - Request terminal parameters.
4356 private void decreqtparm() {
4357 int i
= getCsiParam(0, 0);
4359 if ((i
!= 0) && (i
!= 1)) {
4366 * Request terminal parameters.
4370 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4371 * (CLoCk MULtiplier = 1, STP option flags = 0)
4375 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4378 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4380 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4386 * DECSCA - Select Character Attributes.
4388 private void decsca() {
4389 int i
= getCsiParam(0, 0);
4391 if ((i
== 0) || (i
== 2)) {
4393 currentState
.attr
.setProtect(false);
4397 currentState
.attr
.setProtect(true);
4402 * DECSTR - Soft Terminal Reset.
4404 private void decstr() {
4405 // Do exactly like RIS - Reset to initial state
4407 // Do I clear screen too? I think so...
4408 eraseScreen(0, 0, height
- 1, width
- 1, false);
4409 cursorPosition(0, 0);
4413 * DSR - Device status report.
4415 private void dsr() {
4416 boolean decPrivateModeFlag
= false;
4417 int row
= currentState
.cursorY
;
4419 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4420 if (collectBuffer
.charAt(i
) == '?') {
4421 decPrivateModeFlag
= true;
4426 int i
= getCsiParam(0, 0);
4431 // Request status report. Respond with "OK, no malfunction."
4433 // Send string directly to remote side
4434 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4437 writeRemote("\u009b0n");
4439 writeRemote("\033[0n");
4444 // Request cursor position. Respond with current position.
4445 if (currentState
.originMode
== true) {
4446 row
-= scrollRegionTop
;
4449 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4452 str
= String
.format("\u009b%d;%dR", row
+ 1,
4453 currentState
.cursorX
+ 1);
4455 str
= String
.format("\033[%d;%dR", row
+ 1,
4456 currentState
.cursorX
+ 1);
4459 // Send string directly to remote side
4464 if (decPrivateModeFlag
== true) {
4466 // Request printer status report. Respond with "Printer not
4469 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4470 && (s8c1t
== true)) {
4471 writeRemote("\u009b?13n");
4473 writeRemote("\033[?13n");
4479 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4480 && (decPrivateModeFlag
== true)
4483 // Request user-defined keys are locked or unlocked. Respond
4484 // with "User-defined keys are locked."
4486 if (s8c1t
== true) {
4487 writeRemote("\u009b?21n");
4489 writeRemote("\033[?21n");
4495 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4496 && (decPrivateModeFlag
== true)
4499 // Request keyboard language. Respond with "Keyboard
4500 // language is North American."
4502 if (s8c1t
== true) {
4503 writeRemote("\u009b?27;1n");
4505 writeRemote("\033[?27;1n");
4512 // Some other option, ignore
4518 * TBC - Tabulation clear.
4520 private void tbc() {
4521 int i
= getCsiParam(0, 0);
4523 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4524 for (Integer stop
: tabStops
) {
4525 if (stop
== currentState
.cursorX
) {
4530 tabStops
= newStops
;
4538 * Erase the characters in the current line from the start column to the
4539 * end column, inclusive.
4541 * @param start starting column to erase (between 0 and width - 1)
4542 * @param end ending column to erase (between 0 and width - 1)
4543 * @param honorProtected if true, do not erase characters with the
4544 * protected attribute set
4546 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4551 if (end
> width
- 1) {
4558 for (int i
= start
; i
<= end
; i
++) {
4559 DisplayLine line
= display
.get(currentState
.cursorY
);
4560 if ((!honorProtected
)
4561 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4568 * From the VT102 manual:
4570 * Erasing a character also erases any character
4571 * attribute of the character.
4577 * Erase with the current color a.k.a. back-color erase
4580 line
.setChar(i
, ' ');
4581 line
.setAttr(i
, currentState
.attr
);
4589 * Erase a rectangular section of the screen, inclusive. end column,
4592 * @param startRow starting row to erase (between 0 and height - 1)
4593 * @param startCol starting column to erase (between 0 and width - 1)
4594 * @param endRow ending row to erase (between 0 and height - 1)
4595 * @param endCol ending column to erase (between 0 and width - 1)
4596 * @param honorProtected if true, do not erase characters with the
4597 * protected attribute set
4599 private void eraseScreen(final int startRow
, final int startCol
,
4600 final int endRow
, final int endCol
, final boolean honorProtected
) {
4608 || (endRow
< startRow
)
4609 || (endCol
< startCol
)
4614 oldCursorY
= currentState
.cursorY
;
4615 for (int i
= startRow
; i
<= endRow
; i
++) {
4616 currentState
.cursorY
= i
;
4617 eraseLine(startCol
, endCol
, honorProtected
);
4619 // Erase display clears the double attributes
4620 display
.get(i
).setDoubleWidth(false);
4621 display
.get(i
).setDoubleHeight(0);
4623 currentState
.cursorY
= oldCursorY
;
4627 * VT220 printer functions. All of these are parsed, but won't do
4630 private void printerFunctions() {
4631 boolean decPrivateModeFlag
= false;
4632 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4633 if (collectBuffer
.charAt(i
) == '?') {
4634 decPrivateModeFlag
= true;
4639 int i
= getCsiParam(0, 0);
4644 if (decPrivateModeFlag
== false) {
4650 if (decPrivateModeFlag
== true) {
4651 // Print cursor line
4656 if (decPrivateModeFlag
== true) {
4657 // Auto print mode OFF
4659 // Printer controller OFF
4661 // Characters re-appear on the screen
4662 printerControllerMode
= false;
4667 if (decPrivateModeFlag
== true) {
4671 // Printer controller
4673 // Characters get sucked into oblivion
4674 printerControllerMode
= true;
4685 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4686 * of remote systems will send an XTerm title sequence even if TERM isn't
4689 * @param xtermChar the character received from the remote side
4691 private void oscPut(final char xtermChar
) {
4692 // System.err.println("oscPut: " + xtermChar);
4695 collectBuffer
.append(xtermChar
);
4698 if ((xtermChar
== 0x07)
4699 || (collectBuffer
.toString().endsWith("\033\\"))
4702 if (xtermChar
== 0x07) {
4703 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
4705 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4708 String
[] p
= args
.split(";");
4710 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4717 if (p
[0].equals("4")) {
4718 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
4719 // Set a color index value
4721 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
4722 } catch (NumberFormatException e
) {
4728 if (p
[0].equals("10")) {
4729 if (p
[1].equals("?")) {
4730 // Respond with foreground color.
4731 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToForegroundColor(currentState
.attr
);
4733 writeRemote(String
.format(
4734 "\033]10;rgb:%04x/%04x/%04x\033\\",
4735 color
.getRed() << 8,
4736 color
.getGreen() << 8,
4737 color
.getBlue() << 8));
4741 if (p
[0].equals("11")) {
4742 if (p
[1].equals("?")) {
4743 // Respond with background color.
4744 java
.awt
.Color color
= jexer
.backend
.SwingTerminal
.attrToBackgroundColor(currentState
.attr
);
4746 writeRemote(String
.format(
4747 "\033]11;rgb:%04x/%04x/%04x\033\\",
4748 color
.getRed() << 8,
4749 color
.getGreen() << 8,
4750 color
.getBlue() << 8));
4755 // Go to SCAN_GROUND state
4762 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
4763 * Jexer ECMA48Terminal to talk to ECMA48.
4765 * @param pmChar the character received from the remote side
4767 private void pmPut(final char pmChar
) {
4768 // System.err.println("pmPut: " + pmChar);
4771 collectBuffer
.append(pmChar
);
4774 if (collectBuffer
.toString().endsWith("\033\\")) {
4776 arg
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4778 // System.err.println("arg: '" + arg + "'");
4780 if (arg
.equals("hideMousePointer")) {
4781 hideMousePointer
= true;
4783 if (arg
.equals("showMousePointer")) {
4784 hideMousePointer
= false;
4787 // Go to SCAN_GROUND state
4794 * Perform xterm window operations.
4796 private void xtermWindowOps() {
4797 boolean xtermPrivateModeFlag
= false;
4799 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4800 if (collectBuffer
.charAt(i
) == '?') {
4801 xtermPrivateModeFlag
= true;
4806 int i
= getCsiParam(0, 0);
4808 if (!xtermPrivateModeFlag
) {
4811 // Report xterm text area size in pixels as CSI 4 ; height ;
4813 writeRemote(String
.format("\033[4;%d;%dt", textHeight
* height
,
4814 textWidth
* width
));
4817 // Report character size in pixels as CSI 6 ; height ; width
4819 writeRemote(String
.format("\033[6;%d;%dt", textHeight
,
4823 // Report the text are size in characters as CSI 8 ; height ;
4825 writeRemote(String
.format("\033[8;%d;%dt", height
, width
));
4834 * Respond to xterm sixel query.
4836 private void xtermSixelQuery() {
4837 int item
= getCsiParam(0, 0);
4838 int action
= getCsiParam(1, 0);
4839 int value
= getCsiParam(2, 0);
4844 // Report number of color registers.
4845 writeRemote(String
.format("\033[?%d;%d;%dS", item
, 0, 1024));
4852 // We will not support this option.
4853 writeRemote(String
.format("\033[?%d;%dS", item
, action
));
4857 * Run this input character through the ECMA48 state machine.
4859 * @param ch character from the remote side
4861 private void consume(int ch
) {
4864 // System.err.printf("%c STATE = %s\n", ch, scanState);
4866 // Special case for VT10x: 7-bit characters only
4867 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4871 // Special "anywhere" states
4873 // 18, 1A --> execute, then switch to SCAN_GROUND
4874 if ((ch
== 0x18) || (ch
== 0x1A)) {
4875 // CAN and SUB abort escape sequences
4880 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4884 if ((type
== DeviceType
.XTERM
)
4885 && ((scanState
== ScanState
.OSC_STRING
)
4886 || (scanState
== ScanState
.DCS_SIXEL
)
4887 || (scanState
== ScanState
.SOSPMAPC_STRING
))
4889 // Xterm can pass ESCAPE to its OSC sequence.
4890 // Xterm can pass ESCAPE to its DCS sequence.
4891 // Jexer can pass ESCAPE to its PM sequence.
4892 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
4893 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4894 && (scanState
!= ScanState
.DCS_IGNORE
)
4895 && (scanState
!= ScanState
.DCS_PARAM
)
4896 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4898 scanState
= ScanState
.ESCAPE
;
4903 // 0x9B == CSI 8-bit sequence
4905 scanState
= ScanState
.CSI_ENTRY
;
4909 // 0x9D goes to ScanState.OSC_STRING
4911 scanState
= ScanState
.OSC_STRING
;
4915 // 0x90 goes to DCS_ENTRY
4917 scanState
= ScanState
.DCS_ENTRY
;
4921 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4922 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4923 scanState
= ScanState
.SOSPMAPC_STRING
;
4927 // 0x7F (DEL) is always discarded
4932 switch (scanState
) {
4935 // 00-17, 19, 1C-1F --> execute
4936 // 80-8F, 91-9A, 9C --> execute
4937 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4938 handleControlChar((char) ch
);
4942 if (((ch
>= 0x20) && (ch
<= 0x7F))
4946 // VT220 printer --> trash bin
4947 if (((type
== DeviceType
.VT220
)
4948 || (type
== DeviceType
.XTERM
))
4949 && (printerControllerMode
== true)
4954 // Hang onto this character
4955 repCh
= mapCharacter(ch
);
4957 // Print this character
4958 printCharacter(repCh
);
4963 // 00-17, 19, 1C-1F --> execute
4965 handleControlChar((char) ch
);
4969 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4970 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4972 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4976 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4977 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4988 // DECSC - Save cursor
4989 // Note this code overlaps both ANSI and VT52 mode
4994 // DECRC - Restore cursor
4995 // Note this code overlaps both ANSI and VT52 mode
5004 if (vt52Mode
== true) {
5005 // DECANM - Enter ANSI mode
5007 arrowKeyMode
= ArrowKeyMode
.VT100
;
5010 * From the VT102 docs: "You use ANSI mode to select
5011 * most terminal features; the terminal uses the same
5012 * features when it switches to VT52 mode. You
5013 * cannot, however, change most of these features in
5016 * In other words, do not reset any other attributes
5017 * when switching between VT52 submode and ANSI.
5021 currentState
.g0Charset
= CharacterSet
.US
;
5022 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5024 singleshift
= Singleshift
.NONE
;
5025 currentState
.glLockshift
= LockshiftMode
.NONE
;
5026 currentState
.grLockshift
= LockshiftMode
.NONE
;
5030 // DECKPAM - Keypad application mode
5031 // Note this code overlaps both ANSI and VT52 mode
5035 // DECKPNM - Keypad numeric mode
5036 // Note this code overlaps both ANSI and VT52 mode
5043 if (vt52Mode
== true) {
5044 // Cursor up, and stop at the top without scrolling
5049 if (vt52Mode
== true) {
5050 // Cursor down, and stop at the bottom without scrolling
5051 cursorDown(1, false);
5055 if (vt52Mode
== true) {
5056 // Cursor right, and stop at the right without scrolling
5057 cursorRight(1, false);
5061 if (vt52Mode
== true) {
5062 // Cursor left, and stop at the left without scrolling
5063 cursorLeft(1, false);
5070 if (vt52Mode
== true) {
5078 if (vt52Mode
== true) {
5079 // G0 --> Special graphics
5080 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
5084 if (vt52Mode
== true) {
5086 currentState
.g0Charset
= CharacterSet
.US
;
5090 if (vt52Mode
== true) {
5092 cursorPosition(0, 0);
5094 // HTS - Horizontal tabulation set
5099 if (vt52Mode
== true) {
5100 // Reverse line feed. Same as RI.
5105 if (vt52Mode
== true) {
5106 // Erase to end of screen
5107 eraseLine(currentState
.cursorX
, width
- 1, false);
5108 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
5113 if (vt52Mode
== true) {
5114 // Erase to end of line
5115 eraseLine(currentState
.cursorX
, width
- 1, false);
5121 if (vt52Mode
== true) {
5124 // RI - Reverse index
5129 if (vt52Mode
== false) {
5131 singleshift
= Singleshift
.SS2
;
5135 if (vt52Mode
== false) {
5137 singleshift
= Singleshift
.SS3
;
5144 if ((ch
>= 0x51) && (ch
<= 0x57)) {
5160 if (vt52Mode
== true) {
5161 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
5169 if (vt52Mode
== true) {
5171 // Send string directly to remote side
5172 writeRemote("\033/Z");
5175 // Send string directly to remote side
5176 writeRemote(deviceTypeResponse());
5187 // VT52 cannot get to any of these other states
5188 if (vt52Mode
== true) {
5193 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
5200 // RIS - Reset to initial state
5202 // Do I clear screen too? I think so...
5203 eraseScreen(0, 0, height
- 1, width
- 1, false);
5204 cursorPosition(0, 0);
5218 if ((type
== DeviceType
.VT220
)
5219 || (type
== DeviceType
.XTERM
)) {
5221 // VT220 lockshift G2 into GL
5222 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
5227 if ((type
== DeviceType
.VT220
)
5228 || (type
== DeviceType
.XTERM
)) {
5230 // VT220 lockshift G3 into GL
5231 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
5249 if ((type
== DeviceType
.VT220
)
5250 || (type
== DeviceType
.XTERM
)) {
5252 // VT220 lockshift G3 into GR
5253 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
5258 if ((type
== DeviceType
.VT220
)
5259 || (type
== DeviceType
.XTERM
)) {
5261 // VT220 lockshift G2 into GR
5262 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
5268 if ((type
== DeviceType
.VT220
)
5269 || (type
== DeviceType
.XTERM
)) {
5271 // VT220 lockshift G1 into GR
5272 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
5282 // 0x5B goes to CSI_ENTRY
5284 scanState
= ScanState
.CSI_ENTRY
;
5287 // 0x5D goes to OSC_STRING
5289 scanState
= ScanState
.OSC_STRING
;
5292 // 0x50 goes to DCS_ENTRY
5294 scanState
= ScanState
.DCS_ENTRY
;
5297 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5298 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
5299 scanState
= ScanState
.SOSPMAPC_STRING
;
5304 case ESCAPE_INTERMEDIATE
:
5305 // 00-17, 19, 1C-1F --> execute
5307 handleControlChar((char) ch
);
5310 // 20-2F --> collect
5311 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5315 // 30-7E --> dispatch, then switch to GROUND
5316 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
5319 if ((collectBuffer
.length() == 1)
5320 && (collectBuffer
.charAt(0) == '(')) {
5321 // G0 --> Special graphics
5322 currentState
.g0Charset
= CharacterSet
.DRAWING
;
5324 if ((collectBuffer
.length() == 1)
5325 && (collectBuffer
.charAt(0) == ')')) {
5326 // G1 --> Special graphics
5327 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5329 if ((type
== DeviceType
.VT220
)
5330 || (type
== DeviceType
.XTERM
)) {
5332 if ((collectBuffer
.length() == 1)
5333 && (collectBuffer
.charAt(0) == '*')) {
5334 // G2 --> Special graphics
5335 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5337 if ((collectBuffer
.length() == 1)
5338 && (collectBuffer
.charAt(0) == '+')) {
5339 // G3 --> Special graphics
5340 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5345 if ((collectBuffer
.length() == 1)
5346 && (collectBuffer
.charAt(0) == '(')) {
5347 // G0 --> Alternate character ROM standard character set
5348 currentState
.g0Charset
= CharacterSet
.ROM
;
5350 if ((collectBuffer
.length() == 1)
5351 && (collectBuffer
.charAt(0) == ')')) {
5352 // G1 --> Alternate character ROM standard character set
5353 currentState
.g1Charset
= CharacterSet
.ROM
;
5357 if ((collectBuffer
.length() == 1)
5358 && (collectBuffer
.charAt(0) == '(')) {
5359 // G0 --> Alternate character ROM special graphics
5360 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5362 if ((collectBuffer
.length() == 1)
5363 && (collectBuffer
.charAt(0) == ')')) {
5364 // G1 --> Alternate character ROM special graphics
5365 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5369 if ((collectBuffer
.length() == 1)
5370 && (collectBuffer
.charAt(0) == '#')) {
5371 // DECDHL - Double-height line (top half)
5376 if ((collectBuffer
.length() == 1)
5377 && (collectBuffer
.charAt(0) == '#')) {
5378 // DECDHL - Double-height line (bottom half)
5381 if ((type
== DeviceType
.VT220
)
5382 || (type
== DeviceType
.XTERM
)) {
5384 if ((collectBuffer
.length() == 1)
5385 && (collectBuffer
.charAt(0) == '(')) {
5387 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5389 if ((collectBuffer
.length() == 1)
5390 && (collectBuffer
.charAt(0) == ')')) {
5392 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5394 if ((collectBuffer
.length() == 1)
5395 && (collectBuffer
.charAt(0) == '*')) {
5397 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5399 if ((collectBuffer
.length() == 1)
5400 && (collectBuffer
.charAt(0) == '+')) {
5402 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5407 if ((collectBuffer
.length() == 1)
5408 && (collectBuffer
.charAt(0) == '#')) {
5409 // DECSWL - Single-width line
5412 if ((type
== DeviceType
.VT220
)
5413 || (type
== DeviceType
.XTERM
)) {
5415 if ((collectBuffer
.length() == 1)
5416 && (collectBuffer
.charAt(0) == '(')) {
5418 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5420 if ((collectBuffer
.length() == 1)
5421 && (collectBuffer
.charAt(0) == ')')) {
5423 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5425 if ((collectBuffer
.length() == 1)
5426 && (collectBuffer
.charAt(0) == '*')) {
5428 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5430 if ((collectBuffer
.length() == 1)
5431 && (collectBuffer
.charAt(0) == '+')) {
5433 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5438 if ((collectBuffer
.length() == 1)
5439 && (collectBuffer
.charAt(0) == '#')) {
5440 // DECDWL - Double-width line
5443 if ((type
== DeviceType
.VT220
)
5444 || (type
== DeviceType
.XTERM
)) {
5446 if ((collectBuffer
.length() == 1)
5447 && (collectBuffer
.charAt(0) == '(')) {
5449 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5451 if ((collectBuffer
.length() == 1)
5452 && (collectBuffer
.charAt(0) == ')')) {
5454 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5456 if ((collectBuffer
.length() == 1)
5457 && (collectBuffer
.charAt(0) == '*')) {
5459 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5461 if ((collectBuffer
.length() == 1)
5462 && (collectBuffer
.charAt(0) == '+')) {
5464 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5469 if ((type
== DeviceType
.VT220
)
5470 || (type
== DeviceType
.XTERM
)) {
5472 if ((collectBuffer
.length() == 1)
5473 && (collectBuffer
.charAt(0) == '(')) {
5475 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5477 if ((collectBuffer
.length() == 1)
5478 && (collectBuffer
.charAt(0) == ')')) {
5480 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5482 if ((collectBuffer
.length() == 1)
5483 && (collectBuffer
.charAt(0) == '*')) {
5485 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5487 if ((collectBuffer
.length() == 1)
5488 && (collectBuffer
.charAt(0) == '+')) {
5490 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5495 if ((collectBuffer
.length() == 1)
5496 && (collectBuffer
.charAt(0) == '#')) {
5497 // DECALN - Screen alignment display
5506 if ((type
== DeviceType
.VT220
)
5507 || (type
== DeviceType
.XTERM
)) {
5509 if ((collectBuffer
.length() == 1)
5510 && (collectBuffer
.charAt(0) == '(')) {
5511 // G0 --> DEC_SUPPLEMENTAL
5512 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5514 if ((collectBuffer
.length() == 1)
5515 && (collectBuffer
.charAt(0) == ')')) {
5516 // G1 --> DEC_SUPPLEMENTAL
5517 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5519 if ((collectBuffer
.length() == 1)
5520 && (collectBuffer
.charAt(0) == '*')) {
5521 // G2 --> DEC_SUPPLEMENTAL
5522 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5524 if ((collectBuffer
.length() == 1)
5525 && (collectBuffer
.charAt(0) == '+')) {
5526 // G3 --> DEC_SUPPLEMENTAL
5527 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5532 if ((type
== DeviceType
.VT220
)
5533 || (type
== DeviceType
.XTERM
)) {
5535 if ((collectBuffer
.length() == 1)
5536 && (collectBuffer
.charAt(0) == '(')) {
5538 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5540 if ((collectBuffer
.length() == 1)
5541 && (collectBuffer
.charAt(0) == ')')) {
5543 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5545 if ((collectBuffer
.length() == 1)
5546 && (collectBuffer
.charAt(0) == '*')) {
5548 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5550 if ((collectBuffer
.length() == 1)
5551 && (collectBuffer
.charAt(0) == '+')) {
5553 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5562 if ((collectBuffer
.length() == 1)
5563 && (collectBuffer
.charAt(0) == '(')) {
5564 // G0 --> United Kingdom set
5565 currentState
.g0Charset
= CharacterSet
.UK
;
5567 if ((collectBuffer
.length() == 1)
5568 && (collectBuffer
.charAt(0) == ')')) {
5569 // G1 --> United Kingdom set
5570 currentState
.g1Charset
= CharacterSet
.UK
;
5572 if ((type
== DeviceType
.VT220
)
5573 || (type
== DeviceType
.XTERM
)) {
5575 if ((collectBuffer
.length() == 1)
5576 && (collectBuffer
.charAt(0) == '*')) {
5577 // G2 --> United Kingdom set
5578 currentState
.g2Charset
= CharacterSet
.UK
;
5580 if ((collectBuffer
.length() == 1)
5581 && (collectBuffer
.charAt(0) == '+')) {
5582 // G3 --> United Kingdom set
5583 currentState
.g3Charset
= CharacterSet
.UK
;
5588 if ((collectBuffer
.length() == 1)
5589 && (collectBuffer
.charAt(0) == '(')) {
5591 currentState
.g0Charset
= CharacterSet
.US
;
5593 if ((collectBuffer
.length() == 1)
5594 && (collectBuffer
.charAt(0) == ')')) {
5596 currentState
.g1Charset
= CharacterSet
.US
;
5598 if ((type
== DeviceType
.VT220
)
5599 || (type
== DeviceType
.XTERM
)) {
5601 if ((collectBuffer
.length() == 1)
5602 && (collectBuffer
.charAt(0) == '*')) {
5604 currentState
.g2Charset
= CharacterSet
.US
;
5606 if ((collectBuffer
.length() == 1)
5607 && (collectBuffer
.charAt(0) == '+')) {
5609 currentState
.g3Charset
= CharacterSet
.US
;
5614 if ((type
== DeviceType
.VT220
)
5615 || (type
== DeviceType
.XTERM
)) {
5617 if ((collectBuffer
.length() == 1)
5618 && (collectBuffer
.charAt(0) == '(')) {
5620 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5622 if ((collectBuffer
.length() == 1)
5623 && (collectBuffer
.charAt(0) == ')')) {
5625 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5627 if ((collectBuffer
.length() == 1)
5628 && (collectBuffer
.charAt(0) == '*')) {
5630 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5632 if ((collectBuffer
.length() == 1)
5633 && (collectBuffer
.charAt(0) == '+')) {
5635 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5642 if ((type
== DeviceType
.VT220
)
5643 || (type
== DeviceType
.XTERM
)) {
5645 if ((collectBuffer
.length() == 1)
5646 && (collectBuffer
.charAt(0) == '(')) {
5648 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5650 if ((collectBuffer
.length() == 1)
5651 && (collectBuffer
.charAt(0) == ')')) {
5653 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5655 if ((collectBuffer
.length() == 1)
5656 && (collectBuffer
.charAt(0) == '*')) {
5658 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5660 if ((collectBuffer
.length() == 1)
5661 && (collectBuffer
.charAt(0) == '+')) {
5663 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5668 if ((type
== DeviceType
.VT220
)
5669 || (type
== DeviceType
.XTERM
)) {
5671 if ((collectBuffer
.length() == 1)
5672 && (collectBuffer
.charAt(0) == ' ')) {
5679 if ((type
== DeviceType
.VT220
)
5680 || (type
== DeviceType
.XTERM
)) {
5682 if ((collectBuffer
.length() == 1)
5683 && (collectBuffer
.charAt(0) == ' ')) {
5690 if ((type
== DeviceType
.VT220
)
5691 || (type
== DeviceType
.XTERM
)) {
5693 if ((collectBuffer
.length() == 1)
5694 && (collectBuffer
.charAt(0) == '(')) {
5696 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5698 if ((collectBuffer
.length() == 1)
5699 && (collectBuffer
.charAt(0) == ')')) {
5701 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5703 if ((collectBuffer
.length() == 1)
5704 && (collectBuffer
.charAt(0) == '*')) {
5706 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5708 if ((collectBuffer
.length() == 1)
5709 && (collectBuffer
.charAt(0) == '+')) {
5711 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5719 if ((type
== DeviceType
.VT220
)
5720 || (type
== DeviceType
.XTERM
)) {
5722 if ((collectBuffer
.length() == 1)
5723 && (collectBuffer
.charAt(0) == '(')) {
5725 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5727 if ((collectBuffer
.length() == 1)
5728 && (collectBuffer
.charAt(0) == ')')) {
5730 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5732 if ((collectBuffer
.length() == 1)
5733 && (collectBuffer
.charAt(0) == '*')) {
5735 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5737 if ((collectBuffer
.length() == 1)
5738 && (collectBuffer
.charAt(0) == '+')) {
5740 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5751 if ((type
== DeviceType
.VT220
)
5752 || (type
== DeviceType
.XTERM
)) {
5754 if ((collectBuffer
.length() == 1)
5755 && (collectBuffer
.charAt(0) == '(')) {
5757 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5759 if ((collectBuffer
.length() == 1)
5760 && (collectBuffer
.charAt(0) == ')')) {
5762 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5764 if ((collectBuffer
.length() == 1)
5765 && (collectBuffer
.charAt(0) == '*')) {
5767 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5769 if ((collectBuffer
.length() == 1)
5770 && (collectBuffer
.charAt(0) == '+')) {
5772 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5777 if ((type
== DeviceType
.VT220
)
5778 || (type
== DeviceType
.XTERM
)) {
5780 if ((collectBuffer
.length() == 1)
5781 && (collectBuffer
.charAt(0) == '(')) {
5783 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5785 if ((collectBuffer
.length() == 1)
5786 && (collectBuffer
.charAt(0) == ')')) {
5788 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5790 if ((collectBuffer
.length() == 1)
5791 && (collectBuffer
.charAt(0) == '*')) {
5793 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5795 if ((collectBuffer
.length() == 1)
5796 && (collectBuffer
.charAt(0) == '+')) {
5798 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5810 if ((type
== DeviceType
.VT220
)
5811 || (type
== DeviceType
.XTERM
)) {
5813 if ((collectBuffer
.length() == 1)
5814 && (collectBuffer
.charAt(0) == '(')) {
5816 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5818 if ((collectBuffer
.length() == 1)
5819 && (collectBuffer
.charAt(0) == ')')) {
5821 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5823 if ((collectBuffer
.length() == 1)
5824 && (collectBuffer
.charAt(0) == '*')) {
5826 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5828 if ((collectBuffer
.length() == 1)
5829 && (collectBuffer
.charAt(0) == '+')) {
5831 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5836 if ((type
== DeviceType
.VT220
)
5837 || (type
== DeviceType
.XTERM
)) {
5839 if ((collectBuffer
.length() == 1)
5840 && (collectBuffer
.charAt(0) == '(')) {
5842 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5844 if ((collectBuffer
.length() == 1)
5845 && (collectBuffer
.charAt(0) == ')')) {
5847 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5849 if ((collectBuffer
.length() == 1)
5850 && (collectBuffer
.charAt(0) == '*')) {
5852 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5854 if ((collectBuffer
.length() == 1)
5855 && (collectBuffer
.charAt(0) == '+')) {
5857 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5904 // 0x9C goes to GROUND
5912 // 00-17, 19, 1C-1F --> execute
5914 handleControlChar((char) ch
);
5917 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5918 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5920 scanState
= ScanState
.CSI_INTERMEDIATE
;
5923 // 30-39, 3B --> param, then switch to CSI_PARAM
5924 if ((ch
>= '0') && (ch
<= '9')) {
5926 scanState
= ScanState
.CSI_PARAM
;
5930 scanState
= ScanState
.CSI_PARAM
;
5933 // 3C-3F --> collect, then switch to CSI_PARAM
5934 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5936 scanState
= ScanState
.CSI_PARAM
;
5939 // 40-7E --> dispatch, then switch to GROUND
5940 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5943 // ICH - Insert character
5951 // CUD - Cursor down
5955 // CUF - Cursor forward
5959 // CUB - Cursor backward
5963 // CNL - Cursor down and to column 1
5964 if (type
== DeviceType
.XTERM
) {
5969 // CPL - Cursor up and to column 1
5970 if (type
== DeviceType
.XTERM
) {
5975 // CHA - Cursor to column # in current row
5976 if (type
== DeviceType
.XTERM
) {
5981 // CUP - Cursor position
5985 // CHT - Cursor forward X tab stops (default 1)
5986 if (type
== DeviceType
.XTERM
) {
5991 // ED - Erase in display
5995 // EL - Erase in line
6010 // DCH - Delete character
6017 // Scroll up X lines (default 1)
6018 if (type
== DeviceType
.XTERM
) {
6019 boolean xtermPrivateModeFlag
= false;
6020 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6021 if (collectBuffer
.charAt(i
) == '?') {
6022 xtermPrivateModeFlag
= true;
6026 if (xtermPrivateModeFlag
) {
6034 // Scroll down X lines (default 1)
6035 if (type
== DeviceType
.XTERM
) {
6044 if ((type
== DeviceType
.VT220
)
6045 || (type
== DeviceType
.XTERM
)) {
6047 // ECH - Erase character
6054 // CBT - Cursor backward X tab stops (default 1)
6055 if (type
== DeviceType
.XTERM
) {
6066 // HPA - Cursor to column # in current row. Same as CHA
6067 if (type
== DeviceType
.XTERM
) {
6072 // HPR - Cursor right. Same as CUF
6073 if (type
== DeviceType
.XTERM
) {
6078 // REP - Repeat last char X times
6079 if (type
== DeviceType
.XTERM
) {
6084 // DA - Device attributes
6088 // VPA - Cursor to row, current column.
6089 if (type
== DeviceType
.XTERM
) {
6094 // VPR - Cursor down. Same as CUD
6095 if (type
== DeviceType
.XTERM
) {
6100 // HVP - Horizontal and vertical position
6104 // TBC - Tabulation clear
6108 // Sets an ANSI or DEC private toggle
6112 if ((type
== DeviceType
.VT220
)
6113 || (type
== DeviceType
.XTERM
)) {
6115 // Printer functions
6123 // Sets an ANSI or DEC private toggle
6127 // SGR - Select graphics rendition
6131 // DSR - Device status report
6138 // DECLL - Load leds
6142 // DECSTBM - Set top and bottom margins
6146 // Save cursor (ANSI.SYS)
6147 if (type
== DeviceType
.XTERM
) {
6148 savedState
.cursorX
= currentState
.cursorX
;
6149 savedState
.cursorY
= currentState
.cursorY
;
6153 if (type
== DeviceType
.XTERM
) {
6154 // Window operations
6159 // Restore cursor (ANSI.SYS)
6160 if (type
== DeviceType
.XTERM
) {
6161 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
6168 // DECREQTPARM - Request terminal parameters
6184 // 0x9C goes to GROUND
6189 // 0x3A goes to CSI_IGNORE
6191 scanState
= ScanState
.CSI_IGNORE
;
6196 // 00-17, 19, 1C-1F --> execute
6198 handleControlChar((char) ch
);
6201 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6202 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6204 scanState
= ScanState
.CSI_INTERMEDIATE
;
6207 // 30-39, 3B --> param
6208 if ((ch
>= '0') && (ch
<= '9')) {
6215 // 0x3A goes to CSI_IGNORE
6217 scanState
= ScanState
.CSI_IGNORE
;
6219 // 0x3C-3F goes to CSI_IGNORE
6220 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6221 scanState
= ScanState
.CSI_IGNORE
;
6224 // 40-7E --> dispatch, then switch to GROUND
6225 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6228 // ICH - Insert character
6236 // CUD - Cursor down
6240 // CUF - Cursor forward
6244 // CUB - Cursor backward
6248 // CNL - Cursor down and to column 1
6249 if (type
== DeviceType
.XTERM
) {
6254 // CPL - Cursor up and to column 1
6255 if (type
== DeviceType
.XTERM
) {
6260 // CHA - Cursor to column # in current row
6261 if (type
== DeviceType
.XTERM
) {
6266 // CUP - Cursor position
6270 // CHT - Cursor forward X tab stops (default 1)
6271 if (type
== DeviceType
.XTERM
) {
6276 // ED - Erase in display
6280 // EL - Erase in line
6295 // DCH - Delete character
6302 // Scroll up X lines (default 1)
6303 if (type
== DeviceType
.XTERM
) {
6304 boolean xtermPrivateModeFlag
= false;
6305 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
6306 if (collectBuffer
.charAt(i
) == '?') {
6307 xtermPrivateModeFlag
= true;
6311 if (xtermPrivateModeFlag
) {
6319 // Scroll down X lines (default 1)
6320 if (type
== DeviceType
.XTERM
) {
6329 if ((type
== DeviceType
.VT220
)
6330 || (type
== DeviceType
.XTERM
)) {
6332 // ECH - Erase character
6339 // CBT - Cursor backward X tab stops (default 1)
6340 if (type
== DeviceType
.XTERM
) {
6351 // HPA - Cursor to column # in current row. Same as CHA
6352 if (type
== DeviceType
.XTERM
) {
6357 // HPR - Cursor right. Same as CUF
6358 if (type
== DeviceType
.XTERM
) {
6363 // REP - Repeat last char X times
6364 if (type
== DeviceType
.XTERM
) {
6369 // DA - Device attributes
6373 // VPA - Cursor to row, current column.
6374 if (type
== DeviceType
.XTERM
) {
6379 // VPR - Cursor down. Same as CUD
6380 if (type
== DeviceType
.XTERM
) {
6385 // HVP - Horizontal and vertical position
6389 // TBC - Tabulation clear
6393 // Sets an ANSI or DEC private toggle
6397 if ((type
== DeviceType
.VT220
)
6398 || (type
== DeviceType
.XTERM
)) {
6400 // Printer functions
6408 // Sets an ANSI or DEC private toggle
6412 // SGR - Select graphics rendition
6416 // DSR - Device status report
6423 // DECLL - Load leds
6427 // DECSTBM - Set top and bottom margins
6433 if (type
== DeviceType
.XTERM
) {
6434 // Window operations
6443 // DECREQTPARM - Request terminal parameters
6460 case CSI_INTERMEDIATE
:
6461 // 00-17, 19, 1C-1F --> execute
6463 handleControlChar((char) ch
);
6466 // 20-2F --> collect
6467 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6471 // 0x30-3F goes to CSI_IGNORE
6472 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6473 scanState
= ScanState
.CSI_IGNORE
;
6476 // 40-7E --> dispatch, then switch to GROUND
6477 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6529 if (((type
== DeviceType
.VT220
)
6530 || (type
== DeviceType
.XTERM
))
6531 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6533 // DECSCL - compatibility level
6536 if ((type
== DeviceType
.XTERM
)
6537 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6539 // DECSTR - Soft terminal reset
6544 if (((type
== DeviceType
.VT220
)
6545 || (type
== DeviceType
.XTERM
))
6546 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6574 // 00-17, 19, 1C-1F --> execute
6576 handleControlChar((char) ch
);
6579 // 20-2F --> collect
6580 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6584 // 40-7E --> ignore, then switch to GROUND
6585 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6589 // 20-3F, 7F --> ignore
6595 // 0x9C goes to GROUND
6600 // 0x1B 0x5C goes to GROUND
6605 if ((collectBuffer
.length() > 0)
6606 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6612 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6613 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6615 scanState
= ScanState
.DCS_INTERMEDIATE
;
6618 // 30-39, 3B --> param, then switch to DCS_PARAM
6619 if ((ch
>= '0') && (ch
<= '9')) {
6621 scanState
= ScanState
.DCS_PARAM
;
6625 scanState
= ScanState
.DCS_PARAM
;
6628 // 3C-3F --> collect, then switch to DCS_PARAM
6629 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6631 scanState
= ScanState
.DCS_PARAM
;
6634 // 00-17, 19, 1C-1F, 7F --> ignore
6636 // 0x3A goes to DCS_IGNORE
6638 scanState
= ScanState
.DCS_IGNORE
;
6641 // 0x71 goes to DCS_SIXEL
6643 sixelParseBuffer
= new StringBuilder();
6644 scanState
= ScanState
.DCS_SIXEL
;
6645 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6646 // 0x40-7E goes to DCS_PASSTHROUGH
6647 scanState
= ScanState
.DCS_PASSTHROUGH
;
6651 case DCS_INTERMEDIATE
:
6653 // 0x9C goes to GROUND
6658 // 0x1B 0x5C goes to GROUND
6663 if ((collectBuffer
.length() > 0)
6664 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6670 // 0x30-3F goes to DCS_IGNORE
6671 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6672 scanState
= ScanState
.DCS_IGNORE
;
6675 // 0x40-7E goes to DCS_PASSTHROUGH
6676 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6677 scanState
= ScanState
.DCS_PASSTHROUGH
;
6680 // 00-17, 19, 1C-1F, 7F --> ignore
6685 // 0x9C goes to GROUND
6690 // 0x1B 0x5C goes to GROUND
6695 if ((collectBuffer
.length() > 0)
6696 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6702 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6703 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6705 scanState
= ScanState
.DCS_INTERMEDIATE
;
6708 // 30-39, 3B --> param
6709 if ((ch
>= '0') && (ch
<= '9')) {
6716 // 00-17, 19, 1C-1F, 7F --> ignore
6718 // 0x3A, 3C-3F goes to DCS_IGNORE
6720 scanState
= ScanState
.DCS_IGNORE
;
6722 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6723 scanState
= ScanState
.DCS_IGNORE
;
6726 // 0x71 goes to DCS_SIXEL
6728 sixelParseBuffer
= new StringBuilder();
6729 scanState
= ScanState
.DCS_SIXEL
;
6730 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6731 // 0x40-7E goes to DCS_PASSTHROUGH
6732 scanState
= ScanState
.DCS_PASSTHROUGH
;
6736 case DCS_PASSTHROUGH
:
6737 // 0x9C goes to GROUND
6742 // 0x1B 0x5C goes to GROUND
6747 if ((collectBuffer
.length() > 0)
6748 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6754 // 00-17, 19, 1C-1F, 20-7E --> put
6762 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6765 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6774 // 00-17, 19, 1C-1F, 20-7F --> ignore
6776 // 0x9C goes to GROUND
6784 // 0x9C goes to GROUND
6790 // 0x1B 0x5C goes to GROUND
6795 if ((collectBuffer
.length() > 0)
6796 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6803 // 00-17, 19, 1C-1F, 20-7E --> put
6805 sixelParseBuffer
.append((char) ch
);
6809 sixelParseBuffer
.append((char) ch
);
6812 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6813 sixelParseBuffer
.append((char) ch
);
6816 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6817 sixelParseBuffer
.append((char) ch
);
6825 case SOSPMAPC_STRING
:
6826 // 00-17, 19, 1C-1F, 20-7F --> ignore
6828 // Special case for Jexer: PM can pass one control character
6833 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6837 // 0x9C goes to GROUND
6845 // Special case for Xterm: OSC can pass control characters
6846 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
6850 // 00-17, 19, 1C-1F --> ignore
6852 // 20-7F --> osc_put
6853 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6857 // 0x9C goes to GROUND
6864 case VT52_DIRECT_CURSOR_ADDRESS
:
6865 // This is a special case for the VT52 sequence "ESC Y l c"
6866 if (collectBuffer
.length() == 0) {
6868 } else if (collectBuffer
.length() == 1) {
6869 // We've got the two characters, one in the buffer and the
6871 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
6880 * Expose current cursor X to outside world.
6882 * @return current cursor X
6884 public final int getCursorX() {
6885 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6886 return currentState
.cursorX
* 2;
6888 return currentState
.cursorX
;
6892 * Expose current cursor Y to outside world.
6894 * @return current cursor Y
6896 public final int getCursorY() {
6897 return currentState
.cursorY
;
6901 * Returns true if this terminal has requested the mouse pointer be
6904 * @return true if this terminal has requested the mouse pointer be
6907 public final boolean hasHiddenMousePointer() {
6908 return hideMousePointer
;
6912 * Get the mouse protocol.
6914 * @return MouseProtocol.OFF, MouseProtocol.X10, etc.
6916 public MouseProtocol
getMouseProtocol() {
6917 return mouseProtocol
;
6920 // ------------------------------------------------------------------------
6921 // Sixel support ----------------------------------------------------------
6922 // ------------------------------------------------------------------------
6925 * Set the width of a character cell in pixels.
6927 * @param textWidth the width in pixels of a character cell
6929 public void setTextWidth(final int textWidth
) {
6930 this.textWidth
= textWidth
;
6934 * Set the height of a character cell in pixels.
6936 * @param textHeight the height in pixels of a character cell
6938 public void setTextHeight(final int textHeight
) {
6939 this.textHeight
= textHeight
;
6943 * Parse a sixel string into a bitmap image, and overlay that image onto
6946 private void parseSixel() {
6949 System.err.println("parseSixel(): '" + sixelParseBuffer.toString()
6953 Sixel sixel
= new Sixel(sixelParseBuffer
.toString());
6954 BufferedImage image
= sixel
.getImage();
6956 // System.err.println("parseSixel(): image " + image);
6958 if (image
== null) {
6959 // Sixel data was malformed in some way, bail out.
6966 * Break up the image into text cell sized pieces as a new array of
6969 * Note original column position x0.
6973 * 1. Advance (printCharacter(' ')) for horizontal increment, or
6974 * index (linefeed() + cursorPosition(y, x0)) for vertical
6977 * 2. Set (x, y) cell image data.
6979 * 3. For the right and bottom edges:
6981 * a. Render the text to pixels using Terminus font.
6983 * b. Blit the image on top of the text, using alpha channel.
6985 int cellColumns
= image
.getWidth() / textWidth
;
6986 if (cellColumns
* textWidth
< image
.getWidth()) {
6989 int cellRows
= image
.getHeight() / textHeight
;
6990 if (cellRows
* textHeight
< image
.getHeight()) {
6994 // Break the image up into an array of cells.
6995 Cell
[][] cells
= new Cell
[cellColumns
][cellRows
];
6997 for (int x
= 0; x
< cellColumns
; x
++) {
6998 for (int y
= 0; y
< cellRows
; y
++) {
7000 int width
= textWidth
;
7001 if ((x
+ 1) * textWidth
> image
.getWidth()) {
7002 width
= image
.getWidth() - (x
* textWidth
);
7004 int height
= textHeight
;
7005 if ((y
+ 1) * textHeight
> image
.getHeight()) {
7006 height
= image
.getHeight() - (y
* textHeight
);
7009 Cell cell
= new Cell();
7010 cell
.setImage(image
.getSubimage(x
* textWidth
,
7011 y
* textHeight
, width
, height
));
7017 int x0
= currentState
.cursorX
;
7018 for (int y
= 0; y
< cellRows
; y
++) {
7019 for (int x
= 0; x
< cellColumns
; x
++) {
7020 printCharacter(' ');
7021 cursorLeft(1, false);
7022 if ((x
== cellColumns
- 1) || (y
== cellRows
- 1)) {
7023 // TODO: render text of current cell first, then image
7024 // over it. For now, just copy the cell.
7025 DisplayLine line
= display
.get(currentState
.cursorY
);
7026 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
7028 // Copy the image cell into the display.
7029 DisplayLine line
= display
.get(currentState
.cursorY
);
7030 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
7032 cursorRight(1, false);
7035 cursorPosition(currentState
.cursorY
, x0
);
7041 * Draw the left and right cells of a two-cell-wide (full-width) glyph.
7043 * @param leftX the x position to draw the left half to
7044 * @param leftY the y position to draw the left half to
7045 * @param rightX the x position to draw the right half to
7046 * @param rightY the y position to draw the right half to
7047 * @param ch the character to draw
7049 private void drawHalves(final int leftX
, final int leftY
,
7050 final int rightX
, final int rightY
, final int ch
) {
7052 // System.err.println("drawHalves(): " + Integer.toHexString(ch));
7054 if (lastTextHeight
!= textHeight
) {
7055 glyphMaker
= GlyphMaker
.getInstance(textHeight
);
7056 lastTextHeight
= textHeight
;
7059 Cell cell
= new Cell(ch
, currentState
.attr
);
7060 BufferedImage image
= glyphMaker
.getImage(cell
, textWidth
* 2,
7062 BufferedImage leftImage
= image
.getSubimage(0, 0, textWidth
,
7064 BufferedImage rightImage
= image
.getSubimage(textWidth
, 0, textWidth
,
7067 Cell left
= new Cell(cell
);
7068 left
.setImage(leftImage
);
7069 left
.setWidth(Cell
.Width
.LEFT
);
7070 display
.get(leftY
).replace(leftX
, left
);
7072 Cell right
= new Cell(cell
);
7073 right
.setImage(rightImage
);
7074 right
.setWidth(Cell
.Width
.RIGHT
);
7075 display
.get(rightY
).replace(rightX
, right
);