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