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