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