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