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;
2877 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
2878 if (collectBuffer
.charAt(i
) == '?') {
2879 decPrivateModeFlag
= true;
2884 for (Integer i
: csiParams
) {
2889 if (decPrivateModeFlag
== true) {
2891 if (value
== true) {
2892 // Use application arrow keys
2893 arrowKeyMode
= ArrowKeyMode
.VT100
;
2895 // Use ANSI arrow keys
2896 arrowKeyMode
= ArrowKeyMode
.ANSI
;
2901 if (decPrivateModeFlag
== true) {
2902 if (value
== false) {
2906 arrowKeyMode
= ArrowKeyMode
.VT52
;
2909 * From the VT102 docs: "You use ANSI mode to select
2910 * most terminal features; the terminal uses the same
2911 * features when it switches to VT52 mode. You
2912 * cannot, however, change most of these features in
2915 * In other words, do not reset any other attributes
2916 * when switching between VT52 submode and ANSI.
2918 * HOWEVER, the real vt100 does switch the character
2919 * set according to Usenet.
2921 currentState
.g0Charset
= CharacterSet
.US
;
2922 currentState
.g1Charset
= CharacterSet
.DRAWING
;
2925 if ((type
== DeviceType
.VT220
)
2926 || (type
== DeviceType
.XTERM
)) {
2928 // VT52 mode is explicitly 7-bit
2930 singleshift
= Singleshift
.NONE
;
2935 if (value
== true) {
2936 // Turn off keyboard
2945 if (decPrivateModeFlag
== true) {
2947 if (value
== true) {
2954 if ((displayListener
!= null)
2955 && (type
== DeviceType
.XTERM
)
2957 // For xterms, reset to the actual width, not 80
2959 width
= displayListener
.getDisplayWidth();
2960 rightMargin
= width
- 1;
2963 width
= rightMargin
+ 1;
2966 // Entire screen is cleared, and scrolling region is
2968 eraseScreen(0, 0, height
- 1, width
- 1, false);
2969 scrollRegionTop
= 0;
2970 scrollRegionBottom
= height
- 1;
2971 // Also home the cursor
2972 cursorPosition(0, 0);
2976 if (decPrivateModeFlag
== true) {
2978 if (value
== true) {
2987 if (value
== true) {
2995 if (decPrivateModeFlag
== true) {
2997 if (value
== true) {
2999 * Set selects reverse screen, a white screen
3000 * background with black characters.
3002 if (reverseVideo
!= true) {
3004 * If in normal video, switch it back
3006 invertDisplayColors();
3008 reverseVideo
= true;
3011 * Reset selects normal screen, a black screen
3012 * background with white characters.
3014 if (reverseVideo
== true) {
3016 * If in reverse video already, switch it back
3018 invertDisplayColors();
3020 reverseVideo
= false;
3025 if (decPrivateModeFlag
== true) {
3027 if (value
== true) {
3028 // Origin is relative to scroll region cursor.
3029 // Cursor can NEVER leave scrolling region.
3030 currentState
.originMode
= true;
3031 cursorPosition(0, 0);
3033 // Origin is absolute to entire screen. Cursor can
3034 // leave the scrolling region via cup() and hvp().
3035 currentState
.originMode
= false;
3036 cursorPosition(0, 0);
3041 if (decPrivateModeFlag
== true) {
3043 if (value
== true) {
3045 currentState
.lineWrap
= true;
3047 // Turn linewrap off
3048 currentState
.lineWrap
= false;
3053 if (decPrivateModeFlag
== true) {
3055 if (value
== true) {
3056 // Keyboard auto-repeat on
3059 // Keyboard auto-repeat off
3065 if (decPrivateModeFlag
== false) {
3067 if (value
== true) {
3077 if (decPrivateModeFlag
== true) {
3083 if (decPrivateModeFlag
== true) {
3089 if (decPrivateModeFlag
== false) {
3091 if (value
== true) {
3093 * Set causes a received linefeed, form feed, or
3094 * vertical tab to move cursor to first column of
3095 * next line. RETURN transmits both a carriage return
3096 * and linefeed. This selection is also called new
3102 * Reset causes a received linefeed, form feed, or
3103 * vertical tab to move cursor to next line in
3104 * current column. RETURN transmits a carriage
3107 newLineMode
= false;
3113 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3114 if (decPrivateModeFlag
== true) {
3116 if (value
== true) {
3118 cursorVisible
= true;
3121 cursorVisible
= false;
3128 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
3129 if (decPrivateModeFlag
== true) {
3131 if (value
== true) {
3132 // Select national mode NRC
3135 // Select multi-national mode
3144 if ((type
== DeviceType
.XTERM
)
3145 && (decPrivateModeFlag
== true)
3147 // Mouse: normal tracking mode
3148 if (value
== true) {
3149 mouseProtocol
= MouseProtocol
.NORMAL
;
3151 mouseProtocol
= MouseProtocol
.OFF
;
3157 if ((type
== DeviceType
.XTERM
)
3158 && (decPrivateModeFlag
== true)
3160 // Mouse: normal tracking mode
3161 if (value
== true) {
3162 mouseProtocol
= MouseProtocol
.BUTTONEVENT
;
3164 mouseProtocol
= MouseProtocol
.OFF
;
3170 if ((type
== DeviceType
.XTERM
)
3171 && (decPrivateModeFlag
== true)
3173 // Mouse: Any-event tracking mode
3174 if (value
== true) {
3175 mouseProtocol
= MouseProtocol
.ANYEVENT
;
3177 mouseProtocol
= MouseProtocol
.OFF
;
3183 if ((type
== DeviceType
.XTERM
)
3184 && (decPrivateModeFlag
== true)
3186 // Mouse: UTF-8 coordinates
3187 if (value
== true) {
3188 mouseEncoding
= MouseEncoding
.UTF8
;
3190 mouseEncoding
= MouseEncoding
.X10
;
3196 if ((type
== DeviceType
.XTERM
)
3197 && (decPrivateModeFlag
== true)
3199 // Mouse: SGR coordinates
3200 if (value
== true) {
3201 mouseEncoding
= MouseEncoding
.SGR
;
3203 mouseEncoding
= MouseEncoding
.X10
;
3216 * DECSC - Save cursor.
3218 private void decsc() {
3219 savedState
.setTo(currentState
);
3223 * DECRC - Restore cursor.
3225 private void decrc() {
3226 currentState
.setTo(savedState
);
3232 private void ind() {
3233 // Move the cursor and scroll if necessary. If at the bottom line
3234 // already, a scroll up is supposed to be performed.
3235 if (currentState
.cursorY
== scrollRegionBottom
) {
3236 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3238 cursorDown(1, true);
3242 * RI - Reverse index.
3245 // Move the cursor and scroll if necessary. If at the top line
3246 // already, a scroll down is supposed to be performed.
3247 if (currentState
.cursorY
== scrollRegionTop
) {
3248 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
, 1);
3256 private void nel() {
3257 // Move the cursor and scroll if necessary. If at the bottom line
3258 // already, a scroll up is supposed to be performed.
3259 if (currentState
.cursorY
== scrollRegionBottom
) {
3260 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
, 1);
3262 cursorDown(1, true);
3264 // Reset to the beginning of the next line
3265 currentState
.cursorX
= 0;
3269 * DECKPAM - Keypad application mode.
3271 private void deckpam() {
3272 keypadMode
= KeypadMode
.Application
;
3276 * DECKPNM - Keypad numeric mode.
3278 private void deckpnm() {
3279 keypadMode
= KeypadMode
.Numeric
;
3285 * @param n number of spaces to move
3286 * @param honorScrollRegion if true, then do nothing if the cursor is
3287 * outside the scrolling region
3289 private void cursorUp(final int n
, final boolean honorScrollRegion
) {
3293 * Special case: if a user moves the cursor from the right margin, we
3294 * have to reset the VT100 right margin flag.
3297 wrapLineFlag
= false;
3300 for (int i
= 0; i
< n
; i
++) {
3301 if (honorScrollRegion
== true) {
3302 // Honor the scrolling region
3303 if ((currentState
.cursorY
< scrollRegionTop
)
3304 || (currentState
.cursorY
> scrollRegionBottom
)
3306 // Outside region, do nothing
3309 // Inside region, go up
3310 top
= scrollRegionTop
;
3312 // Non-scrolling case
3316 if (currentState
.cursorY
> top
) {
3317 currentState
.cursorY
--;
3323 * Move down n spaces.
3325 * @param n number of spaces to move
3326 * @param honorScrollRegion if true, then do nothing if the cursor is
3327 * outside the scrolling region
3329 private void cursorDown(final int n
, final boolean honorScrollRegion
) {
3333 * Special case: if a user moves the cursor from the right margin, we
3334 * have to reset the VT100 right margin flag.
3337 wrapLineFlag
= false;
3340 for (int i
= 0; i
< n
; i
++) {
3342 if (honorScrollRegion
== true) {
3343 // Honor the scrolling region
3344 if (currentState
.cursorY
> scrollRegionBottom
) {
3345 // Outside region, do nothing
3348 // Inside region, go down
3349 bottom
= scrollRegionBottom
;
3351 // Non-scrolling case
3352 bottom
= height
- 1;
3355 if (currentState
.cursorY
< bottom
) {
3356 currentState
.cursorY
++;
3362 * Move left n spaces.
3364 * @param n number of spaces to move
3365 * @param honorScrollRegion if true, then do nothing if the cursor is
3366 * outside the scrolling region
3368 private void cursorLeft(final int n
, final boolean honorScrollRegion
) {
3370 * Special case: if a user moves the cursor from the right margin, we
3371 * have to reset the VT100 right margin flag.
3374 wrapLineFlag
= false;
3377 for (int i
= 0; i
< n
; i
++) {
3378 if (honorScrollRegion
== true) {
3379 // Honor the scrolling region
3380 if ((currentState
.cursorY
< scrollRegionTop
)
3381 || (currentState
.cursorY
> scrollRegionBottom
)
3383 // Outside region, do nothing
3388 if (currentState
.cursorX
> 0) {
3389 currentState
.cursorX
--;
3395 * Move right n spaces.
3397 * @param n number of spaces to move
3398 * @param honorScrollRegion if true, then do nothing if the cursor is
3399 * outside the scrolling region
3401 private void cursorRight(final int n
, final boolean honorScrollRegion
) {
3402 int rightMargin
= this.rightMargin
;
3405 * Special case: if a user moves the cursor from the right margin, we
3406 * have to reset the VT100 right margin flag.
3409 wrapLineFlag
= false;
3412 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3413 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3416 for (int i
= 0; i
< n
; i
++) {
3417 if (honorScrollRegion
== true) {
3418 // Honor the scrolling region
3419 if ((currentState
.cursorY
< scrollRegionTop
)
3420 || (currentState
.cursorY
> scrollRegionBottom
)
3422 // Outside region, do nothing
3427 if (currentState
.cursorX
< rightMargin
) {
3428 currentState
.cursorX
++;
3434 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3436 * @param row row to move to
3437 * @param col column to move to
3439 private void cursorPosition(int row
, final int col
) {
3440 int rightMargin
= this.rightMargin
;
3445 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
3446 rightMargin
= ((rightMargin
+ 1) / 2) - 1;
3449 // Set column number
3450 currentState
.cursorX
= col
;
3452 // Sanity check, bring column back to margin.
3453 if (currentState
.cursorX
> rightMargin
) {
3454 currentState
.cursorX
= rightMargin
;
3458 if (currentState
.originMode
== true) {
3459 row
+= scrollRegionTop
;
3461 if (currentState
.cursorY
< row
) {
3462 cursorDown(row
- currentState
.cursorY
, false);
3463 } else if (currentState
.cursorY
> row
) {
3464 cursorUp(currentState
.cursorY
- row
, false);
3467 wrapLineFlag
= false;
3471 * HTS - Horizontal tabulation set.
3473 private void hts() {
3474 for (Integer stop
: tabStops
) {
3475 if (stop
== currentState
.cursorX
) {
3476 // Already have a tab stop here
3481 // Append a tab stop to the end of the array and resort them
3482 tabStops
.add(currentState
.cursorX
);
3483 Collections
.sort(tabStops
);
3487 * DECSWL - Single-width line.
3489 private void decswl() {
3490 display
.get(currentState
.cursorY
).setDoubleWidth(false);
3491 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3495 * DECDWL - Double-width line.
3497 private void decdwl() {
3498 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3499 display
.get(currentState
.cursorY
).setDoubleHeight(0);
3503 * DECHDL - Double-height + double-width line.
3505 * @param topHalf if true, this sets the row to be the top half row of a
3508 private void dechdl(final boolean topHalf
) {
3509 display
.get(currentState
.cursorY
).setDoubleWidth(true);
3510 if (topHalf
== true) {
3511 display
.get(currentState
.cursorY
).setDoubleHeight(1);
3513 display
.get(currentState
.cursorY
).setDoubleHeight(2);
3518 * DECALN - Screen alignment display.
3520 private void decaln() {
3521 Cell newCell
= new Cell('E');
3522 for (DisplayLine line
: display
) {
3523 for (int i
= 0; i
< line
.length(); i
++) {
3524 line
.replace(i
, newCell
);
3530 * DECSCL - Compatibility level.
3532 private void decscl() {
3533 int i
= getCsiParam(0, 0);
3534 int j
= getCsiParam(1, 0);
3538 currentState
.g0Charset
= CharacterSet
.US
;
3539 currentState
.g1Charset
= CharacterSet
.DRAWING
;
3541 } else if (i
== 62) {
3543 if ((j
== 0) || (j
== 2)) {
3545 } else if (j
== 1) {
3552 * CUD - Cursor down.
3554 private void cud() {
3555 cursorDown(getCsiParam(0, 1, 1, height
), true);
3559 * CUF - Cursor forward.
3561 private void cuf() {
3562 cursorRight(getCsiParam(0, 1, 1, rightMargin
+ 1), true);
3566 * CUB - Cursor backward.
3568 private void cub() {
3569 cursorLeft(getCsiParam(0, 1, 1, currentState
.cursorX
+ 1), true);
3575 private void cuu() {
3576 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3580 * CUP - Cursor position.
3582 private void cup() {
3583 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3584 getCsiParam(1, 1, 1, rightMargin
+ 1) - 1);
3588 * CNL - Cursor down and to column 1.
3590 private void cnl() {
3591 cursorDown(getCsiParam(0, 1, 1, height
), true);
3593 cursorLeft(currentState
.cursorX
, true);
3597 * CPL - Cursor up and to column 1.
3599 private void cpl() {
3600 cursorUp(getCsiParam(0, 1, 1, currentState
.cursorY
+ 1), true);
3602 cursorLeft(currentState
.cursorX
, true);
3606 * CHA - Cursor to column # in current row.
3608 private void cha() {
3609 cursorPosition(currentState
.cursorY
,
3610 getCsiParam(0, 1, 1, rightMargin
+ 1) - 1);
3614 * VPA - Cursor to row #, same column.
3616 private void vpa() {
3617 cursorPosition(getCsiParam(0, 1, 1, height
) - 1,
3618 currentState
.cursorX
);
3622 * ED - Erase in display.
3625 boolean honorProtected
= false;
3626 boolean decPrivateModeFlag
= false;
3628 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3629 if (collectBuffer
.charAt(i
) == '?') {
3630 decPrivateModeFlag
= true;
3635 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3636 && (decPrivateModeFlag
== true)
3638 honorProtected
= true;
3641 int i
= getCsiParam(0, 0);
3644 // Erase from here to end of screen
3645 if (currentState
.cursorY
< height
- 1) {
3646 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1, width
- 1,
3649 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3650 } else if (i
== 1) {
3651 // Erase from beginning of screen to here
3652 eraseScreen(0, 0, currentState
.cursorY
- 1, width
- 1,
3654 eraseLine(0, currentState
.cursorX
, honorProtected
);
3655 } else if (i
== 2) {
3656 // Erase entire screen
3657 eraseScreen(0, 0, height
- 1, width
- 1, honorProtected
);
3662 * EL - Erase in line.
3665 boolean honorProtected
= false;
3666 boolean decPrivateModeFlag
= false;
3668 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
3669 if (collectBuffer
.charAt(i
) == '?') {
3670 decPrivateModeFlag
= true;
3675 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
3676 && (decPrivateModeFlag
== true)
3678 honorProtected
= true;
3681 int i
= getCsiParam(0, 0);
3684 // Erase from here to end of line
3685 eraseLine(currentState
.cursorX
, width
- 1, honorProtected
);
3686 } else if (i
== 1) {
3687 // Erase from beginning of line to here
3688 eraseLine(0, currentState
.cursorX
, honorProtected
);
3689 } else if (i
== 2) {
3690 // Erase entire line
3691 eraseLine(0, width
- 1, honorProtected
);
3696 * ECH - Erase # of characters in current row.
3698 private void ech() {
3699 int i
= getCsiParam(0, 1, 1, width
);
3701 // Erase from here to i characters
3702 eraseLine(currentState
.cursorX
, currentState
.cursorX
+ i
- 1, false);
3709 int i
= getCsiParam(0, 1);
3711 if ((currentState
.cursorY
>= scrollRegionTop
)
3712 && (currentState
.cursorY
<= scrollRegionBottom
)
3715 // I can get the same effect with a scroll-down
3716 scrollingRegionScrollDown(currentState
.cursorY
,
3717 scrollRegionBottom
, i
);
3722 * DCH - Delete char.
3724 private void dch() {
3725 int n
= getCsiParam(0, 1);
3726 DisplayLine line
= display
.get(currentState
.cursorY
);
3727 Cell blank
= new Cell();
3728 for (int i
= 0; i
< n
; i
++) {
3729 line
.delete(currentState
.cursorX
, blank
);
3734 * ICH - Insert blank char at cursor.
3736 private void ich() {
3737 int n
= getCsiParam(0, 1);
3738 DisplayLine line
= display
.get(currentState
.cursorY
);
3739 Cell blank
= new Cell();
3740 for (int i
= 0; i
< n
; i
++) {
3741 line
.insert(currentState
.cursorX
, blank
);
3749 int i
= getCsiParam(0, 1);
3751 if ((currentState
.cursorY
>= scrollRegionTop
)
3752 && (currentState
.cursorY
<= scrollRegionBottom
)) {
3754 // I can get the same effect with a scroll-down
3755 scrollingRegionScrollUp(currentState
.cursorY
,
3756 scrollRegionBottom
, i
);
3761 * HVP - Horizontal and vertical position.
3763 private void hvp() {
3768 * REP - Repeat character.
3770 private void rep() {
3771 int n
= getCsiParam(0, 1);
3772 for (int i
= 0; i
< n
; i
++) {
3773 printCharacter(repCh
);
3781 scrollingRegionScrollUp(scrollRegionTop
, scrollRegionBottom
,
3782 getCsiParam(0, 1, 1, height
));
3789 scrollingRegionScrollDown(scrollRegionTop
, scrollRegionBottom
,
3790 getCsiParam(0, 1, 1, height
));
3794 * CBT - Go back X tab stops.
3796 private void cbt() {
3797 int tabsToMove
= getCsiParam(0, 1);
3800 for (int i
= 0; i
< tabsToMove
; i
++) {
3801 int j
= currentState
.cursorX
;
3802 for (tabI
= 0; tabI
< tabStops
.size(); tabI
++) {
3803 if (tabStops
.get(tabI
) >= currentState
.cursorX
) {
3811 j
= tabStops
.get(tabI
);
3813 cursorPosition(currentState
.cursorY
, j
);
3818 * CHT - Advance X tab stops.
3820 private void cht() {
3821 int n
= getCsiParam(0, 1);
3822 for (int i
= 0; i
< n
; i
++) {
3823 advanceToNextTabStop();
3828 * SGR - Select graphics rendition.
3830 private void sgr() {
3832 if (csiParams
.size() == 0) {
3833 currentState
.attr
.reset();
3837 int sgrColorMode
= -1;
3838 boolean idx88Color
= false;
3839 boolean rgbColor
= false;
3843 for (Integer i
: csiParams
) {
3845 if ((sgrColorMode
== 38) || (sgrColorMode
== 48)) {
3847 assert (type
== DeviceType
.XTERM
);
3851 * Indexed color mode, we now have the index number.
3853 if (sgrColorMode
== 38) {
3854 currentState
.attr
.setForeColorRGB(get88Color(i
));
3856 assert (sgrColorMode
== 48);
3857 currentState
.attr
.setBackColorRGB(get88Color(i
));
3866 * RGB color mode, we are collecting tokens.
3870 } else if (rgbGreen
== -1) {
3871 rgbGreen
= i
& 0xFF;
3873 int rgb
= rgbRed
<< 16;
3874 rgb
|= rgbGreen
<< 8;
3877 // System.err.printf("RGB: %08x\n", rgb);
3879 if (sgrColorMode
== 38) {
3880 currentState
.attr
.setForeColorRGB(rgb
);
3882 assert (sgrColorMode
== 48);
3883 currentState
.attr
.setBackColorRGB(rgb
);
3904 * Indexed color mode.
3911 * This is neither indexed nor RGB color. Bail out.
3916 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
3922 currentState
.attr
.reset();
3927 currentState
.attr
.setBold(true);
3932 currentState
.attr
.setUnderline(true);
3937 currentState
.attr
.setBlink(true);
3942 currentState
.attr
.setReverse(true);
3949 if (type
== DeviceType
.XTERM
) {
3959 // Set black foreground
3960 currentState
.attr
.setForeColorRGB(get88Color(8));
3963 // Set red foreground
3964 currentState
.attr
.setForeColorRGB(get88Color(9));
3967 // Set green foreground
3968 currentState
.attr
.setForeColorRGB(get88Color(10));
3971 // Set yellow foreground
3972 currentState
.attr
.setForeColorRGB(get88Color(11));
3975 // Set blue foreground
3976 currentState
.attr
.setForeColorRGB(get88Color(12));
3979 // Set magenta foreground
3980 currentState
.attr
.setForeColorRGB(get88Color(13));
3983 // Set cyan foreground
3984 currentState
.attr
.setForeColorRGB(get88Color(14));
3987 // Set white foreground
3988 currentState
.attr
.setForeColorRGB(get88Color(15));
3992 // Set black background
3993 currentState
.attr
.setBackColorRGB(get88Color(8));
3996 // Set red background
3997 currentState
.attr
.setBackColorRGB(get88Color(9));
4000 // Set green background
4001 currentState
.attr
.setBackColorRGB(get88Color(10));
4004 // Set yellow background
4005 currentState
.attr
.setBackColorRGB(get88Color(11));
4008 // Set blue background
4009 currentState
.attr
.setBackColorRGB(get88Color(12));
4012 // Set magenta background
4013 currentState
.attr
.setBackColorRGB(get88Color(13));
4016 // Set cyan background
4017 currentState
.attr
.setBackColorRGB(get88Color(14));
4020 // Set white background
4021 currentState
.attr
.setBackColorRGB(get88Color(15));
4029 if ((type
== DeviceType
.VT220
)
4030 || (type
== DeviceType
.XTERM
)) {
4036 currentState
.attr
.setBold(false);
4041 currentState
.attr
.setUnderline(false);
4046 currentState
.attr
.setBlink(false);
4051 currentState
.attr
.setReverse(false);
4059 // A true VT100/102/220 does not support color, however everyone
4060 // is used to their terminal emulator supporting color so we will
4061 // unconditionally support color for all DeviceType's.
4066 // Set black foreground
4067 currentState
.attr
.setForeColor(Color
.BLACK
);
4068 currentState
.attr
.setForeColorRGB(-1);
4071 // Set red foreground
4072 currentState
.attr
.setForeColor(Color
.RED
);
4073 currentState
.attr
.setForeColorRGB(-1);
4076 // Set green foreground
4077 currentState
.attr
.setForeColor(Color
.GREEN
);
4078 currentState
.attr
.setForeColorRGB(-1);
4081 // Set yellow foreground
4082 currentState
.attr
.setForeColor(Color
.YELLOW
);
4083 currentState
.attr
.setForeColorRGB(-1);
4086 // Set blue foreground
4087 currentState
.attr
.setForeColor(Color
.BLUE
);
4088 currentState
.attr
.setForeColorRGB(-1);
4091 // Set magenta foreground
4092 currentState
.attr
.setForeColor(Color
.MAGENTA
);
4093 currentState
.attr
.setForeColorRGB(-1);
4096 // Set cyan foreground
4097 currentState
.attr
.setForeColor(Color
.CYAN
);
4098 currentState
.attr
.setForeColorRGB(-1);
4101 // Set white foreground
4102 currentState
.attr
.setForeColor(Color
.WHITE
);
4103 currentState
.attr
.setForeColorRGB(-1);
4106 if (type
== DeviceType
.XTERM
) {
4108 * Xterm supports T.416 / ISO-8613-3 codes to select
4109 * either an indexed color or an RGB value. (It also
4110 * permits these ISO-8613-3 SGR sequences to be separated
4111 * by colons rather than semicolons.)
4113 * We will support only the following:
4115 * 1. Indexed color mode (88- or 256-color modes).
4119 * These cover most of the use cases in the real world.
4121 * HOWEVER, note that this is an awful broken "standard",
4122 * with no way to do it "right". See
4123 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4124 * for a detailed discussion of the current state of RGB
4125 * in various terminals, the point of which is that none
4126 * of them really do the same thing despite all appearing
4130 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4131 * where it is assumed that supporting just the "indexed
4132 * mode" of these sequences (which could align easily
4133 * with existing SGR colors) is assumed to mean full
4134 * support of 24-bit RGB. So it is all or nothing.
4136 * Finally, these sequences break the assumptions of
4137 * standard ECMA-48 style parsers as pointed out at
4138 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4139 * Therefore in order to keep a clean display, we cannot
4140 * parse anything else in this sequence.
4145 // Underscore on, default foreground color
4146 currentState
.attr
.setUnderline(true);
4147 currentState
.attr
.setForeColor(Color
.WHITE
);
4151 // Underscore off, default foreground color
4152 currentState
.attr
.setUnderline(false);
4153 currentState
.attr
.setForeColor(Color
.WHITE
);
4154 currentState
.attr
.setForeColorRGB(-1);
4157 // Set black background
4158 currentState
.attr
.setBackColor(Color
.BLACK
);
4159 currentState
.attr
.setBackColorRGB(-1);
4162 // Set red background
4163 currentState
.attr
.setBackColor(Color
.RED
);
4164 currentState
.attr
.setBackColorRGB(-1);
4167 // Set green background
4168 currentState
.attr
.setBackColor(Color
.GREEN
);
4169 currentState
.attr
.setBackColorRGB(-1);
4172 // Set yellow background
4173 currentState
.attr
.setBackColor(Color
.YELLOW
);
4174 currentState
.attr
.setBackColorRGB(-1);
4177 // Set blue background
4178 currentState
.attr
.setBackColor(Color
.BLUE
);
4179 currentState
.attr
.setBackColorRGB(-1);
4182 // Set magenta background
4183 currentState
.attr
.setBackColor(Color
.MAGENTA
);
4184 currentState
.attr
.setBackColorRGB(-1);
4187 // Set cyan background
4188 currentState
.attr
.setBackColor(Color
.CYAN
);
4189 currentState
.attr
.setBackColorRGB(-1);
4192 // Set white background
4193 currentState
.attr
.setBackColor(Color
.WHITE
);
4194 currentState
.attr
.setBackColorRGB(-1);
4197 if (type
== DeviceType
.XTERM
) {
4199 * Xterm supports T.416 / ISO-8613-3 codes to select
4200 * either an indexed color or an RGB value. (It also
4201 * permits these ISO-8613-3 SGR sequences to be separated
4202 * by colons rather than semicolons.)
4204 * We will support only the following:
4206 * 1. Indexed color mode (88- or 256-color modes).
4210 * These cover most of the use cases in the real world.
4212 * HOWEVER, note that this is an awful broken "standard",
4213 * with no way to do it "right". See
4214 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4215 * for a detailed discussion of the current state of RGB
4216 * in various terminals, the point of which is that none
4217 * of them really do the same thing despite all appearing
4221 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4222 * where it is assumed that supporting just the "indexed
4223 * mode" of these sequences (which could align easily
4224 * with existing SGR colors) is assumed to mean full
4225 * support of 24-bit RGB. So it is all or nothing.
4227 * Finally, these sequences break the assumptions of
4228 * standard ECMA-48 style parsers as pointed out at
4229 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4230 * Therefore in order to keep a clean display, we cannot
4231 * parse anything else in this sequence.
4238 // Default background
4239 currentState
.attr
.setBackColor(Color
.BLACK
);
4240 currentState
.attr
.setBackColorRGB(-1);
4250 * DA - Device attributes.
4253 int extendedFlag
= 0;
4255 if (collectBuffer
.length() > 0) {
4256 String args
= collectBuffer
.substring(1);
4257 if (collectBuffer
.charAt(0) == '>') {
4259 if (collectBuffer
.length() >= 2) {
4260 i
= Integer
.parseInt(args
);
4262 } else if (collectBuffer
.charAt(0) == '=') {
4264 if (collectBuffer
.length() >= 2) {
4265 i
= Integer
.parseInt(args
);
4268 // Unknown code, bail out
4273 if ((i
!= 0) && (i
!= 1)) {
4277 if ((extendedFlag
== 0) && (i
== 0)) {
4278 // Send string directly to remote side
4279 writeRemote(deviceTypeResponse());
4283 if ((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
)) {
4285 if ((extendedFlag
== 1) && (i
== 0)) {
4287 * Request "What type of terminal are you, what is your
4288 * firmware version, and what hardware options do you have
4291 * Respond: "I am a VT220 (identification code of 1), my
4292 * firmware version is _____ (Pv), and I have _____ Po
4293 * options installed."
4299 if (s8c1t
== true) {
4300 writeRemote("\u009b>1;10;0c");
4302 writeRemote("\033[>1;10;0c");
4308 if ((extendedFlag
== 2) && (i
== 0)) {
4311 * Request "What is your unit ID?"
4313 * Respond: "I was manufactured at site 00 and have a unique ID
4317 writeRemote("\033P!|00010203\033\\");
4322 * DECSTBM - Set top and bottom margins.
4324 private void decstbm() {
4325 boolean decPrivateModeFlag
= false;
4327 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4328 if (collectBuffer
.charAt(i
) == '?') {
4329 decPrivateModeFlag
= true;
4333 if (decPrivateModeFlag
) {
4334 // This could be restore DEC private mode values.
4338 int top
= getCsiParam(0, 1, 1, height
) - 1;
4339 int bottom
= getCsiParam(1, height
, 1, height
) - 1;
4344 scrollRegionTop
= top
;
4345 scrollRegionBottom
= bottom
;
4348 cursorPosition(0, 0);
4353 * DECREQTPARM - Request terminal parameters.
4355 private void decreqtparm() {
4356 int i
= getCsiParam(0, 0);
4358 if ((i
!= 0) && (i
!= 1)) {
4365 * Request terminal parameters.
4369 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4370 * (CLoCk MULtiplier = 1, STP option flags = 0)
4374 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4377 str
= String
.format("\u009b%d;1;1;128;128;1;0x", i
+ 2);
4379 str
= String
.format("\033[%d;1;1;128;128;1;0x", i
+ 2);
4385 * DECSCA - Select Character Attributes.
4387 private void decsca() {
4388 int i
= getCsiParam(0, 0);
4390 if ((i
== 0) || (i
== 2)) {
4392 currentState
.attr
.setProtect(false);
4396 currentState
.attr
.setProtect(true);
4401 * DECSTR - Soft Terminal Reset.
4403 private void decstr() {
4404 // Do exactly like RIS - Reset to initial state
4406 // Do I clear screen too? I think so...
4407 eraseScreen(0, 0, height
- 1, width
- 1, false);
4408 cursorPosition(0, 0);
4412 * DSR - Device status report.
4414 private void dsr() {
4415 boolean decPrivateModeFlag
= false;
4416 int row
= currentState
.cursorY
;
4418 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4419 if (collectBuffer
.charAt(i
) == '?') {
4420 decPrivateModeFlag
= true;
4425 int i
= getCsiParam(0, 0);
4430 // Request status report. Respond with "OK, no malfunction."
4432 // Send string directly to remote side
4433 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4436 writeRemote("\u009b0n");
4438 writeRemote("\033[0n");
4443 // Request cursor position. Respond with current position.
4444 if (currentState
.originMode
== true) {
4445 row
-= scrollRegionTop
;
4448 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4451 str
= String
.format("\u009b%d;%dR", row
+ 1,
4452 currentState
.cursorX
+ 1);
4454 str
= String
.format("\033[%d;%dR", row
+ 1,
4455 currentState
.cursorX
+ 1);
4458 // Send string directly to remote side
4463 if (decPrivateModeFlag
== true) {
4465 // Request printer status report. Respond with "Printer not
4468 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4469 && (s8c1t
== true)) {
4470 writeRemote("\u009b?13n");
4472 writeRemote("\033[?13n");
4478 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4479 && (decPrivateModeFlag
== true)
4482 // Request user-defined keys are locked or unlocked. Respond
4483 // with "User-defined keys are locked."
4485 if (s8c1t
== true) {
4486 writeRemote("\u009b?21n");
4488 writeRemote("\033[?21n");
4494 if (((type
== DeviceType
.VT220
) || (type
== DeviceType
.XTERM
))
4495 && (decPrivateModeFlag
== true)
4498 // Request keyboard language. Respond with "Keyboard
4499 // language is North American."
4501 if (s8c1t
== true) {
4502 writeRemote("\u009b?27;1n");
4504 writeRemote("\033[?27;1n");
4511 // Some other option, ignore
4517 * TBC - Tabulation clear.
4519 private void tbc() {
4520 int i
= getCsiParam(0, 0);
4522 List
<Integer
> newStops
= new ArrayList
<Integer
>();
4523 for (Integer stop
: tabStops
) {
4524 if (stop
== currentState
.cursorX
) {
4529 tabStops
= newStops
;
4537 * Erase the characters in the current line from the start column to the
4538 * end column, inclusive.
4540 * @param start starting column to erase (between 0 and width - 1)
4541 * @param end ending column to erase (between 0 and width - 1)
4542 * @param honorProtected if true, do not erase characters with the
4543 * protected attribute set
4545 private void eraseLine(int start
, int end
, final boolean honorProtected
) {
4550 if (end
> width
- 1) {
4557 for (int i
= start
; i
<= end
; i
++) {
4558 DisplayLine line
= display
.get(currentState
.cursorY
);
4559 if ((!honorProtected
)
4560 || ((honorProtected
) && (!line
.charAt(i
).isProtect()))) {
4567 * From the VT102 manual:
4569 * Erasing a character also erases any character
4570 * attribute of the character.
4576 * Erase with the current color a.k.a. back-color erase
4579 line
.setChar(i
, ' ');
4580 line
.setAttr(i
, currentState
.attr
);
4588 * Erase a rectangular section of the screen, inclusive. end column,
4591 * @param startRow starting row to erase (between 0 and height - 1)
4592 * @param startCol starting column to erase (between 0 and width - 1)
4593 * @param endRow ending row to erase (between 0 and height - 1)
4594 * @param endCol ending column to erase (between 0 and width - 1)
4595 * @param honorProtected if true, do not erase characters with the
4596 * protected attribute set
4598 private void eraseScreen(final int startRow
, final int startCol
,
4599 final int endRow
, final int endCol
, final boolean honorProtected
) {
4607 || (endRow
< startRow
)
4608 || (endCol
< startCol
)
4613 oldCursorY
= currentState
.cursorY
;
4614 for (int i
= startRow
; i
<= endRow
; i
++) {
4615 currentState
.cursorY
= i
;
4616 eraseLine(startCol
, endCol
, honorProtected
);
4618 // Erase display clears the double attributes
4619 display
.get(i
).setDoubleWidth(false);
4620 display
.get(i
).setDoubleHeight(0);
4622 currentState
.cursorY
= oldCursorY
;
4626 * VT220 printer functions. All of these are parsed, but won't do
4629 private void printerFunctions() {
4630 boolean decPrivateModeFlag
= false;
4631 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4632 if (collectBuffer
.charAt(i
) == '?') {
4633 decPrivateModeFlag
= true;
4638 int i
= getCsiParam(0, 0);
4643 if (decPrivateModeFlag
== false) {
4649 if (decPrivateModeFlag
== true) {
4650 // Print cursor line
4655 if (decPrivateModeFlag
== true) {
4656 // Auto print mode OFF
4658 // Printer controller OFF
4660 // Characters re-appear on the screen
4661 printerControllerMode
= false;
4666 if (decPrivateModeFlag
== true) {
4670 // Printer controller
4672 // Characters get sucked into oblivion
4673 printerControllerMode
= true;
4684 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4685 * of remote systems will send an XTerm title sequence even if TERM isn't
4688 * @param xtermChar the character received from the remote side
4690 private void oscPut(final char xtermChar
) {
4691 // System.err.println("oscPut: " + xtermChar);
4694 collectBuffer
.append(xtermChar
);
4697 if ((xtermChar
== 0x07)
4698 || (collectBuffer
.toString().endsWith("\033\\"))
4701 if (xtermChar
== 0x07) {
4702 args
= collectBuffer
.substring(0, collectBuffer
.length() - 1);
4704 args
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4707 String
[] p
= args
.split(";");
4709 if ((p
[0].equals("0")) || (p
[0].equals("2"))) {
4716 if (p
[0].equals("4")) {
4717 for (int i
= 1; i
+ 1 < p
.length
; i
+= 2) {
4718 // Set a color index value
4720 set88Color(Integer
.parseInt(p
[i
]), p
[i
+ 1]);
4721 } catch (NumberFormatException e
) {
4728 // Go to SCAN_GROUND state
4735 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
4736 * Jexer ECMA48Terminal to talk to ECMA48.
4738 * @param pmChar the character received from the remote side
4740 private void pmPut(final char pmChar
) {
4741 // System.err.println("pmPut: " + pmChar);
4744 collectBuffer
.append(pmChar
);
4747 if (collectBuffer
.toString().endsWith("\033\\")) {
4749 arg
= collectBuffer
.substring(0, collectBuffer
.length() - 2);
4751 // System.err.println("arg: '" + arg + "'");
4753 if (arg
.equals("hideMousePointer")) {
4754 hideMousePointer
= true;
4756 if (arg
.equals("showMousePointer")) {
4757 hideMousePointer
= false;
4760 // Go to SCAN_GROUND state
4767 * Perform xterm window operations.
4769 private void xtermWindowOps() {
4770 boolean xtermPrivateModeFlag
= false;
4772 for (int i
= 0; i
< collectBuffer
.length(); i
++) {
4773 if (collectBuffer
.charAt(i
) == '?') {
4774 xtermPrivateModeFlag
= true;
4779 int i
= getCsiParam(0, 0);
4781 if (!xtermPrivateModeFlag
) {
4784 // Report xterm text area size in pixels as CSI 4 ; height ;
4786 writeRemote(String
.format("\033[4;%d;%dt", textHeight
* height
,
4787 textWidth
* width
));
4790 // Report character size in pixels as CSI 6 ; height ; width
4792 writeRemote(String
.format("\033[6;%d;%dt", textHeight
,
4796 // Report the text are size in characters as CSI 8 ; height ;
4798 writeRemote(String
.format("\033[8;%d;%dt", height
, width
));
4807 * Run this input character through the ECMA48 state machine.
4809 * @param ch character from the remote side
4811 private void consume(int ch
) {
4814 // System.err.printf("%c STATE = %s\n", ch, scanState);
4816 // Special case for VT10x: 7-bit characters only
4817 if ((type
== DeviceType
.VT100
) || (type
== DeviceType
.VT102
)) {
4821 // Special "anywhere" states
4823 // 18, 1A --> execute, then switch to SCAN_GROUND
4824 if ((ch
== 0x18) || (ch
== 0x1A)) {
4825 // CAN and SUB abort escape sequences
4830 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4834 if ((type
== DeviceType
.XTERM
)
4835 && ((scanState
== ScanState
.OSC_STRING
)
4836 || (scanState
== ScanState
.DCS_SIXEL
)
4837 || (scanState
== ScanState
.SOSPMAPC_STRING
))
4839 // Xterm can pass ESCAPE to its OSC sequence.
4840 // Xterm can pass ESCAPE to its DCS sequence.
4841 // Jexer can pass ESCAPE to its PM sequence.
4842 } else if ((scanState
!= ScanState
.DCS_ENTRY
)
4843 && (scanState
!= ScanState
.DCS_INTERMEDIATE
)
4844 && (scanState
!= ScanState
.DCS_IGNORE
)
4845 && (scanState
!= ScanState
.DCS_PARAM
)
4846 && (scanState
!= ScanState
.DCS_PASSTHROUGH
)
4848 scanState
= ScanState
.ESCAPE
;
4853 // 0x9B == CSI 8-bit sequence
4855 scanState
= ScanState
.CSI_ENTRY
;
4859 // 0x9D goes to ScanState.OSC_STRING
4861 scanState
= ScanState
.OSC_STRING
;
4865 // 0x90 goes to DCS_ENTRY
4867 scanState
= ScanState
.DCS_ENTRY
;
4871 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4872 if ((ch
== 0x98) || (ch
== 0x9E) || (ch
== 0x9F)) {
4873 scanState
= ScanState
.SOSPMAPC_STRING
;
4877 // 0x7F (DEL) is always discarded
4882 switch (scanState
) {
4885 // 00-17, 19, 1C-1F --> execute
4886 // 80-8F, 91-9A, 9C --> execute
4887 if ((ch
<= 0x1F) || ((ch
>= 0x80) && (ch
<= 0x9F))) {
4888 handleControlChar((char) ch
);
4892 if (((ch
>= 0x20) && (ch
<= 0x7F))
4896 // VT220 printer --> trash bin
4897 if (((type
== DeviceType
.VT220
)
4898 || (type
== DeviceType
.XTERM
))
4899 && (printerControllerMode
== true)
4904 // Hang onto this character
4905 repCh
= mapCharacter(ch
);
4907 // Print this character
4908 printCharacter(repCh
);
4913 // 00-17, 19, 1C-1F --> execute
4915 handleControlChar((char) ch
);
4919 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4920 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
4922 scanState
= ScanState
.ESCAPE_INTERMEDIATE
;
4926 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4927 if ((ch
>= 0x30) && (ch
<= 0x4F)) {
4938 // DECSC - Save cursor
4939 // Note this code overlaps both ANSI and VT52 mode
4944 // DECRC - Restore cursor
4945 // Note this code overlaps both ANSI and VT52 mode
4954 if (vt52Mode
== true) {
4955 // DECANM - Enter ANSI mode
4957 arrowKeyMode
= ArrowKeyMode
.VT100
;
4960 * From the VT102 docs: "You use ANSI mode to select
4961 * most terminal features; the terminal uses the same
4962 * features when it switches to VT52 mode. You
4963 * cannot, however, change most of these features in
4966 * In other words, do not reset any other attributes
4967 * when switching between VT52 submode and ANSI.
4971 currentState
.g0Charset
= CharacterSet
.US
;
4972 currentState
.g1Charset
= CharacterSet
.DRAWING
;
4974 singleshift
= Singleshift
.NONE
;
4975 currentState
.glLockshift
= LockshiftMode
.NONE
;
4976 currentState
.grLockshift
= LockshiftMode
.NONE
;
4980 // DECKPAM - Keypad application mode
4981 // Note this code overlaps both ANSI and VT52 mode
4985 // DECKPNM - Keypad numeric mode
4986 // Note this code overlaps both ANSI and VT52 mode
4993 if (vt52Mode
== true) {
4994 // Cursor up, and stop at the top without scrolling
4999 if (vt52Mode
== true) {
5000 // Cursor down, and stop at the bottom without scrolling
5001 cursorDown(1, false);
5005 if (vt52Mode
== true) {
5006 // Cursor right, and stop at the right without scrolling
5007 cursorRight(1, false);
5011 if (vt52Mode
== true) {
5012 // Cursor left, and stop at the left without scrolling
5013 cursorLeft(1, false);
5020 if (vt52Mode
== true) {
5028 if (vt52Mode
== true) {
5029 // G0 --> Special graphics
5030 currentState
.g0Charset
= CharacterSet
.VT52_GRAPHICS
;
5034 if (vt52Mode
== true) {
5036 currentState
.g0Charset
= CharacterSet
.US
;
5040 if (vt52Mode
== true) {
5042 cursorPosition(0, 0);
5044 // HTS - Horizontal tabulation set
5049 if (vt52Mode
== true) {
5050 // Reverse line feed. Same as RI.
5055 if (vt52Mode
== true) {
5056 // Erase to end of screen
5057 eraseLine(currentState
.cursorX
, width
- 1, false);
5058 eraseScreen(currentState
.cursorY
+ 1, 0, height
- 1,
5063 if (vt52Mode
== true) {
5064 // Erase to end of line
5065 eraseLine(currentState
.cursorX
, width
- 1, false);
5071 if (vt52Mode
== true) {
5074 // RI - Reverse index
5079 if (vt52Mode
== false) {
5081 singleshift
= Singleshift
.SS2
;
5085 if (vt52Mode
== false) {
5087 singleshift
= Singleshift
.SS3
;
5094 if ((ch
>= 0x51) && (ch
<= 0x57)) {
5110 if (vt52Mode
== true) {
5111 scanState
= ScanState
.VT52_DIRECT_CURSOR_ADDRESS
;
5119 if (vt52Mode
== true) {
5121 // Send string directly to remote side
5122 writeRemote("\033/Z");
5125 // Send string directly to remote side
5126 writeRemote(deviceTypeResponse());
5137 // VT52 cannot get to any of these other states
5138 if (vt52Mode
== true) {
5143 if ((ch
>= 0x60) && (ch
<= 0x7E)) {
5150 // RIS - Reset to initial state
5152 // Do I clear screen too? I think so...
5153 eraseScreen(0, 0, height
- 1, width
- 1, false);
5154 cursorPosition(0, 0);
5168 if ((type
== DeviceType
.VT220
)
5169 || (type
== DeviceType
.XTERM
)) {
5171 // VT220 lockshift G2 into GL
5172 currentState
.glLockshift
= LockshiftMode
.G2_GL
;
5177 if ((type
== DeviceType
.VT220
)
5178 || (type
== DeviceType
.XTERM
)) {
5180 // VT220 lockshift G3 into GL
5181 currentState
.glLockshift
= LockshiftMode
.G3_GL
;
5199 if ((type
== DeviceType
.VT220
)
5200 || (type
== DeviceType
.XTERM
)) {
5202 // VT220 lockshift G3 into GR
5203 currentState
.grLockshift
= LockshiftMode
.G3_GR
;
5208 if ((type
== DeviceType
.VT220
)
5209 || (type
== DeviceType
.XTERM
)) {
5211 // VT220 lockshift G2 into GR
5212 currentState
.grLockshift
= LockshiftMode
.G2_GR
;
5218 if ((type
== DeviceType
.VT220
)
5219 || (type
== DeviceType
.XTERM
)) {
5221 // VT220 lockshift G1 into GR
5222 currentState
.grLockshift
= LockshiftMode
.G1_GR
;
5232 // 0x5B goes to CSI_ENTRY
5234 scanState
= ScanState
.CSI_ENTRY
;
5237 // 0x5D goes to OSC_STRING
5239 scanState
= ScanState
.OSC_STRING
;
5242 // 0x50 goes to DCS_ENTRY
5244 scanState
= ScanState
.DCS_ENTRY
;
5247 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5248 if ((ch
== 0x58) || (ch
== 0x5E) || (ch
== 0x5F)) {
5249 scanState
= ScanState
.SOSPMAPC_STRING
;
5254 case ESCAPE_INTERMEDIATE
:
5255 // 00-17, 19, 1C-1F --> execute
5257 handleControlChar((char) ch
);
5260 // 20-2F --> collect
5261 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5265 // 30-7E --> dispatch, then switch to GROUND
5266 if ((ch
>= 0x30) && (ch
<= 0x7E)) {
5269 if ((collectBuffer
.length() == 1)
5270 && (collectBuffer
.charAt(0) == '(')) {
5271 // G0 --> Special graphics
5272 currentState
.g0Charset
= CharacterSet
.DRAWING
;
5274 if ((collectBuffer
.length() == 1)
5275 && (collectBuffer
.charAt(0) == ')')) {
5276 // G1 --> Special graphics
5277 currentState
.g1Charset
= CharacterSet
.DRAWING
;
5279 if ((type
== DeviceType
.VT220
)
5280 || (type
== DeviceType
.XTERM
)) {
5282 if ((collectBuffer
.length() == 1)
5283 && (collectBuffer
.charAt(0) == '*')) {
5284 // G2 --> Special graphics
5285 currentState
.g2Charset
= CharacterSet
.DRAWING
;
5287 if ((collectBuffer
.length() == 1)
5288 && (collectBuffer
.charAt(0) == '+')) {
5289 // G3 --> Special graphics
5290 currentState
.g3Charset
= CharacterSet
.DRAWING
;
5295 if ((collectBuffer
.length() == 1)
5296 && (collectBuffer
.charAt(0) == '(')) {
5297 // G0 --> Alternate character ROM standard character set
5298 currentState
.g0Charset
= CharacterSet
.ROM
;
5300 if ((collectBuffer
.length() == 1)
5301 && (collectBuffer
.charAt(0) == ')')) {
5302 // G1 --> Alternate character ROM standard character set
5303 currentState
.g1Charset
= CharacterSet
.ROM
;
5307 if ((collectBuffer
.length() == 1)
5308 && (collectBuffer
.charAt(0) == '(')) {
5309 // G0 --> Alternate character ROM special graphics
5310 currentState
.g0Charset
= CharacterSet
.ROM_SPECIAL
;
5312 if ((collectBuffer
.length() == 1)
5313 && (collectBuffer
.charAt(0) == ')')) {
5314 // G1 --> Alternate character ROM special graphics
5315 currentState
.g1Charset
= CharacterSet
.ROM_SPECIAL
;
5319 if ((collectBuffer
.length() == 1)
5320 && (collectBuffer
.charAt(0) == '#')) {
5321 // DECDHL - Double-height line (top half)
5326 if ((collectBuffer
.length() == 1)
5327 && (collectBuffer
.charAt(0) == '#')) {
5328 // DECDHL - Double-height line (bottom half)
5331 if ((type
== DeviceType
.VT220
)
5332 || (type
== DeviceType
.XTERM
)) {
5334 if ((collectBuffer
.length() == 1)
5335 && (collectBuffer
.charAt(0) == '(')) {
5337 currentState
.g0Charset
= CharacterSet
.NRC_DUTCH
;
5339 if ((collectBuffer
.length() == 1)
5340 && (collectBuffer
.charAt(0) == ')')) {
5342 currentState
.g1Charset
= CharacterSet
.NRC_DUTCH
;
5344 if ((collectBuffer
.length() == 1)
5345 && (collectBuffer
.charAt(0) == '*')) {
5347 currentState
.g2Charset
= CharacterSet
.NRC_DUTCH
;
5349 if ((collectBuffer
.length() == 1)
5350 && (collectBuffer
.charAt(0) == '+')) {
5352 currentState
.g3Charset
= CharacterSet
.NRC_DUTCH
;
5357 if ((collectBuffer
.length() == 1)
5358 && (collectBuffer
.charAt(0) == '#')) {
5359 // DECSWL - Single-width line
5362 if ((type
== DeviceType
.VT220
)
5363 || (type
== DeviceType
.XTERM
)) {
5365 if ((collectBuffer
.length() == 1)
5366 && (collectBuffer
.charAt(0) == '(')) {
5368 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5370 if ((collectBuffer
.length() == 1)
5371 && (collectBuffer
.charAt(0) == ')')) {
5373 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5375 if ((collectBuffer
.length() == 1)
5376 && (collectBuffer
.charAt(0) == '*')) {
5378 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5380 if ((collectBuffer
.length() == 1)
5381 && (collectBuffer
.charAt(0) == '+')) {
5383 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5388 if ((collectBuffer
.length() == 1)
5389 && (collectBuffer
.charAt(0) == '#')) {
5390 // DECDWL - Double-width line
5393 if ((type
== DeviceType
.VT220
)
5394 || (type
== DeviceType
.XTERM
)) {
5396 if ((collectBuffer
.length() == 1)
5397 && (collectBuffer
.charAt(0) == '(')) {
5399 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5401 if ((collectBuffer
.length() == 1)
5402 && (collectBuffer
.charAt(0) == ')')) {
5404 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5406 if ((collectBuffer
.length() == 1)
5407 && (collectBuffer
.charAt(0) == '*')) {
5409 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5411 if ((collectBuffer
.length() == 1)
5412 && (collectBuffer
.charAt(0) == '+')) {
5414 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5419 if ((type
== DeviceType
.VT220
)
5420 || (type
== DeviceType
.XTERM
)) {
5422 if ((collectBuffer
.length() == 1)
5423 && (collectBuffer
.charAt(0) == '(')) {
5425 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5427 if ((collectBuffer
.length() == 1)
5428 && (collectBuffer
.charAt(0) == ')')) {
5430 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5432 if ((collectBuffer
.length() == 1)
5433 && (collectBuffer
.charAt(0) == '*')) {
5435 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5437 if ((collectBuffer
.length() == 1)
5438 && (collectBuffer
.charAt(0) == '+')) {
5440 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5445 if ((collectBuffer
.length() == 1)
5446 && (collectBuffer
.charAt(0) == '#')) {
5447 // DECALN - Screen alignment display
5456 if ((type
== DeviceType
.VT220
)
5457 || (type
== DeviceType
.XTERM
)) {
5459 if ((collectBuffer
.length() == 1)
5460 && (collectBuffer
.charAt(0) == '(')) {
5461 // G0 --> DEC_SUPPLEMENTAL
5462 currentState
.g0Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5464 if ((collectBuffer
.length() == 1)
5465 && (collectBuffer
.charAt(0) == ')')) {
5466 // G1 --> DEC_SUPPLEMENTAL
5467 currentState
.g1Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5469 if ((collectBuffer
.length() == 1)
5470 && (collectBuffer
.charAt(0) == '*')) {
5471 // G2 --> DEC_SUPPLEMENTAL
5472 currentState
.g2Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5474 if ((collectBuffer
.length() == 1)
5475 && (collectBuffer
.charAt(0) == '+')) {
5476 // G3 --> DEC_SUPPLEMENTAL
5477 currentState
.g3Charset
= CharacterSet
.DEC_SUPPLEMENTAL
;
5482 if ((type
== DeviceType
.VT220
)
5483 || (type
== DeviceType
.XTERM
)) {
5485 if ((collectBuffer
.length() == 1)
5486 && (collectBuffer
.charAt(0) == '(')) {
5488 currentState
.g0Charset
= CharacterSet
.NRC_SWISS
;
5490 if ((collectBuffer
.length() == 1)
5491 && (collectBuffer
.charAt(0) == ')')) {
5493 currentState
.g1Charset
= CharacterSet
.NRC_SWISS
;
5495 if ((collectBuffer
.length() == 1)
5496 && (collectBuffer
.charAt(0) == '*')) {
5498 currentState
.g2Charset
= CharacterSet
.NRC_SWISS
;
5500 if ((collectBuffer
.length() == 1)
5501 && (collectBuffer
.charAt(0) == '+')) {
5503 currentState
.g3Charset
= CharacterSet
.NRC_SWISS
;
5512 if ((collectBuffer
.length() == 1)
5513 && (collectBuffer
.charAt(0) == '(')) {
5514 // G0 --> United Kingdom set
5515 currentState
.g0Charset
= CharacterSet
.UK
;
5517 if ((collectBuffer
.length() == 1)
5518 && (collectBuffer
.charAt(0) == ')')) {
5519 // G1 --> United Kingdom set
5520 currentState
.g1Charset
= CharacterSet
.UK
;
5522 if ((type
== DeviceType
.VT220
)
5523 || (type
== DeviceType
.XTERM
)) {
5525 if ((collectBuffer
.length() == 1)
5526 && (collectBuffer
.charAt(0) == '*')) {
5527 // G2 --> United Kingdom set
5528 currentState
.g2Charset
= CharacterSet
.UK
;
5530 if ((collectBuffer
.length() == 1)
5531 && (collectBuffer
.charAt(0) == '+')) {
5532 // G3 --> United Kingdom set
5533 currentState
.g3Charset
= CharacterSet
.UK
;
5538 if ((collectBuffer
.length() == 1)
5539 && (collectBuffer
.charAt(0) == '(')) {
5541 currentState
.g0Charset
= CharacterSet
.US
;
5543 if ((collectBuffer
.length() == 1)
5544 && (collectBuffer
.charAt(0) == ')')) {
5546 currentState
.g1Charset
= CharacterSet
.US
;
5548 if ((type
== DeviceType
.VT220
)
5549 || (type
== DeviceType
.XTERM
)) {
5551 if ((collectBuffer
.length() == 1)
5552 && (collectBuffer
.charAt(0) == '*')) {
5554 currentState
.g2Charset
= CharacterSet
.US
;
5556 if ((collectBuffer
.length() == 1)
5557 && (collectBuffer
.charAt(0) == '+')) {
5559 currentState
.g3Charset
= CharacterSet
.US
;
5564 if ((type
== DeviceType
.VT220
)
5565 || (type
== DeviceType
.XTERM
)) {
5567 if ((collectBuffer
.length() == 1)
5568 && (collectBuffer
.charAt(0) == '(')) {
5570 currentState
.g0Charset
= CharacterSet
.NRC_FINNISH
;
5572 if ((collectBuffer
.length() == 1)
5573 && (collectBuffer
.charAt(0) == ')')) {
5575 currentState
.g1Charset
= CharacterSet
.NRC_FINNISH
;
5577 if ((collectBuffer
.length() == 1)
5578 && (collectBuffer
.charAt(0) == '*')) {
5580 currentState
.g2Charset
= CharacterSet
.NRC_FINNISH
;
5582 if ((collectBuffer
.length() == 1)
5583 && (collectBuffer
.charAt(0) == '+')) {
5585 currentState
.g3Charset
= CharacterSet
.NRC_FINNISH
;
5592 if ((type
== DeviceType
.VT220
)
5593 || (type
== DeviceType
.XTERM
)) {
5595 if ((collectBuffer
.length() == 1)
5596 && (collectBuffer
.charAt(0) == '(')) {
5598 currentState
.g0Charset
= CharacterSet
.NRC_NORWEGIAN
;
5600 if ((collectBuffer
.length() == 1)
5601 && (collectBuffer
.charAt(0) == ')')) {
5603 currentState
.g1Charset
= CharacterSet
.NRC_NORWEGIAN
;
5605 if ((collectBuffer
.length() == 1)
5606 && (collectBuffer
.charAt(0) == '*')) {
5608 currentState
.g2Charset
= CharacterSet
.NRC_NORWEGIAN
;
5610 if ((collectBuffer
.length() == 1)
5611 && (collectBuffer
.charAt(0) == '+')) {
5613 currentState
.g3Charset
= CharacterSet
.NRC_NORWEGIAN
;
5618 if ((type
== DeviceType
.VT220
)
5619 || (type
== DeviceType
.XTERM
)) {
5621 if ((collectBuffer
.length() == 1)
5622 && (collectBuffer
.charAt(0) == ' ')) {
5629 if ((type
== DeviceType
.VT220
)
5630 || (type
== DeviceType
.XTERM
)) {
5632 if ((collectBuffer
.length() == 1)
5633 && (collectBuffer
.charAt(0) == ' ')) {
5640 if ((type
== DeviceType
.VT220
)
5641 || (type
== DeviceType
.XTERM
)) {
5643 if ((collectBuffer
.length() == 1)
5644 && (collectBuffer
.charAt(0) == '(')) {
5646 currentState
.g0Charset
= CharacterSet
.NRC_SWEDISH
;
5648 if ((collectBuffer
.length() == 1)
5649 && (collectBuffer
.charAt(0) == ')')) {
5651 currentState
.g1Charset
= CharacterSet
.NRC_SWEDISH
;
5653 if ((collectBuffer
.length() == 1)
5654 && (collectBuffer
.charAt(0) == '*')) {
5656 currentState
.g2Charset
= CharacterSet
.NRC_SWEDISH
;
5658 if ((collectBuffer
.length() == 1)
5659 && (collectBuffer
.charAt(0) == '+')) {
5661 currentState
.g3Charset
= CharacterSet
.NRC_SWEDISH
;
5669 if ((type
== DeviceType
.VT220
)
5670 || (type
== DeviceType
.XTERM
)) {
5672 if ((collectBuffer
.length() == 1)
5673 && (collectBuffer
.charAt(0) == '(')) {
5675 currentState
.g0Charset
= CharacterSet
.NRC_GERMAN
;
5677 if ((collectBuffer
.length() == 1)
5678 && (collectBuffer
.charAt(0) == ')')) {
5680 currentState
.g1Charset
= CharacterSet
.NRC_GERMAN
;
5682 if ((collectBuffer
.length() == 1)
5683 && (collectBuffer
.charAt(0) == '*')) {
5685 currentState
.g2Charset
= CharacterSet
.NRC_GERMAN
;
5687 if ((collectBuffer
.length() == 1)
5688 && (collectBuffer
.charAt(0) == '+')) {
5690 currentState
.g3Charset
= CharacterSet
.NRC_GERMAN
;
5701 if ((type
== DeviceType
.VT220
)
5702 || (type
== DeviceType
.XTERM
)) {
5704 if ((collectBuffer
.length() == 1)
5705 && (collectBuffer
.charAt(0) == '(')) {
5707 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH_CA
;
5709 if ((collectBuffer
.length() == 1)
5710 && (collectBuffer
.charAt(0) == ')')) {
5712 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH_CA
;
5714 if ((collectBuffer
.length() == 1)
5715 && (collectBuffer
.charAt(0) == '*')) {
5717 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH_CA
;
5719 if ((collectBuffer
.length() == 1)
5720 && (collectBuffer
.charAt(0) == '+')) {
5722 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH_CA
;
5727 if ((type
== DeviceType
.VT220
)
5728 || (type
== DeviceType
.XTERM
)) {
5730 if ((collectBuffer
.length() == 1)
5731 && (collectBuffer
.charAt(0) == '(')) {
5733 currentState
.g0Charset
= CharacterSet
.NRC_FRENCH
;
5735 if ((collectBuffer
.length() == 1)
5736 && (collectBuffer
.charAt(0) == ')')) {
5738 currentState
.g1Charset
= CharacterSet
.NRC_FRENCH
;
5740 if ((collectBuffer
.length() == 1)
5741 && (collectBuffer
.charAt(0) == '*')) {
5743 currentState
.g2Charset
= CharacterSet
.NRC_FRENCH
;
5745 if ((collectBuffer
.length() == 1)
5746 && (collectBuffer
.charAt(0) == '+')) {
5748 currentState
.g3Charset
= CharacterSet
.NRC_FRENCH
;
5760 if ((type
== DeviceType
.VT220
)
5761 || (type
== DeviceType
.XTERM
)) {
5763 if ((collectBuffer
.length() == 1)
5764 && (collectBuffer
.charAt(0) == '(')) {
5766 currentState
.g0Charset
= CharacterSet
.NRC_ITALIAN
;
5768 if ((collectBuffer
.length() == 1)
5769 && (collectBuffer
.charAt(0) == ')')) {
5771 currentState
.g1Charset
= CharacterSet
.NRC_ITALIAN
;
5773 if ((collectBuffer
.length() == 1)
5774 && (collectBuffer
.charAt(0) == '*')) {
5776 currentState
.g2Charset
= CharacterSet
.NRC_ITALIAN
;
5778 if ((collectBuffer
.length() == 1)
5779 && (collectBuffer
.charAt(0) == '+')) {
5781 currentState
.g3Charset
= CharacterSet
.NRC_ITALIAN
;
5786 if ((type
== DeviceType
.VT220
)
5787 || (type
== DeviceType
.XTERM
)) {
5789 if ((collectBuffer
.length() == 1)
5790 && (collectBuffer
.charAt(0) == '(')) {
5792 currentState
.g0Charset
= CharacterSet
.NRC_SPANISH
;
5794 if ((collectBuffer
.length() == 1)
5795 && (collectBuffer
.charAt(0) == ')')) {
5797 currentState
.g1Charset
= CharacterSet
.NRC_SPANISH
;
5799 if ((collectBuffer
.length() == 1)
5800 && (collectBuffer
.charAt(0) == '*')) {
5802 currentState
.g2Charset
= CharacterSet
.NRC_SPANISH
;
5804 if ((collectBuffer
.length() == 1)
5805 && (collectBuffer
.charAt(0) == '+')) {
5807 currentState
.g3Charset
= CharacterSet
.NRC_SPANISH
;
5854 // 0x9C goes to GROUND
5862 // 00-17, 19, 1C-1F --> execute
5864 handleControlChar((char) ch
);
5867 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5868 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
5870 scanState
= ScanState
.CSI_INTERMEDIATE
;
5873 // 30-39, 3B --> param, then switch to CSI_PARAM
5874 if ((ch
>= '0') && (ch
<= '9')) {
5876 scanState
= ScanState
.CSI_PARAM
;
5880 scanState
= ScanState
.CSI_PARAM
;
5883 // 3C-3F --> collect, then switch to CSI_PARAM
5884 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
5886 scanState
= ScanState
.CSI_PARAM
;
5889 // 40-7E --> dispatch, then switch to GROUND
5890 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
5893 // ICH - Insert character
5901 // CUD - Cursor down
5905 // CUF - Cursor forward
5909 // CUB - Cursor backward
5913 // CNL - Cursor down and to column 1
5914 if (type
== DeviceType
.XTERM
) {
5919 // CPL - Cursor up and to column 1
5920 if (type
== DeviceType
.XTERM
) {
5925 // CHA - Cursor to column # in current row
5926 if (type
== DeviceType
.XTERM
) {
5931 // CUP - Cursor position
5935 // CHT - Cursor forward X tab stops (default 1)
5936 if (type
== DeviceType
.XTERM
) {
5941 // ED - Erase in display
5945 // EL - Erase in line
5960 // DCH - Delete character
5967 // Scroll up X lines (default 1)
5968 if (type
== DeviceType
.XTERM
) {
5973 // Scroll down X lines (default 1)
5974 if (type
== DeviceType
.XTERM
) {
5983 if ((type
== DeviceType
.VT220
)
5984 || (type
== DeviceType
.XTERM
)) {
5986 // ECH - Erase character
5993 // CBT - Cursor backward X tab stops (default 1)
5994 if (type
== DeviceType
.XTERM
) {
6005 // HPA - Cursor to column # in current row. Same as CHA
6006 if (type
== DeviceType
.XTERM
) {
6011 // HPR - Cursor right. Same as CUF
6012 if (type
== DeviceType
.XTERM
) {
6017 // REP - Repeat last char X times
6018 if (type
== DeviceType
.XTERM
) {
6023 // DA - Device attributes
6027 // VPA - Cursor to row, current column.
6028 if (type
== DeviceType
.XTERM
) {
6033 // VPR - Cursor down. Same as CUD
6034 if (type
== DeviceType
.XTERM
) {
6039 // HVP - Horizontal and vertical position
6043 // TBC - Tabulation clear
6047 // Sets an ANSI or DEC private toggle
6051 if ((type
== DeviceType
.VT220
)
6052 || (type
== DeviceType
.XTERM
)) {
6054 // Printer functions
6062 // Sets an ANSI or DEC private toggle
6066 // SGR - Select graphics rendition
6070 // DSR - Device status report
6077 // DECLL - Load leds
6081 // DECSTBM - Set top and bottom margins
6085 // Save cursor (ANSI.SYS)
6086 if (type
== DeviceType
.XTERM
) {
6087 savedState
.cursorX
= currentState
.cursorX
;
6088 savedState
.cursorY
= currentState
.cursorY
;
6092 if (type
== DeviceType
.XTERM
) {
6093 // Window operations
6098 // Restore cursor (ANSI.SYS)
6099 if (type
== DeviceType
.XTERM
) {
6100 cursorPosition(savedState
.cursorY
, savedState
.cursorX
);
6107 // DECREQTPARM - Request terminal parameters
6123 // 0x9C goes to GROUND
6128 // 0x3A goes to CSI_IGNORE
6130 scanState
= ScanState
.CSI_IGNORE
;
6135 // 00-17, 19, 1C-1F --> execute
6137 handleControlChar((char) ch
);
6140 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6141 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6143 scanState
= ScanState
.CSI_INTERMEDIATE
;
6146 // 30-39, 3B --> param
6147 if ((ch
>= '0') && (ch
<= '9')) {
6154 // 0x3A goes to CSI_IGNORE
6156 scanState
= ScanState
.CSI_IGNORE
;
6158 // 0x3C-3F goes to CSI_IGNORE
6159 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6160 scanState
= ScanState
.CSI_IGNORE
;
6163 // 40-7E --> dispatch, then switch to GROUND
6164 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6167 // ICH - Insert character
6175 // CUD - Cursor down
6179 // CUF - Cursor forward
6183 // CUB - Cursor backward
6187 // CNL - Cursor down and to column 1
6188 if (type
== DeviceType
.XTERM
) {
6193 // CPL - Cursor up and to column 1
6194 if (type
== DeviceType
.XTERM
) {
6199 // CHA - Cursor to column # in current row
6200 if (type
== DeviceType
.XTERM
) {
6205 // CUP - Cursor position
6209 // CHT - Cursor forward X tab stops (default 1)
6210 if (type
== DeviceType
.XTERM
) {
6215 // ED - Erase in display
6219 // EL - Erase in line
6234 // DCH - Delete character
6241 // Scroll up X lines (default 1)
6242 if (type
== DeviceType
.XTERM
) {
6247 // Scroll down X lines (default 1)
6248 if (type
== DeviceType
.XTERM
) {
6257 if ((type
== DeviceType
.VT220
)
6258 || (type
== DeviceType
.XTERM
)) {
6260 // ECH - Erase character
6267 // CBT - Cursor backward X tab stops (default 1)
6268 if (type
== DeviceType
.XTERM
) {
6279 // HPA - Cursor to column # in current row. Same as CHA
6280 if (type
== DeviceType
.XTERM
) {
6285 // HPR - Cursor right. Same as CUF
6286 if (type
== DeviceType
.XTERM
) {
6291 // REP - Repeat last char X times
6292 if (type
== DeviceType
.XTERM
) {
6297 // DA - Device attributes
6301 // VPA - Cursor to row, current column.
6302 if (type
== DeviceType
.XTERM
) {
6307 // VPR - Cursor down. Same as CUD
6308 if (type
== DeviceType
.XTERM
) {
6313 // HVP - Horizontal and vertical position
6317 // TBC - Tabulation clear
6321 // Sets an ANSI or DEC private toggle
6325 if ((type
== DeviceType
.VT220
)
6326 || (type
== DeviceType
.XTERM
)) {
6328 // Printer functions
6336 // Sets an ANSI or DEC private toggle
6340 // SGR - Select graphics rendition
6344 // DSR - Device status report
6351 // DECLL - Load leds
6355 // DECSTBM - Set top and bottom margins
6361 if (type
== DeviceType
.XTERM
) {
6362 // Window operations
6371 // DECREQTPARM - Request terminal parameters
6388 case CSI_INTERMEDIATE
:
6389 // 00-17, 19, 1C-1F --> execute
6391 handleControlChar((char) ch
);
6394 // 20-2F --> collect
6395 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6399 // 0x30-3F goes to CSI_IGNORE
6400 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6401 scanState
= ScanState
.CSI_IGNORE
;
6404 // 40-7E --> dispatch, then switch to GROUND
6405 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6457 if (((type
== DeviceType
.VT220
)
6458 || (type
== DeviceType
.XTERM
))
6459 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6461 // DECSCL - compatibility level
6464 if ((type
== DeviceType
.XTERM
)
6465 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '!')
6467 // DECSTR - Soft terminal reset
6472 if (((type
== DeviceType
.VT220
)
6473 || (type
== DeviceType
.XTERM
))
6474 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == '\"')
6502 // 00-17, 19, 1C-1F --> execute
6504 handleControlChar((char) ch
);
6507 // 20-2F --> collect
6508 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6512 // 40-7E --> ignore, then switch to GROUND
6513 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6517 // 20-3F, 7F --> ignore
6523 // 0x9C goes to GROUND
6528 // 0x1B 0x5C goes to GROUND
6533 if ((collectBuffer
.length() > 0)
6534 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6540 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6541 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6543 scanState
= ScanState
.DCS_INTERMEDIATE
;
6546 // 30-39, 3B --> param, then switch to DCS_PARAM
6547 if ((ch
>= '0') && (ch
<= '9')) {
6549 scanState
= ScanState
.DCS_PARAM
;
6553 scanState
= ScanState
.DCS_PARAM
;
6556 // 3C-3F --> collect, then switch to DCS_PARAM
6557 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6559 scanState
= ScanState
.DCS_PARAM
;
6562 // 00-17, 19, 1C-1F, 7F --> ignore
6564 // 0x3A goes to DCS_IGNORE
6566 scanState
= ScanState
.DCS_IGNORE
;
6569 // 0x71 goes to DCS_SIXEL
6571 sixelParseBuffer
= new StringBuilder();
6572 scanState
= ScanState
.DCS_SIXEL
;
6573 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6574 // 0x40-7E goes to DCS_PASSTHROUGH
6575 scanState
= ScanState
.DCS_PASSTHROUGH
;
6579 case DCS_INTERMEDIATE
:
6581 // 0x9C goes to GROUND
6586 // 0x1B 0x5C goes to GROUND
6591 if ((collectBuffer
.length() > 0)
6592 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6598 // 0x30-3F goes to DCS_IGNORE
6599 if ((ch
>= 0x30) && (ch
<= 0x3F)) {
6600 scanState
= ScanState
.DCS_IGNORE
;
6603 // 0x40-7E goes to DCS_PASSTHROUGH
6604 if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6605 scanState
= ScanState
.DCS_PASSTHROUGH
;
6608 // 00-17, 19, 1C-1F, 7F --> ignore
6613 // 0x9C goes to GROUND
6618 // 0x1B 0x5C goes to GROUND
6623 if ((collectBuffer
.length() > 0)
6624 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6630 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6631 if ((ch
>= 0x20) && (ch
<= 0x2F)) {
6633 scanState
= ScanState
.DCS_INTERMEDIATE
;
6636 // 30-39, 3B --> param
6637 if ((ch
>= '0') && (ch
<= '9')) {
6644 // 00-17, 19, 1C-1F, 7F --> ignore
6646 // 0x3A, 3C-3F goes to DCS_IGNORE
6648 scanState
= ScanState
.DCS_IGNORE
;
6650 if ((ch
>= 0x3C) && (ch
<= 0x3F)) {
6651 scanState
= ScanState
.DCS_IGNORE
;
6654 // 0x71 goes to DCS_SIXEL
6656 sixelParseBuffer
= new StringBuilder();
6657 scanState
= ScanState
.DCS_SIXEL
;
6658 } else if ((ch
>= 0x40) && (ch
<= 0x7E)) {
6659 // 0x40-7E goes to DCS_PASSTHROUGH
6660 scanState
= ScanState
.DCS_PASSTHROUGH
;
6664 case DCS_PASSTHROUGH
:
6665 // 0x9C goes to GROUND
6670 // 0x1B 0x5C goes to GROUND
6675 if ((collectBuffer
.length() > 0)
6676 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6682 // 00-17, 19, 1C-1F, 20-7E --> put
6690 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6693 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6702 // 00-17, 19, 1C-1F, 20-7F --> ignore
6704 // 0x9C goes to GROUND
6712 // 0x9C goes to GROUND
6718 // 0x1B 0x5C goes to GROUND
6723 if ((collectBuffer
.length() > 0)
6724 && (collectBuffer
.charAt(collectBuffer
.length() - 1) == 0x1B)
6731 // 00-17, 19, 1C-1F, 20-7E --> put
6733 sixelParseBuffer
.append((char) ch
);
6737 sixelParseBuffer
.append((char) ch
);
6740 if ((ch
>= 0x1C) && (ch
<= 0x1F)) {
6741 sixelParseBuffer
.append((char) ch
);
6744 if ((ch
>= 0x20) && (ch
<= 0x7E)) {
6745 sixelParseBuffer
.append((char) ch
);
6753 case SOSPMAPC_STRING
:
6754 // 00-17, 19, 1C-1F, 20-7F --> ignore
6756 // Special case for Jexer: PM can pass one control character
6761 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6765 // 0x9C goes to GROUND
6773 // Special case for Xterm: OSC can pass control characters
6774 if ((ch
== 0x9C) || (ch
== 0x07) || (ch
== 0x1B)) {
6778 // 00-17, 19, 1C-1F --> ignore
6780 // 20-7F --> osc_put
6781 if ((ch
>= 0x20) && (ch
<= 0x7F)) {
6785 // 0x9C goes to GROUND
6792 case VT52_DIRECT_CURSOR_ADDRESS
:
6793 // This is a special case for the VT52 sequence "ESC Y l c"
6794 if (collectBuffer
.length() == 0) {
6796 } else if (collectBuffer
.length() == 1) {
6797 // We've got the two characters, one in the buffer and the
6799 cursorPosition(collectBuffer
.charAt(0) - '\040', ch
- '\040');
6808 * Expose current cursor X to outside world.
6810 * @return current cursor X
6812 public final int getCursorX() {
6813 if (display
.get(currentState
.cursorY
).isDoubleWidth()) {
6814 return currentState
.cursorX
* 2;
6816 return currentState
.cursorX
;
6820 * Expose current cursor Y to outside world.
6822 * @return current cursor Y
6824 public final int getCursorY() {
6825 return currentState
.cursorY
;
6829 * Returns true if this terminal has requested the mouse pointer be
6832 * @return true if this terminal has requested the mouse pointer be
6835 public final boolean hasHiddenMousePointer() {
6836 return hideMousePointer
;
6840 * Get the mouse protocol.
6842 * @return MouseProtocol.OFF, MouseProtocol.X10, etc.
6844 public MouseProtocol
getMouseProtocol() {
6845 return mouseProtocol
;
6848 // ------------------------------------------------------------------------
6849 // Sixel support ----------------------------------------------------------
6850 // ------------------------------------------------------------------------
6853 * Set the width of a character cell in pixels.
6855 * @param textWidth the width in pixels of a character cell
6857 public void setTextWidth(final int textWidth
) {
6858 this.textWidth
= textWidth
;
6862 * Set the height of a character cell in pixels.
6864 * @param textHeight the height in pixels of a character cell
6866 public void setTextHeight(final int textHeight
) {
6867 this.textHeight
= textHeight
;
6871 * Parse a sixel string into a bitmap image, and overlay that image onto
6874 private void parseSixel() {
6877 System.err.println("parseSixel(): '" + sixelParseBuffer.toString()
6881 Sixel sixel
= new Sixel(sixelParseBuffer
.toString());
6882 BufferedImage image
= sixel
.getImage();
6884 // System.err.println("parseSixel(): image " + image);
6886 if (image
== null) {
6887 // Sixel data was malformed in some way, bail out.
6894 * Break up the image into text cell sized pieces as a new array of
6897 * Note original column position x0.
6901 * 1. Advance (printCharacter(' ')) for horizontal increment, or
6902 * index (linefeed() + cursorPosition(y, x0)) for vertical
6905 * 2. Set (x, y) cell image data.
6907 * 3. For the right and bottom edges:
6909 * a. Render the text to pixels using Terminus font.
6911 * b. Blit the image on top of the text, using alpha channel.
6913 int cellColumns
= image
.getWidth() / textWidth
;
6914 if (cellColumns
* textWidth
< image
.getWidth()) {
6917 int cellRows
= image
.getHeight() / textHeight
;
6918 if (cellRows
* textHeight
< image
.getHeight()) {
6922 // Break the image up into an array of cells.
6923 Cell
[][] cells
= new Cell
[cellColumns
][cellRows
];
6925 for (int x
= 0; x
< cellColumns
; x
++) {
6926 for (int y
= 0; y
< cellRows
; y
++) {
6928 int width
= textWidth
;
6929 if ((x
+ 1) * textWidth
> image
.getWidth()) {
6930 width
= image
.getWidth() - (x
* textWidth
);
6932 int height
= textHeight
;
6933 if ((y
+ 1) * textHeight
> image
.getHeight()) {
6934 height
= image
.getHeight() - (y
* textHeight
);
6937 Cell cell
= new Cell();
6938 cell
.setImage(image
.getSubimage(x
* textWidth
,
6939 y
* textHeight
, width
, height
));
6945 int x0
= currentState
.cursorX
;
6946 for (int y
= 0; y
< cellRows
; y
++) {
6947 for (int x
= 0; x
< cellColumns
; x
++) {
6948 printCharacter(' ');
6949 cursorLeft(1, false);
6950 if ((x
== cellColumns
- 1) || (y
== cellRows
- 1)) {
6951 // TODO: render text of current cell first, then image
6952 // over it. For now, just copy the cell.
6953 DisplayLine line
= display
.get(currentState
.cursorY
);
6954 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
6956 // Copy the image cell into the display.
6957 DisplayLine line
= display
.get(currentState
.cursorY
);
6958 line
.replace(currentState
.cursorX
, cells
[x
][y
]);
6960 cursorRight(1, false);
6963 cursorPosition(currentState
.cursorY
, x0
);
6969 * Draw the left and right cells of a two-cell-wide (full-width) glyph.
6971 * @param leftX the x position to draw the left half to
6972 * @param leftY the y position to draw the left half to
6973 * @param rightX the x position to draw the right half to
6974 * @param rightY the y position to draw the right half to
6975 * @param ch the character to draw
6977 private void drawHalves(final int leftX
, final int leftY
,
6978 final int rightX
, final int rightY
, final int ch
) {
6980 // System.err.println("drawHalves(): " + Integer.toHexString(ch));
6982 if (lastTextHeight
!= textHeight
) {
6983 glyphMaker
= GlyphMaker
.getInstance(textHeight
);
6984 lastTextHeight
= textHeight
;
6987 Cell cell
= new Cell(ch
, currentState
.attr
);
6988 BufferedImage image
= glyphMaker
.getImage(cell
, textWidth
* 2,
6990 BufferedImage leftImage
= image
.getSubimage(0, 0, textWidth
,
6992 BufferedImage rightImage
= image
.getSubimage(textWidth
, 0, textWidth
,
6995 Cell left
= new Cell(cell
);
6996 left
.setImage(leftImage
);
6997 left
.setWidth(Cell
.Width
.LEFT
);
6998 display
.get(leftY
).replace(leftX
, left
);
7000 Cell right
= new Cell(cell
);
7001 right
.setImage(rightImage
);
7002 right
.setWidth(Cell
.Width
.RIGHT
);
7003 display
.get(rightY
).replace(rightX
, right
);