run jexer inside jexer
[fanfix.git] / src / jexer / tterminal / ECMA48.java
CommitLineData
34a42e78
KL
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 */
31package jexer.tterminal;
32
33import java.io.InputStream;
34import java.io.InputStreamReader;
35import java.io.IOException;
36import java.io.OutputStream;
37import java.io.OutputStreamWriter;
38import java.io.Reader;
39import java.io.UnsupportedEncodingException;
40import java.io.Writer;
41import java.util.ArrayList;
42import java.util.Collections;
43import java.util.LinkedList;
44import java.util.List;
45
bd8d51fa
KL
46import jexer.TKeypress;
47import jexer.event.TMouseEvent;
34a42e78
KL
48import jexer.bits.Color;
49import jexer.bits.Cell;
50import jexer.bits.CellAttributes;
34a42e78
KL
51import 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>
bd8d51fa
KL
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>
34a42e78
KL
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 */
90public 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 *
bd8d51fa
KL
151 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
152 * @return "vt100", "xterm", etc.
34a42e78 153 */
bd8d51fa
KL
154 public static String deviceTypeTerm(final DeviceType deviceType) {
155 switch (deviceType) {
34a42e78 156 case VT100:
bd8d51fa 157 return "vt100";
34a42e78
KL
158
159 case VT102:
bd8d51fa 160 return "vt102";
34a42e78
KL
161
162 case VT220:
bd8d51fa 163 return "vt220";
34a42e78
KL
164
165 case XTERM:
bd8d51fa 166 return "xterm";
34a42e78
KL
167
168 default:
bd8d51fa
KL
169 throw new IllegalArgumentException("Invalid device type: "
170 + deviceType);
34a42e78
KL
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 *
bd8d51fa 179 * @param deviceType DeviceType.VT100, DeviceType, XTERM, etc.
34a42e78
KL
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 */
bd8d51fa
KL
184 public static String deviceTypeLang(final DeviceType deviceType,
185 final String baseLang) {
34a42e78 186
bd8d51fa 187 switch (deviceType) {
34a42e78 188
bd8d51fa 189 case VT100:
34a42e78 190 case VT102:
34a42e78 191 case VT220:
bd8d51fa 192 return baseLang;
34a42e78
KL
193
194 case XTERM:
bd8d51fa 195 return baseLang + ".UTF-8";
34a42e78
KL
196
197 default:
bd8d51fa
KL
198 throw new IllegalArgumentException("Invalid device type: "
199 + deviceType);
34a42e78
KL
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
bb35d919
KL
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.
34a42e78
KL
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 */
bb35d919 331 private volatile boolean stopReaderThread = false;
34a42e78
KL
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 */
bb35d919 366 private volatile List<DisplayLine> scrollback;
34a42e78
KL
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 */
bb35d919 380 private volatile List<DisplayLine> display;
34a42e78
KL
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
bd8d51fa
KL
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
34a42e78
KL
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
34a42e78
KL
638 /**
639 * Parameter characters being collected.
640 */
641 private List<Integer> csiParams;
642
643 /**
644 * Non-csi collect buffer.
645 */
bd8d51fa 646 private StringBuilder collectBuffer;
34a42e78
KL
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();
bd8d51fa 830 collectBuffer = new StringBuilder(8);
34a42e78
KL
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
bd8d51fa
KL
877 // XTERM
878 mouseProtocol = MouseProtocol.OFF;
879 mouseEncoding = MouseEncoding.X10;
880
34a42e78
KL
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
34a42e78 908 csiParams = new ArrayList<Integer>();
34a42e78
KL
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
bd8d51fa
KL
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) {
a90d3119 1099
bd8d51fa 1100 /*
a90d3119
KL
1101 System.err.printf("mouse(): protocol %s encoding %s mouse %s\n",
1102 mouseProtocol, mouseEncoding, mouse);
bd8d51fa
KL
1103 */
1104
bd8d51fa 1105 if (mouseEncoding != MouseEncoding.UTF8) {
a90d3119
KL
1106 // We will support X10 but only for (160,94) and smaller.
1107 if ((mouse.getX() >= 160) || (mouse.getY() >= 94)) {
1108 return;
1109 }
bd8d51fa 1110 }
bd8d51fa
KL
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');
a90d3119
KL
1161 if (mouse.getType() == TMouseEvent.Type.MOUSE_UP) {
1162 sb.append((char) (0x03 + 32));
1163 } else if (mouse.getMouse1()) {
bd8d51fa
KL
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
34a42e78
KL
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) {
bd8d51fa 2060 collectBuffer.append(ch);
34a42e78
KL
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;
bd8d51fa
KL
2127 for (int i = 0; i < collectBuffer.length(); i++) {
2128 if (collectBuffer.charAt(i) == '?') {
34a42e78 2129 decPrivateModeFlag = true;
bd8d51fa 2130 break;
34a42e78
KL
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
bd8d51fa
KL
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
34a42e78
KL
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
bd8d51fa
KL
2857 for (int i = 0; i < collectBuffer.length(); i++) {
2858 if (collectBuffer.charAt(i) == '?') {
34a42e78 2859 decPrivateModeFlag = true;
bd8d51fa 2860 break;
34a42e78
KL
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
bd8d51fa
KL
2897 for (int i = 0; i < collectBuffer.length(); i++) {
2898 if (collectBuffer.charAt(i) == '?') {
34a42e78 2899 decPrivateModeFlag = true;
bd8d51fa 2900 break;
34a42e78
KL
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;
bd8d51fa
KL
3240 if (collectBuffer.length() > 0) {
3241 String args = collectBuffer.substring(1);
3242 if (collectBuffer.charAt(0) == '>') {
34a42e78 3243 extendedFlag = 1;
bd8d51fa 3244 if (collectBuffer.length() >= 2) {
34a42e78
KL
3245 i = Integer.parseInt(args.toString());
3246 }
bd8d51fa 3247 } else if (collectBuffer.charAt(0) == '=') {
34a42e78 3248 extendedFlag = 2;
bd8d51fa 3249 if (collectBuffer.length() >= 2) {
34a42e78
KL
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() {
36338168 3310 boolean decPrivateModeFlag = false;
34a42e78 3311
36338168
KL
3312 for (int i = 0; i < collectBuffer.length(); i++) {
3313 if (collectBuffer.charAt(i) == '?') {
3314 decPrivateModeFlag = true;
3315 break;
3316 }
34a42e78 3317 }
36338168
KL
3318 if (decPrivateModeFlag) {
3319 // This could be restore DEC private mode values.
3320 // Ignore it.
3321 } else {
3322 // DECSTBM
3323 int top = getCsiParam(0, 1, 1, height) - 1;
3324 int bottom = getCsiParam(1, height, 1, height) - 1;
34a42e78 3325
36338168
KL
3326 if (top > bottom) {
3327 top = bottom;
3328 }
3329 scrollRegionTop = top;
3330 scrollRegionBottom = bottom;
3331
3332 // Home cursor
3333 cursorPosition(0, 0);
3334 }
34a42e78
KL
3335 }
3336
3337 /**
3338 * DECREQTPARM - Request terminal parameters.
3339 */
3340 private void decreqtparm() {
3341 int i = getCsiParam(0, 0);
3342
3343 if ((i != 0) && (i != 1)) {
3344 return;
3345 }
3346
3347 String str = "";
3348
3349 /*
3350 * Request terminal parameters.
3351 *
3352 * Respond with:
3353 *
3354 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3355 * (CLoCk MULtiplier = 1, STP option flags = 0)
3356 *
3357 * (Same as xterm)
3358 */
3359 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
3360 && (s8c1t == true)
3361 ) {
3362 str = String.format("\u009b%d;1;1;128;128;1;0x", i + 2);
3363 } else {
3364 str = String.format("\033[%d;1;1;128;128;1;0x", i + 2);
3365 }
3366 writeRemote(str);
3367 }
3368
3369 /**
3370 * DECSCA - Select Character Attributes.
3371 */
3372 private void decsca() {
3373 int i = getCsiParam(0, 0);
3374
3375 if ((i == 0) || (i == 2)) {
3376 // Protect mode OFF
3377 currentState.attr.setProtect(false);
3378 }
3379 if (i == 1) {
3380 // Protect mode ON
3381 currentState.attr.setProtect(true);
3382 }
3383 }
3384
3385 /**
3386 * DECSTR - Soft Terminal Reset.
3387 */
3388 private void decstr() {
3389 // Do exactly like RIS - Reset to initial state
3390 reset();
3391 // Do I clear screen too? I think so...
3392 eraseScreen(0, 0, height - 1, width - 1, false);
3393 cursorPosition(0, 0);
3394 }
3395
3396 /**
3397 * DSR - Device status report.
3398 */
3399 private void dsr() {
3400 boolean decPrivateModeFlag = false;
3401
bd8d51fa
KL
3402 for (int i = 0; i < collectBuffer.length(); i++) {
3403 if (collectBuffer.charAt(i) == '?') {
34a42e78 3404 decPrivateModeFlag = true;
bd8d51fa 3405 break;
34a42e78
KL
3406 }
3407 }
3408
3409 int i = getCsiParam(0, 0);
3410
3411 switch (i) {
3412
3413 case 5:
3414 // Request status report. Respond with "OK, no malfunction."
3415
3416 // Send string directly to remote side
3417 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
3418 && (s8c1t == true)
3419 ) {
3420 writeRemote("\u009b0n");
3421 } else {
3422 writeRemote("\033[0n");
3423 }
3424 break;
3425
3426 case 6:
3427 // Request cursor position. Respond with current position.
3428 String str = "";
3429 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
3430 && (s8c1t == true)
3431 ) {
3432 str = String.format("\u009b%d;%dR",
3433 currentState.cursorY + 1, currentState.cursorX + 1);
3434 } else {
3435 str = String.format("\033[%d;%dR",
3436 currentState.cursorY + 1, currentState.cursorX + 1);
3437 }
3438
3439 // Send string directly to remote side
3440 writeRemote(str);
3441 break;
3442
3443 case 15:
3444 if (decPrivateModeFlag == true) {
3445
3446 // Request printer status report. Respond with "Printer not
3447 // connected."
3448
3449 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
3450 && (s8c1t == true)) {
3451 writeRemote("\u009b?13n");
3452 } else {
3453 writeRemote("\033[?13n");
3454 }
3455 }
3456 break;
3457
3458 case 25:
3459 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
3460 && (decPrivateModeFlag == true)
3461 ) {
3462
3463 // Request user-defined keys are locked or unlocked. Respond
3464 // with "User-defined keys are locked."
3465
3466 if (s8c1t == true) {
3467 writeRemote("\u009b?21n");
3468 } else {
3469 writeRemote("\033[?21n");
3470 }
3471 }
3472 break;
3473
3474 case 26:
3475 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
3476 && (decPrivateModeFlag == true)
3477 ) {
3478
3479 // Request keyboard language. Respond with "Keyboard
3480 // language is North American."
3481
3482 if (s8c1t == true) {
3483 writeRemote("\u009b?27;1n");
3484 } else {
3485 writeRemote("\033[?27;1n");
3486 }
3487
3488 }
3489 break;
3490
3491 default:
3492 // Some other option, ignore
3493 break;
3494 }
3495 }
3496
3497 /**
3498 * TBC - Tabulation clear.
3499 */
3500 private void tbc() {
3501 int i = getCsiParam(0, 0);
3502 if (i == 0) {
3503 List<Integer> newStops = new ArrayList<Integer>();
3504 for (Integer stop: tabStops) {
3505 if (stop == currentState.cursorX) {
3506 continue;
3507 }
3508 newStops.add(stop);
3509 }
3510 tabStops = newStops;
3511 }
3512 if (i == 3) {
3513 tabStops.clear();
3514 }
3515 }
3516
3517 /**
3518 * Erase the characters in the current line from the start column to the
3519 * end column, inclusive.
3520 *
3521 * @param start starting column to erase (between 0 and width - 1)
3522 * @param end ending column to erase (between 0 and width - 1)
3523 * @param honorProtected if true, do not erase characters with the
3524 * protected attribute set
3525 */
3526 private void eraseLine(int start, int end, final boolean honorProtected) {
3527
3528 if (start > end) {
3529 return;
3530 }
3531 if (end > width - 1) {
3532 end = width - 1;
3533 }
3534 if (start < 0) {
3535 start = 0;
3536 }
3537
3538 for (int i = start; i <= end; i++) {
3539 DisplayLine line = display.get(currentState.cursorY);
3540 if ((!honorProtected)
3541 || ((honorProtected) && (!line.charAt(i).getProtect()))) {
3542
3543 switch (type) {
3544 case VT100:
3545 case VT102:
3546 case VT220:
3547 /*
3548 * From the VT102 manual:
3549 *
3550 * Erasing a character also erases any character
3551 * attribute of the character.
3552 */
3553 line.setBlank(i);
3554 break;
3555 case XTERM:
3556 /*
3557 * Erase with the current color a.k.a. back-color erase
3558 * (bce).
3559 */
3560 line.setChar(i, ' ');
3561 line.setAttr(i, currentState.attr);
3562 break;
3563 }
3564 }
3565 }
3566 }
3567
3568 /**
3569 * Erase a rectangular section of the screen, inclusive. end column,
3570 * inclusive.
3571 *
3572 * @param startRow starting row to erase (between 0 and height - 1)
3573 * @param startCol starting column to erase (between 0 and width - 1)
3574 * @param endRow ending row to erase (between 0 and height - 1)
3575 * @param endCol ending column to erase (between 0 and width - 1)
3576 * @param honorProtected if true, do not erase characters with the
3577 * protected attribute set
3578 */
3579 private void eraseScreen(final int startRow, final int startCol,
3580 final int endRow, final int endCol, final boolean honorProtected) {
3581
3582 int oldCursorY;
3583
3584 if ((startRow < 0)
3585 || (startCol < 0)
3586 || (endRow < 0)
3587 || (endCol < 0)
3588 || (endRow < startRow)
3589 || (endCol < startCol)
3590 ) {
3591 return;
3592 }
3593
3594 oldCursorY = currentState.cursorY;
3595 for (int i = startRow; i <= endRow; i++) {
3596 currentState.cursorY = i;
3597 eraseLine(startCol, endCol, honorProtected);
3598
3599 // Erase display clears the double attributes
3600 display.get(i).setDoubleWidth(false);
3601 display.get(i).setDoubleHeight(0);
3602 }
3603 currentState.cursorY = oldCursorY;
3604 }
3605
3606 /**
3607 * VT220 printer functions. All of these are parsed, but won't do
3608 * anything.
3609 */
3610 private void printerFunctions() {
3611 boolean decPrivateModeFlag = false;
bd8d51fa
KL
3612 for (int i = 0; i < collectBuffer.length(); i++) {
3613 if (collectBuffer.charAt(i) == '?') {
34a42e78 3614 decPrivateModeFlag = true;
bd8d51fa 3615 break;
34a42e78
KL
3616 }
3617 }
3618
3619 int i = getCsiParam(0, 0);
3620
3621 switch (i) {
3622
3623 case 0:
3624 if (decPrivateModeFlag == false) {
3625 // Print screen
3626 }
3627 break;
3628
3629 case 1:
3630 if (decPrivateModeFlag == true) {
3631 // Print cursor line
3632 }
3633 break;
3634
3635 case 4:
3636 if (decPrivateModeFlag == true) {
3637 // Auto print mode OFF
3638 } else {
3639 // Printer controller OFF
3640
3641 // Characters re-appear on the screen
3642 printerControllerMode = false;
3643 }
3644 break;
3645
3646 case 5:
3647 if (decPrivateModeFlag == true) {
3648 // Auto print mode
3649
3650 } else {
3651 // Printer controller
3652
3653 // Characters get sucked into oblivion
3654 printerControllerMode = true;
3655 }
3656 break;
3657
3658 default:
3659 break;
3660
3661 }
3662 }
3663
3664 /**
3665 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
3666 * of remote systems will send an XTerm title sequence even if TERM isn't
3667 * xterm.
3668 *
3669 * @param xtermChar the character received from the remote side
3670 */
3671 private void oscPut(final char xtermChar) {
3672 // Collect first
bd8d51fa 3673 collectBuffer.append(xtermChar);
34a42e78
KL
3674
3675 // Xterm cases...
3676 if (xtermChar == 0x07) {
bd8d51fa
KL
3677 String args = collectBuffer.substring(0,
3678 collectBuffer.length() - 1);
34a42e78
KL
3679 String [] p = args.toString().split(";");
3680 if (p.length > 0) {
3681 if ((p[0].equals("0")) || (p[0].equals("2"))) {
3682 if (p.length > 1) {
3683 // Screen title
3684 screenTitle = p[1];
3685 }
3686 }
3687 }
3688
3689 // Go to SCAN_GROUND state
3690 toGround();
3691 return;
3692 }
3693 }
3694
3695 /**
3696 * Run this input character through the ECMA48 state machine.
3697 *
3698 * @param ch character from the remote side
3699 */
3700 public void consume(char ch) {
3701
3702 // DEBUG
3703 // System.err.printf("%c", ch);
3704
3705 // Special case for VT10x: 7-bit characters only
3706 if ((type == DeviceType.VT100) || (type == DeviceType.VT102)) {
3707 ch = (char)(ch & 0x7F);
3708 }
3709
3710 // Special "anywhere" states
3711
3712 // 18, 1A --> execute, then switch to SCAN_GROUND
3713 if ((ch == 0x18) || (ch == 0x1A)) {
3714 // CAN and SUB abort escape sequences
3715 toGround();
3716 return;
3717 }
3718
3719 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
3720
3721 // 0x1B == ESCAPE
3722 if ((ch == 0x1B)
3723 && (scanState != ScanState.DCS_ENTRY)
3724 && (scanState != ScanState.DCS_INTERMEDIATE)
3725 && (scanState != ScanState.DCS_IGNORE)
3726 && (scanState != ScanState.DCS_PARAM)
3727 && (scanState != ScanState.DCS_PASSTHROUGH)
3728 ) {
3729
3730 scanState = ScanState.ESCAPE;
3731 return;
3732 }
3733
3734 // 0x9B == CSI 8-bit sequence
3735 if (ch == 0x9B) {
3736 scanState = ScanState.CSI_ENTRY;
3737 return;
3738 }
3739
3740 // 0x9D goes to ScanState.OSC_STRING
3741 if (ch == 0x9D) {
3742 scanState = ScanState.OSC_STRING;
3743 return;
3744 }
3745
3746 // 0x90 goes to DCS_ENTRY
3747 if (ch == 0x90) {
3748 scanState = ScanState.DCS_ENTRY;
3749 return;
3750 }
3751
3752 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
3753 if ((ch == 0x98) || (ch == 0x9E) || (ch == 0x9F)) {
3754 scanState = ScanState.SOSPMAPC_STRING;
3755 return;
3756 }
3757
3758 // 0x7F (DEL) is always discarded
3759 if (ch == 0x7F) {
3760 return;
3761 }
3762
3763 switch (scanState) {
3764
3765 case GROUND:
3766 // 00-17, 19, 1C-1F --> execute
3767 // 80-8F, 91-9A, 9C --> execute
3768 if ((ch <= 0x1F) || ((ch >= 0x80) && (ch <= 0x9F))) {
3769 handleControlChar(ch);
3770 }
3771
3772 // 20-7F --> print
3773 if (((ch >= 0x20) && (ch <= 0x7F))
3774 || (ch >= 0xA0)
3775 ) {
3776
3777 // VT220 printer --> trash bin
3778 if (((type == DeviceType.VT220)
3779 || (type == DeviceType.XTERM))
3780 && (printerControllerMode == true)
3781 ) {
3782 return;
3783 }
3784
3785 // Hang onto this character
3786 repCh = mapCharacter(ch);
3787
3788 // Print this character
3789 printCharacter(repCh);
3790 }
3791 return;
3792
3793 case ESCAPE:
3794 // 00-17, 19, 1C-1F --> execute
3795 if (ch <= 0x1F) {
3796 handleControlChar(ch);
3797 return;
3798 }
3799
3800 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
3801 if ((ch >= 0x20) && (ch <= 0x2F)) {
3802 collect(ch);
3803 scanState = ScanState.ESCAPE_INTERMEDIATE;
3804 return;
3805 }
3806
3807 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
3808 if ((ch >= 0x30) && (ch <= 0x4F)) {
3809 switch (ch) {
3810 case '0':
3811 case '1':
3812 case '2':
3813 case '3':
3814 case '4':
3815 case '5':
3816 case '6':
3817 break;
3818 case '7':
3819 // DECSC - Save cursor
3820 // Note this code overlaps both ANSI and VT52 mode
3821 decsc();
3822 break;
3823
3824 case '8':
3825 // DECRC - Restore cursor
3826 // Note this code overlaps both ANSI and VT52 mode
3827 decrc();
3828 break;
3829
3830 case '9':
3831 case ':':
3832 case ';':
3833 break;
3834 case '<':
3835 if (vt52Mode == true) {
3836 // DECANM - Enter ANSI mode
3837 vt52Mode = false;
3838 arrowKeyMode = ArrowKeyMode.VT100;
3839
3840 /*
3841 * From the VT102 docs: "You use ANSI mode to select
3842 * most terminal features; the terminal uses the same
3843 * features when it switches to VT52 mode. You
3844 * cannot, however, change most of these features in
3845 * VT52 mode."
3846 *
3847 * In other words, do not reset any other attributes
3848 * when switching between VT52 submode and ANSI.
3849 */
3850
3851 // Reset fonts
3852 currentState.g0Charset = CharacterSet.US;
3853 currentState.g1Charset = CharacterSet.DRAWING;
3854 s8c1t = false;
3855 singleshift = Singleshift.NONE;
3856 currentState.glLockshift = LockshiftMode.NONE;
3857 currentState.grLockshift = LockshiftMode.NONE;
3858 }
3859 break;
3860 case '=':
3861 // DECKPAM - Keypad application mode
3862 // Note this code overlaps both ANSI and VT52 mode
3863 deckpam();
3864 break;
3865 case '>':
3866 // DECKPNM - Keypad numeric mode
3867 // Note this code overlaps both ANSI and VT52 mode
3868 deckpnm();
3869 break;
3870 case '?':
3871 case '@':
3872 break;
3873 case 'A':
3874 if (vt52Mode == true) {
3875 // Cursor up, and stop at the top without scrolling
3876 cursorUp(1, false);
3877 }
3878 break;
3879 case 'B':
3880 if (vt52Mode == true) {
3881 // Cursor down, and stop at the bottom without scrolling
3882 cursorDown(1, false);
3883 }
3884 break;
3885 case 'C':
3886 if (vt52Mode == true) {
3887 // Cursor right, and stop at the right without scrolling
3888 cursorRight(1, false);
3889 }
3890 break;
3891 case 'D':
3892 if (vt52Mode == true) {
3893 // Cursor left, and stop at the left without scrolling
3894 cursorLeft(1, false);
3895 } else {
3896 // IND - Index
3897 ind();
3898 }
3899 break;
3900 case 'E':
3901 if (vt52Mode == true) {
3902 // Nothing
3903 } else {
3904 // NEL - Next line
3905 nel();
3906 }
3907 break;
3908 case 'F':
3909 if (vt52Mode == true) {
3910 // G0 --> Special graphics
3911 currentState.g0Charset = CharacterSet.VT52_GRAPHICS;
3912 }
3913 break;
3914 case 'G':
3915 if (vt52Mode == true) {
3916 // G0 --> ASCII set
3917 currentState.g0Charset = CharacterSet.US;
3918 }
3919 break;
3920 case 'H':
3921 if (vt52Mode == true) {
3922 // Cursor to home
3923 cursorPosition(0, 0);
3924 } else {
3925 // HTS - Horizontal tabulation set
3926 hts();
3927 }
3928 break;
3929 case 'I':
3930 if (vt52Mode == true) {
3931 // Reverse line feed. Same as RI.
3932 ri();
3933 }
3934 break;
3935 case 'J':
3936 if (vt52Mode == true) {
3937 // Erase to end of screen
3938 eraseLine(currentState.cursorX, width - 1, false);
3939 eraseScreen(currentState.cursorY + 1, 0, height - 1,
3940 width - 1, false);
3941 }
3942 break;
3943 case 'K':
3944 if (vt52Mode == true) {
3945 // Erase to end of line
3946 eraseLine(currentState.cursorX, width - 1, false);
3947 }
3948 break;
3949 case 'L':
3950 break;
3951 case 'M':
3952 if (vt52Mode == true) {
3953 // Nothing
3954 } else {
3955 // RI - Reverse index
3956 ri();
3957 }
3958 break;
3959 case 'N':
3960 if (vt52Mode == false) {
3961 // SS2
3962 singleshift = Singleshift.SS2;
3963 }
3964 break;
3965 case 'O':
3966 if (vt52Mode == false) {
3967 // SS3
3968 singleshift = Singleshift.SS3;
3969 }
3970 break;
3971 }
3972 toGround();
3973 return;
3974 }
3975 if ((ch >= 0x51) && (ch <= 0x57)) {
3976 switch (ch) {
3977 case 'Q':
3978 case 'R':
3979 case 'S':
3980 case 'T':
3981 case 'U':
3982 case 'V':
3983 case 'W':
3984 break;
3985 }
3986 toGround();
3987 return;
3988 }
3989 if (ch == 0x59) {
3990 // 'Y'
3991 if (vt52Mode == true) {
3992 scanState = ScanState.VT52_DIRECT_CURSOR_ADDRESS;
3993 } else {
3994 toGround();
3995 }
3996 return;
3997 }
3998 if (ch == 0x5A) {
3999 // 'Z'
4000 if (vt52Mode == true) {
4001 // Identify
4002 // Send string directly to remote side
4003 writeRemote("\033/Z");
4004 } else {
4005 // DECID
4006 // Send string directly to remote side
4007 writeRemote(deviceTypeResponse());
4008 }
4009 toGround();
4010 return;
4011 }
4012 if (ch == 0x5C) {
4013 // '\'
4014 toGround();
4015 return;
4016 }
4017
4018 // VT52 cannot get to any of these other states
4019 if (vt52Mode == true) {
4020 toGround();
4021 return;
4022 }
4023
4024 if ((ch >= 0x60) && (ch <= 0x7E)) {
4025 switch (ch) {
4026 case '`':
4027 case 'a':
4028 case 'b':
4029 break;
4030 case 'c':
4031 // RIS - Reset to initial state
4032 reset();
4033 // Do I clear screen too? I think so...
4034 eraseScreen(0, 0, height - 1, width - 1, false);
4035 cursorPosition(0, 0);
4036 break;
4037 case 'd':
4038 case 'e':
4039 case 'f':
4040 case 'g':
4041 case 'h':
4042 case 'i':
4043 case 'j':
4044 case 'k':
4045 case 'l':
4046 case 'm':
4047 break;
4048 case 'n':
4049 if ((type == DeviceType.VT220)
4050 || (type == DeviceType.XTERM)) {
4051
4052 // VT220 lockshift G2 into GL
4053 currentState.glLockshift = LockshiftMode.G2_GL;
4054 shiftOut = false;
4055 }
4056 break;
4057 case 'o':
4058 if ((type == DeviceType.VT220)
4059 || (type == DeviceType.XTERM)) {
4060
4061 // VT220 lockshift G3 into GL
4062 currentState.glLockshift = LockshiftMode.G3_GL;
4063 shiftOut = false;
4064 }
4065 break;
4066 case 'p':
4067 case 'q':
4068 case 'r':
4069 case 's':
4070 case 't':
4071 case 'u':
4072 case 'v':
4073 case 'w':
4074 case 'x':
4075 case 'y':
4076 case 'z':
4077 case '{':
4078 break;
4079 case '|':
4080 if ((type == DeviceType.VT220)
4081 || (type == DeviceType.XTERM)) {
4082
4083 // VT220 lockshift G3 into GR
4084 currentState.grLockshift = LockshiftMode.G3_GR;
4085 shiftOut = false;
4086 }
4087 break;
4088 case '}':
4089 if ((type == DeviceType.VT220)
4090 || (type == DeviceType.XTERM)) {
4091
4092 // VT220 lockshift G2 into GR
4093 currentState.grLockshift = LockshiftMode.G2_GR;
4094 shiftOut = false;
4095 }
4096 break;
4097
4098 case '~':
4099 if ((type == DeviceType.VT220)
4100 || (type == DeviceType.XTERM)) {
4101
4102 // VT220 lockshift G1 into GR
4103 currentState.grLockshift = LockshiftMode.G1_GR;
4104 shiftOut = false;
4105 }
4106 break;
4107 }
4108 toGround();
4109 }
4110
4111 // 7F --> ignore
4112
4113 // 0x5B goes to CSI_ENTRY
4114 if (ch == 0x5B) {
4115 scanState = ScanState.CSI_ENTRY;
4116 }
4117
4118 // 0x5D goes to OSC_STRING
4119 if (ch == 0x5D) {
4120 scanState = ScanState.OSC_STRING;
4121 }
4122
4123 // 0x50 goes to DCS_ENTRY
4124 if (ch == 0x50) {
4125 scanState = ScanState.DCS_ENTRY;
4126 }
4127
4128 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
4129 if ((ch == 0x58) || (ch == 0x5E) || (ch == 0x5F)) {
4130 scanState = ScanState.SOSPMAPC_STRING;
4131 }
4132
4133 return;
4134
4135 case ESCAPE_INTERMEDIATE:
4136 // 00-17, 19, 1C-1F --> execute
4137 if (ch <= 0x1F) {
4138 handleControlChar(ch);
4139 }
4140
4141 // 20-2F --> collect
4142 if ((ch >= 0x20) && (ch <= 0x2F)) {
4143 collect(ch);
4144 }
4145
4146 // 30-7E --> dispatch, then switch to GROUND
4147 if ((ch >= 0x30) && (ch <= 0x7E)) {
4148 switch (ch) {
4149 case '0':
bd8d51fa
KL
4150 if ((collectBuffer.length() == 1)
4151 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4152 // G0 --> Special graphics
4153 currentState.g0Charset = CharacterSet.DRAWING;
4154 }
bd8d51fa
KL
4155 if ((collectBuffer.length() == 1)
4156 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4157 // G1 --> Special graphics
4158 currentState.g1Charset = CharacterSet.DRAWING;
4159 }
4160 if ((type == DeviceType.VT220)
4161 || (type == DeviceType.XTERM)) {
4162
bd8d51fa
KL
4163 if ((collectBuffer.length() == 1)
4164 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4165 // G2 --> Special graphics
4166 currentState.g2Charset = CharacterSet.DRAWING;
4167 }
bd8d51fa
KL
4168 if ((collectBuffer.length() == 1)
4169 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4170 // G3 --> Special graphics
4171 currentState.g3Charset = CharacterSet.DRAWING;
4172 }
4173 }
4174 break;
4175 case '1':
bd8d51fa
KL
4176 if ((collectBuffer.length() == 1)
4177 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4178 // G0 --> Alternate character ROM standard character set
4179 currentState.g0Charset = CharacterSet.ROM;
4180 }
bd8d51fa
KL
4181 if ((collectBuffer.length() == 1)
4182 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4183 // G1 --> Alternate character ROM standard character set
4184 currentState.g1Charset = CharacterSet.ROM;
4185 }
4186 break;
4187 case '2':
bd8d51fa
KL
4188 if ((collectBuffer.length() == 1)
4189 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4190 // G0 --> Alternate character ROM special graphics
4191 currentState.g0Charset = CharacterSet.ROM_SPECIAL;
4192 }
bd8d51fa
KL
4193 if ((collectBuffer.length() == 1)
4194 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4195 // G1 --> Alternate character ROM special graphics
4196 currentState.g1Charset = CharacterSet.ROM_SPECIAL;
4197 }
4198 break;
4199 case '3':
bd8d51fa
KL
4200 if ((collectBuffer.length() == 1)
4201 && (collectBuffer.charAt(0) == '#')) {
34a42e78
KL
4202 // DECDHL - Double-height line (top half)
4203 dechdl(true);
4204 }
4205 break;
4206 case '4':
bd8d51fa
KL
4207 if ((collectBuffer.length() == 1)
4208 && (collectBuffer.charAt(0) == '#')) {
34a42e78
KL
4209 // DECDHL - Double-height line (bottom half)
4210 dechdl(false);
4211 }
4212 if ((type == DeviceType.VT220)
4213 || (type == DeviceType.XTERM)) {
4214
bd8d51fa
KL
4215 if ((collectBuffer.length() == 1)
4216 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4217 // G0 --> DUTCH
4218 currentState.g0Charset = CharacterSet.NRC_DUTCH;
4219 }
bd8d51fa
KL
4220 if ((collectBuffer.length() == 1)
4221 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4222 // G1 --> DUTCH
4223 currentState.g1Charset = CharacterSet.NRC_DUTCH;
4224 }
bd8d51fa
KL
4225 if ((collectBuffer.length() == 1)
4226 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4227 // G2 --> DUTCH
4228 currentState.g2Charset = CharacterSet.NRC_DUTCH;
4229 }
bd8d51fa
KL
4230 if ((collectBuffer.length() == 1)
4231 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4232 // G3 --> DUTCH
4233 currentState.g3Charset = CharacterSet.NRC_DUTCH;
4234 }
4235 }
4236 break;
4237 case '5':
bd8d51fa
KL
4238 if ((collectBuffer.length() == 1)
4239 && (collectBuffer.charAt(0) == '#')) {
34a42e78
KL
4240 // DECSWL - Single-width line
4241 decswl();
4242 }
4243 if ((type == DeviceType.VT220)
4244 || (type == DeviceType.XTERM)) {
4245
bd8d51fa
KL
4246 if ((collectBuffer.length() == 1)
4247 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4248 // G0 --> FINNISH
4249 currentState.g0Charset = CharacterSet.NRC_FINNISH;
4250 }
bd8d51fa
KL
4251 if ((collectBuffer.length() == 1)
4252 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4253 // G1 --> FINNISH
4254 currentState.g1Charset = CharacterSet.NRC_FINNISH;
4255 }
bd8d51fa
KL
4256 if ((collectBuffer.length() == 1)
4257 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4258 // G2 --> FINNISH
4259 currentState.g2Charset = CharacterSet.NRC_FINNISH;
4260 }
bd8d51fa
KL
4261 if ((collectBuffer.length() == 1)
4262 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4263 // G3 --> FINNISH
4264 currentState.g3Charset = CharacterSet.NRC_FINNISH;
4265 }
4266 }
4267 break;
4268 case '6':
bd8d51fa
KL
4269 if ((collectBuffer.length() == 1)
4270 && (collectBuffer.charAt(0) == '#')) {
34a42e78
KL
4271 // DECDWL - Double-width line
4272 decdwl();
4273 }
4274 if ((type == DeviceType.VT220)
4275 || (type == DeviceType.XTERM)) {
4276
bd8d51fa
KL
4277 if ((collectBuffer.length() == 1)
4278 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4279 // G0 --> NORWEGIAN
4280 currentState.g0Charset = CharacterSet.NRC_NORWEGIAN;
4281 }
bd8d51fa
KL
4282 if ((collectBuffer.length() == 1)
4283 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4284 // G1 --> NORWEGIAN
4285 currentState.g1Charset = CharacterSet.NRC_NORWEGIAN;
4286 }
bd8d51fa
KL
4287 if ((collectBuffer.length() == 1)
4288 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4289 // G2 --> NORWEGIAN
4290 currentState.g2Charset = CharacterSet.NRC_NORWEGIAN;
4291 }
bd8d51fa
KL
4292 if ((collectBuffer.length() == 1)
4293 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4294 // G3 --> NORWEGIAN
4295 currentState.g3Charset = CharacterSet.NRC_NORWEGIAN;
4296 }
4297 }
4298 break;
4299 case '7':
4300 if ((type == DeviceType.VT220)
4301 || (type == DeviceType.XTERM)) {
4302
bd8d51fa
KL
4303 if ((collectBuffer.length() == 1)
4304 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4305 // G0 --> SWEDISH
4306 currentState.g0Charset = CharacterSet.NRC_SWEDISH;
4307 }
bd8d51fa
KL
4308 if ((collectBuffer.length() == 1)
4309 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4310 // G1 --> SWEDISH
4311 currentState.g1Charset = CharacterSet.NRC_SWEDISH;
4312 }
bd8d51fa
KL
4313 if ((collectBuffer.length() == 1)
4314 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4315 // G2 --> SWEDISH
4316 currentState.g2Charset = CharacterSet.NRC_SWEDISH;
4317 }
bd8d51fa
KL
4318 if ((collectBuffer.length() == 1)
4319 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4320 // G3 --> SWEDISH
4321 currentState.g3Charset = CharacterSet.NRC_SWEDISH;
4322 }
4323 }
4324 break;
4325 case '8':
bd8d51fa
KL
4326 if ((collectBuffer.length() == 1)
4327 && (collectBuffer.charAt(0) == '#')) {
34a42e78
KL
4328 // DECALN - Screen alignment display
4329 decaln();
4330 }
4331 break;
4332 case '9':
4333 case ':':
4334 case ';':
4335 break;
4336 case '<':
4337 if ((type == DeviceType.VT220)
4338 || (type == DeviceType.XTERM)) {
4339
bd8d51fa
KL
4340 if ((collectBuffer.length() == 1)
4341 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4342 // G0 --> DEC_SUPPLEMENTAL
4343 currentState.g0Charset = CharacterSet.DEC_SUPPLEMENTAL;
4344 }
bd8d51fa
KL
4345 if ((collectBuffer.length() == 1)
4346 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4347 // G1 --> DEC_SUPPLEMENTAL
4348 currentState.g1Charset = CharacterSet.DEC_SUPPLEMENTAL;
4349 }
bd8d51fa
KL
4350 if ((collectBuffer.length() == 1)
4351 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4352 // G2 --> DEC_SUPPLEMENTAL
4353 currentState.g2Charset = CharacterSet.DEC_SUPPLEMENTAL;
4354 }
bd8d51fa
KL
4355 if ((collectBuffer.length() == 1)
4356 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4357 // G3 --> DEC_SUPPLEMENTAL
4358 currentState.g3Charset = CharacterSet.DEC_SUPPLEMENTAL;
4359 }
4360 }
4361 break;
4362 case '=':
4363 if ((type == DeviceType.VT220)
4364 || (type == DeviceType.XTERM)) {
4365
bd8d51fa
KL
4366 if ((collectBuffer.length() == 1)
4367 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4368 // G0 --> SWISS
4369 currentState.g0Charset = CharacterSet.NRC_SWISS;
4370 }
bd8d51fa
KL
4371 if ((collectBuffer.length() == 1)
4372 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4373 // G1 --> SWISS
4374 currentState.g1Charset = CharacterSet.NRC_SWISS;
4375 }
bd8d51fa
KL
4376 if ((collectBuffer.length() == 1)
4377 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4378 // G2 --> SWISS
4379 currentState.g2Charset = CharacterSet.NRC_SWISS;
4380 }
bd8d51fa
KL
4381 if ((collectBuffer.length() == 1)
4382 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4383 // G3 --> SWISS
4384 currentState.g3Charset = CharacterSet.NRC_SWISS;
4385 }
4386 }
4387 break;
4388 case '>':
4389 case '?':
4390 case '@':
4391 break;
4392 case 'A':
bd8d51fa
KL
4393 if ((collectBuffer.length() == 1)
4394 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4395 // G0 --> United Kingdom set
4396 currentState.g0Charset = CharacterSet.UK;
4397 }
bd8d51fa
KL
4398 if ((collectBuffer.length() == 1)
4399 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4400 // G1 --> United Kingdom set
4401 currentState.g1Charset = CharacterSet.UK;
4402 }
4403 if ((type == DeviceType.VT220)
4404 || (type == DeviceType.XTERM)) {
4405
bd8d51fa
KL
4406 if ((collectBuffer.length() == 1)
4407 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4408 // G2 --> United Kingdom set
4409 currentState.g2Charset = CharacterSet.UK;
4410 }
bd8d51fa
KL
4411 if ((collectBuffer.length() == 1)
4412 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4413 // G3 --> United Kingdom set
4414 currentState.g3Charset = CharacterSet.UK;
4415 }
4416 }
4417 break;
4418 case 'B':
bd8d51fa
KL
4419 if ((collectBuffer.length() == 1)
4420 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4421 // G0 --> ASCII set
4422 currentState.g0Charset = CharacterSet.US;
4423 }
bd8d51fa
KL
4424 if ((collectBuffer.length() == 1)
4425 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4426 // G1 --> ASCII set
4427 currentState.g1Charset = CharacterSet.US;
4428 }
4429 if ((type == DeviceType.VT220)
4430 || (type == DeviceType.XTERM)) {
4431
bd8d51fa
KL
4432 if ((collectBuffer.length() == 1)
4433 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4434 // G2 --> ASCII
4435 currentState.g2Charset = CharacterSet.US;
4436 }
bd8d51fa
KL
4437 if ((collectBuffer.length() == 1)
4438 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4439 // G3 --> ASCII
4440 currentState.g3Charset = CharacterSet.US;
4441 }
4442 }
4443 break;
4444 case 'C':
4445 if ((type == DeviceType.VT220)
4446 || (type == DeviceType.XTERM)) {
4447
bd8d51fa
KL
4448 if ((collectBuffer.length() == 1)
4449 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4450 // G0 --> FINNISH
4451 currentState.g0Charset = CharacterSet.NRC_FINNISH;
4452 }
bd8d51fa
KL
4453 if ((collectBuffer.length() == 1)
4454 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4455 // G1 --> FINNISH
4456 currentState.g1Charset = CharacterSet.NRC_FINNISH;
4457 }
bd8d51fa
KL
4458 if ((collectBuffer.length() == 1)
4459 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4460 // G2 --> FINNISH
4461 currentState.g2Charset = CharacterSet.NRC_FINNISH;
4462 }
bd8d51fa
KL
4463 if ((collectBuffer.length() == 1)
4464 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4465 // G3 --> FINNISH
4466 currentState.g3Charset = CharacterSet.NRC_FINNISH;
4467 }
4468 }
4469 break;
4470 case 'D':
4471 break;
4472 case 'E':
4473 if ((type == DeviceType.VT220)
4474 || (type == DeviceType.XTERM)) {
4475
bd8d51fa
KL
4476 if ((collectBuffer.length() == 1)
4477 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4478 // G0 --> NORWEGIAN
4479 currentState.g0Charset = CharacterSet.NRC_NORWEGIAN;
4480 }
bd8d51fa
KL
4481 if ((collectBuffer.length() == 1)
4482 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4483 // G1 --> NORWEGIAN
4484 currentState.g1Charset = CharacterSet.NRC_NORWEGIAN;
4485 }
bd8d51fa
KL
4486 if ((collectBuffer.length() == 1)
4487 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4488 // G2 --> NORWEGIAN
4489 currentState.g2Charset = CharacterSet.NRC_NORWEGIAN;
4490 }
bd8d51fa
KL
4491 if ((collectBuffer.length() == 1)
4492 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4493 // G3 --> NORWEGIAN
4494 currentState.g3Charset = CharacterSet.NRC_NORWEGIAN;
4495 }
4496 }
4497 break;
4498 case 'F':
4499 if ((type == DeviceType.VT220)
4500 || (type == DeviceType.XTERM)) {
4501
bd8d51fa
KL
4502 if ((collectBuffer.length() == 1)
4503 && (collectBuffer.charAt(0) == ' ')) {
34a42e78
KL
4504 // S7C1T
4505 s8c1t = false;
4506 }
4507 }
4508 break;
4509 case 'G':
4510 if ((type == DeviceType.VT220)
4511 || (type == DeviceType.XTERM)) {
4512
bd8d51fa
KL
4513 if ((collectBuffer.length() == 1)
4514 && (collectBuffer.charAt(0) == ' ')) {
34a42e78
KL
4515 // S8C1T
4516 s8c1t = true;
4517 }
4518 }
4519 break;
4520 case 'H':
4521 if ((type == DeviceType.VT220)
4522 || (type == DeviceType.XTERM)) {
4523
bd8d51fa
KL
4524 if ((collectBuffer.length() == 1)
4525 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4526 // G0 --> SWEDISH
4527 currentState.g0Charset = CharacterSet.NRC_SWEDISH;
4528 }
bd8d51fa
KL
4529 if ((collectBuffer.length() == 1)
4530 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4531 // G1 --> SWEDISH
4532 currentState.g1Charset = CharacterSet.NRC_SWEDISH;
4533 }
bd8d51fa
KL
4534 if ((collectBuffer.length() == 1)
4535 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4536 // G2 --> SWEDISH
4537 currentState.g2Charset = CharacterSet.NRC_SWEDISH;
4538 }
bd8d51fa
KL
4539 if ((collectBuffer.length() == 1)
4540 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4541 // G3 --> SWEDISH
4542 currentState.g3Charset = CharacterSet.NRC_SWEDISH;
4543 }
4544 }
4545 break;
4546 case 'I':
4547 case 'J':
4548 break;
4549 case 'K':
4550 if ((type == DeviceType.VT220)
4551 || (type == DeviceType.XTERM)) {
4552
bd8d51fa
KL
4553 if ((collectBuffer.length() == 1)
4554 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4555 // G0 --> GERMAN
4556 currentState.g0Charset = CharacterSet.NRC_GERMAN;
4557 }
bd8d51fa
KL
4558 if ((collectBuffer.length() == 1)
4559 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4560 // G1 --> GERMAN
4561 currentState.g1Charset = CharacterSet.NRC_GERMAN;
4562 }
bd8d51fa
KL
4563 if ((collectBuffer.length() == 1)
4564 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4565 // G2 --> GERMAN
4566 currentState.g2Charset = CharacterSet.NRC_GERMAN;
4567 }
bd8d51fa
KL
4568 if ((collectBuffer.length() == 1)
4569 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4570 // G3 --> GERMAN
4571 currentState.g3Charset = CharacterSet.NRC_GERMAN;
4572 }
4573 }
4574 break;
4575 case 'L':
4576 case 'M':
4577 case 'N':
4578 case 'O':
4579 case 'P':
4580 break;
4581 case 'Q':
4582 if ((type == DeviceType.VT220)
4583 || (type == DeviceType.XTERM)) {
4584
bd8d51fa
KL
4585 if ((collectBuffer.length() == 1)
4586 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4587 // G0 --> FRENCH_CA
4588 currentState.g0Charset = CharacterSet.NRC_FRENCH_CA;
4589 }
bd8d51fa
KL
4590 if ((collectBuffer.length() == 1)
4591 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4592 // G1 --> FRENCH_CA
4593 currentState.g1Charset = CharacterSet.NRC_FRENCH_CA;
4594 }
bd8d51fa
KL
4595 if ((collectBuffer.length() == 1)
4596 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4597 // G2 --> FRENCH_CA
4598 currentState.g2Charset = CharacterSet.NRC_FRENCH_CA;
4599 }
bd8d51fa
KL
4600 if ((collectBuffer.length() == 1)
4601 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4602 // G3 --> FRENCH_CA
4603 currentState.g3Charset = CharacterSet.NRC_FRENCH_CA;
4604 }
4605 }
4606 break;
4607 case 'R':
4608 if ((type == DeviceType.VT220)
4609 || (type == DeviceType.XTERM)) {
4610
bd8d51fa
KL
4611 if ((collectBuffer.length() == 1)
4612 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4613 // G0 --> FRENCH
4614 currentState.g0Charset = CharacterSet.NRC_FRENCH;
4615 }
bd8d51fa
KL
4616 if ((collectBuffer.length() == 1)
4617 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4618 // G1 --> FRENCH
4619 currentState.g1Charset = CharacterSet.NRC_FRENCH;
4620 }
bd8d51fa
KL
4621 if ((collectBuffer.length() == 1)
4622 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4623 // G2 --> FRENCH
4624 currentState.g2Charset = CharacterSet.NRC_FRENCH;
4625 }
bd8d51fa
KL
4626 if ((collectBuffer.length() == 1)
4627 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4628 // G3 --> FRENCH
4629 currentState.g3Charset = CharacterSet.NRC_FRENCH;
4630 }
4631 }
4632 break;
4633 case 'S':
4634 case 'T':
4635 case 'U':
4636 case 'V':
4637 case 'W':
4638 case 'X':
4639 break;
4640 case 'Y':
4641 if ((type == DeviceType.VT220)
4642 || (type == DeviceType.XTERM)) {
4643
bd8d51fa
KL
4644 if ((collectBuffer.length() == 1)
4645 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4646 // G0 --> ITALIAN
4647 currentState.g0Charset = CharacterSet.NRC_ITALIAN;
4648 }
bd8d51fa
KL
4649 if ((collectBuffer.length() == 1)
4650 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4651 // G1 --> ITALIAN
4652 currentState.g1Charset = CharacterSet.NRC_ITALIAN;
4653 }
bd8d51fa
KL
4654 if ((collectBuffer.length() == 1)
4655 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4656 // G2 --> ITALIAN
4657 currentState.g2Charset = CharacterSet.NRC_ITALIAN;
4658 }
bd8d51fa
KL
4659 if ((collectBuffer.length() == 1)
4660 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4661 // G3 --> ITALIAN
4662 currentState.g3Charset = CharacterSet.NRC_ITALIAN;
4663 }
4664 }
4665 break;
4666 case 'Z':
4667 if ((type == DeviceType.VT220)
4668 || (type == DeviceType.XTERM)) {
4669
bd8d51fa
KL
4670 if ((collectBuffer.length() == 1)
4671 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
4672 // G0 --> SPANISH
4673 currentState.g0Charset = CharacterSet.NRC_SPANISH;
4674 }
bd8d51fa
KL
4675 if ((collectBuffer.length() == 1)
4676 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
4677 // G1 --> SPANISH
4678 currentState.g1Charset = CharacterSet.NRC_SPANISH;
4679 }
bd8d51fa
KL
4680 if ((collectBuffer.length() == 1)
4681 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
4682 // G2 --> SPANISH
4683 currentState.g2Charset = CharacterSet.NRC_SPANISH;
4684 }
bd8d51fa
KL
4685 if ((collectBuffer.length() == 1)
4686 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
4687 // G3 --> SPANISH
4688 currentState.g3Charset = CharacterSet.NRC_SPANISH;
4689 }
4690 }
4691 break;
4692 case '[':
4693 case '\\':
4694 case ']':
4695 case '^':
4696 case '_':
4697 case '`':
4698 case 'a':
4699 case 'b':
4700 case 'c':
4701 case 'd':
4702 case 'e':
4703 case 'f':
4704 case 'g':
4705 case 'h':
4706 case 'i':
4707 case 'j':
4708 case 'k':
4709 case 'l':
4710 case 'm':
4711 case 'n':
4712 case 'o':
4713 case 'p':
4714 case 'q':
4715 case 'r':
4716 case 's':
4717 case 't':
4718 case 'u':
4719 case 'v':
4720 case 'w':
4721 case 'x':
4722 case 'y':
4723 case 'z':
4724 case '{':
4725 case '|':
4726 case '}':
4727 case '~':
4728 break;
4729 }
4730 toGround();
4731 }
4732
4733 // 7F --> ignore
4734
4735 // 0x9C goes to GROUND
4736 if (ch == 0x9C) {
4737 toGround();
4738 }
4739
4740 return;
4741
4742 case CSI_ENTRY:
4743 // 00-17, 19, 1C-1F --> execute
4744 if (ch <= 0x1F) {
4745 handleControlChar(ch);
4746 }
4747
4748 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
4749 if ((ch >= 0x20) && (ch <= 0x2F)) {
4750 collect(ch);
4751 scanState = ScanState.CSI_INTERMEDIATE;
4752 }
4753
4754 // 30-39, 3B --> param, then switch to CSI_PARAM
4755 if ((ch >= '0') && (ch <= '9')) {
4756 param((byte) ch);
4757 scanState = ScanState.CSI_PARAM;
4758 }
4759 if (ch == ';') {
4760 param((byte) ch);
4761 scanState = ScanState.CSI_PARAM;
4762 }
4763
4764 // 3C-3F --> collect, then switch to CSI_PARAM
4765 if ((ch >= 0x3C) && (ch <= 0x3F)) {
4766 collect(ch);
4767 scanState = ScanState.CSI_PARAM;
4768 }
4769
4770 // 40-7E --> dispatch, then switch to GROUND
4771 if ((ch >= 0x40) && (ch <= 0x7E)) {
4772 switch (ch) {
4773 case '@':
4774 // ICH - Insert character
4775 ich();
4776 break;
4777 case 'A':
4778 // CUU - Cursor up
4779 cuu();
4780 break;
4781 case 'B':
4782 // CUD - Cursor down
4783 cud();
4784 break;
4785 case 'C':
4786 // CUF - Cursor forward
4787 cuf();
4788 break;
4789 case 'D':
4790 // CUB - Cursor backward
4791 cub();
4792 break;
4793 case 'E':
4794 // CNL - Cursor down and to column 1
4795 if (type == DeviceType.XTERM) {
4796 cnl();
4797 }
4798 break;
4799 case 'F':
4800 // CPL - Cursor up and to column 1
4801 if (type == DeviceType.XTERM) {
4802 cpl();
4803 }
4804 break;
4805 case 'G':
4806 // CHA - Cursor to column # in current row
4807 if (type == DeviceType.XTERM) {
4808 cha();
4809 }
4810 break;
4811 case 'H':
4812 // CUP - Cursor position
4813 cup();
4814 break;
4815 case 'I':
4816 // CHT - Cursor forward X tab stops (default 1)
4817 if (type == DeviceType.XTERM) {
4818 cht();
4819 }
4820 break;
4821 case 'J':
4822 // ED - Erase in display
4823 ed();
4824 break;
4825 case 'K':
4826 // EL - Erase in line
4827 el();
4828 break;
4829 case 'L':
4830 // IL - Insert line
4831 il();
4832 break;
4833 case 'M':
4834 // DL - Delete line
4835 dl();
4836 break;
4837 case 'N':
4838 case 'O':
4839 break;
4840 case 'P':
4841 // DCH - Delete character
4842 dch();
4843 break;
4844 case 'Q':
4845 case 'R':
4846 break;
4847 case 'S':
4848 // Scroll up X lines (default 1)
4849 if (type == DeviceType.XTERM) {
4850 su();
4851 }
4852 break;
4853 case 'T':
4854 // Scroll down X lines (default 1)
4855 if (type == DeviceType.XTERM) {
4856 sd();
4857 }
4858 break;
4859 case 'U':
4860 case 'V':
4861 case 'W':
4862 break;
4863 case 'X':
4864 if ((type == DeviceType.VT220)
4865 || (type == DeviceType.XTERM)) {
4866
4867 // ECH - Erase character
4868 ech();
4869 }
4870 break;
4871 case 'Y':
4872 break;
4873 case 'Z':
4874 // CBT - Cursor backward X tab stops (default 1)
4875 if (type == DeviceType.XTERM) {
4876 cbt();
4877 }
4878 break;
4879 case '[':
4880 case '\\':
4881 case ']':
4882 case '^':
4883 case '_':
4884 break;
4885 case '`':
4886 // HPA - Cursor to column # in current row. Same as CHA
4887 if (type == DeviceType.XTERM) {
4888 cha();
4889 }
4890 break;
4891 case 'a':
4892 // HPR - Cursor right. Same as CUF
4893 if (type == DeviceType.XTERM) {
4894 cuf();
4895 }
4896 break;
4897 case 'b':
4898 // REP - Repeat last char X times
4899 if (type == DeviceType.XTERM) {
4900 rep();
4901 }
4902 break;
4903 case 'c':
4904 // DA - Device attributes
4905 da();
4906 break;
4907 case 'd':
4908 // VPA - Cursor to row, current column.
4909 if (type == DeviceType.XTERM) {
4910 vpa();
4911 }
4912 break;
4913 case 'e':
4914 // VPR - Cursor down. Same as CUD
4915 if (type == DeviceType.XTERM) {
4916 cud();
4917 }
4918 break;
4919 case 'f':
4920 // HVP - Horizontal and vertical position
4921 hvp();
4922 break;
4923 case 'g':
4924 // TBC - Tabulation clear
4925 tbc();
4926 break;
4927 case 'h':
4928 // Sets an ANSI or DEC private toggle
4929 setToggle(true);
4930 break;
4931 case 'i':
4932 if ((type == DeviceType.VT220)
4933 || (type == DeviceType.XTERM)) {
4934
4935 // Printer functions
4936 printerFunctions();
4937 }
4938 break;
4939 case 'j':
4940 case 'k':
4941 break;
4942 case 'l':
4943 // Sets an ANSI or DEC private toggle
4944 setToggle(false);
4945 break;
4946 case 'm':
4947 // SGR - Select graphics rendition
4948 sgr();
4949 break;
4950 case 'n':
4951 // DSR - Device status report
4952 dsr();
4953 break;
4954 case 'o':
4955 case 'p':
4956 break;
4957 case 'q':
4958 // DECLL - Load leds
4959 // Not supported
4960 break;
4961 case 'r':
4962 // DECSTBM - Set top and bottom margins
4963 decstbm();
4964 break;
4965 case 's':
4966 // Save cursor (ANSI.SYS)
4967 if (type == DeviceType.XTERM) {
4968 savedState.cursorX = currentState.cursorX;
4969 savedState.cursorY = currentState.cursorY;
4970 }
4971 break;
4972 case 't':
4973 break;
4974 case 'u':
4975 // Restore cursor (ANSI.SYS)
4976 if (type == DeviceType.XTERM) {
4977 cursorPosition(savedState.cursorY, savedState.cursorX);
4978 }
4979 break;
4980 case 'v':
4981 case 'w':
4982 break;
4983 case 'x':
4984 // DECREQTPARM - Request terminal parameters
4985 decreqtparm();
4986 break;
4987 case 'y':
4988 case 'z':
4989 case '{':
4990 case '|':
4991 case '}':
4992 case '~':
4993 break;
4994 }
4995 toGround();
4996 }
4997
4998 // 7F --> ignore
4999
5000 // 0x9C goes to GROUND
5001 if (ch == 0x9C) {
5002 toGround();
5003 }
5004
5005 // 0x3A goes to CSI_IGNORE
5006 if (ch == 0x3A) {
5007 scanState = ScanState.CSI_IGNORE;
5008 }
5009 return;
5010
5011 case CSI_PARAM:
5012 // 00-17, 19, 1C-1F --> execute
5013 if (ch <= 0x1F) {
5014 handleControlChar(ch);
5015 }
5016
5017 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5018 if ((ch >= 0x20) && (ch <= 0x2F)) {
5019 collect(ch);
5020 scanState = ScanState.CSI_INTERMEDIATE;
5021 }
5022
5023 // 30-39, 3B --> param
5024 if ((ch >= '0') && (ch <= '9')) {
5025 param((byte) ch);
5026 }
5027 if (ch == ';') {
5028 param((byte) ch);
5029 }
5030
5031 // 0x3A goes to CSI_IGNORE
5032 if (ch == 0x3A) {
5033 scanState = ScanState.CSI_IGNORE;
5034 }
5035 // 0x3C-3F goes to CSI_IGNORE
5036 if ((ch >= 0x3C) && (ch <= 0x3F)) {
5037 scanState = ScanState.CSI_IGNORE;
5038 }
5039
5040 // 40-7E --> dispatch, then switch to GROUND
5041 if ((ch >= 0x40) && (ch <= 0x7E)) {
5042 switch (ch) {
5043 case '@':
5044 // ICH - Insert character
5045 ich();
5046 break;
5047 case 'A':
5048 // CUU - Cursor up
5049 cuu();
5050 break;
5051 case 'B':
5052 // CUD - Cursor down
5053 cud();
5054 break;
5055 case 'C':
5056 // CUF - Cursor forward
5057 cuf();
5058 break;
5059 case 'D':
5060 // CUB - Cursor backward
5061 cub();
5062 break;
5063 case 'E':
5064 // CNL - Cursor down and to column 1
5065 if (type == DeviceType.XTERM) {
5066 cnl();
5067 }
5068 break;
5069 case 'F':
5070 // CPL - Cursor up and to column 1
5071 if (type == DeviceType.XTERM) {
5072 cpl();
5073 }
5074 break;
5075 case 'G':
5076 // CHA - Cursor to column # in current row
5077 if (type == DeviceType.XTERM) {
5078 cha();
5079 }
5080 break;
5081 case 'H':
5082 // CUP - Cursor position
5083 cup();
5084 break;
5085 case 'I':
5086 // CHT - Cursor forward X tab stops (default 1)
5087 if (type == DeviceType.XTERM) {
5088 cht();
5089 }
5090 break;
5091 case 'J':
5092 // ED - Erase in display
5093 ed();
5094 break;
5095 case 'K':
5096 // EL - Erase in line
5097 el();
5098 break;
5099 case 'L':
5100 // IL - Insert line
5101 il();
5102 break;
5103 case 'M':
5104 // DL - Delete line
5105 dl();
5106 break;
5107 case 'N':
5108 case 'O':
5109 break;
5110 case 'P':
5111 // DCH - Delete character
5112 dch();
5113 break;
5114 case 'Q':
5115 case 'R':
5116 break;
5117 case 'S':
5118 // Scroll up X lines (default 1)
5119 if (type == DeviceType.XTERM) {
5120 su();
5121 }
5122 break;
5123 case 'T':
5124 // Scroll down X lines (default 1)
5125 if (type == DeviceType.XTERM) {
5126 sd();
5127 }
5128 break;
5129 case 'U':
5130 case 'V':
5131 case 'W':
5132 break;
5133 case 'X':
5134 if ((type == DeviceType.VT220)
5135 || (type == DeviceType.XTERM)) {
5136
5137 // ECH - Erase character
5138 ech();
5139 }
5140 break;
5141 case 'Y':
5142 break;
5143 case 'Z':
5144 // CBT - Cursor backward X tab stops (default 1)
5145 if (type == DeviceType.XTERM) {
5146 cbt();
5147 }
5148 break;
5149 case '[':
5150 case '\\':
5151 case ']':
5152 case '^':
5153 case '_':
5154 break;
5155 case '`':
5156 // HPA - Cursor to column # in current row. Same as CHA
5157 if (type == DeviceType.XTERM) {
5158 cha();
5159 }
5160 break;
5161 case 'a':
5162 // HPR - Cursor right. Same as CUF
5163 if (type == DeviceType.XTERM) {
5164 cuf();
5165 }
5166 break;
5167 case 'b':
5168 // REP - Repeat last char X times
5169 if (type == DeviceType.XTERM) {
5170 rep();
5171 }
5172 break;
5173 case 'c':
5174 // DA - Device attributes
5175 da();
5176 break;
5177 case 'd':
5178 // VPA - Cursor to row, current column.
5179 if (type == DeviceType.XTERM) {
5180 vpa();
5181 }
5182 break;
5183 case 'e':
5184 // VPR - Cursor down. Same as CUD
5185 if (type == DeviceType.XTERM) {
5186 cud();
5187 }
5188 break;
5189 case 'f':
5190 // HVP - Horizontal and vertical position
5191 hvp();
5192 break;
5193 case 'g':
5194 // TBC - Tabulation clear
5195 tbc();
5196 break;
5197 case 'h':
5198 // Sets an ANSI or DEC private toggle
5199 setToggle(true);
5200 break;
5201 case 'i':
5202 if ((type == DeviceType.VT220)
5203 || (type == DeviceType.XTERM)) {
5204
5205 // Printer functions
5206 printerFunctions();
5207 }
5208 break;
5209 case 'j':
5210 case 'k':
5211 break;
5212 case 'l':
5213 // Sets an ANSI or DEC private toggle
5214 setToggle(false);
5215 break;
5216 case 'm':
5217 // SGR - Select graphics rendition
5218 sgr();
5219 break;
5220 case 'n':
5221 // DSR - Device status report
5222 dsr();
5223 break;
5224 case 'o':
5225 case 'p':
5226 break;
5227 case 'q':
5228 // DECLL - Load leds
5229 // Not supported
5230 break;
5231 case 'r':
5232 // DECSTBM - Set top and bottom margins
5233 decstbm();
5234 break;
5235 case 's':
5236 case 't':
5237 case 'u':
5238 case 'v':
5239 case 'w':
5240 break;
5241 case 'x':
5242 // DECREQTPARM - Request terminal parameters
5243 decreqtparm();
5244 break;
5245 case 'y':
5246 case 'z':
5247 case '{':
5248 case '|':
5249 case '}':
5250 case '~':
5251 break;
5252 }
5253 toGround();
5254 }
5255
5256 // 7F --> ignore
5257 return;
5258
5259 case CSI_INTERMEDIATE:
5260 // 00-17, 19, 1C-1F --> execute
5261 if (ch <= 0x1F) {
5262 handleControlChar(ch);
5263 }
5264
5265 // 20-2F --> collect
5266 if ((ch >= 0x20) && (ch <= 0x2F)) {
5267 collect(ch);
5268 }
5269
5270 // 0x30-3F goes to CSI_IGNORE
5271 if ((ch >= 0x30) && (ch <= 0x3F)) {
5272 scanState = ScanState.CSI_IGNORE;
5273 }
5274
5275 // 40-7E --> dispatch, then switch to GROUND
5276 if ((ch >= 0x40) && (ch <= 0x7E)) {
5277 switch (ch) {
5278 case '@':
5279 case 'A':
5280 case 'B':
5281 case 'C':
5282 case 'D':
5283 case 'E':
5284 case 'F':
5285 case 'G':
5286 case 'H':
5287 case 'I':
5288 case 'J':
5289 case 'K':
5290 case 'L':
5291 case 'M':
5292 case 'N':
5293 case 'O':
5294 case 'P':
5295 case 'Q':
5296 case 'R':
5297 case 'S':
5298 case 'T':
5299 case 'U':
5300 case 'V':
5301 case 'W':
5302 case 'X':
5303 case 'Y':
5304 case 'Z':
5305 case '[':
5306 case '\\':
5307 case ']':
5308 case '^':
5309 case '_':
5310 case '`':
5311 case 'a':
5312 case 'b':
5313 case 'c':
5314 case 'd':
5315 case 'e':
5316 case 'f':
5317 case 'g':
5318 case 'h':
5319 case 'i':
5320 case 'j':
5321 case 'k':
5322 case 'l':
5323 case 'm':
5324 case 'n':
5325 case 'o':
5326 break;
5327 case 'p':
5328 if (((type == DeviceType.VT220)
5329 || (type == DeviceType.XTERM))
bd8d51fa 5330 && (collectBuffer.charAt(collectBuffer.length() - 1) == '\"')
34a42e78
KL
5331 ) {
5332 // DECSCL - compatibility level
5333 decscl();
5334 }
5335 if ((type == DeviceType.XTERM)
bd8d51fa 5336 && (collectBuffer.charAt(collectBuffer.length() - 1) == '!')
34a42e78
KL
5337 ) {
5338 // DECSTR - Soft terminal reset
5339 decstr();
5340 }
5341 break;
5342 case 'q':
5343 if (((type == DeviceType.VT220)
5344 || (type == DeviceType.XTERM))
bd8d51fa 5345 && (collectBuffer.charAt(collectBuffer.length() - 1) == '\"')
34a42e78
KL
5346 ) {
5347 // DECSCA
5348 decsca();
5349 }
5350 break;
5351 case 'r':
5352 case 's':
5353 case 't':
5354 case 'u':
5355 case 'v':
5356 case 'w':
5357 case 'x':
5358 case 'y':
5359 case 'z':
5360 case '{':
5361 case '|':
5362 case '}':
5363 case '~':
5364 break;
5365 }
5366 toGround();
5367 }
5368
5369 // 7F --> ignore
5370 return;
5371
5372 case CSI_IGNORE:
5373 // 00-17, 19, 1C-1F --> execute
5374 if (ch <= 0x1F) {
5375 handleControlChar(ch);
5376 }
5377
5378 // 20-2F --> collect
5379 if ((ch >= 0x20) && (ch <= 0x2F)) {
5380 collect(ch);
5381 }
5382
5383 // 40-7E --> ignore, then switch to GROUND
5384 if ((ch >= 0x40) && (ch <= 0x7E)) {
5385 toGround();
5386 }
5387
5388 // 20-3F, 7F --> ignore
5389
5390 return;
5391
5392 case DCS_ENTRY:
5393
5394 // 0x9C goes to GROUND
5395 if (ch == 0x9C) {
5396 toGround();
5397 }
5398
5399 // 0x1B 0x5C goes to GROUND
5400 if (ch == 0x1B) {
5401 collect(ch);
5402 }
5403 if (ch == 0x5C) {
bd8d51fa
KL
5404 if ((collectBuffer.length() > 0)
5405 && (collectBuffer.charAt(collectBuffer.length() - 1) == 0x1B)
5406 ) {
34a42e78
KL
5407 toGround();
5408 }
5409 }
5410
5411 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5412 if ((ch >= 0x20) && (ch <= 0x2F)) {
5413 collect(ch);
5414 scanState = ScanState.DCS_INTERMEDIATE;
5415 }
5416
5417 // 30-39, 3B --> param, then switch to DCS_PARAM
5418 if ((ch >= '0') && (ch <= '9')) {
5419 param((byte) ch);
5420 scanState = ScanState.DCS_PARAM;
5421 }
5422 if (ch == ';') {
5423 param((byte) ch);
5424 scanState = ScanState.DCS_PARAM;
5425 }
5426
5427 // 3C-3F --> collect, then switch to DCS_PARAM
5428 if ((ch >= 0x3C) && (ch <= 0x3F)) {
5429 collect(ch);
5430 scanState = ScanState.DCS_PARAM;
5431 }
5432
5433 // 00-17, 19, 1C-1F, 7F --> ignore
5434
5435 // 0x3A goes to DCS_IGNORE
5436 if (ch == 0x3F) {
5437 scanState = ScanState.DCS_IGNORE;
5438 }
5439
5440 // 0x40-7E goes to DCS_PASSTHROUGH
5441 if ((ch >= 0x40) && (ch <= 0x7E)) {
5442 scanState = ScanState.DCS_PASSTHROUGH;
5443 }
5444 return;
5445
5446 case DCS_INTERMEDIATE:
5447
5448 // 0x9C goes to GROUND
5449 if (ch == 0x9C) {
5450 toGround();
5451 }
5452
5453 // 0x1B 0x5C goes to GROUND
5454 if (ch == 0x1B) {
5455 collect(ch);
5456 }
5457 if (ch == 0x5C) {
bd8d51fa
KL
5458 if ((collectBuffer.length() > 0) &&
5459 (collectBuffer.charAt(collectBuffer.length() - 1) == 0x1B)
5460 ) {
34a42e78
KL
5461 toGround();
5462 }
5463 }
5464
5465 // 0x30-3F goes to DCS_IGNORE
5466 if ((ch >= 0x30) && (ch <= 0x3F)) {
5467 scanState = ScanState.DCS_IGNORE;
5468 }
5469
5470 // 0x40-7E goes to DCS_PASSTHROUGH
5471 if ((ch >= 0x40) && (ch <= 0x7E)) {
5472 scanState = ScanState.DCS_PASSTHROUGH;
5473 }
5474
5475 // 00-17, 19, 1C-1F, 7F --> ignore
5476 return;
5477
5478 case DCS_PARAM:
5479
5480 // 0x9C goes to GROUND
5481 if (ch == 0x9C) {
5482 toGround();
5483 }
5484
5485 // 0x1B 0x5C goes to GROUND
5486 if (ch == 0x1B) {
5487 collect(ch);
5488 }
5489 if (ch == 0x5C) {
bd8d51fa
KL
5490 if ((collectBuffer.length() > 0) &&
5491 (collectBuffer.charAt(collectBuffer.length() - 1) == 0x1B)
5492 ) {
34a42e78
KL
5493 toGround();
5494 }
5495 }
5496
5497 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5498 if ((ch >= 0x20) && (ch <= 0x2F)) {
5499 collect(ch);
5500 scanState = ScanState.DCS_INTERMEDIATE;
5501 }
5502
5503 // 30-39, 3B --> param
5504 if ((ch >= '0') && (ch <= '9')) {
5505 param((byte) ch);
5506 }
5507 if (ch == ';') {
5508 param((byte) ch);
5509 }
5510
5511 // 00-17, 19, 1C-1F, 7F --> ignore
5512
5513 // 0x3A, 3C-3F goes to DCS_IGNORE
5514 if (ch == 0x3F) {
5515 scanState = ScanState.DCS_IGNORE;
5516 }
5517 if ((ch >= 0x3C) && (ch <= 0x3F)) {
5518 scanState = ScanState.DCS_IGNORE;
5519 }
5520
5521 // 0x40-7E goes to DCS_PASSTHROUGH
5522 if ((ch >= 0x40) && (ch <= 0x7E)) {
5523 scanState = ScanState.DCS_PASSTHROUGH;
5524 }
5525 return;
5526
5527 case DCS_PASSTHROUGH:
5528 // 0x9C goes to GROUND
5529 if (ch == 0x9C) {
5530 toGround();
5531 }
5532
5533 // 0x1B 0x5C goes to GROUND
5534 if (ch == 0x1B) {
5535 collect(ch);
5536 }
5537 if (ch == 0x5C) {
bd8d51fa
KL
5538 if ((collectBuffer.length() > 0)
5539 && (collectBuffer.charAt(collectBuffer.length() - 1) == 0x1B)
34a42e78
KL
5540 ) {
5541 toGround();
5542 }
5543 }
5544
5545 // 00-17, 19, 1C-1F, 20-7E --> put
5546 // TODO
5547 if (ch <= 0x17) {
5548 return;
5549 }
5550 if (ch == 0x19) {
5551 return;
5552 }
5553 if ((ch >= 0x1C) && (ch <= 0x1F)) {
5554 return;
5555 }
5556 if ((ch >= 0x20) && (ch <= 0x7E)) {
5557 return;
5558 }
5559
5560 // 7F --> ignore
5561
5562 return;
5563
5564 case DCS_IGNORE:
5565 // 00-17, 19, 1C-1F, 20-7F --> ignore
5566
5567 // 0x9C goes to GROUND
5568 if (ch == 0x9C) {
5569 toGround();
5570 }
5571
5572 return;
5573
5574 case SOSPMAPC_STRING:
5575 // 00-17, 19, 1C-1F, 20-7F --> ignore
5576
5577 // 0x9C goes to GROUND
5578 if (ch == 0x9C) {
5579 toGround();
5580 }
5581
5582 return;
5583
5584 case OSC_STRING:
5585 // Special case for Xterm: OSC can pass control characters
5586 if ((ch == 0x9C) || (ch <= 0x07)) {
5587 oscPut(ch);
5588 }
5589
5590 // 00-17, 19, 1C-1F --> ignore
5591
5592 // 20-7F --> osc_put
5593 if ((ch >= 0x20) && (ch <= 0x7F)) {
5594 oscPut(ch);
5595 }
5596
5597 // 0x9C goes to GROUND
5598 if (ch == 0x9C) {
5599 toGround();
5600 }
5601
5602 return;
5603
5604 case VT52_DIRECT_CURSOR_ADDRESS:
5605 // This is a special case for the VT52 sequence "ESC Y l c"
bd8d51fa 5606 if (collectBuffer.length() == 0) {
34a42e78 5607 collect(ch);
bd8d51fa 5608 } else if (collectBuffer.length() == 1) {
34a42e78
KL
5609 // We've got the two characters, one in the buffer and the
5610 // other in ch.
bd8d51fa 5611 cursorPosition(collectBuffer.charAt(0) - '\040', ch - '\040');
34a42e78
KL
5612 toGround();
5613 }
5614 return;
5615 }
5616
5617 }
5618
5619 /**
5620 * Expose current cursor X to outside world.
5621 *
5622 * @return current cursor X
5623 */
5624 public final int getCursorX() {
bd8d51fa
KL
5625 if (display.get(currentState.cursorY).isDoubleWidth()) {
5626 return currentState.cursorX * 2;
5627 }
34a42e78
KL
5628 return currentState.cursorX;
5629 }
5630
5631 /**
5632 * Expose current cursor Y to outside world.
5633 *
5634 * @return current cursor Y
5635 */
5636 public final int getCursorY() {
5637 return currentState.cursorY;
5638 }
5639
5640 /**
5641 * Read function runs on a separate thread.
5642 */
5643 public void run() {
5644 boolean utf8 = false;
5645 boolean done = false;
5646
5647 if (type == DeviceType.XTERM) {
5648 utf8 = true;
5649 }
5650
5651 // available() will often return > 1, so we need to read in chunks to
5652 // stay caught up.
5653 char [] readBufferUTF8 = null;
5654 byte [] readBuffer = null;
5655 if (utf8) {
5656 readBufferUTF8 = new char[128];
5657 } else {
5658 readBuffer = new byte[128];
5659 }
5660
5661 while (!done && !stopReaderThread) {
5662 try {
34a42e78 5663 int n = inputStream.available();
bb35d919
KL
5664 // System.err.printf("available() %d\n", n); System.err.flush();
5665 if (utf8) {
5666 if (readBufferUTF8.length < n) {
5667 // The buffer wasn't big enough, make it huger
5668 int newSizeHalf = Math.max(readBufferUTF8.length, n);
34a42e78 5669
bb35d919 5670 readBufferUTF8 = new char[newSizeHalf * 2];
34a42e78 5671 }
bb35d919
KL
5672 } else {
5673 if (readBuffer.length < n) {
5674 // The buffer wasn't big enough, make it huger
5675 int newSizeHalf = Math.max(readBuffer.length, n);
5676 readBuffer = new byte[newSizeHalf * 2];
34a42e78 5677 }
bb35d919
KL
5678 }
5679
5680 int rc = -1;
5681 if (utf8) {
5682 rc = input.read(readBufferUTF8, 0,
5683 readBufferUTF8.length);
34a42e78 5684 } else {
bb35d919
KL
5685 rc = inputStream.read(readBuffer, 0,
5686 readBuffer.length);
5687 }
5688 // System.err.printf("read() %d\n", rc); System.err.flush();
5689 if (rc == -1) {
5690 // This is EOF
5691 done = true;
5692 } else {
5693 for (int i = 0; i < rc; i++) {
5694 int ch = 0;
5695 if (utf8) {
5696 ch = readBufferUTF8[i];
5697 } else {
5698 ch = readBuffer[i];
5699 }
5700 // Don't step on UI events
5701 synchronized (this) {
5702 consume((char)ch);
5703 }
5704 }
34a42e78
KL
5705 }
5706 // System.err.println("end while loop"); System.err.flush();
34a42e78
KL
5707 } catch (IOException e) {
5708 e.printStackTrace();
5709 done = true;
5710 }
5711 } // while ((done == false) && (stopReaderThread == false))
5712
34a42e78
KL
5713 // Let the rest of the world know that I am done.
5714 stopReaderThread = true;
5715
5716 // System.err.println("*** run() exiting..."); System.err.flush();
5717 }
5718
5719}