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