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