Merge branch 'subtree'
[nikiroo-utils.git] / src / jexer / tterminal / ECMA48.java
... / ...
CommitLineData
1/*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer.tterminal;
30
31import java.awt.Graphics;
32import java.awt.image.BufferedImage;
33import java.io.BufferedInputStream;
34import java.io.BufferedOutputStream;
35import java.io.ByteArrayInputStream;
36import java.io.CharArrayWriter;
37import java.io.InputStream;
38import java.io.InputStreamReader;
39import java.io.IOException;
40import java.io.OutputStream;
41import java.io.OutputStreamWriter;
42import java.io.PrintWriter;
43import java.io.Reader;
44import java.io.UnsupportedEncodingException;
45import java.io.Writer;
46import java.util.ArrayList;
47import java.util.Collections;
48import java.util.HashMap;
49import java.util.List;
50import javax.imageio.ImageIO;
51
52import jexer.TKeypress;
53import jexer.backend.GlyphMaker;
54import jexer.bits.Color;
55import jexer.bits.Cell;
56import jexer.bits.CellAttributes;
57import jexer.bits.StringUtils;
58import jexer.event.TInputEvent;
59import jexer.event.TKeypressEvent;
60import jexer.event.TMouseEvent;
61import jexer.io.ReadTimeoutException;
62import jexer.io.TimeoutInputStream;
63import static jexer.TKeypress.*;
64
65/**
66 * This implements a complex ECMA-48/ISO 6429/ANSI X3.64 type console,
67 * including a scrollback buffer.
68 *
69 * <p>
70 * It currently implements VT100, VT102, VT220, and XTERM with the following
71 * caveats:
72 *
73 * <p>
74 * - The vttest scenario for VT220 8-bit controls (11.1.2.3) reports a
75 * failure with XTERM. This is due to vttest failing to decode the UTF-8
76 * stream.
77 *
78 * <p>
79 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
80 * from VT100 are not supported.
81 *
82 * <p>
83 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
84 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
85 * because DECSCL is not supported, it will fail the last part of the
86 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
87 *
88 * <p>
89 * - Numeric/application keys from the number pad are not supported because
90 * they are not exposed from the TKeypress API.
91 *
92 * <p>
93 * - VT52 HOLD SCREEN mode is not supported.
94 *
95 * <p>
96 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
97 * numerators) are not rendered correctly.
98 *
99 * <p>
100 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
101 */
102public class ECMA48 implements Runnable {
103
104 // ------------------------------------------------------------------------
105 // Constants --------------------------------------------------------------
106 // ------------------------------------------------------------------------
107
108 /**
109 * The emulator can emulate several kinds of terminals.
110 */
111 public enum DeviceType {
112 /**
113 * DEC VT100 but also including the three VT102 functions.
114 */
115 VT100,
116
117 /**
118 * DEC VT102.
119 */
120 VT102,
121
122 /**
123 * DEC VT220.
124 */
125 VT220,
126
127 /**
128 * A subset of xterm.
129 */
130 XTERM
131 }
132
133 /**
134 * Parser character scan states.
135 */
136 private enum ScanState {
137 GROUND,
138 ESCAPE,
139 ESCAPE_INTERMEDIATE,
140 CSI_ENTRY,
141 CSI_PARAM,
142 CSI_INTERMEDIATE,
143 CSI_IGNORE,
144 DCS_ENTRY,
145 DCS_INTERMEDIATE,
146 DCS_PARAM,
147 DCS_PASSTHROUGH,
148 DCS_IGNORE,
149 DCS_SIXEL,
150 SOSPMAPC_STRING,
151 OSC_STRING,
152 VT52_DIRECT_CURSOR_ADDRESS
153 }
154
155 /**
156 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
157 * can't really use it in keypress() because we do not see number pad
158 * events from TKeypress.
159 */
160 private enum KeypadMode {
161 Application,
162 Numeric
163 }
164
165 /**
166 * Arrow keys can emit three different sequences (DECCKM or VT52
167 * submode).
168 */
169 private enum ArrowKeyMode {
170 VT52,
171 ANSI,
172 VT100
173 }
174
175 /**
176 * Available character sets for GL, GR, G0, G1, G2, G3.
177 */
178 private enum CharacterSet {
179 US,
180 UK,
181 DRAWING,
182 ROM,
183 ROM_SPECIAL,
184 VT52_GRAPHICS,
185 DEC_SUPPLEMENTAL,
186 NRC_DUTCH,
187 NRC_FINNISH,
188 NRC_FRENCH,
189 NRC_FRENCH_CA,
190 NRC_GERMAN,
191 NRC_ITALIAN,
192 NRC_NORWEGIAN,
193 NRC_SPANISH,
194 NRC_SWEDISH,
195 NRC_SWISS
196 }
197
198 /**
199 * Single-shift states used by the C1 control characters SS2 (0x8E) and
200 * SS3 (0x8F).
201 */
202 private enum Singleshift {
203 NONE,
204 SS2,
205 SS3
206 }
207
208 /**
209 * VT220+ lockshift states.
210 */
211 private enum LockshiftMode {
212 NONE,
213 G1_GR,
214 G2_GR,
215 G2_GL,
216 G3_GR,
217 G3_GL
218 }
219
220 /**
221 * XTERM mouse reporting protocols.
222 */
223 public enum MouseProtocol {
224 OFF,
225 X10,
226 NORMAL,
227 BUTTONEVENT,
228 ANYEVENT
229 }
230
231 /**
232 * XTERM mouse reporting encodings.
233 */
234 private enum MouseEncoding {
235 X10,
236 UTF8,
237 SGR
238 }
239
240 // ------------------------------------------------------------------------
241 // Variables --------------------------------------------------------------
242 // ------------------------------------------------------------------------
243
244 /**
245 * The enclosing listening object.
246 */
247 private DisplayListener displayListener;
248
249 /**
250 * When true, the reader thread is expected to exit.
251 */
252 private volatile boolean stopReaderThread = false;
253
254 /**
255 * The reader thread.
256 */
257 private Thread readerThread = null;
258
259 /**
260 * The type of emulator to be.
261 */
262 private final DeviceType type;
263
264 /**
265 * The scrollback buffer characters + attributes.
266 */
267 private volatile ArrayList<DisplayLine> scrollback;
268
269 /**
270 * The raw display buffer characters + attributes.
271 */
272 private volatile ArrayList<DisplayLine> display;
273
274 /**
275 * The maximum number of lines in the scrollback buffer.
276 */
277 private int scrollbackMax = 10000;
278
279 /**
280 * The terminal's input. For type == XTERM, this is an InputStreamReader
281 * with UTF-8 encoding.
282 */
283 private Reader input;
284
285 /**
286 * The terminal's raw InputStream. This is used for type != XTERM.
287 */
288 private volatile TimeoutInputStream inputStream;
289
290 /**
291 * The terminal's output. For type == XTERM, this wraps an
292 * OutputStreamWriter with UTF-8 encoding.
293 */
294 private Writer output;
295
296 /**
297 * The terminal's raw OutputStream. This is used for type != XTERM.
298 */
299 private OutputStream outputStream;
300
301 /**
302 * Current scanning state.
303 */
304 private ScanState scanState;
305
306 /**
307 * Which mouse protocol is active.
308 */
309 private MouseProtocol mouseProtocol = MouseProtocol.OFF;
310
311 /**
312 * Which mouse encoding is active.
313 */
314 private MouseEncoding mouseEncoding = MouseEncoding.X10;
315
316 /**
317 * A terminal may request that the mouse pointer be hidden using a
318 * Privacy Message containing either "hideMousePointer" or
319 * "showMousePointer". This is currently only used within Jexer by
320 * TTerminalWindow so that only the bottom-most instance of nested
321 * Jexer's draws the mouse within its application window.
322 */
323 private boolean hideMousePointer = false;
324
325 /**
326 * Physical display width. We start at 80x24, but the user can resize us
327 * bigger/smaller.
328 */
329 private int width = 80;
330
331 /**
332 * Physical display height. We start at 80x24, but the user can resize
333 * us bigger/smaller.
334 */
335 private int height = 24;
336
337 /**
338 * Top margin of the scrolling region.
339 */
340 private int scrollRegionTop = 0;
341
342 /**
343 * Bottom margin of the scrolling region.
344 */
345 private int scrollRegionBottom = height - 1;
346
347 /**
348 * Right margin column number. This can be selected by the remote side
349 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
350 */
351 private int rightMargin = 79;
352
353 /**
354 * Last character printed.
355 */
356 private int repCh;
357
358 /**
359 * VT100-style line wrapping: a character is placed in column 80 (or
360 * 132), but the line does NOT wrap until another character is written to
361 * column 1 of the next line, after which the cursor moves to column 2.
362 */
363 private boolean wrapLineFlag = false;
364
365 /**
366 * VT220 single shift flag.
367 */
368 private Singleshift singleshift = Singleshift.NONE;
369
370 /**
371 * true = insert characters, false = overwrite.
372 */
373 private boolean insertMode = false;
374
375 /**
376 * VT52 mode as selected by DECANM. True means VT52, false means
377 * ANSI. Default is ANSI.
378 */
379 private boolean vt52Mode = false;
380
381 /**
382 * Visible cursor (DECTCEM).
383 */
384 private boolean cursorVisible = true;
385
386 /**
387 * Screen title as set by the xterm OSC sequence. Lots of applications
388 * send a screenTitle regardless of whether it is an xterm client or not.
389 */
390 private String screenTitle = "";
391
392 /**
393 * Parameter characters being collected.
394 */
395 private List<Integer> csiParams;
396
397 /**
398 * Non-csi collect buffer.
399 */
400 private StringBuilder collectBuffer = new StringBuilder(128);
401
402 /**
403 * When true, use the G1 character set.
404 */
405 private boolean shiftOut = false;
406
407 /**
408 * Horizontal tab stop locations.
409 */
410 private List<Integer> tabStops;
411
412 /**
413 * S8C1T. True means 8bit controls, false means 7bit controls.
414 */
415 private boolean s8c1t = false;
416
417 /**
418 * Printer mode. True means send all output to printer, which discards
419 * it.
420 */
421 private boolean printerControllerMode = false;
422
423 /**
424 * LMN line mode. If true, linefeed() puts the cursor on the first
425 * column of the next line. If false, linefeed() puts the cursor one
426 * line down on the current line. The default is false.
427 */
428 private boolean newLineMode = false;
429
430 /**
431 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
432 */
433 private ArrowKeyMode arrowKeyMode;
434
435 /**
436 * Whether number pad keys send VT100 or VT52, application or numeric
437 * sequences.
438 */
439 @SuppressWarnings("unused")
440 private KeypadMode keypadMode;
441
442 /**
443 * When true, the terminal is in 132-column mode (DECCOLM).
444 */
445 private boolean columns132 = false;
446
447 /**
448 * true = reverse video. Set by DECSCNM.
449 */
450 private boolean reverseVideo = false;
451
452 /**
453 * false = echo characters locally.
454 */
455 private boolean fullDuplex = true;
456
457 /**
458 * The current terminal state.
459 */
460 private SaveableState currentState;
461
462 /**
463 * The last saved terminal state.
464 */
465 private SaveableState savedState;
466
467 /**
468 * The 88- or 256-color support RGB colors.
469 */
470 private List<Integer> colors88;
471
472 /**
473 * Sixel collection buffer.
474 */
475 private StringBuilder sixelParseBuffer = new StringBuilder(2048);
476
477 /**
478 * Sixel shared palette.
479 */
480 private HashMap<Integer, java.awt.Color> sixelPalette;
481
482 /**
483 * The width of a character cell in pixels.
484 */
485 private int textWidth = 16;
486
487 /**
488 * The height of a character cell in pixels.
489 */
490 private int textHeight = 20;
491
492 /**
493 * The last used height of a character cell in pixels, only used for
494 * full-width chars.
495 */
496 private int lastTextHeight = -1;
497
498 /**
499 * The glyph drawer for full-width chars.
500 */
501 private GlyphMaker glyphMaker = null;
502
503 /**
504 * Input queue for keystrokes and mouse events to send to the remote
505 * side.
506 */
507 private ArrayList<TInputEvent> userQueue = new ArrayList<TInputEvent>();
508
509 /**
510 * Number of bytes/characters passed to consume().
511 */
512 private long readCount = 0;
513
514 /**
515 * DECSC/DECRC save/restore a subset of the total state. This class
516 * encapsulates those specific flags/modes.
517 */
518 private class SaveableState {
519
520 /**
521 * When true, cursor positions are relative to the scrolling region.
522 */
523 public boolean originMode = false;
524
525 /**
526 * The current editing X position.
527 */
528 public int cursorX = 0;
529
530 /**
531 * The current editing Y position.
532 */
533 public int cursorY = 0;
534
535 /**
536 * Which character set is currently selected in G0.
537 */
538 public CharacterSet g0Charset = CharacterSet.US;
539
540 /**
541 * Which character set is currently selected in G1.
542 */
543 public CharacterSet g1Charset = CharacterSet.DRAWING;
544
545 /**
546 * Which character set is currently selected in G2.
547 */
548 public CharacterSet g2Charset = CharacterSet.US;
549
550 /**
551 * Which character set is currently selected in G3.
552 */
553 public CharacterSet g3Charset = CharacterSet.US;
554
555 /**
556 * Which character set is currently selected in GR.
557 */
558 public CharacterSet grCharset = CharacterSet.DRAWING;
559
560 /**
561 * The current drawing attributes.
562 */
563 public CellAttributes attr;
564
565 /**
566 * GL lockshift mode.
567 */
568 public LockshiftMode glLockshift = LockshiftMode.NONE;
569
570 /**
571 * GR lockshift mode.
572 */
573 public LockshiftMode grLockshift = LockshiftMode.NONE;
574
575 /**
576 * Line wrap.
577 */
578 public boolean lineWrap = true;
579
580 /**
581 * Reset to defaults.
582 */
583 public void reset() {
584 originMode = false;
585 cursorX = 0;
586 cursorY = 0;
587 g0Charset = CharacterSet.US;
588 g1Charset = CharacterSet.DRAWING;
589 g2Charset = CharacterSet.US;
590 g3Charset = CharacterSet.US;
591 grCharset = CharacterSet.DRAWING;
592 attr = new CellAttributes();
593 glLockshift = LockshiftMode.NONE;
594 grLockshift = LockshiftMode.NONE;
595 lineWrap = true;
596 }
597
598 /**
599 * Copy attributes from another instance.
600 *
601 * @param that the other instance to match
602 */
603 public void setTo(final SaveableState that) {
604 this.originMode = that.originMode;
605 this.cursorX = that.cursorX;
606 this.cursorY = that.cursorY;
607 this.g0Charset = that.g0Charset;
608 this.g1Charset = that.g1Charset;
609 this.g2Charset = that.g2Charset;
610 this.g3Charset = that.g3Charset;
611 this.grCharset = that.grCharset;
612 this.attr = new CellAttributes();
613 this.attr.setTo(that.attr);
614 this.glLockshift = that.glLockshift;
615 this.grLockshift = that.grLockshift;
616 this.lineWrap = that.lineWrap;
617 }
618
619 /**
620 * Public constructor.
621 */
622 public SaveableState() {
623 reset();
624 }
625 }
626
627 // ------------------------------------------------------------------------
628 // Constructors -----------------------------------------------------------
629 // ------------------------------------------------------------------------
630
631 /**
632 * Public constructor.
633 *
634 * @param type one of the DeviceType constants to select VT100, VT102,
635 * VT220, or XTERM
636 * @param inputStream an InputStream connected to the remote side. For
637 * type == XTERM, inputStream is converted to a Reader with UTF-8
638 * encoding.
639 * @param outputStream an OutputStream connected to the remote user. For
640 * type == XTERM, outputStream is converted to a Writer with UTF-8
641 * encoding.
642 * @param displayListener a callback to the outer display, or null for
643 * default VT100 behavior
644 * @throws UnsupportedEncodingException if an exception is thrown when
645 * creating the InputStreamReader
646 */
647 public ECMA48(final DeviceType type, final InputStream inputStream,
648 final OutputStream outputStream, final DisplayListener displayListener)
649 throws UnsupportedEncodingException {
650
651 assert (inputStream != null);
652 assert (outputStream != null);
653
654 csiParams = new ArrayList<Integer>();
655 tabStops = new ArrayList<Integer>();
656 scrollback = new ArrayList<DisplayLine>();
657 display = new ArrayList<DisplayLine>();
658
659 this.type = type;
660 if (inputStream instanceof TimeoutInputStream) {
661 this.inputStream = (TimeoutInputStream)inputStream;
662 } else {
663 this.inputStream = new TimeoutInputStream(inputStream, 2000);
664 }
665 if (type == DeviceType.XTERM) {
666 this.input = new InputStreamReader(new BufferedInputStream(
667 this.inputStream, 1024 * 128), "UTF-8");
668 this.output = new OutputStreamWriter(new
669 BufferedOutputStream(outputStream), "UTF-8");
670 this.outputStream = null;
671 } else {
672 this.output = null;
673 this.outputStream = new BufferedOutputStream(outputStream);
674 }
675 this.displayListener = displayListener;
676
677 reset();
678 for (int i = 0; i < height; i++) {
679 display.add(new DisplayLine(currentState.attr));
680 }
681 assert (currentState.cursorY < height);
682 assert (currentState.cursorX < width);
683
684 // Spin up the input reader
685 readerThread = new Thread(this);
686 readerThread.start();
687 }
688
689 // ------------------------------------------------------------------------
690 // Runnable ---------------------------------------------------------------
691 // ------------------------------------------------------------------------
692
693 /**
694 * Read function runs on a separate thread.
695 */
696 public final void run() {
697 boolean utf8 = false;
698 boolean done = false;
699
700 if (type == DeviceType.XTERM) {
701 utf8 = true;
702 }
703
704 // available() will often return > 1, so we need to read in chunks to
705 // stay caught up.
706 char [] readBufferUTF8 = null;
707 byte [] readBuffer = null;
708 if (utf8) {
709 readBufferUTF8 = new char[2048];
710 } else {
711 readBuffer = new byte[2048];
712 }
713
714 while (!done && !stopReaderThread) {
715 synchronized (userQueue) {
716 while (userQueue.size() > 0) {
717 handleUserEvent(userQueue.remove(0));
718 }
719 }
720
721 try {
722 int n = inputStream.available();
723
724 // System.err.printf("available() %d\n", n); System.err.flush();
725 if (utf8) {
726 if (readBufferUTF8.length < n) {
727 // The buffer wasn't big enough, make it huger
728 int newSizeHalf = Math.max(readBufferUTF8.length,
729 n);
730
731 readBufferUTF8 = new char[newSizeHalf * 2];
732 }
733 } else {
734 if (readBuffer.length < n) {
735 // The buffer wasn't big enough, make it huger
736 int newSizeHalf = Math.max(readBuffer.length, n);
737 readBuffer = new byte[newSizeHalf * 2];
738 }
739 }
740 if (n == 0) {
741 try {
742 Thread.sleep(10);
743 } catch (InterruptedException e) {
744 // SQUASH
745 }
746 continue;
747 }
748
749 int rc = -1;
750 try {
751 if (utf8) {
752 rc = input.read(readBufferUTF8, 0,
753 readBufferUTF8.length);
754 } else {
755 rc = inputStream.read(readBuffer, 0,
756 readBuffer.length);
757 }
758 } catch (ReadTimeoutException e) {
759 rc = 0;
760 }
761
762 // System.err.printf("read() %d\n", rc); System.err.flush();
763 if (rc == -1) {
764 // This is EOF
765 done = true;
766 } else {
767 // Don't step on UI events
768 synchronized (this) {
769 if (utf8) {
770 for (int i = 0; i < rc;) {
771 int ch = Character.codePointAt(readBufferUTF8,
772 i);
773 i += Character.charCount(ch);
774
775 // Special case for VT10x: 7-bit characters
776 // only.
777 if ((type == DeviceType.VT100)
778 || (type == DeviceType.VT102)
779 ) {
780 consume(ch & 0x7F);
781 } else {
782 consume(ch);
783 }
784 }
785 } else {
786 for (int i = 0; i < rc; i++) {
787 // Special case for VT10x: 7-bit characters
788 // only.
789 if ((type == DeviceType.VT100)
790 || (type == DeviceType.VT102)
791 ) {
792 consume(readBuffer[i] & 0x7F);
793 } else {
794 consume(readBuffer[i]);
795 }
796 }
797 }
798 }
799 // Permit my enclosing UI to know that I updated.
800 if (displayListener != null) {
801 displayListener.displayChanged();
802 }
803 }
804 // System.err.println("end while loop"); System.err.flush();
805 } catch (IOException e) {
806 done = true;
807
808 // This is an unusual case. We want to see the stack trace,
809 // but it is related to the spawned process rather than the
810 // actual UI. We will generate the stack trace, and consume
811 // it as though it was emitted by the shell.
812 CharArrayWriter writer= new CharArrayWriter();
813 // Send a ST and RIS to clear the emulator state.
814 try {
815 writer.write("\033\\\033c");
816 writer.write("\n-----------------------------------\n");
817 e.printStackTrace(new PrintWriter(writer));
818 writer.write("\n-----------------------------------\n");
819 } catch (IOException e2) {
820 // SQUASH
821 }
822 char [] stackTrace = writer.toCharArray();
823 for (int i = 0; i < stackTrace.length; i++) {
824 if (stackTrace[i] == '\n') {
825 consume('\r');
826 }
827 consume(stackTrace[i]);
828 }
829 }
830
831 } // while ((done == false) && (stopReaderThread == false))
832
833 // Let the rest of the world know that I am done.
834 stopReaderThread = true;
835
836 try {
837 inputStream.cancelRead();
838 inputStream.close();
839 inputStream = null;
840 } catch (IOException e) {
841 // SQUASH
842 }
843 try {
844 input.close();
845 input = null;
846 } catch (IOException e) {
847 // SQUASH
848 }
849
850 // Permit my enclosing UI to know that I updated.
851 if (displayListener != null) {
852 displayListener.displayChanged();
853 }
854
855 // System.err.println("*** run() exiting..."); System.err.flush();
856 }
857
858 // ------------------------------------------------------------------------
859 // ECMA48 -----------------------------------------------------------------
860 // ------------------------------------------------------------------------
861
862 /**
863 * Wait for a period of time to get output from the launched process.
864 *
865 * @param millis millis to wait for, or 0 to wait forever
866 * @return true if the launched process has emitted something
867 */
868 public boolean waitForOutput(final int millis) {
869 if (millis < 0) {
870 throw new IllegalArgumentException("timeout must be >= 0");
871 }
872 int waitedMillis = millis;
873 final int pollTimeout = 5;
874 while (true) {
875 if (readCount != 0) {
876 return true;
877 }
878 if ((millis > 0) && (waitedMillis < 0)){
879 return false;
880 }
881 try {
882 Thread.sleep(pollTimeout);
883 } catch (InterruptedException e) {
884 // SQUASH
885 }
886 waitedMillis -= pollTimeout;
887 }
888 }
889
890 /**
891 * Process keyboard and mouse events from the user.
892 *
893 * @param event the input event to consume
894 */
895 private void handleUserEvent(final TInputEvent event) {
896 if (event instanceof TKeypressEvent) {
897 keypress(((TKeypressEvent) event).getKey());
898 }
899 if (event instanceof TMouseEvent) {
900 mouse((TMouseEvent) event);
901 }
902 }
903
904 /**
905 * Add a keyboard and mouse event from the user to the queue.
906 *
907 * @param event the input event to consume
908 */
909 public void addUserEvent(final TInputEvent event) {
910 synchronized (userQueue) {
911 userQueue.add(event);
912 }
913 }
914
915 /**
916 * Return the proper primary Device Attributes string.
917 *
918 * @return string to send to remote side that is appropriate for the
919 * this.type
920 */
921 private String deviceTypeResponse() {
922 switch (type) {
923 case VT100:
924 // "I am a VT100 with advanced video option" (often VT102)
925 return "\033[?1;2c";
926
927 case VT102:
928 // "I am a VT102"
929 return "\033[?6c";
930
931 case VT220:
932 case XTERM:
933 // "I am a VT220" - 7 bit version, with sixel and Jexer image
934 // support.
935 if (!s8c1t) {
936 return "\033[?62;1;6;9;4;22;444c";
937 }
938 // "I am a VT220" - 8 bit version, with sixel and Jexer image
939 // support.
940 return "\u009b?62;1;6;9;4;22;444c";
941 default:
942 throw new IllegalArgumentException("Invalid device type: " + type);
943 }
944 }
945
946 /**
947 * Return the proper TERM environment variable for this device type.
948 *
949 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
950 * @return "vt100", "xterm", etc.
951 */
952 public static String deviceTypeTerm(final DeviceType deviceType) {
953 switch (deviceType) {
954 case VT100:
955 return "vt100";
956
957 case VT102:
958 return "vt102";
959
960 case VT220:
961 return "vt220";
962
963 case XTERM:
964 return "xterm";
965
966 default:
967 throw new IllegalArgumentException("Invalid device type: "
968 + deviceType);
969 }
970 }
971
972 /**
973 * Return the proper LANG for this device type. Only XTERM devices know
974 * about UTF-8, the others are defined by their standard to be either
975 * 7-bit or 8-bit characters only.
976 *
977 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
978 * @param baseLang a base language without UTF-8 flag such as "C" or
979 * "en_US"
980 * @return "en_US", "en_US.UTF-8", etc.
981 */
982 public static String deviceTypeLang(final DeviceType deviceType,
983 final String baseLang) {
984
985 switch (deviceType) {
986
987 case VT100:
988 case VT102:
989 case VT220:
990 return baseLang;
991
992 case XTERM:
993 return baseLang + ".UTF-8";
994
995 default:
996 throw new IllegalArgumentException("Invalid device type: "
997 + deviceType);
998 }
999 }
1000
1001 /**
1002 * Write a string directly to the remote side.
1003 *
1004 * @param str string to send
1005 */
1006 public void writeRemote(final String str) {
1007 if (stopReaderThread) {
1008 // Reader hit EOF, bail out now.
1009 close();
1010 return;
1011 }
1012
1013 // System.err.printf("writeRemote() '%s'\n", str);
1014
1015 switch (type) {
1016 case VT100:
1017 case VT102:
1018 case VT220:
1019 if (outputStream == null) {
1020 return;
1021 }
1022 try {
1023 outputStream.flush();
1024 for (int i = 0; i < str.length(); i++) {
1025 outputStream.write(str.charAt(i));
1026 }
1027 outputStream.flush();
1028 } catch (IOException e) {
1029 // Assume EOF
1030 close();
1031 }
1032 break;
1033 case XTERM:
1034 if (output == null) {
1035 return;
1036 }
1037 try {
1038 output.flush();
1039 output.write(str);
1040 output.flush();
1041 } catch (IOException e) {
1042 // Assume EOF
1043 close();
1044 }
1045 break;
1046 default:
1047 throw new IllegalArgumentException("Invalid device type: " + type);
1048 }
1049 }
1050
1051 /**
1052 * Close the input and output streams and stop the reader thread. Note
1053 * that it is safe to call this multiple times.
1054 */
1055 public final void close() {
1056
1057 // Tell the reader thread to stop looking at input. It will close
1058 // the input streams.
1059 if (stopReaderThread == false) {
1060 stopReaderThread = true;
1061 }
1062
1063 // Now close the output stream.
1064 switch (type) {
1065 case VT100:
1066 case VT102:
1067 case VT220:
1068 if (outputStream != null) {
1069 try {
1070 outputStream.close();
1071 } catch (IOException e) {
1072 // SQUASH
1073 }
1074 outputStream = null;
1075 }
1076 break;
1077 case XTERM:
1078 if (outputStream != null) {
1079 try {
1080 outputStream.close();
1081 } catch (IOException e) {
1082 // SQUASH
1083 }
1084 outputStream = null;
1085 }
1086 if (output != null) {
1087 try {
1088 output.close();
1089 } catch (IOException e) {
1090 // SQUASH
1091 }
1092 output = null;
1093 }
1094 break;
1095 default:
1096 throw new IllegalArgumentException("Invalid device type: " +
1097 type);
1098 }
1099 }
1100
1101 /**
1102 * See if the reader thread is still running.
1103 *
1104 * @return if true, we are still connected to / reading from the remote
1105 * side
1106 */
1107 public final boolean isReading() {
1108 return (!stopReaderThread);
1109 }
1110
1111 /**
1112 * Obtain a new blank display line for an external user
1113 * (e.g. TTerminalWindow).
1114 *
1115 * @return new blank line
1116 */
1117 public final DisplayLine getBlankDisplayLine() {
1118 return new DisplayLine(currentState.attr);
1119 }
1120
1121 /**
1122 * Get the scrollback buffer.
1123 *
1124 * @return the scrollback buffer
1125 */
1126 public final List<DisplayLine> getScrollbackBuffer() {
1127 return scrollback;
1128 }
1129
1130 /**
1131 * Get the display buffer.
1132 *
1133 * @return the display buffer
1134 */
1135 public final List<DisplayLine> getDisplayBuffer() {
1136 return display;
1137 }
1138
1139 /**
1140 * Get the visible display + scrollback buffer, offset by a specified
1141 * number of rows from the bottom.
1142 *
1143 * @param visibleHeight the total height of the display to show
1144 * @param scrollBottom the number of rows from the bottom to scroll back
1145 * @return a copy of the display + scrollback buffers
1146 */
1147 public final List<DisplayLine> getVisibleDisplay(final int visibleHeight,
1148 final int scrollBottom) {
1149
1150 assert (visibleHeight >= 0);
1151 assert (scrollBottom >= 0);
1152
1153 int visibleBottom = scrollback.size() + display.size() - scrollBottom;
1154
1155 List<DisplayLine> preceedingBlankLines = new ArrayList<DisplayLine>();
1156 int visibleTop = visibleBottom - visibleHeight;
1157 if (visibleTop < 0) {
1158 for (int i = visibleTop; i < 0; i++) {
1159 preceedingBlankLines.add(getBlankDisplayLine());
1160 }
1161 visibleTop = 0;
1162 }
1163 assert (visibleTop >= 0);
1164
1165 List<DisplayLine> displayLines = new ArrayList<DisplayLine>();
1166 displayLines.addAll(scrollback);
1167 displayLines.addAll(display);
1168
1169 List<DisplayLine> visibleLines = new ArrayList<DisplayLine>();
1170 visibleLines.addAll(preceedingBlankLines);
1171 visibleLines.addAll(displayLines.subList(visibleTop, visibleBottom));
1172
1173 // Fill in the blank lines on bottom
1174 int bottomBlankLines = visibleHeight - visibleLines.size();
1175 assert (bottomBlankLines >= 0);
1176 for (int i = 0; i < bottomBlankLines; i++) {
1177 visibleLines.add(getBlankDisplayLine());
1178 }
1179
1180 return copyBuffer(visibleLines);
1181 }
1182
1183 /**
1184 * Copy a display buffer.
1185 *
1186 * @param buffer the buffer to copy
1187 * @return a deep copy of the buffer's data
1188 */
1189 private List<DisplayLine> copyBuffer(final List<DisplayLine> buffer) {
1190 ArrayList<DisplayLine> result = new ArrayList<DisplayLine>(buffer.size());
1191 for (DisplayLine line: buffer) {
1192 result.add(new DisplayLine(line));
1193 }
1194 return result;
1195 }
1196
1197 /**
1198 * Get the display width.
1199 *
1200 * @return the width (usually 80 or 132)
1201 */
1202 public final int getWidth() {
1203 return width;
1204 }
1205
1206 /**
1207 * Set the display width.
1208 *
1209 * @param width the new width
1210 */
1211 public final synchronized void setWidth(final int width) {
1212 this.width = width;
1213 rightMargin = width - 1;
1214 if (currentState.cursorX >= width) {
1215 currentState.cursorX = width - 1;
1216 }
1217 if (savedState.cursorX >= width) {
1218 savedState.cursorX = width - 1;
1219 }
1220 }
1221
1222 /**
1223 * Get the display height.
1224 *
1225 * @return the height (usually 24)
1226 */
1227 public final int getHeight() {
1228 return height;
1229 }
1230
1231 /**
1232 * Set the display height.
1233 *
1234 * @param height the new height
1235 */
1236 public final synchronized void setHeight(final int height) {
1237 int delta = height - this.height;
1238 this.height = height;
1239 scrollRegionBottom += delta;
1240 if ((scrollRegionBottom < 0) || (scrollRegionTop > height - 1)) {
1241 scrollRegionBottom = height - 1;
1242 }
1243 if (scrollRegionTop >= scrollRegionBottom) {
1244 scrollRegionTop = 0;
1245 }
1246 if (currentState.cursorY >= height) {
1247 currentState.cursorY = height - 1;
1248 }
1249 if (savedState.cursorY >= height) {
1250 savedState.cursorY = height - 1;
1251 }
1252 while (display.size() < height) {
1253 DisplayLine line = new DisplayLine(currentState.attr);
1254 line.setReverseColor(reverseVideo);
1255 display.add(line);
1256 }
1257 while (display.size() > height) {
1258 appendScrollbackLine(display.remove(0));
1259 }
1260 }
1261
1262 /**
1263 * Get the maximum number of lines in the scrollback buffer.
1264 *
1265 * @return the maximum number of lines in the scrollback buffer
1266 */
1267 public int getScrollbackMax() {
1268 return scrollbackMax;
1269 }
1270
1271 /**
1272 * Set the maximum number of lines for the scrollback buffer.
1273 *
1274 * @param scrollbackMax the maximum number of lines for the scrollback
1275 * buffer
1276 */
1277 public final void setScrollbackMax(final int scrollbackMax) {
1278 this.scrollbackMax = scrollbackMax;
1279 }
1280
1281 /**
1282 * Get visible cursor flag.
1283 *
1284 * @return if true, the cursor is visible
1285 */
1286 public final boolean isCursorVisible() {
1287 return cursorVisible;
1288 }
1289
1290 /**
1291 * Get the screen title as set by the xterm OSC sequence. Lots of
1292 * applications send a screenTitle regardless of whether it is an xterm
1293 * client or not.
1294 *
1295 * @return screen title
1296 */
1297 public final String getScreenTitle() {
1298 return screenTitle;
1299 }
1300
1301 /**
1302 * Get 132 columns value.
1303 *
1304 * @return if true, the terminal is in 132 column mode
1305 */
1306 public final boolean isColumns132() {
1307 return columns132;
1308 }
1309
1310 /**
1311 * Clear the CSI parameters and flags.
1312 */
1313 private void toGround() {
1314 csiParams.clear();
1315 collectBuffer.setLength(0);
1316 scanState = ScanState.GROUND;
1317 }
1318
1319 /**
1320 * Reset the tab stops list.
1321 */
1322 private void resetTabStops() {
1323 tabStops.clear();
1324 for (int i = 0; (i * 8) <= rightMargin; i++) {
1325 tabStops.add(Integer.valueOf(i * 8));
1326 }
1327 }
1328
1329 /**
1330 * Reset the 88- or 256-colors.
1331 */
1332 private void resetColors() {
1333 colors88 = new ArrayList<Integer>(256);
1334 for (int i = 0; i < 256; i++) {
1335 colors88.add(0);
1336 }
1337
1338 // Set default system colors. These match DOS colors.
1339 colors88.set(0, 0x00000000);
1340 colors88.set(1, 0x00a80000);
1341 colors88.set(2, 0x0000a800);
1342 colors88.set(3, 0x00a85400);
1343 colors88.set(4, 0x000000a8);
1344 colors88.set(5, 0x00a800a8);
1345 colors88.set(6, 0x0000a8a8);
1346 colors88.set(7, 0x00a8a8a8);
1347
1348 colors88.set(8, 0x00545454);
1349 colors88.set(9, 0x00fc5454);
1350 colors88.set(10, 0x0054fc54);
1351 colors88.set(11, 0x00fcfc54);
1352 colors88.set(12, 0x005454fc);
1353 colors88.set(13, 0x00fc54fc);
1354 colors88.set(14, 0x0054fcfc);
1355 colors88.set(15, 0x00fcfcfc);
1356
1357 // These match xterm's default colors from 256colres.h.
1358 colors88.set(16, 0x000000);
1359 colors88.set(17, 0x00005f);
1360 colors88.set(18, 0x000087);
1361 colors88.set(19, 0x0000af);
1362 colors88.set(20, 0x0000d7);
1363 colors88.set(21, 0x0000ff);
1364 colors88.set(22, 0x005f00);
1365 colors88.set(23, 0x005f5f);
1366 colors88.set(24, 0x005f87);
1367 colors88.set(25, 0x005faf);
1368 colors88.set(26, 0x005fd7);
1369 colors88.set(27, 0x005fff);
1370 colors88.set(28, 0x008700);
1371 colors88.set(29, 0x00875f);
1372 colors88.set(30, 0x008787);
1373 colors88.set(31, 0x0087af);
1374 colors88.set(32, 0x0087d7);
1375 colors88.set(33, 0x0087ff);
1376 colors88.set(34, 0x00af00);
1377 colors88.set(35, 0x00af5f);
1378 colors88.set(36, 0x00af87);
1379 colors88.set(37, 0x00afaf);
1380 colors88.set(38, 0x00afd7);
1381 colors88.set(39, 0x00afff);
1382 colors88.set(40, 0x00d700);
1383 colors88.set(41, 0x00d75f);
1384 colors88.set(42, 0x00d787);
1385 colors88.set(43, 0x00d7af);
1386 colors88.set(44, 0x00d7d7);
1387 colors88.set(45, 0x00d7ff);
1388 colors88.set(46, 0x00ff00);
1389 colors88.set(47, 0x00ff5f);
1390 colors88.set(48, 0x00ff87);
1391 colors88.set(49, 0x00ffaf);
1392 colors88.set(50, 0x00ffd7);
1393 colors88.set(51, 0x00ffff);
1394 colors88.set(52, 0x5f0000);
1395 colors88.set(53, 0x5f005f);
1396 colors88.set(54, 0x5f0087);
1397 colors88.set(55, 0x5f00af);
1398 colors88.set(56, 0x5f00d7);
1399 colors88.set(57, 0x5f00ff);
1400 colors88.set(58, 0x5f5f00);
1401 colors88.set(59, 0x5f5f5f);
1402 colors88.set(60, 0x5f5f87);
1403 colors88.set(61, 0x5f5faf);
1404 colors88.set(62, 0x5f5fd7);
1405 colors88.set(63, 0x5f5fff);
1406 colors88.set(64, 0x5f8700);
1407 colors88.set(65, 0x5f875f);
1408 colors88.set(66, 0x5f8787);
1409 colors88.set(67, 0x5f87af);
1410 colors88.set(68, 0x5f87d7);
1411 colors88.set(69, 0x5f87ff);
1412 colors88.set(70, 0x5faf00);
1413 colors88.set(71, 0x5faf5f);
1414 colors88.set(72, 0x5faf87);
1415 colors88.set(73, 0x5fafaf);
1416 colors88.set(74, 0x5fafd7);
1417 colors88.set(75, 0x5fafff);
1418 colors88.set(76, 0x5fd700);
1419 colors88.set(77, 0x5fd75f);
1420 colors88.set(78, 0x5fd787);
1421 colors88.set(79, 0x5fd7af);
1422 colors88.set(80, 0x5fd7d7);
1423 colors88.set(81, 0x5fd7ff);
1424 colors88.set(82, 0x5fff00);
1425 colors88.set(83, 0x5fff5f);
1426 colors88.set(84, 0x5fff87);
1427 colors88.set(85, 0x5fffaf);
1428 colors88.set(86, 0x5fffd7);
1429 colors88.set(87, 0x5fffff);
1430 colors88.set(88, 0x870000);
1431 colors88.set(89, 0x87005f);
1432 colors88.set(90, 0x870087);
1433 colors88.set(91, 0x8700af);
1434 colors88.set(92, 0x8700d7);
1435 colors88.set(93, 0x8700ff);
1436 colors88.set(94, 0x875f00);
1437 colors88.set(95, 0x875f5f);
1438 colors88.set(96, 0x875f87);
1439 colors88.set(97, 0x875faf);
1440 colors88.set(98, 0x875fd7);
1441 colors88.set(99, 0x875fff);
1442 colors88.set(100, 0x878700);
1443 colors88.set(101, 0x87875f);
1444 colors88.set(102, 0x878787);
1445 colors88.set(103, 0x8787af);
1446 colors88.set(104, 0x8787d7);
1447 colors88.set(105, 0x8787ff);
1448 colors88.set(106, 0x87af00);
1449 colors88.set(107, 0x87af5f);
1450 colors88.set(108, 0x87af87);
1451 colors88.set(109, 0x87afaf);
1452 colors88.set(110, 0x87afd7);
1453 colors88.set(111, 0x87afff);
1454 colors88.set(112, 0x87d700);
1455 colors88.set(113, 0x87d75f);
1456 colors88.set(114, 0x87d787);
1457 colors88.set(115, 0x87d7af);
1458 colors88.set(116, 0x87d7d7);
1459 colors88.set(117, 0x87d7ff);
1460 colors88.set(118, 0x87ff00);
1461 colors88.set(119, 0x87ff5f);
1462 colors88.set(120, 0x87ff87);
1463 colors88.set(121, 0x87ffaf);
1464 colors88.set(122, 0x87ffd7);
1465 colors88.set(123, 0x87ffff);
1466 colors88.set(124, 0xaf0000);
1467 colors88.set(125, 0xaf005f);
1468 colors88.set(126, 0xaf0087);
1469 colors88.set(127, 0xaf00af);
1470 colors88.set(128, 0xaf00d7);
1471 colors88.set(129, 0xaf00ff);
1472 colors88.set(130, 0xaf5f00);
1473 colors88.set(131, 0xaf5f5f);
1474 colors88.set(132, 0xaf5f87);
1475 colors88.set(133, 0xaf5faf);
1476 colors88.set(134, 0xaf5fd7);
1477 colors88.set(135, 0xaf5fff);
1478 colors88.set(136, 0xaf8700);
1479 colors88.set(137, 0xaf875f);
1480 colors88.set(138, 0xaf8787);
1481 colors88.set(139, 0xaf87af);
1482 colors88.set(140, 0xaf87d7);
1483 colors88.set(141, 0xaf87ff);
1484 colors88.set(142, 0xafaf00);
1485 colors88.set(143, 0xafaf5f);
1486 colors88.set(144, 0xafaf87);
1487 colors88.set(145, 0xafafaf);
1488 colors88.set(146, 0xafafd7);
1489 colors88.set(147, 0xafafff);
1490 colors88.set(148, 0xafd700);
1491 colors88.set(149, 0xafd75f);
1492 colors88.set(150, 0xafd787);
1493 colors88.set(151, 0xafd7af);
1494 colors88.set(152, 0xafd7d7);
1495 colors88.set(153, 0xafd7ff);
1496 colors88.set(154, 0xafff00);
1497 colors88.set(155, 0xafff5f);
1498 colors88.set(156, 0xafff87);
1499 colors88.set(157, 0xafffaf);
1500 colors88.set(158, 0xafffd7);
1501 colors88.set(159, 0xafffff);
1502 colors88.set(160, 0xd70000);
1503 colors88.set(161, 0xd7005f);
1504 colors88.set(162, 0xd70087);
1505 colors88.set(163, 0xd700af);
1506 colors88.set(164, 0xd700d7);
1507 colors88.set(165, 0xd700ff);
1508 colors88.set(166, 0xd75f00);
1509 colors88.set(167, 0xd75f5f);
1510 colors88.set(168, 0xd75f87);
1511 colors88.set(169, 0xd75faf);
1512 colors88.set(170, 0xd75fd7);
1513 colors88.set(171, 0xd75fff);
1514 colors88.set(172, 0xd78700);
1515 colors88.set(173, 0xd7875f);
1516 colors88.set(174, 0xd78787);
1517 colors88.set(175, 0xd787af);
1518 colors88.set(176, 0xd787d7);
1519 colors88.set(177, 0xd787ff);
1520 colors88.set(178, 0xd7af00);
1521 colors88.set(179, 0xd7af5f);
1522 colors88.set(180, 0xd7af87);
1523 colors88.set(181, 0xd7afaf);
1524 colors88.set(182, 0xd7afd7);
1525 colors88.set(183, 0xd7afff);
1526 colors88.set(184, 0xd7d700);
1527 colors88.set(185, 0xd7d75f);
1528 colors88.set(186, 0xd7d787);
1529 colors88.set(187, 0xd7d7af);
1530 colors88.set(188, 0xd7d7d7);
1531 colors88.set(189, 0xd7d7ff);
1532 colors88.set(190, 0xd7ff00);
1533 colors88.set(191, 0xd7ff5f);
1534 colors88.set(192, 0xd7ff87);
1535 colors88.set(193, 0xd7ffaf);
1536 colors88.set(194, 0xd7ffd7);
1537 colors88.set(195, 0xd7ffff);
1538 colors88.set(196, 0xff0000);
1539 colors88.set(197, 0xff005f);
1540 colors88.set(198, 0xff0087);
1541 colors88.set(199, 0xff00af);
1542 colors88.set(200, 0xff00d7);
1543 colors88.set(201, 0xff00ff);
1544 colors88.set(202, 0xff5f00);
1545 colors88.set(203, 0xff5f5f);
1546 colors88.set(204, 0xff5f87);
1547 colors88.set(205, 0xff5faf);
1548 colors88.set(206, 0xff5fd7);
1549 colors88.set(207, 0xff5fff);
1550 colors88.set(208, 0xff8700);
1551 colors88.set(209, 0xff875f);
1552 colors88.set(210, 0xff8787);
1553 colors88.set(211, 0xff87af);
1554 colors88.set(212, 0xff87d7);
1555 colors88.set(213, 0xff87ff);
1556 colors88.set(214, 0xffaf00);
1557 colors88.set(215, 0xffaf5f);
1558 colors88.set(216, 0xffaf87);
1559 colors88.set(217, 0xffafaf);
1560 colors88.set(218, 0xffafd7);
1561 colors88.set(219, 0xffafff);
1562 colors88.set(220, 0xffd700);
1563 colors88.set(221, 0xffd75f);
1564 colors88.set(222, 0xffd787);
1565 colors88.set(223, 0xffd7af);
1566 colors88.set(224, 0xffd7d7);
1567 colors88.set(225, 0xffd7ff);
1568 colors88.set(226, 0xffff00);
1569 colors88.set(227, 0xffff5f);
1570 colors88.set(228, 0xffff87);
1571 colors88.set(229, 0xffffaf);
1572 colors88.set(230, 0xffffd7);
1573 colors88.set(231, 0xffffff);
1574 colors88.set(232, 0x080808);
1575 colors88.set(233, 0x121212);
1576 colors88.set(234, 0x1c1c1c);
1577 colors88.set(235, 0x262626);
1578 colors88.set(236, 0x303030);
1579 colors88.set(237, 0x3a3a3a);
1580 colors88.set(238, 0x444444);
1581 colors88.set(239, 0x4e4e4e);
1582 colors88.set(240, 0x585858);
1583 colors88.set(241, 0x626262);
1584 colors88.set(242, 0x6c6c6c);
1585 colors88.set(243, 0x767676);
1586 colors88.set(244, 0x808080);
1587 colors88.set(245, 0x8a8a8a);
1588 colors88.set(246, 0x949494);
1589 colors88.set(247, 0x9e9e9e);
1590 colors88.set(248, 0xa8a8a8);
1591 colors88.set(249, 0xb2b2b2);
1592 colors88.set(250, 0xbcbcbc);
1593 colors88.set(251, 0xc6c6c6);
1594 colors88.set(252, 0xd0d0d0);
1595 colors88.set(253, 0xdadada);
1596 colors88.set(254, 0xe4e4e4);
1597 colors88.set(255, 0xeeeeee);
1598
1599 }
1600
1601 /**
1602 * Get the RGB value of one of the indexed colors.
1603 *
1604 * @param index the color index
1605 * @return the RGB value
1606 */
1607 private int get88Color(final int index) {
1608 // System.err.print("get88Color: " + index);
1609 if ((index < 0) || (index > colors88.size())) {
1610 // System.err.println(" -- UNKNOWN");
1611 return 0;
1612 }
1613 // System.err.printf(" %08x\n", colors88.get(index));
1614 return colors88.get(index);
1615 }
1616
1617 /**
1618 * Set one of the indexed colors to a color specification.
1619 *
1620 * @param index the color index
1621 * @param spec the specification, typically something like "rgb:aa/bb/cc"
1622 */
1623 private void set88Color(final int index, final String spec) {
1624 // System.err.println("set88Color: " + index + " '" + spec + "'");
1625
1626 if ((index < 0) || (index > colors88.size())) {
1627 return;
1628 }
1629 if (spec.startsWith("rgb:")) {
1630 String [] rgbTokens = spec.substring(4).split("/");
1631 if (rgbTokens.length == 3) {
1632 try {
1633 int rgb = (Integer.parseInt(rgbTokens[0], 16) << 16);
1634 rgb |= Integer.parseInt(rgbTokens[1], 16) << 8;
1635 rgb |= Integer.parseInt(rgbTokens[2], 16);
1636 // System.err.printf(" set to %08x\n", rgb);
1637 colors88.set(index, rgb);
1638 } catch (NumberFormatException e) {
1639 // SQUASH
1640 }
1641 }
1642 return;
1643 }
1644
1645 if (spec.toLowerCase().equals("black")) {
1646 colors88.set(index, 0x00000000);
1647 } else if (spec.toLowerCase().equals("red")) {
1648 colors88.set(index, 0x00a80000);
1649 } else if (spec.toLowerCase().equals("green")) {
1650 colors88.set(index, 0x0000a800);
1651 } else if (spec.toLowerCase().equals("yellow")) {
1652 colors88.set(index, 0x00a85400);
1653 } else if (spec.toLowerCase().equals("blue")) {
1654 colors88.set(index, 0x000000a8);
1655 } else if (spec.toLowerCase().equals("magenta")) {
1656 colors88.set(index, 0x00a800a8);
1657 } else if (spec.toLowerCase().equals("cyan")) {
1658 colors88.set(index, 0x0000a8a8);
1659 } else if (spec.toLowerCase().equals("white")) {
1660 colors88.set(index, 0x00a8a8a8);
1661 }
1662
1663 }
1664
1665 /**
1666 * Reset the emulation state.
1667 */
1668 private void reset() {
1669
1670 currentState = new SaveableState();
1671 savedState = new SaveableState();
1672 scanState = ScanState.GROUND;
1673 if (displayListener != null) {
1674 width = displayListener.getDisplayWidth();
1675 height = displayListener.getDisplayHeight();
1676 } else {
1677 width = 80;
1678 height = 24;
1679 }
1680 scrollRegionTop = 0;
1681 scrollRegionBottom = height - 1;
1682 rightMargin = width - 1;
1683 newLineMode = false;
1684 arrowKeyMode = ArrowKeyMode.ANSI;
1685 keypadMode = KeypadMode.Numeric;
1686 wrapLineFlag = false;
1687
1688 // Flags
1689 shiftOut = false;
1690 vt52Mode = false;
1691 insertMode = false;
1692 columns132 = false;
1693 newLineMode = false;
1694 reverseVideo = false;
1695 fullDuplex = true;
1696 cursorVisible = true;
1697
1698 // VT220
1699 singleshift = Singleshift.NONE;
1700 s8c1t = false;
1701 printerControllerMode = false;
1702
1703 // XTERM
1704 mouseProtocol = MouseProtocol.OFF;
1705 mouseEncoding = MouseEncoding.X10;
1706
1707 // Tab stops
1708 resetTabStops();
1709
1710 // Reset extra colors
1711 resetColors();
1712
1713 // Clear CSI stuff
1714 toGround();
1715 }
1716
1717 /**
1718 * Append a to the scrollback buffer, clearing image data for lines more
1719 * than three screenfuls in.
1720 */
1721 private void appendScrollbackLine(DisplayLine line) {
1722 scrollback.add(line);
1723 if (scrollback.size() > height * 3) {
1724 scrollback.get(scrollback.size() - (height * 3)).clearImages();
1725 }
1726 }
1727
1728 /**
1729 * Append a new line to the bottom of the display, adding lines off the
1730 * top to the scrollback buffer.
1731 */
1732 private void newDisplayLine() {
1733 // Scroll the top line off into the scrollback buffer
1734 appendScrollbackLine(display.get(0));
1735 while (scrollback.size() > scrollbackMax) {
1736 scrollback.remove(0);
1737 scrollback.trimToSize();
1738 }
1739 display.remove(0);
1740 display.trimToSize();
1741 DisplayLine line = new DisplayLine(currentState.attr);
1742 line.setReverseColor(reverseVideo);
1743 display.add(line);
1744 }
1745
1746 /**
1747 * Wraps the current line.
1748 */
1749 private void wrapCurrentLine() {
1750 if (currentState.cursorY == height - 1) {
1751 newDisplayLine();
1752 }
1753 if (currentState.cursorY < height - 1) {
1754 currentState.cursorY++;
1755 }
1756 currentState.cursorX = 0;
1757 }
1758
1759 /**
1760 * Handle a carriage return.
1761 */
1762 private void carriageReturn() {
1763 currentState.cursorX = 0;
1764 wrapLineFlag = false;
1765 }
1766
1767 /**
1768 * Reverse the color of the visible display.
1769 */
1770 private void invertDisplayColors() {
1771 for (DisplayLine line: display) {
1772 line.setReverseColor(!line.isReverseColor());
1773 }
1774 }
1775
1776 /**
1777 * Handle a linefeed.
1778 */
1779 private void linefeed() {
1780 if (currentState.cursorY < scrollRegionBottom) {
1781 // Increment screen y
1782 currentState.cursorY++;
1783 } else {
1784
1785 // Screen y does not increment
1786
1787 /*
1788 * Two cases: either we're inside a scrolling region or not. If
1789 * the scrolling region bottom is the bottom of the screen, then
1790 * push the top line into the buffer. Else scroll the scrolling
1791 * region up.
1792 */
1793 if ((scrollRegionBottom == height - 1) && (scrollRegionTop == 0)) {
1794
1795 // We're at the bottom of the scroll region, AND the scroll
1796 // region is the entire screen.
1797
1798 // New line
1799 newDisplayLine();
1800
1801 } else {
1802 // We're at the bottom of the scroll region, AND the scroll
1803 // region is NOT the entire screen.
1804 scrollingRegionScrollUp(scrollRegionTop, scrollRegionBottom, 1);
1805 }
1806 }
1807
1808 if (newLineMode) {
1809 currentState.cursorX = 0;
1810 }
1811 wrapLineFlag = false;
1812 }
1813
1814 /**
1815 * Prints one character to the display buffer.
1816 *
1817 * @param ch character to display
1818 */
1819 private void printCharacter(final int ch) {
1820 int rightMargin = this.rightMargin;
1821
1822 if (StringUtils.width(ch) == 2) {
1823 // This is a full-width character. Save two spaces, and then
1824 // draw the character as two image halves.
1825 int x0 = currentState.cursorX;
1826 int y0 = currentState.cursorY;
1827 printCharacter(' ');
1828 printCharacter(' ');
1829 if ((currentState.cursorX == x0 + 2)
1830 && (currentState.cursorY == y0)
1831 ) {
1832 // We can draw both halves of the character.
1833 drawHalves(x0, y0, x0 + 1, y0, ch);
1834 } else if ((currentState.cursorX == x0 + 1)
1835 && (currentState.cursorY == y0)
1836 ) {
1837 // VT100 line wrap behavior: we should be at the right
1838 // margin. We can draw both halves of the character.
1839 drawHalves(x0, y0, x0 + 1, y0, ch);
1840 } else {
1841 // The character splits across the line. Draw the entire
1842 // character on the new line, giving one more space for it.
1843 x0 = currentState.cursorX - 1;
1844 y0 = currentState.cursorY;
1845 printCharacter(' ');
1846 drawHalves(x0, y0, x0 + 1, y0, ch);
1847 }
1848 return;
1849 }
1850
1851 // Check if we have double-width, and if so chop at 40/66 instead of
1852 // 80/132
1853 if (display.get(currentState.cursorY).isDoubleWidth()) {
1854 rightMargin = ((rightMargin + 1) / 2) - 1;
1855 }
1856
1857 // Check the unusually-complicated line wrapping conditions...
1858 if (currentState.cursorX == rightMargin) {
1859
1860 if (currentState.lineWrap == true) {
1861 /*
1862 * This case happens when: the cursor was already on the
1863 * right margin (either through printing or by an explicit
1864 * placement command), and a character was printed.
1865 *
1866 * The line wraps only when a new character arrives AND the
1867 * cursor is already on the right margin AND has placed a
1868 * character in its cell. Easier to see than to explain.
1869 */
1870 if (wrapLineFlag == false) {
1871 /*
1872 * This block marks the case that we are in the margin
1873 * and the first character has been received and printed.
1874 */
1875 wrapLineFlag = true;
1876 } else {
1877 /*
1878 * This block marks the case that we are in the margin
1879 * and the second character has been received and
1880 * printed.
1881 */
1882 wrapLineFlag = false;
1883 wrapCurrentLine();
1884 }
1885 }
1886 } else if (currentState.cursorX <= rightMargin) {
1887 /*
1888 * This is the normal case: a character came in and was printed
1889 * to the left of the right margin column.
1890 */
1891
1892 // Turn off VT100 special-case flag
1893 wrapLineFlag = false;
1894 }
1895
1896 // "Print" the character
1897 Cell newCell = new Cell(ch);
1898 CellAttributes newCellAttributes = (CellAttributes) newCell;
1899 newCellAttributes.setTo(currentState.attr);
1900 DisplayLine line = display.get(currentState.cursorY);
1901
1902 if (StringUtils.width(ch) == 1) {
1903 // Insert mode special case
1904 if (insertMode == true) {
1905 line.insert(currentState.cursorX, newCell);
1906 } else {
1907 // Replace an existing character
1908 line.replace(currentState.cursorX, newCell);
1909 }
1910
1911 // Increment horizontal
1912 if (wrapLineFlag == false) {
1913 currentState.cursorX++;
1914 if (currentState.cursorX > rightMargin) {
1915 currentState.cursorX--;
1916 }
1917 }
1918 }
1919 }
1920
1921 /**
1922 * Translate the mouse event to a VT100, VT220, or XTERM sequence and
1923 * send to the remote side.
1924 *
1925 * @param mouse mouse event received from the local user
1926 */
1927 private void mouse(final TMouseEvent mouse) {
1928
1929 /*
1930 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1931 mouseProtocol, mouseEncoding, mouse);
1932 */
1933
1934 if (mouseEncoding == MouseEncoding.X10) {
1935 // We will support X10 but only for (160,94) and smaller.
1936 if ((mouse.getX() >= 160) || (mouse.getY() >= 94)) {
1937 return;
1938 }
1939 }
1940
1941 switch (mouseProtocol) {
1942
1943 case OFF:
1944 // Do nothing
1945 return;
1946
1947 case X10:
1948 // Only report button presses
1949 if (mouse.getType() != TMouseEvent.Type.MOUSE_DOWN) {
1950 return;
1951 }
1952 break;
1953
1954 case NORMAL:
1955 // Only report button presses and releases
1956 if ((mouse.getType() != TMouseEvent.Type.MOUSE_DOWN)
1957 && (mouse.getType() != TMouseEvent.Type.MOUSE_UP)
1958 ) {
1959 return;
1960 }
1961 break;
1962
1963 case BUTTONEVENT:
1964 /*
1965 * Only report button presses, button releases, and motions that
1966 * have a button down (i.e. drag-and-drop).
1967 */
1968 if (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) {
1969 if (!mouse.isMouse1()
1970 && !mouse.isMouse2()
1971 && !mouse.isMouse3()
1972 && !mouse.isMouseWheelUp()
1973 && !mouse.isMouseWheelDown()
1974 ) {
1975 return;
1976 }
1977 }
1978 break;
1979
1980 case ANYEVENT:
1981 // Report everything
1982 break;
1983 }
1984
1985 // Now encode the event
1986 StringBuilder sb = new StringBuilder(6);
1987 if (mouseEncoding == MouseEncoding.SGR) {
1988 sb.append((char) 0x1B);
1989 sb.append("[<");
1990 int buttons = 0;
1991
1992 if (mouse.isMouse1()) {
1993 if (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) {
1994 buttons = 32;
1995 } else {
1996 buttons = 0;
1997 }
1998 } else if (mouse.isMouse2()) {
1999 if (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) {
2000 buttons = 33;
2001 } else {
2002 buttons = 1;
2003 }
2004 } else if (mouse.isMouse3()) {
2005 if (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) {
2006 buttons = 34;
2007 } else {
2008 buttons = 2;
2009 }
2010 } else if (mouse.isMouseWheelUp()) {
2011 buttons = 64;
2012 } else if (mouse.isMouseWheelDown()) {
2013 buttons = 65;
2014 } else {
2015 // This is motion with no buttons down.
2016 buttons = 35;
2017 }
2018 if (mouse.isAlt()) {
2019 buttons |= 0x08;
2020 }
2021 if (mouse.isCtrl()) {
2022 buttons |= 0x10;
2023 }
2024 if (mouse.isShift()) {
2025 buttons |= 0x04;
2026 }
2027
2028 sb.append(String.format("%d;%d;%d", buttons, mouse.getX() + 1,
2029 mouse.getY() + 1));
2030
2031 if (mouse.getType() == TMouseEvent.Type.MOUSE_UP) {
2032 sb.append("m");
2033 } else {
2034 sb.append("M");
2035 }
2036
2037 } else {
2038 // X10 and UTF8 encodings
2039 sb.append((char) 0x1B);
2040 sb.append('[');
2041 sb.append('M');
2042 int buttons = 0;
2043 if (mouse.getType() == TMouseEvent.Type.MOUSE_UP) {
2044 buttons = 0x03 + 32;
2045 } else if (mouse.isMouse1()) {
2046 if (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) {
2047 buttons = 0x00 + 32 + 32;
2048 } else {
2049 buttons = 0x00 + 32;
2050 }
2051 } else if (mouse.isMouse2()) {
2052 if (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) {
2053 buttons = 0x01 + 32 + 32;
2054 } else {
2055 buttons = 0x01 + 32;
2056 }
2057 } else if (mouse.isMouse3()) {
2058 if (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) {
2059 buttons = 0x02 + 32 + 32;
2060 } else {
2061 buttons = 0x02 + 32;
2062 }
2063 } else if (mouse.isMouseWheelUp()) {
2064 buttons = 0x04 + 64;
2065 } else if (mouse.isMouseWheelDown()) {
2066 buttons = 0x05 + 64;
2067 } else {
2068 // This is motion with no buttons down.
2069 buttons = 0x03 + 32;
2070 }
2071 if (mouse.isAlt()) {
2072 buttons |= 0x08;
2073 }
2074 if (mouse.isCtrl()) {
2075 buttons |= 0x10;
2076 }
2077 if (mouse.isShift()) {
2078 buttons |= 0x04;
2079 }
2080
2081 sb.append((char) (buttons & 0xFF));
2082 sb.append((char) (mouse.getX() + 33));
2083 sb.append((char) (mouse.getY() + 33));
2084 }
2085
2086 // System.err.printf("Would write: \'%s\'\n", sb.toString());
2087 writeRemote(sb.toString());
2088 }
2089
2090 /**
2091 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
2092 * send to the remote side.
2093 *
2094 * @param keypress keypress received from the local user
2095 */
2096 private void keypress(final TKeypress keypress) {
2097 writeRemote(keypressToString(keypress));
2098 }
2099
2100 /**
2101 * Build one of the complex xterm keystroke sequences, storing the result in
2102 * xterm_keystroke_buffer.
2103 *
2104 * @param ss3 the prefix to use based on VT100 state.
2105 * @param first the first character, usually a number.
2106 * @param first the last character, one of the following: ~ A B C D F H
2107 * @param ctrl whether or not ctrl is down
2108 * @param alt whether or not alt is down
2109 * @param shift whether or not shift is down
2110 * @return the buffer with the full key sequence
2111 */
2112 private String xtermBuildKeySequence(final String ss3, final char first,
2113 final char last, boolean ctrl, boolean alt, boolean shift) {
2114
2115 StringBuilder sb = new StringBuilder(ss3);
2116 if ((last == '~') || (ctrl == true) || (alt == true)
2117 || (shift == true)
2118 ) {
2119 sb.append(first);
2120 if ( (ctrl == false) && (alt == false) && (shift == true)) {
2121 sb.append(";2");
2122 } else if ((ctrl == false) && (alt == true) && (shift == false)) {
2123 sb.append(";3");
2124 } else if ((ctrl == false) && (alt == true) && (shift == true)) {
2125 sb.append(";4");
2126 } else if ((ctrl == true) && (alt == false) && (shift == false)) {
2127 sb.append(";5");
2128 } else if ((ctrl == true) && (alt == false) && (shift == true)) {
2129 sb.append(";6");
2130 } else if ((ctrl == true) && (alt == true) && (shift == false)) {
2131 sb.append(";7");
2132 } else if ((ctrl == true) && (alt == true) && (shift == true)) {
2133 sb.append(";8");
2134 }
2135 }
2136 sb.append(last);
2137 return sb.toString();
2138 }
2139
2140 /**
2141 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
2142 *
2143 * @param keypress keypress received from the local user
2144 * @return string to transmit to the remote side
2145 */
2146 @SuppressWarnings("fallthrough")
2147 private String keypressToString(final TKeypress keypress) {
2148
2149 if ((fullDuplex == false) && (!keypress.isFnKey())) {
2150 /*
2151 * If this is a control character, process it like it came from
2152 * the remote side.
2153 */
2154 if (keypress.getChar() < 0x20) {
2155 handleControlChar((char) keypress.getChar());
2156 } else {
2157 // Local echo for everything else
2158 printCharacter(keypress.getChar());
2159 }
2160 if (displayListener != null) {
2161 displayListener.displayChanged();
2162 }
2163 }
2164
2165 if ((newLineMode == true) && (keypress.equals(kbEnter))) {
2166 // NLM: send CRLF
2167 return "\015\012";
2168 }
2169
2170 // Handle control characters
2171 if ((keypress.isCtrl()) && (!keypress.isFnKey())) {
2172 StringBuilder sb = new StringBuilder();
2173 int ch = keypress.getChar();
2174 ch -= 0x40;
2175 sb.append(Character.toChars(ch));
2176 return sb.toString();
2177 }
2178
2179 // Handle alt characters
2180 if ((keypress.isAlt()) && (!keypress.isFnKey())) {
2181 StringBuilder sb = new StringBuilder("\033");
2182 int ch = keypress.getChar();
2183 sb.append(Character.toChars(ch));
2184 return sb.toString();
2185 }
2186
2187 if (keypress.equals(kbBackspaceDel)) {
2188 switch (type) {
2189 case VT100:
2190 return "\010";
2191 case VT102:
2192 return "\010";
2193 case VT220:
2194 return "\177";
2195 case XTERM:
2196 return "\177";
2197 }
2198 }
2199
2200 if (keypress.equalsWithoutModifiers(kbLeft)) {
2201 switch (type) {
2202 case XTERM:
2203 switch (arrowKeyMode) {
2204 case ANSI:
2205 return xtermBuildKeySequence("\033[", '1', 'D',
2206 keypress.isCtrl(), keypress.isAlt(),
2207 keypress.isShift());
2208 case VT52:
2209 return xtermBuildKeySequence("\033", '1', 'D',
2210 keypress.isCtrl(), keypress.isAlt(),
2211 keypress.isShift());
2212 case VT100:
2213 return xtermBuildKeySequence("\033O", '1', 'D',
2214 keypress.isCtrl(), keypress.isAlt(),
2215 keypress.isShift());
2216 }
2217 default:
2218 switch (arrowKeyMode) {
2219 case ANSI:
2220 return "\033[D";
2221 case VT52:
2222 return "\033D";
2223 case VT100:
2224 return "\033OD";
2225 }
2226 }
2227 }
2228
2229 if (keypress.equalsWithoutModifiers(kbRight)) {
2230 switch (type) {
2231 case XTERM:
2232 switch (arrowKeyMode) {
2233 case ANSI:
2234 return xtermBuildKeySequence("\033[", '1', 'C',
2235 keypress.isCtrl(), keypress.isAlt(),
2236 keypress.isShift());
2237 case VT52:
2238 return xtermBuildKeySequence("\033", '1', 'C',
2239 keypress.isCtrl(), keypress.isAlt(),
2240 keypress.isShift());
2241 case VT100:
2242 return xtermBuildKeySequence("\033O", '1', 'C',
2243 keypress.isCtrl(), keypress.isAlt(),
2244 keypress.isShift());
2245 }
2246 default:
2247 switch (arrowKeyMode) {
2248 case ANSI:
2249 return "\033[C";
2250 case VT52:
2251 return "\033C";
2252 case VT100:
2253 return "\033OC";
2254 }
2255 }
2256 }
2257
2258 if (keypress.equalsWithoutModifiers(kbUp)) {
2259 switch (type) {
2260 case XTERM:
2261 switch (arrowKeyMode) {
2262 case ANSI:
2263 return xtermBuildKeySequence("\033[", '1', 'A',
2264 keypress.isCtrl(), keypress.isAlt(),
2265 keypress.isShift());
2266 case VT52:
2267 return xtermBuildKeySequence("\033", '1', 'A',
2268 keypress.isCtrl(), keypress.isAlt(),
2269 keypress.isShift());
2270 case VT100:
2271 return xtermBuildKeySequence("\033O", '1', 'A',
2272 keypress.isCtrl(), keypress.isAlt(),
2273 keypress.isShift());
2274 }
2275 default:
2276 switch (arrowKeyMode) {
2277 case ANSI:
2278 return "\033[A";
2279 case VT52:
2280 return "\033A";
2281 case VT100:
2282 return "\033OA";
2283 }
2284 }
2285 }
2286
2287 if (keypress.equalsWithoutModifiers(kbDown)) {
2288 switch (type) {
2289 case XTERM:
2290 switch (arrowKeyMode) {
2291 case ANSI:
2292 return xtermBuildKeySequence("\033[", '1', 'B',
2293 keypress.isCtrl(), keypress.isAlt(),
2294 keypress.isShift());
2295 case VT52:
2296 return xtermBuildKeySequence("\033", '1', 'B',
2297 keypress.isCtrl(), keypress.isAlt(),
2298 keypress.isShift());
2299 case VT100:
2300 return xtermBuildKeySequence("\033O", '1', 'B',
2301 keypress.isCtrl(), keypress.isAlt(),
2302 keypress.isShift());
2303 }
2304 default:
2305 switch (arrowKeyMode) {
2306 case ANSI:
2307 return "\033[B";
2308 case VT52:
2309 return "\033B";
2310 case VT100:
2311 return "\033OB";
2312 }
2313 }
2314 }
2315
2316 if (keypress.equalsWithoutModifiers(kbHome)) {
2317 switch (type) {
2318 case XTERM:
2319 switch (arrowKeyMode) {
2320 case ANSI:
2321 return xtermBuildKeySequence("\033[", '1', 'H',
2322 keypress.isCtrl(), keypress.isAlt(),
2323 keypress.isShift());
2324 case VT52:
2325 return xtermBuildKeySequence("\033", '1', 'H',
2326 keypress.isCtrl(), keypress.isAlt(),
2327 keypress.isShift());
2328 case VT100:
2329 return xtermBuildKeySequence("\033O", '1', 'H',
2330 keypress.isCtrl(), keypress.isAlt(),
2331 keypress.isShift());
2332 }
2333 default:
2334 switch (arrowKeyMode) {
2335 case ANSI:
2336 return "\033[H";
2337 case VT52:
2338 return "\033H";
2339 case VT100:
2340 return "\033OH";
2341 }
2342 }
2343 }
2344
2345 if (keypress.equalsWithoutModifiers(kbEnd)) {
2346 switch (type) {
2347 case XTERM:
2348 switch (arrowKeyMode) {
2349 case ANSI:
2350 return xtermBuildKeySequence("\033[", '1', 'F',
2351 keypress.isCtrl(), keypress.isAlt(),
2352 keypress.isShift());
2353 case VT52:
2354 return xtermBuildKeySequence("\033", '1', 'F',
2355 keypress.isCtrl(), keypress.isAlt(),
2356 keypress.isShift());
2357 case VT100:
2358 return xtermBuildKeySequence("\033O", '1', 'F',
2359 keypress.isCtrl(), keypress.isAlt(),
2360 keypress.isShift());
2361 }
2362 default:
2363 switch (arrowKeyMode) {
2364 case ANSI:
2365 return "\033[F";
2366 case VT52:
2367 return "\033F";
2368 case VT100:
2369 return "\033OF";
2370 }
2371 }
2372 }
2373
2374 if (keypress.equals(kbF1)) {
2375 // PF1
2376 if (vt52Mode) {
2377 return "\033P";
2378 }
2379 return "\033OP";
2380 }
2381
2382 if (keypress.equals(kbF2)) {
2383 // PF2
2384 if (vt52Mode) {
2385 return "\033Q";
2386 }
2387 return "\033OQ";
2388 }
2389
2390 if (keypress.equals(kbF3)) {
2391 // PF3
2392 if (vt52Mode) {
2393 return "\033R";
2394 }
2395 return "\033OR";
2396 }
2397
2398 if (keypress.equals(kbF4)) {
2399 // PF4
2400 if (vt52Mode) {
2401 return "\033S";
2402 }
2403 return "\033OS";
2404 }
2405
2406 if (keypress.equals(kbF5)) {
2407 switch (type) {
2408 case VT100:
2409 return "\033Ot";
2410 case VT102:
2411 return "\033Ot";
2412 case VT220:
2413 return "\033[15~";
2414 case XTERM:
2415 return "\033[15~";
2416 }
2417 }
2418
2419 if (keypress.equals(kbF6)) {
2420 switch (type) {
2421 case VT100:
2422 return "\033Ou";
2423 case VT102:
2424 return "\033Ou";
2425 case VT220:
2426 return "\033[17~";
2427 case XTERM:
2428 return "\033[17~";
2429 }
2430 }
2431
2432 if (keypress.equals(kbF7)) {
2433 switch (type) {
2434 case VT100:
2435 return "\033Ov";
2436 case VT102:
2437 return "\033Ov";
2438 case VT220:
2439 return "\033[18~";
2440 case XTERM:
2441 return "\033[18~";
2442 }
2443 }
2444
2445 if (keypress.equals(kbF8)) {
2446 switch (type) {
2447 case VT100:
2448 return "\033Ol";
2449 case VT102:
2450 return "\033Ol";
2451 case VT220:
2452 return "\033[19~";
2453 case XTERM:
2454 return "\033[19~";
2455 }
2456 }
2457
2458 if (keypress.equals(kbF9)) {
2459 switch (type) {
2460 case VT100:
2461 return "\033Ow";
2462 case VT102:
2463 return "\033Ow";
2464 case VT220:
2465 return "\033[20~";
2466 case XTERM:
2467 return "\033[20~";
2468 }
2469 }
2470
2471 if (keypress.equals(kbF10)) {
2472 switch (type) {
2473 case VT100:
2474 return "\033Ox";
2475 case VT102:
2476 return "\033Ox";
2477 case VT220:
2478 return "\033[21~";
2479 case XTERM:
2480 return "\033[21~";
2481 }
2482 }
2483
2484 if (keypress.equals(kbF11)) {
2485 return "\033[23~";
2486 }
2487
2488 if (keypress.equals(kbF12)) {
2489 return "\033[24~";
2490 }
2491
2492 if (keypress.equals(kbShiftF1)) {
2493 // Shifted PF1
2494 if (vt52Mode) {
2495 return "\0332P";
2496 }
2497 if (type == DeviceType.XTERM) {
2498 return "\0331;2P";
2499 }
2500 return "\033O2P";
2501 }
2502
2503 if (keypress.equals(kbShiftF2)) {
2504 // Shifted PF2
2505 if (vt52Mode) {
2506 return "\0332Q";
2507 }
2508 if (type == DeviceType.XTERM) {
2509 return "\0331;2Q";
2510 }
2511 return "\033O2Q";
2512 }
2513
2514 if (keypress.equals(kbShiftF3)) {
2515 // Shifted PF3
2516 if (vt52Mode) {
2517 return "\0332R";
2518 }
2519 if (type == DeviceType.XTERM) {
2520 return "\0331;2R";
2521 }
2522 return "\033O2R";
2523 }
2524
2525 if (keypress.equals(kbShiftF4)) {
2526 // Shifted PF4
2527 if (vt52Mode) {
2528 return "\0332S";
2529 }
2530 if (type == DeviceType.XTERM) {
2531 return "\0331;2S";
2532 }
2533 return "\033O2S";
2534 }
2535
2536 if (keypress.equals(kbShiftF5)) {
2537 // Shifted F5
2538 return "\033[15;2~";
2539 }
2540
2541 if (keypress.equals(kbShiftF6)) {
2542 // Shifted F6
2543 return "\033[17;2~";
2544 }
2545
2546 if (keypress.equals(kbShiftF7)) {
2547 // Shifted F7
2548 return "\033[18;2~";
2549 }
2550
2551 if (keypress.equals(kbShiftF8)) {
2552 // Shifted F8
2553 return "\033[19;2~";
2554 }
2555
2556 if (keypress.equals(kbShiftF9)) {
2557 // Shifted F9
2558 return "\033[20;2~";
2559 }
2560
2561 if (keypress.equals(kbShiftF10)) {
2562 // Shifted F10
2563 return "\033[21;2~";
2564 }
2565
2566 if (keypress.equals(kbShiftF11)) {
2567 // Shifted F11
2568 return "\033[23;2~";
2569 }
2570
2571 if (keypress.equals(kbShiftF12)) {
2572 // Shifted F12
2573 return "\033[24;2~";
2574 }
2575
2576 if (keypress.equals(kbCtrlF1)) {
2577 // Control PF1
2578 if (vt52Mode) {
2579 return "\0335P";
2580 }
2581 if (type == DeviceType.XTERM) {
2582 return "\0331;5P";
2583 }
2584 return "\033O5P";
2585 }
2586
2587 if (keypress.equals(kbCtrlF2)) {
2588 // Control PF2
2589 if (vt52Mode) {
2590 return "\0335Q";
2591 }
2592 if (type == DeviceType.XTERM) {
2593 return "\0331;5Q";
2594 }
2595 return "\033O5Q";
2596 }
2597
2598 if (keypress.equals(kbCtrlF3)) {
2599 // Control PF3
2600 if (vt52Mode) {
2601 return "\0335R";
2602 }
2603 if (type == DeviceType.XTERM) {
2604 return "\0331;5R";
2605 }
2606 return "\033O5R";
2607 }
2608
2609 if (keypress.equals(kbCtrlF4)) {
2610 // Control PF4
2611 if (vt52Mode) {
2612 return "\0335S";
2613 }
2614 if (type == DeviceType.XTERM) {
2615 return "\0331;5S";
2616 }
2617 return "\033O5S";
2618 }
2619
2620 if (keypress.equals(kbCtrlF5)) {
2621 // Control F5
2622 return "\033[15;5~";
2623 }
2624
2625 if (keypress.equals(kbCtrlF6)) {
2626 // Control F6
2627 return "\033[17;5~";
2628 }
2629
2630 if (keypress.equals(kbCtrlF7)) {
2631 // Control F7
2632 return "\033[18;5~";
2633 }
2634
2635 if (keypress.equals(kbCtrlF8)) {
2636 // Control F8
2637 return "\033[19;5~";
2638 }
2639
2640 if (keypress.equals(kbCtrlF9)) {
2641 // Control F9
2642 return "\033[20;5~";
2643 }
2644
2645 if (keypress.equals(kbCtrlF10)) {
2646 // Control F10
2647 return "\033[21;5~";
2648 }
2649
2650 if (keypress.equals(kbCtrlF11)) {
2651 // Control F11
2652 return "\033[23;5~";
2653 }
2654
2655 if (keypress.equals(kbCtrlF12)) {
2656 // Control F12
2657 return "\033[24;5~";
2658 }
2659
2660 if (keypress.equalsWithoutModifiers(kbPgUp)) {
2661 switch (type) {
2662 case XTERM:
2663 return xtermBuildKeySequence("\033[", '5', '~',
2664 keypress.isCtrl(), keypress.isAlt(),
2665 keypress.isShift());
2666 default:
2667 return "\033[5~";
2668 }
2669 }
2670
2671 if (keypress.equalsWithoutModifiers(kbPgDn)) {
2672 switch (type) {
2673 case XTERM:
2674 return xtermBuildKeySequence("\033[", '6', '~',
2675 keypress.isCtrl(), keypress.isAlt(),
2676 keypress.isShift());
2677 default:
2678 return "\033[6~";
2679 }
2680 }
2681
2682 if (keypress.equalsWithoutModifiers(kbIns)) {
2683 switch (type) {
2684 case XTERM:
2685 return xtermBuildKeySequence("\033[", '2', '~',
2686 keypress.isCtrl(), keypress.isAlt(),
2687 keypress.isShift());
2688 default:
2689 return "\033[2~";
2690 }
2691 }
2692
2693 if (keypress.equalsWithoutModifiers(kbDel)) {
2694 switch (type) {
2695 case XTERM:
2696 return xtermBuildKeySequence("\033[", '3', '~',
2697 keypress.isCtrl(), keypress.isAlt(),
2698 keypress.isShift());
2699 default:
2700 // Delete sends real delete for VTxxx
2701 return "\177";
2702 }
2703 }
2704
2705 if (keypress.equals(kbEnter)) {
2706 return "\015";
2707 }
2708
2709 if (keypress.equals(kbEsc)) {
2710 return "\033";
2711 }
2712
2713 if (keypress.equals(kbAltEsc)) {
2714 return "\033\033";
2715 }
2716
2717 if (keypress.equals(kbTab)) {
2718 return "\011";
2719 }
2720
2721 if ((keypress.equalsWithoutModifiers(kbBackTab)) ||
2722 (keypress.equals(kbShiftTab))
2723 ) {
2724 switch (type) {
2725 case XTERM:
2726 return "\033[Z";
2727 default:
2728 return "\011";
2729 }
2730 }
2731
2732 // Non-alt, non-ctrl characters
2733 if (!keypress.isFnKey()) {
2734 StringBuilder sb = new StringBuilder();
2735 sb.append(Character.toChars(keypress.getChar()));
2736 return sb.toString();
2737 }
2738 return "";
2739 }
2740
2741 /**
2742 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
2743 * symbol.
2744 *
2745 * @param ch 8-bit character from the remote side
2746 * @param charsetGl character set defined for GL
2747 * @param charsetGr character set defined for GR
2748 * @return character to display on the screen
2749 */
2750 private char mapCharacterCharset(final int ch,
2751 final CharacterSet charsetGl,
2752 final CharacterSet charsetGr) {
2753
2754 int lookupChar = ch;
2755 CharacterSet lookupCharset = charsetGl;
2756
2757 if (ch >= 0x80) {
2758 assert ((type == DeviceType.VT220) || (type == DeviceType.XTERM));
2759 lookupCharset = charsetGr;
2760 lookupChar &= 0x7F;
2761 }
2762
2763 switch (lookupCharset) {
2764
2765 case DRAWING:
2766 return DECCharacterSets.SPECIAL_GRAPHICS[lookupChar];
2767
2768 case UK:
2769 return DECCharacterSets.UK[lookupChar];
2770
2771 case US:
2772 return DECCharacterSets.US_ASCII[lookupChar];
2773
2774 case NRC_DUTCH:
2775 return DECCharacterSets.NL[lookupChar];
2776
2777 case NRC_FINNISH:
2778 return DECCharacterSets.FI[lookupChar];
2779
2780 case NRC_FRENCH:
2781 return DECCharacterSets.FR[lookupChar];
2782
2783 case NRC_FRENCH_CA:
2784 return DECCharacterSets.FR_CA[lookupChar];
2785
2786 case NRC_GERMAN:
2787 return DECCharacterSets.DE[lookupChar];
2788
2789 case NRC_ITALIAN:
2790 return DECCharacterSets.IT[lookupChar];
2791
2792 case NRC_NORWEGIAN:
2793 return DECCharacterSets.NO[lookupChar];
2794
2795 case NRC_SPANISH:
2796 return DECCharacterSets.ES[lookupChar];
2797
2798 case NRC_SWEDISH:
2799 return DECCharacterSets.SV[lookupChar];
2800
2801 case NRC_SWISS:
2802 return DECCharacterSets.SWISS[lookupChar];
2803
2804 case DEC_SUPPLEMENTAL:
2805 return DECCharacterSets.DEC_SUPPLEMENTAL[lookupChar];
2806
2807 case VT52_GRAPHICS:
2808 return DECCharacterSets.VT52_SPECIAL_GRAPHICS[lookupChar];
2809
2810 case ROM:
2811 return DECCharacterSets.US_ASCII[lookupChar];
2812
2813 case ROM_SPECIAL:
2814 return DECCharacterSets.US_ASCII[lookupChar];
2815
2816 default:
2817 throw new IllegalArgumentException("Invalid character set value: "
2818 + lookupCharset);
2819 }
2820 }
2821
2822 /**
2823 * Map an 8-bit byte into a printable character.
2824 *
2825 * @param ch either 8-bit or Unicode character from the remote side
2826 * @return character to display on the screen
2827 */
2828 private int mapCharacter(final int ch) {
2829 if (ch >= 0x100) {
2830 // Unicode character, just return it
2831 return ch;
2832 }
2833
2834 CharacterSet charsetGl = currentState.g0Charset;
2835 CharacterSet charsetGr = currentState.grCharset;
2836
2837 if (vt52Mode == true) {
2838 if (shiftOut == true) {
2839 // Shifted out character, pull from VT52 graphics
2840 charsetGl = currentState.g1Charset;
2841 charsetGr = CharacterSet.US;
2842 } else {
2843 // Normal
2844 charsetGl = currentState.g0Charset;
2845 charsetGr = CharacterSet.US;
2846 }
2847
2848 // Pull the character
2849 return mapCharacterCharset(ch, charsetGl, charsetGr);
2850 }
2851
2852 // shiftOout
2853 if (shiftOut == true) {
2854 // Shifted out character, pull from G1
2855 charsetGl = currentState.g1Charset;
2856 charsetGr = currentState.grCharset;
2857
2858 // Pull the character
2859 return mapCharacterCharset(ch, charsetGl, charsetGr);
2860 }
2861
2862 // SS2
2863 if (singleshift == Singleshift.SS2) {
2864
2865 singleshift = Singleshift.NONE;
2866
2867 // Shifted out character, pull from G2
2868 charsetGl = currentState.g2Charset;
2869 charsetGr = currentState.grCharset;
2870 }
2871
2872 // SS3
2873 if (singleshift == Singleshift.SS3) {
2874
2875 singleshift = Singleshift.NONE;
2876
2877 // Shifted out character, pull from G3
2878 charsetGl = currentState.g3Charset;
2879 charsetGr = currentState.grCharset;
2880 }
2881
2882 if ((type == DeviceType.VT220) || (type == DeviceType.XTERM)) {
2883 // Check for locking shift
2884
2885 switch (currentState.glLockshift) {
2886
2887 case G1_GR:
2888 throw new IllegalArgumentException("programming bug");
2889
2890 case G2_GR:
2891 throw new IllegalArgumentException("programming bug");
2892
2893 case G3_GR:
2894 throw new IllegalArgumentException("programming bug");
2895
2896 case G2_GL:
2897 // LS2
2898 charsetGl = currentState.g2Charset;
2899 break;
2900
2901 case G3_GL:
2902 // LS3
2903 charsetGl = currentState.g3Charset;
2904 break;
2905
2906 case NONE:
2907 // Normal
2908 charsetGl = currentState.g0Charset;
2909 break;
2910 }
2911
2912 switch (currentState.grLockshift) {
2913
2914 case G2_GL:
2915 throw new IllegalArgumentException("programming bug");
2916
2917 case G3_GL:
2918 throw new IllegalArgumentException("programming bug");
2919
2920 case G1_GR:
2921 // LS1R
2922 charsetGr = currentState.g1Charset;
2923 break;
2924
2925 case G2_GR:
2926 // LS2R
2927 charsetGr = currentState.g2Charset;
2928 break;
2929
2930 case G3_GR:
2931 // LS3R
2932 charsetGr = currentState.g3Charset;
2933 break;
2934
2935 case NONE:
2936 // Normal
2937 charsetGr = CharacterSet.DEC_SUPPLEMENTAL;
2938 break;
2939 }
2940
2941
2942 }
2943
2944 // Pull the character
2945 return mapCharacterCharset(ch, charsetGl, charsetGr);
2946 }
2947
2948 /**
2949 * Scroll the text within a scrolling region up n lines.
2950 *
2951 * @param regionTop top row of the scrolling region
2952 * @param regionBottom bottom row of the scrolling region
2953 * @param n number of lines to scroll
2954 */
2955 private void scrollingRegionScrollUp(final int regionTop,
2956 final int regionBottom, final int n) {
2957
2958 if (regionTop >= regionBottom) {
2959 return;
2960 }
2961
2962 // Sanity check: see if there will be any characters left after the
2963 // scroll
2964 if (regionBottom + 1 - regionTop <= n) {
2965 // There won't be anything left in the region, so just call
2966 // eraseScreen() and return.
2967 eraseScreen(regionTop, 0, regionBottom, width - 1, false);
2968 return;
2969 }
2970
2971 int remaining = regionBottom + 1 - regionTop - n;
2972 List<DisplayLine> displayTop = display.subList(0, regionTop);
2973 List<DisplayLine> displayBottom = display.subList(regionBottom + 1,
2974 display.size());
2975 List<DisplayLine> displayMiddle = display.subList(regionBottom + 1
2976 - remaining, regionBottom + 1);
2977 display = new ArrayList<DisplayLine>(displayTop);
2978 display.addAll(displayMiddle);
2979 for (int i = 0; i < n; i++) {
2980 DisplayLine line = new DisplayLine(currentState.attr);
2981 line.setReverseColor(reverseVideo);
2982 display.add(line);
2983 }
2984 display.addAll(displayBottom);
2985
2986 assert (display.size() == height);
2987 }
2988
2989 /**
2990 * Scroll the text within a scrolling region down n lines.
2991 *
2992 * @param regionTop top row of the scrolling region
2993 * @param regionBottom bottom row of the scrolling region
2994 * @param n number of lines to scroll
2995 */
2996 private void scrollingRegionScrollDown(final int regionTop,
2997 final int regionBottom, final int n) {
2998
2999 if (regionTop >= regionBottom) {
3000 return;
3001 }
3002
3003 // Sanity check: see if there will be any characters left after the
3004 // scroll
3005 if (regionBottom + 1 - regionTop <= n) {
3006 // There won't be anything left in the region, so just call
3007 // eraseScreen() and return.
3008 eraseScreen(regionTop, 0, regionBottom, width - 1, false);
3009 return;
3010 }
3011
3012 int remaining = regionBottom + 1 - regionTop - n;
3013 List<DisplayLine> displayTop = display.subList(0, regionTop);
3014 List<DisplayLine> displayBottom = display.subList(regionBottom + 1,
3015 display.size());
3016 List<DisplayLine> displayMiddle = display.subList(regionTop,
3017 regionTop + remaining);
3018 display = new ArrayList<DisplayLine>(displayTop);
3019 for (int i = 0; i < n; i++) {
3020 DisplayLine line = new DisplayLine(currentState.attr);
3021 line.setReverseColor(reverseVideo);
3022 display.add(line);
3023 }
3024 display.addAll(displayMiddle);
3025 display.addAll(displayBottom);
3026
3027 assert (display.size() == height);
3028 }
3029
3030 /**
3031 * Process a control character.
3032 *
3033 * @param ch 8-bit character from the remote side
3034 */
3035 private void handleControlChar(final char ch) {
3036 assert ((ch <= 0x1F) || ((ch >= 0x7F) && (ch <= 0x9F)));
3037
3038 switch (ch) {
3039
3040 case 0x00:
3041 // NUL - discard
3042 return;
3043
3044 case 0x05:
3045 // ENQ
3046
3047 // Transmit the answerback message.
3048 // Not supported
3049 break;
3050
3051 case 0x07:
3052 // BEL
3053 // Not supported
3054 break;
3055
3056 case 0x08:
3057 // BS
3058 cursorLeft(1, false);
3059 break;
3060
3061 case 0x09:
3062 // HT
3063 advanceToNextTabStop();
3064 break;
3065
3066 case 0x0A:
3067 // LF
3068 linefeed();
3069 break;
3070
3071 case 0x0B:
3072 // VT
3073 linefeed();
3074 break;
3075
3076 case 0x0C:
3077 // FF
3078 linefeed();
3079 break;
3080
3081 case 0x0D:
3082 // CR
3083 carriageReturn();
3084 break;
3085
3086 case 0x0E:
3087 // SO
3088 shiftOut = true;
3089 currentState.glLockshift = LockshiftMode.NONE;
3090 break;
3091
3092 case 0x0F:
3093 // SI
3094 shiftOut = false;
3095 currentState.glLockshift = LockshiftMode.NONE;
3096 break;
3097
3098 case 0x84:
3099 // IND
3100 ind();
3101 break;
3102
3103 case 0x85:
3104 // NEL
3105 nel();
3106 break;
3107
3108 case 0x88:
3109 // HTS
3110 hts();
3111 break;
3112
3113 case 0x8D:
3114 // RI
3115 ri();
3116 break;
3117
3118 case 0x8E:
3119 // SS2
3120 singleshift = Singleshift.SS2;
3121 break;
3122
3123 case 0x8F:
3124 // SS3
3125 singleshift = Singleshift.SS3;
3126 break;
3127
3128 default:
3129 break;
3130 }
3131
3132 }
3133
3134 /**
3135 * Advance the cursor to the next tab stop.
3136 */
3137 private void advanceToNextTabStop() {
3138 if (tabStops.size() == 0) {
3139 // Go to the rightmost column
3140 cursorRight(rightMargin - currentState.cursorX, false);
3141 return;
3142 }
3143 for (Integer stop: tabStops) {
3144 if (stop > currentState.cursorX) {
3145 cursorRight(stop - currentState.cursorX, false);
3146 return;
3147 }
3148 }
3149 /*
3150 * We got here, meaning there isn't a tab stop beyond the current
3151 * cursor position. Place the cursor of the right-most edge of the
3152 * screen.
3153 */
3154 cursorRight(rightMargin - currentState.cursorX, false);
3155 }
3156
3157 /**
3158 * Save a character into the collect buffer.
3159 *
3160 * @param ch character to save
3161 */
3162 private void collect(final char ch) {
3163 collectBuffer.append(ch);
3164 }
3165
3166 /**
3167 * Save a byte into the CSI parameters buffer.
3168 *
3169 * @param ch byte to save
3170 */
3171 private void param(final byte ch) {
3172 if (csiParams.size() == 0) {
3173 csiParams.add(Integer.valueOf(0));
3174 }
3175 Integer x = csiParams.get(csiParams.size() - 1);
3176 if ((ch >= '0') && (ch <= '9')) {
3177 x *= 10;
3178 x += (ch - '0');
3179 csiParams.set(csiParams.size() - 1, x);
3180 }
3181
3182 if ((ch == ';') && (csiParams.size() < 16)) {
3183 csiParams.add(Integer.valueOf(0));
3184 }
3185 }
3186
3187 /**
3188 * Get a CSI parameter value, with a default.
3189 *
3190 * @param position parameter index. 0 is the first parameter.
3191 * @param defaultValue value to use if csiParams[position] doesn't exist
3192 * @return parameter value
3193 */
3194 private int getCsiParam(final int position, final int defaultValue) {
3195 if (csiParams.size() < position + 1) {
3196 return defaultValue;
3197 }
3198 return csiParams.get(position).intValue();
3199 }
3200
3201 /**
3202 * Get a CSI parameter value, clamped to within min/max.
3203 *
3204 * @param position parameter index. 0 is the first parameter.
3205 * @param defaultValue value to use if csiParams[position] doesn't exist
3206 * @param minValue minimum value inclusive
3207 * @param maxValue maximum value inclusive
3208 * @return parameter value
3209 */
3210 private int getCsiParam(final int position, final int defaultValue,
3211 final int minValue, final int maxValue) {
3212
3213 assert (minValue <= maxValue);
3214 int value = getCsiParam(position, defaultValue);
3215 if (value < minValue) {
3216 value = minValue;
3217 }
3218 if (value > maxValue) {
3219 value = maxValue;
3220 }
3221 return value;
3222 }
3223
3224 /**
3225 * Set or unset a toggle.
3226 *
3227 * @param value true for set ('h'), false for reset ('l')
3228 */
3229 private void setToggle(final boolean value) {
3230 boolean decPrivateModeFlag = false;
3231
3232 for (int i = 0; i < collectBuffer.length(); i++) {
3233 if (collectBuffer.charAt(i) == '?') {
3234 decPrivateModeFlag = true;
3235 break;
3236 }
3237 }
3238
3239 for (Integer i: csiParams) {
3240
3241 switch (i) {
3242
3243 case 1:
3244 if (decPrivateModeFlag == true) {
3245 // DECCKM
3246 if (value == true) {
3247 // Use application arrow keys
3248 arrowKeyMode = ArrowKeyMode.VT100;
3249 } else {
3250 // Use ANSI arrow keys
3251 arrowKeyMode = ArrowKeyMode.ANSI;
3252 }
3253 }
3254 break;
3255 case 2:
3256 if (decPrivateModeFlag == true) {
3257 if (value == false) {
3258
3259 // DECANM
3260 vt52Mode = true;
3261 arrowKeyMode = ArrowKeyMode.VT52;
3262
3263 /*
3264 * From the VT102 docs: "You use ANSI mode to select
3265 * most terminal features; the terminal uses the same
3266 * features when it switches to VT52 mode. You
3267 * cannot, however, change most of these features in
3268 * VT52 mode."
3269 *
3270 * In other words, do not reset any other attributes
3271 * when switching between VT52 submode and ANSI.
3272 *
3273 * HOWEVER, the real vt100 does switch the character
3274 * set according to Usenet.
3275 */
3276 currentState.g0Charset = CharacterSet.US;
3277 currentState.g1Charset = CharacterSet.DRAWING;
3278 shiftOut = false;
3279
3280 if ((type == DeviceType.VT220)
3281 || (type == DeviceType.XTERM)) {
3282
3283 // VT52 mode is explicitly 7-bit
3284 s8c1t = false;
3285 singleshift = Singleshift.NONE;
3286 }
3287 }
3288 } else {
3289 // KAM
3290 if (value == true) {
3291 // Turn off keyboard
3292 // Not supported
3293 } else {
3294 // Turn on keyboard
3295 // Not supported
3296 }
3297 }
3298 break;
3299 case 3:
3300 if (decPrivateModeFlag == true) {
3301 // DECCOLM
3302 if (value == true) {
3303 // 132 columns
3304 columns132 = true;
3305 rightMargin = 131;
3306 } else {
3307 // 80 columns
3308 columns132 = false;
3309 if ((displayListener != null)
3310 && (type == DeviceType.XTERM)
3311 ) {
3312 // For xterms, reset to the actual width, not 80
3313 // columns.
3314 width = displayListener.getDisplayWidth();
3315 rightMargin = width - 1;
3316 } else {
3317 rightMargin = 79;
3318 width = rightMargin + 1;
3319 }
3320 }
3321 // Entire screen is cleared, and scrolling region is
3322 // reset
3323 eraseScreen(0, 0, height - 1, width - 1, false);
3324 scrollRegionTop = 0;
3325 scrollRegionBottom = height - 1;
3326 // Also home the cursor
3327 cursorPosition(0, 0);
3328 }
3329 break;
3330 case 4:
3331 if (decPrivateModeFlag == true) {
3332 // DECSCLM
3333 if (value == true) {
3334 // Smooth scroll
3335 // Not supported
3336 } else {
3337 // Jump scroll
3338 // Not supported
3339 }
3340 } else {
3341 // IRM
3342 if (value == true) {
3343 insertMode = true;
3344 } else {
3345 insertMode = false;
3346 }
3347 }
3348 break;
3349 case 5:
3350 if (decPrivateModeFlag == true) {
3351 // DECSCNM
3352 if (value == true) {
3353 /*
3354 * Set selects reverse screen, a white screen
3355 * background with black characters.
3356 */
3357 if (reverseVideo != true) {
3358 /*
3359 * If in normal video, switch it back
3360 */
3361 invertDisplayColors();
3362 }
3363 reverseVideo = true;
3364 } else {
3365 /*
3366 * Reset selects normal screen, a black screen
3367 * background with white characters.
3368 */
3369 if (reverseVideo == true) {
3370 /*
3371 * If in reverse video already, switch it back
3372 */
3373 invertDisplayColors();
3374 }
3375 reverseVideo = false;
3376 }
3377 }
3378 break;
3379 case 6:
3380 if (decPrivateModeFlag == true) {
3381 // DECOM
3382 if (value == true) {
3383 // Origin is relative to scroll region cursor.
3384 // Cursor can NEVER leave scrolling region.
3385 currentState.originMode = true;
3386 cursorPosition(0, 0);
3387 } else {
3388 // Origin is absolute to entire screen. Cursor can
3389 // leave the scrolling region via cup() and hvp().
3390 currentState.originMode = false;
3391 cursorPosition(0, 0);
3392 }
3393 }
3394 break;
3395 case 7:
3396 if (decPrivateModeFlag == true) {
3397 // DECAWM
3398 if (value == true) {
3399 // Turn linewrap on
3400 currentState.lineWrap = true;
3401 } else {
3402 // Turn linewrap off
3403 currentState.lineWrap = false;
3404 }
3405 }
3406 break;
3407 case 8:
3408 if (decPrivateModeFlag == true) {
3409 // DECARM
3410 if (value == true) {
3411 // Keyboard auto-repeat on
3412 // Not supported
3413 } else {
3414 // Keyboard auto-repeat off
3415 // Not supported
3416 }
3417 }
3418 break;
3419 case 12:
3420 if (decPrivateModeFlag == false) {
3421 // SRM
3422 if (value == true) {
3423 // Local echo off
3424 fullDuplex = true;
3425 } else {
3426 // Local echo on
3427 fullDuplex = false;
3428 }
3429 }
3430 break;
3431 case 18:
3432 if (decPrivateModeFlag == true) {
3433 // DECPFF
3434 // Not supported
3435 }
3436 break;
3437 case 19:
3438 if (decPrivateModeFlag == true) {
3439 // DECPEX
3440 // Not supported
3441 }
3442 break;
3443 case 20:
3444 if (decPrivateModeFlag == false) {
3445 // LNM
3446 if (value == true) {
3447 /*
3448 * Set causes a received linefeed, form feed, or
3449 * vertical tab to move cursor to first column of
3450 * next line. RETURN transmits both a carriage return
3451 * and linefeed. This selection is also called new
3452 * line option.
3453 */
3454 newLineMode = true;
3455 } else {
3456 /*
3457 * Reset causes a received linefeed, form feed, or
3458 * vertical tab to move cursor to next line in
3459 * current column. RETURN transmits a carriage
3460 * return.
3461 */
3462 newLineMode = false;
3463 }
3464 }
3465 break;
3466
3467 case 25:
3468 if ((type == DeviceType.VT220) || (type == DeviceType.XTERM)) {
3469 if (decPrivateModeFlag == true) {
3470 // DECTCEM
3471 if (value == true) {
3472 // Visible cursor
3473 cursorVisible = true;
3474 } else {
3475 // Invisible cursor
3476 cursorVisible = false;
3477 }
3478 }
3479 }
3480 break;
3481
3482 case 42:
3483 if ((type == DeviceType.VT220) || (type == DeviceType.XTERM)) {
3484 if (decPrivateModeFlag == true) {
3485 // DECNRCM
3486 if (value == true) {
3487 // Select national mode NRC
3488 // Not supported
3489 } else {
3490 // Select multi-national mode
3491 // Not supported
3492 }
3493 }
3494 }
3495
3496 break;
3497
3498 case 80:
3499 if (type == DeviceType.XTERM) {
3500 if (decPrivateModeFlag == true) {
3501 if (value == true) {
3502 // Enable sixel scrolling (default).
3503 // Not supported
3504 } else {
3505 // Disable sixel scrolling.
3506 // Not supported
3507 }
3508 }
3509 }
3510
3511 break;
3512
3513 case 1000:
3514 if ((type == DeviceType.XTERM)
3515 && (decPrivateModeFlag == true)
3516 ) {
3517 // Mouse: normal tracking mode
3518 if (value == true) {
3519 mouseProtocol = MouseProtocol.NORMAL;
3520 } else {
3521 mouseProtocol = MouseProtocol.OFF;
3522 }
3523 }
3524 break;
3525
3526 case 1002:
3527 if ((type == DeviceType.XTERM)
3528 && (decPrivateModeFlag == true)
3529 ) {
3530 // Mouse: normal tracking mode
3531 if (value == true) {
3532 mouseProtocol = MouseProtocol.BUTTONEVENT;
3533 } else {
3534 mouseProtocol = MouseProtocol.OFF;
3535 }
3536 }
3537 break;
3538
3539 case 1003:
3540 if ((type == DeviceType.XTERM)
3541 && (decPrivateModeFlag == true)
3542 ) {
3543 // Mouse: Any-event tracking mode
3544 if (value == true) {
3545 mouseProtocol = MouseProtocol.ANYEVENT;
3546 } else {
3547 mouseProtocol = MouseProtocol.OFF;
3548 }
3549 }
3550 break;
3551
3552 case 1005:
3553 if ((type == DeviceType.XTERM)
3554 && (decPrivateModeFlag == true)
3555 ) {
3556 // Mouse: UTF-8 coordinates
3557 if (value == true) {
3558 mouseEncoding = MouseEncoding.UTF8;
3559 } else {
3560 mouseEncoding = MouseEncoding.X10;
3561 }
3562 }
3563 break;
3564
3565 case 1006:
3566 if ((type == DeviceType.XTERM)
3567 && (decPrivateModeFlag == true)
3568 ) {
3569 // Mouse: SGR coordinates
3570 if (value == true) {
3571 mouseEncoding = MouseEncoding.SGR;
3572 } else {
3573 mouseEncoding = MouseEncoding.X10;
3574 }
3575 }
3576 break;
3577
3578 case 1070:
3579 if (type == DeviceType.XTERM) {
3580 if (decPrivateModeFlag == true) {
3581 if (value == true) {
3582 // Use private color registers for each sixel
3583 // graphic (default).
3584 sixelPalette = null;
3585 } else {
3586 // Use shared color registers for each sixel
3587 // graphic.
3588 sixelPalette = new HashMap<Integer, java.awt.Color>();
3589 }
3590 }
3591 }
3592 break;
3593
3594 default:
3595 break;
3596
3597 }
3598 }
3599 }
3600
3601 /**
3602 * DECSC - Save cursor.
3603 */
3604 private void decsc() {
3605 savedState.setTo(currentState);
3606 }
3607
3608 /**
3609 * DECRC - Restore cursor.
3610 */
3611 private void decrc() {
3612 currentState.setTo(savedState);
3613 }
3614
3615 /**
3616 * IND - Index.
3617 */
3618 private void ind() {
3619 // Move the cursor and scroll if necessary. If at the bottom line
3620 // already, a scroll up is supposed to be performed.
3621 if (currentState.cursorY == scrollRegionBottom) {
3622 scrollingRegionScrollUp(scrollRegionTop, scrollRegionBottom, 1);
3623 }
3624 cursorDown(1, true);
3625 }
3626
3627 /**
3628 * RI - Reverse index.
3629 */
3630 private void ri() {
3631 // Move the cursor and scroll if necessary. If at the top line
3632 // already, a scroll down is supposed to be performed.
3633 if (currentState.cursorY == scrollRegionTop) {
3634 scrollingRegionScrollDown(scrollRegionTop, scrollRegionBottom, 1);
3635 }
3636 cursorUp(1, true);
3637 }
3638
3639 /**
3640 * NEL - Next line.
3641 */
3642 private void nel() {
3643 // Move the cursor and scroll if necessary. If at the bottom line
3644 // already, a scroll up is supposed to be performed.
3645 if (currentState.cursorY == scrollRegionBottom) {
3646 scrollingRegionScrollUp(scrollRegionTop, scrollRegionBottom, 1);
3647 }
3648 cursorDown(1, true);
3649
3650 // Reset to the beginning of the next line
3651 currentState.cursorX = 0;
3652 }
3653
3654 /**
3655 * DECKPAM - Keypad application mode.
3656 */
3657 private void deckpam() {
3658 keypadMode = KeypadMode.Application;
3659 }
3660
3661 /**
3662 * DECKPNM - Keypad numeric mode.
3663 */
3664 private void deckpnm() {
3665 keypadMode = KeypadMode.Numeric;
3666 }
3667
3668 /**
3669 * Move up n spaces.
3670 *
3671 * @param n number of spaces to move
3672 * @param honorScrollRegion if true, then do nothing if the cursor is
3673 * outside the scrolling region
3674 */
3675 private void cursorUp(final int n, final boolean honorScrollRegion) {
3676 int top;
3677
3678 /*
3679 * Special case: if a user moves the cursor from the right margin, we
3680 * have to reset the VT100 right margin flag.
3681 */
3682 if (n > 0) {
3683 wrapLineFlag = false;
3684 }
3685
3686 for (int i = 0; i < n; i++) {
3687 if (honorScrollRegion == true) {
3688 // Honor the scrolling region
3689 if ((currentState.cursorY < scrollRegionTop)
3690 || (currentState.cursorY > scrollRegionBottom)
3691 ) {
3692 // Outside region, do nothing
3693 return;
3694 }
3695 // Inside region, go up
3696 top = scrollRegionTop;
3697 } else {
3698 // Non-scrolling case
3699 top = 0;
3700 }
3701
3702 if (currentState.cursorY > top) {
3703 currentState.cursorY--;
3704 }
3705 }
3706 }
3707
3708 /**
3709 * Move down n spaces.
3710 *
3711 * @param n number of spaces to move
3712 * @param honorScrollRegion if true, then do nothing if the cursor is
3713 * outside the scrolling region
3714 */
3715 private void cursorDown(final int n, final boolean honorScrollRegion) {
3716 int bottom;
3717
3718 /*
3719 * Special case: if a user moves the cursor from the right margin, we
3720 * have to reset the VT100 right margin flag.
3721 */
3722 if (n > 0) {
3723 wrapLineFlag = false;
3724 }
3725
3726 for (int i = 0; i < n; i++) {
3727
3728 if (honorScrollRegion == true) {
3729 // Honor the scrolling region
3730 if (currentState.cursorY > scrollRegionBottom) {
3731 // Outside region, do nothing
3732 return;
3733 }
3734 // Inside region, go down
3735 bottom = scrollRegionBottom;
3736 } else {
3737 // Non-scrolling case
3738 bottom = height - 1;
3739 }
3740
3741 if (currentState.cursorY < bottom) {
3742 currentState.cursorY++;
3743 }
3744 }
3745 }
3746
3747 /**
3748 * Move left n spaces.
3749 *
3750 * @param n number of spaces to move
3751 * @param honorScrollRegion if true, then do nothing if the cursor is
3752 * outside the scrolling region
3753 */
3754 private void cursorLeft(final int n, final boolean honorScrollRegion) {
3755 /*
3756 * Special case: if a user moves the cursor from the right margin, we
3757 * have to reset the VT100 right margin flag.
3758 */
3759 if (n > 0) {
3760 wrapLineFlag = false;
3761 }
3762
3763 for (int i = 0; i < n; i++) {
3764 if (honorScrollRegion == true) {
3765 // Honor the scrolling region
3766 if ((currentState.cursorY < scrollRegionTop)
3767 || (currentState.cursorY > scrollRegionBottom)
3768 ) {
3769 // Outside region, do nothing
3770 return;
3771 }
3772 }
3773
3774 if (currentState.cursorX > 0) {
3775 currentState.cursorX--;
3776 }
3777 }
3778 }
3779
3780 /**
3781 * Move right n spaces.
3782 *
3783 * @param n number of spaces to move
3784 * @param honorScrollRegion if true, then do nothing if the cursor is
3785 * outside the scrolling region
3786 */
3787 private void cursorRight(final int n, final boolean honorScrollRegion) {
3788 int rightMargin = this.rightMargin;
3789
3790 /*
3791 * Special case: if a user moves the cursor from the right margin, we
3792 * have to reset the VT100 right margin flag.
3793 */
3794 if (n > 0) {
3795 wrapLineFlag = false;
3796 }
3797
3798 if (display.get(currentState.cursorY).isDoubleWidth()) {
3799 rightMargin = ((rightMargin + 1) / 2) - 1;
3800 }
3801
3802 for (int i = 0; i < n; i++) {
3803 if (honorScrollRegion == true) {
3804 // Honor the scrolling region
3805 if ((currentState.cursorY < scrollRegionTop)
3806 || (currentState.cursorY > scrollRegionBottom)
3807 ) {
3808 // Outside region, do nothing
3809 return;
3810 }
3811 }
3812
3813 if (currentState.cursorX < rightMargin) {
3814 currentState.cursorX++;
3815 }
3816 }
3817 }
3818
3819 /**
3820 * Move cursor to (col, row) where (0, 0) is the top-left corner.
3821 *
3822 * @param row row to move to
3823 * @param col column to move to
3824 */
3825 private void cursorPosition(int row, final int col) {
3826 int rightMargin = this.rightMargin;
3827
3828 assert (col >= 0);
3829 assert (row >= 0);
3830
3831 if (display.get(currentState.cursorY).isDoubleWidth()) {
3832 rightMargin = ((rightMargin + 1) / 2) - 1;
3833 }
3834
3835 // Set column number
3836 currentState.cursorX = col;
3837
3838 // Sanity check, bring column back to margin.
3839 if (currentState.cursorX > rightMargin) {
3840 currentState.cursorX = rightMargin;
3841 }
3842
3843 // Set row number
3844 if (currentState.originMode == true) {
3845 row += scrollRegionTop;
3846 }
3847 if (currentState.cursorY < row) {
3848 cursorDown(row - currentState.cursorY, false);
3849 } else if (currentState.cursorY > row) {
3850 cursorUp(currentState.cursorY - row, false);
3851 }
3852
3853 wrapLineFlag = false;
3854 }
3855
3856 /**
3857 * HTS - Horizontal tabulation set.
3858 */
3859 private void hts() {
3860 for (Integer stop: tabStops) {
3861 if (stop == currentState.cursorX) {
3862 // Already have a tab stop here
3863 return;
3864 }
3865 }
3866
3867 // Append a tab stop to the end of the array and resort them
3868 tabStops.add(currentState.cursorX);
3869 Collections.sort(tabStops);
3870 }
3871
3872 /**
3873 * DECSWL - Single-width line.
3874 */
3875 private void decswl() {
3876 display.get(currentState.cursorY).setDoubleWidth(false);
3877 display.get(currentState.cursorY).setDoubleHeight(0);
3878 }
3879
3880 /**
3881 * DECDWL - Double-width line.
3882 */
3883 private void decdwl() {
3884 display.get(currentState.cursorY).setDoubleWidth(true);
3885 display.get(currentState.cursorY).setDoubleHeight(0);
3886 }
3887
3888 /**
3889 * DECHDL - Double-height + double-width line.
3890 *
3891 * @param topHalf if true, this sets the row to be the top half row of a
3892 * double-height row
3893 */
3894 private void dechdl(final boolean topHalf) {
3895 display.get(currentState.cursorY).setDoubleWidth(true);
3896 if (topHalf == true) {
3897 display.get(currentState.cursorY).setDoubleHeight(1);
3898 } else {
3899 display.get(currentState.cursorY).setDoubleHeight(2);
3900 }
3901 }
3902
3903 /**
3904 * DECALN - Screen alignment display.
3905 */
3906 private void decaln() {
3907 Cell newCell = new Cell('E');
3908 for (DisplayLine line: display) {
3909 for (int i = 0; i < line.length(); i++) {
3910 line.replace(i, newCell);
3911 }
3912 }
3913 }
3914
3915 /**
3916 * DECSCL - Compatibility level.
3917 */
3918 private void decscl() {
3919 int i = getCsiParam(0, 0);
3920 int j = getCsiParam(1, 0);
3921
3922 if (i == 61) {
3923 // Reset fonts
3924 currentState.g0Charset = CharacterSet.US;
3925 currentState.g1Charset = CharacterSet.DRAWING;
3926 s8c1t = false;
3927 } else if (i == 62) {
3928
3929 if ((j == 0) || (j == 2)) {
3930 s8c1t = true;
3931 } else if (j == 1) {
3932 s8c1t = false;
3933 }
3934 }
3935 }
3936
3937 /**
3938 * CUD - Cursor down.
3939 */
3940 private void cud() {
3941 cursorDown(getCsiParam(0, 1, 1, height), true);
3942 }
3943
3944 /**
3945 * CUF - Cursor forward.
3946 */
3947 private void cuf() {
3948 cursorRight(getCsiParam(0, 1, 1, rightMargin + 1), true);
3949 }
3950
3951 /**
3952 * CUB - Cursor backward.
3953 */
3954 private void cub() {
3955 cursorLeft(getCsiParam(0, 1, 1, currentState.cursorX + 1), true);
3956 }
3957
3958 /**
3959 * CUU - Cursor up.
3960 */
3961 private void cuu() {
3962 cursorUp(getCsiParam(0, 1, 1, currentState.cursorY + 1), true);
3963 }
3964
3965 /**
3966 * CUP - Cursor position.
3967 */
3968 private void cup() {
3969 cursorPosition(getCsiParam(0, 1, 1, height) - 1,
3970 getCsiParam(1, 1, 1, rightMargin + 1) - 1);
3971 }
3972
3973 /**
3974 * CNL - Cursor down and to column 1.
3975 */
3976 private void cnl() {
3977 cursorDown(getCsiParam(0, 1, 1, height), true);
3978 // To column 0
3979 cursorLeft(currentState.cursorX, true);
3980 }
3981
3982 /**
3983 * CPL - Cursor up and to column 1.
3984 */
3985 private void cpl() {
3986 cursorUp(getCsiParam(0, 1, 1, currentState.cursorY + 1), true);
3987 // To column 0
3988 cursorLeft(currentState.cursorX, true);
3989 }
3990
3991 /**
3992 * CHA - Cursor to column # in current row.
3993 */
3994 private void cha() {
3995 cursorPosition(currentState.cursorY,
3996 getCsiParam(0, 1, 1, rightMargin + 1) - 1);
3997 }
3998
3999 /**
4000 * VPA - Cursor to row #, same column.
4001 */
4002 private void vpa() {
4003 cursorPosition(getCsiParam(0, 1, 1, height) - 1,
4004 currentState.cursorX);
4005 }
4006
4007 /**
4008 * ED - Erase in display.
4009 */
4010 private void ed() {
4011 boolean honorProtected = false;
4012 boolean decPrivateModeFlag = false;
4013
4014 for (int i = 0; i < collectBuffer.length(); i++) {
4015 if (collectBuffer.charAt(i) == '?') {
4016 decPrivateModeFlag = true;
4017 break;
4018 }
4019 }
4020
4021 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
4022 && (decPrivateModeFlag == true)
4023 ) {
4024 honorProtected = true;
4025 }
4026
4027 int i = getCsiParam(0, 0);
4028
4029 if (i == 0) {
4030 // Erase from here to end of screen
4031 if (currentState.cursorY < height - 1) {
4032 eraseScreen(currentState.cursorY + 1, 0, height - 1, width - 1,
4033 honorProtected);
4034 }
4035 eraseLine(currentState.cursorX, width - 1, honorProtected);
4036 } else if (i == 1) {
4037 // Erase from beginning of screen to here
4038 eraseScreen(0, 0, currentState.cursorY - 1, width - 1,
4039 honorProtected);
4040 eraseLine(0, currentState.cursorX, honorProtected);
4041 } else if (i == 2) {
4042 // Erase entire screen
4043 eraseScreen(0, 0, height - 1, width - 1, honorProtected);
4044 }
4045 }
4046
4047 /**
4048 * EL - Erase in line.
4049 */
4050 private void el() {
4051 boolean honorProtected = false;
4052 boolean decPrivateModeFlag = false;
4053
4054 for (int i = 0; i < collectBuffer.length(); i++) {
4055 if (collectBuffer.charAt(i) == '?') {
4056 decPrivateModeFlag = true;
4057 break;
4058 }
4059 }
4060
4061 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
4062 && (decPrivateModeFlag == true)
4063 ) {
4064 honorProtected = true;
4065 }
4066
4067 int i = getCsiParam(0, 0);
4068
4069 if (i == 0) {
4070 // Erase from here to end of line
4071 eraseLine(currentState.cursorX, width - 1, honorProtected);
4072 } else if (i == 1) {
4073 // Erase from beginning of line to here
4074 eraseLine(0, currentState.cursorX, honorProtected);
4075 } else if (i == 2) {
4076 // Erase entire line
4077 eraseLine(0, width - 1, honorProtected);
4078 }
4079 }
4080
4081 /**
4082 * ECH - Erase # of characters in current row.
4083 */
4084 private void ech() {
4085 int i = getCsiParam(0, 1, 1, width);
4086
4087 // Erase from here to i characters
4088 eraseLine(currentState.cursorX, currentState.cursorX + i - 1, false);
4089 }
4090
4091 /**
4092 * IL - Insert line.
4093 */
4094 private void il() {
4095 int i = getCsiParam(0, 1);
4096
4097 if ((currentState.cursorY >= scrollRegionTop)
4098 && (currentState.cursorY <= scrollRegionBottom)
4099 ) {
4100
4101 // I can get the same effect with a scroll-down
4102 scrollingRegionScrollDown(currentState.cursorY,
4103 scrollRegionBottom, i);
4104 }
4105 }
4106
4107 /**
4108 * DCH - Delete char.
4109 */
4110 private void dch() {
4111 int n = getCsiParam(0, 1);
4112 DisplayLine line = display.get(currentState.cursorY);
4113 Cell blank = new Cell();
4114 for (int i = 0; i < n; i++) {
4115 line.delete(currentState.cursorX, blank);
4116 }
4117 }
4118
4119 /**
4120 * ICH - Insert blank char at cursor.
4121 */
4122 private void ich() {
4123 int n = getCsiParam(0, 1);
4124 DisplayLine line = display.get(currentState.cursorY);
4125 Cell blank = new Cell();
4126 for (int i = 0; i < n; i++) {
4127 line.insert(currentState.cursorX, blank);
4128 }
4129 }
4130
4131 /**
4132 * DL - Delete line.
4133 */
4134 private void dl() {
4135 int i = getCsiParam(0, 1);
4136
4137 if ((currentState.cursorY >= scrollRegionTop)
4138 && (currentState.cursorY <= scrollRegionBottom)) {
4139
4140 // I can get the same effect with a scroll-down
4141 scrollingRegionScrollUp(currentState.cursorY,
4142 scrollRegionBottom, i);
4143 }
4144 }
4145
4146 /**
4147 * HVP - Horizontal and vertical position.
4148 */
4149 private void hvp() {
4150 cup();
4151 }
4152
4153 /**
4154 * REP - Repeat character.
4155 */
4156 private void rep() {
4157 int n = getCsiParam(0, 1);
4158 for (int i = 0; i < n; i++) {
4159 printCharacter(repCh);
4160 }
4161 }
4162
4163 /**
4164 * SU - Scroll up.
4165 */
4166 private void su() {
4167 scrollingRegionScrollUp(scrollRegionTop, scrollRegionBottom,
4168 getCsiParam(0, 1, 1, height));
4169 }
4170
4171 /**
4172 * SD - Scroll down.
4173 */
4174 private void sd() {
4175 scrollingRegionScrollDown(scrollRegionTop, scrollRegionBottom,
4176 getCsiParam(0, 1, 1, height));
4177 }
4178
4179 /**
4180 * CBT - Go back X tab stops.
4181 */
4182 private void cbt() {
4183 int tabsToMove = getCsiParam(0, 1);
4184 int tabI;
4185
4186 for (int i = 0; i < tabsToMove; i++) {
4187 int j = currentState.cursorX;
4188 for (tabI = 0; tabI < tabStops.size(); tabI++) {
4189 if (tabStops.get(tabI) >= currentState.cursorX) {
4190 break;
4191 }
4192 }
4193 tabI--;
4194 if (tabI <= 0) {
4195 j = 0;
4196 } else {
4197 j = tabStops.get(tabI);
4198 }
4199 cursorPosition(currentState.cursorY, j);
4200 }
4201 }
4202
4203 /**
4204 * CHT - Advance X tab stops.
4205 */
4206 private void cht() {
4207 int n = getCsiParam(0, 1);
4208 for (int i = 0; i < n; i++) {
4209 advanceToNextTabStop();
4210 }
4211 }
4212
4213 /**
4214 * SGR - Select graphics rendition.
4215 */
4216 private void sgr() {
4217
4218 if (csiParams.size() == 0) {
4219 currentState.attr.reset();
4220 return;
4221 }
4222
4223 int sgrColorMode = -1;
4224 boolean idx88Color = false;
4225 boolean rgbColor = false;
4226 int rgbRed = -1;
4227 int rgbGreen = -1;
4228
4229 for (Integer i: csiParams) {
4230
4231 if ((sgrColorMode == 38) || (sgrColorMode == 48)) {
4232
4233 assert (type == DeviceType.XTERM);
4234
4235 if (idx88Color) {
4236 /*
4237 * Indexed color mode, we now have the index number.
4238 */
4239 if (sgrColorMode == 38) {
4240 currentState.attr.setForeColorRGB(get88Color(i));
4241 } else {
4242 assert (sgrColorMode == 48);
4243 currentState.attr.setBackColorRGB(get88Color(i));
4244 }
4245 sgrColorMode = -1;
4246 idx88Color = false;
4247 continue;
4248 }
4249
4250 if (rgbColor) {
4251 /*
4252 * RGB color mode, we are collecting tokens.
4253 */
4254 if (rgbRed == -1) {
4255 rgbRed = i & 0xFF;
4256 } else if (rgbGreen == -1) {
4257 rgbGreen = i & 0xFF;
4258 } else {
4259 int rgb = rgbRed << 16;
4260 rgb |= rgbGreen << 8;
4261 rgb |= i & 0xFF;
4262
4263 // System.err.printf("RGB: %08x\n", rgb);
4264
4265 if (sgrColorMode == 38) {
4266 currentState.attr.setForeColorRGB(rgb);
4267 } else {
4268 assert (sgrColorMode == 48);
4269 currentState.attr.setBackColorRGB(rgb);
4270 }
4271 rgbRed = -1;
4272 rgbGreen = -1;
4273 sgrColorMode = -1;
4274 rgbColor = false;
4275 }
4276 continue;
4277 }
4278
4279 switch (i) {
4280
4281 case 2:
4282 /*
4283 * RGB color mode.
4284 */
4285 rgbColor = true;
4286 continue;
4287
4288 case 5:
4289 /*
4290 * Indexed color mode.
4291 */
4292 idx88Color = true;
4293 continue;
4294
4295 default:
4296 /*
4297 * This is neither indexed nor RGB color. Bail out.
4298 */
4299 return;
4300 }
4301
4302 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
4303
4304 switch (i) {
4305
4306 case 0:
4307 // Normal
4308 currentState.attr.reset();
4309 break;
4310
4311 case 1:
4312 // Bold
4313 currentState.attr.setBold(true);
4314 break;
4315
4316 case 4:
4317 // Underline
4318 currentState.attr.setUnderline(true);
4319 break;
4320
4321 case 5:
4322 // Blink
4323 currentState.attr.setBlink(true);
4324 break;
4325
4326 case 7:
4327 // Reverse
4328 currentState.attr.setReverse(true);
4329 break;
4330
4331 default:
4332 break;
4333 }
4334
4335 if (type == DeviceType.XTERM) {
4336
4337 switch (i) {
4338
4339 case 8:
4340 // Invisible
4341 // Not supported
4342 break;
4343
4344 case 90:
4345 // Set black foreground
4346 currentState.attr.setForeColorRGB(get88Color(8));
4347 break;
4348 case 91:
4349 // Set red foreground
4350 currentState.attr.setForeColorRGB(get88Color(9));
4351 break;
4352 case 92:
4353 // Set green foreground
4354 currentState.attr.setForeColorRGB(get88Color(10));
4355 break;
4356 case 93:
4357 // Set yellow foreground
4358 currentState.attr.setForeColorRGB(get88Color(11));
4359 break;
4360 case 94:
4361 // Set blue foreground
4362 currentState.attr.setForeColorRGB(get88Color(12));
4363 break;
4364 case 95:
4365 // Set magenta foreground
4366 currentState.attr.setForeColorRGB(get88Color(13));
4367 break;
4368 case 96:
4369 // Set cyan foreground
4370 currentState.attr.setForeColorRGB(get88Color(14));
4371 break;
4372 case 97:
4373 // Set white foreground
4374 currentState.attr.setForeColorRGB(get88Color(15));
4375 break;
4376
4377 case 100:
4378 // Set black background
4379 currentState.attr.setBackColorRGB(get88Color(8));
4380 break;
4381 case 101:
4382 // Set red background
4383 currentState.attr.setBackColorRGB(get88Color(9));
4384 break;
4385 case 102:
4386 // Set green background
4387 currentState.attr.setBackColorRGB(get88Color(10));
4388 break;
4389 case 103:
4390 // Set yellow background
4391 currentState.attr.setBackColorRGB(get88Color(11));
4392 break;
4393 case 104:
4394 // Set blue background
4395 currentState.attr.setBackColorRGB(get88Color(12));
4396 break;
4397 case 105:
4398 // Set magenta background
4399 currentState.attr.setBackColorRGB(get88Color(13));
4400 break;
4401 case 106:
4402 // Set cyan background
4403 currentState.attr.setBackColorRGB(get88Color(14));
4404 break;
4405 case 107:
4406 // Set white background
4407 currentState.attr.setBackColorRGB(get88Color(15));
4408 break;
4409
4410 default:
4411 break;
4412 }
4413 }
4414
4415 if ((type == DeviceType.VT220)
4416 || (type == DeviceType.XTERM)) {
4417
4418 switch (i) {
4419
4420 case 22:
4421 // Normal intensity
4422 currentState.attr.setBold(false);
4423 break;
4424
4425 case 24:
4426 // No underline
4427 currentState.attr.setUnderline(false);
4428 break;
4429
4430 case 25:
4431 // No blink
4432 currentState.attr.setBlink(false);
4433 break;
4434
4435 case 27:
4436 // Un-reverse
4437 currentState.attr.setReverse(false);
4438 break;
4439
4440 default:
4441 break;
4442 }
4443 }
4444
4445 // A true VT100/102/220 does not support color, however everyone
4446 // is used to their terminal emulator supporting color so we will
4447 // unconditionally support color for all DeviceType's.
4448
4449 switch (i) {
4450
4451 case 30:
4452 // Set black foreground
4453 currentState.attr.setForeColor(Color.BLACK);
4454 currentState.attr.setForeColorRGB(-1);
4455 break;
4456 case 31:
4457 // Set red foreground
4458 currentState.attr.setForeColor(Color.RED);
4459 currentState.attr.setForeColorRGB(-1);
4460 break;
4461 case 32:
4462 // Set green foreground
4463 currentState.attr.setForeColor(Color.GREEN);
4464 currentState.attr.setForeColorRGB(-1);
4465 break;
4466 case 33:
4467 // Set yellow foreground
4468 currentState.attr.setForeColor(Color.YELLOW);
4469 currentState.attr.setForeColorRGB(-1);
4470 break;
4471 case 34:
4472 // Set blue foreground
4473 currentState.attr.setForeColor(Color.BLUE);
4474 currentState.attr.setForeColorRGB(-1);
4475 break;
4476 case 35:
4477 // Set magenta foreground
4478 currentState.attr.setForeColor(Color.MAGENTA);
4479 currentState.attr.setForeColorRGB(-1);
4480 break;
4481 case 36:
4482 // Set cyan foreground
4483 currentState.attr.setForeColor(Color.CYAN);
4484 currentState.attr.setForeColorRGB(-1);
4485 break;
4486 case 37:
4487 // Set white foreground
4488 currentState.attr.setForeColor(Color.WHITE);
4489 currentState.attr.setForeColorRGB(-1);
4490 break;
4491 case 38:
4492 if (type == DeviceType.XTERM) {
4493 /*
4494 * Xterm supports T.416 / ISO-8613-3 codes to select
4495 * either an indexed color or an RGB value. (It also
4496 * permits these ISO-8613-3 SGR sequences to be separated
4497 * by colons rather than semicolons.)
4498 *
4499 * We will support only the following:
4500 *
4501 * 1. Indexed color mode (88- or 256-color modes).
4502 *
4503 * 2. Direct RGB.
4504 *
4505 * These cover most of the use cases in the real world.
4506 *
4507 * HOWEVER, note that this is an awful broken "standard",
4508 * with no way to do it "right". See
4509 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4510 * for a detailed discussion of the current state of RGB
4511 * in various terminals, the point of which is that none
4512 * of them really do the same thing despite all appearing
4513 * to be "xterm".
4514 *
4515 * Also see
4516 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4517 * where it is assumed that supporting just the "indexed
4518 * mode" of these sequences (which could align easily
4519 * with existing SGR colors) is assumed to mean full
4520 * support of 24-bit RGB. So it is all or nothing.
4521 *
4522 * Finally, these sequences break the assumptions of
4523 * standard ECMA-48 style parsers as pointed out at
4524 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4525 * Therefore in order to keep a clean display, we cannot
4526 * parse anything else in this sequence.
4527 */
4528 sgrColorMode = 38;
4529 continue;
4530 } else {
4531 // Underscore on, default foreground color
4532 currentState.attr.setUnderline(true);
4533 currentState.attr.setForeColor(Color.WHITE);
4534 }
4535 break;
4536 case 39:
4537 // Underscore off, default foreground color
4538 currentState.attr.setUnderline(false);
4539 currentState.attr.setForeColor(Color.WHITE);
4540 currentState.attr.setForeColorRGB(-1);
4541 break;
4542 case 40:
4543 // Set black background
4544 currentState.attr.setBackColor(Color.BLACK);
4545 currentState.attr.setBackColorRGB(-1);
4546 break;
4547 case 41:
4548 // Set red background
4549 currentState.attr.setBackColor(Color.RED);
4550 currentState.attr.setBackColorRGB(-1);
4551 break;
4552 case 42:
4553 // Set green background
4554 currentState.attr.setBackColor(Color.GREEN);
4555 currentState.attr.setBackColorRGB(-1);
4556 break;
4557 case 43:
4558 // Set yellow background
4559 currentState.attr.setBackColor(Color.YELLOW);
4560 currentState.attr.setBackColorRGB(-1);
4561 break;
4562 case 44:
4563 // Set blue background
4564 currentState.attr.setBackColor(Color.BLUE);
4565 currentState.attr.setBackColorRGB(-1);
4566 break;
4567 case 45:
4568 // Set magenta background
4569 currentState.attr.setBackColor(Color.MAGENTA);
4570 currentState.attr.setBackColorRGB(-1);
4571 break;
4572 case 46:
4573 // Set cyan background
4574 currentState.attr.setBackColor(Color.CYAN);
4575 currentState.attr.setBackColorRGB(-1);
4576 break;
4577 case 47:
4578 // Set white background
4579 currentState.attr.setBackColor(Color.WHITE);
4580 currentState.attr.setBackColorRGB(-1);
4581 break;
4582 case 48:
4583 if (type == DeviceType.XTERM) {
4584 /*
4585 * Xterm supports T.416 / ISO-8613-3 codes to select
4586 * either an indexed color or an RGB value. (It also
4587 * permits these ISO-8613-3 SGR sequences to be separated
4588 * by colons rather than semicolons.)
4589 *
4590 * We will support only the following:
4591 *
4592 * 1. Indexed color mode (88- or 256-color modes).
4593 *
4594 * 2. Direct RGB.
4595 *
4596 * These cover most of the use cases in the real world.
4597 *
4598 * HOWEVER, note that this is an awful broken "standard",
4599 * with no way to do it "right". See
4600 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4601 * for a detailed discussion of the current state of RGB
4602 * in various terminals, the point of which is that none
4603 * of them really do the same thing despite all appearing
4604 * to be "xterm".
4605 *
4606 * Also see
4607 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4608 * where it is assumed that supporting just the "indexed
4609 * mode" of these sequences (which could align easily
4610 * with existing SGR colors) is assumed to mean full
4611 * support of 24-bit RGB. So it is all or nothing.
4612 *
4613 * Finally, these sequences break the assumptions of
4614 * standard ECMA-48 style parsers as pointed out at
4615 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4616 * Therefore in order to keep a clean display, we cannot
4617 * parse anything else in this sequence.
4618 */
4619 sgrColorMode = 48;
4620 continue;
4621 }
4622 break;
4623 case 49:
4624 // Default background
4625 currentState.attr.setBackColor(Color.BLACK);
4626 currentState.attr.setBackColorRGB(-1);
4627 break;
4628
4629 default:
4630 break;
4631 }
4632 }
4633 }
4634
4635 /**
4636 * DA - Device attributes.
4637 */
4638 private void da() {
4639 int extendedFlag = 0;
4640 int i = 0;
4641 if (collectBuffer.length() > 0) {
4642 String args = collectBuffer.substring(1);
4643 if (collectBuffer.charAt(0) == '>') {
4644 extendedFlag = 1;
4645 if (collectBuffer.length() >= 2) {
4646 i = Integer.parseInt(args);
4647 }
4648 } else if (collectBuffer.charAt(0) == '=') {
4649 extendedFlag = 2;
4650 if (collectBuffer.length() >= 2) {
4651 i = Integer.parseInt(args);
4652 }
4653 } else {
4654 // Unknown code, bail out
4655 return;
4656 }
4657 }
4658
4659 if ((i != 0) && (i != 1)) {
4660 return;
4661 }
4662
4663 if ((extendedFlag == 0) && (i == 0)) {
4664 // Send string directly to remote side
4665 writeRemote(deviceTypeResponse());
4666 return;
4667 }
4668
4669 if ((type == DeviceType.VT220) || (type == DeviceType.XTERM)) {
4670
4671 if ((extendedFlag == 1) && (i == 0)) {
4672 /*
4673 * Request "What type of terminal are you, what is your
4674 * firmware version, and what hardware options do you have
4675 * installed?"
4676 *
4677 * Respond: "I am a VT220 (identification code of 1), my
4678 * firmware version is _____ (Pv), and I have _____ Po
4679 * options installed."
4680 *
4681 * (Same as xterm)
4682 *
4683 */
4684
4685 if (s8c1t == true) {
4686 writeRemote("\u009b>1;10;0c");
4687 } else {
4688 writeRemote("\033[>1;10;0c");
4689 }
4690 }
4691 }
4692
4693 // VT420 and up
4694 if ((extendedFlag == 2) && (i == 0)) {
4695
4696 /*
4697 * Request "What is your unit ID?"
4698 *
4699 * Respond: "I was manufactured at site 00 and have a unique ID
4700 * number of 123."
4701 *
4702 */
4703 writeRemote("\033P!|00010203\033\\");
4704 }
4705 }
4706
4707 /**
4708 * DECSTBM - Set top and bottom margins.
4709 */
4710 private void decstbm() {
4711 boolean decPrivateModeFlag = false;
4712
4713 for (int i = 0; i < collectBuffer.length(); i++) {
4714 if (collectBuffer.charAt(i) == '?') {
4715 decPrivateModeFlag = true;
4716 break;
4717 }
4718 }
4719 if (decPrivateModeFlag) {
4720 // This could be restore DEC private mode values.
4721 // Ignore it.
4722 } else {
4723 // DECSTBM
4724 int top = getCsiParam(0, 1, 1, height) - 1;
4725 int bottom = getCsiParam(1, height, 1, height) - 1;
4726 if (bottom > height - 1) {
4727 bottom = height - 1;
4728 }
4729
4730 if (top > bottom) {
4731 top = bottom;
4732 }
4733 scrollRegionTop = top;
4734 scrollRegionBottom = bottom;
4735
4736 // Home cursor
4737 cursorPosition(0, 0);
4738 }
4739 }
4740
4741 /**
4742 * DECREQTPARM - Request terminal parameters.
4743 */
4744 private void decreqtparm() {
4745 int i = getCsiParam(0, 0);
4746
4747 if ((i != 0) && (i != 1)) {
4748 return;
4749 }
4750
4751 String str = "";
4752
4753 /*
4754 * Request terminal parameters.
4755 *
4756 * Respond with:
4757 *
4758 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4759 * (CLoCk MULtiplier = 1, STP option flags = 0)
4760 *
4761 * (Same as xterm)
4762 */
4763 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
4764 && (s8c1t == true)
4765 ) {
4766 str = String.format("\u009b%d;1;1;128;128;1;0x", i + 2);
4767 } else {
4768 str = String.format("\033[%d;1;1;128;128;1;0x", i + 2);
4769 }
4770 writeRemote(str);
4771 }
4772
4773 /**
4774 * DECSCA - Select Character Attributes.
4775 */
4776 private void decsca() {
4777 int i = getCsiParam(0, 0);
4778
4779 if ((i == 0) || (i == 2)) {
4780 // Protect mode OFF
4781 currentState.attr.setProtect(false);
4782 }
4783 if (i == 1) {
4784 // Protect mode ON
4785 currentState.attr.setProtect(true);
4786 }
4787 }
4788
4789 /**
4790 * DECSTR - Soft Terminal Reset.
4791 */
4792 private void decstr() {
4793 // Do exactly like RIS - Reset to initial state
4794 reset();
4795 // Do I clear screen too? I think so...
4796 eraseScreen(0, 0, height - 1, width - 1, false);
4797 cursorPosition(0, 0);
4798 }
4799
4800 /**
4801 * DSR - Device status report.
4802 */
4803 private void dsr() {
4804 boolean decPrivateModeFlag = false;
4805 int row = currentState.cursorY;
4806
4807 for (int i = 0; i < collectBuffer.length(); i++) {
4808 if (collectBuffer.charAt(i) == '?') {
4809 decPrivateModeFlag = true;
4810 break;
4811 }
4812 }
4813
4814 int i = getCsiParam(0, 0);
4815
4816 switch (i) {
4817
4818 case 5:
4819 // Request status report. Respond with "OK, no malfunction."
4820
4821 // Send string directly to remote side
4822 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
4823 && (s8c1t == true)
4824 ) {
4825 writeRemote("\u009b0n");
4826 } else {
4827 writeRemote("\033[0n");
4828 }
4829 break;
4830
4831 case 6:
4832 // Request cursor position. Respond with current position.
4833 if (currentState.originMode == true) {
4834 row -= scrollRegionTop;
4835 }
4836 String str = "";
4837 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
4838 && (s8c1t == true)
4839 ) {
4840 str = String.format("\u009b%d;%dR", row + 1,
4841 currentState.cursorX + 1);
4842 } else {
4843 str = String.format("\033[%d;%dR", row + 1,
4844 currentState.cursorX + 1);
4845 }
4846
4847 // Send string directly to remote side
4848 writeRemote(str);
4849 break;
4850
4851 case 15:
4852 if (decPrivateModeFlag == true) {
4853
4854 // Request printer status report. Respond with "Printer not
4855 // connected."
4856
4857 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
4858 && (s8c1t == true)) {
4859 writeRemote("\u009b?13n");
4860 } else {
4861 writeRemote("\033[?13n");
4862 }
4863 }
4864 break;
4865
4866 case 25:
4867 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
4868 && (decPrivateModeFlag == true)
4869 ) {
4870
4871 // Request user-defined keys are locked or unlocked. Respond
4872 // with "User-defined keys are locked."
4873
4874 if (s8c1t == true) {
4875 writeRemote("\u009b?21n");
4876 } else {
4877 writeRemote("\033[?21n");
4878 }
4879 }
4880 break;
4881
4882 case 26:
4883 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
4884 && (decPrivateModeFlag == true)
4885 ) {
4886
4887 // Request keyboard language. Respond with "Keyboard
4888 // language is North American."
4889
4890 if (s8c1t == true) {
4891 writeRemote("\u009b?27;1n");
4892 } else {
4893 writeRemote("\033[?27;1n");
4894 }
4895
4896 }
4897 break;
4898
4899 default:
4900 // Some other option, ignore
4901 break;
4902 }
4903 }
4904
4905 /**
4906 * TBC - Tabulation clear.
4907 */
4908 private void tbc() {
4909 int i = getCsiParam(0, 0);
4910 if (i == 0) {
4911 List<Integer> newStops = new ArrayList<Integer>();
4912 for (Integer stop: tabStops) {
4913 if (stop == currentState.cursorX) {
4914 continue;
4915 }
4916 newStops.add(stop);
4917 }
4918 tabStops = newStops;
4919 }
4920 if (i == 3) {
4921 tabStops.clear();
4922 }
4923 }
4924
4925 /**
4926 * Erase the characters in the current line from the start column to the
4927 * end column, inclusive.
4928 *
4929 * @param start starting column to erase (between 0 and width - 1)
4930 * @param end ending column to erase (between 0 and width - 1)
4931 * @param honorProtected if true, do not erase characters with the
4932 * protected attribute set
4933 */
4934 private void eraseLine(int start, int end, final boolean honorProtected) {
4935
4936 if (start > end) {
4937 return;
4938 }
4939 if (end > width - 1) {
4940 end = width - 1;
4941 }
4942 if (start < 0) {
4943 start = 0;
4944 }
4945
4946 for (int i = start; i <= end; i++) {
4947 DisplayLine line = display.get(currentState.cursorY);
4948 if ((!honorProtected)
4949 || ((honorProtected) && (!line.charAt(i).isProtect()))) {
4950
4951 switch (type) {
4952 case VT100:
4953 case VT102:
4954 case VT220:
4955 /*
4956 * From the VT102 manual:
4957 *
4958 * Erasing a character also erases any character
4959 * attribute of the character.
4960 */
4961 line.setBlank(i);
4962 break;
4963 case XTERM:
4964 /*
4965 * Erase with the current color a.k.a. back-color erase
4966 * (bce).
4967 */
4968 line.setChar(i, ' ');
4969 line.setAttr(i, currentState.attr);
4970 break;
4971 }
4972 }
4973 }
4974 }
4975
4976 /**
4977 * Erase a rectangular section of the screen, inclusive. end column,
4978 * inclusive.
4979 *
4980 * @param startRow starting row to erase (between 0 and height - 1)
4981 * @param startCol starting column to erase (between 0 and width - 1)
4982 * @param endRow ending row to erase (between 0 and height - 1)
4983 * @param endCol ending column to erase (between 0 and width - 1)
4984 * @param honorProtected if true, do not erase characters with the
4985 * protected attribute set
4986 */
4987 private void eraseScreen(final int startRow, final int startCol,
4988 final int endRow, final int endCol, final boolean honorProtected) {
4989
4990 int oldCursorY;
4991
4992 if ((startRow < 0)
4993 || (startCol < 0)
4994 || (endRow < 0)
4995 || (endCol < 0)
4996 || (endRow < startRow)
4997 || (endCol < startCol)
4998 ) {
4999 return;
5000 }
5001
5002 oldCursorY = currentState.cursorY;
5003 for (int i = startRow; i <= endRow; i++) {
5004 currentState.cursorY = i;
5005 eraseLine(startCol, endCol, honorProtected);
5006
5007 // Erase display clears the double attributes
5008 display.get(i).setDoubleWidth(false);
5009 display.get(i).setDoubleHeight(0);
5010 }
5011 currentState.cursorY = oldCursorY;
5012 }
5013
5014 /**
5015 * VT220 printer functions. All of these are parsed, but won't do
5016 * anything.
5017 */
5018 private void printerFunctions() {
5019 boolean decPrivateModeFlag = false;
5020 for (int i = 0; i < collectBuffer.length(); i++) {
5021 if (collectBuffer.charAt(i) == '?') {
5022 decPrivateModeFlag = true;
5023 break;
5024 }
5025 }
5026
5027 int i = getCsiParam(0, 0);
5028
5029 switch (i) {
5030
5031 case 0:
5032 if (decPrivateModeFlag == false) {
5033 // Print screen
5034 }
5035 break;
5036
5037 case 1:
5038 if (decPrivateModeFlag == true) {
5039 // Print cursor line
5040 }
5041 break;
5042
5043 case 4:
5044 if (decPrivateModeFlag == true) {
5045 // Auto print mode OFF
5046 } else {
5047 // Printer controller OFF
5048
5049 // Characters re-appear on the screen
5050 printerControllerMode = false;
5051 }
5052 break;
5053
5054 case 5:
5055 if (decPrivateModeFlag == true) {
5056 // Auto print mode
5057
5058 } else {
5059 // Printer controller
5060
5061 // Characters get sucked into oblivion
5062 printerControllerMode = true;
5063 }
5064 break;
5065
5066 default:
5067 break;
5068
5069 }
5070 }
5071
5072 /**
5073 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
5074 * of remote systems will send an XTerm title sequence even if TERM isn't
5075 * xterm.
5076 *
5077 * @param xtermChar the character received from the remote side
5078 */
5079 private void oscPut(final char xtermChar) {
5080 // System.err.println("oscPut: " + xtermChar);
5081
5082 boolean oscEnd = false;
5083
5084 if (xtermChar == 0x07) {
5085 oscEnd = true;
5086 }
5087 if ((xtermChar == '\\')
5088 && (collectBuffer.charAt(collectBuffer.length() - 1) == '\033')
5089 ) {
5090 oscEnd = true;
5091 }
5092
5093 // Collect first
5094 collectBuffer.append(xtermChar);
5095
5096 // Xterm cases...
5097 if (oscEnd) {
5098 String args = null;
5099 if (xtermChar == 0x07) {
5100 args = collectBuffer.substring(0, collectBuffer.length() - 1);
5101 } else {
5102 args = collectBuffer.substring(0, collectBuffer.length() - 2);
5103 }
5104
5105 String [] p = args.split(";");
5106 if (p.length > 0) {
5107 if ((p[0].equals("0")) || (p[0].equals("2"))) {
5108 if (p.length > 1) {
5109 // Screen title
5110 screenTitle = p[1];
5111 }
5112 }
5113
5114 if (p[0].equals("4")) {
5115 for (int i = 1; i + 1 < p.length; i += 2) {
5116 // Set a color index value
5117 try {
5118 set88Color(Integer.parseInt(p[i]), p[i + 1]);
5119 } catch (NumberFormatException e) {
5120 // SQUASH
5121 }
5122 }
5123 }
5124
5125 if (p[0].equals("10")) {
5126 if (p[1].equals("?")) {
5127 // Respond with foreground color.
5128 java.awt.Color color = jexer.backend.SwingTerminal.attrToForegroundColor(currentState.attr);
5129
5130 writeRemote(String.format(
5131 "\033]10;rgb:%04x/%04x/%04x\033\\",
5132 color.getRed() << 8,
5133 color.getGreen() << 8,
5134 color.getBlue() << 8));
5135 }
5136 }
5137
5138 if (p[0].equals("11")) {
5139 if (p[1].equals("?")) {
5140 // Respond with background color.
5141 java.awt.Color color = jexer.backend.SwingTerminal.attrToBackgroundColor(currentState.attr);
5142
5143 writeRemote(String.format(
5144 "\033]11;rgb:%04x/%04x/%04x\033\\",
5145 color.getRed() << 8,
5146 color.getGreen() << 8,
5147 color.getBlue() << 8));
5148 }
5149 }
5150
5151 if (p[0].equals("444")) {
5152 if (p[1].equals("0") && (p.length == 6)) {
5153 // Jexer image - RGB
5154 parseJexerImageRGB(p[2], p[3], p[4], p[5]);
5155 } else if (p[1].equals("1") && (p.length == 4)) {
5156 // Jexer image - PNG
5157 parseJexerImageFile(1, p[2], p[3]);
5158 } else if (p[1].equals("2") && (p.length == 4)) {
5159 // Jexer image - JPG
5160 parseJexerImageFile(2, p[2], p[3]);
5161 }
5162 }
5163 }
5164
5165 // Go to SCAN_GROUND state
5166 toGround();
5167 return;
5168 }
5169 }
5170
5171 /**
5172 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
5173 * Jexer ECMA48Terminal to talk to ECMA48.
5174 *
5175 * @param pmChar the character received from the remote side
5176 */
5177 private void pmPut(final char pmChar) {
5178 // System.err.println("pmPut: " + pmChar);
5179
5180 boolean pmEnd = false;
5181
5182 if ((pmChar == '\\')
5183 && (collectBuffer.charAt(collectBuffer.length() - 1) == '\033')
5184 ) {
5185 pmEnd = true;
5186 }
5187
5188 // Collect first
5189 collectBuffer.append(pmChar);
5190
5191 // Xterm cases...
5192 if (pmEnd) {
5193 String arg = null;
5194 arg = collectBuffer.substring(0, collectBuffer.length() - 2);
5195
5196 // System.err.println("arg: '" + arg + "'");
5197
5198 if (arg.equals("hideMousePointer")) {
5199 hideMousePointer = true;
5200 }
5201 if (arg.equals("showMousePointer")) {
5202 hideMousePointer = false;
5203 }
5204
5205 // Go to SCAN_GROUND state
5206 toGround();
5207 return;
5208 }
5209 }
5210
5211 /**
5212 * Perform xterm window operations.
5213 */
5214 private void xtermWindowOps() {
5215 boolean xtermPrivateModeFlag = false;
5216
5217 for (int i = 0; i < collectBuffer.length(); i++) {
5218 if (collectBuffer.charAt(i) == '?') {
5219 xtermPrivateModeFlag = true;
5220 break;
5221 }
5222 }
5223
5224 int i = getCsiParam(0, 0);
5225
5226 if (!xtermPrivateModeFlag) {
5227 switch (i) {
5228 case 14:
5229 // Report xterm text area size in pixels as CSI 4 ; height ;
5230 // width t
5231 writeRemote(String.format("\033[4;%d;%dt", textHeight * height,
5232 textWidth * width));
5233 break;
5234 case 16:
5235 // Report character size in pixels as CSI 6 ; height ; width
5236 // t
5237 writeRemote(String.format("\033[6;%d;%dt", textHeight,
5238 textWidth));
5239 break;
5240 case 18:
5241 // Report the text are size in characters as CSI 8 ; height ;
5242 // width t
5243 writeRemote(String.format("\033[8;%d;%dt", height, width));
5244 break;
5245 default:
5246 break;
5247 }
5248 }
5249 }
5250
5251 /**
5252 * Respond to xterm sixel query.
5253 */
5254 private void xtermSixelQuery() {
5255 int item = getCsiParam(0, 0);
5256 int action = getCsiParam(1, 0);
5257 int value = getCsiParam(2, 0);
5258
5259 switch (item) {
5260 case 1:
5261 if (action == 1) {
5262 // Report number of color registers.
5263 writeRemote(String.format("\033[?%d;%d;%dS", item, 0, 1024));
5264 return;
5265 }
5266 break;
5267 default:
5268 break;
5269 }
5270 // We will not support this option.
5271 writeRemote(String.format("\033[?%d;%dS", item, action));
5272 }
5273
5274 /**
5275 * Run this input character through the ECMA48 state machine.
5276 *
5277 * @param ch character from the remote side
5278 */
5279 private void consume(final int ch) {
5280 readCount++;
5281
5282 // DEBUG
5283 // System.err.printf("%c STATE = %s\n", ch, scanState);
5284
5285 // Special "anywhere" states
5286
5287 // 18, 1A --> execute, then switch to SCAN_GROUND
5288 if ((ch == 0x18) || (ch == 0x1A)) {
5289 // CAN and SUB abort escape sequences
5290 toGround();
5291 return;
5292 }
5293
5294 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
5295
5296 // 0x1B == ESCAPE
5297 if (ch == 0x1B) {
5298 if ((type == DeviceType.XTERM)
5299 && ((scanState == ScanState.OSC_STRING)
5300 || (scanState == ScanState.DCS_SIXEL)
5301 || (scanState == ScanState.SOSPMAPC_STRING))
5302 ) {
5303 // Xterm can pass ESCAPE to its OSC sequence.
5304 // Xterm can pass ESCAPE to its DCS sequence.
5305 // Jexer can pass ESCAPE to its PM sequence.
5306 } else if ((scanState != ScanState.DCS_ENTRY)
5307 && (scanState != ScanState.DCS_INTERMEDIATE)
5308 && (scanState != ScanState.DCS_IGNORE)
5309 && (scanState != ScanState.DCS_PARAM)
5310 && (scanState != ScanState.DCS_PASSTHROUGH)
5311 ) {
5312 scanState = ScanState.ESCAPE;
5313 return;
5314 }
5315 }
5316
5317 // 0x9B == CSI 8-bit sequence
5318 if (ch == 0x9B) {
5319 scanState = ScanState.CSI_ENTRY;
5320 return;
5321 }
5322
5323 // 0x9D goes to ScanState.OSC_STRING
5324 if (ch == 0x9D) {
5325 scanState = ScanState.OSC_STRING;
5326 return;
5327 }
5328
5329 // 0x90 goes to DCS_ENTRY
5330 if (ch == 0x90) {
5331 scanState = ScanState.DCS_ENTRY;
5332 return;
5333 }
5334
5335 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
5336 if ((ch == 0x98) || (ch == 0x9E) || (ch == 0x9F)) {
5337 scanState = ScanState.SOSPMAPC_STRING;
5338 return;
5339 }
5340
5341 // 0x7F (DEL) is always discarded
5342 if (ch == 0x7F) {
5343 return;
5344 }
5345
5346 switch (scanState) {
5347
5348 case GROUND:
5349 // 00-17, 19, 1C-1F --> execute
5350 // 80-8F, 91-9A, 9C --> execute
5351 if ((ch <= 0x1F) || ((ch >= 0x80) && (ch <= 0x9F))) {
5352 handleControlChar((char) ch);
5353 }
5354
5355 // 20-7F --> print
5356 if (((ch >= 0x20) && (ch <= 0x7F))
5357 || (ch >= 0xA0)
5358 ) {
5359
5360 // VT220 printer --> trash bin
5361 if (((type == DeviceType.VT220)
5362 || (type == DeviceType.XTERM))
5363 && (printerControllerMode == true)
5364 ) {
5365 return;
5366 }
5367
5368 // Hang onto this character
5369 repCh = mapCharacter(ch);
5370
5371 // Print this character
5372 printCharacter(repCh);
5373 }
5374 return;
5375
5376 case ESCAPE:
5377 // 00-17, 19, 1C-1F --> execute
5378 if (ch <= 0x1F) {
5379 handleControlChar((char) ch);
5380 return;
5381 }
5382
5383 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
5384 if ((ch >= 0x20) && (ch <= 0x2F)) {
5385 collect((char) ch);
5386 scanState = ScanState.ESCAPE_INTERMEDIATE;
5387 return;
5388 }
5389
5390 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
5391 if ((ch >= 0x30) && (ch <= 0x4F)) {
5392 switch (ch) {
5393 case '0':
5394 case '1':
5395 case '2':
5396 case '3':
5397 case '4':
5398 case '5':
5399 case '6':
5400 break;
5401 case '7':
5402 // DECSC - Save cursor
5403 // Note this code overlaps both ANSI and VT52 mode
5404 decsc();
5405 break;
5406
5407 case '8':
5408 // DECRC - Restore cursor
5409 // Note this code overlaps both ANSI and VT52 mode
5410 decrc();
5411 break;
5412
5413 case '9':
5414 case ':':
5415 case ';':
5416 break;
5417 case '<':
5418 if (vt52Mode == true) {
5419 // DECANM - Enter ANSI mode
5420 vt52Mode = false;
5421 arrowKeyMode = ArrowKeyMode.VT100;
5422
5423 /*
5424 * From the VT102 docs: "You use ANSI mode to select
5425 * most terminal features; the terminal uses the same
5426 * features when it switches to VT52 mode. You
5427 * cannot, however, change most of these features in
5428 * VT52 mode."
5429 *
5430 * In other words, do not reset any other attributes
5431 * when switching between VT52 submode and ANSI.
5432 */
5433
5434 // Reset fonts
5435 currentState.g0Charset = CharacterSet.US;
5436 currentState.g1Charset = CharacterSet.DRAWING;
5437 s8c1t = false;
5438 singleshift = Singleshift.NONE;
5439 currentState.glLockshift = LockshiftMode.NONE;
5440 currentState.grLockshift = LockshiftMode.NONE;
5441 }
5442 break;
5443 case '=':
5444 // DECKPAM - Keypad application mode
5445 // Note this code overlaps both ANSI and VT52 mode
5446 deckpam();
5447 break;
5448 case '>':
5449 // DECKPNM - Keypad numeric mode
5450 // Note this code overlaps both ANSI and VT52 mode
5451 deckpnm();
5452 break;
5453 case '?':
5454 case '@':
5455 break;
5456 case 'A':
5457 if (vt52Mode == true) {
5458 // Cursor up, and stop at the top without scrolling
5459 cursorUp(1, false);
5460 }
5461 break;
5462 case 'B':
5463 if (vt52Mode == true) {
5464 // Cursor down, and stop at the bottom without scrolling
5465 cursorDown(1, false);
5466 }
5467 break;
5468 case 'C':
5469 if (vt52Mode == true) {
5470 // Cursor right, and stop at the right without scrolling
5471 cursorRight(1, false);
5472 }
5473 break;
5474 case 'D':
5475 if (vt52Mode == true) {
5476 // Cursor left, and stop at the left without scrolling
5477 cursorLeft(1, false);
5478 } else {
5479 // IND - Index
5480 ind();
5481 }
5482 break;
5483 case 'E':
5484 if (vt52Mode == true) {
5485 // Nothing
5486 } else {
5487 // NEL - Next line
5488 nel();
5489 }
5490 break;
5491 case 'F':
5492 if (vt52Mode == true) {
5493 // G0 --> Special graphics
5494 currentState.g0Charset = CharacterSet.VT52_GRAPHICS;
5495 }
5496 break;
5497 case 'G':
5498 if (vt52Mode == true) {
5499 // G0 --> ASCII set
5500 currentState.g0Charset = CharacterSet.US;
5501 }
5502 break;
5503 case 'H':
5504 if (vt52Mode == true) {
5505 // Cursor to home
5506 cursorPosition(0, 0);
5507 } else {
5508 // HTS - Horizontal tabulation set
5509 hts();
5510 }
5511 break;
5512 case 'I':
5513 if (vt52Mode == true) {
5514 // Reverse line feed. Same as RI.
5515 ri();
5516 }
5517 break;
5518 case 'J':
5519 if (vt52Mode == true) {
5520 // Erase to end of screen
5521 eraseLine(currentState.cursorX, width - 1, false);
5522 eraseScreen(currentState.cursorY + 1, 0, height - 1,
5523 width - 1, false);
5524 }
5525 break;
5526 case 'K':
5527 if (vt52Mode == true) {
5528 // Erase to end of line
5529 eraseLine(currentState.cursorX, width - 1, false);
5530 }
5531 break;
5532 case 'L':
5533 break;
5534 case 'M':
5535 if (vt52Mode == true) {
5536 // Nothing
5537 } else {
5538 // RI - Reverse index
5539 ri();
5540 }
5541 break;
5542 case 'N':
5543 if (vt52Mode == false) {
5544 // SS2
5545 singleshift = Singleshift.SS2;
5546 }
5547 break;
5548 case 'O':
5549 if (vt52Mode == false) {
5550 // SS3
5551 singleshift = Singleshift.SS3;
5552 }
5553 break;
5554 }
5555 toGround();
5556 return;
5557 }
5558 if ((ch >= 0x51) && (ch <= 0x57)) {
5559 switch (ch) {
5560 case 'Q':
5561 case 'R':
5562 case 'S':
5563 case 'T':
5564 case 'U':
5565 case 'V':
5566 case 'W':
5567 break;
5568 }
5569 toGround();
5570 return;
5571 }
5572 if (ch == 0x59) {
5573 // 'Y'
5574 if (vt52Mode == true) {
5575 scanState = ScanState.VT52_DIRECT_CURSOR_ADDRESS;
5576 } else {
5577 toGround();
5578 }
5579 return;
5580 }
5581 if (ch == 0x5A) {
5582 // 'Z'
5583 if (vt52Mode == true) {
5584 // Identify
5585 // Send string directly to remote side
5586 writeRemote("\033/Z");
5587 } else {
5588 // DECID
5589 // Send string directly to remote side
5590 writeRemote(deviceTypeResponse());
5591 }
5592 toGround();
5593 return;
5594 }
5595 if (ch == 0x5C) {
5596 // '\'
5597 toGround();
5598 return;
5599 }
5600
5601 // VT52 cannot get to any of these other states
5602 if (vt52Mode == true) {
5603 toGround();
5604 return;
5605 }
5606
5607 if ((ch >= 0x60) && (ch <= 0x7E)) {
5608 switch (ch) {
5609 case '`':
5610 case 'a':
5611 case 'b':
5612 break;
5613 case 'c':
5614 // RIS - Reset to initial state
5615 reset();
5616 // Do I clear screen too? I think so...
5617 eraseScreen(0, 0, height - 1, width - 1, false);
5618 cursorPosition(0, 0);
5619 break;
5620 case 'd':
5621 case 'e':
5622 case 'f':
5623 case 'g':
5624 case 'h':
5625 case 'i':
5626 case 'j':
5627 case 'k':
5628 case 'l':
5629 case 'm':
5630 break;
5631 case 'n':
5632 if ((type == DeviceType.VT220)
5633 || (type == DeviceType.XTERM)) {
5634
5635 // VT220 lockshift G2 into GL
5636 currentState.glLockshift = LockshiftMode.G2_GL;
5637 shiftOut = false;
5638 }
5639 break;
5640 case 'o':
5641 if ((type == DeviceType.VT220)
5642 || (type == DeviceType.XTERM)) {
5643
5644 // VT220 lockshift G3 into GL
5645 currentState.glLockshift = LockshiftMode.G3_GL;
5646 shiftOut = false;
5647 }
5648 break;
5649 case 'p':
5650 case 'q':
5651 case 'r':
5652 case 's':
5653 case 't':
5654 case 'u':
5655 case 'v':
5656 case 'w':
5657 case 'x':
5658 case 'y':
5659 case 'z':
5660 case '{':
5661 break;
5662 case '|':
5663 if ((type == DeviceType.VT220)
5664 || (type == DeviceType.XTERM)) {
5665
5666 // VT220 lockshift G3 into GR
5667 currentState.grLockshift = LockshiftMode.G3_GR;
5668 shiftOut = false;
5669 }
5670 break;
5671 case '}':
5672 if ((type == DeviceType.VT220)
5673 || (type == DeviceType.XTERM)) {
5674
5675 // VT220 lockshift G2 into GR
5676 currentState.grLockshift = LockshiftMode.G2_GR;
5677 shiftOut = false;
5678 }
5679 break;
5680
5681 case '~':
5682 if ((type == DeviceType.VT220)
5683 || (type == DeviceType.XTERM)) {
5684
5685 // VT220 lockshift G1 into GR
5686 currentState.grLockshift = LockshiftMode.G1_GR;
5687 shiftOut = false;
5688 }
5689 break;
5690 }
5691 toGround();
5692 }
5693
5694 // 7F --> ignore
5695
5696 // 0x5B goes to CSI_ENTRY
5697 if (ch == 0x5B) {
5698 scanState = ScanState.CSI_ENTRY;
5699 }
5700
5701 // 0x5D goes to OSC_STRING
5702 if (ch == 0x5D) {
5703 scanState = ScanState.OSC_STRING;
5704 }
5705
5706 // 0x50 goes to DCS_ENTRY
5707 if (ch == 0x50) {
5708 scanState = ScanState.DCS_ENTRY;
5709 }
5710
5711 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5712 if ((ch == 0x58) || (ch == 0x5E) || (ch == 0x5F)) {
5713 scanState = ScanState.SOSPMAPC_STRING;
5714 }
5715
5716 return;
5717
5718 case ESCAPE_INTERMEDIATE:
5719 // 00-17, 19, 1C-1F --> execute
5720 if (ch <= 0x1F) {
5721 handleControlChar((char) ch);
5722 }
5723
5724 // 20-2F --> collect
5725 if ((ch >= 0x20) && (ch <= 0x2F)) {
5726 collect((char) ch);
5727 }
5728
5729 // 30-7E --> dispatch, then switch to GROUND
5730 if ((ch >= 0x30) && (ch <= 0x7E)) {
5731 switch (ch) {
5732 case '0':
5733 if ((collectBuffer.length() == 1)
5734 && (collectBuffer.charAt(0) == '(')) {
5735 // G0 --> Special graphics
5736 currentState.g0Charset = CharacterSet.DRAWING;
5737 }
5738 if ((collectBuffer.length() == 1)
5739 && (collectBuffer.charAt(0) == ')')) {
5740 // G1 --> Special graphics
5741 currentState.g1Charset = CharacterSet.DRAWING;
5742 }
5743 if ((type == DeviceType.VT220)
5744 || (type == DeviceType.XTERM)) {
5745
5746 if ((collectBuffer.length() == 1)
5747 && (collectBuffer.charAt(0) == '*')) {
5748 // G2 --> Special graphics
5749 currentState.g2Charset = CharacterSet.DRAWING;
5750 }
5751 if ((collectBuffer.length() == 1)
5752 && (collectBuffer.charAt(0) == '+')) {
5753 // G3 --> Special graphics
5754 currentState.g3Charset = CharacterSet.DRAWING;
5755 }
5756 }
5757 break;
5758 case '1':
5759 if ((collectBuffer.length() == 1)
5760 && (collectBuffer.charAt(0) == '(')) {
5761 // G0 --> Alternate character ROM standard character set
5762 currentState.g0Charset = CharacterSet.ROM;
5763 }
5764 if ((collectBuffer.length() == 1)
5765 && (collectBuffer.charAt(0) == ')')) {
5766 // G1 --> Alternate character ROM standard character set
5767 currentState.g1Charset = CharacterSet.ROM;
5768 }
5769 break;
5770 case '2':
5771 if ((collectBuffer.length() == 1)
5772 && (collectBuffer.charAt(0) == '(')) {
5773 // G0 --> Alternate character ROM special graphics
5774 currentState.g0Charset = CharacterSet.ROM_SPECIAL;
5775 }
5776 if ((collectBuffer.length() == 1)
5777 && (collectBuffer.charAt(0) == ')')) {
5778 // G1 --> Alternate character ROM special graphics
5779 currentState.g1Charset = CharacterSet.ROM_SPECIAL;
5780 }
5781 break;
5782 case '3':
5783 if ((collectBuffer.length() == 1)
5784 && (collectBuffer.charAt(0) == '#')) {
5785 // DECDHL - Double-height line (top half)
5786 dechdl(true);
5787 }
5788 break;
5789 case '4':
5790 if ((collectBuffer.length() == 1)
5791 && (collectBuffer.charAt(0) == '#')) {
5792 // DECDHL - Double-height line (bottom half)
5793 dechdl(false);
5794 }
5795 if ((type == DeviceType.VT220)
5796 || (type == DeviceType.XTERM)) {
5797
5798 if ((collectBuffer.length() == 1)
5799 && (collectBuffer.charAt(0) == '(')) {
5800 // G0 --> DUTCH
5801 currentState.g0Charset = CharacterSet.NRC_DUTCH;
5802 }
5803 if ((collectBuffer.length() == 1)
5804 && (collectBuffer.charAt(0) == ')')) {
5805 // G1 --> DUTCH
5806 currentState.g1Charset = CharacterSet.NRC_DUTCH;
5807 }
5808 if ((collectBuffer.length() == 1)
5809 && (collectBuffer.charAt(0) == '*')) {
5810 // G2 --> DUTCH
5811 currentState.g2Charset = CharacterSet.NRC_DUTCH;
5812 }
5813 if ((collectBuffer.length() == 1)
5814 && (collectBuffer.charAt(0) == '+')) {
5815 // G3 --> DUTCH
5816 currentState.g3Charset = CharacterSet.NRC_DUTCH;
5817 }
5818 }
5819 break;
5820 case '5':
5821 if ((collectBuffer.length() == 1)
5822 && (collectBuffer.charAt(0) == '#')) {
5823 // DECSWL - Single-width line
5824 decswl();
5825 }
5826 if ((type == DeviceType.VT220)
5827 || (type == DeviceType.XTERM)) {
5828
5829 if ((collectBuffer.length() == 1)
5830 && (collectBuffer.charAt(0) == '(')) {
5831 // G0 --> FINNISH
5832 currentState.g0Charset = CharacterSet.NRC_FINNISH;
5833 }
5834 if ((collectBuffer.length() == 1)
5835 && (collectBuffer.charAt(0) == ')')) {
5836 // G1 --> FINNISH
5837 currentState.g1Charset = CharacterSet.NRC_FINNISH;
5838 }
5839 if ((collectBuffer.length() == 1)
5840 && (collectBuffer.charAt(0) == '*')) {
5841 // G2 --> FINNISH
5842 currentState.g2Charset = CharacterSet.NRC_FINNISH;
5843 }
5844 if ((collectBuffer.length() == 1)
5845 && (collectBuffer.charAt(0) == '+')) {
5846 // G3 --> FINNISH
5847 currentState.g3Charset = CharacterSet.NRC_FINNISH;
5848 }
5849 }
5850 break;
5851 case '6':
5852 if ((collectBuffer.length() == 1)
5853 && (collectBuffer.charAt(0) == '#')) {
5854 // DECDWL - Double-width line
5855 decdwl();
5856 }
5857 if ((type == DeviceType.VT220)
5858 || (type == DeviceType.XTERM)) {
5859
5860 if ((collectBuffer.length() == 1)
5861 && (collectBuffer.charAt(0) == '(')) {
5862 // G0 --> NORWEGIAN
5863 currentState.g0Charset = CharacterSet.NRC_NORWEGIAN;
5864 }
5865 if ((collectBuffer.length() == 1)
5866 && (collectBuffer.charAt(0) == ')')) {
5867 // G1 --> NORWEGIAN
5868 currentState.g1Charset = CharacterSet.NRC_NORWEGIAN;
5869 }
5870 if ((collectBuffer.length() == 1)
5871 && (collectBuffer.charAt(0) == '*')) {
5872 // G2 --> NORWEGIAN
5873 currentState.g2Charset = CharacterSet.NRC_NORWEGIAN;
5874 }
5875 if ((collectBuffer.length() == 1)
5876 && (collectBuffer.charAt(0) == '+')) {
5877 // G3 --> NORWEGIAN
5878 currentState.g3Charset = CharacterSet.NRC_NORWEGIAN;
5879 }
5880 }
5881 break;
5882 case '7':
5883 if ((type == DeviceType.VT220)
5884 || (type == DeviceType.XTERM)) {
5885
5886 if ((collectBuffer.length() == 1)
5887 && (collectBuffer.charAt(0) == '(')) {
5888 // G0 --> SWEDISH
5889 currentState.g0Charset = CharacterSet.NRC_SWEDISH;
5890 }
5891 if ((collectBuffer.length() == 1)
5892 && (collectBuffer.charAt(0) == ')')) {
5893 // G1 --> SWEDISH
5894 currentState.g1Charset = CharacterSet.NRC_SWEDISH;
5895 }
5896 if ((collectBuffer.length() == 1)
5897 && (collectBuffer.charAt(0) == '*')) {
5898 // G2 --> SWEDISH
5899 currentState.g2Charset = CharacterSet.NRC_SWEDISH;
5900 }
5901 if ((collectBuffer.length() == 1)
5902 && (collectBuffer.charAt(0) == '+')) {
5903 // G3 --> SWEDISH
5904 currentState.g3Charset = CharacterSet.NRC_SWEDISH;
5905 }
5906 }
5907 break;
5908 case '8':
5909 if ((collectBuffer.length() == 1)
5910 && (collectBuffer.charAt(0) == '#')) {
5911 // DECALN - Screen alignment display
5912 decaln();
5913 }
5914 break;
5915 case '9':
5916 case ':':
5917 case ';':
5918 break;
5919 case '<':
5920 if ((type == DeviceType.VT220)
5921 || (type == DeviceType.XTERM)) {
5922
5923 if ((collectBuffer.length() == 1)
5924 && (collectBuffer.charAt(0) == '(')) {
5925 // G0 --> DEC_SUPPLEMENTAL
5926 currentState.g0Charset = CharacterSet.DEC_SUPPLEMENTAL;
5927 }
5928 if ((collectBuffer.length() == 1)
5929 && (collectBuffer.charAt(0) == ')')) {
5930 // G1 --> DEC_SUPPLEMENTAL
5931 currentState.g1Charset = CharacterSet.DEC_SUPPLEMENTAL;
5932 }
5933 if ((collectBuffer.length() == 1)
5934 && (collectBuffer.charAt(0) == '*')) {
5935 // G2 --> DEC_SUPPLEMENTAL
5936 currentState.g2Charset = CharacterSet.DEC_SUPPLEMENTAL;
5937 }
5938 if ((collectBuffer.length() == 1)
5939 && (collectBuffer.charAt(0) == '+')) {
5940 // G3 --> DEC_SUPPLEMENTAL
5941 currentState.g3Charset = CharacterSet.DEC_SUPPLEMENTAL;
5942 }
5943 }
5944 break;
5945 case '=':
5946 if ((type == DeviceType.VT220)
5947 || (type == DeviceType.XTERM)) {
5948
5949 if ((collectBuffer.length() == 1)
5950 && (collectBuffer.charAt(0) == '(')) {
5951 // G0 --> SWISS
5952 currentState.g0Charset = CharacterSet.NRC_SWISS;
5953 }
5954 if ((collectBuffer.length() == 1)
5955 && (collectBuffer.charAt(0) == ')')) {
5956 // G1 --> SWISS
5957 currentState.g1Charset = CharacterSet.NRC_SWISS;
5958 }
5959 if ((collectBuffer.length() == 1)
5960 && (collectBuffer.charAt(0) == '*')) {
5961 // G2 --> SWISS
5962 currentState.g2Charset = CharacterSet.NRC_SWISS;
5963 }
5964 if ((collectBuffer.length() == 1)
5965 && (collectBuffer.charAt(0) == '+')) {
5966 // G3 --> SWISS
5967 currentState.g3Charset = CharacterSet.NRC_SWISS;
5968 }
5969 }
5970 break;
5971 case '>':
5972 case '?':
5973 case '@':
5974 break;
5975 case 'A':
5976 if ((collectBuffer.length() == 1)
5977 && (collectBuffer.charAt(0) == '(')) {
5978 // G0 --> United Kingdom set
5979 currentState.g0Charset = CharacterSet.UK;
5980 }
5981 if ((collectBuffer.length() == 1)
5982 && (collectBuffer.charAt(0) == ')')) {
5983 // G1 --> United Kingdom set
5984 currentState.g1Charset = CharacterSet.UK;
5985 }
5986 if ((type == DeviceType.VT220)
5987 || (type == DeviceType.XTERM)) {
5988
5989 if ((collectBuffer.length() == 1)
5990 && (collectBuffer.charAt(0) == '*')) {
5991 // G2 --> United Kingdom set
5992 currentState.g2Charset = CharacterSet.UK;
5993 }
5994 if ((collectBuffer.length() == 1)
5995 && (collectBuffer.charAt(0) == '+')) {
5996 // G3 --> United Kingdom set
5997 currentState.g3Charset = CharacterSet.UK;
5998 }
5999 }
6000 break;
6001 case 'B':
6002 if ((collectBuffer.length() == 1)
6003 && (collectBuffer.charAt(0) == '(')) {
6004 // G0 --> ASCII set
6005 currentState.g0Charset = CharacterSet.US;
6006 }
6007 if ((collectBuffer.length() == 1)
6008 && (collectBuffer.charAt(0) == ')')) {
6009 // G1 --> ASCII set
6010 currentState.g1Charset = CharacterSet.US;
6011 }
6012 if ((type == DeviceType.VT220)
6013 || (type == DeviceType.XTERM)) {
6014
6015 if ((collectBuffer.length() == 1)
6016 && (collectBuffer.charAt(0) == '*')) {
6017 // G2 --> ASCII
6018 currentState.g2Charset = CharacterSet.US;
6019 }
6020 if ((collectBuffer.length() == 1)
6021 && (collectBuffer.charAt(0) == '+')) {
6022 // G3 --> ASCII
6023 currentState.g3Charset = CharacterSet.US;
6024 }
6025 }
6026 break;
6027 case 'C':
6028 if ((type == DeviceType.VT220)
6029 || (type == DeviceType.XTERM)) {
6030
6031 if ((collectBuffer.length() == 1)
6032 && (collectBuffer.charAt(0) == '(')) {
6033 // G0 --> FINNISH
6034 currentState.g0Charset = CharacterSet.NRC_FINNISH;
6035 }
6036 if ((collectBuffer.length() == 1)
6037 && (collectBuffer.charAt(0) == ')')) {
6038 // G1 --> FINNISH
6039 currentState.g1Charset = CharacterSet.NRC_FINNISH;
6040 }
6041 if ((collectBuffer.length() == 1)
6042 && (collectBuffer.charAt(0) == '*')) {
6043 // G2 --> FINNISH
6044 currentState.g2Charset = CharacterSet.NRC_FINNISH;
6045 }
6046 if ((collectBuffer.length() == 1)
6047 && (collectBuffer.charAt(0) == '+')) {
6048 // G3 --> FINNISH
6049 currentState.g3Charset = CharacterSet.NRC_FINNISH;
6050 }
6051 }
6052 break;
6053 case 'D':
6054 break;
6055 case 'E':
6056 if ((type == DeviceType.VT220)
6057 || (type == DeviceType.XTERM)) {
6058
6059 if ((collectBuffer.length() == 1)
6060 && (collectBuffer.charAt(0) == '(')) {
6061 // G0 --> NORWEGIAN
6062 currentState.g0Charset = CharacterSet.NRC_NORWEGIAN;
6063 }
6064 if ((collectBuffer.length() == 1)
6065 && (collectBuffer.charAt(0) == ')')) {
6066 // G1 --> NORWEGIAN
6067 currentState.g1Charset = CharacterSet.NRC_NORWEGIAN;
6068 }
6069 if ((collectBuffer.length() == 1)
6070 && (collectBuffer.charAt(0) == '*')) {
6071 // G2 --> NORWEGIAN
6072 currentState.g2Charset = CharacterSet.NRC_NORWEGIAN;
6073 }
6074 if ((collectBuffer.length() == 1)
6075 && (collectBuffer.charAt(0) == '+')) {
6076 // G3 --> NORWEGIAN
6077 currentState.g3Charset = CharacterSet.NRC_NORWEGIAN;
6078 }
6079 }
6080 break;
6081 case 'F':
6082 if ((type == DeviceType.VT220)
6083 || (type == DeviceType.XTERM)) {
6084
6085 if ((collectBuffer.length() == 1)
6086 && (collectBuffer.charAt(0) == ' ')) {
6087 // S7C1T
6088 s8c1t = false;
6089 }
6090 }
6091 break;
6092 case 'G':
6093 if ((type == DeviceType.VT220)
6094 || (type == DeviceType.XTERM)) {
6095
6096 if ((collectBuffer.length() == 1)
6097 && (collectBuffer.charAt(0) == ' ')) {
6098 // S8C1T
6099 s8c1t = true;
6100 }
6101 }
6102 break;
6103 case 'H':
6104 if ((type == DeviceType.VT220)
6105 || (type == DeviceType.XTERM)) {
6106
6107 if ((collectBuffer.length() == 1)
6108 && (collectBuffer.charAt(0) == '(')) {
6109 // G0 --> SWEDISH
6110 currentState.g0Charset = CharacterSet.NRC_SWEDISH;
6111 }
6112 if ((collectBuffer.length() == 1)
6113 && (collectBuffer.charAt(0) == ')')) {
6114 // G1 --> SWEDISH
6115 currentState.g1Charset = CharacterSet.NRC_SWEDISH;
6116 }
6117 if ((collectBuffer.length() == 1)
6118 && (collectBuffer.charAt(0) == '*')) {
6119 // G2 --> SWEDISH
6120 currentState.g2Charset = CharacterSet.NRC_SWEDISH;
6121 }
6122 if ((collectBuffer.length() == 1)
6123 && (collectBuffer.charAt(0) == '+')) {
6124 // G3 --> SWEDISH
6125 currentState.g3Charset = CharacterSet.NRC_SWEDISH;
6126 }
6127 }
6128 break;
6129 case 'I':
6130 case 'J':
6131 break;
6132 case 'K':
6133 if ((type == DeviceType.VT220)
6134 || (type == DeviceType.XTERM)) {
6135
6136 if ((collectBuffer.length() == 1)
6137 && (collectBuffer.charAt(0) == '(')) {
6138 // G0 --> GERMAN
6139 currentState.g0Charset = CharacterSet.NRC_GERMAN;
6140 }
6141 if ((collectBuffer.length() == 1)
6142 && (collectBuffer.charAt(0) == ')')) {
6143 // G1 --> GERMAN
6144 currentState.g1Charset = CharacterSet.NRC_GERMAN;
6145 }
6146 if ((collectBuffer.length() == 1)
6147 && (collectBuffer.charAt(0) == '*')) {
6148 // G2 --> GERMAN
6149 currentState.g2Charset = CharacterSet.NRC_GERMAN;
6150 }
6151 if ((collectBuffer.length() == 1)
6152 && (collectBuffer.charAt(0) == '+')) {
6153 // G3 --> GERMAN
6154 currentState.g3Charset = CharacterSet.NRC_GERMAN;
6155 }
6156 }
6157 break;
6158 case 'L':
6159 case 'M':
6160 case 'N':
6161 case 'O':
6162 case 'P':
6163 break;
6164 case 'Q':
6165 if ((type == DeviceType.VT220)
6166 || (type == DeviceType.XTERM)) {
6167
6168 if ((collectBuffer.length() == 1)
6169 && (collectBuffer.charAt(0) == '(')) {
6170 // G0 --> FRENCH_CA
6171 currentState.g0Charset = CharacterSet.NRC_FRENCH_CA;
6172 }
6173 if ((collectBuffer.length() == 1)
6174 && (collectBuffer.charAt(0) == ')')) {
6175 // G1 --> FRENCH_CA
6176 currentState.g1Charset = CharacterSet.NRC_FRENCH_CA;
6177 }
6178 if ((collectBuffer.length() == 1)
6179 && (collectBuffer.charAt(0) == '*')) {
6180 // G2 --> FRENCH_CA
6181 currentState.g2Charset = CharacterSet.NRC_FRENCH_CA;
6182 }
6183 if ((collectBuffer.length() == 1)
6184 && (collectBuffer.charAt(0) == '+')) {
6185 // G3 --> FRENCH_CA
6186 currentState.g3Charset = CharacterSet.NRC_FRENCH_CA;
6187 }
6188 }
6189 break;
6190 case 'R':
6191 if ((type == DeviceType.VT220)
6192 || (type == DeviceType.XTERM)) {
6193
6194 if ((collectBuffer.length() == 1)
6195 && (collectBuffer.charAt(0) == '(')) {
6196 // G0 --> FRENCH
6197 currentState.g0Charset = CharacterSet.NRC_FRENCH;
6198 }
6199 if ((collectBuffer.length() == 1)
6200 && (collectBuffer.charAt(0) == ')')) {
6201 // G1 --> FRENCH
6202 currentState.g1Charset = CharacterSet.NRC_FRENCH;
6203 }
6204 if ((collectBuffer.length() == 1)
6205 && (collectBuffer.charAt(0) == '*')) {
6206 // G2 --> FRENCH
6207 currentState.g2Charset = CharacterSet.NRC_FRENCH;
6208 }
6209 if ((collectBuffer.length() == 1)
6210 && (collectBuffer.charAt(0) == '+')) {
6211 // G3 --> FRENCH
6212 currentState.g3Charset = CharacterSet.NRC_FRENCH;
6213 }
6214 }
6215 break;
6216 case 'S':
6217 case 'T':
6218 case 'U':
6219 case 'V':
6220 case 'W':
6221 case 'X':
6222 break;
6223 case 'Y':
6224 if ((type == DeviceType.VT220)
6225 || (type == DeviceType.XTERM)) {
6226
6227 if ((collectBuffer.length() == 1)
6228 && (collectBuffer.charAt(0) == '(')) {
6229 // G0 --> ITALIAN
6230 currentState.g0Charset = CharacterSet.NRC_ITALIAN;
6231 }
6232 if ((collectBuffer.length() == 1)
6233 && (collectBuffer.charAt(0) == ')')) {
6234 // G1 --> ITALIAN
6235 currentState.g1Charset = CharacterSet.NRC_ITALIAN;
6236 }
6237 if ((collectBuffer.length() == 1)
6238 && (collectBuffer.charAt(0) == '*')) {
6239 // G2 --> ITALIAN
6240 currentState.g2Charset = CharacterSet.NRC_ITALIAN;
6241 }
6242 if ((collectBuffer.length() == 1)
6243 && (collectBuffer.charAt(0) == '+')) {
6244 // G3 --> ITALIAN
6245 currentState.g3Charset = CharacterSet.NRC_ITALIAN;
6246 }
6247 }
6248 break;
6249 case 'Z':
6250 if ((type == DeviceType.VT220)
6251 || (type == DeviceType.XTERM)) {
6252
6253 if ((collectBuffer.length() == 1)
6254 && (collectBuffer.charAt(0) == '(')) {
6255 // G0 --> SPANISH
6256 currentState.g0Charset = CharacterSet.NRC_SPANISH;
6257 }
6258 if ((collectBuffer.length() == 1)
6259 && (collectBuffer.charAt(0) == ')')) {
6260 // G1 --> SPANISH
6261 currentState.g1Charset = CharacterSet.NRC_SPANISH;
6262 }
6263 if ((collectBuffer.length() == 1)
6264 && (collectBuffer.charAt(0) == '*')) {
6265 // G2 --> SPANISH
6266 currentState.g2Charset = CharacterSet.NRC_SPANISH;
6267 }
6268 if ((collectBuffer.length() == 1)
6269 && (collectBuffer.charAt(0) == '+')) {
6270 // G3 --> SPANISH
6271 currentState.g3Charset = CharacterSet.NRC_SPANISH;
6272 }
6273 }
6274 break;
6275 case '[':
6276 case '\\':
6277 case ']':
6278 case '^':
6279 case '_':
6280 case '`':
6281 case 'a':
6282 case 'b':
6283 case 'c':
6284 case 'd':
6285 case 'e':
6286 case 'f':
6287 case 'g':
6288 case 'h':
6289 case 'i':
6290 case 'j':
6291 case 'k':
6292 case 'l':
6293 case 'm':
6294 case 'n':
6295 case 'o':
6296 case 'p':
6297 case 'q':
6298 case 'r':
6299 case 's':
6300 case 't':
6301 case 'u':
6302 case 'v':
6303 case 'w':
6304 case 'x':
6305 case 'y':
6306 case 'z':
6307 case '{':
6308 case '|':
6309 case '}':
6310 case '~':
6311 break;
6312 }
6313 toGround();
6314 }
6315
6316 // 7F --> ignore
6317
6318 // 0x9C goes to GROUND
6319 if (ch == 0x9C) {
6320 toGround();
6321 }
6322
6323 return;
6324
6325 case CSI_ENTRY:
6326 // 00-17, 19, 1C-1F --> execute
6327 if (ch <= 0x1F) {
6328 handleControlChar((char) ch);
6329 }
6330
6331 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6332 if ((ch >= 0x20) && (ch <= 0x2F)) {
6333 collect((char) ch);
6334 scanState = ScanState.CSI_INTERMEDIATE;
6335 }
6336
6337 // 30-39, 3B --> param, then switch to CSI_PARAM
6338 if ((ch >= '0') && (ch <= '9')) {
6339 param((byte) ch);
6340 scanState = ScanState.CSI_PARAM;
6341 }
6342 if (ch == ';') {
6343 param((byte) ch);
6344 scanState = ScanState.CSI_PARAM;
6345 }
6346
6347 // 3C-3F --> collect, then switch to CSI_PARAM
6348 if ((ch >= 0x3C) && (ch <= 0x3F)) {
6349 collect((char) ch);
6350 scanState = ScanState.CSI_PARAM;
6351 }
6352
6353 // 40-7E --> dispatch, then switch to GROUND
6354 if ((ch >= 0x40) && (ch <= 0x7E)) {
6355 switch (ch) {
6356 case '@':
6357 // ICH - Insert character
6358 ich();
6359 break;
6360 case 'A':
6361 // CUU - Cursor up
6362 cuu();
6363 break;
6364 case 'B':
6365 // CUD - Cursor down
6366 cud();
6367 break;
6368 case 'C':
6369 // CUF - Cursor forward
6370 cuf();
6371 break;
6372 case 'D':
6373 // CUB - Cursor backward
6374 cub();
6375 break;
6376 case 'E':
6377 // CNL - Cursor down and to column 1
6378 if (type == DeviceType.XTERM) {
6379 cnl();
6380 }
6381 break;
6382 case 'F':
6383 // CPL - Cursor up and to column 1
6384 if (type == DeviceType.XTERM) {
6385 cpl();
6386 }
6387 break;
6388 case 'G':
6389 // CHA - Cursor to column # in current row
6390 if (type == DeviceType.XTERM) {
6391 cha();
6392 }
6393 break;
6394 case 'H':
6395 // CUP - Cursor position
6396 cup();
6397 break;
6398 case 'I':
6399 // CHT - Cursor forward X tab stops (default 1)
6400 if (type == DeviceType.XTERM) {
6401 cht();
6402 }
6403 break;
6404 case 'J':
6405 // ED - Erase in display
6406 ed();
6407 break;
6408 case 'K':
6409 // EL - Erase in line
6410 el();
6411 break;
6412 case 'L':
6413 // IL - Insert line
6414 il();
6415 break;
6416 case 'M':
6417 // DL - Delete line
6418 dl();
6419 break;
6420 case 'N':
6421 case 'O':
6422 break;
6423 case 'P':
6424 // DCH - Delete character
6425 dch();
6426 break;
6427 case 'Q':
6428 case 'R':
6429 break;
6430 case 'S':
6431 // Scroll up X lines (default 1)
6432 if (type == DeviceType.XTERM) {
6433 boolean xtermPrivateModeFlag = false;
6434 for (int i = 0; i < collectBuffer.length(); i++) {
6435 if (collectBuffer.charAt(i) == '?') {
6436 xtermPrivateModeFlag = true;
6437 break;
6438 }
6439 }
6440 if (xtermPrivateModeFlag) {
6441 xtermSixelQuery();
6442 } else {
6443 su();
6444 }
6445 }
6446 break;
6447 case 'T':
6448 // Scroll down X lines (default 1)
6449 if (type == DeviceType.XTERM) {
6450 sd();
6451 }
6452 break;
6453 case 'U':
6454 case 'V':
6455 case 'W':
6456 break;
6457 case 'X':
6458 if ((type == DeviceType.VT220)
6459 || (type == DeviceType.XTERM)) {
6460
6461 // ECH - Erase character
6462 ech();
6463 }
6464 break;
6465 case 'Y':
6466 break;
6467 case 'Z':
6468 // CBT - Cursor backward X tab stops (default 1)
6469 if (type == DeviceType.XTERM) {
6470 cbt();
6471 }
6472 break;
6473 case '[':
6474 case '\\':
6475 case ']':
6476 case '^':
6477 case '_':
6478 break;
6479 case '`':
6480 // HPA - Cursor to column # in current row. Same as CHA
6481 if (type == DeviceType.XTERM) {
6482 cha();
6483 }
6484 break;
6485 case 'a':
6486 // HPR - Cursor right. Same as CUF
6487 if (type == DeviceType.XTERM) {
6488 cuf();
6489 }
6490 break;
6491 case 'b':
6492 // REP - Repeat last char X times
6493 if (type == DeviceType.XTERM) {
6494 rep();
6495 }
6496 break;
6497 case 'c':
6498 // DA - Device attributes
6499 da();
6500 break;
6501 case 'd':
6502 // VPA - Cursor to row, current column.
6503 if (type == DeviceType.XTERM) {
6504 vpa();
6505 }
6506 break;
6507 case 'e':
6508 // VPR - Cursor down. Same as CUD
6509 if (type == DeviceType.XTERM) {
6510 cud();
6511 }
6512 break;
6513 case 'f':
6514 // HVP - Horizontal and vertical position
6515 hvp();
6516 break;
6517 case 'g':
6518 // TBC - Tabulation clear
6519 tbc();
6520 break;
6521 case 'h':
6522 // Sets an ANSI or DEC private toggle
6523 setToggle(true);
6524 break;
6525 case 'i':
6526 if ((type == DeviceType.VT220)
6527 || (type == DeviceType.XTERM)) {
6528
6529 // Printer functions
6530 printerFunctions();
6531 }
6532 break;
6533 case 'j':
6534 case 'k':
6535 break;
6536 case 'l':
6537 // Sets an ANSI or DEC private toggle
6538 setToggle(false);
6539 break;
6540 case 'm':
6541 // SGR - Select graphics rendition
6542 sgr();
6543 break;
6544 case 'n':
6545 // DSR - Device status report
6546 dsr();
6547 break;
6548 case 'o':
6549 case 'p':
6550 break;
6551 case 'q':
6552 // DECLL - Load leds
6553 // Not supported
6554 break;
6555 case 'r':
6556 // DECSTBM - Set top and bottom margins
6557 decstbm();
6558 break;
6559 case 's':
6560 // Save cursor (ANSI.SYS)
6561 if (type == DeviceType.XTERM) {
6562 savedState.cursorX = currentState.cursorX;
6563 savedState.cursorY = currentState.cursorY;
6564 }
6565 break;
6566 case 't':
6567 if (type == DeviceType.XTERM) {
6568 // Window operations
6569 xtermWindowOps();
6570 }
6571 break;
6572 case 'u':
6573 // Restore cursor (ANSI.SYS)
6574 if (type == DeviceType.XTERM) {
6575 cursorPosition(savedState.cursorY, savedState.cursorX);
6576 }
6577 break;
6578 case 'v':
6579 case 'w':
6580 break;
6581 case 'x':
6582 // DECREQTPARM - Request terminal parameters
6583 decreqtparm();
6584 break;
6585 case 'y':
6586 case 'z':
6587 case '{':
6588 case '|':
6589 case '}':
6590 case '~':
6591 break;
6592 }
6593 toGround();
6594 }
6595
6596 // 7F --> ignore
6597
6598 // 0x9C goes to GROUND
6599 if (ch == 0x9C) {
6600 toGround();
6601 }
6602
6603 // 0x3A goes to CSI_IGNORE
6604 if (ch == 0x3A) {
6605 scanState = ScanState.CSI_IGNORE;
6606 }
6607 return;
6608
6609 case CSI_PARAM:
6610 // 00-17, 19, 1C-1F --> execute
6611 if (ch <= 0x1F) {
6612 handleControlChar((char) ch);
6613 }
6614
6615 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6616 if ((ch >= 0x20) && (ch <= 0x2F)) {
6617 collect((char) ch);
6618 scanState = ScanState.CSI_INTERMEDIATE;
6619 }
6620
6621 // 30-39, 3B --> param
6622 if ((ch >= '0') && (ch <= '9')) {
6623 param((byte) ch);
6624 }
6625 if (ch == ';') {
6626 param((byte) ch);
6627 }
6628
6629 // 0x3A goes to CSI_IGNORE
6630 if (ch == 0x3A) {
6631 scanState = ScanState.CSI_IGNORE;
6632 }
6633 // 0x3C-3F goes to CSI_IGNORE
6634 if ((ch >= 0x3C) && (ch <= 0x3F)) {
6635 scanState = ScanState.CSI_IGNORE;
6636 }
6637
6638 // 40-7E --> dispatch, then switch to GROUND
6639 if ((ch >= 0x40) && (ch <= 0x7E)) {
6640 switch (ch) {
6641 case '@':
6642 // ICH - Insert character
6643 ich();
6644 break;
6645 case 'A':
6646 // CUU - Cursor up
6647 cuu();
6648 break;
6649 case 'B':
6650 // CUD - Cursor down
6651 cud();
6652 break;
6653 case 'C':
6654 // CUF - Cursor forward
6655 cuf();
6656 break;
6657 case 'D':
6658 // CUB - Cursor backward
6659 cub();
6660 break;
6661 case 'E':
6662 // CNL - Cursor down and to column 1
6663 if (type == DeviceType.XTERM) {
6664 cnl();
6665 }
6666 break;
6667 case 'F':
6668 // CPL - Cursor up and to column 1
6669 if (type == DeviceType.XTERM) {
6670 cpl();
6671 }
6672 break;
6673 case 'G':
6674 // CHA - Cursor to column # in current row
6675 if (type == DeviceType.XTERM) {
6676 cha();
6677 }
6678 break;
6679 case 'H':
6680 // CUP - Cursor position
6681 cup();
6682 break;
6683 case 'I':
6684 // CHT - Cursor forward X tab stops (default 1)
6685 if (type == DeviceType.XTERM) {
6686 cht();
6687 }
6688 break;
6689 case 'J':
6690 // ED - Erase in display
6691 ed();
6692 break;
6693 case 'K':
6694 // EL - Erase in line
6695 el();
6696 break;
6697 case 'L':
6698 // IL - Insert line
6699 il();
6700 break;
6701 case 'M':
6702 // DL - Delete line
6703 dl();
6704 break;
6705 case 'N':
6706 case 'O':
6707 break;
6708 case 'P':
6709 // DCH - Delete character
6710 dch();
6711 break;
6712 case 'Q':
6713 case 'R':
6714 break;
6715 case 'S':
6716 // Scroll up X lines (default 1)
6717 if (type == DeviceType.XTERM) {
6718 boolean xtermPrivateModeFlag = false;
6719 for (int i = 0; i < collectBuffer.length(); i++) {
6720 if (collectBuffer.charAt(i) == '?') {
6721 xtermPrivateModeFlag = true;
6722 break;
6723 }
6724 }
6725 if (xtermPrivateModeFlag) {
6726 xtermSixelQuery();
6727 } else {
6728 su();
6729 }
6730 }
6731 break;
6732 case 'T':
6733 // Scroll down X lines (default 1)
6734 if (type == DeviceType.XTERM) {
6735 sd();
6736 }
6737 break;
6738 case 'U':
6739 case 'V':
6740 case 'W':
6741 break;
6742 case 'X':
6743 if ((type == DeviceType.VT220)
6744 || (type == DeviceType.XTERM)) {
6745
6746 // ECH - Erase character
6747 ech();
6748 }
6749 break;
6750 case 'Y':
6751 break;
6752 case 'Z':
6753 // CBT - Cursor backward X tab stops (default 1)
6754 if (type == DeviceType.XTERM) {
6755 cbt();
6756 }
6757 break;
6758 case '[':
6759 case '\\':
6760 case ']':
6761 case '^':
6762 case '_':
6763 break;
6764 case '`':
6765 // HPA - Cursor to column # in current row. Same as CHA
6766 if (type == DeviceType.XTERM) {
6767 cha();
6768 }
6769 break;
6770 case 'a':
6771 // HPR - Cursor right. Same as CUF
6772 if (type == DeviceType.XTERM) {
6773 cuf();
6774 }
6775 break;
6776 case 'b':
6777 // REP - Repeat last char X times
6778 if (type == DeviceType.XTERM) {
6779 rep();
6780 }
6781 break;
6782 case 'c':
6783 // DA - Device attributes
6784 da();
6785 break;
6786 case 'd':
6787 // VPA - Cursor to row, current column.
6788 if (type == DeviceType.XTERM) {
6789 vpa();
6790 }
6791 break;
6792 case 'e':
6793 // VPR - Cursor down. Same as CUD
6794 if (type == DeviceType.XTERM) {
6795 cud();
6796 }
6797 break;
6798 case 'f':
6799 // HVP - Horizontal and vertical position
6800 hvp();
6801 break;
6802 case 'g':
6803 // TBC - Tabulation clear
6804 tbc();
6805 break;
6806 case 'h':
6807 // Sets an ANSI or DEC private toggle
6808 setToggle(true);
6809 break;
6810 case 'i':
6811 if ((type == DeviceType.VT220)
6812 || (type == DeviceType.XTERM)) {
6813
6814 // Printer functions
6815 printerFunctions();
6816 }
6817 break;
6818 case 'j':
6819 case 'k':
6820 break;
6821 case 'l':
6822 // Sets an ANSI or DEC private toggle
6823 setToggle(false);
6824 break;
6825 case 'm':
6826 // SGR - Select graphics rendition
6827 sgr();
6828 break;
6829 case 'n':
6830 // DSR - Device status report
6831 dsr();
6832 break;
6833 case 'o':
6834 case 'p':
6835 break;
6836 case 'q':
6837 // DECLL - Load leds
6838 // Not supported
6839 break;
6840 case 'r':
6841 // DECSTBM - Set top and bottom margins
6842 decstbm();
6843 break;
6844 case 's':
6845 break;
6846 case 't':
6847 if (type == DeviceType.XTERM) {
6848 // Window operations
6849 xtermWindowOps();
6850 }
6851 break;
6852 case 'u':
6853 case 'v':
6854 case 'w':
6855 break;
6856 case 'x':
6857 // DECREQTPARM - Request terminal parameters
6858 decreqtparm();
6859 break;
6860 case 'y':
6861 case 'z':
6862 case '{':
6863 case '|':
6864 case '}':
6865 case '~':
6866 break;
6867 }
6868 toGround();
6869 }
6870
6871 // 7F --> ignore
6872 return;
6873
6874 case CSI_INTERMEDIATE:
6875 // 00-17, 19, 1C-1F --> execute
6876 if (ch <= 0x1F) {
6877 handleControlChar((char) ch);
6878 }
6879
6880 // 20-2F --> collect
6881 if ((ch >= 0x20) && (ch <= 0x2F)) {
6882 collect((char) ch);
6883 }
6884
6885 // 0x30-3F goes to CSI_IGNORE
6886 if ((ch >= 0x30) && (ch <= 0x3F)) {
6887 scanState = ScanState.CSI_IGNORE;
6888 }
6889
6890 // 40-7E --> dispatch, then switch to GROUND
6891 if ((ch >= 0x40) && (ch <= 0x7E)) {
6892 switch (ch) {
6893 case '@':
6894 case 'A':
6895 case 'B':
6896 case 'C':
6897 case 'D':
6898 case 'E':
6899 case 'F':
6900 case 'G':
6901 case 'H':
6902 case 'I':
6903 case 'J':
6904 case 'K':
6905 case 'L':
6906 case 'M':
6907 case 'N':
6908 case 'O':
6909 case 'P':
6910 case 'Q':
6911 case 'R':
6912 case 'S':
6913 case 'T':
6914 case 'U':
6915 case 'V':
6916 case 'W':
6917 case 'X':
6918 case 'Y':
6919 case 'Z':
6920 case '[':
6921 case '\\':
6922 case ']':
6923 case '^':
6924 case '_':
6925 case '`':
6926 case 'a':
6927 case 'b':
6928 case 'c':
6929 case 'd':
6930 case 'e':
6931 case 'f':
6932 case 'g':
6933 case 'h':
6934 case 'i':
6935 case 'j':
6936 case 'k':
6937 case 'l':
6938 case 'm':
6939 case 'n':
6940 case 'o':
6941 break;
6942 case 'p':
6943 if (((type == DeviceType.VT220)
6944 || (type == DeviceType.XTERM))
6945 && (collectBuffer.charAt(collectBuffer.length() - 1) == '\"')
6946 ) {
6947 // DECSCL - compatibility level
6948 decscl();
6949 }
6950 if ((type == DeviceType.XTERM)
6951 && (collectBuffer.charAt(collectBuffer.length() - 1) == '!')
6952 ) {
6953 // DECSTR - Soft terminal reset
6954 decstr();
6955 }
6956 break;
6957 case 'q':
6958 if (((type == DeviceType.VT220)
6959 || (type == DeviceType.XTERM))
6960 && (collectBuffer.charAt(collectBuffer.length() - 1) == '\"')
6961 ) {
6962 // DECSCA
6963 decsca();
6964 }
6965 break;
6966 case 'r':
6967 case 's':
6968 case 't':
6969 case 'u':
6970 case 'v':
6971 case 'w':
6972 case 'x':
6973 case 'y':
6974 case 'z':
6975 case '{':
6976 case '|':
6977 case '}':
6978 case '~':
6979 break;
6980 }
6981 toGround();
6982 }
6983
6984 // 7F --> ignore
6985 return;
6986
6987 case CSI_IGNORE:
6988 // 00-17, 19, 1C-1F --> execute
6989 if (ch <= 0x1F) {
6990 handleControlChar((char) ch);
6991 }
6992
6993 // 20-2F --> collect
6994 if ((ch >= 0x20) && (ch <= 0x2F)) {
6995 collect((char) ch);
6996 }
6997
6998 // 40-7E --> ignore, then switch to GROUND
6999 if ((ch >= 0x40) && (ch <= 0x7E)) {
7000 toGround();
7001 }
7002
7003 // 20-3F, 7F --> ignore
7004
7005 return;
7006
7007 case DCS_ENTRY:
7008
7009 // 0x9C goes to GROUND
7010 if (ch == 0x9C) {
7011 toGround();
7012 }
7013
7014 // 0x1B 0x5C goes to GROUND
7015 if (ch == 0x1B) {
7016 collect((char) ch);
7017 }
7018 if (ch == 0x5C) {
7019 if ((collectBuffer.length() > 0)
7020 && (collectBuffer.charAt(collectBuffer.length() - 1) == 0x1B)
7021 ) {
7022 toGround();
7023 }
7024 }
7025
7026 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
7027 if ((ch >= 0x20) && (ch <= 0x2F)) {
7028 collect((char) ch);
7029 scanState = ScanState.DCS_INTERMEDIATE;
7030 }
7031
7032 // 30-39, 3B --> param, then switch to DCS_PARAM
7033 if ((ch >= '0') && (ch <= '9')) {
7034 param((byte) ch);
7035 scanState = ScanState.DCS_PARAM;
7036 }
7037 if (ch == ';') {
7038 param((byte) ch);
7039 scanState = ScanState.DCS_PARAM;
7040 }
7041
7042 // 3C-3F --> collect, then switch to DCS_PARAM
7043 if ((ch >= 0x3C) && (ch <= 0x3F)) {
7044 collect((char) ch);
7045 scanState = ScanState.DCS_PARAM;
7046 }
7047
7048 // 00-17, 19, 1C-1F, 7F --> ignore
7049
7050 // 0x3A goes to DCS_IGNORE
7051 if (ch == 0x3F) {
7052 scanState = ScanState.DCS_IGNORE;
7053 }
7054
7055 // 0x71 goes to DCS_SIXEL
7056 if (ch == 0x71) {
7057 sixelParseBuffer.setLength(0);
7058 scanState = ScanState.DCS_SIXEL;
7059 } else if ((ch >= 0x40) && (ch <= 0x7E)) {
7060 // 0x40-7E goes to DCS_PASSTHROUGH
7061 scanState = ScanState.DCS_PASSTHROUGH;
7062 }
7063 return;
7064
7065 case DCS_INTERMEDIATE:
7066
7067 // 0x9C goes to GROUND
7068 if (ch == 0x9C) {
7069 toGround();
7070 }
7071
7072 // 0x1B 0x5C goes to GROUND
7073 if (ch == 0x1B) {
7074 collect((char) ch);
7075 }
7076 if (ch == 0x5C) {
7077 if ((collectBuffer.length() > 0)
7078 && (collectBuffer.charAt(collectBuffer.length() - 1) == 0x1B)
7079 ) {
7080 toGround();
7081 }
7082 }
7083
7084 // 0x30-3F goes to DCS_IGNORE
7085 if ((ch >= 0x30) && (ch <= 0x3F)) {
7086 scanState = ScanState.DCS_IGNORE;
7087 }
7088
7089 // 0x40-7E goes to DCS_PASSTHROUGH
7090 if ((ch >= 0x40) && (ch <= 0x7E)) {
7091 scanState = ScanState.DCS_PASSTHROUGH;
7092 }
7093
7094 // 00-17, 19, 1C-1F, 7F --> ignore
7095 return;
7096
7097 case DCS_PARAM:
7098
7099 // 0x9C goes to GROUND
7100 if (ch == 0x9C) {
7101 toGround();
7102 }
7103
7104 // 0x1B 0x5C goes to GROUND
7105 if (ch == 0x1B) {
7106 collect((char) ch);
7107 }
7108 if (ch == 0x5C) {
7109 if ((collectBuffer.length() > 0)
7110 && (collectBuffer.charAt(collectBuffer.length() - 1) == 0x1B)
7111 ) {
7112 toGround();
7113 }
7114 }
7115
7116 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
7117 if ((ch >= 0x20) && (ch <= 0x2F)) {
7118 collect((char) ch);
7119 scanState = ScanState.DCS_INTERMEDIATE;
7120 }
7121
7122 // 30-39, 3B --> param
7123 if ((ch >= '0') && (ch <= '9')) {
7124 param((byte) ch);
7125 }
7126 if (ch == ';') {
7127 param((byte) ch);
7128 }
7129
7130 // 00-17, 19, 1C-1F, 7F --> ignore
7131
7132 // 0x3A, 3C-3F goes to DCS_IGNORE
7133 if (ch == 0x3F) {
7134 scanState = ScanState.DCS_IGNORE;
7135 }
7136 if ((ch >= 0x3C) && (ch <= 0x3F)) {
7137 scanState = ScanState.DCS_IGNORE;
7138 }
7139
7140 // 0x71 goes to DCS_SIXEL
7141 if (ch == 0x71) {
7142 sixelParseBuffer.setLength(0);
7143 scanState = ScanState.DCS_SIXEL;
7144 } else if ((ch >= 0x40) && (ch <= 0x7E)) {
7145 // 0x40-7E goes to DCS_PASSTHROUGH
7146 scanState = ScanState.DCS_PASSTHROUGH;
7147 }
7148 return;
7149
7150 case DCS_PASSTHROUGH:
7151 // 0x9C goes to GROUND
7152 if (ch == 0x9C) {
7153 toGround();
7154 }
7155
7156 // 0x1B 0x5C goes to GROUND
7157 if (ch == 0x1B) {
7158 collect((char) ch);
7159 }
7160 if (ch == 0x5C) {
7161 if ((collectBuffer.length() > 0)
7162 && (collectBuffer.charAt(collectBuffer.length() - 1) == 0x1B)
7163 ) {
7164 toGround();
7165 }
7166 }
7167
7168 // 00-17, 19, 1C-1F, 20-7E --> put
7169 if (ch <= 0x17) {
7170 // We ignore all DCS except sixel.
7171 return;
7172 }
7173 if (ch == 0x19) {
7174 // We ignore all DCS except sixel.
7175 return;
7176 }
7177 if ((ch >= 0x1C) && (ch <= 0x1F)) {
7178 // We ignore all DCS except sixel.
7179 return;
7180 }
7181 if ((ch >= 0x20) && (ch <= 0x7E)) {
7182 // We ignore all DCS except sixel.
7183 return;
7184 }
7185
7186 // 7F --> ignore
7187
7188 return;
7189
7190 case DCS_IGNORE:
7191 // 00-17, 19, 1C-1F, 20-7F --> ignore
7192
7193 // 0x9C goes to GROUND
7194 if (ch == 0x9C) {
7195 toGround();
7196 }
7197
7198 return;
7199
7200 case DCS_SIXEL:
7201 // 0x9C goes to GROUND
7202 if (ch == 0x9C) {
7203 parseSixel();
7204 toGround();
7205 return;
7206 }
7207
7208 // 0x1B 0x5C goes to GROUND
7209 if (ch == 0x1B) {
7210 collect((char) ch);
7211 return;
7212 }
7213 if (ch == 0x5C) {
7214 if ((collectBuffer.length() > 0)
7215 && (collectBuffer.charAt(collectBuffer.length() - 1) == 0x1B)
7216 ) {
7217 parseSixel();
7218 toGround();
7219 return;
7220 }
7221 }
7222
7223 // 00-17, 19, 1C-1F, 20-7E --> put
7224 if ((ch <= 0x17)
7225 || (ch == 0x19)
7226 || ((ch >= 0x1C) && (ch <= 0x1F))
7227 || ((ch >= 0x20) && (ch <= 0x7E))
7228 ) {
7229 sixelParseBuffer.append((char) ch);
7230 }
7231
7232 // 7F --> ignore
7233 return;
7234
7235 case SOSPMAPC_STRING:
7236 // 00-17, 19, 1C-1F, 20-7F --> ignore
7237
7238 // Special case for Jexer: PM can pass one control character
7239 if (ch == 0x1B) {
7240 pmPut((char) ch);
7241 }
7242
7243 if ((ch >= 0x20) && (ch <= 0x7F)) {
7244 pmPut((char) ch);
7245 }
7246
7247 // 0x9C goes to GROUND
7248 if (ch == 0x9C) {
7249 toGround();
7250 }
7251
7252 return;
7253
7254 case OSC_STRING:
7255 // Special case for Xterm: OSC can pass control characters
7256 if ((ch == 0x9C) || (ch == 0x07) || (ch == 0x1B)) {
7257 oscPut((char) ch);
7258 }
7259
7260 // 00-17, 19, 1C-1F --> ignore
7261
7262 // 20-7F --> osc_put
7263 if ((ch >= 0x20) && (ch <= 0x7F)) {
7264 oscPut((char) ch);
7265 }
7266
7267 // 0x9C goes to GROUND
7268 if (ch == 0x9C) {
7269 toGround();
7270 }
7271
7272 return;
7273
7274 case VT52_DIRECT_CURSOR_ADDRESS:
7275 // This is a special case for the VT52 sequence "ESC Y l c"
7276 if (collectBuffer.length() == 0) {
7277 collect((char) ch);
7278 } else if (collectBuffer.length() == 1) {
7279 // We've got the two characters, one in the buffer and the
7280 // other in ch.
7281 cursorPosition(collectBuffer.charAt(0) - '\040', ch - '\040');
7282 toGround();
7283 }
7284 return;
7285 }
7286
7287 }
7288
7289 /**
7290 * Expose current cursor X to outside world.
7291 *
7292 * @return current cursor X
7293 */
7294 public final int getCursorX() {
7295 if (display.get(currentState.cursorY).isDoubleWidth()) {
7296 return currentState.cursorX * 2;
7297 }
7298 return currentState.cursorX;
7299 }
7300
7301 /**
7302 * Expose current cursor Y to outside world.
7303 *
7304 * @return current cursor Y
7305 */
7306 public final int getCursorY() {
7307 return currentState.cursorY;
7308 }
7309
7310 /**
7311 * Returns true if this terminal has requested the mouse pointer be
7312 * hidden.
7313 *
7314 * @return true if this terminal has requested the mouse pointer be
7315 * hidden
7316 */
7317 public final boolean hasHiddenMousePointer() {
7318 return hideMousePointer;
7319 }
7320
7321 /**
7322 * Get the mouse protocol.
7323 *
7324 * @return MouseProtocol.OFF, MouseProtocol.X10, etc.
7325 */
7326 public MouseProtocol getMouseProtocol() {
7327 return mouseProtocol;
7328 }
7329
7330 /**
7331 * Draw the left and right cells of a two-cell-wide (full-width) glyph.
7332 *
7333 * @param leftX the x position to draw the left half to
7334 * @param leftY the y position to draw the left half to
7335 * @param rightX the x position to draw the right half to
7336 * @param rightY the y position to draw the right half to
7337 * @param ch the character to draw
7338 */
7339 private void drawHalves(final int leftX, final int leftY,
7340 final int rightX, final int rightY, final int ch) {
7341
7342 // System.err.println("drawHalves(): " + Integer.toHexString(ch));
7343
7344 if (lastTextHeight != textHeight) {
7345 glyphMaker = GlyphMaker.getInstance(textHeight);
7346 lastTextHeight = textHeight;
7347 }
7348
7349 Cell cell = new Cell(ch, currentState.attr);
7350 BufferedImage image = glyphMaker.getImage(cell, textWidth * 2,
7351 textHeight);
7352 BufferedImage leftImage = image.getSubimage(0, 0, textWidth,
7353 textHeight);
7354 BufferedImage rightImage = image.getSubimage(textWidth, 0, textWidth,
7355 textHeight);
7356
7357 Cell left = new Cell(cell);
7358 left.setImage(leftImage);
7359 left.setWidth(Cell.Width.LEFT);
7360 display.get(leftY).replace(leftX, left);
7361
7362 Cell right = new Cell(cell);
7363 right.setImage(rightImage);
7364 right.setWidth(Cell.Width.RIGHT);
7365 display.get(rightY).replace(rightX, right);
7366 }
7367
7368 /**
7369 * Set the width of a character cell in pixels.
7370 *
7371 * @param textWidth the width in pixels of a character cell
7372 */
7373 public void setTextWidth(final int textWidth) {
7374 this.textWidth = textWidth;
7375 }
7376
7377 /**
7378 * Set the height of a character cell in pixels.
7379 *
7380 * @param textHeight the height in pixels of a character cell
7381 */
7382 public void setTextHeight(final int textHeight) {
7383 this.textHeight = textHeight;
7384 }
7385
7386 /**
7387 * Parse a sixel string into a bitmap image, and overlay that image onto
7388 * the text cells.
7389 */
7390 private void parseSixel() {
7391
7392 /*
7393 System.err.println("parseSixel(): '" + sixelParseBuffer.toString()
7394 + "'");
7395 */
7396
7397 Sixel sixel = new Sixel(sixelParseBuffer.toString(), sixelPalette);
7398 BufferedImage image = sixel.getImage();
7399
7400 // System.err.println("parseSixel(): image " + image);
7401
7402 if (image == null) {
7403 // Sixel data was malformed in some way, bail out.
7404 return;
7405 }
7406 if ((image.getWidth() < 1)
7407 || (image.getWidth() > 10000)
7408 || (image.getHeight() < 1)
7409 || (image.getHeight() > 10000)
7410 ) {
7411 return;
7412 }
7413
7414 imageToCells(image, true);
7415 }
7416
7417 /**
7418 * Parse a "Jexer" RGB image string into a bitmap image, and overlay that
7419 * image onto the text cells.
7420 *
7421 * @param pw width token
7422 * @param ph height token
7423 * @param ps scroll token
7424 * @param data pixel data
7425 */
7426 private void parseJexerImageRGB(final String pw, final String ph,
7427 final String ps, final String data) {
7428
7429 int imageWidth = 0;
7430 int imageHeight = 0;
7431 boolean scroll = false;
7432 try {
7433 imageWidth = Integer.parseInt(pw);
7434 imageHeight = Integer.parseInt(ph);
7435 } catch (NumberFormatException e) {
7436 // SQUASH
7437 return;
7438 }
7439 if ((imageWidth < 1)
7440 || (imageWidth > 10000)
7441 || (imageHeight < 1)
7442 || (imageHeight > 10000)
7443 ) {
7444 return;
7445 }
7446 if (ps.equals("1")) {
7447 scroll = true;
7448 } else if (ps.equals("0")) {
7449 scroll = false;
7450 } else {
7451 return;
7452 }
7453
7454 byte [] bytes = StringUtils.fromBase64(data.getBytes());
7455 if (bytes.length != (imageWidth * imageHeight * 3)) {
7456 return;
7457 }
7458
7459 BufferedImage image = new BufferedImage(imageWidth, imageHeight,
7460 BufferedImage.TYPE_INT_ARGB);
7461
7462 for (int x = 0; x < imageWidth; x++) {
7463 for (int y = 0; y < imageHeight; y++) {
7464 int red = bytes[(y * imageWidth * 3) + (x * 3) ];
7465 if (red < 0) {
7466 red += 256;
7467 }
7468 int green = bytes[(y * imageWidth * 3) + (x * 3) + 1];
7469 if (green < 0) {
7470 green += 256;
7471 }
7472 int blue = bytes[(y * imageWidth * 3) + (x * 3) + 2];
7473 if (blue < 0) {
7474 blue += 256;
7475 }
7476 int rgb = 0xFF000000 | (red << 16) | (green << 8) | blue;
7477 image.setRGB(x, y, rgb);
7478 }
7479 }
7480
7481 imageToCells(image, scroll);
7482 }
7483
7484 /**
7485 * Parse a "Jexer" PNG or JPG image string into a bitmap image, and
7486 * overlay that image onto the text cells.
7487 *
7488 * @param type 1 for PNG, 2 for JPG
7489 * @param ps scroll token
7490 * @param data pixel data
7491 */
7492 private void parseJexerImageFile(final int type, final String ps,
7493 final String data) {
7494
7495 int imageWidth = 0;
7496 int imageHeight = 0;
7497 boolean scroll = false;
7498 BufferedImage image = null;
7499 try {
7500 byte [] bytes = StringUtils.fromBase64(data.getBytes());
7501
7502 switch (type) {
7503 case 1:
7504 if ((bytes[0] != (byte) 0x89)
7505 || (bytes[1] != 'P')
7506 || (bytes[2] != 'N')
7507 || (bytes[3] != 'G')
7508 || (bytes[4] != (byte) 0x0D)
7509 || (bytes[5] != (byte) 0x0A)
7510 || (bytes[6] != (byte) 0x1A)
7511 || (bytes[7] != (byte) 0x0A)
7512 ) {
7513 // File does not have PNG header, bail out.
7514 return;
7515 }
7516 break;
7517
7518 case 2:
7519 if ((bytes[0] != (byte) 0XFF)
7520 || (bytes[1] != (byte) 0xD8)
7521 || (bytes[2] != (byte) 0xFF)
7522 ) {
7523 // File does not have JPG header, bail out.
7524 return;
7525 }
7526 break;
7527
7528 default:
7529 // Unsupported type, bail out.
7530 return;
7531 }
7532
7533 image = ImageIO.read(new ByteArrayInputStream(bytes));
7534 } catch (IOException e) {
7535 // SQUASH
7536 return;
7537 }
7538 assert (image != null);
7539 imageWidth = image.getWidth();
7540 imageHeight = image.getHeight();
7541 if ((imageWidth < 1)
7542 || (imageWidth > 10000)
7543 || (imageHeight < 1)
7544 || (imageHeight > 10000)
7545 ) {
7546 return;
7547 }
7548 if (ps.equals("1")) {
7549 scroll = true;
7550 } else if (ps.equals("0")) {
7551 scroll = false;
7552 } else {
7553 return;
7554 }
7555
7556 imageToCells(image, scroll);
7557 }
7558
7559 /**
7560 * Break up an image into the cells at the current cursor.
7561 *
7562 * @param image the image to display
7563 * @param scroll if true, scroll the image and move the cursor
7564 */
7565 private void imageToCells(final BufferedImage image, final boolean scroll) {
7566 assert (image != null);
7567
7568 /*
7569 * Procedure:
7570 *
7571 * Break up the image into text cell sized pieces as a new array of
7572 * Cells.
7573 *
7574 * Note original column position x0.
7575 *
7576 * For each cell:
7577 *
7578 * 1. Advance (printCharacter(' ')) for horizontal increment, or
7579 * index (linefeed() + cursorPosition(y, x0)) for vertical
7580 * increment.
7581 *
7582 * 2. Set (x, y) cell image data.
7583 *
7584 * 3. For the right and bottom edges:
7585 *
7586 * a. Render the text to pixels using Terminus font.
7587 *
7588 * b. Blit the image on top of the text, using alpha channel.
7589 */
7590 int cellColumns = image.getWidth() / textWidth;
7591 if (cellColumns * textWidth < image.getWidth()) {
7592 cellColumns++;
7593 }
7594 int cellRows = image.getHeight() / textHeight;
7595 if (cellRows * textHeight < image.getHeight()) {
7596 cellRows++;
7597 }
7598
7599 // Break the image up into an array of cells.
7600 Cell [][] cells = new Cell[cellColumns][cellRows];
7601
7602 for (int x = 0; x < cellColumns; x++) {
7603 for (int y = 0; y < cellRows; y++) {
7604
7605 int width = textWidth;
7606 if ((x + 1) * textWidth > image.getWidth()) {
7607 width = image.getWidth() - (x * textWidth);
7608 }
7609 int height = textHeight;
7610 if ((y + 1) * textHeight > image.getHeight()) {
7611 height = image.getHeight() - (y * textHeight);
7612 }
7613
7614 Cell cell = new Cell();
7615 if ((width != textWidth) || (height != textHeight)) {
7616 BufferedImage newImage;
7617 newImage = new BufferedImage(textWidth, textHeight,
7618 BufferedImage.TYPE_INT_ARGB);
7619
7620 Graphics gr = newImage.getGraphics();
7621 gr.drawImage(image.getSubimage(x * textWidth,
7622 y * textHeight, width, height),
7623 0, 0, null, null);
7624 gr.dispose();
7625 cell.setImage(newImage);
7626 } else {
7627 cell.setImage(image.getSubimage(x * textWidth,
7628 y * textHeight, width, height));
7629 }
7630
7631 cells[x][y] = cell;
7632 }
7633 }
7634
7635 int x0 = currentState.cursorX;
7636 int y0 = currentState.cursorY;
7637 for (int y = 0; y < cellRows; y++) {
7638 for (int x = 0; x < cellColumns; x++) {
7639 assert (currentState.cursorX <= rightMargin);
7640
7641 // A real sixel terminal would render the text of the current
7642 // cell first, then image over it (accounting for blank
7643 // pixels). We do not support that. A cell is either text,
7644 // or image, but not a mix of image-over-text.
7645 DisplayLine line = display.get(currentState.cursorY);
7646 line.replace(currentState.cursorX, cells[x][y]);
7647
7648 // If at the end of the visible screen, stop.
7649 if (currentState.cursorX == rightMargin) {
7650 break;
7651 }
7652 // Room for more image on the visible screen.
7653 currentState.cursorX++;
7654 }
7655 if (currentState.cursorY < scrollRegionBottom - 1) {
7656 // Not at the bottom, down a line.
7657 linefeed();
7658 } else if (scroll == true) {
7659 // At the bottom, scroll as needed.
7660 linefeed();
7661 } else {
7662 // At the bottom, no more scrolling, done.
7663 break;
7664 }
7665
7666 cursorPosition(currentState.cursorY, x0);
7667 }
7668
7669 if (scroll == false) {
7670 cursorPosition(y0, x0);
7671 }
7672
7673 }
7674
7675}