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