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