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