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